Fix line dimensions from line-height property
[emacs.git] / src / xdisp.c
blob29b97abcb951c04616a6d90d30d4f42c13496af6
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 int normal_char_height (struct font *);
837 static void append_stretch_glyph (struct it *, Lisp_Object,
838 int, int, int);
841 #endif /* HAVE_WINDOW_SYSTEM */
843 static void produce_special_glyphs (struct it *, enum display_element_type);
844 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
845 static bool coords_in_mouse_face_p (struct window *, int, int);
849 /***********************************************************************
850 Window display dimensions
851 ***********************************************************************/
853 /* Return the bottom boundary y-position for text lines in window W.
854 This is the first y position at which a line cannot start.
855 It is relative to the top of the window.
857 This is the height of W minus the height of a mode line, if any. */
860 window_text_bottom_y (struct window *w)
862 int height = WINDOW_PIXEL_HEIGHT (w);
864 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
866 if (WINDOW_WANTS_MODELINE_P (w))
867 height -= CURRENT_MODE_LINE_HEIGHT (w);
869 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
871 return height;
874 /* Return the pixel width of display area AREA of window W.
875 ANY_AREA means return the total width of W, not including
876 fringes to the left and right of the window. */
879 window_box_width (struct window *w, enum glyph_row_area area)
881 int width = w->pixel_width;
883 if (!w->pseudo_window_p)
885 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
886 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
888 if (area == TEXT_AREA)
889 width -= (WINDOW_MARGINS_WIDTH (w)
890 + WINDOW_FRINGES_WIDTH (w));
891 else if (area == LEFT_MARGIN_AREA)
892 width = WINDOW_LEFT_MARGIN_WIDTH (w);
893 else if (area == RIGHT_MARGIN_AREA)
894 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
897 /* With wide margins, fringes, etc. we might end up with a negative
898 width, correct that here. */
899 return max (0, width);
903 /* Return the pixel height of the display area of window W, not
904 including mode lines of W, if any. */
907 window_box_height (struct window *w)
909 struct frame *f = XFRAME (w->frame);
910 int height = WINDOW_PIXEL_HEIGHT (w);
912 eassert (height >= 0);
914 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
915 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
917 /* Note: the code below that determines the mode-line/header-line
918 height is essentially the same as that contained in the macro
919 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
920 the appropriate glyph row has its `mode_line_p' flag set,
921 and if it doesn't, uses estimate_mode_line_height instead. */
923 if (WINDOW_WANTS_MODELINE_P (w))
925 struct glyph_row *ml_row
926 = (w->current_matrix && w->current_matrix->rows
927 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
928 : 0);
929 if (ml_row && ml_row->mode_line_p)
930 height -= ml_row->height;
931 else
932 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
935 if (WINDOW_WANTS_HEADER_LINE_P (w))
937 struct glyph_row *hl_row
938 = (w->current_matrix && w->current_matrix->rows
939 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
940 : 0);
941 if (hl_row && hl_row->mode_line_p)
942 height -= hl_row->height;
943 else
944 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
947 /* With a very small font and a mode-line that's taller than
948 default, we might end up with a negative height. */
949 return max (0, height);
952 /* Return the window-relative coordinate of the left edge of display
953 area AREA of window W. ANY_AREA means return the left edge of the
954 whole window, to the right of the left fringe of W. */
957 window_box_left_offset (struct window *w, enum glyph_row_area area)
959 int x;
961 if (w->pseudo_window_p)
962 return 0;
964 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
966 if (area == TEXT_AREA)
967 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
968 + window_box_width (w, LEFT_MARGIN_AREA));
969 else if (area == RIGHT_MARGIN_AREA)
970 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
971 + window_box_width (w, LEFT_MARGIN_AREA)
972 + window_box_width (w, TEXT_AREA)
973 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
975 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
976 else if (area == LEFT_MARGIN_AREA
977 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
978 x += WINDOW_LEFT_FRINGE_WIDTH (w);
980 /* Don't return more than the window's pixel width. */
981 return min (x, w->pixel_width);
985 /* Return the window-relative coordinate of the right edge of display
986 area AREA of window W. ANY_AREA means return the right edge of the
987 whole window, to the left of the right fringe of W. */
989 static int
990 window_box_right_offset (struct window *w, enum glyph_row_area area)
992 /* Don't return more than the window's pixel width. */
993 return min (window_box_left_offset (w, area) + window_box_width (w, area),
994 w->pixel_width);
997 /* Return the frame-relative coordinate of the left edge of display
998 area AREA of window W. ANY_AREA means return the left edge of the
999 whole window, to the right of the left fringe of W. */
1002 window_box_left (struct window *w, enum glyph_row_area area)
1004 struct frame *f = XFRAME (w->frame);
1005 int x;
1007 if (w->pseudo_window_p)
1008 return FRAME_INTERNAL_BORDER_WIDTH (f);
1010 x = (WINDOW_LEFT_EDGE_X (w)
1011 + window_box_left_offset (w, area));
1013 return x;
1017 /* Return the frame-relative coordinate of the right edge of display
1018 area AREA of window W. ANY_AREA means return the right edge of the
1019 whole window, to the left of the right fringe of W. */
1022 window_box_right (struct window *w, enum glyph_row_area area)
1024 return window_box_left (w, area) + window_box_width (w, area);
1027 /* Get the bounding box of the display area AREA of window W, without
1028 mode lines, in frame-relative coordinates. ANY_AREA means the
1029 whole window, not including the left and right fringes of
1030 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1031 coordinates of the upper-left corner of the box. Return in
1032 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1034 void
1035 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1036 int *box_y, int *box_width, int *box_height)
1038 if (box_width)
1039 *box_width = window_box_width (w, area);
1040 if (box_height)
1041 *box_height = window_box_height (w);
1042 if (box_x)
1043 *box_x = window_box_left (w, area);
1044 if (box_y)
1046 *box_y = WINDOW_TOP_EDGE_Y (w);
1047 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1052 #ifdef HAVE_WINDOW_SYSTEM
1054 /* Get the bounding box of the display area AREA of window W, without
1055 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1056 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1057 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1058 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1059 box. */
1061 static void
1062 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1063 int *bottom_right_x, int *bottom_right_y)
1065 window_box (w, ANY_AREA, top_left_x, top_left_y,
1066 bottom_right_x, bottom_right_y);
1067 *bottom_right_x += *top_left_x;
1068 *bottom_right_y += *top_left_y;
1071 #endif /* HAVE_WINDOW_SYSTEM */
1073 /***********************************************************************
1074 Utilities
1075 ***********************************************************************/
1077 /* Return the bottom y-position of the line the iterator IT is in.
1078 This can modify IT's settings. */
1081 line_bottom_y (struct it *it)
1083 int line_height = it->max_ascent + it->max_descent;
1084 int line_top_y = it->current_y;
1086 if (line_height == 0)
1088 if (last_height)
1089 line_height = last_height;
1090 else if (IT_CHARPOS (*it) < ZV)
1092 move_it_by_lines (it, 1);
1093 line_height = (it->max_ascent || it->max_descent
1094 ? it->max_ascent + it->max_descent
1095 : last_height);
1097 else
1099 struct glyph_row *row = it->glyph_row;
1101 /* Use the default character height. */
1102 it->glyph_row = NULL;
1103 it->what = IT_CHARACTER;
1104 it->c = ' ';
1105 it->len = 1;
1106 PRODUCE_GLYPHS (it);
1107 line_height = it->ascent + it->descent;
1108 it->glyph_row = row;
1112 return line_top_y + line_height;
1115 DEFUN ("line-pixel-height", Fline_pixel_height,
1116 Sline_pixel_height, 0, 0, 0,
1117 doc: /* Return height in pixels of text line in the selected window.
1119 Value is the height in pixels of the line at point. */)
1120 (void)
1122 struct it it;
1123 struct text_pos pt;
1124 struct window *w = XWINDOW (selected_window);
1125 struct buffer *old_buffer = NULL;
1126 Lisp_Object result;
1128 if (XBUFFER (w->contents) != current_buffer)
1130 old_buffer = current_buffer;
1131 set_buffer_internal_1 (XBUFFER (w->contents));
1133 SET_TEXT_POS (pt, PT, PT_BYTE);
1134 start_display (&it, w, pt);
1135 it.vpos = it.current_y = 0;
1136 last_height = 0;
1137 result = make_number (line_bottom_y (&it));
1138 if (old_buffer)
1139 set_buffer_internal_1 (old_buffer);
1141 return result;
1144 /* Return the default pixel height of text lines in window W. The
1145 value is the canonical height of the W frame's default font, plus
1146 any extra space required by the line-spacing variable or frame
1147 parameter.
1149 Implementation note: this ignores any line-spacing text properties
1150 put on the newline characters. This is because those properties
1151 only affect the _screen_ line ending in the newline (i.e., in a
1152 continued line, only the last screen line will be affected), which
1153 means only a small number of lines in a buffer can ever use this
1154 feature. Since this function is used to compute the default pixel
1155 equivalent of text lines in a window, we can safely ignore those
1156 few lines. For the same reasons, we ignore the line-height
1157 properties. */
1159 default_line_pixel_height (struct window *w)
1161 struct frame *f = WINDOW_XFRAME (w);
1162 int height = FRAME_LINE_HEIGHT (f);
1164 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1166 struct buffer *b = XBUFFER (w->contents);
1167 Lisp_Object val = BVAR (b, extra_line_spacing);
1169 if (NILP (val))
1170 val = BVAR (&buffer_defaults, extra_line_spacing);
1171 if (!NILP (val))
1173 if (RANGED_INTEGERP (0, val, INT_MAX))
1174 height += XFASTINT (val);
1175 else if (FLOATP (val))
1177 int addon = XFLOAT_DATA (val) * height + 0.5;
1179 if (addon >= 0)
1180 height += addon;
1183 else
1184 height += f->extra_line_spacing;
1187 return height;
1190 /* Subroutine of pos_visible_p below. Extracts a display string, if
1191 any, from the display spec given as its argument. */
1192 static Lisp_Object
1193 string_from_display_spec (Lisp_Object spec)
1195 if (CONSP (spec))
1197 while (CONSP (spec))
1199 if (STRINGP (XCAR (spec)))
1200 return XCAR (spec);
1201 spec = XCDR (spec);
1204 else if (VECTORP (spec))
1206 ptrdiff_t i;
1208 for (i = 0; i < ASIZE (spec); i++)
1210 if (STRINGP (AREF (spec, i)))
1211 return AREF (spec, i);
1213 return Qnil;
1216 return spec;
1220 /* Limit insanely large values of W->hscroll on frame F to the largest
1221 value that will still prevent first_visible_x and last_visible_x of
1222 'struct it' from overflowing an int. */
1223 static int
1224 window_hscroll_limited (struct window *w, struct frame *f)
1226 ptrdiff_t window_hscroll = w->hscroll;
1227 int window_text_width = window_box_width (w, TEXT_AREA);
1228 int colwidth = FRAME_COLUMN_WIDTH (f);
1230 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1231 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1233 return window_hscroll;
1236 /* Return true if position CHARPOS is visible in window W.
1237 CHARPOS < 0 means return info about WINDOW_END position.
1238 If visible, set *X and *Y to pixel coordinates of top left corner.
1239 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1240 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1242 bool
1243 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1244 int *rtop, int *rbot, int *rowh, int *vpos)
1246 struct it it;
1247 void *itdata = bidi_shelve_cache ();
1248 struct text_pos top;
1249 bool visible_p = false;
1250 struct buffer *old_buffer = NULL;
1251 bool r2l = false;
1253 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1254 return visible_p;
1256 if (XBUFFER (w->contents) != current_buffer)
1258 old_buffer = current_buffer;
1259 set_buffer_internal_1 (XBUFFER (w->contents));
1262 SET_TEXT_POS_FROM_MARKER (top, w->start);
1263 /* Scrolling a minibuffer window via scroll bar when the echo area
1264 shows long text sometimes resets the minibuffer contents behind
1265 our backs. */
1266 if (CHARPOS (top) > ZV)
1267 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 w->mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 w->header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1302 int bottom_y;
1303 struct it save_it;
1304 void *save_it_data = NULL;
1306 /* Calling line_bottom_y may change it.method, it.position, etc. */
1307 SAVE_IT (save_it, it, save_it_data);
1308 last_height = 0;
1309 bottom_y = line_bottom_y (&it);
1310 if (top_y < window_top_y)
1311 visible_p = bottom_y > window_top_y;
1312 else if (top_y < it.last_visible_y)
1313 visible_p = true;
1314 if (bottom_y >= it.last_visible_y
1315 && it.bidi_p && it.bidi_it.scan_dir == -1
1316 && IT_CHARPOS (it) < charpos)
1318 /* When the last line of the window is scanned backwards
1319 under bidi iteration, we could be duped into thinking
1320 that we have passed CHARPOS, when in fact move_it_to
1321 simply stopped short of CHARPOS because it reached
1322 last_visible_y. To see if that's what happened, we call
1323 move_it_to again with a slightly larger vertical limit,
1324 and see if it actually moved vertically; if it did, we
1325 didn't really reach CHARPOS, which is beyond window end. */
1326 /* Why 10? because we don't know how many canonical lines
1327 will the height of the next line(s) be. So we guess. */
1328 int ten_more_lines = 10 * default_line_pixel_height (w);
1330 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1331 MOVE_TO_POS | MOVE_TO_Y);
1332 if (it.current_y > top_y)
1333 visible_p = false;
1336 RESTORE_IT (&it, &save_it, save_it_data);
1337 if (visible_p)
1339 if (it.method == GET_FROM_DISPLAY_VECTOR)
1341 /* We stopped on the last glyph of a display vector.
1342 Try and recompute. Hack alert! */
1343 if (charpos < 2 || top.charpos >= charpos)
1344 top_x = it.glyph_row->x;
1345 else
1347 struct it it2, it2_prev;
1348 /* The idea is to get to the previous buffer
1349 position, consume the character there, and use
1350 the pixel coordinates we get after that. But if
1351 the previous buffer position is also displayed
1352 from a display vector, we need to consume all of
1353 the glyphs from that display vector. */
1354 start_display (&it2, w, top);
1355 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1356 /* If we didn't get to CHARPOS - 1, there's some
1357 replacing display property at that position, and
1358 we stopped after it. That is exactly the place
1359 whose coordinates we want. */
1360 if (IT_CHARPOS (it2) != charpos - 1)
1361 it2_prev = it2;
1362 else
1364 /* Iterate until we get out of the display
1365 vector that displays the character at
1366 CHARPOS - 1. */
1367 do {
1368 get_next_display_element (&it2);
1369 PRODUCE_GLYPHS (&it2);
1370 it2_prev = it2;
1371 set_iterator_to_next (&it2, true);
1372 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1373 && IT_CHARPOS (it2) < charpos);
1375 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1376 || it2_prev.current_x > it2_prev.last_visible_x)
1377 top_x = it.glyph_row->x;
1378 else
1380 top_x = it2_prev.current_x;
1381 top_y = it2_prev.current_y;
1385 else if (IT_CHARPOS (it) != charpos)
1387 Lisp_Object cpos = make_number (charpos);
1388 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1389 Lisp_Object string = string_from_display_spec (spec);
1390 struct text_pos tpos;
1391 bool newline_in_string
1392 = (STRINGP (string)
1393 && memchr (SDATA (string), '\n', SBYTES (string)));
1395 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1396 bool replacing_spec_p
1397 = (!NILP (spec)
1398 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1399 charpos, FRAME_WINDOW_P (it.f)));
1400 /* The tricky code below is needed because there's a
1401 discrepancy between move_it_to and how we set cursor
1402 when PT is at the beginning of a portion of text
1403 covered by a display property or an overlay with a
1404 display property, or the display line ends in a
1405 newline from a display string. move_it_to will stop
1406 _after_ such display strings, whereas
1407 set_cursor_from_row conspires with cursor_row_p to
1408 place the cursor on the first glyph produced from the
1409 display string. */
1411 /* We have overshoot PT because it is covered by a
1412 display property that replaces the text it covers.
1413 If the string includes embedded newlines, we are also
1414 in the wrong display line. Backtrack to the correct
1415 line, where the display property begins. */
1416 if (replacing_spec_p)
1418 Lisp_Object startpos, endpos;
1419 EMACS_INT start, end;
1420 struct it it3;
1422 /* Find the first and the last buffer positions
1423 covered by the display string. */
1424 endpos =
1425 Fnext_single_char_property_change (cpos, Qdisplay,
1426 Qnil, Qnil);
1427 startpos =
1428 Fprevious_single_char_property_change (endpos, Qdisplay,
1429 Qnil, Qnil);
1430 start = XFASTINT (startpos);
1431 end = XFASTINT (endpos);
1432 /* Move to the last buffer position before the
1433 display property. */
1434 start_display (&it3, w, top);
1435 if (start > CHARPOS (top))
1436 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* Move forward one more line if the position before
1438 the display string is a newline or if it is the
1439 rightmost character on a line that is
1440 continued or word-wrapped. */
1441 if (it3.method == GET_FROM_BUFFER
1442 && (it3.c == '\n'
1443 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1444 move_it_by_lines (&it3, 1);
1445 else if (move_it_in_display_line_to (&it3, -1,
1446 it3.current_x
1447 + it3.pixel_width,
1448 MOVE_TO_X)
1449 == MOVE_LINE_CONTINUED)
1451 move_it_by_lines (&it3, 1);
1452 /* When we are under word-wrap, the #$@%!
1453 move_it_by_lines moves 2 lines, so we need to
1454 fix that up. */
1455 if (it3.line_wrap == WORD_WRAP)
1456 move_it_by_lines (&it3, -1);
1459 /* Record the vertical coordinate of the display
1460 line where we wound up. */
1461 top_y = it3.current_y;
1462 if (it3.bidi_p)
1464 /* When characters are reordered for display,
1465 the character displayed to the left of the
1466 display string could be _after_ the display
1467 property in the logical order. Use the
1468 smallest vertical position of these two. */
1469 start_display (&it3, w, top);
1470 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1471 if (it3.current_y < top_y)
1472 top_y = it3.current_y;
1474 /* Move from the top of the window to the beginning
1475 of the display line where the display string
1476 begins. */
1477 start_display (&it3, w, top);
1478 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1479 /* If it3_moved stays false after the 'while' loop
1480 below, that means we already were at a newline
1481 before the loop (e.g., the display string begins
1482 with a newline), so we don't need to (and cannot)
1483 inspect the glyphs of it3.glyph_row, because
1484 PRODUCE_GLYPHS will not produce anything for a
1485 newline, and thus it3.glyph_row stays at its
1486 stale content it got at top of the window. */
1487 bool it3_moved = false;
1488 /* Finally, advance the iterator until we hit the
1489 first display element whose character position is
1490 CHARPOS, or until the first newline from the
1491 display string, which signals the end of the
1492 display line. */
1493 while (get_next_display_element (&it3))
1495 PRODUCE_GLYPHS (&it3);
1496 if (IT_CHARPOS (it3) == charpos
1497 || ITERATOR_AT_END_OF_LINE_P (&it3))
1498 break;
1499 it3_moved = true;
1500 set_iterator_to_next (&it3, false);
1502 top_x = it3.current_x - it3.pixel_width;
1503 /* Normally, we would exit the above loop because we
1504 found the display element whose character
1505 position is CHARPOS. For the contingency that we
1506 didn't, and stopped at the first newline from the
1507 display string, move back over the glyphs
1508 produced from the string, until we find the
1509 rightmost glyph not from the string. */
1510 if (it3_moved
1511 && newline_in_string
1512 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1514 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1515 + it3.glyph_row->used[TEXT_AREA];
1517 while (EQ ((g - 1)->object, string))
1519 --g;
1520 top_x -= g->pixel_width;
1522 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1523 + it3.glyph_row->used[TEXT_AREA]);
1528 *x = top_x;
1529 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1530 *rtop = max (0, window_top_y - top_y);
1531 *rbot = max (0, bottom_y - it.last_visible_y);
1532 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1533 - max (top_y, window_top_y)));
1534 *vpos = it.vpos;
1535 if (it.bidi_it.paragraph_dir == R2L)
1536 r2l = true;
1539 else
1541 /* Either we were asked to provide info about WINDOW_END, or
1542 CHARPOS is in the partially visible glyph row at end of
1543 window. */
1544 struct it it2;
1545 void *it2data = NULL;
1547 SAVE_IT (it2, it, it2data);
1548 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1549 move_it_by_lines (&it, 1);
1550 if (charpos < IT_CHARPOS (it)
1551 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1553 visible_p = true;
1554 RESTORE_IT (&it2, &it2, it2data);
1555 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1556 *x = it2.current_x;
1557 *y = it2.current_y + it2.max_ascent - it2.ascent;
1558 *rtop = max (0, -it2.current_y);
1559 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1560 - it.last_visible_y));
1561 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1562 it.last_visible_y)
1563 - max (it2.current_y,
1564 WINDOW_HEADER_LINE_HEIGHT (w))));
1565 *vpos = it2.vpos;
1566 if (it2.bidi_it.paragraph_dir == R2L)
1567 r2l = true;
1569 else
1570 bidi_unshelve_cache (it2data, true);
1572 bidi_unshelve_cache (itdata, false);
1574 if (old_buffer)
1575 set_buffer_internal_1 (old_buffer);
1577 if (visible_p)
1579 if (w->hscroll > 0)
1580 *x -=
1581 window_hscroll_limited (w, WINDOW_XFRAME (w))
1582 * WINDOW_FRAME_COLUMN_WIDTH (w);
1583 /* For lines in an R2L paragraph, we need to mirror the X pixel
1584 coordinate wrt the text area. For the reasons, see the
1585 commentary in buffer_posn_from_coords and the explanation of
1586 the geometry used by the move_it_* functions at the end of
1587 the large commentary near the beginning of this file. */
1588 if (r2l)
1589 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1592 #if false
1593 /* Debugging code. */
1594 if (visible_p)
1595 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1596 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1597 else
1598 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1599 #endif
1601 return visible_p;
1605 /* Return the next character from STR. Return in *LEN the length of
1606 the character. This is like STRING_CHAR_AND_LENGTH but never
1607 returns an invalid character. If we find one, we return a `?', but
1608 with the length of the invalid character. */
1610 static int
1611 string_char_and_length (const unsigned char *str, int *len)
1613 int c;
1615 c = STRING_CHAR_AND_LENGTH (str, *len);
1616 if (!CHAR_VALID_P (c))
1617 /* We may not change the length here because other places in Emacs
1618 don't use this function, i.e. they silently accept invalid
1619 characters. */
1620 c = '?';
1622 return c;
1627 /* Given a position POS containing a valid character and byte position
1628 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1630 static struct text_pos
1631 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1633 eassert (STRINGP (string) && nchars >= 0);
1635 if (STRING_MULTIBYTE (string))
1637 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1638 int len;
1640 while (nchars--)
1642 string_char_and_length (p, &len);
1643 p += len;
1644 CHARPOS (pos) += 1;
1645 BYTEPOS (pos) += len;
1648 else
1649 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1651 return pos;
1655 /* Value is the text position, i.e. character and byte position,
1656 for character position CHARPOS in STRING. */
1658 static struct text_pos
1659 string_pos (ptrdiff_t charpos, Lisp_Object string)
1661 struct text_pos pos;
1662 eassert (STRINGP (string));
1663 eassert (charpos >= 0);
1664 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1665 return pos;
1669 /* Value is a text position, i.e. character and byte position, for
1670 character position CHARPOS in C string S. MULTIBYTE_P
1671 means recognize multibyte characters. */
1673 static struct text_pos
1674 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1676 struct text_pos pos;
1678 eassert (s != NULL);
1679 eassert (charpos >= 0);
1681 if (multibyte_p)
1683 int len;
1685 SET_TEXT_POS (pos, 0, 0);
1686 while (charpos--)
1688 string_char_and_length ((const unsigned char *) s, &len);
1689 s += len;
1690 CHARPOS (pos) += 1;
1691 BYTEPOS (pos) += len;
1694 else
1695 SET_TEXT_POS (pos, charpos, charpos);
1697 return pos;
1701 /* Value is the number of characters in C string S. MULTIBYTE_P
1702 means recognize multibyte characters. */
1704 static ptrdiff_t
1705 number_of_chars (const char *s, bool multibyte_p)
1707 ptrdiff_t nchars;
1709 if (multibyte_p)
1711 ptrdiff_t rest = strlen (s);
1712 int len;
1713 const unsigned char *p = (const unsigned char *) s;
1715 for (nchars = 0; rest > 0; ++nchars)
1717 string_char_and_length (p, &len);
1718 rest -= len, p += len;
1721 else
1722 nchars = strlen (s);
1724 return nchars;
1728 /* Compute byte position NEWPOS->bytepos corresponding to
1729 NEWPOS->charpos. POS is a known position in string STRING.
1730 NEWPOS->charpos must be >= POS.charpos. */
1732 static void
1733 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1735 eassert (STRINGP (string));
1736 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1738 if (STRING_MULTIBYTE (string))
1739 *newpos = string_pos_nchars_ahead (pos, string,
1740 CHARPOS (*newpos) - CHARPOS (pos));
1741 else
1742 BYTEPOS (*newpos) = CHARPOS (*newpos);
1745 /* EXPORT:
1746 Return an estimation of the pixel height of mode or header lines on
1747 frame F. FACE_ID specifies what line's height to estimate. */
1750 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1752 #ifdef HAVE_WINDOW_SYSTEM
1753 if (FRAME_WINDOW_P (f))
1755 int height = FONT_HEIGHT (FRAME_FONT (f));
1757 /* This function is called so early when Emacs starts that the face
1758 cache and mode line face are not yet initialized. */
1759 if (FRAME_FACE_CACHE (f))
1761 struct face *face = FACE_FROM_ID (f, face_id);
1762 if (face)
1764 if (face->font)
1765 height = FONT_HEIGHT (face->font);
1766 if (face->box_line_width > 0)
1767 height += 2 * face->box_line_width;
1771 return height;
1773 #endif
1775 return 1;
1778 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1779 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1780 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1781 not force the value into range. */
1783 void
1784 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1785 NativeRectangle *bounds, bool noclip)
1788 #ifdef HAVE_WINDOW_SYSTEM
1789 if (FRAME_WINDOW_P (f))
1791 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1792 even for negative values. */
1793 if (pix_x < 0)
1794 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1795 if (pix_y < 0)
1796 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1798 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1799 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1801 if (bounds)
1802 STORE_NATIVE_RECT (*bounds,
1803 FRAME_COL_TO_PIXEL_X (f, pix_x),
1804 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1805 FRAME_COLUMN_WIDTH (f) - 1,
1806 FRAME_LINE_HEIGHT (f) - 1);
1808 /* PXW: Should we clip pixels before converting to columns/lines? */
1809 if (!noclip)
1811 if (pix_x < 0)
1812 pix_x = 0;
1813 else if (pix_x > FRAME_TOTAL_COLS (f))
1814 pix_x = FRAME_TOTAL_COLS (f);
1816 if (pix_y < 0)
1817 pix_y = 0;
1818 else if (pix_y > FRAME_TOTAL_LINES (f))
1819 pix_y = FRAME_TOTAL_LINES (f);
1822 #endif
1824 *x = pix_x;
1825 *y = pix_y;
1829 /* Find the glyph under window-relative coordinates X/Y in window W.
1830 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1831 strings. Return in *HPOS and *VPOS the row and column number of
1832 the glyph found. Return in *AREA the glyph area containing X.
1833 Value is a pointer to the glyph found or null if X/Y is not on
1834 text, or we can't tell because W's current matrix is not up to
1835 date. */
1837 static struct glyph *
1838 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1839 int *dx, int *dy, int *area)
1841 struct glyph *glyph, *end;
1842 struct glyph_row *row = NULL;
1843 int x0, i;
1845 /* Find row containing Y. Give up if some row is not enabled. */
1846 for (i = 0; i < w->current_matrix->nrows; ++i)
1848 row = MATRIX_ROW (w->current_matrix, i);
1849 if (!row->enabled_p)
1850 return NULL;
1851 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1852 break;
1855 *vpos = i;
1856 *hpos = 0;
1858 /* Give up if Y is not in the window. */
1859 if (i == w->current_matrix->nrows)
1860 return NULL;
1862 /* Get the glyph area containing X. */
1863 if (w->pseudo_window_p)
1865 *area = TEXT_AREA;
1866 x0 = 0;
1868 else
1870 if (x < window_box_left_offset (w, TEXT_AREA))
1872 *area = LEFT_MARGIN_AREA;
1873 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1875 else if (x < window_box_right_offset (w, TEXT_AREA))
1877 *area = TEXT_AREA;
1878 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1880 else
1882 *area = RIGHT_MARGIN_AREA;
1883 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1887 /* Find glyph containing X. */
1888 glyph = row->glyphs[*area];
1889 end = glyph + row->used[*area];
1890 x -= x0;
1891 while (glyph < end && x >= glyph->pixel_width)
1893 x -= glyph->pixel_width;
1894 ++glyph;
1897 if (glyph == end)
1898 return NULL;
1900 if (dx)
1902 *dx = x;
1903 *dy = y - (row->y + row->ascent - glyph->ascent);
1906 *hpos = glyph - row->glyphs[*area];
1907 return glyph;
1910 /* Convert frame-relative x/y to coordinates relative to window W.
1911 Takes pseudo-windows into account. */
1913 static void
1914 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1916 if (w->pseudo_window_p)
1918 /* A pseudo-window is always full-width, and starts at the
1919 left edge of the frame, plus a frame border. */
1920 struct frame *f = XFRAME (w->frame);
1921 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1924 else
1926 *x -= WINDOW_LEFT_EDGE_X (w);
1927 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1931 #ifdef HAVE_WINDOW_SYSTEM
1933 /* EXPORT:
1934 Return in RECTS[] at most N clipping rectangles for glyph string S.
1935 Return the number of stored rectangles. */
1938 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1940 XRectangle r;
1942 if (n <= 0)
1943 return 0;
1945 if (s->row->full_width_p)
1947 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1948 r.x = WINDOW_LEFT_EDGE_X (s->w);
1949 if (s->row->mode_line_p)
1950 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1951 else
1952 r.width = WINDOW_PIXEL_WIDTH (s->w);
1954 /* Unless displaying a mode or menu bar line, which are always
1955 fully visible, clip to the visible part of the row. */
1956 if (s->w->pseudo_window_p)
1957 r.height = s->row->visible_height;
1958 else
1959 r.height = s->height;
1961 else
1963 /* This is a text line that may be partially visible. */
1964 r.x = window_box_left (s->w, s->area);
1965 r.width = window_box_width (s->w, s->area);
1966 r.height = s->row->visible_height;
1969 if (s->clip_head)
1970 if (r.x < s->clip_head->x)
1972 if (r.width >= s->clip_head->x - r.x)
1973 r.width -= s->clip_head->x - r.x;
1974 else
1975 r.width = 0;
1976 r.x = s->clip_head->x;
1978 if (s->clip_tail)
1979 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1981 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1982 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1983 else
1984 r.width = 0;
1987 /* If S draws overlapping rows, it's sufficient to use the top and
1988 bottom of the window for clipping because this glyph string
1989 intentionally draws over other lines. */
1990 if (s->for_overlaps)
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 r.height = window_text_bottom_y (s->w) - r.y;
1995 /* Alas, the above simple strategy does not work for the
1996 environments with anti-aliased text: if the same text is
1997 drawn onto the same place multiple times, it gets thicker.
1998 If the overlap we are processing is for the erased cursor, we
1999 take the intersection with the rectangle of the cursor. */
2000 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2002 XRectangle rc, r_save = r;
2004 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2005 rc.y = s->w->phys_cursor.y;
2006 rc.width = s->w->phys_cursor_width;
2007 rc.height = s->w->phys_cursor_height;
2009 x_intersect_rectangles (&r_save, &rc, &r);
2012 else
2014 /* Don't use S->y for clipping because it doesn't take partially
2015 visible lines into account. For example, it can be negative for
2016 partially visible lines at the top of a window. */
2017 if (!s->row->full_width_p
2018 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2019 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2020 else
2021 r.y = max (0, s->row->y);
2024 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2026 /* If drawing the cursor, don't let glyph draw outside its
2027 advertised boundaries. Cleartype does this under some circumstances. */
2028 if (s->hl == DRAW_CURSOR)
2030 struct glyph *glyph = s->first_glyph;
2031 int height, max_y;
2033 if (s->x > r.x)
2035 if (r.width >= s->x - r.x)
2036 r.width -= s->x - r.x;
2037 else /* R2L hscrolled row with cursor outside text area */
2038 r.width = 0;
2039 r.x = s->x;
2041 r.width = min (r.width, glyph->pixel_width);
2043 /* If r.y is below window bottom, ensure that we still see a cursor. */
2044 height = min (glyph->ascent + glyph->descent,
2045 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2046 max_y = window_text_bottom_y (s->w) - height;
2047 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2048 if (s->ybase - glyph->ascent > max_y)
2050 r.y = max_y;
2051 r.height = height;
2053 else
2055 /* Don't draw cursor glyph taller than our actual glyph. */
2056 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2057 if (height < r.height)
2059 max_y = r.y + r.height;
2060 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2061 r.height = min (max_y - r.y, height);
2066 if (s->row->clip)
2068 XRectangle r_save = r;
2070 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2071 r.width = 0;
2074 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2075 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2077 #ifdef CONVERT_FROM_XRECT
2078 CONVERT_FROM_XRECT (r, *rects);
2079 #else
2080 *rects = r;
2081 #endif
2082 return 1;
2084 else
2086 /* If we are processing overlapping and allowed to return
2087 multiple clipping rectangles, we exclude the row of the glyph
2088 string from the clipping rectangle. This is to avoid drawing
2089 the same text on the environment with anti-aliasing. */
2090 #ifdef CONVERT_FROM_XRECT
2091 XRectangle rs[2];
2092 #else
2093 XRectangle *rs = rects;
2094 #endif
2095 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2097 if (s->for_overlaps & OVERLAPS_PRED)
2099 rs[i] = r;
2100 if (r.y + r.height > row_y)
2102 if (r.y < row_y)
2103 rs[i].height = row_y - r.y;
2104 else
2105 rs[i].height = 0;
2107 i++;
2109 if (s->for_overlaps & OVERLAPS_SUCC)
2111 rs[i] = r;
2112 if (r.y < row_y + s->row->visible_height)
2114 if (r.y + r.height > row_y + s->row->visible_height)
2116 rs[i].y = row_y + s->row->visible_height;
2117 rs[i].height = r.y + r.height - rs[i].y;
2119 else
2120 rs[i].height = 0;
2122 i++;
2125 n = i;
2126 #ifdef CONVERT_FROM_XRECT
2127 for (i = 0; i < n; i++)
2128 CONVERT_FROM_XRECT (rs[i], rects[i]);
2129 #endif
2130 return n;
2134 /* EXPORT:
2135 Return in *NR the clipping rectangle for glyph string S. */
2137 void
2138 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2140 get_glyph_string_clip_rects (s, nr, 1);
2144 /* EXPORT:
2145 Return the position and height of the phys cursor in window W.
2146 Set w->phys_cursor_width to width of phys cursor.
2149 void
2150 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2151 struct glyph *glyph, int *xp, int *yp, int *heightp)
2153 struct frame *f = XFRAME (WINDOW_FRAME (w));
2154 int x, y, wd, h, h0, y0, ascent;
2156 /* Compute the width of the rectangle to draw. If on a stretch
2157 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2158 rectangle as wide as the glyph, but use a canonical character
2159 width instead. */
2160 wd = glyph->pixel_width;
2162 x = w->phys_cursor.x;
2163 if (x < 0)
2165 wd += x;
2166 x = 0;
2169 if (glyph->type == STRETCH_GLYPH
2170 && !x_stretch_cursor_p)
2171 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2172 w->phys_cursor_width = wd;
2174 /* Don't let the hollow cursor glyph descend below the glyph row's
2175 ascent value, lest the hollow cursor looks funny. */
2176 y = w->phys_cursor.y;
2177 ascent = row->ascent;
2178 if (row->ascent < glyph->ascent)
2180 y =- glyph->ascent - row->ascent;
2181 ascent = glyph->ascent;
2184 /* If y is below window bottom, ensure that we still see a cursor. */
2185 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2187 h = max (h0, ascent + glyph->descent);
2188 h0 = min (h0, ascent + glyph->descent);
2190 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2191 if (y < y0)
2193 h = max (h - (y0 - y) + 1, h0);
2194 y = y0 - 1;
2196 else
2198 y0 = window_text_bottom_y (w) - h0;
2199 if (y > y0)
2201 h += y - y0;
2202 y = y0;
2206 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2207 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2208 *heightp = h;
2212 * Remember which glyph the mouse is over.
2215 void
2216 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2218 Lisp_Object window;
2219 struct window *w;
2220 struct glyph_row *r, *gr, *end_row;
2221 enum window_part part;
2222 enum glyph_row_area area;
2223 int x, y, width, height;
2225 /* Try to determine frame pixel position and size of the glyph under
2226 frame pixel coordinates X/Y on frame F. */
2228 if (window_resize_pixelwise)
2230 width = height = 1;
2231 goto virtual_glyph;
2233 else if (!f->glyphs_initialized_p
2234 || (window = window_from_coordinates (f, gx, gy, &part, false),
2235 NILP (window)))
2237 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2238 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2239 goto virtual_glyph;
2242 w = XWINDOW (window);
2243 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2244 height = WINDOW_FRAME_LINE_HEIGHT (w);
2246 x = window_relative_x_coord (w, part, gx);
2247 y = gy - WINDOW_TOP_EDGE_Y (w);
2249 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2250 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2252 if (w->pseudo_window_p)
2254 area = TEXT_AREA;
2255 part = ON_MODE_LINE; /* Don't adjust margin. */
2256 goto text_glyph;
2259 switch (part)
2261 case ON_LEFT_MARGIN:
2262 area = LEFT_MARGIN_AREA;
2263 goto text_glyph;
2265 case ON_RIGHT_MARGIN:
2266 area = RIGHT_MARGIN_AREA;
2267 goto text_glyph;
2269 case ON_HEADER_LINE:
2270 case ON_MODE_LINE:
2271 gr = (part == ON_HEADER_LINE
2272 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2273 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2274 gy = gr->y;
2275 area = TEXT_AREA;
2276 goto text_glyph_row_found;
2278 case ON_TEXT:
2279 area = TEXT_AREA;
2281 text_glyph:
2282 gr = 0; gy = 0;
2283 for (; r <= end_row && r->enabled_p; ++r)
2284 if (r->y + r->height > y)
2286 gr = r; gy = r->y;
2287 break;
2290 text_glyph_row_found:
2291 if (gr && gy <= y)
2293 struct glyph *g = gr->glyphs[area];
2294 struct glyph *end = g + gr->used[area];
2296 height = gr->height;
2297 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2298 if (gx + g->pixel_width > x)
2299 break;
2301 if (g < end)
2303 if (g->type == IMAGE_GLYPH)
2305 /* Don't remember when mouse is over image, as
2306 image may have hot-spots. */
2307 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2308 return;
2310 width = g->pixel_width;
2312 else
2314 /* Use nominal char spacing at end of line. */
2315 x -= gx;
2316 gx += (x / width) * width;
2319 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2321 gx += window_box_left_offset (w, area);
2322 /* Don't expand over the modeline to make sure the vertical
2323 drag cursor is shown early enough. */
2324 height = min (height,
2325 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2328 else
2330 /* Use nominal line height at end of window. */
2331 gx = (x / width) * width;
2332 y -= gy;
2333 gy += (y / height) * height;
2334 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2335 /* See comment above. */
2336 height = min (height,
2337 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2339 break;
2341 case ON_LEFT_FRINGE:
2342 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2343 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2344 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2345 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2346 goto row_glyph;
2348 case ON_RIGHT_FRINGE:
2349 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2350 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2351 : window_box_right_offset (w, TEXT_AREA));
2352 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2353 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2354 && !WINDOW_RIGHTMOST_P (w))
2355 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2356 /* Make sure the vertical border can get her own glyph to the
2357 right of the one we build here. */
2358 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2359 else
2360 width = WINDOW_PIXEL_WIDTH (w) - gx;
2361 else
2362 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2364 goto row_glyph;
2366 case ON_VERTICAL_BORDER:
2367 gx = WINDOW_PIXEL_WIDTH (w) - width;
2368 goto row_glyph;
2370 case ON_VERTICAL_SCROLL_BAR:
2371 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2373 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2374 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2375 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2376 : 0)));
2377 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2379 row_glyph:
2380 gr = 0, gy = 0;
2381 for (; r <= end_row && r->enabled_p; ++r)
2382 if (r->y + r->height > y)
2384 gr = r; gy = r->y;
2385 break;
2388 if (gr && gy <= y)
2389 height = gr->height;
2390 else
2392 /* Use nominal line height at end of window. */
2393 y -= gy;
2394 gy += (y / height) * height;
2396 break;
2398 case ON_RIGHT_DIVIDER:
2399 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2400 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2401 gy = 0;
2402 /* The bottom divider prevails. */
2403 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2404 goto add_edge;
2406 case ON_BOTTOM_DIVIDER:
2407 gx = 0;
2408 width = WINDOW_PIXEL_WIDTH (w);
2409 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2410 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2411 goto add_edge;
2413 default:
2415 virtual_glyph:
2416 /* If there is no glyph under the mouse, then we divide the screen
2417 into a grid of the smallest glyph in the frame, and use that
2418 as our "glyph". */
2420 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2421 round down even for negative values. */
2422 if (gx < 0)
2423 gx -= width - 1;
2424 if (gy < 0)
2425 gy -= height - 1;
2427 gx = (gx / width) * width;
2428 gy = (gy / height) * height;
2430 goto store_rect;
2433 add_edge:
2434 gx += WINDOW_LEFT_EDGE_X (w);
2435 gy += WINDOW_TOP_EDGE_Y (w);
2437 store_rect:
2438 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2440 /* Visible feedback for debugging. */
2441 #if false && defined HAVE_X_WINDOWS
2442 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2443 f->output_data.x->normal_gc,
2444 gx, gy, width, height);
2445 #endif
2449 #endif /* HAVE_WINDOW_SYSTEM */
2451 static void
2452 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2454 eassert (w);
2455 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2456 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2457 w->window_end_vpos
2458 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2461 /***********************************************************************
2462 Lisp form evaluation
2463 ***********************************************************************/
2465 /* Error handler for safe_eval and safe_call. */
2467 static Lisp_Object
2468 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2470 add_to_log ("Error during redisplay: %S signaled %S",
2471 Flist (nargs, args), arg);
2472 return Qnil;
2475 /* Call function FUNC with the rest of NARGS - 1 arguments
2476 following. Return the result, or nil if something went
2477 wrong. Prevent redisplay during the evaluation. */
2479 static Lisp_Object
2480 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2482 Lisp_Object val;
2484 if (inhibit_eval_during_redisplay)
2485 val = Qnil;
2486 else
2488 ptrdiff_t i;
2489 ptrdiff_t count = SPECPDL_INDEX ();
2490 Lisp_Object *args;
2491 USE_SAFE_ALLOCA;
2492 SAFE_ALLOCA_LISP (args, nargs);
2494 args[0] = func;
2495 for (i = 1; i < nargs; i++)
2496 args[i] = va_arg (ap, Lisp_Object);
2498 specbind (Qinhibit_redisplay, Qt);
2499 if (inhibit_quit)
2500 specbind (Qinhibit_quit, Qt);
2501 /* Use Qt to ensure debugger does not run,
2502 so there is no possibility of wanting to redisplay. */
2503 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2504 safe_eval_handler);
2505 SAFE_FREE ();
2506 val = unbind_to (count, val);
2509 return val;
2512 Lisp_Object
2513 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2515 Lisp_Object retval;
2516 va_list ap;
2518 va_start (ap, func);
2519 retval = safe__call (false, nargs, func, ap);
2520 va_end (ap);
2521 return retval;
2524 /* Call function FN with one argument ARG.
2525 Return the result, or nil if something went wrong. */
2527 Lisp_Object
2528 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2530 return safe_call (2, fn, arg);
2533 static Lisp_Object
2534 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2536 Lisp_Object retval;
2537 va_list ap;
2539 va_start (ap, fn);
2540 retval = safe__call (inhibit_quit, 2, fn, ap);
2541 va_end (ap);
2542 return retval;
2545 Lisp_Object
2546 safe_eval (Lisp_Object sexpr)
2548 return safe__call1 (false, Qeval, sexpr);
2551 static Lisp_Object
2552 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2554 return safe__call1 (inhibit_quit, Qeval, sexpr);
2557 /* Call function FN with two arguments ARG1 and ARG2.
2558 Return the result, or nil if something went wrong. */
2560 Lisp_Object
2561 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2563 return safe_call (3, fn, arg1, arg2);
2568 /***********************************************************************
2569 Debugging
2570 ***********************************************************************/
2572 /* Define CHECK_IT to perform sanity checks on iterators.
2573 This is for debugging. It is too slow to do unconditionally. */
2575 static void
2576 CHECK_IT (struct it *it)
2578 #if false
2579 if (it->method == GET_FROM_STRING)
2581 eassert (STRINGP (it->string));
2582 eassert (IT_STRING_CHARPOS (*it) >= 0);
2584 else
2586 eassert (IT_STRING_CHARPOS (*it) < 0);
2587 if (it->method == GET_FROM_BUFFER)
2589 /* Check that character and byte positions agree. */
2590 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2594 if (it->dpvec)
2595 eassert (it->current.dpvec_index >= 0);
2596 else
2597 eassert (it->current.dpvec_index < 0);
2598 #endif
2602 /* Check that the window end of window W is what we expect it
2603 to be---the last row in the current matrix displaying text. */
2605 static void
2606 CHECK_WINDOW_END (struct window *w)
2608 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2609 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2611 struct glyph_row *row;
2612 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2613 !row->enabled_p
2614 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2615 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2617 #endif
2620 /***********************************************************************
2621 Iterator initialization
2622 ***********************************************************************/
2624 /* Initialize IT for displaying current_buffer in window W, starting
2625 at character position CHARPOS. CHARPOS < 0 means that no buffer
2626 position is specified which is useful when the iterator is assigned
2627 a position later. BYTEPOS is the byte position corresponding to
2628 CHARPOS.
2630 If ROW is not null, calls to produce_glyphs with IT as parameter
2631 will produce glyphs in that row.
2633 BASE_FACE_ID is the id of a base face to use. It must be one of
2634 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2635 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2636 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2638 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2639 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2640 will be initialized to use the corresponding mode line glyph row of
2641 the desired matrix of W. */
2643 void
2644 init_iterator (struct it *it, struct window *w,
2645 ptrdiff_t charpos, ptrdiff_t bytepos,
2646 struct glyph_row *row, enum face_id base_face_id)
2648 enum face_id remapped_base_face_id = base_face_id;
2650 /* Some precondition checks. */
2651 eassert (w != NULL && it != NULL);
2652 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2653 && charpos <= ZV));
2655 /* If face attributes have been changed since the last redisplay,
2656 free realized faces now because they depend on face definitions
2657 that might have changed. Don't free faces while there might be
2658 desired matrices pending which reference these faces. */
2659 if (face_change && !inhibit_free_realized_faces)
2661 face_change = false;
2662 free_all_realized_faces (Qnil);
2665 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2666 if (! NILP (Vface_remapping_alist))
2667 remapped_base_face_id
2668 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2670 /* Use one of the mode line rows of W's desired matrix if
2671 appropriate. */
2672 if (row == NULL)
2674 if (base_face_id == MODE_LINE_FACE_ID
2675 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2676 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2677 else if (base_face_id == HEADER_LINE_FACE_ID)
2678 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2681 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2682 Other parts of redisplay rely on that. */
2683 memclear (it, sizeof *it);
2684 it->current.overlay_string_index = -1;
2685 it->current.dpvec_index = -1;
2686 it->base_face_id = remapped_base_face_id;
2687 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2688 it->paragraph_embedding = L2R;
2689 it->bidi_it.w = w;
2691 /* The window in which we iterate over current_buffer: */
2692 XSETWINDOW (it->window, w);
2693 it->w = w;
2694 it->f = XFRAME (w->frame);
2696 it->cmp_it.id = -1;
2698 /* Extra space between lines (on window systems only). */
2699 if (base_face_id == DEFAULT_FACE_ID
2700 && FRAME_WINDOW_P (it->f))
2702 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2703 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2704 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2705 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2706 * FRAME_LINE_HEIGHT (it->f));
2707 else if (it->f->extra_line_spacing > 0)
2708 it->extra_line_spacing = it->f->extra_line_spacing;
2711 /* If realized faces have been removed, e.g. because of face
2712 attribute changes of named faces, recompute them. When running
2713 in batch mode, the face cache of the initial frame is null. If
2714 we happen to get called, make a dummy face cache. */
2715 if (FRAME_FACE_CACHE (it->f) == NULL)
2716 init_frame_faces (it->f);
2717 if (FRAME_FACE_CACHE (it->f)->used == 0)
2718 recompute_basic_faces (it->f);
2720 it->override_ascent = -1;
2722 /* Are control characters displayed as `^C'? */
2723 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2725 /* -1 means everything between a CR and the following line end
2726 is invisible. >0 means lines indented more than this value are
2727 invisible. */
2728 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2729 ? (clip_to_bounds
2730 (-1, XINT (BVAR (current_buffer, selective_display)),
2731 PTRDIFF_MAX))
2732 : (!NILP (BVAR (current_buffer, selective_display))
2733 ? -1 : 0));
2734 it->selective_display_ellipsis_p
2735 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2737 /* Display table to use. */
2738 it->dp = window_display_table (w);
2740 /* Are multibyte characters enabled in current_buffer? */
2741 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2743 /* Get the position at which the redisplay_end_trigger hook should
2744 be run, if it is to be run at all. */
2745 if (MARKERP (w->redisplay_end_trigger)
2746 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2747 it->redisplay_end_trigger_charpos
2748 = marker_position (w->redisplay_end_trigger);
2749 else if (INTEGERP (w->redisplay_end_trigger))
2750 it->redisplay_end_trigger_charpos
2751 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2752 PTRDIFF_MAX);
2754 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2756 /* Are lines in the display truncated? */
2757 if (TRUNCATE != 0)
2758 it->line_wrap = TRUNCATE;
2759 if (base_face_id == DEFAULT_FACE_ID
2760 && !it->w->hscroll
2761 && (WINDOW_FULL_WIDTH_P (it->w)
2762 || NILP (Vtruncate_partial_width_windows)
2763 || (INTEGERP (Vtruncate_partial_width_windows)
2764 /* PXW: Shall we do something about this? */
2765 && (XINT (Vtruncate_partial_width_windows)
2766 <= WINDOW_TOTAL_COLS (it->w))))
2767 && NILP (BVAR (current_buffer, truncate_lines)))
2768 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2769 ? WINDOW_WRAP : WORD_WRAP;
2771 /* Get dimensions of truncation and continuation glyphs. These are
2772 displayed as fringe bitmaps under X, but we need them for such
2773 frames when the fringes are turned off. But leave the dimensions
2774 zero for tooltip frames, as these glyphs look ugly there and also
2775 sabotage calculations of tooltip dimensions in x-show-tip. */
2776 #ifdef HAVE_WINDOW_SYSTEM
2777 if (!(FRAME_WINDOW_P (it->f)
2778 && FRAMEP (tip_frame)
2779 && it->f == XFRAME (tip_frame)))
2780 #endif
2782 if (it->line_wrap == TRUNCATE)
2784 /* We will need the truncation glyph. */
2785 eassert (it->glyph_row == NULL);
2786 produce_special_glyphs (it, IT_TRUNCATION);
2787 it->truncation_pixel_width = it->pixel_width;
2789 else
2791 /* We will need the continuation glyph. */
2792 eassert (it->glyph_row == NULL);
2793 produce_special_glyphs (it, IT_CONTINUATION);
2794 it->continuation_pixel_width = it->pixel_width;
2798 /* Reset these values to zero because the produce_special_glyphs
2799 above has changed them. */
2800 it->pixel_width = it->ascent = it->descent = 0;
2801 it->phys_ascent = it->phys_descent = 0;
2803 /* Set this after getting the dimensions of truncation and
2804 continuation glyphs, so that we don't produce glyphs when calling
2805 produce_special_glyphs, above. */
2806 it->glyph_row = row;
2807 it->area = TEXT_AREA;
2809 /* Get the dimensions of the display area. The display area
2810 consists of the visible window area plus a horizontally scrolled
2811 part to the left of the window. All x-values are relative to the
2812 start of this total display area. */
2813 if (base_face_id != DEFAULT_FACE_ID)
2815 /* Mode lines, menu bar in terminal frames. */
2816 it->first_visible_x = 0;
2817 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2819 else
2821 it->first_visible_x
2822 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2823 it->last_visible_x = (it->first_visible_x
2824 + window_box_width (w, TEXT_AREA));
2826 /* If we truncate lines, leave room for the truncation glyph(s) at
2827 the right margin. Otherwise, leave room for the continuation
2828 glyph(s). Done only if the window has no right fringe. */
2829 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2831 if (it->line_wrap == TRUNCATE)
2832 it->last_visible_x -= it->truncation_pixel_width;
2833 else
2834 it->last_visible_x -= it->continuation_pixel_width;
2837 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2838 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2841 /* Leave room for a border glyph. */
2842 if (!FRAME_WINDOW_P (it->f)
2843 && !WINDOW_RIGHTMOST_P (it->w))
2844 it->last_visible_x -= 1;
2846 it->last_visible_y = window_text_bottom_y (w);
2848 /* For mode lines and alike, arrange for the first glyph having a
2849 left box line if the face specifies a box. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2852 struct face *face;
2854 it->face_id = remapped_base_face_id;
2856 /* If we have a boxed mode line, make the first character appear
2857 with a left box line. */
2858 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2859 if (face && face->box != FACE_NO_BOX)
2860 it->start_of_box_run_p = true;
2863 /* If a buffer position was specified, set the iterator there,
2864 getting overlays and face properties from that position. */
2865 if (charpos >= BUF_BEG (current_buffer))
2867 it->stop_charpos = charpos;
2868 it->end_charpos = ZV;
2869 eassert (charpos == BYTE_TO_CHAR (bytepos));
2870 IT_CHARPOS (*it) = charpos;
2871 IT_BYTEPOS (*it) = bytepos;
2873 /* We will rely on `reseat' to set this up properly, via
2874 handle_face_prop. */
2875 it->face_id = it->base_face_id;
2877 it->start = it->current;
2878 /* Do we need to reorder bidirectional text? Not if this is a
2879 unibyte buffer: by definition, none of the single-byte
2880 characters are strong R2L, so no reordering is needed. And
2881 bidi.c doesn't support unibyte buffers anyway. Also, don't
2882 reorder while we are loading loadup.el, since the tables of
2883 character properties needed for reordering are not yet
2884 available. */
2885 it->bidi_p =
2886 NILP (Vpurify_flag)
2887 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2888 && it->multibyte_p;
2890 /* If we are to reorder bidirectional text, init the bidi
2891 iterator. */
2892 if (it->bidi_p)
2894 /* Since we don't know at this point whether there will be
2895 any R2L lines in the window, we reserve space for
2896 truncation/continuation glyphs even if only the left
2897 fringe is absent. */
2898 if (base_face_id == DEFAULT_FACE_ID
2899 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2900 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2902 if (it->line_wrap == TRUNCATE)
2903 it->last_visible_x -= it->truncation_pixel_width;
2904 else
2905 it->last_visible_x -= it->continuation_pixel_width;
2907 /* Note the paragraph direction that this buffer wants to
2908 use. */
2909 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2910 Qleft_to_right))
2911 it->paragraph_embedding = L2R;
2912 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2913 Qright_to_left))
2914 it->paragraph_embedding = R2L;
2915 else
2916 it->paragraph_embedding = NEUTRAL_DIR;
2917 bidi_unshelve_cache (NULL, false);
2918 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2919 &it->bidi_it);
2922 /* Compute faces etc. */
2923 reseat (it, it->current.pos, true);
2926 CHECK_IT (it);
2930 /* Initialize IT for the display of window W with window start POS. */
2932 void
2933 start_display (struct it *it, struct window *w, struct text_pos pos)
2935 struct glyph_row *row;
2936 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2938 row = w->desired_matrix->rows + first_vpos;
2939 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2940 it->first_vpos = first_vpos;
2942 /* Don't reseat to previous visible line start if current start
2943 position is in a string or image. */
2944 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2946 int first_y = it->current_y;
2948 /* If window start is not at a line start, skip forward to POS to
2949 get the correct continuation lines width. */
2950 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
2951 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2952 if (!start_at_line_beg_p)
2954 int new_x;
2956 reseat_at_previous_visible_line_start (it);
2957 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2959 new_x = it->current_x + it->pixel_width;
2961 /* If lines are continued, this line may end in the middle
2962 of a multi-glyph character (e.g. a control character
2963 displayed as \003, or in the middle of an overlay
2964 string). In this case move_it_to above will not have
2965 taken us to the start of the continuation line but to the
2966 end of the continued line. */
2967 if (it->current_x > 0
2968 && it->line_wrap != TRUNCATE /* Lines are continued. */
2969 && (/* And glyph doesn't fit on the line. */
2970 new_x > it->last_visible_x
2971 /* Or it fits exactly and we're on a window
2972 system frame. */
2973 || (new_x == it->last_visible_x
2974 && FRAME_WINDOW_P (it->f)
2975 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2976 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2977 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2979 if ((it->current.dpvec_index >= 0
2980 || it->current.overlay_string_index >= 0)
2981 /* If we are on a newline from a display vector or
2982 overlay string, then we are already at the end of
2983 a screen line; no need to go to the next line in
2984 that case, as this line is not really continued.
2985 (If we do go to the next line, C-e will not DTRT.) */
2986 && it->c != '\n')
2988 set_iterator_to_next (it, true);
2989 move_it_in_display_line_to (it, -1, -1, 0);
2992 it->continuation_lines_width += it->current_x;
2994 /* If the character at POS is displayed via a display
2995 vector, move_it_to above stops at the final glyph of
2996 IT->dpvec. To make the caller redisplay that character
2997 again (a.k.a. start at POS), we need to reset the
2998 dpvec_index to the beginning of IT->dpvec. */
2999 else if (it->current.dpvec_index >= 0)
3000 it->current.dpvec_index = 0;
3002 /* We're starting a new display line, not affected by the
3003 height of the continued line, so clear the appropriate
3004 fields in the iterator structure. */
3005 it->max_ascent = it->max_descent = 0;
3006 it->max_phys_ascent = it->max_phys_descent = 0;
3008 it->current_y = first_y;
3009 it->vpos = 0;
3010 it->current_x = it->hpos = 0;
3016 /* Return true if POS is a position in ellipses displayed for invisible
3017 text. W is the window we display, for text property lookup. */
3019 static bool
3020 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3022 Lisp_Object prop, window;
3023 bool ellipses_p = false;
3024 ptrdiff_t charpos = CHARPOS (pos->pos);
3026 /* If POS specifies a position in a display vector, this might
3027 be for an ellipsis displayed for invisible text. We won't
3028 get the iterator set up for delivering that ellipsis unless
3029 we make sure that it gets aware of the invisible text. */
3030 if (pos->dpvec_index >= 0
3031 && pos->overlay_string_index < 0
3032 && CHARPOS (pos->string_pos) < 0
3033 && charpos > BEGV
3034 && (XSETWINDOW (window, w),
3035 prop = Fget_char_property (make_number (charpos),
3036 Qinvisible, window),
3037 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3039 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3040 window);
3041 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3044 return ellipses_p;
3048 /* Initialize IT for stepping through current_buffer in window W,
3049 starting at position POS that includes overlay string and display
3050 vector/ control character translation position information. Value
3051 is false if there are overlay strings with newlines at POS. */
3053 static bool
3054 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3056 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3057 int i;
3058 bool overlay_strings_with_newlines = false;
3060 /* If POS specifies a position in a display vector, this might
3061 be for an ellipsis displayed for invisible text. We won't
3062 get the iterator set up for delivering that ellipsis unless
3063 we make sure that it gets aware of the invisible text. */
3064 if (in_ellipses_for_invisible_text_p (pos, w))
3066 --charpos;
3067 bytepos = 0;
3070 /* Keep in mind: the call to reseat in init_iterator skips invisible
3071 text, so we might end up at a position different from POS. This
3072 is only a problem when POS is a row start after a newline and an
3073 overlay starts there with an after-string, and the overlay has an
3074 invisible property. Since we don't skip invisible text in
3075 display_line and elsewhere immediately after consuming the
3076 newline before the row start, such a POS will not be in a string,
3077 but the call to init_iterator below will move us to the
3078 after-string. */
3079 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3081 /* This only scans the current chunk -- it should scan all chunks.
3082 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3083 to 16 in 22.1 to make this a lesser problem. */
3084 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3086 const char *s = SSDATA (it->overlay_strings[i]);
3087 const char *e = s + SBYTES (it->overlay_strings[i]);
3089 while (s < e && *s != '\n')
3090 ++s;
3092 if (s < e)
3094 overlay_strings_with_newlines = true;
3095 break;
3099 /* If position is within an overlay string, set up IT to the right
3100 overlay string. */
3101 if (pos->overlay_string_index >= 0)
3103 int relative_index;
3105 /* If the first overlay string happens to have a `display'
3106 property for an image, the iterator will be set up for that
3107 image, and we have to undo that setup first before we can
3108 correct the overlay string index. */
3109 if (it->method == GET_FROM_IMAGE)
3110 pop_it (it);
3112 /* We already have the first chunk of overlay strings in
3113 IT->overlay_strings. Load more until the one for
3114 pos->overlay_string_index is in IT->overlay_strings. */
3115 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3117 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3118 it->current.overlay_string_index = 0;
3119 while (n--)
3121 load_overlay_strings (it, 0);
3122 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3126 it->current.overlay_string_index = pos->overlay_string_index;
3127 relative_index = (it->current.overlay_string_index
3128 % OVERLAY_STRING_CHUNK_SIZE);
3129 it->string = it->overlay_strings[relative_index];
3130 eassert (STRINGP (it->string));
3131 it->current.string_pos = pos->string_pos;
3132 it->method = GET_FROM_STRING;
3133 it->end_charpos = SCHARS (it->string);
3134 /* Set up the bidi iterator for this overlay string. */
3135 if (it->bidi_p)
3137 it->bidi_it.string.lstring = it->string;
3138 it->bidi_it.string.s = NULL;
3139 it->bidi_it.string.schars = SCHARS (it->string);
3140 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3141 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3142 it->bidi_it.string.unibyte = !it->multibyte_p;
3143 it->bidi_it.w = it->w;
3144 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3145 FRAME_WINDOW_P (it->f), &it->bidi_it);
3147 /* Synchronize the state of the bidi iterator with
3148 pos->string_pos. For any string position other than
3149 zero, this will be done automagically when we resume
3150 iteration over the string and get_visually_first_element
3151 is called. But if string_pos is zero, and the string is
3152 to be reordered for display, we need to resync manually,
3153 since it could be that the iteration state recorded in
3154 pos ended at string_pos of 0 moving backwards in string. */
3155 if (CHARPOS (pos->string_pos) == 0)
3157 get_visually_first_element (it);
3158 if (IT_STRING_CHARPOS (*it) != 0)
3159 do {
3160 /* Paranoia. */
3161 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3162 bidi_move_to_visually_next (&it->bidi_it);
3163 } while (it->bidi_it.charpos != 0);
3165 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3166 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3170 if (CHARPOS (pos->string_pos) >= 0)
3172 /* Recorded position is not in an overlay string, but in another
3173 string. This can only be a string from a `display' property.
3174 IT should already be filled with that string. */
3175 it->current.string_pos = pos->string_pos;
3176 eassert (STRINGP (it->string));
3177 if (it->bidi_p)
3178 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3179 FRAME_WINDOW_P (it->f), &it->bidi_it);
3182 /* Restore position in display vector translations, control
3183 character translations or ellipses. */
3184 if (pos->dpvec_index >= 0)
3186 if (it->dpvec == NULL)
3187 get_next_display_element (it);
3188 eassert (it->dpvec && it->current.dpvec_index == 0);
3189 it->current.dpvec_index = pos->dpvec_index;
3192 CHECK_IT (it);
3193 return !overlay_strings_with_newlines;
3197 /* Initialize IT for stepping through current_buffer in window W
3198 starting at ROW->start. */
3200 static void
3201 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3203 init_from_display_pos (it, w, &row->start);
3204 it->start = row->start;
3205 it->continuation_lines_width = row->continuation_lines_width;
3206 CHECK_IT (it);
3210 /* Initialize IT for stepping through current_buffer in window W
3211 starting in the line following ROW, i.e. starting at ROW->end.
3212 Value is false if there are overlay strings with newlines at ROW's
3213 end position. */
3215 static bool
3216 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3218 bool success = false;
3220 if (init_from_display_pos (it, w, &row->end))
3222 if (row->continued_p)
3223 it->continuation_lines_width
3224 = row->continuation_lines_width + row->pixel_width;
3225 CHECK_IT (it);
3226 success = true;
3229 return success;
3235 /***********************************************************************
3236 Text properties
3237 ***********************************************************************/
3239 /* Called when IT reaches IT->stop_charpos. Handle text property and
3240 overlay changes. Set IT->stop_charpos to the next position where
3241 to stop. */
3243 static void
3244 handle_stop (struct it *it)
3246 enum prop_handled handled;
3247 bool handle_overlay_change_p;
3248 struct props *p;
3250 it->dpvec = NULL;
3251 it->current.dpvec_index = -1;
3252 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3253 it->ellipsis_p = false;
3255 /* Use face of preceding text for ellipsis (if invisible) */
3256 if (it->selective_display_ellipsis_p)
3257 it->saved_face_id = it->face_id;
3259 /* Here's the description of the semantics of, and the logic behind,
3260 the various HANDLED_* statuses:
3262 HANDLED_NORMALLY means the handler did its job, and the loop
3263 should proceed to calling the next handler in order.
3265 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3266 change in the properties and overlays at current position, so the
3267 loop should be restarted, to re-invoke the handlers that were
3268 already called. This happens when fontification-functions were
3269 called by handle_fontified_prop, and actually fontified
3270 something. Another case where HANDLED_RECOMPUTE_PROPS is
3271 returned is when we discover overlay strings that need to be
3272 displayed right away. The loop below will continue for as long
3273 as the status is HANDLED_RECOMPUTE_PROPS.
3275 HANDLED_RETURN means return immediately to the caller, to
3276 continue iteration without calling any further handlers. This is
3277 used when we need to act on some property right away, for example
3278 when we need to display the ellipsis or a replacing display
3279 property, such as display string or image.
3281 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3282 consumed, and the handler switched to the next overlay string.
3283 This signals the loop below to refrain from looking for more
3284 overlays before all the overlay strings of the current overlay
3285 are processed.
3287 Some of the handlers called by the loop push the iterator state
3288 onto the stack (see 'push_it'), and arrange for the iteration to
3289 continue with another object, such as an image, a display string,
3290 or an overlay string. In most such cases, it->stop_charpos is
3291 set to the first character of the string, so that when the
3292 iteration resumes, this function will immediately be called
3293 again, to examine the properties at the beginning of the string.
3295 When a display or overlay string is exhausted, the iterator state
3296 is popped (see 'pop_it'), and iteration continues with the
3297 previous object. Again, in many such cases this function is
3298 called again to find the next position where properties might
3299 change. */
3303 handled = HANDLED_NORMALLY;
3305 /* Call text property handlers. */
3306 for (p = it_props; p->handler; ++p)
3308 handled = p->handler (it);
3310 if (handled == HANDLED_RECOMPUTE_PROPS)
3311 break;
3312 else if (handled == HANDLED_RETURN)
3314 /* We still want to show before and after strings from
3315 overlays even if the actual buffer text is replaced. */
3316 if (!handle_overlay_change_p
3317 || it->sp > 1
3318 /* Don't call get_overlay_strings_1 if we already
3319 have overlay strings loaded, because doing so
3320 will load them again and push the iterator state
3321 onto the stack one more time, which is not
3322 expected by the rest of the code that processes
3323 overlay strings. */
3324 || (it->current.overlay_string_index < 0
3325 && !get_overlay_strings_1 (it, 0, false)))
3327 if (it->ellipsis_p)
3328 setup_for_ellipsis (it, 0);
3329 /* When handling a display spec, we might load an
3330 empty string. In that case, discard it here. We
3331 used to discard it in handle_single_display_spec,
3332 but that causes get_overlay_strings_1, above, to
3333 ignore overlay strings that we must check. */
3334 if (STRINGP (it->string) && !SCHARS (it->string))
3335 pop_it (it);
3336 return;
3338 else if (STRINGP (it->string) && !SCHARS (it->string))
3339 pop_it (it);
3340 else
3342 it->string_from_display_prop_p = false;
3343 it->from_disp_prop_p = false;
3344 handle_overlay_change_p = false;
3346 handled = HANDLED_RECOMPUTE_PROPS;
3347 break;
3349 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3350 handle_overlay_change_p = false;
3353 if (handled != HANDLED_RECOMPUTE_PROPS)
3355 /* Don't check for overlay strings below when set to deliver
3356 characters from a display vector. */
3357 if (it->method == GET_FROM_DISPLAY_VECTOR)
3358 handle_overlay_change_p = false;
3360 /* Handle overlay changes.
3361 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3362 if it finds overlays. */
3363 if (handle_overlay_change_p)
3364 handled = handle_overlay_change (it);
3367 if (it->ellipsis_p)
3369 setup_for_ellipsis (it, 0);
3370 break;
3373 while (handled == HANDLED_RECOMPUTE_PROPS);
3375 /* Determine where to stop next. */
3376 if (handled == HANDLED_NORMALLY)
3377 compute_stop_pos (it);
3381 /* Compute IT->stop_charpos from text property and overlay change
3382 information for IT's current position. */
3384 static void
3385 compute_stop_pos (struct it *it)
3387 register INTERVAL iv, next_iv;
3388 Lisp_Object object, limit, position;
3389 ptrdiff_t charpos, bytepos;
3391 if (STRINGP (it->string))
3393 /* Strings are usually short, so don't limit the search for
3394 properties. */
3395 it->stop_charpos = it->end_charpos;
3396 object = it->string;
3397 limit = Qnil;
3398 charpos = IT_STRING_CHARPOS (*it);
3399 bytepos = IT_STRING_BYTEPOS (*it);
3401 else
3403 ptrdiff_t pos;
3405 /* If end_charpos is out of range for some reason, such as a
3406 misbehaving display function, rationalize it (Bug#5984). */
3407 if (it->end_charpos > ZV)
3408 it->end_charpos = ZV;
3409 it->stop_charpos = it->end_charpos;
3411 /* If next overlay change is in front of the current stop pos
3412 (which is IT->end_charpos), stop there. Note: value of
3413 next_overlay_change is point-max if no overlay change
3414 follows. */
3415 charpos = IT_CHARPOS (*it);
3416 bytepos = IT_BYTEPOS (*it);
3417 pos = next_overlay_change (charpos);
3418 if (pos < it->stop_charpos)
3419 it->stop_charpos = pos;
3421 /* Set up variables for computing the stop position from text
3422 property changes. */
3423 XSETBUFFER (object, current_buffer);
3424 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3427 /* Get the interval containing IT's position. Value is a null
3428 interval if there isn't such an interval. */
3429 position = make_number (charpos);
3430 iv = validate_interval_range (object, &position, &position, false);
3431 if (iv)
3433 Lisp_Object values_here[LAST_PROP_IDX];
3434 struct props *p;
3436 /* Get properties here. */
3437 for (p = it_props; p->handler; ++p)
3438 values_here[p->idx] = textget (iv->plist,
3439 builtin_lisp_symbol (p->name));
3441 /* Look for an interval following iv that has different
3442 properties. */
3443 for (next_iv = next_interval (iv);
3444 (next_iv
3445 && (NILP (limit)
3446 || XFASTINT (limit) > next_iv->position));
3447 next_iv = next_interval (next_iv))
3449 for (p = it_props; p->handler; ++p)
3451 Lisp_Object new_value = textget (next_iv->plist,
3452 builtin_lisp_symbol (p->name));
3453 if (!EQ (values_here[p->idx], new_value))
3454 break;
3457 if (p->handler)
3458 break;
3461 if (next_iv)
3463 if (INTEGERP (limit)
3464 && next_iv->position >= XFASTINT (limit))
3465 /* No text property change up to limit. */
3466 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3467 else
3468 /* Text properties change in next_iv. */
3469 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3473 if (it->cmp_it.id < 0)
3475 ptrdiff_t stoppos = it->end_charpos;
3477 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3478 stoppos = -1;
3479 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3480 stoppos, it->string);
3483 eassert (STRINGP (it->string)
3484 || (it->stop_charpos >= BEGV
3485 && it->stop_charpos >= IT_CHARPOS (*it)));
3489 /* Return the position of the next overlay change after POS in
3490 current_buffer. Value is point-max if no overlay change
3491 follows. This is like `next-overlay-change' but doesn't use
3492 xmalloc. */
3494 static ptrdiff_t
3495 next_overlay_change (ptrdiff_t pos)
3497 ptrdiff_t i, noverlays;
3498 ptrdiff_t endpos;
3499 Lisp_Object *overlays;
3500 USE_SAFE_ALLOCA;
3502 /* Get all overlays at the given position. */
3503 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3505 /* If any of these overlays ends before endpos,
3506 use its ending point instead. */
3507 for (i = 0; i < noverlays; ++i)
3509 Lisp_Object oend;
3510 ptrdiff_t oendpos;
3512 oend = OVERLAY_END (overlays[i]);
3513 oendpos = OVERLAY_POSITION (oend);
3514 endpos = min (endpos, oendpos);
3517 SAFE_FREE ();
3518 return endpos;
3521 /* How many characters forward to search for a display property or
3522 display string. Searching too far forward makes the bidi display
3523 sluggish, especially in small windows. */
3524 #define MAX_DISP_SCAN 250
3526 /* Return the character position of a display string at or after
3527 position specified by POSITION. If no display string exists at or
3528 after POSITION, return ZV. A display string is either an overlay
3529 with `display' property whose value is a string, or a `display'
3530 text property whose value is a string. STRING is data about the
3531 string to iterate; if STRING->lstring is nil, we are iterating a
3532 buffer. FRAME_WINDOW_P is true when we are displaying a window
3533 on a GUI frame. DISP_PROP is set to zero if we searched
3534 MAX_DISP_SCAN characters forward without finding any display
3535 strings, non-zero otherwise. It is set to 2 if the display string
3536 uses any kind of `(space ...)' spec that will produce a stretch of
3537 white space in the text area. */
3538 ptrdiff_t
3539 compute_display_string_pos (struct text_pos *position,
3540 struct bidi_string_data *string,
3541 struct window *w,
3542 bool frame_window_p, int *disp_prop)
3544 /* OBJECT = nil means current buffer. */
3545 Lisp_Object object, object1;
3546 Lisp_Object pos, spec, limpos;
3547 bool string_p = string && (STRINGP (string->lstring) || string->s);
3548 ptrdiff_t eob = string_p ? string->schars : ZV;
3549 ptrdiff_t begb = string_p ? 0 : BEGV;
3550 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3551 ptrdiff_t lim =
3552 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3553 struct text_pos tpos;
3554 int rv = 0;
3556 if (string && STRINGP (string->lstring))
3557 object1 = object = string->lstring;
3558 else if (w && !string_p)
3560 XSETWINDOW (object, w);
3561 object1 = Qnil;
3563 else
3564 object1 = object = Qnil;
3566 *disp_prop = 1;
3568 if (charpos >= eob
3569 /* We don't support display properties whose values are strings
3570 that have display string properties. */
3571 || string->from_disp_str
3572 /* C strings cannot have display properties. */
3573 || (string->s && !STRINGP (object)))
3575 *disp_prop = 0;
3576 return eob;
3579 /* If the character at CHARPOS is where the display string begins,
3580 return CHARPOS. */
3581 pos = make_number (charpos);
3582 if (STRINGP (object))
3583 bufpos = string->bufpos;
3584 else
3585 bufpos = charpos;
3586 tpos = *position;
3587 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3588 && (charpos <= begb
3589 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3590 object),
3591 spec))
3592 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3593 frame_window_p)))
3595 if (rv == 2)
3596 *disp_prop = 2;
3597 return charpos;
3600 /* Look forward for the first character with a `display' property
3601 that will replace the underlying text when displayed. */
3602 limpos = make_number (lim);
3603 do {
3604 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3605 CHARPOS (tpos) = XFASTINT (pos);
3606 if (CHARPOS (tpos) >= lim)
3608 *disp_prop = 0;
3609 break;
3611 if (STRINGP (object))
3612 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3613 else
3614 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3615 spec = Fget_char_property (pos, Qdisplay, object);
3616 if (!STRINGP (object))
3617 bufpos = CHARPOS (tpos);
3618 } while (NILP (spec)
3619 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3620 bufpos, frame_window_p)));
3621 if (rv == 2)
3622 *disp_prop = 2;
3624 return CHARPOS (tpos);
3627 /* Return the character position of the end of the display string that
3628 started at CHARPOS. If there's no display string at CHARPOS,
3629 return -1. A display string is either an overlay with `display'
3630 property whose value is a string or a `display' text property whose
3631 value is a string. */
3632 ptrdiff_t
3633 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3635 /* OBJECT = nil means current buffer. */
3636 Lisp_Object object =
3637 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3638 Lisp_Object pos = make_number (charpos);
3639 ptrdiff_t eob =
3640 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3642 if (charpos >= eob || (string->s && !STRINGP (object)))
3643 return eob;
3645 /* It could happen that the display property or overlay was removed
3646 since we found it in compute_display_string_pos above. One way
3647 this can happen is if JIT font-lock was called (through
3648 handle_fontified_prop), and jit-lock-functions remove text
3649 properties or overlays from the portion of buffer that includes
3650 CHARPOS. Muse mode is known to do that, for example. In this
3651 case, we return -1 to the caller, to signal that no display
3652 string is actually present at CHARPOS. See bidi_fetch_char for
3653 how this is handled.
3655 An alternative would be to never look for display properties past
3656 it->stop_charpos. But neither compute_display_string_pos nor
3657 bidi_fetch_char that calls it know or care where the next
3658 stop_charpos is. */
3659 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3660 return -1;
3662 /* Look forward for the first character where the `display' property
3663 changes. */
3664 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3666 return XFASTINT (pos);
3671 /***********************************************************************
3672 Fontification
3673 ***********************************************************************/
3675 /* Handle changes in the `fontified' property of the current buffer by
3676 calling hook functions from Qfontification_functions to fontify
3677 regions of text. */
3679 static enum prop_handled
3680 handle_fontified_prop (struct it *it)
3682 Lisp_Object prop, pos;
3683 enum prop_handled handled = HANDLED_NORMALLY;
3685 if (!NILP (Vmemory_full))
3686 return handled;
3688 /* Get the value of the `fontified' property at IT's current buffer
3689 position. (The `fontified' property doesn't have a special
3690 meaning in strings.) If the value is nil, call functions from
3691 Qfontification_functions. */
3692 if (!STRINGP (it->string)
3693 && it->s == NULL
3694 && !NILP (Vfontification_functions)
3695 && !NILP (Vrun_hooks)
3696 && (pos = make_number (IT_CHARPOS (*it)),
3697 prop = Fget_char_property (pos, Qfontified, Qnil),
3698 /* Ignore the special cased nil value always present at EOB since
3699 no amount of fontifying will be able to change it. */
3700 NILP (prop) && IT_CHARPOS (*it) < Z))
3702 ptrdiff_t count = SPECPDL_INDEX ();
3703 Lisp_Object val;
3704 struct buffer *obuf = current_buffer;
3705 ptrdiff_t begv = BEGV, zv = ZV;
3706 bool old_clip_changed = current_buffer->clip_changed;
3708 val = Vfontification_functions;
3709 specbind (Qfontification_functions, Qnil);
3711 eassert (it->end_charpos == ZV);
3713 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3714 safe_call1 (val, pos);
3715 else
3717 Lisp_Object fns, fn;
3718 struct gcpro gcpro1, gcpro2;
3720 fns = Qnil;
3721 GCPRO2 (val, fns);
3723 for (; CONSP (val); val = XCDR (val))
3725 fn = XCAR (val);
3727 if (EQ (fn, Qt))
3729 /* A value of t indicates this hook has a local
3730 binding; it means to run the global binding too.
3731 In a global value, t should not occur. If it
3732 does, we must ignore it to avoid an endless
3733 loop. */
3734 for (fns = Fdefault_value (Qfontification_functions);
3735 CONSP (fns);
3736 fns = XCDR (fns))
3738 fn = XCAR (fns);
3739 if (!EQ (fn, Qt))
3740 safe_call1 (fn, pos);
3743 else
3744 safe_call1 (fn, pos);
3747 UNGCPRO;
3750 unbind_to (count, Qnil);
3752 /* Fontification functions routinely call `save-restriction'.
3753 Normally, this tags clip_changed, which can confuse redisplay
3754 (see discussion in Bug#6671). Since we don't perform any
3755 special handling of fontification changes in the case where
3756 `save-restriction' isn't called, there's no point doing so in
3757 this case either. So, if the buffer's restrictions are
3758 actually left unchanged, reset clip_changed. */
3759 if (obuf == current_buffer)
3761 if (begv == BEGV && zv == ZV)
3762 current_buffer->clip_changed = old_clip_changed;
3764 /* There isn't much we can reasonably do to protect against
3765 misbehaving fontification, but here's a fig leaf. */
3766 else if (BUFFER_LIVE_P (obuf))
3767 set_buffer_internal_1 (obuf);
3769 /* The fontification code may have added/removed text.
3770 It could do even a lot worse, but let's at least protect against
3771 the most obvious case where only the text past `pos' gets changed',
3772 as is/was done in grep.el where some escapes sequences are turned
3773 into face properties (bug#7876). */
3774 it->end_charpos = ZV;
3776 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3777 something. This avoids an endless loop if they failed to
3778 fontify the text for which reason ever. */
3779 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3780 handled = HANDLED_RECOMPUTE_PROPS;
3783 return handled;
3788 /***********************************************************************
3789 Faces
3790 ***********************************************************************/
3792 /* Set up iterator IT from face properties at its current position.
3793 Called from handle_stop. */
3795 static enum prop_handled
3796 handle_face_prop (struct it *it)
3798 int new_face_id;
3799 ptrdiff_t next_stop;
3801 if (!STRINGP (it->string))
3803 new_face_id
3804 = face_at_buffer_position (it->w,
3805 IT_CHARPOS (*it),
3806 &next_stop,
3807 (IT_CHARPOS (*it)
3808 + TEXT_PROP_DISTANCE_LIMIT),
3809 false, it->base_face_id);
3811 /* Is this a start of a run of characters with box face?
3812 Caveat: this can be called for a freshly initialized
3813 iterator; face_id is -1 in this case. We know that the new
3814 face will not change until limit, i.e. if the new face has a
3815 box, all characters up to limit will have one. But, as
3816 usual, we don't know whether limit is really the end. */
3817 if (new_face_id != it->face_id)
3819 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3820 /* If it->face_id is -1, old_face below will be NULL, see
3821 the definition of FACE_FROM_ID. This will happen if this
3822 is the initial call that gets the face. */
3823 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3825 /* If the value of face_id of the iterator is -1, we have to
3826 look in front of IT's position and see whether there is a
3827 face there that's different from new_face_id. */
3828 if (!old_face && IT_CHARPOS (*it) > BEG)
3830 int prev_face_id = face_before_it_pos (it);
3832 old_face = FACE_FROM_ID (it->f, prev_face_id);
3835 /* If the new face has a box, but the old face does not,
3836 this is the start of a run of characters with box face,
3837 i.e. this character has a shadow on the left side. */
3838 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3839 && (old_face == NULL || !old_face->box));
3840 it->face_box_p = new_face->box != FACE_NO_BOX;
3843 else
3845 int base_face_id;
3846 ptrdiff_t bufpos;
3847 int i;
3848 Lisp_Object from_overlay
3849 = (it->current.overlay_string_index >= 0
3850 ? it->string_overlays[it->current.overlay_string_index
3851 % OVERLAY_STRING_CHUNK_SIZE]
3852 : Qnil);
3854 /* See if we got to this string directly or indirectly from
3855 an overlay property. That includes the before-string or
3856 after-string of an overlay, strings in display properties
3857 provided by an overlay, their text properties, etc.
3859 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3860 if (! NILP (from_overlay))
3861 for (i = it->sp - 1; i >= 0; i--)
3863 if (it->stack[i].current.overlay_string_index >= 0)
3864 from_overlay
3865 = it->string_overlays[it->stack[i].current.overlay_string_index
3866 % OVERLAY_STRING_CHUNK_SIZE];
3867 else if (! NILP (it->stack[i].from_overlay))
3868 from_overlay = it->stack[i].from_overlay;
3870 if (!NILP (from_overlay))
3871 break;
3874 if (! NILP (from_overlay))
3876 bufpos = IT_CHARPOS (*it);
3877 /* For a string from an overlay, the base face depends
3878 only on text properties and ignores overlays. */
3879 base_face_id
3880 = face_for_overlay_string (it->w,
3881 IT_CHARPOS (*it),
3882 &next_stop,
3883 (IT_CHARPOS (*it)
3884 + TEXT_PROP_DISTANCE_LIMIT),
3885 false,
3886 from_overlay);
3888 else
3890 bufpos = 0;
3892 /* For strings from a `display' property, use the face at
3893 IT's current buffer position as the base face to merge
3894 with, so that overlay strings appear in the same face as
3895 surrounding text, unless they specify their own faces.
3896 For strings from wrap-prefix and line-prefix properties,
3897 use the default face, possibly remapped via
3898 Vface_remapping_alist. */
3899 /* Note that the fact that we use the face at _buffer_
3900 position means that a 'display' property on an overlay
3901 string will not inherit the face of that overlay string,
3902 but will instead revert to the face of buffer text
3903 covered by the overlay. This is visible, e.g., when the
3904 overlay specifies a box face, but neither the buffer nor
3905 the display string do. This sounds like a design bug,
3906 but Emacs always did that since v21.1, so changing that
3907 might be a big deal. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? (!NILP (Vface_remapping_alist)
3910 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3911 : DEFAULT_FACE_ID)
3912 : underlying_face_id (it);
3915 new_face_id = face_at_string_position (it->w,
3916 it->string,
3917 IT_STRING_CHARPOS (*it),
3918 bufpos,
3919 &next_stop,
3920 base_face_id, false);
3922 /* Is this a start of a run of characters with box? Caveat:
3923 this can be called for a freshly allocated iterator; face_id
3924 is -1 is this case. We know that the new face will not
3925 change until the next check pos, i.e. if the new face has a
3926 box, all characters up to that position will have a
3927 box. But, as usual, we don't know whether that position
3928 is really the end. */
3929 if (new_face_id != it->face_id)
3931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3934 /* If new face has a box but old face hasn't, this is the
3935 start of a run of characters with box, i.e. it has a
3936 shadow on the left side. */
3937 it->start_of_box_run_p
3938 = new_face->box && (old_face == NULL || !old_face->box);
3939 it->face_box_p = new_face->box != FACE_NO_BOX;
3943 it->face_id = new_face_id;
3944 return HANDLED_NORMALLY;
3948 /* Return the ID of the face ``underlying'' IT's current position,
3949 which is in a string. If the iterator is associated with a
3950 buffer, return the face at IT's current buffer position.
3951 Otherwise, use the iterator's base_face_id. */
3953 static int
3954 underlying_face_id (struct it *it)
3956 int face_id = it->base_face_id, i;
3958 eassert (STRINGP (it->string));
3960 for (i = it->sp - 1; i >= 0; --i)
3961 if (NILP (it->stack[i].string))
3962 face_id = it->stack[i].face_id;
3964 return face_id;
3968 /* Compute the face one character before or after the current position
3969 of IT, in the visual order. BEFORE_P means get the face
3970 in front (to the left in L2R paragraphs, to the right in R2L
3971 paragraphs) of IT's screen position. Value is the ID of the face. */
3973 static int
3974 face_before_or_after_it_pos (struct it *it, bool before_p)
3976 int face_id, limit;
3977 ptrdiff_t next_check_charpos;
3978 struct it it_copy;
3979 void *it_copy_data = NULL;
3981 eassert (it->s == NULL);
3983 if (STRINGP (it->string))
3985 ptrdiff_t bufpos, charpos;
3986 int base_face_id;
3988 /* No face change past the end of the string (for the case
3989 we are padding with spaces). No face change before the
3990 string start. */
3991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3993 return it->face_id;
3995 if (!it->bidi_p)
3997 /* Set charpos to the position before or after IT's current
3998 position, in the logical order, which in the non-bidi
3999 case is the same as the visual order. */
4000 if (before_p)
4001 charpos = IT_STRING_CHARPOS (*it) - 1;
4002 else if (it->what == IT_COMPOSITION)
4003 /* For composition, we must check the character after the
4004 composition. */
4005 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4006 else
4007 charpos = IT_STRING_CHARPOS (*it) + 1;
4009 else
4011 if (before_p)
4013 /* With bidi iteration, the character before the current
4014 in the visual order cannot be found by simple
4015 iteration, because "reverse" reordering is not
4016 supported. Instead, we need to use the move_it_*
4017 family of functions. */
4018 /* Ignore face changes before the first visible
4019 character on this display line. */
4020 if (it->current_x <= it->first_visible_x)
4021 return it->face_id;
4022 SAVE_IT (it_copy, *it, it_copy_data);
4023 /* Implementation note: Since move_it_in_display_line
4024 works in the iterator geometry, and thinks the first
4025 character is always the leftmost, even in R2L lines,
4026 we don't need to distinguish between the R2L and L2R
4027 cases here. */
4028 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4029 it_copy.current_x - 1, MOVE_TO_X);
4030 charpos = IT_STRING_CHARPOS (it_copy);
4031 RESTORE_IT (it, it, it_copy_data);
4033 else
4035 /* Set charpos to the string position of the character
4036 that comes after IT's current position in the visual
4037 order. */
4038 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4040 it_copy = *it;
4041 while (n--)
4042 bidi_move_to_visually_next (&it_copy.bidi_it);
4044 charpos = it_copy.bidi_it.charpos;
4047 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4049 if (it->current.overlay_string_index >= 0)
4050 bufpos = IT_CHARPOS (*it);
4051 else
4052 bufpos = 0;
4054 base_face_id = underlying_face_id (it);
4056 /* Get the face for ASCII, or unibyte. */
4057 face_id = face_at_string_position (it->w,
4058 it->string,
4059 charpos,
4060 bufpos,
4061 &next_check_charpos,
4062 base_face_id, false);
4064 /* Correct the face for charsets different from ASCII. Do it
4065 for the multibyte case only. The face returned above is
4066 suitable for unibyte text if IT->string is unibyte. */
4067 if (STRING_MULTIBYTE (it->string))
4069 struct text_pos pos1 = string_pos (charpos, it->string);
4070 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4071 int c, len;
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4074 c = string_char_and_length (p, &len);
4075 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4078 else
4080 struct text_pos pos;
4082 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4083 || (IT_CHARPOS (*it) <= BEGV && before_p))
4084 return it->face_id;
4086 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4087 pos = it->current.pos;
4089 if (!it->bidi_p)
4091 if (before_p)
4092 DEC_TEXT_POS (pos, it->multibyte_p);
4093 else
4095 if (it->what == IT_COMPOSITION)
4097 /* For composition, we must check the position after
4098 the composition. */
4099 pos.charpos += it->cmp_it.nchars;
4100 pos.bytepos += it->len;
4102 else
4103 INC_TEXT_POS (pos, it->multibyte_p);
4106 else
4108 if (before_p)
4110 /* With bidi iteration, the character before the current
4111 in the visual order cannot be found by simple
4112 iteration, because "reverse" reordering is not
4113 supported. Instead, we need to use the move_it_*
4114 family of functions. */
4115 /* Ignore face changes before the first visible
4116 character on this display line. */
4117 if (it->current_x <= it->first_visible_x)
4118 return it->face_id;
4119 SAVE_IT (it_copy, *it, it_copy_data);
4120 /* Implementation note: Since move_it_in_display_line
4121 works in the iterator geometry, and thinks the first
4122 character is always the leftmost, even in R2L lines,
4123 we don't need to distinguish between the R2L and L2R
4124 cases here. */
4125 move_it_in_display_line (&it_copy, ZV,
4126 it_copy.current_x - 1, MOVE_TO_X);
4127 pos = it_copy.current.pos;
4128 RESTORE_IT (it, it, it_copy_data);
4130 else
4132 /* Set charpos to the buffer position of the character
4133 that comes after IT's current position in the visual
4134 order. */
4135 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4137 it_copy = *it;
4138 while (n--)
4139 bidi_move_to_visually_next (&it_copy.bidi_it);
4141 SET_TEXT_POS (pos,
4142 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4145 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4147 /* Determine face for CHARSET_ASCII, or unibyte. */
4148 face_id = face_at_buffer_position (it->w,
4149 CHARPOS (pos),
4150 &next_check_charpos,
4151 limit, false, -1);
4153 /* Correct the face for charsets different from ASCII. Do it
4154 for the multibyte case only. The face returned above is
4155 suitable for unibyte text if current_buffer is unibyte. */
4156 if (it->multibyte_p)
4158 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4159 struct face *face = FACE_FROM_ID (it->f, face_id);
4160 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4164 return face_id;
4169 /***********************************************************************
4170 Invisible text
4171 ***********************************************************************/
4173 /* Set up iterator IT from invisible properties at its current
4174 position. Called from handle_stop. */
4176 static enum prop_handled
4177 handle_invisible_prop (struct it *it)
4179 enum prop_handled handled = HANDLED_NORMALLY;
4180 int invis;
4181 Lisp_Object prop;
4183 if (STRINGP (it->string))
4185 Lisp_Object end_charpos, limit, charpos;
4187 /* Get the value of the invisible text property at the
4188 current position. Value will be nil if there is no such
4189 property. */
4190 charpos = make_number (IT_STRING_CHARPOS (*it));
4191 prop = Fget_text_property (charpos, Qinvisible, it->string);
4192 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4194 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4196 /* Record whether we have to display an ellipsis for the
4197 invisible text. */
4198 bool display_ellipsis_p = (invis == 2);
4199 ptrdiff_t len, endpos;
4201 handled = HANDLED_RECOMPUTE_PROPS;
4203 /* Get the position at which the next visible text can be
4204 found in IT->string, if any. */
4205 endpos = len = SCHARS (it->string);
4206 XSETINT (limit, len);
4209 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4210 it->string, limit);
4211 if (INTEGERP (end_charpos))
4213 endpos = XFASTINT (end_charpos);
4214 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4215 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4216 if (invis == 2)
4217 display_ellipsis_p = true;
4220 while (invis != 0 && endpos < len);
4222 if (display_ellipsis_p)
4223 it->ellipsis_p = true;
4225 if (endpos < len)
4227 /* Text at END_CHARPOS is visible. Move IT there. */
4228 struct text_pos old;
4229 ptrdiff_t oldpos;
4231 old = it->current.string_pos;
4232 oldpos = CHARPOS (old);
4233 if (it->bidi_p)
4235 if (it->bidi_it.first_elt
4236 && it->bidi_it.charpos < SCHARS (it->string))
4237 bidi_paragraph_init (it->paragraph_embedding,
4238 &it->bidi_it, true);
4239 /* Bidi-iterate out of the invisible text. */
4242 bidi_move_to_visually_next (&it->bidi_it);
4244 while (oldpos <= it->bidi_it.charpos
4245 && it->bidi_it.charpos < endpos);
4247 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4249 if (IT_CHARPOS (*it) >= endpos)
4250 it->prev_stop = endpos;
4252 else
4254 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4255 compute_string_pos (&it->current.string_pos, old, it->string);
4258 else
4260 /* The rest of the string is invisible. If this is an
4261 overlay string, proceed with the next overlay string
4262 or whatever comes and return a character from there. */
4263 if (it->current.overlay_string_index >= 0
4264 && !display_ellipsis_p)
4266 next_overlay_string (it);
4267 /* Don't check for overlay strings when we just
4268 finished processing them. */
4269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4271 else
4273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4279 else
4281 ptrdiff_t newpos, next_stop, start_charpos, tem;
4282 Lisp_Object pos, overlay;
4284 /* First of all, is there invisible text at this position? */
4285 tem = start_charpos = IT_CHARPOS (*it);
4286 pos = make_number (tem);
4287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4288 &overlay);
4289 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4291 /* If we are on invisible text, skip over it. */
4292 if (invis != 0 && start_charpos < it->end_charpos)
4294 /* Record whether we have to display an ellipsis for the
4295 invisible text. */
4296 bool display_ellipsis_p = invis == 2;
4298 handled = HANDLED_RECOMPUTE_PROPS;
4300 /* Loop skipping over invisible text. The loop is left at
4301 ZV or with IT on the first char being visible again. */
4304 /* Try to skip some invisible text. Return value is the
4305 position reached which can be equal to where we start
4306 if there is nothing invisible there. This skips both
4307 over invisible text properties and overlays with
4308 invisible property. */
4309 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4311 /* If we skipped nothing at all we weren't at invisible
4312 text in the first place. If everything to the end of
4313 the buffer was skipped, end the loop. */
4314 if (newpos == tem || newpos >= ZV)
4315 invis = 0;
4316 else
4318 /* We skipped some characters but not necessarily
4319 all there are. Check if we ended up on visible
4320 text. Fget_char_property returns the property of
4321 the char before the given position, i.e. if we
4322 get invis = 0, this means that the char at
4323 newpos is visible. */
4324 pos = make_number (newpos);
4325 prop = Fget_char_property (pos, Qinvisible, it->window);
4326 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4329 /* If we ended up on invisible text, proceed to
4330 skip starting with next_stop. */
4331 if (invis != 0)
4332 tem = next_stop;
4334 /* If there are adjacent invisible texts, don't lose the
4335 second one's ellipsis. */
4336 if (invis == 2)
4337 display_ellipsis_p = true;
4339 while (invis != 0);
4341 /* The position newpos is now either ZV or on visible text. */
4342 if (it->bidi_p)
4344 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4345 bool on_newline
4346 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4347 bool after_newline
4348 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4350 /* If the invisible text ends on a newline or on a
4351 character after a newline, we can avoid the costly,
4352 character by character, bidi iteration to NEWPOS, and
4353 instead simply reseat the iterator there. That's
4354 because all bidi reordering information is tossed at
4355 the newline. This is a big win for modes that hide
4356 complete lines, like Outline, Org, etc. */
4357 if (on_newline || after_newline)
4359 struct text_pos tpos;
4360 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4362 SET_TEXT_POS (tpos, newpos, bpos);
4363 reseat_1 (it, tpos, false);
4364 /* If we reseat on a newline/ZV, we need to prep the
4365 bidi iterator for advancing to the next character
4366 after the newline/EOB, keeping the current paragraph
4367 direction (so that PRODUCE_GLYPHS does TRT wrt
4368 prepending/appending glyphs to a glyph row). */
4369 if (on_newline)
4371 it->bidi_it.first_elt = false;
4372 it->bidi_it.paragraph_dir = pdir;
4373 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4374 it->bidi_it.nchars = 1;
4375 it->bidi_it.ch_len = 1;
4378 else /* Must use the slow method. */
4380 /* With bidi iteration, the region of invisible text
4381 could start and/or end in the middle of a
4382 non-base embedding level. Therefore, we need to
4383 skip invisible text using the bidi iterator,
4384 starting at IT's current position, until we find
4385 ourselves outside of the invisible text.
4386 Skipping invisible text _after_ bidi iteration
4387 avoids affecting the visual order of the
4388 displayed text when invisible properties are
4389 added or removed. */
4390 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4392 /* If we were `reseat'ed to a new paragraph,
4393 determine the paragraph base direction. We
4394 need to do it now because
4395 next_element_from_buffer may not have a
4396 chance to do it, if we are going to skip any
4397 text at the beginning, which resets the
4398 FIRST_ELT flag. */
4399 bidi_paragraph_init (it->paragraph_embedding,
4400 &it->bidi_it, true);
4404 bidi_move_to_visually_next (&it->bidi_it);
4406 while (it->stop_charpos <= it->bidi_it.charpos
4407 && it->bidi_it.charpos < newpos);
4408 IT_CHARPOS (*it) = it->bidi_it.charpos;
4409 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4410 /* If we overstepped NEWPOS, record its position in
4411 the iterator, so that we skip invisible text if
4412 later the bidi iteration lands us in the
4413 invisible region again. */
4414 if (IT_CHARPOS (*it) >= newpos)
4415 it->prev_stop = newpos;
4418 else
4420 IT_CHARPOS (*it) = newpos;
4421 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4424 if (display_ellipsis_p)
4426 /* Make sure that the glyphs of the ellipsis will get
4427 correct `charpos' values. If we would not update
4428 it->position here, the glyphs would belong to the
4429 last visible character _before_ the invisible
4430 text, which confuses `set_cursor_from_row'.
4432 We use the last invisible position instead of the
4433 first because this way the cursor is always drawn on
4434 the first "." of the ellipsis, whenever PT is inside
4435 the invisible text. Otherwise the cursor would be
4436 placed _after_ the ellipsis when the point is after the
4437 first invisible character. */
4438 if (!STRINGP (it->object))
4440 it->position.charpos = newpos - 1;
4441 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4445 /* If there are before-strings at the start of invisible
4446 text, and the text is invisible because of a text
4447 property, arrange to show before-strings because 20.x did
4448 it that way. (If the text is invisible because of an
4449 overlay property instead of a text property, this is
4450 already handled in the overlay code.) */
4451 if (NILP (overlay)
4452 && get_overlay_strings (it, it->stop_charpos))
4454 handled = HANDLED_RECOMPUTE_PROPS;
4455 if (it->sp > 0)
4457 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4458 /* The call to get_overlay_strings above recomputes
4459 it->stop_charpos, but it only considers changes
4460 in properties and overlays beyond iterator's
4461 current position. This causes us to miss changes
4462 that happen exactly where the invisible property
4463 ended. So we play it safe here and force the
4464 iterator to check for potential stop positions
4465 immediately after the invisible text. Note that
4466 if get_overlay_strings returns true, it
4467 normally also pushed the iterator stack, so we
4468 need to update the stop position in the slot
4469 below the current one. */
4470 it->stack[it->sp - 1].stop_charpos
4471 = CHARPOS (it->stack[it->sp - 1].current.pos);
4474 else if (display_ellipsis_p)
4476 it->ellipsis_p = true;
4477 /* Let the ellipsis display before
4478 considering any properties of the following char.
4479 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4480 handled = HANDLED_RETURN;
4485 return handled;
4489 /* Make iterator IT return `...' next.
4490 Replaces LEN characters from buffer. */
4492 static void
4493 setup_for_ellipsis (struct it *it, int len)
4495 /* Use the display table definition for `...'. Invalid glyphs
4496 will be handled by the method returning elements from dpvec. */
4497 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4499 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4500 it->dpvec = v->contents;
4501 it->dpend = v->contents + v->header.size;
4503 else
4505 /* Default `...'. */
4506 it->dpvec = default_invis_vector;
4507 it->dpend = default_invis_vector + 3;
4510 it->dpvec_char_len = len;
4511 it->current.dpvec_index = 0;
4512 it->dpvec_face_id = -1;
4514 /* Remember the current face id in case glyphs specify faces.
4515 IT's face is restored in set_iterator_to_next.
4516 saved_face_id was set to preceding char's face in handle_stop. */
4517 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4518 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4520 /* If the ellipsis represents buffer text, it means we advanced in
4521 the buffer, so we should no longer ignore overlay strings. */
4522 if (it->method == GET_FROM_BUFFER)
4523 it->ignore_overlay_strings_at_pos_p = false;
4525 it->method = GET_FROM_DISPLAY_VECTOR;
4526 it->ellipsis_p = true;
4531 /***********************************************************************
4532 'display' property
4533 ***********************************************************************/
4535 /* Set up iterator IT from `display' property at its current position.
4536 Called from handle_stop.
4537 We return HANDLED_RETURN if some part of the display property
4538 overrides the display of the buffer text itself.
4539 Otherwise we return HANDLED_NORMALLY. */
4541 static enum prop_handled
4542 handle_display_prop (struct it *it)
4544 Lisp_Object propval, object, overlay;
4545 struct text_pos *position;
4546 ptrdiff_t bufpos;
4547 /* Nonzero if some property replaces the display of the text itself. */
4548 int display_replaced = 0;
4550 if (STRINGP (it->string))
4552 object = it->string;
4553 position = &it->current.string_pos;
4554 bufpos = CHARPOS (it->current.pos);
4556 else
4558 XSETWINDOW (object, it->w);
4559 position = &it->current.pos;
4560 bufpos = CHARPOS (*position);
4563 /* Reset those iterator values set from display property values. */
4564 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4565 it->space_width = Qnil;
4566 it->font_height = Qnil;
4567 it->voffset = 0;
4569 /* We don't support recursive `display' properties, i.e. string
4570 values that have a string `display' property, that have a string
4571 `display' property etc. */
4572 if (!it->string_from_display_prop_p)
4573 it->area = TEXT_AREA;
4575 propval = get_char_property_and_overlay (make_number (position->charpos),
4576 Qdisplay, object, &overlay);
4577 if (NILP (propval))
4578 return HANDLED_NORMALLY;
4579 /* Now OVERLAY is the overlay that gave us this property, or nil
4580 if it was a text property. */
4582 if (!STRINGP (it->string))
4583 object = it->w->contents;
4585 display_replaced = handle_display_spec (it, propval, object, overlay,
4586 position, bufpos,
4587 FRAME_WINDOW_P (it->f));
4588 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4591 /* Subroutine of handle_display_prop. Returns non-zero if the display
4592 specification in SPEC is a replacing specification, i.e. it would
4593 replace the text covered by `display' property with something else,
4594 such as an image or a display string. If SPEC includes any kind or
4595 `(space ...) specification, the value is 2; this is used by
4596 compute_display_string_pos, which see.
4598 See handle_single_display_spec for documentation of arguments.
4599 FRAME_WINDOW_P is true if the window being redisplayed is on a
4600 GUI frame; this argument is used only if IT is NULL, see below.
4602 IT can be NULL, if this is called by the bidi reordering code
4603 through compute_display_string_pos, which see. In that case, this
4604 function only examines SPEC, but does not otherwise "handle" it, in
4605 the sense that it doesn't set up members of IT from the display
4606 spec. */
4607 static int
4608 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4609 Lisp_Object overlay, struct text_pos *position,
4610 ptrdiff_t bufpos, bool frame_window_p)
4612 int replacing = 0;
4614 if (CONSP (spec)
4615 /* Simple specifications. */
4616 && !EQ (XCAR (spec), Qimage)
4617 && !EQ (XCAR (spec), Qspace)
4618 && !EQ (XCAR (spec), Qwhen)
4619 && !EQ (XCAR (spec), Qslice)
4620 && !EQ (XCAR (spec), Qspace_width)
4621 && !EQ (XCAR (spec), Qheight)
4622 && !EQ (XCAR (spec), Qraise)
4623 /* Marginal area specifications. */
4624 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4625 && !EQ (XCAR (spec), Qleft_fringe)
4626 && !EQ (XCAR (spec), Qright_fringe)
4627 && !NILP (XCAR (spec)))
4629 for (; CONSP (spec); spec = XCDR (spec))
4631 int rv = handle_single_display_spec (it, XCAR (spec), object,
4632 overlay, position, bufpos,
4633 replacing, frame_window_p);
4634 if (rv != 0)
4636 replacing = rv;
4637 /* If some text in a string is replaced, `position' no
4638 longer points to the position of `object'. */
4639 if (!it || STRINGP (object))
4640 break;
4644 else if (VECTORP (spec))
4646 ptrdiff_t i;
4647 for (i = 0; i < ASIZE (spec); ++i)
4649 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4650 overlay, position, bufpos,
4651 replacing, frame_window_p);
4652 if (rv != 0)
4654 replacing = rv;
4655 /* If some text in a string is replaced, `position' no
4656 longer points to the position of `object'. */
4657 if (!it || STRINGP (object))
4658 break;
4662 else
4663 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4664 bufpos, 0, frame_window_p);
4665 return replacing;
4668 /* Value is the position of the end of the `display' property starting
4669 at START_POS in OBJECT. */
4671 static struct text_pos
4672 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4674 Lisp_Object end;
4675 struct text_pos end_pos;
4677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4678 Qdisplay, object, Qnil);
4679 CHARPOS (end_pos) = XFASTINT (end);
4680 if (STRINGP (object))
4681 compute_string_pos (&end_pos, start_pos, it->string);
4682 else
4683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4685 return end_pos;
4689 /* Set up IT from a single `display' property specification SPEC. OBJECT
4690 is the object in which the `display' property was found. *POSITION
4691 is the position in OBJECT at which the `display' property was found.
4692 BUFPOS is the buffer position of OBJECT (different from POSITION if
4693 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4694 previously saw a display specification which already replaced text
4695 display with something else, for example an image; we ignore such
4696 properties after the first one has been processed.
4698 OVERLAY is the overlay this `display' property came from,
4699 or nil if it was a text property.
4701 If SPEC is a `space' or `image' specification, and in some other
4702 cases too, set *POSITION to the position where the `display'
4703 property ends.
4705 If IT is NULL, only examine the property specification in SPEC, but
4706 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4707 is intended to be displayed in a window on a GUI frame.
4709 Value is non-zero if something was found which replaces the display
4710 of buffer or string text. */
4712 static int
4713 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4714 Lisp_Object overlay, struct text_pos *position,
4715 ptrdiff_t bufpos, int display_replaced,
4716 bool frame_window_p)
4718 Lisp_Object form;
4719 Lisp_Object location, value;
4720 struct text_pos start_pos = *position;
4722 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4723 If the result is non-nil, use VALUE instead of SPEC. */
4724 form = Qt;
4725 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4727 spec = XCDR (spec);
4728 if (!CONSP (spec))
4729 return 0;
4730 form = XCAR (spec);
4731 spec = XCDR (spec);
4734 if (!NILP (form) && !EQ (form, Qt))
4736 ptrdiff_t count = SPECPDL_INDEX ();
4737 struct gcpro gcpro1;
4739 /* Bind `object' to the object having the `display' property, a
4740 buffer or string. Bind `position' to the position in the
4741 object where the property was found, and `buffer-position'
4742 to the current position in the buffer. */
4744 if (NILP (object))
4745 XSETBUFFER (object, current_buffer);
4746 specbind (Qobject, object);
4747 specbind (Qposition, make_number (CHARPOS (*position)));
4748 specbind (Qbuffer_position, make_number (bufpos));
4749 GCPRO1 (form);
4750 form = safe_eval (form);
4751 UNGCPRO;
4752 unbind_to (count, Qnil);
4755 if (NILP (form))
4756 return 0;
4758 /* Handle `(height HEIGHT)' specifications. */
4759 if (CONSP (spec)
4760 && EQ (XCAR (spec), Qheight)
4761 && CONSP (XCDR (spec)))
4763 if (it)
4765 if (!FRAME_WINDOW_P (it->f))
4766 return 0;
4768 it->font_height = XCAR (XCDR (spec));
4769 if (!NILP (it->font_height))
4771 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4772 int new_height = -1;
4774 if (CONSP (it->font_height)
4775 && (EQ (XCAR (it->font_height), Qplus)
4776 || EQ (XCAR (it->font_height), Qminus))
4777 && CONSP (XCDR (it->font_height))
4778 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4780 /* `(+ N)' or `(- N)' where N is an integer. */
4781 int steps = XINT (XCAR (XCDR (it->font_height)));
4782 if (EQ (XCAR (it->font_height), Qplus))
4783 steps = - steps;
4784 it->face_id = smaller_face (it->f, it->face_id, steps);
4786 else if (FUNCTIONP (it->font_height))
4788 /* Call function with current height as argument.
4789 Value is the new height. */
4790 Lisp_Object height;
4791 height = safe_call1 (it->font_height,
4792 face->lface[LFACE_HEIGHT_INDEX]);
4793 if (NUMBERP (height))
4794 new_height = XFLOATINT (height);
4796 else if (NUMBERP (it->font_height))
4798 /* Value is a multiple of the canonical char height. */
4799 struct face *f;
4801 f = FACE_FROM_ID (it->f,
4802 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4803 new_height = (XFLOATINT (it->font_height)
4804 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4806 else
4808 /* Evaluate IT->font_height with `height' bound to the
4809 current specified height to get the new height. */
4810 ptrdiff_t count = SPECPDL_INDEX ();
4812 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4813 value = safe_eval (it->font_height);
4814 unbind_to (count, Qnil);
4816 if (NUMBERP (value))
4817 new_height = XFLOATINT (value);
4820 if (new_height > 0)
4821 it->face_id = face_with_height (it->f, it->face_id, new_height);
4825 return 0;
4828 /* Handle `(space-width WIDTH)'. */
4829 if (CONSP (spec)
4830 && EQ (XCAR (spec), Qspace_width)
4831 && CONSP (XCDR (spec)))
4833 if (it)
4835 if (!FRAME_WINDOW_P (it->f))
4836 return 0;
4838 value = XCAR (XCDR (spec));
4839 if (NUMBERP (value) && XFLOATINT (value) > 0)
4840 it->space_width = value;
4843 return 0;
4846 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qslice))
4850 Lisp_Object tem;
4852 if (it)
4854 if (!FRAME_WINDOW_P (it->f))
4855 return 0;
4857 if (tem = XCDR (spec), CONSP (tem))
4859 it->slice.x = XCAR (tem);
4860 if (tem = XCDR (tem), CONSP (tem))
4862 it->slice.y = XCAR (tem);
4863 if (tem = XCDR (tem), CONSP (tem))
4865 it->slice.width = XCAR (tem);
4866 if (tem = XCDR (tem), CONSP (tem))
4867 it->slice.height = XCAR (tem);
4873 return 0;
4876 /* Handle `(raise FACTOR)'. */
4877 if (CONSP (spec)
4878 && EQ (XCAR (spec), Qraise)
4879 && CONSP (XCDR (spec)))
4881 if (it)
4883 if (!FRAME_WINDOW_P (it->f))
4884 return 0;
4886 #ifdef HAVE_WINDOW_SYSTEM
4887 value = XCAR (XCDR (spec));
4888 if (NUMBERP (value))
4890 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4891 it->voffset = - (XFLOATINT (value)
4892 * (FONT_HEIGHT (face->font)));
4894 #endif /* HAVE_WINDOW_SYSTEM */
4897 return 0;
4900 /* Don't handle the other kinds of display specifications
4901 inside a string that we got from a `display' property. */
4902 if (it && it->string_from_display_prop_p)
4903 return 0;
4905 /* Characters having this form of property are not displayed, so
4906 we have to find the end of the property. */
4907 if (it)
4909 start_pos = *position;
4910 *position = display_prop_end (it, object, start_pos);
4911 /* If the display property comes from an overlay, don't consider
4912 any potential stop_charpos values before the end of that
4913 overlay. Since display_prop_end will happily find another
4914 'display' property coming from some other overlay or text
4915 property on buffer positions before this overlay's end, we
4916 need to ignore them, or else we risk displaying this
4917 overlay's display string/image twice. */
4918 if (!NILP (overlay))
4920 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4922 if (ovendpos > CHARPOS (*position))
4923 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4926 value = Qnil;
4928 /* Stop the scan at that end position--we assume that all
4929 text properties change there. */
4930 if (it)
4931 it->stop_charpos = position->charpos;
4933 /* Handle `(left-fringe BITMAP [FACE])'
4934 and `(right-fringe BITMAP [FACE])'. */
4935 if (CONSP (spec)
4936 && (EQ (XCAR (spec), Qleft_fringe)
4937 || EQ (XCAR (spec), Qright_fringe))
4938 && CONSP (XCDR (spec)))
4940 int fringe_bitmap;
4942 if (it)
4944 if (!FRAME_WINDOW_P (it->f))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4948 /* Synchronize the bidi iterator with POSITION. This is
4949 needed because we are not going to push the iterator
4950 on behalf of this display property, so there will be
4951 no pop_it call to do this synchronization for us. */
4952 if (it->bidi_p)
4954 it->position = *position;
4955 iterate_out_of_display_property (it);
4956 *position = it->position;
4958 return 1;
4961 else if (!frame_window_p)
4962 return 1;
4964 #ifdef HAVE_WINDOW_SYSTEM
4965 value = XCAR (XCDR (spec));
4966 if (!SYMBOLP (value)
4967 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4968 /* If we return here, POSITION has been advanced
4969 across the text with this property. */
4971 if (it && it->bidi_p)
4973 it->position = *position;
4974 iterate_out_of_display_property (it);
4975 *position = it->position;
4977 return 1;
4980 if (it)
4982 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4984 if (CONSP (XCDR (XCDR (spec))))
4986 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4987 int face_id2 = lookup_derived_face (it->f, face_name,
4988 FRINGE_FACE_ID, false);
4989 if (face_id2 >= 0)
4990 face_id = face_id2;
4993 /* Save current settings of IT so that we can restore them
4994 when we are finished with the glyph property value. */
4995 push_it (it, position);
4997 it->area = TEXT_AREA;
4998 it->what = IT_IMAGE;
4999 it->image_id = -1; /* no image */
5000 it->position = start_pos;
5001 it->object = NILP (object) ? it->w->contents : object;
5002 it->method = GET_FROM_IMAGE;
5003 it->from_overlay = Qnil;
5004 it->face_id = face_id;
5005 it->from_disp_prop_p = true;
5007 /* Say that we haven't consumed the characters with
5008 `display' property yet. The call to pop_it in
5009 set_iterator_to_next will clean this up. */
5010 *position = start_pos;
5012 if (EQ (XCAR (spec), Qleft_fringe))
5014 it->left_user_fringe_bitmap = fringe_bitmap;
5015 it->left_user_fringe_face_id = face_id;
5017 else
5019 it->right_user_fringe_bitmap = fringe_bitmap;
5020 it->right_user_fringe_face_id = face_id;
5023 #endif /* HAVE_WINDOW_SYSTEM */
5024 return 1;
5027 /* Prepare to handle `((margin left-margin) ...)',
5028 `((margin right-margin) ...)' and `((margin nil) ...)'
5029 prefixes for display specifications. */
5030 location = Qunbound;
5031 if (CONSP (spec) && CONSP (XCAR (spec)))
5033 Lisp_Object tem;
5035 value = XCDR (spec);
5036 if (CONSP (value))
5037 value = XCAR (value);
5039 tem = XCAR (spec);
5040 if (EQ (XCAR (tem), Qmargin)
5041 && (tem = XCDR (tem),
5042 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5043 (NILP (tem)
5044 || EQ (tem, Qleft_margin)
5045 || EQ (tem, Qright_margin))))
5046 location = tem;
5049 if (EQ (location, Qunbound))
5051 location = Qnil;
5052 value = spec;
5055 /* After this point, VALUE is the property after any
5056 margin prefix has been stripped. It must be a string,
5057 an image specification, or `(space ...)'.
5059 LOCATION specifies where to display: `left-margin',
5060 `right-margin' or nil. */
5062 bool valid_p = (STRINGP (value)
5063 #ifdef HAVE_WINDOW_SYSTEM
5064 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5065 && valid_image_p (value))
5066 #endif /* not HAVE_WINDOW_SYSTEM */
5067 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5069 if (valid_p && display_replaced == 0)
5071 int retval = 1;
5073 if (!it)
5075 /* Callers need to know whether the display spec is any kind
5076 of `(space ...)' spec that is about to affect text-area
5077 display. */
5078 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5079 retval = 2;
5080 return retval;
5083 /* Save current settings of IT so that we can restore them
5084 when we are finished with the glyph property value. */
5085 push_it (it, position);
5086 it->from_overlay = overlay;
5087 it->from_disp_prop_p = true;
5089 if (NILP (location))
5090 it->area = TEXT_AREA;
5091 else if (EQ (location, Qleft_margin))
5092 it->area = LEFT_MARGIN_AREA;
5093 else
5094 it->area = RIGHT_MARGIN_AREA;
5096 if (STRINGP (value))
5098 it->string = value;
5099 it->multibyte_p = STRING_MULTIBYTE (it->string);
5100 it->current.overlay_string_index = -1;
5101 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5102 it->end_charpos = it->string_nchars = SCHARS (it->string);
5103 it->method = GET_FROM_STRING;
5104 it->stop_charpos = 0;
5105 it->prev_stop = 0;
5106 it->base_level_stop = 0;
5107 it->string_from_display_prop_p = true;
5108 /* Say that we haven't consumed the characters with
5109 `display' property yet. The call to pop_it in
5110 set_iterator_to_next will clean this up. */
5111 if (BUFFERP (object))
5112 *position = start_pos;
5114 /* Force paragraph direction to be that of the parent
5115 object. If the parent object's paragraph direction is
5116 not yet determined, default to L2R. */
5117 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5118 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5119 else
5120 it->paragraph_embedding = L2R;
5122 /* Set up the bidi iterator for this display string. */
5123 if (it->bidi_p)
5125 it->bidi_it.string.lstring = it->string;
5126 it->bidi_it.string.s = NULL;
5127 it->bidi_it.string.schars = it->end_charpos;
5128 it->bidi_it.string.bufpos = bufpos;
5129 it->bidi_it.string.from_disp_str = true;
5130 it->bidi_it.string.unibyte = !it->multibyte_p;
5131 it->bidi_it.w = it->w;
5132 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5135 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5137 it->method = GET_FROM_STRETCH;
5138 it->object = value;
5139 *position = it->position = start_pos;
5140 retval = 1 + (it->area == TEXT_AREA);
5142 #ifdef HAVE_WINDOW_SYSTEM
5143 else
5145 it->what = IT_IMAGE;
5146 it->image_id = lookup_image (it->f, value);
5147 it->position = start_pos;
5148 it->object = NILP (object) ? it->w->contents : object;
5149 it->method = GET_FROM_IMAGE;
5151 /* Say that we haven't consumed the characters with
5152 `display' property yet. The call to pop_it in
5153 set_iterator_to_next will clean this up. */
5154 *position = start_pos;
5156 #endif /* HAVE_WINDOW_SYSTEM */
5158 return retval;
5161 /* Invalid property or property not supported. Restore
5162 POSITION to what it was before. */
5163 *position = start_pos;
5164 return 0;
5167 /* Check if PROP is a display property value whose text should be
5168 treated as intangible. OVERLAY is the overlay from which PROP
5169 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5170 specify the buffer position covered by PROP. */
5172 bool
5173 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5174 ptrdiff_t charpos, ptrdiff_t bytepos)
5176 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5177 struct text_pos position;
5179 SET_TEXT_POS (position, charpos, bytepos);
5180 return (handle_display_spec (NULL, prop, Qnil, overlay,
5181 &position, charpos, frame_window_p)
5182 != 0);
5186 /* Return true if PROP is a display sub-property value containing STRING.
5188 Implementation note: this and the following function are really
5189 special cases of handle_display_spec and
5190 handle_single_display_spec, and should ideally use the same code.
5191 Until they do, these two pairs must be consistent and must be
5192 modified in sync. */
5194 static bool
5195 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5197 if (EQ (string, prop))
5198 return true;
5200 /* Skip over `when FORM'. */
5201 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return false;
5206 /* Actually, the condition following `when' should be eval'ed,
5207 like handle_single_display_spec does, and we should return
5208 false if it evaluates to nil. However, this function is
5209 called only when the buffer was already displayed and some
5210 glyph in the glyph matrix was found to come from a display
5211 string. Therefore, the condition was already evaluated, and
5212 the result was non-nil, otherwise the display string wouldn't
5213 have been displayed and we would have never been called for
5214 this property. Thus, we can skip the evaluation and assume
5215 its result is non-nil. */
5216 prop = XCDR (prop);
5219 if (CONSP (prop))
5220 /* Skip over `margin LOCATION'. */
5221 if (EQ (XCAR (prop), Qmargin))
5223 prop = XCDR (prop);
5224 if (!CONSP (prop))
5225 return false;
5227 prop = XCDR (prop);
5228 if (!CONSP (prop))
5229 return false;
5232 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5236 /* Return true if STRING appears in the `display' property PROP. */
5238 static bool
5239 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5241 if (CONSP (prop)
5242 && !EQ (XCAR (prop), Qwhen)
5243 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5245 /* A list of sub-properties. */
5246 while (CONSP (prop))
5248 if (single_display_spec_string_p (XCAR (prop), string))
5249 return true;
5250 prop = XCDR (prop);
5253 else if (VECTORP (prop))
5255 /* A vector of sub-properties. */
5256 ptrdiff_t i;
5257 for (i = 0; i < ASIZE (prop); ++i)
5258 if (single_display_spec_string_p (AREF (prop, i), string))
5259 return true;
5261 else
5262 return single_display_spec_string_p (prop, string);
5264 return false;
5267 /* Look for STRING in overlays and text properties in the current
5268 buffer, between character positions FROM and TO (excluding TO).
5269 BACK_P means look back (in this case, TO is supposed to be
5270 less than FROM).
5271 Value is the first character position where STRING was found, or
5272 zero if it wasn't found before hitting TO.
5274 This function may only use code that doesn't eval because it is
5275 called asynchronously from note_mouse_highlight. */
5277 static ptrdiff_t
5278 string_buffer_position_lim (Lisp_Object string,
5279 ptrdiff_t from, ptrdiff_t to, bool back_p)
5281 Lisp_Object limit, prop, pos;
5282 bool found = false;
5284 pos = make_number (max (from, BEGV));
5286 if (!back_p) /* looking forward */
5288 limit = make_number (min (to, ZV));
5289 while (!found && !EQ (pos, limit))
5291 prop = Fget_char_property (pos, Qdisplay, Qnil);
5292 if (!NILP (prop) && display_prop_string_p (prop, string))
5293 found = true;
5294 else
5295 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5296 limit);
5299 else /* looking back */
5301 limit = make_number (max (to, BEGV));
5302 while (!found && !EQ (pos, limit))
5304 prop = Fget_char_property (pos, Qdisplay, Qnil);
5305 if (!NILP (prop) && display_prop_string_p (prop, string))
5306 found = true;
5307 else
5308 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5309 limit);
5313 return found ? XINT (pos) : 0;
5316 /* Determine which buffer position in current buffer STRING comes from.
5317 AROUND_CHARPOS is an approximate position where it could come from.
5318 Value is the buffer position or 0 if it couldn't be determined.
5320 This function is necessary because we don't record buffer positions
5321 in glyphs generated from strings (to keep struct glyph small).
5322 This function may only use code that doesn't eval because it is
5323 called asynchronously from note_mouse_highlight. */
5325 static ptrdiff_t
5326 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5328 const int MAX_DISTANCE = 1000;
5329 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5330 around_charpos + MAX_DISTANCE,
5331 false);
5333 if (!found)
5334 found = string_buffer_position_lim (string, around_charpos,
5335 around_charpos - MAX_DISTANCE, true);
5336 return found;
5341 /***********************************************************************
5342 `composition' property
5343 ***********************************************************************/
5345 /* Set up iterator IT from `composition' property at its current
5346 position. Called from handle_stop. */
5348 static enum prop_handled
5349 handle_composition_prop (struct it *it)
5351 Lisp_Object prop, string;
5352 ptrdiff_t pos, pos_byte, start, end;
5354 if (STRINGP (it->string))
5356 unsigned char *s;
5358 pos = IT_STRING_CHARPOS (*it);
5359 pos_byte = IT_STRING_BYTEPOS (*it);
5360 string = it->string;
5361 s = SDATA (string) + pos_byte;
5362 it->c = STRING_CHAR (s);
5364 else
5366 pos = IT_CHARPOS (*it);
5367 pos_byte = IT_BYTEPOS (*it);
5368 string = Qnil;
5369 it->c = FETCH_CHAR (pos_byte);
5372 /* If there's a valid composition and point is not inside of the
5373 composition (in the case that the composition is from the current
5374 buffer), draw a glyph composed from the composition components. */
5375 if (find_composition (pos, -1, &start, &end, &prop, string)
5376 && composition_valid_p (start, end, prop)
5377 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5379 if (start < pos)
5380 /* As we can't handle this situation (perhaps font-lock added
5381 a new composition), we just return here hoping that next
5382 redisplay will detect this composition much earlier. */
5383 return HANDLED_NORMALLY;
5384 if (start != pos)
5386 if (STRINGP (it->string))
5387 pos_byte = string_char_to_byte (it->string, start);
5388 else
5389 pos_byte = CHAR_TO_BYTE (start);
5391 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5392 prop, string);
5394 if (it->cmp_it.id >= 0)
5396 it->cmp_it.ch = -1;
5397 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5398 it->cmp_it.nglyphs = -1;
5402 return HANDLED_NORMALLY;
5407 /***********************************************************************
5408 Overlay strings
5409 ***********************************************************************/
5411 /* The following structure is used to record overlay strings for
5412 later sorting in load_overlay_strings. */
5414 struct overlay_entry
5416 Lisp_Object overlay;
5417 Lisp_Object string;
5418 EMACS_INT priority;
5419 bool after_string_p;
5423 /* Set up iterator IT from overlay strings at its current position.
5424 Called from handle_stop. */
5426 static enum prop_handled
5427 handle_overlay_change (struct it *it)
5429 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5430 return HANDLED_RECOMPUTE_PROPS;
5431 else
5432 return HANDLED_NORMALLY;
5436 /* Set up the next overlay string for delivery by IT, if there is an
5437 overlay string to deliver. Called by set_iterator_to_next when the
5438 end of the current overlay string is reached. If there are more
5439 overlay strings to display, IT->string and
5440 IT->current.overlay_string_index are set appropriately here.
5441 Otherwise IT->string is set to nil. */
5443 static void
5444 next_overlay_string (struct it *it)
5446 ++it->current.overlay_string_index;
5447 if (it->current.overlay_string_index == it->n_overlay_strings)
5449 /* No more overlay strings. Restore IT's settings to what
5450 they were before overlay strings were processed, and
5451 continue to deliver from current_buffer. */
5453 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5454 pop_it (it);
5455 eassert (it->sp > 0
5456 || (NILP (it->string)
5457 && it->method == GET_FROM_BUFFER
5458 && it->stop_charpos >= BEGV
5459 && it->stop_charpos <= it->end_charpos));
5460 it->current.overlay_string_index = -1;
5461 it->n_overlay_strings = 0;
5462 /* If there's an empty display string on the stack, pop the
5463 stack, to resync the bidi iterator with IT's position. Such
5464 empty strings are pushed onto the stack in
5465 get_overlay_strings_1. */
5466 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5467 pop_it (it);
5469 /* Since we've exhausted overlay strings at this buffer
5470 position, set the flag to ignore overlays until we move to
5471 another position. The flag is reset in
5472 next_element_from_buffer. */
5473 it->ignore_overlay_strings_at_pos_p = true;
5475 /* If we're at the end of the buffer, record that we have
5476 processed the overlay strings there already, so that
5477 next_element_from_buffer doesn't try it again. */
5478 if (NILP (it->string)
5479 && IT_CHARPOS (*it) >= it->end_charpos
5480 && it->overlay_strings_charpos >= it->end_charpos)
5481 it->overlay_strings_at_end_processed_p = true;
5482 /* Note: we reset overlay_strings_charpos only here, to make
5483 sure the just-processed overlays were indeed at EOB.
5484 Otherwise, overlays on text with invisible text property,
5485 which are processed with IT's position past the invisible
5486 text, might fool us into thinking the overlays at EOB were
5487 already processed (linum-mode can cause this, for
5488 example). */
5489 it->overlay_strings_charpos = -1;
5491 else
5493 /* There are more overlay strings to process. If
5494 IT->current.overlay_string_index has advanced to a position
5495 where we must load IT->overlay_strings with more strings, do
5496 it. We must load at the IT->overlay_strings_charpos where
5497 IT->n_overlay_strings was originally computed; when invisible
5498 text is present, this might not be IT_CHARPOS (Bug#7016). */
5499 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5501 if (it->current.overlay_string_index && i == 0)
5502 load_overlay_strings (it, it->overlay_strings_charpos);
5504 /* Initialize IT to deliver display elements from the overlay
5505 string. */
5506 it->string = it->overlay_strings[i];
5507 it->multibyte_p = STRING_MULTIBYTE (it->string);
5508 SET_TEXT_POS (it->current.string_pos, 0, 0);
5509 it->method = GET_FROM_STRING;
5510 it->stop_charpos = 0;
5511 it->end_charpos = SCHARS (it->string);
5512 if (it->cmp_it.stop_pos >= 0)
5513 it->cmp_it.stop_pos = 0;
5514 it->prev_stop = 0;
5515 it->base_level_stop = 0;
5517 /* Set up the bidi iterator for this overlay string. */
5518 if (it->bidi_p)
5520 it->bidi_it.string.lstring = it->string;
5521 it->bidi_it.string.s = NULL;
5522 it->bidi_it.string.schars = SCHARS (it->string);
5523 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5524 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5525 it->bidi_it.string.unibyte = !it->multibyte_p;
5526 it->bidi_it.w = it->w;
5527 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5531 CHECK_IT (it);
5535 /* Compare two overlay_entry structures E1 and E2. Used as a
5536 comparison function for qsort in load_overlay_strings. Overlay
5537 strings for the same position are sorted so that
5539 1. All after-strings come in front of before-strings, except
5540 when they come from the same overlay.
5542 2. Within after-strings, strings are sorted so that overlay strings
5543 from overlays with higher priorities come first.
5545 2. Within before-strings, strings are sorted so that overlay
5546 strings from overlays with higher priorities come last.
5548 Value is analogous to strcmp. */
5551 static int
5552 compare_overlay_entries (const void *e1, const void *e2)
5554 struct overlay_entry const *entry1 = e1;
5555 struct overlay_entry const *entry2 = e2;
5556 int result;
5558 if (entry1->after_string_p != entry2->after_string_p)
5560 /* Let after-strings appear in front of before-strings if
5561 they come from different overlays. */
5562 if (EQ (entry1->overlay, entry2->overlay))
5563 result = entry1->after_string_p ? 1 : -1;
5564 else
5565 result = entry1->after_string_p ? -1 : 1;
5567 else if (entry1->priority != entry2->priority)
5569 if (entry1->after_string_p)
5570 /* After-strings sorted in order of decreasing priority. */
5571 result = entry2->priority < entry1->priority ? -1 : 1;
5572 else
5573 /* Before-strings sorted in order of increasing priority. */
5574 result = entry1->priority < entry2->priority ? -1 : 1;
5576 else
5577 result = 0;
5579 return result;
5583 /* Load the vector IT->overlay_strings with overlay strings from IT's
5584 current buffer position, or from CHARPOS if that is > 0. Set
5585 IT->n_overlays to the total number of overlay strings found.
5587 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5588 a time. On entry into load_overlay_strings,
5589 IT->current.overlay_string_index gives the number of overlay
5590 strings that have already been loaded by previous calls to this
5591 function.
5593 IT->add_overlay_start contains an additional overlay start
5594 position to consider for taking overlay strings from, if non-zero.
5595 This position comes into play when the overlay has an `invisible'
5596 property, and both before and after-strings. When we've skipped to
5597 the end of the overlay, because of its `invisible' property, we
5598 nevertheless want its before-string to appear.
5599 IT->add_overlay_start will contain the overlay start position
5600 in this case.
5602 Overlay strings are sorted so that after-string strings come in
5603 front of before-string strings. Within before and after-strings,
5604 strings are sorted by overlay priority. See also function
5605 compare_overlay_entries. */
5607 static void
5608 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5610 Lisp_Object overlay, window, str, invisible;
5611 struct Lisp_Overlay *ov;
5612 ptrdiff_t start, end;
5613 ptrdiff_t n = 0, i, j;
5614 int invis;
5615 struct overlay_entry entriesbuf[20];
5616 ptrdiff_t size = ARRAYELTS (entriesbuf);
5617 struct overlay_entry *entries = entriesbuf;
5618 USE_SAFE_ALLOCA;
5620 if (charpos <= 0)
5621 charpos = IT_CHARPOS (*it);
5623 /* Append the overlay string STRING of overlay OVERLAY to vector
5624 `entries' which has size `size' and currently contains `n'
5625 elements. AFTER_P means STRING is an after-string of
5626 OVERLAY. */
5627 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5628 do \
5630 Lisp_Object priority; \
5632 if (n == size) \
5634 struct overlay_entry *old = entries; \
5635 SAFE_NALLOCA (entries, 2, size); \
5636 memcpy (entries, old, size * sizeof *entries); \
5637 size *= 2; \
5640 entries[n].string = (STRING); \
5641 entries[n].overlay = (OVERLAY); \
5642 priority = Foverlay_get ((OVERLAY), Qpriority); \
5643 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5644 entries[n].after_string_p = (AFTER_P); \
5645 ++n; \
5647 while (false)
5649 /* Process overlay before the overlay center. */
5650 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5652 XSETMISC (overlay, ov);
5653 eassert (OVERLAYP (overlay));
5654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5657 if (end < charpos)
5658 break;
5660 /* Skip this overlay if it doesn't start or end at IT's current
5661 position. */
5662 if (end != charpos && start != charpos)
5663 continue;
5665 /* Skip this overlay if it doesn't apply to IT->w. */
5666 window = Foverlay_get (overlay, Qwindow);
5667 if (WINDOWP (window) && XWINDOW (window) != it->w)
5668 continue;
5670 /* If the text ``under'' the overlay is invisible, both before-
5671 and after-strings from this overlay are visible; start and
5672 end position are indistinguishable. */
5673 invisible = Foverlay_get (overlay, Qinvisible);
5674 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5676 /* If overlay has a non-empty before-string, record it. */
5677 if ((start == charpos || (end == charpos && invis != 0))
5678 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5679 && SCHARS (str))
5680 RECORD_OVERLAY_STRING (overlay, str, false);
5682 /* If overlay has a non-empty after-string, record it. */
5683 if ((end == charpos || (start == charpos && invis != 0))
5684 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5685 && SCHARS (str))
5686 RECORD_OVERLAY_STRING (overlay, str, true);
5689 /* Process overlays after the overlay center. */
5690 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5692 XSETMISC (overlay, ov);
5693 eassert (OVERLAYP (overlay));
5694 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5695 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5697 if (start > charpos)
5698 break;
5700 /* Skip this overlay if it doesn't start or end at IT's current
5701 position. */
5702 if (end != charpos && start != charpos)
5703 continue;
5705 /* Skip this overlay if it doesn't apply to IT->w. */
5706 window = Foverlay_get (overlay, Qwindow);
5707 if (WINDOWP (window) && XWINDOW (window) != it->w)
5708 continue;
5710 /* If the text ``under'' the overlay is invisible, it has a zero
5711 dimension, and both before- and after-strings apply. */
5712 invisible = Foverlay_get (overlay, Qinvisible);
5713 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5715 /* If overlay has a non-empty before-string, record it. */
5716 if ((start == charpos || (end == charpos && invis != 0))
5717 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5718 && SCHARS (str))
5719 RECORD_OVERLAY_STRING (overlay, str, false);
5721 /* If overlay has a non-empty after-string, record it. */
5722 if ((end == charpos || (start == charpos && invis != 0))
5723 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5724 && SCHARS (str))
5725 RECORD_OVERLAY_STRING (overlay, str, true);
5728 #undef RECORD_OVERLAY_STRING
5730 /* Sort entries. */
5731 if (n > 1)
5732 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5734 /* Record number of overlay strings, and where we computed it. */
5735 it->n_overlay_strings = n;
5736 it->overlay_strings_charpos = charpos;
5738 /* IT->current.overlay_string_index is the number of overlay strings
5739 that have already been consumed by IT. Copy some of the
5740 remaining overlay strings to IT->overlay_strings. */
5741 i = 0;
5742 j = it->current.overlay_string_index;
5743 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5745 it->overlay_strings[i] = entries[j].string;
5746 it->string_overlays[i++] = entries[j++].overlay;
5749 CHECK_IT (it);
5750 SAFE_FREE ();
5754 /* Get the first chunk of overlay strings at IT's current buffer
5755 position, or at CHARPOS if that is > 0. Value is true if at
5756 least one overlay string was found. */
5758 static bool
5759 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5761 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5762 process. This fills IT->overlay_strings with strings, and sets
5763 IT->n_overlay_strings to the total number of strings to process.
5764 IT->pos.overlay_string_index has to be set temporarily to zero
5765 because load_overlay_strings needs this; it must be set to -1
5766 when no overlay strings are found because a zero value would
5767 indicate a position in the first overlay string. */
5768 it->current.overlay_string_index = 0;
5769 load_overlay_strings (it, charpos);
5771 /* If we found overlay strings, set up IT to deliver display
5772 elements from the first one. Otherwise set up IT to deliver
5773 from current_buffer. */
5774 if (it->n_overlay_strings)
5776 /* Make sure we know settings in current_buffer, so that we can
5777 restore meaningful values when we're done with the overlay
5778 strings. */
5779 if (compute_stop_p)
5780 compute_stop_pos (it);
5781 eassert (it->face_id >= 0);
5783 /* Save IT's settings. They are restored after all overlay
5784 strings have been processed. */
5785 eassert (!compute_stop_p || it->sp == 0);
5787 /* When called from handle_stop, there might be an empty display
5788 string loaded. In that case, don't bother saving it. But
5789 don't use this optimization with the bidi iterator, since we
5790 need the corresponding pop_it call to resync the bidi
5791 iterator's position with IT's position, after we are done
5792 with the overlay strings. (The corresponding call to pop_it
5793 in case of an empty display string is in
5794 next_overlay_string.) */
5795 if (!(!it->bidi_p
5796 && STRINGP (it->string) && !SCHARS (it->string)))
5797 push_it (it, NULL);
5799 /* Set up IT to deliver display elements from the first overlay
5800 string. */
5801 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5802 it->string = it->overlay_strings[0];
5803 it->from_overlay = Qnil;
5804 it->stop_charpos = 0;
5805 eassert (STRINGP (it->string));
5806 it->end_charpos = SCHARS (it->string);
5807 it->prev_stop = 0;
5808 it->base_level_stop = 0;
5809 it->multibyte_p = STRING_MULTIBYTE (it->string);
5810 it->method = GET_FROM_STRING;
5811 it->from_disp_prop_p = 0;
5813 /* Force paragraph direction to be that of the parent
5814 buffer. */
5815 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5816 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5817 else
5818 it->paragraph_embedding = L2R;
5820 /* Set up the bidi iterator for this overlay string. */
5821 if (it->bidi_p)
5823 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5825 it->bidi_it.string.lstring = it->string;
5826 it->bidi_it.string.s = NULL;
5827 it->bidi_it.string.schars = SCHARS (it->string);
5828 it->bidi_it.string.bufpos = pos;
5829 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5830 it->bidi_it.string.unibyte = !it->multibyte_p;
5831 it->bidi_it.w = it->w;
5832 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5834 return true;
5837 it->current.overlay_string_index = -1;
5838 return false;
5841 static bool
5842 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5844 it->string = Qnil;
5845 it->method = GET_FROM_BUFFER;
5847 get_overlay_strings_1 (it, charpos, true);
5849 CHECK_IT (it);
5851 /* Value is true if we found at least one overlay string. */
5852 return STRINGP (it->string);
5857 /***********************************************************************
5858 Saving and restoring state
5859 ***********************************************************************/
5861 /* Save current settings of IT on IT->stack. Called, for example,
5862 before setting up IT for an overlay string, to be able to restore
5863 IT's settings to what they were after the overlay string has been
5864 processed. If POSITION is non-NULL, it is the position to save on
5865 the stack instead of IT->position. */
5867 static void
5868 push_it (struct it *it, struct text_pos *position)
5870 struct iterator_stack_entry *p;
5872 eassert (it->sp < IT_STACK_SIZE);
5873 p = it->stack + it->sp;
5875 p->stop_charpos = it->stop_charpos;
5876 p->prev_stop = it->prev_stop;
5877 p->base_level_stop = it->base_level_stop;
5878 p->cmp_it = it->cmp_it;
5879 eassert (it->face_id >= 0);
5880 p->face_id = it->face_id;
5881 p->string = it->string;
5882 p->method = it->method;
5883 p->from_overlay = it->from_overlay;
5884 switch (p->method)
5886 case GET_FROM_IMAGE:
5887 p->u.image.object = it->object;
5888 p->u.image.image_id = it->image_id;
5889 p->u.image.slice = it->slice;
5890 break;
5891 case GET_FROM_STRETCH:
5892 p->u.stretch.object = it->object;
5893 break;
5895 p->position = position ? *position : it->position;
5896 p->current = it->current;
5897 p->end_charpos = it->end_charpos;
5898 p->string_nchars = it->string_nchars;
5899 p->area = it->area;
5900 p->multibyte_p = it->multibyte_p;
5901 p->avoid_cursor_p = it->avoid_cursor_p;
5902 p->space_width = it->space_width;
5903 p->font_height = it->font_height;
5904 p->voffset = it->voffset;
5905 p->string_from_display_prop_p = it->string_from_display_prop_p;
5906 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5907 p->display_ellipsis_p = false;
5908 p->line_wrap = it->line_wrap;
5909 p->bidi_p = it->bidi_p;
5910 p->paragraph_embedding = it->paragraph_embedding;
5911 p->from_disp_prop_p = it->from_disp_prop_p;
5912 ++it->sp;
5914 /* Save the state of the bidi iterator as well. */
5915 if (it->bidi_p)
5916 bidi_push_it (&it->bidi_it);
5919 static void
5920 iterate_out_of_display_property (struct it *it)
5922 bool buffer_p = !STRINGP (it->string);
5923 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5924 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5926 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5928 /* Maybe initialize paragraph direction. If we are at the beginning
5929 of a new paragraph, next_element_from_buffer may not have a
5930 chance to do that. */
5931 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5932 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5933 /* prev_stop can be zero, so check against BEGV as well. */
5934 while (it->bidi_it.charpos >= bob
5935 && it->prev_stop <= it->bidi_it.charpos
5936 && it->bidi_it.charpos < CHARPOS (it->position)
5937 && it->bidi_it.charpos < eob)
5938 bidi_move_to_visually_next (&it->bidi_it);
5939 /* Record the stop_pos we just crossed, for when we cross it
5940 back, maybe. */
5941 if (it->bidi_it.charpos > CHARPOS (it->position))
5942 it->prev_stop = CHARPOS (it->position);
5943 /* If we ended up not where pop_it put us, resync IT's
5944 positional members with the bidi iterator. */
5945 if (it->bidi_it.charpos != CHARPOS (it->position))
5946 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5947 if (buffer_p)
5948 it->current.pos = it->position;
5949 else
5950 it->current.string_pos = it->position;
5953 /* Restore IT's settings from IT->stack. Called, for example, when no
5954 more overlay strings must be processed, and we return to delivering
5955 display elements from a buffer, or when the end of a string from a
5956 `display' property is reached and we return to delivering display
5957 elements from an overlay string, or from a buffer. */
5959 static void
5960 pop_it (struct it *it)
5962 struct iterator_stack_entry *p;
5963 bool from_display_prop = it->from_disp_prop_p;
5965 eassert (it->sp > 0);
5966 --it->sp;
5967 p = it->stack + it->sp;
5968 it->stop_charpos = p->stop_charpos;
5969 it->prev_stop = p->prev_stop;
5970 it->base_level_stop = p->base_level_stop;
5971 it->cmp_it = p->cmp_it;
5972 it->face_id = p->face_id;
5973 it->current = p->current;
5974 it->position = p->position;
5975 it->string = p->string;
5976 it->from_overlay = p->from_overlay;
5977 if (NILP (it->string))
5978 SET_TEXT_POS (it->current.string_pos, -1, -1);
5979 it->method = p->method;
5980 switch (it->method)
5982 case GET_FROM_IMAGE:
5983 it->image_id = p->u.image.image_id;
5984 it->object = p->u.image.object;
5985 it->slice = p->u.image.slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 it->object = p->u.stretch.object;
5989 break;
5990 case GET_FROM_BUFFER:
5991 it->object = it->w->contents;
5992 break;
5993 case GET_FROM_STRING:
5995 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5997 /* Restore the face_box_p flag, since it could have been
5998 overwritten by the face of the object that we just finished
5999 displaying. */
6000 if (face)
6001 it->face_box_p = face->box != FACE_NO_BOX;
6002 it->object = it->string;
6004 break;
6005 case GET_FROM_DISPLAY_VECTOR:
6006 if (it->s)
6007 it->method = GET_FROM_C_STRING;
6008 else if (STRINGP (it->string))
6009 it->method = GET_FROM_STRING;
6010 else
6012 it->method = GET_FROM_BUFFER;
6013 it->object = it->w->contents;
6016 it->end_charpos = p->end_charpos;
6017 it->string_nchars = p->string_nchars;
6018 it->area = p->area;
6019 it->multibyte_p = p->multibyte_p;
6020 it->avoid_cursor_p = p->avoid_cursor_p;
6021 it->space_width = p->space_width;
6022 it->font_height = p->font_height;
6023 it->voffset = p->voffset;
6024 it->string_from_display_prop_p = p->string_from_display_prop_p;
6025 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6026 it->line_wrap = p->line_wrap;
6027 it->bidi_p = p->bidi_p;
6028 it->paragraph_embedding = p->paragraph_embedding;
6029 it->from_disp_prop_p = p->from_disp_prop_p;
6030 if (it->bidi_p)
6032 bidi_pop_it (&it->bidi_it);
6033 /* Bidi-iterate until we get out of the portion of text, if any,
6034 covered by a `display' text property or by an overlay with
6035 `display' property. (We cannot just jump there, because the
6036 internal coherency of the bidi iterator state can not be
6037 preserved across such jumps.) We also must determine the
6038 paragraph base direction if the overlay we just processed is
6039 at the beginning of a new paragraph. */
6040 if (from_display_prop
6041 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6042 iterate_out_of_display_property (it);
6044 eassert ((BUFFERP (it->object)
6045 && IT_CHARPOS (*it) == it->bidi_it.charpos
6046 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6047 || (STRINGP (it->object)
6048 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6049 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6050 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6056 /***********************************************************************
6057 Moving over lines
6058 ***********************************************************************/
6060 /* Set IT's current position to the previous line start. */
6062 static void
6063 back_to_previous_line_start (struct it *it)
6065 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6067 DEC_BOTH (cp, bp);
6068 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6072 /* Move IT to the next line start.
6074 Value is true if a newline was found. Set *SKIPPED_P to true if
6075 we skipped over part of the text (as opposed to moving the iterator
6076 continuously over the text). Otherwise, don't change the value
6077 of *SKIPPED_P.
6079 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6080 iterator on the newline, if it was found.
6082 Newlines may come from buffer text, overlay strings, or strings
6083 displayed via the `display' property. That's the reason we can't
6084 simply use find_newline_no_quit.
6086 Note that this function may not skip over invisible text that is so
6087 because of text properties and immediately follows a newline. If
6088 it would, function reseat_at_next_visible_line_start, when called
6089 from set_iterator_to_next, would effectively make invisible
6090 characters following a newline part of the wrong glyph row, which
6091 leads to wrong cursor motion. */
6093 static bool
6094 forward_to_next_line_start (struct it *it, bool *skipped_p,
6095 struct bidi_it *bidi_it_prev)
6097 ptrdiff_t old_selective;
6098 bool newline_found_p = false;
6099 int n;
6100 const int MAX_NEWLINE_DISTANCE = 500;
6102 /* If already on a newline, just consume it to avoid unintended
6103 skipping over invisible text below. */
6104 if (it->what == IT_CHARACTER
6105 && it->c == '\n'
6106 && CHARPOS (it->position) == IT_CHARPOS (*it))
6108 if (it->bidi_p && bidi_it_prev)
6109 *bidi_it_prev = it->bidi_it;
6110 set_iterator_to_next (it, false);
6111 it->c = 0;
6112 return true;
6115 /* Don't handle selective display in the following. It's (a)
6116 unnecessary because it's done by the caller, and (b) leads to an
6117 infinite recursion because next_element_from_ellipsis indirectly
6118 calls this function. */
6119 old_selective = it->selective;
6120 it->selective = 0;
6122 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6123 from buffer text. */
6124 for (n = 0;
6125 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6126 n += !STRINGP (it->string))
6128 if (!get_next_display_element (it))
6129 return false;
6130 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6131 if (newline_found_p && it->bidi_p && bidi_it_prev)
6132 *bidi_it_prev = it->bidi_it;
6133 set_iterator_to_next (it, false);
6136 /* If we didn't find a newline near enough, see if we can use a
6137 short-cut. */
6138 if (!newline_found_p)
6140 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6141 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6142 1, &bytepos);
6143 Lisp_Object pos;
6145 eassert (!STRINGP (it->string));
6147 /* If there isn't any `display' property in sight, and no
6148 overlays, we can just use the position of the newline in
6149 buffer text. */
6150 if (it->stop_charpos >= limit
6151 || ((pos = Fnext_single_property_change (make_number (start),
6152 Qdisplay, Qnil,
6153 make_number (limit)),
6154 NILP (pos))
6155 && next_overlay_change (start) == ZV))
6157 if (!it->bidi_p)
6159 IT_CHARPOS (*it) = limit;
6160 IT_BYTEPOS (*it) = bytepos;
6162 else
6164 struct bidi_it bprev;
6166 /* Help bidi.c avoid expensive searches for display
6167 properties and overlays, by telling it that there are
6168 none up to `limit'. */
6169 if (it->bidi_it.disp_pos < limit)
6171 it->bidi_it.disp_pos = limit;
6172 it->bidi_it.disp_prop = 0;
6174 do {
6175 bprev = it->bidi_it;
6176 bidi_move_to_visually_next (&it->bidi_it);
6177 } while (it->bidi_it.charpos != limit);
6178 IT_CHARPOS (*it) = limit;
6179 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6180 if (bidi_it_prev)
6181 *bidi_it_prev = bprev;
6183 *skipped_p = newline_found_p = true;
6185 else
6187 while (get_next_display_element (it)
6188 && !newline_found_p)
6190 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6191 if (newline_found_p && it->bidi_p && bidi_it_prev)
6192 *bidi_it_prev = it->bidi_it;
6193 set_iterator_to_next (it, false);
6198 it->selective = old_selective;
6199 return newline_found_p;
6203 /* Set IT's current position to the previous visible line start. Skip
6204 invisible text that is so either due to text properties or due to
6205 selective display. Caution: this does not change IT->current_x and
6206 IT->hpos. */
6208 static void
6209 back_to_previous_visible_line_start (struct it *it)
6211 while (IT_CHARPOS (*it) > BEGV)
6213 back_to_previous_line_start (it);
6215 if (IT_CHARPOS (*it) <= BEGV)
6216 break;
6218 /* If selective > 0, then lines indented more than its value are
6219 invisible. */
6220 if (it->selective > 0
6221 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6222 it->selective))
6223 continue;
6225 /* Check the newline before point for invisibility. */
6227 Lisp_Object prop;
6228 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6229 Qinvisible, it->window);
6230 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6231 continue;
6234 if (IT_CHARPOS (*it) <= BEGV)
6235 break;
6238 struct it it2;
6239 void *it2data = NULL;
6240 ptrdiff_t pos;
6241 ptrdiff_t beg, end;
6242 Lisp_Object val, overlay;
6244 SAVE_IT (it2, *it, it2data);
6246 /* If newline is part of a composition, continue from start of composition */
6247 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6248 && beg < IT_CHARPOS (*it))
6249 goto replaced;
6251 /* If newline is replaced by a display property, find start of overlay
6252 or interval and continue search from that point. */
6253 pos = --IT_CHARPOS (it2);
6254 --IT_BYTEPOS (it2);
6255 it2.sp = 0;
6256 bidi_unshelve_cache (NULL, false);
6257 it2.string_from_display_prop_p = false;
6258 it2.from_disp_prop_p = false;
6259 if (handle_display_prop (&it2) == HANDLED_RETURN
6260 && !NILP (val = get_char_property_and_overlay
6261 (make_number (pos), Qdisplay, Qnil, &overlay))
6262 && (OVERLAYP (overlay)
6263 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6264 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6266 RESTORE_IT (it, it, it2data);
6267 goto replaced;
6270 /* Newline is not replaced by anything -- so we are done. */
6271 RESTORE_IT (it, it, it2data);
6272 break;
6274 replaced:
6275 if (beg < BEGV)
6276 beg = BEGV;
6277 IT_CHARPOS (*it) = beg;
6278 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6282 it->continuation_lines_width = 0;
6284 eassert (IT_CHARPOS (*it) >= BEGV);
6285 eassert (IT_CHARPOS (*it) == BEGV
6286 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6287 CHECK_IT (it);
6291 /* Reseat iterator IT at the previous visible line start. Skip
6292 invisible text that is so either due to text properties or due to
6293 selective display. At the end, update IT's overlay information,
6294 face information etc. */
6296 void
6297 reseat_at_previous_visible_line_start (struct it *it)
6299 back_to_previous_visible_line_start (it);
6300 reseat (it, it->current.pos, true);
6301 CHECK_IT (it);
6305 /* Reseat iterator IT on the next visible line start in the current
6306 buffer. ON_NEWLINE_P means position IT on the newline
6307 preceding the line start. Skip over invisible text that is so
6308 because of selective display. Compute faces, overlays etc at the
6309 new position. Note that this function does not skip over text that
6310 is invisible because of text properties. */
6312 static void
6313 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6315 bool skipped_p = false;
6316 struct bidi_it bidi_it_prev;
6317 bool newline_found_p
6318 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6320 /* Skip over lines that are invisible because they are indented
6321 more than the value of IT->selective. */
6322 if (it->selective > 0)
6323 while (IT_CHARPOS (*it) < ZV
6324 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6325 it->selective))
6327 eassert (IT_BYTEPOS (*it) == BEGV
6328 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6329 newline_found_p =
6330 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6333 /* Position on the newline if that's what's requested. */
6334 if (on_newline_p && newline_found_p)
6336 if (STRINGP (it->string))
6338 if (IT_STRING_CHARPOS (*it) > 0)
6340 if (!it->bidi_p)
6342 --IT_STRING_CHARPOS (*it);
6343 --IT_STRING_BYTEPOS (*it);
6345 else
6347 /* We need to restore the bidi iterator to the state
6348 it had on the newline, and resync the IT's
6349 position with that. */
6350 it->bidi_it = bidi_it_prev;
6351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6352 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6356 else if (IT_CHARPOS (*it) > BEGV)
6358 if (!it->bidi_p)
6360 --IT_CHARPOS (*it);
6361 --IT_BYTEPOS (*it);
6363 else
6365 /* We need to restore the bidi iterator to the state it
6366 had on the newline and resync IT with that. */
6367 it->bidi_it = bidi_it_prev;
6368 IT_CHARPOS (*it) = it->bidi_it.charpos;
6369 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6371 reseat (it, it->current.pos, false);
6374 else if (skipped_p)
6375 reseat (it, it->current.pos, false);
6377 CHECK_IT (it);
6382 /***********************************************************************
6383 Changing an iterator's position
6384 ***********************************************************************/
6386 /* Change IT's current position to POS in current_buffer.
6387 If FORCE_P, always check for text properties at the new position.
6388 Otherwise, text properties are only looked up if POS >=
6389 IT->check_charpos of a property. */
6391 static void
6392 reseat (struct it *it, struct text_pos pos, bool force_p)
6394 ptrdiff_t original_pos = IT_CHARPOS (*it);
6396 reseat_1 (it, pos, false);
6398 /* Determine where to check text properties. Avoid doing it
6399 where possible because text property lookup is very expensive. */
6400 if (force_p
6401 || CHARPOS (pos) > it->stop_charpos
6402 || CHARPOS (pos) < original_pos)
6404 if (it->bidi_p)
6406 /* For bidi iteration, we need to prime prev_stop and
6407 base_level_stop with our best estimations. */
6408 /* Implementation note: Of course, POS is not necessarily a
6409 stop position, so assigning prev_pos to it is a lie; we
6410 should have called compute_stop_backwards. However, if
6411 the current buffer does not include any R2L characters,
6412 that call would be a waste of cycles, because the
6413 iterator will never move back, and thus never cross this
6414 "fake" stop position. So we delay that backward search
6415 until the time we really need it, in next_element_from_buffer. */
6416 if (CHARPOS (pos) != it->prev_stop)
6417 it->prev_stop = CHARPOS (pos);
6418 if (CHARPOS (pos) < it->base_level_stop)
6419 it->base_level_stop = 0; /* meaning it's unknown */
6420 handle_stop (it);
6422 else
6424 handle_stop (it);
6425 it->prev_stop = it->base_level_stop = 0;
6430 CHECK_IT (it);
6434 /* Change IT's buffer position to POS. SET_STOP_P means set
6435 IT->stop_pos to POS, also. */
6437 static void
6438 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6440 /* Don't call this function when scanning a C string. */
6441 eassert (it->s == NULL);
6443 /* POS must be a reasonable value. */
6444 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6446 it->current.pos = it->position = pos;
6447 it->end_charpos = ZV;
6448 it->dpvec = NULL;
6449 it->current.dpvec_index = -1;
6450 it->current.overlay_string_index = -1;
6451 IT_STRING_CHARPOS (*it) = -1;
6452 IT_STRING_BYTEPOS (*it) = -1;
6453 it->string = Qnil;
6454 it->method = GET_FROM_BUFFER;
6455 it->object = it->w->contents;
6456 it->area = TEXT_AREA;
6457 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6458 it->sp = 0;
6459 it->string_from_display_prop_p = false;
6460 it->string_from_prefix_prop_p = false;
6462 it->from_disp_prop_p = false;
6463 it->face_before_selective_p = false;
6464 if (it->bidi_p)
6466 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6467 &it->bidi_it);
6468 bidi_unshelve_cache (NULL, false);
6469 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6470 it->bidi_it.string.s = NULL;
6471 it->bidi_it.string.lstring = Qnil;
6472 it->bidi_it.string.bufpos = 0;
6473 it->bidi_it.string.from_disp_str = false;
6474 it->bidi_it.string.unibyte = false;
6475 it->bidi_it.w = it->w;
6478 if (set_stop_p)
6480 it->stop_charpos = CHARPOS (pos);
6481 it->base_level_stop = CHARPOS (pos);
6483 /* This make the information stored in it->cmp_it invalidate. */
6484 it->cmp_it.id = -1;
6488 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6489 If S is non-null, it is a C string to iterate over. Otherwise,
6490 STRING gives a Lisp string to iterate over.
6492 If PRECISION > 0, don't return more then PRECISION number of
6493 characters from the string.
6495 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6496 characters have been returned. FIELD_WIDTH < 0 means an infinite
6497 field width.
6499 MULTIBYTE = 0 means disable processing of multibyte characters,
6500 MULTIBYTE > 0 means enable it,
6501 MULTIBYTE < 0 means use IT->multibyte_p.
6503 IT must be initialized via a prior call to init_iterator before
6504 calling this function. */
6506 static void
6507 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6508 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6509 int multibyte)
6511 /* No text property checks performed by default, but see below. */
6512 it->stop_charpos = -1;
6514 /* Set iterator position and end position. */
6515 memset (&it->current, 0, sizeof it->current);
6516 it->current.overlay_string_index = -1;
6517 it->current.dpvec_index = -1;
6518 eassert (charpos >= 0);
6520 /* If STRING is specified, use its multibyteness, otherwise use the
6521 setting of MULTIBYTE, if specified. */
6522 if (multibyte >= 0)
6523 it->multibyte_p = multibyte > 0;
6525 /* Bidirectional reordering of strings is controlled by the default
6526 value of bidi-display-reordering. Don't try to reorder while
6527 loading loadup.el, as the necessary character property tables are
6528 not yet available. */
6529 it->bidi_p =
6530 NILP (Vpurify_flag)
6531 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6533 if (s == NULL)
6535 eassert (STRINGP (string));
6536 it->string = string;
6537 it->s = NULL;
6538 it->end_charpos = it->string_nchars = SCHARS (string);
6539 it->method = GET_FROM_STRING;
6540 it->current.string_pos = string_pos (charpos, string);
6542 if (it->bidi_p)
6544 it->bidi_it.string.lstring = string;
6545 it->bidi_it.string.s = NULL;
6546 it->bidi_it.string.schars = it->end_charpos;
6547 it->bidi_it.string.bufpos = 0;
6548 it->bidi_it.string.from_disp_str = false;
6549 it->bidi_it.string.unibyte = !it->multibyte_p;
6550 it->bidi_it.w = it->w;
6551 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6552 FRAME_WINDOW_P (it->f), &it->bidi_it);
6555 else
6557 it->s = (const unsigned char *) s;
6558 it->string = Qnil;
6560 /* Note that we use IT->current.pos, not it->current.string_pos,
6561 for displaying C strings. */
6562 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6563 if (it->multibyte_p)
6565 it->current.pos = c_string_pos (charpos, s, true);
6566 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6568 else
6570 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6571 it->end_charpos = it->string_nchars = strlen (s);
6574 if (it->bidi_p)
6576 it->bidi_it.string.lstring = Qnil;
6577 it->bidi_it.string.s = (const unsigned char *) s;
6578 it->bidi_it.string.schars = it->end_charpos;
6579 it->bidi_it.string.bufpos = 0;
6580 it->bidi_it.string.from_disp_str = false;
6581 it->bidi_it.string.unibyte = !it->multibyte_p;
6582 it->bidi_it.w = it->w;
6583 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6584 &it->bidi_it);
6586 it->method = GET_FROM_C_STRING;
6589 /* PRECISION > 0 means don't return more than PRECISION characters
6590 from the string. */
6591 if (precision > 0 && it->end_charpos - charpos > precision)
6593 it->end_charpos = it->string_nchars = charpos + precision;
6594 if (it->bidi_p)
6595 it->bidi_it.string.schars = it->end_charpos;
6598 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6599 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6600 FIELD_WIDTH < 0 means infinite field width. This is useful for
6601 padding with `-' at the end of a mode line. */
6602 if (field_width < 0)
6603 field_width = INFINITY;
6604 /* Implementation note: We deliberately don't enlarge
6605 it->bidi_it.string.schars here to fit it->end_charpos, because
6606 the bidi iterator cannot produce characters out of thin air. */
6607 if (field_width > it->end_charpos - charpos)
6608 it->end_charpos = charpos + field_width;
6610 /* Use the standard display table for displaying strings. */
6611 if (DISP_TABLE_P (Vstandard_display_table))
6612 it->dp = XCHAR_TABLE (Vstandard_display_table);
6614 it->stop_charpos = charpos;
6615 it->prev_stop = charpos;
6616 it->base_level_stop = 0;
6617 if (it->bidi_p)
6619 it->bidi_it.first_elt = true;
6620 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6621 it->bidi_it.disp_pos = -1;
6623 if (s == NULL && it->multibyte_p)
6625 ptrdiff_t endpos = SCHARS (it->string);
6626 if (endpos > it->end_charpos)
6627 endpos = it->end_charpos;
6628 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6629 it->string);
6631 CHECK_IT (it);
6636 /***********************************************************************
6637 Iteration
6638 ***********************************************************************/
6640 /* Map enum it_method value to corresponding next_element_from_* function. */
6642 typedef bool (*next_element_function) (struct it *);
6644 static next_element_function const get_next_element[NUM_IT_METHODS] =
6646 next_element_from_buffer,
6647 next_element_from_display_vector,
6648 next_element_from_string,
6649 next_element_from_c_string,
6650 next_element_from_image,
6651 next_element_from_stretch
6654 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6657 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6658 (possibly with the following characters). */
6660 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6661 ((IT)->cmp_it.id >= 0 \
6662 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6663 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6664 END_CHARPOS, (IT)->w, \
6665 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6666 (IT)->string)))
6669 /* Lookup the char-table Vglyphless_char_display for character C (-1
6670 if we want information for no-font case), and return the display
6671 method symbol. By side-effect, update it->what and
6672 it->glyphless_method. This function is called from
6673 get_next_display_element for each character element, and from
6674 x_produce_glyphs when no suitable font was found. */
6676 Lisp_Object
6677 lookup_glyphless_char_display (int c, struct it *it)
6679 Lisp_Object glyphless_method = Qnil;
6681 if (CHAR_TABLE_P (Vglyphless_char_display)
6682 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6684 if (c >= 0)
6686 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6687 if (CONSP (glyphless_method))
6688 glyphless_method = FRAME_WINDOW_P (it->f)
6689 ? XCAR (glyphless_method)
6690 : XCDR (glyphless_method);
6692 else
6693 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6696 retry:
6697 if (NILP (glyphless_method))
6699 if (c >= 0)
6700 /* The default is to display the character by a proper font. */
6701 return Qnil;
6702 /* The default for the no-font case is to display an empty box. */
6703 glyphless_method = Qempty_box;
6705 if (EQ (glyphless_method, Qzero_width))
6707 if (c >= 0)
6708 return glyphless_method;
6709 /* This method can't be used for the no-font case. */
6710 glyphless_method = Qempty_box;
6712 if (EQ (glyphless_method, Qthin_space))
6713 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6714 else if (EQ (glyphless_method, Qempty_box))
6715 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6716 else if (EQ (glyphless_method, Qhex_code))
6717 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6718 else if (STRINGP (glyphless_method))
6719 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6720 else
6722 /* Invalid value. We use the default method. */
6723 glyphless_method = Qnil;
6724 goto retry;
6726 it->what = IT_GLYPHLESS;
6727 return glyphless_method;
6730 /* Merge escape glyph face and cache the result. */
6732 static struct frame *last_escape_glyph_frame = NULL;
6733 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6734 static int last_escape_glyph_merged_face_id = 0;
6736 static int
6737 merge_escape_glyph_face (struct it *it)
6739 int face_id;
6741 if (it->f == last_escape_glyph_frame
6742 && it->face_id == last_escape_glyph_face_id)
6743 face_id = last_escape_glyph_merged_face_id;
6744 else
6746 /* Merge the `escape-glyph' face into the current face. */
6747 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6748 last_escape_glyph_frame = it->f;
6749 last_escape_glyph_face_id = it->face_id;
6750 last_escape_glyph_merged_face_id = face_id;
6752 return face_id;
6755 /* Likewise for glyphless glyph face. */
6757 static struct frame *last_glyphless_glyph_frame = NULL;
6758 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6759 static int last_glyphless_glyph_merged_face_id = 0;
6762 merge_glyphless_glyph_face (struct it *it)
6764 int face_id;
6766 if (it->f == last_glyphless_glyph_frame
6767 && it->face_id == last_glyphless_glyph_face_id)
6768 face_id = last_glyphless_glyph_merged_face_id;
6769 else
6771 /* Merge the `glyphless-char' face into the current face. */
6772 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6773 last_glyphless_glyph_frame = it->f;
6774 last_glyphless_glyph_face_id = it->face_id;
6775 last_glyphless_glyph_merged_face_id = face_id;
6777 return face_id;
6780 /* Load IT's display element fields with information about the next
6781 display element from the current position of IT. Value is false if
6782 end of buffer (or C string) is reached. */
6784 static bool
6785 get_next_display_element (struct it *it)
6787 /* True means that we found a display element. False means that
6788 we hit the end of what we iterate over. Performance note: the
6789 function pointer `method' used here turns out to be faster than
6790 using a sequence of if-statements. */
6791 bool success_p;
6793 get_next:
6794 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6796 if (it->what == IT_CHARACTER)
6798 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6799 and only if (a) the resolved directionality of that character
6800 is R..." */
6801 /* FIXME: Do we need an exception for characters from display
6802 tables? */
6803 if (it->bidi_p && it->bidi_it.type == STRONG_R
6804 && !inhibit_bidi_mirroring)
6805 it->c = bidi_mirror_char (it->c);
6806 /* Map via display table or translate control characters.
6807 IT->c, IT->len etc. have been set to the next character by
6808 the function call above. If we have a display table, and it
6809 contains an entry for IT->c, translate it. Don't do this if
6810 IT->c itself comes from a display table, otherwise we could
6811 end up in an infinite recursion. (An alternative could be to
6812 count the recursion depth of this function and signal an
6813 error when a certain maximum depth is reached.) Is it worth
6814 it? */
6815 if (success_p && it->dpvec == NULL)
6817 Lisp_Object dv;
6818 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6819 bool nonascii_space_p = false;
6820 bool nonascii_hyphen_p = false;
6821 int c = it->c; /* This is the character to display. */
6823 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6825 eassert (SINGLE_BYTE_CHAR_P (c));
6826 if (unibyte_display_via_language_environment)
6828 c = DECODE_CHAR (unibyte, c);
6829 if (c < 0)
6830 c = BYTE8_TO_CHAR (it->c);
6832 else
6833 c = BYTE8_TO_CHAR (it->c);
6836 if (it->dp
6837 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6838 VECTORP (dv)))
6840 struct Lisp_Vector *v = XVECTOR (dv);
6842 /* Return the first character from the display table
6843 entry, if not empty. If empty, don't display the
6844 current character. */
6845 if (v->header.size)
6847 it->dpvec_char_len = it->len;
6848 it->dpvec = v->contents;
6849 it->dpend = v->contents + v->header.size;
6850 it->current.dpvec_index = 0;
6851 it->dpvec_face_id = -1;
6852 it->saved_face_id = it->face_id;
6853 it->method = GET_FROM_DISPLAY_VECTOR;
6854 it->ellipsis_p = false;
6856 else
6858 set_iterator_to_next (it, false);
6860 goto get_next;
6863 if (! NILP (lookup_glyphless_char_display (c, it)))
6865 if (it->what == IT_GLYPHLESS)
6866 goto done;
6867 /* Don't display this character. */
6868 set_iterator_to_next (it, false);
6869 goto get_next;
6872 /* If `nobreak-char-display' is non-nil, we display
6873 non-ASCII spaces and hyphens specially. */
6874 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6876 if (c == 0xA0)
6877 nonascii_space_p = true;
6878 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6879 nonascii_hyphen_p = true;
6882 /* Translate control characters into `\003' or `^C' form.
6883 Control characters coming from a display table entry are
6884 currently not translated because we use IT->dpvec to hold
6885 the translation. This could easily be changed but I
6886 don't believe that it is worth doing.
6888 The characters handled by `nobreak-char-display' must be
6889 translated too.
6891 Non-printable characters and raw-byte characters are also
6892 translated to octal form. */
6893 if (((c < ' ' || c == 127) /* ASCII control chars. */
6894 ? (it->area != TEXT_AREA
6895 /* In mode line, treat \n, \t like other crl chars. */
6896 || (c != '\t'
6897 && it->glyph_row
6898 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6899 || (c != '\n' && c != '\t'))
6900 : (nonascii_space_p
6901 || nonascii_hyphen_p
6902 || CHAR_BYTE8_P (c)
6903 || ! CHAR_PRINTABLE_P (c))))
6905 /* C is a control character, non-ASCII space/hyphen,
6906 raw-byte, or a non-printable character which must be
6907 displayed either as '\003' or as `^C' where the '\\'
6908 and '^' can be defined in the display table. Fill
6909 IT->ctl_chars with glyphs for what we have to
6910 display. Then, set IT->dpvec to these glyphs. */
6911 Lisp_Object gc;
6912 int ctl_len;
6913 int face_id;
6914 int lface_id = 0;
6915 int escape_glyph;
6917 /* Handle control characters with ^. */
6919 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6921 int g;
6923 g = '^'; /* default glyph for Control */
6924 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6925 if (it->dp
6926 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6928 g = GLYPH_CODE_CHAR (gc);
6929 lface_id = GLYPH_CODE_FACE (gc);
6932 face_id = (lface_id
6933 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6934 : merge_escape_glyph_face (it));
6936 XSETINT (it->ctl_chars[0], g);
6937 XSETINT (it->ctl_chars[1], c ^ 0100);
6938 ctl_len = 2;
6939 goto display_control;
6942 /* Handle non-ascii space in the mode where it only gets
6943 highlighting. */
6945 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6947 /* Merge `nobreak-space' into the current face. */
6948 face_id = merge_faces (it->f, Qnobreak_space, 0,
6949 it->face_id);
6950 XSETINT (it->ctl_chars[0], ' ');
6951 ctl_len = 1;
6952 goto display_control;
6955 /* Handle sequences that start with the "escape glyph". */
6957 /* the default escape glyph is \. */
6958 escape_glyph = '\\';
6960 if (it->dp
6961 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6963 escape_glyph = GLYPH_CODE_CHAR (gc);
6964 lface_id = GLYPH_CODE_FACE (gc);
6967 face_id = (lface_id
6968 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6969 : merge_escape_glyph_face (it));
6971 /* Draw non-ASCII hyphen with just highlighting: */
6973 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6975 XSETINT (it->ctl_chars[0], '-');
6976 ctl_len = 1;
6977 goto display_control;
6980 /* Draw non-ASCII space/hyphen with escape glyph: */
6982 if (nonascii_space_p || nonascii_hyphen_p)
6984 XSETINT (it->ctl_chars[0], escape_glyph);
6985 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6986 ctl_len = 2;
6987 goto display_control;
6991 char str[10];
6992 int len, i;
6994 if (CHAR_BYTE8_P (c))
6995 /* Display \200 instead of \17777600. */
6996 c = CHAR_TO_BYTE8 (c);
6997 len = sprintf (str, "%03o", c + 0u);
6999 XSETINT (it->ctl_chars[0], escape_glyph);
7000 for (i = 0; i < len; i++)
7001 XSETINT (it->ctl_chars[i + 1], str[i]);
7002 ctl_len = len + 1;
7005 display_control:
7006 /* Set up IT->dpvec and return first character from it. */
7007 it->dpvec_char_len = it->len;
7008 it->dpvec = it->ctl_chars;
7009 it->dpend = it->dpvec + ctl_len;
7010 it->current.dpvec_index = 0;
7011 it->dpvec_face_id = face_id;
7012 it->saved_face_id = it->face_id;
7013 it->method = GET_FROM_DISPLAY_VECTOR;
7014 it->ellipsis_p = false;
7015 goto get_next;
7017 it->char_to_display = c;
7019 else if (success_p)
7021 it->char_to_display = it->c;
7025 #ifdef HAVE_WINDOW_SYSTEM
7026 /* Adjust face id for a multibyte character. There are no multibyte
7027 character in unibyte text. */
7028 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7029 && it->multibyte_p
7030 && success_p
7031 && FRAME_WINDOW_P (it->f))
7033 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7035 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7037 /* Automatic composition with glyph-string. */
7038 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7040 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7042 else
7044 ptrdiff_t pos = (it->s ? -1
7045 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7046 : IT_CHARPOS (*it));
7047 int c;
7049 if (it->what == IT_CHARACTER)
7050 c = it->char_to_display;
7051 else
7053 struct composition *cmp = composition_table[it->cmp_it.id];
7054 int i;
7056 c = ' ';
7057 for (i = 0; i < cmp->glyph_len; i++)
7058 /* TAB in a composition means display glyphs with
7059 padding space on the left or right. */
7060 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7061 break;
7063 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7066 #endif /* HAVE_WINDOW_SYSTEM */
7068 done:
7069 /* Is this character the last one of a run of characters with
7070 box? If yes, set IT->end_of_box_run_p to true. */
7071 if (it->face_box_p
7072 && it->s == NULL)
7074 if (it->method == GET_FROM_STRING && it->sp)
7076 int face_id = underlying_face_id (it);
7077 struct face *face = FACE_FROM_ID (it->f, face_id);
7079 if (face)
7081 if (face->box == FACE_NO_BOX)
7083 /* If the box comes from face properties in a
7084 display string, check faces in that string. */
7085 int string_face_id = face_after_it_pos (it);
7086 it->end_of_box_run_p
7087 = (FACE_FROM_ID (it->f, string_face_id)->box
7088 == FACE_NO_BOX);
7090 /* Otherwise, the box comes from the underlying face.
7091 If this is the last string character displayed, check
7092 the next buffer location. */
7093 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7094 /* n_overlay_strings is unreliable unless
7095 overlay_string_index is non-negative. */
7096 && ((it->current.overlay_string_index >= 0
7097 && (it->current.overlay_string_index
7098 == it->n_overlay_strings - 1))
7099 /* A string from display property. */
7100 || it->from_disp_prop_p))
7102 ptrdiff_t ignore;
7103 int next_face_id;
7104 struct text_pos pos = it->current.pos;
7106 /* For a string from a display property, the next
7107 buffer position is stored in the 'position'
7108 member of the iteration stack slot below the
7109 current one, see handle_single_display_spec. By
7110 contrast, it->current.pos was is not yet updated
7111 to point to that buffer position; that will
7112 happen in pop_it, after we finish displaying the
7113 current string. Note that we already checked
7114 above that it->sp is positive, so subtracting one
7115 from it is safe. */
7116 if (it->from_disp_prop_p)
7117 pos = (it->stack + it->sp - 1)->position;
7118 else
7119 INC_TEXT_POS (pos, it->multibyte_p);
7121 if (CHARPOS (pos) >= ZV)
7122 it->end_of_box_run_p = true;
7123 else
7125 next_face_id = face_at_buffer_position
7126 (it->w, CHARPOS (pos), &ignore,
7127 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7128 it->end_of_box_run_p
7129 = (FACE_FROM_ID (it->f, next_face_id)->box
7130 == FACE_NO_BOX);
7135 /* next_element_from_display_vector sets this flag according to
7136 faces of the display vector glyphs, see there. */
7137 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7139 int face_id = face_after_it_pos (it);
7140 it->end_of_box_run_p
7141 = (face_id != it->face_id
7142 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7145 /* If we reached the end of the object we've been iterating (e.g., a
7146 display string or an overlay string), and there's something on
7147 IT->stack, proceed with what's on the stack. It doesn't make
7148 sense to return false if there's unprocessed stuff on the stack,
7149 because otherwise that stuff will never be displayed. */
7150 if (!success_p && it->sp > 0)
7152 set_iterator_to_next (it, false);
7153 success_p = get_next_display_element (it);
7156 /* Value is false if end of buffer or string reached. */
7157 return success_p;
7161 /* Move IT to the next display element.
7163 RESEAT_P means if called on a newline in buffer text,
7164 skip to the next visible line start.
7166 Functions get_next_display_element and set_iterator_to_next are
7167 separate because I find this arrangement easier to handle than a
7168 get_next_display_element function that also increments IT's
7169 position. The way it is we can first look at an iterator's current
7170 display element, decide whether it fits on a line, and if it does,
7171 increment the iterator position. The other way around we probably
7172 would either need a flag indicating whether the iterator has to be
7173 incremented the next time, or we would have to implement a
7174 decrement position function which would not be easy to write. */
7176 void
7177 set_iterator_to_next (struct it *it, bool reseat_p)
7179 /* Reset flags indicating start and end of a sequence of characters
7180 with box. Reset them at the start of this function because
7181 moving the iterator to a new position might set them. */
7182 it->start_of_box_run_p = it->end_of_box_run_p = false;
7184 switch (it->method)
7186 case GET_FROM_BUFFER:
7187 /* The current display element of IT is a character from
7188 current_buffer. Advance in the buffer, and maybe skip over
7189 invisible lines that are so because of selective display. */
7190 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7191 reseat_at_next_visible_line_start (it, false);
7192 else if (it->cmp_it.id >= 0)
7194 /* We are currently getting glyphs from a composition. */
7195 if (! it->bidi_p)
7197 IT_CHARPOS (*it) += it->cmp_it.nchars;
7198 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7200 else
7202 int i;
7204 /* Update IT's char/byte positions to point to the first
7205 character of the next grapheme cluster, or to the
7206 character visually after the current composition. */
7207 for (i = 0; i < it->cmp_it.nchars; i++)
7208 bidi_move_to_visually_next (&it->bidi_it);
7209 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7210 IT_CHARPOS (*it) = it->bidi_it.charpos;
7213 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7214 && it->cmp_it.to < it->cmp_it.nglyphs)
7216 /* Composition created while scanning forward. Proceed
7217 to the next grapheme cluster. */
7218 it->cmp_it.from = it->cmp_it.to;
7220 else if ((it->bidi_p && it->cmp_it.reversed_p)
7221 && it->cmp_it.from > 0)
7223 /* Composition created while scanning backward. Proceed
7224 to the previous grapheme cluster. */
7225 it->cmp_it.to = it->cmp_it.from;
7227 else
7229 /* No more grapheme clusters in this composition.
7230 Find the next stop position. */
7231 ptrdiff_t stop = it->end_charpos;
7233 if (it->bidi_it.scan_dir < 0)
7234 /* Now we are scanning backward and don't know
7235 where to stop. */
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7238 IT_BYTEPOS (*it), stop, Qnil);
7241 else
7243 eassert (it->len != 0);
7245 if (!it->bidi_p)
7247 IT_BYTEPOS (*it) += it->len;
7248 IT_CHARPOS (*it) += 1;
7250 else
7252 int prev_scan_dir = it->bidi_it.scan_dir;
7253 /* If this is a new paragraph, determine its base
7254 direction (a.k.a. its base embedding level). */
7255 if (it->bidi_it.new_paragraph)
7256 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7257 false);
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_CHARPOS (*it) = it->bidi_it.charpos;
7261 if (prev_scan_dir != it->bidi_it.scan_dir)
7263 /* As the scan direction was changed, we must
7264 re-compute the stop position for composition. */
7265 ptrdiff_t stop = it->end_charpos;
7266 if (it->bidi_it.scan_dir < 0)
7267 stop = -1;
7268 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7269 IT_BYTEPOS (*it), stop, Qnil);
7272 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7274 break;
7276 case GET_FROM_C_STRING:
7277 /* Current display element of IT is from a C string. */
7278 if (!it->bidi_p
7279 /* If the string position is beyond string's end, it means
7280 next_element_from_c_string is padding the string with
7281 blanks, in which case we bypass the bidi iterator,
7282 because it cannot deal with such virtual characters. */
7283 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7285 IT_BYTEPOS (*it) += it->len;
7286 IT_CHARPOS (*it) += 1;
7288 else
7290 bidi_move_to_visually_next (&it->bidi_it);
7291 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7292 IT_CHARPOS (*it) = it->bidi_it.charpos;
7294 break;
7296 case GET_FROM_DISPLAY_VECTOR:
7297 /* Current display element of IT is from a display table entry.
7298 Advance in the display table definition. Reset it to null if
7299 end reached, and continue with characters from buffers/
7300 strings. */
7301 ++it->current.dpvec_index;
7303 /* Restore face of the iterator to what they were before the
7304 display vector entry (these entries may contain faces). */
7305 it->face_id = it->saved_face_id;
7307 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7309 bool recheck_faces = it->ellipsis_p;
7311 if (it->s)
7312 it->method = GET_FROM_C_STRING;
7313 else if (STRINGP (it->string))
7314 it->method = GET_FROM_STRING;
7315 else
7317 it->method = GET_FROM_BUFFER;
7318 it->object = it->w->contents;
7321 it->dpvec = NULL;
7322 it->current.dpvec_index = -1;
7324 /* Skip over characters which were displayed via IT->dpvec. */
7325 if (it->dpvec_char_len < 0)
7326 reseat_at_next_visible_line_start (it, true);
7327 else if (it->dpvec_char_len > 0)
7329 it->len = it->dpvec_char_len;
7330 set_iterator_to_next (it, reseat_p);
7333 /* Maybe recheck faces after display vector. */
7334 if (recheck_faces)
7336 if (it->method == GET_FROM_STRING)
7337 it->stop_charpos = IT_STRING_CHARPOS (*it);
7338 else
7339 it->stop_charpos = IT_CHARPOS (*it);
7342 break;
7344 case GET_FROM_STRING:
7345 /* Current display element is a character from a Lisp string. */
7346 eassert (it->s == NULL && STRINGP (it->string));
7347 /* Don't advance past string end. These conditions are true
7348 when set_iterator_to_next is called at the end of
7349 get_next_display_element, in which case the Lisp string is
7350 already exhausted, and all we want is pop the iterator
7351 stack. */
7352 if (it->current.overlay_string_index >= 0)
7354 /* This is an overlay string, so there's no padding with
7355 spaces, and the number of characters in the string is
7356 where the string ends. */
7357 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7358 goto consider_string_end;
7360 else
7362 /* Not an overlay string. There could be padding, so test
7363 against it->end_charpos. */
7364 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7365 goto consider_string_end;
7367 if (it->cmp_it.id >= 0)
7369 /* We are delivering display elements from a composition.
7370 Update the string position past the grapheme cluster
7371 we've just processed. */
7372 if (! it->bidi_p)
7374 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7375 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7377 else
7379 int i;
7381 for (i = 0; i < it->cmp_it.nchars; i++)
7382 bidi_move_to_visually_next (&it->bidi_it);
7383 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7384 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7387 /* Did we exhaust all the grapheme clusters of this
7388 composition? */
7389 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7390 && (it->cmp_it.to < it->cmp_it.nglyphs))
7392 /* Not all the grapheme clusters were processed yet;
7393 advance to the next cluster. */
7394 it->cmp_it.from = it->cmp_it.to;
7396 else if ((it->bidi_p && it->cmp_it.reversed_p)
7397 && it->cmp_it.from > 0)
7399 /* Likewise: advance to the next cluster, but going in
7400 the reverse direction. */
7401 it->cmp_it.to = it->cmp_it.from;
7403 else
7405 /* This composition was fully processed; find the next
7406 candidate place for checking for composed
7407 characters. */
7408 /* Always limit string searches to the string length;
7409 any padding spaces are not part of the string, and
7410 there cannot be any compositions in that padding. */
7411 ptrdiff_t stop = SCHARS (it->string);
7413 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7414 stop = -1;
7415 else if (it->end_charpos < stop)
7417 /* Cf. PRECISION in reseat_to_string: we might be
7418 limited in how many of the string characters we
7419 need to deliver. */
7420 stop = it->end_charpos;
7422 composition_compute_stop_pos (&it->cmp_it,
7423 IT_STRING_CHARPOS (*it),
7424 IT_STRING_BYTEPOS (*it), stop,
7425 it->string);
7428 else
7430 if (!it->bidi_p
7431 /* If the string position is beyond string's end, it
7432 means next_element_from_string is padding the string
7433 with blanks, in which case we bypass the bidi
7434 iterator, because it cannot deal with such virtual
7435 characters. */
7436 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7438 IT_STRING_BYTEPOS (*it) += it->len;
7439 IT_STRING_CHARPOS (*it) += 1;
7441 else
7443 int prev_scan_dir = it->bidi_it.scan_dir;
7445 bidi_move_to_visually_next (&it->bidi_it);
7446 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7447 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7448 /* If the scan direction changes, we may need to update
7449 the place where to check for composed characters. */
7450 if (prev_scan_dir != it->bidi_it.scan_dir)
7452 ptrdiff_t stop = SCHARS (it->string);
7454 if (it->bidi_it.scan_dir < 0)
7455 stop = -1;
7456 else if (it->end_charpos < stop)
7457 stop = it->end_charpos;
7459 composition_compute_stop_pos (&it->cmp_it,
7460 IT_STRING_CHARPOS (*it),
7461 IT_STRING_BYTEPOS (*it), stop,
7462 it->string);
7467 consider_string_end:
7469 if (it->current.overlay_string_index >= 0)
7471 /* IT->string is an overlay string. Advance to the
7472 next, if there is one. */
7473 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7475 it->ellipsis_p = false;
7476 next_overlay_string (it);
7477 if (it->ellipsis_p)
7478 setup_for_ellipsis (it, 0);
7481 else
7483 /* IT->string is not an overlay string. If we reached
7484 its end, and there is something on IT->stack, proceed
7485 with what is on the stack. This can be either another
7486 string, this time an overlay string, or a buffer. */
7487 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7488 && it->sp > 0)
7490 pop_it (it);
7491 if (it->method == GET_FROM_STRING)
7492 goto consider_string_end;
7495 break;
7497 case GET_FROM_IMAGE:
7498 case GET_FROM_STRETCH:
7499 /* The position etc with which we have to proceed are on
7500 the stack. The position may be at the end of a string,
7501 if the `display' property takes up the whole string. */
7502 eassert (it->sp > 0);
7503 pop_it (it);
7504 if (it->method == GET_FROM_STRING)
7505 goto consider_string_end;
7506 break;
7508 default:
7509 /* There are no other methods defined, so this should be a bug. */
7510 emacs_abort ();
7513 eassert (it->method != GET_FROM_STRING
7514 || (STRINGP (it->string)
7515 && IT_STRING_CHARPOS (*it) >= 0));
7518 /* Load IT's display element fields with information about the next
7519 display element which comes from a display table entry or from the
7520 result of translating a control character to one of the forms `^C'
7521 or `\003'.
7523 IT->dpvec holds the glyphs to return as characters.
7524 IT->saved_face_id holds the face id before the display vector--it
7525 is restored into IT->face_id in set_iterator_to_next. */
7527 static bool
7528 next_element_from_display_vector (struct it *it)
7530 Lisp_Object gc;
7531 int prev_face_id = it->face_id;
7532 int next_face_id;
7534 /* Precondition. */
7535 eassert (it->dpvec && it->current.dpvec_index >= 0);
7537 it->face_id = it->saved_face_id;
7539 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7540 That seemed totally bogus - so I changed it... */
7541 gc = it->dpvec[it->current.dpvec_index];
7543 if (GLYPH_CODE_P (gc))
7545 struct face *this_face, *prev_face, *next_face;
7547 it->c = GLYPH_CODE_CHAR (gc);
7548 it->len = CHAR_BYTES (it->c);
7550 /* The entry may contain a face id to use. Such a face id is
7551 the id of a Lisp face, not a realized face. A face id of
7552 zero means no face is specified. */
7553 if (it->dpvec_face_id >= 0)
7554 it->face_id = it->dpvec_face_id;
7555 else
7557 int lface_id = GLYPH_CODE_FACE (gc);
7558 if (lface_id > 0)
7559 it->face_id = merge_faces (it->f, Qt, lface_id,
7560 it->saved_face_id);
7563 /* Glyphs in the display vector could have the box face, so we
7564 need to set the related flags in the iterator, as
7565 appropriate. */
7566 this_face = FACE_FROM_ID (it->f, it->face_id);
7567 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7569 /* Is this character the first character of a box-face run? */
7570 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7571 && (!prev_face
7572 || prev_face->box == FACE_NO_BOX));
7574 /* For the last character of the box-face run, we need to look
7575 either at the next glyph from the display vector, or at the
7576 face we saw before the display vector. */
7577 next_face_id = it->saved_face_id;
7578 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7580 if (it->dpvec_face_id >= 0)
7581 next_face_id = it->dpvec_face_id;
7582 else
7584 int lface_id =
7585 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7587 if (lface_id > 0)
7588 next_face_id = merge_faces (it->f, Qt, lface_id,
7589 it->saved_face_id);
7592 next_face = FACE_FROM_ID (it->f, next_face_id);
7593 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7594 && (!next_face
7595 || next_face->box == FACE_NO_BOX));
7596 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7598 else
7599 /* Display table entry is invalid. Return a space. */
7600 it->c = ' ', it->len = 1;
7602 /* Don't change position and object of the iterator here. They are
7603 still the values of the character that had this display table
7604 entry or was translated, and that's what we want. */
7605 it->what = IT_CHARACTER;
7606 return true;
7609 /* Get the first element of string/buffer in the visual order, after
7610 being reseated to a new position in a string or a buffer. */
7611 static void
7612 get_visually_first_element (struct it *it)
7614 bool string_p = STRINGP (it->string) || it->s;
7615 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7616 ptrdiff_t bob = (string_p ? 0 : BEGV);
7618 if (STRINGP (it->string))
7620 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7621 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7623 else
7625 it->bidi_it.charpos = IT_CHARPOS (*it);
7626 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7629 if (it->bidi_it.charpos == eob)
7631 /* Nothing to do, but reset the FIRST_ELT flag, like
7632 bidi_paragraph_init does, because we are not going to
7633 call it. */
7634 it->bidi_it.first_elt = false;
7636 else if (it->bidi_it.charpos == bob
7637 || (!string_p
7638 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7639 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7641 /* If we are at the beginning of a line/string, we can produce
7642 the next element right away. */
7643 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7644 bidi_move_to_visually_next (&it->bidi_it);
7646 else
7648 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7650 /* We need to prime the bidi iterator starting at the line's or
7651 string's beginning, before we will be able to produce the
7652 next element. */
7653 if (string_p)
7654 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7655 else
7656 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7657 IT_BYTEPOS (*it), -1,
7658 &it->bidi_it.bytepos);
7659 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7662 /* Now return to buffer/string position where we were asked
7663 to get the next display element, and produce that. */
7664 bidi_move_to_visually_next (&it->bidi_it);
7666 while (it->bidi_it.bytepos != orig_bytepos
7667 && it->bidi_it.charpos < eob);
7670 /* Adjust IT's position information to where we ended up. */
7671 if (STRINGP (it->string))
7673 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7674 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7676 else
7678 IT_CHARPOS (*it) = it->bidi_it.charpos;
7679 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7682 if (STRINGP (it->string) || !it->s)
7684 ptrdiff_t stop, charpos, bytepos;
7686 if (STRINGP (it->string))
7688 eassert (!it->s);
7689 stop = SCHARS (it->string);
7690 if (stop > it->end_charpos)
7691 stop = it->end_charpos;
7692 charpos = IT_STRING_CHARPOS (*it);
7693 bytepos = IT_STRING_BYTEPOS (*it);
7695 else
7697 stop = it->end_charpos;
7698 charpos = IT_CHARPOS (*it);
7699 bytepos = IT_BYTEPOS (*it);
7701 if (it->bidi_it.scan_dir < 0)
7702 stop = -1;
7703 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7704 it->string);
7708 /* Load IT with the next display element from Lisp string IT->string.
7709 IT->current.string_pos is the current position within the string.
7710 If IT->current.overlay_string_index >= 0, the Lisp string is an
7711 overlay string. */
7713 static bool
7714 next_element_from_string (struct it *it)
7716 struct text_pos position;
7718 eassert (STRINGP (it->string));
7719 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7720 eassert (IT_STRING_CHARPOS (*it) >= 0);
7721 position = it->current.string_pos;
7723 /* With bidi reordering, the character to display might not be the
7724 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7725 that we were reseat()ed to a new string, whose paragraph
7726 direction is not known. */
7727 if (it->bidi_p && it->bidi_it.first_elt)
7729 get_visually_first_element (it);
7730 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7733 /* Time to check for invisible text? */
7734 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7736 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7738 if (!(!it->bidi_p
7739 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7740 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7742 /* With bidi non-linear iteration, we could find
7743 ourselves far beyond the last computed stop_charpos,
7744 with several other stop positions in between that we
7745 missed. Scan them all now, in buffer's logical
7746 order, until we find and handle the last stop_charpos
7747 that precedes our current position. */
7748 handle_stop_backwards (it, it->stop_charpos);
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7751 else
7753 if (it->bidi_p)
7755 /* Take note of the stop position we just moved
7756 across, for when we will move back across it. */
7757 it->prev_stop = it->stop_charpos;
7758 /* If we are at base paragraph embedding level, take
7759 note of the last stop position seen at this
7760 level. */
7761 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7762 it->base_level_stop = it->stop_charpos;
7764 handle_stop (it);
7766 /* Since a handler may have changed IT->method, we must
7767 recurse here. */
7768 return GET_NEXT_DISPLAY_ELEMENT (it);
7771 else if (it->bidi_p
7772 /* If we are before prev_stop, we may have overstepped
7773 on our way backwards a stop_pos, and if so, we need
7774 to handle that stop_pos. */
7775 && IT_STRING_CHARPOS (*it) < it->prev_stop
7776 /* We can sometimes back up for reasons that have nothing
7777 to do with bidi reordering. E.g., compositions. The
7778 code below is only needed when we are above the base
7779 embedding level, so test for that explicitly. */
7780 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7782 /* If we lost track of base_level_stop, we have no better
7783 place for handle_stop_backwards to start from than string
7784 beginning. This happens, e.g., when we were reseated to
7785 the previous screenful of text by vertical-motion. */
7786 if (it->base_level_stop <= 0
7787 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7788 it->base_level_stop = 0;
7789 handle_stop_backwards (it, it->base_level_stop);
7790 return GET_NEXT_DISPLAY_ELEMENT (it);
7794 if (it->current.overlay_string_index >= 0)
7796 /* Get the next character from an overlay string. In overlay
7797 strings, there is no field width or padding with spaces to
7798 do. */
7799 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7801 it->what = IT_EOB;
7802 return false;
7804 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7805 IT_STRING_BYTEPOS (*it),
7806 it->bidi_it.scan_dir < 0
7807 ? -1
7808 : SCHARS (it->string))
7809 && next_element_from_composition (it))
7811 return true;
7813 else if (STRING_MULTIBYTE (it->string))
7815 const unsigned char *s = (SDATA (it->string)
7816 + IT_STRING_BYTEPOS (*it));
7817 it->c = string_char_and_length (s, &it->len);
7819 else
7821 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7822 it->len = 1;
7825 else
7827 /* Get the next character from a Lisp string that is not an
7828 overlay string. Such strings come from the mode line, for
7829 example. We may have to pad with spaces, or truncate the
7830 string. See also next_element_from_c_string. */
7831 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7833 it->what = IT_EOB;
7834 return false;
7836 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7838 /* Pad with spaces. */
7839 it->c = ' ', it->len = 1;
7840 CHARPOS (position) = BYTEPOS (position) = -1;
7842 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7843 IT_STRING_BYTEPOS (*it),
7844 it->bidi_it.scan_dir < 0
7845 ? -1
7846 : it->string_nchars)
7847 && next_element_from_composition (it))
7849 return true;
7851 else if (STRING_MULTIBYTE (it->string))
7853 const unsigned char *s = (SDATA (it->string)
7854 + IT_STRING_BYTEPOS (*it));
7855 it->c = string_char_and_length (s, &it->len);
7857 else
7859 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7860 it->len = 1;
7864 /* Record what we have and where it came from. */
7865 it->what = IT_CHARACTER;
7866 it->object = it->string;
7867 it->position = position;
7868 return true;
7872 /* Load IT with next display element from C string IT->s.
7873 IT->string_nchars is the maximum number of characters to return
7874 from the string. IT->end_charpos may be greater than
7875 IT->string_nchars when this function is called, in which case we
7876 may have to return padding spaces. Value is false if end of string
7877 reached, including padding spaces. */
7879 static bool
7880 next_element_from_c_string (struct it *it)
7882 bool success_p = true;
7884 eassert (it->s);
7885 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7886 it->what = IT_CHARACTER;
7887 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7888 it->object = make_number (0);
7890 /* With bidi reordering, the character to display might not be the
7891 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7892 we were reseated to a new string, whose paragraph direction is
7893 not known. */
7894 if (it->bidi_p && it->bidi_it.first_elt)
7895 get_visually_first_element (it);
7897 /* IT's position can be greater than IT->string_nchars in case a
7898 field width or precision has been specified when the iterator was
7899 initialized. */
7900 if (IT_CHARPOS (*it) >= it->end_charpos)
7902 /* End of the game. */
7903 it->what = IT_EOB;
7904 success_p = false;
7906 else if (IT_CHARPOS (*it) >= it->string_nchars)
7908 /* Pad with spaces. */
7909 it->c = ' ', it->len = 1;
7910 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7912 else if (it->multibyte_p)
7913 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7914 else
7915 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7917 return success_p;
7921 /* Set up IT to return characters from an ellipsis, if appropriate.
7922 The definition of the ellipsis glyphs may come from a display table
7923 entry. This function fills IT with the first glyph from the
7924 ellipsis if an ellipsis is to be displayed. */
7926 static bool
7927 next_element_from_ellipsis (struct it *it)
7929 if (it->selective_display_ellipsis_p)
7930 setup_for_ellipsis (it, it->len);
7931 else
7933 /* The face at the current position may be different from the
7934 face we find after the invisible text. Remember what it
7935 was in IT->saved_face_id, and signal that it's there by
7936 setting face_before_selective_p. */
7937 it->saved_face_id = it->face_id;
7938 it->method = GET_FROM_BUFFER;
7939 it->object = it->w->contents;
7940 reseat_at_next_visible_line_start (it, true);
7941 it->face_before_selective_p = true;
7944 return GET_NEXT_DISPLAY_ELEMENT (it);
7948 /* Deliver an image display element. The iterator IT is already
7949 filled with image information (done in handle_display_prop). Value
7950 is always true. */
7953 static bool
7954 next_element_from_image (struct it *it)
7956 it->what = IT_IMAGE;
7957 return true;
7961 /* Fill iterator IT with next display element from a stretch glyph
7962 property. IT->object is the value of the text property. Value is
7963 always true. */
7965 static bool
7966 next_element_from_stretch (struct it *it)
7968 it->what = IT_STRETCH;
7969 return true;
7972 /* Scan backwards from IT's current position until we find a stop
7973 position, or until BEGV. This is called when we find ourself
7974 before both the last known prev_stop and base_level_stop while
7975 reordering bidirectional text. */
7977 static void
7978 compute_stop_pos_backwards (struct it *it)
7980 const int SCAN_BACK_LIMIT = 1000;
7981 struct text_pos pos;
7982 struct display_pos save_current = it->current;
7983 struct text_pos save_position = it->position;
7984 ptrdiff_t charpos = IT_CHARPOS (*it);
7985 ptrdiff_t where_we_are = charpos;
7986 ptrdiff_t save_stop_pos = it->stop_charpos;
7987 ptrdiff_t save_end_pos = it->end_charpos;
7989 eassert (NILP (it->string) && !it->s);
7990 eassert (it->bidi_p);
7991 it->bidi_p = false;
7994 it->end_charpos = min (charpos + 1, ZV);
7995 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7996 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7997 reseat_1 (it, pos, false);
7998 compute_stop_pos (it);
7999 /* We must advance forward, right? */
8000 if (it->stop_charpos <= charpos)
8001 emacs_abort ();
8003 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8005 if (it->stop_charpos <= where_we_are)
8006 it->prev_stop = it->stop_charpos;
8007 else
8008 it->prev_stop = BEGV;
8009 it->bidi_p = true;
8010 it->current = save_current;
8011 it->position = save_position;
8012 it->stop_charpos = save_stop_pos;
8013 it->end_charpos = save_end_pos;
8016 /* Scan forward from CHARPOS in the current buffer/string, until we
8017 find a stop position > current IT's position. Then handle the stop
8018 position before that. This is called when we bump into a stop
8019 position while reordering bidirectional text. CHARPOS should be
8020 the last previously processed stop_pos (or BEGV/0, if none were
8021 processed yet) whose position is less that IT's current
8022 position. */
8024 static void
8025 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8027 bool bufp = !STRINGP (it->string);
8028 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8029 struct display_pos save_current = it->current;
8030 struct text_pos save_position = it->position;
8031 struct text_pos pos1;
8032 ptrdiff_t next_stop;
8034 /* Scan in strict logical order. */
8035 eassert (it->bidi_p);
8036 it->bidi_p = false;
8039 it->prev_stop = charpos;
8040 if (bufp)
8042 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8043 reseat_1 (it, pos1, false);
8045 else
8046 it->current.string_pos = string_pos (charpos, it->string);
8047 compute_stop_pos (it);
8048 /* We must advance forward, right? */
8049 if (it->stop_charpos <= it->prev_stop)
8050 emacs_abort ();
8051 charpos = it->stop_charpos;
8053 while (charpos <= where_we_are);
8055 it->bidi_p = true;
8056 it->current = save_current;
8057 it->position = save_position;
8058 next_stop = it->stop_charpos;
8059 it->stop_charpos = it->prev_stop;
8060 handle_stop (it);
8061 it->stop_charpos = next_stop;
8064 /* Load IT with the next display element from current_buffer. Value
8065 is false if end of buffer reached. IT->stop_charpos is the next
8066 position at which to stop and check for text properties or buffer
8067 end. */
8069 static bool
8070 next_element_from_buffer (struct it *it)
8072 bool success_p = true;
8074 eassert (IT_CHARPOS (*it) >= BEGV);
8075 eassert (NILP (it->string) && !it->s);
8076 eassert (!it->bidi_p
8077 || (EQ (it->bidi_it.string.lstring, Qnil)
8078 && it->bidi_it.string.s == NULL));
8080 /* With bidi reordering, the character to display might not be the
8081 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8082 we were reseat()ed to a new buffer position, which is potentially
8083 a different paragraph. */
8084 if (it->bidi_p && it->bidi_it.first_elt)
8086 get_visually_first_element (it);
8087 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8090 if (IT_CHARPOS (*it) >= it->stop_charpos)
8092 if (IT_CHARPOS (*it) >= it->end_charpos)
8094 bool overlay_strings_follow_p;
8096 /* End of the game, except when overlay strings follow that
8097 haven't been returned yet. */
8098 if (it->overlay_strings_at_end_processed_p)
8099 overlay_strings_follow_p = false;
8100 else
8102 it->overlay_strings_at_end_processed_p = true;
8103 overlay_strings_follow_p = get_overlay_strings (it, 0);
8106 if (overlay_strings_follow_p)
8107 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8108 else
8110 it->what = IT_EOB;
8111 it->position = it->current.pos;
8112 success_p = false;
8115 else if (!(!it->bidi_p
8116 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8117 || IT_CHARPOS (*it) == it->stop_charpos))
8119 /* With bidi non-linear iteration, we could find ourselves
8120 far beyond the last computed stop_charpos, with several
8121 other stop positions in between that we missed. Scan
8122 them all now, in buffer's logical order, until we find
8123 and handle the last stop_charpos that precedes our
8124 current position. */
8125 handle_stop_backwards (it, it->stop_charpos);
8126 it->ignore_overlay_strings_at_pos_p = false;
8127 return GET_NEXT_DISPLAY_ELEMENT (it);
8129 else
8131 if (it->bidi_p)
8133 /* Take note of the stop position we just moved across,
8134 for when we will move back across it. */
8135 it->prev_stop = it->stop_charpos;
8136 /* If we are at base paragraph embedding level, take
8137 note of the last stop position seen at this
8138 level. */
8139 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8140 it->base_level_stop = it->stop_charpos;
8142 handle_stop (it);
8143 it->ignore_overlay_strings_at_pos_p = false;
8144 return GET_NEXT_DISPLAY_ELEMENT (it);
8147 else if (it->bidi_p
8148 /* If we are before prev_stop, we may have overstepped on
8149 our way backwards a stop_pos, and if so, we need to
8150 handle that stop_pos. */
8151 && IT_CHARPOS (*it) < it->prev_stop
8152 /* We can sometimes back up for reasons that have nothing
8153 to do with bidi reordering. E.g., compositions. The
8154 code below is only needed when we are above the base
8155 embedding level, so test for that explicitly. */
8156 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8158 if (it->base_level_stop <= 0
8159 || IT_CHARPOS (*it) < it->base_level_stop)
8161 /* If we lost track of base_level_stop, we need to find
8162 prev_stop by looking backwards. This happens, e.g., when
8163 we were reseated to the previous screenful of text by
8164 vertical-motion. */
8165 it->base_level_stop = BEGV;
8166 compute_stop_pos_backwards (it);
8167 handle_stop_backwards (it, it->prev_stop);
8169 else
8170 handle_stop_backwards (it, it->base_level_stop);
8171 it->ignore_overlay_strings_at_pos_p = false;
8172 return GET_NEXT_DISPLAY_ELEMENT (it);
8174 else
8176 /* No face changes, overlays etc. in sight, so just return a
8177 character from current_buffer. */
8178 unsigned char *p;
8179 ptrdiff_t stop;
8181 /* We moved to the next buffer position, so any info about
8182 previously seen overlays is no longer valid. */
8183 it->ignore_overlay_strings_at_pos_p = false;
8185 /* Maybe run the redisplay end trigger hook. Performance note:
8186 This doesn't seem to cost measurable time. */
8187 if (it->redisplay_end_trigger_charpos
8188 && it->glyph_row
8189 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8190 run_redisplay_end_trigger_hook (it);
8192 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8193 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8194 stop)
8195 && next_element_from_composition (it))
8197 return true;
8200 /* Get the next character, maybe multibyte. */
8201 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8202 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8203 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8204 else
8205 it->c = *p, it->len = 1;
8207 /* Record what we have and where it came from. */
8208 it->what = IT_CHARACTER;
8209 it->object = it->w->contents;
8210 it->position = it->current.pos;
8212 /* Normally we return the character found above, except when we
8213 really want to return an ellipsis for selective display. */
8214 if (it->selective)
8216 if (it->c == '\n')
8218 /* A value of selective > 0 means hide lines indented more
8219 than that number of columns. */
8220 if (it->selective > 0
8221 && IT_CHARPOS (*it) + 1 < ZV
8222 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8223 IT_BYTEPOS (*it) + 1,
8224 it->selective))
8226 success_p = next_element_from_ellipsis (it);
8227 it->dpvec_char_len = -1;
8230 else if (it->c == '\r' && it->selective == -1)
8232 /* A value of selective == -1 means that everything from the
8233 CR to the end of the line is invisible, with maybe an
8234 ellipsis displayed for it. */
8235 success_p = next_element_from_ellipsis (it);
8236 it->dpvec_char_len = -1;
8241 /* Value is false if end of buffer reached. */
8242 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8243 return success_p;
8247 /* Run the redisplay end trigger hook for IT. */
8249 static void
8250 run_redisplay_end_trigger_hook (struct it *it)
8252 /* IT->glyph_row should be non-null, i.e. we should be actually
8253 displaying something, or otherwise we should not run the hook. */
8254 eassert (it->glyph_row);
8256 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8257 it->redisplay_end_trigger_charpos = 0;
8259 /* Since we are *trying* to run these functions, don't try to run
8260 them again, even if they get an error. */
8261 wset_redisplay_end_trigger (it->w, Qnil);
8262 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8263 make_number (charpos));
8265 /* Notice if it changed the face of the character we are on. */
8266 handle_face_prop (it);
8270 /* Deliver a composition display element. Unlike the other
8271 next_element_from_XXX, this function is not registered in the array
8272 get_next_element[]. It is called from next_element_from_buffer and
8273 next_element_from_string when necessary. */
8275 static bool
8276 next_element_from_composition (struct it *it)
8278 it->what = IT_COMPOSITION;
8279 it->len = it->cmp_it.nbytes;
8280 if (STRINGP (it->string))
8282 if (it->c < 0)
8284 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8285 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8286 return false;
8288 it->position = it->current.string_pos;
8289 it->object = it->string;
8290 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8291 IT_STRING_BYTEPOS (*it), it->string);
8293 else
8295 if (it->c < 0)
8297 IT_CHARPOS (*it) += it->cmp_it.nchars;
8298 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8299 if (it->bidi_p)
8301 if (it->bidi_it.new_paragraph)
8302 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8303 false);
8304 /* Resync the bidi iterator with IT's new position.
8305 FIXME: this doesn't support bidirectional text. */
8306 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8307 bidi_move_to_visually_next (&it->bidi_it);
8309 return false;
8311 it->position = it->current.pos;
8312 it->object = it->w->contents;
8313 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8314 IT_BYTEPOS (*it), Qnil);
8316 return true;
8321 /***********************************************************************
8322 Moving an iterator without producing glyphs
8323 ***********************************************************************/
8325 /* Check if iterator is at a position corresponding to a valid buffer
8326 position after some move_it_ call. */
8328 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8329 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8332 /* Move iterator IT to a specified buffer or X position within one
8333 line on the display without producing glyphs.
8335 OP should be a bit mask including some or all of these bits:
8336 MOVE_TO_X: Stop upon reaching x-position TO_X.
8337 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8338 Regardless of OP's value, stop upon reaching the end of the display line.
8340 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8341 This means, in particular, that TO_X includes window's horizontal
8342 scroll amount.
8344 The return value has several possible values that
8345 say what condition caused the scan to stop:
8347 MOVE_POS_MATCH_OR_ZV
8348 - when TO_POS or ZV was reached.
8350 MOVE_X_REACHED
8351 -when TO_X was reached before TO_POS or ZV were reached.
8353 MOVE_LINE_CONTINUED
8354 - when we reached the end of the display area and the line must
8355 be continued.
8357 MOVE_LINE_TRUNCATED
8358 - when we reached the end of the display area and the line is
8359 truncated.
8361 MOVE_NEWLINE_OR_CR
8362 - when we stopped at a line end, i.e. a newline or a CR and selective
8363 display is on. */
8365 static enum move_it_result
8366 move_it_in_display_line_to (struct it *it,
8367 ptrdiff_t to_charpos, int to_x,
8368 enum move_operation_enum op)
8370 enum move_it_result result = MOVE_UNDEFINED;
8371 struct glyph_row *saved_glyph_row;
8372 struct it wrap_it, atpos_it, atx_it, ppos_it;
8373 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8374 void *ppos_data = NULL;
8375 bool may_wrap = false;
8376 enum it_method prev_method = it->method;
8377 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8378 bool saw_smaller_pos = prev_pos < to_charpos;
8380 /* Don't produce glyphs in produce_glyphs. */
8381 saved_glyph_row = it->glyph_row;
8382 it->glyph_row = NULL;
8384 /* Use wrap_it to save a copy of IT wherever a word wrap could
8385 occur. Use atpos_it to save a copy of IT at the desired buffer
8386 position, if found, so that we can scan ahead and check if the
8387 word later overshoots the window edge. Use atx_it similarly, for
8388 pixel positions. */
8389 wrap_it.sp = -1;
8390 atpos_it.sp = -1;
8391 atx_it.sp = -1;
8393 /* Use ppos_it under bidi reordering to save a copy of IT for the
8394 initial position. We restore that position in IT when we have
8395 scanned the entire display line without finding a match for
8396 TO_CHARPOS and all the character positions are greater than
8397 TO_CHARPOS. We then restart the scan from the initial position,
8398 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8399 the closest to TO_CHARPOS. */
8400 if (it->bidi_p)
8402 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8404 SAVE_IT (ppos_it, *it, ppos_data);
8405 closest_pos = IT_CHARPOS (*it);
8407 else
8408 closest_pos = ZV;
8411 #define BUFFER_POS_REACHED_P() \
8412 ((op & MOVE_TO_POS) != 0 \
8413 && BUFFERP (it->object) \
8414 && (IT_CHARPOS (*it) == to_charpos \
8415 || ((!it->bidi_p \
8416 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8417 && IT_CHARPOS (*it) > to_charpos) \
8418 || (it->what == IT_COMPOSITION \
8419 && ((IT_CHARPOS (*it) > to_charpos \
8420 && to_charpos >= it->cmp_it.charpos) \
8421 || (IT_CHARPOS (*it) < to_charpos \
8422 && to_charpos <= it->cmp_it.charpos)))) \
8423 && (it->method == GET_FROM_BUFFER \
8424 || (it->method == GET_FROM_DISPLAY_VECTOR \
8425 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8427 /* If there's a line-/wrap-prefix, handle it. */
8428 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8429 && it->current_y < it->last_visible_y)
8430 handle_line_prefix (it);
8432 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8433 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8435 while (true)
8437 int x, i, ascent = 0, descent = 0;
8439 /* Utility macro to reset an iterator with x, ascent, and descent. */
8440 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8441 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8442 (IT)->max_descent = descent)
8444 /* Stop if we move beyond TO_CHARPOS (after an image or a
8445 display string or stretch glyph). */
8446 if ((op & MOVE_TO_POS) != 0
8447 && BUFFERP (it->object)
8448 && it->method == GET_FROM_BUFFER
8449 && (((!it->bidi_p
8450 /* When the iterator is at base embedding level, we
8451 are guaranteed that characters are delivered for
8452 display in strictly increasing order of their
8453 buffer positions. */
8454 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8455 && IT_CHARPOS (*it) > to_charpos)
8456 || (it->bidi_p
8457 && (prev_method == GET_FROM_IMAGE
8458 || prev_method == GET_FROM_STRETCH
8459 || prev_method == GET_FROM_STRING)
8460 /* Passed TO_CHARPOS from left to right. */
8461 && ((prev_pos < to_charpos
8462 && IT_CHARPOS (*it) > to_charpos)
8463 /* Passed TO_CHARPOS from right to left. */
8464 || (prev_pos > to_charpos
8465 && IT_CHARPOS (*it) < to_charpos)))))
8467 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8469 result = MOVE_POS_MATCH_OR_ZV;
8470 break;
8472 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8473 /* If wrap_it is valid, the current position might be in a
8474 word that is wrapped. So, save the iterator in
8475 atpos_it and continue to see if wrapping happens. */
8476 SAVE_IT (atpos_it, *it, atpos_data);
8479 /* Stop when ZV reached.
8480 We used to stop here when TO_CHARPOS reached as well, but that is
8481 too soon if this glyph does not fit on this line. So we handle it
8482 explicitly below. */
8483 if (!get_next_display_element (it))
8485 result = MOVE_POS_MATCH_OR_ZV;
8486 break;
8489 if (it->line_wrap == TRUNCATE)
8491 if (BUFFER_POS_REACHED_P ())
8493 result = MOVE_POS_MATCH_OR_ZV;
8494 break;
8497 else
8499 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8501 if (IT_DISPLAYING_WHITESPACE (it))
8502 may_wrap = true;
8503 else if (may_wrap)
8505 /* We have reached a glyph that follows one or more
8506 whitespace characters. If the position is
8507 already found, we are done. */
8508 if (atpos_it.sp >= 0)
8510 RESTORE_IT (it, &atpos_it, atpos_data);
8511 result = MOVE_POS_MATCH_OR_ZV;
8512 goto done;
8514 if (atx_it.sp >= 0)
8516 RESTORE_IT (it, &atx_it, atx_data);
8517 result = MOVE_X_REACHED;
8518 goto done;
8520 /* Otherwise, we can wrap here. */
8521 SAVE_IT (wrap_it, *it, wrap_data);
8522 may_wrap = false;
8527 /* Remember the line height for the current line, in case
8528 the next element doesn't fit on the line. */
8529 ascent = it->max_ascent;
8530 descent = it->max_descent;
8532 /* The call to produce_glyphs will get the metrics of the
8533 display element IT is loaded with. Record the x-position
8534 before this display element, in case it doesn't fit on the
8535 line. */
8536 x = it->current_x;
8538 PRODUCE_GLYPHS (it);
8540 if (it->area != TEXT_AREA)
8542 prev_method = it->method;
8543 if (it->method == GET_FROM_BUFFER)
8544 prev_pos = IT_CHARPOS (*it);
8545 set_iterator_to_next (it, true);
8546 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8547 SET_TEXT_POS (this_line_min_pos,
8548 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8549 if (it->bidi_p
8550 && (op & MOVE_TO_POS)
8551 && IT_CHARPOS (*it) > to_charpos
8552 && IT_CHARPOS (*it) < closest_pos)
8553 closest_pos = IT_CHARPOS (*it);
8554 continue;
8557 /* The number of glyphs we get back in IT->nglyphs will normally
8558 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8559 character on a terminal frame, or (iii) a line end. For the
8560 second case, IT->nglyphs - 1 padding glyphs will be present.
8561 (On X frames, there is only one glyph produced for a
8562 composite character.)
8564 The behavior implemented below means, for continuation lines,
8565 that as many spaces of a TAB as fit on the current line are
8566 displayed there. For terminal frames, as many glyphs of a
8567 multi-glyph character are displayed in the current line, too.
8568 This is what the old redisplay code did, and we keep it that
8569 way. Under X, the whole shape of a complex character must
8570 fit on the line or it will be completely displayed in the
8571 next line.
8573 Note that both for tabs and padding glyphs, all glyphs have
8574 the same width. */
8575 if (it->nglyphs)
8577 /* More than one glyph or glyph doesn't fit on line. All
8578 glyphs have the same width. */
8579 int single_glyph_width = it->pixel_width / it->nglyphs;
8580 int new_x;
8581 int x_before_this_char = x;
8582 int hpos_before_this_char = it->hpos;
8584 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8586 new_x = x + single_glyph_width;
8588 /* We want to leave anything reaching TO_X to the caller. */
8589 if ((op & MOVE_TO_X) && new_x > to_x)
8591 if (BUFFER_POS_REACHED_P ())
8593 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8594 goto buffer_pos_reached;
8595 if (atpos_it.sp < 0)
8597 SAVE_IT (atpos_it, *it, atpos_data);
8598 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8601 else
8603 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8605 it->current_x = x;
8606 result = MOVE_X_REACHED;
8607 break;
8609 if (atx_it.sp < 0)
8611 SAVE_IT (atx_it, *it, atx_data);
8612 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8617 if (/* Lines are continued. */
8618 it->line_wrap != TRUNCATE
8619 && (/* And glyph doesn't fit on the line. */
8620 new_x > it->last_visible_x
8621 /* Or it fits exactly and we're on a window
8622 system frame. */
8623 || (new_x == it->last_visible_x
8624 && FRAME_WINDOW_P (it->f)
8625 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8626 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8627 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8629 if (/* IT->hpos == 0 means the very first glyph
8630 doesn't fit on the line, e.g. a wide image. */
8631 it->hpos == 0
8632 || (new_x == it->last_visible_x
8633 && FRAME_WINDOW_P (it->f)))
8635 ++it->hpos;
8636 it->current_x = new_x;
8638 /* The character's last glyph just barely fits
8639 in this row. */
8640 if (i == it->nglyphs - 1)
8642 /* If this is the destination position,
8643 return a position *before* it in this row,
8644 now that we know it fits in this row. */
8645 if (BUFFER_POS_REACHED_P ())
8647 if (it->line_wrap != WORD_WRAP
8648 || wrap_it.sp < 0
8649 /* If we've just found whitespace to
8650 wrap, effectively ignore the
8651 previous wrap point -- it is no
8652 longer relevant, but we won't
8653 have an opportunity to update it,
8654 since we've reached the edge of
8655 this screen line. */
8656 || (may_wrap
8657 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8659 it->hpos = hpos_before_this_char;
8660 it->current_x = x_before_this_char;
8661 result = MOVE_POS_MATCH_OR_ZV;
8662 break;
8664 if (it->line_wrap == WORD_WRAP
8665 && atpos_it.sp < 0)
8667 SAVE_IT (atpos_it, *it, atpos_data);
8668 atpos_it.current_x = x_before_this_char;
8669 atpos_it.hpos = hpos_before_this_char;
8673 prev_method = it->method;
8674 if (it->method == GET_FROM_BUFFER)
8675 prev_pos = IT_CHARPOS (*it);
8676 set_iterator_to_next (it, true);
8677 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8678 SET_TEXT_POS (this_line_min_pos,
8679 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8680 /* On graphical terminals, newlines may
8681 "overflow" into the fringe if
8682 overflow-newline-into-fringe is non-nil.
8683 On text terminals, and on graphical
8684 terminals with no right margin, newlines
8685 may overflow into the last glyph on the
8686 display line.*/
8687 if (!FRAME_WINDOW_P (it->f)
8688 || ((it->bidi_p
8689 && it->bidi_it.paragraph_dir == R2L)
8690 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8691 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8692 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8694 if (!get_next_display_element (it))
8696 result = MOVE_POS_MATCH_OR_ZV;
8697 break;
8699 if (BUFFER_POS_REACHED_P ())
8701 if (ITERATOR_AT_END_OF_LINE_P (it))
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 else
8704 result = MOVE_LINE_CONTINUED;
8705 break;
8707 if (ITERATOR_AT_END_OF_LINE_P (it)
8708 && (it->line_wrap != WORD_WRAP
8709 || wrap_it.sp < 0
8710 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8712 result = MOVE_NEWLINE_OR_CR;
8713 break;
8718 else
8719 IT_RESET_X_ASCENT_DESCENT (it);
8721 /* If the screen line ends with whitespace, and we
8722 are under word-wrap, don't use wrap_it: it is no
8723 longer relevant, but we won't have an opportunity
8724 to update it, since we are done with this screen
8725 line. */
8726 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8728 /* If we've found TO_X, go back there, as we now
8729 know the last word fits on this screen line. */
8730 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8731 && atx_it.sp >= 0)
8733 RESTORE_IT (it, &atx_it, atx_data);
8734 atpos_it.sp = -1;
8735 atx_it.sp = -1;
8736 result = MOVE_X_REACHED;
8737 break;
8740 else if (wrap_it.sp >= 0)
8742 RESTORE_IT (it, &wrap_it, wrap_data);
8743 atpos_it.sp = -1;
8744 atx_it.sp = -1;
8747 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8748 IT_CHARPOS (*it)));
8749 result = MOVE_LINE_CONTINUED;
8750 break;
8753 if (BUFFER_POS_REACHED_P ())
8755 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8756 goto buffer_pos_reached;
8757 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8759 SAVE_IT (atpos_it, *it, atpos_data);
8760 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8764 if (new_x > it->first_visible_x)
8766 /* Glyph is visible. Increment number of glyphs that
8767 would be displayed. */
8768 ++it->hpos;
8772 if (result != MOVE_UNDEFINED)
8773 break;
8775 else if (BUFFER_POS_REACHED_P ())
8777 buffer_pos_reached:
8778 IT_RESET_X_ASCENT_DESCENT (it);
8779 result = MOVE_POS_MATCH_OR_ZV;
8780 break;
8782 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8784 /* Stop when TO_X specified and reached. This check is
8785 necessary here because of lines consisting of a line end,
8786 only. The line end will not produce any glyphs and we
8787 would never get MOVE_X_REACHED. */
8788 eassert (it->nglyphs == 0);
8789 result = MOVE_X_REACHED;
8790 break;
8793 /* Is this a line end? If yes, we're done. */
8794 if (ITERATOR_AT_END_OF_LINE_P (it))
8796 /* If we are past TO_CHARPOS, but never saw any character
8797 positions smaller than TO_CHARPOS, return
8798 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8799 did. */
8800 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8802 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8804 if (closest_pos < ZV)
8806 RESTORE_IT (it, &ppos_it, ppos_data);
8807 /* Don't recurse if closest_pos is equal to
8808 to_charpos, since we have just tried that. */
8809 if (closest_pos != to_charpos)
8810 move_it_in_display_line_to (it, closest_pos, -1,
8811 MOVE_TO_POS);
8812 result = MOVE_POS_MATCH_OR_ZV;
8814 else
8815 goto buffer_pos_reached;
8817 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8818 && IT_CHARPOS (*it) > to_charpos)
8819 goto buffer_pos_reached;
8820 else
8821 result = MOVE_NEWLINE_OR_CR;
8823 else
8824 result = MOVE_NEWLINE_OR_CR;
8825 break;
8828 prev_method = it->method;
8829 if (it->method == GET_FROM_BUFFER)
8830 prev_pos = IT_CHARPOS (*it);
8831 /* The current display element has been consumed. Advance
8832 to the next. */
8833 set_iterator_to_next (it, true);
8834 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8835 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8836 if (IT_CHARPOS (*it) < to_charpos)
8837 saw_smaller_pos = true;
8838 if (it->bidi_p
8839 && (op & MOVE_TO_POS)
8840 && IT_CHARPOS (*it) >= to_charpos
8841 && IT_CHARPOS (*it) < closest_pos)
8842 closest_pos = IT_CHARPOS (*it);
8844 /* Stop if lines are truncated and IT's current x-position is
8845 past the right edge of the window now. */
8846 if (it->line_wrap == TRUNCATE
8847 && it->current_x >= it->last_visible_x)
8849 if (!FRAME_WINDOW_P (it->f)
8850 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8851 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8852 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8853 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8855 bool at_eob_p = false;
8857 if ((at_eob_p = !get_next_display_element (it))
8858 || BUFFER_POS_REACHED_P ()
8859 /* If we are past TO_CHARPOS, but never saw any
8860 character positions smaller than TO_CHARPOS,
8861 return MOVE_POS_MATCH_OR_ZV, like the
8862 unidirectional display did. */
8863 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8864 && !saw_smaller_pos
8865 && IT_CHARPOS (*it) > to_charpos))
8867 if (it->bidi_p
8868 && !BUFFER_POS_REACHED_P ()
8869 && !at_eob_p && closest_pos < ZV)
8871 RESTORE_IT (it, &ppos_it, ppos_data);
8872 if (closest_pos != to_charpos)
8873 move_it_in_display_line_to (it, closest_pos, -1,
8874 MOVE_TO_POS);
8876 result = MOVE_POS_MATCH_OR_ZV;
8877 break;
8879 if (ITERATOR_AT_END_OF_LINE_P (it))
8881 result = MOVE_NEWLINE_OR_CR;
8882 break;
8885 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8886 && !saw_smaller_pos
8887 && IT_CHARPOS (*it) > to_charpos)
8889 if (closest_pos < ZV)
8891 RESTORE_IT (it, &ppos_it, ppos_data);
8892 if (closest_pos != to_charpos)
8893 move_it_in_display_line_to (it, closest_pos, -1,
8894 MOVE_TO_POS);
8896 result = MOVE_POS_MATCH_OR_ZV;
8897 break;
8899 result = MOVE_LINE_TRUNCATED;
8900 break;
8902 #undef IT_RESET_X_ASCENT_DESCENT
8905 #undef BUFFER_POS_REACHED_P
8907 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8908 restore the saved iterator. */
8909 if (atpos_it.sp >= 0)
8910 RESTORE_IT (it, &atpos_it, atpos_data);
8911 else if (atx_it.sp >= 0)
8912 RESTORE_IT (it, &atx_it, atx_data);
8914 done:
8916 if (atpos_data)
8917 bidi_unshelve_cache (atpos_data, true);
8918 if (atx_data)
8919 bidi_unshelve_cache (atx_data, true);
8920 if (wrap_data)
8921 bidi_unshelve_cache (wrap_data, true);
8922 if (ppos_data)
8923 bidi_unshelve_cache (ppos_data, true);
8925 /* Restore the iterator settings altered at the beginning of this
8926 function. */
8927 it->glyph_row = saved_glyph_row;
8928 return result;
8931 /* For external use. */
8932 void
8933 move_it_in_display_line (struct it *it,
8934 ptrdiff_t to_charpos, int to_x,
8935 enum move_operation_enum op)
8937 if (it->line_wrap == WORD_WRAP
8938 && (op & MOVE_TO_X))
8940 struct it save_it;
8941 void *save_data = NULL;
8942 int skip;
8944 SAVE_IT (save_it, *it, save_data);
8945 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8946 /* When word-wrap is on, TO_X may lie past the end
8947 of a wrapped line. Then it->current is the
8948 character on the next line, so backtrack to the
8949 space before the wrap point. */
8950 if (skip == MOVE_LINE_CONTINUED)
8952 int prev_x = max (it->current_x - 1, 0);
8953 RESTORE_IT (it, &save_it, save_data);
8954 move_it_in_display_line_to
8955 (it, -1, prev_x, MOVE_TO_X);
8957 else
8958 bidi_unshelve_cache (save_data, true);
8960 else
8961 move_it_in_display_line_to (it, to_charpos, to_x, op);
8965 /* Move IT forward until it satisfies one or more of the criteria in
8966 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8968 OP is a bit-mask that specifies where to stop, and in particular,
8969 which of those four position arguments makes a difference. See the
8970 description of enum move_operation_enum.
8972 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8973 screen line, this function will set IT to the next position that is
8974 displayed to the right of TO_CHARPOS on the screen.
8976 Return the maximum pixel length of any line scanned but never more
8977 than it.last_visible_x. */
8980 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8982 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8983 int line_height, line_start_x = 0, reached = 0;
8984 int max_current_x = 0;
8985 void *backup_data = NULL;
8987 for (;;)
8989 if (op & MOVE_TO_VPOS)
8991 /* If no TO_CHARPOS and no TO_X specified, stop at the
8992 start of the line TO_VPOS. */
8993 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8995 if (it->vpos == to_vpos)
8997 reached = 1;
8998 break;
9000 else
9001 skip = move_it_in_display_line_to (it, -1, -1, 0);
9003 else
9005 /* TO_VPOS >= 0 means stop at TO_X in the line at
9006 TO_VPOS, or at TO_POS, whichever comes first. */
9007 if (it->vpos == to_vpos)
9009 reached = 2;
9010 break;
9013 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9015 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9017 reached = 3;
9018 break;
9020 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9022 /* We have reached TO_X but not in the line we want. */
9023 skip = move_it_in_display_line_to (it, to_charpos,
9024 -1, MOVE_TO_POS);
9025 if (skip == MOVE_POS_MATCH_OR_ZV)
9027 reached = 4;
9028 break;
9033 else if (op & MOVE_TO_Y)
9035 struct it it_backup;
9037 if (it->line_wrap == WORD_WRAP)
9038 SAVE_IT (it_backup, *it, backup_data);
9040 /* TO_Y specified means stop at TO_X in the line containing
9041 TO_Y---or at TO_CHARPOS if this is reached first. The
9042 problem is that we can't really tell whether the line
9043 contains TO_Y before we have completely scanned it, and
9044 this may skip past TO_X. What we do is to first scan to
9045 TO_X.
9047 If TO_X is not specified, use a TO_X of zero. The reason
9048 is to make the outcome of this function more predictable.
9049 If we didn't use TO_X == 0, we would stop at the end of
9050 the line which is probably not what a caller would expect
9051 to happen. */
9052 skip = move_it_in_display_line_to
9053 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9054 (MOVE_TO_X | (op & MOVE_TO_POS)));
9056 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9057 if (skip == MOVE_POS_MATCH_OR_ZV)
9058 reached = 5;
9059 else if (skip == MOVE_X_REACHED)
9061 /* If TO_X was reached, we want to know whether TO_Y is
9062 in the line. We know this is the case if the already
9063 scanned glyphs make the line tall enough. Otherwise,
9064 we must check by scanning the rest of the line. */
9065 line_height = it->max_ascent + it->max_descent;
9066 if (to_y >= it->current_y
9067 && to_y < it->current_y + line_height)
9069 reached = 6;
9070 break;
9072 SAVE_IT (it_backup, *it, backup_data);
9073 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9074 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9075 op & MOVE_TO_POS);
9076 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9077 line_height = it->max_ascent + it->max_descent;
9078 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9080 if (to_y >= it->current_y
9081 && to_y < it->current_y + line_height)
9083 /* If TO_Y is in this line and TO_X was reached
9084 above, we scanned too far. We have to restore
9085 IT's settings to the ones before skipping. But
9086 keep the more accurate values of max_ascent and
9087 max_descent we've found while skipping the rest
9088 of the line, for the sake of callers, such as
9089 pos_visible_p, that need to know the line
9090 height. */
9091 int max_ascent = it->max_ascent;
9092 int max_descent = it->max_descent;
9094 RESTORE_IT (it, &it_backup, backup_data);
9095 it->max_ascent = max_ascent;
9096 it->max_descent = max_descent;
9097 reached = 6;
9099 else
9101 skip = skip2;
9102 if (skip == MOVE_POS_MATCH_OR_ZV)
9103 reached = 7;
9106 else
9108 /* Check whether TO_Y is in this line. */
9109 line_height = it->max_ascent + it->max_descent;
9110 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9112 if (to_y >= it->current_y
9113 && to_y < it->current_y + line_height)
9115 if (to_y > it->current_y)
9116 max_current_x = max (it->current_x, max_current_x);
9118 /* When word-wrap is on, TO_X may lie past the end
9119 of a wrapped line. Then it->current is the
9120 character on the next line, so backtrack to the
9121 space before the wrap point. */
9122 if (skip == MOVE_LINE_CONTINUED
9123 && it->line_wrap == WORD_WRAP)
9125 int prev_x = max (it->current_x - 1, 0);
9126 RESTORE_IT (it, &it_backup, backup_data);
9127 skip = move_it_in_display_line_to
9128 (it, -1, prev_x, MOVE_TO_X);
9131 reached = 6;
9135 if (reached)
9137 max_current_x = max (it->current_x, max_current_x);
9138 break;
9141 else if (BUFFERP (it->object)
9142 && (it->method == GET_FROM_BUFFER
9143 || it->method == GET_FROM_STRETCH)
9144 && IT_CHARPOS (*it) >= to_charpos
9145 /* Under bidi iteration, a call to set_iterator_to_next
9146 can scan far beyond to_charpos if the initial
9147 portion of the next line needs to be reordered. In
9148 that case, give move_it_in_display_line_to another
9149 chance below. */
9150 && !(it->bidi_p
9151 && it->bidi_it.scan_dir == -1))
9152 skip = MOVE_POS_MATCH_OR_ZV;
9153 else
9154 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9156 switch (skip)
9158 case MOVE_POS_MATCH_OR_ZV:
9159 max_current_x = max (it->current_x, max_current_x);
9160 reached = 8;
9161 goto out;
9163 case MOVE_NEWLINE_OR_CR:
9164 max_current_x = max (it->current_x, max_current_x);
9165 set_iterator_to_next (it, true);
9166 it->continuation_lines_width = 0;
9167 break;
9169 case MOVE_LINE_TRUNCATED:
9170 max_current_x = it->last_visible_x;
9171 it->continuation_lines_width = 0;
9172 reseat_at_next_visible_line_start (it, false);
9173 if ((op & MOVE_TO_POS) != 0
9174 && IT_CHARPOS (*it) > to_charpos)
9176 reached = 9;
9177 goto out;
9179 break;
9181 case MOVE_LINE_CONTINUED:
9182 max_current_x = it->last_visible_x;
9183 /* For continued lines ending in a tab, some of the glyphs
9184 associated with the tab are displayed on the current
9185 line. Since it->current_x does not include these glyphs,
9186 we use it->last_visible_x instead. */
9187 if (it->c == '\t')
9189 it->continuation_lines_width += it->last_visible_x;
9190 /* When moving by vpos, ensure that the iterator really
9191 advances to the next line (bug#847, bug#969). Fixme:
9192 do we need to do this in other circumstances? */
9193 if (it->current_x != it->last_visible_x
9194 && (op & MOVE_TO_VPOS)
9195 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9197 line_start_x = it->current_x + it->pixel_width
9198 - it->last_visible_x;
9199 if (FRAME_WINDOW_P (it->f))
9201 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9202 struct font *face_font = face->font;
9204 /* When display_line produces a continued line
9205 that ends in a TAB, it skips a tab stop that
9206 is closer than the font's space character
9207 width (see x_produce_glyphs where it produces
9208 the stretch glyph which represents a TAB).
9209 We need to reproduce the same logic here. */
9210 eassert (face_font);
9211 if (face_font)
9213 if (line_start_x < face_font->space_width)
9214 line_start_x
9215 += it->tab_width * face_font->space_width;
9218 set_iterator_to_next (it, false);
9221 else
9222 it->continuation_lines_width += it->current_x;
9223 break;
9225 default:
9226 emacs_abort ();
9229 /* Reset/increment for the next run. */
9230 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9231 it->current_x = line_start_x;
9232 line_start_x = 0;
9233 it->hpos = 0;
9234 it->current_y += it->max_ascent + it->max_descent;
9235 ++it->vpos;
9236 last_height = it->max_ascent + it->max_descent;
9237 it->max_ascent = it->max_descent = 0;
9240 out:
9242 /* On text terminals, we may stop at the end of a line in the middle
9243 of a multi-character glyph. If the glyph itself is continued,
9244 i.e. it is actually displayed on the next line, don't treat this
9245 stopping point as valid; move to the next line instead (unless
9246 that brings us offscreen). */
9247 if (!FRAME_WINDOW_P (it->f)
9248 && op & MOVE_TO_POS
9249 && IT_CHARPOS (*it) == to_charpos
9250 && it->what == IT_CHARACTER
9251 && it->nglyphs > 1
9252 && it->line_wrap == WINDOW_WRAP
9253 && it->current_x == it->last_visible_x - 1
9254 && it->c != '\n'
9255 && it->c != '\t'
9256 && it->w->window_end_valid
9257 && it->vpos < it->w->window_end_vpos)
9259 it->continuation_lines_width += it->current_x;
9260 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9261 it->current_y += it->max_ascent + it->max_descent;
9262 ++it->vpos;
9263 last_height = it->max_ascent + it->max_descent;
9266 if (backup_data)
9267 bidi_unshelve_cache (backup_data, true);
9269 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9271 return max_current_x;
9275 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9277 If DY > 0, move IT backward at least that many pixels. DY = 0
9278 means move IT backward to the preceding line start or BEGV. This
9279 function may move over more than DY pixels if IT->current_y - DY
9280 ends up in the middle of a line; in this case IT->current_y will be
9281 set to the top of the line moved to. */
9283 void
9284 move_it_vertically_backward (struct it *it, int dy)
9286 int nlines, h;
9287 struct it it2, it3;
9288 void *it2data = NULL, *it3data = NULL;
9289 ptrdiff_t start_pos;
9290 int nchars_per_row
9291 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9292 ptrdiff_t pos_limit;
9294 move_further_back:
9295 eassert (dy >= 0);
9297 start_pos = IT_CHARPOS (*it);
9299 /* Estimate how many newlines we must move back. */
9300 nlines = max (1, dy / default_line_pixel_height (it->w));
9301 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9302 pos_limit = BEGV;
9303 else
9304 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9306 /* Set the iterator's position that many lines back. But don't go
9307 back more than NLINES full screen lines -- this wins a day with
9308 buffers which have very long lines. */
9309 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9310 back_to_previous_visible_line_start (it);
9312 /* Reseat the iterator here. When moving backward, we don't want
9313 reseat to skip forward over invisible text, set up the iterator
9314 to deliver from overlay strings at the new position etc. So,
9315 use reseat_1 here. */
9316 reseat_1 (it, it->current.pos, true);
9318 /* We are now surely at a line start. */
9319 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9320 reordering is in effect. */
9321 it->continuation_lines_width = 0;
9323 /* Move forward and see what y-distance we moved. First move to the
9324 start of the next line so that we get its height. We need this
9325 height to be able to tell whether we reached the specified
9326 y-distance. */
9327 SAVE_IT (it2, *it, it2data);
9328 it2.max_ascent = it2.max_descent = 0;
9331 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9332 MOVE_TO_POS | MOVE_TO_VPOS);
9334 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9335 /* If we are in a display string which starts at START_POS,
9336 and that display string includes a newline, and we are
9337 right after that newline (i.e. at the beginning of a
9338 display line), exit the loop, because otherwise we will
9339 infloop, since move_it_to will see that it is already at
9340 START_POS and will not move. */
9341 || (it2.method == GET_FROM_STRING
9342 && IT_CHARPOS (it2) == start_pos
9343 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9344 eassert (IT_CHARPOS (*it) >= BEGV);
9345 SAVE_IT (it3, it2, it3data);
9347 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9348 eassert (IT_CHARPOS (*it) >= BEGV);
9349 /* H is the actual vertical distance from the position in *IT
9350 and the starting position. */
9351 h = it2.current_y - it->current_y;
9352 /* NLINES is the distance in number of lines. */
9353 nlines = it2.vpos - it->vpos;
9355 /* Correct IT's y and vpos position
9356 so that they are relative to the starting point. */
9357 it->vpos -= nlines;
9358 it->current_y -= h;
9360 if (dy == 0)
9362 /* DY == 0 means move to the start of the screen line. The
9363 value of nlines is > 0 if continuation lines were involved,
9364 or if the original IT position was at start of a line. */
9365 RESTORE_IT (it, it, it2data);
9366 if (nlines > 0)
9367 move_it_by_lines (it, nlines);
9368 /* The above code moves us to some position NLINES down,
9369 usually to its first glyph (leftmost in an L2R line), but
9370 that's not necessarily the start of the line, under bidi
9371 reordering. We want to get to the character position
9372 that is immediately after the newline of the previous
9373 line. */
9374 if (it->bidi_p
9375 && !it->continuation_lines_width
9376 && !STRINGP (it->string)
9377 && IT_CHARPOS (*it) > BEGV
9378 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9380 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9382 DEC_BOTH (cp, bp);
9383 cp = find_newline_no_quit (cp, bp, -1, NULL);
9384 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9386 bidi_unshelve_cache (it3data, true);
9388 else
9390 /* The y-position we try to reach, relative to *IT.
9391 Note that H has been subtracted in front of the if-statement. */
9392 int target_y = it->current_y + h - dy;
9393 int y0 = it3.current_y;
9394 int y1;
9395 int line_height;
9397 RESTORE_IT (&it3, &it3, it3data);
9398 y1 = line_bottom_y (&it3);
9399 line_height = y1 - y0;
9400 RESTORE_IT (it, it, it2data);
9401 /* If we did not reach target_y, try to move further backward if
9402 we can. If we moved too far backward, try to move forward. */
9403 if (target_y < it->current_y
9404 /* This is heuristic. In a window that's 3 lines high, with
9405 a line height of 13 pixels each, recentering with point
9406 on the bottom line will try to move -39/2 = 19 pixels
9407 backward. Try to avoid moving into the first line. */
9408 && (it->current_y - target_y
9409 > min (window_box_height (it->w), line_height * 2 / 3))
9410 && IT_CHARPOS (*it) > BEGV)
9412 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9413 target_y - it->current_y));
9414 dy = it->current_y - target_y;
9415 goto move_further_back;
9417 else if (target_y >= it->current_y + line_height
9418 && IT_CHARPOS (*it) < ZV)
9420 /* Should move forward by at least one line, maybe more.
9422 Note: Calling move_it_by_lines can be expensive on
9423 terminal frames, where compute_motion is used (via
9424 vmotion) to do the job, when there are very long lines
9425 and truncate-lines is nil. That's the reason for
9426 treating terminal frames specially here. */
9428 if (!FRAME_WINDOW_P (it->f))
9429 move_it_vertically (it, target_y - (it->current_y + line_height));
9430 else
9434 move_it_by_lines (it, 1);
9436 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9443 /* Move IT by a specified amount of pixel lines DY. DY negative means
9444 move backwards. DY = 0 means move to start of screen line. At the
9445 end, IT will be on the start of a screen line. */
9447 void
9448 move_it_vertically (struct it *it, int dy)
9450 if (dy <= 0)
9451 move_it_vertically_backward (it, -dy);
9452 else
9454 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9455 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9456 MOVE_TO_POS | MOVE_TO_Y);
9457 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9459 /* If buffer ends in ZV without a newline, move to the start of
9460 the line to satisfy the post-condition. */
9461 if (IT_CHARPOS (*it) == ZV
9462 && ZV > BEGV
9463 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9464 move_it_by_lines (it, 0);
9469 /* Move iterator IT past the end of the text line it is in. */
9471 void
9472 move_it_past_eol (struct it *it)
9474 enum move_it_result rc;
9476 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9477 if (rc == MOVE_NEWLINE_OR_CR)
9478 set_iterator_to_next (it, false);
9482 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9483 negative means move up. DVPOS == 0 means move to the start of the
9484 screen line.
9486 Optimization idea: If we would know that IT->f doesn't use
9487 a face with proportional font, we could be faster for
9488 truncate-lines nil. */
9490 void
9491 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9494 /* The commented-out optimization uses vmotion on terminals. This
9495 gives bad results, because elements like it->what, on which
9496 callers such as pos_visible_p rely, aren't updated. */
9497 /* struct position pos;
9498 if (!FRAME_WINDOW_P (it->f))
9500 struct text_pos textpos;
9502 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9503 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9504 reseat (it, textpos, true);
9505 it->vpos += pos.vpos;
9506 it->current_y += pos.vpos;
9508 else */
9510 if (dvpos == 0)
9512 /* DVPOS == 0 means move to the start of the screen line. */
9513 move_it_vertically_backward (it, 0);
9514 /* Let next call to line_bottom_y calculate real line height. */
9515 last_height = 0;
9517 else if (dvpos > 0)
9519 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9520 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9522 /* Only move to the next buffer position if we ended up in a
9523 string from display property, not in an overlay string
9524 (before-string or after-string). That is because the
9525 latter don't conceal the underlying buffer position, so
9526 we can ask to move the iterator to the exact position we
9527 are interested in. Note that, even if we are already at
9528 IT_CHARPOS (*it), the call below is not a no-op, as it
9529 will detect that we are at the end of the string, pop the
9530 iterator, and compute it->current_x and it->hpos
9531 correctly. */
9532 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9533 -1, -1, -1, MOVE_TO_POS);
9536 else
9538 struct it it2;
9539 void *it2data = NULL;
9540 ptrdiff_t start_charpos, i;
9541 int nchars_per_row
9542 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9543 bool hit_pos_limit = false;
9544 ptrdiff_t pos_limit;
9546 /* Start at the beginning of the screen line containing IT's
9547 position. This may actually move vertically backwards,
9548 in case of overlays, so adjust dvpos accordingly. */
9549 dvpos += it->vpos;
9550 move_it_vertically_backward (it, 0);
9551 dvpos -= it->vpos;
9553 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9554 screen lines, and reseat the iterator there. */
9555 start_charpos = IT_CHARPOS (*it);
9556 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9557 pos_limit = BEGV;
9558 else
9559 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9561 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9562 back_to_previous_visible_line_start (it);
9563 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9564 hit_pos_limit = true;
9565 reseat (it, it->current.pos, true);
9567 /* Move further back if we end up in a string or an image. */
9568 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9570 /* First try to move to start of display line. */
9571 dvpos += it->vpos;
9572 move_it_vertically_backward (it, 0);
9573 dvpos -= it->vpos;
9574 if (IT_POS_VALID_AFTER_MOVE_P (it))
9575 break;
9576 /* If start of line is still in string or image,
9577 move further back. */
9578 back_to_previous_visible_line_start (it);
9579 reseat (it, it->current.pos, true);
9580 dvpos--;
9583 it->current_x = it->hpos = 0;
9585 /* Above call may have moved too far if continuation lines
9586 are involved. Scan forward and see if it did. */
9587 SAVE_IT (it2, *it, it2data);
9588 it2.vpos = it2.current_y = 0;
9589 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9590 it->vpos -= it2.vpos;
9591 it->current_y -= it2.current_y;
9592 it->current_x = it->hpos = 0;
9594 /* If we moved too far back, move IT some lines forward. */
9595 if (it2.vpos > -dvpos)
9597 int delta = it2.vpos + dvpos;
9599 RESTORE_IT (&it2, &it2, it2data);
9600 SAVE_IT (it2, *it, it2data);
9601 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9602 /* Move back again if we got too far ahead. */
9603 if (IT_CHARPOS (*it) >= start_charpos)
9604 RESTORE_IT (it, &it2, it2data);
9605 else
9606 bidi_unshelve_cache (it2data, true);
9608 else if (hit_pos_limit && pos_limit > BEGV
9609 && dvpos < 0 && it2.vpos < -dvpos)
9611 /* If we hit the limit, but still didn't make it far enough
9612 back, that means there's a display string with a newline
9613 covering a large chunk of text, and that caused
9614 back_to_previous_visible_line_start try to go too far.
9615 Punish those who commit such atrocities by going back
9616 until we've reached DVPOS, after lifting the limit, which
9617 could make it slow for very long lines. "If it hurts,
9618 don't do that!" */
9619 dvpos += it2.vpos;
9620 RESTORE_IT (it, it, it2data);
9621 for (i = -dvpos; i > 0; --i)
9623 back_to_previous_visible_line_start (it);
9624 it->vpos--;
9626 reseat_1 (it, it->current.pos, true);
9628 else
9629 RESTORE_IT (it, it, it2data);
9633 /* Return true if IT points into the middle of a display vector. */
9635 bool
9636 in_display_vector_p (struct it *it)
9638 return (it->method == GET_FROM_DISPLAY_VECTOR
9639 && it->current.dpvec_index > 0
9640 && it->dpvec + it->current.dpvec_index != it->dpend);
9643 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9644 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9645 WINDOW must be a live window and defaults to the selected one. The
9646 return value is a cons of the maximum pixel-width of any text line and
9647 the maximum pixel-height of all text lines.
9649 The optional argument FROM, if non-nil, specifies the first text
9650 position and defaults to the minimum accessible position of the buffer.
9651 If FROM is t, use the minimum accessible position that is not a newline
9652 character. TO, if non-nil, specifies the last text position and
9653 defaults to the maximum accessible position of the buffer. If TO is t,
9654 use the maximum accessible position that is not a newline character.
9656 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9657 width that can be returned. X-LIMIT nil or omitted, means to use the
9658 pixel-width of WINDOW's body; use this if you do not intend to change
9659 the width of WINDOW. Use the maximum width WINDOW may assume if you
9660 intend to change WINDOW's width. In any case, text whose x-coordinate
9661 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9662 can take some time, it's always a good idea to make this argument as
9663 small as possible; in particular, if the buffer contains long lines that
9664 shall be truncated anyway.
9666 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9667 height that can be returned. Text lines whose y-coordinate is beyond
9668 Y-LIMIT are ignored. Since calculating the text height of a large
9669 buffer can take some time, it makes sense to specify this argument if
9670 the size of the buffer is unknown.
9672 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9673 include the height of the mode- or header-line of WINDOW in the return
9674 value. If it is either the symbol `mode-line' or `header-line', include
9675 only the height of that line, if present, in the return value. If t,
9676 include the height of both, if present, in the return value. */)
9677 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9678 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9680 struct window *w = decode_live_window (window);
9681 Lisp_Object buffer = w->contents;
9682 struct buffer *b;
9683 struct it it;
9684 struct buffer *old_b = NULL;
9685 ptrdiff_t start, end, pos;
9686 struct text_pos startp;
9687 void *itdata = NULL;
9688 int c, max_y = -1, x = 0, y = 0;
9690 CHECK_BUFFER (buffer);
9691 b = XBUFFER (buffer);
9693 if (b != current_buffer)
9695 old_b = current_buffer;
9696 set_buffer_internal (b);
9699 if (NILP (from))
9700 start = BEGV;
9701 else if (EQ (from, Qt))
9703 start = pos = BEGV;
9704 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9705 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9706 start = pos;
9707 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9708 start = pos;
9710 else
9712 CHECK_NUMBER_COERCE_MARKER (from);
9713 start = min (max (XINT (from), BEGV), ZV);
9716 if (NILP (to))
9717 end = ZV;
9718 else if (EQ (to, Qt))
9720 end = pos = ZV;
9721 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9722 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9723 end = pos;
9724 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9725 end = pos;
9727 else
9729 CHECK_NUMBER_COERCE_MARKER (to);
9730 end = max (start, min (XINT (to), ZV));
9733 if (!NILP (y_limit))
9735 CHECK_NUMBER (y_limit);
9736 max_y = min (XINT (y_limit), INT_MAX);
9739 itdata = bidi_shelve_cache ();
9740 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9741 start_display (&it, w, startp);
9743 if (NILP (x_limit))
9744 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9745 else
9747 CHECK_NUMBER (x_limit);
9748 it.last_visible_x = min (XINT (x_limit), INFINITY);
9749 /* Actually, we never want move_it_to stop at to_x. But to make
9750 sure that move_it_in_display_line_to always moves far enough,
9751 we set it to INT_MAX and specify MOVE_TO_X. */
9752 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9753 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9756 y = it.current_y + it.max_ascent + it.max_descent;
9758 if (!EQ (mode_and_header_line, Qheader_line)
9759 && !EQ (mode_and_header_line, Qt))
9760 /* Do not count the header-line which was counted automatically by
9761 start_display. */
9762 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9764 if (EQ (mode_and_header_line, Qmode_line)
9765 || EQ (mode_and_header_line, Qt))
9766 /* Do count the mode-line which is not included automatically by
9767 start_display. */
9768 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9770 bidi_unshelve_cache (itdata, false);
9772 if (old_b)
9773 set_buffer_internal (old_b);
9775 return Fcons (make_number (x), make_number (y));
9778 /***********************************************************************
9779 Messages
9780 ***********************************************************************/
9783 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9784 to *Messages*. */
9786 void
9787 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9789 Lisp_Object msg, fmt;
9790 char *buffer;
9791 ptrdiff_t len;
9792 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9793 USE_SAFE_ALLOCA;
9795 fmt = msg = Qnil;
9796 GCPRO4 (fmt, msg, arg1, arg2);
9798 fmt = build_string (format);
9799 msg = CALLN (Fformat, fmt, arg1, arg2);
9801 len = SBYTES (msg) + 1;
9802 buffer = SAFE_ALLOCA (len);
9803 memcpy (buffer, SDATA (msg), len);
9805 message_dolog (buffer, len - 1, true, false);
9806 SAFE_FREE ();
9808 UNGCPRO;
9812 /* Output a newline in the *Messages* buffer if "needs" one. */
9814 void
9815 message_log_maybe_newline (void)
9817 if (message_log_need_newline)
9818 message_dolog ("", 0, true, false);
9822 /* Add a string M of length NBYTES to the message log, optionally
9823 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9824 true, means interpret the contents of M as multibyte. This
9825 function calls low-level routines in order to bypass text property
9826 hooks, etc. which might not be safe to run.
9828 This may GC (insert may run before/after change hooks),
9829 so the buffer M must NOT point to a Lisp string. */
9831 void
9832 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9834 const unsigned char *msg = (const unsigned char *) m;
9836 if (!NILP (Vmemory_full))
9837 return;
9839 if (!NILP (Vmessage_log_max))
9841 struct buffer *oldbuf;
9842 Lisp_Object oldpoint, oldbegv, oldzv;
9843 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9844 ptrdiff_t point_at_end = 0;
9845 ptrdiff_t zv_at_end = 0;
9846 Lisp_Object old_deactivate_mark;
9847 struct gcpro gcpro1;
9849 old_deactivate_mark = Vdeactivate_mark;
9850 oldbuf = current_buffer;
9852 /* Ensure the Messages buffer exists, and switch to it.
9853 If we created it, set the major-mode. */
9854 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9855 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9856 if (newbuffer
9857 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9858 call0 (intern ("messages-buffer-mode"));
9860 bset_undo_list (current_buffer, Qt);
9861 bset_cache_long_scans (current_buffer, Qnil);
9863 oldpoint = message_dolog_marker1;
9864 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9865 oldbegv = message_dolog_marker2;
9866 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9867 oldzv = message_dolog_marker3;
9868 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9869 GCPRO1 (old_deactivate_mark);
9871 if (PT == Z)
9872 point_at_end = 1;
9873 if (ZV == Z)
9874 zv_at_end = 1;
9876 BEGV = BEG;
9877 BEGV_BYTE = BEG_BYTE;
9878 ZV = Z;
9879 ZV_BYTE = Z_BYTE;
9880 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9882 /* Insert the string--maybe converting multibyte to single byte
9883 or vice versa, so that all the text fits the buffer. */
9884 if (multibyte
9885 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9887 ptrdiff_t i;
9888 int c, char_bytes;
9889 char work[1];
9891 /* Convert a multibyte string to single-byte
9892 for the *Message* buffer. */
9893 for (i = 0; i < nbytes; i += char_bytes)
9895 c = string_char_and_length (msg + i, &char_bytes);
9896 work[0] = CHAR_TO_BYTE8 (c);
9897 insert_1_both (work, 1, 1, true, false, false);
9900 else if (! multibyte
9901 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9903 ptrdiff_t i;
9904 int c, char_bytes;
9905 unsigned char str[MAX_MULTIBYTE_LENGTH];
9906 /* Convert a single-byte string to multibyte
9907 for the *Message* buffer. */
9908 for (i = 0; i < nbytes; i++)
9910 c = msg[i];
9911 MAKE_CHAR_MULTIBYTE (c);
9912 char_bytes = CHAR_STRING (c, str);
9913 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
9916 else if (nbytes)
9917 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
9918 true, false, false);
9920 if (nlflag)
9922 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9923 printmax_t dups;
9925 insert_1_both ("\n", 1, 1, true, false, false);
9927 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
9928 this_bol = PT;
9929 this_bol_byte = PT_BYTE;
9931 /* See if this line duplicates the previous one.
9932 If so, combine duplicates. */
9933 if (this_bol > BEG)
9935 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
9936 prev_bol = PT;
9937 prev_bol_byte = PT_BYTE;
9939 dups = message_log_check_duplicate (prev_bol_byte,
9940 this_bol_byte);
9941 if (dups)
9943 del_range_both (prev_bol, prev_bol_byte,
9944 this_bol, this_bol_byte, false);
9945 if (dups > 1)
9947 char dupstr[sizeof " [ times]"
9948 + INT_STRLEN_BOUND (printmax_t)];
9950 /* If you change this format, don't forget to also
9951 change message_log_check_duplicate. */
9952 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9953 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9954 insert_1_both (dupstr, duplen, duplen,
9955 true, false, true);
9960 /* If we have more than the desired maximum number of lines
9961 in the *Messages* buffer now, delete the oldest ones.
9962 This is safe because we don't have undo in this buffer. */
9964 if (NATNUMP (Vmessage_log_max))
9966 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9967 -XFASTINT (Vmessage_log_max) - 1, false);
9968 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
9971 BEGV = marker_position (oldbegv);
9972 BEGV_BYTE = marker_byte_position (oldbegv);
9974 if (zv_at_end)
9976 ZV = Z;
9977 ZV_BYTE = Z_BYTE;
9979 else
9981 ZV = marker_position (oldzv);
9982 ZV_BYTE = marker_byte_position (oldzv);
9985 if (point_at_end)
9986 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9987 else
9988 /* We can't do Fgoto_char (oldpoint) because it will run some
9989 Lisp code. */
9990 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9991 marker_byte_position (oldpoint));
9993 UNGCPRO;
9994 unchain_marker (XMARKER (oldpoint));
9995 unchain_marker (XMARKER (oldbegv));
9996 unchain_marker (XMARKER (oldzv));
9998 /* We called insert_1_both above with its 5th argument (PREPARE)
9999 false, which prevents insert_1_both from calling
10000 prepare_to_modify_buffer, which in turns prevents us from
10001 incrementing windows_or_buffers_changed even if *Messages* is
10002 shown in some window. So we must manually set
10003 windows_or_buffers_changed here to make up for that. */
10004 windows_or_buffers_changed = old_windows_or_buffers_changed;
10005 bset_redisplay (current_buffer);
10007 set_buffer_internal (oldbuf);
10009 message_log_need_newline = !nlflag;
10010 Vdeactivate_mark = old_deactivate_mark;
10015 /* We are at the end of the buffer after just having inserted a newline.
10016 (Note: We depend on the fact we won't be crossing the gap.)
10017 Check to see if the most recent message looks a lot like the previous one.
10018 Return 0 if different, 1 if the new one should just replace it, or a
10019 value N > 1 if we should also append " [N times]". */
10021 static intmax_t
10022 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10024 ptrdiff_t i;
10025 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10026 bool seen_dots = false;
10027 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10028 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10030 for (i = 0; i < len; i++)
10032 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10033 seen_dots = true;
10034 if (p1[i] != p2[i])
10035 return seen_dots;
10037 p1 += len;
10038 if (*p1 == '\n')
10039 return 2;
10040 if (*p1++ == ' ' && *p1++ == '[')
10042 char *pend;
10043 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10044 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10045 return n + 1;
10047 return 0;
10051 /* Display an echo area message M with a specified length of NBYTES
10052 bytes. The string may include null characters. If M is not a
10053 string, clear out any existing message, and let the mini-buffer
10054 text show through.
10056 This function cancels echoing. */
10058 void
10059 message3 (Lisp_Object m)
10061 struct gcpro gcpro1;
10063 GCPRO1 (m);
10064 clear_message (true, true);
10065 cancel_echoing ();
10067 /* First flush out any partial line written with print. */
10068 message_log_maybe_newline ();
10069 if (STRINGP (m))
10071 ptrdiff_t nbytes = SBYTES (m);
10072 bool multibyte = STRING_MULTIBYTE (m);
10073 char *buffer;
10074 USE_SAFE_ALLOCA;
10075 SAFE_ALLOCA_STRING (buffer, m);
10076 message_dolog (buffer, nbytes, true, multibyte);
10077 SAFE_FREE ();
10079 if (! inhibit_message)
10080 message3_nolog (m);
10081 UNGCPRO;
10085 /* The non-logging version of message3.
10086 This does not cancel echoing, because it is used for echoing.
10087 Perhaps we need to make a separate function for echoing
10088 and make this cancel echoing. */
10090 void
10091 message3_nolog (Lisp_Object m)
10093 struct frame *sf = SELECTED_FRAME ();
10095 if (FRAME_INITIAL_P (sf))
10097 if (noninteractive_need_newline)
10098 putc ('\n', stderr);
10099 noninteractive_need_newline = false;
10100 if (STRINGP (m))
10102 Lisp_Object s = ENCODE_SYSTEM (m);
10104 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10106 if (!cursor_in_echo_area)
10107 fprintf (stderr, "\n");
10108 fflush (stderr);
10110 /* Error messages get reported properly by cmd_error, so this must be just an
10111 informative message; if the frame hasn't really been initialized yet, just
10112 toss it. */
10113 else if (INTERACTIVE && sf->glyphs_initialized_p)
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 Lisp_Object frame = XWINDOW (mini_window)->frame;
10119 struct frame *f = XFRAME (frame);
10121 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10122 Fmake_frame_visible (frame);
10124 if (STRINGP (m) && SCHARS (m) > 0)
10126 set_message (m);
10127 if (minibuffer_auto_raise)
10128 Fraise_frame (frame);
10129 /* Assume we are not echoing.
10130 (If we are, echo_now will override this.) */
10131 echo_message_buffer = Qnil;
10133 else
10134 clear_message (true, true);
10136 do_pending_window_change (false);
10137 echo_area_display (true);
10138 do_pending_window_change (false);
10139 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10140 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10145 /* Display a null-terminated echo area message M. If M is 0, clear
10146 out any existing message, and let the mini-buffer text show through.
10148 The buffer M must continue to exist until after the echo area gets
10149 cleared or some other message gets displayed there. Do not pass
10150 text that is stored in a Lisp string. Do not pass text in a buffer
10151 that was alloca'd. */
10153 void
10154 message1 (const char *m)
10156 message3 (m ? build_unibyte_string (m) : Qnil);
10160 /* The non-logging counterpart of message1. */
10162 void
10163 message1_nolog (const char *m)
10165 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10168 /* Display a message M which contains a single %s
10169 which gets replaced with STRING. */
10171 void
10172 message_with_string (const char *m, Lisp_Object string, bool log)
10174 CHECK_STRING (string);
10176 if (noninteractive)
10178 if (m)
10180 /* ENCODE_SYSTEM below can GC and/or relocate the
10181 Lisp data, so make sure we don't use it here. */
10182 eassert (relocatable_string_data_p (m) != 1);
10184 if (noninteractive_need_newline)
10185 putc ('\n', stderr);
10186 noninteractive_need_newline = false;
10187 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10188 if (!cursor_in_echo_area)
10189 fprintf (stderr, "\n");
10190 fflush (stderr);
10193 else if (INTERACTIVE)
10195 /* The frame whose minibuffer we're going to display the message on.
10196 It may be larger than the selected frame, so we need
10197 to use its buffer, not the selected frame's buffer. */
10198 Lisp_Object mini_window;
10199 struct frame *f, *sf = SELECTED_FRAME ();
10201 /* Get the frame containing the minibuffer
10202 that the selected frame is using. */
10203 mini_window = FRAME_MINIBUF_WINDOW (sf);
10204 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10206 /* Error messages get reported properly by cmd_error, so this must be
10207 just an informative message; if the frame hasn't really been
10208 initialized yet, just toss it. */
10209 if (f->glyphs_initialized_p)
10211 struct gcpro gcpro1, gcpro2;
10213 Lisp_Object fmt = build_string (m);
10214 Lisp_Object msg = string;
10215 GCPRO2 (fmt, msg);
10217 msg = CALLN (Fformat, fmt, msg);
10219 if (log)
10220 message3 (msg);
10221 else
10222 message3_nolog (msg);
10224 UNGCPRO;
10226 /* Print should start at the beginning of the message
10227 buffer next time. */
10228 message_buf_print = false;
10234 /* Dump an informative message to the minibuf. If M is 0, clear out
10235 any existing message, and let the mini-buffer text show through. */
10237 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10238 vmessage (const char *m, va_list ap)
10240 if (noninteractive)
10242 if (m)
10244 if (noninteractive_need_newline)
10245 putc ('\n', stderr);
10246 noninteractive_need_newline = false;
10247 vfprintf (stderr, m, ap);
10248 if (!cursor_in_echo_area)
10249 fprintf (stderr, "\n");
10250 fflush (stderr);
10253 else if (INTERACTIVE)
10255 /* The frame whose mini-buffer we're going to display the message
10256 on. It may be larger than the selected frame, so we need to
10257 use its buffer, not the selected frame's buffer. */
10258 Lisp_Object mini_window;
10259 struct frame *f, *sf = SELECTED_FRAME ();
10261 /* Get the frame containing the mini-buffer
10262 that the selected frame is using. */
10263 mini_window = FRAME_MINIBUF_WINDOW (sf);
10264 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10266 /* Error messages get reported properly by cmd_error, so this must be
10267 just an informative message; if the frame hasn't really been
10268 initialized yet, just toss it. */
10269 if (f->glyphs_initialized_p)
10271 if (m)
10273 ptrdiff_t len;
10274 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10275 USE_SAFE_ALLOCA;
10276 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10278 len = doprnt (message_buf, maxsize, m, 0, ap);
10280 message3 (make_string (message_buf, len));
10281 SAFE_FREE ();
10283 else
10284 message1 (0);
10286 /* Print should start at the beginning of the message
10287 buffer next time. */
10288 message_buf_print = false;
10293 void
10294 message (const char *m, ...)
10296 va_list ap;
10297 va_start (ap, m);
10298 vmessage (m, ap);
10299 va_end (ap);
10303 #if false
10304 /* The non-logging version of message. */
10306 void
10307 message_nolog (const char *m, ...)
10309 Lisp_Object old_log_max;
10310 va_list ap;
10311 va_start (ap, m);
10312 old_log_max = Vmessage_log_max;
10313 Vmessage_log_max = Qnil;
10314 vmessage (m, ap);
10315 Vmessage_log_max = old_log_max;
10316 va_end (ap);
10318 #endif
10321 /* Display the current message in the current mini-buffer. This is
10322 only called from error handlers in process.c, and is not time
10323 critical. */
10325 void
10326 update_echo_area (void)
10328 if (!NILP (echo_area_buffer[0]))
10330 Lisp_Object string;
10331 string = Fcurrent_message ();
10332 message3 (string);
10337 /* Make sure echo area buffers in `echo_buffers' are live.
10338 If they aren't, make new ones. */
10340 static void
10341 ensure_echo_area_buffers (void)
10343 int i;
10345 for (i = 0; i < 2; ++i)
10346 if (!BUFFERP (echo_buffer[i])
10347 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10349 char name[30];
10350 Lisp_Object old_buffer;
10351 int j;
10353 old_buffer = echo_buffer[i];
10354 echo_buffer[i] = Fget_buffer_create
10355 (make_formatted_string (name, " *Echo Area %d*", i));
10356 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10357 /* to force word wrap in echo area -
10358 it was decided to postpone this*/
10359 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10361 for (j = 0; j < 2; ++j)
10362 if (EQ (old_buffer, echo_area_buffer[j]))
10363 echo_area_buffer[j] = echo_buffer[i];
10368 /* Call FN with args A1..A2 with either the current or last displayed
10369 echo_area_buffer as current buffer.
10371 WHICH zero means use the current message buffer
10372 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10373 from echo_buffer[] and clear it.
10375 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10376 suitable buffer from echo_buffer[] and clear it.
10378 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10379 that the current message becomes the last displayed one, make
10380 choose a suitable buffer for echo_area_buffer[0], and clear it.
10382 Value is what FN returns. */
10384 static bool
10385 with_echo_area_buffer (struct window *w, int which,
10386 bool (*fn) (ptrdiff_t, Lisp_Object),
10387 ptrdiff_t a1, Lisp_Object a2)
10389 Lisp_Object buffer;
10390 bool this_one, the_other, clear_buffer_p, rc;
10391 ptrdiff_t count = SPECPDL_INDEX ();
10393 /* If buffers aren't live, make new ones. */
10394 ensure_echo_area_buffers ();
10396 clear_buffer_p = false;
10398 if (which == 0)
10399 this_one = false, the_other = true;
10400 else if (which > 0)
10401 this_one = true, the_other = false;
10402 else
10404 this_one = false, the_other = true;
10405 clear_buffer_p = true;
10407 /* We need a fresh one in case the current echo buffer equals
10408 the one containing the last displayed echo area message. */
10409 if (!NILP (echo_area_buffer[this_one])
10410 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10411 echo_area_buffer[this_one] = Qnil;
10414 /* Choose a suitable buffer from echo_buffer[] is we don't
10415 have one. */
10416 if (NILP (echo_area_buffer[this_one]))
10418 echo_area_buffer[this_one]
10419 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10420 ? echo_buffer[the_other]
10421 : echo_buffer[this_one]);
10422 clear_buffer_p = true;
10425 buffer = echo_area_buffer[this_one];
10427 /* Don't get confused by reusing the buffer used for echoing
10428 for a different purpose. */
10429 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10430 cancel_echoing ();
10432 record_unwind_protect (unwind_with_echo_area_buffer,
10433 with_echo_area_buffer_unwind_data (w));
10435 /* Make the echo area buffer current. Note that for display
10436 purposes, it is not necessary that the displayed window's buffer
10437 == current_buffer, except for text property lookup. So, let's
10438 only set that buffer temporarily here without doing a full
10439 Fset_window_buffer. We must also change w->pointm, though,
10440 because otherwise an assertions in unshow_buffer fails, and Emacs
10441 aborts. */
10442 set_buffer_internal_1 (XBUFFER (buffer));
10443 if (w)
10445 wset_buffer (w, buffer);
10446 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10447 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10450 bset_undo_list (current_buffer, Qt);
10451 bset_read_only (current_buffer, Qnil);
10452 specbind (Qinhibit_read_only, Qt);
10453 specbind (Qinhibit_modification_hooks, Qt);
10455 if (clear_buffer_p && Z > BEG)
10456 del_range (BEG, Z);
10458 eassert (BEGV >= BEG);
10459 eassert (ZV <= Z && ZV >= BEGV);
10461 rc = fn (a1, a2);
10463 eassert (BEGV >= BEG);
10464 eassert (ZV <= Z && ZV >= BEGV);
10466 unbind_to (count, Qnil);
10467 return rc;
10471 /* Save state that should be preserved around the call to the function
10472 FN called in with_echo_area_buffer. */
10474 static Lisp_Object
10475 with_echo_area_buffer_unwind_data (struct window *w)
10477 int i = 0;
10478 Lisp_Object vector, tmp;
10480 /* Reduce consing by keeping one vector in
10481 Vwith_echo_area_save_vector. */
10482 vector = Vwith_echo_area_save_vector;
10483 Vwith_echo_area_save_vector = Qnil;
10485 if (NILP (vector))
10486 vector = Fmake_vector (make_number (11), Qnil);
10488 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10489 ASET (vector, i, Vdeactivate_mark); ++i;
10490 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10492 if (w)
10494 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10495 ASET (vector, i, w->contents); ++i;
10496 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10497 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10498 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10499 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10500 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10501 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10503 else
10505 int end = i + 8;
10506 for (; i < end; ++i)
10507 ASET (vector, i, Qnil);
10510 eassert (i == ASIZE (vector));
10511 return vector;
10515 /* Restore global state from VECTOR which was created by
10516 with_echo_area_buffer_unwind_data. */
10518 static void
10519 unwind_with_echo_area_buffer (Lisp_Object vector)
10521 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10522 Vdeactivate_mark = AREF (vector, 1);
10523 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10525 if (WINDOWP (AREF (vector, 3)))
10527 struct window *w;
10528 Lisp_Object buffer;
10530 w = XWINDOW (AREF (vector, 3));
10531 buffer = AREF (vector, 4);
10533 wset_buffer (w, buffer);
10534 set_marker_both (w->pointm, buffer,
10535 XFASTINT (AREF (vector, 5)),
10536 XFASTINT (AREF (vector, 6)));
10537 set_marker_both (w->old_pointm, buffer,
10538 XFASTINT (AREF (vector, 7)),
10539 XFASTINT (AREF (vector, 8)));
10540 set_marker_both (w->start, buffer,
10541 XFASTINT (AREF (vector, 9)),
10542 XFASTINT (AREF (vector, 10)));
10545 Vwith_echo_area_save_vector = vector;
10549 /* Set up the echo area for use by print functions. MULTIBYTE_P
10550 means we will print multibyte. */
10552 void
10553 setup_echo_area_for_printing (bool multibyte_p)
10555 /* If we can't find an echo area any more, exit. */
10556 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10557 Fkill_emacs (Qnil);
10559 ensure_echo_area_buffers ();
10561 if (!message_buf_print)
10563 /* A message has been output since the last time we printed.
10564 Choose a fresh echo area buffer. */
10565 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10566 echo_area_buffer[0] = echo_buffer[1];
10567 else
10568 echo_area_buffer[0] = echo_buffer[0];
10570 /* Switch to that buffer and clear it. */
10571 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10572 bset_truncate_lines (current_buffer, Qnil);
10574 if (Z > BEG)
10576 ptrdiff_t count = SPECPDL_INDEX ();
10577 specbind (Qinhibit_read_only, Qt);
10578 /* Note that undo recording is always disabled. */
10579 del_range (BEG, Z);
10580 unbind_to (count, Qnil);
10582 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10584 /* Set up the buffer for the multibyteness we need. */
10585 if (multibyte_p
10586 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10587 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10589 /* Raise the frame containing the echo area. */
10590 if (minibuffer_auto_raise)
10592 struct frame *sf = SELECTED_FRAME ();
10593 Lisp_Object mini_window;
10594 mini_window = FRAME_MINIBUF_WINDOW (sf);
10595 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10598 message_log_maybe_newline ();
10599 message_buf_print = true;
10601 else
10603 if (NILP (echo_area_buffer[0]))
10605 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10606 echo_area_buffer[0] = echo_buffer[1];
10607 else
10608 echo_area_buffer[0] = echo_buffer[0];
10611 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10613 /* Someone switched buffers between print requests. */
10614 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10615 bset_truncate_lines (current_buffer, Qnil);
10621 /* Display an echo area message in window W. Value is true if W's
10622 height is changed. If display_last_displayed_message_p,
10623 display the message that was last displayed, otherwise
10624 display the current message. */
10626 static bool
10627 display_echo_area (struct window *w)
10629 bool no_message_p, window_height_changed_p;
10631 /* Temporarily disable garbage collections while displaying the echo
10632 area. This is done because a GC can print a message itself.
10633 That message would modify the echo area buffer's contents while a
10634 redisplay of the buffer is going on, and seriously confuse
10635 redisplay. */
10636 ptrdiff_t count = inhibit_garbage_collection ();
10638 /* If there is no message, we must call display_echo_area_1
10639 nevertheless because it resizes the window. But we will have to
10640 reset the echo_area_buffer in question to nil at the end because
10641 with_echo_area_buffer will sets it to an empty buffer. */
10642 bool i = display_last_displayed_message_p;
10643 no_message_p = NILP (echo_area_buffer[i]);
10645 window_height_changed_p
10646 = with_echo_area_buffer (w, display_last_displayed_message_p,
10647 display_echo_area_1,
10648 (intptr_t) w, Qnil);
10650 if (no_message_p)
10651 echo_area_buffer[i] = Qnil;
10653 unbind_to (count, Qnil);
10654 return window_height_changed_p;
10658 /* Helper for display_echo_area. Display the current buffer which
10659 contains the current echo area message in window W, a mini-window,
10660 a pointer to which is passed in A1. A2..A4 are currently not used.
10661 Change the height of W so that all of the message is displayed.
10662 Value is true if height of W was changed. */
10664 static bool
10665 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10667 intptr_t i1 = a1;
10668 struct window *w = (struct window *) i1;
10669 Lisp_Object window;
10670 struct text_pos start;
10672 /* Do this before displaying, so that we have a large enough glyph
10673 matrix for the display. If we can't get enough space for the
10674 whole text, display the last N lines. That works by setting w->start. */
10675 bool window_height_changed_p = resize_mini_window (w, false);
10677 /* Use the starting position chosen by resize_mini_window. */
10678 SET_TEXT_POS_FROM_MARKER (start, w->start);
10680 /* Display. */
10681 clear_glyph_matrix (w->desired_matrix);
10682 XSETWINDOW (window, w);
10683 try_window (window, start, 0);
10685 return window_height_changed_p;
10689 /* Resize the echo area window to exactly the size needed for the
10690 currently displayed message, if there is one. If a mini-buffer
10691 is active, don't shrink it. */
10693 void
10694 resize_echo_area_exactly (void)
10696 if (BUFFERP (echo_area_buffer[0])
10697 && WINDOWP (echo_area_window))
10699 struct window *w = XWINDOW (echo_area_window);
10700 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10701 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10702 (intptr_t) w, resize_exactly);
10703 if (resized_p)
10705 windows_or_buffers_changed = 42;
10706 update_mode_lines = 30;
10707 redisplay_internal ();
10713 /* Callback function for with_echo_area_buffer, when used from
10714 resize_echo_area_exactly. A1 contains a pointer to the window to
10715 resize, EXACTLY non-nil means resize the mini-window exactly to the
10716 size of the text displayed. A3 and A4 are not used. Value is what
10717 resize_mini_window returns. */
10719 static bool
10720 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10722 intptr_t i1 = a1;
10723 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10727 /* Resize mini-window W to fit the size of its contents. EXACT_P
10728 means size the window exactly to the size needed. Otherwise, it's
10729 only enlarged until W's buffer is empty.
10731 Set W->start to the right place to begin display. If the whole
10732 contents fit, start at the beginning. Otherwise, start so as
10733 to make the end of the contents appear. This is particularly
10734 important for y-or-n-p, but seems desirable generally.
10736 Value is true if the window height has been changed. */
10738 bool
10739 resize_mini_window (struct window *w, bool exact_p)
10741 struct frame *f = XFRAME (w->frame);
10742 bool window_height_changed_p = false;
10744 eassert (MINI_WINDOW_P (w));
10746 /* By default, start display at the beginning. */
10747 set_marker_both (w->start, w->contents,
10748 BUF_BEGV (XBUFFER (w->contents)),
10749 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10751 /* Don't resize windows while redisplaying a window; it would
10752 confuse redisplay functions when the size of the window they are
10753 displaying changes from under them. Such a resizing can happen,
10754 for instance, when which-func prints a long message while
10755 we are running fontification-functions. We're running these
10756 functions with safe_call which binds inhibit-redisplay to t. */
10757 if (!NILP (Vinhibit_redisplay))
10758 return false;
10760 /* Nil means don't try to resize. */
10761 if (NILP (Vresize_mini_windows)
10762 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10763 return false;
10765 if (!FRAME_MINIBUF_ONLY_P (f))
10767 struct it it;
10768 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10769 + WINDOW_PIXEL_HEIGHT (w));
10770 int unit = FRAME_LINE_HEIGHT (f);
10771 int height, max_height;
10772 struct text_pos start;
10773 struct buffer *old_current_buffer = NULL;
10775 if (current_buffer != XBUFFER (w->contents))
10777 old_current_buffer = current_buffer;
10778 set_buffer_internal (XBUFFER (w->contents));
10781 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10783 /* Compute the max. number of lines specified by the user. */
10784 if (FLOATP (Vmax_mini_window_height))
10785 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10786 else if (INTEGERP (Vmax_mini_window_height))
10787 max_height = XINT (Vmax_mini_window_height) * unit;
10788 else
10789 max_height = total_height / 4;
10791 /* Correct that max. height if it's bogus. */
10792 max_height = clip_to_bounds (unit, max_height, total_height);
10794 /* Find out the height of the text in the window. */
10795 if (it.line_wrap == TRUNCATE)
10796 height = unit;
10797 else
10799 last_height = 0;
10800 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10801 if (it.max_ascent == 0 && it.max_descent == 0)
10802 height = it.current_y + last_height;
10803 else
10804 height = it.current_y + it.max_ascent + it.max_descent;
10805 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10808 /* Compute a suitable window start. */
10809 if (height > max_height)
10811 height = (max_height / unit) * unit;
10812 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10813 move_it_vertically_backward (&it, height - unit);
10814 start = it.current.pos;
10816 else
10817 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10818 SET_MARKER_FROM_TEXT_POS (w->start, start);
10820 if (EQ (Vresize_mini_windows, Qgrow_only))
10822 /* Let it grow only, until we display an empty message, in which
10823 case the window shrinks again. */
10824 if (height > WINDOW_PIXEL_HEIGHT (w))
10826 int old_height = WINDOW_PIXEL_HEIGHT (w);
10828 FRAME_WINDOWS_FROZEN (f) = true;
10829 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10830 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10832 else if (height < WINDOW_PIXEL_HEIGHT (w)
10833 && (exact_p || BEGV == ZV))
10835 int old_height = WINDOW_PIXEL_HEIGHT (w);
10837 FRAME_WINDOWS_FROZEN (f) = false;
10838 shrink_mini_window (w, true);
10839 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10842 else
10844 /* Always resize to exact size needed. */
10845 if (height > WINDOW_PIXEL_HEIGHT (w))
10847 int old_height = WINDOW_PIXEL_HEIGHT (w);
10849 FRAME_WINDOWS_FROZEN (f) = true;
10850 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10851 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10853 else if (height < WINDOW_PIXEL_HEIGHT (w))
10855 int old_height = WINDOW_PIXEL_HEIGHT (w);
10857 FRAME_WINDOWS_FROZEN (f) = false;
10858 shrink_mini_window (w, true);
10860 if (height)
10862 FRAME_WINDOWS_FROZEN (f) = true;
10863 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10866 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10870 if (old_current_buffer)
10871 set_buffer_internal (old_current_buffer);
10874 return window_height_changed_p;
10878 /* Value is the current message, a string, or nil if there is no
10879 current message. */
10881 Lisp_Object
10882 current_message (void)
10884 Lisp_Object msg;
10886 if (!BUFFERP (echo_area_buffer[0]))
10887 msg = Qnil;
10888 else
10890 with_echo_area_buffer (0, 0, current_message_1,
10891 (intptr_t) &msg, Qnil);
10892 if (NILP (msg))
10893 echo_area_buffer[0] = Qnil;
10896 return msg;
10900 static bool
10901 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10903 intptr_t i1 = a1;
10904 Lisp_Object *msg = (Lisp_Object *) i1;
10906 if (Z > BEG)
10907 *msg = make_buffer_string (BEG, Z, true);
10908 else
10909 *msg = Qnil;
10910 return false;
10914 /* Push the current message on Vmessage_stack for later restoration
10915 by restore_message. Value is true if the current message isn't
10916 empty. This is a relatively infrequent operation, so it's not
10917 worth optimizing. */
10919 bool
10920 push_message (void)
10922 Lisp_Object msg = current_message ();
10923 Vmessage_stack = Fcons (msg, Vmessage_stack);
10924 return STRINGP (msg);
10928 /* Restore message display from the top of Vmessage_stack. */
10930 void
10931 restore_message (void)
10933 eassert (CONSP (Vmessage_stack));
10934 message3_nolog (XCAR (Vmessage_stack));
10938 /* Handler for unwind-protect calling pop_message. */
10940 void
10941 pop_message_unwind (void)
10943 /* Pop the top-most entry off Vmessage_stack. */
10944 eassert (CONSP (Vmessage_stack));
10945 Vmessage_stack = XCDR (Vmessage_stack);
10949 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10950 exits. If the stack is not empty, we have a missing pop_message
10951 somewhere. */
10953 void
10954 check_message_stack (void)
10956 if (!NILP (Vmessage_stack))
10957 emacs_abort ();
10961 /* Truncate to NCHARS what will be displayed in the echo area the next
10962 time we display it---but don't redisplay it now. */
10964 void
10965 truncate_echo_area (ptrdiff_t nchars)
10967 if (nchars == 0)
10968 echo_area_buffer[0] = Qnil;
10969 else if (!noninteractive
10970 && INTERACTIVE
10971 && !NILP (echo_area_buffer[0]))
10973 struct frame *sf = SELECTED_FRAME ();
10974 /* Error messages get reported properly by cmd_error, so this must be
10975 just an informative message; if the frame hasn't really been
10976 initialized yet, just toss it. */
10977 if (sf->glyphs_initialized_p)
10978 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10983 /* Helper function for truncate_echo_area. Truncate the current
10984 message to at most NCHARS characters. */
10986 static bool
10987 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10989 if (BEG + nchars < Z)
10990 del_range (BEG + nchars, Z);
10991 if (Z == BEG)
10992 echo_area_buffer[0] = Qnil;
10993 return false;
10996 /* Set the current message to STRING. */
10998 static void
10999 set_message (Lisp_Object string)
11001 eassert (STRINGP (string));
11003 message_enable_multibyte = STRING_MULTIBYTE (string);
11005 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11006 message_buf_print = false;
11007 help_echo_showing_p = false;
11009 if (STRINGP (Vdebug_on_message)
11010 && STRINGP (string)
11011 && fast_string_match (Vdebug_on_message, string) >= 0)
11012 call_debugger (list2 (Qerror, string));
11016 /* Helper function for set_message. First argument is ignored and second
11017 argument has the same meaning as for set_message.
11018 This function is called with the echo area buffer being current. */
11020 static bool
11021 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11023 eassert (STRINGP (string));
11025 /* Change multibyteness of the echo buffer appropriately. */
11026 if (message_enable_multibyte
11027 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11028 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11030 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11031 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11032 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11034 /* Insert new message at BEG. */
11035 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11037 /* This function takes care of single/multibyte conversion.
11038 We just have to ensure that the echo area buffer has the right
11039 setting of enable_multibyte_characters. */
11040 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11042 return false;
11046 /* Clear messages. CURRENT_P means clear the current message.
11047 LAST_DISPLAYED_P means clear the message last displayed. */
11049 void
11050 clear_message (bool current_p, bool last_displayed_p)
11052 if (current_p)
11054 echo_area_buffer[0] = Qnil;
11055 message_cleared_p = true;
11058 if (last_displayed_p)
11059 echo_area_buffer[1] = Qnil;
11061 message_buf_print = false;
11064 /* Clear garbaged frames.
11066 This function is used where the old redisplay called
11067 redraw_garbaged_frames which in turn called redraw_frame which in
11068 turn called clear_frame. The call to clear_frame was a source of
11069 flickering. I believe a clear_frame is not necessary. It should
11070 suffice in the new redisplay to invalidate all current matrices,
11071 and ensure a complete redisplay of all windows. */
11073 static void
11074 clear_garbaged_frames (void)
11076 if (frame_garbaged)
11078 Lisp_Object tail, frame;
11080 FOR_EACH_FRAME (tail, frame)
11082 struct frame *f = XFRAME (frame);
11084 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11086 if (f->resized_p)
11087 redraw_frame (f);
11088 else
11089 clear_current_matrices (f);
11090 fset_redisplay (f);
11091 f->garbaged = false;
11092 f->resized_p = false;
11096 frame_garbaged = false;
11101 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P,
11102 update selected_frame. Value is true if the mini-windows height
11103 has been changed. */
11105 static bool
11106 echo_area_display (bool update_frame_p)
11108 Lisp_Object mini_window;
11109 struct window *w;
11110 struct frame *f;
11111 bool window_height_changed_p = false;
11112 struct frame *sf = SELECTED_FRAME ();
11114 mini_window = FRAME_MINIBUF_WINDOW (sf);
11115 w = XWINDOW (mini_window);
11116 f = XFRAME (WINDOW_FRAME (w));
11118 /* Don't display if frame is invisible or not yet initialized. */
11119 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11120 return false;
11122 #ifdef HAVE_WINDOW_SYSTEM
11123 /* When Emacs starts, selected_frame may be the initial terminal
11124 frame. If we let this through, a message would be displayed on
11125 the terminal. */
11126 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11127 return false;
11128 #endif /* HAVE_WINDOW_SYSTEM */
11130 /* Redraw garbaged frames. */
11131 clear_garbaged_frames ();
11133 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11135 echo_area_window = mini_window;
11136 window_height_changed_p = display_echo_area (w);
11137 w->must_be_updated_p = true;
11139 /* Update the display, unless called from redisplay_internal.
11140 Also don't update the screen during redisplay itself. The
11141 update will happen at the end of redisplay, and an update
11142 here could cause confusion. */
11143 if (update_frame_p && !redisplaying_p)
11145 int n = 0;
11147 /* If the display update has been interrupted by pending
11148 input, update mode lines in the frame. Due to the
11149 pending input, it might have been that redisplay hasn't
11150 been called, so that mode lines above the echo area are
11151 garbaged. This looks odd, so we prevent it here. */
11152 if (!display_completed)
11153 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11155 if (window_height_changed_p
11156 /* Don't do this if Emacs is shutting down. Redisplay
11157 needs to run hooks. */
11158 && !NILP (Vrun_hooks))
11160 /* Must update other windows. Likewise as in other
11161 cases, don't let this update be interrupted by
11162 pending input. */
11163 ptrdiff_t count = SPECPDL_INDEX ();
11164 specbind (Qredisplay_dont_pause, Qt);
11165 windows_or_buffers_changed = 44;
11166 redisplay_internal ();
11167 unbind_to (count, Qnil);
11169 else if (FRAME_WINDOW_P (f) && n == 0)
11171 /* Window configuration is the same as before.
11172 Can do with a display update of the echo area,
11173 unless we displayed some mode lines. */
11174 update_single_window (w);
11175 flush_frame (f);
11177 else
11178 update_frame (f, true, true);
11180 /* If cursor is in the echo area, make sure that the next
11181 redisplay displays the minibuffer, so that the cursor will
11182 be replaced with what the minibuffer wants. */
11183 if (cursor_in_echo_area)
11184 wset_redisplay (XWINDOW (mini_window));
11187 else if (!EQ (mini_window, selected_window))
11188 wset_redisplay (XWINDOW (mini_window));
11190 /* Last displayed message is now the current message. */
11191 echo_area_buffer[1] = echo_area_buffer[0];
11192 /* Inform read_char that we're not echoing. */
11193 echo_message_buffer = Qnil;
11195 /* Prevent redisplay optimization in redisplay_internal by resetting
11196 this_line_start_pos. This is done because the mini-buffer now
11197 displays the message instead of its buffer text. */
11198 if (EQ (mini_window, selected_window))
11199 CHARPOS (this_line_start_pos) = 0;
11201 return window_height_changed_p;
11204 /* True if W's buffer was changed but not saved. */
11206 static bool
11207 window_buffer_changed (struct window *w)
11209 struct buffer *b = XBUFFER (w->contents);
11211 eassert (BUFFER_LIVE_P (b));
11213 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11216 /* True if W has %c in its mode line and mode line should be updated. */
11218 static bool
11219 mode_line_update_needed (struct window *w)
11221 return (w->column_number_displayed != -1
11222 && !(PT == w->last_point && !window_outdated (w))
11223 && (w->column_number_displayed != current_column ()));
11226 /* True if window start of W is frozen and may not be changed during
11227 redisplay. */
11229 static bool
11230 window_frozen_p (struct window *w)
11232 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11234 Lisp_Object window;
11236 XSETWINDOW (window, w);
11237 if (MINI_WINDOW_P (w))
11238 return false;
11239 else if (EQ (window, selected_window))
11240 return false;
11241 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11242 && EQ (window, Vminibuf_scroll_window))
11243 /* This special window can't be frozen too. */
11244 return false;
11245 else
11246 return true;
11248 return false;
11251 /***********************************************************************
11252 Mode Lines and Frame Titles
11253 ***********************************************************************/
11255 /* A buffer for constructing non-propertized mode-line strings and
11256 frame titles in it; allocated from the heap in init_xdisp and
11257 resized as needed in store_mode_line_noprop_char. */
11259 static char *mode_line_noprop_buf;
11261 /* The buffer's end, and a current output position in it. */
11263 static char *mode_line_noprop_buf_end;
11264 static char *mode_line_noprop_ptr;
11266 #define MODE_LINE_NOPROP_LEN(start) \
11267 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11269 static enum {
11270 MODE_LINE_DISPLAY = 0,
11271 MODE_LINE_TITLE,
11272 MODE_LINE_NOPROP,
11273 MODE_LINE_STRING
11274 } mode_line_target;
11276 /* Alist that caches the results of :propertize.
11277 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11278 static Lisp_Object mode_line_proptrans_alist;
11280 /* List of strings making up the mode-line. */
11281 static Lisp_Object mode_line_string_list;
11283 /* Base face property when building propertized mode line string. */
11284 static Lisp_Object mode_line_string_face;
11285 static Lisp_Object mode_line_string_face_prop;
11288 /* Unwind data for mode line strings */
11290 static Lisp_Object Vmode_line_unwind_vector;
11292 static Lisp_Object
11293 format_mode_line_unwind_data (struct frame *target_frame,
11294 struct buffer *obuf,
11295 Lisp_Object owin,
11296 bool save_proptrans)
11298 Lisp_Object vector, tmp;
11300 /* Reduce consing by keeping one vector in
11301 Vwith_echo_area_save_vector. */
11302 vector = Vmode_line_unwind_vector;
11303 Vmode_line_unwind_vector = Qnil;
11305 if (NILP (vector))
11306 vector = Fmake_vector (make_number (10), Qnil);
11308 ASET (vector, 0, make_number (mode_line_target));
11309 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11310 ASET (vector, 2, mode_line_string_list);
11311 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11312 ASET (vector, 4, mode_line_string_face);
11313 ASET (vector, 5, mode_line_string_face_prop);
11315 if (obuf)
11316 XSETBUFFER (tmp, obuf);
11317 else
11318 tmp = Qnil;
11319 ASET (vector, 6, tmp);
11320 ASET (vector, 7, owin);
11321 if (target_frame)
11323 /* Similarly to `with-selected-window', if the operation selects
11324 a window on another frame, we must restore that frame's
11325 selected window, and (for a tty) the top-frame. */
11326 ASET (vector, 8, target_frame->selected_window);
11327 if (FRAME_TERMCAP_P (target_frame))
11328 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11331 return vector;
11334 static void
11335 unwind_format_mode_line (Lisp_Object vector)
11337 Lisp_Object old_window = AREF (vector, 7);
11338 Lisp_Object target_frame_window = AREF (vector, 8);
11339 Lisp_Object old_top_frame = AREF (vector, 9);
11341 mode_line_target = XINT (AREF (vector, 0));
11342 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11343 mode_line_string_list = AREF (vector, 2);
11344 if (! EQ (AREF (vector, 3), Qt))
11345 mode_line_proptrans_alist = AREF (vector, 3);
11346 mode_line_string_face = AREF (vector, 4);
11347 mode_line_string_face_prop = AREF (vector, 5);
11349 /* Select window before buffer, since it may change the buffer. */
11350 if (!NILP (old_window))
11352 /* If the operation that we are unwinding had selected a window
11353 on a different frame, reset its frame-selected-window. For a
11354 text terminal, reset its top-frame if necessary. */
11355 if (!NILP (target_frame_window))
11357 Lisp_Object frame
11358 = WINDOW_FRAME (XWINDOW (target_frame_window));
11360 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11361 Fselect_window (target_frame_window, Qt);
11363 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11364 Fselect_frame (old_top_frame, Qt);
11367 Fselect_window (old_window, Qt);
11370 if (!NILP (AREF (vector, 6)))
11372 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11373 ASET (vector, 6, Qnil);
11376 Vmode_line_unwind_vector = vector;
11380 /* Store a single character C for the frame title in mode_line_noprop_buf.
11381 Re-allocate mode_line_noprop_buf if necessary. */
11383 static void
11384 store_mode_line_noprop_char (char c)
11386 /* If output position has reached the end of the allocated buffer,
11387 increase the buffer's size. */
11388 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11390 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11391 ptrdiff_t size = len;
11392 mode_line_noprop_buf =
11393 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11394 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11395 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11398 *mode_line_noprop_ptr++ = c;
11402 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11403 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11404 characters that yield more columns than PRECISION; PRECISION <= 0
11405 means copy the whole string. Pad with spaces until FIELD_WIDTH
11406 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11407 pad. Called from display_mode_element when it is used to build a
11408 frame title. */
11410 static int
11411 store_mode_line_noprop (const char *string, int field_width, int precision)
11413 const unsigned char *str = (const unsigned char *) string;
11414 int n = 0;
11415 ptrdiff_t dummy, nbytes;
11417 /* Copy at most PRECISION chars from STR. */
11418 nbytes = strlen (string);
11419 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11420 while (nbytes--)
11421 store_mode_line_noprop_char (*str++);
11423 /* Fill up with spaces until FIELD_WIDTH reached. */
11424 while (field_width > 0
11425 && n < field_width)
11427 store_mode_line_noprop_char (' ');
11428 ++n;
11431 return n;
11434 /***********************************************************************
11435 Frame Titles
11436 ***********************************************************************/
11438 #ifdef HAVE_WINDOW_SYSTEM
11440 /* Set the title of FRAME, if it has changed. The title format is
11441 Vicon_title_format if FRAME is iconified, otherwise it is
11442 frame_title_format. */
11444 static void
11445 x_consider_frame_title (Lisp_Object frame)
11447 struct frame *f = XFRAME (frame);
11449 if (FRAME_WINDOW_P (f)
11450 || FRAME_MINIBUF_ONLY_P (f)
11451 || f->explicit_name)
11453 /* Do we have more than one visible frame on this X display? */
11454 Lisp_Object tail, other_frame, fmt;
11455 ptrdiff_t title_start;
11456 char *title;
11457 ptrdiff_t len;
11458 struct it it;
11459 ptrdiff_t count = SPECPDL_INDEX ();
11461 FOR_EACH_FRAME (tail, other_frame)
11463 struct frame *tf = XFRAME (other_frame);
11465 if (tf != f
11466 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11467 && !FRAME_MINIBUF_ONLY_P (tf)
11468 && !EQ (other_frame, tip_frame)
11469 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11470 break;
11473 /* Set global variable indicating that multiple frames exist. */
11474 multiple_frames = CONSP (tail);
11476 /* Switch to the buffer of selected window of the frame. Set up
11477 mode_line_target so that display_mode_element will output into
11478 mode_line_noprop_buf; then display the title. */
11479 record_unwind_protect (unwind_format_mode_line,
11480 format_mode_line_unwind_data
11481 (f, current_buffer, selected_window, false));
11483 Fselect_window (f->selected_window, Qt);
11484 set_buffer_internal_1
11485 (XBUFFER (XWINDOW (f->selected_window)->contents));
11486 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11488 mode_line_target = MODE_LINE_TITLE;
11489 title_start = MODE_LINE_NOPROP_LEN (0);
11490 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11491 NULL, DEFAULT_FACE_ID);
11492 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11493 len = MODE_LINE_NOPROP_LEN (title_start);
11494 title = mode_line_noprop_buf + title_start;
11495 unbind_to (count, Qnil);
11497 /* Set the title only if it's changed. This avoids consing in
11498 the common case where it hasn't. (If it turns out that we've
11499 already wasted too much time by walking through the list with
11500 display_mode_element, then we might need to optimize at a
11501 higher level than this.) */
11502 if (! STRINGP (f->name)
11503 || SBYTES (f->name) != len
11504 || memcmp (title, SDATA (f->name), len) != 0)
11505 x_implicitly_set_name (f, make_string (title, len), Qnil);
11509 #endif /* not HAVE_WINDOW_SYSTEM */
11512 /***********************************************************************
11513 Menu Bars
11514 ***********************************************************************/
11516 /* True if we will not redisplay all visible windows. */
11517 #define REDISPLAY_SOME_P() \
11518 ((windows_or_buffers_changed == 0 \
11519 || windows_or_buffers_changed == REDISPLAY_SOME) \
11520 && (update_mode_lines == 0 \
11521 || update_mode_lines == REDISPLAY_SOME))
11523 /* Prepare for redisplay by updating menu-bar item lists when
11524 appropriate. This can call eval. */
11526 static void
11527 prepare_menu_bars (void)
11529 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11530 bool some_windows = REDISPLAY_SOME_P ();
11531 struct gcpro gcpro1, gcpro2;
11532 Lisp_Object tooltip_frame;
11534 #ifdef HAVE_WINDOW_SYSTEM
11535 tooltip_frame = tip_frame;
11536 #else
11537 tooltip_frame = Qnil;
11538 #endif
11540 if (FUNCTIONP (Vpre_redisplay_function))
11542 Lisp_Object windows = all_windows ? Qt : Qnil;
11543 if (all_windows && some_windows)
11545 Lisp_Object ws = window_list ();
11546 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11548 Lisp_Object this = XCAR (ws);
11549 struct window *w = XWINDOW (this);
11550 if (w->redisplay
11551 || XFRAME (w->frame)->redisplay
11552 || XBUFFER (w->contents)->text->redisplay)
11554 windows = Fcons (this, windows);
11558 safe__call1 (true, Vpre_redisplay_function, windows);
11561 /* Update all frame titles based on their buffer names, etc. We do
11562 this before the menu bars so that the buffer-menu will show the
11563 up-to-date frame titles. */
11564 #ifdef HAVE_WINDOW_SYSTEM
11565 if (all_windows)
11567 Lisp_Object tail, frame;
11569 FOR_EACH_FRAME (tail, frame)
11571 struct frame *f = XFRAME (frame);
11572 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11573 if (some_windows
11574 && !f->redisplay
11575 && !w->redisplay
11576 && !XBUFFER (w->contents)->text->redisplay)
11577 continue;
11579 if (!EQ (frame, tooltip_frame)
11580 && (FRAME_ICONIFIED_P (f)
11581 || FRAME_VISIBLE_P (f) == 1
11582 /* Exclude TTY frames that are obscured because they
11583 are not the top frame on their console. This is
11584 because x_consider_frame_title actually switches
11585 to the frame, which for TTY frames means it is
11586 marked as garbaged, and will be completely
11587 redrawn on the next redisplay cycle. This causes
11588 TTY frames to be completely redrawn, when there
11589 are more than one of them, even though nothing
11590 should be changed on display. */
11591 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11592 x_consider_frame_title (frame);
11595 #endif /* HAVE_WINDOW_SYSTEM */
11597 /* Update the menu bar item lists, if appropriate. This has to be
11598 done before any actual redisplay or generation of display lines. */
11600 if (all_windows)
11602 Lisp_Object tail, frame;
11603 ptrdiff_t count = SPECPDL_INDEX ();
11604 /* True means that update_menu_bar has run its hooks
11605 so any further calls to update_menu_bar shouldn't do so again. */
11606 bool menu_bar_hooks_run = false;
11608 record_unwind_save_match_data ();
11610 FOR_EACH_FRAME (tail, frame)
11612 struct frame *f = XFRAME (frame);
11613 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11615 /* Ignore tooltip frame. */
11616 if (EQ (frame, tooltip_frame))
11617 continue;
11619 if (some_windows
11620 && !f->redisplay
11621 && !w->redisplay
11622 && !XBUFFER (w->contents)->text->redisplay)
11623 continue;
11625 /* If a window on this frame changed size, report that to
11626 the user and clear the size-change flag. */
11627 if (FRAME_WINDOW_SIZES_CHANGED (f))
11629 Lisp_Object functions;
11631 /* Clear flag first in case we get an error below. */
11632 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11633 functions = Vwindow_size_change_functions;
11634 GCPRO2 (tail, functions);
11636 while (CONSP (functions))
11638 if (!EQ (XCAR (functions), Qt))
11639 call1 (XCAR (functions), frame);
11640 functions = XCDR (functions);
11642 UNGCPRO;
11645 GCPRO1 (tail);
11646 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11647 #ifdef HAVE_WINDOW_SYSTEM
11648 update_tool_bar (f, false);
11649 #endif
11650 UNGCPRO;
11653 unbind_to (count, Qnil);
11655 else
11657 struct frame *sf = SELECTED_FRAME ();
11658 update_menu_bar (sf, true, false);
11659 #ifdef HAVE_WINDOW_SYSTEM
11660 update_tool_bar (sf, true);
11661 #endif
11666 /* Update the menu bar item list for frame F. This has to be done
11667 before we start to fill in any display lines, because it can call
11668 eval.
11670 If SAVE_MATCH_DATA, we must save and restore it here.
11672 If HOOKS_RUN, a previous call to update_menu_bar
11673 already ran the menu bar hooks for this redisplay, so there
11674 is no need to run them again. The return value is the
11675 updated value of this flag, to pass to the next call. */
11677 static bool
11678 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11680 Lisp_Object window;
11681 struct window *w;
11683 /* If called recursively during a menu update, do nothing. This can
11684 happen when, for instance, an activate-menubar-hook causes a
11685 redisplay. */
11686 if (inhibit_menubar_update)
11687 return hooks_run;
11689 window = FRAME_SELECTED_WINDOW (f);
11690 w = XWINDOW (window);
11692 if (FRAME_WINDOW_P (f)
11694 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11695 || defined (HAVE_NS) || defined (USE_GTK)
11696 FRAME_EXTERNAL_MENU_BAR (f)
11697 #else
11698 FRAME_MENU_BAR_LINES (f) > 0
11699 #endif
11700 : FRAME_MENU_BAR_LINES (f) > 0)
11702 /* If the user has switched buffers or windows, we need to
11703 recompute to reflect the new bindings. But we'll
11704 recompute when update_mode_lines is set too; that means
11705 that people can use force-mode-line-update to request
11706 that the menu bar be recomputed. The adverse effect on
11707 the rest of the redisplay algorithm is about the same as
11708 windows_or_buffers_changed anyway. */
11709 if (windows_or_buffers_changed
11710 /* This used to test w->update_mode_line, but we believe
11711 there is no need to recompute the menu in that case. */
11712 || update_mode_lines
11713 || window_buffer_changed (w))
11715 struct buffer *prev = current_buffer;
11716 ptrdiff_t count = SPECPDL_INDEX ();
11718 specbind (Qinhibit_menubar_update, Qt);
11720 set_buffer_internal_1 (XBUFFER (w->contents));
11721 if (save_match_data)
11722 record_unwind_save_match_data ();
11723 if (NILP (Voverriding_local_map_menu_flag))
11725 specbind (Qoverriding_terminal_local_map, Qnil);
11726 specbind (Qoverriding_local_map, Qnil);
11729 if (!hooks_run)
11731 /* Run the Lucid hook. */
11732 safe_run_hooks (Qactivate_menubar_hook);
11734 /* If it has changed current-menubar from previous value,
11735 really recompute the menu-bar from the value. */
11736 if (! NILP (Vlucid_menu_bar_dirty_flag))
11737 call0 (Qrecompute_lucid_menubar);
11739 safe_run_hooks (Qmenu_bar_update_hook);
11741 hooks_run = true;
11744 XSETFRAME (Vmenu_updating_frame, f);
11745 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11747 /* Redisplay the menu bar in case we changed it. */
11748 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11749 || defined (HAVE_NS) || defined (USE_GTK)
11750 if (FRAME_WINDOW_P (f))
11752 #if defined (HAVE_NS)
11753 /* All frames on Mac OS share the same menubar. So only
11754 the selected frame should be allowed to set it. */
11755 if (f == SELECTED_FRAME ())
11756 #endif
11757 set_frame_menubar (f, false, false);
11759 else
11760 /* On a terminal screen, the menu bar is an ordinary screen
11761 line, and this makes it get updated. */
11762 w->update_mode_line = true;
11763 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11764 /* In the non-toolkit version, the menu bar is an ordinary screen
11765 line, and this makes it get updated. */
11766 w->update_mode_line = true;
11767 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11769 unbind_to (count, Qnil);
11770 set_buffer_internal_1 (prev);
11774 return hooks_run;
11777 /***********************************************************************
11778 Tool-bars
11779 ***********************************************************************/
11781 #ifdef HAVE_WINDOW_SYSTEM
11783 /* Select `frame' temporarily without running all the code in
11784 do_switch_frame.
11785 FIXME: Maybe do_switch_frame should be trimmed down similarly
11786 when `norecord' is set. */
11787 static void
11788 fast_set_selected_frame (Lisp_Object frame)
11790 if (!EQ (selected_frame, frame))
11792 selected_frame = frame;
11793 selected_window = XFRAME (frame)->selected_window;
11797 /* Update the tool-bar item list for frame F. This has to be done
11798 before we start to fill in any display lines. Called from
11799 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11800 and restore it here. */
11802 static void
11803 update_tool_bar (struct frame *f, bool save_match_data)
11805 #if defined (USE_GTK) || defined (HAVE_NS)
11806 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11807 #else
11808 bool do_update = (WINDOWP (f->tool_bar_window)
11809 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11810 #endif
11812 if (do_update)
11814 Lisp_Object window;
11815 struct window *w;
11817 window = FRAME_SELECTED_WINDOW (f);
11818 w = XWINDOW (window);
11820 /* If the user has switched buffers or windows, we need to
11821 recompute to reflect the new bindings. But we'll
11822 recompute when update_mode_lines is set too; that means
11823 that people can use force-mode-line-update to request
11824 that the menu bar be recomputed. The adverse effect on
11825 the rest of the redisplay algorithm is about the same as
11826 windows_or_buffers_changed anyway. */
11827 if (windows_or_buffers_changed
11828 || w->update_mode_line
11829 || update_mode_lines
11830 || window_buffer_changed (w))
11832 struct buffer *prev = current_buffer;
11833 ptrdiff_t count = SPECPDL_INDEX ();
11834 Lisp_Object frame, new_tool_bar;
11835 int new_n_tool_bar;
11836 struct gcpro gcpro1;
11838 /* Set current_buffer to the buffer of the selected
11839 window of the frame, so that we get the right local
11840 keymaps. */
11841 set_buffer_internal_1 (XBUFFER (w->contents));
11843 /* Save match data, if we must. */
11844 if (save_match_data)
11845 record_unwind_save_match_data ();
11847 /* Make sure that we don't accidentally use bogus keymaps. */
11848 if (NILP (Voverriding_local_map_menu_flag))
11850 specbind (Qoverriding_terminal_local_map, Qnil);
11851 specbind (Qoverriding_local_map, Qnil);
11854 GCPRO1 (new_tool_bar);
11856 /* We must temporarily set the selected frame to this frame
11857 before calling tool_bar_items, because the calculation of
11858 the tool-bar keymap uses the selected frame (see
11859 `tool-bar-make-keymap' in tool-bar.el). */
11860 eassert (EQ (selected_window,
11861 /* Since we only explicitly preserve selected_frame,
11862 check that selected_window would be redundant. */
11863 XFRAME (selected_frame)->selected_window));
11864 record_unwind_protect (fast_set_selected_frame, selected_frame);
11865 XSETFRAME (frame, f);
11866 fast_set_selected_frame (frame);
11868 /* Build desired tool-bar items from keymaps. */
11869 new_tool_bar
11870 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11871 &new_n_tool_bar);
11873 /* Redisplay the tool-bar if we changed it. */
11874 if (new_n_tool_bar != f->n_tool_bar_items
11875 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11877 /* Redisplay that happens asynchronously due to an expose event
11878 may access f->tool_bar_items. Make sure we update both
11879 variables within BLOCK_INPUT so no such event interrupts. */
11880 block_input ();
11881 fset_tool_bar_items (f, new_tool_bar);
11882 f->n_tool_bar_items = new_n_tool_bar;
11883 w->update_mode_line = true;
11884 unblock_input ();
11887 UNGCPRO;
11889 unbind_to (count, Qnil);
11890 set_buffer_internal_1 (prev);
11895 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11897 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11898 F's desired tool-bar contents. F->tool_bar_items must have
11899 been set up previously by calling prepare_menu_bars. */
11901 static void
11902 build_desired_tool_bar_string (struct frame *f)
11904 int i, size, size_needed;
11905 struct gcpro gcpro1, gcpro2;
11906 Lisp_Object image, plist;
11908 image = plist = Qnil;
11909 GCPRO2 (image, plist);
11911 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11912 Otherwise, make a new string. */
11914 /* The size of the string we might be able to reuse. */
11915 size = (STRINGP (f->desired_tool_bar_string)
11916 ? SCHARS (f->desired_tool_bar_string)
11917 : 0);
11919 /* We need one space in the string for each image. */
11920 size_needed = f->n_tool_bar_items;
11922 /* Reuse f->desired_tool_bar_string, if possible. */
11923 if (size < size_needed || NILP (f->desired_tool_bar_string))
11924 fset_desired_tool_bar_string
11925 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11926 else
11928 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11929 struct gcpro gcpro1;
11930 GCPRO1 (props);
11931 Fremove_text_properties (make_number (0), make_number (size),
11932 props, f->desired_tool_bar_string);
11933 UNGCPRO;
11936 /* Put a `display' property on the string for the images to display,
11937 put a `menu_item' property on tool-bar items with a value that
11938 is the index of the item in F's tool-bar item vector. */
11939 for (i = 0; i < f->n_tool_bar_items; ++i)
11941 #define PROP(IDX) \
11942 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11944 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11945 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11946 int hmargin, vmargin, relief, idx, end;
11948 /* If image is a vector, choose the image according to the
11949 button state. */
11950 image = PROP (TOOL_BAR_ITEM_IMAGES);
11951 if (VECTORP (image))
11953 if (enabled_p)
11954 idx = (selected_p
11955 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11956 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11957 else
11958 idx = (selected_p
11959 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11960 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11962 eassert (ASIZE (image) >= idx);
11963 image = AREF (image, idx);
11965 else
11966 idx = -1;
11968 /* Ignore invalid image specifications. */
11969 if (!valid_image_p (image))
11970 continue;
11972 /* Display the tool-bar button pressed, or depressed. */
11973 plist = Fcopy_sequence (XCDR (image));
11975 /* Compute margin and relief to draw. */
11976 relief = (tool_bar_button_relief >= 0
11977 ? tool_bar_button_relief
11978 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11979 hmargin = vmargin = relief;
11981 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11982 INT_MAX - max (hmargin, vmargin)))
11984 hmargin += XFASTINT (Vtool_bar_button_margin);
11985 vmargin += XFASTINT (Vtool_bar_button_margin);
11987 else if (CONSP (Vtool_bar_button_margin))
11989 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11990 INT_MAX - hmargin))
11991 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11993 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11994 INT_MAX - vmargin))
11995 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11998 if (auto_raise_tool_bar_buttons_p)
12000 /* Add a `:relief' property to the image spec if the item is
12001 selected. */
12002 if (selected_p)
12004 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12005 hmargin -= relief;
12006 vmargin -= relief;
12009 else
12011 /* If image is selected, display it pressed, i.e. with a
12012 negative relief. If it's not selected, display it with a
12013 raised relief. */
12014 plist = Fplist_put (plist, QCrelief,
12015 (selected_p
12016 ? make_number (-relief)
12017 : make_number (relief)));
12018 hmargin -= relief;
12019 vmargin -= relief;
12022 /* Put a margin around the image. */
12023 if (hmargin || vmargin)
12025 if (hmargin == vmargin)
12026 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12027 else
12028 plist = Fplist_put (plist, QCmargin,
12029 Fcons (make_number (hmargin),
12030 make_number (vmargin)));
12033 /* If button is not enabled, and we don't have special images
12034 for the disabled state, make the image appear disabled by
12035 applying an appropriate algorithm to it. */
12036 if (!enabled_p && idx < 0)
12037 plist = Fplist_put (plist, QCconversion, Qdisabled);
12039 /* Put a `display' text property on the string for the image to
12040 display. Put a `menu-item' property on the string that gives
12041 the start of this item's properties in the tool-bar items
12042 vector. */
12043 image = Fcons (Qimage, plist);
12044 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12045 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12046 struct gcpro gcpro1;
12047 GCPRO1 (props);
12049 /* Let the last image hide all remaining spaces in the tool bar
12050 string. The string can be longer than needed when we reuse a
12051 previous string. */
12052 if (i + 1 == f->n_tool_bar_items)
12053 end = SCHARS (f->desired_tool_bar_string);
12054 else
12055 end = i + 1;
12056 Fadd_text_properties (make_number (i), make_number (end),
12057 props, f->desired_tool_bar_string);
12058 UNGCPRO;
12059 #undef PROP
12062 UNGCPRO;
12066 /* Display one line of the tool-bar of frame IT->f.
12068 HEIGHT specifies the desired height of the tool-bar line.
12069 If the actual height of the glyph row is less than HEIGHT, the
12070 row's height is increased to HEIGHT, and the icons are centered
12071 vertically in the new height.
12073 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12074 count a final empty row in case the tool-bar width exactly matches
12075 the window width.
12078 static void
12079 display_tool_bar_line (struct it *it, int height)
12081 struct glyph_row *row = it->glyph_row;
12082 int max_x = it->last_visible_x;
12083 struct glyph *last;
12085 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12086 clear_glyph_row (row);
12087 row->enabled_p = true;
12088 row->y = it->current_y;
12090 /* Note that this isn't made use of if the face hasn't a box,
12091 so there's no need to check the face here. */
12092 it->start_of_box_run_p = true;
12094 while (it->current_x < max_x)
12096 int x, n_glyphs_before, i, nglyphs;
12097 struct it it_before;
12099 /* Get the next display element. */
12100 if (!get_next_display_element (it))
12102 /* Don't count empty row if we are counting needed tool-bar lines. */
12103 if (height < 0 && !it->hpos)
12104 return;
12105 break;
12108 /* Produce glyphs. */
12109 n_glyphs_before = row->used[TEXT_AREA];
12110 it_before = *it;
12112 PRODUCE_GLYPHS (it);
12114 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12115 i = 0;
12116 x = it_before.current_x;
12117 while (i < nglyphs)
12119 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12121 if (x + glyph->pixel_width > max_x)
12123 /* Glyph doesn't fit on line. Backtrack. */
12124 row->used[TEXT_AREA] = n_glyphs_before;
12125 *it = it_before;
12126 /* If this is the only glyph on this line, it will never fit on the
12127 tool-bar, so skip it. But ensure there is at least one glyph,
12128 so we don't accidentally disable the tool-bar. */
12129 if (n_glyphs_before == 0
12130 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12131 break;
12132 goto out;
12135 ++it->hpos;
12136 x += glyph->pixel_width;
12137 ++i;
12140 /* Stop at line end. */
12141 if (ITERATOR_AT_END_OF_LINE_P (it))
12142 break;
12144 set_iterator_to_next (it, true);
12147 out:;
12149 row->displays_text_p = row->used[TEXT_AREA] != 0;
12151 /* Use default face for the border below the tool bar.
12153 FIXME: When auto-resize-tool-bars is grow-only, there is
12154 no additional border below the possibly empty tool-bar lines.
12155 So to make the extra empty lines look "normal", we have to
12156 use the tool-bar face for the border too. */
12157 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12158 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12159 it->face_id = DEFAULT_FACE_ID;
12161 extend_face_to_end_of_line (it);
12162 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12163 last->right_box_line_p = true;
12164 if (last == row->glyphs[TEXT_AREA])
12165 last->left_box_line_p = true;
12167 /* Make line the desired height and center it vertically. */
12168 if ((height -= it->max_ascent + it->max_descent) > 0)
12170 /* Don't add more than one line height. */
12171 height %= FRAME_LINE_HEIGHT (it->f);
12172 it->max_ascent += height / 2;
12173 it->max_descent += (height + 1) / 2;
12176 compute_line_metrics (it);
12178 /* If line is empty, make it occupy the rest of the tool-bar. */
12179 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12181 row->height = row->phys_height = it->last_visible_y - row->y;
12182 row->visible_height = row->height;
12183 row->ascent = row->phys_ascent = 0;
12184 row->extra_line_spacing = 0;
12187 row->full_width_p = true;
12188 row->continued_p = false;
12189 row->truncated_on_left_p = false;
12190 row->truncated_on_right_p = false;
12192 it->current_x = it->hpos = 0;
12193 it->current_y += row->height;
12194 ++it->vpos;
12195 ++it->glyph_row;
12199 /* Value is the number of pixels needed to make all tool-bar items of
12200 frame F visible. The actual number of glyph rows needed is
12201 returned in *N_ROWS if non-NULL. */
12202 static int
12203 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12205 struct window *w = XWINDOW (f->tool_bar_window);
12206 struct it it;
12207 /* tool_bar_height is called from redisplay_tool_bar after building
12208 the desired matrix, so use (unused) mode-line row as temporary row to
12209 avoid destroying the first tool-bar row. */
12210 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12212 /* Initialize an iterator for iteration over
12213 F->desired_tool_bar_string in the tool-bar window of frame F. */
12214 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12215 temp_row->reversed_p = false;
12216 it.first_visible_x = 0;
12217 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12218 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12219 it.paragraph_embedding = L2R;
12221 while (!ITERATOR_AT_END_P (&it))
12223 clear_glyph_row (temp_row);
12224 it.glyph_row = temp_row;
12225 display_tool_bar_line (&it, -1);
12227 clear_glyph_row (temp_row);
12229 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12230 if (n_rows)
12231 *n_rows = it.vpos > 0 ? it.vpos : -1;
12233 if (pixelwise)
12234 return it.current_y;
12235 else
12236 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12239 #endif /* !USE_GTK && !HAVE_NS */
12241 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12242 0, 2, 0,
12243 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12244 If FRAME is nil or omitted, use the selected frame. Optional argument
12245 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12246 (Lisp_Object frame, Lisp_Object pixelwise)
12248 int height = 0;
12250 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12251 struct frame *f = decode_any_frame (frame);
12253 if (WINDOWP (f->tool_bar_window)
12254 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12256 update_tool_bar (f, true);
12257 if (f->n_tool_bar_items)
12259 build_desired_tool_bar_string (f);
12260 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12263 #endif
12265 return make_number (height);
12269 /* Display the tool-bar of frame F. Value is true if tool-bar's
12270 height should be changed. */
12271 static bool
12272 redisplay_tool_bar (struct frame *f)
12274 #if defined (USE_GTK) || defined (HAVE_NS)
12276 if (FRAME_EXTERNAL_TOOL_BAR (f))
12277 update_frame_tool_bar (f);
12278 return false;
12280 #else /* !USE_GTK && !HAVE_NS */
12282 struct window *w;
12283 struct it it;
12284 struct glyph_row *row;
12286 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12287 do anything. This means you must start with tool-bar-lines
12288 non-zero to get the auto-sizing effect. Or in other words, you
12289 can turn off tool-bars by specifying tool-bar-lines zero. */
12290 if (!WINDOWP (f->tool_bar_window)
12291 || (w = XWINDOW (f->tool_bar_window),
12292 WINDOW_TOTAL_LINES (w) == 0))
12293 return false;
12295 /* Set up an iterator for the tool-bar window. */
12296 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12297 it.first_visible_x = 0;
12298 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12299 row = it.glyph_row;
12300 row->reversed_p = false;
12302 /* Build a string that represents the contents of the tool-bar. */
12303 build_desired_tool_bar_string (f);
12304 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12305 /* FIXME: This should be controlled by a user option. But it
12306 doesn't make sense to have an R2L tool bar if the menu bar cannot
12307 be drawn also R2L, and making the menu bar R2L is tricky due
12308 toolkit-specific code that implements it. If an R2L tool bar is
12309 ever supported, display_tool_bar_line should also be augmented to
12310 call unproduce_glyphs like display_line and display_string
12311 do. */
12312 it.paragraph_embedding = L2R;
12314 if (f->n_tool_bar_rows == 0)
12316 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12318 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12320 x_change_tool_bar_height (f, new_height);
12321 frame_default_tool_bar_height = new_height;
12322 /* Always do that now. */
12323 clear_glyph_matrix (w->desired_matrix);
12324 f->fonts_changed = true;
12325 return true;
12329 /* Display as many lines as needed to display all tool-bar items. */
12331 if (f->n_tool_bar_rows > 0)
12333 int border, rows, height, extra;
12335 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12336 border = XINT (Vtool_bar_border);
12337 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12338 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12339 else if (EQ (Vtool_bar_border, Qborder_width))
12340 border = f->border_width;
12341 else
12342 border = 0;
12343 if (border < 0)
12344 border = 0;
12346 rows = f->n_tool_bar_rows;
12347 height = max (1, (it.last_visible_y - border) / rows);
12348 extra = it.last_visible_y - border - height * rows;
12350 while (it.current_y < it.last_visible_y)
12352 int h = 0;
12353 if (extra > 0 && rows-- > 0)
12355 h = (extra + rows - 1) / rows;
12356 extra -= h;
12358 display_tool_bar_line (&it, height + h);
12361 else
12363 while (it.current_y < it.last_visible_y)
12364 display_tool_bar_line (&it, 0);
12367 /* It doesn't make much sense to try scrolling in the tool-bar
12368 window, so don't do it. */
12369 w->desired_matrix->no_scrolling_p = true;
12370 w->must_be_updated_p = true;
12372 if (!NILP (Vauto_resize_tool_bars))
12374 bool change_height_p = true;
12376 /* If we couldn't display everything, change the tool-bar's
12377 height if there is room for more. */
12378 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12379 change_height_p = true;
12381 /* We subtract 1 because display_tool_bar_line advances the
12382 glyph_row pointer before returning to its caller. We want to
12383 examine the last glyph row produced by
12384 display_tool_bar_line. */
12385 row = it.glyph_row - 1;
12387 /* If there are blank lines at the end, except for a partially
12388 visible blank line at the end that is smaller than
12389 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12390 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12391 && row->height >= FRAME_LINE_HEIGHT (f))
12392 change_height_p = true;
12394 /* If row displays tool-bar items, but is partially visible,
12395 change the tool-bar's height. */
12396 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12397 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12398 change_height_p = true;
12400 /* Resize windows as needed by changing the `tool-bar-lines'
12401 frame parameter. */
12402 if (change_height_p)
12404 int nrows;
12405 int new_height = tool_bar_height (f, &nrows, true);
12407 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12408 && !f->minimize_tool_bar_window_p)
12409 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12410 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12411 f->minimize_tool_bar_window_p = false;
12413 if (change_height_p)
12415 x_change_tool_bar_height (f, new_height);
12416 frame_default_tool_bar_height = new_height;
12417 clear_glyph_matrix (w->desired_matrix);
12418 f->n_tool_bar_rows = nrows;
12419 f->fonts_changed = true;
12421 return true;
12426 f->minimize_tool_bar_window_p = false;
12427 return false;
12429 #endif /* USE_GTK || HAVE_NS */
12432 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12434 /* Get information about the tool-bar item which is displayed in GLYPH
12435 on frame F. Return in *PROP_IDX the index where tool-bar item
12436 properties start in F->tool_bar_items. Value is false if
12437 GLYPH doesn't display a tool-bar item. */
12439 static bool
12440 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12442 Lisp_Object prop;
12443 int charpos;
12445 /* This function can be called asynchronously, which means we must
12446 exclude any possibility that Fget_text_property signals an
12447 error. */
12448 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12449 charpos = max (0, charpos);
12451 /* Get the text property `menu-item' at pos. The value of that
12452 property is the start index of this item's properties in
12453 F->tool_bar_items. */
12454 prop = Fget_text_property (make_number (charpos),
12455 Qmenu_item, f->current_tool_bar_string);
12456 if (! INTEGERP (prop))
12457 return false;
12458 *prop_idx = XINT (prop);
12459 return true;
12463 /* Get information about the tool-bar item at position X/Y on frame F.
12464 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12465 the current matrix of the tool-bar window of F, or NULL if not
12466 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12467 item in F->tool_bar_items. Value is
12469 -1 if X/Y is not on a tool-bar item
12470 0 if X/Y is on the same item that was highlighted before.
12471 1 otherwise. */
12473 static int
12474 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12475 int *hpos, int *vpos, int *prop_idx)
12477 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12478 struct window *w = XWINDOW (f->tool_bar_window);
12479 int area;
12481 /* Find the glyph under X/Y. */
12482 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12483 if (*glyph == NULL)
12484 return -1;
12486 /* Get the start of this tool-bar item's properties in
12487 f->tool_bar_items. */
12488 if (!tool_bar_item_info (f, *glyph, prop_idx))
12489 return -1;
12491 /* Is mouse on the highlighted item? */
12492 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12493 && *vpos >= hlinfo->mouse_face_beg_row
12494 && *vpos <= hlinfo->mouse_face_end_row
12495 && (*vpos > hlinfo->mouse_face_beg_row
12496 || *hpos >= hlinfo->mouse_face_beg_col)
12497 && (*vpos < hlinfo->mouse_face_end_row
12498 || *hpos < hlinfo->mouse_face_end_col
12499 || hlinfo->mouse_face_past_end))
12500 return 0;
12502 return 1;
12506 /* EXPORT:
12507 Handle mouse button event on the tool-bar of frame F, at
12508 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12509 false for button release. MODIFIERS is event modifiers for button
12510 release. */
12512 void
12513 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12514 int modifiers)
12516 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12517 struct window *w = XWINDOW (f->tool_bar_window);
12518 int hpos, vpos, prop_idx;
12519 struct glyph *glyph;
12520 Lisp_Object enabled_p;
12521 int ts;
12523 /* If not on the highlighted tool-bar item, and mouse-highlight is
12524 non-nil, return. This is so we generate the tool-bar button
12525 click only when the mouse button is released on the same item as
12526 where it was pressed. However, when mouse-highlight is disabled,
12527 generate the click when the button is released regardless of the
12528 highlight, since tool-bar items are not highlighted in that
12529 case. */
12530 frame_to_window_pixel_xy (w, &x, &y);
12531 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12532 if (ts == -1
12533 || (ts != 0 && !NILP (Vmouse_highlight)))
12534 return;
12536 /* When mouse-highlight is off, generate the click for the item
12537 where the button was pressed, disregarding where it was
12538 released. */
12539 if (NILP (Vmouse_highlight) && !down_p)
12540 prop_idx = f->last_tool_bar_item;
12542 /* If item is disabled, do nothing. */
12543 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12544 if (NILP (enabled_p))
12545 return;
12547 if (down_p)
12549 /* Show item in pressed state. */
12550 if (!NILP (Vmouse_highlight))
12551 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12552 f->last_tool_bar_item = prop_idx;
12554 else
12556 Lisp_Object key, frame;
12557 struct input_event event;
12558 EVENT_INIT (event);
12560 /* Show item in released state. */
12561 if (!NILP (Vmouse_highlight))
12562 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12564 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12566 XSETFRAME (frame, f);
12567 event.kind = TOOL_BAR_EVENT;
12568 event.frame_or_window = frame;
12569 event.arg = frame;
12570 kbd_buffer_store_event (&event);
12572 event.kind = TOOL_BAR_EVENT;
12573 event.frame_or_window = frame;
12574 event.arg = key;
12575 event.modifiers = modifiers;
12576 kbd_buffer_store_event (&event);
12577 f->last_tool_bar_item = -1;
12582 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12583 tool-bar window-relative coordinates X/Y. Called from
12584 note_mouse_highlight. */
12586 static void
12587 note_tool_bar_highlight (struct frame *f, int x, int y)
12589 Lisp_Object window = f->tool_bar_window;
12590 struct window *w = XWINDOW (window);
12591 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12593 int hpos, vpos;
12594 struct glyph *glyph;
12595 struct glyph_row *row;
12596 int i;
12597 Lisp_Object enabled_p;
12598 int prop_idx;
12599 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12600 bool mouse_down_p;
12601 int rc;
12603 /* Function note_mouse_highlight is called with negative X/Y
12604 values when mouse moves outside of the frame. */
12605 if (x <= 0 || y <= 0)
12607 clear_mouse_face (hlinfo);
12608 return;
12611 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12612 if (rc < 0)
12614 /* Not on tool-bar item. */
12615 clear_mouse_face (hlinfo);
12616 return;
12618 else if (rc == 0)
12619 /* On same tool-bar item as before. */
12620 goto set_help_echo;
12622 clear_mouse_face (hlinfo);
12624 /* Mouse is down, but on different tool-bar item? */
12625 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12626 && f == dpyinfo->last_mouse_frame);
12628 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12629 return;
12631 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12633 /* If tool-bar item is not enabled, don't highlight it. */
12634 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12635 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12637 /* Compute the x-position of the glyph. In front and past the
12638 image is a space. We include this in the highlighted area. */
12639 row = MATRIX_ROW (w->current_matrix, vpos);
12640 for (i = x = 0; i < hpos; ++i)
12641 x += row->glyphs[TEXT_AREA][i].pixel_width;
12643 /* Record this as the current active region. */
12644 hlinfo->mouse_face_beg_col = hpos;
12645 hlinfo->mouse_face_beg_row = vpos;
12646 hlinfo->mouse_face_beg_x = x;
12647 hlinfo->mouse_face_past_end = false;
12649 hlinfo->mouse_face_end_col = hpos + 1;
12650 hlinfo->mouse_face_end_row = vpos;
12651 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12652 hlinfo->mouse_face_window = window;
12653 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12655 /* Display it as active. */
12656 show_mouse_face (hlinfo, draw);
12659 set_help_echo:
12661 /* Set help_echo_string to a help string to display for this tool-bar item.
12662 XTread_socket does the rest. */
12663 help_echo_object = help_echo_window = Qnil;
12664 help_echo_pos = -1;
12665 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12666 if (NILP (help_echo_string))
12667 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12670 #endif /* !USE_GTK && !HAVE_NS */
12672 #endif /* HAVE_WINDOW_SYSTEM */
12676 /************************************************************************
12677 Horizontal scrolling
12678 ************************************************************************/
12680 /* For all leaf windows in the window tree rooted at WINDOW, set their
12681 hscroll value so that PT is (i) visible in the window, and (ii) so
12682 that it is not within a certain margin at the window's left and
12683 right border. Value is true if any window's hscroll has been
12684 changed. */
12686 static bool
12687 hscroll_window_tree (Lisp_Object window)
12689 bool hscrolled_p = false;
12690 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12691 int hscroll_step_abs = 0;
12692 double hscroll_step_rel = 0;
12694 if (hscroll_relative_p)
12696 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12697 if (hscroll_step_rel < 0)
12699 hscroll_relative_p = false;
12700 hscroll_step_abs = 0;
12703 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12705 hscroll_step_abs = XINT (Vhscroll_step);
12706 if (hscroll_step_abs < 0)
12707 hscroll_step_abs = 0;
12709 else
12710 hscroll_step_abs = 0;
12712 while (WINDOWP (window))
12714 struct window *w = XWINDOW (window);
12716 if (WINDOWP (w->contents))
12717 hscrolled_p |= hscroll_window_tree (w->contents);
12718 else if (w->cursor.vpos >= 0)
12720 int h_margin;
12721 int text_area_width;
12722 struct glyph_row *cursor_row;
12723 struct glyph_row *bottom_row;
12725 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12726 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12727 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12728 else
12729 cursor_row = bottom_row - 1;
12731 if (!cursor_row->enabled_p)
12733 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12734 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12735 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12736 else
12737 cursor_row = bottom_row - 1;
12739 bool row_r2l_p = cursor_row->reversed_p;
12741 text_area_width = window_box_width (w, TEXT_AREA);
12743 /* Scroll when cursor is inside this scroll margin. */
12744 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12746 /* If the position of this window's point has explicitly
12747 changed, no more suspend auto hscrolling. */
12748 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12749 w->suspend_auto_hscroll = false;
12751 /* Remember window point. */
12752 Fset_marker (w->old_pointm,
12753 ((w == XWINDOW (selected_window))
12754 ? make_number (BUF_PT (XBUFFER (w->contents)))
12755 : Fmarker_position (w->pointm)),
12756 w->contents);
12758 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12759 && !w->suspend_auto_hscroll
12760 /* In some pathological cases, like restoring a window
12761 configuration into a frame that is much smaller than
12762 the one from which the configuration was saved, we
12763 get glyph rows whose start and end have zero buffer
12764 positions, which we cannot handle below. Just skip
12765 such windows. */
12766 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12767 /* For left-to-right rows, hscroll when cursor is either
12768 (i) inside the right hscroll margin, or (ii) if it is
12769 inside the left margin and the window is already
12770 hscrolled. */
12771 && ((!row_r2l_p
12772 && ((w->hscroll && w->cursor.x <= h_margin)
12773 || (cursor_row->enabled_p
12774 && cursor_row->truncated_on_right_p
12775 && (w->cursor.x >= text_area_width - h_margin))))
12776 /* For right-to-left rows, the logic is similar,
12777 except that rules for scrolling to left and right
12778 are reversed. E.g., if cursor.x <= h_margin, we
12779 need to hscroll "to the right" unconditionally,
12780 and that will scroll the screen to the left so as
12781 to reveal the next portion of the row. */
12782 || (row_r2l_p
12783 && ((cursor_row->enabled_p
12784 /* FIXME: It is confusing to set the
12785 truncated_on_right_p flag when R2L rows
12786 are actually truncated on the left. */
12787 && cursor_row->truncated_on_right_p
12788 && w->cursor.x <= h_margin)
12789 || (w->hscroll
12790 && (w->cursor.x >= text_area_width - h_margin))))))
12792 struct it it;
12793 ptrdiff_t hscroll;
12794 struct buffer *saved_current_buffer;
12795 ptrdiff_t pt;
12796 int wanted_x;
12798 /* Find point in a display of infinite width. */
12799 saved_current_buffer = current_buffer;
12800 current_buffer = XBUFFER (w->contents);
12802 if (w == XWINDOW (selected_window))
12803 pt = PT;
12804 else
12805 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12807 /* Move iterator to pt starting at cursor_row->start in
12808 a line with infinite width. */
12809 init_to_row_start (&it, w, cursor_row);
12810 it.last_visible_x = INFINITY;
12811 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12812 current_buffer = saved_current_buffer;
12814 /* Position cursor in window. */
12815 if (!hscroll_relative_p && hscroll_step_abs == 0)
12816 hscroll = max (0, (it.current_x
12817 - (ITERATOR_AT_END_OF_LINE_P (&it)
12818 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12819 : (text_area_width / 2))))
12820 / FRAME_COLUMN_WIDTH (it.f);
12821 else if ((!row_r2l_p
12822 && w->cursor.x >= text_area_width - h_margin)
12823 || (row_r2l_p && w->cursor.x <= h_margin))
12825 if (hscroll_relative_p)
12826 wanted_x = text_area_width * (1 - hscroll_step_rel)
12827 - h_margin;
12828 else
12829 wanted_x = text_area_width
12830 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12831 - h_margin;
12832 hscroll
12833 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12835 else
12837 if (hscroll_relative_p)
12838 wanted_x = text_area_width * hscroll_step_rel
12839 + h_margin;
12840 else
12841 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12842 + h_margin;
12843 hscroll
12844 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12846 hscroll = max (hscroll, w->min_hscroll);
12848 /* Don't prevent redisplay optimizations if hscroll
12849 hasn't changed, as it will unnecessarily slow down
12850 redisplay. */
12851 if (w->hscroll != hscroll)
12853 struct buffer *b = XBUFFER (w->contents);
12854 b->prevent_redisplay_optimizations_p = true;
12855 w->hscroll = hscroll;
12856 hscrolled_p = true;
12861 window = w->next;
12864 /* Value is true if hscroll of any leaf window has been changed. */
12865 return hscrolled_p;
12869 /* Set hscroll so that cursor is visible and not inside horizontal
12870 scroll margins for all windows in the tree rooted at WINDOW. See
12871 also hscroll_window_tree above. Value is true if any window's
12872 hscroll has been changed. If it has, desired matrices on the frame
12873 of WINDOW are cleared. */
12875 static bool
12876 hscroll_windows (Lisp_Object window)
12878 bool hscrolled_p = hscroll_window_tree (window);
12879 if (hscrolled_p)
12880 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12881 return hscrolled_p;
12886 /************************************************************************
12887 Redisplay
12888 ************************************************************************/
12890 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12891 This is sometimes handy to have in a debugger session. */
12893 #ifdef GLYPH_DEBUG
12895 /* First and last unchanged row for try_window_id. */
12897 static int debug_first_unchanged_at_end_vpos;
12898 static int debug_last_unchanged_at_beg_vpos;
12900 /* Delta vpos and y. */
12902 static int debug_dvpos, debug_dy;
12904 /* Delta in characters and bytes for try_window_id. */
12906 static ptrdiff_t debug_delta, debug_delta_bytes;
12908 /* Values of window_end_pos and window_end_vpos at the end of
12909 try_window_id. */
12911 static ptrdiff_t debug_end_vpos;
12913 /* Append a string to W->desired_matrix->method. FMT is a printf
12914 format string. If trace_redisplay_p is true also printf the
12915 resulting string to stderr. */
12917 static void debug_method_add (struct window *, char const *, ...)
12918 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12920 static void
12921 debug_method_add (struct window *w, char const *fmt, ...)
12923 void *ptr = w;
12924 char *method = w->desired_matrix->method;
12925 int len = strlen (method);
12926 int size = sizeof w->desired_matrix->method;
12927 int remaining = size - len - 1;
12928 va_list ap;
12930 if (len && remaining)
12932 method[len] = '|';
12933 --remaining, ++len;
12936 va_start (ap, fmt);
12937 vsnprintf (method + len, remaining + 1, fmt, ap);
12938 va_end (ap);
12940 if (trace_redisplay_p)
12941 fprintf (stderr, "%p (%s): %s\n",
12942 ptr,
12943 ((BUFFERP (w->contents)
12944 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12945 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12946 : "no buffer"),
12947 method + len);
12950 #endif /* GLYPH_DEBUG */
12953 /* Value is true if all changes in window W, which displays
12954 current_buffer, are in the text between START and END. START is a
12955 buffer position, END is given as a distance from Z. Used in
12956 redisplay_internal for display optimization. */
12958 static bool
12959 text_outside_line_unchanged_p (struct window *w,
12960 ptrdiff_t start, ptrdiff_t end)
12962 bool unchanged_p = true;
12964 /* If text or overlays have changed, see where. */
12965 if (window_outdated (w))
12967 /* Gap in the line? */
12968 if (GPT < start || Z - GPT < end)
12969 unchanged_p = false;
12971 /* Changes start in front of the line, or end after it? */
12972 if (unchanged_p
12973 && (BEG_UNCHANGED < start - 1
12974 || END_UNCHANGED < end))
12975 unchanged_p = false;
12977 /* If selective display, can't optimize if changes start at the
12978 beginning of the line. */
12979 if (unchanged_p
12980 && INTEGERP (BVAR (current_buffer, selective_display))
12981 && XINT (BVAR (current_buffer, selective_display)) > 0
12982 && (BEG_UNCHANGED < start || GPT <= start))
12983 unchanged_p = false;
12985 /* If there are overlays at the start or end of the line, these
12986 may have overlay strings with newlines in them. A change at
12987 START, for instance, may actually concern the display of such
12988 overlay strings as well, and they are displayed on different
12989 lines. So, quickly rule out this case. (For the future, it
12990 might be desirable to implement something more telling than
12991 just BEG/END_UNCHANGED.) */
12992 if (unchanged_p)
12994 if (BEG + BEG_UNCHANGED == start
12995 && overlay_touches_p (start))
12996 unchanged_p = false;
12997 if (END_UNCHANGED == end
12998 && overlay_touches_p (Z - end))
12999 unchanged_p = false;
13002 /* Under bidi reordering, adding or deleting a character in the
13003 beginning of a paragraph, before the first strong directional
13004 character, can change the base direction of the paragraph (unless
13005 the buffer specifies a fixed paragraph direction), which will
13006 require to redisplay the whole paragraph. It might be worthwhile
13007 to find the paragraph limits and widen the range of redisplayed
13008 lines to that, but for now just give up this optimization. */
13009 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13010 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13011 unchanged_p = false;
13014 return unchanged_p;
13018 /* Do a frame update, taking possible shortcuts into account. This is
13019 the main external entry point for redisplay.
13021 If the last redisplay displayed an echo area message and that message
13022 is no longer requested, we clear the echo area or bring back the
13023 mini-buffer if that is in use. */
13025 void
13026 redisplay (void)
13028 redisplay_internal ();
13032 static Lisp_Object
13033 overlay_arrow_string_or_property (Lisp_Object var)
13035 Lisp_Object val;
13037 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13038 return val;
13040 return Voverlay_arrow_string;
13043 /* Return true if there are any overlay-arrows in current_buffer. */
13044 static bool
13045 overlay_arrow_in_current_buffer_p (void)
13047 Lisp_Object vlist;
13049 for (vlist = Voverlay_arrow_variable_list;
13050 CONSP (vlist);
13051 vlist = XCDR (vlist))
13053 Lisp_Object var = XCAR (vlist);
13054 Lisp_Object val;
13056 if (!SYMBOLP (var))
13057 continue;
13058 val = find_symbol_value (var);
13059 if (MARKERP (val)
13060 && current_buffer == XMARKER (val)->buffer)
13061 return true;
13063 return false;
13067 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13068 has changed. */
13070 static bool
13071 overlay_arrows_changed_p (void)
13073 Lisp_Object vlist;
13075 for (vlist = Voverlay_arrow_variable_list;
13076 CONSP (vlist);
13077 vlist = XCDR (vlist))
13079 Lisp_Object var = XCAR (vlist);
13080 Lisp_Object val, pstr;
13082 if (!SYMBOLP (var))
13083 continue;
13084 val = find_symbol_value (var);
13085 if (!MARKERP (val))
13086 continue;
13087 if (! EQ (COERCE_MARKER (val),
13088 Fget (var, Qlast_arrow_position))
13089 || ! (pstr = overlay_arrow_string_or_property (var),
13090 EQ (pstr, Fget (var, Qlast_arrow_string))))
13091 return true;
13093 return false;
13096 /* Mark overlay arrows to be updated on next redisplay. */
13098 static void
13099 update_overlay_arrows (int up_to_date)
13101 Lisp_Object vlist;
13103 for (vlist = Voverlay_arrow_variable_list;
13104 CONSP (vlist);
13105 vlist = XCDR (vlist))
13107 Lisp_Object var = XCAR (vlist);
13109 if (!SYMBOLP (var))
13110 continue;
13112 if (up_to_date > 0)
13114 Lisp_Object val = find_symbol_value (var);
13115 Fput (var, Qlast_arrow_position,
13116 COERCE_MARKER (val));
13117 Fput (var, Qlast_arrow_string,
13118 overlay_arrow_string_or_property (var));
13120 else if (up_to_date < 0
13121 || !NILP (Fget (var, Qlast_arrow_position)))
13123 Fput (var, Qlast_arrow_position, Qt);
13124 Fput (var, Qlast_arrow_string, Qt);
13130 /* Return overlay arrow string to display at row.
13131 Return integer (bitmap number) for arrow bitmap in left fringe.
13132 Return nil if no overlay arrow. */
13134 static Lisp_Object
13135 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13137 Lisp_Object vlist;
13139 for (vlist = Voverlay_arrow_variable_list;
13140 CONSP (vlist);
13141 vlist = XCDR (vlist))
13143 Lisp_Object var = XCAR (vlist);
13144 Lisp_Object val;
13146 if (!SYMBOLP (var))
13147 continue;
13149 val = find_symbol_value (var);
13151 if (MARKERP (val)
13152 && current_buffer == XMARKER (val)->buffer
13153 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13155 if (FRAME_WINDOW_P (it->f)
13156 /* FIXME: if ROW->reversed_p is set, this should test
13157 the right fringe, not the left one. */
13158 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13160 #ifdef HAVE_WINDOW_SYSTEM
13161 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13163 int fringe_bitmap = lookup_fringe_bitmap (val);
13164 if (fringe_bitmap != 0)
13165 return make_number (fringe_bitmap);
13167 #endif
13168 return make_number (-1); /* Use default arrow bitmap. */
13170 return overlay_arrow_string_or_property (var);
13174 return Qnil;
13177 /* Return true if point moved out of or into a composition. Otherwise
13178 return false. PREV_BUF and PREV_PT are the last point buffer and
13179 position. BUF and PT are the current point buffer and position. */
13181 static bool
13182 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13183 struct buffer *buf, ptrdiff_t pt)
13185 ptrdiff_t start, end;
13186 Lisp_Object prop;
13187 Lisp_Object buffer;
13189 XSETBUFFER (buffer, buf);
13190 /* Check a composition at the last point if point moved within the
13191 same buffer. */
13192 if (prev_buf == buf)
13194 if (prev_pt == pt)
13195 /* Point didn't move. */
13196 return false;
13198 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13199 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13200 && composition_valid_p (start, end, prop)
13201 && start < prev_pt && end > prev_pt)
13202 /* The last point was within the composition. Return true iff
13203 point moved out of the composition. */
13204 return (pt <= start || pt >= end);
13207 /* Check a composition at the current point. */
13208 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13209 && find_composition (pt, -1, &start, &end, &prop, buffer)
13210 && composition_valid_p (start, end, prop)
13211 && start < pt && end > pt);
13214 /* Reconsider the clip changes of buffer which is displayed in W. */
13216 static void
13217 reconsider_clip_changes (struct window *w)
13219 struct buffer *b = XBUFFER (w->contents);
13221 if (b->clip_changed
13222 && w->window_end_valid
13223 && w->current_matrix->buffer == b
13224 && w->current_matrix->zv == BUF_ZV (b)
13225 && w->current_matrix->begv == BUF_BEGV (b))
13226 b->clip_changed = false;
13228 /* If display wasn't paused, and W is not a tool bar window, see if
13229 point has been moved into or out of a composition. In that case,
13230 set b->clip_changed to force updating the screen. If
13231 b->clip_changed has already been set, skip this check. */
13232 if (!b->clip_changed && w->window_end_valid)
13234 ptrdiff_t pt = (w == XWINDOW (selected_window)
13235 ? PT : marker_position (w->pointm));
13237 if ((w->current_matrix->buffer != b || pt != w->last_point)
13238 && check_point_in_composition (w->current_matrix->buffer,
13239 w->last_point, b, pt))
13240 b->clip_changed = true;
13244 static void
13245 propagate_buffer_redisplay (void)
13246 { /* Resetting b->text->redisplay is problematic!
13247 We can't just reset it in the case that some window that displays
13248 it has not been redisplayed; and such a window can stay
13249 unredisplayed for a long time if it's currently invisible.
13250 But we do want to reset it at the end of redisplay otherwise
13251 its displayed windows will keep being redisplayed over and over
13252 again.
13253 So we copy all b->text->redisplay flags up to their windows here,
13254 such that mark_window_display_accurate can safely reset
13255 b->text->redisplay. */
13256 Lisp_Object ws = window_list ();
13257 for (; CONSP (ws); ws = XCDR (ws))
13259 struct window *thisw = XWINDOW (XCAR (ws));
13260 struct buffer *thisb = XBUFFER (thisw->contents);
13261 if (thisb->text->redisplay)
13262 thisw->redisplay = true;
13266 #define STOP_POLLING \
13267 do { if (! polling_stopped_here) stop_polling (); \
13268 polling_stopped_here = true; } while (false)
13270 #define RESUME_POLLING \
13271 do { if (polling_stopped_here) start_polling (); \
13272 polling_stopped_here = false; } while (false)
13275 /* Perhaps in the future avoid recentering windows if it
13276 is not necessary; currently that causes some problems. */
13278 static void
13279 redisplay_internal (void)
13281 struct window *w = XWINDOW (selected_window);
13282 struct window *sw;
13283 struct frame *fr;
13284 bool pending;
13285 bool must_finish = false, match_p;
13286 struct text_pos tlbufpos, tlendpos;
13287 int number_of_visible_frames;
13288 ptrdiff_t count;
13289 struct frame *sf;
13290 bool polling_stopped_here = false;
13291 Lisp_Object tail, frame;
13293 /* True means redisplay has to consider all windows on all
13294 frames. False, only selected_window is considered. */
13295 bool consider_all_windows_p;
13297 /* True means redisplay has to redisplay the miniwindow. */
13298 bool update_miniwindow_p = false;
13300 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13302 /* No redisplay if running in batch mode or frame is not yet fully
13303 initialized, or redisplay is explicitly turned off by setting
13304 Vinhibit_redisplay. */
13305 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13306 || !NILP (Vinhibit_redisplay))
13307 return;
13309 /* Don't examine these until after testing Vinhibit_redisplay.
13310 When Emacs is shutting down, perhaps because its connection to
13311 X has dropped, we should not look at them at all. */
13312 fr = XFRAME (w->frame);
13313 sf = SELECTED_FRAME ();
13315 if (!fr->glyphs_initialized_p)
13316 return;
13318 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13319 if (popup_activated ())
13320 return;
13321 #endif
13323 /* I don't think this happens but let's be paranoid. */
13324 if (redisplaying_p)
13325 return;
13327 /* Record a function that clears redisplaying_p
13328 when we leave this function. */
13329 count = SPECPDL_INDEX ();
13330 record_unwind_protect_void (unwind_redisplay);
13331 redisplaying_p = true;
13332 specbind (Qinhibit_free_realized_faces, Qnil);
13334 /* Record this function, so it appears on the profiler's backtraces. */
13335 record_in_backtrace (Qredisplay_internal, 0, 0);
13337 FOR_EACH_FRAME (tail, frame)
13338 XFRAME (frame)->already_hscrolled_p = false;
13340 retry:
13341 /* Remember the currently selected window. */
13342 sw = w;
13344 pending = false;
13345 last_escape_glyph_frame = NULL;
13346 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13347 last_glyphless_glyph_frame = NULL;
13348 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13350 /* If face_change, init_iterator will free all realized faces, which
13351 includes the faces referenced from current matrices. So, we
13352 can't reuse current matrices in this case. */
13353 if (face_change)
13354 windows_or_buffers_changed = 47;
13356 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13357 && FRAME_TTY (sf)->previous_frame != sf)
13359 /* Since frames on a single ASCII terminal share the same
13360 display area, displaying a different frame means redisplay
13361 the whole thing. */
13362 SET_FRAME_GARBAGED (sf);
13363 #ifndef DOS_NT
13364 set_tty_color_mode (FRAME_TTY (sf), sf);
13365 #endif
13366 FRAME_TTY (sf)->previous_frame = sf;
13369 /* Set the visible flags for all frames. Do this before checking for
13370 resized or garbaged frames; they want to know if their frames are
13371 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13372 number_of_visible_frames = 0;
13374 FOR_EACH_FRAME (tail, frame)
13376 struct frame *f = XFRAME (frame);
13378 if (FRAME_VISIBLE_P (f))
13380 ++number_of_visible_frames;
13381 /* Adjust matrices for visible frames only. */
13382 if (f->fonts_changed)
13384 adjust_frame_glyphs (f);
13385 /* Disable all redisplay optimizations for this frame.
13386 This is because adjust_frame_glyphs resets the
13387 enabled_p flag for all glyph rows of all windows, so
13388 many optimizations will fail anyway, and some might
13389 fail to test that flag and do bogus things as
13390 result. */
13391 SET_FRAME_GARBAGED (f);
13392 f->fonts_changed = false;
13394 /* If cursor type has been changed on the frame
13395 other than selected, consider all frames. */
13396 if (f != sf && f->cursor_type_changed)
13397 update_mode_lines = 31;
13399 clear_desired_matrices (f);
13402 /* Notice any pending interrupt request to change frame size. */
13403 do_pending_window_change (true);
13405 /* do_pending_window_change could change the selected_window due to
13406 frame resizing which makes the selected window too small. */
13407 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13408 sw = w;
13410 /* Clear frames marked as garbaged. */
13411 clear_garbaged_frames ();
13413 /* Build menubar and tool-bar items. */
13414 if (NILP (Vmemory_full))
13415 prepare_menu_bars ();
13417 reconsider_clip_changes (w);
13419 /* In most cases selected window displays current buffer. */
13420 match_p = XBUFFER (w->contents) == current_buffer;
13421 if (match_p)
13423 /* Detect case that we need to write or remove a star in the mode line. */
13424 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13425 w->update_mode_line = true;
13427 if (mode_line_update_needed (w))
13428 w->update_mode_line = true;
13430 /* If reconsider_clip_changes above decided that the narrowing
13431 in the current buffer changed, make sure all other windows
13432 showing that buffer will be redisplayed. */
13433 if (current_buffer->clip_changed)
13434 bset_update_mode_line (current_buffer);
13437 /* Normally the message* functions will have already displayed and
13438 updated the echo area, but the frame may have been trashed, or
13439 the update may have been preempted, so display the echo area
13440 again here. Checking message_cleared_p captures the case that
13441 the echo area should be cleared. */
13442 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13443 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13444 || (message_cleared_p
13445 && minibuf_level == 0
13446 /* If the mini-window is currently selected, this means the
13447 echo-area doesn't show through. */
13448 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13450 bool window_height_changed_p = echo_area_display (false);
13452 if (message_cleared_p)
13453 update_miniwindow_p = true;
13455 must_finish = true;
13457 /* If we don't display the current message, don't clear the
13458 message_cleared_p flag, because, if we did, we wouldn't clear
13459 the echo area in the next redisplay which doesn't preserve
13460 the echo area. */
13461 if (!display_last_displayed_message_p)
13462 message_cleared_p = false;
13464 if (window_height_changed_p)
13466 windows_or_buffers_changed = 50;
13468 /* If window configuration was changed, frames may have been
13469 marked garbaged. Clear them or we will experience
13470 surprises wrt scrolling. */
13471 clear_garbaged_frames ();
13474 else if (EQ (selected_window, minibuf_window)
13475 && (current_buffer->clip_changed || window_outdated (w))
13476 && resize_mini_window (w, false))
13478 /* Resized active mini-window to fit the size of what it is
13479 showing if its contents might have changed. */
13480 must_finish = true;
13482 /* If window configuration was changed, frames may have been
13483 marked garbaged. Clear them or we will experience
13484 surprises wrt scrolling. */
13485 clear_garbaged_frames ();
13488 if (windows_or_buffers_changed && !update_mode_lines)
13489 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13490 only the windows's contents needs to be refreshed, or whether the
13491 mode-lines also need a refresh. */
13492 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13493 ? REDISPLAY_SOME : 32);
13495 /* If specs for an arrow have changed, do thorough redisplay
13496 to ensure we remove any arrow that should no longer exist. */
13497 if (overlay_arrows_changed_p ())
13498 /* Apparently, this is the only case where we update other windows,
13499 without updating other mode-lines. */
13500 windows_or_buffers_changed = 49;
13502 consider_all_windows_p = (update_mode_lines
13503 || windows_or_buffers_changed);
13505 #define AINC(a,i) \
13506 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13507 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13509 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13510 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13512 /* Optimize the case that only the line containing the cursor in the
13513 selected window has changed. Variables starting with this_ are
13514 set in display_line and record information about the line
13515 containing the cursor. */
13516 tlbufpos = this_line_start_pos;
13517 tlendpos = this_line_end_pos;
13518 if (!consider_all_windows_p
13519 && CHARPOS (tlbufpos) > 0
13520 && !w->update_mode_line
13521 && !current_buffer->clip_changed
13522 && !current_buffer->prevent_redisplay_optimizations_p
13523 && FRAME_VISIBLE_P (XFRAME (w->frame))
13524 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13525 && !XFRAME (w->frame)->cursor_type_changed
13526 /* Make sure recorded data applies to current buffer, etc. */
13527 && this_line_buffer == current_buffer
13528 && match_p
13529 && !w->force_start
13530 && !w->optional_new_start
13531 /* Point must be on the line that we have info recorded about. */
13532 && PT >= CHARPOS (tlbufpos)
13533 && PT <= Z - CHARPOS (tlendpos)
13534 /* All text outside that line, including its final newline,
13535 must be unchanged. */
13536 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13537 CHARPOS (tlendpos)))
13539 if (CHARPOS (tlbufpos) > BEGV
13540 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13541 && (CHARPOS (tlbufpos) == ZV
13542 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13543 /* Former continuation line has disappeared by becoming empty. */
13544 goto cancel;
13545 else if (window_outdated (w) || MINI_WINDOW_P (w))
13547 /* We have to handle the case of continuation around a
13548 wide-column character (see the comment in indent.c around
13549 line 1340).
13551 For instance, in the following case:
13553 -------- Insert --------
13554 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13555 J_I_ ==> J_I_ `^^' are cursors.
13556 ^^ ^^
13557 -------- --------
13559 As we have to redraw the line above, we cannot use this
13560 optimization. */
13562 struct it it;
13563 int line_height_before = this_line_pixel_height;
13565 /* Note that start_display will handle the case that the
13566 line starting at tlbufpos is a continuation line. */
13567 start_display (&it, w, tlbufpos);
13569 /* Implementation note: It this still necessary? */
13570 if (it.current_x != this_line_start_x)
13571 goto cancel;
13573 TRACE ((stderr, "trying display optimization 1\n"));
13574 w->cursor.vpos = -1;
13575 overlay_arrow_seen = false;
13576 it.vpos = this_line_vpos;
13577 it.current_y = this_line_y;
13578 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13579 display_line (&it);
13581 /* If line contains point, is not continued,
13582 and ends at same distance from eob as before, we win. */
13583 if (w->cursor.vpos >= 0
13584 /* Line is not continued, otherwise this_line_start_pos
13585 would have been set to 0 in display_line. */
13586 && CHARPOS (this_line_start_pos)
13587 /* Line ends as before. */
13588 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13589 /* Line has same height as before. Otherwise other lines
13590 would have to be shifted up or down. */
13591 && this_line_pixel_height == line_height_before)
13593 /* If this is not the window's last line, we must adjust
13594 the charstarts of the lines below. */
13595 if (it.current_y < it.last_visible_y)
13597 struct glyph_row *row
13598 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13599 ptrdiff_t delta, delta_bytes;
13601 /* We used to distinguish between two cases here,
13602 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13603 when the line ends in a newline or the end of the
13604 buffer's accessible portion. But both cases did
13605 the same, so they were collapsed. */
13606 delta = (Z
13607 - CHARPOS (tlendpos)
13608 - MATRIX_ROW_START_CHARPOS (row));
13609 delta_bytes = (Z_BYTE
13610 - BYTEPOS (tlendpos)
13611 - MATRIX_ROW_START_BYTEPOS (row));
13613 increment_matrix_positions (w->current_matrix,
13614 this_line_vpos + 1,
13615 w->current_matrix->nrows,
13616 delta, delta_bytes);
13619 /* If this row displays text now but previously didn't,
13620 or vice versa, w->window_end_vpos may have to be
13621 adjusted. */
13622 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13624 if (w->window_end_vpos < this_line_vpos)
13625 w->window_end_vpos = this_line_vpos;
13627 else if (w->window_end_vpos == this_line_vpos
13628 && this_line_vpos > 0)
13629 w->window_end_vpos = this_line_vpos - 1;
13630 w->window_end_valid = false;
13632 /* Update hint: No need to try to scroll in update_window. */
13633 w->desired_matrix->no_scrolling_p = true;
13635 #ifdef GLYPH_DEBUG
13636 *w->desired_matrix->method = 0;
13637 debug_method_add (w, "optimization 1");
13638 #endif
13639 #ifdef HAVE_WINDOW_SYSTEM
13640 update_window_fringes (w, false);
13641 #endif
13642 goto update;
13644 else
13645 goto cancel;
13647 else if (/* Cursor position hasn't changed. */
13648 PT == w->last_point
13649 /* Make sure the cursor was last displayed
13650 in this window. Otherwise we have to reposition it. */
13652 /* PXW: Must be converted to pixels, probably. */
13653 && 0 <= w->cursor.vpos
13654 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13656 if (!must_finish)
13658 do_pending_window_change (true);
13659 /* If selected_window changed, redisplay again. */
13660 if (WINDOWP (selected_window)
13661 && (w = XWINDOW (selected_window)) != sw)
13662 goto retry;
13664 /* We used to always goto end_of_redisplay here, but this
13665 isn't enough if we have a blinking cursor. */
13666 if (w->cursor_off_p == w->last_cursor_off_p)
13667 goto end_of_redisplay;
13669 goto update;
13671 /* If highlighting the region, or if the cursor is in the echo area,
13672 then we can't just move the cursor. */
13673 else if (NILP (Vshow_trailing_whitespace)
13674 && !cursor_in_echo_area)
13676 struct it it;
13677 struct glyph_row *row;
13679 /* Skip from tlbufpos to PT and see where it is. Note that
13680 PT may be in invisible text. If so, we will end at the
13681 next visible position. */
13682 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13683 NULL, DEFAULT_FACE_ID);
13684 it.current_x = this_line_start_x;
13685 it.current_y = this_line_y;
13686 it.vpos = this_line_vpos;
13688 /* The call to move_it_to stops in front of PT, but
13689 moves over before-strings. */
13690 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13692 if (it.vpos == this_line_vpos
13693 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13694 row->enabled_p))
13696 eassert (this_line_vpos == it.vpos);
13697 eassert (this_line_y == it.current_y);
13698 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13699 #ifdef GLYPH_DEBUG
13700 *w->desired_matrix->method = 0;
13701 debug_method_add (w, "optimization 3");
13702 #endif
13703 goto update;
13705 else
13706 goto cancel;
13709 cancel:
13710 /* Text changed drastically or point moved off of line. */
13711 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13714 CHARPOS (this_line_start_pos) = 0;
13715 ++clear_face_cache_count;
13716 #ifdef HAVE_WINDOW_SYSTEM
13717 ++clear_image_cache_count;
13718 #endif
13720 /* Build desired matrices, and update the display. If
13721 consider_all_windows_p, do it for all windows on all frames.
13722 Otherwise do it for selected_window, only. */
13724 if (consider_all_windows_p)
13726 FOR_EACH_FRAME (tail, frame)
13727 XFRAME (frame)->updated_p = false;
13729 propagate_buffer_redisplay ();
13731 FOR_EACH_FRAME (tail, frame)
13733 struct frame *f = XFRAME (frame);
13735 /* We don't have to do anything for unselected terminal
13736 frames. */
13737 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13738 && !EQ (FRAME_TTY (f)->top_frame, frame))
13739 continue;
13741 retry_frame:
13743 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13744 /* Redisplay internal tool bar if this is the first time so we
13745 can adjust the frame height right now, if necessary. */
13746 if (!f->tool_bar_redisplayed_once)
13748 if (redisplay_tool_bar (f))
13749 adjust_frame_glyphs (f);
13750 f->tool_bar_redisplayed_once = true;
13752 #endif
13754 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13756 bool gcscrollbars
13757 /* Only GC scrollbars when we redisplay the whole frame. */
13758 = f->redisplay || !REDISPLAY_SOME_P ();
13759 /* Mark all the scroll bars to be removed; we'll redeem
13760 the ones we want when we redisplay their windows. */
13761 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13762 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13764 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13765 redisplay_windows (FRAME_ROOT_WINDOW (f));
13766 /* Remember that the invisible frames need to be redisplayed next
13767 time they're visible. */
13768 else if (!REDISPLAY_SOME_P ())
13769 f->redisplay = true;
13771 /* The X error handler may have deleted that frame. */
13772 if (!FRAME_LIVE_P (f))
13773 continue;
13775 /* Any scroll bars which redisplay_windows should have
13776 nuked should now go away. */
13777 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13778 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13780 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13782 /* If fonts changed on visible frame, display again. */
13783 if (f->fonts_changed)
13785 adjust_frame_glyphs (f);
13786 /* Disable all redisplay optimizations for this
13787 frame. For the reasons, see the comment near
13788 the previous call to adjust_frame_glyphs above. */
13789 SET_FRAME_GARBAGED (f);
13790 f->fonts_changed = false;
13791 goto retry_frame;
13794 /* See if we have to hscroll. */
13795 if (!f->already_hscrolled_p)
13797 f->already_hscrolled_p = true;
13798 if (hscroll_windows (f->root_window))
13799 goto retry_frame;
13802 /* Prevent various kinds of signals during display
13803 update. stdio is not robust about handling
13804 signals, which can cause an apparent I/O error. */
13805 if (interrupt_input)
13806 unrequest_sigio ();
13807 STOP_POLLING;
13809 pending |= update_frame (f, false, false);
13810 f->cursor_type_changed = false;
13811 f->updated_p = true;
13816 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13818 if (!pending)
13820 /* Do the mark_window_display_accurate after all windows have
13821 been redisplayed because this call resets flags in buffers
13822 which are needed for proper redisplay. */
13823 FOR_EACH_FRAME (tail, frame)
13825 struct frame *f = XFRAME (frame);
13826 if (f->updated_p)
13828 f->redisplay = false;
13829 mark_window_display_accurate (f->root_window, true);
13830 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13831 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13836 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13838 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13839 struct frame *mini_frame;
13841 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13842 /* Use list_of_error, not Qerror, so that
13843 we catch only errors and don't run the debugger. */
13844 internal_condition_case_1 (redisplay_window_1, selected_window,
13845 list_of_error,
13846 redisplay_window_error);
13847 if (update_miniwindow_p)
13848 internal_condition_case_1 (redisplay_window_1, mini_window,
13849 list_of_error,
13850 redisplay_window_error);
13852 /* Compare desired and current matrices, perform output. */
13854 update:
13855 /* If fonts changed, display again. */
13856 if (sf->fonts_changed)
13857 goto retry;
13859 /* Prevent various kinds of signals during display update.
13860 stdio is not robust about handling signals,
13861 which can cause an apparent I/O error. */
13862 if (interrupt_input)
13863 unrequest_sigio ();
13864 STOP_POLLING;
13866 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13868 if (hscroll_windows (selected_window))
13869 goto retry;
13871 XWINDOW (selected_window)->must_be_updated_p = true;
13872 pending = update_frame (sf, false, false);
13873 sf->cursor_type_changed = false;
13876 /* We may have called echo_area_display at the top of this
13877 function. If the echo area is on another frame, that may
13878 have put text on a frame other than the selected one, so the
13879 above call to update_frame would not have caught it. Catch
13880 it here. */
13881 mini_window = FRAME_MINIBUF_WINDOW (sf);
13882 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13884 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13886 XWINDOW (mini_window)->must_be_updated_p = true;
13887 pending |= update_frame (mini_frame, false, false);
13888 mini_frame->cursor_type_changed = false;
13889 if (!pending && hscroll_windows (mini_window))
13890 goto retry;
13894 /* If display was paused because of pending input, make sure we do a
13895 thorough update the next time. */
13896 if (pending)
13898 /* Prevent the optimization at the beginning of
13899 redisplay_internal that tries a single-line update of the
13900 line containing the cursor in the selected window. */
13901 CHARPOS (this_line_start_pos) = 0;
13903 /* Let the overlay arrow be updated the next time. */
13904 update_overlay_arrows (0);
13906 /* If we pause after scrolling, some rows in the current
13907 matrices of some windows are not valid. */
13908 if (!WINDOW_FULL_WIDTH_P (w)
13909 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13910 update_mode_lines = 36;
13912 else
13914 if (!consider_all_windows_p)
13916 /* This has already been done above if
13917 consider_all_windows_p is set. */
13918 if (XBUFFER (w->contents)->text->redisplay
13919 && buffer_window_count (XBUFFER (w->contents)) > 1)
13920 /* This can happen if b->text->redisplay was set during
13921 jit-lock. */
13922 propagate_buffer_redisplay ();
13923 mark_window_display_accurate_1 (w, true);
13925 /* Say overlay arrows are up to date. */
13926 update_overlay_arrows (1);
13928 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13929 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13932 update_mode_lines = 0;
13933 windows_or_buffers_changed = 0;
13936 /* Start SIGIO interrupts coming again. Having them off during the
13937 code above makes it less likely one will discard output, but not
13938 impossible, since there might be stuff in the system buffer here.
13939 But it is much hairier to try to do anything about that. */
13940 if (interrupt_input)
13941 request_sigio ();
13942 RESUME_POLLING;
13944 /* If a frame has become visible which was not before, redisplay
13945 again, so that we display it. Expose events for such a frame
13946 (which it gets when becoming visible) don't call the parts of
13947 redisplay constructing glyphs, so simply exposing a frame won't
13948 display anything in this case. So, we have to display these
13949 frames here explicitly. */
13950 if (!pending)
13952 int new_count = 0;
13954 FOR_EACH_FRAME (tail, frame)
13956 if (XFRAME (frame)->visible)
13957 new_count++;
13960 if (new_count != number_of_visible_frames)
13961 windows_or_buffers_changed = 52;
13964 /* Change frame size now if a change is pending. */
13965 do_pending_window_change (true);
13967 /* If we just did a pending size change, or have additional
13968 visible frames, or selected_window changed, redisplay again. */
13969 if ((windows_or_buffers_changed && !pending)
13970 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13971 goto retry;
13973 /* Clear the face and image caches.
13975 We used to do this only if consider_all_windows_p. But the cache
13976 needs to be cleared if a timer creates images in the current
13977 buffer (e.g. the test case in Bug#6230). */
13979 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13981 clear_face_cache (false);
13982 clear_face_cache_count = 0;
13985 #ifdef HAVE_WINDOW_SYSTEM
13986 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13988 clear_image_caches (Qnil);
13989 clear_image_cache_count = 0;
13991 #endif /* HAVE_WINDOW_SYSTEM */
13993 end_of_redisplay:
13994 #ifdef HAVE_NS
13995 ns_set_doc_edited ();
13996 #endif
13997 if (interrupt_input && interrupts_deferred)
13998 request_sigio ();
14000 unbind_to (count, Qnil);
14001 RESUME_POLLING;
14005 /* Redisplay, but leave alone any recent echo area message unless
14006 another message has been requested in its place.
14008 This is useful in situations where you need to redisplay but no
14009 user action has occurred, making it inappropriate for the message
14010 area to be cleared. See tracking_off and
14011 wait_reading_process_output for examples of these situations.
14013 FROM_WHERE is an integer saying from where this function was
14014 called. This is useful for debugging. */
14016 void
14017 redisplay_preserve_echo_area (int from_where)
14019 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14021 if (!NILP (echo_area_buffer[1]))
14023 /* We have a previously displayed message, but no current
14024 message. Redisplay the previous message. */
14025 display_last_displayed_message_p = true;
14026 redisplay_internal ();
14027 display_last_displayed_message_p = false;
14029 else
14030 redisplay_internal ();
14032 flush_frame (SELECTED_FRAME ());
14036 /* Function registered with record_unwind_protect in redisplay_internal. */
14038 static void
14039 unwind_redisplay (void)
14041 redisplaying_p = false;
14045 /* Mark the display of leaf window W as accurate or inaccurate.
14046 If ACCURATE_P, mark display of W as accurate.
14047 If !ACCURATE_P, arrange for W to be redisplayed the next
14048 time redisplay_internal is called. */
14050 static void
14051 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14053 struct buffer *b = XBUFFER (w->contents);
14055 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14056 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14057 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14059 if (accurate_p)
14061 b->clip_changed = false;
14062 b->prevent_redisplay_optimizations_p = false;
14063 eassert (buffer_window_count (b) > 0);
14064 /* Resetting b->text->redisplay is problematic!
14065 In order to make it safer to do it here, redisplay_internal must
14066 have copied all b->text->redisplay to their respective windows. */
14067 b->text->redisplay = false;
14069 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14070 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14071 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14072 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14074 w->current_matrix->buffer = b;
14075 w->current_matrix->begv = BUF_BEGV (b);
14076 w->current_matrix->zv = BUF_ZV (b);
14078 w->last_cursor_vpos = w->cursor.vpos;
14079 w->last_cursor_off_p = w->cursor_off_p;
14081 if (w == XWINDOW (selected_window))
14082 w->last_point = BUF_PT (b);
14083 else
14084 w->last_point = marker_position (w->pointm);
14086 w->window_end_valid = true;
14087 w->update_mode_line = false;
14090 w->redisplay = !accurate_p;
14094 /* Mark the display of windows in the window tree rooted at WINDOW as
14095 accurate or inaccurate. If ACCURATE_P, mark display of
14096 windows as accurate. If !ACCURATE_P, arrange for windows to
14097 be redisplayed the next time redisplay_internal is called. */
14099 void
14100 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14102 struct window *w;
14104 for (; !NILP (window); window = w->next)
14106 w = XWINDOW (window);
14107 if (WINDOWP (w->contents))
14108 mark_window_display_accurate (w->contents, accurate_p);
14109 else
14110 mark_window_display_accurate_1 (w, accurate_p);
14113 if (accurate_p)
14114 update_overlay_arrows (1);
14115 else
14116 /* Force a thorough redisplay the next time by setting
14117 last_arrow_position and last_arrow_string to t, which is
14118 unequal to any useful value of Voverlay_arrow_... */
14119 update_overlay_arrows (-1);
14123 /* Return value in display table DP (Lisp_Char_Table *) for character
14124 C. Since a display table doesn't have any parent, we don't have to
14125 follow parent. Do not call this function directly but use the
14126 macro DISP_CHAR_VECTOR. */
14128 Lisp_Object
14129 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14131 Lisp_Object val;
14133 if (ASCII_CHAR_P (c))
14135 val = dp->ascii;
14136 if (SUB_CHAR_TABLE_P (val))
14137 val = XSUB_CHAR_TABLE (val)->contents[c];
14139 else
14141 Lisp_Object table;
14143 XSETCHAR_TABLE (table, dp);
14144 val = char_table_ref (table, c);
14146 if (NILP (val))
14147 val = dp->defalt;
14148 return val;
14153 /***********************************************************************
14154 Window Redisplay
14155 ***********************************************************************/
14157 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14159 static void
14160 redisplay_windows (Lisp_Object window)
14162 while (!NILP (window))
14164 struct window *w = XWINDOW (window);
14166 if (WINDOWP (w->contents))
14167 redisplay_windows (w->contents);
14168 else if (BUFFERP (w->contents))
14170 displayed_buffer = XBUFFER (w->contents);
14171 /* Use list_of_error, not Qerror, so that
14172 we catch only errors and don't run the debugger. */
14173 internal_condition_case_1 (redisplay_window_0, window,
14174 list_of_error,
14175 redisplay_window_error);
14178 window = w->next;
14182 static Lisp_Object
14183 redisplay_window_error (Lisp_Object ignore)
14185 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14186 return Qnil;
14189 static Lisp_Object
14190 redisplay_window_0 (Lisp_Object window)
14192 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14193 redisplay_window (window, false);
14194 return Qnil;
14197 static Lisp_Object
14198 redisplay_window_1 (Lisp_Object window)
14200 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14201 redisplay_window (window, true);
14202 return Qnil;
14206 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14207 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14208 which positions recorded in ROW differ from current buffer
14209 positions.
14211 Return true iff cursor is on this row. */
14213 static bool
14214 set_cursor_from_row (struct window *w, struct glyph_row *row,
14215 struct glyph_matrix *matrix,
14216 ptrdiff_t delta, ptrdiff_t delta_bytes,
14217 int dy, int dvpos)
14219 struct glyph *glyph = row->glyphs[TEXT_AREA];
14220 struct glyph *end = glyph + row->used[TEXT_AREA];
14221 struct glyph *cursor = NULL;
14222 /* The last known character position in row. */
14223 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14224 int x = row->x;
14225 ptrdiff_t pt_old = PT - delta;
14226 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14227 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14228 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14229 /* A glyph beyond the edge of TEXT_AREA which we should never
14230 touch. */
14231 struct glyph *glyphs_end = end;
14232 /* True means we've found a match for cursor position, but that
14233 glyph has the avoid_cursor_p flag set. */
14234 bool match_with_avoid_cursor = false;
14235 /* True means we've seen at least one glyph that came from a
14236 display string. */
14237 bool string_seen = false;
14238 /* Largest and smallest buffer positions seen so far during scan of
14239 glyph row. */
14240 ptrdiff_t bpos_max = pos_before;
14241 ptrdiff_t bpos_min = pos_after;
14242 /* Last buffer position covered by an overlay string with an integer
14243 `cursor' property. */
14244 ptrdiff_t bpos_covered = 0;
14245 /* True means the display string on which to display the cursor
14246 comes from a text property, not from an overlay. */
14247 bool string_from_text_prop = false;
14249 /* Don't even try doing anything if called for a mode-line or
14250 header-line row, since the rest of the code isn't prepared to
14251 deal with such calamities. */
14252 eassert (!row->mode_line_p);
14253 if (row->mode_line_p)
14254 return false;
14256 /* Skip over glyphs not having an object at the start and the end of
14257 the row. These are special glyphs like truncation marks on
14258 terminal frames. */
14259 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14261 if (!row->reversed_p)
14263 while (glyph < end
14264 && NILP (glyph->object)
14265 && glyph->charpos < 0)
14267 x += glyph->pixel_width;
14268 ++glyph;
14270 while (end > glyph
14271 && NILP ((end - 1)->object)
14272 /* CHARPOS is zero for blanks and stretch glyphs
14273 inserted by extend_face_to_end_of_line. */
14274 && (end - 1)->charpos <= 0)
14275 --end;
14276 glyph_before = glyph - 1;
14277 glyph_after = end;
14279 else
14281 struct glyph *g;
14283 /* If the glyph row is reversed, we need to process it from back
14284 to front, so swap the edge pointers. */
14285 glyphs_end = end = glyph - 1;
14286 glyph += row->used[TEXT_AREA] - 1;
14288 while (glyph > end + 1
14289 && NILP (glyph->object)
14290 && glyph->charpos < 0)
14292 --glyph;
14293 x -= glyph->pixel_width;
14295 if (NILP (glyph->object) && glyph->charpos < 0)
14296 --glyph;
14297 /* By default, in reversed rows we put the cursor on the
14298 rightmost (first in the reading order) glyph. */
14299 for (g = end + 1; g < glyph; g++)
14300 x += g->pixel_width;
14301 while (end < glyph
14302 && NILP ((end + 1)->object)
14303 && (end + 1)->charpos <= 0)
14304 ++end;
14305 glyph_before = glyph + 1;
14306 glyph_after = end;
14309 else if (row->reversed_p)
14311 /* In R2L rows that don't display text, put the cursor on the
14312 rightmost glyph. Case in point: an empty last line that is
14313 part of an R2L paragraph. */
14314 cursor = end - 1;
14315 /* Avoid placing the cursor on the last glyph of the row, where
14316 on terminal frames we hold the vertical border between
14317 adjacent windows. */
14318 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14319 && !WINDOW_RIGHTMOST_P (w)
14320 && cursor == row->glyphs[LAST_AREA] - 1)
14321 cursor--;
14322 x = -1; /* will be computed below, at label compute_x */
14325 /* Step 1: Try to find the glyph whose character position
14326 corresponds to point. If that's not possible, find 2 glyphs
14327 whose character positions are the closest to point, one before
14328 point, the other after it. */
14329 if (!row->reversed_p)
14330 while (/* not marched to end of glyph row */
14331 glyph < end
14332 /* glyph was not inserted by redisplay for internal purposes */
14333 && !NILP (glyph->object))
14335 if (BUFFERP (glyph->object))
14337 ptrdiff_t dpos = glyph->charpos - pt_old;
14339 if (glyph->charpos > bpos_max)
14340 bpos_max = glyph->charpos;
14341 if (glyph->charpos < bpos_min)
14342 bpos_min = glyph->charpos;
14343 if (!glyph->avoid_cursor_p)
14345 /* If we hit point, we've found the glyph on which to
14346 display the cursor. */
14347 if (dpos == 0)
14349 match_with_avoid_cursor = false;
14350 break;
14352 /* See if we've found a better approximation to
14353 POS_BEFORE or to POS_AFTER. */
14354 if (0 > dpos && dpos > pos_before - pt_old)
14356 pos_before = glyph->charpos;
14357 glyph_before = glyph;
14359 else if (0 < dpos && dpos < pos_after - pt_old)
14361 pos_after = glyph->charpos;
14362 glyph_after = glyph;
14365 else if (dpos == 0)
14366 match_with_avoid_cursor = true;
14368 else if (STRINGP (glyph->object))
14370 Lisp_Object chprop;
14371 ptrdiff_t glyph_pos = glyph->charpos;
14373 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14374 glyph->object);
14375 if (!NILP (chprop))
14377 /* If the string came from a `display' text property,
14378 look up the buffer position of that property and
14379 use that position to update bpos_max, as if we
14380 actually saw such a position in one of the row's
14381 glyphs. This helps with supporting integer values
14382 of `cursor' property on the display string in
14383 situations where most or all of the row's buffer
14384 text is completely covered by display properties,
14385 so that no glyph with valid buffer positions is
14386 ever seen in the row. */
14387 ptrdiff_t prop_pos =
14388 string_buffer_position_lim (glyph->object, pos_before,
14389 pos_after, false);
14391 if (prop_pos >= pos_before)
14392 bpos_max = prop_pos;
14394 if (INTEGERP (chprop))
14396 bpos_covered = bpos_max + XINT (chprop);
14397 /* If the `cursor' property covers buffer positions up
14398 to and including point, we should display cursor on
14399 this glyph. Note that, if a `cursor' property on one
14400 of the string's characters has an integer value, we
14401 will break out of the loop below _before_ we get to
14402 the position match above. IOW, integer values of
14403 the `cursor' property override the "exact match for
14404 point" strategy of positioning the cursor. */
14405 /* Implementation note: bpos_max == pt_old when, e.g.,
14406 we are in an empty line, where bpos_max is set to
14407 MATRIX_ROW_START_CHARPOS, see above. */
14408 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14410 cursor = glyph;
14411 break;
14415 string_seen = true;
14417 x += glyph->pixel_width;
14418 ++glyph;
14420 else if (glyph > end) /* row is reversed */
14421 while (!NILP (glyph->object))
14423 if (BUFFERP (glyph->object))
14425 ptrdiff_t dpos = glyph->charpos - pt_old;
14427 if (glyph->charpos > bpos_max)
14428 bpos_max = glyph->charpos;
14429 if (glyph->charpos < bpos_min)
14430 bpos_min = glyph->charpos;
14431 if (!glyph->avoid_cursor_p)
14433 if (dpos == 0)
14435 match_with_avoid_cursor = false;
14436 break;
14438 if (0 > dpos && dpos > pos_before - pt_old)
14440 pos_before = glyph->charpos;
14441 glyph_before = glyph;
14443 else if (0 < dpos && dpos < pos_after - pt_old)
14445 pos_after = glyph->charpos;
14446 glyph_after = glyph;
14449 else if (dpos == 0)
14450 match_with_avoid_cursor = true;
14452 else if (STRINGP (glyph->object))
14454 Lisp_Object chprop;
14455 ptrdiff_t glyph_pos = glyph->charpos;
14457 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14458 glyph->object);
14459 if (!NILP (chprop))
14461 ptrdiff_t prop_pos =
14462 string_buffer_position_lim (glyph->object, pos_before,
14463 pos_after, false);
14465 if (prop_pos >= pos_before)
14466 bpos_max = prop_pos;
14468 if (INTEGERP (chprop))
14470 bpos_covered = bpos_max + XINT (chprop);
14471 /* If the `cursor' property covers buffer positions up
14472 to and including point, we should display cursor on
14473 this glyph. */
14474 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14476 cursor = glyph;
14477 break;
14480 string_seen = true;
14482 --glyph;
14483 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14485 x--; /* can't use any pixel_width */
14486 break;
14488 x -= glyph->pixel_width;
14491 /* Step 2: If we didn't find an exact match for point, we need to
14492 look for a proper place to put the cursor among glyphs between
14493 GLYPH_BEFORE and GLYPH_AFTER. */
14494 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14495 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14496 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14498 /* An empty line has a single glyph whose OBJECT is nil and
14499 whose CHARPOS is the position of a newline on that line.
14500 Note that on a TTY, there are more glyphs after that, which
14501 were produced by extend_face_to_end_of_line, but their
14502 CHARPOS is zero or negative. */
14503 bool empty_line_p =
14504 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14505 && NILP (glyph->object) && glyph->charpos > 0
14506 /* On a TTY, continued and truncated rows also have a glyph at
14507 their end whose OBJECT is nil and whose CHARPOS is
14508 positive (the continuation and truncation glyphs), but such
14509 rows are obviously not "empty". */
14510 && !(row->continued_p || row->truncated_on_right_p));
14512 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14514 ptrdiff_t ellipsis_pos;
14516 /* Scan back over the ellipsis glyphs. */
14517 if (!row->reversed_p)
14519 ellipsis_pos = (glyph - 1)->charpos;
14520 while (glyph > row->glyphs[TEXT_AREA]
14521 && (glyph - 1)->charpos == ellipsis_pos)
14522 glyph--, x -= glyph->pixel_width;
14523 /* That loop always goes one position too far, including
14524 the glyph before the ellipsis. So scan forward over
14525 that one. */
14526 x += glyph->pixel_width;
14527 glyph++;
14529 else /* row is reversed */
14531 ellipsis_pos = (glyph + 1)->charpos;
14532 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14533 && (glyph + 1)->charpos == ellipsis_pos)
14534 glyph++, x += glyph->pixel_width;
14535 x -= glyph->pixel_width;
14536 glyph--;
14539 else if (match_with_avoid_cursor)
14541 cursor = glyph_after;
14542 x = -1;
14544 else if (string_seen)
14546 int incr = row->reversed_p ? -1 : +1;
14548 /* Need to find the glyph that came out of a string which is
14549 present at point. That glyph is somewhere between
14550 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14551 positioned between POS_BEFORE and POS_AFTER in the
14552 buffer. */
14553 struct glyph *start, *stop;
14554 ptrdiff_t pos = pos_before;
14556 x = -1;
14558 /* If the row ends in a newline from a display string,
14559 reordering could have moved the glyphs belonging to the
14560 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14561 in this case we extend the search to the last glyph in
14562 the row that was not inserted by redisplay. */
14563 if (row->ends_in_newline_from_string_p)
14565 glyph_after = end;
14566 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14569 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14570 correspond to POS_BEFORE and POS_AFTER, respectively. We
14571 need START and STOP in the order that corresponds to the
14572 row's direction as given by its reversed_p flag. If the
14573 directionality of characters between POS_BEFORE and
14574 POS_AFTER is the opposite of the row's base direction,
14575 these characters will have been reordered for display,
14576 and we need to reverse START and STOP. */
14577 if (!row->reversed_p)
14579 start = min (glyph_before, glyph_after);
14580 stop = max (glyph_before, glyph_after);
14582 else
14584 start = max (glyph_before, glyph_after);
14585 stop = min (glyph_before, glyph_after);
14587 for (glyph = start + incr;
14588 row->reversed_p ? glyph > stop : glyph < stop; )
14591 /* Any glyphs that come from the buffer are here because
14592 of bidi reordering. Skip them, and only pay
14593 attention to glyphs that came from some string. */
14594 if (STRINGP (glyph->object))
14596 Lisp_Object str;
14597 ptrdiff_t tem;
14598 /* If the display property covers the newline, we
14599 need to search for it one position farther. */
14600 ptrdiff_t lim = pos_after
14601 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14603 string_from_text_prop = false;
14604 str = glyph->object;
14605 tem = string_buffer_position_lim (str, pos, lim, false);
14606 if (tem == 0 /* from overlay */
14607 || pos <= tem)
14609 /* If the string from which this glyph came is
14610 found in the buffer at point, or at position
14611 that is closer to point than pos_after, then
14612 we've found the glyph we've been looking for.
14613 If it comes from an overlay (tem == 0), and
14614 it has the `cursor' property on one of its
14615 glyphs, record that glyph as a candidate for
14616 displaying the cursor. (As in the
14617 unidirectional version, we will display the
14618 cursor on the last candidate we find.) */
14619 if (tem == 0
14620 || tem == pt_old
14621 || (tem - pt_old > 0 && tem < pos_after))
14623 /* The glyphs from this string could have
14624 been reordered. Find the one with the
14625 smallest string position. Or there could
14626 be a character in the string with the
14627 `cursor' property, which means display
14628 cursor on that character's glyph. */
14629 ptrdiff_t strpos = glyph->charpos;
14631 if (tem)
14633 cursor = glyph;
14634 string_from_text_prop = true;
14636 for ( ;
14637 (row->reversed_p ? glyph > stop : glyph < stop)
14638 && EQ (glyph->object, str);
14639 glyph += incr)
14641 Lisp_Object cprop;
14642 ptrdiff_t gpos = glyph->charpos;
14644 cprop = Fget_char_property (make_number (gpos),
14645 Qcursor,
14646 glyph->object);
14647 if (!NILP (cprop))
14649 cursor = glyph;
14650 break;
14652 if (tem && glyph->charpos < strpos)
14654 strpos = glyph->charpos;
14655 cursor = glyph;
14659 if (tem == pt_old
14660 || (tem - pt_old > 0 && tem < pos_after))
14661 goto compute_x;
14663 if (tem)
14664 pos = tem + 1; /* don't find previous instances */
14666 /* This string is not what we want; skip all of the
14667 glyphs that came from it. */
14668 while ((row->reversed_p ? glyph > stop : glyph < stop)
14669 && EQ (glyph->object, str))
14670 glyph += incr;
14672 else
14673 glyph += incr;
14676 /* If we reached the end of the line, and END was from a string,
14677 the cursor is not on this line. */
14678 if (cursor == NULL
14679 && (row->reversed_p ? glyph <= end : glyph >= end)
14680 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14681 && STRINGP (end->object)
14682 && row->continued_p)
14683 return false;
14685 /* A truncated row may not include PT among its character positions.
14686 Setting the cursor inside the scroll margin will trigger
14687 recalculation of hscroll in hscroll_window_tree. But if a
14688 display string covers point, defer to the string-handling
14689 code below to figure this out. */
14690 else if (row->truncated_on_left_p && pt_old < bpos_min)
14692 cursor = glyph_before;
14693 x = -1;
14695 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14696 /* Zero-width characters produce no glyphs. */
14697 || (!empty_line_p
14698 && (row->reversed_p
14699 ? glyph_after > glyphs_end
14700 : glyph_after < glyphs_end)))
14702 cursor = glyph_after;
14703 x = -1;
14707 compute_x:
14708 if (cursor != NULL)
14709 glyph = cursor;
14710 else if (glyph == glyphs_end
14711 && pos_before == pos_after
14712 && STRINGP ((row->reversed_p
14713 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14714 : row->glyphs[TEXT_AREA])->object))
14716 /* If all the glyphs of this row came from strings, put the
14717 cursor on the first glyph of the row. This avoids having the
14718 cursor outside of the text area in this very rare and hard
14719 use case. */
14720 glyph =
14721 row->reversed_p
14722 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14723 : row->glyphs[TEXT_AREA];
14725 if (x < 0)
14727 struct glyph *g;
14729 /* Need to compute x that corresponds to GLYPH. */
14730 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14732 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14733 emacs_abort ();
14734 x += g->pixel_width;
14738 /* ROW could be part of a continued line, which, under bidi
14739 reordering, might have other rows whose start and end charpos
14740 occlude point. Only set w->cursor if we found a better
14741 approximation to the cursor position than we have from previously
14742 examined candidate rows belonging to the same continued line. */
14743 if (/* We already have a candidate row. */
14744 w->cursor.vpos >= 0
14745 /* That candidate is not the row we are processing. */
14746 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14747 /* Make sure cursor.vpos specifies a row whose start and end
14748 charpos occlude point, and it is valid candidate for being a
14749 cursor-row. This is because some callers of this function
14750 leave cursor.vpos at the row where the cursor was displayed
14751 during the last redisplay cycle. */
14752 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14753 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14754 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14756 struct glyph *g1
14757 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14759 /* Don't consider glyphs that are outside TEXT_AREA. */
14760 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14761 return false;
14762 /* Keep the candidate whose buffer position is the closest to
14763 point or has the `cursor' property. */
14764 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14765 w->cursor.hpos >= 0
14766 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14767 && ((BUFFERP (g1->object)
14768 && (g1->charpos == pt_old /* An exact match always wins. */
14769 || (BUFFERP (glyph->object)
14770 && eabs (g1->charpos - pt_old)
14771 < eabs (glyph->charpos - pt_old))))
14772 /* Previous candidate is a glyph from a string that has
14773 a non-nil `cursor' property. */
14774 || (STRINGP (g1->object)
14775 && (!NILP (Fget_char_property (make_number (g1->charpos),
14776 Qcursor, g1->object))
14777 /* Previous candidate is from the same display
14778 string as this one, and the display string
14779 came from a text property. */
14780 || (EQ (g1->object, glyph->object)
14781 && string_from_text_prop)
14782 /* this candidate is from newline and its
14783 position is not an exact match */
14784 || (NILP (glyph->object)
14785 && glyph->charpos != pt_old)))))
14786 return false;
14787 /* If this candidate gives an exact match, use that. */
14788 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14789 /* If this candidate is a glyph created for the
14790 terminating newline of a line, and point is on that
14791 newline, it wins because it's an exact match. */
14792 || (!row->continued_p
14793 && NILP (glyph->object)
14794 && glyph->charpos == 0
14795 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14796 /* Otherwise, keep the candidate that comes from a row
14797 spanning less buffer positions. This may win when one or
14798 both candidate positions are on glyphs that came from
14799 display strings, for which we cannot compare buffer
14800 positions. */
14801 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14802 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14803 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14804 return false;
14806 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14807 w->cursor.x = x;
14808 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14809 w->cursor.y = row->y + dy;
14811 if (w == XWINDOW (selected_window))
14813 if (!row->continued_p
14814 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14815 && row->x == 0)
14817 this_line_buffer = XBUFFER (w->contents);
14819 CHARPOS (this_line_start_pos)
14820 = MATRIX_ROW_START_CHARPOS (row) + delta;
14821 BYTEPOS (this_line_start_pos)
14822 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14824 CHARPOS (this_line_end_pos)
14825 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14826 BYTEPOS (this_line_end_pos)
14827 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14829 this_line_y = w->cursor.y;
14830 this_line_pixel_height = row->height;
14831 this_line_vpos = w->cursor.vpos;
14832 this_line_start_x = row->x;
14834 else
14835 CHARPOS (this_line_start_pos) = 0;
14838 return true;
14842 /* Run window scroll functions, if any, for WINDOW with new window
14843 start STARTP. Sets the window start of WINDOW to that position.
14845 We assume that the window's buffer is really current. */
14847 static struct text_pos
14848 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14850 struct window *w = XWINDOW (window);
14851 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14853 eassert (current_buffer == XBUFFER (w->contents));
14855 if (!NILP (Vwindow_scroll_functions))
14857 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14858 make_number (CHARPOS (startp)));
14859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14860 /* In case the hook functions switch buffers. */
14861 set_buffer_internal (XBUFFER (w->contents));
14864 return startp;
14868 /* Make sure the line containing the cursor is fully visible.
14869 A value of true means there is nothing to be done.
14870 (Either the line is fully visible, or it cannot be made so,
14871 or we cannot tell.)
14873 If FORCE_P, return false even if partial visible cursor row
14874 is higher than window.
14876 If CURRENT_MATRIX_P, use the information from the
14877 window's current glyph matrix; otherwise use the desired glyph
14878 matrix.
14880 A value of false means the caller should do scrolling
14881 as if point had gone off the screen. */
14883 static bool
14884 cursor_row_fully_visible_p (struct window *w, bool force_p,
14885 bool current_matrix_p)
14887 struct glyph_matrix *matrix;
14888 struct glyph_row *row;
14889 int window_height;
14891 if (!make_cursor_line_fully_visible_p)
14892 return true;
14894 /* It's not always possible to find the cursor, e.g, when a window
14895 is full of overlay strings. Don't do anything in that case. */
14896 if (w->cursor.vpos < 0)
14897 return true;
14899 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14900 row = MATRIX_ROW (matrix, w->cursor.vpos);
14902 /* If the cursor row is not partially visible, there's nothing to do. */
14903 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14904 return true;
14906 /* If the row the cursor is in is taller than the window's height,
14907 it's not clear what to do, so do nothing. */
14908 window_height = window_box_height (w);
14909 if (row->height >= window_height)
14911 if (!force_p || MINI_WINDOW_P (w)
14912 || w->vscroll || w->cursor.vpos == 0)
14913 return true;
14915 return false;
14919 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14920 means only WINDOW is redisplayed in redisplay_internal.
14921 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14922 in redisplay_window to bring a partially visible line into view in
14923 the case that only the cursor has moved.
14925 LAST_LINE_MISFIT should be true if we're scrolling because the
14926 last screen line's vertical height extends past the end of the screen.
14928 Value is
14930 1 if scrolling succeeded
14932 0 if scrolling didn't find point.
14934 -1 if new fonts have been loaded so that we must interrupt
14935 redisplay, adjust glyph matrices, and try again. */
14937 enum
14939 SCROLLING_SUCCESS,
14940 SCROLLING_FAILED,
14941 SCROLLING_NEED_LARGER_MATRICES
14944 /* If scroll-conservatively is more than this, never recenter.
14946 If you change this, don't forget to update the doc string of
14947 `scroll-conservatively' and the Emacs manual. */
14948 #define SCROLL_LIMIT 100
14950 static int
14951 try_scrolling (Lisp_Object window, bool just_this_one_p,
14952 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14953 bool temp_scroll_step, bool last_line_misfit)
14955 struct window *w = XWINDOW (window);
14956 struct frame *f = XFRAME (w->frame);
14957 struct text_pos pos, startp;
14958 struct it it;
14959 int this_scroll_margin, scroll_max, rc, height;
14960 int dy = 0, amount_to_scroll = 0;
14961 bool scroll_down_p = false;
14962 int extra_scroll_margin_lines = last_line_misfit;
14963 Lisp_Object aggressive;
14964 /* We will never try scrolling more than this number of lines. */
14965 int scroll_limit = SCROLL_LIMIT;
14966 int frame_line_height = default_line_pixel_height (w);
14967 int window_total_lines
14968 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14970 #ifdef GLYPH_DEBUG
14971 debug_method_add (w, "try_scrolling");
14972 #endif
14974 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14976 /* Compute scroll margin height in pixels. We scroll when point is
14977 within this distance from the top or bottom of the window. */
14978 if (scroll_margin > 0)
14979 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14980 * frame_line_height;
14981 else
14982 this_scroll_margin = 0;
14984 /* Force arg_scroll_conservatively to have a reasonable value, to
14985 avoid scrolling too far away with slow move_it_* functions. Note
14986 that the user can supply scroll-conservatively equal to
14987 `most-positive-fixnum', which can be larger than INT_MAX. */
14988 if (arg_scroll_conservatively > scroll_limit)
14990 arg_scroll_conservatively = scroll_limit + 1;
14991 scroll_max = scroll_limit * frame_line_height;
14993 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14994 /* Compute how much we should try to scroll maximally to bring
14995 point into view. */
14996 scroll_max = (max (scroll_step,
14997 max (arg_scroll_conservatively, temp_scroll_step))
14998 * frame_line_height);
14999 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15000 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15001 /* We're trying to scroll because of aggressive scrolling but no
15002 scroll_step is set. Choose an arbitrary one. */
15003 scroll_max = 10 * frame_line_height;
15004 else
15005 scroll_max = 0;
15007 too_near_end:
15009 /* Decide whether to scroll down. */
15010 if (PT > CHARPOS (startp))
15012 int scroll_margin_y;
15014 /* Compute the pixel ypos of the scroll margin, then move IT to
15015 either that ypos or PT, whichever comes first. */
15016 start_display (&it, w, startp);
15017 scroll_margin_y = it.last_visible_y - this_scroll_margin
15018 - frame_line_height * extra_scroll_margin_lines;
15019 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15020 (MOVE_TO_POS | MOVE_TO_Y));
15022 if (PT > CHARPOS (it.current.pos))
15024 int y0 = line_bottom_y (&it);
15025 /* Compute how many pixels below window bottom to stop searching
15026 for PT. This avoids costly search for PT that is far away if
15027 the user limited scrolling by a small number of lines, but
15028 always finds PT if scroll_conservatively is set to a large
15029 number, such as most-positive-fixnum. */
15030 int slack = max (scroll_max, 10 * frame_line_height);
15031 int y_to_move = it.last_visible_y + slack;
15033 /* Compute the distance from the scroll margin to PT or to
15034 the scroll limit, whichever comes first. This should
15035 include the height of the cursor line, to make that line
15036 fully visible. */
15037 move_it_to (&it, PT, -1, y_to_move,
15038 -1, MOVE_TO_POS | MOVE_TO_Y);
15039 dy = line_bottom_y (&it) - y0;
15041 if (dy > scroll_max)
15042 return SCROLLING_FAILED;
15044 if (dy > 0)
15045 scroll_down_p = true;
15049 if (scroll_down_p)
15051 /* Point is in or below the bottom scroll margin, so move the
15052 window start down. If scrolling conservatively, move it just
15053 enough down to make point visible. If scroll_step is set,
15054 move it down by scroll_step. */
15055 if (arg_scroll_conservatively)
15056 amount_to_scroll
15057 = min (max (dy, frame_line_height),
15058 frame_line_height * arg_scroll_conservatively);
15059 else if (scroll_step || temp_scroll_step)
15060 amount_to_scroll = scroll_max;
15061 else
15063 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15064 height = WINDOW_BOX_TEXT_HEIGHT (w);
15065 if (NUMBERP (aggressive))
15067 double float_amount = XFLOATINT (aggressive) * height;
15068 int aggressive_scroll = float_amount;
15069 if (aggressive_scroll == 0 && float_amount > 0)
15070 aggressive_scroll = 1;
15071 /* Don't let point enter the scroll margin near top of
15072 the window. This could happen if the value of
15073 scroll_up_aggressively is too large and there are
15074 non-zero margins, because scroll_up_aggressively
15075 means put point that fraction of window height
15076 _from_the_bottom_margin_. */
15077 if (aggressive_scroll + 2 * this_scroll_margin > height)
15078 aggressive_scroll = height - 2 * this_scroll_margin;
15079 amount_to_scroll = dy + aggressive_scroll;
15083 if (amount_to_scroll <= 0)
15084 return SCROLLING_FAILED;
15086 start_display (&it, w, startp);
15087 if (arg_scroll_conservatively <= scroll_limit)
15088 move_it_vertically (&it, amount_to_scroll);
15089 else
15091 /* Extra precision for users who set scroll-conservatively
15092 to a large number: make sure the amount we scroll
15093 the window start is never less than amount_to_scroll,
15094 which was computed as distance from window bottom to
15095 point. This matters when lines at window top and lines
15096 below window bottom have different height. */
15097 struct it it1;
15098 void *it1data = NULL;
15099 /* We use a temporary it1 because line_bottom_y can modify
15100 its argument, if it moves one line down; see there. */
15101 int start_y;
15103 SAVE_IT (it1, it, it1data);
15104 start_y = line_bottom_y (&it1);
15105 do {
15106 RESTORE_IT (&it, &it, it1data);
15107 move_it_by_lines (&it, 1);
15108 SAVE_IT (it1, it, it1data);
15109 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15112 /* If STARTP is unchanged, move it down another screen line. */
15113 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15114 move_it_by_lines (&it, 1);
15115 startp = it.current.pos;
15117 else
15119 struct text_pos scroll_margin_pos = startp;
15120 int y_offset = 0;
15122 /* See if point is inside the scroll margin at the top of the
15123 window. */
15124 if (this_scroll_margin)
15126 int y_start;
15128 start_display (&it, w, startp);
15129 y_start = it.current_y;
15130 move_it_vertically (&it, this_scroll_margin);
15131 scroll_margin_pos = it.current.pos;
15132 /* If we didn't move enough before hitting ZV, request
15133 additional amount of scroll, to move point out of the
15134 scroll margin. */
15135 if (IT_CHARPOS (it) == ZV
15136 && it.current_y - y_start < this_scroll_margin)
15137 y_offset = this_scroll_margin - (it.current_y - y_start);
15140 if (PT < CHARPOS (scroll_margin_pos))
15142 /* Point is in the scroll margin at the top of the window or
15143 above what is displayed in the window. */
15144 int y0, y_to_move;
15146 /* Compute the vertical distance from PT to the scroll
15147 margin position. Move as far as scroll_max allows, or
15148 one screenful, or 10 screen lines, whichever is largest.
15149 Give up if distance is greater than scroll_max or if we
15150 didn't reach the scroll margin position. */
15151 SET_TEXT_POS (pos, PT, PT_BYTE);
15152 start_display (&it, w, pos);
15153 y0 = it.current_y;
15154 y_to_move = max (it.last_visible_y,
15155 max (scroll_max, 10 * frame_line_height));
15156 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15157 y_to_move, -1,
15158 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15159 dy = it.current_y - y0;
15160 if (dy > scroll_max
15161 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15162 return SCROLLING_FAILED;
15164 /* Additional scroll for when ZV was too close to point. */
15165 dy += y_offset;
15167 /* Compute new window start. */
15168 start_display (&it, w, startp);
15170 if (arg_scroll_conservatively)
15171 amount_to_scroll = max (dy, frame_line_height
15172 * max (scroll_step, temp_scroll_step));
15173 else if (scroll_step || temp_scroll_step)
15174 amount_to_scroll = scroll_max;
15175 else
15177 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15178 height = WINDOW_BOX_TEXT_HEIGHT (w);
15179 if (NUMBERP (aggressive))
15181 double float_amount = XFLOATINT (aggressive) * height;
15182 int aggressive_scroll = float_amount;
15183 if (aggressive_scroll == 0 && float_amount > 0)
15184 aggressive_scroll = 1;
15185 /* Don't let point enter the scroll margin near
15186 bottom of the window, if the value of
15187 scroll_down_aggressively happens to be too
15188 large. */
15189 if (aggressive_scroll + 2 * this_scroll_margin > height)
15190 aggressive_scroll = height - 2 * this_scroll_margin;
15191 amount_to_scroll = dy + aggressive_scroll;
15195 if (amount_to_scroll <= 0)
15196 return SCROLLING_FAILED;
15198 move_it_vertically_backward (&it, amount_to_scroll);
15199 startp = it.current.pos;
15203 /* Run window scroll functions. */
15204 startp = run_window_scroll_functions (window, startp);
15206 /* Display the window. Give up if new fonts are loaded, or if point
15207 doesn't appear. */
15208 if (!try_window (window, startp, 0))
15209 rc = SCROLLING_NEED_LARGER_MATRICES;
15210 else if (w->cursor.vpos < 0)
15212 clear_glyph_matrix (w->desired_matrix);
15213 rc = SCROLLING_FAILED;
15215 else
15217 /* Maybe forget recorded base line for line number display. */
15218 if (!just_this_one_p
15219 || current_buffer->clip_changed
15220 || BEG_UNCHANGED < CHARPOS (startp))
15221 w->base_line_number = 0;
15223 /* If cursor ends up on a partially visible line,
15224 treat that as being off the bottom of the screen. */
15225 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15226 false)
15227 /* It's possible that the cursor is on the first line of the
15228 buffer, which is partially obscured due to a vscroll
15229 (Bug#7537). In that case, avoid looping forever. */
15230 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15232 clear_glyph_matrix (w->desired_matrix);
15233 ++extra_scroll_margin_lines;
15234 goto too_near_end;
15236 rc = SCROLLING_SUCCESS;
15239 return rc;
15243 /* Compute a suitable window start for window W if display of W starts
15244 on a continuation line. Value is true if a new window start
15245 was computed.
15247 The new window start will be computed, based on W's width, starting
15248 from the start of the continued line. It is the start of the
15249 screen line with the minimum distance from the old start W->start. */
15251 static bool
15252 compute_window_start_on_continuation_line (struct window *w)
15254 struct text_pos pos, start_pos;
15255 bool window_start_changed_p = false;
15257 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15259 /* If window start is on a continuation line... Window start may be
15260 < BEGV in case there's invisible text at the start of the
15261 buffer (M-x rmail, for example). */
15262 if (CHARPOS (start_pos) > BEGV
15263 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15265 struct it it;
15266 struct glyph_row *row;
15268 /* Handle the case that the window start is out of range. */
15269 if (CHARPOS (start_pos) < BEGV)
15270 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15271 else if (CHARPOS (start_pos) > ZV)
15272 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15274 /* Find the start of the continued line. This should be fast
15275 because find_newline is fast (newline cache). */
15276 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15277 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15278 row, DEFAULT_FACE_ID);
15279 reseat_at_previous_visible_line_start (&it);
15281 /* If the line start is "too far" away from the window start,
15282 say it takes too much time to compute a new window start. */
15283 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15284 /* PXW: Do we need upper bounds here? */
15285 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15287 int min_distance, distance;
15289 /* Move forward by display lines to find the new window
15290 start. If window width was enlarged, the new start can
15291 be expected to be > the old start. If window width was
15292 decreased, the new window start will be < the old start.
15293 So, we're looking for the display line start with the
15294 minimum distance from the old window start. */
15295 pos = it.current.pos;
15296 min_distance = INFINITY;
15297 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15298 distance < min_distance)
15300 min_distance = distance;
15301 pos = it.current.pos;
15302 if (it.line_wrap == WORD_WRAP)
15304 /* Under WORD_WRAP, move_it_by_lines is likely to
15305 overshoot and stop not at the first, but the
15306 second character from the left margin. So in
15307 that case, we need a more tight control on the X
15308 coordinate of the iterator than move_it_by_lines
15309 promises in its contract. The method is to first
15310 go to the last (rightmost) visible character of a
15311 line, then move to the leftmost character on the
15312 next line in a separate call. */
15313 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15314 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15315 move_it_to (&it, ZV, 0,
15316 it.current_y + it.max_ascent + it.max_descent, -1,
15317 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15319 else
15320 move_it_by_lines (&it, 1);
15323 /* Set the window start there. */
15324 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15325 window_start_changed_p = true;
15329 return window_start_changed_p;
15333 /* Try cursor movement in case text has not changed in window WINDOW,
15334 with window start STARTP. Value is
15336 CURSOR_MOVEMENT_SUCCESS if successful
15338 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15340 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15341 display. *SCROLL_STEP is set to true, under certain circumstances, if
15342 we want to scroll as if scroll-step were set to 1. See the code.
15344 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15345 which case we have to abort this redisplay, and adjust matrices
15346 first. */
15348 enum
15350 CURSOR_MOVEMENT_SUCCESS,
15351 CURSOR_MOVEMENT_CANNOT_BE_USED,
15352 CURSOR_MOVEMENT_MUST_SCROLL,
15353 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15356 static int
15357 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15358 bool *scroll_step)
15360 struct window *w = XWINDOW (window);
15361 struct frame *f = XFRAME (w->frame);
15362 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15364 #ifdef GLYPH_DEBUG
15365 if (inhibit_try_cursor_movement)
15366 return rc;
15367 #endif
15369 /* Previously, there was a check for Lisp integer in the
15370 if-statement below. Now, this field is converted to
15371 ptrdiff_t, thus zero means invalid position in a buffer. */
15372 eassert (w->last_point > 0);
15373 /* Likewise there was a check whether window_end_vpos is nil or larger
15374 than the window. Now window_end_vpos is int and so never nil, but
15375 let's leave eassert to check whether it fits in the window. */
15376 eassert (!w->window_end_valid
15377 || w->window_end_vpos < w->current_matrix->nrows);
15379 /* Handle case where text has not changed, only point, and it has
15380 not moved off the frame. */
15381 if (/* Point may be in this window. */
15382 PT >= CHARPOS (startp)
15383 /* Selective display hasn't changed. */
15384 && !current_buffer->clip_changed
15385 /* Function force-mode-line-update is used to force a thorough
15386 redisplay. It sets either windows_or_buffers_changed or
15387 update_mode_lines. So don't take a shortcut here for these
15388 cases. */
15389 && !update_mode_lines
15390 && !windows_or_buffers_changed
15391 && !f->cursor_type_changed
15392 && NILP (Vshow_trailing_whitespace)
15393 /* This code is not used for mini-buffer for the sake of the case
15394 of redisplaying to replace an echo area message; since in
15395 that case the mini-buffer contents per se are usually
15396 unchanged. This code is of no real use in the mini-buffer
15397 since the handling of this_line_start_pos, etc., in redisplay
15398 handles the same cases. */
15399 && !EQ (window, minibuf_window)
15400 && (FRAME_WINDOW_P (f)
15401 || !overlay_arrow_in_current_buffer_p ()))
15403 int this_scroll_margin, top_scroll_margin;
15404 struct glyph_row *row = NULL;
15405 int frame_line_height = default_line_pixel_height (w);
15406 int window_total_lines
15407 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15409 #ifdef GLYPH_DEBUG
15410 debug_method_add (w, "cursor movement");
15411 #endif
15413 /* Scroll if point within this distance from the top or bottom
15414 of the window. This is a pixel value. */
15415 if (scroll_margin > 0)
15417 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15418 this_scroll_margin *= frame_line_height;
15420 else
15421 this_scroll_margin = 0;
15423 top_scroll_margin = this_scroll_margin;
15424 if (WINDOW_WANTS_HEADER_LINE_P (w))
15425 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15427 /* Start with the row the cursor was displayed during the last
15428 not paused redisplay. Give up if that row is not valid. */
15429 if (w->last_cursor_vpos < 0
15430 || w->last_cursor_vpos >= w->current_matrix->nrows)
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 else
15434 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15435 if (row->mode_line_p)
15436 ++row;
15437 if (!row->enabled_p)
15438 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15441 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15443 bool scroll_p = false, must_scroll = false;
15444 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15446 if (PT > w->last_point)
15448 /* Point has moved forward. */
15449 while (MATRIX_ROW_END_CHARPOS (row) < PT
15450 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15452 eassert (row->enabled_p);
15453 ++row;
15456 /* If the end position of a row equals the start
15457 position of the next row, and PT is at that position,
15458 we would rather display cursor in the next line. */
15459 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15460 && MATRIX_ROW_END_CHARPOS (row) == PT
15461 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15462 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15463 && !cursor_row_p (row))
15464 ++row;
15466 /* If within the scroll margin, scroll. Note that
15467 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15468 the next line would be drawn, and that
15469 this_scroll_margin can be zero. */
15470 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15471 || PT > MATRIX_ROW_END_CHARPOS (row)
15472 /* Line is completely visible last line in window
15473 and PT is to be set in the next line. */
15474 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15475 && PT == MATRIX_ROW_END_CHARPOS (row)
15476 && !row->ends_at_zv_p
15477 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15478 scroll_p = true;
15480 else if (PT < w->last_point)
15482 /* Cursor has to be moved backward. Note that PT >=
15483 CHARPOS (startp) because of the outer if-statement. */
15484 while (!row->mode_line_p
15485 && (MATRIX_ROW_START_CHARPOS (row) > PT
15486 || (MATRIX_ROW_START_CHARPOS (row) == PT
15487 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15488 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15489 row > w->current_matrix->rows
15490 && (row-1)->ends_in_newline_from_string_p))))
15491 && (row->y > top_scroll_margin
15492 || CHARPOS (startp) == BEGV))
15494 eassert (row->enabled_p);
15495 --row;
15498 /* Consider the following case: Window starts at BEGV,
15499 there is invisible, intangible text at BEGV, so that
15500 display starts at some point START > BEGV. It can
15501 happen that we are called with PT somewhere between
15502 BEGV and START. Try to handle that case. */
15503 if (row < w->current_matrix->rows
15504 || row->mode_line_p)
15506 row = w->current_matrix->rows;
15507 if (row->mode_line_p)
15508 ++row;
15511 /* Due to newlines in overlay strings, we may have to
15512 skip forward over overlay strings. */
15513 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15514 && MATRIX_ROW_END_CHARPOS (row) == PT
15515 && !cursor_row_p (row))
15516 ++row;
15518 /* If within the scroll margin, scroll. */
15519 if (row->y < top_scroll_margin
15520 && CHARPOS (startp) != BEGV)
15521 scroll_p = true;
15523 else
15525 /* Cursor did not move. So don't scroll even if cursor line
15526 is partially visible, as it was so before. */
15527 rc = CURSOR_MOVEMENT_SUCCESS;
15530 if (PT < MATRIX_ROW_START_CHARPOS (row)
15531 || PT > MATRIX_ROW_END_CHARPOS (row))
15533 /* if PT is not in the glyph row, give up. */
15534 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15535 must_scroll = true;
15537 else if (rc != CURSOR_MOVEMENT_SUCCESS
15538 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15540 struct glyph_row *row1;
15542 /* If rows are bidi-reordered and point moved, back up
15543 until we find a row that does not belong to a
15544 continuation line. This is because we must consider
15545 all rows of a continued line as candidates for the
15546 new cursor positioning, since row start and end
15547 positions change non-linearly with vertical position
15548 in such rows. */
15549 /* FIXME: Revisit this when glyph ``spilling'' in
15550 continuation lines' rows is implemented for
15551 bidi-reordered rows. */
15552 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15553 MATRIX_ROW_CONTINUATION_LINE_P (row);
15554 --row)
15556 /* If we hit the beginning of the displayed portion
15557 without finding the first row of a continued
15558 line, give up. */
15559 if (row <= row1)
15561 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15562 break;
15564 eassert (row->enabled_p);
15567 if (must_scroll)
15569 else if (rc != CURSOR_MOVEMENT_SUCCESS
15570 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15571 /* Make sure this isn't a header line by any chance, since
15572 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15573 && !row->mode_line_p
15574 && make_cursor_line_fully_visible_p)
15576 if (PT == MATRIX_ROW_END_CHARPOS (row)
15577 && !row->ends_at_zv_p
15578 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15579 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15580 else if (row->height > window_box_height (w))
15582 /* If we end up in a partially visible line, let's
15583 make it fully visible, except when it's taller
15584 than the window, in which case we can't do much
15585 about it. */
15586 *scroll_step = true;
15587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15589 else
15591 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15592 if (!cursor_row_fully_visible_p (w, false, true))
15593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15594 else
15595 rc = CURSOR_MOVEMENT_SUCCESS;
15598 else if (scroll_p)
15599 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15600 else if (rc != CURSOR_MOVEMENT_SUCCESS
15601 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15603 /* With bidi-reordered rows, there could be more than
15604 one candidate row whose start and end positions
15605 occlude point. We need to let set_cursor_from_row
15606 find the best candidate. */
15607 /* FIXME: Revisit this when glyph ``spilling'' in
15608 continuation lines' rows is implemented for
15609 bidi-reordered rows. */
15610 bool rv = false;
15614 bool at_zv_p = false, exact_match_p = false;
15616 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15617 && PT <= MATRIX_ROW_END_CHARPOS (row)
15618 && cursor_row_p (row))
15619 rv |= set_cursor_from_row (w, row, w->current_matrix,
15620 0, 0, 0, 0);
15621 /* As soon as we've found the exact match for point,
15622 or the first suitable row whose ends_at_zv_p flag
15623 is set, we are done. */
15624 if (rv)
15626 at_zv_p = MATRIX_ROW (w->current_matrix,
15627 w->cursor.vpos)->ends_at_zv_p;
15628 if (!at_zv_p
15629 && w->cursor.hpos >= 0
15630 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15631 w->cursor.vpos))
15633 struct glyph_row *candidate =
15634 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15635 struct glyph *g =
15636 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15637 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15639 exact_match_p =
15640 (BUFFERP (g->object) && g->charpos == PT)
15641 || (NILP (g->object)
15642 && (g->charpos == PT
15643 || (g->charpos == 0 && endpos - 1 == PT)));
15645 if (at_zv_p || exact_match_p)
15647 rc = CURSOR_MOVEMENT_SUCCESS;
15648 break;
15651 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15652 break;
15653 ++row;
15655 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15656 || row->continued_p)
15657 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15658 || (MATRIX_ROW_START_CHARPOS (row) == PT
15659 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15660 /* If we didn't find any candidate rows, or exited the
15661 loop before all the candidates were examined, signal
15662 to the caller that this method failed. */
15663 if (rc != CURSOR_MOVEMENT_SUCCESS
15664 && !(rv
15665 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15666 && !row->continued_p))
15667 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15668 else if (rv)
15669 rc = CURSOR_MOVEMENT_SUCCESS;
15671 else
15675 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15677 rc = CURSOR_MOVEMENT_SUCCESS;
15678 break;
15680 ++row;
15682 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15683 && MATRIX_ROW_START_CHARPOS (row) == PT
15684 && cursor_row_p (row));
15689 return rc;
15693 void
15694 set_vertical_scroll_bar (struct window *w)
15696 ptrdiff_t start, end, whole;
15698 /* Calculate the start and end positions for the current window.
15699 At some point, it would be nice to choose between scrollbars
15700 which reflect the whole buffer size, with special markers
15701 indicating narrowing, and scrollbars which reflect only the
15702 visible region.
15704 Note that mini-buffers sometimes aren't displaying any text. */
15705 if (!MINI_WINDOW_P (w)
15706 || (w == XWINDOW (minibuf_window)
15707 && NILP (echo_area_buffer[0])))
15709 struct buffer *buf = XBUFFER (w->contents);
15710 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15711 start = marker_position (w->start) - BUF_BEGV (buf);
15712 /* I don't think this is guaranteed to be right. For the
15713 moment, we'll pretend it is. */
15714 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15716 if (end < start)
15717 end = start;
15718 if (whole < (end - start))
15719 whole = end - start;
15721 else
15722 start = end = whole = 0;
15724 /* Indicate what this scroll bar ought to be displaying now. */
15725 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15726 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15727 (w, end - start, whole, start);
15731 void
15732 set_horizontal_scroll_bar (struct window *w)
15734 int start, end, whole, portion;
15736 if (!MINI_WINDOW_P (w)
15737 || (w == XWINDOW (minibuf_window)
15738 && NILP (echo_area_buffer[0])))
15740 struct buffer *b = XBUFFER (w->contents);
15741 struct buffer *old_buffer = NULL;
15742 struct it it;
15743 struct text_pos startp;
15745 if (b != current_buffer)
15747 old_buffer = current_buffer;
15748 set_buffer_internal (b);
15751 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15752 start_display (&it, w, startp);
15753 it.last_visible_x = INT_MAX;
15754 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15755 MOVE_TO_X | MOVE_TO_Y);
15756 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15757 window_box_height (w), -1,
15758 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15760 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15761 end = start + window_box_width (w, TEXT_AREA);
15762 portion = end - start;
15763 /* After enlarging a horizontally scrolled window such that it
15764 gets at least as wide as the text it contains, make sure that
15765 the thumb doesn't fill the entire scroll bar so we can still
15766 drag it back to see the entire text. */
15767 whole = max (whole, end);
15769 if (it.bidi_p)
15771 Lisp_Object pdir;
15773 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15774 if (EQ (pdir, Qright_to_left))
15776 start = whole - end;
15777 end = start + portion;
15781 if (old_buffer)
15782 set_buffer_internal (old_buffer);
15784 else
15785 start = end = whole = portion = 0;
15787 w->hscroll_whole = whole;
15789 /* Indicate what this scroll bar ought to be displaying now. */
15790 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15791 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15792 (w, portion, whole, start);
15796 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15797 selected_window is redisplayed.
15799 We can return without actually redisplaying the window if fonts has been
15800 changed on window's frame. In that case, redisplay_internal will retry.
15802 As one of the important parts of redisplaying a window, we need to
15803 decide whether the previous window-start position (stored in the
15804 window's w->start marker position) is still valid, and if it isn't,
15805 recompute it. Some details about that:
15807 . The previous window-start could be in a continuation line, in
15808 which case we need to recompute it when the window width
15809 changes. See compute_window_start_on_continuation_line and its
15810 call below.
15812 . The text that changed since last redisplay could include the
15813 previous window-start position. In that case, we try to salvage
15814 what we can from the current glyph matrix by calling
15815 try_scrolling, which see.
15817 . Some Emacs command could force us to use a specific window-start
15818 position by setting the window's force_start flag, or gently
15819 propose doing that by setting the window's optional_new_start
15820 flag. In these cases, we try using the specified start point if
15821 that succeeds (i.e. the window desired matrix is successfully
15822 recomputed, and point location is within the window). In case
15823 of optional_new_start, we first check if the specified start
15824 position is feasible, i.e. if it will allow point to be
15825 displayed in the window. If using the specified start point
15826 fails, e.g., if new fonts are needed to be loaded, we abort the
15827 redisplay cycle and leave it up to the next cycle to figure out
15828 things.
15830 . Note that the window's force_start flag is sometimes set by
15831 redisplay itself, when it decides that the previous window start
15832 point is fine and should be kept. Search for "goto force_start"
15833 below to see the details. Like the values of window-start
15834 specified outside of redisplay, these internally-deduced values
15835 are tested for feasibility, and ignored if found to be
15836 unfeasible.
15838 . Note that the function try_window, used to completely redisplay
15839 a window, accepts the window's start point as its argument.
15840 This is used several times in the redisplay code to control
15841 where the window start will be, according to user options such
15842 as scroll-conservatively, and also to ensure the screen line
15843 showing point will be fully (as opposed to partially) visible on
15844 display. */
15846 static void
15847 redisplay_window (Lisp_Object window, bool just_this_one_p)
15849 struct window *w = XWINDOW (window);
15850 struct frame *f = XFRAME (w->frame);
15851 struct buffer *buffer = XBUFFER (w->contents);
15852 struct buffer *old = current_buffer;
15853 struct text_pos lpoint, opoint, startp;
15854 bool update_mode_line;
15855 int tem;
15856 struct it it;
15857 /* Record it now because it's overwritten. */
15858 bool current_matrix_up_to_date_p = false;
15859 bool used_current_matrix_p = false;
15860 /* This is less strict than current_matrix_up_to_date_p.
15861 It indicates that the buffer contents and narrowing are unchanged. */
15862 bool buffer_unchanged_p = false;
15863 bool temp_scroll_step = false;
15864 ptrdiff_t count = SPECPDL_INDEX ();
15865 int rc;
15866 int centering_position = -1;
15867 bool last_line_misfit = false;
15868 ptrdiff_t beg_unchanged, end_unchanged;
15869 int frame_line_height;
15871 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15872 opoint = lpoint;
15874 #ifdef GLYPH_DEBUG
15875 *w->desired_matrix->method = 0;
15876 #endif
15878 if (!just_this_one_p
15879 && REDISPLAY_SOME_P ()
15880 && !w->redisplay
15881 && !w->update_mode_line
15882 && !f->redisplay
15883 && !buffer->text->redisplay
15884 && BUF_PT (buffer) == w->last_point)
15885 return;
15887 /* Make sure that both W's markers are valid. */
15888 eassert (XMARKER (w->start)->buffer == buffer);
15889 eassert (XMARKER (w->pointm)->buffer == buffer);
15891 /* We come here again if we need to run window-text-change-functions
15892 below. */
15893 restart:
15894 reconsider_clip_changes (w);
15895 frame_line_height = default_line_pixel_height (w);
15897 /* Has the mode line to be updated? */
15898 update_mode_line = (w->update_mode_line
15899 || update_mode_lines
15900 || buffer->clip_changed
15901 || buffer->prevent_redisplay_optimizations_p);
15903 if (!just_this_one_p)
15904 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15905 cleverly elsewhere. */
15906 w->must_be_updated_p = true;
15908 if (MINI_WINDOW_P (w))
15910 if (w == XWINDOW (echo_area_window)
15911 && !NILP (echo_area_buffer[0]))
15913 if (update_mode_line)
15914 /* We may have to update a tty frame's menu bar or a
15915 tool-bar. Example `M-x C-h C-h C-g'. */
15916 goto finish_menu_bars;
15917 else
15918 /* We've already displayed the echo area glyphs in this window. */
15919 goto finish_scroll_bars;
15921 else if ((w != XWINDOW (minibuf_window)
15922 || minibuf_level == 0)
15923 /* When buffer is nonempty, redisplay window normally. */
15924 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15925 /* Quail displays non-mini buffers in minibuffer window.
15926 In that case, redisplay the window normally. */
15927 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15929 /* W is a mini-buffer window, but it's not active, so clear
15930 it. */
15931 int yb = window_text_bottom_y (w);
15932 struct glyph_row *row;
15933 int y;
15935 for (y = 0, row = w->desired_matrix->rows;
15936 y < yb;
15937 y += row->height, ++row)
15938 blank_row (w, row, y);
15939 goto finish_scroll_bars;
15942 clear_glyph_matrix (w->desired_matrix);
15945 /* Otherwise set up data on this window; select its buffer and point
15946 value. */
15947 /* Really select the buffer, for the sake of buffer-local
15948 variables. */
15949 set_buffer_internal_1 (XBUFFER (w->contents));
15951 current_matrix_up_to_date_p
15952 = (w->window_end_valid
15953 && !current_buffer->clip_changed
15954 && !current_buffer->prevent_redisplay_optimizations_p
15955 && !window_outdated (w));
15957 /* Run the window-text-change-functions
15958 if it is possible that the text on the screen has changed
15959 (either due to modification of the text, or any other reason). */
15960 if (!current_matrix_up_to_date_p
15961 && !NILP (Vwindow_text_change_functions))
15963 safe_run_hooks (Qwindow_text_change_functions);
15964 goto restart;
15967 beg_unchanged = BEG_UNCHANGED;
15968 end_unchanged = END_UNCHANGED;
15970 SET_TEXT_POS (opoint, PT, PT_BYTE);
15972 specbind (Qinhibit_point_motion_hooks, Qt);
15974 buffer_unchanged_p
15975 = (w->window_end_valid
15976 && !current_buffer->clip_changed
15977 && !window_outdated (w));
15979 /* When windows_or_buffers_changed is non-zero, we can't rely
15980 on the window end being valid, so set it to zero there. */
15981 if (windows_or_buffers_changed)
15983 /* If window starts on a continuation line, maybe adjust the
15984 window start in case the window's width changed. */
15985 if (XMARKER (w->start)->buffer == current_buffer)
15986 compute_window_start_on_continuation_line (w);
15988 w->window_end_valid = false;
15989 /* If so, we also can't rely on current matrix
15990 and should not fool try_cursor_movement below. */
15991 current_matrix_up_to_date_p = false;
15994 /* Some sanity checks. */
15995 CHECK_WINDOW_END (w);
15996 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15997 emacs_abort ();
15998 if (BYTEPOS (opoint) < CHARPOS (opoint))
15999 emacs_abort ();
16001 if (mode_line_update_needed (w))
16002 update_mode_line = true;
16004 /* Point refers normally to the selected window. For any other
16005 window, set up appropriate value. */
16006 if (!EQ (window, selected_window))
16008 ptrdiff_t new_pt = marker_position (w->pointm);
16009 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16011 if (new_pt < BEGV)
16013 new_pt = BEGV;
16014 new_pt_byte = BEGV_BYTE;
16015 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16017 else if (new_pt > (ZV - 1))
16019 new_pt = ZV;
16020 new_pt_byte = ZV_BYTE;
16021 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16024 /* We don't use SET_PT so that the point-motion hooks don't run. */
16025 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16028 /* If any of the character widths specified in the display table
16029 have changed, invalidate the width run cache. It's true that
16030 this may be a bit late to catch such changes, but the rest of
16031 redisplay goes (non-fatally) haywire when the display table is
16032 changed, so why should we worry about doing any better? */
16033 if (current_buffer->width_run_cache
16034 || (current_buffer->base_buffer
16035 && current_buffer->base_buffer->width_run_cache))
16037 struct Lisp_Char_Table *disptab = buffer_display_table ();
16039 if (! disptab_matches_widthtab
16040 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16042 struct buffer *buf = current_buffer;
16044 if (buf->base_buffer)
16045 buf = buf->base_buffer;
16046 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16047 recompute_width_table (current_buffer, disptab);
16051 /* If window-start is screwed up, choose a new one. */
16052 if (XMARKER (w->start)->buffer != current_buffer)
16053 goto recenter;
16055 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16057 /* If someone specified a new starting point but did not insist,
16058 check whether it can be used. */
16059 if ((w->optional_new_start || window_frozen_p (w))
16060 && CHARPOS (startp) >= BEGV
16061 && CHARPOS (startp) <= ZV)
16063 ptrdiff_t it_charpos;
16065 w->optional_new_start = false;
16066 start_display (&it, w, startp);
16067 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16068 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16069 /* Record IT's position now, since line_bottom_y might change
16070 that. */
16071 it_charpos = IT_CHARPOS (it);
16072 /* Make sure we set the force_start flag only if the cursor row
16073 will be fully visible. Otherwise, the code under force_start
16074 label below will try to move point back into view, which is
16075 not what the code which sets optional_new_start wants. */
16076 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16077 && !w->force_start)
16079 if (it_charpos == PT)
16080 w->force_start = true;
16081 /* IT may overshoot PT if text at PT is invisible. */
16082 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16083 w->force_start = true;
16084 #ifdef GLYPH_DEBUG
16085 if (w->force_start)
16087 if (window_frozen_p (w))
16088 debug_method_add (w, "set force_start from frozen window start");
16089 else
16090 debug_method_add (w, "set force_start from optional_new_start");
16092 #endif
16096 force_start:
16098 /* Handle case where place to start displaying has been specified,
16099 unless the specified location is outside the accessible range. */
16100 if (w->force_start)
16102 /* We set this later on if we have to adjust point. */
16103 int new_vpos = -1;
16105 w->force_start = false;
16106 w->vscroll = 0;
16107 w->window_end_valid = false;
16109 /* Forget any recorded base line for line number display. */
16110 if (!buffer_unchanged_p)
16111 w->base_line_number = 0;
16113 /* Redisplay the mode line. Select the buffer properly for that.
16114 Also, run the hook window-scroll-functions
16115 because we have scrolled. */
16116 /* Note, we do this after clearing force_start because
16117 if there's an error, it is better to forget about force_start
16118 than to get into an infinite loop calling the hook functions
16119 and having them get more errors. */
16120 if (!update_mode_line
16121 || ! NILP (Vwindow_scroll_functions))
16123 update_mode_line = true;
16124 w->update_mode_line = true;
16125 startp = run_window_scroll_functions (window, startp);
16128 if (CHARPOS (startp) < BEGV)
16129 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16130 else if (CHARPOS (startp) > ZV)
16131 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16133 /* Redisplay, then check if cursor has been set during the
16134 redisplay. Give up if new fonts were loaded. */
16135 /* We used to issue a CHECK_MARGINS argument to try_window here,
16136 but this causes scrolling to fail when point begins inside
16137 the scroll margin (bug#148) -- cyd */
16138 if (!try_window (window, startp, 0))
16140 w->force_start = true;
16141 clear_glyph_matrix (w->desired_matrix);
16142 goto need_larger_matrices;
16145 if (w->cursor.vpos < 0)
16147 /* If point does not appear, try to move point so it does
16148 appear. The desired matrix has been built above, so we
16149 can use it here. */
16150 new_vpos = window_box_height (w) / 2;
16153 if (!cursor_row_fully_visible_p (w, false, false))
16155 /* Point does appear, but on a line partly visible at end of window.
16156 Move it back to a fully-visible line. */
16157 new_vpos = window_box_height (w);
16158 /* But if window_box_height suggests a Y coordinate that is
16159 not less than we already have, that line will clearly not
16160 be fully visible, so give up and scroll the display.
16161 This can happen when the default face uses a font whose
16162 dimensions are different from the frame's default
16163 font. */
16164 if (new_vpos >= w->cursor.y)
16166 w->cursor.vpos = -1;
16167 clear_glyph_matrix (w->desired_matrix);
16168 goto try_to_scroll;
16171 else if (w->cursor.vpos >= 0)
16173 /* Some people insist on not letting point enter the scroll
16174 margin, even though this part handles windows that didn't
16175 scroll at all. */
16176 int window_total_lines
16177 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16178 int margin = min (scroll_margin, window_total_lines / 4);
16179 int pixel_margin = margin * frame_line_height;
16180 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16182 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16183 below, which finds the row to move point to, advances by
16184 the Y coordinate of the _next_ row, see the definition of
16185 MATRIX_ROW_BOTTOM_Y. */
16186 if (w->cursor.vpos < margin + header_line)
16188 w->cursor.vpos = -1;
16189 clear_glyph_matrix (w->desired_matrix);
16190 goto try_to_scroll;
16192 else
16194 int window_height = window_box_height (w);
16196 if (header_line)
16197 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16198 if (w->cursor.y >= window_height - pixel_margin)
16200 w->cursor.vpos = -1;
16201 clear_glyph_matrix (w->desired_matrix);
16202 goto try_to_scroll;
16207 /* If we need to move point for either of the above reasons,
16208 now actually do it. */
16209 if (new_vpos >= 0)
16211 struct glyph_row *row;
16213 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16214 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16215 ++row;
16217 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16218 MATRIX_ROW_START_BYTEPOS (row));
16220 if (w != XWINDOW (selected_window))
16221 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16222 else if (current_buffer == old)
16223 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16225 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16227 /* Re-run pre-redisplay-function so it can update the region
16228 according to the new position of point. */
16229 /* Other than the cursor, w's redisplay is done so we can set its
16230 redisplay to false. Also the buffer's redisplay can be set to
16231 false, since propagate_buffer_redisplay should have already
16232 propagated its info to `w' anyway. */
16233 w->redisplay = false;
16234 XBUFFER (w->contents)->text->redisplay = false;
16235 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16237 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16239 /* pre-redisplay-function made changes (e.g. move the region)
16240 that require another round of redisplay. */
16241 clear_glyph_matrix (w->desired_matrix);
16242 if (!try_window (window, startp, 0))
16243 goto need_larger_matrices;
16246 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16248 clear_glyph_matrix (w->desired_matrix);
16249 goto try_to_scroll;
16252 #ifdef GLYPH_DEBUG
16253 debug_method_add (w, "forced window start");
16254 #endif
16255 goto done;
16258 /* Handle case where text has not changed, only point, and it has
16259 not moved off the frame, and we are not retrying after hscroll.
16260 (current_matrix_up_to_date_p is true when retrying.) */
16261 if (current_matrix_up_to_date_p
16262 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16263 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16265 switch (rc)
16267 case CURSOR_MOVEMENT_SUCCESS:
16268 used_current_matrix_p = true;
16269 goto done;
16271 case CURSOR_MOVEMENT_MUST_SCROLL:
16272 goto try_to_scroll;
16274 default:
16275 emacs_abort ();
16278 /* If current starting point was originally the beginning of a line
16279 but no longer is, find a new starting point. */
16280 else if (w->start_at_line_beg
16281 && !(CHARPOS (startp) <= BEGV
16282 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16284 #ifdef GLYPH_DEBUG
16285 debug_method_add (w, "recenter 1");
16286 #endif
16287 goto recenter;
16290 /* Try scrolling with try_window_id. Value is > 0 if update has
16291 been done, it is -1 if we know that the same window start will
16292 not work. It is 0 if unsuccessful for some other reason. */
16293 else if ((tem = try_window_id (w)) != 0)
16295 #ifdef GLYPH_DEBUG
16296 debug_method_add (w, "try_window_id %d", tem);
16297 #endif
16299 if (f->fonts_changed)
16300 goto need_larger_matrices;
16301 if (tem > 0)
16302 goto done;
16304 /* Otherwise try_window_id has returned -1 which means that we
16305 don't want the alternative below this comment to execute. */
16307 else if (CHARPOS (startp) >= BEGV
16308 && CHARPOS (startp) <= ZV
16309 && PT >= CHARPOS (startp)
16310 && (CHARPOS (startp) < ZV
16311 /* Avoid starting at end of buffer. */
16312 || CHARPOS (startp) == BEGV
16313 || !window_outdated (w)))
16315 int d1, d2, d5, d6;
16316 int rtop, rbot;
16318 /* If first window line is a continuation line, and window start
16319 is inside the modified region, but the first change is before
16320 current window start, we must select a new window start.
16322 However, if this is the result of a down-mouse event (e.g. by
16323 extending the mouse-drag-overlay), we don't want to select a
16324 new window start, since that would change the position under
16325 the mouse, resulting in an unwanted mouse-movement rather
16326 than a simple mouse-click. */
16327 if (!w->start_at_line_beg
16328 && NILP (do_mouse_tracking)
16329 && CHARPOS (startp) > BEGV
16330 && CHARPOS (startp) > BEG + beg_unchanged
16331 && CHARPOS (startp) <= Z - end_unchanged
16332 /* Even if w->start_at_line_beg is nil, a new window may
16333 start at a line_beg, since that's how set_buffer_window
16334 sets it. So, we need to check the return value of
16335 compute_window_start_on_continuation_line. (See also
16336 bug#197). */
16337 && XMARKER (w->start)->buffer == current_buffer
16338 && compute_window_start_on_continuation_line (w)
16339 /* It doesn't make sense to force the window start like we
16340 do at label force_start if it is already known that point
16341 will not be fully visible in the resulting window, because
16342 doing so will move point from its correct position
16343 instead of scrolling the window to bring point into view.
16344 See bug#9324. */
16345 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16346 /* A very tall row could need more than the window height,
16347 in which case we accept that it is partially visible. */
16348 && (rtop != 0) == (rbot != 0))
16350 w->force_start = true;
16351 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16352 #ifdef GLYPH_DEBUG
16353 debug_method_add (w, "recomputed window start in continuation line");
16354 #endif
16355 goto force_start;
16358 #ifdef GLYPH_DEBUG
16359 debug_method_add (w, "same window start");
16360 #endif
16362 /* Try to redisplay starting at same place as before.
16363 If point has not moved off frame, accept the results. */
16364 if (!current_matrix_up_to_date_p
16365 /* Don't use try_window_reusing_current_matrix in this case
16366 because a window scroll function can have changed the
16367 buffer. */
16368 || !NILP (Vwindow_scroll_functions)
16369 || MINI_WINDOW_P (w)
16370 || !(used_current_matrix_p
16371 = try_window_reusing_current_matrix (w)))
16373 IF_DEBUG (debug_method_add (w, "1"));
16374 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16375 /* -1 means we need to scroll.
16376 0 means we need new matrices, but fonts_changed
16377 is set in that case, so we will detect it below. */
16378 goto try_to_scroll;
16381 if (f->fonts_changed)
16382 goto need_larger_matrices;
16384 if (w->cursor.vpos >= 0)
16386 if (!just_this_one_p
16387 || current_buffer->clip_changed
16388 || BEG_UNCHANGED < CHARPOS (startp))
16389 /* Forget any recorded base line for line number display. */
16390 w->base_line_number = 0;
16392 if (!cursor_row_fully_visible_p (w, true, false))
16394 clear_glyph_matrix (w->desired_matrix);
16395 last_line_misfit = true;
16397 /* Drop through and scroll. */
16398 else
16399 goto done;
16401 else
16402 clear_glyph_matrix (w->desired_matrix);
16405 try_to_scroll:
16407 /* Redisplay the mode line. Select the buffer properly for that. */
16408 if (!update_mode_line)
16410 update_mode_line = true;
16411 w->update_mode_line = true;
16414 /* Try to scroll by specified few lines. */
16415 if ((scroll_conservatively
16416 || emacs_scroll_step
16417 || temp_scroll_step
16418 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16419 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16420 && CHARPOS (startp) >= BEGV
16421 && CHARPOS (startp) <= ZV)
16423 /* The function returns -1 if new fonts were loaded, 1 if
16424 successful, 0 if not successful. */
16425 int ss = try_scrolling (window, just_this_one_p,
16426 scroll_conservatively,
16427 emacs_scroll_step,
16428 temp_scroll_step, last_line_misfit);
16429 switch (ss)
16431 case SCROLLING_SUCCESS:
16432 goto done;
16434 case SCROLLING_NEED_LARGER_MATRICES:
16435 goto need_larger_matrices;
16437 case SCROLLING_FAILED:
16438 break;
16440 default:
16441 emacs_abort ();
16445 /* Finally, just choose a place to start which positions point
16446 according to user preferences. */
16448 recenter:
16450 #ifdef GLYPH_DEBUG
16451 debug_method_add (w, "recenter");
16452 #endif
16454 /* Forget any previously recorded base line for line number display. */
16455 if (!buffer_unchanged_p)
16456 w->base_line_number = 0;
16458 /* Determine the window start relative to point. */
16459 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16460 it.current_y = it.last_visible_y;
16461 if (centering_position < 0)
16463 int window_total_lines
16464 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16465 int margin
16466 = scroll_margin > 0
16467 ? min (scroll_margin, window_total_lines / 4)
16468 : 0;
16469 ptrdiff_t margin_pos = CHARPOS (startp);
16470 Lisp_Object aggressive;
16471 bool scrolling_up;
16473 /* If there is a scroll margin at the top of the window, find
16474 its character position. */
16475 if (margin
16476 /* Cannot call start_display if startp is not in the
16477 accessible region of the buffer. This can happen when we
16478 have just switched to a different buffer and/or changed
16479 its restriction. In that case, startp is initialized to
16480 the character position 1 (BEGV) because we did not yet
16481 have chance to display the buffer even once. */
16482 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16484 struct it it1;
16485 void *it1data = NULL;
16487 SAVE_IT (it1, it, it1data);
16488 start_display (&it1, w, startp);
16489 move_it_vertically (&it1, margin * frame_line_height);
16490 margin_pos = IT_CHARPOS (it1);
16491 RESTORE_IT (&it, &it, it1data);
16493 scrolling_up = PT > margin_pos;
16494 aggressive =
16495 scrolling_up
16496 ? BVAR (current_buffer, scroll_up_aggressively)
16497 : BVAR (current_buffer, scroll_down_aggressively);
16499 if (!MINI_WINDOW_P (w)
16500 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16502 int pt_offset = 0;
16504 /* Setting scroll-conservatively overrides
16505 scroll-*-aggressively. */
16506 if (!scroll_conservatively && NUMBERP (aggressive))
16508 double float_amount = XFLOATINT (aggressive);
16510 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16511 if (pt_offset == 0 && float_amount > 0)
16512 pt_offset = 1;
16513 if (pt_offset && margin > 0)
16514 margin -= 1;
16516 /* Compute how much to move the window start backward from
16517 point so that point will be displayed where the user
16518 wants it. */
16519 if (scrolling_up)
16521 centering_position = it.last_visible_y;
16522 if (pt_offset)
16523 centering_position -= pt_offset;
16524 centering_position -=
16525 (frame_line_height * (1 + margin + last_line_misfit)
16526 + WINDOW_HEADER_LINE_HEIGHT (w));
16527 /* Don't let point enter the scroll margin near top of
16528 the window. */
16529 if (centering_position < margin * frame_line_height)
16530 centering_position = margin * frame_line_height;
16532 else
16533 centering_position = margin * frame_line_height + pt_offset;
16535 else
16536 /* Set the window start half the height of the window backward
16537 from point. */
16538 centering_position = window_box_height (w) / 2;
16540 move_it_vertically_backward (&it, centering_position);
16542 eassert (IT_CHARPOS (it) >= BEGV);
16544 /* The function move_it_vertically_backward may move over more
16545 than the specified y-distance. If it->w is small, e.g. a
16546 mini-buffer window, we may end up in front of the window's
16547 display area. Start displaying at the start of the line
16548 containing PT in this case. */
16549 if (it.current_y <= 0)
16551 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16552 move_it_vertically_backward (&it, 0);
16553 it.current_y = 0;
16556 it.current_x = it.hpos = 0;
16558 /* Set the window start position here explicitly, to avoid an
16559 infinite loop in case the functions in window-scroll-functions
16560 get errors. */
16561 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16563 /* Run scroll hooks. */
16564 startp = run_window_scroll_functions (window, it.current.pos);
16566 /* Redisplay the window. */
16567 if (!current_matrix_up_to_date_p
16568 || windows_or_buffers_changed
16569 || f->cursor_type_changed
16570 /* Don't use try_window_reusing_current_matrix in this case
16571 because it can have changed the buffer. */
16572 || !NILP (Vwindow_scroll_functions)
16573 || !just_this_one_p
16574 || MINI_WINDOW_P (w)
16575 || !(used_current_matrix_p
16576 = try_window_reusing_current_matrix (w)))
16577 try_window (window, startp, 0);
16579 /* If new fonts have been loaded (due to fontsets), give up. We
16580 have to start a new redisplay since we need to re-adjust glyph
16581 matrices. */
16582 if (f->fonts_changed)
16583 goto need_larger_matrices;
16585 /* If cursor did not appear assume that the middle of the window is
16586 in the first line of the window. Do it again with the next line.
16587 (Imagine a window of height 100, displaying two lines of height
16588 60. Moving back 50 from it->last_visible_y will end in the first
16589 line.) */
16590 if (w->cursor.vpos < 0)
16592 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16594 clear_glyph_matrix (w->desired_matrix);
16595 move_it_by_lines (&it, 1);
16596 try_window (window, it.current.pos, 0);
16598 else if (PT < IT_CHARPOS (it))
16600 clear_glyph_matrix (w->desired_matrix);
16601 move_it_by_lines (&it, -1);
16602 try_window (window, it.current.pos, 0);
16604 else
16606 /* Not much we can do about it. */
16610 /* Consider the following case: Window starts at BEGV, there is
16611 invisible, intangible text at BEGV, so that display starts at
16612 some point START > BEGV. It can happen that we are called with
16613 PT somewhere between BEGV and START. Try to handle that case,
16614 and similar ones. */
16615 if (w->cursor.vpos < 0)
16617 /* First, try locating the proper glyph row for PT. */
16618 struct glyph_row *row =
16619 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16621 /* Sometimes point is at the beginning of invisible text that is
16622 before the 1st character displayed in the row. In that case,
16623 row_containing_pos fails to find the row, because no glyphs
16624 with appropriate buffer positions are present in the row.
16625 Therefore, we next try to find the row which shows the 1st
16626 position after the invisible text. */
16627 if (!row)
16629 Lisp_Object val =
16630 get_char_property_and_overlay (make_number (PT), Qinvisible,
16631 Qnil, NULL);
16633 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16635 ptrdiff_t alt_pos;
16636 Lisp_Object invis_end =
16637 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16638 Qnil, Qnil);
16640 if (NATNUMP (invis_end))
16641 alt_pos = XFASTINT (invis_end);
16642 else
16643 alt_pos = ZV;
16644 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16645 NULL, 0);
16648 /* Finally, fall back on the first row of the window after the
16649 header line (if any). This is slightly better than not
16650 displaying the cursor at all. */
16651 if (!row)
16653 row = w->current_matrix->rows;
16654 if (row->mode_line_p)
16655 ++row;
16657 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16660 if (!cursor_row_fully_visible_p (w, false, false))
16662 /* If vscroll is enabled, disable it and try again. */
16663 if (w->vscroll)
16665 w->vscroll = 0;
16666 clear_glyph_matrix (w->desired_matrix);
16667 goto recenter;
16670 /* Users who set scroll-conservatively to a large number want
16671 point just above/below the scroll margin. If we ended up
16672 with point's row partially visible, move the window start to
16673 make that row fully visible and out of the margin. */
16674 if (scroll_conservatively > SCROLL_LIMIT)
16676 int window_total_lines
16677 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16678 int margin =
16679 scroll_margin > 0
16680 ? min (scroll_margin, window_total_lines / 4)
16681 : 0;
16682 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16684 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16685 clear_glyph_matrix (w->desired_matrix);
16686 if (1 == try_window (window, it.current.pos,
16687 TRY_WINDOW_CHECK_MARGINS))
16688 goto done;
16691 /* If centering point failed to make the whole line visible,
16692 put point at the top instead. That has to make the whole line
16693 visible, if it can be done. */
16694 if (centering_position == 0)
16695 goto done;
16697 clear_glyph_matrix (w->desired_matrix);
16698 centering_position = 0;
16699 goto recenter;
16702 done:
16704 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16705 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16706 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16708 /* Display the mode line, if we must. */
16709 if ((update_mode_line
16710 /* If window not full width, must redo its mode line
16711 if (a) the window to its side is being redone and
16712 (b) we do a frame-based redisplay. This is a consequence
16713 of how inverted lines are drawn in frame-based redisplay. */
16714 || (!just_this_one_p
16715 && !FRAME_WINDOW_P (f)
16716 && !WINDOW_FULL_WIDTH_P (w))
16717 /* Line number to display. */
16718 || w->base_line_pos > 0
16719 /* Column number is displayed and different from the one displayed. */
16720 || (w->column_number_displayed != -1
16721 && (w->column_number_displayed != current_column ())))
16722 /* This means that the window has a mode line. */
16723 && (WINDOW_WANTS_MODELINE_P (w)
16724 || WINDOW_WANTS_HEADER_LINE_P (w)))
16727 display_mode_lines (w);
16729 /* If mode line height has changed, arrange for a thorough
16730 immediate redisplay using the correct mode line height. */
16731 if (WINDOW_WANTS_MODELINE_P (w)
16732 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16734 f->fonts_changed = true;
16735 w->mode_line_height = -1;
16736 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16737 = DESIRED_MODE_LINE_HEIGHT (w);
16740 /* If header line height has changed, arrange for a thorough
16741 immediate redisplay using the correct header line height. */
16742 if (WINDOW_WANTS_HEADER_LINE_P (w)
16743 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16745 f->fonts_changed = true;
16746 w->header_line_height = -1;
16747 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16748 = DESIRED_HEADER_LINE_HEIGHT (w);
16751 if (f->fonts_changed)
16752 goto need_larger_matrices;
16755 if (!line_number_displayed && w->base_line_pos != -1)
16757 w->base_line_pos = 0;
16758 w->base_line_number = 0;
16761 finish_menu_bars:
16763 /* When we reach a frame's selected window, redo the frame's menu bar. */
16764 if (update_mode_line
16765 && EQ (FRAME_SELECTED_WINDOW (f), window))
16767 bool redisplay_menu_p;
16769 if (FRAME_WINDOW_P (f))
16771 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16772 || defined (HAVE_NS) || defined (USE_GTK)
16773 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16774 #else
16775 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16776 #endif
16778 else
16779 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16781 if (redisplay_menu_p)
16782 display_menu_bar (w);
16784 #ifdef HAVE_WINDOW_SYSTEM
16785 if (FRAME_WINDOW_P (f))
16787 #if defined (USE_GTK) || defined (HAVE_NS)
16788 if (FRAME_EXTERNAL_TOOL_BAR (f))
16789 redisplay_tool_bar (f);
16790 #else
16791 if (WINDOWP (f->tool_bar_window)
16792 && (FRAME_TOOL_BAR_LINES (f) > 0
16793 || !NILP (Vauto_resize_tool_bars))
16794 && redisplay_tool_bar (f))
16795 ignore_mouse_drag_p = true;
16796 #endif
16798 #endif
16801 #ifdef HAVE_WINDOW_SYSTEM
16802 if (FRAME_WINDOW_P (f)
16803 && update_window_fringes (w, (just_this_one_p
16804 || (!used_current_matrix_p && !overlay_arrow_seen)
16805 || w->pseudo_window_p)))
16807 update_begin (f);
16808 block_input ();
16809 if (draw_window_fringes (w, true))
16811 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16812 x_draw_right_divider (w);
16813 else
16814 x_draw_vertical_border (w);
16816 unblock_input ();
16817 update_end (f);
16820 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16821 x_draw_bottom_divider (w);
16822 #endif /* HAVE_WINDOW_SYSTEM */
16824 /* We go to this label, with fonts_changed set, if it is
16825 necessary to try again using larger glyph matrices.
16826 We have to redeem the scroll bar even in this case,
16827 because the loop in redisplay_internal expects that. */
16828 need_larger_matrices:
16830 finish_scroll_bars:
16832 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16834 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16835 /* Set the thumb's position and size. */
16836 set_vertical_scroll_bar (w);
16838 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16839 /* Set the thumb's position and size. */
16840 set_horizontal_scroll_bar (w);
16842 /* Note that we actually used the scroll bar attached to this
16843 window, so it shouldn't be deleted at the end of redisplay. */
16844 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16845 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16848 /* Restore current_buffer and value of point in it. The window
16849 update may have changed the buffer, so first make sure `opoint'
16850 is still valid (Bug#6177). */
16851 if (CHARPOS (opoint) < BEGV)
16852 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16853 else if (CHARPOS (opoint) > ZV)
16854 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16855 else
16856 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16858 set_buffer_internal_1 (old);
16859 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16860 shorter. This can be caused by log truncation in *Messages*. */
16861 if (CHARPOS (lpoint) <= ZV)
16862 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16864 unbind_to (count, Qnil);
16868 /* Build the complete desired matrix of WINDOW with a window start
16869 buffer position POS.
16871 Value is 1 if successful. It is zero if fonts were loaded during
16872 redisplay which makes re-adjusting glyph matrices necessary, and -1
16873 if point would appear in the scroll margins.
16874 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16875 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16876 set in FLAGS.) */
16879 try_window (Lisp_Object window, struct text_pos pos, int flags)
16881 struct window *w = XWINDOW (window);
16882 struct it it;
16883 struct glyph_row *last_text_row = NULL;
16884 struct frame *f = XFRAME (w->frame);
16885 int frame_line_height = default_line_pixel_height (w);
16887 /* Make POS the new window start. */
16888 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16890 /* Mark cursor position as unknown. No overlay arrow seen. */
16891 w->cursor.vpos = -1;
16892 overlay_arrow_seen = false;
16894 /* Initialize iterator and info to start at POS. */
16895 start_display (&it, w, pos);
16896 it.glyph_row->reversed_p = false;
16898 /* Display all lines of W. */
16899 while (it.current_y < it.last_visible_y)
16901 if (display_line (&it))
16902 last_text_row = it.glyph_row - 1;
16903 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16904 return 0;
16907 /* Don't let the cursor end in the scroll margins. */
16908 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16909 && !MINI_WINDOW_P (w))
16911 int this_scroll_margin;
16912 int window_total_lines
16913 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16915 if (scroll_margin > 0)
16917 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16918 this_scroll_margin *= frame_line_height;
16920 else
16921 this_scroll_margin = 0;
16923 if ((w->cursor.y >= 0 /* not vscrolled */
16924 && w->cursor.y < this_scroll_margin
16925 && CHARPOS (pos) > BEGV
16926 && IT_CHARPOS (it) < ZV)
16927 /* rms: considering make_cursor_line_fully_visible_p here
16928 seems to give wrong results. We don't want to recenter
16929 when the last line is partly visible, we want to allow
16930 that case to be handled in the usual way. */
16931 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16933 w->cursor.vpos = -1;
16934 clear_glyph_matrix (w->desired_matrix);
16935 return -1;
16939 /* If bottom moved off end of frame, change mode line percentage. */
16940 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16941 w->update_mode_line = true;
16943 /* Set window_end_pos to the offset of the last character displayed
16944 on the window from the end of current_buffer. Set
16945 window_end_vpos to its row number. */
16946 if (last_text_row)
16948 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16949 adjust_window_ends (w, last_text_row, false);
16950 eassert
16951 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16952 w->window_end_vpos)));
16954 else
16956 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16957 w->window_end_pos = Z - ZV;
16958 w->window_end_vpos = 0;
16961 /* But that is not valid info until redisplay finishes. */
16962 w->window_end_valid = false;
16963 return 1;
16968 /************************************************************************
16969 Window redisplay reusing current matrix when buffer has not changed
16970 ************************************************************************/
16972 /* Try redisplay of window W showing an unchanged buffer with a
16973 different window start than the last time it was displayed by
16974 reusing its current matrix. Value is true if successful.
16975 W->start is the new window start. */
16977 static bool
16978 try_window_reusing_current_matrix (struct window *w)
16980 struct frame *f = XFRAME (w->frame);
16981 struct glyph_row *bottom_row;
16982 struct it it;
16983 struct run run;
16984 struct text_pos start, new_start;
16985 int nrows_scrolled, i;
16986 struct glyph_row *last_text_row;
16987 struct glyph_row *last_reused_text_row;
16988 struct glyph_row *start_row;
16989 int start_vpos, min_y, max_y;
16991 #ifdef GLYPH_DEBUG
16992 if (inhibit_try_window_reusing)
16993 return false;
16994 #endif
16996 if (/* This function doesn't handle terminal frames. */
16997 !FRAME_WINDOW_P (f)
16998 /* Don't try to reuse the display if windows have been split
16999 or such. */
17000 || windows_or_buffers_changed
17001 || f->cursor_type_changed)
17002 return false;
17004 /* Can't do this if showing trailing whitespace. */
17005 if (!NILP (Vshow_trailing_whitespace))
17006 return false;
17008 /* If top-line visibility has changed, give up. */
17009 if (WINDOW_WANTS_HEADER_LINE_P (w)
17010 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17011 return false;
17013 /* Give up if old or new display is scrolled vertically. We could
17014 make this function handle this, but right now it doesn't. */
17015 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17016 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17017 return false;
17019 /* The variable new_start now holds the new window start. The old
17020 start `start' can be determined from the current matrix. */
17021 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17022 start = start_row->minpos;
17023 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17025 /* Clear the desired matrix for the display below. */
17026 clear_glyph_matrix (w->desired_matrix);
17028 if (CHARPOS (new_start) <= CHARPOS (start))
17030 /* Don't use this method if the display starts with an ellipsis
17031 displayed for invisible text. It's not easy to handle that case
17032 below, and it's certainly not worth the effort since this is
17033 not a frequent case. */
17034 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17035 return false;
17037 IF_DEBUG (debug_method_add (w, "twu1"));
17039 /* Display up to a row that can be reused. The variable
17040 last_text_row is set to the last row displayed that displays
17041 text. Note that it.vpos == 0 if or if not there is a
17042 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17043 start_display (&it, w, new_start);
17044 w->cursor.vpos = -1;
17045 last_text_row = last_reused_text_row = NULL;
17047 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17049 /* If we have reached into the characters in the START row,
17050 that means the line boundaries have changed. So we
17051 can't start copying with the row START. Maybe it will
17052 work to start copying with the following row. */
17053 while (IT_CHARPOS (it) > CHARPOS (start))
17055 /* Advance to the next row as the "start". */
17056 start_row++;
17057 start = start_row->minpos;
17058 /* If there are no more rows to try, or just one, give up. */
17059 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17060 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17061 || CHARPOS (start) == ZV)
17063 clear_glyph_matrix (w->desired_matrix);
17064 return false;
17067 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17069 /* If we have reached alignment, we can copy the rest of the
17070 rows. */
17071 if (IT_CHARPOS (it) == CHARPOS (start)
17072 /* Don't accept "alignment" inside a display vector,
17073 since start_row could have started in the middle of
17074 that same display vector (thus their character
17075 positions match), and we have no way of telling if
17076 that is the case. */
17077 && it.current.dpvec_index < 0)
17078 break;
17080 it.glyph_row->reversed_p = false;
17081 if (display_line (&it))
17082 last_text_row = it.glyph_row - 1;
17086 /* A value of current_y < last_visible_y means that we stopped
17087 at the previous window start, which in turn means that we
17088 have at least one reusable row. */
17089 if (it.current_y < it.last_visible_y)
17091 struct glyph_row *row;
17093 /* IT.vpos always starts from 0; it counts text lines. */
17094 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17096 /* Find PT if not already found in the lines displayed. */
17097 if (w->cursor.vpos < 0)
17099 int dy = it.current_y - start_row->y;
17101 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17102 row = row_containing_pos (w, PT, row, NULL, dy);
17103 if (row)
17104 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17105 dy, nrows_scrolled);
17106 else
17108 clear_glyph_matrix (w->desired_matrix);
17109 return false;
17113 /* Scroll the display. Do it before the current matrix is
17114 changed. The problem here is that update has not yet
17115 run, i.e. part of the current matrix is not up to date.
17116 scroll_run_hook will clear the cursor, and use the
17117 current matrix to get the height of the row the cursor is
17118 in. */
17119 run.current_y = start_row->y;
17120 run.desired_y = it.current_y;
17121 run.height = it.last_visible_y - it.current_y;
17123 if (run.height > 0 && run.current_y != run.desired_y)
17125 update_begin (f);
17126 FRAME_RIF (f)->update_window_begin_hook (w);
17127 FRAME_RIF (f)->clear_window_mouse_face (w);
17128 FRAME_RIF (f)->scroll_run_hook (w, &run);
17129 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17130 update_end (f);
17133 /* Shift current matrix down by nrows_scrolled lines. */
17134 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17135 rotate_matrix (w->current_matrix,
17136 start_vpos,
17137 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17138 nrows_scrolled);
17140 /* Disable lines that must be updated. */
17141 for (i = 0; i < nrows_scrolled; ++i)
17142 (start_row + i)->enabled_p = false;
17144 /* Re-compute Y positions. */
17145 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17146 max_y = it.last_visible_y;
17147 for (row = start_row + nrows_scrolled;
17148 row < bottom_row;
17149 ++row)
17151 row->y = it.current_y;
17152 row->visible_height = row->height;
17154 if (row->y < min_y)
17155 row->visible_height -= min_y - row->y;
17156 if (row->y + row->height > max_y)
17157 row->visible_height -= row->y + row->height - max_y;
17158 if (row->fringe_bitmap_periodic_p)
17159 row->redraw_fringe_bitmaps_p = true;
17161 it.current_y += row->height;
17163 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17164 last_reused_text_row = row;
17165 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17166 break;
17169 /* Disable lines in the current matrix which are now
17170 below the window. */
17171 for (++row; row < bottom_row; ++row)
17172 row->enabled_p = row->mode_line_p = false;
17175 /* Update window_end_pos etc.; last_reused_text_row is the last
17176 reused row from the current matrix containing text, if any.
17177 The value of last_text_row is the last displayed line
17178 containing text. */
17179 if (last_reused_text_row)
17180 adjust_window_ends (w, last_reused_text_row, true);
17181 else if (last_text_row)
17182 adjust_window_ends (w, last_text_row, false);
17183 else
17185 /* This window must be completely empty. */
17186 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17187 w->window_end_pos = Z - ZV;
17188 w->window_end_vpos = 0;
17190 w->window_end_valid = false;
17192 /* Update hint: don't try scrolling again in update_window. */
17193 w->desired_matrix->no_scrolling_p = true;
17195 #ifdef GLYPH_DEBUG
17196 debug_method_add (w, "try_window_reusing_current_matrix 1");
17197 #endif
17198 return true;
17200 else if (CHARPOS (new_start) > CHARPOS (start))
17202 struct glyph_row *pt_row, *row;
17203 struct glyph_row *first_reusable_row;
17204 struct glyph_row *first_row_to_display;
17205 int dy;
17206 int yb = window_text_bottom_y (w);
17208 /* Find the row starting at new_start, if there is one. Don't
17209 reuse a partially visible line at the end. */
17210 first_reusable_row = start_row;
17211 while (first_reusable_row->enabled_p
17212 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17213 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17214 < CHARPOS (new_start)))
17215 ++first_reusable_row;
17217 /* Give up if there is no row to reuse. */
17218 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17219 || !first_reusable_row->enabled_p
17220 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17221 != CHARPOS (new_start)))
17222 return false;
17224 /* We can reuse fully visible rows beginning with
17225 first_reusable_row to the end of the window. Set
17226 first_row_to_display to the first row that cannot be reused.
17227 Set pt_row to the row containing point, if there is any. */
17228 pt_row = NULL;
17229 for (first_row_to_display = first_reusable_row;
17230 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17231 ++first_row_to_display)
17233 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17234 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17235 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17236 && first_row_to_display->ends_at_zv_p
17237 && pt_row == NULL)))
17238 pt_row = first_row_to_display;
17241 /* Start displaying at the start of first_row_to_display. */
17242 eassert (first_row_to_display->y < yb);
17243 init_to_row_start (&it, w, first_row_to_display);
17245 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17246 - start_vpos);
17247 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17248 - nrows_scrolled);
17249 it.current_y = (first_row_to_display->y - first_reusable_row->y
17250 + WINDOW_HEADER_LINE_HEIGHT (w));
17252 /* Display lines beginning with first_row_to_display in the
17253 desired matrix. Set last_text_row to the last row displayed
17254 that displays text. */
17255 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17256 if (pt_row == NULL)
17257 w->cursor.vpos = -1;
17258 last_text_row = NULL;
17259 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17260 if (display_line (&it))
17261 last_text_row = it.glyph_row - 1;
17263 /* If point is in a reused row, adjust y and vpos of the cursor
17264 position. */
17265 if (pt_row)
17267 w->cursor.vpos -= nrows_scrolled;
17268 w->cursor.y -= first_reusable_row->y - start_row->y;
17271 /* Give up if point isn't in a row displayed or reused. (This
17272 also handles the case where w->cursor.vpos < nrows_scrolled
17273 after the calls to display_line, which can happen with scroll
17274 margins. See bug#1295.) */
17275 if (w->cursor.vpos < 0)
17277 clear_glyph_matrix (w->desired_matrix);
17278 return false;
17281 /* Scroll the display. */
17282 run.current_y = first_reusable_row->y;
17283 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17284 run.height = it.last_visible_y - run.current_y;
17285 dy = run.current_y - run.desired_y;
17287 if (run.height)
17289 update_begin (f);
17290 FRAME_RIF (f)->update_window_begin_hook (w);
17291 FRAME_RIF (f)->clear_window_mouse_face (w);
17292 FRAME_RIF (f)->scroll_run_hook (w, &run);
17293 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17294 update_end (f);
17297 /* Adjust Y positions of reused rows. */
17298 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17299 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17300 max_y = it.last_visible_y;
17301 for (row = first_reusable_row; row < first_row_to_display; ++row)
17303 row->y -= dy;
17304 row->visible_height = row->height;
17305 if (row->y < min_y)
17306 row->visible_height -= min_y - row->y;
17307 if (row->y + row->height > max_y)
17308 row->visible_height -= row->y + row->height - max_y;
17309 if (row->fringe_bitmap_periodic_p)
17310 row->redraw_fringe_bitmaps_p = true;
17313 /* Scroll the current matrix. */
17314 eassert (nrows_scrolled > 0);
17315 rotate_matrix (w->current_matrix,
17316 start_vpos,
17317 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17318 -nrows_scrolled);
17320 /* Disable rows not reused. */
17321 for (row -= nrows_scrolled; row < bottom_row; ++row)
17322 row->enabled_p = false;
17324 /* Point may have moved to a different line, so we cannot assume that
17325 the previous cursor position is valid; locate the correct row. */
17326 if (pt_row)
17328 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17329 row < bottom_row
17330 && PT >= MATRIX_ROW_END_CHARPOS (row)
17331 && !row->ends_at_zv_p;
17332 row++)
17334 w->cursor.vpos++;
17335 w->cursor.y = row->y;
17337 if (row < bottom_row)
17339 /* Can't simply scan the row for point with
17340 bidi-reordered glyph rows. Let set_cursor_from_row
17341 figure out where to put the cursor, and if it fails,
17342 give up. */
17343 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17345 if (!set_cursor_from_row (w, row, w->current_matrix,
17346 0, 0, 0, 0))
17348 clear_glyph_matrix (w->desired_matrix);
17349 return false;
17352 else
17354 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17355 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17357 for (; glyph < end
17358 && (!BUFFERP (glyph->object)
17359 || glyph->charpos < PT);
17360 glyph++)
17362 w->cursor.hpos++;
17363 w->cursor.x += glyph->pixel_width;
17369 /* Adjust window end. A null value of last_text_row means that
17370 the window end is in reused rows which in turn means that
17371 only its vpos can have changed. */
17372 if (last_text_row)
17373 adjust_window_ends (w, last_text_row, false);
17374 else
17375 w->window_end_vpos -= nrows_scrolled;
17377 w->window_end_valid = false;
17378 w->desired_matrix->no_scrolling_p = true;
17380 #ifdef GLYPH_DEBUG
17381 debug_method_add (w, "try_window_reusing_current_matrix 2");
17382 #endif
17383 return true;
17386 return false;
17391 /************************************************************************
17392 Window redisplay reusing current matrix when buffer has changed
17393 ************************************************************************/
17395 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17396 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17397 ptrdiff_t *, ptrdiff_t *);
17398 static struct glyph_row *
17399 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17400 struct glyph_row *);
17403 /* Return the last row in MATRIX displaying text. If row START is
17404 non-null, start searching with that row. IT gives the dimensions
17405 of the display. Value is null if matrix is empty; otherwise it is
17406 a pointer to the row found. */
17408 static struct glyph_row *
17409 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17410 struct glyph_row *start)
17412 struct glyph_row *row, *row_found;
17414 /* Set row_found to the last row in IT->w's current matrix
17415 displaying text. The loop looks funny but think of partially
17416 visible lines. */
17417 row_found = NULL;
17418 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17419 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17421 eassert (row->enabled_p);
17422 row_found = row;
17423 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17424 break;
17425 ++row;
17428 return row_found;
17432 /* Return the last row in the current matrix of W that is not affected
17433 by changes at the start of current_buffer that occurred since W's
17434 current matrix was built. Value is null if no such row exists.
17436 BEG_UNCHANGED us the number of characters unchanged at the start of
17437 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17438 first changed character in current_buffer. Characters at positions <
17439 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17440 when the current matrix was built. */
17442 static struct glyph_row *
17443 find_last_unchanged_at_beg_row (struct window *w)
17445 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17446 struct glyph_row *row;
17447 struct glyph_row *row_found = NULL;
17448 int yb = window_text_bottom_y (w);
17450 /* Find the last row displaying unchanged text. */
17451 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17452 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17453 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17454 ++row)
17456 if (/* If row ends before first_changed_pos, it is unchanged,
17457 except in some case. */
17458 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17459 /* When row ends in ZV and we write at ZV it is not
17460 unchanged. */
17461 && !row->ends_at_zv_p
17462 /* When first_changed_pos is the end of a continued line,
17463 row is not unchanged because it may be no longer
17464 continued. */
17465 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17466 && (row->continued_p
17467 || row->exact_window_width_line_p))
17468 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17469 needs to be recomputed, so don't consider this row as
17470 unchanged. This happens when the last line was
17471 bidi-reordered and was killed immediately before this
17472 redisplay cycle. In that case, ROW->end stores the
17473 buffer position of the first visual-order character of
17474 the killed text, which is now beyond ZV. */
17475 && CHARPOS (row->end.pos) <= ZV)
17476 row_found = row;
17478 /* Stop if last visible row. */
17479 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17480 break;
17483 return row_found;
17487 /* Find the first glyph row in the current matrix of W that is not
17488 affected by changes at the end of current_buffer since the
17489 time W's current matrix was built.
17491 Return in *DELTA the number of chars by which buffer positions in
17492 unchanged text at the end of current_buffer must be adjusted.
17494 Return in *DELTA_BYTES the corresponding number of bytes.
17496 Value is null if no such row exists, i.e. all rows are affected by
17497 changes. */
17499 static struct glyph_row *
17500 find_first_unchanged_at_end_row (struct window *w,
17501 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17503 struct glyph_row *row;
17504 struct glyph_row *row_found = NULL;
17506 *delta = *delta_bytes = 0;
17508 /* Display must not have been paused, otherwise the current matrix
17509 is not up to date. */
17510 eassert (w->window_end_valid);
17512 /* A value of window_end_pos >= END_UNCHANGED means that the window
17513 end is in the range of changed text. If so, there is no
17514 unchanged row at the end of W's current matrix. */
17515 if (w->window_end_pos >= END_UNCHANGED)
17516 return NULL;
17518 /* Set row to the last row in W's current matrix displaying text. */
17519 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17521 /* If matrix is entirely empty, no unchanged row exists. */
17522 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17524 /* The value of row is the last glyph row in the matrix having a
17525 meaningful buffer position in it. The end position of row
17526 corresponds to window_end_pos. This allows us to translate
17527 buffer positions in the current matrix to current buffer
17528 positions for characters not in changed text. */
17529 ptrdiff_t Z_old =
17530 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17531 ptrdiff_t Z_BYTE_old =
17532 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17533 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17534 struct glyph_row *first_text_row
17535 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17537 *delta = Z - Z_old;
17538 *delta_bytes = Z_BYTE - Z_BYTE_old;
17540 /* Set last_unchanged_pos to the buffer position of the last
17541 character in the buffer that has not been changed. Z is the
17542 index + 1 of the last character in current_buffer, i.e. by
17543 subtracting END_UNCHANGED we get the index of the last
17544 unchanged character, and we have to add BEG to get its buffer
17545 position. */
17546 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17547 last_unchanged_pos_old = last_unchanged_pos - *delta;
17549 /* Search backward from ROW for a row displaying a line that
17550 starts at a minimum position >= last_unchanged_pos_old. */
17551 for (; row > first_text_row; --row)
17553 /* This used to abort, but it can happen.
17554 It is ok to just stop the search instead here. KFS. */
17555 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17556 break;
17558 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17559 row_found = row;
17563 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17565 return row_found;
17569 /* Make sure that glyph rows in the current matrix of window W
17570 reference the same glyph memory as corresponding rows in the
17571 frame's frame matrix. This function is called after scrolling W's
17572 current matrix on a terminal frame in try_window_id and
17573 try_window_reusing_current_matrix. */
17575 static void
17576 sync_frame_with_window_matrix_rows (struct window *w)
17578 struct frame *f = XFRAME (w->frame);
17579 struct glyph_row *window_row, *window_row_end, *frame_row;
17581 /* Preconditions: W must be a leaf window and full-width. Its frame
17582 must have a frame matrix. */
17583 eassert (BUFFERP (w->contents));
17584 eassert (WINDOW_FULL_WIDTH_P (w));
17585 eassert (!FRAME_WINDOW_P (f));
17587 /* If W is a full-width window, glyph pointers in W's current matrix
17588 have, by definition, to be the same as glyph pointers in the
17589 corresponding frame matrix. Note that frame matrices have no
17590 marginal areas (see build_frame_matrix). */
17591 window_row = w->current_matrix->rows;
17592 window_row_end = window_row + w->current_matrix->nrows;
17593 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17594 while (window_row < window_row_end)
17596 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17597 struct glyph *end = window_row->glyphs[LAST_AREA];
17599 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17600 frame_row->glyphs[TEXT_AREA] = start;
17601 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17602 frame_row->glyphs[LAST_AREA] = end;
17604 /* Disable frame rows whose corresponding window rows have
17605 been disabled in try_window_id. */
17606 if (!window_row->enabled_p)
17607 frame_row->enabled_p = false;
17609 ++window_row, ++frame_row;
17614 /* Find the glyph row in window W containing CHARPOS. Consider all
17615 rows between START and END (not inclusive). END null means search
17616 all rows to the end of the display area of W. Value is the row
17617 containing CHARPOS or null. */
17619 struct glyph_row *
17620 row_containing_pos (struct window *w, ptrdiff_t charpos,
17621 struct glyph_row *start, struct glyph_row *end, int dy)
17623 struct glyph_row *row = start;
17624 struct glyph_row *best_row = NULL;
17625 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17626 int last_y;
17628 /* If we happen to start on a header-line, skip that. */
17629 if (row->mode_line_p)
17630 ++row;
17632 if ((end && row >= end) || !row->enabled_p)
17633 return NULL;
17635 last_y = window_text_bottom_y (w) - dy;
17637 while (true)
17639 /* Give up if we have gone too far. */
17640 if (end && row >= end)
17641 return NULL;
17642 /* This formerly returned if they were equal.
17643 I think that both quantities are of a "last plus one" type;
17644 if so, when they are equal, the row is within the screen. -- rms. */
17645 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17646 return NULL;
17648 /* If it is in this row, return this row. */
17649 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17650 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17651 /* The end position of a row equals the start
17652 position of the next row. If CHARPOS is there, we
17653 would rather consider it displayed in the next
17654 line, except when this line ends in ZV. */
17655 && !row_for_charpos_p (row, charpos)))
17656 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17658 struct glyph *g;
17660 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17661 || (!best_row && !row->continued_p))
17662 return row;
17663 /* In bidi-reordered rows, there could be several rows whose
17664 edges surround CHARPOS, all of these rows belonging to
17665 the same continued line. We need to find the row which
17666 fits CHARPOS the best. */
17667 for (g = row->glyphs[TEXT_AREA];
17668 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17669 g++)
17671 if (!STRINGP (g->object))
17673 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17675 mindif = eabs (g->charpos - charpos);
17676 best_row = row;
17677 /* Exact match always wins. */
17678 if (mindif == 0)
17679 return best_row;
17684 else if (best_row && !row->continued_p)
17685 return best_row;
17686 ++row;
17691 /* Try to redisplay window W by reusing its existing display. W's
17692 current matrix must be up to date when this function is called,
17693 i.e., window_end_valid must be true.
17695 Value is
17697 >= 1 if successful, i.e. display has been updated
17698 specifically:
17699 1 means the changes were in front of a newline that precedes
17700 the window start, and the whole current matrix was reused
17701 2 means the changes were after the last position displayed
17702 in the window, and the whole current matrix was reused
17703 3 means portions of the current matrix were reused, while
17704 some of the screen lines were redrawn
17705 -1 if redisplay with same window start is known not to succeed
17706 0 if otherwise unsuccessful
17708 The following steps are performed:
17710 1. Find the last row in the current matrix of W that is not
17711 affected by changes at the start of current_buffer. If no such row
17712 is found, give up.
17714 2. Find the first row in W's current matrix that is not affected by
17715 changes at the end of current_buffer. Maybe there is no such row.
17717 3. Display lines beginning with the row + 1 found in step 1 to the
17718 row found in step 2 or, if step 2 didn't find a row, to the end of
17719 the window.
17721 4. If cursor is not known to appear on the window, give up.
17723 5. If display stopped at the row found in step 2, scroll the
17724 display and current matrix as needed.
17726 6. Maybe display some lines at the end of W, if we must. This can
17727 happen under various circumstances, like a partially visible line
17728 becoming fully visible, or because newly displayed lines are displayed
17729 in smaller font sizes.
17731 7. Update W's window end information. */
17733 static int
17734 try_window_id (struct window *w)
17736 struct frame *f = XFRAME (w->frame);
17737 struct glyph_matrix *current_matrix = w->current_matrix;
17738 struct glyph_matrix *desired_matrix = w->desired_matrix;
17739 struct glyph_row *last_unchanged_at_beg_row;
17740 struct glyph_row *first_unchanged_at_end_row;
17741 struct glyph_row *row;
17742 struct glyph_row *bottom_row;
17743 int bottom_vpos;
17744 struct it it;
17745 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17746 int dvpos, dy;
17747 struct text_pos start_pos;
17748 struct run run;
17749 int first_unchanged_at_end_vpos = 0;
17750 struct glyph_row *last_text_row, *last_text_row_at_end;
17751 struct text_pos start;
17752 ptrdiff_t first_changed_charpos, last_changed_charpos;
17754 #ifdef GLYPH_DEBUG
17755 if (inhibit_try_window_id)
17756 return 0;
17757 #endif
17759 /* This is handy for debugging. */
17760 #if false
17761 #define GIVE_UP(X) \
17762 do { \
17763 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17764 return 0; \
17765 } while (false)
17766 #else
17767 #define GIVE_UP(X) return 0
17768 #endif
17770 SET_TEXT_POS_FROM_MARKER (start, w->start);
17772 /* Don't use this for mini-windows because these can show
17773 messages and mini-buffers, and we don't handle that here. */
17774 if (MINI_WINDOW_P (w))
17775 GIVE_UP (1);
17777 /* This flag is used to prevent redisplay optimizations. */
17778 if (windows_or_buffers_changed || f->cursor_type_changed)
17779 GIVE_UP (2);
17781 /* This function's optimizations cannot be used if overlays have
17782 changed in the buffer displayed by the window, so give up if they
17783 have. */
17784 if (w->last_overlay_modified != OVERLAY_MODIFF)
17785 GIVE_UP (21);
17787 /* Verify that narrowing has not changed.
17788 Also verify that we were not told to prevent redisplay optimizations.
17789 It would be nice to further
17790 reduce the number of cases where this prevents try_window_id. */
17791 if (current_buffer->clip_changed
17792 || current_buffer->prevent_redisplay_optimizations_p)
17793 GIVE_UP (3);
17795 /* Window must either use window-based redisplay or be full width. */
17796 if (!FRAME_WINDOW_P (f)
17797 && (!FRAME_LINE_INS_DEL_OK (f)
17798 || !WINDOW_FULL_WIDTH_P (w)))
17799 GIVE_UP (4);
17801 /* Give up if point is known NOT to appear in W. */
17802 if (PT < CHARPOS (start))
17803 GIVE_UP (5);
17805 /* Another way to prevent redisplay optimizations. */
17806 if (w->last_modified == 0)
17807 GIVE_UP (6);
17809 /* Verify that window is not hscrolled. */
17810 if (w->hscroll != 0)
17811 GIVE_UP (7);
17813 /* Verify that display wasn't paused. */
17814 if (!w->window_end_valid)
17815 GIVE_UP (8);
17817 /* Likewise if highlighting trailing whitespace. */
17818 if (!NILP (Vshow_trailing_whitespace))
17819 GIVE_UP (11);
17821 /* Can't use this if overlay arrow position and/or string have
17822 changed. */
17823 if (overlay_arrows_changed_p ())
17824 GIVE_UP (12);
17826 /* When word-wrap is on, adding a space to the first word of a
17827 wrapped line can change the wrap position, altering the line
17828 above it. It might be worthwhile to handle this more
17829 intelligently, but for now just redisplay from scratch. */
17830 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17831 GIVE_UP (21);
17833 /* Under bidi reordering, adding or deleting a character in the
17834 beginning of a paragraph, before the first strong directional
17835 character, can change the base direction of the paragraph (unless
17836 the buffer specifies a fixed paragraph direction), which will
17837 require to redisplay the whole paragraph. It might be worthwhile
17838 to find the paragraph limits and widen the range of redisplayed
17839 lines to that, but for now just give up this optimization and
17840 redisplay from scratch. */
17841 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17842 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17843 GIVE_UP (22);
17845 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17846 only if buffer has really changed. The reason is that the gap is
17847 initially at Z for freshly visited files. The code below would
17848 set end_unchanged to 0 in that case. */
17849 if (MODIFF > SAVE_MODIFF
17850 /* This seems to happen sometimes after saving a buffer. */
17851 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17853 if (GPT - BEG < BEG_UNCHANGED)
17854 BEG_UNCHANGED = GPT - BEG;
17855 if (Z - GPT < END_UNCHANGED)
17856 END_UNCHANGED = Z - GPT;
17859 /* The position of the first and last character that has been changed. */
17860 first_changed_charpos = BEG + BEG_UNCHANGED;
17861 last_changed_charpos = Z - END_UNCHANGED;
17863 /* If window starts after a line end, and the last change is in
17864 front of that newline, then changes don't affect the display.
17865 This case happens with stealth-fontification. Note that although
17866 the display is unchanged, glyph positions in the matrix have to
17867 be adjusted, of course. */
17868 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17869 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17870 && ((last_changed_charpos < CHARPOS (start)
17871 && CHARPOS (start) == BEGV)
17872 || (last_changed_charpos < CHARPOS (start) - 1
17873 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17875 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17876 struct glyph_row *r0;
17878 /* Compute how many chars/bytes have been added to or removed
17879 from the buffer. */
17880 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17881 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17882 Z_delta = Z - Z_old;
17883 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17885 /* Give up if PT is not in the window. Note that it already has
17886 been checked at the start of try_window_id that PT is not in
17887 front of the window start. */
17888 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17889 GIVE_UP (13);
17891 /* If window start is unchanged, we can reuse the whole matrix
17892 as is, after adjusting glyph positions. No need to compute
17893 the window end again, since its offset from Z hasn't changed. */
17894 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17895 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17896 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17897 /* PT must not be in a partially visible line. */
17898 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17899 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17901 /* Adjust positions in the glyph matrix. */
17902 if (Z_delta || Z_delta_bytes)
17904 struct glyph_row *r1
17905 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17906 increment_matrix_positions (w->current_matrix,
17907 MATRIX_ROW_VPOS (r0, current_matrix),
17908 MATRIX_ROW_VPOS (r1, current_matrix),
17909 Z_delta, Z_delta_bytes);
17912 /* Set the cursor. */
17913 row = row_containing_pos (w, PT, r0, NULL, 0);
17914 if (row)
17915 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17916 return 1;
17920 /* Handle the case that changes are all below what is displayed in
17921 the window, and that PT is in the window. This shortcut cannot
17922 be taken if ZV is visible in the window, and text has been added
17923 there that is visible in the window. */
17924 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17925 /* ZV is not visible in the window, or there are no
17926 changes at ZV, actually. */
17927 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17928 || first_changed_charpos == last_changed_charpos))
17930 struct glyph_row *r0;
17932 /* Give up if PT is not in the window. Note that it already has
17933 been checked at the start of try_window_id that PT is not in
17934 front of the window start. */
17935 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17936 GIVE_UP (14);
17938 /* If window start is unchanged, we can reuse the whole matrix
17939 as is, without changing glyph positions since no text has
17940 been added/removed in front of the window end. */
17941 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17942 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17943 /* PT must not be in a partially visible line. */
17944 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17945 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17947 /* We have to compute the window end anew since text
17948 could have been added/removed after it. */
17949 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17950 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17952 /* Set the cursor. */
17953 row = row_containing_pos (w, PT, r0, NULL, 0);
17954 if (row)
17955 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17956 return 2;
17960 /* Give up if window start is in the changed area.
17962 The condition used to read
17964 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17966 but why that was tested escapes me at the moment. */
17967 if (CHARPOS (start) >= first_changed_charpos
17968 && CHARPOS (start) <= last_changed_charpos)
17969 GIVE_UP (15);
17971 /* Check that window start agrees with the start of the first glyph
17972 row in its current matrix. Check this after we know the window
17973 start is not in changed text, otherwise positions would not be
17974 comparable. */
17975 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17976 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17977 GIVE_UP (16);
17979 /* Give up if the window ends in strings. Overlay strings
17980 at the end are difficult to handle, so don't try. */
17981 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17982 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17983 GIVE_UP (20);
17985 /* Compute the position at which we have to start displaying new
17986 lines. Some of the lines at the top of the window might be
17987 reusable because they are not displaying changed text. Find the
17988 last row in W's current matrix not affected by changes at the
17989 start of current_buffer. Value is null if changes start in the
17990 first line of window. */
17991 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17992 if (last_unchanged_at_beg_row)
17994 /* Avoid starting to display in the middle of a character, a TAB
17995 for instance. This is easier than to set up the iterator
17996 exactly, and it's not a frequent case, so the additional
17997 effort wouldn't really pay off. */
17998 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17999 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18000 && last_unchanged_at_beg_row > w->current_matrix->rows)
18001 --last_unchanged_at_beg_row;
18003 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18004 GIVE_UP (17);
18006 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18007 GIVE_UP (18);
18008 start_pos = it.current.pos;
18010 /* Start displaying new lines in the desired matrix at the same
18011 vpos we would use in the current matrix, i.e. below
18012 last_unchanged_at_beg_row. */
18013 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18014 current_matrix);
18015 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18016 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18018 eassert (it.hpos == 0 && it.current_x == 0);
18020 else
18022 /* There are no reusable lines at the start of the window.
18023 Start displaying in the first text line. */
18024 start_display (&it, w, start);
18025 it.vpos = it.first_vpos;
18026 start_pos = it.current.pos;
18029 /* Find the first row that is not affected by changes at the end of
18030 the buffer. Value will be null if there is no unchanged row, in
18031 which case we must redisplay to the end of the window. delta
18032 will be set to the value by which buffer positions beginning with
18033 first_unchanged_at_end_row have to be adjusted due to text
18034 changes. */
18035 first_unchanged_at_end_row
18036 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18037 IF_DEBUG (debug_delta = delta);
18038 IF_DEBUG (debug_delta_bytes = delta_bytes);
18040 /* Set stop_pos to the buffer position up to which we will have to
18041 display new lines. If first_unchanged_at_end_row != NULL, this
18042 is the buffer position of the start of the line displayed in that
18043 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18044 that we don't stop at a buffer position. */
18045 stop_pos = 0;
18046 if (first_unchanged_at_end_row)
18048 eassert (last_unchanged_at_beg_row == NULL
18049 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18051 /* If this is a continuation line, move forward to the next one
18052 that isn't. Changes in lines above affect this line.
18053 Caution: this may move first_unchanged_at_end_row to a row
18054 not displaying text. */
18055 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18056 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18057 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18058 < it.last_visible_y))
18059 ++first_unchanged_at_end_row;
18061 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18062 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18063 >= it.last_visible_y))
18064 first_unchanged_at_end_row = NULL;
18065 else
18067 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18068 + delta);
18069 first_unchanged_at_end_vpos
18070 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18071 eassert (stop_pos >= Z - END_UNCHANGED);
18074 else if (last_unchanged_at_beg_row == NULL)
18075 GIVE_UP (19);
18078 #ifdef GLYPH_DEBUG
18080 /* Either there is no unchanged row at the end, or the one we have
18081 now displays text. This is a necessary condition for the window
18082 end pos calculation at the end of this function. */
18083 eassert (first_unchanged_at_end_row == NULL
18084 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18086 debug_last_unchanged_at_beg_vpos
18087 = (last_unchanged_at_beg_row
18088 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18089 : -1);
18090 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18092 #endif /* GLYPH_DEBUG */
18095 /* Display new lines. Set last_text_row to the last new line
18096 displayed which has text on it, i.e. might end up as being the
18097 line where the window_end_vpos is. */
18098 w->cursor.vpos = -1;
18099 last_text_row = NULL;
18100 overlay_arrow_seen = false;
18101 if (it.current_y < it.last_visible_y
18102 && !f->fonts_changed
18103 && (first_unchanged_at_end_row == NULL
18104 || IT_CHARPOS (it) < stop_pos))
18105 it.glyph_row->reversed_p = false;
18106 while (it.current_y < it.last_visible_y
18107 && !f->fonts_changed
18108 && (first_unchanged_at_end_row == NULL
18109 || IT_CHARPOS (it) < stop_pos))
18111 if (display_line (&it))
18112 last_text_row = it.glyph_row - 1;
18115 if (f->fonts_changed)
18116 return -1;
18118 /* The redisplay iterations in display_line above could have
18119 triggered font-lock, which could have done something that
18120 invalidates IT->w window's end-point information, on which we
18121 rely below. E.g., one package, which will remain unnamed, used
18122 to install a font-lock-fontify-region-function that called
18123 bury-buffer, whose side effect is to switch the buffer displayed
18124 by IT->w, and that predictably resets IT->w's window_end_valid
18125 flag, which we already tested at the entry to this function.
18126 Amply punish such packages/modes by giving up on this
18127 optimization in those cases. */
18128 if (!w->window_end_valid)
18130 clear_glyph_matrix (w->desired_matrix);
18131 return -1;
18134 /* Compute differences in buffer positions, y-positions etc. for
18135 lines reused at the bottom of the window. Compute what we can
18136 scroll. */
18137 if (first_unchanged_at_end_row
18138 /* No lines reused because we displayed everything up to the
18139 bottom of the window. */
18140 && it.current_y < it.last_visible_y)
18142 dvpos = (it.vpos
18143 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18144 current_matrix));
18145 dy = it.current_y - first_unchanged_at_end_row->y;
18146 run.current_y = first_unchanged_at_end_row->y;
18147 run.desired_y = run.current_y + dy;
18148 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18150 else
18152 delta = delta_bytes = dvpos = dy
18153 = run.current_y = run.desired_y = run.height = 0;
18154 first_unchanged_at_end_row = NULL;
18156 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18159 /* Find the cursor if not already found. We have to decide whether
18160 PT will appear on this window (it sometimes doesn't, but this is
18161 not a very frequent case.) This decision has to be made before
18162 the current matrix is altered. A value of cursor.vpos < 0 means
18163 that PT is either in one of the lines beginning at
18164 first_unchanged_at_end_row or below the window. Don't care for
18165 lines that might be displayed later at the window end; as
18166 mentioned, this is not a frequent case. */
18167 if (w->cursor.vpos < 0)
18169 /* Cursor in unchanged rows at the top? */
18170 if (PT < CHARPOS (start_pos)
18171 && last_unchanged_at_beg_row)
18173 row = row_containing_pos (w, PT,
18174 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18175 last_unchanged_at_beg_row + 1, 0);
18176 if (row)
18177 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18180 /* Start from first_unchanged_at_end_row looking for PT. */
18181 else if (first_unchanged_at_end_row)
18183 row = row_containing_pos (w, PT - delta,
18184 first_unchanged_at_end_row, NULL, 0);
18185 if (row)
18186 set_cursor_from_row (w, row, w->current_matrix, delta,
18187 delta_bytes, dy, dvpos);
18190 /* Give up if cursor was not found. */
18191 if (w->cursor.vpos < 0)
18193 clear_glyph_matrix (w->desired_matrix);
18194 return -1;
18198 /* Don't let the cursor end in the scroll margins. */
18200 int this_scroll_margin, cursor_height;
18201 int frame_line_height = default_line_pixel_height (w);
18202 int window_total_lines
18203 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18205 this_scroll_margin =
18206 max (0, min (scroll_margin, window_total_lines / 4));
18207 this_scroll_margin *= frame_line_height;
18208 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18210 if ((w->cursor.y < this_scroll_margin
18211 && CHARPOS (start) > BEGV)
18212 /* Old redisplay didn't take scroll margin into account at the bottom,
18213 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18214 || (w->cursor.y + (make_cursor_line_fully_visible_p
18215 ? cursor_height + this_scroll_margin
18216 : 1)) > it.last_visible_y)
18218 w->cursor.vpos = -1;
18219 clear_glyph_matrix (w->desired_matrix);
18220 return -1;
18224 /* Scroll the display. Do it before changing the current matrix so
18225 that xterm.c doesn't get confused about where the cursor glyph is
18226 found. */
18227 if (dy && run.height)
18229 update_begin (f);
18231 if (FRAME_WINDOW_P (f))
18233 FRAME_RIF (f)->update_window_begin_hook (w);
18234 FRAME_RIF (f)->clear_window_mouse_face (w);
18235 FRAME_RIF (f)->scroll_run_hook (w, &run);
18236 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18238 else
18240 /* Terminal frame. In this case, dvpos gives the number of
18241 lines to scroll by; dvpos < 0 means scroll up. */
18242 int from_vpos
18243 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18244 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18245 int end = (WINDOW_TOP_EDGE_LINE (w)
18246 + WINDOW_WANTS_HEADER_LINE_P (w)
18247 + window_internal_height (w));
18249 #if defined (HAVE_GPM) || defined (MSDOS)
18250 x_clear_window_mouse_face (w);
18251 #endif
18252 /* Perform the operation on the screen. */
18253 if (dvpos > 0)
18255 /* Scroll last_unchanged_at_beg_row to the end of the
18256 window down dvpos lines. */
18257 set_terminal_window (f, end);
18259 /* On dumb terminals delete dvpos lines at the end
18260 before inserting dvpos empty lines. */
18261 if (!FRAME_SCROLL_REGION_OK (f))
18262 ins_del_lines (f, end - dvpos, -dvpos);
18264 /* Insert dvpos empty lines in front of
18265 last_unchanged_at_beg_row. */
18266 ins_del_lines (f, from, dvpos);
18268 else if (dvpos < 0)
18270 /* Scroll up last_unchanged_at_beg_vpos to the end of
18271 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18272 set_terminal_window (f, end);
18274 /* Delete dvpos lines in front of
18275 last_unchanged_at_beg_vpos. ins_del_lines will set
18276 the cursor to the given vpos and emit |dvpos| delete
18277 line sequences. */
18278 ins_del_lines (f, from + dvpos, dvpos);
18280 /* On a dumb terminal insert dvpos empty lines at the
18281 end. */
18282 if (!FRAME_SCROLL_REGION_OK (f))
18283 ins_del_lines (f, end + dvpos, -dvpos);
18286 set_terminal_window (f, 0);
18289 update_end (f);
18292 /* Shift reused rows of the current matrix to the right position.
18293 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18294 text. */
18295 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18296 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18297 if (dvpos < 0)
18299 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18300 bottom_vpos, dvpos);
18301 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18302 bottom_vpos);
18304 else if (dvpos > 0)
18306 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18307 bottom_vpos, dvpos);
18308 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18309 first_unchanged_at_end_vpos + dvpos);
18312 /* For frame-based redisplay, make sure that current frame and window
18313 matrix are in sync with respect to glyph memory. */
18314 if (!FRAME_WINDOW_P (f))
18315 sync_frame_with_window_matrix_rows (w);
18317 /* Adjust buffer positions in reused rows. */
18318 if (delta || delta_bytes)
18319 increment_matrix_positions (current_matrix,
18320 first_unchanged_at_end_vpos + dvpos,
18321 bottom_vpos, delta, delta_bytes);
18323 /* Adjust Y positions. */
18324 if (dy)
18325 shift_glyph_matrix (w, current_matrix,
18326 first_unchanged_at_end_vpos + dvpos,
18327 bottom_vpos, dy);
18329 if (first_unchanged_at_end_row)
18331 first_unchanged_at_end_row += dvpos;
18332 if (first_unchanged_at_end_row->y >= it.last_visible_y
18333 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18334 first_unchanged_at_end_row = NULL;
18337 /* If scrolling up, there may be some lines to display at the end of
18338 the window. */
18339 last_text_row_at_end = NULL;
18340 if (dy < 0)
18342 /* Scrolling up can leave for example a partially visible line
18343 at the end of the window to be redisplayed. */
18344 /* Set last_row to the glyph row in the current matrix where the
18345 window end line is found. It has been moved up or down in
18346 the matrix by dvpos. */
18347 int last_vpos = w->window_end_vpos + dvpos;
18348 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18350 /* If last_row is the window end line, it should display text. */
18351 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18353 /* If window end line was partially visible before, begin
18354 displaying at that line. Otherwise begin displaying with the
18355 line following it. */
18356 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18358 init_to_row_start (&it, w, last_row);
18359 it.vpos = last_vpos;
18360 it.current_y = last_row->y;
18362 else
18364 init_to_row_end (&it, w, last_row);
18365 it.vpos = 1 + last_vpos;
18366 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18367 ++last_row;
18370 /* We may start in a continuation line. If so, we have to
18371 get the right continuation_lines_width and current_x. */
18372 it.continuation_lines_width = last_row->continuation_lines_width;
18373 it.hpos = it.current_x = 0;
18375 /* Display the rest of the lines at the window end. */
18376 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18377 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18379 /* Is it always sure that the display agrees with lines in
18380 the current matrix? I don't think so, so we mark rows
18381 displayed invalid in the current matrix by setting their
18382 enabled_p flag to false. */
18383 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18384 if (display_line (&it))
18385 last_text_row_at_end = it.glyph_row - 1;
18389 /* Update window_end_pos and window_end_vpos. */
18390 if (first_unchanged_at_end_row && !last_text_row_at_end)
18392 /* Window end line if one of the preserved rows from the current
18393 matrix. Set row to the last row displaying text in current
18394 matrix starting at first_unchanged_at_end_row, after
18395 scrolling. */
18396 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18397 row = find_last_row_displaying_text (w->current_matrix, &it,
18398 first_unchanged_at_end_row);
18399 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18400 adjust_window_ends (w, row, true);
18401 eassert (w->window_end_bytepos >= 0);
18402 IF_DEBUG (debug_method_add (w, "A"));
18404 else if (last_text_row_at_end)
18406 adjust_window_ends (w, last_text_row_at_end, false);
18407 eassert (w->window_end_bytepos >= 0);
18408 IF_DEBUG (debug_method_add (w, "B"));
18410 else if (last_text_row)
18412 /* We have displayed either to the end of the window or at the
18413 end of the window, i.e. the last row with text is to be found
18414 in the desired matrix. */
18415 adjust_window_ends (w, last_text_row, false);
18416 eassert (w->window_end_bytepos >= 0);
18418 else if (first_unchanged_at_end_row == NULL
18419 && last_text_row == NULL
18420 && last_text_row_at_end == NULL)
18422 /* Displayed to end of window, but no line containing text was
18423 displayed. Lines were deleted at the end of the window. */
18424 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18425 int vpos = w->window_end_vpos;
18426 struct glyph_row *current_row = current_matrix->rows + vpos;
18427 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18429 for (row = NULL;
18430 row == NULL && vpos >= first_vpos;
18431 --vpos, --current_row, --desired_row)
18433 if (desired_row->enabled_p)
18435 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18436 row = desired_row;
18438 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18439 row = current_row;
18442 eassert (row != NULL);
18443 w->window_end_vpos = vpos + 1;
18444 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18445 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18446 eassert (w->window_end_bytepos >= 0);
18447 IF_DEBUG (debug_method_add (w, "C"));
18449 else
18450 emacs_abort ();
18452 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18453 debug_end_vpos = w->window_end_vpos));
18455 /* Record that display has not been completed. */
18456 w->window_end_valid = false;
18457 w->desired_matrix->no_scrolling_p = true;
18458 return 3;
18460 #undef GIVE_UP
18465 /***********************************************************************
18466 More debugging support
18467 ***********************************************************************/
18469 #ifdef GLYPH_DEBUG
18471 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18472 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18473 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18476 /* Dump the contents of glyph matrix MATRIX on stderr.
18478 GLYPHS 0 means don't show glyph contents.
18479 GLYPHS 1 means show glyphs in short form
18480 GLYPHS > 1 means show glyphs in long form. */
18482 void
18483 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18485 int i;
18486 for (i = 0; i < matrix->nrows; ++i)
18487 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18491 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18492 the glyph row and area where the glyph comes from. */
18494 void
18495 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18497 if (glyph->type == CHAR_GLYPH
18498 || glyph->type == GLYPHLESS_GLYPH)
18500 fprintf (stderr,
18501 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18502 glyph - row->glyphs[TEXT_AREA],
18503 (glyph->type == CHAR_GLYPH
18504 ? 'C'
18505 : 'G'),
18506 glyph->charpos,
18507 (BUFFERP (glyph->object)
18508 ? 'B'
18509 : (STRINGP (glyph->object)
18510 ? 'S'
18511 : (NILP (glyph->object)
18512 ? '0'
18513 : '-'))),
18514 glyph->pixel_width,
18515 glyph->u.ch,
18516 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18517 ? glyph->u.ch
18518 : '.'),
18519 glyph->face_id,
18520 glyph->left_box_line_p,
18521 glyph->right_box_line_p);
18523 else if (glyph->type == STRETCH_GLYPH)
18525 fprintf (stderr,
18526 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18527 glyph - row->glyphs[TEXT_AREA],
18528 'S',
18529 glyph->charpos,
18530 (BUFFERP (glyph->object)
18531 ? 'B'
18532 : (STRINGP (glyph->object)
18533 ? 'S'
18534 : (NILP (glyph->object)
18535 ? '0'
18536 : '-'))),
18537 glyph->pixel_width,
18539 ' ',
18540 glyph->face_id,
18541 glyph->left_box_line_p,
18542 glyph->right_box_line_p);
18544 else if (glyph->type == IMAGE_GLYPH)
18546 fprintf (stderr,
18547 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18548 glyph - row->glyphs[TEXT_AREA],
18549 'I',
18550 glyph->charpos,
18551 (BUFFERP (glyph->object)
18552 ? 'B'
18553 : (STRINGP (glyph->object)
18554 ? 'S'
18555 : (NILP (glyph->object)
18556 ? '0'
18557 : '-'))),
18558 glyph->pixel_width,
18559 glyph->u.img_id,
18560 '.',
18561 glyph->face_id,
18562 glyph->left_box_line_p,
18563 glyph->right_box_line_p);
18565 else if (glyph->type == COMPOSITE_GLYPH)
18567 fprintf (stderr,
18568 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18569 glyph - row->glyphs[TEXT_AREA],
18570 '+',
18571 glyph->charpos,
18572 (BUFFERP (glyph->object)
18573 ? 'B'
18574 : (STRINGP (glyph->object)
18575 ? 'S'
18576 : (NILP (glyph->object)
18577 ? '0'
18578 : '-'))),
18579 glyph->pixel_width,
18580 glyph->u.cmp.id);
18581 if (glyph->u.cmp.automatic)
18582 fprintf (stderr,
18583 "[%d-%d]",
18584 glyph->slice.cmp.from, glyph->slice.cmp.to);
18585 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18586 glyph->face_id,
18587 glyph->left_box_line_p,
18588 glyph->right_box_line_p);
18593 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18594 GLYPHS 0 means don't show glyph contents.
18595 GLYPHS 1 means show glyphs in short form
18596 GLYPHS > 1 means show glyphs in long form. */
18598 void
18599 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18601 if (glyphs != 1)
18603 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18604 fprintf (stderr, "==============================================================================\n");
18606 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18607 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18608 vpos,
18609 MATRIX_ROW_START_CHARPOS (row),
18610 MATRIX_ROW_END_CHARPOS (row),
18611 row->used[TEXT_AREA],
18612 row->contains_overlapping_glyphs_p,
18613 row->enabled_p,
18614 row->truncated_on_left_p,
18615 row->truncated_on_right_p,
18616 row->continued_p,
18617 MATRIX_ROW_CONTINUATION_LINE_P (row),
18618 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18619 row->ends_at_zv_p,
18620 row->fill_line_p,
18621 row->ends_in_middle_of_char_p,
18622 row->starts_in_middle_of_char_p,
18623 row->mouse_face_p,
18624 row->x,
18625 row->y,
18626 row->pixel_width,
18627 row->height,
18628 row->visible_height,
18629 row->ascent,
18630 row->phys_ascent);
18631 /* The next 3 lines should align to "Start" in the header. */
18632 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18633 row->end.overlay_string_index,
18634 row->continuation_lines_width);
18635 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18636 CHARPOS (row->start.string_pos),
18637 CHARPOS (row->end.string_pos));
18638 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18639 row->end.dpvec_index);
18642 if (glyphs > 1)
18644 int area;
18646 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18648 struct glyph *glyph = row->glyphs[area];
18649 struct glyph *glyph_end = glyph + row->used[area];
18651 /* Glyph for a line end in text. */
18652 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18653 ++glyph_end;
18655 if (glyph < glyph_end)
18656 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18658 for (; glyph < glyph_end; ++glyph)
18659 dump_glyph (row, glyph, area);
18662 else if (glyphs == 1)
18664 int area;
18665 char s[SHRT_MAX + 4];
18667 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18669 int i;
18671 for (i = 0; i < row->used[area]; ++i)
18673 struct glyph *glyph = row->glyphs[area] + i;
18674 if (i == row->used[area] - 1
18675 && area == TEXT_AREA
18676 && NILP (glyph->object)
18677 && glyph->type == CHAR_GLYPH
18678 && glyph->u.ch == ' ')
18680 strcpy (&s[i], "[\\n]");
18681 i += 4;
18683 else if (glyph->type == CHAR_GLYPH
18684 && glyph->u.ch < 0x80
18685 && glyph->u.ch >= ' ')
18686 s[i] = glyph->u.ch;
18687 else
18688 s[i] = '.';
18691 s[i] = '\0';
18692 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18698 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18699 Sdump_glyph_matrix, 0, 1, "p",
18700 doc: /* Dump the current matrix of the selected window to stderr.
18701 Shows contents of glyph row structures. With non-nil
18702 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18703 glyphs in short form, otherwise show glyphs in long form.
18705 Interactively, no argument means show glyphs in short form;
18706 with numeric argument, its value is passed as the GLYPHS flag. */)
18707 (Lisp_Object glyphs)
18709 struct window *w = XWINDOW (selected_window);
18710 struct buffer *buffer = XBUFFER (w->contents);
18712 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18713 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18714 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18715 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18716 fprintf (stderr, "=============================================\n");
18717 dump_glyph_matrix (w->current_matrix,
18718 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18719 return Qnil;
18723 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18724 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18725 Only text-mode frames have frame glyph matrices. */)
18726 (void)
18728 struct frame *f = XFRAME (selected_frame);
18730 if (f->current_matrix)
18731 dump_glyph_matrix (f->current_matrix, 1);
18732 else
18733 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18734 return Qnil;
18738 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18739 doc: /* Dump glyph row ROW to stderr.
18740 GLYPH 0 means don't dump glyphs.
18741 GLYPH 1 means dump glyphs in short form.
18742 GLYPH > 1 or omitted means dump glyphs in long form. */)
18743 (Lisp_Object row, Lisp_Object glyphs)
18745 struct glyph_matrix *matrix;
18746 EMACS_INT vpos;
18748 CHECK_NUMBER (row);
18749 matrix = XWINDOW (selected_window)->current_matrix;
18750 vpos = XINT (row);
18751 if (vpos >= 0 && vpos < matrix->nrows)
18752 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18753 vpos,
18754 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18755 return Qnil;
18759 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18760 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18761 GLYPH 0 means don't dump glyphs.
18762 GLYPH 1 means dump glyphs in short form.
18763 GLYPH > 1 or omitted means dump glyphs in long form.
18765 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18766 do nothing. */)
18767 (Lisp_Object row, Lisp_Object glyphs)
18769 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18770 struct frame *sf = SELECTED_FRAME ();
18771 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18772 EMACS_INT vpos;
18774 CHECK_NUMBER (row);
18775 vpos = XINT (row);
18776 if (vpos >= 0 && vpos < m->nrows)
18777 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18778 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18779 #endif
18780 return Qnil;
18784 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18785 doc: /* Toggle tracing of redisplay.
18786 With ARG, turn tracing on if and only if ARG is positive. */)
18787 (Lisp_Object arg)
18789 if (NILP (arg))
18790 trace_redisplay_p = !trace_redisplay_p;
18791 else
18793 arg = Fprefix_numeric_value (arg);
18794 trace_redisplay_p = XINT (arg) > 0;
18797 return Qnil;
18801 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18802 doc: /* Like `format', but print result to stderr.
18803 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18804 (ptrdiff_t nargs, Lisp_Object *args)
18806 Lisp_Object s = Fformat (nargs, args);
18807 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18808 return Qnil;
18811 #endif /* GLYPH_DEBUG */
18815 /***********************************************************************
18816 Building Desired Matrix Rows
18817 ***********************************************************************/
18819 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18820 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18822 static struct glyph_row *
18823 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18825 struct frame *f = XFRAME (WINDOW_FRAME (w));
18826 struct buffer *buffer = XBUFFER (w->contents);
18827 struct buffer *old = current_buffer;
18828 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18829 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18830 const unsigned char *arrow_end = arrow_string + arrow_len;
18831 const unsigned char *p;
18832 struct it it;
18833 bool multibyte_p;
18834 int n_glyphs_before;
18836 set_buffer_temp (buffer);
18837 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18838 scratch_glyph_row.reversed_p = false;
18839 it.glyph_row->used[TEXT_AREA] = 0;
18840 SET_TEXT_POS (it.position, 0, 0);
18842 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18843 p = arrow_string;
18844 while (p < arrow_end)
18846 Lisp_Object face, ilisp;
18848 /* Get the next character. */
18849 if (multibyte_p)
18850 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18851 else
18853 it.c = it.char_to_display = *p, it.len = 1;
18854 if (! ASCII_CHAR_P (it.c))
18855 it.char_to_display = BYTE8_TO_CHAR (it.c);
18857 p += it.len;
18859 /* Get its face. */
18860 ilisp = make_number (p - arrow_string);
18861 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18862 it.face_id = compute_char_face (f, it.char_to_display, face);
18864 /* Compute its width, get its glyphs. */
18865 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18866 SET_TEXT_POS (it.position, -1, -1);
18867 PRODUCE_GLYPHS (&it);
18869 /* If this character doesn't fit any more in the line, we have
18870 to remove some glyphs. */
18871 if (it.current_x > it.last_visible_x)
18873 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18874 break;
18878 set_buffer_temp (old);
18879 return it.glyph_row;
18883 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18884 glyphs to insert is determined by produce_special_glyphs. */
18886 static void
18887 insert_left_trunc_glyphs (struct it *it)
18889 struct it truncate_it;
18890 struct glyph *from, *end, *to, *toend;
18892 eassert (!FRAME_WINDOW_P (it->f)
18893 || (!it->glyph_row->reversed_p
18894 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18895 || (it->glyph_row->reversed_p
18896 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18898 /* Get the truncation glyphs. */
18899 truncate_it = *it;
18900 truncate_it.current_x = 0;
18901 truncate_it.face_id = DEFAULT_FACE_ID;
18902 truncate_it.glyph_row = &scratch_glyph_row;
18903 truncate_it.area = TEXT_AREA;
18904 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18905 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18906 truncate_it.object = Qnil;
18907 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18909 /* Overwrite glyphs from IT with truncation glyphs. */
18910 if (!it->glyph_row->reversed_p)
18912 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18914 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18915 end = from + tused;
18916 to = it->glyph_row->glyphs[TEXT_AREA];
18917 toend = to + it->glyph_row->used[TEXT_AREA];
18918 if (FRAME_WINDOW_P (it->f))
18920 /* On GUI frames, when variable-size fonts are displayed,
18921 the truncation glyphs may need more pixels than the row's
18922 glyphs they overwrite. We overwrite more glyphs to free
18923 enough screen real estate, and enlarge the stretch glyph
18924 on the right (see display_line), if there is one, to
18925 preserve the screen position of the truncation glyphs on
18926 the right. */
18927 int w = 0;
18928 struct glyph *g = to;
18929 short used;
18931 /* The first glyph could be partially visible, in which case
18932 it->glyph_row->x will be negative. But we want the left
18933 truncation glyphs to be aligned at the left margin of the
18934 window, so we override the x coordinate at which the row
18935 will begin. */
18936 it->glyph_row->x = 0;
18937 while (g < toend && w < it->truncation_pixel_width)
18939 w += g->pixel_width;
18940 ++g;
18942 if (g - to - tused > 0)
18944 memmove (to + tused, g, (toend - g) * sizeof(*g));
18945 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18947 used = it->glyph_row->used[TEXT_AREA];
18948 if (it->glyph_row->truncated_on_right_p
18949 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18950 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18951 == STRETCH_GLYPH)
18953 int extra = w - it->truncation_pixel_width;
18955 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18959 while (from < end)
18960 *to++ = *from++;
18962 /* There may be padding glyphs left over. Overwrite them too. */
18963 if (!FRAME_WINDOW_P (it->f))
18965 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18967 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18968 while (from < end)
18969 *to++ = *from++;
18973 if (to > toend)
18974 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18976 else
18978 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18980 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18981 that back to front. */
18982 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18983 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18984 toend = it->glyph_row->glyphs[TEXT_AREA];
18985 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18986 if (FRAME_WINDOW_P (it->f))
18988 int w = 0;
18989 struct glyph *g = to;
18991 while (g >= toend && w < it->truncation_pixel_width)
18993 w += g->pixel_width;
18994 --g;
18996 if (to - g - tused > 0)
18997 to = g + tused;
18998 if (it->glyph_row->truncated_on_right_p
18999 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19000 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19002 int extra = w - it->truncation_pixel_width;
19004 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19008 while (from >= end && to >= toend)
19009 *to-- = *from--;
19010 if (!FRAME_WINDOW_P (it->f))
19012 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19014 from =
19015 truncate_it.glyph_row->glyphs[TEXT_AREA]
19016 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19017 while (from >= end && to >= toend)
19018 *to-- = *from--;
19021 if (from >= end)
19023 /* Need to free some room before prepending additional
19024 glyphs. */
19025 int move_by = from - end + 1;
19026 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19027 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19029 for ( ; g >= g0; g--)
19030 g[move_by] = *g;
19031 while (from >= end)
19032 *to-- = *from--;
19033 it->glyph_row->used[TEXT_AREA] += move_by;
19038 /* Compute the hash code for ROW. */
19039 unsigned
19040 row_hash (struct glyph_row *row)
19042 int area, k;
19043 unsigned hashval = 0;
19045 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19046 for (k = 0; k < row->used[area]; ++k)
19047 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19048 + row->glyphs[area][k].u.val
19049 + row->glyphs[area][k].face_id
19050 + row->glyphs[area][k].padding_p
19051 + (row->glyphs[area][k].type << 2));
19053 return hashval;
19056 /* Compute the pixel height and width of IT->glyph_row.
19058 Most of the time, ascent and height of a display line will be equal
19059 to the max_ascent and max_height values of the display iterator
19060 structure. This is not the case if
19062 1. We hit ZV without displaying anything. In this case, max_ascent
19063 and max_height will be zero.
19065 2. We have some glyphs that don't contribute to the line height.
19066 (The glyph row flag contributes_to_line_height_p is for future
19067 pixmap extensions).
19069 The first case is easily covered by using default values because in
19070 these cases, the line height does not really matter, except that it
19071 must not be zero. */
19073 static void
19074 compute_line_metrics (struct it *it)
19076 struct glyph_row *row = it->glyph_row;
19078 if (FRAME_WINDOW_P (it->f))
19080 int i, min_y, max_y;
19082 /* The line may consist of one space only, that was added to
19083 place the cursor on it. If so, the row's height hasn't been
19084 computed yet. */
19085 if (row->height == 0)
19087 if (it->max_ascent + it->max_descent == 0)
19088 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19089 row->ascent = it->max_ascent;
19090 row->height = it->max_ascent + it->max_descent;
19091 row->phys_ascent = it->max_phys_ascent;
19092 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19093 row->extra_line_spacing = it->max_extra_line_spacing;
19096 /* Compute the width of this line. */
19097 row->pixel_width = row->x;
19098 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19099 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19101 eassert (row->pixel_width >= 0);
19102 eassert (row->ascent >= 0 && row->height > 0);
19104 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19105 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19107 /* If first line's physical ascent is larger than its logical
19108 ascent, use the physical ascent, and make the row taller.
19109 This makes accented characters fully visible. */
19110 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19111 && row->phys_ascent > row->ascent)
19113 row->height += row->phys_ascent - row->ascent;
19114 row->ascent = row->phys_ascent;
19117 /* Compute how much of the line is visible. */
19118 row->visible_height = row->height;
19120 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19121 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19123 if (row->y < min_y)
19124 row->visible_height -= min_y - row->y;
19125 if (row->y + row->height > max_y)
19126 row->visible_height -= row->y + row->height - max_y;
19128 else
19130 row->pixel_width = row->used[TEXT_AREA];
19131 if (row->continued_p)
19132 row->pixel_width -= it->continuation_pixel_width;
19133 else if (row->truncated_on_right_p)
19134 row->pixel_width -= it->truncation_pixel_width;
19135 row->ascent = row->phys_ascent = 0;
19136 row->height = row->phys_height = row->visible_height = 1;
19137 row->extra_line_spacing = 0;
19140 /* Compute a hash code for this row. */
19141 row->hash = row_hash (row);
19143 it->max_ascent = it->max_descent = 0;
19144 it->max_phys_ascent = it->max_phys_descent = 0;
19148 /* Append one space to the glyph row of iterator IT if doing a
19149 window-based redisplay. The space has the same face as
19150 IT->face_id. Value is true if a space was added.
19152 This function is called to make sure that there is always one glyph
19153 at the end of a glyph row that the cursor can be set on under
19154 window-systems. (If there weren't such a glyph we would not know
19155 how wide and tall a box cursor should be displayed).
19157 At the same time this space let's a nicely handle clearing to the
19158 end of the line if the row ends in italic text. */
19160 static bool
19161 append_space_for_newline (struct it *it, bool default_face_p)
19163 if (FRAME_WINDOW_P (it->f))
19165 int n = it->glyph_row->used[TEXT_AREA];
19167 if (it->glyph_row->glyphs[TEXT_AREA] + n
19168 < it->glyph_row->glyphs[1 + TEXT_AREA])
19170 /* Save some values that must not be changed.
19171 Must save IT->c and IT->len because otherwise
19172 ITERATOR_AT_END_P wouldn't work anymore after
19173 append_space_for_newline has been called. */
19174 enum display_element_type saved_what = it->what;
19175 int saved_c = it->c, saved_len = it->len;
19176 int saved_char_to_display = it->char_to_display;
19177 int saved_x = it->current_x;
19178 int saved_face_id = it->face_id;
19179 bool saved_box_end = it->end_of_box_run_p;
19180 struct text_pos saved_pos;
19181 Lisp_Object saved_object;
19182 struct face *face;
19183 struct glyph *g;
19185 saved_object = it->object;
19186 saved_pos = it->position;
19188 it->what = IT_CHARACTER;
19189 memset (&it->position, 0, sizeof it->position);
19190 it->object = Qnil;
19191 it->c = it->char_to_display = ' ';
19192 it->len = 1;
19194 /* If the default face was remapped, be sure to use the
19195 remapped face for the appended newline. */
19196 if (default_face_p)
19197 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19198 else if (it->face_before_selective_p)
19199 it->face_id = it->saved_face_id;
19200 face = FACE_FROM_ID (it->f, it->face_id);
19201 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19202 /* In R2L rows, we will prepend a stretch glyph that will
19203 have the end_of_box_run_p flag set for it, so there's no
19204 need for the appended newline glyph to have that flag
19205 set. */
19206 if (it->glyph_row->reversed_p
19207 /* But if the appended newline glyph goes all the way to
19208 the end of the row, there will be no stretch glyph,
19209 so leave the box flag set. */
19210 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19211 it->end_of_box_run_p = false;
19213 PRODUCE_GLYPHS (it);
19215 /* Make sure this space glyph has the right ascent value, or
19216 else hollow cursor at end of line will look funny. */
19217 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19218 g->ascent = it->glyph_row->ascent;
19220 it->override_ascent = -1;
19221 it->constrain_row_ascent_descent_p = false;
19222 it->current_x = saved_x;
19223 it->object = saved_object;
19224 it->position = saved_pos;
19225 it->what = saved_what;
19226 it->face_id = saved_face_id;
19227 it->len = saved_len;
19228 it->c = saved_c;
19229 it->char_to_display = saved_char_to_display;
19230 it->end_of_box_run_p = saved_box_end;
19231 return true;
19235 return false;
19239 /* Extend the face of the last glyph in the text area of IT->glyph_row
19240 to the end of the display line. Called from display_line. If the
19241 glyph row is empty, add a space glyph to it so that we know the
19242 face to draw. Set the glyph row flag fill_line_p. If the glyph
19243 row is R2L, prepend a stretch glyph to cover the empty space to the
19244 left of the leftmost glyph. */
19246 static void
19247 extend_face_to_end_of_line (struct it *it)
19249 struct face *face, *default_face;
19250 struct frame *f = it->f;
19252 /* If line is already filled, do nothing. Non window-system frames
19253 get a grace of one more ``pixel'' because their characters are
19254 1-``pixel'' wide, so they hit the equality too early. This grace
19255 is needed only for R2L rows that are not continued, to produce
19256 one extra blank where we could display the cursor. */
19257 if ((it->current_x >= it->last_visible_x
19258 + (!FRAME_WINDOW_P (f)
19259 && it->glyph_row->reversed_p
19260 && !it->glyph_row->continued_p))
19261 /* If the window has display margins, we will need to extend
19262 their face even if the text area is filled. */
19263 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19264 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19265 return;
19267 /* The default face, possibly remapped. */
19268 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19270 /* Face extension extends the background and box of IT->face_id
19271 to the end of the line. If the background equals the background
19272 of the frame, we don't have to do anything. */
19273 if (it->face_before_selective_p)
19274 face = FACE_FROM_ID (f, it->saved_face_id);
19275 else
19276 face = FACE_FROM_ID (f, it->face_id);
19278 if (FRAME_WINDOW_P (f)
19279 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19280 && face->box == FACE_NO_BOX
19281 && face->background == FRAME_BACKGROUND_PIXEL (f)
19282 #ifdef HAVE_WINDOW_SYSTEM
19283 && !face->stipple
19284 #endif
19285 && !it->glyph_row->reversed_p)
19286 return;
19288 /* Set the glyph row flag indicating that the face of the last glyph
19289 in the text area has to be drawn to the end of the text area. */
19290 it->glyph_row->fill_line_p = true;
19292 /* If current character of IT is not ASCII, make sure we have the
19293 ASCII face. This will be automatically undone the next time
19294 get_next_display_element returns a multibyte character. Note
19295 that the character will always be single byte in unibyte
19296 text. */
19297 if (!ASCII_CHAR_P (it->c))
19299 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19302 if (FRAME_WINDOW_P (f))
19304 /* If the row is empty, add a space with the current face of IT,
19305 so that we know which face to draw. */
19306 if (it->glyph_row->used[TEXT_AREA] == 0)
19308 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19309 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19310 it->glyph_row->used[TEXT_AREA] = 1;
19312 /* Mode line and the header line don't have margins, and
19313 likewise the frame's tool-bar window, if there is any. */
19314 if (!(it->glyph_row->mode_line_p
19315 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19316 || (WINDOWP (f->tool_bar_window)
19317 && it->w == XWINDOW (f->tool_bar_window))
19318 #endif
19321 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19322 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19324 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19325 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19326 default_face->id;
19327 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19329 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19330 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19332 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19333 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19334 default_face->id;
19335 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19338 #ifdef HAVE_WINDOW_SYSTEM
19339 if (it->glyph_row->reversed_p)
19341 /* Prepend a stretch glyph to the row, such that the
19342 rightmost glyph will be drawn flushed all the way to the
19343 right margin of the window. The stretch glyph that will
19344 occupy the empty space, if any, to the left of the
19345 glyphs. */
19346 struct font *font = face->font ? face->font : FRAME_FONT (f);
19347 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19348 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19349 struct glyph *g;
19350 int row_width, stretch_ascent, stretch_width;
19351 struct text_pos saved_pos;
19352 int saved_face_id;
19353 bool saved_avoid_cursor, saved_box_start;
19355 for (row_width = 0, g = row_start; g < row_end; g++)
19356 row_width += g->pixel_width;
19358 /* FIXME: There are various minor display glitches in R2L
19359 rows when only one of the fringes is missing. The
19360 strange condition below produces the least bad effect. */
19361 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19362 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19363 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19364 stretch_width = window_box_width (it->w, TEXT_AREA);
19365 else
19366 stretch_width = it->last_visible_x - it->first_visible_x;
19367 stretch_width -= row_width;
19369 if (stretch_width > 0)
19371 stretch_ascent =
19372 (((it->ascent + it->descent)
19373 * FONT_BASE (font)) / FONT_HEIGHT (font));
19374 saved_pos = it->position;
19375 memset (&it->position, 0, sizeof it->position);
19376 saved_avoid_cursor = it->avoid_cursor_p;
19377 it->avoid_cursor_p = true;
19378 saved_face_id = it->face_id;
19379 saved_box_start = it->start_of_box_run_p;
19380 /* The last row's stretch glyph should get the default
19381 face, to avoid painting the rest of the window with
19382 the region face, if the region ends at ZV. */
19383 if (it->glyph_row->ends_at_zv_p)
19384 it->face_id = default_face->id;
19385 else
19386 it->face_id = face->id;
19387 it->start_of_box_run_p = false;
19388 append_stretch_glyph (it, Qnil, stretch_width,
19389 it->ascent + it->descent, stretch_ascent);
19390 it->position = saved_pos;
19391 it->avoid_cursor_p = saved_avoid_cursor;
19392 it->face_id = saved_face_id;
19393 it->start_of_box_run_p = saved_box_start;
19395 /* If stretch_width comes out negative, it means that the
19396 last glyph is only partially visible. In R2L rows, we
19397 want the leftmost glyph to be partially visible, so we
19398 need to give the row the corresponding left offset. */
19399 if (stretch_width < 0)
19400 it->glyph_row->x = stretch_width;
19402 #endif /* HAVE_WINDOW_SYSTEM */
19404 else
19406 /* Save some values that must not be changed. */
19407 int saved_x = it->current_x;
19408 struct text_pos saved_pos;
19409 Lisp_Object saved_object;
19410 enum display_element_type saved_what = it->what;
19411 int saved_face_id = it->face_id;
19413 saved_object = it->object;
19414 saved_pos = it->position;
19416 it->what = IT_CHARACTER;
19417 memset (&it->position, 0, sizeof it->position);
19418 it->object = Qnil;
19419 it->c = it->char_to_display = ' ';
19420 it->len = 1;
19422 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19423 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19424 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19425 && !it->glyph_row->mode_line_p
19426 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19428 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19429 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19431 for (it->current_x = 0; g < e; g++)
19432 it->current_x += g->pixel_width;
19434 it->area = LEFT_MARGIN_AREA;
19435 it->face_id = default_face->id;
19436 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19437 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19439 PRODUCE_GLYPHS (it);
19440 /* term.c:produce_glyphs advances it->current_x only for
19441 TEXT_AREA. */
19442 it->current_x += it->pixel_width;
19445 it->current_x = saved_x;
19446 it->area = TEXT_AREA;
19449 /* The last row's blank glyphs should get the default face, to
19450 avoid painting the rest of the window with the region face,
19451 if the region ends at ZV. */
19452 if (it->glyph_row->ends_at_zv_p)
19453 it->face_id = default_face->id;
19454 else
19455 it->face_id = face->id;
19456 PRODUCE_GLYPHS (it);
19458 while (it->current_x <= it->last_visible_x)
19459 PRODUCE_GLYPHS (it);
19461 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19462 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19463 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19464 && !it->glyph_row->mode_line_p
19465 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19467 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19468 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19470 for ( ; g < e; g++)
19471 it->current_x += g->pixel_width;
19473 it->area = RIGHT_MARGIN_AREA;
19474 it->face_id = default_face->id;
19475 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19476 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19478 PRODUCE_GLYPHS (it);
19479 it->current_x += it->pixel_width;
19482 it->area = TEXT_AREA;
19485 /* Don't count these blanks really. It would let us insert a left
19486 truncation glyph below and make us set the cursor on them, maybe. */
19487 it->current_x = saved_x;
19488 it->object = saved_object;
19489 it->position = saved_pos;
19490 it->what = saved_what;
19491 it->face_id = saved_face_id;
19496 /* Value is true if text starting at CHARPOS in current_buffer is
19497 trailing whitespace. */
19499 static bool
19500 trailing_whitespace_p (ptrdiff_t charpos)
19502 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19503 int c = 0;
19505 while (bytepos < ZV_BYTE
19506 && (c = FETCH_CHAR (bytepos),
19507 c == ' ' || c == '\t'))
19508 ++bytepos;
19510 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19512 if (bytepos != PT_BYTE)
19513 return true;
19515 return false;
19519 /* Highlight trailing whitespace, if any, in ROW. */
19521 static void
19522 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19524 int used = row->used[TEXT_AREA];
19526 if (used)
19528 struct glyph *start = row->glyphs[TEXT_AREA];
19529 struct glyph *glyph = start + used - 1;
19531 if (row->reversed_p)
19533 /* Right-to-left rows need to be processed in the opposite
19534 direction, so swap the edge pointers. */
19535 glyph = start;
19536 start = row->glyphs[TEXT_AREA] + used - 1;
19539 /* Skip over glyphs inserted to display the cursor at the
19540 end of a line, for extending the face of the last glyph
19541 to the end of the line on terminals, and for truncation
19542 and continuation glyphs. */
19543 if (!row->reversed_p)
19545 while (glyph >= start
19546 && glyph->type == CHAR_GLYPH
19547 && NILP (glyph->object))
19548 --glyph;
19550 else
19552 while (glyph <= start
19553 && glyph->type == CHAR_GLYPH
19554 && NILP (glyph->object))
19555 ++glyph;
19558 /* If last glyph is a space or stretch, and it's trailing
19559 whitespace, set the face of all trailing whitespace glyphs in
19560 IT->glyph_row to `trailing-whitespace'. */
19561 if ((row->reversed_p ? glyph <= start : glyph >= start)
19562 && BUFFERP (glyph->object)
19563 && (glyph->type == STRETCH_GLYPH
19564 || (glyph->type == CHAR_GLYPH
19565 && glyph->u.ch == ' '))
19566 && trailing_whitespace_p (glyph->charpos))
19568 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19569 if (face_id < 0)
19570 return;
19572 if (!row->reversed_p)
19574 while (glyph >= start
19575 && BUFFERP (glyph->object)
19576 && (glyph->type == STRETCH_GLYPH
19577 || (glyph->type == CHAR_GLYPH
19578 && glyph->u.ch == ' ')))
19579 (glyph--)->face_id = face_id;
19581 else
19583 while (glyph <= start
19584 && BUFFERP (glyph->object)
19585 && (glyph->type == STRETCH_GLYPH
19586 || (glyph->type == CHAR_GLYPH
19587 && glyph->u.ch == ' ')))
19588 (glyph++)->face_id = face_id;
19595 /* Value is true if glyph row ROW should be
19596 considered to hold the buffer position CHARPOS. */
19598 static bool
19599 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19601 bool result = true;
19603 if (charpos == CHARPOS (row->end.pos)
19604 || charpos == MATRIX_ROW_END_CHARPOS (row))
19606 /* Suppose the row ends on a string.
19607 Unless the row is continued, that means it ends on a newline
19608 in the string. If it's anything other than a display string
19609 (e.g., a before-string from an overlay), we don't want the
19610 cursor there. (This heuristic seems to give the optimal
19611 behavior for the various types of multi-line strings.)
19612 One exception: if the string has `cursor' property on one of
19613 its characters, we _do_ want the cursor there. */
19614 if (CHARPOS (row->end.string_pos) >= 0)
19616 if (row->continued_p)
19617 result = true;
19618 else
19620 /* Check for `display' property. */
19621 struct glyph *beg = row->glyphs[TEXT_AREA];
19622 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19623 struct glyph *glyph;
19625 result = false;
19626 for (glyph = end; glyph >= beg; --glyph)
19627 if (STRINGP (glyph->object))
19629 Lisp_Object prop
19630 = Fget_char_property (make_number (charpos),
19631 Qdisplay, Qnil);
19632 result =
19633 (!NILP (prop)
19634 && display_prop_string_p (prop, glyph->object));
19635 /* If there's a `cursor' property on one of the
19636 string's characters, this row is a cursor row,
19637 even though this is not a display string. */
19638 if (!result)
19640 Lisp_Object s = glyph->object;
19642 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19644 ptrdiff_t gpos = glyph->charpos;
19646 if (!NILP (Fget_char_property (make_number (gpos),
19647 Qcursor, s)))
19649 result = true;
19650 break;
19654 break;
19658 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19660 /* If the row ends in middle of a real character,
19661 and the line is continued, we want the cursor here.
19662 That's because CHARPOS (ROW->end.pos) would equal
19663 PT if PT is before the character. */
19664 if (!row->ends_in_ellipsis_p)
19665 result = row->continued_p;
19666 else
19667 /* If the row ends in an ellipsis, then
19668 CHARPOS (ROW->end.pos) will equal point after the
19669 invisible text. We want that position to be displayed
19670 after the ellipsis. */
19671 result = false;
19673 /* If the row ends at ZV, display the cursor at the end of that
19674 row instead of at the start of the row below. */
19675 else
19676 result = row->ends_at_zv_p;
19679 return result;
19682 /* Value is true if glyph row ROW should be
19683 used to hold the cursor. */
19685 static bool
19686 cursor_row_p (struct glyph_row *row)
19688 return row_for_charpos_p (row, PT);
19693 /* Push the property PROP so that it will be rendered at the current
19694 position in IT. Return true if PROP was successfully pushed, false
19695 otherwise. Called from handle_line_prefix to handle the
19696 `line-prefix' and `wrap-prefix' properties. */
19698 static bool
19699 push_prefix_prop (struct it *it, Lisp_Object prop)
19701 struct text_pos pos =
19702 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19704 eassert (it->method == GET_FROM_BUFFER
19705 || it->method == GET_FROM_DISPLAY_VECTOR
19706 || it->method == GET_FROM_STRING);
19708 /* We need to save the current buffer/string position, so it will be
19709 restored by pop_it, because iterate_out_of_display_property
19710 depends on that being set correctly, but some situations leave
19711 it->position not yet set when this function is called. */
19712 push_it (it, &pos);
19714 if (STRINGP (prop))
19716 if (SCHARS (prop) == 0)
19718 pop_it (it);
19719 return false;
19722 it->string = prop;
19723 it->string_from_prefix_prop_p = true;
19724 it->multibyte_p = STRING_MULTIBYTE (it->string);
19725 it->current.overlay_string_index = -1;
19726 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19727 it->end_charpos = it->string_nchars = SCHARS (it->string);
19728 it->method = GET_FROM_STRING;
19729 it->stop_charpos = 0;
19730 it->prev_stop = 0;
19731 it->base_level_stop = 0;
19733 /* Force paragraph direction to be that of the parent
19734 buffer/string. */
19735 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19736 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19737 else
19738 it->paragraph_embedding = L2R;
19740 /* Set up the bidi iterator for this display string. */
19741 if (it->bidi_p)
19743 it->bidi_it.string.lstring = it->string;
19744 it->bidi_it.string.s = NULL;
19745 it->bidi_it.string.schars = it->end_charpos;
19746 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19747 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19748 it->bidi_it.string.unibyte = !it->multibyte_p;
19749 it->bidi_it.w = it->w;
19750 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19753 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19755 it->method = GET_FROM_STRETCH;
19756 it->object = prop;
19758 #ifdef HAVE_WINDOW_SYSTEM
19759 else if (IMAGEP (prop))
19761 it->what = IT_IMAGE;
19762 it->image_id = lookup_image (it->f, prop);
19763 it->method = GET_FROM_IMAGE;
19765 #endif /* HAVE_WINDOW_SYSTEM */
19766 else
19768 pop_it (it); /* bogus display property, give up */
19769 return false;
19772 return true;
19775 /* Return the character-property PROP at the current position in IT. */
19777 static Lisp_Object
19778 get_it_property (struct it *it, Lisp_Object prop)
19780 Lisp_Object position, object = it->object;
19782 if (STRINGP (object))
19783 position = make_number (IT_STRING_CHARPOS (*it));
19784 else if (BUFFERP (object))
19786 position = make_number (IT_CHARPOS (*it));
19787 object = it->window;
19789 else
19790 return Qnil;
19792 return Fget_char_property (position, prop, object);
19795 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19797 static void
19798 handle_line_prefix (struct it *it)
19800 Lisp_Object prefix;
19802 if (it->continuation_lines_width > 0)
19804 prefix = get_it_property (it, Qwrap_prefix);
19805 if (NILP (prefix))
19806 prefix = Vwrap_prefix;
19808 else
19810 prefix = get_it_property (it, Qline_prefix);
19811 if (NILP (prefix))
19812 prefix = Vline_prefix;
19814 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19816 /* If the prefix is wider than the window, and we try to wrap
19817 it, it would acquire its own wrap prefix, and so on till the
19818 iterator stack overflows. So, don't wrap the prefix. */
19819 it->line_wrap = TRUNCATE;
19820 it->avoid_cursor_p = true;
19826 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19827 only for R2L lines from display_line and display_string, when they
19828 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19829 the line/string needs to be continued on the next glyph row. */
19830 static void
19831 unproduce_glyphs (struct it *it, int n)
19833 struct glyph *glyph, *end;
19835 eassert (it->glyph_row);
19836 eassert (it->glyph_row->reversed_p);
19837 eassert (it->area == TEXT_AREA);
19838 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19840 if (n > it->glyph_row->used[TEXT_AREA])
19841 n = it->glyph_row->used[TEXT_AREA];
19842 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19843 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19844 for ( ; glyph < end; glyph++)
19845 glyph[-n] = *glyph;
19848 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19849 and ROW->maxpos. */
19850 static void
19851 find_row_edges (struct it *it, struct glyph_row *row,
19852 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19853 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19855 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19856 lines' rows is implemented for bidi-reordered rows. */
19858 /* ROW->minpos is the value of min_pos, the minimal buffer position
19859 we have in ROW, or ROW->start.pos if that is smaller. */
19860 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19861 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19862 else
19863 /* We didn't find buffer positions smaller than ROW->start, or
19864 didn't find _any_ valid buffer positions in any of the glyphs,
19865 so we must trust the iterator's computed positions. */
19866 row->minpos = row->start.pos;
19867 if (max_pos <= 0)
19869 max_pos = CHARPOS (it->current.pos);
19870 max_bpos = BYTEPOS (it->current.pos);
19873 /* Here are the various use-cases for ending the row, and the
19874 corresponding values for ROW->maxpos:
19876 Line ends in a newline from buffer eol_pos + 1
19877 Line is continued from buffer max_pos + 1
19878 Line is truncated on right it->current.pos
19879 Line ends in a newline from string max_pos + 1(*)
19880 (*) + 1 only when line ends in a forward scan
19881 Line is continued from string max_pos
19882 Line is continued from display vector max_pos
19883 Line is entirely from a string min_pos == max_pos
19884 Line is entirely from a display vector min_pos == max_pos
19885 Line that ends at ZV ZV
19887 If you discover other use-cases, please add them here as
19888 appropriate. */
19889 if (row->ends_at_zv_p)
19890 row->maxpos = it->current.pos;
19891 else if (row->used[TEXT_AREA])
19893 bool seen_this_string = false;
19894 struct glyph_row *r1 = row - 1;
19896 /* Did we see the same display string on the previous row? */
19897 if (STRINGP (it->object)
19898 /* this is not the first row */
19899 && row > it->w->desired_matrix->rows
19900 /* previous row is not the header line */
19901 && !r1->mode_line_p
19902 /* previous row also ends in a newline from a string */
19903 && r1->ends_in_newline_from_string_p)
19905 struct glyph *start, *end;
19907 /* Search for the last glyph of the previous row that came
19908 from buffer or string. Depending on whether the row is
19909 L2R or R2L, we need to process it front to back or the
19910 other way round. */
19911 if (!r1->reversed_p)
19913 start = r1->glyphs[TEXT_AREA];
19914 end = start + r1->used[TEXT_AREA];
19915 /* Glyphs inserted by redisplay have nil as their object. */
19916 while (end > start
19917 && NILP ((end - 1)->object)
19918 && (end - 1)->charpos <= 0)
19919 --end;
19920 if (end > start)
19922 if (EQ ((end - 1)->object, it->object))
19923 seen_this_string = true;
19925 else
19926 /* If all the glyphs of the previous row were inserted
19927 by redisplay, it means the previous row was
19928 produced from a single newline, which is only
19929 possible if that newline came from the same string
19930 as the one which produced this ROW. */
19931 seen_this_string = true;
19933 else
19935 end = r1->glyphs[TEXT_AREA] - 1;
19936 start = end + r1->used[TEXT_AREA];
19937 while (end < start
19938 && NILP ((end + 1)->object)
19939 && (end + 1)->charpos <= 0)
19940 ++end;
19941 if (end < start)
19943 if (EQ ((end + 1)->object, it->object))
19944 seen_this_string = true;
19946 else
19947 seen_this_string = true;
19950 /* Take note of each display string that covers a newline only
19951 once, the first time we see it. This is for when a display
19952 string includes more than one newline in it. */
19953 if (row->ends_in_newline_from_string_p && !seen_this_string)
19955 /* If we were scanning the buffer forward when we displayed
19956 the string, we want to account for at least one buffer
19957 position that belongs to this row (position covered by
19958 the display string), so that cursor positioning will
19959 consider this row as a candidate when point is at the end
19960 of the visual line represented by this row. This is not
19961 required when scanning back, because max_pos will already
19962 have a much larger value. */
19963 if (CHARPOS (row->end.pos) > max_pos)
19964 INC_BOTH (max_pos, max_bpos);
19965 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19967 else if (CHARPOS (it->eol_pos) > 0)
19968 SET_TEXT_POS (row->maxpos,
19969 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19970 else if (row->continued_p)
19972 /* If max_pos is different from IT's current position, it
19973 means IT->method does not belong to the display element
19974 at max_pos. However, it also means that the display
19975 element at max_pos was displayed in its entirety on this
19976 line, which is equivalent to saying that the next line
19977 starts at the next buffer position. */
19978 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19979 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19980 else
19982 INC_BOTH (max_pos, max_bpos);
19983 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19986 else if (row->truncated_on_right_p)
19987 /* display_line already called reseat_at_next_visible_line_start,
19988 which puts the iterator at the beginning of the next line, in
19989 the logical order. */
19990 row->maxpos = it->current.pos;
19991 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19992 /* A line that is entirely from a string/image/stretch... */
19993 row->maxpos = row->minpos;
19994 else
19995 emacs_abort ();
19997 else
19998 row->maxpos = it->current.pos;
20001 /* Construct the glyph row IT->glyph_row in the desired matrix of
20002 IT->w from text at the current position of IT. See dispextern.h
20003 for an overview of struct it. Value is true if
20004 IT->glyph_row displays text, as opposed to a line displaying ZV
20005 only. */
20007 static bool
20008 display_line (struct it *it)
20010 struct glyph_row *row = it->glyph_row;
20011 Lisp_Object overlay_arrow_string;
20012 struct it wrap_it;
20013 void *wrap_data = NULL;
20014 bool may_wrap = false;
20015 int wrap_x IF_LINT (= 0);
20016 int wrap_row_used = -1;
20017 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20018 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20019 int wrap_row_extra_line_spacing IF_LINT (= 0);
20020 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20021 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20022 int cvpos;
20023 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20024 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20025 bool pending_handle_line_prefix = false;
20027 /* We always start displaying at hpos zero even if hscrolled. */
20028 eassert (it->hpos == 0 && it->current_x == 0);
20030 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20031 >= it->w->desired_matrix->nrows)
20033 it->w->nrows_scale_factor++;
20034 it->f->fonts_changed = true;
20035 return false;
20038 /* Clear the result glyph row and enable it. */
20039 prepare_desired_row (it->w, row, false);
20041 row->y = it->current_y;
20042 row->start = it->start;
20043 row->continuation_lines_width = it->continuation_lines_width;
20044 row->displays_text_p = true;
20045 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20046 it->starts_in_middle_of_char_p = false;
20048 /* Arrange the overlays nicely for our purposes. Usually, we call
20049 display_line on only one line at a time, in which case this
20050 can't really hurt too much, or we call it on lines which appear
20051 one after another in the buffer, in which case all calls to
20052 recenter_overlay_lists but the first will be pretty cheap. */
20053 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20055 /* Move over display elements that are not visible because we are
20056 hscrolled. This may stop at an x-position < IT->first_visible_x
20057 if the first glyph is partially visible or if we hit a line end. */
20058 if (it->current_x < it->first_visible_x)
20060 enum move_it_result move_result;
20062 this_line_min_pos = row->start.pos;
20063 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20064 MOVE_TO_POS | MOVE_TO_X);
20065 /* If we are under a large hscroll, move_it_in_display_line_to
20066 could hit the end of the line without reaching
20067 it->first_visible_x. Pretend that we did reach it. This is
20068 especially important on a TTY, where we will call
20069 extend_face_to_end_of_line, which needs to know how many
20070 blank glyphs to produce. */
20071 if (it->current_x < it->first_visible_x
20072 && (move_result == MOVE_NEWLINE_OR_CR
20073 || move_result == MOVE_POS_MATCH_OR_ZV))
20074 it->current_x = it->first_visible_x;
20076 /* Record the smallest positions seen while we moved over
20077 display elements that are not visible. This is needed by
20078 redisplay_internal for optimizing the case where the cursor
20079 stays inside the same line. The rest of this function only
20080 considers positions that are actually displayed, so
20081 RECORD_MAX_MIN_POS will not otherwise record positions that
20082 are hscrolled to the left of the left edge of the window. */
20083 min_pos = CHARPOS (this_line_min_pos);
20084 min_bpos = BYTEPOS (this_line_min_pos);
20086 else if (it->area == TEXT_AREA)
20088 /* We only do this when not calling move_it_in_display_line_to
20089 above, because that function calls itself handle_line_prefix. */
20090 handle_line_prefix (it);
20092 else
20094 /* Line-prefix and wrap-prefix are always displayed in the text
20095 area. But if this is the first call to display_line after
20096 init_iterator, the iterator might have been set up to write
20097 into a marginal area, e.g. if the line begins with some
20098 display property that writes to the margins. So we need to
20099 wait with the call to handle_line_prefix until whatever
20100 writes to the margin has done its job. */
20101 pending_handle_line_prefix = true;
20104 /* Get the initial row height. This is either the height of the
20105 text hscrolled, if there is any, or zero. */
20106 row->ascent = it->max_ascent;
20107 row->height = it->max_ascent + it->max_descent;
20108 row->phys_ascent = it->max_phys_ascent;
20109 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20110 row->extra_line_spacing = it->max_extra_line_spacing;
20112 /* Utility macro to record max and min buffer positions seen until now. */
20113 #define RECORD_MAX_MIN_POS(IT) \
20114 do \
20116 bool composition_p \
20117 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20118 ptrdiff_t current_pos = \
20119 composition_p ? (IT)->cmp_it.charpos \
20120 : IT_CHARPOS (*(IT)); \
20121 ptrdiff_t current_bpos = \
20122 composition_p ? CHAR_TO_BYTE (current_pos) \
20123 : IT_BYTEPOS (*(IT)); \
20124 if (current_pos < min_pos) \
20126 min_pos = current_pos; \
20127 min_bpos = current_bpos; \
20129 if (IT_CHARPOS (*it) > max_pos) \
20131 max_pos = IT_CHARPOS (*it); \
20132 max_bpos = IT_BYTEPOS (*it); \
20135 while (false)
20137 /* Loop generating characters. The loop is left with IT on the next
20138 character to display. */
20139 while (true)
20141 int n_glyphs_before, hpos_before, x_before;
20142 int x, nglyphs;
20143 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20145 /* Retrieve the next thing to display. Value is false if end of
20146 buffer reached. */
20147 if (!get_next_display_element (it))
20149 /* Maybe add a space at the end of this line that is used to
20150 display the cursor there under X. Set the charpos of the
20151 first glyph of blank lines not corresponding to any text
20152 to -1. */
20153 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20154 row->exact_window_width_line_p = true;
20155 else if ((append_space_for_newline (it, true)
20156 && row->used[TEXT_AREA] == 1)
20157 || row->used[TEXT_AREA] == 0)
20159 row->glyphs[TEXT_AREA]->charpos = -1;
20160 row->displays_text_p = false;
20162 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20163 && (!MINI_WINDOW_P (it->w)
20164 || (minibuf_level && EQ (it->window, minibuf_window))))
20165 row->indicate_empty_line_p = true;
20168 it->continuation_lines_width = 0;
20169 row->ends_at_zv_p = true;
20170 /* A row that displays right-to-left text must always have
20171 its last face extended all the way to the end of line,
20172 even if this row ends in ZV, because we still write to
20173 the screen left to right. We also need to extend the
20174 last face if the default face is remapped to some
20175 different face, otherwise the functions that clear
20176 portions of the screen will clear with the default face's
20177 background color. */
20178 if (row->reversed_p
20179 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20180 extend_face_to_end_of_line (it);
20181 break;
20184 /* Now, get the metrics of what we want to display. This also
20185 generates glyphs in `row' (which is IT->glyph_row). */
20186 n_glyphs_before = row->used[TEXT_AREA];
20187 x = it->current_x;
20189 /* Remember the line height so far in case the next element doesn't
20190 fit on the line. */
20191 if (it->line_wrap != TRUNCATE)
20193 ascent = it->max_ascent;
20194 descent = it->max_descent;
20195 phys_ascent = it->max_phys_ascent;
20196 phys_descent = it->max_phys_descent;
20198 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20200 if (IT_DISPLAYING_WHITESPACE (it))
20201 may_wrap = true;
20202 else if (may_wrap)
20204 SAVE_IT (wrap_it, *it, wrap_data);
20205 wrap_x = x;
20206 wrap_row_used = row->used[TEXT_AREA];
20207 wrap_row_ascent = row->ascent;
20208 wrap_row_height = row->height;
20209 wrap_row_phys_ascent = row->phys_ascent;
20210 wrap_row_phys_height = row->phys_height;
20211 wrap_row_extra_line_spacing = row->extra_line_spacing;
20212 wrap_row_min_pos = min_pos;
20213 wrap_row_min_bpos = min_bpos;
20214 wrap_row_max_pos = max_pos;
20215 wrap_row_max_bpos = max_bpos;
20216 may_wrap = false;
20221 PRODUCE_GLYPHS (it);
20223 /* If this display element was in marginal areas, continue with
20224 the next one. */
20225 if (it->area != TEXT_AREA)
20227 row->ascent = max (row->ascent, it->max_ascent);
20228 row->height = max (row->height, it->max_ascent + it->max_descent);
20229 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20230 row->phys_height = max (row->phys_height,
20231 it->max_phys_ascent + it->max_phys_descent);
20232 row->extra_line_spacing = max (row->extra_line_spacing,
20233 it->max_extra_line_spacing);
20234 set_iterator_to_next (it, true);
20235 /* If we didn't handle the line/wrap prefix above, and the
20236 call to set_iterator_to_next just switched to TEXT_AREA,
20237 process the prefix now. */
20238 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20240 pending_handle_line_prefix = false;
20241 handle_line_prefix (it);
20243 continue;
20246 /* Does the display element fit on the line? If we truncate
20247 lines, we should draw past the right edge of the window. If
20248 we don't truncate, we want to stop so that we can display the
20249 continuation glyph before the right margin. If lines are
20250 continued, there are two possible strategies for characters
20251 resulting in more than 1 glyph (e.g. tabs): Display as many
20252 glyphs as possible in this line and leave the rest for the
20253 continuation line, or display the whole element in the next
20254 line. Original redisplay did the former, so we do it also. */
20255 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20256 hpos_before = it->hpos;
20257 x_before = x;
20259 if (/* Not a newline. */
20260 nglyphs > 0
20261 /* Glyphs produced fit entirely in the line. */
20262 && it->current_x < it->last_visible_x)
20264 it->hpos += nglyphs;
20265 row->ascent = max (row->ascent, it->max_ascent);
20266 row->height = max (row->height, it->max_ascent + it->max_descent);
20267 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20268 row->phys_height = max (row->phys_height,
20269 it->max_phys_ascent + it->max_phys_descent);
20270 row->extra_line_spacing = max (row->extra_line_spacing,
20271 it->max_extra_line_spacing);
20272 if (it->current_x - it->pixel_width < it->first_visible_x
20273 /* In R2L rows, we arrange in extend_face_to_end_of_line
20274 to add a right offset to the line, by a suitable
20275 change to the stretch glyph that is the leftmost
20276 glyph of the line. */
20277 && !row->reversed_p)
20278 row->x = x - it->first_visible_x;
20279 /* Record the maximum and minimum buffer positions seen so
20280 far in glyphs that will be displayed by this row. */
20281 if (it->bidi_p)
20282 RECORD_MAX_MIN_POS (it);
20284 else
20286 int i, new_x;
20287 struct glyph *glyph;
20289 for (i = 0; i < nglyphs; ++i, x = new_x)
20291 /* Identify the glyphs added by the last call to
20292 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20293 the previous glyphs. */
20294 if (!row->reversed_p)
20295 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20296 else
20297 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20298 new_x = x + glyph->pixel_width;
20300 if (/* Lines are continued. */
20301 it->line_wrap != TRUNCATE
20302 && (/* Glyph doesn't fit on the line. */
20303 new_x > it->last_visible_x
20304 /* Or it fits exactly on a window system frame. */
20305 || (new_x == it->last_visible_x
20306 && FRAME_WINDOW_P (it->f)
20307 && (row->reversed_p
20308 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20309 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20311 /* End of a continued line. */
20313 if (it->hpos == 0
20314 || (new_x == it->last_visible_x
20315 && FRAME_WINDOW_P (it->f)
20316 && (row->reversed_p
20317 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20318 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20320 /* Current glyph is the only one on the line or
20321 fits exactly on the line. We must continue
20322 the line because we can't draw the cursor
20323 after the glyph. */
20324 row->continued_p = true;
20325 it->current_x = new_x;
20326 it->continuation_lines_width += new_x;
20327 ++it->hpos;
20328 if (i == nglyphs - 1)
20330 /* If line-wrap is on, check if a previous
20331 wrap point was found. */
20332 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20333 && wrap_row_used > 0
20334 /* Even if there is a previous wrap
20335 point, continue the line here as
20336 usual, if (i) the previous character
20337 was a space or tab AND (ii) the
20338 current character is not. */
20339 && (!may_wrap
20340 || IT_DISPLAYING_WHITESPACE (it)))
20341 goto back_to_wrap;
20343 /* Record the maximum and minimum buffer
20344 positions seen so far in glyphs that will be
20345 displayed by this row. */
20346 if (it->bidi_p)
20347 RECORD_MAX_MIN_POS (it);
20348 set_iterator_to_next (it, true);
20349 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20351 if (!get_next_display_element (it))
20353 row->exact_window_width_line_p = true;
20354 it->continuation_lines_width = 0;
20355 row->continued_p = false;
20356 row->ends_at_zv_p = true;
20358 else if (ITERATOR_AT_END_OF_LINE_P (it))
20360 row->continued_p = false;
20361 row->exact_window_width_line_p = true;
20363 /* If line-wrap is on, check if a
20364 previous wrap point was found. */
20365 else if (wrap_row_used > 0
20366 /* Even if there is a previous wrap
20367 point, continue the line here as
20368 usual, if (i) the previous character
20369 was a space or tab AND (ii) the
20370 current character is not. */
20371 && (!may_wrap
20372 || IT_DISPLAYING_WHITESPACE (it)))
20373 goto back_to_wrap;
20377 else if (it->bidi_p)
20378 RECORD_MAX_MIN_POS (it);
20379 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20380 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20381 extend_face_to_end_of_line (it);
20383 else if (CHAR_GLYPH_PADDING_P (*glyph)
20384 && !FRAME_WINDOW_P (it->f))
20386 /* A padding glyph that doesn't fit on this line.
20387 This means the whole character doesn't fit
20388 on the line. */
20389 if (row->reversed_p)
20390 unproduce_glyphs (it, row->used[TEXT_AREA]
20391 - n_glyphs_before);
20392 row->used[TEXT_AREA] = n_glyphs_before;
20394 /* Fill the rest of the row with continuation
20395 glyphs like in 20.x. */
20396 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20397 < row->glyphs[1 + TEXT_AREA])
20398 produce_special_glyphs (it, IT_CONTINUATION);
20400 row->continued_p = true;
20401 it->current_x = x_before;
20402 it->continuation_lines_width += x_before;
20404 /* Restore the height to what it was before the
20405 element not fitting on the line. */
20406 it->max_ascent = ascent;
20407 it->max_descent = descent;
20408 it->max_phys_ascent = phys_ascent;
20409 it->max_phys_descent = phys_descent;
20410 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20411 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20412 extend_face_to_end_of_line (it);
20414 else if (wrap_row_used > 0)
20416 back_to_wrap:
20417 if (row->reversed_p)
20418 unproduce_glyphs (it,
20419 row->used[TEXT_AREA] - wrap_row_used);
20420 RESTORE_IT (it, &wrap_it, wrap_data);
20421 it->continuation_lines_width += wrap_x;
20422 row->used[TEXT_AREA] = wrap_row_used;
20423 row->ascent = wrap_row_ascent;
20424 row->height = wrap_row_height;
20425 row->phys_ascent = wrap_row_phys_ascent;
20426 row->phys_height = wrap_row_phys_height;
20427 row->extra_line_spacing = wrap_row_extra_line_spacing;
20428 min_pos = wrap_row_min_pos;
20429 min_bpos = wrap_row_min_bpos;
20430 max_pos = wrap_row_max_pos;
20431 max_bpos = wrap_row_max_bpos;
20432 row->continued_p = true;
20433 row->ends_at_zv_p = false;
20434 row->exact_window_width_line_p = false;
20435 it->continuation_lines_width += x;
20437 /* Make sure that a non-default face is extended
20438 up to the right margin of the window. */
20439 extend_face_to_end_of_line (it);
20441 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20443 /* A TAB that extends past the right edge of the
20444 window. This produces a single glyph on
20445 window system frames. We leave the glyph in
20446 this row and let it fill the row, but don't
20447 consume the TAB. */
20448 if ((row->reversed_p
20449 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20450 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20451 produce_special_glyphs (it, IT_CONTINUATION);
20452 it->continuation_lines_width += it->last_visible_x;
20453 row->ends_in_middle_of_char_p = true;
20454 row->continued_p = true;
20455 glyph->pixel_width = it->last_visible_x - x;
20456 it->starts_in_middle_of_char_p = true;
20457 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20458 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20459 extend_face_to_end_of_line (it);
20461 else
20463 /* Something other than a TAB that draws past
20464 the right edge of the window. Restore
20465 positions to values before the element. */
20466 if (row->reversed_p)
20467 unproduce_glyphs (it, row->used[TEXT_AREA]
20468 - (n_glyphs_before + i));
20469 row->used[TEXT_AREA] = n_glyphs_before + i;
20471 /* Display continuation glyphs. */
20472 it->current_x = x_before;
20473 it->continuation_lines_width += x;
20474 if (!FRAME_WINDOW_P (it->f)
20475 || (row->reversed_p
20476 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20477 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20478 produce_special_glyphs (it, IT_CONTINUATION);
20479 row->continued_p = true;
20481 extend_face_to_end_of_line (it);
20483 if (nglyphs > 1 && i > 0)
20485 row->ends_in_middle_of_char_p = true;
20486 it->starts_in_middle_of_char_p = true;
20489 /* Restore the height to what it was before the
20490 element not fitting on the line. */
20491 it->max_ascent = ascent;
20492 it->max_descent = descent;
20493 it->max_phys_ascent = phys_ascent;
20494 it->max_phys_descent = phys_descent;
20497 break;
20499 else if (new_x > it->first_visible_x)
20501 /* Increment number of glyphs actually displayed. */
20502 ++it->hpos;
20504 /* Record the maximum and minimum buffer positions
20505 seen so far in glyphs that will be displayed by
20506 this row. */
20507 if (it->bidi_p)
20508 RECORD_MAX_MIN_POS (it);
20510 if (x < it->first_visible_x && !row->reversed_p)
20511 /* Glyph is partially visible, i.e. row starts at
20512 negative X position. Don't do that in R2L
20513 rows, where we arrange to add a right offset to
20514 the line in extend_face_to_end_of_line, by a
20515 suitable change to the stretch glyph that is
20516 the leftmost glyph of the line. */
20517 row->x = x - it->first_visible_x;
20518 /* When the last glyph of an R2L row only fits
20519 partially on the line, we need to set row->x to a
20520 negative offset, so that the leftmost glyph is
20521 the one that is partially visible. But if we are
20522 going to produce the truncation glyph, this will
20523 be taken care of in produce_special_glyphs. */
20524 if (row->reversed_p
20525 && new_x > it->last_visible_x
20526 && !(it->line_wrap == TRUNCATE
20527 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20529 eassert (FRAME_WINDOW_P (it->f));
20530 row->x = it->last_visible_x - new_x;
20533 else
20535 /* Glyph is completely off the left margin of the
20536 window. This should not happen because of the
20537 move_it_in_display_line at the start of this
20538 function, unless the text display area of the
20539 window is empty. */
20540 eassert (it->first_visible_x <= it->last_visible_x);
20543 /* Even if this display element produced no glyphs at all,
20544 we want to record its position. */
20545 if (it->bidi_p && nglyphs == 0)
20546 RECORD_MAX_MIN_POS (it);
20548 row->ascent = max (row->ascent, it->max_ascent);
20549 row->height = max (row->height, it->max_ascent + it->max_descent);
20550 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20551 row->phys_height = max (row->phys_height,
20552 it->max_phys_ascent + it->max_phys_descent);
20553 row->extra_line_spacing = max (row->extra_line_spacing,
20554 it->max_extra_line_spacing);
20556 /* End of this display line if row is continued. */
20557 if (row->continued_p || row->ends_at_zv_p)
20558 break;
20561 at_end_of_line:
20562 /* Is this a line end? If yes, we're also done, after making
20563 sure that a non-default face is extended up to the right
20564 margin of the window. */
20565 if (ITERATOR_AT_END_OF_LINE_P (it))
20567 int used_before = row->used[TEXT_AREA];
20569 row->ends_in_newline_from_string_p = STRINGP (it->object);
20571 /* Add a space at the end of the line that is used to
20572 display the cursor there. */
20573 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20574 append_space_for_newline (it, false);
20576 /* Extend the face to the end of the line. */
20577 extend_face_to_end_of_line (it);
20579 /* Make sure we have the position. */
20580 if (used_before == 0)
20581 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20583 /* Record the position of the newline, for use in
20584 find_row_edges. */
20585 it->eol_pos = it->current.pos;
20587 /* Consume the line end. This skips over invisible lines. */
20588 set_iterator_to_next (it, true);
20589 it->continuation_lines_width = 0;
20590 break;
20593 /* Proceed with next display element. Note that this skips
20594 over lines invisible because of selective display. */
20595 set_iterator_to_next (it, true);
20597 /* If we truncate lines, we are done when the last displayed
20598 glyphs reach past the right margin of the window. */
20599 if (it->line_wrap == TRUNCATE
20600 && ((FRAME_WINDOW_P (it->f)
20601 /* Images are preprocessed in produce_image_glyph such
20602 that they are cropped at the right edge of the
20603 window, so an image glyph will always end exactly at
20604 last_visible_x, even if there's no right fringe. */
20605 && ((row->reversed_p
20606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20608 || it->what == IT_IMAGE))
20609 ? (it->current_x >= it->last_visible_x)
20610 : (it->current_x > it->last_visible_x)))
20612 /* Maybe add truncation glyphs. */
20613 if (!FRAME_WINDOW_P (it->f)
20614 || (row->reversed_p
20615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20618 int i, n;
20620 if (!row->reversed_p)
20622 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20623 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20624 break;
20626 else
20628 for (i = 0; i < row->used[TEXT_AREA]; i++)
20629 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20630 break;
20631 /* Remove any padding glyphs at the front of ROW, to
20632 make room for the truncation glyphs we will be
20633 adding below. The loop below always inserts at
20634 least one truncation glyph, so also remove the
20635 last glyph added to ROW. */
20636 unproduce_glyphs (it, i + 1);
20637 /* Adjust i for the loop below. */
20638 i = row->used[TEXT_AREA] - (i + 1);
20641 /* produce_special_glyphs overwrites the last glyph, so
20642 we don't want that if we want to keep that last
20643 glyph, which means it's an image. */
20644 if (it->current_x > it->last_visible_x)
20646 it->current_x = x_before;
20647 if (!FRAME_WINDOW_P (it->f))
20649 for (n = row->used[TEXT_AREA]; i < n; ++i)
20651 row->used[TEXT_AREA] = i;
20652 produce_special_glyphs (it, IT_TRUNCATION);
20655 else
20657 row->used[TEXT_AREA] = i;
20658 produce_special_glyphs (it, IT_TRUNCATION);
20660 it->hpos = hpos_before;
20663 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20665 /* Don't truncate if we can overflow newline into fringe. */
20666 if (!get_next_display_element (it))
20668 it->continuation_lines_width = 0;
20669 row->ends_at_zv_p = true;
20670 row->exact_window_width_line_p = true;
20671 break;
20673 if (ITERATOR_AT_END_OF_LINE_P (it))
20675 row->exact_window_width_line_p = true;
20676 goto at_end_of_line;
20678 it->current_x = x_before;
20679 it->hpos = hpos_before;
20682 row->truncated_on_right_p = true;
20683 it->continuation_lines_width = 0;
20684 reseat_at_next_visible_line_start (it, false);
20685 /* We insist below that IT's position be at ZV because in
20686 bidi-reordered lines the character at visible line start
20687 might not be the character that follows the newline in
20688 the logical order. */
20689 if (IT_BYTEPOS (*it) > BEG_BYTE)
20690 row->ends_at_zv_p =
20691 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20692 else
20693 row->ends_at_zv_p = false;
20694 break;
20698 if (wrap_data)
20699 bidi_unshelve_cache (wrap_data, true);
20701 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20702 at the left window margin. */
20703 if (it->first_visible_x
20704 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20706 if (!FRAME_WINDOW_P (it->f)
20707 || (((row->reversed_p
20708 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20709 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20710 /* Don't let insert_left_trunc_glyphs overwrite the
20711 first glyph of the row if it is an image. */
20712 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20713 insert_left_trunc_glyphs (it);
20714 row->truncated_on_left_p = true;
20717 /* Remember the position at which this line ends.
20719 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20720 cannot be before the call to find_row_edges below, since that is
20721 where these positions are determined. */
20722 row->end = it->current;
20723 if (!it->bidi_p)
20725 row->minpos = row->start.pos;
20726 row->maxpos = row->end.pos;
20728 else
20730 /* ROW->minpos and ROW->maxpos must be the smallest and
20731 `1 + the largest' buffer positions in ROW. But if ROW was
20732 bidi-reordered, these two positions can be anywhere in the
20733 row, so we must determine them now. */
20734 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20737 /* If the start of this line is the overlay arrow-position, then
20738 mark this glyph row as the one containing the overlay arrow.
20739 This is clearly a mess with variable size fonts. It would be
20740 better to let it be displayed like cursors under X. */
20741 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20742 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20743 !NILP (overlay_arrow_string)))
20745 /* Overlay arrow in window redisplay is a fringe bitmap. */
20746 if (STRINGP (overlay_arrow_string))
20748 struct glyph_row *arrow_row
20749 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20750 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20751 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20752 struct glyph *p = row->glyphs[TEXT_AREA];
20753 struct glyph *p2, *end;
20755 /* Copy the arrow glyphs. */
20756 while (glyph < arrow_end)
20757 *p++ = *glyph++;
20759 /* Throw away padding glyphs. */
20760 p2 = p;
20761 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20762 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20763 ++p2;
20764 if (p2 > p)
20766 while (p2 < end)
20767 *p++ = *p2++;
20768 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20771 else
20773 eassert (INTEGERP (overlay_arrow_string));
20774 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20776 overlay_arrow_seen = true;
20779 /* Highlight trailing whitespace. */
20780 if (!NILP (Vshow_trailing_whitespace))
20781 highlight_trailing_whitespace (it->f, it->glyph_row);
20783 /* Compute pixel dimensions of this line. */
20784 compute_line_metrics (it);
20786 /* Implementation note: No changes in the glyphs of ROW or in their
20787 faces can be done past this point, because compute_line_metrics
20788 computes ROW's hash value and stores it within the glyph_row
20789 structure. */
20791 /* Record whether this row ends inside an ellipsis. */
20792 row->ends_in_ellipsis_p
20793 = (it->method == GET_FROM_DISPLAY_VECTOR
20794 && it->ellipsis_p);
20796 /* Save fringe bitmaps in this row. */
20797 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20798 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20799 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20800 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20802 it->left_user_fringe_bitmap = 0;
20803 it->left_user_fringe_face_id = 0;
20804 it->right_user_fringe_bitmap = 0;
20805 it->right_user_fringe_face_id = 0;
20807 /* Maybe set the cursor. */
20808 cvpos = it->w->cursor.vpos;
20809 if ((cvpos < 0
20810 /* In bidi-reordered rows, keep checking for proper cursor
20811 position even if one has been found already, because buffer
20812 positions in such rows change non-linearly with ROW->VPOS,
20813 when a line is continued. One exception: when we are at ZV,
20814 display cursor on the first suitable glyph row, since all
20815 the empty rows after that also have their position set to ZV. */
20816 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20817 lines' rows is implemented for bidi-reordered rows. */
20818 || (it->bidi_p
20819 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20820 && PT >= MATRIX_ROW_START_CHARPOS (row)
20821 && PT <= MATRIX_ROW_END_CHARPOS (row)
20822 && cursor_row_p (row))
20823 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20825 /* Prepare for the next line. This line starts horizontally at (X
20826 HPOS) = (0 0). Vertical positions are incremented. As a
20827 convenience for the caller, IT->glyph_row is set to the next
20828 row to be used. */
20829 it->current_x = it->hpos = 0;
20830 it->current_y += row->height;
20831 SET_TEXT_POS (it->eol_pos, 0, 0);
20832 ++it->vpos;
20833 ++it->glyph_row;
20834 /* The next row should by default use the same value of the
20835 reversed_p flag as this one. set_iterator_to_next decides when
20836 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20837 the flag accordingly. */
20838 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20839 it->glyph_row->reversed_p = row->reversed_p;
20840 it->start = row->end;
20841 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20843 #undef RECORD_MAX_MIN_POS
20846 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20847 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20848 doc: /* Return paragraph direction at point in BUFFER.
20849 Value is either `left-to-right' or `right-to-left'.
20850 If BUFFER is omitted or nil, it defaults to the current buffer.
20852 Paragraph direction determines how the text in the paragraph is displayed.
20853 In left-to-right paragraphs, text begins at the left margin of the window
20854 and the reading direction is generally left to right. In right-to-left
20855 paragraphs, text begins at the right margin and is read from right to left.
20857 See also `bidi-paragraph-direction'. */)
20858 (Lisp_Object buffer)
20860 struct buffer *buf = current_buffer;
20861 struct buffer *old = buf;
20863 if (! NILP (buffer))
20865 CHECK_BUFFER (buffer);
20866 buf = XBUFFER (buffer);
20869 if (NILP (BVAR (buf, bidi_display_reordering))
20870 || NILP (BVAR (buf, enable_multibyte_characters))
20871 /* When we are loading loadup.el, the character property tables
20872 needed for bidi iteration are not yet available. */
20873 || !NILP (Vpurify_flag))
20874 return Qleft_to_right;
20875 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20876 return BVAR (buf, bidi_paragraph_direction);
20877 else
20879 /* Determine the direction from buffer text. We could try to
20880 use current_matrix if it is up to date, but this seems fast
20881 enough as it is. */
20882 struct bidi_it itb;
20883 ptrdiff_t pos = BUF_PT (buf);
20884 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20885 int c;
20886 void *itb_data = bidi_shelve_cache ();
20888 set_buffer_temp (buf);
20889 /* bidi_paragraph_init finds the base direction of the paragraph
20890 by searching forward from paragraph start. We need the base
20891 direction of the current or _previous_ paragraph, so we need
20892 to make sure we are within that paragraph. To that end, find
20893 the previous non-empty line. */
20894 if (pos >= ZV && pos > BEGV)
20895 DEC_BOTH (pos, bytepos);
20896 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20897 if (fast_looking_at (trailing_white_space,
20898 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20900 while ((c = FETCH_BYTE (bytepos)) == '\n'
20901 || c == ' ' || c == '\t' || c == '\f')
20903 if (bytepos <= BEGV_BYTE)
20904 break;
20905 bytepos--;
20906 pos--;
20908 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20909 bytepos--;
20911 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20912 itb.paragraph_dir = NEUTRAL_DIR;
20913 itb.string.s = NULL;
20914 itb.string.lstring = Qnil;
20915 itb.string.bufpos = 0;
20916 itb.string.from_disp_str = false;
20917 itb.string.unibyte = false;
20918 /* We have no window to use here for ignoring window-specific
20919 overlays. Using NULL for window pointer will cause
20920 compute_display_string_pos to use the current buffer. */
20921 itb.w = NULL;
20922 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
20923 bidi_unshelve_cache (itb_data, false);
20924 set_buffer_temp (old);
20925 switch (itb.paragraph_dir)
20927 case L2R:
20928 return Qleft_to_right;
20929 break;
20930 case R2L:
20931 return Qright_to_left;
20932 break;
20933 default:
20934 emacs_abort ();
20939 DEFUN ("bidi-find-overridden-directionality",
20940 Fbidi_find_overridden_directionality,
20941 Sbidi_find_overridden_directionality, 2, 3, 0,
20942 doc: /* Return position between FROM and TO where directionality was overridden.
20944 This function returns the first character position in the specified
20945 region of OBJECT where there is a character whose `bidi-class' property
20946 is `L', but which was forced to display as `R' by a directional
20947 override, and likewise with characters whose `bidi-class' is `R'
20948 or `AL' that were forced to display as `L'.
20950 If no such character is found, the function returns nil.
20952 OBJECT is a Lisp string or buffer to search for overridden
20953 directionality, and defaults to the current buffer if nil or omitted.
20954 OBJECT can also be a window, in which case the function will search
20955 the buffer displayed in that window. Passing the window instead of
20956 a buffer is preferable when the buffer is displayed in some window,
20957 because this function will then be able to correctly account for
20958 window-specific overlays, which can affect the results.
20960 Strong directional characters `L', `R', and `AL' can have their
20961 intrinsic directionality overridden by directional override
20962 control characters RLO \(u+202e) and LRO \(u+202d). See the
20963 function `get-char-code-property' for a way to inquire about
20964 the `bidi-class' property of a character. */)
20965 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20967 struct buffer *buf = current_buffer;
20968 struct buffer *old = buf;
20969 struct window *w = NULL;
20970 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20971 struct bidi_it itb;
20972 ptrdiff_t from_pos, to_pos, from_bpos;
20973 void *itb_data;
20975 if (!NILP (object))
20977 if (BUFFERP (object))
20978 buf = XBUFFER (object);
20979 else if (WINDOWP (object))
20981 w = decode_live_window (object);
20982 buf = XBUFFER (w->contents);
20983 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20985 else
20986 CHECK_STRING (object);
20989 if (STRINGP (object))
20991 /* Characters in unibyte strings are always treated by bidi.c as
20992 strong LTR. */
20993 if (!STRING_MULTIBYTE (object)
20994 /* When we are loading loadup.el, the character property
20995 tables needed for bidi iteration are not yet
20996 available. */
20997 || !NILP (Vpurify_flag))
20998 return Qnil;
21000 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21001 if (from_pos >= SCHARS (object))
21002 return Qnil;
21004 /* Set up the bidi iterator. */
21005 itb_data = bidi_shelve_cache ();
21006 itb.paragraph_dir = NEUTRAL_DIR;
21007 itb.string.lstring = object;
21008 itb.string.s = NULL;
21009 itb.string.schars = SCHARS (object);
21010 itb.string.bufpos = 0;
21011 itb.string.from_disp_str = false;
21012 itb.string.unibyte = false;
21013 itb.w = w;
21014 bidi_init_it (0, 0, frame_window_p, &itb);
21016 else
21018 /* Nothing this fancy can happen in unibyte buffers, or in a
21019 buffer that disabled reordering, or if FROM is at EOB. */
21020 if (NILP (BVAR (buf, bidi_display_reordering))
21021 || NILP (BVAR (buf, enable_multibyte_characters))
21022 /* When we are loading loadup.el, the character property
21023 tables needed for bidi iteration are not yet
21024 available. */
21025 || !NILP (Vpurify_flag))
21026 return Qnil;
21028 set_buffer_temp (buf);
21029 validate_region (&from, &to);
21030 from_pos = XINT (from);
21031 to_pos = XINT (to);
21032 if (from_pos >= ZV)
21033 return Qnil;
21035 /* Set up the bidi iterator. */
21036 itb_data = bidi_shelve_cache ();
21037 from_bpos = CHAR_TO_BYTE (from_pos);
21038 if (from_pos == BEGV)
21040 itb.charpos = BEGV;
21041 itb.bytepos = BEGV_BYTE;
21043 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21045 itb.charpos = from_pos;
21046 itb.bytepos = from_bpos;
21048 else
21049 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21050 -1, &itb.bytepos);
21051 itb.paragraph_dir = NEUTRAL_DIR;
21052 itb.string.s = NULL;
21053 itb.string.lstring = Qnil;
21054 itb.string.bufpos = 0;
21055 itb.string.from_disp_str = false;
21056 itb.string.unibyte = false;
21057 itb.w = w;
21058 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21061 ptrdiff_t found;
21062 do {
21063 /* For the purposes of this function, the actual base direction of
21064 the paragraph doesn't matter, so just set it to L2R. */
21065 bidi_paragraph_init (L2R, &itb, false);
21066 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21068 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21070 bidi_unshelve_cache (itb_data, false);
21071 set_buffer_temp (old);
21073 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21076 DEFUN ("move-point-visually", Fmove_point_visually,
21077 Smove_point_visually, 1, 1, 0,
21078 doc: /* Move point in the visual order in the specified DIRECTION.
21079 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21080 left.
21082 Value is the new character position of point. */)
21083 (Lisp_Object direction)
21085 struct window *w = XWINDOW (selected_window);
21086 struct buffer *b = XBUFFER (w->contents);
21087 struct glyph_row *row;
21088 int dir;
21089 Lisp_Object paragraph_dir;
21091 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21092 (!(ROW)->continued_p \
21093 && NILP ((GLYPH)->object) \
21094 && (GLYPH)->type == CHAR_GLYPH \
21095 && (GLYPH)->u.ch == ' ' \
21096 && (GLYPH)->charpos >= 0 \
21097 && !(GLYPH)->avoid_cursor_p)
21099 CHECK_NUMBER (direction);
21100 dir = XINT (direction);
21101 if (dir > 0)
21102 dir = 1;
21103 else
21104 dir = -1;
21106 /* If current matrix is up-to-date, we can use the information
21107 recorded in the glyphs, at least as long as the goal is on the
21108 screen. */
21109 if (w->window_end_valid
21110 && !windows_or_buffers_changed
21111 && b
21112 && !b->clip_changed
21113 && !b->prevent_redisplay_optimizations_p
21114 && !window_outdated (w)
21115 /* We rely below on the cursor coordinates to be up to date, but
21116 we cannot trust them if some command moved point since the
21117 last complete redisplay. */
21118 && w->last_point == BUF_PT (b)
21119 && w->cursor.vpos >= 0
21120 && w->cursor.vpos < w->current_matrix->nrows
21121 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21123 struct glyph *g = row->glyphs[TEXT_AREA];
21124 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21125 struct glyph *gpt = g + w->cursor.hpos;
21127 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21129 if (BUFFERP (g->object) && g->charpos != PT)
21131 SET_PT (g->charpos);
21132 w->cursor.vpos = -1;
21133 return make_number (PT);
21135 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21137 ptrdiff_t new_pos;
21139 if (BUFFERP (gpt->object))
21141 new_pos = PT;
21142 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21143 new_pos += (row->reversed_p ? -dir : dir);
21144 else
21145 new_pos -= (row->reversed_p ? -dir : dir);
21147 else if (BUFFERP (g->object))
21148 new_pos = g->charpos;
21149 else
21150 break;
21151 SET_PT (new_pos);
21152 w->cursor.vpos = -1;
21153 return make_number (PT);
21155 else if (ROW_GLYPH_NEWLINE_P (row, g))
21157 /* Glyphs inserted at the end of a non-empty line for
21158 positioning the cursor have zero charpos, so we must
21159 deduce the value of point by other means. */
21160 if (g->charpos > 0)
21161 SET_PT (g->charpos);
21162 else if (row->ends_at_zv_p && PT != ZV)
21163 SET_PT (ZV);
21164 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21165 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21166 else
21167 break;
21168 w->cursor.vpos = -1;
21169 return make_number (PT);
21172 if (g == e || NILP (g->object))
21174 if (row->truncated_on_left_p || row->truncated_on_right_p)
21175 goto simulate_display;
21176 if (!row->reversed_p)
21177 row += dir;
21178 else
21179 row -= dir;
21180 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21181 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21182 goto simulate_display;
21184 if (dir > 0)
21186 if (row->reversed_p && !row->continued_p)
21188 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21189 w->cursor.vpos = -1;
21190 return make_number (PT);
21192 g = row->glyphs[TEXT_AREA];
21193 e = g + row->used[TEXT_AREA];
21194 for ( ; g < e; g++)
21196 if (BUFFERP (g->object)
21197 /* Empty lines have only one glyph, which stands
21198 for the newline, and whose charpos is the
21199 buffer position of the newline. */
21200 || ROW_GLYPH_NEWLINE_P (row, g)
21201 /* When the buffer ends in a newline, the line at
21202 EOB also has one glyph, but its charpos is -1. */
21203 || (row->ends_at_zv_p
21204 && !row->reversed_p
21205 && NILP (g->object)
21206 && g->type == CHAR_GLYPH
21207 && g->u.ch == ' '))
21209 if (g->charpos > 0)
21210 SET_PT (g->charpos);
21211 else if (!row->reversed_p
21212 && row->ends_at_zv_p
21213 && PT != ZV)
21214 SET_PT (ZV);
21215 else
21216 continue;
21217 w->cursor.vpos = -1;
21218 return make_number (PT);
21222 else
21224 if (!row->reversed_p && !row->continued_p)
21226 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21227 w->cursor.vpos = -1;
21228 return make_number (PT);
21230 e = row->glyphs[TEXT_AREA];
21231 g = e + row->used[TEXT_AREA] - 1;
21232 for ( ; g >= e; g--)
21234 if (BUFFERP (g->object)
21235 || (ROW_GLYPH_NEWLINE_P (row, g)
21236 && g->charpos > 0)
21237 /* Empty R2L lines on GUI frames have the buffer
21238 position of the newline stored in the stretch
21239 glyph. */
21240 || g->type == STRETCH_GLYPH
21241 || (row->ends_at_zv_p
21242 && row->reversed_p
21243 && NILP (g->object)
21244 && g->type == CHAR_GLYPH
21245 && g->u.ch == ' '))
21247 if (g->charpos > 0)
21248 SET_PT (g->charpos);
21249 else if (row->reversed_p
21250 && row->ends_at_zv_p
21251 && PT != ZV)
21252 SET_PT (ZV);
21253 else
21254 continue;
21255 w->cursor.vpos = -1;
21256 return make_number (PT);
21263 simulate_display:
21265 /* If we wind up here, we failed to move by using the glyphs, so we
21266 need to simulate display instead. */
21268 if (b)
21269 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21270 else
21271 paragraph_dir = Qleft_to_right;
21272 if (EQ (paragraph_dir, Qright_to_left))
21273 dir = -dir;
21274 if (PT <= BEGV && dir < 0)
21275 xsignal0 (Qbeginning_of_buffer);
21276 else if (PT >= ZV && dir > 0)
21277 xsignal0 (Qend_of_buffer);
21278 else
21280 struct text_pos pt;
21281 struct it it;
21282 int pt_x, target_x, pixel_width, pt_vpos;
21283 bool at_eol_p;
21284 bool overshoot_expected = false;
21285 bool target_is_eol_p = false;
21287 /* Setup the arena. */
21288 SET_TEXT_POS (pt, PT, PT_BYTE);
21289 start_display (&it, w, pt);
21291 if (it.cmp_it.id < 0
21292 && it.method == GET_FROM_STRING
21293 && it.area == TEXT_AREA
21294 && it.string_from_display_prop_p
21295 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21296 overshoot_expected = true;
21298 /* Find the X coordinate of point. We start from the beginning
21299 of this or previous line to make sure we are before point in
21300 the logical order (since the move_it_* functions can only
21301 move forward). */
21302 reseat:
21303 reseat_at_previous_visible_line_start (&it);
21304 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21305 if (IT_CHARPOS (it) != PT)
21307 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21308 -1, -1, -1, MOVE_TO_POS);
21309 /* If we missed point because the character there is
21310 displayed out of a display vector that has more than one
21311 glyph, retry expecting overshoot. */
21312 if (it.method == GET_FROM_DISPLAY_VECTOR
21313 && it.current.dpvec_index > 0
21314 && !overshoot_expected)
21316 overshoot_expected = true;
21317 goto reseat;
21319 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21320 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21322 pt_x = it.current_x;
21323 pt_vpos = it.vpos;
21324 if (dir > 0 || overshoot_expected)
21326 struct glyph_row *row = it.glyph_row;
21328 /* When point is at beginning of line, we don't have
21329 information about the glyph there loaded into struct
21330 it. Calling get_next_display_element fixes that. */
21331 if (pt_x == 0)
21332 get_next_display_element (&it);
21333 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21334 it.glyph_row = NULL;
21335 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21336 it.glyph_row = row;
21337 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21338 it, lest it will become out of sync with it's buffer
21339 position. */
21340 it.current_x = pt_x;
21342 else
21343 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21344 pixel_width = it.pixel_width;
21345 if (overshoot_expected && at_eol_p)
21346 pixel_width = 0;
21347 else if (pixel_width <= 0)
21348 pixel_width = 1;
21350 /* If there's a display string (or something similar) at point,
21351 we are actually at the glyph to the left of point, so we need
21352 to correct the X coordinate. */
21353 if (overshoot_expected)
21355 if (it.bidi_p)
21356 pt_x += pixel_width * it.bidi_it.scan_dir;
21357 else
21358 pt_x += pixel_width;
21361 /* Compute target X coordinate, either to the left or to the
21362 right of point. On TTY frames, all characters have the same
21363 pixel width of 1, so we can use that. On GUI frames we don't
21364 have an easy way of getting at the pixel width of the
21365 character to the left of point, so we use a different method
21366 of getting to that place. */
21367 if (dir > 0)
21368 target_x = pt_x + pixel_width;
21369 else
21370 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21372 /* Target X coordinate could be one line above or below the line
21373 of point, in which case we need to adjust the target X
21374 coordinate. Also, if moving to the left, we need to begin at
21375 the left edge of the point's screen line. */
21376 if (dir < 0)
21378 if (pt_x > 0)
21380 start_display (&it, w, pt);
21381 reseat_at_previous_visible_line_start (&it);
21382 it.current_x = it.current_y = it.hpos = 0;
21383 if (pt_vpos != 0)
21384 move_it_by_lines (&it, pt_vpos);
21386 else
21388 move_it_by_lines (&it, -1);
21389 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21390 target_is_eol_p = true;
21391 /* Under word-wrap, we don't know the x coordinate of
21392 the last character displayed on the previous line,
21393 which immediately precedes the wrap point. To find
21394 out its x coordinate, we try moving to the right
21395 margin of the window, which will stop at the wrap
21396 point, and then reset target_x to point at the
21397 character that precedes the wrap point. This is not
21398 needed on GUI frames, because (see below) there we
21399 move from the left margin one grapheme cluster at a
21400 time, and stop when we hit the wrap point. */
21401 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21403 void *it_data = NULL;
21404 struct it it2;
21406 SAVE_IT (it2, it, it_data);
21407 move_it_in_display_line_to (&it, ZV, target_x,
21408 MOVE_TO_POS | MOVE_TO_X);
21409 /* If we arrived at target_x, that _is_ the last
21410 character on the previous line. */
21411 if (it.current_x != target_x)
21412 target_x = it.current_x - 1;
21413 RESTORE_IT (&it, &it2, it_data);
21417 else
21419 if (at_eol_p
21420 || (target_x >= it.last_visible_x
21421 && it.line_wrap != TRUNCATE))
21423 if (pt_x > 0)
21424 move_it_by_lines (&it, 0);
21425 move_it_by_lines (&it, 1);
21426 target_x = 0;
21430 /* Move to the target X coordinate. */
21431 #ifdef HAVE_WINDOW_SYSTEM
21432 /* On GUI frames, as we don't know the X coordinate of the
21433 character to the left of point, moving point to the left
21434 requires walking, one grapheme cluster at a time, until we
21435 find ourself at a place immediately to the left of the
21436 character at point. */
21437 if (FRAME_WINDOW_P (it.f) && dir < 0)
21439 struct text_pos new_pos;
21440 enum move_it_result rc = MOVE_X_REACHED;
21442 if (it.current_x == 0)
21443 get_next_display_element (&it);
21444 if (it.what == IT_COMPOSITION)
21446 new_pos.charpos = it.cmp_it.charpos;
21447 new_pos.bytepos = -1;
21449 else
21450 new_pos = it.current.pos;
21452 while (it.current_x + it.pixel_width <= target_x
21453 && (rc == MOVE_X_REACHED
21454 /* Under word-wrap, move_it_in_display_line_to
21455 stops at correct coordinates, but sometimes
21456 returns MOVE_POS_MATCH_OR_ZV. */
21457 || (it.line_wrap == WORD_WRAP
21458 && rc == MOVE_POS_MATCH_OR_ZV)))
21460 int new_x = it.current_x + it.pixel_width;
21462 /* For composed characters, we want the position of the
21463 first character in the grapheme cluster (usually, the
21464 composition's base character), whereas it.current
21465 might give us the position of the _last_ one, e.g. if
21466 the composition is rendered in reverse due to bidi
21467 reordering. */
21468 if (it.what == IT_COMPOSITION)
21470 new_pos.charpos = it.cmp_it.charpos;
21471 new_pos.bytepos = -1;
21473 else
21474 new_pos = it.current.pos;
21475 if (new_x == it.current_x)
21476 new_x++;
21477 rc = move_it_in_display_line_to (&it, ZV, new_x,
21478 MOVE_TO_POS | MOVE_TO_X);
21479 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21480 break;
21482 /* The previous position we saw in the loop is the one we
21483 want. */
21484 if (new_pos.bytepos == -1)
21485 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21486 it.current.pos = new_pos;
21488 else
21489 #endif
21490 if (it.current_x != target_x)
21491 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21493 /* When lines are truncated, the above loop will stop at the
21494 window edge. But we want to get to the end of line, even if
21495 it is beyond the window edge; automatic hscroll will then
21496 scroll the window to show point as appropriate. */
21497 if (target_is_eol_p && it.line_wrap == TRUNCATE
21498 && get_next_display_element (&it))
21500 struct text_pos new_pos = it.current.pos;
21502 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21504 set_iterator_to_next (&it, false);
21505 if (it.method == GET_FROM_BUFFER)
21506 new_pos = it.current.pos;
21507 if (!get_next_display_element (&it))
21508 break;
21511 it.current.pos = new_pos;
21514 /* If we ended up in a display string that covers point, move to
21515 buffer position to the right in the visual order. */
21516 if (dir > 0)
21518 while (IT_CHARPOS (it) == PT)
21520 set_iterator_to_next (&it, false);
21521 if (!get_next_display_element (&it))
21522 break;
21526 /* Move point to that position. */
21527 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21530 return make_number (PT);
21532 #undef ROW_GLYPH_NEWLINE_P
21535 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21536 Sbidi_resolved_levels, 0, 1, 0,
21537 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21539 The resolved levels are produced by the Emacs bidi reordering engine
21540 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21541 read the Unicode Standard Annex 9 (UAX#9) for background information
21542 about these levels.
21544 VPOS is the zero-based number of the current window's screen line
21545 for which to produce the resolved levels. If VPOS is nil or omitted,
21546 it defaults to the screen line of point. If the window displays a
21547 header line, VPOS of zero will report on the header line, and first
21548 line of text in the window will have VPOS of 1.
21550 Value is an array of resolved levels, indexed by glyph number.
21551 Glyphs are numbered from zero starting from the beginning of the
21552 screen line, i.e. the left edge of the window for left-to-right lines
21553 and from the right edge for right-to-left lines. The resolved levels
21554 are produced only for the window's text area; text in display margins
21555 is not included.
21557 If the selected window's display is not up-to-date, or if the specified
21558 screen line does not display text, this function returns nil. It is
21559 highly recommended to bind this function to some simple key, like F8,
21560 in order to avoid these problems.
21562 This function exists mainly for testing the correctness of the
21563 Emacs UBA implementation, in particular with the test suite. */)
21564 (Lisp_Object vpos)
21566 struct window *w = XWINDOW (selected_window);
21567 struct buffer *b = XBUFFER (w->contents);
21568 int nrow;
21569 struct glyph_row *row;
21571 if (NILP (vpos))
21573 int d1, d2, d3, d4, d5;
21575 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21577 else
21579 CHECK_NUMBER_COERCE_MARKER (vpos);
21580 nrow = XINT (vpos);
21583 /* We require up-to-date glyph matrix for this window. */
21584 if (w->window_end_valid
21585 && !windows_or_buffers_changed
21586 && b
21587 && !b->clip_changed
21588 && !b->prevent_redisplay_optimizations_p
21589 && !window_outdated (w)
21590 && nrow >= 0
21591 && nrow < w->current_matrix->nrows
21592 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21593 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21595 struct glyph *g, *e, *g1;
21596 int nglyphs, i;
21597 Lisp_Object levels;
21599 if (!row->reversed_p) /* Left-to-right glyph row. */
21601 g = g1 = row->glyphs[TEXT_AREA];
21602 e = g + row->used[TEXT_AREA];
21604 /* Skip over glyphs at the start of the row that was
21605 generated by redisplay for its own needs. */
21606 while (g < e
21607 && NILP (g->object)
21608 && g->charpos < 0)
21609 g++;
21610 g1 = g;
21612 /* Count the "interesting" glyphs in this row. */
21613 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21614 nglyphs++;
21616 /* Create and fill the array. */
21617 levels = make_uninit_vector (nglyphs);
21618 for (i = 0; g1 < g; i++, g1++)
21619 ASET (levels, i, make_number (g1->resolved_level));
21621 else /* Right-to-left glyph row. */
21623 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21624 e = row->glyphs[TEXT_AREA] - 1;
21625 while (g > e
21626 && NILP (g->object)
21627 && g->charpos < 0)
21628 g--;
21629 g1 = g;
21630 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21631 nglyphs++;
21632 levels = make_uninit_vector (nglyphs);
21633 for (i = 0; g1 > g; i++, g1--)
21634 ASET (levels, i, make_number (g1->resolved_level));
21636 return levels;
21638 else
21639 return Qnil;
21644 /***********************************************************************
21645 Menu Bar
21646 ***********************************************************************/
21648 /* Redisplay the menu bar in the frame for window W.
21650 The menu bar of X frames that don't have X toolkit support is
21651 displayed in a special window W->frame->menu_bar_window.
21653 The menu bar of terminal frames is treated specially as far as
21654 glyph matrices are concerned. Menu bar lines are not part of
21655 windows, so the update is done directly on the frame matrix rows
21656 for the menu bar. */
21658 static void
21659 display_menu_bar (struct window *w)
21661 struct frame *f = XFRAME (WINDOW_FRAME (w));
21662 struct it it;
21663 Lisp_Object items;
21664 int i;
21666 /* Don't do all this for graphical frames. */
21667 #ifdef HAVE_NTGUI
21668 if (FRAME_W32_P (f))
21669 return;
21670 #endif
21671 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21672 if (FRAME_X_P (f))
21673 return;
21674 #endif
21676 #ifdef HAVE_NS
21677 if (FRAME_NS_P (f))
21678 return;
21679 #endif /* HAVE_NS */
21681 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21682 eassert (!FRAME_WINDOW_P (f));
21683 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21684 it.first_visible_x = 0;
21685 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21686 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21687 if (FRAME_WINDOW_P (f))
21689 /* Menu bar lines are displayed in the desired matrix of the
21690 dummy window menu_bar_window. */
21691 struct window *menu_w;
21692 menu_w = XWINDOW (f->menu_bar_window);
21693 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21694 MENU_FACE_ID);
21695 it.first_visible_x = 0;
21696 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21698 else
21699 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21701 /* This is a TTY frame, i.e. character hpos/vpos are used as
21702 pixel x/y. */
21703 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21704 MENU_FACE_ID);
21705 it.first_visible_x = 0;
21706 it.last_visible_x = FRAME_COLS (f);
21709 /* FIXME: This should be controlled by a user option. See the
21710 comments in redisplay_tool_bar and display_mode_line about
21711 this. */
21712 it.paragraph_embedding = L2R;
21714 /* Clear all rows of the menu bar. */
21715 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21717 struct glyph_row *row = it.glyph_row + i;
21718 clear_glyph_row (row);
21719 row->enabled_p = true;
21720 row->full_width_p = true;
21721 row->reversed_p = false;
21724 /* Display all items of the menu bar. */
21725 items = FRAME_MENU_BAR_ITEMS (it.f);
21726 for (i = 0; i < ASIZE (items); i += 4)
21728 Lisp_Object string;
21730 /* Stop at nil string. */
21731 string = AREF (items, i + 1);
21732 if (NILP (string))
21733 break;
21735 /* Remember where item was displayed. */
21736 ASET (items, i + 3, make_number (it.hpos));
21738 /* Display the item, pad with one space. */
21739 if (it.current_x < it.last_visible_x)
21740 display_string (NULL, string, Qnil, 0, 0, &it,
21741 SCHARS (string) + 1, 0, 0, -1);
21744 /* Fill out the line with spaces. */
21745 if (it.current_x < it.last_visible_x)
21746 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21748 /* Compute the total height of the lines. */
21749 compute_line_metrics (&it);
21752 /* Deep copy of a glyph row, including the glyphs. */
21753 static void
21754 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21756 struct glyph *pointers[1 + LAST_AREA];
21757 int to_used = to->used[TEXT_AREA];
21759 /* Save glyph pointers of TO. */
21760 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21762 /* Do a structure assignment. */
21763 *to = *from;
21765 /* Restore original glyph pointers of TO. */
21766 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21768 /* Copy the glyphs. */
21769 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21770 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21772 /* If we filled only part of the TO row, fill the rest with
21773 space_glyph (which will display as empty space). */
21774 if (to_used > from->used[TEXT_AREA])
21775 fill_up_frame_row_with_spaces (to, to_used);
21778 /* Display one menu item on a TTY, by overwriting the glyphs in the
21779 frame F's desired glyph matrix with glyphs produced from the menu
21780 item text. Called from term.c to display TTY drop-down menus one
21781 item at a time.
21783 ITEM_TEXT is the menu item text as a C string.
21785 FACE_ID is the face ID to be used for this menu item. FACE_ID
21786 could specify one of 3 faces: a face for an enabled item, a face
21787 for a disabled item, or a face for a selected item.
21789 X and Y are coordinates of the first glyph in the frame's desired
21790 matrix to be overwritten by the menu item. Since this is a TTY, Y
21791 is the zero-based number of the glyph row and X is the zero-based
21792 glyph number in the row, starting from left, where to start
21793 displaying the item.
21795 SUBMENU means this menu item drops down a submenu, which
21796 should be indicated by displaying a proper visual cue after the
21797 item text. */
21799 void
21800 display_tty_menu_item (const char *item_text, int width, int face_id,
21801 int x, int y, bool submenu)
21803 struct it it;
21804 struct frame *f = SELECTED_FRAME ();
21805 struct window *w = XWINDOW (f->selected_window);
21806 struct glyph_row *row;
21807 size_t item_len = strlen (item_text);
21809 eassert (FRAME_TERMCAP_P (f));
21811 /* Don't write beyond the matrix's last row. This can happen for
21812 TTY screens that are not high enough to show the entire menu.
21813 (This is actually a bit of defensive programming, as
21814 tty_menu_display already limits the number of menu items to one
21815 less than the number of screen lines.) */
21816 if (y >= f->desired_matrix->nrows)
21817 return;
21819 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21820 it.first_visible_x = 0;
21821 it.last_visible_x = FRAME_COLS (f) - 1;
21822 row = it.glyph_row;
21823 /* Start with the row contents from the current matrix. */
21824 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21825 bool saved_width = row->full_width_p;
21826 row->full_width_p = true;
21827 bool saved_reversed = row->reversed_p;
21828 row->reversed_p = false;
21829 row->enabled_p = true;
21831 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21832 desired face. */
21833 eassert (x < f->desired_matrix->matrix_w);
21834 it.current_x = it.hpos = x;
21835 it.current_y = it.vpos = y;
21836 int saved_used = row->used[TEXT_AREA];
21837 bool saved_truncated = row->truncated_on_right_p;
21838 row->used[TEXT_AREA] = x;
21839 it.face_id = face_id;
21840 it.line_wrap = TRUNCATE;
21842 /* FIXME: This should be controlled by a user option. See the
21843 comments in redisplay_tool_bar and display_mode_line about this.
21844 Also, if paragraph_embedding could ever be R2L, changes will be
21845 needed to avoid shifting to the right the row characters in
21846 term.c:append_glyph. */
21847 it.paragraph_embedding = L2R;
21849 /* Pad with a space on the left. */
21850 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21851 width--;
21852 /* Display the menu item, pad with spaces to WIDTH. */
21853 if (submenu)
21855 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21856 item_len, 0, FRAME_COLS (f) - 1, -1);
21857 width -= item_len;
21858 /* Indicate with " >" that there's a submenu. */
21859 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21860 FRAME_COLS (f) - 1, -1);
21862 else
21863 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21864 width, 0, FRAME_COLS (f) - 1, -1);
21866 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21867 row->truncated_on_right_p = saved_truncated;
21868 row->hash = row_hash (row);
21869 row->full_width_p = saved_width;
21870 row->reversed_p = saved_reversed;
21873 /***********************************************************************
21874 Mode Line
21875 ***********************************************************************/
21877 /* Redisplay mode lines in the window tree whose root is WINDOW.
21878 If FORCE, redisplay mode lines unconditionally.
21879 Otherwise, redisplay only mode lines that are garbaged. Value is
21880 the number of windows whose mode lines were redisplayed. */
21882 static int
21883 redisplay_mode_lines (Lisp_Object window, bool force)
21885 int nwindows = 0;
21887 while (!NILP (window))
21889 struct window *w = XWINDOW (window);
21891 if (WINDOWP (w->contents))
21892 nwindows += redisplay_mode_lines (w->contents, force);
21893 else if (force
21894 || FRAME_GARBAGED_P (XFRAME (w->frame))
21895 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21897 struct text_pos lpoint;
21898 struct buffer *old = current_buffer;
21900 /* Set the window's buffer for the mode line display. */
21901 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21902 set_buffer_internal_1 (XBUFFER (w->contents));
21904 /* Point refers normally to the selected window. For any
21905 other window, set up appropriate value. */
21906 if (!EQ (window, selected_window))
21908 struct text_pos pt;
21910 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21911 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21914 /* Display mode lines. */
21915 clear_glyph_matrix (w->desired_matrix);
21916 if (display_mode_lines (w))
21917 ++nwindows;
21919 /* Restore old settings. */
21920 set_buffer_internal_1 (old);
21921 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21924 window = w->next;
21927 return nwindows;
21931 /* Display the mode and/or header line of window W. Value is the
21932 sum number of mode lines and header lines displayed. */
21934 static int
21935 display_mode_lines (struct window *w)
21937 Lisp_Object old_selected_window = selected_window;
21938 Lisp_Object old_selected_frame = selected_frame;
21939 Lisp_Object new_frame = w->frame;
21940 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21941 int n = 0;
21943 selected_frame = new_frame;
21944 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21945 or window's point, then we'd need select_window_1 here as well. */
21946 XSETWINDOW (selected_window, w);
21947 XFRAME (new_frame)->selected_window = selected_window;
21949 /* These will be set while the mode line specs are processed. */
21950 line_number_displayed = false;
21951 w->column_number_displayed = -1;
21953 if (WINDOW_WANTS_MODELINE_P (w))
21955 struct window *sel_w = XWINDOW (old_selected_window);
21957 /* Select mode line face based on the real selected window. */
21958 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21959 BVAR (current_buffer, mode_line_format));
21960 ++n;
21963 if (WINDOW_WANTS_HEADER_LINE_P (w))
21965 display_mode_line (w, HEADER_LINE_FACE_ID,
21966 BVAR (current_buffer, header_line_format));
21967 ++n;
21970 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21971 selected_frame = old_selected_frame;
21972 selected_window = old_selected_window;
21973 if (n > 0)
21974 w->must_be_updated_p = true;
21975 return n;
21979 /* Display mode or header line of window W. FACE_ID specifies which
21980 line to display; it is either MODE_LINE_FACE_ID or
21981 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21982 display. Value is the pixel height of the mode/header line
21983 displayed. */
21985 static int
21986 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21988 struct it it;
21989 struct face *face;
21990 ptrdiff_t count = SPECPDL_INDEX ();
21992 init_iterator (&it, w, -1, -1, NULL, face_id);
21993 /* Don't extend on a previously drawn mode-line.
21994 This may happen if called from pos_visible_p. */
21995 it.glyph_row->enabled_p = false;
21996 prepare_desired_row (w, it.glyph_row, true);
21998 it.glyph_row->mode_line_p = true;
22000 /* FIXME: This should be controlled by a user option. But
22001 supporting such an option is not trivial, since the mode line is
22002 made up of many separate strings. */
22003 it.paragraph_embedding = L2R;
22005 record_unwind_protect (unwind_format_mode_line,
22006 format_mode_line_unwind_data (NULL, NULL,
22007 Qnil, false));
22009 mode_line_target = MODE_LINE_DISPLAY;
22011 /* Temporarily make frame's keyboard the current kboard so that
22012 kboard-local variables in the mode_line_format will get the right
22013 values. */
22014 push_kboard (FRAME_KBOARD (it.f));
22015 record_unwind_save_match_data ();
22016 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22017 pop_kboard ();
22019 unbind_to (count, Qnil);
22021 /* Fill up with spaces. */
22022 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22024 compute_line_metrics (&it);
22025 it.glyph_row->full_width_p = true;
22026 it.glyph_row->continued_p = false;
22027 it.glyph_row->truncated_on_left_p = false;
22028 it.glyph_row->truncated_on_right_p = false;
22030 /* Make a 3D mode-line have a shadow at its right end. */
22031 face = FACE_FROM_ID (it.f, face_id);
22032 extend_face_to_end_of_line (&it);
22033 if (face->box != FACE_NO_BOX)
22035 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22036 + it.glyph_row->used[TEXT_AREA] - 1);
22037 last->right_box_line_p = true;
22040 return it.glyph_row->height;
22043 /* Move element ELT in LIST to the front of LIST.
22044 Return the updated list. */
22046 static Lisp_Object
22047 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22049 register Lisp_Object tail, prev;
22050 register Lisp_Object tem;
22052 tail = list;
22053 prev = Qnil;
22054 while (CONSP (tail))
22056 tem = XCAR (tail);
22058 if (EQ (elt, tem))
22060 /* Splice out the link TAIL. */
22061 if (NILP (prev))
22062 list = XCDR (tail);
22063 else
22064 Fsetcdr (prev, XCDR (tail));
22066 /* Now make it the first. */
22067 Fsetcdr (tail, list);
22068 return tail;
22070 else
22071 prev = tail;
22072 tail = XCDR (tail);
22073 QUIT;
22076 /* Not found--return unchanged LIST. */
22077 return list;
22080 /* Contribute ELT to the mode line for window IT->w. How it
22081 translates into text depends on its data type.
22083 IT describes the display environment in which we display, as usual.
22085 DEPTH is the depth in recursion. It is used to prevent
22086 infinite recursion here.
22088 FIELD_WIDTH is the number of characters the display of ELT should
22089 occupy in the mode line, and PRECISION is the maximum number of
22090 characters to display from ELT's representation. See
22091 display_string for details.
22093 Returns the hpos of the end of the text generated by ELT.
22095 PROPS is a property list to add to any string we encounter.
22097 If RISKY, remove (disregard) any properties in any string
22098 we encounter, and ignore :eval and :propertize.
22100 The global variable `mode_line_target' determines whether the
22101 output is passed to `store_mode_line_noprop',
22102 `store_mode_line_string', or `display_string'. */
22104 static int
22105 display_mode_element (struct it *it, int depth, int field_width, int precision,
22106 Lisp_Object elt, Lisp_Object props, bool risky)
22108 int n = 0, field, prec;
22109 bool literal = false;
22111 tail_recurse:
22112 if (depth > 100)
22113 elt = build_string ("*too-deep*");
22115 depth++;
22117 switch (XTYPE (elt))
22119 case Lisp_String:
22121 /* A string: output it and check for %-constructs within it. */
22122 unsigned char c;
22123 ptrdiff_t offset = 0;
22125 if (SCHARS (elt) > 0
22126 && (!NILP (props) || risky))
22128 Lisp_Object oprops, aelt;
22129 oprops = Ftext_properties_at (make_number (0), elt);
22131 /* If the starting string's properties are not what
22132 we want, translate the string. Also, if the string
22133 is risky, do that anyway. */
22135 if (NILP (Fequal (props, oprops)) || risky)
22137 /* If the starting string has properties,
22138 merge the specified ones onto the existing ones. */
22139 if (! NILP (oprops) && !risky)
22141 Lisp_Object tem;
22143 oprops = Fcopy_sequence (oprops);
22144 tem = props;
22145 while (CONSP (tem))
22147 oprops = Fplist_put (oprops, XCAR (tem),
22148 XCAR (XCDR (tem)));
22149 tem = XCDR (XCDR (tem));
22151 props = oprops;
22154 aelt = Fassoc (elt, mode_line_proptrans_alist);
22155 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22157 /* AELT is what we want. Move it to the front
22158 without consing. */
22159 elt = XCAR (aelt);
22160 mode_line_proptrans_alist
22161 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22163 else
22165 Lisp_Object tem;
22167 /* If AELT has the wrong props, it is useless.
22168 so get rid of it. */
22169 if (! NILP (aelt))
22170 mode_line_proptrans_alist
22171 = Fdelq (aelt, mode_line_proptrans_alist);
22173 elt = Fcopy_sequence (elt);
22174 Fset_text_properties (make_number (0), Flength (elt),
22175 props, elt);
22176 /* Add this item to mode_line_proptrans_alist. */
22177 mode_line_proptrans_alist
22178 = Fcons (Fcons (elt, props),
22179 mode_line_proptrans_alist);
22180 /* Truncate mode_line_proptrans_alist
22181 to at most 50 elements. */
22182 tem = Fnthcdr (make_number (50),
22183 mode_line_proptrans_alist);
22184 if (! NILP (tem))
22185 XSETCDR (tem, Qnil);
22190 offset = 0;
22192 if (literal)
22194 prec = precision - n;
22195 switch (mode_line_target)
22197 case MODE_LINE_NOPROP:
22198 case MODE_LINE_TITLE:
22199 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22200 break;
22201 case MODE_LINE_STRING:
22202 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22203 break;
22204 case MODE_LINE_DISPLAY:
22205 n += display_string (NULL, elt, Qnil, 0, 0, it,
22206 0, prec, 0, STRING_MULTIBYTE (elt));
22207 break;
22210 break;
22213 /* Handle the non-literal case. */
22215 while ((precision <= 0 || n < precision)
22216 && SREF (elt, offset) != 0
22217 && (mode_line_target != MODE_LINE_DISPLAY
22218 || it->current_x < it->last_visible_x))
22220 ptrdiff_t last_offset = offset;
22222 /* Advance to end of string or next format specifier. */
22223 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22226 if (offset - 1 != last_offset)
22228 ptrdiff_t nchars, nbytes;
22230 /* Output to end of string or up to '%'. Field width
22231 is length of string. Don't output more than
22232 PRECISION allows us. */
22233 offset--;
22235 prec = c_string_width (SDATA (elt) + last_offset,
22236 offset - last_offset, precision - n,
22237 &nchars, &nbytes);
22239 switch (mode_line_target)
22241 case MODE_LINE_NOPROP:
22242 case MODE_LINE_TITLE:
22243 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22244 break;
22245 case MODE_LINE_STRING:
22247 ptrdiff_t bytepos = last_offset;
22248 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22249 ptrdiff_t endpos = (precision <= 0
22250 ? string_byte_to_char (elt, offset)
22251 : charpos + nchars);
22252 Lisp_Object mode_string
22253 = Fsubstring (elt, make_number (charpos),
22254 make_number (endpos));
22255 n += store_mode_line_string (NULL, mode_string, false,
22256 0, 0, Qnil);
22258 break;
22259 case MODE_LINE_DISPLAY:
22261 ptrdiff_t bytepos = last_offset;
22262 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22264 if (precision <= 0)
22265 nchars = string_byte_to_char (elt, offset) - charpos;
22266 n += display_string (NULL, elt, Qnil, 0, charpos,
22267 it, 0, nchars, 0,
22268 STRING_MULTIBYTE (elt));
22270 break;
22273 else /* c == '%' */
22275 ptrdiff_t percent_position = offset;
22277 /* Get the specified minimum width. Zero means
22278 don't pad. */
22279 field = 0;
22280 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22281 field = field * 10 + c - '0';
22283 /* Don't pad beyond the total padding allowed. */
22284 if (field_width - n > 0 && field > field_width - n)
22285 field = field_width - n;
22287 /* Note that either PRECISION <= 0 or N < PRECISION. */
22288 prec = precision - n;
22290 if (c == 'M')
22291 n += display_mode_element (it, depth, field, prec,
22292 Vglobal_mode_string, props,
22293 risky);
22294 else if (c != 0)
22296 bool multibyte;
22297 ptrdiff_t bytepos, charpos;
22298 const char *spec;
22299 Lisp_Object string;
22301 bytepos = percent_position;
22302 charpos = (STRING_MULTIBYTE (elt)
22303 ? string_byte_to_char (elt, bytepos)
22304 : bytepos);
22305 spec = decode_mode_spec (it->w, c, field, &string);
22306 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22308 switch (mode_line_target)
22310 case MODE_LINE_NOPROP:
22311 case MODE_LINE_TITLE:
22312 n += store_mode_line_noprop (spec, field, prec);
22313 break;
22314 case MODE_LINE_STRING:
22316 Lisp_Object tem = build_string (spec);
22317 props = Ftext_properties_at (make_number (charpos), elt);
22318 /* Should only keep face property in props */
22319 n += store_mode_line_string (NULL, tem, false,
22320 field, prec, props);
22322 break;
22323 case MODE_LINE_DISPLAY:
22325 int nglyphs_before, nwritten;
22327 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22328 nwritten = display_string (spec, string, elt,
22329 charpos, 0, it,
22330 field, prec, 0,
22331 multibyte);
22333 /* Assign to the glyphs written above the
22334 string where the `%x' came from, position
22335 of the `%'. */
22336 if (nwritten > 0)
22338 struct glyph *glyph
22339 = (it->glyph_row->glyphs[TEXT_AREA]
22340 + nglyphs_before);
22341 int i;
22343 for (i = 0; i < nwritten; ++i)
22345 glyph[i].object = elt;
22346 glyph[i].charpos = charpos;
22349 n += nwritten;
22352 break;
22355 else /* c == 0 */
22356 break;
22360 break;
22362 case Lisp_Symbol:
22363 /* A symbol: process the value of the symbol recursively
22364 as if it appeared here directly. Avoid error if symbol void.
22365 Special case: if value of symbol is a string, output the string
22366 literally. */
22368 register Lisp_Object tem;
22370 /* If the variable is not marked as risky to set
22371 then its contents are risky to use. */
22372 if (NILP (Fget (elt, Qrisky_local_variable)))
22373 risky = true;
22375 tem = Fboundp (elt);
22376 if (!NILP (tem))
22378 tem = Fsymbol_value (elt);
22379 /* If value is a string, output that string literally:
22380 don't check for % within it. */
22381 if (STRINGP (tem))
22382 literal = true;
22384 if (!EQ (tem, elt))
22386 /* Give up right away for nil or t. */
22387 elt = tem;
22388 goto tail_recurse;
22392 break;
22394 case Lisp_Cons:
22396 register Lisp_Object car, tem;
22398 /* A cons cell: five distinct cases.
22399 If first element is :eval or :propertize, do something special.
22400 If first element is a string or a cons, process all the elements
22401 and effectively concatenate them.
22402 If first element is a negative number, truncate displaying cdr to
22403 at most that many characters. If positive, pad (with spaces)
22404 to at least that many characters.
22405 If first element is a symbol, process the cadr or caddr recursively
22406 according to whether the symbol's value is non-nil or nil. */
22407 car = XCAR (elt);
22408 if (EQ (car, QCeval))
22410 /* An element of the form (:eval FORM) means evaluate FORM
22411 and use the result as mode line elements. */
22413 if (risky)
22414 break;
22416 if (CONSP (XCDR (elt)))
22418 Lisp_Object spec;
22419 spec = safe__eval (true, XCAR (XCDR (elt)));
22420 n += display_mode_element (it, depth, field_width - n,
22421 precision - n, spec, props,
22422 risky);
22425 else if (EQ (car, QCpropertize))
22427 /* An element of the form (:propertize ELT PROPS...)
22428 means display ELT but applying properties PROPS. */
22430 if (risky)
22431 break;
22433 if (CONSP (XCDR (elt)))
22434 n += display_mode_element (it, depth, field_width - n,
22435 precision - n, XCAR (XCDR (elt)),
22436 XCDR (XCDR (elt)), risky);
22438 else if (SYMBOLP (car))
22440 tem = Fboundp (car);
22441 elt = XCDR (elt);
22442 if (!CONSP (elt))
22443 goto invalid;
22444 /* elt is now the cdr, and we know it is a cons cell.
22445 Use its car if CAR has a non-nil value. */
22446 if (!NILP (tem))
22448 tem = Fsymbol_value (car);
22449 if (!NILP (tem))
22451 elt = XCAR (elt);
22452 goto tail_recurse;
22455 /* Symbol's value is nil (or symbol is unbound)
22456 Get the cddr of the original list
22457 and if possible find the caddr and use that. */
22458 elt = XCDR (elt);
22459 if (NILP (elt))
22460 break;
22461 else if (!CONSP (elt))
22462 goto invalid;
22463 elt = XCAR (elt);
22464 goto tail_recurse;
22466 else if (INTEGERP (car))
22468 register int lim = XINT (car);
22469 elt = XCDR (elt);
22470 if (lim < 0)
22472 /* Negative int means reduce maximum width. */
22473 if (precision <= 0)
22474 precision = -lim;
22475 else
22476 precision = min (precision, -lim);
22478 else if (lim > 0)
22480 /* Padding specified. Don't let it be more than
22481 current maximum. */
22482 if (precision > 0)
22483 lim = min (precision, lim);
22485 /* If that's more padding than already wanted, queue it.
22486 But don't reduce padding already specified even if
22487 that is beyond the current truncation point. */
22488 field_width = max (lim, field_width);
22490 goto tail_recurse;
22492 else if (STRINGP (car) || CONSP (car))
22494 Lisp_Object halftail = elt;
22495 int len = 0;
22497 while (CONSP (elt)
22498 && (precision <= 0 || n < precision))
22500 n += display_mode_element (it, depth,
22501 /* Do padding only after the last
22502 element in the list. */
22503 (! CONSP (XCDR (elt))
22504 ? field_width - n
22505 : 0),
22506 precision - n, XCAR (elt),
22507 props, risky);
22508 elt = XCDR (elt);
22509 len++;
22510 if ((len & 1) == 0)
22511 halftail = XCDR (halftail);
22512 /* Check for cycle. */
22513 if (EQ (halftail, elt))
22514 break;
22518 break;
22520 default:
22521 invalid:
22522 elt = build_string ("*invalid*");
22523 goto tail_recurse;
22526 /* Pad to FIELD_WIDTH. */
22527 if (field_width > 0 && n < field_width)
22529 switch (mode_line_target)
22531 case MODE_LINE_NOPROP:
22532 case MODE_LINE_TITLE:
22533 n += store_mode_line_noprop ("", field_width - n, 0);
22534 break;
22535 case MODE_LINE_STRING:
22536 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22537 Qnil);
22538 break;
22539 case MODE_LINE_DISPLAY:
22540 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22541 0, 0, 0);
22542 break;
22546 return n;
22549 /* Store a mode-line string element in mode_line_string_list.
22551 If STRING is non-null, display that C string. Otherwise, the Lisp
22552 string LISP_STRING is displayed.
22554 FIELD_WIDTH is the minimum number of output glyphs to produce.
22555 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22556 with spaces. FIELD_WIDTH <= 0 means don't pad.
22558 PRECISION is the maximum number of characters to output from
22559 STRING. PRECISION <= 0 means don't truncate the string.
22561 If COPY_STRING, make a copy of LISP_STRING before adding
22562 properties to the string.
22564 PROPS are the properties to add to the string.
22565 The mode_line_string_face face property is always added to the string.
22568 static int
22569 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22570 bool copy_string,
22571 int field_width, int precision, Lisp_Object props)
22573 ptrdiff_t len;
22574 int n = 0;
22576 if (string != NULL)
22578 len = strlen (string);
22579 if (precision > 0 && len > precision)
22580 len = precision;
22581 lisp_string = make_string (string, len);
22582 if (NILP (props))
22583 props = mode_line_string_face_prop;
22584 else if (!NILP (mode_line_string_face))
22586 Lisp_Object face = Fplist_get (props, Qface);
22587 props = Fcopy_sequence (props);
22588 if (NILP (face))
22589 face = mode_line_string_face;
22590 else
22591 face = list2 (face, mode_line_string_face);
22592 props = Fplist_put (props, Qface, face);
22594 Fadd_text_properties (make_number (0), make_number (len),
22595 props, lisp_string);
22597 else
22599 len = XFASTINT (Flength (lisp_string));
22600 if (precision > 0 && len > precision)
22602 len = precision;
22603 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22604 precision = -1;
22606 if (!NILP (mode_line_string_face))
22608 Lisp_Object face;
22609 if (NILP (props))
22610 props = Ftext_properties_at (make_number (0), lisp_string);
22611 face = Fplist_get (props, Qface);
22612 if (NILP (face))
22613 face = mode_line_string_face;
22614 else
22615 face = list2 (face, mode_line_string_face);
22616 props = list2 (Qface, face);
22617 if (copy_string)
22618 lisp_string = Fcopy_sequence (lisp_string);
22620 if (!NILP (props))
22621 Fadd_text_properties (make_number (0), make_number (len),
22622 props, lisp_string);
22625 if (len > 0)
22627 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22628 n += len;
22631 if (field_width > len)
22633 field_width -= len;
22634 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22635 if (!NILP (props))
22636 Fadd_text_properties (make_number (0), make_number (field_width),
22637 props, lisp_string);
22638 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22639 n += field_width;
22642 return n;
22646 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22647 1, 4, 0,
22648 doc: /* Format a string out of a mode line format specification.
22649 First arg FORMAT specifies the mode line format (see `mode-line-format'
22650 for details) to use.
22652 By default, the format is evaluated for the currently selected window.
22654 Optional second arg FACE specifies the face property to put on all
22655 characters for which no face is specified. The value nil means the
22656 default face. The value t means whatever face the window's mode line
22657 currently uses (either `mode-line' or `mode-line-inactive',
22658 depending on whether the window is the selected window or not).
22659 An integer value means the value string has no text
22660 properties.
22662 Optional third and fourth args WINDOW and BUFFER specify the window
22663 and buffer to use as the context for the formatting (defaults
22664 are the selected window and the WINDOW's buffer). */)
22665 (Lisp_Object format, Lisp_Object face,
22666 Lisp_Object window, Lisp_Object buffer)
22668 struct it it;
22669 int len;
22670 struct window *w;
22671 struct buffer *old_buffer = NULL;
22672 int face_id;
22673 bool no_props = INTEGERP (face);
22674 ptrdiff_t count = SPECPDL_INDEX ();
22675 Lisp_Object str;
22676 int string_start = 0;
22678 w = decode_any_window (window);
22679 XSETWINDOW (window, w);
22681 if (NILP (buffer))
22682 buffer = w->contents;
22683 CHECK_BUFFER (buffer);
22685 /* Make formatting the modeline a non-op when noninteractive, otherwise
22686 there will be problems later caused by a partially initialized frame. */
22687 if (NILP (format) || noninteractive)
22688 return empty_unibyte_string;
22690 if (no_props)
22691 face = Qnil;
22693 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22694 : EQ (face, Qt) ? (EQ (window, selected_window)
22695 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22696 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22697 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22698 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22699 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22700 : DEFAULT_FACE_ID;
22702 old_buffer = current_buffer;
22704 /* Save things including mode_line_proptrans_alist,
22705 and set that to nil so that we don't alter the outer value. */
22706 record_unwind_protect (unwind_format_mode_line,
22707 format_mode_line_unwind_data
22708 (XFRAME (WINDOW_FRAME (w)),
22709 old_buffer, selected_window, true));
22710 mode_line_proptrans_alist = Qnil;
22712 Fselect_window (window, Qt);
22713 set_buffer_internal_1 (XBUFFER (buffer));
22715 init_iterator (&it, w, -1, -1, NULL, face_id);
22717 if (no_props)
22719 mode_line_target = MODE_LINE_NOPROP;
22720 mode_line_string_face_prop = Qnil;
22721 mode_line_string_list = Qnil;
22722 string_start = MODE_LINE_NOPROP_LEN (0);
22724 else
22726 mode_line_target = MODE_LINE_STRING;
22727 mode_line_string_list = Qnil;
22728 mode_line_string_face = face;
22729 mode_line_string_face_prop
22730 = NILP (face) ? Qnil : list2 (Qface, face);
22733 push_kboard (FRAME_KBOARD (it.f));
22734 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22735 pop_kboard ();
22737 if (no_props)
22739 len = MODE_LINE_NOPROP_LEN (string_start);
22740 str = make_string (mode_line_noprop_buf + string_start, len);
22742 else
22744 mode_line_string_list = Fnreverse (mode_line_string_list);
22745 str = Fmapconcat (Qidentity, mode_line_string_list,
22746 empty_unibyte_string);
22749 unbind_to (count, Qnil);
22750 return str;
22753 /* Write a null-terminated, right justified decimal representation of
22754 the positive integer D to BUF using a minimal field width WIDTH. */
22756 static void
22757 pint2str (register char *buf, register int width, register ptrdiff_t d)
22759 register char *p = buf;
22761 if (d <= 0)
22762 *p++ = '0';
22763 else
22765 while (d > 0)
22767 *p++ = d % 10 + '0';
22768 d /= 10;
22772 for (width -= (int) (p - buf); width > 0; --width)
22773 *p++ = ' ';
22774 *p-- = '\0';
22775 while (p > buf)
22777 d = *buf;
22778 *buf++ = *p;
22779 *p-- = d;
22783 /* Write a null-terminated, right justified decimal and "human
22784 readable" representation of the nonnegative integer D to BUF using
22785 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22787 static const char power_letter[] =
22789 0, /* no letter */
22790 'k', /* kilo */
22791 'M', /* mega */
22792 'G', /* giga */
22793 'T', /* tera */
22794 'P', /* peta */
22795 'E', /* exa */
22796 'Z', /* zetta */
22797 'Y' /* yotta */
22800 static void
22801 pint2hrstr (char *buf, int width, ptrdiff_t d)
22803 /* We aim to represent the nonnegative integer D as
22804 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22805 ptrdiff_t quotient = d;
22806 int remainder = 0;
22807 /* -1 means: do not use TENTHS. */
22808 int tenths = -1;
22809 int exponent = 0;
22811 /* Length of QUOTIENT.TENTHS as a string. */
22812 int length;
22814 char * psuffix;
22815 char * p;
22817 if (quotient >= 1000)
22819 /* Scale to the appropriate EXPONENT. */
22822 remainder = quotient % 1000;
22823 quotient /= 1000;
22824 exponent++;
22826 while (quotient >= 1000);
22828 /* Round to nearest and decide whether to use TENTHS or not. */
22829 if (quotient <= 9)
22831 tenths = remainder / 100;
22832 if (remainder % 100 >= 50)
22834 if (tenths < 9)
22835 tenths++;
22836 else
22838 quotient++;
22839 if (quotient == 10)
22840 tenths = -1;
22841 else
22842 tenths = 0;
22846 else
22847 if (remainder >= 500)
22849 if (quotient < 999)
22850 quotient++;
22851 else
22853 quotient = 1;
22854 exponent++;
22855 tenths = 0;
22860 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22861 if (tenths == -1 && quotient <= 99)
22862 if (quotient <= 9)
22863 length = 1;
22864 else
22865 length = 2;
22866 else
22867 length = 3;
22868 p = psuffix = buf + max (width, length);
22870 /* Print EXPONENT. */
22871 *psuffix++ = power_letter[exponent];
22872 *psuffix = '\0';
22874 /* Print TENTHS. */
22875 if (tenths >= 0)
22877 *--p = '0' + tenths;
22878 *--p = '.';
22881 /* Print QUOTIENT. */
22884 int digit = quotient % 10;
22885 *--p = '0' + digit;
22887 while ((quotient /= 10) != 0);
22889 /* Print leading spaces. */
22890 while (buf < p)
22891 *--p = ' ';
22894 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22895 If EOL_FLAG, set also a mnemonic character for end-of-line
22896 type of CODING_SYSTEM. Return updated pointer into BUF. */
22898 static unsigned char invalid_eol_type[] = "(*invalid*)";
22900 static char *
22901 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
22903 Lisp_Object val;
22904 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22905 const unsigned char *eol_str;
22906 int eol_str_len;
22907 /* The EOL conversion we are using. */
22908 Lisp_Object eoltype;
22910 val = CODING_SYSTEM_SPEC (coding_system);
22911 eoltype = Qnil;
22913 if (!VECTORP (val)) /* Not yet decided. */
22915 *buf++ = multibyte ? '-' : ' ';
22916 if (eol_flag)
22917 eoltype = eol_mnemonic_undecided;
22918 /* Don't mention EOL conversion if it isn't decided. */
22920 else
22922 Lisp_Object attrs;
22923 Lisp_Object eolvalue;
22925 attrs = AREF (val, 0);
22926 eolvalue = AREF (val, 2);
22928 *buf++ = multibyte
22929 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22930 : ' ';
22932 if (eol_flag)
22934 /* The EOL conversion that is normal on this system. */
22936 if (NILP (eolvalue)) /* Not yet decided. */
22937 eoltype = eol_mnemonic_undecided;
22938 else if (VECTORP (eolvalue)) /* Not yet decided. */
22939 eoltype = eol_mnemonic_undecided;
22940 else /* eolvalue is Qunix, Qdos, or Qmac. */
22941 eoltype = (EQ (eolvalue, Qunix)
22942 ? eol_mnemonic_unix
22943 : EQ (eolvalue, Qdos)
22944 ? eol_mnemonic_dos : eol_mnemonic_mac);
22948 if (eol_flag)
22950 /* Mention the EOL conversion if it is not the usual one. */
22951 if (STRINGP (eoltype))
22953 eol_str = SDATA (eoltype);
22954 eol_str_len = SBYTES (eoltype);
22956 else if (CHARACTERP (eoltype))
22958 int c = XFASTINT (eoltype);
22959 return buf + CHAR_STRING (c, (unsigned char *) buf);
22961 else
22963 eol_str = invalid_eol_type;
22964 eol_str_len = sizeof (invalid_eol_type) - 1;
22966 memcpy (buf, eol_str, eol_str_len);
22967 buf += eol_str_len;
22970 return buf;
22973 /* Return a string for the output of a mode line %-spec for window W,
22974 generated by character C. FIELD_WIDTH > 0 means pad the string
22975 returned with spaces to that value. Return a Lisp string in
22976 *STRING if the resulting string is taken from that Lisp string.
22978 Note we operate on the current buffer for most purposes. */
22980 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22982 static const char *
22983 decode_mode_spec (struct window *w, register int c, int field_width,
22984 Lisp_Object *string)
22986 Lisp_Object obj;
22987 struct frame *f = XFRAME (WINDOW_FRAME (w));
22988 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22989 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22990 produce strings from numerical values, so limit preposterously
22991 large values of FIELD_WIDTH to avoid overrunning the buffer's
22992 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22993 bytes plus the terminating null. */
22994 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22995 struct buffer *b = current_buffer;
22997 obj = Qnil;
22998 *string = Qnil;
23000 switch (c)
23002 case '*':
23003 if (!NILP (BVAR (b, read_only)))
23004 return "%";
23005 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23006 return "*";
23007 return "-";
23009 case '+':
23010 /* This differs from %* only for a modified read-only buffer. */
23011 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23012 return "*";
23013 if (!NILP (BVAR (b, read_only)))
23014 return "%";
23015 return "-";
23017 case '&':
23018 /* This differs from %* in ignoring read-only-ness. */
23019 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23020 return "*";
23021 return "-";
23023 case '%':
23024 return "%";
23026 case '[':
23028 int i;
23029 char *p;
23031 if (command_loop_level > 5)
23032 return "[[[... ";
23033 p = decode_mode_spec_buf;
23034 for (i = 0; i < command_loop_level; i++)
23035 *p++ = '[';
23036 *p = 0;
23037 return decode_mode_spec_buf;
23040 case ']':
23042 int i;
23043 char *p;
23045 if (command_loop_level > 5)
23046 return " ...]]]";
23047 p = decode_mode_spec_buf;
23048 for (i = 0; i < command_loop_level; i++)
23049 *p++ = ']';
23050 *p = 0;
23051 return decode_mode_spec_buf;
23054 case '-':
23056 register int i;
23058 /* Let lots_of_dashes be a string of infinite length. */
23059 if (mode_line_target == MODE_LINE_NOPROP
23060 || mode_line_target == MODE_LINE_STRING)
23061 return "--";
23062 if (field_width <= 0
23063 || field_width > sizeof (lots_of_dashes))
23065 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23066 decode_mode_spec_buf[i] = '-';
23067 decode_mode_spec_buf[i] = '\0';
23068 return decode_mode_spec_buf;
23070 else
23071 return lots_of_dashes;
23074 case 'b':
23075 obj = BVAR (b, name);
23076 break;
23078 case 'c':
23079 /* %c and %l are ignored in `frame-title-format'.
23080 (In redisplay_internal, the frame title is drawn _before_ the
23081 windows are updated, so the stuff which depends on actual
23082 window contents (such as %l) may fail to render properly, or
23083 even crash emacs.) */
23084 if (mode_line_target == MODE_LINE_TITLE)
23085 return "";
23086 else
23088 ptrdiff_t col = current_column ();
23089 w->column_number_displayed = col;
23090 pint2str (decode_mode_spec_buf, width, col);
23091 return decode_mode_spec_buf;
23094 case 'e':
23095 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23097 if (NILP (Vmemory_full))
23098 return "";
23099 else
23100 return "!MEM FULL! ";
23102 #else
23103 return "";
23104 #endif
23106 case 'F':
23107 /* %F displays the frame name. */
23108 if (!NILP (f->title))
23109 return SSDATA (f->title);
23110 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23111 return SSDATA (f->name);
23112 return "Emacs";
23114 case 'f':
23115 obj = BVAR (b, filename);
23116 break;
23118 case 'i':
23120 ptrdiff_t size = ZV - BEGV;
23121 pint2str (decode_mode_spec_buf, width, size);
23122 return decode_mode_spec_buf;
23125 case 'I':
23127 ptrdiff_t size = ZV - BEGV;
23128 pint2hrstr (decode_mode_spec_buf, width, size);
23129 return decode_mode_spec_buf;
23132 case 'l':
23134 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23135 ptrdiff_t topline, nlines, height;
23136 ptrdiff_t junk;
23138 /* %c and %l are ignored in `frame-title-format'. */
23139 if (mode_line_target == MODE_LINE_TITLE)
23140 return "";
23142 startpos = marker_position (w->start);
23143 startpos_byte = marker_byte_position (w->start);
23144 height = WINDOW_TOTAL_LINES (w);
23146 /* If we decided that this buffer isn't suitable for line numbers,
23147 don't forget that too fast. */
23148 if (w->base_line_pos == -1)
23149 goto no_value;
23151 /* If the buffer is very big, don't waste time. */
23152 if (INTEGERP (Vline_number_display_limit)
23153 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23155 w->base_line_pos = 0;
23156 w->base_line_number = 0;
23157 goto no_value;
23160 if (w->base_line_number > 0
23161 && w->base_line_pos > 0
23162 && w->base_line_pos <= startpos)
23164 line = w->base_line_number;
23165 linepos = w->base_line_pos;
23166 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23168 else
23170 line = 1;
23171 linepos = BUF_BEGV (b);
23172 linepos_byte = BUF_BEGV_BYTE (b);
23175 /* Count lines from base line to window start position. */
23176 nlines = display_count_lines (linepos_byte,
23177 startpos_byte,
23178 startpos, &junk);
23180 topline = nlines + line;
23182 /* Determine a new base line, if the old one is too close
23183 or too far away, or if we did not have one.
23184 "Too close" means it's plausible a scroll-down would
23185 go back past it. */
23186 if (startpos == BUF_BEGV (b))
23188 w->base_line_number = topline;
23189 w->base_line_pos = BUF_BEGV (b);
23191 else if (nlines < height + 25 || nlines > height * 3 + 50
23192 || linepos == BUF_BEGV (b))
23194 ptrdiff_t limit = BUF_BEGV (b);
23195 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23196 ptrdiff_t position;
23197 ptrdiff_t distance =
23198 (height * 2 + 30) * line_number_display_limit_width;
23200 if (startpos - distance > limit)
23202 limit = startpos - distance;
23203 limit_byte = CHAR_TO_BYTE (limit);
23206 nlines = display_count_lines (startpos_byte,
23207 limit_byte,
23208 - (height * 2 + 30),
23209 &position);
23210 /* If we couldn't find the lines we wanted within
23211 line_number_display_limit_width chars per line,
23212 give up on line numbers for this window. */
23213 if (position == limit_byte && limit == startpos - distance)
23215 w->base_line_pos = -1;
23216 w->base_line_number = 0;
23217 goto no_value;
23220 w->base_line_number = topline - nlines;
23221 w->base_line_pos = BYTE_TO_CHAR (position);
23224 /* Now count lines from the start pos to point. */
23225 nlines = display_count_lines (startpos_byte,
23226 PT_BYTE, PT, &junk);
23228 /* Record that we did display the line number. */
23229 line_number_displayed = true;
23231 /* Make the string to show. */
23232 pint2str (decode_mode_spec_buf, width, topline + nlines);
23233 return decode_mode_spec_buf;
23234 no_value:
23236 char *p = decode_mode_spec_buf;
23237 int pad = width - 2;
23238 while (pad-- > 0)
23239 *p++ = ' ';
23240 *p++ = '?';
23241 *p++ = '?';
23242 *p = '\0';
23243 return decode_mode_spec_buf;
23246 break;
23248 case 'm':
23249 obj = BVAR (b, mode_name);
23250 break;
23252 case 'n':
23253 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23254 return " Narrow";
23255 break;
23257 case 'p':
23259 ptrdiff_t pos = marker_position (w->start);
23260 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23262 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23264 if (pos <= BUF_BEGV (b))
23265 return "All";
23266 else
23267 return "Bottom";
23269 else if (pos <= BUF_BEGV (b))
23270 return "Top";
23271 else
23273 if (total > 1000000)
23274 /* Do it differently for a large value, to avoid overflow. */
23275 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23276 else
23277 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23278 /* We can't normally display a 3-digit number,
23279 so get us a 2-digit number that is close. */
23280 if (total == 100)
23281 total = 99;
23282 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23283 return decode_mode_spec_buf;
23287 /* Display percentage of size above the bottom of the screen. */
23288 case 'P':
23290 ptrdiff_t toppos = marker_position (w->start);
23291 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23292 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23294 if (botpos >= BUF_ZV (b))
23296 if (toppos <= BUF_BEGV (b))
23297 return "All";
23298 else
23299 return "Bottom";
23301 else
23303 if (total > 1000000)
23304 /* Do it differently for a large value, to avoid overflow. */
23305 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23306 else
23307 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23308 /* We can't normally display a 3-digit number,
23309 so get us a 2-digit number that is close. */
23310 if (total == 100)
23311 total = 99;
23312 if (toppos <= BUF_BEGV (b))
23313 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23314 else
23315 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23316 return decode_mode_spec_buf;
23320 case 's':
23321 /* status of process */
23322 obj = Fget_buffer_process (Fcurrent_buffer ());
23323 if (NILP (obj))
23324 return "no process";
23325 #ifndef MSDOS
23326 obj = Fsymbol_name (Fprocess_status (obj));
23327 #endif
23328 break;
23330 case '@':
23332 ptrdiff_t count = inhibit_garbage_collection ();
23333 Lisp_Object curdir = BVAR (current_buffer, directory);
23334 Lisp_Object val = Qnil;
23336 if (STRINGP (curdir))
23337 val = call1 (intern ("file-remote-p"), curdir);
23339 unbind_to (count, Qnil);
23341 if (NILP (val))
23342 return "-";
23343 else
23344 return "@";
23347 case 'z':
23348 /* coding-system (not including end-of-line format) */
23349 case 'Z':
23350 /* coding-system (including end-of-line type) */
23352 bool eol_flag = (c == 'Z');
23353 char *p = decode_mode_spec_buf;
23355 if (! FRAME_WINDOW_P (f))
23357 /* No need to mention EOL here--the terminal never needs
23358 to do EOL conversion. */
23359 p = decode_mode_spec_coding (CODING_ID_NAME
23360 (FRAME_KEYBOARD_CODING (f)->id),
23361 p, false);
23362 p = decode_mode_spec_coding (CODING_ID_NAME
23363 (FRAME_TERMINAL_CODING (f)->id),
23364 p, false);
23366 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23367 p, eol_flag);
23369 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23370 #ifdef subprocesses
23371 obj = Fget_buffer_process (Fcurrent_buffer ());
23372 if (PROCESSP (obj))
23374 p = decode_mode_spec_coding
23375 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23376 p = decode_mode_spec_coding
23377 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23379 #endif /* subprocesses */
23380 #endif /* false */
23381 *p = 0;
23382 return decode_mode_spec_buf;
23386 if (STRINGP (obj))
23388 *string = obj;
23389 return SSDATA (obj);
23391 else
23392 return "";
23396 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23397 means count lines back from START_BYTE. But don't go beyond
23398 LIMIT_BYTE. Return the number of lines thus found (always
23399 nonnegative).
23401 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23402 either the position COUNT lines after/before START_BYTE, if we
23403 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23404 COUNT lines. */
23406 static ptrdiff_t
23407 display_count_lines (ptrdiff_t start_byte,
23408 ptrdiff_t limit_byte, ptrdiff_t count,
23409 ptrdiff_t *byte_pos_ptr)
23411 register unsigned char *cursor;
23412 unsigned char *base;
23414 register ptrdiff_t ceiling;
23415 register unsigned char *ceiling_addr;
23416 ptrdiff_t orig_count = count;
23418 /* If we are not in selective display mode,
23419 check only for newlines. */
23420 bool selective_display
23421 = (!NILP (BVAR (current_buffer, selective_display))
23422 && !INTEGERP (BVAR (current_buffer, selective_display)));
23424 if (count > 0)
23426 while (start_byte < limit_byte)
23428 ceiling = BUFFER_CEILING_OF (start_byte);
23429 ceiling = min (limit_byte - 1, ceiling);
23430 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23431 base = (cursor = BYTE_POS_ADDR (start_byte));
23435 if (selective_display)
23437 while (*cursor != '\n' && *cursor != 015
23438 && ++cursor != ceiling_addr)
23439 continue;
23440 if (cursor == ceiling_addr)
23441 break;
23443 else
23445 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23446 if (! cursor)
23447 break;
23450 cursor++;
23452 if (--count == 0)
23454 start_byte += cursor - base;
23455 *byte_pos_ptr = start_byte;
23456 return orig_count;
23459 while (cursor < ceiling_addr);
23461 start_byte += ceiling_addr - base;
23464 else
23466 while (start_byte > limit_byte)
23468 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23469 ceiling = max (limit_byte, ceiling);
23470 ceiling_addr = BYTE_POS_ADDR (ceiling);
23471 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23472 while (true)
23474 if (selective_display)
23476 while (--cursor >= ceiling_addr
23477 && *cursor != '\n' && *cursor != 015)
23478 continue;
23479 if (cursor < ceiling_addr)
23480 break;
23482 else
23484 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23485 if (! cursor)
23486 break;
23489 if (++count == 0)
23491 start_byte += cursor - base + 1;
23492 *byte_pos_ptr = start_byte;
23493 /* When scanning backwards, we should
23494 not count the newline posterior to which we stop. */
23495 return - orig_count - 1;
23498 start_byte += ceiling_addr - base;
23502 *byte_pos_ptr = limit_byte;
23504 if (count < 0)
23505 return - orig_count + count;
23506 return orig_count - count;
23512 /***********************************************************************
23513 Displaying strings
23514 ***********************************************************************/
23516 /* Display a NUL-terminated string, starting with index START.
23518 If STRING is non-null, display that C string. Otherwise, the Lisp
23519 string LISP_STRING is displayed. There's a case that STRING is
23520 non-null and LISP_STRING is not nil. It means STRING is a string
23521 data of LISP_STRING. In that case, we display LISP_STRING while
23522 ignoring its text properties.
23524 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23525 FACE_STRING. Display STRING or LISP_STRING with the face at
23526 FACE_STRING_POS in FACE_STRING:
23528 Display the string in the environment given by IT, but use the
23529 standard display table, temporarily.
23531 FIELD_WIDTH is the minimum number of output glyphs to produce.
23532 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23533 with spaces. If STRING has more characters, more than FIELD_WIDTH
23534 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23536 PRECISION is the maximum number of characters to output from
23537 STRING. PRECISION < 0 means don't truncate the string.
23539 This is roughly equivalent to printf format specifiers:
23541 FIELD_WIDTH PRECISION PRINTF
23542 ----------------------------------------
23543 -1 -1 %s
23544 -1 10 %.10s
23545 10 -1 %10s
23546 20 10 %20.10s
23548 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23549 display them, and < 0 means obey the current buffer's value of
23550 enable_multibyte_characters.
23552 Value is the number of columns displayed. */
23554 static int
23555 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23556 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23557 int field_width, int precision, int max_x, int multibyte)
23559 int hpos_at_start = it->hpos;
23560 int saved_face_id = it->face_id;
23561 struct glyph_row *row = it->glyph_row;
23562 ptrdiff_t it_charpos;
23564 /* Initialize the iterator IT for iteration over STRING beginning
23565 with index START. */
23566 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23567 precision, field_width, multibyte);
23568 if (string && STRINGP (lisp_string))
23569 /* LISP_STRING is the one returned by decode_mode_spec. We should
23570 ignore its text properties. */
23571 it->stop_charpos = it->end_charpos;
23573 /* If displaying STRING, set up the face of the iterator from
23574 FACE_STRING, if that's given. */
23575 if (STRINGP (face_string))
23577 ptrdiff_t endptr;
23578 struct face *face;
23580 it->face_id
23581 = face_at_string_position (it->w, face_string, face_string_pos,
23582 0, &endptr, it->base_face_id, false);
23583 face = FACE_FROM_ID (it->f, it->face_id);
23584 it->face_box_p = face->box != FACE_NO_BOX;
23587 /* Set max_x to the maximum allowed X position. Don't let it go
23588 beyond the right edge of the window. */
23589 if (max_x <= 0)
23590 max_x = it->last_visible_x;
23591 else
23592 max_x = min (max_x, it->last_visible_x);
23594 /* Skip over display elements that are not visible. because IT->w is
23595 hscrolled. */
23596 if (it->current_x < it->first_visible_x)
23597 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23598 MOVE_TO_POS | MOVE_TO_X);
23600 row->ascent = it->max_ascent;
23601 row->height = it->max_ascent + it->max_descent;
23602 row->phys_ascent = it->max_phys_ascent;
23603 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23604 row->extra_line_spacing = it->max_extra_line_spacing;
23606 if (STRINGP (it->string))
23607 it_charpos = IT_STRING_CHARPOS (*it);
23608 else
23609 it_charpos = IT_CHARPOS (*it);
23611 /* This condition is for the case that we are called with current_x
23612 past last_visible_x. */
23613 while (it->current_x < max_x)
23615 int x_before, x, n_glyphs_before, i, nglyphs;
23617 /* Get the next display element. */
23618 if (!get_next_display_element (it))
23619 break;
23621 /* Produce glyphs. */
23622 x_before = it->current_x;
23623 n_glyphs_before = row->used[TEXT_AREA];
23624 PRODUCE_GLYPHS (it);
23626 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23627 i = 0;
23628 x = x_before;
23629 while (i < nglyphs)
23631 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23633 if (it->line_wrap != TRUNCATE
23634 && x + glyph->pixel_width > max_x)
23636 /* End of continued line or max_x reached. */
23637 if (CHAR_GLYPH_PADDING_P (*glyph))
23639 /* A wide character is unbreakable. */
23640 if (row->reversed_p)
23641 unproduce_glyphs (it, row->used[TEXT_AREA]
23642 - n_glyphs_before);
23643 row->used[TEXT_AREA] = n_glyphs_before;
23644 it->current_x = x_before;
23646 else
23648 if (row->reversed_p)
23649 unproduce_glyphs (it, row->used[TEXT_AREA]
23650 - (n_glyphs_before + i));
23651 row->used[TEXT_AREA] = n_glyphs_before + i;
23652 it->current_x = x;
23654 break;
23656 else if (x + glyph->pixel_width >= it->first_visible_x)
23658 /* Glyph is at least partially visible. */
23659 ++it->hpos;
23660 if (x < it->first_visible_x)
23661 row->x = x - it->first_visible_x;
23663 else
23665 /* Glyph is off the left margin of the display area.
23666 Should not happen. */
23667 emacs_abort ();
23670 row->ascent = max (row->ascent, it->max_ascent);
23671 row->height = max (row->height, it->max_ascent + it->max_descent);
23672 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23673 row->phys_height = max (row->phys_height,
23674 it->max_phys_ascent + it->max_phys_descent);
23675 row->extra_line_spacing = max (row->extra_line_spacing,
23676 it->max_extra_line_spacing);
23677 x += glyph->pixel_width;
23678 ++i;
23681 /* Stop if max_x reached. */
23682 if (i < nglyphs)
23683 break;
23685 /* Stop at line ends. */
23686 if (ITERATOR_AT_END_OF_LINE_P (it))
23688 it->continuation_lines_width = 0;
23689 break;
23692 set_iterator_to_next (it, true);
23693 if (STRINGP (it->string))
23694 it_charpos = IT_STRING_CHARPOS (*it);
23695 else
23696 it_charpos = IT_CHARPOS (*it);
23698 /* Stop if truncating at the right edge. */
23699 if (it->line_wrap == TRUNCATE
23700 && it->current_x >= it->last_visible_x)
23702 /* Add truncation mark, but don't do it if the line is
23703 truncated at a padding space. */
23704 if (it_charpos < it->string_nchars)
23706 if (!FRAME_WINDOW_P (it->f))
23708 int ii, n;
23710 if (it->current_x > it->last_visible_x)
23712 if (!row->reversed_p)
23714 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23715 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23716 break;
23718 else
23720 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23721 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23722 break;
23723 unproduce_glyphs (it, ii + 1);
23724 ii = row->used[TEXT_AREA] - (ii + 1);
23726 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23728 row->used[TEXT_AREA] = ii;
23729 produce_special_glyphs (it, IT_TRUNCATION);
23732 produce_special_glyphs (it, IT_TRUNCATION);
23734 row->truncated_on_right_p = true;
23736 break;
23740 /* Maybe insert a truncation at the left. */
23741 if (it->first_visible_x
23742 && it_charpos > 0)
23744 if (!FRAME_WINDOW_P (it->f)
23745 || (row->reversed_p
23746 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23747 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23748 insert_left_trunc_glyphs (it);
23749 row->truncated_on_left_p = true;
23752 it->face_id = saved_face_id;
23754 /* Value is number of columns displayed. */
23755 return it->hpos - hpos_at_start;
23760 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23761 appears as an element of LIST or as the car of an element of LIST.
23762 If PROPVAL is a list, compare each element against LIST in that
23763 way, and return 1/2 if any element of PROPVAL is found in LIST.
23764 Otherwise return 0. This function cannot quit.
23765 The return value is 2 if the text is invisible but with an ellipsis
23766 and 1 if it's invisible and without an ellipsis. */
23769 invisible_prop (Lisp_Object propval, Lisp_Object list)
23771 Lisp_Object tail, proptail;
23773 for (tail = list; CONSP (tail); tail = XCDR (tail))
23775 register Lisp_Object tem;
23776 tem = XCAR (tail);
23777 if (EQ (propval, tem))
23778 return 1;
23779 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23780 return NILP (XCDR (tem)) ? 1 : 2;
23783 if (CONSP (propval))
23785 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23787 Lisp_Object propelt;
23788 propelt = XCAR (proptail);
23789 for (tail = list; CONSP (tail); tail = XCDR (tail))
23791 register Lisp_Object tem;
23792 tem = XCAR (tail);
23793 if (EQ (propelt, tem))
23794 return 1;
23795 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23796 return NILP (XCDR (tem)) ? 1 : 2;
23801 return 0;
23804 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23805 doc: /* Non-nil if the property makes the text invisible.
23806 POS-OR-PROP can be a marker or number, in which case it is taken to be
23807 a position in the current buffer and the value of the `invisible' property
23808 is checked; or it can be some other value, which is then presumed to be the
23809 value of the `invisible' property of the text of interest.
23810 The non-nil value returned can be t for truly invisible text or something
23811 else if the text is replaced by an ellipsis. */)
23812 (Lisp_Object pos_or_prop)
23814 Lisp_Object prop
23815 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23816 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23817 : pos_or_prop);
23818 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23819 return (invis == 0 ? Qnil
23820 : invis == 1 ? Qt
23821 : make_number (invis));
23824 /* Calculate a width or height in pixels from a specification using
23825 the following elements:
23827 SPEC ::=
23828 NUM - a (fractional) multiple of the default font width/height
23829 (NUM) - specifies exactly NUM pixels
23830 UNIT - a fixed number of pixels, see below.
23831 ELEMENT - size of a display element in pixels, see below.
23832 (NUM . SPEC) - equals NUM * SPEC
23833 (+ SPEC SPEC ...) - add pixel values
23834 (- SPEC SPEC ...) - subtract pixel values
23835 (- SPEC) - negate pixel value
23837 NUM ::=
23838 INT or FLOAT - a number constant
23839 SYMBOL - use symbol's (buffer local) variable binding.
23841 UNIT ::=
23842 in - pixels per inch *)
23843 mm - pixels per 1/1000 meter *)
23844 cm - pixels per 1/100 meter *)
23845 width - width of current font in pixels.
23846 height - height of current font in pixels.
23848 *) using the ratio(s) defined in display-pixels-per-inch.
23850 ELEMENT ::=
23852 left-fringe - left fringe width in pixels
23853 right-fringe - right fringe width in pixels
23855 left-margin - left margin width in pixels
23856 right-margin - right margin width in pixels
23858 scroll-bar - scroll-bar area width in pixels
23860 Examples:
23862 Pixels corresponding to 5 inches:
23863 (5 . in)
23865 Total width of non-text areas on left side of window (if scroll-bar is on left):
23866 '(space :width (+ left-fringe left-margin scroll-bar))
23868 Align to first text column (in header line):
23869 '(space :align-to 0)
23871 Align to middle of text area minus half the width of variable `my-image'
23872 containing a loaded image:
23873 '(space :align-to (0.5 . (- text my-image)))
23875 Width of left margin minus width of 1 character in the default font:
23876 '(space :width (- left-margin 1))
23878 Width of left margin minus width of 2 characters in the current font:
23879 '(space :width (- left-margin (2 . width)))
23881 Center 1 character over left-margin (in header line):
23882 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23884 Different ways to express width of left fringe plus left margin minus one pixel:
23885 '(space :width (- (+ left-fringe left-margin) (1)))
23886 '(space :width (+ left-fringe left-margin (- (1))))
23887 '(space :width (+ left-fringe left-margin (-1)))
23891 static bool
23892 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23893 struct font *font, bool width_p, int *align_to)
23895 double pixels;
23897 # define OK_PIXELS(val) (*res = (val), true)
23898 # define OK_ALIGN_TO(val) (*align_to = (val), true)
23900 if (NILP (prop))
23901 return OK_PIXELS (0);
23903 eassert (FRAME_LIVE_P (it->f));
23905 if (SYMBOLP (prop))
23907 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23909 char *unit = SSDATA (SYMBOL_NAME (prop));
23911 if (unit[0] == 'i' && unit[1] == 'n')
23912 pixels = 1.0;
23913 else if (unit[0] == 'm' && unit[1] == 'm')
23914 pixels = 25.4;
23915 else if (unit[0] == 'c' && unit[1] == 'm')
23916 pixels = 2.54;
23917 else
23918 pixels = 0;
23919 if (pixels > 0)
23921 double ppi = (width_p ? FRAME_RES_X (it->f)
23922 : FRAME_RES_Y (it->f));
23924 if (ppi > 0)
23925 return OK_PIXELS (ppi / pixels);
23926 return false;
23930 #ifdef HAVE_WINDOW_SYSTEM
23931 if (EQ (prop, Qheight))
23932 return OK_PIXELS (font
23933 ? normal_char_height (font)
23934 : FRAME_LINE_HEIGHT (it->f));
23935 if (EQ (prop, Qwidth))
23936 return OK_PIXELS (font
23937 ? FONT_WIDTH (font)
23938 : FRAME_COLUMN_WIDTH (it->f));
23939 #else
23940 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23941 return OK_PIXELS (1);
23942 #endif
23944 if (EQ (prop, Qtext))
23945 return OK_PIXELS (width_p
23946 ? window_box_width (it->w, TEXT_AREA)
23947 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23949 if (align_to && *align_to < 0)
23951 *res = 0;
23952 if (EQ (prop, Qleft))
23953 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23954 if (EQ (prop, Qright))
23955 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23956 if (EQ (prop, Qcenter))
23957 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23958 + window_box_width (it->w, TEXT_AREA) / 2);
23959 if (EQ (prop, Qleft_fringe))
23960 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23961 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23962 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23963 if (EQ (prop, Qright_fringe))
23964 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23965 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23966 : window_box_right_offset (it->w, TEXT_AREA));
23967 if (EQ (prop, Qleft_margin))
23968 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23969 if (EQ (prop, Qright_margin))
23970 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23971 if (EQ (prop, Qscroll_bar))
23972 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23974 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23975 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23976 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23977 : 0)));
23979 else
23981 if (EQ (prop, Qleft_fringe))
23982 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23983 if (EQ (prop, Qright_fringe))
23984 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23985 if (EQ (prop, Qleft_margin))
23986 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23987 if (EQ (prop, Qright_margin))
23988 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23989 if (EQ (prop, Qscroll_bar))
23990 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23993 prop = buffer_local_value (prop, it->w->contents);
23994 if (EQ (prop, Qunbound))
23995 prop = Qnil;
23998 if (INTEGERP (prop) || FLOATP (prop))
24000 int base_unit = (width_p
24001 ? FRAME_COLUMN_WIDTH (it->f)
24002 : FRAME_LINE_HEIGHT (it->f));
24003 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24006 if (CONSP (prop))
24008 Lisp_Object car = XCAR (prop);
24009 Lisp_Object cdr = XCDR (prop);
24011 if (SYMBOLP (car))
24013 #ifdef HAVE_WINDOW_SYSTEM
24014 if (FRAME_WINDOW_P (it->f)
24015 && valid_image_p (prop))
24017 ptrdiff_t id = lookup_image (it->f, prop);
24018 struct image *img = IMAGE_FROM_ID (it->f, id);
24020 return OK_PIXELS (width_p ? img->width : img->height);
24022 #endif
24023 if (EQ (car, Qplus) || EQ (car, Qminus))
24025 bool first = true;
24026 double px;
24028 pixels = 0;
24029 while (CONSP (cdr))
24031 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24032 font, width_p, align_to))
24033 return false;
24034 if (first)
24035 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24036 else
24037 pixels += px;
24038 cdr = XCDR (cdr);
24040 if (EQ (car, Qminus))
24041 pixels = -pixels;
24042 return OK_PIXELS (pixels);
24045 car = buffer_local_value (car, it->w->contents);
24046 if (EQ (car, Qunbound))
24047 car = Qnil;
24050 if (INTEGERP (car) || FLOATP (car))
24052 double fact;
24053 pixels = XFLOATINT (car);
24054 if (NILP (cdr))
24055 return OK_PIXELS (pixels);
24056 if (calc_pixel_width_or_height (&fact, it, cdr,
24057 font, width_p, align_to))
24058 return OK_PIXELS (pixels * fact);
24059 return false;
24062 return false;
24065 return false;
24069 /***********************************************************************
24070 Glyph Display
24071 ***********************************************************************/
24073 #ifdef HAVE_WINDOW_SYSTEM
24075 #ifdef GLYPH_DEBUG
24077 void
24078 dump_glyph_string (struct glyph_string *s)
24080 fprintf (stderr, "glyph string\n");
24081 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24082 s->x, s->y, s->width, s->height);
24083 fprintf (stderr, " ybase = %d\n", s->ybase);
24084 fprintf (stderr, " hl = %d\n", s->hl);
24085 fprintf (stderr, " left overhang = %d, right = %d\n",
24086 s->left_overhang, s->right_overhang);
24087 fprintf (stderr, " nchars = %d\n", s->nchars);
24088 fprintf (stderr, " extends to end of line = %d\n",
24089 s->extends_to_end_of_line_p);
24090 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24091 fprintf (stderr, " bg width = %d\n", s->background_width);
24094 #endif /* GLYPH_DEBUG */
24096 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24097 of XChar2b structures for S; it can't be allocated in
24098 init_glyph_string because it must be allocated via `alloca'. W
24099 is the window on which S is drawn. ROW and AREA are the glyph row
24100 and area within the row from which S is constructed. START is the
24101 index of the first glyph structure covered by S. HL is a
24102 face-override for drawing S. */
24104 #ifdef HAVE_NTGUI
24105 #define OPTIONAL_HDC(hdc) HDC hdc,
24106 #define DECLARE_HDC(hdc) HDC hdc;
24107 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24108 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24109 #endif
24111 #ifndef OPTIONAL_HDC
24112 #define OPTIONAL_HDC(hdc)
24113 #define DECLARE_HDC(hdc)
24114 #define ALLOCATE_HDC(hdc, f)
24115 #define RELEASE_HDC(hdc, f)
24116 #endif
24118 static void
24119 init_glyph_string (struct glyph_string *s,
24120 OPTIONAL_HDC (hdc)
24121 XChar2b *char2b, struct window *w, struct glyph_row *row,
24122 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24124 memset (s, 0, sizeof *s);
24125 s->w = w;
24126 s->f = XFRAME (w->frame);
24127 #ifdef HAVE_NTGUI
24128 s->hdc = hdc;
24129 #endif
24130 s->display = FRAME_X_DISPLAY (s->f);
24131 s->window = FRAME_X_WINDOW (s->f);
24132 s->char2b = char2b;
24133 s->hl = hl;
24134 s->row = row;
24135 s->area = area;
24136 s->first_glyph = row->glyphs[area] + start;
24137 s->height = row->height;
24138 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24139 s->ybase = s->y + row->ascent;
24143 /* Append the list of glyph strings with head H and tail T to the list
24144 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24146 static void
24147 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24148 struct glyph_string *h, struct glyph_string *t)
24150 if (h)
24152 if (*head)
24153 (*tail)->next = h;
24154 else
24155 *head = h;
24156 h->prev = *tail;
24157 *tail = t;
24162 /* Prepend the list of glyph strings with head H and tail T to the
24163 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24164 result. */
24166 static void
24167 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24168 struct glyph_string *h, struct glyph_string *t)
24170 if (h)
24172 if (*head)
24173 (*head)->prev = t;
24174 else
24175 *tail = t;
24176 t->next = *head;
24177 *head = h;
24182 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24183 Set *HEAD and *TAIL to the resulting list. */
24185 static void
24186 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24187 struct glyph_string *s)
24189 s->next = s->prev = NULL;
24190 append_glyph_string_lists (head, tail, s, s);
24194 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24195 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24196 make sure that X resources for the face returned are allocated.
24197 Value is a pointer to a realized face that is ready for display if
24198 DISPLAY_P. */
24200 static struct face *
24201 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24202 XChar2b *char2b, bool display_p)
24204 struct face *face = FACE_FROM_ID (f, face_id);
24205 unsigned code = 0;
24207 if (face->font)
24209 code = face->font->driver->encode_char (face->font, c);
24211 if (code == FONT_INVALID_CODE)
24212 code = 0;
24214 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24216 /* Make sure X resources of the face are allocated. */
24217 #ifdef HAVE_X_WINDOWS
24218 if (display_p)
24219 #endif
24221 eassert (face != NULL);
24222 prepare_face_for_display (f, face);
24225 return face;
24229 /* Get face and two-byte form of character glyph GLYPH on frame F.
24230 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24231 a pointer to a realized face that is ready for display. */
24233 static struct face *
24234 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24235 XChar2b *char2b)
24237 struct face *face;
24238 unsigned code = 0;
24240 eassert (glyph->type == CHAR_GLYPH);
24241 face = FACE_FROM_ID (f, glyph->face_id);
24243 /* Make sure X resources of the face are allocated. */
24244 eassert (face != NULL);
24245 prepare_face_for_display (f, face);
24247 if (face->font)
24249 if (CHAR_BYTE8_P (glyph->u.ch))
24250 code = CHAR_TO_BYTE8 (glyph->u.ch);
24251 else
24252 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24254 if (code == FONT_INVALID_CODE)
24255 code = 0;
24258 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24259 return face;
24263 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24264 Return true iff FONT has a glyph for C. */
24266 static bool
24267 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24269 unsigned code;
24271 if (CHAR_BYTE8_P (c))
24272 code = CHAR_TO_BYTE8 (c);
24273 else
24274 code = font->driver->encode_char (font, c);
24276 if (code == FONT_INVALID_CODE)
24277 return false;
24278 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24279 return true;
24283 /* Fill glyph string S with composition components specified by S->cmp.
24285 BASE_FACE is the base face of the composition.
24286 S->cmp_from is the index of the first component for S.
24288 OVERLAPS non-zero means S should draw the foreground only, and use
24289 its physical height for clipping. See also draw_glyphs.
24291 Value is the index of a component not in S. */
24293 static int
24294 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24295 int overlaps)
24297 int i;
24298 /* For all glyphs of this composition, starting at the offset
24299 S->cmp_from, until we reach the end of the definition or encounter a
24300 glyph that requires the different face, add it to S. */
24301 struct face *face;
24303 eassert (s);
24305 s->for_overlaps = overlaps;
24306 s->face = NULL;
24307 s->font = NULL;
24308 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24310 int c = COMPOSITION_GLYPH (s->cmp, i);
24312 /* TAB in a composition means display glyphs with padding space
24313 on the left or right. */
24314 if (c != '\t')
24316 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24317 -1, Qnil);
24319 face = get_char_face_and_encoding (s->f, c, face_id,
24320 s->char2b + i, true);
24321 if (face)
24323 if (! s->face)
24325 s->face = face;
24326 s->font = s->face->font;
24328 else if (s->face != face)
24329 break;
24332 ++s->nchars;
24334 s->cmp_to = i;
24336 if (s->face == NULL)
24338 s->face = base_face->ascii_face;
24339 s->font = s->face->font;
24342 /* All glyph strings for the same composition has the same width,
24343 i.e. the width set for the first component of the composition. */
24344 s->width = s->first_glyph->pixel_width;
24346 /* If the specified font could not be loaded, use the frame's
24347 default font, but record the fact that we couldn't load it in
24348 the glyph string so that we can draw rectangles for the
24349 characters of the glyph string. */
24350 if (s->font == NULL)
24352 s->font_not_found_p = true;
24353 s->font = FRAME_FONT (s->f);
24356 /* Adjust base line for subscript/superscript text. */
24357 s->ybase += s->first_glyph->voffset;
24359 return s->cmp_to;
24362 static int
24363 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24364 int start, int end, int overlaps)
24366 struct glyph *glyph, *last;
24367 Lisp_Object lgstring;
24368 int i;
24370 s->for_overlaps = overlaps;
24371 glyph = s->row->glyphs[s->area] + start;
24372 last = s->row->glyphs[s->area] + end;
24373 s->cmp_id = glyph->u.cmp.id;
24374 s->cmp_from = glyph->slice.cmp.from;
24375 s->cmp_to = glyph->slice.cmp.to + 1;
24376 s->face = FACE_FROM_ID (s->f, face_id);
24377 lgstring = composition_gstring_from_id (s->cmp_id);
24378 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24379 glyph++;
24380 while (glyph < last
24381 && glyph->u.cmp.automatic
24382 && glyph->u.cmp.id == s->cmp_id
24383 && s->cmp_to == glyph->slice.cmp.from)
24384 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24386 for (i = s->cmp_from; i < s->cmp_to; i++)
24388 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24389 unsigned code = LGLYPH_CODE (lglyph);
24391 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24393 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24394 return glyph - s->row->glyphs[s->area];
24398 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24399 See the comment of fill_glyph_string for arguments.
24400 Value is the index of the first glyph not in S. */
24403 static int
24404 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24405 int start, int end, int overlaps)
24407 struct glyph *glyph, *last;
24408 int voffset;
24410 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
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->face = FACE_FROM_ID (s->f, face_id);
24416 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24417 s->nchars = 1;
24418 s->width = glyph->pixel_width;
24419 glyph++;
24420 while (glyph < last
24421 && glyph->type == GLYPHLESS_GLYPH
24422 && glyph->voffset == voffset
24423 && glyph->face_id == face_id)
24425 s->nchars++;
24426 s->width += glyph->pixel_width;
24427 glyph++;
24429 s->ybase += voffset;
24430 return glyph - s->row->glyphs[s->area];
24434 /* Fill glyph string S from a sequence of character glyphs.
24436 FACE_ID is the face id of the string. START is the index of the
24437 first glyph to consider, END is the index of the last + 1.
24438 OVERLAPS non-zero means S should draw the foreground only, and use
24439 its physical height for clipping. See also draw_glyphs.
24441 Value is the index of the first glyph not in S. */
24443 static int
24444 fill_glyph_string (struct glyph_string *s, int face_id,
24445 int start, int end, int overlaps)
24447 struct glyph *glyph, *last;
24448 int voffset;
24449 bool glyph_not_available_p;
24451 eassert (s->f == XFRAME (s->w->frame));
24452 eassert (s->nchars == 0);
24453 eassert (start >= 0 && end > start);
24455 s->for_overlaps = overlaps;
24456 glyph = s->row->glyphs[s->area] + start;
24457 last = s->row->glyphs[s->area] + end;
24458 voffset = glyph->voffset;
24459 s->padding_p = glyph->padding_p;
24460 glyph_not_available_p = glyph->glyph_not_available_p;
24462 while (glyph < last
24463 && glyph->type == CHAR_GLYPH
24464 && glyph->voffset == voffset
24465 /* Same face id implies same font, nowadays. */
24466 && glyph->face_id == face_id
24467 && glyph->glyph_not_available_p == glyph_not_available_p)
24469 s->face = get_glyph_face_and_encoding (s->f, glyph,
24470 s->char2b + s->nchars);
24471 ++s->nchars;
24472 eassert (s->nchars <= end - start);
24473 s->width += glyph->pixel_width;
24474 if (glyph++->padding_p != s->padding_p)
24475 break;
24478 s->font = s->face->font;
24480 /* If the specified font could not be loaded, use the frame's font,
24481 but record the fact that we couldn't load it in
24482 S->font_not_found_p so that we can draw rectangles for the
24483 characters of the glyph string. */
24484 if (s->font == NULL || glyph_not_available_p)
24486 s->font_not_found_p = true;
24487 s->font = FRAME_FONT (s->f);
24490 /* Adjust base line for subscript/superscript text. */
24491 s->ybase += voffset;
24493 eassert (s->face && s->face->gc);
24494 return glyph - s->row->glyphs[s->area];
24498 /* Fill glyph string S from image glyph S->first_glyph. */
24500 static void
24501 fill_image_glyph_string (struct glyph_string *s)
24503 eassert (s->first_glyph->type == IMAGE_GLYPH);
24504 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24505 eassert (s->img);
24506 s->slice = s->first_glyph->slice.img;
24507 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24508 s->font = s->face->font;
24509 s->width = s->first_glyph->pixel_width;
24511 /* Adjust base line for subscript/superscript text. */
24512 s->ybase += s->first_glyph->voffset;
24516 /* Fill glyph string S from a sequence of stretch glyphs.
24518 START is the index of the first glyph to consider,
24519 END is the index of the last + 1.
24521 Value is the index of the first glyph not in S. */
24523 static int
24524 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24526 struct glyph *glyph, *last;
24527 int voffset, face_id;
24529 eassert (s->first_glyph->type == STRETCH_GLYPH);
24531 glyph = s->row->glyphs[s->area] + start;
24532 last = s->row->glyphs[s->area] + end;
24533 face_id = glyph->face_id;
24534 s->face = FACE_FROM_ID (s->f, face_id);
24535 s->font = s->face->font;
24536 s->width = glyph->pixel_width;
24537 s->nchars = 1;
24538 voffset = glyph->voffset;
24540 for (++glyph;
24541 (glyph < last
24542 && glyph->type == STRETCH_GLYPH
24543 && glyph->voffset == voffset
24544 && glyph->face_id == face_id);
24545 ++glyph)
24546 s->width += glyph->pixel_width;
24548 /* Adjust base line for subscript/superscript text. */
24549 s->ybase += voffset;
24551 /* The case that face->gc == 0 is handled when drawing the glyph
24552 string by calling prepare_face_for_display. */
24553 eassert (s->face);
24554 return glyph - s->row->glyphs[s->area];
24557 static struct font_metrics *
24558 get_per_char_metric (struct font *font, XChar2b *char2b)
24560 static struct font_metrics metrics;
24561 unsigned code;
24563 if (! font)
24564 return NULL;
24565 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24566 if (code == FONT_INVALID_CODE)
24567 return NULL;
24568 font->driver->text_extents (font, &code, 1, &metrics);
24569 return &metrics;
24572 /* A subroutine that computes a reasonable "normal character height"
24573 for fonts that claim preposterously large vertical dimensions, but
24574 whose glyphs are actually reasonably sized. */
24575 static int
24576 normal_char_height (struct font *font)
24578 int default_height = FONT_HEIGHT (font);
24580 /* If the font claims too large height, use the metrics of the SPC
24581 character instead. Note that this could still fail to produce a
24582 better value if the font or the font driver don't support the
24583 functionality required by get_per_char_metric. */
24584 if (FONT_TOO_HIGH (font))
24586 XChar2b char2b;
24588 /* Get metrics of the SPC character. */
24589 if (get_char_glyph_code (' ', font, &char2b))
24591 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24593 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24594 default_height = pcm->ascent + pcm->descent;
24597 return default_height;
24600 /* A subroutine that computes "normal" values of ascent and descent
24601 for fonts that claim preposterously large values, but whose glyphs
24602 actually have reasonable dimensions. */
24603 static void
24604 normal_char_ascent_descent (struct font *font, int *ascent, int *descent)
24606 *ascent = FONT_BASE (font);
24607 *descent = FONT_DESCENT (font);
24609 if (FONT_TOO_HIGH (font))
24611 XChar2b char2b;
24613 /* Get metrics of a reasonably sized ASCII character. */
24614 if (get_char_glyph_code ('{', font, &char2b))
24616 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24618 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24620 *ascent = pcm->ascent;
24621 *descent = pcm->descent;
24627 /* EXPORT for RIF:
24628 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24629 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24630 assumed to be zero. */
24632 void
24633 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24635 *left = *right = 0;
24637 if (glyph->type == CHAR_GLYPH)
24639 XChar2b char2b;
24640 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24641 if (face->font)
24643 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24644 if (pcm)
24646 if (pcm->rbearing > pcm->width)
24647 *right = pcm->rbearing - pcm->width;
24648 if (pcm->lbearing < 0)
24649 *left = -pcm->lbearing;
24653 else if (glyph->type == COMPOSITE_GLYPH)
24655 if (! glyph->u.cmp.automatic)
24657 struct composition *cmp = composition_table[glyph->u.cmp.id];
24659 if (cmp->rbearing > cmp->pixel_width)
24660 *right = cmp->rbearing - cmp->pixel_width;
24661 if (cmp->lbearing < 0)
24662 *left = - cmp->lbearing;
24664 else
24666 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24667 struct font_metrics metrics;
24669 composition_gstring_width (gstring, glyph->slice.cmp.from,
24670 glyph->slice.cmp.to + 1, &metrics);
24671 if (metrics.rbearing > metrics.width)
24672 *right = metrics.rbearing - metrics.width;
24673 if (metrics.lbearing < 0)
24674 *left = - metrics.lbearing;
24680 /* Return the index of the first glyph preceding glyph string S that
24681 is overwritten by S because of S's left overhang. Value is -1
24682 if no glyphs are overwritten. */
24684 static int
24685 left_overwritten (struct glyph_string *s)
24687 int k;
24689 if (s->left_overhang)
24691 int x = 0, i;
24692 struct glyph *glyphs = s->row->glyphs[s->area];
24693 int first = s->first_glyph - glyphs;
24695 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24696 x -= glyphs[i].pixel_width;
24698 k = i + 1;
24700 else
24701 k = -1;
24703 return k;
24707 /* Return the index of the first glyph preceding glyph string S that
24708 is overwriting S because of its right overhang. Value is -1 if no
24709 glyph in front of S overwrites S. */
24711 static int
24712 left_overwriting (struct glyph_string *s)
24714 int i, k, x;
24715 struct glyph *glyphs = s->row->glyphs[s->area];
24716 int first = s->first_glyph - glyphs;
24718 k = -1;
24719 x = 0;
24720 for (i = first - 1; i >= 0; --i)
24722 int left, right;
24723 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24724 if (x + right > 0)
24725 k = i;
24726 x -= glyphs[i].pixel_width;
24729 return k;
24733 /* Return the index of the last glyph following glyph string S that is
24734 overwritten by S because of S's right overhang. Value is -1 if
24735 no such glyph is found. */
24737 static int
24738 right_overwritten (struct glyph_string *s)
24740 int k = -1;
24742 if (s->right_overhang)
24744 int x = 0, i;
24745 struct glyph *glyphs = s->row->glyphs[s->area];
24746 int first = (s->first_glyph - glyphs
24747 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24748 int end = s->row->used[s->area];
24750 for (i = first; i < end && s->right_overhang > x; ++i)
24751 x += glyphs[i].pixel_width;
24753 k = i;
24756 return k;
24760 /* Return the index of the last glyph following glyph string S that
24761 overwrites S because of its left overhang. Value is negative
24762 if no such glyph is found. */
24764 static int
24765 right_overwriting (struct glyph_string *s)
24767 int i, k, x;
24768 int end = s->row->used[s->area];
24769 struct glyph *glyphs = s->row->glyphs[s->area];
24770 int first = (s->first_glyph - glyphs
24771 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24773 k = -1;
24774 x = 0;
24775 for (i = first; i < end; ++i)
24777 int left, right;
24778 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24779 if (x - left < 0)
24780 k = i;
24781 x += glyphs[i].pixel_width;
24784 return k;
24788 /* Set background width of glyph string S. START is the index of the
24789 first glyph following S. LAST_X is the right-most x-position + 1
24790 in the drawing area. */
24792 static void
24793 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24795 /* If the face of this glyph string has to be drawn to the end of
24796 the drawing area, set S->extends_to_end_of_line_p. */
24798 if (start == s->row->used[s->area]
24799 && ((s->row->fill_line_p
24800 && (s->hl == DRAW_NORMAL_TEXT
24801 || s->hl == DRAW_IMAGE_RAISED
24802 || s->hl == DRAW_IMAGE_SUNKEN))
24803 || s->hl == DRAW_MOUSE_FACE))
24804 s->extends_to_end_of_line_p = true;
24806 /* If S extends its face to the end of the line, set its
24807 background_width to the distance to the right edge of the drawing
24808 area. */
24809 if (s->extends_to_end_of_line_p)
24810 s->background_width = last_x - s->x + 1;
24811 else
24812 s->background_width = s->width;
24816 /* Compute overhangs and x-positions for glyph string S and its
24817 predecessors, or successors. X is the starting x-position for S.
24818 BACKWARD_P means process predecessors. */
24820 static void
24821 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24823 if (backward_p)
24825 while (s)
24827 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24828 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24829 x -= s->width;
24830 s->x = x;
24831 s = s->prev;
24834 else
24836 while (s)
24838 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24839 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24840 s->x = x;
24841 x += s->width;
24842 s = s->next;
24849 /* The following macros are only called from draw_glyphs below.
24850 They reference the following parameters of that function directly:
24851 `w', `row', `area', and `overlap_p'
24852 as well as the following local variables:
24853 `s', `f', and `hdc' (in W32) */
24855 #ifdef HAVE_NTGUI
24856 /* On W32, silently add local `hdc' variable to argument list of
24857 init_glyph_string. */
24858 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24859 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24860 #else
24861 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24862 init_glyph_string (s, char2b, w, row, area, start, hl)
24863 #endif
24865 /* Add a glyph string for a stretch glyph to the list of strings
24866 between HEAD and TAIL. START is the index of the stretch glyph in
24867 row area AREA of glyph row ROW. END is the index of the last glyph
24868 in that glyph row area. X is the current output position assigned
24869 to the new glyph string constructed. HL overrides that face of the
24870 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24871 is the right-most x-position of the drawing area. */
24873 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24874 and below -- keep them on one line. */
24875 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24876 do \
24878 s = alloca (sizeof *s); \
24879 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24880 START = fill_stretch_glyph_string (s, START, END); \
24881 append_glyph_string (&HEAD, &TAIL, s); \
24882 s->x = (X); \
24884 while (false)
24887 /* Add a glyph string for an image glyph to the list of strings
24888 between HEAD and TAIL. START is the index of the image glyph in
24889 row area AREA of glyph row ROW. END is the index of the last glyph
24890 in that glyph row area. X is the current output position assigned
24891 to the new glyph string constructed. HL overrides that face of the
24892 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24893 is the right-most x-position of the drawing area. */
24895 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24896 do \
24898 s = alloca (sizeof *s); \
24899 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24900 fill_image_glyph_string (s); \
24901 append_glyph_string (&HEAD, &TAIL, s); \
24902 ++START; \
24903 s->x = (X); \
24905 while (false)
24908 /* Add a glyph string for a sequence of character glyphs to the list
24909 of strings between HEAD and TAIL. START is the index of the first
24910 glyph in row area AREA of glyph row ROW that is part of the new
24911 glyph string. END is the index of the last glyph in that glyph row
24912 area. X is the current output position assigned to the new glyph
24913 string constructed. HL overrides that face of the glyph; e.g. it
24914 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24915 right-most x-position of the drawing area. */
24917 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24918 do \
24920 int face_id; \
24921 XChar2b *char2b; \
24923 face_id = (row)->glyphs[area][START].face_id; \
24925 s = alloca (sizeof *s); \
24926 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24927 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24928 append_glyph_string (&HEAD, &TAIL, s); \
24929 s->x = (X); \
24930 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24932 while (false)
24935 /* Add a glyph string for a composite sequence to the list of strings
24936 between HEAD and TAIL. START is the index of the first glyph in
24937 row area AREA of glyph row ROW that is part of the new glyph
24938 string. END is the index of the last glyph in that glyph row area.
24939 X is the current output position assigned to the new glyph string
24940 constructed. HL overrides that face of the glyph; e.g. it is
24941 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24942 x-position of the drawing area. */
24944 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24945 do { \
24946 int face_id = (row)->glyphs[area][START].face_id; \
24947 struct face *base_face = FACE_FROM_ID (f, face_id); \
24948 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24949 struct composition *cmp = composition_table[cmp_id]; \
24950 XChar2b *char2b; \
24951 struct glyph_string *first_s = NULL; \
24952 int n; \
24954 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24956 /* Make glyph_strings for each glyph sequence that is drawable by \
24957 the same face, and append them to HEAD/TAIL. */ \
24958 for (n = 0; n < cmp->glyph_len;) \
24960 s = alloca (sizeof *s); \
24961 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24962 append_glyph_string (&(HEAD), &(TAIL), s); \
24963 s->cmp = cmp; \
24964 s->cmp_from = n; \
24965 s->x = (X); \
24966 if (n == 0) \
24967 first_s = s; \
24968 n = fill_composite_glyph_string (s, base_face, overlaps); \
24971 ++START; \
24972 s = first_s; \
24973 } while (false)
24976 /* Add a glyph string for a glyph-string sequence to the list of strings
24977 between HEAD and TAIL. */
24979 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24980 do { \
24981 int face_id; \
24982 XChar2b *char2b; \
24983 Lisp_Object gstring; \
24985 face_id = (row)->glyphs[area][START].face_id; \
24986 gstring = (composition_gstring_from_id \
24987 ((row)->glyphs[area][START].u.cmp.id)); \
24988 s = alloca (sizeof *s); \
24989 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24990 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24991 append_glyph_string (&(HEAD), &(TAIL), s); \
24992 s->x = (X); \
24993 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24994 } while (false)
24997 /* Add a glyph string for a sequence of glyphless character's glyphs
24998 to the list of strings between HEAD and TAIL. The meanings of
24999 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25001 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25002 do \
25004 int face_id; \
25006 face_id = (row)->glyphs[area][START].face_id; \
25008 s = alloca (sizeof *s); \
25009 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25010 append_glyph_string (&HEAD, &TAIL, s); \
25011 s->x = (X); \
25012 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25013 overlaps); \
25015 while (false)
25018 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25019 of AREA of glyph row ROW on window W between indices START and END.
25020 HL overrides the face for drawing glyph strings, e.g. it is
25021 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25022 x-positions of the drawing area.
25024 This is an ugly monster macro construct because we must use alloca
25025 to allocate glyph strings (because draw_glyphs can be called
25026 asynchronously). */
25028 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25029 do \
25031 HEAD = TAIL = NULL; \
25032 while (START < END) \
25034 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25035 switch (first_glyph->type) \
25037 case CHAR_GLYPH: \
25038 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25039 HL, X, LAST_X); \
25040 break; \
25042 case COMPOSITE_GLYPH: \
25043 if (first_glyph->u.cmp.automatic) \
25044 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25045 HL, X, LAST_X); \
25046 else \
25047 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25048 HL, X, LAST_X); \
25049 break; \
25051 case STRETCH_GLYPH: \
25052 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25053 HL, X, LAST_X); \
25054 break; \
25056 case IMAGE_GLYPH: \
25057 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25058 HL, X, LAST_X); \
25059 break; \
25061 case GLYPHLESS_GLYPH: \
25062 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25063 HL, X, LAST_X); \
25064 break; \
25066 default: \
25067 emacs_abort (); \
25070 if (s) \
25072 set_glyph_string_background_width (s, START, LAST_X); \
25073 (X) += s->width; \
25076 } while (false)
25079 /* Draw glyphs between START and END in AREA of ROW on window W,
25080 starting at x-position X. X is relative to AREA in W. HL is a
25081 face-override with the following meaning:
25083 DRAW_NORMAL_TEXT draw normally
25084 DRAW_CURSOR draw in cursor face
25085 DRAW_MOUSE_FACE draw in mouse face.
25086 DRAW_INVERSE_VIDEO draw in mode line face
25087 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25088 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25090 If OVERLAPS is non-zero, draw only the foreground of characters and
25091 clip to the physical height of ROW. Non-zero value also defines
25092 the overlapping part to be drawn:
25094 OVERLAPS_PRED overlap with preceding rows
25095 OVERLAPS_SUCC overlap with succeeding rows
25096 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25097 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25099 Value is the x-position reached, relative to AREA of W. */
25101 static int
25102 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25103 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25104 enum draw_glyphs_face hl, int overlaps)
25106 struct glyph_string *head, *tail;
25107 struct glyph_string *s;
25108 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25109 int i, j, x_reached, last_x, area_left = 0;
25110 struct frame *f = XFRAME (WINDOW_FRAME (w));
25111 DECLARE_HDC (hdc);
25113 ALLOCATE_HDC (hdc, f);
25115 /* Let's rather be paranoid than getting a SEGV. */
25116 end = min (end, row->used[area]);
25117 start = clip_to_bounds (0, start, end);
25119 /* Translate X to frame coordinates. Set last_x to the right
25120 end of the drawing area. */
25121 if (row->full_width_p)
25123 /* X is relative to the left edge of W, without scroll bars
25124 or fringes. */
25125 area_left = WINDOW_LEFT_EDGE_X (w);
25126 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25127 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25129 else
25131 area_left = window_box_left (w, area);
25132 last_x = area_left + window_box_width (w, area);
25134 x += area_left;
25136 /* Build a doubly-linked list of glyph_string structures between
25137 head and tail from what we have to draw. Note that the macro
25138 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25139 the reason we use a separate variable `i'. */
25140 i = start;
25141 USE_SAFE_ALLOCA;
25142 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25143 if (tail)
25144 x_reached = tail->x + tail->background_width;
25145 else
25146 x_reached = x;
25148 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25149 the row, redraw some glyphs in front or following the glyph
25150 strings built above. */
25151 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25153 struct glyph_string *h, *t;
25154 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25155 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25156 bool check_mouse_face = false;
25157 int dummy_x = 0;
25159 /* If mouse highlighting is on, we may need to draw adjacent
25160 glyphs using mouse-face highlighting. */
25161 if (area == TEXT_AREA && row->mouse_face_p
25162 && hlinfo->mouse_face_beg_row >= 0
25163 && hlinfo->mouse_face_end_row >= 0)
25165 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25167 if (row_vpos >= hlinfo->mouse_face_beg_row
25168 && row_vpos <= hlinfo->mouse_face_end_row)
25170 check_mouse_face = true;
25171 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25172 ? hlinfo->mouse_face_beg_col : 0;
25173 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25174 ? hlinfo->mouse_face_end_col
25175 : row->used[TEXT_AREA];
25179 /* Compute overhangs for all glyph strings. */
25180 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25181 for (s = head; s; s = s->next)
25182 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25184 /* Prepend glyph strings for glyphs in front of the first glyph
25185 string that are overwritten because of the first glyph
25186 string's left overhang. The background of all strings
25187 prepended must be drawn because the first glyph string
25188 draws over it. */
25189 i = left_overwritten (head);
25190 if (i >= 0)
25192 enum draw_glyphs_face overlap_hl;
25194 /* If this row contains mouse highlighting, attempt to draw
25195 the overlapped glyphs with the correct highlight. This
25196 code fails if the overlap encompasses more than one glyph
25197 and mouse-highlight spans only some of these glyphs.
25198 However, making it work perfectly involves a lot more
25199 code, and I don't know if the pathological case occurs in
25200 practice, so we'll stick to this for now. --- cyd */
25201 if (check_mouse_face
25202 && mouse_beg_col < start && mouse_end_col > i)
25203 overlap_hl = DRAW_MOUSE_FACE;
25204 else
25205 overlap_hl = DRAW_NORMAL_TEXT;
25207 if (hl != overlap_hl)
25208 clip_head = head;
25209 j = i;
25210 BUILD_GLYPH_STRINGS (j, start, h, t,
25211 overlap_hl, dummy_x, last_x);
25212 start = i;
25213 compute_overhangs_and_x (t, head->x, true);
25214 prepend_glyph_string_lists (&head, &tail, h, t);
25215 if (clip_head == NULL)
25216 clip_head = head;
25219 /* Prepend glyph strings for glyphs in front of the first glyph
25220 string that overwrite that glyph string because of their
25221 right overhang. For these strings, only the foreground must
25222 be drawn, because it draws over the glyph string at `head'.
25223 The background must not be drawn because this would overwrite
25224 right overhangs of preceding glyphs for which no glyph
25225 strings exist. */
25226 i = left_overwriting (head);
25227 if (i >= 0)
25229 enum draw_glyphs_face overlap_hl;
25231 if (check_mouse_face
25232 && mouse_beg_col < start && mouse_end_col > i)
25233 overlap_hl = DRAW_MOUSE_FACE;
25234 else
25235 overlap_hl = DRAW_NORMAL_TEXT;
25237 if (hl == overlap_hl || clip_head == NULL)
25238 clip_head = head;
25239 BUILD_GLYPH_STRINGS (i, start, h, t,
25240 overlap_hl, dummy_x, last_x);
25241 for (s = h; s; s = s->next)
25242 s->background_filled_p = true;
25243 compute_overhangs_and_x (t, head->x, true);
25244 prepend_glyph_string_lists (&head, &tail, h, t);
25247 /* Append glyphs strings for glyphs following the last glyph
25248 string tail that are overwritten by tail. The background of
25249 these strings has to be drawn because tail's foreground draws
25250 over it. */
25251 i = right_overwritten (tail);
25252 if (i >= 0)
25254 enum draw_glyphs_face overlap_hl;
25256 if (check_mouse_face
25257 && mouse_beg_col < i && mouse_end_col > end)
25258 overlap_hl = DRAW_MOUSE_FACE;
25259 else
25260 overlap_hl = DRAW_NORMAL_TEXT;
25262 if (hl != overlap_hl)
25263 clip_tail = tail;
25264 BUILD_GLYPH_STRINGS (end, i, h, t,
25265 overlap_hl, x, last_x);
25266 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25267 we don't have `end = i;' here. */
25268 compute_overhangs_and_x (h, tail->x + tail->width, false);
25269 append_glyph_string_lists (&head, &tail, h, t);
25270 if (clip_tail == NULL)
25271 clip_tail = tail;
25274 /* Append glyph strings for glyphs following the last glyph
25275 string tail that overwrite tail. The foreground of such
25276 glyphs has to be drawn because it writes into the background
25277 of tail. The background must not be drawn because it could
25278 paint over the foreground of following glyphs. */
25279 i = right_overwriting (tail);
25280 if (i >= 0)
25282 enum draw_glyphs_face overlap_hl;
25283 if (check_mouse_face
25284 && mouse_beg_col < i && mouse_end_col > end)
25285 overlap_hl = DRAW_MOUSE_FACE;
25286 else
25287 overlap_hl = DRAW_NORMAL_TEXT;
25289 if (hl == overlap_hl || clip_tail == NULL)
25290 clip_tail = tail;
25291 i++; /* We must include the Ith glyph. */
25292 BUILD_GLYPH_STRINGS (end, i, h, t,
25293 overlap_hl, x, last_x);
25294 for (s = h; s; s = s->next)
25295 s->background_filled_p = true;
25296 compute_overhangs_and_x (h, tail->x + tail->width, false);
25297 append_glyph_string_lists (&head, &tail, h, t);
25299 if (clip_head || clip_tail)
25300 for (s = head; s; s = s->next)
25302 s->clip_head = clip_head;
25303 s->clip_tail = clip_tail;
25307 /* Draw all strings. */
25308 for (s = head; s; s = s->next)
25309 FRAME_RIF (f)->draw_glyph_string (s);
25311 #ifndef HAVE_NS
25312 /* When focus a sole frame and move horizontally, this clears on_p
25313 causing a failure to erase prev cursor position. */
25314 if (area == TEXT_AREA
25315 && !row->full_width_p
25316 /* When drawing overlapping rows, only the glyph strings'
25317 foreground is drawn, which doesn't erase a cursor
25318 completely. */
25319 && !overlaps)
25321 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25322 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25323 : (tail ? tail->x + tail->background_width : x));
25324 x0 -= area_left;
25325 x1 -= area_left;
25327 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25328 row->y, MATRIX_ROW_BOTTOM_Y (row));
25330 #endif
25332 /* Value is the x-position up to which drawn, relative to AREA of W.
25333 This doesn't include parts drawn because of overhangs. */
25334 if (row->full_width_p)
25335 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25336 else
25337 x_reached -= area_left;
25339 RELEASE_HDC (hdc, f);
25341 SAFE_FREE ();
25342 return x_reached;
25345 /* Expand row matrix if too narrow. Don't expand if area
25346 is not present. */
25348 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25350 if (!it->f->fonts_changed \
25351 && (it->glyph_row->glyphs[area] \
25352 < it->glyph_row->glyphs[area + 1])) \
25354 it->w->ncols_scale_factor++; \
25355 it->f->fonts_changed = true; \
25359 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25360 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25362 static void
25363 append_glyph (struct it *it)
25365 struct glyph *glyph;
25366 enum glyph_row_area area = it->area;
25368 eassert (it->glyph_row);
25369 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25371 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25372 if (glyph < it->glyph_row->glyphs[area + 1])
25374 /* If the glyph row is reversed, we need to prepend the glyph
25375 rather than append it. */
25376 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25378 struct glyph *g;
25380 /* Make room for the additional glyph. */
25381 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25382 g[1] = *g;
25383 glyph = it->glyph_row->glyphs[area];
25385 glyph->charpos = CHARPOS (it->position);
25386 glyph->object = it->object;
25387 if (it->pixel_width > 0)
25389 glyph->pixel_width = it->pixel_width;
25390 glyph->padding_p = false;
25392 else
25394 /* Assure at least 1-pixel width. Otherwise, cursor can't
25395 be displayed correctly. */
25396 glyph->pixel_width = 1;
25397 glyph->padding_p = true;
25399 glyph->ascent = it->ascent;
25400 glyph->descent = it->descent;
25401 glyph->voffset = it->voffset;
25402 glyph->type = CHAR_GLYPH;
25403 glyph->avoid_cursor_p = it->avoid_cursor_p;
25404 glyph->multibyte_p = it->multibyte_p;
25405 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25407 /* In R2L rows, the left and the right box edges need to be
25408 drawn in reverse direction. */
25409 glyph->right_box_line_p = it->start_of_box_run_p;
25410 glyph->left_box_line_p = it->end_of_box_run_p;
25412 else
25414 glyph->left_box_line_p = it->start_of_box_run_p;
25415 glyph->right_box_line_p = it->end_of_box_run_p;
25417 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25418 || it->phys_descent > it->descent);
25419 glyph->glyph_not_available_p = it->glyph_not_available_p;
25420 glyph->face_id = it->face_id;
25421 glyph->u.ch = it->char_to_display;
25422 glyph->slice.img = null_glyph_slice;
25423 glyph->font_type = FONT_TYPE_UNKNOWN;
25424 if (it->bidi_p)
25426 glyph->resolved_level = it->bidi_it.resolved_level;
25427 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25428 glyph->bidi_type = it->bidi_it.type;
25430 else
25432 glyph->resolved_level = 0;
25433 glyph->bidi_type = UNKNOWN_BT;
25435 ++it->glyph_row->used[area];
25437 else
25438 IT_EXPAND_MATRIX_WIDTH (it, area);
25441 /* Store one glyph for the composition IT->cmp_it.id in
25442 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25443 non-null. */
25445 static void
25446 append_composite_glyph (struct it *it)
25448 struct glyph *glyph;
25449 enum glyph_row_area area = it->area;
25451 eassert (it->glyph_row);
25453 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25454 if (glyph < it->glyph_row->glyphs[area + 1])
25456 /* If the glyph row is reversed, we need to prepend the glyph
25457 rather than append it. */
25458 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25460 struct glyph *g;
25462 /* Make room for the new glyph. */
25463 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25464 g[1] = *g;
25465 glyph = it->glyph_row->glyphs[it->area];
25467 glyph->charpos = it->cmp_it.charpos;
25468 glyph->object = it->object;
25469 glyph->pixel_width = it->pixel_width;
25470 glyph->ascent = it->ascent;
25471 glyph->descent = it->descent;
25472 glyph->voffset = it->voffset;
25473 glyph->type = COMPOSITE_GLYPH;
25474 if (it->cmp_it.ch < 0)
25476 glyph->u.cmp.automatic = false;
25477 glyph->u.cmp.id = it->cmp_it.id;
25478 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25480 else
25482 glyph->u.cmp.automatic = true;
25483 glyph->u.cmp.id = it->cmp_it.id;
25484 glyph->slice.cmp.from = it->cmp_it.from;
25485 glyph->slice.cmp.to = it->cmp_it.to - 1;
25487 glyph->avoid_cursor_p = it->avoid_cursor_p;
25488 glyph->multibyte_p = it->multibyte_p;
25489 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25491 /* In R2L rows, the left and the right box edges need to be
25492 drawn in reverse direction. */
25493 glyph->right_box_line_p = it->start_of_box_run_p;
25494 glyph->left_box_line_p = it->end_of_box_run_p;
25496 else
25498 glyph->left_box_line_p = it->start_of_box_run_p;
25499 glyph->right_box_line_p = it->end_of_box_run_p;
25501 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25502 || it->phys_descent > it->descent);
25503 glyph->padding_p = false;
25504 glyph->glyph_not_available_p = false;
25505 glyph->face_id = it->face_id;
25506 glyph->font_type = FONT_TYPE_UNKNOWN;
25507 if (it->bidi_p)
25509 glyph->resolved_level = it->bidi_it.resolved_level;
25510 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25511 glyph->bidi_type = it->bidi_it.type;
25513 ++it->glyph_row->used[area];
25515 else
25516 IT_EXPAND_MATRIX_WIDTH (it, area);
25520 /* Change IT->ascent and IT->height according to the setting of
25521 IT->voffset. */
25523 static void
25524 take_vertical_position_into_account (struct it *it)
25526 if (it->voffset)
25528 if (it->voffset < 0)
25529 /* Increase the ascent so that we can display the text higher
25530 in the line. */
25531 it->ascent -= it->voffset;
25532 else
25533 /* Increase the descent so that we can display the text lower
25534 in the line. */
25535 it->descent += it->voffset;
25540 /* Produce glyphs/get display metrics for the image IT is loaded with.
25541 See the description of struct display_iterator in dispextern.h for
25542 an overview of struct display_iterator. */
25544 static void
25545 produce_image_glyph (struct it *it)
25547 struct image *img;
25548 struct face *face;
25549 int glyph_ascent, crop;
25550 struct glyph_slice slice;
25552 eassert (it->what == IT_IMAGE);
25554 face = FACE_FROM_ID (it->f, it->face_id);
25555 eassert (face);
25556 /* Make sure X resources of the face is loaded. */
25557 prepare_face_for_display (it->f, face);
25559 if (it->image_id < 0)
25561 /* Fringe bitmap. */
25562 it->ascent = it->phys_ascent = 0;
25563 it->descent = it->phys_descent = 0;
25564 it->pixel_width = 0;
25565 it->nglyphs = 0;
25566 return;
25569 img = IMAGE_FROM_ID (it->f, it->image_id);
25570 eassert (img);
25571 /* Make sure X resources of the image is loaded. */
25572 prepare_image_for_display (it->f, img);
25574 slice.x = slice.y = 0;
25575 slice.width = img->width;
25576 slice.height = img->height;
25578 if (INTEGERP (it->slice.x))
25579 slice.x = XINT (it->slice.x);
25580 else if (FLOATP (it->slice.x))
25581 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25583 if (INTEGERP (it->slice.y))
25584 slice.y = XINT (it->slice.y);
25585 else if (FLOATP (it->slice.y))
25586 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25588 if (INTEGERP (it->slice.width))
25589 slice.width = XINT (it->slice.width);
25590 else if (FLOATP (it->slice.width))
25591 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25593 if (INTEGERP (it->slice.height))
25594 slice.height = XINT (it->slice.height);
25595 else if (FLOATP (it->slice.height))
25596 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25598 if (slice.x >= img->width)
25599 slice.x = img->width;
25600 if (slice.y >= img->height)
25601 slice.y = img->height;
25602 if (slice.x + slice.width >= img->width)
25603 slice.width = img->width - slice.x;
25604 if (slice.y + slice.height > img->height)
25605 slice.height = img->height - slice.y;
25607 if (slice.width == 0 || slice.height == 0)
25608 return;
25610 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25612 it->descent = slice.height - glyph_ascent;
25613 if (slice.y == 0)
25614 it->descent += img->vmargin;
25615 if (slice.y + slice.height == img->height)
25616 it->descent += img->vmargin;
25617 it->phys_descent = it->descent;
25619 it->pixel_width = slice.width;
25620 if (slice.x == 0)
25621 it->pixel_width += img->hmargin;
25622 if (slice.x + slice.width == img->width)
25623 it->pixel_width += img->hmargin;
25625 /* It's quite possible for images to have an ascent greater than
25626 their height, so don't get confused in that case. */
25627 if (it->descent < 0)
25628 it->descent = 0;
25630 it->nglyphs = 1;
25632 if (face->box != FACE_NO_BOX)
25634 if (face->box_line_width > 0)
25636 if (slice.y == 0)
25637 it->ascent += face->box_line_width;
25638 if (slice.y + slice.height == img->height)
25639 it->descent += face->box_line_width;
25642 if (it->start_of_box_run_p && slice.x == 0)
25643 it->pixel_width += eabs (face->box_line_width);
25644 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25645 it->pixel_width += eabs (face->box_line_width);
25648 take_vertical_position_into_account (it);
25650 /* Automatically crop wide image glyphs at right edge so we can
25651 draw the cursor on same display row. */
25652 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25653 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25655 it->pixel_width -= crop;
25656 slice.width -= crop;
25659 if (it->glyph_row)
25661 struct glyph *glyph;
25662 enum glyph_row_area area = it->area;
25664 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25665 if (it->glyph_row->reversed_p)
25667 struct glyph *g;
25669 /* Make room for the new glyph. */
25670 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25671 g[1] = *g;
25672 glyph = it->glyph_row->glyphs[it->area];
25674 if (glyph < it->glyph_row->glyphs[area + 1])
25676 glyph->charpos = CHARPOS (it->position);
25677 glyph->object = it->object;
25678 glyph->pixel_width = it->pixel_width;
25679 glyph->ascent = glyph_ascent;
25680 glyph->descent = it->descent;
25681 glyph->voffset = it->voffset;
25682 glyph->type = IMAGE_GLYPH;
25683 glyph->avoid_cursor_p = it->avoid_cursor_p;
25684 glyph->multibyte_p = it->multibyte_p;
25685 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25687 /* In R2L rows, the left and the right box edges need to be
25688 drawn in reverse direction. */
25689 glyph->right_box_line_p = it->start_of_box_run_p;
25690 glyph->left_box_line_p = it->end_of_box_run_p;
25692 else
25694 glyph->left_box_line_p = it->start_of_box_run_p;
25695 glyph->right_box_line_p = it->end_of_box_run_p;
25697 glyph->overlaps_vertically_p = false;
25698 glyph->padding_p = false;
25699 glyph->glyph_not_available_p = false;
25700 glyph->face_id = it->face_id;
25701 glyph->u.img_id = img->id;
25702 glyph->slice.img = slice;
25703 glyph->font_type = FONT_TYPE_UNKNOWN;
25704 if (it->bidi_p)
25706 glyph->resolved_level = it->bidi_it.resolved_level;
25707 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25708 glyph->bidi_type = it->bidi_it.type;
25710 ++it->glyph_row->used[area];
25712 else
25713 IT_EXPAND_MATRIX_WIDTH (it, area);
25718 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25719 of the glyph, WIDTH and HEIGHT are the width and height of the
25720 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25722 static void
25723 append_stretch_glyph (struct it *it, Lisp_Object object,
25724 int width, int height, int ascent)
25726 struct glyph *glyph;
25727 enum glyph_row_area area = it->area;
25729 eassert (ascent >= 0 && ascent <= height);
25731 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25732 if (glyph < it->glyph_row->glyphs[area + 1])
25734 /* If the glyph row is reversed, we need to prepend the glyph
25735 rather than append it. */
25736 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25738 struct glyph *g;
25740 /* Make room for the additional glyph. */
25741 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25742 g[1] = *g;
25743 glyph = it->glyph_row->glyphs[area];
25745 /* Decrease the width of the first glyph of the row that
25746 begins before first_visible_x (e.g., due to hscroll).
25747 This is so the overall width of the row becomes smaller
25748 by the scroll amount, and the stretch glyph appended by
25749 extend_face_to_end_of_line will be wider, to shift the
25750 row glyphs to the right. (In L2R rows, the corresponding
25751 left-shift effect is accomplished by setting row->x to a
25752 negative value, which won't work with R2L rows.)
25754 This must leave us with a positive value of WIDTH, since
25755 otherwise the call to move_it_in_display_line_to at the
25756 beginning of display_line would have got past the entire
25757 first glyph, and then it->current_x would have been
25758 greater or equal to it->first_visible_x. */
25759 if (it->current_x < it->first_visible_x)
25760 width -= it->first_visible_x - it->current_x;
25761 eassert (width > 0);
25763 glyph->charpos = CHARPOS (it->position);
25764 glyph->object = object;
25765 glyph->pixel_width = width;
25766 glyph->ascent = ascent;
25767 glyph->descent = height - ascent;
25768 glyph->voffset = it->voffset;
25769 glyph->type = STRETCH_GLYPH;
25770 glyph->avoid_cursor_p = it->avoid_cursor_p;
25771 glyph->multibyte_p = it->multibyte_p;
25772 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25774 /* In R2L rows, the left and the right box edges need to be
25775 drawn in reverse direction. */
25776 glyph->right_box_line_p = it->start_of_box_run_p;
25777 glyph->left_box_line_p = it->end_of_box_run_p;
25779 else
25781 glyph->left_box_line_p = it->start_of_box_run_p;
25782 glyph->right_box_line_p = it->end_of_box_run_p;
25784 glyph->overlaps_vertically_p = false;
25785 glyph->padding_p = false;
25786 glyph->glyph_not_available_p = false;
25787 glyph->face_id = it->face_id;
25788 glyph->u.stretch.ascent = ascent;
25789 glyph->u.stretch.height = height;
25790 glyph->slice.img = null_glyph_slice;
25791 glyph->font_type = FONT_TYPE_UNKNOWN;
25792 if (it->bidi_p)
25794 glyph->resolved_level = it->bidi_it.resolved_level;
25795 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25796 glyph->bidi_type = it->bidi_it.type;
25798 else
25800 glyph->resolved_level = 0;
25801 glyph->bidi_type = UNKNOWN_BT;
25803 ++it->glyph_row->used[area];
25805 else
25806 IT_EXPAND_MATRIX_WIDTH (it, area);
25809 #endif /* HAVE_WINDOW_SYSTEM */
25811 /* Produce a stretch glyph for iterator IT. IT->object is the value
25812 of the glyph property displayed. The value must be a list
25813 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25814 being recognized:
25816 1. `:width WIDTH' specifies that the space should be WIDTH *
25817 canonical char width wide. WIDTH may be an integer or floating
25818 point number.
25820 2. `:relative-width FACTOR' specifies that the width of the stretch
25821 should be computed from the width of the first character having the
25822 `glyph' property, and should be FACTOR times that width.
25824 3. `:align-to HPOS' specifies that the space should be wide enough
25825 to reach HPOS, a value in canonical character units.
25827 Exactly one of the above pairs must be present.
25829 4. `:height HEIGHT' specifies that the height of the stretch produced
25830 should be HEIGHT, measured in canonical character units.
25832 5. `:relative-height FACTOR' specifies that the height of the
25833 stretch should be FACTOR times the height of the characters having
25834 the glyph property.
25836 Either none or exactly one of 4 or 5 must be present.
25838 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25839 of the stretch should be used for the ascent of the stretch.
25840 ASCENT must be in the range 0 <= ASCENT <= 100. */
25842 void
25843 produce_stretch_glyph (struct it *it)
25845 /* (space :width WIDTH :height HEIGHT ...) */
25846 Lisp_Object prop, plist;
25847 int width = 0, height = 0, align_to = -1;
25848 bool zero_width_ok_p = false;
25849 double tem;
25850 struct font *font = NULL;
25852 #ifdef HAVE_WINDOW_SYSTEM
25853 int ascent = 0;
25854 bool zero_height_ok_p = false;
25856 if (FRAME_WINDOW_P (it->f))
25858 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25859 font = face->font ? face->font : FRAME_FONT (it->f);
25860 prepare_face_for_display (it->f, face);
25862 #endif
25864 /* List should start with `space'. */
25865 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25866 plist = XCDR (it->object);
25868 /* Compute the width of the stretch. */
25869 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25870 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
25872 /* Absolute width `:width WIDTH' specified and valid. */
25873 zero_width_ok_p = true;
25874 width = (int)tem;
25876 #ifdef HAVE_WINDOW_SYSTEM
25877 else if (FRAME_WINDOW_P (it->f)
25878 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25880 /* Relative width `:relative-width FACTOR' specified and valid.
25881 Compute the width of the characters having the `glyph'
25882 property. */
25883 struct it it2;
25884 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25886 it2 = *it;
25887 if (it->multibyte_p)
25888 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25889 else
25891 it2.c = it2.char_to_display = *p, it2.len = 1;
25892 if (! ASCII_CHAR_P (it2.c))
25893 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25896 it2.glyph_row = NULL;
25897 it2.what = IT_CHARACTER;
25898 x_produce_glyphs (&it2);
25899 width = NUMVAL (prop) * it2.pixel_width;
25901 #endif /* HAVE_WINDOW_SYSTEM */
25902 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25903 && calc_pixel_width_or_height (&tem, it, prop, font, true,
25904 &align_to))
25906 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25907 align_to = (align_to < 0
25909 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25910 else if (align_to < 0)
25911 align_to = window_box_left_offset (it->w, TEXT_AREA);
25912 width = max (0, (int)tem + align_to - it->current_x);
25913 zero_width_ok_p = true;
25915 else
25916 /* Nothing specified -> width defaults to canonical char width. */
25917 width = FRAME_COLUMN_WIDTH (it->f);
25919 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25920 width = 1;
25922 #ifdef HAVE_WINDOW_SYSTEM
25923 /* Compute height. */
25924 if (FRAME_WINDOW_P (it->f))
25926 int default_height = normal_char_height (font);
25928 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25929 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25931 height = (int)tem;
25932 zero_height_ok_p = true;
25934 else if (prop = Fplist_get (plist, QCrelative_height),
25935 NUMVAL (prop) > 0)
25936 height = default_height * NUMVAL (prop);
25937 else
25938 height = default_height;
25940 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25941 height = 1;
25943 /* Compute percentage of height used for ascent. If
25944 `:ascent ASCENT' is present and valid, use that. Otherwise,
25945 derive the ascent from the font in use. */
25946 if (prop = Fplist_get (plist, QCascent),
25947 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25948 ascent = height * NUMVAL (prop) / 100.0;
25949 else if (!NILP (prop)
25950 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25951 ascent = min (max (0, (int)tem), height);
25952 else
25953 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25955 else
25956 #endif /* HAVE_WINDOW_SYSTEM */
25957 height = 1;
25959 if (width > 0 && it->line_wrap != TRUNCATE
25960 && it->current_x + width > it->last_visible_x)
25962 width = it->last_visible_x - it->current_x;
25963 #ifdef HAVE_WINDOW_SYSTEM
25964 /* Subtract one more pixel from the stretch width, but only on
25965 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25966 width -= FRAME_WINDOW_P (it->f);
25967 #endif
25970 if (width > 0 && height > 0 && it->glyph_row)
25972 Lisp_Object o_object = it->object;
25973 Lisp_Object object = it->stack[it->sp - 1].string;
25974 int n = width;
25976 if (!STRINGP (object))
25977 object = it->w->contents;
25978 #ifdef HAVE_WINDOW_SYSTEM
25979 if (FRAME_WINDOW_P (it->f))
25980 append_stretch_glyph (it, object, width, height, ascent);
25981 else
25982 #endif
25984 it->object = object;
25985 it->char_to_display = ' ';
25986 it->pixel_width = it->len = 1;
25987 while (n--)
25988 tty_append_glyph (it);
25989 it->object = o_object;
25993 it->pixel_width = width;
25994 #ifdef HAVE_WINDOW_SYSTEM
25995 if (FRAME_WINDOW_P (it->f))
25997 it->ascent = it->phys_ascent = ascent;
25998 it->descent = it->phys_descent = height - it->ascent;
25999 it->nglyphs = width > 0 && height > 0;
26000 take_vertical_position_into_account (it);
26002 else
26003 #endif
26004 it->nglyphs = width;
26007 /* Get information about special display element WHAT in an
26008 environment described by IT. WHAT is one of IT_TRUNCATION or
26009 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26010 non-null glyph_row member. This function ensures that fields like
26011 face_id, c, len of IT are left untouched. */
26013 static void
26014 produce_special_glyphs (struct it *it, enum display_element_type what)
26016 struct it temp_it;
26017 Lisp_Object gc;
26018 GLYPH glyph;
26020 temp_it = *it;
26021 temp_it.object = Qnil;
26022 memset (&temp_it.current, 0, sizeof temp_it.current);
26024 if (what == IT_CONTINUATION)
26026 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26027 if (it->bidi_it.paragraph_dir == R2L)
26028 SET_GLYPH_FROM_CHAR (glyph, '/');
26029 else
26030 SET_GLYPH_FROM_CHAR (glyph, '\\');
26031 if (it->dp
26032 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26034 /* FIXME: Should we mirror GC for R2L lines? */
26035 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26036 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26039 else if (what == IT_TRUNCATION)
26041 /* Truncation glyph. */
26042 SET_GLYPH_FROM_CHAR (glyph, '$');
26043 if (it->dp
26044 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26046 /* FIXME: Should we mirror GC for R2L lines? */
26047 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26048 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26051 else
26052 emacs_abort ();
26054 #ifdef HAVE_WINDOW_SYSTEM
26055 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26056 is turned off, we precede the truncation/continuation glyphs by a
26057 stretch glyph whose width is computed such that these special
26058 glyphs are aligned at the window margin, even when very different
26059 fonts are used in different glyph rows. */
26060 if (FRAME_WINDOW_P (temp_it.f)
26061 /* init_iterator calls this with it->glyph_row == NULL, and it
26062 wants only the pixel width of the truncation/continuation
26063 glyphs. */
26064 && temp_it.glyph_row
26065 /* insert_left_trunc_glyphs calls us at the beginning of the
26066 row, and it has its own calculation of the stretch glyph
26067 width. */
26068 && temp_it.glyph_row->used[TEXT_AREA] > 0
26069 && (temp_it.glyph_row->reversed_p
26070 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26071 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26073 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26075 if (stretch_width > 0)
26077 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26078 struct font *font =
26079 face->font ? face->font : FRAME_FONT (temp_it.f);
26080 int stretch_ascent =
26081 (((temp_it.ascent + temp_it.descent)
26082 * FONT_BASE (font)) / FONT_HEIGHT (font));
26084 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26085 temp_it.ascent + temp_it.descent,
26086 stretch_ascent);
26089 #endif
26091 temp_it.dp = NULL;
26092 temp_it.what = IT_CHARACTER;
26093 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26094 temp_it.face_id = GLYPH_FACE (glyph);
26095 temp_it.len = CHAR_BYTES (temp_it.c);
26097 PRODUCE_GLYPHS (&temp_it);
26098 it->pixel_width = temp_it.pixel_width;
26099 it->nglyphs = temp_it.nglyphs;
26102 #ifdef HAVE_WINDOW_SYSTEM
26104 /* Calculate line-height and line-spacing properties.
26105 An integer value specifies explicit pixel value.
26106 A float value specifies relative value to current face height.
26107 A cons (float . face-name) specifies relative value to
26108 height of specified face font.
26110 Returns height in pixels, or nil. */
26112 static Lisp_Object
26113 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26114 int boff, bool override)
26116 Lisp_Object face_name = Qnil;
26117 int ascent, descent, height;
26119 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26120 return val;
26122 if (CONSP (val))
26124 face_name = XCAR (val);
26125 val = XCDR (val);
26126 if (!NUMBERP (val))
26127 val = make_number (1);
26128 if (NILP (face_name))
26130 height = it->ascent + it->descent;
26131 goto scale;
26135 if (NILP (face_name))
26137 font = FRAME_FONT (it->f);
26138 boff = FRAME_BASELINE_OFFSET (it->f);
26140 else if (EQ (face_name, Qt))
26142 override = false;
26144 else
26146 int face_id;
26147 struct face *face;
26149 face_id = lookup_named_face (it->f, face_name, false);
26150 if (face_id < 0)
26151 return make_number (-1);
26153 face = FACE_FROM_ID (it->f, face_id);
26154 font = face->font;
26155 if (font == NULL)
26156 return make_number (-1);
26157 boff = font->baseline_offset;
26158 if (font->vertical_centering)
26159 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26162 normal_char_ascent_descent (font, &ascent, &descent);
26164 if (override)
26166 it->override_ascent = ascent;
26167 it->override_descent = descent;
26168 it->override_boff = boff;
26171 height = ascent + descent;
26173 scale:
26174 if (FLOATP (val))
26175 height = (int)(XFLOAT_DATA (val) * height);
26176 else if (INTEGERP (val))
26177 height *= XINT (val);
26179 return make_number (height);
26183 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26184 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26185 and only if this is for a character for which no font was found.
26187 If the display method (it->glyphless_method) is
26188 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26189 length of the acronym or the hexadecimal string, UPPER_XOFF and
26190 UPPER_YOFF are pixel offsets for the upper part of the string,
26191 LOWER_XOFF and LOWER_YOFF are for the lower part.
26193 For the other display methods, LEN through LOWER_YOFF are zero. */
26195 static void
26196 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26197 short upper_xoff, short upper_yoff,
26198 short lower_xoff, short lower_yoff)
26200 struct glyph *glyph;
26201 enum glyph_row_area area = it->area;
26203 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26204 if (glyph < it->glyph_row->glyphs[area + 1])
26206 /* If the glyph row is reversed, we need to prepend the glyph
26207 rather than append it. */
26208 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26210 struct glyph *g;
26212 /* Make room for the additional glyph. */
26213 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26214 g[1] = *g;
26215 glyph = it->glyph_row->glyphs[area];
26217 glyph->charpos = CHARPOS (it->position);
26218 glyph->object = it->object;
26219 glyph->pixel_width = it->pixel_width;
26220 glyph->ascent = it->ascent;
26221 glyph->descent = it->descent;
26222 glyph->voffset = it->voffset;
26223 glyph->type = GLYPHLESS_GLYPH;
26224 glyph->u.glyphless.method = it->glyphless_method;
26225 glyph->u.glyphless.for_no_font = for_no_font;
26226 glyph->u.glyphless.len = len;
26227 glyph->u.glyphless.ch = it->c;
26228 glyph->slice.glyphless.upper_xoff = upper_xoff;
26229 glyph->slice.glyphless.upper_yoff = upper_yoff;
26230 glyph->slice.glyphless.lower_xoff = lower_xoff;
26231 glyph->slice.glyphless.lower_yoff = lower_yoff;
26232 glyph->avoid_cursor_p = it->avoid_cursor_p;
26233 glyph->multibyte_p = it->multibyte_p;
26234 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26236 /* In R2L rows, the left and the right box edges need to be
26237 drawn in reverse direction. */
26238 glyph->right_box_line_p = it->start_of_box_run_p;
26239 glyph->left_box_line_p = it->end_of_box_run_p;
26241 else
26243 glyph->left_box_line_p = it->start_of_box_run_p;
26244 glyph->right_box_line_p = it->end_of_box_run_p;
26246 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26247 || it->phys_descent > it->descent);
26248 glyph->padding_p = false;
26249 glyph->glyph_not_available_p = false;
26250 glyph->face_id = face_id;
26251 glyph->font_type = FONT_TYPE_UNKNOWN;
26252 if (it->bidi_p)
26254 glyph->resolved_level = it->bidi_it.resolved_level;
26255 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26256 glyph->bidi_type = it->bidi_it.type;
26258 ++it->glyph_row->used[area];
26260 else
26261 IT_EXPAND_MATRIX_WIDTH (it, area);
26265 /* Produce a glyph for a glyphless character for iterator IT.
26266 IT->glyphless_method specifies which method to use for displaying
26267 the character. See the description of enum
26268 glyphless_display_method in dispextern.h for the detail.
26270 FOR_NO_FONT is true if and only if this is for a character for
26271 which no font was found. ACRONYM, if non-nil, is an acronym string
26272 for the character. */
26274 static void
26275 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26277 int face_id;
26278 struct face *face;
26279 struct font *font;
26280 int base_width, base_height, width, height;
26281 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26282 int len;
26284 /* Get the metrics of the base font. We always refer to the current
26285 ASCII face. */
26286 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26287 font = face->font ? face->font : FRAME_FONT (it->f);
26288 normal_char_ascent_descent (font, &it->ascent, &it->descent);
26289 it->ascent += font->baseline_offset;
26290 it->descent -= font->baseline_offset;
26291 base_height = it->ascent + it->descent;
26292 base_width = font->average_width;
26294 face_id = merge_glyphless_glyph_face (it);
26296 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26298 it->pixel_width = THIN_SPACE_WIDTH;
26299 len = 0;
26300 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26302 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26304 width = CHAR_WIDTH (it->c);
26305 if (width == 0)
26306 width = 1;
26307 else if (width > 4)
26308 width = 4;
26309 it->pixel_width = base_width * width;
26310 len = 0;
26311 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26313 else
26315 char buf[7];
26316 const char *str;
26317 unsigned int code[6];
26318 int upper_len;
26319 int ascent, descent;
26320 struct font_metrics metrics_upper, metrics_lower;
26322 face = FACE_FROM_ID (it->f, face_id);
26323 font = face->font ? face->font : FRAME_FONT (it->f);
26324 prepare_face_for_display (it->f, face);
26326 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26328 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26329 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26330 if (CONSP (acronym))
26331 acronym = XCAR (acronym);
26332 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26334 else
26336 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26337 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26338 str = buf;
26340 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26341 code[len] = font->driver->encode_char (font, str[len]);
26342 upper_len = (len + 1) / 2;
26343 font->driver->text_extents (font, code, upper_len,
26344 &metrics_upper);
26345 font->driver->text_extents (font, code + upper_len, len - upper_len,
26346 &metrics_lower);
26350 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26351 width = max (metrics_upper.width, metrics_lower.width) + 4;
26352 upper_xoff = upper_yoff = 2; /* the typical case */
26353 if (base_width >= width)
26355 /* Align the upper to the left, the lower to the right. */
26356 it->pixel_width = base_width;
26357 lower_xoff = base_width - 2 - metrics_lower.width;
26359 else
26361 /* Center the shorter one. */
26362 it->pixel_width = width;
26363 if (metrics_upper.width >= metrics_lower.width)
26364 lower_xoff = (width - metrics_lower.width) / 2;
26365 else
26367 /* FIXME: This code doesn't look right. It formerly was
26368 missing the "lower_xoff = 0;", which couldn't have
26369 been right since it left lower_xoff uninitialized. */
26370 lower_xoff = 0;
26371 upper_xoff = (width - metrics_upper.width) / 2;
26375 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26376 top, bottom, and between upper and lower strings. */
26377 height = (metrics_upper.ascent + metrics_upper.descent
26378 + metrics_lower.ascent + metrics_lower.descent) + 5;
26379 /* Center vertically.
26380 H:base_height, D:base_descent
26381 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26383 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26384 descent = D - H/2 + h/2;
26385 lower_yoff = descent - 2 - ld;
26386 upper_yoff = lower_yoff - la - 1 - ud; */
26387 ascent = - (it->descent - (base_height + height + 1) / 2);
26388 descent = it->descent - (base_height - height) / 2;
26389 lower_yoff = descent - 2 - metrics_lower.descent;
26390 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26391 - metrics_upper.descent);
26392 /* Don't make the height shorter than the base height. */
26393 if (height > base_height)
26395 it->ascent = ascent;
26396 it->descent = descent;
26400 it->phys_ascent = it->ascent;
26401 it->phys_descent = it->descent;
26402 if (it->glyph_row)
26403 append_glyphless_glyph (it, face_id, for_no_font, len,
26404 upper_xoff, upper_yoff,
26405 lower_xoff, lower_yoff);
26406 it->nglyphs = 1;
26407 take_vertical_position_into_account (it);
26411 /* RIF:
26412 Produce glyphs/get display metrics for the display element IT is
26413 loaded with. See the description of struct it in dispextern.h
26414 for an overview of struct it. */
26416 void
26417 x_produce_glyphs (struct it *it)
26419 int extra_line_spacing = it->extra_line_spacing;
26421 it->glyph_not_available_p = false;
26423 if (it->what == IT_CHARACTER)
26425 XChar2b char2b;
26426 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26427 struct font *font = face->font;
26428 struct font_metrics *pcm = NULL;
26429 int boff; /* Baseline offset. */
26431 if (font == NULL)
26433 /* When no suitable font is found, display this character by
26434 the method specified in the first extra slot of
26435 Vglyphless_char_display. */
26436 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26438 eassert (it->what == IT_GLYPHLESS);
26439 produce_glyphless_glyph (it, true,
26440 STRINGP (acronym) ? acronym : Qnil);
26441 goto done;
26444 boff = font->baseline_offset;
26445 if (font->vertical_centering)
26446 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26448 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26450 it->nglyphs = 1;
26452 if (it->override_ascent >= 0)
26454 it->ascent = it->override_ascent;
26455 it->descent = it->override_descent;
26456 boff = it->override_boff;
26458 else
26460 it->ascent = FONT_BASE (font) + boff;
26461 it->descent = FONT_DESCENT (font) - boff;
26464 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26466 pcm = get_per_char_metric (font, &char2b);
26467 if (pcm->width == 0
26468 && pcm->rbearing == 0 && pcm->lbearing == 0)
26469 pcm = NULL;
26472 if (pcm)
26474 it->phys_ascent = pcm->ascent + boff;
26475 it->phys_descent = pcm->descent - boff;
26476 it->pixel_width = pcm->width;
26477 /* Don't use font-global values for ascent and descent
26478 if they result in an exceedingly large line height. */
26479 if (it->override_ascent < 0)
26481 if (FONT_TOO_HIGH (font))
26483 it->ascent = it->phys_ascent;
26484 it->descent = it->phys_descent;
26485 /* These limitations are enforced by an
26486 assertion near the end of this function. */
26487 if (it->ascent < 0)
26488 it->ascent = 0;
26489 if (it->descent < 0)
26490 it->descent = 0;
26494 else
26496 it->glyph_not_available_p = true;
26497 it->phys_ascent = it->ascent;
26498 it->phys_descent = it->descent;
26499 it->pixel_width = font->space_width;
26502 if (it->constrain_row_ascent_descent_p)
26504 if (it->descent > it->max_descent)
26506 it->ascent += it->descent - it->max_descent;
26507 it->descent = it->max_descent;
26509 if (it->ascent > it->max_ascent)
26511 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26512 it->ascent = it->max_ascent;
26514 it->phys_ascent = min (it->phys_ascent, it->ascent);
26515 it->phys_descent = min (it->phys_descent, it->descent);
26516 extra_line_spacing = 0;
26519 /* If this is a space inside a region of text with
26520 `space-width' property, change its width. */
26521 bool stretched_p
26522 = it->char_to_display == ' ' && !NILP (it->space_width);
26523 if (stretched_p)
26524 it->pixel_width *= XFLOATINT (it->space_width);
26526 /* If face has a box, add the box thickness to the character
26527 height. If character has a box line to the left and/or
26528 right, add the box line width to the character's width. */
26529 if (face->box != FACE_NO_BOX)
26531 int thick = face->box_line_width;
26533 if (thick > 0)
26535 it->ascent += thick;
26536 it->descent += thick;
26538 else
26539 thick = -thick;
26541 if (it->start_of_box_run_p)
26542 it->pixel_width += thick;
26543 if (it->end_of_box_run_p)
26544 it->pixel_width += thick;
26547 /* If face has an overline, add the height of the overline
26548 (1 pixel) and a 1 pixel margin to the character height. */
26549 if (face->overline_p)
26550 it->ascent += overline_margin;
26552 if (it->constrain_row_ascent_descent_p)
26554 if (it->ascent > it->max_ascent)
26555 it->ascent = it->max_ascent;
26556 if (it->descent > it->max_descent)
26557 it->descent = it->max_descent;
26560 take_vertical_position_into_account (it);
26562 /* If we have to actually produce glyphs, do it. */
26563 if (it->glyph_row)
26565 if (stretched_p)
26567 /* Translate a space with a `space-width' property
26568 into a stretch glyph. */
26569 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26570 / FONT_HEIGHT (font));
26571 append_stretch_glyph (it, it->object, it->pixel_width,
26572 it->ascent + it->descent, ascent);
26574 else
26575 append_glyph (it);
26577 /* If characters with lbearing or rbearing are displayed
26578 in this line, record that fact in a flag of the
26579 glyph row. This is used to optimize X output code. */
26580 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26581 it->glyph_row->contains_overlapping_glyphs_p = true;
26583 if (! stretched_p && it->pixel_width == 0)
26584 /* We assure that all visible glyphs have at least 1-pixel
26585 width. */
26586 it->pixel_width = 1;
26588 else if (it->char_to_display == '\n')
26590 /* A newline has no width, but we need the height of the
26591 line. But if previous part of the line sets a height,
26592 don't increase that height. */
26594 Lisp_Object height;
26595 Lisp_Object total_height = Qnil;
26597 it->override_ascent = -1;
26598 it->pixel_width = 0;
26599 it->nglyphs = 0;
26601 height = get_it_property (it, Qline_height);
26602 /* Split (line-height total-height) list. */
26603 if (CONSP (height)
26604 && CONSP (XCDR (height))
26605 && NILP (XCDR (XCDR (height))))
26607 total_height = XCAR (XCDR (height));
26608 height = XCAR (height);
26610 height = calc_line_height_property (it, height, font, boff, true);
26612 if (it->override_ascent >= 0)
26614 it->ascent = it->override_ascent;
26615 it->descent = it->override_descent;
26616 boff = it->override_boff;
26618 else
26620 if (FONT_TOO_HIGH (font))
26622 it->ascent = font->pixel_size + boff - 1;
26623 it->descent = -boff + 1;
26624 if (it->descent < 0)
26625 it->descent = 0;
26627 else
26629 it->ascent = FONT_BASE (font) + boff;
26630 it->descent = FONT_DESCENT (font) - boff;
26634 if (EQ (height, Qt))
26636 if (it->descent > it->max_descent)
26638 it->ascent += it->descent - it->max_descent;
26639 it->descent = it->max_descent;
26641 if (it->ascent > it->max_ascent)
26643 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26644 it->ascent = it->max_ascent;
26646 it->phys_ascent = min (it->phys_ascent, it->ascent);
26647 it->phys_descent = min (it->phys_descent, it->descent);
26648 it->constrain_row_ascent_descent_p = true;
26649 extra_line_spacing = 0;
26651 else
26653 Lisp_Object spacing;
26655 it->phys_ascent = it->ascent;
26656 it->phys_descent = it->descent;
26658 if ((it->max_ascent > 0 || it->max_descent > 0)
26659 && face->box != FACE_NO_BOX
26660 && face->box_line_width > 0)
26662 it->ascent += face->box_line_width;
26663 it->descent += face->box_line_width;
26665 if (!NILP (height)
26666 && XINT (height) > it->ascent + it->descent)
26667 it->ascent = XINT (height) - it->descent;
26669 if (!NILP (total_height))
26670 spacing = calc_line_height_property (it, total_height, font,
26671 boff, false);
26672 else
26674 spacing = get_it_property (it, Qline_spacing);
26675 spacing = calc_line_height_property (it, spacing, font,
26676 boff, false);
26678 if (INTEGERP (spacing))
26680 extra_line_spacing = XINT (spacing);
26681 if (!NILP (total_height))
26682 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26686 else /* i.e. (it->char_to_display == '\t') */
26688 if (font->space_width > 0)
26690 int tab_width = it->tab_width * font->space_width;
26691 int x = it->current_x + it->continuation_lines_width;
26692 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26694 /* If the distance from the current position to the next tab
26695 stop is less than a space character width, use the
26696 tab stop after that. */
26697 if (next_tab_x - x < font->space_width)
26698 next_tab_x += tab_width;
26700 it->pixel_width = next_tab_x - x;
26701 it->nglyphs = 1;
26702 if (FONT_TOO_HIGH (font))
26704 if (get_char_glyph_code (' ', font, &char2b))
26706 pcm = get_per_char_metric (font, &char2b);
26707 if (pcm->width == 0
26708 && pcm->rbearing == 0 && pcm->lbearing == 0)
26709 pcm = NULL;
26712 if (pcm)
26714 it->ascent = pcm->ascent + boff;
26715 it->descent = pcm->descent - boff;
26717 else
26719 it->ascent = font->pixel_size + boff - 1;
26720 it->descent = -boff + 1;
26722 if (it->ascent < 0)
26723 it->ascent = 0;
26724 if (it->descent < 0)
26725 it->descent = 0;
26727 else
26729 it->ascent = FONT_BASE (font) + boff;
26730 it->descent = FONT_DESCENT (font) - boff;
26732 it->phys_ascent = it->ascent;
26733 it->phys_descent = it->descent;
26735 if (it->glyph_row)
26737 append_stretch_glyph (it, it->object, it->pixel_width,
26738 it->ascent + it->descent, it->ascent);
26741 else
26743 it->pixel_width = 0;
26744 it->nglyphs = 1;
26748 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26750 /* A static composition.
26752 Note: A composition is represented as one glyph in the
26753 glyph matrix. There are no padding glyphs.
26755 Important note: pixel_width, ascent, and descent are the
26756 values of what is drawn by draw_glyphs (i.e. the values of
26757 the overall glyphs composed). */
26758 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26759 int boff; /* baseline offset */
26760 struct composition *cmp = composition_table[it->cmp_it.id];
26761 int glyph_len = cmp->glyph_len;
26762 struct font *font = face->font;
26764 it->nglyphs = 1;
26766 /* If we have not yet calculated pixel size data of glyphs of
26767 the composition for the current face font, calculate them
26768 now. Theoretically, we have to check all fonts for the
26769 glyphs, but that requires much time and memory space. So,
26770 here we check only the font of the first glyph. This may
26771 lead to incorrect display, but it's very rare, and C-l
26772 (recenter-top-bottom) can correct the display anyway. */
26773 if (! cmp->font || cmp->font != font)
26775 /* Ascent and descent of the font of the first character
26776 of this composition (adjusted by baseline offset).
26777 Ascent and descent of overall glyphs should not be less
26778 than these, respectively. */
26779 int font_ascent, font_descent, font_height;
26780 /* Bounding box of the overall glyphs. */
26781 int leftmost, rightmost, lowest, highest;
26782 int lbearing, rbearing;
26783 int i, width, ascent, descent;
26784 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26785 XChar2b char2b;
26786 struct font_metrics *pcm;
26787 ptrdiff_t pos;
26789 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26790 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26791 break;
26792 bool right_padded = glyph_len < cmp->glyph_len;
26793 for (i = 0; i < glyph_len; i++)
26795 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26796 break;
26797 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26799 bool left_padded = i > 0;
26801 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26802 : IT_CHARPOS (*it));
26803 /* If no suitable font is found, use the default font. */
26804 bool font_not_found_p = font == NULL;
26805 if (font_not_found_p)
26807 face = face->ascii_face;
26808 font = face->font;
26810 boff = font->baseline_offset;
26811 if (font->vertical_centering)
26812 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26813 font_ascent = FONT_BASE (font) + boff;
26814 font_descent = FONT_DESCENT (font) - boff;
26815 font_height = FONT_HEIGHT (font);
26817 cmp->font = font;
26819 pcm = NULL;
26820 if (! font_not_found_p)
26822 get_char_face_and_encoding (it->f, c, it->face_id,
26823 &char2b, false);
26824 pcm = get_per_char_metric (font, &char2b);
26827 /* Initialize the bounding box. */
26828 if (pcm)
26830 width = cmp->glyph_len > 0 ? pcm->width : 0;
26831 ascent = pcm->ascent;
26832 descent = pcm->descent;
26833 lbearing = pcm->lbearing;
26834 rbearing = pcm->rbearing;
26836 else
26838 width = cmp->glyph_len > 0 ? font->space_width : 0;
26839 ascent = FONT_BASE (font);
26840 descent = FONT_DESCENT (font);
26841 lbearing = 0;
26842 rbearing = width;
26845 rightmost = width;
26846 leftmost = 0;
26847 lowest = - descent + boff;
26848 highest = ascent + boff;
26850 if (! font_not_found_p
26851 && font->default_ascent
26852 && CHAR_TABLE_P (Vuse_default_ascent)
26853 && !NILP (Faref (Vuse_default_ascent,
26854 make_number (it->char_to_display))))
26855 highest = font->default_ascent + boff;
26857 /* Draw the first glyph at the normal position. It may be
26858 shifted to right later if some other glyphs are drawn
26859 at the left. */
26860 cmp->offsets[i * 2] = 0;
26861 cmp->offsets[i * 2 + 1] = boff;
26862 cmp->lbearing = lbearing;
26863 cmp->rbearing = rbearing;
26865 /* Set cmp->offsets for the remaining glyphs. */
26866 for (i++; i < glyph_len; i++)
26868 int left, right, btm, top;
26869 int ch = COMPOSITION_GLYPH (cmp, i);
26870 int face_id;
26871 struct face *this_face;
26873 if (ch == '\t')
26874 ch = ' ';
26875 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26876 this_face = FACE_FROM_ID (it->f, face_id);
26877 font = this_face->font;
26879 if (font == NULL)
26880 pcm = NULL;
26881 else
26883 get_char_face_and_encoding (it->f, ch, face_id,
26884 &char2b, false);
26885 pcm = get_per_char_metric (font, &char2b);
26887 if (! pcm)
26888 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26889 else
26891 width = pcm->width;
26892 ascent = pcm->ascent;
26893 descent = pcm->descent;
26894 lbearing = pcm->lbearing;
26895 rbearing = pcm->rbearing;
26896 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26898 /* Relative composition with or without
26899 alternate chars. */
26900 left = (leftmost + rightmost - width) / 2;
26901 btm = - descent + boff;
26902 if (font->relative_compose
26903 && (! CHAR_TABLE_P (Vignore_relative_composition)
26904 || NILP (Faref (Vignore_relative_composition,
26905 make_number (ch)))))
26908 if (- descent >= font->relative_compose)
26909 /* One extra pixel between two glyphs. */
26910 btm = highest + 1;
26911 else if (ascent <= 0)
26912 /* One extra pixel between two glyphs. */
26913 btm = lowest - 1 - ascent - descent;
26916 else
26918 /* A composition rule is specified by an integer
26919 value that encodes global and new reference
26920 points (GREF and NREF). GREF and NREF are
26921 specified by numbers as below:
26923 0---1---2 -- ascent
26927 9--10--11 -- center
26929 ---3---4---5--- baseline
26931 6---7---8 -- descent
26933 int rule = COMPOSITION_RULE (cmp, i);
26934 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26936 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26937 grefx = gref % 3, nrefx = nref % 3;
26938 grefy = gref / 3, nrefy = nref / 3;
26939 if (xoff)
26940 xoff = font_height * (xoff - 128) / 256;
26941 if (yoff)
26942 yoff = font_height * (yoff - 128) / 256;
26944 left = (leftmost
26945 + grefx * (rightmost - leftmost) / 2
26946 - nrefx * width / 2
26947 + xoff);
26949 btm = ((grefy == 0 ? highest
26950 : grefy == 1 ? 0
26951 : grefy == 2 ? lowest
26952 : (highest + lowest) / 2)
26953 - (nrefy == 0 ? ascent + descent
26954 : nrefy == 1 ? descent - boff
26955 : nrefy == 2 ? 0
26956 : (ascent + descent) / 2)
26957 + yoff);
26960 cmp->offsets[i * 2] = left;
26961 cmp->offsets[i * 2 + 1] = btm + descent;
26963 /* Update the bounding box of the overall glyphs. */
26964 if (width > 0)
26966 right = left + width;
26967 if (left < leftmost)
26968 leftmost = left;
26969 if (right > rightmost)
26970 rightmost = right;
26972 top = btm + descent + ascent;
26973 if (top > highest)
26974 highest = top;
26975 if (btm < lowest)
26976 lowest = btm;
26978 if (cmp->lbearing > left + lbearing)
26979 cmp->lbearing = left + lbearing;
26980 if (cmp->rbearing < left + rbearing)
26981 cmp->rbearing = left + rbearing;
26985 /* If there are glyphs whose x-offsets are negative,
26986 shift all glyphs to the right and make all x-offsets
26987 non-negative. */
26988 if (leftmost < 0)
26990 for (i = 0; i < cmp->glyph_len; i++)
26991 cmp->offsets[i * 2] -= leftmost;
26992 rightmost -= leftmost;
26993 cmp->lbearing -= leftmost;
26994 cmp->rbearing -= leftmost;
26997 if (left_padded && cmp->lbearing < 0)
26999 for (i = 0; i < cmp->glyph_len; i++)
27000 cmp->offsets[i * 2] -= cmp->lbearing;
27001 rightmost -= cmp->lbearing;
27002 cmp->rbearing -= cmp->lbearing;
27003 cmp->lbearing = 0;
27005 if (right_padded && rightmost < cmp->rbearing)
27007 rightmost = cmp->rbearing;
27010 cmp->pixel_width = rightmost;
27011 cmp->ascent = highest;
27012 cmp->descent = - lowest;
27013 if (cmp->ascent < font_ascent)
27014 cmp->ascent = font_ascent;
27015 if (cmp->descent < font_descent)
27016 cmp->descent = font_descent;
27019 if (it->glyph_row
27020 && (cmp->lbearing < 0
27021 || cmp->rbearing > cmp->pixel_width))
27022 it->glyph_row->contains_overlapping_glyphs_p = true;
27024 it->pixel_width = cmp->pixel_width;
27025 it->ascent = it->phys_ascent = cmp->ascent;
27026 it->descent = it->phys_descent = cmp->descent;
27027 if (face->box != FACE_NO_BOX)
27029 int thick = face->box_line_width;
27031 if (thick > 0)
27033 it->ascent += thick;
27034 it->descent += thick;
27036 else
27037 thick = - thick;
27039 if (it->start_of_box_run_p)
27040 it->pixel_width += thick;
27041 if (it->end_of_box_run_p)
27042 it->pixel_width += thick;
27045 /* If face has an overline, add the height of the overline
27046 (1 pixel) and a 1 pixel margin to the character height. */
27047 if (face->overline_p)
27048 it->ascent += overline_margin;
27050 take_vertical_position_into_account (it);
27051 if (it->ascent < 0)
27052 it->ascent = 0;
27053 if (it->descent < 0)
27054 it->descent = 0;
27056 if (it->glyph_row && cmp->glyph_len > 0)
27057 append_composite_glyph (it);
27059 else if (it->what == IT_COMPOSITION)
27061 /* A dynamic (automatic) composition. */
27062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27063 Lisp_Object gstring;
27064 struct font_metrics metrics;
27066 it->nglyphs = 1;
27068 gstring = composition_gstring_from_id (it->cmp_it.id);
27069 it->pixel_width
27070 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27071 &metrics);
27072 if (it->glyph_row
27073 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27074 it->glyph_row->contains_overlapping_glyphs_p = true;
27075 it->ascent = it->phys_ascent = metrics.ascent;
27076 it->descent = it->phys_descent = metrics.descent;
27077 if (face->box != FACE_NO_BOX)
27079 int thick = face->box_line_width;
27081 if (thick > 0)
27083 it->ascent += thick;
27084 it->descent += thick;
27086 else
27087 thick = - thick;
27089 if (it->start_of_box_run_p)
27090 it->pixel_width += thick;
27091 if (it->end_of_box_run_p)
27092 it->pixel_width += thick;
27094 /* If face has an overline, add the height of the overline
27095 (1 pixel) and a 1 pixel margin to the character height. */
27096 if (face->overline_p)
27097 it->ascent += overline_margin;
27098 take_vertical_position_into_account (it);
27099 if (it->ascent < 0)
27100 it->ascent = 0;
27101 if (it->descent < 0)
27102 it->descent = 0;
27104 if (it->glyph_row)
27105 append_composite_glyph (it);
27107 else if (it->what == IT_GLYPHLESS)
27108 produce_glyphless_glyph (it, false, Qnil);
27109 else if (it->what == IT_IMAGE)
27110 produce_image_glyph (it);
27111 else if (it->what == IT_STRETCH)
27112 produce_stretch_glyph (it);
27114 done:
27115 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27116 because this isn't true for images with `:ascent 100'. */
27117 eassert (it->ascent >= 0 && it->descent >= 0);
27118 if (it->area == TEXT_AREA)
27119 it->current_x += it->pixel_width;
27121 if (extra_line_spacing > 0)
27123 it->descent += extra_line_spacing;
27124 if (extra_line_spacing > it->max_extra_line_spacing)
27125 it->max_extra_line_spacing = extra_line_spacing;
27128 it->max_ascent = max (it->max_ascent, it->ascent);
27129 it->max_descent = max (it->max_descent, it->descent);
27130 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27131 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27134 /* EXPORT for RIF:
27135 Output LEN glyphs starting at START at the nominal cursor position.
27136 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27137 being updated, and UPDATED_AREA is the area of that row being updated. */
27139 void
27140 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27141 struct glyph *start, enum glyph_row_area updated_area, int len)
27143 int x, hpos, chpos = w->phys_cursor.hpos;
27145 eassert (updated_row);
27146 /* When the window is hscrolled, cursor hpos can legitimately be out
27147 of bounds, but we draw the cursor at the corresponding window
27148 margin in that case. */
27149 if (!updated_row->reversed_p && chpos < 0)
27150 chpos = 0;
27151 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27152 chpos = updated_row->used[TEXT_AREA] - 1;
27154 block_input ();
27156 /* Write glyphs. */
27158 hpos = start - updated_row->glyphs[updated_area];
27159 x = draw_glyphs (w, w->output_cursor.x,
27160 updated_row, updated_area,
27161 hpos, hpos + len,
27162 DRAW_NORMAL_TEXT, 0);
27164 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27165 if (updated_area == TEXT_AREA
27166 && w->phys_cursor_on_p
27167 && w->phys_cursor.vpos == w->output_cursor.vpos
27168 && chpos >= hpos
27169 && chpos < hpos + len)
27170 w->phys_cursor_on_p = false;
27172 unblock_input ();
27174 /* Advance the output cursor. */
27175 w->output_cursor.hpos += len;
27176 w->output_cursor.x = x;
27180 /* EXPORT for RIF:
27181 Insert LEN glyphs from START at the nominal cursor position. */
27183 void
27184 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27185 struct glyph *start, enum glyph_row_area updated_area, int len)
27187 struct frame *f;
27188 int line_height, shift_by_width, shifted_region_width;
27189 struct glyph_row *row;
27190 struct glyph *glyph;
27191 int frame_x, frame_y;
27192 ptrdiff_t hpos;
27194 eassert (updated_row);
27195 block_input ();
27196 f = XFRAME (WINDOW_FRAME (w));
27198 /* Get the height of the line we are in. */
27199 row = updated_row;
27200 line_height = row->height;
27202 /* Get the width of the glyphs to insert. */
27203 shift_by_width = 0;
27204 for (glyph = start; glyph < start + len; ++glyph)
27205 shift_by_width += glyph->pixel_width;
27207 /* Get the width of the region to shift right. */
27208 shifted_region_width = (window_box_width (w, updated_area)
27209 - w->output_cursor.x
27210 - shift_by_width);
27212 /* Shift right. */
27213 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27214 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27216 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27217 line_height, shift_by_width);
27219 /* Write the glyphs. */
27220 hpos = start - row->glyphs[updated_area];
27221 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27222 hpos, hpos + len,
27223 DRAW_NORMAL_TEXT, 0);
27225 /* Advance the output cursor. */
27226 w->output_cursor.hpos += len;
27227 w->output_cursor.x += shift_by_width;
27228 unblock_input ();
27232 /* EXPORT for RIF:
27233 Erase the current text line from the nominal cursor position
27234 (inclusive) to pixel column TO_X (exclusive). The idea is that
27235 everything from TO_X onward is already erased.
27237 TO_X is a pixel position relative to UPDATED_AREA of currently
27238 updated window W. TO_X == -1 means clear to the end of this area. */
27240 void
27241 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27242 enum glyph_row_area updated_area, int to_x)
27244 struct frame *f;
27245 int max_x, min_y, max_y;
27246 int from_x, from_y, to_y;
27248 eassert (updated_row);
27249 f = XFRAME (w->frame);
27251 if (updated_row->full_width_p)
27252 max_x = (WINDOW_PIXEL_WIDTH (w)
27253 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27254 else
27255 max_x = window_box_width (w, updated_area);
27256 max_y = window_text_bottom_y (w);
27258 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27259 of window. For TO_X > 0, truncate to end of drawing area. */
27260 if (to_x == 0)
27261 return;
27262 else if (to_x < 0)
27263 to_x = max_x;
27264 else
27265 to_x = min (to_x, max_x);
27267 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27269 /* Notice if the cursor will be cleared by this operation. */
27270 if (!updated_row->full_width_p)
27271 notice_overwritten_cursor (w, updated_area,
27272 w->output_cursor.x, -1,
27273 updated_row->y,
27274 MATRIX_ROW_BOTTOM_Y (updated_row));
27276 from_x = w->output_cursor.x;
27278 /* Translate to frame coordinates. */
27279 if (updated_row->full_width_p)
27281 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27282 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27284 else
27286 int area_left = window_box_left (w, updated_area);
27287 from_x += area_left;
27288 to_x += area_left;
27291 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27292 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27293 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27295 /* Prevent inadvertently clearing to end of the X window. */
27296 if (to_x > from_x && to_y > from_y)
27298 block_input ();
27299 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27300 to_x - from_x, to_y - from_y);
27301 unblock_input ();
27305 #endif /* HAVE_WINDOW_SYSTEM */
27309 /***********************************************************************
27310 Cursor types
27311 ***********************************************************************/
27313 /* Value is the internal representation of the specified cursor type
27314 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27315 of the bar cursor. */
27317 static enum text_cursor_kinds
27318 get_specified_cursor_type (Lisp_Object arg, int *width)
27320 enum text_cursor_kinds type;
27322 if (NILP (arg))
27323 return NO_CURSOR;
27325 if (EQ (arg, Qbox))
27326 return FILLED_BOX_CURSOR;
27328 if (EQ (arg, Qhollow))
27329 return HOLLOW_BOX_CURSOR;
27331 if (EQ (arg, Qbar))
27333 *width = 2;
27334 return BAR_CURSOR;
27337 if (CONSP (arg)
27338 && EQ (XCAR (arg), Qbar)
27339 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27341 *width = XINT (XCDR (arg));
27342 return BAR_CURSOR;
27345 if (EQ (arg, Qhbar))
27347 *width = 2;
27348 return HBAR_CURSOR;
27351 if (CONSP (arg)
27352 && EQ (XCAR (arg), Qhbar)
27353 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27355 *width = XINT (XCDR (arg));
27356 return HBAR_CURSOR;
27359 /* Treat anything unknown as "hollow box cursor".
27360 It was bad to signal an error; people have trouble fixing
27361 .Xdefaults with Emacs, when it has something bad in it. */
27362 type = HOLLOW_BOX_CURSOR;
27364 return type;
27367 /* Set the default cursor types for specified frame. */
27368 void
27369 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27371 int width = 1;
27372 Lisp_Object tem;
27374 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27375 FRAME_CURSOR_WIDTH (f) = width;
27377 /* By default, set up the blink-off state depending on the on-state. */
27379 tem = Fassoc (arg, Vblink_cursor_alist);
27380 if (!NILP (tem))
27382 FRAME_BLINK_OFF_CURSOR (f)
27383 = get_specified_cursor_type (XCDR (tem), &width);
27384 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27386 else
27387 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27389 /* Make sure the cursor gets redrawn. */
27390 f->cursor_type_changed = true;
27394 #ifdef HAVE_WINDOW_SYSTEM
27396 /* Return the cursor we want to be displayed in window W. Return
27397 width of bar/hbar cursor through WIDTH arg. Return with
27398 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27399 (i.e. if the `system caret' should track this cursor).
27401 In a mini-buffer window, we want the cursor only to appear if we
27402 are reading input from this window. For the selected window, we
27403 want the cursor type given by the frame parameter or buffer local
27404 setting of cursor-type. If explicitly marked off, draw no cursor.
27405 In all other cases, we want a hollow box cursor. */
27407 static enum text_cursor_kinds
27408 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27409 bool *active_cursor)
27411 struct frame *f = XFRAME (w->frame);
27412 struct buffer *b = XBUFFER (w->contents);
27413 int cursor_type = DEFAULT_CURSOR;
27414 Lisp_Object alt_cursor;
27415 bool non_selected = false;
27417 *active_cursor = true;
27419 /* Echo area */
27420 if (cursor_in_echo_area
27421 && FRAME_HAS_MINIBUF_P (f)
27422 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27424 if (w == XWINDOW (echo_area_window))
27426 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27428 *width = FRAME_CURSOR_WIDTH (f);
27429 return FRAME_DESIRED_CURSOR (f);
27431 else
27432 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27435 *active_cursor = false;
27436 non_selected = true;
27439 /* Detect a nonselected window or nonselected frame. */
27440 else if (w != XWINDOW (f->selected_window)
27441 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27443 *active_cursor = false;
27445 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27446 return NO_CURSOR;
27448 non_selected = true;
27451 /* Never display a cursor in a window in which cursor-type is nil. */
27452 if (NILP (BVAR (b, cursor_type)))
27453 return NO_CURSOR;
27455 /* Get the normal cursor type for this window. */
27456 if (EQ (BVAR (b, cursor_type), Qt))
27458 cursor_type = FRAME_DESIRED_CURSOR (f);
27459 *width = FRAME_CURSOR_WIDTH (f);
27461 else
27462 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27464 /* Use cursor-in-non-selected-windows instead
27465 for non-selected window or frame. */
27466 if (non_selected)
27468 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27469 if (!EQ (Qt, alt_cursor))
27470 return get_specified_cursor_type (alt_cursor, width);
27471 /* t means modify the normal cursor type. */
27472 if (cursor_type == FILLED_BOX_CURSOR)
27473 cursor_type = HOLLOW_BOX_CURSOR;
27474 else if (cursor_type == BAR_CURSOR && *width > 1)
27475 --*width;
27476 return cursor_type;
27479 /* Use normal cursor if not blinked off. */
27480 if (!w->cursor_off_p)
27482 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27484 if (cursor_type == FILLED_BOX_CURSOR)
27486 /* Using a block cursor on large images can be very annoying.
27487 So use a hollow cursor for "large" images.
27488 If image is not transparent (no mask), also use hollow cursor. */
27489 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27490 if (img != NULL && IMAGEP (img->spec))
27492 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27493 where N = size of default frame font size.
27494 This should cover most of the "tiny" icons people may use. */
27495 if (!img->mask
27496 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27497 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27498 cursor_type = HOLLOW_BOX_CURSOR;
27501 else if (cursor_type != NO_CURSOR)
27503 /* Display current only supports BOX and HOLLOW cursors for images.
27504 So for now, unconditionally use a HOLLOW cursor when cursor is
27505 not a solid box cursor. */
27506 cursor_type = HOLLOW_BOX_CURSOR;
27509 return cursor_type;
27512 /* Cursor is blinked off, so determine how to "toggle" it. */
27514 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27515 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27516 return get_specified_cursor_type (XCDR (alt_cursor), width);
27518 /* Then see if frame has specified a specific blink off cursor type. */
27519 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27521 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27522 return FRAME_BLINK_OFF_CURSOR (f);
27525 #if false
27526 /* Some people liked having a permanently visible blinking cursor,
27527 while others had very strong opinions against it. So it was
27528 decided to remove it. KFS 2003-09-03 */
27530 /* Finally perform built-in cursor blinking:
27531 filled box <-> hollow box
27532 wide [h]bar <-> narrow [h]bar
27533 narrow [h]bar <-> no cursor
27534 other type <-> no cursor */
27536 if (cursor_type == FILLED_BOX_CURSOR)
27537 return HOLLOW_BOX_CURSOR;
27539 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27541 *width = 1;
27542 return cursor_type;
27544 #endif
27546 return NO_CURSOR;
27550 /* Notice when the text cursor of window W has been completely
27551 overwritten by a drawing operation that outputs glyphs in AREA
27552 starting at X0 and ending at X1 in the line starting at Y0 and
27553 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27554 the rest of the line after X0 has been written. Y coordinates
27555 are window-relative. */
27557 static void
27558 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27559 int x0, int x1, int y0, int y1)
27561 int cx0, cx1, cy0, cy1;
27562 struct glyph_row *row;
27564 if (!w->phys_cursor_on_p)
27565 return;
27566 if (area != TEXT_AREA)
27567 return;
27569 if (w->phys_cursor.vpos < 0
27570 || w->phys_cursor.vpos >= w->current_matrix->nrows
27571 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27572 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27573 return;
27575 if (row->cursor_in_fringe_p)
27577 row->cursor_in_fringe_p = false;
27578 draw_fringe_bitmap (w, row, row->reversed_p);
27579 w->phys_cursor_on_p = false;
27580 return;
27583 cx0 = w->phys_cursor.x;
27584 cx1 = cx0 + w->phys_cursor_width;
27585 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27586 return;
27588 /* The cursor image will be completely removed from the
27589 screen if the output area intersects the cursor area in
27590 y-direction. When we draw in [y0 y1[, and some part of
27591 the cursor is at y < y0, that part must have been drawn
27592 before. When scrolling, the cursor is erased before
27593 actually scrolling, so we don't come here. When not
27594 scrolling, the rows above the old cursor row must have
27595 changed, and in this case these rows must have written
27596 over the cursor image.
27598 Likewise if part of the cursor is below y1, with the
27599 exception of the cursor being in the first blank row at
27600 the buffer and window end because update_text_area
27601 doesn't draw that row. (Except when it does, but
27602 that's handled in update_text_area.) */
27604 cy0 = w->phys_cursor.y;
27605 cy1 = cy0 + w->phys_cursor_height;
27606 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27607 return;
27609 w->phys_cursor_on_p = false;
27612 #endif /* HAVE_WINDOW_SYSTEM */
27615 /************************************************************************
27616 Mouse Face
27617 ************************************************************************/
27619 #ifdef HAVE_WINDOW_SYSTEM
27621 /* EXPORT for RIF:
27622 Fix the display of area AREA of overlapping row ROW in window W
27623 with respect to the overlapping part OVERLAPS. */
27625 void
27626 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27627 enum glyph_row_area area, int overlaps)
27629 int i, x;
27631 block_input ();
27633 x = 0;
27634 for (i = 0; i < row->used[area];)
27636 if (row->glyphs[area][i].overlaps_vertically_p)
27638 int start = i, start_x = x;
27642 x += row->glyphs[area][i].pixel_width;
27643 ++i;
27645 while (i < row->used[area]
27646 && row->glyphs[area][i].overlaps_vertically_p);
27648 draw_glyphs (w, start_x, row, area,
27649 start, i,
27650 DRAW_NORMAL_TEXT, overlaps);
27652 else
27654 x += row->glyphs[area][i].pixel_width;
27655 ++i;
27659 unblock_input ();
27663 /* EXPORT:
27664 Draw the cursor glyph of window W in glyph row ROW. See the
27665 comment of draw_glyphs for the meaning of HL. */
27667 void
27668 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27669 enum draw_glyphs_face hl)
27671 /* If cursor hpos is out of bounds, don't draw garbage. This can
27672 happen in mini-buffer windows when switching between echo area
27673 glyphs and mini-buffer. */
27674 if ((row->reversed_p
27675 ? (w->phys_cursor.hpos >= 0)
27676 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27678 bool on_p = w->phys_cursor_on_p;
27679 int x1;
27680 int hpos = w->phys_cursor.hpos;
27682 /* When the window is hscrolled, cursor hpos can legitimately be
27683 out of bounds, but we draw the cursor at the corresponding
27684 window margin in that case. */
27685 if (!row->reversed_p && hpos < 0)
27686 hpos = 0;
27687 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27688 hpos = row->used[TEXT_AREA] - 1;
27690 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27691 hl, 0);
27692 w->phys_cursor_on_p = on_p;
27694 if (hl == DRAW_CURSOR)
27695 w->phys_cursor_width = x1 - w->phys_cursor.x;
27696 /* When we erase the cursor, and ROW is overlapped by other
27697 rows, make sure that these overlapping parts of other rows
27698 are redrawn. */
27699 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27701 w->phys_cursor_width = x1 - w->phys_cursor.x;
27703 if (row > w->current_matrix->rows
27704 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27705 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27706 OVERLAPS_ERASED_CURSOR);
27708 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27709 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27710 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27711 OVERLAPS_ERASED_CURSOR);
27717 /* Erase the image of a cursor of window W from the screen. */
27719 void
27720 erase_phys_cursor (struct window *w)
27722 struct frame *f = XFRAME (w->frame);
27723 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27724 int hpos = w->phys_cursor.hpos;
27725 int vpos = w->phys_cursor.vpos;
27726 bool mouse_face_here_p = false;
27727 struct glyph_matrix *active_glyphs = w->current_matrix;
27728 struct glyph_row *cursor_row;
27729 struct glyph *cursor_glyph;
27730 enum draw_glyphs_face hl;
27732 /* No cursor displayed or row invalidated => nothing to do on the
27733 screen. */
27734 if (w->phys_cursor_type == NO_CURSOR)
27735 goto mark_cursor_off;
27737 /* VPOS >= active_glyphs->nrows means that window has been resized.
27738 Don't bother to erase the cursor. */
27739 if (vpos >= active_glyphs->nrows)
27740 goto mark_cursor_off;
27742 /* If row containing cursor is marked invalid, there is nothing we
27743 can do. */
27744 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27745 if (!cursor_row->enabled_p)
27746 goto mark_cursor_off;
27748 /* If line spacing is > 0, old cursor may only be partially visible in
27749 window after split-window. So adjust visible height. */
27750 cursor_row->visible_height = min (cursor_row->visible_height,
27751 window_text_bottom_y (w) - cursor_row->y);
27753 /* If row is completely invisible, don't attempt to delete a cursor which
27754 isn't there. This can happen if cursor is at top of a window, and
27755 we switch to a buffer with a header line in that window. */
27756 if (cursor_row->visible_height <= 0)
27757 goto mark_cursor_off;
27759 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27760 if (cursor_row->cursor_in_fringe_p)
27762 cursor_row->cursor_in_fringe_p = false;
27763 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27764 goto mark_cursor_off;
27767 /* This can happen when the new row is shorter than the old one.
27768 In this case, either draw_glyphs or clear_end_of_line
27769 should have cleared the cursor. Note that we wouldn't be
27770 able to erase the cursor in this case because we don't have a
27771 cursor glyph at hand. */
27772 if ((cursor_row->reversed_p
27773 ? (w->phys_cursor.hpos < 0)
27774 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27775 goto mark_cursor_off;
27777 /* When the window is hscrolled, cursor hpos can legitimately be out
27778 of bounds, but we draw the cursor at the corresponding window
27779 margin in that case. */
27780 if (!cursor_row->reversed_p && hpos < 0)
27781 hpos = 0;
27782 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27783 hpos = cursor_row->used[TEXT_AREA] - 1;
27785 /* If the cursor is in the mouse face area, redisplay that when
27786 we clear the cursor. */
27787 if (! NILP (hlinfo->mouse_face_window)
27788 && coords_in_mouse_face_p (w, hpos, vpos)
27789 /* Don't redraw the cursor's spot in mouse face if it is at the
27790 end of a line (on a newline). The cursor appears there, but
27791 mouse highlighting does not. */
27792 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27793 mouse_face_here_p = true;
27795 /* Maybe clear the display under the cursor. */
27796 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27798 int x, y;
27799 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27800 int width;
27802 cursor_glyph = get_phys_cursor_glyph (w);
27803 if (cursor_glyph == NULL)
27804 goto mark_cursor_off;
27806 width = cursor_glyph->pixel_width;
27807 x = w->phys_cursor.x;
27808 if (x < 0)
27810 width += x;
27811 x = 0;
27813 width = min (width, window_box_width (w, TEXT_AREA) - x);
27814 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27815 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27817 if (width > 0)
27818 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27821 /* Erase the cursor by redrawing the character underneath it. */
27822 if (mouse_face_here_p)
27823 hl = DRAW_MOUSE_FACE;
27824 else
27825 hl = DRAW_NORMAL_TEXT;
27826 draw_phys_cursor_glyph (w, cursor_row, hl);
27828 mark_cursor_off:
27829 w->phys_cursor_on_p = false;
27830 w->phys_cursor_type = NO_CURSOR;
27834 /* Display or clear cursor of window W. If !ON, clear the cursor.
27835 If ON, display the cursor; where to put the cursor is specified by
27836 HPOS, VPOS, X and Y. */
27838 void
27839 display_and_set_cursor (struct window *w, bool on,
27840 int hpos, int vpos, int x, int y)
27842 struct frame *f = XFRAME (w->frame);
27843 int new_cursor_type;
27844 int new_cursor_width;
27845 bool active_cursor;
27846 struct glyph_row *glyph_row;
27847 struct glyph *glyph;
27849 /* This is pointless on invisible frames, and dangerous on garbaged
27850 windows and frames; in the latter case, the frame or window may
27851 be in the midst of changing its size, and x and y may be off the
27852 window. */
27853 if (! FRAME_VISIBLE_P (f)
27854 || FRAME_GARBAGED_P (f)
27855 || vpos >= w->current_matrix->nrows
27856 || hpos >= w->current_matrix->matrix_w)
27857 return;
27859 /* If cursor is off and we want it off, return quickly. */
27860 if (!on && !w->phys_cursor_on_p)
27861 return;
27863 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27864 /* If cursor row is not enabled, we don't really know where to
27865 display the cursor. */
27866 if (!glyph_row->enabled_p)
27868 w->phys_cursor_on_p = false;
27869 return;
27872 glyph = NULL;
27873 if (!glyph_row->exact_window_width_line_p
27874 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27875 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27877 eassert (input_blocked_p ());
27879 /* Set new_cursor_type to the cursor we want to be displayed. */
27880 new_cursor_type = get_window_cursor_type (w, glyph,
27881 &new_cursor_width, &active_cursor);
27883 /* If cursor is currently being shown and we don't want it to be or
27884 it is in the wrong place, or the cursor type is not what we want,
27885 erase it. */
27886 if (w->phys_cursor_on_p
27887 && (!on
27888 || w->phys_cursor.x != x
27889 || w->phys_cursor.y != y
27890 /* HPOS can be negative in R2L rows whose
27891 exact_window_width_line_p flag is set (i.e. their newline
27892 would "overflow into the fringe"). */
27893 || hpos < 0
27894 || new_cursor_type != w->phys_cursor_type
27895 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27896 && new_cursor_width != w->phys_cursor_width)))
27897 erase_phys_cursor (w);
27899 /* Don't check phys_cursor_on_p here because that flag is only set
27900 to false in some cases where we know that the cursor has been
27901 completely erased, to avoid the extra work of erasing the cursor
27902 twice. In other words, phys_cursor_on_p can be true and the cursor
27903 still not be visible, or it has only been partly erased. */
27904 if (on)
27906 w->phys_cursor_ascent = glyph_row->ascent;
27907 w->phys_cursor_height = glyph_row->height;
27909 /* Set phys_cursor_.* before x_draw_.* is called because some
27910 of them may need the information. */
27911 w->phys_cursor.x = x;
27912 w->phys_cursor.y = glyph_row->y;
27913 w->phys_cursor.hpos = hpos;
27914 w->phys_cursor.vpos = vpos;
27917 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27918 new_cursor_type, new_cursor_width,
27919 on, active_cursor);
27923 /* Switch the display of W's cursor on or off, according to the value
27924 of ON. */
27926 static void
27927 update_window_cursor (struct window *w, bool on)
27929 /* Don't update cursor in windows whose frame is in the process
27930 of being deleted. */
27931 if (w->current_matrix)
27933 int hpos = w->phys_cursor.hpos;
27934 int vpos = w->phys_cursor.vpos;
27935 struct glyph_row *row;
27937 if (vpos >= w->current_matrix->nrows
27938 || hpos >= w->current_matrix->matrix_w)
27939 return;
27941 row = MATRIX_ROW (w->current_matrix, vpos);
27943 /* When the window is hscrolled, cursor hpos can legitimately be
27944 out of bounds, but we draw the cursor at the corresponding
27945 window margin in that case. */
27946 if (!row->reversed_p && hpos < 0)
27947 hpos = 0;
27948 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27949 hpos = row->used[TEXT_AREA] - 1;
27951 block_input ();
27952 display_and_set_cursor (w, on, hpos, vpos,
27953 w->phys_cursor.x, w->phys_cursor.y);
27954 unblock_input ();
27959 /* Call update_window_cursor with parameter ON_P on all leaf windows
27960 in the window tree rooted at W. */
27962 static void
27963 update_cursor_in_window_tree (struct window *w, bool on_p)
27965 while (w)
27967 if (WINDOWP (w->contents))
27968 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27969 else
27970 update_window_cursor (w, on_p);
27972 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27977 /* EXPORT:
27978 Display the cursor on window W, or clear it, according to ON_P.
27979 Don't change the cursor's position. */
27981 void
27982 x_update_cursor (struct frame *f, bool on_p)
27984 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27988 /* EXPORT:
27989 Clear the cursor of window W to background color, and mark the
27990 cursor as not shown. This is used when the text where the cursor
27991 is about to be rewritten. */
27993 void
27994 x_clear_cursor (struct window *w)
27996 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27997 update_window_cursor (w, false);
28000 #endif /* HAVE_WINDOW_SYSTEM */
28002 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28003 and MSDOS. */
28004 static void
28005 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28006 int start_hpos, int end_hpos,
28007 enum draw_glyphs_face draw)
28009 #ifdef HAVE_WINDOW_SYSTEM
28010 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28012 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28013 return;
28015 #endif
28016 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28017 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28018 #endif
28021 /* Display the active region described by mouse_face_* according to DRAW. */
28023 static void
28024 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28026 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28027 struct frame *f = XFRAME (WINDOW_FRAME (w));
28029 if (/* If window is in the process of being destroyed, don't bother
28030 to do anything. */
28031 w->current_matrix != NULL
28032 /* Don't update mouse highlight if hidden. */
28033 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28034 /* Recognize when we are called to operate on rows that don't exist
28035 anymore. This can happen when a window is split. */
28036 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28038 bool phys_cursor_on_p = w->phys_cursor_on_p;
28039 struct glyph_row *row, *first, *last;
28041 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28042 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28044 for (row = first; row <= last && row->enabled_p; ++row)
28046 int start_hpos, end_hpos, start_x;
28048 /* For all but the first row, the highlight starts at column 0. */
28049 if (row == first)
28051 /* R2L rows have BEG and END in reversed order, but the
28052 screen drawing geometry is always left to right. So
28053 we need to mirror the beginning and end of the
28054 highlighted area in R2L rows. */
28055 if (!row->reversed_p)
28057 start_hpos = hlinfo->mouse_face_beg_col;
28058 start_x = hlinfo->mouse_face_beg_x;
28060 else if (row == last)
28062 start_hpos = hlinfo->mouse_face_end_col;
28063 start_x = hlinfo->mouse_face_end_x;
28065 else
28067 start_hpos = 0;
28068 start_x = 0;
28071 else if (row->reversed_p && row == last)
28073 start_hpos = hlinfo->mouse_face_end_col;
28074 start_x = hlinfo->mouse_face_end_x;
28076 else
28078 start_hpos = 0;
28079 start_x = 0;
28082 if (row == last)
28084 if (!row->reversed_p)
28085 end_hpos = hlinfo->mouse_face_end_col;
28086 else if (row == first)
28087 end_hpos = hlinfo->mouse_face_beg_col;
28088 else
28090 end_hpos = row->used[TEXT_AREA];
28091 if (draw == DRAW_NORMAL_TEXT)
28092 row->fill_line_p = true; /* Clear to end of line. */
28095 else if (row->reversed_p && row == first)
28096 end_hpos = hlinfo->mouse_face_beg_col;
28097 else
28099 end_hpos = row->used[TEXT_AREA];
28100 if (draw == DRAW_NORMAL_TEXT)
28101 row->fill_line_p = true; /* Clear to end of line. */
28104 if (end_hpos > start_hpos)
28106 draw_row_with_mouse_face (w, start_x, row,
28107 start_hpos, end_hpos, draw);
28109 row->mouse_face_p
28110 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28114 #ifdef HAVE_WINDOW_SYSTEM
28115 /* When we've written over the cursor, arrange for it to
28116 be displayed again. */
28117 if (FRAME_WINDOW_P (f)
28118 && phys_cursor_on_p && !w->phys_cursor_on_p)
28120 int hpos = w->phys_cursor.hpos;
28122 /* When the window is hscrolled, cursor hpos can legitimately be
28123 out of bounds, but we draw the cursor at the corresponding
28124 window margin in that case. */
28125 if (!row->reversed_p && hpos < 0)
28126 hpos = 0;
28127 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28128 hpos = row->used[TEXT_AREA] - 1;
28130 block_input ();
28131 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28132 w->phys_cursor.x, w->phys_cursor.y);
28133 unblock_input ();
28135 #endif /* HAVE_WINDOW_SYSTEM */
28138 #ifdef HAVE_WINDOW_SYSTEM
28139 /* Change the mouse cursor. */
28140 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28142 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28143 if (draw == DRAW_NORMAL_TEXT
28144 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28145 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28146 else
28147 #endif
28148 if (draw == DRAW_MOUSE_FACE)
28149 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28150 else
28151 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28153 #endif /* HAVE_WINDOW_SYSTEM */
28156 /* EXPORT:
28157 Clear out the mouse-highlighted active region.
28158 Redraw it un-highlighted first. Value is true if mouse
28159 face was actually drawn unhighlighted. */
28161 bool
28162 clear_mouse_face (Mouse_HLInfo *hlinfo)
28164 bool cleared
28165 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28166 if (cleared)
28167 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28168 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28169 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28170 hlinfo->mouse_face_window = Qnil;
28171 hlinfo->mouse_face_overlay = Qnil;
28172 return cleared;
28175 /* Return true if the coordinates HPOS and VPOS on windows W are
28176 within the mouse face on that window. */
28177 static bool
28178 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28180 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28182 /* Quickly resolve the easy cases. */
28183 if (!(WINDOWP (hlinfo->mouse_face_window)
28184 && XWINDOW (hlinfo->mouse_face_window) == w))
28185 return false;
28186 if (vpos < hlinfo->mouse_face_beg_row
28187 || vpos > hlinfo->mouse_face_end_row)
28188 return false;
28189 if (vpos > hlinfo->mouse_face_beg_row
28190 && vpos < hlinfo->mouse_face_end_row)
28191 return true;
28193 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28195 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28197 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28198 return true;
28200 else if ((vpos == hlinfo->mouse_face_beg_row
28201 && hpos >= hlinfo->mouse_face_beg_col)
28202 || (vpos == hlinfo->mouse_face_end_row
28203 && hpos < hlinfo->mouse_face_end_col))
28204 return true;
28206 else
28208 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28210 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28211 return true;
28213 else if ((vpos == hlinfo->mouse_face_beg_row
28214 && hpos <= hlinfo->mouse_face_beg_col)
28215 || (vpos == hlinfo->mouse_face_end_row
28216 && hpos > hlinfo->mouse_face_end_col))
28217 return true;
28219 return false;
28223 /* EXPORT:
28224 True if physical cursor of window W is within mouse face. */
28226 bool
28227 cursor_in_mouse_face_p (struct window *w)
28229 int hpos = w->phys_cursor.hpos;
28230 int vpos = w->phys_cursor.vpos;
28231 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28233 /* When the window is hscrolled, cursor hpos can legitimately be out
28234 of bounds, but we draw the cursor at the corresponding window
28235 margin in that case. */
28236 if (!row->reversed_p && hpos < 0)
28237 hpos = 0;
28238 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28239 hpos = row->used[TEXT_AREA] - 1;
28241 return coords_in_mouse_face_p (w, hpos, vpos);
28246 /* Find the glyph rows START_ROW and END_ROW of window W that display
28247 characters between buffer positions START_CHARPOS and END_CHARPOS
28248 (excluding END_CHARPOS). DISP_STRING is a display string that
28249 covers these buffer positions. This is similar to
28250 row_containing_pos, but is more accurate when bidi reordering makes
28251 buffer positions change non-linearly with glyph rows. */
28252 static void
28253 rows_from_pos_range (struct window *w,
28254 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28255 Lisp_Object disp_string,
28256 struct glyph_row **start, struct glyph_row **end)
28258 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28259 int last_y = window_text_bottom_y (w);
28260 struct glyph_row *row;
28262 *start = NULL;
28263 *end = NULL;
28265 while (!first->enabled_p
28266 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28267 first++;
28269 /* Find the START row. */
28270 for (row = first;
28271 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28272 row++)
28274 /* A row can potentially be the START row if the range of the
28275 characters it displays intersects the range
28276 [START_CHARPOS..END_CHARPOS). */
28277 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28278 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28279 /* See the commentary in row_containing_pos, for the
28280 explanation of the complicated way to check whether
28281 some position is beyond the end of the characters
28282 displayed by a row. */
28283 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28284 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28285 && !row->ends_at_zv_p
28286 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28287 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28288 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28289 && !row->ends_at_zv_p
28290 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28292 /* Found a candidate row. Now make sure at least one of the
28293 glyphs it displays has a charpos from the range
28294 [START_CHARPOS..END_CHARPOS).
28296 This is not obvious because bidi reordering could make
28297 buffer positions of a row be 1,2,3,102,101,100, and if we
28298 want to highlight characters in [50..60), we don't want
28299 this row, even though [50..60) does intersect [1..103),
28300 the range of character positions given by the row's start
28301 and end positions. */
28302 struct glyph *g = row->glyphs[TEXT_AREA];
28303 struct glyph *e = g + row->used[TEXT_AREA];
28305 while (g < e)
28307 if (((BUFFERP (g->object) || NILP (g->object))
28308 && start_charpos <= g->charpos && g->charpos < end_charpos)
28309 /* A glyph that comes from DISP_STRING is by
28310 definition to be highlighted. */
28311 || EQ (g->object, disp_string))
28312 *start = row;
28313 g++;
28315 if (*start)
28316 break;
28320 /* Find the END row. */
28321 if (!*start
28322 /* If the last row is partially visible, start looking for END
28323 from that row, instead of starting from FIRST. */
28324 && !(row->enabled_p
28325 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28326 row = first;
28327 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28329 struct glyph_row *next = row + 1;
28330 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28332 if (!next->enabled_p
28333 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28334 /* The first row >= START whose range of displayed characters
28335 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28336 is the row END + 1. */
28337 || (start_charpos < next_start
28338 && end_charpos < next_start)
28339 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28340 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28341 && !next->ends_at_zv_p
28342 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28343 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28344 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28345 && !next->ends_at_zv_p
28346 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28348 *end = row;
28349 break;
28351 else
28353 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28354 but none of the characters it displays are in the range, it is
28355 also END + 1. */
28356 struct glyph *g = next->glyphs[TEXT_AREA];
28357 struct glyph *s = g;
28358 struct glyph *e = g + next->used[TEXT_AREA];
28360 while (g < e)
28362 if (((BUFFERP (g->object) || NILP (g->object))
28363 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28364 /* If the buffer position of the first glyph in
28365 the row is equal to END_CHARPOS, it means
28366 the last character to be highlighted is the
28367 newline of ROW, and we must consider NEXT as
28368 END, not END+1. */
28369 || (((!next->reversed_p && g == s)
28370 || (next->reversed_p && g == e - 1))
28371 && (g->charpos == end_charpos
28372 /* Special case for when NEXT is an
28373 empty line at ZV. */
28374 || (g->charpos == -1
28375 && !row->ends_at_zv_p
28376 && next_start == end_charpos)))))
28377 /* A glyph that comes from DISP_STRING is by
28378 definition to be highlighted. */
28379 || EQ (g->object, disp_string))
28380 break;
28381 g++;
28383 if (g == e)
28385 *end = row;
28386 break;
28388 /* The first row that ends at ZV must be the last to be
28389 highlighted. */
28390 else if (next->ends_at_zv_p)
28392 *end = next;
28393 break;
28399 /* This function sets the mouse_face_* elements of HLINFO, assuming
28400 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28401 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28402 for the overlay or run of text properties specifying the mouse
28403 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28404 before-string and after-string that must also be highlighted.
28405 DISP_STRING, if non-nil, is a display string that may cover some
28406 or all of the highlighted text. */
28408 static void
28409 mouse_face_from_buffer_pos (Lisp_Object window,
28410 Mouse_HLInfo *hlinfo,
28411 ptrdiff_t mouse_charpos,
28412 ptrdiff_t start_charpos,
28413 ptrdiff_t end_charpos,
28414 Lisp_Object before_string,
28415 Lisp_Object after_string,
28416 Lisp_Object disp_string)
28418 struct window *w = XWINDOW (window);
28419 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28420 struct glyph_row *r1, *r2;
28421 struct glyph *glyph, *end;
28422 ptrdiff_t ignore, pos;
28423 int x;
28425 eassert (NILP (disp_string) || STRINGP (disp_string));
28426 eassert (NILP (before_string) || STRINGP (before_string));
28427 eassert (NILP (after_string) || STRINGP (after_string));
28429 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28430 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28431 if (r1 == NULL)
28432 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28433 /* If the before-string or display-string contains newlines,
28434 rows_from_pos_range skips to its last row. Move back. */
28435 if (!NILP (before_string) || !NILP (disp_string))
28437 struct glyph_row *prev;
28438 while ((prev = r1 - 1, prev >= first)
28439 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28440 && prev->used[TEXT_AREA] > 0)
28442 struct glyph *beg = prev->glyphs[TEXT_AREA];
28443 glyph = beg + prev->used[TEXT_AREA];
28444 while (--glyph >= beg && NILP (glyph->object));
28445 if (glyph < beg
28446 || !(EQ (glyph->object, before_string)
28447 || EQ (glyph->object, disp_string)))
28448 break;
28449 r1 = prev;
28452 if (r2 == NULL)
28454 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28455 hlinfo->mouse_face_past_end = true;
28457 else if (!NILP (after_string))
28459 /* If the after-string has newlines, advance to its last row. */
28460 struct glyph_row *next;
28461 struct glyph_row *last
28462 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28464 for (next = r2 + 1;
28465 next <= last
28466 && next->used[TEXT_AREA] > 0
28467 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28468 ++next)
28469 r2 = next;
28471 /* The rest of the display engine assumes that mouse_face_beg_row is
28472 either above mouse_face_end_row or identical to it. But with
28473 bidi-reordered continued lines, the row for START_CHARPOS could
28474 be below the row for END_CHARPOS. If so, swap the rows and store
28475 them in correct order. */
28476 if (r1->y > r2->y)
28478 struct glyph_row *tem = r2;
28480 r2 = r1;
28481 r1 = tem;
28484 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28485 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28487 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28488 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28489 could be anywhere in the row and in any order. The strategy
28490 below is to find the leftmost and the rightmost glyph that
28491 belongs to either of these 3 strings, or whose position is
28492 between START_CHARPOS and END_CHARPOS, and highlight all the
28493 glyphs between those two. This may cover more than just the text
28494 between START_CHARPOS and END_CHARPOS if the range of characters
28495 strides the bidi level boundary, e.g. if the beginning is in R2L
28496 text while the end is in L2R text or vice versa. */
28497 if (!r1->reversed_p)
28499 /* This row is in a left to right paragraph. Scan it left to
28500 right. */
28501 glyph = r1->glyphs[TEXT_AREA];
28502 end = glyph + r1->used[TEXT_AREA];
28503 x = r1->x;
28505 /* Skip truncation glyphs at the start of the glyph row. */
28506 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28507 for (; glyph < end
28508 && NILP (glyph->object)
28509 && glyph->charpos < 0;
28510 ++glyph)
28511 x += glyph->pixel_width;
28513 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28514 or DISP_STRING, and the first glyph from buffer whose
28515 position is between START_CHARPOS and END_CHARPOS. */
28516 for (; glyph < end
28517 && !NILP (glyph->object)
28518 && !EQ (glyph->object, disp_string)
28519 && !(BUFFERP (glyph->object)
28520 && (glyph->charpos >= start_charpos
28521 && glyph->charpos < end_charpos));
28522 ++glyph)
28524 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28525 are present at buffer positions between START_CHARPOS and
28526 END_CHARPOS, or if they come from an overlay. */
28527 if (EQ (glyph->object, before_string))
28529 pos = string_buffer_position (before_string,
28530 start_charpos);
28531 /* If pos == 0, it means before_string came from an
28532 overlay, not from a buffer position. */
28533 if (!pos || (pos >= start_charpos && pos < end_charpos))
28534 break;
28536 else if (EQ (glyph->object, after_string))
28538 pos = string_buffer_position (after_string, end_charpos);
28539 if (!pos || (pos >= start_charpos && pos < end_charpos))
28540 break;
28542 x += glyph->pixel_width;
28544 hlinfo->mouse_face_beg_x = x;
28545 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28547 else
28549 /* This row is in a right to left paragraph. Scan it right to
28550 left. */
28551 struct glyph *g;
28553 end = r1->glyphs[TEXT_AREA] - 1;
28554 glyph = end + r1->used[TEXT_AREA];
28556 /* Skip truncation glyphs at the start of the glyph row. */
28557 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28558 for (; glyph > end
28559 && NILP (glyph->object)
28560 && glyph->charpos < 0;
28561 --glyph)
28564 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28565 or DISP_STRING, and the first glyph from buffer whose
28566 position is between START_CHARPOS and END_CHARPOS. */
28567 for (; glyph > end
28568 && !NILP (glyph->object)
28569 && !EQ (glyph->object, disp_string)
28570 && !(BUFFERP (glyph->object)
28571 && (glyph->charpos >= start_charpos
28572 && glyph->charpos < end_charpos));
28573 --glyph)
28575 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28576 are present at buffer positions between START_CHARPOS and
28577 END_CHARPOS, or if they come from an overlay. */
28578 if (EQ (glyph->object, before_string))
28580 pos = string_buffer_position (before_string, start_charpos);
28581 /* If pos == 0, it means before_string came from an
28582 overlay, not from a buffer position. */
28583 if (!pos || (pos >= start_charpos && pos < end_charpos))
28584 break;
28586 else if (EQ (glyph->object, after_string))
28588 pos = string_buffer_position (after_string, end_charpos);
28589 if (!pos || (pos >= start_charpos && pos < end_charpos))
28590 break;
28594 glyph++; /* first glyph to the right of the highlighted area */
28595 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28596 x += g->pixel_width;
28597 hlinfo->mouse_face_beg_x = x;
28598 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28601 /* If the highlight ends in a different row, compute GLYPH and END
28602 for the end row. Otherwise, reuse the values computed above for
28603 the row where the highlight begins. */
28604 if (r2 != r1)
28606 if (!r2->reversed_p)
28608 glyph = r2->glyphs[TEXT_AREA];
28609 end = glyph + r2->used[TEXT_AREA];
28610 x = r2->x;
28612 else
28614 end = r2->glyphs[TEXT_AREA] - 1;
28615 glyph = end + r2->used[TEXT_AREA];
28619 if (!r2->reversed_p)
28621 /* Skip truncation and continuation glyphs near the end of the
28622 row, and also blanks and stretch glyphs inserted by
28623 extend_face_to_end_of_line. */
28624 while (end > glyph
28625 && NILP ((end - 1)->object))
28626 --end;
28627 /* Scan the rest of the glyph row from the end, looking for the
28628 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28629 DISP_STRING, or whose position is between START_CHARPOS
28630 and END_CHARPOS */
28631 for (--end;
28632 end > glyph
28633 && !NILP (end->object)
28634 && !EQ (end->object, disp_string)
28635 && !(BUFFERP (end->object)
28636 && (end->charpos >= start_charpos
28637 && end->charpos < end_charpos));
28638 --end)
28640 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28641 are present at buffer positions between START_CHARPOS and
28642 END_CHARPOS, or if they come from an overlay. */
28643 if (EQ (end->object, before_string))
28645 pos = string_buffer_position (before_string, start_charpos);
28646 if (!pos || (pos >= start_charpos && pos < end_charpos))
28647 break;
28649 else if (EQ (end->object, after_string))
28651 pos = string_buffer_position (after_string, end_charpos);
28652 if (!pos || (pos >= start_charpos && pos < end_charpos))
28653 break;
28656 /* Find the X coordinate of the last glyph to be highlighted. */
28657 for (; glyph <= end; ++glyph)
28658 x += glyph->pixel_width;
28660 hlinfo->mouse_face_end_x = x;
28661 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28663 else
28665 /* Skip truncation and continuation glyphs near the end of the
28666 row, and also blanks and stretch glyphs inserted by
28667 extend_face_to_end_of_line. */
28668 x = r2->x;
28669 end++;
28670 while (end < glyph
28671 && NILP (end->object))
28673 x += end->pixel_width;
28674 ++end;
28676 /* Scan the rest of the glyph row from the end, looking for the
28677 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28678 DISP_STRING, or whose position is between START_CHARPOS
28679 and END_CHARPOS */
28680 for ( ;
28681 end < glyph
28682 && !NILP (end->object)
28683 && !EQ (end->object, disp_string)
28684 && !(BUFFERP (end->object)
28685 && (end->charpos >= start_charpos
28686 && end->charpos < end_charpos));
28687 ++end)
28689 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28690 are present at buffer positions between START_CHARPOS and
28691 END_CHARPOS, or if they come from an overlay. */
28692 if (EQ (end->object, before_string))
28694 pos = string_buffer_position (before_string, start_charpos);
28695 if (!pos || (pos >= start_charpos && pos < end_charpos))
28696 break;
28698 else if (EQ (end->object, after_string))
28700 pos = string_buffer_position (after_string, end_charpos);
28701 if (!pos || (pos >= start_charpos && pos < end_charpos))
28702 break;
28704 x += end->pixel_width;
28706 /* If we exited the above loop because we arrived at the last
28707 glyph of the row, and its buffer position is still not in
28708 range, it means the last character in range is the preceding
28709 newline. Bump the end column and x values to get past the
28710 last glyph. */
28711 if (end == glyph
28712 && BUFFERP (end->object)
28713 && (end->charpos < start_charpos
28714 || end->charpos >= end_charpos))
28716 x += end->pixel_width;
28717 ++end;
28719 hlinfo->mouse_face_end_x = x;
28720 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28723 hlinfo->mouse_face_window = window;
28724 hlinfo->mouse_face_face_id
28725 = face_at_buffer_position (w, mouse_charpos, &ignore,
28726 mouse_charpos + 1,
28727 !hlinfo->mouse_face_hidden, -1);
28728 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28731 /* The following function is not used anymore (replaced with
28732 mouse_face_from_string_pos), but I leave it here for the time
28733 being, in case someone would. */
28735 #if false /* not used */
28737 /* Find the position of the glyph for position POS in OBJECT in
28738 window W's current matrix, and return in *X, *Y the pixel
28739 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28741 RIGHT_P means return the position of the right edge of the glyph.
28742 !RIGHT_P means return the left edge position.
28744 If no glyph for POS exists in the matrix, return the position of
28745 the glyph with the next smaller position that is in the matrix, if
28746 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28747 exists in the matrix, return the position of the glyph with the
28748 next larger position in OBJECT.
28750 Value is true if a glyph was found. */
28752 static bool
28753 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28754 int *hpos, int *vpos, int *x, int *y, bool right_p)
28756 int yb = window_text_bottom_y (w);
28757 struct glyph_row *r;
28758 struct glyph *best_glyph = NULL;
28759 struct glyph_row *best_row = NULL;
28760 int best_x = 0;
28762 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28763 r->enabled_p && r->y < yb;
28764 ++r)
28766 struct glyph *g = r->glyphs[TEXT_AREA];
28767 struct glyph *e = g + r->used[TEXT_AREA];
28768 int gx;
28770 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28771 if (EQ (g->object, object))
28773 if (g->charpos == pos)
28775 best_glyph = g;
28776 best_x = gx;
28777 best_row = r;
28778 goto found;
28780 else if (best_glyph == NULL
28781 || ((eabs (g->charpos - pos)
28782 < eabs (best_glyph->charpos - pos))
28783 && (right_p
28784 ? g->charpos < pos
28785 : g->charpos > pos)))
28787 best_glyph = g;
28788 best_x = gx;
28789 best_row = r;
28794 found:
28796 if (best_glyph)
28798 *x = best_x;
28799 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28801 if (right_p)
28803 *x += best_glyph->pixel_width;
28804 ++*hpos;
28807 *y = best_row->y;
28808 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28811 return best_glyph != NULL;
28813 #endif /* not used */
28815 /* Find the positions of the first and the last glyphs in window W's
28816 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28817 (assumed to be a string), and return in HLINFO's mouse_face_*
28818 members the pixel and column/row coordinates of those glyphs. */
28820 static void
28821 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28822 Lisp_Object object,
28823 ptrdiff_t startpos, ptrdiff_t endpos)
28825 int yb = window_text_bottom_y (w);
28826 struct glyph_row *r;
28827 struct glyph *g, *e;
28828 int gx;
28829 bool found = false;
28831 /* Find the glyph row with at least one position in the range
28832 [STARTPOS..ENDPOS), and the first glyph in that row whose
28833 position belongs to that range. */
28834 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28835 r->enabled_p && r->y < yb;
28836 ++r)
28838 if (!r->reversed_p)
28840 g = r->glyphs[TEXT_AREA];
28841 e = g + r->used[TEXT_AREA];
28842 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28843 if (EQ (g->object, object)
28844 && startpos <= g->charpos && g->charpos < endpos)
28846 hlinfo->mouse_face_beg_row
28847 = MATRIX_ROW_VPOS (r, w->current_matrix);
28848 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28849 hlinfo->mouse_face_beg_x = gx;
28850 found = true;
28851 break;
28854 else
28856 struct glyph *g1;
28858 e = r->glyphs[TEXT_AREA];
28859 g = e + r->used[TEXT_AREA];
28860 for ( ; g > e; --g)
28861 if (EQ ((g-1)->object, object)
28862 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28864 hlinfo->mouse_face_beg_row
28865 = MATRIX_ROW_VPOS (r, w->current_matrix);
28866 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28867 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28868 gx += g1->pixel_width;
28869 hlinfo->mouse_face_beg_x = gx;
28870 found = true;
28871 break;
28874 if (found)
28875 break;
28878 if (!found)
28879 return;
28881 /* Starting with the next row, look for the first row which does NOT
28882 include any glyphs whose positions are in the range. */
28883 for (++r; r->enabled_p && r->y < yb; ++r)
28885 g = r->glyphs[TEXT_AREA];
28886 e = g + r->used[TEXT_AREA];
28887 found = false;
28888 for ( ; g < e; ++g)
28889 if (EQ (g->object, object)
28890 && startpos <= g->charpos && g->charpos < endpos)
28892 found = true;
28893 break;
28895 if (!found)
28896 break;
28899 /* The highlighted region ends on the previous row. */
28900 r--;
28902 /* Set the end row. */
28903 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28905 /* Compute and set the end column and the end column's horizontal
28906 pixel coordinate. */
28907 if (!r->reversed_p)
28909 g = r->glyphs[TEXT_AREA];
28910 e = g + r->used[TEXT_AREA];
28911 for ( ; e > g; --e)
28912 if (EQ ((e-1)->object, object)
28913 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28914 break;
28915 hlinfo->mouse_face_end_col = e - g;
28917 for (gx = r->x; g < e; ++g)
28918 gx += g->pixel_width;
28919 hlinfo->mouse_face_end_x = gx;
28921 else
28923 e = r->glyphs[TEXT_AREA];
28924 g = e + r->used[TEXT_AREA];
28925 for (gx = r->x ; e < g; ++e)
28927 if (EQ (e->object, object)
28928 && startpos <= e->charpos && e->charpos < endpos)
28929 break;
28930 gx += e->pixel_width;
28932 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28933 hlinfo->mouse_face_end_x = gx;
28937 #ifdef HAVE_WINDOW_SYSTEM
28939 /* See if position X, Y is within a hot-spot of an image. */
28941 static bool
28942 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28944 if (!CONSP (hot_spot))
28945 return false;
28947 if (EQ (XCAR (hot_spot), Qrect))
28949 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28950 Lisp_Object rect = XCDR (hot_spot);
28951 Lisp_Object tem;
28952 if (!CONSP (rect))
28953 return false;
28954 if (!CONSP (XCAR (rect)))
28955 return false;
28956 if (!CONSP (XCDR (rect)))
28957 return false;
28958 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28959 return false;
28960 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28961 return false;
28962 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28963 return false;
28964 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28965 return false;
28966 return true;
28968 else if (EQ (XCAR (hot_spot), Qcircle))
28970 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28971 Lisp_Object circ = XCDR (hot_spot);
28972 Lisp_Object lr, lx0, ly0;
28973 if (CONSP (circ)
28974 && CONSP (XCAR (circ))
28975 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28976 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28977 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28979 double r = XFLOATINT (lr);
28980 double dx = XINT (lx0) - x;
28981 double dy = XINT (ly0) - y;
28982 return (dx * dx + dy * dy <= r * r);
28985 else if (EQ (XCAR (hot_spot), Qpoly))
28987 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28988 if (VECTORP (XCDR (hot_spot)))
28990 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28991 Lisp_Object *poly = v->contents;
28992 ptrdiff_t n = v->header.size;
28993 ptrdiff_t i;
28994 bool inside = false;
28995 Lisp_Object lx, ly;
28996 int x0, y0;
28998 /* Need an even number of coordinates, and at least 3 edges. */
28999 if (n < 6 || n & 1)
29000 return false;
29002 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29003 If count is odd, we are inside polygon. Pixels on edges
29004 may or may not be included depending on actual geometry of the
29005 polygon. */
29006 if ((lx = poly[n-2], !INTEGERP (lx))
29007 || (ly = poly[n-1], !INTEGERP (lx)))
29008 return false;
29009 x0 = XINT (lx), y0 = XINT (ly);
29010 for (i = 0; i < n; i += 2)
29012 int x1 = x0, y1 = y0;
29013 if ((lx = poly[i], !INTEGERP (lx))
29014 || (ly = poly[i+1], !INTEGERP (ly)))
29015 return false;
29016 x0 = XINT (lx), y0 = XINT (ly);
29018 /* Does this segment cross the X line? */
29019 if (x0 >= x)
29021 if (x1 >= x)
29022 continue;
29024 else if (x1 < x)
29025 continue;
29026 if (y > y0 && y > y1)
29027 continue;
29028 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29029 inside = !inside;
29031 return inside;
29034 return false;
29037 Lisp_Object
29038 find_hot_spot (Lisp_Object map, int x, int y)
29040 while (CONSP (map))
29042 if (CONSP (XCAR (map))
29043 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29044 return XCAR (map);
29045 map = XCDR (map);
29048 return Qnil;
29051 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29052 3, 3, 0,
29053 doc: /* Lookup in image map MAP coordinates X and Y.
29054 An image map is an alist where each element has the format (AREA ID PLIST).
29055 An AREA is specified as either a rectangle, a circle, or a polygon:
29056 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29057 pixel coordinates of the upper left and bottom right corners.
29058 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29059 and the radius of the circle; r may be a float or integer.
29060 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29061 vector describes one corner in the polygon.
29062 Returns the alist element for the first matching AREA in MAP. */)
29063 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29065 if (NILP (map))
29066 return Qnil;
29068 CHECK_NUMBER (x);
29069 CHECK_NUMBER (y);
29071 return find_hot_spot (map,
29072 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29073 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29077 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29078 static void
29079 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29081 /* Do not change cursor shape while dragging mouse. */
29082 if (!NILP (do_mouse_tracking))
29083 return;
29085 if (!NILP (pointer))
29087 if (EQ (pointer, Qarrow))
29088 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29089 else if (EQ (pointer, Qhand))
29090 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29091 else if (EQ (pointer, Qtext))
29092 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29093 else if (EQ (pointer, intern ("hdrag")))
29094 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29095 else if (EQ (pointer, intern ("nhdrag")))
29096 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29097 #ifdef HAVE_X_WINDOWS
29098 else if (EQ (pointer, intern ("vdrag")))
29099 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29100 #endif
29101 else if (EQ (pointer, intern ("hourglass")))
29102 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29103 else if (EQ (pointer, Qmodeline))
29104 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29105 else
29106 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29109 if (cursor != No_Cursor)
29110 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29113 #endif /* HAVE_WINDOW_SYSTEM */
29115 /* Take proper action when mouse has moved to the mode or header line
29116 or marginal area AREA of window W, x-position X and y-position Y.
29117 X is relative to the start of the text display area of W, so the
29118 width of bitmap areas and scroll bars must be subtracted to get a
29119 position relative to the start of the mode line. */
29121 static void
29122 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29123 enum window_part area)
29125 struct window *w = XWINDOW (window);
29126 struct frame *f = XFRAME (w->frame);
29127 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29128 #ifdef HAVE_WINDOW_SYSTEM
29129 Display_Info *dpyinfo;
29130 #endif
29131 Cursor cursor = No_Cursor;
29132 Lisp_Object pointer = Qnil;
29133 int dx, dy, width, height;
29134 ptrdiff_t charpos;
29135 Lisp_Object string, object = Qnil;
29136 Lisp_Object pos IF_LINT (= Qnil), help;
29138 Lisp_Object mouse_face;
29139 int original_x_pixel = x;
29140 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29141 struct glyph_row *row IF_LINT (= 0);
29143 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29145 int x0;
29146 struct glyph *end;
29148 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29149 returns them in row/column units! */
29150 string = mode_line_string (w, area, &x, &y, &charpos,
29151 &object, &dx, &dy, &width, &height);
29153 row = (area == ON_MODE_LINE
29154 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29155 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29157 /* Find the glyph under the mouse pointer. */
29158 if (row->mode_line_p && row->enabled_p)
29160 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29161 end = glyph + row->used[TEXT_AREA];
29163 for (x0 = original_x_pixel;
29164 glyph < end && x0 >= glyph->pixel_width;
29165 ++glyph)
29166 x0 -= glyph->pixel_width;
29168 if (glyph >= end)
29169 glyph = NULL;
29172 else
29174 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29175 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29176 returns them in row/column units! */
29177 string = marginal_area_string (w, area, &x, &y, &charpos,
29178 &object, &dx, &dy, &width, &height);
29181 help = Qnil;
29183 #ifdef HAVE_WINDOW_SYSTEM
29184 if (IMAGEP (object))
29186 Lisp_Object image_map, hotspot;
29187 if ((image_map = Fplist_get (XCDR (object), QCmap),
29188 !NILP (image_map))
29189 && (hotspot = find_hot_spot (image_map, dx, dy),
29190 CONSP (hotspot))
29191 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29193 Lisp_Object plist;
29195 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29196 If so, we could look for mouse-enter, mouse-leave
29197 properties in PLIST (and do something...). */
29198 hotspot = XCDR (hotspot);
29199 if (CONSP (hotspot)
29200 && (plist = XCAR (hotspot), CONSP (plist)))
29202 pointer = Fplist_get (plist, Qpointer);
29203 if (NILP (pointer))
29204 pointer = Qhand;
29205 help = Fplist_get (plist, Qhelp_echo);
29206 if (!NILP (help))
29208 help_echo_string = help;
29209 XSETWINDOW (help_echo_window, w);
29210 help_echo_object = w->contents;
29211 help_echo_pos = charpos;
29215 if (NILP (pointer))
29216 pointer = Fplist_get (XCDR (object), QCpointer);
29218 #endif /* HAVE_WINDOW_SYSTEM */
29220 if (STRINGP (string))
29221 pos = make_number (charpos);
29223 /* Set the help text and mouse pointer. If the mouse is on a part
29224 of the mode line without any text (e.g. past the right edge of
29225 the mode line text), use the default help text and pointer. */
29226 if (STRINGP (string) || area == ON_MODE_LINE)
29228 /* Arrange to display the help by setting the global variables
29229 help_echo_string, help_echo_object, and help_echo_pos. */
29230 if (NILP (help))
29232 if (STRINGP (string))
29233 help = Fget_text_property (pos, Qhelp_echo, string);
29235 if (!NILP (help))
29237 help_echo_string = help;
29238 XSETWINDOW (help_echo_window, w);
29239 help_echo_object = string;
29240 help_echo_pos = charpos;
29242 else if (area == ON_MODE_LINE)
29244 Lisp_Object default_help
29245 = buffer_local_value (Qmode_line_default_help_echo,
29246 w->contents);
29248 if (STRINGP (default_help))
29250 help_echo_string = default_help;
29251 XSETWINDOW (help_echo_window, w);
29252 help_echo_object = Qnil;
29253 help_echo_pos = -1;
29258 #ifdef HAVE_WINDOW_SYSTEM
29259 /* Change the mouse pointer according to what is under it. */
29260 if (FRAME_WINDOW_P (f))
29262 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29263 || minibuf_level
29264 || NILP (Vresize_mini_windows));
29266 dpyinfo = FRAME_DISPLAY_INFO (f);
29267 if (STRINGP (string))
29269 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29271 if (NILP (pointer))
29272 pointer = Fget_text_property (pos, Qpointer, string);
29274 /* Change the mouse pointer according to what is under X/Y. */
29275 if (NILP (pointer)
29276 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29278 Lisp_Object map;
29279 map = Fget_text_property (pos, Qlocal_map, string);
29280 if (!KEYMAPP (map))
29281 map = Fget_text_property (pos, Qkeymap, string);
29282 if (!KEYMAPP (map) && draggable)
29283 cursor = dpyinfo->vertical_scroll_bar_cursor;
29286 else if (draggable)
29287 /* Default mode-line pointer. */
29288 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29290 #endif
29293 /* Change the mouse face according to what is under X/Y. */
29294 bool mouse_face_shown = false;
29295 if (STRINGP (string))
29297 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29298 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29299 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29300 && glyph)
29302 Lisp_Object b, e;
29304 struct glyph * tmp_glyph;
29306 int gpos;
29307 int gseq_length;
29308 int total_pixel_width;
29309 ptrdiff_t begpos, endpos, ignore;
29311 int vpos, hpos;
29313 b = Fprevious_single_property_change (make_number (charpos + 1),
29314 Qmouse_face, string, Qnil);
29315 if (NILP (b))
29316 begpos = 0;
29317 else
29318 begpos = XINT (b);
29320 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29321 if (NILP (e))
29322 endpos = SCHARS (string);
29323 else
29324 endpos = XINT (e);
29326 /* Calculate the glyph position GPOS of GLYPH in the
29327 displayed string, relative to the beginning of the
29328 highlighted part of the string.
29330 Note: GPOS is different from CHARPOS. CHARPOS is the
29331 position of GLYPH in the internal string object. A mode
29332 line string format has structures which are converted to
29333 a flattened string by the Emacs Lisp interpreter. The
29334 internal string is an element of those structures. The
29335 displayed string is the flattened string. */
29336 tmp_glyph = row_start_glyph;
29337 while (tmp_glyph < glyph
29338 && (!(EQ (tmp_glyph->object, glyph->object)
29339 && begpos <= tmp_glyph->charpos
29340 && tmp_glyph->charpos < endpos)))
29341 tmp_glyph++;
29342 gpos = glyph - tmp_glyph;
29344 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29345 the highlighted part of the displayed string to which
29346 GLYPH belongs. Note: GSEQ_LENGTH is different from
29347 SCHARS (STRING), because the latter returns the length of
29348 the internal string. */
29349 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29350 tmp_glyph > glyph
29351 && (!(EQ (tmp_glyph->object, glyph->object)
29352 && begpos <= tmp_glyph->charpos
29353 && tmp_glyph->charpos < endpos));
29354 tmp_glyph--)
29356 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29358 /* Calculate the total pixel width of all the glyphs between
29359 the beginning of the highlighted area and GLYPH. */
29360 total_pixel_width = 0;
29361 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29362 total_pixel_width += tmp_glyph->pixel_width;
29364 /* Pre calculation of re-rendering position. Note: X is in
29365 column units here, after the call to mode_line_string or
29366 marginal_area_string. */
29367 hpos = x - gpos;
29368 vpos = (area == ON_MODE_LINE
29369 ? (w->current_matrix)->nrows - 1
29370 : 0);
29372 /* If GLYPH's position is included in the region that is
29373 already drawn in mouse face, we have nothing to do. */
29374 if ( EQ (window, hlinfo->mouse_face_window)
29375 && (!row->reversed_p
29376 ? (hlinfo->mouse_face_beg_col <= hpos
29377 && hpos < hlinfo->mouse_face_end_col)
29378 /* In R2L rows we swap BEG and END, see below. */
29379 : (hlinfo->mouse_face_end_col <= hpos
29380 && hpos < hlinfo->mouse_face_beg_col))
29381 && hlinfo->mouse_face_beg_row == vpos )
29382 return;
29384 if (clear_mouse_face (hlinfo))
29385 cursor = No_Cursor;
29387 if (!row->reversed_p)
29389 hlinfo->mouse_face_beg_col = hpos;
29390 hlinfo->mouse_face_beg_x = original_x_pixel
29391 - (total_pixel_width + dx);
29392 hlinfo->mouse_face_end_col = hpos + gseq_length;
29393 hlinfo->mouse_face_end_x = 0;
29395 else
29397 /* In R2L rows, show_mouse_face expects BEG and END
29398 coordinates to be swapped. */
29399 hlinfo->mouse_face_end_col = hpos;
29400 hlinfo->mouse_face_end_x = original_x_pixel
29401 - (total_pixel_width + dx);
29402 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29403 hlinfo->mouse_face_beg_x = 0;
29406 hlinfo->mouse_face_beg_row = vpos;
29407 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29408 hlinfo->mouse_face_past_end = false;
29409 hlinfo->mouse_face_window = window;
29411 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29412 charpos,
29413 0, &ignore,
29414 glyph->face_id,
29415 true);
29416 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29417 mouse_face_shown = true;
29419 if (NILP (pointer))
29420 pointer = Qhand;
29424 /* If mouse-face doesn't need to be shown, clear any existing
29425 mouse-face. */
29426 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29427 clear_mouse_face (hlinfo);
29429 #ifdef HAVE_WINDOW_SYSTEM
29430 if (FRAME_WINDOW_P (f))
29431 define_frame_cursor1 (f, cursor, pointer);
29432 #endif
29436 /* EXPORT:
29437 Take proper action when the mouse has moved to position X, Y on
29438 frame F with regards to highlighting portions of display that have
29439 mouse-face properties. Also de-highlight portions of display where
29440 the mouse was before, set the mouse pointer shape as appropriate
29441 for the mouse coordinates, and activate help echo (tooltips).
29442 X and Y can be negative or out of range. */
29444 void
29445 note_mouse_highlight (struct frame *f, int x, int y)
29447 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29448 enum window_part part = ON_NOTHING;
29449 Lisp_Object window;
29450 struct window *w;
29451 Cursor cursor = No_Cursor;
29452 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29453 struct buffer *b;
29455 /* When a menu is active, don't highlight because this looks odd. */
29456 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29457 if (popup_activated ())
29458 return;
29459 #endif
29461 if (!f->glyphs_initialized_p
29462 || f->pointer_invisible)
29463 return;
29465 hlinfo->mouse_face_mouse_x = x;
29466 hlinfo->mouse_face_mouse_y = y;
29467 hlinfo->mouse_face_mouse_frame = f;
29469 if (hlinfo->mouse_face_defer)
29470 return;
29472 /* Which window is that in? */
29473 window = window_from_coordinates (f, x, y, &part, true);
29475 /* If displaying active text in another window, clear that. */
29476 if (! EQ (window, hlinfo->mouse_face_window)
29477 /* Also clear if we move out of text area in same window. */
29478 || (!NILP (hlinfo->mouse_face_window)
29479 && !NILP (window)
29480 && part != ON_TEXT
29481 && part != ON_MODE_LINE
29482 && part != ON_HEADER_LINE))
29483 clear_mouse_face (hlinfo);
29485 /* Not on a window -> return. */
29486 if (!WINDOWP (window))
29487 return;
29489 /* Reset help_echo_string. It will get recomputed below. */
29490 help_echo_string = Qnil;
29492 /* Convert to window-relative pixel coordinates. */
29493 w = XWINDOW (window);
29494 frame_to_window_pixel_xy (w, &x, &y);
29496 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29497 /* Handle tool-bar window differently since it doesn't display a
29498 buffer. */
29499 if (EQ (window, f->tool_bar_window))
29501 note_tool_bar_highlight (f, x, y);
29502 return;
29504 #endif
29506 /* Mouse is on the mode, header line or margin? */
29507 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29508 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29510 note_mode_line_or_margin_highlight (window, x, y, part);
29512 #ifdef HAVE_WINDOW_SYSTEM
29513 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29515 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29516 /* Show non-text cursor (Bug#16647). */
29517 goto set_cursor;
29519 else
29520 #endif
29521 return;
29524 #ifdef HAVE_WINDOW_SYSTEM
29525 if (part == ON_VERTICAL_BORDER)
29527 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29528 help_echo_string = build_string ("drag-mouse-1: resize");
29530 else if (part == ON_RIGHT_DIVIDER)
29532 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29533 help_echo_string = build_string ("drag-mouse-1: resize");
29535 else if (part == ON_BOTTOM_DIVIDER)
29536 if (! WINDOW_BOTTOMMOST_P (w)
29537 || minibuf_level
29538 || NILP (Vresize_mini_windows))
29540 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29541 help_echo_string = build_string ("drag-mouse-1: resize");
29543 else
29544 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29545 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29546 || part == ON_VERTICAL_SCROLL_BAR
29547 || part == ON_HORIZONTAL_SCROLL_BAR)
29548 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29549 else
29550 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29551 #endif
29553 /* Are we in a window whose display is up to date?
29554 And verify the buffer's text has not changed. */
29555 b = XBUFFER (w->contents);
29556 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29558 int hpos, vpos, dx, dy, area = LAST_AREA;
29559 ptrdiff_t pos;
29560 struct glyph *glyph;
29561 Lisp_Object object;
29562 Lisp_Object mouse_face = Qnil, position;
29563 Lisp_Object *overlay_vec = NULL;
29564 ptrdiff_t i, noverlays;
29565 struct buffer *obuf;
29566 ptrdiff_t obegv, ozv;
29567 bool same_region;
29569 /* Find the glyph under X/Y. */
29570 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29572 #ifdef HAVE_WINDOW_SYSTEM
29573 /* Look for :pointer property on image. */
29574 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29576 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29577 if (img != NULL && IMAGEP (img->spec))
29579 Lisp_Object image_map, hotspot;
29580 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29581 !NILP (image_map))
29582 && (hotspot = find_hot_spot (image_map,
29583 glyph->slice.img.x + dx,
29584 glyph->slice.img.y + dy),
29585 CONSP (hotspot))
29586 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29588 Lisp_Object plist;
29590 /* Could check XCAR (hotspot) to see if we enter/leave
29591 this hot-spot.
29592 If so, we could look for mouse-enter, mouse-leave
29593 properties in PLIST (and do something...). */
29594 hotspot = XCDR (hotspot);
29595 if (CONSP (hotspot)
29596 && (plist = XCAR (hotspot), CONSP (plist)))
29598 pointer = Fplist_get (plist, Qpointer);
29599 if (NILP (pointer))
29600 pointer = Qhand;
29601 help_echo_string = Fplist_get (plist, Qhelp_echo);
29602 if (!NILP (help_echo_string))
29604 help_echo_window = window;
29605 help_echo_object = glyph->object;
29606 help_echo_pos = glyph->charpos;
29610 if (NILP (pointer))
29611 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29614 #endif /* HAVE_WINDOW_SYSTEM */
29616 /* Clear mouse face if X/Y not over text. */
29617 if (glyph == NULL
29618 || area != TEXT_AREA
29619 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29620 /* Glyph's OBJECT is nil for glyphs inserted by the
29621 display engine for its internal purposes, like truncation
29622 and continuation glyphs and blanks beyond the end of
29623 line's text on text terminals. If we are over such a
29624 glyph, we are not over any text. */
29625 || NILP (glyph->object)
29626 /* R2L rows have a stretch glyph at their front, which
29627 stands for no text, whereas L2R rows have no glyphs at
29628 all beyond the end of text. Treat such stretch glyphs
29629 like we do with NULL glyphs in L2R rows. */
29630 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29631 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29632 && glyph->type == STRETCH_GLYPH
29633 && glyph->avoid_cursor_p))
29635 if (clear_mouse_face (hlinfo))
29636 cursor = No_Cursor;
29637 #ifdef HAVE_WINDOW_SYSTEM
29638 if (FRAME_WINDOW_P (f) && NILP (pointer))
29640 if (area != TEXT_AREA)
29641 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29642 else
29643 pointer = Vvoid_text_area_pointer;
29645 #endif
29646 goto set_cursor;
29649 pos = glyph->charpos;
29650 object = glyph->object;
29651 if (!STRINGP (object) && !BUFFERP (object))
29652 goto set_cursor;
29654 /* If we get an out-of-range value, return now; avoid an error. */
29655 if (BUFFERP (object) && pos > BUF_Z (b))
29656 goto set_cursor;
29658 /* Make the window's buffer temporarily current for
29659 overlays_at and compute_char_face. */
29660 obuf = current_buffer;
29661 current_buffer = b;
29662 obegv = BEGV;
29663 ozv = ZV;
29664 BEGV = BEG;
29665 ZV = Z;
29667 /* Is this char mouse-active or does it have help-echo? */
29668 position = make_number (pos);
29670 USE_SAFE_ALLOCA;
29672 if (BUFFERP (object))
29674 /* Put all the overlays we want in a vector in overlay_vec. */
29675 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29676 /* Sort overlays into increasing priority order. */
29677 noverlays = sort_overlays (overlay_vec, noverlays, w);
29679 else
29680 noverlays = 0;
29682 if (NILP (Vmouse_highlight))
29684 clear_mouse_face (hlinfo);
29685 goto check_help_echo;
29688 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29690 if (same_region)
29691 cursor = No_Cursor;
29693 /* Check mouse-face highlighting. */
29694 if (! same_region
29695 /* If there exists an overlay with mouse-face overlapping
29696 the one we are currently highlighting, we have to
29697 check if we enter the overlapping overlay, and then
29698 highlight only that. */
29699 || (OVERLAYP (hlinfo->mouse_face_overlay)
29700 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29702 /* Find the highest priority overlay with a mouse-face. */
29703 Lisp_Object overlay = Qnil;
29704 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29706 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29707 if (!NILP (mouse_face))
29708 overlay = overlay_vec[i];
29711 /* If we're highlighting the same overlay as before, there's
29712 no need to do that again. */
29713 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29714 goto check_help_echo;
29715 hlinfo->mouse_face_overlay = overlay;
29717 /* Clear the display of the old active region, if any. */
29718 if (clear_mouse_face (hlinfo))
29719 cursor = No_Cursor;
29721 /* If no overlay applies, get a text property. */
29722 if (NILP (overlay))
29723 mouse_face = Fget_text_property (position, Qmouse_face, object);
29725 /* Next, compute the bounds of the mouse highlighting and
29726 display it. */
29727 if (!NILP (mouse_face) && STRINGP (object))
29729 /* The mouse-highlighting comes from a display string
29730 with a mouse-face. */
29731 Lisp_Object s, e;
29732 ptrdiff_t ignore;
29734 s = Fprevious_single_property_change
29735 (make_number (pos + 1), Qmouse_face, object, Qnil);
29736 e = Fnext_single_property_change
29737 (position, Qmouse_face, object, Qnil);
29738 if (NILP (s))
29739 s = make_number (0);
29740 if (NILP (e))
29741 e = make_number (SCHARS (object));
29742 mouse_face_from_string_pos (w, hlinfo, object,
29743 XINT (s), XINT (e));
29744 hlinfo->mouse_face_past_end = false;
29745 hlinfo->mouse_face_window = window;
29746 hlinfo->mouse_face_face_id
29747 = face_at_string_position (w, object, pos, 0, &ignore,
29748 glyph->face_id, true);
29749 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29750 cursor = No_Cursor;
29752 else
29754 /* The mouse-highlighting, if any, comes from an overlay
29755 or text property in the buffer. */
29756 Lisp_Object buffer IF_LINT (= Qnil);
29757 Lisp_Object disp_string IF_LINT (= Qnil);
29759 if (STRINGP (object))
29761 /* If we are on a display string with no mouse-face,
29762 check if the text under it has one. */
29763 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29764 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29765 pos = string_buffer_position (object, start);
29766 if (pos > 0)
29768 mouse_face = get_char_property_and_overlay
29769 (make_number (pos), Qmouse_face, w->contents, &overlay);
29770 buffer = w->contents;
29771 disp_string = object;
29774 else
29776 buffer = object;
29777 disp_string = Qnil;
29780 if (!NILP (mouse_face))
29782 Lisp_Object before, after;
29783 Lisp_Object before_string, after_string;
29784 /* To correctly find the limits of mouse highlight
29785 in a bidi-reordered buffer, we must not use the
29786 optimization of limiting the search in
29787 previous-single-property-change and
29788 next-single-property-change, because
29789 rows_from_pos_range needs the real start and end
29790 positions to DTRT in this case. That's because
29791 the first row visible in a window does not
29792 necessarily display the character whose position
29793 is the smallest. */
29794 Lisp_Object lim1
29795 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29796 ? Fmarker_position (w->start)
29797 : Qnil;
29798 Lisp_Object lim2
29799 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29800 ? make_number (BUF_Z (XBUFFER (buffer))
29801 - w->window_end_pos)
29802 : Qnil;
29804 if (NILP (overlay))
29806 /* Handle the text property case. */
29807 before = Fprevious_single_property_change
29808 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29809 after = Fnext_single_property_change
29810 (make_number (pos), Qmouse_face, buffer, lim2);
29811 before_string = after_string = Qnil;
29813 else
29815 /* Handle the overlay case. */
29816 before = Foverlay_start (overlay);
29817 after = Foverlay_end (overlay);
29818 before_string = Foverlay_get (overlay, Qbefore_string);
29819 after_string = Foverlay_get (overlay, Qafter_string);
29821 if (!STRINGP (before_string)) before_string = Qnil;
29822 if (!STRINGP (after_string)) after_string = Qnil;
29825 mouse_face_from_buffer_pos (window, hlinfo, pos,
29826 NILP (before)
29828 : XFASTINT (before),
29829 NILP (after)
29830 ? BUF_Z (XBUFFER (buffer))
29831 : XFASTINT (after),
29832 before_string, after_string,
29833 disp_string);
29834 cursor = No_Cursor;
29839 check_help_echo:
29841 /* Look for a `help-echo' property. */
29842 if (NILP (help_echo_string)) {
29843 Lisp_Object help, overlay;
29845 /* Check overlays first. */
29846 help = overlay = Qnil;
29847 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29849 overlay = overlay_vec[i];
29850 help = Foverlay_get (overlay, Qhelp_echo);
29853 if (!NILP (help))
29855 help_echo_string = help;
29856 help_echo_window = window;
29857 help_echo_object = overlay;
29858 help_echo_pos = pos;
29860 else
29862 Lisp_Object obj = glyph->object;
29863 ptrdiff_t charpos = glyph->charpos;
29865 /* Try text properties. */
29866 if (STRINGP (obj)
29867 && charpos >= 0
29868 && charpos < SCHARS (obj))
29870 help = Fget_text_property (make_number (charpos),
29871 Qhelp_echo, obj);
29872 if (NILP (help))
29874 /* If the string itself doesn't specify a help-echo,
29875 see if the buffer text ``under'' it does. */
29876 struct glyph_row *r
29877 = MATRIX_ROW (w->current_matrix, vpos);
29878 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29879 ptrdiff_t p = string_buffer_position (obj, start);
29880 if (p > 0)
29882 help = Fget_char_property (make_number (p),
29883 Qhelp_echo, w->contents);
29884 if (!NILP (help))
29886 charpos = p;
29887 obj = w->contents;
29892 else if (BUFFERP (obj)
29893 && charpos >= BEGV
29894 && charpos < ZV)
29895 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29896 obj);
29898 if (!NILP (help))
29900 help_echo_string = help;
29901 help_echo_window = window;
29902 help_echo_object = obj;
29903 help_echo_pos = charpos;
29908 #ifdef HAVE_WINDOW_SYSTEM
29909 /* Look for a `pointer' property. */
29910 if (FRAME_WINDOW_P (f) && NILP (pointer))
29912 /* Check overlays first. */
29913 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29914 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29916 if (NILP (pointer))
29918 Lisp_Object obj = glyph->object;
29919 ptrdiff_t charpos = glyph->charpos;
29921 /* Try text properties. */
29922 if (STRINGP (obj)
29923 && charpos >= 0
29924 && charpos < SCHARS (obj))
29926 pointer = Fget_text_property (make_number (charpos),
29927 Qpointer, obj);
29928 if (NILP (pointer))
29930 /* If the string itself doesn't specify a pointer,
29931 see if the buffer text ``under'' it does. */
29932 struct glyph_row *r
29933 = MATRIX_ROW (w->current_matrix, vpos);
29934 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29935 ptrdiff_t p = string_buffer_position (obj, start);
29936 if (p > 0)
29937 pointer = Fget_char_property (make_number (p),
29938 Qpointer, w->contents);
29941 else if (BUFFERP (obj)
29942 && charpos >= BEGV
29943 && charpos < ZV)
29944 pointer = Fget_text_property (make_number (charpos),
29945 Qpointer, obj);
29948 #endif /* HAVE_WINDOW_SYSTEM */
29950 BEGV = obegv;
29951 ZV = ozv;
29952 current_buffer = obuf;
29953 SAFE_FREE ();
29956 set_cursor:
29958 #ifdef HAVE_WINDOW_SYSTEM
29959 if (FRAME_WINDOW_P (f))
29960 define_frame_cursor1 (f, cursor, pointer);
29961 #else
29962 /* This is here to prevent a compiler error, about "label at end of
29963 compound statement". */
29964 return;
29965 #endif
29969 /* EXPORT for RIF:
29970 Clear any mouse-face on window W. This function is part of the
29971 redisplay interface, and is called from try_window_id and similar
29972 functions to ensure the mouse-highlight is off. */
29974 void
29975 x_clear_window_mouse_face (struct window *w)
29977 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29978 Lisp_Object window;
29980 block_input ();
29981 XSETWINDOW (window, w);
29982 if (EQ (window, hlinfo->mouse_face_window))
29983 clear_mouse_face (hlinfo);
29984 unblock_input ();
29988 /* EXPORT:
29989 Just discard the mouse face information for frame F, if any.
29990 This is used when the size of F is changed. */
29992 void
29993 cancel_mouse_face (struct frame *f)
29995 Lisp_Object window;
29996 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29998 window = hlinfo->mouse_face_window;
29999 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30000 reset_mouse_highlight (hlinfo);
30005 /***********************************************************************
30006 Exposure Events
30007 ***********************************************************************/
30009 #ifdef HAVE_WINDOW_SYSTEM
30011 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30012 which intersects rectangle R. R is in window-relative coordinates. */
30014 static void
30015 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30016 enum glyph_row_area area)
30018 struct glyph *first = row->glyphs[area];
30019 struct glyph *end = row->glyphs[area] + row->used[area];
30020 struct glyph *last;
30021 int first_x, start_x, x;
30023 if (area == TEXT_AREA && row->fill_line_p)
30024 /* If row extends face to end of line write the whole line. */
30025 draw_glyphs (w, 0, row, area,
30026 0, row->used[area],
30027 DRAW_NORMAL_TEXT, 0);
30028 else
30030 /* Set START_X to the window-relative start position for drawing glyphs of
30031 AREA. The first glyph of the text area can be partially visible.
30032 The first glyphs of other areas cannot. */
30033 start_x = window_box_left_offset (w, area);
30034 x = start_x;
30035 if (area == TEXT_AREA)
30036 x += row->x;
30038 /* Find the first glyph that must be redrawn. */
30039 while (first < end
30040 && x + first->pixel_width < r->x)
30042 x += first->pixel_width;
30043 ++first;
30046 /* Find the last one. */
30047 last = first;
30048 first_x = x;
30049 while (last < end
30050 && x < r->x + r->width)
30052 x += last->pixel_width;
30053 ++last;
30056 /* Repaint. */
30057 if (last > first)
30058 draw_glyphs (w, first_x - start_x, row, area,
30059 first - row->glyphs[area], last - row->glyphs[area],
30060 DRAW_NORMAL_TEXT, 0);
30065 /* Redraw the parts of the glyph row ROW on window W intersecting
30066 rectangle R. R is in window-relative coordinates. Value is
30067 true if mouse-face was overwritten. */
30069 static bool
30070 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30072 eassert (row->enabled_p);
30074 if (row->mode_line_p || w->pseudo_window_p)
30075 draw_glyphs (w, 0, row, TEXT_AREA,
30076 0, row->used[TEXT_AREA],
30077 DRAW_NORMAL_TEXT, 0);
30078 else
30080 if (row->used[LEFT_MARGIN_AREA])
30081 expose_area (w, row, r, LEFT_MARGIN_AREA);
30082 if (row->used[TEXT_AREA])
30083 expose_area (w, row, r, TEXT_AREA);
30084 if (row->used[RIGHT_MARGIN_AREA])
30085 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30086 draw_row_fringe_bitmaps (w, row);
30089 return row->mouse_face_p;
30093 /* Redraw those parts of glyphs rows during expose event handling that
30094 overlap other rows. Redrawing of an exposed line writes over parts
30095 of lines overlapping that exposed line; this function fixes that.
30097 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30098 row in W's current matrix that is exposed and overlaps other rows.
30099 LAST_OVERLAPPING_ROW is the last such row. */
30101 static void
30102 expose_overlaps (struct window *w,
30103 struct glyph_row *first_overlapping_row,
30104 struct glyph_row *last_overlapping_row,
30105 XRectangle *r)
30107 struct glyph_row *row;
30109 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30110 if (row->overlapping_p)
30112 eassert (row->enabled_p && !row->mode_line_p);
30114 row->clip = r;
30115 if (row->used[LEFT_MARGIN_AREA])
30116 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30118 if (row->used[TEXT_AREA])
30119 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30121 if (row->used[RIGHT_MARGIN_AREA])
30122 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30123 row->clip = NULL;
30128 /* Return true if W's cursor intersects rectangle R. */
30130 static bool
30131 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30133 XRectangle cr, result;
30134 struct glyph *cursor_glyph;
30135 struct glyph_row *row;
30137 if (w->phys_cursor.vpos >= 0
30138 && w->phys_cursor.vpos < w->current_matrix->nrows
30139 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30140 row->enabled_p)
30141 && row->cursor_in_fringe_p)
30143 /* Cursor is in the fringe. */
30144 cr.x = window_box_right_offset (w,
30145 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30146 ? RIGHT_MARGIN_AREA
30147 : TEXT_AREA));
30148 cr.y = row->y;
30149 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30150 cr.height = row->height;
30151 return x_intersect_rectangles (&cr, r, &result);
30154 cursor_glyph = get_phys_cursor_glyph (w);
30155 if (cursor_glyph)
30157 /* r is relative to W's box, but w->phys_cursor.x is relative
30158 to left edge of W's TEXT area. Adjust it. */
30159 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30160 cr.y = w->phys_cursor.y;
30161 cr.width = cursor_glyph->pixel_width;
30162 cr.height = w->phys_cursor_height;
30163 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30164 I assume the effect is the same -- and this is portable. */
30165 return x_intersect_rectangles (&cr, r, &result);
30167 /* If we don't understand the format, pretend we're not in the hot-spot. */
30168 return false;
30172 /* EXPORT:
30173 Draw a vertical window border to the right of window W if W doesn't
30174 have vertical scroll bars. */
30176 void
30177 x_draw_vertical_border (struct window *w)
30179 struct frame *f = XFRAME (WINDOW_FRAME (w));
30181 /* We could do better, if we knew what type of scroll-bar the adjacent
30182 windows (on either side) have... But we don't :-(
30183 However, I think this works ok. ++KFS 2003-04-25 */
30185 /* Redraw borders between horizontally adjacent windows. Don't
30186 do it for frames with vertical scroll bars because either the
30187 right scroll bar of a window, or the left scroll bar of its
30188 neighbor will suffice as a border. */
30189 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30190 return;
30192 /* Note: It is necessary to redraw both the left and the right
30193 borders, for when only this single window W is being
30194 redisplayed. */
30195 if (!WINDOW_RIGHTMOST_P (w)
30196 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30198 int x0, x1, y0, y1;
30200 window_box_edges (w, &x0, &y0, &x1, &y1);
30201 y1 -= 1;
30203 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30204 x1 -= 1;
30206 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30209 if (!WINDOW_LEFTMOST_P (w)
30210 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30212 int x0, x1, y0, y1;
30214 window_box_edges (w, &x0, &y0, &x1, &y1);
30215 y1 -= 1;
30217 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30218 x0 -= 1;
30220 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30225 /* Draw window dividers for window W. */
30227 void
30228 x_draw_right_divider (struct window *w)
30230 struct frame *f = WINDOW_XFRAME (w);
30232 if (w->mini || w->pseudo_window_p)
30233 return;
30234 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30236 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30237 int x1 = WINDOW_RIGHT_EDGE_X (w);
30238 int y0 = WINDOW_TOP_EDGE_Y (w);
30239 /* The bottom divider prevails. */
30240 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30242 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30246 static void
30247 x_draw_bottom_divider (struct window *w)
30249 struct frame *f = XFRAME (WINDOW_FRAME (w));
30251 if (w->mini || w->pseudo_window_p)
30252 return;
30253 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30255 int x0 = WINDOW_LEFT_EDGE_X (w);
30256 int x1 = WINDOW_RIGHT_EDGE_X (w);
30257 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30258 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30260 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30264 /* Redraw the part of window W intersection rectangle FR. Pixel
30265 coordinates in FR are frame-relative. Call this function with
30266 input blocked. Value is true if the exposure overwrites
30267 mouse-face. */
30269 static bool
30270 expose_window (struct window *w, XRectangle *fr)
30272 struct frame *f = XFRAME (w->frame);
30273 XRectangle wr, r;
30274 bool mouse_face_overwritten_p = false;
30276 /* If window is not yet fully initialized, do nothing. This can
30277 happen when toolkit scroll bars are used and a window is split.
30278 Reconfiguring the scroll bar will generate an expose for a newly
30279 created window. */
30280 if (w->current_matrix == NULL)
30281 return false;
30283 /* When we're currently updating the window, display and current
30284 matrix usually don't agree. Arrange for a thorough display
30285 later. */
30286 if (w->must_be_updated_p)
30288 SET_FRAME_GARBAGED (f);
30289 return false;
30292 /* Frame-relative pixel rectangle of W. */
30293 wr.x = WINDOW_LEFT_EDGE_X (w);
30294 wr.y = WINDOW_TOP_EDGE_Y (w);
30295 wr.width = WINDOW_PIXEL_WIDTH (w);
30296 wr.height = WINDOW_PIXEL_HEIGHT (w);
30298 if (x_intersect_rectangles (fr, &wr, &r))
30300 int yb = window_text_bottom_y (w);
30301 struct glyph_row *row;
30302 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30304 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30305 r.x, r.y, r.width, r.height));
30307 /* Convert to window coordinates. */
30308 r.x -= WINDOW_LEFT_EDGE_X (w);
30309 r.y -= WINDOW_TOP_EDGE_Y (w);
30311 /* Turn off the cursor. */
30312 bool cursor_cleared_p = (!w->pseudo_window_p
30313 && phys_cursor_in_rect_p (w, &r));
30314 if (cursor_cleared_p)
30315 x_clear_cursor (w);
30317 /* If the row containing the cursor extends face to end of line,
30318 then expose_area might overwrite the cursor outside the
30319 rectangle and thus notice_overwritten_cursor might clear
30320 w->phys_cursor_on_p. We remember the original value and
30321 check later if it is changed. */
30322 bool phys_cursor_on_p = w->phys_cursor_on_p;
30324 /* Update lines intersecting rectangle R. */
30325 first_overlapping_row = last_overlapping_row = NULL;
30326 for (row = w->current_matrix->rows;
30327 row->enabled_p;
30328 ++row)
30330 int y0 = row->y;
30331 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30333 if ((y0 >= r.y && y0 < r.y + r.height)
30334 || (y1 > r.y && y1 < r.y + r.height)
30335 || (r.y >= y0 && r.y < y1)
30336 || (r.y + r.height > y0 && r.y + r.height < y1))
30338 /* A header line may be overlapping, but there is no need
30339 to fix overlapping areas for them. KFS 2005-02-12 */
30340 if (row->overlapping_p && !row->mode_line_p)
30342 if (first_overlapping_row == NULL)
30343 first_overlapping_row = row;
30344 last_overlapping_row = row;
30347 row->clip = fr;
30348 if (expose_line (w, row, &r))
30349 mouse_face_overwritten_p = true;
30350 row->clip = NULL;
30352 else if (row->overlapping_p)
30354 /* We must redraw a row overlapping the exposed area. */
30355 if (y0 < r.y
30356 ? y0 + row->phys_height > r.y
30357 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30359 if (first_overlapping_row == NULL)
30360 first_overlapping_row = row;
30361 last_overlapping_row = row;
30365 if (y1 >= yb)
30366 break;
30369 /* Display the mode line if there is one. */
30370 if (WINDOW_WANTS_MODELINE_P (w)
30371 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30372 row->enabled_p)
30373 && row->y < r.y + r.height)
30375 if (expose_line (w, row, &r))
30376 mouse_face_overwritten_p = true;
30379 if (!w->pseudo_window_p)
30381 /* Fix the display of overlapping rows. */
30382 if (first_overlapping_row)
30383 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30384 fr);
30386 /* Draw border between windows. */
30387 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30388 x_draw_right_divider (w);
30389 else
30390 x_draw_vertical_border (w);
30392 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30393 x_draw_bottom_divider (w);
30395 /* Turn the cursor on again. */
30396 if (cursor_cleared_p
30397 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30398 update_window_cursor (w, true);
30402 return mouse_face_overwritten_p;
30407 /* Redraw (parts) of all windows in the window tree rooted at W that
30408 intersect R. R contains frame pixel coordinates. Value is
30409 true if the exposure overwrites mouse-face. */
30411 static bool
30412 expose_window_tree (struct window *w, XRectangle *r)
30414 struct frame *f = XFRAME (w->frame);
30415 bool mouse_face_overwritten_p = false;
30417 while (w && !FRAME_GARBAGED_P (f))
30419 mouse_face_overwritten_p
30420 |= (WINDOWP (w->contents)
30421 ? expose_window_tree (XWINDOW (w->contents), r)
30422 : expose_window (w, r));
30424 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30427 return mouse_face_overwritten_p;
30431 /* EXPORT:
30432 Redisplay an exposed area of frame F. X and Y are the upper-left
30433 corner of the exposed rectangle. W and H are width and height of
30434 the exposed area. All are pixel values. W or H zero means redraw
30435 the entire frame. */
30437 void
30438 expose_frame (struct frame *f, int x, int y, int w, int h)
30440 XRectangle r;
30441 bool mouse_face_overwritten_p = false;
30443 TRACE ((stderr, "expose_frame "));
30445 /* No need to redraw if frame will be redrawn soon. */
30446 if (FRAME_GARBAGED_P (f))
30448 TRACE ((stderr, " garbaged\n"));
30449 return;
30452 /* If basic faces haven't been realized yet, there is no point in
30453 trying to redraw anything. This can happen when we get an expose
30454 event while Emacs is starting, e.g. by moving another window. */
30455 if (FRAME_FACE_CACHE (f) == NULL
30456 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30458 TRACE ((stderr, " no faces\n"));
30459 return;
30462 if (w == 0 || h == 0)
30464 r.x = r.y = 0;
30465 r.width = FRAME_TEXT_WIDTH (f);
30466 r.height = FRAME_TEXT_HEIGHT (f);
30468 else
30470 r.x = x;
30471 r.y = y;
30472 r.width = w;
30473 r.height = h;
30476 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30477 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30479 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30480 if (WINDOWP (f->tool_bar_window))
30481 mouse_face_overwritten_p
30482 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30483 #endif
30485 #ifdef HAVE_X_WINDOWS
30486 #ifndef MSDOS
30487 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30488 if (WINDOWP (f->menu_bar_window))
30489 mouse_face_overwritten_p
30490 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30491 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30492 #endif
30493 #endif
30495 /* Some window managers support a focus-follows-mouse style with
30496 delayed raising of frames. Imagine a partially obscured frame,
30497 and moving the mouse into partially obscured mouse-face on that
30498 frame. The visible part of the mouse-face will be highlighted,
30499 then the WM raises the obscured frame. With at least one WM, KDE
30500 2.1, Emacs is not getting any event for the raising of the frame
30501 (even tried with SubstructureRedirectMask), only Expose events.
30502 These expose events will draw text normally, i.e. not
30503 highlighted. Which means we must redo the highlight here.
30504 Subsume it under ``we love X''. --gerd 2001-08-15 */
30505 /* Included in Windows version because Windows most likely does not
30506 do the right thing if any third party tool offers
30507 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30508 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30510 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30511 if (f == hlinfo->mouse_face_mouse_frame)
30513 int mouse_x = hlinfo->mouse_face_mouse_x;
30514 int mouse_y = hlinfo->mouse_face_mouse_y;
30515 clear_mouse_face (hlinfo);
30516 note_mouse_highlight (f, mouse_x, mouse_y);
30522 /* EXPORT:
30523 Determine the intersection of two rectangles R1 and R2. Return
30524 the intersection in *RESULT. Value is true if RESULT is not
30525 empty. */
30527 bool
30528 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30530 XRectangle *left, *right;
30531 XRectangle *upper, *lower;
30532 bool intersection_p = false;
30534 /* Rearrange so that R1 is the left-most rectangle. */
30535 if (r1->x < r2->x)
30536 left = r1, right = r2;
30537 else
30538 left = r2, right = r1;
30540 /* X0 of the intersection is right.x0, if this is inside R1,
30541 otherwise there is no intersection. */
30542 if (right->x <= left->x + left->width)
30544 result->x = right->x;
30546 /* The right end of the intersection is the minimum of
30547 the right ends of left and right. */
30548 result->width = (min (left->x + left->width, right->x + right->width)
30549 - result->x);
30551 /* Same game for Y. */
30552 if (r1->y < r2->y)
30553 upper = r1, lower = r2;
30554 else
30555 upper = r2, lower = r1;
30557 /* The upper end of the intersection is lower.y0, if this is inside
30558 of upper. Otherwise, there is no intersection. */
30559 if (lower->y <= upper->y + upper->height)
30561 result->y = lower->y;
30563 /* The lower end of the intersection is the minimum of the lower
30564 ends of upper and lower. */
30565 result->height = (min (lower->y + lower->height,
30566 upper->y + upper->height)
30567 - result->y);
30568 intersection_p = true;
30572 return intersection_p;
30575 #endif /* HAVE_WINDOW_SYSTEM */
30578 /***********************************************************************
30579 Initialization
30580 ***********************************************************************/
30582 void
30583 syms_of_xdisp (void)
30585 Vwith_echo_area_save_vector = Qnil;
30586 staticpro (&Vwith_echo_area_save_vector);
30588 Vmessage_stack = Qnil;
30589 staticpro (&Vmessage_stack);
30591 /* Non-nil means don't actually do any redisplay. */
30592 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30594 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30596 DEFVAR_BOOL("inhibit-message", inhibit_message,
30597 doc: /* Non-nil means calls to `message' are not displayed.
30598 They are still logged to the *Messages* buffer. */);
30599 inhibit_message = 0;
30601 message_dolog_marker1 = Fmake_marker ();
30602 staticpro (&message_dolog_marker1);
30603 message_dolog_marker2 = Fmake_marker ();
30604 staticpro (&message_dolog_marker2);
30605 message_dolog_marker3 = Fmake_marker ();
30606 staticpro (&message_dolog_marker3);
30608 #ifdef GLYPH_DEBUG
30609 defsubr (&Sdump_frame_glyph_matrix);
30610 defsubr (&Sdump_glyph_matrix);
30611 defsubr (&Sdump_glyph_row);
30612 defsubr (&Sdump_tool_bar_row);
30613 defsubr (&Strace_redisplay);
30614 defsubr (&Strace_to_stderr);
30615 #endif
30616 #ifdef HAVE_WINDOW_SYSTEM
30617 defsubr (&Stool_bar_height);
30618 defsubr (&Slookup_image_map);
30619 #endif
30620 defsubr (&Sline_pixel_height);
30621 defsubr (&Sformat_mode_line);
30622 defsubr (&Sinvisible_p);
30623 defsubr (&Scurrent_bidi_paragraph_direction);
30624 defsubr (&Swindow_text_pixel_size);
30625 defsubr (&Smove_point_visually);
30626 defsubr (&Sbidi_find_overridden_directionality);
30628 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30629 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30630 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30631 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30632 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30633 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30634 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30635 DEFSYM (Qeval, "eval");
30636 DEFSYM (QCdata, ":data");
30638 /* Names of text properties relevant for redisplay. */
30639 DEFSYM (Qdisplay, "display");
30640 DEFSYM (Qspace_width, "space-width");
30641 DEFSYM (Qraise, "raise");
30642 DEFSYM (Qslice, "slice");
30643 DEFSYM (Qspace, "space");
30644 DEFSYM (Qmargin, "margin");
30645 DEFSYM (Qpointer, "pointer");
30646 DEFSYM (Qleft_margin, "left-margin");
30647 DEFSYM (Qright_margin, "right-margin");
30648 DEFSYM (Qcenter, "center");
30649 DEFSYM (Qline_height, "line-height");
30650 DEFSYM (QCalign_to, ":align-to");
30651 DEFSYM (QCrelative_width, ":relative-width");
30652 DEFSYM (QCrelative_height, ":relative-height");
30653 DEFSYM (QCeval, ":eval");
30654 DEFSYM (QCpropertize, ":propertize");
30655 DEFSYM (QCfile, ":file");
30656 DEFSYM (Qfontified, "fontified");
30657 DEFSYM (Qfontification_functions, "fontification-functions");
30659 /* Name of the face used to highlight trailing whitespace. */
30660 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30662 /* Name and number of the face used to highlight escape glyphs. */
30663 DEFSYM (Qescape_glyph, "escape-glyph");
30665 /* Name and number of the face used to highlight non-breaking spaces. */
30666 DEFSYM (Qnobreak_space, "nobreak-space");
30668 /* The symbol 'image' which is the car of the lists used to represent
30669 images in Lisp. Also a tool bar style. */
30670 DEFSYM (Qimage, "image");
30672 /* Tool bar styles. */
30673 DEFSYM (Qtext, "text");
30674 DEFSYM (Qboth, "both");
30675 DEFSYM (Qboth_horiz, "both-horiz");
30676 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30678 /* The image map types. */
30679 DEFSYM (QCmap, ":map");
30680 DEFSYM (QCpointer, ":pointer");
30681 DEFSYM (Qrect, "rect");
30682 DEFSYM (Qcircle, "circle");
30683 DEFSYM (Qpoly, "poly");
30685 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30686 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30687 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30689 DEFSYM (Qgrow_only, "grow-only");
30690 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30691 DEFSYM (Qposition, "position");
30692 DEFSYM (Qbuffer_position, "buffer-position");
30693 DEFSYM (Qobject, "object");
30695 /* Cursor shapes. */
30696 DEFSYM (Qbar, "bar");
30697 DEFSYM (Qhbar, "hbar");
30698 DEFSYM (Qbox, "box");
30699 DEFSYM (Qhollow, "hollow");
30701 /* Pointer shapes. */
30702 DEFSYM (Qhand, "hand");
30703 DEFSYM (Qarrow, "arrow");
30704 /* also Qtext */
30706 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30708 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30709 staticpro (&list_of_error);
30711 /* Values of those variables at last redisplay are stored as
30712 properties on 'overlay-arrow-position' symbol. However, if
30713 Voverlay_arrow_position is a marker, last-arrow-position is its
30714 numerical position. */
30715 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30716 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30718 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30719 properties on a symbol in overlay-arrow-variable-list. */
30720 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30721 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30723 echo_buffer[0] = echo_buffer[1] = Qnil;
30724 staticpro (&echo_buffer[0]);
30725 staticpro (&echo_buffer[1]);
30727 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30728 staticpro (&echo_area_buffer[0]);
30729 staticpro (&echo_area_buffer[1]);
30731 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30732 staticpro (&Vmessages_buffer_name);
30734 mode_line_proptrans_alist = Qnil;
30735 staticpro (&mode_line_proptrans_alist);
30736 mode_line_string_list = Qnil;
30737 staticpro (&mode_line_string_list);
30738 mode_line_string_face = Qnil;
30739 staticpro (&mode_line_string_face);
30740 mode_line_string_face_prop = Qnil;
30741 staticpro (&mode_line_string_face_prop);
30742 Vmode_line_unwind_vector = Qnil;
30743 staticpro (&Vmode_line_unwind_vector);
30745 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30747 help_echo_string = Qnil;
30748 staticpro (&help_echo_string);
30749 help_echo_object = Qnil;
30750 staticpro (&help_echo_object);
30751 help_echo_window = Qnil;
30752 staticpro (&help_echo_window);
30753 previous_help_echo_string = Qnil;
30754 staticpro (&previous_help_echo_string);
30755 help_echo_pos = -1;
30757 DEFSYM (Qright_to_left, "right-to-left");
30758 DEFSYM (Qleft_to_right, "left-to-right");
30759 defsubr (&Sbidi_resolved_levels);
30761 #ifdef HAVE_WINDOW_SYSTEM
30762 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30763 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30764 For example, if a block cursor is over a tab, it will be drawn as
30765 wide as that tab on the display. */);
30766 x_stretch_cursor_p = 0;
30767 #endif
30769 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30770 doc: /* Non-nil means highlight trailing whitespace.
30771 The face used for trailing whitespace is `trailing-whitespace'. */);
30772 Vshow_trailing_whitespace = Qnil;
30774 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30775 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30776 If the value is t, Emacs highlights non-ASCII chars which have the
30777 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30778 or `escape-glyph' face respectively.
30780 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30781 U+2011 (non-breaking hyphen) are affected.
30783 Any other non-nil value means to display these characters as a escape
30784 glyph followed by an ordinary space or hyphen.
30786 A value of nil means no special handling of these characters. */);
30787 Vnobreak_char_display = Qt;
30789 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30790 doc: /* The pointer shape to show in void text areas.
30791 A value of nil means to show the text pointer. Other options are
30792 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30793 `hourglass'. */);
30794 Vvoid_text_area_pointer = Qarrow;
30796 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30797 doc: /* Non-nil means don't actually do any redisplay.
30798 This is used for internal purposes. */);
30799 Vinhibit_redisplay = Qnil;
30801 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30802 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30803 Vglobal_mode_string = Qnil;
30805 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30806 doc: /* Marker for where to display an arrow on top of the buffer text.
30807 This must be the beginning of a line in order to work.
30808 See also `overlay-arrow-string'. */);
30809 Voverlay_arrow_position = Qnil;
30811 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30812 doc: /* String to display as an arrow in non-window frames.
30813 See also `overlay-arrow-position'. */);
30814 Voverlay_arrow_string = build_pure_c_string ("=>");
30816 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30817 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30818 The symbols on this list are examined during redisplay to determine
30819 where to display overlay arrows. */);
30820 Voverlay_arrow_variable_list
30821 = list1 (intern_c_string ("overlay-arrow-position"));
30823 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30824 doc: /* The number of lines to try scrolling a window by when point moves out.
30825 If that fails to bring point back on frame, point is centered instead.
30826 If this is zero, point is always centered after it moves off frame.
30827 If you want scrolling to always be a line at a time, you should set
30828 `scroll-conservatively' to a large value rather than set this to 1. */);
30830 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30831 doc: /* Scroll up to this many lines, to bring point back on screen.
30832 If point moves off-screen, redisplay will scroll by up to
30833 `scroll-conservatively' lines in order to bring point just barely
30834 onto the screen again. If that cannot be done, then redisplay
30835 recenters point as usual.
30837 If the value is greater than 100, redisplay will never recenter point,
30838 but will always scroll just enough text to bring point into view, even
30839 if you move far away.
30841 A value of zero means always recenter point if it moves off screen. */);
30842 scroll_conservatively = 0;
30844 DEFVAR_INT ("scroll-margin", scroll_margin,
30845 doc: /* Number of lines of margin at the top and bottom of a window.
30846 Recenter the window whenever point gets within this many lines
30847 of the top or bottom of the window. */);
30848 scroll_margin = 0;
30850 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30851 doc: /* Pixels per inch value for non-window system displays.
30852 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30853 Vdisplay_pixels_per_inch = make_float (72.0);
30855 #ifdef GLYPH_DEBUG
30856 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30857 #endif
30859 DEFVAR_LISP ("truncate-partial-width-windows",
30860 Vtruncate_partial_width_windows,
30861 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30862 For an integer value, truncate lines in each window narrower than the
30863 full frame width, provided the window width is less than that integer;
30864 otherwise, respect the value of `truncate-lines'.
30866 For any other non-nil value, truncate lines in all windows that do
30867 not span the full frame width.
30869 A value of nil means to respect the value of `truncate-lines'.
30871 If `word-wrap' is enabled, you might want to reduce this. */);
30872 Vtruncate_partial_width_windows = make_number (50);
30874 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30875 doc: /* Maximum buffer size for which line number should be displayed.
30876 If the buffer is bigger than this, the line number does not appear
30877 in the mode line. A value of nil means no limit. */);
30878 Vline_number_display_limit = Qnil;
30880 DEFVAR_INT ("line-number-display-limit-width",
30881 line_number_display_limit_width,
30882 doc: /* Maximum line width (in characters) for line number display.
30883 If the average length of the lines near point is bigger than this, then the
30884 line number may be omitted from the mode line. */);
30885 line_number_display_limit_width = 200;
30887 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30888 doc: /* Non-nil means highlight region even in nonselected windows. */);
30889 highlight_nonselected_windows = false;
30891 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30892 doc: /* Non-nil if more than one frame is visible on this display.
30893 Minibuffer-only frames don't count, but iconified frames do.
30894 This variable is not guaranteed to be accurate except while processing
30895 `frame-title-format' and `icon-title-format'. */);
30897 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30898 doc: /* Template for displaying the title bar of visible frames.
30899 \(Assuming the window manager supports this feature.)
30901 This variable has the same structure as `mode-line-format', except that
30902 the %c and %l constructs are ignored. It is used only on frames for
30903 which no explicit name has been set \(see `modify-frame-parameters'). */);
30905 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30906 doc: /* Template for displaying the title bar of an iconified frame.
30907 \(Assuming the window manager supports this feature.)
30908 This variable has the same structure as `mode-line-format' (which see),
30909 and is used only on frames for which no explicit name has been set
30910 \(see `modify-frame-parameters'). */);
30911 Vicon_title_format
30912 = Vframe_title_format
30913 = listn (CONSTYPE_PURE, 3,
30914 intern_c_string ("multiple-frames"),
30915 build_pure_c_string ("%b"),
30916 listn (CONSTYPE_PURE, 4,
30917 empty_unibyte_string,
30918 intern_c_string ("invocation-name"),
30919 build_pure_c_string ("@"),
30920 intern_c_string ("system-name")));
30922 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30923 doc: /* Maximum number of lines to keep in the message log buffer.
30924 If nil, disable message logging. If t, log messages but don't truncate
30925 the buffer when it becomes large. */);
30926 Vmessage_log_max = make_number (1000);
30928 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30929 doc: /* Functions called before redisplay, if window sizes have changed.
30930 The value should be a list of functions that take one argument.
30931 Just before redisplay, for each frame, if any of its windows have changed
30932 size since the last redisplay, or have been split or deleted,
30933 all the functions in the list are called, with the frame as argument. */);
30934 Vwindow_size_change_functions = Qnil;
30936 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30937 doc: /* List of functions to call before redisplaying a window with scrolling.
30938 Each function is called with two arguments, the window and its new
30939 display-start position.
30940 These functions are called whenever the `window-start' marker is modified,
30941 either to point into another buffer (e.g. via `set-window-buffer') or another
30942 place in the same buffer.
30943 Note that the value of `window-end' is not valid when these functions are
30944 called.
30946 Warning: Do not use this feature to alter the way the window
30947 is scrolled. It is not designed for that, and such use probably won't
30948 work. */);
30949 Vwindow_scroll_functions = Qnil;
30951 DEFVAR_LISP ("window-text-change-functions",
30952 Vwindow_text_change_functions,
30953 doc: /* Functions to call in redisplay when text in the window might change. */);
30954 Vwindow_text_change_functions = Qnil;
30956 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30957 doc: /* Functions called when redisplay of a window reaches the end trigger.
30958 Each function is called with two arguments, the window and the end trigger value.
30959 See `set-window-redisplay-end-trigger'. */);
30960 Vredisplay_end_trigger_functions = Qnil;
30962 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30963 doc: /* Non-nil means autoselect window with mouse pointer.
30964 If nil, do not autoselect windows.
30965 A positive number means delay autoselection by that many seconds: a
30966 window is autoselected only after the mouse has remained in that
30967 window for the duration of the delay.
30968 A negative number has a similar effect, but causes windows to be
30969 autoselected only after the mouse has stopped moving. \(Because of
30970 the way Emacs compares mouse events, you will occasionally wait twice
30971 that time before the window gets selected.\)
30972 Any other value means to autoselect window instantaneously when the
30973 mouse pointer enters it.
30975 Autoselection selects the minibuffer only if it is active, and never
30976 unselects the minibuffer if it is active.
30978 When customizing this variable make sure that the actual value of
30979 `focus-follows-mouse' matches the behavior of your window manager. */);
30980 Vmouse_autoselect_window = Qnil;
30982 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30983 doc: /* Non-nil means automatically resize tool-bars.
30984 This dynamically changes the tool-bar's height to the minimum height
30985 that is needed to make all tool-bar items visible.
30986 If value is `grow-only', the tool-bar's height is only increased
30987 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30988 Vauto_resize_tool_bars = Qt;
30990 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30991 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30992 auto_raise_tool_bar_buttons_p = true;
30994 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30995 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30996 make_cursor_line_fully_visible_p = true;
30998 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30999 doc: /* Border below tool-bar in pixels.
31000 If an integer, use it as the height of the border.
31001 If it is one of `internal-border-width' or `border-width', use the
31002 value of the corresponding frame parameter.
31003 Otherwise, no border is added below the tool-bar. */);
31004 Vtool_bar_border = Qinternal_border_width;
31006 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31007 doc: /* Margin around tool-bar buttons in pixels.
31008 If an integer, use that for both horizontal and vertical margins.
31009 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31010 HORZ specifying the horizontal margin, and VERT specifying the
31011 vertical margin. */);
31012 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31014 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31015 doc: /* Relief thickness of tool-bar buttons. */);
31016 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31018 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31019 doc: /* Tool bar style to use.
31020 It can be one of
31021 image - show images only
31022 text - show text only
31023 both - show both, text below image
31024 both-horiz - show text to the right of the image
31025 text-image-horiz - show text to the left of the image
31026 any other - use system default or image if no system default.
31028 This variable only affects the GTK+ toolkit version of Emacs. */);
31029 Vtool_bar_style = Qnil;
31031 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31032 doc: /* Maximum number of characters a label can have to be shown.
31033 The tool bar style must also show labels for this to have any effect, see
31034 `tool-bar-style'. */);
31035 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31037 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31038 doc: /* List of functions to call to fontify regions of text.
31039 Each function is called with one argument POS. Functions must
31040 fontify a region starting at POS in the current buffer, and give
31041 fontified regions the property `fontified'. */);
31042 Vfontification_functions = Qnil;
31043 Fmake_variable_buffer_local (Qfontification_functions);
31045 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31046 unibyte_display_via_language_environment,
31047 doc: /* Non-nil means display unibyte text according to language environment.
31048 Specifically, this means that raw bytes in the range 160-255 decimal
31049 are displayed by converting them to the equivalent multibyte characters
31050 according to the current language environment. As a result, they are
31051 displayed according to the current fontset.
31053 Note that this variable affects only how these bytes are displayed,
31054 but does not change the fact they are interpreted as raw bytes. */);
31055 unibyte_display_via_language_environment = false;
31057 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31058 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31059 If a float, it specifies a fraction of the mini-window frame's height.
31060 If an integer, it specifies a number of lines. */);
31061 Vmax_mini_window_height = make_float (0.25);
31063 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31064 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31065 A value of nil means don't automatically resize mini-windows.
31066 A value of t means resize them to fit the text displayed in them.
31067 A value of `grow-only', the default, means let mini-windows grow only;
31068 they return to their normal size when the minibuffer is closed, or the
31069 echo area becomes empty. */);
31070 Vresize_mini_windows = Qgrow_only;
31072 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31073 doc: /* Alist specifying how to blink the cursor off.
31074 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31075 `cursor-type' frame-parameter or variable equals ON-STATE,
31076 comparing using `equal', Emacs uses OFF-STATE to specify
31077 how to blink it off. ON-STATE and OFF-STATE are values for
31078 the `cursor-type' frame parameter.
31080 If a frame's ON-STATE has no entry in this list,
31081 the frame's other specifications determine how to blink the cursor off. */);
31082 Vblink_cursor_alist = Qnil;
31084 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31085 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31086 If non-nil, windows are automatically scrolled horizontally to make
31087 point visible. */);
31088 automatic_hscrolling_p = true;
31089 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31091 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31092 doc: /* How many columns away from the window edge point is allowed to get
31093 before automatic hscrolling will horizontally scroll the window. */);
31094 hscroll_margin = 5;
31096 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31097 doc: /* How many columns to scroll the window when point gets too close to the edge.
31098 When point is less than `hscroll-margin' columns from the window
31099 edge, automatic hscrolling will scroll the window by the amount of columns
31100 determined by this variable. If its value is a positive integer, scroll that
31101 many columns. If it's a positive floating-point number, it specifies the
31102 fraction of the window's width to scroll. If it's nil or zero, point will be
31103 centered horizontally after the scroll. Any other value, including negative
31104 numbers, are treated as if the value were zero.
31106 Automatic hscrolling always moves point outside the scroll margin, so if
31107 point was more than scroll step columns inside the margin, the window will
31108 scroll more than the value given by the scroll step.
31110 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31111 and `scroll-right' overrides this variable's effect. */);
31112 Vhscroll_step = make_number (0);
31114 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31115 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31116 Bind this around calls to `message' to let it take effect. */);
31117 message_truncate_lines = false;
31119 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31120 doc: /* Normal hook run to update the menu bar definitions.
31121 Redisplay runs this hook before it redisplays the menu bar.
31122 This is used to update menus such as Buffers, whose contents depend on
31123 various data. */);
31124 Vmenu_bar_update_hook = Qnil;
31126 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31127 doc: /* Frame for which we are updating a menu.
31128 The enable predicate for a menu binding should check this variable. */);
31129 Vmenu_updating_frame = Qnil;
31131 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31132 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31133 inhibit_menubar_update = false;
31135 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31136 doc: /* Prefix prepended to all continuation lines at display time.
31137 The value may be a string, an image, or a stretch-glyph; it is
31138 interpreted in the same way as the value of a `display' text property.
31140 This variable is overridden by any `wrap-prefix' text or overlay
31141 property.
31143 To add a prefix to non-continuation lines, use `line-prefix'. */);
31144 Vwrap_prefix = Qnil;
31145 DEFSYM (Qwrap_prefix, "wrap-prefix");
31146 Fmake_variable_buffer_local (Qwrap_prefix);
31148 DEFVAR_LISP ("line-prefix", Vline_prefix,
31149 doc: /* Prefix prepended to all non-continuation lines at display time.
31150 The value may be a string, an image, or a stretch-glyph; it is
31151 interpreted in the same way as the value of a `display' text property.
31153 This variable is overridden by any `line-prefix' text or overlay
31154 property.
31156 To add a prefix to continuation lines, use `wrap-prefix'. */);
31157 Vline_prefix = Qnil;
31158 DEFSYM (Qline_prefix, "line-prefix");
31159 Fmake_variable_buffer_local (Qline_prefix);
31161 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31162 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31163 inhibit_eval_during_redisplay = false;
31165 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31166 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31167 inhibit_free_realized_faces = false;
31169 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31170 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31171 Intended for use during debugging and for testing bidi display;
31172 see biditest.el in the test suite. */);
31173 inhibit_bidi_mirroring = false;
31175 #ifdef GLYPH_DEBUG
31176 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31177 doc: /* Inhibit try_window_id display optimization. */);
31178 inhibit_try_window_id = false;
31180 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31181 doc: /* Inhibit try_window_reusing display optimization. */);
31182 inhibit_try_window_reusing = false;
31184 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31185 doc: /* Inhibit try_cursor_movement display optimization. */);
31186 inhibit_try_cursor_movement = false;
31187 #endif /* GLYPH_DEBUG */
31189 DEFVAR_INT ("overline-margin", overline_margin,
31190 doc: /* Space between overline and text, in pixels.
31191 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31192 margin to the character height. */);
31193 overline_margin = 2;
31195 DEFVAR_INT ("underline-minimum-offset",
31196 underline_minimum_offset,
31197 doc: /* Minimum distance between baseline and underline.
31198 This can improve legibility of underlined text at small font sizes,
31199 particularly when using variable `x-use-underline-position-properties'
31200 with fonts that specify an UNDERLINE_POSITION relatively close to the
31201 baseline. The default value is 1. */);
31202 underline_minimum_offset = 1;
31204 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31205 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31206 This feature only works when on a window system that can change
31207 cursor shapes. */);
31208 display_hourglass_p = true;
31210 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31211 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31212 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31214 #ifdef HAVE_WINDOW_SYSTEM
31215 hourglass_atimer = NULL;
31216 hourglass_shown_p = false;
31217 #endif /* HAVE_WINDOW_SYSTEM */
31219 /* Name of the face used to display glyphless characters. */
31220 DEFSYM (Qglyphless_char, "glyphless-char");
31222 /* Method symbols for Vglyphless_char_display. */
31223 DEFSYM (Qhex_code, "hex-code");
31224 DEFSYM (Qempty_box, "empty-box");
31225 DEFSYM (Qthin_space, "thin-space");
31226 DEFSYM (Qzero_width, "zero-width");
31228 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31229 doc: /* Function run just before redisplay.
31230 It is called with one argument, which is the set of windows that are to
31231 be redisplayed. This set can be nil (meaning, only the selected window),
31232 or t (meaning all windows). */);
31233 Vpre_redisplay_function = intern ("ignore");
31235 /* Symbol for the purpose of Vglyphless_char_display. */
31236 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31237 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31239 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31240 doc: /* Char-table defining glyphless characters.
31241 Each element, if non-nil, should be one of the following:
31242 an ASCII acronym string: display this string in a box
31243 `hex-code': display the hexadecimal code of a character in a box
31244 `empty-box': display as an empty box
31245 `thin-space': display as 1-pixel width space
31246 `zero-width': don't display
31247 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31248 display method for graphical terminals and text terminals respectively.
31249 GRAPHICAL and TEXT should each have one of the values listed above.
31251 The char-table has one extra slot to control the display of a character for
31252 which no font is found. This slot only takes effect on graphical terminals.
31253 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31254 `thin-space'. The default is `empty-box'.
31256 If a character has a non-nil entry in an active display table, the
31257 display table takes effect; in this case, Emacs does not consult
31258 `glyphless-char-display' at all. */);
31259 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31260 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31261 Qempty_box);
31263 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31264 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31265 Vdebug_on_message = Qnil;
31267 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31268 doc: /* */);
31269 Vredisplay__all_windows_cause
31270 = Fmake_vector (make_number (100), make_number (0));
31272 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31273 doc: /* */);
31274 Vredisplay__mode_lines_cause
31275 = Fmake_vector (make_number (100), make_number (0));
31279 /* Initialize this module when Emacs starts. */
31281 void
31282 init_xdisp (void)
31284 CHARPOS (this_line_start_pos) = 0;
31286 if (!noninteractive)
31288 struct window *m = XWINDOW (minibuf_window);
31289 Lisp_Object frame = m->frame;
31290 struct frame *f = XFRAME (frame);
31291 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31292 struct window *r = XWINDOW (root);
31293 int i;
31295 echo_area_window = minibuf_window;
31297 r->top_line = FRAME_TOP_MARGIN (f);
31298 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31299 r->total_cols = FRAME_COLS (f);
31300 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31301 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31302 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31304 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31305 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31306 m->total_cols = FRAME_COLS (f);
31307 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31308 m->total_lines = 1;
31309 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31311 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31312 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31313 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31315 /* The default ellipsis glyphs `...'. */
31316 for (i = 0; i < 3; ++i)
31317 default_invis_vector[i] = make_number ('.');
31321 /* Allocate the buffer for frame titles.
31322 Also used for `format-mode-line'. */
31323 int size = 100;
31324 mode_line_noprop_buf = xmalloc (size);
31325 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31326 mode_line_noprop_ptr = mode_line_noprop_buf;
31327 mode_line_target = MODE_LINE_DISPLAY;
31330 help_echo_showing_p = false;
31333 #ifdef HAVE_WINDOW_SYSTEM
31335 /* Platform-independent portion of hourglass implementation. */
31337 /* Timer function of hourglass_atimer. */
31339 static void
31340 show_hourglass (struct atimer *timer)
31342 /* The timer implementation will cancel this timer automatically
31343 after this function has run. Set hourglass_atimer to null
31344 so that we know the timer doesn't have to be canceled. */
31345 hourglass_atimer = NULL;
31347 if (!hourglass_shown_p)
31349 Lisp_Object tail, frame;
31351 block_input ();
31353 FOR_EACH_FRAME (tail, frame)
31355 struct frame *f = XFRAME (frame);
31357 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31358 && FRAME_RIF (f)->show_hourglass)
31359 FRAME_RIF (f)->show_hourglass (f);
31362 hourglass_shown_p = true;
31363 unblock_input ();
31367 /* Cancel a currently active hourglass timer, and start a new one. */
31369 void
31370 start_hourglass (void)
31372 struct timespec delay;
31374 cancel_hourglass ();
31376 if (INTEGERP (Vhourglass_delay)
31377 && XINT (Vhourglass_delay) > 0)
31378 delay = make_timespec (min (XINT (Vhourglass_delay),
31379 TYPE_MAXIMUM (time_t)),
31381 else if (FLOATP (Vhourglass_delay)
31382 && XFLOAT_DATA (Vhourglass_delay) > 0)
31383 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31384 else
31385 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31387 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31388 show_hourglass, NULL);
31391 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31392 shown. */
31394 void
31395 cancel_hourglass (void)
31397 if (hourglass_atimer)
31399 cancel_atimer (hourglass_atimer);
31400 hourglass_atimer = NULL;
31403 if (hourglass_shown_p)
31405 Lisp_Object tail, frame;
31407 block_input ();
31409 FOR_EACH_FRAME (tail, frame)
31411 struct frame *f = XFRAME (frame);
31413 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31414 && FRAME_RIF (f)->hide_hourglass)
31415 FRAME_RIF (f)->hide_hourglass (f);
31416 #ifdef HAVE_NTGUI
31417 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31418 else if (!FRAME_W32_P (f))
31419 w32_arrow_cursor ();
31420 #endif
31423 hourglass_shown_p = false;
31424 unblock_input ();
31428 #endif /* HAVE_WINDOW_SYSTEM */