Merge from origin/emacs-25
[emacs.git] / src / xdisp.c
blob1a27c4c3c9635870891e5e70b70b2bee22774cbb
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 void
626 maybe_set_redisplay (Lisp_Object symbol)
628 if (HASH_TABLE_P (Vredisplay__variables)
629 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void redisplay_windows (Lisp_Object);
822 static void redisplay_window (Lisp_Object, bool);
823 static Lisp_Object redisplay_window_error (Lisp_Object);
824 static Lisp_Object redisplay_window_0 (Lisp_Object);
825 static Lisp_Object redisplay_window_1 (Lisp_Object);
826 static bool set_cursor_from_row (struct window *, struct glyph_row *,
827 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
828 int, int);
829 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
830 static bool update_menu_bar (struct frame *, bool, bool);
831 static bool try_window_reusing_current_matrix (struct window *);
832 static int try_window_id (struct window *);
833 static bool display_line (struct it *);
834 static int display_mode_lines (struct window *);
835 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
836 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
837 Lisp_Object, bool);
838 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
839 Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
843 ptrdiff_t *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static bool get_overlay_strings (struct it *, ptrdiff_t);
849 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, bool);
852 static void reseat_1 (struct it *, struct text_pos, bool);
853 static bool next_element_from_display_vector (struct it *);
854 static bool next_element_from_string (struct it *);
855 static bool next_element_from_c_string (struct it *);
856 static bool next_element_from_buffer (struct it *);
857 static bool next_element_from_composition (struct it *);
858 static bool next_element_from_image (struct it *);
859 static bool next_element_from_stretch (struct it *);
860 static bool next_element_from_xwidget (struct it *);
861 static void load_overlay_strings (struct it *, ptrdiff_t);
862 static bool get_next_display_element (struct it *);
863 static enum move_it_result
864 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
865 enum move_operation_enum);
866 static void get_visually_first_element (struct it *);
867 static void compute_stop_pos (struct it *);
868 static int face_before_or_after_it_pos (struct it *, bool);
869 static ptrdiff_t next_overlay_change (ptrdiff_t);
870 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
871 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
872 static int handle_single_display_spec (struct it *, Lisp_Object,
873 Lisp_Object, Lisp_Object,
874 struct text_pos *, ptrdiff_t, int, bool);
875 static int underlying_face_id (struct it *);
877 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
878 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
880 #ifdef HAVE_WINDOW_SYSTEM
882 static void update_tool_bar (struct frame *, bool);
883 static void x_draw_bottom_divider (struct window *w);
884 static void notice_overwritten_cursor (struct window *,
885 enum glyph_row_area,
886 int, int, int, int);
887 static int normal_char_height (struct font *, int);
888 static void normal_char_ascent_descent (struct font *, int, int *, int *);
890 static void append_stretch_glyph (struct it *, Lisp_Object,
891 int, int, int);
893 static Lisp_Object get_it_property (struct it *, Lisp_Object);
894 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
895 struct font *, int, bool);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return true if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1298 bool
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 bool visible_p = false;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. Also, someone might narrow-to-region and immediately
1322 call a scroll function. */
1323 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1324 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1326 /* If the top of the window is after CHARPOS, the latter is surely
1327 not visible. */
1328 if (charpos >= 0 && CHARPOS (top) > charpos)
1329 return visible_p;
1331 /* Compute exact mode line heights. */
1332 if (WINDOW_WANTS_MODELINE_P (w))
1333 w->mode_line_height
1334 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1335 BVAR (current_buffer, mode_line_format));
1337 if (WINDOW_WANTS_HEADER_LINE_P (w))
1338 w->header_line_height
1339 = display_mode_line (w, HEADER_LINE_FACE_ID,
1340 BVAR (current_buffer, header_line_format));
1342 start_display (&it, w, top);
1343 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1344 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1346 if (charpos >= 0
1347 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1348 && IT_CHARPOS (it) >= charpos)
1349 /* When scanning backwards under bidi iteration, move_it_to
1350 stops at or _before_ CHARPOS, because it stops at or to
1351 the _right_ of the character at CHARPOS. */
1352 || (it.bidi_p && it.bidi_it.scan_dir == -1
1353 && IT_CHARPOS (it) <= charpos)))
1355 /* We have reached CHARPOS, or passed it. How the call to
1356 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1357 or covered by a display property, move_it_to stops at the end
1358 of the invisible text, to the right of CHARPOS. (ii) If
1359 CHARPOS is in a display vector, move_it_to stops on its last
1360 glyph. */
1361 int top_x = it.current_x;
1362 int top_y = it.current_y;
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364 int bottom_y;
1365 struct it save_it;
1366 void *save_it_data = NULL;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 SAVE_IT (save_it, it, save_it_data);
1370 last_height = 0;
1371 bottom_y = line_bottom_y (&it);
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = true;
1376 if (bottom_y >= it.last_visible_y
1377 && it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) < charpos)
1380 /* When the last line of the window is scanned backwards
1381 under bidi iteration, we could be duped into thinking
1382 that we have passed CHARPOS, when in fact move_it_to
1383 simply stopped short of CHARPOS because it reached
1384 last_visible_y. To see if that's what happened, we call
1385 move_it_to again with a slightly larger vertical limit,
1386 and see if it actually moved vertically; if it did, we
1387 didn't really reach CHARPOS, which is beyond window end. */
1388 /* Why 10? because we don't know how many canonical lines
1389 will the height of the next line(s) be. So we guess. */
1390 int ten_more_lines = 10 * default_line_pixel_height (w);
1392 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1393 MOVE_TO_POS | MOVE_TO_Y);
1394 if (it.current_y > top_y)
1395 visible_p = false;
1398 RESTORE_IT (&it, &save_it, save_it_data);
1399 if (visible_p)
1401 if (it.method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2, it2_prev;
1410 /* The idea is to get to the previous buffer
1411 position, consume the character there, and use
1412 the pixel coordinates we get after that. But if
1413 the previous buffer position is also displayed
1414 from a display vector, we need to consume all of
1415 the glyphs from that display vector. */
1416 start_display (&it2, w, top);
1417 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1418 /* If we didn't get to CHARPOS - 1, there's some
1419 replacing display property at that position, and
1420 we stopped after it. That is exactly the place
1421 whose coordinates we want. */
1422 if (IT_CHARPOS (it2) != charpos - 1)
1423 it2_prev = it2;
1424 else
1426 /* Iterate until we get out of the display
1427 vector that displays the character at
1428 CHARPOS - 1. */
1429 do {
1430 get_next_display_element (&it2);
1431 PRODUCE_GLYPHS (&it2);
1432 it2_prev = it2;
1433 set_iterator_to_next (&it2, true);
1434 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1435 && IT_CHARPOS (it2) < charpos);
1437 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1438 || it2_prev.current_x > it2_prev.last_visible_x)
1439 top_x = it.glyph_row->x;
1440 else
1442 top_x = it2_prev.current_x;
1443 top_y = it2_prev.current_y;
1447 else if (IT_CHARPOS (it) != charpos)
1449 Lisp_Object cpos = make_number (charpos);
1450 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1451 Lisp_Object string = string_from_display_spec (spec);
1452 struct text_pos tpos;
1453 bool newline_in_string
1454 = (STRINGP (string)
1455 && memchr (SDATA (string), '\n', SBYTES (string)));
1457 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1458 bool replacing_spec_p
1459 = (!NILP (spec)
1460 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1461 charpos, FRAME_WINDOW_P (it.f)));
1462 /* The tricky code below is needed because there's a
1463 discrepancy between move_it_to and how we set cursor
1464 when PT is at the beginning of a portion of text
1465 covered by a display property or an overlay with a
1466 display property, or the display line ends in a
1467 newline from a display string. move_it_to will stop
1468 _after_ such display strings, whereas
1469 set_cursor_from_row conspires with cursor_row_p to
1470 place the cursor on the first glyph produced from the
1471 display string. */
1473 /* We have overshoot PT because it is covered by a
1474 display property that replaces the text it covers.
1475 If the string includes embedded newlines, we are also
1476 in the wrong display line. Backtrack to the correct
1477 line, where the display property begins. */
1478 if (replacing_spec_p)
1480 Lisp_Object startpos, endpos;
1481 EMACS_INT start, end;
1482 struct it it3;
1484 /* Find the first and the last buffer positions
1485 covered by the display string. */
1486 endpos =
1487 Fnext_single_char_property_change (cpos, Qdisplay,
1488 Qnil, Qnil);
1489 startpos =
1490 Fprevious_single_char_property_change (endpos, Qdisplay,
1491 Qnil, Qnil);
1492 start = XFASTINT (startpos);
1493 end = XFASTINT (endpos);
1494 /* Move to the last buffer position before the
1495 display property. */
1496 start_display (&it3, w, top);
1497 if (start > CHARPOS (top))
1498 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1499 /* Move forward one more line if the position before
1500 the display string is a newline or if it is the
1501 rightmost character on a line that is
1502 continued or word-wrapped. */
1503 if (it3.method == GET_FROM_BUFFER
1504 && (it3.c == '\n'
1505 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1506 move_it_by_lines (&it3, 1);
1507 else if (move_it_in_display_line_to (&it3, -1,
1508 it3.current_x
1509 + it3.pixel_width,
1510 MOVE_TO_X)
1511 == MOVE_LINE_CONTINUED)
1513 move_it_by_lines (&it3, 1);
1514 /* When we are under word-wrap, the #$@%!
1515 move_it_by_lines moves 2 lines, so we need to
1516 fix that up. */
1517 if (it3.line_wrap == WORD_WRAP)
1518 move_it_by_lines (&it3, -1);
1521 /* Record the vertical coordinate of the display
1522 line where we wound up. */
1523 top_y = it3.current_y;
1524 if (it3.bidi_p)
1526 /* When characters are reordered for display,
1527 the character displayed to the left of the
1528 display string could be _after_ the display
1529 property in the logical order. Use the
1530 smallest vertical position of these two. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1533 if (it3.current_y < top_y)
1534 top_y = it3.current_y;
1536 /* Move from the top of the window to the beginning
1537 of the display line where the display string
1538 begins. */
1539 start_display (&it3, w, top);
1540 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1541 /* If it3_moved stays false after the 'while' loop
1542 below, that means we already were at a newline
1543 before the loop (e.g., the display string begins
1544 with a newline), so we don't need to (and cannot)
1545 inspect the glyphs of it3.glyph_row, because
1546 PRODUCE_GLYPHS will not produce anything for a
1547 newline, and thus it3.glyph_row stays at its
1548 stale content it got at top of the window. */
1549 bool it3_moved = false;
1550 /* Finally, advance the iterator until we hit the
1551 first display element whose character position is
1552 CHARPOS, or until the first newline from the
1553 display string, which signals the end of the
1554 display line. */
1555 while (get_next_display_element (&it3))
1557 PRODUCE_GLYPHS (&it3);
1558 if (IT_CHARPOS (it3) == charpos
1559 || ITERATOR_AT_END_OF_LINE_P (&it3))
1560 break;
1561 it3_moved = true;
1562 set_iterator_to_next (&it3, false);
1564 top_x = it3.current_x - it3.pixel_width;
1565 /* Normally, we would exit the above loop because we
1566 found the display element whose character
1567 position is CHARPOS. For the contingency that we
1568 didn't, and stopped at the first newline from the
1569 display string, move back over the glyphs
1570 produced from the string, until we find the
1571 rightmost glyph not from the string. */
1572 if (it3_moved
1573 && newline_in_string
1574 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1576 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA];
1579 while (EQ ((g - 1)->object, string))
1581 --g;
1582 top_x -= g->pixel_width;
1584 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1585 + it3.glyph_row->used[TEXT_AREA]);
1590 *x = top_x;
1591 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1592 *rtop = max (0, window_top_y - top_y);
1593 *rbot = max (0, bottom_y - it.last_visible_y);
1594 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1595 - max (top_y, window_top_y)));
1596 *vpos = it.vpos;
1597 if (it.bidi_it.paragraph_dir == R2L)
1598 r2l = true;
1601 else
1603 /* Either we were asked to provide info about WINDOW_END, or
1604 CHARPOS is in the partially visible glyph row at end of
1605 window. */
1606 struct it it2;
1607 void *it2data = NULL;
1609 SAVE_IT (it2, it, it2data);
1610 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1611 move_it_by_lines (&it, 1);
1612 if (charpos < IT_CHARPOS (it)
1613 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1615 visible_p = true;
1616 RESTORE_IT (&it2, &it2, it2data);
1617 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1618 *x = it2.current_x;
1619 *y = it2.current_y + it2.max_ascent - it2.ascent;
1620 *rtop = max (0, -it2.current_y);
1621 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1622 - it.last_visible_y));
1623 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1624 it.last_visible_y)
1625 - max (it2.current_y,
1626 WINDOW_HEADER_LINE_HEIGHT (w))));
1627 *vpos = it2.vpos;
1628 if (it2.bidi_it.paragraph_dir == R2L)
1629 r2l = true;
1631 else
1632 bidi_unshelve_cache (it2data, true);
1634 bidi_unshelve_cache (itdata, false);
1636 if (old_buffer)
1637 set_buffer_internal_1 (old_buffer);
1639 if (visible_p)
1641 if (w->hscroll > 0)
1642 *x -=
1643 window_hscroll_limited (w, WINDOW_XFRAME (w))
1644 * WINDOW_FRAME_COLUMN_WIDTH (w);
1645 /* For lines in an R2L paragraph, we need to mirror the X pixel
1646 coordinate wrt the text area. For the reasons, see the
1647 commentary in buffer_posn_from_coords and the explanation of
1648 the geometry used by the move_it_* functions at the end of
1649 the large commentary near the beginning of this file. */
1650 if (r2l)
1651 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1654 #if false
1655 /* Debugging code. */
1656 if (visible_p)
1657 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1658 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1659 else
1660 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1661 #endif
1663 return visible_p;
1667 /* Return the next character from STR. Return in *LEN the length of
1668 the character. This is like STRING_CHAR_AND_LENGTH but never
1669 returns an invalid character. If we find one, we return a `?', but
1670 with the length of the invalid character. */
1672 static int
1673 string_char_and_length (const unsigned char *str, int *len)
1675 int c;
1677 c = STRING_CHAR_AND_LENGTH (str, *len);
1678 if (!CHAR_VALID_P (c))
1679 /* We may not change the length here because other places in Emacs
1680 don't use this function, i.e. they silently accept invalid
1681 characters. */
1682 c = '?';
1684 return c;
1689 /* Given a position POS containing a valid character and byte position
1690 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1692 static struct text_pos
1693 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1695 eassert (STRINGP (string) && nchars >= 0);
1697 if (STRING_MULTIBYTE (string))
1699 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1700 int len;
1702 while (nchars--)
1704 string_char_and_length (p, &len);
1705 p += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1710 else
1711 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1713 return pos;
1717 /* Value is the text position, i.e. character and byte position,
1718 for character position CHARPOS in STRING. */
1720 static struct text_pos
1721 string_pos (ptrdiff_t charpos, Lisp_Object string)
1723 struct text_pos pos;
1724 eassert (STRINGP (string));
1725 eassert (charpos >= 0);
1726 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1727 return pos;
1731 /* Value is a text position, i.e. character and byte position, for
1732 character position CHARPOS in C string S. MULTIBYTE_P
1733 means recognize multibyte characters. */
1735 static struct text_pos
1736 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1738 struct text_pos pos;
1740 eassert (s != NULL);
1741 eassert (charpos >= 0);
1743 if (multibyte_p)
1745 int len;
1747 SET_TEXT_POS (pos, 0, 0);
1748 while (charpos--)
1750 string_char_and_length ((const unsigned char *) s, &len);
1751 s += len;
1752 CHARPOS (pos) += 1;
1753 BYTEPOS (pos) += len;
1756 else
1757 SET_TEXT_POS (pos, charpos, charpos);
1759 return pos;
1763 /* Value is the number of characters in C string S. MULTIBYTE_P
1764 means recognize multibyte characters. */
1766 static ptrdiff_t
1767 number_of_chars (const char *s, bool multibyte_p)
1769 ptrdiff_t nchars;
1771 if (multibyte_p)
1773 ptrdiff_t rest = strlen (s);
1774 int len;
1775 const unsigned char *p = (const unsigned char *) s;
1777 for (nchars = 0; rest > 0; ++nchars)
1779 string_char_and_length (p, &len);
1780 rest -= len, p += len;
1783 else
1784 nchars = strlen (s);
1786 return nchars;
1790 /* Compute byte position NEWPOS->bytepos corresponding to
1791 NEWPOS->charpos. POS is a known position in string STRING.
1792 NEWPOS->charpos must be >= POS.charpos. */
1794 static void
1795 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1797 eassert (STRINGP (string));
1798 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1800 if (STRING_MULTIBYTE (string))
1801 *newpos = string_pos_nchars_ahead (pos, string,
1802 CHARPOS (*newpos) - CHARPOS (pos));
1803 else
1804 BYTEPOS (*newpos) = CHARPOS (*newpos);
1807 /* EXPORT:
1808 Return an estimation of the pixel height of mode or header lines on
1809 frame F. FACE_ID specifies what line's height to estimate. */
1812 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 if (FRAME_WINDOW_P (f))
1817 int height = FONT_HEIGHT (FRAME_FONT (f));
1819 /* This function is called so early when Emacs starts that the face
1820 cache and mode line face are not yet initialized. */
1821 if (FRAME_FACE_CACHE (f))
1823 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1824 if (face)
1826 if (face->font)
1827 height = normal_char_height (face->font, -1);
1828 if (face->box_line_width > 0)
1829 height += 2 * face->box_line_width;
1833 return height;
1835 #endif
1837 return 1;
1840 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1841 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1842 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1843 not force the value into range. */
1845 void
1846 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1847 NativeRectangle *bounds, bool noclip)
1850 #ifdef HAVE_WINDOW_SYSTEM
1851 if (FRAME_WINDOW_P (f))
1853 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1854 even for negative values. */
1855 if (pix_x < 0)
1856 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1857 if (pix_y < 0)
1858 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1860 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1861 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1863 if (bounds)
1864 STORE_NATIVE_RECT (*bounds,
1865 FRAME_COL_TO_PIXEL_X (f, pix_x),
1866 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1867 FRAME_COLUMN_WIDTH (f) - 1,
1868 FRAME_LINE_HEIGHT (f) - 1);
1870 /* PXW: Should we clip pixels before converting to columns/lines? */
1871 if (!noclip)
1873 if (pix_x < 0)
1874 pix_x = 0;
1875 else if (pix_x > FRAME_TOTAL_COLS (f))
1876 pix_x = FRAME_TOTAL_COLS (f);
1878 if (pix_y < 0)
1879 pix_y = 0;
1880 else if (pix_y > FRAME_TOTAL_LINES (f))
1881 pix_y = FRAME_TOTAL_LINES (f);
1884 #endif
1886 *x = pix_x;
1887 *y = pix_y;
1891 /* Find the glyph under window-relative coordinates X/Y in window W.
1892 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1893 strings. Return in *HPOS and *VPOS the row and column number of
1894 the glyph found. Return in *AREA the glyph area containing X.
1895 Value is a pointer to the glyph found or null if X/Y is not on
1896 text, or we can't tell because W's current matrix is not up to
1897 date. */
1899 static struct glyph *
1900 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1901 int *dx, int *dy, int *area)
1903 struct glyph *glyph, *end;
1904 struct glyph_row *row = NULL;
1905 int x0, i;
1907 /* Find row containing Y. Give up if some row is not enabled. */
1908 for (i = 0; i < w->current_matrix->nrows; ++i)
1910 row = MATRIX_ROW (w->current_matrix, i);
1911 if (!row->enabled_p)
1912 return NULL;
1913 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1914 break;
1917 *vpos = i;
1918 *hpos = 0;
1920 /* Give up if Y is not in the window. */
1921 if (i == w->current_matrix->nrows)
1922 return NULL;
1924 /* Get the glyph area containing X. */
1925 if (w->pseudo_window_p)
1927 *area = TEXT_AREA;
1928 x0 = 0;
1930 else
1932 if (x < window_box_left_offset (w, TEXT_AREA))
1934 *area = LEFT_MARGIN_AREA;
1935 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1937 else if (x < window_box_right_offset (w, TEXT_AREA))
1939 *area = TEXT_AREA;
1940 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1942 else
1944 *area = RIGHT_MARGIN_AREA;
1945 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1949 /* Find glyph containing X. */
1950 glyph = row->glyphs[*area];
1951 end = glyph + row->used[*area];
1952 x -= x0;
1953 while (glyph < end && x >= glyph->pixel_width)
1955 x -= glyph->pixel_width;
1956 ++glyph;
1959 if (glyph == end)
1960 return NULL;
1962 if (dx)
1964 *dx = x;
1965 *dy = y - (row->y + row->ascent - glyph->ascent);
1968 *hpos = glyph - row->glyphs[*area];
1969 return glyph;
1972 /* Convert frame-relative x/y to coordinates relative to window W.
1973 Takes pseudo-windows into account. */
1975 static void
1976 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1978 if (w->pseudo_window_p)
1980 /* A pseudo-window is always full-width, and starts at the
1981 left edge of the frame, plus a frame border. */
1982 struct frame *f = XFRAME (w->frame);
1983 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 else
1988 *x -= WINDOW_LEFT_EDGE_X (w);
1989 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1993 #ifdef HAVE_WINDOW_SYSTEM
1995 /* EXPORT:
1996 Return in RECTS[] at most N clipping rectangles for glyph string S.
1997 Return the number of stored rectangles. */
2000 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2002 XRectangle r;
2004 if (n <= 0)
2005 return 0;
2007 if (s->row->full_width_p)
2009 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2010 r.x = WINDOW_LEFT_EDGE_X (s->w);
2011 if (s->row->mode_line_p)
2012 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2013 else
2014 r.width = WINDOW_PIXEL_WIDTH (s->w);
2016 /* Unless displaying a mode or menu bar line, which are always
2017 fully visible, clip to the visible part of the row. */
2018 if (s->w->pseudo_window_p)
2019 r.height = s->row->visible_height;
2020 else
2021 r.height = s->height;
2023 else
2025 /* This is a text line that may be partially visible. */
2026 r.x = window_box_left (s->w, s->area);
2027 r.width = window_box_width (s->w, s->area);
2028 r.height = s->row->visible_height;
2031 if (s->clip_head)
2032 if (r.x < s->clip_head->x)
2034 if (r.width >= s->clip_head->x - r.x)
2035 r.width -= s->clip_head->x - r.x;
2036 else
2037 r.width = 0;
2038 r.x = s->clip_head->x;
2040 if (s->clip_tail)
2041 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2043 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2044 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2045 else
2046 r.width = 0;
2049 /* If S draws overlapping rows, it's sufficient to use the top and
2050 bottom of the window for clipping because this glyph string
2051 intentionally draws over other lines. */
2052 if (s->for_overlaps)
2054 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2055 r.height = window_text_bottom_y (s->w) - r.y;
2057 /* Alas, the above simple strategy does not work for the
2058 environments with anti-aliased text: if the same text is
2059 drawn onto the same place multiple times, it gets thicker.
2060 If the overlap we are processing is for the erased cursor, we
2061 take the intersection with the rectangle of the cursor. */
2062 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2064 XRectangle rc, r_save = r;
2066 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2067 rc.y = s->w->phys_cursor.y;
2068 rc.width = s->w->phys_cursor_width;
2069 rc.height = s->w->phys_cursor_height;
2071 x_intersect_rectangles (&r_save, &rc, &r);
2074 else
2076 /* Don't use S->y for clipping because it doesn't take partially
2077 visible lines into account. For example, it can be negative for
2078 partially visible lines at the top of a window. */
2079 if (!s->row->full_width_p
2080 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2081 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2082 else
2083 r.y = max (0, s->row->y);
2086 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2088 /* If drawing the cursor, don't let glyph draw outside its
2089 advertised boundaries. Cleartype does this under some circumstances. */
2090 if (s->hl == DRAW_CURSOR)
2092 struct glyph *glyph = s->first_glyph;
2093 int height, max_y;
2095 if (s->x > r.x)
2097 if (r.width >= s->x - r.x)
2098 r.width -= s->x - r.x;
2099 else /* R2L hscrolled row with cursor outside text area */
2100 r.width = 0;
2101 r.x = s->x;
2103 r.width = min (r.width, glyph->pixel_width);
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2112 r.y = max_y;
2113 r.height = height;
2115 else
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2128 if (s->row->clip)
2130 XRectangle r_save = r;
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2146 else
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2159 if (s->for_overlaps & OVERLAPS_PRED)
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2176 if (r.y + r.height > row_y + s->row->visible_height)
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2181 else
2182 rs[i].height = 0;
2184 i++;
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2202 get_glyph_string_clip_rects (s, nr, 1);
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0, ascent;
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width;
2224 x = w->phys_cursor.x;
2225 if (x < 0)
2227 wd += x;
2228 x = 0;
2231 if (glyph->type == STRETCH_GLYPH
2232 && !x_stretch_cursor_p)
2233 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2234 w->phys_cursor_width = wd;
2236 /* Don't let the hollow cursor glyph descend below the glyph row's
2237 ascent value, lest the hollow cursor looks funny. */
2238 y = w->phys_cursor.y;
2239 ascent = row->ascent;
2240 if (row->ascent < glyph->ascent)
2242 y -= glyph->ascent - row->ascent;
2243 ascent = glyph->ascent;
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249 h = max (h0, ascent + glyph->descent);
2250 h0 = min (h0, ascent + glyph->descent);
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2258 else
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2263 h += y - y0;
2264 y = y0;
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2274 * Remember which glyph the mouse is over.
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2290 if (window_resize_pixelwise)
2292 width = height = 1;
2293 goto virtual_glyph;
2295 else if (!f->glyphs_initialized_p
2296 || (window = window_from_coordinates (f, gx, gy, &part, false),
2297 NILP (window)))
2299 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2300 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2301 goto virtual_glyph;
2304 w = XWINDOW (window);
2305 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2306 height = WINDOW_FRAME_LINE_HEIGHT (w);
2308 x = window_relative_x_coord (w, part, gx);
2309 y = gy - WINDOW_TOP_EDGE_Y (w);
2311 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2312 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2314 if (w->pseudo_window_p)
2316 area = TEXT_AREA;
2317 part = ON_MODE_LINE; /* Don't adjust margin. */
2318 goto text_glyph;
2321 switch (part)
2323 case ON_LEFT_MARGIN:
2324 area = LEFT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_RIGHT_MARGIN:
2328 area = RIGHT_MARGIN_AREA;
2329 goto text_glyph;
2331 case ON_HEADER_LINE:
2332 case ON_MODE_LINE:
2333 gr = (part == ON_HEADER_LINE
2334 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2335 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2336 gy = gr->y;
2337 area = TEXT_AREA;
2338 goto text_glyph_row_found;
2340 case ON_TEXT:
2341 area = TEXT_AREA;
2343 text_glyph:
2344 gr = 0; gy = 0;
2345 for (; r <= end_row && r->enabled_p; ++r)
2346 if (r->y + r->height > y)
2348 gr = r; gy = r->y;
2349 break;
2352 text_glyph_row_found:
2353 if (gr && gy <= y)
2355 struct glyph *g = gr->glyphs[area];
2356 struct glyph *end = g + gr->used[area];
2358 height = gr->height;
2359 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2360 if (gx + g->pixel_width > x)
2361 break;
2363 if (g < end)
2365 if (g->type == IMAGE_GLYPH)
2367 /* Don't remember when mouse is over image, as
2368 image may have hot-spots. */
2369 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2370 return;
2372 width = g->pixel_width;
2374 else
2376 /* Use nominal char spacing at end of line. */
2377 x -= gx;
2378 gx += (x / width) * width;
2381 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2383 gx += window_box_left_offset (w, area);
2384 /* Don't expand over the modeline to make sure the vertical
2385 drag cursor is shown early enough. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2390 else
2392 /* Use nominal line height at end of window. */
2393 gx = (x / width) * width;
2394 y -= gy;
2395 gy += (y / height) * height;
2396 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2397 /* See comment above. */
2398 height = min (height,
2399 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2401 break;
2403 case ON_LEFT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2406 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2407 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2408 goto row_glyph;
2410 case ON_RIGHT_FRINGE:
2411 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2412 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2413 : window_box_right_offset (w, TEXT_AREA));
2414 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2415 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2416 && !WINDOW_RIGHTMOST_P (w))
2417 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2418 /* Make sure the vertical border can get her own glyph to the
2419 right of the one we build here. */
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2421 else
2422 width = WINDOW_PIXEL_WIDTH (w) - gx;
2423 else
2424 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2426 goto row_glyph;
2428 case ON_VERTICAL_BORDER:
2429 gx = WINDOW_PIXEL_WIDTH (w) - width;
2430 goto row_glyph;
2432 case ON_VERTICAL_SCROLL_BAR:
2433 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2436 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2438 : 0)));
2439 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441 row_glyph:
2442 gr = 0, gy = 0;
2443 for (; r <= end_row && r->enabled_p; ++r)
2444 if (r->y + r->height > y)
2446 gr = r; gy = r->y;
2447 break;
2450 if (gr && gy <= y)
2451 height = gr->height;
2452 else
2454 /* Use nominal line height at end of window. */
2455 y -= gy;
2456 gy += (y / height) * height;
2458 break;
2460 case ON_RIGHT_DIVIDER:
2461 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2462 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2463 gy = 0;
2464 /* The bottom divider prevails. */
2465 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 case ON_BOTTOM_DIVIDER:
2469 gx = 0;
2470 width = WINDOW_PIXEL_WIDTH (w);
2471 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2472 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2473 goto add_edge;
2475 default:
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2492 goto store_rect;
2495 add_edge:
2496 gx += WINDOW_LEFT_EDGE_X (w);
2497 gy += WINDOW_TOP_EDGE_Y (w);
2499 store_rect:
2500 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2502 /* Visible feedback for debugging. */
2503 #if false && defined HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2511 #endif /* HAVE_WINDOW_SYSTEM */
2513 static void
2514 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 eassert (w);
2517 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2518 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2519 w->window_end_vpos
2520 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2523 /***********************************************************************
2524 Lisp form evaluation
2525 ***********************************************************************/
2527 /* Error handler for safe_eval and safe_call. */
2529 static Lisp_Object
2530 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 add_to_log ("Error during redisplay: %S signaled %S",
2533 Flist (nargs, args), arg);
2534 return Qnil;
2537 /* Call function FUNC with the rest of NARGS - 1 arguments
2538 following. Return the result, or nil if something went
2539 wrong. Prevent redisplay during the evaluation. */
2541 static Lisp_Object
2542 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2544 Lisp_Object val;
2546 if (inhibit_eval_during_redisplay)
2547 val = Qnil;
2548 else
2550 ptrdiff_t i;
2551 ptrdiff_t count = SPECPDL_INDEX ();
2552 Lisp_Object *args;
2553 USE_SAFE_ALLOCA;
2554 SAFE_ALLOCA_LISP (args, nargs);
2556 args[0] = func;
2557 for (i = 1; i < nargs; i++)
2558 args[i] = va_arg (ap, Lisp_Object);
2560 specbind (Qinhibit_redisplay, Qt);
2561 if (inhibit_quit)
2562 specbind (Qinhibit_quit, Qt);
2563 /* Use Qt to ensure debugger does not run,
2564 so there is no possibility of wanting to redisplay. */
2565 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2566 safe_eval_handler);
2567 SAFE_FREE ();
2568 val = unbind_to (count, val);
2571 return val;
2574 Lisp_Object
2575 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2577 Lisp_Object retval;
2578 va_list ap;
2580 va_start (ap, func);
2581 retval = safe__call (false, nargs, func, ap);
2582 va_end (ap);
2583 return retval;
2586 /* Call function FN with one argument ARG.
2587 Return the result, or nil if something went wrong. */
2589 Lisp_Object
2590 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2592 return safe_call (2, fn, arg);
2595 static Lisp_Object
2596 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2598 Lisp_Object retval;
2599 va_list ap;
2601 va_start (ap, fn);
2602 retval = safe__call (inhibit_quit, 2, fn, ap);
2603 va_end (ap);
2604 return retval;
2607 Lisp_Object
2608 safe_eval (Lisp_Object sexpr)
2610 return safe__call1 (false, Qeval, sexpr);
2613 static Lisp_Object
2614 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2616 return safe__call1 (inhibit_quit, Qeval, sexpr);
2619 /* Call function FN with two arguments ARG1 and ARG2.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2625 return safe_call (3, fn, arg1, arg2);
2630 /***********************************************************************
2631 Debugging
2632 ***********************************************************************/
2634 /* Define CHECK_IT to perform sanity checks on iterators.
2635 This is for debugging. It is too slow to do unconditionally. */
2637 static void
2638 CHECK_IT (struct it *it)
2640 #if false
2641 if (it->method == GET_FROM_STRING)
2643 eassert (STRINGP (it->string));
2644 eassert (IT_STRING_CHARPOS (*it) >= 0);
2646 else
2648 eassert (IT_STRING_CHARPOS (*it) < 0);
2649 if (it->method == GET_FROM_BUFFER)
2651 /* Check that character and byte positions agree. */
2652 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2656 if (it->dpvec)
2657 eassert (it->current.dpvec_index >= 0);
2658 else
2659 eassert (it->current.dpvec_index < 0);
2660 #endif
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 CHECK_WINDOW_END (struct window *w)
2670 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2671 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2673 struct glyph_row *row;
2674 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2675 !row->enabled_p
2676 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2677 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2679 #endif
2682 /***********************************************************************
2683 Iterator initialization
2684 ***********************************************************************/
2686 /* Initialize IT for displaying current_buffer in window W, starting
2687 at character position CHARPOS. CHARPOS < 0 means that no buffer
2688 position is specified which is useful when the iterator is assigned
2689 a position later. BYTEPOS is the byte position corresponding to
2690 CHARPOS.
2692 If ROW is not null, calls to produce_glyphs with IT as parameter
2693 will produce glyphs in that row.
2695 BASE_FACE_ID is the id of a base face to use. It must be one of
2696 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2697 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2698 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2700 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2701 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2702 will be initialized to use the corresponding mode line glyph row of
2703 the desired matrix of W. */
2705 void
2706 init_iterator (struct it *it, struct window *w,
2707 ptrdiff_t charpos, ptrdiff_t bytepos,
2708 struct glyph_row *row, enum face_id base_face_id)
2710 enum face_id remapped_base_face_id = base_face_id;
2712 /* Some precondition checks. */
2713 eassert (w != NULL && it != NULL);
2714 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2715 && charpos <= ZV));
2717 /* If face attributes have been changed since the last redisplay,
2718 free realized faces now because they depend on face definitions
2719 that might have changed. Don't free faces while there might be
2720 desired matrices pending which reference these faces. */
2721 if (!inhibit_free_realized_faces)
2723 if (face_change)
2725 face_change = false;
2726 free_all_realized_faces (Qnil);
2728 else if (XFRAME (w->frame)->face_change)
2730 XFRAME (w->frame)->face_change = 0;
2731 free_all_realized_faces (w->frame);
2735 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2736 if (! NILP (Vface_remapping_alist))
2737 remapped_base_face_id
2738 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2740 /* Use one of the mode line rows of W's desired matrix if
2741 appropriate. */
2742 if (row == NULL)
2744 if (base_face_id == MODE_LINE_FACE_ID
2745 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2746 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2747 else if (base_face_id == HEADER_LINE_FACE_ID)
2748 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2751 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2752 Other parts of redisplay rely on that. */
2753 memclear (it, sizeof *it);
2754 it->current.overlay_string_index = -1;
2755 it->current.dpvec_index = -1;
2756 it->base_face_id = remapped_base_face_id;
2757 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2758 it->paragraph_embedding = L2R;
2759 it->bidi_it.w = w;
2761 /* The window in which we iterate over current_buffer: */
2762 XSETWINDOW (it->window, w);
2763 it->w = w;
2764 it->f = XFRAME (w->frame);
2766 it->cmp_it.id = -1;
2768 /* Extra space between lines (on window systems only). */
2769 if (base_face_id == DEFAULT_FACE_ID
2770 && FRAME_WINDOW_P (it->f))
2772 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2774 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2776 * FRAME_LINE_HEIGHT (it->f));
2777 else if (it->f->extra_line_spacing > 0)
2778 it->extra_line_spacing = it->f->extra_line_spacing;
2781 /* If realized faces have been removed, e.g. because of face
2782 attribute changes of named faces, recompute them. When running
2783 in batch mode, the face cache of the initial frame is null. If
2784 we happen to get called, make a dummy face cache. */
2785 if (FRAME_FACE_CACHE (it->f) == NULL)
2786 init_frame_faces (it->f);
2787 if (FRAME_FACE_CACHE (it->f)->used == 0)
2788 recompute_basic_faces (it->f);
2790 it->override_ascent = -1;
2792 /* Are control characters displayed as `^C'? */
2793 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2795 /* -1 means everything between a CR and the following line end
2796 is invisible. >0 means lines indented more than this value are
2797 invisible. */
2798 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2799 ? (clip_to_bounds
2800 (-1, XINT (BVAR (current_buffer, selective_display)),
2801 PTRDIFF_MAX))
2802 : (!NILP (BVAR (current_buffer, selective_display))
2803 ? -1 : 0));
2804 it->selective_display_ellipsis_p
2805 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2807 /* Display table to use. */
2808 it->dp = window_display_table (w);
2810 /* Are multibyte characters enabled in current_buffer? */
2811 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2813 /* Get the position at which the redisplay_end_trigger hook should
2814 be run, if it is to be run at all. */
2815 if (MARKERP (w->redisplay_end_trigger)
2816 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2817 it->redisplay_end_trigger_charpos
2818 = marker_position (w->redisplay_end_trigger);
2819 else if (INTEGERP (w->redisplay_end_trigger))
2820 it->redisplay_end_trigger_charpos
2821 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2822 PTRDIFF_MAX);
2824 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2826 /* Are lines in the display truncated? */
2827 if (TRUNCATE != 0)
2828 it->line_wrap = TRUNCATE;
2829 if (base_face_id == DEFAULT_FACE_ID
2830 && !it->w->hscroll
2831 && (WINDOW_FULL_WIDTH_P (it->w)
2832 || NILP (Vtruncate_partial_width_windows)
2833 || (INTEGERP (Vtruncate_partial_width_windows)
2834 /* PXW: Shall we do something about this? */
2835 && (XINT (Vtruncate_partial_width_windows)
2836 <= WINDOW_TOTAL_COLS (it->w))))
2837 && NILP (BVAR (current_buffer, truncate_lines)))
2838 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2839 ? WINDOW_WRAP : WORD_WRAP;
2841 /* Get dimensions of truncation and continuation glyphs. These are
2842 displayed as fringe bitmaps under X, but we need them for such
2843 frames when the fringes are turned off. But leave the dimensions
2844 zero for tooltip frames, as these glyphs look ugly there and also
2845 sabotage calculations of tooltip dimensions in x-show-tip. */
2846 #ifdef HAVE_WINDOW_SYSTEM
2847 if (!(FRAME_WINDOW_P (it->f)
2848 && FRAMEP (tip_frame)
2849 && it->f == XFRAME (tip_frame)))
2850 #endif
2852 if (it->line_wrap == TRUNCATE)
2854 /* We will need the truncation glyph. */
2855 eassert (it->glyph_row == NULL);
2856 produce_special_glyphs (it, IT_TRUNCATION);
2857 it->truncation_pixel_width = it->pixel_width;
2859 else
2861 /* We will need the continuation glyph. */
2862 eassert (it->glyph_row == NULL);
2863 produce_special_glyphs (it, IT_CONTINUATION);
2864 it->continuation_pixel_width = it->pixel_width;
2868 /* Reset these values to zero because the produce_special_glyphs
2869 above has changed them. */
2870 it->pixel_width = it->ascent = it->descent = 0;
2871 it->phys_ascent = it->phys_descent = 0;
2873 /* Set this after getting the dimensions of truncation and
2874 continuation glyphs, so that we don't produce glyphs when calling
2875 produce_special_glyphs, above. */
2876 it->glyph_row = row;
2877 it->area = TEXT_AREA;
2879 /* Get the dimensions of the display area. The display area
2880 consists of the visible window area plus a horizontally scrolled
2881 part to the left of the window. All x-values are relative to the
2882 start of this total display area. */
2883 if (base_face_id != DEFAULT_FACE_ID)
2885 /* Mode lines, menu bar in terminal frames. */
2886 it->first_visible_x = 0;
2887 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2889 else
2891 it->first_visible_x
2892 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2893 it->last_visible_x = (it->first_visible_x
2894 + window_box_width (w, TEXT_AREA));
2896 /* If we truncate lines, leave room for the truncation glyph(s) at
2897 the right margin. Otherwise, leave room for the continuation
2898 glyph(s). Done only if the window has no right fringe. */
2899 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2901 if (it->line_wrap == TRUNCATE)
2902 it->last_visible_x -= it->truncation_pixel_width;
2903 else
2904 it->last_visible_x -= it->continuation_pixel_width;
2907 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2908 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 /* Leave room for a border glyph. */
2912 if (!FRAME_WINDOW_P (it->f)
2913 && !WINDOW_RIGHTMOST_P (it->w))
2914 it->last_visible_x -= 1;
2916 it->last_visible_y = window_text_bottom_y (w);
2918 /* For mode lines and alike, arrange for the first glyph having a
2919 left box line if the face specifies a box. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 struct face *face;
2924 it->face_id = remapped_base_face_id;
2926 /* If we have a boxed mode line, make the first character appear
2927 with a left box line. */
2928 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2929 if (face && face->box != FACE_NO_BOX)
2930 it->start_of_box_run_p = true;
2933 /* If a buffer position was specified, set the iterator there,
2934 getting overlays and face properties from that position. */
2935 if (charpos >= BUF_BEG (current_buffer))
2937 it->stop_charpos = charpos;
2938 it->end_charpos = ZV;
2939 eassert (charpos == BYTE_TO_CHAR (bytepos));
2940 IT_CHARPOS (*it) = charpos;
2941 IT_BYTEPOS (*it) = bytepos;
2943 /* We will rely on `reseat' to set this up properly, via
2944 handle_face_prop. */
2945 it->face_id = it->base_face_id;
2947 it->start = it->current;
2948 /* Do we need to reorder bidirectional text? Not if this is a
2949 unibyte buffer: by definition, none of the single-byte
2950 characters are strong R2L, so no reordering is needed. And
2951 bidi.c doesn't support unibyte buffers anyway. Also, don't
2952 reorder while we are loading loadup.el, since the tables of
2953 character properties needed for reordering are not yet
2954 available. */
2955 it->bidi_p =
2956 !redisplay__inhibit_bidi
2957 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2958 && it->multibyte_p;
2960 /* If we are to reorder bidirectional text, init the bidi
2961 iterator. */
2962 if (it->bidi_p)
2964 /* Since we don't know at this point whether there will be
2965 any R2L lines in the window, we reserve space for
2966 truncation/continuation glyphs even if only the left
2967 fringe is absent. */
2968 if (base_face_id == DEFAULT_FACE_ID
2969 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2970 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2972 if (it->line_wrap == TRUNCATE)
2973 it->last_visible_x -= it->truncation_pixel_width;
2974 else
2975 it->last_visible_x -= it->continuation_pixel_width;
2977 /* Note the paragraph direction that this buffer wants to
2978 use. */
2979 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2980 Qleft_to_right))
2981 it->paragraph_embedding = L2R;
2982 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2983 Qright_to_left))
2984 it->paragraph_embedding = R2L;
2985 else
2986 it->paragraph_embedding = NEUTRAL_DIR;
2987 bidi_unshelve_cache (NULL, false);
2988 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2989 &it->bidi_it);
2992 /* Compute faces etc. */
2993 reseat (it, it->current.pos, true);
2996 CHECK_IT (it);
3000 /* Initialize IT for the display of window W with window start POS. */
3002 void
3003 start_display (struct it *it, struct window *w, struct text_pos pos)
3005 struct glyph_row *row;
3006 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3008 row = w->desired_matrix->rows + first_vpos;
3009 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3010 it->first_vpos = first_vpos;
3012 /* Don't reseat to previous visible line start if current start
3013 position is in a string or image. */
3014 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3016 int first_y = it->current_y;
3018 /* If window start is not at a line start, skip forward to POS to
3019 get the correct continuation lines width. */
3020 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3021 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3022 if (!start_at_line_beg_p)
3024 int new_x;
3026 reseat_at_previous_visible_line_start (it);
3027 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3029 new_x = it->current_x + it->pixel_width;
3031 /* If lines are continued, this line may end in the middle
3032 of a multi-glyph character (e.g. a control character
3033 displayed as \003, or in the middle of an overlay
3034 string). In this case move_it_to above will not have
3035 taken us to the start of the continuation line but to the
3036 end of the continued line. */
3037 if (it->current_x > 0
3038 && it->line_wrap != TRUNCATE /* Lines are continued. */
3039 && (/* And glyph doesn't fit on the line. */
3040 new_x > it->last_visible_x
3041 /* Or it fits exactly and we're on a window
3042 system frame. */
3043 || (new_x == it->last_visible_x
3044 && FRAME_WINDOW_P (it->f)
3045 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3046 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3047 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3049 if ((it->current.dpvec_index >= 0
3050 || it->current.overlay_string_index >= 0)
3051 /* If we are on a newline from a display vector or
3052 overlay string, then we are already at the end of
3053 a screen line; no need to go to the next line in
3054 that case, as this line is not really continued.
3055 (If we do go to the next line, C-e will not DTRT.) */
3056 && it->c != '\n')
3058 set_iterator_to_next (it, true);
3059 move_it_in_display_line_to (it, -1, -1, 0);
3062 it->continuation_lines_width += it->current_x;
3064 /* If the character at POS is displayed via a display
3065 vector, move_it_to above stops at the final glyph of
3066 IT->dpvec. To make the caller redisplay that character
3067 again (a.k.a. start at POS), we need to reset the
3068 dpvec_index to the beginning of IT->dpvec. */
3069 else if (it->current.dpvec_index >= 0)
3070 it->current.dpvec_index = 0;
3072 /* We're starting a new display line, not affected by the
3073 height of the continued line, so clear the appropriate
3074 fields in the iterator structure. */
3075 it->max_ascent = it->max_descent = 0;
3076 it->max_phys_ascent = it->max_phys_descent = 0;
3078 it->current_y = first_y;
3079 it->vpos = 0;
3080 it->current_x = it->hpos = 0;
3086 /* Return true if POS is a position in ellipses displayed for invisible
3087 text. W is the window we display, for text property lookup. */
3089 static bool
3090 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3092 Lisp_Object prop, window;
3093 bool ellipses_p = false;
3094 ptrdiff_t charpos = CHARPOS (pos->pos);
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (pos->dpvec_index >= 0
3101 && pos->overlay_string_index < 0
3102 && CHARPOS (pos->string_pos) < 0
3103 && charpos > BEGV
3104 && (XSETWINDOW (window, w),
3105 prop = Fget_char_property (make_number (charpos),
3106 Qinvisible, window),
3107 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3109 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3110 window);
3111 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3114 return ellipses_p;
3118 /* Initialize IT for stepping through current_buffer in window W,
3119 starting at position POS that includes overlay string and display
3120 vector/ control character translation position information. Value
3121 is false if there are overlay strings with newlines at POS. */
3123 static bool
3124 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3126 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3127 int i;
3128 bool overlay_strings_with_newlines = false;
3130 /* If POS specifies a position in a display vector, this might
3131 be for an ellipsis displayed for invisible text. We won't
3132 get the iterator set up for delivering that ellipsis unless
3133 we make sure that it gets aware of the invisible text. */
3134 if (in_ellipses_for_invisible_text_p (pos, w))
3136 --charpos;
3137 bytepos = 0;
3140 /* Keep in mind: the call to reseat in init_iterator skips invisible
3141 text, so we might end up at a position different from POS. This
3142 is only a problem when POS is a row start after a newline and an
3143 overlay starts there with an after-string, and the overlay has an
3144 invisible property. Since we don't skip invisible text in
3145 display_line and elsewhere immediately after consuming the
3146 newline before the row start, such a POS will not be in a string,
3147 but the call to init_iterator below will move us to the
3148 after-string. */
3149 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3151 /* This only scans the current chunk -- it should scan all chunks.
3152 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3153 to 16 in 22.1 to make this a lesser problem. */
3154 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3156 const char *s = SSDATA (it->overlay_strings[i]);
3157 const char *e = s + SBYTES (it->overlay_strings[i]);
3159 while (s < e && *s != '\n')
3160 ++s;
3162 if (s < e)
3164 overlay_strings_with_newlines = true;
3165 break;
3169 /* If position is within an overlay string, set up IT to the right
3170 overlay string. */
3171 if (pos->overlay_string_index >= 0)
3173 int relative_index;
3175 /* If the first overlay string happens to have a `display'
3176 property for an image, the iterator will be set up for that
3177 image, and we have to undo that setup first before we can
3178 correct the overlay string index. */
3179 if (it->method == GET_FROM_IMAGE)
3180 pop_it (it);
3182 /* We already have the first chunk of overlay strings in
3183 IT->overlay_strings. Load more until the one for
3184 pos->overlay_string_index is in IT->overlay_strings. */
3185 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3187 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3188 it->current.overlay_string_index = 0;
3189 while (n--)
3191 load_overlay_strings (it, 0);
3192 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = pos->overlay_string_index;
3197 relative_index = (it->current.overlay_string_index
3198 % OVERLAY_STRING_CHUNK_SIZE);
3199 it->string = it->overlay_strings[relative_index];
3200 eassert (STRINGP (it->string));
3201 it->current.string_pos = pos->string_pos;
3202 it->method = GET_FROM_STRING;
3203 it->end_charpos = SCHARS (it->string);
3204 /* Set up the bidi iterator for this overlay string. */
3205 if (it->bidi_p)
3207 it->bidi_it.string.lstring = it->string;
3208 it->bidi_it.string.s = NULL;
3209 it->bidi_it.string.schars = SCHARS (it->string);
3210 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3211 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3212 it->bidi_it.string.unibyte = !it->multibyte_p;
3213 it->bidi_it.w = it->w;
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3217 /* Synchronize the state of the bidi iterator with
3218 pos->string_pos. For any string position other than
3219 zero, this will be done automagically when we resume
3220 iteration over the string and get_visually_first_element
3221 is called. But if string_pos is zero, and the string is
3222 to be reordered for display, we need to resync manually,
3223 since it could be that the iteration state recorded in
3224 pos ended at string_pos of 0 moving backwards in string. */
3225 if (CHARPOS (pos->string_pos) == 0)
3227 get_visually_first_element (it);
3228 if (IT_STRING_CHARPOS (*it) != 0)
3229 do {
3230 /* Paranoia. */
3231 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3232 bidi_move_to_visually_next (&it->bidi_it);
3233 } while (it->bidi_it.charpos != 0);
3235 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3236 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3240 if (CHARPOS (pos->string_pos) >= 0)
3242 /* Recorded position is not in an overlay string, but in another
3243 string. This can only be a string from a `display' property.
3244 IT should already be filled with that string. */
3245 it->current.string_pos = pos->string_pos;
3246 eassert (STRINGP (it->string));
3247 if (it->bidi_p)
3248 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3249 FRAME_WINDOW_P (it->f), &it->bidi_it);
3252 /* Restore position in display vector translations, control
3253 character translations or ellipses. */
3254 if (pos->dpvec_index >= 0)
3256 if (it->dpvec == NULL)
3257 get_next_display_element (it);
3258 eassert (it->dpvec && it->current.dpvec_index == 0);
3259 it->current.dpvec_index = pos->dpvec_index;
3262 CHECK_IT (it);
3263 return !overlay_strings_with_newlines;
3267 /* Initialize IT for stepping through current_buffer in window W
3268 starting at ROW->start. */
3270 static void
3271 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3273 init_from_display_pos (it, w, &row->start);
3274 it->start = row->start;
3275 it->continuation_lines_width = row->continuation_lines_width;
3276 CHECK_IT (it);
3280 /* Initialize IT for stepping through current_buffer in window W
3281 starting in the line following ROW, i.e. starting at ROW->end.
3282 Value is false if there are overlay strings with newlines at ROW's
3283 end position. */
3285 static bool
3286 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3288 bool success = false;
3290 if (init_from_display_pos (it, w, &row->end))
3292 if (row->continued_p)
3293 it->continuation_lines_width
3294 = row->continuation_lines_width + row->pixel_width;
3295 CHECK_IT (it);
3296 success = true;
3299 return success;
3305 /***********************************************************************
3306 Text properties
3307 ***********************************************************************/
3309 /* Called when IT reaches IT->stop_charpos. Handle text property and
3310 overlay changes. Set IT->stop_charpos to the next position where
3311 to stop. */
3313 static void
3314 handle_stop (struct it *it)
3316 enum prop_handled handled;
3317 bool handle_overlay_change_p;
3318 struct props *p;
3320 it->dpvec = NULL;
3321 it->current.dpvec_index = -1;
3322 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3323 it->ellipsis_p = false;
3325 /* Use face of preceding text for ellipsis (if invisible) */
3326 if (it->selective_display_ellipsis_p)
3327 it->saved_face_id = it->face_id;
3329 /* Here's the description of the semantics of, and the logic behind,
3330 the various HANDLED_* statuses:
3332 HANDLED_NORMALLY means the handler did its job, and the loop
3333 should proceed to calling the next handler in order.
3335 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3336 change in the properties and overlays at current position, so the
3337 loop should be restarted, to re-invoke the handlers that were
3338 already called. This happens when fontification-functions were
3339 called by handle_fontified_prop, and actually fontified
3340 something. Another case where HANDLED_RECOMPUTE_PROPS is
3341 returned is when we discover overlay strings that need to be
3342 displayed right away. The loop below will continue for as long
3343 as the status is HANDLED_RECOMPUTE_PROPS.
3345 HANDLED_RETURN means return immediately to the caller, to
3346 continue iteration without calling any further handlers. This is
3347 used when we need to act on some property right away, for example
3348 when we need to display the ellipsis or a replacing display
3349 property, such as display string or image.
3351 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3352 consumed, and the handler switched to the next overlay string.
3353 This signals the loop below to refrain from looking for more
3354 overlays before all the overlay strings of the current overlay
3355 are processed.
3357 Some of the handlers called by the loop push the iterator state
3358 onto the stack (see 'push_it'), and arrange for the iteration to
3359 continue with another object, such as an image, a display string,
3360 or an overlay string. In most such cases, it->stop_charpos is
3361 set to the first character of the string, so that when the
3362 iteration resumes, this function will immediately be called
3363 again, to examine the properties at the beginning of the string.
3365 When a display or overlay string is exhausted, the iterator state
3366 is popped (see 'pop_it'), and iteration continues with the
3367 previous object. Again, in many such cases this function is
3368 called again to find the next position where properties might
3369 change. */
3373 handled = HANDLED_NORMALLY;
3375 /* Call text property handlers. */
3376 for (p = it_props; p->handler; ++p)
3378 handled = p->handler (it);
3380 if (handled == HANDLED_RECOMPUTE_PROPS)
3381 break;
3382 else if (handled == HANDLED_RETURN)
3384 /* We still want to show before and after strings from
3385 overlays even if the actual buffer text is replaced. */
3386 if (!handle_overlay_change_p
3387 || it->sp > 1
3388 /* Don't call get_overlay_strings_1 if we already
3389 have overlay strings loaded, because doing so
3390 will load them again and push the iterator state
3391 onto the stack one more time, which is not
3392 expected by the rest of the code that processes
3393 overlay strings. */
3394 || (it->current.overlay_string_index < 0
3395 && !get_overlay_strings_1 (it, 0, false)))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->string_from_display_prop_p = false;
3413 it->from_disp_prop_p = false;
3414 handle_overlay_change_p = false;
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 break;
3419 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3420 handle_overlay_change_p = false;
3423 if (handled != HANDLED_RECOMPUTE_PROPS)
3425 /* Don't check for overlay strings below when set to deliver
3426 characters from a display vector. */
3427 if (it->method == GET_FROM_DISPLAY_VECTOR)
3428 handle_overlay_change_p = false;
3430 /* Handle overlay changes.
3431 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3432 if it finds overlays. */
3433 if (handle_overlay_change_p)
3434 handled = handle_overlay_change (it);
3437 if (it->ellipsis_p)
3439 setup_for_ellipsis (it, 0);
3440 break;
3443 while (handled == HANDLED_RECOMPUTE_PROPS);
3445 /* Determine where to stop next. */
3446 if (handled == HANDLED_NORMALLY)
3447 compute_stop_pos (it);
3451 /* Compute IT->stop_charpos from text property and overlay change
3452 information for IT's current position. */
3454 static void
3455 compute_stop_pos (struct it *it)
3457 register INTERVAL iv, next_iv;
3458 Lisp_Object object, limit, position;
3459 ptrdiff_t charpos, bytepos;
3461 if (STRINGP (it->string))
3463 /* Strings are usually short, so don't limit the search for
3464 properties. */
3465 it->stop_charpos = it->end_charpos;
3466 object = it->string;
3467 limit = Qnil;
3468 charpos = IT_STRING_CHARPOS (*it);
3469 bytepos = IT_STRING_BYTEPOS (*it);
3471 else
3473 ptrdiff_t pos;
3475 /* If end_charpos is out of range for some reason, such as a
3476 misbehaving display function, rationalize it (Bug#5984). */
3477 if (it->end_charpos > ZV)
3478 it->end_charpos = ZV;
3479 it->stop_charpos = it->end_charpos;
3481 /* If next overlay change is in front of the current stop pos
3482 (which is IT->end_charpos), stop there. Note: value of
3483 next_overlay_change is point-max if no overlay change
3484 follows. */
3485 charpos = IT_CHARPOS (*it);
3486 bytepos = IT_BYTEPOS (*it);
3487 pos = next_overlay_change (charpos);
3488 if (pos < it->stop_charpos)
3489 it->stop_charpos = pos;
3491 /* Set up variables for computing the stop position from text
3492 property changes. */
3493 XSETBUFFER (object, current_buffer);
3494 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3497 /* Get the interval containing IT's position. Value is a null
3498 interval if there isn't such an interval. */
3499 position = make_number (charpos);
3500 iv = validate_interval_range (object, &position, &position, false);
3501 if (iv)
3503 Lisp_Object values_here[LAST_PROP_IDX];
3504 struct props *p;
3506 /* Get properties here. */
3507 for (p = it_props; p->handler; ++p)
3508 values_here[p->idx] = textget (iv->plist,
3509 builtin_lisp_symbol (p->name));
3511 /* Look for an interval following iv that has different
3512 properties. */
3513 for (next_iv = next_interval (iv);
3514 (next_iv
3515 && (NILP (limit)
3516 || XFASTINT (limit) > next_iv->position));
3517 next_iv = next_interval (next_iv))
3519 for (p = it_props; p->handler; ++p)
3521 Lisp_Object new_value = textget (next_iv->plist,
3522 builtin_lisp_symbol (p->name));
3523 if (!EQ (values_here[p->idx], new_value))
3524 break;
3527 if (p->handler)
3528 break;
3531 if (next_iv)
3533 if (INTEGERP (limit)
3534 && next_iv->position >= XFASTINT (limit))
3535 /* No text property change up to limit. */
3536 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3537 else
3538 /* Text properties change in next_iv. */
3539 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3543 if (it->cmp_it.id < 0)
3545 ptrdiff_t stoppos = it->end_charpos;
3547 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3548 stoppos = -1;
3549 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3550 stoppos, it->string);
3553 eassert (STRINGP (it->string)
3554 || (it->stop_charpos >= BEGV
3555 && it->stop_charpos >= IT_CHARPOS (*it)));
3559 /* Return the position of the next overlay change after POS in
3560 current_buffer. Value is point-max if no overlay change
3561 follows. This is like `next-overlay-change' but doesn't use
3562 xmalloc. */
3564 static ptrdiff_t
3565 next_overlay_change (ptrdiff_t pos)
3567 ptrdiff_t i, noverlays;
3568 ptrdiff_t endpos;
3569 Lisp_Object *overlays;
3570 USE_SAFE_ALLOCA;
3572 /* Get all overlays at the given position. */
3573 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3575 /* If any of these overlays ends before endpos,
3576 use its ending point instead. */
3577 for (i = 0; i < noverlays; ++i)
3579 Lisp_Object oend;
3580 ptrdiff_t oendpos;
3582 oend = OVERLAY_END (overlays[i]);
3583 oendpos = OVERLAY_POSITION (oend);
3584 endpos = min (endpos, oendpos);
3587 SAFE_FREE ();
3588 return endpos;
3591 /* How many characters forward to search for a display property or
3592 display string. Searching too far forward makes the bidi display
3593 sluggish, especially in small windows. */
3594 #define MAX_DISP_SCAN 250
3596 /* Return the character position of a display string at or after
3597 position specified by POSITION. If no display string exists at or
3598 after POSITION, return ZV. A display string is either an overlay
3599 with `display' property whose value is a string, or a `display'
3600 text property whose value is a string. STRING is data about the
3601 string to iterate; if STRING->lstring is nil, we are iterating a
3602 buffer. FRAME_WINDOW_P is true when we are displaying a window
3603 on a GUI frame. DISP_PROP is set to zero if we searched
3604 MAX_DISP_SCAN characters forward without finding any display
3605 strings, non-zero otherwise. It is set to 2 if the display string
3606 uses any kind of `(space ...)' spec that will produce a stretch of
3607 white space in the text area. */
3608 ptrdiff_t
3609 compute_display_string_pos (struct text_pos *position,
3610 struct bidi_string_data *string,
3611 struct window *w,
3612 bool frame_window_p, int *disp_prop)
3614 /* OBJECT = nil means current buffer. */
3615 Lisp_Object object, object1;
3616 Lisp_Object pos, spec, limpos;
3617 bool string_p = string && (STRINGP (string->lstring) || string->s);
3618 ptrdiff_t eob = string_p ? string->schars : ZV;
3619 ptrdiff_t begb = string_p ? 0 : BEGV;
3620 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3621 ptrdiff_t lim =
3622 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3623 struct text_pos tpos;
3624 int rv = 0;
3626 if (string && STRINGP (string->lstring))
3627 object1 = object = string->lstring;
3628 else if (w && !string_p)
3630 XSETWINDOW (object, w);
3631 object1 = Qnil;
3633 else
3634 object1 = object = Qnil;
3636 *disp_prop = 1;
3638 if (charpos >= eob
3639 /* We don't support display properties whose values are strings
3640 that have display string properties. */
3641 || string->from_disp_str
3642 /* C strings cannot have display properties. */
3643 || (string->s && !STRINGP (object)))
3645 *disp_prop = 0;
3646 return eob;
3649 /* If the character at CHARPOS is where the display string begins,
3650 return CHARPOS. */
3651 pos = make_number (charpos);
3652 if (STRINGP (object))
3653 bufpos = string->bufpos;
3654 else
3655 bufpos = charpos;
3656 tpos = *position;
3657 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3658 && (charpos <= begb
3659 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3660 object),
3661 spec))
3662 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3663 frame_window_p)))
3665 if (rv == 2)
3666 *disp_prop = 2;
3667 return charpos;
3670 /* Look forward for the first character with a `display' property
3671 that will replace the underlying text when displayed. */
3672 limpos = make_number (lim);
3673 do {
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3675 CHARPOS (tpos) = XFASTINT (pos);
3676 if (CHARPOS (tpos) >= lim)
3678 *disp_prop = 0;
3679 break;
3681 if (STRINGP (object))
3682 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3683 else
3684 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3685 spec = Fget_char_property (pos, Qdisplay, object);
3686 if (!STRINGP (object))
3687 bufpos = CHARPOS (tpos);
3688 } while (NILP (spec)
3689 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3690 bufpos, frame_window_p)));
3691 if (rv == 2)
3692 *disp_prop = 2;
3694 return CHARPOS (tpos);
3697 /* Return the character position of the end of the display string that
3698 started at CHARPOS. If there's no display string at CHARPOS,
3699 return -1. A display string is either an overlay with `display'
3700 property whose value is a string or a `display' text property whose
3701 value is a string. */
3702 ptrdiff_t
3703 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3705 /* OBJECT = nil means current buffer. */
3706 Lisp_Object object =
3707 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3708 Lisp_Object pos = make_number (charpos);
3709 ptrdiff_t eob =
3710 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3712 if (charpos >= eob || (string->s && !STRINGP (object)))
3713 return eob;
3715 /* It could happen that the display property or overlay was removed
3716 since we found it in compute_display_string_pos above. One way
3717 this can happen is if JIT font-lock was called (through
3718 handle_fontified_prop), and jit-lock-functions remove text
3719 properties or overlays from the portion of buffer that includes
3720 CHARPOS. Muse mode is known to do that, for example. In this
3721 case, we return -1 to the caller, to signal that no display
3722 string is actually present at CHARPOS. See bidi_fetch_char for
3723 how this is handled.
3725 An alternative would be to never look for display properties past
3726 it->stop_charpos. But neither compute_display_string_pos nor
3727 bidi_fetch_char that calls it know or care where the next
3728 stop_charpos is. */
3729 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3730 return -1;
3732 /* Look forward for the first character where the `display' property
3733 changes. */
3734 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3736 return XFASTINT (pos);
3741 /***********************************************************************
3742 Fontification
3743 ***********************************************************************/
3745 /* Handle changes in the `fontified' property of the current buffer by
3746 calling hook functions from Qfontification_functions to fontify
3747 regions of text. */
3749 static enum prop_handled
3750 handle_fontified_prop (struct it *it)
3752 Lisp_Object prop, pos;
3753 enum prop_handled handled = HANDLED_NORMALLY;
3755 if (!NILP (Vmemory_full))
3756 return handled;
3758 /* Get the value of the `fontified' property at IT's current buffer
3759 position. (The `fontified' property doesn't have a special
3760 meaning in strings.) If the value is nil, call functions from
3761 Qfontification_functions. */
3762 if (!STRINGP (it->string)
3763 && it->s == NULL
3764 && !NILP (Vfontification_functions)
3765 && !NILP (Vrun_hooks)
3766 && (pos = make_number (IT_CHARPOS (*it)),
3767 prop = Fget_char_property (pos, Qfontified, Qnil),
3768 /* Ignore the special cased nil value always present at EOB since
3769 no amount of fontifying will be able to change it. */
3770 NILP (prop) && IT_CHARPOS (*it) < Z))
3772 ptrdiff_t count = SPECPDL_INDEX ();
3773 Lisp_Object val;
3774 struct buffer *obuf = current_buffer;
3775 ptrdiff_t begv = BEGV, zv = ZV;
3776 bool old_clip_changed = current_buffer->clip_changed;
3778 val = Vfontification_functions;
3779 specbind (Qfontification_functions, Qnil);
3781 eassert (it->end_charpos == ZV);
3783 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3784 safe_call1 (val, pos);
3785 else
3787 Lisp_Object fns, fn;
3789 fns = Qnil;
3791 for (; CONSP (val); val = XCDR (val))
3793 fn = XCAR (val);
3795 if (EQ (fn, Qt))
3797 /* A value of t indicates this hook has a local
3798 binding; it means to run the global binding too.
3799 In a global value, t should not occur. If it
3800 does, we must ignore it to avoid an endless
3801 loop. */
3802 for (fns = Fdefault_value (Qfontification_functions);
3803 CONSP (fns);
3804 fns = XCDR (fns))
3806 fn = XCAR (fns);
3807 if (!EQ (fn, Qt))
3808 safe_call1 (fn, pos);
3811 else
3812 safe_call1 (fn, pos);
3816 unbind_to (count, Qnil);
3818 /* Fontification functions routinely call `save-restriction'.
3819 Normally, this tags clip_changed, which can confuse redisplay
3820 (see discussion in Bug#6671). Since we don't perform any
3821 special handling of fontification changes in the case where
3822 `save-restriction' isn't called, there's no point doing so in
3823 this case either. So, if the buffer's restrictions are
3824 actually left unchanged, reset clip_changed. */
3825 if (obuf == current_buffer)
3827 if (begv == BEGV && zv == ZV)
3828 current_buffer->clip_changed = old_clip_changed;
3830 /* There isn't much we can reasonably do to protect against
3831 misbehaving fontification, but here's a fig leaf. */
3832 else if (BUFFER_LIVE_P (obuf))
3833 set_buffer_internal_1 (obuf);
3835 /* The fontification code may have added/removed text.
3836 It could do even a lot worse, but let's at least protect against
3837 the most obvious case where only the text past `pos' gets changed',
3838 as is/was done in grep.el where some escapes sequences are turned
3839 into face properties (bug#7876). */
3840 it->end_charpos = ZV;
3842 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3843 something. This avoids an endless loop if they failed to
3844 fontify the text for which reason ever. */
3845 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3846 handled = HANDLED_RECOMPUTE_PROPS;
3849 return handled;
3854 /***********************************************************************
3855 Faces
3856 ***********************************************************************/
3858 /* Set up iterator IT from face properties at its current position.
3859 Called from handle_stop. */
3861 static enum prop_handled
3862 handle_face_prop (struct it *it)
3864 int new_face_id;
3865 ptrdiff_t next_stop;
3867 if (!STRINGP (it->string))
3869 new_face_id
3870 = face_at_buffer_position (it->w,
3871 IT_CHARPOS (*it),
3872 &next_stop,
3873 (IT_CHARPOS (*it)
3874 + TEXT_PROP_DISTANCE_LIMIT),
3875 false, it->base_face_id);
3877 /* Is this a start of a run of characters with box face?
3878 Caveat: this can be called for a freshly initialized
3879 iterator; face_id is -1 in this case. We know that the new
3880 face will not change until limit, i.e. if the new face has a
3881 box, all characters up to limit will have one. But, as
3882 usual, we don't know whether limit is really the end. */
3883 if (new_face_id != it->face_id)
3885 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3886 /* If it->face_id is -1, old_face below will be NULL, see
3887 the definition of FACE_FROM_ID_OR_NULL. This will happen
3888 if this is the initial call that gets the face. */
3889 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3891 /* If the value of face_id of the iterator is -1, we have to
3892 look in front of IT's position and see whether there is a
3893 face there that's different from new_face_id. */
3894 if (!old_face && IT_CHARPOS (*it) > BEG)
3896 int prev_face_id = face_before_it_pos (it);
3898 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3901 /* If the new face has a box, but the old face does not,
3902 this is the start of a run of characters with box face,
3903 i.e. this character has a shadow on the left side. */
3904 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3905 && (old_face == NULL || !old_face->box));
3906 it->face_box_p = new_face->box != FACE_NO_BOX;
3909 else
3911 int base_face_id;
3912 ptrdiff_t bufpos;
3913 int i;
3914 Lisp_Object from_overlay
3915 = (it->current.overlay_string_index >= 0
3916 ? it->string_overlays[it->current.overlay_string_index
3917 % OVERLAY_STRING_CHUNK_SIZE]
3918 : Qnil);
3920 /* See if we got to this string directly or indirectly from
3921 an overlay property. That includes the before-string or
3922 after-string of an overlay, strings in display properties
3923 provided by an overlay, their text properties, etc.
3925 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3926 if (! NILP (from_overlay))
3927 for (i = it->sp - 1; i >= 0; i--)
3929 if (it->stack[i].current.overlay_string_index >= 0)
3930 from_overlay
3931 = it->string_overlays[it->stack[i].current.overlay_string_index
3932 % OVERLAY_STRING_CHUNK_SIZE];
3933 else if (! NILP (it->stack[i].from_overlay))
3934 from_overlay = it->stack[i].from_overlay;
3936 if (!NILP (from_overlay))
3937 break;
3940 if (! NILP (from_overlay))
3942 bufpos = IT_CHARPOS (*it);
3943 /* For a string from an overlay, the base face depends
3944 only on text properties and ignores overlays. */
3945 base_face_id
3946 = face_for_overlay_string (it->w,
3947 IT_CHARPOS (*it),
3948 &next_stop,
3949 (IT_CHARPOS (*it)
3950 + TEXT_PROP_DISTANCE_LIMIT),
3951 false,
3952 from_overlay);
3954 else
3956 bufpos = 0;
3958 /* For strings from a `display' property, use the face at
3959 IT's current buffer position as the base face to merge
3960 with, so that overlay strings appear in the same face as
3961 surrounding text, unless they specify their own faces.
3962 For strings from wrap-prefix and line-prefix properties,
3963 use the default face, possibly remapped via
3964 Vface_remapping_alist. */
3965 /* Note that the fact that we use the face at _buffer_
3966 position means that a 'display' property on an overlay
3967 string will not inherit the face of that overlay string,
3968 but will instead revert to the face of buffer text
3969 covered by the overlay. This is visible, e.g., when the
3970 overlay specifies a box face, but neither the buffer nor
3971 the display string do. This sounds like a design bug,
3972 but Emacs always did that since v21.1, so changing that
3973 might be a big deal. */
3974 base_face_id = it->string_from_prefix_prop_p
3975 ? (!NILP (Vface_remapping_alist)
3976 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3977 : DEFAULT_FACE_ID)
3978 : underlying_face_id (it);
3981 new_face_id = face_at_string_position (it->w,
3982 it->string,
3983 IT_STRING_CHARPOS (*it),
3984 bufpos,
3985 &next_stop,
3986 base_face_id, false);
3988 /* Is this a start of a run of characters with box? Caveat:
3989 this can be called for a freshly allocated iterator; face_id
3990 is -1 is this case. We know that the new face will not
3991 change until the next check pos, i.e. if the new face has a
3992 box, all characters up to that position will have a
3993 box. But, as usual, we don't know whether that position
3994 is really the end. */
3995 if (new_face_id != it->face_id)
3997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3998 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4000 /* If new face has a box but old face hasn't, this is the
4001 start of a run of characters with box, i.e. it has a
4002 shadow on the left side. */
4003 it->start_of_box_run_p
4004 = new_face->box && (old_face == NULL || !old_face->box);
4005 it->face_box_p = new_face->box != FACE_NO_BOX;
4009 it->face_id = new_face_id;
4010 return HANDLED_NORMALLY;
4014 /* Return the ID of the face ``underlying'' IT's current position,
4015 which is in a string. If the iterator is associated with a
4016 buffer, return the face at IT's current buffer position.
4017 Otherwise, use the iterator's base_face_id. */
4019 static int
4020 underlying_face_id (struct it *it)
4022 int face_id = it->base_face_id, i;
4024 eassert (STRINGP (it->string));
4026 for (i = it->sp - 1; i >= 0; --i)
4027 if (NILP (it->stack[i].string))
4028 face_id = it->stack[i].face_id;
4030 return face_id;
4034 /* Compute the face one character before or after the current position
4035 of IT, in the visual order. BEFORE_P means get the face
4036 in front (to the left in L2R paragraphs, to the right in R2L
4037 paragraphs) of IT's screen position. Value is the ID of the face. */
4039 static int
4040 face_before_or_after_it_pos (struct it *it, bool before_p)
4042 int face_id, limit;
4043 ptrdiff_t next_check_charpos;
4044 struct it it_copy;
4045 void *it_copy_data = NULL;
4047 eassert (it->s == NULL);
4049 if (STRINGP (it->string))
4051 ptrdiff_t bufpos, charpos;
4052 int base_face_id;
4054 /* No face change past the end of the string (for the case
4055 we are padding with spaces). No face change before the
4056 string start. */
4057 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4058 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4059 return it->face_id;
4061 if (!it->bidi_p)
4063 /* Set charpos to the position before or after IT's current
4064 position, in the logical order, which in the non-bidi
4065 case is the same as the visual order. */
4066 if (before_p)
4067 charpos = IT_STRING_CHARPOS (*it) - 1;
4068 else if (it->what == IT_COMPOSITION)
4069 /* For composition, we must check the character after the
4070 composition. */
4071 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4072 else
4073 charpos = IT_STRING_CHARPOS (*it) + 1;
4075 else
4077 if (before_p)
4079 /* With bidi iteration, the character before the current
4080 in the visual order cannot be found by simple
4081 iteration, because "reverse" reordering is not
4082 supported. Instead, we need to start from the string
4083 beginning and go all the way to the current string
4084 position, remembering the previous position. */
4085 /* Ignore face changes before the first visible
4086 character on this display line. */
4087 if (it->current_x <= it->first_visible_x)
4088 return it->face_id;
4089 SAVE_IT (it_copy, *it, it_copy_data);
4090 IT_STRING_CHARPOS (it_copy) = 0;
4091 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4095 charpos = IT_STRING_CHARPOS (it_copy);
4096 if (charpos >= SCHARS (it->string))
4097 break;
4098 bidi_move_to_visually_next (&it_copy.bidi_it);
4100 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 int current_x;
4183 /* With bidi iteration, the character before the current
4184 in the visual order cannot be found by simple
4185 iteration, because "reverse" reordering is not
4186 supported. Instead, we need to use the move_it_*
4187 family of functions, and move to the previous
4188 character starting from the beginning of the visual
4189 line. */
4190 /* Ignore face changes before the first visible
4191 character on this display line. */
4192 if (it->current_x <= it->first_visible_x)
4193 return it->face_id;
4194 SAVE_IT (it_copy, *it, it_copy_data);
4195 /* Implementation note: Since move_it_in_display_line
4196 works in the iterator geometry, and thinks the first
4197 character is always the leftmost, even in R2L lines,
4198 we don't need to distinguish between the R2L and L2R
4199 cases here. */
4200 current_x = it_copy.current_x;
4201 move_it_vertically_backward (&it_copy, 0);
4202 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4203 pos = it_copy.current.pos;
4204 RESTORE_IT (it, it, it_copy_data);
4206 else
4208 /* Set charpos to the buffer position of the character
4209 that comes after IT's current position in the visual
4210 order. */
4211 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4213 it_copy = *it;
4214 while (n--)
4215 bidi_move_to_visually_next (&it_copy.bidi_it);
4217 SET_TEXT_POS (pos,
4218 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4221 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4223 /* Determine face for CHARSET_ASCII, or unibyte. */
4224 face_id = face_at_buffer_position (it->w,
4225 CHARPOS (pos),
4226 &next_check_charpos,
4227 limit, false, -1);
4229 /* Correct the face for charsets different from ASCII. Do it
4230 for the multibyte case only. The face returned above is
4231 suitable for unibyte text if current_buffer is unibyte. */
4232 if (it->multibyte_p)
4234 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4235 struct face *face = FACE_FROM_ID (it->f, face_id);
4236 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4240 return face_id;
4245 /***********************************************************************
4246 Invisible text
4247 ***********************************************************************/
4249 /* Set up iterator IT from invisible properties at its current
4250 position. Called from handle_stop. */
4252 static enum prop_handled
4253 handle_invisible_prop (struct it *it)
4255 enum prop_handled handled = HANDLED_NORMALLY;
4256 int invis;
4257 Lisp_Object prop;
4259 if (STRINGP (it->string))
4261 Lisp_Object end_charpos, limit;
4263 /* Get the value of the invisible text property at the
4264 current position. Value will be nil if there is no such
4265 property. */
4266 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4267 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4268 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4272 /* Record whether we have to display an ellipsis for the
4273 invisible text. */
4274 bool display_ellipsis_p = (invis == 2);
4275 ptrdiff_t len, endpos;
4277 handled = HANDLED_RECOMPUTE_PROPS;
4279 /* Get the position at which the next visible text can be
4280 found in IT->string, if any. */
4281 endpos = len = SCHARS (it->string);
4282 XSETINT (limit, len);
4285 end_charpos
4286 = Fnext_single_property_change (end_charpos, Qinvisible,
4287 it->string, limit);
4288 /* Since LIMIT is always an integer, so should be the
4289 value returned by Fnext_single_property_change. */
4290 eassert (INTEGERP (end_charpos));
4291 if (INTEGERP (end_charpos))
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis == 2)
4297 display_ellipsis_p = true;
4299 else /* Should never happen; but if it does, exit the loop. */
4300 endpos = len;
4302 while (invis != 0 && endpos < len);
4304 if (display_ellipsis_p)
4305 it->ellipsis_p = true;
4307 if (endpos < len)
4309 /* Text at END_CHARPOS is visible. Move IT there. */
4310 struct text_pos old;
4311 ptrdiff_t oldpos;
4313 old = it->current.string_pos;
4314 oldpos = CHARPOS (old);
4315 if (it->bidi_p)
4317 if (it->bidi_it.first_elt
4318 && it->bidi_it.charpos < SCHARS (it->string))
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, true);
4321 /* Bidi-iterate out of the invisible text. */
4324 bidi_move_to_visually_next (&it->bidi_it);
4326 while (oldpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < endpos);
4329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4331 if (IT_CHARPOS (*it) >= endpos)
4332 it->prev_stop = endpos;
4334 else
4336 IT_STRING_CHARPOS (*it) = endpos;
4337 compute_string_pos (&it->current.string_pos, old, it->string);
4340 else
4342 /* The rest of the string is invisible. If this is an
4343 overlay string, proceed with the next overlay string
4344 or whatever comes and return a character from there. */
4345 if (it->current.overlay_string_index >= 0
4346 && !display_ellipsis_p)
4348 next_overlay_string (it);
4349 /* Don't check for overlay strings when we just
4350 finished processing them. */
4351 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4353 else
4355 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4356 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4361 else
4363 ptrdiff_t newpos, next_stop, start_charpos, tem;
4364 Lisp_Object pos, overlay;
4366 /* First of all, is there invisible text at this position? */
4367 tem = start_charpos = IT_CHARPOS (*it);
4368 pos = make_number (tem);
4369 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4370 &overlay);
4371 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4373 /* If we are on invisible text, skip over it. */
4374 if (invis != 0 && start_charpos < it->end_charpos)
4376 /* Record whether we have to display an ellipsis for the
4377 invisible text. */
4378 bool display_ellipsis_p = invis == 2;
4380 handled = HANDLED_RECOMPUTE_PROPS;
4382 /* Loop skipping over invisible text. The loop is left at
4383 ZV or with IT on the first char being visible again. */
4386 /* Try to skip some invisible text. Return value is the
4387 position reached which can be equal to where we start
4388 if there is nothing invisible there. This skips both
4389 over invisible text properties and overlays with
4390 invisible property. */
4391 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4393 /* If we skipped nothing at all we weren't at invisible
4394 text in the first place. If everything to the end of
4395 the buffer was skipped, end the loop. */
4396 if (newpos == tem || newpos >= ZV)
4397 invis = 0;
4398 else
4400 /* We skipped some characters but not necessarily
4401 all there are. Check if we ended up on visible
4402 text. Fget_char_property returns the property of
4403 the char before the given position, i.e. if we
4404 get invis = 0, this means that the char at
4405 newpos is visible. */
4406 pos = make_number (newpos);
4407 prop = Fget_char_property (pos, Qinvisible, it->window);
4408 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4411 /* If we ended up on invisible text, proceed to
4412 skip starting with next_stop. */
4413 if (invis != 0)
4414 tem = next_stop;
4416 /* If there are adjacent invisible texts, don't lose the
4417 second one's ellipsis. */
4418 if (invis == 2)
4419 display_ellipsis_p = true;
4421 while (invis != 0);
4423 /* The position newpos is now either ZV or on visible text. */
4424 if (it->bidi_p)
4426 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4427 bool on_newline
4428 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4429 bool after_newline
4430 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4432 /* If the invisible text ends on a newline or on a
4433 character after a newline, we can avoid the costly,
4434 character by character, bidi iteration to NEWPOS, and
4435 instead simply reseat the iterator there. That's
4436 because all bidi reordering information is tossed at
4437 the newline. This is a big win for modes that hide
4438 complete lines, like Outline, Org, etc. */
4439 if (on_newline || after_newline)
4441 struct text_pos tpos;
4442 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4444 SET_TEXT_POS (tpos, newpos, bpos);
4445 reseat_1 (it, tpos, false);
4446 /* If we reseat on a newline/ZV, we need to prep the
4447 bidi iterator for advancing to the next character
4448 after the newline/EOB, keeping the current paragraph
4449 direction (so that PRODUCE_GLYPHS does TRT wrt
4450 prepending/appending glyphs to a glyph row). */
4451 if (on_newline)
4453 it->bidi_it.first_elt = false;
4454 it->bidi_it.paragraph_dir = pdir;
4455 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4456 it->bidi_it.nchars = 1;
4457 it->bidi_it.ch_len = 1;
4460 else /* Must use the slow method. */
4462 /* With bidi iteration, the region of invisible text
4463 could start and/or end in the middle of a
4464 non-base embedding level. Therefore, we need to
4465 skip invisible text using the bidi iterator,
4466 starting at IT's current position, until we find
4467 ourselves outside of the invisible text.
4468 Skipping invisible text _after_ bidi iteration
4469 avoids affecting the visual order of the
4470 displayed text when invisible properties are
4471 added or removed. */
4472 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4474 /* If we were `reseat'ed to a new paragraph,
4475 determine the paragraph base direction. We
4476 need to do it now because
4477 next_element_from_buffer may not have a
4478 chance to do it, if we are going to skip any
4479 text at the beginning, which resets the
4480 FIRST_ELT flag. */
4481 bidi_paragraph_init (it->paragraph_embedding,
4482 &it->bidi_it, true);
4486 bidi_move_to_visually_next (&it->bidi_it);
4488 while (it->stop_charpos <= it->bidi_it.charpos
4489 && it->bidi_it.charpos < newpos);
4490 IT_CHARPOS (*it) = it->bidi_it.charpos;
4491 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4492 /* If we overstepped NEWPOS, record its position in
4493 the iterator, so that we skip invisible text if
4494 later the bidi iteration lands us in the
4495 invisible region again. */
4496 if (IT_CHARPOS (*it) >= newpos)
4497 it->prev_stop = newpos;
4500 else
4502 IT_CHARPOS (*it) = newpos;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4506 if (display_ellipsis_p)
4508 /* Make sure that the glyphs of the ellipsis will get
4509 correct `charpos' values. If we would not update
4510 it->position here, the glyphs would belong to the
4511 last visible character _before_ the invisible
4512 text, which confuses `set_cursor_from_row'.
4514 We use the last invisible position instead of the
4515 first because this way the cursor is always drawn on
4516 the first "." of the ellipsis, whenever PT is inside
4517 the invisible text. Otherwise the cursor would be
4518 placed _after_ the ellipsis when the point is after the
4519 first invisible character. */
4520 if (!STRINGP (it->object))
4522 it->position.charpos = newpos - 1;
4523 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4527 /* If there are before-strings at the start of invisible
4528 text, and the text is invisible because of a text
4529 property, arrange to show before-strings because 20.x did
4530 it that way. (If the text is invisible because of an
4531 overlay property instead of a text property, this is
4532 already handled in the overlay code.) */
4533 if (NILP (overlay)
4534 && get_overlay_strings (it, it->stop_charpos))
4536 handled = HANDLED_RECOMPUTE_PROPS;
4537 if (it->sp > 0)
4539 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4540 /* The call to get_overlay_strings above recomputes
4541 it->stop_charpos, but it only considers changes
4542 in properties and overlays beyond iterator's
4543 current position. This causes us to miss changes
4544 that happen exactly where the invisible property
4545 ended. So we play it safe here and force the
4546 iterator to check for potential stop positions
4547 immediately after the invisible text. Note that
4548 if get_overlay_strings returns true, it
4549 normally also pushed the iterator stack, so we
4550 need to update the stop position in the slot
4551 below the current one. */
4552 it->stack[it->sp - 1].stop_charpos
4553 = CHARPOS (it->stack[it->sp - 1].current.pos);
4556 else if (display_ellipsis_p)
4558 it->ellipsis_p = true;
4559 /* Let the ellipsis display before
4560 considering any properties of the following char.
4561 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4562 handled = HANDLED_RETURN;
4567 return handled;
4571 /* Make iterator IT return `...' next.
4572 Replaces LEN characters from buffer. */
4574 static void
4575 setup_for_ellipsis (struct it *it, int len)
4577 /* Use the display table definition for `...'. Invalid glyphs
4578 will be handled by the method returning elements from dpvec. */
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4581 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4582 it->dpvec = v->contents;
4583 it->dpend = v->contents + v->header.size;
4585 else
4587 /* Default `...'. */
4588 it->dpvec = default_invis_vector;
4589 it->dpend = default_invis_vector + 3;
4592 it->dpvec_char_len = len;
4593 it->current.dpvec_index = 0;
4594 it->dpvec_face_id = -1;
4596 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4597 face as the preceding text. IT->saved_face_id was set in
4598 handle_stop to the face of the preceding character, and will be
4599 different from IT->face_id only if the invisible text skipped in
4600 handle_invisible_prop has some non-default face on its first
4601 character. We thus ignore the face of the invisible text when we
4602 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4603 if (it->saved_face_id >= 0)
4604 it->face_id = it->saved_face_id;
4606 /* If the ellipsis represents buffer text, it means we advanced in
4607 the buffer, so we should no longer ignore overlay strings. */
4608 if (it->method == GET_FROM_BUFFER)
4609 it->ignore_overlay_strings_at_pos_p = false;
4611 it->method = GET_FROM_DISPLAY_VECTOR;
4612 it->ellipsis_p = true;
4617 /***********************************************************************
4618 'display' property
4619 ***********************************************************************/
4621 /* Set up iterator IT from `display' property at its current position.
4622 Called from handle_stop.
4623 We return HANDLED_RETURN if some part of the display property
4624 overrides the display of the buffer text itself.
4625 Otherwise we return HANDLED_NORMALLY. */
4627 static enum prop_handled
4628 handle_display_prop (struct it *it)
4630 Lisp_Object propval, object, overlay;
4631 struct text_pos *position;
4632 ptrdiff_t bufpos;
4633 /* Nonzero if some property replaces the display of the text itself. */
4634 int display_replaced = 0;
4636 if (STRINGP (it->string))
4638 object = it->string;
4639 position = &it->current.string_pos;
4640 bufpos = CHARPOS (it->current.pos);
4642 else
4644 XSETWINDOW (object, it->w);
4645 position = &it->current.pos;
4646 bufpos = CHARPOS (*position);
4649 /* Reset those iterator values set from display property values. */
4650 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4651 it->space_width = Qnil;
4652 it->font_height = Qnil;
4653 it->voffset = 0;
4655 /* We don't support recursive `display' properties, i.e. string
4656 values that have a string `display' property, that have a string
4657 `display' property etc. */
4658 if (!it->string_from_display_prop_p)
4659 it->area = TEXT_AREA;
4661 propval = get_char_property_and_overlay (make_number (position->charpos),
4662 Qdisplay, object, &overlay);
4663 if (NILP (propval))
4664 return HANDLED_NORMALLY;
4665 /* Now OVERLAY is the overlay that gave us this property, or nil
4666 if it was a text property. */
4668 if (!STRINGP (it->string))
4669 object = it->w->contents;
4671 display_replaced = handle_display_spec (it, propval, object, overlay,
4672 position, bufpos,
4673 FRAME_WINDOW_P (it->f));
4674 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4677 /* Subroutine of handle_display_prop. Returns non-zero if the display
4678 specification in SPEC is a replacing specification, i.e. it would
4679 replace the text covered by `display' property with something else,
4680 such as an image or a display string. If SPEC includes any kind or
4681 `(space ...) specification, the value is 2; this is used by
4682 compute_display_string_pos, which see.
4684 See handle_single_display_spec for documentation of arguments.
4685 FRAME_WINDOW_P is true if the window being redisplayed is on a
4686 GUI frame; this argument is used only if IT is NULL, see below.
4688 IT can be NULL, if this is called by the bidi reordering code
4689 through compute_display_string_pos, which see. In that case, this
4690 function only examines SPEC, but does not otherwise "handle" it, in
4691 the sense that it doesn't set up members of IT from the display
4692 spec. */
4693 static int
4694 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4695 Lisp_Object overlay, struct text_pos *position,
4696 ptrdiff_t bufpos, bool frame_window_p)
4698 int replacing = 0;
4700 if (CONSP (spec)
4701 /* Simple specifications. */
4702 && !EQ (XCAR (spec), Qimage)
4703 #ifdef HAVE_XWIDGETS
4704 && !EQ (XCAR (spec), Qxwidget)
4705 #endif
4706 && !EQ (XCAR (spec), Qspace)
4707 && !EQ (XCAR (spec), Qwhen)
4708 && !EQ (XCAR (spec), Qslice)
4709 && !EQ (XCAR (spec), Qspace_width)
4710 && !EQ (XCAR (spec), Qheight)
4711 && !EQ (XCAR (spec), Qraise)
4712 /* Marginal area specifications. */
4713 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4714 && !EQ (XCAR (spec), Qleft_fringe)
4715 && !EQ (XCAR (spec), Qright_fringe)
4716 && !NILP (XCAR (spec)))
4718 for (; CONSP (spec); spec = XCDR (spec))
4720 int rv = handle_single_display_spec (it, XCAR (spec), object,
4721 overlay, position, bufpos,
4722 replacing, frame_window_p);
4723 if (rv != 0)
4725 replacing = rv;
4726 /* If some text in a string is replaced, `position' no
4727 longer points to the position of `object'. */
4728 if (!it || STRINGP (object))
4729 break;
4733 else if (VECTORP (spec))
4735 ptrdiff_t i;
4736 for (i = 0; i < ASIZE (spec); ++i)
4738 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4739 overlay, position, bufpos,
4740 replacing, frame_window_p);
4741 if (rv != 0)
4743 replacing = rv;
4744 /* If some text in a string is replaced, `position' no
4745 longer points to the position of `object'. */
4746 if (!it || STRINGP (object))
4747 break;
4751 else
4752 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4753 bufpos, 0, frame_window_p);
4754 return replacing;
4757 /* Value is the position of the end of the `display' property starting
4758 at START_POS in OBJECT. */
4760 static struct text_pos
4761 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4763 Lisp_Object end;
4764 struct text_pos end_pos;
4766 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4767 Qdisplay, object, Qnil);
4768 CHARPOS (end_pos) = XFASTINT (end);
4769 if (STRINGP (object))
4770 compute_string_pos (&end_pos, start_pos, it->string);
4771 else
4772 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4774 return end_pos;
4778 /* Set up IT from a single `display' property specification SPEC. OBJECT
4779 is the object in which the `display' property was found. *POSITION
4780 is the position in OBJECT at which the `display' property was found.
4781 BUFPOS is the buffer position of OBJECT (different from POSITION if
4782 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4783 previously saw a display specification which already replaced text
4784 display with something else, for example an image; we ignore such
4785 properties after the first one has been processed.
4787 OVERLAY is the overlay this `display' property came from,
4788 or nil if it was a text property.
4790 If SPEC is a `space' or `image' specification, and in some other
4791 cases too, set *POSITION to the position where the `display'
4792 property ends.
4794 If IT is NULL, only examine the property specification in SPEC, but
4795 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4796 is intended to be displayed in a window on a GUI frame.
4798 Value is non-zero if something was found which replaces the display
4799 of buffer or string text. */
4801 static int
4802 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4803 Lisp_Object overlay, struct text_pos *position,
4804 ptrdiff_t bufpos, int display_replaced,
4805 bool frame_window_p)
4807 Lisp_Object form;
4808 Lisp_Object location, value;
4809 struct text_pos start_pos = *position;
4811 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4812 If the result is non-nil, use VALUE instead of SPEC. */
4813 form = Qt;
4814 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4816 spec = XCDR (spec);
4817 if (!CONSP (spec))
4818 return 0;
4819 form = XCAR (spec);
4820 spec = XCDR (spec);
4823 if (!NILP (form) && !EQ (form, Qt))
4825 ptrdiff_t count = SPECPDL_INDEX ();
4827 /* Bind `object' to the object having the `display' property, a
4828 buffer or string. Bind `position' to the position in the
4829 object where the property was found, and `buffer-position'
4830 to the current position in the buffer. */
4832 if (NILP (object))
4833 XSETBUFFER (object, current_buffer);
4834 specbind (Qobject, object);
4835 specbind (Qposition, make_number (CHARPOS (*position)));
4836 specbind (Qbuffer_position, make_number (bufpos));
4837 form = safe_eval (form);
4838 unbind_to (count, Qnil);
4841 if (NILP (form))
4842 return 0;
4844 /* Handle `(height HEIGHT)' specifications. */
4845 if (CONSP (spec)
4846 && EQ (XCAR (spec), Qheight)
4847 && CONSP (XCDR (spec)))
4849 if (it)
4851 if (!FRAME_WINDOW_P (it->f))
4852 return 0;
4854 it->font_height = XCAR (XCDR (spec));
4855 if (!NILP (it->font_height))
4857 int new_height = -1;
4859 if (CONSP (it->font_height)
4860 && (EQ (XCAR (it->font_height), Qplus)
4861 || EQ (XCAR (it->font_height), Qminus))
4862 && CONSP (XCDR (it->font_height))
4863 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4865 /* `(+ N)' or `(- N)' where N is an integer. */
4866 int steps = XINT (XCAR (XCDR (it->font_height)));
4867 if (EQ (XCAR (it->font_height), Qplus))
4868 steps = - steps;
4869 it->face_id = smaller_face (it->f, it->face_id, steps);
4871 else if (FUNCTIONP (it->font_height))
4873 /* Call function with current height as argument.
4874 Value is the new height. */
4875 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4876 Lisp_Object height;
4877 height = safe_call1 (it->font_height,
4878 face->lface[LFACE_HEIGHT_INDEX]);
4879 if (NUMBERP (height))
4880 new_height = XFLOATINT (height);
4882 else if (NUMBERP (it->font_height))
4884 /* Value is a multiple of the canonical char height. */
4885 struct face *f;
4887 f = FACE_FROM_ID (it->f,
4888 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4889 new_height = (XFLOATINT (it->font_height)
4890 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4892 else
4894 /* Evaluate IT->font_height with `height' bound to the
4895 current specified height to get the new height. */
4896 ptrdiff_t count = SPECPDL_INDEX ();
4897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4899 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4900 value = safe_eval (it->font_height);
4901 unbind_to (count, Qnil);
4903 if (NUMBERP (value))
4904 new_height = XFLOATINT (value);
4907 if (new_height > 0)
4908 it->face_id = face_with_height (it->f, it->face_id, new_height);
4912 return 0;
4915 /* Handle `(space-width WIDTH)'. */
4916 if (CONSP (spec)
4917 && EQ (XCAR (spec), Qspace_width)
4918 && CONSP (XCDR (spec)))
4920 if (it)
4922 if (!FRAME_WINDOW_P (it->f))
4923 return 0;
4925 value = XCAR (XCDR (spec));
4926 if (NUMBERP (value) && XFLOATINT (value) > 0)
4927 it->space_width = value;
4930 return 0;
4933 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4934 if (CONSP (spec)
4935 && EQ (XCAR (spec), Qslice))
4937 Lisp_Object tem;
4939 if (it)
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4944 if (tem = XCDR (spec), CONSP (tem))
4946 it->slice.x = XCAR (tem);
4947 if (tem = XCDR (tem), CONSP (tem))
4949 it->slice.y = XCAR (tem);
4950 if (tem = XCDR (tem), CONSP (tem))
4952 it->slice.width = XCAR (tem);
4953 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.height = XCAR (tem);
4960 return 0;
4963 /* Handle `(raise FACTOR)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qraise)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 #ifdef HAVE_WINDOW_SYSTEM
4974 value = XCAR (XCDR (spec));
4975 if (NUMBERP (value))
4977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4978 it->voffset = - (XFLOATINT (value)
4979 * (normal_char_height (face->font, -1)));
4981 #endif /* HAVE_WINDOW_SYSTEM */
4984 return 0;
4987 /* Don't handle the other kinds of display specifications
4988 inside a string that we got from a `display' property. */
4989 if (it && it->string_from_display_prop_p)
4990 return 0;
4992 /* Characters having this form of property are not displayed, so
4993 we have to find the end of the property. */
4994 if (it)
4996 start_pos = *position;
4997 *position = display_prop_end (it, object, start_pos);
4998 /* If the display property comes from an overlay, don't consider
4999 any potential stop_charpos values before the end of that
5000 overlay. Since display_prop_end will happily find another
5001 'display' property coming from some other overlay or text
5002 property on buffer positions before this overlay's end, we
5003 need to ignore them, or else we risk displaying this
5004 overlay's display string/image twice. */
5005 if (!NILP (overlay))
5007 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5009 if (ovendpos > CHARPOS (*position))
5010 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5013 value = Qnil;
5015 /* Stop the scan at that end position--we assume that all
5016 text properties change there. */
5017 if (it)
5018 it->stop_charpos = position->charpos;
5020 /* Handle `(left-fringe BITMAP [FACE])'
5021 and `(right-fringe BITMAP [FACE])'. */
5022 if (CONSP (spec)
5023 && (EQ (XCAR (spec), Qleft_fringe)
5024 || EQ (XCAR (spec), Qright_fringe))
5025 && CONSP (XCDR (spec)))
5027 if (it)
5029 if (!FRAME_WINDOW_P (it->f))
5030 /* If we return here, POSITION has been advanced
5031 across the text with this property. */
5033 /* Synchronize the bidi iterator with POSITION. This is
5034 needed because we are not going to push the iterator
5035 on behalf of this display property, so there will be
5036 no pop_it call to do this synchronization for us. */
5037 if (it->bidi_p)
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5043 return 1;
5046 else if (!frame_window_p)
5047 return 1;
5049 #ifdef HAVE_WINDOW_SYSTEM
5050 value = XCAR (XCDR (spec));
5051 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5052 if (! fringe_bitmap)
5053 /* If we return here, POSITION has been advanced
5054 across the text with this property. */
5056 if (it && it->bidi_p)
5058 it->position = *position;
5059 iterate_out_of_display_property (it);
5060 *position = it->position;
5062 return 1;
5065 if (it)
5067 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5069 if (CONSP (XCDR (XCDR (spec))))
5071 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5072 int face_id2 = lookup_derived_face (it->f, face_name,
5073 FRINGE_FACE_ID, false);
5074 if (face_id2 >= 0)
5075 face_id = face_id2;
5078 /* Save current settings of IT so that we can restore them
5079 when we are finished with the glyph property value. */
5080 push_it (it, position);
5082 it->area = TEXT_AREA;
5083 it->what = IT_IMAGE;
5084 it->image_id = -1; /* no image */
5085 it->position = start_pos;
5086 it->object = NILP (object) ? it->w->contents : object;
5087 it->method = GET_FROM_IMAGE;
5088 it->from_overlay = Qnil;
5089 it->face_id = face_id;
5090 it->from_disp_prop_p = true;
5092 /* Say that we haven't consumed the characters with
5093 `display' property yet. The call to pop_it in
5094 set_iterator_to_next will clean this up. */
5095 *position = start_pos;
5097 if (EQ (XCAR (spec), Qleft_fringe))
5099 it->left_user_fringe_bitmap = fringe_bitmap;
5100 it->left_user_fringe_face_id = face_id;
5102 else
5104 it->right_user_fringe_bitmap = fringe_bitmap;
5105 it->right_user_fringe_face_id = face_id;
5108 #endif /* HAVE_WINDOW_SYSTEM */
5109 return 1;
5112 /* Prepare to handle `((margin left-margin) ...)',
5113 `((margin right-margin) ...)' and `((margin nil) ...)'
5114 prefixes for display specifications. */
5115 location = Qunbound;
5116 if (CONSP (spec) && CONSP (XCAR (spec)))
5118 Lisp_Object tem;
5120 value = XCDR (spec);
5121 if (CONSP (value))
5122 value = XCAR (value);
5124 tem = XCAR (spec);
5125 if (EQ (XCAR (tem), Qmargin)
5126 && (tem = XCDR (tem),
5127 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5128 (NILP (tem)
5129 || EQ (tem, Qleft_margin)
5130 || EQ (tem, Qright_margin))))
5131 location = tem;
5134 if (EQ (location, Qunbound))
5136 location = Qnil;
5137 value = spec;
5140 /* After this point, VALUE is the property after any
5141 margin prefix has been stripped. It must be a string,
5142 an image specification, or `(space ...)'.
5144 LOCATION specifies where to display: `left-margin',
5145 `right-margin' or nil. */
5147 bool valid_p = (STRINGP (value)
5148 #ifdef HAVE_WINDOW_SYSTEM
5149 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5150 && valid_image_p (value))
5151 #endif /* not HAVE_WINDOW_SYSTEM */
5152 || (CONSP (value) && EQ (XCAR (value), Qspace))
5153 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5154 && valid_xwidget_spec_p (value)));
5156 if (valid_p && display_replaced == 0)
5158 int retval = 1;
5160 if (!it)
5162 /* Callers need to know whether the display spec is any kind
5163 of `(space ...)' spec that is about to affect text-area
5164 display. */
5165 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5166 retval = 2;
5167 return retval;
5170 /* Save current settings of IT so that we can restore them
5171 when we are finished with the glyph property value. */
5172 push_it (it, position);
5173 it->from_overlay = overlay;
5174 it->from_disp_prop_p = true;
5176 if (NILP (location))
5177 it->area = TEXT_AREA;
5178 else if (EQ (location, Qleft_margin))
5179 it->area = LEFT_MARGIN_AREA;
5180 else
5181 it->area = RIGHT_MARGIN_AREA;
5183 if (STRINGP (value))
5185 it->string = value;
5186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5187 it->current.overlay_string_index = -1;
5188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5189 it->end_charpos = it->string_nchars = SCHARS (it->string);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 it->prev_stop = 0;
5193 it->base_level_stop = 0;
5194 it->string_from_display_prop_p = true;
5195 /* Say that we haven't consumed the characters with
5196 `display' property yet. The call to pop_it in
5197 set_iterator_to_next will clean this up. */
5198 if (BUFFERP (object))
5199 *position = start_pos;
5201 /* Force paragraph direction to be that of the parent
5202 object. If the parent object's paragraph direction is
5203 not yet determined, default to L2R. */
5204 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5205 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5206 else
5207 it->paragraph_embedding = L2R;
5209 /* Set up the bidi iterator for this display string. */
5210 if (it->bidi_p)
5212 it->bidi_it.string.lstring = it->string;
5213 it->bidi_it.string.s = NULL;
5214 it->bidi_it.string.schars = it->end_charpos;
5215 it->bidi_it.string.bufpos = bufpos;
5216 it->bidi_it.string.from_disp_str = true;
5217 it->bidi_it.string.unibyte = !it->multibyte_p;
5218 it->bidi_it.w = it->w;
5219 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5222 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5224 it->method = GET_FROM_STRETCH;
5225 it->object = value;
5226 *position = it->position = start_pos;
5227 retval = 1 + (it->area == TEXT_AREA);
5229 else if (valid_xwidget_spec_p (value))
5231 it->what = IT_XWIDGET;
5232 it->method = GET_FROM_XWIDGET;
5233 it->position = start_pos;
5234 it->object = NILP (object) ? it->w->contents : object;
5235 *position = start_pos;
5236 it->xwidget = lookup_xwidget (value);
5238 #ifdef HAVE_WINDOW_SYSTEM
5239 else
5241 it->what = IT_IMAGE;
5242 it->image_id = lookup_image (it->f, value);
5243 it->position = start_pos;
5244 it->object = NILP (object) ? it->w->contents : object;
5245 it->method = GET_FROM_IMAGE;
5247 /* Say that we haven't consumed the characters with
5248 `display' property yet. The call to pop_it in
5249 set_iterator_to_next will clean this up. */
5250 *position = start_pos;
5252 #endif /* HAVE_WINDOW_SYSTEM */
5254 return retval;
5257 /* Invalid property or property not supported. Restore
5258 POSITION to what it was before. */
5259 *position = start_pos;
5260 return 0;
5263 /* Check if PROP is a display property value whose text should be
5264 treated as intangible. OVERLAY is the overlay from which PROP
5265 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5266 specify the buffer position covered by PROP. */
5268 bool
5269 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5270 ptrdiff_t charpos, ptrdiff_t bytepos)
5272 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5273 struct text_pos position;
5275 SET_TEXT_POS (position, charpos, bytepos);
5276 return (handle_display_spec (NULL, prop, Qnil, overlay,
5277 &position, charpos, frame_window_p)
5278 != 0);
5282 /* Return true if PROP is a display sub-property value containing STRING.
5284 Implementation note: this and the following function are really
5285 special cases of handle_display_spec and
5286 handle_single_display_spec, and should ideally use the same code.
5287 Until they do, these two pairs must be consistent and must be
5288 modified in sync. */
5290 static bool
5291 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5293 if (EQ (string, prop))
5294 return true;
5296 /* Skip over `when FORM'. */
5297 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5299 prop = XCDR (prop);
5300 if (!CONSP (prop))
5301 return false;
5302 /* Actually, the condition following `when' should be eval'ed,
5303 like handle_single_display_spec does, and we should return
5304 false if it evaluates to nil. However, this function is
5305 called only when the buffer was already displayed and some
5306 glyph in the glyph matrix was found to come from a display
5307 string. Therefore, the condition was already evaluated, and
5308 the result was non-nil, otherwise the display string wouldn't
5309 have been displayed and we would have never been called for
5310 this property. Thus, we can skip the evaluation and assume
5311 its result is non-nil. */
5312 prop = XCDR (prop);
5315 if (CONSP (prop))
5316 /* Skip over `margin LOCATION'. */
5317 if (EQ (XCAR (prop), Qmargin))
5319 prop = XCDR (prop);
5320 if (!CONSP (prop))
5321 return false;
5323 prop = XCDR (prop);
5324 if (!CONSP (prop))
5325 return false;
5328 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5332 /* Return true if STRING appears in the `display' property PROP. */
5334 static bool
5335 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5337 if (CONSP (prop)
5338 && !EQ (XCAR (prop), Qwhen)
5339 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5341 /* A list of sub-properties. */
5342 while (CONSP (prop))
5344 if (single_display_spec_string_p (XCAR (prop), string))
5345 return true;
5346 prop = XCDR (prop);
5349 else if (VECTORP (prop))
5351 /* A vector of sub-properties. */
5352 ptrdiff_t i;
5353 for (i = 0; i < ASIZE (prop); ++i)
5354 if (single_display_spec_string_p (AREF (prop, i), string))
5355 return true;
5357 else
5358 return single_display_spec_string_p (prop, string);
5360 return false;
5363 /* Look for STRING in overlays and text properties in the current
5364 buffer, between character positions FROM and TO (excluding TO).
5365 BACK_P means look back (in this case, TO is supposed to be
5366 less than FROM).
5367 Value is the first character position where STRING was found, or
5368 zero if it wasn't found before hitting TO.
5370 This function may only use code that doesn't eval because it is
5371 called asynchronously from note_mouse_highlight. */
5373 static ptrdiff_t
5374 string_buffer_position_lim (Lisp_Object string,
5375 ptrdiff_t from, ptrdiff_t to, bool back_p)
5377 Lisp_Object limit, prop, pos;
5378 bool found = false;
5380 pos = make_number (max (from, BEGV));
5382 if (!back_p) /* looking forward */
5384 limit = make_number (min (to, ZV));
5385 while (!found && !EQ (pos, limit))
5387 prop = Fget_char_property (pos, Qdisplay, Qnil);
5388 if (!NILP (prop) && display_prop_string_p (prop, string))
5389 found = true;
5390 else
5391 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5392 limit);
5395 else /* looking back */
5397 limit = make_number (max (to, BEGV));
5398 while (!found && !EQ (pos, limit))
5400 prop = Fget_char_property (pos, Qdisplay, Qnil);
5401 if (!NILP (prop) && display_prop_string_p (prop, string))
5402 found = true;
5403 else
5404 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5405 limit);
5409 return found ? XINT (pos) : 0;
5412 /* Determine which buffer position in current buffer STRING comes from.
5413 AROUND_CHARPOS is an approximate position where it could come from.
5414 Value is the buffer position or 0 if it couldn't be determined.
5416 This function is necessary because we don't record buffer positions
5417 in glyphs generated from strings (to keep struct glyph small).
5418 This function may only use code that doesn't eval because it is
5419 called asynchronously from note_mouse_highlight. */
5421 static ptrdiff_t
5422 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5424 const int MAX_DISTANCE = 1000;
5425 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5426 around_charpos + MAX_DISTANCE,
5427 false);
5429 if (!found)
5430 found = string_buffer_position_lim (string, around_charpos,
5431 around_charpos - MAX_DISTANCE, true);
5432 return found;
5437 /***********************************************************************
5438 `composition' property
5439 ***********************************************************************/
5441 /* Set up iterator IT from `composition' property at its current
5442 position. Called from handle_stop. */
5444 static enum prop_handled
5445 handle_composition_prop (struct it *it)
5447 Lisp_Object prop, string;
5448 ptrdiff_t pos, pos_byte, start, end;
5450 if (STRINGP (it->string))
5452 unsigned char *s;
5454 pos = IT_STRING_CHARPOS (*it);
5455 pos_byte = IT_STRING_BYTEPOS (*it);
5456 string = it->string;
5457 s = SDATA (string) + pos_byte;
5458 it->c = STRING_CHAR (s);
5460 else
5462 pos = IT_CHARPOS (*it);
5463 pos_byte = IT_BYTEPOS (*it);
5464 string = Qnil;
5465 it->c = FETCH_CHAR (pos_byte);
5468 /* If there's a valid composition and point is not inside of the
5469 composition (in the case that the composition is from the current
5470 buffer), draw a glyph composed from the composition components. */
5471 if (find_composition (pos, -1, &start, &end, &prop, string)
5472 && composition_valid_p (start, end, prop)
5473 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5475 if (start < pos)
5476 /* As we can't handle this situation (perhaps font-lock added
5477 a new composition), we just return here hoping that next
5478 redisplay will detect this composition much earlier. */
5479 return HANDLED_NORMALLY;
5480 if (start != pos)
5482 if (STRINGP (it->string))
5483 pos_byte = string_char_to_byte (it->string, start);
5484 else
5485 pos_byte = CHAR_TO_BYTE (start);
5487 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5488 prop, string);
5490 if (it->cmp_it.id >= 0)
5492 it->cmp_it.ch = -1;
5493 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5494 it->cmp_it.nglyphs = -1;
5498 return HANDLED_NORMALLY;
5503 /***********************************************************************
5504 Overlay strings
5505 ***********************************************************************/
5507 /* The following structure is used to record overlay strings for
5508 later sorting in load_overlay_strings. */
5510 struct overlay_entry
5512 Lisp_Object overlay;
5513 Lisp_Object string;
5514 EMACS_INT priority;
5515 bool after_string_p;
5519 /* Set up iterator IT from overlay strings at its current position.
5520 Called from handle_stop. */
5522 static enum prop_handled
5523 handle_overlay_change (struct it *it)
5525 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5526 return HANDLED_RECOMPUTE_PROPS;
5527 else
5528 return HANDLED_NORMALLY;
5532 /* Set up the next overlay string for delivery by IT, if there is an
5533 overlay string to deliver. Called by set_iterator_to_next when the
5534 end of the current overlay string is reached. If there are more
5535 overlay strings to display, IT->string and
5536 IT->current.overlay_string_index are set appropriately here.
5537 Otherwise IT->string is set to nil. */
5539 static void
5540 next_overlay_string (struct it *it)
5542 ++it->current.overlay_string_index;
5543 if (it->current.overlay_string_index == it->n_overlay_strings)
5545 /* No more overlay strings. Restore IT's settings to what
5546 they were before overlay strings were processed, and
5547 continue to deliver from current_buffer. */
5549 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5550 pop_it (it);
5551 eassert (it->sp > 0
5552 || (NILP (it->string)
5553 && it->method == GET_FROM_BUFFER
5554 && it->stop_charpos >= BEGV
5555 && it->stop_charpos <= it->end_charpos));
5556 it->current.overlay_string_index = -1;
5557 it->n_overlay_strings = 0;
5558 /* If there's an empty display string on the stack, pop the
5559 stack, to resync the bidi iterator with IT's position. Such
5560 empty strings are pushed onto the stack in
5561 get_overlay_strings_1. */
5562 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5563 pop_it (it);
5565 /* Since we've exhausted overlay strings at this buffer
5566 position, set the flag to ignore overlays until we move to
5567 another position. The flag is reset in
5568 next_element_from_buffer. */
5569 it->ignore_overlay_strings_at_pos_p = true;
5571 /* If we're at the end of the buffer, record that we have
5572 processed the overlay strings there already, so that
5573 next_element_from_buffer doesn't try it again. */
5574 if (NILP (it->string)
5575 && IT_CHARPOS (*it) >= it->end_charpos
5576 && it->overlay_strings_charpos >= it->end_charpos)
5577 it->overlay_strings_at_end_processed_p = true;
5578 /* Note: we reset overlay_strings_charpos only here, to make
5579 sure the just-processed overlays were indeed at EOB.
5580 Otherwise, overlays on text with invisible text property,
5581 which are processed with IT's position past the invisible
5582 text, might fool us into thinking the overlays at EOB were
5583 already processed (linum-mode can cause this, for
5584 example). */
5585 it->overlay_strings_charpos = -1;
5587 else
5589 /* There are more overlay strings to process. If
5590 IT->current.overlay_string_index has advanced to a position
5591 where we must load IT->overlay_strings with more strings, do
5592 it. We must load at the IT->overlay_strings_charpos where
5593 IT->n_overlay_strings was originally computed; when invisible
5594 text is present, this might not be IT_CHARPOS (Bug#7016). */
5595 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5597 if (it->current.overlay_string_index && i == 0)
5598 load_overlay_strings (it, it->overlay_strings_charpos);
5600 /* Initialize IT to deliver display elements from the overlay
5601 string. */
5602 it->string = it->overlay_strings[i];
5603 it->multibyte_p = STRING_MULTIBYTE (it->string);
5604 SET_TEXT_POS (it->current.string_pos, 0, 0);
5605 it->method = GET_FROM_STRING;
5606 it->stop_charpos = 0;
5607 it->end_charpos = SCHARS (it->string);
5608 if (it->cmp_it.stop_pos >= 0)
5609 it->cmp_it.stop_pos = 0;
5610 it->prev_stop = 0;
5611 it->base_level_stop = 0;
5613 /* Set up the bidi iterator for this overlay string. */
5614 if (it->bidi_p)
5616 it->bidi_it.string.lstring = it->string;
5617 it->bidi_it.string.s = NULL;
5618 it->bidi_it.string.schars = SCHARS (it->string);
5619 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5620 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5621 it->bidi_it.string.unibyte = !it->multibyte_p;
5622 it->bidi_it.w = it->w;
5623 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5627 CHECK_IT (it);
5631 /* Compare two overlay_entry structures E1 and E2. Used as a
5632 comparison function for qsort in load_overlay_strings. Overlay
5633 strings for the same position are sorted so that
5635 1. All after-strings come in front of before-strings, except
5636 when they come from the same overlay.
5638 2. Within after-strings, strings are sorted so that overlay strings
5639 from overlays with higher priorities come first.
5641 2. Within before-strings, strings are sorted so that overlay
5642 strings from overlays with higher priorities come last.
5644 Value is analogous to strcmp. */
5647 static int
5648 compare_overlay_entries (const void *e1, const void *e2)
5650 struct overlay_entry const *entry1 = e1;
5651 struct overlay_entry const *entry2 = e2;
5652 int result;
5654 if (entry1->after_string_p != entry2->after_string_p)
5656 /* Let after-strings appear in front of before-strings if
5657 they come from different overlays. */
5658 if (EQ (entry1->overlay, entry2->overlay))
5659 result = entry1->after_string_p ? 1 : -1;
5660 else
5661 result = entry1->after_string_p ? -1 : 1;
5663 else if (entry1->priority != entry2->priority)
5665 if (entry1->after_string_p)
5666 /* After-strings sorted in order of decreasing priority. */
5667 result = entry2->priority < entry1->priority ? -1 : 1;
5668 else
5669 /* Before-strings sorted in order of increasing priority. */
5670 result = entry1->priority < entry2->priority ? -1 : 1;
5672 else
5673 result = 0;
5675 return result;
5679 /* Load the vector IT->overlay_strings with overlay strings from IT's
5680 current buffer position, or from CHARPOS if that is > 0. Set
5681 IT->n_overlays to the total number of overlay strings found.
5683 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5684 a time. On entry into load_overlay_strings,
5685 IT->current.overlay_string_index gives the number of overlay
5686 strings that have already been loaded by previous calls to this
5687 function.
5689 IT->add_overlay_start contains an additional overlay start
5690 position to consider for taking overlay strings from, if non-zero.
5691 This position comes into play when the overlay has an `invisible'
5692 property, and both before and after-strings. When we've skipped to
5693 the end of the overlay, because of its `invisible' property, we
5694 nevertheless want its before-string to appear.
5695 IT->add_overlay_start will contain the overlay start position
5696 in this case.
5698 Overlay strings are sorted so that after-string strings come in
5699 front of before-string strings. Within before and after-strings,
5700 strings are sorted by overlay priority. See also function
5701 compare_overlay_entries. */
5703 static void
5704 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5706 Lisp_Object overlay, window, str, invisible;
5707 struct Lisp_Overlay *ov;
5708 ptrdiff_t start, end;
5709 ptrdiff_t n = 0, i, j;
5710 int invis;
5711 struct overlay_entry entriesbuf[20];
5712 ptrdiff_t size = ARRAYELTS (entriesbuf);
5713 struct overlay_entry *entries = entriesbuf;
5714 USE_SAFE_ALLOCA;
5716 if (charpos <= 0)
5717 charpos = IT_CHARPOS (*it);
5719 /* Append the overlay string STRING of overlay OVERLAY to vector
5720 `entries' which has size `size' and currently contains `n'
5721 elements. AFTER_P means STRING is an after-string of
5722 OVERLAY. */
5723 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5724 do \
5726 Lisp_Object priority; \
5728 if (n == size) \
5730 struct overlay_entry *old = entries; \
5731 SAFE_NALLOCA (entries, 2, size); \
5732 memcpy (entries, old, size * sizeof *entries); \
5733 size *= 2; \
5736 entries[n].string = (STRING); \
5737 entries[n].overlay = (OVERLAY); \
5738 priority = Foverlay_get ((OVERLAY), Qpriority); \
5739 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5740 entries[n].after_string_p = (AFTER_P); \
5741 ++n; \
5743 while (false)
5745 /* Process overlay before the overlay center. */
5746 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5748 XSETMISC (overlay, ov);
5749 eassert (OVERLAYP (overlay));
5750 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5751 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5753 if (end < charpos)
5754 break;
5756 /* Skip this overlay if it doesn't start or end at IT's current
5757 position. */
5758 if (end != charpos && start != charpos)
5759 continue;
5761 /* Skip this overlay if it doesn't apply to IT->w. */
5762 window = Foverlay_get (overlay, Qwindow);
5763 if (WINDOWP (window) && XWINDOW (window) != it->w)
5764 continue;
5766 /* If the text ``under'' the overlay is invisible, both before-
5767 and after-strings from this overlay are visible; start and
5768 end position are indistinguishable. */
5769 invisible = Foverlay_get (overlay, Qinvisible);
5770 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772 /* If overlay has a non-empty before-string, record it. */
5773 if ((start == charpos || (end == charpos && invis != 0))
5774 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5775 && SCHARS (str))
5776 RECORD_OVERLAY_STRING (overlay, str, false);
5778 /* If overlay has a non-empty after-string, record it. */
5779 if ((end == charpos || (start == charpos && invis != 0))
5780 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5781 && SCHARS (str))
5782 RECORD_OVERLAY_STRING (overlay, str, true);
5785 /* Process overlays after the overlay center. */
5786 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5788 XSETMISC (overlay, ov);
5789 eassert (OVERLAYP (overlay));
5790 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5791 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5793 if (start > charpos)
5794 break;
5796 /* Skip this overlay if it doesn't start or end at IT's current
5797 position. */
5798 if (end != charpos && start != charpos)
5799 continue;
5801 /* Skip this overlay if it doesn't apply to IT->w. */
5802 window = Foverlay_get (overlay, Qwindow);
5803 if (WINDOWP (window) && XWINDOW (window) != it->w)
5804 continue;
5806 /* If the text ``under'' the overlay is invisible, it has a zero
5807 dimension, and both before- and after-strings apply. */
5808 invisible = Foverlay_get (overlay, Qinvisible);
5809 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5811 /* If overlay has a non-empty before-string, record it. */
5812 if ((start == charpos || (end == charpos && invis != 0))
5813 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5814 && SCHARS (str))
5815 RECORD_OVERLAY_STRING (overlay, str, false);
5817 /* If overlay has a non-empty after-string, record it. */
5818 if ((end == charpos || (start == charpos && invis != 0))
5819 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5820 && SCHARS (str))
5821 RECORD_OVERLAY_STRING (overlay, str, true);
5824 #undef RECORD_OVERLAY_STRING
5826 /* Sort entries. */
5827 if (n > 1)
5828 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5830 /* Record number of overlay strings, and where we computed it. */
5831 it->n_overlay_strings = n;
5832 it->overlay_strings_charpos = charpos;
5834 /* IT->current.overlay_string_index is the number of overlay strings
5835 that have already been consumed by IT. Copy some of the
5836 remaining overlay strings to IT->overlay_strings. */
5837 i = 0;
5838 j = it->current.overlay_string_index;
5839 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5841 it->overlay_strings[i] = entries[j].string;
5842 it->string_overlays[i++] = entries[j++].overlay;
5845 CHECK_IT (it);
5846 SAFE_FREE ();
5850 /* Get the first chunk of overlay strings at IT's current buffer
5851 position, or at CHARPOS if that is > 0. Value is true if at
5852 least one overlay string was found. */
5854 static bool
5855 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5857 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5858 process. This fills IT->overlay_strings with strings, and sets
5859 IT->n_overlay_strings to the total number of strings to process.
5860 IT->pos.overlay_string_index has to be set temporarily to zero
5861 because load_overlay_strings needs this; it must be set to -1
5862 when no overlay strings are found because a zero value would
5863 indicate a position in the first overlay string. */
5864 it->current.overlay_string_index = 0;
5865 load_overlay_strings (it, charpos);
5867 /* If we found overlay strings, set up IT to deliver display
5868 elements from the first one. Otherwise set up IT to deliver
5869 from current_buffer. */
5870 if (it->n_overlay_strings)
5872 /* Make sure we know settings in current_buffer, so that we can
5873 restore meaningful values when we're done with the overlay
5874 strings. */
5875 if (compute_stop_p)
5876 compute_stop_pos (it);
5877 eassert (it->face_id >= 0);
5879 /* Save IT's settings. They are restored after all overlay
5880 strings have been processed. */
5881 eassert (!compute_stop_p || it->sp == 0);
5883 /* When called from handle_stop, there might be an empty display
5884 string loaded. In that case, don't bother saving it. But
5885 don't use this optimization with the bidi iterator, since we
5886 need the corresponding pop_it call to resync the bidi
5887 iterator's position with IT's position, after we are done
5888 with the overlay strings. (The corresponding call to pop_it
5889 in case of an empty display string is in
5890 next_overlay_string.) */
5891 if (!(!it->bidi_p
5892 && STRINGP (it->string) && !SCHARS (it->string)))
5893 push_it (it, NULL);
5895 /* Set up IT to deliver display elements from the first overlay
5896 string. */
5897 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5898 it->string = it->overlay_strings[0];
5899 it->from_overlay = Qnil;
5900 it->stop_charpos = 0;
5901 eassert (STRINGP (it->string));
5902 it->end_charpos = SCHARS (it->string);
5903 it->prev_stop = 0;
5904 it->base_level_stop = 0;
5905 it->multibyte_p = STRING_MULTIBYTE (it->string);
5906 it->method = GET_FROM_STRING;
5907 it->from_disp_prop_p = 0;
5909 /* Force paragraph direction to be that of the parent
5910 buffer. */
5911 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5912 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5913 else
5914 it->paragraph_embedding = L2R;
5916 /* Set up the bidi iterator for this overlay string. */
5917 if (it->bidi_p)
5919 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5921 it->bidi_it.string.lstring = it->string;
5922 it->bidi_it.string.s = NULL;
5923 it->bidi_it.string.schars = SCHARS (it->string);
5924 it->bidi_it.string.bufpos = pos;
5925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5926 it->bidi_it.string.unibyte = !it->multibyte_p;
5927 it->bidi_it.w = it->w;
5928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5930 return true;
5933 it->current.overlay_string_index = -1;
5934 return false;
5937 static bool
5938 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5940 it->string = Qnil;
5941 it->method = GET_FROM_BUFFER;
5943 get_overlay_strings_1 (it, charpos, true);
5945 CHECK_IT (it);
5947 /* Value is true if we found at least one overlay string. */
5948 return STRINGP (it->string);
5953 /***********************************************************************
5954 Saving and restoring state
5955 ***********************************************************************/
5957 /* Save current settings of IT on IT->stack. Called, for example,
5958 before setting up IT for an overlay string, to be able to restore
5959 IT's settings to what they were after the overlay string has been
5960 processed. If POSITION is non-NULL, it is the position to save on
5961 the stack instead of IT->position. */
5963 static void
5964 push_it (struct it *it, struct text_pos *position)
5966 struct iterator_stack_entry *p;
5968 eassert (it->sp < IT_STACK_SIZE);
5969 p = it->stack + it->sp;
5971 p->stop_charpos = it->stop_charpos;
5972 p->prev_stop = it->prev_stop;
5973 p->base_level_stop = it->base_level_stop;
5974 p->cmp_it = it->cmp_it;
5975 eassert (it->face_id >= 0);
5976 p->face_id = it->face_id;
5977 p->string = it->string;
5978 p->method = it->method;
5979 p->from_overlay = it->from_overlay;
5980 switch (p->method)
5982 case GET_FROM_IMAGE:
5983 p->u.image.object = it->object;
5984 p->u.image.image_id = it->image_id;
5985 p->u.image.slice = it->slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 p->u.stretch.object = it->object;
5989 break;
5990 case GET_FROM_XWIDGET:
5991 p->u.xwidget.object = it->object;
5992 break;
5993 case GET_FROM_BUFFER:
5994 case GET_FROM_DISPLAY_VECTOR:
5995 case GET_FROM_STRING:
5996 case GET_FROM_C_STRING:
5997 break;
5998 default:
5999 emacs_abort ();
6001 p->position = position ? *position : it->position;
6002 p->current = it->current;
6003 p->end_charpos = it->end_charpos;
6004 p->string_nchars = it->string_nchars;
6005 p->area = it->area;
6006 p->multibyte_p = it->multibyte_p;
6007 p->avoid_cursor_p = it->avoid_cursor_p;
6008 p->space_width = it->space_width;
6009 p->font_height = it->font_height;
6010 p->voffset = it->voffset;
6011 p->string_from_display_prop_p = it->string_from_display_prop_p;
6012 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6013 p->display_ellipsis_p = false;
6014 p->line_wrap = it->line_wrap;
6015 p->bidi_p = it->bidi_p;
6016 p->paragraph_embedding = it->paragraph_embedding;
6017 p->from_disp_prop_p = it->from_disp_prop_p;
6018 ++it->sp;
6020 /* Save the state of the bidi iterator as well. */
6021 if (it->bidi_p)
6022 bidi_push_it (&it->bidi_it);
6025 static void
6026 iterate_out_of_display_property (struct it *it)
6028 bool buffer_p = !STRINGP (it->string);
6029 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6030 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6032 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6034 /* Maybe initialize paragraph direction. If we are at the beginning
6035 of a new paragraph, next_element_from_buffer may not have a
6036 chance to do that. */
6037 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6038 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6039 /* prev_stop can be zero, so check against BEGV as well. */
6040 while (it->bidi_it.charpos >= bob
6041 && it->prev_stop <= it->bidi_it.charpos
6042 && it->bidi_it.charpos < CHARPOS (it->position)
6043 && it->bidi_it.charpos < eob)
6044 bidi_move_to_visually_next (&it->bidi_it);
6045 /* Record the stop_pos we just crossed, for when we cross it
6046 back, maybe. */
6047 if (it->bidi_it.charpos > CHARPOS (it->position))
6048 it->prev_stop = CHARPOS (it->position);
6049 /* If we ended up not where pop_it put us, resync IT's
6050 positional members with the bidi iterator. */
6051 if (it->bidi_it.charpos != CHARPOS (it->position))
6052 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6053 if (buffer_p)
6054 it->current.pos = it->position;
6055 else
6056 it->current.string_pos = it->position;
6059 /* Restore IT's settings from IT->stack. Called, for example, when no
6060 more overlay strings must be processed, and we return to delivering
6061 display elements from a buffer, or when the end of a string from a
6062 `display' property is reached and we return to delivering display
6063 elements from an overlay string, or from a buffer. */
6065 static void
6066 pop_it (struct it *it)
6068 struct iterator_stack_entry *p;
6069 bool from_display_prop = it->from_disp_prop_p;
6070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6072 eassert (it->sp > 0);
6073 --it->sp;
6074 p = it->stack + it->sp;
6075 it->stop_charpos = p->stop_charpos;
6076 it->prev_stop = p->prev_stop;
6077 it->base_level_stop = p->base_level_stop;
6078 it->cmp_it = p->cmp_it;
6079 it->face_id = p->face_id;
6080 it->current = p->current;
6081 it->position = p->position;
6082 it->string = p->string;
6083 it->from_overlay = p->from_overlay;
6084 if (NILP (it->string))
6085 SET_TEXT_POS (it->current.string_pos, -1, -1);
6086 it->method = p->method;
6087 switch (it->method)
6089 case GET_FROM_IMAGE:
6090 it->image_id = p->u.image.image_id;
6091 it->object = p->u.image.object;
6092 it->slice = p->u.image.slice;
6093 break;
6094 case GET_FROM_XWIDGET:
6095 it->object = p->u.xwidget.object;
6096 break;
6097 case GET_FROM_STRETCH:
6098 it->object = p->u.stretch.object;
6099 break;
6100 case GET_FROM_BUFFER:
6101 it->object = it->w->contents;
6102 break;
6103 case GET_FROM_STRING:
6105 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6107 /* Restore the face_box_p flag, since it could have been
6108 overwritten by the face of the object that we just finished
6109 displaying. */
6110 if (face)
6111 it->face_box_p = face->box != FACE_NO_BOX;
6112 it->object = it->string;
6114 break;
6115 case GET_FROM_DISPLAY_VECTOR:
6116 if (it->s)
6117 it->method = GET_FROM_C_STRING;
6118 else if (STRINGP (it->string))
6119 it->method = GET_FROM_STRING;
6120 else
6122 it->method = GET_FROM_BUFFER;
6123 it->object = it->w->contents;
6125 break;
6126 case GET_FROM_C_STRING:
6127 break;
6128 default:
6129 emacs_abort ();
6131 it->end_charpos = p->end_charpos;
6132 it->string_nchars = p->string_nchars;
6133 it->area = p->area;
6134 it->multibyte_p = p->multibyte_p;
6135 it->avoid_cursor_p = p->avoid_cursor_p;
6136 it->space_width = p->space_width;
6137 it->font_height = p->font_height;
6138 it->voffset = p->voffset;
6139 it->string_from_display_prop_p = p->string_from_display_prop_p;
6140 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6141 it->line_wrap = p->line_wrap;
6142 it->bidi_p = p->bidi_p;
6143 it->paragraph_embedding = p->paragraph_embedding;
6144 it->from_disp_prop_p = p->from_disp_prop_p;
6145 if (it->bidi_p)
6147 bidi_pop_it (&it->bidi_it);
6148 /* Bidi-iterate until we get out of the portion of text, if any,
6149 covered by a `display' text property or by an overlay with
6150 `display' property. (We cannot just jump there, because the
6151 internal coherency of the bidi iterator state can not be
6152 preserved across such jumps.) We also must determine the
6153 paragraph base direction if the overlay we just processed is
6154 at the beginning of a new paragraph. */
6155 if (from_display_prop
6156 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6157 iterate_out_of_display_property (it);
6159 eassert ((BUFFERP (it->object)
6160 && IT_CHARPOS (*it) == it->bidi_it.charpos
6161 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6162 || (STRINGP (it->object)
6163 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6164 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6165 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6167 /* If we move the iterator over text covered by a display property
6168 to a new buffer position, any info about previously seen overlays
6169 is no longer valid. */
6170 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6171 it->ignore_overlay_strings_at_pos_p = false;
6176 /***********************************************************************
6177 Moving over lines
6178 ***********************************************************************/
6180 /* Set IT's current position to the previous line start. */
6182 static void
6183 back_to_previous_line_start (struct it *it)
6185 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6187 DEC_BOTH (cp, bp);
6188 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6192 /* Move IT to the next line start.
6194 Value is true if a newline was found. Set *SKIPPED_P to true if
6195 we skipped over part of the text (as opposed to moving the iterator
6196 continuously over the text). Otherwise, don't change the value
6197 of *SKIPPED_P.
6199 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6200 iterator on the newline, if it was found.
6202 Newlines may come from buffer text, overlay strings, or strings
6203 displayed via the `display' property. That's the reason we can't
6204 simply use find_newline_no_quit.
6206 Note that this function may not skip over invisible text that is so
6207 because of text properties and immediately follows a newline. If
6208 it would, function reseat_at_next_visible_line_start, when called
6209 from set_iterator_to_next, would effectively make invisible
6210 characters following a newline part of the wrong glyph row, which
6211 leads to wrong cursor motion. */
6213 static bool
6214 forward_to_next_line_start (struct it *it, bool *skipped_p,
6215 struct bidi_it *bidi_it_prev)
6217 ptrdiff_t old_selective;
6218 bool newline_found_p = false;
6219 int n;
6220 const int MAX_NEWLINE_DISTANCE = 500;
6222 /* If already on a newline, just consume it to avoid unintended
6223 skipping over invisible text below. */
6224 if (it->what == IT_CHARACTER
6225 && it->c == '\n'
6226 && CHARPOS (it->position) == IT_CHARPOS (*it))
6228 if (it->bidi_p && bidi_it_prev)
6229 *bidi_it_prev = it->bidi_it;
6230 set_iterator_to_next (it, false);
6231 it->c = 0;
6232 return true;
6235 /* Don't handle selective display in the following. It's (a)
6236 unnecessary because it's done by the caller, and (b) leads to an
6237 infinite recursion because next_element_from_ellipsis indirectly
6238 calls this function. */
6239 old_selective = it->selective;
6240 it->selective = 0;
6242 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6243 from buffer text. */
6244 for (n = 0;
6245 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6246 n += !STRINGP (it->string))
6248 if (!get_next_display_element (it))
6249 return false;
6250 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6251 if (newline_found_p && it->bidi_p && bidi_it_prev)
6252 *bidi_it_prev = it->bidi_it;
6253 set_iterator_to_next (it, false);
6256 /* If we didn't find a newline near enough, see if we can use a
6257 short-cut. */
6258 if (!newline_found_p)
6260 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6261 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6262 1, &bytepos);
6263 Lisp_Object pos;
6265 eassert (!STRINGP (it->string));
6267 /* If there isn't any `display' property in sight, and no
6268 overlays, we can just use the position of the newline in
6269 buffer text. */
6270 if (it->stop_charpos >= limit
6271 || ((pos = Fnext_single_property_change (make_number (start),
6272 Qdisplay, Qnil,
6273 make_number (limit)),
6274 NILP (pos))
6275 && next_overlay_change (start) == ZV))
6277 if (!it->bidi_p)
6279 IT_CHARPOS (*it) = limit;
6280 IT_BYTEPOS (*it) = bytepos;
6282 else
6284 struct bidi_it bprev;
6286 /* Help bidi.c avoid expensive searches for display
6287 properties and overlays, by telling it that there are
6288 none up to `limit'. */
6289 if (it->bidi_it.disp_pos < limit)
6291 it->bidi_it.disp_pos = limit;
6292 it->bidi_it.disp_prop = 0;
6294 do {
6295 bprev = it->bidi_it;
6296 bidi_move_to_visually_next (&it->bidi_it);
6297 } while (it->bidi_it.charpos != limit);
6298 IT_CHARPOS (*it) = limit;
6299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 if (bidi_it_prev)
6301 *bidi_it_prev = bprev;
6303 *skipped_p = newline_found_p = true;
6305 else
6307 while (!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 specbind (Qinhibit_free_realized_faces, Qnil);
13578 /* Record this function, so it appears on the profiler's backtraces. */
13579 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13581 FOR_EACH_FRAME (tail, frame)
13582 XFRAME (frame)->already_hscrolled_p = false;
13584 retry:
13585 /* Remember the currently selected window. */
13586 sw = w;
13588 pending = false;
13589 forget_escape_and_glyphless_faces ();
13591 inhibit_free_realized_faces = false;
13593 /* If face_change, init_iterator will free all realized faces, which
13594 includes the faces referenced from current matrices. So, we
13595 can't reuse current matrices in this case. */
13596 if (face_change)
13597 windows_or_buffers_changed = 47;
13599 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13600 && FRAME_TTY (sf)->previous_frame != sf)
13602 /* Since frames on a single ASCII terminal share the same
13603 display area, displaying a different frame means redisplay
13604 the whole thing. */
13605 SET_FRAME_GARBAGED (sf);
13606 #ifndef DOS_NT
13607 set_tty_color_mode (FRAME_TTY (sf), sf);
13608 #endif
13609 FRAME_TTY (sf)->previous_frame = sf;
13612 /* Set the visible flags for all frames. Do this before checking for
13613 resized or garbaged frames; they want to know if their frames are
13614 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13615 number_of_visible_frames = 0;
13617 FOR_EACH_FRAME (tail, frame)
13619 struct frame *f = XFRAME (frame);
13621 if (FRAME_VISIBLE_P (f))
13623 ++number_of_visible_frames;
13624 /* Adjust matrices for visible frames only. */
13625 if (f->fonts_changed)
13627 adjust_frame_glyphs (f);
13628 /* Disable all redisplay optimizations for this frame.
13629 This is because adjust_frame_glyphs resets the
13630 enabled_p flag for all glyph rows of all windows, so
13631 many optimizations will fail anyway, and some might
13632 fail to test that flag and do bogus things as
13633 result. */
13634 SET_FRAME_GARBAGED (f);
13635 f->fonts_changed = false;
13637 /* If cursor type has been changed on the frame
13638 other than selected, consider all frames. */
13639 if (f != sf && f->cursor_type_changed)
13640 fset_redisplay (f);
13642 clear_desired_matrices (f);
13645 /* Notice any pending interrupt request to change frame size. */
13646 do_pending_window_change (true);
13648 /* do_pending_window_change could change the selected_window due to
13649 frame resizing which makes the selected window too small. */
13650 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13651 sw = w;
13653 /* Clear frames marked as garbaged. */
13654 clear_garbaged_frames ();
13656 /* Build menubar and tool-bar items. */
13657 if (NILP (Vmemory_full))
13658 prepare_menu_bars ();
13660 reconsider_clip_changes (w);
13662 /* In most cases selected window displays current buffer. */
13663 match_p = XBUFFER (w->contents) == current_buffer;
13664 if (match_p)
13666 /* Detect case that we need to write or remove a star in the mode line. */
13667 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13668 w->update_mode_line = true;
13670 if (mode_line_update_needed (w))
13671 w->update_mode_line = true;
13673 /* If reconsider_clip_changes above decided that the narrowing
13674 in the current buffer changed, make sure all other windows
13675 showing that buffer will be redisplayed. */
13676 if (current_buffer->clip_changed)
13677 bset_update_mode_line (current_buffer);
13680 /* Normally the message* functions will have already displayed and
13681 updated the echo area, but the frame may have been trashed, or
13682 the update may have been preempted, so display the echo area
13683 again here. Checking message_cleared_p captures the case that
13684 the echo area should be cleared. */
13685 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13686 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13687 || (message_cleared_p
13688 && minibuf_level == 0
13689 /* If the mini-window is currently selected, this means the
13690 echo-area doesn't show through. */
13691 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13693 echo_area_display (false);
13695 /* If echo_area_display resizes the mini-window, the redisplay and
13696 window_sizes_changed flags of the selected frame are set, but
13697 it's too late for the hooks in window-size-change-functions,
13698 which have been examined already in prepare_menu_bars. So in
13699 that case we call the hooks here only for the selected frame. */
13700 if (sf->redisplay)
13702 ptrdiff_t count1 = SPECPDL_INDEX ();
13704 record_unwind_save_match_data ();
13705 run_window_size_change_functions (selected_frame);
13706 unbind_to (count1, Qnil);
13709 if (message_cleared_p)
13710 update_miniwindow_p = true;
13712 must_finish = true;
13714 /* If we don't display the current message, don't clear the
13715 message_cleared_p flag, because, if we did, we wouldn't clear
13716 the echo area in the next redisplay which doesn't preserve
13717 the echo area. */
13718 if (!display_last_displayed_message_p)
13719 message_cleared_p = false;
13721 else if (EQ (selected_window, minibuf_window)
13722 && (current_buffer->clip_changed || window_outdated (w))
13723 && resize_mini_window (w, false))
13725 if (sf->redisplay)
13727 ptrdiff_t count1 = SPECPDL_INDEX ();
13729 record_unwind_save_match_data ();
13730 run_window_size_change_functions (selected_frame);
13731 unbind_to (count1, Qnil);
13734 /* Resized active mini-window to fit the size of what it is
13735 showing if its contents might have changed. */
13736 must_finish = true;
13738 /* If window configuration was changed, frames may have been
13739 marked garbaged. Clear them or we will experience
13740 surprises wrt scrolling. */
13741 clear_garbaged_frames ();
13744 if (windows_or_buffers_changed && !update_mode_lines)
13745 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13746 only the windows's contents needs to be refreshed, or whether the
13747 mode-lines also need a refresh. */
13748 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13749 ? REDISPLAY_SOME : 32);
13751 /* If specs for an arrow have changed, do thorough redisplay
13752 to ensure we remove any arrow that should no longer exist. */
13753 if (overlay_arrows_changed_p ())
13754 /* Apparently, this is the only case where we update other windows,
13755 without updating other mode-lines. */
13756 windows_or_buffers_changed = 49;
13758 consider_all_windows_p = (update_mode_lines
13759 || windows_or_buffers_changed);
13761 #define AINC(a,i) \
13763 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13764 if (INTEGERP (entry)) \
13765 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13768 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13769 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13771 /* Optimize the case that only the line containing the cursor in the
13772 selected window has changed. Variables starting with this_ are
13773 set in display_line and record information about the line
13774 containing the cursor. */
13775 tlbufpos = this_line_start_pos;
13776 tlendpos = this_line_end_pos;
13777 if (!consider_all_windows_p
13778 && CHARPOS (tlbufpos) > 0
13779 && !w->update_mode_line
13780 && !current_buffer->clip_changed
13781 && !current_buffer->prevent_redisplay_optimizations_p
13782 && FRAME_VISIBLE_P (XFRAME (w->frame))
13783 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13784 && !XFRAME (w->frame)->cursor_type_changed
13785 && !XFRAME (w->frame)->face_change
13786 /* Make sure recorded data applies to current buffer, etc. */
13787 && this_line_buffer == current_buffer
13788 && match_p
13789 && !w->force_start
13790 && !w->optional_new_start
13791 /* Point must be on the line that we have info recorded about. */
13792 && PT >= CHARPOS (tlbufpos)
13793 && PT <= Z - CHARPOS (tlendpos)
13794 /* All text outside that line, including its final newline,
13795 must be unchanged. */
13796 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13797 CHARPOS (tlendpos)))
13799 if (CHARPOS (tlbufpos) > BEGV
13800 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13801 && (CHARPOS (tlbufpos) == ZV
13802 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13803 /* Former continuation line has disappeared by becoming empty. */
13804 goto cancel;
13805 else if (window_outdated (w) || MINI_WINDOW_P (w))
13807 /* We have to handle the case of continuation around a
13808 wide-column character (see the comment in indent.c around
13809 line 1340).
13811 For instance, in the following case:
13813 -------- Insert --------
13814 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13815 J_I_ ==> J_I_ `^^' are cursors.
13816 ^^ ^^
13817 -------- --------
13819 As we have to redraw the line above, we cannot use this
13820 optimization. */
13822 struct it it;
13823 int line_height_before = this_line_pixel_height;
13825 /* Note that start_display will handle the case that the
13826 line starting at tlbufpos is a continuation line. */
13827 start_display (&it, w, tlbufpos);
13829 /* Implementation note: It this still necessary? */
13830 if (it.current_x != this_line_start_x)
13831 goto cancel;
13833 TRACE ((stderr, "trying display optimization 1\n"));
13834 w->cursor.vpos = -1;
13835 overlay_arrow_seen = false;
13836 it.vpos = this_line_vpos;
13837 it.current_y = this_line_y;
13838 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13839 display_line (&it);
13841 /* If line contains point, is not continued,
13842 and ends at same distance from eob as before, we win. */
13843 if (w->cursor.vpos >= 0
13844 /* Line is not continued, otherwise this_line_start_pos
13845 would have been set to 0 in display_line. */
13846 && CHARPOS (this_line_start_pos)
13847 /* Line ends as before. */
13848 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13849 /* Line has same height as before. Otherwise other lines
13850 would have to be shifted up or down. */
13851 && this_line_pixel_height == line_height_before)
13853 /* If this is not the window's last line, we must adjust
13854 the charstarts of the lines below. */
13855 if (it.current_y < it.last_visible_y)
13857 struct glyph_row *row
13858 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13859 ptrdiff_t delta, delta_bytes;
13861 /* We used to distinguish between two cases here,
13862 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13863 when the line ends in a newline or the end of the
13864 buffer's accessible portion. But both cases did
13865 the same, so they were collapsed. */
13866 delta = (Z
13867 - CHARPOS (tlendpos)
13868 - MATRIX_ROW_START_CHARPOS (row));
13869 delta_bytes = (Z_BYTE
13870 - BYTEPOS (tlendpos)
13871 - MATRIX_ROW_START_BYTEPOS (row));
13873 increment_matrix_positions (w->current_matrix,
13874 this_line_vpos + 1,
13875 w->current_matrix->nrows,
13876 delta, delta_bytes);
13879 /* If this row displays text now but previously didn't,
13880 or vice versa, w->window_end_vpos may have to be
13881 adjusted. */
13882 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13884 if (w->window_end_vpos < this_line_vpos)
13885 w->window_end_vpos = this_line_vpos;
13887 else if (w->window_end_vpos == this_line_vpos
13888 && this_line_vpos > 0)
13889 w->window_end_vpos = this_line_vpos - 1;
13890 w->window_end_valid = false;
13892 /* Update hint: No need to try to scroll in update_window. */
13893 w->desired_matrix->no_scrolling_p = true;
13895 #ifdef GLYPH_DEBUG
13896 *w->desired_matrix->method = 0;
13897 debug_method_add (w, "optimization 1");
13898 #endif
13899 #ifdef HAVE_WINDOW_SYSTEM
13900 update_window_fringes (w, false);
13901 #endif
13902 goto update;
13904 else
13905 goto cancel;
13907 else if (/* Cursor position hasn't changed. */
13908 PT == w->last_point
13909 /* Make sure the cursor was last displayed
13910 in this window. Otherwise we have to reposition it. */
13912 /* PXW: Must be converted to pixels, probably. */
13913 && 0 <= w->cursor.vpos
13914 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13916 if (!must_finish)
13918 do_pending_window_change (true);
13919 /* If selected_window changed, redisplay again. */
13920 if (WINDOWP (selected_window)
13921 && (w = XWINDOW (selected_window)) != sw)
13922 goto retry;
13924 /* We used to always goto end_of_redisplay here, but this
13925 isn't enough if we have a blinking cursor. */
13926 if (w->cursor_off_p == w->last_cursor_off_p)
13927 goto end_of_redisplay;
13929 goto update;
13931 /* If highlighting the region, or if the cursor is in the echo area,
13932 then we can't just move the cursor. */
13933 else if (NILP (Vshow_trailing_whitespace)
13934 && !cursor_in_echo_area)
13936 struct it it;
13937 struct glyph_row *row;
13939 /* Skip from tlbufpos to PT and see where it is. Note that
13940 PT may be in invisible text. If so, we will end at the
13941 next visible position. */
13942 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13943 NULL, DEFAULT_FACE_ID);
13944 it.current_x = this_line_start_x;
13945 it.current_y = this_line_y;
13946 it.vpos = this_line_vpos;
13948 /* The call to move_it_to stops in front of PT, but
13949 moves over before-strings. */
13950 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13952 if (it.vpos == this_line_vpos
13953 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13954 row->enabled_p))
13956 eassert (this_line_vpos == it.vpos);
13957 eassert (this_line_y == it.current_y);
13958 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13959 if (cursor_row_fully_visible_p (w, false, true))
13961 #ifdef GLYPH_DEBUG
13962 *w->desired_matrix->method = 0;
13963 debug_method_add (w, "optimization 3");
13964 #endif
13965 goto update;
13967 else
13968 goto cancel;
13970 else
13971 goto cancel;
13974 cancel:
13975 /* Text changed drastically or point moved off of line. */
13976 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13979 CHARPOS (this_line_start_pos) = 0;
13980 ++clear_face_cache_count;
13981 #ifdef HAVE_WINDOW_SYSTEM
13982 ++clear_image_cache_count;
13983 #endif
13985 /* Build desired matrices, and update the display. If
13986 consider_all_windows_p, do it for all windows on all frames that
13987 require redisplay, as specified by their 'redisplay' flag.
13988 Otherwise do it for selected_window, only. */
13990 if (consider_all_windows_p)
13992 FOR_EACH_FRAME (tail, frame)
13993 XFRAME (frame)->updated_p = false;
13995 propagate_buffer_redisplay ();
13997 FOR_EACH_FRAME (tail, frame)
13999 struct frame *f = XFRAME (frame);
14001 /* We don't have to do anything for unselected terminal
14002 frames. */
14003 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14004 && !EQ (FRAME_TTY (f)->top_frame, frame))
14005 continue;
14007 retry_frame:
14008 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14010 bool gcscrollbars
14011 /* Only GC scrollbars when we redisplay the whole frame. */
14012 = f->redisplay || !REDISPLAY_SOME_P ();
14013 bool f_redisplay_flag = f->redisplay;
14014 /* Mark all the scroll bars to be removed; we'll redeem
14015 the ones we want when we redisplay their windows. */
14016 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14017 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14019 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14020 redisplay_windows (FRAME_ROOT_WINDOW (f));
14021 /* Remember that the invisible frames need to be redisplayed next
14022 time they're visible. */
14023 else if (!REDISPLAY_SOME_P ())
14024 f->redisplay = true;
14026 /* The X error handler may have deleted that frame. */
14027 if (!FRAME_LIVE_P (f))
14028 continue;
14030 /* Any scroll bars which redisplay_windows should have
14031 nuked should now go away. */
14032 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14033 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14035 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14037 /* If fonts changed on visible frame, display again. */
14038 if (f->fonts_changed)
14040 adjust_frame_glyphs (f);
14041 /* Disable all redisplay optimizations for this
14042 frame. For the reasons, see the comment near
14043 the previous call to adjust_frame_glyphs above. */
14044 SET_FRAME_GARBAGED (f);
14045 f->fonts_changed = false;
14046 goto retry_frame;
14049 /* See if we have to hscroll. */
14050 if (!f->already_hscrolled_p)
14052 f->already_hscrolled_p = true;
14053 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14054 && hscroll_windows (f->root_window))
14056 hscroll_retries++;
14057 goto retry_frame;
14061 /* If the frame's redisplay flag was not set before
14062 we went about redisplaying its windows, but it is
14063 set now, that means we employed some redisplay
14064 optimizations inside redisplay_windows, and
14065 bypassed producing some screen lines. But if
14066 f->redisplay is now set, it might mean the old
14067 faces are no longer valid (e.g., if redisplaying
14068 some window called some Lisp which defined a new
14069 face or redefined an existing face), so trying to
14070 use them in update_frame will segfault.
14071 Therefore, we must redisplay this frame. */
14072 if (!f_redisplay_flag && f->redisplay)
14073 goto retry_frame;
14075 /* Prevent various kinds of signals during display
14076 update. stdio is not robust about handling
14077 signals, which can cause an apparent I/O error. */
14078 if (interrupt_input)
14079 unrequest_sigio ();
14080 STOP_POLLING;
14082 pending |= update_frame (f, false, false);
14083 f->cursor_type_changed = false;
14084 f->updated_p = true;
14089 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14091 if (!pending)
14093 /* Do the mark_window_display_accurate after all windows have
14094 been redisplayed because this call resets flags in buffers
14095 which are needed for proper redisplay. */
14096 FOR_EACH_FRAME (tail, frame)
14098 struct frame *f = XFRAME (frame);
14099 if (f->updated_p)
14101 f->redisplay = false;
14102 mark_window_display_accurate (f->root_window, true);
14103 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14104 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14109 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14111 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14112 /* Use list_of_error, not Qerror, so that
14113 we catch only errors and don't run the debugger. */
14114 internal_condition_case_1 (redisplay_window_1, selected_window,
14115 list_of_error,
14116 redisplay_window_error);
14117 if (update_miniwindow_p)
14118 internal_condition_case_1 (redisplay_window_1,
14119 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14120 redisplay_window_error);
14122 /* Compare desired and current matrices, perform output. */
14124 update:
14125 /* If fonts changed, display again. Likewise if redisplay_window_1
14126 above caused some change (e.g., a change in faces) that requires
14127 considering the entire frame again. */
14128 if (sf->fonts_changed || sf->redisplay)
14130 if (sf->redisplay)
14132 /* Set this to force a more thorough redisplay.
14133 Otherwise, we might immediately loop back to the
14134 above "else-if" clause (since all the conditions that
14135 led here might still be true), and we will then
14136 infloop, because the selected-frame's redisplay flag
14137 is not (and cannot be) reset. */
14138 windows_or_buffers_changed = 50;
14140 goto retry;
14143 /* Prevent freeing of realized faces, since desired matrices are
14144 pending that reference the faces we computed and cached. */
14145 inhibit_free_realized_faces = true;
14147 /* Prevent various kinds of signals during display update.
14148 stdio is not robust about handling signals,
14149 which can cause an apparent I/O error. */
14150 if (interrupt_input)
14151 unrequest_sigio ();
14152 STOP_POLLING;
14154 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14156 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14157 && hscroll_windows (selected_window))
14159 hscroll_retries++;
14160 goto retry;
14163 XWINDOW (selected_window)->must_be_updated_p = true;
14164 pending = update_frame (sf, false, false);
14165 sf->cursor_type_changed = false;
14168 /* We may have called echo_area_display at the top of this
14169 function. If the echo area is on another frame, that may
14170 have put text on a frame other than the selected one, so the
14171 above call to update_frame would not have caught it. Catch
14172 it here. */
14173 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14174 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14176 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14178 XWINDOW (mini_window)->must_be_updated_p = true;
14179 pending |= update_frame (mini_frame, false, false);
14180 mini_frame->cursor_type_changed = false;
14181 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14182 && hscroll_windows (mini_window))
14184 hscroll_retries++;
14185 goto retry;
14190 /* If display was paused because of pending input, make sure we do a
14191 thorough update the next time. */
14192 if (pending)
14194 /* Prevent the optimization at the beginning of
14195 redisplay_internal that tries a single-line update of the
14196 line containing the cursor in the selected window. */
14197 CHARPOS (this_line_start_pos) = 0;
14199 /* Let the overlay arrow be updated the next time. */
14200 update_overlay_arrows (0);
14202 /* If we pause after scrolling, some rows in the current
14203 matrices of some windows are not valid. */
14204 if (!WINDOW_FULL_WIDTH_P (w)
14205 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14206 update_mode_lines = 36;
14208 else
14210 if (!consider_all_windows_p)
14212 /* This has already been done above if
14213 consider_all_windows_p is set. */
14214 if (XBUFFER (w->contents)->text->redisplay
14215 && buffer_window_count (XBUFFER (w->contents)) > 1)
14216 /* This can happen if b->text->redisplay was set during
14217 jit-lock. */
14218 propagate_buffer_redisplay ();
14219 mark_window_display_accurate_1 (w, true);
14221 /* Say overlay arrows are up to date. */
14222 update_overlay_arrows (1);
14224 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14225 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14228 update_mode_lines = 0;
14229 windows_or_buffers_changed = 0;
14232 /* Start SIGIO interrupts coming again. Having them off during the
14233 code above makes it less likely one will discard output, but not
14234 impossible, since there might be stuff in the system buffer here.
14235 But it is much hairier to try to do anything about that. */
14236 if (interrupt_input)
14237 request_sigio ();
14238 RESUME_POLLING;
14240 /* If a frame has become visible which was not before, redisplay
14241 again, so that we display it. Expose events for such a frame
14242 (which it gets when becoming visible) don't call the parts of
14243 redisplay constructing glyphs, so simply exposing a frame won't
14244 display anything in this case. So, we have to display these
14245 frames here explicitly. */
14246 if (!pending)
14248 int new_count = 0;
14250 FOR_EACH_FRAME (tail, frame)
14252 if (XFRAME (frame)->visible)
14253 new_count++;
14256 if (new_count != number_of_visible_frames)
14257 windows_or_buffers_changed = 52;
14260 /* Change frame size now if a change is pending. */
14261 do_pending_window_change (true);
14263 /* If we just did a pending size change, or have additional
14264 visible frames, or selected_window changed, redisplay again. */
14265 if ((windows_or_buffers_changed && !pending)
14266 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14267 goto retry;
14269 /* Clear the face and image caches.
14271 We used to do this only if consider_all_windows_p. But the cache
14272 needs to be cleared if a timer creates images in the current
14273 buffer (e.g. the test case in Bug#6230). */
14275 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14277 clear_face_cache (false);
14278 clear_face_cache_count = 0;
14281 #ifdef HAVE_WINDOW_SYSTEM
14282 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14284 clear_image_caches (Qnil);
14285 clear_image_cache_count = 0;
14287 #endif /* HAVE_WINDOW_SYSTEM */
14289 end_of_redisplay:
14290 #ifdef HAVE_NS
14291 ns_set_doc_edited ();
14292 #endif
14293 if (interrupt_input && interrupts_deferred)
14294 request_sigio ();
14296 unbind_to (count, Qnil);
14297 RESUME_POLLING;
14301 /* Redisplay, but leave alone any recent echo area message unless
14302 another message has been requested in its place.
14304 This is useful in situations where you need to redisplay but no
14305 user action has occurred, making it inappropriate for the message
14306 area to be cleared. See tracking_off and
14307 wait_reading_process_output for examples of these situations.
14309 FROM_WHERE is an integer saying from where this function was
14310 called. This is useful for debugging. */
14312 void
14313 redisplay_preserve_echo_area (int from_where)
14315 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14317 if (!NILP (echo_area_buffer[1]))
14319 /* We have a previously displayed message, but no current
14320 message. Redisplay the previous message. */
14321 display_last_displayed_message_p = true;
14322 redisplay_internal ();
14323 display_last_displayed_message_p = false;
14325 else
14326 redisplay_internal ();
14328 flush_frame (SELECTED_FRAME ());
14332 /* Function registered with record_unwind_protect in redisplay_internal. */
14334 static void
14335 unwind_redisplay (void)
14337 redisplaying_p = false;
14341 /* Mark the display of leaf window W as accurate or inaccurate.
14342 If ACCURATE_P, mark display of W as accurate.
14343 If !ACCURATE_P, arrange for W to be redisplayed the next
14344 time redisplay_internal is called. */
14346 static void
14347 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14349 struct buffer *b = XBUFFER (w->contents);
14351 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14352 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14353 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14355 if (accurate_p)
14357 b->clip_changed = false;
14358 b->prevent_redisplay_optimizations_p = false;
14359 eassert (buffer_window_count (b) > 0);
14360 /* Resetting b->text->redisplay is problematic!
14361 In order to make it safer to do it here, redisplay_internal must
14362 have copied all b->text->redisplay to their respective windows. */
14363 b->text->redisplay = false;
14365 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14366 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14367 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14368 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14370 w->current_matrix->buffer = b;
14371 w->current_matrix->begv = BUF_BEGV (b);
14372 w->current_matrix->zv = BUF_ZV (b);
14374 w->last_cursor_vpos = w->cursor.vpos;
14375 w->last_cursor_off_p = w->cursor_off_p;
14377 if (w == XWINDOW (selected_window))
14378 w->last_point = BUF_PT (b);
14379 else
14380 w->last_point = marker_position (w->pointm);
14382 w->window_end_valid = true;
14383 w->update_mode_line = false;
14386 w->redisplay = !accurate_p;
14390 /* Mark the display of windows in the window tree rooted at WINDOW as
14391 accurate or inaccurate. If ACCURATE_P, mark display of
14392 windows as accurate. If !ACCURATE_P, arrange for windows to
14393 be redisplayed the next time redisplay_internal is called. */
14395 void
14396 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14398 struct window *w;
14400 for (; !NILP (window); window = w->next)
14402 w = XWINDOW (window);
14403 if (WINDOWP (w->contents))
14404 mark_window_display_accurate (w->contents, accurate_p);
14405 else
14406 mark_window_display_accurate_1 (w, accurate_p);
14409 if (accurate_p)
14410 update_overlay_arrows (1);
14411 else
14412 /* Force a thorough redisplay the next time by setting
14413 last_arrow_position and last_arrow_string to t, which is
14414 unequal to any useful value of Voverlay_arrow_... */
14415 update_overlay_arrows (-1);
14419 /* Return value in display table DP (Lisp_Char_Table *) for character
14420 C. Since a display table doesn't have any parent, we don't have to
14421 follow parent. Do not call this function directly but use the
14422 macro DISP_CHAR_VECTOR. */
14424 Lisp_Object
14425 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14427 Lisp_Object val;
14429 if (ASCII_CHAR_P (c))
14431 val = dp->ascii;
14432 if (SUB_CHAR_TABLE_P (val))
14433 val = XSUB_CHAR_TABLE (val)->contents[c];
14435 else
14437 Lisp_Object table;
14439 XSETCHAR_TABLE (table, dp);
14440 val = char_table_ref (table, c);
14442 if (NILP (val))
14443 val = dp->defalt;
14444 return val;
14449 /***********************************************************************
14450 Window Redisplay
14451 ***********************************************************************/
14453 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14455 static void
14456 redisplay_windows (Lisp_Object window)
14458 while (!NILP (window))
14460 struct window *w = XWINDOW (window);
14462 if (WINDOWP (w->contents))
14463 redisplay_windows (w->contents);
14464 else if (BUFFERP (w->contents))
14466 displayed_buffer = XBUFFER (w->contents);
14467 /* Use list_of_error, not Qerror, so that
14468 we catch only errors and don't run the debugger. */
14469 internal_condition_case_1 (redisplay_window_0, window,
14470 list_of_error,
14471 redisplay_window_error);
14474 window = w->next;
14478 static Lisp_Object
14479 redisplay_window_error (Lisp_Object ignore)
14481 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14482 return Qnil;
14485 static Lisp_Object
14486 redisplay_window_0 (Lisp_Object window)
14488 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14489 redisplay_window (window, false);
14490 return Qnil;
14493 static Lisp_Object
14494 redisplay_window_1 (Lisp_Object window)
14496 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14497 redisplay_window (window, true);
14498 return Qnil;
14502 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14503 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14504 which positions recorded in ROW differ from current buffer
14505 positions.
14507 Return true iff cursor is on this row. */
14509 static bool
14510 set_cursor_from_row (struct window *w, struct glyph_row *row,
14511 struct glyph_matrix *matrix,
14512 ptrdiff_t delta, ptrdiff_t delta_bytes,
14513 int dy, int dvpos)
14515 struct glyph *glyph = row->glyphs[TEXT_AREA];
14516 struct glyph *end = glyph + row->used[TEXT_AREA];
14517 struct glyph *cursor = NULL;
14518 /* The last known character position in row. */
14519 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14520 int x = row->x;
14521 ptrdiff_t pt_old = PT - delta;
14522 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14523 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14524 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14525 /* A glyph beyond the edge of TEXT_AREA which we should never
14526 touch. */
14527 struct glyph *glyphs_end = end;
14528 /* True means we've found a match for cursor position, but that
14529 glyph has the avoid_cursor_p flag set. */
14530 bool match_with_avoid_cursor = false;
14531 /* True means we've seen at least one glyph that came from a
14532 display string. */
14533 bool string_seen = false;
14534 /* Largest and smallest buffer positions seen so far during scan of
14535 glyph row. */
14536 ptrdiff_t bpos_max = pos_before;
14537 ptrdiff_t bpos_min = pos_after;
14538 /* Last buffer position covered by an overlay string with an integer
14539 `cursor' property. */
14540 ptrdiff_t bpos_covered = 0;
14541 /* True means the display string on which to display the cursor
14542 comes from a text property, not from an overlay. */
14543 bool string_from_text_prop = false;
14545 /* Don't even try doing anything if called for a mode-line or
14546 header-line row, since the rest of the code isn't prepared to
14547 deal with such calamities. */
14548 eassert (!row->mode_line_p);
14549 if (row->mode_line_p)
14550 return false;
14552 /* Skip over glyphs not having an object at the start and the end of
14553 the row. These are special glyphs like truncation marks on
14554 terminal frames. */
14555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14557 if (!row->reversed_p)
14559 while (glyph < end
14560 && NILP (glyph->object)
14561 && glyph->charpos < 0)
14563 x += glyph->pixel_width;
14564 ++glyph;
14566 while (end > glyph
14567 && NILP ((end - 1)->object)
14568 /* CHARPOS is zero for blanks and stretch glyphs
14569 inserted by extend_face_to_end_of_line. */
14570 && (end - 1)->charpos <= 0)
14571 --end;
14572 glyph_before = glyph - 1;
14573 glyph_after = end;
14575 else
14577 struct glyph *g;
14579 /* If the glyph row is reversed, we need to process it from back
14580 to front, so swap the edge pointers. */
14581 glyphs_end = end = glyph - 1;
14582 glyph += row->used[TEXT_AREA] - 1;
14584 while (glyph > end + 1
14585 && NILP (glyph->object)
14586 && glyph->charpos < 0)
14588 --glyph;
14589 x -= glyph->pixel_width;
14591 if (NILP (glyph->object) && glyph->charpos < 0)
14592 --glyph;
14593 /* By default, in reversed rows we put the cursor on the
14594 rightmost (first in the reading order) glyph. */
14595 for (g = end + 1; g < glyph; g++)
14596 x += g->pixel_width;
14597 while (end < glyph
14598 && NILP ((end + 1)->object)
14599 && (end + 1)->charpos <= 0)
14600 ++end;
14601 glyph_before = glyph + 1;
14602 glyph_after = end;
14605 else if (row->reversed_p)
14607 /* In R2L rows that don't display text, put the cursor on the
14608 rightmost glyph. Case in point: an empty last line that is
14609 part of an R2L paragraph. */
14610 cursor = end - 1;
14611 /* Avoid placing the cursor on the last glyph of the row, where
14612 on terminal frames we hold the vertical border between
14613 adjacent windows. */
14614 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14615 && !WINDOW_RIGHTMOST_P (w)
14616 && cursor == row->glyphs[LAST_AREA] - 1)
14617 cursor--;
14618 x = -1; /* will be computed below, at label compute_x */
14621 /* Step 1: Try to find the glyph whose character position
14622 corresponds to point. If that's not possible, find 2 glyphs
14623 whose character positions are the closest to point, one before
14624 point, the other after it. */
14625 if (!row->reversed_p)
14626 while (/* not marched to end of glyph row */
14627 glyph < end
14628 /* glyph was not inserted by redisplay for internal purposes */
14629 && !NILP (glyph->object))
14631 if (BUFFERP (glyph->object))
14633 ptrdiff_t dpos = glyph->charpos - pt_old;
14635 if (glyph->charpos > bpos_max)
14636 bpos_max = glyph->charpos;
14637 if (glyph->charpos < bpos_min)
14638 bpos_min = glyph->charpos;
14639 if (!glyph->avoid_cursor_p)
14641 /* If we hit point, we've found the glyph on which to
14642 display the cursor. */
14643 if (dpos == 0)
14645 match_with_avoid_cursor = false;
14646 break;
14648 /* See if we've found a better approximation to
14649 POS_BEFORE or to POS_AFTER. */
14650 if (0 > dpos && dpos > pos_before - pt_old)
14652 pos_before = glyph->charpos;
14653 glyph_before = glyph;
14655 else if (0 < dpos && dpos < pos_after - pt_old)
14657 pos_after = glyph->charpos;
14658 glyph_after = glyph;
14661 else if (dpos == 0)
14662 match_with_avoid_cursor = true;
14664 else if (STRINGP (glyph->object))
14666 Lisp_Object chprop;
14667 ptrdiff_t glyph_pos = glyph->charpos;
14669 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14670 glyph->object);
14671 if (!NILP (chprop))
14673 /* If the string came from a `display' text property,
14674 look up the buffer position of that property and
14675 use that position to update bpos_max, as if we
14676 actually saw such a position in one of the row's
14677 glyphs. This helps with supporting integer values
14678 of `cursor' property on the display string in
14679 situations where most or all of the row's buffer
14680 text is completely covered by display properties,
14681 so that no glyph with valid buffer positions is
14682 ever seen in the row. */
14683 ptrdiff_t prop_pos =
14684 string_buffer_position_lim (glyph->object, pos_before,
14685 pos_after, false);
14687 if (prop_pos >= pos_before)
14688 bpos_max = prop_pos;
14690 if (INTEGERP (chprop))
14692 bpos_covered = bpos_max + XINT (chprop);
14693 /* If the `cursor' property covers buffer positions up
14694 to and including point, we should display cursor on
14695 this glyph. Note that, if a `cursor' property on one
14696 of the string's characters has an integer value, we
14697 will break out of the loop below _before_ we get to
14698 the position match above. IOW, integer values of
14699 the `cursor' property override the "exact match for
14700 point" strategy of positioning the cursor. */
14701 /* Implementation note: bpos_max == pt_old when, e.g.,
14702 we are in an empty line, where bpos_max is set to
14703 MATRIX_ROW_START_CHARPOS, see above. */
14704 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14706 cursor = glyph;
14707 break;
14711 string_seen = true;
14713 x += glyph->pixel_width;
14714 ++glyph;
14716 else if (glyph > end) /* row is reversed */
14717 while (!NILP (glyph->object))
14719 if (BUFFERP (glyph->object))
14721 ptrdiff_t dpos = glyph->charpos - pt_old;
14723 if (glyph->charpos > bpos_max)
14724 bpos_max = glyph->charpos;
14725 if (glyph->charpos < bpos_min)
14726 bpos_min = glyph->charpos;
14727 if (!glyph->avoid_cursor_p)
14729 if (dpos == 0)
14731 match_with_avoid_cursor = false;
14732 break;
14734 if (0 > dpos && dpos > pos_before - pt_old)
14736 pos_before = glyph->charpos;
14737 glyph_before = glyph;
14739 else if (0 < dpos && dpos < pos_after - pt_old)
14741 pos_after = glyph->charpos;
14742 glyph_after = glyph;
14745 else if (dpos == 0)
14746 match_with_avoid_cursor = true;
14748 else if (STRINGP (glyph->object))
14750 Lisp_Object chprop;
14751 ptrdiff_t glyph_pos = glyph->charpos;
14753 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14754 glyph->object);
14755 if (!NILP (chprop))
14757 ptrdiff_t prop_pos =
14758 string_buffer_position_lim (glyph->object, pos_before,
14759 pos_after, false);
14761 if (prop_pos >= pos_before)
14762 bpos_max = prop_pos;
14764 if (INTEGERP (chprop))
14766 bpos_covered = bpos_max + XINT (chprop);
14767 /* If the `cursor' property covers buffer positions up
14768 to and including point, we should display cursor on
14769 this glyph. */
14770 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14772 cursor = glyph;
14773 break;
14776 string_seen = true;
14778 --glyph;
14779 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14781 x--; /* can't use any pixel_width */
14782 break;
14784 x -= glyph->pixel_width;
14787 /* Step 2: If we didn't find an exact match for point, we need to
14788 look for a proper place to put the cursor among glyphs between
14789 GLYPH_BEFORE and GLYPH_AFTER. */
14790 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14791 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14792 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14794 /* An empty line has a single glyph whose OBJECT is nil and
14795 whose CHARPOS is the position of a newline on that line.
14796 Note that on a TTY, there are more glyphs after that, which
14797 were produced by extend_face_to_end_of_line, but their
14798 CHARPOS is zero or negative. */
14799 bool empty_line_p =
14800 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14801 && NILP (glyph->object) && glyph->charpos > 0
14802 /* On a TTY, continued and truncated rows also have a glyph at
14803 their end whose OBJECT is nil and whose CHARPOS is
14804 positive (the continuation and truncation glyphs), but such
14805 rows are obviously not "empty". */
14806 && !(row->continued_p || row->truncated_on_right_p));
14808 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14810 ptrdiff_t ellipsis_pos;
14812 /* Scan back over the ellipsis glyphs. */
14813 if (!row->reversed_p)
14815 ellipsis_pos = (glyph - 1)->charpos;
14816 while (glyph > row->glyphs[TEXT_AREA]
14817 && (glyph - 1)->charpos == ellipsis_pos)
14818 glyph--, x -= glyph->pixel_width;
14819 /* That loop always goes one position too far, including
14820 the glyph before the ellipsis. So scan forward over
14821 that one. */
14822 x += glyph->pixel_width;
14823 glyph++;
14825 else /* row is reversed */
14827 ellipsis_pos = (glyph + 1)->charpos;
14828 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14829 && (glyph + 1)->charpos == ellipsis_pos)
14830 glyph++, x += glyph->pixel_width;
14831 x -= glyph->pixel_width;
14832 glyph--;
14835 else if (match_with_avoid_cursor)
14837 cursor = glyph_after;
14838 x = -1;
14840 else if (string_seen)
14842 int incr = row->reversed_p ? -1 : +1;
14844 /* Need to find the glyph that came out of a string which is
14845 present at point. That glyph is somewhere between
14846 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14847 positioned between POS_BEFORE and POS_AFTER in the
14848 buffer. */
14849 struct glyph *start, *stop;
14850 ptrdiff_t pos = pos_before;
14852 x = -1;
14854 /* If the row ends in a newline from a display string,
14855 reordering could have moved the glyphs belonging to the
14856 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14857 in this case we extend the search to the last glyph in
14858 the row that was not inserted by redisplay. */
14859 if (row->ends_in_newline_from_string_p)
14861 glyph_after = end;
14862 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14865 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14866 correspond to POS_BEFORE and POS_AFTER, respectively. We
14867 need START and STOP in the order that corresponds to the
14868 row's direction as given by its reversed_p flag. If the
14869 directionality of characters between POS_BEFORE and
14870 POS_AFTER is the opposite of the row's base direction,
14871 these characters will have been reordered for display,
14872 and we need to reverse START and STOP. */
14873 if (!row->reversed_p)
14875 start = min (glyph_before, glyph_after);
14876 stop = max (glyph_before, glyph_after);
14878 else
14880 start = max (glyph_before, glyph_after);
14881 stop = min (glyph_before, glyph_after);
14883 for (glyph = start + incr;
14884 row->reversed_p ? glyph > stop : glyph < stop; )
14887 /* Any glyphs that come from the buffer are here because
14888 of bidi reordering. Skip them, and only pay
14889 attention to glyphs that came from some string. */
14890 if (STRINGP (glyph->object))
14892 Lisp_Object str;
14893 ptrdiff_t tem;
14894 /* If the display property covers the newline, we
14895 need to search for it one position farther. */
14896 ptrdiff_t lim = pos_after
14897 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14899 string_from_text_prop = false;
14900 str = glyph->object;
14901 tem = string_buffer_position_lim (str, pos, lim, false);
14902 if (tem == 0 /* from overlay */
14903 || pos <= tem)
14905 /* If the string from which this glyph came is
14906 found in the buffer at point, or at position
14907 that is closer to point than pos_after, then
14908 we've found the glyph we've been looking for.
14909 If it comes from an overlay (tem == 0), and
14910 it has the `cursor' property on one of its
14911 glyphs, record that glyph as a candidate for
14912 displaying the cursor. (As in the
14913 unidirectional version, we will display the
14914 cursor on the last candidate we find.) */
14915 if (tem == 0
14916 || tem == pt_old
14917 || (tem - pt_old > 0 && tem < pos_after))
14919 /* The glyphs from this string could have
14920 been reordered. Find the one with the
14921 smallest string position. Or there could
14922 be a character in the string with the
14923 `cursor' property, which means display
14924 cursor on that character's glyph. */
14925 ptrdiff_t strpos = glyph->charpos;
14927 if (tem)
14929 cursor = glyph;
14930 string_from_text_prop = true;
14932 for ( ;
14933 (row->reversed_p ? glyph > stop : glyph < stop)
14934 && EQ (glyph->object, str);
14935 glyph += incr)
14937 Lisp_Object cprop;
14938 ptrdiff_t gpos = glyph->charpos;
14940 cprop = Fget_char_property (make_number (gpos),
14941 Qcursor,
14942 glyph->object);
14943 if (!NILP (cprop))
14945 cursor = glyph;
14946 break;
14948 if (tem && glyph->charpos < strpos)
14950 strpos = glyph->charpos;
14951 cursor = glyph;
14955 if (tem == pt_old
14956 || (tem - pt_old > 0 && tem < pos_after))
14957 goto compute_x;
14959 if (tem)
14960 pos = tem + 1; /* don't find previous instances */
14962 /* This string is not what we want; skip all of the
14963 glyphs that came from it. */
14964 while ((row->reversed_p ? glyph > stop : glyph < stop)
14965 && EQ (glyph->object, str))
14966 glyph += incr;
14968 else
14969 glyph += incr;
14972 /* If we reached the end of the line, and END was from a string,
14973 the cursor is not on this line. */
14974 if (cursor == NULL
14975 && (row->reversed_p ? glyph <= end : glyph >= end)
14976 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14977 && STRINGP (end->object)
14978 && row->continued_p)
14979 return false;
14981 /* A truncated row may not include PT among its character positions.
14982 Setting the cursor inside the scroll margin will trigger
14983 recalculation of hscroll in hscroll_window_tree. But if a
14984 display string covers point, defer to the string-handling
14985 code below to figure this out. */
14986 else if (row->truncated_on_left_p && pt_old < bpos_min)
14988 cursor = glyph_before;
14989 x = -1;
14991 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14992 /* Zero-width characters produce no glyphs. */
14993 || (!empty_line_p
14994 && (row->reversed_p
14995 ? glyph_after > glyphs_end
14996 : glyph_after < glyphs_end)))
14998 cursor = glyph_after;
14999 x = -1;
15003 compute_x:
15004 if (cursor != NULL)
15005 glyph = cursor;
15006 else if (glyph == glyphs_end
15007 && pos_before == pos_after
15008 && STRINGP ((row->reversed_p
15009 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15010 : row->glyphs[TEXT_AREA])->object))
15012 /* If all the glyphs of this row came from strings, put the
15013 cursor on the first glyph of the row. This avoids having the
15014 cursor outside of the text area in this very rare and hard
15015 use case. */
15016 glyph =
15017 row->reversed_p
15018 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15019 : row->glyphs[TEXT_AREA];
15021 if (x < 0)
15023 struct glyph *g;
15025 /* Need to compute x that corresponds to GLYPH. */
15026 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15028 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15029 emacs_abort ();
15030 x += g->pixel_width;
15034 /* ROW could be part of a continued line, which, under bidi
15035 reordering, might have other rows whose start and end charpos
15036 occlude point. Only set w->cursor if we found a better
15037 approximation to the cursor position than we have from previously
15038 examined candidate rows belonging to the same continued line. */
15039 if (/* We already have a candidate row. */
15040 w->cursor.vpos >= 0
15041 /* That candidate is not the row we are processing. */
15042 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15043 /* Make sure cursor.vpos specifies a row whose start and end
15044 charpos occlude point, and it is valid candidate for being a
15045 cursor-row. This is because some callers of this function
15046 leave cursor.vpos at the row where the cursor was displayed
15047 during the last redisplay cycle. */
15048 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15049 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15050 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15052 struct glyph *g1
15053 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15055 /* Don't consider glyphs that are outside TEXT_AREA. */
15056 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15057 return false;
15058 /* Keep the candidate whose buffer position is the closest to
15059 point or has the `cursor' property. */
15060 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15061 w->cursor.hpos >= 0
15062 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15063 && ((BUFFERP (g1->object)
15064 && (g1->charpos == pt_old /* An exact match always wins. */
15065 || (BUFFERP (glyph->object)
15066 && eabs (g1->charpos - pt_old)
15067 < eabs (glyph->charpos - pt_old))))
15068 /* Previous candidate is a glyph from a string that has
15069 a non-nil `cursor' property. */
15070 || (STRINGP (g1->object)
15071 && (!NILP (Fget_char_property (make_number (g1->charpos),
15072 Qcursor, g1->object))
15073 /* Previous candidate is from the same display
15074 string as this one, and the display string
15075 came from a text property. */
15076 || (EQ (g1->object, glyph->object)
15077 && string_from_text_prop)
15078 /* this candidate is from newline and its
15079 position is not an exact match */
15080 || (NILP (glyph->object)
15081 && glyph->charpos != pt_old)))))
15082 return false;
15083 /* If this candidate gives an exact match, use that. */
15084 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15085 /* If this candidate is a glyph created for the
15086 terminating newline of a line, and point is on that
15087 newline, it wins because it's an exact match. */
15088 || (!row->continued_p
15089 && NILP (glyph->object)
15090 && glyph->charpos == 0
15091 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15092 /* Otherwise, keep the candidate that comes from a row
15093 spanning less buffer positions. This may win when one or
15094 both candidate positions are on glyphs that came from
15095 display strings, for which we cannot compare buffer
15096 positions. */
15097 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15098 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15099 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15100 return false;
15102 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15103 w->cursor.x = x;
15104 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15105 w->cursor.y = row->y + dy;
15107 if (w == XWINDOW (selected_window))
15109 if (!row->continued_p
15110 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15111 && row->x == 0)
15113 this_line_buffer = XBUFFER (w->contents);
15115 CHARPOS (this_line_start_pos)
15116 = MATRIX_ROW_START_CHARPOS (row) + delta;
15117 BYTEPOS (this_line_start_pos)
15118 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15120 CHARPOS (this_line_end_pos)
15121 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15122 BYTEPOS (this_line_end_pos)
15123 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15125 this_line_y = w->cursor.y;
15126 this_line_pixel_height = row->height;
15127 this_line_vpos = w->cursor.vpos;
15128 this_line_start_x = row->x;
15130 else
15131 CHARPOS (this_line_start_pos) = 0;
15134 return true;
15138 /* Run window scroll functions, if any, for WINDOW with new window
15139 start STARTP. Sets the window start of WINDOW to that position.
15141 We assume that the window's buffer is really current. */
15143 static struct text_pos
15144 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15146 struct window *w = XWINDOW (window);
15147 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15149 eassert (current_buffer == XBUFFER (w->contents));
15151 if (!NILP (Vwindow_scroll_functions))
15153 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15154 make_number (CHARPOS (startp)));
15155 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15156 /* In case the hook functions switch buffers. */
15157 set_buffer_internal (XBUFFER (w->contents));
15160 return startp;
15164 /* Make sure the line containing the cursor is fully visible.
15165 A value of true means there is nothing to be done.
15166 (Either the line is fully visible, or it cannot be made so,
15167 or we cannot tell.)
15169 If FORCE_P, return false even if partial visible cursor row
15170 is higher than window.
15172 If CURRENT_MATRIX_P, use the information from the
15173 window's current glyph matrix; otherwise use the desired glyph
15174 matrix.
15176 A value of false means the caller should do scrolling
15177 as if point had gone off the screen. */
15179 static bool
15180 cursor_row_fully_visible_p (struct window *w, bool force_p,
15181 bool current_matrix_p)
15183 struct glyph_matrix *matrix;
15184 struct glyph_row *row;
15185 int window_height;
15187 if (!make_cursor_line_fully_visible_p)
15188 return true;
15190 /* It's not always possible to find the cursor, e.g, when a window
15191 is full of overlay strings. Don't do anything in that case. */
15192 if (w->cursor.vpos < 0)
15193 return true;
15195 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15196 row = MATRIX_ROW (matrix, w->cursor.vpos);
15198 /* If the cursor row is not partially visible, there's nothing to do. */
15199 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15200 return true;
15202 /* If the row the cursor is in is taller than the window's height,
15203 it's not clear what to do, so do nothing. */
15204 window_height = window_box_height (w);
15205 if (row->height >= window_height)
15207 if (!force_p || MINI_WINDOW_P (w)
15208 || w->vscroll || w->cursor.vpos == 0)
15209 return true;
15211 return false;
15215 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15216 means only WINDOW is redisplayed in redisplay_internal.
15217 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15218 in redisplay_window to bring a partially visible line into view in
15219 the case that only the cursor has moved.
15221 LAST_LINE_MISFIT should be true if we're scrolling because the
15222 last screen line's vertical height extends past the end of the screen.
15224 Value is
15226 1 if scrolling succeeded
15228 0 if scrolling didn't find point.
15230 -1 if new fonts have been loaded so that we must interrupt
15231 redisplay, adjust glyph matrices, and try again. */
15233 enum
15235 SCROLLING_SUCCESS,
15236 SCROLLING_FAILED,
15237 SCROLLING_NEED_LARGER_MATRICES
15240 /* If scroll-conservatively is more than this, never recenter.
15242 If you change this, don't forget to update the doc string of
15243 `scroll-conservatively' and the Emacs manual. */
15244 #define SCROLL_LIMIT 100
15246 static int
15247 try_scrolling (Lisp_Object window, bool just_this_one_p,
15248 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15249 bool temp_scroll_step, bool last_line_misfit)
15251 struct window *w = XWINDOW (window);
15252 struct frame *f = XFRAME (w->frame);
15253 struct text_pos pos, startp;
15254 struct it it;
15255 int this_scroll_margin, scroll_max, rc, height;
15256 int dy = 0, amount_to_scroll = 0;
15257 bool scroll_down_p = false;
15258 int extra_scroll_margin_lines = last_line_misfit;
15259 Lisp_Object aggressive;
15260 /* We will never try scrolling more than this number of lines. */
15261 int scroll_limit = SCROLL_LIMIT;
15262 int frame_line_height = default_line_pixel_height (w);
15263 int window_total_lines
15264 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15266 #ifdef GLYPH_DEBUG
15267 debug_method_add (w, "try_scrolling");
15268 #endif
15270 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15272 /* Compute scroll margin height in pixels. We scroll when point is
15273 within this distance from the top or bottom of the window. */
15274 if (scroll_margin > 0)
15275 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15276 * frame_line_height;
15277 else
15278 this_scroll_margin = 0;
15280 /* Force arg_scroll_conservatively to have a reasonable value, to
15281 avoid scrolling too far away with slow move_it_* functions. Note
15282 that the user can supply scroll-conservatively equal to
15283 `most-positive-fixnum', which can be larger than INT_MAX. */
15284 if (arg_scroll_conservatively > scroll_limit)
15286 arg_scroll_conservatively = scroll_limit + 1;
15287 scroll_max = scroll_limit * frame_line_height;
15289 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15290 /* Compute how much we should try to scroll maximally to bring
15291 point into view. */
15292 scroll_max = (max (scroll_step,
15293 max (arg_scroll_conservatively, temp_scroll_step))
15294 * frame_line_height);
15295 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15296 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15297 /* We're trying to scroll because of aggressive scrolling but no
15298 scroll_step is set. Choose an arbitrary one. */
15299 scroll_max = 10 * frame_line_height;
15300 else
15301 scroll_max = 0;
15303 too_near_end:
15305 /* Decide whether to scroll down. */
15306 if (PT > CHARPOS (startp))
15308 int scroll_margin_y;
15310 /* Compute the pixel ypos of the scroll margin, then move IT to
15311 either that ypos or PT, whichever comes first. */
15312 start_display (&it, w, startp);
15313 scroll_margin_y = it.last_visible_y - this_scroll_margin
15314 - frame_line_height * extra_scroll_margin_lines;
15315 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15316 (MOVE_TO_POS | MOVE_TO_Y));
15318 if (PT > CHARPOS (it.current.pos))
15320 int y0 = line_bottom_y (&it);
15321 /* Compute how many pixels below window bottom to stop searching
15322 for PT. This avoids costly search for PT that is far away if
15323 the user limited scrolling by a small number of lines, but
15324 always finds PT if scroll_conservatively is set to a large
15325 number, such as most-positive-fixnum. */
15326 int slack = max (scroll_max, 10 * frame_line_height);
15327 int y_to_move = it.last_visible_y + slack;
15329 /* Compute the distance from the scroll margin to PT or to
15330 the scroll limit, whichever comes first. This should
15331 include the height of the cursor line, to make that line
15332 fully visible. */
15333 move_it_to (&it, PT, -1, y_to_move,
15334 -1, MOVE_TO_POS | MOVE_TO_Y);
15335 dy = line_bottom_y (&it) - y0;
15337 if (dy > scroll_max)
15338 return SCROLLING_FAILED;
15340 if (dy > 0)
15341 scroll_down_p = true;
15343 else if (PT == IT_CHARPOS (it)
15344 && IT_CHARPOS (it) < ZV
15345 && it.method == GET_FROM_STRING
15346 && arg_scroll_conservatively > scroll_limit
15347 && it.current_x == 0)
15349 enum move_it_result skip;
15350 int y1 = it.current_y;
15351 int vpos;
15353 /* A before-string that includes newlines and is displayed
15354 on the last visible screen line could fail us under
15355 scroll-conservatively > 100, because we will be unable to
15356 position the cursor on that last visible line. Try to
15357 recover by finding the first screen line that has some
15358 glyphs coming from the buffer text. */
15359 do {
15360 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15361 if (skip != MOVE_NEWLINE_OR_CR
15362 || IT_CHARPOS (it) != PT
15363 || it.method == GET_FROM_BUFFER)
15364 break;
15365 vpos = it.vpos;
15366 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15367 } while (it.vpos > vpos);
15369 dy = it.current_y - y1;
15371 if (dy > scroll_max)
15372 return SCROLLING_FAILED;
15374 if (dy > 0)
15375 scroll_down_p = true;
15379 if (scroll_down_p)
15381 /* Point is in or below the bottom scroll margin, so move the
15382 window start down. If scrolling conservatively, move it just
15383 enough down to make point visible. If scroll_step is set,
15384 move it down by scroll_step. */
15385 if (arg_scroll_conservatively)
15386 amount_to_scroll
15387 = min (max (dy, frame_line_height),
15388 frame_line_height * arg_scroll_conservatively);
15389 else if (scroll_step || temp_scroll_step)
15390 amount_to_scroll = scroll_max;
15391 else
15393 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15394 height = WINDOW_BOX_TEXT_HEIGHT (w);
15395 if (NUMBERP (aggressive))
15397 double float_amount = XFLOATINT (aggressive) * height;
15398 int aggressive_scroll = float_amount;
15399 if (aggressive_scroll == 0 && float_amount > 0)
15400 aggressive_scroll = 1;
15401 /* Don't let point enter the scroll margin near top of
15402 the window. This could happen if the value of
15403 scroll_up_aggressively is too large and there are
15404 non-zero margins, because scroll_up_aggressively
15405 means put point that fraction of window height
15406 _from_the_bottom_margin_. */
15407 if (aggressive_scroll + 2 * this_scroll_margin > height)
15408 aggressive_scroll = height - 2 * this_scroll_margin;
15409 amount_to_scroll = dy + aggressive_scroll;
15413 if (amount_to_scroll <= 0)
15414 return SCROLLING_FAILED;
15416 start_display (&it, w, startp);
15417 if (arg_scroll_conservatively <= scroll_limit)
15418 move_it_vertically (&it, amount_to_scroll);
15419 else
15421 /* Extra precision for users who set scroll-conservatively
15422 to a large number: make sure the amount we scroll
15423 the window start is never less than amount_to_scroll,
15424 which was computed as distance from window bottom to
15425 point. This matters when lines at window top and lines
15426 below window bottom have different height. */
15427 struct it it1;
15428 void *it1data = NULL;
15429 /* We use a temporary it1 because line_bottom_y can modify
15430 its argument, if it moves one line down; see there. */
15431 int start_y;
15433 SAVE_IT (it1, it, it1data);
15434 start_y = line_bottom_y (&it1);
15435 do {
15436 RESTORE_IT (&it, &it, it1data);
15437 move_it_by_lines (&it, 1);
15438 SAVE_IT (it1, it, it1data);
15439 } while (IT_CHARPOS (it) < ZV
15440 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15441 bidi_unshelve_cache (it1data, true);
15444 /* If STARTP is unchanged, move it down another screen line. */
15445 if (IT_CHARPOS (it) == CHARPOS (startp))
15446 move_it_by_lines (&it, 1);
15447 startp = it.current.pos;
15449 else
15451 struct text_pos scroll_margin_pos = startp;
15452 int y_offset = 0;
15454 /* See if point is inside the scroll margin at the top of the
15455 window. */
15456 if (this_scroll_margin)
15458 int y_start;
15460 start_display (&it, w, startp);
15461 y_start = it.current_y;
15462 move_it_vertically (&it, this_scroll_margin);
15463 scroll_margin_pos = it.current.pos;
15464 /* If we didn't move enough before hitting ZV, request
15465 additional amount of scroll, to move point out of the
15466 scroll margin. */
15467 if (IT_CHARPOS (it) == ZV
15468 && it.current_y - y_start < this_scroll_margin)
15469 y_offset = this_scroll_margin - (it.current_y - y_start);
15472 if (PT < CHARPOS (scroll_margin_pos))
15474 /* Point is in the scroll margin at the top of the window or
15475 above what is displayed in the window. */
15476 int y0, y_to_move;
15478 /* Compute the vertical distance from PT to the scroll
15479 margin position. Move as far as scroll_max allows, or
15480 one screenful, or 10 screen lines, whichever is largest.
15481 Give up if distance is greater than scroll_max or if we
15482 didn't reach the scroll margin position. */
15483 SET_TEXT_POS (pos, PT, PT_BYTE);
15484 start_display (&it, w, pos);
15485 y0 = it.current_y;
15486 y_to_move = max (it.last_visible_y,
15487 max (scroll_max, 10 * frame_line_height));
15488 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15489 y_to_move, -1,
15490 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15491 dy = it.current_y - y0;
15492 if (dy > scroll_max
15493 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15494 return SCROLLING_FAILED;
15496 /* Additional scroll for when ZV was too close to point. */
15497 dy += y_offset;
15499 /* Compute new window start. */
15500 start_display (&it, w, startp);
15502 if (arg_scroll_conservatively)
15503 amount_to_scroll = max (dy, frame_line_height
15504 * max (scroll_step, temp_scroll_step));
15505 else if (scroll_step || temp_scroll_step)
15506 amount_to_scroll = scroll_max;
15507 else
15509 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15510 height = WINDOW_BOX_TEXT_HEIGHT (w);
15511 if (NUMBERP (aggressive))
15513 double float_amount = XFLOATINT (aggressive) * height;
15514 int aggressive_scroll = float_amount;
15515 if (aggressive_scroll == 0 && float_amount > 0)
15516 aggressive_scroll = 1;
15517 /* Don't let point enter the scroll margin near
15518 bottom of the window, if the value of
15519 scroll_down_aggressively happens to be too
15520 large. */
15521 if (aggressive_scroll + 2 * this_scroll_margin > height)
15522 aggressive_scroll = height - 2 * this_scroll_margin;
15523 amount_to_scroll = dy + aggressive_scroll;
15527 if (amount_to_scroll <= 0)
15528 return SCROLLING_FAILED;
15530 move_it_vertically_backward (&it, amount_to_scroll);
15531 startp = it.current.pos;
15535 /* Run window scroll functions. */
15536 startp = run_window_scroll_functions (window, startp);
15538 /* Display the window. Give up if new fonts are loaded, or if point
15539 doesn't appear. */
15540 if (!try_window (window, startp, 0))
15541 rc = SCROLLING_NEED_LARGER_MATRICES;
15542 else if (w->cursor.vpos < 0)
15544 clear_glyph_matrix (w->desired_matrix);
15545 rc = SCROLLING_FAILED;
15547 else
15549 /* Maybe forget recorded base line for line number display. */
15550 if (!just_this_one_p
15551 || current_buffer->clip_changed
15552 || BEG_UNCHANGED < CHARPOS (startp))
15553 w->base_line_number = 0;
15555 /* If cursor ends up on a partially visible line,
15556 treat that as being off the bottom of the screen. */
15557 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15558 false)
15559 /* It's possible that the cursor is on the first line of the
15560 buffer, which is partially obscured due to a vscroll
15561 (Bug#7537). In that case, avoid looping forever. */
15562 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15564 clear_glyph_matrix (w->desired_matrix);
15565 ++extra_scroll_margin_lines;
15566 goto too_near_end;
15568 rc = SCROLLING_SUCCESS;
15571 return rc;
15575 /* Compute a suitable window start for window W if display of W starts
15576 on a continuation line. Value is true if a new window start
15577 was computed.
15579 The new window start will be computed, based on W's width, starting
15580 from the start of the continued line. It is the start of the
15581 screen line with the minimum distance from the old start W->start,
15582 which is still before point (otherwise point will definitely not
15583 be visible in the window). */
15585 static bool
15586 compute_window_start_on_continuation_line (struct window *w)
15588 struct text_pos pos, start_pos, pos_before_pt;
15589 bool window_start_changed_p = false;
15591 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15593 /* If window start is on a continuation line... Window start may be
15594 < BEGV in case there's invisible text at the start of the
15595 buffer (M-x rmail, for example). */
15596 if (CHARPOS (start_pos) > BEGV
15597 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15599 struct it it;
15600 struct glyph_row *row;
15602 /* Handle the case that the window start is out of range. */
15603 if (CHARPOS (start_pos) < BEGV)
15604 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15605 else if (CHARPOS (start_pos) > ZV)
15606 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15608 /* Find the start of the continued line. This should be fast
15609 because find_newline is fast (newline cache). */
15610 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15611 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15612 row, DEFAULT_FACE_ID);
15613 reseat_at_previous_visible_line_start (&it);
15615 /* If the line start is "too far" away from the window start,
15616 say it takes too much time to compute a new window start.
15617 Also, give up if the line start is after point, as in that
15618 case point will not be visible with any window start we
15619 compute. */
15620 if (IT_CHARPOS (it) <= PT
15621 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15622 /* PXW: Do we need upper bounds here? */
15623 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15625 int min_distance, distance;
15627 /* Move forward by display lines to find the new window
15628 start. If window width was enlarged, the new start can
15629 be expected to be > the old start. If window width was
15630 decreased, the new window start will be < the old start.
15631 So, we're looking for the display line start with the
15632 minimum distance from the old window start. */
15633 pos_before_pt = pos = it.current.pos;
15634 min_distance = INFINITY;
15635 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15636 distance < min_distance)
15638 min_distance = distance;
15639 if (CHARPOS (pos) <= PT)
15640 pos_before_pt = pos;
15641 pos = it.current.pos;
15642 if (it.line_wrap == WORD_WRAP)
15644 /* Under WORD_WRAP, move_it_by_lines is likely to
15645 overshoot and stop not at the first, but the
15646 second character from the left margin. So in
15647 that case, we need a more tight control on the X
15648 coordinate of the iterator than move_it_by_lines
15649 promises in its contract. The method is to first
15650 go to the last (rightmost) visible character of a
15651 line, then move to the leftmost character on the
15652 next line in a separate call. */
15653 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15654 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15655 move_it_to (&it, ZV, 0,
15656 it.current_y + it.max_ascent + it.max_descent, -1,
15657 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15659 else
15660 move_it_by_lines (&it, 1);
15663 /* It makes very little sense to make the new window start
15664 after point, as point won't be visible. If that's what
15665 the loop above finds, fall back on the candidate before
15666 or at point that is closest to the old window start. */
15667 if (CHARPOS (pos) > PT)
15668 pos = pos_before_pt;
15670 /* Set the window start there. */
15671 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15672 window_start_changed_p = true;
15676 return window_start_changed_p;
15680 /* Try cursor movement in case text has not changed in window WINDOW,
15681 with window start STARTP. Value is
15683 CURSOR_MOVEMENT_SUCCESS if successful
15685 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15687 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15688 display. *SCROLL_STEP is set to true, under certain circumstances, if
15689 we want to scroll as if scroll-step were set to 1. See the code.
15691 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15692 which case we have to abort this redisplay, and adjust matrices
15693 first. */
15695 enum
15697 CURSOR_MOVEMENT_SUCCESS,
15698 CURSOR_MOVEMENT_CANNOT_BE_USED,
15699 CURSOR_MOVEMENT_MUST_SCROLL,
15700 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15703 static int
15704 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15705 bool *scroll_step)
15707 struct window *w = XWINDOW (window);
15708 struct frame *f = XFRAME (w->frame);
15709 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15711 #ifdef GLYPH_DEBUG
15712 if (inhibit_try_cursor_movement)
15713 return rc;
15714 #endif
15716 /* Previously, there was a check for Lisp integer in the
15717 if-statement below. Now, this field is converted to
15718 ptrdiff_t, thus zero means invalid position in a buffer. */
15719 eassert (w->last_point > 0);
15720 /* Likewise there was a check whether window_end_vpos is nil or larger
15721 than the window. Now window_end_vpos is int and so never nil, but
15722 let's leave eassert to check whether it fits in the window. */
15723 eassert (!w->window_end_valid
15724 || w->window_end_vpos < w->current_matrix->nrows);
15726 /* Handle case where text has not changed, only point, and it has
15727 not moved off the frame. */
15728 if (/* Point may be in this window. */
15729 PT >= CHARPOS (startp)
15730 /* Selective display hasn't changed. */
15731 && !current_buffer->clip_changed
15732 /* Function force-mode-line-update is used to force a thorough
15733 redisplay. It sets either windows_or_buffers_changed or
15734 update_mode_lines. So don't take a shortcut here for these
15735 cases. */
15736 && !update_mode_lines
15737 && !windows_or_buffers_changed
15738 && !f->cursor_type_changed
15739 && NILP (Vshow_trailing_whitespace)
15740 /* This code is not used for mini-buffer for the sake of the case
15741 of redisplaying to replace an echo area message; since in
15742 that case the mini-buffer contents per se are usually
15743 unchanged. This code is of no real use in the mini-buffer
15744 since the handling of this_line_start_pos, etc., in redisplay
15745 handles the same cases. */
15746 && !EQ (window, minibuf_window)
15747 && (FRAME_WINDOW_P (f)
15748 || !overlay_arrow_in_current_buffer_p ()))
15750 int this_scroll_margin, top_scroll_margin;
15751 struct glyph_row *row = NULL;
15752 int frame_line_height = default_line_pixel_height (w);
15753 int window_total_lines
15754 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15756 #ifdef GLYPH_DEBUG
15757 debug_method_add (w, "cursor movement");
15758 #endif
15760 /* Scroll if point within this distance from the top or bottom
15761 of the window. This is a pixel value. */
15762 if (scroll_margin > 0)
15764 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15765 this_scroll_margin *= frame_line_height;
15767 else
15768 this_scroll_margin = 0;
15770 top_scroll_margin = this_scroll_margin;
15771 if (WINDOW_WANTS_HEADER_LINE_P (w))
15772 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15774 /* Start with the row the cursor was displayed during the last
15775 not paused redisplay. Give up if that row is not valid. */
15776 if (w->last_cursor_vpos < 0
15777 || w->last_cursor_vpos >= w->current_matrix->nrows)
15778 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15779 else
15781 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15782 if (row->mode_line_p)
15783 ++row;
15784 if (!row->enabled_p)
15785 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15788 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15790 bool scroll_p = false, must_scroll = false;
15791 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15793 if (PT > w->last_point)
15795 /* Point has moved forward. */
15796 while (MATRIX_ROW_END_CHARPOS (row) < PT
15797 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15799 eassert (row->enabled_p);
15800 ++row;
15803 /* If the end position of a row equals the start
15804 position of the next row, and PT is at that position,
15805 we would rather display cursor in the next line. */
15806 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15807 && MATRIX_ROW_END_CHARPOS (row) == PT
15808 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15809 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15810 && !cursor_row_p (row))
15811 ++row;
15813 /* If within the scroll margin, scroll. Note that
15814 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15815 the next line would be drawn, and that
15816 this_scroll_margin can be zero. */
15817 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15818 || PT > MATRIX_ROW_END_CHARPOS (row)
15819 /* Line is completely visible last line in window
15820 and PT is to be set in the next line. */
15821 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15822 && PT == MATRIX_ROW_END_CHARPOS (row)
15823 && !row->ends_at_zv_p
15824 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15825 scroll_p = true;
15827 else if (PT < w->last_point)
15829 /* Cursor has to be moved backward. Note that PT >=
15830 CHARPOS (startp) because of the outer if-statement. */
15831 while (!row->mode_line_p
15832 && (MATRIX_ROW_START_CHARPOS (row) > PT
15833 || (MATRIX_ROW_START_CHARPOS (row) == PT
15834 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15835 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15836 row > w->current_matrix->rows
15837 && (row-1)->ends_in_newline_from_string_p))))
15838 && (row->y > top_scroll_margin
15839 || CHARPOS (startp) == BEGV))
15841 eassert (row->enabled_p);
15842 --row;
15845 /* Consider the following case: Window starts at BEGV,
15846 there is invisible, intangible text at BEGV, so that
15847 display starts at some point START > BEGV. It can
15848 happen that we are called with PT somewhere between
15849 BEGV and START. Try to handle that case. */
15850 if (row < w->current_matrix->rows
15851 || row->mode_line_p)
15853 row = w->current_matrix->rows;
15854 if (row->mode_line_p)
15855 ++row;
15858 /* Due to newlines in overlay strings, we may have to
15859 skip forward over overlay strings. */
15860 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15861 && MATRIX_ROW_END_CHARPOS (row) == PT
15862 && !cursor_row_p (row))
15863 ++row;
15865 /* If within the scroll margin, scroll. */
15866 if (row->y < top_scroll_margin
15867 && CHARPOS (startp) != BEGV)
15868 scroll_p = true;
15870 else
15872 /* Cursor did not move. So don't scroll even if cursor line
15873 is partially visible, as it was so before. */
15874 rc = CURSOR_MOVEMENT_SUCCESS;
15877 if (PT < MATRIX_ROW_START_CHARPOS (row)
15878 || PT > MATRIX_ROW_END_CHARPOS (row))
15880 /* if PT is not in the glyph row, give up. */
15881 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15882 must_scroll = true;
15884 else if (rc != CURSOR_MOVEMENT_SUCCESS
15885 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15887 struct glyph_row *row1;
15889 /* If rows are bidi-reordered and point moved, back up
15890 until we find a row that does not belong to a
15891 continuation line. This is because we must consider
15892 all rows of a continued line as candidates for the
15893 new cursor positioning, since row start and end
15894 positions change non-linearly with vertical position
15895 in such rows. */
15896 /* FIXME: Revisit this when glyph ``spilling'' in
15897 continuation lines' rows is implemented for
15898 bidi-reordered rows. */
15899 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15900 MATRIX_ROW_CONTINUATION_LINE_P (row);
15901 --row)
15903 /* If we hit the beginning of the displayed portion
15904 without finding the first row of a continued
15905 line, give up. */
15906 if (row <= row1)
15908 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15909 break;
15911 eassert (row->enabled_p);
15914 if (must_scroll)
15916 else if (rc != CURSOR_MOVEMENT_SUCCESS
15917 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15918 /* Make sure this isn't a header line by any chance, since
15919 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15920 && !row->mode_line_p
15921 && make_cursor_line_fully_visible_p)
15923 if (PT == MATRIX_ROW_END_CHARPOS (row)
15924 && !row->ends_at_zv_p
15925 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15926 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15927 else if (row->height > window_box_height (w))
15929 /* If we end up in a partially visible line, let's
15930 make it fully visible, except when it's taller
15931 than the window, in which case we can't do much
15932 about it. */
15933 *scroll_step = true;
15934 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15936 else
15938 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15939 if (!cursor_row_fully_visible_p (w, false, true))
15940 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15941 else
15942 rc = CURSOR_MOVEMENT_SUCCESS;
15945 else if (scroll_p)
15946 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15947 else if (rc != CURSOR_MOVEMENT_SUCCESS
15948 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15950 /* With bidi-reordered rows, there could be more than
15951 one candidate row whose start and end positions
15952 occlude point. We need to let set_cursor_from_row
15953 find the best candidate. */
15954 /* FIXME: Revisit this when glyph ``spilling'' in
15955 continuation lines' rows is implemented for
15956 bidi-reordered rows. */
15957 bool rv = false;
15961 bool at_zv_p = false, exact_match_p = false;
15963 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15964 && PT <= MATRIX_ROW_END_CHARPOS (row)
15965 && cursor_row_p (row))
15966 rv |= set_cursor_from_row (w, row, w->current_matrix,
15967 0, 0, 0, 0);
15968 /* As soon as we've found the exact match for point,
15969 or the first suitable row whose ends_at_zv_p flag
15970 is set, we are done. */
15971 if (rv)
15973 at_zv_p = MATRIX_ROW (w->current_matrix,
15974 w->cursor.vpos)->ends_at_zv_p;
15975 if (!at_zv_p
15976 && w->cursor.hpos >= 0
15977 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15978 w->cursor.vpos))
15980 struct glyph_row *candidate =
15981 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15982 struct glyph *g =
15983 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15984 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15986 exact_match_p =
15987 (BUFFERP (g->object) && g->charpos == PT)
15988 || (NILP (g->object)
15989 && (g->charpos == PT
15990 || (g->charpos == 0 && endpos - 1 == PT)));
15992 if (at_zv_p || exact_match_p)
15994 rc = CURSOR_MOVEMENT_SUCCESS;
15995 break;
15998 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15999 break;
16000 ++row;
16002 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16003 || row->continued_p)
16004 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16005 || (MATRIX_ROW_START_CHARPOS (row) == PT
16006 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16007 /* If we didn't find any candidate rows, or exited the
16008 loop before all the candidates were examined, signal
16009 to the caller that this method failed. */
16010 if (rc != CURSOR_MOVEMENT_SUCCESS
16011 && !(rv
16012 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16013 && !row->continued_p))
16014 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16015 else if (rv)
16016 rc = CURSOR_MOVEMENT_SUCCESS;
16018 else
16022 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16024 rc = CURSOR_MOVEMENT_SUCCESS;
16025 break;
16027 ++row;
16029 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16030 && MATRIX_ROW_START_CHARPOS (row) == PT
16031 && cursor_row_p (row));
16036 return rc;
16040 void
16041 set_vertical_scroll_bar (struct window *w)
16043 ptrdiff_t start, end, whole;
16045 /* Calculate the start and end positions for the current window.
16046 At some point, it would be nice to choose between scrollbars
16047 which reflect the whole buffer size, with special markers
16048 indicating narrowing, and scrollbars which reflect only the
16049 visible region.
16051 Note that mini-buffers sometimes aren't displaying any text. */
16052 if (!MINI_WINDOW_P (w)
16053 || (w == XWINDOW (minibuf_window)
16054 && NILP (echo_area_buffer[0])))
16056 struct buffer *buf = XBUFFER (w->contents);
16057 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16058 start = marker_position (w->start) - BUF_BEGV (buf);
16059 /* I don't think this is guaranteed to be right. For the
16060 moment, we'll pretend it is. */
16061 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16063 if (end < start)
16064 end = start;
16065 if (whole < (end - start))
16066 whole = end - start;
16068 else
16069 start = end = whole = 0;
16071 /* Indicate what this scroll bar ought to be displaying now. */
16072 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16073 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16074 (w, end - start, whole, start);
16078 void
16079 set_horizontal_scroll_bar (struct window *w)
16081 int start, end, whole, portion;
16083 if (!MINI_WINDOW_P (w)
16084 || (w == XWINDOW (minibuf_window)
16085 && NILP (echo_area_buffer[0])))
16087 struct buffer *b = XBUFFER (w->contents);
16088 struct buffer *old_buffer = NULL;
16089 struct it it;
16090 struct text_pos startp;
16092 if (b != current_buffer)
16094 old_buffer = current_buffer;
16095 set_buffer_internal (b);
16098 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16099 start_display (&it, w, startp);
16100 it.last_visible_x = INT_MAX;
16101 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16102 MOVE_TO_X | MOVE_TO_Y);
16103 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16104 window_box_height (w), -1,
16105 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16107 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16108 end = start + window_box_width (w, TEXT_AREA);
16109 portion = end - start;
16110 /* After enlarging a horizontally scrolled window such that it
16111 gets at least as wide as the text it contains, make sure that
16112 the thumb doesn't fill the entire scroll bar so we can still
16113 drag it back to see the entire text. */
16114 whole = max (whole, end);
16116 if (it.bidi_p)
16118 Lisp_Object pdir;
16120 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16121 if (EQ (pdir, Qright_to_left))
16123 start = whole - end;
16124 end = start + portion;
16128 if (old_buffer)
16129 set_buffer_internal (old_buffer);
16131 else
16132 start = end = whole = portion = 0;
16134 w->hscroll_whole = whole;
16136 /* Indicate what this scroll bar ought to be displaying now. */
16137 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16138 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16139 (w, portion, whole, start);
16143 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16144 selected_window is redisplayed.
16146 We can return without actually redisplaying the window if fonts has been
16147 changed on window's frame. In that case, redisplay_internal will retry.
16149 As one of the important parts of redisplaying a window, we need to
16150 decide whether the previous window-start position (stored in the
16151 window's w->start marker position) is still valid, and if it isn't,
16152 recompute it. Some details about that:
16154 . The previous window-start could be in a continuation line, in
16155 which case we need to recompute it when the window width
16156 changes. See compute_window_start_on_continuation_line and its
16157 call below.
16159 . The text that changed since last redisplay could include the
16160 previous window-start position. In that case, we try to salvage
16161 what we can from the current glyph matrix by calling
16162 try_scrolling, which see.
16164 . Some Emacs command could force us to use a specific window-start
16165 position by setting the window's force_start flag, or gently
16166 propose doing that by setting the window's optional_new_start
16167 flag. In these cases, we try using the specified start point if
16168 that succeeds (i.e. the window desired matrix is successfully
16169 recomputed, and point location is within the window). In case
16170 of optional_new_start, we first check if the specified start
16171 position is feasible, i.e. if it will allow point to be
16172 displayed in the window. If using the specified start point
16173 fails, e.g., if new fonts are needed to be loaded, we abort the
16174 redisplay cycle and leave it up to the next cycle to figure out
16175 things.
16177 . Note that the window's force_start flag is sometimes set by
16178 redisplay itself, when it decides that the previous window start
16179 point is fine and should be kept. Search for "goto force_start"
16180 below to see the details. Like the values of window-start
16181 specified outside of redisplay, these internally-deduced values
16182 are tested for feasibility, and ignored if found to be
16183 unfeasible.
16185 . Note that the function try_window, used to completely redisplay
16186 a window, accepts the window's start point as its argument.
16187 This is used several times in the redisplay code to control
16188 where the window start will be, according to user options such
16189 as scroll-conservatively, and also to ensure the screen line
16190 showing point will be fully (as opposed to partially) visible on
16191 display. */
16193 static void
16194 redisplay_window (Lisp_Object window, bool just_this_one_p)
16196 struct window *w = XWINDOW (window);
16197 struct frame *f = XFRAME (w->frame);
16198 struct buffer *buffer = XBUFFER (w->contents);
16199 struct buffer *old = current_buffer;
16200 struct text_pos lpoint, opoint, startp;
16201 bool update_mode_line;
16202 int tem;
16203 struct it it;
16204 /* Record it now because it's overwritten. */
16205 bool current_matrix_up_to_date_p = false;
16206 bool used_current_matrix_p = false;
16207 /* This is less strict than current_matrix_up_to_date_p.
16208 It indicates that the buffer contents and narrowing are unchanged. */
16209 bool buffer_unchanged_p = false;
16210 bool temp_scroll_step = false;
16211 ptrdiff_t count = SPECPDL_INDEX ();
16212 int rc;
16213 int centering_position = -1;
16214 bool last_line_misfit = false;
16215 ptrdiff_t beg_unchanged, end_unchanged;
16216 int frame_line_height;
16217 bool use_desired_matrix;
16218 void *itdata = NULL;
16220 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16221 opoint = lpoint;
16223 #ifdef GLYPH_DEBUG
16224 *w->desired_matrix->method = 0;
16225 #endif
16227 if (!just_this_one_p
16228 && REDISPLAY_SOME_P ()
16229 && !w->redisplay
16230 && !w->update_mode_line
16231 && !f->face_change
16232 && !f->redisplay
16233 && !buffer->text->redisplay
16234 && BUF_PT (buffer) == w->last_point)
16235 return;
16237 /* Make sure that both W's markers are valid. */
16238 eassert (XMARKER (w->start)->buffer == buffer);
16239 eassert (XMARKER (w->pointm)->buffer == buffer);
16241 /* We come here again if we need to run window-text-change-functions
16242 below. */
16243 restart:
16244 reconsider_clip_changes (w);
16245 frame_line_height = default_line_pixel_height (w);
16247 /* Has the mode line to be updated? */
16248 update_mode_line = (w->update_mode_line
16249 || update_mode_lines
16250 || buffer->clip_changed
16251 || buffer->prevent_redisplay_optimizations_p);
16253 if (!just_this_one_p)
16254 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16255 cleverly elsewhere. */
16256 w->must_be_updated_p = true;
16258 if (MINI_WINDOW_P (w))
16260 if (w == XWINDOW (echo_area_window)
16261 && !NILP (echo_area_buffer[0]))
16263 if (update_mode_line)
16264 /* We may have to update a tty frame's menu bar or a
16265 tool-bar. Example `M-x C-h C-h C-g'. */
16266 goto finish_menu_bars;
16267 else
16268 /* We've already displayed the echo area glyphs in this window. */
16269 goto finish_scroll_bars;
16271 else if ((w != XWINDOW (minibuf_window)
16272 || minibuf_level == 0)
16273 /* When buffer is nonempty, redisplay window normally. */
16274 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16275 /* Quail displays non-mini buffers in minibuffer window.
16276 In that case, redisplay the window normally. */
16277 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16279 /* W is a mini-buffer window, but it's not active, so clear
16280 it. */
16281 int yb = window_text_bottom_y (w);
16282 struct glyph_row *row;
16283 int y;
16285 for (y = 0, row = w->desired_matrix->rows;
16286 y < yb;
16287 y += row->height, ++row)
16288 blank_row (w, row, y);
16289 goto finish_scroll_bars;
16292 clear_glyph_matrix (w->desired_matrix);
16295 /* Otherwise set up data on this window; select its buffer and point
16296 value. */
16297 /* Really select the buffer, for the sake of buffer-local
16298 variables. */
16299 set_buffer_internal_1 (XBUFFER (w->contents));
16301 current_matrix_up_to_date_p
16302 = (w->window_end_valid
16303 && !current_buffer->clip_changed
16304 && !current_buffer->prevent_redisplay_optimizations_p
16305 && !window_outdated (w));
16307 /* Run the window-text-change-functions
16308 if it is possible that the text on the screen has changed
16309 (either due to modification of the text, or any other reason). */
16310 if (!current_matrix_up_to_date_p
16311 && !NILP (Vwindow_text_change_functions))
16313 safe_run_hooks (Qwindow_text_change_functions);
16314 goto restart;
16317 beg_unchanged = BEG_UNCHANGED;
16318 end_unchanged = END_UNCHANGED;
16320 SET_TEXT_POS (opoint, PT, PT_BYTE);
16322 specbind (Qinhibit_point_motion_hooks, Qt);
16324 buffer_unchanged_p
16325 = (w->window_end_valid
16326 && !current_buffer->clip_changed
16327 && !window_outdated (w));
16329 /* When windows_or_buffers_changed is non-zero, we can't rely
16330 on the window end being valid, so set it to zero there. */
16331 if (windows_or_buffers_changed)
16333 /* If window starts on a continuation line, maybe adjust the
16334 window start in case the window's width changed. */
16335 if (XMARKER (w->start)->buffer == current_buffer)
16336 compute_window_start_on_continuation_line (w);
16338 w->window_end_valid = false;
16339 /* If so, we also can't rely on current matrix
16340 and should not fool try_cursor_movement below. */
16341 current_matrix_up_to_date_p = false;
16344 /* Some sanity checks. */
16345 CHECK_WINDOW_END (w);
16346 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16347 emacs_abort ();
16348 if (BYTEPOS (opoint) < CHARPOS (opoint))
16349 emacs_abort ();
16351 if (mode_line_update_needed (w))
16352 update_mode_line = true;
16354 /* Point refers normally to the selected window. For any other
16355 window, set up appropriate value. */
16356 if (!EQ (window, selected_window))
16358 ptrdiff_t new_pt = marker_position (w->pointm);
16359 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16361 if (new_pt < BEGV)
16363 new_pt = BEGV;
16364 new_pt_byte = BEGV_BYTE;
16365 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16367 else if (new_pt > (ZV - 1))
16369 new_pt = ZV;
16370 new_pt_byte = ZV_BYTE;
16371 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16374 /* We don't use SET_PT so that the point-motion hooks don't run. */
16375 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16378 /* If any of the character widths specified in the display table
16379 have changed, invalidate the width run cache. It's true that
16380 this may be a bit late to catch such changes, but the rest of
16381 redisplay goes (non-fatally) haywire when the display table is
16382 changed, so why should we worry about doing any better? */
16383 if (current_buffer->width_run_cache
16384 || (current_buffer->base_buffer
16385 && current_buffer->base_buffer->width_run_cache))
16387 struct Lisp_Char_Table *disptab = buffer_display_table ();
16389 if (! disptab_matches_widthtab
16390 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16392 struct buffer *buf = current_buffer;
16394 if (buf->base_buffer)
16395 buf = buf->base_buffer;
16396 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16397 recompute_width_table (current_buffer, disptab);
16401 /* If window-start is screwed up, choose a new one. */
16402 if (XMARKER (w->start)->buffer != current_buffer)
16403 goto recenter;
16405 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16407 /* If someone specified a new starting point but did not insist,
16408 check whether it can be used. */
16409 if ((w->optional_new_start || window_frozen_p (w))
16410 && CHARPOS (startp) >= BEGV
16411 && CHARPOS (startp) <= ZV)
16413 ptrdiff_t it_charpos;
16415 w->optional_new_start = false;
16416 start_display (&it, w, startp);
16417 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16418 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16419 /* Record IT's position now, since line_bottom_y might change
16420 that. */
16421 it_charpos = IT_CHARPOS (it);
16422 /* Make sure we set the force_start flag only if the cursor row
16423 will be fully visible. Otherwise, the code under force_start
16424 label below will try to move point back into view, which is
16425 not what the code which sets optional_new_start wants. */
16426 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16427 && !w->force_start)
16429 if (it_charpos == PT)
16430 w->force_start = true;
16431 /* IT may overshoot PT if text at PT is invisible. */
16432 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16433 w->force_start = true;
16434 #ifdef GLYPH_DEBUG
16435 if (w->force_start)
16437 if (window_frozen_p (w))
16438 debug_method_add (w, "set force_start from frozen window start");
16439 else
16440 debug_method_add (w, "set force_start from optional_new_start");
16442 #endif
16446 force_start:
16448 /* Handle case where place to start displaying has been specified,
16449 unless the specified location is outside the accessible range. */
16450 if (w->force_start)
16452 /* We set this later on if we have to adjust point. */
16453 int new_vpos = -1;
16455 w->force_start = false;
16456 w->vscroll = 0;
16457 w->window_end_valid = false;
16459 /* Forget any recorded base line for line number display. */
16460 if (!buffer_unchanged_p)
16461 w->base_line_number = 0;
16463 /* Redisplay the mode line. Select the buffer properly for that.
16464 Also, run the hook window-scroll-functions
16465 because we have scrolled. */
16466 /* Note, we do this after clearing force_start because
16467 if there's an error, it is better to forget about force_start
16468 than to get into an infinite loop calling the hook functions
16469 and having them get more errors. */
16470 if (!update_mode_line
16471 || ! NILP (Vwindow_scroll_functions))
16473 update_mode_line = true;
16474 w->update_mode_line = true;
16475 startp = run_window_scroll_functions (window, startp);
16478 if (CHARPOS (startp) < BEGV)
16479 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16480 else if (CHARPOS (startp) > ZV)
16481 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16483 /* Redisplay, then check if cursor has been set during the
16484 redisplay. Give up if new fonts were loaded. */
16485 /* We used to issue a CHECK_MARGINS argument to try_window here,
16486 but this causes scrolling to fail when point begins inside
16487 the scroll margin (bug#148) -- cyd */
16488 if (!try_window (window, startp, 0))
16490 w->force_start = true;
16491 clear_glyph_matrix (w->desired_matrix);
16492 goto need_larger_matrices;
16495 if (w->cursor.vpos < 0)
16497 /* If point does not appear, try to move point so it does
16498 appear. The desired matrix has been built above, so we
16499 can use it here. First see if point is in invisible
16500 text, and if so, move it to the first visible buffer
16501 position past that. */
16502 struct glyph_row *r = NULL;
16503 Lisp_Object invprop =
16504 get_char_property_and_overlay (make_number (PT), Qinvisible,
16505 Qnil, NULL);
16507 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16509 ptrdiff_t alt_pt;
16510 Lisp_Object invprop_end =
16511 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16512 Qnil, Qnil);
16514 if (NATNUMP (invprop_end))
16515 alt_pt = XFASTINT (invprop_end);
16516 else
16517 alt_pt = ZV;
16518 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16519 NULL, 0);
16521 if (r)
16522 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16523 else /* Give up and just move to the middle of the window. */
16524 new_vpos = window_box_height (w) / 2;
16527 if (!cursor_row_fully_visible_p (w, false, false))
16529 /* Point does appear, but on a line partly visible at end of window.
16530 Move it back to a fully-visible line. */
16531 new_vpos = window_box_height (w);
16532 /* But if window_box_height suggests a Y coordinate that is
16533 not less than we already have, that line will clearly not
16534 be fully visible, so give up and scroll the display.
16535 This can happen when the default face uses a font whose
16536 dimensions are different from the frame's default
16537 font. */
16538 if (new_vpos >= w->cursor.y)
16540 w->cursor.vpos = -1;
16541 clear_glyph_matrix (w->desired_matrix);
16542 goto try_to_scroll;
16545 else if (w->cursor.vpos >= 0)
16547 /* Some people insist on not letting point enter the scroll
16548 margin, even though this part handles windows that didn't
16549 scroll at all. */
16550 int window_total_lines
16551 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16552 int margin = min (scroll_margin, window_total_lines / 4);
16553 int pixel_margin = margin * frame_line_height;
16554 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16556 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16557 below, which finds the row to move point to, advances by
16558 the Y coordinate of the _next_ row, see the definition of
16559 MATRIX_ROW_BOTTOM_Y. */
16560 if (w->cursor.vpos < margin + header_line)
16562 w->cursor.vpos = -1;
16563 clear_glyph_matrix (w->desired_matrix);
16564 goto try_to_scroll;
16566 else
16568 int window_height = window_box_height (w);
16570 if (header_line)
16571 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16572 if (w->cursor.y >= window_height - pixel_margin)
16574 w->cursor.vpos = -1;
16575 clear_glyph_matrix (w->desired_matrix);
16576 goto try_to_scroll;
16581 /* If we need to move point for either of the above reasons,
16582 now actually do it. */
16583 if (new_vpos >= 0)
16585 struct glyph_row *row;
16587 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16588 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16589 ++row;
16591 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16592 MATRIX_ROW_START_BYTEPOS (row));
16594 if (w != XWINDOW (selected_window))
16595 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16596 else if (current_buffer == old)
16597 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16599 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16601 /* Re-run pre-redisplay-function so it can update the region
16602 according to the new position of point. */
16603 /* Other than the cursor, w's redisplay is done so we can set its
16604 redisplay to false. Also the buffer's redisplay can be set to
16605 false, since propagate_buffer_redisplay should have already
16606 propagated its info to `w' anyway. */
16607 w->redisplay = false;
16608 XBUFFER (w->contents)->text->redisplay = false;
16609 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16611 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16613 /* pre-redisplay-function made changes (e.g. move the region)
16614 that require another round of redisplay. */
16615 clear_glyph_matrix (w->desired_matrix);
16616 if (!try_window (window, startp, 0))
16617 goto need_larger_matrices;
16620 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16622 clear_glyph_matrix (w->desired_matrix);
16623 goto try_to_scroll;
16626 #ifdef GLYPH_DEBUG
16627 debug_method_add (w, "forced window start");
16628 #endif
16629 goto done;
16632 /* Handle case where text has not changed, only point, and it has
16633 not moved off the frame, and we are not retrying after hscroll.
16634 (current_matrix_up_to_date_p is true when retrying.) */
16635 if (current_matrix_up_to_date_p
16636 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16637 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16639 switch (rc)
16641 case CURSOR_MOVEMENT_SUCCESS:
16642 used_current_matrix_p = true;
16643 goto done;
16645 case CURSOR_MOVEMENT_MUST_SCROLL:
16646 goto try_to_scroll;
16648 default:
16649 emacs_abort ();
16652 /* If current starting point was originally the beginning of a line
16653 but no longer is, find a new starting point. */
16654 else if (w->start_at_line_beg
16655 && !(CHARPOS (startp) <= BEGV
16656 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16658 #ifdef GLYPH_DEBUG
16659 debug_method_add (w, "recenter 1");
16660 #endif
16661 goto recenter;
16664 /* Try scrolling with try_window_id. Value is > 0 if update has
16665 been done, it is -1 if we know that the same window start will
16666 not work. It is 0 if unsuccessful for some other reason. */
16667 else if ((tem = try_window_id (w)) != 0)
16669 #ifdef GLYPH_DEBUG
16670 debug_method_add (w, "try_window_id %d", tem);
16671 #endif
16673 if (f->fonts_changed)
16674 goto need_larger_matrices;
16675 if (tem > 0)
16676 goto done;
16678 /* Otherwise try_window_id has returned -1 which means that we
16679 don't want the alternative below this comment to execute. */
16681 else if (CHARPOS (startp) >= BEGV
16682 && CHARPOS (startp) <= ZV
16683 && PT >= CHARPOS (startp)
16684 && (CHARPOS (startp) < ZV
16685 /* Avoid starting at end of buffer. */
16686 || CHARPOS (startp) == BEGV
16687 || !window_outdated (w)))
16689 int d1, d2, d5, d6;
16690 int rtop, rbot;
16692 /* If first window line is a continuation line, and window start
16693 is inside the modified region, but the first change is before
16694 current window start, we must select a new window start.
16696 However, if this is the result of a down-mouse event (e.g. by
16697 extending the mouse-drag-overlay), we don't want to select a
16698 new window start, since that would change the position under
16699 the mouse, resulting in an unwanted mouse-movement rather
16700 than a simple mouse-click. */
16701 if (!w->start_at_line_beg
16702 && NILP (do_mouse_tracking)
16703 && CHARPOS (startp) > BEGV
16704 && CHARPOS (startp) > BEG + beg_unchanged
16705 && CHARPOS (startp) <= Z - end_unchanged
16706 /* Even if w->start_at_line_beg is nil, a new window may
16707 start at a line_beg, since that's how set_buffer_window
16708 sets it. So, we need to check the return value of
16709 compute_window_start_on_continuation_line. (See also
16710 bug#197). */
16711 && XMARKER (w->start)->buffer == current_buffer
16712 && compute_window_start_on_continuation_line (w)
16713 /* It doesn't make sense to force the window start like we
16714 do at label force_start if it is already known that point
16715 will not be fully visible in the resulting window, because
16716 doing so will move point from its correct position
16717 instead of scrolling the window to bring point into view.
16718 See bug#9324. */
16719 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16720 /* A very tall row could need more than the window height,
16721 in which case we accept that it is partially visible. */
16722 && (rtop != 0) == (rbot != 0))
16724 w->force_start = true;
16725 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16726 #ifdef GLYPH_DEBUG
16727 debug_method_add (w, "recomputed window start in continuation line");
16728 #endif
16729 goto force_start;
16732 #ifdef GLYPH_DEBUG
16733 debug_method_add (w, "same window start");
16734 #endif
16736 /* Try to redisplay starting at same place as before.
16737 If point has not moved off frame, accept the results. */
16738 if (!current_matrix_up_to_date_p
16739 /* Don't use try_window_reusing_current_matrix in this case
16740 because a window scroll function can have changed the
16741 buffer. */
16742 || !NILP (Vwindow_scroll_functions)
16743 || MINI_WINDOW_P (w)
16744 || !(used_current_matrix_p
16745 = try_window_reusing_current_matrix (w)))
16747 IF_DEBUG (debug_method_add (w, "1"));
16748 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16749 /* -1 means we need to scroll.
16750 0 means we need new matrices, but fonts_changed
16751 is set in that case, so we will detect it below. */
16752 goto try_to_scroll;
16755 if (f->fonts_changed)
16756 goto need_larger_matrices;
16758 if (w->cursor.vpos >= 0)
16760 if (!just_this_one_p
16761 || current_buffer->clip_changed
16762 || BEG_UNCHANGED < CHARPOS (startp))
16763 /* Forget any recorded base line for line number display. */
16764 w->base_line_number = 0;
16766 if (!cursor_row_fully_visible_p (w, true, false))
16768 clear_glyph_matrix (w->desired_matrix);
16769 last_line_misfit = true;
16771 /* Drop through and scroll. */
16772 else
16773 goto done;
16775 else
16776 clear_glyph_matrix (w->desired_matrix);
16779 try_to_scroll:
16781 /* Redisplay the mode line. Select the buffer properly for that. */
16782 if (!update_mode_line)
16784 update_mode_line = true;
16785 w->update_mode_line = true;
16788 /* Try to scroll by specified few lines. */
16789 if ((scroll_conservatively
16790 || emacs_scroll_step
16791 || temp_scroll_step
16792 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16793 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16794 && CHARPOS (startp) >= BEGV
16795 && CHARPOS (startp) <= ZV)
16797 /* The function returns -1 if new fonts were loaded, 1 if
16798 successful, 0 if not successful. */
16799 int ss = try_scrolling (window, just_this_one_p,
16800 scroll_conservatively,
16801 emacs_scroll_step,
16802 temp_scroll_step, last_line_misfit);
16803 switch (ss)
16805 case SCROLLING_SUCCESS:
16806 goto done;
16808 case SCROLLING_NEED_LARGER_MATRICES:
16809 goto need_larger_matrices;
16811 case SCROLLING_FAILED:
16812 break;
16814 default:
16815 emacs_abort ();
16819 /* Finally, just choose a place to start which positions point
16820 according to user preferences. */
16822 recenter:
16824 #ifdef GLYPH_DEBUG
16825 debug_method_add (w, "recenter");
16826 #endif
16828 /* Forget any previously recorded base line for line number display. */
16829 if (!buffer_unchanged_p)
16830 w->base_line_number = 0;
16832 /* Determine the window start relative to point. */
16833 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16834 it.current_y = it.last_visible_y;
16835 if (centering_position < 0)
16837 int window_total_lines
16838 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16839 int margin
16840 = scroll_margin > 0
16841 ? min (scroll_margin, window_total_lines / 4)
16842 : 0;
16843 ptrdiff_t margin_pos = CHARPOS (startp);
16844 Lisp_Object aggressive;
16845 bool scrolling_up;
16847 /* If there is a scroll margin at the top of the window, find
16848 its character position. */
16849 if (margin
16850 /* Cannot call start_display if startp is not in the
16851 accessible region of the buffer. This can happen when we
16852 have just switched to a different buffer and/or changed
16853 its restriction. In that case, startp is initialized to
16854 the character position 1 (BEGV) because we did not yet
16855 have chance to display the buffer even once. */
16856 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16858 struct it it1;
16859 void *it1data = NULL;
16861 SAVE_IT (it1, it, it1data);
16862 start_display (&it1, w, startp);
16863 move_it_vertically (&it1, margin * frame_line_height);
16864 margin_pos = IT_CHARPOS (it1);
16865 RESTORE_IT (&it, &it, it1data);
16867 scrolling_up = PT > margin_pos;
16868 aggressive =
16869 scrolling_up
16870 ? BVAR (current_buffer, scroll_up_aggressively)
16871 : BVAR (current_buffer, scroll_down_aggressively);
16873 if (!MINI_WINDOW_P (w)
16874 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16876 int pt_offset = 0;
16878 /* Setting scroll-conservatively overrides
16879 scroll-*-aggressively. */
16880 if (!scroll_conservatively && NUMBERP (aggressive))
16882 double float_amount = XFLOATINT (aggressive);
16884 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16885 if (pt_offset == 0 && float_amount > 0)
16886 pt_offset = 1;
16887 if (pt_offset && margin > 0)
16888 margin -= 1;
16890 /* Compute how much to move the window start backward from
16891 point so that point will be displayed where the user
16892 wants it. */
16893 if (scrolling_up)
16895 centering_position = it.last_visible_y;
16896 if (pt_offset)
16897 centering_position -= pt_offset;
16898 centering_position -=
16899 (frame_line_height * (1 + margin + last_line_misfit)
16900 + WINDOW_HEADER_LINE_HEIGHT (w));
16901 /* Don't let point enter the scroll margin near top of
16902 the window. */
16903 if (centering_position < margin * frame_line_height)
16904 centering_position = margin * frame_line_height;
16906 else
16907 centering_position = margin * frame_line_height + pt_offset;
16909 else
16910 /* Set the window start half the height of the window backward
16911 from point. */
16912 centering_position = window_box_height (w) / 2;
16914 move_it_vertically_backward (&it, centering_position);
16916 eassert (IT_CHARPOS (it) >= BEGV);
16918 /* The function move_it_vertically_backward may move over more
16919 than the specified y-distance. If it->w is small, e.g. a
16920 mini-buffer window, we may end up in front of the window's
16921 display area. Start displaying at the start of the line
16922 containing PT in this case. */
16923 if (it.current_y <= 0)
16925 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16926 move_it_vertically_backward (&it, 0);
16927 it.current_y = 0;
16930 it.current_x = it.hpos = 0;
16932 /* Set the window start position here explicitly, to avoid an
16933 infinite loop in case the functions in window-scroll-functions
16934 get errors. */
16935 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16937 /* Run scroll hooks. */
16938 startp = run_window_scroll_functions (window, it.current.pos);
16940 /* We invoke try_window and try_window_reusing_current_matrix below,
16941 and they manipulate the bidi cache. Save and restore the cache
16942 state of our iterator, so we could continue using it after that. */
16943 itdata = bidi_shelve_cache ();
16945 /* Redisplay the window. */
16946 use_desired_matrix = false;
16947 if (!current_matrix_up_to_date_p
16948 || windows_or_buffers_changed
16949 || f->cursor_type_changed
16950 /* Don't use try_window_reusing_current_matrix in this case
16951 because it can have changed the buffer. */
16952 || !NILP (Vwindow_scroll_functions)
16953 || !just_this_one_p
16954 || MINI_WINDOW_P (w)
16955 || !(used_current_matrix_p
16956 = try_window_reusing_current_matrix (w)))
16957 use_desired_matrix = (try_window (window, startp, 0) == 1);
16959 bidi_unshelve_cache (itdata, false);
16961 /* If new fonts have been loaded (due to fontsets), give up. We
16962 have to start a new redisplay since we need to re-adjust glyph
16963 matrices. */
16964 if (f->fonts_changed)
16965 goto need_larger_matrices;
16967 /* If cursor did not appear assume that the middle of the window is
16968 in the first line of the window. Do it again with the next line.
16969 (Imagine a window of height 100, displaying two lines of height
16970 60. Moving back 50 from it->last_visible_y will end in the first
16971 line.) */
16972 if (w->cursor.vpos < 0)
16974 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16976 clear_glyph_matrix (w->desired_matrix);
16977 move_it_by_lines (&it, 1);
16978 try_window (window, it.current.pos, 0);
16980 else if (PT < IT_CHARPOS (it))
16982 clear_glyph_matrix (w->desired_matrix);
16983 move_it_by_lines (&it, -1);
16984 try_window (window, it.current.pos, 0);
16986 else if (scroll_conservatively > SCROLL_LIMIT
16987 && (it.method == GET_FROM_STRING
16988 || overlay_touches_p (IT_CHARPOS (it)))
16989 && IT_CHARPOS (it) < ZV)
16991 /* If the window starts with a before-string that spans more
16992 than one screen line, using that position to display the
16993 window might fail to bring point into the view, because
16994 start_display will always start by displaying the string,
16995 whereas the code above determines where to set w->start
16996 by the buffer position of the place where it takes screen
16997 coordinates. Try to recover by finding the next screen
16998 line that displays buffer text. */
16999 ptrdiff_t pos0 = IT_CHARPOS (it);
17001 clear_glyph_matrix (w->desired_matrix);
17002 do {
17003 move_it_by_lines (&it, 1);
17004 } while (IT_CHARPOS (it) == pos0);
17005 try_window (window, it.current.pos, 0);
17007 else
17009 /* Not much we can do about it. */
17013 /* Consider the following case: Window starts at BEGV, there is
17014 invisible, intangible text at BEGV, so that display starts at
17015 some point START > BEGV. It can happen that we are called with
17016 PT somewhere between BEGV and START. Try to handle that case,
17017 and similar ones. */
17018 if (w->cursor.vpos < 0)
17020 /* Prefer the desired matrix to the current matrix, if possible,
17021 in the fallback calculations below. This is because using
17022 the current matrix might completely goof, e.g. if its first
17023 row is after point. */
17024 struct glyph_matrix *matrix =
17025 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17026 /* First, try locating the proper glyph row for PT. */
17027 struct glyph_row *row =
17028 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17030 /* Sometimes point is at the beginning of invisible text that is
17031 before the 1st character displayed in the row. In that case,
17032 row_containing_pos fails to find the row, because no glyphs
17033 with appropriate buffer positions are present in the row.
17034 Therefore, we next try to find the row which shows the 1st
17035 position after the invisible text. */
17036 if (!row)
17038 Lisp_Object val =
17039 get_char_property_and_overlay (make_number (PT), Qinvisible,
17040 Qnil, NULL);
17042 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17044 ptrdiff_t alt_pos;
17045 Lisp_Object invis_end =
17046 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17047 Qnil, Qnil);
17049 if (NATNUMP (invis_end))
17050 alt_pos = XFASTINT (invis_end);
17051 else
17052 alt_pos = ZV;
17053 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17056 /* Finally, fall back on the first row of the window after the
17057 header line (if any). This is slightly better than not
17058 displaying the cursor at all. */
17059 if (!row)
17061 row = matrix->rows;
17062 if (row->mode_line_p)
17063 ++row;
17065 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17068 if (!cursor_row_fully_visible_p (w, false, false))
17070 /* If vscroll is enabled, disable it and try again. */
17071 if (w->vscroll)
17073 w->vscroll = 0;
17074 clear_glyph_matrix (w->desired_matrix);
17075 goto recenter;
17078 /* Users who set scroll-conservatively to a large number want
17079 point just above/below the scroll margin. If we ended up
17080 with point's row partially visible, move the window start to
17081 make that row fully visible and out of the margin. */
17082 if (scroll_conservatively > SCROLL_LIMIT)
17084 int window_total_lines
17085 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17086 int margin =
17087 scroll_margin > 0
17088 ? min (scroll_margin, window_total_lines / 4)
17089 : 0;
17090 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17092 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17093 clear_glyph_matrix (w->desired_matrix);
17094 if (1 == try_window (window, it.current.pos,
17095 TRY_WINDOW_CHECK_MARGINS))
17096 goto done;
17099 /* If centering point failed to make the whole line visible,
17100 put point at the top instead. That has to make the whole line
17101 visible, if it can be done. */
17102 if (centering_position == 0)
17103 goto done;
17105 clear_glyph_matrix (w->desired_matrix);
17106 centering_position = 0;
17107 goto recenter;
17110 done:
17112 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17113 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17114 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17116 /* Display the mode line, if we must. */
17117 if ((update_mode_line
17118 /* If window not full width, must redo its mode line
17119 if (a) the window to its side is being redone and
17120 (b) we do a frame-based redisplay. This is a consequence
17121 of how inverted lines are drawn in frame-based redisplay. */
17122 || (!just_this_one_p
17123 && !FRAME_WINDOW_P (f)
17124 && !WINDOW_FULL_WIDTH_P (w))
17125 /* Line number to display. */
17126 || w->base_line_pos > 0
17127 /* Column number is displayed and different from the one displayed. */
17128 || (w->column_number_displayed != -1
17129 && (w->column_number_displayed != current_column ())))
17130 /* This means that the window has a mode line. */
17131 && (WINDOW_WANTS_MODELINE_P (w)
17132 || WINDOW_WANTS_HEADER_LINE_P (w)))
17135 display_mode_lines (w);
17137 /* If mode line height has changed, arrange for a thorough
17138 immediate redisplay using the correct mode line height. */
17139 if (WINDOW_WANTS_MODELINE_P (w)
17140 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17142 f->fonts_changed = true;
17143 w->mode_line_height = -1;
17144 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17145 = DESIRED_MODE_LINE_HEIGHT (w);
17148 /* If header line height has changed, arrange for a thorough
17149 immediate redisplay using the correct header line height. */
17150 if (WINDOW_WANTS_HEADER_LINE_P (w)
17151 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17153 f->fonts_changed = true;
17154 w->header_line_height = -1;
17155 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17156 = DESIRED_HEADER_LINE_HEIGHT (w);
17159 if (f->fonts_changed)
17160 goto need_larger_matrices;
17163 if (!line_number_displayed && w->base_line_pos != -1)
17165 w->base_line_pos = 0;
17166 w->base_line_number = 0;
17169 finish_menu_bars:
17171 /* When we reach a frame's selected window, redo the frame's menu
17172 bar and the frame's title. */
17173 if (update_mode_line
17174 && EQ (FRAME_SELECTED_WINDOW (f), window))
17176 bool redisplay_menu_p;
17178 if (FRAME_WINDOW_P (f))
17180 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17181 || defined (HAVE_NS) || defined (USE_GTK)
17182 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17183 #else
17184 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17185 #endif
17187 else
17188 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17190 if (redisplay_menu_p)
17191 display_menu_bar (w);
17193 #ifdef HAVE_WINDOW_SYSTEM
17194 if (FRAME_WINDOW_P (f))
17196 #if defined (USE_GTK) || defined (HAVE_NS)
17197 if (FRAME_EXTERNAL_TOOL_BAR (f))
17198 redisplay_tool_bar (f);
17199 #else
17200 if (WINDOWP (f->tool_bar_window)
17201 && (FRAME_TOOL_BAR_LINES (f) > 0
17202 || !NILP (Vauto_resize_tool_bars))
17203 && redisplay_tool_bar (f))
17204 ignore_mouse_drag_p = true;
17205 #endif
17207 x_consider_frame_title (w->frame);
17208 #endif
17211 #ifdef HAVE_WINDOW_SYSTEM
17212 if (FRAME_WINDOW_P (f)
17213 && update_window_fringes (w, (just_this_one_p
17214 || (!used_current_matrix_p && !overlay_arrow_seen)
17215 || w->pseudo_window_p)))
17217 update_begin (f);
17218 block_input ();
17219 if (draw_window_fringes (w, true))
17221 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17222 x_draw_right_divider (w);
17223 else
17224 x_draw_vertical_border (w);
17226 unblock_input ();
17227 update_end (f);
17230 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17231 x_draw_bottom_divider (w);
17232 #endif /* HAVE_WINDOW_SYSTEM */
17234 /* We go to this label, with fonts_changed set, if it is
17235 necessary to try again using larger glyph matrices.
17236 We have to redeem the scroll bar even in this case,
17237 because the loop in redisplay_internal expects that. */
17238 need_larger_matrices:
17240 finish_scroll_bars:
17242 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17244 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17245 /* Set the thumb's position and size. */
17246 set_vertical_scroll_bar (w);
17248 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17249 /* Set the thumb's position and size. */
17250 set_horizontal_scroll_bar (w);
17252 /* Note that we actually used the scroll bar attached to this
17253 window, so it shouldn't be deleted at the end of redisplay. */
17254 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17255 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17258 /* Restore current_buffer and value of point in it. The window
17259 update may have changed the buffer, so first make sure `opoint'
17260 is still valid (Bug#6177). */
17261 if (CHARPOS (opoint) < BEGV)
17262 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17263 else if (CHARPOS (opoint) > ZV)
17264 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17265 else
17266 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17268 set_buffer_internal_1 (old);
17269 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17270 shorter. This can be caused by log truncation in *Messages*. */
17271 if (CHARPOS (lpoint) <= ZV)
17272 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17274 unbind_to (count, Qnil);
17278 /* Build the complete desired matrix of WINDOW with a window start
17279 buffer position POS.
17281 Value is 1 if successful. It is zero if fonts were loaded during
17282 redisplay which makes re-adjusting glyph matrices necessary, and -1
17283 if point would appear in the scroll margins.
17284 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17285 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17286 set in FLAGS.) */
17289 try_window (Lisp_Object window, struct text_pos pos, int flags)
17291 struct window *w = XWINDOW (window);
17292 struct it it;
17293 struct glyph_row *last_text_row = NULL;
17294 struct frame *f = XFRAME (w->frame);
17295 int frame_line_height = default_line_pixel_height (w);
17297 /* Make POS the new window start. */
17298 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17300 /* Mark cursor position as unknown. No overlay arrow seen. */
17301 w->cursor.vpos = -1;
17302 overlay_arrow_seen = false;
17304 /* Initialize iterator and info to start at POS. */
17305 start_display (&it, w, pos);
17306 it.glyph_row->reversed_p = false;
17308 /* Display all lines of W. */
17309 while (it.current_y < it.last_visible_y)
17311 if (display_line (&it))
17312 last_text_row = it.glyph_row - 1;
17313 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17314 return 0;
17317 /* Don't let the cursor end in the scroll margins. */
17318 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17319 && !MINI_WINDOW_P (w))
17321 int this_scroll_margin;
17322 int window_total_lines
17323 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17325 if (scroll_margin > 0)
17327 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17328 this_scroll_margin *= frame_line_height;
17330 else
17331 this_scroll_margin = 0;
17333 if ((w->cursor.y >= 0 /* not vscrolled */
17334 && w->cursor.y < this_scroll_margin
17335 && CHARPOS (pos) > BEGV
17336 && IT_CHARPOS (it) < ZV)
17337 /* rms: considering make_cursor_line_fully_visible_p here
17338 seems to give wrong results. We don't want to recenter
17339 when the last line is partly visible, we want to allow
17340 that case to be handled in the usual way. */
17341 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17343 w->cursor.vpos = -1;
17344 clear_glyph_matrix (w->desired_matrix);
17345 return -1;
17349 /* If bottom moved off end of frame, change mode line percentage. */
17350 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17351 w->update_mode_line = true;
17353 /* Set window_end_pos to the offset of the last character displayed
17354 on the window from the end of current_buffer. Set
17355 window_end_vpos to its row number. */
17356 if (last_text_row)
17358 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17359 adjust_window_ends (w, last_text_row, false);
17360 eassert
17361 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17362 w->window_end_vpos)));
17364 else
17366 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17367 w->window_end_pos = Z - ZV;
17368 w->window_end_vpos = 0;
17371 /* But that is not valid info until redisplay finishes. */
17372 w->window_end_valid = false;
17373 return 1;
17378 /************************************************************************
17379 Window redisplay reusing current matrix when buffer has not changed
17380 ************************************************************************/
17382 /* Try redisplay of window W showing an unchanged buffer with a
17383 different window start than the last time it was displayed by
17384 reusing its current matrix. Value is true if successful.
17385 W->start is the new window start. */
17387 static bool
17388 try_window_reusing_current_matrix (struct window *w)
17390 struct frame *f = XFRAME (w->frame);
17391 struct glyph_row *bottom_row;
17392 struct it it;
17393 struct run run;
17394 struct text_pos start, new_start;
17395 int nrows_scrolled, i;
17396 struct glyph_row *last_text_row;
17397 struct glyph_row *last_reused_text_row;
17398 struct glyph_row *start_row;
17399 int start_vpos, min_y, max_y;
17401 #ifdef GLYPH_DEBUG
17402 if (inhibit_try_window_reusing)
17403 return false;
17404 #endif
17406 if (/* This function doesn't handle terminal frames. */
17407 !FRAME_WINDOW_P (f)
17408 /* Don't try to reuse the display if windows have been split
17409 or such. */
17410 || windows_or_buffers_changed
17411 || f->cursor_type_changed)
17412 return false;
17414 /* Can't do this if showing trailing whitespace. */
17415 if (!NILP (Vshow_trailing_whitespace))
17416 return false;
17418 /* If top-line visibility has changed, give up. */
17419 if (WINDOW_WANTS_HEADER_LINE_P (w)
17420 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17421 return false;
17423 /* Give up if old or new display is scrolled vertically. We could
17424 make this function handle this, but right now it doesn't. */
17425 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17426 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17427 return false;
17429 /* The variable new_start now holds the new window start. The old
17430 start `start' can be determined from the current matrix. */
17431 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17432 start = start_row->minpos;
17433 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17435 /* Clear the desired matrix for the display below. */
17436 clear_glyph_matrix (w->desired_matrix);
17438 if (CHARPOS (new_start) <= CHARPOS (start))
17440 /* Don't use this method if the display starts with an ellipsis
17441 displayed for invisible text. It's not easy to handle that case
17442 below, and it's certainly not worth the effort since this is
17443 not a frequent case. */
17444 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17445 return false;
17447 IF_DEBUG (debug_method_add (w, "twu1"));
17449 /* Display up to a row that can be reused. The variable
17450 last_text_row is set to the last row displayed that displays
17451 text. Note that it.vpos == 0 if or if not there is a
17452 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17453 start_display (&it, w, new_start);
17454 w->cursor.vpos = -1;
17455 last_text_row = last_reused_text_row = NULL;
17457 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17459 /* If we have reached into the characters in the START row,
17460 that means the line boundaries have changed. So we
17461 can't start copying with the row START. Maybe it will
17462 work to start copying with the following row. */
17463 while (IT_CHARPOS (it) > CHARPOS (start))
17465 /* Advance to the next row as the "start". */
17466 start_row++;
17467 start = start_row->minpos;
17468 /* If there are no more rows to try, or just one, give up. */
17469 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17470 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17471 || CHARPOS (start) == ZV)
17473 clear_glyph_matrix (w->desired_matrix);
17474 return false;
17477 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17479 /* If we have reached alignment, we can copy the rest of the
17480 rows. */
17481 if (IT_CHARPOS (it) == CHARPOS (start)
17482 /* Don't accept "alignment" inside a display vector,
17483 since start_row could have started in the middle of
17484 that same display vector (thus their character
17485 positions match), and we have no way of telling if
17486 that is the case. */
17487 && it.current.dpvec_index < 0)
17488 break;
17490 it.glyph_row->reversed_p = false;
17491 if (display_line (&it))
17492 last_text_row = it.glyph_row - 1;
17496 /* A value of current_y < last_visible_y means that we stopped
17497 at the previous window start, which in turn means that we
17498 have at least one reusable row. */
17499 if (it.current_y < it.last_visible_y)
17501 struct glyph_row *row;
17503 /* IT.vpos always starts from 0; it counts text lines. */
17504 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17506 /* Find PT if not already found in the lines displayed. */
17507 if (w->cursor.vpos < 0)
17509 int dy = it.current_y - start_row->y;
17511 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17512 row = row_containing_pos (w, PT, row, NULL, dy);
17513 if (row)
17514 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17515 dy, nrows_scrolled);
17516 else
17518 clear_glyph_matrix (w->desired_matrix);
17519 return false;
17523 /* Scroll the display. Do it before the current matrix is
17524 changed. The problem here is that update has not yet
17525 run, i.e. part of the current matrix is not up to date.
17526 scroll_run_hook will clear the cursor, and use the
17527 current matrix to get the height of the row the cursor is
17528 in. */
17529 run.current_y = start_row->y;
17530 run.desired_y = it.current_y;
17531 run.height = it.last_visible_y - it.current_y;
17533 if (run.height > 0 && run.current_y != run.desired_y)
17535 update_begin (f);
17536 FRAME_RIF (f)->update_window_begin_hook (w);
17537 FRAME_RIF (f)->clear_window_mouse_face (w);
17538 FRAME_RIF (f)->scroll_run_hook (w, &run);
17539 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17540 update_end (f);
17543 /* Shift current matrix down by nrows_scrolled lines. */
17544 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17545 rotate_matrix (w->current_matrix,
17546 start_vpos,
17547 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17548 nrows_scrolled);
17550 /* Disable lines that must be updated. */
17551 for (i = 0; i < nrows_scrolled; ++i)
17552 (start_row + i)->enabled_p = false;
17554 /* Re-compute Y positions. */
17555 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17556 max_y = it.last_visible_y;
17557 for (row = start_row + nrows_scrolled;
17558 row < bottom_row;
17559 ++row)
17561 row->y = it.current_y;
17562 row->visible_height = row->height;
17564 if (row->y < min_y)
17565 row->visible_height -= min_y - row->y;
17566 if (row->y + row->height > max_y)
17567 row->visible_height -= row->y + row->height - max_y;
17568 if (row->fringe_bitmap_periodic_p)
17569 row->redraw_fringe_bitmaps_p = true;
17571 it.current_y += row->height;
17573 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17574 last_reused_text_row = row;
17575 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17576 break;
17579 /* Disable lines in the current matrix which are now
17580 below the window. */
17581 for (++row; row < bottom_row; ++row)
17582 row->enabled_p = row->mode_line_p = false;
17585 /* Update window_end_pos etc.; last_reused_text_row is the last
17586 reused row from the current matrix containing text, if any.
17587 The value of last_text_row is the last displayed line
17588 containing text. */
17589 if (last_reused_text_row)
17590 adjust_window_ends (w, last_reused_text_row, true);
17591 else if (last_text_row)
17592 adjust_window_ends (w, last_text_row, false);
17593 else
17595 /* This window must be completely empty. */
17596 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17597 w->window_end_pos = Z - ZV;
17598 w->window_end_vpos = 0;
17600 w->window_end_valid = false;
17602 /* Update hint: don't try scrolling again in update_window. */
17603 w->desired_matrix->no_scrolling_p = true;
17605 #ifdef GLYPH_DEBUG
17606 debug_method_add (w, "try_window_reusing_current_matrix 1");
17607 #endif
17608 return true;
17610 else if (CHARPOS (new_start) > CHARPOS (start))
17612 struct glyph_row *pt_row, *row;
17613 struct glyph_row *first_reusable_row;
17614 struct glyph_row *first_row_to_display;
17615 int dy;
17616 int yb = window_text_bottom_y (w);
17618 /* Find the row starting at new_start, if there is one. Don't
17619 reuse a partially visible line at the end. */
17620 first_reusable_row = start_row;
17621 while (first_reusable_row->enabled_p
17622 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17623 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17624 < CHARPOS (new_start)))
17625 ++first_reusable_row;
17627 /* Give up if there is no row to reuse. */
17628 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17629 || !first_reusable_row->enabled_p
17630 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17631 != CHARPOS (new_start)))
17632 return false;
17634 /* We can reuse fully visible rows beginning with
17635 first_reusable_row to the end of the window. Set
17636 first_row_to_display to the first row that cannot be reused.
17637 Set pt_row to the row containing point, if there is any. */
17638 pt_row = NULL;
17639 for (first_row_to_display = first_reusable_row;
17640 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17641 ++first_row_to_display)
17643 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17644 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17645 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17646 && first_row_to_display->ends_at_zv_p
17647 && pt_row == NULL)))
17648 pt_row = first_row_to_display;
17651 /* Start displaying at the start of first_row_to_display. */
17652 eassert (first_row_to_display->y < yb);
17653 init_to_row_start (&it, w, first_row_to_display);
17655 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17656 - start_vpos);
17657 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17658 - nrows_scrolled);
17659 it.current_y = (first_row_to_display->y - first_reusable_row->y
17660 + WINDOW_HEADER_LINE_HEIGHT (w));
17662 /* Display lines beginning with first_row_to_display in the
17663 desired matrix. Set last_text_row to the last row displayed
17664 that displays text. */
17665 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17666 if (pt_row == NULL)
17667 w->cursor.vpos = -1;
17668 last_text_row = NULL;
17669 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17670 if (display_line (&it))
17671 last_text_row = it.glyph_row - 1;
17673 /* If point is in a reused row, adjust y and vpos of the cursor
17674 position. */
17675 if (pt_row)
17677 w->cursor.vpos -= nrows_scrolled;
17678 w->cursor.y -= first_reusable_row->y - start_row->y;
17681 /* Give up if point isn't in a row displayed or reused. (This
17682 also handles the case where w->cursor.vpos < nrows_scrolled
17683 after the calls to display_line, which can happen with scroll
17684 margins. See bug#1295.) */
17685 if (w->cursor.vpos < 0)
17687 clear_glyph_matrix (w->desired_matrix);
17688 return false;
17691 /* Scroll the display. */
17692 run.current_y = first_reusable_row->y;
17693 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17694 run.height = it.last_visible_y - run.current_y;
17695 dy = run.current_y - run.desired_y;
17697 if (run.height)
17699 update_begin (f);
17700 FRAME_RIF (f)->update_window_begin_hook (w);
17701 FRAME_RIF (f)->clear_window_mouse_face (w);
17702 FRAME_RIF (f)->scroll_run_hook (w, &run);
17703 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17704 update_end (f);
17707 /* Adjust Y positions of reused rows. */
17708 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17709 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17710 max_y = it.last_visible_y;
17711 for (row = first_reusable_row; row < first_row_to_display; ++row)
17713 row->y -= dy;
17714 row->visible_height = row->height;
17715 if (row->y < min_y)
17716 row->visible_height -= min_y - row->y;
17717 if (row->y + row->height > max_y)
17718 row->visible_height -= row->y + row->height - max_y;
17719 if (row->fringe_bitmap_periodic_p)
17720 row->redraw_fringe_bitmaps_p = true;
17723 /* Scroll the current matrix. */
17724 eassert (nrows_scrolled > 0);
17725 rotate_matrix (w->current_matrix,
17726 start_vpos,
17727 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17728 -nrows_scrolled);
17730 /* Disable rows not reused. */
17731 for (row -= nrows_scrolled; row < bottom_row; ++row)
17732 row->enabled_p = false;
17734 /* Point may have moved to a different line, so we cannot assume that
17735 the previous cursor position is valid; locate the correct row. */
17736 if (pt_row)
17738 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17739 row < bottom_row
17740 && PT >= MATRIX_ROW_END_CHARPOS (row)
17741 && !row->ends_at_zv_p;
17742 row++)
17744 w->cursor.vpos++;
17745 w->cursor.y = row->y;
17747 if (row < bottom_row)
17749 /* Can't simply scan the row for point with
17750 bidi-reordered glyph rows. Let set_cursor_from_row
17751 figure out where to put the cursor, and if it fails,
17752 give up. */
17753 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17755 if (!set_cursor_from_row (w, row, w->current_matrix,
17756 0, 0, 0, 0))
17758 clear_glyph_matrix (w->desired_matrix);
17759 return false;
17762 else
17764 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17765 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17767 for (; glyph < end
17768 && (!BUFFERP (glyph->object)
17769 || glyph->charpos < PT);
17770 glyph++)
17772 w->cursor.hpos++;
17773 w->cursor.x += glyph->pixel_width;
17779 /* Adjust window end. A null value of last_text_row means that
17780 the window end is in reused rows which in turn means that
17781 only its vpos can have changed. */
17782 if (last_text_row)
17783 adjust_window_ends (w, last_text_row, false);
17784 else
17785 w->window_end_vpos -= nrows_scrolled;
17787 w->window_end_valid = false;
17788 w->desired_matrix->no_scrolling_p = true;
17790 #ifdef GLYPH_DEBUG
17791 debug_method_add (w, "try_window_reusing_current_matrix 2");
17792 #endif
17793 return true;
17796 return false;
17801 /************************************************************************
17802 Window redisplay reusing current matrix when buffer has changed
17803 ************************************************************************/
17805 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17806 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17807 ptrdiff_t *, ptrdiff_t *);
17808 static struct glyph_row *
17809 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17810 struct glyph_row *);
17813 /* Return the last row in MATRIX displaying text. If row START is
17814 non-null, start searching with that row. IT gives the dimensions
17815 of the display. Value is null if matrix is empty; otherwise it is
17816 a pointer to the row found. */
17818 static struct glyph_row *
17819 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17820 struct glyph_row *start)
17822 struct glyph_row *row, *row_found;
17824 /* Set row_found to the last row in IT->w's current matrix
17825 displaying text. The loop looks funny but think of partially
17826 visible lines. */
17827 row_found = NULL;
17828 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17829 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17831 eassert (row->enabled_p);
17832 row_found = row;
17833 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17834 break;
17835 ++row;
17838 return row_found;
17842 /* Return the last row in the current matrix of W that is not affected
17843 by changes at the start of current_buffer that occurred since W's
17844 current matrix was built. Value is null if no such row exists.
17846 BEG_UNCHANGED us the number of characters unchanged at the start of
17847 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17848 first changed character in current_buffer. Characters at positions <
17849 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17850 when the current matrix was built. */
17852 static struct glyph_row *
17853 find_last_unchanged_at_beg_row (struct window *w)
17855 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17856 struct glyph_row *row;
17857 struct glyph_row *row_found = NULL;
17858 int yb = window_text_bottom_y (w);
17860 /* Find the last row displaying unchanged text. */
17861 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17862 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17863 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17864 ++row)
17866 if (/* If row ends before first_changed_pos, it is unchanged,
17867 except in some case. */
17868 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17869 /* When row ends in ZV and we write at ZV it is not
17870 unchanged. */
17871 && !row->ends_at_zv_p
17872 /* When first_changed_pos is the end of a continued line,
17873 row is not unchanged because it may be no longer
17874 continued. */
17875 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17876 && (row->continued_p
17877 || row->exact_window_width_line_p))
17878 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17879 needs to be recomputed, so don't consider this row as
17880 unchanged. This happens when the last line was
17881 bidi-reordered and was killed immediately before this
17882 redisplay cycle. In that case, ROW->end stores the
17883 buffer position of the first visual-order character of
17884 the killed text, which is now beyond ZV. */
17885 && CHARPOS (row->end.pos) <= ZV)
17886 row_found = row;
17888 /* Stop if last visible row. */
17889 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17890 break;
17893 return row_found;
17897 /* Find the first glyph row in the current matrix of W that is not
17898 affected by changes at the end of current_buffer since the
17899 time W's current matrix was built.
17901 Return in *DELTA the number of chars by which buffer positions in
17902 unchanged text at the end of current_buffer must be adjusted.
17904 Return in *DELTA_BYTES the corresponding number of bytes.
17906 Value is null if no such row exists, i.e. all rows are affected by
17907 changes. */
17909 static struct glyph_row *
17910 find_first_unchanged_at_end_row (struct window *w,
17911 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17913 struct glyph_row *row;
17914 struct glyph_row *row_found = NULL;
17916 *delta = *delta_bytes = 0;
17918 /* Display must not have been paused, otherwise the current matrix
17919 is not up to date. */
17920 eassert (w->window_end_valid);
17922 /* A value of window_end_pos >= END_UNCHANGED means that the window
17923 end is in the range of changed text. If so, there is no
17924 unchanged row at the end of W's current matrix. */
17925 if (w->window_end_pos >= END_UNCHANGED)
17926 return NULL;
17928 /* Set row to the last row in W's current matrix displaying text. */
17929 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17931 /* If matrix is entirely empty, no unchanged row exists. */
17932 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17934 /* The value of row is the last glyph row in the matrix having a
17935 meaningful buffer position in it. The end position of row
17936 corresponds to window_end_pos. This allows us to translate
17937 buffer positions in the current matrix to current buffer
17938 positions for characters not in changed text. */
17939 ptrdiff_t Z_old =
17940 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17941 ptrdiff_t Z_BYTE_old =
17942 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17943 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17944 struct glyph_row *first_text_row
17945 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17947 *delta = Z - Z_old;
17948 *delta_bytes = Z_BYTE - Z_BYTE_old;
17950 /* Set last_unchanged_pos to the buffer position of the last
17951 character in the buffer that has not been changed. Z is the
17952 index + 1 of the last character in current_buffer, i.e. by
17953 subtracting END_UNCHANGED we get the index of the last
17954 unchanged character, and we have to add BEG to get its buffer
17955 position. */
17956 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17957 last_unchanged_pos_old = last_unchanged_pos - *delta;
17959 /* Search backward from ROW for a row displaying a line that
17960 starts at a minimum position >= last_unchanged_pos_old. */
17961 for (; row > first_text_row; --row)
17963 /* This used to abort, but it can happen.
17964 It is ok to just stop the search instead here. KFS. */
17965 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17966 break;
17968 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17969 row_found = row;
17973 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17975 return row_found;
17979 /* Make sure that glyph rows in the current matrix of window W
17980 reference the same glyph memory as corresponding rows in the
17981 frame's frame matrix. This function is called after scrolling W's
17982 current matrix on a terminal frame in try_window_id and
17983 try_window_reusing_current_matrix. */
17985 static void
17986 sync_frame_with_window_matrix_rows (struct window *w)
17988 struct frame *f = XFRAME (w->frame);
17989 struct glyph_row *window_row, *window_row_end, *frame_row;
17991 /* Preconditions: W must be a leaf window and full-width. Its frame
17992 must have a frame matrix. */
17993 eassert (BUFFERP (w->contents));
17994 eassert (WINDOW_FULL_WIDTH_P (w));
17995 eassert (!FRAME_WINDOW_P (f));
17997 /* If W is a full-width window, glyph pointers in W's current matrix
17998 have, by definition, to be the same as glyph pointers in the
17999 corresponding frame matrix. Note that frame matrices have no
18000 marginal areas (see build_frame_matrix). */
18001 window_row = w->current_matrix->rows;
18002 window_row_end = window_row + w->current_matrix->nrows;
18003 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18004 while (window_row < window_row_end)
18006 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18007 struct glyph *end = window_row->glyphs[LAST_AREA];
18009 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18010 frame_row->glyphs[TEXT_AREA] = start;
18011 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18012 frame_row->glyphs[LAST_AREA] = end;
18014 /* Disable frame rows whose corresponding window rows have
18015 been disabled in try_window_id. */
18016 if (!window_row->enabled_p)
18017 frame_row->enabled_p = false;
18019 ++window_row, ++frame_row;
18024 /* Find the glyph row in window W containing CHARPOS. Consider all
18025 rows between START and END (not inclusive). END null means search
18026 all rows to the end of the display area of W. Value is the row
18027 containing CHARPOS or null. */
18029 struct glyph_row *
18030 row_containing_pos (struct window *w, ptrdiff_t charpos,
18031 struct glyph_row *start, struct glyph_row *end, int dy)
18033 struct glyph_row *row = start;
18034 struct glyph_row *best_row = NULL;
18035 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18036 int last_y;
18038 /* If we happen to start on a header-line, skip that. */
18039 if (row->mode_line_p)
18040 ++row;
18042 if ((end && row >= end) || !row->enabled_p)
18043 return NULL;
18045 last_y = window_text_bottom_y (w) - dy;
18047 while (true)
18049 /* Give up if we have gone too far. */
18050 if ((end && row >= end) || !row->enabled_p)
18051 return NULL;
18052 /* This formerly returned if they were equal.
18053 I think that both quantities are of a "last plus one" type;
18054 if so, when they are equal, the row is within the screen. -- rms. */
18055 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18056 return NULL;
18058 /* If it is in this row, return this row. */
18059 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18060 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18061 /* The end position of a row equals the start
18062 position of the next row. If CHARPOS is there, we
18063 would rather consider it displayed in the next
18064 line, except when this line ends in ZV. */
18065 && !row_for_charpos_p (row, charpos)))
18066 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18068 struct glyph *g;
18070 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18071 || (!best_row && !row->continued_p))
18072 return row;
18073 /* In bidi-reordered rows, there could be several rows whose
18074 edges surround CHARPOS, all of these rows belonging to
18075 the same continued line. We need to find the row which
18076 fits CHARPOS the best. */
18077 for (g = row->glyphs[TEXT_AREA];
18078 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18079 g++)
18081 if (!STRINGP (g->object))
18083 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18085 mindif = eabs (g->charpos - charpos);
18086 best_row = row;
18087 /* Exact match always wins. */
18088 if (mindif == 0)
18089 return best_row;
18094 else if (best_row && !row->continued_p)
18095 return best_row;
18096 ++row;
18101 /* Try to redisplay window W by reusing its existing display. W's
18102 current matrix must be up to date when this function is called,
18103 i.e., window_end_valid must be true.
18105 Value is
18107 >= 1 if successful, i.e. display has been updated
18108 specifically:
18109 1 means the changes were in front of a newline that precedes
18110 the window start, and the whole current matrix was reused
18111 2 means the changes were after the last position displayed
18112 in the window, and the whole current matrix was reused
18113 3 means portions of the current matrix were reused, while
18114 some of the screen lines were redrawn
18115 -1 if redisplay with same window start is known not to succeed
18116 0 if otherwise unsuccessful
18118 The following steps are performed:
18120 1. Find the last row in the current matrix of W that is not
18121 affected by changes at the start of current_buffer. If no such row
18122 is found, give up.
18124 2. Find the first row in W's current matrix that is not affected by
18125 changes at the end of current_buffer. Maybe there is no such row.
18127 3. Display lines beginning with the row + 1 found in step 1 to the
18128 row found in step 2 or, if step 2 didn't find a row, to the end of
18129 the window.
18131 4. If cursor is not known to appear on the window, give up.
18133 5. If display stopped at the row found in step 2, scroll the
18134 display and current matrix as needed.
18136 6. Maybe display some lines at the end of W, if we must. This can
18137 happen under various circumstances, like a partially visible line
18138 becoming fully visible, or because newly displayed lines are displayed
18139 in smaller font sizes.
18141 7. Update W's window end information. */
18143 static int
18144 try_window_id (struct window *w)
18146 struct frame *f = XFRAME (w->frame);
18147 struct glyph_matrix *current_matrix = w->current_matrix;
18148 struct glyph_matrix *desired_matrix = w->desired_matrix;
18149 struct glyph_row *last_unchanged_at_beg_row;
18150 struct glyph_row *first_unchanged_at_end_row;
18151 struct glyph_row *row;
18152 struct glyph_row *bottom_row;
18153 int bottom_vpos;
18154 struct it it;
18155 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18156 int dvpos, dy;
18157 struct text_pos start_pos;
18158 struct run run;
18159 int first_unchanged_at_end_vpos = 0;
18160 struct glyph_row *last_text_row, *last_text_row_at_end;
18161 struct text_pos start;
18162 ptrdiff_t first_changed_charpos, last_changed_charpos;
18164 #ifdef GLYPH_DEBUG
18165 if (inhibit_try_window_id)
18166 return 0;
18167 #endif
18169 /* This is handy for debugging. */
18170 #if false
18171 #define GIVE_UP(X) \
18172 do { \
18173 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18174 return 0; \
18175 } while (false)
18176 #else
18177 #define GIVE_UP(X) return 0
18178 #endif
18180 SET_TEXT_POS_FROM_MARKER (start, w->start);
18182 /* Don't use this for mini-windows because these can show
18183 messages and mini-buffers, and we don't handle that here. */
18184 if (MINI_WINDOW_P (w))
18185 GIVE_UP (1);
18187 /* This flag is used to prevent redisplay optimizations. */
18188 if (windows_or_buffers_changed || f->cursor_type_changed)
18189 GIVE_UP (2);
18191 /* This function's optimizations cannot be used if overlays have
18192 changed in the buffer displayed by the window, so give up if they
18193 have. */
18194 if (w->last_overlay_modified != OVERLAY_MODIFF)
18195 GIVE_UP (200);
18197 /* Verify that narrowing has not changed.
18198 Also verify that we were not told to prevent redisplay optimizations.
18199 It would be nice to further
18200 reduce the number of cases where this prevents try_window_id. */
18201 if (current_buffer->clip_changed
18202 || current_buffer->prevent_redisplay_optimizations_p)
18203 GIVE_UP (3);
18205 /* Window must either use window-based redisplay or be full width. */
18206 if (!FRAME_WINDOW_P (f)
18207 && (!FRAME_LINE_INS_DEL_OK (f)
18208 || !WINDOW_FULL_WIDTH_P (w)))
18209 GIVE_UP (4);
18211 /* Give up if point is known NOT to appear in W. */
18212 if (PT < CHARPOS (start))
18213 GIVE_UP (5);
18215 /* Another way to prevent redisplay optimizations. */
18216 if (w->last_modified == 0)
18217 GIVE_UP (6);
18219 /* Verify that window is not hscrolled. */
18220 if (w->hscroll != 0)
18221 GIVE_UP (7);
18223 /* Verify that display wasn't paused. */
18224 if (!w->window_end_valid)
18225 GIVE_UP (8);
18227 /* Likewise if highlighting trailing whitespace. */
18228 if (!NILP (Vshow_trailing_whitespace))
18229 GIVE_UP (11);
18231 /* Can't use this if overlay arrow position and/or string have
18232 changed. */
18233 if (overlay_arrows_changed_p ())
18234 GIVE_UP (12);
18236 /* When word-wrap is on, adding a space to the first word of a
18237 wrapped line can change the wrap position, altering the line
18238 above it. It might be worthwhile to handle this more
18239 intelligently, but for now just redisplay from scratch. */
18240 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18241 GIVE_UP (21);
18243 /* Under bidi reordering, adding or deleting a character in the
18244 beginning of a paragraph, before the first strong directional
18245 character, can change the base direction of the paragraph (unless
18246 the buffer specifies a fixed paragraph direction), which will
18247 require redisplaying the whole paragraph. It might be worthwhile
18248 to find the paragraph limits and widen the range of redisplayed
18249 lines to that, but for now just give up this optimization and
18250 redisplay from scratch. */
18251 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18252 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18253 GIVE_UP (22);
18255 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18256 to that variable require thorough redisplay. */
18257 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18258 GIVE_UP (23);
18260 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18261 only if buffer has really changed. The reason is that the gap is
18262 initially at Z for freshly visited files. The code below would
18263 set end_unchanged to 0 in that case. */
18264 if (MODIFF > SAVE_MODIFF
18265 /* This seems to happen sometimes after saving a buffer. */
18266 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18268 if (GPT - BEG < BEG_UNCHANGED)
18269 BEG_UNCHANGED = GPT - BEG;
18270 if (Z - GPT < END_UNCHANGED)
18271 END_UNCHANGED = Z - GPT;
18274 /* The position of the first and last character that has been changed. */
18275 first_changed_charpos = BEG + BEG_UNCHANGED;
18276 last_changed_charpos = Z - END_UNCHANGED;
18278 /* If window starts after a line end, and the last change is in
18279 front of that newline, then changes don't affect the display.
18280 This case happens with stealth-fontification. Note that although
18281 the display is unchanged, glyph positions in the matrix have to
18282 be adjusted, of course. */
18283 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18284 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18285 && ((last_changed_charpos < CHARPOS (start)
18286 && CHARPOS (start) == BEGV)
18287 || (last_changed_charpos < CHARPOS (start) - 1
18288 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18290 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18291 struct glyph_row *r0;
18293 /* Compute how many chars/bytes have been added to or removed
18294 from the buffer. */
18295 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18296 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18297 Z_delta = Z - Z_old;
18298 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18300 /* Give up if PT is not in the window. Note that it already has
18301 been checked at the start of try_window_id that PT is not in
18302 front of the window start. */
18303 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18304 GIVE_UP (13);
18306 /* If window start is unchanged, we can reuse the whole matrix
18307 as is, after adjusting glyph positions. No need to compute
18308 the window end again, since its offset from Z hasn't changed. */
18309 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18310 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18311 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18312 /* PT must not be in a partially visible line. */
18313 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18314 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18316 /* Adjust positions in the glyph matrix. */
18317 if (Z_delta || Z_delta_bytes)
18319 struct glyph_row *r1
18320 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18321 increment_matrix_positions (w->current_matrix,
18322 MATRIX_ROW_VPOS (r0, current_matrix),
18323 MATRIX_ROW_VPOS (r1, current_matrix),
18324 Z_delta, Z_delta_bytes);
18327 /* Set the cursor. */
18328 row = row_containing_pos (w, PT, r0, NULL, 0);
18329 if (row)
18330 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18331 return 1;
18335 /* Handle the case that changes are all below what is displayed in
18336 the window, and that PT is in the window. This shortcut cannot
18337 be taken if ZV is visible in the window, and text has been added
18338 there that is visible in the window. */
18339 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18340 /* ZV is not visible in the window, or there are no
18341 changes at ZV, actually. */
18342 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18343 || first_changed_charpos == last_changed_charpos))
18345 struct glyph_row *r0;
18347 /* Give up if PT is not in the window. Note that it already has
18348 been checked at the start of try_window_id that PT is not in
18349 front of the window start. */
18350 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18351 GIVE_UP (14);
18353 /* If window start is unchanged, we can reuse the whole matrix
18354 as is, without changing glyph positions since no text has
18355 been added/removed in front of the window end. */
18356 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18357 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18358 /* PT must not be in a partially visible line. */
18359 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18360 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18362 /* We have to compute the window end anew since text
18363 could have been added/removed after it. */
18364 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18365 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18367 /* Set the cursor. */
18368 row = row_containing_pos (w, PT, r0, NULL, 0);
18369 if (row)
18370 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18371 return 2;
18375 /* Give up if window start is in the changed area.
18377 The condition used to read
18379 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18381 but why that was tested escapes me at the moment. */
18382 if (CHARPOS (start) >= first_changed_charpos
18383 && CHARPOS (start) <= last_changed_charpos)
18384 GIVE_UP (15);
18386 /* Check that window start agrees with the start of the first glyph
18387 row in its current matrix. Check this after we know the window
18388 start is not in changed text, otherwise positions would not be
18389 comparable. */
18390 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18391 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18392 GIVE_UP (16);
18394 /* Give up if the window ends in strings. Overlay strings
18395 at the end are difficult to handle, so don't try. */
18396 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18397 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18398 GIVE_UP (20);
18400 /* Compute the position at which we have to start displaying new
18401 lines. Some of the lines at the top of the window might be
18402 reusable because they are not displaying changed text. Find the
18403 last row in W's current matrix not affected by changes at the
18404 start of current_buffer. Value is null if changes start in the
18405 first line of window. */
18406 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18407 if (last_unchanged_at_beg_row)
18409 /* Avoid starting to display in the middle of a character, a TAB
18410 for instance. This is easier than to set up the iterator
18411 exactly, and it's not a frequent case, so the additional
18412 effort wouldn't really pay off. */
18413 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18414 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18415 && last_unchanged_at_beg_row > w->current_matrix->rows)
18416 --last_unchanged_at_beg_row;
18418 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18419 GIVE_UP (17);
18421 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18422 GIVE_UP (18);
18423 start_pos = it.current.pos;
18425 /* Start displaying new lines in the desired matrix at the same
18426 vpos we would use in the current matrix, i.e. below
18427 last_unchanged_at_beg_row. */
18428 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18429 current_matrix);
18430 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18431 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18433 eassert (it.hpos == 0 && it.current_x == 0);
18435 else
18437 /* There are no reusable lines at the start of the window.
18438 Start displaying in the first text line. */
18439 start_display (&it, w, start);
18440 it.vpos = it.first_vpos;
18441 start_pos = it.current.pos;
18444 /* Find the first row that is not affected by changes at the end of
18445 the buffer. Value will be null if there is no unchanged row, in
18446 which case we must redisplay to the end of the window. delta
18447 will be set to the value by which buffer positions beginning with
18448 first_unchanged_at_end_row have to be adjusted due to text
18449 changes. */
18450 first_unchanged_at_end_row
18451 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18452 IF_DEBUG (debug_delta = delta);
18453 IF_DEBUG (debug_delta_bytes = delta_bytes);
18455 /* Set stop_pos to the buffer position up to which we will have to
18456 display new lines. If first_unchanged_at_end_row != NULL, this
18457 is the buffer position of the start of the line displayed in that
18458 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18459 that we don't stop at a buffer position. */
18460 stop_pos = 0;
18461 if (first_unchanged_at_end_row)
18463 eassert (last_unchanged_at_beg_row == NULL
18464 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18466 /* If this is a continuation line, move forward to the next one
18467 that isn't. Changes in lines above affect this line.
18468 Caution: this may move first_unchanged_at_end_row to a row
18469 not displaying text. */
18470 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18471 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18472 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18473 < it.last_visible_y))
18474 ++first_unchanged_at_end_row;
18476 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18477 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18478 >= it.last_visible_y))
18479 first_unchanged_at_end_row = NULL;
18480 else
18482 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18483 + delta);
18484 first_unchanged_at_end_vpos
18485 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18486 eassert (stop_pos >= Z - END_UNCHANGED);
18489 else if (last_unchanged_at_beg_row == NULL)
18490 GIVE_UP (19);
18493 #ifdef GLYPH_DEBUG
18495 /* Either there is no unchanged row at the end, or the one we have
18496 now displays text. This is a necessary condition for the window
18497 end pos calculation at the end of this function. */
18498 eassert (first_unchanged_at_end_row == NULL
18499 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18501 debug_last_unchanged_at_beg_vpos
18502 = (last_unchanged_at_beg_row
18503 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18504 : -1);
18505 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18507 #endif /* GLYPH_DEBUG */
18510 /* Display new lines. Set last_text_row to the last new line
18511 displayed which has text on it, i.e. might end up as being the
18512 line where the window_end_vpos is. */
18513 w->cursor.vpos = -1;
18514 last_text_row = NULL;
18515 overlay_arrow_seen = false;
18516 if (it.current_y < it.last_visible_y
18517 && !f->fonts_changed
18518 && (first_unchanged_at_end_row == NULL
18519 || IT_CHARPOS (it) < stop_pos))
18520 it.glyph_row->reversed_p = false;
18521 while (it.current_y < it.last_visible_y
18522 && !f->fonts_changed
18523 && (first_unchanged_at_end_row == NULL
18524 || IT_CHARPOS (it) < stop_pos))
18526 if (display_line (&it))
18527 last_text_row = it.glyph_row - 1;
18530 if (f->fonts_changed)
18531 return -1;
18533 /* The redisplay iterations in display_line above could have
18534 triggered font-lock, which could have done something that
18535 invalidates IT->w window's end-point information, on which we
18536 rely below. E.g., one package, which will remain unnamed, used
18537 to install a font-lock-fontify-region-function that called
18538 bury-buffer, whose side effect is to switch the buffer displayed
18539 by IT->w, and that predictably resets IT->w's window_end_valid
18540 flag, which we already tested at the entry to this function.
18541 Amply punish such packages/modes by giving up on this
18542 optimization in those cases. */
18543 if (!w->window_end_valid)
18545 clear_glyph_matrix (w->desired_matrix);
18546 return -1;
18549 /* Compute differences in buffer positions, y-positions etc. for
18550 lines reused at the bottom of the window. Compute what we can
18551 scroll. */
18552 if (first_unchanged_at_end_row
18553 /* No lines reused because we displayed everything up to the
18554 bottom of the window. */
18555 && it.current_y < it.last_visible_y)
18557 dvpos = (it.vpos
18558 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18559 current_matrix));
18560 dy = it.current_y - first_unchanged_at_end_row->y;
18561 run.current_y = first_unchanged_at_end_row->y;
18562 run.desired_y = run.current_y + dy;
18563 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18565 else
18567 delta = delta_bytes = dvpos = dy
18568 = run.current_y = run.desired_y = run.height = 0;
18569 first_unchanged_at_end_row = NULL;
18571 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18574 /* Find the cursor if not already found. We have to decide whether
18575 PT will appear on this window (it sometimes doesn't, but this is
18576 not a very frequent case.) This decision has to be made before
18577 the current matrix is altered. A value of cursor.vpos < 0 means
18578 that PT is either in one of the lines beginning at
18579 first_unchanged_at_end_row or below the window. Don't care for
18580 lines that might be displayed later at the window end; as
18581 mentioned, this is not a frequent case. */
18582 if (w->cursor.vpos < 0)
18584 /* Cursor in unchanged rows at the top? */
18585 if (PT < CHARPOS (start_pos)
18586 && last_unchanged_at_beg_row)
18588 row = row_containing_pos (w, PT,
18589 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18590 last_unchanged_at_beg_row + 1, 0);
18591 if (row)
18592 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18595 /* Start from first_unchanged_at_end_row looking for PT. */
18596 else if (first_unchanged_at_end_row)
18598 row = row_containing_pos (w, PT - delta,
18599 first_unchanged_at_end_row, NULL, 0);
18600 if (row)
18601 set_cursor_from_row (w, row, w->current_matrix, delta,
18602 delta_bytes, dy, dvpos);
18605 /* Give up if cursor was not found. */
18606 if (w->cursor.vpos < 0)
18608 clear_glyph_matrix (w->desired_matrix);
18609 return -1;
18613 /* Don't let the cursor end in the scroll margins. */
18615 int this_scroll_margin, cursor_height;
18616 int frame_line_height = default_line_pixel_height (w);
18617 int window_total_lines
18618 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18620 this_scroll_margin =
18621 max (0, min (scroll_margin, window_total_lines / 4));
18622 this_scroll_margin *= frame_line_height;
18623 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18625 if ((w->cursor.y < this_scroll_margin
18626 && CHARPOS (start) > BEGV)
18627 /* Old redisplay didn't take scroll margin into account at the bottom,
18628 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18629 || (w->cursor.y + (make_cursor_line_fully_visible_p
18630 ? cursor_height + this_scroll_margin
18631 : 1)) > it.last_visible_y)
18633 w->cursor.vpos = -1;
18634 clear_glyph_matrix (w->desired_matrix);
18635 return -1;
18639 /* Scroll the display. Do it before changing the current matrix so
18640 that xterm.c doesn't get confused about where the cursor glyph is
18641 found. */
18642 if (dy && run.height)
18644 update_begin (f);
18646 if (FRAME_WINDOW_P (f))
18648 FRAME_RIF (f)->update_window_begin_hook (w);
18649 FRAME_RIF (f)->clear_window_mouse_face (w);
18650 FRAME_RIF (f)->scroll_run_hook (w, &run);
18651 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18653 else
18655 /* Terminal frame. In this case, dvpos gives the number of
18656 lines to scroll by; dvpos < 0 means scroll up. */
18657 int from_vpos
18658 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18659 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18660 int end = (WINDOW_TOP_EDGE_LINE (w)
18661 + WINDOW_WANTS_HEADER_LINE_P (w)
18662 + window_internal_height (w));
18664 #if defined (HAVE_GPM) || defined (MSDOS)
18665 x_clear_window_mouse_face (w);
18666 #endif
18667 /* Perform the operation on the screen. */
18668 if (dvpos > 0)
18670 /* Scroll last_unchanged_at_beg_row to the end of the
18671 window down dvpos lines. */
18672 set_terminal_window (f, end);
18674 /* On dumb terminals delete dvpos lines at the end
18675 before inserting dvpos empty lines. */
18676 if (!FRAME_SCROLL_REGION_OK (f))
18677 ins_del_lines (f, end - dvpos, -dvpos);
18679 /* Insert dvpos empty lines in front of
18680 last_unchanged_at_beg_row. */
18681 ins_del_lines (f, from, dvpos);
18683 else if (dvpos < 0)
18685 /* Scroll up last_unchanged_at_beg_vpos to the end of
18686 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18687 set_terminal_window (f, end);
18689 /* Delete dvpos lines in front of
18690 last_unchanged_at_beg_vpos. ins_del_lines will set
18691 the cursor to the given vpos and emit |dvpos| delete
18692 line sequences. */
18693 ins_del_lines (f, from + dvpos, dvpos);
18695 /* On a dumb terminal insert dvpos empty lines at the
18696 end. */
18697 if (!FRAME_SCROLL_REGION_OK (f))
18698 ins_del_lines (f, end + dvpos, -dvpos);
18701 set_terminal_window (f, 0);
18704 update_end (f);
18707 /* Shift reused rows of the current matrix to the right position.
18708 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18709 text. */
18710 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18711 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18712 if (dvpos < 0)
18714 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18715 bottom_vpos, dvpos);
18716 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18717 bottom_vpos);
18719 else if (dvpos > 0)
18721 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18722 bottom_vpos, dvpos);
18723 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18724 first_unchanged_at_end_vpos + dvpos);
18727 /* For frame-based redisplay, make sure that current frame and window
18728 matrix are in sync with respect to glyph memory. */
18729 if (!FRAME_WINDOW_P (f))
18730 sync_frame_with_window_matrix_rows (w);
18732 /* Adjust buffer positions in reused rows. */
18733 if (delta || delta_bytes)
18734 increment_matrix_positions (current_matrix,
18735 first_unchanged_at_end_vpos + dvpos,
18736 bottom_vpos, delta, delta_bytes);
18738 /* Adjust Y positions. */
18739 if (dy)
18740 shift_glyph_matrix (w, current_matrix,
18741 first_unchanged_at_end_vpos + dvpos,
18742 bottom_vpos, dy);
18744 if (first_unchanged_at_end_row)
18746 first_unchanged_at_end_row += dvpos;
18747 if (first_unchanged_at_end_row->y >= it.last_visible_y
18748 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18749 first_unchanged_at_end_row = NULL;
18752 /* If scrolling up, there may be some lines to display at the end of
18753 the window. */
18754 last_text_row_at_end = NULL;
18755 if (dy < 0)
18757 /* Scrolling up can leave for example a partially visible line
18758 at the end of the window to be redisplayed. */
18759 /* Set last_row to the glyph row in the current matrix where the
18760 window end line is found. It has been moved up or down in
18761 the matrix by dvpos. */
18762 int last_vpos = w->window_end_vpos + dvpos;
18763 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18765 /* If last_row is the window end line, it should display text. */
18766 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18768 /* If window end line was partially visible before, begin
18769 displaying at that line. Otherwise begin displaying with the
18770 line following it. */
18771 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18773 init_to_row_start (&it, w, last_row);
18774 it.vpos = last_vpos;
18775 it.current_y = last_row->y;
18777 else
18779 init_to_row_end (&it, w, last_row);
18780 it.vpos = 1 + last_vpos;
18781 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18782 ++last_row;
18785 /* We may start in a continuation line. If so, we have to
18786 get the right continuation_lines_width and current_x. */
18787 it.continuation_lines_width = last_row->continuation_lines_width;
18788 it.hpos = it.current_x = 0;
18790 /* Display the rest of the lines at the window end. */
18791 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18792 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18794 /* Is it always sure that the display agrees with lines in
18795 the current matrix? I don't think so, so we mark rows
18796 displayed invalid in the current matrix by setting their
18797 enabled_p flag to false. */
18798 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18799 if (display_line (&it))
18800 last_text_row_at_end = it.glyph_row - 1;
18804 /* Update window_end_pos and window_end_vpos. */
18805 if (first_unchanged_at_end_row && !last_text_row_at_end)
18807 /* Window end line if one of the preserved rows from the current
18808 matrix. Set row to the last row displaying text in current
18809 matrix starting at first_unchanged_at_end_row, after
18810 scrolling. */
18811 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18812 row = find_last_row_displaying_text (w->current_matrix, &it,
18813 first_unchanged_at_end_row);
18814 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18815 adjust_window_ends (w, row, true);
18816 eassert (w->window_end_bytepos >= 0);
18817 IF_DEBUG (debug_method_add (w, "A"));
18819 else if (last_text_row_at_end)
18821 adjust_window_ends (w, last_text_row_at_end, false);
18822 eassert (w->window_end_bytepos >= 0);
18823 IF_DEBUG (debug_method_add (w, "B"));
18825 else if (last_text_row)
18827 /* We have displayed either to the end of the window or at the
18828 end of the window, i.e. the last row with text is to be found
18829 in the desired matrix. */
18830 adjust_window_ends (w, last_text_row, false);
18831 eassert (w->window_end_bytepos >= 0);
18833 else if (first_unchanged_at_end_row == NULL
18834 && last_text_row == NULL
18835 && last_text_row_at_end == NULL)
18837 /* Displayed to end of window, but no line containing text was
18838 displayed. Lines were deleted at the end of the window. */
18839 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18840 int vpos = w->window_end_vpos;
18841 struct glyph_row *current_row = current_matrix->rows + vpos;
18842 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18844 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18846 eassert (first_vpos <= vpos);
18847 if (desired_row->enabled_p)
18849 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18850 row = desired_row;
18852 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18853 row = current_row;
18856 w->window_end_vpos = vpos + 1;
18857 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18858 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18859 eassert (w->window_end_bytepos >= 0);
18860 IF_DEBUG (debug_method_add (w, "C"));
18862 else
18863 emacs_abort ();
18865 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18866 debug_end_vpos = w->window_end_vpos));
18868 /* Record that display has not been completed. */
18869 w->window_end_valid = false;
18870 w->desired_matrix->no_scrolling_p = true;
18871 return 3;
18873 #undef GIVE_UP
18878 /***********************************************************************
18879 More debugging support
18880 ***********************************************************************/
18882 #ifdef GLYPH_DEBUG
18884 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18885 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18886 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18889 /* Dump the contents of glyph matrix MATRIX on stderr.
18891 GLYPHS 0 means don't show glyph contents.
18892 GLYPHS 1 means show glyphs in short form
18893 GLYPHS > 1 means show glyphs in long form. */
18895 void
18896 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18898 int i;
18899 for (i = 0; i < matrix->nrows; ++i)
18900 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18904 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18905 the glyph row and area where the glyph comes from. */
18907 void
18908 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18910 if (glyph->type == CHAR_GLYPH
18911 || glyph->type == GLYPHLESS_GLYPH)
18913 fprintf (stderr,
18914 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18915 glyph - row->glyphs[TEXT_AREA],
18916 (glyph->type == CHAR_GLYPH
18917 ? 'C'
18918 : 'G'),
18919 glyph->charpos,
18920 (BUFFERP (glyph->object)
18921 ? 'B'
18922 : (STRINGP (glyph->object)
18923 ? 'S'
18924 : (NILP (glyph->object)
18925 ? '0'
18926 : '-'))),
18927 glyph->pixel_width,
18928 glyph->u.ch,
18929 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18930 ? glyph->u.ch
18931 : '.'),
18932 glyph->face_id,
18933 glyph->left_box_line_p,
18934 glyph->right_box_line_p);
18936 else if (glyph->type == STRETCH_GLYPH)
18938 fprintf (stderr,
18939 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18940 glyph - row->glyphs[TEXT_AREA],
18941 'S',
18942 glyph->charpos,
18943 (BUFFERP (glyph->object)
18944 ? 'B'
18945 : (STRINGP (glyph->object)
18946 ? 'S'
18947 : (NILP (glyph->object)
18948 ? '0'
18949 : '-'))),
18950 glyph->pixel_width,
18952 ' ',
18953 glyph->face_id,
18954 glyph->left_box_line_p,
18955 glyph->right_box_line_p);
18957 else if (glyph->type == IMAGE_GLYPH)
18959 fprintf (stderr,
18960 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18961 glyph - row->glyphs[TEXT_AREA],
18962 'I',
18963 glyph->charpos,
18964 (BUFFERP (glyph->object)
18965 ? 'B'
18966 : (STRINGP (glyph->object)
18967 ? 'S'
18968 : (NILP (glyph->object)
18969 ? '0'
18970 : '-'))),
18971 glyph->pixel_width,
18972 glyph->u.img_id,
18973 '.',
18974 glyph->face_id,
18975 glyph->left_box_line_p,
18976 glyph->right_box_line_p);
18978 else if (glyph->type == COMPOSITE_GLYPH)
18980 fprintf (stderr,
18981 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18982 glyph - row->glyphs[TEXT_AREA],
18983 '+',
18984 glyph->charpos,
18985 (BUFFERP (glyph->object)
18986 ? 'B'
18987 : (STRINGP (glyph->object)
18988 ? 'S'
18989 : (NILP (glyph->object)
18990 ? '0'
18991 : '-'))),
18992 glyph->pixel_width,
18993 glyph->u.cmp.id);
18994 if (glyph->u.cmp.automatic)
18995 fprintf (stderr,
18996 "[%d-%d]",
18997 glyph->slice.cmp.from, glyph->slice.cmp.to);
18998 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18999 glyph->face_id,
19000 glyph->left_box_line_p,
19001 glyph->right_box_line_p);
19003 else if (glyph->type == XWIDGET_GLYPH)
19005 #ifndef HAVE_XWIDGETS
19006 eassume (false);
19007 #else
19008 fprintf (stderr,
19009 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19010 glyph - row->glyphs[TEXT_AREA],
19011 'X',
19012 glyph->charpos,
19013 (BUFFERP (glyph->object)
19014 ? 'B'
19015 : (STRINGP (glyph->object)
19016 ? 'S'
19017 : '-')),
19018 glyph->pixel_width,
19019 glyph->u.xwidget,
19020 '.',
19021 glyph->face_id,
19022 glyph->left_box_line_p,
19023 glyph->right_box_line_p);
19024 #endif
19029 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19030 GLYPHS 0 means don't show glyph contents.
19031 GLYPHS 1 means show glyphs in short form
19032 GLYPHS > 1 means show glyphs in long form. */
19034 void
19035 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19037 if (glyphs != 1)
19039 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19040 fprintf (stderr, "==============================================================================\n");
19042 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19043 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19044 vpos,
19045 MATRIX_ROW_START_CHARPOS (row),
19046 MATRIX_ROW_END_CHARPOS (row),
19047 row->used[TEXT_AREA],
19048 row->contains_overlapping_glyphs_p,
19049 row->enabled_p,
19050 row->truncated_on_left_p,
19051 row->truncated_on_right_p,
19052 row->continued_p,
19053 MATRIX_ROW_CONTINUATION_LINE_P (row),
19054 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19055 row->ends_at_zv_p,
19056 row->fill_line_p,
19057 row->ends_in_middle_of_char_p,
19058 row->starts_in_middle_of_char_p,
19059 row->mouse_face_p,
19060 row->x,
19061 row->y,
19062 row->pixel_width,
19063 row->height,
19064 row->visible_height,
19065 row->ascent,
19066 row->phys_ascent);
19067 /* The next 3 lines should align to "Start" in the header. */
19068 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19069 row->end.overlay_string_index,
19070 row->continuation_lines_width);
19071 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19072 CHARPOS (row->start.string_pos),
19073 CHARPOS (row->end.string_pos));
19074 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19075 row->end.dpvec_index);
19078 if (glyphs > 1)
19080 int area;
19082 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19084 struct glyph *glyph = row->glyphs[area];
19085 struct glyph *glyph_end = glyph + row->used[area];
19087 /* Glyph for a line end in text. */
19088 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19089 ++glyph_end;
19091 if (glyph < glyph_end)
19092 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19094 for (; glyph < glyph_end; ++glyph)
19095 dump_glyph (row, glyph, area);
19098 else if (glyphs == 1)
19100 int area;
19101 char s[SHRT_MAX + 4];
19103 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19105 int i;
19107 for (i = 0; i < row->used[area]; ++i)
19109 struct glyph *glyph = row->glyphs[area] + i;
19110 if (i == row->used[area] - 1
19111 && area == TEXT_AREA
19112 && NILP (glyph->object)
19113 && glyph->type == CHAR_GLYPH
19114 && glyph->u.ch == ' ')
19116 strcpy (&s[i], "[\\n]");
19117 i += 4;
19119 else if (glyph->type == CHAR_GLYPH
19120 && glyph->u.ch < 0x80
19121 && glyph->u.ch >= ' ')
19122 s[i] = glyph->u.ch;
19123 else
19124 s[i] = '.';
19127 s[i] = '\0';
19128 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19134 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19135 Sdump_glyph_matrix, 0, 1, "p",
19136 doc: /* Dump the current matrix of the selected window to stderr.
19137 Shows contents of glyph row structures. With non-nil
19138 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19139 glyphs in short form, otherwise show glyphs in long form.
19141 Interactively, no argument means show glyphs in short form;
19142 with numeric argument, its value is passed as the GLYPHS flag. */)
19143 (Lisp_Object glyphs)
19145 struct window *w = XWINDOW (selected_window);
19146 struct buffer *buffer = XBUFFER (w->contents);
19148 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19149 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19150 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19151 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19152 fprintf (stderr, "=============================================\n");
19153 dump_glyph_matrix (w->current_matrix,
19154 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19155 return Qnil;
19159 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19160 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19161 Only text-mode frames have frame glyph matrices. */)
19162 (void)
19164 struct frame *f = XFRAME (selected_frame);
19166 if (f->current_matrix)
19167 dump_glyph_matrix (f->current_matrix, 1);
19168 else
19169 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19170 return Qnil;
19174 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19175 doc: /* Dump glyph row ROW to stderr.
19176 GLYPH 0 means don't dump glyphs.
19177 GLYPH 1 means dump glyphs in short form.
19178 GLYPH > 1 or omitted means dump glyphs in long form. */)
19179 (Lisp_Object row, Lisp_Object glyphs)
19181 struct glyph_matrix *matrix;
19182 EMACS_INT vpos;
19184 CHECK_NUMBER (row);
19185 matrix = XWINDOW (selected_window)->current_matrix;
19186 vpos = XINT (row);
19187 if (vpos >= 0 && vpos < matrix->nrows)
19188 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19189 vpos,
19190 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19191 return Qnil;
19195 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19196 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19197 GLYPH 0 means don't dump glyphs.
19198 GLYPH 1 means dump glyphs in short form.
19199 GLYPH > 1 or omitted means dump glyphs in long form.
19201 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19202 do nothing. */)
19203 (Lisp_Object row, Lisp_Object glyphs)
19205 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19206 struct frame *sf = SELECTED_FRAME ();
19207 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19208 EMACS_INT vpos;
19210 CHECK_NUMBER (row);
19211 vpos = XINT (row);
19212 if (vpos >= 0 && vpos < m->nrows)
19213 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19214 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19215 #endif
19216 return Qnil;
19220 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19221 doc: /* Toggle tracing of redisplay.
19222 With ARG, turn tracing on if and only if ARG is positive. */)
19223 (Lisp_Object arg)
19225 if (NILP (arg))
19226 trace_redisplay_p = !trace_redisplay_p;
19227 else
19229 arg = Fprefix_numeric_value (arg);
19230 trace_redisplay_p = XINT (arg) > 0;
19233 return Qnil;
19237 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19238 doc: /* Like `format', but print result to stderr.
19239 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19240 (ptrdiff_t nargs, Lisp_Object *args)
19242 Lisp_Object s = Fformat (nargs, args);
19243 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19244 return Qnil;
19247 #endif /* GLYPH_DEBUG */
19251 /***********************************************************************
19252 Building Desired Matrix Rows
19253 ***********************************************************************/
19255 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19256 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19258 static struct glyph_row *
19259 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19261 struct frame *f = XFRAME (WINDOW_FRAME (w));
19262 struct buffer *buffer = XBUFFER (w->contents);
19263 struct buffer *old = current_buffer;
19264 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19265 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19266 const unsigned char *arrow_end = arrow_string + arrow_len;
19267 const unsigned char *p;
19268 struct it it;
19269 bool multibyte_p;
19270 int n_glyphs_before;
19272 set_buffer_temp (buffer);
19273 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19274 scratch_glyph_row.reversed_p = false;
19275 it.glyph_row->used[TEXT_AREA] = 0;
19276 SET_TEXT_POS (it.position, 0, 0);
19278 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19279 p = arrow_string;
19280 while (p < arrow_end)
19282 Lisp_Object face, ilisp;
19284 /* Get the next character. */
19285 if (multibyte_p)
19286 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19287 else
19289 it.c = it.char_to_display = *p, it.len = 1;
19290 if (! ASCII_CHAR_P (it.c))
19291 it.char_to_display = BYTE8_TO_CHAR (it.c);
19293 p += it.len;
19295 /* Get its face. */
19296 ilisp = make_number (p - arrow_string);
19297 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19298 it.face_id = compute_char_face (f, it.char_to_display, face);
19300 /* Compute its width, get its glyphs. */
19301 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19302 SET_TEXT_POS (it.position, -1, -1);
19303 PRODUCE_GLYPHS (&it);
19305 /* If this character doesn't fit any more in the line, we have
19306 to remove some glyphs. */
19307 if (it.current_x > it.last_visible_x)
19309 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19310 break;
19314 set_buffer_temp (old);
19315 return it.glyph_row;
19319 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19320 glyphs to insert is determined by produce_special_glyphs. */
19322 static void
19323 insert_left_trunc_glyphs (struct it *it)
19325 struct it truncate_it;
19326 struct glyph *from, *end, *to, *toend;
19328 eassert (!FRAME_WINDOW_P (it->f)
19329 || (!it->glyph_row->reversed_p
19330 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19331 || (it->glyph_row->reversed_p
19332 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19334 /* Get the truncation glyphs. */
19335 truncate_it = *it;
19336 truncate_it.current_x = 0;
19337 truncate_it.face_id = DEFAULT_FACE_ID;
19338 truncate_it.glyph_row = &scratch_glyph_row;
19339 truncate_it.area = TEXT_AREA;
19340 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19341 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19342 truncate_it.object = Qnil;
19343 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19345 /* Overwrite glyphs from IT with truncation glyphs. */
19346 if (!it->glyph_row->reversed_p)
19348 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19350 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19351 end = from + tused;
19352 to = it->glyph_row->glyphs[TEXT_AREA];
19353 toend = to + it->glyph_row->used[TEXT_AREA];
19354 if (FRAME_WINDOW_P (it->f))
19356 /* On GUI frames, when variable-size fonts are displayed,
19357 the truncation glyphs may need more pixels than the row's
19358 glyphs they overwrite. We overwrite more glyphs to free
19359 enough screen real estate, and enlarge the stretch glyph
19360 on the right (see display_line), if there is one, to
19361 preserve the screen position of the truncation glyphs on
19362 the right. */
19363 int w = 0;
19364 struct glyph *g = to;
19365 short used;
19367 /* The first glyph could be partially visible, in which case
19368 it->glyph_row->x will be negative. But we want the left
19369 truncation glyphs to be aligned at the left margin of the
19370 window, so we override the x coordinate at which the row
19371 will begin. */
19372 it->glyph_row->x = 0;
19373 while (g < toend && w < it->truncation_pixel_width)
19375 w += g->pixel_width;
19376 ++g;
19378 if (g - to - tused > 0)
19380 memmove (to + tused, g, (toend - g) * sizeof(*g));
19381 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19383 used = it->glyph_row->used[TEXT_AREA];
19384 if (it->glyph_row->truncated_on_right_p
19385 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19386 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19387 == STRETCH_GLYPH)
19389 int extra = w - it->truncation_pixel_width;
19391 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19395 while (from < end)
19396 *to++ = *from++;
19398 /* There may be padding glyphs left over. Overwrite them too. */
19399 if (!FRAME_WINDOW_P (it->f))
19401 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19403 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19404 while (from < end)
19405 *to++ = *from++;
19409 if (to > toend)
19410 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19412 else
19414 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19416 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19417 that back to front. */
19418 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19419 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19420 toend = it->glyph_row->glyphs[TEXT_AREA];
19421 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19422 if (FRAME_WINDOW_P (it->f))
19424 int w = 0;
19425 struct glyph *g = to;
19427 while (g >= toend && w < it->truncation_pixel_width)
19429 w += g->pixel_width;
19430 --g;
19432 if (to - g - tused > 0)
19433 to = g + tused;
19434 if (it->glyph_row->truncated_on_right_p
19435 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19436 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19438 int extra = w - it->truncation_pixel_width;
19440 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19444 while (from >= end && to >= toend)
19445 *to-- = *from--;
19446 if (!FRAME_WINDOW_P (it->f))
19448 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19450 from =
19451 truncate_it.glyph_row->glyphs[TEXT_AREA]
19452 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19453 while (from >= end && to >= toend)
19454 *to-- = *from--;
19457 if (from >= end)
19459 /* Need to free some room before prepending additional
19460 glyphs. */
19461 int move_by = from - end + 1;
19462 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19463 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19465 for ( ; g >= g0; g--)
19466 g[move_by] = *g;
19467 while (from >= end)
19468 *to-- = *from--;
19469 it->glyph_row->used[TEXT_AREA] += move_by;
19474 /* Compute the hash code for ROW. */
19475 unsigned
19476 row_hash (struct glyph_row *row)
19478 int area, k;
19479 unsigned hashval = 0;
19481 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19482 for (k = 0; k < row->used[area]; ++k)
19483 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19484 + row->glyphs[area][k].u.val
19485 + row->glyphs[area][k].face_id
19486 + row->glyphs[area][k].padding_p
19487 + (row->glyphs[area][k].type << 2));
19489 return hashval;
19492 /* Compute the pixel height and width of IT->glyph_row.
19494 Most of the time, ascent and height of a display line will be equal
19495 to the max_ascent and max_height values of the display iterator
19496 structure. This is not the case if
19498 1. We hit ZV without displaying anything. In this case, max_ascent
19499 and max_height will be zero.
19501 2. We have some glyphs that don't contribute to the line height.
19502 (The glyph row flag contributes_to_line_height_p is for future
19503 pixmap extensions).
19505 The first case is easily covered by using default values because in
19506 these cases, the line height does not really matter, except that it
19507 must not be zero. */
19509 static void
19510 compute_line_metrics (struct it *it)
19512 struct glyph_row *row = it->glyph_row;
19514 if (FRAME_WINDOW_P (it->f))
19516 int i, min_y, max_y;
19518 /* The line may consist of one space only, that was added to
19519 place the cursor on it. If so, the row's height hasn't been
19520 computed yet. */
19521 if (row->height == 0)
19523 if (it->max_ascent + it->max_descent == 0)
19524 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19525 row->ascent = it->max_ascent;
19526 row->height = it->max_ascent + it->max_descent;
19527 row->phys_ascent = it->max_phys_ascent;
19528 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19529 row->extra_line_spacing = it->max_extra_line_spacing;
19532 /* Compute the width of this line. */
19533 row->pixel_width = row->x;
19534 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19535 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19537 eassert (row->pixel_width >= 0);
19538 eassert (row->ascent >= 0 && row->height > 0);
19540 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19541 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19543 /* If first line's physical ascent is larger than its logical
19544 ascent, use the physical ascent, and make the row taller.
19545 This makes accented characters fully visible. */
19546 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19547 && row->phys_ascent > row->ascent)
19549 row->height += row->phys_ascent - row->ascent;
19550 row->ascent = row->phys_ascent;
19553 /* Compute how much of the line is visible. */
19554 row->visible_height = row->height;
19556 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19557 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19559 if (row->y < min_y)
19560 row->visible_height -= min_y - row->y;
19561 if (row->y + row->height > max_y)
19562 row->visible_height -= row->y + row->height - max_y;
19564 else
19566 row->pixel_width = row->used[TEXT_AREA];
19567 if (row->continued_p)
19568 row->pixel_width -= it->continuation_pixel_width;
19569 else if (row->truncated_on_right_p)
19570 row->pixel_width -= it->truncation_pixel_width;
19571 row->ascent = row->phys_ascent = 0;
19572 row->height = row->phys_height = row->visible_height = 1;
19573 row->extra_line_spacing = 0;
19576 /* Compute a hash code for this row. */
19577 row->hash = row_hash (row);
19579 it->max_ascent = it->max_descent = 0;
19580 it->max_phys_ascent = it->max_phys_descent = 0;
19584 /* Append one space to the glyph row of iterator IT if doing a
19585 window-based redisplay. The space has the same face as
19586 IT->face_id. Value is true if a space was added.
19588 This function is called to make sure that there is always one glyph
19589 at the end of a glyph row that the cursor can be set on under
19590 window-systems. (If there weren't such a glyph we would not know
19591 how wide and tall a box cursor should be displayed).
19593 At the same time this space let's a nicely handle clearing to the
19594 end of the line if the row ends in italic text. */
19596 static bool
19597 append_space_for_newline (struct it *it, bool default_face_p)
19599 if (FRAME_WINDOW_P (it->f))
19601 int n = it->glyph_row->used[TEXT_AREA];
19603 if (it->glyph_row->glyphs[TEXT_AREA] + n
19604 < it->glyph_row->glyphs[1 + TEXT_AREA])
19606 /* Save some values that must not be changed.
19607 Must save IT->c and IT->len because otherwise
19608 ITERATOR_AT_END_P wouldn't work anymore after
19609 append_space_for_newline has been called. */
19610 enum display_element_type saved_what = it->what;
19611 int saved_c = it->c, saved_len = it->len;
19612 int saved_char_to_display = it->char_to_display;
19613 int saved_x = it->current_x;
19614 int saved_face_id = it->face_id;
19615 bool saved_box_end = it->end_of_box_run_p;
19616 struct text_pos saved_pos;
19617 Lisp_Object saved_object;
19618 struct face *face;
19620 saved_object = it->object;
19621 saved_pos = it->position;
19623 it->what = IT_CHARACTER;
19624 memset (&it->position, 0, sizeof it->position);
19625 it->object = Qnil;
19626 it->c = it->char_to_display = ' ';
19627 it->len = 1;
19629 /* If the default face was remapped, be sure to use the
19630 remapped face for the appended newline. */
19631 if (default_face_p)
19632 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19633 else if (it->face_before_selective_p)
19634 it->face_id = it->saved_face_id;
19635 face = FACE_FROM_ID (it->f, it->face_id);
19636 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19637 /* In R2L rows, we will prepend a stretch glyph that will
19638 have the end_of_box_run_p flag set for it, so there's no
19639 need for the appended newline glyph to have that flag
19640 set. */
19641 if (it->glyph_row->reversed_p
19642 /* But if the appended newline glyph goes all the way to
19643 the end of the row, there will be no stretch glyph,
19644 so leave the box flag set. */
19645 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19646 it->end_of_box_run_p = false;
19648 PRODUCE_GLYPHS (it);
19650 #ifdef HAVE_WINDOW_SYSTEM
19651 /* Make sure this space glyph has the right ascent and
19652 descent values, or else cursor at end of line will look
19653 funny, and height of empty lines will be incorrect. */
19654 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19655 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19656 if (n == 0)
19658 Lisp_Object height, total_height;
19659 int extra_line_spacing = it->extra_line_spacing;
19660 int boff = font->baseline_offset;
19662 if (font->vertical_centering)
19663 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19665 it->object = saved_object; /* get_it_property needs this */
19666 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19667 /* Must do a subset of line height processing from
19668 x_produce_glyph for newline characters. */
19669 height = get_it_property (it, Qline_height);
19670 if (CONSP (height)
19671 && CONSP (XCDR (height))
19672 && NILP (XCDR (XCDR (height))))
19674 total_height = XCAR (XCDR (height));
19675 height = XCAR (height);
19677 else
19678 total_height = Qnil;
19679 height = calc_line_height_property (it, height, font, boff, true);
19681 if (it->override_ascent >= 0)
19683 it->ascent = it->override_ascent;
19684 it->descent = it->override_descent;
19685 boff = it->override_boff;
19687 if (EQ (height, Qt))
19688 extra_line_spacing = 0;
19689 else
19691 Lisp_Object spacing;
19693 it->phys_ascent = it->ascent;
19694 it->phys_descent = it->descent;
19695 if (!NILP (height)
19696 && XINT (height) > it->ascent + it->descent)
19697 it->ascent = XINT (height) - it->descent;
19699 if (!NILP (total_height))
19700 spacing = calc_line_height_property (it, total_height, font,
19701 boff, false);
19702 else
19704 spacing = get_it_property (it, Qline_spacing);
19705 spacing = calc_line_height_property (it, spacing, font,
19706 boff, false);
19708 if (INTEGERP (spacing))
19710 extra_line_spacing = XINT (spacing);
19711 if (!NILP (total_height))
19712 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19715 if (extra_line_spacing > 0)
19717 it->descent += extra_line_spacing;
19718 if (extra_line_spacing > it->max_extra_line_spacing)
19719 it->max_extra_line_spacing = extra_line_spacing;
19721 it->max_ascent = it->ascent;
19722 it->max_descent = it->descent;
19723 /* Make sure compute_line_metrics recomputes the row height. */
19724 it->glyph_row->height = 0;
19727 g->ascent = it->max_ascent;
19728 g->descent = it->max_descent;
19729 #endif
19731 it->override_ascent = -1;
19732 it->constrain_row_ascent_descent_p = false;
19733 it->current_x = saved_x;
19734 it->object = saved_object;
19735 it->position = saved_pos;
19736 it->what = saved_what;
19737 it->face_id = saved_face_id;
19738 it->len = saved_len;
19739 it->c = saved_c;
19740 it->char_to_display = saved_char_to_display;
19741 it->end_of_box_run_p = saved_box_end;
19742 return true;
19746 return false;
19750 /* Extend the face of the last glyph in the text area of IT->glyph_row
19751 to the end of the display line. Called from display_line. If the
19752 glyph row is empty, add a space glyph to it so that we know the
19753 face to draw. Set the glyph row flag fill_line_p. If the glyph
19754 row is R2L, prepend a stretch glyph to cover the empty space to the
19755 left of the leftmost glyph. */
19757 static void
19758 extend_face_to_end_of_line (struct it *it)
19760 struct face *face, *default_face;
19761 struct frame *f = it->f;
19763 /* If line is already filled, do nothing. Non window-system frames
19764 get a grace of one more ``pixel'' because their characters are
19765 1-``pixel'' wide, so they hit the equality too early. This grace
19766 is needed only for R2L rows that are not continued, to produce
19767 one extra blank where we could display the cursor. */
19768 if ((it->current_x >= it->last_visible_x
19769 + (!FRAME_WINDOW_P (f)
19770 && it->glyph_row->reversed_p
19771 && !it->glyph_row->continued_p))
19772 /* If the window has display margins, we will need to extend
19773 their face even if the text area is filled. */
19774 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19775 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19776 return;
19778 /* The default face, possibly remapped. */
19779 default_face = FACE_FROM_ID_OR_NULL (f,
19780 lookup_basic_face (f, DEFAULT_FACE_ID));
19782 /* Face extension extends the background and box of IT->face_id
19783 to the end of the line. If the background equals the background
19784 of the frame, we don't have to do anything. */
19785 face = FACE_FROM_ID (f, (it->face_before_selective_p
19786 ? it->saved_face_id
19787 : it->face_id));
19789 if (FRAME_WINDOW_P (f)
19790 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19791 && face->box == FACE_NO_BOX
19792 && face->background == FRAME_BACKGROUND_PIXEL (f)
19793 #ifdef HAVE_WINDOW_SYSTEM
19794 && !face->stipple
19795 #endif
19796 && !it->glyph_row->reversed_p)
19797 return;
19799 /* Set the glyph row flag indicating that the face of the last glyph
19800 in the text area has to be drawn to the end of the text area. */
19801 it->glyph_row->fill_line_p = true;
19803 /* If current character of IT is not ASCII, make sure we have the
19804 ASCII face. This will be automatically undone the next time
19805 get_next_display_element returns a multibyte character. Note
19806 that the character will always be single byte in unibyte
19807 text. */
19808 if (!ASCII_CHAR_P (it->c))
19810 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19813 if (FRAME_WINDOW_P (f))
19815 /* If the row is empty, add a space with the current face of IT,
19816 so that we know which face to draw. */
19817 if (it->glyph_row->used[TEXT_AREA] == 0)
19819 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19820 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19821 it->glyph_row->used[TEXT_AREA] = 1;
19823 /* Mode line and the header line don't have margins, and
19824 likewise the frame's tool-bar window, if there is any. */
19825 if (!(it->glyph_row->mode_line_p
19826 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19827 || (WINDOWP (f->tool_bar_window)
19828 && it->w == XWINDOW (f->tool_bar_window))
19829 #endif
19832 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19833 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19835 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19836 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19837 default_face->id;
19838 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19840 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19841 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19843 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19844 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19845 default_face->id;
19846 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19849 #ifdef HAVE_WINDOW_SYSTEM
19850 if (it->glyph_row->reversed_p)
19852 /* Prepend a stretch glyph to the row, such that the
19853 rightmost glyph will be drawn flushed all the way to the
19854 right margin of the window. The stretch glyph that will
19855 occupy the empty space, if any, to the left of the
19856 glyphs. */
19857 struct font *font = face->font ? face->font : FRAME_FONT (f);
19858 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19859 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19860 struct glyph *g;
19861 int row_width, stretch_ascent, stretch_width;
19862 struct text_pos saved_pos;
19863 int saved_face_id;
19864 bool saved_avoid_cursor, saved_box_start;
19866 for (row_width = 0, g = row_start; g < row_end; g++)
19867 row_width += g->pixel_width;
19869 /* FIXME: There are various minor display glitches in R2L
19870 rows when only one of the fringes is missing. The
19871 strange condition below produces the least bad effect. */
19872 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19873 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19874 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19875 stretch_width = window_box_width (it->w, TEXT_AREA);
19876 else
19877 stretch_width = it->last_visible_x - it->first_visible_x;
19878 stretch_width -= row_width;
19880 if (stretch_width > 0)
19882 stretch_ascent =
19883 (((it->ascent + it->descent)
19884 * FONT_BASE (font)) / FONT_HEIGHT (font));
19885 saved_pos = it->position;
19886 memset (&it->position, 0, sizeof it->position);
19887 saved_avoid_cursor = it->avoid_cursor_p;
19888 it->avoid_cursor_p = true;
19889 saved_face_id = it->face_id;
19890 saved_box_start = it->start_of_box_run_p;
19891 /* The last row's stretch glyph should get the default
19892 face, to avoid painting the rest of the window with
19893 the region face, if the region ends at ZV. */
19894 if (it->glyph_row->ends_at_zv_p)
19895 it->face_id = default_face->id;
19896 else
19897 it->face_id = face->id;
19898 it->start_of_box_run_p = false;
19899 append_stretch_glyph (it, Qnil, stretch_width,
19900 it->ascent + it->descent, stretch_ascent);
19901 it->position = saved_pos;
19902 it->avoid_cursor_p = saved_avoid_cursor;
19903 it->face_id = saved_face_id;
19904 it->start_of_box_run_p = saved_box_start;
19906 /* If stretch_width comes out negative, it means that the
19907 last glyph is only partially visible. In R2L rows, we
19908 want the leftmost glyph to be partially visible, so we
19909 need to give the row the corresponding left offset. */
19910 if (stretch_width < 0)
19911 it->glyph_row->x = stretch_width;
19913 #endif /* HAVE_WINDOW_SYSTEM */
19915 else
19917 /* Save some values that must not be changed. */
19918 int saved_x = it->current_x;
19919 struct text_pos saved_pos;
19920 Lisp_Object saved_object;
19921 enum display_element_type saved_what = it->what;
19922 int saved_face_id = it->face_id;
19924 saved_object = it->object;
19925 saved_pos = it->position;
19927 it->what = IT_CHARACTER;
19928 memset (&it->position, 0, sizeof it->position);
19929 it->object = Qnil;
19930 it->c = it->char_to_display = ' ';
19931 it->len = 1;
19933 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19934 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19935 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19936 && !it->glyph_row->mode_line_p
19937 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19939 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19940 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19942 for (it->current_x = 0; g < e; g++)
19943 it->current_x += g->pixel_width;
19945 it->area = LEFT_MARGIN_AREA;
19946 it->face_id = default_face->id;
19947 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19948 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19950 PRODUCE_GLYPHS (it);
19951 /* term.c:produce_glyphs advances it->current_x only for
19952 TEXT_AREA. */
19953 it->current_x += it->pixel_width;
19956 it->current_x = saved_x;
19957 it->area = TEXT_AREA;
19960 /* The last row's blank glyphs should get the default face, to
19961 avoid painting the rest of the window with the region face,
19962 if the region ends at ZV. */
19963 if (it->glyph_row->ends_at_zv_p)
19964 it->face_id = default_face->id;
19965 else
19966 it->face_id = face->id;
19967 PRODUCE_GLYPHS (it);
19969 while (it->current_x <= it->last_visible_x)
19970 PRODUCE_GLYPHS (it);
19972 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19973 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19974 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19975 && !it->glyph_row->mode_line_p
19976 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19978 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19979 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19981 for ( ; g < e; g++)
19982 it->current_x += g->pixel_width;
19984 it->area = RIGHT_MARGIN_AREA;
19985 it->face_id = default_face->id;
19986 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19987 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19989 PRODUCE_GLYPHS (it);
19990 it->current_x += it->pixel_width;
19993 it->area = TEXT_AREA;
19996 /* Don't count these blanks really. It would let us insert a left
19997 truncation glyph below and make us set the cursor on them, maybe. */
19998 it->current_x = saved_x;
19999 it->object = saved_object;
20000 it->position = saved_pos;
20001 it->what = saved_what;
20002 it->face_id = saved_face_id;
20007 /* Value is true if text starting at CHARPOS in current_buffer is
20008 trailing whitespace. */
20010 static bool
20011 trailing_whitespace_p (ptrdiff_t charpos)
20013 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20014 int c = 0;
20016 while (bytepos < ZV_BYTE
20017 && (c = FETCH_CHAR (bytepos),
20018 c == ' ' || c == '\t'))
20019 ++bytepos;
20021 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20023 if (bytepos != PT_BYTE)
20024 return true;
20026 return false;
20030 /* Highlight trailing whitespace, if any, in ROW. */
20032 static void
20033 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20035 int used = row->used[TEXT_AREA];
20037 if (used)
20039 struct glyph *start = row->glyphs[TEXT_AREA];
20040 struct glyph *glyph = start + used - 1;
20042 if (row->reversed_p)
20044 /* Right-to-left rows need to be processed in the opposite
20045 direction, so swap the edge pointers. */
20046 glyph = start;
20047 start = row->glyphs[TEXT_AREA] + used - 1;
20050 /* Skip over glyphs inserted to display the cursor at the
20051 end of a line, for extending the face of the last glyph
20052 to the end of the line on terminals, and for truncation
20053 and continuation glyphs. */
20054 if (!row->reversed_p)
20056 while (glyph >= start
20057 && glyph->type == CHAR_GLYPH
20058 && NILP (glyph->object))
20059 --glyph;
20061 else
20063 while (glyph <= start
20064 && glyph->type == CHAR_GLYPH
20065 && NILP (glyph->object))
20066 ++glyph;
20069 /* If last glyph is a space or stretch, and it's trailing
20070 whitespace, set the face of all trailing whitespace glyphs in
20071 IT->glyph_row to `trailing-whitespace'. */
20072 if ((row->reversed_p ? glyph <= start : glyph >= start)
20073 && BUFFERP (glyph->object)
20074 && (glyph->type == STRETCH_GLYPH
20075 || (glyph->type == CHAR_GLYPH
20076 && glyph->u.ch == ' '))
20077 && trailing_whitespace_p (glyph->charpos))
20079 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20080 if (face_id < 0)
20081 return;
20083 if (!row->reversed_p)
20085 while (glyph >= start
20086 && BUFFERP (glyph->object)
20087 && (glyph->type == STRETCH_GLYPH
20088 || (glyph->type == CHAR_GLYPH
20089 && glyph->u.ch == ' ')))
20090 (glyph--)->face_id = face_id;
20092 else
20094 while (glyph <= start
20095 && BUFFERP (glyph->object)
20096 && (glyph->type == STRETCH_GLYPH
20097 || (glyph->type == CHAR_GLYPH
20098 && glyph->u.ch == ' ')))
20099 (glyph++)->face_id = face_id;
20106 /* Value is true if glyph row ROW should be
20107 considered to hold the buffer position CHARPOS. */
20109 static bool
20110 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20112 bool result = true;
20114 if (charpos == CHARPOS (row->end.pos)
20115 || charpos == MATRIX_ROW_END_CHARPOS (row))
20117 /* Suppose the row ends on a string.
20118 Unless the row is continued, that means it ends on a newline
20119 in the string. If it's anything other than a display string
20120 (e.g., a before-string from an overlay), we don't want the
20121 cursor there. (This heuristic seems to give the optimal
20122 behavior for the various types of multi-line strings.)
20123 One exception: if the string has `cursor' property on one of
20124 its characters, we _do_ want the cursor there. */
20125 if (CHARPOS (row->end.string_pos) >= 0)
20127 if (row->continued_p)
20128 result = true;
20129 else
20131 /* Check for `display' property. */
20132 struct glyph *beg = row->glyphs[TEXT_AREA];
20133 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20134 struct glyph *glyph;
20136 result = false;
20137 for (glyph = end; glyph >= beg; --glyph)
20138 if (STRINGP (glyph->object))
20140 Lisp_Object prop
20141 = Fget_char_property (make_number (charpos),
20142 Qdisplay, Qnil);
20143 result =
20144 (!NILP (prop)
20145 && display_prop_string_p (prop, glyph->object));
20146 /* If there's a `cursor' property on one of the
20147 string's characters, this row is a cursor row,
20148 even though this is not a display string. */
20149 if (!result)
20151 Lisp_Object s = glyph->object;
20153 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20155 ptrdiff_t gpos = glyph->charpos;
20157 if (!NILP (Fget_char_property (make_number (gpos),
20158 Qcursor, s)))
20160 result = true;
20161 break;
20165 break;
20169 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20171 /* If the row ends in middle of a real character,
20172 and the line is continued, we want the cursor here.
20173 That's because CHARPOS (ROW->end.pos) would equal
20174 PT if PT is before the character. */
20175 if (!row->ends_in_ellipsis_p)
20176 result = row->continued_p;
20177 else
20178 /* If the row ends in an ellipsis, then
20179 CHARPOS (ROW->end.pos) will equal point after the
20180 invisible text. We want that position to be displayed
20181 after the ellipsis. */
20182 result = false;
20184 /* If the row ends at ZV, display the cursor at the end of that
20185 row instead of at the start of the row below. */
20186 else
20187 result = row->ends_at_zv_p;
20190 return result;
20193 /* Value is true if glyph row ROW should be
20194 used to hold the cursor. */
20196 static bool
20197 cursor_row_p (struct glyph_row *row)
20199 return row_for_charpos_p (row, PT);
20204 /* Push the property PROP so that it will be rendered at the current
20205 position in IT. Return true if PROP was successfully pushed, false
20206 otherwise. Called from handle_line_prefix to handle the
20207 `line-prefix' and `wrap-prefix' properties. */
20209 static bool
20210 push_prefix_prop (struct it *it, Lisp_Object prop)
20212 struct text_pos pos =
20213 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20215 eassert (it->method == GET_FROM_BUFFER
20216 || it->method == GET_FROM_DISPLAY_VECTOR
20217 || it->method == GET_FROM_STRING
20218 || it->method == GET_FROM_IMAGE);
20220 /* We need to save the current buffer/string position, so it will be
20221 restored by pop_it, because iterate_out_of_display_property
20222 depends on that being set correctly, but some situations leave
20223 it->position not yet set when this function is called. */
20224 push_it (it, &pos);
20226 if (STRINGP (prop))
20228 if (SCHARS (prop) == 0)
20230 pop_it (it);
20231 return false;
20234 it->string = prop;
20235 it->string_from_prefix_prop_p = true;
20236 it->multibyte_p = STRING_MULTIBYTE (it->string);
20237 it->current.overlay_string_index = -1;
20238 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20239 it->end_charpos = it->string_nchars = SCHARS (it->string);
20240 it->method = GET_FROM_STRING;
20241 it->stop_charpos = 0;
20242 it->prev_stop = 0;
20243 it->base_level_stop = 0;
20245 /* Force paragraph direction to be that of the parent
20246 buffer/string. */
20247 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20248 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20249 else
20250 it->paragraph_embedding = L2R;
20252 /* Set up the bidi iterator for this display string. */
20253 if (it->bidi_p)
20255 it->bidi_it.string.lstring = it->string;
20256 it->bidi_it.string.s = NULL;
20257 it->bidi_it.string.schars = it->end_charpos;
20258 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20259 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20260 it->bidi_it.string.unibyte = !it->multibyte_p;
20261 it->bidi_it.w = it->w;
20262 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20265 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20267 it->method = GET_FROM_STRETCH;
20268 it->object = prop;
20270 #ifdef HAVE_WINDOW_SYSTEM
20271 else if (IMAGEP (prop))
20273 it->what = IT_IMAGE;
20274 it->image_id = lookup_image (it->f, prop);
20275 it->method = GET_FROM_IMAGE;
20277 #endif /* HAVE_WINDOW_SYSTEM */
20278 else
20280 pop_it (it); /* bogus display property, give up */
20281 return false;
20284 return true;
20287 /* Return the character-property PROP at the current position in IT. */
20289 static Lisp_Object
20290 get_it_property (struct it *it, Lisp_Object prop)
20292 Lisp_Object position, object = it->object;
20294 if (STRINGP (object))
20295 position = make_number (IT_STRING_CHARPOS (*it));
20296 else if (BUFFERP (object))
20298 position = make_number (IT_CHARPOS (*it));
20299 object = it->window;
20301 else
20302 return Qnil;
20304 return Fget_char_property (position, prop, object);
20307 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20309 static void
20310 handle_line_prefix (struct it *it)
20312 Lisp_Object prefix;
20314 if (it->continuation_lines_width > 0)
20316 prefix = get_it_property (it, Qwrap_prefix);
20317 if (NILP (prefix))
20318 prefix = Vwrap_prefix;
20320 else
20322 prefix = get_it_property (it, Qline_prefix);
20323 if (NILP (prefix))
20324 prefix = Vline_prefix;
20326 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20328 /* If the prefix is wider than the window, and we try to wrap
20329 it, it would acquire its own wrap prefix, and so on till the
20330 iterator stack overflows. So, don't wrap the prefix. */
20331 it->line_wrap = TRUNCATE;
20332 it->avoid_cursor_p = true;
20338 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20339 only for R2L lines from display_line and display_string, when they
20340 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20341 the line/string needs to be continued on the next glyph row. */
20342 static void
20343 unproduce_glyphs (struct it *it, int n)
20345 struct glyph *glyph, *end;
20347 eassert (it->glyph_row);
20348 eassert (it->glyph_row->reversed_p);
20349 eassert (it->area == TEXT_AREA);
20350 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20352 if (n > it->glyph_row->used[TEXT_AREA])
20353 n = it->glyph_row->used[TEXT_AREA];
20354 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20355 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20356 for ( ; glyph < end; glyph++)
20357 glyph[-n] = *glyph;
20360 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20361 and ROW->maxpos. */
20362 static void
20363 find_row_edges (struct it *it, struct glyph_row *row,
20364 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20365 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20367 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20368 lines' rows is implemented for bidi-reordered rows. */
20370 /* ROW->minpos is the value of min_pos, the minimal buffer position
20371 we have in ROW, or ROW->start.pos if that is smaller. */
20372 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20373 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20374 else
20375 /* We didn't find buffer positions smaller than ROW->start, or
20376 didn't find _any_ valid buffer positions in any of the glyphs,
20377 so we must trust the iterator's computed positions. */
20378 row->minpos = row->start.pos;
20379 if (max_pos <= 0)
20381 max_pos = CHARPOS (it->current.pos);
20382 max_bpos = BYTEPOS (it->current.pos);
20385 /* Here are the various use-cases for ending the row, and the
20386 corresponding values for ROW->maxpos:
20388 Line ends in a newline from buffer eol_pos + 1
20389 Line is continued from buffer max_pos + 1
20390 Line is truncated on right it->current.pos
20391 Line ends in a newline from string max_pos + 1(*)
20392 (*) + 1 only when line ends in a forward scan
20393 Line is continued from string max_pos
20394 Line is continued from display vector max_pos
20395 Line is entirely from a string min_pos == max_pos
20396 Line is entirely from a display vector min_pos == max_pos
20397 Line that ends at ZV ZV
20399 If you discover other use-cases, please add them here as
20400 appropriate. */
20401 if (row->ends_at_zv_p)
20402 row->maxpos = it->current.pos;
20403 else if (row->used[TEXT_AREA])
20405 bool seen_this_string = false;
20406 struct glyph_row *r1 = row - 1;
20408 /* Did we see the same display string on the previous row? */
20409 if (STRINGP (it->object)
20410 /* this is not the first row */
20411 && row > it->w->desired_matrix->rows
20412 /* previous row is not the header line */
20413 && !r1->mode_line_p
20414 /* previous row also ends in a newline from a string */
20415 && r1->ends_in_newline_from_string_p)
20417 struct glyph *start, *end;
20419 /* Search for the last glyph of the previous row that came
20420 from buffer or string. Depending on whether the row is
20421 L2R or R2L, we need to process it front to back or the
20422 other way round. */
20423 if (!r1->reversed_p)
20425 start = r1->glyphs[TEXT_AREA];
20426 end = start + r1->used[TEXT_AREA];
20427 /* Glyphs inserted by redisplay have nil as their object. */
20428 while (end > start
20429 && NILP ((end - 1)->object)
20430 && (end - 1)->charpos <= 0)
20431 --end;
20432 if (end > start)
20434 if (EQ ((end - 1)->object, it->object))
20435 seen_this_string = true;
20437 else
20438 /* If all the glyphs of the previous row were inserted
20439 by redisplay, it means the previous row was
20440 produced from a single newline, which is only
20441 possible if that newline came from the same string
20442 as the one which produced this ROW. */
20443 seen_this_string = true;
20445 else
20447 end = r1->glyphs[TEXT_AREA] - 1;
20448 start = end + r1->used[TEXT_AREA];
20449 while (end < start
20450 && NILP ((end + 1)->object)
20451 && (end + 1)->charpos <= 0)
20452 ++end;
20453 if (end < start)
20455 if (EQ ((end + 1)->object, it->object))
20456 seen_this_string = true;
20458 else
20459 seen_this_string = true;
20462 /* Take note of each display string that covers a newline only
20463 once, the first time we see it. This is for when a display
20464 string includes more than one newline in it. */
20465 if (row->ends_in_newline_from_string_p && !seen_this_string)
20467 /* If we were scanning the buffer forward when we displayed
20468 the string, we want to account for at least one buffer
20469 position that belongs to this row (position covered by
20470 the display string), so that cursor positioning will
20471 consider this row as a candidate when point is at the end
20472 of the visual line represented by this row. This is not
20473 required when scanning back, because max_pos will already
20474 have a much larger value. */
20475 if (CHARPOS (row->end.pos) > max_pos)
20476 INC_BOTH (max_pos, max_bpos);
20477 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20479 else if (CHARPOS (it->eol_pos) > 0)
20480 SET_TEXT_POS (row->maxpos,
20481 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20482 else if (row->continued_p)
20484 /* If max_pos is different from IT's current position, it
20485 means IT->method does not belong to the display element
20486 at max_pos. However, it also means that the display
20487 element at max_pos was displayed in its entirety on this
20488 line, which is equivalent to saying that the next line
20489 starts at the next buffer position. */
20490 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20491 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20492 else
20494 INC_BOTH (max_pos, max_bpos);
20495 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20498 else if (row->truncated_on_right_p)
20499 /* display_line already called reseat_at_next_visible_line_start,
20500 which puts the iterator at the beginning of the next line, in
20501 the logical order. */
20502 row->maxpos = it->current.pos;
20503 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20504 /* A line that is entirely from a string/image/stretch... */
20505 row->maxpos = row->minpos;
20506 else
20507 emacs_abort ();
20509 else
20510 row->maxpos = it->current.pos;
20513 /* Construct the glyph row IT->glyph_row in the desired matrix of
20514 IT->w from text at the current position of IT. See dispextern.h
20515 for an overview of struct it. Value is true if
20516 IT->glyph_row displays text, as opposed to a line displaying ZV
20517 only. */
20519 static bool
20520 display_line (struct it *it)
20522 struct glyph_row *row = it->glyph_row;
20523 Lisp_Object overlay_arrow_string;
20524 struct it wrap_it;
20525 void *wrap_data = NULL;
20526 bool may_wrap = false;
20527 int wrap_x UNINIT;
20528 int wrap_row_used = -1;
20529 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20530 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20531 int wrap_row_extra_line_spacing UNINIT;
20532 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20533 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20534 int cvpos;
20535 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20536 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20537 bool pending_handle_line_prefix = false;
20539 /* We always start displaying at hpos zero even if hscrolled. */
20540 eassert (it->hpos == 0 && it->current_x == 0);
20542 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20543 >= it->w->desired_matrix->nrows)
20545 it->w->nrows_scale_factor++;
20546 it->f->fonts_changed = true;
20547 return false;
20550 /* Clear the result glyph row and enable it. */
20551 prepare_desired_row (it->w, row, false);
20553 row->y = it->current_y;
20554 row->start = it->start;
20555 row->continuation_lines_width = it->continuation_lines_width;
20556 row->displays_text_p = true;
20557 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20558 it->starts_in_middle_of_char_p = false;
20560 /* Arrange the overlays nicely for our purposes. Usually, we call
20561 display_line on only one line at a time, in which case this
20562 can't really hurt too much, or we call it on lines which appear
20563 one after another in the buffer, in which case all calls to
20564 recenter_overlay_lists but the first will be pretty cheap. */
20565 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20567 /* Move over display elements that are not visible because we are
20568 hscrolled. This may stop at an x-position < IT->first_visible_x
20569 if the first glyph is partially visible or if we hit a line end. */
20570 if (it->current_x < it->first_visible_x)
20572 enum move_it_result move_result;
20574 this_line_min_pos = row->start.pos;
20575 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20576 MOVE_TO_POS | MOVE_TO_X);
20577 /* If we are under a large hscroll, move_it_in_display_line_to
20578 could hit the end of the line without reaching
20579 it->first_visible_x. Pretend that we did reach it. This is
20580 especially important on a TTY, where we will call
20581 extend_face_to_end_of_line, which needs to know how many
20582 blank glyphs to produce. */
20583 if (it->current_x < it->first_visible_x
20584 && (move_result == MOVE_NEWLINE_OR_CR
20585 || move_result == MOVE_POS_MATCH_OR_ZV))
20586 it->current_x = it->first_visible_x;
20588 /* Record the smallest positions seen while we moved over
20589 display elements that are not visible. This is needed by
20590 redisplay_internal for optimizing the case where the cursor
20591 stays inside the same line. The rest of this function only
20592 considers positions that are actually displayed, so
20593 RECORD_MAX_MIN_POS will not otherwise record positions that
20594 are hscrolled to the left of the left edge of the window. */
20595 min_pos = CHARPOS (this_line_min_pos);
20596 min_bpos = BYTEPOS (this_line_min_pos);
20598 else if (it->area == TEXT_AREA)
20600 /* We only do this when not calling move_it_in_display_line_to
20601 above, because that function calls itself handle_line_prefix. */
20602 handle_line_prefix (it);
20604 else
20606 /* Line-prefix and wrap-prefix are always displayed in the text
20607 area. But if this is the first call to display_line after
20608 init_iterator, the iterator might have been set up to write
20609 into a marginal area, e.g. if the line begins with some
20610 display property that writes to the margins. So we need to
20611 wait with the call to handle_line_prefix until whatever
20612 writes to the margin has done its job. */
20613 pending_handle_line_prefix = true;
20616 /* Get the initial row height. This is either the height of the
20617 text hscrolled, if there is any, or zero. */
20618 row->ascent = it->max_ascent;
20619 row->height = it->max_ascent + it->max_descent;
20620 row->phys_ascent = it->max_phys_ascent;
20621 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20622 row->extra_line_spacing = it->max_extra_line_spacing;
20624 /* Utility macro to record max and min buffer positions seen until now. */
20625 #define RECORD_MAX_MIN_POS(IT) \
20626 do \
20628 bool composition_p \
20629 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20630 ptrdiff_t current_pos = \
20631 composition_p ? (IT)->cmp_it.charpos \
20632 : IT_CHARPOS (*(IT)); \
20633 ptrdiff_t current_bpos = \
20634 composition_p ? CHAR_TO_BYTE (current_pos) \
20635 : IT_BYTEPOS (*(IT)); \
20636 if (current_pos < min_pos) \
20638 min_pos = current_pos; \
20639 min_bpos = current_bpos; \
20641 if (IT_CHARPOS (*it) > max_pos) \
20643 max_pos = IT_CHARPOS (*it); \
20644 max_bpos = IT_BYTEPOS (*it); \
20647 while (false)
20649 /* Loop generating characters. The loop is left with IT on the next
20650 character to display. */
20651 while (true)
20653 int n_glyphs_before, hpos_before, x_before;
20654 int x, nglyphs;
20655 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20657 /* Retrieve the next thing to display. Value is false if end of
20658 buffer reached. */
20659 if (!get_next_display_element (it))
20661 /* Maybe add a space at the end of this line that is used to
20662 display the cursor there under X. Set the charpos of the
20663 first glyph of blank lines not corresponding to any text
20664 to -1. */
20665 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20666 row->exact_window_width_line_p = true;
20667 else if ((append_space_for_newline (it, true)
20668 && row->used[TEXT_AREA] == 1)
20669 || row->used[TEXT_AREA] == 0)
20671 row->glyphs[TEXT_AREA]->charpos = -1;
20672 row->displays_text_p = false;
20674 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20675 && (!MINI_WINDOW_P (it->w)
20676 || (minibuf_level && EQ (it->window, minibuf_window))))
20677 row->indicate_empty_line_p = true;
20680 it->continuation_lines_width = 0;
20681 row->ends_at_zv_p = true;
20682 /* A row that displays right-to-left text must always have
20683 its last face extended all the way to the end of line,
20684 even if this row ends in ZV, because we still write to
20685 the screen left to right. We also need to extend the
20686 last face if the default face is remapped to some
20687 different face, otherwise the functions that clear
20688 portions of the screen will clear with the default face's
20689 background color. */
20690 if (row->reversed_p
20691 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20692 extend_face_to_end_of_line (it);
20693 break;
20696 /* Now, get the metrics of what we want to display. This also
20697 generates glyphs in `row' (which is IT->glyph_row). */
20698 n_glyphs_before = row->used[TEXT_AREA];
20699 x = it->current_x;
20701 /* Remember the line height so far in case the next element doesn't
20702 fit on the line. */
20703 if (it->line_wrap != TRUNCATE)
20705 ascent = it->max_ascent;
20706 descent = it->max_descent;
20707 phys_ascent = it->max_phys_ascent;
20708 phys_descent = it->max_phys_descent;
20710 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20712 if (IT_DISPLAYING_WHITESPACE (it))
20713 may_wrap = true;
20714 else if (may_wrap)
20716 SAVE_IT (wrap_it, *it, wrap_data);
20717 wrap_x = x;
20718 wrap_row_used = row->used[TEXT_AREA];
20719 wrap_row_ascent = row->ascent;
20720 wrap_row_height = row->height;
20721 wrap_row_phys_ascent = row->phys_ascent;
20722 wrap_row_phys_height = row->phys_height;
20723 wrap_row_extra_line_spacing = row->extra_line_spacing;
20724 wrap_row_min_pos = min_pos;
20725 wrap_row_min_bpos = min_bpos;
20726 wrap_row_max_pos = max_pos;
20727 wrap_row_max_bpos = max_bpos;
20728 may_wrap = false;
20733 PRODUCE_GLYPHS (it);
20735 /* If this display element was in marginal areas, continue with
20736 the next one. */
20737 if (it->area != TEXT_AREA)
20739 row->ascent = max (row->ascent, it->max_ascent);
20740 row->height = max (row->height, it->max_ascent + it->max_descent);
20741 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20742 row->phys_height = max (row->phys_height,
20743 it->max_phys_ascent + it->max_phys_descent);
20744 row->extra_line_spacing = max (row->extra_line_spacing,
20745 it->max_extra_line_spacing);
20746 set_iterator_to_next (it, true);
20747 /* If we didn't handle the line/wrap prefix above, and the
20748 call to set_iterator_to_next just switched to TEXT_AREA,
20749 process the prefix now. */
20750 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20752 pending_handle_line_prefix = false;
20753 handle_line_prefix (it);
20755 continue;
20758 /* Does the display element fit on the line? If we truncate
20759 lines, we should draw past the right edge of the window. If
20760 we don't truncate, we want to stop so that we can display the
20761 continuation glyph before the right margin. If lines are
20762 continued, there are two possible strategies for characters
20763 resulting in more than 1 glyph (e.g. tabs): Display as many
20764 glyphs as possible in this line and leave the rest for the
20765 continuation line, or display the whole element in the next
20766 line. Original redisplay did the former, so we do it also. */
20767 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20768 hpos_before = it->hpos;
20769 x_before = x;
20771 if (/* Not a newline. */
20772 nglyphs > 0
20773 /* Glyphs produced fit entirely in the line. */
20774 && it->current_x < it->last_visible_x)
20776 it->hpos += nglyphs;
20777 row->ascent = max (row->ascent, it->max_ascent);
20778 row->height = max (row->height, it->max_ascent + it->max_descent);
20779 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20780 row->phys_height = max (row->phys_height,
20781 it->max_phys_ascent + it->max_phys_descent);
20782 row->extra_line_spacing = max (row->extra_line_spacing,
20783 it->max_extra_line_spacing);
20784 if (it->current_x - it->pixel_width < it->first_visible_x
20785 /* In R2L rows, we arrange in extend_face_to_end_of_line
20786 to add a right offset to the line, by a suitable
20787 change to the stretch glyph that is the leftmost
20788 glyph of the line. */
20789 && !row->reversed_p)
20790 row->x = x - it->first_visible_x;
20791 /* Record the maximum and minimum buffer positions seen so
20792 far in glyphs that will be displayed by this row. */
20793 if (it->bidi_p)
20794 RECORD_MAX_MIN_POS (it);
20796 else
20798 int i, new_x;
20799 struct glyph *glyph;
20801 for (i = 0; i < nglyphs; ++i, x = new_x)
20803 /* Identify the glyphs added by the last call to
20804 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20805 the previous glyphs. */
20806 if (!row->reversed_p)
20807 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20808 else
20809 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20810 new_x = x + glyph->pixel_width;
20812 if (/* Lines are continued. */
20813 it->line_wrap != TRUNCATE
20814 && (/* Glyph doesn't fit on the line. */
20815 new_x > it->last_visible_x
20816 /* Or it fits exactly on a window system frame. */
20817 || (new_x == it->last_visible_x
20818 && FRAME_WINDOW_P (it->f)
20819 && (row->reversed_p
20820 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20821 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20823 /* End of a continued line. */
20825 if (it->hpos == 0
20826 || (new_x == it->last_visible_x
20827 && FRAME_WINDOW_P (it->f)
20828 && (row->reversed_p
20829 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20830 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20832 /* Current glyph is the only one on the line or
20833 fits exactly on the line. We must continue
20834 the line because we can't draw the cursor
20835 after the glyph. */
20836 row->continued_p = true;
20837 it->current_x = new_x;
20838 it->continuation_lines_width += new_x;
20839 ++it->hpos;
20840 if (i == nglyphs - 1)
20842 /* If line-wrap is on, check if a previous
20843 wrap point was found. */
20844 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20845 && wrap_row_used > 0
20846 /* Even if there is a previous wrap
20847 point, continue the line here as
20848 usual, if (i) the previous character
20849 was a space or tab AND (ii) the
20850 current character is not. */
20851 && (!may_wrap
20852 || IT_DISPLAYING_WHITESPACE (it)))
20853 goto back_to_wrap;
20855 /* Record the maximum and minimum buffer
20856 positions seen so far in glyphs that will be
20857 displayed by this row. */
20858 if (it->bidi_p)
20859 RECORD_MAX_MIN_POS (it);
20860 set_iterator_to_next (it, true);
20861 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20863 if (!get_next_display_element (it))
20865 row->exact_window_width_line_p = true;
20866 it->continuation_lines_width = 0;
20867 row->continued_p = false;
20868 row->ends_at_zv_p = true;
20870 else if (ITERATOR_AT_END_OF_LINE_P (it))
20872 row->continued_p = false;
20873 row->exact_window_width_line_p = true;
20875 /* If line-wrap is on, check if a
20876 previous wrap point was found. */
20877 else if (wrap_row_used > 0
20878 /* Even if there is a previous wrap
20879 point, continue the line here as
20880 usual, if (i) the previous character
20881 was a space or tab AND (ii) the
20882 current character is not. */
20883 && (!may_wrap
20884 || IT_DISPLAYING_WHITESPACE (it)))
20885 goto back_to_wrap;
20889 else if (it->bidi_p)
20890 RECORD_MAX_MIN_POS (it);
20891 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20892 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20893 extend_face_to_end_of_line (it);
20895 else if (CHAR_GLYPH_PADDING_P (*glyph)
20896 && !FRAME_WINDOW_P (it->f))
20898 /* A padding glyph that doesn't fit on this line.
20899 This means the whole character doesn't fit
20900 on the line. */
20901 if (row->reversed_p)
20902 unproduce_glyphs (it, row->used[TEXT_AREA]
20903 - n_glyphs_before);
20904 row->used[TEXT_AREA] = n_glyphs_before;
20906 /* Fill the rest of the row with continuation
20907 glyphs like in 20.x. */
20908 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20909 < row->glyphs[1 + TEXT_AREA])
20910 produce_special_glyphs (it, IT_CONTINUATION);
20912 row->continued_p = true;
20913 it->current_x = x_before;
20914 it->continuation_lines_width += x_before;
20916 /* Restore the height to what it was before the
20917 element not fitting on the line. */
20918 it->max_ascent = ascent;
20919 it->max_descent = descent;
20920 it->max_phys_ascent = phys_ascent;
20921 it->max_phys_descent = phys_descent;
20922 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20923 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20924 extend_face_to_end_of_line (it);
20926 else if (wrap_row_used > 0)
20928 back_to_wrap:
20929 if (row->reversed_p)
20930 unproduce_glyphs (it,
20931 row->used[TEXT_AREA] - wrap_row_used);
20932 RESTORE_IT (it, &wrap_it, wrap_data);
20933 it->continuation_lines_width += wrap_x;
20934 row->used[TEXT_AREA] = wrap_row_used;
20935 row->ascent = wrap_row_ascent;
20936 row->height = wrap_row_height;
20937 row->phys_ascent = wrap_row_phys_ascent;
20938 row->phys_height = wrap_row_phys_height;
20939 row->extra_line_spacing = wrap_row_extra_line_spacing;
20940 min_pos = wrap_row_min_pos;
20941 min_bpos = wrap_row_min_bpos;
20942 max_pos = wrap_row_max_pos;
20943 max_bpos = wrap_row_max_bpos;
20944 row->continued_p = true;
20945 row->ends_at_zv_p = false;
20946 row->exact_window_width_line_p = false;
20947 it->continuation_lines_width += x;
20949 /* Make sure that a non-default face is extended
20950 up to the right margin of the window. */
20951 extend_face_to_end_of_line (it);
20953 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20955 /* A TAB that extends past the right edge of the
20956 window. This produces a single glyph on
20957 window system frames. We leave the glyph in
20958 this row and let it fill the row, but don't
20959 consume the TAB. */
20960 if ((row->reversed_p
20961 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20962 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20963 produce_special_glyphs (it, IT_CONTINUATION);
20964 it->continuation_lines_width += it->last_visible_x;
20965 row->ends_in_middle_of_char_p = true;
20966 row->continued_p = true;
20967 glyph->pixel_width = it->last_visible_x - x;
20968 it->starts_in_middle_of_char_p = true;
20969 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20970 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20971 extend_face_to_end_of_line (it);
20973 else
20975 /* Something other than a TAB that draws past
20976 the right edge of the window. Restore
20977 positions to values before the element. */
20978 if (row->reversed_p)
20979 unproduce_glyphs (it, row->used[TEXT_AREA]
20980 - (n_glyphs_before + i));
20981 row->used[TEXT_AREA] = n_glyphs_before + i;
20983 /* Display continuation glyphs. */
20984 it->current_x = x_before;
20985 it->continuation_lines_width += x;
20986 if (!FRAME_WINDOW_P (it->f)
20987 || (row->reversed_p
20988 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20989 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20990 produce_special_glyphs (it, IT_CONTINUATION);
20991 row->continued_p = true;
20993 extend_face_to_end_of_line (it);
20995 if (nglyphs > 1 && i > 0)
20997 row->ends_in_middle_of_char_p = true;
20998 it->starts_in_middle_of_char_p = true;
21001 /* Restore the height to what it was before the
21002 element not fitting on the line. */
21003 it->max_ascent = ascent;
21004 it->max_descent = descent;
21005 it->max_phys_ascent = phys_ascent;
21006 it->max_phys_descent = phys_descent;
21009 break;
21011 else if (new_x > it->first_visible_x)
21013 /* Increment number of glyphs actually displayed. */
21014 ++it->hpos;
21016 /* Record the maximum and minimum buffer positions
21017 seen so far in glyphs that will be displayed by
21018 this row. */
21019 if (it->bidi_p)
21020 RECORD_MAX_MIN_POS (it);
21022 if (x < it->first_visible_x && !row->reversed_p)
21023 /* Glyph is partially visible, i.e. row starts at
21024 negative X position. Don't do that in R2L
21025 rows, where we arrange to add a right offset to
21026 the line in extend_face_to_end_of_line, by a
21027 suitable change to the stretch glyph that is
21028 the leftmost glyph of the line. */
21029 row->x = x - it->first_visible_x;
21030 /* When the last glyph of an R2L row only fits
21031 partially on the line, we need to set row->x to a
21032 negative offset, so that the leftmost glyph is
21033 the one that is partially visible. But if we are
21034 going to produce the truncation glyph, this will
21035 be taken care of in produce_special_glyphs. */
21036 if (row->reversed_p
21037 && new_x > it->last_visible_x
21038 && !(it->line_wrap == TRUNCATE
21039 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21041 eassert (FRAME_WINDOW_P (it->f));
21042 row->x = it->last_visible_x - new_x;
21045 else
21047 /* Glyph is completely off the left margin of the
21048 window. This should not happen because of the
21049 move_it_in_display_line at the start of this
21050 function, unless the text display area of the
21051 window is empty. */
21052 eassert (it->first_visible_x <= it->last_visible_x);
21055 /* Even if this display element produced no glyphs at all,
21056 we want to record its position. */
21057 if (it->bidi_p && nglyphs == 0)
21058 RECORD_MAX_MIN_POS (it);
21060 row->ascent = max (row->ascent, it->max_ascent);
21061 row->height = max (row->height, it->max_ascent + it->max_descent);
21062 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21063 row->phys_height = max (row->phys_height,
21064 it->max_phys_ascent + it->max_phys_descent);
21065 row->extra_line_spacing = max (row->extra_line_spacing,
21066 it->max_extra_line_spacing);
21068 /* End of this display line if row is continued. */
21069 if (row->continued_p || row->ends_at_zv_p)
21070 break;
21073 at_end_of_line:
21074 /* Is this a line end? If yes, we're also done, after making
21075 sure that a non-default face is extended up to the right
21076 margin of the window. */
21077 if (ITERATOR_AT_END_OF_LINE_P (it))
21079 int used_before = row->used[TEXT_AREA];
21081 row->ends_in_newline_from_string_p = STRINGP (it->object);
21083 /* Add a space at the end of the line that is used to
21084 display the cursor there. */
21085 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21086 append_space_for_newline (it, false);
21088 /* Extend the face to the end of the line. */
21089 extend_face_to_end_of_line (it);
21091 /* Make sure we have the position. */
21092 if (used_before == 0)
21093 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21095 /* Record the position of the newline, for use in
21096 find_row_edges. */
21097 it->eol_pos = it->current.pos;
21099 /* Consume the line end. This skips over invisible lines. */
21100 set_iterator_to_next (it, true);
21101 it->continuation_lines_width = 0;
21102 break;
21105 /* Proceed with next display element. Note that this skips
21106 over lines invisible because of selective display. */
21107 set_iterator_to_next (it, true);
21109 /* If we truncate lines, we are done when the last displayed
21110 glyphs reach past the right margin of the window. */
21111 if (it->line_wrap == TRUNCATE
21112 && ((FRAME_WINDOW_P (it->f)
21113 /* Images are preprocessed in produce_image_glyph such
21114 that they are cropped at the right edge of the
21115 window, so an image glyph will always end exactly at
21116 last_visible_x, even if there's no right fringe. */
21117 && ((row->reversed_p
21118 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21119 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21120 || it->what == IT_IMAGE))
21121 ? (it->current_x >= it->last_visible_x)
21122 : (it->current_x > it->last_visible_x)))
21124 /* Maybe add truncation glyphs. */
21125 if (!FRAME_WINDOW_P (it->f)
21126 || (row->reversed_p
21127 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21128 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21130 int i, n;
21132 if (!row->reversed_p)
21134 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21135 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21136 break;
21138 else
21140 for (i = 0; i < row->used[TEXT_AREA]; i++)
21141 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21142 break;
21143 /* Remove any padding glyphs at the front of ROW, to
21144 make room for the truncation glyphs we will be
21145 adding below. The loop below always inserts at
21146 least one truncation glyph, so also remove the
21147 last glyph added to ROW. */
21148 unproduce_glyphs (it, i + 1);
21149 /* Adjust i for the loop below. */
21150 i = row->used[TEXT_AREA] - (i + 1);
21153 /* produce_special_glyphs overwrites the last glyph, so
21154 we don't want that if we want to keep that last
21155 glyph, which means it's an image. */
21156 if (it->current_x > it->last_visible_x)
21158 it->current_x = x_before;
21159 if (!FRAME_WINDOW_P (it->f))
21161 for (n = row->used[TEXT_AREA]; i < n; ++i)
21163 row->used[TEXT_AREA] = i;
21164 produce_special_glyphs (it, IT_TRUNCATION);
21167 else
21169 row->used[TEXT_AREA] = i;
21170 produce_special_glyphs (it, IT_TRUNCATION);
21172 it->hpos = hpos_before;
21175 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21177 /* Don't truncate if we can overflow newline into fringe. */
21178 if (!get_next_display_element (it))
21180 it->continuation_lines_width = 0;
21181 row->ends_at_zv_p = true;
21182 row->exact_window_width_line_p = true;
21183 break;
21185 if (ITERATOR_AT_END_OF_LINE_P (it))
21187 row->exact_window_width_line_p = true;
21188 goto at_end_of_line;
21190 it->current_x = x_before;
21191 it->hpos = hpos_before;
21194 row->truncated_on_right_p = true;
21195 it->continuation_lines_width = 0;
21196 reseat_at_next_visible_line_start (it, false);
21197 /* We insist below that IT's position be at ZV because in
21198 bidi-reordered lines the character at visible line start
21199 might not be the character that follows the newline in
21200 the logical order. */
21201 if (IT_BYTEPOS (*it) > BEG_BYTE)
21202 row->ends_at_zv_p =
21203 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21204 else
21205 row->ends_at_zv_p = false;
21206 break;
21210 if (wrap_data)
21211 bidi_unshelve_cache (wrap_data, true);
21213 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21214 at the left window margin. */
21215 if (it->first_visible_x
21216 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21218 if (!FRAME_WINDOW_P (it->f)
21219 || (((row->reversed_p
21220 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21221 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21222 /* Don't let insert_left_trunc_glyphs overwrite the
21223 first glyph of the row if it is an image. */
21224 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21225 insert_left_trunc_glyphs (it);
21226 row->truncated_on_left_p = true;
21229 /* Remember the position at which this line ends.
21231 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21232 cannot be before the call to find_row_edges below, since that is
21233 where these positions are determined. */
21234 row->end = it->current;
21235 if (!it->bidi_p)
21237 row->minpos = row->start.pos;
21238 row->maxpos = row->end.pos;
21240 else
21242 /* ROW->minpos and ROW->maxpos must be the smallest and
21243 `1 + the largest' buffer positions in ROW. But if ROW was
21244 bidi-reordered, these two positions can be anywhere in the
21245 row, so we must determine them now. */
21246 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21249 /* If the start of this line is the overlay arrow-position, then
21250 mark this glyph row as the one containing the overlay arrow.
21251 This is clearly a mess with variable size fonts. It would be
21252 better to let it be displayed like cursors under X. */
21253 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21254 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21255 !NILP (overlay_arrow_string)))
21257 /* Overlay arrow in window redisplay is a fringe bitmap. */
21258 if (STRINGP (overlay_arrow_string))
21260 struct glyph_row *arrow_row
21261 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21262 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21263 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21264 struct glyph *p = row->glyphs[TEXT_AREA];
21265 struct glyph *p2, *end;
21267 /* Copy the arrow glyphs. */
21268 while (glyph < arrow_end)
21269 *p++ = *glyph++;
21271 /* Throw away padding glyphs. */
21272 p2 = p;
21273 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21274 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21275 ++p2;
21276 if (p2 > p)
21278 while (p2 < end)
21279 *p++ = *p2++;
21280 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21283 else
21285 eassert (INTEGERP (overlay_arrow_string));
21286 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21288 overlay_arrow_seen = true;
21291 /* Highlight trailing whitespace. */
21292 if (!NILP (Vshow_trailing_whitespace))
21293 highlight_trailing_whitespace (it->f, it->glyph_row);
21295 /* Compute pixel dimensions of this line. */
21296 compute_line_metrics (it);
21298 /* Implementation note: No changes in the glyphs of ROW or in their
21299 faces can be done past this point, because compute_line_metrics
21300 computes ROW's hash value and stores it within the glyph_row
21301 structure. */
21303 /* Record whether this row ends inside an ellipsis. */
21304 row->ends_in_ellipsis_p
21305 = (it->method == GET_FROM_DISPLAY_VECTOR
21306 && it->ellipsis_p);
21308 /* Save fringe bitmaps in this row. */
21309 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21310 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21311 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21312 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21314 it->left_user_fringe_bitmap = 0;
21315 it->left_user_fringe_face_id = 0;
21316 it->right_user_fringe_bitmap = 0;
21317 it->right_user_fringe_face_id = 0;
21319 /* Maybe set the cursor. */
21320 cvpos = it->w->cursor.vpos;
21321 if ((cvpos < 0
21322 /* In bidi-reordered rows, keep checking for proper cursor
21323 position even if one has been found already, because buffer
21324 positions in such rows change non-linearly with ROW->VPOS,
21325 when a line is continued. One exception: when we are at ZV,
21326 display cursor on the first suitable glyph row, since all
21327 the empty rows after that also have their position set to ZV. */
21328 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21329 lines' rows is implemented for bidi-reordered rows. */
21330 || (it->bidi_p
21331 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21332 && PT >= MATRIX_ROW_START_CHARPOS (row)
21333 && PT <= MATRIX_ROW_END_CHARPOS (row)
21334 && cursor_row_p (row))
21335 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21337 /* Prepare for the next line. This line starts horizontally at (X
21338 HPOS) = (0 0). Vertical positions are incremented. As a
21339 convenience for the caller, IT->glyph_row is set to the next
21340 row to be used. */
21341 it->current_x = it->hpos = 0;
21342 it->current_y += row->height;
21343 SET_TEXT_POS (it->eol_pos, 0, 0);
21344 ++it->vpos;
21345 ++it->glyph_row;
21346 /* The next row should by default use the same value of the
21347 reversed_p flag as this one. set_iterator_to_next decides when
21348 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21349 the flag accordingly. */
21350 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21351 it->glyph_row->reversed_p = row->reversed_p;
21352 it->start = row->end;
21353 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21355 #undef RECORD_MAX_MIN_POS
21358 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21359 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21360 doc: /* Return paragraph direction at point in BUFFER.
21361 Value is either `left-to-right' or `right-to-left'.
21362 If BUFFER is omitted or nil, it defaults to the current buffer.
21364 Paragraph direction determines how the text in the paragraph is displayed.
21365 In left-to-right paragraphs, text begins at the left margin of the window
21366 and the reading direction is generally left to right. In right-to-left
21367 paragraphs, text begins at the right margin and is read from right to left.
21369 See also `bidi-paragraph-direction'. */)
21370 (Lisp_Object buffer)
21372 struct buffer *buf = current_buffer;
21373 struct buffer *old = buf;
21375 if (! NILP (buffer))
21377 CHECK_BUFFER (buffer);
21378 buf = XBUFFER (buffer);
21381 if (NILP (BVAR (buf, bidi_display_reordering))
21382 || NILP (BVAR (buf, enable_multibyte_characters))
21383 /* When we are loading loadup.el, the character property tables
21384 needed for bidi iteration are not yet available. */
21385 || redisplay__inhibit_bidi)
21386 return Qleft_to_right;
21387 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21388 return BVAR (buf, bidi_paragraph_direction);
21389 else
21391 /* Determine the direction from buffer text. We could try to
21392 use current_matrix if it is up to date, but this seems fast
21393 enough as it is. */
21394 struct bidi_it itb;
21395 ptrdiff_t pos = BUF_PT (buf);
21396 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21397 int c;
21398 void *itb_data = bidi_shelve_cache ();
21400 set_buffer_temp (buf);
21401 /* bidi_paragraph_init finds the base direction of the paragraph
21402 by searching forward from paragraph start. We need the base
21403 direction of the current or _previous_ paragraph, so we need
21404 to make sure we are within that paragraph. To that end, find
21405 the previous non-empty line. */
21406 if (pos >= ZV && pos > BEGV)
21407 DEC_BOTH (pos, bytepos);
21408 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21409 if (fast_looking_at (trailing_white_space,
21410 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21412 while ((c = FETCH_BYTE (bytepos)) == '\n'
21413 || c == ' ' || c == '\t' || c == '\f')
21415 if (bytepos <= BEGV_BYTE)
21416 break;
21417 bytepos--;
21418 pos--;
21420 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21421 bytepos--;
21423 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21424 itb.paragraph_dir = NEUTRAL_DIR;
21425 itb.string.s = NULL;
21426 itb.string.lstring = Qnil;
21427 itb.string.bufpos = 0;
21428 itb.string.from_disp_str = false;
21429 itb.string.unibyte = false;
21430 /* We have no window to use here for ignoring window-specific
21431 overlays. Using NULL for window pointer will cause
21432 compute_display_string_pos to use the current buffer. */
21433 itb.w = NULL;
21434 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21435 bidi_unshelve_cache (itb_data, false);
21436 set_buffer_temp (old);
21437 switch (itb.paragraph_dir)
21439 case L2R:
21440 return Qleft_to_right;
21441 break;
21442 case R2L:
21443 return Qright_to_left;
21444 break;
21445 default:
21446 emacs_abort ();
21451 DEFUN ("bidi-find-overridden-directionality",
21452 Fbidi_find_overridden_directionality,
21453 Sbidi_find_overridden_directionality, 2, 3, 0,
21454 doc: /* Return position between FROM and TO where directionality was overridden.
21456 This function returns the first character position in the specified
21457 region of OBJECT where there is a character whose `bidi-class' property
21458 is `L', but which was forced to display as `R' by a directional
21459 override, and likewise with characters whose `bidi-class' is `R'
21460 or `AL' that were forced to display as `L'.
21462 If no such character is found, the function returns nil.
21464 OBJECT is a Lisp string or buffer to search for overridden
21465 directionality, and defaults to the current buffer if nil or omitted.
21466 OBJECT can also be a window, in which case the function will search
21467 the buffer displayed in that window. Passing the window instead of
21468 a buffer is preferable when the buffer is displayed in some window,
21469 because this function will then be able to correctly account for
21470 window-specific overlays, which can affect the results.
21472 Strong directional characters `L', `R', and `AL' can have their
21473 intrinsic directionality overridden by directional override
21474 control characters RLO (u+202e) and LRO (u+202d). See the
21475 function `get-char-code-property' for a way to inquire about
21476 the `bidi-class' property of a character. */)
21477 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21479 struct buffer *buf = current_buffer;
21480 struct buffer *old = buf;
21481 struct window *w = NULL;
21482 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21483 struct bidi_it itb;
21484 ptrdiff_t from_pos, to_pos, from_bpos;
21485 void *itb_data;
21487 if (!NILP (object))
21489 if (BUFFERP (object))
21490 buf = XBUFFER (object);
21491 else if (WINDOWP (object))
21493 w = decode_live_window (object);
21494 buf = XBUFFER (w->contents);
21495 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21497 else
21498 CHECK_STRING (object);
21501 if (STRINGP (object))
21503 /* Characters in unibyte strings are always treated by bidi.c as
21504 strong LTR. */
21505 if (!STRING_MULTIBYTE (object)
21506 /* When we are loading loadup.el, the character property
21507 tables needed for bidi iteration are not yet
21508 available. */
21509 || redisplay__inhibit_bidi)
21510 return Qnil;
21512 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21513 if (from_pos >= SCHARS (object))
21514 return Qnil;
21516 /* Set up the bidi iterator. */
21517 itb_data = bidi_shelve_cache ();
21518 itb.paragraph_dir = NEUTRAL_DIR;
21519 itb.string.lstring = object;
21520 itb.string.s = NULL;
21521 itb.string.schars = SCHARS (object);
21522 itb.string.bufpos = 0;
21523 itb.string.from_disp_str = false;
21524 itb.string.unibyte = false;
21525 itb.w = w;
21526 bidi_init_it (0, 0, frame_window_p, &itb);
21528 else
21530 /* Nothing this fancy can happen in unibyte buffers, or in a
21531 buffer that disabled reordering, or if FROM is at EOB. */
21532 if (NILP (BVAR (buf, bidi_display_reordering))
21533 || NILP (BVAR (buf, enable_multibyte_characters))
21534 /* When we are loading loadup.el, the character property
21535 tables needed for bidi iteration are not yet
21536 available. */
21537 || redisplay__inhibit_bidi)
21538 return Qnil;
21540 set_buffer_temp (buf);
21541 validate_region (&from, &to);
21542 from_pos = XINT (from);
21543 to_pos = XINT (to);
21544 if (from_pos >= ZV)
21545 return Qnil;
21547 /* Set up the bidi iterator. */
21548 itb_data = bidi_shelve_cache ();
21549 from_bpos = CHAR_TO_BYTE (from_pos);
21550 if (from_pos == BEGV)
21552 itb.charpos = BEGV;
21553 itb.bytepos = BEGV_BYTE;
21555 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21557 itb.charpos = from_pos;
21558 itb.bytepos = from_bpos;
21560 else
21561 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21562 -1, &itb.bytepos);
21563 itb.paragraph_dir = NEUTRAL_DIR;
21564 itb.string.s = NULL;
21565 itb.string.lstring = Qnil;
21566 itb.string.bufpos = 0;
21567 itb.string.from_disp_str = false;
21568 itb.string.unibyte = false;
21569 itb.w = w;
21570 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21573 ptrdiff_t found;
21574 do {
21575 /* For the purposes of this function, the actual base direction of
21576 the paragraph doesn't matter, so just set it to L2R. */
21577 bidi_paragraph_init (L2R, &itb, false);
21578 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21580 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21582 bidi_unshelve_cache (itb_data, false);
21583 set_buffer_temp (old);
21585 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21588 DEFUN ("move-point-visually", Fmove_point_visually,
21589 Smove_point_visually, 1, 1, 0,
21590 doc: /* Move point in the visual order in the specified DIRECTION.
21591 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21592 left.
21594 Value is the new character position of point. */)
21595 (Lisp_Object direction)
21597 struct window *w = XWINDOW (selected_window);
21598 struct buffer *b = XBUFFER (w->contents);
21599 struct glyph_row *row;
21600 int dir;
21601 Lisp_Object paragraph_dir;
21603 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21604 (!(ROW)->continued_p \
21605 && NILP ((GLYPH)->object) \
21606 && (GLYPH)->type == CHAR_GLYPH \
21607 && (GLYPH)->u.ch == ' ' \
21608 && (GLYPH)->charpos >= 0 \
21609 && !(GLYPH)->avoid_cursor_p)
21611 CHECK_NUMBER (direction);
21612 dir = XINT (direction);
21613 if (dir > 0)
21614 dir = 1;
21615 else
21616 dir = -1;
21618 /* If current matrix is up-to-date, we can use the information
21619 recorded in the glyphs, at least as long as the goal is on the
21620 screen. */
21621 if (w->window_end_valid
21622 && !windows_or_buffers_changed
21623 && b
21624 && !b->clip_changed
21625 && !b->prevent_redisplay_optimizations_p
21626 && !window_outdated (w)
21627 /* We rely below on the cursor coordinates to be up to date, but
21628 we cannot trust them if some command moved point since the
21629 last complete redisplay. */
21630 && w->last_point == BUF_PT (b)
21631 && w->cursor.vpos >= 0
21632 && w->cursor.vpos < w->current_matrix->nrows
21633 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21635 struct glyph *g = row->glyphs[TEXT_AREA];
21636 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21637 struct glyph *gpt = g + w->cursor.hpos;
21639 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21641 if (BUFFERP (g->object) && g->charpos != PT)
21643 SET_PT (g->charpos);
21644 w->cursor.vpos = -1;
21645 return make_number (PT);
21647 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21649 ptrdiff_t new_pos;
21651 if (BUFFERP (gpt->object))
21653 new_pos = PT;
21654 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21655 new_pos += (row->reversed_p ? -dir : dir);
21656 else
21657 new_pos -= (row->reversed_p ? -dir : dir);
21659 else if (BUFFERP (g->object))
21660 new_pos = g->charpos;
21661 else
21662 break;
21663 SET_PT (new_pos);
21664 w->cursor.vpos = -1;
21665 return make_number (PT);
21667 else if (ROW_GLYPH_NEWLINE_P (row, g))
21669 /* Glyphs inserted at the end of a non-empty line for
21670 positioning the cursor have zero charpos, so we must
21671 deduce the value of point by other means. */
21672 if (g->charpos > 0)
21673 SET_PT (g->charpos);
21674 else if (row->ends_at_zv_p && PT != ZV)
21675 SET_PT (ZV);
21676 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21677 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21678 else
21679 break;
21680 w->cursor.vpos = -1;
21681 return make_number (PT);
21684 if (g == e || NILP (g->object))
21686 if (row->truncated_on_left_p || row->truncated_on_right_p)
21687 goto simulate_display;
21688 if (!row->reversed_p)
21689 row += dir;
21690 else
21691 row -= dir;
21692 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21693 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21694 goto simulate_display;
21696 if (dir > 0)
21698 if (row->reversed_p && !row->continued_p)
21700 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21701 w->cursor.vpos = -1;
21702 return make_number (PT);
21704 g = row->glyphs[TEXT_AREA];
21705 e = g + row->used[TEXT_AREA];
21706 for ( ; g < e; g++)
21708 if (BUFFERP (g->object)
21709 /* Empty lines have only one glyph, which stands
21710 for the newline, and whose charpos is the
21711 buffer position of the newline. */
21712 || ROW_GLYPH_NEWLINE_P (row, g)
21713 /* When the buffer ends in a newline, the line at
21714 EOB also has one glyph, but its charpos is -1. */
21715 || (row->ends_at_zv_p
21716 && !row->reversed_p
21717 && NILP (g->object)
21718 && g->type == CHAR_GLYPH
21719 && g->u.ch == ' '))
21721 if (g->charpos > 0)
21722 SET_PT (g->charpos);
21723 else if (!row->reversed_p
21724 && row->ends_at_zv_p
21725 && PT != ZV)
21726 SET_PT (ZV);
21727 else
21728 continue;
21729 w->cursor.vpos = -1;
21730 return make_number (PT);
21734 else
21736 if (!row->reversed_p && !row->continued_p)
21738 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21739 w->cursor.vpos = -1;
21740 return make_number (PT);
21742 e = row->glyphs[TEXT_AREA];
21743 g = e + row->used[TEXT_AREA] - 1;
21744 for ( ; g >= e; g--)
21746 if (BUFFERP (g->object)
21747 || (ROW_GLYPH_NEWLINE_P (row, g)
21748 && g->charpos > 0)
21749 /* Empty R2L lines on GUI frames have the buffer
21750 position of the newline stored in the stretch
21751 glyph. */
21752 || g->type == STRETCH_GLYPH
21753 || (row->ends_at_zv_p
21754 && row->reversed_p
21755 && NILP (g->object)
21756 && g->type == CHAR_GLYPH
21757 && g->u.ch == ' '))
21759 if (g->charpos > 0)
21760 SET_PT (g->charpos);
21761 else if (row->reversed_p
21762 && row->ends_at_zv_p
21763 && PT != ZV)
21764 SET_PT (ZV);
21765 else
21766 continue;
21767 w->cursor.vpos = -1;
21768 return make_number (PT);
21775 simulate_display:
21777 /* If we wind up here, we failed to move by using the glyphs, so we
21778 need to simulate display instead. */
21780 if (b)
21781 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21782 else
21783 paragraph_dir = Qleft_to_right;
21784 if (EQ (paragraph_dir, Qright_to_left))
21785 dir = -dir;
21786 if (PT <= BEGV && dir < 0)
21787 xsignal0 (Qbeginning_of_buffer);
21788 else if (PT >= ZV && dir > 0)
21789 xsignal0 (Qend_of_buffer);
21790 else
21792 struct text_pos pt;
21793 struct it it;
21794 int pt_x, target_x, pixel_width, pt_vpos;
21795 bool at_eol_p;
21796 bool overshoot_expected = false;
21797 bool target_is_eol_p = false;
21799 /* Setup the arena. */
21800 SET_TEXT_POS (pt, PT, PT_BYTE);
21801 start_display (&it, w, pt);
21802 /* When lines are truncated, we could be called with point
21803 outside of the windows edges, in which case move_it_*
21804 functions either prematurely stop at window's edge or jump to
21805 the next screen line, whereas we rely below on our ability to
21806 reach point, in order to start from its X coordinate. So we
21807 need to disregard the window's horizontal extent in that case. */
21808 if (it.line_wrap == TRUNCATE)
21809 it.last_visible_x = INFINITY;
21811 if (it.cmp_it.id < 0
21812 && it.method == GET_FROM_STRING
21813 && it.area == TEXT_AREA
21814 && it.string_from_display_prop_p
21815 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21816 overshoot_expected = true;
21818 /* Find the X coordinate of point. We start from the beginning
21819 of this or previous line to make sure we are before point in
21820 the logical order (since the move_it_* functions can only
21821 move forward). */
21822 reseat:
21823 reseat_at_previous_visible_line_start (&it);
21824 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21825 if (IT_CHARPOS (it) != PT)
21827 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21828 -1, -1, -1, MOVE_TO_POS);
21829 /* If we missed point because the character there is
21830 displayed out of a display vector that has more than one
21831 glyph, retry expecting overshoot. */
21832 if (it.method == GET_FROM_DISPLAY_VECTOR
21833 && it.current.dpvec_index > 0
21834 && !overshoot_expected)
21836 overshoot_expected = true;
21837 goto reseat;
21839 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21840 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21842 pt_x = it.current_x;
21843 pt_vpos = it.vpos;
21844 if (dir > 0 || overshoot_expected)
21846 struct glyph_row *row = it.glyph_row;
21848 /* When point is at beginning of line, we don't have
21849 information about the glyph there loaded into struct
21850 it. Calling get_next_display_element fixes that. */
21851 if (pt_x == 0)
21852 get_next_display_element (&it);
21853 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21854 it.glyph_row = NULL;
21855 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21856 it.glyph_row = row;
21857 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21858 it, lest it will become out of sync with it's buffer
21859 position. */
21860 it.current_x = pt_x;
21862 else
21863 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21864 pixel_width = it.pixel_width;
21865 if (overshoot_expected && at_eol_p)
21866 pixel_width = 0;
21867 else if (pixel_width <= 0)
21868 pixel_width = 1;
21870 /* If there's a display string (or something similar) at point,
21871 we are actually at the glyph to the left of point, so we need
21872 to correct the X coordinate. */
21873 if (overshoot_expected)
21875 if (it.bidi_p)
21876 pt_x += pixel_width * it.bidi_it.scan_dir;
21877 else
21878 pt_x += pixel_width;
21881 /* Compute target X coordinate, either to the left or to the
21882 right of point. On TTY frames, all characters have the same
21883 pixel width of 1, so we can use that. On GUI frames we don't
21884 have an easy way of getting at the pixel width of the
21885 character to the left of point, so we use a different method
21886 of getting to that place. */
21887 if (dir > 0)
21888 target_x = pt_x + pixel_width;
21889 else
21890 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21892 /* Target X coordinate could be one line above or below the line
21893 of point, in which case we need to adjust the target X
21894 coordinate. Also, if moving to the left, we need to begin at
21895 the left edge of the point's screen line. */
21896 if (dir < 0)
21898 if (pt_x > 0)
21900 start_display (&it, w, pt);
21901 if (it.line_wrap == TRUNCATE)
21902 it.last_visible_x = INFINITY;
21903 reseat_at_previous_visible_line_start (&it);
21904 it.current_x = it.current_y = it.hpos = 0;
21905 if (pt_vpos != 0)
21906 move_it_by_lines (&it, pt_vpos);
21908 else
21910 move_it_by_lines (&it, -1);
21911 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21912 target_is_eol_p = true;
21913 /* Under word-wrap, we don't know the x coordinate of
21914 the last character displayed on the previous line,
21915 which immediately precedes the wrap point. To find
21916 out its x coordinate, we try moving to the right
21917 margin of the window, which will stop at the wrap
21918 point, and then reset target_x to point at the
21919 character that precedes the wrap point. This is not
21920 needed on GUI frames, because (see below) there we
21921 move from the left margin one grapheme cluster at a
21922 time, and stop when we hit the wrap point. */
21923 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21925 void *it_data = NULL;
21926 struct it it2;
21928 SAVE_IT (it2, it, it_data);
21929 move_it_in_display_line_to (&it, ZV, target_x,
21930 MOVE_TO_POS | MOVE_TO_X);
21931 /* If we arrived at target_x, that _is_ the last
21932 character on the previous line. */
21933 if (it.current_x != target_x)
21934 target_x = it.current_x - 1;
21935 RESTORE_IT (&it, &it2, it_data);
21939 else
21941 if (at_eol_p
21942 || (target_x >= it.last_visible_x
21943 && it.line_wrap != TRUNCATE))
21945 if (pt_x > 0)
21946 move_it_by_lines (&it, 0);
21947 move_it_by_lines (&it, 1);
21948 target_x = 0;
21952 /* Move to the target X coordinate. */
21953 /* On GUI frames, as we don't know the X coordinate of the
21954 character to the left of point, moving point to the left
21955 requires walking, one grapheme cluster at a time, until we
21956 find ourself at a place immediately to the left of the
21957 character at point. */
21958 if (FRAME_WINDOW_P (it.f) && dir < 0)
21960 struct text_pos new_pos;
21961 enum move_it_result rc = MOVE_X_REACHED;
21963 if (it.current_x == 0)
21964 get_next_display_element (&it);
21965 if (it.what == IT_COMPOSITION)
21967 new_pos.charpos = it.cmp_it.charpos;
21968 new_pos.bytepos = -1;
21970 else
21971 new_pos = it.current.pos;
21973 while (it.current_x + it.pixel_width <= target_x
21974 && (rc == MOVE_X_REACHED
21975 /* Under word-wrap, move_it_in_display_line_to
21976 stops at correct coordinates, but sometimes
21977 returns MOVE_POS_MATCH_OR_ZV. */
21978 || (it.line_wrap == WORD_WRAP
21979 && rc == MOVE_POS_MATCH_OR_ZV)))
21981 int new_x = it.current_x + it.pixel_width;
21983 /* For composed characters, we want the position of the
21984 first character in the grapheme cluster (usually, the
21985 composition's base character), whereas it.current
21986 might give us the position of the _last_ one, e.g. if
21987 the composition is rendered in reverse due to bidi
21988 reordering. */
21989 if (it.what == IT_COMPOSITION)
21991 new_pos.charpos = it.cmp_it.charpos;
21992 new_pos.bytepos = -1;
21994 else
21995 new_pos = it.current.pos;
21996 if (new_x == it.current_x)
21997 new_x++;
21998 rc = move_it_in_display_line_to (&it, ZV, new_x,
21999 MOVE_TO_POS | MOVE_TO_X);
22000 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22001 break;
22003 /* The previous position we saw in the loop is the one we
22004 want. */
22005 if (new_pos.bytepos == -1)
22006 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22007 it.current.pos = new_pos;
22009 else if (it.current_x != target_x)
22010 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22012 /* If we ended up in a display string that covers point, move to
22013 buffer position to the right in the visual order. */
22014 if (dir > 0)
22016 while (IT_CHARPOS (it) == PT)
22018 set_iterator_to_next (&it, false);
22019 if (!get_next_display_element (&it))
22020 break;
22024 /* Move point to that position. */
22025 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22028 return make_number (PT);
22030 #undef ROW_GLYPH_NEWLINE_P
22033 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22034 Sbidi_resolved_levels, 0, 1, 0,
22035 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22037 The resolved levels are produced by the Emacs bidi reordering engine
22038 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22039 read the Unicode Standard Annex 9 (UAX#9) for background information
22040 about these levels.
22042 VPOS is the zero-based number of the current window's screen line
22043 for which to produce the resolved levels. If VPOS is nil or omitted,
22044 it defaults to the screen line of point. If the window displays a
22045 header line, VPOS of zero will report on the header line, and first
22046 line of text in the window will have VPOS of 1.
22048 Value is an array of resolved levels, indexed by glyph number.
22049 Glyphs are numbered from zero starting from the beginning of the
22050 screen line, i.e. the left edge of the window for left-to-right lines
22051 and from the right edge for right-to-left lines. The resolved levels
22052 are produced only for the window's text area; text in display margins
22053 is not included.
22055 If the selected window's display is not up-to-date, or if the specified
22056 screen line does not display text, this function returns nil. It is
22057 highly recommended to bind this function to some simple key, like F8,
22058 in order to avoid these problems.
22060 This function exists mainly for testing the correctness of the
22061 Emacs UBA implementation, in particular with the test suite. */)
22062 (Lisp_Object vpos)
22064 struct window *w = XWINDOW (selected_window);
22065 struct buffer *b = XBUFFER (w->contents);
22066 int nrow;
22067 struct glyph_row *row;
22069 if (NILP (vpos))
22071 int d1, d2, d3, d4, d5;
22073 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22075 else
22077 CHECK_NUMBER_COERCE_MARKER (vpos);
22078 nrow = XINT (vpos);
22081 /* We require up-to-date glyph matrix for this window. */
22082 if (w->window_end_valid
22083 && !windows_or_buffers_changed
22084 && b
22085 && !b->clip_changed
22086 && !b->prevent_redisplay_optimizations_p
22087 && !window_outdated (w)
22088 && nrow >= 0
22089 && nrow < w->current_matrix->nrows
22090 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22091 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22093 struct glyph *g, *e, *g1;
22094 int nglyphs, i;
22095 Lisp_Object levels;
22097 if (!row->reversed_p) /* Left-to-right glyph row. */
22099 g = g1 = row->glyphs[TEXT_AREA];
22100 e = g + row->used[TEXT_AREA];
22102 /* Skip over glyphs at the start of the row that was
22103 generated by redisplay for its own needs. */
22104 while (g < e
22105 && NILP (g->object)
22106 && g->charpos < 0)
22107 g++;
22108 g1 = g;
22110 /* Count the "interesting" glyphs in this row. */
22111 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22112 nglyphs++;
22114 /* Create and fill the array. */
22115 levels = make_uninit_vector (nglyphs);
22116 for (i = 0; g1 < g; i++, g1++)
22117 ASET (levels, i, make_number (g1->resolved_level));
22119 else /* Right-to-left glyph row. */
22121 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22122 e = row->glyphs[TEXT_AREA] - 1;
22123 while (g > e
22124 && NILP (g->object)
22125 && g->charpos < 0)
22126 g--;
22127 g1 = g;
22128 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22129 nglyphs++;
22130 levels = make_uninit_vector (nglyphs);
22131 for (i = 0; g1 > g; i++, g1--)
22132 ASET (levels, i, make_number (g1->resolved_level));
22134 return levels;
22136 else
22137 return Qnil;
22142 /***********************************************************************
22143 Menu Bar
22144 ***********************************************************************/
22146 /* Redisplay the menu bar in the frame for window W.
22148 The menu bar of X frames that don't have X toolkit support is
22149 displayed in a special window W->frame->menu_bar_window.
22151 The menu bar of terminal frames is treated specially as far as
22152 glyph matrices are concerned. Menu bar lines are not part of
22153 windows, so the update is done directly on the frame matrix rows
22154 for the menu bar. */
22156 static void
22157 display_menu_bar (struct window *w)
22159 struct frame *f = XFRAME (WINDOW_FRAME (w));
22160 struct it it;
22161 Lisp_Object items;
22162 int i;
22164 /* Don't do all this for graphical frames. */
22165 #ifdef HAVE_NTGUI
22166 if (FRAME_W32_P (f))
22167 return;
22168 #endif
22169 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22170 if (FRAME_X_P (f))
22171 return;
22172 #endif
22174 #ifdef HAVE_NS
22175 if (FRAME_NS_P (f))
22176 return;
22177 #endif /* HAVE_NS */
22179 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22180 eassert (!FRAME_WINDOW_P (f));
22181 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22182 it.first_visible_x = 0;
22183 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22184 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22185 if (FRAME_WINDOW_P (f))
22187 /* Menu bar lines are displayed in the desired matrix of the
22188 dummy window menu_bar_window. */
22189 struct window *menu_w;
22190 menu_w = XWINDOW (f->menu_bar_window);
22191 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22192 MENU_FACE_ID);
22193 it.first_visible_x = 0;
22194 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22196 else
22197 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22199 /* This is a TTY frame, i.e. character hpos/vpos are used as
22200 pixel x/y. */
22201 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22202 MENU_FACE_ID);
22203 it.first_visible_x = 0;
22204 it.last_visible_x = FRAME_COLS (f);
22207 /* FIXME: This should be controlled by a user option. See the
22208 comments in redisplay_tool_bar and display_mode_line about
22209 this. */
22210 it.paragraph_embedding = L2R;
22212 /* Clear all rows of the menu bar. */
22213 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22215 struct glyph_row *row = it.glyph_row + i;
22216 clear_glyph_row (row);
22217 row->enabled_p = true;
22218 row->full_width_p = true;
22219 row->reversed_p = false;
22222 /* Display all items of the menu bar. */
22223 items = FRAME_MENU_BAR_ITEMS (it.f);
22224 for (i = 0; i < ASIZE (items); i += 4)
22226 Lisp_Object string;
22228 /* Stop at nil string. */
22229 string = AREF (items, i + 1);
22230 if (NILP (string))
22231 break;
22233 /* Remember where item was displayed. */
22234 ASET (items, i + 3, make_number (it.hpos));
22236 /* Display the item, pad with one space. */
22237 if (it.current_x < it.last_visible_x)
22238 display_string (NULL, string, Qnil, 0, 0, &it,
22239 SCHARS (string) + 1, 0, 0, -1);
22242 /* Fill out the line with spaces. */
22243 if (it.current_x < it.last_visible_x)
22244 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22246 /* Compute the total height of the lines. */
22247 compute_line_metrics (&it);
22250 /* Deep copy of a glyph row, including the glyphs. */
22251 static void
22252 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22254 struct glyph *pointers[1 + LAST_AREA];
22255 int to_used = to->used[TEXT_AREA];
22257 /* Save glyph pointers of TO. */
22258 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22260 /* Do a structure assignment. */
22261 *to = *from;
22263 /* Restore original glyph pointers of TO. */
22264 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22266 /* Copy the glyphs. */
22267 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22268 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22270 /* If we filled only part of the TO row, fill the rest with
22271 space_glyph (which will display as empty space). */
22272 if (to_used > from->used[TEXT_AREA])
22273 fill_up_frame_row_with_spaces (to, to_used);
22276 /* Display one menu item on a TTY, by overwriting the glyphs in the
22277 frame F's desired glyph matrix with glyphs produced from the menu
22278 item text. Called from term.c to display TTY drop-down menus one
22279 item at a time.
22281 ITEM_TEXT is the menu item text as a C string.
22283 FACE_ID is the face ID to be used for this menu item. FACE_ID
22284 could specify one of 3 faces: a face for an enabled item, a face
22285 for a disabled item, or a face for a selected item.
22287 X and Y are coordinates of the first glyph in the frame's desired
22288 matrix to be overwritten by the menu item. Since this is a TTY, Y
22289 is the zero-based number of the glyph row and X is the zero-based
22290 glyph number in the row, starting from left, where to start
22291 displaying the item.
22293 SUBMENU means this menu item drops down a submenu, which
22294 should be indicated by displaying a proper visual cue after the
22295 item text. */
22297 void
22298 display_tty_menu_item (const char *item_text, int width, int face_id,
22299 int x, int y, bool submenu)
22301 struct it it;
22302 struct frame *f = SELECTED_FRAME ();
22303 struct window *w = XWINDOW (f->selected_window);
22304 struct glyph_row *row;
22305 size_t item_len = strlen (item_text);
22307 eassert (FRAME_TERMCAP_P (f));
22309 /* Don't write beyond the matrix's last row. This can happen for
22310 TTY screens that are not high enough to show the entire menu.
22311 (This is actually a bit of defensive programming, as
22312 tty_menu_display already limits the number of menu items to one
22313 less than the number of screen lines.) */
22314 if (y >= f->desired_matrix->nrows)
22315 return;
22317 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22318 it.first_visible_x = 0;
22319 it.last_visible_x = FRAME_COLS (f) - 1;
22320 row = it.glyph_row;
22321 /* Start with the row contents from the current matrix. */
22322 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22323 bool saved_width = row->full_width_p;
22324 row->full_width_p = true;
22325 bool saved_reversed = row->reversed_p;
22326 row->reversed_p = false;
22327 row->enabled_p = true;
22329 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22330 desired face. */
22331 eassert (x < f->desired_matrix->matrix_w);
22332 it.current_x = it.hpos = x;
22333 it.current_y = it.vpos = y;
22334 int saved_used = row->used[TEXT_AREA];
22335 bool saved_truncated = row->truncated_on_right_p;
22336 row->used[TEXT_AREA] = x;
22337 it.face_id = face_id;
22338 it.line_wrap = TRUNCATE;
22340 /* FIXME: This should be controlled by a user option. See the
22341 comments in redisplay_tool_bar and display_mode_line about this.
22342 Also, if paragraph_embedding could ever be R2L, changes will be
22343 needed to avoid shifting to the right the row characters in
22344 term.c:append_glyph. */
22345 it.paragraph_embedding = L2R;
22347 /* Pad with a space on the left. */
22348 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22349 width--;
22350 /* Display the menu item, pad with spaces to WIDTH. */
22351 if (submenu)
22353 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22354 item_len, 0, FRAME_COLS (f) - 1, -1);
22355 width -= item_len;
22356 /* Indicate with " >" that there's a submenu. */
22357 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22358 FRAME_COLS (f) - 1, -1);
22360 else
22361 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22362 width, 0, FRAME_COLS (f) - 1, -1);
22364 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22365 row->truncated_on_right_p = saved_truncated;
22366 row->hash = row_hash (row);
22367 row->full_width_p = saved_width;
22368 row->reversed_p = saved_reversed;
22371 /***********************************************************************
22372 Mode Line
22373 ***********************************************************************/
22375 /* Redisplay mode lines in the window tree whose root is WINDOW.
22376 If FORCE, redisplay mode lines unconditionally.
22377 Otherwise, redisplay only mode lines that are garbaged. Value is
22378 the number of windows whose mode lines were redisplayed. */
22380 static int
22381 redisplay_mode_lines (Lisp_Object window, bool force)
22383 int nwindows = 0;
22385 while (!NILP (window))
22387 struct window *w = XWINDOW (window);
22389 if (WINDOWP (w->contents))
22390 nwindows += redisplay_mode_lines (w->contents, force);
22391 else if (force
22392 || FRAME_GARBAGED_P (XFRAME (w->frame))
22393 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22395 struct text_pos lpoint;
22396 struct buffer *old = current_buffer;
22398 /* Set the window's buffer for the mode line display. */
22399 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22400 set_buffer_internal_1 (XBUFFER (w->contents));
22402 /* Point refers normally to the selected window. For any
22403 other window, set up appropriate value. */
22404 if (!EQ (window, selected_window))
22406 struct text_pos pt;
22408 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22409 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22412 /* Display mode lines. */
22413 clear_glyph_matrix (w->desired_matrix);
22414 if (display_mode_lines (w))
22415 ++nwindows;
22417 /* Restore old settings. */
22418 set_buffer_internal_1 (old);
22419 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22422 window = w->next;
22425 return nwindows;
22429 /* Display the mode and/or header line of window W. Value is the
22430 sum number of mode lines and header lines displayed. */
22432 static int
22433 display_mode_lines (struct window *w)
22435 Lisp_Object old_selected_window = selected_window;
22436 Lisp_Object old_selected_frame = selected_frame;
22437 Lisp_Object new_frame = w->frame;
22438 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22439 int n = 0;
22441 selected_frame = new_frame;
22442 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22443 or window's point, then we'd need select_window_1 here as well. */
22444 XSETWINDOW (selected_window, w);
22445 XFRAME (new_frame)->selected_window = selected_window;
22447 /* These will be set while the mode line specs are processed. */
22448 line_number_displayed = false;
22449 w->column_number_displayed = -1;
22451 if (WINDOW_WANTS_MODELINE_P (w))
22453 struct window *sel_w = XWINDOW (old_selected_window);
22455 /* Select mode line face based on the real selected window. */
22456 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22457 BVAR (current_buffer, mode_line_format));
22458 ++n;
22461 if (WINDOW_WANTS_HEADER_LINE_P (w))
22463 display_mode_line (w, HEADER_LINE_FACE_ID,
22464 BVAR (current_buffer, header_line_format));
22465 ++n;
22468 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22469 selected_frame = old_selected_frame;
22470 selected_window = old_selected_window;
22471 if (n > 0)
22472 w->must_be_updated_p = true;
22473 return n;
22477 /* Display mode or header line of window W. FACE_ID specifies which
22478 line to display; it is either MODE_LINE_FACE_ID or
22479 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22480 display. Value is the pixel height of the mode/header line
22481 displayed. */
22483 static int
22484 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22486 struct it it;
22487 struct face *face;
22488 ptrdiff_t count = SPECPDL_INDEX ();
22490 init_iterator (&it, w, -1, -1, NULL, face_id);
22491 /* Don't extend on a previously drawn mode-line.
22492 This may happen if called from pos_visible_p. */
22493 it.glyph_row->enabled_p = false;
22494 prepare_desired_row (w, it.glyph_row, true);
22496 it.glyph_row->mode_line_p = true;
22498 /* FIXME: This should be controlled by a user option. But
22499 supporting such an option is not trivial, since the mode line is
22500 made up of many separate strings. */
22501 it.paragraph_embedding = L2R;
22503 record_unwind_protect (unwind_format_mode_line,
22504 format_mode_line_unwind_data (NULL, NULL,
22505 Qnil, false));
22507 mode_line_target = MODE_LINE_DISPLAY;
22509 /* Temporarily make frame's keyboard the current kboard so that
22510 kboard-local variables in the mode_line_format will get the right
22511 values. */
22512 push_kboard (FRAME_KBOARD (it.f));
22513 record_unwind_save_match_data ();
22514 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22515 pop_kboard ();
22517 unbind_to (count, Qnil);
22519 /* Fill up with spaces. */
22520 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22522 compute_line_metrics (&it);
22523 it.glyph_row->full_width_p = true;
22524 it.glyph_row->continued_p = false;
22525 it.glyph_row->truncated_on_left_p = false;
22526 it.glyph_row->truncated_on_right_p = false;
22528 /* Make a 3D mode-line have a shadow at its right end. */
22529 face = FACE_FROM_ID (it.f, face_id);
22530 extend_face_to_end_of_line (&it);
22531 if (face->box != FACE_NO_BOX)
22533 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22534 + it.glyph_row->used[TEXT_AREA] - 1);
22535 last->right_box_line_p = true;
22538 return it.glyph_row->height;
22541 /* Move element ELT in LIST to the front of LIST.
22542 Return the updated list. */
22544 static Lisp_Object
22545 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22547 register Lisp_Object tail, prev;
22548 register Lisp_Object tem;
22550 tail = list;
22551 prev = Qnil;
22552 while (CONSP (tail))
22554 tem = XCAR (tail);
22556 if (EQ (elt, tem))
22558 /* Splice out the link TAIL. */
22559 if (NILP (prev))
22560 list = XCDR (tail);
22561 else
22562 Fsetcdr (prev, XCDR (tail));
22564 /* Now make it the first. */
22565 Fsetcdr (tail, list);
22566 return tail;
22568 else
22569 prev = tail;
22570 tail = XCDR (tail);
22571 QUIT;
22574 /* Not found--return unchanged LIST. */
22575 return list;
22578 /* Contribute ELT to the mode line for window IT->w. How it
22579 translates into text depends on its data type.
22581 IT describes the display environment in which we display, as usual.
22583 DEPTH is the depth in recursion. It is used to prevent
22584 infinite recursion here.
22586 FIELD_WIDTH is the number of characters the display of ELT should
22587 occupy in the mode line, and PRECISION is the maximum number of
22588 characters to display from ELT's representation. See
22589 display_string for details.
22591 Returns the hpos of the end of the text generated by ELT.
22593 PROPS is a property list to add to any string we encounter.
22595 If RISKY, remove (disregard) any properties in any string
22596 we encounter, and ignore :eval and :propertize.
22598 The global variable `mode_line_target' determines whether the
22599 output is passed to `store_mode_line_noprop',
22600 `store_mode_line_string', or `display_string'. */
22602 static int
22603 display_mode_element (struct it *it, int depth, int field_width, int precision,
22604 Lisp_Object elt, Lisp_Object props, bool risky)
22606 int n = 0, field, prec;
22607 bool literal = false;
22609 tail_recurse:
22610 if (depth > 100)
22611 elt = build_string ("*too-deep*");
22613 depth++;
22615 switch (XTYPE (elt))
22617 case Lisp_String:
22619 /* A string: output it and check for %-constructs within it. */
22620 unsigned char c;
22621 ptrdiff_t offset = 0;
22623 if (SCHARS (elt) > 0
22624 && (!NILP (props) || risky))
22626 Lisp_Object oprops, aelt;
22627 oprops = Ftext_properties_at (make_number (0), elt);
22629 /* If the starting string's properties are not what
22630 we want, translate the string. Also, if the string
22631 is risky, do that anyway. */
22633 if (NILP (Fequal (props, oprops)) || risky)
22635 /* If the starting string has properties,
22636 merge the specified ones onto the existing ones. */
22637 if (! NILP (oprops) && !risky)
22639 Lisp_Object tem;
22641 oprops = Fcopy_sequence (oprops);
22642 tem = props;
22643 while (CONSP (tem))
22645 oprops = Fplist_put (oprops, XCAR (tem),
22646 XCAR (XCDR (tem)));
22647 tem = XCDR (XCDR (tem));
22649 props = oprops;
22652 aelt = Fassoc (elt, mode_line_proptrans_alist);
22653 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22655 /* AELT is what we want. Move it to the front
22656 without consing. */
22657 elt = XCAR (aelt);
22658 mode_line_proptrans_alist
22659 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22661 else
22663 Lisp_Object tem;
22665 /* If AELT has the wrong props, it is useless.
22666 so get rid of it. */
22667 if (! NILP (aelt))
22668 mode_line_proptrans_alist
22669 = Fdelq (aelt, mode_line_proptrans_alist);
22671 elt = Fcopy_sequence (elt);
22672 Fset_text_properties (make_number (0), Flength (elt),
22673 props, elt);
22674 /* Add this item to mode_line_proptrans_alist. */
22675 mode_line_proptrans_alist
22676 = Fcons (Fcons (elt, props),
22677 mode_line_proptrans_alist);
22678 /* Truncate mode_line_proptrans_alist
22679 to at most 50 elements. */
22680 tem = Fnthcdr (make_number (50),
22681 mode_line_proptrans_alist);
22682 if (! NILP (tem))
22683 XSETCDR (tem, Qnil);
22688 offset = 0;
22690 if (literal)
22692 prec = precision - n;
22693 switch (mode_line_target)
22695 case MODE_LINE_NOPROP:
22696 case MODE_LINE_TITLE:
22697 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22698 break;
22699 case MODE_LINE_STRING:
22700 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22701 break;
22702 case MODE_LINE_DISPLAY:
22703 n += display_string (NULL, elt, Qnil, 0, 0, it,
22704 0, prec, 0, STRING_MULTIBYTE (elt));
22705 break;
22708 break;
22711 /* Handle the non-literal case. */
22713 while ((precision <= 0 || n < precision)
22714 && SREF (elt, offset) != 0
22715 && (mode_line_target != MODE_LINE_DISPLAY
22716 || it->current_x < it->last_visible_x))
22718 ptrdiff_t last_offset = offset;
22720 /* Advance to end of string or next format specifier. */
22721 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22724 if (offset - 1 != last_offset)
22726 ptrdiff_t nchars, nbytes;
22728 /* Output to end of string or up to '%'. Field width
22729 is length of string. Don't output more than
22730 PRECISION allows us. */
22731 offset--;
22733 prec = c_string_width (SDATA (elt) + last_offset,
22734 offset - last_offset, precision - n,
22735 &nchars, &nbytes);
22737 switch (mode_line_target)
22739 case MODE_LINE_NOPROP:
22740 case MODE_LINE_TITLE:
22741 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22742 break;
22743 case MODE_LINE_STRING:
22745 ptrdiff_t bytepos = last_offset;
22746 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22747 ptrdiff_t endpos = (precision <= 0
22748 ? string_byte_to_char (elt, offset)
22749 : charpos + nchars);
22750 Lisp_Object mode_string
22751 = Fsubstring (elt, make_number (charpos),
22752 make_number (endpos));
22753 n += store_mode_line_string (NULL, mode_string, false,
22754 0, 0, Qnil);
22756 break;
22757 case MODE_LINE_DISPLAY:
22759 ptrdiff_t bytepos = last_offset;
22760 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22762 if (precision <= 0)
22763 nchars = string_byte_to_char (elt, offset) - charpos;
22764 n += display_string (NULL, elt, Qnil, 0, charpos,
22765 it, 0, nchars, 0,
22766 STRING_MULTIBYTE (elt));
22768 break;
22771 else /* c == '%' */
22773 ptrdiff_t percent_position = offset;
22775 /* Get the specified minimum width. Zero means
22776 don't pad. */
22777 field = 0;
22778 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22779 field = field * 10 + c - '0';
22781 /* Don't pad beyond the total padding allowed. */
22782 if (field_width - n > 0 && field > field_width - n)
22783 field = field_width - n;
22785 /* Note that either PRECISION <= 0 or N < PRECISION. */
22786 prec = precision - n;
22788 if (c == 'M')
22789 n += display_mode_element (it, depth, field, prec,
22790 Vglobal_mode_string, props,
22791 risky);
22792 else if (c != 0)
22794 bool multibyte;
22795 ptrdiff_t bytepos, charpos;
22796 const char *spec;
22797 Lisp_Object string;
22799 bytepos = percent_position;
22800 charpos = (STRING_MULTIBYTE (elt)
22801 ? string_byte_to_char (elt, bytepos)
22802 : bytepos);
22803 spec = decode_mode_spec (it->w, c, field, &string);
22804 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22806 switch (mode_line_target)
22808 case MODE_LINE_NOPROP:
22809 case MODE_LINE_TITLE:
22810 n += store_mode_line_noprop (spec, field, prec);
22811 break;
22812 case MODE_LINE_STRING:
22814 Lisp_Object tem = build_string (spec);
22815 props = Ftext_properties_at (make_number (charpos), elt);
22816 /* Should only keep face property in props */
22817 n += store_mode_line_string (NULL, tem, false,
22818 field, prec, props);
22820 break;
22821 case MODE_LINE_DISPLAY:
22823 int nglyphs_before, nwritten;
22825 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22826 nwritten = display_string (spec, string, elt,
22827 charpos, 0, it,
22828 field, prec, 0,
22829 multibyte);
22831 /* Assign to the glyphs written above the
22832 string where the `%x' came from, position
22833 of the `%'. */
22834 if (nwritten > 0)
22836 struct glyph *glyph
22837 = (it->glyph_row->glyphs[TEXT_AREA]
22838 + nglyphs_before);
22839 int i;
22841 for (i = 0; i < nwritten; ++i)
22843 glyph[i].object = elt;
22844 glyph[i].charpos = charpos;
22847 n += nwritten;
22850 break;
22853 else /* c == 0 */
22854 break;
22858 break;
22860 case Lisp_Symbol:
22861 /* A symbol: process the value of the symbol recursively
22862 as if it appeared here directly. Avoid error if symbol void.
22863 Special case: if value of symbol is a string, output the string
22864 literally. */
22866 register Lisp_Object tem;
22868 /* If the variable is not marked as risky to set
22869 then its contents are risky to use. */
22870 if (NILP (Fget (elt, Qrisky_local_variable)))
22871 risky = true;
22873 tem = Fboundp (elt);
22874 if (!NILP (tem))
22876 tem = Fsymbol_value (elt);
22877 /* If value is a string, output that string literally:
22878 don't check for % within it. */
22879 if (STRINGP (tem))
22880 literal = true;
22882 if (!EQ (tem, elt))
22884 /* Give up right away for nil or t. */
22885 elt = tem;
22886 goto tail_recurse;
22890 break;
22892 case Lisp_Cons:
22894 register Lisp_Object car, tem;
22896 /* A cons cell: five distinct cases.
22897 If first element is :eval or :propertize, do something special.
22898 If first element is a string or a cons, process all the elements
22899 and effectively concatenate them.
22900 If first element is a negative number, truncate displaying cdr to
22901 at most that many characters. If positive, pad (with spaces)
22902 to at least that many characters.
22903 If first element is a symbol, process the cadr or caddr recursively
22904 according to whether the symbol's value is non-nil or nil. */
22905 car = XCAR (elt);
22906 if (EQ (car, QCeval))
22908 /* An element of the form (:eval FORM) means evaluate FORM
22909 and use the result as mode line elements. */
22911 if (risky)
22912 break;
22914 if (CONSP (XCDR (elt)))
22916 Lisp_Object spec;
22917 spec = safe__eval (true, XCAR (XCDR (elt)));
22918 n += display_mode_element (it, depth, field_width - n,
22919 precision - n, spec, props,
22920 risky);
22923 else if (EQ (car, QCpropertize))
22925 /* An element of the form (:propertize ELT PROPS...)
22926 means display ELT but applying properties PROPS. */
22928 if (risky)
22929 break;
22931 if (CONSP (XCDR (elt)))
22932 n += display_mode_element (it, depth, field_width - n,
22933 precision - n, XCAR (XCDR (elt)),
22934 XCDR (XCDR (elt)), risky);
22936 else if (SYMBOLP (car))
22938 tem = Fboundp (car);
22939 elt = XCDR (elt);
22940 if (!CONSP (elt))
22941 goto invalid;
22942 /* elt is now the cdr, and we know it is a cons cell.
22943 Use its car if CAR has a non-nil value. */
22944 if (!NILP (tem))
22946 tem = Fsymbol_value (car);
22947 if (!NILP (tem))
22949 elt = XCAR (elt);
22950 goto tail_recurse;
22953 /* Symbol's value is nil (or symbol is unbound)
22954 Get the cddr of the original list
22955 and if possible find the caddr and use that. */
22956 elt = XCDR (elt);
22957 if (NILP (elt))
22958 break;
22959 else if (!CONSP (elt))
22960 goto invalid;
22961 elt = XCAR (elt);
22962 goto tail_recurse;
22964 else if (INTEGERP (car))
22966 register int lim = XINT (car);
22967 elt = XCDR (elt);
22968 if (lim < 0)
22970 /* Negative int means reduce maximum width. */
22971 if (precision <= 0)
22972 precision = -lim;
22973 else
22974 precision = min (precision, -lim);
22976 else if (lim > 0)
22978 /* Padding specified. Don't let it be more than
22979 current maximum. */
22980 if (precision > 0)
22981 lim = min (precision, lim);
22983 /* If that's more padding than already wanted, queue it.
22984 But don't reduce padding already specified even if
22985 that is beyond the current truncation point. */
22986 field_width = max (lim, field_width);
22988 goto tail_recurse;
22990 else if (STRINGP (car) || CONSP (car))
22992 Lisp_Object halftail = elt;
22993 int len = 0;
22995 while (CONSP (elt)
22996 && (precision <= 0 || n < precision))
22998 n += display_mode_element (it, depth,
22999 /* Do padding only after the last
23000 element in the list. */
23001 (! CONSP (XCDR (elt))
23002 ? field_width - n
23003 : 0),
23004 precision - n, XCAR (elt),
23005 props, risky);
23006 elt = XCDR (elt);
23007 len++;
23008 if ((len & 1) == 0)
23009 halftail = XCDR (halftail);
23010 /* Check for cycle. */
23011 if (EQ (halftail, elt))
23012 break;
23016 break;
23018 default:
23019 invalid:
23020 elt = build_string ("*invalid*");
23021 goto tail_recurse;
23024 /* Pad to FIELD_WIDTH. */
23025 if (field_width > 0 && n < field_width)
23027 switch (mode_line_target)
23029 case MODE_LINE_NOPROP:
23030 case MODE_LINE_TITLE:
23031 n += store_mode_line_noprop ("", field_width - n, 0);
23032 break;
23033 case MODE_LINE_STRING:
23034 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23035 Qnil);
23036 break;
23037 case MODE_LINE_DISPLAY:
23038 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23039 0, 0, 0);
23040 break;
23044 return n;
23047 /* Store a mode-line string element in mode_line_string_list.
23049 If STRING is non-null, display that C string. Otherwise, the Lisp
23050 string LISP_STRING is displayed.
23052 FIELD_WIDTH is the minimum number of output glyphs to produce.
23053 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23054 with spaces. FIELD_WIDTH <= 0 means don't pad.
23056 PRECISION is the maximum number of characters to output from
23057 STRING. PRECISION <= 0 means don't truncate the string.
23059 If COPY_STRING, make a copy of LISP_STRING before adding
23060 properties to the string.
23062 PROPS are the properties to add to the string.
23063 The mode_line_string_face face property is always added to the string.
23066 static int
23067 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23068 bool copy_string,
23069 int field_width, int precision, Lisp_Object props)
23071 ptrdiff_t len;
23072 int n = 0;
23074 if (string != NULL)
23076 len = strlen (string);
23077 if (precision > 0 && len > precision)
23078 len = precision;
23079 lisp_string = make_string (string, len);
23080 if (NILP (props))
23081 props = mode_line_string_face_prop;
23082 else if (!NILP (mode_line_string_face))
23084 Lisp_Object face = Fplist_get (props, Qface);
23085 props = Fcopy_sequence (props);
23086 if (NILP (face))
23087 face = mode_line_string_face;
23088 else
23089 face = list2 (face, mode_line_string_face);
23090 props = Fplist_put (props, Qface, face);
23092 Fadd_text_properties (make_number (0), make_number (len),
23093 props, lisp_string);
23095 else
23097 len = XFASTINT (Flength (lisp_string));
23098 if (precision > 0 && len > precision)
23100 len = precision;
23101 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23102 precision = -1;
23104 if (!NILP (mode_line_string_face))
23106 Lisp_Object face;
23107 if (NILP (props))
23108 props = Ftext_properties_at (make_number (0), lisp_string);
23109 face = Fplist_get (props, Qface);
23110 if (NILP (face))
23111 face = mode_line_string_face;
23112 else
23113 face = list2 (face, mode_line_string_face);
23114 props = list2 (Qface, face);
23115 if (copy_string)
23116 lisp_string = Fcopy_sequence (lisp_string);
23118 if (!NILP (props))
23119 Fadd_text_properties (make_number (0), make_number (len),
23120 props, lisp_string);
23123 if (len > 0)
23125 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23126 n += len;
23129 if (field_width > len)
23131 field_width -= len;
23132 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23133 if (!NILP (props))
23134 Fadd_text_properties (make_number (0), make_number (field_width),
23135 props, lisp_string);
23136 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23137 n += field_width;
23140 return n;
23144 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23145 1, 4, 0,
23146 doc: /* Format a string out of a mode line format specification.
23147 First arg FORMAT specifies the mode line format (see `mode-line-format'
23148 for details) to use.
23150 By default, the format is evaluated for the currently selected window.
23152 Optional second arg FACE specifies the face property to put on all
23153 characters for which no face is specified. The value nil means the
23154 default face. The value t means whatever face the window's mode line
23155 currently uses (either `mode-line' or `mode-line-inactive',
23156 depending on whether the window is the selected window or not).
23157 An integer value means the value string has no text
23158 properties.
23160 Optional third and fourth args WINDOW and BUFFER specify the window
23161 and buffer to use as the context for the formatting (defaults
23162 are the selected window and the WINDOW's buffer). */)
23163 (Lisp_Object format, Lisp_Object face,
23164 Lisp_Object window, Lisp_Object buffer)
23166 struct it it;
23167 int len;
23168 struct window *w;
23169 struct buffer *old_buffer = NULL;
23170 int face_id;
23171 bool no_props = INTEGERP (face);
23172 ptrdiff_t count = SPECPDL_INDEX ();
23173 Lisp_Object str;
23174 int string_start = 0;
23176 w = decode_any_window (window);
23177 XSETWINDOW (window, w);
23179 if (NILP (buffer))
23180 buffer = w->contents;
23181 CHECK_BUFFER (buffer);
23183 /* Make formatting the modeline a non-op when noninteractive, otherwise
23184 there will be problems later caused by a partially initialized frame. */
23185 if (NILP (format) || noninteractive)
23186 return empty_unibyte_string;
23188 if (no_props)
23189 face = Qnil;
23191 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23192 : EQ (face, Qt) ? (EQ (window, selected_window)
23193 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23194 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23195 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23196 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23197 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23198 : DEFAULT_FACE_ID;
23200 old_buffer = current_buffer;
23202 /* Save things including mode_line_proptrans_alist,
23203 and set that to nil so that we don't alter the outer value. */
23204 record_unwind_protect (unwind_format_mode_line,
23205 format_mode_line_unwind_data
23206 (XFRAME (WINDOW_FRAME (w)),
23207 old_buffer, selected_window, true));
23208 mode_line_proptrans_alist = Qnil;
23210 Fselect_window (window, Qt);
23211 set_buffer_internal_1 (XBUFFER (buffer));
23213 init_iterator (&it, w, -1, -1, NULL, face_id);
23215 if (no_props)
23217 mode_line_target = MODE_LINE_NOPROP;
23218 mode_line_string_face_prop = Qnil;
23219 mode_line_string_list = Qnil;
23220 string_start = MODE_LINE_NOPROP_LEN (0);
23222 else
23224 mode_line_target = MODE_LINE_STRING;
23225 mode_line_string_list = Qnil;
23226 mode_line_string_face = face;
23227 mode_line_string_face_prop
23228 = NILP (face) ? Qnil : list2 (Qface, face);
23231 push_kboard (FRAME_KBOARD (it.f));
23232 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23233 pop_kboard ();
23235 if (no_props)
23237 len = MODE_LINE_NOPROP_LEN (string_start);
23238 str = make_string (mode_line_noprop_buf + string_start, len);
23240 else
23242 mode_line_string_list = Fnreverse (mode_line_string_list);
23243 str = Fmapconcat (Qidentity, mode_line_string_list,
23244 empty_unibyte_string);
23247 unbind_to (count, Qnil);
23248 return str;
23251 /* Write a null-terminated, right justified decimal representation of
23252 the positive integer D to BUF using a minimal field width WIDTH. */
23254 static void
23255 pint2str (register char *buf, register int width, register ptrdiff_t d)
23257 register char *p = buf;
23259 if (d <= 0)
23260 *p++ = '0';
23261 else
23263 while (d > 0)
23265 *p++ = d % 10 + '0';
23266 d /= 10;
23270 for (width -= (int) (p - buf); width > 0; --width)
23271 *p++ = ' ';
23272 *p-- = '\0';
23273 while (p > buf)
23275 d = *buf;
23276 *buf++ = *p;
23277 *p-- = d;
23281 /* Write a null-terminated, right justified decimal and "human
23282 readable" representation of the nonnegative integer D to BUF using
23283 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23285 static const char power_letter[] =
23287 0, /* no letter */
23288 'k', /* kilo */
23289 'M', /* mega */
23290 'G', /* giga */
23291 'T', /* tera */
23292 'P', /* peta */
23293 'E', /* exa */
23294 'Z', /* zetta */
23295 'Y' /* yotta */
23298 static void
23299 pint2hrstr (char *buf, int width, ptrdiff_t d)
23301 /* We aim to represent the nonnegative integer D as
23302 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23303 ptrdiff_t quotient = d;
23304 int remainder = 0;
23305 /* -1 means: do not use TENTHS. */
23306 int tenths = -1;
23307 int exponent = 0;
23309 /* Length of QUOTIENT.TENTHS as a string. */
23310 int length;
23312 char * psuffix;
23313 char * p;
23315 if (quotient >= 1000)
23317 /* Scale to the appropriate EXPONENT. */
23320 remainder = quotient % 1000;
23321 quotient /= 1000;
23322 exponent++;
23324 while (quotient >= 1000);
23326 /* Round to nearest and decide whether to use TENTHS or not. */
23327 if (quotient <= 9)
23329 tenths = remainder / 100;
23330 if (remainder % 100 >= 50)
23332 if (tenths < 9)
23333 tenths++;
23334 else
23336 quotient++;
23337 if (quotient == 10)
23338 tenths = -1;
23339 else
23340 tenths = 0;
23344 else
23345 if (remainder >= 500)
23347 if (quotient < 999)
23348 quotient++;
23349 else
23351 quotient = 1;
23352 exponent++;
23353 tenths = 0;
23358 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23359 if (tenths == -1 && quotient <= 99)
23360 if (quotient <= 9)
23361 length = 1;
23362 else
23363 length = 2;
23364 else
23365 length = 3;
23366 p = psuffix = buf + max (width, length);
23368 /* Print EXPONENT. */
23369 *psuffix++ = power_letter[exponent];
23370 *psuffix = '\0';
23372 /* Print TENTHS. */
23373 if (tenths >= 0)
23375 *--p = '0' + tenths;
23376 *--p = '.';
23379 /* Print QUOTIENT. */
23382 int digit = quotient % 10;
23383 *--p = '0' + digit;
23385 while ((quotient /= 10) != 0);
23387 /* Print leading spaces. */
23388 while (buf < p)
23389 *--p = ' ';
23392 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23393 If EOL_FLAG, set also a mnemonic character for end-of-line
23394 type of CODING_SYSTEM. Return updated pointer into BUF. */
23396 static unsigned char invalid_eol_type[] = "(*invalid*)";
23398 static char *
23399 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23401 Lisp_Object val;
23402 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23403 const unsigned char *eol_str;
23404 int eol_str_len;
23405 /* The EOL conversion we are using. */
23406 Lisp_Object eoltype;
23408 val = CODING_SYSTEM_SPEC (coding_system);
23409 eoltype = Qnil;
23411 if (!VECTORP (val)) /* Not yet decided. */
23413 *buf++ = multibyte ? '-' : ' ';
23414 if (eol_flag)
23415 eoltype = eol_mnemonic_undecided;
23416 /* Don't mention EOL conversion if it isn't decided. */
23418 else
23420 Lisp_Object attrs;
23421 Lisp_Object eolvalue;
23423 attrs = AREF (val, 0);
23424 eolvalue = AREF (val, 2);
23426 *buf++ = multibyte
23427 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23428 : ' ';
23430 if (eol_flag)
23432 /* The EOL conversion that is normal on this system. */
23434 if (NILP (eolvalue)) /* Not yet decided. */
23435 eoltype = eol_mnemonic_undecided;
23436 else if (VECTORP (eolvalue)) /* Not yet decided. */
23437 eoltype = eol_mnemonic_undecided;
23438 else /* eolvalue is Qunix, Qdos, or Qmac. */
23439 eoltype = (EQ (eolvalue, Qunix)
23440 ? eol_mnemonic_unix
23441 : EQ (eolvalue, Qdos)
23442 ? eol_mnemonic_dos : eol_mnemonic_mac);
23446 if (eol_flag)
23448 /* Mention the EOL conversion if it is not the usual one. */
23449 if (STRINGP (eoltype))
23451 eol_str = SDATA (eoltype);
23452 eol_str_len = SBYTES (eoltype);
23454 else if (CHARACTERP (eoltype))
23456 int c = XFASTINT (eoltype);
23457 return buf + CHAR_STRING (c, (unsigned char *) buf);
23459 else
23461 eol_str = invalid_eol_type;
23462 eol_str_len = sizeof (invalid_eol_type) - 1;
23464 memcpy (buf, eol_str, eol_str_len);
23465 buf += eol_str_len;
23468 return buf;
23471 /* Return the approximate percentage N is of D (rounding upward), or 99,
23472 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23474 static int
23475 percent99 (ptrdiff_t n, ptrdiff_t d)
23477 int percent = (d - 1 + 100.0 * n) / d;
23478 return min (percent, 99);
23481 /* Return a string for the output of a mode line %-spec for window W,
23482 generated by character C. FIELD_WIDTH > 0 means pad the string
23483 returned with spaces to that value. Return a Lisp string in
23484 *STRING if the resulting string is taken from that Lisp string.
23486 Note we operate on the current buffer for most purposes. */
23488 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23490 static const char *
23491 decode_mode_spec (struct window *w, register int c, int field_width,
23492 Lisp_Object *string)
23494 Lisp_Object obj;
23495 struct frame *f = XFRAME (WINDOW_FRAME (w));
23496 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23497 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23498 produce strings from numerical values, so limit preposterously
23499 large values of FIELD_WIDTH to avoid overrunning the buffer's
23500 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23501 bytes plus the terminating null. */
23502 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23503 struct buffer *b = current_buffer;
23505 obj = Qnil;
23506 *string = Qnil;
23508 switch (c)
23510 case '*':
23511 if (!NILP (BVAR (b, read_only)))
23512 return "%";
23513 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23514 return "*";
23515 return "-";
23517 case '+':
23518 /* This differs from %* only for a modified read-only buffer. */
23519 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23520 return "*";
23521 if (!NILP (BVAR (b, read_only)))
23522 return "%";
23523 return "-";
23525 case '&':
23526 /* This differs from %* in ignoring read-only-ness. */
23527 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23528 return "*";
23529 return "-";
23531 case '%':
23532 return "%";
23534 case '[':
23536 int i;
23537 char *p;
23539 if (command_loop_level > 5)
23540 return "[[[... ";
23541 p = decode_mode_spec_buf;
23542 for (i = 0; i < command_loop_level; i++)
23543 *p++ = '[';
23544 *p = 0;
23545 return decode_mode_spec_buf;
23548 case ']':
23550 int i;
23551 char *p;
23553 if (command_loop_level > 5)
23554 return " ...]]]";
23555 p = decode_mode_spec_buf;
23556 for (i = 0; i < command_loop_level; i++)
23557 *p++ = ']';
23558 *p = 0;
23559 return decode_mode_spec_buf;
23562 case '-':
23564 register int i;
23566 /* Let lots_of_dashes be a string of infinite length. */
23567 if (mode_line_target == MODE_LINE_NOPROP
23568 || mode_line_target == MODE_LINE_STRING)
23569 return "--";
23570 if (field_width <= 0
23571 || field_width > sizeof (lots_of_dashes))
23573 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23574 decode_mode_spec_buf[i] = '-';
23575 decode_mode_spec_buf[i] = '\0';
23576 return decode_mode_spec_buf;
23578 else
23579 return lots_of_dashes;
23582 case 'b':
23583 obj = BVAR (b, name);
23584 break;
23586 case 'c':
23587 /* %c and %l are ignored in `frame-title-format'.
23588 (In redisplay_internal, the frame title is drawn _before_ the
23589 windows are updated, so the stuff which depends on actual
23590 window contents (such as %l) may fail to render properly, or
23591 even crash emacs.) */
23592 if (mode_line_target == MODE_LINE_TITLE)
23593 return "";
23594 else
23596 ptrdiff_t col = current_column ();
23597 w->column_number_displayed = col;
23598 pint2str (decode_mode_spec_buf, width, col);
23599 return decode_mode_spec_buf;
23602 case 'e':
23603 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23605 if (NILP (Vmemory_full))
23606 return "";
23607 else
23608 return "!MEM FULL! ";
23610 #else
23611 return "";
23612 #endif
23614 case 'F':
23615 /* %F displays the frame name. */
23616 if (!NILP (f->title))
23617 return SSDATA (f->title);
23618 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23619 return SSDATA (f->name);
23620 return "Emacs";
23622 case 'f':
23623 obj = BVAR (b, filename);
23624 break;
23626 case 'i':
23628 ptrdiff_t size = ZV - BEGV;
23629 pint2str (decode_mode_spec_buf, width, size);
23630 return decode_mode_spec_buf;
23633 case 'I':
23635 ptrdiff_t size = ZV - BEGV;
23636 pint2hrstr (decode_mode_spec_buf, width, size);
23637 return decode_mode_spec_buf;
23640 case 'l':
23642 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23643 ptrdiff_t topline, nlines, height;
23644 ptrdiff_t junk;
23646 /* %c and %l are ignored in `frame-title-format'. */
23647 if (mode_line_target == MODE_LINE_TITLE)
23648 return "";
23650 startpos = marker_position (w->start);
23651 startpos_byte = marker_byte_position (w->start);
23652 height = WINDOW_TOTAL_LINES (w);
23654 /* If we decided that this buffer isn't suitable for line numbers,
23655 don't forget that too fast. */
23656 if (w->base_line_pos == -1)
23657 goto no_value;
23659 /* If the buffer is very big, don't waste time. */
23660 if (INTEGERP (Vline_number_display_limit)
23661 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23663 w->base_line_pos = 0;
23664 w->base_line_number = 0;
23665 goto no_value;
23668 if (w->base_line_number > 0
23669 && w->base_line_pos > 0
23670 && w->base_line_pos <= startpos)
23672 line = w->base_line_number;
23673 linepos = w->base_line_pos;
23674 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23676 else
23678 line = 1;
23679 linepos = BUF_BEGV (b);
23680 linepos_byte = BUF_BEGV_BYTE (b);
23683 /* Count lines from base line to window start position. */
23684 nlines = display_count_lines (linepos_byte,
23685 startpos_byte,
23686 startpos, &junk);
23688 topline = nlines + line;
23690 /* Determine a new base line, if the old one is too close
23691 or too far away, or if we did not have one.
23692 "Too close" means it's plausible a scroll-down would
23693 go back past it. */
23694 if (startpos == BUF_BEGV (b))
23696 w->base_line_number = topline;
23697 w->base_line_pos = BUF_BEGV (b);
23699 else if (nlines < height + 25 || nlines > height * 3 + 50
23700 || linepos == BUF_BEGV (b))
23702 ptrdiff_t limit = BUF_BEGV (b);
23703 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23704 ptrdiff_t position;
23705 ptrdiff_t distance =
23706 (height * 2 + 30) * line_number_display_limit_width;
23708 if (startpos - distance > limit)
23710 limit = startpos - distance;
23711 limit_byte = CHAR_TO_BYTE (limit);
23714 nlines = display_count_lines (startpos_byte,
23715 limit_byte,
23716 - (height * 2 + 30),
23717 &position);
23718 /* If we couldn't find the lines we wanted within
23719 line_number_display_limit_width chars per line,
23720 give up on line numbers for this window. */
23721 if (position == limit_byte && limit == startpos - distance)
23723 w->base_line_pos = -1;
23724 w->base_line_number = 0;
23725 goto no_value;
23728 w->base_line_number = topline - nlines;
23729 w->base_line_pos = BYTE_TO_CHAR (position);
23732 /* Now count lines from the start pos to point. */
23733 nlines = display_count_lines (startpos_byte,
23734 PT_BYTE, PT, &junk);
23736 /* Record that we did display the line number. */
23737 line_number_displayed = true;
23739 /* Make the string to show. */
23740 pint2str (decode_mode_spec_buf, width, topline + nlines);
23741 return decode_mode_spec_buf;
23742 no_value:
23744 char *p = decode_mode_spec_buf;
23745 int pad = width - 2;
23746 while (pad-- > 0)
23747 *p++ = ' ';
23748 *p++ = '?';
23749 *p++ = '?';
23750 *p = '\0';
23751 return decode_mode_spec_buf;
23754 break;
23756 case 'm':
23757 obj = BVAR (b, mode_name);
23758 break;
23760 case 'n':
23761 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23762 return " Narrow";
23763 break;
23765 case 'p':
23767 ptrdiff_t pos = marker_position (w->start);
23768 ptrdiff_t begv = BUF_BEGV (b);
23769 ptrdiff_t zv = BUF_ZV (b);
23771 if (w->window_end_pos <= BUF_Z (b) - zv)
23772 return pos <= begv ? "All" : "Bottom";
23773 else if (pos <= begv)
23774 return "Top";
23775 else
23777 sprintf (decode_mode_spec_buf, "%2d%%",
23778 percent99 (pos - begv, zv - begv));
23779 return decode_mode_spec_buf;
23783 /* Display percentage of size above the bottom of the screen. */
23784 case 'P':
23786 ptrdiff_t toppos = marker_position (w->start);
23787 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23788 ptrdiff_t begv = BUF_BEGV (b);
23789 ptrdiff_t zv = BUF_ZV (b);
23791 if (zv <= botpos)
23792 return toppos <= begv ? "All" : "Bottom";
23793 else
23795 sprintf (decode_mode_spec_buf,
23796 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23797 percent99 (botpos - begv, zv - begv));
23798 return decode_mode_spec_buf;
23802 case 's':
23803 /* status of process */
23804 obj = Fget_buffer_process (Fcurrent_buffer ());
23805 if (NILP (obj))
23806 return "no process";
23807 #ifndef MSDOS
23808 obj = Fsymbol_name (Fprocess_status (obj));
23809 #endif
23810 break;
23812 case '@':
23814 ptrdiff_t count = inhibit_garbage_collection ();
23815 Lisp_Object curdir = BVAR (current_buffer, directory);
23816 Lisp_Object val = Qnil;
23818 if (STRINGP (curdir))
23819 val = call1 (intern ("file-remote-p"), curdir);
23821 unbind_to (count, Qnil);
23823 if (NILP (val))
23824 return "-";
23825 else
23826 return "@";
23829 case 'z':
23830 /* coding-system (not including end-of-line format) */
23831 case 'Z':
23832 /* coding-system (including end-of-line type) */
23834 bool eol_flag = (c == 'Z');
23835 char *p = decode_mode_spec_buf;
23837 if (! FRAME_WINDOW_P (f))
23839 /* No need to mention EOL here--the terminal never needs
23840 to do EOL conversion. */
23841 p = decode_mode_spec_coding (CODING_ID_NAME
23842 (FRAME_KEYBOARD_CODING (f)->id),
23843 p, false);
23844 p = decode_mode_spec_coding (CODING_ID_NAME
23845 (FRAME_TERMINAL_CODING (f)->id),
23846 p, false);
23848 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23849 p, eol_flag);
23851 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23852 #ifdef subprocesses
23853 obj = Fget_buffer_process (Fcurrent_buffer ());
23854 if (PROCESSP (obj))
23856 p = decode_mode_spec_coding
23857 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23858 p = decode_mode_spec_coding
23859 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23861 #endif /* subprocesses */
23862 #endif /* false */
23863 *p = 0;
23864 return decode_mode_spec_buf;
23868 if (STRINGP (obj))
23870 *string = obj;
23871 return SSDATA (obj);
23873 else
23874 return "";
23878 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23879 means count lines back from START_BYTE. But don't go beyond
23880 LIMIT_BYTE. Return the number of lines thus found (always
23881 nonnegative).
23883 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23884 either the position COUNT lines after/before START_BYTE, if we
23885 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23886 COUNT lines. */
23888 static ptrdiff_t
23889 display_count_lines (ptrdiff_t start_byte,
23890 ptrdiff_t limit_byte, ptrdiff_t count,
23891 ptrdiff_t *byte_pos_ptr)
23893 register unsigned char *cursor;
23894 unsigned char *base;
23896 register ptrdiff_t ceiling;
23897 register unsigned char *ceiling_addr;
23898 ptrdiff_t orig_count = count;
23900 /* If we are not in selective display mode,
23901 check only for newlines. */
23902 bool selective_display
23903 = (!NILP (BVAR (current_buffer, selective_display))
23904 && !INTEGERP (BVAR (current_buffer, selective_display)));
23906 if (count > 0)
23908 while (start_byte < limit_byte)
23910 ceiling = BUFFER_CEILING_OF (start_byte);
23911 ceiling = min (limit_byte - 1, ceiling);
23912 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23913 base = (cursor = BYTE_POS_ADDR (start_byte));
23917 if (selective_display)
23919 while (*cursor != '\n' && *cursor != 015
23920 && ++cursor != ceiling_addr)
23921 continue;
23922 if (cursor == ceiling_addr)
23923 break;
23925 else
23927 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23928 if (! cursor)
23929 break;
23932 cursor++;
23934 if (--count == 0)
23936 start_byte += cursor - base;
23937 *byte_pos_ptr = start_byte;
23938 return orig_count;
23941 while (cursor < ceiling_addr);
23943 start_byte += ceiling_addr - base;
23946 else
23948 while (start_byte > limit_byte)
23950 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23951 ceiling = max (limit_byte, ceiling);
23952 ceiling_addr = BYTE_POS_ADDR (ceiling);
23953 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23954 while (true)
23956 if (selective_display)
23958 while (--cursor >= ceiling_addr
23959 && *cursor != '\n' && *cursor != 015)
23960 continue;
23961 if (cursor < ceiling_addr)
23962 break;
23964 else
23966 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23967 if (! cursor)
23968 break;
23971 if (++count == 0)
23973 start_byte += cursor - base + 1;
23974 *byte_pos_ptr = start_byte;
23975 /* When scanning backwards, we should
23976 not count the newline posterior to which we stop. */
23977 return - orig_count - 1;
23980 start_byte += ceiling_addr - base;
23984 *byte_pos_ptr = limit_byte;
23986 if (count < 0)
23987 return - orig_count + count;
23988 return orig_count - count;
23994 /***********************************************************************
23995 Displaying strings
23996 ***********************************************************************/
23998 /* Display a NUL-terminated string, starting with index START.
24000 If STRING is non-null, display that C string. Otherwise, the Lisp
24001 string LISP_STRING is displayed. There's a case that STRING is
24002 non-null and LISP_STRING is not nil. It means STRING is a string
24003 data of LISP_STRING. In that case, we display LISP_STRING while
24004 ignoring its text properties.
24006 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24007 FACE_STRING. Display STRING or LISP_STRING with the face at
24008 FACE_STRING_POS in FACE_STRING:
24010 Display the string in the environment given by IT, but use the
24011 standard display table, temporarily.
24013 FIELD_WIDTH is the minimum number of output glyphs to produce.
24014 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24015 with spaces. If STRING has more characters, more than FIELD_WIDTH
24016 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24018 PRECISION is the maximum number of characters to output from
24019 STRING. PRECISION < 0 means don't truncate the string.
24021 This is roughly equivalent to printf format specifiers:
24023 FIELD_WIDTH PRECISION PRINTF
24024 ----------------------------------------
24025 -1 -1 %s
24026 -1 10 %.10s
24027 10 -1 %10s
24028 20 10 %20.10s
24030 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24031 display them, and < 0 means obey the current buffer's value of
24032 enable_multibyte_characters.
24034 Value is the number of columns displayed. */
24036 static int
24037 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24038 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24039 int field_width, int precision, int max_x, int multibyte)
24041 int hpos_at_start = it->hpos;
24042 int saved_face_id = it->face_id;
24043 struct glyph_row *row = it->glyph_row;
24044 ptrdiff_t it_charpos;
24046 /* Initialize the iterator IT for iteration over STRING beginning
24047 with index START. */
24048 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24049 precision, field_width, multibyte);
24050 if (string && STRINGP (lisp_string))
24051 /* LISP_STRING is the one returned by decode_mode_spec. We should
24052 ignore its text properties. */
24053 it->stop_charpos = it->end_charpos;
24055 /* If displaying STRING, set up the face of the iterator from
24056 FACE_STRING, if that's given. */
24057 if (STRINGP (face_string))
24059 ptrdiff_t endptr;
24060 struct face *face;
24062 it->face_id
24063 = face_at_string_position (it->w, face_string, face_string_pos,
24064 0, &endptr, it->base_face_id, false);
24065 face = FACE_FROM_ID (it->f, it->face_id);
24066 it->face_box_p = face->box != FACE_NO_BOX;
24069 /* Set max_x to the maximum allowed X position. Don't let it go
24070 beyond the right edge of the window. */
24071 if (max_x <= 0)
24072 max_x = it->last_visible_x;
24073 else
24074 max_x = min (max_x, it->last_visible_x);
24076 /* Skip over display elements that are not visible. because IT->w is
24077 hscrolled. */
24078 if (it->current_x < it->first_visible_x)
24079 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24080 MOVE_TO_POS | MOVE_TO_X);
24082 row->ascent = it->max_ascent;
24083 row->height = it->max_ascent + it->max_descent;
24084 row->phys_ascent = it->max_phys_ascent;
24085 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24086 row->extra_line_spacing = it->max_extra_line_spacing;
24088 if (STRINGP (it->string))
24089 it_charpos = IT_STRING_CHARPOS (*it);
24090 else
24091 it_charpos = IT_CHARPOS (*it);
24093 /* This condition is for the case that we are called with current_x
24094 past last_visible_x. */
24095 while (it->current_x < max_x)
24097 int x_before, x, n_glyphs_before, i, nglyphs;
24099 /* Get the next display element. */
24100 if (!get_next_display_element (it))
24101 break;
24103 /* Produce glyphs. */
24104 x_before = it->current_x;
24105 n_glyphs_before = row->used[TEXT_AREA];
24106 PRODUCE_GLYPHS (it);
24108 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24109 i = 0;
24110 x = x_before;
24111 while (i < nglyphs)
24113 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24115 if (it->line_wrap != TRUNCATE
24116 && x + glyph->pixel_width > max_x)
24118 /* End of continued line or max_x reached. */
24119 if (CHAR_GLYPH_PADDING_P (*glyph))
24121 /* A wide character is unbreakable. */
24122 if (row->reversed_p)
24123 unproduce_glyphs (it, row->used[TEXT_AREA]
24124 - n_glyphs_before);
24125 row->used[TEXT_AREA] = n_glyphs_before;
24126 it->current_x = x_before;
24128 else
24130 if (row->reversed_p)
24131 unproduce_glyphs (it, row->used[TEXT_AREA]
24132 - (n_glyphs_before + i));
24133 row->used[TEXT_AREA] = n_glyphs_before + i;
24134 it->current_x = x;
24136 break;
24138 else if (x + glyph->pixel_width >= it->first_visible_x)
24140 /* Glyph is at least partially visible. */
24141 ++it->hpos;
24142 if (x < it->first_visible_x)
24143 row->x = x - it->first_visible_x;
24145 else
24147 /* Glyph is off the left margin of the display area.
24148 Should not happen. */
24149 emacs_abort ();
24152 row->ascent = max (row->ascent, it->max_ascent);
24153 row->height = max (row->height, it->max_ascent + it->max_descent);
24154 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24155 row->phys_height = max (row->phys_height,
24156 it->max_phys_ascent + it->max_phys_descent);
24157 row->extra_line_spacing = max (row->extra_line_spacing,
24158 it->max_extra_line_spacing);
24159 x += glyph->pixel_width;
24160 ++i;
24163 /* Stop if max_x reached. */
24164 if (i < nglyphs)
24165 break;
24167 /* Stop at line ends. */
24168 if (ITERATOR_AT_END_OF_LINE_P (it))
24170 it->continuation_lines_width = 0;
24171 break;
24174 set_iterator_to_next (it, true);
24175 if (STRINGP (it->string))
24176 it_charpos = IT_STRING_CHARPOS (*it);
24177 else
24178 it_charpos = IT_CHARPOS (*it);
24180 /* Stop if truncating at the right edge. */
24181 if (it->line_wrap == TRUNCATE
24182 && it->current_x >= it->last_visible_x)
24184 /* Add truncation mark, but don't do it if the line is
24185 truncated at a padding space. */
24186 if (it_charpos < it->string_nchars)
24188 if (!FRAME_WINDOW_P (it->f))
24190 int ii, n;
24192 if (it->current_x > it->last_visible_x)
24194 if (!row->reversed_p)
24196 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24197 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24198 break;
24200 else
24202 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24203 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24204 break;
24205 unproduce_glyphs (it, ii + 1);
24206 ii = row->used[TEXT_AREA] - (ii + 1);
24208 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24210 row->used[TEXT_AREA] = ii;
24211 produce_special_glyphs (it, IT_TRUNCATION);
24214 produce_special_glyphs (it, IT_TRUNCATION);
24216 row->truncated_on_right_p = true;
24218 break;
24222 /* Maybe insert a truncation at the left. */
24223 if (it->first_visible_x
24224 && it_charpos > 0)
24226 if (!FRAME_WINDOW_P (it->f)
24227 || (row->reversed_p
24228 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24229 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24230 insert_left_trunc_glyphs (it);
24231 row->truncated_on_left_p = true;
24234 it->face_id = saved_face_id;
24236 /* Value is number of columns displayed. */
24237 return it->hpos - hpos_at_start;
24242 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24243 appears as an element of LIST or as the car of an element of LIST.
24244 If PROPVAL is a list, compare each element against LIST in that
24245 way, and return 1/2 if any element of PROPVAL is found in LIST.
24246 Otherwise return 0. This function cannot quit.
24247 The return value is 2 if the text is invisible but with an ellipsis
24248 and 1 if it's invisible and without an ellipsis. */
24251 invisible_prop (Lisp_Object propval, Lisp_Object list)
24253 Lisp_Object tail, proptail;
24255 for (tail = list; CONSP (tail); tail = XCDR (tail))
24257 register Lisp_Object tem;
24258 tem = XCAR (tail);
24259 if (EQ (propval, tem))
24260 return 1;
24261 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24262 return NILP (XCDR (tem)) ? 1 : 2;
24265 if (CONSP (propval))
24267 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24269 Lisp_Object propelt;
24270 propelt = XCAR (proptail);
24271 for (tail = list; CONSP (tail); tail = XCDR (tail))
24273 register Lisp_Object tem;
24274 tem = XCAR (tail);
24275 if (EQ (propelt, tem))
24276 return 1;
24277 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24278 return NILP (XCDR (tem)) ? 1 : 2;
24283 return 0;
24286 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24287 doc: /* Non-nil if the property makes the text invisible.
24288 POS-OR-PROP can be a marker or number, in which case it is taken to be
24289 a position in the current buffer and the value of the `invisible' property
24290 is checked; or it can be some other value, which is then presumed to be the
24291 value of the `invisible' property of the text of interest.
24292 The non-nil value returned can be t for truly invisible text or something
24293 else if the text is replaced by an ellipsis. */)
24294 (Lisp_Object pos_or_prop)
24296 Lisp_Object prop
24297 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24298 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24299 : pos_or_prop);
24300 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24301 return (invis == 0 ? Qnil
24302 : invis == 1 ? Qt
24303 : make_number (invis));
24306 /* Calculate a width or height in pixels from a specification using
24307 the following elements:
24309 SPEC ::=
24310 NUM - a (fractional) multiple of the default font width/height
24311 (NUM) - specifies exactly NUM pixels
24312 UNIT - a fixed number of pixels, see below.
24313 ELEMENT - size of a display element in pixels, see below.
24314 (NUM . SPEC) - equals NUM * SPEC
24315 (+ SPEC SPEC ...) - add pixel values
24316 (- SPEC SPEC ...) - subtract pixel values
24317 (- SPEC) - negate pixel value
24319 NUM ::=
24320 INT or FLOAT - a number constant
24321 SYMBOL - use symbol's (buffer local) variable binding.
24323 UNIT ::=
24324 in - pixels per inch *)
24325 mm - pixels per 1/1000 meter *)
24326 cm - pixels per 1/100 meter *)
24327 width - width of current font in pixels.
24328 height - height of current font in pixels.
24330 *) using the ratio(s) defined in display-pixels-per-inch.
24332 ELEMENT ::=
24334 left-fringe - left fringe width in pixels
24335 right-fringe - right fringe width in pixels
24337 left-margin - left margin width in pixels
24338 right-margin - right margin width in pixels
24340 scroll-bar - scroll-bar area width in pixels
24342 Examples:
24344 Pixels corresponding to 5 inches:
24345 (5 . in)
24347 Total width of non-text areas on left side of window (if scroll-bar is on left):
24348 '(space :width (+ left-fringe left-margin scroll-bar))
24350 Align to first text column (in header line):
24351 '(space :align-to 0)
24353 Align to middle of text area minus half the width of variable `my-image'
24354 containing a loaded image:
24355 '(space :align-to (0.5 . (- text my-image)))
24357 Width of left margin minus width of 1 character in the default font:
24358 '(space :width (- left-margin 1))
24360 Width of left margin minus width of 2 characters in the current font:
24361 '(space :width (- left-margin (2 . width)))
24363 Center 1 character over left-margin (in header line):
24364 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24366 Different ways to express width of left fringe plus left margin minus one pixel:
24367 '(space :width (- (+ left-fringe left-margin) (1)))
24368 '(space :width (+ left-fringe left-margin (- (1))))
24369 '(space :width (+ left-fringe left-margin (-1)))
24373 static bool
24374 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24375 struct font *font, bool width_p, int *align_to)
24377 double pixels;
24379 # define OK_PIXELS(val) (*res = (val), true)
24380 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24382 if (NILP (prop))
24383 return OK_PIXELS (0);
24385 eassert (FRAME_LIVE_P (it->f));
24387 if (SYMBOLP (prop))
24389 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24391 char *unit = SSDATA (SYMBOL_NAME (prop));
24393 if (unit[0] == 'i' && unit[1] == 'n')
24394 pixels = 1.0;
24395 else if (unit[0] == 'm' && unit[1] == 'm')
24396 pixels = 25.4;
24397 else if (unit[0] == 'c' && unit[1] == 'm')
24398 pixels = 2.54;
24399 else
24400 pixels = 0;
24401 if (pixels > 0)
24403 double ppi = (width_p ? FRAME_RES_X (it->f)
24404 : FRAME_RES_Y (it->f));
24406 if (ppi > 0)
24407 return OK_PIXELS (ppi / pixels);
24408 return false;
24412 #ifdef HAVE_WINDOW_SYSTEM
24413 if (EQ (prop, Qheight))
24414 return OK_PIXELS (font
24415 ? normal_char_height (font, -1)
24416 : FRAME_LINE_HEIGHT (it->f));
24417 if (EQ (prop, Qwidth))
24418 return OK_PIXELS (font
24419 ? FONT_WIDTH (font)
24420 : FRAME_COLUMN_WIDTH (it->f));
24421 #else
24422 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24423 return OK_PIXELS (1);
24424 #endif
24426 if (EQ (prop, Qtext))
24427 return OK_PIXELS (width_p
24428 ? window_box_width (it->w, TEXT_AREA)
24429 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24431 if (align_to && *align_to < 0)
24433 *res = 0;
24434 if (EQ (prop, Qleft))
24435 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24436 if (EQ (prop, Qright))
24437 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24438 if (EQ (prop, Qcenter))
24439 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24440 + window_box_width (it->w, TEXT_AREA) / 2);
24441 if (EQ (prop, Qleft_fringe))
24442 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24443 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24444 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24445 if (EQ (prop, Qright_fringe))
24446 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24447 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24448 : window_box_right_offset (it->w, TEXT_AREA));
24449 if (EQ (prop, Qleft_margin))
24450 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24451 if (EQ (prop, Qright_margin))
24452 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24453 if (EQ (prop, Qscroll_bar))
24454 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24456 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24457 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24458 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24459 : 0)));
24461 else
24463 if (EQ (prop, Qleft_fringe))
24464 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24465 if (EQ (prop, Qright_fringe))
24466 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24467 if (EQ (prop, Qleft_margin))
24468 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24469 if (EQ (prop, Qright_margin))
24470 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24471 if (EQ (prop, Qscroll_bar))
24472 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24475 prop = buffer_local_value (prop, it->w->contents);
24476 if (EQ (prop, Qunbound))
24477 prop = Qnil;
24480 if (NUMBERP (prop))
24482 int base_unit = (width_p
24483 ? FRAME_COLUMN_WIDTH (it->f)
24484 : FRAME_LINE_HEIGHT (it->f));
24485 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24488 if (CONSP (prop))
24490 Lisp_Object car = XCAR (prop);
24491 Lisp_Object cdr = XCDR (prop);
24493 if (SYMBOLP (car))
24495 #ifdef HAVE_WINDOW_SYSTEM
24496 if (FRAME_WINDOW_P (it->f)
24497 && valid_image_p (prop))
24499 ptrdiff_t id = lookup_image (it->f, prop);
24500 struct image *img = IMAGE_FROM_ID (it->f, id);
24502 return OK_PIXELS (width_p ? img->width : img->height);
24504 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24506 // TODO: Don't return dummy size.
24507 return OK_PIXELS (100);
24509 #endif
24510 if (EQ (car, Qplus) || EQ (car, Qminus))
24512 bool first = true;
24513 double px;
24515 pixels = 0;
24516 while (CONSP (cdr))
24518 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24519 font, width_p, align_to))
24520 return false;
24521 if (first)
24522 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24523 else
24524 pixels += px;
24525 cdr = XCDR (cdr);
24527 if (EQ (car, Qminus))
24528 pixels = -pixels;
24529 return OK_PIXELS (pixels);
24532 car = buffer_local_value (car, it->w->contents);
24533 if (EQ (car, Qunbound))
24534 car = Qnil;
24537 if (NUMBERP (car))
24539 double fact;
24540 pixels = XFLOATINT (car);
24541 if (NILP (cdr))
24542 return OK_PIXELS (pixels);
24543 if (calc_pixel_width_or_height (&fact, it, cdr,
24544 font, width_p, align_to))
24545 return OK_PIXELS (pixels * fact);
24546 return false;
24549 return false;
24552 return false;
24555 void
24556 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24558 #ifdef HAVE_WINDOW_SYSTEM
24559 normal_char_ascent_descent (font, -1, ascent, descent);
24560 #else
24561 *ascent = 1;
24562 *descent = 0;
24563 #endif
24567 /***********************************************************************
24568 Glyph Display
24569 ***********************************************************************/
24571 #ifdef HAVE_WINDOW_SYSTEM
24573 #ifdef GLYPH_DEBUG
24575 void
24576 dump_glyph_string (struct glyph_string *s)
24578 fprintf (stderr, "glyph string\n");
24579 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24580 s->x, s->y, s->width, s->height);
24581 fprintf (stderr, " ybase = %d\n", s->ybase);
24582 fprintf (stderr, " hl = %d\n", s->hl);
24583 fprintf (stderr, " left overhang = %d, right = %d\n",
24584 s->left_overhang, s->right_overhang);
24585 fprintf (stderr, " nchars = %d\n", s->nchars);
24586 fprintf (stderr, " extends to end of line = %d\n",
24587 s->extends_to_end_of_line_p);
24588 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24589 fprintf (stderr, " bg width = %d\n", s->background_width);
24592 #endif /* GLYPH_DEBUG */
24594 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24595 of XChar2b structures for S; it can't be allocated in
24596 init_glyph_string because it must be allocated via `alloca'. W
24597 is the window on which S is drawn. ROW and AREA are the glyph row
24598 and area within the row from which S is constructed. START is the
24599 index of the first glyph structure covered by S. HL is a
24600 face-override for drawing S. */
24602 #ifdef HAVE_NTGUI
24603 #define OPTIONAL_HDC(hdc) HDC hdc,
24604 #define DECLARE_HDC(hdc) HDC hdc;
24605 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24606 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24607 #endif
24609 #ifndef OPTIONAL_HDC
24610 #define OPTIONAL_HDC(hdc)
24611 #define DECLARE_HDC(hdc)
24612 #define ALLOCATE_HDC(hdc, f)
24613 #define RELEASE_HDC(hdc, f)
24614 #endif
24616 static void
24617 init_glyph_string (struct glyph_string *s,
24618 OPTIONAL_HDC (hdc)
24619 XChar2b *char2b, struct window *w, struct glyph_row *row,
24620 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24622 memset (s, 0, sizeof *s);
24623 s->w = w;
24624 s->f = XFRAME (w->frame);
24625 #ifdef HAVE_NTGUI
24626 s->hdc = hdc;
24627 #endif
24628 s->display = FRAME_X_DISPLAY (s->f);
24629 s->window = FRAME_X_WINDOW (s->f);
24630 s->char2b = char2b;
24631 s->hl = hl;
24632 s->row = row;
24633 s->area = area;
24634 s->first_glyph = row->glyphs[area] + start;
24635 s->height = row->height;
24636 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24637 s->ybase = s->y + row->ascent;
24641 /* Append the list of glyph strings with head H and tail T to the list
24642 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24644 static void
24645 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24646 struct glyph_string *h, struct glyph_string *t)
24648 if (h)
24650 if (*head)
24651 (*tail)->next = h;
24652 else
24653 *head = h;
24654 h->prev = *tail;
24655 *tail = t;
24660 /* Prepend the list of glyph strings with head H and tail T to the
24661 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24662 result. */
24664 static void
24665 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24666 struct glyph_string *h, struct glyph_string *t)
24668 if (h)
24670 if (*head)
24671 (*head)->prev = t;
24672 else
24673 *tail = t;
24674 t->next = *head;
24675 *head = h;
24680 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24681 Set *HEAD and *TAIL to the resulting list. */
24683 static void
24684 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24685 struct glyph_string *s)
24687 s->next = s->prev = NULL;
24688 append_glyph_string_lists (head, tail, s, s);
24692 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24693 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24694 make sure that X resources for the face returned are allocated.
24695 Value is a pointer to a realized face that is ready for display if
24696 DISPLAY_P. */
24698 static struct face *
24699 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24700 XChar2b *char2b, bool display_p)
24702 struct face *face = FACE_FROM_ID (f, face_id);
24703 unsigned code = 0;
24705 if (face->font)
24707 code = face->font->driver->encode_char (face->font, c);
24709 if (code == FONT_INVALID_CODE)
24710 code = 0;
24712 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24714 /* Make sure X resources of the face are allocated. */
24715 #ifdef HAVE_X_WINDOWS
24716 if (display_p)
24717 #endif
24719 eassert (face != NULL);
24720 prepare_face_for_display (f, face);
24723 return face;
24727 /* Get face and two-byte form of character glyph GLYPH on frame F.
24728 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24729 a pointer to a realized face that is ready for display. */
24731 static struct face *
24732 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24733 XChar2b *char2b)
24735 struct face *face;
24736 unsigned code = 0;
24738 eassert (glyph->type == CHAR_GLYPH);
24739 face = FACE_FROM_ID (f, glyph->face_id);
24741 /* Make sure X resources of the face are allocated. */
24742 prepare_face_for_display (f, face);
24744 if (face->font)
24746 if (CHAR_BYTE8_P (glyph->u.ch))
24747 code = CHAR_TO_BYTE8 (glyph->u.ch);
24748 else
24749 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24751 if (code == FONT_INVALID_CODE)
24752 code = 0;
24755 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24756 return face;
24760 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24761 Return true iff FONT has a glyph for C. */
24763 static bool
24764 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24766 unsigned code;
24768 if (CHAR_BYTE8_P (c))
24769 code = CHAR_TO_BYTE8 (c);
24770 else
24771 code = font->driver->encode_char (font, c);
24773 if (code == FONT_INVALID_CODE)
24774 return false;
24775 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24776 return true;
24780 /* Fill glyph string S with composition components specified by S->cmp.
24782 BASE_FACE is the base face of the composition.
24783 S->cmp_from is the index of the first component for S.
24785 OVERLAPS non-zero means S should draw the foreground only, and use
24786 its physical height for clipping. See also draw_glyphs.
24788 Value is the index of a component not in S. */
24790 static int
24791 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24792 int overlaps)
24794 int i;
24795 /* For all glyphs of this composition, starting at the offset
24796 S->cmp_from, until we reach the end of the definition or encounter a
24797 glyph that requires the different face, add it to S. */
24798 struct face *face;
24800 eassert (s);
24802 s->for_overlaps = overlaps;
24803 s->face = NULL;
24804 s->font = NULL;
24805 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24807 int c = COMPOSITION_GLYPH (s->cmp, i);
24809 /* TAB in a composition means display glyphs with padding space
24810 on the left or right. */
24811 if (c != '\t')
24813 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24814 -1, Qnil);
24816 face = get_char_face_and_encoding (s->f, c, face_id,
24817 s->char2b + i, true);
24818 if (face)
24820 if (! s->face)
24822 s->face = face;
24823 s->font = s->face->font;
24825 else if (s->face != face)
24826 break;
24829 ++s->nchars;
24831 s->cmp_to = i;
24833 if (s->face == NULL)
24835 s->face = base_face->ascii_face;
24836 s->font = s->face->font;
24839 /* All glyph strings for the same composition has the same width,
24840 i.e. the width set for the first component of the composition. */
24841 s->width = s->first_glyph->pixel_width;
24843 /* If the specified font could not be loaded, use the frame's
24844 default font, but record the fact that we couldn't load it in
24845 the glyph string so that we can draw rectangles for the
24846 characters of the glyph string. */
24847 if (s->font == NULL)
24849 s->font_not_found_p = true;
24850 s->font = FRAME_FONT (s->f);
24853 /* Adjust base line for subscript/superscript text. */
24854 s->ybase += s->first_glyph->voffset;
24856 return s->cmp_to;
24859 static int
24860 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24861 int start, int end, int overlaps)
24863 struct glyph *glyph, *last;
24864 Lisp_Object lgstring;
24865 int i;
24867 s->for_overlaps = overlaps;
24868 glyph = s->row->glyphs[s->area] + start;
24869 last = s->row->glyphs[s->area] + end;
24870 s->cmp_id = glyph->u.cmp.id;
24871 s->cmp_from = glyph->slice.cmp.from;
24872 s->cmp_to = glyph->slice.cmp.to + 1;
24873 s->face = FACE_FROM_ID (s->f, face_id);
24874 lgstring = composition_gstring_from_id (s->cmp_id);
24875 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24876 glyph++;
24877 while (glyph < last
24878 && glyph->u.cmp.automatic
24879 && glyph->u.cmp.id == s->cmp_id
24880 && s->cmp_to == glyph->slice.cmp.from)
24881 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24883 for (i = s->cmp_from; i < s->cmp_to; i++)
24885 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24886 unsigned code = LGLYPH_CODE (lglyph);
24888 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24890 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24891 return glyph - s->row->glyphs[s->area];
24895 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24896 See the comment of fill_glyph_string for arguments.
24897 Value is the index of the first glyph not in S. */
24900 static int
24901 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24902 int start, int end, int overlaps)
24904 struct glyph *glyph, *last;
24905 int voffset;
24907 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24908 s->for_overlaps = overlaps;
24909 glyph = s->row->glyphs[s->area] + start;
24910 last = s->row->glyphs[s->area] + end;
24911 voffset = glyph->voffset;
24912 s->face = FACE_FROM_ID (s->f, face_id);
24913 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24914 s->nchars = 1;
24915 s->width = glyph->pixel_width;
24916 glyph++;
24917 while (glyph < last
24918 && glyph->type == GLYPHLESS_GLYPH
24919 && glyph->voffset == voffset
24920 && glyph->face_id == face_id)
24922 s->nchars++;
24923 s->width += glyph->pixel_width;
24924 glyph++;
24926 s->ybase += voffset;
24927 return glyph - s->row->glyphs[s->area];
24931 /* Fill glyph string S from a sequence of character glyphs.
24933 FACE_ID is the face id of the string. START is the index of the
24934 first glyph to consider, END is the index of the last + 1.
24935 OVERLAPS non-zero means S should draw the foreground only, and use
24936 its physical height for clipping. See also draw_glyphs.
24938 Value is the index of the first glyph not in S. */
24940 static int
24941 fill_glyph_string (struct glyph_string *s, int face_id,
24942 int start, int end, int overlaps)
24944 struct glyph *glyph, *last;
24945 int voffset;
24946 bool glyph_not_available_p;
24948 eassert (s->f == XFRAME (s->w->frame));
24949 eassert (s->nchars == 0);
24950 eassert (start >= 0 && end > start);
24952 s->for_overlaps = overlaps;
24953 glyph = s->row->glyphs[s->area] + start;
24954 last = s->row->glyphs[s->area] + end;
24955 voffset = glyph->voffset;
24956 s->padding_p = glyph->padding_p;
24957 glyph_not_available_p = glyph->glyph_not_available_p;
24959 while (glyph < last
24960 && glyph->type == CHAR_GLYPH
24961 && glyph->voffset == voffset
24962 /* Same face id implies same font, nowadays. */
24963 && glyph->face_id == face_id
24964 && glyph->glyph_not_available_p == glyph_not_available_p)
24966 s->face = get_glyph_face_and_encoding (s->f, glyph,
24967 s->char2b + s->nchars);
24968 ++s->nchars;
24969 eassert (s->nchars <= end - start);
24970 s->width += glyph->pixel_width;
24971 if (glyph++->padding_p != s->padding_p)
24972 break;
24975 s->font = s->face->font;
24977 /* If the specified font could not be loaded, use the frame's font,
24978 but record the fact that we couldn't load it in
24979 S->font_not_found_p so that we can draw rectangles for the
24980 characters of the glyph string. */
24981 if (s->font == NULL || glyph_not_available_p)
24983 s->font_not_found_p = true;
24984 s->font = FRAME_FONT (s->f);
24987 /* Adjust base line for subscript/superscript text. */
24988 s->ybase += voffset;
24990 eassert (s->face && s->face->gc);
24991 return glyph - s->row->glyphs[s->area];
24995 /* Fill glyph string S from image glyph S->first_glyph. */
24997 static void
24998 fill_image_glyph_string (struct glyph_string *s)
25000 eassert (s->first_glyph->type == IMAGE_GLYPH);
25001 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25002 eassert (s->img);
25003 s->slice = s->first_glyph->slice.img;
25004 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25005 s->font = s->face->font;
25006 s->width = s->first_glyph->pixel_width;
25008 /* Adjust base line for subscript/superscript text. */
25009 s->ybase += s->first_glyph->voffset;
25013 #ifdef HAVE_XWIDGETS
25014 static void
25015 fill_xwidget_glyph_string (struct glyph_string *s)
25017 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25018 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25019 s->font = s->face->font;
25020 s->width = s->first_glyph->pixel_width;
25021 s->ybase += s->first_glyph->voffset;
25022 s->xwidget = s->first_glyph->u.xwidget;
25024 #endif
25025 /* Fill glyph string S from a sequence of stretch glyphs.
25027 START is the index of the first glyph to consider,
25028 END is the index of the last + 1.
25030 Value is the index of the first glyph not in S. */
25032 static int
25033 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25035 struct glyph *glyph, *last;
25036 int voffset, face_id;
25038 eassert (s->first_glyph->type == STRETCH_GLYPH);
25040 glyph = s->row->glyphs[s->area] + start;
25041 last = s->row->glyphs[s->area] + end;
25042 face_id = glyph->face_id;
25043 s->face = FACE_FROM_ID (s->f, face_id);
25044 s->font = s->face->font;
25045 s->width = glyph->pixel_width;
25046 s->nchars = 1;
25047 voffset = glyph->voffset;
25049 for (++glyph;
25050 (glyph < last
25051 && glyph->type == STRETCH_GLYPH
25052 && glyph->voffset == voffset
25053 && glyph->face_id == face_id);
25054 ++glyph)
25055 s->width += glyph->pixel_width;
25057 /* Adjust base line for subscript/superscript text. */
25058 s->ybase += voffset;
25060 /* The case that face->gc == 0 is handled when drawing the glyph
25061 string by calling prepare_face_for_display. */
25062 eassert (s->face);
25063 return glyph - s->row->glyphs[s->area];
25066 static struct font_metrics *
25067 get_per_char_metric (struct font *font, XChar2b *char2b)
25069 static struct font_metrics metrics;
25070 unsigned code;
25072 if (! font)
25073 return NULL;
25074 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25075 if (code == FONT_INVALID_CODE)
25076 return NULL;
25077 font->driver->text_extents (font, &code, 1, &metrics);
25078 return &metrics;
25081 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25082 for FONT. Values are taken from font-global ones, except for fonts
25083 that claim preposterously large values, but whose glyphs actually
25084 have reasonable dimensions. C is the character to use for metrics
25085 if the font-global values are too large; if C is negative, the
25086 function selects a default character. */
25087 static void
25088 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25090 *ascent = FONT_BASE (font);
25091 *descent = FONT_DESCENT (font);
25093 if (FONT_TOO_HIGH (font))
25095 XChar2b char2b;
25097 /* Get metrics of C, defaulting to a reasonably sized ASCII
25098 character. */
25099 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25101 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25103 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25105 /* We add 1 pixel to character dimensions as heuristics
25106 that produces nicer display, e.g. when the face has
25107 the box attribute. */
25108 *ascent = pcm->ascent + 1;
25109 *descent = pcm->descent + 1;
25115 /* A subroutine that computes a reasonable "normal character height"
25116 for fonts that claim preposterously large vertical dimensions, but
25117 whose glyphs are actually reasonably sized. C is the character
25118 whose metrics to use for those fonts, or -1 for default
25119 character. */
25120 static int
25121 normal_char_height (struct font *font, int c)
25123 int ascent, descent;
25125 normal_char_ascent_descent (font, c, &ascent, &descent);
25127 return ascent + descent;
25130 /* EXPORT for RIF:
25131 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25132 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25133 assumed to be zero. */
25135 void
25136 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25138 *left = *right = 0;
25140 if (glyph->type == CHAR_GLYPH)
25142 XChar2b char2b;
25143 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25144 if (face->font)
25146 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25147 if (pcm)
25149 if (pcm->rbearing > pcm->width)
25150 *right = pcm->rbearing - pcm->width;
25151 if (pcm->lbearing < 0)
25152 *left = -pcm->lbearing;
25156 else if (glyph->type == COMPOSITE_GLYPH)
25158 if (! glyph->u.cmp.automatic)
25160 struct composition *cmp = composition_table[glyph->u.cmp.id];
25162 if (cmp->rbearing > cmp->pixel_width)
25163 *right = cmp->rbearing - cmp->pixel_width;
25164 if (cmp->lbearing < 0)
25165 *left = - cmp->lbearing;
25167 else
25169 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25170 struct font_metrics metrics;
25172 composition_gstring_width (gstring, glyph->slice.cmp.from,
25173 glyph->slice.cmp.to + 1, &metrics);
25174 if (metrics.rbearing > metrics.width)
25175 *right = metrics.rbearing - metrics.width;
25176 if (metrics.lbearing < 0)
25177 *left = - metrics.lbearing;
25183 /* Return the index of the first glyph preceding glyph string S that
25184 is overwritten by S because of S's left overhang. Value is -1
25185 if no glyphs are overwritten. */
25187 static int
25188 left_overwritten (struct glyph_string *s)
25190 int k;
25192 if (s->left_overhang)
25194 int x = 0, i;
25195 struct glyph *glyphs = s->row->glyphs[s->area];
25196 int first = s->first_glyph - glyphs;
25198 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25199 x -= glyphs[i].pixel_width;
25201 k = i + 1;
25203 else
25204 k = -1;
25206 return k;
25210 /* Return the index of the first glyph preceding glyph string S that
25211 is overwriting S because of its right overhang. Value is -1 if no
25212 glyph in front of S overwrites S. */
25214 static int
25215 left_overwriting (struct glyph_string *s)
25217 int i, k, x;
25218 struct glyph *glyphs = s->row->glyphs[s->area];
25219 int first = s->first_glyph - glyphs;
25221 k = -1;
25222 x = 0;
25223 for (i = first - 1; i >= 0; --i)
25225 int left, right;
25226 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25227 if (x + right > 0)
25228 k = i;
25229 x -= glyphs[i].pixel_width;
25232 return k;
25236 /* Return the index of the last glyph following glyph string S that is
25237 overwritten by S because of S's right overhang. Value is -1 if
25238 no such glyph is found. */
25240 static int
25241 right_overwritten (struct glyph_string *s)
25243 int k = -1;
25245 if (s->right_overhang)
25247 int x = 0, i;
25248 struct glyph *glyphs = s->row->glyphs[s->area];
25249 int first = (s->first_glyph - glyphs
25250 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25251 int end = s->row->used[s->area];
25253 for (i = first; i < end && s->right_overhang > x; ++i)
25254 x += glyphs[i].pixel_width;
25256 k = i;
25259 return k;
25263 /* Return the index of the last glyph following glyph string S that
25264 overwrites S because of its left overhang. Value is negative
25265 if no such glyph is found. */
25267 static int
25268 right_overwriting (struct glyph_string *s)
25270 int i, k, x;
25271 int end = s->row->used[s->area];
25272 struct glyph *glyphs = s->row->glyphs[s->area];
25273 int first = (s->first_glyph - glyphs
25274 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25276 k = -1;
25277 x = 0;
25278 for (i = first; i < end; ++i)
25280 int left, right;
25281 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25282 if (x - left < 0)
25283 k = i;
25284 x += glyphs[i].pixel_width;
25287 return k;
25291 /* Set background width of glyph string S. START is the index of the
25292 first glyph following S. LAST_X is the right-most x-position + 1
25293 in the drawing area. */
25295 static void
25296 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25298 /* If the face of this glyph string has to be drawn to the end of
25299 the drawing area, set S->extends_to_end_of_line_p. */
25301 if (start == s->row->used[s->area]
25302 && ((s->row->fill_line_p
25303 && (s->hl == DRAW_NORMAL_TEXT
25304 || s->hl == DRAW_IMAGE_RAISED
25305 || s->hl == DRAW_IMAGE_SUNKEN))
25306 || s->hl == DRAW_MOUSE_FACE))
25307 s->extends_to_end_of_line_p = true;
25309 /* If S extends its face to the end of the line, set its
25310 background_width to the distance to the right edge of the drawing
25311 area. */
25312 if (s->extends_to_end_of_line_p)
25313 s->background_width = last_x - s->x + 1;
25314 else
25315 s->background_width = s->width;
25319 /* Compute overhangs and x-positions for glyph string S and its
25320 predecessors, or successors. X is the starting x-position for S.
25321 BACKWARD_P means process predecessors. */
25323 static void
25324 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25326 if (backward_p)
25328 while (s)
25330 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25331 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25332 x -= s->width;
25333 s->x = x;
25334 s = s->prev;
25337 else
25339 while (s)
25341 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25342 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25343 s->x = x;
25344 x += s->width;
25345 s = s->next;
25352 /* The following macros are only called from draw_glyphs below.
25353 They reference the following parameters of that function directly:
25354 `w', `row', `area', and `overlap_p'
25355 as well as the following local variables:
25356 `s', `f', and `hdc' (in W32) */
25358 #ifdef HAVE_NTGUI
25359 /* On W32, silently add local `hdc' variable to argument list of
25360 init_glyph_string. */
25361 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25362 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25363 #else
25364 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25365 init_glyph_string (s, char2b, w, row, area, start, hl)
25366 #endif
25368 /* Add a glyph string for a stretch glyph to the list of strings
25369 between HEAD and TAIL. START is the index of the stretch glyph in
25370 row area AREA of glyph row ROW. END is the index of the last glyph
25371 in that glyph row area. X is the current output position assigned
25372 to the new glyph string constructed. HL overrides that face of the
25373 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25374 is the right-most x-position of the drawing area. */
25376 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25377 and below -- keep them on one line. */
25378 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25379 do \
25381 s = alloca (sizeof *s); \
25382 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25383 START = fill_stretch_glyph_string (s, START, END); \
25384 append_glyph_string (&HEAD, &TAIL, s); \
25385 s->x = (X); \
25387 while (false)
25390 /* Add a glyph string for an image glyph to the list of strings
25391 between HEAD and TAIL. START is the index of the image glyph in
25392 row area AREA of glyph row ROW. END is the index of the last glyph
25393 in that glyph row area. X is the current output position assigned
25394 to the new glyph string constructed. HL overrides that face of the
25395 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25396 is the right-most x-position of the drawing area. */
25398 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25399 do \
25401 s = alloca (sizeof *s); \
25402 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25403 fill_image_glyph_string (s); \
25404 append_glyph_string (&HEAD, &TAIL, s); \
25405 ++START; \
25406 s->x = (X); \
25408 while (false)
25410 #ifndef HAVE_XWIDGETS
25411 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25412 eassume (false)
25413 #else
25414 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25415 do \
25417 s = alloca (sizeof *s); \
25418 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25419 fill_xwidget_glyph_string (s); \
25420 append_glyph_string (&(HEAD), &(TAIL), s); \
25421 ++(START); \
25422 s->x = (X); \
25424 while (false)
25425 #endif
25427 /* Add a glyph string for a sequence of character glyphs to the list
25428 of strings between HEAD and TAIL. START is the index of the first
25429 glyph in row area AREA of glyph row ROW that is part of the new
25430 glyph string. END is the index of the last glyph in that glyph row
25431 area. X is the current output position assigned to the new glyph
25432 string constructed. HL overrides that face of the glyph; e.g. it
25433 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25434 right-most x-position of the drawing area. */
25436 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25437 do \
25439 int face_id; \
25440 XChar2b *char2b; \
25442 face_id = (row)->glyphs[area][START].face_id; \
25444 s = alloca (sizeof *s); \
25445 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25446 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25447 append_glyph_string (&HEAD, &TAIL, s); \
25448 s->x = (X); \
25449 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25451 while (false)
25454 /* Add a glyph string for a composite sequence to the list of strings
25455 between HEAD and TAIL. START is the index of the first glyph in
25456 row area AREA of glyph row ROW that is part of the new glyph
25457 string. END is the index of the last glyph in that glyph row area.
25458 X is the current output position assigned to the new glyph string
25459 constructed. HL overrides that face of the glyph; e.g. it is
25460 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25461 x-position of the drawing area. */
25463 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25464 do { \
25465 int face_id = (row)->glyphs[area][START].face_id; \
25466 struct face *base_face = FACE_FROM_ID (f, face_id); \
25467 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25468 struct composition *cmp = composition_table[cmp_id]; \
25469 XChar2b *char2b; \
25470 struct glyph_string *first_s = NULL; \
25471 int n; \
25473 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25475 /* Make glyph_strings for each glyph sequence that is drawable by \
25476 the same face, and append them to HEAD/TAIL. */ \
25477 for (n = 0; n < cmp->glyph_len;) \
25479 s = alloca (sizeof *s); \
25480 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25481 append_glyph_string (&(HEAD), &(TAIL), s); \
25482 s->cmp = cmp; \
25483 s->cmp_from = n; \
25484 s->x = (X); \
25485 if (n == 0) \
25486 first_s = s; \
25487 n = fill_composite_glyph_string (s, base_face, overlaps); \
25490 ++START; \
25491 s = first_s; \
25492 } while (false)
25495 /* Add a glyph string for a glyph-string sequence to the list of strings
25496 between HEAD and TAIL. */
25498 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25499 do { \
25500 int face_id; \
25501 XChar2b *char2b; \
25502 Lisp_Object gstring; \
25504 face_id = (row)->glyphs[area][START].face_id; \
25505 gstring = (composition_gstring_from_id \
25506 ((row)->glyphs[area][START].u.cmp.id)); \
25507 s = alloca (sizeof *s); \
25508 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25509 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25510 append_glyph_string (&(HEAD), &(TAIL), s); \
25511 s->x = (X); \
25512 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25513 } while (false)
25516 /* Add a glyph string for a sequence of glyphless character's glyphs
25517 to the list of strings between HEAD and TAIL. The meanings of
25518 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25520 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25521 do \
25523 int face_id; \
25525 face_id = (row)->glyphs[area][START].face_id; \
25527 s = alloca (sizeof *s); \
25528 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25529 append_glyph_string (&HEAD, &TAIL, s); \
25530 s->x = (X); \
25531 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25532 overlaps); \
25534 while (false)
25537 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25538 of AREA of glyph row ROW on window W between indices START and END.
25539 HL overrides the face for drawing glyph strings, e.g. it is
25540 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25541 x-positions of the drawing area.
25543 This is an ugly monster macro construct because we must use alloca
25544 to allocate glyph strings (because draw_glyphs can be called
25545 asynchronously). */
25547 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25548 do \
25550 HEAD = TAIL = NULL; \
25551 while (START < END) \
25553 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25554 switch (first_glyph->type) \
25556 case CHAR_GLYPH: \
25557 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25558 HL, X, LAST_X); \
25559 break; \
25561 case COMPOSITE_GLYPH: \
25562 if (first_glyph->u.cmp.automatic) \
25563 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25564 HL, X, LAST_X); \
25565 else \
25566 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25567 HL, X, LAST_X); \
25568 break; \
25570 case STRETCH_GLYPH: \
25571 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25572 HL, X, LAST_X); \
25573 break; \
25575 case IMAGE_GLYPH: \
25576 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25577 HL, X, LAST_X); \
25578 break;
25580 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25581 case XWIDGET_GLYPH: \
25582 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25583 HL, X, LAST_X); \
25584 break;
25586 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25587 case GLYPHLESS_GLYPH: \
25588 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25589 HL, X, LAST_X); \
25590 break; \
25592 default: \
25593 emacs_abort (); \
25596 if (s) \
25598 set_glyph_string_background_width (s, START, LAST_X); \
25599 (X) += s->width; \
25602 } while (false)
25605 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25606 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25607 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25608 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25611 /* Draw glyphs between START and END in AREA of ROW on window W,
25612 starting at x-position X. X is relative to AREA in W. HL is a
25613 face-override with the following meaning:
25615 DRAW_NORMAL_TEXT draw normally
25616 DRAW_CURSOR draw in cursor face
25617 DRAW_MOUSE_FACE draw in mouse face.
25618 DRAW_INVERSE_VIDEO draw in mode line face
25619 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25620 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25622 If OVERLAPS is non-zero, draw only the foreground of characters and
25623 clip to the physical height of ROW. Non-zero value also defines
25624 the overlapping part to be drawn:
25626 OVERLAPS_PRED overlap with preceding rows
25627 OVERLAPS_SUCC overlap with succeeding rows
25628 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25629 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25631 Value is the x-position reached, relative to AREA of W. */
25633 static int
25634 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25635 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25636 enum draw_glyphs_face hl, int overlaps)
25638 struct glyph_string *head, *tail;
25639 struct glyph_string *s;
25640 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25641 int i, j, x_reached, last_x, area_left = 0;
25642 struct frame *f = XFRAME (WINDOW_FRAME (w));
25643 DECLARE_HDC (hdc);
25645 ALLOCATE_HDC (hdc, f);
25647 /* Let's rather be paranoid than getting a SEGV. */
25648 end = min (end, row->used[area]);
25649 start = clip_to_bounds (0, start, end);
25651 /* Translate X to frame coordinates. Set last_x to the right
25652 end of the drawing area. */
25653 if (row->full_width_p)
25655 /* X is relative to the left edge of W, without scroll bars
25656 or fringes. */
25657 area_left = WINDOW_LEFT_EDGE_X (w);
25658 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25659 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25661 else
25663 area_left = window_box_left (w, area);
25664 last_x = area_left + window_box_width (w, area);
25666 x += area_left;
25668 /* Build a doubly-linked list of glyph_string structures between
25669 head and tail from what we have to draw. Note that the macro
25670 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25671 the reason we use a separate variable `i'. */
25672 i = start;
25673 USE_SAFE_ALLOCA;
25674 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25675 if (tail)
25676 x_reached = tail->x + tail->background_width;
25677 else
25678 x_reached = x;
25680 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25681 the row, redraw some glyphs in front or following the glyph
25682 strings built above. */
25683 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25685 struct glyph_string *h, *t;
25686 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25687 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25688 bool check_mouse_face = false;
25689 int dummy_x = 0;
25691 /* If mouse highlighting is on, we may need to draw adjacent
25692 glyphs using mouse-face highlighting. */
25693 if (area == TEXT_AREA && row->mouse_face_p
25694 && hlinfo->mouse_face_beg_row >= 0
25695 && hlinfo->mouse_face_end_row >= 0)
25697 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25699 if (row_vpos >= hlinfo->mouse_face_beg_row
25700 && row_vpos <= hlinfo->mouse_face_end_row)
25702 check_mouse_face = true;
25703 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25704 ? hlinfo->mouse_face_beg_col : 0;
25705 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25706 ? hlinfo->mouse_face_end_col
25707 : row->used[TEXT_AREA];
25711 /* Compute overhangs for all glyph strings. */
25712 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25713 for (s = head; s; s = s->next)
25714 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25716 /* Prepend glyph strings for glyphs in front of the first glyph
25717 string that are overwritten because of the first glyph
25718 string's left overhang. The background of all strings
25719 prepended must be drawn because the first glyph string
25720 draws over it. */
25721 i = left_overwritten (head);
25722 if (i >= 0)
25724 enum draw_glyphs_face overlap_hl;
25726 /* If this row contains mouse highlighting, attempt to draw
25727 the overlapped glyphs with the correct highlight. This
25728 code fails if the overlap encompasses more than one glyph
25729 and mouse-highlight spans only some of these glyphs.
25730 However, making it work perfectly involves a lot more
25731 code, and I don't know if the pathological case occurs in
25732 practice, so we'll stick to this for now. --- cyd */
25733 if (check_mouse_face
25734 && mouse_beg_col < start && mouse_end_col > i)
25735 overlap_hl = DRAW_MOUSE_FACE;
25736 else
25737 overlap_hl = DRAW_NORMAL_TEXT;
25739 if (hl != overlap_hl)
25740 clip_head = head;
25741 j = i;
25742 BUILD_GLYPH_STRINGS (j, start, h, t,
25743 overlap_hl, dummy_x, last_x);
25744 start = i;
25745 compute_overhangs_and_x (t, head->x, true);
25746 prepend_glyph_string_lists (&head, &tail, h, t);
25747 if (clip_head == NULL)
25748 clip_head = head;
25751 /* Prepend glyph strings for glyphs in front of the first glyph
25752 string that overwrite that glyph string because of their
25753 right overhang. For these strings, only the foreground must
25754 be drawn, because it draws over the glyph string at `head'.
25755 The background must not be drawn because this would overwrite
25756 right overhangs of preceding glyphs for which no glyph
25757 strings exist. */
25758 i = left_overwriting (head);
25759 if (i >= 0)
25761 enum draw_glyphs_face overlap_hl;
25763 if (check_mouse_face
25764 && mouse_beg_col < start && mouse_end_col > i)
25765 overlap_hl = DRAW_MOUSE_FACE;
25766 else
25767 overlap_hl = DRAW_NORMAL_TEXT;
25769 if (hl == overlap_hl || clip_head == NULL)
25770 clip_head = head;
25771 BUILD_GLYPH_STRINGS (i, start, h, t,
25772 overlap_hl, dummy_x, last_x);
25773 for (s = h; s; s = s->next)
25774 s->background_filled_p = true;
25775 compute_overhangs_and_x (t, head->x, true);
25776 prepend_glyph_string_lists (&head, &tail, h, t);
25779 /* Append glyphs strings for glyphs following the last glyph
25780 string tail that are overwritten by tail. The background of
25781 these strings has to be drawn because tail's foreground draws
25782 over it. */
25783 i = right_overwritten (tail);
25784 if (i >= 0)
25786 enum draw_glyphs_face overlap_hl;
25788 if (check_mouse_face
25789 && mouse_beg_col < i && mouse_end_col > end)
25790 overlap_hl = DRAW_MOUSE_FACE;
25791 else
25792 overlap_hl = DRAW_NORMAL_TEXT;
25794 if (hl != overlap_hl)
25795 clip_tail = tail;
25796 BUILD_GLYPH_STRINGS (end, i, h, t,
25797 overlap_hl, x, last_x);
25798 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25799 we don't have `end = i;' here. */
25800 compute_overhangs_and_x (h, tail->x + tail->width, false);
25801 append_glyph_string_lists (&head, &tail, h, t);
25802 if (clip_tail == NULL)
25803 clip_tail = tail;
25806 /* Append glyph strings for glyphs following the last glyph
25807 string tail that overwrite tail. The foreground of such
25808 glyphs has to be drawn because it writes into the background
25809 of tail. The background must not be drawn because it could
25810 paint over the foreground of following glyphs. */
25811 i = right_overwriting (tail);
25812 if (i >= 0)
25814 enum draw_glyphs_face overlap_hl;
25815 if (check_mouse_face
25816 && mouse_beg_col < i && mouse_end_col > end)
25817 overlap_hl = DRAW_MOUSE_FACE;
25818 else
25819 overlap_hl = DRAW_NORMAL_TEXT;
25821 if (hl == overlap_hl || clip_tail == NULL)
25822 clip_tail = tail;
25823 i++; /* We must include the Ith glyph. */
25824 BUILD_GLYPH_STRINGS (end, i, h, t,
25825 overlap_hl, x, last_x);
25826 for (s = h; s; s = s->next)
25827 s->background_filled_p = true;
25828 compute_overhangs_and_x (h, tail->x + tail->width, false);
25829 append_glyph_string_lists (&head, &tail, h, t);
25831 if (clip_head || clip_tail)
25832 for (s = head; s; s = s->next)
25834 s->clip_head = clip_head;
25835 s->clip_tail = clip_tail;
25839 /* Draw all strings. */
25840 for (s = head; s; s = s->next)
25841 FRAME_RIF (f)->draw_glyph_string (s);
25843 #ifndef HAVE_NS
25844 /* When focus a sole frame and move horizontally, this clears on_p
25845 causing a failure to erase prev cursor position. */
25846 if (area == TEXT_AREA
25847 && !row->full_width_p
25848 /* When drawing overlapping rows, only the glyph strings'
25849 foreground is drawn, which doesn't erase a cursor
25850 completely. */
25851 && !overlaps)
25853 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25854 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25855 : (tail ? tail->x + tail->background_width : x));
25856 x0 -= area_left;
25857 x1 -= area_left;
25859 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25860 row->y, MATRIX_ROW_BOTTOM_Y (row));
25862 #endif
25864 /* Value is the x-position up to which drawn, relative to AREA of W.
25865 This doesn't include parts drawn because of overhangs. */
25866 if (row->full_width_p)
25867 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25868 else
25869 x_reached -= area_left;
25871 RELEASE_HDC (hdc, f);
25873 SAFE_FREE ();
25874 return x_reached;
25877 /* Expand row matrix if too narrow. Don't expand if area
25878 is not present. */
25880 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25882 if (!it->f->fonts_changed \
25883 && (it->glyph_row->glyphs[area] \
25884 < it->glyph_row->glyphs[area + 1])) \
25886 it->w->ncols_scale_factor++; \
25887 it->f->fonts_changed = true; \
25891 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25892 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25894 static void
25895 append_glyph (struct it *it)
25897 struct glyph *glyph;
25898 enum glyph_row_area area = it->area;
25900 eassert (it->glyph_row);
25901 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25903 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25904 if (glyph < it->glyph_row->glyphs[area + 1])
25906 /* If the glyph row is reversed, we need to prepend the glyph
25907 rather than append it. */
25908 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25910 struct glyph *g;
25912 /* Make room for the additional glyph. */
25913 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25914 g[1] = *g;
25915 glyph = it->glyph_row->glyphs[area];
25917 glyph->charpos = CHARPOS (it->position);
25918 glyph->object = it->object;
25919 if (it->pixel_width > 0)
25921 eassert (it->pixel_width <= SHRT_MAX);
25922 glyph->pixel_width = it->pixel_width;
25923 glyph->padding_p = false;
25925 else
25927 /* Assure at least 1-pixel width. Otherwise, cursor can't
25928 be displayed correctly. */
25929 glyph->pixel_width = 1;
25930 glyph->padding_p = true;
25932 glyph->ascent = it->ascent;
25933 glyph->descent = it->descent;
25934 glyph->voffset = it->voffset;
25935 glyph->type = CHAR_GLYPH;
25936 glyph->avoid_cursor_p = it->avoid_cursor_p;
25937 glyph->multibyte_p = it->multibyte_p;
25938 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25940 /* In R2L rows, the left and the right box edges need to be
25941 drawn in reverse direction. */
25942 glyph->right_box_line_p = it->start_of_box_run_p;
25943 glyph->left_box_line_p = it->end_of_box_run_p;
25945 else
25947 glyph->left_box_line_p = it->start_of_box_run_p;
25948 glyph->right_box_line_p = it->end_of_box_run_p;
25950 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25951 || it->phys_descent > it->descent);
25952 glyph->glyph_not_available_p = it->glyph_not_available_p;
25953 glyph->face_id = it->face_id;
25954 glyph->u.ch = it->char_to_display;
25955 glyph->slice.img = null_glyph_slice;
25956 glyph->font_type = FONT_TYPE_UNKNOWN;
25957 if (it->bidi_p)
25959 glyph->resolved_level = it->bidi_it.resolved_level;
25960 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25961 glyph->bidi_type = it->bidi_it.type;
25963 else
25965 glyph->resolved_level = 0;
25966 glyph->bidi_type = UNKNOWN_BT;
25968 ++it->glyph_row->used[area];
25970 else
25971 IT_EXPAND_MATRIX_WIDTH (it, area);
25974 /* Store one glyph for the composition IT->cmp_it.id in
25975 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25976 non-null. */
25978 static void
25979 append_composite_glyph (struct it *it)
25981 struct glyph *glyph;
25982 enum glyph_row_area area = it->area;
25984 eassert (it->glyph_row);
25986 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25987 if (glyph < it->glyph_row->glyphs[area + 1])
25989 /* If the glyph row is reversed, we need to prepend the glyph
25990 rather than append it. */
25991 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25993 struct glyph *g;
25995 /* Make room for the new glyph. */
25996 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25997 g[1] = *g;
25998 glyph = it->glyph_row->glyphs[it->area];
26000 glyph->charpos = it->cmp_it.charpos;
26001 glyph->object = it->object;
26002 eassert (it->pixel_width <= SHRT_MAX);
26003 glyph->pixel_width = it->pixel_width;
26004 glyph->ascent = it->ascent;
26005 glyph->descent = it->descent;
26006 glyph->voffset = it->voffset;
26007 glyph->type = COMPOSITE_GLYPH;
26008 if (it->cmp_it.ch < 0)
26010 glyph->u.cmp.automatic = false;
26011 glyph->u.cmp.id = it->cmp_it.id;
26012 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26014 else
26016 glyph->u.cmp.automatic = true;
26017 glyph->u.cmp.id = it->cmp_it.id;
26018 glyph->slice.cmp.from = it->cmp_it.from;
26019 glyph->slice.cmp.to = it->cmp_it.to - 1;
26021 glyph->avoid_cursor_p = it->avoid_cursor_p;
26022 glyph->multibyte_p = it->multibyte_p;
26023 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26025 /* In R2L rows, the left and the right box edges need to be
26026 drawn in reverse direction. */
26027 glyph->right_box_line_p = it->start_of_box_run_p;
26028 glyph->left_box_line_p = it->end_of_box_run_p;
26030 else
26032 glyph->left_box_line_p = it->start_of_box_run_p;
26033 glyph->right_box_line_p = it->end_of_box_run_p;
26035 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26036 || it->phys_descent > it->descent);
26037 glyph->padding_p = false;
26038 glyph->glyph_not_available_p = false;
26039 glyph->face_id = it->face_id;
26040 glyph->font_type = FONT_TYPE_UNKNOWN;
26041 if (it->bidi_p)
26043 glyph->resolved_level = it->bidi_it.resolved_level;
26044 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26045 glyph->bidi_type = it->bidi_it.type;
26047 ++it->glyph_row->used[area];
26049 else
26050 IT_EXPAND_MATRIX_WIDTH (it, area);
26054 /* Change IT->ascent and IT->height according to the setting of
26055 IT->voffset. */
26057 static void
26058 take_vertical_position_into_account (struct it *it)
26060 if (it->voffset)
26062 if (it->voffset < 0)
26063 /* Increase the ascent so that we can display the text higher
26064 in the line. */
26065 it->ascent -= it->voffset;
26066 else
26067 /* Increase the descent so that we can display the text lower
26068 in the line. */
26069 it->descent += it->voffset;
26074 /* Produce glyphs/get display metrics for the image IT is loaded with.
26075 See the description of struct display_iterator in dispextern.h for
26076 an overview of struct display_iterator. */
26078 static void
26079 produce_image_glyph (struct it *it)
26081 struct image *img;
26082 struct face *face;
26083 int glyph_ascent, crop;
26084 struct glyph_slice slice;
26086 eassert (it->what == IT_IMAGE);
26088 face = FACE_FROM_ID (it->f, it->face_id);
26089 /* Make sure X resources of the face is loaded. */
26090 prepare_face_for_display (it->f, face);
26092 if (it->image_id < 0)
26094 /* Fringe bitmap. */
26095 it->ascent = it->phys_ascent = 0;
26096 it->descent = it->phys_descent = 0;
26097 it->pixel_width = 0;
26098 it->nglyphs = 0;
26099 return;
26102 img = IMAGE_FROM_ID (it->f, it->image_id);
26103 /* Make sure X resources of the image is loaded. */
26104 prepare_image_for_display (it->f, img);
26106 slice.x = slice.y = 0;
26107 slice.width = img->width;
26108 slice.height = img->height;
26110 if (INTEGERP (it->slice.x))
26111 slice.x = XINT (it->slice.x);
26112 else if (FLOATP (it->slice.x))
26113 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26115 if (INTEGERP (it->slice.y))
26116 slice.y = XINT (it->slice.y);
26117 else if (FLOATP (it->slice.y))
26118 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26120 if (INTEGERP (it->slice.width))
26121 slice.width = XINT (it->slice.width);
26122 else if (FLOATP (it->slice.width))
26123 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26125 if (INTEGERP (it->slice.height))
26126 slice.height = XINT (it->slice.height);
26127 else if (FLOATP (it->slice.height))
26128 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26130 if (slice.x >= img->width)
26131 slice.x = img->width;
26132 if (slice.y >= img->height)
26133 slice.y = img->height;
26134 if (slice.x + slice.width >= img->width)
26135 slice.width = img->width - slice.x;
26136 if (slice.y + slice.height > img->height)
26137 slice.height = img->height - slice.y;
26139 if (slice.width == 0 || slice.height == 0)
26140 return;
26142 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26144 it->descent = slice.height - glyph_ascent;
26145 if (slice.y == 0)
26146 it->descent += img->vmargin;
26147 if (slice.y + slice.height == img->height)
26148 it->descent += img->vmargin;
26149 it->phys_descent = it->descent;
26151 it->pixel_width = slice.width;
26152 if (slice.x == 0)
26153 it->pixel_width += img->hmargin;
26154 if (slice.x + slice.width == img->width)
26155 it->pixel_width += img->hmargin;
26157 /* It's quite possible for images to have an ascent greater than
26158 their height, so don't get confused in that case. */
26159 if (it->descent < 0)
26160 it->descent = 0;
26162 it->nglyphs = 1;
26164 if (face->box != FACE_NO_BOX)
26166 if (face->box_line_width > 0)
26168 if (slice.y == 0)
26169 it->ascent += face->box_line_width;
26170 if (slice.y + slice.height == img->height)
26171 it->descent += face->box_line_width;
26174 if (it->start_of_box_run_p && slice.x == 0)
26175 it->pixel_width += eabs (face->box_line_width);
26176 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26177 it->pixel_width += eabs (face->box_line_width);
26180 take_vertical_position_into_account (it);
26182 /* Automatically crop wide image glyphs at right edge so we can
26183 draw the cursor on same display row. */
26184 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26185 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26187 it->pixel_width -= crop;
26188 slice.width -= crop;
26191 if (it->glyph_row)
26193 struct glyph *glyph;
26194 enum glyph_row_area area = it->area;
26196 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26197 if (it->glyph_row->reversed_p)
26199 struct glyph *g;
26201 /* Make room for the new glyph. */
26202 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26203 g[1] = *g;
26204 glyph = it->glyph_row->glyphs[it->area];
26206 if (glyph < it->glyph_row->glyphs[area + 1])
26208 glyph->charpos = CHARPOS (it->position);
26209 glyph->object = it->object;
26210 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26211 glyph->ascent = glyph_ascent;
26212 glyph->descent = it->descent;
26213 glyph->voffset = it->voffset;
26214 glyph->type = IMAGE_GLYPH;
26215 glyph->avoid_cursor_p = it->avoid_cursor_p;
26216 glyph->multibyte_p = it->multibyte_p;
26217 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26219 /* In R2L rows, the left and the right box edges need to be
26220 drawn in reverse direction. */
26221 glyph->right_box_line_p = it->start_of_box_run_p;
26222 glyph->left_box_line_p = it->end_of_box_run_p;
26224 else
26226 glyph->left_box_line_p = it->start_of_box_run_p;
26227 glyph->right_box_line_p = it->end_of_box_run_p;
26229 glyph->overlaps_vertically_p = false;
26230 glyph->padding_p = false;
26231 glyph->glyph_not_available_p = false;
26232 glyph->face_id = it->face_id;
26233 glyph->u.img_id = img->id;
26234 glyph->slice.img = slice;
26235 glyph->font_type = FONT_TYPE_UNKNOWN;
26236 if (it->bidi_p)
26238 glyph->resolved_level = it->bidi_it.resolved_level;
26239 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26240 glyph->bidi_type = it->bidi_it.type;
26242 ++it->glyph_row->used[area];
26244 else
26245 IT_EXPAND_MATRIX_WIDTH (it, area);
26249 static void
26250 produce_xwidget_glyph (struct it *it)
26252 #ifdef HAVE_XWIDGETS
26253 struct xwidget *xw;
26254 int glyph_ascent, crop;
26255 eassert (it->what == IT_XWIDGET);
26257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26258 /* Make sure X resources of the face is loaded. */
26259 prepare_face_for_display (it->f, face);
26261 xw = it->xwidget;
26262 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26263 it->descent = xw->height/2;
26264 it->phys_descent = it->descent;
26265 it->pixel_width = xw->width;
26266 /* It's quite possible for images to have an ascent greater than
26267 their height, so don't get confused in that case. */
26268 if (it->descent < 0)
26269 it->descent = 0;
26271 it->nglyphs = 1;
26273 if (face->box != FACE_NO_BOX)
26275 if (face->box_line_width > 0)
26277 it->ascent += face->box_line_width;
26278 it->descent += face->box_line_width;
26281 if (it->start_of_box_run_p)
26282 it->pixel_width += eabs (face->box_line_width);
26283 it->pixel_width += eabs (face->box_line_width);
26286 take_vertical_position_into_account (it);
26288 /* Automatically crop wide image glyphs at right edge so we can
26289 draw the cursor on same display row. */
26290 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26291 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26292 it->pixel_width -= crop;
26294 if (it->glyph_row)
26296 enum glyph_row_area area = it->area;
26297 struct glyph *glyph
26298 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26300 if (it->glyph_row->reversed_p)
26302 struct glyph *g;
26304 /* Make room for the new glyph. */
26305 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26306 g[1] = *g;
26307 glyph = it->glyph_row->glyphs[it->area];
26309 if (glyph < it->glyph_row->glyphs[area + 1])
26311 glyph->charpos = CHARPOS (it->position);
26312 glyph->object = it->object;
26313 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26314 glyph->ascent = glyph_ascent;
26315 glyph->descent = it->descent;
26316 glyph->voffset = it->voffset;
26317 glyph->type = XWIDGET_GLYPH;
26318 glyph->avoid_cursor_p = it->avoid_cursor_p;
26319 glyph->multibyte_p = it->multibyte_p;
26320 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26322 /* In R2L rows, the left and the right box edges need to be
26323 drawn in reverse direction. */
26324 glyph->right_box_line_p = it->start_of_box_run_p;
26325 glyph->left_box_line_p = it->end_of_box_run_p;
26327 else
26329 glyph->left_box_line_p = it->start_of_box_run_p;
26330 glyph->right_box_line_p = it->end_of_box_run_p;
26332 glyph->overlaps_vertically_p = 0;
26333 glyph->padding_p = 0;
26334 glyph->glyph_not_available_p = 0;
26335 glyph->face_id = it->face_id;
26336 glyph->u.xwidget = it->xwidget;
26337 glyph->font_type = FONT_TYPE_UNKNOWN;
26338 if (it->bidi_p)
26340 glyph->resolved_level = it->bidi_it.resolved_level;
26341 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26342 glyph->bidi_type = it->bidi_it.type;
26344 ++it->glyph_row->used[area];
26346 else
26347 IT_EXPAND_MATRIX_WIDTH (it, area);
26349 #endif
26352 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26353 of the glyph, WIDTH and HEIGHT are the width and height of the
26354 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26356 static void
26357 append_stretch_glyph (struct it *it, Lisp_Object object,
26358 int width, int height, int ascent)
26360 struct glyph *glyph;
26361 enum glyph_row_area area = it->area;
26363 eassert (ascent >= 0 && ascent <= height);
26365 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26366 if (glyph < it->glyph_row->glyphs[area + 1])
26368 /* If the glyph row is reversed, we need to prepend the glyph
26369 rather than append it. */
26370 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26372 struct glyph *g;
26374 /* Make room for the additional glyph. */
26375 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26376 g[1] = *g;
26377 glyph = it->glyph_row->glyphs[area];
26379 /* Decrease the width of the first glyph of the row that
26380 begins before first_visible_x (e.g., due to hscroll).
26381 This is so the overall width of the row becomes smaller
26382 by the scroll amount, and the stretch glyph appended by
26383 extend_face_to_end_of_line will be wider, to shift the
26384 row glyphs to the right. (In L2R rows, the corresponding
26385 left-shift effect is accomplished by setting row->x to a
26386 negative value, which won't work with R2L rows.)
26388 This must leave us with a positive value of WIDTH, since
26389 otherwise the call to move_it_in_display_line_to at the
26390 beginning of display_line would have got past the entire
26391 first glyph, and then it->current_x would have been
26392 greater or equal to it->first_visible_x. */
26393 if (it->current_x < it->first_visible_x)
26394 width -= it->first_visible_x - it->current_x;
26395 eassert (width > 0);
26397 glyph->charpos = CHARPOS (it->position);
26398 glyph->object = object;
26399 /* FIXME: It would be better to use TYPE_MAX here, but
26400 __typeof__ is not portable enough... */
26401 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26402 glyph->ascent = ascent;
26403 glyph->descent = height - ascent;
26404 glyph->voffset = it->voffset;
26405 glyph->type = STRETCH_GLYPH;
26406 glyph->avoid_cursor_p = it->avoid_cursor_p;
26407 glyph->multibyte_p = it->multibyte_p;
26408 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26410 /* In R2L rows, the left and the right box edges need to be
26411 drawn in reverse direction. */
26412 glyph->right_box_line_p = it->start_of_box_run_p;
26413 glyph->left_box_line_p = it->end_of_box_run_p;
26415 else
26417 glyph->left_box_line_p = it->start_of_box_run_p;
26418 glyph->right_box_line_p = it->end_of_box_run_p;
26420 glyph->overlaps_vertically_p = false;
26421 glyph->padding_p = false;
26422 glyph->glyph_not_available_p = false;
26423 glyph->face_id = it->face_id;
26424 glyph->u.stretch.ascent = ascent;
26425 glyph->u.stretch.height = height;
26426 glyph->slice.img = null_glyph_slice;
26427 glyph->font_type = FONT_TYPE_UNKNOWN;
26428 if (it->bidi_p)
26430 glyph->resolved_level = it->bidi_it.resolved_level;
26431 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26432 glyph->bidi_type = it->bidi_it.type;
26434 else
26436 glyph->resolved_level = 0;
26437 glyph->bidi_type = UNKNOWN_BT;
26439 ++it->glyph_row->used[area];
26441 else
26442 IT_EXPAND_MATRIX_WIDTH (it, area);
26445 #endif /* HAVE_WINDOW_SYSTEM */
26447 /* Produce a stretch glyph for iterator IT. IT->object is the value
26448 of the glyph property displayed. The value must be a list
26449 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26450 being recognized:
26452 1. `:width WIDTH' specifies that the space should be WIDTH *
26453 canonical char width wide. WIDTH may be an integer or floating
26454 point number.
26456 2. `:relative-width FACTOR' specifies that the width of the stretch
26457 should be computed from the width of the first character having the
26458 `glyph' property, and should be FACTOR times that width.
26460 3. `:align-to HPOS' specifies that the space should be wide enough
26461 to reach HPOS, a value in canonical character units.
26463 Exactly one of the above pairs must be present.
26465 4. `:height HEIGHT' specifies that the height of the stretch produced
26466 should be HEIGHT, measured in canonical character units.
26468 5. `:relative-height FACTOR' specifies that the height of the
26469 stretch should be FACTOR times the height of the characters having
26470 the glyph property.
26472 Either none or exactly one of 4 or 5 must be present.
26474 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26475 of the stretch should be used for the ascent of the stretch.
26476 ASCENT must be in the range 0 <= ASCENT <= 100. */
26478 void
26479 produce_stretch_glyph (struct it *it)
26481 /* (space :width WIDTH :height HEIGHT ...) */
26482 Lisp_Object prop, plist;
26483 int width = 0, height = 0, align_to = -1;
26484 bool zero_width_ok_p = false;
26485 double tem;
26486 struct font *font = NULL;
26488 #ifdef HAVE_WINDOW_SYSTEM
26489 int ascent = 0;
26490 bool zero_height_ok_p = false;
26492 if (FRAME_WINDOW_P (it->f))
26494 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26495 font = face->font ? face->font : FRAME_FONT (it->f);
26496 prepare_face_for_display (it->f, face);
26498 #endif
26500 /* List should start with `space'. */
26501 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26502 plist = XCDR (it->object);
26504 /* Compute the width of the stretch. */
26505 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26506 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26508 /* Absolute width `:width WIDTH' specified and valid. */
26509 zero_width_ok_p = true;
26510 width = (int)tem;
26512 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26514 /* Relative width `:relative-width FACTOR' specified and valid.
26515 Compute the width of the characters having the `glyph'
26516 property. */
26517 struct it it2;
26518 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26520 it2 = *it;
26521 if (it->multibyte_p)
26522 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26523 else
26525 it2.c = it2.char_to_display = *p, it2.len = 1;
26526 if (! ASCII_CHAR_P (it2.c))
26527 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26530 it2.glyph_row = NULL;
26531 it2.what = IT_CHARACTER;
26532 PRODUCE_GLYPHS (&it2);
26533 width = NUMVAL (prop) * it2.pixel_width;
26535 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26536 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26537 &align_to))
26539 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26540 align_to = (align_to < 0
26542 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26543 else if (align_to < 0)
26544 align_to = window_box_left_offset (it->w, TEXT_AREA);
26545 width = max (0, (int)tem + align_to - it->current_x);
26546 zero_width_ok_p = true;
26548 else
26549 /* Nothing specified -> width defaults to canonical char width. */
26550 width = FRAME_COLUMN_WIDTH (it->f);
26552 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26553 width = 1;
26555 #ifdef HAVE_WINDOW_SYSTEM
26556 /* Compute height. */
26557 if (FRAME_WINDOW_P (it->f))
26559 int default_height = normal_char_height (font, ' ');
26561 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26562 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26564 height = (int)tem;
26565 zero_height_ok_p = true;
26567 else if (prop = Fplist_get (plist, QCrelative_height),
26568 NUMVAL (prop) > 0)
26569 height = default_height * NUMVAL (prop);
26570 else
26571 height = default_height;
26573 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26574 height = 1;
26576 /* Compute percentage of height used for ascent. If
26577 `:ascent ASCENT' is present and valid, use that. Otherwise,
26578 derive the ascent from the font in use. */
26579 if (prop = Fplist_get (plist, QCascent),
26580 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26581 ascent = height * NUMVAL (prop) / 100.0;
26582 else if (!NILP (prop)
26583 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26584 ascent = min (max (0, (int)tem), height);
26585 else
26586 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26588 else
26589 #endif /* HAVE_WINDOW_SYSTEM */
26590 height = 1;
26592 if (width > 0 && it->line_wrap != TRUNCATE
26593 && it->current_x + width > it->last_visible_x)
26595 width = it->last_visible_x - it->current_x;
26596 #ifdef HAVE_WINDOW_SYSTEM
26597 /* Subtract one more pixel from the stretch width, but only on
26598 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26599 width -= FRAME_WINDOW_P (it->f);
26600 #endif
26603 if (width > 0 && height > 0 && it->glyph_row)
26605 Lisp_Object o_object = it->object;
26606 Lisp_Object object = it->stack[it->sp - 1].string;
26607 int n = width;
26609 if (!STRINGP (object))
26610 object = it->w->contents;
26611 #ifdef HAVE_WINDOW_SYSTEM
26612 if (FRAME_WINDOW_P (it->f))
26613 append_stretch_glyph (it, object, width, height, ascent);
26614 else
26615 #endif
26617 it->object = object;
26618 it->char_to_display = ' ';
26619 it->pixel_width = it->len = 1;
26620 while (n--)
26621 tty_append_glyph (it);
26622 it->object = o_object;
26626 it->pixel_width = width;
26627 #ifdef HAVE_WINDOW_SYSTEM
26628 if (FRAME_WINDOW_P (it->f))
26630 it->ascent = it->phys_ascent = ascent;
26631 it->descent = it->phys_descent = height - it->ascent;
26632 it->nglyphs = width > 0 && height > 0;
26633 take_vertical_position_into_account (it);
26635 else
26636 #endif
26637 it->nglyphs = width;
26640 /* Get information about special display element WHAT in an
26641 environment described by IT. WHAT is one of IT_TRUNCATION or
26642 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26643 non-null glyph_row member. This function ensures that fields like
26644 face_id, c, len of IT are left untouched. */
26646 static void
26647 produce_special_glyphs (struct it *it, enum display_element_type what)
26649 struct it temp_it;
26650 Lisp_Object gc;
26651 GLYPH glyph;
26653 temp_it = *it;
26654 temp_it.object = Qnil;
26655 memset (&temp_it.current, 0, sizeof temp_it.current);
26657 if (what == IT_CONTINUATION)
26659 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26660 if (it->bidi_it.paragraph_dir == R2L)
26661 SET_GLYPH_FROM_CHAR (glyph, '/');
26662 else
26663 SET_GLYPH_FROM_CHAR (glyph, '\\');
26664 if (it->dp
26665 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26667 /* FIXME: Should we mirror GC for R2L lines? */
26668 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26669 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26672 else if (what == IT_TRUNCATION)
26674 /* Truncation glyph. */
26675 SET_GLYPH_FROM_CHAR (glyph, '$');
26676 if (it->dp
26677 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26679 /* FIXME: Should we mirror GC for R2L lines? */
26680 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26681 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26684 else
26685 emacs_abort ();
26687 #ifdef HAVE_WINDOW_SYSTEM
26688 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26689 is turned off, we precede the truncation/continuation glyphs by a
26690 stretch glyph whose width is computed such that these special
26691 glyphs are aligned at the window margin, even when very different
26692 fonts are used in different glyph rows. */
26693 if (FRAME_WINDOW_P (temp_it.f)
26694 /* init_iterator calls this with it->glyph_row == NULL, and it
26695 wants only the pixel width of the truncation/continuation
26696 glyphs. */
26697 && temp_it.glyph_row
26698 /* insert_left_trunc_glyphs calls us at the beginning of the
26699 row, and it has its own calculation of the stretch glyph
26700 width. */
26701 && temp_it.glyph_row->used[TEXT_AREA] > 0
26702 && (temp_it.glyph_row->reversed_p
26703 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26704 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26706 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26708 if (stretch_width > 0)
26710 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26711 struct font *font =
26712 face->font ? face->font : FRAME_FONT (temp_it.f);
26713 int stretch_ascent =
26714 (((temp_it.ascent + temp_it.descent)
26715 * FONT_BASE (font)) / FONT_HEIGHT (font));
26717 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26718 temp_it.ascent + temp_it.descent,
26719 stretch_ascent);
26722 #endif
26724 temp_it.dp = NULL;
26725 temp_it.what = IT_CHARACTER;
26726 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26727 temp_it.face_id = GLYPH_FACE (glyph);
26728 temp_it.len = CHAR_BYTES (temp_it.c);
26730 PRODUCE_GLYPHS (&temp_it);
26731 it->pixel_width = temp_it.pixel_width;
26732 it->nglyphs = temp_it.nglyphs;
26735 #ifdef HAVE_WINDOW_SYSTEM
26737 /* Calculate line-height and line-spacing properties.
26738 An integer value specifies explicit pixel value.
26739 A float value specifies relative value to current face height.
26740 A cons (float . face-name) specifies relative value to
26741 height of specified face font.
26743 Returns height in pixels, or nil. */
26745 static Lisp_Object
26746 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26747 int boff, bool override)
26749 Lisp_Object face_name = Qnil;
26750 int ascent, descent, height;
26752 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26753 return val;
26755 if (CONSP (val))
26757 face_name = XCAR (val);
26758 val = XCDR (val);
26759 if (!NUMBERP (val))
26760 val = make_number (1);
26761 if (NILP (face_name))
26763 height = it->ascent + it->descent;
26764 goto scale;
26768 if (NILP (face_name))
26770 font = FRAME_FONT (it->f);
26771 boff = FRAME_BASELINE_OFFSET (it->f);
26773 else if (EQ (face_name, Qt))
26775 override = false;
26777 else
26779 int face_id;
26780 struct face *face;
26782 face_id = lookup_named_face (it->f, face_name, false);
26783 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26784 if (face == NULL || ((font = face->font) == NULL))
26785 return make_number (-1);
26786 boff = font->baseline_offset;
26787 if (font->vertical_centering)
26788 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26791 normal_char_ascent_descent (font, -1, &ascent, &descent);
26793 if (override)
26795 it->override_ascent = ascent;
26796 it->override_descent = descent;
26797 it->override_boff = boff;
26800 height = ascent + descent;
26802 scale:
26803 if (FLOATP (val))
26804 height = (int)(XFLOAT_DATA (val) * height);
26805 else if (INTEGERP (val))
26806 height *= XINT (val);
26808 return make_number (height);
26812 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26813 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26814 and only if this is for a character for which no font was found.
26816 If the display method (it->glyphless_method) is
26817 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26818 length of the acronym or the hexadecimal string, UPPER_XOFF and
26819 UPPER_YOFF are pixel offsets for the upper part of the string,
26820 LOWER_XOFF and LOWER_YOFF are for the lower part.
26822 For the other display methods, LEN through LOWER_YOFF are zero. */
26824 static void
26825 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26826 short upper_xoff, short upper_yoff,
26827 short lower_xoff, short lower_yoff)
26829 struct glyph *glyph;
26830 enum glyph_row_area area = it->area;
26832 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26833 if (glyph < it->glyph_row->glyphs[area + 1])
26835 /* If the glyph row is reversed, we need to prepend the glyph
26836 rather than append it. */
26837 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26839 struct glyph *g;
26841 /* Make room for the additional glyph. */
26842 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26843 g[1] = *g;
26844 glyph = it->glyph_row->glyphs[area];
26846 glyph->charpos = CHARPOS (it->position);
26847 glyph->object = it->object;
26848 eassert (it->pixel_width <= SHRT_MAX);
26849 glyph->pixel_width = it->pixel_width;
26850 glyph->ascent = it->ascent;
26851 glyph->descent = it->descent;
26852 glyph->voffset = it->voffset;
26853 glyph->type = GLYPHLESS_GLYPH;
26854 glyph->u.glyphless.method = it->glyphless_method;
26855 glyph->u.glyphless.for_no_font = for_no_font;
26856 glyph->u.glyphless.len = len;
26857 glyph->u.glyphless.ch = it->c;
26858 glyph->slice.glyphless.upper_xoff = upper_xoff;
26859 glyph->slice.glyphless.upper_yoff = upper_yoff;
26860 glyph->slice.glyphless.lower_xoff = lower_xoff;
26861 glyph->slice.glyphless.lower_yoff = lower_yoff;
26862 glyph->avoid_cursor_p = it->avoid_cursor_p;
26863 glyph->multibyte_p = it->multibyte_p;
26864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26866 /* In R2L rows, the left and the right box edges need to be
26867 drawn in reverse direction. */
26868 glyph->right_box_line_p = it->start_of_box_run_p;
26869 glyph->left_box_line_p = it->end_of_box_run_p;
26871 else
26873 glyph->left_box_line_p = it->start_of_box_run_p;
26874 glyph->right_box_line_p = it->end_of_box_run_p;
26876 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26877 || it->phys_descent > it->descent);
26878 glyph->padding_p = false;
26879 glyph->glyph_not_available_p = false;
26880 glyph->face_id = face_id;
26881 glyph->font_type = FONT_TYPE_UNKNOWN;
26882 if (it->bidi_p)
26884 glyph->resolved_level = it->bidi_it.resolved_level;
26885 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26886 glyph->bidi_type = it->bidi_it.type;
26888 ++it->glyph_row->used[area];
26890 else
26891 IT_EXPAND_MATRIX_WIDTH (it, area);
26895 /* Produce a glyph for a glyphless character for iterator IT.
26896 IT->glyphless_method specifies which method to use for displaying
26897 the character. See the description of enum
26898 glyphless_display_method in dispextern.h for the detail.
26900 FOR_NO_FONT is true if and only if this is for a character for
26901 which no font was found. ACRONYM, if non-nil, is an acronym string
26902 for the character. */
26904 static void
26905 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26907 int face_id;
26908 struct face *face;
26909 struct font *font;
26910 int base_width, base_height, width, height;
26911 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26912 int len;
26914 /* Get the metrics of the base font. We always refer to the current
26915 ASCII face. */
26916 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26917 font = face->font ? face->font : FRAME_FONT (it->f);
26918 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26919 it->ascent += font->baseline_offset;
26920 it->descent -= font->baseline_offset;
26921 base_height = it->ascent + it->descent;
26922 base_width = font->average_width;
26924 face_id = merge_glyphless_glyph_face (it);
26926 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26928 it->pixel_width = THIN_SPACE_WIDTH;
26929 len = 0;
26930 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26932 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26934 width = CHARACTER_WIDTH (it->c);
26935 if (width == 0)
26936 width = 1;
26937 else if (width > 4)
26938 width = 4;
26939 it->pixel_width = base_width * width;
26940 len = 0;
26941 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26943 else
26945 char buf[7];
26946 const char *str;
26947 unsigned int code[6];
26948 int upper_len;
26949 int ascent, descent;
26950 struct font_metrics metrics_upper, metrics_lower;
26952 face = FACE_FROM_ID (it->f, face_id);
26953 font = face->font ? face->font : FRAME_FONT (it->f);
26954 prepare_face_for_display (it->f, face);
26956 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26958 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26959 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26960 if (CONSP (acronym))
26961 acronym = XCAR (acronym);
26962 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26964 else
26966 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26967 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26968 str = buf;
26970 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26971 code[len] = font->driver->encode_char (font, str[len]);
26972 upper_len = (len + 1) / 2;
26973 font->driver->text_extents (font, code, upper_len,
26974 &metrics_upper);
26975 font->driver->text_extents (font, code + upper_len, len - upper_len,
26976 &metrics_lower);
26980 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26981 width = max (metrics_upper.width, metrics_lower.width) + 4;
26982 upper_xoff = upper_yoff = 2; /* the typical case */
26983 if (base_width >= width)
26985 /* Align the upper to the left, the lower to the right. */
26986 it->pixel_width = base_width;
26987 lower_xoff = base_width - 2 - metrics_lower.width;
26989 else
26991 /* Center the shorter one. */
26992 it->pixel_width = width;
26993 if (metrics_upper.width >= metrics_lower.width)
26994 lower_xoff = (width - metrics_lower.width) / 2;
26995 else
26997 /* FIXME: This code doesn't look right. It formerly was
26998 missing the "lower_xoff = 0;", which couldn't have
26999 been right since it left lower_xoff uninitialized. */
27000 lower_xoff = 0;
27001 upper_xoff = (width - metrics_upper.width) / 2;
27005 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27006 top, bottom, and between upper and lower strings. */
27007 height = (metrics_upper.ascent + metrics_upper.descent
27008 + metrics_lower.ascent + metrics_lower.descent) + 5;
27009 /* Center vertically.
27010 H:base_height, D:base_descent
27011 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27013 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27014 descent = D - H/2 + h/2;
27015 lower_yoff = descent - 2 - ld;
27016 upper_yoff = lower_yoff - la - 1 - ud; */
27017 ascent = - (it->descent - (base_height + height + 1) / 2);
27018 descent = it->descent - (base_height - height) / 2;
27019 lower_yoff = descent - 2 - metrics_lower.descent;
27020 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27021 - metrics_upper.descent);
27022 /* Don't make the height shorter than the base height. */
27023 if (height > base_height)
27025 it->ascent = ascent;
27026 it->descent = descent;
27030 it->phys_ascent = it->ascent;
27031 it->phys_descent = it->descent;
27032 if (it->glyph_row)
27033 append_glyphless_glyph (it, face_id, for_no_font, len,
27034 upper_xoff, upper_yoff,
27035 lower_xoff, lower_yoff);
27036 it->nglyphs = 1;
27037 take_vertical_position_into_account (it);
27041 /* RIF:
27042 Produce glyphs/get display metrics for the display element IT is
27043 loaded with. See the description of struct it in dispextern.h
27044 for an overview of struct it. */
27046 void
27047 x_produce_glyphs (struct it *it)
27049 int extra_line_spacing = it->extra_line_spacing;
27051 it->glyph_not_available_p = false;
27053 if (it->what == IT_CHARACTER)
27055 XChar2b char2b;
27056 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27057 struct font *font = face->font;
27058 struct font_metrics *pcm = NULL;
27059 int boff; /* Baseline offset. */
27061 if (font == NULL)
27063 /* When no suitable font is found, display this character by
27064 the method specified in the first extra slot of
27065 Vglyphless_char_display. */
27066 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27068 eassert (it->what == IT_GLYPHLESS);
27069 produce_glyphless_glyph (it, true,
27070 STRINGP (acronym) ? acronym : Qnil);
27071 goto done;
27074 boff = font->baseline_offset;
27075 if (font->vertical_centering)
27076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27078 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27080 it->nglyphs = 1;
27082 if (it->override_ascent >= 0)
27084 it->ascent = it->override_ascent;
27085 it->descent = it->override_descent;
27086 boff = it->override_boff;
27088 else
27090 it->ascent = FONT_BASE (font) + boff;
27091 it->descent = FONT_DESCENT (font) - boff;
27094 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27096 pcm = get_per_char_metric (font, &char2b);
27097 if (pcm->width == 0
27098 && pcm->rbearing == 0 && pcm->lbearing == 0)
27099 pcm = NULL;
27102 if (pcm)
27104 it->phys_ascent = pcm->ascent + boff;
27105 it->phys_descent = pcm->descent - boff;
27106 it->pixel_width = pcm->width;
27107 /* Don't use font-global values for ascent and descent
27108 if they result in an exceedingly large line height. */
27109 if (it->override_ascent < 0)
27111 if (FONT_TOO_HIGH (font))
27113 it->ascent = it->phys_ascent;
27114 it->descent = it->phys_descent;
27115 /* These limitations are enforced by an
27116 assertion near the end of this function. */
27117 if (it->ascent < 0)
27118 it->ascent = 0;
27119 if (it->descent < 0)
27120 it->descent = 0;
27124 else
27126 it->glyph_not_available_p = true;
27127 it->phys_ascent = it->ascent;
27128 it->phys_descent = it->descent;
27129 it->pixel_width = font->space_width;
27132 if (it->constrain_row_ascent_descent_p)
27134 if (it->descent > it->max_descent)
27136 it->ascent += it->descent - it->max_descent;
27137 it->descent = it->max_descent;
27139 if (it->ascent > it->max_ascent)
27141 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27142 it->ascent = it->max_ascent;
27144 it->phys_ascent = min (it->phys_ascent, it->ascent);
27145 it->phys_descent = min (it->phys_descent, it->descent);
27146 extra_line_spacing = 0;
27149 /* If this is a space inside a region of text with
27150 `space-width' property, change its width. */
27151 bool stretched_p
27152 = it->char_to_display == ' ' && !NILP (it->space_width);
27153 if (stretched_p)
27154 it->pixel_width *= XFLOATINT (it->space_width);
27156 /* If face has a box, add the box thickness to the character
27157 height. If character has a box line to the left and/or
27158 right, add the box line width to the character's width. */
27159 if (face->box != FACE_NO_BOX)
27161 int thick = face->box_line_width;
27163 if (thick > 0)
27165 it->ascent += thick;
27166 it->descent += thick;
27168 else
27169 thick = -thick;
27171 if (it->start_of_box_run_p)
27172 it->pixel_width += thick;
27173 if (it->end_of_box_run_p)
27174 it->pixel_width += thick;
27177 /* If face has an overline, add the height of the overline
27178 (1 pixel) and a 1 pixel margin to the character height. */
27179 if (face->overline_p)
27180 it->ascent += overline_margin;
27182 if (it->constrain_row_ascent_descent_p)
27184 if (it->ascent > it->max_ascent)
27185 it->ascent = it->max_ascent;
27186 if (it->descent > it->max_descent)
27187 it->descent = it->max_descent;
27190 take_vertical_position_into_account (it);
27192 /* If we have to actually produce glyphs, do it. */
27193 if (it->glyph_row)
27195 if (stretched_p)
27197 /* Translate a space with a `space-width' property
27198 into a stretch glyph. */
27199 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27200 / FONT_HEIGHT (font));
27201 append_stretch_glyph (it, it->object, it->pixel_width,
27202 it->ascent + it->descent, ascent);
27204 else
27205 append_glyph (it);
27207 /* If characters with lbearing or rbearing are displayed
27208 in this line, record that fact in a flag of the
27209 glyph row. This is used to optimize X output code. */
27210 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27211 it->glyph_row->contains_overlapping_glyphs_p = true;
27213 if (! stretched_p && it->pixel_width == 0)
27214 /* We assure that all visible glyphs have at least 1-pixel
27215 width. */
27216 it->pixel_width = 1;
27218 else if (it->char_to_display == '\n')
27220 /* A newline has no width, but we need the height of the
27221 line. But if previous part of the line sets a height,
27222 don't increase that height. */
27224 Lisp_Object height;
27225 Lisp_Object total_height = Qnil;
27227 it->override_ascent = -1;
27228 it->pixel_width = 0;
27229 it->nglyphs = 0;
27231 height = get_it_property (it, Qline_height);
27232 /* Split (line-height total-height) list. */
27233 if (CONSP (height)
27234 && CONSP (XCDR (height))
27235 && NILP (XCDR (XCDR (height))))
27237 total_height = XCAR (XCDR (height));
27238 height = XCAR (height);
27240 height = calc_line_height_property (it, height, font, boff, true);
27242 if (it->override_ascent >= 0)
27244 it->ascent = it->override_ascent;
27245 it->descent = it->override_descent;
27246 boff = it->override_boff;
27248 else
27250 if (FONT_TOO_HIGH (font))
27252 it->ascent = font->pixel_size + boff - 1;
27253 it->descent = -boff + 1;
27254 if (it->descent < 0)
27255 it->descent = 0;
27257 else
27259 it->ascent = FONT_BASE (font) + boff;
27260 it->descent = FONT_DESCENT (font) - boff;
27264 if (EQ (height, Qt))
27266 if (it->descent > it->max_descent)
27268 it->ascent += it->descent - it->max_descent;
27269 it->descent = it->max_descent;
27271 if (it->ascent > it->max_ascent)
27273 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27274 it->ascent = it->max_ascent;
27276 it->phys_ascent = min (it->phys_ascent, it->ascent);
27277 it->phys_descent = min (it->phys_descent, it->descent);
27278 it->constrain_row_ascent_descent_p = true;
27279 extra_line_spacing = 0;
27281 else
27283 Lisp_Object spacing;
27285 it->phys_ascent = it->ascent;
27286 it->phys_descent = it->descent;
27288 if ((it->max_ascent > 0 || it->max_descent > 0)
27289 && face->box != FACE_NO_BOX
27290 && face->box_line_width > 0)
27292 it->ascent += face->box_line_width;
27293 it->descent += face->box_line_width;
27295 if (!NILP (height)
27296 && XINT (height) > it->ascent + it->descent)
27297 it->ascent = XINT (height) - it->descent;
27299 if (!NILP (total_height))
27300 spacing = calc_line_height_property (it, total_height, font,
27301 boff, false);
27302 else
27304 spacing = get_it_property (it, Qline_spacing);
27305 spacing = calc_line_height_property (it, spacing, font,
27306 boff, false);
27308 if (INTEGERP (spacing))
27310 extra_line_spacing = XINT (spacing);
27311 if (!NILP (total_height))
27312 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27316 else /* i.e. (it->char_to_display == '\t') */
27318 if (font->space_width > 0)
27320 int tab_width = it->tab_width * font->space_width;
27321 int x = it->current_x + it->continuation_lines_width;
27322 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27324 /* If the distance from the current position to the next tab
27325 stop is less than a space character width, use the
27326 tab stop after that. */
27327 if (next_tab_x - x < font->space_width)
27328 next_tab_x += tab_width;
27330 it->pixel_width = next_tab_x - x;
27331 it->nglyphs = 1;
27332 if (FONT_TOO_HIGH (font))
27334 if (get_char_glyph_code (' ', font, &char2b))
27336 pcm = get_per_char_metric (font, &char2b);
27337 if (pcm->width == 0
27338 && pcm->rbearing == 0 && pcm->lbearing == 0)
27339 pcm = NULL;
27342 if (pcm)
27344 it->ascent = pcm->ascent + boff;
27345 it->descent = pcm->descent - boff;
27347 else
27349 it->ascent = font->pixel_size + boff - 1;
27350 it->descent = -boff + 1;
27352 if (it->ascent < 0)
27353 it->ascent = 0;
27354 if (it->descent < 0)
27355 it->descent = 0;
27357 else
27359 it->ascent = FONT_BASE (font) + boff;
27360 it->descent = FONT_DESCENT (font) - boff;
27362 it->phys_ascent = it->ascent;
27363 it->phys_descent = it->descent;
27365 if (it->glyph_row)
27367 append_stretch_glyph (it, it->object, it->pixel_width,
27368 it->ascent + it->descent, it->ascent);
27371 else
27373 it->pixel_width = 0;
27374 it->nglyphs = 1;
27378 if (FONT_TOO_HIGH (font))
27380 int font_ascent, font_descent;
27382 /* For very large fonts, where we ignore the declared font
27383 dimensions, and go by per-character metrics instead,
27384 don't let the row ascent and descent values (and the row
27385 height computed from them) be smaller than the "normal"
27386 character metrics. This avoids unpleasant effects
27387 whereby lines on display would change their height
27388 depending on which characters are shown. */
27389 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27390 it->max_ascent = max (it->max_ascent, font_ascent);
27391 it->max_descent = max (it->max_descent, font_descent);
27394 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27396 /* A static composition.
27398 Note: A composition is represented as one glyph in the
27399 glyph matrix. There are no padding glyphs.
27401 Important note: pixel_width, ascent, and descent are the
27402 values of what is drawn by draw_glyphs (i.e. the values of
27403 the overall glyphs composed). */
27404 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27405 int boff; /* baseline offset */
27406 struct composition *cmp = composition_table[it->cmp_it.id];
27407 int glyph_len = cmp->glyph_len;
27408 struct font *font = face->font;
27410 it->nglyphs = 1;
27412 /* If we have not yet calculated pixel size data of glyphs of
27413 the composition for the current face font, calculate them
27414 now. Theoretically, we have to check all fonts for the
27415 glyphs, but that requires much time and memory space. So,
27416 here we check only the font of the first glyph. This may
27417 lead to incorrect display, but it's very rare, and C-l
27418 (recenter-top-bottom) can correct the display anyway. */
27419 if (! cmp->font || cmp->font != font)
27421 /* Ascent and descent of the font of the first character
27422 of this composition (adjusted by baseline offset).
27423 Ascent and descent of overall glyphs should not be less
27424 than these, respectively. */
27425 int font_ascent, font_descent, font_height;
27426 /* Bounding box of the overall glyphs. */
27427 int leftmost, rightmost, lowest, highest;
27428 int lbearing, rbearing;
27429 int i, width, ascent, descent;
27430 int c;
27431 XChar2b char2b;
27432 struct font_metrics *pcm;
27433 ptrdiff_t pos;
27435 eassume (0 < glyph_len); /* See Bug#8512. */
27437 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27438 while (c == '\t' && 0 < --glyph_len);
27440 bool right_padded = glyph_len < cmp->glyph_len;
27441 for (i = 0; i < glyph_len; i++)
27443 c = COMPOSITION_GLYPH (cmp, i);
27444 if (c != '\t')
27445 break;
27446 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27448 bool left_padded = i > 0;
27450 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27451 : IT_CHARPOS (*it));
27452 /* If no suitable font is found, use the default font. */
27453 bool font_not_found_p = font == NULL;
27454 if (font_not_found_p)
27456 face = face->ascii_face;
27457 font = face->font;
27459 boff = font->baseline_offset;
27460 if (font->vertical_centering)
27461 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27462 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27463 font_ascent += boff;
27464 font_descent -= boff;
27465 font_height = font_ascent + font_descent;
27467 cmp->font = font;
27469 pcm = NULL;
27470 if (! font_not_found_p)
27472 get_char_face_and_encoding (it->f, c, it->face_id,
27473 &char2b, false);
27474 pcm = get_per_char_metric (font, &char2b);
27477 /* Initialize the bounding box. */
27478 if (pcm)
27480 width = cmp->glyph_len > 0 ? pcm->width : 0;
27481 ascent = pcm->ascent;
27482 descent = pcm->descent;
27483 lbearing = pcm->lbearing;
27484 rbearing = pcm->rbearing;
27486 else
27488 width = cmp->glyph_len > 0 ? font->space_width : 0;
27489 ascent = FONT_BASE (font);
27490 descent = FONT_DESCENT (font);
27491 lbearing = 0;
27492 rbearing = width;
27495 rightmost = width;
27496 leftmost = 0;
27497 lowest = - descent + boff;
27498 highest = ascent + boff;
27500 if (! font_not_found_p
27501 && font->default_ascent
27502 && CHAR_TABLE_P (Vuse_default_ascent)
27503 && !NILP (Faref (Vuse_default_ascent,
27504 make_number (it->char_to_display))))
27505 highest = font->default_ascent + boff;
27507 /* Draw the first glyph at the normal position. It may be
27508 shifted to right later if some other glyphs are drawn
27509 at the left. */
27510 cmp->offsets[i * 2] = 0;
27511 cmp->offsets[i * 2 + 1] = boff;
27512 cmp->lbearing = lbearing;
27513 cmp->rbearing = rbearing;
27515 /* Set cmp->offsets for the remaining glyphs. */
27516 for (i++; i < glyph_len; i++)
27518 int left, right, btm, top;
27519 int ch = COMPOSITION_GLYPH (cmp, i);
27520 int face_id;
27521 struct face *this_face;
27523 if (ch == '\t')
27524 ch = ' ';
27525 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27526 this_face = FACE_FROM_ID (it->f, face_id);
27527 font = this_face->font;
27529 if (font == NULL)
27530 pcm = NULL;
27531 else
27533 get_char_face_and_encoding (it->f, ch, face_id,
27534 &char2b, false);
27535 pcm = get_per_char_metric (font, &char2b);
27537 if (! pcm)
27538 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27539 else
27541 width = pcm->width;
27542 ascent = pcm->ascent;
27543 descent = pcm->descent;
27544 lbearing = pcm->lbearing;
27545 rbearing = pcm->rbearing;
27546 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27548 /* Relative composition with or without
27549 alternate chars. */
27550 left = (leftmost + rightmost - width) / 2;
27551 btm = - descent + boff;
27552 if (font->relative_compose
27553 && (! CHAR_TABLE_P (Vignore_relative_composition)
27554 || NILP (Faref (Vignore_relative_composition,
27555 make_number (ch)))))
27558 if (- descent >= font->relative_compose)
27559 /* One extra pixel between two glyphs. */
27560 btm = highest + 1;
27561 else if (ascent <= 0)
27562 /* One extra pixel between two glyphs. */
27563 btm = lowest - 1 - ascent - descent;
27566 else
27568 /* A composition rule is specified by an integer
27569 value that encodes global and new reference
27570 points (GREF and NREF). GREF and NREF are
27571 specified by numbers as below:
27573 0---1---2 -- ascent
27577 9--10--11 -- center
27579 ---3---4---5--- baseline
27581 6---7---8 -- descent
27583 int rule = COMPOSITION_RULE (cmp, i);
27584 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27586 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27587 grefx = gref % 3, nrefx = nref % 3;
27588 grefy = gref / 3, nrefy = nref / 3;
27589 if (xoff)
27590 xoff = font_height * (xoff - 128) / 256;
27591 if (yoff)
27592 yoff = font_height * (yoff - 128) / 256;
27594 left = (leftmost
27595 + grefx * (rightmost - leftmost) / 2
27596 - nrefx * width / 2
27597 + xoff);
27599 btm = ((grefy == 0 ? highest
27600 : grefy == 1 ? 0
27601 : grefy == 2 ? lowest
27602 : (highest + lowest) / 2)
27603 - (nrefy == 0 ? ascent + descent
27604 : nrefy == 1 ? descent - boff
27605 : nrefy == 2 ? 0
27606 : (ascent + descent) / 2)
27607 + yoff);
27610 cmp->offsets[i * 2] = left;
27611 cmp->offsets[i * 2 + 1] = btm + descent;
27613 /* Update the bounding box of the overall glyphs. */
27614 if (width > 0)
27616 right = left + width;
27617 if (left < leftmost)
27618 leftmost = left;
27619 if (right > rightmost)
27620 rightmost = right;
27622 top = btm + descent + ascent;
27623 if (top > highest)
27624 highest = top;
27625 if (btm < lowest)
27626 lowest = btm;
27628 if (cmp->lbearing > left + lbearing)
27629 cmp->lbearing = left + lbearing;
27630 if (cmp->rbearing < left + rbearing)
27631 cmp->rbearing = left + rbearing;
27635 /* If there are glyphs whose x-offsets are negative,
27636 shift all glyphs to the right and make all x-offsets
27637 non-negative. */
27638 if (leftmost < 0)
27640 for (i = 0; i < cmp->glyph_len; i++)
27641 cmp->offsets[i * 2] -= leftmost;
27642 rightmost -= leftmost;
27643 cmp->lbearing -= leftmost;
27644 cmp->rbearing -= leftmost;
27647 if (left_padded && cmp->lbearing < 0)
27649 for (i = 0; i < cmp->glyph_len; i++)
27650 cmp->offsets[i * 2] -= cmp->lbearing;
27651 rightmost -= cmp->lbearing;
27652 cmp->rbearing -= cmp->lbearing;
27653 cmp->lbearing = 0;
27655 if (right_padded && rightmost < cmp->rbearing)
27657 rightmost = cmp->rbearing;
27660 cmp->pixel_width = rightmost;
27661 cmp->ascent = highest;
27662 cmp->descent = - lowest;
27663 if (cmp->ascent < font_ascent)
27664 cmp->ascent = font_ascent;
27665 if (cmp->descent < font_descent)
27666 cmp->descent = font_descent;
27669 if (it->glyph_row
27670 && (cmp->lbearing < 0
27671 || cmp->rbearing > cmp->pixel_width))
27672 it->glyph_row->contains_overlapping_glyphs_p = true;
27674 it->pixel_width = cmp->pixel_width;
27675 it->ascent = it->phys_ascent = cmp->ascent;
27676 it->descent = it->phys_descent = cmp->descent;
27677 if (face->box != FACE_NO_BOX)
27679 int thick = face->box_line_width;
27681 if (thick > 0)
27683 it->ascent += thick;
27684 it->descent += thick;
27686 else
27687 thick = - thick;
27689 if (it->start_of_box_run_p)
27690 it->pixel_width += thick;
27691 if (it->end_of_box_run_p)
27692 it->pixel_width += thick;
27695 /* If face has an overline, add the height of the overline
27696 (1 pixel) and a 1 pixel margin to the character height. */
27697 if (face->overline_p)
27698 it->ascent += overline_margin;
27700 take_vertical_position_into_account (it);
27701 if (it->ascent < 0)
27702 it->ascent = 0;
27703 if (it->descent < 0)
27704 it->descent = 0;
27706 if (it->glyph_row && cmp->glyph_len > 0)
27707 append_composite_glyph (it);
27709 else if (it->what == IT_COMPOSITION)
27711 /* A dynamic (automatic) composition. */
27712 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27713 Lisp_Object gstring;
27714 struct font_metrics metrics;
27716 it->nglyphs = 1;
27718 gstring = composition_gstring_from_id (it->cmp_it.id);
27719 it->pixel_width
27720 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27721 &metrics);
27722 if (it->glyph_row
27723 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27724 it->glyph_row->contains_overlapping_glyphs_p = true;
27725 it->ascent = it->phys_ascent = metrics.ascent;
27726 it->descent = it->phys_descent = metrics.descent;
27727 if (face->box != FACE_NO_BOX)
27729 int thick = face->box_line_width;
27731 if (thick > 0)
27733 it->ascent += thick;
27734 it->descent += thick;
27736 else
27737 thick = - thick;
27739 if (it->start_of_box_run_p)
27740 it->pixel_width += thick;
27741 if (it->end_of_box_run_p)
27742 it->pixel_width += thick;
27744 /* If face has an overline, add the height of the overline
27745 (1 pixel) and a 1 pixel margin to the character height. */
27746 if (face->overline_p)
27747 it->ascent += overline_margin;
27748 take_vertical_position_into_account (it);
27749 if (it->ascent < 0)
27750 it->ascent = 0;
27751 if (it->descent < 0)
27752 it->descent = 0;
27754 if (it->glyph_row)
27755 append_composite_glyph (it);
27757 else if (it->what == IT_GLYPHLESS)
27758 produce_glyphless_glyph (it, false, Qnil);
27759 else if (it->what == IT_IMAGE)
27760 produce_image_glyph (it);
27761 else if (it->what == IT_STRETCH)
27762 produce_stretch_glyph (it);
27763 else if (it->what == IT_XWIDGET)
27764 produce_xwidget_glyph (it);
27766 done:
27767 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27768 because this isn't true for images with `:ascent 100'. */
27769 eassert (it->ascent >= 0 && it->descent >= 0);
27770 if (it->area == TEXT_AREA)
27771 it->current_x += it->pixel_width;
27773 if (extra_line_spacing > 0)
27775 it->descent += extra_line_spacing;
27776 if (extra_line_spacing > it->max_extra_line_spacing)
27777 it->max_extra_line_spacing = extra_line_spacing;
27780 it->max_ascent = max (it->max_ascent, it->ascent);
27781 it->max_descent = max (it->max_descent, it->descent);
27782 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27783 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27786 /* EXPORT for RIF:
27787 Output LEN glyphs starting at START at the nominal cursor position.
27788 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27789 being updated, and UPDATED_AREA is the area of that row being updated. */
27791 void
27792 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27793 struct glyph *start, enum glyph_row_area updated_area, int len)
27795 int x, hpos, chpos = w->phys_cursor.hpos;
27797 eassert (updated_row);
27798 /* When the window is hscrolled, cursor hpos can legitimately be out
27799 of bounds, but we draw the cursor at the corresponding window
27800 margin in that case. */
27801 if (!updated_row->reversed_p && chpos < 0)
27802 chpos = 0;
27803 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27804 chpos = updated_row->used[TEXT_AREA] - 1;
27806 block_input ();
27808 /* Write glyphs. */
27810 hpos = start - updated_row->glyphs[updated_area];
27811 x = draw_glyphs (w, w->output_cursor.x,
27812 updated_row, updated_area,
27813 hpos, hpos + len,
27814 DRAW_NORMAL_TEXT, 0);
27816 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27817 if (updated_area == TEXT_AREA
27818 && w->phys_cursor_on_p
27819 && w->phys_cursor.vpos == w->output_cursor.vpos
27820 && chpos >= hpos
27821 && chpos < hpos + len)
27822 w->phys_cursor_on_p = false;
27824 unblock_input ();
27826 /* Advance the output cursor. */
27827 w->output_cursor.hpos += len;
27828 w->output_cursor.x = x;
27832 /* EXPORT for RIF:
27833 Insert LEN glyphs from START at the nominal cursor position. */
27835 void
27836 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27837 struct glyph *start, enum glyph_row_area updated_area, int len)
27839 struct frame *f;
27840 int line_height, shift_by_width, shifted_region_width;
27841 struct glyph_row *row;
27842 struct glyph *glyph;
27843 int frame_x, frame_y;
27844 ptrdiff_t hpos;
27846 eassert (updated_row);
27847 block_input ();
27848 f = XFRAME (WINDOW_FRAME (w));
27850 /* Get the height of the line we are in. */
27851 row = updated_row;
27852 line_height = row->height;
27854 /* Get the width of the glyphs to insert. */
27855 shift_by_width = 0;
27856 for (glyph = start; glyph < start + len; ++glyph)
27857 shift_by_width += glyph->pixel_width;
27859 /* Get the width of the region to shift right. */
27860 shifted_region_width = (window_box_width (w, updated_area)
27861 - w->output_cursor.x
27862 - shift_by_width);
27864 /* Shift right. */
27865 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27866 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27868 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27869 line_height, shift_by_width);
27871 /* Write the glyphs. */
27872 hpos = start - row->glyphs[updated_area];
27873 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27874 hpos, hpos + len,
27875 DRAW_NORMAL_TEXT, 0);
27877 /* Advance the output cursor. */
27878 w->output_cursor.hpos += len;
27879 w->output_cursor.x += shift_by_width;
27880 unblock_input ();
27884 /* EXPORT for RIF:
27885 Erase the current text line from the nominal cursor position
27886 (inclusive) to pixel column TO_X (exclusive). The idea is that
27887 everything from TO_X onward is already erased.
27889 TO_X is a pixel position relative to UPDATED_AREA of currently
27890 updated window W. TO_X == -1 means clear to the end of this area. */
27892 void
27893 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27894 enum glyph_row_area updated_area, int to_x)
27896 struct frame *f;
27897 int max_x, min_y, max_y;
27898 int from_x, from_y, to_y;
27900 eassert (updated_row);
27901 f = XFRAME (w->frame);
27903 if (updated_row->full_width_p)
27904 max_x = (WINDOW_PIXEL_WIDTH (w)
27905 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27906 else
27907 max_x = window_box_width (w, updated_area);
27908 max_y = window_text_bottom_y (w);
27910 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27911 of window. For TO_X > 0, truncate to end of drawing area. */
27912 if (to_x == 0)
27913 return;
27914 else if (to_x < 0)
27915 to_x = max_x;
27916 else
27917 to_x = min (to_x, max_x);
27919 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27921 /* Notice if the cursor will be cleared by this operation. */
27922 if (!updated_row->full_width_p)
27923 notice_overwritten_cursor (w, updated_area,
27924 w->output_cursor.x, -1,
27925 updated_row->y,
27926 MATRIX_ROW_BOTTOM_Y (updated_row));
27928 from_x = w->output_cursor.x;
27930 /* Translate to frame coordinates. */
27931 if (updated_row->full_width_p)
27933 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27934 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27936 else
27938 int area_left = window_box_left (w, updated_area);
27939 from_x += area_left;
27940 to_x += area_left;
27943 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27944 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27945 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27947 /* Prevent inadvertently clearing to end of the X window. */
27948 if (to_x > from_x && to_y > from_y)
27950 block_input ();
27951 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27952 to_x - from_x, to_y - from_y);
27953 unblock_input ();
27957 #endif /* HAVE_WINDOW_SYSTEM */
27961 /***********************************************************************
27962 Cursor types
27963 ***********************************************************************/
27965 /* Value is the internal representation of the specified cursor type
27966 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27967 of the bar cursor. */
27969 static enum text_cursor_kinds
27970 get_specified_cursor_type (Lisp_Object arg, int *width)
27972 enum text_cursor_kinds type;
27974 if (NILP (arg))
27975 return NO_CURSOR;
27977 if (EQ (arg, Qbox))
27978 return FILLED_BOX_CURSOR;
27980 if (EQ (arg, Qhollow))
27981 return HOLLOW_BOX_CURSOR;
27983 if (EQ (arg, Qbar))
27985 *width = 2;
27986 return BAR_CURSOR;
27989 if (CONSP (arg)
27990 && EQ (XCAR (arg), Qbar)
27991 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27993 *width = XINT (XCDR (arg));
27994 return BAR_CURSOR;
27997 if (EQ (arg, Qhbar))
27999 *width = 2;
28000 return HBAR_CURSOR;
28003 if (CONSP (arg)
28004 && EQ (XCAR (arg), Qhbar)
28005 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28007 *width = XINT (XCDR (arg));
28008 return HBAR_CURSOR;
28011 /* Treat anything unknown as "hollow box cursor".
28012 It was bad to signal an error; people have trouble fixing
28013 .Xdefaults with Emacs, when it has something bad in it. */
28014 type = HOLLOW_BOX_CURSOR;
28016 return type;
28019 /* Set the default cursor types for specified frame. */
28020 void
28021 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28023 int width = 1;
28024 Lisp_Object tem;
28026 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28027 FRAME_CURSOR_WIDTH (f) = width;
28029 /* By default, set up the blink-off state depending on the on-state. */
28031 tem = Fassoc (arg, Vblink_cursor_alist);
28032 if (!NILP (tem))
28034 FRAME_BLINK_OFF_CURSOR (f)
28035 = get_specified_cursor_type (XCDR (tem), &width);
28036 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28038 else
28039 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28041 /* Make sure the cursor gets redrawn. */
28042 f->cursor_type_changed = true;
28046 #ifdef HAVE_WINDOW_SYSTEM
28048 /* Return the cursor we want to be displayed in window W. Return
28049 width of bar/hbar cursor through WIDTH arg. Return with
28050 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28051 (i.e. if the `system caret' should track this cursor).
28053 In a mini-buffer window, we want the cursor only to appear if we
28054 are reading input from this window. For the selected window, we
28055 want the cursor type given by the frame parameter or buffer local
28056 setting of cursor-type. If explicitly marked off, draw no cursor.
28057 In all other cases, we want a hollow box cursor. */
28059 static enum text_cursor_kinds
28060 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28061 bool *active_cursor)
28063 struct frame *f = XFRAME (w->frame);
28064 struct buffer *b = XBUFFER (w->contents);
28065 int cursor_type = DEFAULT_CURSOR;
28066 Lisp_Object alt_cursor;
28067 bool non_selected = false;
28069 *active_cursor = true;
28071 /* Echo area */
28072 if (cursor_in_echo_area
28073 && FRAME_HAS_MINIBUF_P (f)
28074 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28076 if (w == XWINDOW (echo_area_window))
28078 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28080 *width = FRAME_CURSOR_WIDTH (f);
28081 return FRAME_DESIRED_CURSOR (f);
28083 else
28084 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28087 *active_cursor = false;
28088 non_selected = true;
28091 /* Detect a nonselected window or nonselected frame. */
28092 else if (w != XWINDOW (f->selected_window)
28093 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28095 *active_cursor = false;
28097 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28098 return NO_CURSOR;
28100 non_selected = true;
28103 /* Never display a cursor in a window in which cursor-type is nil. */
28104 if (NILP (BVAR (b, cursor_type)))
28105 return NO_CURSOR;
28107 /* Get the normal cursor type for this window. */
28108 if (EQ (BVAR (b, cursor_type), Qt))
28110 cursor_type = FRAME_DESIRED_CURSOR (f);
28111 *width = FRAME_CURSOR_WIDTH (f);
28113 else
28114 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28116 /* Use cursor-in-non-selected-windows instead
28117 for non-selected window or frame. */
28118 if (non_selected)
28120 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28121 if (!EQ (Qt, alt_cursor))
28122 return get_specified_cursor_type (alt_cursor, width);
28123 /* t means modify the normal cursor type. */
28124 if (cursor_type == FILLED_BOX_CURSOR)
28125 cursor_type = HOLLOW_BOX_CURSOR;
28126 else if (cursor_type == BAR_CURSOR && *width > 1)
28127 --*width;
28128 return cursor_type;
28131 /* Use normal cursor if not blinked off. */
28132 if (!w->cursor_off_p)
28134 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28135 return NO_CURSOR;
28136 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28138 if (cursor_type == FILLED_BOX_CURSOR)
28140 /* Using a block cursor on large images can be very annoying.
28141 So use a hollow cursor for "large" images.
28142 If image is not transparent (no mask), also use hollow cursor. */
28143 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28144 if (img != NULL && IMAGEP (img->spec))
28146 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28147 where N = size of default frame font size.
28148 This should cover most of the "tiny" icons people may use. */
28149 if (!img->mask
28150 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28151 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28152 cursor_type = HOLLOW_BOX_CURSOR;
28155 else if (cursor_type != NO_CURSOR)
28157 /* Display current only supports BOX and HOLLOW cursors for images.
28158 So for now, unconditionally use a HOLLOW cursor when cursor is
28159 not a solid box cursor. */
28160 cursor_type = HOLLOW_BOX_CURSOR;
28163 return cursor_type;
28166 /* Cursor is blinked off, so determine how to "toggle" it. */
28168 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28169 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28170 return get_specified_cursor_type (XCDR (alt_cursor), width);
28172 /* Then see if frame has specified a specific blink off cursor type. */
28173 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28175 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28176 return FRAME_BLINK_OFF_CURSOR (f);
28179 #if false
28180 /* Some people liked having a permanently visible blinking cursor,
28181 while others had very strong opinions against it. So it was
28182 decided to remove it. KFS 2003-09-03 */
28184 /* Finally perform built-in cursor blinking:
28185 filled box <-> hollow box
28186 wide [h]bar <-> narrow [h]bar
28187 narrow [h]bar <-> no cursor
28188 other type <-> no cursor */
28190 if (cursor_type == FILLED_BOX_CURSOR)
28191 return HOLLOW_BOX_CURSOR;
28193 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28195 *width = 1;
28196 return cursor_type;
28198 #endif
28200 return NO_CURSOR;
28204 /* Notice when the text cursor of window W has been completely
28205 overwritten by a drawing operation that outputs glyphs in AREA
28206 starting at X0 and ending at X1 in the line starting at Y0 and
28207 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28208 the rest of the line after X0 has been written. Y coordinates
28209 are window-relative. */
28211 static void
28212 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28213 int x0, int x1, int y0, int y1)
28215 int cx0, cx1, cy0, cy1;
28216 struct glyph_row *row;
28218 if (!w->phys_cursor_on_p)
28219 return;
28220 if (area != TEXT_AREA)
28221 return;
28223 if (w->phys_cursor.vpos < 0
28224 || w->phys_cursor.vpos >= w->current_matrix->nrows
28225 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28226 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28227 return;
28229 if (row->cursor_in_fringe_p)
28231 row->cursor_in_fringe_p = false;
28232 draw_fringe_bitmap (w, row, row->reversed_p);
28233 w->phys_cursor_on_p = false;
28234 return;
28237 cx0 = w->phys_cursor.x;
28238 cx1 = cx0 + w->phys_cursor_width;
28239 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28240 return;
28242 /* The cursor image will be completely removed from the
28243 screen if the output area intersects the cursor area in
28244 y-direction. When we draw in [y0 y1[, and some part of
28245 the cursor is at y < y0, that part must have been drawn
28246 before. When scrolling, the cursor is erased before
28247 actually scrolling, so we don't come here. When not
28248 scrolling, the rows above the old cursor row must have
28249 changed, and in this case these rows must have written
28250 over the cursor image.
28252 Likewise if part of the cursor is below y1, with the
28253 exception of the cursor being in the first blank row at
28254 the buffer and window end because update_text_area
28255 doesn't draw that row. (Except when it does, but
28256 that's handled in update_text_area.) */
28258 cy0 = w->phys_cursor.y;
28259 cy1 = cy0 + w->phys_cursor_height;
28260 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28261 return;
28263 w->phys_cursor_on_p = false;
28266 #endif /* HAVE_WINDOW_SYSTEM */
28269 /************************************************************************
28270 Mouse Face
28271 ************************************************************************/
28273 #ifdef HAVE_WINDOW_SYSTEM
28275 /* EXPORT for RIF:
28276 Fix the display of area AREA of overlapping row ROW in window W
28277 with respect to the overlapping part OVERLAPS. */
28279 void
28280 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28281 enum glyph_row_area area, int overlaps)
28283 int i, x;
28285 block_input ();
28287 x = 0;
28288 for (i = 0; i < row->used[area];)
28290 if (row->glyphs[area][i].overlaps_vertically_p)
28292 int start = i, start_x = x;
28296 x += row->glyphs[area][i].pixel_width;
28297 ++i;
28299 while (i < row->used[area]
28300 && row->glyphs[area][i].overlaps_vertically_p);
28302 draw_glyphs (w, start_x, row, area,
28303 start, i,
28304 DRAW_NORMAL_TEXT, overlaps);
28306 else
28308 x += row->glyphs[area][i].pixel_width;
28309 ++i;
28313 unblock_input ();
28317 /* EXPORT:
28318 Draw the cursor glyph of window W in glyph row ROW. See the
28319 comment of draw_glyphs for the meaning of HL. */
28321 void
28322 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28323 enum draw_glyphs_face hl)
28325 /* If cursor hpos is out of bounds, don't draw garbage. This can
28326 happen in mini-buffer windows when switching between echo area
28327 glyphs and mini-buffer. */
28328 if ((row->reversed_p
28329 ? (w->phys_cursor.hpos >= 0)
28330 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28332 bool on_p = w->phys_cursor_on_p;
28333 int x1;
28334 int hpos = w->phys_cursor.hpos;
28336 /* When the window is hscrolled, cursor hpos can legitimately be
28337 out of bounds, but we draw the cursor at the corresponding
28338 window margin in that case. */
28339 if (!row->reversed_p && hpos < 0)
28340 hpos = 0;
28341 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28342 hpos = row->used[TEXT_AREA] - 1;
28344 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28345 hl, 0);
28346 w->phys_cursor_on_p = on_p;
28348 if (hl == DRAW_CURSOR)
28349 w->phys_cursor_width = x1 - w->phys_cursor.x;
28350 /* When we erase the cursor, and ROW is overlapped by other
28351 rows, make sure that these overlapping parts of other rows
28352 are redrawn. */
28353 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28355 w->phys_cursor_width = x1 - w->phys_cursor.x;
28357 if (row > w->current_matrix->rows
28358 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28359 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28360 OVERLAPS_ERASED_CURSOR);
28362 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28363 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28364 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28365 OVERLAPS_ERASED_CURSOR);
28371 /* Erase the image of a cursor of window W from the screen. */
28373 void
28374 erase_phys_cursor (struct window *w)
28376 struct frame *f = XFRAME (w->frame);
28377 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28378 int hpos = w->phys_cursor.hpos;
28379 int vpos = w->phys_cursor.vpos;
28380 bool mouse_face_here_p = false;
28381 struct glyph_matrix *active_glyphs = w->current_matrix;
28382 struct glyph_row *cursor_row;
28383 struct glyph *cursor_glyph;
28384 enum draw_glyphs_face hl;
28386 /* No cursor displayed or row invalidated => nothing to do on the
28387 screen. */
28388 if (w->phys_cursor_type == NO_CURSOR)
28389 goto mark_cursor_off;
28391 /* VPOS >= active_glyphs->nrows means that window has been resized.
28392 Don't bother to erase the cursor. */
28393 if (vpos >= active_glyphs->nrows)
28394 goto mark_cursor_off;
28396 /* If row containing cursor is marked invalid, there is nothing we
28397 can do. */
28398 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28399 if (!cursor_row->enabled_p)
28400 goto mark_cursor_off;
28402 /* If line spacing is > 0, old cursor may only be partially visible in
28403 window after split-window. So adjust visible height. */
28404 cursor_row->visible_height = min (cursor_row->visible_height,
28405 window_text_bottom_y (w) - cursor_row->y);
28407 /* If row is completely invisible, don't attempt to delete a cursor which
28408 isn't there. This can happen if cursor is at top of a window, and
28409 we switch to a buffer with a header line in that window. */
28410 if (cursor_row->visible_height <= 0)
28411 goto mark_cursor_off;
28413 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28414 if (cursor_row->cursor_in_fringe_p)
28416 cursor_row->cursor_in_fringe_p = false;
28417 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28418 goto mark_cursor_off;
28421 /* This can happen when the new row is shorter than the old one.
28422 In this case, either draw_glyphs or clear_end_of_line
28423 should have cleared the cursor. Note that we wouldn't be
28424 able to erase the cursor in this case because we don't have a
28425 cursor glyph at hand. */
28426 if ((cursor_row->reversed_p
28427 ? (w->phys_cursor.hpos < 0)
28428 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28429 goto mark_cursor_off;
28431 /* When the window is hscrolled, cursor hpos can legitimately be out
28432 of bounds, but we draw the cursor at the corresponding window
28433 margin in that case. */
28434 if (!cursor_row->reversed_p && hpos < 0)
28435 hpos = 0;
28436 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28437 hpos = cursor_row->used[TEXT_AREA] - 1;
28439 /* If the cursor is in the mouse face area, redisplay that when
28440 we clear the cursor. */
28441 if (! NILP (hlinfo->mouse_face_window)
28442 && coords_in_mouse_face_p (w, hpos, vpos)
28443 /* Don't redraw the cursor's spot in mouse face if it is at the
28444 end of a line (on a newline). The cursor appears there, but
28445 mouse highlighting does not. */
28446 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28447 mouse_face_here_p = true;
28449 /* Maybe clear the display under the cursor. */
28450 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28452 int x, y;
28453 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28454 int width;
28456 cursor_glyph = get_phys_cursor_glyph (w);
28457 if (cursor_glyph == NULL)
28458 goto mark_cursor_off;
28460 width = cursor_glyph->pixel_width;
28461 x = w->phys_cursor.x;
28462 if (x < 0)
28464 width += x;
28465 x = 0;
28467 width = min (width, window_box_width (w, TEXT_AREA) - x);
28468 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28469 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28471 if (width > 0)
28472 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28475 /* Erase the cursor by redrawing the character underneath it. */
28476 if (mouse_face_here_p)
28477 hl = DRAW_MOUSE_FACE;
28478 else
28479 hl = DRAW_NORMAL_TEXT;
28480 draw_phys_cursor_glyph (w, cursor_row, hl);
28482 mark_cursor_off:
28483 w->phys_cursor_on_p = false;
28484 w->phys_cursor_type = NO_CURSOR;
28488 /* Display or clear cursor of window W. If !ON, clear the cursor.
28489 If ON, display the cursor; where to put the cursor is specified by
28490 HPOS, VPOS, X and Y. */
28492 void
28493 display_and_set_cursor (struct window *w, bool on,
28494 int hpos, int vpos, int x, int y)
28496 struct frame *f = XFRAME (w->frame);
28497 int new_cursor_type;
28498 int new_cursor_width;
28499 bool active_cursor;
28500 struct glyph_row *glyph_row;
28501 struct glyph *glyph;
28503 /* This is pointless on invisible frames, and dangerous on garbaged
28504 windows and frames; in the latter case, the frame or window may
28505 be in the midst of changing its size, and x and y may be off the
28506 window. */
28507 if (! FRAME_VISIBLE_P (f)
28508 || FRAME_GARBAGED_P (f)
28509 || vpos >= w->current_matrix->nrows
28510 || hpos >= w->current_matrix->matrix_w)
28511 return;
28513 /* If cursor is off and we want it off, return quickly. */
28514 if (!on && !w->phys_cursor_on_p)
28515 return;
28517 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28518 /* If cursor row is not enabled, we don't really know where to
28519 display the cursor. */
28520 if (!glyph_row->enabled_p)
28522 w->phys_cursor_on_p = false;
28523 return;
28526 glyph = NULL;
28527 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28528 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28530 eassert (input_blocked_p ());
28532 /* Set new_cursor_type to the cursor we want to be displayed. */
28533 new_cursor_type = get_window_cursor_type (w, glyph,
28534 &new_cursor_width, &active_cursor);
28536 /* If cursor is currently being shown and we don't want it to be or
28537 it is in the wrong place, or the cursor type is not what we want,
28538 erase it. */
28539 if (w->phys_cursor_on_p
28540 && (!on
28541 || w->phys_cursor.x != x
28542 || w->phys_cursor.y != y
28543 /* HPOS can be negative in R2L rows whose
28544 exact_window_width_line_p flag is set (i.e. their newline
28545 would "overflow into the fringe"). */
28546 || hpos < 0
28547 || new_cursor_type != w->phys_cursor_type
28548 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28549 && new_cursor_width != w->phys_cursor_width)))
28550 erase_phys_cursor (w);
28552 /* Don't check phys_cursor_on_p here because that flag is only set
28553 to false in some cases where we know that the cursor has been
28554 completely erased, to avoid the extra work of erasing the cursor
28555 twice. In other words, phys_cursor_on_p can be true and the cursor
28556 still not be visible, or it has only been partly erased. */
28557 if (on)
28559 w->phys_cursor_ascent = glyph_row->ascent;
28560 w->phys_cursor_height = glyph_row->height;
28562 /* Set phys_cursor_.* before x_draw_.* is called because some
28563 of them may need the information. */
28564 w->phys_cursor.x = x;
28565 w->phys_cursor.y = glyph_row->y;
28566 w->phys_cursor.hpos = hpos;
28567 w->phys_cursor.vpos = vpos;
28570 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28571 new_cursor_type, new_cursor_width,
28572 on, active_cursor);
28576 /* Switch the display of W's cursor on or off, according to the value
28577 of ON. */
28579 static void
28580 update_window_cursor (struct window *w, bool on)
28582 /* Don't update cursor in windows whose frame is in the process
28583 of being deleted. */
28584 if (w->current_matrix)
28586 int hpos = w->phys_cursor.hpos;
28587 int vpos = w->phys_cursor.vpos;
28588 struct glyph_row *row;
28590 if (vpos >= w->current_matrix->nrows
28591 || hpos >= w->current_matrix->matrix_w)
28592 return;
28594 row = MATRIX_ROW (w->current_matrix, vpos);
28596 /* When the window is hscrolled, cursor hpos can legitimately be
28597 out of bounds, but we draw the cursor at the corresponding
28598 window margin in that case. */
28599 if (!row->reversed_p && hpos < 0)
28600 hpos = 0;
28601 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28602 hpos = row->used[TEXT_AREA] - 1;
28604 block_input ();
28605 display_and_set_cursor (w, on, hpos, vpos,
28606 w->phys_cursor.x, w->phys_cursor.y);
28607 unblock_input ();
28612 /* Call update_window_cursor with parameter ON_P on all leaf windows
28613 in the window tree rooted at W. */
28615 static void
28616 update_cursor_in_window_tree (struct window *w, bool on_p)
28618 while (w)
28620 if (WINDOWP (w->contents))
28621 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28622 else
28623 update_window_cursor (w, on_p);
28625 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28630 /* EXPORT:
28631 Display the cursor on window W, or clear it, according to ON_P.
28632 Don't change the cursor's position. */
28634 void
28635 x_update_cursor (struct frame *f, bool on_p)
28637 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28641 /* EXPORT:
28642 Clear the cursor of window W to background color, and mark the
28643 cursor as not shown. This is used when the text where the cursor
28644 is about to be rewritten. */
28646 void
28647 x_clear_cursor (struct window *w)
28649 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28650 update_window_cursor (w, false);
28653 #endif /* HAVE_WINDOW_SYSTEM */
28655 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28656 and MSDOS. */
28657 static void
28658 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28659 int start_hpos, int end_hpos,
28660 enum draw_glyphs_face draw)
28662 #ifdef HAVE_WINDOW_SYSTEM
28663 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28665 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28666 return;
28668 #endif
28669 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28670 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28671 #endif
28674 /* Display the active region described by mouse_face_* according to DRAW. */
28676 static void
28677 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28679 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28680 struct frame *f = XFRAME (WINDOW_FRAME (w));
28682 if (/* If window is in the process of being destroyed, don't bother
28683 to do anything. */
28684 w->current_matrix != NULL
28685 /* Don't update mouse highlight if hidden. */
28686 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28687 /* Recognize when we are called to operate on rows that don't exist
28688 anymore. This can happen when a window is split. */
28689 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28691 bool phys_cursor_on_p = w->phys_cursor_on_p;
28692 struct glyph_row *row, *first, *last;
28694 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28695 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28697 for (row = first; row <= last && row->enabled_p; ++row)
28699 int start_hpos, end_hpos, start_x;
28701 /* For all but the first row, the highlight starts at column 0. */
28702 if (row == first)
28704 /* R2L rows have BEG and END in reversed order, but the
28705 screen drawing geometry is always left to right. So
28706 we need to mirror the beginning and end of the
28707 highlighted area in R2L rows. */
28708 if (!row->reversed_p)
28710 start_hpos = hlinfo->mouse_face_beg_col;
28711 start_x = hlinfo->mouse_face_beg_x;
28713 else if (row == last)
28715 start_hpos = hlinfo->mouse_face_end_col;
28716 start_x = hlinfo->mouse_face_end_x;
28718 else
28720 start_hpos = 0;
28721 start_x = 0;
28724 else if (row->reversed_p && row == last)
28726 start_hpos = hlinfo->mouse_face_end_col;
28727 start_x = hlinfo->mouse_face_end_x;
28729 else
28731 start_hpos = 0;
28732 start_x = 0;
28735 if (row == last)
28737 if (!row->reversed_p)
28738 end_hpos = hlinfo->mouse_face_end_col;
28739 else if (row == first)
28740 end_hpos = hlinfo->mouse_face_beg_col;
28741 else
28743 end_hpos = row->used[TEXT_AREA];
28744 if (draw == DRAW_NORMAL_TEXT)
28745 row->fill_line_p = true; /* Clear to end of line. */
28748 else if (row->reversed_p && row == first)
28749 end_hpos = hlinfo->mouse_face_beg_col;
28750 else
28752 end_hpos = row->used[TEXT_AREA];
28753 if (draw == DRAW_NORMAL_TEXT)
28754 row->fill_line_p = true; /* Clear to end of line. */
28757 if (end_hpos > start_hpos)
28759 draw_row_with_mouse_face (w, start_x, row,
28760 start_hpos, end_hpos, draw);
28762 row->mouse_face_p
28763 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28767 /* When we've written over the cursor, arrange for it to
28768 be displayed again. */
28769 if (FRAME_WINDOW_P (f)
28770 && phys_cursor_on_p && !w->phys_cursor_on_p)
28772 #ifdef HAVE_WINDOW_SYSTEM
28773 int hpos = w->phys_cursor.hpos;
28775 /* When the window is hscrolled, cursor hpos can legitimately be
28776 out of bounds, but we draw the cursor at the corresponding
28777 window margin in that case. */
28778 if (!row->reversed_p && hpos < 0)
28779 hpos = 0;
28780 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28781 hpos = row->used[TEXT_AREA] - 1;
28783 block_input ();
28784 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28785 w->phys_cursor.x, w->phys_cursor.y);
28786 unblock_input ();
28787 #endif /* HAVE_WINDOW_SYSTEM */
28791 #ifdef HAVE_WINDOW_SYSTEM
28792 /* Change the mouse cursor. */
28793 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28795 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28796 if (draw == DRAW_NORMAL_TEXT
28797 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28798 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28799 else
28800 #endif
28801 if (draw == DRAW_MOUSE_FACE)
28802 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28803 else
28804 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28806 #endif /* HAVE_WINDOW_SYSTEM */
28809 /* EXPORT:
28810 Clear out the mouse-highlighted active region.
28811 Redraw it un-highlighted first. Value is true if mouse
28812 face was actually drawn unhighlighted. */
28814 bool
28815 clear_mouse_face (Mouse_HLInfo *hlinfo)
28817 bool cleared
28818 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28819 if (cleared)
28820 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28821 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28822 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28823 hlinfo->mouse_face_window = Qnil;
28824 hlinfo->mouse_face_overlay = Qnil;
28825 return cleared;
28828 /* Return true if the coordinates HPOS and VPOS on windows W are
28829 within the mouse face on that window. */
28830 static bool
28831 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28833 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28835 /* Quickly resolve the easy cases. */
28836 if (!(WINDOWP (hlinfo->mouse_face_window)
28837 && XWINDOW (hlinfo->mouse_face_window) == w))
28838 return false;
28839 if (vpos < hlinfo->mouse_face_beg_row
28840 || vpos > hlinfo->mouse_face_end_row)
28841 return false;
28842 if (vpos > hlinfo->mouse_face_beg_row
28843 && vpos < hlinfo->mouse_face_end_row)
28844 return true;
28846 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28848 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28850 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28851 return true;
28853 else if ((vpos == hlinfo->mouse_face_beg_row
28854 && hpos >= hlinfo->mouse_face_beg_col)
28855 || (vpos == hlinfo->mouse_face_end_row
28856 && hpos < hlinfo->mouse_face_end_col))
28857 return true;
28859 else
28861 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28863 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28864 return true;
28866 else if ((vpos == hlinfo->mouse_face_beg_row
28867 && hpos <= hlinfo->mouse_face_beg_col)
28868 || (vpos == hlinfo->mouse_face_end_row
28869 && hpos > hlinfo->mouse_face_end_col))
28870 return true;
28872 return false;
28876 /* EXPORT:
28877 True if physical cursor of window W is within mouse face. */
28879 bool
28880 cursor_in_mouse_face_p (struct window *w)
28882 int hpos = w->phys_cursor.hpos;
28883 int vpos = w->phys_cursor.vpos;
28884 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28886 /* When the window is hscrolled, cursor hpos can legitimately be out
28887 of bounds, but we draw the cursor at the corresponding window
28888 margin in that case. */
28889 if (!row->reversed_p && hpos < 0)
28890 hpos = 0;
28891 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28892 hpos = row->used[TEXT_AREA] - 1;
28894 return coords_in_mouse_face_p (w, hpos, vpos);
28899 /* Find the glyph rows START_ROW and END_ROW of window W that display
28900 characters between buffer positions START_CHARPOS and END_CHARPOS
28901 (excluding END_CHARPOS). DISP_STRING is a display string that
28902 covers these buffer positions. This is similar to
28903 row_containing_pos, but is more accurate when bidi reordering makes
28904 buffer positions change non-linearly with glyph rows. */
28905 static void
28906 rows_from_pos_range (struct window *w,
28907 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28908 Lisp_Object disp_string,
28909 struct glyph_row **start, struct glyph_row **end)
28911 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28912 int last_y = window_text_bottom_y (w);
28913 struct glyph_row *row;
28915 *start = NULL;
28916 *end = NULL;
28918 while (!first->enabled_p
28919 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28920 first++;
28922 /* Find the START row. */
28923 for (row = first;
28924 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28925 row++)
28927 /* A row can potentially be the START row if the range of the
28928 characters it displays intersects the range
28929 [START_CHARPOS..END_CHARPOS). */
28930 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28931 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28932 /* See the commentary in row_containing_pos, for the
28933 explanation of the complicated way to check whether
28934 some position is beyond the end of the characters
28935 displayed by a row. */
28936 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28937 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28938 && !row->ends_at_zv_p
28939 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28940 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28941 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28942 && !row->ends_at_zv_p
28943 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28945 /* Found a candidate row. Now make sure at least one of the
28946 glyphs it displays has a charpos from the range
28947 [START_CHARPOS..END_CHARPOS).
28949 This is not obvious because bidi reordering could make
28950 buffer positions of a row be 1,2,3,102,101,100, and if we
28951 want to highlight characters in [50..60), we don't want
28952 this row, even though [50..60) does intersect [1..103),
28953 the range of character positions given by the row's start
28954 and end positions. */
28955 struct glyph *g = row->glyphs[TEXT_AREA];
28956 struct glyph *e = g + row->used[TEXT_AREA];
28958 while (g < e)
28960 if (((BUFFERP (g->object) || NILP (g->object))
28961 && start_charpos <= g->charpos && g->charpos < end_charpos)
28962 /* A glyph that comes from DISP_STRING is by
28963 definition to be highlighted. */
28964 || EQ (g->object, disp_string))
28965 *start = row;
28966 g++;
28968 if (*start)
28969 break;
28973 /* Find the END row. */
28974 if (!*start
28975 /* If the last row is partially visible, start looking for END
28976 from that row, instead of starting from FIRST. */
28977 && !(row->enabled_p
28978 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28979 row = first;
28980 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28982 struct glyph_row *next = row + 1;
28983 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28985 if (!next->enabled_p
28986 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28987 /* The first row >= START whose range of displayed characters
28988 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28989 is the row END + 1. */
28990 || (start_charpos < next_start
28991 && end_charpos < next_start)
28992 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28993 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28994 && !next->ends_at_zv_p
28995 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28996 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28997 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28998 && !next->ends_at_zv_p
28999 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29001 *end = row;
29002 break;
29004 else
29006 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29007 but none of the characters it displays are in the range, it is
29008 also END + 1. */
29009 struct glyph *g = next->glyphs[TEXT_AREA];
29010 struct glyph *s = g;
29011 struct glyph *e = g + next->used[TEXT_AREA];
29013 while (g < e)
29015 if (((BUFFERP (g->object) || NILP (g->object))
29016 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29017 /* If the buffer position of the first glyph in
29018 the row is equal to END_CHARPOS, it means
29019 the last character to be highlighted is the
29020 newline of ROW, and we must consider NEXT as
29021 END, not END+1. */
29022 || (((!next->reversed_p && g == s)
29023 || (next->reversed_p && g == e - 1))
29024 && (g->charpos == end_charpos
29025 /* Special case for when NEXT is an
29026 empty line at ZV. */
29027 || (g->charpos == -1
29028 && !row->ends_at_zv_p
29029 && next_start == end_charpos)))))
29030 /* A glyph that comes from DISP_STRING is by
29031 definition to be highlighted. */
29032 || EQ (g->object, disp_string))
29033 break;
29034 g++;
29036 if (g == e)
29038 *end = row;
29039 break;
29041 /* The first row that ends at ZV must be the last to be
29042 highlighted. */
29043 else if (next->ends_at_zv_p)
29045 *end = next;
29046 break;
29052 /* This function sets the mouse_face_* elements of HLINFO, assuming
29053 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29054 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29055 for the overlay or run of text properties specifying the mouse
29056 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29057 before-string and after-string that must also be highlighted.
29058 DISP_STRING, if non-nil, is a display string that may cover some
29059 or all of the highlighted text. */
29061 static void
29062 mouse_face_from_buffer_pos (Lisp_Object window,
29063 Mouse_HLInfo *hlinfo,
29064 ptrdiff_t mouse_charpos,
29065 ptrdiff_t start_charpos,
29066 ptrdiff_t end_charpos,
29067 Lisp_Object before_string,
29068 Lisp_Object after_string,
29069 Lisp_Object disp_string)
29071 struct window *w = XWINDOW (window);
29072 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29073 struct glyph_row *r1, *r2;
29074 struct glyph *glyph, *end;
29075 ptrdiff_t ignore, pos;
29076 int x;
29078 eassert (NILP (disp_string) || STRINGP (disp_string));
29079 eassert (NILP (before_string) || STRINGP (before_string));
29080 eassert (NILP (after_string) || STRINGP (after_string));
29082 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29083 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29084 if (r1 == NULL)
29085 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29086 /* If the before-string or display-string contains newlines,
29087 rows_from_pos_range skips to its last row. Move back. */
29088 if (!NILP (before_string) || !NILP (disp_string))
29090 struct glyph_row *prev;
29091 while ((prev = r1 - 1, prev >= first)
29092 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29093 && prev->used[TEXT_AREA] > 0)
29095 struct glyph *beg = prev->glyphs[TEXT_AREA];
29096 glyph = beg + prev->used[TEXT_AREA];
29097 while (--glyph >= beg && NILP (glyph->object));
29098 if (glyph < beg
29099 || !(EQ (glyph->object, before_string)
29100 || EQ (glyph->object, disp_string)))
29101 break;
29102 r1 = prev;
29105 if (r2 == NULL)
29107 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29108 hlinfo->mouse_face_past_end = true;
29110 else if (!NILP (after_string))
29112 /* If the after-string has newlines, advance to its last row. */
29113 struct glyph_row *next;
29114 struct glyph_row *last
29115 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29117 for (next = r2 + 1;
29118 next <= last
29119 && next->used[TEXT_AREA] > 0
29120 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29121 ++next)
29122 r2 = next;
29124 /* The rest of the display engine assumes that mouse_face_beg_row is
29125 either above mouse_face_end_row or identical to it. But with
29126 bidi-reordered continued lines, the row for START_CHARPOS could
29127 be below the row for END_CHARPOS. If so, swap the rows and store
29128 them in correct order. */
29129 if (r1->y > r2->y)
29131 struct glyph_row *tem = r2;
29133 r2 = r1;
29134 r1 = tem;
29137 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29138 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29140 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29141 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29142 could be anywhere in the row and in any order. The strategy
29143 below is to find the leftmost and the rightmost glyph that
29144 belongs to either of these 3 strings, or whose position is
29145 between START_CHARPOS and END_CHARPOS, and highlight all the
29146 glyphs between those two. This may cover more than just the text
29147 between START_CHARPOS and END_CHARPOS if the range of characters
29148 strides the bidi level boundary, e.g. if the beginning is in R2L
29149 text while the end is in L2R text or vice versa. */
29150 if (!r1->reversed_p)
29152 /* This row is in a left to right paragraph. Scan it left to
29153 right. */
29154 glyph = r1->glyphs[TEXT_AREA];
29155 end = glyph + r1->used[TEXT_AREA];
29156 x = r1->x;
29158 /* Skip truncation glyphs at the start of the glyph row. */
29159 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29160 for (; glyph < end
29161 && NILP (glyph->object)
29162 && glyph->charpos < 0;
29163 ++glyph)
29164 x += glyph->pixel_width;
29166 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29167 or DISP_STRING, and the first glyph from buffer whose
29168 position is between START_CHARPOS and END_CHARPOS. */
29169 for (; glyph < end
29170 && !NILP (glyph->object)
29171 && !EQ (glyph->object, disp_string)
29172 && !(BUFFERP (glyph->object)
29173 && (glyph->charpos >= start_charpos
29174 && glyph->charpos < end_charpos));
29175 ++glyph)
29177 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29178 are present at buffer positions between START_CHARPOS and
29179 END_CHARPOS, or if they come from an overlay. */
29180 if (EQ (glyph->object, before_string))
29182 pos = string_buffer_position (before_string,
29183 start_charpos);
29184 /* If pos == 0, it means before_string came from an
29185 overlay, not from a buffer position. */
29186 if (!pos || (pos >= start_charpos && pos < end_charpos))
29187 break;
29189 else if (EQ (glyph->object, after_string))
29191 pos = string_buffer_position (after_string, end_charpos);
29192 if (!pos || (pos >= start_charpos && pos < end_charpos))
29193 break;
29195 x += glyph->pixel_width;
29197 hlinfo->mouse_face_beg_x = x;
29198 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29200 else
29202 /* This row is in a right to left paragraph. Scan it right to
29203 left. */
29204 struct glyph *g;
29206 end = r1->glyphs[TEXT_AREA] - 1;
29207 glyph = end + r1->used[TEXT_AREA];
29209 /* Skip truncation glyphs at the start of the glyph row. */
29210 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29211 for (; glyph > end
29212 && NILP (glyph->object)
29213 && glyph->charpos < 0;
29214 --glyph)
29217 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29218 or DISP_STRING, and the first glyph from buffer whose
29219 position is between START_CHARPOS and END_CHARPOS. */
29220 for (; glyph > end
29221 && !NILP (glyph->object)
29222 && !EQ (glyph->object, disp_string)
29223 && !(BUFFERP (glyph->object)
29224 && (glyph->charpos >= start_charpos
29225 && glyph->charpos < end_charpos));
29226 --glyph)
29228 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29229 are present at buffer positions between START_CHARPOS and
29230 END_CHARPOS, or if they come from an overlay. */
29231 if (EQ (glyph->object, before_string))
29233 pos = string_buffer_position (before_string, start_charpos);
29234 /* If pos == 0, it means before_string came from an
29235 overlay, not from a buffer position. */
29236 if (!pos || (pos >= start_charpos && pos < end_charpos))
29237 break;
29239 else if (EQ (glyph->object, after_string))
29241 pos = string_buffer_position (after_string, end_charpos);
29242 if (!pos || (pos >= start_charpos && pos < end_charpos))
29243 break;
29247 glyph++; /* first glyph to the right of the highlighted area */
29248 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29249 x += g->pixel_width;
29250 hlinfo->mouse_face_beg_x = x;
29251 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29254 /* If the highlight ends in a different row, compute GLYPH and END
29255 for the end row. Otherwise, reuse the values computed above for
29256 the row where the highlight begins. */
29257 if (r2 != r1)
29259 if (!r2->reversed_p)
29261 glyph = r2->glyphs[TEXT_AREA];
29262 end = glyph + r2->used[TEXT_AREA];
29263 x = r2->x;
29265 else
29267 end = r2->glyphs[TEXT_AREA] - 1;
29268 glyph = end + r2->used[TEXT_AREA];
29272 if (!r2->reversed_p)
29274 /* Skip truncation and continuation glyphs near the end of the
29275 row, and also blanks and stretch glyphs inserted by
29276 extend_face_to_end_of_line. */
29277 while (end > glyph
29278 && NILP ((end - 1)->object))
29279 --end;
29280 /* Scan the rest of the glyph row from the end, looking for the
29281 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29282 DISP_STRING, or whose position is between START_CHARPOS
29283 and END_CHARPOS */
29284 for (--end;
29285 end > glyph
29286 && !NILP (end->object)
29287 && !EQ (end->object, disp_string)
29288 && !(BUFFERP (end->object)
29289 && (end->charpos >= start_charpos
29290 && end->charpos < end_charpos));
29291 --end)
29293 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29294 are present at buffer positions between START_CHARPOS and
29295 END_CHARPOS, or if they come from an overlay. */
29296 if (EQ (end->object, before_string))
29298 pos = string_buffer_position (before_string, start_charpos);
29299 if (!pos || (pos >= start_charpos && pos < end_charpos))
29300 break;
29302 else if (EQ (end->object, after_string))
29304 pos = string_buffer_position (after_string, end_charpos);
29305 if (!pos || (pos >= start_charpos && pos < end_charpos))
29306 break;
29309 /* Find the X coordinate of the last glyph to be highlighted. */
29310 for (; glyph <= end; ++glyph)
29311 x += glyph->pixel_width;
29313 hlinfo->mouse_face_end_x = x;
29314 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29316 else
29318 /* Skip truncation and continuation glyphs near the end of the
29319 row, and also blanks and stretch glyphs inserted by
29320 extend_face_to_end_of_line. */
29321 x = r2->x;
29322 end++;
29323 while (end < glyph
29324 && NILP (end->object))
29326 x += end->pixel_width;
29327 ++end;
29329 /* Scan the rest of the glyph row from the end, looking for the
29330 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29331 DISP_STRING, or whose position is between START_CHARPOS
29332 and END_CHARPOS */
29333 for ( ;
29334 end < glyph
29335 && !NILP (end->object)
29336 && !EQ (end->object, disp_string)
29337 && !(BUFFERP (end->object)
29338 && (end->charpos >= start_charpos
29339 && end->charpos < end_charpos));
29340 ++end)
29342 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29343 are present at buffer positions between START_CHARPOS and
29344 END_CHARPOS, or if they come from an overlay. */
29345 if (EQ (end->object, before_string))
29347 pos = string_buffer_position (before_string, start_charpos);
29348 if (!pos || (pos >= start_charpos && pos < end_charpos))
29349 break;
29351 else if (EQ (end->object, after_string))
29353 pos = string_buffer_position (after_string, end_charpos);
29354 if (!pos || (pos >= start_charpos && pos < end_charpos))
29355 break;
29357 x += end->pixel_width;
29359 /* If we exited the above loop because we arrived at the last
29360 glyph of the row, and its buffer position is still not in
29361 range, it means the last character in range is the preceding
29362 newline. Bump the end column and x values to get past the
29363 last glyph. */
29364 if (end == glyph
29365 && BUFFERP (end->object)
29366 && (end->charpos < start_charpos
29367 || end->charpos >= end_charpos))
29369 x += end->pixel_width;
29370 ++end;
29372 hlinfo->mouse_face_end_x = x;
29373 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29376 hlinfo->mouse_face_window = window;
29377 hlinfo->mouse_face_face_id
29378 = face_at_buffer_position (w, mouse_charpos, &ignore,
29379 mouse_charpos + 1,
29380 !hlinfo->mouse_face_hidden, -1);
29381 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29384 /* The following function is not used anymore (replaced with
29385 mouse_face_from_string_pos), but I leave it here for the time
29386 being, in case someone would. */
29388 #if false /* not used */
29390 /* Find the position of the glyph for position POS in OBJECT in
29391 window W's current matrix, and return in *X, *Y the pixel
29392 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29394 RIGHT_P means return the position of the right edge of the glyph.
29395 !RIGHT_P means return the left edge position.
29397 If no glyph for POS exists in the matrix, return the position of
29398 the glyph with the next smaller position that is in the matrix, if
29399 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29400 exists in the matrix, return the position of the glyph with the
29401 next larger position in OBJECT.
29403 Value is true if a glyph was found. */
29405 static bool
29406 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29407 int *hpos, int *vpos, int *x, int *y, bool right_p)
29409 int yb = window_text_bottom_y (w);
29410 struct glyph_row *r;
29411 struct glyph *best_glyph = NULL;
29412 struct glyph_row *best_row = NULL;
29413 int best_x = 0;
29415 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29416 r->enabled_p && r->y < yb;
29417 ++r)
29419 struct glyph *g = r->glyphs[TEXT_AREA];
29420 struct glyph *e = g + r->used[TEXT_AREA];
29421 int gx;
29423 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29424 if (EQ (g->object, object))
29426 if (g->charpos == pos)
29428 best_glyph = g;
29429 best_x = gx;
29430 best_row = r;
29431 goto found;
29433 else if (best_glyph == NULL
29434 || ((eabs (g->charpos - pos)
29435 < eabs (best_glyph->charpos - pos))
29436 && (right_p
29437 ? g->charpos < pos
29438 : g->charpos > pos)))
29440 best_glyph = g;
29441 best_x = gx;
29442 best_row = r;
29447 found:
29449 if (best_glyph)
29451 *x = best_x;
29452 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29454 if (right_p)
29456 *x += best_glyph->pixel_width;
29457 ++*hpos;
29460 *y = best_row->y;
29461 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29464 return best_glyph != NULL;
29466 #endif /* not used */
29468 /* Find the positions of the first and the last glyphs in window W's
29469 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29470 (assumed to be a string), and return in HLINFO's mouse_face_*
29471 members the pixel and column/row coordinates of those glyphs. */
29473 static void
29474 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29475 Lisp_Object object,
29476 ptrdiff_t startpos, ptrdiff_t endpos)
29478 int yb = window_text_bottom_y (w);
29479 struct glyph_row *r;
29480 struct glyph *g, *e;
29481 int gx;
29482 bool found = false;
29484 /* Find the glyph row with at least one position in the range
29485 [STARTPOS..ENDPOS), and the first glyph in that row whose
29486 position belongs to that range. */
29487 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29488 r->enabled_p && r->y < yb;
29489 ++r)
29491 if (!r->reversed_p)
29493 g = r->glyphs[TEXT_AREA];
29494 e = g + r->used[TEXT_AREA];
29495 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29496 if (EQ (g->object, object)
29497 && startpos <= g->charpos && g->charpos < endpos)
29499 hlinfo->mouse_face_beg_row
29500 = MATRIX_ROW_VPOS (r, w->current_matrix);
29501 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29502 hlinfo->mouse_face_beg_x = gx;
29503 found = true;
29504 break;
29507 else
29509 struct glyph *g1;
29511 e = r->glyphs[TEXT_AREA];
29512 g = e + r->used[TEXT_AREA];
29513 for ( ; g > e; --g)
29514 if (EQ ((g-1)->object, object)
29515 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29517 hlinfo->mouse_face_beg_row
29518 = MATRIX_ROW_VPOS (r, w->current_matrix);
29519 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29520 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29521 gx += g1->pixel_width;
29522 hlinfo->mouse_face_beg_x = gx;
29523 found = true;
29524 break;
29527 if (found)
29528 break;
29531 if (!found)
29532 return;
29534 /* Starting with the next row, look for the first row which does NOT
29535 include any glyphs whose positions are in the range. */
29536 for (++r; r->enabled_p && r->y < yb; ++r)
29538 g = r->glyphs[TEXT_AREA];
29539 e = g + r->used[TEXT_AREA];
29540 found = false;
29541 for ( ; g < e; ++g)
29542 if (EQ (g->object, object)
29543 && startpos <= g->charpos && g->charpos < endpos)
29545 found = true;
29546 break;
29548 if (!found)
29549 break;
29552 /* The highlighted region ends on the previous row. */
29553 r--;
29555 /* Set the end row. */
29556 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29558 /* Compute and set the end column and the end column's horizontal
29559 pixel coordinate. */
29560 if (!r->reversed_p)
29562 g = r->glyphs[TEXT_AREA];
29563 e = g + r->used[TEXT_AREA];
29564 for ( ; e > g; --e)
29565 if (EQ ((e-1)->object, object)
29566 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29567 break;
29568 hlinfo->mouse_face_end_col = e - g;
29570 for (gx = r->x; g < e; ++g)
29571 gx += g->pixel_width;
29572 hlinfo->mouse_face_end_x = gx;
29574 else
29576 e = r->glyphs[TEXT_AREA];
29577 g = e + r->used[TEXT_AREA];
29578 for (gx = r->x ; e < g; ++e)
29580 if (EQ (e->object, object)
29581 && startpos <= e->charpos && e->charpos < endpos)
29582 break;
29583 gx += e->pixel_width;
29585 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29586 hlinfo->mouse_face_end_x = gx;
29590 #ifdef HAVE_WINDOW_SYSTEM
29592 /* See if position X, Y is within a hot-spot of an image. */
29594 static bool
29595 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29597 if (!CONSP (hot_spot))
29598 return false;
29600 if (EQ (XCAR (hot_spot), Qrect))
29602 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29603 Lisp_Object rect = XCDR (hot_spot);
29604 Lisp_Object tem;
29605 if (!CONSP (rect))
29606 return false;
29607 if (!CONSP (XCAR (rect)))
29608 return false;
29609 if (!CONSP (XCDR (rect)))
29610 return false;
29611 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29612 return false;
29613 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29614 return false;
29615 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29616 return false;
29617 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29618 return false;
29619 return true;
29621 else if (EQ (XCAR (hot_spot), Qcircle))
29623 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29624 Lisp_Object circ = XCDR (hot_spot);
29625 Lisp_Object lr, lx0, ly0;
29626 if (CONSP (circ)
29627 && CONSP (XCAR (circ))
29628 && (lr = XCDR (circ), NUMBERP (lr))
29629 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29630 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29632 double r = XFLOATINT (lr);
29633 double dx = XINT (lx0) - x;
29634 double dy = XINT (ly0) - y;
29635 return (dx * dx + dy * dy <= r * r);
29638 else if (EQ (XCAR (hot_spot), Qpoly))
29640 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29641 if (VECTORP (XCDR (hot_spot)))
29643 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29644 Lisp_Object *poly = v->contents;
29645 ptrdiff_t n = v->header.size;
29646 ptrdiff_t i;
29647 bool inside = false;
29648 Lisp_Object lx, ly;
29649 int x0, y0;
29651 /* Need an even number of coordinates, and at least 3 edges. */
29652 if (n < 6 || n & 1)
29653 return false;
29655 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29656 If count is odd, we are inside polygon. Pixels on edges
29657 may or may not be included depending on actual geometry of the
29658 polygon. */
29659 if ((lx = poly[n-2], !INTEGERP (lx))
29660 || (ly = poly[n-1], !INTEGERP (lx)))
29661 return false;
29662 x0 = XINT (lx), y0 = XINT (ly);
29663 for (i = 0; i < n; i += 2)
29665 int x1 = x0, y1 = y0;
29666 if ((lx = poly[i], !INTEGERP (lx))
29667 || (ly = poly[i+1], !INTEGERP (ly)))
29668 return false;
29669 x0 = XINT (lx), y0 = XINT (ly);
29671 /* Does this segment cross the X line? */
29672 if (x0 >= x)
29674 if (x1 >= x)
29675 continue;
29677 else if (x1 < x)
29678 continue;
29679 if (y > y0 && y > y1)
29680 continue;
29681 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29682 inside = !inside;
29684 return inside;
29687 return false;
29690 Lisp_Object
29691 find_hot_spot (Lisp_Object map, int x, int y)
29693 while (CONSP (map))
29695 if (CONSP (XCAR (map))
29696 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29697 return XCAR (map);
29698 map = XCDR (map);
29701 return Qnil;
29704 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29705 3, 3, 0,
29706 doc: /* Lookup in image map MAP coordinates X and Y.
29707 An image map is an alist where each element has the format (AREA ID PLIST).
29708 An AREA is specified as either a rectangle, a circle, or a polygon:
29709 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29710 pixel coordinates of the upper left and bottom right corners.
29711 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29712 and the radius of the circle; r may be a float or integer.
29713 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29714 vector describes one corner in the polygon.
29715 Returns the alist element for the first matching AREA in MAP. */)
29716 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29718 if (NILP (map))
29719 return Qnil;
29721 CHECK_NUMBER (x);
29722 CHECK_NUMBER (y);
29724 return find_hot_spot (map,
29725 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29726 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29728 #endif /* HAVE_WINDOW_SYSTEM */
29731 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29732 static void
29733 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29735 #ifdef HAVE_WINDOW_SYSTEM
29736 if (!FRAME_WINDOW_P (f))
29737 return;
29739 /* Do not change cursor shape while dragging mouse. */
29740 if (EQ (do_mouse_tracking, Qdragging))
29741 return;
29743 if (!NILP (pointer))
29745 if (EQ (pointer, Qarrow))
29746 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29747 else if (EQ (pointer, Qhand))
29748 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29749 else if (EQ (pointer, Qtext))
29750 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29751 else if (EQ (pointer, intern ("hdrag")))
29752 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29753 else if (EQ (pointer, intern ("nhdrag")))
29754 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29755 # ifdef HAVE_X_WINDOWS
29756 else if (EQ (pointer, intern ("vdrag")))
29757 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29758 # endif
29759 else if (EQ (pointer, intern ("hourglass")))
29760 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29761 else if (EQ (pointer, Qmodeline))
29762 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29763 else
29764 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29767 if (cursor != No_Cursor)
29768 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29769 #endif
29772 /* Take proper action when mouse has moved to the mode or header line
29773 or marginal area AREA of window W, x-position X and y-position Y.
29774 X is relative to the start of the text display area of W, so the
29775 width of bitmap areas and scroll bars must be subtracted to get a
29776 position relative to the start of the mode line. */
29778 static void
29779 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29780 enum window_part area)
29782 struct window *w = XWINDOW (window);
29783 struct frame *f = XFRAME (w->frame);
29784 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29785 #ifdef HAVE_WINDOW_SYSTEM
29786 Display_Info *dpyinfo;
29787 #endif
29788 Cursor cursor = No_Cursor;
29789 Lisp_Object pointer = Qnil;
29790 int dx, dy, width, height;
29791 ptrdiff_t charpos;
29792 Lisp_Object string, object = Qnil;
29793 Lisp_Object pos UNINIT;
29794 Lisp_Object mouse_face;
29795 int original_x_pixel = x;
29796 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29797 struct glyph_row *row UNINIT;
29799 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29801 int x0;
29802 struct glyph *end;
29804 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29805 returns them in row/column units! */
29806 string = mode_line_string (w, area, &x, &y, &charpos,
29807 &object, &dx, &dy, &width, &height);
29809 row = (area == ON_MODE_LINE
29810 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29811 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29813 /* Find the glyph under the mouse pointer. */
29814 if (row->mode_line_p && row->enabled_p)
29816 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29817 end = glyph + row->used[TEXT_AREA];
29819 for (x0 = original_x_pixel;
29820 glyph < end && x0 >= glyph->pixel_width;
29821 ++glyph)
29822 x0 -= glyph->pixel_width;
29824 if (glyph >= end)
29825 glyph = NULL;
29828 else
29830 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29831 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29832 returns them in row/column units! */
29833 string = marginal_area_string (w, area, &x, &y, &charpos,
29834 &object, &dx, &dy, &width, &height);
29837 Lisp_Object help = Qnil;
29839 #ifdef HAVE_WINDOW_SYSTEM
29840 if (IMAGEP (object))
29842 Lisp_Object image_map, hotspot;
29843 if ((image_map = Fplist_get (XCDR (object), QCmap),
29844 !NILP (image_map))
29845 && (hotspot = find_hot_spot (image_map, dx, dy),
29846 CONSP (hotspot))
29847 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29849 Lisp_Object plist;
29851 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29852 If so, we could look for mouse-enter, mouse-leave
29853 properties in PLIST (and do something...). */
29854 hotspot = XCDR (hotspot);
29855 if (CONSP (hotspot)
29856 && (plist = XCAR (hotspot), CONSP (plist)))
29858 pointer = Fplist_get (plist, Qpointer);
29859 if (NILP (pointer))
29860 pointer = Qhand;
29861 help = Fplist_get (plist, Qhelp_echo);
29862 if (!NILP (help))
29864 help_echo_string = help;
29865 XSETWINDOW (help_echo_window, w);
29866 help_echo_object = w->contents;
29867 help_echo_pos = charpos;
29871 if (NILP (pointer))
29872 pointer = Fplist_get (XCDR (object), QCpointer);
29874 #endif /* HAVE_WINDOW_SYSTEM */
29876 if (STRINGP (string))
29877 pos = make_number (charpos);
29879 /* Set the help text and mouse pointer. If the mouse is on a part
29880 of the mode line without any text (e.g. past the right edge of
29881 the mode line text), use the default help text and pointer. */
29882 if (STRINGP (string) || area == ON_MODE_LINE)
29884 /* Arrange to display the help by setting the global variables
29885 help_echo_string, help_echo_object, and help_echo_pos. */
29886 if (NILP (help))
29888 if (STRINGP (string))
29889 help = Fget_text_property (pos, Qhelp_echo, string);
29891 if (!NILP (help))
29893 help_echo_string = help;
29894 XSETWINDOW (help_echo_window, w);
29895 help_echo_object = string;
29896 help_echo_pos = charpos;
29898 else if (area == ON_MODE_LINE)
29900 Lisp_Object default_help
29901 = buffer_local_value (Qmode_line_default_help_echo,
29902 w->contents);
29904 if (STRINGP (default_help))
29906 help_echo_string = default_help;
29907 XSETWINDOW (help_echo_window, w);
29908 help_echo_object = Qnil;
29909 help_echo_pos = -1;
29914 #ifdef HAVE_WINDOW_SYSTEM
29915 /* Change the mouse pointer according to what is under it. */
29916 if (FRAME_WINDOW_P (f))
29918 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29919 || minibuf_level
29920 || NILP (Vresize_mini_windows));
29922 dpyinfo = FRAME_DISPLAY_INFO (f);
29923 if (STRINGP (string))
29925 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29927 if (NILP (pointer))
29928 pointer = Fget_text_property (pos, Qpointer, string);
29930 /* Change the mouse pointer according to what is under X/Y. */
29931 if (NILP (pointer)
29932 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29934 Lisp_Object map;
29935 map = Fget_text_property (pos, Qlocal_map, string);
29936 if (!KEYMAPP (map))
29937 map = Fget_text_property (pos, Qkeymap, string);
29938 if (!KEYMAPP (map) && draggable)
29939 cursor = dpyinfo->vertical_scroll_bar_cursor;
29942 else if (draggable)
29943 /* Default mode-line pointer. */
29944 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29946 #endif
29949 /* Change the mouse face according to what is under X/Y. */
29950 bool mouse_face_shown = false;
29951 if (STRINGP (string))
29953 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29954 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29955 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29956 && glyph)
29958 Lisp_Object b, e;
29960 struct glyph * tmp_glyph;
29962 int gpos;
29963 int gseq_length;
29964 int total_pixel_width;
29965 ptrdiff_t begpos, endpos, ignore;
29967 int vpos, hpos;
29969 b = Fprevious_single_property_change (make_number (charpos + 1),
29970 Qmouse_face, string, Qnil);
29971 if (NILP (b))
29972 begpos = 0;
29973 else
29974 begpos = XINT (b);
29976 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29977 if (NILP (e))
29978 endpos = SCHARS (string);
29979 else
29980 endpos = XINT (e);
29982 /* Calculate the glyph position GPOS of GLYPH in the
29983 displayed string, relative to the beginning of the
29984 highlighted part of the string.
29986 Note: GPOS is different from CHARPOS. CHARPOS is the
29987 position of GLYPH in the internal string object. A mode
29988 line string format has structures which are converted to
29989 a flattened string by the Emacs Lisp interpreter. The
29990 internal string is an element of those structures. The
29991 displayed string is the flattened string. */
29992 tmp_glyph = row_start_glyph;
29993 while (tmp_glyph < glyph
29994 && (!(EQ (tmp_glyph->object, glyph->object)
29995 && begpos <= tmp_glyph->charpos
29996 && tmp_glyph->charpos < endpos)))
29997 tmp_glyph++;
29998 gpos = glyph - tmp_glyph;
30000 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30001 the highlighted part of the displayed string to which
30002 GLYPH belongs. Note: GSEQ_LENGTH is different from
30003 SCHARS (STRING), because the latter returns the length of
30004 the internal string. */
30005 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30006 tmp_glyph > glyph
30007 && (!(EQ (tmp_glyph->object, glyph->object)
30008 && begpos <= tmp_glyph->charpos
30009 && tmp_glyph->charpos < endpos));
30010 tmp_glyph--)
30012 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30014 /* Calculate the total pixel width of all the glyphs between
30015 the beginning of the highlighted area and GLYPH. */
30016 total_pixel_width = 0;
30017 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30018 total_pixel_width += tmp_glyph->pixel_width;
30020 /* Pre calculation of re-rendering position. Note: X is in
30021 column units here, after the call to mode_line_string or
30022 marginal_area_string. */
30023 hpos = x - gpos;
30024 vpos = (area == ON_MODE_LINE
30025 ? (w->current_matrix)->nrows - 1
30026 : 0);
30028 /* If GLYPH's position is included in the region that is
30029 already drawn in mouse face, we have nothing to do. */
30030 if ( EQ (window, hlinfo->mouse_face_window)
30031 && (!row->reversed_p
30032 ? (hlinfo->mouse_face_beg_col <= hpos
30033 && hpos < hlinfo->mouse_face_end_col)
30034 /* In R2L rows we swap BEG and END, see below. */
30035 : (hlinfo->mouse_face_end_col <= hpos
30036 && hpos < hlinfo->mouse_face_beg_col))
30037 && hlinfo->mouse_face_beg_row == vpos )
30038 return;
30040 if (clear_mouse_face (hlinfo))
30041 cursor = No_Cursor;
30043 if (!row->reversed_p)
30045 hlinfo->mouse_face_beg_col = hpos;
30046 hlinfo->mouse_face_beg_x = original_x_pixel
30047 - (total_pixel_width + dx);
30048 hlinfo->mouse_face_end_col = hpos + gseq_length;
30049 hlinfo->mouse_face_end_x = 0;
30051 else
30053 /* In R2L rows, show_mouse_face expects BEG and END
30054 coordinates to be swapped. */
30055 hlinfo->mouse_face_end_col = hpos;
30056 hlinfo->mouse_face_end_x = original_x_pixel
30057 - (total_pixel_width + dx);
30058 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30059 hlinfo->mouse_face_beg_x = 0;
30062 hlinfo->mouse_face_beg_row = vpos;
30063 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30064 hlinfo->mouse_face_past_end = false;
30065 hlinfo->mouse_face_window = window;
30067 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30068 charpos,
30069 0, &ignore,
30070 glyph->face_id,
30071 true);
30072 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30073 mouse_face_shown = true;
30075 if (NILP (pointer))
30076 pointer = Qhand;
30080 /* If mouse-face doesn't need to be shown, clear any existing
30081 mouse-face. */
30082 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30083 clear_mouse_face (hlinfo);
30085 define_frame_cursor1 (f, cursor, pointer);
30089 /* EXPORT:
30090 Take proper action when the mouse has moved to position X, Y on
30091 frame F with regards to highlighting portions of display that have
30092 mouse-face properties. Also de-highlight portions of display where
30093 the mouse was before, set the mouse pointer shape as appropriate
30094 for the mouse coordinates, and activate help echo (tooltips).
30095 X and Y can be negative or out of range. */
30097 void
30098 note_mouse_highlight (struct frame *f, int x, int y)
30100 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30101 enum window_part part = ON_NOTHING;
30102 Lisp_Object window;
30103 struct window *w;
30104 Cursor cursor = No_Cursor;
30105 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30106 struct buffer *b;
30108 /* When a menu is active, don't highlight because this looks odd. */
30109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30110 if (popup_activated ())
30111 return;
30112 #endif
30114 if (!f->glyphs_initialized_p
30115 || f->pointer_invisible)
30116 return;
30118 hlinfo->mouse_face_mouse_x = x;
30119 hlinfo->mouse_face_mouse_y = y;
30120 hlinfo->mouse_face_mouse_frame = f;
30122 if (hlinfo->mouse_face_defer)
30123 return;
30125 /* Which window is that in? */
30126 window = window_from_coordinates (f, x, y, &part, true);
30128 /* If displaying active text in another window, clear that. */
30129 if (! EQ (window, hlinfo->mouse_face_window)
30130 /* Also clear if we move out of text area in same window. */
30131 || (!NILP (hlinfo->mouse_face_window)
30132 && !NILP (window)
30133 && part != ON_TEXT
30134 && part != ON_MODE_LINE
30135 && part != ON_HEADER_LINE))
30136 clear_mouse_face (hlinfo);
30138 /* Not on a window -> return. */
30139 if (!WINDOWP (window))
30140 return;
30142 /* Reset help_echo_string. It will get recomputed below. */
30143 help_echo_string = Qnil;
30145 /* Convert to window-relative pixel coordinates. */
30146 w = XWINDOW (window);
30147 frame_to_window_pixel_xy (w, &x, &y);
30149 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30150 /* Handle tool-bar window differently since it doesn't display a
30151 buffer. */
30152 if (EQ (window, f->tool_bar_window))
30154 note_tool_bar_highlight (f, x, y);
30155 return;
30157 #endif
30159 /* Mouse is on the mode, header line or margin? */
30160 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30161 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30163 note_mode_line_or_margin_highlight (window, x, y, part);
30165 #ifdef HAVE_WINDOW_SYSTEM
30166 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30168 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30169 /* Show non-text cursor (Bug#16647). */
30170 goto set_cursor;
30172 else
30173 #endif
30174 return;
30177 #ifdef HAVE_WINDOW_SYSTEM
30178 if (part == ON_VERTICAL_BORDER)
30180 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30181 help_echo_string = build_string ("drag-mouse-1: resize");
30183 else if (part == ON_RIGHT_DIVIDER)
30185 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30186 help_echo_string = build_string ("drag-mouse-1: resize");
30188 else if (part == ON_BOTTOM_DIVIDER)
30189 if (! WINDOW_BOTTOMMOST_P (w)
30190 || minibuf_level
30191 || NILP (Vresize_mini_windows))
30193 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30194 help_echo_string = build_string ("drag-mouse-1: resize");
30196 else
30197 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30198 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30199 || part == ON_VERTICAL_SCROLL_BAR
30200 || part == ON_HORIZONTAL_SCROLL_BAR)
30201 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30202 else
30203 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30204 #endif
30206 /* Are we in a window whose display is up to date?
30207 And verify the buffer's text has not changed. */
30208 b = XBUFFER (w->contents);
30209 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30211 int hpos, vpos, dx, dy, area = LAST_AREA;
30212 ptrdiff_t pos;
30213 struct glyph *glyph;
30214 Lisp_Object object;
30215 Lisp_Object mouse_face = Qnil, position;
30216 Lisp_Object *overlay_vec = NULL;
30217 ptrdiff_t i, noverlays;
30218 struct buffer *obuf;
30219 ptrdiff_t obegv, ozv;
30220 bool same_region;
30222 /* Find the glyph under X/Y. */
30223 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30225 #ifdef HAVE_WINDOW_SYSTEM
30226 /* Look for :pointer property on image. */
30227 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30229 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30230 if (img != NULL && IMAGEP (img->spec))
30232 Lisp_Object image_map, hotspot;
30233 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30234 !NILP (image_map))
30235 && (hotspot = find_hot_spot (image_map,
30236 glyph->slice.img.x + dx,
30237 glyph->slice.img.y + dy),
30238 CONSP (hotspot))
30239 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30241 Lisp_Object plist;
30243 /* Could check XCAR (hotspot) to see if we enter/leave
30244 this hot-spot.
30245 If so, we could look for mouse-enter, mouse-leave
30246 properties in PLIST (and do something...). */
30247 hotspot = XCDR (hotspot);
30248 if (CONSP (hotspot)
30249 && (plist = XCAR (hotspot), CONSP (plist)))
30251 pointer = Fplist_get (plist, Qpointer);
30252 if (NILP (pointer))
30253 pointer = Qhand;
30254 help_echo_string = Fplist_get (plist, Qhelp_echo);
30255 if (!NILP (help_echo_string))
30257 help_echo_window = window;
30258 help_echo_object = glyph->object;
30259 help_echo_pos = glyph->charpos;
30263 if (NILP (pointer))
30264 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30267 #endif /* HAVE_WINDOW_SYSTEM */
30269 /* Clear mouse face if X/Y not over text. */
30270 if (glyph == NULL
30271 || area != TEXT_AREA
30272 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30273 /* Glyph's OBJECT is nil for glyphs inserted by the
30274 display engine for its internal purposes, like truncation
30275 and continuation glyphs and blanks beyond the end of
30276 line's text on text terminals. If we are over such a
30277 glyph, we are not over any text. */
30278 || NILP (glyph->object)
30279 /* R2L rows have a stretch glyph at their front, which
30280 stands for no text, whereas L2R rows have no glyphs at
30281 all beyond the end of text. Treat such stretch glyphs
30282 like we do with NULL glyphs in L2R rows. */
30283 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30284 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30285 && glyph->type == STRETCH_GLYPH
30286 && glyph->avoid_cursor_p))
30288 if (clear_mouse_face (hlinfo))
30289 cursor = No_Cursor;
30290 if (FRAME_WINDOW_P (f) && NILP (pointer))
30292 #ifdef HAVE_WINDOW_SYSTEM
30293 if (area != TEXT_AREA)
30294 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30295 else
30296 pointer = Vvoid_text_area_pointer;
30297 #endif
30299 goto set_cursor;
30302 pos = glyph->charpos;
30303 object = glyph->object;
30304 if (!STRINGP (object) && !BUFFERP (object))
30305 goto set_cursor;
30307 /* If we get an out-of-range value, return now; avoid an error. */
30308 if (BUFFERP (object) && pos > BUF_Z (b))
30309 goto set_cursor;
30311 /* Make the window's buffer temporarily current for
30312 overlays_at and compute_char_face. */
30313 obuf = current_buffer;
30314 current_buffer = b;
30315 obegv = BEGV;
30316 ozv = ZV;
30317 BEGV = BEG;
30318 ZV = Z;
30320 /* Is this char mouse-active or does it have help-echo? */
30321 position = make_number (pos);
30323 USE_SAFE_ALLOCA;
30325 if (BUFFERP (object))
30327 /* Put all the overlays we want in a vector in overlay_vec. */
30328 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30329 /* Sort overlays into increasing priority order. */
30330 noverlays = sort_overlays (overlay_vec, noverlays, w);
30332 else
30333 noverlays = 0;
30335 if (NILP (Vmouse_highlight))
30337 clear_mouse_face (hlinfo);
30338 goto check_help_echo;
30341 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30343 if (same_region)
30344 cursor = No_Cursor;
30346 /* Check mouse-face highlighting. */
30347 if (! same_region
30348 /* If there exists an overlay with mouse-face overlapping
30349 the one we are currently highlighting, we have to
30350 check if we enter the overlapping overlay, and then
30351 highlight only that. */
30352 || (OVERLAYP (hlinfo->mouse_face_overlay)
30353 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30355 /* Find the highest priority overlay with a mouse-face. */
30356 Lisp_Object overlay = Qnil;
30357 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30359 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30360 if (!NILP (mouse_face))
30361 overlay = overlay_vec[i];
30364 /* If we're highlighting the same overlay as before, there's
30365 no need to do that again. */
30366 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30367 goto check_help_echo;
30368 hlinfo->mouse_face_overlay = overlay;
30370 /* Clear the display of the old active region, if any. */
30371 if (clear_mouse_face (hlinfo))
30372 cursor = No_Cursor;
30374 /* If no overlay applies, get a text property. */
30375 if (NILP (overlay))
30376 mouse_face = Fget_text_property (position, Qmouse_face, object);
30378 /* Next, compute the bounds of the mouse highlighting and
30379 display it. */
30380 if (!NILP (mouse_face) && STRINGP (object))
30382 /* The mouse-highlighting comes from a display string
30383 with a mouse-face. */
30384 Lisp_Object s, e;
30385 ptrdiff_t ignore;
30387 s = Fprevious_single_property_change
30388 (make_number (pos + 1), Qmouse_face, object, Qnil);
30389 e = Fnext_single_property_change
30390 (position, Qmouse_face, object, Qnil);
30391 if (NILP (s))
30392 s = make_number (0);
30393 if (NILP (e))
30394 e = make_number (SCHARS (object));
30395 mouse_face_from_string_pos (w, hlinfo, object,
30396 XINT (s), XINT (e));
30397 hlinfo->mouse_face_past_end = false;
30398 hlinfo->mouse_face_window = window;
30399 hlinfo->mouse_face_face_id
30400 = face_at_string_position (w, object, pos, 0, &ignore,
30401 glyph->face_id, true);
30402 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30403 cursor = No_Cursor;
30405 else
30407 /* The mouse-highlighting, if any, comes from an overlay
30408 or text property in the buffer. */
30409 Lisp_Object buffer UNINIT;
30410 Lisp_Object disp_string UNINIT;
30412 if (STRINGP (object))
30414 /* If we are on a display string with no mouse-face,
30415 check if the text under it has one. */
30416 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30417 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30418 pos = string_buffer_position (object, start);
30419 if (pos > 0)
30421 mouse_face = get_char_property_and_overlay
30422 (make_number (pos), Qmouse_face, w->contents, &overlay);
30423 buffer = w->contents;
30424 disp_string = object;
30427 else
30429 buffer = object;
30430 disp_string = Qnil;
30433 if (!NILP (mouse_face))
30435 Lisp_Object before, after;
30436 Lisp_Object before_string, after_string;
30437 /* To correctly find the limits of mouse highlight
30438 in a bidi-reordered buffer, we must not use the
30439 optimization of limiting the search in
30440 previous-single-property-change and
30441 next-single-property-change, because
30442 rows_from_pos_range needs the real start and end
30443 positions to DTRT in this case. That's because
30444 the first row visible in a window does not
30445 necessarily display the character whose position
30446 is the smallest. */
30447 Lisp_Object lim1
30448 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30449 ? Fmarker_position (w->start)
30450 : Qnil;
30451 Lisp_Object lim2
30452 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30453 ? make_number (BUF_Z (XBUFFER (buffer))
30454 - w->window_end_pos)
30455 : Qnil;
30457 if (NILP (overlay))
30459 /* Handle the text property case. */
30460 before = Fprevious_single_property_change
30461 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30462 after = Fnext_single_property_change
30463 (make_number (pos), Qmouse_face, buffer, lim2);
30464 before_string = after_string = Qnil;
30466 else
30468 /* Handle the overlay case. */
30469 before = Foverlay_start (overlay);
30470 after = Foverlay_end (overlay);
30471 before_string = Foverlay_get (overlay, Qbefore_string);
30472 after_string = Foverlay_get (overlay, Qafter_string);
30474 if (!STRINGP (before_string)) before_string = Qnil;
30475 if (!STRINGP (after_string)) after_string = Qnil;
30478 mouse_face_from_buffer_pos (window, hlinfo, pos,
30479 NILP (before)
30481 : XFASTINT (before),
30482 NILP (after)
30483 ? BUF_Z (XBUFFER (buffer))
30484 : XFASTINT (after),
30485 before_string, after_string,
30486 disp_string);
30487 cursor = No_Cursor;
30492 check_help_echo:
30494 /* Look for a `help-echo' property. */
30495 if (NILP (help_echo_string)) {
30496 Lisp_Object help, overlay;
30498 /* Check overlays first. */
30499 help = overlay = Qnil;
30500 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30502 overlay = overlay_vec[i];
30503 help = Foverlay_get (overlay, Qhelp_echo);
30506 if (!NILP (help))
30508 help_echo_string = help;
30509 help_echo_window = window;
30510 help_echo_object = overlay;
30511 help_echo_pos = pos;
30513 else
30515 Lisp_Object obj = glyph->object;
30516 ptrdiff_t charpos = glyph->charpos;
30518 /* Try text properties. */
30519 if (STRINGP (obj)
30520 && charpos >= 0
30521 && charpos < SCHARS (obj))
30523 help = Fget_text_property (make_number (charpos),
30524 Qhelp_echo, obj);
30525 if (NILP (help))
30527 /* If the string itself doesn't specify a help-echo,
30528 see if the buffer text ``under'' it does. */
30529 struct glyph_row *r
30530 = MATRIX_ROW (w->current_matrix, vpos);
30531 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30532 ptrdiff_t p = string_buffer_position (obj, start);
30533 if (p > 0)
30535 help = Fget_char_property (make_number (p),
30536 Qhelp_echo, w->contents);
30537 if (!NILP (help))
30539 charpos = p;
30540 obj = w->contents;
30545 else if (BUFFERP (obj)
30546 && charpos >= BEGV
30547 && charpos < ZV)
30548 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30549 obj);
30551 if (!NILP (help))
30553 help_echo_string = help;
30554 help_echo_window = window;
30555 help_echo_object = obj;
30556 help_echo_pos = charpos;
30561 #ifdef HAVE_WINDOW_SYSTEM
30562 /* Look for a `pointer' property. */
30563 if (FRAME_WINDOW_P (f) && NILP (pointer))
30565 /* Check overlays first. */
30566 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30567 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30569 if (NILP (pointer))
30571 Lisp_Object obj = glyph->object;
30572 ptrdiff_t charpos = glyph->charpos;
30574 /* Try text properties. */
30575 if (STRINGP (obj)
30576 && charpos >= 0
30577 && charpos < SCHARS (obj))
30579 pointer = Fget_text_property (make_number (charpos),
30580 Qpointer, obj);
30581 if (NILP (pointer))
30583 /* If the string itself doesn't specify a pointer,
30584 see if the buffer text ``under'' it does. */
30585 struct glyph_row *r
30586 = MATRIX_ROW (w->current_matrix, vpos);
30587 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30588 ptrdiff_t p = string_buffer_position (obj, start);
30589 if (p > 0)
30590 pointer = Fget_char_property (make_number (p),
30591 Qpointer, w->contents);
30594 else if (BUFFERP (obj)
30595 && charpos >= BEGV
30596 && charpos < ZV)
30597 pointer = Fget_text_property (make_number (charpos),
30598 Qpointer, obj);
30601 #endif /* HAVE_WINDOW_SYSTEM */
30603 BEGV = obegv;
30604 ZV = ozv;
30605 current_buffer = obuf;
30606 SAFE_FREE ();
30609 set_cursor:
30610 define_frame_cursor1 (f, cursor, pointer);
30614 /* EXPORT for RIF:
30615 Clear any mouse-face on window W. This function is part of the
30616 redisplay interface, and is called from try_window_id and similar
30617 functions to ensure the mouse-highlight is off. */
30619 void
30620 x_clear_window_mouse_face (struct window *w)
30622 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30623 Lisp_Object window;
30625 block_input ();
30626 XSETWINDOW (window, w);
30627 if (EQ (window, hlinfo->mouse_face_window))
30628 clear_mouse_face (hlinfo);
30629 unblock_input ();
30633 /* EXPORT:
30634 Just discard the mouse face information for frame F, if any.
30635 This is used when the size of F is changed. */
30637 void
30638 cancel_mouse_face (struct frame *f)
30640 Lisp_Object window;
30641 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30643 window = hlinfo->mouse_face_window;
30644 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30645 reset_mouse_highlight (hlinfo);
30650 /***********************************************************************
30651 Exposure Events
30652 ***********************************************************************/
30654 #ifdef HAVE_WINDOW_SYSTEM
30656 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30657 which intersects rectangle R. R is in window-relative coordinates. */
30659 static void
30660 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30661 enum glyph_row_area area)
30663 struct glyph *first = row->glyphs[area];
30664 struct glyph *end = row->glyphs[area] + row->used[area];
30665 struct glyph *last;
30666 int first_x, start_x, x;
30668 if (area == TEXT_AREA && row->fill_line_p)
30669 /* If row extends face to end of line write the whole line. */
30670 draw_glyphs (w, 0, row, area,
30671 0, row->used[area],
30672 DRAW_NORMAL_TEXT, 0);
30673 else
30675 /* Set START_X to the window-relative start position for drawing glyphs of
30676 AREA. The first glyph of the text area can be partially visible.
30677 The first glyphs of other areas cannot. */
30678 start_x = window_box_left_offset (w, area);
30679 x = start_x;
30680 if (area == TEXT_AREA)
30681 x += row->x;
30683 /* Find the first glyph that must be redrawn. */
30684 while (first < end
30685 && x + first->pixel_width < r->x)
30687 x += first->pixel_width;
30688 ++first;
30691 /* Find the last one. */
30692 last = first;
30693 first_x = x;
30694 /* Use a signed int intermediate value to avoid catastrophic
30695 failures due to comparison between signed and unsigned, when
30696 x is negative (can happen for wide images that are hscrolled). */
30697 int r_end = r->x + r->width;
30698 while (last < end && x < r_end)
30700 x += last->pixel_width;
30701 ++last;
30704 /* Repaint. */
30705 if (last > first)
30706 draw_glyphs (w, first_x - start_x, row, area,
30707 first - row->glyphs[area], last - row->glyphs[area],
30708 DRAW_NORMAL_TEXT, 0);
30713 /* Redraw the parts of the glyph row ROW on window W intersecting
30714 rectangle R. R is in window-relative coordinates. Value is
30715 true if mouse-face was overwritten. */
30717 static bool
30718 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30720 eassert (row->enabled_p);
30722 if (row->mode_line_p || w->pseudo_window_p)
30723 draw_glyphs (w, 0, row, TEXT_AREA,
30724 0, row->used[TEXT_AREA],
30725 DRAW_NORMAL_TEXT, 0);
30726 else
30728 if (row->used[LEFT_MARGIN_AREA])
30729 expose_area (w, row, r, LEFT_MARGIN_AREA);
30730 if (row->used[TEXT_AREA])
30731 expose_area (w, row, r, TEXT_AREA);
30732 if (row->used[RIGHT_MARGIN_AREA])
30733 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30734 draw_row_fringe_bitmaps (w, row);
30737 return row->mouse_face_p;
30741 /* Redraw those parts of glyphs rows during expose event handling that
30742 overlap other rows. Redrawing of an exposed line writes over parts
30743 of lines overlapping that exposed line; this function fixes that.
30745 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30746 row in W's current matrix that is exposed and overlaps other rows.
30747 LAST_OVERLAPPING_ROW is the last such row. */
30749 static void
30750 expose_overlaps (struct window *w,
30751 struct glyph_row *first_overlapping_row,
30752 struct glyph_row *last_overlapping_row,
30753 XRectangle *r)
30755 struct glyph_row *row;
30757 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30758 if (row->overlapping_p)
30760 eassert (row->enabled_p && !row->mode_line_p);
30762 row->clip = r;
30763 if (row->used[LEFT_MARGIN_AREA])
30764 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30766 if (row->used[TEXT_AREA])
30767 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30769 if (row->used[RIGHT_MARGIN_AREA])
30770 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30771 row->clip = NULL;
30776 /* Return true if W's cursor intersects rectangle R. */
30778 static bool
30779 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30781 XRectangle cr, result;
30782 struct glyph *cursor_glyph;
30783 struct glyph_row *row;
30785 if (w->phys_cursor.vpos >= 0
30786 && w->phys_cursor.vpos < w->current_matrix->nrows
30787 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30788 row->enabled_p)
30789 && row->cursor_in_fringe_p)
30791 /* Cursor is in the fringe. */
30792 cr.x = window_box_right_offset (w,
30793 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30794 ? RIGHT_MARGIN_AREA
30795 : TEXT_AREA));
30796 cr.y = row->y;
30797 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30798 cr.height = row->height;
30799 return x_intersect_rectangles (&cr, r, &result);
30802 cursor_glyph = get_phys_cursor_glyph (w);
30803 if (cursor_glyph)
30805 /* r is relative to W's box, but w->phys_cursor.x is relative
30806 to left edge of W's TEXT area. Adjust it. */
30807 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30808 cr.y = w->phys_cursor.y;
30809 cr.width = cursor_glyph->pixel_width;
30810 cr.height = w->phys_cursor_height;
30811 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30812 I assume the effect is the same -- and this is portable. */
30813 return x_intersect_rectangles (&cr, r, &result);
30815 /* If we don't understand the format, pretend we're not in the hot-spot. */
30816 return false;
30820 /* EXPORT:
30821 Draw a vertical window border to the right of window W if W doesn't
30822 have vertical scroll bars. */
30824 void
30825 x_draw_vertical_border (struct window *w)
30827 struct frame *f = XFRAME (WINDOW_FRAME (w));
30829 /* We could do better, if we knew what type of scroll-bar the adjacent
30830 windows (on either side) have... But we don't :-(
30831 However, I think this works ok. ++KFS 2003-04-25 */
30833 /* Redraw borders between horizontally adjacent windows. Don't
30834 do it for frames with vertical scroll bars because either the
30835 right scroll bar of a window, or the left scroll bar of its
30836 neighbor will suffice as a border. */
30837 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30838 return;
30840 /* Note: It is necessary to redraw both the left and the right
30841 borders, for when only this single window W is being
30842 redisplayed. */
30843 if (!WINDOW_RIGHTMOST_P (w)
30844 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30846 int x0, x1, y0, y1;
30848 window_box_edges (w, &x0, &y0, &x1, &y1);
30849 y1 -= 1;
30851 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30852 x1 -= 1;
30854 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30857 if (!WINDOW_LEFTMOST_P (w)
30858 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30860 int x0, x1, y0, y1;
30862 window_box_edges (w, &x0, &y0, &x1, &y1);
30863 y1 -= 1;
30865 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30866 x0 -= 1;
30868 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30873 /* Draw window dividers for window W. */
30875 void
30876 x_draw_right_divider (struct window *w)
30878 struct frame *f = WINDOW_XFRAME (w);
30880 if (w->mini || w->pseudo_window_p)
30881 return;
30882 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30884 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30885 int x1 = WINDOW_RIGHT_EDGE_X (w);
30886 int y0 = WINDOW_TOP_EDGE_Y (w);
30887 /* The bottom divider prevails. */
30888 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30890 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30894 static void
30895 x_draw_bottom_divider (struct window *w)
30897 struct frame *f = XFRAME (WINDOW_FRAME (w));
30899 if (w->mini || w->pseudo_window_p)
30900 return;
30901 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30903 int x0 = WINDOW_LEFT_EDGE_X (w);
30904 int x1 = WINDOW_RIGHT_EDGE_X (w);
30905 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30906 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30908 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30912 /* Redraw the part of window W intersection rectangle FR. Pixel
30913 coordinates in FR are frame-relative. Call this function with
30914 input blocked. Value is true if the exposure overwrites
30915 mouse-face. */
30917 static bool
30918 expose_window (struct window *w, XRectangle *fr)
30920 struct frame *f = XFRAME (w->frame);
30921 XRectangle wr, r;
30922 bool mouse_face_overwritten_p = false;
30924 /* If window is not yet fully initialized, do nothing. This can
30925 happen when toolkit scroll bars are used and a window is split.
30926 Reconfiguring the scroll bar will generate an expose for a newly
30927 created window. */
30928 if (w->current_matrix == NULL)
30929 return false;
30931 /* When we're currently updating the window, display and current
30932 matrix usually don't agree. Arrange for a thorough display
30933 later. */
30934 if (w->must_be_updated_p)
30936 SET_FRAME_GARBAGED (f);
30937 return false;
30940 /* Frame-relative pixel rectangle of W. */
30941 wr.x = WINDOW_LEFT_EDGE_X (w);
30942 wr.y = WINDOW_TOP_EDGE_Y (w);
30943 wr.width = WINDOW_PIXEL_WIDTH (w);
30944 wr.height = WINDOW_PIXEL_HEIGHT (w);
30946 if (x_intersect_rectangles (fr, &wr, &r))
30948 int yb = window_text_bottom_y (w);
30949 struct glyph_row *row;
30950 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30952 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30953 r.x, r.y, r.width, r.height));
30955 /* Convert to window coordinates. */
30956 r.x -= WINDOW_LEFT_EDGE_X (w);
30957 r.y -= WINDOW_TOP_EDGE_Y (w);
30959 /* Turn off the cursor. */
30960 bool cursor_cleared_p = (!w->pseudo_window_p
30961 && phys_cursor_in_rect_p (w, &r));
30962 if (cursor_cleared_p)
30963 x_clear_cursor (w);
30965 /* If the row containing the cursor extends face to end of line,
30966 then expose_area might overwrite the cursor outside the
30967 rectangle and thus notice_overwritten_cursor might clear
30968 w->phys_cursor_on_p. We remember the original value and
30969 check later if it is changed. */
30970 bool phys_cursor_on_p = w->phys_cursor_on_p;
30972 /* Use a signed int intermediate value to avoid catastrophic
30973 failures due to comparison between signed and unsigned, when
30974 y0 or y1 is negative (can happen for tall images). */
30975 int r_bottom = r.y + r.height;
30977 /* Update lines intersecting rectangle R. */
30978 first_overlapping_row = last_overlapping_row = NULL;
30979 for (row = w->current_matrix->rows;
30980 row->enabled_p;
30981 ++row)
30983 int y0 = row->y;
30984 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30986 if ((y0 >= r.y && y0 < r_bottom)
30987 || (y1 > r.y && y1 < r_bottom)
30988 || (r.y >= y0 && r.y < y1)
30989 || (r_bottom > y0 && r_bottom < y1))
30991 /* A header line may be overlapping, but there is no need
30992 to fix overlapping areas for them. KFS 2005-02-12 */
30993 if (row->overlapping_p && !row->mode_line_p)
30995 if (first_overlapping_row == NULL)
30996 first_overlapping_row = row;
30997 last_overlapping_row = row;
31000 row->clip = fr;
31001 if (expose_line (w, row, &r))
31002 mouse_face_overwritten_p = true;
31003 row->clip = NULL;
31005 else if (row->overlapping_p)
31007 /* We must redraw a row overlapping the exposed area. */
31008 if (y0 < r.y
31009 ? y0 + row->phys_height > r.y
31010 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31012 if (first_overlapping_row == NULL)
31013 first_overlapping_row = row;
31014 last_overlapping_row = row;
31018 if (y1 >= yb)
31019 break;
31022 /* Display the mode line if there is one. */
31023 if (WINDOW_WANTS_MODELINE_P (w)
31024 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31025 row->enabled_p)
31026 && row->y < r_bottom)
31028 if (expose_line (w, row, &r))
31029 mouse_face_overwritten_p = true;
31032 if (!w->pseudo_window_p)
31034 /* Fix the display of overlapping rows. */
31035 if (first_overlapping_row)
31036 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31037 fr);
31039 /* Draw border between windows. */
31040 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31041 x_draw_right_divider (w);
31042 else
31043 x_draw_vertical_border (w);
31045 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31046 x_draw_bottom_divider (w);
31048 /* Turn the cursor on again. */
31049 if (cursor_cleared_p
31050 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31051 update_window_cursor (w, true);
31055 return mouse_face_overwritten_p;
31060 /* Redraw (parts) of all windows in the window tree rooted at W that
31061 intersect R. R contains frame pixel coordinates. Value is
31062 true if the exposure overwrites mouse-face. */
31064 static bool
31065 expose_window_tree (struct window *w, XRectangle *r)
31067 struct frame *f = XFRAME (w->frame);
31068 bool mouse_face_overwritten_p = false;
31070 while (w && !FRAME_GARBAGED_P (f))
31072 mouse_face_overwritten_p
31073 |= (WINDOWP (w->contents)
31074 ? expose_window_tree (XWINDOW (w->contents), r)
31075 : expose_window (w, r));
31077 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31080 return mouse_face_overwritten_p;
31084 /* EXPORT:
31085 Redisplay an exposed area of frame F. X and Y are the upper-left
31086 corner of the exposed rectangle. W and H are width and height of
31087 the exposed area. All are pixel values. W or H zero means redraw
31088 the entire frame. */
31090 void
31091 expose_frame (struct frame *f, int x, int y, int w, int h)
31093 XRectangle r;
31094 bool mouse_face_overwritten_p = false;
31096 TRACE ((stderr, "expose_frame "));
31098 /* No need to redraw if frame will be redrawn soon. */
31099 if (FRAME_GARBAGED_P (f))
31101 TRACE ((stderr, " garbaged\n"));
31102 return;
31105 /* If basic faces haven't been realized yet, there is no point in
31106 trying to redraw anything. This can happen when we get an expose
31107 event while Emacs is starting, e.g. by moving another window. */
31108 if (FRAME_FACE_CACHE (f) == NULL
31109 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31111 TRACE ((stderr, " no faces\n"));
31112 return;
31115 if (w == 0 || h == 0)
31117 r.x = r.y = 0;
31118 r.width = FRAME_TEXT_WIDTH (f);
31119 r.height = FRAME_TEXT_HEIGHT (f);
31121 else
31123 r.x = x;
31124 r.y = y;
31125 r.width = w;
31126 r.height = h;
31129 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31130 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31132 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31133 if (WINDOWP (f->tool_bar_window))
31134 mouse_face_overwritten_p
31135 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31136 #endif
31138 #ifdef HAVE_X_WINDOWS
31139 #ifndef MSDOS
31140 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31141 if (WINDOWP (f->menu_bar_window))
31142 mouse_face_overwritten_p
31143 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31144 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31145 #endif
31146 #endif
31148 /* Some window managers support a focus-follows-mouse style with
31149 delayed raising of frames. Imagine a partially obscured frame,
31150 and moving the mouse into partially obscured mouse-face on that
31151 frame. The visible part of the mouse-face will be highlighted,
31152 then the WM raises the obscured frame. With at least one WM, KDE
31153 2.1, Emacs is not getting any event for the raising of the frame
31154 (even tried with SubstructureRedirectMask), only Expose events.
31155 These expose events will draw text normally, i.e. not
31156 highlighted. Which means we must redo the highlight here.
31157 Subsume it under ``we love X''. --gerd 2001-08-15 */
31158 /* Included in Windows version because Windows most likely does not
31159 do the right thing if any third party tool offers
31160 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31161 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31163 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31164 if (f == hlinfo->mouse_face_mouse_frame)
31166 int mouse_x = hlinfo->mouse_face_mouse_x;
31167 int mouse_y = hlinfo->mouse_face_mouse_y;
31168 clear_mouse_face (hlinfo);
31169 note_mouse_highlight (f, mouse_x, mouse_y);
31175 /* EXPORT:
31176 Determine the intersection of two rectangles R1 and R2. Return
31177 the intersection in *RESULT. Value is true if RESULT is not
31178 empty. */
31180 bool
31181 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31183 XRectangle *left, *right;
31184 XRectangle *upper, *lower;
31185 bool intersection_p = false;
31187 /* Rearrange so that R1 is the left-most rectangle. */
31188 if (r1->x < r2->x)
31189 left = r1, right = r2;
31190 else
31191 left = r2, right = r1;
31193 /* X0 of the intersection is right.x0, if this is inside R1,
31194 otherwise there is no intersection. */
31195 if (right->x <= left->x + left->width)
31197 result->x = right->x;
31199 /* The right end of the intersection is the minimum of
31200 the right ends of left and right. */
31201 result->width = (min (left->x + left->width, right->x + right->width)
31202 - result->x);
31204 /* Same game for Y. */
31205 if (r1->y < r2->y)
31206 upper = r1, lower = r2;
31207 else
31208 upper = r2, lower = r1;
31210 /* The upper end of the intersection is lower.y0, if this is inside
31211 of upper. Otherwise, there is no intersection. */
31212 if (lower->y <= upper->y + upper->height)
31214 result->y = lower->y;
31216 /* The lower end of the intersection is the minimum of the lower
31217 ends of upper and lower. */
31218 result->height = (min (lower->y + lower->height,
31219 upper->y + upper->height)
31220 - result->y);
31221 intersection_p = true;
31225 return intersection_p;
31228 #endif /* HAVE_WINDOW_SYSTEM */
31231 /***********************************************************************
31232 Initialization
31233 ***********************************************************************/
31235 void
31236 syms_of_xdisp (void)
31238 Vwith_echo_area_save_vector = Qnil;
31239 staticpro (&Vwith_echo_area_save_vector);
31241 Vmessage_stack = Qnil;
31242 staticpro (&Vmessage_stack);
31244 /* Non-nil means don't actually do any redisplay. */
31245 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31247 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31249 DEFVAR_BOOL("inhibit-message", inhibit_message,
31250 doc: /* Non-nil means calls to `message' are not displayed.
31251 They are still logged to the *Messages* buffer. */);
31252 inhibit_message = 0;
31254 message_dolog_marker1 = Fmake_marker ();
31255 staticpro (&message_dolog_marker1);
31256 message_dolog_marker2 = Fmake_marker ();
31257 staticpro (&message_dolog_marker2);
31258 message_dolog_marker3 = Fmake_marker ();
31259 staticpro (&message_dolog_marker3);
31261 #ifdef GLYPH_DEBUG
31262 defsubr (&Sdump_frame_glyph_matrix);
31263 defsubr (&Sdump_glyph_matrix);
31264 defsubr (&Sdump_glyph_row);
31265 defsubr (&Sdump_tool_bar_row);
31266 defsubr (&Strace_redisplay);
31267 defsubr (&Strace_to_stderr);
31268 #endif
31269 #ifdef HAVE_WINDOW_SYSTEM
31270 defsubr (&Stool_bar_height);
31271 defsubr (&Slookup_image_map);
31272 #endif
31273 defsubr (&Sline_pixel_height);
31274 defsubr (&Sformat_mode_line);
31275 defsubr (&Sinvisible_p);
31276 defsubr (&Scurrent_bidi_paragraph_direction);
31277 defsubr (&Swindow_text_pixel_size);
31278 defsubr (&Smove_point_visually);
31279 defsubr (&Sbidi_find_overridden_directionality);
31281 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31282 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31283 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31284 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31285 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31286 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31287 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31288 DEFSYM (Qeval, "eval");
31289 DEFSYM (QCdata, ":data");
31291 /* Names of text properties relevant for redisplay. */
31292 DEFSYM (Qdisplay, "display");
31293 DEFSYM (Qspace_width, "space-width");
31294 DEFSYM (Qraise, "raise");
31295 DEFSYM (Qslice, "slice");
31296 DEFSYM (Qspace, "space");
31297 DEFSYM (Qmargin, "margin");
31298 DEFSYM (Qpointer, "pointer");
31299 DEFSYM (Qleft_margin, "left-margin");
31300 DEFSYM (Qright_margin, "right-margin");
31301 DEFSYM (Qcenter, "center");
31302 DEFSYM (Qline_height, "line-height");
31303 DEFSYM (QCalign_to, ":align-to");
31304 DEFSYM (QCrelative_width, ":relative-width");
31305 DEFSYM (QCrelative_height, ":relative-height");
31306 DEFSYM (QCeval, ":eval");
31307 DEFSYM (QCpropertize, ":propertize");
31308 DEFSYM (QCfile, ":file");
31309 DEFSYM (Qfontified, "fontified");
31310 DEFSYM (Qfontification_functions, "fontification-functions");
31312 /* Name of the face used to highlight trailing whitespace. */
31313 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31315 /* Name and number of the face used to highlight escape glyphs. */
31316 DEFSYM (Qescape_glyph, "escape-glyph");
31318 /* Name and number of the face used to highlight non-breaking
31319 spaces/hyphens. */
31320 DEFSYM (Qnobreak_space, "nobreak-space");
31321 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31323 /* The symbol 'image' which is the car of the lists used to represent
31324 images in Lisp. Also a tool bar style. */
31325 DEFSYM (Qimage, "image");
31327 /* Tool bar styles. */
31328 DEFSYM (Qtext, "text");
31329 DEFSYM (Qboth, "both");
31330 DEFSYM (Qboth_horiz, "both-horiz");
31331 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31333 /* The image map types. */
31334 DEFSYM (QCmap, ":map");
31335 DEFSYM (QCpointer, ":pointer");
31336 DEFSYM (Qrect, "rect");
31337 DEFSYM (Qcircle, "circle");
31338 DEFSYM (Qpoly, "poly");
31340 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31342 DEFSYM (Qgrow_only, "grow-only");
31343 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31344 DEFSYM (Qposition, "position");
31345 DEFSYM (Qbuffer_position, "buffer-position");
31346 DEFSYM (Qobject, "object");
31348 /* Cursor shapes. */
31349 DEFSYM (Qbar, "bar");
31350 DEFSYM (Qhbar, "hbar");
31351 DEFSYM (Qbox, "box");
31352 DEFSYM (Qhollow, "hollow");
31354 /* Pointer shapes. */
31355 DEFSYM (Qhand, "hand");
31356 DEFSYM (Qarrow, "arrow");
31357 /* also Qtext */
31359 DEFSYM (Qdragging, "dragging");
31361 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31363 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31364 staticpro (&list_of_error);
31366 /* Values of those variables at last redisplay are stored as
31367 properties on 'overlay-arrow-position' symbol. However, if
31368 Voverlay_arrow_position is a marker, last-arrow-position is its
31369 numerical position. */
31370 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31371 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31373 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31374 properties on a symbol in overlay-arrow-variable-list. */
31375 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31376 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31378 echo_buffer[0] = echo_buffer[1] = Qnil;
31379 staticpro (&echo_buffer[0]);
31380 staticpro (&echo_buffer[1]);
31382 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31383 staticpro (&echo_area_buffer[0]);
31384 staticpro (&echo_area_buffer[1]);
31386 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31387 staticpro (&Vmessages_buffer_name);
31389 mode_line_proptrans_alist = Qnil;
31390 staticpro (&mode_line_proptrans_alist);
31391 mode_line_string_list = Qnil;
31392 staticpro (&mode_line_string_list);
31393 mode_line_string_face = Qnil;
31394 staticpro (&mode_line_string_face);
31395 mode_line_string_face_prop = Qnil;
31396 staticpro (&mode_line_string_face_prop);
31397 Vmode_line_unwind_vector = Qnil;
31398 staticpro (&Vmode_line_unwind_vector);
31400 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31402 help_echo_string = Qnil;
31403 staticpro (&help_echo_string);
31404 help_echo_object = Qnil;
31405 staticpro (&help_echo_object);
31406 help_echo_window = Qnil;
31407 staticpro (&help_echo_window);
31408 previous_help_echo_string = Qnil;
31409 staticpro (&previous_help_echo_string);
31410 help_echo_pos = -1;
31412 DEFSYM (Qright_to_left, "right-to-left");
31413 DEFSYM (Qleft_to_right, "left-to-right");
31414 defsubr (&Sbidi_resolved_levels);
31416 #ifdef HAVE_WINDOW_SYSTEM
31417 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31418 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31419 For example, if a block cursor is over a tab, it will be drawn as
31420 wide as that tab on the display. */);
31421 x_stretch_cursor_p = 0;
31422 #endif
31424 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31425 doc: /* Non-nil means highlight trailing whitespace.
31426 The face used for trailing whitespace is `trailing-whitespace'. */);
31427 Vshow_trailing_whitespace = Qnil;
31429 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31430 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31431 If the value is t, Emacs highlights non-ASCII chars which have the
31432 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31433 or `nobreak-hyphen' face respectively.
31435 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31436 U+2011 (non-breaking hyphen) are affected.
31438 Any other non-nil value means to display these characters as a escape
31439 glyph followed by an ordinary space or hyphen.
31441 A value of nil means no special handling of these characters. */);
31442 Vnobreak_char_display = Qt;
31444 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31445 doc: /* The pointer shape to show in void text areas.
31446 A value of nil means to show the text pointer. Other options are
31447 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31448 `hourglass'. */);
31449 Vvoid_text_area_pointer = Qarrow;
31451 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31452 doc: /* Non-nil means don't actually do any redisplay.
31453 This is used for internal purposes. */);
31454 Vinhibit_redisplay = Qnil;
31456 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31457 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31458 Vglobal_mode_string = Qnil;
31460 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31461 doc: /* Marker for where to display an arrow on top of the buffer text.
31462 This must be the beginning of a line in order to work.
31463 See also `overlay-arrow-string'. */);
31464 Voverlay_arrow_position = Qnil;
31466 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31467 doc: /* String to display as an arrow in non-window frames.
31468 See also `overlay-arrow-position'. */);
31469 Voverlay_arrow_string = build_pure_c_string ("=>");
31471 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31472 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31473 The symbols on this list are examined during redisplay to determine
31474 where to display overlay arrows. */);
31475 Voverlay_arrow_variable_list
31476 = list1 (intern_c_string ("overlay-arrow-position"));
31478 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31479 doc: /* The number of lines to try scrolling a window by when point moves out.
31480 If that fails to bring point back on frame, point is centered instead.
31481 If this is zero, point is always centered after it moves off frame.
31482 If you want scrolling to always be a line at a time, you should set
31483 `scroll-conservatively' to a large value rather than set this to 1. */);
31485 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31486 doc: /* Scroll up to this many lines, to bring point back on screen.
31487 If point moves off-screen, redisplay will scroll by up to
31488 `scroll-conservatively' lines in order to bring point just barely
31489 onto the screen again. If that cannot be done, then redisplay
31490 recenters point as usual.
31492 If the value is greater than 100, redisplay will never recenter point,
31493 but will always scroll just enough text to bring point into view, even
31494 if you move far away.
31496 A value of zero means always recenter point if it moves off screen. */);
31497 scroll_conservatively = 0;
31499 DEFVAR_INT ("scroll-margin", scroll_margin,
31500 doc: /* Number of lines of margin at the top and bottom of a window.
31501 Recenter the window whenever point gets within this many lines
31502 of the top or bottom of the window. */);
31503 scroll_margin = 0;
31505 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31506 doc: /* Pixels per inch value for non-window system displays.
31507 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31508 Vdisplay_pixels_per_inch = make_float (72.0);
31510 #ifdef GLYPH_DEBUG
31511 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31512 #endif
31514 DEFVAR_LISP ("truncate-partial-width-windows",
31515 Vtruncate_partial_width_windows,
31516 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31517 For an integer value, truncate lines in each window narrower than the
31518 full frame width, provided the total window width in column units is less
31519 than that integer; otherwise, respect the value of `truncate-lines'.
31520 The total width of the window is as returned by `window-total-width', it
31521 includes the fringes, the continuation and truncation glyphs, the
31522 display margins (if any), and the scroll bar
31524 For any other non-nil value, truncate lines in all windows that do
31525 not span the full frame width.
31527 A value of nil means to respect the value of `truncate-lines'.
31529 If `word-wrap' is enabled, you might want to reduce this. */);
31530 Vtruncate_partial_width_windows = make_number (50);
31532 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31533 doc: /* Maximum buffer size for which line number should be displayed.
31534 If the buffer is bigger than this, the line number does not appear
31535 in the mode line. A value of nil means no limit. */);
31536 Vline_number_display_limit = Qnil;
31538 DEFVAR_INT ("line-number-display-limit-width",
31539 line_number_display_limit_width,
31540 doc: /* Maximum line width (in characters) for line number display.
31541 If the average length of the lines near point is bigger than this, then the
31542 line number may be omitted from the mode line. */);
31543 line_number_display_limit_width = 200;
31545 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31546 doc: /* Non-nil means highlight region even in nonselected windows. */);
31547 highlight_nonselected_windows = false;
31549 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31550 doc: /* Non-nil if more than one frame is visible on this display.
31551 Minibuffer-only frames don't count, but iconified frames do.
31552 This variable is not guaranteed to be accurate except while processing
31553 `frame-title-format' and `icon-title-format'. */);
31555 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31556 doc: /* Template for displaying the title bar of visible frames.
31557 \(Assuming the window manager supports this feature.)
31559 This variable has the same structure as `mode-line-format', except that
31560 the %c and %l constructs are ignored. It is used only on frames for
31561 which no explicit name has been set (see `modify-frame-parameters'). */);
31563 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31564 doc: /* Template for displaying the title bar of an iconified frame.
31565 \(Assuming the window manager supports this feature.)
31566 This variable has the same structure as `mode-line-format' (which see),
31567 and is used only on frames for which no explicit name has been set
31568 \(see `modify-frame-parameters'). */);
31569 Vicon_title_format
31570 = Vframe_title_format
31571 = listn (CONSTYPE_PURE, 3,
31572 intern_c_string ("multiple-frames"),
31573 build_pure_c_string ("%b"),
31574 listn (CONSTYPE_PURE, 4,
31575 empty_unibyte_string,
31576 intern_c_string ("invocation-name"),
31577 build_pure_c_string ("@"),
31578 intern_c_string ("system-name")));
31580 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31581 doc: /* Maximum number of lines to keep in the message log buffer.
31582 If nil, disable message logging. If t, log messages but don't truncate
31583 the buffer when it becomes large. */);
31584 Vmessage_log_max = make_number (1000);
31586 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31587 doc: /* List of functions to call before redisplaying a window with scrolling.
31588 Each function is called with two arguments, the window and its new
31589 display-start position.
31590 These functions are called whenever the `window-start' marker is modified,
31591 either to point into another buffer (e.g. via `set-window-buffer') or another
31592 place in the same buffer.
31593 Note that the value of `window-end' is not valid when these functions are
31594 called.
31596 Warning: Do not use this feature to alter the way the window
31597 is scrolled. It is not designed for that, and such use probably won't
31598 work. */);
31599 Vwindow_scroll_functions = Qnil;
31601 DEFVAR_LISP ("window-text-change-functions",
31602 Vwindow_text_change_functions,
31603 doc: /* Functions to call in redisplay when text in the window might change. */);
31604 Vwindow_text_change_functions = Qnil;
31606 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31607 doc: /* Functions called when redisplay of a window reaches the end trigger.
31608 Each function is called with two arguments, the window and the end trigger value.
31609 See `set-window-redisplay-end-trigger'. */);
31610 Vredisplay_end_trigger_functions = Qnil;
31612 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31613 doc: /* Non-nil means autoselect window with mouse pointer.
31614 If nil, do not autoselect windows.
31615 A positive number means delay autoselection by that many seconds: a
31616 window is autoselected only after the mouse has remained in that
31617 window for the duration of the delay.
31618 A negative number has a similar effect, but causes windows to be
31619 autoselected only after the mouse has stopped moving. (Because of
31620 the way Emacs compares mouse events, you will occasionally wait twice
31621 that time before the window gets selected.)
31622 Any other value means to autoselect window instantaneously when the
31623 mouse pointer enters it.
31625 Autoselection selects the minibuffer only if it is active, and never
31626 unselects the minibuffer if it is active.
31628 When customizing this variable make sure that the actual value of
31629 `focus-follows-mouse' matches the behavior of your window manager. */);
31630 Vmouse_autoselect_window = Qnil;
31632 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31633 doc: /* Non-nil means automatically resize tool-bars.
31634 This dynamically changes the tool-bar's height to the minimum height
31635 that is needed to make all tool-bar items visible.
31636 If value is `grow-only', the tool-bar's height is only increased
31637 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31638 Vauto_resize_tool_bars = Qt;
31640 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31641 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31642 auto_raise_tool_bar_buttons_p = true;
31644 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31645 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31646 make_cursor_line_fully_visible_p = true;
31648 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31649 doc: /* Border below tool-bar in pixels.
31650 If an integer, use it as the height of the border.
31651 If it is one of `internal-border-width' or `border-width', use the
31652 value of the corresponding frame parameter.
31653 Otherwise, no border is added below the tool-bar. */);
31654 Vtool_bar_border = Qinternal_border_width;
31656 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31657 doc: /* Margin around tool-bar buttons in pixels.
31658 If an integer, use that for both horizontal and vertical margins.
31659 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31660 HORZ specifying the horizontal margin, and VERT specifying the
31661 vertical margin. */);
31662 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31664 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31665 doc: /* Relief thickness of tool-bar buttons. */);
31666 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31668 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31669 doc: /* Tool bar style to use.
31670 It can be one of
31671 image - show images only
31672 text - show text only
31673 both - show both, text below image
31674 both-horiz - show text to the right of the image
31675 text-image-horiz - show text to the left of the image
31676 any other - use system default or image if no system default.
31678 This variable only affects the GTK+ toolkit version of Emacs. */);
31679 Vtool_bar_style = Qnil;
31681 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31682 doc: /* Maximum number of characters a label can have to be shown.
31683 The tool bar style must also show labels for this to have any effect, see
31684 `tool-bar-style'. */);
31685 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31687 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31688 doc: /* List of functions to call to fontify regions of text.
31689 Each function is called with one argument POS. Functions must
31690 fontify a region starting at POS in the current buffer, and give
31691 fontified regions the property `fontified'. */);
31692 Vfontification_functions = Qnil;
31693 Fmake_variable_buffer_local (Qfontification_functions);
31695 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31696 unibyte_display_via_language_environment,
31697 doc: /* Non-nil means display unibyte text according to language environment.
31698 Specifically, this means that raw bytes in the range 160-255 decimal
31699 are displayed by converting them to the equivalent multibyte characters
31700 according to the current language environment. As a result, they are
31701 displayed according to the current fontset.
31703 Note that this variable affects only how these bytes are displayed,
31704 but does not change the fact they are interpreted as raw bytes. */);
31705 unibyte_display_via_language_environment = false;
31707 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31708 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31709 If a float, it specifies a fraction of the mini-window frame's height.
31710 If an integer, it specifies a number of lines. */);
31711 Vmax_mini_window_height = make_float (0.25);
31713 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31714 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31715 A value of nil means don't automatically resize mini-windows.
31716 A value of t means resize them to fit the text displayed in them.
31717 A value of `grow-only', the default, means let mini-windows grow only;
31718 they return to their normal size when the minibuffer is closed, or the
31719 echo area becomes empty. */);
31720 /* Contrary to the doc string, we initialize this to nil, so that
31721 loading loadup.el won't try to resize windows before loading
31722 window.el, where some functions we need to call for this live.
31723 We assign the 'grow-only' value right after loading window.el
31724 during loadup. */
31725 Vresize_mini_windows = Qnil;
31727 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31728 doc: /* Alist specifying how to blink the cursor off.
31729 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31730 `cursor-type' frame-parameter or variable equals ON-STATE,
31731 comparing using `equal', Emacs uses OFF-STATE to specify
31732 how to blink it off. ON-STATE and OFF-STATE are values for
31733 the `cursor-type' frame parameter.
31735 If a frame's ON-STATE has no entry in this list,
31736 the frame's other specifications determine how to blink the cursor off. */);
31737 Vblink_cursor_alist = Qnil;
31739 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31740 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31741 If non-nil, windows are automatically scrolled horizontally to make
31742 point visible. */);
31743 automatic_hscrolling_p = true;
31744 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31746 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31747 doc: /* How many columns away from the window edge point is allowed to get
31748 before automatic hscrolling will horizontally scroll the window. */);
31749 hscroll_margin = 5;
31751 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31752 doc: /* How many columns to scroll the window when point gets too close to the edge.
31753 When point is less than `hscroll-margin' columns from the window
31754 edge, automatic hscrolling will scroll the window by the amount of columns
31755 determined by this variable. If its value is a positive integer, scroll that
31756 many columns. If it's a positive floating-point number, it specifies the
31757 fraction of the window's width to scroll. If it's nil or zero, point will be
31758 centered horizontally after the scroll. Any other value, including negative
31759 numbers, are treated as if the value were zero.
31761 Automatic hscrolling always moves point outside the scroll margin, so if
31762 point was more than scroll step columns inside the margin, the window will
31763 scroll more than the value given by the scroll step.
31765 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31766 and `scroll-right' overrides this variable's effect. */);
31767 Vhscroll_step = make_number (0);
31769 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31770 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31771 Bind this around calls to `message' to let it take effect. */);
31772 message_truncate_lines = false;
31774 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31775 doc: /* Normal hook run to update the menu bar definitions.
31776 Redisplay runs this hook before it redisplays the menu bar.
31777 This is used to update menus such as Buffers, whose contents depend on
31778 various data. */);
31779 Vmenu_bar_update_hook = Qnil;
31781 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31782 doc: /* Frame for which we are updating a menu.
31783 The enable predicate for a menu binding should check this variable. */);
31784 Vmenu_updating_frame = Qnil;
31786 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31787 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31788 inhibit_menubar_update = false;
31790 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31791 doc: /* Prefix prepended to all continuation lines at display time.
31792 The value may be a string, an image, or a stretch-glyph; it is
31793 interpreted in the same way as the value of a `display' text property.
31795 This variable is overridden by any `wrap-prefix' text or overlay
31796 property.
31798 To add a prefix to non-continuation lines, use `line-prefix'. */);
31799 Vwrap_prefix = Qnil;
31800 DEFSYM (Qwrap_prefix, "wrap-prefix");
31801 Fmake_variable_buffer_local (Qwrap_prefix);
31803 DEFVAR_LISP ("line-prefix", Vline_prefix,
31804 doc: /* Prefix prepended to all non-continuation lines at display time.
31805 The value may be a string, an image, or a stretch-glyph; it is
31806 interpreted in the same way as the value of a `display' text property.
31808 This variable is overridden by any `line-prefix' text or overlay
31809 property.
31811 To add a prefix to continuation lines, use `wrap-prefix'. */);
31812 Vline_prefix = Qnil;
31813 DEFSYM (Qline_prefix, "line-prefix");
31814 Fmake_variable_buffer_local (Qline_prefix);
31816 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31817 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31818 inhibit_eval_during_redisplay = false;
31820 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31821 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31822 inhibit_free_realized_faces = false;
31824 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31825 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31826 Intended for use during debugging and for testing bidi display;
31827 see biditest.el in the test suite. */);
31828 inhibit_bidi_mirroring = false;
31830 #ifdef GLYPH_DEBUG
31831 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31832 doc: /* Inhibit try_window_id display optimization. */);
31833 inhibit_try_window_id = false;
31835 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31836 doc: /* Inhibit try_window_reusing display optimization. */);
31837 inhibit_try_window_reusing = false;
31839 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31840 doc: /* Inhibit try_cursor_movement display optimization. */);
31841 inhibit_try_cursor_movement = false;
31842 #endif /* GLYPH_DEBUG */
31844 DEFVAR_INT ("overline-margin", overline_margin,
31845 doc: /* Space between overline and text, in pixels.
31846 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31847 margin to the character height. */);
31848 overline_margin = 2;
31850 DEFVAR_INT ("underline-minimum-offset",
31851 underline_minimum_offset,
31852 doc: /* Minimum distance between baseline and underline.
31853 This can improve legibility of underlined text at small font sizes,
31854 particularly when using variable `x-use-underline-position-properties'
31855 with fonts that specify an UNDERLINE_POSITION relatively close to the
31856 baseline. The default value is 1. */);
31857 underline_minimum_offset = 1;
31859 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31860 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31861 This feature only works when on a window system that can change
31862 cursor shapes. */);
31863 display_hourglass_p = true;
31865 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31866 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31867 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31869 #ifdef HAVE_WINDOW_SYSTEM
31870 hourglass_atimer = NULL;
31871 hourglass_shown_p = false;
31872 #endif /* HAVE_WINDOW_SYSTEM */
31874 /* Name of the face used to display glyphless characters. */
31875 DEFSYM (Qglyphless_char, "glyphless-char");
31877 /* Method symbols for Vglyphless_char_display. */
31878 DEFSYM (Qhex_code, "hex-code");
31879 DEFSYM (Qempty_box, "empty-box");
31880 DEFSYM (Qthin_space, "thin-space");
31881 DEFSYM (Qzero_width, "zero-width");
31883 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31884 doc: /* Function run just before redisplay.
31885 It is called with one argument, which is the set of windows that are to
31886 be redisplayed. This set can be nil (meaning, only the selected window),
31887 or t (meaning all windows). */);
31888 Vpre_redisplay_function = intern ("ignore");
31890 /* Symbol for the purpose of Vglyphless_char_display. */
31891 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31892 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31894 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31895 doc: /* Char-table defining glyphless characters.
31896 Each element, if non-nil, should be one of the following:
31897 an ASCII acronym string: display this string in a box
31898 `hex-code': display the hexadecimal code of a character in a box
31899 `empty-box': display as an empty box
31900 `thin-space': display as 1-pixel width space
31901 `zero-width': don't display
31902 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31903 display method for graphical terminals and text terminals respectively.
31904 GRAPHICAL and TEXT should each have one of the values listed above.
31906 The char-table has one extra slot to control the display of a character for
31907 which no font is found. This slot only takes effect on graphical terminals.
31908 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31909 `thin-space'. The default is `empty-box'.
31911 If a character has a non-nil entry in an active display table, the
31912 display table takes effect; in this case, Emacs does not consult
31913 `glyphless-char-display' at all. */);
31914 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31915 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31916 Qempty_box);
31918 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31919 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31920 Vdebug_on_message = Qnil;
31922 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31923 doc: /* */);
31924 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31926 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31927 doc: /* */);
31928 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31930 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31931 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31932 Vredisplay__variables = Qnil;
31934 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31935 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31936 /* Initialize to t, since we need to disable reordering until
31937 loadup.el successfully loads charprop.el. */
31938 redisplay__inhibit_bidi = true;
31942 /* Initialize this module when Emacs starts. */
31944 void
31945 init_xdisp (void)
31947 CHARPOS (this_line_start_pos) = 0;
31949 if (!noninteractive)
31951 struct window *m = XWINDOW (minibuf_window);
31952 Lisp_Object frame = m->frame;
31953 struct frame *f = XFRAME (frame);
31954 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31955 struct window *r = XWINDOW (root);
31956 int i;
31958 echo_area_window = minibuf_window;
31960 r->top_line = FRAME_TOP_MARGIN (f);
31961 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31962 r->total_cols = FRAME_COLS (f);
31963 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31964 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31965 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31967 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31968 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31969 m->total_cols = FRAME_COLS (f);
31970 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31971 m->total_lines = 1;
31972 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31974 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31975 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31976 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31978 /* The default ellipsis glyphs `...'. */
31979 for (i = 0; i < 3; ++i)
31980 default_invis_vector[i] = make_number ('.');
31984 /* Allocate the buffer for frame titles.
31985 Also used for `format-mode-line'. */
31986 int size = 100;
31987 mode_line_noprop_buf = xmalloc (size);
31988 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31989 mode_line_noprop_ptr = mode_line_noprop_buf;
31990 mode_line_target = MODE_LINE_DISPLAY;
31993 help_echo_showing_p = false;
31996 #ifdef HAVE_WINDOW_SYSTEM
31998 /* Platform-independent portion of hourglass implementation. */
32000 /* Timer function of hourglass_atimer. */
32002 static void
32003 show_hourglass (struct atimer *timer)
32005 /* The timer implementation will cancel this timer automatically
32006 after this function has run. Set hourglass_atimer to null
32007 so that we know the timer doesn't have to be canceled. */
32008 hourglass_atimer = NULL;
32010 if (!hourglass_shown_p)
32012 Lisp_Object tail, frame;
32014 block_input ();
32016 FOR_EACH_FRAME (tail, frame)
32018 struct frame *f = XFRAME (frame);
32020 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32021 && FRAME_RIF (f)->show_hourglass)
32022 FRAME_RIF (f)->show_hourglass (f);
32025 hourglass_shown_p = true;
32026 unblock_input ();
32030 /* Cancel a currently active hourglass timer, and start a new one. */
32032 void
32033 start_hourglass (void)
32035 struct timespec delay;
32037 cancel_hourglass ();
32039 if (INTEGERP (Vhourglass_delay)
32040 && XINT (Vhourglass_delay) > 0)
32041 delay = make_timespec (min (XINT (Vhourglass_delay),
32042 TYPE_MAXIMUM (time_t)),
32044 else if (FLOATP (Vhourglass_delay)
32045 && XFLOAT_DATA (Vhourglass_delay) > 0)
32046 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32047 else
32048 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32050 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32051 show_hourglass, NULL);
32054 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32055 shown. */
32057 void
32058 cancel_hourglass (void)
32060 if (hourglass_atimer)
32062 cancel_atimer (hourglass_atimer);
32063 hourglass_atimer = NULL;
32066 if (hourglass_shown_p)
32068 Lisp_Object tail, frame;
32070 block_input ();
32072 FOR_EACH_FRAME (tail, frame)
32074 struct frame *f = XFRAME (frame);
32076 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32077 && FRAME_RIF (f)->hide_hourglass)
32078 FRAME_RIF (f)->hide_hourglass (f);
32079 #ifdef HAVE_NTGUI
32080 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32081 else if (!FRAME_W32_P (f))
32082 w32_arrow_cursor ();
32083 #endif
32086 hourglass_shown_p = false;
32087 unblock_input ();
32091 #endif /* HAVE_WINDOW_SYSTEM */