Fix redrawing non-selected frame after resize on MS-Windows
[emacs.git] / src / xdisp.c
blob1420a4a7e36e242ea7f60c2e5236df702bc20346
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 void
626 maybe_set_redisplay (Lisp_Object symbol)
628 if (HASH_TABLE_P (Vredisplay__variables)
629 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void redisplay_windows (Lisp_Object);
822 static void redisplay_window (Lisp_Object, bool);
823 static Lisp_Object redisplay_window_error (Lisp_Object);
824 static Lisp_Object redisplay_window_0 (Lisp_Object);
825 static Lisp_Object redisplay_window_1 (Lisp_Object);
826 static bool set_cursor_from_row (struct window *, struct glyph_row *,
827 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
828 int, int);
829 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
830 static bool update_menu_bar (struct frame *, bool, bool);
831 static bool try_window_reusing_current_matrix (struct window *);
832 static int try_window_id (struct window *);
833 static bool display_line (struct it *);
834 static int display_mode_lines (struct window *);
835 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
836 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
837 Lisp_Object, bool);
838 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
839 Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
843 ptrdiff_t *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static bool get_overlay_strings (struct it *, ptrdiff_t);
849 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, bool);
852 static void reseat_1 (struct it *, struct text_pos, bool);
853 static bool next_element_from_display_vector (struct it *);
854 static bool next_element_from_string (struct it *);
855 static bool next_element_from_c_string (struct it *);
856 static bool next_element_from_buffer (struct it *);
857 static bool next_element_from_composition (struct it *);
858 static bool next_element_from_image (struct it *);
859 static bool next_element_from_stretch (struct it *);
860 static bool next_element_from_xwidget (struct it *);
861 static void load_overlay_strings (struct it *, ptrdiff_t);
862 static bool get_next_display_element (struct it *);
863 static enum move_it_result
864 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
865 enum move_operation_enum);
866 static void get_visually_first_element (struct it *);
867 static void compute_stop_pos (struct it *);
868 static int face_before_or_after_it_pos (struct it *, bool);
869 static ptrdiff_t next_overlay_change (ptrdiff_t);
870 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
871 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
872 static int handle_single_display_spec (struct it *, Lisp_Object,
873 Lisp_Object, Lisp_Object,
874 struct text_pos *, ptrdiff_t, int, bool);
875 static int underlying_face_id (struct it *);
877 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
878 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
880 #ifdef HAVE_WINDOW_SYSTEM
882 static void update_tool_bar (struct frame *, bool);
883 static void x_draw_bottom_divider (struct window *w);
884 static void notice_overwritten_cursor (struct window *,
885 enum glyph_row_area,
886 int, int, int, int);
887 static int normal_char_height (struct font *, int);
888 static void normal_char_ascent_descent (struct font *, int, int *, int *);
890 static void append_stretch_glyph (struct it *, Lisp_Object,
891 int, int, int);
893 static Lisp_Object get_it_property (struct it *, Lisp_Object);
894 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
895 struct font *, int, bool);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return true if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1298 bool
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 bool visible_p = false;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. Also, someone might narrow-to-region and immediately
1322 call a scroll function. */
1323 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1324 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1326 /* If the top of the window is after CHARPOS, the latter is surely
1327 not visible. */
1328 if (charpos >= 0 && CHARPOS (top) > charpos)
1329 return visible_p;
1331 /* Compute exact mode line heights. */
1332 if (WINDOW_WANTS_MODELINE_P (w))
1333 w->mode_line_height
1334 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1335 BVAR (current_buffer, mode_line_format));
1337 if (WINDOW_WANTS_HEADER_LINE_P (w))
1338 w->header_line_height
1339 = display_mode_line (w, HEADER_LINE_FACE_ID,
1340 BVAR (current_buffer, header_line_format));
1342 start_display (&it, w, top);
1343 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1344 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1346 if (charpos >= 0
1347 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1348 && IT_CHARPOS (it) >= charpos)
1349 /* When scanning backwards under bidi iteration, move_it_to
1350 stops at or _before_ CHARPOS, because it stops at or to
1351 the _right_ of the character at CHARPOS. */
1352 || (it.bidi_p && it.bidi_it.scan_dir == -1
1353 && IT_CHARPOS (it) <= charpos)))
1355 /* We have reached CHARPOS, or passed it. How the call to
1356 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1357 or covered by a display property, move_it_to stops at the end
1358 of the invisible text, to the right of CHARPOS. (ii) If
1359 CHARPOS is in a display vector, move_it_to stops on its last
1360 glyph. */
1361 int top_x = it.current_x;
1362 int top_y = it.current_y;
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364 int bottom_y;
1365 struct it save_it;
1366 void *save_it_data = NULL;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 SAVE_IT (save_it, it, save_it_data);
1370 last_height = 0;
1371 bottom_y = line_bottom_y (&it);
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = true;
1376 if (bottom_y >= it.last_visible_y
1377 && it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) < charpos)
1380 /* When the last line of the window is scanned backwards
1381 under bidi iteration, we could be duped into thinking
1382 that we have passed CHARPOS, when in fact move_it_to
1383 simply stopped short of CHARPOS because it reached
1384 last_visible_y. To see if that's what happened, we call
1385 move_it_to again with a slightly larger vertical limit,
1386 and see if it actually moved vertically; if it did, we
1387 didn't really reach CHARPOS, which is beyond window end. */
1388 /* Why 10? because we don't know how many canonical lines
1389 will the height of the next line(s) be. So we guess. */
1390 int ten_more_lines = 10 * default_line_pixel_height (w);
1392 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1393 MOVE_TO_POS | MOVE_TO_Y);
1394 if (it.current_y > top_y)
1395 visible_p = false;
1398 RESTORE_IT (&it, &save_it, save_it_data);
1399 if (visible_p)
1401 if (it.method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2, it2_prev;
1410 /* The idea is to get to the previous buffer
1411 position, consume the character there, and use
1412 the pixel coordinates we get after that. But if
1413 the previous buffer position is also displayed
1414 from a display vector, we need to consume all of
1415 the glyphs from that display vector. */
1416 start_display (&it2, w, top);
1417 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1418 /* If we didn't get to CHARPOS - 1, there's some
1419 replacing display property at that position, and
1420 we stopped after it. That is exactly the place
1421 whose coordinates we want. */
1422 if (IT_CHARPOS (it2) != charpos - 1)
1423 it2_prev = it2;
1424 else
1426 /* Iterate until we get out of the display
1427 vector that displays the character at
1428 CHARPOS - 1. */
1429 do {
1430 get_next_display_element (&it2);
1431 PRODUCE_GLYPHS (&it2);
1432 it2_prev = it2;
1433 set_iterator_to_next (&it2, true);
1434 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1435 && IT_CHARPOS (it2) < charpos);
1437 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1438 || it2_prev.current_x > it2_prev.last_visible_x)
1439 top_x = it.glyph_row->x;
1440 else
1442 top_x = it2_prev.current_x;
1443 top_y = it2_prev.current_y;
1447 else if (IT_CHARPOS (it) != charpos)
1449 Lisp_Object cpos = make_number (charpos);
1450 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1451 Lisp_Object string = string_from_display_spec (spec);
1452 struct text_pos tpos;
1453 bool newline_in_string
1454 = (STRINGP (string)
1455 && memchr (SDATA (string), '\n', SBYTES (string)));
1457 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1458 bool replacing_spec_p
1459 = (!NILP (spec)
1460 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1461 charpos, FRAME_WINDOW_P (it.f)));
1462 /* The tricky code below is needed because there's a
1463 discrepancy between move_it_to and how we set cursor
1464 when PT is at the beginning of a portion of text
1465 covered by a display property or an overlay with a
1466 display property, or the display line ends in a
1467 newline from a display string. move_it_to will stop
1468 _after_ such display strings, whereas
1469 set_cursor_from_row conspires with cursor_row_p to
1470 place the cursor on the first glyph produced from the
1471 display string. */
1473 /* We have overshoot PT because it is covered by a
1474 display property that replaces the text it covers.
1475 If the string includes embedded newlines, we are also
1476 in the wrong display line. Backtrack to the correct
1477 line, where the display property begins. */
1478 if (replacing_spec_p)
1480 Lisp_Object startpos, endpos;
1481 EMACS_INT start, end;
1482 struct it it3;
1484 /* Find the first and the last buffer positions
1485 covered by the display string. */
1486 endpos =
1487 Fnext_single_char_property_change (cpos, Qdisplay,
1488 Qnil, Qnil);
1489 startpos =
1490 Fprevious_single_char_property_change (endpos, Qdisplay,
1491 Qnil, Qnil);
1492 start = XFASTINT (startpos);
1493 end = XFASTINT (endpos);
1494 /* Move to the last buffer position before the
1495 display property. */
1496 start_display (&it3, w, top);
1497 if (start > CHARPOS (top))
1498 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1499 /* Move forward one more line if the position before
1500 the display string is a newline or if it is the
1501 rightmost character on a line that is
1502 continued or word-wrapped. */
1503 if (it3.method == GET_FROM_BUFFER
1504 && (it3.c == '\n'
1505 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1506 move_it_by_lines (&it3, 1);
1507 else if (move_it_in_display_line_to (&it3, -1,
1508 it3.current_x
1509 + it3.pixel_width,
1510 MOVE_TO_X)
1511 == MOVE_LINE_CONTINUED)
1513 move_it_by_lines (&it3, 1);
1514 /* When we are under word-wrap, the #$@%!
1515 move_it_by_lines moves 2 lines, so we need to
1516 fix that up. */
1517 if (it3.line_wrap == WORD_WRAP)
1518 move_it_by_lines (&it3, -1);
1521 /* Record the vertical coordinate of the display
1522 line where we wound up. */
1523 top_y = it3.current_y;
1524 if (it3.bidi_p)
1526 /* When characters are reordered for display,
1527 the character displayed to the left of the
1528 display string could be _after_ the display
1529 property in the logical order. Use the
1530 smallest vertical position of these two. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1533 if (it3.current_y < top_y)
1534 top_y = it3.current_y;
1536 /* Move from the top of the window to the beginning
1537 of the display line where the display string
1538 begins. */
1539 start_display (&it3, w, top);
1540 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1541 /* If it3_moved stays false after the 'while' loop
1542 below, that means we already were at a newline
1543 before the loop (e.g., the display string begins
1544 with a newline), so we don't need to (and cannot)
1545 inspect the glyphs of it3.glyph_row, because
1546 PRODUCE_GLYPHS will not produce anything for a
1547 newline, and thus it3.glyph_row stays at its
1548 stale content it got at top of the window. */
1549 bool it3_moved = false;
1550 /* Finally, advance the iterator until we hit the
1551 first display element whose character position is
1552 CHARPOS, or until the first newline from the
1553 display string, which signals the end of the
1554 display line. */
1555 while (get_next_display_element (&it3))
1557 PRODUCE_GLYPHS (&it3);
1558 if (IT_CHARPOS (it3) == charpos
1559 || ITERATOR_AT_END_OF_LINE_P (&it3))
1560 break;
1561 it3_moved = true;
1562 set_iterator_to_next (&it3, false);
1564 top_x = it3.current_x - it3.pixel_width;
1565 /* Normally, we would exit the above loop because we
1566 found the display element whose character
1567 position is CHARPOS. For the contingency that we
1568 didn't, and stopped at the first newline from the
1569 display string, move back over the glyphs
1570 produced from the string, until we find the
1571 rightmost glyph not from the string. */
1572 if (it3_moved
1573 && newline_in_string
1574 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1576 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA];
1579 while (EQ ((g - 1)->object, string))
1581 --g;
1582 top_x -= g->pixel_width;
1584 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1585 + it3.glyph_row->used[TEXT_AREA]);
1590 *x = top_x;
1591 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1592 *rtop = max (0, window_top_y - top_y);
1593 *rbot = max (0, bottom_y - it.last_visible_y);
1594 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1595 - max (top_y, window_top_y)));
1596 *vpos = it.vpos;
1597 if (it.bidi_it.paragraph_dir == R2L)
1598 r2l = true;
1601 else
1603 /* Either we were asked to provide info about WINDOW_END, or
1604 CHARPOS is in the partially visible glyph row at end of
1605 window. */
1606 struct it it2;
1607 void *it2data = NULL;
1609 SAVE_IT (it2, it, it2data);
1610 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1611 move_it_by_lines (&it, 1);
1612 if (charpos < IT_CHARPOS (it)
1613 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1615 visible_p = true;
1616 RESTORE_IT (&it2, &it2, it2data);
1617 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1618 *x = it2.current_x;
1619 *y = it2.current_y + it2.max_ascent - it2.ascent;
1620 *rtop = max (0, -it2.current_y);
1621 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1622 - it.last_visible_y));
1623 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1624 it.last_visible_y)
1625 - max (it2.current_y,
1626 WINDOW_HEADER_LINE_HEIGHT (w))));
1627 *vpos = it2.vpos;
1628 if (it2.bidi_it.paragraph_dir == R2L)
1629 r2l = true;
1631 else
1632 bidi_unshelve_cache (it2data, true);
1634 bidi_unshelve_cache (itdata, false);
1636 if (old_buffer)
1637 set_buffer_internal_1 (old_buffer);
1639 if (visible_p)
1641 if (w->hscroll > 0)
1642 *x -=
1643 window_hscroll_limited (w, WINDOW_XFRAME (w))
1644 * WINDOW_FRAME_COLUMN_WIDTH (w);
1645 /* For lines in an R2L paragraph, we need to mirror the X pixel
1646 coordinate wrt the text area. For the reasons, see the
1647 commentary in buffer_posn_from_coords and the explanation of
1648 the geometry used by the move_it_* functions at the end of
1649 the large commentary near the beginning of this file. */
1650 if (r2l)
1651 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1654 #if false
1655 /* Debugging code. */
1656 if (visible_p)
1657 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1658 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1659 else
1660 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1661 #endif
1663 return visible_p;
1667 /* Return the next character from STR. Return in *LEN the length of
1668 the character. This is like STRING_CHAR_AND_LENGTH but never
1669 returns an invalid character. If we find one, we return a `?', but
1670 with the length of the invalid character. */
1672 static int
1673 string_char_and_length (const unsigned char *str, int *len)
1675 int c;
1677 c = STRING_CHAR_AND_LENGTH (str, *len);
1678 if (!CHAR_VALID_P (c))
1679 /* We may not change the length here because other places in Emacs
1680 don't use this function, i.e. they silently accept invalid
1681 characters. */
1682 c = '?';
1684 return c;
1689 /* Given a position POS containing a valid character and byte position
1690 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1692 static struct text_pos
1693 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1695 eassert (STRINGP (string) && nchars >= 0);
1697 if (STRING_MULTIBYTE (string))
1699 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1700 int len;
1702 while (nchars--)
1704 string_char_and_length (p, &len);
1705 p += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1710 else
1711 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1713 return pos;
1717 /* Value is the text position, i.e. character and byte position,
1718 for character position CHARPOS in STRING. */
1720 static struct text_pos
1721 string_pos (ptrdiff_t charpos, Lisp_Object string)
1723 struct text_pos pos;
1724 eassert (STRINGP (string));
1725 eassert (charpos >= 0);
1726 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1727 return pos;
1731 /* Value is a text position, i.e. character and byte position, for
1732 character position CHARPOS in C string S. MULTIBYTE_P
1733 means recognize multibyte characters. */
1735 static struct text_pos
1736 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1738 struct text_pos pos;
1740 eassert (s != NULL);
1741 eassert (charpos >= 0);
1743 if (multibyte_p)
1745 int len;
1747 SET_TEXT_POS (pos, 0, 0);
1748 while (charpos--)
1750 string_char_and_length ((const unsigned char *) s, &len);
1751 s += len;
1752 CHARPOS (pos) += 1;
1753 BYTEPOS (pos) += len;
1756 else
1757 SET_TEXT_POS (pos, charpos, charpos);
1759 return pos;
1763 /* Value is the number of characters in C string S. MULTIBYTE_P
1764 means recognize multibyte characters. */
1766 static ptrdiff_t
1767 number_of_chars (const char *s, bool multibyte_p)
1769 ptrdiff_t nchars;
1771 if (multibyte_p)
1773 ptrdiff_t rest = strlen (s);
1774 int len;
1775 const unsigned char *p = (const unsigned char *) s;
1777 for (nchars = 0; rest > 0; ++nchars)
1779 string_char_and_length (p, &len);
1780 rest -= len, p += len;
1783 else
1784 nchars = strlen (s);
1786 return nchars;
1790 /* Compute byte position NEWPOS->bytepos corresponding to
1791 NEWPOS->charpos. POS is a known position in string STRING.
1792 NEWPOS->charpos must be >= POS.charpos. */
1794 static void
1795 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1797 eassert (STRINGP (string));
1798 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1800 if (STRING_MULTIBYTE (string))
1801 *newpos = string_pos_nchars_ahead (pos, string,
1802 CHARPOS (*newpos) - CHARPOS (pos));
1803 else
1804 BYTEPOS (*newpos) = CHARPOS (*newpos);
1807 /* EXPORT:
1808 Return an estimation of the pixel height of mode or header lines on
1809 frame F. FACE_ID specifies what line's height to estimate. */
1812 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 if (FRAME_WINDOW_P (f))
1817 int height = FONT_HEIGHT (FRAME_FONT (f));
1819 /* This function is called so early when Emacs starts that the face
1820 cache and mode line face are not yet initialized. */
1821 if (FRAME_FACE_CACHE (f))
1823 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1824 if (face)
1826 if (face->font)
1827 height = normal_char_height (face->font, -1);
1828 if (face->box_line_width > 0)
1829 height += 2 * face->box_line_width;
1833 return height;
1835 #endif
1837 return 1;
1840 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1841 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1842 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1843 not force the value into range. */
1845 void
1846 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1847 NativeRectangle *bounds, bool noclip)
1850 #ifdef HAVE_WINDOW_SYSTEM
1851 if (FRAME_WINDOW_P (f))
1853 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1854 even for negative values. */
1855 if (pix_x < 0)
1856 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1857 if (pix_y < 0)
1858 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1860 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1861 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1863 if (bounds)
1864 STORE_NATIVE_RECT (*bounds,
1865 FRAME_COL_TO_PIXEL_X (f, pix_x),
1866 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1867 FRAME_COLUMN_WIDTH (f) - 1,
1868 FRAME_LINE_HEIGHT (f) - 1);
1870 /* PXW: Should we clip pixels before converting to columns/lines? */
1871 if (!noclip)
1873 if (pix_x < 0)
1874 pix_x = 0;
1875 else if (pix_x > FRAME_TOTAL_COLS (f))
1876 pix_x = FRAME_TOTAL_COLS (f);
1878 if (pix_y < 0)
1879 pix_y = 0;
1880 else if (pix_y > FRAME_TOTAL_LINES (f))
1881 pix_y = FRAME_TOTAL_LINES (f);
1884 #endif
1886 *x = pix_x;
1887 *y = pix_y;
1891 /* Find the glyph under window-relative coordinates X/Y in window W.
1892 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1893 strings. Return in *HPOS and *VPOS the row and column number of
1894 the glyph found. Return in *AREA the glyph area containing X.
1895 Value is a pointer to the glyph found or null if X/Y is not on
1896 text, or we can't tell because W's current matrix is not up to
1897 date. */
1899 static struct glyph *
1900 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1901 int *dx, int *dy, int *area)
1903 struct glyph *glyph, *end;
1904 struct glyph_row *row = NULL;
1905 int x0, i;
1907 /* Find row containing Y. Give up if some row is not enabled. */
1908 for (i = 0; i < w->current_matrix->nrows; ++i)
1910 row = MATRIX_ROW (w->current_matrix, i);
1911 if (!row->enabled_p)
1912 return NULL;
1913 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1914 break;
1917 *vpos = i;
1918 *hpos = 0;
1920 /* Give up if Y is not in the window. */
1921 if (i == w->current_matrix->nrows)
1922 return NULL;
1924 /* Get the glyph area containing X. */
1925 if (w->pseudo_window_p)
1927 *area = TEXT_AREA;
1928 x0 = 0;
1930 else
1932 if (x < window_box_left_offset (w, TEXT_AREA))
1934 *area = LEFT_MARGIN_AREA;
1935 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1937 else if (x < window_box_right_offset (w, TEXT_AREA))
1939 *area = TEXT_AREA;
1940 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1942 else
1944 *area = RIGHT_MARGIN_AREA;
1945 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1949 /* Find glyph containing X. */
1950 glyph = row->glyphs[*area];
1951 end = glyph + row->used[*area];
1952 x -= x0;
1953 while (glyph < end && x >= glyph->pixel_width)
1955 x -= glyph->pixel_width;
1956 ++glyph;
1959 if (glyph == end)
1960 return NULL;
1962 if (dx)
1964 *dx = x;
1965 *dy = y - (row->y + row->ascent - glyph->ascent);
1968 *hpos = glyph - row->glyphs[*area];
1969 return glyph;
1972 /* Convert frame-relative x/y to coordinates relative to window W.
1973 Takes pseudo-windows into account. */
1975 static void
1976 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1978 if (w->pseudo_window_p)
1980 /* A pseudo-window is always full-width, and starts at the
1981 left edge of the frame, plus a frame border. */
1982 struct frame *f = XFRAME (w->frame);
1983 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 else
1988 *x -= WINDOW_LEFT_EDGE_X (w);
1989 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1993 #ifdef HAVE_WINDOW_SYSTEM
1995 /* EXPORT:
1996 Return in RECTS[] at most N clipping rectangles for glyph string S.
1997 Return the number of stored rectangles. */
2000 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2002 XRectangle r;
2004 if (n <= 0)
2005 return 0;
2007 if (s->row->full_width_p)
2009 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2010 r.x = WINDOW_LEFT_EDGE_X (s->w);
2011 if (s->row->mode_line_p)
2012 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2013 else
2014 r.width = WINDOW_PIXEL_WIDTH (s->w);
2016 /* Unless displaying a mode or menu bar line, which are always
2017 fully visible, clip to the visible part of the row. */
2018 if (s->w->pseudo_window_p)
2019 r.height = s->row->visible_height;
2020 else
2021 r.height = s->height;
2023 else
2025 /* This is a text line that may be partially visible. */
2026 r.x = window_box_left (s->w, s->area);
2027 r.width = window_box_width (s->w, s->area);
2028 r.height = s->row->visible_height;
2031 if (s->clip_head)
2032 if (r.x < s->clip_head->x)
2034 if (r.width >= s->clip_head->x - r.x)
2035 r.width -= s->clip_head->x - r.x;
2036 else
2037 r.width = 0;
2038 r.x = s->clip_head->x;
2040 if (s->clip_tail)
2041 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2043 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2044 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2045 else
2046 r.width = 0;
2049 /* If S draws overlapping rows, it's sufficient to use the top and
2050 bottom of the window for clipping because this glyph string
2051 intentionally draws over other lines. */
2052 if (s->for_overlaps)
2054 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2055 r.height = window_text_bottom_y (s->w) - r.y;
2057 /* Alas, the above simple strategy does not work for the
2058 environments with anti-aliased text: if the same text is
2059 drawn onto the same place multiple times, it gets thicker.
2060 If the overlap we are processing is for the erased cursor, we
2061 take the intersection with the rectangle of the cursor. */
2062 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2064 XRectangle rc, r_save = r;
2066 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2067 rc.y = s->w->phys_cursor.y;
2068 rc.width = s->w->phys_cursor_width;
2069 rc.height = s->w->phys_cursor_height;
2071 x_intersect_rectangles (&r_save, &rc, &r);
2074 else
2076 /* Don't use S->y for clipping because it doesn't take partially
2077 visible lines into account. For example, it can be negative for
2078 partially visible lines at the top of a window. */
2079 if (!s->row->full_width_p
2080 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2081 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2082 else
2083 r.y = max (0, s->row->y);
2086 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2088 /* If drawing the cursor, don't let glyph draw outside its
2089 advertised boundaries. Cleartype does this under some circumstances. */
2090 if (s->hl == DRAW_CURSOR)
2092 struct glyph *glyph = s->first_glyph;
2093 int height, max_y;
2095 if (s->x > r.x)
2097 if (r.width >= s->x - r.x)
2098 r.width -= s->x - r.x;
2099 else /* R2L hscrolled row with cursor outside text area */
2100 r.width = 0;
2101 r.x = s->x;
2103 r.width = min (r.width, glyph->pixel_width);
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2112 r.y = max_y;
2113 r.height = height;
2115 else
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2128 if (s->row->clip)
2130 XRectangle r_save = r;
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2146 else
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2159 if (s->for_overlaps & OVERLAPS_PRED)
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2176 if (r.y + r.height > row_y + s->row->visible_height)
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2181 else
2182 rs[i].height = 0;
2184 i++;
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2202 get_glyph_string_clip_rects (s, nr, 1);
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0, ascent;
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width;
2224 x = w->phys_cursor.x;
2225 if (x < 0)
2227 wd += x;
2228 x = 0;
2231 if (glyph->type == STRETCH_GLYPH
2232 && !x_stretch_cursor_p)
2233 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2234 w->phys_cursor_width = wd;
2236 /* Don't let the hollow cursor glyph descend below the glyph row's
2237 ascent value, lest the hollow cursor looks funny. */
2238 y = w->phys_cursor.y;
2239 ascent = row->ascent;
2240 if (row->ascent < glyph->ascent)
2242 y -= glyph->ascent - row->ascent;
2243 ascent = glyph->ascent;
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249 h = max (h0, ascent + glyph->descent);
2250 h0 = min (h0, ascent + glyph->descent);
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2258 else
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2263 h += y - y0;
2264 y = y0;
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2274 * Remember which glyph the mouse is over.
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2290 if (window_resize_pixelwise)
2292 width = height = 1;
2293 goto virtual_glyph;
2295 else if (!f->glyphs_initialized_p
2296 || (window = window_from_coordinates (f, gx, gy, &part, false),
2297 NILP (window)))
2299 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2300 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2301 goto virtual_glyph;
2304 w = XWINDOW (window);
2305 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2306 height = WINDOW_FRAME_LINE_HEIGHT (w);
2308 x = window_relative_x_coord (w, part, gx);
2309 y = gy - WINDOW_TOP_EDGE_Y (w);
2311 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2312 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2314 if (w->pseudo_window_p)
2316 area = TEXT_AREA;
2317 part = ON_MODE_LINE; /* Don't adjust margin. */
2318 goto text_glyph;
2321 switch (part)
2323 case ON_LEFT_MARGIN:
2324 area = LEFT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_RIGHT_MARGIN:
2328 area = RIGHT_MARGIN_AREA;
2329 goto text_glyph;
2331 case ON_HEADER_LINE:
2332 case ON_MODE_LINE:
2333 gr = (part == ON_HEADER_LINE
2334 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2335 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2336 gy = gr->y;
2337 area = TEXT_AREA;
2338 goto text_glyph_row_found;
2340 case ON_TEXT:
2341 area = TEXT_AREA;
2343 text_glyph:
2344 gr = 0; gy = 0;
2345 for (; r <= end_row && r->enabled_p; ++r)
2346 if (r->y + r->height > y)
2348 gr = r; gy = r->y;
2349 break;
2352 text_glyph_row_found:
2353 if (gr && gy <= y)
2355 struct glyph *g = gr->glyphs[area];
2356 struct glyph *end = g + gr->used[area];
2358 height = gr->height;
2359 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2360 if (gx + g->pixel_width > x)
2361 break;
2363 if (g < end)
2365 if (g->type == IMAGE_GLYPH)
2367 /* Don't remember when mouse is over image, as
2368 image may have hot-spots. */
2369 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2370 return;
2372 width = g->pixel_width;
2374 else
2376 /* Use nominal char spacing at end of line. */
2377 x -= gx;
2378 gx += (x / width) * width;
2381 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2383 gx += window_box_left_offset (w, area);
2384 /* Don't expand over the modeline to make sure the vertical
2385 drag cursor is shown early enough. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2390 else
2392 /* Use nominal line height at end of window. */
2393 gx = (x / width) * width;
2394 y -= gy;
2395 gy += (y / height) * height;
2396 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2397 /* See comment above. */
2398 height = min (height,
2399 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2401 break;
2403 case ON_LEFT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2406 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2407 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2408 goto row_glyph;
2410 case ON_RIGHT_FRINGE:
2411 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2412 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2413 : window_box_right_offset (w, TEXT_AREA));
2414 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2415 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2416 && !WINDOW_RIGHTMOST_P (w))
2417 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2418 /* Make sure the vertical border can get her own glyph to the
2419 right of the one we build here. */
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2421 else
2422 width = WINDOW_PIXEL_WIDTH (w) - gx;
2423 else
2424 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2426 goto row_glyph;
2428 case ON_VERTICAL_BORDER:
2429 gx = WINDOW_PIXEL_WIDTH (w) - width;
2430 goto row_glyph;
2432 case ON_VERTICAL_SCROLL_BAR:
2433 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2436 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2438 : 0)));
2439 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441 row_glyph:
2442 gr = 0, gy = 0;
2443 for (; r <= end_row && r->enabled_p; ++r)
2444 if (r->y + r->height > y)
2446 gr = r; gy = r->y;
2447 break;
2450 if (gr && gy <= y)
2451 height = gr->height;
2452 else
2454 /* Use nominal line height at end of window. */
2455 y -= gy;
2456 gy += (y / height) * height;
2458 break;
2460 case ON_RIGHT_DIVIDER:
2461 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2462 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2463 gy = 0;
2464 /* The bottom divider prevails. */
2465 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 case ON_BOTTOM_DIVIDER:
2469 gx = 0;
2470 width = WINDOW_PIXEL_WIDTH (w);
2471 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2472 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2473 goto add_edge;
2475 default:
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2492 goto store_rect;
2495 add_edge:
2496 gx += WINDOW_LEFT_EDGE_X (w);
2497 gy += WINDOW_TOP_EDGE_Y (w);
2499 store_rect:
2500 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2502 /* Visible feedback for debugging. */
2503 #if false && defined HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2511 #endif /* HAVE_WINDOW_SYSTEM */
2513 static void
2514 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 eassert (w);
2517 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2518 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2519 w->window_end_vpos
2520 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2523 /***********************************************************************
2524 Lisp form evaluation
2525 ***********************************************************************/
2527 /* Error handler for safe_eval and safe_call. */
2529 static Lisp_Object
2530 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 add_to_log ("Error during redisplay: %S signaled %S",
2533 Flist (nargs, args), arg);
2534 return Qnil;
2537 /* Call function FUNC with the rest of NARGS - 1 arguments
2538 following. Return the result, or nil if something went
2539 wrong. Prevent redisplay during the evaluation. */
2541 static Lisp_Object
2542 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2544 Lisp_Object val;
2546 if (inhibit_eval_during_redisplay)
2547 val = Qnil;
2548 else
2550 ptrdiff_t i;
2551 ptrdiff_t count = SPECPDL_INDEX ();
2552 Lisp_Object *args;
2553 USE_SAFE_ALLOCA;
2554 SAFE_ALLOCA_LISP (args, nargs);
2556 args[0] = func;
2557 for (i = 1; i < nargs; i++)
2558 args[i] = va_arg (ap, Lisp_Object);
2560 specbind (Qinhibit_redisplay, Qt);
2561 if (inhibit_quit)
2562 specbind (Qinhibit_quit, Qt);
2563 /* Use Qt to ensure debugger does not run,
2564 so there is no possibility of wanting to redisplay. */
2565 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2566 safe_eval_handler);
2567 SAFE_FREE ();
2568 val = unbind_to (count, val);
2571 return val;
2574 Lisp_Object
2575 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2577 Lisp_Object retval;
2578 va_list ap;
2580 va_start (ap, func);
2581 retval = safe__call (false, nargs, func, ap);
2582 va_end (ap);
2583 return retval;
2586 /* Call function FN with one argument ARG.
2587 Return the result, or nil if something went wrong. */
2589 Lisp_Object
2590 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2592 return safe_call (2, fn, arg);
2595 static Lisp_Object
2596 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2598 Lisp_Object retval;
2599 va_list ap;
2601 va_start (ap, fn);
2602 retval = safe__call (inhibit_quit, 2, fn, ap);
2603 va_end (ap);
2604 return retval;
2607 Lisp_Object
2608 safe_eval (Lisp_Object sexpr)
2610 return safe__call1 (false, Qeval, sexpr);
2613 static Lisp_Object
2614 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2616 return safe__call1 (inhibit_quit, Qeval, sexpr);
2619 /* Call function FN with two arguments ARG1 and ARG2.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2625 return safe_call (3, fn, arg1, arg2);
2630 /***********************************************************************
2631 Debugging
2632 ***********************************************************************/
2634 /* Define CHECK_IT to perform sanity checks on iterators.
2635 This is for debugging. It is too slow to do unconditionally. */
2637 static void
2638 CHECK_IT (struct it *it)
2640 #if false
2641 if (it->method == GET_FROM_STRING)
2643 eassert (STRINGP (it->string));
2644 eassert (IT_STRING_CHARPOS (*it) >= 0);
2646 else
2648 eassert (IT_STRING_CHARPOS (*it) < 0);
2649 if (it->method == GET_FROM_BUFFER)
2651 /* Check that character and byte positions agree. */
2652 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2656 if (it->dpvec)
2657 eassert (it->current.dpvec_index >= 0);
2658 else
2659 eassert (it->current.dpvec_index < 0);
2660 #endif
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 CHECK_WINDOW_END (struct window *w)
2670 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2671 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2673 struct glyph_row *row;
2674 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2675 !row->enabled_p
2676 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2677 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2679 #endif
2682 /***********************************************************************
2683 Iterator initialization
2684 ***********************************************************************/
2686 /* Initialize IT for displaying current_buffer in window W, starting
2687 at character position CHARPOS. CHARPOS < 0 means that no buffer
2688 position is specified which is useful when the iterator is assigned
2689 a position later. BYTEPOS is the byte position corresponding to
2690 CHARPOS.
2692 If ROW is not null, calls to produce_glyphs with IT as parameter
2693 will produce glyphs in that row.
2695 BASE_FACE_ID is the id of a base face to use. It must be one of
2696 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2697 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2698 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2700 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2701 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2702 will be initialized to use the corresponding mode line glyph row of
2703 the desired matrix of W. */
2705 void
2706 init_iterator (struct it *it, struct window *w,
2707 ptrdiff_t charpos, ptrdiff_t bytepos,
2708 struct glyph_row *row, enum face_id base_face_id)
2710 enum face_id remapped_base_face_id = base_face_id;
2712 /* Some precondition checks. */
2713 eassert (w != NULL && it != NULL);
2714 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2715 && charpos <= ZV));
2717 /* If face attributes have been changed since the last redisplay,
2718 free realized faces now because they depend on face definitions
2719 that might have changed. Don't free faces while there might be
2720 desired matrices pending which reference these faces. */
2721 if (!inhibit_free_realized_faces)
2723 if (face_change)
2725 face_change = false;
2726 free_all_realized_faces (Qnil);
2728 else if (XFRAME (w->frame)->face_change)
2730 XFRAME (w->frame)->face_change = 0;
2731 free_all_realized_faces (w->frame);
2735 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2736 if (! NILP (Vface_remapping_alist))
2737 remapped_base_face_id
2738 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2740 /* Use one of the mode line rows of W's desired matrix if
2741 appropriate. */
2742 if (row == NULL)
2744 if (base_face_id == MODE_LINE_FACE_ID
2745 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2746 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2747 else if (base_face_id == HEADER_LINE_FACE_ID)
2748 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2751 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2752 Other parts of redisplay rely on that. */
2753 memclear (it, sizeof *it);
2754 it->current.overlay_string_index = -1;
2755 it->current.dpvec_index = -1;
2756 it->base_face_id = remapped_base_face_id;
2757 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2758 it->paragraph_embedding = L2R;
2759 it->bidi_it.w = w;
2761 /* The window in which we iterate over current_buffer: */
2762 XSETWINDOW (it->window, w);
2763 it->w = w;
2764 it->f = XFRAME (w->frame);
2766 it->cmp_it.id = -1;
2768 /* Extra space between lines (on window systems only). */
2769 if (base_face_id == DEFAULT_FACE_ID
2770 && FRAME_WINDOW_P (it->f))
2772 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2774 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2776 * FRAME_LINE_HEIGHT (it->f));
2777 else if (it->f->extra_line_spacing > 0)
2778 it->extra_line_spacing = it->f->extra_line_spacing;
2781 /* If realized faces have been removed, e.g. because of face
2782 attribute changes of named faces, recompute them. When running
2783 in batch mode, the face cache of the initial frame is null. If
2784 we happen to get called, make a dummy face cache. */
2785 if (FRAME_FACE_CACHE (it->f) == NULL)
2786 init_frame_faces (it->f);
2787 if (FRAME_FACE_CACHE (it->f)->used == 0)
2788 recompute_basic_faces (it->f);
2790 it->override_ascent = -1;
2792 /* Are control characters displayed as `^C'? */
2793 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2795 /* -1 means everything between a CR and the following line end
2796 is invisible. >0 means lines indented more than this value are
2797 invisible. */
2798 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2799 ? (clip_to_bounds
2800 (-1, XINT (BVAR (current_buffer, selective_display)),
2801 PTRDIFF_MAX))
2802 : (!NILP (BVAR (current_buffer, selective_display))
2803 ? -1 : 0));
2804 it->selective_display_ellipsis_p
2805 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2807 /* Display table to use. */
2808 it->dp = window_display_table (w);
2810 /* Are multibyte characters enabled in current_buffer? */
2811 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2813 /* Get the position at which the redisplay_end_trigger hook should
2814 be run, if it is to be run at all. */
2815 if (MARKERP (w->redisplay_end_trigger)
2816 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2817 it->redisplay_end_trigger_charpos
2818 = marker_position (w->redisplay_end_trigger);
2819 else if (INTEGERP (w->redisplay_end_trigger))
2820 it->redisplay_end_trigger_charpos
2821 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2822 PTRDIFF_MAX);
2824 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2826 /* Are lines in the display truncated? */
2827 if (TRUNCATE != 0)
2828 it->line_wrap = TRUNCATE;
2829 if (base_face_id == DEFAULT_FACE_ID
2830 && !it->w->hscroll
2831 && (WINDOW_FULL_WIDTH_P (it->w)
2832 || NILP (Vtruncate_partial_width_windows)
2833 || (INTEGERP (Vtruncate_partial_width_windows)
2834 /* PXW: Shall we do something about this? */
2835 && (XINT (Vtruncate_partial_width_windows)
2836 <= WINDOW_TOTAL_COLS (it->w))))
2837 && NILP (BVAR (current_buffer, truncate_lines)))
2838 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2839 ? WINDOW_WRAP : WORD_WRAP;
2841 /* Get dimensions of truncation and continuation glyphs. These are
2842 displayed as fringe bitmaps under X, but we need them for such
2843 frames when the fringes are turned off. But leave the dimensions
2844 zero for tooltip frames, as these glyphs look ugly there and also
2845 sabotage calculations of tooltip dimensions in x-show-tip. */
2846 #ifdef HAVE_WINDOW_SYSTEM
2847 if (!(FRAME_WINDOW_P (it->f)
2848 && FRAMEP (tip_frame)
2849 && it->f == XFRAME (tip_frame)))
2850 #endif
2852 if (it->line_wrap == TRUNCATE)
2854 /* We will need the truncation glyph. */
2855 eassert (it->glyph_row == NULL);
2856 produce_special_glyphs (it, IT_TRUNCATION);
2857 it->truncation_pixel_width = it->pixel_width;
2859 else
2861 /* We will need the continuation glyph. */
2862 eassert (it->glyph_row == NULL);
2863 produce_special_glyphs (it, IT_CONTINUATION);
2864 it->continuation_pixel_width = it->pixel_width;
2868 /* Reset these values to zero because the produce_special_glyphs
2869 above has changed them. */
2870 it->pixel_width = it->ascent = it->descent = 0;
2871 it->phys_ascent = it->phys_descent = 0;
2873 /* Set this after getting the dimensions of truncation and
2874 continuation glyphs, so that we don't produce glyphs when calling
2875 produce_special_glyphs, above. */
2876 it->glyph_row = row;
2877 it->area = TEXT_AREA;
2879 /* Get the dimensions of the display area. The display area
2880 consists of the visible window area plus a horizontally scrolled
2881 part to the left of the window. All x-values are relative to the
2882 start of this total display area. */
2883 if (base_face_id != DEFAULT_FACE_ID)
2885 /* Mode lines, menu bar in terminal frames. */
2886 it->first_visible_x = 0;
2887 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2889 else
2891 it->first_visible_x
2892 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2893 it->last_visible_x = (it->first_visible_x
2894 + window_box_width (w, TEXT_AREA));
2896 /* If we truncate lines, leave room for the truncation glyph(s) at
2897 the right margin. Otherwise, leave room for the continuation
2898 glyph(s). Done only if the window has no right fringe. */
2899 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2901 if (it->line_wrap == TRUNCATE)
2902 it->last_visible_x -= it->truncation_pixel_width;
2903 else
2904 it->last_visible_x -= it->continuation_pixel_width;
2907 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2908 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 /* Leave room for a border glyph. */
2912 if (!FRAME_WINDOW_P (it->f)
2913 && !WINDOW_RIGHTMOST_P (it->w))
2914 it->last_visible_x -= 1;
2916 it->last_visible_y = window_text_bottom_y (w);
2918 /* For mode lines and alike, arrange for the first glyph having a
2919 left box line if the face specifies a box. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 struct face *face;
2924 it->face_id = remapped_base_face_id;
2926 /* If we have a boxed mode line, make the first character appear
2927 with a left box line. */
2928 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2929 if (face && face->box != FACE_NO_BOX)
2930 it->start_of_box_run_p = true;
2933 /* If a buffer position was specified, set the iterator there,
2934 getting overlays and face properties from that position. */
2935 if (charpos >= BUF_BEG (current_buffer))
2937 it->stop_charpos = charpos;
2938 it->end_charpos = ZV;
2939 eassert (charpos == BYTE_TO_CHAR (bytepos));
2940 IT_CHARPOS (*it) = charpos;
2941 IT_BYTEPOS (*it) = bytepos;
2943 /* We will rely on `reseat' to set this up properly, via
2944 handle_face_prop. */
2945 it->face_id = it->base_face_id;
2947 it->start = it->current;
2948 /* Do we need to reorder bidirectional text? Not if this is a
2949 unibyte buffer: by definition, none of the single-byte
2950 characters are strong R2L, so no reordering is needed. And
2951 bidi.c doesn't support unibyte buffers anyway. Also, don't
2952 reorder while we are loading loadup.el, since the tables of
2953 character properties needed for reordering are not yet
2954 available. */
2955 it->bidi_p =
2956 !redisplay__inhibit_bidi
2957 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2958 && it->multibyte_p;
2960 /* If we are to reorder bidirectional text, init the bidi
2961 iterator. */
2962 if (it->bidi_p)
2964 /* Since we don't know at this point whether there will be
2965 any R2L lines in the window, we reserve space for
2966 truncation/continuation glyphs even if only the left
2967 fringe is absent. */
2968 if (base_face_id == DEFAULT_FACE_ID
2969 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2970 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2972 if (it->line_wrap == TRUNCATE)
2973 it->last_visible_x -= it->truncation_pixel_width;
2974 else
2975 it->last_visible_x -= it->continuation_pixel_width;
2977 /* Note the paragraph direction that this buffer wants to
2978 use. */
2979 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2980 Qleft_to_right))
2981 it->paragraph_embedding = L2R;
2982 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2983 Qright_to_left))
2984 it->paragraph_embedding = R2L;
2985 else
2986 it->paragraph_embedding = NEUTRAL_DIR;
2987 bidi_unshelve_cache (NULL, false);
2988 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2989 &it->bidi_it);
2992 /* Compute faces etc. */
2993 reseat (it, it->current.pos, true);
2996 CHECK_IT (it);
3000 /* Initialize IT for the display of window W with window start POS. */
3002 void
3003 start_display (struct it *it, struct window *w, struct text_pos pos)
3005 struct glyph_row *row;
3006 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3008 row = w->desired_matrix->rows + first_vpos;
3009 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3010 it->first_vpos = first_vpos;
3012 /* Don't reseat to previous visible line start if current start
3013 position is in a string or image. */
3014 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3016 int first_y = it->current_y;
3018 /* If window start is not at a line start, skip forward to POS to
3019 get the correct continuation lines width. */
3020 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3021 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3022 if (!start_at_line_beg_p)
3024 int new_x;
3026 reseat_at_previous_visible_line_start (it);
3027 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3029 new_x = it->current_x + it->pixel_width;
3031 /* If lines are continued, this line may end in the middle
3032 of a multi-glyph character (e.g. a control character
3033 displayed as \003, or in the middle of an overlay
3034 string). In this case move_it_to above will not have
3035 taken us to the start of the continuation line but to the
3036 end of the continued line. */
3037 if (it->current_x > 0
3038 && it->line_wrap != TRUNCATE /* Lines are continued. */
3039 && (/* And glyph doesn't fit on the line. */
3040 new_x > it->last_visible_x
3041 /* Or it fits exactly and we're on a window
3042 system frame. */
3043 || (new_x == it->last_visible_x
3044 && FRAME_WINDOW_P (it->f)
3045 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3046 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3047 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3049 if ((it->current.dpvec_index >= 0
3050 || it->current.overlay_string_index >= 0)
3051 /* If we are on a newline from a display vector or
3052 overlay string, then we are already at the end of
3053 a screen line; no need to go to the next line in
3054 that case, as this line is not really continued.
3055 (If we do go to the next line, C-e will not DTRT.) */
3056 && it->c != '\n')
3058 set_iterator_to_next (it, true);
3059 move_it_in_display_line_to (it, -1, -1, 0);
3062 it->continuation_lines_width += it->current_x;
3064 /* If the character at POS is displayed via a display
3065 vector, move_it_to above stops at the final glyph of
3066 IT->dpvec. To make the caller redisplay that character
3067 again (a.k.a. start at POS), we need to reset the
3068 dpvec_index to the beginning of IT->dpvec. */
3069 else if (it->current.dpvec_index >= 0)
3070 it->current.dpvec_index = 0;
3072 /* We're starting a new display line, not affected by the
3073 height of the continued line, so clear the appropriate
3074 fields in the iterator structure. */
3075 it->max_ascent = it->max_descent = 0;
3076 it->max_phys_ascent = it->max_phys_descent = 0;
3078 it->current_y = first_y;
3079 it->vpos = 0;
3080 it->current_x = it->hpos = 0;
3086 /* Return true if POS is a position in ellipses displayed for invisible
3087 text. W is the window we display, for text property lookup. */
3089 static bool
3090 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3092 Lisp_Object prop, window;
3093 bool ellipses_p = false;
3094 ptrdiff_t charpos = CHARPOS (pos->pos);
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (pos->dpvec_index >= 0
3101 && pos->overlay_string_index < 0
3102 && CHARPOS (pos->string_pos) < 0
3103 && charpos > BEGV
3104 && (XSETWINDOW (window, w),
3105 prop = Fget_char_property (make_number (charpos),
3106 Qinvisible, window),
3107 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3109 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3110 window);
3111 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3114 return ellipses_p;
3118 /* Initialize IT for stepping through current_buffer in window W,
3119 starting at position POS that includes overlay string and display
3120 vector/ control character translation position information. Value
3121 is false if there are overlay strings with newlines at POS. */
3123 static bool
3124 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3126 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3127 int i;
3128 bool overlay_strings_with_newlines = false;
3130 /* If POS specifies a position in a display vector, this might
3131 be for an ellipsis displayed for invisible text. We won't
3132 get the iterator set up for delivering that ellipsis unless
3133 we make sure that it gets aware of the invisible text. */
3134 if (in_ellipses_for_invisible_text_p (pos, w))
3136 --charpos;
3137 bytepos = 0;
3140 /* Keep in mind: the call to reseat in init_iterator skips invisible
3141 text, so we might end up at a position different from POS. This
3142 is only a problem when POS is a row start after a newline and an
3143 overlay starts there with an after-string, and the overlay has an
3144 invisible property. Since we don't skip invisible text in
3145 display_line and elsewhere immediately after consuming the
3146 newline before the row start, such a POS will not be in a string,
3147 but the call to init_iterator below will move us to the
3148 after-string. */
3149 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3151 /* This only scans the current chunk -- it should scan all chunks.
3152 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3153 to 16 in 22.1 to make this a lesser problem. */
3154 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3156 const char *s = SSDATA (it->overlay_strings[i]);
3157 const char *e = s + SBYTES (it->overlay_strings[i]);
3159 while (s < e && *s != '\n')
3160 ++s;
3162 if (s < e)
3164 overlay_strings_with_newlines = true;
3165 break;
3169 /* If position is within an overlay string, set up IT to the right
3170 overlay string. */
3171 if (pos->overlay_string_index >= 0)
3173 int relative_index;
3175 /* If the first overlay string happens to have a `display'
3176 property for an image, the iterator will be set up for that
3177 image, and we have to undo that setup first before we can
3178 correct the overlay string index. */
3179 if (it->method == GET_FROM_IMAGE)
3180 pop_it (it);
3182 /* We already have the first chunk of overlay strings in
3183 IT->overlay_strings. Load more until the one for
3184 pos->overlay_string_index is in IT->overlay_strings. */
3185 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3187 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3188 it->current.overlay_string_index = 0;
3189 while (n--)
3191 load_overlay_strings (it, 0);
3192 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = pos->overlay_string_index;
3197 relative_index = (it->current.overlay_string_index
3198 % OVERLAY_STRING_CHUNK_SIZE);
3199 it->string = it->overlay_strings[relative_index];
3200 eassert (STRINGP (it->string));
3201 it->current.string_pos = pos->string_pos;
3202 it->method = GET_FROM_STRING;
3203 it->end_charpos = SCHARS (it->string);
3204 /* Set up the bidi iterator for this overlay string. */
3205 if (it->bidi_p)
3207 it->bidi_it.string.lstring = it->string;
3208 it->bidi_it.string.s = NULL;
3209 it->bidi_it.string.schars = SCHARS (it->string);
3210 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3211 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3212 it->bidi_it.string.unibyte = !it->multibyte_p;
3213 it->bidi_it.w = it->w;
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3217 /* Synchronize the state of the bidi iterator with
3218 pos->string_pos. For any string position other than
3219 zero, this will be done automagically when we resume
3220 iteration over the string and get_visually_first_element
3221 is called. But if string_pos is zero, and the string is
3222 to be reordered for display, we need to resync manually,
3223 since it could be that the iteration state recorded in
3224 pos ended at string_pos of 0 moving backwards in string. */
3225 if (CHARPOS (pos->string_pos) == 0)
3227 get_visually_first_element (it);
3228 if (IT_STRING_CHARPOS (*it) != 0)
3229 do {
3230 /* Paranoia. */
3231 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3232 bidi_move_to_visually_next (&it->bidi_it);
3233 } while (it->bidi_it.charpos != 0);
3235 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3236 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3240 if (CHARPOS (pos->string_pos) >= 0)
3242 /* Recorded position is not in an overlay string, but in another
3243 string. This can only be a string from a `display' property.
3244 IT should already be filled with that string. */
3245 it->current.string_pos = pos->string_pos;
3246 eassert (STRINGP (it->string));
3247 if (it->bidi_p)
3248 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3249 FRAME_WINDOW_P (it->f), &it->bidi_it);
3252 /* Restore position in display vector translations, control
3253 character translations or ellipses. */
3254 if (pos->dpvec_index >= 0)
3256 if (it->dpvec == NULL)
3257 get_next_display_element (it);
3258 eassert (it->dpvec && it->current.dpvec_index == 0);
3259 it->current.dpvec_index = pos->dpvec_index;
3262 CHECK_IT (it);
3263 return !overlay_strings_with_newlines;
3267 /* Initialize IT for stepping through current_buffer in window W
3268 starting at ROW->start. */
3270 static void
3271 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3273 init_from_display_pos (it, w, &row->start);
3274 it->start = row->start;
3275 it->continuation_lines_width = row->continuation_lines_width;
3276 CHECK_IT (it);
3280 /* Initialize IT for stepping through current_buffer in window W
3281 starting in the line following ROW, i.e. starting at ROW->end.
3282 Value is false if there are overlay strings with newlines at ROW's
3283 end position. */
3285 static bool
3286 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3288 bool success = false;
3290 if (init_from_display_pos (it, w, &row->end))
3292 if (row->continued_p)
3293 it->continuation_lines_width
3294 = row->continuation_lines_width + row->pixel_width;
3295 CHECK_IT (it);
3296 success = true;
3299 return success;
3305 /***********************************************************************
3306 Text properties
3307 ***********************************************************************/
3309 /* Called when IT reaches IT->stop_charpos. Handle text property and
3310 overlay changes. Set IT->stop_charpos to the next position where
3311 to stop. */
3313 static void
3314 handle_stop (struct it *it)
3316 enum prop_handled handled;
3317 bool handle_overlay_change_p;
3318 struct props *p;
3320 it->dpvec = NULL;
3321 it->current.dpvec_index = -1;
3322 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3323 it->ellipsis_p = false;
3325 /* Use face of preceding text for ellipsis (if invisible) */
3326 if (it->selective_display_ellipsis_p)
3327 it->saved_face_id = it->face_id;
3329 /* Here's the description of the semantics of, and the logic behind,
3330 the various HANDLED_* statuses:
3332 HANDLED_NORMALLY means the handler did its job, and the loop
3333 should proceed to calling the next handler in order.
3335 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3336 change in the properties and overlays at current position, so the
3337 loop should be restarted, to re-invoke the handlers that were
3338 already called. This happens when fontification-functions were
3339 called by handle_fontified_prop, and actually fontified
3340 something. Another case where HANDLED_RECOMPUTE_PROPS is
3341 returned is when we discover overlay strings that need to be
3342 displayed right away. The loop below will continue for as long
3343 as the status is HANDLED_RECOMPUTE_PROPS.
3345 HANDLED_RETURN means return immediately to the caller, to
3346 continue iteration without calling any further handlers. This is
3347 used when we need to act on some property right away, for example
3348 when we need to display the ellipsis or a replacing display
3349 property, such as display string or image.
3351 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3352 consumed, and the handler switched to the next overlay string.
3353 This signals the loop below to refrain from looking for more
3354 overlays before all the overlay strings of the current overlay
3355 are processed.
3357 Some of the handlers called by the loop push the iterator state
3358 onto the stack (see 'push_it'), and arrange for the iteration to
3359 continue with another object, such as an image, a display string,
3360 or an overlay string. In most such cases, it->stop_charpos is
3361 set to the first character of the string, so that when the
3362 iteration resumes, this function will immediately be called
3363 again, to examine the properties at the beginning of the string.
3365 When a display or overlay string is exhausted, the iterator state
3366 is popped (see 'pop_it'), and iteration continues with the
3367 previous object. Again, in many such cases this function is
3368 called again to find the next position where properties might
3369 change. */
3373 handled = HANDLED_NORMALLY;
3375 /* Call text property handlers. */
3376 for (p = it_props; p->handler; ++p)
3378 handled = p->handler (it);
3380 if (handled == HANDLED_RECOMPUTE_PROPS)
3381 break;
3382 else if (handled == HANDLED_RETURN)
3384 /* We still want to show before and after strings from
3385 overlays even if the actual buffer text is replaced. */
3386 if (!handle_overlay_change_p
3387 || it->sp > 1
3388 /* Don't call get_overlay_strings_1 if we already
3389 have overlay strings loaded, because doing so
3390 will load them again and push the iterator state
3391 onto the stack one more time, which is not
3392 expected by the rest of the code that processes
3393 overlay strings. */
3394 || (it->current.overlay_string_index < 0
3395 && !get_overlay_strings_1 (it, 0, false)))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->string_from_display_prop_p = false;
3413 it->from_disp_prop_p = false;
3414 handle_overlay_change_p = false;
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 break;
3419 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3420 handle_overlay_change_p = false;
3423 if (handled != HANDLED_RECOMPUTE_PROPS)
3425 /* Don't check for overlay strings below when set to deliver
3426 characters from a display vector. */
3427 if (it->method == GET_FROM_DISPLAY_VECTOR)
3428 handle_overlay_change_p = false;
3430 /* Handle overlay changes.
3431 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3432 if it finds overlays. */
3433 if (handle_overlay_change_p)
3434 handled = handle_overlay_change (it);
3437 if (it->ellipsis_p)
3439 setup_for_ellipsis (it, 0);
3440 break;
3443 while (handled == HANDLED_RECOMPUTE_PROPS);
3445 /* Determine where to stop next. */
3446 if (handled == HANDLED_NORMALLY)
3447 compute_stop_pos (it);
3451 /* Compute IT->stop_charpos from text property and overlay change
3452 information for IT's current position. */
3454 static void
3455 compute_stop_pos (struct it *it)
3457 register INTERVAL iv, next_iv;
3458 Lisp_Object object, limit, position;
3459 ptrdiff_t charpos, bytepos;
3461 if (STRINGP (it->string))
3463 /* Strings are usually short, so don't limit the search for
3464 properties. */
3465 it->stop_charpos = it->end_charpos;
3466 object = it->string;
3467 limit = Qnil;
3468 charpos = IT_STRING_CHARPOS (*it);
3469 bytepos = IT_STRING_BYTEPOS (*it);
3471 else
3473 ptrdiff_t pos;
3475 /* If end_charpos is out of range for some reason, such as a
3476 misbehaving display function, rationalize it (Bug#5984). */
3477 if (it->end_charpos > ZV)
3478 it->end_charpos = ZV;
3479 it->stop_charpos = it->end_charpos;
3481 /* If next overlay change is in front of the current stop pos
3482 (which is IT->end_charpos), stop there. Note: value of
3483 next_overlay_change is point-max if no overlay change
3484 follows. */
3485 charpos = IT_CHARPOS (*it);
3486 bytepos = IT_BYTEPOS (*it);
3487 pos = next_overlay_change (charpos);
3488 if (pos < it->stop_charpos)
3489 it->stop_charpos = pos;
3491 /* Set up variables for computing the stop position from text
3492 property changes. */
3493 XSETBUFFER (object, current_buffer);
3494 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3497 /* Get the interval containing IT's position. Value is a null
3498 interval if there isn't such an interval. */
3499 position = make_number (charpos);
3500 iv = validate_interval_range (object, &position, &position, false);
3501 if (iv)
3503 Lisp_Object values_here[LAST_PROP_IDX];
3504 struct props *p;
3506 /* Get properties here. */
3507 for (p = it_props; p->handler; ++p)
3508 values_here[p->idx] = textget (iv->plist,
3509 builtin_lisp_symbol (p->name));
3511 /* Look for an interval following iv that has different
3512 properties. */
3513 for (next_iv = next_interval (iv);
3514 (next_iv
3515 && (NILP (limit)
3516 || XFASTINT (limit) > next_iv->position));
3517 next_iv = next_interval (next_iv))
3519 for (p = it_props; p->handler; ++p)
3521 Lisp_Object new_value = textget (next_iv->plist,
3522 builtin_lisp_symbol (p->name));
3523 if (!EQ (values_here[p->idx], new_value))
3524 break;
3527 if (p->handler)
3528 break;
3531 if (next_iv)
3533 if (INTEGERP (limit)
3534 && next_iv->position >= XFASTINT (limit))
3535 /* No text property change up to limit. */
3536 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3537 else
3538 /* Text properties change in next_iv. */
3539 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3543 if (it->cmp_it.id < 0)
3545 ptrdiff_t stoppos = it->end_charpos;
3547 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3548 stoppos = -1;
3549 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3550 stoppos, it->string);
3553 eassert (STRINGP (it->string)
3554 || (it->stop_charpos >= BEGV
3555 && it->stop_charpos >= IT_CHARPOS (*it)));
3559 /* Return the position of the next overlay change after POS in
3560 current_buffer. Value is point-max if no overlay change
3561 follows. This is like `next-overlay-change' but doesn't use
3562 xmalloc. */
3564 static ptrdiff_t
3565 next_overlay_change (ptrdiff_t pos)
3567 ptrdiff_t i, noverlays;
3568 ptrdiff_t endpos;
3569 Lisp_Object *overlays;
3570 USE_SAFE_ALLOCA;
3572 /* Get all overlays at the given position. */
3573 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3575 /* If any of these overlays ends before endpos,
3576 use its ending point instead. */
3577 for (i = 0; i < noverlays; ++i)
3579 Lisp_Object oend;
3580 ptrdiff_t oendpos;
3582 oend = OVERLAY_END (overlays[i]);
3583 oendpos = OVERLAY_POSITION (oend);
3584 endpos = min (endpos, oendpos);
3587 SAFE_FREE ();
3588 return endpos;
3591 /* How many characters forward to search for a display property or
3592 display string. Searching too far forward makes the bidi display
3593 sluggish, especially in small windows. */
3594 #define MAX_DISP_SCAN 250
3596 /* Return the character position of a display string at or after
3597 position specified by POSITION. If no display string exists at or
3598 after POSITION, return ZV. A display string is either an overlay
3599 with `display' property whose value is a string, or a `display'
3600 text property whose value is a string. STRING is data about the
3601 string to iterate; if STRING->lstring is nil, we are iterating a
3602 buffer. FRAME_WINDOW_P is true when we are displaying a window
3603 on a GUI frame. DISP_PROP is set to zero if we searched
3604 MAX_DISP_SCAN characters forward without finding any display
3605 strings, non-zero otherwise. It is set to 2 if the display string
3606 uses any kind of `(space ...)' spec that will produce a stretch of
3607 white space in the text area. */
3608 ptrdiff_t
3609 compute_display_string_pos (struct text_pos *position,
3610 struct bidi_string_data *string,
3611 struct window *w,
3612 bool frame_window_p, int *disp_prop)
3614 /* OBJECT = nil means current buffer. */
3615 Lisp_Object object, object1;
3616 Lisp_Object pos, spec, limpos;
3617 bool string_p = string && (STRINGP (string->lstring) || string->s);
3618 ptrdiff_t eob = string_p ? string->schars : ZV;
3619 ptrdiff_t begb = string_p ? 0 : BEGV;
3620 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3621 ptrdiff_t lim =
3622 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3623 struct text_pos tpos;
3624 int rv = 0;
3626 if (string && STRINGP (string->lstring))
3627 object1 = object = string->lstring;
3628 else if (w && !string_p)
3630 XSETWINDOW (object, w);
3631 object1 = Qnil;
3633 else
3634 object1 = object = Qnil;
3636 *disp_prop = 1;
3638 if (charpos >= eob
3639 /* We don't support display properties whose values are strings
3640 that have display string properties. */
3641 || string->from_disp_str
3642 /* C strings cannot have display properties. */
3643 || (string->s && !STRINGP (object)))
3645 *disp_prop = 0;
3646 return eob;
3649 /* If the character at CHARPOS is where the display string begins,
3650 return CHARPOS. */
3651 pos = make_number (charpos);
3652 if (STRINGP (object))
3653 bufpos = string->bufpos;
3654 else
3655 bufpos = charpos;
3656 tpos = *position;
3657 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3658 && (charpos <= begb
3659 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3660 object),
3661 spec))
3662 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3663 frame_window_p)))
3665 if (rv == 2)
3666 *disp_prop = 2;
3667 return charpos;
3670 /* Look forward for the first character with a `display' property
3671 that will replace the underlying text when displayed. */
3672 limpos = make_number (lim);
3673 do {
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3675 CHARPOS (tpos) = XFASTINT (pos);
3676 if (CHARPOS (tpos) >= lim)
3678 *disp_prop = 0;
3679 break;
3681 if (STRINGP (object))
3682 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3683 else
3684 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3685 spec = Fget_char_property (pos, Qdisplay, object);
3686 if (!STRINGP (object))
3687 bufpos = CHARPOS (tpos);
3688 } while (NILP (spec)
3689 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3690 bufpos, frame_window_p)));
3691 if (rv == 2)
3692 *disp_prop = 2;
3694 return CHARPOS (tpos);
3697 /* Return the character position of the end of the display string that
3698 started at CHARPOS. If there's no display string at CHARPOS,
3699 return -1. A display string is either an overlay with `display'
3700 property whose value is a string or a `display' text property whose
3701 value is a string. */
3702 ptrdiff_t
3703 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3705 /* OBJECT = nil means current buffer. */
3706 Lisp_Object object =
3707 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3708 Lisp_Object pos = make_number (charpos);
3709 ptrdiff_t eob =
3710 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3712 if (charpos >= eob || (string->s && !STRINGP (object)))
3713 return eob;
3715 /* It could happen that the display property or overlay was removed
3716 since we found it in compute_display_string_pos above. One way
3717 this can happen is if JIT font-lock was called (through
3718 handle_fontified_prop), and jit-lock-functions remove text
3719 properties or overlays from the portion of buffer that includes
3720 CHARPOS. Muse mode is known to do that, for example. In this
3721 case, we return -1 to the caller, to signal that no display
3722 string is actually present at CHARPOS. See bidi_fetch_char for
3723 how this is handled.
3725 An alternative would be to never look for display properties past
3726 it->stop_charpos. But neither compute_display_string_pos nor
3727 bidi_fetch_char that calls it know or care where the next
3728 stop_charpos is. */
3729 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3730 return -1;
3732 /* Look forward for the first character where the `display' property
3733 changes. */
3734 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3736 return XFASTINT (pos);
3741 /***********************************************************************
3742 Fontification
3743 ***********************************************************************/
3745 /* Handle changes in the `fontified' property of the current buffer by
3746 calling hook functions from Qfontification_functions to fontify
3747 regions of text. */
3749 static enum prop_handled
3750 handle_fontified_prop (struct it *it)
3752 Lisp_Object prop, pos;
3753 enum prop_handled handled = HANDLED_NORMALLY;
3755 if (!NILP (Vmemory_full))
3756 return handled;
3758 /* Get the value of the `fontified' property at IT's current buffer
3759 position. (The `fontified' property doesn't have a special
3760 meaning in strings.) If the value is nil, call functions from
3761 Qfontification_functions. */
3762 if (!STRINGP (it->string)
3763 && it->s == NULL
3764 && !NILP (Vfontification_functions)
3765 && !NILP (Vrun_hooks)
3766 && (pos = make_number (IT_CHARPOS (*it)),
3767 prop = Fget_char_property (pos, Qfontified, Qnil),
3768 /* Ignore the special cased nil value always present at EOB since
3769 no amount of fontifying will be able to change it. */
3770 NILP (prop) && IT_CHARPOS (*it) < Z))
3772 ptrdiff_t count = SPECPDL_INDEX ();
3773 Lisp_Object val;
3774 struct buffer *obuf = current_buffer;
3775 ptrdiff_t begv = BEGV, zv = ZV;
3776 bool old_clip_changed = current_buffer->clip_changed;
3778 val = Vfontification_functions;
3779 specbind (Qfontification_functions, Qnil);
3781 eassert (it->end_charpos == ZV);
3783 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3784 safe_call1 (val, pos);
3785 else
3787 Lisp_Object fns, fn;
3789 fns = Qnil;
3791 for (; CONSP (val); val = XCDR (val))
3793 fn = XCAR (val);
3795 if (EQ (fn, Qt))
3797 /* A value of t indicates this hook has a local
3798 binding; it means to run the global binding too.
3799 In a global value, t should not occur. If it
3800 does, we must ignore it to avoid an endless
3801 loop. */
3802 for (fns = Fdefault_value (Qfontification_functions);
3803 CONSP (fns);
3804 fns = XCDR (fns))
3806 fn = XCAR (fns);
3807 if (!EQ (fn, Qt))
3808 safe_call1 (fn, pos);
3811 else
3812 safe_call1 (fn, pos);
3816 unbind_to (count, Qnil);
3818 /* Fontification functions routinely call `save-restriction'.
3819 Normally, this tags clip_changed, which can confuse redisplay
3820 (see discussion in Bug#6671). Since we don't perform any
3821 special handling of fontification changes in the case where
3822 `save-restriction' isn't called, there's no point doing so in
3823 this case either. So, if the buffer's restrictions are
3824 actually left unchanged, reset clip_changed. */
3825 if (obuf == current_buffer)
3827 if (begv == BEGV && zv == ZV)
3828 current_buffer->clip_changed = old_clip_changed;
3830 /* There isn't much we can reasonably do to protect against
3831 misbehaving fontification, but here's a fig leaf. */
3832 else if (BUFFER_LIVE_P (obuf))
3833 set_buffer_internal_1 (obuf);
3835 /* The fontification code may have added/removed text.
3836 It could do even a lot worse, but let's at least protect against
3837 the most obvious case where only the text past `pos' gets changed',
3838 as is/was done in grep.el where some escapes sequences are turned
3839 into face properties (bug#7876). */
3840 it->end_charpos = ZV;
3842 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3843 something. This avoids an endless loop if they failed to
3844 fontify the text for which reason ever. */
3845 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3846 handled = HANDLED_RECOMPUTE_PROPS;
3849 return handled;
3854 /***********************************************************************
3855 Faces
3856 ***********************************************************************/
3858 /* Set up iterator IT from face properties at its current position.
3859 Called from handle_stop. */
3861 static enum prop_handled
3862 handle_face_prop (struct it *it)
3864 int new_face_id;
3865 ptrdiff_t next_stop;
3867 if (!STRINGP (it->string))
3869 new_face_id
3870 = face_at_buffer_position (it->w,
3871 IT_CHARPOS (*it),
3872 &next_stop,
3873 (IT_CHARPOS (*it)
3874 + TEXT_PROP_DISTANCE_LIMIT),
3875 false, it->base_face_id);
3877 /* Is this a start of a run of characters with box face?
3878 Caveat: this can be called for a freshly initialized
3879 iterator; face_id is -1 in this case. We know that the new
3880 face will not change until limit, i.e. if the new face has a
3881 box, all characters up to limit will have one. But, as
3882 usual, we don't know whether limit is really the end. */
3883 if (new_face_id != it->face_id)
3885 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3886 /* If it->face_id is -1, old_face below will be NULL, see
3887 the definition of FACE_FROM_ID_OR_NULL. This will happen
3888 if this is the initial call that gets the face. */
3889 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3891 /* If the value of face_id of the iterator is -1, we have to
3892 look in front of IT's position and see whether there is a
3893 face there that's different from new_face_id. */
3894 if (!old_face && IT_CHARPOS (*it) > BEG)
3896 int prev_face_id = face_before_it_pos (it);
3898 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3901 /* If the new face has a box, but the old face does not,
3902 this is the start of a run of characters with box face,
3903 i.e. this character has a shadow on the left side. */
3904 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3905 && (old_face == NULL || !old_face->box));
3906 it->face_box_p = new_face->box != FACE_NO_BOX;
3909 else
3911 int base_face_id;
3912 ptrdiff_t bufpos;
3913 int i;
3914 Lisp_Object from_overlay
3915 = (it->current.overlay_string_index >= 0
3916 ? it->string_overlays[it->current.overlay_string_index
3917 % OVERLAY_STRING_CHUNK_SIZE]
3918 : Qnil);
3920 /* See if we got to this string directly or indirectly from
3921 an overlay property. That includes the before-string or
3922 after-string of an overlay, strings in display properties
3923 provided by an overlay, their text properties, etc.
3925 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3926 if (! NILP (from_overlay))
3927 for (i = it->sp - 1; i >= 0; i--)
3929 if (it->stack[i].current.overlay_string_index >= 0)
3930 from_overlay
3931 = it->string_overlays[it->stack[i].current.overlay_string_index
3932 % OVERLAY_STRING_CHUNK_SIZE];
3933 else if (! NILP (it->stack[i].from_overlay))
3934 from_overlay = it->stack[i].from_overlay;
3936 if (!NILP (from_overlay))
3937 break;
3940 if (! NILP (from_overlay))
3942 bufpos = IT_CHARPOS (*it);
3943 /* For a string from an overlay, the base face depends
3944 only on text properties and ignores overlays. */
3945 base_face_id
3946 = face_for_overlay_string (it->w,
3947 IT_CHARPOS (*it),
3948 &next_stop,
3949 (IT_CHARPOS (*it)
3950 + TEXT_PROP_DISTANCE_LIMIT),
3951 false,
3952 from_overlay);
3954 else
3956 bufpos = 0;
3958 /* For strings from a `display' property, use the face at
3959 IT's current buffer position as the base face to merge
3960 with, so that overlay strings appear in the same face as
3961 surrounding text, unless they specify their own faces.
3962 For strings from wrap-prefix and line-prefix properties,
3963 use the default face, possibly remapped via
3964 Vface_remapping_alist. */
3965 /* Note that the fact that we use the face at _buffer_
3966 position means that a 'display' property on an overlay
3967 string will not inherit the face of that overlay string,
3968 but will instead revert to the face of buffer text
3969 covered by the overlay. This is visible, e.g., when the
3970 overlay specifies a box face, but neither the buffer nor
3971 the display string do. This sounds like a design bug,
3972 but Emacs always did that since v21.1, so changing that
3973 might be a big deal. */
3974 base_face_id = it->string_from_prefix_prop_p
3975 ? (!NILP (Vface_remapping_alist)
3976 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3977 : DEFAULT_FACE_ID)
3978 : underlying_face_id (it);
3981 new_face_id = face_at_string_position (it->w,
3982 it->string,
3983 IT_STRING_CHARPOS (*it),
3984 bufpos,
3985 &next_stop,
3986 base_face_id, false);
3988 /* Is this a start of a run of characters with box? Caveat:
3989 this can be called for a freshly allocated iterator; face_id
3990 is -1 is this case. We know that the new face will not
3991 change until the next check pos, i.e. if the new face has a
3992 box, all characters up to that position will have a
3993 box. But, as usual, we don't know whether that position
3994 is really the end. */
3995 if (new_face_id != it->face_id)
3997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3998 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4000 /* If new face has a box but old face hasn't, this is the
4001 start of a run of characters with box, i.e. it has a
4002 shadow on the left side. */
4003 it->start_of_box_run_p
4004 = new_face->box && (old_face == NULL || !old_face->box);
4005 it->face_box_p = new_face->box != FACE_NO_BOX;
4009 it->face_id = new_face_id;
4010 return HANDLED_NORMALLY;
4014 /* Return the ID of the face ``underlying'' IT's current position,
4015 which is in a string. If the iterator is associated with a
4016 buffer, return the face at IT's current buffer position.
4017 Otherwise, use the iterator's base_face_id. */
4019 static int
4020 underlying_face_id (struct it *it)
4022 int face_id = it->base_face_id, i;
4024 eassert (STRINGP (it->string));
4026 for (i = it->sp - 1; i >= 0; --i)
4027 if (NILP (it->stack[i].string))
4028 face_id = it->stack[i].face_id;
4030 return face_id;
4034 /* Compute the face one character before or after the current position
4035 of IT, in the visual order. BEFORE_P means get the face
4036 in front (to the left in L2R paragraphs, to the right in R2L
4037 paragraphs) of IT's screen position. Value is the ID of the face. */
4039 static int
4040 face_before_or_after_it_pos (struct it *it, bool before_p)
4042 int face_id, limit;
4043 ptrdiff_t next_check_charpos;
4044 struct it it_copy;
4045 void *it_copy_data = NULL;
4047 eassert (it->s == NULL);
4049 if (STRINGP (it->string))
4051 ptrdiff_t bufpos, charpos;
4052 int base_face_id;
4054 /* No face change past the end of the string (for the case
4055 we are padding with spaces). No face change before the
4056 string start. */
4057 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4058 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4059 return it->face_id;
4061 if (!it->bidi_p)
4063 /* Set charpos to the position before or after IT's current
4064 position, in the logical order, which in the non-bidi
4065 case is the same as the visual order. */
4066 if (before_p)
4067 charpos = IT_STRING_CHARPOS (*it) - 1;
4068 else if (it->what == IT_COMPOSITION)
4069 /* For composition, we must check the character after the
4070 composition. */
4071 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4072 else
4073 charpos = IT_STRING_CHARPOS (*it) + 1;
4075 else
4077 if (before_p)
4079 /* With bidi iteration, the character before the current
4080 in the visual order cannot be found by simple
4081 iteration, because "reverse" reordering is not
4082 supported. Instead, we need to start from the string
4083 beginning and go all the way to the current string
4084 position, remembering the previous position. */
4085 /* Ignore face changes before the first visible
4086 character on this display line. */
4087 if (it->current_x <= it->first_visible_x)
4088 return it->face_id;
4089 SAVE_IT (it_copy, *it, it_copy_data);
4090 IT_STRING_CHARPOS (it_copy) = 0;
4091 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4095 charpos = IT_STRING_CHARPOS (it_copy);
4096 if (charpos >= SCHARS (it->string))
4097 break;
4098 bidi_move_to_visually_next (&it_copy.bidi_it);
4100 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 int current_x;
4183 /* With bidi iteration, the character before the current
4184 in the visual order cannot be found by simple
4185 iteration, because "reverse" reordering is not
4186 supported. Instead, we need to use the move_it_*
4187 family of functions, and move to the previous
4188 character starting from the beginning of the visual
4189 line. */
4190 /* Ignore face changes before the first visible
4191 character on this display line. */
4192 if (it->current_x <= it->first_visible_x)
4193 return it->face_id;
4194 SAVE_IT (it_copy, *it, it_copy_data);
4195 /* Implementation note: Since move_it_in_display_line
4196 works in the iterator geometry, and thinks the first
4197 character is always the leftmost, even in R2L lines,
4198 we don't need to distinguish between the R2L and L2R
4199 cases here. */
4200 current_x = it_copy.current_x;
4201 move_it_vertically_backward (&it_copy, 0);
4202 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4203 pos = it_copy.current.pos;
4204 RESTORE_IT (it, it, it_copy_data);
4206 else
4208 /* Set charpos to the buffer position of the character
4209 that comes after IT's current position in the visual
4210 order. */
4211 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4213 it_copy = *it;
4214 while (n--)
4215 bidi_move_to_visually_next (&it_copy.bidi_it);
4217 SET_TEXT_POS (pos,
4218 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4221 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4223 /* Determine face for CHARSET_ASCII, or unibyte. */
4224 face_id = face_at_buffer_position (it->w,
4225 CHARPOS (pos),
4226 &next_check_charpos,
4227 limit, false, -1);
4229 /* Correct the face for charsets different from ASCII. Do it
4230 for the multibyte case only. The face returned above is
4231 suitable for unibyte text if current_buffer is unibyte. */
4232 if (it->multibyte_p)
4234 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4235 struct face *face = FACE_FROM_ID (it->f, face_id);
4236 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4240 return face_id;
4245 /***********************************************************************
4246 Invisible text
4247 ***********************************************************************/
4249 /* Set up iterator IT from invisible properties at its current
4250 position. Called from handle_stop. */
4252 static enum prop_handled
4253 handle_invisible_prop (struct it *it)
4255 enum prop_handled handled = HANDLED_NORMALLY;
4256 int invis;
4257 Lisp_Object prop;
4259 if (STRINGP (it->string))
4261 Lisp_Object end_charpos, limit;
4263 /* Get the value of the invisible text property at the
4264 current position. Value will be nil if there is no such
4265 property. */
4266 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4267 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4268 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4272 /* Record whether we have to display an ellipsis for the
4273 invisible text. */
4274 bool display_ellipsis_p = (invis == 2);
4275 ptrdiff_t len, endpos;
4277 handled = HANDLED_RECOMPUTE_PROPS;
4279 /* Get the position at which the next visible text can be
4280 found in IT->string, if any. */
4281 endpos = len = SCHARS (it->string);
4282 XSETINT (limit, len);
4285 end_charpos
4286 = Fnext_single_property_change (end_charpos, Qinvisible,
4287 it->string, limit);
4288 /* Since LIMIT is always an integer, so should be the
4289 value returned by Fnext_single_property_change. */
4290 eassert (INTEGERP (end_charpos));
4291 if (INTEGERP (end_charpos))
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis == 2)
4297 display_ellipsis_p = true;
4299 else /* Should never happen; but if it does, exit the loop. */
4300 endpos = len;
4302 while (invis != 0 && endpos < len);
4304 if (display_ellipsis_p)
4305 it->ellipsis_p = true;
4307 if (endpos < len)
4309 /* Text at END_CHARPOS is visible. Move IT there. */
4310 struct text_pos old;
4311 ptrdiff_t oldpos;
4313 old = it->current.string_pos;
4314 oldpos = CHARPOS (old);
4315 if (it->bidi_p)
4317 if (it->bidi_it.first_elt
4318 && it->bidi_it.charpos < SCHARS (it->string))
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, true);
4321 /* Bidi-iterate out of the invisible text. */
4324 bidi_move_to_visually_next (&it->bidi_it);
4326 while (oldpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < endpos);
4329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4331 if (IT_CHARPOS (*it) >= endpos)
4332 it->prev_stop = endpos;
4334 else
4336 IT_STRING_CHARPOS (*it) = endpos;
4337 compute_string_pos (&it->current.string_pos, old, it->string);
4340 else
4342 /* The rest of the string is invisible. If this is an
4343 overlay string, proceed with the next overlay string
4344 or whatever comes and return a character from there. */
4345 if (it->current.overlay_string_index >= 0
4346 && !display_ellipsis_p)
4348 next_overlay_string (it);
4349 /* Don't check for overlay strings when we just
4350 finished processing them. */
4351 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4353 else
4355 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4356 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4361 else
4363 ptrdiff_t newpos, next_stop, start_charpos, tem;
4364 Lisp_Object pos, overlay;
4366 /* First of all, is there invisible text at this position? */
4367 tem = start_charpos = IT_CHARPOS (*it);
4368 pos = make_number (tem);
4369 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4370 &overlay);
4371 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4373 /* If we are on invisible text, skip over it. */
4374 if (invis != 0 && start_charpos < it->end_charpos)
4376 /* Record whether we have to display an ellipsis for the
4377 invisible text. */
4378 bool display_ellipsis_p = invis == 2;
4380 handled = HANDLED_RECOMPUTE_PROPS;
4382 /* Loop skipping over invisible text. The loop is left at
4383 ZV or with IT on the first char being visible again. */
4386 /* Try to skip some invisible text. Return value is the
4387 position reached which can be equal to where we start
4388 if there is nothing invisible there. This skips both
4389 over invisible text properties and overlays with
4390 invisible property. */
4391 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4393 /* If we skipped nothing at all we weren't at invisible
4394 text in the first place. If everything to the end of
4395 the buffer was skipped, end the loop. */
4396 if (newpos == tem || newpos >= ZV)
4397 invis = 0;
4398 else
4400 /* We skipped some characters but not necessarily
4401 all there are. Check if we ended up on visible
4402 text. Fget_char_property returns the property of
4403 the char before the given position, i.e. if we
4404 get invis = 0, this means that the char at
4405 newpos is visible. */
4406 pos = make_number (newpos);
4407 prop = Fget_char_property (pos, Qinvisible, it->window);
4408 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4411 /* If we ended up on invisible text, proceed to
4412 skip starting with next_stop. */
4413 if (invis != 0)
4414 tem = next_stop;
4416 /* If there are adjacent invisible texts, don't lose the
4417 second one's ellipsis. */
4418 if (invis == 2)
4419 display_ellipsis_p = true;
4421 while (invis != 0);
4423 /* The position newpos is now either ZV or on visible text. */
4424 if (it->bidi_p)
4426 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4427 bool on_newline
4428 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4429 bool after_newline
4430 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4432 /* If the invisible text ends on a newline or on a
4433 character after a newline, we can avoid the costly,
4434 character by character, bidi iteration to NEWPOS, and
4435 instead simply reseat the iterator there. That's
4436 because all bidi reordering information is tossed at
4437 the newline. This is a big win for modes that hide
4438 complete lines, like Outline, Org, etc. */
4439 if (on_newline || after_newline)
4441 struct text_pos tpos;
4442 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4444 SET_TEXT_POS (tpos, newpos, bpos);
4445 reseat_1 (it, tpos, false);
4446 /* If we reseat on a newline/ZV, we need to prep the
4447 bidi iterator for advancing to the next character
4448 after the newline/EOB, keeping the current paragraph
4449 direction (so that PRODUCE_GLYPHS does TRT wrt
4450 prepending/appending glyphs to a glyph row). */
4451 if (on_newline)
4453 it->bidi_it.first_elt = false;
4454 it->bidi_it.paragraph_dir = pdir;
4455 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4456 it->bidi_it.nchars = 1;
4457 it->bidi_it.ch_len = 1;
4460 else /* Must use the slow method. */
4462 /* With bidi iteration, the region of invisible text
4463 could start and/or end in the middle of a
4464 non-base embedding level. Therefore, we need to
4465 skip invisible text using the bidi iterator,
4466 starting at IT's current position, until we find
4467 ourselves outside of the invisible text.
4468 Skipping invisible text _after_ bidi iteration
4469 avoids affecting the visual order of the
4470 displayed text when invisible properties are
4471 added or removed. */
4472 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4474 /* If we were `reseat'ed to a new paragraph,
4475 determine the paragraph base direction. We
4476 need to do it now because
4477 next_element_from_buffer may not have a
4478 chance to do it, if we are going to skip any
4479 text at the beginning, which resets the
4480 FIRST_ELT flag. */
4481 bidi_paragraph_init (it->paragraph_embedding,
4482 &it->bidi_it, true);
4486 bidi_move_to_visually_next (&it->bidi_it);
4488 while (it->stop_charpos <= it->bidi_it.charpos
4489 && it->bidi_it.charpos < newpos);
4490 IT_CHARPOS (*it) = it->bidi_it.charpos;
4491 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4492 /* If we overstepped NEWPOS, record its position in
4493 the iterator, so that we skip invisible text if
4494 later the bidi iteration lands us in the
4495 invisible region again. */
4496 if (IT_CHARPOS (*it) >= newpos)
4497 it->prev_stop = newpos;
4500 else
4502 IT_CHARPOS (*it) = newpos;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4506 if (display_ellipsis_p)
4508 /* Make sure that the glyphs of the ellipsis will get
4509 correct `charpos' values. If we would not update
4510 it->position here, the glyphs would belong to the
4511 last visible character _before_ the invisible
4512 text, which confuses `set_cursor_from_row'.
4514 We use the last invisible position instead of the
4515 first because this way the cursor is always drawn on
4516 the first "." of the ellipsis, whenever PT is inside
4517 the invisible text. Otherwise the cursor would be
4518 placed _after_ the ellipsis when the point is after the
4519 first invisible character. */
4520 if (!STRINGP (it->object))
4522 it->position.charpos = newpos - 1;
4523 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4527 /* If there are before-strings at the start of invisible
4528 text, and the text is invisible because of a text
4529 property, arrange to show before-strings because 20.x did
4530 it that way. (If the text is invisible because of an
4531 overlay property instead of a text property, this is
4532 already handled in the overlay code.) */
4533 if (NILP (overlay)
4534 && get_overlay_strings (it, it->stop_charpos))
4536 handled = HANDLED_RECOMPUTE_PROPS;
4537 if (it->sp > 0)
4539 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4540 /* The call to get_overlay_strings above recomputes
4541 it->stop_charpos, but it only considers changes
4542 in properties and overlays beyond iterator's
4543 current position. This causes us to miss changes
4544 that happen exactly where the invisible property
4545 ended. So we play it safe here and force the
4546 iterator to check for potential stop positions
4547 immediately after the invisible text. Note that
4548 if get_overlay_strings returns true, it
4549 normally also pushed the iterator stack, so we
4550 need to update the stop position in the slot
4551 below the current one. */
4552 it->stack[it->sp - 1].stop_charpos
4553 = CHARPOS (it->stack[it->sp - 1].current.pos);
4556 else if (display_ellipsis_p)
4558 it->ellipsis_p = true;
4559 /* Let the ellipsis display before
4560 considering any properties of the following char.
4561 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4562 handled = HANDLED_RETURN;
4567 return handled;
4571 /* Make iterator IT return `...' next.
4572 Replaces LEN characters from buffer. */
4574 static void
4575 setup_for_ellipsis (struct it *it, int len)
4577 /* Use the display table definition for `...'. Invalid glyphs
4578 will be handled by the method returning elements from dpvec. */
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4581 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4582 it->dpvec = v->contents;
4583 it->dpend = v->contents + v->header.size;
4585 else
4587 /* Default `...'. */
4588 it->dpvec = default_invis_vector;
4589 it->dpend = default_invis_vector + 3;
4592 it->dpvec_char_len = len;
4593 it->current.dpvec_index = 0;
4594 it->dpvec_face_id = -1;
4596 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4597 face as the preceding text. IT->saved_face_id was set in
4598 handle_stop to the face of the preceding character, and will be
4599 different from IT->face_id only if the invisible text skipped in
4600 handle_invisible_prop has some non-default face on its first
4601 character. We thus ignore the face of the invisible text when we
4602 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4603 if (it->saved_face_id >= 0)
4604 it->face_id = it->saved_face_id;
4606 /* If the ellipsis represents buffer text, it means we advanced in
4607 the buffer, so we should no longer ignore overlay strings. */
4608 if (it->method == GET_FROM_BUFFER)
4609 it->ignore_overlay_strings_at_pos_p = false;
4611 it->method = GET_FROM_DISPLAY_VECTOR;
4612 it->ellipsis_p = true;
4617 /***********************************************************************
4618 'display' property
4619 ***********************************************************************/
4621 /* Set up iterator IT from `display' property at its current position.
4622 Called from handle_stop.
4623 We return HANDLED_RETURN if some part of the display property
4624 overrides the display of the buffer text itself.
4625 Otherwise we return HANDLED_NORMALLY. */
4627 static enum prop_handled
4628 handle_display_prop (struct it *it)
4630 Lisp_Object propval, object, overlay;
4631 struct text_pos *position;
4632 ptrdiff_t bufpos;
4633 /* Nonzero if some property replaces the display of the text itself. */
4634 int display_replaced = 0;
4636 if (STRINGP (it->string))
4638 object = it->string;
4639 position = &it->current.string_pos;
4640 bufpos = CHARPOS (it->current.pos);
4642 else
4644 XSETWINDOW (object, it->w);
4645 position = &it->current.pos;
4646 bufpos = CHARPOS (*position);
4649 /* Reset those iterator values set from display property values. */
4650 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4651 it->space_width = Qnil;
4652 it->font_height = Qnil;
4653 it->voffset = 0;
4655 /* We don't support recursive `display' properties, i.e. string
4656 values that have a string `display' property, that have a string
4657 `display' property etc. */
4658 if (!it->string_from_display_prop_p)
4659 it->area = TEXT_AREA;
4661 propval = get_char_property_and_overlay (make_number (position->charpos),
4662 Qdisplay, object, &overlay);
4663 if (NILP (propval))
4664 return HANDLED_NORMALLY;
4665 /* Now OVERLAY is the overlay that gave us this property, or nil
4666 if it was a text property. */
4668 if (!STRINGP (it->string))
4669 object = it->w->contents;
4671 display_replaced = handle_display_spec (it, propval, object, overlay,
4672 position, bufpos,
4673 FRAME_WINDOW_P (it->f));
4674 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4677 /* Subroutine of handle_display_prop. Returns non-zero if the display
4678 specification in SPEC is a replacing specification, i.e. it would
4679 replace the text covered by `display' property with something else,
4680 such as an image or a display string. If SPEC includes any kind or
4681 `(space ...) specification, the value is 2; this is used by
4682 compute_display_string_pos, which see.
4684 See handle_single_display_spec for documentation of arguments.
4685 FRAME_WINDOW_P is true if the window being redisplayed is on a
4686 GUI frame; this argument is used only if IT is NULL, see below.
4688 IT can be NULL, if this is called by the bidi reordering code
4689 through compute_display_string_pos, which see. In that case, this
4690 function only examines SPEC, but does not otherwise "handle" it, in
4691 the sense that it doesn't set up members of IT from the display
4692 spec. */
4693 static int
4694 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4695 Lisp_Object overlay, struct text_pos *position,
4696 ptrdiff_t bufpos, bool frame_window_p)
4698 int replacing = 0;
4700 if (CONSP (spec)
4701 /* Simple specifications. */
4702 && !EQ (XCAR (spec), Qimage)
4703 #ifdef HAVE_XWIDGETS
4704 && !EQ (XCAR (spec), Qxwidget)
4705 #endif
4706 && !EQ (XCAR (spec), Qspace)
4707 && !EQ (XCAR (spec), Qwhen)
4708 && !EQ (XCAR (spec), Qslice)
4709 && !EQ (XCAR (spec), Qspace_width)
4710 && !EQ (XCAR (spec), Qheight)
4711 && !EQ (XCAR (spec), Qraise)
4712 /* Marginal area specifications. */
4713 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4714 && !EQ (XCAR (spec), Qleft_fringe)
4715 && !EQ (XCAR (spec), Qright_fringe)
4716 && !NILP (XCAR (spec)))
4718 for (; CONSP (spec); spec = XCDR (spec))
4720 int rv = handle_single_display_spec (it, XCAR (spec), object,
4721 overlay, position, bufpos,
4722 replacing, frame_window_p);
4723 if (rv != 0)
4725 replacing = rv;
4726 /* If some text in a string is replaced, `position' no
4727 longer points to the position of `object'. */
4728 if (!it || STRINGP (object))
4729 break;
4733 else if (VECTORP (spec))
4735 ptrdiff_t i;
4736 for (i = 0; i < ASIZE (spec); ++i)
4738 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4739 overlay, position, bufpos,
4740 replacing, frame_window_p);
4741 if (rv != 0)
4743 replacing = rv;
4744 /* If some text in a string is replaced, `position' no
4745 longer points to the position of `object'. */
4746 if (!it || STRINGP (object))
4747 break;
4751 else
4752 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4753 bufpos, 0, frame_window_p);
4754 return replacing;
4757 /* Value is the position of the end of the `display' property starting
4758 at START_POS in OBJECT. */
4760 static struct text_pos
4761 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4763 Lisp_Object end;
4764 struct text_pos end_pos;
4766 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4767 Qdisplay, object, Qnil);
4768 CHARPOS (end_pos) = XFASTINT (end);
4769 if (STRINGP (object))
4770 compute_string_pos (&end_pos, start_pos, it->string);
4771 else
4772 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4774 return end_pos;
4778 /* Set up IT from a single `display' property specification SPEC. OBJECT
4779 is the object in which the `display' property was found. *POSITION
4780 is the position in OBJECT at which the `display' property was found.
4781 BUFPOS is the buffer position of OBJECT (different from POSITION if
4782 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4783 previously saw a display specification which already replaced text
4784 display with something else, for example an image; we ignore such
4785 properties after the first one has been processed.
4787 OVERLAY is the overlay this `display' property came from,
4788 or nil if it was a text property.
4790 If SPEC is a `space' or `image' specification, and in some other
4791 cases too, set *POSITION to the position where the `display'
4792 property ends.
4794 If IT is NULL, only examine the property specification in SPEC, but
4795 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4796 is intended to be displayed in a window on a GUI frame.
4798 Value is non-zero if something was found which replaces the display
4799 of buffer or string text. */
4801 static int
4802 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4803 Lisp_Object overlay, struct text_pos *position,
4804 ptrdiff_t bufpos, int display_replaced,
4805 bool frame_window_p)
4807 Lisp_Object form;
4808 Lisp_Object location, value;
4809 struct text_pos start_pos = *position;
4811 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4812 If the result is non-nil, use VALUE instead of SPEC. */
4813 form = Qt;
4814 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4816 spec = XCDR (spec);
4817 if (!CONSP (spec))
4818 return 0;
4819 form = XCAR (spec);
4820 spec = XCDR (spec);
4823 if (!NILP (form) && !EQ (form, Qt))
4825 ptrdiff_t count = SPECPDL_INDEX ();
4827 /* Bind `object' to the object having the `display' property, a
4828 buffer or string. Bind `position' to the position in the
4829 object where the property was found, and `buffer-position'
4830 to the current position in the buffer. */
4832 if (NILP (object))
4833 XSETBUFFER (object, current_buffer);
4834 specbind (Qobject, object);
4835 specbind (Qposition, make_number (CHARPOS (*position)));
4836 specbind (Qbuffer_position, make_number (bufpos));
4837 form = safe_eval (form);
4838 unbind_to (count, Qnil);
4841 if (NILP (form))
4842 return 0;
4844 /* Handle `(height HEIGHT)' specifications. */
4845 if (CONSP (spec)
4846 && EQ (XCAR (spec), Qheight)
4847 && CONSP (XCDR (spec)))
4849 if (it)
4851 if (!FRAME_WINDOW_P (it->f))
4852 return 0;
4854 it->font_height = XCAR (XCDR (spec));
4855 if (!NILP (it->font_height))
4857 int new_height = -1;
4859 if (CONSP (it->font_height)
4860 && (EQ (XCAR (it->font_height), Qplus)
4861 || EQ (XCAR (it->font_height), Qminus))
4862 && CONSP (XCDR (it->font_height))
4863 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4865 /* `(+ N)' or `(- N)' where N is an integer. */
4866 int steps = XINT (XCAR (XCDR (it->font_height)));
4867 if (EQ (XCAR (it->font_height), Qplus))
4868 steps = - steps;
4869 it->face_id = smaller_face (it->f, it->face_id, steps);
4871 else if (FUNCTIONP (it->font_height))
4873 /* Call function with current height as argument.
4874 Value is the new height. */
4875 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4876 Lisp_Object height;
4877 height = safe_call1 (it->font_height,
4878 face->lface[LFACE_HEIGHT_INDEX]);
4879 if (NUMBERP (height))
4880 new_height = XFLOATINT (height);
4882 else if (NUMBERP (it->font_height))
4884 /* Value is a multiple of the canonical char height. */
4885 struct face *f;
4887 f = FACE_FROM_ID (it->f,
4888 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4889 new_height = (XFLOATINT (it->font_height)
4890 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4892 else
4894 /* Evaluate IT->font_height with `height' bound to the
4895 current specified height to get the new height. */
4896 ptrdiff_t count = SPECPDL_INDEX ();
4897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4899 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4900 value = safe_eval (it->font_height);
4901 unbind_to (count, Qnil);
4903 if (NUMBERP (value))
4904 new_height = XFLOATINT (value);
4907 if (new_height > 0)
4908 it->face_id = face_with_height (it->f, it->face_id, new_height);
4912 return 0;
4915 /* Handle `(space-width WIDTH)'. */
4916 if (CONSP (spec)
4917 && EQ (XCAR (spec), Qspace_width)
4918 && CONSP (XCDR (spec)))
4920 if (it)
4922 if (!FRAME_WINDOW_P (it->f))
4923 return 0;
4925 value = XCAR (XCDR (spec));
4926 if (NUMBERP (value) && XFLOATINT (value) > 0)
4927 it->space_width = value;
4930 return 0;
4933 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4934 if (CONSP (spec)
4935 && EQ (XCAR (spec), Qslice))
4937 Lisp_Object tem;
4939 if (it)
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4944 if (tem = XCDR (spec), CONSP (tem))
4946 it->slice.x = XCAR (tem);
4947 if (tem = XCDR (tem), CONSP (tem))
4949 it->slice.y = XCAR (tem);
4950 if (tem = XCDR (tem), CONSP (tem))
4952 it->slice.width = XCAR (tem);
4953 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.height = XCAR (tem);
4960 return 0;
4963 /* Handle `(raise FACTOR)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qraise)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 #ifdef HAVE_WINDOW_SYSTEM
4974 value = XCAR (XCDR (spec));
4975 if (NUMBERP (value))
4977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4978 it->voffset = - (XFLOATINT (value)
4979 * (normal_char_height (face->font, -1)));
4981 #endif /* HAVE_WINDOW_SYSTEM */
4984 return 0;
4987 /* Don't handle the other kinds of display specifications
4988 inside a string that we got from a `display' property. */
4989 if (it && it->string_from_display_prop_p)
4990 return 0;
4992 /* Characters having this form of property are not displayed, so
4993 we have to find the end of the property. */
4994 if (it)
4996 start_pos = *position;
4997 *position = display_prop_end (it, object, start_pos);
4998 /* If the display property comes from an overlay, don't consider
4999 any potential stop_charpos values before the end of that
5000 overlay. Since display_prop_end will happily find another
5001 'display' property coming from some other overlay or text
5002 property on buffer positions before this overlay's end, we
5003 need to ignore them, or else we risk displaying this
5004 overlay's display string/image twice. */
5005 if (!NILP (overlay))
5007 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5009 if (ovendpos > CHARPOS (*position))
5010 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5013 value = Qnil;
5015 /* Stop the scan at that end position--we assume that all
5016 text properties change there. */
5017 if (it)
5018 it->stop_charpos = position->charpos;
5020 /* Handle `(left-fringe BITMAP [FACE])'
5021 and `(right-fringe BITMAP [FACE])'. */
5022 if (CONSP (spec)
5023 && (EQ (XCAR (spec), Qleft_fringe)
5024 || EQ (XCAR (spec), Qright_fringe))
5025 && CONSP (XCDR (spec)))
5027 if (it)
5029 if (!FRAME_WINDOW_P (it->f))
5030 /* If we return here, POSITION has been advanced
5031 across the text with this property. */
5033 /* Synchronize the bidi iterator with POSITION. This is
5034 needed because we are not going to push the iterator
5035 on behalf of this display property, so there will be
5036 no pop_it call to do this synchronization for us. */
5037 if (it->bidi_p)
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5043 return 1;
5046 else if (!frame_window_p)
5047 return 1;
5049 #ifdef HAVE_WINDOW_SYSTEM
5050 value = XCAR (XCDR (spec));
5051 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5052 if (! fringe_bitmap)
5053 /* If we return here, POSITION has been advanced
5054 across the text with this property. */
5056 if (it && it->bidi_p)
5058 it->position = *position;
5059 iterate_out_of_display_property (it);
5060 *position = it->position;
5062 return 1;
5065 if (it)
5067 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5069 if (CONSP (XCDR (XCDR (spec))))
5071 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5072 int face_id2 = lookup_derived_face (it->f, face_name,
5073 FRINGE_FACE_ID, false);
5074 if (face_id2 >= 0)
5075 face_id = face_id2;
5078 /* Save current settings of IT so that we can restore them
5079 when we are finished with the glyph property value. */
5080 push_it (it, position);
5082 it->area = TEXT_AREA;
5083 it->what = IT_IMAGE;
5084 it->image_id = -1; /* no image */
5085 it->position = start_pos;
5086 it->object = NILP (object) ? it->w->contents : object;
5087 it->method = GET_FROM_IMAGE;
5088 it->from_overlay = Qnil;
5089 it->face_id = face_id;
5090 it->from_disp_prop_p = true;
5092 /* Say that we haven't consumed the characters with
5093 `display' property yet. The call to pop_it in
5094 set_iterator_to_next will clean this up. */
5095 *position = start_pos;
5097 if (EQ (XCAR (spec), Qleft_fringe))
5099 it->left_user_fringe_bitmap = fringe_bitmap;
5100 it->left_user_fringe_face_id = face_id;
5102 else
5104 it->right_user_fringe_bitmap = fringe_bitmap;
5105 it->right_user_fringe_face_id = face_id;
5108 #endif /* HAVE_WINDOW_SYSTEM */
5109 return 1;
5112 /* Prepare to handle `((margin left-margin) ...)',
5113 `((margin right-margin) ...)' and `((margin nil) ...)'
5114 prefixes for display specifications. */
5115 location = Qunbound;
5116 if (CONSP (spec) && CONSP (XCAR (spec)))
5118 Lisp_Object tem;
5120 value = XCDR (spec);
5121 if (CONSP (value))
5122 value = XCAR (value);
5124 tem = XCAR (spec);
5125 if (EQ (XCAR (tem), Qmargin)
5126 && (tem = XCDR (tem),
5127 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5128 (NILP (tem)
5129 || EQ (tem, Qleft_margin)
5130 || EQ (tem, Qright_margin))))
5131 location = tem;
5134 if (EQ (location, Qunbound))
5136 location = Qnil;
5137 value = spec;
5140 /* After this point, VALUE is the property after any
5141 margin prefix has been stripped. It must be a string,
5142 an image specification, or `(space ...)'.
5144 LOCATION specifies where to display: `left-margin',
5145 `right-margin' or nil. */
5147 bool valid_p = (STRINGP (value)
5148 #ifdef HAVE_WINDOW_SYSTEM
5149 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5150 && valid_image_p (value))
5151 #endif /* not HAVE_WINDOW_SYSTEM */
5152 || (CONSP (value) && EQ (XCAR (value), Qspace))
5153 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5154 && valid_xwidget_spec_p (value)));
5156 if (valid_p && display_replaced == 0)
5158 int retval = 1;
5160 if (!it)
5162 /* Callers need to know whether the display spec is any kind
5163 of `(space ...)' spec that is about to affect text-area
5164 display. */
5165 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5166 retval = 2;
5167 return retval;
5170 /* Save current settings of IT so that we can restore them
5171 when we are finished with the glyph property value. */
5172 push_it (it, position);
5173 it->from_overlay = overlay;
5174 it->from_disp_prop_p = true;
5176 if (NILP (location))
5177 it->area = TEXT_AREA;
5178 else if (EQ (location, Qleft_margin))
5179 it->area = LEFT_MARGIN_AREA;
5180 else
5181 it->area = RIGHT_MARGIN_AREA;
5183 if (STRINGP (value))
5185 it->string = value;
5186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5187 it->current.overlay_string_index = -1;
5188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5189 it->end_charpos = it->string_nchars = SCHARS (it->string);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 it->prev_stop = 0;
5193 it->base_level_stop = 0;
5194 it->string_from_display_prop_p = true;
5195 /* Say that we haven't consumed the characters with
5196 `display' property yet. The call to pop_it in
5197 set_iterator_to_next will clean this up. */
5198 if (BUFFERP (object))
5199 *position = start_pos;
5201 /* Force paragraph direction to be that of the parent
5202 object. If the parent object's paragraph direction is
5203 not yet determined, default to L2R. */
5204 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5205 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5206 else
5207 it->paragraph_embedding = L2R;
5209 /* Set up the bidi iterator for this display string. */
5210 if (it->bidi_p)
5212 it->bidi_it.string.lstring = it->string;
5213 it->bidi_it.string.s = NULL;
5214 it->bidi_it.string.schars = it->end_charpos;
5215 it->bidi_it.string.bufpos = bufpos;
5216 it->bidi_it.string.from_disp_str = true;
5217 it->bidi_it.string.unibyte = !it->multibyte_p;
5218 it->bidi_it.w = it->w;
5219 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5222 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5224 it->method = GET_FROM_STRETCH;
5225 it->object = value;
5226 *position = it->position = start_pos;
5227 retval = 1 + (it->area == TEXT_AREA);
5229 else if (valid_xwidget_spec_p (value))
5231 it->what = IT_XWIDGET;
5232 it->method = GET_FROM_XWIDGET;
5233 it->position = start_pos;
5234 it->object = NILP (object) ? it->w->contents : object;
5235 *position = start_pos;
5236 it->xwidget = lookup_xwidget (value);
5238 #ifdef HAVE_WINDOW_SYSTEM
5239 else
5241 it->what = IT_IMAGE;
5242 it->image_id = lookup_image (it->f, value);
5243 it->position = start_pos;
5244 it->object = NILP (object) ? it->w->contents : object;
5245 it->method = GET_FROM_IMAGE;
5247 /* Say that we haven't consumed the characters with
5248 `display' property yet. The call to pop_it in
5249 set_iterator_to_next will clean this up. */
5250 *position = start_pos;
5252 #endif /* HAVE_WINDOW_SYSTEM */
5254 return retval;
5257 /* Invalid property or property not supported. Restore
5258 POSITION to what it was before. */
5259 *position = start_pos;
5260 return 0;
5263 /* Check if PROP is a display property value whose text should be
5264 treated as intangible. OVERLAY is the overlay from which PROP
5265 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5266 specify the buffer position covered by PROP. */
5268 bool
5269 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5270 ptrdiff_t charpos, ptrdiff_t bytepos)
5272 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5273 struct text_pos position;
5275 SET_TEXT_POS (position, charpos, bytepos);
5276 return (handle_display_spec (NULL, prop, Qnil, overlay,
5277 &position, charpos, frame_window_p)
5278 != 0);
5282 /* Return true if PROP is a display sub-property value containing STRING.
5284 Implementation note: this and the following function are really
5285 special cases of handle_display_spec and
5286 handle_single_display_spec, and should ideally use the same code.
5287 Until they do, these two pairs must be consistent and must be
5288 modified in sync. */
5290 static bool
5291 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5293 if (EQ (string, prop))
5294 return true;
5296 /* Skip over `when FORM'. */
5297 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5299 prop = XCDR (prop);
5300 if (!CONSP (prop))
5301 return false;
5302 /* Actually, the condition following `when' should be eval'ed,
5303 like handle_single_display_spec does, and we should return
5304 false if it evaluates to nil. However, this function is
5305 called only when the buffer was already displayed and some
5306 glyph in the glyph matrix was found to come from a display
5307 string. Therefore, the condition was already evaluated, and
5308 the result was non-nil, otherwise the display string wouldn't
5309 have been displayed and we would have never been called for
5310 this property. Thus, we can skip the evaluation and assume
5311 its result is non-nil. */
5312 prop = XCDR (prop);
5315 if (CONSP (prop))
5316 /* Skip over `margin LOCATION'. */
5317 if (EQ (XCAR (prop), Qmargin))
5319 prop = XCDR (prop);
5320 if (!CONSP (prop))
5321 return false;
5323 prop = XCDR (prop);
5324 if (!CONSP (prop))
5325 return false;
5328 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5332 /* Return true if STRING appears in the `display' property PROP. */
5334 static bool
5335 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5337 if (CONSP (prop)
5338 && !EQ (XCAR (prop), Qwhen)
5339 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5341 /* A list of sub-properties. */
5342 while (CONSP (prop))
5344 if (single_display_spec_string_p (XCAR (prop), string))
5345 return true;
5346 prop = XCDR (prop);
5349 else if (VECTORP (prop))
5351 /* A vector of sub-properties. */
5352 ptrdiff_t i;
5353 for (i = 0; i < ASIZE (prop); ++i)
5354 if (single_display_spec_string_p (AREF (prop, i), string))
5355 return true;
5357 else
5358 return single_display_spec_string_p (prop, string);
5360 return false;
5363 /* Look for STRING in overlays and text properties in the current
5364 buffer, between character positions FROM and TO (excluding TO).
5365 BACK_P means look back (in this case, TO is supposed to be
5366 less than FROM).
5367 Value is the first character position where STRING was found, or
5368 zero if it wasn't found before hitting TO.
5370 This function may only use code that doesn't eval because it is
5371 called asynchronously from note_mouse_highlight. */
5373 static ptrdiff_t
5374 string_buffer_position_lim (Lisp_Object string,
5375 ptrdiff_t from, ptrdiff_t to, bool back_p)
5377 Lisp_Object limit, prop, pos;
5378 bool found = false;
5380 pos = make_number (max (from, BEGV));
5382 if (!back_p) /* looking forward */
5384 limit = make_number (min (to, ZV));
5385 while (!found && !EQ (pos, limit))
5387 prop = Fget_char_property (pos, Qdisplay, Qnil);
5388 if (!NILP (prop) && display_prop_string_p (prop, string))
5389 found = true;
5390 else
5391 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5392 limit);
5395 else /* looking back */
5397 limit = make_number (max (to, BEGV));
5398 while (!found && !EQ (pos, limit))
5400 prop = Fget_char_property (pos, Qdisplay, Qnil);
5401 if (!NILP (prop) && display_prop_string_p (prop, string))
5402 found = true;
5403 else
5404 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5405 limit);
5409 return found ? XINT (pos) : 0;
5412 /* Determine which buffer position in current buffer STRING comes from.
5413 AROUND_CHARPOS is an approximate position where it could come from.
5414 Value is the buffer position or 0 if it couldn't be determined.
5416 This function is necessary because we don't record buffer positions
5417 in glyphs generated from strings (to keep struct glyph small).
5418 This function may only use code that doesn't eval because it is
5419 called asynchronously from note_mouse_highlight. */
5421 static ptrdiff_t
5422 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5424 const int MAX_DISTANCE = 1000;
5425 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5426 around_charpos + MAX_DISTANCE,
5427 false);
5429 if (!found)
5430 found = string_buffer_position_lim (string, around_charpos,
5431 around_charpos - MAX_DISTANCE, true);
5432 return found;
5437 /***********************************************************************
5438 `composition' property
5439 ***********************************************************************/
5441 /* Set up iterator IT from `composition' property at its current
5442 position. Called from handle_stop. */
5444 static enum prop_handled
5445 handle_composition_prop (struct it *it)
5447 Lisp_Object prop, string;
5448 ptrdiff_t pos, pos_byte, start, end;
5450 if (STRINGP (it->string))
5452 unsigned char *s;
5454 pos = IT_STRING_CHARPOS (*it);
5455 pos_byte = IT_STRING_BYTEPOS (*it);
5456 string = it->string;
5457 s = SDATA (string) + pos_byte;
5458 it->c = STRING_CHAR (s);
5460 else
5462 pos = IT_CHARPOS (*it);
5463 pos_byte = IT_BYTEPOS (*it);
5464 string = Qnil;
5465 it->c = FETCH_CHAR (pos_byte);
5468 /* If there's a valid composition and point is not inside of the
5469 composition (in the case that the composition is from the current
5470 buffer), draw a glyph composed from the composition components. */
5471 if (find_composition (pos, -1, &start, &end, &prop, string)
5472 && composition_valid_p (start, end, prop)
5473 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5475 if (start < pos)
5476 /* As we can't handle this situation (perhaps font-lock added
5477 a new composition), we just return here hoping that next
5478 redisplay will detect this composition much earlier. */
5479 return HANDLED_NORMALLY;
5480 if (start != pos)
5482 if (STRINGP (it->string))
5483 pos_byte = string_char_to_byte (it->string, start);
5484 else
5485 pos_byte = CHAR_TO_BYTE (start);
5487 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5488 prop, string);
5490 if (it->cmp_it.id >= 0)
5492 it->cmp_it.ch = -1;
5493 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5494 it->cmp_it.nglyphs = -1;
5498 return HANDLED_NORMALLY;
5503 /***********************************************************************
5504 Overlay strings
5505 ***********************************************************************/
5507 /* The following structure is used to record overlay strings for
5508 later sorting in load_overlay_strings. */
5510 struct overlay_entry
5512 Lisp_Object overlay;
5513 Lisp_Object string;
5514 EMACS_INT priority;
5515 bool after_string_p;
5519 /* Set up iterator IT from overlay strings at its current position.
5520 Called from handle_stop. */
5522 static enum prop_handled
5523 handle_overlay_change (struct it *it)
5525 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5526 return HANDLED_RECOMPUTE_PROPS;
5527 else
5528 return HANDLED_NORMALLY;
5532 /* Set up the next overlay string for delivery by IT, if there is an
5533 overlay string to deliver. Called by set_iterator_to_next when the
5534 end of the current overlay string is reached. If there are more
5535 overlay strings to display, IT->string and
5536 IT->current.overlay_string_index are set appropriately here.
5537 Otherwise IT->string is set to nil. */
5539 static void
5540 next_overlay_string (struct it *it)
5542 ++it->current.overlay_string_index;
5543 if (it->current.overlay_string_index == it->n_overlay_strings)
5545 /* No more overlay strings. Restore IT's settings to what
5546 they were before overlay strings were processed, and
5547 continue to deliver from current_buffer. */
5549 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5550 pop_it (it);
5551 eassert (it->sp > 0
5552 || (NILP (it->string)
5553 && it->method == GET_FROM_BUFFER
5554 && it->stop_charpos >= BEGV
5555 && it->stop_charpos <= it->end_charpos));
5556 it->current.overlay_string_index = -1;
5557 it->n_overlay_strings = 0;
5558 /* If there's an empty display string on the stack, pop the
5559 stack, to resync the bidi iterator with IT's position. Such
5560 empty strings are pushed onto the stack in
5561 get_overlay_strings_1. */
5562 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5563 pop_it (it);
5565 /* Since we've exhausted overlay strings at this buffer
5566 position, set the flag to ignore overlays until we move to
5567 another position. The flag is reset in
5568 next_element_from_buffer. */
5569 it->ignore_overlay_strings_at_pos_p = true;
5571 /* If we're at the end of the buffer, record that we have
5572 processed the overlay strings there already, so that
5573 next_element_from_buffer doesn't try it again. */
5574 if (NILP (it->string)
5575 && IT_CHARPOS (*it) >= it->end_charpos
5576 && it->overlay_strings_charpos >= it->end_charpos)
5577 it->overlay_strings_at_end_processed_p = true;
5578 /* Note: we reset overlay_strings_charpos only here, to make
5579 sure the just-processed overlays were indeed at EOB.
5580 Otherwise, overlays on text with invisible text property,
5581 which are processed with IT's position past the invisible
5582 text, might fool us into thinking the overlays at EOB were
5583 already processed (linum-mode can cause this, for
5584 example). */
5585 it->overlay_strings_charpos = -1;
5587 else
5589 /* There are more overlay strings to process. If
5590 IT->current.overlay_string_index has advanced to a position
5591 where we must load IT->overlay_strings with more strings, do
5592 it. We must load at the IT->overlay_strings_charpos where
5593 IT->n_overlay_strings was originally computed; when invisible
5594 text is present, this might not be IT_CHARPOS (Bug#7016). */
5595 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5597 if (it->current.overlay_string_index && i == 0)
5598 load_overlay_strings (it, it->overlay_strings_charpos);
5600 /* Initialize IT to deliver display elements from the overlay
5601 string. */
5602 it->string = it->overlay_strings[i];
5603 it->multibyte_p = STRING_MULTIBYTE (it->string);
5604 SET_TEXT_POS (it->current.string_pos, 0, 0);
5605 it->method = GET_FROM_STRING;
5606 it->stop_charpos = 0;
5607 it->end_charpos = SCHARS (it->string);
5608 if (it->cmp_it.stop_pos >= 0)
5609 it->cmp_it.stop_pos = 0;
5610 it->prev_stop = 0;
5611 it->base_level_stop = 0;
5613 /* Set up the bidi iterator for this overlay string. */
5614 if (it->bidi_p)
5616 it->bidi_it.string.lstring = it->string;
5617 it->bidi_it.string.s = NULL;
5618 it->bidi_it.string.schars = SCHARS (it->string);
5619 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5620 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5621 it->bidi_it.string.unibyte = !it->multibyte_p;
5622 it->bidi_it.w = it->w;
5623 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5627 CHECK_IT (it);
5631 /* Compare two overlay_entry structures E1 and E2. Used as a
5632 comparison function for qsort in load_overlay_strings. Overlay
5633 strings for the same position are sorted so that
5635 1. All after-strings come in front of before-strings, except
5636 when they come from the same overlay.
5638 2. Within after-strings, strings are sorted so that overlay strings
5639 from overlays with higher priorities come first.
5641 2. Within before-strings, strings are sorted so that overlay
5642 strings from overlays with higher priorities come last.
5644 Value is analogous to strcmp. */
5647 static int
5648 compare_overlay_entries (const void *e1, const void *e2)
5650 struct overlay_entry const *entry1 = e1;
5651 struct overlay_entry const *entry2 = e2;
5652 int result;
5654 if (entry1->after_string_p != entry2->after_string_p)
5656 /* Let after-strings appear in front of before-strings if
5657 they come from different overlays. */
5658 if (EQ (entry1->overlay, entry2->overlay))
5659 result = entry1->after_string_p ? 1 : -1;
5660 else
5661 result = entry1->after_string_p ? -1 : 1;
5663 else if (entry1->priority != entry2->priority)
5665 if (entry1->after_string_p)
5666 /* After-strings sorted in order of decreasing priority. */
5667 result = entry2->priority < entry1->priority ? -1 : 1;
5668 else
5669 /* Before-strings sorted in order of increasing priority. */
5670 result = entry1->priority < entry2->priority ? -1 : 1;
5672 else
5673 result = 0;
5675 return result;
5679 /* Load the vector IT->overlay_strings with overlay strings from IT's
5680 current buffer position, or from CHARPOS if that is > 0. Set
5681 IT->n_overlays to the total number of overlay strings found.
5683 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5684 a time. On entry into load_overlay_strings,
5685 IT->current.overlay_string_index gives the number of overlay
5686 strings that have already been loaded by previous calls to this
5687 function.
5689 IT->add_overlay_start contains an additional overlay start
5690 position to consider for taking overlay strings from, if non-zero.
5691 This position comes into play when the overlay has an `invisible'
5692 property, and both before and after-strings. When we've skipped to
5693 the end of the overlay, because of its `invisible' property, we
5694 nevertheless want its before-string to appear.
5695 IT->add_overlay_start will contain the overlay start position
5696 in this case.
5698 Overlay strings are sorted so that after-string strings come in
5699 front of before-string strings. Within before and after-strings,
5700 strings are sorted by overlay priority. See also function
5701 compare_overlay_entries. */
5703 static void
5704 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5706 Lisp_Object overlay, window, str, invisible;
5707 struct Lisp_Overlay *ov;
5708 ptrdiff_t start, end;
5709 ptrdiff_t n = 0, i, j;
5710 int invis;
5711 struct overlay_entry entriesbuf[20];
5712 ptrdiff_t size = ARRAYELTS (entriesbuf);
5713 struct overlay_entry *entries = entriesbuf;
5714 USE_SAFE_ALLOCA;
5716 if (charpos <= 0)
5717 charpos = IT_CHARPOS (*it);
5719 /* Append the overlay string STRING of overlay OVERLAY to vector
5720 `entries' which has size `size' and currently contains `n'
5721 elements. AFTER_P means STRING is an after-string of
5722 OVERLAY. */
5723 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5724 do \
5726 Lisp_Object priority; \
5728 if (n == size) \
5730 struct overlay_entry *old = entries; \
5731 SAFE_NALLOCA (entries, 2, size); \
5732 memcpy (entries, old, size * sizeof *entries); \
5733 size *= 2; \
5736 entries[n].string = (STRING); \
5737 entries[n].overlay = (OVERLAY); \
5738 priority = Foverlay_get ((OVERLAY), Qpriority); \
5739 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5740 entries[n].after_string_p = (AFTER_P); \
5741 ++n; \
5743 while (false)
5745 /* Process overlay before the overlay center. */
5746 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5748 XSETMISC (overlay, ov);
5749 eassert (OVERLAYP (overlay));
5750 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5751 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5753 if (end < charpos)
5754 break;
5756 /* Skip this overlay if it doesn't start or end at IT's current
5757 position. */
5758 if (end != charpos && start != charpos)
5759 continue;
5761 /* Skip this overlay if it doesn't apply to IT->w. */
5762 window = Foverlay_get (overlay, Qwindow);
5763 if (WINDOWP (window) && XWINDOW (window) != it->w)
5764 continue;
5766 /* If the text ``under'' the overlay is invisible, both before-
5767 and after-strings from this overlay are visible; start and
5768 end position are indistinguishable. */
5769 invisible = Foverlay_get (overlay, Qinvisible);
5770 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772 /* If overlay has a non-empty before-string, record it. */
5773 if ((start == charpos || (end == charpos && invis != 0))
5774 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5775 && SCHARS (str))
5776 RECORD_OVERLAY_STRING (overlay, str, false);
5778 /* If overlay has a non-empty after-string, record it. */
5779 if ((end == charpos || (start == charpos && invis != 0))
5780 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5781 && SCHARS (str))
5782 RECORD_OVERLAY_STRING (overlay, str, true);
5785 /* Process overlays after the overlay center. */
5786 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5788 XSETMISC (overlay, ov);
5789 eassert (OVERLAYP (overlay));
5790 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5791 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5793 if (start > charpos)
5794 break;
5796 /* Skip this overlay if it doesn't start or end at IT's current
5797 position. */
5798 if (end != charpos && start != charpos)
5799 continue;
5801 /* Skip this overlay if it doesn't apply to IT->w. */
5802 window = Foverlay_get (overlay, Qwindow);
5803 if (WINDOWP (window) && XWINDOW (window) != it->w)
5804 continue;
5806 /* If the text ``under'' the overlay is invisible, it has a zero
5807 dimension, and both before- and after-strings apply. */
5808 invisible = Foverlay_get (overlay, Qinvisible);
5809 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5811 /* If overlay has a non-empty before-string, record it. */
5812 if ((start == charpos || (end == charpos && invis != 0))
5813 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5814 && SCHARS (str))
5815 RECORD_OVERLAY_STRING (overlay, str, false);
5817 /* If overlay has a non-empty after-string, record it. */
5818 if ((end == charpos || (start == charpos && invis != 0))
5819 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5820 && SCHARS (str))
5821 RECORD_OVERLAY_STRING (overlay, str, true);
5824 #undef RECORD_OVERLAY_STRING
5826 /* Sort entries. */
5827 if (n > 1)
5828 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5830 /* Record number of overlay strings, and where we computed it. */
5831 it->n_overlay_strings = n;
5832 it->overlay_strings_charpos = charpos;
5834 /* IT->current.overlay_string_index is the number of overlay strings
5835 that have already been consumed by IT. Copy some of the
5836 remaining overlay strings to IT->overlay_strings. */
5837 i = 0;
5838 j = it->current.overlay_string_index;
5839 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5841 it->overlay_strings[i] = entries[j].string;
5842 it->string_overlays[i++] = entries[j++].overlay;
5845 CHECK_IT (it);
5846 SAFE_FREE ();
5850 /* Get the first chunk of overlay strings at IT's current buffer
5851 position, or at CHARPOS if that is > 0. Value is true if at
5852 least one overlay string was found. */
5854 static bool
5855 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5857 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5858 process. This fills IT->overlay_strings with strings, and sets
5859 IT->n_overlay_strings to the total number of strings to process.
5860 IT->pos.overlay_string_index has to be set temporarily to zero
5861 because load_overlay_strings needs this; it must be set to -1
5862 when no overlay strings are found because a zero value would
5863 indicate a position in the first overlay string. */
5864 it->current.overlay_string_index = 0;
5865 load_overlay_strings (it, charpos);
5867 /* If we found overlay strings, set up IT to deliver display
5868 elements from the first one. Otherwise set up IT to deliver
5869 from current_buffer. */
5870 if (it->n_overlay_strings)
5872 /* Make sure we know settings in current_buffer, so that we can
5873 restore meaningful values when we're done with the overlay
5874 strings. */
5875 if (compute_stop_p)
5876 compute_stop_pos (it);
5877 eassert (it->face_id >= 0);
5879 /* Save IT's settings. They are restored after all overlay
5880 strings have been processed. */
5881 eassert (!compute_stop_p || it->sp == 0);
5883 /* When called from handle_stop, there might be an empty display
5884 string loaded. In that case, don't bother saving it. But
5885 don't use this optimization with the bidi iterator, since we
5886 need the corresponding pop_it call to resync the bidi
5887 iterator's position with IT's position, after we are done
5888 with the overlay strings. (The corresponding call to pop_it
5889 in case of an empty display string is in
5890 next_overlay_string.) */
5891 if (!(!it->bidi_p
5892 && STRINGP (it->string) && !SCHARS (it->string)))
5893 push_it (it, NULL);
5895 /* Set up IT to deliver display elements from the first overlay
5896 string. */
5897 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5898 it->string = it->overlay_strings[0];
5899 it->from_overlay = Qnil;
5900 it->stop_charpos = 0;
5901 eassert (STRINGP (it->string));
5902 it->end_charpos = SCHARS (it->string);
5903 it->prev_stop = 0;
5904 it->base_level_stop = 0;
5905 it->multibyte_p = STRING_MULTIBYTE (it->string);
5906 it->method = GET_FROM_STRING;
5907 it->from_disp_prop_p = 0;
5909 /* Force paragraph direction to be that of the parent
5910 buffer. */
5911 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5912 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5913 else
5914 it->paragraph_embedding = L2R;
5916 /* Set up the bidi iterator for this overlay string. */
5917 if (it->bidi_p)
5919 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5921 it->bidi_it.string.lstring = it->string;
5922 it->bidi_it.string.s = NULL;
5923 it->bidi_it.string.schars = SCHARS (it->string);
5924 it->bidi_it.string.bufpos = pos;
5925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5926 it->bidi_it.string.unibyte = !it->multibyte_p;
5927 it->bidi_it.w = it->w;
5928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5930 return true;
5933 it->current.overlay_string_index = -1;
5934 return false;
5937 static bool
5938 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5940 it->string = Qnil;
5941 it->method = GET_FROM_BUFFER;
5943 get_overlay_strings_1 (it, charpos, true);
5945 CHECK_IT (it);
5947 /* Value is true if we found at least one overlay string. */
5948 return STRINGP (it->string);
5953 /***********************************************************************
5954 Saving and restoring state
5955 ***********************************************************************/
5957 /* Save current settings of IT on IT->stack. Called, for example,
5958 before setting up IT for an overlay string, to be able to restore
5959 IT's settings to what they were after the overlay string has been
5960 processed. If POSITION is non-NULL, it is the position to save on
5961 the stack instead of IT->position. */
5963 static void
5964 push_it (struct it *it, struct text_pos *position)
5966 struct iterator_stack_entry *p;
5968 eassert (it->sp < IT_STACK_SIZE);
5969 p = it->stack + it->sp;
5971 p->stop_charpos = it->stop_charpos;
5972 p->prev_stop = it->prev_stop;
5973 p->base_level_stop = it->base_level_stop;
5974 p->cmp_it = it->cmp_it;
5975 eassert (it->face_id >= 0);
5976 p->face_id = it->face_id;
5977 p->string = it->string;
5978 p->method = it->method;
5979 p->from_overlay = it->from_overlay;
5980 switch (p->method)
5982 case GET_FROM_IMAGE:
5983 p->u.image.object = it->object;
5984 p->u.image.image_id = it->image_id;
5985 p->u.image.slice = it->slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 p->u.stretch.object = it->object;
5989 break;
5990 case GET_FROM_XWIDGET:
5991 p->u.xwidget.object = it->object;
5992 break;
5993 case GET_FROM_BUFFER:
5994 case GET_FROM_DISPLAY_VECTOR:
5995 case GET_FROM_STRING:
5996 case GET_FROM_C_STRING:
5997 break;
5998 default:
5999 emacs_abort ();
6001 p->position = position ? *position : it->position;
6002 p->current = it->current;
6003 p->end_charpos = it->end_charpos;
6004 p->string_nchars = it->string_nchars;
6005 p->area = it->area;
6006 p->multibyte_p = it->multibyte_p;
6007 p->avoid_cursor_p = it->avoid_cursor_p;
6008 p->space_width = it->space_width;
6009 p->font_height = it->font_height;
6010 p->voffset = it->voffset;
6011 p->string_from_display_prop_p = it->string_from_display_prop_p;
6012 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6013 p->display_ellipsis_p = false;
6014 p->line_wrap = it->line_wrap;
6015 p->bidi_p = it->bidi_p;
6016 p->paragraph_embedding = it->paragraph_embedding;
6017 p->from_disp_prop_p = it->from_disp_prop_p;
6018 ++it->sp;
6020 /* Save the state of the bidi iterator as well. */
6021 if (it->bidi_p)
6022 bidi_push_it (&it->bidi_it);
6025 static void
6026 iterate_out_of_display_property (struct it *it)
6028 bool buffer_p = !STRINGP (it->string);
6029 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6030 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6032 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6034 /* Maybe initialize paragraph direction. If we are at the beginning
6035 of a new paragraph, next_element_from_buffer may not have a
6036 chance to do that. */
6037 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6038 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6039 /* prev_stop can be zero, so check against BEGV as well. */
6040 while (it->bidi_it.charpos >= bob
6041 && it->prev_stop <= it->bidi_it.charpos
6042 && it->bidi_it.charpos < CHARPOS (it->position)
6043 && it->bidi_it.charpos < eob)
6044 bidi_move_to_visually_next (&it->bidi_it);
6045 /* Record the stop_pos we just crossed, for when we cross it
6046 back, maybe. */
6047 if (it->bidi_it.charpos > CHARPOS (it->position))
6048 it->prev_stop = CHARPOS (it->position);
6049 /* If we ended up not where pop_it put us, resync IT's
6050 positional members with the bidi iterator. */
6051 if (it->bidi_it.charpos != CHARPOS (it->position))
6052 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6053 if (buffer_p)
6054 it->current.pos = it->position;
6055 else
6056 it->current.string_pos = it->position;
6059 /* Restore IT's settings from IT->stack. Called, for example, when no
6060 more overlay strings must be processed, and we return to delivering
6061 display elements from a buffer, or when the end of a string from a
6062 `display' property is reached and we return to delivering display
6063 elements from an overlay string, or from a buffer. */
6065 static void
6066 pop_it (struct it *it)
6068 struct iterator_stack_entry *p;
6069 bool from_display_prop = it->from_disp_prop_p;
6070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6072 eassert (it->sp > 0);
6073 --it->sp;
6074 p = it->stack + it->sp;
6075 it->stop_charpos = p->stop_charpos;
6076 it->prev_stop = p->prev_stop;
6077 it->base_level_stop = p->base_level_stop;
6078 it->cmp_it = p->cmp_it;
6079 it->face_id = p->face_id;
6080 it->current = p->current;
6081 it->position = p->position;
6082 it->string = p->string;
6083 it->from_overlay = p->from_overlay;
6084 if (NILP (it->string))
6085 SET_TEXT_POS (it->current.string_pos, -1, -1);
6086 it->method = p->method;
6087 switch (it->method)
6089 case GET_FROM_IMAGE:
6090 it->image_id = p->u.image.image_id;
6091 it->object = p->u.image.object;
6092 it->slice = p->u.image.slice;
6093 break;
6094 case GET_FROM_XWIDGET:
6095 it->object = p->u.xwidget.object;
6096 break;
6097 case GET_FROM_STRETCH:
6098 it->object = p->u.stretch.object;
6099 break;
6100 case GET_FROM_BUFFER:
6101 it->object = it->w->contents;
6102 break;
6103 case GET_FROM_STRING:
6105 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6107 /* Restore the face_box_p flag, since it could have been
6108 overwritten by the face of the object that we just finished
6109 displaying. */
6110 if (face)
6111 it->face_box_p = face->box != FACE_NO_BOX;
6112 it->object = it->string;
6114 break;
6115 case GET_FROM_DISPLAY_VECTOR:
6116 if (it->s)
6117 it->method = GET_FROM_C_STRING;
6118 else if (STRINGP (it->string))
6119 it->method = GET_FROM_STRING;
6120 else
6122 it->method = GET_FROM_BUFFER;
6123 it->object = it->w->contents;
6125 break;
6126 case GET_FROM_C_STRING:
6127 break;
6128 default:
6129 emacs_abort ();
6131 it->end_charpos = p->end_charpos;
6132 it->string_nchars = p->string_nchars;
6133 it->area = p->area;
6134 it->multibyte_p = p->multibyte_p;
6135 it->avoid_cursor_p = p->avoid_cursor_p;
6136 it->space_width = p->space_width;
6137 it->font_height = p->font_height;
6138 it->voffset = p->voffset;
6139 it->string_from_display_prop_p = p->string_from_display_prop_p;
6140 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6141 it->line_wrap = p->line_wrap;
6142 it->bidi_p = p->bidi_p;
6143 it->paragraph_embedding = p->paragraph_embedding;
6144 it->from_disp_prop_p = p->from_disp_prop_p;
6145 if (it->bidi_p)
6147 bidi_pop_it (&it->bidi_it);
6148 /* Bidi-iterate until we get out of the portion of text, if any,
6149 covered by a `display' text property or by an overlay with
6150 `display' property. (We cannot just jump there, because the
6151 internal coherency of the bidi iterator state can not be
6152 preserved across such jumps.) We also must determine the
6153 paragraph base direction if the overlay we just processed is
6154 at the beginning of a new paragraph. */
6155 if (from_display_prop
6156 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6157 iterate_out_of_display_property (it);
6159 eassert ((BUFFERP (it->object)
6160 && IT_CHARPOS (*it) == it->bidi_it.charpos
6161 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6162 || (STRINGP (it->object)
6163 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6164 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6165 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6167 /* If we move the iterator over text covered by a display property
6168 to a new buffer position, any info about previously seen overlays
6169 is no longer valid. */
6170 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6171 it->ignore_overlay_strings_at_pos_p = false;
6176 /***********************************************************************
6177 Moving over lines
6178 ***********************************************************************/
6180 /* Set IT's current position to the previous line start. */
6182 static void
6183 back_to_previous_line_start (struct it *it)
6185 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6187 DEC_BOTH (cp, bp);
6188 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6192 /* Move IT to the next line start.
6194 Value is true if a newline was found. Set *SKIPPED_P to true if
6195 we skipped over part of the text (as opposed to moving the iterator
6196 continuously over the text). Otherwise, don't change the value
6197 of *SKIPPED_P.
6199 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6200 iterator on the newline, if it was found.
6202 Newlines may come from buffer text, overlay strings, or strings
6203 displayed via the `display' property. That's the reason we can't
6204 simply use find_newline_no_quit.
6206 Note that this function may not skip over invisible text that is so
6207 because of text properties and immediately follows a newline. If
6208 it would, function reseat_at_next_visible_line_start, when called
6209 from set_iterator_to_next, would effectively make invisible
6210 characters following a newline part of the wrong glyph row, which
6211 leads to wrong cursor motion. */
6213 static bool
6214 forward_to_next_line_start (struct it *it, bool *skipped_p,
6215 struct bidi_it *bidi_it_prev)
6217 ptrdiff_t old_selective;
6218 bool newline_found_p = false;
6219 int n;
6220 const int MAX_NEWLINE_DISTANCE = 500;
6222 /* If already on a newline, just consume it to avoid unintended
6223 skipping over invisible text below. */
6224 if (it->what == IT_CHARACTER
6225 && it->c == '\n'
6226 && CHARPOS (it->position) == IT_CHARPOS (*it))
6228 if (it->bidi_p && bidi_it_prev)
6229 *bidi_it_prev = it->bidi_it;
6230 set_iterator_to_next (it, false);
6231 it->c = 0;
6232 return true;
6235 /* Don't handle selective display in the following. It's (a)
6236 unnecessary because it's done by the caller, and (b) leads to an
6237 infinite recursion because next_element_from_ellipsis indirectly
6238 calls this function. */
6239 old_selective = it->selective;
6240 it->selective = 0;
6242 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6243 from buffer text. */
6244 for (n = 0;
6245 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6246 n += !STRINGP (it->string))
6248 if (!get_next_display_element (it))
6249 return false;
6250 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6251 if (newline_found_p && it->bidi_p && bidi_it_prev)
6252 *bidi_it_prev = it->bidi_it;
6253 set_iterator_to_next (it, false);
6256 /* If we didn't find a newline near enough, see if we can use a
6257 short-cut. */
6258 if (!newline_found_p)
6260 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6261 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6262 1, &bytepos);
6263 Lisp_Object pos;
6265 eassert (!STRINGP (it->string));
6267 /* If there isn't any `display' property in sight, and no
6268 overlays, we can just use the position of the newline in
6269 buffer text. */
6270 if (it->stop_charpos >= limit
6271 || ((pos = Fnext_single_property_change (make_number (start),
6272 Qdisplay, Qnil,
6273 make_number (limit)),
6274 NILP (pos))
6275 && next_overlay_change (start) == ZV))
6277 if (!it->bidi_p)
6279 IT_CHARPOS (*it) = limit;
6280 IT_BYTEPOS (*it) = bytepos;
6282 else
6284 struct bidi_it bprev;
6286 /* Help bidi.c avoid expensive searches for display
6287 properties and overlays, by telling it that there are
6288 none up to `limit'. */
6289 if (it->bidi_it.disp_pos < limit)
6291 it->bidi_it.disp_pos = limit;
6292 it->bidi_it.disp_prop = 0;
6294 do {
6295 bprev = it->bidi_it;
6296 bidi_move_to_visually_next (&it->bidi_it);
6297 } while (it->bidi_it.charpos != limit);
6298 IT_CHARPOS (*it) = limit;
6299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 if (bidi_it_prev)
6301 *bidi_it_prev = bprev;
6303 *skipped_p = newline_found_p = true;
6305 else
6307 while (!newline_found_p)
6309 if (!get_next_display_element (it))
6310 break;
6311 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6312 if (newline_found_p && it->bidi_p && bidi_it_prev)
6313 *bidi_it_prev = it->bidi_it;
6314 set_iterator_to_next (it, false);
6319 it->selective = old_selective;
6320 return newline_found_p;
6324 /* Set IT's current position to the previous visible line start. Skip
6325 invisible text that is so either due to text properties or due to
6326 selective display. Caution: this does not change IT->current_x and
6327 IT->hpos. */
6329 static void
6330 back_to_previous_visible_line_start (struct it *it)
6332 while (IT_CHARPOS (*it) > BEGV)
6334 back_to_previous_line_start (it);
6336 if (IT_CHARPOS (*it) <= BEGV)
6337 break;
6339 /* If selective > 0, then lines indented more than its value are
6340 invisible. */
6341 if (it->selective > 0
6342 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6343 it->selective))
6344 continue;
6346 /* Check the newline before point for invisibility. */
6348 Lisp_Object prop;
6349 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6350 Qinvisible, it->window);
6351 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6352 continue;
6355 if (IT_CHARPOS (*it) <= BEGV)
6356 break;
6359 struct it it2;
6360 void *it2data = NULL;
6361 ptrdiff_t pos;
6362 ptrdiff_t beg, end;
6363 Lisp_Object val, overlay;
6365 SAVE_IT (it2, *it, it2data);
6367 /* If newline is part of a composition, continue from start of composition */
6368 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6369 && beg < IT_CHARPOS (*it))
6370 goto replaced;
6372 /* If newline is replaced by a display property, find start of overlay
6373 or interval and continue search from that point. */
6374 pos = --IT_CHARPOS (it2);
6375 --IT_BYTEPOS (it2);
6376 it2.sp = 0;
6377 bidi_unshelve_cache (NULL, false);
6378 it2.string_from_display_prop_p = false;
6379 it2.from_disp_prop_p = false;
6380 if (handle_display_prop (&it2) == HANDLED_RETURN
6381 && !NILP (val = get_char_property_and_overlay
6382 (make_number (pos), Qdisplay, Qnil, &overlay))
6383 && (OVERLAYP (overlay)
6384 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6385 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6387 RESTORE_IT (it, it, it2data);
6388 goto replaced;
6391 /* Newline is not replaced by anything -- so we are done. */
6392 RESTORE_IT (it, it, it2data);
6393 break;
6395 replaced:
6396 if (beg < BEGV)
6397 beg = BEGV;
6398 IT_CHARPOS (*it) = beg;
6399 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6403 it->continuation_lines_width = 0;
6405 eassert (IT_CHARPOS (*it) >= BEGV);
6406 eassert (IT_CHARPOS (*it) == BEGV
6407 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6408 CHECK_IT (it);
6412 /* Reseat iterator IT at the previous visible line start. Skip
6413 invisible text that is so either due to text properties or due to
6414 selective display. At the end, update IT's overlay information,
6415 face information etc. */
6417 void
6418 reseat_at_previous_visible_line_start (struct it *it)
6420 back_to_previous_visible_line_start (it);
6421 reseat (it, it->current.pos, true);
6422 CHECK_IT (it);
6426 /* Reseat iterator IT on the next visible line start in the current
6427 buffer. ON_NEWLINE_P means position IT on the newline
6428 preceding the line start. Skip over invisible text that is so
6429 because of selective display. Compute faces, overlays etc at the
6430 new position. Note that this function does not skip over text that
6431 is invisible because of text properties. */
6433 static void
6434 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6436 bool skipped_p = false;
6437 struct bidi_it bidi_it_prev;
6438 bool newline_found_p
6439 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6441 /* Skip over lines that are invisible because they are indented
6442 more than the value of IT->selective. */
6443 if (it->selective > 0)
6444 while (IT_CHARPOS (*it) < ZV
6445 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6446 it->selective))
6448 eassert (IT_BYTEPOS (*it) == BEGV
6449 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6450 newline_found_p =
6451 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6454 /* Position on the newline if that's what's requested. */
6455 if (on_newline_p && newline_found_p)
6457 if (STRINGP (it->string))
6459 if (IT_STRING_CHARPOS (*it) > 0)
6461 if (!it->bidi_p)
6463 --IT_STRING_CHARPOS (*it);
6464 --IT_STRING_BYTEPOS (*it);
6466 else
6468 /* We need to restore the bidi iterator to the state
6469 it had on the newline, and resync the IT's
6470 position with that. */
6471 it->bidi_it = bidi_it_prev;
6472 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6473 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6477 else if (IT_CHARPOS (*it) > BEGV)
6479 if (!it->bidi_p)
6481 --IT_CHARPOS (*it);
6482 --IT_BYTEPOS (*it);
6484 else
6486 /* We need to restore the bidi iterator to the state it
6487 had on the newline and resync IT with that. */
6488 it->bidi_it = bidi_it_prev;
6489 IT_CHARPOS (*it) = it->bidi_it.charpos;
6490 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6492 reseat (it, it->current.pos, false);
6495 else if (skipped_p)
6496 reseat (it, it->current.pos, false);
6498 CHECK_IT (it);
6503 /***********************************************************************
6504 Changing an iterator's position
6505 ***********************************************************************/
6507 /* Change IT's current position to POS in current_buffer.
6508 If FORCE_P, always check for text properties at the new position.
6509 Otherwise, text properties are only looked up if POS >=
6510 IT->check_charpos of a property. */
6512 static void
6513 reseat (struct it *it, struct text_pos pos, bool force_p)
6515 ptrdiff_t original_pos = IT_CHARPOS (*it);
6517 reseat_1 (it, pos, false);
6519 /* Determine where to check text properties. Avoid doing it
6520 where possible because text property lookup is very expensive. */
6521 if (force_p
6522 || CHARPOS (pos) > it->stop_charpos
6523 || CHARPOS (pos) < original_pos)
6525 if (it->bidi_p)
6527 /* For bidi iteration, we need to prime prev_stop and
6528 base_level_stop with our best estimations. */
6529 /* Implementation note: Of course, POS is not necessarily a
6530 stop position, so assigning prev_pos to it is a lie; we
6531 should have called compute_stop_backwards. However, if
6532 the current buffer does not include any R2L characters,
6533 that call would be a waste of cycles, because the
6534 iterator will never move back, and thus never cross this
6535 "fake" stop position. So we delay that backward search
6536 until the time we really need it, in next_element_from_buffer. */
6537 if (CHARPOS (pos) != it->prev_stop)
6538 it->prev_stop = CHARPOS (pos);
6539 if (CHARPOS (pos) < it->base_level_stop)
6540 it->base_level_stop = 0; /* meaning it's unknown */
6541 handle_stop (it);
6543 else
6545 handle_stop (it);
6546 it->prev_stop = it->base_level_stop = 0;
6551 CHECK_IT (it);
6555 /* Change IT's buffer position to POS. SET_STOP_P means set
6556 IT->stop_pos to POS, also. */
6558 static void
6559 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6561 /* Don't call this function when scanning a C string. */
6562 eassert (it->s == NULL);
6564 /* POS must be a reasonable value. */
6565 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6567 it->current.pos = it->position = pos;
6568 it->end_charpos = ZV;
6569 it->dpvec = NULL;
6570 it->current.dpvec_index = -1;
6571 it->current.overlay_string_index = -1;
6572 IT_STRING_CHARPOS (*it) = -1;
6573 IT_STRING_BYTEPOS (*it) = -1;
6574 it->string = Qnil;
6575 it->method = GET_FROM_BUFFER;
6576 it->object = it->w->contents;
6577 it->area = TEXT_AREA;
6578 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6579 it->sp = 0;
6580 it->string_from_display_prop_p = false;
6581 it->string_from_prefix_prop_p = false;
6583 it->from_disp_prop_p = false;
6584 it->face_before_selective_p = false;
6585 if (it->bidi_p)
6587 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6588 &it->bidi_it);
6589 bidi_unshelve_cache (NULL, false);
6590 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6591 it->bidi_it.string.s = NULL;
6592 it->bidi_it.string.lstring = Qnil;
6593 it->bidi_it.string.bufpos = 0;
6594 it->bidi_it.string.from_disp_str = false;
6595 it->bidi_it.string.unibyte = false;
6596 it->bidi_it.w = it->w;
6599 if (set_stop_p)
6601 it->stop_charpos = CHARPOS (pos);
6602 it->base_level_stop = CHARPOS (pos);
6604 /* This make the information stored in it->cmp_it invalidate. */
6605 it->cmp_it.id = -1;
6609 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6610 If S is non-null, it is a C string to iterate over. Otherwise,
6611 STRING gives a Lisp string to iterate over.
6613 If PRECISION > 0, don't return more then PRECISION number of
6614 characters from the string.
6616 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6617 characters have been returned. FIELD_WIDTH < 0 means an infinite
6618 field width.
6620 MULTIBYTE = 0 means disable processing of multibyte characters,
6621 MULTIBYTE > 0 means enable it,
6622 MULTIBYTE < 0 means use IT->multibyte_p.
6624 IT must be initialized via a prior call to init_iterator before
6625 calling this function. */
6627 static void
6628 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6629 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6630 int multibyte)
6632 /* No text property checks performed by default, but see below. */
6633 it->stop_charpos = -1;
6635 /* Set iterator position and end position. */
6636 memset (&it->current, 0, sizeof it->current);
6637 it->current.overlay_string_index = -1;
6638 it->current.dpvec_index = -1;
6639 eassert (charpos >= 0);
6641 /* If STRING is specified, use its multibyteness, otherwise use the
6642 setting of MULTIBYTE, if specified. */
6643 if (multibyte >= 0)
6644 it->multibyte_p = multibyte > 0;
6646 /* Bidirectional reordering of strings is controlled by the default
6647 value of bidi-display-reordering. Don't try to reorder while
6648 loading loadup.el, as the necessary character property tables are
6649 not yet available. */
6650 it->bidi_p =
6651 !redisplay__inhibit_bidi
6652 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6654 if (s == NULL)
6656 eassert (STRINGP (string));
6657 it->string = string;
6658 it->s = NULL;
6659 it->end_charpos = it->string_nchars = SCHARS (string);
6660 it->method = GET_FROM_STRING;
6661 it->current.string_pos = string_pos (charpos, string);
6663 if (it->bidi_p)
6665 it->bidi_it.string.lstring = string;
6666 it->bidi_it.string.s = NULL;
6667 it->bidi_it.string.schars = it->end_charpos;
6668 it->bidi_it.string.bufpos = 0;
6669 it->bidi_it.string.from_disp_str = false;
6670 it->bidi_it.string.unibyte = !it->multibyte_p;
6671 it->bidi_it.w = it->w;
6672 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6673 FRAME_WINDOW_P (it->f), &it->bidi_it);
6676 else
6678 it->s = (const unsigned char *) s;
6679 it->string = Qnil;
6681 /* Note that we use IT->current.pos, not it->current.string_pos,
6682 for displaying C strings. */
6683 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6684 if (it->multibyte_p)
6686 it->current.pos = c_string_pos (charpos, s, true);
6687 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6689 else
6691 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6692 it->end_charpos = it->string_nchars = strlen (s);
6695 if (it->bidi_p)
6697 it->bidi_it.string.lstring = Qnil;
6698 it->bidi_it.string.s = (const unsigned char *) s;
6699 it->bidi_it.string.schars = it->end_charpos;
6700 it->bidi_it.string.bufpos = 0;
6701 it->bidi_it.string.from_disp_str = false;
6702 it->bidi_it.string.unibyte = !it->multibyte_p;
6703 it->bidi_it.w = it->w;
6704 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6705 &it->bidi_it);
6707 it->method = GET_FROM_C_STRING;
6710 /* PRECISION > 0 means don't return more than PRECISION characters
6711 from the string. */
6712 if (precision > 0 && it->end_charpos - charpos > precision)
6714 it->end_charpos = it->string_nchars = charpos + precision;
6715 if (it->bidi_p)
6716 it->bidi_it.string.schars = it->end_charpos;
6719 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6720 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6721 FIELD_WIDTH < 0 means infinite field width. This is useful for
6722 padding with `-' at the end of a mode line. */
6723 if (field_width < 0)
6724 field_width = INFINITY;
6725 /* Implementation note: We deliberately don't enlarge
6726 it->bidi_it.string.schars here to fit it->end_charpos, because
6727 the bidi iterator cannot produce characters out of thin air. */
6728 if (field_width > it->end_charpos - charpos)
6729 it->end_charpos = charpos + field_width;
6731 /* Use the standard display table for displaying strings. */
6732 if (DISP_TABLE_P (Vstandard_display_table))
6733 it->dp = XCHAR_TABLE (Vstandard_display_table);
6735 it->stop_charpos = charpos;
6736 it->prev_stop = charpos;
6737 it->base_level_stop = 0;
6738 if (it->bidi_p)
6740 it->bidi_it.first_elt = true;
6741 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6742 it->bidi_it.disp_pos = -1;
6744 if (s == NULL && it->multibyte_p)
6746 ptrdiff_t endpos = SCHARS (it->string);
6747 if (endpos > it->end_charpos)
6748 endpos = it->end_charpos;
6749 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6750 it->string);
6752 CHECK_IT (it);
6757 /***********************************************************************
6758 Iteration
6759 ***********************************************************************/
6761 /* Map enum it_method value to corresponding next_element_from_* function. */
6763 typedef bool (*next_element_function) (struct it *);
6765 static next_element_function const get_next_element[NUM_IT_METHODS] =
6767 next_element_from_buffer,
6768 next_element_from_display_vector,
6769 next_element_from_string,
6770 next_element_from_c_string,
6771 next_element_from_image,
6772 next_element_from_stretch,
6773 next_element_from_xwidget,
6776 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6779 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6780 (possibly with the following characters). */
6782 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6783 ((IT)->cmp_it.id >= 0 \
6784 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6785 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6786 END_CHARPOS, (IT)->w, \
6787 FACE_FROM_ID_OR_NULL ((IT)->f, \
6788 (IT)->face_id), \
6789 (IT)->string)))
6792 /* Lookup the char-table Vglyphless_char_display for character C (-1
6793 if we want information for no-font case), and return the display
6794 method symbol. By side-effect, update it->what and
6795 it->glyphless_method. This function is called from
6796 get_next_display_element for each character element, and from
6797 x_produce_glyphs when no suitable font was found. */
6799 Lisp_Object
6800 lookup_glyphless_char_display (int c, struct it *it)
6802 Lisp_Object glyphless_method = Qnil;
6804 if (CHAR_TABLE_P (Vglyphless_char_display)
6805 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6807 if (c >= 0)
6809 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6810 if (CONSP (glyphless_method))
6811 glyphless_method = FRAME_WINDOW_P (it->f)
6812 ? XCAR (glyphless_method)
6813 : XCDR (glyphless_method);
6815 else
6816 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6819 retry:
6820 if (NILP (glyphless_method))
6822 if (c >= 0)
6823 /* The default is to display the character by a proper font. */
6824 return Qnil;
6825 /* The default for the no-font case is to display an empty box. */
6826 glyphless_method = Qempty_box;
6828 if (EQ (glyphless_method, Qzero_width))
6830 if (c >= 0)
6831 return glyphless_method;
6832 /* This method can't be used for the no-font case. */
6833 glyphless_method = Qempty_box;
6835 if (EQ (glyphless_method, Qthin_space))
6836 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6837 else if (EQ (glyphless_method, Qempty_box))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6839 else if (EQ (glyphless_method, Qhex_code))
6840 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6841 else if (STRINGP (glyphless_method))
6842 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6843 else
6845 /* Invalid value. We use the default method. */
6846 glyphless_method = Qnil;
6847 goto retry;
6849 it->what = IT_GLYPHLESS;
6850 return glyphless_method;
6853 /* Merge escape glyph face and cache the result. */
6855 static struct frame *last_escape_glyph_frame = NULL;
6856 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6857 static int last_escape_glyph_merged_face_id = 0;
6859 static int
6860 merge_escape_glyph_face (struct it *it)
6862 int face_id;
6864 if (it->f == last_escape_glyph_frame
6865 && it->face_id == last_escape_glyph_face_id)
6866 face_id = last_escape_glyph_merged_face_id;
6867 else
6869 /* Merge the `escape-glyph' face into the current face. */
6870 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6871 last_escape_glyph_frame = it->f;
6872 last_escape_glyph_face_id = it->face_id;
6873 last_escape_glyph_merged_face_id = face_id;
6875 return face_id;
6878 /* Likewise for glyphless glyph face. */
6880 static struct frame *last_glyphless_glyph_frame = NULL;
6881 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6882 static int last_glyphless_glyph_merged_face_id = 0;
6885 merge_glyphless_glyph_face (struct it *it)
6887 int face_id;
6889 if (it->f == last_glyphless_glyph_frame
6890 && it->face_id == last_glyphless_glyph_face_id)
6891 face_id = last_glyphless_glyph_merged_face_id;
6892 else
6894 /* Merge the `glyphless-char' face into the current face. */
6895 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6896 last_glyphless_glyph_frame = it->f;
6897 last_glyphless_glyph_face_id = it->face_id;
6898 last_glyphless_glyph_merged_face_id = face_id;
6900 return face_id;
6903 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6904 be called before redisplaying windows, and when the frame's face
6905 cache is freed. */
6906 void
6907 forget_escape_and_glyphless_faces (void)
6909 last_escape_glyph_frame = NULL;
6910 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6911 last_glyphless_glyph_frame = NULL;
6912 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6915 /* Load IT's display element fields with information about the next
6916 display element from the current position of IT. Value is false if
6917 end of buffer (or C string) is reached. */
6919 static bool
6920 get_next_display_element (struct it *it)
6922 /* True means that we found a display element. False means that
6923 we hit the end of what we iterate over. Performance note: the
6924 function pointer `method' used here turns out to be faster than
6925 using a sequence of if-statements. */
6926 bool success_p;
6928 get_next:
6929 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6931 if (it->what == IT_CHARACTER)
6933 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6934 and only if (a) the resolved directionality of that character
6935 is R..." */
6936 /* FIXME: Do we need an exception for characters from display
6937 tables? */
6938 if (it->bidi_p && it->bidi_it.type == STRONG_R
6939 && !inhibit_bidi_mirroring)
6940 it->c = bidi_mirror_char (it->c);
6941 /* Map via display table or translate control characters.
6942 IT->c, IT->len etc. have been set to the next character by
6943 the function call above. If we have a display table, and it
6944 contains an entry for IT->c, translate it. Don't do this if
6945 IT->c itself comes from a display table, otherwise we could
6946 end up in an infinite recursion. (An alternative could be to
6947 count the recursion depth of this function and signal an
6948 error when a certain maximum depth is reached.) Is it worth
6949 it? */
6950 if (success_p && it->dpvec == NULL)
6952 Lisp_Object dv;
6953 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6954 bool nonascii_space_p = false;
6955 bool nonascii_hyphen_p = false;
6956 int c = it->c; /* This is the character to display. */
6958 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6960 eassert (SINGLE_BYTE_CHAR_P (c));
6961 if (unibyte_display_via_language_environment)
6963 c = DECODE_CHAR (unibyte, c);
6964 if (c < 0)
6965 c = BYTE8_TO_CHAR (it->c);
6967 else
6968 c = BYTE8_TO_CHAR (it->c);
6971 if (it->dp
6972 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6973 VECTORP (dv)))
6975 struct Lisp_Vector *v = XVECTOR (dv);
6977 /* Return the first character from the display table
6978 entry, if not empty. If empty, don't display the
6979 current character. */
6980 if (v->header.size)
6982 it->dpvec_char_len = it->len;
6983 it->dpvec = v->contents;
6984 it->dpend = v->contents + v->header.size;
6985 it->current.dpvec_index = 0;
6986 it->dpvec_face_id = -1;
6987 it->saved_face_id = it->face_id;
6988 it->method = GET_FROM_DISPLAY_VECTOR;
6989 it->ellipsis_p = false;
6991 else
6993 set_iterator_to_next (it, false);
6995 goto get_next;
6998 if (! NILP (lookup_glyphless_char_display (c, it)))
7000 if (it->what == IT_GLYPHLESS)
7001 goto done;
7002 /* Don't display this character. */
7003 set_iterator_to_next (it, false);
7004 goto get_next;
7007 /* If `nobreak-char-display' is non-nil, we display
7008 non-ASCII spaces and hyphens specially. */
7009 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7011 if (c == NO_BREAK_SPACE)
7012 nonascii_space_p = true;
7013 else if (c == SOFT_HYPHEN || c == HYPHEN
7014 || c == NON_BREAKING_HYPHEN)
7015 nonascii_hyphen_p = true;
7018 /* Translate control characters into `\003' or `^C' form.
7019 Control characters coming from a display table entry are
7020 currently not translated because we use IT->dpvec to hold
7021 the translation. This could easily be changed but I
7022 don't believe that it is worth doing.
7024 The characters handled by `nobreak-char-display' must be
7025 translated too.
7027 Non-printable characters and raw-byte characters are also
7028 translated to octal form. */
7029 if (((c < ' ' || c == 127) /* ASCII control chars. */
7030 ? (it->area != TEXT_AREA
7031 /* In mode line, treat \n, \t like other crl chars. */
7032 || (c != '\t'
7033 && it->glyph_row
7034 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7035 || (c != '\n' && c != '\t'))
7036 : (nonascii_space_p
7037 || nonascii_hyphen_p
7038 || CHAR_BYTE8_P (c)
7039 || ! CHAR_PRINTABLE_P (c))))
7041 /* C is a control character, non-ASCII space/hyphen,
7042 raw-byte, or a non-printable character which must be
7043 displayed either as '\003' or as `^C' where the '\\'
7044 and '^' can be defined in the display table. Fill
7045 IT->ctl_chars with glyphs for what we have to
7046 display. Then, set IT->dpvec to these glyphs. */
7047 Lisp_Object gc;
7048 int ctl_len;
7049 int face_id;
7050 int lface_id = 0;
7051 int escape_glyph;
7053 /* Handle control characters with ^. */
7055 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7057 int g;
7059 g = '^'; /* default glyph for Control */
7060 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7061 if (it->dp
7062 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7064 g = GLYPH_CODE_CHAR (gc);
7065 lface_id = GLYPH_CODE_FACE (gc);
7068 face_id = (lface_id
7069 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7070 : merge_escape_glyph_face (it));
7072 XSETINT (it->ctl_chars[0], g);
7073 XSETINT (it->ctl_chars[1], c ^ 0100);
7074 ctl_len = 2;
7075 goto display_control;
7078 /* Handle non-ascii space in the mode where it only gets
7079 highlighting. */
7081 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7083 /* Merge `nobreak-space' into the current face. */
7084 face_id = merge_faces (it->f, Qnobreak_space, 0,
7085 it->face_id);
7086 XSETINT (it->ctl_chars[0], ' ');
7087 ctl_len = 1;
7088 goto display_control;
7091 /* Handle non-ascii hyphens in the mode where it only
7092 gets highlighting. */
7094 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7096 /* Merge `nobreak-space' into the current face. */
7097 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7098 it->face_id);
7099 XSETINT (it->ctl_chars[0], '-');
7100 ctl_len = 1;
7101 goto display_control;
7104 /* Handle sequences that start with the "escape glyph". */
7106 /* the default escape glyph is \. */
7107 escape_glyph = '\\';
7109 if (it->dp
7110 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7112 escape_glyph = GLYPH_CODE_CHAR (gc);
7113 lface_id = GLYPH_CODE_FACE (gc);
7116 face_id = (lface_id
7117 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7118 : merge_escape_glyph_face (it));
7120 /* Draw non-ASCII space/hyphen with escape glyph: */
7122 if (nonascii_space_p || nonascii_hyphen_p)
7124 XSETINT (it->ctl_chars[0], escape_glyph);
7125 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7126 ctl_len = 2;
7127 goto display_control;
7131 char str[10];
7132 int len, i;
7134 if (CHAR_BYTE8_P (c))
7135 /* Display \200 instead of \17777600. */
7136 c = CHAR_TO_BYTE8 (c);
7137 len = sprintf (str, "%03o", c + 0u);
7139 XSETINT (it->ctl_chars[0], escape_glyph);
7140 for (i = 0; i < len; i++)
7141 XSETINT (it->ctl_chars[i + 1], str[i]);
7142 ctl_len = len + 1;
7145 display_control:
7146 /* Set up IT->dpvec and return first character from it. */
7147 it->dpvec_char_len = it->len;
7148 it->dpvec = it->ctl_chars;
7149 it->dpend = it->dpvec + ctl_len;
7150 it->current.dpvec_index = 0;
7151 it->dpvec_face_id = face_id;
7152 it->saved_face_id = it->face_id;
7153 it->method = GET_FROM_DISPLAY_VECTOR;
7154 it->ellipsis_p = false;
7155 goto get_next;
7157 it->char_to_display = c;
7159 else if (success_p)
7161 it->char_to_display = it->c;
7165 #ifdef HAVE_WINDOW_SYSTEM
7166 /* Adjust face id for a multibyte character. There are no multibyte
7167 character in unibyte text. */
7168 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7169 && it->multibyte_p
7170 && success_p
7171 && FRAME_WINDOW_P (it->f))
7173 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7175 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7177 /* Automatic composition with glyph-string. */
7178 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7180 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7182 else
7184 ptrdiff_t pos = (it->s ? -1
7185 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7186 : IT_CHARPOS (*it));
7187 int c;
7189 if (it->what == IT_CHARACTER)
7190 c = it->char_to_display;
7191 else
7193 struct composition *cmp = composition_table[it->cmp_it.id];
7194 int i;
7196 c = ' ';
7197 for (i = 0; i < cmp->glyph_len; i++)
7198 /* TAB in a composition means display glyphs with
7199 padding space on the left or right. */
7200 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7201 break;
7203 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7206 #endif /* HAVE_WINDOW_SYSTEM */
7208 done:
7209 /* Is this character the last one of a run of characters with
7210 box? If yes, set IT->end_of_box_run_p to true. */
7211 if (it->face_box_p
7212 && it->s == NULL)
7214 if (it->method == GET_FROM_STRING && it->sp)
7216 int face_id = underlying_face_id (it);
7217 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7219 if (face)
7221 if (face->box == FACE_NO_BOX)
7223 /* If the box comes from face properties in a
7224 display string, check faces in that string. */
7225 int string_face_id = face_after_it_pos (it);
7226 it->end_of_box_run_p
7227 = (FACE_FROM_ID (it->f, string_face_id)->box
7228 == FACE_NO_BOX);
7230 /* Otherwise, the box comes from the underlying face.
7231 If this is the last string character displayed, check
7232 the next buffer location. */
7233 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7234 /* n_overlay_strings is unreliable unless
7235 overlay_string_index is non-negative. */
7236 && ((it->current.overlay_string_index >= 0
7237 && (it->current.overlay_string_index
7238 == it->n_overlay_strings - 1))
7239 /* A string from display property. */
7240 || it->from_disp_prop_p))
7242 ptrdiff_t ignore;
7243 int next_face_id;
7244 bool text_from_string = false;
7245 /* Normally, the next buffer location is stored in
7246 IT->current.pos... */
7247 struct text_pos pos = it->current.pos;
7249 /* ...but for a string from a display property, the
7250 next buffer position is stored in the 'position'
7251 member of the iteration stack slot below the
7252 current one, see handle_single_display_spec. By
7253 contrast, it->current.pos was not yet updated to
7254 point to that buffer position; that will happen
7255 in pop_it, after we finish displaying the current
7256 string. Note that we already checked above that
7257 it->sp is positive, so subtracting one from it is
7258 safe. */
7259 if (it->from_disp_prop_p)
7261 int stackp = it->sp - 1;
7263 /* Find the stack level with data from buffer. */
7264 while (stackp >= 0
7265 && STRINGP ((it->stack + stackp)->string))
7266 stackp--;
7267 if (stackp < 0)
7269 /* If no stack slot was found for iterating
7270 a buffer, we are displaying text from a
7271 string, most probably the mode line or
7272 the header line, and that string has a
7273 display string on some of its
7274 characters. */
7275 text_from_string = true;
7276 pos = it->stack[it->sp - 1].position;
7278 else
7279 pos = (it->stack + stackp)->position;
7281 else
7282 INC_TEXT_POS (pos, it->multibyte_p);
7284 if (text_from_string)
7286 Lisp_Object base_string = it->stack[it->sp - 1].string;
7288 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7289 it->end_of_box_run_p = true;
7290 else
7292 next_face_id
7293 = face_at_string_position (it->w, base_string,
7294 CHARPOS (pos), 0,
7295 &ignore, face_id, false);
7296 it->end_of_box_run_p
7297 = (FACE_FROM_ID (it->f, next_face_id)->box
7298 == FACE_NO_BOX);
7301 else if (CHARPOS (pos) >= ZV)
7302 it->end_of_box_run_p = true;
7303 else
7305 next_face_id =
7306 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7307 CHARPOS (pos)
7308 + TEXT_PROP_DISTANCE_LIMIT,
7309 false, -1);
7310 it->end_of_box_run_p
7311 = (FACE_FROM_ID (it->f, next_face_id)->box
7312 == FACE_NO_BOX);
7317 /* next_element_from_display_vector sets this flag according to
7318 faces of the display vector glyphs, see there. */
7319 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7321 int face_id = face_after_it_pos (it);
7322 it->end_of_box_run_p
7323 = (face_id != it->face_id
7324 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7327 /* If we reached the end of the object we've been iterating (e.g., a
7328 display string or an overlay string), and there's something on
7329 IT->stack, proceed with what's on the stack. It doesn't make
7330 sense to return false if there's unprocessed stuff on the stack,
7331 because otherwise that stuff will never be displayed. */
7332 if (!success_p && it->sp > 0)
7334 set_iterator_to_next (it, false);
7335 success_p = get_next_display_element (it);
7338 /* Value is false if end of buffer or string reached. */
7339 return success_p;
7343 /* Move IT to the next display element.
7345 RESEAT_P means if called on a newline in buffer text,
7346 skip to the next visible line start.
7348 Functions get_next_display_element and set_iterator_to_next are
7349 separate because I find this arrangement easier to handle than a
7350 get_next_display_element function that also increments IT's
7351 position. The way it is we can first look at an iterator's current
7352 display element, decide whether it fits on a line, and if it does,
7353 increment the iterator position. The other way around we probably
7354 would either need a flag indicating whether the iterator has to be
7355 incremented the next time, or we would have to implement a
7356 decrement position function which would not be easy to write. */
7358 void
7359 set_iterator_to_next (struct it *it, bool reseat_p)
7361 /* Reset flags indicating start and end of a sequence of characters
7362 with box. Reset them at the start of this function because
7363 moving the iterator to a new position might set them. */
7364 it->start_of_box_run_p = it->end_of_box_run_p = false;
7366 switch (it->method)
7368 case GET_FROM_BUFFER:
7369 /* The current display element of IT is a character from
7370 current_buffer. Advance in the buffer, and maybe skip over
7371 invisible lines that are so because of selective display. */
7372 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7373 reseat_at_next_visible_line_start (it, false);
7374 else if (it->cmp_it.id >= 0)
7376 /* We are currently getting glyphs from a composition. */
7377 if (! it->bidi_p)
7379 IT_CHARPOS (*it) += it->cmp_it.nchars;
7380 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7382 else
7384 int i;
7386 /* Update IT's char/byte positions to point to the first
7387 character of the next grapheme cluster, or to the
7388 character visually after the current composition. */
7389 for (i = 0; i < it->cmp_it.nchars; i++)
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_CHARPOS (*it) = it->bidi_it.charpos;
7395 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7396 && it->cmp_it.to < it->cmp_it.nglyphs)
7398 /* Composition created while scanning forward. Proceed
7399 to the next grapheme cluster. */
7400 it->cmp_it.from = it->cmp_it.to;
7402 else if ((it->bidi_p && it->cmp_it.reversed_p)
7403 && it->cmp_it.from > 0)
7405 /* Composition created while scanning backward. Proceed
7406 to the previous grapheme cluster. */
7407 it->cmp_it.to = it->cmp_it.from;
7409 else
7411 /* No more grapheme clusters in this composition.
7412 Find the next stop position. */
7413 ptrdiff_t stop = it->end_charpos;
7415 if (it->bidi_it.scan_dir < 0)
7416 /* Now we are scanning backward and don't know
7417 where to stop. */
7418 stop = -1;
7419 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7420 IT_BYTEPOS (*it), stop, Qnil);
7423 else
7425 eassert (it->len != 0);
7427 if (!it->bidi_p)
7429 IT_BYTEPOS (*it) += it->len;
7430 IT_CHARPOS (*it) += 1;
7432 else
7434 int prev_scan_dir = it->bidi_it.scan_dir;
7435 /* If this is a new paragraph, determine its base
7436 direction (a.k.a. its base embedding level). */
7437 if (it->bidi_it.new_paragraph)
7438 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7439 false);
7440 bidi_move_to_visually_next (&it->bidi_it);
7441 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7442 IT_CHARPOS (*it) = it->bidi_it.charpos;
7443 if (prev_scan_dir != it->bidi_it.scan_dir)
7445 /* As the scan direction was changed, we must
7446 re-compute the stop position for composition. */
7447 ptrdiff_t stop = it->end_charpos;
7448 if (it->bidi_it.scan_dir < 0)
7449 stop = -1;
7450 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7451 IT_BYTEPOS (*it), stop, Qnil);
7454 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7456 break;
7458 case GET_FROM_C_STRING:
7459 /* Current display element of IT is from a C string. */
7460 if (!it->bidi_p
7461 /* If the string position is beyond string's end, it means
7462 next_element_from_c_string is padding the string with
7463 blanks, in which case we bypass the bidi iterator,
7464 because it cannot deal with such virtual characters. */
7465 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7467 IT_BYTEPOS (*it) += it->len;
7468 IT_CHARPOS (*it) += 1;
7470 else
7472 bidi_move_to_visually_next (&it->bidi_it);
7473 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7474 IT_CHARPOS (*it) = it->bidi_it.charpos;
7476 break;
7478 case GET_FROM_DISPLAY_VECTOR:
7479 /* Current display element of IT is from a display table entry.
7480 Advance in the display table definition. Reset it to null if
7481 end reached, and continue with characters from buffers/
7482 strings. */
7483 ++it->current.dpvec_index;
7485 /* Restore face of the iterator to what they were before the
7486 display vector entry (these entries may contain faces). */
7487 it->face_id = it->saved_face_id;
7489 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7491 bool recheck_faces = it->ellipsis_p;
7493 if (it->s)
7494 it->method = GET_FROM_C_STRING;
7495 else if (STRINGP (it->string))
7496 it->method = GET_FROM_STRING;
7497 else
7499 it->method = GET_FROM_BUFFER;
7500 it->object = it->w->contents;
7503 it->dpvec = NULL;
7504 it->current.dpvec_index = -1;
7506 /* Skip over characters which were displayed via IT->dpvec. */
7507 if (it->dpvec_char_len < 0)
7508 reseat_at_next_visible_line_start (it, true);
7509 else if (it->dpvec_char_len > 0)
7511 it->len = it->dpvec_char_len;
7512 set_iterator_to_next (it, reseat_p);
7515 /* Maybe recheck faces after display vector. */
7516 if (recheck_faces)
7518 if (it->method == GET_FROM_STRING)
7519 it->stop_charpos = IT_STRING_CHARPOS (*it);
7520 else
7521 it->stop_charpos = IT_CHARPOS (*it);
7524 break;
7526 case GET_FROM_STRING:
7527 /* Current display element is a character from a Lisp string. */
7528 eassert (it->s == NULL && STRINGP (it->string));
7529 /* Don't advance past string end. These conditions are true
7530 when set_iterator_to_next is called at the end of
7531 get_next_display_element, in which case the Lisp string is
7532 already exhausted, and all we want is pop the iterator
7533 stack. */
7534 if (it->current.overlay_string_index >= 0)
7536 /* This is an overlay string, so there's no padding with
7537 spaces, and the number of characters in the string is
7538 where the string ends. */
7539 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7540 goto consider_string_end;
7542 else
7544 /* Not an overlay string. There could be padding, so test
7545 against it->end_charpos. */
7546 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7547 goto consider_string_end;
7549 if (it->cmp_it.id >= 0)
7551 /* We are delivering display elements from a composition.
7552 Update the string position past the grapheme cluster
7553 we've just processed. */
7554 if (! it->bidi_p)
7556 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7557 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7559 else
7561 int i;
7563 for (i = 0; i < it->cmp_it.nchars; i++)
7564 bidi_move_to_visually_next (&it->bidi_it);
7565 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7566 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7569 /* Did we exhaust all the grapheme clusters of this
7570 composition? */
7571 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7572 && (it->cmp_it.to < it->cmp_it.nglyphs))
7574 /* Not all the grapheme clusters were processed yet;
7575 advance to the next cluster. */
7576 it->cmp_it.from = it->cmp_it.to;
7578 else if ((it->bidi_p && it->cmp_it.reversed_p)
7579 && it->cmp_it.from > 0)
7581 /* Likewise: advance to the next cluster, but going in
7582 the reverse direction. */
7583 it->cmp_it.to = it->cmp_it.from;
7585 else
7587 /* This composition was fully processed; find the next
7588 candidate place for checking for composed
7589 characters. */
7590 /* Always limit string searches to the string length;
7591 any padding spaces are not part of the string, and
7592 there cannot be any compositions in that padding. */
7593 ptrdiff_t stop = SCHARS (it->string);
7595 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7596 stop = -1;
7597 else if (it->end_charpos < stop)
7599 /* Cf. PRECISION in reseat_to_string: we might be
7600 limited in how many of the string characters we
7601 need to deliver. */
7602 stop = it->end_charpos;
7604 composition_compute_stop_pos (&it->cmp_it,
7605 IT_STRING_CHARPOS (*it),
7606 IT_STRING_BYTEPOS (*it), stop,
7607 it->string);
7610 else
7612 if (!it->bidi_p
7613 /* If the string position is beyond string's end, it
7614 means next_element_from_string is padding the string
7615 with blanks, in which case we bypass the bidi
7616 iterator, because it cannot deal with such virtual
7617 characters. */
7618 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7620 IT_STRING_BYTEPOS (*it) += it->len;
7621 IT_STRING_CHARPOS (*it) += 1;
7623 else
7625 int prev_scan_dir = it->bidi_it.scan_dir;
7627 bidi_move_to_visually_next (&it->bidi_it);
7628 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7629 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7630 /* If the scan direction changes, we may need to update
7631 the place where to check for composed characters. */
7632 if (prev_scan_dir != it->bidi_it.scan_dir)
7634 ptrdiff_t stop = SCHARS (it->string);
7636 if (it->bidi_it.scan_dir < 0)
7637 stop = -1;
7638 else if (it->end_charpos < stop)
7639 stop = it->end_charpos;
7641 composition_compute_stop_pos (&it->cmp_it,
7642 IT_STRING_CHARPOS (*it),
7643 IT_STRING_BYTEPOS (*it), stop,
7644 it->string);
7649 consider_string_end:
7651 if (it->current.overlay_string_index >= 0)
7653 /* IT->string is an overlay string. Advance to the
7654 next, if there is one. */
7655 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7657 it->ellipsis_p = false;
7658 next_overlay_string (it);
7659 if (it->ellipsis_p)
7660 setup_for_ellipsis (it, 0);
7663 else
7665 /* IT->string is not an overlay string. If we reached
7666 its end, and there is something on IT->stack, proceed
7667 with what is on the stack. This can be either another
7668 string, this time an overlay string, or a buffer. */
7669 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7670 && it->sp > 0)
7672 pop_it (it);
7673 if (it->method == GET_FROM_STRING)
7674 goto consider_string_end;
7677 break;
7679 case GET_FROM_IMAGE:
7680 case GET_FROM_STRETCH:
7681 case GET_FROM_XWIDGET:
7683 /* The position etc with which we have to proceed are on
7684 the stack. The position may be at the end of a string,
7685 if the `display' property takes up the whole string. */
7686 eassert (it->sp > 0);
7687 pop_it (it);
7688 if (it->method == GET_FROM_STRING)
7689 goto consider_string_end;
7690 break;
7692 default:
7693 /* There are no other methods defined, so this should be a bug. */
7694 emacs_abort ();
7697 eassert (it->method != GET_FROM_STRING
7698 || (STRINGP (it->string)
7699 && IT_STRING_CHARPOS (*it) >= 0));
7702 /* Load IT's display element fields with information about the next
7703 display element which comes from a display table entry or from the
7704 result of translating a control character to one of the forms `^C'
7705 or `\003'.
7707 IT->dpvec holds the glyphs to return as characters.
7708 IT->saved_face_id holds the face id before the display vector--it
7709 is restored into IT->face_id in set_iterator_to_next. */
7711 static bool
7712 next_element_from_display_vector (struct it *it)
7714 Lisp_Object gc;
7715 int prev_face_id = it->face_id;
7716 int next_face_id;
7718 /* Precondition. */
7719 eassert (it->dpvec && it->current.dpvec_index >= 0);
7721 it->face_id = it->saved_face_id;
7723 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7724 That seemed totally bogus - so I changed it... */
7725 gc = it->dpvec[it->current.dpvec_index];
7727 if (GLYPH_CODE_P (gc))
7729 struct face *this_face, *prev_face, *next_face;
7731 it->c = GLYPH_CODE_CHAR (gc);
7732 it->len = CHAR_BYTES (it->c);
7734 /* The entry may contain a face id to use. Such a face id is
7735 the id of a Lisp face, not a realized face. A face id of
7736 zero means no face is specified. */
7737 if (it->dpvec_face_id >= 0)
7738 it->face_id = it->dpvec_face_id;
7739 else
7741 int lface_id = GLYPH_CODE_FACE (gc);
7742 if (lface_id > 0)
7743 it->face_id = merge_faces (it->f, Qt, lface_id,
7744 it->saved_face_id);
7747 /* Glyphs in the display vector could have the box face, so we
7748 need to set the related flags in the iterator, as
7749 appropriate. */
7750 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7751 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7753 /* Is this character the first character of a box-face run? */
7754 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7755 && (!prev_face
7756 || prev_face->box == FACE_NO_BOX));
7758 /* For the last character of the box-face run, we need to look
7759 either at the next glyph from the display vector, or at the
7760 face we saw before the display vector. */
7761 next_face_id = it->saved_face_id;
7762 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7764 if (it->dpvec_face_id >= 0)
7765 next_face_id = it->dpvec_face_id;
7766 else
7768 int lface_id =
7769 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7771 if (lface_id > 0)
7772 next_face_id = merge_faces (it->f, Qt, lface_id,
7773 it->saved_face_id);
7776 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7777 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7778 && (!next_face
7779 || next_face->box == FACE_NO_BOX));
7780 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7782 else
7783 /* Display table entry is invalid. Return a space. */
7784 it->c = ' ', it->len = 1;
7786 /* Don't change position and object of the iterator here. They are
7787 still the values of the character that had this display table
7788 entry or was translated, and that's what we want. */
7789 it->what = IT_CHARACTER;
7790 return true;
7793 /* Get the first element of string/buffer in the visual order, after
7794 being reseated to a new position in a string or a buffer. */
7795 static void
7796 get_visually_first_element (struct it *it)
7798 bool string_p = STRINGP (it->string) || it->s;
7799 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7800 ptrdiff_t bob = (string_p ? 0 : BEGV);
7802 if (STRINGP (it->string))
7804 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7805 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7807 else
7809 it->bidi_it.charpos = IT_CHARPOS (*it);
7810 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7813 if (it->bidi_it.charpos == eob)
7815 /* Nothing to do, but reset the FIRST_ELT flag, like
7816 bidi_paragraph_init does, because we are not going to
7817 call it. */
7818 it->bidi_it.first_elt = false;
7820 else if (it->bidi_it.charpos == bob
7821 || (!string_p
7822 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7823 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7825 /* If we are at the beginning of a line/string, we can produce
7826 the next element right away. */
7827 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7828 bidi_move_to_visually_next (&it->bidi_it);
7830 else
7832 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7834 /* We need to prime the bidi iterator starting at the line's or
7835 string's beginning, before we will be able to produce the
7836 next element. */
7837 if (string_p)
7838 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7839 else
7840 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7841 IT_BYTEPOS (*it), -1,
7842 &it->bidi_it.bytepos);
7843 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7846 /* Now return to buffer/string position where we were asked
7847 to get the next display element, and produce that. */
7848 bidi_move_to_visually_next (&it->bidi_it);
7850 while (it->bidi_it.bytepos != orig_bytepos
7851 && it->bidi_it.charpos < eob);
7854 /* Adjust IT's position information to where we ended up. */
7855 if (STRINGP (it->string))
7857 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7858 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7860 else
7862 IT_CHARPOS (*it) = it->bidi_it.charpos;
7863 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7866 if (STRINGP (it->string) || !it->s)
7868 ptrdiff_t stop, charpos, bytepos;
7870 if (STRINGP (it->string))
7872 eassert (!it->s);
7873 stop = SCHARS (it->string);
7874 if (stop > it->end_charpos)
7875 stop = it->end_charpos;
7876 charpos = IT_STRING_CHARPOS (*it);
7877 bytepos = IT_STRING_BYTEPOS (*it);
7879 else
7881 stop = it->end_charpos;
7882 charpos = IT_CHARPOS (*it);
7883 bytepos = IT_BYTEPOS (*it);
7885 if (it->bidi_it.scan_dir < 0)
7886 stop = -1;
7887 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7888 it->string);
7892 /* Load IT with the next display element from Lisp string IT->string.
7893 IT->current.string_pos is the current position within the string.
7894 If IT->current.overlay_string_index >= 0, the Lisp string is an
7895 overlay string. */
7897 static bool
7898 next_element_from_string (struct it *it)
7900 struct text_pos position;
7902 eassert (STRINGP (it->string));
7903 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7904 eassert (IT_STRING_CHARPOS (*it) >= 0);
7905 position = it->current.string_pos;
7907 /* With bidi reordering, the character to display might not be the
7908 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7909 that we were reseat()ed to a new string, whose paragraph
7910 direction is not known. */
7911 if (it->bidi_p && it->bidi_it.first_elt)
7913 get_visually_first_element (it);
7914 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7917 /* Time to check for invisible text? */
7918 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7920 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7922 if (!(!it->bidi_p
7923 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7924 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7926 /* With bidi non-linear iteration, we could find
7927 ourselves far beyond the last computed stop_charpos,
7928 with several other stop positions in between that we
7929 missed. Scan them all now, in buffer's logical
7930 order, until we find and handle the last stop_charpos
7931 that precedes our current position. */
7932 handle_stop_backwards (it, it->stop_charpos);
7933 return GET_NEXT_DISPLAY_ELEMENT (it);
7935 else
7937 if (it->bidi_p)
7939 /* Take note of the stop position we just moved
7940 across, for when we will move back across it. */
7941 it->prev_stop = it->stop_charpos;
7942 /* If we are at base paragraph embedding level, take
7943 note of the last stop position seen at this
7944 level. */
7945 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7946 it->base_level_stop = it->stop_charpos;
7948 handle_stop (it);
7950 /* Since a handler may have changed IT->method, we must
7951 recurse here. */
7952 return GET_NEXT_DISPLAY_ELEMENT (it);
7955 else if (it->bidi_p
7956 /* If we are before prev_stop, we may have overstepped
7957 on our way backwards a stop_pos, and if so, we need
7958 to handle that stop_pos. */
7959 && IT_STRING_CHARPOS (*it) < it->prev_stop
7960 /* We can sometimes back up for reasons that have nothing
7961 to do with bidi reordering. E.g., compositions. The
7962 code below is only needed when we are above the base
7963 embedding level, so test for that explicitly. */
7964 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7966 /* If we lost track of base_level_stop, we have no better
7967 place for handle_stop_backwards to start from than string
7968 beginning. This happens, e.g., when we were reseated to
7969 the previous screenful of text by vertical-motion. */
7970 if (it->base_level_stop <= 0
7971 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7972 it->base_level_stop = 0;
7973 handle_stop_backwards (it, it->base_level_stop);
7974 return GET_NEXT_DISPLAY_ELEMENT (it);
7978 if (it->current.overlay_string_index >= 0)
7980 /* Get the next character from an overlay string. In overlay
7981 strings, there is no field width or padding with spaces to
7982 do. */
7983 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7985 it->what = IT_EOB;
7986 return false;
7988 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7989 IT_STRING_BYTEPOS (*it),
7990 it->bidi_it.scan_dir < 0
7991 ? -1
7992 : SCHARS (it->string))
7993 && next_element_from_composition (it))
7995 return true;
7997 else if (STRING_MULTIBYTE (it->string))
7999 const unsigned char *s = (SDATA (it->string)
8000 + IT_STRING_BYTEPOS (*it));
8001 it->c = string_char_and_length (s, &it->len);
8003 else
8005 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8006 it->len = 1;
8009 else
8011 /* Get the next character from a Lisp string that is not an
8012 overlay string. Such strings come from the mode line, for
8013 example. We may have to pad with spaces, or truncate the
8014 string. See also next_element_from_c_string. */
8015 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8017 it->what = IT_EOB;
8018 return false;
8020 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8022 /* Pad with spaces. */
8023 it->c = ' ', it->len = 1;
8024 CHARPOS (position) = BYTEPOS (position) = -1;
8026 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8027 IT_STRING_BYTEPOS (*it),
8028 it->bidi_it.scan_dir < 0
8029 ? -1
8030 : it->string_nchars)
8031 && next_element_from_composition (it))
8033 return true;
8035 else if (STRING_MULTIBYTE (it->string))
8037 const unsigned char *s = (SDATA (it->string)
8038 + IT_STRING_BYTEPOS (*it));
8039 it->c = string_char_and_length (s, &it->len);
8041 else
8043 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8044 it->len = 1;
8048 /* Record what we have and where it came from. */
8049 it->what = IT_CHARACTER;
8050 it->object = it->string;
8051 it->position = position;
8052 return true;
8056 /* Load IT with next display element from C string IT->s.
8057 IT->string_nchars is the maximum number of characters to return
8058 from the string. IT->end_charpos may be greater than
8059 IT->string_nchars when this function is called, in which case we
8060 may have to return padding spaces. Value is false if end of string
8061 reached, including padding spaces. */
8063 static bool
8064 next_element_from_c_string (struct it *it)
8066 bool success_p = true;
8068 eassert (it->s);
8069 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8070 it->what = IT_CHARACTER;
8071 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8072 it->object = make_number (0);
8074 /* With bidi reordering, the character to display might not be the
8075 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8076 we were reseated to a new string, whose paragraph direction is
8077 not known. */
8078 if (it->bidi_p && it->bidi_it.first_elt)
8079 get_visually_first_element (it);
8081 /* IT's position can be greater than IT->string_nchars in case a
8082 field width or precision has been specified when the iterator was
8083 initialized. */
8084 if (IT_CHARPOS (*it) >= it->end_charpos)
8086 /* End of the game. */
8087 it->what = IT_EOB;
8088 success_p = false;
8090 else if (IT_CHARPOS (*it) >= it->string_nchars)
8092 /* Pad with spaces. */
8093 it->c = ' ', it->len = 1;
8094 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8096 else if (it->multibyte_p)
8097 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8098 else
8099 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8101 return success_p;
8105 /* Set up IT to return characters from an ellipsis, if appropriate.
8106 The definition of the ellipsis glyphs may come from a display table
8107 entry. This function fills IT with the first glyph from the
8108 ellipsis if an ellipsis is to be displayed. */
8110 static bool
8111 next_element_from_ellipsis (struct it *it)
8113 if (it->selective_display_ellipsis_p)
8114 setup_for_ellipsis (it, it->len);
8115 else
8117 /* The face at the current position may be different from the
8118 face we find after the invisible text. Remember what it
8119 was in IT->saved_face_id, and signal that it's there by
8120 setting face_before_selective_p. */
8121 it->saved_face_id = it->face_id;
8122 it->method = GET_FROM_BUFFER;
8123 it->object = it->w->contents;
8124 reseat_at_next_visible_line_start (it, true);
8125 it->face_before_selective_p = true;
8128 return GET_NEXT_DISPLAY_ELEMENT (it);
8132 /* Deliver an image display element. The iterator IT is already
8133 filled with image information (done in handle_display_prop). Value
8134 is always true. */
8137 static bool
8138 next_element_from_image (struct it *it)
8140 it->what = IT_IMAGE;
8141 return true;
8144 static bool
8145 next_element_from_xwidget (struct it *it)
8147 it->what = IT_XWIDGET;
8148 return true;
8152 /* Fill iterator IT with next display element from a stretch glyph
8153 property. IT->object is the value of the text property. Value is
8154 always true. */
8156 static bool
8157 next_element_from_stretch (struct it *it)
8159 it->what = IT_STRETCH;
8160 return true;
8163 /* Scan backwards from IT's current position until we find a stop
8164 position, or until BEGV. This is called when we find ourself
8165 before both the last known prev_stop and base_level_stop while
8166 reordering bidirectional text. */
8168 static void
8169 compute_stop_pos_backwards (struct it *it)
8171 const int SCAN_BACK_LIMIT = 1000;
8172 struct text_pos pos;
8173 struct display_pos save_current = it->current;
8174 struct text_pos save_position = it->position;
8175 ptrdiff_t charpos = IT_CHARPOS (*it);
8176 ptrdiff_t where_we_are = charpos;
8177 ptrdiff_t save_stop_pos = it->stop_charpos;
8178 ptrdiff_t save_end_pos = it->end_charpos;
8180 eassert (NILP (it->string) && !it->s);
8181 eassert (it->bidi_p);
8182 it->bidi_p = false;
8185 it->end_charpos = min (charpos + 1, ZV);
8186 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8187 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8188 reseat_1 (it, pos, false);
8189 compute_stop_pos (it);
8190 /* We must advance forward, right? */
8191 if (it->stop_charpos <= charpos)
8192 emacs_abort ();
8194 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8196 if (it->stop_charpos <= where_we_are)
8197 it->prev_stop = it->stop_charpos;
8198 else
8199 it->prev_stop = BEGV;
8200 it->bidi_p = true;
8201 it->current = save_current;
8202 it->position = save_position;
8203 it->stop_charpos = save_stop_pos;
8204 it->end_charpos = save_end_pos;
8207 /* Scan forward from CHARPOS in the current buffer/string, until we
8208 find a stop position > current IT's position. Then handle the stop
8209 position before that. This is called when we bump into a stop
8210 position while reordering bidirectional text. CHARPOS should be
8211 the last previously processed stop_pos (or BEGV/0, if none were
8212 processed yet) whose position is less that IT's current
8213 position. */
8215 static void
8216 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8218 bool bufp = !STRINGP (it->string);
8219 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8220 struct display_pos save_current = it->current;
8221 struct text_pos save_position = it->position;
8222 struct text_pos pos1;
8223 ptrdiff_t next_stop;
8225 /* Scan in strict logical order. */
8226 eassert (it->bidi_p);
8227 it->bidi_p = false;
8230 it->prev_stop = charpos;
8231 if (bufp)
8233 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8234 reseat_1 (it, pos1, false);
8236 else
8237 it->current.string_pos = string_pos (charpos, it->string);
8238 compute_stop_pos (it);
8239 /* We must advance forward, right? */
8240 if (it->stop_charpos <= it->prev_stop)
8241 emacs_abort ();
8242 charpos = it->stop_charpos;
8244 while (charpos <= where_we_are);
8246 it->bidi_p = true;
8247 it->current = save_current;
8248 it->position = save_position;
8249 next_stop = it->stop_charpos;
8250 it->stop_charpos = it->prev_stop;
8251 handle_stop (it);
8252 it->stop_charpos = next_stop;
8255 /* Load IT with the next display element from current_buffer. Value
8256 is false if end of buffer reached. IT->stop_charpos is the next
8257 position at which to stop and check for text properties or buffer
8258 end. */
8260 static bool
8261 next_element_from_buffer (struct it *it)
8263 bool success_p = true;
8265 eassert (IT_CHARPOS (*it) >= BEGV);
8266 eassert (NILP (it->string) && !it->s);
8267 eassert (!it->bidi_p
8268 || (EQ (it->bidi_it.string.lstring, Qnil)
8269 && it->bidi_it.string.s == NULL));
8271 /* With bidi reordering, the character to display might not be the
8272 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8273 we were reseat()ed to a new buffer position, which is potentially
8274 a different paragraph. */
8275 if (it->bidi_p && it->bidi_it.first_elt)
8277 get_visually_first_element (it);
8278 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8281 if (IT_CHARPOS (*it) >= it->stop_charpos)
8283 if (IT_CHARPOS (*it) >= it->end_charpos)
8285 bool overlay_strings_follow_p;
8287 /* End of the game, except when overlay strings follow that
8288 haven't been returned yet. */
8289 if (it->overlay_strings_at_end_processed_p)
8290 overlay_strings_follow_p = false;
8291 else
8293 it->overlay_strings_at_end_processed_p = true;
8294 overlay_strings_follow_p = get_overlay_strings (it, 0);
8297 if (overlay_strings_follow_p)
8298 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8299 else
8301 it->what = IT_EOB;
8302 it->position = it->current.pos;
8303 success_p = false;
8306 else if (!(!it->bidi_p
8307 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8308 || IT_CHARPOS (*it) == it->stop_charpos))
8310 /* With bidi non-linear iteration, we could find ourselves
8311 far beyond the last computed stop_charpos, with several
8312 other stop positions in between that we missed. Scan
8313 them all now, in buffer's logical order, until we find
8314 and handle the last stop_charpos that precedes our
8315 current position. */
8316 handle_stop_backwards (it, it->stop_charpos);
8317 it->ignore_overlay_strings_at_pos_p = false;
8318 return GET_NEXT_DISPLAY_ELEMENT (it);
8320 else
8322 if (it->bidi_p)
8324 /* Take note of the stop position we just moved across,
8325 for when we will move back across it. */
8326 it->prev_stop = it->stop_charpos;
8327 /* If we are at base paragraph embedding level, take
8328 note of the last stop position seen at this
8329 level. */
8330 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8331 it->base_level_stop = it->stop_charpos;
8333 handle_stop (it);
8334 it->ignore_overlay_strings_at_pos_p = false;
8335 return GET_NEXT_DISPLAY_ELEMENT (it);
8338 else if (it->bidi_p
8339 /* If we are before prev_stop, we may have overstepped on
8340 our way backwards a stop_pos, and if so, we need to
8341 handle that stop_pos. */
8342 && IT_CHARPOS (*it) < it->prev_stop
8343 /* We can sometimes back up for reasons that have nothing
8344 to do with bidi reordering. E.g., compositions. The
8345 code below is only needed when we are above the base
8346 embedding level, so test for that explicitly. */
8347 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8349 if (it->base_level_stop <= 0
8350 || IT_CHARPOS (*it) < it->base_level_stop)
8352 /* If we lost track of base_level_stop, we need to find
8353 prev_stop by looking backwards. This happens, e.g., when
8354 we were reseated to the previous screenful of text by
8355 vertical-motion. */
8356 it->base_level_stop = BEGV;
8357 compute_stop_pos_backwards (it);
8358 handle_stop_backwards (it, it->prev_stop);
8360 else
8361 handle_stop_backwards (it, it->base_level_stop);
8362 it->ignore_overlay_strings_at_pos_p = false;
8363 return GET_NEXT_DISPLAY_ELEMENT (it);
8365 else
8367 /* No face changes, overlays etc. in sight, so just return a
8368 character from current_buffer. */
8369 unsigned char *p;
8370 ptrdiff_t stop;
8372 /* We moved to the next buffer position, so any info about
8373 previously seen overlays is no longer valid. */
8374 it->ignore_overlay_strings_at_pos_p = false;
8376 /* Maybe run the redisplay end trigger hook. Performance note:
8377 This doesn't seem to cost measurable time. */
8378 if (it->redisplay_end_trigger_charpos
8379 && it->glyph_row
8380 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8381 run_redisplay_end_trigger_hook (it);
8383 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8384 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8385 stop)
8386 && next_element_from_composition (it))
8388 return true;
8391 /* Get the next character, maybe multibyte. */
8392 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8393 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8394 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8395 else
8396 it->c = *p, it->len = 1;
8398 /* Record what we have and where it came from. */
8399 it->what = IT_CHARACTER;
8400 it->object = it->w->contents;
8401 it->position = it->current.pos;
8403 /* Normally we return the character found above, except when we
8404 really want to return an ellipsis for selective display. */
8405 if (it->selective)
8407 if (it->c == '\n')
8409 /* A value of selective > 0 means hide lines indented more
8410 than that number of columns. */
8411 if (it->selective > 0
8412 && IT_CHARPOS (*it) + 1 < ZV
8413 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8414 IT_BYTEPOS (*it) + 1,
8415 it->selective))
8417 success_p = next_element_from_ellipsis (it);
8418 it->dpvec_char_len = -1;
8421 else if (it->c == '\r' && it->selective == -1)
8423 /* A value of selective == -1 means that everything from the
8424 CR to the end of the line is invisible, with maybe an
8425 ellipsis displayed for it. */
8426 success_p = next_element_from_ellipsis (it);
8427 it->dpvec_char_len = -1;
8432 /* Value is false if end of buffer reached. */
8433 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8434 return success_p;
8438 /* Run the redisplay end trigger hook for IT. */
8440 static void
8441 run_redisplay_end_trigger_hook (struct it *it)
8443 /* IT->glyph_row should be non-null, i.e. we should be actually
8444 displaying something, or otherwise we should not run the hook. */
8445 eassert (it->glyph_row);
8447 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8448 it->redisplay_end_trigger_charpos = 0;
8450 /* Since we are *trying* to run these functions, don't try to run
8451 them again, even if they get an error. */
8452 wset_redisplay_end_trigger (it->w, Qnil);
8453 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8454 make_number (charpos));
8456 /* Notice if it changed the face of the character we are on. */
8457 handle_face_prop (it);
8461 /* Deliver a composition display element. Unlike the other
8462 next_element_from_XXX, this function is not registered in the array
8463 get_next_element[]. It is called from next_element_from_buffer and
8464 next_element_from_string when necessary. */
8466 static bool
8467 next_element_from_composition (struct it *it)
8469 it->what = IT_COMPOSITION;
8470 it->len = it->cmp_it.nbytes;
8471 if (STRINGP (it->string))
8473 if (it->c < 0)
8475 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8476 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8477 return false;
8479 it->position = it->current.string_pos;
8480 it->object = it->string;
8481 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8482 IT_STRING_BYTEPOS (*it), it->string);
8484 else
8486 if (it->c < 0)
8488 IT_CHARPOS (*it) += it->cmp_it.nchars;
8489 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8490 if (it->bidi_p)
8492 if (it->bidi_it.new_paragraph)
8493 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8494 false);
8495 /* Resync the bidi iterator with IT's new position.
8496 FIXME: this doesn't support bidirectional text. */
8497 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8498 bidi_move_to_visually_next (&it->bidi_it);
8500 return false;
8502 it->position = it->current.pos;
8503 it->object = it->w->contents;
8504 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8505 IT_BYTEPOS (*it), Qnil);
8507 return true;
8512 /***********************************************************************
8513 Moving an iterator without producing glyphs
8514 ***********************************************************************/
8516 /* Check if iterator is at a position corresponding to a valid buffer
8517 position after some move_it_ call. */
8519 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8520 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8523 /* Move iterator IT to a specified buffer or X position within one
8524 line on the display without producing glyphs.
8526 OP should be a bit mask including some or all of these bits:
8527 MOVE_TO_X: Stop upon reaching x-position TO_X.
8528 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8529 Regardless of OP's value, stop upon reaching the end of the display line.
8531 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8532 This means, in particular, that TO_X includes window's horizontal
8533 scroll amount.
8535 The return value has several possible values that
8536 say what condition caused the scan to stop:
8538 MOVE_POS_MATCH_OR_ZV
8539 - when TO_POS or ZV was reached.
8541 MOVE_X_REACHED
8542 -when TO_X was reached before TO_POS or ZV were reached.
8544 MOVE_LINE_CONTINUED
8545 - when we reached the end of the display area and the line must
8546 be continued.
8548 MOVE_LINE_TRUNCATED
8549 - when we reached the end of the display area and the line is
8550 truncated.
8552 MOVE_NEWLINE_OR_CR
8553 - when we stopped at a line end, i.e. a newline or a CR and selective
8554 display is on. */
8556 static enum move_it_result
8557 move_it_in_display_line_to (struct it *it,
8558 ptrdiff_t to_charpos, int to_x,
8559 enum move_operation_enum op)
8561 enum move_it_result result = MOVE_UNDEFINED;
8562 struct glyph_row *saved_glyph_row;
8563 struct it wrap_it, atpos_it, atx_it, ppos_it;
8564 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8565 void *ppos_data = NULL;
8566 bool may_wrap = false;
8567 enum it_method prev_method = it->method;
8568 ptrdiff_t closest_pos UNINIT;
8569 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8570 bool saw_smaller_pos = prev_pos < to_charpos;
8572 /* Don't produce glyphs in produce_glyphs. */
8573 saved_glyph_row = it->glyph_row;
8574 it->glyph_row = NULL;
8576 /* Use wrap_it to save a copy of IT wherever a word wrap could
8577 occur. Use atpos_it to save a copy of IT at the desired buffer
8578 position, if found, so that we can scan ahead and check if the
8579 word later overshoots the window edge. Use atx_it similarly, for
8580 pixel positions. */
8581 wrap_it.sp = -1;
8582 atpos_it.sp = -1;
8583 atx_it.sp = -1;
8585 /* Use ppos_it under bidi reordering to save a copy of IT for the
8586 initial position. We restore that position in IT when we have
8587 scanned the entire display line without finding a match for
8588 TO_CHARPOS and all the character positions are greater than
8589 TO_CHARPOS. We then restart the scan from the initial position,
8590 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8591 the closest to TO_CHARPOS. */
8592 if (it->bidi_p)
8594 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8596 SAVE_IT (ppos_it, *it, ppos_data);
8597 closest_pos = IT_CHARPOS (*it);
8599 else
8600 closest_pos = ZV;
8603 #define BUFFER_POS_REACHED_P() \
8604 ((op & MOVE_TO_POS) != 0 \
8605 && BUFFERP (it->object) \
8606 && (IT_CHARPOS (*it) == to_charpos \
8607 || ((!it->bidi_p \
8608 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8609 && IT_CHARPOS (*it) > to_charpos) \
8610 || (it->what == IT_COMPOSITION \
8611 && ((IT_CHARPOS (*it) > to_charpos \
8612 && to_charpos >= it->cmp_it.charpos) \
8613 || (IT_CHARPOS (*it) < to_charpos \
8614 && to_charpos <= it->cmp_it.charpos)))) \
8615 && (it->method == GET_FROM_BUFFER \
8616 || (it->method == GET_FROM_DISPLAY_VECTOR \
8617 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8619 /* If there's a line-/wrap-prefix, handle it. */
8620 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8621 handle_line_prefix (it);
8623 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8624 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8626 while (true)
8628 int x, i, ascent = 0, descent = 0;
8630 /* Utility macro to reset an iterator with x, ascent, and descent. */
8631 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8632 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8633 (IT)->max_descent = descent)
8635 /* Stop if we move beyond TO_CHARPOS (after an image or a
8636 display string or stretch glyph). */
8637 if ((op & MOVE_TO_POS) != 0
8638 && BUFFERP (it->object)
8639 && it->method == GET_FROM_BUFFER
8640 && (((!it->bidi_p
8641 /* When the iterator is at base embedding level, we
8642 are guaranteed that characters are delivered for
8643 display in strictly increasing order of their
8644 buffer positions. */
8645 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8646 && IT_CHARPOS (*it) > to_charpos)
8647 || (it->bidi_p
8648 && (prev_method == GET_FROM_IMAGE
8649 || prev_method == GET_FROM_STRETCH
8650 || prev_method == GET_FROM_STRING)
8651 /* Passed TO_CHARPOS from left to right. */
8652 && ((prev_pos < to_charpos
8653 && IT_CHARPOS (*it) > to_charpos)
8654 /* Passed TO_CHARPOS from right to left. */
8655 || (prev_pos > to_charpos
8656 && IT_CHARPOS (*it) < to_charpos)))))
8658 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8660 result = MOVE_POS_MATCH_OR_ZV;
8661 break;
8663 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8664 /* If wrap_it is valid, the current position might be in a
8665 word that is wrapped. So, save the iterator in
8666 atpos_it and continue to see if wrapping happens. */
8667 SAVE_IT (atpos_it, *it, atpos_data);
8670 /* Stop when ZV reached.
8671 We used to stop here when TO_CHARPOS reached as well, but that is
8672 too soon if this glyph does not fit on this line. So we handle it
8673 explicitly below. */
8674 if (!get_next_display_element (it))
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8680 if (it->line_wrap == TRUNCATE)
8682 if (BUFFER_POS_REACHED_P ())
8684 result = MOVE_POS_MATCH_OR_ZV;
8685 break;
8688 else
8690 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8692 if (IT_DISPLAYING_WHITESPACE (it))
8693 may_wrap = true;
8694 else if (may_wrap)
8696 /* We have reached a glyph that follows one or more
8697 whitespace characters. If the position is
8698 already found, we are done. */
8699 if (atpos_it.sp >= 0)
8701 RESTORE_IT (it, &atpos_it, atpos_data);
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 goto done;
8705 if (atx_it.sp >= 0)
8707 RESTORE_IT (it, &atx_it, atx_data);
8708 result = MOVE_X_REACHED;
8709 goto done;
8711 /* Otherwise, we can wrap here. */
8712 SAVE_IT (wrap_it, *it, wrap_data);
8713 may_wrap = false;
8718 /* Remember the line height for the current line, in case
8719 the next element doesn't fit on the line. */
8720 ascent = it->max_ascent;
8721 descent = it->max_descent;
8723 /* The call to produce_glyphs will get the metrics of the
8724 display element IT is loaded with. Record the x-position
8725 before this display element, in case it doesn't fit on the
8726 line. */
8727 x = it->current_x;
8729 PRODUCE_GLYPHS (it);
8731 if (it->area != TEXT_AREA)
8733 prev_method = it->method;
8734 if (it->method == GET_FROM_BUFFER)
8735 prev_pos = IT_CHARPOS (*it);
8736 set_iterator_to_next (it, true);
8737 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8738 SET_TEXT_POS (this_line_min_pos,
8739 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8740 if (it->bidi_p
8741 && (op & MOVE_TO_POS)
8742 && IT_CHARPOS (*it) > to_charpos
8743 && IT_CHARPOS (*it) < closest_pos)
8744 closest_pos = IT_CHARPOS (*it);
8745 continue;
8748 /* The number of glyphs we get back in IT->nglyphs will normally
8749 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8750 character on a terminal frame, or (iii) a line end. For the
8751 second case, IT->nglyphs - 1 padding glyphs will be present.
8752 (On X frames, there is only one glyph produced for a
8753 composite character.)
8755 The behavior implemented below means, for continuation lines,
8756 that as many spaces of a TAB as fit on the current line are
8757 displayed there. For terminal frames, as many glyphs of a
8758 multi-glyph character are displayed in the current line, too.
8759 This is what the old redisplay code did, and we keep it that
8760 way. Under X, the whole shape of a complex character must
8761 fit on the line or it will be completely displayed in the
8762 next line.
8764 Note that both for tabs and padding glyphs, all glyphs have
8765 the same width. */
8766 if (it->nglyphs)
8768 /* More than one glyph or glyph doesn't fit on line. All
8769 glyphs have the same width. */
8770 int single_glyph_width = it->pixel_width / it->nglyphs;
8771 int new_x;
8772 int x_before_this_char = x;
8773 int hpos_before_this_char = it->hpos;
8775 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8777 new_x = x + single_glyph_width;
8779 /* We want to leave anything reaching TO_X to the caller. */
8780 if ((op & MOVE_TO_X) && new_x > to_x)
8782 if (BUFFER_POS_REACHED_P ())
8784 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8785 goto buffer_pos_reached;
8786 if (atpos_it.sp < 0)
8788 SAVE_IT (atpos_it, *it, atpos_data);
8789 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8792 else
8794 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8796 it->current_x = x;
8797 result = MOVE_X_REACHED;
8798 break;
8800 if (atx_it.sp < 0)
8802 SAVE_IT (atx_it, *it, atx_data);
8803 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8808 if (/* Lines are continued. */
8809 it->line_wrap != TRUNCATE
8810 && (/* And glyph doesn't fit on the line. */
8811 new_x > it->last_visible_x
8812 /* Or it fits exactly and we're on a window
8813 system frame. */
8814 || (new_x == it->last_visible_x
8815 && FRAME_WINDOW_P (it->f)
8816 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8817 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8818 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8820 bool moved_forward = false;
8822 if (/* IT->hpos == 0 means the very first glyph
8823 doesn't fit on the line, e.g. a wide image. */
8824 it->hpos == 0
8825 || (new_x == it->last_visible_x
8826 && FRAME_WINDOW_P (it->f)))
8828 ++it->hpos;
8829 it->current_x = new_x;
8831 /* The character's last glyph just barely fits
8832 in this row. */
8833 if (i == it->nglyphs - 1)
8835 /* If this is the destination position,
8836 return a position *before* it in this row,
8837 now that we know it fits in this row. */
8838 if (BUFFER_POS_REACHED_P ())
8840 bool can_wrap = true;
8842 /* If we are at a whitespace character
8843 that barely fits on this screen line,
8844 but the next character is also
8845 whitespace, we cannot wrap here. */
8846 if (it->line_wrap == WORD_WRAP
8847 && wrap_it.sp >= 0
8848 && may_wrap
8849 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8851 struct it tem_it;
8852 void *tem_data = NULL;
8854 SAVE_IT (tem_it, *it, tem_data);
8855 set_iterator_to_next (it, true);
8856 if (get_next_display_element (it)
8857 && IT_DISPLAYING_WHITESPACE (it))
8858 can_wrap = false;
8859 RESTORE_IT (it, &tem_it, tem_data);
8861 if (it->line_wrap != WORD_WRAP
8862 || wrap_it.sp < 0
8863 /* If we've just found whitespace
8864 where we can wrap, effectively
8865 ignore the previous wrap point --
8866 it is no longer relevant, but we
8867 won't have an opportunity to
8868 update it, since we've reached
8869 the edge of this screen line. */
8870 || (may_wrap && can_wrap
8871 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8873 it->hpos = hpos_before_this_char;
8874 it->current_x = x_before_this_char;
8875 result = MOVE_POS_MATCH_OR_ZV;
8876 break;
8878 if (it->line_wrap == WORD_WRAP
8879 && atpos_it.sp < 0)
8881 SAVE_IT (atpos_it, *it, atpos_data);
8882 atpos_it.current_x = x_before_this_char;
8883 atpos_it.hpos = hpos_before_this_char;
8887 prev_method = it->method;
8888 if (it->method == GET_FROM_BUFFER)
8889 prev_pos = IT_CHARPOS (*it);
8890 set_iterator_to_next (it, true);
8891 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8892 SET_TEXT_POS (this_line_min_pos,
8893 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8894 /* On graphical terminals, newlines may
8895 "overflow" into the fringe if
8896 overflow-newline-into-fringe is non-nil.
8897 On text terminals, and on graphical
8898 terminals with no right margin, newlines
8899 may overflow into the last glyph on the
8900 display line.*/
8901 if (!FRAME_WINDOW_P (it->f)
8902 || ((it->bidi_p
8903 && it->bidi_it.paragraph_dir == R2L)
8904 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8905 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8906 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8908 if (!get_next_display_element (it))
8910 result = MOVE_POS_MATCH_OR_ZV;
8911 break;
8913 moved_forward = true;
8914 if (BUFFER_POS_REACHED_P ())
8916 if (ITERATOR_AT_END_OF_LINE_P (it))
8917 result = MOVE_POS_MATCH_OR_ZV;
8918 else
8919 result = MOVE_LINE_CONTINUED;
8920 break;
8922 if (ITERATOR_AT_END_OF_LINE_P (it)
8923 && (it->line_wrap != WORD_WRAP
8924 || wrap_it.sp < 0
8925 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8927 result = MOVE_NEWLINE_OR_CR;
8928 break;
8933 else
8934 IT_RESET_X_ASCENT_DESCENT (it);
8936 /* If the screen line ends with whitespace, and we
8937 are under word-wrap, don't use wrap_it: it is no
8938 longer relevant, but we won't have an opportunity
8939 to update it, since we are done with this screen
8940 line. */
8941 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8942 /* If the character after the one which set the
8943 may_wrap flag is also whitespace, we can't
8944 wrap here, since the screen line cannot be
8945 wrapped in the middle of whitespace.
8946 Therefore, wrap_it _is_ relevant in that
8947 case. */
8948 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8950 /* If we've found TO_X, go back there, as we now
8951 know the last word fits on this screen line. */
8952 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8953 && atx_it.sp >= 0)
8955 RESTORE_IT (it, &atx_it, atx_data);
8956 atpos_it.sp = -1;
8957 atx_it.sp = -1;
8958 result = MOVE_X_REACHED;
8959 break;
8962 else if (wrap_it.sp >= 0)
8964 RESTORE_IT (it, &wrap_it, wrap_data);
8965 atpos_it.sp = -1;
8966 atx_it.sp = -1;
8969 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8970 IT_CHARPOS (*it)));
8971 result = MOVE_LINE_CONTINUED;
8972 break;
8975 if (BUFFER_POS_REACHED_P ())
8977 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8978 goto buffer_pos_reached;
8979 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8981 SAVE_IT (atpos_it, *it, atpos_data);
8982 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8986 if (new_x > it->first_visible_x)
8988 /* Glyph is visible. Increment number of glyphs that
8989 would be displayed. */
8990 ++it->hpos;
8994 if (result != MOVE_UNDEFINED)
8995 break;
8997 else if (BUFFER_POS_REACHED_P ())
8999 buffer_pos_reached:
9000 IT_RESET_X_ASCENT_DESCENT (it);
9001 result = MOVE_POS_MATCH_OR_ZV;
9002 break;
9004 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9006 /* Stop when TO_X specified and reached. This check is
9007 necessary here because of lines consisting of a line end,
9008 only. The line end will not produce any glyphs and we
9009 would never get MOVE_X_REACHED. */
9010 eassert (it->nglyphs == 0);
9011 result = MOVE_X_REACHED;
9012 break;
9015 /* Is this a line end? If yes, we're done. */
9016 if (ITERATOR_AT_END_OF_LINE_P (it))
9018 /* If we are past TO_CHARPOS, but never saw any character
9019 positions smaller than TO_CHARPOS, return
9020 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9021 did. */
9022 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9024 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9026 if (closest_pos < ZV)
9028 RESTORE_IT (it, &ppos_it, ppos_data);
9029 /* Don't recurse if closest_pos is equal to
9030 to_charpos, since we have just tried that. */
9031 if (closest_pos != to_charpos)
9032 move_it_in_display_line_to (it, closest_pos, -1,
9033 MOVE_TO_POS);
9034 result = MOVE_POS_MATCH_OR_ZV;
9036 else
9037 goto buffer_pos_reached;
9039 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9040 && IT_CHARPOS (*it) > to_charpos)
9041 goto buffer_pos_reached;
9042 else
9043 result = MOVE_NEWLINE_OR_CR;
9045 else
9046 result = MOVE_NEWLINE_OR_CR;
9047 /* If we've processed the newline, make sure this flag is
9048 reset, as it must only be set when the newline itself is
9049 processed. */
9050 if (result == MOVE_NEWLINE_OR_CR)
9051 it->constrain_row_ascent_descent_p = false;
9052 break;
9055 prev_method = it->method;
9056 if (it->method == GET_FROM_BUFFER)
9057 prev_pos = IT_CHARPOS (*it);
9058 /* The current display element has been consumed. Advance
9059 to the next. */
9060 set_iterator_to_next (it, true);
9061 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9062 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9063 if (IT_CHARPOS (*it) < to_charpos)
9064 saw_smaller_pos = true;
9065 if (it->bidi_p
9066 && (op & MOVE_TO_POS)
9067 && IT_CHARPOS (*it) >= to_charpos
9068 && IT_CHARPOS (*it) < closest_pos)
9069 closest_pos = IT_CHARPOS (*it);
9071 /* Stop if lines are truncated and IT's current x-position is
9072 past the right edge of the window now. */
9073 if (it->line_wrap == TRUNCATE
9074 && it->current_x >= it->last_visible_x)
9076 if (!FRAME_WINDOW_P (it->f)
9077 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9078 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9079 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9080 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9082 bool at_eob_p = false;
9084 if ((at_eob_p = !get_next_display_element (it))
9085 || BUFFER_POS_REACHED_P ()
9086 /* If we are past TO_CHARPOS, but never saw any
9087 character positions smaller than TO_CHARPOS,
9088 return MOVE_POS_MATCH_OR_ZV, like the
9089 unidirectional display did. */
9090 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9091 && !saw_smaller_pos
9092 && IT_CHARPOS (*it) > to_charpos))
9094 if (it->bidi_p
9095 && !BUFFER_POS_REACHED_P ()
9096 && !at_eob_p && closest_pos < ZV)
9098 RESTORE_IT (it, &ppos_it, ppos_data);
9099 if (closest_pos != to_charpos)
9100 move_it_in_display_line_to (it, closest_pos, -1,
9101 MOVE_TO_POS);
9103 result = MOVE_POS_MATCH_OR_ZV;
9104 break;
9106 if (ITERATOR_AT_END_OF_LINE_P (it))
9108 result = MOVE_NEWLINE_OR_CR;
9109 break;
9112 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9113 && !saw_smaller_pos
9114 && IT_CHARPOS (*it) > to_charpos)
9116 if (closest_pos < ZV)
9118 RESTORE_IT (it, &ppos_it, ppos_data);
9119 if (closest_pos != to_charpos)
9120 move_it_in_display_line_to (it, closest_pos, -1,
9121 MOVE_TO_POS);
9123 result = MOVE_POS_MATCH_OR_ZV;
9124 break;
9126 result = MOVE_LINE_TRUNCATED;
9127 break;
9129 #undef IT_RESET_X_ASCENT_DESCENT
9132 #undef BUFFER_POS_REACHED_P
9134 /* If we scanned beyond TO_POS, restore the saved iterator either to
9135 the wrap point (if found), or to atpos/atx location. We decide which
9136 data to use to restore the saved iterator state by their X coordinates,
9137 since buffer positions might increase non-monotonically with screen
9138 coordinates due to bidi reordering. */
9139 if (result == MOVE_LINE_CONTINUED
9140 && it->line_wrap == WORD_WRAP
9141 && wrap_it.sp >= 0
9142 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9143 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9144 RESTORE_IT (it, &wrap_it, wrap_data);
9145 else if (atpos_it.sp >= 0)
9146 RESTORE_IT (it, &atpos_it, atpos_data);
9147 else if (atx_it.sp >= 0)
9148 RESTORE_IT (it, &atx_it, atx_data);
9150 done:
9152 if (atpos_data)
9153 bidi_unshelve_cache (atpos_data, true);
9154 if (atx_data)
9155 bidi_unshelve_cache (atx_data, true);
9156 if (wrap_data)
9157 bidi_unshelve_cache (wrap_data, true);
9158 if (ppos_data)
9159 bidi_unshelve_cache (ppos_data, true);
9161 /* Restore the iterator settings altered at the beginning of this
9162 function. */
9163 it->glyph_row = saved_glyph_row;
9164 return result;
9167 /* For external use. */
9168 void
9169 move_it_in_display_line (struct it *it,
9170 ptrdiff_t to_charpos, int to_x,
9171 enum move_operation_enum op)
9173 if (it->line_wrap == WORD_WRAP
9174 && (op & MOVE_TO_X))
9176 struct it save_it;
9177 void *save_data = NULL;
9178 int skip;
9180 SAVE_IT (save_it, *it, save_data);
9181 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9182 /* When word-wrap is on, TO_X may lie past the end
9183 of a wrapped line. Then it->current is the
9184 character on the next line, so backtrack to the
9185 space before the wrap point. */
9186 if (skip == MOVE_LINE_CONTINUED)
9188 int prev_x = max (it->current_x - 1, 0);
9189 RESTORE_IT (it, &save_it, save_data);
9190 move_it_in_display_line_to
9191 (it, -1, prev_x, MOVE_TO_X);
9193 else
9194 bidi_unshelve_cache (save_data, true);
9196 else
9197 move_it_in_display_line_to (it, to_charpos, to_x, op);
9201 /* Move IT forward until it satisfies one or more of the criteria in
9202 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9204 OP is a bit-mask that specifies where to stop, and in particular,
9205 which of those four position arguments makes a difference. See the
9206 description of enum move_operation_enum.
9208 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9209 screen line, this function will set IT to the next position that is
9210 displayed to the right of TO_CHARPOS on the screen.
9212 Return the maximum pixel length of any line scanned but never more
9213 than it.last_visible_x. */
9216 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9218 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9219 int line_height, line_start_x = 0, reached = 0;
9220 int max_current_x = 0;
9221 void *backup_data = NULL;
9223 for (;;)
9225 if (op & MOVE_TO_VPOS)
9227 /* If no TO_CHARPOS and no TO_X specified, stop at the
9228 start of the line TO_VPOS. */
9229 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9231 if (it->vpos == to_vpos)
9233 reached = 1;
9234 break;
9236 else
9237 skip = move_it_in_display_line_to (it, -1, -1, 0);
9239 else
9241 /* TO_VPOS >= 0 means stop at TO_X in the line at
9242 TO_VPOS, or at TO_POS, whichever comes first. */
9243 if (it->vpos == to_vpos)
9245 reached = 2;
9246 break;
9249 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9251 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9253 reached = 3;
9254 break;
9256 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9258 /* We have reached TO_X but not in the line we want. */
9259 skip = move_it_in_display_line_to (it, to_charpos,
9260 -1, MOVE_TO_POS);
9261 if (skip == MOVE_POS_MATCH_OR_ZV)
9263 reached = 4;
9264 break;
9269 else if (op & MOVE_TO_Y)
9271 struct it it_backup;
9273 if (it->line_wrap == WORD_WRAP)
9274 SAVE_IT (it_backup, *it, backup_data);
9276 /* TO_Y specified means stop at TO_X in the line containing
9277 TO_Y---or at TO_CHARPOS if this is reached first. The
9278 problem is that we can't really tell whether the line
9279 contains TO_Y before we have completely scanned it, and
9280 this may skip past TO_X. What we do is to first scan to
9281 TO_X.
9283 If TO_X is not specified, use a TO_X of zero. The reason
9284 is to make the outcome of this function more predictable.
9285 If we didn't use TO_X == 0, we would stop at the end of
9286 the line which is probably not what a caller would expect
9287 to happen. */
9288 skip = move_it_in_display_line_to
9289 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9290 (MOVE_TO_X | (op & MOVE_TO_POS)));
9292 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9293 if (skip == MOVE_POS_MATCH_OR_ZV)
9294 reached = 5;
9295 else if (skip == MOVE_X_REACHED)
9297 /* If TO_X was reached, we want to know whether TO_Y is
9298 in the line. We know this is the case if the already
9299 scanned glyphs make the line tall enough. Otherwise,
9300 we must check by scanning the rest of the line. */
9301 line_height = it->max_ascent + it->max_descent;
9302 if (to_y >= it->current_y
9303 && to_y < it->current_y + line_height)
9305 reached = 6;
9306 break;
9308 SAVE_IT (it_backup, *it, backup_data);
9309 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9310 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9311 op & MOVE_TO_POS);
9312 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9313 line_height = it->max_ascent + it->max_descent;
9314 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9316 if (to_y >= it->current_y
9317 && to_y < it->current_y + line_height)
9319 /* If TO_Y is in this line and TO_X was reached
9320 above, we scanned too far. We have to restore
9321 IT's settings to the ones before skipping. But
9322 keep the more accurate values of max_ascent and
9323 max_descent we've found while skipping the rest
9324 of the line, for the sake of callers, such as
9325 pos_visible_p, that need to know the line
9326 height. */
9327 int max_ascent = it->max_ascent;
9328 int max_descent = it->max_descent;
9330 RESTORE_IT (it, &it_backup, backup_data);
9331 it->max_ascent = max_ascent;
9332 it->max_descent = max_descent;
9333 reached = 6;
9335 else
9337 skip = skip2;
9338 if (skip == MOVE_POS_MATCH_OR_ZV)
9339 reached = 7;
9342 else
9344 /* Check whether TO_Y is in this line. */
9345 line_height = it->max_ascent + it->max_descent;
9346 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9348 if (to_y >= it->current_y
9349 && to_y < it->current_y + line_height)
9351 if (to_y > it->current_y)
9352 max_current_x = max (it->current_x, max_current_x);
9354 /* When word-wrap is on, TO_X may lie past the end
9355 of a wrapped line. Then it->current is the
9356 character on the next line, so backtrack to the
9357 space before the wrap point. */
9358 if (skip == MOVE_LINE_CONTINUED
9359 && it->line_wrap == WORD_WRAP)
9361 int prev_x = max (it->current_x - 1, 0);
9362 RESTORE_IT (it, &it_backup, backup_data);
9363 skip = move_it_in_display_line_to
9364 (it, -1, prev_x, MOVE_TO_X);
9367 reached = 6;
9371 if (reached)
9373 max_current_x = max (it->current_x, max_current_x);
9374 break;
9377 else if (BUFFERP (it->object)
9378 && (it->method == GET_FROM_BUFFER
9379 || it->method == GET_FROM_STRETCH)
9380 && IT_CHARPOS (*it) >= to_charpos
9381 /* Under bidi iteration, a call to set_iterator_to_next
9382 can scan far beyond to_charpos if the initial
9383 portion of the next line needs to be reordered. In
9384 that case, give move_it_in_display_line_to another
9385 chance below. */
9386 && !(it->bidi_p
9387 && it->bidi_it.scan_dir == -1))
9388 skip = MOVE_POS_MATCH_OR_ZV;
9389 else
9390 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9392 switch (skip)
9394 case MOVE_POS_MATCH_OR_ZV:
9395 max_current_x = max (it->current_x, max_current_x);
9396 reached = 8;
9397 goto out;
9399 case MOVE_NEWLINE_OR_CR:
9400 max_current_x = max (it->current_x, max_current_x);
9401 set_iterator_to_next (it, true);
9402 it->continuation_lines_width = 0;
9403 break;
9405 case MOVE_LINE_TRUNCATED:
9406 max_current_x = it->last_visible_x;
9407 it->continuation_lines_width = 0;
9408 reseat_at_next_visible_line_start (it, false);
9409 if ((op & MOVE_TO_POS) != 0
9410 && IT_CHARPOS (*it) > to_charpos)
9412 reached = 9;
9413 goto out;
9415 break;
9417 case MOVE_LINE_CONTINUED:
9418 max_current_x = it->last_visible_x;
9419 /* For continued lines ending in a tab, some of the glyphs
9420 associated with the tab are displayed on the current
9421 line. Since it->current_x does not include these glyphs,
9422 we use it->last_visible_x instead. */
9423 if (it->c == '\t')
9425 it->continuation_lines_width += it->last_visible_x;
9426 /* When moving by vpos, ensure that the iterator really
9427 advances to the next line (bug#847, bug#969). Fixme:
9428 do we need to do this in other circumstances? */
9429 if (it->current_x != it->last_visible_x
9430 && (op & MOVE_TO_VPOS)
9431 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9433 line_start_x = it->current_x + it->pixel_width
9434 - it->last_visible_x;
9435 if (FRAME_WINDOW_P (it->f))
9437 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9438 struct font *face_font = face->font;
9440 /* When display_line produces a continued line
9441 that ends in a TAB, it skips a tab stop that
9442 is closer than the font's space character
9443 width (see x_produce_glyphs where it produces
9444 the stretch glyph which represents a TAB).
9445 We need to reproduce the same logic here. */
9446 eassert (face_font);
9447 if (face_font)
9449 if (line_start_x < face_font->space_width)
9450 line_start_x
9451 += it->tab_width * face_font->space_width;
9454 set_iterator_to_next (it, false);
9457 else
9458 it->continuation_lines_width += it->current_x;
9459 break;
9461 default:
9462 emacs_abort ();
9465 /* Reset/increment for the next run. */
9466 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9467 it->current_x = line_start_x;
9468 line_start_x = 0;
9469 it->hpos = 0;
9470 it->current_y += it->max_ascent + it->max_descent;
9471 ++it->vpos;
9472 last_height = it->max_ascent + it->max_descent;
9473 it->max_ascent = it->max_descent = 0;
9476 out:
9478 /* On text terminals, we may stop at the end of a line in the middle
9479 of a multi-character glyph. If the glyph itself is continued,
9480 i.e. it is actually displayed on the next line, don't treat this
9481 stopping point as valid; move to the next line instead (unless
9482 that brings us offscreen). */
9483 if (!FRAME_WINDOW_P (it->f)
9484 && op & MOVE_TO_POS
9485 && IT_CHARPOS (*it) == to_charpos
9486 && it->what == IT_CHARACTER
9487 && it->nglyphs > 1
9488 && it->line_wrap == WINDOW_WRAP
9489 && it->current_x == it->last_visible_x - 1
9490 && it->c != '\n'
9491 && it->c != '\t'
9492 && it->w->window_end_valid
9493 && it->vpos < it->w->window_end_vpos)
9495 it->continuation_lines_width += it->current_x;
9496 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9497 it->current_y += it->max_ascent + it->max_descent;
9498 ++it->vpos;
9499 last_height = it->max_ascent + it->max_descent;
9502 if (backup_data)
9503 bidi_unshelve_cache (backup_data, true);
9505 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9507 return max_current_x;
9511 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9513 If DY > 0, move IT backward at least that many pixels. DY = 0
9514 means move IT backward to the preceding line start or BEGV. This
9515 function may move over more than DY pixels if IT->current_y - DY
9516 ends up in the middle of a line; in this case IT->current_y will be
9517 set to the top of the line moved to. */
9519 void
9520 move_it_vertically_backward (struct it *it, int dy)
9522 int nlines, h;
9523 struct it it2, it3;
9524 void *it2data = NULL, *it3data = NULL;
9525 ptrdiff_t start_pos;
9526 int nchars_per_row
9527 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9528 ptrdiff_t pos_limit;
9530 move_further_back:
9531 eassert (dy >= 0);
9533 start_pos = IT_CHARPOS (*it);
9535 /* Estimate how many newlines we must move back. */
9536 nlines = max (1, dy / default_line_pixel_height (it->w));
9537 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9538 pos_limit = BEGV;
9539 else
9540 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9542 /* Set the iterator's position that many lines back. But don't go
9543 back more than NLINES full screen lines -- this wins a day with
9544 buffers which have very long lines. */
9545 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9546 back_to_previous_visible_line_start (it);
9548 /* Reseat the iterator here. When moving backward, we don't want
9549 reseat to skip forward over invisible text, set up the iterator
9550 to deliver from overlay strings at the new position etc. So,
9551 use reseat_1 here. */
9552 reseat_1 (it, it->current.pos, true);
9554 /* We are now surely at a line start. */
9555 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9556 reordering is in effect. */
9557 it->continuation_lines_width = 0;
9559 /* Move forward and see what y-distance we moved. First move to the
9560 start of the next line so that we get its height. We need this
9561 height to be able to tell whether we reached the specified
9562 y-distance. */
9563 SAVE_IT (it2, *it, it2data);
9564 it2.max_ascent = it2.max_descent = 0;
9567 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9568 MOVE_TO_POS | MOVE_TO_VPOS);
9570 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9571 /* If we are in a display string which starts at START_POS,
9572 and that display string includes a newline, and we are
9573 right after that newline (i.e. at the beginning of a
9574 display line), exit the loop, because otherwise we will
9575 infloop, since move_it_to will see that it is already at
9576 START_POS and will not move. */
9577 || (it2.method == GET_FROM_STRING
9578 && IT_CHARPOS (it2) == start_pos
9579 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9580 eassert (IT_CHARPOS (*it) >= BEGV);
9581 SAVE_IT (it3, it2, it3data);
9583 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9584 eassert (IT_CHARPOS (*it) >= BEGV);
9585 /* H is the actual vertical distance from the position in *IT
9586 and the starting position. */
9587 h = it2.current_y - it->current_y;
9588 /* NLINES is the distance in number of lines. */
9589 nlines = it2.vpos - it->vpos;
9591 /* Correct IT's y and vpos position
9592 so that they are relative to the starting point. */
9593 it->vpos -= nlines;
9594 it->current_y -= h;
9596 if (dy == 0)
9598 /* DY == 0 means move to the start of the screen line. The
9599 value of nlines is > 0 if continuation lines were involved,
9600 or if the original IT position was at start of a line. */
9601 RESTORE_IT (it, it, it2data);
9602 if (nlines > 0)
9603 move_it_by_lines (it, nlines);
9604 /* The above code moves us to some position NLINES down,
9605 usually to its first glyph (leftmost in an L2R line), but
9606 that's not necessarily the start of the line, under bidi
9607 reordering. We want to get to the character position
9608 that is immediately after the newline of the previous
9609 line. */
9610 if (it->bidi_p
9611 && !it->continuation_lines_width
9612 && !STRINGP (it->string)
9613 && IT_CHARPOS (*it) > BEGV
9614 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9616 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9618 DEC_BOTH (cp, bp);
9619 cp = find_newline_no_quit (cp, bp, -1, NULL);
9620 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9622 bidi_unshelve_cache (it3data, true);
9624 else
9626 /* The y-position we try to reach, relative to *IT.
9627 Note that H has been subtracted in front of the if-statement. */
9628 int target_y = it->current_y + h - dy;
9629 int y0 = it3.current_y;
9630 int y1;
9631 int line_height;
9633 RESTORE_IT (&it3, &it3, it3data);
9634 y1 = line_bottom_y (&it3);
9635 line_height = y1 - y0;
9636 RESTORE_IT (it, it, it2data);
9637 /* If we did not reach target_y, try to move further backward if
9638 we can. If we moved too far backward, try to move forward. */
9639 if (target_y < it->current_y
9640 /* This is heuristic. In a window that's 3 lines high, with
9641 a line height of 13 pixels each, recentering with point
9642 on the bottom line will try to move -39/2 = 19 pixels
9643 backward. Try to avoid moving into the first line. */
9644 && (it->current_y - target_y
9645 > min (window_box_height (it->w), line_height * 2 / 3))
9646 && IT_CHARPOS (*it) > BEGV)
9648 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9649 target_y - it->current_y));
9650 dy = it->current_y - target_y;
9651 goto move_further_back;
9653 else if (target_y >= it->current_y + line_height
9654 && IT_CHARPOS (*it) < ZV)
9656 /* Should move forward by at least one line, maybe more.
9658 Note: Calling move_it_by_lines can be expensive on
9659 terminal frames, where compute_motion is used (via
9660 vmotion) to do the job, when there are very long lines
9661 and truncate-lines is nil. That's the reason for
9662 treating terminal frames specially here. */
9664 if (!FRAME_WINDOW_P (it->f))
9665 move_it_vertically (it, target_y - it->current_y);
9666 else
9670 move_it_by_lines (it, 1);
9672 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9679 /* Move IT by a specified amount of pixel lines DY. DY negative means
9680 move backwards. DY = 0 means move to start of screen line. At the
9681 end, IT will be on the start of a screen line. */
9683 void
9684 move_it_vertically (struct it *it, int dy)
9686 if (dy <= 0)
9687 move_it_vertically_backward (it, -dy);
9688 else
9690 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9691 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9692 MOVE_TO_POS | MOVE_TO_Y);
9693 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9695 /* If buffer ends in ZV without a newline, move to the start of
9696 the line to satisfy the post-condition. */
9697 if (IT_CHARPOS (*it) == ZV
9698 && ZV > BEGV
9699 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9700 move_it_by_lines (it, 0);
9705 /* Move iterator IT past the end of the text line it is in. */
9707 void
9708 move_it_past_eol (struct it *it)
9710 enum move_it_result rc;
9712 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9713 if (rc == MOVE_NEWLINE_OR_CR)
9714 set_iterator_to_next (it, false);
9718 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9719 negative means move up. DVPOS == 0 means move to the start of the
9720 screen line.
9722 Optimization idea: If we would know that IT->f doesn't use
9723 a face with proportional font, we could be faster for
9724 truncate-lines nil. */
9726 void
9727 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9730 /* The commented-out optimization uses vmotion on terminals. This
9731 gives bad results, because elements like it->what, on which
9732 callers such as pos_visible_p rely, aren't updated. */
9733 /* struct position pos;
9734 if (!FRAME_WINDOW_P (it->f))
9736 struct text_pos textpos;
9738 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9739 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9740 reseat (it, textpos, true);
9741 it->vpos += pos.vpos;
9742 it->current_y += pos.vpos;
9744 else */
9746 if (dvpos == 0)
9748 /* DVPOS == 0 means move to the start of the screen line. */
9749 move_it_vertically_backward (it, 0);
9750 /* Let next call to line_bottom_y calculate real line height. */
9751 last_height = 0;
9753 else if (dvpos > 0)
9755 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9756 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9758 /* Only move to the next buffer position if we ended up in a
9759 string from display property, not in an overlay string
9760 (before-string or after-string). That is because the
9761 latter don't conceal the underlying buffer position, so
9762 we can ask to move the iterator to the exact position we
9763 are interested in. Note that, even if we are already at
9764 IT_CHARPOS (*it), the call below is not a no-op, as it
9765 will detect that we are at the end of the string, pop the
9766 iterator, and compute it->current_x and it->hpos
9767 correctly. */
9768 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9769 -1, -1, -1, MOVE_TO_POS);
9772 else
9774 struct it it2;
9775 void *it2data = NULL;
9776 ptrdiff_t start_charpos, i;
9777 int nchars_per_row
9778 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9779 bool hit_pos_limit = false;
9780 ptrdiff_t pos_limit;
9782 /* Start at the beginning of the screen line containing IT's
9783 position. This may actually move vertically backwards,
9784 in case of overlays, so adjust dvpos accordingly. */
9785 dvpos += it->vpos;
9786 move_it_vertically_backward (it, 0);
9787 dvpos -= it->vpos;
9789 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9790 screen lines, and reseat the iterator there. */
9791 start_charpos = IT_CHARPOS (*it);
9792 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9793 pos_limit = BEGV;
9794 else
9795 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9797 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9798 back_to_previous_visible_line_start (it);
9799 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9800 hit_pos_limit = true;
9801 reseat (it, it->current.pos, true);
9803 /* Move further back if we end up in a string or an image. */
9804 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9806 /* First try to move to start of display line. */
9807 dvpos += it->vpos;
9808 move_it_vertically_backward (it, 0);
9809 dvpos -= it->vpos;
9810 if (IT_POS_VALID_AFTER_MOVE_P (it))
9811 break;
9812 /* If start of line is still in string or image,
9813 move further back. */
9814 back_to_previous_visible_line_start (it);
9815 reseat (it, it->current.pos, true);
9816 dvpos--;
9819 it->current_x = it->hpos = 0;
9821 /* Above call may have moved too far if continuation lines
9822 are involved. Scan forward and see if it did. */
9823 SAVE_IT (it2, *it, it2data);
9824 it2.vpos = it2.current_y = 0;
9825 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9826 it->vpos -= it2.vpos;
9827 it->current_y -= it2.current_y;
9828 it->current_x = it->hpos = 0;
9830 /* If we moved too far back, move IT some lines forward. */
9831 if (it2.vpos > -dvpos)
9833 int delta = it2.vpos + dvpos;
9835 RESTORE_IT (&it2, &it2, it2data);
9836 SAVE_IT (it2, *it, it2data);
9837 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9838 /* Move back again if we got too far ahead. */
9839 if (IT_CHARPOS (*it) >= start_charpos)
9840 RESTORE_IT (it, &it2, it2data);
9841 else
9842 bidi_unshelve_cache (it2data, true);
9844 else if (hit_pos_limit && pos_limit > BEGV
9845 && dvpos < 0 && it2.vpos < -dvpos)
9847 /* If we hit the limit, but still didn't make it far enough
9848 back, that means there's a display string with a newline
9849 covering a large chunk of text, and that caused
9850 back_to_previous_visible_line_start try to go too far.
9851 Punish those who commit such atrocities by going back
9852 until we've reached DVPOS, after lifting the limit, which
9853 could make it slow for very long lines. "If it hurts,
9854 don't do that!" */
9855 dvpos += it2.vpos;
9856 RESTORE_IT (it, it, it2data);
9857 for (i = -dvpos; i > 0; --i)
9859 back_to_previous_visible_line_start (it);
9860 it->vpos--;
9862 reseat_1 (it, it->current.pos, true);
9864 else
9865 RESTORE_IT (it, it, it2data);
9869 /* Return true if IT points into the middle of a display vector. */
9871 bool
9872 in_display_vector_p (struct it *it)
9874 return (it->method == GET_FROM_DISPLAY_VECTOR
9875 && it->current.dpvec_index > 0
9876 && it->dpvec + it->current.dpvec_index != it->dpend);
9879 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9880 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9881 WINDOW must be a live window and defaults to the selected one. The
9882 return value is a cons of the maximum pixel-width of any text line and
9883 the maximum pixel-height of all text lines.
9885 The optional argument FROM, if non-nil, specifies the first text
9886 position and defaults to the minimum accessible position of the buffer.
9887 If FROM is t, use the minimum accessible position that starts a
9888 non-empty line. TO, if non-nil, specifies the last text position and
9889 defaults to the maximum accessible position of the buffer. If TO is t,
9890 use the maximum accessible position that ends a non-empty line.
9892 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9893 width that can be returned. X-LIMIT nil or omitted, means to use the
9894 pixel-width of WINDOW's body; use this if you want to know how high
9895 WINDOW should be become in order to fit all of its buffer's text with
9896 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9897 if you intend to change WINDOW's width. In any case, text whose
9898 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9899 of long lines can take some time, it's always a good idea to make this
9900 argument as small as possible; in particular, if the buffer contains
9901 long lines that shall be truncated anyway.
9903 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9904 height (excluding the height of the mode- or header-line, if any) that
9905 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9906 ignored. Since calculating the text height of a large buffer can take
9907 some time, it makes sense to specify this argument if the size of the
9908 buffer is large or unknown.
9910 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9911 include the height of the mode- or header-line of WINDOW in the return
9912 value. If it is either the symbol `mode-line' or `header-line', include
9913 only the height of that line, if present, in the return value. If t,
9914 include the height of both, if present, in the return value. */)
9915 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9916 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9918 struct window *w = decode_live_window (window);
9919 Lisp_Object buffer = w->contents;
9920 struct buffer *b;
9921 struct it it;
9922 struct buffer *old_b = NULL;
9923 ptrdiff_t start, end, pos;
9924 struct text_pos startp;
9925 void *itdata = NULL;
9926 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9928 CHECK_BUFFER (buffer);
9929 b = XBUFFER (buffer);
9931 if (b != current_buffer)
9933 old_b = current_buffer;
9934 set_buffer_internal (b);
9937 if (NILP (from))
9938 start = BEGV;
9939 else if (EQ (from, Qt))
9941 start = pos = BEGV;
9942 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9943 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9944 start = pos;
9945 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9946 start = pos;
9948 else
9950 CHECK_NUMBER_COERCE_MARKER (from);
9951 start = min (max (XINT (from), BEGV), ZV);
9954 if (NILP (to))
9955 end = ZV;
9956 else if (EQ (to, Qt))
9958 end = pos = ZV;
9959 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9960 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9961 end = pos;
9962 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9963 end = pos;
9965 else
9967 CHECK_NUMBER_COERCE_MARKER (to);
9968 end = max (start, min (XINT (to), ZV));
9971 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9972 max_x = XINT (x_limit);
9974 if (NILP (y_limit))
9975 max_y = INT_MAX;
9976 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9977 max_y = XINT (y_limit);
9979 itdata = bidi_shelve_cache ();
9980 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9981 start_display (&it, w, startp);
9983 if (NILP (x_limit))
9984 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9985 else
9987 it.last_visible_x = max_x;
9988 /* Actually, we never want move_it_to stop at to_x. But to make
9989 sure that move_it_in_display_line_to always moves far enough,
9990 we set it to INT_MAX and specify MOVE_TO_X. */
9991 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9992 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9993 /* Don't return more than X-LIMIT. */
9994 if (x > max_x)
9995 x = max_x;
9998 /* Subtract height of header-line which was counted automatically by
9999 start_display. */
10000 y = it.current_y + it.max_ascent + it.max_descent
10001 - WINDOW_HEADER_LINE_HEIGHT (w);
10002 /* Don't return more than Y-LIMIT. */
10003 if (y > max_y)
10004 y = max_y;
10006 if (EQ (mode_and_header_line, Qheader_line)
10007 || EQ (mode_and_header_line, Qt))
10008 /* Re-add height of header-line as requested. */
10009 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10011 if (EQ (mode_and_header_line, Qmode_line)
10012 || EQ (mode_and_header_line, Qt))
10013 /* Add height of mode-line as requested. */
10014 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10016 bidi_unshelve_cache (itdata, false);
10018 if (old_b)
10019 set_buffer_internal (old_b);
10021 return Fcons (make_number (x), make_number (y));
10024 /***********************************************************************
10025 Messages
10026 ***********************************************************************/
10028 /* Return the number of arguments the format string FORMAT needs. */
10030 static ptrdiff_t
10031 format_nargs (char const *format)
10033 ptrdiff_t nargs = 0;
10034 for (char const *p = format; (p = strchr (p, '%')); p++)
10035 if (p[1] == '%')
10036 p++;
10037 else
10038 nargs++;
10039 return nargs;
10042 /* Add a message with format string FORMAT and formatted arguments
10043 to *Messages*. */
10045 void
10046 add_to_log (const char *format, ...)
10048 va_list ap;
10049 va_start (ap, format);
10050 vadd_to_log (format, ap);
10051 va_end (ap);
10054 void
10055 vadd_to_log (char const *format, va_list ap)
10057 ptrdiff_t form_nargs = format_nargs (format);
10058 ptrdiff_t nargs = 1 + form_nargs;
10059 Lisp_Object args[10];
10060 eassert (nargs <= ARRAYELTS (args));
10061 AUTO_STRING (args0, format);
10062 args[0] = args0;
10063 for (ptrdiff_t i = 1; i <= nargs; i++)
10064 args[i] = va_arg (ap, Lisp_Object);
10065 Lisp_Object msg = Qnil;
10066 msg = Fformat_message (nargs, args);
10068 ptrdiff_t len = SBYTES (msg) + 1;
10069 USE_SAFE_ALLOCA;
10070 char *buffer = SAFE_ALLOCA (len);
10071 memcpy (buffer, SDATA (msg), len);
10073 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10074 SAFE_FREE ();
10078 /* Output a newline in the *Messages* buffer if "needs" one. */
10080 void
10081 message_log_maybe_newline (void)
10083 if (message_log_need_newline)
10084 message_dolog ("", 0, true, false);
10088 /* Add a string M of length NBYTES to the message log, optionally
10089 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10090 true, means interpret the contents of M as multibyte. This
10091 function calls low-level routines in order to bypass text property
10092 hooks, etc. which might not be safe to run.
10094 This may GC (insert may run before/after change hooks),
10095 so the buffer M must NOT point to a Lisp string. */
10097 void
10098 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10100 const unsigned char *msg = (const unsigned char *) m;
10102 if (!NILP (Vmemory_full))
10103 return;
10105 if (!NILP (Vmessage_log_max))
10107 struct buffer *oldbuf;
10108 Lisp_Object oldpoint, oldbegv, oldzv;
10109 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10110 ptrdiff_t point_at_end = 0;
10111 ptrdiff_t zv_at_end = 0;
10112 Lisp_Object old_deactivate_mark;
10114 old_deactivate_mark = Vdeactivate_mark;
10115 oldbuf = current_buffer;
10117 /* Ensure the Messages buffer exists, and switch to it.
10118 If we created it, set the major-mode. */
10119 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10120 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10121 if (newbuffer
10122 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10123 call0 (intern ("messages-buffer-mode"));
10125 bset_undo_list (current_buffer, Qt);
10126 bset_cache_long_scans (current_buffer, Qnil);
10128 oldpoint = message_dolog_marker1;
10129 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10130 oldbegv = message_dolog_marker2;
10131 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10132 oldzv = message_dolog_marker3;
10133 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10135 if (PT == Z)
10136 point_at_end = 1;
10137 if (ZV == Z)
10138 zv_at_end = 1;
10140 BEGV = BEG;
10141 BEGV_BYTE = BEG_BYTE;
10142 ZV = Z;
10143 ZV_BYTE = Z_BYTE;
10144 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10146 /* Insert the string--maybe converting multibyte to single byte
10147 or vice versa, so that all the text fits the buffer. */
10148 if (multibyte
10149 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10151 ptrdiff_t i;
10152 int c, char_bytes;
10153 char work[1];
10155 /* Convert a multibyte string to single-byte
10156 for the *Message* buffer. */
10157 for (i = 0; i < nbytes; i += char_bytes)
10159 c = string_char_and_length (msg + i, &char_bytes);
10160 work[0] = CHAR_TO_BYTE8 (c);
10161 insert_1_both (work, 1, 1, true, false, false);
10164 else if (! multibyte
10165 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10167 ptrdiff_t i;
10168 int c, char_bytes;
10169 unsigned char str[MAX_MULTIBYTE_LENGTH];
10170 /* Convert a single-byte string to multibyte
10171 for the *Message* buffer. */
10172 for (i = 0; i < nbytes; i++)
10174 c = msg[i];
10175 MAKE_CHAR_MULTIBYTE (c);
10176 char_bytes = CHAR_STRING (c, str);
10177 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10180 else if (nbytes)
10181 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10182 true, false, false);
10184 if (nlflag)
10186 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10187 printmax_t dups;
10189 insert_1_both ("\n", 1, 1, true, false, false);
10191 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10192 this_bol = PT;
10193 this_bol_byte = PT_BYTE;
10195 /* See if this line duplicates the previous one.
10196 If so, combine duplicates. */
10197 if (this_bol > BEG)
10199 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10200 prev_bol = PT;
10201 prev_bol_byte = PT_BYTE;
10203 dups = message_log_check_duplicate (prev_bol_byte,
10204 this_bol_byte);
10205 if (dups)
10207 del_range_both (prev_bol, prev_bol_byte,
10208 this_bol, this_bol_byte, false);
10209 if (dups > 1)
10211 char dupstr[sizeof " [ times]"
10212 + INT_STRLEN_BOUND (printmax_t)];
10214 /* If you change this format, don't forget to also
10215 change message_log_check_duplicate. */
10216 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10217 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10218 insert_1_both (dupstr, duplen, duplen,
10219 true, false, true);
10224 /* If we have more than the desired maximum number of lines
10225 in the *Messages* buffer now, delete the oldest ones.
10226 This is safe because we don't have undo in this buffer. */
10228 if (NATNUMP (Vmessage_log_max))
10230 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10231 -XFASTINT (Vmessage_log_max) - 1, false);
10232 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10235 BEGV = marker_position (oldbegv);
10236 BEGV_BYTE = marker_byte_position (oldbegv);
10238 if (zv_at_end)
10240 ZV = Z;
10241 ZV_BYTE = Z_BYTE;
10243 else
10245 ZV = marker_position (oldzv);
10246 ZV_BYTE = marker_byte_position (oldzv);
10249 if (point_at_end)
10250 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10251 else
10252 /* We can't do Fgoto_char (oldpoint) because it will run some
10253 Lisp code. */
10254 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10255 marker_byte_position (oldpoint));
10257 unchain_marker (XMARKER (oldpoint));
10258 unchain_marker (XMARKER (oldbegv));
10259 unchain_marker (XMARKER (oldzv));
10261 /* We called insert_1_both above with its 5th argument (PREPARE)
10262 false, which prevents insert_1_both from calling
10263 prepare_to_modify_buffer, which in turns prevents us from
10264 incrementing windows_or_buffers_changed even if *Messages* is
10265 shown in some window. So we must manually set
10266 windows_or_buffers_changed here to make up for that. */
10267 windows_or_buffers_changed = old_windows_or_buffers_changed;
10268 bset_redisplay (current_buffer);
10270 set_buffer_internal (oldbuf);
10272 message_log_need_newline = !nlflag;
10273 Vdeactivate_mark = old_deactivate_mark;
10278 /* We are at the end of the buffer after just having inserted a newline.
10279 (Note: We depend on the fact we won't be crossing the gap.)
10280 Check to see if the most recent message looks a lot like the previous one.
10281 Return 0 if different, 1 if the new one should just replace it, or a
10282 value N > 1 if we should also append " [N times]". */
10284 static intmax_t
10285 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10287 ptrdiff_t i;
10288 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10289 bool seen_dots = false;
10290 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10291 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10293 for (i = 0; i < len; i++)
10295 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10296 seen_dots = true;
10297 if (p1[i] != p2[i])
10298 return seen_dots;
10300 p1 += len;
10301 if (*p1 == '\n')
10302 return 2;
10303 if (*p1++ == ' ' && *p1++ == '[')
10305 char *pend;
10306 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10307 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10308 return n + 1;
10310 return 0;
10314 /* Display an echo area message M with a specified length of NBYTES
10315 bytes. The string may include null characters. If M is not a
10316 string, clear out any existing message, and let the mini-buffer
10317 text show through.
10319 This function cancels echoing. */
10321 void
10322 message3 (Lisp_Object m)
10324 clear_message (true, true);
10325 cancel_echoing ();
10327 /* First flush out any partial line written with print. */
10328 message_log_maybe_newline ();
10329 if (STRINGP (m))
10331 ptrdiff_t nbytes = SBYTES (m);
10332 bool multibyte = STRING_MULTIBYTE (m);
10333 char *buffer;
10334 USE_SAFE_ALLOCA;
10335 SAFE_ALLOCA_STRING (buffer, m);
10336 message_dolog (buffer, nbytes, true, multibyte);
10337 SAFE_FREE ();
10339 if (! inhibit_message)
10340 message3_nolog (m);
10343 /* Log the message M to stderr. Log an empty line if M is not a string. */
10345 static void
10346 message_to_stderr (Lisp_Object m)
10348 if (noninteractive_need_newline)
10350 noninteractive_need_newline = false;
10351 fputc ('\n', stderr);
10353 if (STRINGP (m))
10355 Lisp_Object coding_system = Vlocale_coding_system;
10356 Lisp_Object s;
10358 if (!NILP (Vcoding_system_for_write))
10359 coding_system = Vcoding_system_for_write;
10360 if (!NILP (coding_system))
10361 s = code_convert_string_norecord (m, coding_system, true);
10362 else
10363 s = m;
10365 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10367 if (!cursor_in_echo_area)
10368 fputc ('\n', stderr);
10369 fflush (stderr);
10372 /* The non-logging version of message3.
10373 This does not cancel echoing, because it is used for echoing.
10374 Perhaps we need to make a separate function for echoing
10375 and make this cancel echoing. */
10377 void
10378 message3_nolog (Lisp_Object m)
10380 struct frame *sf = SELECTED_FRAME ();
10382 if (FRAME_INITIAL_P (sf))
10383 message_to_stderr (m);
10384 /* Error messages get reported properly by cmd_error, so this must be just an
10385 informative message; if the frame hasn't really been initialized yet, just
10386 toss it. */
10387 else if (INTERACTIVE && sf->glyphs_initialized_p)
10389 /* Get the frame containing the mini-buffer
10390 that the selected frame is using. */
10391 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10392 Lisp_Object frame = XWINDOW (mini_window)->frame;
10393 struct frame *f = XFRAME (frame);
10395 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10396 Fmake_frame_visible (frame);
10398 if (STRINGP (m) && SCHARS (m) > 0)
10400 set_message (m);
10401 if (minibuffer_auto_raise)
10402 Fraise_frame (frame);
10403 /* Assume we are not echoing.
10404 (If we are, echo_now will override this.) */
10405 echo_message_buffer = Qnil;
10407 else
10408 clear_message (true, true);
10410 do_pending_window_change (false);
10411 echo_area_display (true);
10412 do_pending_window_change (false);
10413 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10414 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10419 /* Display a null-terminated echo area message M. If M is 0, clear
10420 out any existing message, and let the mini-buffer text show through.
10422 The buffer M must continue to exist until after the echo area gets
10423 cleared or some other message gets displayed there. Do not pass
10424 text that is stored in a Lisp string. Do not pass text in a buffer
10425 that was alloca'd. */
10427 void
10428 message1 (const char *m)
10430 message3 (m ? build_unibyte_string (m) : Qnil);
10434 /* The non-logging counterpart of message1. */
10436 void
10437 message1_nolog (const char *m)
10439 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10442 /* Display a message M which contains a single %s
10443 which gets replaced with STRING. */
10445 void
10446 message_with_string (const char *m, Lisp_Object string, bool log)
10448 CHECK_STRING (string);
10450 bool need_message;
10451 if (noninteractive)
10452 need_message = !!m;
10453 else if (!INTERACTIVE)
10454 need_message = false;
10455 else
10457 /* The frame whose minibuffer we're going to display the message on.
10458 It may be larger than the selected frame, so we need
10459 to use its buffer, not the selected frame's buffer. */
10460 Lisp_Object mini_window;
10461 struct frame *f, *sf = SELECTED_FRAME ();
10463 /* Get the frame containing the minibuffer
10464 that the selected frame is using. */
10465 mini_window = FRAME_MINIBUF_WINDOW (sf);
10466 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10468 /* Error messages get reported properly by cmd_error, so this must be
10469 just an informative message; if the frame hasn't really been
10470 initialized yet, just toss it. */
10471 need_message = f->glyphs_initialized_p;
10474 if (need_message)
10476 AUTO_STRING (fmt, m);
10477 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10479 if (noninteractive)
10480 message_to_stderr (msg);
10481 else
10483 if (log)
10484 message3 (msg);
10485 else
10486 message3_nolog (msg);
10488 /* Print should start at the beginning of the message
10489 buffer next time. */
10490 message_buf_print = false;
10496 /* Dump an informative message to the minibuf. If M is 0, clear out
10497 any existing message, and let the mini-buffer text show through.
10499 The message must be safe ASCII and the format must not contain ` or
10500 '. If your message and format do not fit into this category,
10501 convert your arguments to Lisp objects and use Fmessage instead. */
10503 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10504 vmessage (const char *m, va_list ap)
10506 if (noninteractive)
10508 if (m)
10510 if (noninteractive_need_newline)
10511 putc ('\n', stderr);
10512 noninteractive_need_newline = false;
10513 vfprintf (stderr, m, ap);
10514 if (!cursor_in_echo_area)
10515 fprintf (stderr, "\n");
10516 fflush (stderr);
10519 else if (INTERACTIVE)
10521 /* The frame whose mini-buffer we're going to display the message
10522 on. It may be larger than the selected frame, so we need to
10523 use its buffer, not the selected frame's buffer. */
10524 Lisp_Object mini_window;
10525 struct frame *f, *sf = SELECTED_FRAME ();
10527 /* Get the frame containing the mini-buffer
10528 that the selected frame is using. */
10529 mini_window = FRAME_MINIBUF_WINDOW (sf);
10530 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10532 /* Error messages get reported properly by cmd_error, so this must be
10533 just an informative message; if the frame hasn't really been
10534 initialized yet, just toss it. */
10535 if (f->glyphs_initialized_p)
10537 if (m)
10539 ptrdiff_t len;
10540 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10541 USE_SAFE_ALLOCA;
10542 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10544 len = doprnt (message_buf, maxsize, m, 0, ap);
10546 message3 (make_string (message_buf, len));
10547 SAFE_FREE ();
10549 else
10550 message1 (0);
10552 /* Print should start at the beginning of the message
10553 buffer next time. */
10554 message_buf_print = false;
10559 void
10560 message (const char *m, ...)
10562 va_list ap;
10563 va_start (ap, m);
10564 vmessage (m, ap);
10565 va_end (ap);
10569 /* Display the current message in the current mini-buffer. This is
10570 only called from error handlers in process.c, and is not time
10571 critical. */
10573 void
10574 update_echo_area (void)
10576 if (!NILP (echo_area_buffer[0]))
10578 Lisp_Object string;
10579 string = Fcurrent_message ();
10580 message3 (string);
10585 /* Make sure echo area buffers in `echo_buffers' are live.
10586 If they aren't, make new ones. */
10588 static void
10589 ensure_echo_area_buffers (void)
10591 for (int i = 0; i < 2; i++)
10592 if (!BUFFERP (echo_buffer[i])
10593 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10595 Lisp_Object old_buffer = echo_buffer[i];
10596 static char const name_fmt[] = " *Echo Area %d*";
10597 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10598 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10599 echo_buffer[i] = Fget_buffer_create (lname);
10600 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10601 /* to force word wrap in echo area -
10602 it was decided to postpone this*/
10603 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10605 for (int j = 0; j < 2; j++)
10606 if (EQ (old_buffer, echo_area_buffer[j]))
10607 echo_area_buffer[j] = echo_buffer[i];
10612 /* Call FN with args A1..A2 with either the current or last displayed
10613 echo_area_buffer as current buffer.
10615 WHICH zero means use the current message buffer
10616 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10617 from echo_buffer[] and clear it.
10619 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10620 suitable buffer from echo_buffer[] and clear it.
10622 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10623 that the current message becomes the last displayed one, choose a
10624 suitable buffer for echo_area_buffer[0], and clear it.
10626 Value is what FN returns. */
10628 static bool
10629 with_echo_area_buffer (struct window *w, int which,
10630 bool (*fn) (ptrdiff_t, Lisp_Object),
10631 ptrdiff_t a1, Lisp_Object a2)
10633 Lisp_Object buffer;
10634 bool this_one, the_other, clear_buffer_p, rc;
10635 ptrdiff_t count = SPECPDL_INDEX ();
10637 /* If buffers aren't live, make new ones. */
10638 ensure_echo_area_buffers ();
10640 clear_buffer_p = false;
10642 if (which == 0)
10643 this_one = false, the_other = true;
10644 else if (which > 0)
10645 this_one = true, the_other = false;
10646 else
10648 this_one = false, the_other = true;
10649 clear_buffer_p = true;
10651 /* We need a fresh one in case the current echo buffer equals
10652 the one containing the last displayed echo area message. */
10653 if (!NILP (echo_area_buffer[this_one])
10654 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10655 echo_area_buffer[this_one] = Qnil;
10658 /* Choose a suitable buffer from echo_buffer[] if we don't
10659 have one. */
10660 if (NILP (echo_area_buffer[this_one]))
10662 echo_area_buffer[this_one]
10663 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10664 ? echo_buffer[the_other]
10665 : echo_buffer[this_one]);
10666 clear_buffer_p = true;
10669 buffer = echo_area_buffer[this_one];
10671 /* Don't get confused by reusing the buffer used for echoing
10672 for a different purpose. */
10673 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10674 cancel_echoing ();
10676 record_unwind_protect (unwind_with_echo_area_buffer,
10677 with_echo_area_buffer_unwind_data (w));
10679 /* Make the echo area buffer current. Note that for display
10680 purposes, it is not necessary that the displayed window's buffer
10681 == current_buffer, except for text property lookup. So, let's
10682 only set that buffer temporarily here without doing a full
10683 Fset_window_buffer. We must also change w->pointm, though,
10684 because otherwise an assertions in unshow_buffer fails, and Emacs
10685 aborts. */
10686 set_buffer_internal_1 (XBUFFER (buffer));
10687 if (w)
10689 wset_buffer (w, buffer);
10690 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10691 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10694 bset_undo_list (current_buffer, Qt);
10695 bset_read_only (current_buffer, Qnil);
10696 specbind (Qinhibit_read_only, Qt);
10697 specbind (Qinhibit_modification_hooks, Qt);
10699 if (clear_buffer_p && Z > BEG)
10700 del_range (BEG, Z);
10702 eassert (BEGV >= BEG);
10703 eassert (ZV <= Z && ZV >= BEGV);
10705 rc = fn (a1, a2);
10707 eassert (BEGV >= BEG);
10708 eassert (ZV <= Z && ZV >= BEGV);
10710 unbind_to (count, Qnil);
10711 return rc;
10715 /* Save state that should be preserved around the call to the function
10716 FN called in with_echo_area_buffer. */
10718 static Lisp_Object
10719 with_echo_area_buffer_unwind_data (struct window *w)
10721 int i = 0;
10722 Lisp_Object vector, tmp;
10724 /* Reduce consing by keeping one vector in
10725 Vwith_echo_area_save_vector. */
10726 vector = Vwith_echo_area_save_vector;
10727 Vwith_echo_area_save_vector = Qnil;
10729 if (NILP (vector))
10730 vector = Fmake_vector (make_number (11), Qnil);
10732 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10733 ASET (vector, i, Vdeactivate_mark); ++i;
10734 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10736 if (w)
10738 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10739 ASET (vector, i, w->contents); ++i;
10740 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10741 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10742 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10743 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10744 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10745 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10747 else
10749 int end = i + 8;
10750 for (; i < end; ++i)
10751 ASET (vector, i, Qnil);
10754 eassert (i == ASIZE (vector));
10755 return vector;
10759 /* Restore global state from VECTOR which was created by
10760 with_echo_area_buffer_unwind_data. */
10762 static void
10763 unwind_with_echo_area_buffer (Lisp_Object vector)
10765 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10766 Vdeactivate_mark = AREF (vector, 1);
10767 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10769 if (WINDOWP (AREF (vector, 3)))
10771 struct window *w;
10772 Lisp_Object buffer;
10774 w = XWINDOW (AREF (vector, 3));
10775 buffer = AREF (vector, 4);
10777 wset_buffer (w, buffer);
10778 set_marker_both (w->pointm, buffer,
10779 XFASTINT (AREF (vector, 5)),
10780 XFASTINT (AREF (vector, 6)));
10781 set_marker_both (w->old_pointm, buffer,
10782 XFASTINT (AREF (vector, 7)),
10783 XFASTINT (AREF (vector, 8)));
10784 set_marker_both (w->start, buffer,
10785 XFASTINT (AREF (vector, 9)),
10786 XFASTINT (AREF (vector, 10)));
10789 Vwith_echo_area_save_vector = vector;
10793 /* Set up the echo area for use by print functions. MULTIBYTE_P
10794 means we will print multibyte. */
10796 void
10797 setup_echo_area_for_printing (bool multibyte_p)
10799 /* If we can't find an echo area any more, exit. */
10800 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10801 Fkill_emacs (Qnil);
10803 ensure_echo_area_buffers ();
10805 if (!message_buf_print)
10807 /* A message has been output since the last time we printed.
10808 Choose a fresh echo area buffer. */
10809 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10810 echo_area_buffer[0] = echo_buffer[1];
10811 else
10812 echo_area_buffer[0] = echo_buffer[0];
10814 /* Switch to that buffer and clear it. */
10815 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10816 bset_truncate_lines (current_buffer, Qnil);
10818 if (Z > BEG)
10820 ptrdiff_t count = SPECPDL_INDEX ();
10821 specbind (Qinhibit_read_only, Qt);
10822 /* Note that undo recording is always disabled. */
10823 del_range (BEG, Z);
10824 unbind_to (count, Qnil);
10826 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10828 /* Set up the buffer for the multibyteness we need. */
10829 if (multibyte_p
10830 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10831 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10833 /* Raise the frame containing the echo area. */
10834 if (minibuffer_auto_raise)
10836 struct frame *sf = SELECTED_FRAME ();
10837 Lisp_Object mini_window;
10838 mini_window = FRAME_MINIBUF_WINDOW (sf);
10839 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10842 message_log_maybe_newline ();
10843 message_buf_print = true;
10845 else
10847 if (NILP (echo_area_buffer[0]))
10849 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10850 echo_area_buffer[0] = echo_buffer[1];
10851 else
10852 echo_area_buffer[0] = echo_buffer[0];
10855 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10857 /* Someone switched buffers between print requests. */
10858 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10859 bset_truncate_lines (current_buffer, Qnil);
10865 /* Display an echo area message in window W. Value is true if W's
10866 height is changed. If display_last_displayed_message_p,
10867 display the message that was last displayed, otherwise
10868 display the current message. */
10870 static bool
10871 display_echo_area (struct window *w)
10873 bool no_message_p, window_height_changed_p;
10875 /* Temporarily disable garbage collections while displaying the echo
10876 area. This is done because a GC can print a message itself.
10877 That message would modify the echo area buffer's contents while a
10878 redisplay of the buffer is going on, and seriously confuse
10879 redisplay. */
10880 ptrdiff_t count = inhibit_garbage_collection ();
10882 /* If there is no message, we must call display_echo_area_1
10883 nevertheless because it resizes the window. But we will have to
10884 reset the echo_area_buffer in question to nil at the end because
10885 with_echo_area_buffer will sets it to an empty buffer. */
10886 bool i = display_last_displayed_message_p;
10887 /* According to the C99, C11 and C++11 standards, the integral value
10888 of a "bool" is always 0 or 1, so this array access is safe here,
10889 if oddly typed. */
10890 no_message_p = NILP (echo_area_buffer[i]);
10892 window_height_changed_p
10893 = with_echo_area_buffer (w, display_last_displayed_message_p,
10894 display_echo_area_1,
10895 (intptr_t) w, Qnil);
10897 if (no_message_p)
10898 echo_area_buffer[i] = Qnil;
10900 unbind_to (count, Qnil);
10901 return window_height_changed_p;
10905 /* Helper for display_echo_area. Display the current buffer which
10906 contains the current echo area message in window W, a mini-window,
10907 a pointer to which is passed in A1. A2..A4 are currently not used.
10908 Change the height of W so that all of the message is displayed.
10909 Value is true if height of W was changed. */
10911 static bool
10912 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10914 intptr_t i1 = a1;
10915 struct window *w = (struct window *) i1;
10916 Lisp_Object window;
10917 struct text_pos start;
10919 /* We are about to enter redisplay without going through
10920 redisplay_internal, so we need to forget these faces by hand
10921 here. */
10922 forget_escape_and_glyphless_faces ();
10924 /* Do this before displaying, so that we have a large enough glyph
10925 matrix for the display. If we can't get enough space for the
10926 whole text, display the last N lines. That works by setting w->start. */
10927 bool window_height_changed_p = resize_mini_window (w, false);
10929 /* Use the starting position chosen by resize_mini_window. */
10930 SET_TEXT_POS_FROM_MARKER (start, w->start);
10932 /* Display. */
10933 clear_glyph_matrix (w->desired_matrix);
10934 XSETWINDOW (window, w);
10935 try_window (window, start, 0);
10937 return window_height_changed_p;
10941 /* Resize the echo area window to exactly the size needed for the
10942 currently displayed message, if there is one. If a mini-buffer
10943 is active, don't shrink it. */
10945 void
10946 resize_echo_area_exactly (void)
10948 if (BUFFERP (echo_area_buffer[0])
10949 && WINDOWP (echo_area_window))
10951 struct window *w = XWINDOW (echo_area_window);
10952 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10953 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10954 (intptr_t) w, resize_exactly);
10955 if (resized_p)
10957 windows_or_buffers_changed = 42;
10958 update_mode_lines = 30;
10959 redisplay_internal ();
10965 /* Callback function for with_echo_area_buffer, when used from
10966 resize_echo_area_exactly. A1 contains a pointer to the window to
10967 resize, EXACTLY non-nil means resize the mini-window exactly to the
10968 size of the text displayed. A3 and A4 are not used. Value is what
10969 resize_mini_window returns. */
10971 static bool
10972 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10974 intptr_t i1 = a1;
10975 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10979 /* Resize mini-window W to fit the size of its contents. EXACT_P
10980 means size the window exactly to the size needed. Otherwise, it's
10981 only enlarged until W's buffer is empty.
10983 Set W->start to the right place to begin display. If the whole
10984 contents fit, start at the beginning. Otherwise, start so as
10985 to make the end of the contents appear. This is particularly
10986 important for y-or-n-p, but seems desirable generally.
10988 Value is true if the window height has been changed. */
10990 bool
10991 resize_mini_window (struct window *w, bool exact_p)
10993 struct frame *f = XFRAME (w->frame);
10994 bool window_height_changed_p = false;
10996 eassert (MINI_WINDOW_P (w));
10998 /* By default, start display at the beginning. */
10999 set_marker_both (w->start, w->contents,
11000 BUF_BEGV (XBUFFER (w->contents)),
11001 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11003 /* Don't resize windows while redisplaying a window; it would
11004 confuse redisplay functions when the size of the window they are
11005 displaying changes from under them. Such a resizing can happen,
11006 for instance, when which-func prints a long message while
11007 we are running fontification-functions. We're running these
11008 functions with safe_call which binds inhibit-redisplay to t. */
11009 if (!NILP (Vinhibit_redisplay))
11010 return false;
11012 /* Nil means don't try to resize. */
11013 if (NILP (Vresize_mini_windows)
11014 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11015 return false;
11017 if (!FRAME_MINIBUF_ONLY_P (f))
11019 struct it it;
11020 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11021 + WINDOW_PIXEL_HEIGHT (w));
11022 int unit = FRAME_LINE_HEIGHT (f);
11023 int height, max_height;
11024 struct text_pos start;
11025 struct buffer *old_current_buffer = NULL;
11027 if (current_buffer != XBUFFER (w->contents))
11029 old_current_buffer = current_buffer;
11030 set_buffer_internal (XBUFFER (w->contents));
11033 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11035 /* Compute the max. number of lines specified by the user. */
11036 if (FLOATP (Vmax_mini_window_height))
11037 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11038 else if (INTEGERP (Vmax_mini_window_height))
11039 max_height = XINT (Vmax_mini_window_height) * unit;
11040 else
11041 max_height = total_height / 4;
11043 /* Correct that max. height if it's bogus. */
11044 max_height = clip_to_bounds (unit, max_height, total_height);
11046 /* Find out the height of the text in the window. */
11047 if (it.line_wrap == TRUNCATE)
11048 height = unit;
11049 else
11051 last_height = 0;
11052 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11053 if (it.max_ascent == 0 && it.max_descent == 0)
11054 height = it.current_y + last_height;
11055 else
11056 height = it.current_y + it.max_ascent + it.max_descent;
11057 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11060 /* Compute a suitable window start. */
11061 if (height > max_height)
11063 height = (max_height / unit) * unit;
11064 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11065 move_it_vertically_backward (&it, height - unit);
11066 start = it.current.pos;
11068 else
11069 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11070 SET_MARKER_FROM_TEXT_POS (w->start, start);
11072 if (EQ (Vresize_mini_windows, Qgrow_only))
11074 /* Let it grow only, until we display an empty message, in which
11075 case the window shrinks again. */
11076 if (height > WINDOW_PIXEL_HEIGHT (w))
11078 int old_height = WINDOW_PIXEL_HEIGHT (w);
11080 FRAME_WINDOWS_FROZEN (f) = true;
11081 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11082 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11084 else if (height < WINDOW_PIXEL_HEIGHT (w)
11085 && (exact_p || BEGV == ZV))
11087 int old_height = WINDOW_PIXEL_HEIGHT (w);
11089 FRAME_WINDOWS_FROZEN (f) = false;
11090 shrink_mini_window (w, true);
11091 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11094 else
11096 /* Always resize to exact size needed. */
11097 if (height > WINDOW_PIXEL_HEIGHT (w))
11099 int old_height = WINDOW_PIXEL_HEIGHT (w);
11101 FRAME_WINDOWS_FROZEN (f) = true;
11102 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11103 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11105 else if (height < WINDOW_PIXEL_HEIGHT (w))
11107 int old_height = WINDOW_PIXEL_HEIGHT (w);
11109 FRAME_WINDOWS_FROZEN (f) = false;
11110 shrink_mini_window (w, true);
11112 if (height)
11114 FRAME_WINDOWS_FROZEN (f) = true;
11115 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11118 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11122 if (old_current_buffer)
11123 set_buffer_internal (old_current_buffer);
11126 return window_height_changed_p;
11130 /* Value is the current message, a string, or nil if there is no
11131 current message. */
11133 Lisp_Object
11134 current_message (void)
11136 Lisp_Object msg;
11138 if (!BUFFERP (echo_area_buffer[0]))
11139 msg = Qnil;
11140 else
11142 with_echo_area_buffer (0, 0, current_message_1,
11143 (intptr_t) &msg, Qnil);
11144 if (NILP (msg))
11145 echo_area_buffer[0] = Qnil;
11148 return msg;
11152 static bool
11153 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11155 intptr_t i1 = a1;
11156 Lisp_Object *msg = (Lisp_Object *) i1;
11158 if (Z > BEG)
11159 *msg = make_buffer_string (BEG, Z, true);
11160 else
11161 *msg = Qnil;
11162 return false;
11166 /* Push the current message on Vmessage_stack for later restoration
11167 by restore_message. Value is true if the current message isn't
11168 empty. This is a relatively infrequent operation, so it's not
11169 worth optimizing. */
11171 bool
11172 push_message (void)
11174 Lisp_Object msg = current_message ();
11175 Vmessage_stack = Fcons (msg, Vmessage_stack);
11176 return STRINGP (msg);
11180 /* Restore message display from the top of Vmessage_stack. */
11182 void
11183 restore_message (void)
11185 eassert (CONSP (Vmessage_stack));
11186 message3_nolog (XCAR (Vmessage_stack));
11190 /* Handler for unwind-protect calling pop_message. */
11192 void
11193 pop_message_unwind (void)
11195 /* Pop the top-most entry off Vmessage_stack. */
11196 eassert (CONSP (Vmessage_stack));
11197 Vmessage_stack = XCDR (Vmessage_stack);
11201 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11202 exits. If the stack is not empty, we have a missing pop_message
11203 somewhere. */
11205 void
11206 check_message_stack (void)
11208 if (!NILP (Vmessage_stack))
11209 emacs_abort ();
11213 /* Truncate to NCHARS what will be displayed in the echo area the next
11214 time we display it---but don't redisplay it now. */
11216 void
11217 truncate_echo_area (ptrdiff_t nchars)
11219 if (nchars == 0)
11220 echo_area_buffer[0] = Qnil;
11221 else if (!noninteractive
11222 && INTERACTIVE
11223 && !NILP (echo_area_buffer[0]))
11225 struct frame *sf = SELECTED_FRAME ();
11226 /* Error messages get reported properly by cmd_error, so this must be
11227 just an informative message; if the frame hasn't really been
11228 initialized yet, just toss it. */
11229 if (sf->glyphs_initialized_p)
11230 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11235 /* Helper function for truncate_echo_area. Truncate the current
11236 message to at most NCHARS characters. */
11238 static bool
11239 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11241 if (BEG + nchars < Z)
11242 del_range (BEG + nchars, Z);
11243 if (Z == BEG)
11244 echo_area_buffer[0] = Qnil;
11245 return false;
11248 /* Set the current message to STRING. */
11250 static void
11251 set_message (Lisp_Object string)
11253 eassert (STRINGP (string));
11255 message_enable_multibyte = STRING_MULTIBYTE (string);
11257 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11258 message_buf_print = false;
11259 help_echo_showing_p = false;
11261 if (STRINGP (Vdebug_on_message)
11262 && STRINGP (string)
11263 && fast_string_match (Vdebug_on_message, string) >= 0)
11264 call_debugger (list2 (Qerror, string));
11268 /* Helper function for set_message. First argument is ignored and second
11269 argument has the same meaning as for set_message.
11270 This function is called with the echo area buffer being current. */
11272 static bool
11273 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11275 eassert (STRINGP (string));
11277 /* Change multibyteness of the echo buffer appropriately. */
11278 if (message_enable_multibyte
11279 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11280 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11282 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11283 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11284 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11286 /* Insert new message at BEG. */
11287 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11289 /* This function takes care of single/multibyte conversion.
11290 We just have to ensure that the echo area buffer has the right
11291 setting of enable_multibyte_characters. */
11292 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11294 return false;
11298 /* Clear messages. CURRENT_P means clear the current message.
11299 LAST_DISPLAYED_P means clear the message last displayed. */
11301 void
11302 clear_message (bool current_p, bool last_displayed_p)
11304 if (current_p)
11306 echo_area_buffer[0] = Qnil;
11307 message_cleared_p = true;
11310 if (last_displayed_p)
11311 echo_area_buffer[1] = Qnil;
11313 message_buf_print = false;
11316 /* Clear garbaged frames.
11318 This function is used where the old redisplay called
11319 redraw_garbaged_frames which in turn called redraw_frame which in
11320 turn called clear_frame. The call to clear_frame was a source of
11321 flickering. I believe a clear_frame is not necessary. It should
11322 suffice in the new redisplay to invalidate all current matrices,
11323 and ensure a complete redisplay of all windows. */
11325 static void
11326 clear_garbaged_frames (void)
11328 if (frame_garbaged)
11330 Lisp_Object tail, frame;
11331 struct frame *sf = SELECTED_FRAME ();
11333 FOR_EACH_FRAME (tail, frame)
11335 struct frame *f = XFRAME (frame);
11337 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11339 if (f->resized_p
11340 /* It makes no sense to redraw a non-selected TTY
11341 frame, since that will actually clear the
11342 selected frame, and might leave the selected
11343 frame with corrupted display, if it happens not
11344 to be marked garbaged. */
11345 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11346 redraw_frame (f);
11347 else
11348 clear_current_matrices (f);
11349 fset_redisplay (f);
11350 f->garbaged = false;
11351 f->resized_p = false;
11355 frame_garbaged = false;
11360 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11361 selected_frame. */
11363 static void
11364 echo_area_display (bool update_frame_p)
11366 Lisp_Object mini_window;
11367 struct window *w;
11368 struct frame *f;
11369 bool window_height_changed_p = false;
11370 struct frame *sf = SELECTED_FRAME ();
11372 mini_window = FRAME_MINIBUF_WINDOW (sf);
11373 w = XWINDOW (mini_window);
11374 f = XFRAME (WINDOW_FRAME (w));
11376 /* Don't display if frame is invisible or not yet initialized. */
11377 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11378 return;
11380 #ifdef HAVE_WINDOW_SYSTEM
11381 /* When Emacs starts, selected_frame may be the initial terminal
11382 frame. If we let this through, a message would be displayed on
11383 the terminal. */
11384 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11385 return;
11386 #endif /* HAVE_WINDOW_SYSTEM */
11388 /* Redraw garbaged frames. */
11389 clear_garbaged_frames ();
11391 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11393 echo_area_window = mini_window;
11394 window_height_changed_p = display_echo_area (w);
11395 w->must_be_updated_p = true;
11397 /* Update the display, unless called from redisplay_internal.
11398 Also don't update the screen during redisplay itself. The
11399 update will happen at the end of redisplay, and an update
11400 here could cause confusion. */
11401 if (update_frame_p && !redisplaying_p)
11403 int n = 0;
11405 /* If the display update has been interrupted by pending
11406 input, update mode lines in the frame. Due to the
11407 pending input, it might have been that redisplay hasn't
11408 been called, so that mode lines above the echo area are
11409 garbaged. This looks odd, so we prevent it here. */
11410 if (!display_completed)
11411 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11413 if (window_height_changed_p
11414 /* Don't do this if Emacs is shutting down. Redisplay
11415 needs to run hooks. */
11416 && !NILP (Vrun_hooks))
11418 /* Must update other windows. Likewise as in other
11419 cases, don't let this update be interrupted by
11420 pending input. */
11421 ptrdiff_t count = SPECPDL_INDEX ();
11422 specbind (Qredisplay_dont_pause, Qt);
11423 fset_redisplay (f);
11424 redisplay_internal ();
11425 unbind_to (count, Qnil);
11427 else if (FRAME_WINDOW_P (f) && n == 0)
11429 /* Window configuration is the same as before.
11430 Can do with a display update of the echo area,
11431 unless we displayed some mode lines. */
11432 update_single_window (w);
11433 flush_frame (f);
11435 else
11436 update_frame (f, true, true);
11438 /* If cursor is in the echo area, make sure that the next
11439 redisplay displays the minibuffer, so that the cursor will
11440 be replaced with what the minibuffer wants. */
11441 if (cursor_in_echo_area)
11442 wset_redisplay (XWINDOW (mini_window));
11445 else if (!EQ (mini_window, selected_window))
11446 wset_redisplay (XWINDOW (mini_window));
11448 /* Last displayed message is now the current message. */
11449 echo_area_buffer[1] = echo_area_buffer[0];
11450 /* Inform read_char that we're not echoing. */
11451 echo_message_buffer = Qnil;
11453 /* Prevent redisplay optimization in redisplay_internal by resetting
11454 this_line_start_pos. This is done because the mini-buffer now
11455 displays the message instead of its buffer text. */
11456 if (EQ (mini_window, selected_window))
11457 CHARPOS (this_line_start_pos) = 0;
11459 if (window_height_changed_p)
11461 fset_redisplay (f);
11463 /* If window configuration was changed, frames may have been
11464 marked garbaged. Clear them or we will experience
11465 surprises wrt scrolling.
11466 FIXME: How/why/when? */
11467 clear_garbaged_frames ();
11471 /* True if W's buffer was changed but not saved. */
11473 static bool
11474 window_buffer_changed (struct window *w)
11476 struct buffer *b = XBUFFER (w->contents);
11478 eassert (BUFFER_LIVE_P (b));
11480 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11483 /* True if W has %c in its mode line and mode line should be updated. */
11485 static bool
11486 mode_line_update_needed (struct window *w)
11488 return (w->column_number_displayed != -1
11489 && !(PT == w->last_point && !window_outdated (w))
11490 && (w->column_number_displayed != current_column ()));
11493 /* True if window start of W is frozen and may not be changed during
11494 redisplay. */
11496 static bool
11497 window_frozen_p (struct window *w)
11499 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11501 Lisp_Object window;
11503 XSETWINDOW (window, w);
11504 if (MINI_WINDOW_P (w))
11505 return false;
11506 else if (EQ (window, selected_window))
11507 return false;
11508 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11509 && EQ (window, Vminibuf_scroll_window))
11510 /* This special window can't be frozen too. */
11511 return false;
11512 else
11513 return true;
11515 return false;
11518 /***********************************************************************
11519 Mode Lines and Frame Titles
11520 ***********************************************************************/
11522 /* A buffer for constructing non-propertized mode-line strings and
11523 frame titles in it; allocated from the heap in init_xdisp and
11524 resized as needed in store_mode_line_noprop_char. */
11526 static char *mode_line_noprop_buf;
11528 /* The buffer's end, and a current output position in it. */
11530 static char *mode_line_noprop_buf_end;
11531 static char *mode_line_noprop_ptr;
11533 #define MODE_LINE_NOPROP_LEN(start) \
11534 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11536 static enum {
11537 MODE_LINE_DISPLAY = 0,
11538 MODE_LINE_TITLE,
11539 MODE_LINE_NOPROP,
11540 MODE_LINE_STRING
11541 } mode_line_target;
11543 /* Alist that caches the results of :propertize.
11544 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11545 static Lisp_Object mode_line_proptrans_alist;
11547 /* List of strings making up the mode-line. */
11548 static Lisp_Object mode_line_string_list;
11550 /* Base face property when building propertized mode line string. */
11551 static Lisp_Object mode_line_string_face;
11552 static Lisp_Object mode_line_string_face_prop;
11555 /* Unwind data for mode line strings */
11557 static Lisp_Object Vmode_line_unwind_vector;
11559 static Lisp_Object
11560 format_mode_line_unwind_data (struct frame *target_frame,
11561 struct buffer *obuf,
11562 Lisp_Object owin,
11563 bool save_proptrans)
11565 Lisp_Object vector, tmp;
11567 /* Reduce consing by keeping one vector in
11568 Vwith_echo_area_save_vector. */
11569 vector = Vmode_line_unwind_vector;
11570 Vmode_line_unwind_vector = Qnil;
11572 if (NILP (vector))
11573 vector = Fmake_vector (make_number (10), Qnil);
11575 ASET (vector, 0, make_number (mode_line_target));
11576 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11577 ASET (vector, 2, mode_line_string_list);
11578 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11579 ASET (vector, 4, mode_line_string_face);
11580 ASET (vector, 5, mode_line_string_face_prop);
11582 if (obuf)
11583 XSETBUFFER (tmp, obuf);
11584 else
11585 tmp = Qnil;
11586 ASET (vector, 6, tmp);
11587 ASET (vector, 7, owin);
11588 if (target_frame)
11590 /* Similarly to `with-selected-window', if the operation selects
11591 a window on another frame, we must restore that frame's
11592 selected window, and (for a tty) the top-frame. */
11593 ASET (vector, 8, target_frame->selected_window);
11594 if (FRAME_TERMCAP_P (target_frame))
11595 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11598 return vector;
11601 static void
11602 unwind_format_mode_line (Lisp_Object vector)
11604 Lisp_Object old_window = AREF (vector, 7);
11605 Lisp_Object target_frame_window = AREF (vector, 8);
11606 Lisp_Object old_top_frame = AREF (vector, 9);
11608 mode_line_target = XINT (AREF (vector, 0));
11609 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11610 mode_line_string_list = AREF (vector, 2);
11611 if (! EQ (AREF (vector, 3), Qt))
11612 mode_line_proptrans_alist = AREF (vector, 3);
11613 mode_line_string_face = AREF (vector, 4);
11614 mode_line_string_face_prop = AREF (vector, 5);
11616 /* Select window before buffer, since it may change the buffer. */
11617 if (!NILP (old_window))
11619 /* If the operation that we are unwinding had selected a window
11620 on a different frame, reset its frame-selected-window. For a
11621 text terminal, reset its top-frame if necessary. */
11622 if (!NILP (target_frame_window))
11624 Lisp_Object frame
11625 = WINDOW_FRAME (XWINDOW (target_frame_window));
11627 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11628 Fselect_window (target_frame_window, Qt);
11630 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11631 Fselect_frame (old_top_frame, Qt);
11634 Fselect_window (old_window, Qt);
11637 if (!NILP (AREF (vector, 6)))
11639 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11640 ASET (vector, 6, Qnil);
11643 Vmode_line_unwind_vector = vector;
11647 /* Store a single character C for the frame title in mode_line_noprop_buf.
11648 Re-allocate mode_line_noprop_buf if necessary. */
11650 static void
11651 store_mode_line_noprop_char (char c)
11653 /* If output position has reached the end of the allocated buffer,
11654 increase the buffer's size. */
11655 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11657 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11658 ptrdiff_t size = len;
11659 mode_line_noprop_buf =
11660 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11661 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11662 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11665 *mode_line_noprop_ptr++ = c;
11669 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11670 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11671 characters that yield more columns than PRECISION; PRECISION <= 0
11672 means copy the whole string. Pad with spaces until FIELD_WIDTH
11673 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11674 pad. Called from display_mode_element when it is used to build a
11675 frame title. */
11677 static int
11678 store_mode_line_noprop (const char *string, int field_width, int precision)
11680 const unsigned char *str = (const unsigned char *) string;
11681 int n = 0;
11682 ptrdiff_t dummy, nbytes;
11684 /* Copy at most PRECISION chars from STR. */
11685 nbytes = strlen (string);
11686 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11687 while (nbytes--)
11688 store_mode_line_noprop_char (*str++);
11690 /* Fill up with spaces until FIELD_WIDTH reached. */
11691 while (field_width > 0
11692 && n < field_width)
11694 store_mode_line_noprop_char (' ');
11695 ++n;
11698 return n;
11701 /***********************************************************************
11702 Frame Titles
11703 ***********************************************************************/
11705 #ifdef HAVE_WINDOW_SYSTEM
11707 /* Set the title of FRAME, if it has changed. The title format is
11708 Vicon_title_format if FRAME is iconified, otherwise it is
11709 frame_title_format. */
11711 static void
11712 x_consider_frame_title (Lisp_Object frame)
11714 struct frame *f = XFRAME (frame);
11716 if ((FRAME_WINDOW_P (f)
11717 || FRAME_MINIBUF_ONLY_P (f)
11718 || f->explicit_name)
11719 && NILP (Fframe_parameter (frame, Qtooltip)))
11721 /* Do we have more than one visible frame on this X display? */
11722 Lisp_Object tail, other_frame, fmt;
11723 ptrdiff_t title_start;
11724 char *title;
11725 ptrdiff_t len;
11726 struct it it;
11727 ptrdiff_t count = SPECPDL_INDEX ();
11729 FOR_EACH_FRAME (tail, other_frame)
11731 struct frame *tf = XFRAME (other_frame);
11733 if (tf != f
11734 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11735 && !FRAME_MINIBUF_ONLY_P (tf)
11736 && !EQ (other_frame, tip_frame)
11737 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11738 break;
11741 /* Set global variable indicating that multiple frames exist. */
11742 multiple_frames = CONSP (tail);
11744 /* Switch to the buffer of selected window of the frame. Set up
11745 mode_line_target so that display_mode_element will output into
11746 mode_line_noprop_buf; then display the title. */
11747 record_unwind_protect (unwind_format_mode_line,
11748 format_mode_line_unwind_data
11749 (f, current_buffer, selected_window, false));
11750 /* select-frame calls resize_mini_window, which could resize the
11751 mini-window and by that undo the effect of this redisplay
11752 cycle wrt minibuffer and echo-area display. Binding
11753 inhibit-redisplay to t makes the call to resize_mini_window a
11754 no-op, thus avoiding the adverse side effects. */
11755 specbind (Qinhibit_redisplay, Qt);
11757 Fselect_window (f->selected_window, Qt);
11758 set_buffer_internal_1
11759 (XBUFFER (XWINDOW (f->selected_window)->contents));
11760 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11762 mode_line_target = MODE_LINE_TITLE;
11763 title_start = MODE_LINE_NOPROP_LEN (0);
11764 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11765 NULL, DEFAULT_FACE_ID);
11766 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11767 len = MODE_LINE_NOPROP_LEN (title_start);
11768 title = mode_line_noprop_buf + title_start;
11769 unbind_to (count, Qnil);
11771 /* Set the title only if it's changed. This avoids consing in
11772 the common case where it hasn't. (If it turns out that we've
11773 already wasted too much time by walking through the list with
11774 display_mode_element, then we might need to optimize at a
11775 higher level than this.) */
11776 if (! STRINGP (f->name)
11777 || SBYTES (f->name) != len
11778 || memcmp (title, SDATA (f->name), len) != 0)
11779 x_implicitly_set_name (f, make_string (title, len), Qnil);
11783 #endif /* not HAVE_WINDOW_SYSTEM */
11786 /***********************************************************************
11787 Menu Bars
11788 ***********************************************************************/
11790 /* True if we will not redisplay all visible windows. */
11791 #define REDISPLAY_SOME_P() \
11792 ((windows_or_buffers_changed == 0 \
11793 || windows_or_buffers_changed == REDISPLAY_SOME) \
11794 && (update_mode_lines == 0 \
11795 || update_mode_lines == REDISPLAY_SOME))
11797 /* Prepare for redisplay by updating menu-bar item lists when
11798 appropriate. This can call eval. */
11800 static void
11801 prepare_menu_bars (void)
11803 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11804 bool some_windows = REDISPLAY_SOME_P ();
11805 Lisp_Object tooltip_frame;
11807 #ifdef HAVE_WINDOW_SYSTEM
11808 tooltip_frame = tip_frame;
11809 #else
11810 tooltip_frame = Qnil;
11811 #endif
11813 if (FUNCTIONP (Vpre_redisplay_function))
11815 Lisp_Object windows = all_windows ? Qt : Qnil;
11816 if (all_windows && some_windows)
11818 Lisp_Object ws = window_list ();
11819 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11821 Lisp_Object this = XCAR (ws);
11822 struct window *w = XWINDOW (this);
11823 if (w->redisplay
11824 || XFRAME (w->frame)->redisplay
11825 || XBUFFER (w->contents)->text->redisplay)
11827 windows = Fcons (this, windows);
11831 safe__call1 (true, Vpre_redisplay_function, windows);
11834 /* Update all frame titles based on their buffer names, etc. We do
11835 this before the menu bars so that the buffer-menu will show the
11836 up-to-date frame titles. */
11837 #ifdef HAVE_WINDOW_SYSTEM
11838 if (all_windows)
11840 Lisp_Object tail, frame;
11842 FOR_EACH_FRAME (tail, frame)
11844 struct frame *f = XFRAME (frame);
11845 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11846 if (some_windows
11847 && !f->redisplay
11848 && !w->redisplay
11849 && !XBUFFER (w->contents)->text->redisplay)
11850 continue;
11852 if (!EQ (frame, tooltip_frame)
11853 && (FRAME_ICONIFIED_P (f)
11854 || FRAME_VISIBLE_P (f) == 1
11855 /* Exclude TTY frames that are obscured because they
11856 are not the top frame on their console. This is
11857 because x_consider_frame_title actually switches
11858 to the frame, which for TTY frames means it is
11859 marked as garbaged, and will be completely
11860 redrawn on the next redisplay cycle. This causes
11861 TTY frames to be completely redrawn, when there
11862 are more than one of them, even though nothing
11863 should be changed on display. */
11864 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11865 x_consider_frame_title (frame);
11868 #endif /* HAVE_WINDOW_SYSTEM */
11870 /* Update the menu bar item lists, if appropriate. This has to be
11871 done before any actual redisplay or generation of display lines. */
11873 if (all_windows)
11875 Lisp_Object tail, frame;
11876 ptrdiff_t count = SPECPDL_INDEX ();
11877 /* True means that update_menu_bar has run its hooks
11878 so any further calls to update_menu_bar shouldn't do so again. */
11879 bool menu_bar_hooks_run = false;
11881 record_unwind_save_match_data ();
11883 FOR_EACH_FRAME (tail, frame)
11885 struct frame *f = XFRAME (frame);
11886 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11888 /* Ignore tooltip frame. */
11889 if (EQ (frame, tooltip_frame))
11890 continue;
11892 if (some_windows
11893 && !f->redisplay
11894 && !w->redisplay
11895 && !XBUFFER (w->contents)->text->redisplay)
11896 continue;
11898 run_window_size_change_functions (frame);
11899 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11900 #ifdef HAVE_WINDOW_SYSTEM
11901 update_tool_bar (f, false);
11902 #endif
11905 unbind_to (count, Qnil);
11907 else
11909 struct frame *sf = SELECTED_FRAME ();
11910 update_menu_bar (sf, true, false);
11911 #ifdef HAVE_WINDOW_SYSTEM
11912 update_tool_bar (sf, true);
11913 #endif
11918 /* Update the menu bar item list for frame F. This has to be done
11919 before we start to fill in any display lines, because it can call
11920 eval.
11922 If SAVE_MATCH_DATA, we must save and restore it here.
11924 If HOOKS_RUN, a previous call to update_menu_bar
11925 already ran the menu bar hooks for this redisplay, so there
11926 is no need to run them again. The return value is the
11927 updated value of this flag, to pass to the next call. */
11929 static bool
11930 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11932 Lisp_Object window;
11933 struct window *w;
11935 /* If called recursively during a menu update, do nothing. This can
11936 happen when, for instance, an activate-menubar-hook causes a
11937 redisplay. */
11938 if (inhibit_menubar_update)
11939 return hooks_run;
11941 window = FRAME_SELECTED_WINDOW (f);
11942 w = XWINDOW (window);
11944 if (FRAME_WINDOW_P (f)
11946 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11947 || defined (HAVE_NS) || defined (USE_GTK)
11948 FRAME_EXTERNAL_MENU_BAR (f)
11949 #else
11950 FRAME_MENU_BAR_LINES (f) > 0
11951 #endif
11952 : FRAME_MENU_BAR_LINES (f) > 0)
11954 /* If the user has switched buffers or windows, we need to
11955 recompute to reflect the new bindings. But we'll
11956 recompute when update_mode_lines is set too; that means
11957 that people can use force-mode-line-update to request
11958 that the menu bar be recomputed. The adverse effect on
11959 the rest of the redisplay algorithm is about the same as
11960 windows_or_buffers_changed anyway. */
11961 if (windows_or_buffers_changed
11962 /* This used to test w->update_mode_line, but we believe
11963 there is no need to recompute the menu in that case. */
11964 || update_mode_lines
11965 || window_buffer_changed (w))
11967 struct buffer *prev = current_buffer;
11968 ptrdiff_t count = SPECPDL_INDEX ();
11970 specbind (Qinhibit_menubar_update, Qt);
11972 set_buffer_internal_1 (XBUFFER (w->contents));
11973 if (save_match_data)
11974 record_unwind_save_match_data ();
11975 if (NILP (Voverriding_local_map_menu_flag))
11977 specbind (Qoverriding_terminal_local_map, Qnil);
11978 specbind (Qoverriding_local_map, Qnil);
11981 if (!hooks_run)
11983 /* Run the Lucid hook. */
11984 safe_run_hooks (Qactivate_menubar_hook);
11986 /* If it has changed current-menubar from previous value,
11987 really recompute the menu-bar from the value. */
11988 if (! NILP (Vlucid_menu_bar_dirty_flag))
11989 call0 (Qrecompute_lucid_menubar);
11991 safe_run_hooks (Qmenu_bar_update_hook);
11993 hooks_run = true;
11996 XSETFRAME (Vmenu_updating_frame, f);
11997 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11999 /* Redisplay the menu bar in case we changed it. */
12000 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12001 || defined (HAVE_NS) || defined (USE_GTK)
12002 if (FRAME_WINDOW_P (f))
12004 #if defined (HAVE_NS)
12005 /* All frames on Mac OS share the same menubar. So only
12006 the selected frame should be allowed to set it. */
12007 if (f == SELECTED_FRAME ())
12008 #endif
12009 set_frame_menubar (f, false, false);
12011 else
12012 /* On a terminal screen, the menu bar is an ordinary screen
12013 line, and this makes it get updated. */
12014 w->update_mode_line = true;
12015 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12016 /* In the non-toolkit version, the menu bar is an ordinary screen
12017 line, and this makes it get updated. */
12018 w->update_mode_line = true;
12019 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12021 unbind_to (count, Qnil);
12022 set_buffer_internal_1 (prev);
12026 return hooks_run;
12029 /***********************************************************************
12030 Tool-bars
12031 ***********************************************************************/
12033 #ifdef HAVE_WINDOW_SYSTEM
12035 /* Select `frame' temporarily without running all the code in
12036 do_switch_frame.
12037 FIXME: Maybe do_switch_frame should be trimmed down similarly
12038 when `norecord' is set. */
12039 static void
12040 fast_set_selected_frame (Lisp_Object frame)
12042 if (!EQ (selected_frame, frame))
12044 selected_frame = frame;
12045 selected_window = XFRAME (frame)->selected_window;
12049 /* Update the tool-bar item list for frame F. This has to be done
12050 before we start to fill in any display lines. Called from
12051 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12052 and restore it here. */
12054 static void
12055 update_tool_bar (struct frame *f, bool save_match_data)
12057 #if defined (USE_GTK) || defined (HAVE_NS)
12058 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12059 #else
12060 bool do_update = (WINDOWP (f->tool_bar_window)
12061 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12062 #endif
12064 if (do_update)
12066 Lisp_Object window;
12067 struct window *w;
12069 window = FRAME_SELECTED_WINDOW (f);
12070 w = XWINDOW (window);
12072 /* If the user has switched buffers or windows, we need to
12073 recompute to reflect the new bindings. But we'll
12074 recompute when update_mode_lines is set too; that means
12075 that people can use force-mode-line-update to request
12076 that the menu bar be recomputed. The adverse effect on
12077 the rest of the redisplay algorithm is about the same as
12078 windows_or_buffers_changed anyway. */
12079 if (windows_or_buffers_changed
12080 || w->update_mode_line
12081 || update_mode_lines
12082 || window_buffer_changed (w))
12084 struct buffer *prev = current_buffer;
12085 ptrdiff_t count = SPECPDL_INDEX ();
12086 Lisp_Object frame, new_tool_bar;
12087 int new_n_tool_bar;
12089 /* Set current_buffer to the buffer of the selected
12090 window of the frame, so that we get the right local
12091 keymaps. */
12092 set_buffer_internal_1 (XBUFFER (w->contents));
12094 /* Save match data, if we must. */
12095 if (save_match_data)
12096 record_unwind_save_match_data ();
12098 /* Make sure that we don't accidentally use bogus keymaps. */
12099 if (NILP (Voverriding_local_map_menu_flag))
12101 specbind (Qoverriding_terminal_local_map, Qnil);
12102 specbind (Qoverriding_local_map, Qnil);
12105 /* We must temporarily set the selected frame to this frame
12106 before calling tool_bar_items, because the calculation of
12107 the tool-bar keymap uses the selected frame (see
12108 `tool-bar-make-keymap' in tool-bar.el). */
12109 eassert (EQ (selected_window,
12110 /* Since we only explicitly preserve selected_frame,
12111 check that selected_window would be redundant. */
12112 XFRAME (selected_frame)->selected_window));
12113 record_unwind_protect (fast_set_selected_frame, selected_frame);
12114 XSETFRAME (frame, f);
12115 fast_set_selected_frame (frame);
12117 /* Build desired tool-bar items from keymaps. */
12118 new_tool_bar
12119 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12120 &new_n_tool_bar);
12122 /* Redisplay the tool-bar if we changed it. */
12123 if (new_n_tool_bar != f->n_tool_bar_items
12124 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12126 /* Redisplay that happens asynchronously due to an expose event
12127 may access f->tool_bar_items. Make sure we update both
12128 variables within BLOCK_INPUT so no such event interrupts. */
12129 block_input ();
12130 fset_tool_bar_items (f, new_tool_bar);
12131 f->n_tool_bar_items = new_n_tool_bar;
12132 w->update_mode_line = true;
12133 unblock_input ();
12136 unbind_to (count, Qnil);
12137 set_buffer_internal_1 (prev);
12142 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12144 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12145 F's desired tool-bar contents. F->tool_bar_items must have
12146 been set up previously by calling prepare_menu_bars. */
12148 static void
12149 build_desired_tool_bar_string (struct frame *f)
12151 int i, size, size_needed;
12152 Lisp_Object image, plist;
12154 image = plist = Qnil;
12156 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12157 Otherwise, make a new string. */
12159 /* The size of the string we might be able to reuse. */
12160 size = (STRINGP (f->desired_tool_bar_string)
12161 ? SCHARS (f->desired_tool_bar_string)
12162 : 0);
12164 /* We need one space in the string for each image. */
12165 size_needed = f->n_tool_bar_items;
12167 /* Reuse f->desired_tool_bar_string, if possible. */
12168 if (size < size_needed || NILP (f->desired_tool_bar_string))
12169 fset_desired_tool_bar_string
12170 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12171 else
12173 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12174 Fremove_text_properties (make_number (0), make_number (size),
12175 props, f->desired_tool_bar_string);
12178 /* Put a `display' property on the string for the images to display,
12179 put a `menu_item' property on tool-bar items with a value that
12180 is the index of the item in F's tool-bar item vector. */
12181 for (i = 0; i < f->n_tool_bar_items; ++i)
12183 #define PROP(IDX) \
12184 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12186 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12187 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12188 int hmargin, vmargin, relief, idx, end;
12190 /* If image is a vector, choose the image according to the
12191 button state. */
12192 image = PROP (TOOL_BAR_ITEM_IMAGES);
12193 if (VECTORP (image))
12195 if (enabled_p)
12196 idx = (selected_p
12197 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12198 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12199 else
12200 idx = (selected_p
12201 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12202 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12204 eassert (ASIZE (image) >= idx);
12205 image = AREF (image, idx);
12207 else
12208 idx = -1;
12210 /* Ignore invalid image specifications. */
12211 if (!valid_image_p (image))
12212 continue;
12214 /* Display the tool-bar button pressed, or depressed. */
12215 plist = Fcopy_sequence (XCDR (image));
12217 /* Compute margin and relief to draw. */
12218 relief = (tool_bar_button_relief >= 0
12219 ? tool_bar_button_relief
12220 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12221 hmargin = vmargin = relief;
12223 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12224 INT_MAX - max (hmargin, vmargin)))
12226 hmargin += XFASTINT (Vtool_bar_button_margin);
12227 vmargin += XFASTINT (Vtool_bar_button_margin);
12229 else if (CONSP (Vtool_bar_button_margin))
12231 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12232 INT_MAX - hmargin))
12233 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12235 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12236 INT_MAX - vmargin))
12237 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12240 if (auto_raise_tool_bar_buttons_p)
12242 /* Add a `:relief' property to the image spec if the item is
12243 selected. */
12244 if (selected_p)
12246 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12247 hmargin -= relief;
12248 vmargin -= relief;
12251 else
12253 /* If image is selected, display it pressed, i.e. with a
12254 negative relief. If it's not selected, display it with a
12255 raised relief. */
12256 plist = Fplist_put (plist, QCrelief,
12257 (selected_p
12258 ? make_number (-relief)
12259 : make_number (relief)));
12260 hmargin -= relief;
12261 vmargin -= relief;
12264 /* Put a margin around the image. */
12265 if (hmargin || vmargin)
12267 if (hmargin == vmargin)
12268 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12269 else
12270 plist = Fplist_put (plist, QCmargin,
12271 Fcons (make_number (hmargin),
12272 make_number (vmargin)));
12275 /* If button is not enabled, and we don't have special images
12276 for the disabled state, make the image appear disabled by
12277 applying an appropriate algorithm to it. */
12278 if (!enabled_p && idx < 0)
12279 plist = Fplist_put (plist, QCconversion, Qdisabled);
12281 /* Put a `display' text property on the string for the image to
12282 display. Put a `menu-item' property on the string that gives
12283 the start of this item's properties in the tool-bar items
12284 vector. */
12285 image = Fcons (Qimage, plist);
12286 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12287 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12289 /* Let the last image hide all remaining spaces in the tool bar
12290 string. The string can be longer than needed when we reuse a
12291 previous string. */
12292 if (i + 1 == f->n_tool_bar_items)
12293 end = SCHARS (f->desired_tool_bar_string);
12294 else
12295 end = i + 1;
12296 Fadd_text_properties (make_number (i), make_number (end),
12297 props, f->desired_tool_bar_string);
12298 #undef PROP
12303 /* Display one line of the tool-bar of frame IT->f.
12305 HEIGHT specifies the desired height of the tool-bar line.
12306 If the actual height of the glyph row is less than HEIGHT, the
12307 row's height is increased to HEIGHT, and the icons are centered
12308 vertically in the new height.
12310 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12311 count a final empty row in case the tool-bar width exactly matches
12312 the window width.
12315 static void
12316 display_tool_bar_line (struct it *it, int height)
12318 struct glyph_row *row = it->glyph_row;
12319 int max_x = it->last_visible_x;
12320 struct glyph *last;
12322 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12323 clear_glyph_row (row);
12324 row->enabled_p = true;
12325 row->y = it->current_y;
12327 /* Note that this isn't made use of if the face hasn't a box,
12328 so there's no need to check the face here. */
12329 it->start_of_box_run_p = true;
12331 while (it->current_x < max_x)
12333 int x, n_glyphs_before, i, nglyphs;
12334 struct it it_before;
12336 /* Get the next display element. */
12337 if (!get_next_display_element (it))
12339 /* Don't count empty row if we are counting needed tool-bar lines. */
12340 if (height < 0 && !it->hpos)
12341 return;
12342 break;
12345 /* Produce glyphs. */
12346 n_glyphs_before = row->used[TEXT_AREA];
12347 it_before = *it;
12349 PRODUCE_GLYPHS (it);
12351 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12352 i = 0;
12353 x = it_before.current_x;
12354 while (i < nglyphs)
12356 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12358 if (x + glyph->pixel_width > max_x)
12360 /* Glyph doesn't fit on line. Backtrack. */
12361 row->used[TEXT_AREA] = n_glyphs_before;
12362 *it = it_before;
12363 /* If this is the only glyph on this line, it will never fit on the
12364 tool-bar, so skip it. But ensure there is at least one glyph,
12365 so we don't accidentally disable the tool-bar. */
12366 if (n_glyphs_before == 0
12367 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12368 break;
12369 goto out;
12372 ++it->hpos;
12373 x += glyph->pixel_width;
12374 ++i;
12377 /* Stop at line end. */
12378 if (ITERATOR_AT_END_OF_LINE_P (it))
12379 break;
12381 set_iterator_to_next (it, true);
12384 out:;
12386 row->displays_text_p = row->used[TEXT_AREA] != 0;
12388 /* Use default face for the border below the tool bar.
12390 FIXME: When auto-resize-tool-bars is grow-only, there is
12391 no additional border below the possibly empty tool-bar lines.
12392 So to make the extra empty lines look "normal", we have to
12393 use the tool-bar face for the border too. */
12394 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12395 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12396 it->face_id = DEFAULT_FACE_ID;
12398 extend_face_to_end_of_line (it);
12399 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12400 last->right_box_line_p = true;
12401 if (last == row->glyphs[TEXT_AREA])
12402 last->left_box_line_p = true;
12404 /* Make line the desired height and center it vertically. */
12405 if ((height -= it->max_ascent + it->max_descent) > 0)
12407 /* Don't add more than one line height. */
12408 height %= FRAME_LINE_HEIGHT (it->f);
12409 it->max_ascent += height / 2;
12410 it->max_descent += (height + 1) / 2;
12413 compute_line_metrics (it);
12415 /* If line is empty, make it occupy the rest of the tool-bar. */
12416 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12418 row->height = row->phys_height = it->last_visible_y - row->y;
12419 row->visible_height = row->height;
12420 row->ascent = row->phys_ascent = 0;
12421 row->extra_line_spacing = 0;
12424 row->full_width_p = true;
12425 row->continued_p = false;
12426 row->truncated_on_left_p = false;
12427 row->truncated_on_right_p = false;
12429 it->current_x = it->hpos = 0;
12430 it->current_y += row->height;
12431 ++it->vpos;
12432 ++it->glyph_row;
12436 /* Value is the number of pixels needed to make all tool-bar items of
12437 frame F visible. The actual number of glyph rows needed is
12438 returned in *N_ROWS if non-NULL. */
12439 static int
12440 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12442 struct window *w = XWINDOW (f->tool_bar_window);
12443 struct it it;
12444 /* tool_bar_height is called from redisplay_tool_bar after building
12445 the desired matrix, so use (unused) mode-line row as temporary row to
12446 avoid destroying the first tool-bar row. */
12447 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12449 /* Initialize an iterator for iteration over
12450 F->desired_tool_bar_string in the tool-bar window of frame F. */
12451 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12452 temp_row->reversed_p = false;
12453 it.first_visible_x = 0;
12454 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12455 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12456 it.paragraph_embedding = L2R;
12458 while (!ITERATOR_AT_END_P (&it))
12460 clear_glyph_row (temp_row);
12461 it.glyph_row = temp_row;
12462 display_tool_bar_line (&it, -1);
12464 clear_glyph_row (temp_row);
12466 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12467 if (n_rows)
12468 *n_rows = it.vpos > 0 ? it.vpos : -1;
12470 if (pixelwise)
12471 return it.current_y;
12472 else
12473 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12476 #endif /* !USE_GTK && !HAVE_NS */
12478 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12479 0, 2, 0,
12480 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12481 If FRAME is nil or omitted, use the selected frame. Optional argument
12482 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12483 (Lisp_Object frame, Lisp_Object pixelwise)
12485 int height = 0;
12487 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12488 struct frame *f = decode_any_frame (frame);
12490 if (WINDOWP (f->tool_bar_window)
12491 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12493 update_tool_bar (f, true);
12494 if (f->n_tool_bar_items)
12496 build_desired_tool_bar_string (f);
12497 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12500 #endif
12502 return make_number (height);
12506 /* Display the tool-bar of frame F. Value is true if tool-bar's
12507 height should be changed. */
12508 static bool
12509 redisplay_tool_bar (struct frame *f)
12511 f->tool_bar_redisplayed = true;
12512 #if defined (USE_GTK) || defined (HAVE_NS)
12514 if (FRAME_EXTERNAL_TOOL_BAR (f))
12515 update_frame_tool_bar (f);
12516 return false;
12518 #else /* !USE_GTK && !HAVE_NS */
12520 struct window *w;
12521 struct it it;
12522 struct glyph_row *row;
12524 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12525 do anything. This means you must start with tool-bar-lines
12526 non-zero to get the auto-sizing effect. Or in other words, you
12527 can turn off tool-bars by specifying tool-bar-lines zero. */
12528 if (!WINDOWP (f->tool_bar_window)
12529 || (w = XWINDOW (f->tool_bar_window),
12530 WINDOW_TOTAL_LINES (w) == 0))
12531 return false;
12533 /* Set up an iterator for the tool-bar window. */
12534 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12535 it.first_visible_x = 0;
12536 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12537 row = it.glyph_row;
12538 row->reversed_p = false;
12540 /* Build a string that represents the contents of the tool-bar. */
12541 build_desired_tool_bar_string (f);
12542 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12543 /* FIXME: This should be controlled by a user option. But it
12544 doesn't make sense to have an R2L tool bar if the menu bar cannot
12545 be drawn also R2L, and making the menu bar R2L is tricky due
12546 toolkit-specific code that implements it. If an R2L tool bar is
12547 ever supported, display_tool_bar_line should also be augmented to
12548 call unproduce_glyphs like display_line and display_string
12549 do. */
12550 it.paragraph_embedding = L2R;
12552 if (f->n_tool_bar_rows == 0)
12554 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12556 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12558 x_change_tool_bar_height (f, new_height);
12559 frame_default_tool_bar_height = new_height;
12560 /* Always do that now. */
12561 clear_glyph_matrix (w->desired_matrix);
12562 f->fonts_changed = true;
12563 return true;
12567 /* Display as many lines as needed to display all tool-bar items. */
12569 if (f->n_tool_bar_rows > 0)
12571 int border, rows, height, extra;
12573 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12574 border = XINT (Vtool_bar_border);
12575 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12576 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12577 else if (EQ (Vtool_bar_border, Qborder_width))
12578 border = f->border_width;
12579 else
12580 border = 0;
12581 if (border < 0)
12582 border = 0;
12584 rows = f->n_tool_bar_rows;
12585 height = max (1, (it.last_visible_y - border) / rows);
12586 extra = it.last_visible_y - border - height * rows;
12588 while (it.current_y < it.last_visible_y)
12590 int h = 0;
12591 if (extra > 0 && rows-- > 0)
12593 h = (extra + rows - 1) / rows;
12594 extra -= h;
12596 display_tool_bar_line (&it, height + h);
12599 else
12601 while (it.current_y < it.last_visible_y)
12602 display_tool_bar_line (&it, 0);
12605 /* It doesn't make much sense to try scrolling in the tool-bar
12606 window, so don't do it. */
12607 w->desired_matrix->no_scrolling_p = true;
12608 w->must_be_updated_p = true;
12610 if (!NILP (Vauto_resize_tool_bars))
12612 bool change_height_p = true;
12614 /* If we couldn't display everything, change the tool-bar's
12615 height if there is room for more. */
12616 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12617 change_height_p = true;
12619 /* We subtract 1 because display_tool_bar_line advances the
12620 glyph_row pointer before returning to its caller. We want to
12621 examine the last glyph row produced by
12622 display_tool_bar_line. */
12623 row = it.glyph_row - 1;
12625 /* If there are blank lines at the end, except for a partially
12626 visible blank line at the end that is smaller than
12627 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12628 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12629 && row->height >= FRAME_LINE_HEIGHT (f))
12630 change_height_p = true;
12632 /* If row displays tool-bar items, but is partially visible,
12633 change the tool-bar's height. */
12634 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12635 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12636 change_height_p = true;
12638 /* Resize windows as needed by changing the `tool-bar-lines'
12639 frame parameter. */
12640 if (change_height_p)
12642 int nrows;
12643 int new_height = tool_bar_height (f, &nrows, true);
12645 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12646 && !f->minimize_tool_bar_window_p)
12647 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12648 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12649 f->minimize_tool_bar_window_p = false;
12651 if (change_height_p)
12653 x_change_tool_bar_height (f, new_height);
12654 frame_default_tool_bar_height = new_height;
12655 clear_glyph_matrix (w->desired_matrix);
12656 f->n_tool_bar_rows = nrows;
12657 f->fonts_changed = true;
12659 return true;
12664 f->minimize_tool_bar_window_p = false;
12665 return false;
12667 #endif /* USE_GTK || HAVE_NS */
12670 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12672 /* Get information about the tool-bar item which is displayed in GLYPH
12673 on frame F. Return in *PROP_IDX the index where tool-bar item
12674 properties start in F->tool_bar_items. Value is false if
12675 GLYPH doesn't display a tool-bar item. */
12677 static bool
12678 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12680 Lisp_Object prop;
12681 int charpos;
12683 /* This function can be called asynchronously, which means we must
12684 exclude any possibility that Fget_text_property signals an
12685 error. */
12686 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12687 charpos = max (0, charpos);
12689 /* Get the text property `menu-item' at pos. The value of that
12690 property is the start index of this item's properties in
12691 F->tool_bar_items. */
12692 prop = Fget_text_property (make_number (charpos),
12693 Qmenu_item, f->current_tool_bar_string);
12694 if (! INTEGERP (prop))
12695 return false;
12696 *prop_idx = XINT (prop);
12697 return true;
12701 /* Get information about the tool-bar item at position X/Y on frame F.
12702 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12703 the current matrix of the tool-bar window of F, or NULL if not
12704 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12705 item in F->tool_bar_items. Value is
12707 -1 if X/Y is not on a tool-bar item
12708 0 if X/Y is on the same item that was highlighted before.
12709 1 otherwise. */
12711 static int
12712 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12713 int *hpos, int *vpos, int *prop_idx)
12715 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12716 struct window *w = XWINDOW (f->tool_bar_window);
12717 int area;
12719 /* Find the glyph under X/Y. */
12720 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12721 if (*glyph == NULL)
12722 return -1;
12724 /* Get the start of this tool-bar item's properties in
12725 f->tool_bar_items. */
12726 if (!tool_bar_item_info (f, *glyph, prop_idx))
12727 return -1;
12729 /* Is mouse on the highlighted item? */
12730 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12731 && *vpos >= hlinfo->mouse_face_beg_row
12732 && *vpos <= hlinfo->mouse_face_end_row
12733 && (*vpos > hlinfo->mouse_face_beg_row
12734 || *hpos >= hlinfo->mouse_face_beg_col)
12735 && (*vpos < hlinfo->mouse_face_end_row
12736 || *hpos < hlinfo->mouse_face_end_col
12737 || hlinfo->mouse_face_past_end))
12738 return 0;
12740 return 1;
12744 /* EXPORT:
12745 Handle mouse button event on the tool-bar of frame F, at
12746 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12747 false for button release. MODIFIERS is event modifiers for button
12748 release. */
12750 void
12751 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12752 int modifiers)
12754 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12755 struct window *w = XWINDOW (f->tool_bar_window);
12756 int hpos, vpos, prop_idx;
12757 struct glyph *glyph;
12758 Lisp_Object enabled_p;
12759 int ts;
12761 /* If not on the highlighted tool-bar item, and mouse-highlight is
12762 non-nil, return. This is so we generate the tool-bar button
12763 click only when the mouse button is released on the same item as
12764 where it was pressed. However, when mouse-highlight is disabled,
12765 generate the click when the button is released regardless of the
12766 highlight, since tool-bar items are not highlighted in that
12767 case. */
12768 frame_to_window_pixel_xy (w, &x, &y);
12769 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12770 if (ts == -1
12771 || (ts != 0 && !NILP (Vmouse_highlight)))
12772 return;
12774 /* When mouse-highlight is off, generate the click for the item
12775 where the button was pressed, disregarding where it was
12776 released. */
12777 if (NILP (Vmouse_highlight) && !down_p)
12778 prop_idx = f->last_tool_bar_item;
12780 /* If item is disabled, do nothing. */
12781 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12782 if (NILP (enabled_p))
12783 return;
12785 if (down_p)
12787 /* Show item in pressed state. */
12788 if (!NILP (Vmouse_highlight))
12789 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12790 f->last_tool_bar_item = prop_idx;
12792 else
12794 Lisp_Object key, frame;
12795 struct input_event event;
12796 EVENT_INIT (event);
12798 /* Show item in released state. */
12799 if (!NILP (Vmouse_highlight))
12800 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12802 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12804 XSETFRAME (frame, f);
12805 event.kind = TOOL_BAR_EVENT;
12806 event.frame_or_window = frame;
12807 event.arg = frame;
12808 kbd_buffer_store_event (&event);
12810 event.kind = TOOL_BAR_EVENT;
12811 event.frame_or_window = frame;
12812 event.arg = key;
12813 event.modifiers = modifiers;
12814 kbd_buffer_store_event (&event);
12815 f->last_tool_bar_item = -1;
12820 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12821 tool-bar window-relative coordinates X/Y. Called from
12822 note_mouse_highlight. */
12824 static void
12825 note_tool_bar_highlight (struct frame *f, int x, int y)
12827 Lisp_Object window = f->tool_bar_window;
12828 struct window *w = XWINDOW (window);
12829 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12830 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12831 int hpos, vpos;
12832 struct glyph *glyph;
12833 struct glyph_row *row;
12834 int i;
12835 Lisp_Object enabled_p;
12836 int prop_idx;
12837 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12838 bool mouse_down_p;
12839 int rc;
12841 /* Function note_mouse_highlight is called with negative X/Y
12842 values when mouse moves outside of the frame. */
12843 if (x <= 0 || y <= 0)
12845 clear_mouse_face (hlinfo);
12846 return;
12849 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12850 if (rc < 0)
12852 /* Not on tool-bar item. */
12853 clear_mouse_face (hlinfo);
12854 return;
12856 else if (rc == 0)
12857 /* On same tool-bar item as before. */
12858 goto set_help_echo;
12860 clear_mouse_face (hlinfo);
12862 /* Mouse is down, but on different tool-bar item? */
12863 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12864 && f == dpyinfo->last_mouse_frame);
12866 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12867 return;
12869 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12871 /* If tool-bar item is not enabled, don't highlight it. */
12872 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12873 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12875 /* Compute the x-position of the glyph. In front and past the
12876 image is a space. We include this in the highlighted area. */
12877 row = MATRIX_ROW (w->current_matrix, vpos);
12878 for (i = x = 0; i < hpos; ++i)
12879 x += row->glyphs[TEXT_AREA][i].pixel_width;
12881 /* Record this as the current active region. */
12882 hlinfo->mouse_face_beg_col = hpos;
12883 hlinfo->mouse_face_beg_row = vpos;
12884 hlinfo->mouse_face_beg_x = x;
12885 hlinfo->mouse_face_past_end = false;
12887 hlinfo->mouse_face_end_col = hpos + 1;
12888 hlinfo->mouse_face_end_row = vpos;
12889 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12890 hlinfo->mouse_face_window = window;
12891 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12893 /* Display it as active. */
12894 show_mouse_face (hlinfo, draw);
12897 set_help_echo:
12899 /* Set help_echo_string to a help string to display for this tool-bar item.
12900 XTread_socket does the rest. */
12901 help_echo_object = help_echo_window = Qnil;
12902 help_echo_pos = -1;
12903 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12904 if (NILP (help_echo_string))
12905 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12908 #endif /* !USE_GTK && !HAVE_NS */
12910 #endif /* HAVE_WINDOW_SYSTEM */
12914 /************************************************************************
12915 Horizontal scrolling
12916 ************************************************************************/
12918 /* For all leaf windows in the window tree rooted at WINDOW, set their
12919 hscroll value so that PT is (i) visible in the window, and (ii) so
12920 that it is not within a certain margin at the window's left and
12921 right border. Value is true if any window's hscroll has been
12922 changed. */
12924 static bool
12925 hscroll_window_tree (Lisp_Object window)
12927 bool hscrolled_p = false;
12928 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12929 int hscroll_step_abs = 0;
12930 double hscroll_step_rel = 0;
12932 if (hscroll_relative_p)
12934 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12935 if (hscroll_step_rel < 0)
12937 hscroll_relative_p = false;
12938 hscroll_step_abs = 0;
12941 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12943 hscroll_step_abs = XINT (Vhscroll_step);
12944 if (hscroll_step_abs < 0)
12945 hscroll_step_abs = 0;
12947 else
12948 hscroll_step_abs = 0;
12950 while (WINDOWP (window))
12952 struct window *w = XWINDOW (window);
12954 if (WINDOWP (w->contents))
12955 hscrolled_p |= hscroll_window_tree (w->contents);
12956 else if (w->cursor.vpos >= 0)
12958 int h_margin;
12959 int text_area_width;
12960 struct glyph_row *cursor_row;
12961 struct glyph_row *bottom_row;
12963 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12964 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12965 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12966 else
12967 cursor_row = bottom_row - 1;
12969 if (!cursor_row->enabled_p)
12971 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12972 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12973 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12974 else
12975 cursor_row = bottom_row - 1;
12977 bool row_r2l_p = cursor_row->reversed_p;
12979 text_area_width = window_box_width (w, TEXT_AREA);
12981 /* Scroll when cursor is inside this scroll margin. */
12982 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12984 /* If the position of this window's point has explicitly
12985 changed, no more suspend auto hscrolling. */
12986 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12987 w->suspend_auto_hscroll = false;
12989 /* Remember window point. */
12990 Fset_marker (w->old_pointm,
12991 ((w == XWINDOW (selected_window))
12992 ? make_number (BUF_PT (XBUFFER (w->contents)))
12993 : Fmarker_position (w->pointm)),
12994 w->contents);
12996 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12997 && !w->suspend_auto_hscroll
12998 /* In some pathological cases, like restoring a window
12999 configuration into a frame that is much smaller than
13000 the one from which the configuration was saved, we
13001 get glyph rows whose start and end have zero buffer
13002 positions, which we cannot handle below. Just skip
13003 such windows. */
13004 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13005 /* For left-to-right rows, hscroll when cursor is either
13006 (i) inside the right hscroll margin, or (ii) if it is
13007 inside the left margin and the window is already
13008 hscrolled. */
13009 && ((!row_r2l_p
13010 && ((w->hscroll && w->cursor.x <= h_margin)
13011 || (cursor_row->enabled_p
13012 && cursor_row->truncated_on_right_p
13013 && (w->cursor.x >= text_area_width - h_margin))))
13014 /* For right-to-left rows, the logic is similar,
13015 except that rules for scrolling to left and right
13016 are reversed. E.g., if cursor.x <= h_margin, we
13017 need to hscroll "to the right" unconditionally,
13018 and that will scroll the screen to the left so as
13019 to reveal the next portion of the row. */
13020 || (row_r2l_p
13021 && ((cursor_row->enabled_p
13022 /* FIXME: It is confusing to set the
13023 truncated_on_right_p flag when R2L rows
13024 are actually truncated on the left. */
13025 && cursor_row->truncated_on_right_p
13026 && w->cursor.x <= h_margin)
13027 || (w->hscroll
13028 && (w->cursor.x >= text_area_width - h_margin))))))
13030 struct it it;
13031 ptrdiff_t hscroll;
13032 struct buffer *saved_current_buffer;
13033 ptrdiff_t pt;
13034 int wanted_x;
13036 /* Find point in a display of infinite width. */
13037 saved_current_buffer = current_buffer;
13038 current_buffer = XBUFFER (w->contents);
13040 if (w == XWINDOW (selected_window))
13041 pt = PT;
13042 else
13043 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13045 /* Move iterator to pt starting at cursor_row->start in
13046 a line with infinite width. */
13047 init_to_row_start (&it, w, cursor_row);
13048 it.last_visible_x = INFINITY;
13049 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13050 current_buffer = saved_current_buffer;
13052 /* Position cursor in window. */
13053 if (!hscroll_relative_p && hscroll_step_abs == 0)
13054 hscroll = max (0, (it.current_x
13055 - (ITERATOR_AT_END_OF_LINE_P (&it)
13056 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13057 : (text_area_width / 2))))
13058 / FRAME_COLUMN_WIDTH (it.f);
13059 else if ((!row_r2l_p
13060 && w->cursor.x >= text_area_width - h_margin)
13061 || (row_r2l_p && w->cursor.x <= h_margin))
13063 if (hscroll_relative_p)
13064 wanted_x = text_area_width * (1 - hscroll_step_rel)
13065 - h_margin;
13066 else
13067 wanted_x = text_area_width
13068 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13069 - h_margin;
13070 hscroll
13071 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13073 else
13075 if (hscroll_relative_p)
13076 wanted_x = text_area_width * hscroll_step_rel
13077 + h_margin;
13078 else
13079 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13080 + h_margin;
13081 hscroll
13082 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13084 hscroll = max (hscroll, w->min_hscroll);
13086 /* Don't prevent redisplay optimizations if hscroll
13087 hasn't changed, as it will unnecessarily slow down
13088 redisplay. */
13089 if (w->hscroll != hscroll)
13091 struct buffer *b = XBUFFER (w->contents);
13092 b->prevent_redisplay_optimizations_p = true;
13093 w->hscroll = hscroll;
13094 hscrolled_p = true;
13099 window = w->next;
13102 /* Value is true if hscroll of any leaf window has been changed. */
13103 return hscrolled_p;
13107 /* Set hscroll so that cursor is visible and not inside horizontal
13108 scroll margins for all windows in the tree rooted at WINDOW. See
13109 also hscroll_window_tree above. Value is true if any window's
13110 hscroll has been changed. If it has, desired matrices on the frame
13111 of WINDOW are cleared. */
13113 static bool
13114 hscroll_windows (Lisp_Object window)
13116 bool hscrolled_p = hscroll_window_tree (window);
13117 if (hscrolled_p)
13118 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13119 return hscrolled_p;
13124 /************************************************************************
13125 Redisplay
13126 ************************************************************************/
13128 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13129 This is sometimes handy to have in a debugger session. */
13131 #ifdef GLYPH_DEBUG
13133 /* First and last unchanged row for try_window_id. */
13135 static int debug_first_unchanged_at_end_vpos;
13136 static int debug_last_unchanged_at_beg_vpos;
13138 /* Delta vpos and y. */
13140 static int debug_dvpos, debug_dy;
13142 /* Delta in characters and bytes for try_window_id. */
13144 static ptrdiff_t debug_delta, debug_delta_bytes;
13146 /* Values of window_end_pos and window_end_vpos at the end of
13147 try_window_id. */
13149 static ptrdiff_t debug_end_vpos;
13151 /* Append a string to W->desired_matrix->method. FMT is a printf
13152 format string. If trace_redisplay_p is true also printf the
13153 resulting string to stderr. */
13155 static void debug_method_add (struct window *, char const *, ...)
13156 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13158 static void
13159 debug_method_add (struct window *w, char const *fmt, ...)
13161 void *ptr = w;
13162 char *method = w->desired_matrix->method;
13163 int len = strlen (method);
13164 int size = sizeof w->desired_matrix->method;
13165 int remaining = size - len - 1;
13166 va_list ap;
13168 if (len && remaining)
13170 method[len] = '|';
13171 --remaining, ++len;
13174 va_start (ap, fmt);
13175 vsnprintf (method + len, remaining + 1, fmt, ap);
13176 va_end (ap);
13178 if (trace_redisplay_p)
13179 fprintf (stderr, "%p (%s): %s\n",
13180 ptr,
13181 ((BUFFERP (w->contents)
13182 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13183 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13184 : "no buffer"),
13185 method + len);
13188 #endif /* GLYPH_DEBUG */
13191 /* Value is true if all changes in window W, which displays
13192 current_buffer, are in the text between START and END. START is a
13193 buffer position, END is given as a distance from Z. Used in
13194 redisplay_internal for display optimization. */
13196 static bool
13197 text_outside_line_unchanged_p (struct window *w,
13198 ptrdiff_t start, ptrdiff_t end)
13200 bool unchanged_p = true;
13202 /* If text or overlays have changed, see where. */
13203 if (window_outdated (w))
13205 /* Gap in the line? */
13206 if (GPT < start || Z - GPT < end)
13207 unchanged_p = false;
13209 /* Changes start in front of the line, or end after it? */
13210 if (unchanged_p
13211 && (BEG_UNCHANGED < start - 1
13212 || END_UNCHANGED < end))
13213 unchanged_p = false;
13215 /* If selective display, can't optimize if changes start at the
13216 beginning of the line. */
13217 if (unchanged_p
13218 && INTEGERP (BVAR (current_buffer, selective_display))
13219 && XINT (BVAR (current_buffer, selective_display)) > 0
13220 && (BEG_UNCHANGED < start || GPT <= start))
13221 unchanged_p = false;
13223 /* If there are overlays at the start or end of the line, these
13224 may have overlay strings with newlines in them. A change at
13225 START, for instance, may actually concern the display of such
13226 overlay strings as well, and they are displayed on different
13227 lines. So, quickly rule out this case. (For the future, it
13228 might be desirable to implement something more telling than
13229 just BEG/END_UNCHANGED.) */
13230 if (unchanged_p)
13232 if (BEG + BEG_UNCHANGED == start
13233 && overlay_touches_p (start))
13234 unchanged_p = false;
13235 if (END_UNCHANGED == end
13236 && overlay_touches_p (Z - end))
13237 unchanged_p = false;
13240 /* Under bidi reordering, adding or deleting a character in the
13241 beginning of a paragraph, before the first strong directional
13242 character, can change the base direction of the paragraph (unless
13243 the buffer specifies a fixed paragraph direction), which will
13244 require redisplaying the whole paragraph. It might be worthwhile
13245 to find the paragraph limits and widen the range of redisplayed
13246 lines to that, but for now just give up this optimization. */
13247 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13248 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13249 unchanged_p = false;
13252 return unchanged_p;
13256 /* Do a frame update, taking possible shortcuts into account. This is
13257 the main external entry point for redisplay.
13259 If the last redisplay displayed an echo area message and that message
13260 is no longer requested, we clear the echo area or bring back the
13261 mini-buffer if that is in use. */
13263 void
13264 redisplay (void)
13266 redisplay_internal ();
13270 static Lisp_Object
13271 overlay_arrow_string_or_property (Lisp_Object var)
13273 Lisp_Object val;
13275 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13276 return val;
13278 return Voverlay_arrow_string;
13281 /* Return true if there are any overlay-arrows in current_buffer. */
13282 static bool
13283 overlay_arrow_in_current_buffer_p (void)
13285 Lisp_Object vlist;
13287 for (vlist = Voverlay_arrow_variable_list;
13288 CONSP (vlist);
13289 vlist = XCDR (vlist))
13291 Lisp_Object var = XCAR (vlist);
13292 Lisp_Object val;
13294 if (!SYMBOLP (var))
13295 continue;
13296 val = find_symbol_value (var);
13297 if (MARKERP (val)
13298 && current_buffer == XMARKER (val)->buffer)
13299 return true;
13301 return false;
13305 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13306 has changed. */
13308 static bool
13309 overlay_arrows_changed_p (void)
13311 Lisp_Object vlist;
13313 for (vlist = Voverlay_arrow_variable_list;
13314 CONSP (vlist);
13315 vlist = XCDR (vlist))
13317 Lisp_Object var = XCAR (vlist);
13318 Lisp_Object val, pstr;
13320 if (!SYMBOLP (var))
13321 continue;
13322 val = find_symbol_value (var);
13323 if (!MARKERP (val))
13324 continue;
13325 if (! EQ (COERCE_MARKER (val),
13326 Fget (var, Qlast_arrow_position))
13327 || ! (pstr = overlay_arrow_string_or_property (var),
13328 EQ (pstr, Fget (var, Qlast_arrow_string))))
13329 return true;
13331 return false;
13334 /* Mark overlay arrows to be updated on next redisplay. */
13336 static void
13337 update_overlay_arrows (int up_to_date)
13339 Lisp_Object vlist;
13341 for (vlist = Voverlay_arrow_variable_list;
13342 CONSP (vlist);
13343 vlist = XCDR (vlist))
13345 Lisp_Object var = XCAR (vlist);
13347 if (!SYMBOLP (var))
13348 continue;
13350 if (up_to_date > 0)
13352 Lisp_Object val = find_symbol_value (var);
13353 Fput (var, Qlast_arrow_position,
13354 COERCE_MARKER (val));
13355 Fput (var, Qlast_arrow_string,
13356 overlay_arrow_string_or_property (var));
13358 else if (up_to_date < 0
13359 || !NILP (Fget (var, Qlast_arrow_position)))
13361 Fput (var, Qlast_arrow_position, Qt);
13362 Fput (var, Qlast_arrow_string, Qt);
13368 /* Return overlay arrow string to display at row.
13369 Return integer (bitmap number) for arrow bitmap in left fringe.
13370 Return nil if no overlay arrow. */
13372 static Lisp_Object
13373 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13375 Lisp_Object vlist;
13377 for (vlist = Voverlay_arrow_variable_list;
13378 CONSP (vlist);
13379 vlist = XCDR (vlist))
13381 Lisp_Object var = XCAR (vlist);
13382 Lisp_Object val;
13384 if (!SYMBOLP (var))
13385 continue;
13387 val = find_symbol_value (var);
13389 if (MARKERP (val)
13390 && current_buffer == XMARKER (val)->buffer
13391 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13393 if (FRAME_WINDOW_P (it->f)
13394 /* FIXME: if ROW->reversed_p is set, this should test
13395 the right fringe, not the left one. */
13396 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13398 #ifdef HAVE_WINDOW_SYSTEM
13399 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13401 int fringe_bitmap = lookup_fringe_bitmap (val);
13402 if (fringe_bitmap != 0)
13403 return make_number (fringe_bitmap);
13405 #endif
13406 return make_number (-1); /* Use default arrow bitmap. */
13408 return overlay_arrow_string_or_property (var);
13412 return Qnil;
13415 /* Return true if point moved out of or into a composition. Otherwise
13416 return false. PREV_BUF and PREV_PT are the last point buffer and
13417 position. BUF and PT are the current point buffer and position. */
13419 static bool
13420 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13421 struct buffer *buf, ptrdiff_t pt)
13423 ptrdiff_t start, end;
13424 Lisp_Object prop;
13425 Lisp_Object buffer;
13427 XSETBUFFER (buffer, buf);
13428 /* Check a composition at the last point if point moved within the
13429 same buffer. */
13430 if (prev_buf == buf)
13432 if (prev_pt == pt)
13433 /* Point didn't move. */
13434 return false;
13436 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13437 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13438 && composition_valid_p (start, end, prop)
13439 && start < prev_pt && end > prev_pt)
13440 /* The last point was within the composition. Return true iff
13441 point moved out of the composition. */
13442 return (pt <= start || pt >= end);
13445 /* Check a composition at the current point. */
13446 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13447 && find_composition (pt, -1, &start, &end, &prop, buffer)
13448 && composition_valid_p (start, end, prop)
13449 && start < pt && end > pt);
13452 /* Reconsider the clip changes of buffer which is displayed in W. */
13454 static void
13455 reconsider_clip_changes (struct window *w)
13457 struct buffer *b = XBUFFER (w->contents);
13459 if (b->clip_changed
13460 && w->window_end_valid
13461 && w->current_matrix->buffer == b
13462 && w->current_matrix->zv == BUF_ZV (b)
13463 && w->current_matrix->begv == BUF_BEGV (b))
13464 b->clip_changed = false;
13466 /* If display wasn't paused, and W is not a tool bar window, see if
13467 point has been moved into or out of a composition. In that case,
13468 set b->clip_changed to force updating the screen. If
13469 b->clip_changed has already been set, skip this check. */
13470 if (!b->clip_changed && w->window_end_valid)
13472 ptrdiff_t pt = (w == XWINDOW (selected_window)
13473 ? PT : marker_position (w->pointm));
13475 if ((w->current_matrix->buffer != b || pt != w->last_point)
13476 && check_point_in_composition (w->current_matrix->buffer,
13477 w->last_point, b, pt))
13478 b->clip_changed = true;
13482 static void
13483 propagate_buffer_redisplay (void)
13484 { /* Resetting b->text->redisplay is problematic!
13485 We can't just reset it in the case that some window that displays
13486 it has not been redisplayed; and such a window can stay
13487 unredisplayed for a long time if it's currently invisible.
13488 But we do want to reset it at the end of redisplay otherwise
13489 its displayed windows will keep being redisplayed over and over
13490 again.
13491 So we copy all b->text->redisplay flags up to their windows here,
13492 such that mark_window_display_accurate can safely reset
13493 b->text->redisplay. */
13494 Lisp_Object ws = window_list ();
13495 for (; CONSP (ws); ws = XCDR (ws))
13497 struct window *thisw = XWINDOW (XCAR (ws));
13498 struct buffer *thisb = XBUFFER (thisw->contents);
13499 if (thisb->text->redisplay)
13500 thisw->redisplay = true;
13504 #define STOP_POLLING \
13505 do { if (! polling_stopped_here) stop_polling (); \
13506 polling_stopped_here = true; } while (false)
13508 #define RESUME_POLLING \
13509 do { if (polling_stopped_here) start_polling (); \
13510 polling_stopped_here = false; } while (false)
13513 /* Perhaps in the future avoid recentering windows if it
13514 is not necessary; currently that causes some problems. */
13516 static void
13517 redisplay_internal (void)
13519 struct window *w = XWINDOW (selected_window);
13520 struct window *sw;
13521 struct frame *fr;
13522 bool pending;
13523 bool must_finish = false, match_p;
13524 struct text_pos tlbufpos, tlendpos;
13525 int number_of_visible_frames;
13526 ptrdiff_t count;
13527 struct frame *sf;
13528 bool polling_stopped_here = false;
13529 Lisp_Object tail, frame;
13531 /* Set a limit to the number of retries we perform due to horizontal
13532 scrolling, this avoids getting stuck in an uninterruptible
13533 infinite loop (Bug #24633). */
13534 enum { MAX_HSCROLL_RETRIES = 16 };
13535 int hscroll_retries = 0;
13537 /* True means redisplay has to consider all windows on all
13538 frames. False, only selected_window is considered. */
13539 bool consider_all_windows_p;
13541 /* True means redisplay has to redisplay the miniwindow. */
13542 bool update_miniwindow_p = false;
13544 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13546 /* No redisplay if running in batch mode or frame is not yet fully
13547 initialized, or redisplay is explicitly turned off by setting
13548 Vinhibit_redisplay. */
13549 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13550 || !NILP (Vinhibit_redisplay))
13551 return;
13553 /* Don't examine these until after testing Vinhibit_redisplay.
13554 When Emacs is shutting down, perhaps because its connection to
13555 X has dropped, we should not look at them at all. */
13556 fr = XFRAME (w->frame);
13557 sf = SELECTED_FRAME ();
13559 if (!fr->glyphs_initialized_p)
13560 return;
13562 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13563 if (popup_activated ())
13564 return;
13565 #endif
13567 /* I don't think this happens but let's be paranoid. */
13568 if (redisplaying_p)
13569 return;
13571 /* Record a function that clears redisplaying_p
13572 when we leave this function. */
13573 count = SPECPDL_INDEX ();
13574 record_unwind_protect_void (unwind_redisplay);
13575 redisplaying_p = true;
13576 block_buffer_flips ();
13577 specbind (Qinhibit_free_realized_faces, Qnil);
13579 /* Record this function, so it appears on the profiler's backtraces. */
13580 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13582 FOR_EACH_FRAME (tail, frame)
13583 XFRAME (frame)->already_hscrolled_p = false;
13585 retry:
13586 /* Remember the currently selected window. */
13587 sw = w;
13589 pending = false;
13590 forget_escape_and_glyphless_faces ();
13592 inhibit_free_realized_faces = false;
13594 /* If face_change, init_iterator will free all realized faces, which
13595 includes the faces referenced from current matrices. So, we
13596 can't reuse current matrices in this case. */
13597 if (face_change)
13598 windows_or_buffers_changed = 47;
13600 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13601 && FRAME_TTY (sf)->previous_frame != sf)
13603 /* Since frames on a single ASCII terminal share the same
13604 display area, displaying a different frame means redisplay
13605 the whole thing. */
13606 SET_FRAME_GARBAGED (sf);
13607 #ifndef DOS_NT
13608 set_tty_color_mode (FRAME_TTY (sf), sf);
13609 #endif
13610 FRAME_TTY (sf)->previous_frame = sf;
13613 /* Set the visible flags for all frames. Do this before checking for
13614 resized or garbaged frames; they want to know if their frames are
13615 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13616 number_of_visible_frames = 0;
13618 FOR_EACH_FRAME (tail, frame)
13620 struct frame *f = XFRAME (frame);
13622 if (FRAME_VISIBLE_P (f))
13624 ++number_of_visible_frames;
13625 /* Adjust matrices for visible frames only. */
13626 if (f->fonts_changed)
13628 adjust_frame_glyphs (f);
13629 /* Disable all redisplay optimizations for this frame.
13630 This is because adjust_frame_glyphs resets the
13631 enabled_p flag for all glyph rows of all windows, so
13632 many optimizations will fail anyway, and some might
13633 fail to test that flag and do bogus things as
13634 result. */
13635 SET_FRAME_GARBAGED (f);
13636 f->fonts_changed = false;
13638 /* If cursor type has been changed on the frame
13639 other than selected, consider all frames. */
13640 if (f != sf && f->cursor_type_changed)
13641 fset_redisplay (f);
13643 clear_desired_matrices (f);
13646 /* Notice any pending interrupt request to change frame size. */
13647 do_pending_window_change (true);
13649 /* do_pending_window_change could change the selected_window due to
13650 frame resizing which makes the selected window too small. */
13651 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13652 sw = w;
13654 /* Clear frames marked as garbaged. */
13655 clear_garbaged_frames ();
13657 /* Build menubar and tool-bar items. */
13658 if (NILP (Vmemory_full))
13659 prepare_menu_bars ();
13661 reconsider_clip_changes (w);
13663 /* In most cases selected window displays current buffer. */
13664 match_p = XBUFFER (w->contents) == current_buffer;
13665 if (match_p)
13667 /* Detect case that we need to write or remove a star in the mode line. */
13668 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13669 w->update_mode_line = true;
13671 if (mode_line_update_needed (w))
13672 w->update_mode_line = true;
13674 /* If reconsider_clip_changes above decided that the narrowing
13675 in the current buffer changed, make sure all other windows
13676 showing that buffer will be redisplayed. */
13677 if (current_buffer->clip_changed)
13678 bset_update_mode_line (current_buffer);
13681 /* Normally the message* functions will have already displayed and
13682 updated the echo area, but the frame may have been trashed, or
13683 the update may have been preempted, so display the echo area
13684 again here. Checking message_cleared_p captures the case that
13685 the echo area should be cleared. */
13686 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13687 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13688 || (message_cleared_p
13689 && minibuf_level == 0
13690 /* If the mini-window is currently selected, this means the
13691 echo-area doesn't show through. */
13692 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13694 echo_area_display (false);
13696 /* If echo_area_display resizes the mini-window, the redisplay and
13697 window_sizes_changed flags of the selected frame are set, but
13698 it's too late for the hooks in window-size-change-functions,
13699 which have been examined already in prepare_menu_bars. So in
13700 that case we call the hooks here only for the selected frame. */
13701 if (sf->redisplay)
13703 ptrdiff_t count1 = SPECPDL_INDEX ();
13705 record_unwind_save_match_data ();
13706 run_window_size_change_functions (selected_frame);
13707 unbind_to (count1, Qnil);
13710 if (message_cleared_p)
13711 update_miniwindow_p = true;
13713 must_finish = true;
13715 /* If we don't display the current message, don't clear the
13716 message_cleared_p flag, because, if we did, we wouldn't clear
13717 the echo area in the next redisplay which doesn't preserve
13718 the echo area. */
13719 if (!display_last_displayed_message_p)
13720 message_cleared_p = false;
13722 else if (EQ (selected_window, minibuf_window)
13723 && (current_buffer->clip_changed || window_outdated (w))
13724 && resize_mini_window (w, false))
13726 if (sf->redisplay)
13728 ptrdiff_t count1 = SPECPDL_INDEX ();
13730 record_unwind_save_match_data ();
13731 run_window_size_change_functions (selected_frame);
13732 unbind_to (count1, Qnil);
13735 /* Resized active mini-window to fit the size of what it is
13736 showing if its contents might have changed. */
13737 must_finish = true;
13739 /* If window configuration was changed, frames may have been
13740 marked garbaged. Clear them or we will experience
13741 surprises wrt scrolling. */
13742 clear_garbaged_frames ();
13745 if (windows_or_buffers_changed && !update_mode_lines)
13746 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13747 only the windows's contents needs to be refreshed, or whether the
13748 mode-lines also need a refresh. */
13749 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13750 ? REDISPLAY_SOME : 32);
13752 /* If specs for an arrow have changed, do thorough redisplay
13753 to ensure we remove any arrow that should no longer exist. */
13754 if (overlay_arrows_changed_p ())
13755 /* Apparently, this is the only case where we update other windows,
13756 without updating other mode-lines. */
13757 windows_or_buffers_changed = 49;
13759 consider_all_windows_p = (update_mode_lines
13760 || windows_or_buffers_changed);
13762 #define AINC(a,i) \
13764 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13765 if (INTEGERP (entry)) \
13766 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13769 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13770 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13772 /* Optimize the case that only the line containing the cursor in the
13773 selected window has changed. Variables starting with this_ are
13774 set in display_line and record information about the line
13775 containing the cursor. */
13776 tlbufpos = this_line_start_pos;
13777 tlendpos = this_line_end_pos;
13778 if (!consider_all_windows_p
13779 && CHARPOS (tlbufpos) > 0
13780 && !w->update_mode_line
13781 && !current_buffer->clip_changed
13782 && !current_buffer->prevent_redisplay_optimizations_p
13783 && FRAME_VISIBLE_P (XFRAME (w->frame))
13784 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13785 && !XFRAME (w->frame)->cursor_type_changed
13786 && !XFRAME (w->frame)->face_change
13787 /* Make sure recorded data applies to current buffer, etc. */
13788 && this_line_buffer == current_buffer
13789 && match_p
13790 && !w->force_start
13791 && !w->optional_new_start
13792 /* Point must be on the line that we have info recorded about. */
13793 && PT >= CHARPOS (tlbufpos)
13794 && PT <= Z - CHARPOS (tlendpos)
13795 /* All text outside that line, including its final newline,
13796 must be unchanged. */
13797 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13798 CHARPOS (tlendpos)))
13800 if (CHARPOS (tlbufpos) > BEGV
13801 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13802 && (CHARPOS (tlbufpos) == ZV
13803 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13804 /* Former continuation line has disappeared by becoming empty. */
13805 goto cancel;
13806 else if (window_outdated (w) || MINI_WINDOW_P (w))
13808 /* We have to handle the case of continuation around a
13809 wide-column character (see the comment in indent.c around
13810 line 1340).
13812 For instance, in the following case:
13814 -------- Insert --------
13815 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13816 J_I_ ==> J_I_ `^^' are cursors.
13817 ^^ ^^
13818 -------- --------
13820 As we have to redraw the line above, we cannot use this
13821 optimization. */
13823 struct it it;
13824 int line_height_before = this_line_pixel_height;
13826 /* Note that start_display will handle the case that the
13827 line starting at tlbufpos is a continuation line. */
13828 start_display (&it, w, tlbufpos);
13830 /* Implementation note: It this still necessary? */
13831 if (it.current_x != this_line_start_x)
13832 goto cancel;
13834 TRACE ((stderr, "trying display optimization 1\n"));
13835 w->cursor.vpos = -1;
13836 overlay_arrow_seen = false;
13837 it.vpos = this_line_vpos;
13838 it.current_y = this_line_y;
13839 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13840 display_line (&it);
13842 /* If line contains point, is not continued,
13843 and ends at same distance from eob as before, we win. */
13844 if (w->cursor.vpos >= 0
13845 /* Line is not continued, otherwise this_line_start_pos
13846 would have been set to 0 in display_line. */
13847 && CHARPOS (this_line_start_pos)
13848 /* Line ends as before. */
13849 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13850 /* Line has same height as before. Otherwise other lines
13851 would have to be shifted up or down. */
13852 && this_line_pixel_height == line_height_before)
13854 /* If this is not the window's last line, we must adjust
13855 the charstarts of the lines below. */
13856 if (it.current_y < it.last_visible_y)
13858 struct glyph_row *row
13859 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13860 ptrdiff_t delta, delta_bytes;
13862 /* We used to distinguish between two cases here,
13863 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13864 when the line ends in a newline or the end of the
13865 buffer's accessible portion. But both cases did
13866 the same, so they were collapsed. */
13867 delta = (Z
13868 - CHARPOS (tlendpos)
13869 - MATRIX_ROW_START_CHARPOS (row));
13870 delta_bytes = (Z_BYTE
13871 - BYTEPOS (tlendpos)
13872 - MATRIX_ROW_START_BYTEPOS (row));
13874 increment_matrix_positions (w->current_matrix,
13875 this_line_vpos + 1,
13876 w->current_matrix->nrows,
13877 delta, delta_bytes);
13880 /* If this row displays text now but previously didn't,
13881 or vice versa, w->window_end_vpos may have to be
13882 adjusted. */
13883 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13885 if (w->window_end_vpos < this_line_vpos)
13886 w->window_end_vpos = this_line_vpos;
13888 else if (w->window_end_vpos == this_line_vpos
13889 && this_line_vpos > 0)
13890 w->window_end_vpos = this_line_vpos - 1;
13891 w->window_end_valid = false;
13893 /* Update hint: No need to try to scroll in update_window. */
13894 w->desired_matrix->no_scrolling_p = true;
13896 #ifdef GLYPH_DEBUG
13897 *w->desired_matrix->method = 0;
13898 debug_method_add (w, "optimization 1");
13899 #endif
13900 #ifdef HAVE_WINDOW_SYSTEM
13901 update_window_fringes (w, false);
13902 #endif
13903 goto update;
13905 else
13906 goto cancel;
13908 else if (/* Cursor position hasn't changed. */
13909 PT == w->last_point
13910 /* Make sure the cursor was last displayed
13911 in this window. Otherwise we have to reposition it. */
13913 /* PXW: Must be converted to pixels, probably. */
13914 && 0 <= w->cursor.vpos
13915 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13917 if (!must_finish)
13919 do_pending_window_change (true);
13920 /* If selected_window changed, redisplay again. */
13921 if (WINDOWP (selected_window)
13922 && (w = XWINDOW (selected_window)) != sw)
13923 goto retry;
13925 /* We used to always goto end_of_redisplay here, but this
13926 isn't enough if we have a blinking cursor. */
13927 if (w->cursor_off_p == w->last_cursor_off_p)
13928 goto end_of_redisplay;
13930 goto update;
13932 /* If highlighting the region, or if the cursor is in the echo area,
13933 then we can't just move the cursor. */
13934 else if (NILP (Vshow_trailing_whitespace)
13935 && !cursor_in_echo_area)
13937 struct it it;
13938 struct glyph_row *row;
13940 /* Skip from tlbufpos to PT and see where it is. Note that
13941 PT may be in invisible text. If so, we will end at the
13942 next visible position. */
13943 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13944 NULL, DEFAULT_FACE_ID);
13945 it.current_x = this_line_start_x;
13946 it.current_y = this_line_y;
13947 it.vpos = this_line_vpos;
13949 /* The call to move_it_to stops in front of PT, but
13950 moves over before-strings. */
13951 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13953 if (it.vpos == this_line_vpos
13954 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13955 row->enabled_p))
13957 eassert (this_line_vpos == it.vpos);
13958 eassert (this_line_y == it.current_y);
13959 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13960 if (cursor_row_fully_visible_p (w, false, true))
13962 #ifdef GLYPH_DEBUG
13963 *w->desired_matrix->method = 0;
13964 debug_method_add (w, "optimization 3");
13965 #endif
13966 goto update;
13968 else
13969 goto cancel;
13971 else
13972 goto cancel;
13975 cancel:
13976 /* Text changed drastically or point moved off of line. */
13977 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13980 CHARPOS (this_line_start_pos) = 0;
13981 ++clear_face_cache_count;
13982 #ifdef HAVE_WINDOW_SYSTEM
13983 ++clear_image_cache_count;
13984 #endif
13986 /* Build desired matrices, and update the display. If
13987 consider_all_windows_p, do it for all windows on all frames that
13988 require redisplay, as specified by their 'redisplay' flag.
13989 Otherwise do it for selected_window, only. */
13991 if (consider_all_windows_p)
13993 FOR_EACH_FRAME (tail, frame)
13994 XFRAME (frame)->updated_p = false;
13996 propagate_buffer_redisplay ();
13998 FOR_EACH_FRAME (tail, frame)
14000 struct frame *f = XFRAME (frame);
14002 /* We don't have to do anything for unselected terminal
14003 frames. */
14004 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14005 && !EQ (FRAME_TTY (f)->top_frame, frame))
14006 continue;
14008 retry_frame:
14009 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14011 bool gcscrollbars
14012 /* Only GC scrollbars when we redisplay the whole frame. */
14013 = f->redisplay || !REDISPLAY_SOME_P ();
14014 bool f_redisplay_flag = f->redisplay;
14015 /* Mark all the scroll bars to be removed; we'll redeem
14016 the ones we want when we redisplay their windows. */
14017 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14018 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14020 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14021 redisplay_windows (FRAME_ROOT_WINDOW (f));
14022 /* Remember that the invisible frames need to be redisplayed next
14023 time they're visible. */
14024 else if (!REDISPLAY_SOME_P ())
14025 f->redisplay = true;
14027 /* The X error handler may have deleted that frame. */
14028 if (!FRAME_LIVE_P (f))
14029 continue;
14031 /* Any scroll bars which redisplay_windows should have
14032 nuked should now go away. */
14033 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14034 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14036 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14038 /* If fonts changed on visible frame, display again. */
14039 if (f->fonts_changed)
14041 adjust_frame_glyphs (f);
14042 /* Disable all redisplay optimizations for this
14043 frame. For the reasons, see the comment near
14044 the previous call to adjust_frame_glyphs above. */
14045 SET_FRAME_GARBAGED (f);
14046 f->fonts_changed = false;
14047 goto retry_frame;
14050 /* See if we have to hscroll. */
14051 if (!f->already_hscrolled_p)
14053 f->already_hscrolled_p = true;
14054 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14055 && hscroll_windows (f->root_window))
14057 hscroll_retries++;
14058 goto retry_frame;
14062 /* If the frame's redisplay flag was not set before
14063 we went about redisplaying its windows, but it is
14064 set now, that means we employed some redisplay
14065 optimizations inside redisplay_windows, and
14066 bypassed producing some screen lines. But if
14067 f->redisplay is now set, it might mean the old
14068 faces are no longer valid (e.g., if redisplaying
14069 some window called some Lisp which defined a new
14070 face or redefined an existing face), so trying to
14071 use them in update_frame will segfault.
14072 Therefore, we must redisplay this frame. */
14073 if (!f_redisplay_flag && f->redisplay)
14074 goto retry_frame;
14076 /* In some case (e.g., window resize), we notice
14077 only during window updating that the window
14078 content changed unpredictably (e.g., a GTK
14079 scrollbar moved) and that our previous estimation
14080 of the frame content was garbage. We have to
14081 start over. These cases should be rare, so going
14082 all the way back to the top of redisplay should
14083 be good enough.
14085 Why FRAME_WINDOW_P? See
14086 https://lists.gnu.org/archive/html/emacs-devel/2016-10/msg00957.html
14089 if (FRAME_GARBAGED_P (f) && FRAME_WINDOW_P (f))
14090 goto retry;
14092 /* Prevent various kinds of signals during display
14093 update. stdio is not robust about handling
14094 signals, which can cause an apparent I/O error. */
14095 if (interrupt_input)
14096 unrequest_sigio ();
14097 STOP_POLLING;
14099 pending |= update_frame (f, false, false);
14100 f->cursor_type_changed = false;
14101 f->updated_p = true;
14106 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14108 if (!pending)
14110 /* Do the mark_window_display_accurate after all windows have
14111 been redisplayed because this call resets flags in buffers
14112 which are needed for proper redisplay. */
14113 FOR_EACH_FRAME (tail, frame)
14115 struct frame *f = XFRAME (frame);
14116 if (f->updated_p)
14118 f->redisplay = false;
14119 f->garbaged = false;
14120 mark_window_display_accurate (f->root_window, true);
14121 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14122 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14127 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14129 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14130 /* Use list_of_error, not Qerror, so that
14131 we catch only errors and don't run the debugger. */
14132 internal_condition_case_1 (redisplay_window_1, selected_window,
14133 list_of_error,
14134 redisplay_window_error);
14135 if (update_miniwindow_p)
14136 internal_condition_case_1 (redisplay_window_1,
14137 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14138 redisplay_window_error);
14140 /* Compare desired and current matrices, perform output. */
14142 update:
14143 /* If fonts changed, display again. Likewise if redisplay_window_1
14144 above caused some change (e.g., a change in faces) that requires
14145 considering the entire frame again. */
14146 if (sf->fonts_changed || sf->redisplay)
14148 if (sf->redisplay)
14150 /* Set this to force a more thorough redisplay.
14151 Otherwise, we might immediately loop back to the
14152 above "else-if" clause (since all the conditions that
14153 led here might still be true), and we will then
14154 infloop, because the selected-frame's redisplay flag
14155 is not (and cannot be) reset. */
14156 windows_or_buffers_changed = 50;
14158 goto retry;
14161 /* Prevent freeing of realized faces, since desired matrices are
14162 pending that reference the faces we computed and cached. */
14163 inhibit_free_realized_faces = true;
14165 /* Prevent various kinds of signals during display update.
14166 stdio is not robust about handling signals,
14167 which can cause an apparent I/O error. */
14168 if (interrupt_input)
14169 unrequest_sigio ();
14170 STOP_POLLING;
14172 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14174 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14175 && hscroll_windows (selected_window))
14177 hscroll_retries++;
14178 goto retry;
14181 XWINDOW (selected_window)->must_be_updated_p = true;
14182 pending = update_frame (sf, false, false);
14183 sf->cursor_type_changed = false;
14186 /* We may have called echo_area_display at the top of this
14187 function. If the echo area is on another frame, that may
14188 have put text on a frame other than the selected one, so the
14189 above call to update_frame would not have caught it. Catch
14190 it here. */
14191 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14192 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14194 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14196 XWINDOW (mini_window)->must_be_updated_p = true;
14197 pending |= update_frame (mini_frame, false, false);
14198 mini_frame->cursor_type_changed = false;
14199 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14200 && hscroll_windows (mini_window))
14202 hscroll_retries++;
14203 goto retry;
14208 /* If display was paused because of pending input, make sure we do a
14209 thorough update the next time. */
14210 if (pending)
14212 /* Prevent the optimization at the beginning of
14213 redisplay_internal that tries a single-line update of the
14214 line containing the cursor in the selected window. */
14215 CHARPOS (this_line_start_pos) = 0;
14217 /* Let the overlay arrow be updated the next time. */
14218 update_overlay_arrows (0);
14220 /* If we pause after scrolling, some rows in the current
14221 matrices of some windows are not valid. */
14222 if (!WINDOW_FULL_WIDTH_P (w)
14223 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14224 update_mode_lines = 36;
14226 else
14228 if (!consider_all_windows_p)
14230 /* This has already been done above if
14231 consider_all_windows_p is set. */
14232 if (XBUFFER (w->contents)->text->redisplay
14233 && buffer_window_count (XBUFFER (w->contents)) > 1)
14234 /* This can happen if b->text->redisplay was set during
14235 jit-lock. */
14236 propagate_buffer_redisplay ();
14237 mark_window_display_accurate_1 (w, true);
14239 /* Say overlay arrows are up to date. */
14240 update_overlay_arrows (1);
14242 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14243 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14246 update_mode_lines = 0;
14247 windows_or_buffers_changed = 0;
14250 /* Start SIGIO interrupts coming again. Having them off during the
14251 code above makes it less likely one will discard output, but not
14252 impossible, since there might be stuff in the system buffer here.
14253 But it is much hairier to try to do anything about that. */
14254 if (interrupt_input)
14255 request_sigio ();
14256 RESUME_POLLING;
14258 /* If a frame has become visible which was not before, redisplay
14259 again, so that we display it. Expose events for such a frame
14260 (which it gets when becoming visible) don't call the parts of
14261 redisplay constructing glyphs, so simply exposing a frame won't
14262 display anything in this case. So, we have to display these
14263 frames here explicitly. */
14264 if (!pending)
14266 int new_count = 0;
14268 FOR_EACH_FRAME (tail, frame)
14270 if (XFRAME (frame)->visible)
14271 new_count++;
14274 if (new_count != number_of_visible_frames)
14275 windows_or_buffers_changed = 52;
14278 /* Change frame size now if a change is pending. */
14279 do_pending_window_change (true);
14281 /* If we just did a pending size change, or have additional
14282 visible frames, or selected_window changed, redisplay again. */
14283 if ((windows_or_buffers_changed && !pending)
14284 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14285 goto retry;
14287 /* Clear the face and image caches.
14289 We used to do this only if consider_all_windows_p. But the cache
14290 needs to be cleared if a timer creates images in the current
14291 buffer (e.g. the test case in Bug#6230). */
14293 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14295 clear_face_cache (false);
14296 clear_face_cache_count = 0;
14299 #ifdef HAVE_WINDOW_SYSTEM
14300 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14302 clear_image_caches (Qnil);
14303 clear_image_cache_count = 0;
14305 #endif /* HAVE_WINDOW_SYSTEM */
14307 end_of_redisplay:
14308 #ifdef HAVE_NS
14309 ns_set_doc_edited ();
14310 #endif
14311 if (interrupt_input && interrupts_deferred)
14312 request_sigio ();
14314 unbind_to (count, Qnil);
14315 RESUME_POLLING;
14318 static void
14319 unwind_redisplay_preserve_echo_area (void)
14321 unblock_buffer_flips ();
14324 /* Redisplay, but leave alone any recent echo area message unless
14325 another message has been requested in its place.
14327 This is useful in situations where you need to redisplay but no
14328 user action has occurred, making it inappropriate for the message
14329 area to be cleared. See tracking_off and
14330 wait_reading_process_output for examples of these situations.
14332 FROM_WHERE is an integer saying from where this function was
14333 called. This is useful for debugging. */
14335 void
14336 redisplay_preserve_echo_area (int from_where)
14338 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14340 block_input ();
14341 ptrdiff_t count = SPECPDL_INDEX ();
14342 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14343 block_buffer_flips ();
14344 unblock_input ();
14346 if (!NILP (echo_area_buffer[1]))
14348 /* We have a previously displayed message, but no current
14349 message. Redisplay the previous message. */
14350 display_last_displayed_message_p = true;
14351 redisplay_internal ();
14352 display_last_displayed_message_p = false;
14354 else
14355 redisplay_internal ();
14357 flush_frame (SELECTED_FRAME ());
14358 unbind_to (count, Qnil);
14362 /* Function registered with record_unwind_protect in redisplay_internal. */
14364 static void
14365 unwind_redisplay (void)
14367 redisplaying_p = false;
14368 unblock_buffer_flips ();
14372 /* Mark the display of leaf window W as accurate or inaccurate.
14373 If ACCURATE_P, mark display of W as accurate.
14374 If !ACCURATE_P, arrange for W to be redisplayed the next
14375 time redisplay_internal is called. */
14377 static void
14378 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14380 struct buffer *b = XBUFFER (w->contents);
14382 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14383 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14384 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14386 if (accurate_p)
14388 b->clip_changed = false;
14389 b->prevent_redisplay_optimizations_p = false;
14390 eassert (buffer_window_count (b) > 0);
14391 /* Resetting b->text->redisplay is problematic!
14392 In order to make it safer to do it here, redisplay_internal must
14393 have copied all b->text->redisplay to their respective windows. */
14394 b->text->redisplay = false;
14396 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14397 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14398 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14399 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14401 w->current_matrix->buffer = b;
14402 w->current_matrix->begv = BUF_BEGV (b);
14403 w->current_matrix->zv = BUF_ZV (b);
14405 w->last_cursor_vpos = w->cursor.vpos;
14406 w->last_cursor_off_p = w->cursor_off_p;
14408 if (w == XWINDOW (selected_window))
14409 w->last_point = BUF_PT (b);
14410 else
14411 w->last_point = marker_position (w->pointm);
14413 w->window_end_valid = true;
14414 w->update_mode_line = false;
14417 w->redisplay = !accurate_p;
14421 /* Mark the display of windows in the window tree rooted at WINDOW as
14422 accurate or inaccurate. If ACCURATE_P, mark display of
14423 windows as accurate. If !ACCURATE_P, arrange for windows to
14424 be redisplayed the next time redisplay_internal is called. */
14426 void
14427 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14429 struct window *w;
14431 for (; !NILP (window); window = w->next)
14433 w = XWINDOW (window);
14434 if (WINDOWP (w->contents))
14435 mark_window_display_accurate (w->contents, accurate_p);
14436 else
14437 mark_window_display_accurate_1 (w, accurate_p);
14440 if (accurate_p)
14441 update_overlay_arrows (1);
14442 else
14443 /* Force a thorough redisplay the next time by setting
14444 last_arrow_position and last_arrow_string to t, which is
14445 unequal to any useful value of Voverlay_arrow_... */
14446 update_overlay_arrows (-1);
14450 /* Return value in display table DP (Lisp_Char_Table *) for character
14451 C. Since a display table doesn't have any parent, we don't have to
14452 follow parent. Do not call this function directly but use the
14453 macro DISP_CHAR_VECTOR. */
14455 Lisp_Object
14456 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14458 Lisp_Object val;
14460 if (ASCII_CHAR_P (c))
14462 val = dp->ascii;
14463 if (SUB_CHAR_TABLE_P (val))
14464 val = XSUB_CHAR_TABLE (val)->contents[c];
14466 else
14468 Lisp_Object table;
14470 XSETCHAR_TABLE (table, dp);
14471 val = char_table_ref (table, c);
14473 if (NILP (val))
14474 val = dp->defalt;
14475 return val;
14478 static int buffer_flip_blocked_depth;
14480 void
14481 block_buffer_flips(void)
14483 eassert (buffer_flip_blocked_depth >= 0);
14484 buffer_flip_blocked_depth++;
14487 void
14488 unblock_buffer_flips(void)
14490 eassert (buffer_flip_blocked_depth > 0);
14491 if (--buffer_flip_blocked_depth == 0)
14493 Lisp_Object tail, frame;
14494 block_input ();
14495 FOR_EACH_FRAME (tail, frame)
14497 struct frame *f = XFRAME (frame);
14498 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14499 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14501 unblock_input ();
14505 bool
14506 buffer_flipping_blocked_p (void)
14508 return buffer_flip_blocked_depth > 0;
14512 /***********************************************************************
14513 Window Redisplay
14514 ***********************************************************************/
14516 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14518 static void
14519 redisplay_windows (Lisp_Object window)
14521 while (!NILP (window))
14523 struct window *w = XWINDOW (window);
14525 if (WINDOWP (w->contents))
14526 redisplay_windows (w->contents);
14527 else if (BUFFERP (w->contents))
14529 displayed_buffer = XBUFFER (w->contents);
14530 /* Use list_of_error, not Qerror, so that
14531 we catch only errors and don't run the debugger. */
14532 internal_condition_case_1 (redisplay_window_0, window,
14533 list_of_error,
14534 redisplay_window_error);
14537 window = w->next;
14541 static Lisp_Object
14542 redisplay_window_error (Lisp_Object ignore)
14544 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14545 return Qnil;
14548 static Lisp_Object
14549 redisplay_window_0 (Lisp_Object window)
14551 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14552 redisplay_window (window, false);
14553 return Qnil;
14556 static Lisp_Object
14557 redisplay_window_1 (Lisp_Object window)
14559 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14560 redisplay_window (window, true);
14561 return Qnil;
14565 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14566 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14567 which positions recorded in ROW differ from current buffer
14568 positions.
14570 Return true iff cursor is on this row. */
14572 static bool
14573 set_cursor_from_row (struct window *w, struct glyph_row *row,
14574 struct glyph_matrix *matrix,
14575 ptrdiff_t delta, ptrdiff_t delta_bytes,
14576 int dy, int dvpos)
14578 struct glyph *glyph = row->glyphs[TEXT_AREA];
14579 struct glyph *end = glyph + row->used[TEXT_AREA];
14580 struct glyph *cursor = NULL;
14581 /* The last known character position in row. */
14582 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14583 int x = row->x;
14584 ptrdiff_t pt_old = PT - delta;
14585 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14586 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14587 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14588 /* A glyph beyond the edge of TEXT_AREA which we should never
14589 touch. */
14590 struct glyph *glyphs_end = end;
14591 /* True means we've found a match for cursor position, but that
14592 glyph has the avoid_cursor_p flag set. */
14593 bool match_with_avoid_cursor = false;
14594 /* True means we've seen at least one glyph that came from a
14595 display string. */
14596 bool string_seen = false;
14597 /* Largest and smallest buffer positions seen so far during scan of
14598 glyph row. */
14599 ptrdiff_t bpos_max = pos_before;
14600 ptrdiff_t bpos_min = pos_after;
14601 /* Last buffer position covered by an overlay string with an integer
14602 `cursor' property. */
14603 ptrdiff_t bpos_covered = 0;
14604 /* True means the display string on which to display the cursor
14605 comes from a text property, not from an overlay. */
14606 bool string_from_text_prop = false;
14608 /* Don't even try doing anything if called for a mode-line or
14609 header-line row, since the rest of the code isn't prepared to
14610 deal with such calamities. */
14611 eassert (!row->mode_line_p);
14612 if (row->mode_line_p)
14613 return false;
14615 /* Skip over glyphs not having an object at the start and the end of
14616 the row. These are special glyphs like truncation marks on
14617 terminal frames. */
14618 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14620 if (!row->reversed_p)
14622 while (glyph < end
14623 && NILP (glyph->object)
14624 && glyph->charpos < 0)
14626 x += glyph->pixel_width;
14627 ++glyph;
14629 while (end > glyph
14630 && NILP ((end - 1)->object)
14631 /* CHARPOS is zero for blanks and stretch glyphs
14632 inserted by extend_face_to_end_of_line. */
14633 && (end - 1)->charpos <= 0)
14634 --end;
14635 glyph_before = glyph - 1;
14636 glyph_after = end;
14638 else
14640 struct glyph *g;
14642 /* If the glyph row is reversed, we need to process it from back
14643 to front, so swap the edge pointers. */
14644 glyphs_end = end = glyph - 1;
14645 glyph += row->used[TEXT_AREA] - 1;
14647 while (glyph > end + 1
14648 && NILP (glyph->object)
14649 && glyph->charpos < 0)
14651 --glyph;
14652 x -= glyph->pixel_width;
14654 if (NILP (glyph->object) && glyph->charpos < 0)
14655 --glyph;
14656 /* By default, in reversed rows we put the cursor on the
14657 rightmost (first in the reading order) glyph. */
14658 for (g = end + 1; g < glyph; g++)
14659 x += g->pixel_width;
14660 while (end < glyph
14661 && NILP ((end + 1)->object)
14662 && (end + 1)->charpos <= 0)
14663 ++end;
14664 glyph_before = glyph + 1;
14665 glyph_after = end;
14668 else if (row->reversed_p)
14670 /* In R2L rows that don't display text, put the cursor on the
14671 rightmost glyph. Case in point: an empty last line that is
14672 part of an R2L paragraph. */
14673 cursor = end - 1;
14674 /* Avoid placing the cursor on the last glyph of the row, where
14675 on terminal frames we hold the vertical border between
14676 adjacent windows. */
14677 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14678 && !WINDOW_RIGHTMOST_P (w)
14679 && cursor == row->glyphs[LAST_AREA] - 1)
14680 cursor--;
14681 x = -1; /* will be computed below, at label compute_x */
14684 /* Step 1: Try to find the glyph whose character position
14685 corresponds to point. If that's not possible, find 2 glyphs
14686 whose character positions are the closest to point, one before
14687 point, the other after it. */
14688 if (!row->reversed_p)
14689 while (/* not marched to end of glyph row */
14690 glyph < end
14691 /* glyph was not inserted by redisplay for internal purposes */
14692 && !NILP (glyph->object))
14694 if (BUFFERP (glyph->object))
14696 ptrdiff_t dpos = glyph->charpos - pt_old;
14698 if (glyph->charpos > bpos_max)
14699 bpos_max = glyph->charpos;
14700 if (glyph->charpos < bpos_min)
14701 bpos_min = glyph->charpos;
14702 if (!glyph->avoid_cursor_p)
14704 /* If we hit point, we've found the glyph on which to
14705 display the cursor. */
14706 if (dpos == 0)
14708 match_with_avoid_cursor = false;
14709 break;
14711 /* See if we've found a better approximation to
14712 POS_BEFORE or to POS_AFTER. */
14713 if (0 > dpos && dpos > pos_before - pt_old)
14715 pos_before = glyph->charpos;
14716 glyph_before = glyph;
14718 else if (0 < dpos && dpos < pos_after - pt_old)
14720 pos_after = glyph->charpos;
14721 glyph_after = glyph;
14724 else if (dpos == 0)
14725 match_with_avoid_cursor = true;
14727 else if (STRINGP (glyph->object))
14729 Lisp_Object chprop;
14730 ptrdiff_t glyph_pos = glyph->charpos;
14732 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14733 glyph->object);
14734 if (!NILP (chprop))
14736 /* If the string came from a `display' text property,
14737 look up the buffer position of that property and
14738 use that position to update bpos_max, as if we
14739 actually saw such a position in one of the row's
14740 glyphs. This helps with supporting integer values
14741 of `cursor' property on the display string in
14742 situations where most or all of the row's buffer
14743 text is completely covered by display properties,
14744 so that no glyph with valid buffer positions is
14745 ever seen in the row. */
14746 ptrdiff_t prop_pos =
14747 string_buffer_position_lim (glyph->object, pos_before,
14748 pos_after, false);
14750 if (prop_pos >= pos_before)
14751 bpos_max = prop_pos;
14753 if (INTEGERP (chprop))
14755 bpos_covered = bpos_max + XINT (chprop);
14756 /* If the `cursor' property covers buffer positions up
14757 to and including point, we should display cursor on
14758 this glyph. Note that, if a `cursor' property on one
14759 of the string's characters has an integer value, we
14760 will break out of the loop below _before_ we get to
14761 the position match above. IOW, integer values of
14762 the `cursor' property override the "exact match for
14763 point" strategy of positioning the cursor. */
14764 /* Implementation note: bpos_max == pt_old when, e.g.,
14765 we are in an empty line, where bpos_max is set to
14766 MATRIX_ROW_START_CHARPOS, see above. */
14767 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14769 cursor = glyph;
14770 break;
14774 string_seen = true;
14776 x += glyph->pixel_width;
14777 ++glyph;
14779 else if (glyph > end) /* row is reversed */
14780 while (!NILP (glyph->object))
14782 if (BUFFERP (glyph->object))
14784 ptrdiff_t dpos = glyph->charpos - pt_old;
14786 if (glyph->charpos > bpos_max)
14787 bpos_max = glyph->charpos;
14788 if (glyph->charpos < bpos_min)
14789 bpos_min = glyph->charpos;
14790 if (!glyph->avoid_cursor_p)
14792 if (dpos == 0)
14794 match_with_avoid_cursor = false;
14795 break;
14797 if (0 > dpos && dpos > pos_before - pt_old)
14799 pos_before = glyph->charpos;
14800 glyph_before = glyph;
14802 else if (0 < dpos && dpos < pos_after - pt_old)
14804 pos_after = glyph->charpos;
14805 glyph_after = glyph;
14808 else if (dpos == 0)
14809 match_with_avoid_cursor = true;
14811 else if (STRINGP (glyph->object))
14813 Lisp_Object chprop;
14814 ptrdiff_t glyph_pos = glyph->charpos;
14816 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14817 glyph->object);
14818 if (!NILP (chprop))
14820 ptrdiff_t prop_pos =
14821 string_buffer_position_lim (glyph->object, pos_before,
14822 pos_after, false);
14824 if (prop_pos >= pos_before)
14825 bpos_max = prop_pos;
14827 if (INTEGERP (chprop))
14829 bpos_covered = bpos_max + XINT (chprop);
14830 /* If the `cursor' property covers buffer positions up
14831 to and including point, we should display cursor on
14832 this glyph. */
14833 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14835 cursor = glyph;
14836 break;
14839 string_seen = true;
14841 --glyph;
14842 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14844 x--; /* can't use any pixel_width */
14845 break;
14847 x -= glyph->pixel_width;
14850 /* Step 2: If we didn't find an exact match for point, we need to
14851 look for a proper place to put the cursor among glyphs between
14852 GLYPH_BEFORE and GLYPH_AFTER. */
14853 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14854 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14855 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14857 /* An empty line has a single glyph whose OBJECT is nil and
14858 whose CHARPOS is the position of a newline on that line.
14859 Note that on a TTY, there are more glyphs after that, which
14860 were produced by extend_face_to_end_of_line, but their
14861 CHARPOS is zero or negative. */
14862 bool empty_line_p =
14863 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14864 && NILP (glyph->object) && glyph->charpos > 0
14865 /* On a TTY, continued and truncated rows also have a glyph at
14866 their end whose OBJECT is nil and whose CHARPOS is
14867 positive (the continuation and truncation glyphs), but such
14868 rows are obviously not "empty". */
14869 && !(row->continued_p || row->truncated_on_right_p));
14871 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14873 ptrdiff_t ellipsis_pos;
14875 /* Scan back over the ellipsis glyphs. */
14876 if (!row->reversed_p)
14878 ellipsis_pos = (glyph - 1)->charpos;
14879 while (glyph > row->glyphs[TEXT_AREA]
14880 && (glyph - 1)->charpos == ellipsis_pos)
14881 glyph--, x -= glyph->pixel_width;
14882 /* That loop always goes one position too far, including
14883 the glyph before the ellipsis. So scan forward over
14884 that one. */
14885 x += glyph->pixel_width;
14886 glyph++;
14888 else /* row is reversed */
14890 ellipsis_pos = (glyph + 1)->charpos;
14891 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14892 && (glyph + 1)->charpos == ellipsis_pos)
14893 glyph++, x += glyph->pixel_width;
14894 x -= glyph->pixel_width;
14895 glyph--;
14898 else if (match_with_avoid_cursor)
14900 cursor = glyph_after;
14901 x = -1;
14903 else if (string_seen)
14905 int incr = row->reversed_p ? -1 : +1;
14907 /* Need to find the glyph that came out of a string which is
14908 present at point. That glyph is somewhere between
14909 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14910 positioned between POS_BEFORE and POS_AFTER in the
14911 buffer. */
14912 struct glyph *start, *stop;
14913 ptrdiff_t pos = pos_before;
14915 x = -1;
14917 /* If the row ends in a newline from a display string,
14918 reordering could have moved the glyphs belonging to the
14919 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14920 in this case we extend the search to the last glyph in
14921 the row that was not inserted by redisplay. */
14922 if (row->ends_in_newline_from_string_p)
14924 glyph_after = end;
14925 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14928 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14929 correspond to POS_BEFORE and POS_AFTER, respectively. We
14930 need START and STOP in the order that corresponds to the
14931 row's direction as given by its reversed_p flag. If the
14932 directionality of characters between POS_BEFORE and
14933 POS_AFTER is the opposite of the row's base direction,
14934 these characters will have been reordered for display,
14935 and we need to reverse START and STOP. */
14936 if (!row->reversed_p)
14938 start = min (glyph_before, glyph_after);
14939 stop = max (glyph_before, glyph_after);
14941 else
14943 start = max (glyph_before, glyph_after);
14944 stop = min (glyph_before, glyph_after);
14946 for (glyph = start + incr;
14947 row->reversed_p ? glyph > stop : glyph < stop; )
14950 /* Any glyphs that come from the buffer are here because
14951 of bidi reordering. Skip them, and only pay
14952 attention to glyphs that came from some string. */
14953 if (STRINGP (glyph->object))
14955 Lisp_Object str;
14956 ptrdiff_t tem;
14957 /* If the display property covers the newline, we
14958 need to search for it one position farther. */
14959 ptrdiff_t lim = pos_after
14960 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14962 string_from_text_prop = false;
14963 str = glyph->object;
14964 tem = string_buffer_position_lim (str, pos, lim, false);
14965 if (tem == 0 /* from overlay */
14966 || pos <= tem)
14968 /* If the string from which this glyph came is
14969 found in the buffer at point, or at position
14970 that is closer to point than pos_after, then
14971 we've found the glyph we've been looking for.
14972 If it comes from an overlay (tem == 0), and
14973 it has the `cursor' property on one of its
14974 glyphs, record that glyph as a candidate for
14975 displaying the cursor. (As in the
14976 unidirectional version, we will display the
14977 cursor on the last candidate we find.) */
14978 if (tem == 0
14979 || tem == pt_old
14980 || (tem - pt_old > 0 && tem < pos_after))
14982 /* The glyphs from this string could have
14983 been reordered. Find the one with the
14984 smallest string position. Or there could
14985 be a character in the string with the
14986 `cursor' property, which means display
14987 cursor on that character's glyph. */
14988 ptrdiff_t strpos = glyph->charpos;
14990 if (tem)
14992 cursor = glyph;
14993 string_from_text_prop = true;
14995 for ( ;
14996 (row->reversed_p ? glyph > stop : glyph < stop)
14997 && EQ (glyph->object, str);
14998 glyph += incr)
15000 Lisp_Object cprop;
15001 ptrdiff_t gpos = glyph->charpos;
15003 cprop = Fget_char_property (make_number (gpos),
15004 Qcursor,
15005 glyph->object);
15006 if (!NILP (cprop))
15008 cursor = glyph;
15009 break;
15011 if (tem && glyph->charpos < strpos)
15013 strpos = glyph->charpos;
15014 cursor = glyph;
15018 if (tem == pt_old
15019 || (tem - pt_old > 0 && tem < pos_after))
15020 goto compute_x;
15022 if (tem)
15023 pos = tem + 1; /* don't find previous instances */
15025 /* This string is not what we want; skip all of the
15026 glyphs that came from it. */
15027 while ((row->reversed_p ? glyph > stop : glyph < stop)
15028 && EQ (glyph->object, str))
15029 glyph += incr;
15031 else
15032 glyph += incr;
15035 /* If we reached the end of the line, and END was from a string,
15036 the cursor is not on this line. */
15037 if (cursor == NULL
15038 && (row->reversed_p ? glyph <= end : glyph >= end)
15039 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15040 && STRINGP (end->object)
15041 && row->continued_p)
15042 return false;
15044 /* A truncated row may not include PT among its character positions.
15045 Setting the cursor inside the scroll margin will trigger
15046 recalculation of hscroll in hscroll_window_tree. But if a
15047 display string covers point, defer to the string-handling
15048 code below to figure this out. */
15049 else if (row->truncated_on_left_p && pt_old < bpos_min)
15051 cursor = glyph_before;
15052 x = -1;
15054 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15055 /* Zero-width characters produce no glyphs. */
15056 || (!empty_line_p
15057 && (row->reversed_p
15058 ? glyph_after > glyphs_end
15059 : glyph_after < glyphs_end)))
15061 cursor = glyph_after;
15062 x = -1;
15066 compute_x:
15067 if (cursor != NULL)
15068 glyph = cursor;
15069 else if (glyph == glyphs_end
15070 && pos_before == pos_after
15071 && STRINGP ((row->reversed_p
15072 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15073 : row->glyphs[TEXT_AREA])->object))
15075 /* If all the glyphs of this row came from strings, put the
15076 cursor on the first glyph of the row. This avoids having the
15077 cursor outside of the text area in this very rare and hard
15078 use case. */
15079 glyph =
15080 row->reversed_p
15081 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15082 : row->glyphs[TEXT_AREA];
15084 if (x < 0)
15086 struct glyph *g;
15088 /* Need to compute x that corresponds to GLYPH. */
15089 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15091 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15092 emacs_abort ();
15093 x += g->pixel_width;
15097 /* ROW could be part of a continued line, which, under bidi
15098 reordering, might have other rows whose start and end charpos
15099 occlude point. Only set w->cursor if we found a better
15100 approximation to the cursor position than we have from previously
15101 examined candidate rows belonging to the same continued line. */
15102 if (/* We already have a candidate row. */
15103 w->cursor.vpos >= 0
15104 /* That candidate is not the row we are processing. */
15105 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15106 /* Make sure cursor.vpos specifies a row whose start and end
15107 charpos occlude point, and it is valid candidate for being a
15108 cursor-row. This is because some callers of this function
15109 leave cursor.vpos at the row where the cursor was displayed
15110 during the last redisplay cycle. */
15111 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15112 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15113 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15115 struct glyph *g1
15116 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15118 /* Don't consider glyphs that are outside TEXT_AREA. */
15119 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15120 return false;
15121 /* Keep the candidate whose buffer position is the closest to
15122 point or has the `cursor' property. */
15123 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15124 w->cursor.hpos >= 0
15125 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15126 && ((BUFFERP (g1->object)
15127 && (g1->charpos == pt_old /* An exact match always wins. */
15128 || (BUFFERP (glyph->object)
15129 && eabs (g1->charpos - pt_old)
15130 < eabs (glyph->charpos - pt_old))))
15131 /* Previous candidate is a glyph from a string that has
15132 a non-nil `cursor' property. */
15133 || (STRINGP (g1->object)
15134 && (!NILP (Fget_char_property (make_number (g1->charpos),
15135 Qcursor, g1->object))
15136 /* Previous candidate is from the same display
15137 string as this one, and the display string
15138 came from a text property. */
15139 || (EQ (g1->object, glyph->object)
15140 && string_from_text_prop)
15141 /* this candidate is from newline and its
15142 position is not an exact match */
15143 || (NILP (glyph->object)
15144 && glyph->charpos != pt_old)))))
15145 return false;
15146 /* If this candidate gives an exact match, use that. */
15147 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15148 /* If this candidate is a glyph created for the
15149 terminating newline of a line, and point is on that
15150 newline, it wins because it's an exact match. */
15151 || (!row->continued_p
15152 && NILP (glyph->object)
15153 && glyph->charpos == 0
15154 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15155 /* Otherwise, keep the candidate that comes from a row
15156 spanning less buffer positions. This may win when one or
15157 both candidate positions are on glyphs that came from
15158 display strings, for which we cannot compare buffer
15159 positions. */
15160 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15161 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15162 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15163 return false;
15165 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15166 w->cursor.x = x;
15167 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15168 w->cursor.y = row->y + dy;
15170 if (w == XWINDOW (selected_window))
15172 if (!row->continued_p
15173 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15174 && row->x == 0)
15176 this_line_buffer = XBUFFER (w->contents);
15178 CHARPOS (this_line_start_pos)
15179 = MATRIX_ROW_START_CHARPOS (row) + delta;
15180 BYTEPOS (this_line_start_pos)
15181 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15183 CHARPOS (this_line_end_pos)
15184 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15185 BYTEPOS (this_line_end_pos)
15186 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15188 this_line_y = w->cursor.y;
15189 this_line_pixel_height = row->height;
15190 this_line_vpos = w->cursor.vpos;
15191 this_line_start_x = row->x;
15193 else
15194 CHARPOS (this_line_start_pos) = 0;
15197 return true;
15201 /* Run window scroll functions, if any, for WINDOW with new window
15202 start STARTP. Sets the window start of WINDOW to that position.
15204 We assume that the window's buffer is really current. */
15206 static struct text_pos
15207 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15209 struct window *w = XWINDOW (window);
15210 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15212 eassert (current_buffer == XBUFFER (w->contents));
15214 if (!NILP (Vwindow_scroll_functions))
15216 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15217 make_number (CHARPOS (startp)));
15218 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15219 /* In case the hook functions switch buffers. */
15220 set_buffer_internal (XBUFFER (w->contents));
15223 return startp;
15227 /* Make sure the line containing the cursor is fully visible.
15228 A value of true means there is nothing to be done.
15229 (Either the line is fully visible, or it cannot be made so,
15230 or we cannot tell.)
15232 If FORCE_P, return false even if partial visible cursor row
15233 is higher than window.
15235 If CURRENT_MATRIX_P, use the information from the
15236 window's current glyph matrix; otherwise use the desired glyph
15237 matrix.
15239 A value of false means the caller should do scrolling
15240 as if point had gone off the screen. */
15242 static bool
15243 cursor_row_fully_visible_p (struct window *w, bool force_p,
15244 bool current_matrix_p)
15246 struct glyph_matrix *matrix;
15247 struct glyph_row *row;
15248 int window_height;
15250 if (!make_cursor_line_fully_visible_p)
15251 return true;
15253 /* It's not always possible to find the cursor, e.g, when a window
15254 is full of overlay strings. Don't do anything in that case. */
15255 if (w->cursor.vpos < 0)
15256 return true;
15258 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15259 row = MATRIX_ROW (matrix, w->cursor.vpos);
15261 /* If the cursor row is not partially visible, there's nothing to do. */
15262 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15263 return true;
15265 /* If the row the cursor is in is taller than the window's height,
15266 it's not clear what to do, so do nothing. */
15267 window_height = window_box_height (w);
15268 if (row->height >= window_height)
15270 if (!force_p || MINI_WINDOW_P (w)
15271 || w->vscroll || w->cursor.vpos == 0)
15272 return true;
15274 return false;
15278 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15279 means only WINDOW is redisplayed in redisplay_internal.
15280 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15281 in redisplay_window to bring a partially visible line into view in
15282 the case that only the cursor has moved.
15284 LAST_LINE_MISFIT should be true if we're scrolling because the
15285 last screen line's vertical height extends past the end of the screen.
15287 Value is
15289 1 if scrolling succeeded
15291 0 if scrolling didn't find point.
15293 -1 if new fonts have been loaded so that we must interrupt
15294 redisplay, adjust glyph matrices, and try again. */
15296 enum
15298 SCROLLING_SUCCESS,
15299 SCROLLING_FAILED,
15300 SCROLLING_NEED_LARGER_MATRICES
15303 /* If scroll-conservatively is more than this, never recenter.
15305 If you change this, don't forget to update the doc string of
15306 `scroll-conservatively' and the Emacs manual. */
15307 #define SCROLL_LIMIT 100
15309 static int
15310 try_scrolling (Lisp_Object window, bool just_this_one_p,
15311 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15312 bool temp_scroll_step, bool last_line_misfit)
15314 struct window *w = XWINDOW (window);
15315 struct frame *f = XFRAME (w->frame);
15316 struct text_pos pos, startp;
15317 struct it it;
15318 int this_scroll_margin, scroll_max, rc, height;
15319 int dy = 0, amount_to_scroll = 0;
15320 bool scroll_down_p = false;
15321 int extra_scroll_margin_lines = last_line_misfit;
15322 Lisp_Object aggressive;
15323 /* We will never try scrolling more than this number of lines. */
15324 int scroll_limit = SCROLL_LIMIT;
15325 int frame_line_height = default_line_pixel_height (w);
15326 int window_total_lines
15327 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15329 #ifdef GLYPH_DEBUG
15330 debug_method_add (w, "try_scrolling");
15331 #endif
15333 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15335 /* Compute scroll margin height in pixels. We scroll when point is
15336 within this distance from the top or bottom of the window. */
15337 if (scroll_margin > 0)
15338 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15339 * frame_line_height;
15340 else
15341 this_scroll_margin = 0;
15343 /* Force arg_scroll_conservatively to have a reasonable value, to
15344 avoid scrolling too far away with slow move_it_* functions. Note
15345 that the user can supply scroll-conservatively equal to
15346 `most-positive-fixnum', which can be larger than INT_MAX. */
15347 if (arg_scroll_conservatively > scroll_limit)
15349 arg_scroll_conservatively = scroll_limit + 1;
15350 scroll_max = scroll_limit * frame_line_height;
15352 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15353 /* Compute how much we should try to scroll maximally to bring
15354 point into view. */
15355 scroll_max = (max (scroll_step,
15356 max (arg_scroll_conservatively, temp_scroll_step))
15357 * frame_line_height);
15358 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15359 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15360 /* We're trying to scroll because of aggressive scrolling but no
15361 scroll_step is set. Choose an arbitrary one. */
15362 scroll_max = 10 * frame_line_height;
15363 else
15364 scroll_max = 0;
15366 too_near_end:
15368 /* Decide whether to scroll down. */
15369 if (PT > CHARPOS (startp))
15371 int scroll_margin_y;
15373 /* Compute the pixel ypos of the scroll margin, then move IT to
15374 either that ypos or PT, whichever comes first. */
15375 start_display (&it, w, startp);
15376 scroll_margin_y = it.last_visible_y - this_scroll_margin
15377 - frame_line_height * extra_scroll_margin_lines;
15378 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15379 (MOVE_TO_POS | MOVE_TO_Y));
15381 if (PT > CHARPOS (it.current.pos))
15383 int y0 = line_bottom_y (&it);
15384 /* Compute how many pixels below window bottom to stop searching
15385 for PT. This avoids costly search for PT that is far away if
15386 the user limited scrolling by a small number of lines, but
15387 always finds PT if scroll_conservatively is set to a large
15388 number, such as most-positive-fixnum. */
15389 int slack = max (scroll_max, 10 * frame_line_height);
15390 int y_to_move = it.last_visible_y + slack;
15392 /* Compute the distance from the scroll margin to PT or to
15393 the scroll limit, whichever comes first. This should
15394 include the height of the cursor line, to make that line
15395 fully visible. */
15396 move_it_to (&it, PT, -1, y_to_move,
15397 -1, MOVE_TO_POS | MOVE_TO_Y);
15398 dy = line_bottom_y (&it) - y0;
15400 if (dy > scroll_max)
15401 return SCROLLING_FAILED;
15403 if (dy > 0)
15404 scroll_down_p = true;
15406 else if (PT == IT_CHARPOS (it)
15407 && IT_CHARPOS (it) < ZV
15408 && it.method == GET_FROM_STRING
15409 && arg_scroll_conservatively > scroll_limit
15410 && it.current_x == 0)
15412 enum move_it_result skip;
15413 int y1 = it.current_y;
15414 int vpos;
15416 /* A before-string that includes newlines and is displayed
15417 on the last visible screen line could fail us under
15418 scroll-conservatively > 100, because we will be unable to
15419 position the cursor on that last visible line. Try to
15420 recover by finding the first screen line that has some
15421 glyphs coming from the buffer text. */
15422 do {
15423 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15424 if (skip != MOVE_NEWLINE_OR_CR
15425 || IT_CHARPOS (it) != PT
15426 || it.method == GET_FROM_BUFFER)
15427 break;
15428 vpos = it.vpos;
15429 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15430 } while (it.vpos > vpos);
15432 dy = it.current_y - y1;
15434 if (dy > scroll_max)
15435 return SCROLLING_FAILED;
15437 if (dy > 0)
15438 scroll_down_p = true;
15442 if (scroll_down_p)
15444 /* Point is in or below the bottom scroll margin, so move the
15445 window start down. If scrolling conservatively, move it just
15446 enough down to make point visible. If scroll_step is set,
15447 move it down by scroll_step. */
15448 if (arg_scroll_conservatively)
15449 amount_to_scroll
15450 = min (max (dy, frame_line_height),
15451 frame_line_height * arg_scroll_conservatively);
15452 else if (scroll_step || temp_scroll_step)
15453 amount_to_scroll = scroll_max;
15454 else
15456 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15457 height = WINDOW_BOX_TEXT_HEIGHT (w);
15458 if (NUMBERP (aggressive))
15460 double float_amount = XFLOATINT (aggressive) * height;
15461 int aggressive_scroll = float_amount;
15462 if (aggressive_scroll == 0 && float_amount > 0)
15463 aggressive_scroll = 1;
15464 /* Don't let point enter the scroll margin near top of
15465 the window. This could happen if the value of
15466 scroll_up_aggressively is too large and there are
15467 non-zero margins, because scroll_up_aggressively
15468 means put point that fraction of window height
15469 _from_the_bottom_margin_. */
15470 if (aggressive_scroll + 2 * this_scroll_margin > height)
15471 aggressive_scroll = height - 2 * this_scroll_margin;
15472 amount_to_scroll = dy + aggressive_scroll;
15476 if (amount_to_scroll <= 0)
15477 return SCROLLING_FAILED;
15479 start_display (&it, w, startp);
15480 if (arg_scroll_conservatively <= scroll_limit)
15481 move_it_vertically (&it, amount_to_scroll);
15482 else
15484 /* Extra precision for users who set scroll-conservatively
15485 to a large number: make sure the amount we scroll
15486 the window start is never less than amount_to_scroll,
15487 which was computed as distance from window bottom to
15488 point. This matters when lines at window top and lines
15489 below window bottom have different height. */
15490 struct it it1;
15491 void *it1data = NULL;
15492 /* We use a temporary it1 because line_bottom_y can modify
15493 its argument, if it moves one line down; see there. */
15494 int start_y;
15496 SAVE_IT (it1, it, it1data);
15497 start_y = line_bottom_y (&it1);
15498 do {
15499 RESTORE_IT (&it, &it, it1data);
15500 move_it_by_lines (&it, 1);
15501 SAVE_IT (it1, it, it1data);
15502 } while (IT_CHARPOS (it) < ZV
15503 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15504 bidi_unshelve_cache (it1data, true);
15507 /* If STARTP is unchanged, move it down another screen line. */
15508 if (IT_CHARPOS (it) == CHARPOS (startp))
15509 move_it_by_lines (&it, 1);
15510 startp = it.current.pos;
15512 else
15514 struct text_pos scroll_margin_pos = startp;
15515 int y_offset = 0;
15517 /* See if point is inside the scroll margin at the top of the
15518 window. */
15519 if (this_scroll_margin)
15521 int y_start;
15523 start_display (&it, w, startp);
15524 y_start = it.current_y;
15525 move_it_vertically (&it, this_scroll_margin);
15526 scroll_margin_pos = it.current.pos;
15527 /* If we didn't move enough before hitting ZV, request
15528 additional amount of scroll, to move point out of the
15529 scroll margin. */
15530 if (IT_CHARPOS (it) == ZV
15531 && it.current_y - y_start < this_scroll_margin)
15532 y_offset = this_scroll_margin - (it.current_y - y_start);
15535 if (PT < CHARPOS (scroll_margin_pos))
15537 /* Point is in the scroll margin at the top of the window or
15538 above what is displayed in the window. */
15539 int y0, y_to_move;
15541 /* Compute the vertical distance from PT to the scroll
15542 margin position. Move as far as scroll_max allows, or
15543 one screenful, or 10 screen lines, whichever is largest.
15544 Give up if distance is greater than scroll_max or if we
15545 didn't reach the scroll margin position. */
15546 SET_TEXT_POS (pos, PT, PT_BYTE);
15547 start_display (&it, w, pos);
15548 y0 = it.current_y;
15549 y_to_move = max (it.last_visible_y,
15550 max (scroll_max, 10 * frame_line_height));
15551 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15552 y_to_move, -1,
15553 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15554 dy = it.current_y - y0;
15555 if (dy > scroll_max
15556 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15557 return SCROLLING_FAILED;
15559 /* Additional scroll for when ZV was too close to point. */
15560 dy += y_offset;
15562 /* Compute new window start. */
15563 start_display (&it, w, startp);
15565 if (arg_scroll_conservatively)
15566 amount_to_scroll = max (dy, frame_line_height
15567 * max (scroll_step, temp_scroll_step));
15568 else if (scroll_step || temp_scroll_step)
15569 amount_to_scroll = scroll_max;
15570 else
15572 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15573 height = WINDOW_BOX_TEXT_HEIGHT (w);
15574 if (NUMBERP (aggressive))
15576 double float_amount = XFLOATINT (aggressive) * height;
15577 int aggressive_scroll = float_amount;
15578 if (aggressive_scroll == 0 && float_amount > 0)
15579 aggressive_scroll = 1;
15580 /* Don't let point enter the scroll margin near
15581 bottom of the window, if the value of
15582 scroll_down_aggressively happens to be too
15583 large. */
15584 if (aggressive_scroll + 2 * this_scroll_margin > height)
15585 aggressive_scroll = height - 2 * this_scroll_margin;
15586 amount_to_scroll = dy + aggressive_scroll;
15590 if (amount_to_scroll <= 0)
15591 return SCROLLING_FAILED;
15593 move_it_vertically_backward (&it, amount_to_scroll);
15594 startp = it.current.pos;
15598 /* Run window scroll functions. */
15599 startp = run_window_scroll_functions (window, startp);
15601 /* Display the window. Give up if new fonts are loaded, or if point
15602 doesn't appear. */
15603 if (!try_window (window, startp, 0))
15604 rc = SCROLLING_NEED_LARGER_MATRICES;
15605 else if (w->cursor.vpos < 0)
15607 clear_glyph_matrix (w->desired_matrix);
15608 rc = SCROLLING_FAILED;
15610 else
15612 /* Maybe forget recorded base line for line number display. */
15613 if (!just_this_one_p
15614 || current_buffer->clip_changed
15615 || BEG_UNCHANGED < CHARPOS (startp))
15616 w->base_line_number = 0;
15618 /* If cursor ends up on a partially visible line,
15619 treat that as being off the bottom of the screen. */
15620 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15621 false)
15622 /* It's possible that the cursor is on the first line of the
15623 buffer, which is partially obscured due to a vscroll
15624 (Bug#7537). In that case, avoid looping forever. */
15625 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15627 clear_glyph_matrix (w->desired_matrix);
15628 ++extra_scroll_margin_lines;
15629 goto too_near_end;
15631 rc = SCROLLING_SUCCESS;
15634 return rc;
15638 /* Compute a suitable window start for window W if display of W starts
15639 on a continuation line. Value is true if a new window start
15640 was computed.
15642 The new window start will be computed, based on W's width, starting
15643 from the start of the continued line. It is the start of the
15644 screen line with the minimum distance from the old start W->start,
15645 which is still before point (otherwise point will definitely not
15646 be visible in the window). */
15648 static bool
15649 compute_window_start_on_continuation_line (struct window *w)
15651 struct text_pos pos, start_pos, pos_before_pt;
15652 bool window_start_changed_p = false;
15654 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15656 /* If window start is on a continuation line... Window start may be
15657 < BEGV in case there's invisible text at the start of the
15658 buffer (M-x rmail, for example). */
15659 if (CHARPOS (start_pos) > BEGV
15660 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15662 struct it it;
15663 struct glyph_row *row;
15665 /* Handle the case that the window start is out of range. */
15666 if (CHARPOS (start_pos) < BEGV)
15667 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15668 else if (CHARPOS (start_pos) > ZV)
15669 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15671 /* Find the start of the continued line. This should be fast
15672 because find_newline is fast (newline cache). */
15673 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15674 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15675 row, DEFAULT_FACE_ID);
15676 reseat_at_previous_visible_line_start (&it);
15678 /* If the line start is "too far" away from the window start,
15679 say it takes too much time to compute a new window start.
15680 Also, give up if the line start is after point, as in that
15681 case point will not be visible with any window start we
15682 compute. */
15683 if (IT_CHARPOS (it) <= PT
15684 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15685 /* PXW: Do we need upper bounds here? */
15686 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15688 int min_distance, distance;
15690 /* Move forward by display lines to find the new window
15691 start. If window width was enlarged, the new start can
15692 be expected to be > the old start. If window width was
15693 decreased, the new window start will be < the old start.
15694 So, we're looking for the display line start with the
15695 minimum distance from the old window start. */
15696 pos_before_pt = pos = it.current.pos;
15697 min_distance = INFINITY;
15698 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15699 distance < min_distance)
15701 min_distance = distance;
15702 if (CHARPOS (pos) <= PT)
15703 pos_before_pt = pos;
15704 pos = it.current.pos;
15705 if (it.line_wrap == WORD_WRAP)
15707 /* Under WORD_WRAP, move_it_by_lines is likely to
15708 overshoot and stop not at the first, but the
15709 second character from the left margin. So in
15710 that case, we need a more tight control on the X
15711 coordinate of the iterator than move_it_by_lines
15712 promises in its contract. The method is to first
15713 go to the last (rightmost) visible character of a
15714 line, then move to the leftmost character on the
15715 next line in a separate call. */
15716 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15717 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15718 move_it_to (&it, ZV, 0,
15719 it.current_y + it.max_ascent + it.max_descent, -1,
15720 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15722 else
15723 move_it_by_lines (&it, 1);
15726 /* It makes very little sense to make the new window start
15727 after point, as point won't be visible. If that's what
15728 the loop above finds, fall back on the candidate before
15729 or at point that is closest to the old window start. */
15730 if (CHARPOS (pos) > PT)
15731 pos = pos_before_pt;
15733 /* Set the window start there. */
15734 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15735 window_start_changed_p = true;
15739 return window_start_changed_p;
15743 /* Try cursor movement in case text has not changed in window WINDOW,
15744 with window start STARTP. Value is
15746 CURSOR_MOVEMENT_SUCCESS if successful
15748 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15750 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15751 display. *SCROLL_STEP is set to true, under certain circumstances, if
15752 we want to scroll as if scroll-step were set to 1. See the code.
15754 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15755 which case we have to abort this redisplay, and adjust matrices
15756 first. */
15758 enum
15760 CURSOR_MOVEMENT_SUCCESS,
15761 CURSOR_MOVEMENT_CANNOT_BE_USED,
15762 CURSOR_MOVEMENT_MUST_SCROLL,
15763 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15766 static int
15767 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15768 bool *scroll_step)
15770 struct window *w = XWINDOW (window);
15771 struct frame *f = XFRAME (w->frame);
15772 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15774 #ifdef GLYPH_DEBUG
15775 if (inhibit_try_cursor_movement)
15776 return rc;
15777 #endif
15779 /* Previously, there was a check for Lisp integer in the
15780 if-statement below. Now, this field is converted to
15781 ptrdiff_t, thus zero means invalid position in a buffer. */
15782 eassert (w->last_point > 0);
15783 /* Likewise there was a check whether window_end_vpos is nil or larger
15784 than the window. Now window_end_vpos is int and so never nil, but
15785 let's leave eassert to check whether it fits in the window. */
15786 eassert (!w->window_end_valid
15787 || w->window_end_vpos < w->current_matrix->nrows);
15789 /* Handle case where text has not changed, only point, and it has
15790 not moved off the frame. */
15791 if (/* Point may be in this window. */
15792 PT >= CHARPOS (startp)
15793 /* Selective display hasn't changed. */
15794 && !current_buffer->clip_changed
15795 /* Function force-mode-line-update is used to force a thorough
15796 redisplay. It sets either windows_or_buffers_changed or
15797 update_mode_lines. So don't take a shortcut here for these
15798 cases. */
15799 && !update_mode_lines
15800 && !windows_or_buffers_changed
15801 && !f->cursor_type_changed
15802 && NILP (Vshow_trailing_whitespace)
15803 /* This code is not used for mini-buffer for the sake of the case
15804 of redisplaying to replace an echo area message; since in
15805 that case the mini-buffer contents per se are usually
15806 unchanged. This code is of no real use in the mini-buffer
15807 since the handling of this_line_start_pos, etc., in redisplay
15808 handles the same cases. */
15809 && !EQ (window, minibuf_window)
15810 && (FRAME_WINDOW_P (f)
15811 || !overlay_arrow_in_current_buffer_p ()))
15813 int this_scroll_margin, top_scroll_margin;
15814 struct glyph_row *row = NULL;
15815 int frame_line_height = default_line_pixel_height (w);
15816 int window_total_lines
15817 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15819 #ifdef GLYPH_DEBUG
15820 debug_method_add (w, "cursor movement");
15821 #endif
15823 /* Scroll if point within this distance from the top or bottom
15824 of the window. This is a pixel value. */
15825 if (scroll_margin > 0)
15827 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15828 this_scroll_margin *= frame_line_height;
15830 else
15831 this_scroll_margin = 0;
15833 top_scroll_margin = this_scroll_margin;
15834 if (WINDOW_WANTS_HEADER_LINE_P (w))
15835 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15837 /* Start with the row the cursor was displayed during the last
15838 not paused redisplay. Give up if that row is not valid. */
15839 if (w->last_cursor_vpos < 0
15840 || w->last_cursor_vpos >= w->current_matrix->nrows)
15841 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15842 else
15844 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15845 if (row->mode_line_p)
15846 ++row;
15847 if (!row->enabled_p)
15848 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15851 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15853 bool scroll_p = false, must_scroll = false;
15854 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15856 if (PT > w->last_point)
15858 /* Point has moved forward. */
15859 while (MATRIX_ROW_END_CHARPOS (row) < PT
15860 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15862 eassert (row->enabled_p);
15863 ++row;
15866 /* If the end position of a row equals the start
15867 position of the next row, and PT is at that position,
15868 we would rather display cursor in the next line. */
15869 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15870 && MATRIX_ROW_END_CHARPOS (row) == PT
15871 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15872 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15873 && !cursor_row_p (row))
15874 ++row;
15876 /* If within the scroll margin, scroll. Note that
15877 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15878 the next line would be drawn, and that
15879 this_scroll_margin can be zero. */
15880 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15881 || PT > MATRIX_ROW_END_CHARPOS (row)
15882 /* Line is completely visible last line in window
15883 and PT is to be set in the next line. */
15884 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15885 && PT == MATRIX_ROW_END_CHARPOS (row)
15886 && !row->ends_at_zv_p
15887 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15888 scroll_p = true;
15890 else if (PT < w->last_point)
15892 /* Cursor has to be moved backward. Note that PT >=
15893 CHARPOS (startp) because of the outer if-statement. */
15894 while (!row->mode_line_p
15895 && (MATRIX_ROW_START_CHARPOS (row) > PT
15896 || (MATRIX_ROW_START_CHARPOS (row) == PT
15897 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15898 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15899 row > w->current_matrix->rows
15900 && (row-1)->ends_in_newline_from_string_p))))
15901 && (row->y > top_scroll_margin
15902 || CHARPOS (startp) == BEGV))
15904 eassert (row->enabled_p);
15905 --row;
15908 /* Consider the following case: Window starts at BEGV,
15909 there is invisible, intangible text at BEGV, so that
15910 display starts at some point START > BEGV. It can
15911 happen that we are called with PT somewhere between
15912 BEGV and START. Try to handle that case. */
15913 if (row < w->current_matrix->rows
15914 || row->mode_line_p)
15916 row = w->current_matrix->rows;
15917 if (row->mode_line_p)
15918 ++row;
15921 /* Due to newlines in overlay strings, we may have to
15922 skip forward over overlay strings. */
15923 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15924 && MATRIX_ROW_END_CHARPOS (row) == PT
15925 && !cursor_row_p (row))
15926 ++row;
15928 /* If within the scroll margin, scroll. */
15929 if (row->y < top_scroll_margin
15930 && CHARPOS (startp) != BEGV)
15931 scroll_p = true;
15933 else
15935 /* Cursor did not move. So don't scroll even if cursor line
15936 is partially visible, as it was so before. */
15937 rc = CURSOR_MOVEMENT_SUCCESS;
15940 if (PT < MATRIX_ROW_START_CHARPOS (row)
15941 || PT > MATRIX_ROW_END_CHARPOS (row))
15943 /* if PT is not in the glyph row, give up. */
15944 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15945 must_scroll = true;
15947 else if (rc != CURSOR_MOVEMENT_SUCCESS
15948 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15950 struct glyph_row *row1;
15952 /* If rows are bidi-reordered and point moved, back up
15953 until we find a row that does not belong to a
15954 continuation line. This is because we must consider
15955 all rows of a continued line as candidates for the
15956 new cursor positioning, since row start and end
15957 positions change non-linearly with vertical position
15958 in such rows. */
15959 /* FIXME: Revisit this when glyph ``spilling'' in
15960 continuation lines' rows is implemented for
15961 bidi-reordered rows. */
15962 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15963 MATRIX_ROW_CONTINUATION_LINE_P (row);
15964 --row)
15966 /* If we hit the beginning of the displayed portion
15967 without finding the first row of a continued
15968 line, give up. */
15969 if (row <= row1)
15971 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15972 break;
15974 eassert (row->enabled_p);
15977 if (must_scroll)
15979 else if (rc != CURSOR_MOVEMENT_SUCCESS
15980 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15981 /* Make sure this isn't a header line by any chance, since
15982 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15983 && !row->mode_line_p
15984 && make_cursor_line_fully_visible_p)
15986 if (PT == MATRIX_ROW_END_CHARPOS (row)
15987 && !row->ends_at_zv_p
15988 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15990 else if (row->height > window_box_height (w))
15992 /* If we end up in a partially visible line, let's
15993 make it fully visible, except when it's taller
15994 than the window, in which case we can't do much
15995 about it. */
15996 *scroll_step = true;
15997 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15999 else
16001 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16002 if (!cursor_row_fully_visible_p (w, false, true))
16003 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16004 else
16005 rc = CURSOR_MOVEMENT_SUCCESS;
16008 else if (scroll_p)
16009 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16010 else if (rc != CURSOR_MOVEMENT_SUCCESS
16011 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16013 /* With bidi-reordered rows, there could be more than
16014 one candidate row whose start and end positions
16015 occlude point. We need to let set_cursor_from_row
16016 find the best candidate. */
16017 /* FIXME: Revisit this when glyph ``spilling'' in
16018 continuation lines' rows is implemented for
16019 bidi-reordered rows. */
16020 bool rv = false;
16024 bool at_zv_p = false, exact_match_p = false;
16026 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16027 && PT <= MATRIX_ROW_END_CHARPOS (row)
16028 && cursor_row_p (row))
16029 rv |= set_cursor_from_row (w, row, w->current_matrix,
16030 0, 0, 0, 0);
16031 /* As soon as we've found the exact match for point,
16032 or the first suitable row whose ends_at_zv_p flag
16033 is set, we are done. */
16034 if (rv)
16036 at_zv_p = MATRIX_ROW (w->current_matrix,
16037 w->cursor.vpos)->ends_at_zv_p;
16038 if (!at_zv_p
16039 && w->cursor.hpos >= 0
16040 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16041 w->cursor.vpos))
16043 struct glyph_row *candidate =
16044 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16045 struct glyph *g =
16046 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16047 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16049 exact_match_p =
16050 (BUFFERP (g->object) && g->charpos == PT)
16051 || (NILP (g->object)
16052 && (g->charpos == PT
16053 || (g->charpos == 0 && endpos - 1 == PT)));
16055 if (at_zv_p || exact_match_p)
16057 rc = CURSOR_MOVEMENT_SUCCESS;
16058 break;
16061 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16062 break;
16063 ++row;
16065 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16066 || row->continued_p)
16067 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16068 || (MATRIX_ROW_START_CHARPOS (row) == PT
16069 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16070 /* If we didn't find any candidate rows, or exited the
16071 loop before all the candidates were examined, signal
16072 to the caller that this method failed. */
16073 if (rc != CURSOR_MOVEMENT_SUCCESS
16074 && !(rv
16075 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16076 && !row->continued_p))
16077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16078 else if (rv)
16079 rc = CURSOR_MOVEMENT_SUCCESS;
16081 else
16085 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16087 rc = CURSOR_MOVEMENT_SUCCESS;
16088 break;
16090 ++row;
16092 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16093 && MATRIX_ROW_START_CHARPOS (row) == PT
16094 && cursor_row_p (row));
16099 return rc;
16103 void
16104 set_vertical_scroll_bar (struct window *w)
16106 ptrdiff_t start, end, whole;
16108 /* Calculate the start and end positions for the current window.
16109 At some point, it would be nice to choose between scrollbars
16110 which reflect the whole buffer size, with special markers
16111 indicating narrowing, and scrollbars which reflect only the
16112 visible region.
16114 Note that mini-buffers sometimes aren't displaying any text. */
16115 if (!MINI_WINDOW_P (w)
16116 || (w == XWINDOW (minibuf_window)
16117 && NILP (echo_area_buffer[0])))
16119 struct buffer *buf = XBUFFER (w->contents);
16120 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16121 start = marker_position (w->start) - BUF_BEGV (buf);
16122 /* I don't think this is guaranteed to be right. For the
16123 moment, we'll pretend it is. */
16124 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16126 if (end < start)
16127 end = start;
16128 if (whole < (end - start))
16129 whole = end - start;
16131 else
16132 start = end = whole = 0;
16134 /* Indicate what this scroll bar ought to be displaying now. */
16135 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16136 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16137 (w, end - start, whole, start);
16141 void
16142 set_horizontal_scroll_bar (struct window *w)
16144 int start, end, whole, portion;
16146 if (!MINI_WINDOW_P (w)
16147 || (w == XWINDOW (minibuf_window)
16148 && NILP (echo_area_buffer[0])))
16150 struct buffer *b = XBUFFER (w->contents);
16151 struct buffer *old_buffer = NULL;
16152 struct it it;
16153 struct text_pos startp;
16155 if (b != current_buffer)
16157 old_buffer = current_buffer;
16158 set_buffer_internal (b);
16161 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16162 start_display (&it, w, startp);
16163 it.last_visible_x = INT_MAX;
16164 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16165 MOVE_TO_X | MOVE_TO_Y);
16166 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16167 window_box_height (w), -1,
16168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16170 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16171 end = start + window_box_width (w, TEXT_AREA);
16172 portion = end - start;
16173 /* After enlarging a horizontally scrolled window such that it
16174 gets at least as wide as the text it contains, make sure that
16175 the thumb doesn't fill the entire scroll bar so we can still
16176 drag it back to see the entire text. */
16177 whole = max (whole, end);
16179 if (it.bidi_p)
16181 Lisp_Object pdir;
16183 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16184 if (EQ (pdir, Qright_to_left))
16186 start = whole - end;
16187 end = start + portion;
16191 if (old_buffer)
16192 set_buffer_internal (old_buffer);
16194 else
16195 start = end = whole = portion = 0;
16197 w->hscroll_whole = whole;
16199 /* Indicate what this scroll bar ought to be displaying now. */
16200 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16201 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16202 (w, portion, whole, start);
16206 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16207 selected_window is redisplayed.
16209 We can return without actually redisplaying the window if fonts has been
16210 changed on window's frame. In that case, redisplay_internal will retry.
16212 As one of the important parts of redisplaying a window, we need to
16213 decide whether the previous window-start position (stored in the
16214 window's w->start marker position) is still valid, and if it isn't,
16215 recompute it. Some details about that:
16217 . The previous window-start could be in a continuation line, in
16218 which case we need to recompute it when the window width
16219 changes. See compute_window_start_on_continuation_line and its
16220 call below.
16222 . The text that changed since last redisplay could include the
16223 previous window-start position. In that case, we try to salvage
16224 what we can from the current glyph matrix by calling
16225 try_scrolling, which see.
16227 . Some Emacs command could force us to use a specific window-start
16228 position by setting the window's force_start flag, or gently
16229 propose doing that by setting the window's optional_new_start
16230 flag. In these cases, we try using the specified start point if
16231 that succeeds (i.e. the window desired matrix is successfully
16232 recomputed, and point location is within the window). In case
16233 of optional_new_start, we first check if the specified start
16234 position is feasible, i.e. if it will allow point to be
16235 displayed in the window. If using the specified start point
16236 fails, e.g., if new fonts are needed to be loaded, we abort the
16237 redisplay cycle and leave it up to the next cycle to figure out
16238 things.
16240 . Note that the window's force_start flag is sometimes set by
16241 redisplay itself, when it decides that the previous window start
16242 point is fine and should be kept. Search for "goto force_start"
16243 below to see the details. Like the values of window-start
16244 specified outside of redisplay, these internally-deduced values
16245 are tested for feasibility, and ignored if found to be
16246 unfeasible.
16248 . Note that the function try_window, used to completely redisplay
16249 a window, accepts the window's start point as its argument.
16250 This is used several times in the redisplay code to control
16251 where the window start will be, according to user options such
16252 as scroll-conservatively, and also to ensure the screen line
16253 showing point will be fully (as opposed to partially) visible on
16254 display. */
16256 static void
16257 redisplay_window (Lisp_Object window, bool just_this_one_p)
16259 struct window *w = XWINDOW (window);
16260 struct frame *f = XFRAME (w->frame);
16261 struct buffer *buffer = XBUFFER (w->contents);
16262 struct buffer *old = current_buffer;
16263 struct text_pos lpoint, opoint, startp;
16264 bool update_mode_line;
16265 int tem;
16266 struct it it;
16267 /* Record it now because it's overwritten. */
16268 bool current_matrix_up_to_date_p = false;
16269 bool used_current_matrix_p = false;
16270 /* This is less strict than current_matrix_up_to_date_p.
16271 It indicates that the buffer contents and narrowing are unchanged. */
16272 bool buffer_unchanged_p = false;
16273 bool temp_scroll_step = false;
16274 ptrdiff_t count = SPECPDL_INDEX ();
16275 int rc;
16276 int centering_position = -1;
16277 bool last_line_misfit = false;
16278 ptrdiff_t beg_unchanged, end_unchanged;
16279 int frame_line_height;
16280 bool use_desired_matrix;
16281 void *itdata = NULL;
16283 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16284 opoint = lpoint;
16286 #ifdef GLYPH_DEBUG
16287 *w->desired_matrix->method = 0;
16288 #endif
16290 if (!just_this_one_p
16291 && REDISPLAY_SOME_P ()
16292 && !w->redisplay
16293 && !w->update_mode_line
16294 && !f->face_change
16295 && !f->redisplay
16296 && !buffer->text->redisplay
16297 && BUF_PT (buffer) == w->last_point)
16298 return;
16300 /* Make sure that both W's markers are valid. */
16301 eassert (XMARKER (w->start)->buffer == buffer);
16302 eassert (XMARKER (w->pointm)->buffer == buffer);
16304 /* We come here again if we need to run window-text-change-functions
16305 below. */
16306 restart:
16307 reconsider_clip_changes (w);
16308 frame_line_height = default_line_pixel_height (w);
16310 /* Has the mode line to be updated? */
16311 update_mode_line = (w->update_mode_line
16312 || update_mode_lines
16313 || buffer->clip_changed
16314 || buffer->prevent_redisplay_optimizations_p);
16316 if (!just_this_one_p)
16317 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16318 cleverly elsewhere. */
16319 w->must_be_updated_p = true;
16321 if (MINI_WINDOW_P (w))
16323 if (w == XWINDOW (echo_area_window)
16324 && !NILP (echo_area_buffer[0]))
16326 if (update_mode_line)
16327 /* We may have to update a tty frame's menu bar or a
16328 tool-bar. Example `M-x C-h C-h C-g'. */
16329 goto finish_menu_bars;
16330 else
16331 /* We've already displayed the echo area glyphs in this window. */
16332 goto finish_scroll_bars;
16334 else if ((w != XWINDOW (minibuf_window)
16335 || minibuf_level == 0)
16336 /* When buffer is nonempty, redisplay window normally. */
16337 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16338 /* Quail displays non-mini buffers in minibuffer window.
16339 In that case, redisplay the window normally. */
16340 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16342 /* W is a mini-buffer window, but it's not active, so clear
16343 it. */
16344 int yb = window_text_bottom_y (w);
16345 struct glyph_row *row;
16346 int y;
16348 for (y = 0, row = w->desired_matrix->rows;
16349 y < yb;
16350 y += row->height, ++row)
16351 blank_row (w, row, y);
16352 goto finish_scroll_bars;
16355 clear_glyph_matrix (w->desired_matrix);
16358 /* Otherwise set up data on this window; select its buffer and point
16359 value. */
16360 /* Really select the buffer, for the sake of buffer-local
16361 variables. */
16362 set_buffer_internal_1 (XBUFFER (w->contents));
16364 current_matrix_up_to_date_p
16365 = (w->window_end_valid
16366 && !current_buffer->clip_changed
16367 && !current_buffer->prevent_redisplay_optimizations_p
16368 && !window_outdated (w));
16370 /* Run the window-text-change-functions
16371 if it is possible that the text on the screen has changed
16372 (either due to modification of the text, or any other reason). */
16373 if (!current_matrix_up_to_date_p
16374 && !NILP (Vwindow_text_change_functions))
16376 safe_run_hooks (Qwindow_text_change_functions);
16377 goto restart;
16380 beg_unchanged = BEG_UNCHANGED;
16381 end_unchanged = END_UNCHANGED;
16383 SET_TEXT_POS (opoint, PT, PT_BYTE);
16385 specbind (Qinhibit_point_motion_hooks, Qt);
16387 buffer_unchanged_p
16388 = (w->window_end_valid
16389 && !current_buffer->clip_changed
16390 && !window_outdated (w));
16392 /* When windows_or_buffers_changed is non-zero, we can't rely
16393 on the window end being valid, so set it to zero there. */
16394 if (windows_or_buffers_changed)
16396 /* If window starts on a continuation line, maybe adjust the
16397 window start in case the window's width changed. */
16398 if (XMARKER (w->start)->buffer == current_buffer)
16399 compute_window_start_on_continuation_line (w);
16401 w->window_end_valid = false;
16402 /* If so, we also can't rely on current matrix
16403 and should not fool try_cursor_movement below. */
16404 current_matrix_up_to_date_p = false;
16407 /* Some sanity checks. */
16408 CHECK_WINDOW_END (w);
16409 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16410 emacs_abort ();
16411 if (BYTEPOS (opoint) < CHARPOS (opoint))
16412 emacs_abort ();
16414 if (mode_line_update_needed (w))
16415 update_mode_line = true;
16417 /* Point refers normally to the selected window. For any other
16418 window, set up appropriate value. */
16419 if (!EQ (window, selected_window))
16421 ptrdiff_t new_pt = marker_position (w->pointm);
16422 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16424 if (new_pt < BEGV)
16426 new_pt = BEGV;
16427 new_pt_byte = BEGV_BYTE;
16428 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16430 else if (new_pt > (ZV - 1))
16432 new_pt = ZV;
16433 new_pt_byte = ZV_BYTE;
16434 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16437 /* We don't use SET_PT so that the point-motion hooks don't run. */
16438 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16441 /* If any of the character widths specified in the display table
16442 have changed, invalidate the width run cache. It's true that
16443 this may be a bit late to catch such changes, but the rest of
16444 redisplay goes (non-fatally) haywire when the display table is
16445 changed, so why should we worry about doing any better? */
16446 if (current_buffer->width_run_cache
16447 || (current_buffer->base_buffer
16448 && current_buffer->base_buffer->width_run_cache))
16450 struct Lisp_Char_Table *disptab = buffer_display_table ();
16452 if (! disptab_matches_widthtab
16453 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16455 struct buffer *buf = current_buffer;
16457 if (buf->base_buffer)
16458 buf = buf->base_buffer;
16459 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16460 recompute_width_table (current_buffer, disptab);
16464 /* If window-start is screwed up, choose a new one. */
16465 if (XMARKER (w->start)->buffer != current_buffer)
16466 goto recenter;
16468 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16470 /* If someone specified a new starting point but did not insist,
16471 check whether it can be used. */
16472 if ((w->optional_new_start || window_frozen_p (w))
16473 && CHARPOS (startp) >= BEGV
16474 && CHARPOS (startp) <= ZV)
16476 ptrdiff_t it_charpos;
16478 w->optional_new_start = false;
16479 start_display (&it, w, startp);
16480 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16481 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16482 /* Record IT's position now, since line_bottom_y might change
16483 that. */
16484 it_charpos = IT_CHARPOS (it);
16485 /* Make sure we set the force_start flag only if the cursor row
16486 will be fully visible. Otherwise, the code under force_start
16487 label below will try to move point back into view, which is
16488 not what the code which sets optional_new_start wants. */
16489 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16490 && !w->force_start)
16492 if (it_charpos == PT)
16493 w->force_start = true;
16494 /* IT may overshoot PT if text at PT is invisible. */
16495 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16496 w->force_start = true;
16497 #ifdef GLYPH_DEBUG
16498 if (w->force_start)
16500 if (window_frozen_p (w))
16501 debug_method_add (w, "set force_start from frozen window start");
16502 else
16503 debug_method_add (w, "set force_start from optional_new_start");
16505 #endif
16509 force_start:
16511 /* Handle case where place to start displaying has been specified,
16512 unless the specified location is outside the accessible range. */
16513 if (w->force_start)
16515 /* We set this later on if we have to adjust point. */
16516 int new_vpos = -1;
16518 w->force_start = false;
16519 w->vscroll = 0;
16520 w->window_end_valid = false;
16522 /* Forget any recorded base line for line number display. */
16523 if (!buffer_unchanged_p)
16524 w->base_line_number = 0;
16526 /* Redisplay the mode line. Select the buffer properly for that.
16527 Also, run the hook window-scroll-functions
16528 because we have scrolled. */
16529 /* Note, we do this after clearing force_start because
16530 if there's an error, it is better to forget about force_start
16531 than to get into an infinite loop calling the hook functions
16532 and having them get more errors. */
16533 if (!update_mode_line
16534 || ! NILP (Vwindow_scroll_functions))
16536 update_mode_line = true;
16537 w->update_mode_line = true;
16538 startp = run_window_scroll_functions (window, startp);
16541 if (CHARPOS (startp) < BEGV)
16542 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16543 else if (CHARPOS (startp) > ZV)
16544 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16546 /* Redisplay, then check if cursor has been set during the
16547 redisplay. Give up if new fonts were loaded. */
16548 /* We used to issue a CHECK_MARGINS argument to try_window here,
16549 but this causes scrolling to fail when point begins inside
16550 the scroll margin (bug#148) -- cyd */
16551 if (!try_window (window, startp, 0))
16553 w->force_start = true;
16554 clear_glyph_matrix (w->desired_matrix);
16555 goto need_larger_matrices;
16558 if (w->cursor.vpos < 0)
16560 /* If point does not appear, try to move point so it does
16561 appear. The desired matrix has been built above, so we
16562 can use it here. First see if point is in invisible
16563 text, and if so, move it to the first visible buffer
16564 position past that. */
16565 struct glyph_row *r = NULL;
16566 Lisp_Object invprop =
16567 get_char_property_and_overlay (make_number (PT), Qinvisible,
16568 Qnil, NULL);
16570 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16572 ptrdiff_t alt_pt;
16573 Lisp_Object invprop_end =
16574 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16575 Qnil, Qnil);
16577 if (NATNUMP (invprop_end))
16578 alt_pt = XFASTINT (invprop_end);
16579 else
16580 alt_pt = ZV;
16581 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16582 NULL, 0);
16584 if (r)
16585 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16586 else /* Give up and just move to the middle of the window. */
16587 new_vpos = window_box_height (w) / 2;
16590 if (!cursor_row_fully_visible_p (w, false, false))
16592 /* Point does appear, but on a line partly visible at end of window.
16593 Move it back to a fully-visible line. */
16594 new_vpos = window_box_height (w);
16595 /* But if window_box_height suggests a Y coordinate that is
16596 not less than we already have, that line will clearly not
16597 be fully visible, so give up and scroll the display.
16598 This can happen when the default face uses a font whose
16599 dimensions are different from the frame's default
16600 font. */
16601 if (new_vpos >= w->cursor.y)
16603 w->cursor.vpos = -1;
16604 clear_glyph_matrix (w->desired_matrix);
16605 goto try_to_scroll;
16608 else if (w->cursor.vpos >= 0)
16610 /* Some people insist on not letting point enter the scroll
16611 margin, even though this part handles windows that didn't
16612 scroll at all. */
16613 int window_total_lines
16614 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16615 int margin = min (scroll_margin, window_total_lines / 4);
16616 int pixel_margin = margin * frame_line_height;
16617 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16619 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16620 below, which finds the row to move point to, advances by
16621 the Y coordinate of the _next_ row, see the definition of
16622 MATRIX_ROW_BOTTOM_Y. */
16623 if (w->cursor.vpos < margin + header_line)
16625 w->cursor.vpos = -1;
16626 clear_glyph_matrix (w->desired_matrix);
16627 goto try_to_scroll;
16629 else
16631 int window_height = window_box_height (w);
16633 if (header_line)
16634 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16635 if (w->cursor.y >= window_height - pixel_margin)
16637 w->cursor.vpos = -1;
16638 clear_glyph_matrix (w->desired_matrix);
16639 goto try_to_scroll;
16644 /* If we need to move point for either of the above reasons,
16645 now actually do it. */
16646 if (new_vpos >= 0)
16648 struct glyph_row *row;
16650 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16651 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16652 ++row;
16654 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16655 MATRIX_ROW_START_BYTEPOS (row));
16657 if (w != XWINDOW (selected_window))
16658 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16659 else if (current_buffer == old)
16660 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16662 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16664 /* Re-run pre-redisplay-function so it can update the region
16665 according to the new position of point. */
16666 /* Other than the cursor, w's redisplay is done so we can set its
16667 redisplay to false. Also the buffer's redisplay can be set to
16668 false, since propagate_buffer_redisplay should have already
16669 propagated its info to `w' anyway. */
16670 w->redisplay = false;
16671 XBUFFER (w->contents)->text->redisplay = false;
16672 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16674 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16676 /* pre-redisplay-function made changes (e.g. move the region)
16677 that require another round of redisplay. */
16678 clear_glyph_matrix (w->desired_matrix);
16679 if (!try_window (window, startp, 0))
16680 goto need_larger_matrices;
16683 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16685 clear_glyph_matrix (w->desired_matrix);
16686 goto try_to_scroll;
16689 #ifdef GLYPH_DEBUG
16690 debug_method_add (w, "forced window start");
16691 #endif
16692 goto done;
16695 /* Handle case where text has not changed, only point, and it has
16696 not moved off the frame, and we are not retrying after hscroll.
16697 (current_matrix_up_to_date_p is true when retrying.) */
16698 if (current_matrix_up_to_date_p
16699 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16700 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16702 switch (rc)
16704 case CURSOR_MOVEMENT_SUCCESS:
16705 used_current_matrix_p = true;
16706 goto done;
16708 case CURSOR_MOVEMENT_MUST_SCROLL:
16709 goto try_to_scroll;
16711 default:
16712 emacs_abort ();
16715 /* If current starting point was originally the beginning of a line
16716 but no longer is, find a new starting point. */
16717 else if (w->start_at_line_beg
16718 && !(CHARPOS (startp) <= BEGV
16719 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16721 #ifdef GLYPH_DEBUG
16722 debug_method_add (w, "recenter 1");
16723 #endif
16724 goto recenter;
16727 /* Try scrolling with try_window_id. Value is > 0 if update has
16728 been done, it is -1 if we know that the same window start will
16729 not work. It is 0 if unsuccessful for some other reason. */
16730 else if ((tem = try_window_id (w)) != 0)
16732 #ifdef GLYPH_DEBUG
16733 debug_method_add (w, "try_window_id %d", tem);
16734 #endif
16736 if (f->fonts_changed)
16737 goto need_larger_matrices;
16738 if (tem > 0)
16739 goto done;
16741 /* Otherwise try_window_id has returned -1 which means that we
16742 don't want the alternative below this comment to execute. */
16744 else if (CHARPOS (startp) >= BEGV
16745 && CHARPOS (startp) <= ZV
16746 && PT >= CHARPOS (startp)
16747 && (CHARPOS (startp) < ZV
16748 /* Avoid starting at end of buffer. */
16749 || CHARPOS (startp) == BEGV
16750 || !window_outdated (w)))
16752 int d1, d2, d5, d6;
16753 int rtop, rbot;
16755 /* If first window line is a continuation line, and window start
16756 is inside the modified region, but the first change is before
16757 current window start, we must select a new window start.
16759 However, if this is the result of a down-mouse event (e.g. by
16760 extending the mouse-drag-overlay), we don't want to select a
16761 new window start, since that would change the position under
16762 the mouse, resulting in an unwanted mouse-movement rather
16763 than a simple mouse-click. */
16764 if (!w->start_at_line_beg
16765 && NILP (do_mouse_tracking)
16766 && CHARPOS (startp) > BEGV
16767 && CHARPOS (startp) > BEG + beg_unchanged
16768 && CHARPOS (startp) <= Z - end_unchanged
16769 /* Even if w->start_at_line_beg is nil, a new window may
16770 start at a line_beg, since that's how set_buffer_window
16771 sets it. So, we need to check the return value of
16772 compute_window_start_on_continuation_line. (See also
16773 bug#197). */
16774 && XMARKER (w->start)->buffer == current_buffer
16775 && compute_window_start_on_continuation_line (w)
16776 /* It doesn't make sense to force the window start like we
16777 do at label force_start if it is already known that point
16778 will not be fully visible in the resulting window, because
16779 doing so will move point from its correct position
16780 instead of scrolling the window to bring point into view.
16781 See bug#9324. */
16782 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16783 /* A very tall row could need more than the window height,
16784 in which case we accept that it is partially visible. */
16785 && (rtop != 0) == (rbot != 0))
16787 w->force_start = true;
16788 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16789 #ifdef GLYPH_DEBUG
16790 debug_method_add (w, "recomputed window start in continuation line");
16791 #endif
16792 goto force_start;
16795 #ifdef GLYPH_DEBUG
16796 debug_method_add (w, "same window start");
16797 #endif
16799 /* Try to redisplay starting at same place as before.
16800 If point has not moved off frame, accept the results. */
16801 if (!current_matrix_up_to_date_p
16802 /* Don't use try_window_reusing_current_matrix in this case
16803 because a window scroll function can have changed the
16804 buffer. */
16805 || !NILP (Vwindow_scroll_functions)
16806 || MINI_WINDOW_P (w)
16807 || !(used_current_matrix_p
16808 = try_window_reusing_current_matrix (w)))
16810 IF_DEBUG (debug_method_add (w, "1"));
16811 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16812 /* -1 means we need to scroll.
16813 0 means we need new matrices, but fonts_changed
16814 is set in that case, so we will detect it below. */
16815 goto try_to_scroll;
16818 if (f->fonts_changed)
16819 goto need_larger_matrices;
16821 if (w->cursor.vpos >= 0)
16823 if (!just_this_one_p
16824 || current_buffer->clip_changed
16825 || BEG_UNCHANGED < CHARPOS (startp))
16826 /* Forget any recorded base line for line number display. */
16827 w->base_line_number = 0;
16829 if (!cursor_row_fully_visible_p (w, true, false))
16831 clear_glyph_matrix (w->desired_matrix);
16832 last_line_misfit = true;
16834 /* Drop through and scroll. */
16835 else
16836 goto done;
16838 else
16839 clear_glyph_matrix (w->desired_matrix);
16842 try_to_scroll:
16844 /* Redisplay the mode line. Select the buffer properly for that. */
16845 if (!update_mode_line)
16847 update_mode_line = true;
16848 w->update_mode_line = true;
16851 /* Try to scroll by specified few lines. */
16852 if ((scroll_conservatively
16853 || emacs_scroll_step
16854 || temp_scroll_step
16855 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16856 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16857 && CHARPOS (startp) >= BEGV
16858 && CHARPOS (startp) <= ZV)
16860 /* The function returns -1 if new fonts were loaded, 1 if
16861 successful, 0 if not successful. */
16862 int ss = try_scrolling (window, just_this_one_p,
16863 scroll_conservatively,
16864 emacs_scroll_step,
16865 temp_scroll_step, last_line_misfit);
16866 switch (ss)
16868 case SCROLLING_SUCCESS:
16869 goto done;
16871 case SCROLLING_NEED_LARGER_MATRICES:
16872 goto need_larger_matrices;
16874 case SCROLLING_FAILED:
16875 break;
16877 default:
16878 emacs_abort ();
16882 /* Finally, just choose a place to start which positions point
16883 according to user preferences. */
16885 recenter:
16887 #ifdef GLYPH_DEBUG
16888 debug_method_add (w, "recenter");
16889 #endif
16891 /* Forget any previously recorded base line for line number display. */
16892 if (!buffer_unchanged_p)
16893 w->base_line_number = 0;
16895 /* Determine the window start relative to point. */
16896 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16897 it.current_y = it.last_visible_y;
16898 if (centering_position < 0)
16900 int window_total_lines
16901 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16902 int margin
16903 = scroll_margin > 0
16904 ? min (scroll_margin, window_total_lines / 4)
16905 : 0;
16906 ptrdiff_t margin_pos = CHARPOS (startp);
16907 Lisp_Object aggressive;
16908 bool scrolling_up;
16910 /* If there is a scroll margin at the top of the window, find
16911 its character position. */
16912 if (margin
16913 /* Cannot call start_display if startp is not in the
16914 accessible region of the buffer. This can happen when we
16915 have just switched to a different buffer and/or changed
16916 its restriction. In that case, startp is initialized to
16917 the character position 1 (BEGV) because we did not yet
16918 have chance to display the buffer even once. */
16919 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16921 struct it it1;
16922 void *it1data = NULL;
16924 SAVE_IT (it1, it, it1data);
16925 start_display (&it1, w, startp);
16926 move_it_vertically (&it1, margin * frame_line_height);
16927 margin_pos = IT_CHARPOS (it1);
16928 RESTORE_IT (&it, &it, it1data);
16930 scrolling_up = PT > margin_pos;
16931 aggressive =
16932 scrolling_up
16933 ? BVAR (current_buffer, scroll_up_aggressively)
16934 : BVAR (current_buffer, scroll_down_aggressively);
16936 if (!MINI_WINDOW_P (w)
16937 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16939 int pt_offset = 0;
16941 /* Setting scroll-conservatively overrides
16942 scroll-*-aggressively. */
16943 if (!scroll_conservatively && NUMBERP (aggressive))
16945 double float_amount = XFLOATINT (aggressive);
16947 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16948 if (pt_offset == 0 && float_amount > 0)
16949 pt_offset = 1;
16950 if (pt_offset && margin > 0)
16951 margin -= 1;
16953 /* Compute how much to move the window start backward from
16954 point so that point will be displayed where the user
16955 wants it. */
16956 if (scrolling_up)
16958 centering_position = it.last_visible_y;
16959 if (pt_offset)
16960 centering_position -= pt_offset;
16961 centering_position -=
16962 (frame_line_height * (1 + margin + last_line_misfit)
16963 + WINDOW_HEADER_LINE_HEIGHT (w));
16964 /* Don't let point enter the scroll margin near top of
16965 the window. */
16966 if (centering_position < margin * frame_line_height)
16967 centering_position = margin * frame_line_height;
16969 else
16970 centering_position = margin * frame_line_height + pt_offset;
16972 else
16973 /* Set the window start half the height of the window backward
16974 from point. */
16975 centering_position = window_box_height (w) / 2;
16977 move_it_vertically_backward (&it, centering_position);
16979 eassert (IT_CHARPOS (it) >= BEGV);
16981 /* The function move_it_vertically_backward may move over more
16982 than the specified y-distance. If it->w is small, e.g. a
16983 mini-buffer window, we may end up in front of the window's
16984 display area. Start displaying at the start of the line
16985 containing PT in this case. */
16986 if (it.current_y <= 0)
16988 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16989 move_it_vertically_backward (&it, 0);
16990 it.current_y = 0;
16993 it.current_x = it.hpos = 0;
16995 /* Set the window start position here explicitly, to avoid an
16996 infinite loop in case the functions in window-scroll-functions
16997 get errors. */
16998 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17000 /* Run scroll hooks. */
17001 startp = run_window_scroll_functions (window, it.current.pos);
17003 /* We invoke try_window and try_window_reusing_current_matrix below,
17004 and they manipulate the bidi cache. Save and restore the cache
17005 state of our iterator, so we could continue using it after that. */
17006 itdata = bidi_shelve_cache ();
17008 /* Redisplay the window. */
17009 use_desired_matrix = false;
17010 if (!current_matrix_up_to_date_p
17011 || windows_or_buffers_changed
17012 || f->cursor_type_changed
17013 /* Don't use try_window_reusing_current_matrix in this case
17014 because it can have changed the buffer. */
17015 || !NILP (Vwindow_scroll_functions)
17016 || !just_this_one_p
17017 || MINI_WINDOW_P (w)
17018 || !(used_current_matrix_p
17019 = try_window_reusing_current_matrix (w)))
17020 use_desired_matrix = (try_window (window, startp, 0) == 1);
17022 bidi_unshelve_cache (itdata, false);
17024 /* If new fonts have been loaded (due to fontsets), give up. We
17025 have to start a new redisplay since we need to re-adjust glyph
17026 matrices. */
17027 if (f->fonts_changed)
17028 goto need_larger_matrices;
17030 /* If cursor did not appear assume that the middle of the window is
17031 in the first line of the window. Do it again with the next line.
17032 (Imagine a window of height 100, displaying two lines of height
17033 60. Moving back 50 from it->last_visible_y will end in the first
17034 line.) */
17035 if (w->cursor.vpos < 0)
17037 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17039 clear_glyph_matrix (w->desired_matrix);
17040 move_it_by_lines (&it, 1);
17041 try_window (window, it.current.pos, 0);
17043 else if (PT < IT_CHARPOS (it))
17045 clear_glyph_matrix (w->desired_matrix);
17046 move_it_by_lines (&it, -1);
17047 try_window (window, it.current.pos, 0);
17049 else if (scroll_conservatively > SCROLL_LIMIT
17050 && (it.method == GET_FROM_STRING
17051 || overlay_touches_p (IT_CHARPOS (it)))
17052 && IT_CHARPOS (it) < ZV)
17054 /* If the window starts with a before-string that spans more
17055 than one screen line, using that position to display the
17056 window might fail to bring point into the view, because
17057 start_display will always start by displaying the string,
17058 whereas the code above determines where to set w->start
17059 by the buffer position of the place where it takes screen
17060 coordinates. Try to recover by finding the next screen
17061 line that displays buffer text. */
17062 ptrdiff_t pos0 = IT_CHARPOS (it);
17064 clear_glyph_matrix (w->desired_matrix);
17065 do {
17066 move_it_by_lines (&it, 1);
17067 } while (IT_CHARPOS (it) == pos0);
17068 try_window (window, it.current.pos, 0);
17070 else
17072 /* Not much we can do about it. */
17076 /* Consider the following case: Window starts at BEGV, there is
17077 invisible, intangible text at BEGV, so that display starts at
17078 some point START > BEGV. It can happen that we are called with
17079 PT somewhere between BEGV and START. Try to handle that case,
17080 and similar ones. */
17081 if (w->cursor.vpos < 0)
17083 /* Prefer the desired matrix to the current matrix, if possible,
17084 in the fallback calculations below. This is because using
17085 the current matrix might completely goof, e.g. if its first
17086 row is after point. */
17087 struct glyph_matrix *matrix =
17088 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17089 /* First, try locating the proper glyph row for PT. */
17090 struct glyph_row *row =
17091 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17093 /* Sometimes point is at the beginning of invisible text that is
17094 before the 1st character displayed in the row. In that case,
17095 row_containing_pos fails to find the row, because no glyphs
17096 with appropriate buffer positions are present in the row.
17097 Therefore, we next try to find the row which shows the 1st
17098 position after the invisible text. */
17099 if (!row)
17101 Lisp_Object val =
17102 get_char_property_and_overlay (make_number (PT), Qinvisible,
17103 Qnil, NULL);
17105 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17107 ptrdiff_t alt_pos;
17108 Lisp_Object invis_end =
17109 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17110 Qnil, Qnil);
17112 if (NATNUMP (invis_end))
17113 alt_pos = XFASTINT (invis_end);
17114 else
17115 alt_pos = ZV;
17116 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17119 /* Finally, fall back on the first row of the window after the
17120 header line (if any). This is slightly better than not
17121 displaying the cursor at all. */
17122 if (!row)
17124 row = matrix->rows;
17125 if (row->mode_line_p)
17126 ++row;
17128 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17131 if (!cursor_row_fully_visible_p (w, false, false))
17133 /* If vscroll is enabled, disable it and try again. */
17134 if (w->vscroll)
17136 w->vscroll = 0;
17137 clear_glyph_matrix (w->desired_matrix);
17138 goto recenter;
17141 /* Users who set scroll-conservatively to a large number want
17142 point just above/below the scroll margin. If we ended up
17143 with point's row partially visible, move the window start to
17144 make that row fully visible and out of the margin. */
17145 if (scroll_conservatively > SCROLL_LIMIT)
17147 int window_total_lines
17148 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17149 int margin =
17150 scroll_margin > 0
17151 ? min (scroll_margin, window_total_lines / 4)
17152 : 0;
17153 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17155 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17156 clear_glyph_matrix (w->desired_matrix);
17157 if (1 == try_window (window, it.current.pos,
17158 TRY_WINDOW_CHECK_MARGINS))
17159 goto done;
17162 /* If centering point failed to make the whole line visible,
17163 put point at the top instead. That has to make the whole line
17164 visible, if it can be done. */
17165 if (centering_position == 0)
17166 goto done;
17168 clear_glyph_matrix (w->desired_matrix);
17169 centering_position = 0;
17170 goto recenter;
17173 done:
17175 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17176 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17177 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17179 /* Display the mode line, if we must. */
17180 if ((update_mode_line
17181 /* If window not full width, must redo its mode line
17182 if (a) the window to its side is being redone and
17183 (b) we do a frame-based redisplay. This is a consequence
17184 of how inverted lines are drawn in frame-based redisplay. */
17185 || (!just_this_one_p
17186 && !FRAME_WINDOW_P (f)
17187 && !WINDOW_FULL_WIDTH_P (w))
17188 /* Line number to display. */
17189 || w->base_line_pos > 0
17190 /* Column number is displayed and different from the one displayed. */
17191 || (w->column_number_displayed != -1
17192 && (w->column_number_displayed != current_column ())))
17193 /* This means that the window has a mode line. */
17194 && (WINDOW_WANTS_MODELINE_P (w)
17195 || WINDOW_WANTS_HEADER_LINE_P (w)))
17198 display_mode_lines (w);
17200 /* If mode line height has changed, arrange for a thorough
17201 immediate redisplay using the correct mode line height. */
17202 if (WINDOW_WANTS_MODELINE_P (w)
17203 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17205 f->fonts_changed = true;
17206 w->mode_line_height = -1;
17207 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17208 = DESIRED_MODE_LINE_HEIGHT (w);
17211 /* If header line height has changed, arrange for a thorough
17212 immediate redisplay using the correct header line height. */
17213 if (WINDOW_WANTS_HEADER_LINE_P (w)
17214 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17216 f->fonts_changed = true;
17217 w->header_line_height = -1;
17218 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17219 = DESIRED_HEADER_LINE_HEIGHT (w);
17222 if (f->fonts_changed)
17223 goto need_larger_matrices;
17226 if (!line_number_displayed && w->base_line_pos != -1)
17228 w->base_line_pos = 0;
17229 w->base_line_number = 0;
17232 finish_menu_bars:
17234 /* When we reach a frame's selected window, redo the frame's menu
17235 bar and the frame's title. */
17236 if (update_mode_line
17237 && EQ (FRAME_SELECTED_WINDOW (f), window))
17239 bool redisplay_menu_p;
17241 if (FRAME_WINDOW_P (f))
17243 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17244 || defined (HAVE_NS) || defined (USE_GTK)
17245 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17246 #else
17247 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17248 #endif
17250 else
17251 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17253 if (redisplay_menu_p)
17254 display_menu_bar (w);
17256 #ifdef HAVE_WINDOW_SYSTEM
17257 if (FRAME_WINDOW_P (f))
17259 #if defined (USE_GTK) || defined (HAVE_NS)
17260 if (FRAME_EXTERNAL_TOOL_BAR (f))
17261 redisplay_tool_bar (f);
17262 #else
17263 if (WINDOWP (f->tool_bar_window)
17264 && (FRAME_TOOL_BAR_LINES (f) > 0
17265 || !NILP (Vauto_resize_tool_bars))
17266 && redisplay_tool_bar (f))
17267 ignore_mouse_drag_p = true;
17268 #endif
17270 x_consider_frame_title (w->frame);
17271 #endif
17274 #ifdef HAVE_WINDOW_SYSTEM
17275 if (FRAME_WINDOW_P (f)
17276 && update_window_fringes (w, (just_this_one_p
17277 || (!used_current_matrix_p && !overlay_arrow_seen)
17278 || w->pseudo_window_p)))
17280 update_begin (f);
17281 block_input ();
17282 if (draw_window_fringes (w, true))
17284 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17285 x_draw_right_divider (w);
17286 else
17287 x_draw_vertical_border (w);
17289 unblock_input ();
17290 update_end (f);
17293 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17294 x_draw_bottom_divider (w);
17295 #endif /* HAVE_WINDOW_SYSTEM */
17297 /* We go to this label, with fonts_changed set, if it is
17298 necessary to try again using larger glyph matrices.
17299 We have to redeem the scroll bar even in this case,
17300 because the loop in redisplay_internal expects that. */
17301 need_larger_matrices:
17303 finish_scroll_bars:
17305 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17307 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17308 /* Set the thumb's position and size. */
17309 set_vertical_scroll_bar (w);
17311 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17312 /* Set the thumb's position and size. */
17313 set_horizontal_scroll_bar (w);
17315 /* Note that we actually used the scroll bar attached to this
17316 window, so it shouldn't be deleted at the end of redisplay. */
17317 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17318 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17321 /* Restore current_buffer and value of point in it. The window
17322 update may have changed the buffer, so first make sure `opoint'
17323 is still valid (Bug#6177). */
17324 if (CHARPOS (opoint) < BEGV)
17325 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17326 else if (CHARPOS (opoint) > ZV)
17327 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17328 else
17329 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17331 set_buffer_internal_1 (old);
17332 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17333 shorter. This can be caused by log truncation in *Messages*. */
17334 if (CHARPOS (lpoint) <= ZV)
17335 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17337 unbind_to (count, Qnil);
17341 /* Build the complete desired matrix of WINDOW with a window start
17342 buffer position POS.
17344 Value is 1 if successful. It is zero if fonts were loaded during
17345 redisplay which makes re-adjusting glyph matrices necessary, and -1
17346 if point would appear in the scroll margins.
17347 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17348 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17349 set in FLAGS.) */
17352 try_window (Lisp_Object window, struct text_pos pos, int flags)
17354 struct window *w = XWINDOW (window);
17355 struct it it;
17356 struct glyph_row *last_text_row = NULL;
17357 struct frame *f = XFRAME (w->frame);
17358 int frame_line_height = default_line_pixel_height (w);
17360 /* Make POS the new window start. */
17361 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17363 /* Mark cursor position as unknown. No overlay arrow seen. */
17364 w->cursor.vpos = -1;
17365 overlay_arrow_seen = false;
17367 /* Initialize iterator and info to start at POS. */
17368 start_display (&it, w, pos);
17369 it.glyph_row->reversed_p = false;
17371 /* Display all lines of W. */
17372 while (it.current_y < it.last_visible_y)
17374 if (display_line (&it))
17375 last_text_row = it.glyph_row - 1;
17376 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17377 return 0;
17380 /* Don't let the cursor end in the scroll margins. */
17381 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17382 && !MINI_WINDOW_P (w))
17384 int this_scroll_margin;
17385 int window_total_lines
17386 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17388 if (scroll_margin > 0)
17390 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17391 this_scroll_margin *= frame_line_height;
17393 else
17394 this_scroll_margin = 0;
17396 if ((w->cursor.y >= 0 /* not vscrolled */
17397 && w->cursor.y < this_scroll_margin
17398 && CHARPOS (pos) > BEGV
17399 && IT_CHARPOS (it) < ZV)
17400 /* rms: considering make_cursor_line_fully_visible_p here
17401 seems to give wrong results. We don't want to recenter
17402 when the last line is partly visible, we want to allow
17403 that case to be handled in the usual way. */
17404 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17406 w->cursor.vpos = -1;
17407 clear_glyph_matrix (w->desired_matrix);
17408 return -1;
17412 /* If bottom moved off end of frame, change mode line percentage. */
17413 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17414 w->update_mode_line = true;
17416 /* Set window_end_pos to the offset of the last character displayed
17417 on the window from the end of current_buffer. Set
17418 window_end_vpos to its row number. */
17419 if (last_text_row)
17421 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17422 adjust_window_ends (w, last_text_row, false);
17423 eassert
17424 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17425 w->window_end_vpos)));
17427 else
17429 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17430 w->window_end_pos = Z - ZV;
17431 w->window_end_vpos = 0;
17434 /* But that is not valid info until redisplay finishes. */
17435 w->window_end_valid = false;
17436 return 1;
17441 /************************************************************************
17442 Window redisplay reusing current matrix when buffer has not changed
17443 ************************************************************************/
17445 /* Try redisplay of window W showing an unchanged buffer with a
17446 different window start than the last time it was displayed by
17447 reusing its current matrix. Value is true if successful.
17448 W->start is the new window start. */
17450 static bool
17451 try_window_reusing_current_matrix (struct window *w)
17453 struct frame *f = XFRAME (w->frame);
17454 struct glyph_row *bottom_row;
17455 struct it it;
17456 struct run run;
17457 struct text_pos start, new_start;
17458 int nrows_scrolled, i;
17459 struct glyph_row *last_text_row;
17460 struct glyph_row *last_reused_text_row;
17461 struct glyph_row *start_row;
17462 int start_vpos, min_y, max_y;
17464 #ifdef GLYPH_DEBUG
17465 if (inhibit_try_window_reusing)
17466 return false;
17467 #endif
17469 if (/* This function doesn't handle terminal frames. */
17470 !FRAME_WINDOW_P (f)
17471 /* Don't try to reuse the display if windows have been split
17472 or such. */
17473 || windows_or_buffers_changed
17474 || f->cursor_type_changed)
17475 return false;
17477 /* Can't do this if showing trailing whitespace. */
17478 if (!NILP (Vshow_trailing_whitespace))
17479 return false;
17481 /* If top-line visibility has changed, give up. */
17482 if (WINDOW_WANTS_HEADER_LINE_P (w)
17483 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17484 return false;
17486 /* Give up if old or new display is scrolled vertically. We could
17487 make this function handle this, but right now it doesn't. */
17488 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17489 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17490 return false;
17492 /* The variable new_start now holds the new window start. The old
17493 start `start' can be determined from the current matrix. */
17494 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17495 start = start_row->minpos;
17496 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17498 /* Clear the desired matrix for the display below. */
17499 clear_glyph_matrix (w->desired_matrix);
17501 if (CHARPOS (new_start) <= CHARPOS (start))
17503 /* Don't use this method if the display starts with an ellipsis
17504 displayed for invisible text. It's not easy to handle that case
17505 below, and it's certainly not worth the effort since this is
17506 not a frequent case. */
17507 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17508 return false;
17510 IF_DEBUG (debug_method_add (w, "twu1"));
17512 /* Display up to a row that can be reused. The variable
17513 last_text_row is set to the last row displayed that displays
17514 text. Note that it.vpos == 0 if or if not there is a
17515 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17516 start_display (&it, w, new_start);
17517 w->cursor.vpos = -1;
17518 last_text_row = last_reused_text_row = NULL;
17520 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17522 /* If we have reached into the characters in the START row,
17523 that means the line boundaries have changed. So we
17524 can't start copying with the row START. Maybe it will
17525 work to start copying with the following row. */
17526 while (IT_CHARPOS (it) > CHARPOS (start))
17528 /* Advance to the next row as the "start". */
17529 start_row++;
17530 start = start_row->minpos;
17531 /* If there are no more rows to try, or just one, give up. */
17532 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17533 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17534 || CHARPOS (start) == ZV)
17536 clear_glyph_matrix (w->desired_matrix);
17537 return false;
17540 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17542 /* If we have reached alignment, we can copy the rest of the
17543 rows. */
17544 if (IT_CHARPOS (it) == CHARPOS (start)
17545 /* Don't accept "alignment" inside a display vector,
17546 since start_row could have started in the middle of
17547 that same display vector (thus their character
17548 positions match), and we have no way of telling if
17549 that is the case. */
17550 && it.current.dpvec_index < 0)
17551 break;
17553 it.glyph_row->reversed_p = false;
17554 if (display_line (&it))
17555 last_text_row = it.glyph_row - 1;
17559 /* A value of current_y < last_visible_y means that we stopped
17560 at the previous window start, which in turn means that we
17561 have at least one reusable row. */
17562 if (it.current_y < it.last_visible_y)
17564 struct glyph_row *row;
17566 /* IT.vpos always starts from 0; it counts text lines. */
17567 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17569 /* Find PT if not already found in the lines displayed. */
17570 if (w->cursor.vpos < 0)
17572 int dy = it.current_y - start_row->y;
17574 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17575 row = row_containing_pos (w, PT, row, NULL, dy);
17576 if (row)
17577 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17578 dy, nrows_scrolled);
17579 else
17581 clear_glyph_matrix (w->desired_matrix);
17582 return false;
17586 /* Scroll the display. Do it before the current matrix is
17587 changed. The problem here is that update has not yet
17588 run, i.e. part of the current matrix is not up to date.
17589 scroll_run_hook will clear the cursor, and use the
17590 current matrix to get the height of the row the cursor is
17591 in. */
17592 run.current_y = start_row->y;
17593 run.desired_y = it.current_y;
17594 run.height = it.last_visible_y - it.current_y;
17596 if (run.height > 0 && run.current_y != run.desired_y)
17598 update_begin (f);
17599 FRAME_RIF (f)->update_window_begin_hook (w);
17600 FRAME_RIF (f)->clear_window_mouse_face (w);
17601 FRAME_RIF (f)->scroll_run_hook (w, &run);
17602 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17603 update_end (f);
17606 /* Shift current matrix down by nrows_scrolled lines. */
17607 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17608 rotate_matrix (w->current_matrix,
17609 start_vpos,
17610 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17611 nrows_scrolled);
17613 /* Disable lines that must be updated. */
17614 for (i = 0; i < nrows_scrolled; ++i)
17615 (start_row + i)->enabled_p = false;
17617 /* Re-compute Y positions. */
17618 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17619 max_y = it.last_visible_y;
17620 for (row = start_row + nrows_scrolled;
17621 row < bottom_row;
17622 ++row)
17624 row->y = it.current_y;
17625 row->visible_height = row->height;
17627 if (row->y < min_y)
17628 row->visible_height -= min_y - row->y;
17629 if (row->y + row->height > max_y)
17630 row->visible_height -= row->y + row->height - max_y;
17631 if (row->fringe_bitmap_periodic_p)
17632 row->redraw_fringe_bitmaps_p = true;
17634 it.current_y += row->height;
17636 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17637 last_reused_text_row = row;
17638 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17639 break;
17642 /* Disable lines in the current matrix which are now
17643 below the window. */
17644 for (++row; row < bottom_row; ++row)
17645 row->enabled_p = row->mode_line_p = false;
17648 /* Update window_end_pos etc.; last_reused_text_row is the last
17649 reused row from the current matrix containing text, if any.
17650 The value of last_text_row is the last displayed line
17651 containing text. */
17652 if (last_reused_text_row)
17653 adjust_window_ends (w, last_reused_text_row, true);
17654 else if (last_text_row)
17655 adjust_window_ends (w, last_text_row, false);
17656 else
17658 /* This window must be completely empty. */
17659 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17660 w->window_end_pos = Z - ZV;
17661 w->window_end_vpos = 0;
17663 w->window_end_valid = false;
17665 /* Update hint: don't try scrolling again in update_window. */
17666 w->desired_matrix->no_scrolling_p = true;
17668 #ifdef GLYPH_DEBUG
17669 debug_method_add (w, "try_window_reusing_current_matrix 1");
17670 #endif
17671 return true;
17673 else if (CHARPOS (new_start) > CHARPOS (start))
17675 struct glyph_row *pt_row, *row;
17676 struct glyph_row *first_reusable_row;
17677 struct glyph_row *first_row_to_display;
17678 int dy;
17679 int yb = window_text_bottom_y (w);
17681 /* Find the row starting at new_start, if there is one. Don't
17682 reuse a partially visible line at the end. */
17683 first_reusable_row = start_row;
17684 while (first_reusable_row->enabled_p
17685 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17686 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17687 < CHARPOS (new_start)))
17688 ++first_reusable_row;
17690 /* Give up if there is no row to reuse. */
17691 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17692 || !first_reusable_row->enabled_p
17693 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17694 != CHARPOS (new_start)))
17695 return false;
17697 /* We can reuse fully visible rows beginning with
17698 first_reusable_row to the end of the window. Set
17699 first_row_to_display to the first row that cannot be reused.
17700 Set pt_row to the row containing point, if there is any. */
17701 pt_row = NULL;
17702 for (first_row_to_display = first_reusable_row;
17703 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17704 ++first_row_to_display)
17706 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17707 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17708 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17709 && first_row_to_display->ends_at_zv_p
17710 && pt_row == NULL)))
17711 pt_row = first_row_to_display;
17714 /* Start displaying at the start of first_row_to_display. */
17715 eassert (first_row_to_display->y < yb);
17716 init_to_row_start (&it, w, first_row_to_display);
17718 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17719 - start_vpos);
17720 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17721 - nrows_scrolled);
17722 it.current_y = (first_row_to_display->y - first_reusable_row->y
17723 + WINDOW_HEADER_LINE_HEIGHT (w));
17725 /* Display lines beginning with first_row_to_display in the
17726 desired matrix. Set last_text_row to the last row displayed
17727 that displays text. */
17728 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17729 if (pt_row == NULL)
17730 w->cursor.vpos = -1;
17731 last_text_row = NULL;
17732 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17733 if (display_line (&it))
17734 last_text_row = it.glyph_row - 1;
17736 /* If point is in a reused row, adjust y and vpos of the cursor
17737 position. */
17738 if (pt_row)
17740 w->cursor.vpos -= nrows_scrolled;
17741 w->cursor.y -= first_reusable_row->y - start_row->y;
17744 /* Give up if point isn't in a row displayed or reused. (This
17745 also handles the case where w->cursor.vpos < nrows_scrolled
17746 after the calls to display_line, which can happen with scroll
17747 margins. See bug#1295.) */
17748 if (w->cursor.vpos < 0)
17750 clear_glyph_matrix (w->desired_matrix);
17751 return false;
17754 /* Scroll the display. */
17755 run.current_y = first_reusable_row->y;
17756 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17757 run.height = it.last_visible_y - run.current_y;
17758 dy = run.current_y - run.desired_y;
17760 if (run.height)
17762 update_begin (f);
17763 FRAME_RIF (f)->update_window_begin_hook (w);
17764 FRAME_RIF (f)->clear_window_mouse_face (w);
17765 FRAME_RIF (f)->scroll_run_hook (w, &run);
17766 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17767 update_end (f);
17770 /* Adjust Y positions of reused rows. */
17771 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17772 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17773 max_y = it.last_visible_y;
17774 for (row = first_reusable_row; row < first_row_to_display; ++row)
17776 row->y -= dy;
17777 row->visible_height = row->height;
17778 if (row->y < min_y)
17779 row->visible_height -= min_y - row->y;
17780 if (row->y + row->height > max_y)
17781 row->visible_height -= row->y + row->height - max_y;
17782 if (row->fringe_bitmap_periodic_p)
17783 row->redraw_fringe_bitmaps_p = true;
17786 /* Scroll the current matrix. */
17787 eassert (nrows_scrolled > 0);
17788 rotate_matrix (w->current_matrix,
17789 start_vpos,
17790 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17791 -nrows_scrolled);
17793 /* Disable rows not reused. */
17794 for (row -= nrows_scrolled; row < bottom_row; ++row)
17795 row->enabled_p = false;
17797 /* Point may have moved to a different line, so we cannot assume that
17798 the previous cursor position is valid; locate the correct row. */
17799 if (pt_row)
17801 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17802 row < bottom_row
17803 && PT >= MATRIX_ROW_END_CHARPOS (row)
17804 && !row->ends_at_zv_p;
17805 row++)
17807 w->cursor.vpos++;
17808 w->cursor.y = row->y;
17810 if (row < bottom_row)
17812 /* Can't simply scan the row for point with
17813 bidi-reordered glyph rows. Let set_cursor_from_row
17814 figure out where to put the cursor, and if it fails,
17815 give up. */
17816 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17818 if (!set_cursor_from_row (w, row, w->current_matrix,
17819 0, 0, 0, 0))
17821 clear_glyph_matrix (w->desired_matrix);
17822 return false;
17825 else
17827 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17828 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17830 for (; glyph < end
17831 && (!BUFFERP (glyph->object)
17832 || glyph->charpos < PT);
17833 glyph++)
17835 w->cursor.hpos++;
17836 w->cursor.x += glyph->pixel_width;
17842 /* Adjust window end. A null value of last_text_row means that
17843 the window end is in reused rows which in turn means that
17844 only its vpos can have changed. */
17845 if (last_text_row)
17846 adjust_window_ends (w, last_text_row, false);
17847 else
17848 w->window_end_vpos -= nrows_scrolled;
17850 w->window_end_valid = false;
17851 w->desired_matrix->no_scrolling_p = true;
17853 #ifdef GLYPH_DEBUG
17854 debug_method_add (w, "try_window_reusing_current_matrix 2");
17855 #endif
17856 return true;
17859 return false;
17864 /************************************************************************
17865 Window redisplay reusing current matrix when buffer has changed
17866 ************************************************************************/
17868 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17869 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17870 ptrdiff_t *, ptrdiff_t *);
17871 static struct glyph_row *
17872 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17873 struct glyph_row *);
17876 /* Return the last row in MATRIX displaying text. If row START is
17877 non-null, start searching with that row. IT gives the dimensions
17878 of the display. Value is null if matrix is empty; otherwise it is
17879 a pointer to the row found. */
17881 static struct glyph_row *
17882 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17883 struct glyph_row *start)
17885 struct glyph_row *row, *row_found;
17887 /* Set row_found to the last row in IT->w's current matrix
17888 displaying text. The loop looks funny but think of partially
17889 visible lines. */
17890 row_found = NULL;
17891 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17892 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17894 eassert (row->enabled_p);
17895 row_found = row;
17896 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17897 break;
17898 ++row;
17901 return row_found;
17905 /* Return the last row in the current matrix of W that is not affected
17906 by changes at the start of current_buffer that occurred since W's
17907 current matrix was built. Value is null if no such row exists.
17909 BEG_UNCHANGED us the number of characters unchanged at the start of
17910 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17911 first changed character in current_buffer. Characters at positions <
17912 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17913 when the current matrix was built. */
17915 static struct glyph_row *
17916 find_last_unchanged_at_beg_row (struct window *w)
17918 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17919 struct glyph_row *row;
17920 struct glyph_row *row_found = NULL;
17921 int yb = window_text_bottom_y (w);
17923 /* Find the last row displaying unchanged text. */
17924 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17925 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17926 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17927 ++row)
17929 if (/* If row ends before first_changed_pos, it is unchanged,
17930 except in some case. */
17931 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17932 /* When row ends in ZV and we write at ZV it is not
17933 unchanged. */
17934 && !row->ends_at_zv_p
17935 /* When first_changed_pos is the end of a continued line,
17936 row is not unchanged because it may be no longer
17937 continued. */
17938 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17939 && (row->continued_p
17940 || row->exact_window_width_line_p))
17941 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17942 needs to be recomputed, so don't consider this row as
17943 unchanged. This happens when the last line was
17944 bidi-reordered and was killed immediately before this
17945 redisplay cycle. In that case, ROW->end stores the
17946 buffer position of the first visual-order character of
17947 the killed text, which is now beyond ZV. */
17948 && CHARPOS (row->end.pos) <= ZV)
17949 row_found = row;
17951 /* Stop if last visible row. */
17952 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17953 break;
17956 return row_found;
17960 /* Find the first glyph row in the current matrix of W that is not
17961 affected by changes at the end of current_buffer since the
17962 time W's current matrix was built.
17964 Return in *DELTA the number of chars by which buffer positions in
17965 unchanged text at the end of current_buffer must be adjusted.
17967 Return in *DELTA_BYTES the corresponding number of bytes.
17969 Value is null if no such row exists, i.e. all rows are affected by
17970 changes. */
17972 static struct glyph_row *
17973 find_first_unchanged_at_end_row (struct window *w,
17974 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17976 struct glyph_row *row;
17977 struct glyph_row *row_found = NULL;
17979 *delta = *delta_bytes = 0;
17981 /* Display must not have been paused, otherwise the current matrix
17982 is not up to date. */
17983 eassert (w->window_end_valid);
17985 /* A value of window_end_pos >= END_UNCHANGED means that the window
17986 end is in the range of changed text. If so, there is no
17987 unchanged row at the end of W's current matrix. */
17988 if (w->window_end_pos >= END_UNCHANGED)
17989 return NULL;
17991 /* Set row to the last row in W's current matrix displaying text. */
17992 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17994 /* If matrix is entirely empty, no unchanged row exists. */
17995 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17997 /* The value of row is the last glyph row in the matrix having a
17998 meaningful buffer position in it. The end position of row
17999 corresponds to window_end_pos. This allows us to translate
18000 buffer positions in the current matrix to current buffer
18001 positions for characters not in changed text. */
18002 ptrdiff_t Z_old =
18003 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18004 ptrdiff_t Z_BYTE_old =
18005 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18006 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18007 struct glyph_row *first_text_row
18008 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18010 *delta = Z - Z_old;
18011 *delta_bytes = Z_BYTE - Z_BYTE_old;
18013 /* Set last_unchanged_pos to the buffer position of the last
18014 character in the buffer that has not been changed. Z is the
18015 index + 1 of the last character in current_buffer, i.e. by
18016 subtracting END_UNCHANGED we get the index of the last
18017 unchanged character, and we have to add BEG to get its buffer
18018 position. */
18019 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18020 last_unchanged_pos_old = last_unchanged_pos - *delta;
18022 /* Search backward from ROW for a row displaying a line that
18023 starts at a minimum position >= last_unchanged_pos_old. */
18024 for (; row > first_text_row; --row)
18026 /* This used to abort, but it can happen.
18027 It is ok to just stop the search instead here. KFS. */
18028 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18029 break;
18031 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18032 row_found = row;
18036 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18038 return row_found;
18042 /* Make sure that glyph rows in the current matrix of window W
18043 reference the same glyph memory as corresponding rows in the
18044 frame's frame matrix. This function is called after scrolling W's
18045 current matrix on a terminal frame in try_window_id and
18046 try_window_reusing_current_matrix. */
18048 static void
18049 sync_frame_with_window_matrix_rows (struct window *w)
18051 struct frame *f = XFRAME (w->frame);
18052 struct glyph_row *window_row, *window_row_end, *frame_row;
18054 /* Preconditions: W must be a leaf window and full-width. Its frame
18055 must have a frame matrix. */
18056 eassert (BUFFERP (w->contents));
18057 eassert (WINDOW_FULL_WIDTH_P (w));
18058 eassert (!FRAME_WINDOW_P (f));
18060 /* If W is a full-width window, glyph pointers in W's current matrix
18061 have, by definition, to be the same as glyph pointers in the
18062 corresponding frame matrix. Note that frame matrices have no
18063 marginal areas (see build_frame_matrix). */
18064 window_row = w->current_matrix->rows;
18065 window_row_end = window_row + w->current_matrix->nrows;
18066 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18067 while (window_row < window_row_end)
18069 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18070 struct glyph *end = window_row->glyphs[LAST_AREA];
18072 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18073 frame_row->glyphs[TEXT_AREA] = start;
18074 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18075 frame_row->glyphs[LAST_AREA] = end;
18077 /* Disable frame rows whose corresponding window rows have
18078 been disabled in try_window_id. */
18079 if (!window_row->enabled_p)
18080 frame_row->enabled_p = false;
18082 ++window_row, ++frame_row;
18087 /* Find the glyph row in window W containing CHARPOS. Consider all
18088 rows between START and END (not inclusive). END null means search
18089 all rows to the end of the display area of W. Value is the row
18090 containing CHARPOS or null. */
18092 struct glyph_row *
18093 row_containing_pos (struct window *w, ptrdiff_t charpos,
18094 struct glyph_row *start, struct glyph_row *end, int dy)
18096 struct glyph_row *row = start;
18097 struct glyph_row *best_row = NULL;
18098 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18099 int last_y;
18101 /* If we happen to start on a header-line, skip that. */
18102 if (row->mode_line_p)
18103 ++row;
18105 if ((end && row >= end) || !row->enabled_p)
18106 return NULL;
18108 last_y = window_text_bottom_y (w) - dy;
18110 while (true)
18112 /* Give up if we have gone too far. */
18113 if ((end && row >= end) || !row->enabled_p)
18114 return NULL;
18115 /* This formerly returned if they were equal.
18116 I think that both quantities are of a "last plus one" type;
18117 if so, when they are equal, the row is within the screen. -- rms. */
18118 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18119 return NULL;
18121 /* If it is in this row, return this row. */
18122 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18123 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18124 /* The end position of a row equals the start
18125 position of the next row. If CHARPOS is there, we
18126 would rather consider it displayed in the next
18127 line, except when this line ends in ZV. */
18128 && !row_for_charpos_p (row, charpos)))
18129 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18131 struct glyph *g;
18133 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18134 || (!best_row && !row->continued_p))
18135 return row;
18136 /* In bidi-reordered rows, there could be several rows whose
18137 edges surround CHARPOS, all of these rows belonging to
18138 the same continued line. We need to find the row which
18139 fits CHARPOS the best. */
18140 for (g = row->glyphs[TEXT_AREA];
18141 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18142 g++)
18144 if (!STRINGP (g->object))
18146 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18148 mindif = eabs (g->charpos - charpos);
18149 best_row = row;
18150 /* Exact match always wins. */
18151 if (mindif == 0)
18152 return best_row;
18157 else if (best_row && !row->continued_p)
18158 return best_row;
18159 ++row;
18164 /* Try to redisplay window W by reusing its existing display. W's
18165 current matrix must be up to date when this function is called,
18166 i.e., window_end_valid must be true.
18168 Value is
18170 >= 1 if successful, i.e. display has been updated
18171 specifically:
18172 1 means the changes were in front of a newline that precedes
18173 the window start, and the whole current matrix was reused
18174 2 means the changes were after the last position displayed
18175 in the window, and the whole current matrix was reused
18176 3 means portions of the current matrix were reused, while
18177 some of the screen lines were redrawn
18178 -1 if redisplay with same window start is known not to succeed
18179 0 if otherwise unsuccessful
18181 The following steps are performed:
18183 1. Find the last row in the current matrix of W that is not
18184 affected by changes at the start of current_buffer. If no such row
18185 is found, give up.
18187 2. Find the first row in W's current matrix that is not affected by
18188 changes at the end of current_buffer. Maybe there is no such row.
18190 3. Display lines beginning with the row + 1 found in step 1 to the
18191 row found in step 2 or, if step 2 didn't find a row, to the end of
18192 the window.
18194 4. If cursor is not known to appear on the window, give up.
18196 5. If display stopped at the row found in step 2, scroll the
18197 display and current matrix as needed.
18199 6. Maybe display some lines at the end of W, if we must. This can
18200 happen under various circumstances, like a partially visible line
18201 becoming fully visible, or because newly displayed lines are displayed
18202 in smaller font sizes.
18204 7. Update W's window end information. */
18206 static int
18207 try_window_id (struct window *w)
18209 struct frame *f = XFRAME (w->frame);
18210 struct glyph_matrix *current_matrix = w->current_matrix;
18211 struct glyph_matrix *desired_matrix = w->desired_matrix;
18212 struct glyph_row *last_unchanged_at_beg_row;
18213 struct glyph_row *first_unchanged_at_end_row;
18214 struct glyph_row *row;
18215 struct glyph_row *bottom_row;
18216 int bottom_vpos;
18217 struct it it;
18218 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18219 int dvpos, dy;
18220 struct text_pos start_pos;
18221 struct run run;
18222 int first_unchanged_at_end_vpos = 0;
18223 struct glyph_row *last_text_row, *last_text_row_at_end;
18224 struct text_pos start;
18225 ptrdiff_t first_changed_charpos, last_changed_charpos;
18227 #ifdef GLYPH_DEBUG
18228 if (inhibit_try_window_id)
18229 return 0;
18230 #endif
18232 /* This is handy for debugging. */
18233 #if false
18234 #define GIVE_UP(X) \
18235 do { \
18236 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18237 return 0; \
18238 } while (false)
18239 #else
18240 #define GIVE_UP(X) return 0
18241 #endif
18243 SET_TEXT_POS_FROM_MARKER (start, w->start);
18245 /* Don't use this for mini-windows because these can show
18246 messages and mini-buffers, and we don't handle that here. */
18247 if (MINI_WINDOW_P (w))
18248 GIVE_UP (1);
18250 /* This flag is used to prevent redisplay optimizations. */
18251 if (windows_or_buffers_changed || f->cursor_type_changed)
18252 GIVE_UP (2);
18254 /* This function's optimizations cannot be used if overlays have
18255 changed in the buffer displayed by the window, so give up if they
18256 have. */
18257 if (w->last_overlay_modified != OVERLAY_MODIFF)
18258 GIVE_UP (200);
18260 /* Verify that narrowing has not changed.
18261 Also verify that we were not told to prevent redisplay optimizations.
18262 It would be nice to further
18263 reduce the number of cases where this prevents try_window_id. */
18264 if (current_buffer->clip_changed
18265 || current_buffer->prevent_redisplay_optimizations_p)
18266 GIVE_UP (3);
18268 /* Window must either use window-based redisplay or be full width. */
18269 if (!FRAME_WINDOW_P (f)
18270 && (!FRAME_LINE_INS_DEL_OK (f)
18271 || !WINDOW_FULL_WIDTH_P (w)))
18272 GIVE_UP (4);
18274 /* Give up if point is known NOT to appear in W. */
18275 if (PT < CHARPOS (start))
18276 GIVE_UP (5);
18278 /* Another way to prevent redisplay optimizations. */
18279 if (w->last_modified == 0)
18280 GIVE_UP (6);
18282 /* Verify that window is not hscrolled. */
18283 if (w->hscroll != 0)
18284 GIVE_UP (7);
18286 /* Verify that display wasn't paused. */
18287 if (!w->window_end_valid)
18288 GIVE_UP (8);
18290 /* Likewise if highlighting trailing whitespace. */
18291 if (!NILP (Vshow_trailing_whitespace))
18292 GIVE_UP (11);
18294 /* Can't use this if overlay arrow position and/or string have
18295 changed. */
18296 if (overlay_arrows_changed_p ())
18297 GIVE_UP (12);
18299 /* When word-wrap is on, adding a space to the first word of a
18300 wrapped line can change the wrap position, altering the line
18301 above it. It might be worthwhile to handle this more
18302 intelligently, but for now just redisplay from scratch. */
18303 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18304 GIVE_UP (21);
18306 /* Under bidi reordering, adding or deleting a character in the
18307 beginning of a paragraph, before the first strong directional
18308 character, can change the base direction of the paragraph (unless
18309 the buffer specifies a fixed paragraph direction), which will
18310 require redisplaying the whole paragraph. It might be worthwhile
18311 to find the paragraph limits and widen the range of redisplayed
18312 lines to that, but for now just give up this optimization and
18313 redisplay from scratch. */
18314 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18315 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18316 GIVE_UP (22);
18318 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18319 to that variable require thorough redisplay. */
18320 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18321 GIVE_UP (23);
18323 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18324 only if buffer has really changed. The reason is that the gap is
18325 initially at Z for freshly visited files. The code below would
18326 set end_unchanged to 0 in that case. */
18327 if (MODIFF > SAVE_MODIFF
18328 /* This seems to happen sometimes after saving a buffer. */
18329 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18331 if (GPT - BEG < BEG_UNCHANGED)
18332 BEG_UNCHANGED = GPT - BEG;
18333 if (Z - GPT < END_UNCHANGED)
18334 END_UNCHANGED = Z - GPT;
18337 /* The position of the first and last character that has been changed. */
18338 first_changed_charpos = BEG + BEG_UNCHANGED;
18339 last_changed_charpos = Z - END_UNCHANGED;
18341 /* If window starts after a line end, and the last change is in
18342 front of that newline, then changes don't affect the display.
18343 This case happens with stealth-fontification. Note that although
18344 the display is unchanged, glyph positions in the matrix have to
18345 be adjusted, of course. */
18346 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18347 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18348 && ((last_changed_charpos < CHARPOS (start)
18349 && CHARPOS (start) == BEGV)
18350 || (last_changed_charpos < CHARPOS (start) - 1
18351 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18353 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18354 struct glyph_row *r0;
18356 /* Compute how many chars/bytes have been added to or removed
18357 from the buffer. */
18358 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18359 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18360 Z_delta = Z - Z_old;
18361 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18363 /* Give up if PT is not in the window. Note that it already has
18364 been checked at the start of try_window_id that PT is not in
18365 front of the window start. */
18366 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18367 GIVE_UP (13);
18369 /* If window start is unchanged, we can reuse the whole matrix
18370 as is, after adjusting glyph positions. No need to compute
18371 the window end again, since its offset from Z hasn't changed. */
18372 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18373 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18374 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18375 /* PT must not be in a partially visible line. */
18376 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18377 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18379 /* Adjust positions in the glyph matrix. */
18380 if (Z_delta || Z_delta_bytes)
18382 struct glyph_row *r1
18383 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18384 increment_matrix_positions (w->current_matrix,
18385 MATRIX_ROW_VPOS (r0, current_matrix),
18386 MATRIX_ROW_VPOS (r1, current_matrix),
18387 Z_delta, Z_delta_bytes);
18390 /* Set the cursor. */
18391 row = row_containing_pos (w, PT, r0, NULL, 0);
18392 if (row)
18393 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18394 return 1;
18398 /* Handle the case that changes are all below what is displayed in
18399 the window, and that PT is in the window. This shortcut cannot
18400 be taken if ZV is visible in the window, and text has been added
18401 there that is visible in the window. */
18402 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18403 /* ZV is not visible in the window, or there are no
18404 changes at ZV, actually. */
18405 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18406 || first_changed_charpos == last_changed_charpos))
18408 struct glyph_row *r0;
18410 /* Give up if PT is not in the window. Note that it already has
18411 been checked at the start of try_window_id that PT is not in
18412 front of the window start. */
18413 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18414 GIVE_UP (14);
18416 /* If window start is unchanged, we can reuse the whole matrix
18417 as is, without changing glyph positions since no text has
18418 been added/removed in front of the window end. */
18419 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18420 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18421 /* PT must not be in a partially visible line. */
18422 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18423 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18425 /* We have to compute the window end anew since text
18426 could have been added/removed after it. */
18427 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18428 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18430 /* Set the cursor. */
18431 row = row_containing_pos (w, PT, r0, NULL, 0);
18432 if (row)
18433 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18434 return 2;
18438 /* Give up if window start is in the changed area.
18440 The condition used to read
18442 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18444 but why that was tested escapes me at the moment. */
18445 if (CHARPOS (start) >= first_changed_charpos
18446 && CHARPOS (start) <= last_changed_charpos)
18447 GIVE_UP (15);
18449 /* Check that window start agrees with the start of the first glyph
18450 row in its current matrix. Check this after we know the window
18451 start is not in changed text, otherwise positions would not be
18452 comparable. */
18453 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18454 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18455 GIVE_UP (16);
18457 /* Give up if the window ends in strings. Overlay strings
18458 at the end are difficult to handle, so don't try. */
18459 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18460 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18461 GIVE_UP (20);
18463 /* Compute the position at which we have to start displaying new
18464 lines. Some of the lines at the top of the window might be
18465 reusable because they are not displaying changed text. Find the
18466 last row in W's current matrix not affected by changes at the
18467 start of current_buffer. Value is null if changes start in the
18468 first line of window. */
18469 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18470 if (last_unchanged_at_beg_row)
18472 /* Avoid starting to display in the middle of a character, a TAB
18473 for instance. This is easier than to set up the iterator
18474 exactly, and it's not a frequent case, so the additional
18475 effort wouldn't really pay off. */
18476 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18477 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18478 && last_unchanged_at_beg_row > w->current_matrix->rows)
18479 --last_unchanged_at_beg_row;
18481 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18482 GIVE_UP (17);
18484 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18485 GIVE_UP (18);
18486 start_pos = it.current.pos;
18488 /* Start displaying new lines in the desired matrix at the same
18489 vpos we would use in the current matrix, i.e. below
18490 last_unchanged_at_beg_row. */
18491 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18492 current_matrix);
18493 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18494 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18496 eassert (it.hpos == 0 && it.current_x == 0);
18498 else
18500 /* There are no reusable lines at the start of the window.
18501 Start displaying in the first text line. */
18502 start_display (&it, w, start);
18503 it.vpos = it.first_vpos;
18504 start_pos = it.current.pos;
18507 /* Find the first row that is not affected by changes at the end of
18508 the buffer. Value will be null if there is no unchanged row, in
18509 which case we must redisplay to the end of the window. delta
18510 will be set to the value by which buffer positions beginning with
18511 first_unchanged_at_end_row have to be adjusted due to text
18512 changes. */
18513 first_unchanged_at_end_row
18514 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18515 IF_DEBUG (debug_delta = delta);
18516 IF_DEBUG (debug_delta_bytes = delta_bytes);
18518 /* Set stop_pos to the buffer position up to which we will have to
18519 display new lines. If first_unchanged_at_end_row != NULL, this
18520 is the buffer position of the start of the line displayed in that
18521 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18522 that we don't stop at a buffer position. */
18523 stop_pos = 0;
18524 if (first_unchanged_at_end_row)
18526 eassert (last_unchanged_at_beg_row == NULL
18527 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18529 /* If this is a continuation line, move forward to the next one
18530 that isn't. Changes in lines above affect this line.
18531 Caution: this may move first_unchanged_at_end_row to a row
18532 not displaying text. */
18533 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18534 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18535 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18536 < it.last_visible_y))
18537 ++first_unchanged_at_end_row;
18539 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18540 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18541 >= it.last_visible_y))
18542 first_unchanged_at_end_row = NULL;
18543 else
18545 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18546 + delta);
18547 first_unchanged_at_end_vpos
18548 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18549 eassert (stop_pos >= Z - END_UNCHANGED);
18552 else if (last_unchanged_at_beg_row == NULL)
18553 GIVE_UP (19);
18556 #ifdef GLYPH_DEBUG
18558 /* Either there is no unchanged row at the end, or the one we have
18559 now displays text. This is a necessary condition for the window
18560 end pos calculation at the end of this function. */
18561 eassert (first_unchanged_at_end_row == NULL
18562 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18564 debug_last_unchanged_at_beg_vpos
18565 = (last_unchanged_at_beg_row
18566 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18567 : -1);
18568 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18570 #endif /* GLYPH_DEBUG */
18573 /* Display new lines. Set last_text_row to the last new line
18574 displayed which has text on it, i.e. might end up as being the
18575 line where the window_end_vpos is. */
18576 w->cursor.vpos = -1;
18577 last_text_row = NULL;
18578 overlay_arrow_seen = false;
18579 if (it.current_y < it.last_visible_y
18580 && !f->fonts_changed
18581 && (first_unchanged_at_end_row == NULL
18582 || IT_CHARPOS (it) < stop_pos))
18583 it.glyph_row->reversed_p = false;
18584 while (it.current_y < it.last_visible_y
18585 && !f->fonts_changed
18586 && (first_unchanged_at_end_row == NULL
18587 || IT_CHARPOS (it) < stop_pos))
18589 if (display_line (&it))
18590 last_text_row = it.glyph_row - 1;
18593 if (f->fonts_changed)
18594 return -1;
18596 /* The redisplay iterations in display_line above could have
18597 triggered font-lock, which could have done something that
18598 invalidates IT->w window's end-point information, on which we
18599 rely below. E.g., one package, which will remain unnamed, used
18600 to install a font-lock-fontify-region-function that called
18601 bury-buffer, whose side effect is to switch the buffer displayed
18602 by IT->w, and that predictably resets IT->w's window_end_valid
18603 flag, which we already tested at the entry to this function.
18604 Amply punish such packages/modes by giving up on this
18605 optimization in those cases. */
18606 if (!w->window_end_valid)
18608 clear_glyph_matrix (w->desired_matrix);
18609 return -1;
18612 /* Compute differences in buffer positions, y-positions etc. for
18613 lines reused at the bottom of the window. Compute what we can
18614 scroll. */
18615 if (first_unchanged_at_end_row
18616 /* No lines reused because we displayed everything up to the
18617 bottom of the window. */
18618 && it.current_y < it.last_visible_y)
18620 dvpos = (it.vpos
18621 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18622 current_matrix));
18623 dy = it.current_y - first_unchanged_at_end_row->y;
18624 run.current_y = first_unchanged_at_end_row->y;
18625 run.desired_y = run.current_y + dy;
18626 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18628 else
18630 delta = delta_bytes = dvpos = dy
18631 = run.current_y = run.desired_y = run.height = 0;
18632 first_unchanged_at_end_row = NULL;
18634 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18637 /* Find the cursor if not already found. We have to decide whether
18638 PT will appear on this window (it sometimes doesn't, but this is
18639 not a very frequent case.) This decision has to be made before
18640 the current matrix is altered. A value of cursor.vpos < 0 means
18641 that PT is either in one of the lines beginning at
18642 first_unchanged_at_end_row or below the window. Don't care for
18643 lines that might be displayed later at the window end; as
18644 mentioned, this is not a frequent case. */
18645 if (w->cursor.vpos < 0)
18647 /* Cursor in unchanged rows at the top? */
18648 if (PT < CHARPOS (start_pos)
18649 && last_unchanged_at_beg_row)
18651 row = row_containing_pos (w, PT,
18652 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18653 last_unchanged_at_beg_row + 1, 0);
18654 if (row)
18655 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18658 /* Start from first_unchanged_at_end_row looking for PT. */
18659 else if (first_unchanged_at_end_row)
18661 row = row_containing_pos (w, PT - delta,
18662 first_unchanged_at_end_row, NULL, 0);
18663 if (row)
18664 set_cursor_from_row (w, row, w->current_matrix, delta,
18665 delta_bytes, dy, dvpos);
18668 /* Give up if cursor was not found. */
18669 if (w->cursor.vpos < 0)
18671 clear_glyph_matrix (w->desired_matrix);
18672 return -1;
18676 /* Don't let the cursor end in the scroll margins. */
18678 int this_scroll_margin, cursor_height;
18679 int frame_line_height = default_line_pixel_height (w);
18680 int window_total_lines
18681 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18683 this_scroll_margin =
18684 max (0, min (scroll_margin, window_total_lines / 4));
18685 this_scroll_margin *= frame_line_height;
18686 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18688 if ((w->cursor.y < this_scroll_margin
18689 && CHARPOS (start) > BEGV)
18690 /* Old redisplay didn't take scroll margin into account at the bottom,
18691 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18692 || (w->cursor.y + (make_cursor_line_fully_visible_p
18693 ? cursor_height + this_scroll_margin
18694 : 1)) > it.last_visible_y)
18696 w->cursor.vpos = -1;
18697 clear_glyph_matrix (w->desired_matrix);
18698 return -1;
18702 /* Scroll the display. Do it before changing the current matrix so
18703 that xterm.c doesn't get confused about where the cursor glyph is
18704 found. */
18705 if (dy && run.height)
18707 update_begin (f);
18709 if (FRAME_WINDOW_P (f))
18711 FRAME_RIF (f)->update_window_begin_hook (w);
18712 FRAME_RIF (f)->clear_window_mouse_face (w);
18713 FRAME_RIF (f)->scroll_run_hook (w, &run);
18714 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18716 else
18718 /* Terminal frame. In this case, dvpos gives the number of
18719 lines to scroll by; dvpos < 0 means scroll up. */
18720 int from_vpos
18721 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18722 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18723 int end = (WINDOW_TOP_EDGE_LINE (w)
18724 + WINDOW_WANTS_HEADER_LINE_P (w)
18725 + window_internal_height (w));
18727 #if defined (HAVE_GPM) || defined (MSDOS)
18728 x_clear_window_mouse_face (w);
18729 #endif
18730 /* Perform the operation on the screen. */
18731 if (dvpos > 0)
18733 /* Scroll last_unchanged_at_beg_row to the end of the
18734 window down dvpos lines. */
18735 set_terminal_window (f, end);
18737 /* On dumb terminals delete dvpos lines at the end
18738 before inserting dvpos empty lines. */
18739 if (!FRAME_SCROLL_REGION_OK (f))
18740 ins_del_lines (f, end - dvpos, -dvpos);
18742 /* Insert dvpos empty lines in front of
18743 last_unchanged_at_beg_row. */
18744 ins_del_lines (f, from, dvpos);
18746 else if (dvpos < 0)
18748 /* Scroll up last_unchanged_at_beg_vpos to the end of
18749 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18750 set_terminal_window (f, end);
18752 /* Delete dvpos lines in front of
18753 last_unchanged_at_beg_vpos. ins_del_lines will set
18754 the cursor to the given vpos and emit |dvpos| delete
18755 line sequences. */
18756 ins_del_lines (f, from + dvpos, dvpos);
18758 /* On a dumb terminal insert dvpos empty lines at the
18759 end. */
18760 if (!FRAME_SCROLL_REGION_OK (f))
18761 ins_del_lines (f, end + dvpos, -dvpos);
18764 set_terminal_window (f, 0);
18767 update_end (f);
18770 /* Shift reused rows of the current matrix to the right position.
18771 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18772 text. */
18773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18774 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18775 if (dvpos < 0)
18777 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18778 bottom_vpos, dvpos);
18779 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18780 bottom_vpos);
18782 else if (dvpos > 0)
18784 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18785 bottom_vpos, dvpos);
18786 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18787 first_unchanged_at_end_vpos + dvpos);
18790 /* For frame-based redisplay, make sure that current frame and window
18791 matrix are in sync with respect to glyph memory. */
18792 if (!FRAME_WINDOW_P (f))
18793 sync_frame_with_window_matrix_rows (w);
18795 /* Adjust buffer positions in reused rows. */
18796 if (delta || delta_bytes)
18797 increment_matrix_positions (current_matrix,
18798 first_unchanged_at_end_vpos + dvpos,
18799 bottom_vpos, delta, delta_bytes);
18801 /* Adjust Y positions. */
18802 if (dy)
18803 shift_glyph_matrix (w, current_matrix,
18804 first_unchanged_at_end_vpos + dvpos,
18805 bottom_vpos, dy);
18807 if (first_unchanged_at_end_row)
18809 first_unchanged_at_end_row += dvpos;
18810 if (first_unchanged_at_end_row->y >= it.last_visible_y
18811 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18812 first_unchanged_at_end_row = NULL;
18815 /* If scrolling up, there may be some lines to display at the end of
18816 the window. */
18817 last_text_row_at_end = NULL;
18818 if (dy < 0)
18820 /* Scrolling up can leave for example a partially visible line
18821 at the end of the window to be redisplayed. */
18822 /* Set last_row to the glyph row in the current matrix where the
18823 window end line is found. It has been moved up or down in
18824 the matrix by dvpos. */
18825 int last_vpos = w->window_end_vpos + dvpos;
18826 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18828 /* If last_row is the window end line, it should display text. */
18829 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18831 /* If window end line was partially visible before, begin
18832 displaying at that line. Otherwise begin displaying with the
18833 line following it. */
18834 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18836 init_to_row_start (&it, w, last_row);
18837 it.vpos = last_vpos;
18838 it.current_y = last_row->y;
18840 else
18842 init_to_row_end (&it, w, last_row);
18843 it.vpos = 1 + last_vpos;
18844 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18845 ++last_row;
18848 /* We may start in a continuation line. If so, we have to
18849 get the right continuation_lines_width and current_x. */
18850 it.continuation_lines_width = last_row->continuation_lines_width;
18851 it.hpos = it.current_x = 0;
18853 /* Display the rest of the lines at the window end. */
18854 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18855 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18857 /* Is it always sure that the display agrees with lines in
18858 the current matrix? I don't think so, so we mark rows
18859 displayed invalid in the current matrix by setting their
18860 enabled_p flag to false. */
18861 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18862 if (display_line (&it))
18863 last_text_row_at_end = it.glyph_row - 1;
18867 /* Update window_end_pos and window_end_vpos. */
18868 if (first_unchanged_at_end_row && !last_text_row_at_end)
18870 /* Window end line if one of the preserved rows from the current
18871 matrix. Set row to the last row displaying text in current
18872 matrix starting at first_unchanged_at_end_row, after
18873 scrolling. */
18874 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18875 row = find_last_row_displaying_text (w->current_matrix, &it,
18876 first_unchanged_at_end_row);
18877 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18878 adjust_window_ends (w, row, true);
18879 eassert (w->window_end_bytepos >= 0);
18880 IF_DEBUG (debug_method_add (w, "A"));
18882 else if (last_text_row_at_end)
18884 adjust_window_ends (w, last_text_row_at_end, false);
18885 eassert (w->window_end_bytepos >= 0);
18886 IF_DEBUG (debug_method_add (w, "B"));
18888 else if (last_text_row)
18890 /* We have displayed either to the end of the window or at the
18891 end of the window, i.e. the last row with text is to be found
18892 in the desired matrix. */
18893 adjust_window_ends (w, last_text_row, false);
18894 eassert (w->window_end_bytepos >= 0);
18896 else if (first_unchanged_at_end_row == NULL
18897 && last_text_row == NULL
18898 && last_text_row_at_end == NULL)
18900 /* Displayed to end of window, but no line containing text was
18901 displayed. Lines were deleted at the end of the window. */
18902 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18903 int vpos = w->window_end_vpos;
18904 struct glyph_row *current_row = current_matrix->rows + vpos;
18905 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18907 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18909 eassert (first_vpos <= vpos);
18910 if (desired_row->enabled_p)
18912 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18913 row = desired_row;
18915 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18916 row = current_row;
18919 w->window_end_vpos = vpos + 1;
18920 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18921 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18922 eassert (w->window_end_bytepos >= 0);
18923 IF_DEBUG (debug_method_add (w, "C"));
18925 else
18926 emacs_abort ();
18928 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18929 debug_end_vpos = w->window_end_vpos));
18931 /* Record that display has not been completed. */
18932 w->window_end_valid = false;
18933 w->desired_matrix->no_scrolling_p = true;
18934 return 3;
18936 #undef GIVE_UP
18941 /***********************************************************************
18942 More debugging support
18943 ***********************************************************************/
18945 #ifdef GLYPH_DEBUG
18947 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18948 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18949 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18952 /* Dump the contents of glyph matrix MATRIX on stderr.
18954 GLYPHS 0 means don't show glyph contents.
18955 GLYPHS 1 means show glyphs in short form
18956 GLYPHS > 1 means show glyphs in long form. */
18958 void
18959 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18961 int i;
18962 for (i = 0; i < matrix->nrows; ++i)
18963 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18967 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18968 the glyph row and area where the glyph comes from. */
18970 void
18971 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18973 if (glyph->type == CHAR_GLYPH
18974 || glyph->type == GLYPHLESS_GLYPH)
18976 fprintf (stderr,
18977 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18978 glyph - row->glyphs[TEXT_AREA],
18979 (glyph->type == CHAR_GLYPH
18980 ? 'C'
18981 : 'G'),
18982 glyph->charpos,
18983 (BUFFERP (glyph->object)
18984 ? 'B'
18985 : (STRINGP (glyph->object)
18986 ? 'S'
18987 : (NILP (glyph->object)
18988 ? '0'
18989 : '-'))),
18990 glyph->pixel_width,
18991 glyph->u.ch,
18992 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18993 ? glyph->u.ch
18994 : '.'),
18995 glyph->face_id,
18996 glyph->left_box_line_p,
18997 glyph->right_box_line_p);
18999 else if (glyph->type == STRETCH_GLYPH)
19001 fprintf (stderr,
19002 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19003 glyph - row->glyphs[TEXT_AREA],
19004 'S',
19005 glyph->charpos,
19006 (BUFFERP (glyph->object)
19007 ? 'B'
19008 : (STRINGP (glyph->object)
19009 ? 'S'
19010 : (NILP (glyph->object)
19011 ? '0'
19012 : '-'))),
19013 glyph->pixel_width,
19015 ' ',
19016 glyph->face_id,
19017 glyph->left_box_line_p,
19018 glyph->right_box_line_p);
19020 else if (glyph->type == IMAGE_GLYPH)
19022 fprintf (stderr,
19023 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19024 glyph - row->glyphs[TEXT_AREA],
19025 'I',
19026 glyph->charpos,
19027 (BUFFERP (glyph->object)
19028 ? 'B'
19029 : (STRINGP (glyph->object)
19030 ? 'S'
19031 : (NILP (glyph->object)
19032 ? '0'
19033 : '-'))),
19034 glyph->pixel_width,
19035 glyph->u.img_id,
19036 '.',
19037 glyph->face_id,
19038 glyph->left_box_line_p,
19039 glyph->right_box_line_p);
19041 else if (glyph->type == COMPOSITE_GLYPH)
19043 fprintf (stderr,
19044 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19045 glyph - row->glyphs[TEXT_AREA],
19046 '+',
19047 glyph->charpos,
19048 (BUFFERP (glyph->object)
19049 ? 'B'
19050 : (STRINGP (glyph->object)
19051 ? 'S'
19052 : (NILP (glyph->object)
19053 ? '0'
19054 : '-'))),
19055 glyph->pixel_width,
19056 glyph->u.cmp.id);
19057 if (glyph->u.cmp.automatic)
19058 fprintf (stderr,
19059 "[%d-%d]",
19060 glyph->slice.cmp.from, glyph->slice.cmp.to);
19061 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19062 glyph->face_id,
19063 glyph->left_box_line_p,
19064 glyph->right_box_line_p);
19066 else if (glyph->type == XWIDGET_GLYPH)
19068 #ifndef HAVE_XWIDGETS
19069 eassume (false);
19070 #else
19071 fprintf (stderr,
19072 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19073 glyph - row->glyphs[TEXT_AREA],
19074 'X',
19075 glyph->charpos,
19076 (BUFFERP (glyph->object)
19077 ? 'B'
19078 : (STRINGP (glyph->object)
19079 ? 'S'
19080 : '-')),
19081 glyph->pixel_width,
19082 glyph->u.xwidget,
19083 '.',
19084 glyph->face_id,
19085 glyph->left_box_line_p,
19086 glyph->right_box_line_p);
19087 #endif
19092 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19093 GLYPHS 0 means don't show glyph contents.
19094 GLYPHS 1 means show glyphs in short form
19095 GLYPHS > 1 means show glyphs in long form. */
19097 void
19098 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19100 if (glyphs != 1)
19102 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19103 fprintf (stderr, "==============================================================================\n");
19105 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19106 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19107 vpos,
19108 MATRIX_ROW_START_CHARPOS (row),
19109 MATRIX_ROW_END_CHARPOS (row),
19110 row->used[TEXT_AREA],
19111 row->contains_overlapping_glyphs_p,
19112 row->enabled_p,
19113 row->truncated_on_left_p,
19114 row->truncated_on_right_p,
19115 row->continued_p,
19116 MATRIX_ROW_CONTINUATION_LINE_P (row),
19117 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19118 row->ends_at_zv_p,
19119 row->fill_line_p,
19120 row->ends_in_middle_of_char_p,
19121 row->starts_in_middle_of_char_p,
19122 row->mouse_face_p,
19123 row->x,
19124 row->y,
19125 row->pixel_width,
19126 row->height,
19127 row->visible_height,
19128 row->ascent,
19129 row->phys_ascent);
19130 /* The next 3 lines should align to "Start" in the header. */
19131 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19132 row->end.overlay_string_index,
19133 row->continuation_lines_width);
19134 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19135 CHARPOS (row->start.string_pos),
19136 CHARPOS (row->end.string_pos));
19137 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19138 row->end.dpvec_index);
19141 if (glyphs > 1)
19143 int area;
19145 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19147 struct glyph *glyph = row->glyphs[area];
19148 struct glyph *glyph_end = glyph + row->used[area];
19150 /* Glyph for a line end in text. */
19151 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19152 ++glyph_end;
19154 if (glyph < glyph_end)
19155 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19157 for (; glyph < glyph_end; ++glyph)
19158 dump_glyph (row, glyph, area);
19161 else if (glyphs == 1)
19163 int area;
19164 char s[SHRT_MAX + 4];
19166 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19168 int i;
19170 for (i = 0; i < row->used[area]; ++i)
19172 struct glyph *glyph = row->glyphs[area] + i;
19173 if (i == row->used[area] - 1
19174 && area == TEXT_AREA
19175 && NILP (glyph->object)
19176 && glyph->type == CHAR_GLYPH
19177 && glyph->u.ch == ' ')
19179 strcpy (&s[i], "[\\n]");
19180 i += 4;
19182 else if (glyph->type == CHAR_GLYPH
19183 && glyph->u.ch < 0x80
19184 && glyph->u.ch >= ' ')
19185 s[i] = glyph->u.ch;
19186 else
19187 s[i] = '.';
19190 s[i] = '\0';
19191 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19197 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19198 Sdump_glyph_matrix, 0, 1, "p",
19199 doc: /* Dump the current matrix of the selected window to stderr.
19200 Shows contents of glyph row structures. With non-nil
19201 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19202 glyphs in short form, otherwise show glyphs in long form.
19204 Interactively, no argument means show glyphs in short form;
19205 with numeric argument, its value is passed as the GLYPHS flag. */)
19206 (Lisp_Object glyphs)
19208 struct window *w = XWINDOW (selected_window);
19209 struct buffer *buffer = XBUFFER (w->contents);
19211 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19212 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19213 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19214 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19215 fprintf (stderr, "=============================================\n");
19216 dump_glyph_matrix (w->current_matrix,
19217 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19218 return Qnil;
19222 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19223 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19224 Only text-mode frames have frame glyph matrices. */)
19225 (void)
19227 struct frame *f = XFRAME (selected_frame);
19229 if (f->current_matrix)
19230 dump_glyph_matrix (f->current_matrix, 1);
19231 else
19232 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19233 return Qnil;
19237 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19238 doc: /* Dump glyph row ROW to stderr.
19239 GLYPH 0 means don't dump glyphs.
19240 GLYPH 1 means dump glyphs in short form.
19241 GLYPH > 1 or omitted means dump glyphs in long form. */)
19242 (Lisp_Object row, Lisp_Object glyphs)
19244 struct glyph_matrix *matrix;
19245 EMACS_INT vpos;
19247 CHECK_NUMBER (row);
19248 matrix = XWINDOW (selected_window)->current_matrix;
19249 vpos = XINT (row);
19250 if (vpos >= 0 && vpos < matrix->nrows)
19251 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19252 vpos,
19253 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19254 return Qnil;
19258 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19259 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19260 GLYPH 0 means don't dump glyphs.
19261 GLYPH 1 means dump glyphs in short form.
19262 GLYPH > 1 or omitted means dump glyphs in long form.
19264 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19265 do nothing. */)
19266 (Lisp_Object row, Lisp_Object glyphs)
19268 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19269 struct frame *sf = SELECTED_FRAME ();
19270 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19271 EMACS_INT vpos;
19273 CHECK_NUMBER (row);
19274 vpos = XINT (row);
19275 if (vpos >= 0 && vpos < m->nrows)
19276 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19277 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19278 #endif
19279 return Qnil;
19283 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19284 doc: /* Toggle tracing of redisplay.
19285 With ARG, turn tracing on if and only if ARG is positive. */)
19286 (Lisp_Object arg)
19288 if (NILP (arg))
19289 trace_redisplay_p = !trace_redisplay_p;
19290 else
19292 arg = Fprefix_numeric_value (arg);
19293 trace_redisplay_p = XINT (arg) > 0;
19296 return Qnil;
19300 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19301 doc: /* Like `format', but print result to stderr.
19302 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19303 (ptrdiff_t nargs, Lisp_Object *args)
19305 Lisp_Object s = Fformat (nargs, args);
19306 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19307 return Qnil;
19310 #endif /* GLYPH_DEBUG */
19314 /***********************************************************************
19315 Building Desired Matrix Rows
19316 ***********************************************************************/
19318 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19319 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19321 static struct glyph_row *
19322 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19324 struct frame *f = XFRAME (WINDOW_FRAME (w));
19325 struct buffer *buffer = XBUFFER (w->contents);
19326 struct buffer *old = current_buffer;
19327 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19328 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19329 const unsigned char *arrow_end = arrow_string + arrow_len;
19330 const unsigned char *p;
19331 struct it it;
19332 bool multibyte_p;
19333 int n_glyphs_before;
19335 set_buffer_temp (buffer);
19336 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19337 scratch_glyph_row.reversed_p = false;
19338 it.glyph_row->used[TEXT_AREA] = 0;
19339 SET_TEXT_POS (it.position, 0, 0);
19341 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19342 p = arrow_string;
19343 while (p < arrow_end)
19345 Lisp_Object face, ilisp;
19347 /* Get the next character. */
19348 if (multibyte_p)
19349 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19350 else
19352 it.c = it.char_to_display = *p, it.len = 1;
19353 if (! ASCII_CHAR_P (it.c))
19354 it.char_to_display = BYTE8_TO_CHAR (it.c);
19356 p += it.len;
19358 /* Get its face. */
19359 ilisp = make_number (p - arrow_string);
19360 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19361 it.face_id = compute_char_face (f, it.char_to_display, face);
19363 /* Compute its width, get its glyphs. */
19364 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19365 SET_TEXT_POS (it.position, -1, -1);
19366 PRODUCE_GLYPHS (&it);
19368 /* If this character doesn't fit any more in the line, we have
19369 to remove some glyphs. */
19370 if (it.current_x > it.last_visible_x)
19372 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19373 break;
19377 set_buffer_temp (old);
19378 return it.glyph_row;
19382 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19383 glyphs to insert is determined by produce_special_glyphs. */
19385 static void
19386 insert_left_trunc_glyphs (struct it *it)
19388 struct it truncate_it;
19389 struct glyph *from, *end, *to, *toend;
19391 eassert (!FRAME_WINDOW_P (it->f)
19392 || (!it->glyph_row->reversed_p
19393 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19394 || (it->glyph_row->reversed_p
19395 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19397 /* Get the truncation glyphs. */
19398 truncate_it = *it;
19399 truncate_it.current_x = 0;
19400 truncate_it.face_id = DEFAULT_FACE_ID;
19401 truncate_it.glyph_row = &scratch_glyph_row;
19402 truncate_it.area = TEXT_AREA;
19403 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19404 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19405 truncate_it.object = Qnil;
19406 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19408 /* Overwrite glyphs from IT with truncation glyphs. */
19409 if (!it->glyph_row->reversed_p)
19411 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19413 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19414 end = from + tused;
19415 to = it->glyph_row->glyphs[TEXT_AREA];
19416 toend = to + it->glyph_row->used[TEXT_AREA];
19417 if (FRAME_WINDOW_P (it->f))
19419 /* On GUI frames, when variable-size fonts are displayed,
19420 the truncation glyphs may need more pixels than the row's
19421 glyphs they overwrite. We overwrite more glyphs to free
19422 enough screen real estate, and enlarge the stretch glyph
19423 on the right (see display_line), if there is one, to
19424 preserve the screen position of the truncation glyphs on
19425 the right. */
19426 int w = 0;
19427 struct glyph *g = to;
19428 short used;
19430 /* The first glyph could be partially visible, in which case
19431 it->glyph_row->x will be negative. But we want the left
19432 truncation glyphs to be aligned at the left margin of the
19433 window, so we override the x coordinate at which the row
19434 will begin. */
19435 it->glyph_row->x = 0;
19436 while (g < toend && w < it->truncation_pixel_width)
19438 w += g->pixel_width;
19439 ++g;
19441 if (g - to - tused > 0)
19443 memmove (to + tused, g, (toend - g) * sizeof(*g));
19444 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19446 used = it->glyph_row->used[TEXT_AREA];
19447 if (it->glyph_row->truncated_on_right_p
19448 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19449 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19450 == STRETCH_GLYPH)
19452 int extra = w - it->truncation_pixel_width;
19454 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19458 while (from < end)
19459 *to++ = *from++;
19461 /* There may be padding glyphs left over. Overwrite them too. */
19462 if (!FRAME_WINDOW_P (it->f))
19464 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19466 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19467 while (from < end)
19468 *to++ = *from++;
19472 if (to > toend)
19473 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19475 else
19477 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19479 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19480 that back to front. */
19481 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19482 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19483 toend = it->glyph_row->glyphs[TEXT_AREA];
19484 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19485 if (FRAME_WINDOW_P (it->f))
19487 int w = 0;
19488 struct glyph *g = to;
19490 while (g >= toend && w < it->truncation_pixel_width)
19492 w += g->pixel_width;
19493 --g;
19495 if (to - g - tused > 0)
19496 to = g + tused;
19497 if (it->glyph_row->truncated_on_right_p
19498 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19499 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19501 int extra = w - it->truncation_pixel_width;
19503 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19507 while (from >= end && to >= toend)
19508 *to-- = *from--;
19509 if (!FRAME_WINDOW_P (it->f))
19511 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19513 from =
19514 truncate_it.glyph_row->glyphs[TEXT_AREA]
19515 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19516 while (from >= end && to >= toend)
19517 *to-- = *from--;
19520 if (from >= end)
19522 /* Need to free some room before prepending additional
19523 glyphs. */
19524 int move_by = from - end + 1;
19525 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19526 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19528 for ( ; g >= g0; g--)
19529 g[move_by] = *g;
19530 while (from >= end)
19531 *to-- = *from--;
19532 it->glyph_row->used[TEXT_AREA] += move_by;
19537 /* Compute the hash code for ROW. */
19538 unsigned
19539 row_hash (struct glyph_row *row)
19541 int area, k;
19542 unsigned hashval = 0;
19544 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19545 for (k = 0; k < row->used[area]; ++k)
19546 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19547 + row->glyphs[area][k].u.val
19548 + row->glyphs[area][k].face_id
19549 + row->glyphs[area][k].padding_p
19550 + (row->glyphs[area][k].type << 2));
19552 return hashval;
19555 /* Compute the pixel height and width of IT->glyph_row.
19557 Most of the time, ascent and height of a display line will be equal
19558 to the max_ascent and max_height values of the display iterator
19559 structure. This is not the case if
19561 1. We hit ZV without displaying anything. In this case, max_ascent
19562 and max_height will be zero.
19564 2. We have some glyphs that don't contribute to the line height.
19565 (The glyph row flag contributes_to_line_height_p is for future
19566 pixmap extensions).
19568 The first case is easily covered by using default values because in
19569 these cases, the line height does not really matter, except that it
19570 must not be zero. */
19572 static void
19573 compute_line_metrics (struct it *it)
19575 struct glyph_row *row = it->glyph_row;
19577 if (FRAME_WINDOW_P (it->f))
19579 int i, min_y, max_y;
19581 /* The line may consist of one space only, that was added to
19582 place the cursor on it. If so, the row's height hasn't been
19583 computed yet. */
19584 if (row->height == 0)
19586 if (it->max_ascent + it->max_descent == 0)
19587 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19588 row->ascent = it->max_ascent;
19589 row->height = it->max_ascent + it->max_descent;
19590 row->phys_ascent = it->max_phys_ascent;
19591 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19592 row->extra_line_spacing = it->max_extra_line_spacing;
19595 /* Compute the width of this line. */
19596 row->pixel_width = row->x;
19597 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19598 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19600 eassert (row->pixel_width >= 0);
19601 eassert (row->ascent >= 0 && row->height > 0);
19603 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19604 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19606 /* If first line's physical ascent is larger than its logical
19607 ascent, use the physical ascent, and make the row taller.
19608 This makes accented characters fully visible. */
19609 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19610 && row->phys_ascent > row->ascent)
19612 row->height += row->phys_ascent - row->ascent;
19613 row->ascent = row->phys_ascent;
19616 /* Compute how much of the line is visible. */
19617 row->visible_height = row->height;
19619 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19620 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19622 if (row->y < min_y)
19623 row->visible_height -= min_y - row->y;
19624 if (row->y + row->height > max_y)
19625 row->visible_height -= row->y + row->height - max_y;
19627 else
19629 row->pixel_width = row->used[TEXT_AREA];
19630 if (row->continued_p)
19631 row->pixel_width -= it->continuation_pixel_width;
19632 else if (row->truncated_on_right_p)
19633 row->pixel_width -= it->truncation_pixel_width;
19634 row->ascent = row->phys_ascent = 0;
19635 row->height = row->phys_height = row->visible_height = 1;
19636 row->extra_line_spacing = 0;
19639 /* Compute a hash code for this row. */
19640 row->hash = row_hash (row);
19642 it->max_ascent = it->max_descent = 0;
19643 it->max_phys_ascent = it->max_phys_descent = 0;
19647 /* Append one space to the glyph row of iterator IT if doing a
19648 window-based redisplay. The space has the same face as
19649 IT->face_id. Value is true if a space was added.
19651 This function is called to make sure that there is always one glyph
19652 at the end of a glyph row that the cursor can be set on under
19653 window-systems. (If there weren't such a glyph we would not know
19654 how wide and tall a box cursor should be displayed).
19656 At the same time this space let's a nicely handle clearing to the
19657 end of the line if the row ends in italic text. */
19659 static bool
19660 append_space_for_newline (struct it *it, bool default_face_p)
19662 if (FRAME_WINDOW_P (it->f))
19664 int n = it->glyph_row->used[TEXT_AREA];
19666 if (it->glyph_row->glyphs[TEXT_AREA] + n
19667 < it->glyph_row->glyphs[1 + TEXT_AREA])
19669 /* Save some values that must not be changed.
19670 Must save IT->c and IT->len because otherwise
19671 ITERATOR_AT_END_P wouldn't work anymore after
19672 append_space_for_newline has been called. */
19673 enum display_element_type saved_what = it->what;
19674 int saved_c = it->c, saved_len = it->len;
19675 int saved_char_to_display = it->char_to_display;
19676 int saved_x = it->current_x;
19677 int saved_face_id = it->face_id;
19678 bool saved_box_end = it->end_of_box_run_p;
19679 struct text_pos saved_pos;
19680 Lisp_Object saved_object;
19681 struct face *face;
19683 saved_object = it->object;
19684 saved_pos = it->position;
19686 it->what = IT_CHARACTER;
19687 memset (&it->position, 0, sizeof it->position);
19688 it->object = Qnil;
19689 it->c = it->char_to_display = ' ';
19690 it->len = 1;
19692 /* If the default face was remapped, be sure to use the
19693 remapped face for the appended newline. */
19694 if (default_face_p)
19695 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19696 else if (it->face_before_selective_p)
19697 it->face_id = it->saved_face_id;
19698 face = FACE_FROM_ID (it->f, it->face_id);
19699 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19700 /* In R2L rows, we will prepend a stretch glyph that will
19701 have the end_of_box_run_p flag set for it, so there's no
19702 need for the appended newline glyph to have that flag
19703 set. */
19704 if (it->glyph_row->reversed_p
19705 /* But if the appended newline glyph goes all the way to
19706 the end of the row, there will be no stretch glyph,
19707 so leave the box flag set. */
19708 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19709 it->end_of_box_run_p = false;
19711 PRODUCE_GLYPHS (it);
19713 #ifdef HAVE_WINDOW_SYSTEM
19714 /* Make sure this space glyph has the right ascent and
19715 descent values, or else cursor at end of line will look
19716 funny, and height of empty lines will be incorrect. */
19717 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19718 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19719 if (n == 0)
19721 Lisp_Object height, total_height;
19722 int extra_line_spacing = it->extra_line_spacing;
19723 int boff = font->baseline_offset;
19725 if (font->vertical_centering)
19726 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19728 it->object = saved_object; /* get_it_property needs this */
19729 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19730 /* Must do a subset of line height processing from
19731 x_produce_glyph for newline characters. */
19732 height = get_it_property (it, Qline_height);
19733 if (CONSP (height)
19734 && CONSP (XCDR (height))
19735 && NILP (XCDR (XCDR (height))))
19737 total_height = XCAR (XCDR (height));
19738 height = XCAR (height);
19740 else
19741 total_height = Qnil;
19742 height = calc_line_height_property (it, height, font, boff, true);
19744 if (it->override_ascent >= 0)
19746 it->ascent = it->override_ascent;
19747 it->descent = it->override_descent;
19748 boff = it->override_boff;
19750 if (EQ (height, Qt))
19751 extra_line_spacing = 0;
19752 else
19754 Lisp_Object spacing;
19756 it->phys_ascent = it->ascent;
19757 it->phys_descent = it->descent;
19758 if (!NILP (height)
19759 && XINT (height) > it->ascent + it->descent)
19760 it->ascent = XINT (height) - it->descent;
19762 if (!NILP (total_height))
19763 spacing = calc_line_height_property (it, total_height, font,
19764 boff, false);
19765 else
19767 spacing = get_it_property (it, Qline_spacing);
19768 spacing = calc_line_height_property (it, spacing, font,
19769 boff, false);
19771 if (INTEGERP (spacing))
19773 extra_line_spacing = XINT (spacing);
19774 if (!NILP (total_height))
19775 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19778 if (extra_line_spacing > 0)
19780 it->descent += extra_line_spacing;
19781 if (extra_line_spacing > it->max_extra_line_spacing)
19782 it->max_extra_line_spacing = extra_line_spacing;
19784 it->max_ascent = it->ascent;
19785 it->max_descent = it->descent;
19786 /* Make sure compute_line_metrics recomputes the row height. */
19787 it->glyph_row->height = 0;
19790 g->ascent = it->max_ascent;
19791 g->descent = it->max_descent;
19792 #endif
19794 it->override_ascent = -1;
19795 it->constrain_row_ascent_descent_p = false;
19796 it->current_x = saved_x;
19797 it->object = saved_object;
19798 it->position = saved_pos;
19799 it->what = saved_what;
19800 it->face_id = saved_face_id;
19801 it->len = saved_len;
19802 it->c = saved_c;
19803 it->char_to_display = saved_char_to_display;
19804 it->end_of_box_run_p = saved_box_end;
19805 return true;
19809 return false;
19813 /* Extend the face of the last glyph in the text area of IT->glyph_row
19814 to the end of the display line. Called from display_line. If the
19815 glyph row is empty, add a space glyph to it so that we know the
19816 face to draw. Set the glyph row flag fill_line_p. If the glyph
19817 row is R2L, prepend a stretch glyph to cover the empty space to the
19818 left of the leftmost glyph. */
19820 static void
19821 extend_face_to_end_of_line (struct it *it)
19823 struct face *face, *default_face;
19824 struct frame *f = it->f;
19826 /* If line is already filled, do nothing. Non window-system frames
19827 get a grace of one more ``pixel'' because their characters are
19828 1-``pixel'' wide, so they hit the equality too early. This grace
19829 is needed only for R2L rows that are not continued, to produce
19830 one extra blank where we could display the cursor. */
19831 if ((it->current_x >= it->last_visible_x
19832 + (!FRAME_WINDOW_P (f)
19833 && it->glyph_row->reversed_p
19834 && !it->glyph_row->continued_p))
19835 /* If the window has display margins, we will need to extend
19836 their face even if the text area is filled. */
19837 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19838 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19839 return;
19841 /* The default face, possibly remapped. */
19842 default_face = FACE_FROM_ID_OR_NULL (f,
19843 lookup_basic_face (f, DEFAULT_FACE_ID));
19845 /* Face extension extends the background and box of IT->face_id
19846 to the end of the line. If the background equals the background
19847 of the frame, we don't have to do anything. */
19848 face = FACE_FROM_ID (f, (it->face_before_selective_p
19849 ? it->saved_face_id
19850 : it->face_id));
19852 if (FRAME_WINDOW_P (f)
19853 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19854 && face->box == FACE_NO_BOX
19855 && face->background == FRAME_BACKGROUND_PIXEL (f)
19856 #ifdef HAVE_WINDOW_SYSTEM
19857 && !face->stipple
19858 #endif
19859 && !it->glyph_row->reversed_p)
19860 return;
19862 /* Set the glyph row flag indicating that the face of the last glyph
19863 in the text area has to be drawn to the end of the text area. */
19864 it->glyph_row->fill_line_p = true;
19866 /* If current character of IT is not ASCII, make sure we have the
19867 ASCII face. This will be automatically undone the next time
19868 get_next_display_element returns a multibyte character. Note
19869 that the character will always be single byte in unibyte
19870 text. */
19871 if (!ASCII_CHAR_P (it->c))
19873 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19876 if (FRAME_WINDOW_P (f))
19878 /* If the row is empty, add a space with the current face of IT,
19879 so that we know which face to draw. */
19880 if (it->glyph_row->used[TEXT_AREA] == 0)
19882 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19883 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19884 it->glyph_row->used[TEXT_AREA] = 1;
19886 /* Mode line and the header line don't have margins, and
19887 likewise the frame's tool-bar window, if there is any. */
19888 if (!(it->glyph_row->mode_line_p
19889 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19890 || (WINDOWP (f->tool_bar_window)
19891 && it->w == XWINDOW (f->tool_bar_window))
19892 #endif
19895 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19896 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19898 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19899 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19900 default_face->id;
19901 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19903 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19904 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19906 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19907 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19908 default_face->id;
19909 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19912 #ifdef HAVE_WINDOW_SYSTEM
19913 if (it->glyph_row->reversed_p)
19915 /* Prepend a stretch glyph to the row, such that the
19916 rightmost glyph will be drawn flushed all the way to the
19917 right margin of the window. The stretch glyph that will
19918 occupy the empty space, if any, to the left of the
19919 glyphs. */
19920 struct font *font = face->font ? face->font : FRAME_FONT (f);
19921 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19922 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19923 struct glyph *g;
19924 int row_width, stretch_ascent, stretch_width;
19925 struct text_pos saved_pos;
19926 int saved_face_id;
19927 bool saved_avoid_cursor, saved_box_start;
19929 for (row_width = 0, g = row_start; g < row_end; g++)
19930 row_width += g->pixel_width;
19932 /* FIXME: There are various minor display glitches in R2L
19933 rows when only one of the fringes is missing. The
19934 strange condition below produces the least bad effect. */
19935 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19936 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19937 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19938 stretch_width = window_box_width (it->w, TEXT_AREA);
19939 else
19940 stretch_width = it->last_visible_x - it->first_visible_x;
19941 stretch_width -= row_width;
19943 if (stretch_width > 0)
19945 stretch_ascent =
19946 (((it->ascent + it->descent)
19947 * FONT_BASE (font)) / FONT_HEIGHT (font));
19948 saved_pos = it->position;
19949 memset (&it->position, 0, sizeof it->position);
19950 saved_avoid_cursor = it->avoid_cursor_p;
19951 it->avoid_cursor_p = true;
19952 saved_face_id = it->face_id;
19953 saved_box_start = it->start_of_box_run_p;
19954 /* The last row's stretch glyph should get the default
19955 face, to avoid painting the rest of the window with
19956 the region face, if the region ends at ZV. */
19957 if (it->glyph_row->ends_at_zv_p)
19958 it->face_id = default_face->id;
19959 else
19960 it->face_id = face->id;
19961 it->start_of_box_run_p = false;
19962 append_stretch_glyph (it, Qnil, stretch_width,
19963 it->ascent + it->descent, stretch_ascent);
19964 it->position = saved_pos;
19965 it->avoid_cursor_p = saved_avoid_cursor;
19966 it->face_id = saved_face_id;
19967 it->start_of_box_run_p = saved_box_start;
19969 /* If stretch_width comes out negative, it means that the
19970 last glyph is only partially visible. In R2L rows, we
19971 want the leftmost glyph to be partially visible, so we
19972 need to give the row the corresponding left offset. */
19973 if (stretch_width < 0)
19974 it->glyph_row->x = stretch_width;
19976 #endif /* HAVE_WINDOW_SYSTEM */
19978 else
19980 /* Save some values that must not be changed. */
19981 int saved_x = it->current_x;
19982 struct text_pos saved_pos;
19983 Lisp_Object saved_object;
19984 enum display_element_type saved_what = it->what;
19985 int saved_face_id = it->face_id;
19987 saved_object = it->object;
19988 saved_pos = it->position;
19990 it->what = IT_CHARACTER;
19991 memset (&it->position, 0, sizeof it->position);
19992 it->object = Qnil;
19993 it->c = it->char_to_display = ' ';
19994 it->len = 1;
19996 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19997 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19998 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19999 && !it->glyph_row->mode_line_p
20000 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20002 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20003 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20005 for (it->current_x = 0; g < e; g++)
20006 it->current_x += g->pixel_width;
20008 it->area = LEFT_MARGIN_AREA;
20009 it->face_id = default_face->id;
20010 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20011 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20013 PRODUCE_GLYPHS (it);
20014 /* term.c:produce_glyphs advances it->current_x only for
20015 TEXT_AREA. */
20016 it->current_x += it->pixel_width;
20019 it->current_x = saved_x;
20020 it->area = TEXT_AREA;
20023 /* The last row's blank glyphs should get the default face, to
20024 avoid painting the rest of the window with the region face,
20025 if the region ends at ZV. */
20026 if (it->glyph_row->ends_at_zv_p)
20027 it->face_id = default_face->id;
20028 else
20029 it->face_id = face->id;
20030 PRODUCE_GLYPHS (it);
20032 while (it->current_x <= it->last_visible_x)
20033 PRODUCE_GLYPHS (it);
20035 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20036 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20037 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20038 && !it->glyph_row->mode_line_p
20039 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20041 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20042 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20044 for ( ; g < e; g++)
20045 it->current_x += g->pixel_width;
20047 it->area = RIGHT_MARGIN_AREA;
20048 it->face_id = default_face->id;
20049 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20050 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20052 PRODUCE_GLYPHS (it);
20053 it->current_x += it->pixel_width;
20056 it->area = TEXT_AREA;
20059 /* Don't count these blanks really. It would let us insert a left
20060 truncation glyph below and make us set the cursor on them, maybe. */
20061 it->current_x = saved_x;
20062 it->object = saved_object;
20063 it->position = saved_pos;
20064 it->what = saved_what;
20065 it->face_id = saved_face_id;
20070 /* Value is true if text starting at CHARPOS in current_buffer is
20071 trailing whitespace. */
20073 static bool
20074 trailing_whitespace_p (ptrdiff_t charpos)
20076 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20077 int c = 0;
20079 while (bytepos < ZV_BYTE
20080 && (c = FETCH_CHAR (bytepos),
20081 c == ' ' || c == '\t'))
20082 ++bytepos;
20084 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20086 if (bytepos != PT_BYTE)
20087 return true;
20089 return false;
20093 /* Highlight trailing whitespace, if any, in ROW. */
20095 static void
20096 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20098 int used = row->used[TEXT_AREA];
20100 if (used)
20102 struct glyph *start = row->glyphs[TEXT_AREA];
20103 struct glyph *glyph = start + used - 1;
20105 if (row->reversed_p)
20107 /* Right-to-left rows need to be processed in the opposite
20108 direction, so swap the edge pointers. */
20109 glyph = start;
20110 start = row->glyphs[TEXT_AREA] + used - 1;
20113 /* Skip over glyphs inserted to display the cursor at the
20114 end of a line, for extending the face of the last glyph
20115 to the end of the line on terminals, and for truncation
20116 and continuation glyphs. */
20117 if (!row->reversed_p)
20119 while (glyph >= start
20120 && glyph->type == CHAR_GLYPH
20121 && NILP (glyph->object))
20122 --glyph;
20124 else
20126 while (glyph <= start
20127 && glyph->type == CHAR_GLYPH
20128 && NILP (glyph->object))
20129 ++glyph;
20132 /* If last glyph is a space or stretch, and it's trailing
20133 whitespace, set the face of all trailing whitespace glyphs in
20134 IT->glyph_row to `trailing-whitespace'. */
20135 if ((row->reversed_p ? glyph <= start : glyph >= start)
20136 && BUFFERP (glyph->object)
20137 && (glyph->type == STRETCH_GLYPH
20138 || (glyph->type == CHAR_GLYPH
20139 && glyph->u.ch == ' '))
20140 && trailing_whitespace_p (glyph->charpos))
20142 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20143 if (face_id < 0)
20144 return;
20146 if (!row->reversed_p)
20148 while (glyph >= start
20149 && BUFFERP (glyph->object)
20150 && (glyph->type == STRETCH_GLYPH
20151 || (glyph->type == CHAR_GLYPH
20152 && glyph->u.ch == ' ')))
20153 (glyph--)->face_id = face_id;
20155 else
20157 while (glyph <= start
20158 && BUFFERP (glyph->object)
20159 && (glyph->type == STRETCH_GLYPH
20160 || (glyph->type == CHAR_GLYPH
20161 && glyph->u.ch == ' ')))
20162 (glyph++)->face_id = face_id;
20169 /* Value is true if glyph row ROW should be
20170 considered to hold the buffer position CHARPOS. */
20172 static bool
20173 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20175 bool result = true;
20177 if (charpos == CHARPOS (row->end.pos)
20178 || charpos == MATRIX_ROW_END_CHARPOS (row))
20180 /* Suppose the row ends on a string.
20181 Unless the row is continued, that means it ends on a newline
20182 in the string. If it's anything other than a display string
20183 (e.g., a before-string from an overlay), we don't want the
20184 cursor there. (This heuristic seems to give the optimal
20185 behavior for the various types of multi-line strings.)
20186 One exception: if the string has `cursor' property on one of
20187 its characters, we _do_ want the cursor there. */
20188 if (CHARPOS (row->end.string_pos) >= 0)
20190 if (row->continued_p)
20191 result = true;
20192 else
20194 /* Check for `display' property. */
20195 struct glyph *beg = row->glyphs[TEXT_AREA];
20196 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20197 struct glyph *glyph;
20199 result = false;
20200 for (glyph = end; glyph >= beg; --glyph)
20201 if (STRINGP (glyph->object))
20203 Lisp_Object prop
20204 = Fget_char_property (make_number (charpos),
20205 Qdisplay, Qnil);
20206 result =
20207 (!NILP (prop)
20208 && display_prop_string_p (prop, glyph->object));
20209 /* If there's a `cursor' property on one of the
20210 string's characters, this row is a cursor row,
20211 even though this is not a display string. */
20212 if (!result)
20214 Lisp_Object s = glyph->object;
20216 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20218 ptrdiff_t gpos = glyph->charpos;
20220 if (!NILP (Fget_char_property (make_number (gpos),
20221 Qcursor, s)))
20223 result = true;
20224 break;
20228 break;
20232 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20234 /* If the row ends in middle of a real character,
20235 and the line is continued, we want the cursor here.
20236 That's because CHARPOS (ROW->end.pos) would equal
20237 PT if PT is before the character. */
20238 if (!row->ends_in_ellipsis_p)
20239 result = row->continued_p;
20240 else
20241 /* If the row ends in an ellipsis, then
20242 CHARPOS (ROW->end.pos) will equal point after the
20243 invisible text. We want that position to be displayed
20244 after the ellipsis. */
20245 result = false;
20247 /* If the row ends at ZV, display the cursor at the end of that
20248 row instead of at the start of the row below. */
20249 else
20250 result = row->ends_at_zv_p;
20253 return result;
20256 /* Value is true if glyph row ROW should be
20257 used to hold the cursor. */
20259 static bool
20260 cursor_row_p (struct glyph_row *row)
20262 return row_for_charpos_p (row, PT);
20267 /* Push the property PROP so that it will be rendered at the current
20268 position in IT. Return true if PROP was successfully pushed, false
20269 otherwise. Called from handle_line_prefix to handle the
20270 `line-prefix' and `wrap-prefix' properties. */
20272 static bool
20273 push_prefix_prop (struct it *it, Lisp_Object prop)
20275 struct text_pos pos =
20276 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20278 eassert (it->method == GET_FROM_BUFFER
20279 || it->method == GET_FROM_DISPLAY_VECTOR
20280 || it->method == GET_FROM_STRING
20281 || it->method == GET_FROM_IMAGE);
20283 /* We need to save the current buffer/string position, so it will be
20284 restored by pop_it, because iterate_out_of_display_property
20285 depends on that being set correctly, but some situations leave
20286 it->position not yet set when this function is called. */
20287 push_it (it, &pos);
20289 if (STRINGP (prop))
20291 if (SCHARS (prop) == 0)
20293 pop_it (it);
20294 return false;
20297 it->string = prop;
20298 it->string_from_prefix_prop_p = true;
20299 it->multibyte_p = STRING_MULTIBYTE (it->string);
20300 it->current.overlay_string_index = -1;
20301 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20302 it->end_charpos = it->string_nchars = SCHARS (it->string);
20303 it->method = GET_FROM_STRING;
20304 it->stop_charpos = 0;
20305 it->prev_stop = 0;
20306 it->base_level_stop = 0;
20308 /* Force paragraph direction to be that of the parent
20309 buffer/string. */
20310 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20311 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20312 else
20313 it->paragraph_embedding = L2R;
20315 /* Set up the bidi iterator for this display string. */
20316 if (it->bidi_p)
20318 it->bidi_it.string.lstring = it->string;
20319 it->bidi_it.string.s = NULL;
20320 it->bidi_it.string.schars = it->end_charpos;
20321 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20322 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20323 it->bidi_it.string.unibyte = !it->multibyte_p;
20324 it->bidi_it.w = it->w;
20325 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20328 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20330 it->method = GET_FROM_STRETCH;
20331 it->object = prop;
20333 #ifdef HAVE_WINDOW_SYSTEM
20334 else if (IMAGEP (prop))
20336 it->what = IT_IMAGE;
20337 it->image_id = lookup_image (it->f, prop);
20338 it->method = GET_FROM_IMAGE;
20340 #endif /* HAVE_WINDOW_SYSTEM */
20341 else
20343 pop_it (it); /* bogus display property, give up */
20344 return false;
20347 return true;
20350 /* Return the character-property PROP at the current position in IT. */
20352 static Lisp_Object
20353 get_it_property (struct it *it, Lisp_Object prop)
20355 Lisp_Object position, object = it->object;
20357 if (STRINGP (object))
20358 position = make_number (IT_STRING_CHARPOS (*it));
20359 else if (BUFFERP (object))
20361 position = make_number (IT_CHARPOS (*it));
20362 object = it->window;
20364 else
20365 return Qnil;
20367 return Fget_char_property (position, prop, object);
20370 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20372 static void
20373 handle_line_prefix (struct it *it)
20375 Lisp_Object prefix;
20377 if (it->continuation_lines_width > 0)
20379 prefix = get_it_property (it, Qwrap_prefix);
20380 if (NILP (prefix))
20381 prefix = Vwrap_prefix;
20383 else
20385 prefix = get_it_property (it, Qline_prefix);
20386 if (NILP (prefix))
20387 prefix = Vline_prefix;
20389 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20391 /* If the prefix is wider than the window, and we try to wrap
20392 it, it would acquire its own wrap prefix, and so on till the
20393 iterator stack overflows. So, don't wrap the prefix. */
20394 it->line_wrap = TRUNCATE;
20395 it->avoid_cursor_p = true;
20401 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20402 only for R2L lines from display_line and display_string, when they
20403 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20404 the line/string needs to be continued on the next glyph row. */
20405 static void
20406 unproduce_glyphs (struct it *it, int n)
20408 struct glyph *glyph, *end;
20410 eassert (it->glyph_row);
20411 eassert (it->glyph_row->reversed_p);
20412 eassert (it->area == TEXT_AREA);
20413 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20415 if (n > it->glyph_row->used[TEXT_AREA])
20416 n = it->glyph_row->used[TEXT_AREA];
20417 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20418 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20419 for ( ; glyph < end; glyph++)
20420 glyph[-n] = *glyph;
20423 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20424 and ROW->maxpos. */
20425 static void
20426 find_row_edges (struct it *it, struct glyph_row *row,
20427 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20428 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20430 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20431 lines' rows is implemented for bidi-reordered rows. */
20433 /* ROW->minpos is the value of min_pos, the minimal buffer position
20434 we have in ROW, or ROW->start.pos if that is smaller. */
20435 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20436 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20437 else
20438 /* We didn't find buffer positions smaller than ROW->start, or
20439 didn't find _any_ valid buffer positions in any of the glyphs,
20440 so we must trust the iterator's computed positions. */
20441 row->minpos = row->start.pos;
20442 if (max_pos <= 0)
20444 max_pos = CHARPOS (it->current.pos);
20445 max_bpos = BYTEPOS (it->current.pos);
20448 /* Here are the various use-cases for ending the row, and the
20449 corresponding values for ROW->maxpos:
20451 Line ends in a newline from buffer eol_pos + 1
20452 Line is continued from buffer max_pos + 1
20453 Line is truncated on right it->current.pos
20454 Line ends in a newline from string max_pos + 1(*)
20455 (*) + 1 only when line ends in a forward scan
20456 Line is continued from string max_pos
20457 Line is continued from display vector max_pos
20458 Line is entirely from a string min_pos == max_pos
20459 Line is entirely from a display vector min_pos == max_pos
20460 Line that ends at ZV ZV
20462 If you discover other use-cases, please add them here as
20463 appropriate. */
20464 if (row->ends_at_zv_p)
20465 row->maxpos = it->current.pos;
20466 else if (row->used[TEXT_AREA])
20468 bool seen_this_string = false;
20469 struct glyph_row *r1 = row - 1;
20471 /* Did we see the same display string on the previous row? */
20472 if (STRINGP (it->object)
20473 /* this is not the first row */
20474 && row > it->w->desired_matrix->rows
20475 /* previous row is not the header line */
20476 && !r1->mode_line_p
20477 /* previous row also ends in a newline from a string */
20478 && r1->ends_in_newline_from_string_p)
20480 struct glyph *start, *end;
20482 /* Search for the last glyph of the previous row that came
20483 from buffer or string. Depending on whether the row is
20484 L2R or R2L, we need to process it front to back or the
20485 other way round. */
20486 if (!r1->reversed_p)
20488 start = r1->glyphs[TEXT_AREA];
20489 end = start + r1->used[TEXT_AREA];
20490 /* Glyphs inserted by redisplay have nil as their object. */
20491 while (end > start
20492 && NILP ((end - 1)->object)
20493 && (end - 1)->charpos <= 0)
20494 --end;
20495 if (end > start)
20497 if (EQ ((end - 1)->object, it->object))
20498 seen_this_string = true;
20500 else
20501 /* If all the glyphs of the previous row were inserted
20502 by redisplay, it means the previous row was
20503 produced from a single newline, which is only
20504 possible if that newline came from the same string
20505 as the one which produced this ROW. */
20506 seen_this_string = true;
20508 else
20510 end = r1->glyphs[TEXT_AREA] - 1;
20511 start = end + r1->used[TEXT_AREA];
20512 while (end < start
20513 && NILP ((end + 1)->object)
20514 && (end + 1)->charpos <= 0)
20515 ++end;
20516 if (end < start)
20518 if (EQ ((end + 1)->object, it->object))
20519 seen_this_string = true;
20521 else
20522 seen_this_string = true;
20525 /* Take note of each display string that covers a newline only
20526 once, the first time we see it. This is for when a display
20527 string includes more than one newline in it. */
20528 if (row->ends_in_newline_from_string_p && !seen_this_string)
20530 /* If we were scanning the buffer forward when we displayed
20531 the string, we want to account for at least one buffer
20532 position that belongs to this row (position covered by
20533 the display string), so that cursor positioning will
20534 consider this row as a candidate when point is at the end
20535 of the visual line represented by this row. This is not
20536 required when scanning back, because max_pos will already
20537 have a much larger value. */
20538 if (CHARPOS (row->end.pos) > max_pos)
20539 INC_BOTH (max_pos, max_bpos);
20540 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20542 else if (CHARPOS (it->eol_pos) > 0)
20543 SET_TEXT_POS (row->maxpos,
20544 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20545 else if (row->continued_p)
20547 /* If max_pos is different from IT's current position, it
20548 means IT->method does not belong to the display element
20549 at max_pos. However, it also means that the display
20550 element at max_pos was displayed in its entirety on this
20551 line, which is equivalent to saying that the next line
20552 starts at the next buffer position. */
20553 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20554 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20555 else
20557 INC_BOTH (max_pos, max_bpos);
20558 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20561 else if (row->truncated_on_right_p)
20562 /* display_line already called reseat_at_next_visible_line_start,
20563 which puts the iterator at the beginning of the next line, in
20564 the logical order. */
20565 row->maxpos = it->current.pos;
20566 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20567 /* A line that is entirely from a string/image/stretch... */
20568 row->maxpos = row->minpos;
20569 else
20570 emacs_abort ();
20572 else
20573 row->maxpos = it->current.pos;
20576 /* Construct the glyph row IT->glyph_row in the desired matrix of
20577 IT->w from text at the current position of IT. See dispextern.h
20578 for an overview of struct it. Value is true if
20579 IT->glyph_row displays text, as opposed to a line displaying ZV
20580 only. */
20582 static bool
20583 display_line (struct it *it)
20585 struct glyph_row *row = it->glyph_row;
20586 Lisp_Object overlay_arrow_string;
20587 struct it wrap_it;
20588 void *wrap_data = NULL;
20589 bool may_wrap = false;
20590 int wrap_x UNINIT;
20591 int wrap_row_used = -1;
20592 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20593 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20594 int wrap_row_extra_line_spacing UNINIT;
20595 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20596 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20597 int cvpos;
20598 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20599 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20600 bool pending_handle_line_prefix = false;
20602 /* We always start displaying at hpos zero even if hscrolled. */
20603 eassert (it->hpos == 0 && it->current_x == 0);
20605 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20606 >= it->w->desired_matrix->nrows)
20608 it->w->nrows_scale_factor++;
20609 it->f->fonts_changed = true;
20610 return false;
20613 /* Clear the result glyph row and enable it. */
20614 prepare_desired_row (it->w, row, false);
20616 row->y = it->current_y;
20617 row->start = it->start;
20618 row->continuation_lines_width = it->continuation_lines_width;
20619 row->displays_text_p = true;
20620 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20621 it->starts_in_middle_of_char_p = false;
20623 /* Arrange the overlays nicely for our purposes. Usually, we call
20624 display_line on only one line at a time, in which case this
20625 can't really hurt too much, or we call it on lines which appear
20626 one after another in the buffer, in which case all calls to
20627 recenter_overlay_lists but the first will be pretty cheap. */
20628 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20630 /* Move over display elements that are not visible because we are
20631 hscrolled. This may stop at an x-position < IT->first_visible_x
20632 if the first glyph is partially visible or if we hit a line end. */
20633 if (it->current_x < it->first_visible_x)
20635 enum move_it_result move_result;
20637 this_line_min_pos = row->start.pos;
20638 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20639 MOVE_TO_POS | MOVE_TO_X);
20640 /* If we are under a large hscroll, move_it_in_display_line_to
20641 could hit the end of the line without reaching
20642 it->first_visible_x. Pretend that we did reach it. This is
20643 especially important on a TTY, where we will call
20644 extend_face_to_end_of_line, which needs to know how many
20645 blank glyphs to produce. */
20646 if (it->current_x < it->first_visible_x
20647 && (move_result == MOVE_NEWLINE_OR_CR
20648 || move_result == MOVE_POS_MATCH_OR_ZV))
20649 it->current_x = it->first_visible_x;
20651 /* Record the smallest positions seen while we moved over
20652 display elements that are not visible. This is needed by
20653 redisplay_internal for optimizing the case where the cursor
20654 stays inside the same line. The rest of this function only
20655 considers positions that are actually displayed, so
20656 RECORD_MAX_MIN_POS will not otherwise record positions that
20657 are hscrolled to the left of the left edge of the window. */
20658 min_pos = CHARPOS (this_line_min_pos);
20659 min_bpos = BYTEPOS (this_line_min_pos);
20661 else if (it->area == TEXT_AREA)
20663 /* We only do this when not calling move_it_in_display_line_to
20664 above, because that function calls itself handle_line_prefix. */
20665 handle_line_prefix (it);
20667 else
20669 /* Line-prefix and wrap-prefix are always displayed in the text
20670 area. But if this is the first call to display_line after
20671 init_iterator, the iterator might have been set up to write
20672 into a marginal area, e.g. if the line begins with some
20673 display property that writes to the margins. So we need to
20674 wait with the call to handle_line_prefix until whatever
20675 writes to the margin has done its job. */
20676 pending_handle_line_prefix = true;
20679 /* Get the initial row height. This is either the height of the
20680 text hscrolled, if there is any, or zero. */
20681 row->ascent = it->max_ascent;
20682 row->height = it->max_ascent + it->max_descent;
20683 row->phys_ascent = it->max_phys_ascent;
20684 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20685 row->extra_line_spacing = it->max_extra_line_spacing;
20687 /* Utility macro to record max and min buffer positions seen until now. */
20688 #define RECORD_MAX_MIN_POS(IT) \
20689 do \
20691 bool composition_p \
20692 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20693 ptrdiff_t current_pos = \
20694 composition_p ? (IT)->cmp_it.charpos \
20695 : IT_CHARPOS (*(IT)); \
20696 ptrdiff_t current_bpos = \
20697 composition_p ? CHAR_TO_BYTE (current_pos) \
20698 : IT_BYTEPOS (*(IT)); \
20699 if (current_pos < min_pos) \
20701 min_pos = current_pos; \
20702 min_bpos = current_bpos; \
20704 if (IT_CHARPOS (*it) > max_pos) \
20706 max_pos = IT_CHARPOS (*it); \
20707 max_bpos = IT_BYTEPOS (*it); \
20710 while (false)
20712 /* Loop generating characters. The loop is left with IT on the next
20713 character to display. */
20714 while (true)
20716 int n_glyphs_before, hpos_before, x_before;
20717 int x, nglyphs;
20718 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20720 /* Retrieve the next thing to display. Value is false if end of
20721 buffer reached. */
20722 if (!get_next_display_element (it))
20724 /* Maybe add a space at the end of this line that is used to
20725 display the cursor there under X. Set the charpos of the
20726 first glyph of blank lines not corresponding to any text
20727 to -1. */
20728 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20729 row->exact_window_width_line_p = true;
20730 else if ((append_space_for_newline (it, true)
20731 && row->used[TEXT_AREA] == 1)
20732 || row->used[TEXT_AREA] == 0)
20734 row->glyphs[TEXT_AREA]->charpos = -1;
20735 row->displays_text_p = false;
20737 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20738 && (!MINI_WINDOW_P (it->w)
20739 || (minibuf_level && EQ (it->window, minibuf_window))))
20740 row->indicate_empty_line_p = true;
20743 it->continuation_lines_width = 0;
20744 row->ends_at_zv_p = true;
20745 /* A row that displays right-to-left text must always have
20746 its last face extended all the way to the end of line,
20747 even if this row ends in ZV, because we still write to
20748 the screen left to right. We also need to extend the
20749 last face if the default face is remapped to some
20750 different face, otherwise the functions that clear
20751 portions of the screen will clear with the default face's
20752 background color. */
20753 if (row->reversed_p
20754 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20755 extend_face_to_end_of_line (it);
20756 break;
20759 /* Now, get the metrics of what we want to display. This also
20760 generates glyphs in `row' (which is IT->glyph_row). */
20761 n_glyphs_before = row->used[TEXT_AREA];
20762 x = it->current_x;
20764 /* Remember the line height so far in case the next element doesn't
20765 fit on the line. */
20766 if (it->line_wrap != TRUNCATE)
20768 ascent = it->max_ascent;
20769 descent = it->max_descent;
20770 phys_ascent = it->max_phys_ascent;
20771 phys_descent = it->max_phys_descent;
20773 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20775 if (IT_DISPLAYING_WHITESPACE (it))
20776 may_wrap = true;
20777 else if (may_wrap)
20779 SAVE_IT (wrap_it, *it, wrap_data);
20780 wrap_x = x;
20781 wrap_row_used = row->used[TEXT_AREA];
20782 wrap_row_ascent = row->ascent;
20783 wrap_row_height = row->height;
20784 wrap_row_phys_ascent = row->phys_ascent;
20785 wrap_row_phys_height = row->phys_height;
20786 wrap_row_extra_line_spacing = row->extra_line_spacing;
20787 wrap_row_min_pos = min_pos;
20788 wrap_row_min_bpos = min_bpos;
20789 wrap_row_max_pos = max_pos;
20790 wrap_row_max_bpos = max_bpos;
20791 may_wrap = false;
20796 PRODUCE_GLYPHS (it);
20798 /* If this display element was in marginal areas, continue with
20799 the next one. */
20800 if (it->area != TEXT_AREA)
20802 row->ascent = max (row->ascent, it->max_ascent);
20803 row->height = max (row->height, it->max_ascent + it->max_descent);
20804 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20805 row->phys_height = max (row->phys_height,
20806 it->max_phys_ascent + it->max_phys_descent);
20807 row->extra_line_spacing = max (row->extra_line_spacing,
20808 it->max_extra_line_spacing);
20809 set_iterator_to_next (it, true);
20810 /* If we didn't handle the line/wrap prefix above, and the
20811 call to set_iterator_to_next just switched to TEXT_AREA,
20812 process the prefix now. */
20813 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20815 pending_handle_line_prefix = false;
20816 handle_line_prefix (it);
20818 continue;
20821 /* Does the display element fit on the line? If we truncate
20822 lines, we should draw past the right edge of the window. If
20823 we don't truncate, we want to stop so that we can display the
20824 continuation glyph before the right margin. If lines are
20825 continued, there are two possible strategies for characters
20826 resulting in more than 1 glyph (e.g. tabs): Display as many
20827 glyphs as possible in this line and leave the rest for the
20828 continuation line, or display the whole element in the next
20829 line. Original redisplay did the former, so we do it also. */
20830 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20831 hpos_before = it->hpos;
20832 x_before = x;
20834 if (/* Not a newline. */
20835 nglyphs > 0
20836 /* Glyphs produced fit entirely in the line. */
20837 && it->current_x < it->last_visible_x)
20839 it->hpos += nglyphs;
20840 row->ascent = max (row->ascent, it->max_ascent);
20841 row->height = max (row->height, it->max_ascent + it->max_descent);
20842 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20843 row->phys_height = max (row->phys_height,
20844 it->max_phys_ascent + it->max_phys_descent);
20845 row->extra_line_spacing = max (row->extra_line_spacing,
20846 it->max_extra_line_spacing);
20847 if (it->current_x - it->pixel_width < it->first_visible_x
20848 /* In R2L rows, we arrange in extend_face_to_end_of_line
20849 to add a right offset to the line, by a suitable
20850 change to the stretch glyph that is the leftmost
20851 glyph of the line. */
20852 && !row->reversed_p)
20853 row->x = x - it->first_visible_x;
20854 /* Record the maximum and minimum buffer positions seen so
20855 far in glyphs that will be displayed by this row. */
20856 if (it->bidi_p)
20857 RECORD_MAX_MIN_POS (it);
20859 else
20861 int i, new_x;
20862 struct glyph *glyph;
20864 for (i = 0; i < nglyphs; ++i, x = new_x)
20866 /* Identify the glyphs added by the last call to
20867 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20868 the previous glyphs. */
20869 if (!row->reversed_p)
20870 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20871 else
20872 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20873 new_x = x + glyph->pixel_width;
20875 if (/* Lines are continued. */
20876 it->line_wrap != TRUNCATE
20877 && (/* Glyph doesn't fit on the line. */
20878 new_x > it->last_visible_x
20879 /* Or it fits exactly on a window system frame. */
20880 || (new_x == it->last_visible_x
20881 && FRAME_WINDOW_P (it->f)
20882 && (row->reversed_p
20883 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20884 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20886 /* End of a continued line. */
20888 if (it->hpos == 0
20889 || (new_x == it->last_visible_x
20890 && FRAME_WINDOW_P (it->f)
20891 && (row->reversed_p
20892 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20893 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20895 /* Current glyph is the only one on the line or
20896 fits exactly on the line. We must continue
20897 the line because we can't draw the cursor
20898 after the glyph. */
20899 row->continued_p = true;
20900 it->current_x = new_x;
20901 it->continuation_lines_width += new_x;
20902 ++it->hpos;
20903 if (i == nglyphs - 1)
20905 /* If line-wrap is on, check if a previous
20906 wrap point was found. */
20907 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20908 && wrap_row_used > 0
20909 /* Even if there is a previous wrap
20910 point, continue the line here as
20911 usual, if (i) the previous character
20912 was a space or tab AND (ii) the
20913 current character is not. */
20914 && (!may_wrap
20915 || IT_DISPLAYING_WHITESPACE (it)))
20916 goto back_to_wrap;
20918 /* Record the maximum and minimum buffer
20919 positions seen so far in glyphs that will be
20920 displayed by this row. */
20921 if (it->bidi_p)
20922 RECORD_MAX_MIN_POS (it);
20923 set_iterator_to_next (it, true);
20924 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20926 if (!get_next_display_element (it))
20928 row->exact_window_width_line_p = true;
20929 it->continuation_lines_width = 0;
20930 row->continued_p = false;
20931 row->ends_at_zv_p = true;
20933 else if (ITERATOR_AT_END_OF_LINE_P (it))
20935 row->continued_p = false;
20936 row->exact_window_width_line_p = true;
20938 /* If line-wrap is on, check if a
20939 previous wrap point was found. */
20940 else if (wrap_row_used > 0
20941 /* Even if there is a previous wrap
20942 point, continue the line here as
20943 usual, if (i) the previous character
20944 was a space or tab AND (ii) the
20945 current character is not. */
20946 && (!may_wrap
20947 || IT_DISPLAYING_WHITESPACE (it)))
20948 goto back_to_wrap;
20952 else if (it->bidi_p)
20953 RECORD_MAX_MIN_POS (it);
20954 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20955 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20956 extend_face_to_end_of_line (it);
20958 else if (CHAR_GLYPH_PADDING_P (*glyph)
20959 && !FRAME_WINDOW_P (it->f))
20961 /* A padding glyph that doesn't fit on this line.
20962 This means the whole character doesn't fit
20963 on the line. */
20964 if (row->reversed_p)
20965 unproduce_glyphs (it, row->used[TEXT_AREA]
20966 - n_glyphs_before);
20967 row->used[TEXT_AREA] = n_glyphs_before;
20969 /* Fill the rest of the row with continuation
20970 glyphs like in 20.x. */
20971 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20972 < row->glyphs[1 + TEXT_AREA])
20973 produce_special_glyphs (it, IT_CONTINUATION);
20975 row->continued_p = true;
20976 it->current_x = x_before;
20977 it->continuation_lines_width += x_before;
20979 /* Restore the height to what it was before the
20980 element not fitting on the line. */
20981 it->max_ascent = ascent;
20982 it->max_descent = descent;
20983 it->max_phys_ascent = phys_ascent;
20984 it->max_phys_descent = phys_descent;
20985 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20986 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20987 extend_face_to_end_of_line (it);
20989 else if (wrap_row_used > 0)
20991 back_to_wrap:
20992 if (row->reversed_p)
20993 unproduce_glyphs (it,
20994 row->used[TEXT_AREA] - wrap_row_used);
20995 RESTORE_IT (it, &wrap_it, wrap_data);
20996 it->continuation_lines_width += wrap_x;
20997 row->used[TEXT_AREA] = wrap_row_used;
20998 row->ascent = wrap_row_ascent;
20999 row->height = wrap_row_height;
21000 row->phys_ascent = wrap_row_phys_ascent;
21001 row->phys_height = wrap_row_phys_height;
21002 row->extra_line_spacing = wrap_row_extra_line_spacing;
21003 min_pos = wrap_row_min_pos;
21004 min_bpos = wrap_row_min_bpos;
21005 max_pos = wrap_row_max_pos;
21006 max_bpos = wrap_row_max_bpos;
21007 row->continued_p = true;
21008 row->ends_at_zv_p = false;
21009 row->exact_window_width_line_p = false;
21010 it->continuation_lines_width += x;
21012 /* Make sure that a non-default face is extended
21013 up to the right margin of the window. */
21014 extend_face_to_end_of_line (it);
21016 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
21018 /* A TAB that extends past the right edge of the
21019 window. This produces a single glyph on
21020 window system frames. We leave the glyph in
21021 this row and let it fill the row, but don't
21022 consume the TAB. */
21023 if ((row->reversed_p
21024 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21025 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21026 produce_special_glyphs (it, IT_CONTINUATION);
21027 it->continuation_lines_width += it->last_visible_x;
21028 row->ends_in_middle_of_char_p = true;
21029 row->continued_p = true;
21030 glyph->pixel_width = it->last_visible_x - x;
21031 it->starts_in_middle_of_char_p = true;
21032 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21033 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21034 extend_face_to_end_of_line (it);
21036 else
21038 /* Something other than a TAB that draws past
21039 the right edge of the window. Restore
21040 positions to values before the element. */
21041 if (row->reversed_p)
21042 unproduce_glyphs (it, row->used[TEXT_AREA]
21043 - (n_glyphs_before + i));
21044 row->used[TEXT_AREA] = n_glyphs_before + i;
21046 /* Display continuation glyphs. */
21047 it->current_x = x_before;
21048 it->continuation_lines_width += x;
21049 if (!FRAME_WINDOW_P (it->f)
21050 || (row->reversed_p
21051 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21052 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21053 produce_special_glyphs (it, IT_CONTINUATION);
21054 row->continued_p = true;
21056 extend_face_to_end_of_line (it);
21058 if (nglyphs > 1 && i > 0)
21060 row->ends_in_middle_of_char_p = true;
21061 it->starts_in_middle_of_char_p = true;
21064 /* Restore the height to what it was before the
21065 element not fitting on the line. */
21066 it->max_ascent = ascent;
21067 it->max_descent = descent;
21068 it->max_phys_ascent = phys_ascent;
21069 it->max_phys_descent = phys_descent;
21072 break;
21074 else if (new_x > it->first_visible_x)
21076 /* Increment number of glyphs actually displayed. */
21077 ++it->hpos;
21079 /* Record the maximum and minimum buffer positions
21080 seen so far in glyphs that will be displayed by
21081 this row. */
21082 if (it->bidi_p)
21083 RECORD_MAX_MIN_POS (it);
21085 if (x < it->first_visible_x && !row->reversed_p)
21086 /* Glyph is partially visible, i.e. row starts at
21087 negative X position. Don't do that in R2L
21088 rows, where we arrange to add a right offset to
21089 the line in extend_face_to_end_of_line, by a
21090 suitable change to the stretch glyph that is
21091 the leftmost glyph of the line. */
21092 row->x = x - it->first_visible_x;
21093 /* When the last glyph of an R2L row only fits
21094 partially on the line, we need to set row->x to a
21095 negative offset, so that the leftmost glyph is
21096 the one that is partially visible. But if we are
21097 going to produce the truncation glyph, this will
21098 be taken care of in produce_special_glyphs. */
21099 if (row->reversed_p
21100 && new_x > it->last_visible_x
21101 && !(it->line_wrap == TRUNCATE
21102 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21104 eassert (FRAME_WINDOW_P (it->f));
21105 row->x = it->last_visible_x - new_x;
21108 else
21110 /* Glyph is completely off the left margin of the
21111 window. This should not happen because of the
21112 move_it_in_display_line at the start of this
21113 function, unless the text display area of the
21114 window is empty. */
21115 eassert (it->first_visible_x <= it->last_visible_x);
21118 /* Even if this display element produced no glyphs at all,
21119 we want to record its position. */
21120 if (it->bidi_p && nglyphs == 0)
21121 RECORD_MAX_MIN_POS (it);
21123 row->ascent = max (row->ascent, it->max_ascent);
21124 row->height = max (row->height, it->max_ascent + it->max_descent);
21125 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21126 row->phys_height = max (row->phys_height,
21127 it->max_phys_ascent + it->max_phys_descent);
21128 row->extra_line_spacing = max (row->extra_line_spacing,
21129 it->max_extra_line_spacing);
21131 /* End of this display line if row is continued. */
21132 if (row->continued_p || row->ends_at_zv_p)
21133 break;
21136 at_end_of_line:
21137 /* Is this a line end? If yes, we're also done, after making
21138 sure that a non-default face is extended up to the right
21139 margin of the window. */
21140 if (ITERATOR_AT_END_OF_LINE_P (it))
21142 int used_before = row->used[TEXT_AREA];
21144 row->ends_in_newline_from_string_p = STRINGP (it->object);
21146 /* Add a space at the end of the line that is used to
21147 display the cursor there. */
21148 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21149 append_space_for_newline (it, false);
21151 /* Extend the face to the end of the line. */
21152 extend_face_to_end_of_line (it);
21154 /* Make sure we have the position. */
21155 if (used_before == 0)
21156 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21158 /* Record the position of the newline, for use in
21159 find_row_edges. */
21160 it->eol_pos = it->current.pos;
21162 /* Consume the line end. This skips over invisible lines. */
21163 set_iterator_to_next (it, true);
21164 it->continuation_lines_width = 0;
21165 break;
21168 /* Proceed with next display element. Note that this skips
21169 over lines invisible because of selective display. */
21170 set_iterator_to_next (it, true);
21172 /* If we truncate lines, we are done when the last displayed
21173 glyphs reach past the right margin of the window. */
21174 if (it->line_wrap == TRUNCATE
21175 && ((FRAME_WINDOW_P (it->f)
21176 /* Images are preprocessed in produce_image_glyph such
21177 that they are cropped at the right edge of the
21178 window, so an image glyph will always end exactly at
21179 last_visible_x, even if there's no right fringe. */
21180 && ((row->reversed_p
21181 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21182 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21183 || it->what == IT_IMAGE))
21184 ? (it->current_x >= it->last_visible_x)
21185 : (it->current_x > it->last_visible_x)))
21187 /* Maybe add truncation glyphs. */
21188 if (!FRAME_WINDOW_P (it->f)
21189 || (row->reversed_p
21190 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21191 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21193 int i, n;
21195 if (!row->reversed_p)
21197 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21198 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21199 break;
21201 else
21203 for (i = 0; i < row->used[TEXT_AREA]; i++)
21204 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21205 break;
21206 /* Remove any padding glyphs at the front of ROW, to
21207 make room for the truncation glyphs we will be
21208 adding below. The loop below always inserts at
21209 least one truncation glyph, so also remove the
21210 last glyph added to ROW. */
21211 unproduce_glyphs (it, i + 1);
21212 /* Adjust i for the loop below. */
21213 i = row->used[TEXT_AREA] - (i + 1);
21216 /* produce_special_glyphs overwrites the last glyph, so
21217 we don't want that if we want to keep that last
21218 glyph, which means it's an image. */
21219 if (it->current_x > it->last_visible_x)
21221 it->current_x = x_before;
21222 if (!FRAME_WINDOW_P (it->f))
21224 for (n = row->used[TEXT_AREA]; i < n; ++i)
21226 row->used[TEXT_AREA] = i;
21227 produce_special_glyphs (it, IT_TRUNCATION);
21230 else
21232 row->used[TEXT_AREA] = i;
21233 produce_special_glyphs (it, IT_TRUNCATION);
21235 it->hpos = hpos_before;
21238 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21240 /* Don't truncate if we can overflow newline into fringe. */
21241 if (!get_next_display_element (it))
21243 it->continuation_lines_width = 0;
21244 row->ends_at_zv_p = true;
21245 row->exact_window_width_line_p = true;
21246 break;
21248 if (ITERATOR_AT_END_OF_LINE_P (it))
21250 row->exact_window_width_line_p = true;
21251 goto at_end_of_line;
21253 it->current_x = x_before;
21254 it->hpos = hpos_before;
21257 row->truncated_on_right_p = true;
21258 it->continuation_lines_width = 0;
21259 reseat_at_next_visible_line_start (it, false);
21260 /* We insist below that IT's position be at ZV because in
21261 bidi-reordered lines the character at visible line start
21262 might not be the character that follows the newline in
21263 the logical order. */
21264 if (IT_BYTEPOS (*it) > BEG_BYTE)
21265 row->ends_at_zv_p =
21266 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21267 else
21268 row->ends_at_zv_p = false;
21269 break;
21273 if (wrap_data)
21274 bidi_unshelve_cache (wrap_data, true);
21276 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21277 at the left window margin. */
21278 if (it->first_visible_x
21279 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21281 if (!FRAME_WINDOW_P (it->f)
21282 || (((row->reversed_p
21283 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21284 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21285 /* Don't let insert_left_trunc_glyphs overwrite the
21286 first glyph of the row if it is an image. */
21287 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21288 insert_left_trunc_glyphs (it);
21289 row->truncated_on_left_p = true;
21292 /* Remember the position at which this line ends.
21294 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21295 cannot be before the call to find_row_edges below, since that is
21296 where these positions are determined. */
21297 row->end = it->current;
21298 if (!it->bidi_p)
21300 row->minpos = row->start.pos;
21301 row->maxpos = row->end.pos;
21303 else
21305 /* ROW->minpos and ROW->maxpos must be the smallest and
21306 `1 + the largest' buffer positions in ROW. But if ROW was
21307 bidi-reordered, these two positions can be anywhere in the
21308 row, so we must determine them now. */
21309 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21312 /* If the start of this line is the overlay arrow-position, then
21313 mark this glyph row as the one containing the overlay arrow.
21314 This is clearly a mess with variable size fonts. It would be
21315 better to let it be displayed like cursors under X. */
21316 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21317 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21318 !NILP (overlay_arrow_string)))
21320 /* Overlay arrow in window redisplay is a fringe bitmap. */
21321 if (STRINGP (overlay_arrow_string))
21323 struct glyph_row *arrow_row
21324 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21325 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21326 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21327 struct glyph *p = row->glyphs[TEXT_AREA];
21328 struct glyph *p2, *end;
21330 /* Copy the arrow glyphs. */
21331 while (glyph < arrow_end)
21332 *p++ = *glyph++;
21334 /* Throw away padding glyphs. */
21335 p2 = p;
21336 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21337 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21338 ++p2;
21339 if (p2 > p)
21341 while (p2 < end)
21342 *p++ = *p2++;
21343 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21346 else
21348 eassert (INTEGERP (overlay_arrow_string));
21349 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21351 overlay_arrow_seen = true;
21354 /* Highlight trailing whitespace. */
21355 if (!NILP (Vshow_trailing_whitespace))
21356 highlight_trailing_whitespace (it->f, it->glyph_row);
21358 /* Compute pixel dimensions of this line. */
21359 compute_line_metrics (it);
21361 /* Implementation note: No changes in the glyphs of ROW or in their
21362 faces can be done past this point, because compute_line_metrics
21363 computes ROW's hash value and stores it within the glyph_row
21364 structure. */
21366 /* Record whether this row ends inside an ellipsis. */
21367 row->ends_in_ellipsis_p
21368 = (it->method == GET_FROM_DISPLAY_VECTOR
21369 && it->ellipsis_p);
21371 /* Save fringe bitmaps in this row. */
21372 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21373 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21374 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21375 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21377 it->left_user_fringe_bitmap = 0;
21378 it->left_user_fringe_face_id = 0;
21379 it->right_user_fringe_bitmap = 0;
21380 it->right_user_fringe_face_id = 0;
21382 /* Maybe set the cursor. */
21383 cvpos = it->w->cursor.vpos;
21384 if ((cvpos < 0
21385 /* In bidi-reordered rows, keep checking for proper cursor
21386 position even if one has been found already, because buffer
21387 positions in such rows change non-linearly with ROW->VPOS,
21388 when a line is continued. One exception: when we are at ZV,
21389 display cursor on the first suitable glyph row, since all
21390 the empty rows after that also have their position set to ZV. */
21391 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21392 lines' rows is implemented for bidi-reordered rows. */
21393 || (it->bidi_p
21394 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21395 && PT >= MATRIX_ROW_START_CHARPOS (row)
21396 && PT <= MATRIX_ROW_END_CHARPOS (row)
21397 && cursor_row_p (row))
21398 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21400 /* Prepare for the next line. This line starts horizontally at (X
21401 HPOS) = (0 0). Vertical positions are incremented. As a
21402 convenience for the caller, IT->glyph_row is set to the next
21403 row to be used. */
21404 it->current_x = it->hpos = 0;
21405 it->current_y += row->height;
21406 SET_TEXT_POS (it->eol_pos, 0, 0);
21407 ++it->vpos;
21408 ++it->glyph_row;
21409 /* The next row should by default use the same value of the
21410 reversed_p flag as this one. set_iterator_to_next decides when
21411 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21412 the flag accordingly. */
21413 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21414 it->glyph_row->reversed_p = row->reversed_p;
21415 it->start = row->end;
21416 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21418 #undef RECORD_MAX_MIN_POS
21421 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21422 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21423 doc: /* Return paragraph direction at point in BUFFER.
21424 Value is either `left-to-right' or `right-to-left'.
21425 If BUFFER is omitted or nil, it defaults to the current buffer.
21427 Paragraph direction determines how the text in the paragraph is displayed.
21428 In left-to-right paragraphs, text begins at the left margin of the window
21429 and the reading direction is generally left to right. In right-to-left
21430 paragraphs, text begins at the right margin and is read from right to left.
21432 See also `bidi-paragraph-direction'. */)
21433 (Lisp_Object buffer)
21435 struct buffer *buf = current_buffer;
21436 struct buffer *old = buf;
21438 if (! NILP (buffer))
21440 CHECK_BUFFER (buffer);
21441 buf = XBUFFER (buffer);
21444 if (NILP (BVAR (buf, bidi_display_reordering))
21445 || NILP (BVAR (buf, enable_multibyte_characters))
21446 /* When we are loading loadup.el, the character property tables
21447 needed for bidi iteration are not yet available. */
21448 || redisplay__inhibit_bidi)
21449 return Qleft_to_right;
21450 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21451 return BVAR (buf, bidi_paragraph_direction);
21452 else
21454 /* Determine the direction from buffer text. We could try to
21455 use current_matrix if it is up to date, but this seems fast
21456 enough as it is. */
21457 struct bidi_it itb;
21458 ptrdiff_t pos = BUF_PT (buf);
21459 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21460 int c;
21461 void *itb_data = bidi_shelve_cache ();
21463 set_buffer_temp (buf);
21464 /* bidi_paragraph_init finds the base direction of the paragraph
21465 by searching forward from paragraph start. We need the base
21466 direction of the current or _previous_ paragraph, so we need
21467 to make sure we are within that paragraph. To that end, find
21468 the previous non-empty line. */
21469 if (pos >= ZV && pos > BEGV)
21470 DEC_BOTH (pos, bytepos);
21471 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21472 if (fast_looking_at (trailing_white_space,
21473 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21475 while ((c = FETCH_BYTE (bytepos)) == '\n'
21476 || c == ' ' || c == '\t' || c == '\f')
21478 if (bytepos <= BEGV_BYTE)
21479 break;
21480 bytepos--;
21481 pos--;
21483 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21484 bytepos--;
21486 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21487 itb.paragraph_dir = NEUTRAL_DIR;
21488 itb.string.s = NULL;
21489 itb.string.lstring = Qnil;
21490 itb.string.bufpos = 0;
21491 itb.string.from_disp_str = false;
21492 itb.string.unibyte = false;
21493 /* We have no window to use here for ignoring window-specific
21494 overlays. Using NULL for window pointer will cause
21495 compute_display_string_pos to use the current buffer. */
21496 itb.w = NULL;
21497 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21498 bidi_unshelve_cache (itb_data, false);
21499 set_buffer_temp (old);
21500 switch (itb.paragraph_dir)
21502 case L2R:
21503 return Qleft_to_right;
21504 break;
21505 case R2L:
21506 return Qright_to_left;
21507 break;
21508 default:
21509 emacs_abort ();
21514 DEFUN ("bidi-find-overridden-directionality",
21515 Fbidi_find_overridden_directionality,
21516 Sbidi_find_overridden_directionality, 2, 3, 0,
21517 doc: /* Return position between FROM and TO where directionality was overridden.
21519 This function returns the first character position in the specified
21520 region of OBJECT where there is a character whose `bidi-class' property
21521 is `L', but which was forced to display as `R' by a directional
21522 override, and likewise with characters whose `bidi-class' is `R'
21523 or `AL' that were forced to display as `L'.
21525 If no such character is found, the function returns nil.
21527 OBJECT is a Lisp string or buffer to search for overridden
21528 directionality, and defaults to the current buffer if nil or omitted.
21529 OBJECT can also be a window, in which case the function will search
21530 the buffer displayed in that window. Passing the window instead of
21531 a buffer is preferable when the buffer is displayed in some window,
21532 because this function will then be able to correctly account for
21533 window-specific overlays, which can affect the results.
21535 Strong directional characters `L', `R', and `AL' can have their
21536 intrinsic directionality overridden by directional override
21537 control characters RLO (u+202e) and LRO (u+202d). See the
21538 function `get-char-code-property' for a way to inquire about
21539 the `bidi-class' property of a character. */)
21540 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21542 struct buffer *buf = current_buffer;
21543 struct buffer *old = buf;
21544 struct window *w = NULL;
21545 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21546 struct bidi_it itb;
21547 ptrdiff_t from_pos, to_pos, from_bpos;
21548 void *itb_data;
21550 if (!NILP (object))
21552 if (BUFFERP (object))
21553 buf = XBUFFER (object);
21554 else if (WINDOWP (object))
21556 w = decode_live_window (object);
21557 buf = XBUFFER (w->contents);
21558 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21560 else
21561 CHECK_STRING (object);
21564 if (STRINGP (object))
21566 /* Characters in unibyte strings are always treated by bidi.c as
21567 strong LTR. */
21568 if (!STRING_MULTIBYTE (object)
21569 /* When we are loading loadup.el, the character property
21570 tables needed for bidi iteration are not yet
21571 available. */
21572 || redisplay__inhibit_bidi)
21573 return Qnil;
21575 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21576 if (from_pos >= SCHARS (object))
21577 return Qnil;
21579 /* Set up the bidi iterator. */
21580 itb_data = bidi_shelve_cache ();
21581 itb.paragraph_dir = NEUTRAL_DIR;
21582 itb.string.lstring = object;
21583 itb.string.s = NULL;
21584 itb.string.schars = SCHARS (object);
21585 itb.string.bufpos = 0;
21586 itb.string.from_disp_str = false;
21587 itb.string.unibyte = false;
21588 itb.w = w;
21589 bidi_init_it (0, 0, frame_window_p, &itb);
21591 else
21593 /* Nothing this fancy can happen in unibyte buffers, or in a
21594 buffer that disabled reordering, or if FROM is at EOB. */
21595 if (NILP (BVAR (buf, bidi_display_reordering))
21596 || NILP (BVAR (buf, enable_multibyte_characters))
21597 /* When we are loading loadup.el, the character property
21598 tables needed for bidi iteration are not yet
21599 available. */
21600 || redisplay__inhibit_bidi)
21601 return Qnil;
21603 set_buffer_temp (buf);
21604 validate_region (&from, &to);
21605 from_pos = XINT (from);
21606 to_pos = XINT (to);
21607 if (from_pos >= ZV)
21608 return Qnil;
21610 /* Set up the bidi iterator. */
21611 itb_data = bidi_shelve_cache ();
21612 from_bpos = CHAR_TO_BYTE (from_pos);
21613 if (from_pos == BEGV)
21615 itb.charpos = BEGV;
21616 itb.bytepos = BEGV_BYTE;
21618 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21620 itb.charpos = from_pos;
21621 itb.bytepos = from_bpos;
21623 else
21624 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21625 -1, &itb.bytepos);
21626 itb.paragraph_dir = NEUTRAL_DIR;
21627 itb.string.s = NULL;
21628 itb.string.lstring = Qnil;
21629 itb.string.bufpos = 0;
21630 itb.string.from_disp_str = false;
21631 itb.string.unibyte = false;
21632 itb.w = w;
21633 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21636 ptrdiff_t found;
21637 do {
21638 /* For the purposes of this function, the actual base direction of
21639 the paragraph doesn't matter, so just set it to L2R. */
21640 bidi_paragraph_init (L2R, &itb, false);
21641 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21643 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21645 bidi_unshelve_cache (itb_data, false);
21646 set_buffer_temp (old);
21648 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21651 DEFUN ("move-point-visually", Fmove_point_visually,
21652 Smove_point_visually, 1, 1, 0,
21653 doc: /* Move point in the visual order in the specified DIRECTION.
21654 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21655 left.
21657 Value is the new character position of point. */)
21658 (Lisp_Object direction)
21660 struct window *w = XWINDOW (selected_window);
21661 struct buffer *b = XBUFFER (w->contents);
21662 struct glyph_row *row;
21663 int dir;
21664 Lisp_Object paragraph_dir;
21666 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21667 (!(ROW)->continued_p \
21668 && NILP ((GLYPH)->object) \
21669 && (GLYPH)->type == CHAR_GLYPH \
21670 && (GLYPH)->u.ch == ' ' \
21671 && (GLYPH)->charpos >= 0 \
21672 && !(GLYPH)->avoid_cursor_p)
21674 CHECK_NUMBER (direction);
21675 dir = XINT (direction);
21676 if (dir > 0)
21677 dir = 1;
21678 else
21679 dir = -1;
21681 /* If current matrix is up-to-date, we can use the information
21682 recorded in the glyphs, at least as long as the goal is on the
21683 screen. */
21684 if (w->window_end_valid
21685 && !windows_or_buffers_changed
21686 && b
21687 && !b->clip_changed
21688 && !b->prevent_redisplay_optimizations_p
21689 && !window_outdated (w)
21690 /* We rely below on the cursor coordinates to be up to date, but
21691 we cannot trust them if some command moved point since the
21692 last complete redisplay. */
21693 && w->last_point == BUF_PT (b)
21694 && w->cursor.vpos >= 0
21695 && w->cursor.vpos < w->current_matrix->nrows
21696 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21698 struct glyph *g = row->glyphs[TEXT_AREA];
21699 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21700 struct glyph *gpt = g + w->cursor.hpos;
21702 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21704 if (BUFFERP (g->object) && g->charpos != PT)
21706 SET_PT (g->charpos);
21707 w->cursor.vpos = -1;
21708 return make_number (PT);
21710 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21712 ptrdiff_t new_pos;
21714 if (BUFFERP (gpt->object))
21716 new_pos = PT;
21717 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21718 new_pos += (row->reversed_p ? -dir : dir);
21719 else
21720 new_pos -= (row->reversed_p ? -dir : dir);
21722 else if (BUFFERP (g->object))
21723 new_pos = g->charpos;
21724 else
21725 break;
21726 SET_PT (new_pos);
21727 w->cursor.vpos = -1;
21728 return make_number (PT);
21730 else if (ROW_GLYPH_NEWLINE_P (row, g))
21732 /* Glyphs inserted at the end of a non-empty line for
21733 positioning the cursor have zero charpos, so we must
21734 deduce the value of point by other means. */
21735 if (g->charpos > 0)
21736 SET_PT (g->charpos);
21737 else if (row->ends_at_zv_p && PT != ZV)
21738 SET_PT (ZV);
21739 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21740 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21741 else
21742 break;
21743 w->cursor.vpos = -1;
21744 return make_number (PT);
21747 if (g == e || NILP (g->object))
21749 if (row->truncated_on_left_p || row->truncated_on_right_p)
21750 goto simulate_display;
21751 if (!row->reversed_p)
21752 row += dir;
21753 else
21754 row -= dir;
21755 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21756 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21757 goto simulate_display;
21759 if (dir > 0)
21761 if (row->reversed_p && !row->continued_p)
21763 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21764 w->cursor.vpos = -1;
21765 return make_number (PT);
21767 g = row->glyphs[TEXT_AREA];
21768 e = g + row->used[TEXT_AREA];
21769 for ( ; g < e; g++)
21771 if (BUFFERP (g->object)
21772 /* Empty lines have only one glyph, which stands
21773 for the newline, and whose charpos is the
21774 buffer position of the newline. */
21775 || ROW_GLYPH_NEWLINE_P (row, g)
21776 /* When the buffer ends in a newline, the line at
21777 EOB also has one glyph, but its charpos is -1. */
21778 || (row->ends_at_zv_p
21779 && !row->reversed_p
21780 && NILP (g->object)
21781 && g->type == CHAR_GLYPH
21782 && g->u.ch == ' '))
21784 if (g->charpos > 0)
21785 SET_PT (g->charpos);
21786 else if (!row->reversed_p
21787 && row->ends_at_zv_p
21788 && PT != ZV)
21789 SET_PT (ZV);
21790 else
21791 continue;
21792 w->cursor.vpos = -1;
21793 return make_number (PT);
21797 else
21799 if (!row->reversed_p && !row->continued_p)
21801 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21802 w->cursor.vpos = -1;
21803 return make_number (PT);
21805 e = row->glyphs[TEXT_AREA];
21806 g = e + row->used[TEXT_AREA] - 1;
21807 for ( ; g >= e; g--)
21809 if (BUFFERP (g->object)
21810 || (ROW_GLYPH_NEWLINE_P (row, g)
21811 && g->charpos > 0)
21812 /* Empty R2L lines on GUI frames have the buffer
21813 position of the newline stored in the stretch
21814 glyph. */
21815 || g->type == STRETCH_GLYPH
21816 || (row->ends_at_zv_p
21817 && row->reversed_p
21818 && NILP (g->object)
21819 && g->type == CHAR_GLYPH
21820 && g->u.ch == ' '))
21822 if (g->charpos > 0)
21823 SET_PT (g->charpos);
21824 else if (row->reversed_p
21825 && row->ends_at_zv_p
21826 && PT != ZV)
21827 SET_PT (ZV);
21828 else
21829 continue;
21830 w->cursor.vpos = -1;
21831 return make_number (PT);
21838 simulate_display:
21840 /* If we wind up here, we failed to move by using the glyphs, so we
21841 need to simulate display instead. */
21843 if (b)
21844 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21845 else
21846 paragraph_dir = Qleft_to_right;
21847 if (EQ (paragraph_dir, Qright_to_left))
21848 dir = -dir;
21849 if (PT <= BEGV && dir < 0)
21850 xsignal0 (Qbeginning_of_buffer);
21851 else if (PT >= ZV && dir > 0)
21852 xsignal0 (Qend_of_buffer);
21853 else
21855 struct text_pos pt;
21856 struct it it;
21857 int pt_x, target_x, pixel_width, pt_vpos;
21858 bool at_eol_p;
21859 bool overshoot_expected = false;
21860 bool target_is_eol_p = false;
21862 /* Setup the arena. */
21863 SET_TEXT_POS (pt, PT, PT_BYTE);
21864 start_display (&it, w, pt);
21865 /* When lines are truncated, we could be called with point
21866 outside of the windows edges, in which case move_it_*
21867 functions either prematurely stop at window's edge or jump to
21868 the next screen line, whereas we rely below on our ability to
21869 reach point, in order to start from its X coordinate. So we
21870 need to disregard the window's horizontal extent in that case. */
21871 if (it.line_wrap == TRUNCATE)
21872 it.last_visible_x = INFINITY;
21874 if (it.cmp_it.id < 0
21875 && it.method == GET_FROM_STRING
21876 && it.area == TEXT_AREA
21877 && it.string_from_display_prop_p
21878 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21879 overshoot_expected = true;
21881 /* Find the X coordinate of point. We start from the beginning
21882 of this or previous line to make sure we are before point in
21883 the logical order (since the move_it_* functions can only
21884 move forward). */
21885 reseat:
21886 reseat_at_previous_visible_line_start (&it);
21887 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21888 if (IT_CHARPOS (it) != PT)
21890 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21891 -1, -1, -1, MOVE_TO_POS);
21892 /* If we missed point because the character there is
21893 displayed out of a display vector that has more than one
21894 glyph, retry expecting overshoot. */
21895 if (it.method == GET_FROM_DISPLAY_VECTOR
21896 && it.current.dpvec_index > 0
21897 && !overshoot_expected)
21899 overshoot_expected = true;
21900 goto reseat;
21902 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21903 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21905 pt_x = it.current_x;
21906 pt_vpos = it.vpos;
21907 if (dir > 0 || overshoot_expected)
21909 struct glyph_row *row = it.glyph_row;
21911 /* When point is at beginning of line, we don't have
21912 information about the glyph there loaded into struct
21913 it. Calling get_next_display_element fixes that. */
21914 if (pt_x == 0)
21915 get_next_display_element (&it);
21916 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21917 it.glyph_row = NULL;
21918 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21919 it.glyph_row = row;
21920 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21921 it, lest it will become out of sync with it's buffer
21922 position. */
21923 it.current_x = pt_x;
21925 else
21926 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21927 pixel_width = it.pixel_width;
21928 if (overshoot_expected && at_eol_p)
21929 pixel_width = 0;
21930 else if (pixel_width <= 0)
21931 pixel_width = 1;
21933 /* If there's a display string (or something similar) at point,
21934 we are actually at the glyph to the left of point, so we need
21935 to correct the X coordinate. */
21936 if (overshoot_expected)
21938 if (it.bidi_p)
21939 pt_x += pixel_width * it.bidi_it.scan_dir;
21940 else
21941 pt_x += pixel_width;
21944 /* Compute target X coordinate, either to the left or to the
21945 right of point. On TTY frames, all characters have the same
21946 pixel width of 1, so we can use that. On GUI frames we don't
21947 have an easy way of getting at the pixel width of the
21948 character to the left of point, so we use a different method
21949 of getting to that place. */
21950 if (dir > 0)
21951 target_x = pt_x + pixel_width;
21952 else
21953 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21955 /* Target X coordinate could be one line above or below the line
21956 of point, in which case we need to adjust the target X
21957 coordinate. Also, if moving to the left, we need to begin at
21958 the left edge of the point's screen line. */
21959 if (dir < 0)
21961 if (pt_x > 0)
21963 start_display (&it, w, pt);
21964 if (it.line_wrap == TRUNCATE)
21965 it.last_visible_x = INFINITY;
21966 reseat_at_previous_visible_line_start (&it);
21967 it.current_x = it.current_y = it.hpos = 0;
21968 if (pt_vpos != 0)
21969 move_it_by_lines (&it, pt_vpos);
21971 else
21973 move_it_by_lines (&it, -1);
21974 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21975 target_is_eol_p = true;
21976 /* Under word-wrap, we don't know the x coordinate of
21977 the last character displayed on the previous line,
21978 which immediately precedes the wrap point. To find
21979 out its x coordinate, we try moving to the right
21980 margin of the window, which will stop at the wrap
21981 point, and then reset target_x to point at the
21982 character that precedes the wrap point. This is not
21983 needed on GUI frames, because (see below) there we
21984 move from the left margin one grapheme cluster at a
21985 time, and stop when we hit the wrap point. */
21986 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21988 void *it_data = NULL;
21989 struct it it2;
21991 SAVE_IT (it2, it, it_data);
21992 move_it_in_display_line_to (&it, ZV, target_x,
21993 MOVE_TO_POS | MOVE_TO_X);
21994 /* If we arrived at target_x, that _is_ the last
21995 character on the previous line. */
21996 if (it.current_x != target_x)
21997 target_x = it.current_x - 1;
21998 RESTORE_IT (&it, &it2, it_data);
22002 else
22004 if (at_eol_p
22005 || (target_x >= it.last_visible_x
22006 && it.line_wrap != TRUNCATE))
22008 if (pt_x > 0)
22009 move_it_by_lines (&it, 0);
22010 move_it_by_lines (&it, 1);
22011 target_x = 0;
22015 /* Move to the target X coordinate. */
22016 /* On GUI frames, as we don't know the X coordinate of the
22017 character to the left of point, moving point to the left
22018 requires walking, one grapheme cluster at a time, until we
22019 find ourself at a place immediately to the left of the
22020 character at point. */
22021 if (FRAME_WINDOW_P (it.f) && dir < 0)
22023 struct text_pos new_pos;
22024 enum move_it_result rc = MOVE_X_REACHED;
22026 if (it.current_x == 0)
22027 get_next_display_element (&it);
22028 if (it.what == IT_COMPOSITION)
22030 new_pos.charpos = it.cmp_it.charpos;
22031 new_pos.bytepos = -1;
22033 else
22034 new_pos = it.current.pos;
22036 while (it.current_x + it.pixel_width <= target_x
22037 && (rc == MOVE_X_REACHED
22038 /* Under word-wrap, move_it_in_display_line_to
22039 stops at correct coordinates, but sometimes
22040 returns MOVE_POS_MATCH_OR_ZV. */
22041 || (it.line_wrap == WORD_WRAP
22042 && rc == MOVE_POS_MATCH_OR_ZV)))
22044 int new_x = it.current_x + it.pixel_width;
22046 /* For composed characters, we want the position of the
22047 first character in the grapheme cluster (usually, the
22048 composition's base character), whereas it.current
22049 might give us the position of the _last_ one, e.g. if
22050 the composition is rendered in reverse due to bidi
22051 reordering. */
22052 if (it.what == IT_COMPOSITION)
22054 new_pos.charpos = it.cmp_it.charpos;
22055 new_pos.bytepos = -1;
22057 else
22058 new_pos = it.current.pos;
22059 if (new_x == it.current_x)
22060 new_x++;
22061 rc = move_it_in_display_line_to (&it, ZV, new_x,
22062 MOVE_TO_POS | MOVE_TO_X);
22063 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22064 break;
22066 /* The previous position we saw in the loop is the one we
22067 want. */
22068 if (new_pos.bytepos == -1)
22069 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22070 it.current.pos = new_pos;
22072 else if (it.current_x != target_x)
22073 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22075 /* If we ended up in a display string that covers point, move to
22076 buffer position to the right in the visual order. */
22077 if (dir > 0)
22079 while (IT_CHARPOS (it) == PT)
22081 set_iterator_to_next (&it, false);
22082 if (!get_next_display_element (&it))
22083 break;
22087 /* Move point to that position. */
22088 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22091 return make_number (PT);
22093 #undef ROW_GLYPH_NEWLINE_P
22096 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22097 Sbidi_resolved_levels, 0, 1, 0,
22098 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22100 The resolved levels are produced by the Emacs bidi reordering engine
22101 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22102 read the Unicode Standard Annex 9 (UAX#9) for background information
22103 about these levels.
22105 VPOS is the zero-based number of the current window's screen line
22106 for which to produce the resolved levels. If VPOS is nil or omitted,
22107 it defaults to the screen line of point. If the window displays a
22108 header line, VPOS of zero will report on the header line, and first
22109 line of text in the window will have VPOS of 1.
22111 Value is an array of resolved levels, indexed by glyph number.
22112 Glyphs are numbered from zero starting from the beginning of the
22113 screen line, i.e. the left edge of the window for left-to-right lines
22114 and from the right edge for right-to-left lines. The resolved levels
22115 are produced only for the window's text area; text in display margins
22116 is not included.
22118 If the selected window's display is not up-to-date, or if the specified
22119 screen line does not display text, this function returns nil. It is
22120 highly recommended to bind this function to some simple key, like F8,
22121 in order to avoid these problems.
22123 This function exists mainly for testing the correctness of the
22124 Emacs UBA implementation, in particular with the test suite. */)
22125 (Lisp_Object vpos)
22127 struct window *w = XWINDOW (selected_window);
22128 struct buffer *b = XBUFFER (w->contents);
22129 int nrow;
22130 struct glyph_row *row;
22132 if (NILP (vpos))
22134 int d1, d2, d3, d4, d5;
22136 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22138 else
22140 CHECK_NUMBER_COERCE_MARKER (vpos);
22141 nrow = XINT (vpos);
22144 /* We require up-to-date glyph matrix for this window. */
22145 if (w->window_end_valid
22146 && !windows_or_buffers_changed
22147 && b
22148 && !b->clip_changed
22149 && !b->prevent_redisplay_optimizations_p
22150 && !window_outdated (w)
22151 && nrow >= 0
22152 && nrow < w->current_matrix->nrows
22153 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22154 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22156 struct glyph *g, *e, *g1;
22157 int nglyphs, i;
22158 Lisp_Object levels;
22160 if (!row->reversed_p) /* Left-to-right glyph row. */
22162 g = g1 = row->glyphs[TEXT_AREA];
22163 e = g + row->used[TEXT_AREA];
22165 /* Skip over glyphs at the start of the row that was
22166 generated by redisplay for its own needs. */
22167 while (g < e
22168 && NILP (g->object)
22169 && g->charpos < 0)
22170 g++;
22171 g1 = g;
22173 /* Count the "interesting" glyphs in this row. */
22174 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22175 nglyphs++;
22177 /* Create and fill the array. */
22178 levels = make_uninit_vector (nglyphs);
22179 for (i = 0; g1 < g; i++, g1++)
22180 ASET (levels, i, make_number (g1->resolved_level));
22182 else /* Right-to-left glyph row. */
22184 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22185 e = row->glyphs[TEXT_AREA] - 1;
22186 while (g > e
22187 && NILP (g->object)
22188 && g->charpos < 0)
22189 g--;
22190 g1 = g;
22191 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22192 nglyphs++;
22193 levels = make_uninit_vector (nglyphs);
22194 for (i = 0; g1 > g; i++, g1--)
22195 ASET (levels, i, make_number (g1->resolved_level));
22197 return levels;
22199 else
22200 return Qnil;
22205 /***********************************************************************
22206 Menu Bar
22207 ***********************************************************************/
22209 /* Redisplay the menu bar in the frame for window W.
22211 The menu bar of X frames that don't have X toolkit support is
22212 displayed in a special window W->frame->menu_bar_window.
22214 The menu bar of terminal frames is treated specially as far as
22215 glyph matrices are concerned. Menu bar lines are not part of
22216 windows, so the update is done directly on the frame matrix rows
22217 for the menu bar. */
22219 static void
22220 display_menu_bar (struct window *w)
22222 struct frame *f = XFRAME (WINDOW_FRAME (w));
22223 struct it it;
22224 Lisp_Object items;
22225 int i;
22227 /* Don't do all this for graphical frames. */
22228 #ifdef HAVE_NTGUI
22229 if (FRAME_W32_P (f))
22230 return;
22231 #endif
22232 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22233 if (FRAME_X_P (f))
22234 return;
22235 #endif
22237 #ifdef HAVE_NS
22238 if (FRAME_NS_P (f))
22239 return;
22240 #endif /* HAVE_NS */
22242 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22243 eassert (!FRAME_WINDOW_P (f));
22244 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22245 it.first_visible_x = 0;
22246 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22247 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22248 if (FRAME_WINDOW_P (f))
22250 /* Menu bar lines are displayed in the desired matrix of the
22251 dummy window menu_bar_window. */
22252 struct window *menu_w;
22253 menu_w = XWINDOW (f->menu_bar_window);
22254 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22255 MENU_FACE_ID);
22256 it.first_visible_x = 0;
22257 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22259 else
22260 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22262 /* This is a TTY frame, i.e. character hpos/vpos are used as
22263 pixel x/y. */
22264 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22265 MENU_FACE_ID);
22266 it.first_visible_x = 0;
22267 it.last_visible_x = FRAME_COLS (f);
22270 /* FIXME: This should be controlled by a user option. See the
22271 comments in redisplay_tool_bar and display_mode_line about
22272 this. */
22273 it.paragraph_embedding = L2R;
22275 /* Clear all rows of the menu bar. */
22276 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22278 struct glyph_row *row = it.glyph_row + i;
22279 clear_glyph_row (row);
22280 row->enabled_p = true;
22281 row->full_width_p = true;
22282 row->reversed_p = false;
22285 /* Display all items of the menu bar. */
22286 items = FRAME_MENU_BAR_ITEMS (it.f);
22287 for (i = 0; i < ASIZE (items); i += 4)
22289 Lisp_Object string;
22291 /* Stop at nil string. */
22292 string = AREF (items, i + 1);
22293 if (NILP (string))
22294 break;
22296 /* Remember where item was displayed. */
22297 ASET (items, i + 3, make_number (it.hpos));
22299 /* Display the item, pad with one space. */
22300 if (it.current_x < it.last_visible_x)
22301 display_string (NULL, string, Qnil, 0, 0, &it,
22302 SCHARS (string) + 1, 0, 0, -1);
22305 /* Fill out the line with spaces. */
22306 if (it.current_x < it.last_visible_x)
22307 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22309 /* Compute the total height of the lines. */
22310 compute_line_metrics (&it);
22313 /* Deep copy of a glyph row, including the glyphs. */
22314 static void
22315 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22317 struct glyph *pointers[1 + LAST_AREA];
22318 int to_used = to->used[TEXT_AREA];
22320 /* Save glyph pointers of TO. */
22321 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22323 /* Do a structure assignment. */
22324 *to = *from;
22326 /* Restore original glyph pointers of TO. */
22327 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22329 /* Copy the glyphs. */
22330 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22331 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22333 /* If we filled only part of the TO row, fill the rest with
22334 space_glyph (which will display as empty space). */
22335 if (to_used > from->used[TEXT_AREA])
22336 fill_up_frame_row_with_spaces (to, to_used);
22339 /* Display one menu item on a TTY, by overwriting the glyphs in the
22340 frame F's desired glyph matrix with glyphs produced from the menu
22341 item text. Called from term.c to display TTY drop-down menus one
22342 item at a time.
22344 ITEM_TEXT is the menu item text as a C string.
22346 FACE_ID is the face ID to be used for this menu item. FACE_ID
22347 could specify one of 3 faces: a face for an enabled item, a face
22348 for a disabled item, or a face for a selected item.
22350 X and Y are coordinates of the first glyph in the frame's desired
22351 matrix to be overwritten by the menu item. Since this is a TTY, Y
22352 is the zero-based number of the glyph row and X is the zero-based
22353 glyph number in the row, starting from left, where to start
22354 displaying the item.
22356 SUBMENU means this menu item drops down a submenu, which
22357 should be indicated by displaying a proper visual cue after the
22358 item text. */
22360 void
22361 display_tty_menu_item (const char *item_text, int width, int face_id,
22362 int x, int y, bool submenu)
22364 struct it it;
22365 struct frame *f = SELECTED_FRAME ();
22366 struct window *w = XWINDOW (f->selected_window);
22367 struct glyph_row *row;
22368 size_t item_len = strlen (item_text);
22370 eassert (FRAME_TERMCAP_P (f));
22372 /* Don't write beyond the matrix's last row. This can happen for
22373 TTY screens that are not high enough to show the entire menu.
22374 (This is actually a bit of defensive programming, as
22375 tty_menu_display already limits the number of menu items to one
22376 less than the number of screen lines.) */
22377 if (y >= f->desired_matrix->nrows)
22378 return;
22380 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22381 it.first_visible_x = 0;
22382 it.last_visible_x = FRAME_COLS (f) - 1;
22383 row = it.glyph_row;
22384 /* Start with the row contents from the current matrix. */
22385 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22386 bool saved_width = row->full_width_p;
22387 row->full_width_p = true;
22388 bool saved_reversed = row->reversed_p;
22389 row->reversed_p = false;
22390 row->enabled_p = true;
22392 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22393 desired face. */
22394 eassert (x < f->desired_matrix->matrix_w);
22395 it.current_x = it.hpos = x;
22396 it.current_y = it.vpos = y;
22397 int saved_used = row->used[TEXT_AREA];
22398 bool saved_truncated = row->truncated_on_right_p;
22399 row->used[TEXT_AREA] = x;
22400 it.face_id = face_id;
22401 it.line_wrap = TRUNCATE;
22403 /* FIXME: This should be controlled by a user option. See the
22404 comments in redisplay_tool_bar and display_mode_line about this.
22405 Also, if paragraph_embedding could ever be R2L, changes will be
22406 needed to avoid shifting to the right the row characters in
22407 term.c:append_glyph. */
22408 it.paragraph_embedding = L2R;
22410 /* Pad with a space on the left. */
22411 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22412 width--;
22413 /* Display the menu item, pad with spaces to WIDTH. */
22414 if (submenu)
22416 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22417 item_len, 0, FRAME_COLS (f) - 1, -1);
22418 width -= item_len;
22419 /* Indicate with " >" that there's a submenu. */
22420 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22421 FRAME_COLS (f) - 1, -1);
22423 else
22424 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22425 width, 0, FRAME_COLS (f) - 1, -1);
22427 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22428 row->truncated_on_right_p = saved_truncated;
22429 row->hash = row_hash (row);
22430 row->full_width_p = saved_width;
22431 row->reversed_p = saved_reversed;
22434 /***********************************************************************
22435 Mode Line
22436 ***********************************************************************/
22438 /* Redisplay mode lines in the window tree whose root is WINDOW.
22439 If FORCE, redisplay mode lines unconditionally.
22440 Otherwise, redisplay only mode lines that are garbaged. Value is
22441 the number of windows whose mode lines were redisplayed. */
22443 static int
22444 redisplay_mode_lines (Lisp_Object window, bool force)
22446 int nwindows = 0;
22448 while (!NILP (window))
22450 struct window *w = XWINDOW (window);
22452 if (WINDOWP (w->contents))
22453 nwindows += redisplay_mode_lines (w->contents, force);
22454 else if (force
22455 || FRAME_GARBAGED_P (XFRAME (w->frame))
22456 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22458 struct text_pos lpoint;
22459 struct buffer *old = current_buffer;
22461 /* Set the window's buffer for the mode line display. */
22462 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22463 set_buffer_internal_1 (XBUFFER (w->contents));
22465 /* Point refers normally to the selected window. For any
22466 other window, set up appropriate value. */
22467 if (!EQ (window, selected_window))
22469 struct text_pos pt;
22471 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22472 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22475 /* Display mode lines. */
22476 clear_glyph_matrix (w->desired_matrix);
22477 if (display_mode_lines (w))
22478 ++nwindows;
22480 /* Restore old settings. */
22481 set_buffer_internal_1 (old);
22482 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22485 window = w->next;
22488 return nwindows;
22492 /* Display the mode and/or header line of window W. Value is the
22493 sum number of mode lines and header lines displayed. */
22495 static int
22496 display_mode_lines (struct window *w)
22498 Lisp_Object old_selected_window = selected_window;
22499 Lisp_Object old_selected_frame = selected_frame;
22500 Lisp_Object new_frame = w->frame;
22501 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22502 int n = 0;
22504 selected_frame = new_frame;
22505 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22506 or window's point, then we'd need select_window_1 here as well. */
22507 XSETWINDOW (selected_window, w);
22508 XFRAME (new_frame)->selected_window = selected_window;
22510 /* These will be set while the mode line specs are processed. */
22511 line_number_displayed = false;
22512 w->column_number_displayed = -1;
22514 if (WINDOW_WANTS_MODELINE_P (w))
22516 struct window *sel_w = XWINDOW (old_selected_window);
22518 /* Select mode line face based on the real selected window. */
22519 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22520 BVAR (current_buffer, mode_line_format));
22521 ++n;
22524 if (WINDOW_WANTS_HEADER_LINE_P (w))
22526 display_mode_line (w, HEADER_LINE_FACE_ID,
22527 BVAR (current_buffer, header_line_format));
22528 ++n;
22531 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22532 selected_frame = old_selected_frame;
22533 selected_window = old_selected_window;
22534 if (n > 0)
22535 w->must_be_updated_p = true;
22536 return n;
22540 /* Display mode or header line of window W. FACE_ID specifies which
22541 line to display; it is either MODE_LINE_FACE_ID or
22542 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22543 display. Value is the pixel height of the mode/header line
22544 displayed. */
22546 static int
22547 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22549 struct it it;
22550 struct face *face;
22551 ptrdiff_t count = SPECPDL_INDEX ();
22553 init_iterator (&it, w, -1, -1, NULL, face_id);
22554 /* Don't extend on a previously drawn mode-line.
22555 This may happen if called from pos_visible_p. */
22556 it.glyph_row->enabled_p = false;
22557 prepare_desired_row (w, it.glyph_row, true);
22559 it.glyph_row->mode_line_p = true;
22561 /* FIXME: This should be controlled by a user option. But
22562 supporting such an option is not trivial, since the mode line is
22563 made up of many separate strings. */
22564 it.paragraph_embedding = L2R;
22566 record_unwind_protect (unwind_format_mode_line,
22567 format_mode_line_unwind_data (NULL, NULL,
22568 Qnil, false));
22570 mode_line_target = MODE_LINE_DISPLAY;
22572 /* Temporarily make frame's keyboard the current kboard so that
22573 kboard-local variables in the mode_line_format will get the right
22574 values. */
22575 push_kboard (FRAME_KBOARD (it.f));
22576 record_unwind_save_match_data ();
22577 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22578 pop_kboard ();
22580 unbind_to (count, Qnil);
22582 /* Fill up with spaces. */
22583 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22585 compute_line_metrics (&it);
22586 it.glyph_row->full_width_p = true;
22587 it.glyph_row->continued_p = false;
22588 it.glyph_row->truncated_on_left_p = false;
22589 it.glyph_row->truncated_on_right_p = false;
22591 /* Make a 3D mode-line have a shadow at its right end. */
22592 face = FACE_FROM_ID (it.f, face_id);
22593 extend_face_to_end_of_line (&it);
22594 if (face->box != FACE_NO_BOX)
22596 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22597 + it.glyph_row->used[TEXT_AREA] - 1);
22598 last->right_box_line_p = true;
22601 return it.glyph_row->height;
22604 /* Move element ELT in LIST to the front of LIST.
22605 Return the updated list. */
22607 static Lisp_Object
22608 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22610 register Lisp_Object tail, prev;
22611 register Lisp_Object tem;
22613 tail = list;
22614 prev = Qnil;
22615 while (CONSP (tail))
22617 tem = XCAR (tail);
22619 if (EQ (elt, tem))
22621 /* Splice out the link TAIL. */
22622 if (NILP (prev))
22623 list = XCDR (tail);
22624 else
22625 Fsetcdr (prev, XCDR (tail));
22627 /* Now make it the first. */
22628 Fsetcdr (tail, list);
22629 return tail;
22631 else
22632 prev = tail;
22633 tail = XCDR (tail);
22634 QUIT;
22637 /* Not found--return unchanged LIST. */
22638 return list;
22641 /* Contribute ELT to the mode line for window IT->w. How it
22642 translates into text depends on its data type.
22644 IT describes the display environment in which we display, as usual.
22646 DEPTH is the depth in recursion. It is used to prevent
22647 infinite recursion here.
22649 FIELD_WIDTH is the number of characters the display of ELT should
22650 occupy in the mode line, and PRECISION is the maximum number of
22651 characters to display from ELT's representation. See
22652 display_string for details.
22654 Returns the hpos of the end of the text generated by ELT.
22656 PROPS is a property list to add to any string we encounter.
22658 If RISKY, remove (disregard) any properties in any string
22659 we encounter, and ignore :eval and :propertize.
22661 The global variable `mode_line_target' determines whether the
22662 output is passed to `store_mode_line_noprop',
22663 `store_mode_line_string', or `display_string'. */
22665 static int
22666 display_mode_element (struct it *it, int depth, int field_width, int precision,
22667 Lisp_Object elt, Lisp_Object props, bool risky)
22669 int n = 0, field, prec;
22670 bool literal = false;
22672 tail_recurse:
22673 if (depth > 100)
22674 elt = build_string ("*too-deep*");
22676 depth++;
22678 switch (XTYPE (elt))
22680 case Lisp_String:
22682 /* A string: output it and check for %-constructs within it. */
22683 unsigned char c;
22684 ptrdiff_t offset = 0;
22686 if (SCHARS (elt) > 0
22687 && (!NILP (props) || risky))
22689 Lisp_Object oprops, aelt;
22690 oprops = Ftext_properties_at (make_number (0), elt);
22692 /* If the starting string's properties are not what
22693 we want, translate the string. Also, if the string
22694 is risky, do that anyway. */
22696 if (NILP (Fequal (props, oprops)) || risky)
22698 /* If the starting string has properties,
22699 merge the specified ones onto the existing ones. */
22700 if (! NILP (oprops) && !risky)
22702 Lisp_Object tem;
22704 oprops = Fcopy_sequence (oprops);
22705 tem = props;
22706 while (CONSP (tem))
22708 oprops = Fplist_put (oprops, XCAR (tem),
22709 XCAR (XCDR (tem)));
22710 tem = XCDR (XCDR (tem));
22712 props = oprops;
22715 aelt = Fassoc (elt, mode_line_proptrans_alist);
22716 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22718 /* AELT is what we want. Move it to the front
22719 without consing. */
22720 elt = XCAR (aelt);
22721 mode_line_proptrans_alist
22722 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22724 else
22726 Lisp_Object tem;
22728 /* If AELT has the wrong props, it is useless.
22729 so get rid of it. */
22730 if (! NILP (aelt))
22731 mode_line_proptrans_alist
22732 = Fdelq (aelt, mode_line_proptrans_alist);
22734 elt = Fcopy_sequence (elt);
22735 Fset_text_properties (make_number (0), Flength (elt),
22736 props, elt);
22737 /* Add this item to mode_line_proptrans_alist. */
22738 mode_line_proptrans_alist
22739 = Fcons (Fcons (elt, props),
22740 mode_line_proptrans_alist);
22741 /* Truncate mode_line_proptrans_alist
22742 to at most 50 elements. */
22743 tem = Fnthcdr (make_number (50),
22744 mode_line_proptrans_alist);
22745 if (! NILP (tem))
22746 XSETCDR (tem, Qnil);
22751 offset = 0;
22753 if (literal)
22755 prec = precision - n;
22756 switch (mode_line_target)
22758 case MODE_LINE_NOPROP:
22759 case MODE_LINE_TITLE:
22760 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22761 break;
22762 case MODE_LINE_STRING:
22763 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22764 break;
22765 case MODE_LINE_DISPLAY:
22766 n += display_string (NULL, elt, Qnil, 0, 0, it,
22767 0, prec, 0, STRING_MULTIBYTE (elt));
22768 break;
22771 break;
22774 /* Handle the non-literal case. */
22776 while ((precision <= 0 || n < precision)
22777 && SREF (elt, offset) != 0
22778 && (mode_line_target != MODE_LINE_DISPLAY
22779 || it->current_x < it->last_visible_x))
22781 ptrdiff_t last_offset = offset;
22783 /* Advance to end of string or next format specifier. */
22784 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22787 if (offset - 1 != last_offset)
22789 ptrdiff_t nchars, nbytes;
22791 /* Output to end of string or up to '%'. Field width
22792 is length of string. Don't output more than
22793 PRECISION allows us. */
22794 offset--;
22796 prec = c_string_width (SDATA (elt) + last_offset,
22797 offset - last_offset, precision - n,
22798 &nchars, &nbytes);
22800 switch (mode_line_target)
22802 case MODE_LINE_NOPROP:
22803 case MODE_LINE_TITLE:
22804 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22805 break;
22806 case MODE_LINE_STRING:
22808 ptrdiff_t bytepos = last_offset;
22809 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22810 ptrdiff_t endpos = (precision <= 0
22811 ? string_byte_to_char (elt, offset)
22812 : charpos + nchars);
22813 Lisp_Object mode_string
22814 = Fsubstring (elt, make_number (charpos),
22815 make_number (endpos));
22816 n += store_mode_line_string (NULL, mode_string, false,
22817 0, 0, Qnil);
22819 break;
22820 case MODE_LINE_DISPLAY:
22822 ptrdiff_t bytepos = last_offset;
22823 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22825 if (precision <= 0)
22826 nchars = string_byte_to_char (elt, offset) - charpos;
22827 n += display_string (NULL, elt, Qnil, 0, charpos,
22828 it, 0, nchars, 0,
22829 STRING_MULTIBYTE (elt));
22831 break;
22834 else /* c == '%' */
22836 ptrdiff_t percent_position = offset;
22838 /* Get the specified minimum width. Zero means
22839 don't pad. */
22840 field = 0;
22841 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22842 field = field * 10 + c - '0';
22844 /* Don't pad beyond the total padding allowed. */
22845 if (field_width - n > 0 && field > field_width - n)
22846 field = field_width - n;
22848 /* Note that either PRECISION <= 0 or N < PRECISION. */
22849 prec = precision - n;
22851 if (c == 'M')
22852 n += display_mode_element (it, depth, field, prec,
22853 Vglobal_mode_string, props,
22854 risky);
22855 else if (c != 0)
22857 bool multibyte;
22858 ptrdiff_t bytepos, charpos;
22859 const char *spec;
22860 Lisp_Object string;
22862 bytepos = percent_position;
22863 charpos = (STRING_MULTIBYTE (elt)
22864 ? string_byte_to_char (elt, bytepos)
22865 : bytepos);
22866 spec = decode_mode_spec (it->w, c, field, &string);
22867 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22869 switch (mode_line_target)
22871 case MODE_LINE_NOPROP:
22872 case MODE_LINE_TITLE:
22873 n += store_mode_line_noprop (spec, field, prec);
22874 break;
22875 case MODE_LINE_STRING:
22877 Lisp_Object tem = build_string (spec);
22878 props = Ftext_properties_at (make_number (charpos), elt);
22879 /* Should only keep face property in props */
22880 n += store_mode_line_string (NULL, tem, false,
22881 field, prec, props);
22883 break;
22884 case MODE_LINE_DISPLAY:
22886 int nglyphs_before, nwritten;
22888 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22889 nwritten = display_string (spec, string, elt,
22890 charpos, 0, it,
22891 field, prec, 0,
22892 multibyte);
22894 /* Assign to the glyphs written above the
22895 string where the `%x' came from, position
22896 of the `%'. */
22897 if (nwritten > 0)
22899 struct glyph *glyph
22900 = (it->glyph_row->glyphs[TEXT_AREA]
22901 + nglyphs_before);
22902 int i;
22904 for (i = 0; i < nwritten; ++i)
22906 glyph[i].object = elt;
22907 glyph[i].charpos = charpos;
22910 n += nwritten;
22913 break;
22916 else /* c == 0 */
22917 break;
22921 break;
22923 case Lisp_Symbol:
22924 /* A symbol: process the value of the symbol recursively
22925 as if it appeared here directly. Avoid error if symbol void.
22926 Special case: if value of symbol is a string, output the string
22927 literally. */
22929 register Lisp_Object tem;
22931 /* If the variable is not marked as risky to set
22932 then its contents are risky to use. */
22933 if (NILP (Fget (elt, Qrisky_local_variable)))
22934 risky = true;
22936 tem = Fboundp (elt);
22937 if (!NILP (tem))
22939 tem = Fsymbol_value (elt);
22940 /* If value is a string, output that string literally:
22941 don't check for % within it. */
22942 if (STRINGP (tem))
22943 literal = true;
22945 if (!EQ (tem, elt))
22947 /* Give up right away for nil or t. */
22948 elt = tem;
22949 goto tail_recurse;
22953 break;
22955 case Lisp_Cons:
22957 register Lisp_Object car, tem;
22959 /* A cons cell: five distinct cases.
22960 If first element is :eval or :propertize, do something special.
22961 If first element is a string or a cons, process all the elements
22962 and effectively concatenate them.
22963 If first element is a negative number, truncate displaying cdr to
22964 at most that many characters. If positive, pad (with spaces)
22965 to at least that many characters.
22966 If first element is a symbol, process the cadr or caddr recursively
22967 according to whether the symbol's value is non-nil or nil. */
22968 car = XCAR (elt);
22969 if (EQ (car, QCeval))
22971 /* An element of the form (:eval FORM) means evaluate FORM
22972 and use the result as mode line elements. */
22974 if (risky)
22975 break;
22977 if (CONSP (XCDR (elt)))
22979 Lisp_Object spec;
22980 spec = safe__eval (true, XCAR (XCDR (elt)));
22981 n += display_mode_element (it, depth, field_width - n,
22982 precision - n, spec, props,
22983 risky);
22986 else if (EQ (car, QCpropertize))
22988 /* An element of the form (:propertize ELT PROPS...)
22989 means display ELT but applying properties PROPS. */
22991 if (risky)
22992 break;
22994 if (CONSP (XCDR (elt)))
22995 n += display_mode_element (it, depth, field_width - n,
22996 precision - n, XCAR (XCDR (elt)),
22997 XCDR (XCDR (elt)), risky);
22999 else if (SYMBOLP (car))
23001 tem = Fboundp (car);
23002 elt = XCDR (elt);
23003 if (!CONSP (elt))
23004 goto invalid;
23005 /* elt is now the cdr, and we know it is a cons cell.
23006 Use its car if CAR has a non-nil value. */
23007 if (!NILP (tem))
23009 tem = Fsymbol_value (car);
23010 if (!NILP (tem))
23012 elt = XCAR (elt);
23013 goto tail_recurse;
23016 /* Symbol's value is nil (or symbol is unbound)
23017 Get the cddr of the original list
23018 and if possible find the caddr and use that. */
23019 elt = XCDR (elt);
23020 if (NILP (elt))
23021 break;
23022 else if (!CONSP (elt))
23023 goto invalid;
23024 elt = XCAR (elt);
23025 goto tail_recurse;
23027 else if (INTEGERP (car))
23029 register int lim = XINT (car);
23030 elt = XCDR (elt);
23031 if (lim < 0)
23033 /* Negative int means reduce maximum width. */
23034 if (precision <= 0)
23035 precision = -lim;
23036 else
23037 precision = min (precision, -lim);
23039 else if (lim > 0)
23041 /* Padding specified. Don't let it be more than
23042 current maximum. */
23043 if (precision > 0)
23044 lim = min (precision, lim);
23046 /* If that's more padding than already wanted, queue it.
23047 But don't reduce padding already specified even if
23048 that is beyond the current truncation point. */
23049 field_width = max (lim, field_width);
23051 goto tail_recurse;
23053 else if (STRINGP (car) || CONSP (car))
23055 Lisp_Object halftail = elt;
23056 int len = 0;
23058 while (CONSP (elt)
23059 && (precision <= 0 || n < precision))
23061 n += display_mode_element (it, depth,
23062 /* Do padding only after the last
23063 element in the list. */
23064 (! CONSP (XCDR (elt))
23065 ? field_width - n
23066 : 0),
23067 precision - n, XCAR (elt),
23068 props, risky);
23069 elt = XCDR (elt);
23070 len++;
23071 if ((len & 1) == 0)
23072 halftail = XCDR (halftail);
23073 /* Check for cycle. */
23074 if (EQ (halftail, elt))
23075 break;
23079 break;
23081 default:
23082 invalid:
23083 elt = build_string ("*invalid*");
23084 goto tail_recurse;
23087 /* Pad to FIELD_WIDTH. */
23088 if (field_width > 0 && n < field_width)
23090 switch (mode_line_target)
23092 case MODE_LINE_NOPROP:
23093 case MODE_LINE_TITLE:
23094 n += store_mode_line_noprop ("", field_width - n, 0);
23095 break;
23096 case MODE_LINE_STRING:
23097 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23098 Qnil);
23099 break;
23100 case MODE_LINE_DISPLAY:
23101 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23102 0, 0, 0);
23103 break;
23107 return n;
23110 /* Store a mode-line string element in mode_line_string_list.
23112 If STRING is non-null, display that C string. Otherwise, the Lisp
23113 string LISP_STRING is displayed.
23115 FIELD_WIDTH is the minimum number of output glyphs to produce.
23116 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23117 with spaces. FIELD_WIDTH <= 0 means don't pad.
23119 PRECISION is the maximum number of characters to output from
23120 STRING. PRECISION <= 0 means don't truncate the string.
23122 If COPY_STRING, make a copy of LISP_STRING before adding
23123 properties to the string.
23125 PROPS are the properties to add to the string.
23126 The mode_line_string_face face property is always added to the string.
23129 static int
23130 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23131 bool copy_string,
23132 int field_width, int precision, Lisp_Object props)
23134 ptrdiff_t len;
23135 int n = 0;
23137 if (string != NULL)
23139 len = strlen (string);
23140 if (precision > 0 && len > precision)
23141 len = precision;
23142 lisp_string = make_string (string, len);
23143 if (NILP (props))
23144 props = mode_line_string_face_prop;
23145 else if (!NILP (mode_line_string_face))
23147 Lisp_Object face = Fplist_get (props, Qface);
23148 props = Fcopy_sequence (props);
23149 if (NILP (face))
23150 face = mode_line_string_face;
23151 else
23152 face = list2 (face, mode_line_string_face);
23153 props = Fplist_put (props, Qface, face);
23155 Fadd_text_properties (make_number (0), make_number (len),
23156 props, lisp_string);
23158 else
23160 len = XFASTINT (Flength (lisp_string));
23161 if (precision > 0 && len > precision)
23163 len = precision;
23164 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23165 precision = -1;
23167 if (!NILP (mode_line_string_face))
23169 Lisp_Object face;
23170 if (NILP (props))
23171 props = Ftext_properties_at (make_number (0), lisp_string);
23172 face = Fplist_get (props, Qface);
23173 if (NILP (face))
23174 face = mode_line_string_face;
23175 else
23176 face = list2 (face, mode_line_string_face);
23177 props = list2 (Qface, face);
23178 if (copy_string)
23179 lisp_string = Fcopy_sequence (lisp_string);
23181 if (!NILP (props))
23182 Fadd_text_properties (make_number (0), make_number (len),
23183 props, lisp_string);
23186 if (len > 0)
23188 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23189 n += len;
23192 if (field_width > len)
23194 field_width -= len;
23195 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23196 if (!NILP (props))
23197 Fadd_text_properties (make_number (0), make_number (field_width),
23198 props, lisp_string);
23199 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23200 n += field_width;
23203 return n;
23207 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23208 1, 4, 0,
23209 doc: /* Format a string out of a mode line format specification.
23210 First arg FORMAT specifies the mode line format (see `mode-line-format'
23211 for details) to use.
23213 By default, the format is evaluated for the currently selected window.
23215 Optional second arg FACE specifies the face property to put on all
23216 characters for which no face is specified. The value nil means the
23217 default face. The value t means whatever face the window's mode line
23218 currently uses (either `mode-line' or `mode-line-inactive',
23219 depending on whether the window is the selected window or not).
23220 An integer value means the value string has no text
23221 properties.
23223 Optional third and fourth args WINDOW and BUFFER specify the window
23224 and buffer to use as the context for the formatting (defaults
23225 are the selected window and the WINDOW's buffer). */)
23226 (Lisp_Object format, Lisp_Object face,
23227 Lisp_Object window, Lisp_Object buffer)
23229 struct it it;
23230 int len;
23231 struct window *w;
23232 struct buffer *old_buffer = NULL;
23233 int face_id;
23234 bool no_props = INTEGERP (face);
23235 ptrdiff_t count = SPECPDL_INDEX ();
23236 Lisp_Object str;
23237 int string_start = 0;
23239 w = decode_any_window (window);
23240 XSETWINDOW (window, w);
23242 if (NILP (buffer))
23243 buffer = w->contents;
23244 CHECK_BUFFER (buffer);
23246 /* Make formatting the modeline a non-op when noninteractive, otherwise
23247 there will be problems later caused by a partially initialized frame. */
23248 if (NILP (format) || noninteractive)
23249 return empty_unibyte_string;
23251 if (no_props)
23252 face = Qnil;
23254 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23255 : EQ (face, Qt) ? (EQ (window, selected_window)
23256 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23257 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23258 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23259 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23260 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23261 : DEFAULT_FACE_ID;
23263 old_buffer = current_buffer;
23265 /* Save things including mode_line_proptrans_alist,
23266 and set that to nil so that we don't alter the outer value. */
23267 record_unwind_protect (unwind_format_mode_line,
23268 format_mode_line_unwind_data
23269 (XFRAME (WINDOW_FRAME (w)),
23270 old_buffer, selected_window, true));
23271 mode_line_proptrans_alist = Qnil;
23273 Fselect_window (window, Qt);
23274 set_buffer_internal_1 (XBUFFER (buffer));
23276 init_iterator (&it, w, -1, -1, NULL, face_id);
23278 if (no_props)
23280 mode_line_target = MODE_LINE_NOPROP;
23281 mode_line_string_face_prop = Qnil;
23282 mode_line_string_list = Qnil;
23283 string_start = MODE_LINE_NOPROP_LEN (0);
23285 else
23287 mode_line_target = MODE_LINE_STRING;
23288 mode_line_string_list = Qnil;
23289 mode_line_string_face = face;
23290 mode_line_string_face_prop
23291 = NILP (face) ? Qnil : list2 (Qface, face);
23294 push_kboard (FRAME_KBOARD (it.f));
23295 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23296 pop_kboard ();
23298 if (no_props)
23300 len = MODE_LINE_NOPROP_LEN (string_start);
23301 str = make_string (mode_line_noprop_buf + string_start, len);
23303 else
23305 mode_line_string_list = Fnreverse (mode_line_string_list);
23306 str = Fmapconcat (Qidentity, mode_line_string_list,
23307 empty_unibyte_string);
23310 unbind_to (count, Qnil);
23311 return str;
23314 /* Write a null-terminated, right justified decimal representation of
23315 the positive integer D to BUF using a minimal field width WIDTH. */
23317 static void
23318 pint2str (register char *buf, register int width, register ptrdiff_t d)
23320 register char *p = buf;
23322 if (d <= 0)
23323 *p++ = '0';
23324 else
23326 while (d > 0)
23328 *p++ = d % 10 + '0';
23329 d /= 10;
23333 for (width -= (int) (p - buf); width > 0; --width)
23334 *p++ = ' ';
23335 *p-- = '\0';
23336 while (p > buf)
23338 d = *buf;
23339 *buf++ = *p;
23340 *p-- = d;
23344 /* Write a null-terminated, right justified decimal and "human
23345 readable" representation of the nonnegative integer D to BUF using
23346 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23348 static const char power_letter[] =
23350 0, /* no letter */
23351 'k', /* kilo */
23352 'M', /* mega */
23353 'G', /* giga */
23354 'T', /* tera */
23355 'P', /* peta */
23356 'E', /* exa */
23357 'Z', /* zetta */
23358 'Y' /* yotta */
23361 static void
23362 pint2hrstr (char *buf, int width, ptrdiff_t d)
23364 /* We aim to represent the nonnegative integer D as
23365 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23366 ptrdiff_t quotient = d;
23367 int remainder = 0;
23368 /* -1 means: do not use TENTHS. */
23369 int tenths = -1;
23370 int exponent = 0;
23372 /* Length of QUOTIENT.TENTHS as a string. */
23373 int length;
23375 char * psuffix;
23376 char * p;
23378 if (quotient >= 1000)
23380 /* Scale to the appropriate EXPONENT. */
23383 remainder = quotient % 1000;
23384 quotient /= 1000;
23385 exponent++;
23387 while (quotient >= 1000);
23389 /* Round to nearest and decide whether to use TENTHS or not. */
23390 if (quotient <= 9)
23392 tenths = remainder / 100;
23393 if (remainder % 100 >= 50)
23395 if (tenths < 9)
23396 tenths++;
23397 else
23399 quotient++;
23400 if (quotient == 10)
23401 tenths = -1;
23402 else
23403 tenths = 0;
23407 else
23408 if (remainder >= 500)
23410 if (quotient < 999)
23411 quotient++;
23412 else
23414 quotient = 1;
23415 exponent++;
23416 tenths = 0;
23421 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23422 if (tenths == -1 && quotient <= 99)
23423 if (quotient <= 9)
23424 length = 1;
23425 else
23426 length = 2;
23427 else
23428 length = 3;
23429 p = psuffix = buf + max (width, length);
23431 /* Print EXPONENT. */
23432 *psuffix++ = power_letter[exponent];
23433 *psuffix = '\0';
23435 /* Print TENTHS. */
23436 if (tenths >= 0)
23438 *--p = '0' + tenths;
23439 *--p = '.';
23442 /* Print QUOTIENT. */
23445 int digit = quotient % 10;
23446 *--p = '0' + digit;
23448 while ((quotient /= 10) != 0);
23450 /* Print leading spaces. */
23451 while (buf < p)
23452 *--p = ' ';
23455 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23456 If EOL_FLAG, set also a mnemonic character for end-of-line
23457 type of CODING_SYSTEM. Return updated pointer into BUF. */
23459 static unsigned char invalid_eol_type[] = "(*invalid*)";
23461 static char *
23462 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23464 Lisp_Object val;
23465 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23466 const unsigned char *eol_str;
23467 int eol_str_len;
23468 /* The EOL conversion we are using. */
23469 Lisp_Object eoltype;
23471 val = CODING_SYSTEM_SPEC (coding_system);
23472 eoltype = Qnil;
23474 if (!VECTORP (val)) /* Not yet decided. */
23476 *buf++ = multibyte ? '-' : ' ';
23477 if (eol_flag)
23478 eoltype = eol_mnemonic_undecided;
23479 /* Don't mention EOL conversion if it isn't decided. */
23481 else
23483 Lisp_Object attrs;
23484 Lisp_Object eolvalue;
23486 attrs = AREF (val, 0);
23487 eolvalue = AREF (val, 2);
23489 *buf++ = multibyte
23490 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23491 : ' ';
23493 if (eol_flag)
23495 /* The EOL conversion that is normal on this system. */
23497 if (NILP (eolvalue)) /* Not yet decided. */
23498 eoltype = eol_mnemonic_undecided;
23499 else if (VECTORP (eolvalue)) /* Not yet decided. */
23500 eoltype = eol_mnemonic_undecided;
23501 else /* eolvalue is Qunix, Qdos, or Qmac. */
23502 eoltype = (EQ (eolvalue, Qunix)
23503 ? eol_mnemonic_unix
23504 : EQ (eolvalue, Qdos)
23505 ? eol_mnemonic_dos : eol_mnemonic_mac);
23509 if (eol_flag)
23511 /* Mention the EOL conversion if it is not the usual one. */
23512 if (STRINGP (eoltype))
23514 eol_str = SDATA (eoltype);
23515 eol_str_len = SBYTES (eoltype);
23517 else if (CHARACTERP (eoltype))
23519 int c = XFASTINT (eoltype);
23520 return buf + CHAR_STRING (c, (unsigned char *) buf);
23522 else
23524 eol_str = invalid_eol_type;
23525 eol_str_len = sizeof (invalid_eol_type) - 1;
23527 memcpy (buf, eol_str, eol_str_len);
23528 buf += eol_str_len;
23531 return buf;
23534 /* Return the approximate percentage N is of D (rounding upward), or 99,
23535 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23537 static int
23538 percent99 (ptrdiff_t n, ptrdiff_t d)
23540 int percent = (d - 1 + 100.0 * n) / d;
23541 return min (percent, 99);
23544 /* Return a string for the output of a mode line %-spec for window W,
23545 generated by character C. FIELD_WIDTH > 0 means pad the string
23546 returned with spaces to that value. Return a Lisp string in
23547 *STRING if the resulting string is taken from that Lisp string.
23549 Note we operate on the current buffer for most purposes. */
23551 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23553 static const char *
23554 decode_mode_spec (struct window *w, register int c, int field_width,
23555 Lisp_Object *string)
23557 Lisp_Object obj;
23558 struct frame *f = XFRAME (WINDOW_FRAME (w));
23559 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23560 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23561 produce strings from numerical values, so limit preposterously
23562 large values of FIELD_WIDTH to avoid overrunning the buffer's
23563 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23564 bytes plus the terminating null. */
23565 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23566 struct buffer *b = current_buffer;
23568 obj = Qnil;
23569 *string = Qnil;
23571 switch (c)
23573 case '*':
23574 if (!NILP (BVAR (b, read_only)))
23575 return "%";
23576 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23577 return "*";
23578 return "-";
23580 case '+':
23581 /* This differs from %* only for a modified read-only buffer. */
23582 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23583 return "*";
23584 if (!NILP (BVAR (b, read_only)))
23585 return "%";
23586 return "-";
23588 case '&':
23589 /* This differs from %* in ignoring read-only-ness. */
23590 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23591 return "*";
23592 return "-";
23594 case '%':
23595 return "%";
23597 case '[':
23599 int i;
23600 char *p;
23602 if (command_loop_level > 5)
23603 return "[[[... ";
23604 p = decode_mode_spec_buf;
23605 for (i = 0; i < command_loop_level; i++)
23606 *p++ = '[';
23607 *p = 0;
23608 return decode_mode_spec_buf;
23611 case ']':
23613 int i;
23614 char *p;
23616 if (command_loop_level > 5)
23617 return " ...]]]";
23618 p = decode_mode_spec_buf;
23619 for (i = 0; i < command_loop_level; i++)
23620 *p++ = ']';
23621 *p = 0;
23622 return decode_mode_spec_buf;
23625 case '-':
23627 register int i;
23629 /* Let lots_of_dashes be a string of infinite length. */
23630 if (mode_line_target == MODE_LINE_NOPROP
23631 || mode_line_target == MODE_LINE_STRING)
23632 return "--";
23633 if (field_width <= 0
23634 || field_width > sizeof (lots_of_dashes))
23636 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23637 decode_mode_spec_buf[i] = '-';
23638 decode_mode_spec_buf[i] = '\0';
23639 return decode_mode_spec_buf;
23641 else
23642 return lots_of_dashes;
23645 case 'b':
23646 obj = BVAR (b, name);
23647 break;
23649 case 'c':
23650 /* %c and %l are ignored in `frame-title-format'.
23651 (In redisplay_internal, the frame title is drawn _before_ the
23652 windows are updated, so the stuff which depends on actual
23653 window contents (such as %l) may fail to render properly, or
23654 even crash emacs.) */
23655 if (mode_line_target == MODE_LINE_TITLE)
23656 return "";
23657 else
23659 ptrdiff_t col = current_column ();
23660 w->column_number_displayed = col;
23661 pint2str (decode_mode_spec_buf, width, col);
23662 return decode_mode_spec_buf;
23665 case 'e':
23666 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23668 if (NILP (Vmemory_full))
23669 return "";
23670 else
23671 return "!MEM FULL! ";
23673 #else
23674 return "";
23675 #endif
23677 case 'F':
23678 /* %F displays the frame name. */
23679 if (!NILP (f->title))
23680 return SSDATA (f->title);
23681 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23682 return SSDATA (f->name);
23683 return "Emacs";
23685 case 'f':
23686 obj = BVAR (b, filename);
23687 break;
23689 case 'i':
23691 ptrdiff_t size = ZV - BEGV;
23692 pint2str (decode_mode_spec_buf, width, size);
23693 return decode_mode_spec_buf;
23696 case 'I':
23698 ptrdiff_t size = ZV - BEGV;
23699 pint2hrstr (decode_mode_spec_buf, width, size);
23700 return decode_mode_spec_buf;
23703 case 'l':
23705 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23706 ptrdiff_t topline, nlines, height;
23707 ptrdiff_t junk;
23709 /* %c and %l are ignored in `frame-title-format'. */
23710 if (mode_line_target == MODE_LINE_TITLE)
23711 return "";
23713 startpos = marker_position (w->start);
23714 startpos_byte = marker_byte_position (w->start);
23715 height = WINDOW_TOTAL_LINES (w);
23717 /* If we decided that this buffer isn't suitable for line numbers,
23718 don't forget that too fast. */
23719 if (w->base_line_pos == -1)
23720 goto no_value;
23722 /* If the buffer is very big, don't waste time. */
23723 if (INTEGERP (Vline_number_display_limit)
23724 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23726 w->base_line_pos = 0;
23727 w->base_line_number = 0;
23728 goto no_value;
23731 if (w->base_line_number > 0
23732 && w->base_line_pos > 0
23733 && w->base_line_pos <= startpos)
23735 line = w->base_line_number;
23736 linepos = w->base_line_pos;
23737 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23739 else
23741 line = 1;
23742 linepos = BUF_BEGV (b);
23743 linepos_byte = BUF_BEGV_BYTE (b);
23746 /* Count lines from base line to window start position. */
23747 nlines = display_count_lines (linepos_byte,
23748 startpos_byte,
23749 startpos, &junk);
23751 topline = nlines + line;
23753 /* Determine a new base line, if the old one is too close
23754 or too far away, or if we did not have one.
23755 "Too close" means it's plausible a scroll-down would
23756 go back past it. */
23757 if (startpos == BUF_BEGV (b))
23759 w->base_line_number = topline;
23760 w->base_line_pos = BUF_BEGV (b);
23762 else if (nlines < height + 25 || nlines > height * 3 + 50
23763 || linepos == BUF_BEGV (b))
23765 ptrdiff_t limit = BUF_BEGV (b);
23766 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23767 ptrdiff_t position;
23768 ptrdiff_t distance =
23769 (height * 2 + 30) * line_number_display_limit_width;
23771 if (startpos - distance > limit)
23773 limit = startpos - distance;
23774 limit_byte = CHAR_TO_BYTE (limit);
23777 nlines = display_count_lines (startpos_byte,
23778 limit_byte,
23779 - (height * 2 + 30),
23780 &position);
23781 /* If we couldn't find the lines we wanted within
23782 line_number_display_limit_width chars per line,
23783 give up on line numbers for this window. */
23784 if (position == limit_byte && limit == startpos - distance)
23786 w->base_line_pos = -1;
23787 w->base_line_number = 0;
23788 goto no_value;
23791 w->base_line_number = topline - nlines;
23792 w->base_line_pos = BYTE_TO_CHAR (position);
23795 /* Now count lines from the start pos to point. */
23796 nlines = display_count_lines (startpos_byte,
23797 PT_BYTE, PT, &junk);
23799 /* Record that we did display the line number. */
23800 line_number_displayed = true;
23802 /* Make the string to show. */
23803 pint2str (decode_mode_spec_buf, width, topline + nlines);
23804 return decode_mode_spec_buf;
23805 no_value:
23807 char *p = decode_mode_spec_buf;
23808 int pad = width - 2;
23809 while (pad-- > 0)
23810 *p++ = ' ';
23811 *p++ = '?';
23812 *p++ = '?';
23813 *p = '\0';
23814 return decode_mode_spec_buf;
23817 break;
23819 case 'm':
23820 obj = BVAR (b, mode_name);
23821 break;
23823 case 'n':
23824 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23825 return " Narrow";
23826 break;
23828 case 'p':
23830 ptrdiff_t pos = marker_position (w->start);
23831 ptrdiff_t begv = BUF_BEGV (b);
23832 ptrdiff_t zv = BUF_ZV (b);
23834 if (w->window_end_pos <= BUF_Z (b) - zv)
23835 return pos <= begv ? "All" : "Bottom";
23836 else if (pos <= begv)
23837 return "Top";
23838 else
23840 sprintf (decode_mode_spec_buf, "%2d%%",
23841 percent99 (pos - begv, zv - begv));
23842 return decode_mode_spec_buf;
23846 /* Display percentage of size above the bottom of the screen. */
23847 case 'P':
23849 ptrdiff_t toppos = marker_position (w->start);
23850 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23851 ptrdiff_t begv = BUF_BEGV (b);
23852 ptrdiff_t zv = BUF_ZV (b);
23854 if (zv <= botpos)
23855 return toppos <= begv ? "All" : "Bottom";
23856 else
23858 sprintf (decode_mode_spec_buf,
23859 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23860 percent99 (botpos - begv, zv - begv));
23861 return decode_mode_spec_buf;
23865 case 's':
23866 /* status of process */
23867 obj = Fget_buffer_process (Fcurrent_buffer ());
23868 if (NILP (obj))
23869 return "no process";
23870 #ifndef MSDOS
23871 obj = Fsymbol_name (Fprocess_status (obj));
23872 #endif
23873 break;
23875 case '@':
23877 ptrdiff_t count = inhibit_garbage_collection ();
23878 Lisp_Object curdir = BVAR (current_buffer, directory);
23879 Lisp_Object val = Qnil;
23881 if (STRINGP (curdir))
23882 val = call1 (intern ("file-remote-p"), curdir);
23884 unbind_to (count, Qnil);
23886 if (NILP (val))
23887 return "-";
23888 else
23889 return "@";
23892 case 'z':
23893 /* coding-system (not including end-of-line format) */
23894 case 'Z':
23895 /* coding-system (including end-of-line type) */
23897 bool eol_flag = (c == 'Z');
23898 char *p = decode_mode_spec_buf;
23900 if (! FRAME_WINDOW_P (f))
23902 /* No need to mention EOL here--the terminal never needs
23903 to do EOL conversion. */
23904 p = decode_mode_spec_coding (CODING_ID_NAME
23905 (FRAME_KEYBOARD_CODING (f)->id),
23906 p, false);
23907 p = decode_mode_spec_coding (CODING_ID_NAME
23908 (FRAME_TERMINAL_CODING (f)->id),
23909 p, false);
23911 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23912 p, eol_flag);
23914 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23915 #ifdef subprocesses
23916 obj = Fget_buffer_process (Fcurrent_buffer ());
23917 if (PROCESSP (obj))
23919 p = decode_mode_spec_coding
23920 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23921 p = decode_mode_spec_coding
23922 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23924 #endif /* subprocesses */
23925 #endif /* false */
23926 *p = 0;
23927 return decode_mode_spec_buf;
23931 if (STRINGP (obj))
23933 *string = obj;
23934 return SSDATA (obj);
23936 else
23937 return "";
23941 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23942 means count lines back from START_BYTE. But don't go beyond
23943 LIMIT_BYTE. Return the number of lines thus found (always
23944 nonnegative).
23946 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23947 either the position COUNT lines after/before START_BYTE, if we
23948 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23949 COUNT lines. */
23951 static ptrdiff_t
23952 display_count_lines (ptrdiff_t start_byte,
23953 ptrdiff_t limit_byte, ptrdiff_t count,
23954 ptrdiff_t *byte_pos_ptr)
23956 register unsigned char *cursor;
23957 unsigned char *base;
23959 register ptrdiff_t ceiling;
23960 register unsigned char *ceiling_addr;
23961 ptrdiff_t orig_count = count;
23963 /* If we are not in selective display mode,
23964 check only for newlines. */
23965 bool selective_display
23966 = (!NILP (BVAR (current_buffer, selective_display))
23967 && !INTEGERP (BVAR (current_buffer, selective_display)));
23969 if (count > 0)
23971 while (start_byte < limit_byte)
23973 ceiling = BUFFER_CEILING_OF (start_byte);
23974 ceiling = min (limit_byte - 1, ceiling);
23975 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23976 base = (cursor = BYTE_POS_ADDR (start_byte));
23980 if (selective_display)
23982 while (*cursor != '\n' && *cursor != 015
23983 && ++cursor != ceiling_addr)
23984 continue;
23985 if (cursor == ceiling_addr)
23986 break;
23988 else
23990 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23991 if (! cursor)
23992 break;
23995 cursor++;
23997 if (--count == 0)
23999 start_byte += cursor - base;
24000 *byte_pos_ptr = start_byte;
24001 return orig_count;
24004 while (cursor < ceiling_addr);
24006 start_byte += ceiling_addr - base;
24009 else
24011 while (start_byte > limit_byte)
24013 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24014 ceiling = max (limit_byte, ceiling);
24015 ceiling_addr = BYTE_POS_ADDR (ceiling);
24016 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24017 while (true)
24019 if (selective_display)
24021 while (--cursor >= ceiling_addr
24022 && *cursor != '\n' && *cursor != 015)
24023 continue;
24024 if (cursor < ceiling_addr)
24025 break;
24027 else
24029 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24030 if (! cursor)
24031 break;
24034 if (++count == 0)
24036 start_byte += cursor - base + 1;
24037 *byte_pos_ptr = start_byte;
24038 /* When scanning backwards, we should
24039 not count the newline posterior to which we stop. */
24040 return - orig_count - 1;
24043 start_byte += ceiling_addr - base;
24047 *byte_pos_ptr = limit_byte;
24049 if (count < 0)
24050 return - orig_count + count;
24051 return orig_count - count;
24057 /***********************************************************************
24058 Displaying strings
24059 ***********************************************************************/
24061 /* Display a NUL-terminated string, starting with index START.
24063 If STRING is non-null, display that C string. Otherwise, the Lisp
24064 string LISP_STRING is displayed. There's a case that STRING is
24065 non-null and LISP_STRING is not nil. It means STRING is a string
24066 data of LISP_STRING. In that case, we display LISP_STRING while
24067 ignoring its text properties.
24069 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24070 FACE_STRING. Display STRING or LISP_STRING with the face at
24071 FACE_STRING_POS in FACE_STRING:
24073 Display the string in the environment given by IT, but use the
24074 standard display table, temporarily.
24076 FIELD_WIDTH is the minimum number of output glyphs to produce.
24077 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24078 with spaces. If STRING has more characters, more than FIELD_WIDTH
24079 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24081 PRECISION is the maximum number of characters to output from
24082 STRING. PRECISION < 0 means don't truncate the string.
24084 This is roughly equivalent to printf format specifiers:
24086 FIELD_WIDTH PRECISION PRINTF
24087 ----------------------------------------
24088 -1 -1 %s
24089 -1 10 %.10s
24090 10 -1 %10s
24091 20 10 %20.10s
24093 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24094 display them, and < 0 means obey the current buffer's value of
24095 enable_multibyte_characters.
24097 Value is the number of columns displayed. */
24099 static int
24100 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24101 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24102 int field_width, int precision, int max_x, int multibyte)
24104 int hpos_at_start = it->hpos;
24105 int saved_face_id = it->face_id;
24106 struct glyph_row *row = it->glyph_row;
24107 ptrdiff_t it_charpos;
24109 /* Initialize the iterator IT for iteration over STRING beginning
24110 with index START. */
24111 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24112 precision, field_width, multibyte);
24113 if (string && STRINGP (lisp_string))
24114 /* LISP_STRING is the one returned by decode_mode_spec. We should
24115 ignore its text properties. */
24116 it->stop_charpos = it->end_charpos;
24118 /* If displaying STRING, set up the face of the iterator from
24119 FACE_STRING, if that's given. */
24120 if (STRINGP (face_string))
24122 ptrdiff_t endptr;
24123 struct face *face;
24125 it->face_id
24126 = face_at_string_position (it->w, face_string, face_string_pos,
24127 0, &endptr, it->base_face_id, false);
24128 face = FACE_FROM_ID (it->f, it->face_id);
24129 it->face_box_p = face->box != FACE_NO_BOX;
24132 /* Set max_x to the maximum allowed X position. Don't let it go
24133 beyond the right edge of the window. */
24134 if (max_x <= 0)
24135 max_x = it->last_visible_x;
24136 else
24137 max_x = min (max_x, it->last_visible_x);
24139 /* Skip over display elements that are not visible. because IT->w is
24140 hscrolled. */
24141 if (it->current_x < it->first_visible_x)
24142 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24143 MOVE_TO_POS | MOVE_TO_X);
24145 row->ascent = it->max_ascent;
24146 row->height = it->max_ascent + it->max_descent;
24147 row->phys_ascent = it->max_phys_ascent;
24148 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24149 row->extra_line_spacing = it->max_extra_line_spacing;
24151 if (STRINGP (it->string))
24152 it_charpos = IT_STRING_CHARPOS (*it);
24153 else
24154 it_charpos = IT_CHARPOS (*it);
24156 /* This condition is for the case that we are called with current_x
24157 past last_visible_x. */
24158 while (it->current_x < max_x)
24160 int x_before, x, n_glyphs_before, i, nglyphs;
24162 /* Get the next display element. */
24163 if (!get_next_display_element (it))
24164 break;
24166 /* Produce glyphs. */
24167 x_before = it->current_x;
24168 n_glyphs_before = row->used[TEXT_AREA];
24169 PRODUCE_GLYPHS (it);
24171 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24172 i = 0;
24173 x = x_before;
24174 while (i < nglyphs)
24176 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24178 if (it->line_wrap != TRUNCATE
24179 && x + glyph->pixel_width > max_x)
24181 /* End of continued line or max_x reached. */
24182 if (CHAR_GLYPH_PADDING_P (*glyph))
24184 /* A wide character is unbreakable. */
24185 if (row->reversed_p)
24186 unproduce_glyphs (it, row->used[TEXT_AREA]
24187 - n_glyphs_before);
24188 row->used[TEXT_AREA] = n_glyphs_before;
24189 it->current_x = x_before;
24191 else
24193 if (row->reversed_p)
24194 unproduce_glyphs (it, row->used[TEXT_AREA]
24195 - (n_glyphs_before + i));
24196 row->used[TEXT_AREA] = n_glyphs_before + i;
24197 it->current_x = x;
24199 break;
24201 else if (x + glyph->pixel_width >= it->first_visible_x)
24203 /* Glyph is at least partially visible. */
24204 ++it->hpos;
24205 if (x < it->first_visible_x)
24206 row->x = x - it->first_visible_x;
24208 else
24210 /* Glyph is off the left margin of the display area.
24211 Should not happen. */
24212 emacs_abort ();
24215 row->ascent = max (row->ascent, it->max_ascent);
24216 row->height = max (row->height, it->max_ascent + it->max_descent);
24217 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24218 row->phys_height = max (row->phys_height,
24219 it->max_phys_ascent + it->max_phys_descent);
24220 row->extra_line_spacing = max (row->extra_line_spacing,
24221 it->max_extra_line_spacing);
24222 x += glyph->pixel_width;
24223 ++i;
24226 /* Stop if max_x reached. */
24227 if (i < nglyphs)
24228 break;
24230 /* Stop at line ends. */
24231 if (ITERATOR_AT_END_OF_LINE_P (it))
24233 it->continuation_lines_width = 0;
24234 break;
24237 set_iterator_to_next (it, true);
24238 if (STRINGP (it->string))
24239 it_charpos = IT_STRING_CHARPOS (*it);
24240 else
24241 it_charpos = IT_CHARPOS (*it);
24243 /* Stop if truncating at the right edge. */
24244 if (it->line_wrap == TRUNCATE
24245 && it->current_x >= it->last_visible_x)
24247 /* Add truncation mark, but don't do it if the line is
24248 truncated at a padding space. */
24249 if (it_charpos < it->string_nchars)
24251 if (!FRAME_WINDOW_P (it->f))
24253 int ii, n;
24255 if (it->current_x > it->last_visible_x)
24257 if (!row->reversed_p)
24259 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24260 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24261 break;
24263 else
24265 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24266 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24267 break;
24268 unproduce_glyphs (it, ii + 1);
24269 ii = row->used[TEXT_AREA] - (ii + 1);
24271 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24273 row->used[TEXT_AREA] = ii;
24274 produce_special_glyphs (it, IT_TRUNCATION);
24277 produce_special_glyphs (it, IT_TRUNCATION);
24279 row->truncated_on_right_p = true;
24281 break;
24285 /* Maybe insert a truncation at the left. */
24286 if (it->first_visible_x
24287 && it_charpos > 0)
24289 if (!FRAME_WINDOW_P (it->f)
24290 || (row->reversed_p
24291 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24292 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24293 insert_left_trunc_glyphs (it);
24294 row->truncated_on_left_p = true;
24297 it->face_id = saved_face_id;
24299 /* Value is number of columns displayed. */
24300 return it->hpos - hpos_at_start;
24305 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24306 appears as an element of LIST or as the car of an element of LIST.
24307 If PROPVAL is a list, compare each element against LIST in that
24308 way, and return 1/2 if any element of PROPVAL is found in LIST.
24309 Otherwise return 0. This function cannot quit.
24310 The return value is 2 if the text is invisible but with an ellipsis
24311 and 1 if it's invisible and without an ellipsis. */
24314 invisible_prop (Lisp_Object propval, Lisp_Object list)
24316 Lisp_Object tail, proptail;
24318 for (tail = list; CONSP (tail); tail = XCDR (tail))
24320 register Lisp_Object tem;
24321 tem = XCAR (tail);
24322 if (EQ (propval, tem))
24323 return 1;
24324 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24325 return NILP (XCDR (tem)) ? 1 : 2;
24328 if (CONSP (propval))
24330 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24332 Lisp_Object propelt;
24333 propelt = XCAR (proptail);
24334 for (tail = list; CONSP (tail); tail = XCDR (tail))
24336 register Lisp_Object tem;
24337 tem = XCAR (tail);
24338 if (EQ (propelt, tem))
24339 return 1;
24340 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24341 return NILP (XCDR (tem)) ? 1 : 2;
24346 return 0;
24349 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24350 doc: /* Non-nil if the property makes the text invisible.
24351 POS-OR-PROP can be a marker or number, in which case it is taken to be
24352 a position in the current buffer and the value of the `invisible' property
24353 is checked; or it can be some other value, which is then presumed to be the
24354 value of the `invisible' property of the text of interest.
24355 The non-nil value returned can be t for truly invisible text or something
24356 else if the text is replaced by an ellipsis. */)
24357 (Lisp_Object pos_or_prop)
24359 Lisp_Object prop
24360 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24361 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24362 : pos_or_prop);
24363 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24364 return (invis == 0 ? Qnil
24365 : invis == 1 ? Qt
24366 : make_number (invis));
24369 /* Calculate a width or height in pixels from a specification using
24370 the following elements:
24372 SPEC ::=
24373 NUM - a (fractional) multiple of the default font width/height
24374 (NUM) - specifies exactly NUM pixels
24375 UNIT - a fixed number of pixels, see below.
24376 ELEMENT - size of a display element in pixels, see below.
24377 (NUM . SPEC) - equals NUM * SPEC
24378 (+ SPEC SPEC ...) - add pixel values
24379 (- SPEC SPEC ...) - subtract pixel values
24380 (- SPEC) - negate pixel value
24382 NUM ::=
24383 INT or FLOAT - a number constant
24384 SYMBOL - use symbol's (buffer local) variable binding.
24386 UNIT ::=
24387 in - pixels per inch *)
24388 mm - pixels per 1/1000 meter *)
24389 cm - pixels per 1/100 meter *)
24390 width - width of current font in pixels.
24391 height - height of current font in pixels.
24393 *) using the ratio(s) defined in display-pixels-per-inch.
24395 ELEMENT ::=
24397 left-fringe - left fringe width in pixels
24398 right-fringe - right fringe width in pixels
24400 left-margin - left margin width in pixels
24401 right-margin - right margin width in pixels
24403 scroll-bar - scroll-bar area width in pixels
24405 Examples:
24407 Pixels corresponding to 5 inches:
24408 (5 . in)
24410 Total width of non-text areas on left side of window (if scroll-bar is on left):
24411 '(space :width (+ left-fringe left-margin scroll-bar))
24413 Align to first text column (in header line):
24414 '(space :align-to 0)
24416 Align to middle of text area minus half the width of variable `my-image'
24417 containing a loaded image:
24418 '(space :align-to (0.5 . (- text my-image)))
24420 Width of left margin minus width of 1 character in the default font:
24421 '(space :width (- left-margin 1))
24423 Width of left margin minus width of 2 characters in the current font:
24424 '(space :width (- left-margin (2 . width)))
24426 Center 1 character over left-margin (in header line):
24427 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24429 Different ways to express width of left fringe plus left margin minus one pixel:
24430 '(space :width (- (+ left-fringe left-margin) (1)))
24431 '(space :width (+ left-fringe left-margin (- (1))))
24432 '(space :width (+ left-fringe left-margin (-1)))
24436 static bool
24437 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24438 struct font *font, bool width_p, int *align_to)
24440 double pixels;
24442 # define OK_PIXELS(val) (*res = (val), true)
24443 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24445 if (NILP (prop))
24446 return OK_PIXELS (0);
24448 eassert (FRAME_LIVE_P (it->f));
24450 if (SYMBOLP (prop))
24452 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24454 char *unit = SSDATA (SYMBOL_NAME (prop));
24456 if (unit[0] == 'i' && unit[1] == 'n')
24457 pixels = 1.0;
24458 else if (unit[0] == 'm' && unit[1] == 'm')
24459 pixels = 25.4;
24460 else if (unit[0] == 'c' && unit[1] == 'm')
24461 pixels = 2.54;
24462 else
24463 pixels = 0;
24464 if (pixels > 0)
24466 double ppi = (width_p ? FRAME_RES_X (it->f)
24467 : FRAME_RES_Y (it->f));
24469 if (ppi > 0)
24470 return OK_PIXELS (ppi / pixels);
24471 return false;
24475 #ifdef HAVE_WINDOW_SYSTEM
24476 if (EQ (prop, Qheight))
24477 return OK_PIXELS (font
24478 ? normal_char_height (font, -1)
24479 : FRAME_LINE_HEIGHT (it->f));
24480 if (EQ (prop, Qwidth))
24481 return OK_PIXELS (font
24482 ? FONT_WIDTH (font)
24483 : FRAME_COLUMN_WIDTH (it->f));
24484 #else
24485 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24486 return OK_PIXELS (1);
24487 #endif
24489 if (EQ (prop, Qtext))
24490 return OK_PIXELS (width_p
24491 ? window_box_width (it->w, TEXT_AREA)
24492 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24494 if (align_to && *align_to < 0)
24496 *res = 0;
24497 if (EQ (prop, Qleft))
24498 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24499 if (EQ (prop, Qright))
24500 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24501 if (EQ (prop, Qcenter))
24502 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24503 + window_box_width (it->w, TEXT_AREA) / 2);
24504 if (EQ (prop, Qleft_fringe))
24505 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24506 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24507 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24508 if (EQ (prop, Qright_fringe))
24509 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24510 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24511 : window_box_right_offset (it->w, TEXT_AREA));
24512 if (EQ (prop, Qleft_margin))
24513 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24514 if (EQ (prop, Qright_margin))
24515 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24516 if (EQ (prop, Qscroll_bar))
24517 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24519 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24520 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24521 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24522 : 0)));
24524 else
24526 if (EQ (prop, Qleft_fringe))
24527 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24528 if (EQ (prop, Qright_fringe))
24529 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24530 if (EQ (prop, Qleft_margin))
24531 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24532 if (EQ (prop, Qright_margin))
24533 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24534 if (EQ (prop, Qscroll_bar))
24535 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24538 prop = buffer_local_value (prop, it->w->contents);
24539 if (EQ (prop, Qunbound))
24540 prop = Qnil;
24543 if (NUMBERP (prop))
24545 int base_unit = (width_p
24546 ? FRAME_COLUMN_WIDTH (it->f)
24547 : FRAME_LINE_HEIGHT (it->f));
24548 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24551 if (CONSP (prop))
24553 Lisp_Object car = XCAR (prop);
24554 Lisp_Object cdr = XCDR (prop);
24556 if (SYMBOLP (car))
24558 #ifdef HAVE_WINDOW_SYSTEM
24559 if (FRAME_WINDOW_P (it->f)
24560 && valid_image_p (prop))
24562 ptrdiff_t id = lookup_image (it->f, prop);
24563 struct image *img = IMAGE_FROM_ID (it->f, id);
24565 return OK_PIXELS (width_p ? img->width : img->height);
24567 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24569 /* TODO: Don't return dummy size. */
24570 return OK_PIXELS (100);
24572 #endif
24573 if (EQ (car, Qplus) || EQ (car, Qminus))
24575 bool first = true;
24576 double px;
24578 pixels = 0;
24579 while (CONSP (cdr))
24581 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24582 font, width_p, align_to))
24583 return false;
24584 if (first)
24585 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24586 else
24587 pixels += px;
24588 cdr = XCDR (cdr);
24590 if (EQ (car, Qminus))
24591 pixels = -pixels;
24592 return OK_PIXELS (pixels);
24595 car = buffer_local_value (car, it->w->contents);
24596 if (EQ (car, Qunbound))
24597 car = Qnil;
24600 if (NUMBERP (car))
24602 double fact;
24603 pixels = XFLOATINT (car);
24604 if (NILP (cdr))
24605 return OK_PIXELS (pixels);
24606 if (calc_pixel_width_or_height (&fact, it, cdr,
24607 font, width_p, align_to))
24608 return OK_PIXELS (pixels * fact);
24609 return false;
24612 return false;
24615 return false;
24618 void
24619 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24621 #ifdef HAVE_WINDOW_SYSTEM
24622 normal_char_ascent_descent (font, -1, ascent, descent);
24623 #else
24624 *ascent = 1;
24625 *descent = 0;
24626 #endif
24630 /***********************************************************************
24631 Glyph Display
24632 ***********************************************************************/
24634 #ifdef HAVE_WINDOW_SYSTEM
24636 #ifdef GLYPH_DEBUG
24638 void
24639 dump_glyph_string (struct glyph_string *s)
24641 fprintf (stderr, "glyph string\n");
24642 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24643 s->x, s->y, s->width, s->height);
24644 fprintf (stderr, " ybase = %d\n", s->ybase);
24645 fprintf (stderr, " hl = %d\n", s->hl);
24646 fprintf (stderr, " left overhang = %d, right = %d\n",
24647 s->left_overhang, s->right_overhang);
24648 fprintf (stderr, " nchars = %d\n", s->nchars);
24649 fprintf (stderr, " extends to end of line = %d\n",
24650 s->extends_to_end_of_line_p);
24651 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24652 fprintf (stderr, " bg width = %d\n", s->background_width);
24655 #endif /* GLYPH_DEBUG */
24657 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24658 of XChar2b structures for S; it can't be allocated in
24659 init_glyph_string because it must be allocated via `alloca'. W
24660 is the window on which S is drawn. ROW and AREA are the glyph row
24661 and area within the row from which S is constructed. START is the
24662 index of the first glyph structure covered by S. HL is a
24663 face-override for drawing S. */
24665 #ifdef HAVE_NTGUI
24666 #define OPTIONAL_HDC(hdc) HDC hdc,
24667 #define DECLARE_HDC(hdc) HDC hdc;
24668 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24669 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24670 #endif
24672 #ifndef OPTIONAL_HDC
24673 #define OPTIONAL_HDC(hdc)
24674 #define DECLARE_HDC(hdc)
24675 #define ALLOCATE_HDC(hdc, f)
24676 #define RELEASE_HDC(hdc, f)
24677 #endif
24679 static void
24680 init_glyph_string (struct glyph_string *s,
24681 OPTIONAL_HDC (hdc)
24682 XChar2b *char2b, struct window *w, struct glyph_row *row,
24683 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24685 memset (s, 0, sizeof *s);
24686 s->w = w;
24687 s->f = XFRAME (w->frame);
24688 #ifdef HAVE_NTGUI
24689 s->hdc = hdc;
24690 #endif
24691 s->display = FRAME_X_DISPLAY (s->f);
24692 s->char2b = char2b;
24693 s->hl = hl;
24694 s->row = row;
24695 s->area = area;
24696 s->first_glyph = row->glyphs[area] + start;
24697 s->height = row->height;
24698 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24699 s->ybase = s->y + row->ascent;
24703 /* Append the list of glyph strings with head H and tail T to the list
24704 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24706 static void
24707 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24708 struct glyph_string *h, struct glyph_string *t)
24710 if (h)
24712 if (*head)
24713 (*tail)->next = h;
24714 else
24715 *head = h;
24716 h->prev = *tail;
24717 *tail = t;
24722 /* Prepend the list of glyph strings with head H and tail T to the
24723 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24724 result. */
24726 static void
24727 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24728 struct glyph_string *h, struct glyph_string *t)
24730 if (h)
24732 if (*head)
24733 (*head)->prev = t;
24734 else
24735 *tail = t;
24736 t->next = *head;
24737 *head = h;
24742 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24743 Set *HEAD and *TAIL to the resulting list. */
24745 static void
24746 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24747 struct glyph_string *s)
24749 s->next = s->prev = NULL;
24750 append_glyph_string_lists (head, tail, s, s);
24754 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24755 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24756 make sure that X resources for the face returned are allocated.
24757 Value is a pointer to a realized face that is ready for display if
24758 DISPLAY_P. */
24760 static struct face *
24761 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24762 XChar2b *char2b, bool display_p)
24764 struct face *face = FACE_FROM_ID (f, face_id);
24765 unsigned code = 0;
24767 if (face->font)
24769 code = face->font->driver->encode_char (face->font, c);
24771 if (code == FONT_INVALID_CODE)
24772 code = 0;
24774 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24776 /* Make sure X resources of the face are allocated. */
24777 #ifdef HAVE_X_WINDOWS
24778 if (display_p)
24779 #endif
24781 eassert (face != NULL);
24782 prepare_face_for_display (f, face);
24785 return face;
24789 /* Get face and two-byte form of character glyph GLYPH on frame F.
24790 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24791 a pointer to a realized face that is ready for display. */
24793 static struct face *
24794 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24795 XChar2b *char2b)
24797 struct face *face;
24798 unsigned code = 0;
24800 eassert (glyph->type == CHAR_GLYPH);
24801 face = FACE_FROM_ID (f, glyph->face_id);
24803 /* Make sure X resources of the face are allocated. */
24804 prepare_face_for_display (f, face);
24806 if (face->font)
24808 if (CHAR_BYTE8_P (glyph->u.ch))
24809 code = CHAR_TO_BYTE8 (glyph->u.ch);
24810 else
24811 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24813 if (code == FONT_INVALID_CODE)
24814 code = 0;
24817 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24818 return face;
24822 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24823 Return true iff FONT has a glyph for C. */
24825 static bool
24826 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24828 unsigned code;
24830 if (CHAR_BYTE8_P (c))
24831 code = CHAR_TO_BYTE8 (c);
24832 else
24833 code = font->driver->encode_char (font, c);
24835 if (code == FONT_INVALID_CODE)
24836 return false;
24837 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24838 return true;
24842 /* Fill glyph string S with composition components specified by S->cmp.
24844 BASE_FACE is the base face of the composition.
24845 S->cmp_from is the index of the first component for S.
24847 OVERLAPS non-zero means S should draw the foreground only, and use
24848 its physical height for clipping. See also draw_glyphs.
24850 Value is the index of a component not in S. */
24852 static int
24853 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24854 int overlaps)
24856 int i;
24857 /* For all glyphs of this composition, starting at the offset
24858 S->cmp_from, until we reach the end of the definition or encounter a
24859 glyph that requires the different face, add it to S. */
24860 struct face *face;
24862 eassert (s);
24864 s->for_overlaps = overlaps;
24865 s->face = NULL;
24866 s->font = NULL;
24867 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24869 int c = COMPOSITION_GLYPH (s->cmp, i);
24871 /* TAB in a composition means display glyphs with padding space
24872 on the left or right. */
24873 if (c != '\t')
24875 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24876 -1, Qnil);
24878 face = get_char_face_and_encoding (s->f, c, face_id,
24879 s->char2b + i, true);
24880 if (face)
24882 if (! s->face)
24884 s->face = face;
24885 s->font = s->face->font;
24887 else if (s->face != face)
24888 break;
24891 ++s->nchars;
24893 s->cmp_to = i;
24895 if (s->face == NULL)
24897 s->face = base_face->ascii_face;
24898 s->font = s->face->font;
24901 /* All glyph strings for the same composition has the same width,
24902 i.e. the width set for the first component of the composition. */
24903 s->width = s->first_glyph->pixel_width;
24905 /* If the specified font could not be loaded, use the frame's
24906 default font, but record the fact that we couldn't load it in
24907 the glyph string so that we can draw rectangles for the
24908 characters of the glyph string. */
24909 if (s->font == NULL)
24911 s->font_not_found_p = true;
24912 s->font = FRAME_FONT (s->f);
24915 /* Adjust base line for subscript/superscript text. */
24916 s->ybase += s->first_glyph->voffset;
24918 return s->cmp_to;
24921 static int
24922 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24923 int start, int end, int overlaps)
24925 struct glyph *glyph, *last;
24926 Lisp_Object lgstring;
24927 int i;
24929 s->for_overlaps = overlaps;
24930 glyph = s->row->glyphs[s->area] + start;
24931 last = s->row->glyphs[s->area] + end;
24932 s->cmp_id = glyph->u.cmp.id;
24933 s->cmp_from = glyph->slice.cmp.from;
24934 s->cmp_to = glyph->slice.cmp.to + 1;
24935 s->face = FACE_FROM_ID (s->f, face_id);
24936 lgstring = composition_gstring_from_id (s->cmp_id);
24937 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24938 glyph++;
24939 while (glyph < last
24940 && glyph->u.cmp.automatic
24941 && glyph->u.cmp.id == s->cmp_id
24942 && s->cmp_to == glyph->slice.cmp.from)
24943 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24945 for (i = s->cmp_from; i < s->cmp_to; i++)
24947 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24948 unsigned code = LGLYPH_CODE (lglyph);
24950 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24952 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24953 return glyph - s->row->glyphs[s->area];
24957 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24958 See the comment of fill_glyph_string for arguments.
24959 Value is the index of the first glyph not in S. */
24962 static int
24963 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24964 int start, int end, int overlaps)
24966 struct glyph *glyph, *last;
24967 int voffset;
24969 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24970 s->for_overlaps = overlaps;
24971 glyph = s->row->glyphs[s->area] + start;
24972 last = s->row->glyphs[s->area] + end;
24973 voffset = glyph->voffset;
24974 s->face = FACE_FROM_ID (s->f, face_id);
24975 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24976 s->nchars = 1;
24977 s->width = glyph->pixel_width;
24978 glyph++;
24979 while (glyph < last
24980 && glyph->type == GLYPHLESS_GLYPH
24981 && glyph->voffset == voffset
24982 && glyph->face_id == face_id)
24984 s->nchars++;
24985 s->width += glyph->pixel_width;
24986 glyph++;
24988 s->ybase += voffset;
24989 return glyph - s->row->glyphs[s->area];
24993 /* Fill glyph string S from a sequence of character glyphs.
24995 FACE_ID is the face id of the string. START is the index of the
24996 first glyph to consider, END is the index of the last + 1.
24997 OVERLAPS non-zero means S should draw the foreground only, and use
24998 its physical height for clipping. See also draw_glyphs.
25000 Value is the index of the first glyph not in S. */
25002 static int
25003 fill_glyph_string (struct glyph_string *s, int face_id,
25004 int start, int end, int overlaps)
25006 struct glyph *glyph, *last;
25007 int voffset;
25008 bool glyph_not_available_p;
25010 eassert (s->f == XFRAME (s->w->frame));
25011 eassert (s->nchars == 0);
25012 eassert (start >= 0 && end > start);
25014 s->for_overlaps = overlaps;
25015 glyph = s->row->glyphs[s->area] + start;
25016 last = s->row->glyphs[s->area] + end;
25017 voffset = glyph->voffset;
25018 s->padding_p = glyph->padding_p;
25019 glyph_not_available_p = glyph->glyph_not_available_p;
25021 while (glyph < last
25022 && glyph->type == CHAR_GLYPH
25023 && glyph->voffset == voffset
25024 /* Same face id implies same font, nowadays. */
25025 && glyph->face_id == face_id
25026 && glyph->glyph_not_available_p == glyph_not_available_p)
25028 s->face = get_glyph_face_and_encoding (s->f, glyph,
25029 s->char2b + s->nchars);
25030 ++s->nchars;
25031 eassert (s->nchars <= end - start);
25032 s->width += glyph->pixel_width;
25033 if (glyph++->padding_p != s->padding_p)
25034 break;
25037 s->font = s->face->font;
25039 /* If the specified font could not be loaded, use the frame's font,
25040 but record the fact that we couldn't load it in
25041 S->font_not_found_p so that we can draw rectangles for the
25042 characters of the glyph string. */
25043 if (s->font == NULL || glyph_not_available_p)
25045 s->font_not_found_p = true;
25046 s->font = FRAME_FONT (s->f);
25049 /* Adjust base line for subscript/superscript text. */
25050 s->ybase += voffset;
25052 eassert (s->face && s->face->gc);
25053 return glyph - s->row->glyphs[s->area];
25057 /* Fill glyph string S from image glyph S->first_glyph. */
25059 static void
25060 fill_image_glyph_string (struct glyph_string *s)
25062 eassert (s->first_glyph->type == IMAGE_GLYPH);
25063 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25064 eassert (s->img);
25065 s->slice = s->first_glyph->slice.img;
25066 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25067 s->font = s->face->font;
25068 s->width = s->first_glyph->pixel_width;
25070 /* Adjust base line for subscript/superscript text. */
25071 s->ybase += s->first_glyph->voffset;
25075 #ifdef HAVE_XWIDGETS
25076 static void
25077 fill_xwidget_glyph_string (struct glyph_string *s)
25079 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25080 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25081 s->font = s->face->font;
25082 s->width = s->first_glyph->pixel_width;
25083 s->ybase += s->first_glyph->voffset;
25084 s->xwidget = s->first_glyph->u.xwidget;
25086 #endif
25087 /* Fill glyph string S from a sequence of stretch glyphs.
25089 START is the index of the first glyph to consider,
25090 END is the index of the last + 1.
25092 Value is the index of the first glyph not in S. */
25094 static int
25095 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25097 struct glyph *glyph, *last;
25098 int voffset, face_id;
25100 eassert (s->first_glyph->type == STRETCH_GLYPH);
25102 glyph = s->row->glyphs[s->area] + start;
25103 last = s->row->glyphs[s->area] + end;
25104 face_id = glyph->face_id;
25105 s->face = FACE_FROM_ID (s->f, face_id);
25106 s->font = s->face->font;
25107 s->width = glyph->pixel_width;
25108 s->nchars = 1;
25109 voffset = glyph->voffset;
25111 for (++glyph;
25112 (glyph < last
25113 && glyph->type == STRETCH_GLYPH
25114 && glyph->voffset == voffset
25115 && glyph->face_id == face_id);
25116 ++glyph)
25117 s->width += glyph->pixel_width;
25119 /* Adjust base line for subscript/superscript text. */
25120 s->ybase += voffset;
25122 /* The case that face->gc == 0 is handled when drawing the glyph
25123 string by calling prepare_face_for_display. */
25124 eassert (s->face);
25125 return glyph - s->row->glyphs[s->area];
25128 static struct font_metrics *
25129 get_per_char_metric (struct font *font, XChar2b *char2b)
25131 static struct font_metrics metrics;
25132 unsigned code;
25134 if (! font)
25135 return NULL;
25136 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25137 if (code == FONT_INVALID_CODE)
25138 return NULL;
25139 font->driver->text_extents (font, &code, 1, &metrics);
25140 return &metrics;
25143 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25144 for FONT. Values are taken from font-global ones, except for fonts
25145 that claim preposterously large values, but whose glyphs actually
25146 have reasonable dimensions. C is the character to use for metrics
25147 if the font-global values are too large; if C is negative, the
25148 function selects a default character. */
25149 static void
25150 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25152 *ascent = FONT_BASE (font);
25153 *descent = FONT_DESCENT (font);
25155 if (FONT_TOO_HIGH (font))
25157 XChar2b char2b;
25159 /* Get metrics of C, defaulting to a reasonably sized ASCII
25160 character. */
25161 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25163 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25165 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25167 /* We add 1 pixel to character dimensions as heuristics
25168 that produces nicer display, e.g. when the face has
25169 the box attribute. */
25170 *ascent = pcm->ascent + 1;
25171 *descent = pcm->descent + 1;
25177 /* A subroutine that computes a reasonable "normal character height"
25178 for fonts that claim preposterously large vertical dimensions, but
25179 whose glyphs are actually reasonably sized. C is the character
25180 whose metrics to use for those fonts, or -1 for default
25181 character. */
25182 static int
25183 normal_char_height (struct font *font, int c)
25185 int ascent, descent;
25187 normal_char_ascent_descent (font, c, &ascent, &descent);
25189 return ascent + descent;
25192 /* EXPORT for RIF:
25193 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25194 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25195 assumed to be zero. */
25197 void
25198 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25200 *left = *right = 0;
25202 if (glyph->type == CHAR_GLYPH)
25204 XChar2b char2b;
25205 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25206 if (face->font)
25208 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25209 if (pcm)
25211 if (pcm->rbearing > pcm->width)
25212 *right = pcm->rbearing - pcm->width;
25213 if (pcm->lbearing < 0)
25214 *left = -pcm->lbearing;
25218 else if (glyph->type == COMPOSITE_GLYPH)
25220 if (! glyph->u.cmp.automatic)
25222 struct composition *cmp = composition_table[glyph->u.cmp.id];
25224 if (cmp->rbearing > cmp->pixel_width)
25225 *right = cmp->rbearing - cmp->pixel_width;
25226 if (cmp->lbearing < 0)
25227 *left = - cmp->lbearing;
25229 else
25231 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25232 struct font_metrics metrics;
25234 composition_gstring_width (gstring, glyph->slice.cmp.from,
25235 glyph->slice.cmp.to + 1, &metrics);
25236 if (metrics.rbearing > metrics.width)
25237 *right = metrics.rbearing - metrics.width;
25238 if (metrics.lbearing < 0)
25239 *left = - metrics.lbearing;
25245 /* Return the index of the first glyph preceding glyph string S that
25246 is overwritten by S because of S's left overhang. Value is -1
25247 if no glyphs are overwritten. */
25249 static int
25250 left_overwritten (struct glyph_string *s)
25252 int k;
25254 if (s->left_overhang)
25256 int x = 0, i;
25257 struct glyph *glyphs = s->row->glyphs[s->area];
25258 int first = s->first_glyph - glyphs;
25260 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25261 x -= glyphs[i].pixel_width;
25263 k = i + 1;
25265 else
25266 k = -1;
25268 return k;
25272 /* Return the index of the first glyph preceding glyph string S that
25273 is overwriting S because of its right overhang. Value is -1 if no
25274 glyph in front of S overwrites S. */
25276 static int
25277 left_overwriting (struct glyph_string *s)
25279 int i, k, x;
25280 struct glyph *glyphs = s->row->glyphs[s->area];
25281 int first = s->first_glyph - glyphs;
25283 k = -1;
25284 x = 0;
25285 for (i = first - 1; i >= 0; --i)
25287 int left, right;
25288 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25289 if (x + right > 0)
25290 k = i;
25291 x -= glyphs[i].pixel_width;
25294 return k;
25298 /* Return the index of the last glyph following glyph string S that is
25299 overwritten by S because of S's right overhang. Value is -1 if
25300 no such glyph is found. */
25302 static int
25303 right_overwritten (struct glyph_string *s)
25305 int k = -1;
25307 if (s->right_overhang)
25309 int x = 0, i;
25310 struct glyph *glyphs = s->row->glyphs[s->area];
25311 int first = (s->first_glyph - glyphs
25312 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25313 int end = s->row->used[s->area];
25315 for (i = first; i < end && s->right_overhang > x; ++i)
25316 x += glyphs[i].pixel_width;
25318 k = i;
25321 return k;
25325 /* Return the index of the last glyph following glyph string S that
25326 overwrites S because of its left overhang. Value is negative
25327 if no such glyph is found. */
25329 static int
25330 right_overwriting (struct glyph_string *s)
25332 int i, k, x;
25333 int end = s->row->used[s->area];
25334 struct glyph *glyphs = s->row->glyphs[s->area];
25335 int first = (s->first_glyph - glyphs
25336 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25338 k = -1;
25339 x = 0;
25340 for (i = first; i < end; ++i)
25342 int left, right;
25343 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25344 if (x - left < 0)
25345 k = i;
25346 x += glyphs[i].pixel_width;
25349 return k;
25353 /* Set background width of glyph string S. START is the index of the
25354 first glyph following S. LAST_X is the right-most x-position + 1
25355 in the drawing area. */
25357 static void
25358 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25360 /* If the face of this glyph string has to be drawn to the end of
25361 the drawing area, set S->extends_to_end_of_line_p. */
25363 if (start == s->row->used[s->area]
25364 && ((s->row->fill_line_p
25365 && (s->hl == DRAW_NORMAL_TEXT
25366 || s->hl == DRAW_IMAGE_RAISED
25367 || s->hl == DRAW_IMAGE_SUNKEN))
25368 || s->hl == DRAW_MOUSE_FACE))
25369 s->extends_to_end_of_line_p = true;
25371 /* If S extends its face to the end of the line, set its
25372 background_width to the distance to the right edge of the drawing
25373 area. */
25374 if (s->extends_to_end_of_line_p)
25375 s->background_width = last_x - s->x + 1;
25376 else
25377 s->background_width = s->width;
25381 /* Compute overhangs and x-positions for glyph string S and its
25382 predecessors, or successors. X is the starting x-position for S.
25383 BACKWARD_P means process predecessors. */
25385 static void
25386 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25388 if (backward_p)
25390 while (s)
25392 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25393 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25394 x -= s->width;
25395 s->x = x;
25396 s = s->prev;
25399 else
25401 while (s)
25403 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25404 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25405 s->x = x;
25406 x += s->width;
25407 s = s->next;
25414 /* The following macros are only called from draw_glyphs below.
25415 They reference the following parameters of that function directly:
25416 `w', `row', `area', and `overlap_p'
25417 as well as the following local variables:
25418 `s', `f', and `hdc' (in W32) */
25420 #ifdef HAVE_NTGUI
25421 /* On W32, silently add local `hdc' variable to argument list of
25422 init_glyph_string. */
25423 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25424 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25425 #else
25426 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25427 init_glyph_string (s, char2b, w, row, area, start, hl)
25428 #endif
25430 /* Add a glyph string for a stretch glyph to the list of strings
25431 between HEAD and TAIL. START is the index of the stretch glyph in
25432 row area AREA of glyph row ROW. END is the index of the last glyph
25433 in that glyph row area. X is the current output position assigned
25434 to the new glyph string constructed. HL overrides that face of the
25435 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25436 is the right-most x-position of the drawing area. */
25438 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25439 and below -- keep them on one line. */
25440 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25441 do \
25443 s = alloca (sizeof *s); \
25444 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25445 START = fill_stretch_glyph_string (s, START, END); \
25446 append_glyph_string (&HEAD, &TAIL, s); \
25447 s->x = (X); \
25449 while (false)
25452 /* Add a glyph string for an image glyph to the list of strings
25453 between HEAD and TAIL. START is the index of the image glyph in
25454 row area AREA of glyph row ROW. END is the index of the last glyph
25455 in that glyph row area. X is the current output position assigned
25456 to the new glyph string constructed. HL overrides that face of the
25457 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25458 is the right-most x-position of the drawing area. */
25460 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25461 do \
25463 s = alloca (sizeof *s); \
25464 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25465 fill_image_glyph_string (s); \
25466 append_glyph_string (&HEAD, &TAIL, s); \
25467 ++START; \
25468 s->x = (X); \
25470 while (false)
25472 #ifndef HAVE_XWIDGETS
25473 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25474 eassume (false)
25475 #else
25476 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25477 do \
25479 s = alloca (sizeof *s); \
25480 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25481 fill_xwidget_glyph_string (s); \
25482 append_glyph_string (&(HEAD), &(TAIL), s); \
25483 ++(START); \
25484 s->x = (X); \
25486 while (false)
25487 #endif
25489 /* Add a glyph string for a sequence of character glyphs to the list
25490 of strings between HEAD and TAIL. START is the index of the first
25491 glyph in row area AREA of glyph row ROW that is part of the new
25492 glyph string. END is the index of the last glyph in that glyph row
25493 area. X is the current output position assigned to the new glyph
25494 string constructed. HL overrides that face of the glyph; e.g. it
25495 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25496 right-most x-position of the drawing area. */
25498 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25499 do \
25501 int face_id; \
25502 XChar2b *char2b; \
25504 face_id = (row)->glyphs[area][START].face_id; \
25506 s = alloca (sizeof *s); \
25507 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25508 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25509 append_glyph_string (&HEAD, &TAIL, s); \
25510 s->x = (X); \
25511 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25513 while (false)
25516 /* Add a glyph string for a composite sequence to the list of strings
25517 between HEAD and TAIL. START is the index of the first glyph in
25518 row area AREA of glyph row ROW that is part of the new glyph
25519 string. END is the index of the last glyph in that glyph row area.
25520 X is the current output position assigned to the new glyph string
25521 constructed. HL overrides that face of the glyph; e.g. it is
25522 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25523 x-position of the drawing area. */
25525 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25526 do { \
25527 int face_id = (row)->glyphs[area][START].face_id; \
25528 struct face *base_face = FACE_FROM_ID (f, face_id); \
25529 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25530 struct composition *cmp = composition_table[cmp_id]; \
25531 XChar2b *char2b; \
25532 struct glyph_string *first_s = NULL; \
25533 int n; \
25535 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25537 /* Make glyph_strings for each glyph sequence that is drawable by \
25538 the same face, and append them to HEAD/TAIL. */ \
25539 for (n = 0; n < cmp->glyph_len;) \
25541 s = alloca (sizeof *s); \
25542 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25543 append_glyph_string (&(HEAD), &(TAIL), s); \
25544 s->cmp = cmp; \
25545 s->cmp_from = n; \
25546 s->x = (X); \
25547 if (n == 0) \
25548 first_s = s; \
25549 n = fill_composite_glyph_string (s, base_face, overlaps); \
25552 ++START; \
25553 s = first_s; \
25554 } while (false)
25557 /* Add a glyph string for a glyph-string sequence to the list of strings
25558 between HEAD and TAIL. */
25560 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25561 do { \
25562 int face_id; \
25563 XChar2b *char2b; \
25564 Lisp_Object gstring; \
25566 face_id = (row)->glyphs[area][START].face_id; \
25567 gstring = (composition_gstring_from_id \
25568 ((row)->glyphs[area][START].u.cmp.id)); \
25569 s = alloca (sizeof *s); \
25570 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25571 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25572 append_glyph_string (&(HEAD), &(TAIL), s); \
25573 s->x = (X); \
25574 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25575 } while (false)
25578 /* Add a glyph string for a sequence of glyphless character's glyphs
25579 to the list of strings between HEAD and TAIL. The meanings of
25580 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25582 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25583 do \
25585 int face_id; \
25587 face_id = (row)->glyphs[area][START].face_id; \
25589 s = alloca (sizeof *s); \
25590 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25591 append_glyph_string (&HEAD, &TAIL, s); \
25592 s->x = (X); \
25593 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25594 overlaps); \
25596 while (false)
25599 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25600 of AREA of glyph row ROW on window W between indices START and END.
25601 HL overrides the face for drawing glyph strings, e.g. it is
25602 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25603 x-positions of the drawing area.
25605 This is an ugly monster macro construct because we must use alloca
25606 to allocate glyph strings (because draw_glyphs can be called
25607 asynchronously). */
25609 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25610 do \
25612 HEAD = TAIL = NULL; \
25613 while (START < END) \
25615 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25616 switch (first_glyph->type) \
25618 case CHAR_GLYPH: \
25619 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25620 HL, X, LAST_X); \
25621 break; \
25623 case COMPOSITE_GLYPH: \
25624 if (first_glyph->u.cmp.automatic) \
25625 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25626 HL, X, LAST_X); \
25627 else \
25628 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25629 HL, X, LAST_X); \
25630 break; \
25632 case STRETCH_GLYPH: \
25633 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25634 HL, X, LAST_X); \
25635 break; \
25637 case IMAGE_GLYPH: \
25638 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25639 HL, X, LAST_X); \
25640 break;
25642 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25643 case XWIDGET_GLYPH: \
25644 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25645 HL, X, LAST_X); \
25646 break;
25648 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25649 case GLYPHLESS_GLYPH: \
25650 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25651 HL, X, LAST_X); \
25652 break; \
25654 default: \
25655 emacs_abort (); \
25658 if (s) \
25660 set_glyph_string_background_width (s, START, LAST_X); \
25661 (X) += s->width; \
25664 } while (false)
25667 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25668 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25669 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25670 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25673 /* Draw glyphs between START and END in AREA of ROW on window W,
25674 starting at x-position X. X is relative to AREA in W. HL is a
25675 face-override with the following meaning:
25677 DRAW_NORMAL_TEXT draw normally
25678 DRAW_CURSOR draw in cursor face
25679 DRAW_MOUSE_FACE draw in mouse face.
25680 DRAW_INVERSE_VIDEO draw in mode line face
25681 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25682 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25684 If OVERLAPS is non-zero, draw only the foreground of characters and
25685 clip to the physical height of ROW. Non-zero value also defines
25686 the overlapping part to be drawn:
25688 OVERLAPS_PRED overlap with preceding rows
25689 OVERLAPS_SUCC overlap with succeeding rows
25690 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25691 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25693 Value is the x-position reached, relative to AREA of W. */
25695 static int
25696 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25697 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25698 enum draw_glyphs_face hl, int overlaps)
25700 struct glyph_string *head, *tail;
25701 struct glyph_string *s;
25702 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25703 int i, j, x_reached, last_x, area_left = 0;
25704 struct frame *f = XFRAME (WINDOW_FRAME (w));
25705 DECLARE_HDC (hdc);
25707 ALLOCATE_HDC (hdc, f);
25709 /* Let's rather be paranoid than getting a SEGV. */
25710 end = min (end, row->used[area]);
25711 start = clip_to_bounds (0, start, end);
25713 /* Translate X to frame coordinates. Set last_x to the right
25714 end of the drawing area. */
25715 if (row->full_width_p)
25717 /* X is relative to the left edge of W, without scroll bars
25718 or fringes. */
25719 area_left = WINDOW_LEFT_EDGE_X (w);
25720 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25721 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25723 else
25725 area_left = window_box_left (w, area);
25726 last_x = area_left + window_box_width (w, area);
25728 x += area_left;
25730 /* Build a doubly-linked list of glyph_string structures between
25731 head and tail from what we have to draw. Note that the macro
25732 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25733 the reason we use a separate variable `i'. */
25734 i = start;
25735 USE_SAFE_ALLOCA;
25736 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25737 if (tail)
25738 x_reached = tail->x + tail->background_width;
25739 else
25740 x_reached = x;
25742 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25743 the row, redraw some glyphs in front or following the glyph
25744 strings built above. */
25745 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25747 struct glyph_string *h, *t;
25748 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25749 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25750 bool check_mouse_face = false;
25751 int dummy_x = 0;
25753 /* If mouse highlighting is on, we may need to draw adjacent
25754 glyphs using mouse-face highlighting. */
25755 if (area == TEXT_AREA && row->mouse_face_p
25756 && hlinfo->mouse_face_beg_row >= 0
25757 && hlinfo->mouse_face_end_row >= 0)
25759 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25761 if (row_vpos >= hlinfo->mouse_face_beg_row
25762 && row_vpos <= hlinfo->mouse_face_end_row)
25764 check_mouse_face = true;
25765 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25766 ? hlinfo->mouse_face_beg_col : 0;
25767 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25768 ? hlinfo->mouse_face_end_col
25769 : row->used[TEXT_AREA];
25773 /* Compute overhangs for all glyph strings. */
25774 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25775 for (s = head; s; s = s->next)
25776 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25778 /* Prepend glyph strings for glyphs in front of the first glyph
25779 string that are overwritten because of the first glyph
25780 string's left overhang. The background of all strings
25781 prepended must be drawn because the first glyph string
25782 draws over it. */
25783 i = left_overwritten (head);
25784 if (i >= 0)
25786 enum draw_glyphs_face overlap_hl;
25788 /* If this row contains mouse highlighting, attempt to draw
25789 the overlapped glyphs with the correct highlight. This
25790 code fails if the overlap encompasses more than one glyph
25791 and mouse-highlight spans only some of these glyphs.
25792 However, making it work perfectly involves a lot more
25793 code, and I don't know if the pathological case occurs in
25794 practice, so we'll stick to this for now. --- cyd */
25795 if (check_mouse_face
25796 && mouse_beg_col < start && mouse_end_col > i)
25797 overlap_hl = DRAW_MOUSE_FACE;
25798 else
25799 overlap_hl = DRAW_NORMAL_TEXT;
25801 if (hl != overlap_hl)
25802 clip_head = head;
25803 j = i;
25804 BUILD_GLYPH_STRINGS (j, start, h, t,
25805 overlap_hl, dummy_x, last_x);
25806 start = i;
25807 compute_overhangs_and_x (t, head->x, true);
25808 prepend_glyph_string_lists (&head, &tail, h, t);
25809 if (clip_head == NULL)
25810 clip_head = head;
25813 /* Prepend glyph strings for glyphs in front of the first glyph
25814 string that overwrite that glyph string because of their
25815 right overhang. For these strings, only the foreground must
25816 be drawn, because it draws over the glyph string at `head'.
25817 The background must not be drawn because this would overwrite
25818 right overhangs of preceding glyphs for which no glyph
25819 strings exist. */
25820 i = left_overwriting (head);
25821 if (i >= 0)
25823 enum draw_glyphs_face overlap_hl;
25825 if (check_mouse_face
25826 && mouse_beg_col < start && mouse_end_col > i)
25827 overlap_hl = DRAW_MOUSE_FACE;
25828 else
25829 overlap_hl = DRAW_NORMAL_TEXT;
25831 if (hl == overlap_hl || clip_head == NULL)
25832 clip_head = head;
25833 BUILD_GLYPH_STRINGS (i, start, h, t,
25834 overlap_hl, dummy_x, last_x);
25835 for (s = h; s; s = s->next)
25836 s->background_filled_p = true;
25837 compute_overhangs_and_x (t, head->x, true);
25838 prepend_glyph_string_lists (&head, &tail, h, t);
25841 /* Append glyphs strings for glyphs following the last glyph
25842 string tail that are overwritten by tail. The background of
25843 these strings has to be drawn because tail's foreground draws
25844 over it. */
25845 i = right_overwritten (tail);
25846 if (i >= 0)
25848 enum draw_glyphs_face overlap_hl;
25850 if (check_mouse_face
25851 && mouse_beg_col < i && mouse_end_col > end)
25852 overlap_hl = DRAW_MOUSE_FACE;
25853 else
25854 overlap_hl = DRAW_NORMAL_TEXT;
25856 if (hl != overlap_hl)
25857 clip_tail = tail;
25858 BUILD_GLYPH_STRINGS (end, i, h, t,
25859 overlap_hl, x, last_x);
25860 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25861 we don't have `end = i;' here. */
25862 compute_overhangs_and_x (h, tail->x + tail->width, false);
25863 append_glyph_string_lists (&head, &tail, h, t);
25864 if (clip_tail == NULL)
25865 clip_tail = tail;
25868 /* Append glyph strings for glyphs following the last glyph
25869 string tail that overwrite tail. The foreground of such
25870 glyphs has to be drawn because it writes into the background
25871 of tail. The background must not be drawn because it could
25872 paint over the foreground of following glyphs. */
25873 i = right_overwriting (tail);
25874 if (i >= 0)
25876 enum draw_glyphs_face overlap_hl;
25877 if (check_mouse_face
25878 && mouse_beg_col < i && mouse_end_col > end)
25879 overlap_hl = DRAW_MOUSE_FACE;
25880 else
25881 overlap_hl = DRAW_NORMAL_TEXT;
25883 if (hl == overlap_hl || clip_tail == NULL)
25884 clip_tail = tail;
25885 i++; /* We must include the Ith glyph. */
25886 BUILD_GLYPH_STRINGS (end, i, h, t,
25887 overlap_hl, x, last_x);
25888 for (s = h; s; s = s->next)
25889 s->background_filled_p = true;
25890 compute_overhangs_and_x (h, tail->x + tail->width, false);
25891 append_glyph_string_lists (&head, &tail, h, t);
25893 if (clip_head || clip_tail)
25894 for (s = head; s; s = s->next)
25896 s->clip_head = clip_head;
25897 s->clip_tail = clip_tail;
25901 /* Draw all strings. */
25902 for (s = head; s; s = s->next)
25903 FRAME_RIF (f)->draw_glyph_string (s);
25905 #ifndef HAVE_NS
25906 /* When focus a sole frame and move horizontally, this clears on_p
25907 causing a failure to erase prev cursor position. */
25908 if (area == TEXT_AREA
25909 && !row->full_width_p
25910 /* When drawing overlapping rows, only the glyph strings'
25911 foreground is drawn, which doesn't erase a cursor
25912 completely. */
25913 && !overlaps)
25915 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25916 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25917 : (tail ? tail->x + tail->background_width : x));
25918 x0 -= area_left;
25919 x1 -= area_left;
25921 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25922 row->y, MATRIX_ROW_BOTTOM_Y (row));
25924 #endif
25926 /* Value is the x-position up to which drawn, relative to AREA of W.
25927 This doesn't include parts drawn because of overhangs. */
25928 if (row->full_width_p)
25929 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25930 else
25931 x_reached -= area_left;
25933 RELEASE_HDC (hdc, f);
25935 SAFE_FREE ();
25936 return x_reached;
25939 /* Expand row matrix if too narrow. Don't expand if area
25940 is not present. */
25942 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25944 if (!it->f->fonts_changed \
25945 && (it->glyph_row->glyphs[area] \
25946 < it->glyph_row->glyphs[area + 1])) \
25948 it->w->ncols_scale_factor++; \
25949 it->f->fonts_changed = true; \
25953 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25954 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25956 static void
25957 append_glyph (struct it *it)
25959 struct glyph *glyph;
25960 enum glyph_row_area area = it->area;
25962 eassert (it->glyph_row);
25963 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25965 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25966 if (glyph < it->glyph_row->glyphs[area + 1])
25968 /* If the glyph row is reversed, we need to prepend the glyph
25969 rather than append it. */
25970 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25972 struct glyph *g;
25974 /* Make room for the additional glyph. */
25975 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25976 g[1] = *g;
25977 glyph = it->glyph_row->glyphs[area];
25979 glyph->charpos = CHARPOS (it->position);
25980 glyph->object = it->object;
25981 if (it->pixel_width > 0)
25983 eassert (it->pixel_width <= SHRT_MAX);
25984 glyph->pixel_width = it->pixel_width;
25985 glyph->padding_p = false;
25987 else
25989 /* Assure at least 1-pixel width. Otherwise, cursor can't
25990 be displayed correctly. */
25991 glyph->pixel_width = 1;
25992 glyph->padding_p = true;
25994 glyph->ascent = it->ascent;
25995 glyph->descent = it->descent;
25996 glyph->voffset = it->voffset;
25997 glyph->type = CHAR_GLYPH;
25998 glyph->avoid_cursor_p = it->avoid_cursor_p;
25999 glyph->multibyte_p = it->multibyte_p;
26000 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26002 /* In R2L rows, the left and the right box edges need to be
26003 drawn in reverse direction. */
26004 glyph->right_box_line_p = it->start_of_box_run_p;
26005 glyph->left_box_line_p = it->end_of_box_run_p;
26007 else
26009 glyph->left_box_line_p = it->start_of_box_run_p;
26010 glyph->right_box_line_p = it->end_of_box_run_p;
26012 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26013 || it->phys_descent > it->descent);
26014 glyph->glyph_not_available_p = it->glyph_not_available_p;
26015 glyph->face_id = it->face_id;
26016 glyph->u.ch = it->char_to_display;
26017 glyph->slice.img = null_glyph_slice;
26018 glyph->font_type = FONT_TYPE_UNKNOWN;
26019 if (it->bidi_p)
26021 glyph->resolved_level = it->bidi_it.resolved_level;
26022 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26023 glyph->bidi_type = it->bidi_it.type;
26025 else
26027 glyph->resolved_level = 0;
26028 glyph->bidi_type = UNKNOWN_BT;
26030 ++it->glyph_row->used[area];
26032 else
26033 IT_EXPAND_MATRIX_WIDTH (it, area);
26036 /* Store one glyph for the composition IT->cmp_it.id in
26037 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26038 non-null. */
26040 static void
26041 append_composite_glyph (struct it *it)
26043 struct glyph *glyph;
26044 enum glyph_row_area area = it->area;
26046 eassert (it->glyph_row);
26048 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26049 if (glyph < it->glyph_row->glyphs[area + 1])
26051 /* If the glyph row is reversed, we need to prepend the glyph
26052 rather than append it. */
26053 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26055 struct glyph *g;
26057 /* Make room for the new glyph. */
26058 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26059 g[1] = *g;
26060 glyph = it->glyph_row->glyphs[it->area];
26062 glyph->charpos = it->cmp_it.charpos;
26063 glyph->object = it->object;
26064 eassert (it->pixel_width <= SHRT_MAX);
26065 glyph->pixel_width = it->pixel_width;
26066 glyph->ascent = it->ascent;
26067 glyph->descent = it->descent;
26068 glyph->voffset = it->voffset;
26069 glyph->type = COMPOSITE_GLYPH;
26070 if (it->cmp_it.ch < 0)
26072 glyph->u.cmp.automatic = false;
26073 glyph->u.cmp.id = it->cmp_it.id;
26074 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26076 else
26078 glyph->u.cmp.automatic = true;
26079 glyph->u.cmp.id = it->cmp_it.id;
26080 glyph->slice.cmp.from = it->cmp_it.from;
26081 glyph->slice.cmp.to = it->cmp_it.to - 1;
26083 glyph->avoid_cursor_p = it->avoid_cursor_p;
26084 glyph->multibyte_p = it->multibyte_p;
26085 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26087 /* In R2L rows, the left and the right box edges need to be
26088 drawn in reverse direction. */
26089 glyph->right_box_line_p = it->start_of_box_run_p;
26090 glyph->left_box_line_p = it->end_of_box_run_p;
26092 else
26094 glyph->left_box_line_p = it->start_of_box_run_p;
26095 glyph->right_box_line_p = it->end_of_box_run_p;
26097 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26098 || it->phys_descent > it->descent);
26099 glyph->padding_p = false;
26100 glyph->glyph_not_available_p = false;
26101 glyph->face_id = it->face_id;
26102 glyph->font_type = FONT_TYPE_UNKNOWN;
26103 if (it->bidi_p)
26105 glyph->resolved_level = it->bidi_it.resolved_level;
26106 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26107 glyph->bidi_type = it->bidi_it.type;
26109 ++it->glyph_row->used[area];
26111 else
26112 IT_EXPAND_MATRIX_WIDTH (it, area);
26116 /* Change IT->ascent and IT->height according to the setting of
26117 IT->voffset. */
26119 static void
26120 take_vertical_position_into_account (struct it *it)
26122 if (it->voffset)
26124 if (it->voffset < 0)
26125 /* Increase the ascent so that we can display the text higher
26126 in the line. */
26127 it->ascent -= it->voffset;
26128 else
26129 /* Increase the descent so that we can display the text lower
26130 in the line. */
26131 it->descent += it->voffset;
26136 /* Produce glyphs/get display metrics for the image IT is loaded with.
26137 See the description of struct display_iterator in dispextern.h for
26138 an overview of struct display_iterator. */
26140 static void
26141 produce_image_glyph (struct it *it)
26143 struct image *img;
26144 struct face *face;
26145 int glyph_ascent, crop;
26146 struct glyph_slice slice;
26148 eassert (it->what == IT_IMAGE);
26150 face = FACE_FROM_ID (it->f, it->face_id);
26151 /* Make sure X resources of the face is loaded. */
26152 prepare_face_for_display (it->f, face);
26154 if (it->image_id < 0)
26156 /* Fringe bitmap. */
26157 it->ascent = it->phys_ascent = 0;
26158 it->descent = it->phys_descent = 0;
26159 it->pixel_width = 0;
26160 it->nglyphs = 0;
26161 return;
26164 img = IMAGE_FROM_ID (it->f, it->image_id);
26165 /* Make sure X resources of the image is loaded. */
26166 prepare_image_for_display (it->f, img);
26168 slice.x = slice.y = 0;
26169 slice.width = img->width;
26170 slice.height = img->height;
26172 if (INTEGERP (it->slice.x))
26173 slice.x = XINT (it->slice.x);
26174 else if (FLOATP (it->slice.x))
26175 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26177 if (INTEGERP (it->slice.y))
26178 slice.y = XINT (it->slice.y);
26179 else if (FLOATP (it->slice.y))
26180 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26182 if (INTEGERP (it->slice.width))
26183 slice.width = XINT (it->slice.width);
26184 else if (FLOATP (it->slice.width))
26185 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26187 if (INTEGERP (it->slice.height))
26188 slice.height = XINT (it->slice.height);
26189 else if (FLOATP (it->slice.height))
26190 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26192 if (slice.x >= img->width)
26193 slice.x = img->width;
26194 if (slice.y >= img->height)
26195 slice.y = img->height;
26196 if (slice.x + slice.width >= img->width)
26197 slice.width = img->width - slice.x;
26198 if (slice.y + slice.height > img->height)
26199 slice.height = img->height - slice.y;
26201 if (slice.width == 0 || slice.height == 0)
26202 return;
26204 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26206 it->descent = slice.height - glyph_ascent;
26207 if (slice.y == 0)
26208 it->descent += img->vmargin;
26209 if (slice.y + slice.height == img->height)
26210 it->descent += img->vmargin;
26211 it->phys_descent = it->descent;
26213 it->pixel_width = slice.width;
26214 if (slice.x == 0)
26215 it->pixel_width += img->hmargin;
26216 if (slice.x + slice.width == img->width)
26217 it->pixel_width += img->hmargin;
26219 /* It's quite possible for images to have an ascent greater than
26220 their height, so don't get confused in that case. */
26221 if (it->descent < 0)
26222 it->descent = 0;
26224 it->nglyphs = 1;
26226 if (face->box != FACE_NO_BOX)
26228 if (face->box_line_width > 0)
26230 if (slice.y == 0)
26231 it->ascent += face->box_line_width;
26232 if (slice.y + slice.height == img->height)
26233 it->descent += face->box_line_width;
26236 if (it->start_of_box_run_p && slice.x == 0)
26237 it->pixel_width += eabs (face->box_line_width);
26238 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26239 it->pixel_width += eabs (face->box_line_width);
26242 take_vertical_position_into_account (it);
26244 /* Automatically crop wide image glyphs at right edge so we can
26245 draw the cursor on same display row. */
26246 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26247 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26249 it->pixel_width -= crop;
26250 slice.width -= crop;
26253 if (it->glyph_row)
26255 struct glyph *glyph;
26256 enum glyph_row_area area = it->area;
26258 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26259 if (it->glyph_row->reversed_p)
26261 struct glyph *g;
26263 /* Make room for the new glyph. */
26264 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26265 g[1] = *g;
26266 glyph = it->glyph_row->glyphs[it->area];
26268 if (glyph < it->glyph_row->glyphs[area + 1])
26270 glyph->charpos = CHARPOS (it->position);
26271 glyph->object = it->object;
26272 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26273 glyph->ascent = glyph_ascent;
26274 glyph->descent = it->descent;
26275 glyph->voffset = it->voffset;
26276 glyph->type = IMAGE_GLYPH;
26277 glyph->avoid_cursor_p = it->avoid_cursor_p;
26278 glyph->multibyte_p = it->multibyte_p;
26279 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26281 /* In R2L rows, the left and the right box edges need to be
26282 drawn in reverse direction. */
26283 glyph->right_box_line_p = it->start_of_box_run_p;
26284 glyph->left_box_line_p = it->end_of_box_run_p;
26286 else
26288 glyph->left_box_line_p = it->start_of_box_run_p;
26289 glyph->right_box_line_p = it->end_of_box_run_p;
26291 glyph->overlaps_vertically_p = false;
26292 glyph->padding_p = false;
26293 glyph->glyph_not_available_p = false;
26294 glyph->face_id = it->face_id;
26295 glyph->u.img_id = img->id;
26296 glyph->slice.img = slice;
26297 glyph->font_type = FONT_TYPE_UNKNOWN;
26298 if (it->bidi_p)
26300 glyph->resolved_level = it->bidi_it.resolved_level;
26301 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26302 glyph->bidi_type = it->bidi_it.type;
26304 ++it->glyph_row->used[area];
26306 else
26307 IT_EXPAND_MATRIX_WIDTH (it, area);
26311 static void
26312 produce_xwidget_glyph (struct it *it)
26314 #ifdef HAVE_XWIDGETS
26315 struct xwidget *xw;
26316 int glyph_ascent, crop;
26317 eassert (it->what == IT_XWIDGET);
26319 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26320 /* Make sure X resources of the face is loaded. */
26321 prepare_face_for_display (it->f, face);
26323 xw = it->xwidget;
26324 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26325 it->descent = xw->height/2;
26326 it->phys_descent = it->descent;
26327 it->pixel_width = xw->width;
26328 /* It's quite possible for images to have an ascent greater than
26329 their height, so don't get confused in that case. */
26330 if (it->descent < 0)
26331 it->descent = 0;
26333 it->nglyphs = 1;
26335 if (face->box != FACE_NO_BOX)
26337 if (face->box_line_width > 0)
26339 it->ascent += face->box_line_width;
26340 it->descent += face->box_line_width;
26343 if (it->start_of_box_run_p)
26344 it->pixel_width += eabs (face->box_line_width);
26345 it->pixel_width += eabs (face->box_line_width);
26348 take_vertical_position_into_account (it);
26350 /* Automatically crop wide image glyphs at right edge so we can
26351 draw the cursor on same display row. */
26352 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26353 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26354 it->pixel_width -= crop;
26356 if (it->glyph_row)
26358 enum glyph_row_area area = it->area;
26359 struct glyph *glyph
26360 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26362 if (it->glyph_row->reversed_p)
26364 struct glyph *g;
26366 /* Make room for the new glyph. */
26367 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26368 g[1] = *g;
26369 glyph = it->glyph_row->glyphs[it->area];
26371 if (glyph < it->glyph_row->glyphs[area + 1])
26373 glyph->charpos = CHARPOS (it->position);
26374 glyph->object = it->object;
26375 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26376 glyph->ascent = glyph_ascent;
26377 glyph->descent = it->descent;
26378 glyph->voffset = it->voffset;
26379 glyph->type = XWIDGET_GLYPH;
26380 glyph->avoid_cursor_p = it->avoid_cursor_p;
26381 glyph->multibyte_p = it->multibyte_p;
26382 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26384 /* In R2L rows, the left and the right box edges need to be
26385 drawn in reverse direction. */
26386 glyph->right_box_line_p = it->start_of_box_run_p;
26387 glyph->left_box_line_p = it->end_of_box_run_p;
26389 else
26391 glyph->left_box_line_p = it->start_of_box_run_p;
26392 glyph->right_box_line_p = it->end_of_box_run_p;
26394 glyph->overlaps_vertically_p = 0;
26395 glyph->padding_p = 0;
26396 glyph->glyph_not_available_p = 0;
26397 glyph->face_id = it->face_id;
26398 glyph->u.xwidget = it->xwidget;
26399 glyph->font_type = FONT_TYPE_UNKNOWN;
26400 if (it->bidi_p)
26402 glyph->resolved_level = it->bidi_it.resolved_level;
26403 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26404 glyph->bidi_type = it->bidi_it.type;
26406 ++it->glyph_row->used[area];
26408 else
26409 IT_EXPAND_MATRIX_WIDTH (it, area);
26411 #endif
26414 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26415 of the glyph, WIDTH and HEIGHT are the width and height of the
26416 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26418 static void
26419 append_stretch_glyph (struct it *it, Lisp_Object object,
26420 int width, int height, int ascent)
26422 struct glyph *glyph;
26423 enum glyph_row_area area = it->area;
26425 eassert (ascent >= 0 && ascent <= height);
26427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26428 if (glyph < it->glyph_row->glyphs[area + 1])
26430 /* If the glyph row is reversed, we need to prepend the glyph
26431 rather than append it. */
26432 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26434 struct glyph *g;
26436 /* Make room for the additional glyph. */
26437 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26438 g[1] = *g;
26439 glyph = it->glyph_row->glyphs[area];
26441 /* Decrease the width of the first glyph of the row that
26442 begins before first_visible_x (e.g., due to hscroll).
26443 This is so the overall width of the row becomes smaller
26444 by the scroll amount, and the stretch glyph appended by
26445 extend_face_to_end_of_line will be wider, to shift the
26446 row glyphs to the right. (In L2R rows, the corresponding
26447 left-shift effect is accomplished by setting row->x to a
26448 negative value, which won't work with R2L rows.)
26450 This must leave us with a positive value of WIDTH, since
26451 otherwise the call to move_it_in_display_line_to at the
26452 beginning of display_line would have got past the entire
26453 first glyph, and then it->current_x would have been
26454 greater or equal to it->first_visible_x. */
26455 if (it->current_x < it->first_visible_x)
26456 width -= it->first_visible_x - it->current_x;
26457 eassert (width > 0);
26459 glyph->charpos = CHARPOS (it->position);
26460 glyph->object = object;
26461 /* FIXME: It would be better to use TYPE_MAX here, but
26462 __typeof__ is not portable enough... */
26463 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26464 glyph->ascent = ascent;
26465 glyph->descent = height - ascent;
26466 glyph->voffset = it->voffset;
26467 glyph->type = STRETCH_GLYPH;
26468 glyph->avoid_cursor_p = it->avoid_cursor_p;
26469 glyph->multibyte_p = it->multibyte_p;
26470 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26472 /* In R2L rows, the left and the right box edges need to be
26473 drawn in reverse direction. */
26474 glyph->right_box_line_p = it->start_of_box_run_p;
26475 glyph->left_box_line_p = it->end_of_box_run_p;
26477 else
26479 glyph->left_box_line_p = it->start_of_box_run_p;
26480 glyph->right_box_line_p = it->end_of_box_run_p;
26482 glyph->overlaps_vertically_p = false;
26483 glyph->padding_p = false;
26484 glyph->glyph_not_available_p = false;
26485 glyph->face_id = it->face_id;
26486 glyph->u.stretch.ascent = ascent;
26487 glyph->u.stretch.height = height;
26488 glyph->slice.img = null_glyph_slice;
26489 glyph->font_type = FONT_TYPE_UNKNOWN;
26490 if (it->bidi_p)
26492 glyph->resolved_level = it->bidi_it.resolved_level;
26493 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26494 glyph->bidi_type = it->bidi_it.type;
26496 else
26498 glyph->resolved_level = 0;
26499 glyph->bidi_type = UNKNOWN_BT;
26501 ++it->glyph_row->used[area];
26503 else
26504 IT_EXPAND_MATRIX_WIDTH (it, area);
26507 #endif /* HAVE_WINDOW_SYSTEM */
26509 /* Produce a stretch glyph for iterator IT. IT->object is the value
26510 of the glyph property displayed. The value must be a list
26511 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26512 being recognized:
26514 1. `:width WIDTH' specifies that the space should be WIDTH *
26515 canonical char width wide. WIDTH may be an integer or floating
26516 point number.
26518 2. `:relative-width FACTOR' specifies that the width of the stretch
26519 should be computed from the width of the first character having the
26520 `glyph' property, and should be FACTOR times that width.
26522 3. `:align-to HPOS' specifies that the space should be wide enough
26523 to reach HPOS, a value in canonical character units.
26525 Exactly one of the above pairs must be present.
26527 4. `:height HEIGHT' specifies that the height of the stretch produced
26528 should be HEIGHT, measured in canonical character units.
26530 5. `:relative-height FACTOR' specifies that the height of the
26531 stretch should be FACTOR times the height of the characters having
26532 the glyph property.
26534 Either none or exactly one of 4 or 5 must be present.
26536 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26537 of the stretch should be used for the ascent of the stretch.
26538 ASCENT must be in the range 0 <= ASCENT <= 100. */
26540 void
26541 produce_stretch_glyph (struct it *it)
26543 /* (space :width WIDTH :height HEIGHT ...) */
26544 Lisp_Object prop, plist;
26545 int width = 0, height = 0, align_to = -1;
26546 bool zero_width_ok_p = false;
26547 double tem;
26548 struct font *font = NULL;
26550 #ifdef HAVE_WINDOW_SYSTEM
26551 int ascent = 0;
26552 bool zero_height_ok_p = false;
26554 if (FRAME_WINDOW_P (it->f))
26556 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26557 font = face->font ? face->font : FRAME_FONT (it->f);
26558 prepare_face_for_display (it->f, face);
26560 #endif
26562 /* List should start with `space'. */
26563 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26564 plist = XCDR (it->object);
26566 /* Compute the width of the stretch. */
26567 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26568 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26570 /* Absolute width `:width WIDTH' specified and valid. */
26571 zero_width_ok_p = true;
26572 width = (int)tem;
26574 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26576 /* Relative width `:relative-width FACTOR' specified and valid.
26577 Compute the width of the characters having the `glyph'
26578 property. */
26579 struct it it2;
26580 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26582 it2 = *it;
26583 if (it->multibyte_p)
26584 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26585 else
26587 it2.c = it2.char_to_display = *p, it2.len = 1;
26588 if (! ASCII_CHAR_P (it2.c))
26589 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26592 it2.glyph_row = NULL;
26593 it2.what = IT_CHARACTER;
26594 PRODUCE_GLYPHS (&it2);
26595 width = NUMVAL (prop) * it2.pixel_width;
26597 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26598 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26599 &align_to))
26601 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26602 align_to = (align_to < 0
26604 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26605 else if (align_to < 0)
26606 align_to = window_box_left_offset (it->w, TEXT_AREA);
26607 width = max (0, (int)tem + align_to - it->current_x);
26608 zero_width_ok_p = true;
26610 else
26611 /* Nothing specified -> width defaults to canonical char width. */
26612 width = FRAME_COLUMN_WIDTH (it->f);
26614 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26615 width = 1;
26617 #ifdef HAVE_WINDOW_SYSTEM
26618 /* Compute height. */
26619 if (FRAME_WINDOW_P (it->f))
26621 int default_height = normal_char_height (font, ' ');
26623 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26624 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26626 height = (int)tem;
26627 zero_height_ok_p = true;
26629 else if (prop = Fplist_get (plist, QCrelative_height),
26630 NUMVAL (prop) > 0)
26631 height = default_height * NUMVAL (prop);
26632 else
26633 height = default_height;
26635 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26636 height = 1;
26638 /* Compute percentage of height used for ascent. If
26639 `:ascent ASCENT' is present and valid, use that. Otherwise,
26640 derive the ascent from the font in use. */
26641 if (prop = Fplist_get (plist, QCascent),
26642 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26643 ascent = height * NUMVAL (prop) / 100.0;
26644 else if (!NILP (prop)
26645 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26646 ascent = min (max (0, (int)tem), height);
26647 else
26648 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26650 else
26651 #endif /* HAVE_WINDOW_SYSTEM */
26652 height = 1;
26654 if (width > 0 && it->line_wrap != TRUNCATE
26655 && it->current_x + width > it->last_visible_x)
26657 width = it->last_visible_x - it->current_x;
26658 #ifdef HAVE_WINDOW_SYSTEM
26659 /* Subtract one more pixel from the stretch width, but only on
26660 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26661 width -= FRAME_WINDOW_P (it->f);
26662 #endif
26665 if (width > 0 && height > 0 && it->glyph_row)
26667 Lisp_Object o_object = it->object;
26668 Lisp_Object object = it->stack[it->sp - 1].string;
26669 int n = width;
26671 if (!STRINGP (object))
26672 object = it->w->contents;
26673 #ifdef HAVE_WINDOW_SYSTEM
26674 if (FRAME_WINDOW_P (it->f))
26675 append_stretch_glyph (it, object, width, height, ascent);
26676 else
26677 #endif
26679 it->object = object;
26680 it->char_to_display = ' ';
26681 it->pixel_width = it->len = 1;
26682 while (n--)
26683 tty_append_glyph (it);
26684 it->object = o_object;
26688 it->pixel_width = width;
26689 #ifdef HAVE_WINDOW_SYSTEM
26690 if (FRAME_WINDOW_P (it->f))
26692 it->ascent = it->phys_ascent = ascent;
26693 it->descent = it->phys_descent = height - it->ascent;
26694 it->nglyphs = width > 0 && height > 0;
26695 take_vertical_position_into_account (it);
26697 else
26698 #endif
26699 it->nglyphs = width;
26702 /* Get information about special display element WHAT in an
26703 environment described by IT. WHAT is one of IT_TRUNCATION or
26704 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26705 non-null glyph_row member. This function ensures that fields like
26706 face_id, c, len of IT are left untouched. */
26708 static void
26709 produce_special_glyphs (struct it *it, enum display_element_type what)
26711 struct it temp_it;
26712 Lisp_Object gc;
26713 GLYPH glyph;
26715 temp_it = *it;
26716 temp_it.object = Qnil;
26717 memset (&temp_it.current, 0, sizeof temp_it.current);
26719 if (what == IT_CONTINUATION)
26721 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26722 if (it->bidi_it.paragraph_dir == R2L)
26723 SET_GLYPH_FROM_CHAR (glyph, '/');
26724 else
26725 SET_GLYPH_FROM_CHAR (glyph, '\\');
26726 if (it->dp
26727 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26729 /* FIXME: Should we mirror GC for R2L lines? */
26730 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26731 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26734 else if (what == IT_TRUNCATION)
26736 /* Truncation glyph. */
26737 SET_GLYPH_FROM_CHAR (glyph, '$');
26738 if (it->dp
26739 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26741 /* FIXME: Should we mirror GC for R2L lines? */
26742 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26743 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26746 else
26747 emacs_abort ();
26749 #ifdef HAVE_WINDOW_SYSTEM
26750 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26751 is turned off, we precede the truncation/continuation glyphs by a
26752 stretch glyph whose width is computed such that these special
26753 glyphs are aligned at the window margin, even when very different
26754 fonts are used in different glyph rows. */
26755 if (FRAME_WINDOW_P (temp_it.f)
26756 /* init_iterator calls this with it->glyph_row == NULL, and it
26757 wants only the pixel width of the truncation/continuation
26758 glyphs. */
26759 && temp_it.glyph_row
26760 /* insert_left_trunc_glyphs calls us at the beginning of the
26761 row, and it has its own calculation of the stretch glyph
26762 width. */
26763 && temp_it.glyph_row->used[TEXT_AREA] > 0
26764 && (temp_it.glyph_row->reversed_p
26765 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26766 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26768 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26770 if (stretch_width > 0)
26772 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26773 struct font *font =
26774 face->font ? face->font : FRAME_FONT (temp_it.f);
26775 int stretch_ascent =
26776 (((temp_it.ascent + temp_it.descent)
26777 * FONT_BASE (font)) / FONT_HEIGHT (font));
26779 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26780 temp_it.ascent + temp_it.descent,
26781 stretch_ascent);
26784 #endif
26786 temp_it.dp = NULL;
26787 temp_it.what = IT_CHARACTER;
26788 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26789 temp_it.face_id = GLYPH_FACE (glyph);
26790 temp_it.len = CHAR_BYTES (temp_it.c);
26792 PRODUCE_GLYPHS (&temp_it);
26793 it->pixel_width = temp_it.pixel_width;
26794 it->nglyphs = temp_it.nglyphs;
26797 #ifdef HAVE_WINDOW_SYSTEM
26799 /* Calculate line-height and line-spacing properties.
26800 An integer value specifies explicit pixel value.
26801 A float value specifies relative value to current face height.
26802 A cons (float . face-name) specifies relative value to
26803 height of specified face font.
26805 Returns height in pixels, or nil. */
26807 static Lisp_Object
26808 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26809 int boff, bool override)
26811 Lisp_Object face_name = Qnil;
26812 int ascent, descent, height;
26814 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26815 return val;
26817 if (CONSP (val))
26819 face_name = XCAR (val);
26820 val = XCDR (val);
26821 if (!NUMBERP (val))
26822 val = make_number (1);
26823 if (NILP (face_name))
26825 height = it->ascent + it->descent;
26826 goto scale;
26830 if (NILP (face_name))
26832 font = FRAME_FONT (it->f);
26833 boff = FRAME_BASELINE_OFFSET (it->f);
26835 else if (EQ (face_name, Qt))
26837 override = false;
26839 else
26841 int face_id;
26842 struct face *face;
26844 face_id = lookup_named_face (it->f, face_name, false);
26845 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26846 if (face == NULL || ((font = face->font) == NULL))
26847 return make_number (-1);
26848 boff = font->baseline_offset;
26849 if (font->vertical_centering)
26850 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26853 normal_char_ascent_descent (font, -1, &ascent, &descent);
26855 if (override)
26857 it->override_ascent = ascent;
26858 it->override_descent = descent;
26859 it->override_boff = boff;
26862 height = ascent + descent;
26864 scale:
26865 if (FLOATP (val))
26866 height = (int)(XFLOAT_DATA (val) * height);
26867 else if (INTEGERP (val))
26868 height *= XINT (val);
26870 return make_number (height);
26874 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26875 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26876 and only if this is for a character for which no font was found.
26878 If the display method (it->glyphless_method) is
26879 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26880 length of the acronym or the hexadecimal string, UPPER_XOFF and
26881 UPPER_YOFF are pixel offsets for the upper part of the string,
26882 LOWER_XOFF and LOWER_YOFF are for the lower part.
26884 For the other display methods, LEN through LOWER_YOFF are zero. */
26886 static void
26887 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26888 short upper_xoff, short upper_yoff,
26889 short lower_xoff, short lower_yoff)
26891 struct glyph *glyph;
26892 enum glyph_row_area area = it->area;
26894 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26895 if (glyph < it->glyph_row->glyphs[area + 1])
26897 /* If the glyph row is reversed, we need to prepend the glyph
26898 rather than append it. */
26899 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26901 struct glyph *g;
26903 /* Make room for the additional glyph. */
26904 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26905 g[1] = *g;
26906 glyph = it->glyph_row->glyphs[area];
26908 glyph->charpos = CHARPOS (it->position);
26909 glyph->object = it->object;
26910 eassert (it->pixel_width <= SHRT_MAX);
26911 glyph->pixel_width = it->pixel_width;
26912 glyph->ascent = it->ascent;
26913 glyph->descent = it->descent;
26914 glyph->voffset = it->voffset;
26915 glyph->type = GLYPHLESS_GLYPH;
26916 glyph->u.glyphless.method = it->glyphless_method;
26917 glyph->u.glyphless.for_no_font = for_no_font;
26918 glyph->u.glyphless.len = len;
26919 glyph->u.glyphless.ch = it->c;
26920 glyph->slice.glyphless.upper_xoff = upper_xoff;
26921 glyph->slice.glyphless.upper_yoff = upper_yoff;
26922 glyph->slice.glyphless.lower_xoff = lower_xoff;
26923 glyph->slice.glyphless.lower_yoff = lower_yoff;
26924 glyph->avoid_cursor_p = it->avoid_cursor_p;
26925 glyph->multibyte_p = it->multibyte_p;
26926 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26928 /* In R2L rows, the left and the right box edges need to be
26929 drawn in reverse direction. */
26930 glyph->right_box_line_p = it->start_of_box_run_p;
26931 glyph->left_box_line_p = it->end_of_box_run_p;
26933 else
26935 glyph->left_box_line_p = it->start_of_box_run_p;
26936 glyph->right_box_line_p = it->end_of_box_run_p;
26938 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26939 || it->phys_descent > it->descent);
26940 glyph->padding_p = false;
26941 glyph->glyph_not_available_p = false;
26942 glyph->face_id = face_id;
26943 glyph->font_type = FONT_TYPE_UNKNOWN;
26944 if (it->bidi_p)
26946 glyph->resolved_level = it->bidi_it.resolved_level;
26947 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26948 glyph->bidi_type = it->bidi_it.type;
26950 ++it->glyph_row->used[area];
26952 else
26953 IT_EXPAND_MATRIX_WIDTH (it, area);
26957 /* Produce a glyph for a glyphless character for iterator IT.
26958 IT->glyphless_method specifies which method to use for displaying
26959 the character. See the description of enum
26960 glyphless_display_method in dispextern.h for the detail.
26962 FOR_NO_FONT is true if and only if this is for a character for
26963 which no font was found. ACRONYM, if non-nil, is an acronym string
26964 for the character. */
26966 static void
26967 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26969 int face_id;
26970 struct face *face;
26971 struct font *font;
26972 int base_width, base_height, width, height;
26973 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26974 int len;
26976 /* Get the metrics of the base font. We always refer to the current
26977 ASCII face. */
26978 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26979 font = face->font ? face->font : FRAME_FONT (it->f);
26980 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26981 it->ascent += font->baseline_offset;
26982 it->descent -= font->baseline_offset;
26983 base_height = it->ascent + it->descent;
26984 base_width = font->average_width;
26986 face_id = merge_glyphless_glyph_face (it);
26988 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26990 it->pixel_width = THIN_SPACE_WIDTH;
26991 len = 0;
26992 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26994 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26996 width = CHARACTER_WIDTH (it->c);
26997 if (width == 0)
26998 width = 1;
26999 else if (width > 4)
27000 width = 4;
27001 it->pixel_width = base_width * width;
27002 len = 0;
27003 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27005 else
27007 char buf[7];
27008 const char *str;
27009 unsigned int code[6];
27010 int upper_len;
27011 int ascent, descent;
27012 struct font_metrics metrics_upper, metrics_lower;
27014 face = FACE_FROM_ID (it->f, face_id);
27015 font = face->font ? face->font : FRAME_FONT (it->f);
27016 prepare_face_for_display (it->f, face);
27018 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27020 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27021 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27022 if (CONSP (acronym))
27023 acronym = XCAR (acronym);
27024 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27026 else
27028 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27029 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27030 str = buf;
27032 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27033 code[len] = font->driver->encode_char (font, str[len]);
27034 upper_len = (len + 1) / 2;
27035 font->driver->text_extents (font, code, upper_len,
27036 &metrics_upper);
27037 font->driver->text_extents (font, code + upper_len, len - upper_len,
27038 &metrics_lower);
27042 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27043 width = max (metrics_upper.width, metrics_lower.width) + 4;
27044 upper_xoff = upper_yoff = 2; /* the typical case */
27045 if (base_width >= width)
27047 /* Align the upper to the left, the lower to the right. */
27048 it->pixel_width = base_width;
27049 lower_xoff = base_width - 2 - metrics_lower.width;
27051 else
27053 /* Center the shorter one. */
27054 it->pixel_width = width;
27055 if (metrics_upper.width >= metrics_lower.width)
27056 lower_xoff = (width - metrics_lower.width) / 2;
27057 else
27059 /* FIXME: This code doesn't look right. It formerly was
27060 missing the "lower_xoff = 0;", which couldn't have
27061 been right since it left lower_xoff uninitialized. */
27062 lower_xoff = 0;
27063 upper_xoff = (width - metrics_upper.width) / 2;
27067 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27068 top, bottom, and between upper and lower strings. */
27069 height = (metrics_upper.ascent + metrics_upper.descent
27070 + metrics_lower.ascent + metrics_lower.descent) + 5;
27071 /* Center vertically.
27072 H:base_height, D:base_descent
27073 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27075 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27076 descent = D - H/2 + h/2;
27077 lower_yoff = descent - 2 - ld;
27078 upper_yoff = lower_yoff - la - 1 - ud; */
27079 ascent = - (it->descent - (base_height + height + 1) / 2);
27080 descent = it->descent - (base_height - height) / 2;
27081 lower_yoff = descent - 2 - metrics_lower.descent;
27082 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27083 - metrics_upper.descent);
27084 /* Don't make the height shorter than the base height. */
27085 if (height > base_height)
27087 it->ascent = ascent;
27088 it->descent = descent;
27092 it->phys_ascent = it->ascent;
27093 it->phys_descent = it->descent;
27094 if (it->glyph_row)
27095 append_glyphless_glyph (it, face_id, for_no_font, len,
27096 upper_xoff, upper_yoff,
27097 lower_xoff, lower_yoff);
27098 it->nglyphs = 1;
27099 take_vertical_position_into_account (it);
27103 /* RIF:
27104 Produce glyphs/get display metrics for the display element IT is
27105 loaded with. See the description of struct it in dispextern.h
27106 for an overview of struct it. */
27108 void
27109 x_produce_glyphs (struct it *it)
27111 int extra_line_spacing = it->extra_line_spacing;
27113 it->glyph_not_available_p = false;
27115 if (it->what == IT_CHARACTER)
27117 XChar2b char2b;
27118 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27119 struct font *font = face->font;
27120 struct font_metrics *pcm = NULL;
27121 int boff; /* Baseline offset. */
27123 if (font == NULL)
27125 /* When no suitable font is found, display this character by
27126 the method specified in the first extra slot of
27127 Vglyphless_char_display. */
27128 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27130 eassert (it->what == IT_GLYPHLESS);
27131 produce_glyphless_glyph (it, true,
27132 STRINGP (acronym) ? acronym : Qnil);
27133 goto done;
27136 boff = font->baseline_offset;
27137 if (font->vertical_centering)
27138 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27140 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27142 it->nglyphs = 1;
27144 if (it->override_ascent >= 0)
27146 it->ascent = it->override_ascent;
27147 it->descent = it->override_descent;
27148 boff = it->override_boff;
27150 else
27152 it->ascent = FONT_BASE (font) + boff;
27153 it->descent = FONT_DESCENT (font) - boff;
27156 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27158 pcm = get_per_char_metric (font, &char2b);
27159 if (pcm->width == 0
27160 && pcm->rbearing == 0 && pcm->lbearing == 0)
27161 pcm = NULL;
27164 if (pcm)
27166 it->phys_ascent = pcm->ascent + boff;
27167 it->phys_descent = pcm->descent - boff;
27168 it->pixel_width = pcm->width;
27169 /* Don't use font-global values for ascent and descent
27170 if they result in an exceedingly large line height. */
27171 if (it->override_ascent < 0)
27173 if (FONT_TOO_HIGH (font))
27175 it->ascent = it->phys_ascent;
27176 it->descent = it->phys_descent;
27177 /* These limitations are enforced by an
27178 assertion near the end of this function. */
27179 if (it->ascent < 0)
27180 it->ascent = 0;
27181 if (it->descent < 0)
27182 it->descent = 0;
27186 else
27188 it->glyph_not_available_p = true;
27189 it->phys_ascent = it->ascent;
27190 it->phys_descent = it->descent;
27191 it->pixel_width = font->space_width;
27194 if (it->constrain_row_ascent_descent_p)
27196 if (it->descent > it->max_descent)
27198 it->ascent += it->descent - it->max_descent;
27199 it->descent = it->max_descent;
27201 if (it->ascent > it->max_ascent)
27203 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27204 it->ascent = it->max_ascent;
27206 it->phys_ascent = min (it->phys_ascent, it->ascent);
27207 it->phys_descent = min (it->phys_descent, it->descent);
27208 extra_line_spacing = 0;
27211 /* If this is a space inside a region of text with
27212 `space-width' property, change its width. */
27213 bool stretched_p
27214 = it->char_to_display == ' ' && !NILP (it->space_width);
27215 if (stretched_p)
27216 it->pixel_width *= XFLOATINT (it->space_width);
27218 /* If face has a box, add the box thickness to the character
27219 height. If character has a box line to the left and/or
27220 right, add the box line width to the character's width. */
27221 if (face->box != FACE_NO_BOX)
27223 int thick = face->box_line_width;
27225 if (thick > 0)
27227 it->ascent += thick;
27228 it->descent += thick;
27230 else
27231 thick = -thick;
27233 if (it->start_of_box_run_p)
27234 it->pixel_width += thick;
27235 if (it->end_of_box_run_p)
27236 it->pixel_width += thick;
27239 /* If face has an overline, add the height of the overline
27240 (1 pixel) and a 1 pixel margin to the character height. */
27241 if (face->overline_p)
27242 it->ascent += overline_margin;
27244 if (it->constrain_row_ascent_descent_p)
27246 if (it->ascent > it->max_ascent)
27247 it->ascent = it->max_ascent;
27248 if (it->descent > it->max_descent)
27249 it->descent = it->max_descent;
27252 take_vertical_position_into_account (it);
27254 /* If we have to actually produce glyphs, do it. */
27255 if (it->glyph_row)
27257 if (stretched_p)
27259 /* Translate a space with a `space-width' property
27260 into a stretch glyph. */
27261 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27262 / FONT_HEIGHT (font));
27263 append_stretch_glyph (it, it->object, it->pixel_width,
27264 it->ascent + it->descent, ascent);
27266 else
27267 append_glyph (it);
27269 /* If characters with lbearing or rbearing are displayed
27270 in this line, record that fact in a flag of the
27271 glyph row. This is used to optimize X output code. */
27272 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27273 it->glyph_row->contains_overlapping_glyphs_p = true;
27275 if (! stretched_p && it->pixel_width == 0)
27276 /* We assure that all visible glyphs have at least 1-pixel
27277 width. */
27278 it->pixel_width = 1;
27280 else if (it->char_to_display == '\n')
27282 /* A newline has no width, but we need the height of the
27283 line. But if previous part of the line sets a height,
27284 don't increase that height. */
27286 Lisp_Object height;
27287 Lisp_Object total_height = Qnil;
27289 it->override_ascent = -1;
27290 it->pixel_width = 0;
27291 it->nglyphs = 0;
27293 height = get_it_property (it, Qline_height);
27294 /* Split (line-height total-height) list. */
27295 if (CONSP (height)
27296 && CONSP (XCDR (height))
27297 && NILP (XCDR (XCDR (height))))
27299 total_height = XCAR (XCDR (height));
27300 height = XCAR (height);
27302 height = calc_line_height_property (it, height, font, boff, true);
27304 if (it->override_ascent >= 0)
27306 it->ascent = it->override_ascent;
27307 it->descent = it->override_descent;
27308 boff = it->override_boff;
27310 else
27312 if (FONT_TOO_HIGH (font))
27314 it->ascent = font->pixel_size + boff - 1;
27315 it->descent = -boff + 1;
27316 if (it->descent < 0)
27317 it->descent = 0;
27319 else
27321 it->ascent = FONT_BASE (font) + boff;
27322 it->descent = FONT_DESCENT (font) - boff;
27326 if (EQ (height, Qt))
27328 if (it->descent > it->max_descent)
27330 it->ascent += it->descent - it->max_descent;
27331 it->descent = it->max_descent;
27333 if (it->ascent > it->max_ascent)
27335 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27336 it->ascent = it->max_ascent;
27338 it->phys_ascent = min (it->phys_ascent, it->ascent);
27339 it->phys_descent = min (it->phys_descent, it->descent);
27340 it->constrain_row_ascent_descent_p = true;
27341 extra_line_spacing = 0;
27343 else
27345 Lisp_Object spacing;
27347 it->phys_ascent = it->ascent;
27348 it->phys_descent = it->descent;
27350 if ((it->max_ascent > 0 || it->max_descent > 0)
27351 && face->box != FACE_NO_BOX
27352 && face->box_line_width > 0)
27354 it->ascent += face->box_line_width;
27355 it->descent += face->box_line_width;
27357 if (!NILP (height)
27358 && XINT (height) > it->ascent + it->descent)
27359 it->ascent = XINT (height) - it->descent;
27361 if (!NILP (total_height))
27362 spacing = calc_line_height_property (it, total_height, font,
27363 boff, false);
27364 else
27366 spacing = get_it_property (it, Qline_spacing);
27367 spacing = calc_line_height_property (it, spacing, font,
27368 boff, false);
27370 if (INTEGERP (spacing))
27372 extra_line_spacing = XINT (spacing);
27373 if (!NILP (total_height))
27374 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27378 else /* i.e. (it->char_to_display == '\t') */
27380 if (font->space_width > 0)
27382 int tab_width = it->tab_width * font->space_width;
27383 int x = it->current_x + it->continuation_lines_width;
27384 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27386 /* If the distance from the current position to the next tab
27387 stop is less than a space character width, use the
27388 tab stop after that. */
27389 if (next_tab_x - x < font->space_width)
27390 next_tab_x += tab_width;
27392 it->pixel_width = next_tab_x - x;
27393 it->nglyphs = 1;
27394 if (FONT_TOO_HIGH (font))
27396 if (get_char_glyph_code (' ', font, &char2b))
27398 pcm = get_per_char_metric (font, &char2b);
27399 if (pcm->width == 0
27400 && pcm->rbearing == 0 && pcm->lbearing == 0)
27401 pcm = NULL;
27404 if (pcm)
27406 it->ascent = pcm->ascent + boff;
27407 it->descent = pcm->descent - boff;
27409 else
27411 it->ascent = font->pixel_size + boff - 1;
27412 it->descent = -boff + 1;
27414 if (it->ascent < 0)
27415 it->ascent = 0;
27416 if (it->descent < 0)
27417 it->descent = 0;
27419 else
27421 it->ascent = FONT_BASE (font) + boff;
27422 it->descent = FONT_DESCENT (font) - boff;
27424 it->phys_ascent = it->ascent;
27425 it->phys_descent = it->descent;
27427 if (it->glyph_row)
27429 append_stretch_glyph (it, it->object, it->pixel_width,
27430 it->ascent + it->descent, it->ascent);
27433 else
27435 it->pixel_width = 0;
27436 it->nglyphs = 1;
27440 if (FONT_TOO_HIGH (font))
27442 int font_ascent, font_descent;
27444 /* For very large fonts, where we ignore the declared font
27445 dimensions, and go by per-character metrics instead,
27446 don't let the row ascent and descent values (and the row
27447 height computed from them) be smaller than the "normal"
27448 character metrics. This avoids unpleasant effects
27449 whereby lines on display would change their height
27450 depending on which characters are shown. */
27451 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27452 it->max_ascent = max (it->max_ascent, font_ascent);
27453 it->max_descent = max (it->max_descent, font_descent);
27456 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27458 /* A static composition.
27460 Note: A composition is represented as one glyph in the
27461 glyph matrix. There are no padding glyphs.
27463 Important note: pixel_width, ascent, and descent are the
27464 values of what is drawn by draw_glyphs (i.e. the values of
27465 the overall glyphs composed). */
27466 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27467 int boff; /* baseline offset */
27468 struct composition *cmp = composition_table[it->cmp_it.id];
27469 int glyph_len = cmp->glyph_len;
27470 struct font *font = face->font;
27472 it->nglyphs = 1;
27474 /* If we have not yet calculated pixel size data of glyphs of
27475 the composition for the current face font, calculate them
27476 now. Theoretically, we have to check all fonts for the
27477 glyphs, but that requires much time and memory space. So,
27478 here we check only the font of the first glyph. This may
27479 lead to incorrect display, but it's very rare, and C-l
27480 (recenter-top-bottom) can correct the display anyway. */
27481 if (! cmp->font || cmp->font != font)
27483 /* Ascent and descent of the font of the first character
27484 of this composition (adjusted by baseline offset).
27485 Ascent and descent of overall glyphs should not be less
27486 than these, respectively. */
27487 int font_ascent, font_descent, font_height;
27488 /* Bounding box of the overall glyphs. */
27489 int leftmost, rightmost, lowest, highest;
27490 int lbearing, rbearing;
27491 int i, width, ascent, descent;
27492 int c;
27493 XChar2b char2b;
27494 struct font_metrics *pcm;
27495 ptrdiff_t pos;
27497 eassume (0 < glyph_len); /* See Bug#8512. */
27499 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27500 while (c == '\t' && 0 < --glyph_len);
27502 bool right_padded = glyph_len < cmp->glyph_len;
27503 for (i = 0; i < glyph_len; i++)
27505 c = COMPOSITION_GLYPH (cmp, i);
27506 if (c != '\t')
27507 break;
27508 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27510 bool left_padded = i > 0;
27512 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27513 : IT_CHARPOS (*it));
27514 /* If no suitable font is found, use the default font. */
27515 bool font_not_found_p = font == NULL;
27516 if (font_not_found_p)
27518 face = face->ascii_face;
27519 font = face->font;
27521 boff = font->baseline_offset;
27522 if (font->vertical_centering)
27523 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27524 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27525 font_ascent += boff;
27526 font_descent -= boff;
27527 font_height = font_ascent + font_descent;
27529 cmp->font = font;
27531 pcm = NULL;
27532 if (! font_not_found_p)
27534 get_char_face_and_encoding (it->f, c, it->face_id,
27535 &char2b, false);
27536 pcm = get_per_char_metric (font, &char2b);
27539 /* Initialize the bounding box. */
27540 if (pcm)
27542 width = cmp->glyph_len > 0 ? pcm->width : 0;
27543 ascent = pcm->ascent;
27544 descent = pcm->descent;
27545 lbearing = pcm->lbearing;
27546 rbearing = pcm->rbearing;
27548 else
27550 width = cmp->glyph_len > 0 ? font->space_width : 0;
27551 ascent = FONT_BASE (font);
27552 descent = FONT_DESCENT (font);
27553 lbearing = 0;
27554 rbearing = width;
27557 rightmost = width;
27558 leftmost = 0;
27559 lowest = - descent + boff;
27560 highest = ascent + boff;
27562 if (! font_not_found_p
27563 && font->default_ascent
27564 && CHAR_TABLE_P (Vuse_default_ascent)
27565 && !NILP (Faref (Vuse_default_ascent,
27566 make_number (it->char_to_display))))
27567 highest = font->default_ascent + boff;
27569 /* Draw the first glyph at the normal position. It may be
27570 shifted to right later if some other glyphs are drawn
27571 at the left. */
27572 cmp->offsets[i * 2] = 0;
27573 cmp->offsets[i * 2 + 1] = boff;
27574 cmp->lbearing = lbearing;
27575 cmp->rbearing = rbearing;
27577 /* Set cmp->offsets for the remaining glyphs. */
27578 for (i++; i < glyph_len; i++)
27580 int left, right, btm, top;
27581 int ch = COMPOSITION_GLYPH (cmp, i);
27582 int face_id;
27583 struct face *this_face;
27585 if (ch == '\t')
27586 ch = ' ';
27587 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27588 this_face = FACE_FROM_ID (it->f, face_id);
27589 font = this_face->font;
27591 if (font == NULL)
27592 pcm = NULL;
27593 else
27595 get_char_face_and_encoding (it->f, ch, face_id,
27596 &char2b, false);
27597 pcm = get_per_char_metric (font, &char2b);
27599 if (! pcm)
27600 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27601 else
27603 width = pcm->width;
27604 ascent = pcm->ascent;
27605 descent = pcm->descent;
27606 lbearing = pcm->lbearing;
27607 rbearing = pcm->rbearing;
27608 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27610 /* Relative composition with or without
27611 alternate chars. */
27612 left = (leftmost + rightmost - width) / 2;
27613 btm = - descent + boff;
27614 if (font->relative_compose
27615 && (! CHAR_TABLE_P (Vignore_relative_composition)
27616 || NILP (Faref (Vignore_relative_composition,
27617 make_number (ch)))))
27620 if (- descent >= font->relative_compose)
27621 /* One extra pixel between two glyphs. */
27622 btm = highest + 1;
27623 else if (ascent <= 0)
27624 /* One extra pixel between two glyphs. */
27625 btm = lowest - 1 - ascent - descent;
27628 else
27630 /* A composition rule is specified by an integer
27631 value that encodes global and new reference
27632 points (GREF and NREF). GREF and NREF are
27633 specified by numbers as below:
27635 0---1---2 -- ascent
27639 9--10--11 -- center
27641 ---3---4---5--- baseline
27643 6---7---8 -- descent
27645 int rule = COMPOSITION_RULE (cmp, i);
27646 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27648 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27649 grefx = gref % 3, nrefx = nref % 3;
27650 grefy = gref / 3, nrefy = nref / 3;
27651 if (xoff)
27652 xoff = font_height * (xoff - 128) / 256;
27653 if (yoff)
27654 yoff = font_height * (yoff - 128) / 256;
27656 left = (leftmost
27657 + grefx * (rightmost - leftmost) / 2
27658 - nrefx * width / 2
27659 + xoff);
27661 btm = ((grefy == 0 ? highest
27662 : grefy == 1 ? 0
27663 : grefy == 2 ? lowest
27664 : (highest + lowest) / 2)
27665 - (nrefy == 0 ? ascent + descent
27666 : nrefy == 1 ? descent - boff
27667 : nrefy == 2 ? 0
27668 : (ascent + descent) / 2)
27669 + yoff);
27672 cmp->offsets[i * 2] = left;
27673 cmp->offsets[i * 2 + 1] = btm + descent;
27675 /* Update the bounding box of the overall glyphs. */
27676 if (width > 0)
27678 right = left + width;
27679 if (left < leftmost)
27680 leftmost = left;
27681 if (right > rightmost)
27682 rightmost = right;
27684 top = btm + descent + ascent;
27685 if (top > highest)
27686 highest = top;
27687 if (btm < lowest)
27688 lowest = btm;
27690 if (cmp->lbearing > left + lbearing)
27691 cmp->lbearing = left + lbearing;
27692 if (cmp->rbearing < left + rbearing)
27693 cmp->rbearing = left + rbearing;
27697 /* If there are glyphs whose x-offsets are negative,
27698 shift all glyphs to the right and make all x-offsets
27699 non-negative. */
27700 if (leftmost < 0)
27702 for (i = 0; i < cmp->glyph_len; i++)
27703 cmp->offsets[i * 2] -= leftmost;
27704 rightmost -= leftmost;
27705 cmp->lbearing -= leftmost;
27706 cmp->rbearing -= leftmost;
27709 if (left_padded && cmp->lbearing < 0)
27711 for (i = 0; i < cmp->glyph_len; i++)
27712 cmp->offsets[i * 2] -= cmp->lbearing;
27713 rightmost -= cmp->lbearing;
27714 cmp->rbearing -= cmp->lbearing;
27715 cmp->lbearing = 0;
27717 if (right_padded && rightmost < cmp->rbearing)
27719 rightmost = cmp->rbearing;
27722 cmp->pixel_width = rightmost;
27723 cmp->ascent = highest;
27724 cmp->descent = - lowest;
27725 if (cmp->ascent < font_ascent)
27726 cmp->ascent = font_ascent;
27727 if (cmp->descent < font_descent)
27728 cmp->descent = font_descent;
27731 if (it->glyph_row
27732 && (cmp->lbearing < 0
27733 || cmp->rbearing > cmp->pixel_width))
27734 it->glyph_row->contains_overlapping_glyphs_p = true;
27736 it->pixel_width = cmp->pixel_width;
27737 it->ascent = it->phys_ascent = cmp->ascent;
27738 it->descent = it->phys_descent = cmp->descent;
27739 if (face->box != FACE_NO_BOX)
27741 int thick = face->box_line_width;
27743 if (thick > 0)
27745 it->ascent += thick;
27746 it->descent += thick;
27748 else
27749 thick = - thick;
27751 if (it->start_of_box_run_p)
27752 it->pixel_width += thick;
27753 if (it->end_of_box_run_p)
27754 it->pixel_width += thick;
27757 /* If face has an overline, add the height of the overline
27758 (1 pixel) and a 1 pixel margin to the character height. */
27759 if (face->overline_p)
27760 it->ascent += overline_margin;
27762 take_vertical_position_into_account (it);
27763 if (it->ascent < 0)
27764 it->ascent = 0;
27765 if (it->descent < 0)
27766 it->descent = 0;
27768 if (it->glyph_row && cmp->glyph_len > 0)
27769 append_composite_glyph (it);
27771 else if (it->what == IT_COMPOSITION)
27773 /* A dynamic (automatic) composition. */
27774 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27775 Lisp_Object gstring;
27776 struct font_metrics metrics;
27778 it->nglyphs = 1;
27780 gstring = composition_gstring_from_id (it->cmp_it.id);
27781 it->pixel_width
27782 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27783 &metrics);
27784 if (it->glyph_row
27785 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27786 it->glyph_row->contains_overlapping_glyphs_p = true;
27787 it->ascent = it->phys_ascent = metrics.ascent;
27788 it->descent = it->phys_descent = metrics.descent;
27789 if (face->box != FACE_NO_BOX)
27791 int thick = face->box_line_width;
27793 if (thick > 0)
27795 it->ascent += thick;
27796 it->descent += thick;
27798 else
27799 thick = - thick;
27801 if (it->start_of_box_run_p)
27802 it->pixel_width += thick;
27803 if (it->end_of_box_run_p)
27804 it->pixel_width += thick;
27806 /* If face has an overline, add the height of the overline
27807 (1 pixel) and a 1 pixel margin to the character height. */
27808 if (face->overline_p)
27809 it->ascent += overline_margin;
27810 take_vertical_position_into_account (it);
27811 if (it->ascent < 0)
27812 it->ascent = 0;
27813 if (it->descent < 0)
27814 it->descent = 0;
27816 if (it->glyph_row)
27817 append_composite_glyph (it);
27819 else if (it->what == IT_GLYPHLESS)
27820 produce_glyphless_glyph (it, false, Qnil);
27821 else if (it->what == IT_IMAGE)
27822 produce_image_glyph (it);
27823 else if (it->what == IT_STRETCH)
27824 produce_stretch_glyph (it);
27825 else if (it->what == IT_XWIDGET)
27826 produce_xwidget_glyph (it);
27828 done:
27829 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27830 because this isn't true for images with `:ascent 100'. */
27831 eassert (it->ascent >= 0 && it->descent >= 0);
27832 if (it->area == TEXT_AREA)
27833 it->current_x += it->pixel_width;
27835 if (extra_line_spacing > 0)
27837 it->descent += extra_line_spacing;
27838 if (extra_line_spacing > it->max_extra_line_spacing)
27839 it->max_extra_line_spacing = extra_line_spacing;
27842 it->max_ascent = max (it->max_ascent, it->ascent);
27843 it->max_descent = max (it->max_descent, it->descent);
27844 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27845 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27848 /* EXPORT for RIF:
27849 Output LEN glyphs starting at START at the nominal cursor position.
27850 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27851 being updated, and UPDATED_AREA is the area of that row being updated. */
27853 void
27854 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27855 struct glyph *start, enum glyph_row_area updated_area, int len)
27857 int x, hpos, chpos = w->phys_cursor.hpos;
27859 eassert (updated_row);
27860 /* When the window is hscrolled, cursor hpos can legitimately be out
27861 of bounds, but we draw the cursor at the corresponding window
27862 margin in that case. */
27863 if (!updated_row->reversed_p && chpos < 0)
27864 chpos = 0;
27865 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27866 chpos = updated_row->used[TEXT_AREA] - 1;
27868 block_input ();
27870 /* Write glyphs. */
27872 hpos = start - updated_row->glyphs[updated_area];
27873 x = draw_glyphs (w, w->output_cursor.x,
27874 updated_row, updated_area,
27875 hpos, hpos + len,
27876 DRAW_NORMAL_TEXT, 0);
27878 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27879 if (updated_area == TEXT_AREA
27880 && w->phys_cursor_on_p
27881 && w->phys_cursor.vpos == w->output_cursor.vpos
27882 && chpos >= hpos
27883 && chpos < hpos + len)
27884 w->phys_cursor_on_p = false;
27886 unblock_input ();
27888 /* Advance the output cursor. */
27889 w->output_cursor.hpos += len;
27890 w->output_cursor.x = x;
27894 /* EXPORT for RIF:
27895 Insert LEN glyphs from START at the nominal cursor position. */
27897 void
27898 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27899 struct glyph *start, enum glyph_row_area updated_area, int len)
27901 struct frame *f;
27902 int line_height, shift_by_width, shifted_region_width;
27903 struct glyph_row *row;
27904 struct glyph *glyph;
27905 int frame_x, frame_y;
27906 ptrdiff_t hpos;
27908 eassert (updated_row);
27909 block_input ();
27910 f = XFRAME (WINDOW_FRAME (w));
27912 /* Get the height of the line we are in. */
27913 row = updated_row;
27914 line_height = row->height;
27916 /* Get the width of the glyphs to insert. */
27917 shift_by_width = 0;
27918 for (glyph = start; glyph < start + len; ++glyph)
27919 shift_by_width += glyph->pixel_width;
27921 /* Get the width of the region to shift right. */
27922 shifted_region_width = (window_box_width (w, updated_area)
27923 - w->output_cursor.x
27924 - shift_by_width);
27926 /* Shift right. */
27927 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27928 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27930 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27931 line_height, shift_by_width);
27933 /* Write the glyphs. */
27934 hpos = start - row->glyphs[updated_area];
27935 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27936 hpos, hpos + len,
27937 DRAW_NORMAL_TEXT, 0);
27939 /* Advance the output cursor. */
27940 w->output_cursor.hpos += len;
27941 w->output_cursor.x += shift_by_width;
27942 unblock_input ();
27946 /* EXPORT for RIF:
27947 Erase the current text line from the nominal cursor position
27948 (inclusive) to pixel column TO_X (exclusive). The idea is that
27949 everything from TO_X onward is already erased.
27951 TO_X is a pixel position relative to UPDATED_AREA of currently
27952 updated window W. TO_X == -1 means clear to the end of this area. */
27954 void
27955 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27956 enum glyph_row_area updated_area, int to_x)
27958 struct frame *f;
27959 int max_x, min_y, max_y;
27960 int from_x, from_y, to_y;
27962 eassert (updated_row);
27963 f = XFRAME (w->frame);
27965 if (updated_row->full_width_p)
27966 max_x = (WINDOW_PIXEL_WIDTH (w)
27967 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27968 else
27969 max_x = window_box_width (w, updated_area);
27970 max_y = window_text_bottom_y (w);
27972 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27973 of window. For TO_X > 0, truncate to end of drawing area. */
27974 if (to_x == 0)
27975 return;
27976 else if (to_x < 0)
27977 to_x = max_x;
27978 else
27979 to_x = min (to_x, max_x);
27981 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27983 /* Notice if the cursor will be cleared by this operation. */
27984 if (!updated_row->full_width_p)
27985 notice_overwritten_cursor (w, updated_area,
27986 w->output_cursor.x, -1,
27987 updated_row->y,
27988 MATRIX_ROW_BOTTOM_Y (updated_row));
27990 from_x = w->output_cursor.x;
27992 /* Translate to frame coordinates. */
27993 if (updated_row->full_width_p)
27995 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27996 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27998 else
28000 int area_left = window_box_left (w, updated_area);
28001 from_x += area_left;
28002 to_x += area_left;
28005 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28006 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28007 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28009 /* Prevent inadvertently clearing to end of the X window. */
28010 if (to_x > from_x && to_y > from_y)
28012 block_input ();
28013 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28014 to_x - from_x, to_y - from_y);
28015 unblock_input ();
28019 #endif /* HAVE_WINDOW_SYSTEM */
28023 /***********************************************************************
28024 Cursor types
28025 ***********************************************************************/
28027 /* Value is the internal representation of the specified cursor type
28028 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28029 of the bar cursor. */
28031 static enum text_cursor_kinds
28032 get_specified_cursor_type (Lisp_Object arg, int *width)
28034 enum text_cursor_kinds type;
28036 if (NILP (arg))
28037 return NO_CURSOR;
28039 if (EQ (arg, Qbox))
28040 return FILLED_BOX_CURSOR;
28042 if (EQ (arg, Qhollow))
28043 return HOLLOW_BOX_CURSOR;
28045 if (EQ (arg, Qbar))
28047 *width = 2;
28048 return BAR_CURSOR;
28051 if (CONSP (arg)
28052 && EQ (XCAR (arg), Qbar)
28053 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28055 *width = XINT (XCDR (arg));
28056 return BAR_CURSOR;
28059 if (EQ (arg, Qhbar))
28061 *width = 2;
28062 return HBAR_CURSOR;
28065 if (CONSP (arg)
28066 && EQ (XCAR (arg), Qhbar)
28067 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28069 *width = XINT (XCDR (arg));
28070 return HBAR_CURSOR;
28073 /* Treat anything unknown as "hollow box cursor".
28074 It was bad to signal an error; people have trouble fixing
28075 .Xdefaults with Emacs, when it has something bad in it. */
28076 type = HOLLOW_BOX_CURSOR;
28078 return type;
28081 /* Set the default cursor types for specified frame. */
28082 void
28083 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28085 int width = 1;
28086 Lisp_Object tem;
28088 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28089 FRAME_CURSOR_WIDTH (f) = width;
28091 /* By default, set up the blink-off state depending on the on-state. */
28093 tem = Fassoc (arg, Vblink_cursor_alist);
28094 if (!NILP (tem))
28096 FRAME_BLINK_OFF_CURSOR (f)
28097 = get_specified_cursor_type (XCDR (tem), &width);
28098 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28100 else
28101 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28103 /* Make sure the cursor gets redrawn. */
28104 f->cursor_type_changed = true;
28108 #ifdef HAVE_WINDOW_SYSTEM
28110 /* Return the cursor we want to be displayed in window W. Return
28111 width of bar/hbar cursor through WIDTH arg. Return with
28112 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28113 (i.e. if the `system caret' should track this cursor).
28115 In a mini-buffer window, we want the cursor only to appear if we
28116 are reading input from this window. For the selected window, we
28117 want the cursor type given by the frame parameter or buffer local
28118 setting of cursor-type. If explicitly marked off, draw no cursor.
28119 In all other cases, we want a hollow box cursor. */
28121 static enum text_cursor_kinds
28122 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28123 bool *active_cursor)
28125 struct frame *f = XFRAME (w->frame);
28126 struct buffer *b = XBUFFER (w->contents);
28127 int cursor_type = DEFAULT_CURSOR;
28128 Lisp_Object alt_cursor;
28129 bool non_selected = false;
28131 *active_cursor = true;
28133 /* Echo area */
28134 if (cursor_in_echo_area
28135 && FRAME_HAS_MINIBUF_P (f)
28136 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28138 if (w == XWINDOW (echo_area_window))
28140 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28142 *width = FRAME_CURSOR_WIDTH (f);
28143 return FRAME_DESIRED_CURSOR (f);
28145 else
28146 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28149 *active_cursor = false;
28150 non_selected = true;
28153 /* Detect a nonselected window or nonselected frame. */
28154 else if (w != XWINDOW (f->selected_window)
28155 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28157 *active_cursor = false;
28159 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28160 return NO_CURSOR;
28162 non_selected = true;
28165 /* Never display a cursor in a window in which cursor-type is nil. */
28166 if (NILP (BVAR (b, cursor_type)))
28167 return NO_CURSOR;
28169 /* Get the normal cursor type for this window. */
28170 if (EQ (BVAR (b, cursor_type), Qt))
28172 cursor_type = FRAME_DESIRED_CURSOR (f);
28173 *width = FRAME_CURSOR_WIDTH (f);
28175 else
28176 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28178 /* Use cursor-in-non-selected-windows instead
28179 for non-selected window or frame. */
28180 if (non_selected)
28182 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28183 if (!EQ (Qt, alt_cursor))
28184 return get_specified_cursor_type (alt_cursor, width);
28185 /* t means modify the normal cursor type. */
28186 if (cursor_type == FILLED_BOX_CURSOR)
28187 cursor_type = HOLLOW_BOX_CURSOR;
28188 else if (cursor_type == BAR_CURSOR && *width > 1)
28189 --*width;
28190 return cursor_type;
28193 /* Use normal cursor if not blinked off. */
28194 if (!w->cursor_off_p)
28196 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28197 return NO_CURSOR;
28198 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28200 if (cursor_type == FILLED_BOX_CURSOR)
28202 /* Using a block cursor on large images can be very annoying.
28203 So use a hollow cursor for "large" images.
28204 If image is not transparent (no mask), also use hollow cursor. */
28205 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28206 if (img != NULL && IMAGEP (img->spec))
28208 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28209 where N = size of default frame font size.
28210 This should cover most of the "tiny" icons people may use. */
28211 if (!img->mask
28212 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28213 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28214 cursor_type = HOLLOW_BOX_CURSOR;
28217 else if (cursor_type != NO_CURSOR)
28219 /* Display current only supports BOX and HOLLOW cursors for images.
28220 So for now, unconditionally use a HOLLOW cursor when cursor is
28221 not a solid box cursor. */
28222 cursor_type = HOLLOW_BOX_CURSOR;
28225 return cursor_type;
28228 /* Cursor is blinked off, so determine how to "toggle" it. */
28230 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28231 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28232 return get_specified_cursor_type (XCDR (alt_cursor), width);
28234 /* Then see if frame has specified a specific blink off cursor type. */
28235 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28237 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28238 return FRAME_BLINK_OFF_CURSOR (f);
28241 #if false
28242 /* Some people liked having a permanently visible blinking cursor,
28243 while others had very strong opinions against it. So it was
28244 decided to remove it. KFS 2003-09-03 */
28246 /* Finally perform built-in cursor blinking:
28247 filled box <-> hollow box
28248 wide [h]bar <-> narrow [h]bar
28249 narrow [h]bar <-> no cursor
28250 other type <-> no cursor */
28252 if (cursor_type == FILLED_BOX_CURSOR)
28253 return HOLLOW_BOX_CURSOR;
28255 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28257 *width = 1;
28258 return cursor_type;
28260 #endif
28262 return NO_CURSOR;
28266 /* Notice when the text cursor of window W has been completely
28267 overwritten by a drawing operation that outputs glyphs in AREA
28268 starting at X0 and ending at X1 in the line starting at Y0 and
28269 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28270 the rest of the line after X0 has been written. Y coordinates
28271 are window-relative. */
28273 static void
28274 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28275 int x0, int x1, int y0, int y1)
28277 int cx0, cx1, cy0, cy1;
28278 struct glyph_row *row;
28280 if (!w->phys_cursor_on_p)
28281 return;
28282 if (area != TEXT_AREA)
28283 return;
28285 if (w->phys_cursor.vpos < 0
28286 || w->phys_cursor.vpos >= w->current_matrix->nrows
28287 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28288 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28289 return;
28291 if (row->cursor_in_fringe_p)
28293 row->cursor_in_fringe_p = false;
28294 draw_fringe_bitmap (w, row, row->reversed_p);
28295 w->phys_cursor_on_p = false;
28296 return;
28299 cx0 = w->phys_cursor.x;
28300 cx1 = cx0 + w->phys_cursor_width;
28301 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28302 return;
28304 /* The cursor image will be completely removed from the
28305 screen if the output area intersects the cursor area in
28306 y-direction. When we draw in [y0 y1[, and some part of
28307 the cursor is at y < y0, that part must have been drawn
28308 before. When scrolling, the cursor is erased before
28309 actually scrolling, so we don't come here. When not
28310 scrolling, the rows above the old cursor row must have
28311 changed, and in this case these rows must have written
28312 over the cursor image.
28314 Likewise if part of the cursor is below y1, with the
28315 exception of the cursor being in the first blank row at
28316 the buffer and window end because update_text_area
28317 doesn't draw that row. (Except when it does, but
28318 that's handled in update_text_area.) */
28320 cy0 = w->phys_cursor.y;
28321 cy1 = cy0 + w->phys_cursor_height;
28322 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28323 return;
28325 w->phys_cursor_on_p = false;
28328 #endif /* HAVE_WINDOW_SYSTEM */
28331 /************************************************************************
28332 Mouse Face
28333 ************************************************************************/
28335 #ifdef HAVE_WINDOW_SYSTEM
28337 /* EXPORT for RIF:
28338 Fix the display of area AREA of overlapping row ROW in window W
28339 with respect to the overlapping part OVERLAPS. */
28341 void
28342 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28343 enum glyph_row_area area, int overlaps)
28345 int i, x;
28347 block_input ();
28349 x = 0;
28350 for (i = 0; i < row->used[area];)
28352 if (row->glyphs[area][i].overlaps_vertically_p)
28354 int start = i, start_x = x;
28358 x += row->glyphs[area][i].pixel_width;
28359 ++i;
28361 while (i < row->used[area]
28362 && row->glyphs[area][i].overlaps_vertically_p);
28364 draw_glyphs (w, start_x, row, area,
28365 start, i,
28366 DRAW_NORMAL_TEXT, overlaps);
28368 else
28370 x += row->glyphs[area][i].pixel_width;
28371 ++i;
28375 unblock_input ();
28379 /* EXPORT:
28380 Draw the cursor glyph of window W in glyph row ROW. See the
28381 comment of draw_glyphs for the meaning of HL. */
28383 void
28384 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28385 enum draw_glyphs_face hl)
28387 /* If cursor hpos is out of bounds, don't draw garbage. This can
28388 happen in mini-buffer windows when switching between echo area
28389 glyphs and mini-buffer. */
28390 if ((row->reversed_p
28391 ? (w->phys_cursor.hpos >= 0)
28392 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28394 bool on_p = w->phys_cursor_on_p;
28395 int x1;
28396 int hpos = w->phys_cursor.hpos;
28398 /* When the window is hscrolled, cursor hpos can legitimately be
28399 out of bounds, but we draw the cursor at the corresponding
28400 window margin in that case. */
28401 if (!row->reversed_p && hpos < 0)
28402 hpos = 0;
28403 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28404 hpos = row->used[TEXT_AREA] - 1;
28406 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28407 hl, 0);
28408 w->phys_cursor_on_p = on_p;
28410 if (hl == DRAW_CURSOR)
28411 w->phys_cursor_width = x1 - w->phys_cursor.x;
28412 /* When we erase the cursor, and ROW is overlapped by other
28413 rows, make sure that these overlapping parts of other rows
28414 are redrawn. */
28415 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28417 w->phys_cursor_width = x1 - w->phys_cursor.x;
28419 if (row > w->current_matrix->rows
28420 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28421 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28422 OVERLAPS_ERASED_CURSOR);
28424 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28425 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28426 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28427 OVERLAPS_ERASED_CURSOR);
28433 /* Erase the image of a cursor of window W from the screen. */
28435 void
28436 erase_phys_cursor (struct window *w)
28438 struct frame *f = XFRAME (w->frame);
28439 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28440 int hpos = w->phys_cursor.hpos;
28441 int vpos = w->phys_cursor.vpos;
28442 bool mouse_face_here_p = false;
28443 struct glyph_matrix *active_glyphs = w->current_matrix;
28444 struct glyph_row *cursor_row;
28445 struct glyph *cursor_glyph;
28446 enum draw_glyphs_face hl;
28448 /* No cursor displayed or row invalidated => nothing to do on the
28449 screen. */
28450 if (w->phys_cursor_type == NO_CURSOR)
28451 goto mark_cursor_off;
28453 /* VPOS >= active_glyphs->nrows means that window has been resized.
28454 Don't bother to erase the cursor. */
28455 if (vpos >= active_glyphs->nrows)
28456 goto mark_cursor_off;
28458 /* If row containing cursor is marked invalid, there is nothing we
28459 can do. */
28460 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28461 if (!cursor_row->enabled_p)
28462 goto mark_cursor_off;
28464 /* If line spacing is > 0, old cursor may only be partially visible in
28465 window after split-window. So adjust visible height. */
28466 cursor_row->visible_height = min (cursor_row->visible_height,
28467 window_text_bottom_y (w) - cursor_row->y);
28469 /* If row is completely invisible, don't attempt to delete a cursor which
28470 isn't there. This can happen if cursor is at top of a window, and
28471 we switch to a buffer with a header line in that window. */
28472 if (cursor_row->visible_height <= 0)
28473 goto mark_cursor_off;
28475 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28476 if (cursor_row->cursor_in_fringe_p)
28478 cursor_row->cursor_in_fringe_p = false;
28479 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28480 goto mark_cursor_off;
28483 /* This can happen when the new row is shorter than the old one.
28484 In this case, either draw_glyphs or clear_end_of_line
28485 should have cleared the cursor. Note that we wouldn't be
28486 able to erase the cursor in this case because we don't have a
28487 cursor glyph at hand. */
28488 if ((cursor_row->reversed_p
28489 ? (w->phys_cursor.hpos < 0)
28490 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28491 goto mark_cursor_off;
28493 /* When the window is hscrolled, cursor hpos can legitimately be out
28494 of bounds, but we draw the cursor at the corresponding window
28495 margin in that case. */
28496 if (!cursor_row->reversed_p && hpos < 0)
28497 hpos = 0;
28498 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28499 hpos = cursor_row->used[TEXT_AREA] - 1;
28501 /* If the cursor is in the mouse face area, redisplay that when
28502 we clear the cursor. */
28503 if (! NILP (hlinfo->mouse_face_window)
28504 && coords_in_mouse_face_p (w, hpos, vpos)
28505 /* Don't redraw the cursor's spot in mouse face if it is at the
28506 end of a line (on a newline). The cursor appears there, but
28507 mouse highlighting does not. */
28508 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28509 mouse_face_here_p = true;
28511 /* Maybe clear the display under the cursor. */
28512 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28514 int x, y;
28515 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28516 int width;
28518 cursor_glyph = get_phys_cursor_glyph (w);
28519 if (cursor_glyph == NULL)
28520 goto mark_cursor_off;
28522 width = cursor_glyph->pixel_width;
28523 x = w->phys_cursor.x;
28524 if (x < 0)
28526 width += x;
28527 x = 0;
28529 width = min (width, window_box_width (w, TEXT_AREA) - x);
28530 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28531 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28533 if (width > 0)
28534 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28537 /* Erase the cursor by redrawing the character underneath it. */
28538 if (mouse_face_here_p)
28539 hl = DRAW_MOUSE_FACE;
28540 else
28541 hl = DRAW_NORMAL_TEXT;
28542 draw_phys_cursor_glyph (w, cursor_row, hl);
28544 mark_cursor_off:
28545 w->phys_cursor_on_p = false;
28546 w->phys_cursor_type = NO_CURSOR;
28550 /* Display or clear cursor of window W. If !ON, clear the cursor.
28551 If ON, display the cursor; where to put the cursor is specified by
28552 HPOS, VPOS, X and Y. */
28554 void
28555 display_and_set_cursor (struct window *w, bool on,
28556 int hpos, int vpos, int x, int y)
28558 struct frame *f = XFRAME (w->frame);
28559 int new_cursor_type;
28560 int new_cursor_width;
28561 bool active_cursor;
28562 struct glyph_row *glyph_row;
28563 struct glyph *glyph;
28565 /* This is pointless on invisible frames, and dangerous on garbaged
28566 windows and frames; in the latter case, the frame or window may
28567 be in the midst of changing its size, and x and y may be off the
28568 window. */
28569 if (! FRAME_VISIBLE_P (f)
28570 || FRAME_GARBAGED_P (f)
28571 || vpos >= w->current_matrix->nrows
28572 || hpos >= w->current_matrix->matrix_w)
28573 return;
28575 /* If cursor is off and we want it off, return quickly. */
28576 if (!on && !w->phys_cursor_on_p)
28577 return;
28579 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28580 /* If cursor row is not enabled, we don't really know where to
28581 display the cursor. */
28582 if (!glyph_row->enabled_p)
28584 w->phys_cursor_on_p = false;
28585 return;
28588 glyph = NULL;
28589 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28590 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28592 eassert (input_blocked_p ());
28594 /* Set new_cursor_type to the cursor we want to be displayed. */
28595 new_cursor_type = get_window_cursor_type (w, glyph,
28596 &new_cursor_width, &active_cursor);
28598 /* If cursor is currently being shown and we don't want it to be or
28599 it is in the wrong place, or the cursor type is not what we want,
28600 erase it. */
28601 if (w->phys_cursor_on_p
28602 && (!on
28603 || w->phys_cursor.x != x
28604 || w->phys_cursor.y != y
28605 /* HPOS can be negative in R2L rows whose
28606 exact_window_width_line_p flag is set (i.e. their newline
28607 would "overflow into the fringe"). */
28608 || hpos < 0
28609 || new_cursor_type != w->phys_cursor_type
28610 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28611 && new_cursor_width != w->phys_cursor_width)))
28612 erase_phys_cursor (w);
28614 /* Don't check phys_cursor_on_p here because that flag is only set
28615 to false in some cases where we know that the cursor has been
28616 completely erased, to avoid the extra work of erasing the cursor
28617 twice. In other words, phys_cursor_on_p can be true and the cursor
28618 still not be visible, or it has only been partly erased. */
28619 if (on)
28621 w->phys_cursor_ascent = glyph_row->ascent;
28622 w->phys_cursor_height = glyph_row->height;
28624 /* Set phys_cursor_.* before x_draw_.* is called because some
28625 of them may need the information. */
28626 w->phys_cursor.x = x;
28627 w->phys_cursor.y = glyph_row->y;
28628 w->phys_cursor.hpos = hpos;
28629 w->phys_cursor.vpos = vpos;
28632 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28633 new_cursor_type, new_cursor_width,
28634 on, active_cursor);
28638 /* Switch the display of W's cursor on or off, according to the value
28639 of ON. */
28641 static void
28642 update_window_cursor (struct window *w, bool on)
28644 /* Don't update cursor in windows whose frame is in the process
28645 of being deleted. */
28646 if (w->current_matrix)
28648 int hpos = w->phys_cursor.hpos;
28649 int vpos = w->phys_cursor.vpos;
28650 struct glyph_row *row;
28652 if (vpos >= w->current_matrix->nrows
28653 || hpos >= w->current_matrix->matrix_w)
28654 return;
28656 row = MATRIX_ROW (w->current_matrix, vpos);
28658 /* When the window is hscrolled, cursor hpos can legitimately be
28659 out of bounds, but we draw the cursor at the corresponding
28660 window margin in that case. */
28661 if (!row->reversed_p && hpos < 0)
28662 hpos = 0;
28663 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28664 hpos = row->used[TEXT_AREA] - 1;
28666 block_input ();
28667 display_and_set_cursor (w, on, hpos, vpos,
28668 w->phys_cursor.x, w->phys_cursor.y);
28669 unblock_input ();
28674 /* Call update_window_cursor with parameter ON_P on all leaf windows
28675 in the window tree rooted at W. */
28677 static void
28678 update_cursor_in_window_tree (struct window *w, bool on_p)
28680 while (w)
28682 if (WINDOWP (w->contents))
28683 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28684 else
28685 update_window_cursor (w, on_p);
28687 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28692 /* EXPORT:
28693 Display the cursor on window W, or clear it, according to ON_P.
28694 Don't change the cursor's position. */
28696 void
28697 x_update_cursor (struct frame *f, bool on_p)
28699 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28703 /* EXPORT:
28704 Clear the cursor of window W to background color, and mark the
28705 cursor as not shown. This is used when the text where the cursor
28706 is about to be rewritten. */
28708 void
28709 x_clear_cursor (struct window *w)
28711 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28712 update_window_cursor (w, false);
28715 #endif /* HAVE_WINDOW_SYSTEM */
28717 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28718 and MSDOS. */
28719 static void
28720 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28721 int start_hpos, int end_hpos,
28722 enum draw_glyphs_face draw)
28724 #ifdef HAVE_WINDOW_SYSTEM
28725 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28727 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28728 return;
28730 #endif
28731 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28732 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28733 #endif
28736 /* Display the active region described by mouse_face_* according to DRAW. */
28738 static void
28739 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28741 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28742 struct frame *f = XFRAME (WINDOW_FRAME (w));
28744 if (/* If window is in the process of being destroyed, don't bother
28745 to do anything. */
28746 w->current_matrix != NULL
28747 /* Don't update mouse highlight if hidden. */
28748 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28749 /* Recognize when we are called to operate on rows that don't exist
28750 anymore. This can happen when a window is split. */
28751 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28753 bool phys_cursor_on_p = w->phys_cursor_on_p;
28754 struct glyph_row *row, *first, *last;
28756 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28757 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28759 for (row = first; row <= last && row->enabled_p; ++row)
28761 int start_hpos, end_hpos, start_x;
28763 /* For all but the first row, the highlight starts at column 0. */
28764 if (row == first)
28766 /* R2L rows have BEG and END in reversed order, but the
28767 screen drawing geometry is always left to right. So
28768 we need to mirror the beginning and end of the
28769 highlighted area in R2L rows. */
28770 if (!row->reversed_p)
28772 start_hpos = hlinfo->mouse_face_beg_col;
28773 start_x = hlinfo->mouse_face_beg_x;
28775 else if (row == last)
28777 start_hpos = hlinfo->mouse_face_end_col;
28778 start_x = hlinfo->mouse_face_end_x;
28780 else
28782 start_hpos = 0;
28783 start_x = 0;
28786 else if (row->reversed_p && row == last)
28788 start_hpos = hlinfo->mouse_face_end_col;
28789 start_x = hlinfo->mouse_face_end_x;
28791 else
28793 start_hpos = 0;
28794 start_x = 0;
28797 if (row == last)
28799 if (!row->reversed_p)
28800 end_hpos = hlinfo->mouse_face_end_col;
28801 else if (row == first)
28802 end_hpos = hlinfo->mouse_face_beg_col;
28803 else
28805 end_hpos = row->used[TEXT_AREA];
28806 if (draw == DRAW_NORMAL_TEXT)
28807 row->fill_line_p = true; /* Clear to end of line. */
28810 else if (row->reversed_p && row == first)
28811 end_hpos = hlinfo->mouse_face_beg_col;
28812 else
28814 end_hpos = row->used[TEXT_AREA];
28815 if (draw == DRAW_NORMAL_TEXT)
28816 row->fill_line_p = true; /* Clear to end of line. */
28819 if (end_hpos > start_hpos)
28821 draw_row_with_mouse_face (w, start_x, row,
28822 start_hpos, end_hpos, draw);
28824 row->mouse_face_p
28825 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28829 /* When we've written over the cursor, arrange for it to
28830 be displayed again. */
28831 if (FRAME_WINDOW_P (f)
28832 && phys_cursor_on_p && !w->phys_cursor_on_p)
28834 #ifdef HAVE_WINDOW_SYSTEM
28835 int hpos = w->phys_cursor.hpos;
28837 /* When the window is hscrolled, cursor hpos can legitimately be
28838 out of bounds, but we draw the cursor at the corresponding
28839 window margin in that case. */
28840 if (!row->reversed_p && hpos < 0)
28841 hpos = 0;
28842 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28843 hpos = row->used[TEXT_AREA] - 1;
28845 block_input ();
28846 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28847 w->phys_cursor.x, w->phys_cursor.y);
28848 unblock_input ();
28849 #endif /* HAVE_WINDOW_SYSTEM */
28853 #ifdef HAVE_WINDOW_SYSTEM
28854 /* Change the mouse cursor. */
28855 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28857 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28858 if (draw == DRAW_NORMAL_TEXT
28859 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28860 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28861 else
28862 #endif
28863 if (draw == DRAW_MOUSE_FACE)
28864 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28865 else
28866 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28868 #endif /* HAVE_WINDOW_SYSTEM */
28871 /* EXPORT:
28872 Clear out the mouse-highlighted active region.
28873 Redraw it un-highlighted first. Value is true if mouse
28874 face was actually drawn unhighlighted. */
28876 bool
28877 clear_mouse_face (Mouse_HLInfo *hlinfo)
28879 bool cleared
28880 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28881 if (cleared)
28882 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28883 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28884 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28885 hlinfo->mouse_face_window = Qnil;
28886 hlinfo->mouse_face_overlay = Qnil;
28887 return cleared;
28890 /* Return true if the coordinates HPOS and VPOS on windows W are
28891 within the mouse face on that window. */
28892 static bool
28893 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28895 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28897 /* Quickly resolve the easy cases. */
28898 if (!(WINDOWP (hlinfo->mouse_face_window)
28899 && XWINDOW (hlinfo->mouse_face_window) == w))
28900 return false;
28901 if (vpos < hlinfo->mouse_face_beg_row
28902 || vpos > hlinfo->mouse_face_end_row)
28903 return false;
28904 if (vpos > hlinfo->mouse_face_beg_row
28905 && vpos < hlinfo->mouse_face_end_row)
28906 return true;
28908 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28910 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28912 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28913 return true;
28915 else if ((vpos == hlinfo->mouse_face_beg_row
28916 && hpos >= hlinfo->mouse_face_beg_col)
28917 || (vpos == hlinfo->mouse_face_end_row
28918 && hpos < hlinfo->mouse_face_end_col))
28919 return true;
28921 else
28923 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28925 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28926 return true;
28928 else if ((vpos == hlinfo->mouse_face_beg_row
28929 && hpos <= hlinfo->mouse_face_beg_col)
28930 || (vpos == hlinfo->mouse_face_end_row
28931 && hpos > hlinfo->mouse_face_end_col))
28932 return true;
28934 return false;
28938 /* EXPORT:
28939 True if physical cursor of window W is within mouse face. */
28941 bool
28942 cursor_in_mouse_face_p (struct window *w)
28944 int hpos = w->phys_cursor.hpos;
28945 int vpos = w->phys_cursor.vpos;
28946 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28948 /* When the window is hscrolled, cursor hpos can legitimately be out
28949 of bounds, but we draw the cursor at the corresponding window
28950 margin in that case. */
28951 if (!row->reversed_p && hpos < 0)
28952 hpos = 0;
28953 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28954 hpos = row->used[TEXT_AREA] - 1;
28956 return coords_in_mouse_face_p (w, hpos, vpos);
28961 /* Find the glyph rows START_ROW and END_ROW of window W that display
28962 characters between buffer positions START_CHARPOS and END_CHARPOS
28963 (excluding END_CHARPOS). DISP_STRING is a display string that
28964 covers these buffer positions. This is similar to
28965 row_containing_pos, but is more accurate when bidi reordering makes
28966 buffer positions change non-linearly with glyph rows. */
28967 static void
28968 rows_from_pos_range (struct window *w,
28969 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28970 Lisp_Object disp_string,
28971 struct glyph_row **start, struct glyph_row **end)
28973 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28974 int last_y = window_text_bottom_y (w);
28975 struct glyph_row *row;
28977 *start = NULL;
28978 *end = NULL;
28980 while (!first->enabled_p
28981 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28982 first++;
28984 /* Find the START row. */
28985 for (row = first;
28986 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28987 row++)
28989 /* A row can potentially be the START row if the range of the
28990 characters it displays intersects the range
28991 [START_CHARPOS..END_CHARPOS). */
28992 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28993 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28994 /* See the commentary in row_containing_pos, for the
28995 explanation of the complicated way to check whether
28996 some position is beyond the end of the characters
28997 displayed by a row. */
28998 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28999 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29000 && !row->ends_at_zv_p
29001 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29002 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29003 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29004 && !row->ends_at_zv_p
29005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29007 /* Found a candidate row. Now make sure at least one of the
29008 glyphs it displays has a charpos from the range
29009 [START_CHARPOS..END_CHARPOS).
29011 This is not obvious because bidi reordering could make
29012 buffer positions of a row be 1,2,3,102,101,100, and if we
29013 want to highlight characters in [50..60), we don't want
29014 this row, even though [50..60) does intersect [1..103),
29015 the range of character positions given by the row's start
29016 and end positions. */
29017 struct glyph *g = row->glyphs[TEXT_AREA];
29018 struct glyph *e = g + row->used[TEXT_AREA];
29020 while (g < e)
29022 if (((BUFFERP (g->object) || NILP (g->object))
29023 && start_charpos <= g->charpos && g->charpos < end_charpos)
29024 /* A glyph that comes from DISP_STRING is by
29025 definition to be highlighted. */
29026 || EQ (g->object, disp_string))
29027 *start = row;
29028 g++;
29030 if (*start)
29031 break;
29035 /* Find the END row. */
29036 if (!*start
29037 /* If the last row is partially visible, start looking for END
29038 from that row, instead of starting from FIRST. */
29039 && !(row->enabled_p
29040 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29041 row = first;
29042 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29044 struct glyph_row *next = row + 1;
29045 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29047 if (!next->enabled_p
29048 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29049 /* The first row >= START whose range of displayed characters
29050 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29051 is the row END + 1. */
29052 || (start_charpos < next_start
29053 && end_charpos < next_start)
29054 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29055 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29056 && !next->ends_at_zv_p
29057 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29058 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29059 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29060 && !next->ends_at_zv_p
29061 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29063 *end = row;
29064 break;
29066 else
29068 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29069 but none of the characters it displays are in the range, it is
29070 also END + 1. */
29071 struct glyph *g = next->glyphs[TEXT_AREA];
29072 struct glyph *s = g;
29073 struct glyph *e = g + next->used[TEXT_AREA];
29075 while (g < e)
29077 if (((BUFFERP (g->object) || NILP (g->object))
29078 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29079 /* If the buffer position of the first glyph in
29080 the row is equal to END_CHARPOS, it means
29081 the last character to be highlighted is the
29082 newline of ROW, and we must consider NEXT as
29083 END, not END+1. */
29084 || (((!next->reversed_p && g == s)
29085 || (next->reversed_p && g == e - 1))
29086 && (g->charpos == end_charpos
29087 /* Special case for when NEXT is an
29088 empty line at ZV. */
29089 || (g->charpos == -1
29090 && !row->ends_at_zv_p
29091 && next_start == end_charpos)))))
29092 /* A glyph that comes from DISP_STRING is by
29093 definition to be highlighted. */
29094 || EQ (g->object, disp_string))
29095 break;
29096 g++;
29098 if (g == e)
29100 *end = row;
29101 break;
29103 /* The first row that ends at ZV must be the last to be
29104 highlighted. */
29105 else if (next->ends_at_zv_p)
29107 *end = next;
29108 break;
29114 /* This function sets the mouse_face_* elements of HLINFO, assuming
29115 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29116 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29117 for the overlay or run of text properties specifying the mouse
29118 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29119 before-string and after-string that must also be highlighted.
29120 DISP_STRING, if non-nil, is a display string that may cover some
29121 or all of the highlighted text. */
29123 static void
29124 mouse_face_from_buffer_pos (Lisp_Object window,
29125 Mouse_HLInfo *hlinfo,
29126 ptrdiff_t mouse_charpos,
29127 ptrdiff_t start_charpos,
29128 ptrdiff_t end_charpos,
29129 Lisp_Object before_string,
29130 Lisp_Object after_string,
29131 Lisp_Object disp_string)
29133 struct window *w = XWINDOW (window);
29134 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29135 struct glyph_row *r1, *r2;
29136 struct glyph *glyph, *end;
29137 ptrdiff_t ignore, pos;
29138 int x;
29140 eassert (NILP (disp_string) || STRINGP (disp_string));
29141 eassert (NILP (before_string) || STRINGP (before_string));
29142 eassert (NILP (after_string) || STRINGP (after_string));
29144 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29145 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29146 if (r1 == NULL)
29147 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29148 /* If the before-string or display-string contains newlines,
29149 rows_from_pos_range skips to its last row. Move back. */
29150 if (!NILP (before_string) || !NILP (disp_string))
29152 struct glyph_row *prev;
29153 while ((prev = r1 - 1, prev >= first)
29154 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29155 && prev->used[TEXT_AREA] > 0)
29157 struct glyph *beg = prev->glyphs[TEXT_AREA];
29158 glyph = beg + prev->used[TEXT_AREA];
29159 while (--glyph >= beg && NILP (glyph->object));
29160 if (glyph < beg
29161 || !(EQ (glyph->object, before_string)
29162 || EQ (glyph->object, disp_string)))
29163 break;
29164 r1 = prev;
29167 if (r2 == NULL)
29169 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29170 hlinfo->mouse_face_past_end = true;
29172 else if (!NILP (after_string))
29174 /* If the after-string has newlines, advance to its last row. */
29175 struct glyph_row *next;
29176 struct glyph_row *last
29177 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29179 for (next = r2 + 1;
29180 next <= last
29181 && next->used[TEXT_AREA] > 0
29182 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29183 ++next)
29184 r2 = next;
29186 /* The rest of the display engine assumes that mouse_face_beg_row is
29187 either above mouse_face_end_row or identical to it. But with
29188 bidi-reordered continued lines, the row for START_CHARPOS could
29189 be below the row for END_CHARPOS. If so, swap the rows and store
29190 them in correct order. */
29191 if (r1->y > r2->y)
29193 struct glyph_row *tem = r2;
29195 r2 = r1;
29196 r1 = tem;
29199 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29200 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29202 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29203 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29204 could be anywhere in the row and in any order. The strategy
29205 below is to find the leftmost and the rightmost glyph that
29206 belongs to either of these 3 strings, or whose position is
29207 between START_CHARPOS and END_CHARPOS, and highlight all the
29208 glyphs between those two. This may cover more than just the text
29209 between START_CHARPOS and END_CHARPOS if the range of characters
29210 strides the bidi level boundary, e.g. if the beginning is in R2L
29211 text while the end is in L2R text or vice versa. */
29212 if (!r1->reversed_p)
29214 /* This row is in a left to right paragraph. Scan it left to
29215 right. */
29216 glyph = r1->glyphs[TEXT_AREA];
29217 end = glyph + r1->used[TEXT_AREA];
29218 x = r1->x;
29220 /* Skip truncation glyphs at the start of the glyph row. */
29221 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29222 for (; glyph < end
29223 && NILP (glyph->object)
29224 && glyph->charpos < 0;
29225 ++glyph)
29226 x += glyph->pixel_width;
29228 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29229 or DISP_STRING, and the first glyph from buffer whose
29230 position is between START_CHARPOS and END_CHARPOS. */
29231 for (; glyph < end
29232 && !NILP (glyph->object)
29233 && !EQ (glyph->object, disp_string)
29234 && !(BUFFERP (glyph->object)
29235 && (glyph->charpos >= start_charpos
29236 && glyph->charpos < end_charpos));
29237 ++glyph)
29239 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29240 are present at buffer positions between START_CHARPOS and
29241 END_CHARPOS, or if they come from an overlay. */
29242 if (EQ (glyph->object, before_string))
29244 pos = string_buffer_position (before_string,
29245 start_charpos);
29246 /* If pos == 0, it means before_string came from an
29247 overlay, not from a buffer position. */
29248 if (!pos || (pos >= start_charpos && pos < end_charpos))
29249 break;
29251 else if (EQ (glyph->object, after_string))
29253 pos = string_buffer_position (after_string, end_charpos);
29254 if (!pos || (pos >= start_charpos && pos < end_charpos))
29255 break;
29257 x += glyph->pixel_width;
29259 hlinfo->mouse_face_beg_x = x;
29260 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29262 else
29264 /* This row is in a right to left paragraph. Scan it right to
29265 left. */
29266 struct glyph *g;
29268 end = r1->glyphs[TEXT_AREA] - 1;
29269 glyph = end + r1->used[TEXT_AREA];
29271 /* Skip truncation glyphs at the start of the glyph row. */
29272 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29273 for (; glyph > end
29274 && NILP (glyph->object)
29275 && glyph->charpos < 0;
29276 --glyph)
29279 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29280 or DISP_STRING, and the first glyph from buffer whose
29281 position is between START_CHARPOS and END_CHARPOS. */
29282 for (; glyph > end
29283 && !NILP (glyph->object)
29284 && !EQ (glyph->object, disp_string)
29285 && !(BUFFERP (glyph->object)
29286 && (glyph->charpos >= start_charpos
29287 && glyph->charpos < end_charpos));
29288 --glyph)
29290 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29291 are present at buffer positions between START_CHARPOS and
29292 END_CHARPOS, or if they come from an overlay. */
29293 if (EQ (glyph->object, before_string))
29295 pos = string_buffer_position (before_string, start_charpos);
29296 /* If pos == 0, it means before_string came from an
29297 overlay, not from a buffer position. */
29298 if (!pos || (pos >= start_charpos && pos < end_charpos))
29299 break;
29301 else if (EQ (glyph->object, after_string))
29303 pos = string_buffer_position (after_string, end_charpos);
29304 if (!pos || (pos >= start_charpos && pos < end_charpos))
29305 break;
29309 glyph++; /* first glyph to the right of the highlighted area */
29310 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29311 x += g->pixel_width;
29312 hlinfo->mouse_face_beg_x = x;
29313 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29316 /* If the highlight ends in a different row, compute GLYPH and END
29317 for the end row. Otherwise, reuse the values computed above for
29318 the row where the highlight begins. */
29319 if (r2 != r1)
29321 if (!r2->reversed_p)
29323 glyph = r2->glyphs[TEXT_AREA];
29324 end = glyph + r2->used[TEXT_AREA];
29325 x = r2->x;
29327 else
29329 end = r2->glyphs[TEXT_AREA] - 1;
29330 glyph = end + r2->used[TEXT_AREA];
29334 if (!r2->reversed_p)
29336 /* Skip truncation and continuation glyphs near the end of the
29337 row, and also blanks and stretch glyphs inserted by
29338 extend_face_to_end_of_line. */
29339 while (end > glyph
29340 && NILP ((end - 1)->object))
29341 --end;
29342 /* Scan the rest of the glyph row from the end, looking for the
29343 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29344 DISP_STRING, or whose position is between START_CHARPOS
29345 and END_CHARPOS */
29346 for (--end;
29347 end > glyph
29348 && !NILP (end->object)
29349 && !EQ (end->object, disp_string)
29350 && !(BUFFERP (end->object)
29351 && (end->charpos >= start_charpos
29352 && end->charpos < end_charpos));
29353 --end)
29355 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29356 are present at buffer positions between START_CHARPOS and
29357 END_CHARPOS, or if they come from an overlay. */
29358 if (EQ (end->object, before_string))
29360 pos = string_buffer_position (before_string, start_charpos);
29361 if (!pos || (pos >= start_charpos && pos < end_charpos))
29362 break;
29364 else if (EQ (end->object, after_string))
29366 pos = string_buffer_position (after_string, end_charpos);
29367 if (!pos || (pos >= start_charpos && pos < end_charpos))
29368 break;
29371 /* Find the X coordinate of the last glyph to be highlighted. */
29372 for (; glyph <= end; ++glyph)
29373 x += glyph->pixel_width;
29375 hlinfo->mouse_face_end_x = x;
29376 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29378 else
29380 /* Skip truncation and continuation glyphs near the end of the
29381 row, and also blanks and stretch glyphs inserted by
29382 extend_face_to_end_of_line. */
29383 x = r2->x;
29384 end++;
29385 while (end < glyph
29386 && NILP (end->object))
29388 x += end->pixel_width;
29389 ++end;
29391 /* Scan the rest of the glyph row from the end, looking for the
29392 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29393 DISP_STRING, or whose position is between START_CHARPOS
29394 and END_CHARPOS */
29395 for ( ;
29396 end < glyph
29397 && !NILP (end->object)
29398 && !EQ (end->object, disp_string)
29399 && !(BUFFERP (end->object)
29400 && (end->charpos >= start_charpos
29401 && end->charpos < end_charpos));
29402 ++end)
29404 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29405 are present at buffer positions between START_CHARPOS and
29406 END_CHARPOS, or if they come from an overlay. */
29407 if (EQ (end->object, before_string))
29409 pos = string_buffer_position (before_string, start_charpos);
29410 if (!pos || (pos >= start_charpos && pos < end_charpos))
29411 break;
29413 else if (EQ (end->object, after_string))
29415 pos = string_buffer_position (after_string, end_charpos);
29416 if (!pos || (pos >= start_charpos && pos < end_charpos))
29417 break;
29419 x += end->pixel_width;
29421 /* If we exited the above loop because we arrived at the last
29422 glyph of the row, and its buffer position is still not in
29423 range, it means the last character in range is the preceding
29424 newline. Bump the end column and x values to get past the
29425 last glyph. */
29426 if (end == glyph
29427 && BUFFERP (end->object)
29428 && (end->charpos < start_charpos
29429 || end->charpos >= end_charpos))
29431 x += end->pixel_width;
29432 ++end;
29434 hlinfo->mouse_face_end_x = x;
29435 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29438 hlinfo->mouse_face_window = window;
29439 hlinfo->mouse_face_face_id
29440 = face_at_buffer_position (w, mouse_charpos, &ignore,
29441 mouse_charpos + 1,
29442 !hlinfo->mouse_face_hidden, -1);
29443 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29446 /* The following function is not used anymore (replaced with
29447 mouse_face_from_string_pos), but I leave it here for the time
29448 being, in case someone would. */
29450 #if false /* not used */
29452 /* Find the position of the glyph for position POS in OBJECT in
29453 window W's current matrix, and return in *X, *Y the pixel
29454 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29456 RIGHT_P means return the position of the right edge of the glyph.
29457 !RIGHT_P means return the left edge position.
29459 If no glyph for POS exists in the matrix, return the position of
29460 the glyph with the next smaller position that is in the matrix, if
29461 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29462 exists in the matrix, return the position of the glyph with the
29463 next larger position in OBJECT.
29465 Value is true if a glyph was found. */
29467 static bool
29468 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29469 int *hpos, int *vpos, int *x, int *y, bool right_p)
29471 int yb = window_text_bottom_y (w);
29472 struct glyph_row *r;
29473 struct glyph *best_glyph = NULL;
29474 struct glyph_row *best_row = NULL;
29475 int best_x = 0;
29477 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29478 r->enabled_p && r->y < yb;
29479 ++r)
29481 struct glyph *g = r->glyphs[TEXT_AREA];
29482 struct glyph *e = g + r->used[TEXT_AREA];
29483 int gx;
29485 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29486 if (EQ (g->object, object))
29488 if (g->charpos == pos)
29490 best_glyph = g;
29491 best_x = gx;
29492 best_row = r;
29493 goto found;
29495 else if (best_glyph == NULL
29496 || ((eabs (g->charpos - pos)
29497 < eabs (best_glyph->charpos - pos))
29498 && (right_p
29499 ? g->charpos < pos
29500 : g->charpos > pos)))
29502 best_glyph = g;
29503 best_x = gx;
29504 best_row = r;
29509 found:
29511 if (best_glyph)
29513 *x = best_x;
29514 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29516 if (right_p)
29518 *x += best_glyph->pixel_width;
29519 ++*hpos;
29522 *y = best_row->y;
29523 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29526 return best_glyph != NULL;
29528 #endif /* not used */
29530 /* Find the positions of the first and the last glyphs in window W's
29531 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29532 (assumed to be a string), and return in HLINFO's mouse_face_*
29533 members the pixel and column/row coordinates of those glyphs. */
29535 static void
29536 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29537 Lisp_Object object,
29538 ptrdiff_t startpos, ptrdiff_t endpos)
29540 int yb = window_text_bottom_y (w);
29541 struct glyph_row *r;
29542 struct glyph *g, *e;
29543 int gx;
29544 bool found = false;
29546 /* Find the glyph row with at least one position in the range
29547 [STARTPOS..ENDPOS), and the first glyph in that row whose
29548 position belongs to that range. */
29549 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29550 r->enabled_p && r->y < yb;
29551 ++r)
29553 if (!r->reversed_p)
29555 g = r->glyphs[TEXT_AREA];
29556 e = g + r->used[TEXT_AREA];
29557 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29558 if (EQ (g->object, object)
29559 && startpos <= g->charpos && g->charpos < endpos)
29561 hlinfo->mouse_face_beg_row
29562 = MATRIX_ROW_VPOS (r, w->current_matrix);
29563 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29564 hlinfo->mouse_face_beg_x = gx;
29565 found = true;
29566 break;
29569 else
29571 struct glyph *g1;
29573 e = r->glyphs[TEXT_AREA];
29574 g = e + r->used[TEXT_AREA];
29575 for ( ; g > e; --g)
29576 if (EQ ((g-1)->object, object)
29577 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29579 hlinfo->mouse_face_beg_row
29580 = MATRIX_ROW_VPOS (r, w->current_matrix);
29581 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29582 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29583 gx += g1->pixel_width;
29584 hlinfo->mouse_face_beg_x = gx;
29585 found = true;
29586 break;
29589 if (found)
29590 break;
29593 if (!found)
29594 return;
29596 /* Starting with the next row, look for the first row which does NOT
29597 include any glyphs whose positions are in the range. */
29598 for (++r; r->enabled_p && r->y < yb; ++r)
29600 g = r->glyphs[TEXT_AREA];
29601 e = g + r->used[TEXT_AREA];
29602 found = false;
29603 for ( ; g < e; ++g)
29604 if (EQ (g->object, object)
29605 && startpos <= g->charpos && g->charpos < endpos)
29607 found = true;
29608 break;
29610 if (!found)
29611 break;
29614 /* The highlighted region ends on the previous row. */
29615 r--;
29617 /* Set the end row. */
29618 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29620 /* Compute and set the end column and the end column's horizontal
29621 pixel coordinate. */
29622 if (!r->reversed_p)
29624 g = r->glyphs[TEXT_AREA];
29625 e = g + r->used[TEXT_AREA];
29626 for ( ; e > g; --e)
29627 if (EQ ((e-1)->object, object)
29628 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29629 break;
29630 hlinfo->mouse_face_end_col = e - g;
29632 for (gx = r->x; g < e; ++g)
29633 gx += g->pixel_width;
29634 hlinfo->mouse_face_end_x = gx;
29636 else
29638 e = r->glyphs[TEXT_AREA];
29639 g = e + r->used[TEXT_AREA];
29640 for (gx = r->x ; e < g; ++e)
29642 if (EQ (e->object, object)
29643 && startpos <= e->charpos && e->charpos < endpos)
29644 break;
29645 gx += e->pixel_width;
29647 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29648 hlinfo->mouse_face_end_x = gx;
29652 #ifdef HAVE_WINDOW_SYSTEM
29654 /* See if position X, Y is within a hot-spot of an image. */
29656 static bool
29657 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29659 if (!CONSP (hot_spot))
29660 return false;
29662 if (EQ (XCAR (hot_spot), Qrect))
29664 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29665 Lisp_Object rect = XCDR (hot_spot);
29666 Lisp_Object tem;
29667 if (!CONSP (rect))
29668 return false;
29669 if (!CONSP (XCAR (rect)))
29670 return false;
29671 if (!CONSP (XCDR (rect)))
29672 return false;
29673 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29674 return false;
29675 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29676 return false;
29677 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29678 return false;
29679 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29680 return false;
29681 return true;
29683 else if (EQ (XCAR (hot_spot), Qcircle))
29685 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29686 Lisp_Object circ = XCDR (hot_spot);
29687 Lisp_Object lr, lx0, ly0;
29688 if (CONSP (circ)
29689 && CONSP (XCAR (circ))
29690 && (lr = XCDR (circ), NUMBERP (lr))
29691 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29692 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29694 double r = XFLOATINT (lr);
29695 double dx = XINT (lx0) - x;
29696 double dy = XINT (ly0) - y;
29697 return (dx * dx + dy * dy <= r * r);
29700 else if (EQ (XCAR (hot_spot), Qpoly))
29702 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29703 if (VECTORP (XCDR (hot_spot)))
29705 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29706 Lisp_Object *poly = v->contents;
29707 ptrdiff_t n = v->header.size;
29708 ptrdiff_t i;
29709 bool inside = false;
29710 Lisp_Object lx, ly;
29711 int x0, y0;
29713 /* Need an even number of coordinates, and at least 3 edges. */
29714 if (n < 6 || n & 1)
29715 return false;
29717 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29718 If count is odd, we are inside polygon. Pixels on edges
29719 may or may not be included depending on actual geometry of the
29720 polygon. */
29721 if ((lx = poly[n-2], !INTEGERP (lx))
29722 || (ly = poly[n-1], !INTEGERP (lx)))
29723 return false;
29724 x0 = XINT (lx), y0 = XINT (ly);
29725 for (i = 0; i < n; i += 2)
29727 int x1 = x0, y1 = y0;
29728 if ((lx = poly[i], !INTEGERP (lx))
29729 || (ly = poly[i+1], !INTEGERP (ly)))
29730 return false;
29731 x0 = XINT (lx), y0 = XINT (ly);
29733 /* Does this segment cross the X line? */
29734 if (x0 >= x)
29736 if (x1 >= x)
29737 continue;
29739 else if (x1 < x)
29740 continue;
29741 if (y > y0 && y > y1)
29742 continue;
29743 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29744 inside = !inside;
29746 return inside;
29749 return false;
29752 Lisp_Object
29753 find_hot_spot (Lisp_Object map, int x, int y)
29755 while (CONSP (map))
29757 if (CONSP (XCAR (map))
29758 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29759 return XCAR (map);
29760 map = XCDR (map);
29763 return Qnil;
29766 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29767 3, 3, 0,
29768 doc: /* Lookup in image map MAP coordinates X and Y.
29769 An image map is an alist where each element has the format (AREA ID PLIST).
29770 An AREA is specified as either a rectangle, a circle, or a polygon:
29771 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29772 pixel coordinates of the upper left and bottom right corners.
29773 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29774 and the radius of the circle; r may be a float or integer.
29775 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29776 vector describes one corner in the polygon.
29777 Returns the alist element for the first matching AREA in MAP. */)
29778 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29780 if (NILP (map))
29781 return Qnil;
29783 CHECK_NUMBER (x);
29784 CHECK_NUMBER (y);
29786 return find_hot_spot (map,
29787 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29788 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29790 #endif /* HAVE_WINDOW_SYSTEM */
29793 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29794 static void
29795 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29797 #ifdef HAVE_WINDOW_SYSTEM
29798 if (!FRAME_WINDOW_P (f))
29799 return;
29801 /* Do not change cursor shape while dragging mouse. */
29802 if (EQ (do_mouse_tracking, Qdragging))
29803 return;
29805 if (!NILP (pointer))
29807 if (EQ (pointer, Qarrow))
29808 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29809 else if (EQ (pointer, Qhand))
29810 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29811 else if (EQ (pointer, Qtext))
29812 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29813 else if (EQ (pointer, intern ("hdrag")))
29814 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29815 else if (EQ (pointer, intern ("nhdrag")))
29816 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29817 # ifdef HAVE_X_WINDOWS
29818 else if (EQ (pointer, intern ("vdrag")))
29819 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29820 # endif
29821 else if (EQ (pointer, intern ("hourglass")))
29822 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29823 else if (EQ (pointer, Qmodeline))
29824 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29825 else
29826 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29829 if (cursor != No_Cursor)
29830 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29831 #endif
29834 /* Take proper action when mouse has moved to the mode or header line
29835 or marginal area AREA of window W, x-position X and y-position Y.
29836 X is relative to the start of the text display area of W, so the
29837 width of bitmap areas and scroll bars must be subtracted to get a
29838 position relative to the start of the mode line. */
29840 static void
29841 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29842 enum window_part area)
29844 struct window *w = XWINDOW (window);
29845 struct frame *f = XFRAME (w->frame);
29846 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29847 #ifdef HAVE_WINDOW_SYSTEM
29848 Display_Info *dpyinfo;
29849 #endif
29850 Cursor cursor = No_Cursor;
29851 Lisp_Object pointer = Qnil;
29852 int dx, dy, width, height;
29853 ptrdiff_t charpos;
29854 Lisp_Object string, object = Qnil;
29855 Lisp_Object pos UNINIT;
29856 Lisp_Object mouse_face;
29857 int original_x_pixel = x;
29858 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29859 struct glyph_row *row UNINIT;
29861 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29863 int x0;
29864 struct glyph *end;
29866 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29867 returns them in row/column units! */
29868 string = mode_line_string (w, area, &x, &y, &charpos,
29869 &object, &dx, &dy, &width, &height);
29871 row = (area == ON_MODE_LINE
29872 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29873 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29875 /* Find the glyph under the mouse pointer. */
29876 if (row->mode_line_p && row->enabled_p)
29878 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29879 end = glyph + row->used[TEXT_AREA];
29881 for (x0 = original_x_pixel;
29882 glyph < end && x0 >= glyph->pixel_width;
29883 ++glyph)
29884 x0 -= glyph->pixel_width;
29886 if (glyph >= end)
29887 glyph = NULL;
29890 else
29892 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29893 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29894 returns them in row/column units! */
29895 string = marginal_area_string (w, area, &x, &y, &charpos,
29896 &object, &dx, &dy, &width, &height);
29899 Lisp_Object help = Qnil;
29901 #ifdef HAVE_WINDOW_SYSTEM
29902 if (IMAGEP (object))
29904 Lisp_Object image_map, hotspot;
29905 if ((image_map = Fplist_get (XCDR (object), QCmap),
29906 !NILP (image_map))
29907 && (hotspot = find_hot_spot (image_map, dx, dy),
29908 CONSP (hotspot))
29909 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29911 Lisp_Object plist;
29913 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29914 If so, we could look for mouse-enter, mouse-leave
29915 properties in PLIST (and do something...). */
29916 hotspot = XCDR (hotspot);
29917 if (CONSP (hotspot)
29918 && (plist = XCAR (hotspot), CONSP (plist)))
29920 pointer = Fplist_get (plist, Qpointer);
29921 if (NILP (pointer))
29922 pointer = Qhand;
29923 help = Fplist_get (plist, Qhelp_echo);
29924 if (!NILP (help))
29926 help_echo_string = help;
29927 XSETWINDOW (help_echo_window, w);
29928 help_echo_object = w->contents;
29929 help_echo_pos = charpos;
29933 if (NILP (pointer))
29934 pointer = Fplist_get (XCDR (object), QCpointer);
29936 #endif /* HAVE_WINDOW_SYSTEM */
29938 if (STRINGP (string))
29939 pos = make_number (charpos);
29941 /* Set the help text and mouse pointer. If the mouse is on a part
29942 of the mode line without any text (e.g. past the right edge of
29943 the mode line text), use the default help text and pointer. */
29944 if (STRINGP (string) || area == ON_MODE_LINE)
29946 /* Arrange to display the help by setting the global variables
29947 help_echo_string, help_echo_object, and help_echo_pos. */
29948 if (NILP (help))
29950 if (STRINGP (string))
29951 help = Fget_text_property (pos, Qhelp_echo, string);
29953 if (!NILP (help))
29955 help_echo_string = help;
29956 XSETWINDOW (help_echo_window, w);
29957 help_echo_object = string;
29958 help_echo_pos = charpos;
29960 else if (area == ON_MODE_LINE)
29962 Lisp_Object default_help
29963 = buffer_local_value (Qmode_line_default_help_echo,
29964 w->contents);
29966 if (STRINGP (default_help))
29968 help_echo_string = default_help;
29969 XSETWINDOW (help_echo_window, w);
29970 help_echo_object = Qnil;
29971 help_echo_pos = -1;
29976 #ifdef HAVE_WINDOW_SYSTEM
29977 /* Change the mouse pointer according to what is under it. */
29978 if (FRAME_WINDOW_P (f))
29980 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29981 || minibuf_level
29982 || NILP (Vresize_mini_windows));
29984 dpyinfo = FRAME_DISPLAY_INFO (f);
29985 if (STRINGP (string))
29987 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29989 if (NILP (pointer))
29990 pointer = Fget_text_property (pos, Qpointer, string);
29992 /* Change the mouse pointer according to what is under X/Y. */
29993 if (NILP (pointer)
29994 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29996 Lisp_Object map;
29997 map = Fget_text_property (pos, Qlocal_map, string);
29998 if (!KEYMAPP (map))
29999 map = Fget_text_property (pos, Qkeymap, string);
30000 if (!KEYMAPP (map) && draggable)
30001 cursor = dpyinfo->vertical_scroll_bar_cursor;
30004 else if (draggable)
30005 /* Default mode-line pointer. */
30006 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30008 #endif
30011 /* Change the mouse face according to what is under X/Y. */
30012 bool mouse_face_shown = false;
30013 if (STRINGP (string))
30015 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30016 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30017 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30018 && glyph)
30020 Lisp_Object b, e;
30022 struct glyph * tmp_glyph;
30024 int gpos;
30025 int gseq_length;
30026 int total_pixel_width;
30027 ptrdiff_t begpos, endpos, ignore;
30029 int vpos, hpos;
30031 b = Fprevious_single_property_change (make_number (charpos + 1),
30032 Qmouse_face, string, Qnil);
30033 if (NILP (b))
30034 begpos = 0;
30035 else
30036 begpos = XINT (b);
30038 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30039 if (NILP (e))
30040 endpos = SCHARS (string);
30041 else
30042 endpos = XINT (e);
30044 /* Calculate the glyph position GPOS of GLYPH in the
30045 displayed string, relative to the beginning of the
30046 highlighted part of the string.
30048 Note: GPOS is different from CHARPOS. CHARPOS is the
30049 position of GLYPH in the internal string object. A mode
30050 line string format has structures which are converted to
30051 a flattened string by the Emacs Lisp interpreter. The
30052 internal string is an element of those structures. The
30053 displayed string is the flattened string. */
30054 tmp_glyph = row_start_glyph;
30055 while (tmp_glyph < glyph
30056 && (!(EQ (tmp_glyph->object, glyph->object)
30057 && begpos <= tmp_glyph->charpos
30058 && tmp_glyph->charpos < endpos)))
30059 tmp_glyph++;
30060 gpos = glyph - tmp_glyph;
30062 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30063 the highlighted part of the displayed string to which
30064 GLYPH belongs. Note: GSEQ_LENGTH is different from
30065 SCHARS (STRING), because the latter returns the length of
30066 the internal string. */
30067 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30068 tmp_glyph > glyph
30069 && (!(EQ (tmp_glyph->object, glyph->object)
30070 && begpos <= tmp_glyph->charpos
30071 && tmp_glyph->charpos < endpos));
30072 tmp_glyph--)
30074 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30076 /* Calculate the total pixel width of all the glyphs between
30077 the beginning of the highlighted area and GLYPH. */
30078 total_pixel_width = 0;
30079 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30080 total_pixel_width += tmp_glyph->pixel_width;
30082 /* Pre calculation of re-rendering position. Note: X is in
30083 column units here, after the call to mode_line_string or
30084 marginal_area_string. */
30085 hpos = x - gpos;
30086 vpos = (area == ON_MODE_LINE
30087 ? (w->current_matrix)->nrows - 1
30088 : 0);
30090 /* If GLYPH's position is included in the region that is
30091 already drawn in mouse face, we have nothing to do. */
30092 if ( EQ (window, hlinfo->mouse_face_window)
30093 && (!row->reversed_p
30094 ? (hlinfo->mouse_face_beg_col <= hpos
30095 && hpos < hlinfo->mouse_face_end_col)
30096 /* In R2L rows we swap BEG and END, see below. */
30097 : (hlinfo->mouse_face_end_col <= hpos
30098 && hpos < hlinfo->mouse_face_beg_col))
30099 && hlinfo->mouse_face_beg_row == vpos )
30100 return;
30102 if (clear_mouse_face (hlinfo))
30103 cursor = No_Cursor;
30105 if (!row->reversed_p)
30107 hlinfo->mouse_face_beg_col = hpos;
30108 hlinfo->mouse_face_beg_x = original_x_pixel
30109 - (total_pixel_width + dx);
30110 hlinfo->mouse_face_end_col = hpos + gseq_length;
30111 hlinfo->mouse_face_end_x = 0;
30113 else
30115 /* In R2L rows, show_mouse_face expects BEG and END
30116 coordinates to be swapped. */
30117 hlinfo->mouse_face_end_col = hpos;
30118 hlinfo->mouse_face_end_x = original_x_pixel
30119 - (total_pixel_width + dx);
30120 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30121 hlinfo->mouse_face_beg_x = 0;
30124 hlinfo->mouse_face_beg_row = vpos;
30125 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30126 hlinfo->mouse_face_past_end = false;
30127 hlinfo->mouse_face_window = window;
30129 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30130 charpos,
30131 0, &ignore,
30132 glyph->face_id,
30133 true);
30134 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30135 mouse_face_shown = true;
30137 if (NILP (pointer))
30138 pointer = Qhand;
30142 /* If mouse-face doesn't need to be shown, clear any existing
30143 mouse-face. */
30144 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30145 clear_mouse_face (hlinfo);
30147 define_frame_cursor1 (f, cursor, pointer);
30151 /* EXPORT:
30152 Take proper action when the mouse has moved to position X, Y on
30153 frame F with regards to highlighting portions of display that have
30154 mouse-face properties. Also de-highlight portions of display where
30155 the mouse was before, set the mouse pointer shape as appropriate
30156 for the mouse coordinates, and activate help echo (tooltips).
30157 X and Y can be negative or out of range. */
30159 void
30160 note_mouse_highlight (struct frame *f, int x, int y)
30162 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30163 enum window_part part = ON_NOTHING;
30164 Lisp_Object window;
30165 struct window *w;
30166 Cursor cursor = No_Cursor;
30167 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30168 struct buffer *b;
30170 /* When a menu is active, don't highlight because this looks odd. */
30171 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30172 if (popup_activated ())
30173 return;
30174 #endif
30176 if (!f->glyphs_initialized_p
30177 || f->pointer_invisible)
30178 return;
30180 hlinfo->mouse_face_mouse_x = x;
30181 hlinfo->mouse_face_mouse_y = y;
30182 hlinfo->mouse_face_mouse_frame = f;
30184 if (hlinfo->mouse_face_defer)
30185 return;
30187 /* Which window is that in? */
30188 window = window_from_coordinates (f, x, y, &part, true);
30190 /* If displaying active text in another window, clear that. */
30191 if (! EQ (window, hlinfo->mouse_face_window)
30192 /* Also clear if we move out of text area in same window. */
30193 || (!NILP (hlinfo->mouse_face_window)
30194 && !NILP (window)
30195 && part != ON_TEXT
30196 && part != ON_MODE_LINE
30197 && part != ON_HEADER_LINE))
30198 clear_mouse_face (hlinfo);
30200 /* Not on a window -> return. */
30201 if (!WINDOWP (window))
30202 return;
30204 /* Reset help_echo_string. It will get recomputed below. */
30205 help_echo_string = Qnil;
30207 /* Convert to window-relative pixel coordinates. */
30208 w = XWINDOW (window);
30209 frame_to_window_pixel_xy (w, &x, &y);
30211 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30212 /* Handle tool-bar window differently since it doesn't display a
30213 buffer. */
30214 if (EQ (window, f->tool_bar_window))
30216 note_tool_bar_highlight (f, x, y);
30217 return;
30219 #endif
30221 /* Mouse is on the mode, header line or margin? */
30222 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30223 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30225 note_mode_line_or_margin_highlight (window, x, y, part);
30227 #ifdef HAVE_WINDOW_SYSTEM
30228 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30231 /* Show non-text cursor (Bug#16647). */
30232 goto set_cursor;
30234 else
30235 #endif
30236 return;
30239 #ifdef HAVE_WINDOW_SYSTEM
30240 if (part == ON_VERTICAL_BORDER)
30242 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30243 help_echo_string = build_string ("drag-mouse-1: resize");
30245 else if (part == ON_RIGHT_DIVIDER)
30247 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30248 help_echo_string = build_string ("drag-mouse-1: resize");
30250 else if (part == ON_BOTTOM_DIVIDER)
30251 if (! WINDOW_BOTTOMMOST_P (w)
30252 || minibuf_level
30253 || NILP (Vresize_mini_windows))
30255 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30256 help_echo_string = build_string ("drag-mouse-1: resize");
30258 else
30259 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30260 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30261 || part == ON_VERTICAL_SCROLL_BAR
30262 || part == ON_HORIZONTAL_SCROLL_BAR)
30263 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30264 else
30265 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30266 #endif
30268 /* Are we in a window whose display is up to date?
30269 And verify the buffer's text has not changed. */
30270 b = XBUFFER (w->contents);
30271 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30273 int hpos, vpos, dx, dy, area = LAST_AREA;
30274 ptrdiff_t pos;
30275 struct glyph *glyph;
30276 Lisp_Object object;
30277 Lisp_Object mouse_face = Qnil, position;
30278 Lisp_Object *overlay_vec = NULL;
30279 ptrdiff_t i, noverlays;
30280 struct buffer *obuf;
30281 ptrdiff_t obegv, ozv;
30282 bool same_region;
30284 /* Find the glyph under X/Y. */
30285 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30287 #ifdef HAVE_WINDOW_SYSTEM
30288 /* Look for :pointer property on image. */
30289 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30291 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30292 if (img != NULL && IMAGEP (img->spec))
30294 Lisp_Object image_map, hotspot;
30295 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30296 !NILP (image_map))
30297 && (hotspot = find_hot_spot (image_map,
30298 glyph->slice.img.x + dx,
30299 glyph->slice.img.y + dy),
30300 CONSP (hotspot))
30301 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30303 Lisp_Object plist;
30305 /* Could check XCAR (hotspot) to see if we enter/leave
30306 this hot-spot.
30307 If so, we could look for mouse-enter, mouse-leave
30308 properties in PLIST (and do something...). */
30309 hotspot = XCDR (hotspot);
30310 if (CONSP (hotspot)
30311 && (plist = XCAR (hotspot), CONSP (plist)))
30313 pointer = Fplist_get (plist, Qpointer);
30314 if (NILP (pointer))
30315 pointer = Qhand;
30316 help_echo_string = Fplist_get (plist, Qhelp_echo);
30317 if (!NILP (help_echo_string))
30319 help_echo_window = window;
30320 help_echo_object = glyph->object;
30321 help_echo_pos = glyph->charpos;
30325 if (NILP (pointer))
30326 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30329 #endif /* HAVE_WINDOW_SYSTEM */
30331 /* Clear mouse face if X/Y not over text. */
30332 if (glyph == NULL
30333 || area != TEXT_AREA
30334 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30335 /* Glyph's OBJECT is nil for glyphs inserted by the
30336 display engine for its internal purposes, like truncation
30337 and continuation glyphs and blanks beyond the end of
30338 line's text on text terminals. If we are over such a
30339 glyph, we are not over any text. */
30340 || NILP (glyph->object)
30341 /* R2L rows have a stretch glyph at their front, which
30342 stands for no text, whereas L2R rows have no glyphs at
30343 all beyond the end of text. Treat such stretch glyphs
30344 like we do with NULL glyphs in L2R rows. */
30345 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30346 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30347 && glyph->type == STRETCH_GLYPH
30348 && glyph->avoid_cursor_p))
30350 if (clear_mouse_face (hlinfo))
30351 cursor = No_Cursor;
30352 if (FRAME_WINDOW_P (f) && NILP (pointer))
30354 #ifdef HAVE_WINDOW_SYSTEM
30355 if (area != TEXT_AREA)
30356 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30357 else
30358 pointer = Vvoid_text_area_pointer;
30359 #endif
30361 goto set_cursor;
30364 pos = glyph->charpos;
30365 object = glyph->object;
30366 if (!STRINGP (object) && !BUFFERP (object))
30367 goto set_cursor;
30369 /* If we get an out-of-range value, return now; avoid an error. */
30370 if (BUFFERP (object) && pos > BUF_Z (b))
30371 goto set_cursor;
30373 /* Make the window's buffer temporarily current for
30374 overlays_at and compute_char_face. */
30375 obuf = current_buffer;
30376 current_buffer = b;
30377 obegv = BEGV;
30378 ozv = ZV;
30379 BEGV = BEG;
30380 ZV = Z;
30382 /* Is this char mouse-active or does it have help-echo? */
30383 position = make_number (pos);
30385 USE_SAFE_ALLOCA;
30387 if (BUFFERP (object))
30389 /* Put all the overlays we want in a vector in overlay_vec. */
30390 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30391 /* Sort overlays into increasing priority order. */
30392 noverlays = sort_overlays (overlay_vec, noverlays, w);
30394 else
30395 noverlays = 0;
30397 if (NILP (Vmouse_highlight))
30399 clear_mouse_face (hlinfo);
30400 goto check_help_echo;
30403 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30405 if (same_region)
30406 cursor = No_Cursor;
30408 /* Check mouse-face highlighting. */
30409 if (! same_region
30410 /* If there exists an overlay with mouse-face overlapping
30411 the one we are currently highlighting, we have to
30412 check if we enter the overlapping overlay, and then
30413 highlight only that. */
30414 || (OVERLAYP (hlinfo->mouse_face_overlay)
30415 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30417 /* Find the highest priority overlay with a mouse-face. */
30418 Lisp_Object overlay = Qnil;
30419 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30421 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30422 if (!NILP (mouse_face))
30423 overlay = overlay_vec[i];
30426 /* If we're highlighting the same overlay as before, there's
30427 no need to do that again. */
30428 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30429 goto check_help_echo;
30430 hlinfo->mouse_face_overlay = overlay;
30432 /* Clear the display of the old active region, if any. */
30433 if (clear_mouse_face (hlinfo))
30434 cursor = No_Cursor;
30436 /* If no overlay applies, get a text property. */
30437 if (NILP (overlay))
30438 mouse_face = Fget_text_property (position, Qmouse_face, object);
30440 /* Next, compute the bounds of the mouse highlighting and
30441 display it. */
30442 if (!NILP (mouse_face) && STRINGP (object))
30444 /* The mouse-highlighting comes from a display string
30445 with a mouse-face. */
30446 Lisp_Object s, e;
30447 ptrdiff_t ignore;
30449 s = Fprevious_single_property_change
30450 (make_number (pos + 1), Qmouse_face, object, Qnil);
30451 e = Fnext_single_property_change
30452 (position, Qmouse_face, object, Qnil);
30453 if (NILP (s))
30454 s = make_number (0);
30455 if (NILP (e))
30456 e = make_number (SCHARS (object));
30457 mouse_face_from_string_pos (w, hlinfo, object,
30458 XINT (s), XINT (e));
30459 hlinfo->mouse_face_past_end = false;
30460 hlinfo->mouse_face_window = window;
30461 hlinfo->mouse_face_face_id
30462 = face_at_string_position (w, object, pos, 0, &ignore,
30463 glyph->face_id, true);
30464 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30465 cursor = No_Cursor;
30467 else
30469 /* The mouse-highlighting, if any, comes from an overlay
30470 or text property in the buffer. */
30471 Lisp_Object buffer UNINIT;
30472 Lisp_Object disp_string UNINIT;
30474 if (STRINGP (object))
30476 /* If we are on a display string with no mouse-face,
30477 check if the text under it has one. */
30478 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30479 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30480 pos = string_buffer_position (object, start);
30481 if (pos > 0)
30483 mouse_face = get_char_property_and_overlay
30484 (make_number (pos), Qmouse_face, w->contents, &overlay);
30485 buffer = w->contents;
30486 disp_string = object;
30489 else
30491 buffer = object;
30492 disp_string = Qnil;
30495 if (!NILP (mouse_face))
30497 Lisp_Object before, after;
30498 Lisp_Object before_string, after_string;
30499 /* To correctly find the limits of mouse highlight
30500 in a bidi-reordered buffer, we must not use the
30501 optimization of limiting the search in
30502 previous-single-property-change and
30503 next-single-property-change, because
30504 rows_from_pos_range needs the real start and end
30505 positions to DTRT in this case. That's because
30506 the first row visible in a window does not
30507 necessarily display the character whose position
30508 is the smallest. */
30509 Lisp_Object lim1
30510 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30511 ? Fmarker_position (w->start)
30512 : Qnil;
30513 Lisp_Object lim2
30514 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30515 ? make_number (BUF_Z (XBUFFER (buffer))
30516 - w->window_end_pos)
30517 : Qnil;
30519 if (NILP (overlay))
30521 /* Handle the text property case. */
30522 before = Fprevious_single_property_change
30523 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30524 after = Fnext_single_property_change
30525 (make_number (pos), Qmouse_face, buffer, lim2);
30526 before_string = after_string = Qnil;
30528 else
30530 /* Handle the overlay case. */
30531 before = Foverlay_start (overlay);
30532 after = Foverlay_end (overlay);
30533 before_string = Foverlay_get (overlay, Qbefore_string);
30534 after_string = Foverlay_get (overlay, Qafter_string);
30536 if (!STRINGP (before_string)) before_string = Qnil;
30537 if (!STRINGP (after_string)) after_string = Qnil;
30540 mouse_face_from_buffer_pos (window, hlinfo, pos,
30541 NILP (before)
30543 : XFASTINT (before),
30544 NILP (after)
30545 ? BUF_Z (XBUFFER (buffer))
30546 : XFASTINT (after),
30547 before_string, after_string,
30548 disp_string);
30549 cursor = No_Cursor;
30554 check_help_echo:
30556 /* Look for a `help-echo' property. */
30557 if (NILP (help_echo_string)) {
30558 Lisp_Object help, overlay;
30560 /* Check overlays first. */
30561 help = overlay = Qnil;
30562 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30564 overlay = overlay_vec[i];
30565 help = Foverlay_get (overlay, Qhelp_echo);
30568 if (!NILP (help))
30570 help_echo_string = help;
30571 help_echo_window = window;
30572 help_echo_object = overlay;
30573 help_echo_pos = pos;
30575 else
30577 Lisp_Object obj = glyph->object;
30578 ptrdiff_t charpos = glyph->charpos;
30580 /* Try text properties. */
30581 if (STRINGP (obj)
30582 && charpos >= 0
30583 && charpos < SCHARS (obj))
30585 help = Fget_text_property (make_number (charpos),
30586 Qhelp_echo, obj);
30587 if (NILP (help))
30589 /* If the string itself doesn't specify a help-echo,
30590 see if the buffer text ``under'' it does. */
30591 struct glyph_row *r
30592 = MATRIX_ROW (w->current_matrix, vpos);
30593 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30594 ptrdiff_t p = string_buffer_position (obj, start);
30595 if (p > 0)
30597 help = Fget_char_property (make_number (p),
30598 Qhelp_echo, w->contents);
30599 if (!NILP (help))
30601 charpos = p;
30602 obj = w->contents;
30607 else if (BUFFERP (obj)
30608 && charpos >= BEGV
30609 && charpos < ZV)
30610 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30611 obj);
30613 if (!NILP (help))
30615 help_echo_string = help;
30616 help_echo_window = window;
30617 help_echo_object = obj;
30618 help_echo_pos = charpos;
30623 #ifdef HAVE_WINDOW_SYSTEM
30624 /* Look for a `pointer' property. */
30625 if (FRAME_WINDOW_P (f) && NILP (pointer))
30627 /* Check overlays first. */
30628 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30629 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30631 if (NILP (pointer))
30633 Lisp_Object obj = glyph->object;
30634 ptrdiff_t charpos = glyph->charpos;
30636 /* Try text properties. */
30637 if (STRINGP (obj)
30638 && charpos >= 0
30639 && charpos < SCHARS (obj))
30641 pointer = Fget_text_property (make_number (charpos),
30642 Qpointer, obj);
30643 if (NILP (pointer))
30645 /* If the string itself doesn't specify a pointer,
30646 see if the buffer text ``under'' it does. */
30647 struct glyph_row *r
30648 = MATRIX_ROW (w->current_matrix, vpos);
30649 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30650 ptrdiff_t p = string_buffer_position (obj, start);
30651 if (p > 0)
30652 pointer = Fget_char_property (make_number (p),
30653 Qpointer, w->contents);
30656 else if (BUFFERP (obj)
30657 && charpos >= BEGV
30658 && charpos < ZV)
30659 pointer = Fget_text_property (make_number (charpos),
30660 Qpointer, obj);
30663 #endif /* HAVE_WINDOW_SYSTEM */
30665 BEGV = obegv;
30666 ZV = ozv;
30667 current_buffer = obuf;
30668 SAFE_FREE ();
30671 set_cursor:
30672 define_frame_cursor1 (f, cursor, pointer);
30676 /* EXPORT for RIF:
30677 Clear any mouse-face on window W. This function is part of the
30678 redisplay interface, and is called from try_window_id and similar
30679 functions to ensure the mouse-highlight is off. */
30681 void
30682 x_clear_window_mouse_face (struct window *w)
30684 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30685 Lisp_Object window;
30687 block_input ();
30688 XSETWINDOW (window, w);
30689 if (EQ (window, hlinfo->mouse_face_window))
30690 clear_mouse_face (hlinfo);
30691 unblock_input ();
30695 /* EXPORT:
30696 Just discard the mouse face information for frame F, if any.
30697 This is used when the size of F is changed. */
30699 void
30700 cancel_mouse_face (struct frame *f)
30702 Lisp_Object window;
30703 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30705 window = hlinfo->mouse_face_window;
30706 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30707 reset_mouse_highlight (hlinfo);
30712 /***********************************************************************
30713 Exposure Events
30714 ***********************************************************************/
30716 #ifdef HAVE_WINDOW_SYSTEM
30718 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30719 which intersects rectangle R. R is in window-relative coordinates. */
30721 static void
30722 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30723 enum glyph_row_area area)
30725 struct glyph *first = row->glyphs[area];
30726 struct glyph *end = row->glyphs[area] + row->used[area];
30727 struct glyph *last;
30728 int first_x, start_x, x;
30730 if (area == TEXT_AREA && row->fill_line_p)
30731 /* If row extends face to end of line write the whole line. */
30732 draw_glyphs (w, 0, row, area,
30733 0, row->used[area],
30734 DRAW_NORMAL_TEXT, 0);
30735 else
30737 /* Set START_X to the window-relative start position for drawing glyphs of
30738 AREA. The first glyph of the text area can be partially visible.
30739 The first glyphs of other areas cannot. */
30740 start_x = window_box_left_offset (w, area);
30741 x = start_x;
30742 if (area == TEXT_AREA)
30743 x += row->x;
30745 /* Find the first glyph that must be redrawn. */
30746 while (first < end
30747 && x + first->pixel_width < r->x)
30749 x += first->pixel_width;
30750 ++first;
30753 /* Find the last one. */
30754 last = first;
30755 first_x = x;
30756 /* Use a signed int intermediate value to avoid catastrophic
30757 failures due to comparison between signed and unsigned, when
30758 x is negative (can happen for wide images that are hscrolled). */
30759 int r_end = r->x + r->width;
30760 while (last < end && x < r_end)
30762 x += last->pixel_width;
30763 ++last;
30766 /* Repaint. */
30767 if (last > first)
30768 draw_glyphs (w, first_x - start_x, row, area,
30769 first - row->glyphs[area], last - row->glyphs[area],
30770 DRAW_NORMAL_TEXT, 0);
30775 /* Redraw the parts of the glyph row ROW on window W intersecting
30776 rectangle R. R is in window-relative coordinates. Value is
30777 true if mouse-face was overwritten. */
30779 static bool
30780 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30782 eassert (row->enabled_p);
30784 if (row->mode_line_p || w->pseudo_window_p)
30785 draw_glyphs (w, 0, row, TEXT_AREA,
30786 0, row->used[TEXT_AREA],
30787 DRAW_NORMAL_TEXT, 0);
30788 else
30790 if (row->used[LEFT_MARGIN_AREA])
30791 expose_area (w, row, r, LEFT_MARGIN_AREA);
30792 if (row->used[TEXT_AREA])
30793 expose_area (w, row, r, TEXT_AREA);
30794 if (row->used[RIGHT_MARGIN_AREA])
30795 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30796 draw_row_fringe_bitmaps (w, row);
30799 return row->mouse_face_p;
30803 /* Redraw those parts of glyphs rows during expose event handling that
30804 overlap other rows. Redrawing of an exposed line writes over parts
30805 of lines overlapping that exposed line; this function fixes that.
30807 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30808 row in W's current matrix that is exposed and overlaps other rows.
30809 LAST_OVERLAPPING_ROW is the last such row. */
30811 static void
30812 expose_overlaps (struct window *w,
30813 struct glyph_row *first_overlapping_row,
30814 struct glyph_row *last_overlapping_row,
30815 XRectangle *r)
30817 struct glyph_row *row;
30819 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30820 if (row->overlapping_p)
30822 eassert (row->enabled_p && !row->mode_line_p);
30824 row->clip = r;
30825 if (row->used[LEFT_MARGIN_AREA])
30826 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30828 if (row->used[TEXT_AREA])
30829 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30831 if (row->used[RIGHT_MARGIN_AREA])
30832 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30833 row->clip = NULL;
30838 /* Return true if W's cursor intersects rectangle R. */
30840 static bool
30841 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30843 XRectangle cr, result;
30844 struct glyph *cursor_glyph;
30845 struct glyph_row *row;
30847 if (w->phys_cursor.vpos >= 0
30848 && w->phys_cursor.vpos < w->current_matrix->nrows
30849 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30850 row->enabled_p)
30851 && row->cursor_in_fringe_p)
30853 /* Cursor is in the fringe. */
30854 cr.x = window_box_right_offset (w,
30855 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30856 ? RIGHT_MARGIN_AREA
30857 : TEXT_AREA));
30858 cr.y = row->y;
30859 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30860 cr.height = row->height;
30861 return x_intersect_rectangles (&cr, r, &result);
30864 cursor_glyph = get_phys_cursor_glyph (w);
30865 if (cursor_glyph)
30867 /* r is relative to W's box, but w->phys_cursor.x is relative
30868 to left edge of W's TEXT area. Adjust it. */
30869 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30870 cr.y = w->phys_cursor.y;
30871 cr.width = cursor_glyph->pixel_width;
30872 cr.height = w->phys_cursor_height;
30873 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30874 I assume the effect is the same -- and this is portable. */
30875 return x_intersect_rectangles (&cr, r, &result);
30877 /* If we don't understand the format, pretend we're not in the hot-spot. */
30878 return false;
30882 /* EXPORT:
30883 Draw a vertical window border to the right of window W if W doesn't
30884 have vertical scroll bars. */
30886 void
30887 x_draw_vertical_border (struct window *w)
30889 struct frame *f = XFRAME (WINDOW_FRAME (w));
30891 /* We could do better, if we knew what type of scroll-bar the adjacent
30892 windows (on either side) have... But we don't :-(
30893 However, I think this works ok. ++KFS 2003-04-25 */
30895 /* Redraw borders between horizontally adjacent windows. Don't
30896 do it for frames with vertical scroll bars because either the
30897 right scroll bar of a window, or the left scroll bar of its
30898 neighbor will suffice as a border. */
30899 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30900 return;
30902 /* Note: It is necessary to redraw both the left and the right
30903 borders, for when only this single window W is being
30904 redisplayed. */
30905 if (!WINDOW_RIGHTMOST_P (w)
30906 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30908 int x0, x1, y0, y1;
30910 window_box_edges (w, &x0, &y0, &x1, &y1);
30911 y1 -= 1;
30913 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30914 x1 -= 1;
30916 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30919 if (!WINDOW_LEFTMOST_P (w)
30920 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30922 int x0, x1, y0, y1;
30924 window_box_edges (w, &x0, &y0, &x1, &y1);
30925 y1 -= 1;
30927 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30928 x0 -= 1;
30930 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30935 /* Draw window dividers for window W. */
30937 void
30938 x_draw_right_divider (struct window *w)
30940 struct frame *f = WINDOW_XFRAME (w);
30942 if (w->mini || w->pseudo_window_p)
30943 return;
30944 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30946 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30947 int x1 = WINDOW_RIGHT_EDGE_X (w);
30948 int y0 = WINDOW_TOP_EDGE_Y (w);
30949 /* The bottom divider prevails. */
30950 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30952 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30956 static void
30957 x_draw_bottom_divider (struct window *w)
30959 struct frame *f = XFRAME (WINDOW_FRAME (w));
30961 if (w->mini || w->pseudo_window_p)
30962 return;
30963 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30965 int x0 = WINDOW_LEFT_EDGE_X (w);
30966 int x1 = WINDOW_RIGHT_EDGE_X (w);
30967 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30968 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30970 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30974 /* Redraw the part of window W intersection rectangle FR. Pixel
30975 coordinates in FR are frame-relative. Call this function with
30976 input blocked. Value is true if the exposure overwrites
30977 mouse-face. */
30979 static bool
30980 expose_window (struct window *w, XRectangle *fr)
30982 struct frame *f = XFRAME (w->frame);
30983 XRectangle wr, r;
30984 bool mouse_face_overwritten_p = false;
30986 /* If window is not yet fully initialized, do nothing. This can
30987 happen when toolkit scroll bars are used and a window is split.
30988 Reconfiguring the scroll bar will generate an expose for a newly
30989 created window. */
30990 if (w->current_matrix == NULL)
30991 return false;
30993 /* When we're currently updating the window, display and current
30994 matrix usually don't agree. Arrange for a thorough display
30995 later. */
30996 if (w->must_be_updated_p)
30998 SET_FRAME_GARBAGED (f);
30999 return false;
31002 /* Frame-relative pixel rectangle of W. */
31003 wr.x = WINDOW_LEFT_EDGE_X (w);
31004 wr.y = WINDOW_TOP_EDGE_Y (w);
31005 wr.width = WINDOW_PIXEL_WIDTH (w);
31006 wr.height = WINDOW_PIXEL_HEIGHT (w);
31008 if (x_intersect_rectangles (fr, &wr, &r))
31010 int yb = window_text_bottom_y (w);
31011 struct glyph_row *row;
31012 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31014 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31015 r.x, r.y, r.width, r.height));
31017 /* Convert to window coordinates. */
31018 r.x -= WINDOW_LEFT_EDGE_X (w);
31019 r.y -= WINDOW_TOP_EDGE_Y (w);
31021 /* Turn off the cursor. */
31022 bool cursor_cleared_p = (!w->pseudo_window_p
31023 && phys_cursor_in_rect_p (w, &r));
31024 if (cursor_cleared_p)
31025 x_clear_cursor (w);
31027 /* If the row containing the cursor extends face to end of line,
31028 then expose_area might overwrite the cursor outside the
31029 rectangle and thus notice_overwritten_cursor might clear
31030 w->phys_cursor_on_p. We remember the original value and
31031 check later if it is changed. */
31032 bool phys_cursor_on_p = w->phys_cursor_on_p;
31034 /* Use a signed int intermediate value to avoid catastrophic
31035 failures due to comparison between signed and unsigned, when
31036 y0 or y1 is negative (can happen for tall images). */
31037 int r_bottom = r.y + r.height;
31039 /* Update lines intersecting rectangle R. */
31040 first_overlapping_row = last_overlapping_row = NULL;
31041 for (row = w->current_matrix->rows;
31042 row->enabled_p;
31043 ++row)
31045 int y0 = row->y;
31046 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31048 if ((y0 >= r.y && y0 < r_bottom)
31049 || (y1 > r.y && y1 < r_bottom)
31050 || (r.y >= y0 && r.y < y1)
31051 || (r_bottom > y0 && r_bottom < y1))
31053 /* A header line may be overlapping, but there is no need
31054 to fix overlapping areas for them. KFS 2005-02-12 */
31055 if (row->overlapping_p && !row->mode_line_p)
31057 if (first_overlapping_row == NULL)
31058 first_overlapping_row = row;
31059 last_overlapping_row = row;
31062 row->clip = fr;
31063 if (expose_line (w, row, &r))
31064 mouse_face_overwritten_p = true;
31065 row->clip = NULL;
31067 else if (row->overlapping_p)
31069 /* We must redraw a row overlapping the exposed area. */
31070 if (y0 < r.y
31071 ? y0 + row->phys_height > r.y
31072 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31074 if (first_overlapping_row == NULL)
31075 first_overlapping_row = row;
31076 last_overlapping_row = row;
31080 if (y1 >= yb)
31081 break;
31084 /* Display the mode line if there is one. */
31085 if (WINDOW_WANTS_MODELINE_P (w)
31086 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31087 row->enabled_p)
31088 && row->y < r_bottom)
31090 if (expose_line (w, row, &r))
31091 mouse_face_overwritten_p = true;
31094 if (!w->pseudo_window_p)
31096 /* Fix the display of overlapping rows. */
31097 if (first_overlapping_row)
31098 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31099 fr);
31101 /* Draw border between windows. */
31102 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31103 x_draw_right_divider (w);
31104 else
31105 x_draw_vertical_border (w);
31107 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31108 x_draw_bottom_divider (w);
31110 /* Turn the cursor on again. */
31111 if (cursor_cleared_p
31112 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31113 update_window_cursor (w, true);
31117 return mouse_face_overwritten_p;
31122 /* Redraw (parts) of all windows in the window tree rooted at W that
31123 intersect R. R contains frame pixel coordinates. Value is
31124 true if the exposure overwrites mouse-face. */
31126 static bool
31127 expose_window_tree (struct window *w, XRectangle *r)
31129 struct frame *f = XFRAME (w->frame);
31130 bool mouse_face_overwritten_p = false;
31132 while (w && !FRAME_GARBAGED_P (f))
31134 mouse_face_overwritten_p
31135 |= (WINDOWP (w->contents)
31136 ? expose_window_tree (XWINDOW (w->contents), r)
31137 : expose_window (w, r));
31139 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31142 return mouse_face_overwritten_p;
31146 /* EXPORT:
31147 Redisplay an exposed area of frame F. X and Y are the upper-left
31148 corner of the exposed rectangle. W and H are width and height of
31149 the exposed area. All are pixel values. W or H zero means redraw
31150 the entire frame. */
31152 void
31153 expose_frame (struct frame *f, int x, int y, int w, int h)
31155 XRectangle r;
31156 bool mouse_face_overwritten_p = false;
31158 TRACE ((stderr, "expose_frame "));
31160 /* No need to redraw if frame will be redrawn soon. */
31161 if (FRAME_GARBAGED_P (f))
31163 TRACE ((stderr, " garbaged\n"));
31164 return;
31167 /* If basic faces haven't been realized yet, there is no point in
31168 trying to redraw anything. This can happen when we get an expose
31169 event while Emacs is starting, e.g. by moving another window. */
31170 if (FRAME_FACE_CACHE (f) == NULL
31171 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31173 TRACE ((stderr, " no faces\n"));
31174 return;
31177 if (w == 0 || h == 0)
31179 r.x = r.y = 0;
31180 r.width = FRAME_TEXT_WIDTH (f);
31181 r.height = FRAME_TEXT_HEIGHT (f);
31183 else
31185 r.x = x;
31186 r.y = y;
31187 r.width = w;
31188 r.height = h;
31191 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31192 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31194 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31195 if (WINDOWP (f->tool_bar_window))
31196 mouse_face_overwritten_p
31197 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31198 #endif
31200 #ifdef HAVE_X_WINDOWS
31201 #ifndef MSDOS
31202 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31203 if (WINDOWP (f->menu_bar_window))
31204 mouse_face_overwritten_p
31205 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31206 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31207 #endif
31208 #endif
31210 /* Some window managers support a focus-follows-mouse style with
31211 delayed raising of frames. Imagine a partially obscured frame,
31212 and moving the mouse into partially obscured mouse-face on that
31213 frame. The visible part of the mouse-face will be highlighted,
31214 then the WM raises the obscured frame. With at least one WM, KDE
31215 2.1, Emacs is not getting any event for the raising of the frame
31216 (even tried with SubstructureRedirectMask), only Expose events.
31217 These expose events will draw text normally, i.e. not
31218 highlighted. Which means we must redo the highlight here.
31219 Subsume it under ``we love X''. --gerd 2001-08-15 */
31220 /* Included in Windows version because Windows most likely does not
31221 do the right thing if any third party tool offers
31222 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31223 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31225 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31226 if (f == hlinfo->mouse_face_mouse_frame)
31228 int mouse_x = hlinfo->mouse_face_mouse_x;
31229 int mouse_y = hlinfo->mouse_face_mouse_y;
31230 clear_mouse_face (hlinfo);
31231 note_mouse_highlight (f, mouse_x, mouse_y);
31237 /* EXPORT:
31238 Determine the intersection of two rectangles R1 and R2. Return
31239 the intersection in *RESULT. Value is true if RESULT is not
31240 empty. */
31242 bool
31243 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31245 XRectangle *left, *right;
31246 XRectangle *upper, *lower;
31247 bool intersection_p = false;
31249 /* Rearrange so that R1 is the left-most rectangle. */
31250 if (r1->x < r2->x)
31251 left = r1, right = r2;
31252 else
31253 left = r2, right = r1;
31255 /* X0 of the intersection is right.x0, if this is inside R1,
31256 otherwise there is no intersection. */
31257 if (right->x <= left->x + left->width)
31259 result->x = right->x;
31261 /* The right end of the intersection is the minimum of
31262 the right ends of left and right. */
31263 result->width = (min (left->x + left->width, right->x + right->width)
31264 - result->x);
31266 /* Same game for Y. */
31267 if (r1->y < r2->y)
31268 upper = r1, lower = r2;
31269 else
31270 upper = r2, lower = r1;
31272 /* The upper end of the intersection is lower.y0, if this is inside
31273 of upper. Otherwise, there is no intersection. */
31274 if (lower->y <= upper->y + upper->height)
31276 result->y = lower->y;
31278 /* The lower end of the intersection is the minimum of the lower
31279 ends of upper and lower. */
31280 result->height = (min (lower->y + lower->height,
31281 upper->y + upper->height)
31282 - result->y);
31283 intersection_p = true;
31287 return intersection_p;
31290 #endif /* HAVE_WINDOW_SYSTEM */
31293 /***********************************************************************
31294 Initialization
31295 ***********************************************************************/
31297 void
31298 syms_of_xdisp (void)
31300 Vwith_echo_area_save_vector = Qnil;
31301 staticpro (&Vwith_echo_area_save_vector);
31303 Vmessage_stack = Qnil;
31304 staticpro (&Vmessage_stack);
31306 /* Non-nil means don't actually do any redisplay. */
31307 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31309 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31311 DEFVAR_BOOL("inhibit-message", inhibit_message,
31312 doc: /* Non-nil means calls to `message' are not displayed.
31313 They are still logged to the *Messages* buffer. */);
31314 inhibit_message = 0;
31316 message_dolog_marker1 = Fmake_marker ();
31317 staticpro (&message_dolog_marker1);
31318 message_dolog_marker2 = Fmake_marker ();
31319 staticpro (&message_dolog_marker2);
31320 message_dolog_marker3 = Fmake_marker ();
31321 staticpro (&message_dolog_marker3);
31323 #ifdef GLYPH_DEBUG
31324 defsubr (&Sdump_frame_glyph_matrix);
31325 defsubr (&Sdump_glyph_matrix);
31326 defsubr (&Sdump_glyph_row);
31327 defsubr (&Sdump_tool_bar_row);
31328 defsubr (&Strace_redisplay);
31329 defsubr (&Strace_to_stderr);
31330 #endif
31331 #ifdef HAVE_WINDOW_SYSTEM
31332 defsubr (&Stool_bar_height);
31333 defsubr (&Slookup_image_map);
31334 #endif
31335 defsubr (&Sline_pixel_height);
31336 defsubr (&Sformat_mode_line);
31337 defsubr (&Sinvisible_p);
31338 defsubr (&Scurrent_bidi_paragraph_direction);
31339 defsubr (&Swindow_text_pixel_size);
31340 defsubr (&Smove_point_visually);
31341 defsubr (&Sbidi_find_overridden_directionality);
31343 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31344 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31345 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31346 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31347 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31348 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31349 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31350 DEFSYM (Qeval, "eval");
31351 DEFSYM (QCdata, ":data");
31353 /* Names of text properties relevant for redisplay. */
31354 DEFSYM (Qdisplay, "display");
31355 DEFSYM (Qspace_width, "space-width");
31356 DEFSYM (Qraise, "raise");
31357 DEFSYM (Qslice, "slice");
31358 DEFSYM (Qspace, "space");
31359 DEFSYM (Qmargin, "margin");
31360 DEFSYM (Qpointer, "pointer");
31361 DEFSYM (Qleft_margin, "left-margin");
31362 DEFSYM (Qright_margin, "right-margin");
31363 DEFSYM (Qcenter, "center");
31364 DEFSYM (Qline_height, "line-height");
31365 DEFSYM (QCalign_to, ":align-to");
31366 DEFSYM (QCrelative_width, ":relative-width");
31367 DEFSYM (QCrelative_height, ":relative-height");
31368 DEFSYM (QCeval, ":eval");
31369 DEFSYM (QCpropertize, ":propertize");
31370 DEFSYM (QCfile, ":file");
31371 DEFSYM (Qfontified, "fontified");
31372 DEFSYM (Qfontification_functions, "fontification-functions");
31374 /* Name of the face used to highlight trailing whitespace. */
31375 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31377 /* Name and number of the face used to highlight escape glyphs. */
31378 DEFSYM (Qescape_glyph, "escape-glyph");
31380 /* Name and number of the face used to highlight non-breaking
31381 spaces/hyphens. */
31382 DEFSYM (Qnobreak_space, "nobreak-space");
31383 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31385 /* The symbol 'image' which is the car of the lists used to represent
31386 images in Lisp. Also a tool bar style. */
31387 DEFSYM (Qimage, "image");
31389 /* Tool bar styles. */
31390 DEFSYM (Qtext, "text");
31391 DEFSYM (Qboth, "both");
31392 DEFSYM (Qboth_horiz, "both-horiz");
31393 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31395 /* The image map types. */
31396 DEFSYM (QCmap, ":map");
31397 DEFSYM (QCpointer, ":pointer");
31398 DEFSYM (Qrect, "rect");
31399 DEFSYM (Qcircle, "circle");
31400 DEFSYM (Qpoly, "poly");
31402 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31404 DEFSYM (Qgrow_only, "grow-only");
31405 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31406 DEFSYM (Qposition, "position");
31407 DEFSYM (Qbuffer_position, "buffer-position");
31408 DEFSYM (Qobject, "object");
31410 /* Cursor shapes. */
31411 DEFSYM (Qbar, "bar");
31412 DEFSYM (Qhbar, "hbar");
31413 DEFSYM (Qbox, "box");
31414 DEFSYM (Qhollow, "hollow");
31416 /* Pointer shapes. */
31417 DEFSYM (Qhand, "hand");
31418 DEFSYM (Qarrow, "arrow");
31419 /* also Qtext */
31421 DEFSYM (Qdragging, "dragging");
31423 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31425 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31426 staticpro (&list_of_error);
31428 /* Values of those variables at last redisplay are stored as
31429 properties on 'overlay-arrow-position' symbol. However, if
31430 Voverlay_arrow_position is a marker, last-arrow-position is its
31431 numerical position. */
31432 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31433 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31435 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31436 properties on a symbol in overlay-arrow-variable-list. */
31437 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31438 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31440 echo_buffer[0] = echo_buffer[1] = Qnil;
31441 staticpro (&echo_buffer[0]);
31442 staticpro (&echo_buffer[1]);
31444 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31445 staticpro (&echo_area_buffer[0]);
31446 staticpro (&echo_area_buffer[1]);
31448 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31449 staticpro (&Vmessages_buffer_name);
31451 mode_line_proptrans_alist = Qnil;
31452 staticpro (&mode_line_proptrans_alist);
31453 mode_line_string_list = Qnil;
31454 staticpro (&mode_line_string_list);
31455 mode_line_string_face = Qnil;
31456 staticpro (&mode_line_string_face);
31457 mode_line_string_face_prop = Qnil;
31458 staticpro (&mode_line_string_face_prop);
31459 Vmode_line_unwind_vector = Qnil;
31460 staticpro (&Vmode_line_unwind_vector);
31462 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31464 help_echo_string = Qnil;
31465 staticpro (&help_echo_string);
31466 help_echo_object = Qnil;
31467 staticpro (&help_echo_object);
31468 help_echo_window = Qnil;
31469 staticpro (&help_echo_window);
31470 previous_help_echo_string = Qnil;
31471 staticpro (&previous_help_echo_string);
31472 help_echo_pos = -1;
31474 DEFSYM (Qright_to_left, "right-to-left");
31475 DEFSYM (Qleft_to_right, "left-to-right");
31476 defsubr (&Sbidi_resolved_levels);
31478 #ifdef HAVE_WINDOW_SYSTEM
31479 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31480 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31481 For example, if a block cursor is over a tab, it will be drawn as
31482 wide as that tab on the display. */);
31483 x_stretch_cursor_p = 0;
31484 #endif
31486 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31487 doc: /* Non-nil means highlight trailing whitespace.
31488 The face used for trailing whitespace is `trailing-whitespace'. */);
31489 Vshow_trailing_whitespace = Qnil;
31491 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31492 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31493 If the value is t, Emacs highlights non-ASCII chars which have the
31494 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31495 or `nobreak-hyphen' face respectively.
31497 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31498 U+2011 (non-breaking hyphen) are affected.
31500 Any other non-nil value means to display these characters as a escape
31501 glyph followed by an ordinary space or hyphen.
31503 A value of nil means no special handling of these characters. */);
31504 Vnobreak_char_display = Qt;
31506 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31507 doc: /* The pointer shape to show in void text areas.
31508 A value of nil means to show the text pointer. Other options are
31509 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31510 `hourglass'. */);
31511 Vvoid_text_area_pointer = Qarrow;
31513 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31514 doc: /* Non-nil means don't actually do any redisplay.
31515 This is used for internal purposes. */);
31516 Vinhibit_redisplay = Qnil;
31518 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31519 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31520 Vglobal_mode_string = Qnil;
31522 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31523 doc: /* Marker for where to display an arrow on top of the buffer text.
31524 This must be the beginning of a line in order to work.
31525 See also `overlay-arrow-string'. */);
31526 Voverlay_arrow_position = Qnil;
31528 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31529 doc: /* String to display as an arrow in non-window frames.
31530 See also `overlay-arrow-position'. */);
31531 Voverlay_arrow_string = build_pure_c_string ("=>");
31533 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31534 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31535 The symbols on this list are examined during redisplay to determine
31536 where to display overlay arrows. */);
31537 Voverlay_arrow_variable_list
31538 = list1 (intern_c_string ("overlay-arrow-position"));
31540 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31541 doc: /* The number of lines to try scrolling a window by when point moves out.
31542 If that fails to bring point back on frame, point is centered instead.
31543 If this is zero, point is always centered after it moves off frame.
31544 If you want scrolling to always be a line at a time, you should set
31545 `scroll-conservatively' to a large value rather than set this to 1. */);
31547 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31548 doc: /* Scroll up to this many lines, to bring point back on screen.
31549 If point moves off-screen, redisplay will scroll by up to
31550 `scroll-conservatively' lines in order to bring point just barely
31551 onto the screen again. If that cannot be done, then redisplay
31552 recenters point as usual.
31554 If the value is greater than 100, redisplay will never recenter point,
31555 but will always scroll just enough text to bring point into view, even
31556 if you move far away.
31558 A value of zero means always recenter point if it moves off screen. */);
31559 scroll_conservatively = 0;
31561 DEFVAR_INT ("scroll-margin", scroll_margin,
31562 doc: /* Number of lines of margin at the top and bottom of a window.
31563 Recenter the window whenever point gets within this many lines
31564 of the top or bottom of the window. */);
31565 scroll_margin = 0;
31567 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31568 doc: /* Pixels per inch value for non-window system displays.
31569 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31570 Vdisplay_pixels_per_inch = make_float (72.0);
31572 #ifdef GLYPH_DEBUG
31573 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31574 #endif
31576 DEFVAR_LISP ("truncate-partial-width-windows",
31577 Vtruncate_partial_width_windows,
31578 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31579 For an integer value, truncate lines in each window narrower than the
31580 full frame width, provided the total window width in column units is less
31581 than that integer; otherwise, respect the value of `truncate-lines'.
31582 The total width of the window is as returned by `window-total-width', it
31583 includes the fringes, the continuation and truncation glyphs, the
31584 display margins (if any), and the scroll bar
31586 For any other non-nil value, truncate lines in all windows that do
31587 not span the full frame width.
31589 A value of nil means to respect the value of `truncate-lines'.
31591 If `word-wrap' is enabled, you might want to reduce this. */);
31592 Vtruncate_partial_width_windows = make_number (50);
31594 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31595 doc: /* Maximum buffer size for which line number should be displayed.
31596 If the buffer is bigger than this, the line number does not appear
31597 in the mode line. A value of nil means no limit. */);
31598 Vline_number_display_limit = Qnil;
31600 DEFVAR_INT ("line-number-display-limit-width",
31601 line_number_display_limit_width,
31602 doc: /* Maximum line width (in characters) for line number display.
31603 If the average length of the lines near point is bigger than this, then the
31604 line number may be omitted from the mode line. */);
31605 line_number_display_limit_width = 200;
31607 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31608 doc: /* Non-nil means highlight region even in nonselected windows. */);
31609 highlight_nonselected_windows = false;
31611 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31612 doc: /* Non-nil if more than one frame is visible on this display.
31613 Minibuffer-only frames don't count, but iconified frames do.
31614 This variable is not guaranteed to be accurate except while processing
31615 `frame-title-format' and `icon-title-format'. */);
31617 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31618 doc: /* Template for displaying the title bar of visible frames.
31619 \(Assuming the window manager supports this feature.)
31621 This variable has the same structure as `mode-line-format', except that
31622 the %c and %l constructs are ignored. It is used only on frames for
31623 which no explicit name has been set (see `modify-frame-parameters'). */);
31625 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31626 doc: /* Template for displaying the title bar of an iconified frame.
31627 \(Assuming the window manager supports this feature.)
31628 This variable has the same structure as `mode-line-format' (which see),
31629 and is used only on frames for which no explicit name has been set
31630 \(see `modify-frame-parameters'). */);
31631 Vicon_title_format
31632 = Vframe_title_format
31633 = listn (CONSTYPE_PURE, 3,
31634 intern_c_string ("multiple-frames"),
31635 build_pure_c_string ("%b"),
31636 listn (CONSTYPE_PURE, 4,
31637 empty_unibyte_string,
31638 intern_c_string ("invocation-name"),
31639 build_pure_c_string ("@"),
31640 intern_c_string ("system-name")));
31642 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31643 doc: /* Maximum number of lines to keep in the message log buffer.
31644 If nil, disable message logging. If t, log messages but don't truncate
31645 the buffer when it becomes large. */);
31646 Vmessage_log_max = make_number (1000);
31648 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31649 doc: /* List of functions to call before redisplaying a window with scrolling.
31650 Each function is called with two arguments, the window and its new
31651 display-start position.
31652 These functions are called whenever the `window-start' marker is modified,
31653 either to point into another buffer (e.g. via `set-window-buffer') or another
31654 place in the same buffer.
31655 Note that the value of `window-end' is not valid when these functions are
31656 called.
31658 Warning: Do not use this feature to alter the way the window
31659 is scrolled. It is not designed for that, and such use probably won't
31660 work. */);
31661 Vwindow_scroll_functions = Qnil;
31663 DEFVAR_LISP ("window-text-change-functions",
31664 Vwindow_text_change_functions,
31665 doc: /* Functions to call in redisplay when text in the window might change. */);
31666 Vwindow_text_change_functions = Qnil;
31668 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31669 doc: /* Functions called when redisplay of a window reaches the end trigger.
31670 Each function is called with two arguments, the window and the end trigger value.
31671 See `set-window-redisplay-end-trigger'. */);
31672 Vredisplay_end_trigger_functions = Qnil;
31674 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31675 doc: /* Non-nil means autoselect window with mouse pointer.
31676 If nil, do not autoselect windows.
31677 A positive number means delay autoselection by that many seconds: a
31678 window is autoselected only after the mouse has remained in that
31679 window for the duration of the delay.
31680 A negative number has a similar effect, but causes windows to be
31681 autoselected only after the mouse has stopped moving. (Because of
31682 the way Emacs compares mouse events, you will occasionally wait twice
31683 that time before the window gets selected.)
31684 Any other value means to autoselect window instantaneously when the
31685 mouse pointer enters it.
31687 Autoselection selects the minibuffer only if it is active, and never
31688 unselects the minibuffer if it is active.
31690 When customizing this variable make sure that the actual value of
31691 `focus-follows-mouse' matches the behavior of your window manager. */);
31692 Vmouse_autoselect_window = Qnil;
31694 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31695 doc: /* Non-nil means automatically resize tool-bars.
31696 This dynamically changes the tool-bar's height to the minimum height
31697 that is needed to make all tool-bar items visible.
31698 If value is `grow-only', the tool-bar's height is only increased
31699 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31700 Vauto_resize_tool_bars = Qt;
31702 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31703 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31704 auto_raise_tool_bar_buttons_p = true;
31706 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31707 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31708 make_cursor_line_fully_visible_p = true;
31710 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31711 doc: /* Border below tool-bar in pixels.
31712 If an integer, use it as the height of the border.
31713 If it is one of `internal-border-width' or `border-width', use the
31714 value of the corresponding frame parameter.
31715 Otherwise, no border is added below the tool-bar. */);
31716 Vtool_bar_border = Qinternal_border_width;
31718 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31719 doc: /* Margin around tool-bar buttons in pixels.
31720 If an integer, use that for both horizontal and vertical margins.
31721 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31722 HORZ specifying the horizontal margin, and VERT specifying the
31723 vertical margin. */);
31724 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31726 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31727 doc: /* Relief thickness of tool-bar buttons. */);
31728 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31730 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31731 doc: /* Tool bar style to use.
31732 It can be one of
31733 image - show images only
31734 text - show text only
31735 both - show both, text below image
31736 both-horiz - show text to the right of the image
31737 text-image-horiz - show text to the left of the image
31738 any other - use system default or image if no system default.
31740 This variable only affects the GTK+ toolkit version of Emacs. */);
31741 Vtool_bar_style = Qnil;
31743 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31744 doc: /* Maximum number of characters a label can have to be shown.
31745 The tool bar style must also show labels for this to have any effect, see
31746 `tool-bar-style'. */);
31747 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31749 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31750 doc: /* List of functions to call to fontify regions of text.
31751 Each function is called with one argument POS. Functions must
31752 fontify a region starting at POS in the current buffer, and give
31753 fontified regions the property `fontified'. */);
31754 Vfontification_functions = Qnil;
31755 Fmake_variable_buffer_local (Qfontification_functions);
31757 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31758 unibyte_display_via_language_environment,
31759 doc: /* Non-nil means display unibyte text according to language environment.
31760 Specifically, this means that raw bytes in the range 160-255 decimal
31761 are displayed by converting them to the equivalent multibyte characters
31762 according to the current language environment. As a result, they are
31763 displayed according to the current fontset.
31765 Note that this variable affects only how these bytes are displayed,
31766 but does not change the fact they are interpreted as raw bytes. */);
31767 unibyte_display_via_language_environment = false;
31769 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31770 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31771 If a float, it specifies a fraction of the mini-window frame's height.
31772 If an integer, it specifies a number of lines. */);
31773 Vmax_mini_window_height = make_float (0.25);
31775 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31776 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31777 A value of nil means don't automatically resize mini-windows.
31778 A value of t means resize them to fit the text displayed in them.
31779 A value of `grow-only', the default, means let mini-windows grow only;
31780 they return to their normal size when the minibuffer is closed, or the
31781 echo area becomes empty. */);
31782 /* Contrary to the doc string, we initialize this to nil, so that
31783 loading loadup.el won't try to resize windows before loading
31784 window.el, where some functions we need to call for this live.
31785 We assign the 'grow-only' value right after loading window.el
31786 during loadup. */
31787 Vresize_mini_windows = Qnil;
31789 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31790 doc: /* Alist specifying how to blink the cursor off.
31791 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31792 `cursor-type' frame-parameter or variable equals ON-STATE,
31793 comparing using `equal', Emacs uses OFF-STATE to specify
31794 how to blink it off. ON-STATE and OFF-STATE are values for
31795 the `cursor-type' frame parameter.
31797 If a frame's ON-STATE has no entry in this list,
31798 the frame's other specifications determine how to blink the cursor off. */);
31799 Vblink_cursor_alist = Qnil;
31801 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31802 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31803 If non-nil, windows are automatically scrolled horizontally to make
31804 point visible. */);
31805 automatic_hscrolling_p = true;
31806 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31808 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31809 doc: /* How many columns away from the window edge point is allowed to get
31810 before automatic hscrolling will horizontally scroll the window. */);
31811 hscroll_margin = 5;
31813 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31814 doc: /* How many columns to scroll the window when point gets too close to the edge.
31815 When point is less than `hscroll-margin' columns from the window
31816 edge, automatic hscrolling will scroll the window by the amount of columns
31817 determined by this variable. If its value is a positive integer, scroll that
31818 many columns. If it's a positive floating-point number, it specifies the
31819 fraction of the window's width to scroll. If it's nil or zero, point will be
31820 centered horizontally after the scroll. Any other value, including negative
31821 numbers, are treated as if the value were zero.
31823 Automatic hscrolling always moves point outside the scroll margin, so if
31824 point was more than scroll step columns inside the margin, the window will
31825 scroll more than the value given by the scroll step.
31827 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31828 and `scroll-right' overrides this variable's effect. */);
31829 Vhscroll_step = make_number (0);
31831 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31832 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31833 Bind this around calls to `message' to let it take effect. */);
31834 message_truncate_lines = false;
31836 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31837 doc: /* Normal hook run to update the menu bar definitions.
31838 Redisplay runs this hook before it redisplays the menu bar.
31839 This is used to update menus such as Buffers, whose contents depend on
31840 various data. */);
31841 Vmenu_bar_update_hook = Qnil;
31843 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31844 doc: /* Frame for which we are updating a menu.
31845 The enable predicate for a menu binding should check this variable. */);
31846 Vmenu_updating_frame = Qnil;
31848 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31849 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31850 inhibit_menubar_update = false;
31852 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31853 doc: /* Prefix prepended to all continuation lines at display time.
31854 The value may be a string, an image, or a stretch-glyph; it is
31855 interpreted in the same way as the value of a `display' text property.
31857 This variable is overridden by any `wrap-prefix' text or overlay
31858 property.
31860 To add a prefix to non-continuation lines, use `line-prefix'. */);
31861 Vwrap_prefix = Qnil;
31862 DEFSYM (Qwrap_prefix, "wrap-prefix");
31863 Fmake_variable_buffer_local (Qwrap_prefix);
31865 DEFVAR_LISP ("line-prefix", Vline_prefix,
31866 doc: /* Prefix prepended to all non-continuation lines at display time.
31867 The value may be a string, an image, or a stretch-glyph; it is
31868 interpreted in the same way as the value of a `display' text property.
31870 This variable is overridden by any `line-prefix' text or overlay
31871 property.
31873 To add a prefix to continuation lines, use `wrap-prefix'. */);
31874 Vline_prefix = Qnil;
31875 DEFSYM (Qline_prefix, "line-prefix");
31876 Fmake_variable_buffer_local (Qline_prefix);
31878 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31879 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31880 inhibit_eval_during_redisplay = false;
31882 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31883 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31884 inhibit_free_realized_faces = false;
31886 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31887 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31888 Intended for use during debugging and for testing bidi display;
31889 see biditest.el in the test suite. */);
31890 inhibit_bidi_mirroring = false;
31892 #ifdef GLYPH_DEBUG
31893 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31894 doc: /* Inhibit try_window_id display optimization. */);
31895 inhibit_try_window_id = false;
31897 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31898 doc: /* Inhibit try_window_reusing display optimization. */);
31899 inhibit_try_window_reusing = false;
31901 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31902 doc: /* Inhibit try_cursor_movement display optimization. */);
31903 inhibit_try_cursor_movement = false;
31904 #endif /* GLYPH_DEBUG */
31906 DEFVAR_INT ("overline-margin", overline_margin,
31907 doc: /* Space between overline and text, in pixels.
31908 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31909 margin to the character height. */);
31910 overline_margin = 2;
31912 DEFVAR_INT ("underline-minimum-offset",
31913 underline_minimum_offset,
31914 doc: /* Minimum distance between baseline and underline.
31915 This can improve legibility of underlined text at small font sizes,
31916 particularly when using variable `x-use-underline-position-properties'
31917 with fonts that specify an UNDERLINE_POSITION relatively close to the
31918 baseline. The default value is 1. */);
31919 underline_minimum_offset = 1;
31921 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31922 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31923 This feature only works when on a window system that can change
31924 cursor shapes. */);
31925 display_hourglass_p = true;
31927 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31928 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31929 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31931 #ifdef HAVE_WINDOW_SYSTEM
31932 hourglass_atimer = NULL;
31933 hourglass_shown_p = false;
31934 #endif /* HAVE_WINDOW_SYSTEM */
31936 /* Name of the face used to display glyphless characters. */
31937 DEFSYM (Qglyphless_char, "glyphless-char");
31939 /* Method symbols for Vglyphless_char_display. */
31940 DEFSYM (Qhex_code, "hex-code");
31941 DEFSYM (Qempty_box, "empty-box");
31942 DEFSYM (Qthin_space, "thin-space");
31943 DEFSYM (Qzero_width, "zero-width");
31945 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31946 doc: /* Function run just before redisplay.
31947 It is called with one argument, which is the set of windows that are to
31948 be redisplayed. This set can be nil (meaning, only the selected window),
31949 or t (meaning all windows). */);
31950 Vpre_redisplay_function = intern ("ignore");
31952 /* Symbol for the purpose of Vglyphless_char_display. */
31953 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31954 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31956 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31957 doc: /* Char-table defining glyphless characters.
31958 Each element, if non-nil, should be one of the following:
31959 an ASCII acronym string: display this string in a box
31960 `hex-code': display the hexadecimal code of a character in a box
31961 `empty-box': display as an empty box
31962 `thin-space': display as 1-pixel width space
31963 `zero-width': don't display
31964 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31965 display method for graphical terminals and text terminals respectively.
31966 GRAPHICAL and TEXT should each have one of the values listed above.
31968 The char-table has one extra slot to control the display of a character for
31969 which no font is found. This slot only takes effect on graphical terminals.
31970 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31971 `thin-space'. The default is `empty-box'.
31973 If a character has a non-nil entry in an active display table, the
31974 display table takes effect; in this case, Emacs does not consult
31975 `glyphless-char-display' at all. */);
31976 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31977 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31978 Qempty_box);
31980 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31981 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31982 Vdebug_on_message = Qnil;
31984 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31985 doc: /* */);
31986 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31988 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31989 doc: /* */);
31990 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31992 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31993 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31994 Vredisplay__variables = Qnil;
31996 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31997 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31998 /* Initialize to t, since we need to disable reordering until
31999 loadup.el successfully loads charprop.el. */
32000 redisplay__inhibit_bidi = true;
32004 /* Initialize this module when Emacs starts. */
32006 void
32007 init_xdisp (void)
32009 CHARPOS (this_line_start_pos) = 0;
32011 if (!noninteractive)
32013 struct window *m = XWINDOW (minibuf_window);
32014 Lisp_Object frame = m->frame;
32015 struct frame *f = XFRAME (frame);
32016 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32017 struct window *r = XWINDOW (root);
32018 int i;
32020 echo_area_window = minibuf_window;
32022 r->top_line = FRAME_TOP_MARGIN (f);
32023 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32024 r->total_cols = FRAME_COLS (f);
32025 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32026 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32027 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32029 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32030 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32031 m->total_cols = FRAME_COLS (f);
32032 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32033 m->total_lines = 1;
32034 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32036 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32037 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32038 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32040 /* The default ellipsis glyphs `...'. */
32041 for (i = 0; i < 3; ++i)
32042 default_invis_vector[i] = make_number ('.');
32046 /* Allocate the buffer for frame titles.
32047 Also used for `format-mode-line'. */
32048 int size = 100;
32049 mode_line_noprop_buf = xmalloc (size);
32050 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32051 mode_line_noprop_ptr = mode_line_noprop_buf;
32052 mode_line_target = MODE_LINE_DISPLAY;
32055 help_echo_showing_p = false;
32058 #ifdef HAVE_WINDOW_SYSTEM
32060 /* Platform-independent portion of hourglass implementation. */
32062 /* Timer function of hourglass_atimer. */
32064 static void
32065 show_hourglass (struct atimer *timer)
32067 /* The timer implementation will cancel this timer automatically
32068 after this function has run. Set hourglass_atimer to null
32069 so that we know the timer doesn't have to be canceled. */
32070 hourglass_atimer = NULL;
32072 if (!hourglass_shown_p)
32074 Lisp_Object tail, frame;
32076 block_input ();
32078 FOR_EACH_FRAME (tail, frame)
32080 struct frame *f = XFRAME (frame);
32082 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32083 && FRAME_RIF (f)->show_hourglass)
32084 FRAME_RIF (f)->show_hourglass (f);
32087 hourglass_shown_p = true;
32088 unblock_input ();
32092 /* Cancel a currently active hourglass timer, and start a new one. */
32094 void
32095 start_hourglass (void)
32097 struct timespec delay;
32099 cancel_hourglass ();
32101 if (INTEGERP (Vhourglass_delay)
32102 && XINT (Vhourglass_delay) > 0)
32103 delay = make_timespec (min (XINT (Vhourglass_delay),
32104 TYPE_MAXIMUM (time_t)),
32106 else if (FLOATP (Vhourglass_delay)
32107 && XFLOAT_DATA (Vhourglass_delay) > 0)
32108 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32109 else
32110 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32112 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32113 show_hourglass, NULL);
32116 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32117 shown. */
32119 void
32120 cancel_hourglass (void)
32122 if (hourglass_atimer)
32124 cancel_atimer (hourglass_atimer);
32125 hourglass_atimer = NULL;
32128 if (hourglass_shown_p)
32130 Lisp_Object tail, frame;
32132 block_input ();
32134 FOR_EACH_FRAME (tail, frame)
32136 struct frame *f = XFRAME (frame);
32138 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32139 && FRAME_RIF (f)->hide_hourglass)
32140 FRAME_RIF (f)->hide_hourglass (f);
32141 #ifdef HAVE_NTGUI
32142 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32143 else if (!FRAME_W32_P (f))
32144 w32_arrow_cursor ();
32145 #endif
32148 hourglass_shown_p = false;
32149 unblock_input ();
32153 #endif /* HAVE_WINDOW_SYSTEM */