Avoid duplicate calls to xfree for the same pointer
[emacs.git] / src / xdisp.c
blobdc23959aadbc8a891cfd60782af0bf284f759e8e
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2017 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 <https://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>
293 #include <math.h>
295 #include "lisp.h"
296 #include "atimer.h"
297 #include "composite.h"
298 #include "keyboard.h"
299 #include "systime.h"
300 #include "frame.h"
301 #include "window.h"
302 #include "termchar.h"
303 #include "dispextern.h"
304 #include "character.h"
305 #include "buffer.h"
306 #include "charset.h"
307 #include "indent.h"
308 #include "commands.h"
309 #include "keymap.h"
310 #include "disptab.h"
311 #include "termhooks.h"
312 #include "termopts.h"
313 #include "intervals.h"
314 #include "coding.h"
315 #include "region-cache.h"
316 #include "font.h"
317 #include "fontset.h"
318 #include "blockinput.h"
319 #include "xwidget.h"
320 #ifdef HAVE_WINDOW_SYSTEM
321 #include TERM_HEADER
322 #endif /* HAVE_WINDOW_SYSTEM */
324 #ifndef FRAME_X_OUTPUT
325 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
326 #endif
328 #define DISP_INFINITY 10000000
330 /* Holds the list (error). */
331 static Lisp_Object list_of_error;
333 #ifdef HAVE_WINDOW_SYSTEM
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P ((IT)->f) \
341 && ((IT)->bidi_it.paragraph_dir == R2L \
342 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
343 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
344 && (IT)->current_x == (IT)->last_visible_x)
346 #else /* !HAVE_WINDOW_SYSTEM */
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
348 #endif /* HAVE_WINDOW_SYSTEM */
350 /* Test if the display element loaded in IT, or the underlying buffer
351 or string character, is a space or a TAB character. This is used
352 to determine where word wrapping can occur. */
354 #define IT_DISPLAYING_WHITESPACE(it) \
355 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
356 || ((STRINGP (it->string) \
357 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
358 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
359 || (it->s \
360 && (it->s[IT_BYTEPOS (*it)] == ' ' \
361 || it->s[IT_BYTEPOS (*it)] == '\t')) \
362 || (IT_BYTEPOS (*it) < ZV_BYTE \
363 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
364 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
366 /* True means print newline to stdout before next mini-buffer message. */
368 bool noninteractive_need_newline;
370 /* True means print newline to message log before next message. */
372 static bool message_log_need_newline;
374 /* Three markers that message_dolog uses.
375 It could allocate them itself, but that causes trouble
376 in handling memory-full errors. */
377 static Lisp_Object message_dolog_marker1;
378 static Lisp_Object message_dolog_marker2;
379 static Lisp_Object message_dolog_marker3;
381 /* The buffer position of the first character appearing entirely or
382 partially on the line of the selected window which contains the
383 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
384 redisplay optimization in redisplay_internal. */
386 static struct text_pos this_line_start_pos;
388 /* Number of characters past the end of the line above, including the
389 terminating newline. */
391 static struct text_pos this_line_end_pos;
393 /* The vertical positions and the height of this line. */
395 static int this_line_vpos;
396 static int this_line_y;
397 static int this_line_pixel_height;
399 /* X position at which this display line starts. Usually zero;
400 negative if first character is partially visible. */
402 static int this_line_start_x;
404 /* The smallest character position seen by move_it_* functions as they
405 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
406 hscrolled lines, see display_line. */
408 static struct text_pos this_line_min_pos;
410 /* Buffer that this_line_.* variables are referring to. */
412 static struct buffer *this_line_buffer;
414 /* True if an overlay arrow has been displayed in this window. */
416 static bool overlay_arrow_seen;
418 /* Vector containing glyphs for an ellipsis `...'. */
420 static Lisp_Object default_invis_vector[3];
422 /* This is the window where the echo area message was displayed. It
423 is always a mini-buffer window, but it may not be the same window
424 currently active as a mini-buffer. */
426 Lisp_Object echo_area_window;
428 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
429 pushes the current message and the value of
430 message_enable_multibyte on the stack, the function restore_message
431 pops the stack and displays MESSAGE again. */
433 static Lisp_Object Vmessage_stack;
435 /* True means multibyte characters were enabled when the echo area
436 message was specified. */
438 static bool message_enable_multibyte;
440 /* At each redisplay cycle, we should refresh everything there is to refresh.
441 To do that efficiently, we use many optimizations that try to make sure we
442 don't waste too much time updating things that haven't changed.
443 The coarsest such optimization is that, in the most common cases, we only
444 look at the selected-window.
446 To know whether other windows should be considered for redisplay, we use the
447 variable windows_or_buffers_changed: as long as it is 0, it means that we
448 have not noticed anything that should require updating anything else than
449 the selected-window. If it is set to REDISPLAY_SOME, it means that since
450 last redisplay, some changes have been made which could impact other
451 windows. To know which ones need redisplay, every buffer, window, and frame
452 has a `redisplay' bit, which (if true) means that this object needs to be
453 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
454 looking for those `redisplay' bits (actually, there might be some such bits
455 set, but then only on objects which aren't displayed anyway).
457 OTOH if it's non-zero we wil have to loop through all windows and then check
458 the `redisplay' bit of the corresponding window, frame, and buffer, in order
459 to decide whether that window needs attention or not. Note that we can't
460 just look at the frame's redisplay bit to decide that the whole frame can be
461 skipped, since even if the frame's redisplay bit is unset, some of its
462 windows's redisplay bits may be set.
464 Mostly for historical reasons, windows_or_buffers_changed can also take
465 other non-zero values. In that case, the precise value doesn't matter (it
466 encodes the cause of the setting but is only used for debugging purposes),
467 and what it means is that we shouldn't pay attention to any `redisplay' bits
468 and we should simply try and redisplay every window out there. */
470 int windows_or_buffers_changed;
472 /* Nonzero if we should redraw the mode lines on the next redisplay.
473 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
474 then only redisplay the mode lines in those buffers/windows/frames where the
475 `redisplay' bit has been set.
476 For any other value, redisplay all mode lines (the number used is then only
477 used to track down the cause for this full-redisplay).
479 Since the frame title uses the same %-constructs as the mode line
480 (except %c, %C, and %l), if this variable is non-zero, we also consider
481 redisplaying the title of each frame, see x_consider_frame_title.
483 The `redisplay' bits are the same as those used for
484 windows_or_buffers_changed, and setting windows_or_buffers_changed also
485 causes recomputation of the mode lines of all those windows. IOW this
486 variable only has an effect if windows_or_buffers_changed is zero, in which
487 case we should only need to redisplay the mode-line of those objects with
488 a `redisplay' bit set but not the window's text content (tho we may still
489 need to refresh the text content of the selected-window). */
491 int update_mode_lines;
493 /* True after display_mode_line if %l was used and it displayed a
494 line number. */
496 static bool line_number_displayed;
498 /* The name of the *Messages* buffer, a string. */
500 static Lisp_Object Vmessages_buffer_name;
502 /* Current, index 0, and last displayed echo area message. Either
503 buffers from echo_buffers, or nil to indicate no message. */
505 Lisp_Object echo_area_buffer[2];
507 /* The buffers referenced from echo_area_buffer. */
509 static Lisp_Object echo_buffer[2];
511 /* A vector saved used in with_area_buffer to reduce consing. */
513 static Lisp_Object Vwith_echo_area_save_vector;
515 /* True means display_echo_area should display the last echo area
516 message again. Set by redisplay_preserve_echo_area. */
518 static bool display_last_displayed_message_p;
520 /* True if echo area is being used by print; false if being used by
521 message. */
523 static bool message_buf_print;
525 /* Set to true in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
528 static bool message_cleared_p;
530 /* A scratch glyph row with contents used for generating truncation
531 glyphs. Also used in direct_output_for_insert. */
533 #define MAX_SCRATCH_GLYPHS 100
534 static struct glyph_row scratch_glyph_row;
535 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
537 /* Ascent and height of the last line processed by move_it_to. */
539 static int last_height;
541 /* True if there's a help-echo in the echo area. */
543 bool help_echo_showing_p;
545 /* The maximum distance to look ahead for text properties. Values
546 that are too small let us call compute_char_face and similar
547 functions too often which is expensive. Values that are too large
548 let us call compute_char_face and alike too often because we
549 might not be interested in text properties that far away. */
551 #define TEXT_PROP_DISTANCE_LIMIT 100
553 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
554 iterator state and later restore it. This is needed because the
555 bidi iterator on bidi.c keeps a stacked cache of its states, which
556 is really a singleton. When we use scratch iterator objects to
557 move around the buffer, we can cause the bidi cache to be pushed or
558 popped, and therefore we need to restore the cache state when we
559 return to the original iterator. */
560 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
561 do { \
562 if (CACHE) \
563 bidi_unshelve_cache (CACHE, true); \
564 ITCOPY = ITORIG; \
565 CACHE = bidi_shelve_cache (); \
566 } while (false)
568 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
569 do { \
570 if (pITORIG != pITCOPY) \
571 *(pITORIG) = *(pITCOPY); \
572 bidi_unshelve_cache (CACHE, false); \
573 CACHE = NULL; \
574 } while (false)
576 /* Functions to mark elements as needing redisplay. */
577 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
579 void
580 redisplay_other_windows (void)
582 if (!windows_or_buffers_changed)
583 windows_or_buffers_changed = REDISPLAY_SOME;
586 void
587 wset_redisplay (struct window *w)
589 /* Beware: selected_window can be nil during early stages. */
590 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
591 redisplay_other_windows ();
592 w->redisplay = true;
595 void
596 fset_redisplay (struct frame *f)
598 redisplay_other_windows ();
599 f->redisplay = true;
602 void
603 bset_redisplay (struct buffer *b)
605 int count = buffer_window_count (b);
606 if (count > 0)
608 /* ... it's visible in other window than selected, */
609 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
610 redisplay_other_windows ();
611 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
612 so that if we later set windows_or_buffers_changed, this buffer will
613 not be omitted. */
614 b->text->redisplay = true;
618 void
619 bset_update_mode_line (struct buffer *b)
621 if (!update_mode_lines)
622 update_mode_lines = REDISPLAY_SOME;
623 b->text->redisplay = true;
626 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
627 Sset_buffer_redisplay, 4, 4, 0,
628 doc: /* Mark the current buffer for redisplay.
629 This function may be passed to `add-variable-watcher'. */)
630 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
632 bset_update_mode_line (current_buffer);
633 current_buffer->prevent_redisplay_optimizations_p = true;
634 return Qnil;
637 #ifdef GLYPH_DEBUG
639 /* True means print traces of redisplay if compiled with
640 GLYPH_DEBUG defined. */
642 bool trace_redisplay_p;
644 #endif /* GLYPH_DEBUG */
646 #ifdef DEBUG_TRACE_MOVE
647 /* True means trace with TRACE_MOVE to stderr. */
648 static bool trace_move;
650 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
651 #else
652 #define TRACE_MOVE(x) (void) 0
653 #endif
655 /* Buffer being redisplayed -- for redisplay_window_error. */
657 static struct buffer *displayed_buffer;
659 /* Value returned from text property handlers (see below). */
661 enum prop_handled
663 HANDLED_NORMALLY,
664 HANDLED_RECOMPUTE_PROPS,
665 HANDLED_OVERLAY_STRING_CONSUMED,
666 HANDLED_RETURN
669 /* A description of text properties that redisplay is interested
670 in. */
672 struct props
674 /* The symbol index of the name of the property. */
675 short name;
677 /* A unique index for the property. */
678 enum prop_idx idx;
680 /* A handler function called to set up iterator IT from the property
681 at IT's current position. Value is used to steer handle_stop. */
682 enum prop_handled (*handler) (struct it *it);
685 static enum prop_handled handle_face_prop (struct it *);
686 static enum prop_handled handle_invisible_prop (struct it *);
687 static enum prop_handled handle_display_prop (struct it *);
688 static enum prop_handled handle_composition_prop (struct it *);
689 static enum prop_handled handle_overlay_change (struct it *);
690 static enum prop_handled handle_fontified_prop (struct it *);
692 /* Properties handled by iterators. */
694 static struct props it_props[] =
696 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
697 /* Handle `face' before `display' because some sub-properties of
698 `display' need to know the face. */
699 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
700 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
701 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
702 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
703 {0, 0, NULL}
706 /* Value is the position described by X. If X is a marker, value is
707 the marker_position of X. Otherwise, value is X. */
709 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
711 /* Enumeration returned by some move_it_.* functions internally. */
713 enum move_it_result
715 /* Not used. Undefined value. */
716 MOVE_UNDEFINED,
718 /* Move ended at the requested buffer position or ZV. */
719 MOVE_POS_MATCH_OR_ZV,
721 /* Move ended at the requested X pixel position. */
722 MOVE_X_REACHED,
724 /* Move within a line ended at the end of a line that must be
725 continued. */
726 MOVE_LINE_CONTINUED,
728 /* Move within a line ended at the end of a line that would
729 be displayed truncated. */
730 MOVE_LINE_TRUNCATED,
732 /* Move within a line ended at a line end. */
733 MOVE_NEWLINE_OR_CR
736 /* This counter is used to clear the face cache every once in a while
737 in redisplay_internal. It is incremented for each redisplay.
738 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
739 cleared. */
741 #define CLEAR_FACE_CACHE_COUNT 500
742 static int clear_face_cache_count;
744 /* Similarly for the image cache. */
746 #ifdef HAVE_WINDOW_SYSTEM
747 #define CLEAR_IMAGE_CACHE_COUNT 101
748 static int clear_image_cache_count;
750 /* Null glyph slice */
751 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
752 #endif
754 /* True while redisplay_internal is in progress. */
756 bool redisplaying_p;
758 /* If a string, XTread_socket generates an event to display that string.
759 (The display is done in read_char.) */
761 Lisp_Object help_echo_string;
762 Lisp_Object help_echo_window;
763 Lisp_Object help_echo_object;
764 ptrdiff_t help_echo_pos;
766 /* Temporary variable for XTread_socket. */
768 Lisp_Object previous_help_echo_string;
770 /* Platform-independent portion of hourglass implementation. */
772 #ifdef HAVE_WINDOW_SYSTEM
774 /* True means an hourglass cursor is currently shown. */
775 static bool hourglass_shown_p;
777 /* If non-null, an asynchronous timer that, when it expires, displays
778 an hourglass cursor on all frames. */
779 static struct atimer *hourglass_atimer;
781 #endif /* HAVE_WINDOW_SYSTEM */
783 /* Default number of seconds to wait before displaying an hourglass
784 cursor. */
785 #define DEFAULT_HOURGLASS_DELAY 1
787 #ifdef HAVE_WINDOW_SYSTEM
789 /* Default pixel width of `thin-space' display method. */
790 #define THIN_SPACE_WIDTH 1
792 #endif /* HAVE_WINDOW_SYSTEM */
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, bool);
798 static void mark_window_display_accurate_1 (struct window *, bool);
799 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
800 static bool cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, bool);
803 static void handle_line_prefix (struct it *);
805 static void handle_stop_backwards (struct it *, ptrdiff_t);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static bool current_message_1 (ptrdiff_t, Lisp_Object);
809 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static bool set_message_1 (ptrdiff_t, Lisp_Object);
812 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static void extend_face_to_end_of_line (struct it *);
816 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
817 static void push_it (struct it *, struct text_pos *);
818 static void iterate_out_of_display_property (struct it *);
819 static void pop_it (struct it *);
820 static void redisplay_internal (void);
821 static void echo_area_display (bool);
822 static void block_buffer_flips (void);
823 static void unblock_buffer_flips (void);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, bool);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static bool set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
831 int, int);
832 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
833 static bool update_menu_bar (struct frame *, bool, bool);
834 static bool try_window_reusing_current_matrix (struct window *);
835 static int try_window_id (struct window *);
836 static void maybe_produce_line_number (struct it *);
837 static bool should_produce_line_number (struct it *);
838 static bool display_line (struct it *, int);
839 static int display_mode_lines (struct window *);
840 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
841 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
842 Lisp_Object, bool);
843 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
844 Lisp_Object);
845 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
846 static void display_menu_bar (struct window *);
847 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
848 ptrdiff_t *);
849 static void pint2str (register char *, register int, register ptrdiff_t);
851 static int display_string (const char *, Lisp_Object, Lisp_Object,
852 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
853 static void compute_line_metrics (struct it *);
854 static void run_redisplay_end_trigger_hook (struct it *);
855 static bool get_overlay_strings (struct it *, ptrdiff_t);
856 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
857 static void next_overlay_string (struct it *);
858 static void reseat (struct it *, struct text_pos, bool);
859 static void reseat_1 (struct it *, struct text_pos, bool);
860 static bool next_element_from_display_vector (struct it *);
861 static bool next_element_from_string (struct it *);
862 static bool next_element_from_c_string (struct it *);
863 static bool next_element_from_buffer (struct it *);
864 static bool next_element_from_composition (struct it *);
865 static bool next_element_from_image (struct it *);
866 static bool next_element_from_stretch (struct it *);
867 static bool next_element_from_xwidget (struct it *);
868 static void load_overlay_strings (struct it *, ptrdiff_t);
869 static bool get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
872 enum move_operation_enum);
873 static void get_visually_first_element (struct it *);
874 static void compute_stop_pos (struct it *);
875 static int face_before_or_after_it_pos (struct it *, bool);
876 static ptrdiff_t next_overlay_change (ptrdiff_t);
877 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
878 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
879 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
880 Lisp_Object, struct text_pos *,
881 ptrdiff_t, int, bool, bool);
882 static int underlying_face_id (struct it *);
884 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
885 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
887 #ifdef HAVE_WINDOW_SYSTEM
889 static void update_tool_bar (struct frame *, bool);
890 static void x_draw_bottom_divider (struct window *w);
891 static void notice_overwritten_cursor (struct window *,
892 enum glyph_row_area,
893 int, int, int, int);
894 static int normal_char_height (struct font *, int);
895 static void normal_char_ascent_descent (struct font *, int, int *, int *);
897 static void append_stretch_glyph (struct it *, Lisp_Object,
898 int, int, int);
900 static Lisp_Object get_it_property (struct it *, Lisp_Object);
901 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
902 struct font *, int, bool);
904 #endif /* HAVE_WINDOW_SYSTEM */
906 static void produce_special_glyphs (struct it *, enum display_element_type);
907 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
908 static bool coords_in_mouse_face_p (struct window *, int, int);
912 /***********************************************************************
913 Window display dimensions
914 ***********************************************************************/
916 /* Return the bottom boundary y-position for text lines in window W.
917 This is the first y position at which a line cannot start.
918 It is relative to the top of the window.
920 This is the height of W minus the height of a mode line, if any. */
923 window_text_bottom_y (struct window *w)
925 int height = WINDOW_PIXEL_HEIGHT (w);
927 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
929 if (window_wants_mode_line (w))
930 height -= CURRENT_MODE_LINE_HEIGHT (w);
932 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
934 return height;
937 /* Return the pixel width of display area AREA of window W.
938 ANY_AREA means return the total width of W, not including
939 fringes to the left and right of the window. */
942 window_box_width (struct window *w, enum glyph_row_area area)
944 int width = w->pixel_width;
946 if (!w->pseudo_window_p)
948 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
949 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
951 if (area == TEXT_AREA)
952 width -= (WINDOW_MARGINS_WIDTH (w)
953 + WINDOW_FRINGES_WIDTH (w));
954 else if (area == LEFT_MARGIN_AREA)
955 width = WINDOW_LEFT_MARGIN_WIDTH (w);
956 else if (area == RIGHT_MARGIN_AREA)
957 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
960 /* With wide margins, fringes, etc. we might end up with a negative
961 width, correct that here. */
962 return max (0, width);
966 /* Return the pixel height of the display area of window W, not
967 including mode lines of W, if any. */
970 window_box_height (struct window *w)
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_PIXEL_HEIGHT (w);
975 eassert (height >= 0);
977 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
978 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
980 /* Note: the code below that determines the mode-line/header-line
981 height is essentially the same as that contained in the macro
982 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
983 the appropriate glyph row has its `mode_line_p' flag set,
984 and if it doesn't, uses estimate_mode_line_height instead. */
986 if (window_wants_mode_line (w))
988 struct glyph_row *ml_row
989 = (w->current_matrix && w->current_matrix->rows
990 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
991 : 0);
992 if (ml_row && ml_row->mode_line_p)
993 height -= ml_row->height;
994 else
995 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
998 if (window_wants_header_line (w))
1000 struct glyph_row *hl_row
1001 = (w->current_matrix && w->current_matrix->rows
1002 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1003 : 0);
1004 if (hl_row && hl_row->mode_line_p)
1005 height -= hl_row->height;
1006 else
1007 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1010 /* With a very small font and a mode-line that's taller than
1011 default, we might end up with a negative height. */
1012 return max (0, height);
1015 /* Return the window-relative coordinate of the left edge of display
1016 area AREA of window W. ANY_AREA means return the left edge of the
1017 whole window, to the right of the left fringe of W. */
1020 window_box_left_offset (struct window *w, enum glyph_row_area area)
1022 int x;
1024 if (w->pseudo_window_p)
1025 return 0;
1027 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1031 + window_box_width (w, LEFT_MARGIN_AREA));
1032 else if (area == RIGHT_MARGIN_AREA)
1033 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1034 + window_box_width (w, LEFT_MARGIN_AREA)
1035 + window_box_width (w, TEXT_AREA)
1036 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1038 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1039 else if (area == LEFT_MARGIN_AREA
1040 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1041 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1043 /* Don't return more than the window's pixel width. */
1044 return min (x, w->pixel_width);
1048 /* Return the window-relative coordinate of the right edge of display
1049 area AREA of window W. ANY_AREA means return the right edge of the
1050 whole window, to the left of the right fringe of W. */
1052 static int
1053 window_box_right_offset (struct window *w, enum glyph_row_area area)
1055 /* Don't return more than the window's pixel width. */
1056 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1057 w->pixel_width);
1060 /* Return the frame-relative coordinate of the left edge of display
1061 area AREA of window W. ANY_AREA means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left (struct window *w, enum glyph_row_area area)
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1076 return x;
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. ANY_AREA means return the right edge of the
1082 whole window, to the left of the right fringe of W. */
1085 window_box_right (struct window *w, enum glyph_row_area area)
1087 return window_box_left (w, area) + window_box_width (w, area);
1090 /* Get the bounding box of the display area AREA of window W, without
1091 mode lines, in frame-relative coordinates. ANY_AREA means the
1092 whole window, not including the left and right fringes of
1093 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1094 coordinates of the upper-left corner of the box. Return in
1095 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1097 void
1098 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1099 int *box_y, int *box_width, int *box_height)
1101 if (box_width)
1102 *box_width = window_box_width (w, area);
1103 if (box_height)
1104 *box_height = window_box_height (w);
1105 if (box_x)
1106 *box_x = window_box_left (w, area);
1107 if (box_y)
1109 *box_y = WINDOW_TOP_EDGE_Y (w);
1110 if (window_wants_header_line (w))
1111 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1115 #ifdef HAVE_WINDOW_SYSTEM
1117 /* Get the bounding box of the display area AREA of window W, without
1118 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1119 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1120 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1121 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1122 box. */
1124 static void
1125 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1126 int *bottom_right_x, int *bottom_right_y)
1128 window_box (w, ANY_AREA, top_left_x, top_left_y,
1129 bottom_right_x, bottom_right_y);
1130 *bottom_right_x += *top_left_x;
1131 *bottom_right_y += *top_left_y;
1134 #endif /* HAVE_WINDOW_SYSTEM */
1136 /***********************************************************************
1137 Utilities
1138 ***********************************************************************/
1140 /* Return the bottom y-position of the line the iterator IT is in.
1141 This can modify IT's settings. */
1144 line_bottom_y (struct it *it)
1146 int line_height = it->max_ascent + it->max_descent;
1147 int line_top_y = it->current_y;
1149 if (line_height == 0)
1151 if (last_height)
1152 line_height = last_height;
1153 else if (IT_CHARPOS (*it) < ZV)
1155 move_it_by_lines (it, 1);
1156 line_height = (it->max_ascent || it->max_descent
1157 ? it->max_ascent + it->max_descent
1158 : last_height);
1160 else
1162 struct glyph_row *row = it->glyph_row;
1164 /* Use the default character height. */
1165 it->glyph_row = NULL;
1166 it->what = IT_CHARACTER;
1167 it->c = ' ';
1168 it->len = 1;
1169 PRODUCE_GLYPHS (it);
1170 line_height = it->ascent + it->descent;
1171 it->glyph_row = row;
1175 return line_top_y + line_height;
1178 DEFUN ("line-pixel-height", Fline_pixel_height,
1179 Sline_pixel_height, 0, 0, 0,
1180 doc: /* Return height in pixels of text line in the selected window.
1182 Value is the height in pixels of the line at point. */)
1183 (void)
1185 struct it it;
1186 struct text_pos pt;
1187 struct window *w = XWINDOW (selected_window);
1188 struct buffer *old_buffer = NULL;
1189 Lisp_Object result;
1191 if (XBUFFER (w->contents) != current_buffer)
1193 old_buffer = current_buffer;
1194 set_buffer_internal_1 (XBUFFER (w->contents));
1196 SET_TEXT_POS (pt, PT, PT_BYTE);
1197 start_display (&it, w, pt);
1198 /* Start from the beginning of the screen line, to make sure we
1199 traverse all of its display elements, and thus capture the
1200 correct metrics. */
1201 move_it_by_lines (&it, 0);
1202 it.vpos = it.current_y = 0;
1203 last_height = 0;
1204 result = make_number (line_bottom_y (&it));
1205 if (old_buffer)
1206 set_buffer_internal_1 (old_buffer);
1208 return result;
1211 /* Return the default pixel height of text lines in window W. The
1212 value is the canonical height of the W frame's default font, plus
1213 any extra space required by the line-spacing variable or frame
1214 parameter.
1216 Implementation note: this ignores any line-spacing text properties
1217 put on the newline characters. This is because those properties
1218 only affect the _screen_ line ending in the newline (i.e., in a
1219 continued line, only the last screen line will be affected), which
1220 means only a small number of lines in a buffer can ever use this
1221 feature. Since this function is used to compute the default pixel
1222 equivalent of text lines in a window, we can safely ignore those
1223 few lines. For the same reasons, we ignore the line-height
1224 properties. */
1226 default_line_pixel_height (struct window *w)
1228 struct frame *f = WINDOW_XFRAME (w);
1229 int height = FRAME_LINE_HEIGHT (f);
1231 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1233 struct buffer *b = XBUFFER (w->contents);
1234 Lisp_Object val = BVAR (b, extra_line_spacing);
1236 if (NILP (val))
1237 val = BVAR (&buffer_defaults, extra_line_spacing);
1238 if (!NILP (val))
1240 if (RANGED_INTEGERP (0, val, INT_MAX))
1241 height += XFASTINT (val);
1242 else if (FLOATP (val))
1244 int addon = XFLOAT_DATA (val) * height + 0.5;
1246 if (addon >= 0)
1247 height += addon;
1250 else
1251 height += f->extra_line_spacing;
1254 return height;
1257 /* Subroutine of pos_visible_p below. Extracts a display string, if
1258 any, from the display spec given as its argument. */
1259 static Lisp_Object
1260 string_from_display_spec (Lisp_Object spec)
1262 if (VECTORP (spec))
1264 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1265 if (STRINGP (AREF (spec, i)))
1266 return AREF (spec, i);
1268 else
1270 for (; CONSP (spec); spec = XCDR (spec))
1271 if (STRINGP (XCAR (spec)))
1272 return XCAR (spec);
1274 return spec;
1278 /* Limit insanely large values of W->hscroll on frame F to the largest
1279 value that will still prevent first_visible_x and last_visible_x of
1280 'struct it' from overflowing an int. */
1281 static int
1282 window_hscroll_limited (struct window *w, struct frame *f)
1284 ptrdiff_t window_hscroll = w->hscroll;
1285 int window_text_width = window_box_width (w, TEXT_AREA);
1286 int colwidth = FRAME_COLUMN_WIDTH (f);
1288 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1289 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1291 return window_hscroll;
1294 /* Return true if position CHARPOS is visible in window W.
1295 CHARPOS < 0 means return info about WINDOW_END position.
1296 If visible, set *X and *Y to pixel coordinates of top left corner.
1297 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1298 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1300 bool
1301 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1302 int *rtop, int *rbot, int *rowh, int *vpos)
1304 struct it it;
1305 void *itdata = bidi_shelve_cache ();
1306 struct text_pos top;
1307 bool visible_p = false;
1308 struct buffer *old_buffer = NULL;
1309 bool r2l = false;
1311 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1312 return visible_p;
1314 if (XBUFFER (w->contents) != current_buffer)
1316 old_buffer = current_buffer;
1317 set_buffer_internal_1 (XBUFFER (w->contents));
1320 SET_TEXT_POS_FROM_MARKER (top, w->start);
1321 /* Scrolling a minibuffer window via scroll bar when the echo area
1322 shows long text sometimes resets the minibuffer contents behind
1323 our backs. Also, someone might narrow-to-region and immediately
1324 call a scroll function. */
1325 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1328 /* If the top of the window is after CHARPOS, the latter is surely
1329 not visible. */
1330 if (charpos >= 0 && CHARPOS (top) > charpos)
1331 return visible_p;
1333 /* Some Lisp hook could call us in the middle of redisplaying this
1334 very window. If, by some bad luck, we are retrying redisplay
1335 because we found that the mode-line height and/or header-line
1336 height needs to be updated, the assignment of mode_line_height
1337 and header_line_height below could disrupt that, due to the
1338 selected/nonselected window dance during mode-line display, and
1339 we could infloop. Avoid that. */
1340 int prev_mode_line_height = w->mode_line_height;
1341 int prev_header_line_height = w->header_line_height;
1342 /* Compute exact mode line heights. */
1343 if (window_wants_mode_line (w))
1345 Lisp_Object window_mode_line_format
1346 = window_parameter (w, Qmode_line_format);
1348 w->mode_line_height
1349 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1350 NILP (window_mode_line_format)
1351 ? BVAR (current_buffer, mode_line_format)
1352 : window_mode_line_format);
1355 if (window_wants_header_line (w))
1357 Lisp_Object window_header_line_format
1358 = window_parameter (w, Qheader_line_format);
1360 w->header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 NILP (window_header_line_format)
1363 ? BVAR (current_buffer, header_line_format)
1364 : window_header_line_format);
1367 start_display (&it, w, top);
1368 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1369 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1371 if (charpos >= 0
1372 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1373 && IT_CHARPOS (it) >= charpos)
1374 /* When scanning backwards under bidi iteration, move_it_to
1375 stops at or _before_ CHARPOS, because it stops at or to
1376 the _right_ of the character at CHARPOS. */
1377 || (it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) <= charpos)))
1380 /* We have reached CHARPOS, or passed it. How the call to
1381 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1382 or covered by a display property, move_it_to stops at the end
1383 of the invisible text, to the right of CHARPOS. (ii) If
1384 CHARPOS is in a display vector, move_it_to stops on its last
1385 glyph. */
1386 int top_x = it.current_x;
1387 int top_y = it.current_y;
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1389 int bottom_y;
1390 struct it save_it;
1391 void *save_it_data = NULL;
1393 /* Calling line_bottom_y may change it.method, it.position, etc. */
1394 SAVE_IT (save_it, it, save_it_data);
1395 last_height = 0;
1396 bottom_y = line_bottom_y (&it);
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = true;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 /* Why 10? because we don't know how many canonical lines
1414 will the height of the next line(s) be. So we guess. */
1415 int ten_more_lines = 10 * default_line_pixel_height (w);
1417 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1418 MOVE_TO_POS | MOVE_TO_Y);
1419 if (it.current_y > top_y)
1420 visible_p = false;
1423 RESTORE_IT (&it, &save_it, save_it_data);
1424 if (visible_p)
1426 if (it.method == GET_FROM_DISPLAY_VECTOR)
1428 /* We stopped on the last glyph of a display vector.
1429 Try and recompute. Hack alert! */
1430 if (charpos < 2 || top.charpos >= charpos)
1431 top_x = it.glyph_row->x;
1432 else
1434 struct it it2, it2_prev;
1435 /* The idea is to get to the previous buffer
1436 position, consume the character there, and use
1437 the pixel coordinates we get after that. But if
1438 the previous buffer position is also displayed
1439 from a display vector, we need to consume all of
1440 the glyphs from that display vector. */
1441 start_display (&it2, w, top);
1442 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* If we didn't get to CHARPOS - 1, there's some
1444 replacing display property at that position, and
1445 we stopped after it. That is exactly the place
1446 whose coordinates we want. */
1447 if (IT_CHARPOS (it2) != charpos - 1)
1448 it2_prev = it2;
1449 else
1451 /* Iterate until we get out of the display
1452 vector that displays the character at
1453 CHARPOS - 1. */
1454 do {
1455 get_next_display_element (&it2);
1456 PRODUCE_GLYPHS (&it2);
1457 it2_prev = it2;
1458 set_iterator_to_next (&it2, true);
1459 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1460 && IT_CHARPOS (it2) < charpos);
1462 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1463 || it2_prev.current_x > it2_prev.last_visible_x)
1464 top_x = it.glyph_row->x;
1465 else
1467 top_x = it2_prev.current_x;
1468 top_y = it2_prev.current_y;
1472 else if (IT_CHARPOS (it) != charpos)
1474 Lisp_Object cpos = make_number (charpos);
1475 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1476 Lisp_Object string = string_from_display_spec (spec);
1477 struct text_pos tpos;
1478 bool newline_in_string
1479 = (STRINGP (string)
1480 && memchr (SDATA (string), '\n', SBYTES (string)));
1482 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1483 bool replacing_spec_p
1484 = (!NILP (spec)
1485 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1486 charpos, FRAME_WINDOW_P (it.f)));
1487 /* The tricky code below is needed because there's a
1488 discrepancy between move_it_to and how we set cursor
1489 when PT is at the beginning of a portion of text
1490 covered by a display property or an overlay with a
1491 display property, or the display line ends in a
1492 newline from a display string. move_it_to will stop
1493 _after_ such display strings, whereas
1494 set_cursor_from_row conspires with cursor_row_p to
1495 place the cursor on the first glyph produced from the
1496 display string. */
1498 /* We have overshoot PT because it is covered by a
1499 display property that replaces the text it covers.
1500 If the string includes embedded newlines, we are also
1501 in the wrong display line. Backtrack to the correct
1502 line, where the display property begins. */
1503 if (replacing_spec_p)
1505 Lisp_Object startpos, endpos;
1506 EMACS_INT start, end;
1507 struct it it3;
1509 /* Find the first and the last buffer positions
1510 covered by the display string. */
1511 endpos =
1512 Fnext_single_char_property_change (cpos, Qdisplay,
1513 Qnil, Qnil);
1514 startpos =
1515 Fprevious_single_char_property_change (endpos, Qdisplay,
1516 Qnil, Qnil);
1517 start = XFASTINT (startpos);
1518 end = XFASTINT (endpos);
1519 /* Move to the last buffer position before the
1520 display property. */
1521 start_display (&it3, w, top);
1522 if (start > CHARPOS (top))
1523 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1524 /* Move forward one more line if the position before
1525 the display string is a newline or if it is the
1526 rightmost character on a line that is
1527 continued or word-wrapped. */
1528 if (it3.method == GET_FROM_BUFFER
1529 && (it3.c == '\n'
1530 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1531 move_it_by_lines (&it3, 1);
1532 else if (move_it_in_display_line_to (&it3, -1,
1533 it3.current_x
1534 + it3.pixel_width,
1535 MOVE_TO_X)
1536 == MOVE_LINE_CONTINUED)
1538 move_it_by_lines (&it3, 1);
1539 /* When we are under word-wrap, the #$@%!
1540 move_it_by_lines moves 2 lines, so we need to
1541 fix that up. */
1542 if (it3.line_wrap == WORD_WRAP)
1543 move_it_by_lines (&it3, -1);
1546 /* Record the vertical coordinate of the display
1547 line where we wound up. */
1548 top_y = it3.current_y;
1549 if (it3.bidi_p)
1551 /* When characters are reordered for display,
1552 the character displayed to the left of the
1553 display string could be _after_ the display
1554 property in the logical order. Use the
1555 smallest vertical position of these two. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1558 if (it3.current_y < top_y)
1559 top_y = it3.current_y;
1561 /* Move from the top of the window to the beginning
1562 of the display line where the display string
1563 begins. */
1564 start_display (&it3, w, top);
1565 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1566 /* If it3_moved stays false after the 'while' loop
1567 below, that means we already were at a newline
1568 before the loop (e.g., the display string begins
1569 with a newline), so we don't need to (and cannot)
1570 inspect the glyphs of it3.glyph_row, because
1571 PRODUCE_GLYPHS will not produce anything for a
1572 newline, and thus it3.glyph_row stays at its
1573 stale content it got at top of the window. */
1574 bool it3_moved = false;
1575 /* Finally, advance the iterator until we hit the
1576 first display element whose character position is
1577 CHARPOS, or until the first newline from the
1578 display string, which signals the end of the
1579 display line. */
1580 while (get_next_display_element (&it3))
1582 PRODUCE_GLYPHS (&it3);
1583 if (IT_CHARPOS (it3) == charpos
1584 || ITERATOR_AT_END_OF_LINE_P (&it3))
1585 break;
1586 it3_moved = true;
1587 set_iterator_to_next (&it3, false);
1589 top_x = it3.current_x - it3.pixel_width;
1590 /* Normally, we would exit the above loop because we
1591 found the display element whose character
1592 position is CHARPOS. For the contingency that we
1593 didn't, and stopped at the first newline from the
1594 display string, move back over the glyphs
1595 produced from the string, until we find the
1596 rightmost glyph not from the string. */
1597 if (it3_moved
1598 && newline_in_string
1599 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1601 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1602 + it3.glyph_row->used[TEXT_AREA];
1604 while (EQ ((g - 1)->object, string))
1606 --g;
1607 top_x -= g->pixel_width;
1609 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1610 + it3.glyph_row->used[TEXT_AREA]);
1615 *x = top_x;
1616 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1617 *rtop = max (0, window_top_y - top_y);
1618 *rbot = max (0, bottom_y - it.last_visible_y);
1619 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1620 - max (top_y, window_top_y)));
1621 *vpos = it.vpos;
1622 if (it.bidi_it.paragraph_dir == R2L)
1623 r2l = true;
1626 else
1628 /* Either we were asked to provide info about WINDOW_END, or
1629 CHARPOS is in the partially visible glyph row at end of
1630 window. */
1631 struct it it2;
1632 void *it2data = NULL;
1634 SAVE_IT (it2, it, it2data);
1635 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1636 move_it_by_lines (&it, 1);
1637 if (charpos < IT_CHARPOS (it)
1638 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1640 visible_p = true;
1641 RESTORE_IT (&it2, &it2, it2data);
1642 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1643 *x = it2.current_x;
1644 *y = it2.current_y + it2.max_ascent - it2.ascent;
1645 *rtop = max (0, -it2.current_y);
1646 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1647 - it.last_visible_y));
1648 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1649 it.last_visible_y)
1650 - max (it2.current_y,
1651 WINDOW_HEADER_LINE_HEIGHT (w))));
1652 *vpos = it2.vpos;
1653 if (it2.bidi_it.paragraph_dir == R2L)
1654 r2l = true;
1656 else
1657 bidi_unshelve_cache (it2data, true);
1659 bidi_unshelve_cache (itdata, false);
1661 if (old_buffer)
1662 set_buffer_internal_1 (old_buffer);
1664 if (visible_p)
1666 if (w->hscroll > 0)
1667 *x -=
1668 window_hscroll_limited (w, WINDOW_XFRAME (w))
1669 * WINDOW_FRAME_COLUMN_WIDTH (w);
1670 /* For lines in an R2L paragraph, we need to mirror the X pixel
1671 coordinate wrt the text area. For the reasons, see the
1672 commentary in buffer_posn_from_coords and the explanation of
1673 the geometry used by the move_it_* functions at the end of
1674 the large commentary near the beginning of this file. */
1675 if (r2l)
1676 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1679 #if false
1680 /* Debugging code. */
1681 if (visible_p)
1682 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1683 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1684 else
1685 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1686 #endif
1688 /* Restore potentially overwritten values. */
1689 w->mode_line_height = prev_mode_line_height;
1690 w->header_line_height = prev_header_line_height;
1692 return visible_p;
1696 /* Return the next character from STR. Return in *LEN the length of
1697 the character. This is like STRING_CHAR_AND_LENGTH but never
1698 returns an invalid character. If we find one, we return a `?', but
1699 with the length of the invalid character. */
1701 static int
1702 string_char_and_length (const unsigned char *str, int *len)
1704 int c;
1706 c = STRING_CHAR_AND_LENGTH (str, *len);
1707 if (!CHAR_VALID_P (c))
1708 /* We may not change the length here because other places in Emacs
1709 don't use this function, i.e. they silently accept invalid
1710 characters. */
1711 c = '?';
1713 return c;
1718 /* Given a position POS containing a valid character and byte position
1719 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1721 static struct text_pos
1722 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1724 eassert (STRINGP (string) && nchars >= 0);
1726 if (STRING_MULTIBYTE (string))
1728 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1729 int len;
1731 while (nchars--)
1733 string_char_and_length (p, &len);
1734 p += len;
1735 CHARPOS (pos) += 1;
1736 BYTEPOS (pos) += len;
1739 else
1740 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1742 return pos;
1746 /* Value is the text position, i.e. character and byte position,
1747 for character position CHARPOS in STRING. */
1749 static struct text_pos
1750 string_pos (ptrdiff_t charpos, Lisp_Object string)
1752 struct text_pos pos;
1753 eassert (STRINGP (string));
1754 eassert (charpos >= 0);
1755 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1756 return pos;
1760 /* Value is a text position, i.e. character and byte position, for
1761 character position CHARPOS in C string S. MULTIBYTE_P
1762 means recognize multibyte characters. */
1764 static struct text_pos
1765 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1767 struct text_pos pos;
1769 eassert (s != NULL);
1770 eassert (charpos >= 0);
1772 if (multibyte_p)
1774 int len;
1776 SET_TEXT_POS (pos, 0, 0);
1777 while (charpos--)
1779 string_char_and_length ((const unsigned char *) s, &len);
1780 s += len;
1781 CHARPOS (pos) += 1;
1782 BYTEPOS (pos) += len;
1785 else
1786 SET_TEXT_POS (pos, charpos, charpos);
1788 return pos;
1792 /* Value is the number of characters in C string S. MULTIBYTE_P
1793 means recognize multibyte characters. */
1795 static ptrdiff_t
1796 number_of_chars (const char *s, bool multibyte_p)
1798 ptrdiff_t nchars;
1800 if (multibyte_p)
1802 ptrdiff_t rest = strlen (s);
1803 int len;
1804 const unsigned char *p = (const unsigned char *) s;
1806 for (nchars = 0; rest > 0; ++nchars)
1808 string_char_and_length (p, &len);
1809 rest -= len, p += len;
1812 else
1813 nchars = strlen (s);
1815 return nchars;
1819 /* Compute byte position NEWPOS->bytepos corresponding to
1820 NEWPOS->charpos. POS is a known position in string STRING.
1821 NEWPOS->charpos must be >= POS.charpos. */
1823 static void
1824 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1826 eassert (STRINGP (string));
1827 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1829 if (STRING_MULTIBYTE (string))
1830 *newpos = string_pos_nchars_ahead (pos, string,
1831 CHARPOS (*newpos) - CHARPOS (pos));
1832 else
1833 BYTEPOS (*newpos) = CHARPOS (*newpos);
1836 /* EXPORT:
1837 Return an estimation of the pixel height of mode or header lines on
1838 frame F. FACE_ID specifies what line's height to estimate. */
1841 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1843 #ifdef HAVE_WINDOW_SYSTEM
1844 if (FRAME_WINDOW_P (f))
1846 int height = FONT_HEIGHT (FRAME_FONT (f));
1848 /* This function is called so early when Emacs starts that the face
1849 cache and mode line face are not yet initialized. */
1850 if (FRAME_FACE_CACHE (f))
1852 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1853 if (face)
1855 if (face->font)
1856 height = normal_char_height (face->font, -1);
1857 if (face->box_line_width > 0)
1858 height += 2 * face->box_line_width;
1862 return height;
1864 #endif
1866 return 1;
1869 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1870 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1871 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1872 not force the value into range. */
1874 void
1875 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1876 NativeRectangle *bounds, bool noclip)
1879 #ifdef HAVE_WINDOW_SYSTEM
1880 if (FRAME_WINDOW_P (f))
1882 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1883 even for negative values. */
1884 if (pix_x < 0)
1885 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1886 if (pix_y < 0)
1887 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1889 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1890 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1892 if (bounds)
1893 STORE_NATIVE_RECT (*bounds,
1894 FRAME_COL_TO_PIXEL_X (f, pix_x),
1895 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1896 FRAME_COLUMN_WIDTH (f) - 1,
1897 FRAME_LINE_HEIGHT (f) - 1);
1899 /* PXW: Should we clip pixels before converting to columns/lines? */
1900 if (!noclip)
1902 if (pix_x < 0)
1903 pix_x = 0;
1904 else if (pix_x > FRAME_TOTAL_COLS (f))
1905 pix_x = FRAME_TOTAL_COLS (f);
1907 if (pix_y < 0)
1908 pix_y = 0;
1909 else if (pix_y > FRAME_TOTAL_LINES (f))
1910 pix_y = FRAME_TOTAL_LINES (f);
1913 #endif
1915 *x = pix_x;
1916 *y = pix_y;
1920 /* Find the glyph under window-relative coordinates X/Y in window W.
1921 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1922 strings. Return in *HPOS and *VPOS the row and column number of
1923 the glyph found. Return in *AREA the glyph area containing X.
1924 Value is a pointer to the glyph found or null if X/Y is not on
1925 text, or we can't tell because W's current matrix is not up to
1926 date. */
1928 static struct glyph *
1929 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1930 int *dx, int *dy, int *area)
1932 struct glyph *glyph, *end;
1933 struct glyph_row *row = NULL;
1934 int x0, i;
1936 /* Find row containing Y. Give up if some row is not enabled. */
1937 for (i = 0; i < w->current_matrix->nrows; ++i)
1939 row = MATRIX_ROW (w->current_matrix, i);
1940 if (!row->enabled_p)
1941 return NULL;
1942 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1943 break;
1946 *vpos = i;
1947 *hpos = 0;
1949 /* Give up if Y is not in the window. */
1950 if (i == w->current_matrix->nrows)
1951 return NULL;
1953 /* Get the glyph area containing X. */
1954 if (w->pseudo_window_p)
1956 *area = TEXT_AREA;
1957 x0 = 0;
1959 else
1961 if (x < window_box_left_offset (w, TEXT_AREA))
1963 *area = LEFT_MARGIN_AREA;
1964 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1966 else if (x < window_box_right_offset (w, TEXT_AREA))
1968 *area = TEXT_AREA;
1969 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1971 else
1973 *area = RIGHT_MARGIN_AREA;
1974 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1978 /* Find glyph containing X. */
1979 glyph = row->glyphs[*area];
1980 end = glyph + row->used[*area];
1981 x -= x0;
1982 while (glyph < end && x >= glyph->pixel_width)
1984 x -= glyph->pixel_width;
1985 ++glyph;
1988 if (glyph == end)
1989 return NULL;
1991 if (dx)
1993 *dx = x;
1994 *dy = y - (row->y + row->ascent - glyph->ascent);
1997 *hpos = glyph - row->glyphs[*area];
1998 return glyph;
2001 /* Convert frame-relative x/y to coordinates relative to window W.
2002 Takes pseudo-windows into account. */
2004 static void
2005 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2007 if (w->pseudo_window_p)
2009 /* A pseudo-window is always full-width, and starts at the
2010 left edge of the frame, plus a frame border. */
2011 struct frame *f = XFRAME (w->frame);
2012 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2013 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2015 else
2017 *x -= WINDOW_LEFT_EDGE_X (w);
2018 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2022 #ifdef HAVE_WINDOW_SYSTEM
2024 /* EXPORT:
2025 Return in RECTS[] at most N clipping rectangles for glyph string S.
2026 Return the number of stored rectangles. */
2029 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2031 XRectangle r;
2033 if (n <= 0)
2034 return 0;
2036 if (s->row->full_width_p)
2038 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2039 r.x = WINDOW_LEFT_EDGE_X (s->w);
2040 if (s->row->mode_line_p)
2041 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2042 else
2043 r.width = WINDOW_PIXEL_WIDTH (s->w);
2045 /* Unless displaying a mode or menu bar line, which are always
2046 fully visible, clip to the visible part of the row. */
2047 if (s->w->pseudo_window_p)
2048 r.height = s->row->visible_height;
2049 else
2050 r.height = s->height;
2052 else
2054 /* This is a text line that may be partially visible. */
2055 r.x = window_box_left (s->w, s->area);
2056 r.width = window_box_width (s->w, s->area);
2057 r.height = s->row->visible_height;
2060 if (s->clip_head)
2061 if (r.x < s->clip_head->x)
2063 if (r.width >= s->clip_head->x - r.x)
2064 r.width -= s->clip_head->x - r.x;
2065 else
2066 r.width = 0;
2067 r.x = s->clip_head->x;
2069 if (s->clip_tail)
2070 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2072 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2073 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2074 else
2075 r.width = 0;
2078 /* If S draws overlapping rows, it's sufficient to use the top and
2079 bottom of the window for clipping because this glyph string
2080 intentionally draws over other lines. */
2081 if (s->for_overlaps)
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 r.height = window_text_bottom_y (s->w) - r.y;
2086 /* Alas, the above simple strategy does not work for the
2087 environments with anti-aliased text: if the same text is
2088 drawn onto the same place multiple times, it gets thicker.
2089 If the overlap we are processing is for the erased cursor, we
2090 take the intersection with the rectangle of the cursor. */
2091 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2093 XRectangle rc, r_save = r;
2095 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2096 rc.y = s->w->phys_cursor.y;
2097 rc.width = s->w->phys_cursor_width;
2098 rc.height = s->w->phys_cursor_height;
2100 x_intersect_rectangles (&r_save, &rc, &r);
2103 else
2105 /* Don't use S->y for clipping because it doesn't take partially
2106 visible lines into account. For example, it can be negative for
2107 partially visible lines at the top of a window. */
2108 if (!s->row->full_width_p
2109 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2110 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2111 else
2112 r.y = max (0, s->row->y);
2115 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2117 /* If drawing the cursor, don't let glyph draw outside its
2118 advertised boundaries. Cleartype does this under some circumstances. */
2119 if (s->hl == DRAW_CURSOR)
2121 struct glyph *glyph = s->first_glyph;
2122 int height, max_y;
2124 if (s->x > r.x)
2126 if (r.width >= s->x - r.x)
2127 r.width -= s->x - r.x;
2128 else /* R2L hscrolled row with cursor outside text area */
2129 r.width = 0;
2130 r.x = s->x;
2132 r.width = min (r.width, glyph->pixel_width);
2134 /* If r.y is below window bottom, ensure that we still see a cursor. */
2135 height = min (glyph->ascent + glyph->descent,
2136 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2137 max_y = window_text_bottom_y (s->w) - height;
2138 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2139 if (s->ybase - glyph->ascent > max_y)
2141 r.y = max_y;
2142 r.height = height;
2144 else
2146 /* Don't draw cursor glyph taller than our actual glyph. */
2147 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2148 if (height < r.height)
2150 max_y = r.y + r.height;
2151 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2152 r.height = min (max_y - r.y, height);
2157 if (s->row->clip)
2159 XRectangle r_save = r;
2161 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2162 r.width = 0;
2165 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2166 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2168 #ifdef CONVERT_FROM_XRECT
2169 CONVERT_FROM_XRECT (r, *rects);
2170 #else
2171 *rects = r;
2172 #endif
2173 return 1;
2175 else
2177 /* If we are processing overlapping and allowed to return
2178 multiple clipping rectangles, we exclude the row of the glyph
2179 string from the clipping rectangle. This is to avoid drawing
2180 the same text on the environment with anti-aliasing. */
2181 #ifdef CONVERT_FROM_XRECT
2182 XRectangle rs[2];
2183 #else
2184 XRectangle *rs = rects;
2185 #endif
2186 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2188 if (s->for_overlaps & OVERLAPS_PRED)
2190 rs[i] = r;
2191 if (r.y + r.height > row_y)
2193 if (r.y < row_y)
2194 rs[i].height = row_y - r.y;
2195 else
2196 rs[i].height = 0;
2198 i++;
2200 if (s->for_overlaps & OVERLAPS_SUCC)
2202 rs[i] = r;
2203 if (r.y < row_y + s->row->visible_height)
2205 if (r.y + r.height > row_y + s->row->visible_height)
2207 rs[i].y = row_y + s->row->visible_height;
2208 rs[i].height = r.y + r.height - rs[i].y;
2210 else
2211 rs[i].height = 0;
2213 i++;
2216 n = i;
2217 #ifdef CONVERT_FROM_XRECT
2218 for (i = 0; i < n; i++)
2219 CONVERT_FROM_XRECT (rs[i], rects[i]);
2220 #endif
2221 return n;
2225 /* EXPORT:
2226 Return in *NR the clipping rectangle for glyph string S. */
2228 void
2229 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2231 get_glyph_string_clip_rects (s, nr, 1);
2235 /* EXPORT:
2236 Return the position and height of the phys cursor in window W.
2237 Set w->phys_cursor_width to width of phys cursor.
2240 void
2241 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2242 struct glyph *glyph, int *xp, int *yp, int *heightp)
2244 struct frame *f = XFRAME (WINDOW_FRAME (w));
2245 int x, y, wd, h, h0, y0, ascent;
2247 /* Compute the width of the rectangle to draw. If on a stretch
2248 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2249 rectangle as wide as the glyph, but use a canonical character
2250 width instead. */
2251 wd = glyph->pixel_width;
2253 x = w->phys_cursor.x;
2254 if (x < 0)
2256 wd += x;
2257 x = 0;
2260 if (glyph->type == STRETCH_GLYPH
2261 && !x_stretch_cursor_p)
2262 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2263 w->phys_cursor_width = wd;
2265 /* Don't let the hollow cursor glyph descend below the glyph row's
2266 ascent value, lest the hollow cursor looks funny. */
2267 y = w->phys_cursor.y;
2268 ascent = row->ascent;
2269 if (row->ascent < glyph->ascent)
2271 y -= glyph->ascent - row->ascent;
2272 ascent = glyph->ascent;
2275 /* If y is below window bottom, ensure that we still see a cursor. */
2276 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2278 h = max (h0, ascent + glyph->descent);
2279 h0 = min (h0, ascent + glyph->descent);
2281 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2282 if (y < y0)
2284 h = max (h - (y0 - y) + 1, h0);
2285 y = y0 - 1;
2287 else
2289 y0 = window_text_bottom_y (w) - h0;
2290 if (y > y0)
2292 h += y - y0;
2293 y = y0;
2297 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2298 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2299 *heightp = h;
2303 * Remember which glyph the mouse is over.
2306 void
2307 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2309 Lisp_Object window;
2310 struct window *w;
2311 struct glyph_row *r, *gr, *end_row;
2312 enum window_part part;
2313 enum glyph_row_area area;
2314 int x, y, width, height;
2316 /* Try to determine frame pixel position and size of the glyph under
2317 frame pixel coordinates X/Y on frame F. */
2319 if (window_resize_pixelwise)
2321 width = height = 1;
2322 goto virtual_glyph;
2324 else if (!f->glyphs_initialized_p
2325 || (window = window_from_coordinates (f, gx, gy, &part, false),
2326 NILP (window)))
2328 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2329 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2330 goto virtual_glyph;
2333 w = XWINDOW (window);
2334 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2335 height = WINDOW_FRAME_LINE_HEIGHT (w);
2337 x = window_relative_x_coord (w, part, gx);
2338 y = gy - WINDOW_TOP_EDGE_Y (w);
2340 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2341 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2343 if (w->pseudo_window_p)
2345 area = TEXT_AREA;
2346 part = ON_MODE_LINE; /* Don't adjust margin. */
2347 goto text_glyph;
2350 switch (part)
2352 case ON_LEFT_MARGIN:
2353 area = LEFT_MARGIN_AREA;
2354 goto text_glyph;
2356 case ON_RIGHT_MARGIN:
2357 area = RIGHT_MARGIN_AREA;
2358 goto text_glyph;
2360 case ON_HEADER_LINE:
2361 case ON_MODE_LINE:
2362 gr = (part == ON_HEADER_LINE
2363 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2364 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2365 gy = gr->y;
2366 area = TEXT_AREA;
2367 goto text_glyph_row_found;
2369 case ON_TEXT:
2370 area = TEXT_AREA;
2372 text_glyph:
2373 gr = 0; gy = 0;
2374 for (; r <= end_row && r->enabled_p; ++r)
2375 if (r->y + r->height > y)
2377 gr = r; gy = r->y;
2378 break;
2381 text_glyph_row_found:
2382 if (gr && gy <= y)
2384 struct glyph *g = gr->glyphs[area];
2385 struct glyph *end = g + gr->used[area];
2387 height = gr->height;
2388 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2389 if (gx + g->pixel_width > x)
2390 break;
2392 if (g < end)
2394 if (g->type == IMAGE_GLYPH)
2396 /* Don't remember when mouse is over image, as
2397 image may have hot-spots. */
2398 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2399 return;
2401 width = g->pixel_width;
2403 else
2405 /* Use nominal char spacing at end of line. */
2406 x -= gx;
2407 gx += (x / width) * width;
2410 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2412 gx += window_box_left_offset (w, area);
2413 /* Don't expand over the modeline to make sure the vertical
2414 drag cursor is shown early enough. */
2415 height = min (height,
2416 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2419 else
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2425 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2426 /* See comment above. */
2427 height = min (height,
2428 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2430 break;
2432 case ON_LEFT_FRINGE:
2433 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2434 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2435 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2436 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2437 goto row_glyph;
2439 case ON_RIGHT_FRINGE:
2440 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2441 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2442 : window_box_right_offset (w, TEXT_AREA));
2443 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2444 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2445 && !WINDOW_RIGHTMOST_P (w))
2446 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2447 /* Make sure the vertical border can get her own glyph to the
2448 right of the one we build here. */
2449 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2450 else
2451 width = WINDOW_PIXEL_WIDTH (w) - gx;
2452 else
2453 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2455 goto row_glyph;
2457 case ON_VERTICAL_BORDER:
2458 gx = WINDOW_PIXEL_WIDTH (w) - width;
2459 goto row_glyph;
2461 case ON_VERTICAL_SCROLL_BAR:
2462 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2464 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2465 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2466 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2467 : 0)));
2468 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2470 row_glyph:
2471 gr = 0, gy = 0;
2472 for (; r <= end_row && r->enabled_p; ++r)
2473 if (r->y + r->height > y)
2475 gr = r; gy = r->y;
2476 break;
2479 if (gr && gy <= y)
2480 height = gr->height;
2481 else
2483 /* Use nominal line height at end of window. */
2484 y -= gy;
2485 gy += (y / height) * height;
2487 break;
2489 case ON_RIGHT_DIVIDER:
2490 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2491 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2492 gy = 0;
2493 /* The bottom divider prevails. */
2494 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2495 goto add_edge;
2497 case ON_BOTTOM_DIVIDER:
2498 gx = 0;
2499 width = WINDOW_PIXEL_WIDTH (w);
2500 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2501 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2502 goto add_edge;
2504 default:
2506 virtual_glyph:
2507 /* If there is no glyph under the mouse, then we divide the screen
2508 into a grid of the smallest glyph in the frame, and use that
2509 as our "glyph". */
2511 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2512 round down even for negative values. */
2513 if (gx < 0)
2514 gx -= width - 1;
2515 if (gy < 0)
2516 gy -= height - 1;
2518 gx = (gx / width) * width;
2519 gy = (gy / height) * height;
2521 goto store_rect;
2524 add_edge:
2525 gx += WINDOW_LEFT_EDGE_X (w);
2526 gy += WINDOW_TOP_EDGE_Y (w);
2528 store_rect:
2529 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2531 /* Visible feedback for debugging. */
2532 #if false && defined HAVE_X_WINDOWS
2533 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2534 f->output_data.x->normal_gc,
2535 gx, gy, width, height);
2536 #endif
2540 #endif /* HAVE_WINDOW_SYSTEM */
2542 static void
2543 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2545 eassert (w);
2546 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2547 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2548 w->window_end_vpos
2549 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2552 static bool
2553 hscrolling_current_line_p (struct window *w)
2555 return (!w->suspend_auto_hscroll
2556 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2557 Qcurrent_line));
2560 /***********************************************************************
2561 Lisp form evaluation
2562 ***********************************************************************/
2564 /* Error handler for safe_eval and safe_call. */
2566 static Lisp_Object
2567 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2569 add_to_log ("Error during redisplay: %S signaled %S",
2570 Flist (nargs, args), arg);
2571 return Qnil;
2574 /* Call function FUNC with the rest of NARGS - 1 arguments
2575 following. Return the result, or nil if something went
2576 wrong. Prevent redisplay during the evaluation. */
2578 static Lisp_Object
2579 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2581 Lisp_Object val;
2583 if (inhibit_eval_during_redisplay)
2584 val = Qnil;
2585 else
2587 ptrdiff_t i;
2588 ptrdiff_t count = SPECPDL_INDEX ();
2589 Lisp_Object *args;
2590 USE_SAFE_ALLOCA;
2591 SAFE_ALLOCA_LISP (args, nargs);
2593 args[0] = func;
2594 for (i = 1; i < nargs; i++)
2595 args[i] = va_arg (ap, Lisp_Object);
2597 specbind (Qinhibit_redisplay, Qt);
2598 if (inhibit_quit)
2599 specbind (Qinhibit_quit, Qt);
2600 /* Use Qt to ensure debugger does not run,
2601 so there is no possibility of wanting to redisplay. */
2602 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2603 safe_eval_handler);
2604 SAFE_FREE ();
2605 val = unbind_to (count, val);
2608 return val;
2611 Lisp_Object
2612 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2614 Lisp_Object retval;
2615 va_list ap;
2617 va_start (ap, func);
2618 retval = safe__call (false, nargs, func, ap);
2619 va_end (ap);
2620 return retval;
2623 /* Call function FN with one argument ARG.
2624 Return the result, or nil if something went wrong. */
2626 Lisp_Object
2627 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2629 return safe_call (2, fn, arg);
2632 static Lisp_Object
2633 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2635 Lisp_Object retval;
2636 va_list ap;
2638 va_start (ap, fn);
2639 retval = safe__call (inhibit_quit, 2, fn, ap);
2640 va_end (ap);
2641 return retval;
2644 Lisp_Object
2645 safe_eval (Lisp_Object sexpr)
2647 return safe__call1 (false, Qeval, sexpr);
2650 static Lisp_Object
2651 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2653 return safe__call1 (inhibit_quit, Qeval, sexpr);
2656 /* Call function FN with two arguments ARG1 and ARG2.
2657 Return the result, or nil if something went wrong. */
2659 Lisp_Object
2660 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2662 return safe_call (3, fn, arg1, arg2);
2667 /***********************************************************************
2668 Debugging
2669 ***********************************************************************/
2671 /* Define CHECK_IT to perform sanity checks on iterators.
2672 This is for debugging. It is too slow to do unconditionally. */
2674 static void
2675 CHECK_IT (struct it *it)
2677 #if false
2678 if (it->method == GET_FROM_STRING)
2680 eassert (STRINGP (it->string));
2681 eassert (IT_STRING_CHARPOS (*it) >= 0);
2683 else
2685 eassert (IT_STRING_CHARPOS (*it) < 0);
2686 if (it->method == GET_FROM_BUFFER)
2688 /* Check that character and byte positions agree. */
2689 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2693 if (it->dpvec)
2694 eassert (it->current.dpvec_index >= 0);
2695 else
2696 eassert (it->current.dpvec_index < 0);
2697 #endif
2701 /* Check that the window end of window W is what we expect it
2702 to be---the last row in the current matrix displaying text. */
2704 static void
2705 CHECK_WINDOW_END (struct window *w)
2707 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2708 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2710 struct glyph_row *row;
2711 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2712 !row->enabled_p
2713 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2714 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2716 #endif
2719 /***********************************************************************
2720 Iterator initialization
2721 ***********************************************************************/
2723 /* Initialize IT for displaying current_buffer in window W, starting
2724 at character position CHARPOS. CHARPOS < 0 means that no buffer
2725 position is specified which is useful when the iterator is assigned
2726 a position later. BYTEPOS is the byte position corresponding to
2727 CHARPOS.
2729 If ROW is not null, calls to produce_glyphs with IT as parameter
2730 will produce glyphs in that row.
2732 BASE_FACE_ID is the id of a base face to use. It must be one of
2733 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2735 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2737 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2738 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2739 will be initialized to use the corresponding mode line glyph row of
2740 the desired matrix of W. */
2742 void
2743 init_iterator (struct it *it, struct window *w,
2744 ptrdiff_t charpos, ptrdiff_t bytepos,
2745 struct glyph_row *row, enum face_id base_face_id)
2747 enum face_id remapped_base_face_id = base_face_id;
2749 /* Some precondition checks. */
2750 eassert (w != NULL && it != NULL);
2751 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2752 && charpos <= ZV));
2754 /* If face attributes have been changed since the last redisplay,
2755 free realized faces now because they depend on face definitions
2756 that might have changed. Don't free faces while there might be
2757 desired matrices pending which reference these faces. */
2758 if (!inhibit_free_realized_faces)
2760 if (face_change)
2762 face_change = false;
2763 XFRAME (w->frame)->face_change = 0;
2764 free_all_realized_faces (Qnil);
2766 else if (XFRAME (w->frame)->face_change)
2768 XFRAME (w->frame)->face_change = 0;
2769 free_all_realized_faces (w->frame);
2773 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2774 if (! NILP (Vface_remapping_alist))
2775 remapped_base_face_id
2776 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2778 /* Use one of the mode line rows of W's desired matrix if
2779 appropriate. */
2780 if (row == NULL)
2782 if (base_face_id == MODE_LINE_FACE_ID
2783 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2784 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2785 else if (base_face_id == HEADER_LINE_FACE_ID)
2786 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2789 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2790 Other parts of redisplay rely on that. */
2791 memclear (it, sizeof *it);
2792 it->current.overlay_string_index = -1;
2793 it->current.dpvec_index = -1;
2794 it->base_face_id = remapped_base_face_id;
2795 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2796 it->paragraph_embedding = L2R;
2797 it->bidi_it.w = w;
2799 /* The window in which we iterate over current_buffer: */
2800 XSETWINDOW (it->window, w);
2801 it->w = w;
2802 it->f = XFRAME (w->frame);
2804 it->cmp_it.id = -1;
2806 /* Extra space between lines (on window systems only). */
2807 if (base_face_id == DEFAULT_FACE_ID
2808 && FRAME_WINDOW_P (it->f))
2810 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2811 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2812 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2813 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2814 * FRAME_LINE_HEIGHT (it->f));
2815 else if (it->f->extra_line_spacing > 0)
2816 it->extra_line_spacing = it->f->extra_line_spacing;
2819 /* If realized faces have been removed, e.g. because of face
2820 attribute changes of named faces, recompute them. When running
2821 in batch mode, the face cache of the initial frame is null. If
2822 we happen to get called, make a dummy face cache. */
2823 if (FRAME_FACE_CACHE (it->f) == NULL)
2824 init_frame_faces (it->f);
2825 if (FRAME_FACE_CACHE (it->f)->used == 0)
2826 recompute_basic_faces (it->f);
2828 it->override_ascent = -1;
2830 /* Are control characters displayed as `^C'? */
2831 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2833 /* -1 means everything between a CR and the following line end
2834 is invisible. >0 means lines indented more than this value are
2835 invisible. */
2836 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2837 ? (clip_to_bounds
2838 (-1, XINT (BVAR (current_buffer, selective_display)),
2839 PTRDIFF_MAX))
2840 : (!NILP (BVAR (current_buffer, selective_display))
2841 ? -1 : 0));
2842 it->selective_display_ellipsis_p
2843 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2845 /* Display table to use. */
2846 it->dp = window_display_table (w);
2848 /* Are multibyte characters enabled in current_buffer? */
2849 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2851 /* Get the position at which the redisplay_end_trigger hook should
2852 be run, if it is to be run at all. */
2853 if (MARKERP (w->redisplay_end_trigger)
2854 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2855 it->redisplay_end_trigger_charpos
2856 = marker_position (w->redisplay_end_trigger);
2857 else if (INTEGERP (w->redisplay_end_trigger))
2858 it->redisplay_end_trigger_charpos
2859 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2860 PTRDIFF_MAX);
2862 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2864 /* Are lines in the display truncated? */
2865 if (TRUNCATE != 0)
2866 it->line_wrap = TRUNCATE;
2867 if (base_face_id == DEFAULT_FACE_ID
2868 && !it->w->hscroll
2869 && (WINDOW_FULL_WIDTH_P (it->w)
2870 || NILP (Vtruncate_partial_width_windows)
2871 || (INTEGERP (Vtruncate_partial_width_windows)
2872 /* PXW: Shall we do something about this? */
2873 && (XINT (Vtruncate_partial_width_windows)
2874 <= WINDOW_TOTAL_COLS (it->w))))
2875 && NILP (BVAR (current_buffer, truncate_lines)))
2876 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2877 ? WINDOW_WRAP : WORD_WRAP;
2879 /* Get dimensions of truncation and continuation glyphs. These are
2880 displayed as fringe bitmaps under X, but we need them for such
2881 frames when the fringes are turned off. The no_special_glyphs slot
2882 of the iterator's frame, when set, suppresses their display - by
2883 default for tooltip frames and when set via the 'no-special-glyphs'
2884 frame parameter. */
2885 #ifdef HAVE_WINDOW_SYSTEM
2886 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2887 #endif
2889 if (it->line_wrap == TRUNCATE)
2891 /* We will need the truncation glyph. */
2892 eassert (it->glyph_row == NULL);
2893 produce_special_glyphs (it, IT_TRUNCATION);
2894 it->truncation_pixel_width = it->pixel_width;
2896 else
2898 /* We will need the continuation glyph. */
2899 eassert (it->glyph_row == NULL);
2900 produce_special_glyphs (it, IT_CONTINUATION);
2901 it->continuation_pixel_width = it->pixel_width;
2905 /* Reset these values to zero because the produce_special_glyphs
2906 above has changed them. */
2907 it->pixel_width = it->ascent = it->descent = 0;
2908 it->phys_ascent = it->phys_descent = 0;
2910 /* Set this after getting the dimensions of truncation and
2911 continuation glyphs, so that we don't produce glyphs when calling
2912 produce_special_glyphs, above. */
2913 it->glyph_row = row;
2914 it->area = TEXT_AREA;
2916 /* Get the dimensions of the display area. The display area
2917 consists of the visible window area plus a horizontally scrolled
2918 part to the left of the window. All x-values are relative to the
2919 start of this total display area. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 /* Mode lines, menu bar in terminal frames. */
2923 it->first_visible_x = 0;
2924 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2926 else
2928 /* When hscrolling only the current line, don't apply the
2929 hscroll here, it will be applied by display_line when it gets
2930 to laying out the line showing point. However, if the
2931 window's min_hscroll is positive, the user specified a lower
2932 bound for automatic hscrolling, so they expect the
2933 non-current lines to obey that hscroll amount. */
2934 if (hscrolling_current_line_p (w))
2936 if (w->min_hscroll > 0)
2937 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2938 else
2939 it->first_visible_x = 0;
2941 else
2942 it->first_visible_x =
2943 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2944 it->last_visible_x = (it->first_visible_x
2945 + window_box_width (w, TEXT_AREA));
2947 /* If we truncate lines, leave room for the truncation glyph(s) at
2948 the right margin. Otherwise, leave room for the continuation
2949 glyph(s). Done only if the window has no right fringe. */
2950 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2952 if (it->line_wrap == TRUNCATE)
2953 it->last_visible_x -= it->truncation_pixel_width;
2954 else
2955 it->last_visible_x -= it->continuation_pixel_width;
2958 it->header_line_p = window_wants_header_line (w);
2959 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2962 /* Leave room for a border glyph. */
2963 if (!FRAME_WINDOW_P (it->f)
2964 && !WINDOW_RIGHTMOST_P (it->w))
2965 it->last_visible_x -= 1;
2967 it->last_visible_y = window_text_bottom_y (w);
2969 /* For mode lines and alike, arrange for the first glyph having a
2970 left box line if the face specifies a box. */
2971 if (base_face_id != DEFAULT_FACE_ID)
2973 struct face *face;
2975 it->face_id = remapped_base_face_id;
2977 /* If we have a boxed mode line, make the first character appear
2978 with a left box line. */
2979 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2980 if (face && face->box != FACE_NO_BOX)
2981 it->start_of_box_run_p = true;
2984 /* If a buffer position was specified, set the iterator there,
2985 getting overlays and face properties from that position. */
2986 if (charpos >= BUF_BEG (current_buffer))
2988 it->stop_charpos = charpos;
2989 it->end_charpos = ZV;
2990 eassert (charpos == BYTE_TO_CHAR (bytepos));
2991 IT_CHARPOS (*it) = charpos;
2992 IT_BYTEPOS (*it) = bytepos;
2994 /* We will rely on `reseat' to set this up properly, via
2995 handle_face_prop. */
2996 it->face_id = it->base_face_id;
2998 it->start = it->current;
2999 /* Do we need to reorder bidirectional text? Not if this is a
3000 unibyte buffer: by definition, none of the single-byte
3001 characters are strong R2L, so no reordering is needed. And
3002 bidi.c doesn't support unibyte buffers anyway. Also, don't
3003 reorder while we are loading loadup.el, since the tables of
3004 character properties needed for reordering are not yet
3005 available. */
3006 it->bidi_p =
3007 !redisplay__inhibit_bidi
3008 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3009 && it->multibyte_p;
3011 /* If we are to reorder bidirectional text, init the bidi
3012 iterator. */
3013 if (it->bidi_p)
3015 /* Since we don't know at this point whether there will be
3016 any R2L lines in the window, we reserve space for
3017 truncation/continuation glyphs even if only the left
3018 fringe is absent. */
3019 if (base_face_id == DEFAULT_FACE_ID
3020 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3021 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3023 if (it->line_wrap == TRUNCATE)
3024 it->last_visible_x -= it->truncation_pixel_width;
3025 else
3026 it->last_visible_x -= it->continuation_pixel_width;
3028 /* Note the paragraph direction that this buffer wants to
3029 use. */
3030 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3031 Qleft_to_right))
3032 it->paragraph_embedding = L2R;
3033 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3034 Qright_to_left))
3035 it->paragraph_embedding = R2L;
3036 else
3037 it->paragraph_embedding = NEUTRAL_DIR;
3038 bidi_unshelve_cache (NULL, false);
3039 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3040 &it->bidi_it);
3043 /* Compute faces etc. */
3044 reseat (it, it->current.pos, true);
3047 CHECK_IT (it);
3051 /* Initialize IT for the display of window W with window start POS. */
3053 void
3054 start_display (struct it *it, struct window *w, struct text_pos pos)
3056 struct glyph_row *row;
3057 bool first_vpos = window_wants_header_line (w);
3059 row = w->desired_matrix->rows + first_vpos;
3060 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3061 it->first_vpos = first_vpos;
3063 /* Don't reseat to previous visible line start if current start
3064 position is in a string or image. */
3065 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3067 int first_y = it->current_y;
3069 /* If window start is not at a line start, skip forward to POS to
3070 get the correct continuation lines width. */
3071 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3072 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3073 if (!start_at_line_beg_p)
3075 int new_x;
3077 reseat_at_previous_visible_line_start (it);
3078 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3080 new_x = it->current_x + it->pixel_width;
3082 /* If lines are continued, this line may end in the middle
3083 of a multi-glyph character (e.g. a control character
3084 displayed as \003, or in the middle of an overlay
3085 string). In this case move_it_to above will not have
3086 taken us to the start of the continuation line but to the
3087 end of the continued line. */
3088 if (it->current_x > 0
3089 && it->line_wrap != TRUNCATE /* Lines are continued. */
3090 && (/* And glyph doesn't fit on the line. */
3091 new_x > it->last_visible_x
3092 /* Or it fits exactly and we're on a window
3093 system frame. */
3094 || (new_x == it->last_visible_x
3095 && FRAME_WINDOW_P (it->f)
3096 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3097 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3098 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3100 if ((it->current.dpvec_index >= 0
3101 || it->current.overlay_string_index >= 0)
3102 /* If we are on a newline from a display vector or
3103 overlay string, then we are already at the end of
3104 a screen line; no need to go to the next line in
3105 that case, as this line is not really continued.
3106 (If we do go to the next line, C-e will not DTRT.) */
3107 && it->c != '\n')
3109 set_iterator_to_next (it, true);
3110 move_it_in_display_line_to (it, -1, -1, 0);
3113 it->continuation_lines_width += it->current_x;
3115 /* If the character at POS is displayed via a display
3116 vector, move_it_to above stops at the final glyph of
3117 IT->dpvec. To make the caller redisplay that character
3118 again (a.k.a. start at POS), we need to reset the
3119 dpvec_index to the beginning of IT->dpvec. */
3120 else if (it->current.dpvec_index >= 0)
3121 it->current.dpvec_index = 0;
3123 /* We're starting a new display line, not affected by the
3124 height of the continued line, so clear the appropriate
3125 fields in the iterator structure. */
3126 it->max_ascent = it->max_descent = 0;
3127 it->max_phys_ascent = it->max_phys_descent = 0;
3129 it->current_y = first_y;
3130 it->vpos = 0;
3131 it->current_x = it->hpos = 0;
3137 /* Return true if POS is a position in ellipses displayed for invisible
3138 text. W is the window we display, for text property lookup. */
3140 static bool
3141 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3143 Lisp_Object prop, window;
3144 bool ellipses_p = false;
3145 ptrdiff_t charpos = CHARPOS (pos->pos);
3147 /* If POS specifies a position in a display vector, this might
3148 be for an ellipsis displayed for invisible text. We won't
3149 get the iterator set up for delivering that ellipsis unless
3150 we make sure that it gets aware of the invisible text. */
3151 if (pos->dpvec_index >= 0
3152 && pos->overlay_string_index < 0
3153 && CHARPOS (pos->string_pos) < 0
3154 && charpos > BEGV
3155 && (XSETWINDOW (window, w),
3156 prop = Fget_char_property (make_number (charpos),
3157 Qinvisible, window),
3158 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3160 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3161 window);
3162 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3165 return ellipses_p;
3169 /* Initialize IT for stepping through current_buffer in window W,
3170 starting at position POS that includes overlay string and display
3171 vector/ control character translation position information. Value
3172 is false if there are overlay strings with newlines at POS. */
3174 static bool
3175 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3177 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3178 int i;
3179 bool overlay_strings_with_newlines = false;
3181 /* If POS specifies a position in a display vector, this might
3182 be for an ellipsis displayed for invisible text. We won't
3183 get the iterator set up for delivering that ellipsis unless
3184 we make sure that it gets aware of the invisible text. */
3185 if (in_ellipses_for_invisible_text_p (pos, w))
3187 --charpos;
3188 bytepos = 0;
3191 /* Keep in mind: the call to reseat in init_iterator skips invisible
3192 text, so we might end up at a position different from POS. This
3193 is only a problem when POS is a row start after a newline and an
3194 overlay starts there with an after-string, and the overlay has an
3195 invisible property. Since we don't skip invisible text in
3196 display_line and elsewhere immediately after consuming the
3197 newline before the row start, such a POS will not be in a string,
3198 but the call to init_iterator below will move us to the
3199 after-string. */
3200 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3202 /* This only scans the current chunk -- it should scan all chunks.
3203 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3204 to 16 in 22.1 to make this a lesser problem. */
3205 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3207 const char *s = SSDATA (it->overlay_strings[i]);
3208 const char *e = s + SBYTES (it->overlay_strings[i]);
3210 while (s < e && *s != '\n')
3211 ++s;
3213 if (s < e)
3215 overlay_strings_with_newlines = true;
3216 break;
3220 /* If position is within an overlay string, set up IT to the right
3221 overlay string. */
3222 if (pos->overlay_string_index >= 0)
3224 int relative_index;
3226 /* If the first overlay string happens to have a `display'
3227 property for an image, the iterator will be set up for that
3228 image, and we have to undo that setup first before we can
3229 correct the overlay string index. */
3230 if (it->method == GET_FROM_IMAGE)
3231 pop_it (it);
3233 /* We already have the first chunk of overlay strings in
3234 IT->overlay_strings. Load more until the one for
3235 pos->overlay_string_index is in IT->overlay_strings. */
3236 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3238 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3239 it->current.overlay_string_index = 0;
3240 while (n--)
3242 load_overlay_strings (it, 0);
3243 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3247 it->current.overlay_string_index = pos->overlay_string_index;
3248 relative_index = (it->current.overlay_string_index
3249 % OVERLAY_STRING_CHUNK_SIZE);
3250 it->string = it->overlay_strings[relative_index];
3251 eassert (STRINGP (it->string));
3252 it->current.string_pos = pos->string_pos;
3253 it->method = GET_FROM_STRING;
3254 it->end_charpos = SCHARS (it->string);
3255 /* Set up the bidi iterator for this overlay string. */
3256 if (it->bidi_p)
3258 it->bidi_it.string.lstring = it->string;
3259 it->bidi_it.string.s = NULL;
3260 it->bidi_it.string.schars = SCHARS (it->string);
3261 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3262 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3263 it->bidi_it.string.unibyte = !it->multibyte_p;
3264 it->bidi_it.w = it->w;
3265 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3266 FRAME_WINDOW_P (it->f), &it->bidi_it);
3268 /* Synchronize the state of the bidi iterator with
3269 pos->string_pos. For any string position other than
3270 zero, this will be done automagically when we resume
3271 iteration over the string and get_visually_first_element
3272 is called. But if string_pos is zero, and the string is
3273 to be reordered for display, we need to resync manually,
3274 since it could be that the iteration state recorded in
3275 pos ended at string_pos of 0 moving backwards in string. */
3276 if (CHARPOS (pos->string_pos) == 0)
3278 get_visually_first_element (it);
3279 if (IT_STRING_CHARPOS (*it) != 0)
3280 do {
3281 /* Paranoia. */
3282 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3283 bidi_move_to_visually_next (&it->bidi_it);
3284 } while (it->bidi_it.charpos != 0);
3286 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3287 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3291 if (CHARPOS (pos->string_pos) >= 0)
3293 /* Recorded position is not in an overlay string, but in another
3294 string. This can only be a string from a `display' property.
3295 IT should already be filled with that string. */
3296 it->current.string_pos = pos->string_pos;
3297 eassert (STRINGP (it->string));
3298 if (it->bidi_p)
3299 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3300 FRAME_WINDOW_P (it->f), &it->bidi_it);
3303 /* Restore position in display vector translations, control
3304 character translations or ellipses. */
3305 if (pos->dpvec_index >= 0)
3307 if (it->dpvec == NULL)
3308 get_next_display_element (it);
3309 eassert (it->dpvec && it->current.dpvec_index == 0);
3310 it->current.dpvec_index = pos->dpvec_index;
3313 CHECK_IT (it);
3314 return !overlay_strings_with_newlines;
3318 /* Initialize IT for stepping through current_buffer in window W
3319 starting at ROW->start. */
3321 static void
3322 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3324 init_from_display_pos (it, w, &row->start);
3325 it->start = row->start;
3326 it->continuation_lines_width = row->continuation_lines_width;
3327 CHECK_IT (it);
3331 /* Initialize IT for stepping through current_buffer in window W
3332 starting in the line following ROW, i.e. starting at ROW->end.
3333 Value is false if there are overlay strings with newlines at ROW's
3334 end position. */
3336 static bool
3337 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3339 bool success = false;
3341 if (init_from_display_pos (it, w, &row->end))
3343 if (row->continued_p)
3344 it->continuation_lines_width
3345 = row->continuation_lines_width + row->pixel_width;
3346 CHECK_IT (it);
3347 success = true;
3350 return success;
3356 /***********************************************************************
3357 Text properties
3358 ***********************************************************************/
3360 /* Called when IT reaches IT->stop_charpos. Handle text property and
3361 overlay changes. Set IT->stop_charpos to the next position where
3362 to stop. */
3364 static void
3365 handle_stop (struct it *it)
3367 enum prop_handled handled;
3368 bool handle_overlay_change_p;
3369 struct props *p;
3371 it->dpvec = NULL;
3372 it->current.dpvec_index = -1;
3373 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3374 it->ellipsis_p = false;
3376 /* Use face of preceding text for ellipsis (if invisible) */
3377 if (it->selective_display_ellipsis_p)
3378 it->saved_face_id = it->face_id;
3380 /* Here's the description of the semantics of, and the logic behind,
3381 the various HANDLED_* statuses:
3383 HANDLED_NORMALLY means the handler did its job, and the loop
3384 should proceed to calling the next handler in order.
3386 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3387 change in the properties and overlays at current position, so the
3388 loop should be restarted, to re-invoke the handlers that were
3389 already called. This happens when fontification-functions were
3390 called by handle_fontified_prop, and actually fontified
3391 something. Another case where HANDLED_RECOMPUTE_PROPS is
3392 returned is when we discover overlay strings that need to be
3393 displayed right away. The loop below will continue for as long
3394 as the status is HANDLED_RECOMPUTE_PROPS.
3396 HANDLED_RETURN means return immediately to the caller, to
3397 continue iteration without calling any further handlers. This is
3398 used when we need to act on some property right away, for example
3399 when we need to display the ellipsis or a replacing display
3400 property, such as display string or image.
3402 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3403 consumed, and the handler switched to the next overlay string.
3404 This signals the loop below to refrain from looking for more
3405 overlays before all the overlay strings of the current overlay
3406 are processed.
3408 Some of the handlers called by the loop push the iterator state
3409 onto the stack (see 'push_it'), and arrange for the iteration to
3410 continue with another object, such as an image, a display string,
3411 or an overlay string. In most such cases, it->stop_charpos is
3412 set to the first character of the string, so that when the
3413 iteration resumes, this function will immediately be called
3414 again, to examine the properties at the beginning of the string.
3416 When a display or overlay string is exhausted, the iterator state
3417 is popped (see 'pop_it'), and iteration continues with the
3418 previous object. Again, in many such cases this function is
3419 called again to find the next position where properties might
3420 change. */
3424 handled = HANDLED_NORMALLY;
3426 /* Call text property handlers. */
3427 for (p = it_props; p->handler; ++p)
3429 handled = p->handler (it);
3431 if (handled == HANDLED_RECOMPUTE_PROPS)
3432 break;
3433 else if (handled == HANDLED_RETURN)
3435 /* We still want to show before and after strings from
3436 overlays even if the actual buffer text is replaced. */
3437 if (!handle_overlay_change_p
3438 || it->sp > 1
3439 /* Don't call get_overlay_strings_1 if we already
3440 have overlay strings loaded, because doing so
3441 will load them again and push the iterator state
3442 onto the stack one more time, which is not
3443 expected by the rest of the code that processes
3444 overlay strings. */
3445 || (it->current.overlay_string_index < 0
3446 && !get_overlay_strings_1 (it, 0, false)))
3448 if (it->ellipsis_p)
3449 setup_for_ellipsis (it, 0);
3450 /* When handling a display spec, we might load an
3451 empty string. In that case, discard it here. We
3452 used to discard it in handle_single_display_spec,
3453 but that causes get_overlay_strings_1, above, to
3454 ignore overlay strings that we must check. */
3455 if (STRINGP (it->string) && !SCHARS (it->string))
3456 pop_it (it);
3457 return;
3459 else if (STRINGP (it->string) && !SCHARS (it->string))
3460 pop_it (it);
3461 else
3463 it->string_from_display_prop_p = false;
3464 it->from_disp_prop_p = false;
3465 handle_overlay_change_p = false;
3467 handled = HANDLED_RECOMPUTE_PROPS;
3468 break;
3470 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3471 handle_overlay_change_p = false;
3474 if (handled != HANDLED_RECOMPUTE_PROPS)
3476 /* Don't check for overlay strings below when set to deliver
3477 characters from a display vector. */
3478 if (it->method == GET_FROM_DISPLAY_VECTOR)
3479 handle_overlay_change_p = false;
3481 /* Handle overlay changes.
3482 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3483 if it finds overlays. */
3484 if (handle_overlay_change_p)
3485 handled = handle_overlay_change (it);
3488 if (it->ellipsis_p)
3490 setup_for_ellipsis (it, 0);
3491 break;
3494 while (handled == HANDLED_RECOMPUTE_PROPS);
3496 /* Determine where to stop next. */
3497 if (handled == HANDLED_NORMALLY)
3498 compute_stop_pos (it);
3502 /* Compute IT->stop_charpos from text property and overlay change
3503 information for IT's current position. */
3505 static void
3506 compute_stop_pos (struct it *it)
3508 register INTERVAL iv, next_iv;
3509 Lisp_Object object, limit, position;
3510 ptrdiff_t charpos, bytepos;
3512 if (STRINGP (it->string))
3514 /* Strings are usually short, so don't limit the search for
3515 properties. */
3516 it->stop_charpos = it->end_charpos;
3517 object = it->string;
3518 limit = Qnil;
3519 charpos = IT_STRING_CHARPOS (*it);
3520 bytepos = IT_STRING_BYTEPOS (*it);
3522 else
3524 ptrdiff_t pos;
3526 /* If end_charpos is out of range for some reason, such as a
3527 misbehaving display function, rationalize it (Bug#5984). */
3528 if (it->end_charpos > ZV)
3529 it->end_charpos = ZV;
3530 it->stop_charpos = it->end_charpos;
3532 /* If next overlay change is in front of the current stop pos
3533 (which is IT->end_charpos), stop there. Note: value of
3534 next_overlay_change is point-max if no overlay change
3535 follows. */
3536 charpos = IT_CHARPOS (*it);
3537 bytepos = IT_BYTEPOS (*it);
3538 pos = next_overlay_change (charpos);
3539 if (pos < it->stop_charpos)
3540 it->stop_charpos = pos;
3542 /* Set up variables for computing the stop position from text
3543 property changes. */
3544 XSETBUFFER (object, current_buffer);
3545 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3548 /* Get the interval containing IT's position. Value is a null
3549 interval if there isn't such an interval. */
3550 position = make_number (charpos);
3551 iv = validate_interval_range (object, &position, &position, false);
3552 if (iv)
3554 Lisp_Object values_here[LAST_PROP_IDX];
3555 struct props *p;
3557 /* Get properties here. */
3558 for (p = it_props; p->handler; ++p)
3559 values_here[p->idx] = textget (iv->plist,
3560 builtin_lisp_symbol (p->name));
3562 /* Look for an interval following iv that has different
3563 properties. */
3564 for (next_iv = next_interval (iv);
3565 (next_iv
3566 && (NILP (limit)
3567 || XFASTINT (limit) > next_iv->position));
3568 next_iv = next_interval (next_iv))
3570 for (p = it_props; p->handler; ++p)
3572 Lisp_Object new_value = textget (next_iv->plist,
3573 builtin_lisp_symbol (p->name));
3574 if (!EQ (values_here[p->idx], new_value))
3575 break;
3578 if (p->handler)
3579 break;
3582 if (next_iv)
3584 if (INTEGERP (limit)
3585 && next_iv->position >= XFASTINT (limit))
3586 /* No text property change up to limit. */
3587 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3588 else
3589 /* Text properties change in next_iv. */
3590 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3594 if (it->cmp_it.id < 0)
3596 ptrdiff_t stoppos = it->end_charpos;
3598 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3599 stoppos = -1;
3600 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3601 stoppos, it->string);
3604 eassert (STRINGP (it->string)
3605 || (it->stop_charpos >= BEGV
3606 && it->stop_charpos >= IT_CHARPOS (*it)));
3610 /* Return the position of the next overlay change after POS in
3611 current_buffer. Value is point-max if no overlay change
3612 follows. This is like `next-overlay-change' but doesn't use
3613 xmalloc. */
3615 static ptrdiff_t
3616 next_overlay_change (ptrdiff_t pos)
3618 ptrdiff_t i, noverlays;
3619 ptrdiff_t endpos;
3620 Lisp_Object *overlays;
3621 USE_SAFE_ALLOCA;
3623 /* Get all overlays at the given position. */
3624 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3626 /* If any of these overlays ends before endpos,
3627 use its ending point instead. */
3628 for (i = 0; i < noverlays; ++i)
3630 Lisp_Object oend;
3631 ptrdiff_t oendpos;
3633 oend = OVERLAY_END (overlays[i]);
3634 oendpos = OVERLAY_POSITION (oend);
3635 endpos = min (endpos, oendpos);
3638 SAFE_FREE ();
3639 return endpos;
3642 /* How many characters forward to search for a display property or
3643 display string. Searching too far forward makes the bidi display
3644 sluggish, especially in small windows. */
3645 #define MAX_DISP_SCAN 250
3647 /* Return the character position of a display string at or after
3648 position specified by POSITION. If no display string exists at or
3649 after POSITION, return ZV. A display string is either an overlay
3650 with `display' property whose value is a string, or a `display'
3651 text property whose value is a string. STRING is data about the
3652 string to iterate; if STRING->lstring is nil, we are iterating a
3653 buffer. FRAME_WINDOW_P is true when we are displaying a window
3654 on a GUI frame. DISP_PROP is set to zero if we searched
3655 MAX_DISP_SCAN characters forward without finding any display
3656 strings, non-zero otherwise. It is set to 2 if the display string
3657 uses any kind of `(space ...)' spec that will produce a stretch of
3658 white space in the text area. */
3659 ptrdiff_t
3660 compute_display_string_pos (struct text_pos *position,
3661 struct bidi_string_data *string,
3662 struct window *w,
3663 bool frame_window_p, int *disp_prop)
3665 /* OBJECT = nil means current buffer. */
3666 Lisp_Object object, object1;
3667 Lisp_Object pos, spec, limpos;
3668 bool string_p = string && (STRINGP (string->lstring) || string->s);
3669 ptrdiff_t eob = string_p ? string->schars : ZV;
3670 ptrdiff_t begb = string_p ? 0 : BEGV;
3671 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3672 ptrdiff_t lim =
3673 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3674 struct text_pos tpos;
3675 int rv = 0;
3677 if (string && STRINGP (string->lstring))
3678 object1 = object = string->lstring;
3679 else if (w && !string_p)
3681 XSETWINDOW (object, w);
3682 object1 = Qnil;
3684 else
3685 object1 = object = Qnil;
3687 *disp_prop = 1;
3689 if (charpos >= eob
3690 /* We don't support display properties whose values are strings
3691 that have display string properties. */
3692 || string->from_disp_str
3693 /* C strings cannot have display properties. */
3694 || (string->s && !STRINGP (object)))
3696 *disp_prop = 0;
3697 return eob;
3700 /* If the character at CHARPOS is where the display string begins,
3701 return CHARPOS. */
3702 pos = make_number (charpos);
3703 if (STRINGP (object))
3704 bufpos = string->bufpos;
3705 else
3706 bufpos = charpos;
3707 tpos = *position;
3708 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3709 && (charpos <= begb
3710 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3711 object),
3712 spec))
3713 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3714 frame_window_p)))
3716 if (rv == 2)
3717 *disp_prop = 2;
3718 return charpos;
3721 /* Look forward for the first character with a `display' property
3722 that will replace the underlying text when displayed. */
3723 limpos = make_number (lim);
3724 do {
3725 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3726 CHARPOS (tpos) = XFASTINT (pos);
3727 if (CHARPOS (tpos) >= lim)
3729 *disp_prop = 0;
3730 break;
3732 if (STRINGP (object))
3733 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3734 else
3735 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3736 spec = Fget_char_property (pos, Qdisplay, object);
3737 if (!STRINGP (object))
3738 bufpos = CHARPOS (tpos);
3739 } while (NILP (spec)
3740 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3741 bufpos, frame_window_p)));
3742 if (rv == 2)
3743 *disp_prop = 2;
3745 return CHARPOS (tpos);
3748 /* Return the character position of the end of the display string that
3749 started at CHARPOS. If there's no display string at CHARPOS,
3750 return -1. A display string is either an overlay with `display'
3751 property whose value is a string or a `display' text property whose
3752 value is a string. */
3753 ptrdiff_t
3754 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3756 /* OBJECT = nil means current buffer. */
3757 Lisp_Object object =
3758 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3759 Lisp_Object pos = make_number (charpos);
3760 ptrdiff_t eob =
3761 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3763 if (charpos >= eob || (string->s && !STRINGP (object)))
3764 return eob;
3766 /* It could happen that the display property or overlay was removed
3767 since we found it in compute_display_string_pos above. One way
3768 this can happen is if JIT font-lock was called (through
3769 handle_fontified_prop), and jit-lock-functions remove text
3770 properties or overlays from the portion of buffer that includes
3771 CHARPOS. Muse mode is known to do that, for example. In this
3772 case, we return -1 to the caller, to signal that no display
3773 string is actually present at CHARPOS. See bidi_fetch_char for
3774 how this is handled.
3776 An alternative would be to never look for display properties past
3777 it->stop_charpos. But neither compute_display_string_pos nor
3778 bidi_fetch_char that calls it know or care where the next
3779 stop_charpos is. */
3780 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3781 return -1;
3783 /* Look forward for the first character where the `display' property
3784 changes. */
3785 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3787 return XFASTINT (pos);
3792 /***********************************************************************
3793 Fontification
3794 ***********************************************************************/
3796 /* Handle changes in the `fontified' property of the current buffer by
3797 calling hook functions from Qfontification_functions to fontify
3798 regions of text. */
3800 static enum prop_handled
3801 handle_fontified_prop (struct it *it)
3803 Lisp_Object prop, pos;
3804 enum prop_handled handled = HANDLED_NORMALLY;
3806 if (!NILP (Vmemory_full))
3807 return handled;
3809 /* Get the value of the `fontified' property at IT's current buffer
3810 position. (The `fontified' property doesn't have a special
3811 meaning in strings.) If the value is nil, call functions from
3812 Qfontification_functions. */
3813 if (!STRINGP (it->string)
3814 && it->s == NULL
3815 && !NILP (Vfontification_functions)
3816 && !NILP (Vrun_hooks)
3817 && (pos = make_number (IT_CHARPOS (*it)),
3818 prop = Fget_char_property (pos, Qfontified, Qnil),
3819 /* Ignore the special cased nil value always present at EOB since
3820 no amount of fontifying will be able to change it. */
3821 NILP (prop) && IT_CHARPOS (*it) < Z))
3823 ptrdiff_t count = SPECPDL_INDEX ();
3824 Lisp_Object val;
3825 struct buffer *obuf = current_buffer;
3826 ptrdiff_t begv = BEGV, zv = ZV;
3827 bool old_clip_changed = current_buffer->clip_changed;
3829 val = Vfontification_functions;
3830 specbind (Qfontification_functions, Qnil);
3832 eassert (it->end_charpos == ZV);
3834 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3835 safe_call1 (val, pos);
3836 else
3838 Lisp_Object fns, fn;
3840 fns = Qnil;
3842 for (; CONSP (val); val = XCDR (val))
3844 fn = XCAR (val);
3846 if (EQ (fn, Qt))
3848 /* A value of t indicates this hook has a local
3849 binding; it means to run the global binding too.
3850 In a global value, t should not occur. If it
3851 does, we must ignore it to avoid an endless
3852 loop. */
3853 for (fns = Fdefault_value (Qfontification_functions);
3854 CONSP (fns);
3855 fns = XCDR (fns))
3857 fn = XCAR (fns);
3858 if (!EQ (fn, Qt))
3859 safe_call1 (fn, pos);
3862 else
3863 safe_call1 (fn, pos);
3867 unbind_to (count, Qnil);
3869 /* Fontification functions routinely call `save-restriction'.
3870 Normally, this tags clip_changed, which can confuse redisplay
3871 (see discussion in Bug#6671). Since we don't perform any
3872 special handling of fontification changes in the case where
3873 `save-restriction' isn't called, there's no point doing so in
3874 this case either. So, if the buffer's restrictions are
3875 actually left unchanged, reset clip_changed. */
3876 if (obuf == current_buffer)
3878 if (begv == BEGV && zv == ZV)
3879 current_buffer->clip_changed = old_clip_changed;
3881 /* There isn't much we can reasonably do to protect against
3882 misbehaving fontification, but here's a fig leaf. */
3883 else if (BUFFER_LIVE_P (obuf))
3884 set_buffer_internal_1 (obuf);
3886 /* The fontification code may have added/removed text.
3887 It could do even a lot worse, but let's at least protect against
3888 the most obvious case where only the text past `pos' gets changed',
3889 as is/was done in grep.el where some escapes sequences are turned
3890 into face properties (bug#7876). */
3891 it->end_charpos = ZV;
3893 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3894 something. This avoids an endless loop if they failed to
3895 fontify the text for which reason ever. */
3896 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3897 handled = HANDLED_RECOMPUTE_PROPS;
3900 return handled;
3905 /***********************************************************************
3906 Faces
3907 ***********************************************************************/
3909 /* Set up iterator IT from face properties at its current position.
3910 Called from handle_stop. */
3912 static enum prop_handled
3913 handle_face_prop (struct it *it)
3915 int new_face_id;
3916 ptrdiff_t next_stop;
3918 if (!STRINGP (it->string))
3920 new_face_id
3921 = face_at_buffer_position (it->w,
3922 IT_CHARPOS (*it),
3923 &next_stop,
3924 (IT_CHARPOS (*it)
3925 + TEXT_PROP_DISTANCE_LIMIT),
3926 false, it->base_face_id);
3928 /* Is this a start of a run of characters with box face?
3929 Caveat: this can be called for a freshly initialized
3930 iterator; face_id is -1 in this case. We know that the new
3931 face will not change until limit, i.e. if the new face has a
3932 box, all characters up to limit will have one. But, as
3933 usual, we don't know whether limit is really the end. */
3934 if (new_face_id != it->face_id)
3936 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3937 /* If it->face_id is -1, old_face below will be NULL, see
3938 the definition of FACE_FROM_ID_OR_NULL. This will happen
3939 if this is the initial call that gets the face. */
3940 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3942 /* If the value of face_id of the iterator is -1, we have to
3943 look in front of IT's position and see whether there is a
3944 face there that's different from new_face_id. */
3945 if (!old_face && IT_CHARPOS (*it) > BEG)
3947 int prev_face_id = face_before_it_pos (it);
3949 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3952 /* If the new face has a box, but the old face does not,
3953 this is the start of a run of characters with box face,
3954 i.e. this character has a shadow on the left side. */
3955 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3956 && (old_face == NULL || !old_face->box));
3957 it->face_box_p = new_face->box != FACE_NO_BOX;
3960 else
3962 int base_face_id;
3963 ptrdiff_t bufpos;
3964 int i;
3965 Lisp_Object from_overlay
3966 = (it->current.overlay_string_index >= 0
3967 ? it->string_overlays[it->current.overlay_string_index
3968 % OVERLAY_STRING_CHUNK_SIZE]
3969 : Qnil);
3971 /* See if we got to this string directly or indirectly from
3972 an overlay property. That includes the before-string or
3973 after-string of an overlay, strings in display properties
3974 provided by an overlay, their text properties, etc.
3976 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3977 if (! NILP (from_overlay))
3978 for (i = it->sp - 1; i >= 0; i--)
3980 if (it->stack[i].current.overlay_string_index >= 0)
3981 from_overlay
3982 = it->string_overlays[it->stack[i].current.overlay_string_index
3983 % OVERLAY_STRING_CHUNK_SIZE];
3984 else if (! NILP (it->stack[i].from_overlay))
3985 from_overlay = it->stack[i].from_overlay;
3987 if (!NILP (from_overlay))
3988 break;
3991 if (! NILP (from_overlay))
3993 bufpos = IT_CHARPOS (*it);
3994 /* For a string from an overlay, the base face depends
3995 only on text properties and ignores overlays. */
3996 base_face_id
3997 = face_for_overlay_string (it->w,
3998 IT_CHARPOS (*it),
3999 &next_stop,
4000 (IT_CHARPOS (*it)
4001 + TEXT_PROP_DISTANCE_LIMIT),
4002 false,
4003 from_overlay);
4005 else
4007 bufpos = 0;
4009 /* For strings from a `display' property, use the face at
4010 IT's current buffer position as the base face to merge
4011 with, so that overlay strings appear in the same face as
4012 surrounding text, unless they specify their own faces.
4013 For strings from wrap-prefix and line-prefix properties,
4014 use the default face, possibly remapped via
4015 Vface_remapping_alist. */
4016 /* Note that the fact that we use the face at _buffer_
4017 position means that a 'display' property on an overlay
4018 string will not inherit the face of that overlay string,
4019 but will instead revert to the face of buffer text
4020 covered by the overlay. This is visible, e.g., when the
4021 overlay specifies a box face, but neither the buffer nor
4022 the display string do. This sounds like a design bug,
4023 but Emacs always did that since v21.1, so changing that
4024 might be a big deal. */
4025 base_face_id = it->string_from_prefix_prop_p
4026 ? (!NILP (Vface_remapping_alist)
4027 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4028 : DEFAULT_FACE_ID)
4029 : underlying_face_id (it);
4032 new_face_id = face_at_string_position (it->w,
4033 it->string,
4034 IT_STRING_CHARPOS (*it),
4035 bufpos,
4036 &next_stop,
4037 base_face_id, false);
4039 /* Is this a start of a run of characters with box? Caveat:
4040 this can be called for a freshly allocated iterator; face_id
4041 is -1 is this case. We know that the new face will not
4042 change until the next check pos, i.e. if the new face has a
4043 box, all characters up to that position will have a
4044 box. But, as usual, we don't know whether that position
4045 is really the end. */
4046 if (new_face_id != it->face_id)
4048 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4049 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4051 /* If new face has a box but old face hasn't, this is the
4052 start of a run of characters with box, i.e. it has a
4053 shadow on the left side. */
4054 it->start_of_box_run_p
4055 = new_face->box && (old_face == NULL || !old_face->box);
4056 it->face_box_p = new_face->box != FACE_NO_BOX;
4060 it->face_id = new_face_id;
4061 return HANDLED_NORMALLY;
4065 /* Return the ID of the face ``underlying'' IT's current position,
4066 which is in a string. If the iterator is associated with a
4067 buffer, return the face at IT's current buffer position.
4068 Otherwise, use the iterator's base_face_id. */
4070 static int
4071 underlying_face_id (struct it *it)
4073 int face_id = it->base_face_id, i;
4075 eassert (STRINGP (it->string));
4077 for (i = it->sp - 1; i >= 0; --i)
4078 if (NILP (it->stack[i].string))
4079 face_id = it->stack[i].face_id;
4081 return face_id;
4085 /* Compute the face one character before or after the current position
4086 of IT, in the visual order. BEFORE_P means get the face
4087 in front (to the left in L2R paragraphs, to the right in R2L
4088 paragraphs) of IT's screen position. Value is the ID of the face. */
4090 static int
4091 face_before_or_after_it_pos (struct it *it, bool before_p)
4093 int face_id, limit;
4094 ptrdiff_t next_check_charpos;
4095 struct it it_copy;
4096 void *it_copy_data = NULL;
4098 eassert (it->s == NULL);
4100 if (STRINGP (it->string))
4102 ptrdiff_t bufpos, charpos;
4103 int base_face_id;
4105 /* No face change past the end of the string (for the case
4106 we are padding with spaces). No face change before the
4107 string start. */
4108 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4109 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4110 return it->face_id;
4112 if (!it->bidi_p)
4114 /* Set charpos to the position before or after IT's current
4115 position, in the logical order, which in the non-bidi
4116 case is the same as the visual order. */
4117 if (before_p)
4118 charpos = IT_STRING_CHARPOS (*it) - 1;
4119 else if (it->what == IT_COMPOSITION)
4120 /* For composition, we must check the character after the
4121 composition. */
4122 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4123 else
4124 charpos = IT_STRING_CHARPOS (*it) + 1;
4126 else
4128 if (before_p)
4130 /* With bidi iteration, the character before the current
4131 in the visual order cannot be found by simple
4132 iteration, because "reverse" reordering is not
4133 supported. Instead, we need to start from the string
4134 beginning and go all the way to the current string
4135 position, remembering the previous position. */
4136 /* Ignore face changes before the first visible
4137 character on this display line. */
4138 if (it->current_x <= it->first_visible_x)
4139 return it->face_id;
4140 SAVE_IT (it_copy, *it, it_copy_data);
4141 IT_STRING_CHARPOS (it_copy) = 0;
4142 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4146 charpos = IT_STRING_CHARPOS (it_copy);
4147 if (charpos >= SCHARS (it->string))
4148 break;
4149 bidi_move_to_visually_next (&it_copy.bidi_it);
4151 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4153 RESTORE_IT (it, it, it_copy_data);
4155 else
4157 /* Set charpos to the string position of the character
4158 that comes after IT's current position in the visual
4159 order. */
4160 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4162 it_copy = *it;
4163 while (n--)
4164 bidi_move_to_visually_next (&it_copy.bidi_it);
4166 charpos = it_copy.bidi_it.charpos;
4169 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4171 if (it->current.overlay_string_index >= 0)
4172 bufpos = IT_CHARPOS (*it);
4173 else
4174 bufpos = 0;
4176 base_face_id = underlying_face_id (it);
4178 /* Get the face for ASCII, or unibyte. */
4179 face_id = face_at_string_position (it->w,
4180 it->string,
4181 charpos,
4182 bufpos,
4183 &next_check_charpos,
4184 base_face_id, false);
4186 /* Correct the face for charsets different from ASCII. Do it
4187 for the multibyte case only. The face returned above is
4188 suitable for unibyte text if IT->string is unibyte. */
4189 if (STRING_MULTIBYTE (it->string))
4191 struct text_pos pos1 = string_pos (charpos, it->string);
4192 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4193 int c, len;
4194 struct face *face = FACE_FROM_ID (it->f, face_id);
4196 c = string_char_and_length (p, &len);
4197 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4200 else
4202 struct text_pos pos;
4204 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4205 || (IT_CHARPOS (*it) <= BEGV && before_p))
4206 return it->face_id;
4208 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4209 pos = it->current.pos;
4211 if (!it->bidi_p)
4213 if (before_p)
4214 DEC_TEXT_POS (pos, it->multibyte_p);
4215 else
4217 if (it->what == IT_COMPOSITION)
4219 /* For composition, we must check the position after
4220 the composition. */
4221 pos.charpos += it->cmp_it.nchars;
4222 pos.bytepos += it->len;
4224 else
4225 INC_TEXT_POS (pos, it->multibyte_p);
4228 else
4230 if (before_p)
4232 int current_x;
4234 /* With bidi iteration, the character before the current
4235 in the visual order cannot be found by simple
4236 iteration, because "reverse" reordering is not
4237 supported. Instead, we need to use the move_it_*
4238 family of functions, and move to the previous
4239 character starting from the beginning of the visual
4240 line. */
4241 /* Ignore face changes before the first visible
4242 character on this display line. */
4243 if (it->current_x <= it->first_visible_x)
4244 return it->face_id;
4245 SAVE_IT (it_copy, *it, it_copy_data);
4246 /* Implementation note: Since move_it_in_display_line
4247 works in the iterator geometry, and thinks the first
4248 character is always the leftmost, even in R2L lines,
4249 we don't need to distinguish between the R2L and L2R
4250 cases here. */
4251 current_x = it_copy.current_x;
4252 move_it_vertically_backward (&it_copy, 0);
4253 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4254 pos = it_copy.current.pos;
4255 RESTORE_IT (it, it, it_copy_data);
4257 else
4259 /* Set charpos to the buffer position of the character
4260 that comes after IT's current position in the visual
4261 order. */
4262 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4264 it_copy = *it;
4265 while (n--)
4266 bidi_move_to_visually_next (&it_copy.bidi_it);
4268 SET_TEXT_POS (pos,
4269 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4272 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4274 /* Determine face for CHARSET_ASCII, or unibyte. */
4275 face_id = face_at_buffer_position (it->w,
4276 CHARPOS (pos),
4277 &next_check_charpos,
4278 limit, false, -1);
4280 /* Correct the face for charsets different from ASCII. Do it
4281 for the multibyte case only. The face returned above is
4282 suitable for unibyte text if current_buffer is unibyte. */
4283 if (it->multibyte_p)
4285 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4286 struct face *face = FACE_FROM_ID (it->f, face_id);
4287 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4291 return face_id;
4296 /***********************************************************************
4297 Invisible text
4298 ***********************************************************************/
4300 /* Set up iterator IT from invisible properties at its current
4301 position. Called from handle_stop. */
4303 static enum prop_handled
4304 handle_invisible_prop (struct it *it)
4306 enum prop_handled handled = HANDLED_NORMALLY;
4307 int invis;
4308 Lisp_Object prop;
4310 if (STRINGP (it->string))
4312 Lisp_Object end_charpos, limit;
4314 /* Get the value of the invisible text property at the
4315 current position. Value will be nil if there is no such
4316 property. */
4317 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4318 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4319 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4321 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4323 /* Record whether we have to display an ellipsis for the
4324 invisible text. */
4325 bool display_ellipsis_p = (invis == 2);
4326 ptrdiff_t len, endpos;
4328 handled = HANDLED_RECOMPUTE_PROPS;
4330 /* Get the position at which the next visible text can be
4331 found in IT->string, if any. */
4332 endpos = len = SCHARS (it->string);
4333 XSETINT (limit, len);
4336 end_charpos
4337 = Fnext_single_property_change (end_charpos, Qinvisible,
4338 it->string, limit);
4339 /* Since LIMIT is always an integer, so should be the
4340 value returned by Fnext_single_property_change. */
4341 eassert (INTEGERP (end_charpos));
4342 if (INTEGERP (end_charpos))
4344 endpos = XFASTINT (end_charpos);
4345 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4346 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 if (invis == 2)
4348 display_ellipsis_p = true;
4350 else /* Should never happen; but if it does, exit the loop. */
4351 endpos = len;
4353 while (invis != 0 && endpos < len);
4355 if (display_ellipsis_p)
4356 it->ellipsis_p = true;
4358 if (endpos < len)
4360 /* Text at END_CHARPOS is visible. Move IT there. */
4361 struct text_pos old;
4362 ptrdiff_t oldpos;
4364 old = it->current.string_pos;
4365 oldpos = CHARPOS (old);
4366 if (it->bidi_p)
4368 if (it->bidi_it.first_elt
4369 && it->bidi_it.charpos < SCHARS (it->string))
4370 bidi_paragraph_init (it->paragraph_embedding,
4371 &it->bidi_it, true);
4372 /* Bidi-iterate out of the invisible text. */
4375 bidi_move_to_visually_next (&it->bidi_it);
4377 while (oldpos <= it->bidi_it.charpos
4378 && it->bidi_it.charpos < endpos
4379 && it->bidi_it.charpos < it->bidi_it.string.schars);
4381 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4383 if (IT_CHARPOS (*it) >= endpos)
4384 it->prev_stop = endpos;
4386 else
4388 IT_STRING_CHARPOS (*it) = endpos;
4389 compute_string_pos (&it->current.string_pos, old, it->string);
4392 else
4394 /* The rest of the string is invisible. If this is an
4395 overlay string, proceed with the next overlay string
4396 or whatever comes and return a character from there. */
4397 if (it->current.overlay_string_index >= 0
4398 && !display_ellipsis_p)
4400 next_overlay_string (it);
4401 /* Don't check for overlay strings when we just
4402 finished processing them. */
4403 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4405 else
4407 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4408 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4413 else
4415 ptrdiff_t newpos, next_stop, start_charpos, tem;
4416 Lisp_Object pos, overlay;
4418 /* First of all, is there invisible text at this position? */
4419 tem = start_charpos = IT_CHARPOS (*it);
4420 pos = make_number (tem);
4421 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4422 &overlay);
4423 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4425 /* If we are on invisible text, skip over it. */
4426 if (invis != 0 && start_charpos < it->end_charpos)
4428 /* Record whether we have to display an ellipsis for the
4429 invisible text. */
4430 bool display_ellipsis_p = invis == 2;
4432 handled = HANDLED_RECOMPUTE_PROPS;
4434 /* Loop skipping over invisible text. The loop is left at
4435 ZV or with IT on the first char being visible again. */
4438 /* Try to skip some invisible text. Return value is the
4439 position reached which can be equal to where we start
4440 if there is nothing invisible there. This skips both
4441 over invisible text properties and overlays with
4442 invisible property. */
4443 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4445 /* If we skipped nothing at all we weren't at invisible
4446 text in the first place. If everything to the end of
4447 the buffer was skipped, end the loop. */
4448 if (newpos == tem || newpos >= ZV)
4449 invis = 0;
4450 else
4452 /* We skipped some characters but not necessarily
4453 all there are. Check if we ended up on visible
4454 text. Fget_char_property returns the property of
4455 the char before the given position, i.e. if we
4456 get invis = 0, this means that the char at
4457 newpos is visible. */
4458 pos = make_number (newpos);
4459 prop = Fget_char_property (pos, Qinvisible, it->window);
4460 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4463 /* If we ended up on invisible text, proceed to
4464 skip starting with next_stop. */
4465 if (invis != 0)
4466 tem = next_stop;
4468 /* If there are adjacent invisible texts, don't lose the
4469 second one's ellipsis. */
4470 if (invis == 2)
4471 display_ellipsis_p = true;
4473 while (invis != 0);
4475 /* The position newpos is now either ZV or on visible text. */
4476 if (it->bidi_p)
4478 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4479 bool on_newline
4480 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4481 bool after_newline
4482 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4484 /* If the invisible text ends on a newline or on a
4485 character after a newline, we can avoid the costly,
4486 character by character, bidi iteration to NEWPOS, and
4487 instead simply reseat the iterator there. That's
4488 because all bidi reordering information is tossed at
4489 the newline. This is a big win for modes that hide
4490 complete lines, like Outline, Org, etc. */
4491 if (on_newline || after_newline)
4493 struct text_pos tpos;
4494 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4496 SET_TEXT_POS (tpos, newpos, bpos);
4497 reseat_1 (it, tpos, false);
4498 /* If we reseat on a newline/ZV, we need to prep the
4499 bidi iterator for advancing to the next character
4500 after the newline/EOB, keeping the current paragraph
4501 direction (so that PRODUCE_GLYPHS does TRT wrt
4502 prepending/appending glyphs to a glyph row). */
4503 if (on_newline)
4505 it->bidi_it.first_elt = false;
4506 it->bidi_it.paragraph_dir = pdir;
4507 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4508 it->bidi_it.nchars = 1;
4509 it->bidi_it.ch_len = 1;
4512 else /* Must use the slow method. */
4514 /* With bidi iteration, the region of invisible text
4515 could start and/or end in the middle of a
4516 non-base embedding level. Therefore, we need to
4517 skip invisible text using the bidi iterator,
4518 starting at IT's current position, until we find
4519 ourselves outside of the invisible text.
4520 Skipping invisible text _after_ bidi iteration
4521 avoids affecting the visual order of the
4522 displayed text when invisible properties are
4523 added or removed. */
4524 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4526 /* If we were `reseat'ed to a new paragraph,
4527 determine the paragraph base direction. We
4528 need to do it now because
4529 next_element_from_buffer may not have a
4530 chance to do it, if we are going to skip any
4531 text at the beginning, which resets the
4532 FIRST_ELT flag. */
4533 bidi_paragraph_init (it->paragraph_embedding,
4534 &it->bidi_it, true);
4538 bidi_move_to_visually_next (&it->bidi_it);
4540 while (it->stop_charpos <= it->bidi_it.charpos
4541 && it->bidi_it.charpos < newpos);
4542 IT_CHARPOS (*it) = it->bidi_it.charpos;
4543 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4544 /* If we overstepped NEWPOS, record its position in
4545 the iterator, so that we skip invisible text if
4546 later the bidi iteration lands us in the
4547 invisible region again. */
4548 if (IT_CHARPOS (*it) >= newpos)
4549 it->prev_stop = newpos;
4552 else
4554 IT_CHARPOS (*it) = newpos;
4555 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4558 if (display_ellipsis_p)
4560 /* Make sure that the glyphs of the ellipsis will get
4561 correct `charpos' values. If we would not update
4562 it->position here, the glyphs would belong to the
4563 last visible character _before_ the invisible
4564 text, which confuses `set_cursor_from_row'.
4566 We use the last invisible position instead of the
4567 first because this way the cursor is always drawn on
4568 the first "." of the ellipsis, whenever PT is inside
4569 the invisible text. Otherwise the cursor would be
4570 placed _after_ the ellipsis when the point is after the
4571 first invisible character. */
4572 if (!STRINGP (it->object))
4574 it->position.charpos = newpos - 1;
4575 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4579 /* If there are before-strings at the start of invisible
4580 text, and the text is invisible because of a text
4581 property, arrange to show before-strings because 20.x did
4582 it that way. (If the text is invisible because of an
4583 overlay property instead of a text property, this is
4584 already handled in the overlay code.) */
4585 if (NILP (overlay)
4586 && get_overlay_strings (it, it->stop_charpos))
4588 handled = HANDLED_RECOMPUTE_PROPS;
4589 if (it->sp > 0)
4591 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4592 /* The call to get_overlay_strings above recomputes
4593 it->stop_charpos, but it only considers changes
4594 in properties and overlays beyond iterator's
4595 current position. This causes us to miss changes
4596 that happen exactly where the invisible property
4597 ended. So we play it safe here and force the
4598 iterator to check for potential stop positions
4599 immediately after the invisible text. Note that
4600 if get_overlay_strings returns true, it
4601 normally also pushed the iterator stack, so we
4602 need to update the stop position in the slot
4603 below the current one. */
4604 it->stack[it->sp - 1].stop_charpos
4605 = CHARPOS (it->stack[it->sp - 1].current.pos);
4608 else if (display_ellipsis_p)
4610 it->ellipsis_p = true;
4611 /* Let the ellipsis display before
4612 considering any properties of the following char.
4613 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4614 handled = HANDLED_RETURN;
4619 return handled;
4623 /* Make iterator IT return `...' next.
4624 Replaces LEN characters from buffer. */
4626 static void
4627 setup_for_ellipsis (struct it *it, int len)
4629 /* Use the display table definition for `...'. Invalid glyphs
4630 will be handled by the method returning elements from dpvec. */
4631 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4633 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4634 it->dpvec = v->contents;
4635 it->dpend = v->contents + v->header.size;
4637 else
4639 /* Default `...'. */
4640 it->dpvec = default_invis_vector;
4641 it->dpend = default_invis_vector + 3;
4644 it->dpvec_char_len = len;
4645 it->current.dpvec_index = 0;
4646 it->dpvec_face_id = -1;
4648 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4649 face as the preceding text. IT->saved_face_id was set in
4650 handle_stop to the face of the preceding character, and will be
4651 different from IT->face_id only if the invisible text skipped in
4652 handle_invisible_prop has some non-default face on its first
4653 character. We thus ignore the face of the invisible text when we
4654 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4655 if (it->saved_face_id >= 0)
4656 it->face_id = it->saved_face_id;
4658 /* If the ellipsis represents buffer text, it means we advanced in
4659 the buffer, so we should no longer ignore overlay strings. */
4660 if (it->method == GET_FROM_BUFFER)
4661 it->ignore_overlay_strings_at_pos_p = false;
4663 it->method = GET_FROM_DISPLAY_VECTOR;
4664 it->ellipsis_p = true;
4669 /***********************************************************************
4670 'display' property
4671 ***********************************************************************/
4673 /* Set up iterator IT from `display' property at its current position.
4674 Called from handle_stop.
4675 We return HANDLED_RETURN if some part of the display property
4676 overrides the display of the buffer text itself.
4677 Otherwise we return HANDLED_NORMALLY. */
4679 static enum prop_handled
4680 handle_display_prop (struct it *it)
4682 Lisp_Object propval, object, overlay;
4683 struct text_pos *position;
4684 ptrdiff_t bufpos;
4685 /* Nonzero if some property replaces the display of the text itself. */
4686 int display_replaced = 0;
4688 if (STRINGP (it->string))
4690 object = it->string;
4691 position = &it->current.string_pos;
4692 bufpos = CHARPOS (it->current.pos);
4694 else
4696 XSETWINDOW (object, it->w);
4697 position = &it->current.pos;
4698 bufpos = CHARPOS (*position);
4701 /* Reset those iterator values set from display property values. */
4702 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4703 it->space_width = Qnil;
4704 it->font_height = Qnil;
4705 it->voffset = 0;
4707 /* We don't support recursive `display' properties, i.e. string
4708 values that have a string `display' property, that have a string
4709 `display' property etc. */
4710 if (!it->string_from_display_prop_p)
4711 it->area = TEXT_AREA;
4713 propval = get_char_property_and_overlay (make_number (position->charpos),
4714 Qdisplay, object, &overlay);
4715 if (NILP (propval))
4716 return HANDLED_NORMALLY;
4717 /* Now OVERLAY is the overlay that gave us this property, or nil
4718 if it was a text property. */
4720 if (!STRINGP (it->string))
4721 object = it->w->contents;
4723 display_replaced = handle_display_spec (it, propval, object, overlay,
4724 position, bufpos,
4725 FRAME_WINDOW_P (it->f));
4726 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4729 /* Subroutine of handle_display_prop. Returns non-zero if the display
4730 specification in SPEC is a replacing specification, i.e. it would
4731 replace the text covered by `display' property with something else,
4732 such as an image or a display string. If SPEC includes any kind or
4733 `(space ...) specification, the value is 2; this is used by
4734 compute_display_string_pos, which see.
4736 See handle_single_display_spec for documentation of arguments.
4737 FRAME_WINDOW_P is true if the window being redisplayed is on a
4738 GUI frame; this argument is used only if IT is NULL, see below.
4740 IT can be NULL, if this is called by the bidi reordering code
4741 through compute_display_string_pos, which see. In that case, this
4742 function only examines SPEC, but does not otherwise "handle" it, in
4743 the sense that it doesn't set up members of IT from the display
4744 spec. */
4745 static int
4746 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4747 Lisp_Object overlay, struct text_pos *position,
4748 ptrdiff_t bufpos, bool frame_window_p)
4750 int replacing = 0;
4751 bool enable_eval = true;
4753 /* Support (disable-eval PROP) which is used by enriched.el. */
4754 if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
4756 enable_eval = false;
4757 spec = XCAR (XCDR (spec));
4760 if (CONSP (spec)
4761 /* Simple specifications. */
4762 && !EQ (XCAR (spec), Qimage)
4763 #ifdef HAVE_XWIDGETS
4764 && !EQ (XCAR (spec), Qxwidget)
4765 #endif
4766 && !EQ (XCAR (spec), Qspace)
4767 && !EQ (XCAR (spec), Qwhen)
4768 && !EQ (XCAR (spec), Qslice)
4769 && !EQ (XCAR (spec), Qspace_width)
4770 && !EQ (XCAR (spec), Qheight)
4771 && !EQ (XCAR (spec), Qraise)
4772 /* Marginal area specifications. */
4773 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4774 && !EQ (XCAR (spec), Qleft_fringe)
4775 && !EQ (XCAR (spec), Qright_fringe)
4776 && !NILP (XCAR (spec)))
4778 for (; CONSP (spec); spec = XCDR (spec))
4780 int rv = handle_single_display_spec (it, XCAR (spec), object,
4781 overlay, position, bufpos,
4782 replacing, frame_window_p,
4783 enable_eval);
4784 if (rv != 0)
4786 replacing = rv;
4787 /* If some text in a string is replaced, `position' no
4788 longer points to the position of `object'. */
4789 if (!it || STRINGP (object))
4790 break;
4794 else if (VECTORP (spec))
4796 ptrdiff_t i;
4797 for (i = 0; i < ASIZE (spec); ++i)
4799 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4800 overlay, position, bufpos,
4801 replacing, frame_window_p,
4802 enable_eval);
4803 if (rv != 0)
4805 replacing = rv;
4806 /* If some text in a string is replaced, `position' no
4807 longer points to the position of `object'. */
4808 if (!it || STRINGP (object))
4809 break;
4813 else
4814 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4815 bufpos, 0, frame_window_p,
4816 enable_eval);
4817 return replacing;
4820 /* Value is the position of the end of the `display' property starting
4821 at START_POS in OBJECT. */
4823 static struct text_pos
4824 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4826 Lisp_Object end;
4827 struct text_pos end_pos;
4829 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4830 Qdisplay, object, Qnil);
4831 CHARPOS (end_pos) = XFASTINT (end);
4832 if (STRINGP (object))
4833 compute_string_pos (&end_pos, start_pos, it->string);
4834 else
4835 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4837 return end_pos;
4841 /* Set up IT from a single `display' property specification SPEC. OBJECT
4842 is the object in which the `display' property was found. *POSITION
4843 is the position in OBJECT at which the `display' property was found.
4844 BUFPOS is the buffer position of OBJECT (different from POSITION if
4845 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4846 previously saw a display specification which already replaced text
4847 display with something else, for example an image; we ignore such
4848 properties after the first one has been processed.
4850 OVERLAY is the overlay this `display' property came from,
4851 or nil if it was a text property.
4853 If SPEC is a `space' or `image' specification, and in some other
4854 cases too, set *POSITION to the position where the `display'
4855 property ends.
4857 If IT is NULL, only examine the property specification in SPEC, but
4858 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4859 is intended to be displayed in a window on a GUI frame.
4861 Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
4863 Value is non-zero if something was found which replaces the display
4864 of buffer or string text. */
4866 static int
4867 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4868 Lisp_Object overlay, struct text_pos *position,
4869 ptrdiff_t bufpos, int display_replaced,
4870 bool frame_window_p, bool enable_eval_p)
4872 Lisp_Object form;
4873 Lisp_Object location, value;
4874 struct text_pos start_pos = *position;
4876 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4877 If the result is non-nil, use VALUE instead of SPEC. */
4878 form = Qt;
4879 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4881 spec = XCDR (spec);
4882 if (!CONSP (spec))
4883 return 0;
4884 form = XCAR (spec);
4885 spec = XCDR (spec);
4888 if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
4889 form = Qnil;
4890 if (!NILP (form) && !EQ (form, Qt))
4892 ptrdiff_t count = SPECPDL_INDEX ();
4894 /* Bind `object' to the object having the `display' property, a
4895 buffer or string. Bind `position' to the position in the
4896 object where the property was found, and `buffer-position'
4897 to the current position in the buffer. */
4899 if (NILP (object))
4900 XSETBUFFER (object, current_buffer);
4901 specbind (Qobject, object);
4902 specbind (Qposition, make_number (CHARPOS (*position)));
4903 specbind (Qbuffer_position, make_number (bufpos));
4904 form = safe_eval (form);
4905 unbind_to (count, Qnil);
4908 if (NILP (form))
4909 return 0;
4911 /* Handle `(height HEIGHT)' specifications. */
4912 if (CONSP (spec)
4913 && EQ (XCAR (spec), Qheight)
4914 && CONSP (XCDR (spec)))
4916 if (it)
4918 if (!FRAME_WINDOW_P (it->f))
4919 return 0;
4921 it->font_height = XCAR (XCDR (spec));
4922 if (!NILP (it->font_height))
4924 int new_height = -1;
4926 if (CONSP (it->font_height)
4927 && (EQ (XCAR (it->font_height), Qplus)
4928 || EQ (XCAR (it->font_height), Qminus))
4929 && CONSP (XCDR (it->font_height))
4930 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4932 /* `(+ N)' or `(- N)' where N is an integer. */
4933 int steps = XINT (XCAR (XCDR (it->font_height)));
4934 if (EQ (XCAR (it->font_height), Qplus))
4935 steps = - steps;
4936 it->face_id = smaller_face (it->f, it->face_id, steps);
4938 else if (FUNCTIONP (it->font_height) && enable_eval_p)
4940 /* Call function with current height as argument.
4941 Value is the new height. */
4942 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4943 Lisp_Object height;
4944 height = safe_call1 (it->font_height,
4945 face->lface[LFACE_HEIGHT_INDEX]);
4946 if (NUMBERP (height))
4947 new_height = XFLOATINT (height);
4949 else if (NUMBERP (it->font_height))
4951 /* Value is a multiple of the canonical char height. */
4952 struct face *f;
4954 f = FACE_FROM_ID (it->f,
4955 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4956 new_height = (XFLOATINT (it->font_height)
4957 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4959 else if (enable_eval_p)
4961 /* Evaluate IT->font_height with `height' bound to the
4962 current specified height to get the new height. */
4963 ptrdiff_t count = SPECPDL_INDEX ();
4964 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4966 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4967 value = safe_eval (it->font_height);
4968 unbind_to (count, Qnil);
4970 if (NUMBERP (value))
4971 new_height = XFLOATINT (value);
4974 if (new_height > 0)
4975 it->face_id = face_with_height (it->f, it->face_id, new_height);
4979 return 0;
4982 /* Handle `(space-width WIDTH)'. */
4983 if (CONSP (spec)
4984 && EQ (XCAR (spec), Qspace_width)
4985 && CONSP (XCDR (spec)))
4987 if (it)
4989 if (!FRAME_WINDOW_P (it->f))
4990 return 0;
4992 value = XCAR (XCDR (spec));
4993 if (NUMBERP (value) && XFLOATINT (value) > 0)
4994 it->space_width = value;
4997 return 0;
5000 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5001 if (CONSP (spec)
5002 && EQ (XCAR (spec), Qslice))
5004 Lisp_Object tem;
5006 if (it)
5008 if (!FRAME_WINDOW_P (it->f))
5009 return 0;
5011 if (tem = XCDR (spec), CONSP (tem))
5013 it->slice.x = XCAR (tem);
5014 if (tem = XCDR (tem), CONSP (tem))
5016 it->slice.y = XCAR (tem);
5017 if (tem = XCDR (tem), CONSP (tem))
5019 it->slice.width = XCAR (tem);
5020 if (tem = XCDR (tem), CONSP (tem))
5021 it->slice.height = XCAR (tem);
5027 return 0;
5030 /* Handle `(raise FACTOR)'. */
5031 if (CONSP (spec)
5032 && EQ (XCAR (spec), Qraise)
5033 && CONSP (XCDR (spec)))
5035 if (it)
5037 if (!FRAME_WINDOW_P (it->f))
5038 return 0;
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 value = XCAR (XCDR (spec));
5042 if (NUMBERP (value))
5044 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5045 it->voffset = - (XFLOATINT (value)
5046 * (normal_char_height (face->font, -1)));
5048 #endif /* HAVE_WINDOW_SYSTEM */
5051 return 0;
5054 /* Don't handle the other kinds of display specifications
5055 inside a string that we got from a `display' property. */
5056 if (it && it->string_from_display_prop_p)
5057 return 0;
5059 /* Characters having this form of property are not displayed, so
5060 we have to find the end of the property. */
5061 if (it)
5063 start_pos = *position;
5064 *position = display_prop_end (it, object, start_pos);
5065 /* If the display property comes from an overlay, don't consider
5066 any potential stop_charpos values before the end of that
5067 overlay. Since display_prop_end will happily find another
5068 'display' property coming from some other overlay or text
5069 property on buffer positions before this overlay's end, we
5070 need to ignore them, or else we risk displaying this
5071 overlay's display string/image twice. */
5072 if (!NILP (overlay))
5074 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5076 /* Some borderline-sane Lisp might call us with the current
5077 buffer narrowed so that overlay-end is outside the
5078 POINT_MIN..POINT_MAX region, which will then cause
5079 various assertion violations and crashes down the road,
5080 starting with pop_it when it will attempt to use POSITION
5081 set below. Prevent that. */
5082 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5084 if (ovendpos > CHARPOS (*position))
5085 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5088 value = Qnil;
5090 /* Stop the scan at that end position--we assume that all
5091 text properties change there. */
5092 if (it)
5093 it->stop_charpos = position->charpos;
5095 /* Handle `(left-fringe BITMAP [FACE])'
5096 and `(right-fringe BITMAP [FACE])'. */
5097 if (CONSP (spec)
5098 && (EQ (XCAR (spec), Qleft_fringe)
5099 || EQ (XCAR (spec), Qright_fringe))
5100 && CONSP (XCDR (spec)))
5102 if (it)
5104 if (!FRAME_WINDOW_P (it->f))
5105 /* If we return here, POSITION has been advanced
5106 across the text with this property. */
5108 /* Synchronize the bidi iterator with POSITION. This is
5109 needed because we are not going to push the iterator
5110 on behalf of this display property, so there will be
5111 no pop_it call to do this synchronization for us. */
5112 if (it->bidi_p)
5114 it->position = *position;
5115 iterate_out_of_display_property (it);
5116 *position = it->position;
5118 return 1;
5121 else if (!frame_window_p)
5122 return 1;
5124 #ifdef HAVE_WINDOW_SYSTEM
5125 value = XCAR (XCDR (spec));
5126 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5127 if (! fringe_bitmap)
5128 /* If we return here, POSITION has been advanced
5129 across the text with this property. */
5131 if (it && it->bidi_p)
5133 it->position = *position;
5134 iterate_out_of_display_property (it);
5135 *position = it->position;
5137 return 1;
5140 if (it)
5142 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5144 if (CONSP (XCDR (XCDR (spec))))
5146 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5147 int face_id2 = lookup_derived_face (it->f, face_name,
5148 FRINGE_FACE_ID, false);
5149 if (face_id2 >= 0)
5150 face_id = face_id2;
5153 /* Save current settings of IT so that we can restore them
5154 when we are finished with the glyph property value. */
5155 push_it (it, position);
5157 it->area = TEXT_AREA;
5158 it->what = IT_IMAGE;
5159 it->image_id = -1; /* no image */
5160 it->position = start_pos;
5161 it->object = NILP (object) ? it->w->contents : object;
5162 it->method = GET_FROM_IMAGE;
5163 it->from_overlay = Qnil;
5164 it->face_id = face_id;
5165 it->from_disp_prop_p = true;
5167 /* Say that we haven't consumed the characters with
5168 `display' property yet. The call to pop_it in
5169 set_iterator_to_next will clean this up. */
5170 *position = start_pos;
5172 if (EQ (XCAR (spec), Qleft_fringe))
5174 it->left_user_fringe_bitmap = fringe_bitmap;
5175 it->left_user_fringe_face_id = face_id;
5177 else
5179 it->right_user_fringe_bitmap = fringe_bitmap;
5180 it->right_user_fringe_face_id = face_id;
5183 #endif /* HAVE_WINDOW_SYSTEM */
5184 return 1;
5187 /* Prepare to handle `((margin left-margin) ...)',
5188 `((margin right-margin) ...)' and `((margin nil) ...)'
5189 prefixes for display specifications. */
5190 location = Qunbound;
5191 if (CONSP (spec) && CONSP (XCAR (spec)))
5193 Lisp_Object tem;
5195 value = XCDR (spec);
5196 if (CONSP (value))
5197 value = XCAR (value);
5199 tem = XCAR (spec);
5200 if (EQ (XCAR (tem), Qmargin)
5201 && (tem = XCDR (tem),
5202 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5203 (NILP (tem)
5204 || EQ (tem, Qleft_margin)
5205 || EQ (tem, Qright_margin))))
5206 location = tem;
5209 if (EQ (location, Qunbound))
5211 location = Qnil;
5212 value = spec;
5215 /* After this point, VALUE is the property after any
5216 margin prefix has been stripped. It must be a string,
5217 an image specification, or `(space ...)'.
5219 LOCATION specifies where to display: `left-margin',
5220 `right-margin' or nil. */
5222 bool valid_p = (STRINGP (value)
5223 #ifdef HAVE_WINDOW_SYSTEM
5224 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5225 && valid_image_p (value))
5226 #endif /* not HAVE_WINDOW_SYSTEM */
5227 || (CONSP (value) && EQ (XCAR (value), Qspace))
5228 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5229 && valid_xwidget_spec_p (value)));
5231 if (valid_p && display_replaced == 0)
5233 int retval = 1;
5235 if (!it)
5237 /* Callers need to know whether the display spec is any kind
5238 of `(space ...)' spec that is about to affect text-area
5239 display. */
5240 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5241 retval = 2;
5242 return retval;
5245 /* Save current settings of IT so that we can restore them
5246 when we are finished with the glyph property value. */
5247 push_it (it, position);
5248 it->from_overlay = overlay;
5249 it->from_disp_prop_p = true;
5251 if (NILP (location))
5252 it->area = TEXT_AREA;
5253 else if (EQ (location, Qleft_margin))
5254 it->area = LEFT_MARGIN_AREA;
5255 else
5256 it->area = RIGHT_MARGIN_AREA;
5258 if (STRINGP (value))
5260 it->string = value;
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 it->current.overlay_string_index = -1;
5263 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5264 it->end_charpos = it->string_nchars = SCHARS (it->string);
5265 it->method = GET_FROM_STRING;
5266 it->stop_charpos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5269 it->string_from_display_prop_p = true;
5270 it->cmp_it.id = -1;
5271 /* Say that we haven't consumed the characters with
5272 `display' property yet. The call to pop_it in
5273 set_iterator_to_next will clean this up. */
5274 if (BUFFERP (object))
5275 *position = start_pos;
5277 /* Force paragraph direction to be that of the parent
5278 object. If the parent object's paragraph direction is
5279 not yet determined, default to L2R. */
5280 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5281 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5282 else
5283 it->paragraph_embedding = L2R;
5285 /* Set up the bidi iterator for this display string. */
5286 if (it->bidi_p)
5288 it->bidi_it.string.lstring = it->string;
5289 it->bidi_it.string.s = NULL;
5290 it->bidi_it.string.schars = it->end_charpos;
5291 it->bidi_it.string.bufpos = bufpos;
5292 it->bidi_it.string.from_disp_str = true;
5293 it->bidi_it.string.unibyte = !it->multibyte_p;
5294 it->bidi_it.w = it->w;
5295 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5298 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5300 it->method = GET_FROM_STRETCH;
5301 it->object = value;
5302 *position = it->position = start_pos;
5303 retval = 1 + (it->area == TEXT_AREA);
5305 else if (valid_xwidget_spec_p (value))
5307 it->what = IT_XWIDGET;
5308 it->method = GET_FROM_XWIDGET;
5309 it->position = start_pos;
5310 it->object = NILP (object) ? it->w->contents : object;
5311 *position = start_pos;
5312 it->xwidget = lookup_xwidget (value);
5314 #ifdef HAVE_WINDOW_SYSTEM
5315 else
5317 it->what = IT_IMAGE;
5318 it->image_id = lookup_image (it->f, value);
5319 it->position = start_pos;
5320 it->object = NILP (object) ? it->w->contents : object;
5321 it->method = GET_FROM_IMAGE;
5323 /* Say that we haven't consumed the characters with
5324 `display' property yet. The call to pop_it in
5325 set_iterator_to_next will clean this up. */
5326 *position = start_pos;
5328 #endif /* HAVE_WINDOW_SYSTEM */
5330 return retval;
5333 /* Invalid property or property not supported. Restore
5334 POSITION to what it was before. */
5335 *position = start_pos;
5336 return 0;
5339 /* Check if PROP is a display property value whose text should be
5340 treated as intangible. OVERLAY is the overlay from which PROP
5341 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5342 specify the buffer position covered by PROP. */
5344 bool
5345 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5346 ptrdiff_t charpos, ptrdiff_t bytepos)
5348 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5349 struct text_pos position;
5351 SET_TEXT_POS (position, charpos, bytepos);
5352 return (handle_display_spec (NULL, prop, Qnil, overlay,
5353 &position, charpos, frame_window_p)
5354 != 0);
5358 /* Return true if PROP is a display sub-property value containing STRING.
5360 Implementation note: this and the following function are really
5361 special cases of handle_display_spec and
5362 handle_single_display_spec, and should ideally use the same code.
5363 Until they do, these two pairs must be consistent and must be
5364 modified in sync. */
5366 static bool
5367 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5369 if (EQ (string, prop))
5370 return true;
5372 /* Skip over `when FORM'. */
5373 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5375 prop = XCDR (prop);
5376 if (!CONSP (prop))
5377 return false;
5378 /* Actually, the condition following `when' should be eval'ed,
5379 like handle_single_display_spec does, and we should return
5380 false if it evaluates to nil. However, this function is
5381 called only when the buffer was already displayed and some
5382 glyph in the glyph matrix was found to come from a display
5383 string. Therefore, the condition was already evaluated, and
5384 the result was non-nil, otherwise the display string wouldn't
5385 have been displayed and we would have never been called for
5386 this property. Thus, we can skip the evaluation and assume
5387 its result is non-nil. */
5388 prop = XCDR (prop);
5391 if (CONSP (prop))
5392 /* Skip over `margin LOCATION'. */
5393 if (EQ (XCAR (prop), Qmargin))
5395 prop = XCDR (prop);
5396 if (!CONSP (prop))
5397 return false;
5399 prop = XCDR (prop);
5400 if (!CONSP (prop))
5401 return false;
5404 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5408 /* Return true if STRING appears in the `display' property PROP. */
5410 static bool
5411 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5413 if (CONSP (prop)
5414 && !EQ (XCAR (prop), Qwhen)
5415 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5417 /* A list of sub-properties. */
5418 while (CONSP (prop))
5420 if (single_display_spec_string_p (XCAR (prop), string))
5421 return true;
5422 prop = XCDR (prop);
5425 else if (VECTORP (prop))
5427 /* A vector of sub-properties. */
5428 ptrdiff_t i;
5429 for (i = 0; i < ASIZE (prop); ++i)
5430 if (single_display_spec_string_p (AREF (prop, i), string))
5431 return true;
5433 else
5434 return single_display_spec_string_p (prop, string);
5436 return false;
5439 /* Look for STRING in overlays and text properties in the current
5440 buffer, between character positions FROM and TO (excluding TO).
5441 BACK_P means look back (in this case, TO is supposed to be
5442 less than FROM).
5443 Value is the first character position where STRING was found, or
5444 zero if it wasn't found before hitting TO.
5446 This function may only use code that doesn't eval because it is
5447 called asynchronously from note_mouse_highlight. */
5449 static ptrdiff_t
5450 string_buffer_position_lim (Lisp_Object string,
5451 ptrdiff_t from, ptrdiff_t to, bool back_p)
5453 Lisp_Object limit, prop, pos;
5454 bool found = false;
5456 pos = make_number (max (from, BEGV));
5458 if (!back_p) /* looking forward */
5460 limit = make_number (min (to, ZV));
5461 while (!found && !EQ (pos, limit))
5463 prop = Fget_char_property (pos, Qdisplay, Qnil);
5464 if (!NILP (prop) && display_prop_string_p (prop, string))
5465 found = true;
5466 else
5467 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5468 limit);
5471 else /* looking back */
5473 limit = make_number (max (to, BEGV));
5474 while (!found && !EQ (pos, limit))
5476 prop = Fget_char_property (pos, Qdisplay, Qnil);
5477 if (!NILP (prop) && display_prop_string_p (prop, string))
5478 found = true;
5479 else
5480 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5481 limit);
5485 return found ? XINT (pos) : 0;
5488 /* Determine which buffer position in current buffer STRING comes from.
5489 AROUND_CHARPOS is an approximate position where it could come from.
5490 Value is the buffer position or 0 if it couldn't be determined.
5492 This function is necessary because we don't record buffer positions
5493 in glyphs generated from strings (to keep struct glyph small).
5494 This function may only use code that doesn't eval because it is
5495 called asynchronously from note_mouse_highlight. */
5497 static ptrdiff_t
5498 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5500 const int MAX_DISTANCE = 1000;
5501 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5502 around_charpos + MAX_DISTANCE,
5503 false);
5505 if (!found)
5506 found = string_buffer_position_lim (string, around_charpos,
5507 around_charpos - MAX_DISTANCE, true);
5508 return found;
5513 /***********************************************************************
5514 `composition' property
5515 ***********************************************************************/
5517 /* Set up iterator IT from `composition' property at its current
5518 position. Called from handle_stop. */
5520 static enum prop_handled
5521 handle_composition_prop (struct it *it)
5523 Lisp_Object prop, string;
5524 ptrdiff_t pos, pos_byte, start, end;
5526 if (STRINGP (it->string))
5528 unsigned char *s;
5530 pos = IT_STRING_CHARPOS (*it);
5531 pos_byte = IT_STRING_BYTEPOS (*it);
5532 string = it->string;
5533 s = SDATA (string) + pos_byte;
5534 it->c = STRING_CHAR (s);
5536 else
5538 pos = IT_CHARPOS (*it);
5539 pos_byte = IT_BYTEPOS (*it);
5540 string = Qnil;
5541 it->c = FETCH_CHAR (pos_byte);
5544 /* If there's a valid composition and point is not inside of the
5545 composition (in the case that the composition is from the current
5546 buffer), draw a glyph composed from the composition components. */
5547 if (find_composition (pos, -1, &start, &end, &prop, string)
5548 && composition_valid_p (start, end, prop)
5549 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5551 if (start < pos)
5552 /* As we can't handle this situation (perhaps font-lock added
5553 a new composition), we just return here hoping that next
5554 redisplay will detect this composition much earlier. */
5555 return HANDLED_NORMALLY;
5556 if (start != pos)
5558 if (STRINGP (it->string))
5559 pos_byte = string_char_to_byte (it->string, start);
5560 else
5561 pos_byte = CHAR_TO_BYTE (start);
5563 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5564 prop, string);
5566 if (it->cmp_it.id >= 0)
5568 it->cmp_it.ch = -1;
5569 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5570 it->cmp_it.nglyphs = -1;
5574 return HANDLED_NORMALLY;
5579 /***********************************************************************
5580 Overlay strings
5581 ***********************************************************************/
5583 /* The following structure is used to record overlay strings for
5584 later sorting in load_overlay_strings. */
5586 struct overlay_entry
5588 Lisp_Object overlay;
5589 Lisp_Object string;
5590 EMACS_INT priority;
5591 bool after_string_p;
5595 /* Set up iterator IT from overlay strings at its current position.
5596 Called from handle_stop. */
5598 static enum prop_handled
5599 handle_overlay_change (struct it *it)
5601 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5602 return HANDLED_RECOMPUTE_PROPS;
5603 else
5604 return HANDLED_NORMALLY;
5608 /* Set up the next overlay string for delivery by IT, if there is an
5609 overlay string to deliver. Called by set_iterator_to_next when the
5610 end of the current overlay string is reached. If there are more
5611 overlay strings to display, IT->string and
5612 IT->current.overlay_string_index are set appropriately here.
5613 Otherwise IT->string is set to nil. */
5615 static void
5616 next_overlay_string (struct it *it)
5618 ++it->current.overlay_string_index;
5619 if (it->current.overlay_string_index == it->n_overlay_strings)
5621 /* No more overlay strings. Restore IT's settings to what
5622 they were before overlay strings were processed, and
5623 continue to deliver from current_buffer. */
5625 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5626 pop_it (it);
5627 eassert (it->sp > 0
5628 || (NILP (it->string)
5629 && it->method == GET_FROM_BUFFER
5630 && it->stop_charpos >= BEGV
5631 && it->stop_charpos <= it->end_charpos));
5632 it->current.overlay_string_index = -1;
5633 it->n_overlay_strings = 0;
5634 /* If there's an empty display string on the stack, pop the
5635 stack, to resync the bidi iterator with IT's position. Such
5636 empty strings are pushed onto the stack in
5637 get_overlay_strings_1. */
5638 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5639 pop_it (it);
5641 /* Since we've exhausted overlay strings at this buffer
5642 position, set the flag to ignore overlays until we move to
5643 another position. (The flag will be reset in
5644 next_element_from_buffer.) But don't do that if the overlay
5645 strings were loaded at position other than the current one,
5646 which could happen if we called pop_it above, or if the
5647 overlay strings were loaded by handle_invisible_prop at the
5648 beginning of invisible text. */
5649 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5650 it->ignore_overlay_strings_at_pos_p = true;
5652 /* If we're at the end of the buffer, record that we have
5653 processed the overlay strings there already, so that
5654 next_element_from_buffer doesn't try it again. */
5655 if (NILP (it->string)
5656 && IT_CHARPOS (*it) >= it->end_charpos
5657 && it->overlay_strings_charpos >= it->end_charpos)
5658 it->overlay_strings_at_end_processed_p = true;
5659 /* Note: we reset overlay_strings_charpos only here, to make
5660 sure the just-processed overlays were indeed at EOB.
5661 Otherwise, overlays on text with invisible text property,
5662 which are processed with IT's position past the invisible
5663 text, might fool us into thinking the overlays at EOB were
5664 already processed (linum-mode can cause this, for
5665 example). */
5666 it->overlay_strings_charpos = -1;
5668 else
5670 /* There are more overlay strings to process. If
5671 IT->current.overlay_string_index has advanced to a position
5672 where we must load IT->overlay_strings with more strings, do
5673 it. We must load at the IT->overlay_strings_charpos where
5674 IT->n_overlay_strings was originally computed; when invisible
5675 text is present, this might not be IT_CHARPOS (Bug#7016). */
5676 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5678 if (it->current.overlay_string_index && i == 0)
5679 load_overlay_strings (it, it->overlay_strings_charpos);
5681 /* Initialize IT to deliver display elements from the overlay
5682 string. */
5683 it->string = it->overlay_strings[i];
5684 it->multibyte_p = STRING_MULTIBYTE (it->string);
5685 SET_TEXT_POS (it->current.string_pos, 0, 0);
5686 it->method = GET_FROM_STRING;
5687 it->stop_charpos = 0;
5688 it->end_charpos = SCHARS (it->string);
5689 if (it->cmp_it.stop_pos >= 0)
5690 it->cmp_it.stop_pos = 0;
5691 it->prev_stop = 0;
5692 it->base_level_stop = 0;
5694 /* Set up the bidi iterator for this overlay string. */
5695 if (it->bidi_p)
5697 it->bidi_it.string.lstring = it->string;
5698 it->bidi_it.string.s = NULL;
5699 it->bidi_it.string.schars = SCHARS (it->string);
5700 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5701 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5702 it->bidi_it.string.unibyte = !it->multibyte_p;
5703 it->bidi_it.w = it->w;
5704 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5708 CHECK_IT (it);
5712 /* Compare two overlay_entry structures E1 and E2. Used as a
5713 comparison function for qsort in load_overlay_strings. Overlay
5714 strings for the same position are sorted so that
5716 1. All after-strings come in front of before-strings, except
5717 when they come from the same overlay.
5719 2. Within after-strings, strings are sorted so that overlay strings
5720 from overlays with higher priorities come first.
5722 2. Within before-strings, strings are sorted so that overlay
5723 strings from overlays with higher priorities come last.
5725 Value is analogous to strcmp. */
5728 static int
5729 compare_overlay_entries (const void *e1, const void *e2)
5731 struct overlay_entry const *entry1 = e1;
5732 struct overlay_entry const *entry2 = e2;
5733 int result;
5735 if (entry1->after_string_p != entry2->after_string_p)
5737 /* Let after-strings appear in front of before-strings if
5738 they come from different overlays. */
5739 if (EQ (entry1->overlay, entry2->overlay))
5740 result = entry1->after_string_p ? 1 : -1;
5741 else
5742 result = entry1->after_string_p ? -1 : 1;
5744 else if (entry1->priority != entry2->priority)
5746 if (entry1->after_string_p)
5747 /* After-strings sorted in order of decreasing priority. */
5748 result = entry2->priority < entry1->priority ? -1 : 1;
5749 else
5750 /* Before-strings sorted in order of increasing priority. */
5751 result = entry1->priority < entry2->priority ? -1 : 1;
5753 else
5754 result = 0;
5756 return result;
5760 /* Load the vector IT->overlay_strings with overlay strings from IT's
5761 current buffer position, or from CHARPOS if that is > 0. Set
5762 IT->n_overlays to the total number of overlay strings found.
5764 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5765 a time. On entry into load_overlay_strings,
5766 IT->current.overlay_string_index gives the number of overlay
5767 strings that have already been loaded by previous calls to this
5768 function.
5770 IT->add_overlay_start contains an additional overlay start
5771 position to consider for taking overlay strings from, if non-zero.
5772 This position comes into play when the overlay has an `invisible'
5773 property, and both before and after-strings. When we've skipped to
5774 the end of the overlay, because of its `invisible' property, we
5775 nevertheless want its before-string to appear.
5776 IT->add_overlay_start will contain the overlay start position
5777 in this case.
5779 Overlay strings are sorted so that after-string strings come in
5780 front of before-string strings. Within before and after-strings,
5781 strings are sorted by overlay priority. See also function
5782 compare_overlay_entries. */
5784 static void
5785 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5787 Lisp_Object overlay, window, str, invisible;
5788 struct Lisp_Overlay *ov;
5789 ptrdiff_t start, end;
5790 ptrdiff_t n = 0, i, j;
5791 int invis;
5792 struct overlay_entry entriesbuf[20];
5793 ptrdiff_t size = ARRAYELTS (entriesbuf);
5794 struct overlay_entry *entries = entriesbuf;
5795 USE_SAFE_ALLOCA;
5797 if (charpos <= 0)
5798 charpos = IT_CHARPOS (*it);
5800 /* Append the overlay string STRING of overlay OVERLAY to vector
5801 `entries' which has size `size' and currently contains `n'
5802 elements. AFTER_P means STRING is an after-string of
5803 OVERLAY. */
5804 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5805 do \
5807 Lisp_Object priority; \
5809 if (n == size) \
5811 struct overlay_entry *old = entries; \
5812 SAFE_NALLOCA (entries, 2, size); \
5813 memcpy (entries, old, size * sizeof *entries); \
5814 size *= 2; \
5817 entries[n].string = (STRING); \
5818 entries[n].overlay = (OVERLAY); \
5819 priority = Foverlay_get ((OVERLAY), Qpriority); \
5820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5821 entries[n].after_string_p = (AFTER_P); \
5822 ++n; \
5824 while (false)
5826 /* Process overlay before the overlay center. */
5827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5829 XSETMISC (overlay, ov);
5830 eassert (OVERLAYP (overlay));
5831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5834 if (end < charpos)
5835 break;
5837 /* Skip this overlay if it doesn't start or end at IT's current
5838 position. */
5839 if (end != charpos && start != charpos)
5840 continue;
5842 /* Skip this overlay if it doesn't apply to IT->w. */
5843 window = Foverlay_get (overlay, Qwindow);
5844 if (WINDOWP (window) && XWINDOW (window) != it->w)
5845 continue;
5847 /* If the text ``under'' the overlay is invisible, both before-
5848 and after-strings from this overlay are visible; start and
5849 end position are indistinguishable. */
5850 invisible = Foverlay_get (overlay, Qinvisible);
5851 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5853 /* If overlay has a non-empty before-string, record it. */
5854 if ((start == charpos || (end == charpos && invis != 0))
5855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5856 && SCHARS (str))
5857 RECORD_OVERLAY_STRING (overlay, str, false);
5859 /* If overlay has a non-empty after-string, record it. */
5860 if ((end == charpos || (start == charpos && invis != 0))
5861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5862 && SCHARS (str))
5863 RECORD_OVERLAY_STRING (overlay, str, true);
5866 /* Process overlays after the overlay center. */
5867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5869 XSETMISC (overlay, ov);
5870 eassert (OVERLAYP (overlay));
5871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5874 if (start > charpos)
5875 break;
5877 /* Skip this overlay if it doesn't start or end at IT's current
5878 position. */
5879 if (end != charpos && start != charpos)
5880 continue;
5882 /* Skip this overlay if it doesn't apply to IT->w. */
5883 window = Foverlay_get (overlay, Qwindow);
5884 if (WINDOWP (window) && XWINDOW (window) != it->w)
5885 continue;
5887 /* If the text ``under'' the overlay is invisible, it has a zero
5888 dimension, and both before- and after-strings apply. */
5889 invisible = Foverlay_get (overlay, Qinvisible);
5890 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5892 /* If overlay has a non-empty before-string, record it. */
5893 if ((start == charpos || (end == charpos && invis != 0))
5894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5895 && SCHARS (str))
5896 RECORD_OVERLAY_STRING (overlay, str, false);
5898 /* If overlay has a non-empty after-string, record it. */
5899 if ((end == charpos || (start == charpos && invis != 0))
5900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5901 && SCHARS (str))
5902 RECORD_OVERLAY_STRING (overlay, str, true);
5905 #undef RECORD_OVERLAY_STRING
5907 /* Sort entries. */
5908 if (n > 1)
5909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5911 /* Record number of overlay strings, and where we computed it. */
5912 it->n_overlay_strings = n;
5913 it->overlay_strings_charpos = charpos;
5915 /* IT->current.overlay_string_index is the number of overlay strings
5916 that have already been consumed by IT. Copy some of the
5917 remaining overlay strings to IT->overlay_strings. */
5918 i = 0;
5919 j = it->current.overlay_string_index;
5920 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5922 it->overlay_strings[i] = entries[j].string;
5923 it->string_overlays[i++] = entries[j++].overlay;
5926 CHECK_IT (it);
5927 SAFE_FREE ();
5931 /* Get the first chunk of overlay strings at IT's current buffer
5932 position, or at CHARPOS if that is > 0. Value is true if at
5933 least one overlay string was found. */
5935 static bool
5936 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5938 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5939 process. This fills IT->overlay_strings with strings, and sets
5940 IT->n_overlay_strings to the total number of strings to process.
5941 IT->pos.overlay_string_index has to be set temporarily to zero
5942 because load_overlay_strings needs this; it must be set to -1
5943 when no overlay strings are found because a zero value would
5944 indicate a position in the first overlay string. */
5945 it->current.overlay_string_index = 0;
5946 load_overlay_strings (it, charpos);
5948 /* If we found overlay strings, set up IT to deliver display
5949 elements from the first one. Otherwise set up IT to deliver
5950 from current_buffer. */
5951 if (it->n_overlay_strings)
5953 /* Make sure we know settings in current_buffer, so that we can
5954 restore meaningful values when we're done with the overlay
5955 strings. */
5956 if (compute_stop_p)
5957 compute_stop_pos (it);
5958 eassert (it->face_id >= 0);
5960 /* Save IT's settings. They are restored after all overlay
5961 strings have been processed. */
5962 eassert (!compute_stop_p || it->sp == 0);
5964 /* When called from handle_stop, there might be an empty display
5965 string loaded. In that case, don't bother saving it. But
5966 don't use this optimization with the bidi iterator, since we
5967 need the corresponding pop_it call to resync the bidi
5968 iterator's position with IT's position, after we are done
5969 with the overlay strings. (The corresponding call to pop_it
5970 in case of an empty display string is in
5971 next_overlay_string.) */
5972 if (!(!it->bidi_p
5973 && STRINGP (it->string) && !SCHARS (it->string)))
5974 push_it (it, NULL);
5976 /* Set up IT to deliver display elements from the first overlay
5977 string. */
5978 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5979 it->string = it->overlay_strings[0];
5980 it->from_overlay = Qnil;
5981 it->stop_charpos = 0;
5982 eassert (STRINGP (it->string));
5983 it->end_charpos = SCHARS (it->string);
5984 it->prev_stop = 0;
5985 it->base_level_stop = 0;
5986 it->multibyte_p = STRING_MULTIBYTE (it->string);
5987 it->method = GET_FROM_STRING;
5988 it->from_disp_prop_p = 0;
5989 it->cmp_it.id = -1;
5991 /* Force paragraph direction to be that of the parent
5992 buffer. */
5993 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5994 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5995 else
5996 it->paragraph_embedding = L2R;
5998 /* Set up the bidi iterator for this overlay string. */
5999 if (it->bidi_p)
6001 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
6003 it->bidi_it.string.lstring = it->string;
6004 it->bidi_it.string.s = NULL;
6005 it->bidi_it.string.schars = SCHARS (it->string);
6006 it->bidi_it.string.bufpos = pos;
6007 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6008 it->bidi_it.string.unibyte = !it->multibyte_p;
6009 it->bidi_it.w = it->w;
6010 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6012 return true;
6015 it->current.overlay_string_index = -1;
6016 return false;
6019 static bool
6020 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6022 it->string = Qnil;
6023 it->method = GET_FROM_BUFFER;
6025 get_overlay_strings_1 (it, charpos, true);
6027 CHECK_IT (it);
6029 /* Value is true if we found at least one overlay string. */
6030 return STRINGP (it->string);
6035 /***********************************************************************
6036 Saving and restoring state
6037 ***********************************************************************/
6039 /* Save current settings of IT on IT->stack. Called, for example,
6040 before setting up IT for an overlay string, to be able to restore
6041 IT's settings to what they were after the overlay string has been
6042 processed. If POSITION is non-NULL, it is the position to save on
6043 the stack instead of IT->position. */
6045 static void
6046 push_it (struct it *it, struct text_pos *position)
6048 struct iterator_stack_entry *p;
6050 eassert (it->sp < IT_STACK_SIZE);
6051 p = it->stack + it->sp;
6053 p->stop_charpos = it->stop_charpos;
6054 p->prev_stop = it->prev_stop;
6055 p->base_level_stop = it->base_level_stop;
6056 p->cmp_it = it->cmp_it;
6057 eassert (it->face_id >= 0);
6058 p->face_id = it->face_id;
6059 p->string = it->string;
6060 p->method = it->method;
6061 p->from_overlay = it->from_overlay;
6062 switch (p->method)
6064 case GET_FROM_IMAGE:
6065 p->u.image.object = it->object;
6066 p->u.image.image_id = it->image_id;
6067 p->u.image.slice = it->slice;
6068 break;
6069 case GET_FROM_STRETCH:
6070 p->u.stretch.object = it->object;
6071 break;
6072 case GET_FROM_XWIDGET:
6073 p->u.xwidget.object = it->object;
6074 break;
6075 case GET_FROM_BUFFER:
6076 case GET_FROM_DISPLAY_VECTOR:
6077 case GET_FROM_STRING:
6078 case GET_FROM_C_STRING:
6079 break;
6080 default:
6081 emacs_abort ();
6083 p->position = position ? *position : it->position;
6084 p->current = it->current;
6085 p->end_charpos = it->end_charpos;
6086 p->string_nchars = it->string_nchars;
6087 p->area = it->area;
6088 p->multibyte_p = it->multibyte_p;
6089 p->avoid_cursor_p = it->avoid_cursor_p;
6090 p->space_width = it->space_width;
6091 p->font_height = it->font_height;
6092 p->voffset = it->voffset;
6093 p->string_from_display_prop_p = it->string_from_display_prop_p;
6094 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6095 p->display_ellipsis_p = false;
6096 p->line_wrap = it->line_wrap;
6097 p->bidi_p = it->bidi_p;
6098 p->paragraph_embedding = it->paragraph_embedding;
6099 p->from_disp_prop_p = it->from_disp_prop_p;
6100 ++it->sp;
6102 /* Save the state of the bidi iterator as well. */
6103 if (it->bidi_p)
6104 bidi_push_it (&it->bidi_it);
6107 static void
6108 iterate_out_of_display_property (struct it *it)
6110 bool buffer_p = !STRINGP (it->string);
6111 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6112 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6114 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6116 /* Maybe initialize paragraph direction. If we are at the beginning
6117 of a new paragraph, next_element_from_buffer may not have a
6118 chance to do that. */
6119 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6120 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6121 /* prev_stop can be zero, so check against BEGV as well. */
6122 while (it->bidi_it.charpos >= bob
6123 && it->prev_stop <= it->bidi_it.charpos
6124 && it->bidi_it.charpos < CHARPOS (it->position)
6125 && it->bidi_it.charpos < eob)
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 /* Record the stop_pos we just crossed, for when we cross it
6128 back, maybe. */
6129 if (it->bidi_it.charpos > CHARPOS (it->position))
6130 it->prev_stop = CHARPOS (it->position);
6131 /* If we ended up not where pop_it put us, resync IT's
6132 positional members with the bidi iterator. */
6133 if (it->bidi_it.charpos != CHARPOS (it->position))
6134 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6135 if (buffer_p)
6136 it->current.pos = it->position;
6137 else
6138 it->current.string_pos = it->position;
6141 /* Restore IT's settings from IT->stack. Called, for example, when no
6142 more overlay strings must be processed, and we return to delivering
6143 display elements from a buffer, or when the end of a string from a
6144 `display' property is reached and we return to delivering display
6145 elements from an overlay string, or from a buffer. */
6147 static void
6148 pop_it (struct it *it)
6150 struct iterator_stack_entry *p;
6151 bool from_display_prop = it->from_disp_prop_p;
6152 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6154 eassert (it->sp > 0);
6155 --it->sp;
6156 p = it->stack + it->sp;
6157 it->stop_charpos = p->stop_charpos;
6158 it->prev_stop = p->prev_stop;
6159 it->base_level_stop = p->base_level_stop;
6160 it->cmp_it = p->cmp_it;
6161 it->face_id = p->face_id;
6162 it->current = p->current;
6163 it->position = p->position;
6164 it->string = p->string;
6165 it->from_overlay = p->from_overlay;
6166 if (NILP (it->string))
6167 SET_TEXT_POS (it->current.string_pos, -1, -1);
6168 it->method = p->method;
6169 switch (it->method)
6171 case GET_FROM_IMAGE:
6172 it->image_id = p->u.image.image_id;
6173 it->object = p->u.image.object;
6174 it->slice = p->u.image.slice;
6175 break;
6176 case GET_FROM_XWIDGET:
6177 it->object = p->u.xwidget.object;
6178 break;
6179 case GET_FROM_STRETCH:
6180 it->object = p->u.stretch.object;
6181 break;
6182 case GET_FROM_BUFFER:
6183 it->object = it->w->contents;
6184 break;
6185 case GET_FROM_STRING:
6187 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6189 /* Restore the face_box_p flag, since it could have been
6190 overwritten by the face of the object that we just finished
6191 displaying. */
6192 if (face)
6193 it->face_box_p = face->box != FACE_NO_BOX;
6194 it->object = it->string;
6196 break;
6197 case GET_FROM_DISPLAY_VECTOR:
6198 if (it->s)
6199 it->method = GET_FROM_C_STRING;
6200 else if (STRINGP (it->string))
6201 it->method = GET_FROM_STRING;
6202 else
6204 it->method = GET_FROM_BUFFER;
6205 it->object = it->w->contents;
6207 break;
6208 case GET_FROM_C_STRING:
6209 break;
6210 default:
6211 emacs_abort ();
6213 it->end_charpos = p->end_charpos;
6214 it->string_nchars = p->string_nchars;
6215 it->area = p->area;
6216 it->multibyte_p = p->multibyte_p;
6217 it->avoid_cursor_p = p->avoid_cursor_p;
6218 it->space_width = p->space_width;
6219 it->font_height = p->font_height;
6220 it->voffset = p->voffset;
6221 it->string_from_display_prop_p = p->string_from_display_prop_p;
6222 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6223 it->line_wrap = p->line_wrap;
6224 it->bidi_p = p->bidi_p;
6225 it->paragraph_embedding = p->paragraph_embedding;
6226 it->from_disp_prop_p = p->from_disp_prop_p;
6227 if (it->bidi_p)
6229 bidi_pop_it (&it->bidi_it);
6230 /* Bidi-iterate until we get out of the portion of text, if any,
6231 covered by a `display' text property or by an overlay with
6232 `display' property. (We cannot just jump there, because the
6233 internal coherency of the bidi iterator state can not be
6234 preserved across such jumps.) We also must determine the
6235 paragraph base direction if the overlay we just processed is
6236 at the beginning of a new paragraph. */
6237 if (from_display_prop
6238 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6239 iterate_out_of_display_property (it);
6241 eassert ((BUFFERP (it->object)
6242 && IT_CHARPOS (*it) == it->bidi_it.charpos
6243 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6244 || (STRINGP (it->object)
6245 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6246 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6247 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6249 /* If we move the iterator over text covered by a display property
6250 to a new buffer position, any info about previously seen overlays
6251 is no longer valid. */
6252 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6253 it->ignore_overlay_strings_at_pos_p = false;
6258 /***********************************************************************
6259 Moving over lines
6260 ***********************************************************************/
6262 /* Set IT's current position to the previous line start. */
6264 static void
6265 back_to_previous_line_start (struct it *it)
6267 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6269 DEC_BOTH (cp, bp);
6270 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6274 /* Move IT to the next line start.
6276 Value is true if a newline was found. Set *SKIPPED_P to true if
6277 we skipped over part of the text (as opposed to moving the iterator
6278 continuously over the text). Otherwise, don't change the value
6279 of *SKIPPED_P.
6281 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6282 iterator on the newline, if it was found.
6284 Newlines may come from buffer text, overlay strings, or strings
6285 displayed via the `display' property. That's the reason we can't
6286 simply use find_newline_no_quit.
6288 Note that this function may not skip over invisible text that is so
6289 because of text properties and immediately follows a newline. If
6290 it would, function reseat_at_next_visible_line_start, when called
6291 from set_iterator_to_next, would effectively make invisible
6292 characters following a newline part of the wrong glyph row, which
6293 leads to wrong cursor motion. */
6295 static bool
6296 forward_to_next_line_start (struct it *it, bool *skipped_p,
6297 struct bidi_it *bidi_it_prev)
6299 ptrdiff_t old_selective;
6300 bool newline_found_p = false;
6301 int n;
6302 const int MAX_NEWLINE_DISTANCE = 500;
6304 /* If already on a newline, just consume it to avoid unintended
6305 skipping over invisible text below. */
6306 if (it->what == IT_CHARACTER
6307 && it->c == '\n'
6308 && CHARPOS (it->position) == IT_CHARPOS (*it))
6310 if (it->bidi_p && bidi_it_prev)
6311 *bidi_it_prev = it->bidi_it;
6312 set_iterator_to_next (it, false);
6313 it->c = 0;
6314 return true;
6317 /* Don't handle selective display in the following. It's (a)
6318 unnecessary because it's done by the caller, and (b) leads to an
6319 infinite recursion because next_element_from_ellipsis indirectly
6320 calls this function. */
6321 old_selective = it->selective;
6322 it->selective = 0;
6324 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6325 from buffer text. */
6326 for (n = 0;
6327 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6328 n += !STRINGP (it->string))
6330 if (!get_next_display_element (it))
6331 return false;
6332 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6333 if (newline_found_p && it->bidi_p && bidi_it_prev)
6334 *bidi_it_prev = it->bidi_it;
6335 set_iterator_to_next (it, false);
6338 /* If we didn't find a newline near enough, see if we can use a
6339 short-cut. */
6340 if (!newline_found_p)
6342 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6343 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6344 1, &bytepos);
6345 Lisp_Object pos;
6347 eassert (!STRINGP (it->string));
6349 /* If there isn't any `display' property in sight, and no
6350 overlays, we can just use the position of the newline in
6351 buffer text. */
6352 if (it->stop_charpos >= limit
6353 || ((pos = Fnext_single_property_change (make_number (start),
6354 Qdisplay, Qnil,
6355 make_number (limit)),
6356 NILP (pos))
6357 && next_overlay_change (start) == ZV))
6359 if (!it->bidi_p)
6361 IT_CHARPOS (*it) = limit;
6362 IT_BYTEPOS (*it) = bytepos;
6364 else
6366 struct bidi_it bprev;
6368 /* Help bidi.c avoid expensive searches for display
6369 properties and overlays, by telling it that there are
6370 none up to `limit'. */
6371 if (it->bidi_it.disp_pos < limit)
6373 it->bidi_it.disp_pos = limit;
6374 it->bidi_it.disp_prop = 0;
6376 do {
6377 bprev = it->bidi_it;
6378 bidi_move_to_visually_next (&it->bidi_it);
6379 } while (it->bidi_it.charpos != limit);
6380 IT_CHARPOS (*it) = limit;
6381 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6382 if (bidi_it_prev)
6383 *bidi_it_prev = bprev;
6385 *skipped_p = newline_found_p = true;
6387 else
6389 while (!newline_found_p)
6391 if (!get_next_display_element (it))
6392 break;
6393 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6394 if (newline_found_p && it->bidi_p && bidi_it_prev)
6395 *bidi_it_prev = it->bidi_it;
6396 set_iterator_to_next (it, false);
6401 it->selective = old_selective;
6402 return newline_found_p;
6406 /* Set IT's current position to the previous visible line start. Skip
6407 invisible text that is so either due to text properties or due to
6408 selective display. Caution: this does not change IT->current_x and
6409 IT->hpos. */
6411 static void
6412 back_to_previous_visible_line_start (struct it *it)
6414 while (IT_CHARPOS (*it) > BEGV)
6416 back_to_previous_line_start (it);
6418 if (IT_CHARPOS (*it) <= BEGV)
6419 break;
6421 /* If selective > 0, then lines indented more than its value are
6422 invisible. */
6423 if (it->selective > 0
6424 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6425 it->selective))
6426 continue;
6428 /* Check the newline before point for invisibility. */
6430 Lisp_Object prop;
6431 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6432 Qinvisible, it->window);
6433 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6434 continue;
6437 if (IT_CHARPOS (*it) <= BEGV)
6438 break;
6441 struct it it2;
6442 void *it2data = NULL;
6443 ptrdiff_t pos;
6444 ptrdiff_t beg, end;
6445 Lisp_Object val, overlay;
6447 SAVE_IT (it2, *it, it2data);
6449 /* If newline is part of a composition, continue from start of composition */
6450 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6451 && beg < IT_CHARPOS (*it))
6452 goto replaced;
6454 /* If newline is replaced by a display property, find start of overlay
6455 or interval and continue search from that point. */
6456 pos = --IT_CHARPOS (it2);
6457 --IT_BYTEPOS (it2);
6458 it2.sp = 0;
6459 bidi_unshelve_cache (NULL, false);
6460 it2.string_from_display_prop_p = false;
6461 it2.from_disp_prop_p = false;
6462 if (handle_display_prop (&it2) == HANDLED_RETURN
6463 && !NILP (val = get_char_property_and_overlay
6464 (make_number (pos), Qdisplay, Qnil, &overlay))
6465 && (OVERLAYP (overlay)
6466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6469 RESTORE_IT (it, it, it2data);
6470 goto replaced;
6473 /* Newline is not replaced by anything -- so we are done. */
6474 RESTORE_IT (it, it, it2data);
6475 break;
6477 replaced:
6478 if (beg < BEGV)
6479 beg = BEGV;
6480 IT_CHARPOS (*it) = beg;
6481 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6485 it->continuation_lines_width = 0;
6487 eassert (IT_CHARPOS (*it) >= BEGV);
6488 eassert (IT_CHARPOS (*it) == BEGV
6489 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6490 CHECK_IT (it);
6494 /* Reseat iterator IT at the previous visible line start. Skip
6495 invisible text that is so either due to text properties or due to
6496 selective display. At the end, update IT's overlay information,
6497 face information etc. */
6499 void
6500 reseat_at_previous_visible_line_start (struct it *it)
6502 back_to_previous_visible_line_start (it);
6503 reseat (it, it->current.pos, true);
6504 CHECK_IT (it);
6508 /* Reseat iterator IT on the next visible line start in the current
6509 buffer. ON_NEWLINE_P means position IT on the newline
6510 preceding the line start. Skip over invisible text that is so
6511 because of selective display. Compute faces, overlays etc at the
6512 new position. Note that this function does not skip over text that
6513 is invisible because of text properties. */
6515 static void
6516 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6518 bool skipped_p = false;
6519 struct bidi_it bidi_it_prev;
6520 bool newline_found_p
6521 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6523 /* Skip over lines that are invisible because they are indented
6524 more than the value of IT->selective. */
6525 if (it->selective > 0)
6526 while (IT_CHARPOS (*it) < ZV
6527 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6528 it->selective))
6530 eassert (IT_BYTEPOS (*it) == BEGV
6531 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6532 newline_found_p =
6533 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6536 /* Position on the newline if that's what's requested. */
6537 if (on_newline_p && newline_found_p)
6539 if (STRINGP (it->string))
6541 if (IT_STRING_CHARPOS (*it) > 0)
6543 if (!it->bidi_p)
6545 --IT_STRING_CHARPOS (*it);
6546 --IT_STRING_BYTEPOS (*it);
6548 else
6550 /* We need to restore the bidi iterator to the state
6551 it had on the newline, and resync the IT's
6552 position with that. */
6553 it->bidi_it = bidi_it_prev;
6554 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6555 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6559 else if (IT_CHARPOS (*it) > BEGV)
6561 if (!it->bidi_p)
6563 --IT_CHARPOS (*it);
6564 --IT_BYTEPOS (*it);
6566 else
6568 /* We need to restore the bidi iterator to the state it
6569 had on the newline and resync IT with that. */
6570 it->bidi_it = bidi_it_prev;
6571 IT_CHARPOS (*it) = it->bidi_it.charpos;
6572 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6574 reseat (it, it->current.pos, false);
6577 else if (skipped_p)
6578 reseat (it, it->current.pos, false);
6580 CHECK_IT (it);
6585 /***********************************************************************
6586 Changing an iterator's position
6587 ***********************************************************************/
6589 /* Change IT's current position to POS in current_buffer.
6590 If FORCE_P, always check for text properties at the new position.
6591 Otherwise, text properties are only looked up if POS >=
6592 IT->check_charpos of a property. */
6594 static void
6595 reseat (struct it *it, struct text_pos pos, bool force_p)
6597 ptrdiff_t original_pos = IT_CHARPOS (*it);
6599 reseat_1 (it, pos, false);
6601 /* Determine where to check text properties. Avoid doing it
6602 where possible because text property lookup is very expensive. */
6603 if (force_p
6604 || CHARPOS (pos) > it->stop_charpos
6605 || CHARPOS (pos) < original_pos)
6607 if (it->bidi_p)
6609 /* For bidi iteration, we need to prime prev_stop and
6610 base_level_stop with our best estimations. */
6611 /* Implementation note: Of course, POS is not necessarily a
6612 stop position, so assigning prev_pos to it is a lie; we
6613 should have called compute_stop_backwards. However, if
6614 the current buffer does not include any R2L characters,
6615 that call would be a waste of cycles, because the
6616 iterator will never move back, and thus never cross this
6617 "fake" stop position. So we delay that backward search
6618 until the time we really need it, in next_element_from_buffer. */
6619 if (CHARPOS (pos) != it->prev_stop)
6620 it->prev_stop = CHARPOS (pos);
6621 if (CHARPOS (pos) < it->base_level_stop)
6622 it->base_level_stop = 0; /* meaning it's unknown */
6623 handle_stop (it);
6625 else
6627 handle_stop (it);
6628 it->prev_stop = it->base_level_stop = 0;
6633 CHECK_IT (it);
6637 /* Change IT's buffer position to POS. SET_STOP_P means set
6638 IT->stop_pos to POS, also. */
6640 static void
6641 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6643 /* Don't call this function when scanning a C string. */
6644 eassert (it->s == NULL);
6646 /* POS must be a reasonable value. */
6647 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6649 it->current.pos = it->position = pos;
6650 it->end_charpos = ZV;
6651 it->dpvec = NULL;
6652 it->current.dpvec_index = -1;
6653 it->current.overlay_string_index = -1;
6654 IT_STRING_CHARPOS (*it) = -1;
6655 IT_STRING_BYTEPOS (*it) = -1;
6656 it->string = Qnil;
6657 it->method = GET_FROM_BUFFER;
6658 it->object = it->w->contents;
6659 it->area = TEXT_AREA;
6660 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6661 it->sp = 0;
6662 it->string_from_display_prop_p = false;
6663 it->string_from_prefix_prop_p = false;
6665 it->from_disp_prop_p = false;
6666 it->face_before_selective_p = false;
6667 if (it->bidi_p)
6669 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6670 &it->bidi_it);
6671 bidi_unshelve_cache (NULL, false);
6672 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6673 it->bidi_it.string.s = NULL;
6674 it->bidi_it.string.lstring = Qnil;
6675 it->bidi_it.string.bufpos = 0;
6676 it->bidi_it.string.from_disp_str = false;
6677 it->bidi_it.string.unibyte = false;
6678 it->bidi_it.w = it->w;
6681 if (set_stop_p)
6683 it->stop_charpos = CHARPOS (pos);
6684 it->base_level_stop = CHARPOS (pos);
6686 /* This make the information stored in it->cmp_it invalidate. */
6687 it->cmp_it.id = -1;
6691 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6692 If S is non-null, it is a C string to iterate over. Otherwise,
6693 STRING gives a Lisp string to iterate over.
6695 If PRECISION > 0, don't return more then PRECISION number of
6696 characters from the string.
6698 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6699 characters have been returned. FIELD_WIDTH < 0 means an infinite
6700 field width.
6702 MULTIBYTE = 0 means disable processing of multibyte characters,
6703 MULTIBYTE > 0 means enable it,
6704 MULTIBYTE < 0 means use IT->multibyte_p.
6706 IT must be initialized via a prior call to init_iterator before
6707 calling this function. */
6709 static void
6710 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6711 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6712 int multibyte)
6714 /* No text property checks performed by default, but see below. */
6715 it->stop_charpos = -1;
6717 /* Set iterator position and end position. */
6718 memset (&it->current, 0, sizeof it->current);
6719 it->current.overlay_string_index = -1;
6720 it->current.dpvec_index = -1;
6721 eassert (charpos >= 0);
6723 /* If STRING is specified, use its multibyteness, otherwise use the
6724 setting of MULTIBYTE, if specified. */
6725 if (multibyte >= 0)
6726 it->multibyte_p = multibyte > 0;
6728 /* Bidirectional reordering of strings is controlled by the default
6729 value of bidi-display-reordering. Don't try to reorder while
6730 loading loadup.el, as the necessary character property tables are
6731 not yet available. */
6732 it->bidi_p =
6733 !redisplay__inhibit_bidi
6734 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6736 if (s == NULL)
6738 eassert (STRINGP (string));
6739 it->string = string;
6740 it->s = NULL;
6741 it->end_charpos = it->string_nchars = SCHARS (string);
6742 it->method = GET_FROM_STRING;
6743 it->current.string_pos = string_pos (charpos, string);
6745 if (it->bidi_p)
6747 it->bidi_it.string.lstring = string;
6748 it->bidi_it.string.s = NULL;
6749 it->bidi_it.string.schars = it->end_charpos;
6750 it->bidi_it.string.bufpos = 0;
6751 it->bidi_it.string.from_disp_str = false;
6752 it->bidi_it.string.unibyte = !it->multibyte_p;
6753 it->bidi_it.w = it->w;
6754 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6755 FRAME_WINDOW_P (it->f), &it->bidi_it);
6758 else
6760 it->s = (const unsigned char *) s;
6761 it->string = Qnil;
6763 /* Note that we use IT->current.pos, not it->current.string_pos,
6764 for displaying C strings. */
6765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6766 if (it->multibyte_p)
6768 it->current.pos = c_string_pos (charpos, s, true);
6769 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6771 else
6773 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6774 it->end_charpos = it->string_nchars = strlen (s);
6777 if (it->bidi_p)
6779 it->bidi_it.string.lstring = Qnil;
6780 it->bidi_it.string.s = (const unsigned char *) s;
6781 it->bidi_it.string.schars = it->end_charpos;
6782 it->bidi_it.string.bufpos = 0;
6783 it->bidi_it.string.from_disp_str = false;
6784 it->bidi_it.string.unibyte = !it->multibyte_p;
6785 it->bidi_it.w = it->w;
6786 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6787 &it->bidi_it);
6789 it->method = GET_FROM_C_STRING;
6792 /* PRECISION > 0 means don't return more than PRECISION characters
6793 from the string. */
6794 if (precision > 0 && it->end_charpos - charpos > precision)
6796 it->end_charpos = it->string_nchars = charpos + precision;
6797 if (it->bidi_p)
6798 it->bidi_it.string.schars = it->end_charpos;
6801 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6802 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6803 FIELD_WIDTH < 0 means infinite field width. This is useful for
6804 padding with `-' at the end of a mode line. */
6805 if (field_width < 0)
6806 field_width = DISP_INFINITY;
6807 /* Implementation note: We deliberately don't enlarge
6808 it->bidi_it.string.schars here to fit it->end_charpos, because
6809 the bidi iterator cannot produce characters out of thin air. */
6810 if (field_width > it->end_charpos - charpos)
6811 it->end_charpos = charpos + field_width;
6813 /* Use the standard display table for displaying strings. */
6814 if (DISP_TABLE_P (Vstandard_display_table))
6815 it->dp = XCHAR_TABLE (Vstandard_display_table);
6817 it->stop_charpos = charpos;
6818 it->prev_stop = charpos;
6819 it->base_level_stop = 0;
6820 if (it->bidi_p)
6822 it->bidi_it.first_elt = true;
6823 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6824 it->bidi_it.disp_pos = -1;
6826 if (s == NULL && it->multibyte_p)
6828 ptrdiff_t endpos = SCHARS (it->string);
6829 if (endpos > it->end_charpos)
6830 endpos = it->end_charpos;
6831 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6832 it->string);
6834 CHECK_IT (it);
6839 /***********************************************************************
6840 Iteration
6841 ***********************************************************************/
6843 /* Map enum it_method value to corresponding next_element_from_* function. */
6845 typedef bool (*next_element_function) (struct it *);
6847 static next_element_function const get_next_element[NUM_IT_METHODS] =
6849 next_element_from_buffer,
6850 next_element_from_display_vector,
6851 next_element_from_string,
6852 next_element_from_c_string,
6853 next_element_from_image,
6854 next_element_from_stretch,
6855 next_element_from_xwidget,
6858 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6861 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6862 (possibly with the following characters). */
6864 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6865 ((IT)->cmp_it.id >= 0 \
6866 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6867 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6868 END_CHARPOS, (IT)->w, \
6869 FACE_FROM_ID_OR_NULL ((IT)->f, \
6870 (IT)->face_id), \
6871 (IT)->string)))
6874 /* Lookup the char-table Vglyphless_char_display for character C (-1
6875 if we want information for no-font case), and return the display
6876 method symbol. By side-effect, update it->what and
6877 it->glyphless_method. This function is called from
6878 get_next_display_element for each character element, and from
6879 x_produce_glyphs when no suitable font was found. */
6881 Lisp_Object
6882 lookup_glyphless_char_display (int c, struct it *it)
6884 Lisp_Object glyphless_method = Qnil;
6886 if (CHAR_TABLE_P (Vglyphless_char_display)
6887 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6889 if (c >= 0)
6891 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6892 if (CONSP (glyphless_method))
6893 glyphless_method = FRAME_WINDOW_P (it->f)
6894 ? XCAR (glyphless_method)
6895 : XCDR (glyphless_method);
6897 else
6898 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6901 retry:
6902 if (NILP (glyphless_method))
6904 if (c >= 0)
6905 /* The default is to display the character by a proper font. */
6906 return Qnil;
6907 /* The default for the no-font case is to display an empty box. */
6908 glyphless_method = Qempty_box;
6910 if (EQ (glyphless_method, Qzero_width))
6912 if (c >= 0)
6913 return glyphless_method;
6914 /* This method can't be used for the no-font case. */
6915 glyphless_method = Qempty_box;
6917 if (EQ (glyphless_method, Qthin_space))
6918 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6919 else if (EQ (glyphless_method, Qempty_box))
6920 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6921 else if (EQ (glyphless_method, Qhex_code))
6922 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6923 else if (STRINGP (glyphless_method))
6924 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6925 else
6927 /* Invalid value. We use the default method. */
6928 glyphless_method = Qnil;
6929 goto retry;
6931 it->what = IT_GLYPHLESS;
6932 return glyphless_method;
6935 /* Merge escape glyph face and cache the result. */
6937 static struct frame *last_escape_glyph_frame = NULL;
6938 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6939 static int last_escape_glyph_merged_face_id = 0;
6941 static int
6942 merge_escape_glyph_face (struct it *it)
6944 int face_id;
6946 if (it->f == last_escape_glyph_frame
6947 && it->face_id == last_escape_glyph_face_id)
6948 face_id = last_escape_glyph_merged_face_id;
6949 else
6951 /* Merge the `escape-glyph' face into the current face. */
6952 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6953 last_escape_glyph_frame = it->f;
6954 last_escape_glyph_face_id = it->face_id;
6955 last_escape_glyph_merged_face_id = face_id;
6957 return face_id;
6960 /* Likewise for glyphless glyph face. */
6962 static struct frame *last_glyphless_glyph_frame = NULL;
6963 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6964 static int last_glyphless_glyph_merged_face_id = 0;
6967 merge_glyphless_glyph_face (struct it *it)
6969 int face_id;
6971 if (it->f == last_glyphless_glyph_frame
6972 && it->face_id == last_glyphless_glyph_face_id)
6973 face_id = last_glyphless_glyph_merged_face_id;
6974 else
6976 /* Merge the `glyphless-char' face into the current face. */
6977 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6978 last_glyphless_glyph_frame = it->f;
6979 last_glyphless_glyph_face_id = it->face_id;
6980 last_glyphless_glyph_merged_face_id = face_id;
6982 return face_id;
6985 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6986 be called before redisplaying windows, and when the frame's face
6987 cache is freed. */
6988 void
6989 forget_escape_and_glyphless_faces (void)
6991 last_escape_glyph_frame = NULL;
6992 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6993 last_glyphless_glyph_frame = NULL;
6994 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6997 /* Load IT's display element fields with information about the next
6998 display element from the current position of IT. Value is false if
6999 end of buffer (or C string) is reached. */
7001 static bool
7002 get_next_display_element (struct it *it)
7004 /* True means that we found a display element. False means that
7005 we hit the end of what we iterate over. Performance note: the
7006 function pointer `method' used here turns out to be faster than
7007 using a sequence of if-statements. */
7008 bool success_p;
7010 get_next:
7011 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7013 if (it->what == IT_CHARACTER)
7015 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7016 and only if (a) the resolved directionality of that character
7017 is R..." */
7018 /* FIXME: Do we need an exception for characters from display
7019 tables? */
7020 if (it->bidi_p && it->bidi_it.type == STRONG_R
7021 && !inhibit_bidi_mirroring)
7022 it->c = bidi_mirror_char (it->c);
7023 /* Map via display table or translate control characters.
7024 IT->c, IT->len etc. have been set to the next character by
7025 the function call above. If we have a display table, and it
7026 contains an entry for IT->c, translate it. Don't do this if
7027 IT->c itself comes from a display table, otherwise we could
7028 end up in an infinite recursion. (An alternative could be to
7029 count the recursion depth of this function and signal an
7030 error when a certain maximum depth is reached.) Is it worth
7031 it? */
7032 if (success_p && it->dpvec == NULL)
7034 Lisp_Object dv;
7035 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7036 bool nonascii_space_p = false;
7037 bool nonascii_hyphen_p = false;
7038 int c = it->c; /* This is the character to display. */
7040 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7042 eassert (SINGLE_BYTE_CHAR_P (c));
7043 if (unibyte_display_via_language_environment)
7045 c = DECODE_CHAR (unibyte, c);
7046 if (c < 0)
7047 c = BYTE8_TO_CHAR (it->c);
7049 else
7050 c = BYTE8_TO_CHAR (it->c);
7053 if (it->dp
7054 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7055 VECTORP (dv)))
7057 struct Lisp_Vector *v = XVECTOR (dv);
7059 /* Return the first character from the display table
7060 entry, if not empty. If empty, don't display the
7061 current character. */
7062 if (v->header.size)
7064 it->dpvec_char_len = it->len;
7065 it->dpvec = v->contents;
7066 it->dpend = v->contents + v->header.size;
7067 it->current.dpvec_index = 0;
7068 it->dpvec_face_id = -1;
7069 it->saved_face_id = it->face_id;
7070 it->method = GET_FROM_DISPLAY_VECTOR;
7071 it->ellipsis_p = false;
7073 else
7075 set_iterator_to_next (it, false);
7077 goto get_next;
7080 if (! NILP (lookup_glyphless_char_display (c, it)))
7082 if (it->what == IT_GLYPHLESS)
7083 goto done;
7084 /* Don't display this character. */
7085 set_iterator_to_next (it, false);
7086 goto get_next;
7089 /* If `nobreak-char-display' is non-nil, we display
7090 non-ASCII spaces and hyphens specially. */
7091 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7093 if (c == NO_BREAK_SPACE)
7094 nonascii_space_p = true;
7095 else if (c == SOFT_HYPHEN || c == HYPHEN
7096 || c == NON_BREAKING_HYPHEN)
7097 nonascii_hyphen_p = true;
7100 /* Translate control characters into `\003' or `^C' form.
7101 Control characters coming from a display table entry are
7102 currently not translated because we use IT->dpvec to hold
7103 the translation. This could easily be changed but I
7104 don't believe that it is worth doing.
7106 The characters handled by `nobreak-char-display' must be
7107 translated too.
7109 Non-printable characters and raw-byte characters are also
7110 translated to octal or hexadecimal form. */
7111 if (((c < ' ' || c == 127) /* ASCII control chars. */
7112 ? (it->area != TEXT_AREA
7113 /* In mode line, treat \n, \t like other crl chars. */
7114 || (c != '\t'
7115 && it->glyph_row
7116 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7117 || (c != '\n' && c != '\t'))
7118 : (nonascii_space_p
7119 || nonascii_hyphen_p
7120 || CHAR_BYTE8_P (c)
7121 || ! CHAR_PRINTABLE_P (c))))
7123 /* C is a control character, non-ASCII space/hyphen,
7124 raw-byte, or a non-printable character which must be
7125 displayed either as '\003' or as `^C' where the '\\'
7126 and '^' can be defined in the display table. Fill
7127 IT->ctl_chars with glyphs for what we have to
7128 display. Then, set IT->dpvec to these glyphs. */
7129 Lisp_Object gc;
7130 int ctl_len;
7131 int face_id;
7132 int lface_id = 0;
7133 int escape_glyph;
7135 /* Handle control characters with ^. */
7137 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7139 int g;
7141 g = '^'; /* default glyph for Control */
7142 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7143 if (it->dp
7144 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7146 g = GLYPH_CODE_CHAR (gc);
7147 lface_id = GLYPH_CODE_FACE (gc);
7150 face_id = (lface_id
7151 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7152 : merge_escape_glyph_face (it));
7154 XSETINT (it->ctl_chars[0], g);
7155 XSETINT (it->ctl_chars[1], c ^ 0100);
7156 ctl_len = 2;
7157 goto display_control;
7160 /* Handle non-ascii space in the mode where it only gets
7161 highlighting. */
7163 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7165 /* Merge `nobreak-space' into the current face. */
7166 face_id = merge_faces (it->f, Qnobreak_space, 0,
7167 it->face_id);
7168 XSETINT (it->ctl_chars[0], ' ');
7169 ctl_len = 1;
7170 goto display_control;
7173 /* Handle non-ascii hyphens in the mode where it only
7174 gets highlighting. */
7176 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7178 /* Merge `nobreak-space' into the current face. */
7179 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7180 it->face_id);
7181 XSETINT (it->ctl_chars[0], '-');
7182 ctl_len = 1;
7183 goto display_control;
7186 /* Handle sequences that start with the "escape glyph". */
7188 /* the default escape glyph is \. */
7189 escape_glyph = '\\';
7191 if (it->dp
7192 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7194 escape_glyph = GLYPH_CODE_CHAR (gc);
7195 lface_id = GLYPH_CODE_FACE (gc);
7198 face_id = (lface_id
7199 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7200 : merge_escape_glyph_face (it));
7202 /* Draw non-ASCII space/hyphen with escape glyph: */
7204 if (nonascii_space_p || nonascii_hyphen_p)
7206 XSETINT (it->ctl_chars[0], escape_glyph);
7207 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7208 ctl_len = 2;
7209 goto display_control;
7213 char str[10];
7214 int len, i;
7216 if (CHAR_BYTE8_P (c))
7217 /* Display \200 or \x80 instead of \17777600. */
7218 c = CHAR_TO_BYTE8 (c);
7219 const char *format_string = display_raw_bytes_as_hex
7220 ? "x%02x"
7221 : "%03o";
7222 len = sprintf (str, format_string, c + 0u);
7224 XSETINT (it->ctl_chars[0], escape_glyph);
7225 for (i = 0; i < len; i++)
7226 XSETINT (it->ctl_chars[i + 1], str[i]);
7227 ctl_len = len + 1;
7230 display_control:
7231 /* Set up IT->dpvec and return first character from it. */
7232 it->dpvec_char_len = it->len;
7233 it->dpvec = it->ctl_chars;
7234 it->dpend = it->dpvec + ctl_len;
7235 it->current.dpvec_index = 0;
7236 it->dpvec_face_id = face_id;
7237 it->saved_face_id = it->face_id;
7238 it->method = GET_FROM_DISPLAY_VECTOR;
7239 it->ellipsis_p = false;
7240 goto get_next;
7242 it->char_to_display = c;
7244 else if (success_p)
7246 it->char_to_display = it->c;
7250 #ifdef HAVE_WINDOW_SYSTEM
7251 /* Adjust face id for a multibyte character. There are no multibyte
7252 character in unibyte text. */
7253 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7254 && it->multibyte_p
7255 && success_p
7256 && FRAME_WINDOW_P (it->f))
7258 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7260 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7262 /* Automatic composition with glyph-string. */
7263 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7265 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7267 else
7269 ptrdiff_t pos = (it->s ? -1
7270 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7271 : IT_CHARPOS (*it));
7272 int c;
7274 if (it->what == IT_CHARACTER)
7275 c = it->char_to_display;
7276 else
7278 struct composition *cmp = composition_table[it->cmp_it.id];
7279 int i;
7281 c = ' ';
7282 for (i = 0; i < cmp->glyph_len; i++)
7283 /* TAB in a composition means display glyphs with
7284 padding space on the left or right. */
7285 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7286 break;
7288 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7291 #endif /* HAVE_WINDOW_SYSTEM */
7293 done:
7294 /* Is this character the last one of a run of characters with
7295 box? If yes, set IT->end_of_box_run_p to true. */
7296 if (it->face_box_p
7297 && it->s == NULL)
7299 if (it->method == GET_FROM_STRING && it->sp)
7301 int face_id = underlying_face_id (it);
7302 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7304 if (face)
7306 if (face->box == FACE_NO_BOX)
7308 /* If the box comes from face properties in a
7309 display string, check faces in that string. */
7310 int string_face_id = face_after_it_pos (it);
7311 it->end_of_box_run_p
7312 = (FACE_FROM_ID (it->f, string_face_id)->box
7313 == FACE_NO_BOX);
7315 /* Otherwise, the box comes from the underlying face.
7316 If this is the last string character displayed, check
7317 the next buffer location. */
7318 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7319 /* n_overlay_strings is unreliable unless
7320 overlay_string_index is non-negative. */
7321 && ((it->current.overlay_string_index >= 0
7322 && (it->current.overlay_string_index
7323 == it->n_overlay_strings - 1))
7324 /* A string from display property. */
7325 || it->from_disp_prop_p))
7327 ptrdiff_t ignore;
7328 int next_face_id;
7329 bool text_from_string = false;
7330 /* Normally, the next buffer location is stored in
7331 IT->current.pos... */
7332 struct text_pos pos = it->current.pos;
7334 /* ...but for a string from a display property, the
7335 next buffer position is stored in the 'position'
7336 member of the iteration stack slot below the
7337 current one, see handle_single_display_spec. By
7338 contrast, it->current.pos was not yet updated to
7339 point to that buffer position; that will happen
7340 in pop_it, after we finish displaying the current
7341 string. Note that we already checked above that
7342 it->sp is positive, so subtracting one from it is
7343 safe. */
7344 if (it->from_disp_prop_p)
7346 int stackp = it->sp - 1;
7348 /* Find the stack level with data from buffer. */
7349 while (stackp >= 0
7350 && STRINGP ((it->stack + stackp)->string))
7351 stackp--;
7352 if (stackp < 0)
7354 /* If no stack slot was found for iterating
7355 a buffer, we are displaying text from a
7356 string, most probably the mode line or
7357 the header line, and that string has a
7358 display string on some of its
7359 characters. */
7360 text_from_string = true;
7361 pos = it->stack[it->sp - 1].position;
7363 else
7364 pos = (it->stack + stackp)->position;
7366 else
7367 INC_TEXT_POS (pos, it->multibyte_p);
7369 if (text_from_string)
7371 Lisp_Object base_string = it->stack[it->sp - 1].string;
7373 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7374 it->end_of_box_run_p = true;
7375 else
7377 next_face_id
7378 = face_at_string_position (it->w, base_string,
7379 CHARPOS (pos), 0,
7380 &ignore, face_id, false);
7381 it->end_of_box_run_p
7382 = (FACE_FROM_ID (it->f, next_face_id)->box
7383 == FACE_NO_BOX);
7386 else if (CHARPOS (pos) >= ZV)
7387 it->end_of_box_run_p = true;
7388 else
7390 next_face_id =
7391 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7392 CHARPOS (pos)
7393 + TEXT_PROP_DISTANCE_LIMIT,
7394 false, -1);
7395 it->end_of_box_run_p
7396 = (FACE_FROM_ID (it->f, next_face_id)->box
7397 == FACE_NO_BOX);
7402 /* next_element_from_display_vector sets this flag according to
7403 faces of the display vector glyphs, see there. */
7404 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7406 int face_id = face_after_it_pos (it);
7407 it->end_of_box_run_p
7408 = (face_id != it->face_id
7409 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7412 /* If we reached the end of the object we've been iterating (e.g., a
7413 display string or an overlay string), and there's something on
7414 IT->stack, proceed with what's on the stack. It doesn't make
7415 sense to return false if there's unprocessed stuff on the stack,
7416 because otherwise that stuff will never be displayed. */
7417 if (!success_p && it->sp > 0)
7419 set_iterator_to_next (it, false);
7420 success_p = get_next_display_element (it);
7423 /* Value is false if end of buffer or string reached. */
7424 return success_p;
7428 /* Move IT to the next display element.
7430 RESEAT_P means if called on a newline in buffer text,
7431 skip to the next visible line start.
7433 Functions get_next_display_element and set_iterator_to_next are
7434 separate because I find this arrangement easier to handle than a
7435 get_next_display_element function that also increments IT's
7436 position. The way it is we can first look at an iterator's current
7437 display element, decide whether it fits on a line, and if it does,
7438 increment the iterator position. The other way around we probably
7439 would either need a flag indicating whether the iterator has to be
7440 incremented the next time, or we would have to implement a
7441 decrement position function which would not be easy to write. */
7443 void
7444 set_iterator_to_next (struct it *it, bool reseat_p)
7446 /* Reset flags indicating start and end of a sequence of characters
7447 with box. Reset them at the start of this function because
7448 moving the iterator to a new position might set them. */
7449 it->start_of_box_run_p = it->end_of_box_run_p = false;
7451 switch (it->method)
7453 case GET_FROM_BUFFER:
7454 /* The current display element of IT is a character from
7455 current_buffer. Advance in the buffer, and maybe skip over
7456 invisible lines that are so because of selective display. */
7457 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7458 reseat_at_next_visible_line_start (it, false);
7459 else if (it->cmp_it.id >= 0)
7461 /* We are currently getting glyphs from a composition. */
7462 if (! it->bidi_p)
7464 IT_CHARPOS (*it) += it->cmp_it.nchars;
7465 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7467 else
7469 int i;
7471 /* Update IT's char/byte positions to point to the first
7472 character of the next grapheme cluster, or to the
7473 character visually after the current composition. */
7474 for (i = 0; i < it->cmp_it.nchars; i++)
7475 bidi_move_to_visually_next (&it->bidi_it);
7476 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7477 IT_CHARPOS (*it) = it->bidi_it.charpos;
7480 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7481 && it->cmp_it.to < it->cmp_it.nglyphs)
7483 /* Composition created while scanning forward. Proceed
7484 to the next grapheme cluster. */
7485 it->cmp_it.from = it->cmp_it.to;
7487 else if ((it->bidi_p && it->cmp_it.reversed_p)
7488 && it->cmp_it.from > 0)
7490 /* Composition created while scanning backward. Proceed
7491 to the previous grapheme cluster. */
7492 it->cmp_it.to = it->cmp_it.from;
7494 else
7496 /* No more grapheme clusters in this composition.
7497 Find the next stop position. */
7498 ptrdiff_t stop = it->end_charpos;
7500 if (it->bidi_it.scan_dir < 0)
7501 /* Now we are scanning backward and don't know
7502 where to stop. */
7503 stop = -1;
7504 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7505 IT_BYTEPOS (*it), stop, Qnil);
7508 else
7510 eassert (it->len != 0);
7512 if (!it->bidi_p)
7514 IT_BYTEPOS (*it) += it->len;
7515 IT_CHARPOS (*it) += 1;
7517 else
7519 int prev_scan_dir = it->bidi_it.scan_dir;
7520 /* If this is a new paragraph, determine its base
7521 direction (a.k.a. its base embedding level). */
7522 if (it->bidi_it.new_paragraph)
7523 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7524 false);
7525 bidi_move_to_visually_next (&it->bidi_it);
7526 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7527 IT_CHARPOS (*it) = it->bidi_it.charpos;
7528 if (prev_scan_dir != it->bidi_it.scan_dir)
7530 /* As the scan direction was changed, we must
7531 re-compute the stop position for composition. */
7532 ptrdiff_t stop = it->end_charpos;
7533 if (it->bidi_it.scan_dir < 0)
7534 stop = -1;
7535 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7536 IT_BYTEPOS (*it), stop, Qnil);
7539 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7541 break;
7543 case GET_FROM_C_STRING:
7544 /* Current display element of IT is from a C string. */
7545 if (!it->bidi_p
7546 /* If the string position is beyond string's end, it means
7547 next_element_from_c_string is padding the string with
7548 blanks, in which case we bypass the bidi iterator,
7549 because it cannot deal with such virtual characters. */
7550 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7552 IT_BYTEPOS (*it) += it->len;
7553 IT_CHARPOS (*it) += 1;
7555 else
7557 bidi_move_to_visually_next (&it->bidi_it);
7558 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7559 IT_CHARPOS (*it) = it->bidi_it.charpos;
7561 break;
7563 case GET_FROM_DISPLAY_VECTOR:
7564 /* Current display element of IT is from a display table entry.
7565 Advance in the display table definition. Reset it to null if
7566 end reached, and continue with characters from buffers/
7567 strings. */
7568 ++it->current.dpvec_index;
7570 /* Restore face of the iterator to what they were before the
7571 display vector entry (these entries may contain faces). */
7572 it->face_id = it->saved_face_id;
7574 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7576 bool recheck_faces = it->ellipsis_p;
7578 if (it->s)
7579 it->method = GET_FROM_C_STRING;
7580 else if (STRINGP (it->string))
7581 it->method = GET_FROM_STRING;
7582 else
7584 it->method = GET_FROM_BUFFER;
7585 it->object = it->w->contents;
7588 it->dpvec = NULL;
7589 it->current.dpvec_index = -1;
7591 /* Skip over characters which were displayed via IT->dpvec. */
7592 if (it->dpvec_char_len < 0)
7593 reseat_at_next_visible_line_start (it, true);
7594 else if (it->dpvec_char_len > 0)
7596 it->len = it->dpvec_char_len;
7597 set_iterator_to_next (it, reseat_p);
7600 /* Maybe recheck faces after display vector. */
7601 if (recheck_faces)
7603 if (it->method == GET_FROM_STRING)
7604 it->stop_charpos = IT_STRING_CHARPOS (*it);
7605 else
7606 it->stop_charpos = IT_CHARPOS (*it);
7609 break;
7611 case GET_FROM_STRING:
7612 /* Current display element is a character from a Lisp string. */
7613 eassert (it->s == NULL && STRINGP (it->string));
7614 /* Don't advance past string end. These conditions are true
7615 when set_iterator_to_next is called at the end of
7616 get_next_display_element, in which case the Lisp string is
7617 already exhausted, and all we want is pop the iterator
7618 stack. */
7619 if (it->current.overlay_string_index >= 0)
7621 /* This is an overlay string, so there's no padding with
7622 spaces, and the number of characters in the string is
7623 where the string ends. */
7624 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7625 goto consider_string_end;
7627 else
7629 /* Not an overlay string. There could be padding, so test
7630 against it->end_charpos. */
7631 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7632 goto consider_string_end;
7634 if (it->cmp_it.id >= 0)
7636 /* We are delivering display elements from a composition.
7637 Update the string position past the grapheme cluster
7638 we've just processed. */
7639 if (! it->bidi_p)
7641 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7642 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7644 else
7646 int i;
7648 for (i = 0; i < it->cmp_it.nchars; i++)
7649 bidi_move_to_visually_next (&it->bidi_it);
7650 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7651 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7654 /* Did we exhaust all the grapheme clusters of this
7655 composition? */
7656 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7657 && (it->cmp_it.to < it->cmp_it.nglyphs))
7659 /* Not all the grapheme clusters were processed yet;
7660 advance to the next cluster. */
7661 it->cmp_it.from = it->cmp_it.to;
7663 else if ((it->bidi_p && it->cmp_it.reversed_p)
7664 && it->cmp_it.from > 0)
7666 /* Likewise: advance to the next cluster, but going in
7667 the reverse direction. */
7668 it->cmp_it.to = it->cmp_it.from;
7670 else
7672 /* This composition was fully processed; find the next
7673 candidate place for checking for composed
7674 characters. */
7675 /* Always limit string searches to the string length;
7676 any padding spaces are not part of the string, and
7677 there cannot be any compositions in that padding. */
7678 ptrdiff_t stop = SCHARS (it->string);
7680 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7681 stop = -1;
7682 else if (it->end_charpos < stop)
7684 /* Cf. PRECISION in reseat_to_string: we might be
7685 limited in how many of the string characters we
7686 need to deliver. */
7687 stop = it->end_charpos;
7689 composition_compute_stop_pos (&it->cmp_it,
7690 IT_STRING_CHARPOS (*it),
7691 IT_STRING_BYTEPOS (*it), stop,
7692 it->string);
7695 else
7697 if (!it->bidi_p
7698 /* If the string position is beyond string's end, it
7699 means next_element_from_string is padding the string
7700 with blanks, in which case we bypass the bidi
7701 iterator, because it cannot deal with such virtual
7702 characters. */
7703 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7705 IT_STRING_BYTEPOS (*it) += it->len;
7706 IT_STRING_CHARPOS (*it) += 1;
7708 else
7710 int prev_scan_dir = it->bidi_it.scan_dir;
7712 bidi_move_to_visually_next (&it->bidi_it);
7713 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7714 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7715 /* If the scan direction changes, we may need to update
7716 the place where to check for composed characters. */
7717 if (prev_scan_dir != it->bidi_it.scan_dir)
7719 ptrdiff_t stop = SCHARS (it->string);
7721 if (it->bidi_it.scan_dir < 0)
7722 stop = -1;
7723 else if (it->end_charpos < stop)
7724 stop = it->end_charpos;
7726 composition_compute_stop_pos (&it->cmp_it,
7727 IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it), stop,
7729 it->string);
7734 consider_string_end:
7736 if (it->current.overlay_string_index >= 0)
7738 /* IT->string is an overlay string. Advance to the
7739 next, if there is one. */
7740 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7742 it->ellipsis_p = false;
7743 next_overlay_string (it);
7744 if (it->ellipsis_p)
7745 setup_for_ellipsis (it, 0);
7748 else
7750 /* IT->string is not an overlay string. If we reached
7751 its end, and there is something on IT->stack, proceed
7752 with what is on the stack. This can be either another
7753 string, this time an overlay string, or a buffer. */
7754 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7755 && it->sp > 0)
7757 pop_it (it);
7758 if (it->method == GET_FROM_STRING)
7759 goto consider_string_end;
7762 break;
7764 case GET_FROM_IMAGE:
7765 case GET_FROM_STRETCH:
7766 case GET_FROM_XWIDGET:
7768 /* The position etc with which we have to proceed are on
7769 the stack. The position may be at the end of a string,
7770 if the `display' property takes up the whole string. */
7771 eassert (it->sp > 0);
7772 pop_it (it);
7773 if (it->method == GET_FROM_STRING)
7774 goto consider_string_end;
7775 break;
7777 default:
7778 /* There are no other methods defined, so this should be a bug. */
7779 emacs_abort ();
7782 eassert (it->method != GET_FROM_STRING
7783 || (STRINGP (it->string)
7784 && IT_STRING_CHARPOS (*it) >= 0));
7787 /* Load IT's display element fields with information about the next
7788 display element which comes from a display table entry or from the
7789 result of translating a control character to one of the forms `^C'
7790 or `\003'.
7792 IT->dpvec holds the glyphs to return as characters.
7793 IT->saved_face_id holds the face id before the display vector--it
7794 is restored into IT->face_id in set_iterator_to_next. */
7796 static bool
7797 next_element_from_display_vector (struct it *it)
7799 Lisp_Object gc;
7800 int prev_face_id = it->face_id;
7801 int next_face_id;
7803 /* Precondition. */
7804 eassert (it->dpvec && it->current.dpvec_index >= 0);
7806 it->face_id = it->saved_face_id;
7808 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7809 That seemed totally bogus - so I changed it... */
7810 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7811 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7813 struct face *this_face, *prev_face, *next_face;
7815 it->c = GLYPH_CODE_CHAR (gc);
7816 it->len = CHAR_BYTES (it->c);
7818 /* The entry may contain a face id to use. Such a face id is
7819 the id of a Lisp face, not a realized face. A face id of
7820 zero means no face is specified. */
7821 if (it->dpvec_face_id >= 0)
7822 it->face_id = it->dpvec_face_id;
7823 else
7825 int lface_id = GLYPH_CODE_FACE (gc);
7826 if (lface_id > 0)
7827 it->face_id = merge_faces (it->f, Qt, lface_id,
7828 it->saved_face_id);
7831 /* Glyphs in the display vector could have the box face, so we
7832 need to set the related flags in the iterator, as
7833 appropriate. */
7834 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7835 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7837 /* Is this character the first character of a box-face run? */
7838 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7839 && (!prev_face
7840 || prev_face->box == FACE_NO_BOX));
7842 /* For the last character of the box-face run, we need to look
7843 either at the next glyph from the display vector, or at the
7844 face we saw before the display vector. */
7845 next_face_id = it->saved_face_id;
7846 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7848 if (it->dpvec_face_id >= 0)
7849 next_face_id = it->dpvec_face_id;
7850 else
7852 int lface_id =
7853 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7855 if (lface_id > 0)
7856 next_face_id = merge_faces (it->f, Qt, lface_id,
7857 it->saved_face_id);
7860 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7861 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7862 && (!next_face
7863 || next_face->box == FACE_NO_BOX));
7864 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7866 else
7867 /* Display table entry is invalid. Return a space. */
7868 it->c = ' ', it->len = 1;
7870 /* Don't change position and object of the iterator here. They are
7871 still the values of the character that had this display table
7872 entry or was translated, and that's what we want. */
7873 it->what = IT_CHARACTER;
7874 return true;
7877 /* Get the first element of string/buffer in the visual order, after
7878 being reseated to a new position in a string or a buffer. */
7879 static void
7880 get_visually_first_element (struct it *it)
7882 bool string_p = STRINGP (it->string) || it->s;
7883 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7884 ptrdiff_t bob = (string_p ? 0 : BEGV);
7886 if (STRINGP (it->string))
7888 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7889 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7891 else
7893 it->bidi_it.charpos = IT_CHARPOS (*it);
7894 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7897 if (it->bidi_it.charpos == eob)
7899 /* Nothing to do, but reset the FIRST_ELT flag, like
7900 bidi_paragraph_init does, because we are not going to
7901 call it. */
7902 it->bidi_it.first_elt = false;
7904 else if (it->bidi_it.charpos == bob
7905 || (!string_p
7906 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7907 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7909 /* If we are at the beginning of a line/string, we can produce
7910 the next element right away. */
7911 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7912 bidi_move_to_visually_next (&it->bidi_it);
7914 else
7916 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7918 /* We need to prime the bidi iterator starting at the line's or
7919 string's beginning, before we will be able to produce the
7920 next element. */
7921 if (string_p)
7922 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7923 else
7924 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7925 IT_BYTEPOS (*it), -1,
7926 &it->bidi_it.bytepos);
7927 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7930 /* Now return to buffer/string position where we were asked
7931 to get the next display element, and produce that. */
7932 bidi_move_to_visually_next (&it->bidi_it);
7934 while (it->bidi_it.bytepos != orig_bytepos
7935 && it->bidi_it.charpos < eob);
7938 /* Adjust IT's position information to where we ended up. */
7939 if (STRINGP (it->string))
7941 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7942 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7944 else
7946 IT_CHARPOS (*it) = it->bidi_it.charpos;
7947 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7950 if (STRINGP (it->string) || !it->s)
7952 ptrdiff_t stop, charpos, bytepos;
7954 if (STRINGP (it->string))
7956 eassert (!it->s);
7957 stop = SCHARS (it->string);
7958 if (stop > it->end_charpos)
7959 stop = it->end_charpos;
7960 charpos = IT_STRING_CHARPOS (*it);
7961 bytepos = IT_STRING_BYTEPOS (*it);
7963 else
7965 stop = it->end_charpos;
7966 charpos = IT_CHARPOS (*it);
7967 bytepos = IT_BYTEPOS (*it);
7969 if (it->bidi_it.scan_dir < 0)
7970 stop = -1;
7971 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7972 it->string);
7976 /* Load IT with the next display element from Lisp string IT->string.
7977 IT->current.string_pos is the current position within the string.
7978 If IT->current.overlay_string_index >= 0, the Lisp string is an
7979 overlay string. */
7981 static bool
7982 next_element_from_string (struct it *it)
7984 struct text_pos position;
7986 eassert (STRINGP (it->string));
7987 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7988 eassert (IT_STRING_CHARPOS (*it) >= 0);
7989 position = it->current.string_pos;
7991 /* With bidi reordering, the character to display might not be the
7992 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7993 that we were reseat()ed to a new string, whose paragraph
7994 direction is not known. */
7995 if (it->bidi_p && it->bidi_it.first_elt)
7997 get_visually_first_element (it);
7998 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
8001 /* Time to check for invisible text? */
8002 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
8004 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
8006 if (!(!it->bidi_p
8007 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8008 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
8010 /* With bidi non-linear iteration, we could find
8011 ourselves far beyond the last computed stop_charpos,
8012 with several other stop positions in between that we
8013 missed. Scan them all now, in buffer's logical
8014 order, until we find and handle the last stop_charpos
8015 that precedes our current position. */
8016 handle_stop_backwards (it, it->stop_charpos);
8017 return GET_NEXT_DISPLAY_ELEMENT (it);
8019 else
8021 if (it->bidi_p)
8023 /* Take note of the stop position we just moved
8024 across, for when we will move back across it. */
8025 it->prev_stop = it->stop_charpos;
8026 /* If we are at base paragraph embedding level, take
8027 note of the last stop position seen at this
8028 level. */
8029 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8030 it->base_level_stop = it->stop_charpos;
8032 handle_stop (it);
8034 /* Since a handler may have changed IT->method, we must
8035 recurse here. */
8036 return GET_NEXT_DISPLAY_ELEMENT (it);
8039 else if (it->bidi_p
8040 /* If we are before prev_stop, we may have overstepped
8041 on our way backwards a stop_pos, and if so, we need
8042 to handle that stop_pos. */
8043 && IT_STRING_CHARPOS (*it) < it->prev_stop
8044 /* We can sometimes back up for reasons that have nothing
8045 to do with bidi reordering. E.g., compositions. The
8046 code below is only needed when we are above the base
8047 embedding level, so test for that explicitly. */
8048 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 /* If we lost track of base_level_stop, we have no better
8051 place for handle_stop_backwards to start from than string
8052 beginning. This happens, e.g., when we were reseated to
8053 the previous screenful of text by vertical-motion. */
8054 if (it->base_level_stop <= 0
8055 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8056 it->base_level_stop = 0;
8057 handle_stop_backwards (it, it->base_level_stop);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 if (it->current.overlay_string_index >= 0)
8064 /* Get the next character from an overlay string. In overlay
8065 strings, there is no field width or padding with spaces to
8066 do. */
8067 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8069 it->what = IT_EOB;
8070 return false;
8072 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8073 IT_STRING_BYTEPOS (*it),
8074 it->bidi_it.scan_dir < 0
8075 ? -1
8076 : SCHARS (it->string))
8077 && next_element_from_composition (it))
8079 return true;
8081 else if (STRING_MULTIBYTE (it->string))
8083 const unsigned char *s = (SDATA (it->string)
8084 + IT_STRING_BYTEPOS (*it));
8085 it->c = string_char_and_length (s, &it->len);
8087 else
8089 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8090 it->len = 1;
8093 else
8095 /* Get the next character from a Lisp string that is not an
8096 overlay string. Such strings come from the mode line, for
8097 example. We may have to pad with spaces, or truncate the
8098 string. See also next_element_from_c_string. */
8099 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8101 it->what = IT_EOB;
8102 return false;
8104 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8106 /* Pad with spaces. */
8107 it->c = ' ', it->len = 1;
8108 CHARPOS (position) = BYTEPOS (position) = -1;
8110 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8111 IT_STRING_BYTEPOS (*it),
8112 it->bidi_it.scan_dir < 0
8113 ? -1
8114 : it->string_nchars)
8115 && next_element_from_composition (it))
8117 return true;
8119 else if (STRING_MULTIBYTE (it->string))
8121 const unsigned char *s = (SDATA (it->string)
8122 + IT_STRING_BYTEPOS (*it));
8123 it->c = string_char_and_length (s, &it->len);
8125 else
8127 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8128 it->len = 1;
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->string;
8135 it->position = position;
8136 return true;
8140 /* Load IT with next display element from C string IT->s.
8141 IT->string_nchars is the maximum number of characters to return
8142 from the string. IT->end_charpos may be greater than
8143 IT->string_nchars when this function is called, in which case we
8144 may have to return padding spaces. Value is false if end of string
8145 reached, including padding spaces. */
8147 static bool
8148 next_element_from_c_string (struct it *it)
8150 bool success_p = true;
8152 eassert (it->s);
8153 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8154 it->what = IT_CHARACTER;
8155 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8156 it->object = make_number (0);
8158 /* With bidi reordering, the character to display might not be the
8159 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8160 we were reseated to a new string, whose paragraph direction is
8161 not known. */
8162 if (it->bidi_p && it->bidi_it.first_elt)
8163 get_visually_first_element (it);
8165 /* IT's position can be greater than IT->string_nchars in case a
8166 field width or precision has been specified when the iterator was
8167 initialized. */
8168 if (IT_CHARPOS (*it) >= it->end_charpos)
8170 /* End of the game. */
8171 it->what = IT_EOB;
8172 success_p = false;
8174 else if (IT_CHARPOS (*it) >= it->string_nchars)
8176 /* Pad with spaces. */
8177 it->c = ' ', it->len = 1;
8178 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8180 else if (it->multibyte_p)
8181 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8182 else
8183 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8185 return success_p;
8189 /* Set up IT to return characters from an ellipsis, if appropriate.
8190 The definition of the ellipsis glyphs may come from a display table
8191 entry. This function fills IT with the first glyph from the
8192 ellipsis if an ellipsis is to be displayed. */
8194 static bool
8195 next_element_from_ellipsis (struct it *it)
8197 if (it->selective_display_ellipsis_p)
8198 setup_for_ellipsis (it, it->len);
8199 else
8201 /* The face at the current position may be different from the
8202 face we find after the invisible text. Remember what it
8203 was in IT->saved_face_id, and signal that it's there by
8204 setting face_before_selective_p. */
8205 it->saved_face_id = it->face_id;
8206 it->method = GET_FROM_BUFFER;
8207 it->object = it->w->contents;
8208 reseat_at_next_visible_line_start (it, true);
8209 it->face_before_selective_p = true;
8212 return GET_NEXT_DISPLAY_ELEMENT (it);
8216 /* Deliver an image display element. The iterator IT is already
8217 filled with image information (done in handle_display_prop). Value
8218 is always true. */
8221 static bool
8222 next_element_from_image (struct it *it)
8224 it->what = IT_IMAGE;
8225 return true;
8228 static bool
8229 next_element_from_xwidget (struct it *it)
8231 it->what = IT_XWIDGET;
8232 return true;
8236 /* Fill iterator IT with next display element from a stretch glyph
8237 property. IT->object is the value of the text property. Value is
8238 always true. */
8240 static bool
8241 next_element_from_stretch (struct it *it)
8243 it->what = IT_STRETCH;
8244 return true;
8247 /* Scan backwards from IT's current position until we find a stop
8248 position, or until BEGV. This is called when we find ourself
8249 before both the last known prev_stop and base_level_stop while
8250 reordering bidirectional text. */
8252 static void
8253 compute_stop_pos_backwards (struct it *it)
8255 const int SCAN_BACK_LIMIT = 1000;
8256 struct text_pos pos;
8257 struct display_pos save_current = it->current;
8258 struct text_pos save_position = it->position;
8259 ptrdiff_t charpos = IT_CHARPOS (*it);
8260 ptrdiff_t where_we_are = charpos;
8261 ptrdiff_t save_stop_pos = it->stop_charpos;
8262 ptrdiff_t save_end_pos = it->end_charpos;
8264 eassert (NILP (it->string) && !it->s);
8265 eassert (it->bidi_p);
8266 it->bidi_p = false;
8269 it->end_charpos = min (charpos + 1, ZV);
8270 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8271 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8272 reseat_1 (it, pos, false);
8273 compute_stop_pos (it);
8274 /* We must advance forward, right? */
8275 if (it->stop_charpos <= charpos)
8276 emacs_abort ();
8278 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8280 if (it->stop_charpos <= where_we_are)
8281 it->prev_stop = it->stop_charpos;
8282 else
8283 it->prev_stop = BEGV;
8284 it->bidi_p = true;
8285 it->current = save_current;
8286 it->position = save_position;
8287 it->stop_charpos = save_stop_pos;
8288 it->end_charpos = save_end_pos;
8291 /* Scan forward from CHARPOS in the current buffer/string, until we
8292 find a stop position > current IT's position. Then handle the stop
8293 position before that. This is called when we bump into a stop
8294 position while reordering bidirectional text. CHARPOS should be
8295 the last previously processed stop_pos (or BEGV/0, if none were
8296 processed yet) whose position is less that IT's current
8297 position. */
8299 static void
8300 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8302 bool bufp = !STRINGP (it->string);
8303 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8304 struct display_pos save_current = it->current;
8305 struct text_pos save_position = it->position;
8306 struct text_pos pos1;
8307 ptrdiff_t next_stop;
8309 /* Scan in strict logical order. */
8310 eassert (it->bidi_p);
8311 it->bidi_p = false;
8314 it->prev_stop = charpos;
8315 if (bufp)
8317 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8318 reseat_1 (it, pos1, false);
8320 else
8321 it->current.string_pos = string_pos (charpos, it->string);
8322 compute_stop_pos (it);
8323 /* We must advance forward, right? */
8324 if (it->stop_charpos <= it->prev_stop)
8325 emacs_abort ();
8326 charpos = it->stop_charpos;
8328 while (charpos <= where_we_are);
8330 it->bidi_p = true;
8331 it->current = save_current;
8332 it->position = save_position;
8333 next_stop = it->stop_charpos;
8334 it->stop_charpos = it->prev_stop;
8335 handle_stop (it);
8336 it->stop_charpos = next_stop;
8339 /* Load IT with the next display element from current_buffer. Value
8340 is false if end of buffer reached. IT->stop_charpos is the next
8341 position at which to stop and check for text properties or buffer
8342 end. */
8344 static bool
8345 next_element_from_buffer (struct it *it)
8347 bool success_p = true;
8349 eassert (IT_CHARPOS (*it) >= BEGV);
8350 eassert (NILP (it->string) && !it->s);
8351 eassert (!it->bidi_p
8352 || (EQ (it->bidi_it.string.lstring, Qnil)
8353 && it->bidi_it.string.s == NULL));
8355 /* With bidi reordering, the character to display might not be the
8356 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8357 we were reseat()ed to a new buffer position, which is potentially
8358 a different paragraph. */
8359 if (it->bidi_p && it->bidi_it.first_elt)
8361 get_visually_first_element (it);
8362 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8365 if (IT_CHARPOS (*it) >= it->stop_charpos)
8367 if (IT_CHARPOS (*it) >= it->end_charpos)
8369 bool overlay_strings_follow_p;
8371 /* End of the game, except when overlay strings follow that
8372 haven't been returned yet. */
8373 if (it->overlay_strings_at_end_processed_p)
8374 overlay_strings_follow_p = false;
8375 else
8377 it->overlay_strings_at_end_processed_p = true;
8378 overlay_strings_follow_p = get_overlay_strings (it, 0);
8381 if (overlay_strings_follow_p)
8382 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8383 else
8385 it->what = IT_EOB;
8386 it->position = it->current.pos;
8387 success_p = false;
8390 else if (!(!it->bidi_p
8391 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8392 || IT_CHARPOS (*it) == it->stop_charpos))
8394 /* With bidi non-linear iteration, we could find ourselves
8395 far beyond the last computed stop_charpos, with several
8396 other stop positions in between that we missed. Scan
8397 them all now, in buffer's logical order, until we find
8398 and handle the last stop_charpos that precedes our
8399 current position. */
8400 handle_stop_backwards (it, it->stop_charpos);
8401 it->ignore_overlay_strings_at_pos_p = false;
8402 return GET_NEXT_DISPLAY_ELEMENT (it);
8404 else
8406 if (it->bidi_p)
8408 /* Take note of the stop position we just moved across,
8409 for when we will move back across it. */
8410 it->prev_stop = it->stop_charpos;
8411 /* If we are at base paragraph embedding level, take
8412 note of the last stop position seen at this
8413 level. */
8414 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8415 it->base_level_stop = it->stop_charpos;
8417 handle_stop (it);
8418 it->ignore_overlay_strings_at_pos_p = false;
8419 return GET_NEXT_DISPLAY_ELEMENT (it);
8422 else if (it->bidi_p
8423 /* If we are before prev_stop, we may have overstepped on
8424 our way backwards a stop_pos, and if so, we need to
8425 handle that stop_pos. */
8426 && IT_CHARPOS (*it) < it->prev_stop
8427 /* We can sometimes back up for reasons that have nothing
8428 to do with bidi reordering. E.g., compositions. The
8429 code below is only needed when we are above the base
8430 embedding level, so test for that explicitly. */
8431 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8433 if (it->base_level_stop <= 0
8434 || IT_CHARPOS (*it) < it->base_level_stop)
8436 /* If we lost track of base_level_stop, we need to find
8437 prev_stop by looking backwards. This happens, e.g., when
8438 we were reseated to the previous screenful of text by
8439 vertical-motion. */
8440 it->base_level_stop = BEGV;
8441 compute_stop_pos_backwards (it);
8442 handle_stop_backwards (it, it->prev_stop);
8444 else
8445 handle_stop_backwards (it, it->base_level_stop);
8446 it->ignore_overlay_strings_at_pos_p = false;
8447 return GET_NEXT_DISPLAY_ELEMENT (it);
8449 else
8451 /* No face changes, overlays etc. in sight, so just return a
8452 character from current_buffer. */
8453 unsigned char *p;
8454 ptrdiff_t stop;
8456 /* We moved to the next buffer position, so any info about
8457 previously seen overlays is no longer valid. */
8458 it->ignore_overlay_strings_at_pos_p = false;
8460 /* Maybe run the redisplay end trigger hook. Performance note:
8461 This doesn't seem to cost measurable time. */
8462 if (it->redisplay_end_trigger_charpos
8463 && it->glyph_row
8464 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8465 run_redisplay_end_trigger_hook (it);
8467 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8468 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8469 stop)
8470 && next_element_from_composition (it))
8472 return true;
8475 /* Get the next character, maybe multibyte. */
8476 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8477 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8478 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8479 else
8480 it->c = *p, it->len = 1;
8482 /* Record what we have and where it came from. */
8483 it->what = IT_CHARACTER;
8484 it->object = it->w->contents;
8485 it->position = it->current.pos;
8487 /* Normally we return the character found above, except when we
8488 really want to return an ellipsis for selective display. */
8489 if (it->selective)
8491 if (it->c == '\n')
8493 /* A value of selective > 0 means hide lines indented more
8494 than that number of columns. */
8495 if (it->selective > 0
8496 && IT_CHARPOS (*it) + 1 < ZV
8497 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8498 IT_BYTEPOS (*it) + 1,
8499 it->selective))
8501 success_p = next_element_from_ellipsis (it);
8502 it->dpvec_char_len = -1;
8505 else if (it->c == '\r' && it->selective == -1)
8507 /* A value of selective == -1 means that everything from the
8508 CR to the end of the line is invisible, with maybe an
8509 ellipsis displayed for it. */
8510 success_p = next_element_from_ellipsis (it);
8511 it->dpvec_char_len = -1;
8516 /* Value is false if end of buffer reached. */
8517 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8518 return success_p;
8522 /* Run the redisplay end trigger hook for IT. */
8524 static void
8525 run_redisplay_end_trigger_hook (struct it *it)
8527 /* IT->glyph_row should be non-null, i.e. we should be actually
8528 displaying something, or otherwise we should not run the hook. */
8529 eassert (it->glyph_row);
8531 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8532 it->redisplay_end_trigger_charpos = 0;
8534 /* Since we are *trying* to run these functions, don't try to run
8535 them again, even if they get an error. */
8536 wset_redisplay_end_trigger (it->w, Qnil);
8537 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8538 make_number (charpos));
8540 /* Notice if it changed the face of the character we are on. */
8541 handle_face_prop (it);
8545 /* Deliver a composition display element. Unlike the other
8546 next_element_from_XXX, this function is not registered in the array
8547 get_next_element[]. It is called from next_element_from_buffer and
8548 next_element_from_string when necessary. */
8550 static bool
8551 next_element_from_composition (struct it *it)
8553 it->what = IT_COMPOSITION;
8554 it->len = it->cmp_it.nbytes;
8555 if (STRINGP (it->string))
8557 if (it->c < 0)
8559 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8560 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8561 return false;
8563 it->position = it->current.string_pos;
8564 it->object = it->string;
8565 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8566 IT_STRING_BYTEPOS (*it), it->string);
8568 else
8570 if (it->c < 0)
8572 IT_CHARPOS (*it) += it->cmp_it.nchars;
8573 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8574 if (it->bidi_p)
8576 if (it->bidi_it.new_paragraph)
8577 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8578 false);
8579 /* Resync the bidi iterator with IT's new position.
8580 FIXME: this doesn't support bidirectional text. */
8581 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8582 bidi_move_to_visually_next (&it->bidi_it);
8584 return false;
8586 it->position = it->current.pos;
8587 it->object = it->w->contents;
8588 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8589 IT_BYTEPOS (*it), Qnil);
8591 return true;
8596 /***********************************************************************
8597 Moving an iterator without producing glyphs
8598 ***********************************************************************/
8600 /* Check if iterator is at a position corresponding to a valid buffer
8601 position after some move_it_ call. */
8603 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8604 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8607 /* Move iterator IT to a specified buffer or X position within one
8608 line on the display without producing glyphs.
8610 OP should be a bit mask including some or all of these bits:
8611 MOVE_TO_X: Stop upon reaching x-position TO_X.
8612 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8613 Regardless of OP's value, stop upon reaching the end of the display line.
8615 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8616 This means, in particular, that TO_X includes window's horizontal
8617 scroll amount.
8619 The return value has several possible values that
8620 say what condition caused the scan to stop:
8622 MOVE_POS_MATCH_OR_ZV
8623 - when TO_POS or ZV was reached.
8625 MOVE_X_REACHED
8626 -when TO_X was reached before TO_POS or ZV were reached.
8628 MOVE_LINE_CONTINUED
8629 - when we reached the end of the display area and the line must
8630 be continued.
8632 MOVE_LINE_TRUNCATED
8633 - when we reached the end of the display area and the line is
8634 truncated.
8636 MOVE_NEWLINE_OR_CR
8637 - when we stopped at a line end, i.e. a newline or a CR and selective
8638 display is on. */
8640 static enum move_it_result
8641 move_it_in_display_line_to (struct it *it,
8642 ptrdiff_t to_charpos, int to_x,
8643 enum move_operation_enum op)
8645 enum move_it_result result = MOVE_UNDEFINED;
8646 struct glyph_row *saved_glyph_row;
8647 struct it wrap_it, atpos_it, atx_it, ppos_it;
8648 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8649 void *ppos_data = NULL;
8650 bool may_wrap = false;
8651 enum it_method prev_method = it->method;
8652 ptrdiff_t closest_pos UNINIT;
8653 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8654 bool saw_smaller_pos = prev_pos < to_charpos;
8655 bool line_number_pending = false;
8657 /* Don't produce glyphs in produce_glyphs. */
8658 saved_glyph_row = it->glyph_row;
8659 it->glyph_row = NULL;
8661 /* Use wrap_it to save a copy of IT wherever a word wrap could
8662 occur. Use atpos_it to save a copy of IT at the desired buffer
8663 position, if found, so that we can scan ahead and check if the
8664 word later overshoots the window edge. Use atx_it similarly, for
8665 pixel positions. */
8666 wrap_it.sp = -1;
8667 atpos_it.sp = -1;
8668 atx_it.sp = -1;
8670 /* Use ppos_it under bidi reordering to save a copy of IT for the
8671 initial position. We restore that position in IT when we have
8672 scanned the entire display line without finding a match for
8673 TO_CHARPOS and all the character positions are greater than
8674 TO_CHARPOS. We then restart the scan from the initial position,
8675 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8676 the closest to TO_CHARPOS. */
8677 if (it->bidi_p)
8679 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8681 SAVE_IT (ppos_it, *it, ppos_data);
8682 closest_pos = IT_CHARPOS (*it);
8684 else
8685 closest_pos = ZV;
8688 #define BUFFER_POS_REACHED_P() \
8689 ((op & MOVE_TO_POS) != 0 \
8690 && BUFFERP (it->object) \
8691 && (IT_CHARPOS (*it) == to_charpos \
8692 || ((!it->bidi_p \
8693 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8694 && IT_CHARPOS (*it) > to_charpos) \
8695 || (it->what == IT_COMPOSITION \
8696 && ((IT_CHARPOS (*it) > to_charpos \
8697 && to_charpos >= it->cmp_it.charpos) \
8698 || (IT_CHARPOS (*it) < to_charpos \
8699 && to_charpos <= it->cmp_it.charpos)))) \
8700 && (it->method == GET_FROM_BUFFER \
8701 || (it->method == GET_FROM_DISPLAY_VECTOR \
8702 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8704 if (it->hpos == 0)
8706 /* If line numbers are being displayed, produce a line number. */
8707 if (should_produce_line_number (it))
8709 if (it->current_x == it->first_visible_x)
8710 maybe_produce_line_number (it);
8711 else
8712 line_number_pending = true;
8714 /* If there's a line-/wrap-prefix, handle it. */
8715 if (it->method == GET_FROM_BUFFER)
8716 handle_line_prefix (it);
8719 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8720 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8722 while (true)
8724 int x, i, ascent = 0, descent = 0;
8726 /* Utility macro to reset an iterator with x, ascent, and descent. */
8727 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8728 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8729 (IT)->max_descent = descent)
8731 /* Stop if we move beyond TO_CHARPOS (after an image or a
8732 display string or stretch glyph). */
8733 if ((op & MOVE_TO_POS) != 0
8734 && BUFFERP (it->object)
8735 && it->method == GET_FROM_BUFFER
8736 && (((!it->bidi_p
8737 /* When the iterator is at base embedding level, we
8738 are guaranteed that characters are delivered for
8739 display in strictly increasing order of their
8740 buffer positions. */
8741 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8742 && IT_CHARPOS (*it) > to_charpos)
8743 || (it->bidi_p
8744 && (prev_method == GET_FROM_IMAGE
8745 || prev_method == GET_FROM_STRETCH
8746 || prev_method == GET_FROM_STRING)
8747 /* Passed TO_CHARPOS from left to right. */
8748 && ((prev_pos < to_charpos
8749 && IT_CHARPOS (*it) > to_charpos)
8750 /* Passed TO_CHARPOS from right to left. */
8751 || (prev_pos > to_charpos
8752 && IT_CHARPOS (*it) < to_charpos)))))
8754 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8756 result = MOVE_POS_MATCH_OR_ZV;
8757 break;
8759 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8760 /* If wrap_it is valid, the current position might be in a
8761 word that is wrapped. So, save the iterator in
8762 atpos_it and continue to see if wrapping happens. */
8763 SAVE_IT (atpos_it, *it, atpos_data);
8766 /* Stop when ZV reached.
8767 We used to stop here when TO_CHARPOS reached as well, but that is
8768 too soon if this glyph does not fit on this line. So we handle it
8769 explicitly below. */
8770 if (!get_next_display_element (it))
8772 result = MOVE_POS_MATCH_OR_ZV;
8773 break;
8776 if (it->line_wrap == TRUNCATE)
8778 if (BUFFER_POS_REACHED_P ())
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8784 else
8786 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8788 if (IT_DISPLAYING_WHITESPACE (it))
8789 may_wrap = true;
8790 else if (may_wrap)
8792 /* We have reached a glyph that follows one or more
8793 whitespace characters. If the position is
8794 already found, we are done. */
8795 if (atpos_it.sp >= 0)
8797 RESTORE_IT (it, &atpos_it, atpos_data);
8798 result = MOVE_POS_MATCH_OR_ZV;
8799 goto done;
8801 if (atx_it.sp >= 0)
8803 RESTORE_IT (it, &atx_it, atx_data);
8804 result = MOVE_X_REACHED;
8805 goto done;
8807 /* Otherwise, we can wrap here. */
8808 SAVE_IT (wrap_it, *it, wrap_data);
8809 may_wrap = false;
8814 /* Remember the line height for the current line, in case
8815 the next element doesn't fit on the line. */
8816 ascent = it->max_ascent;
8817 descent = it->max_descent;
8819 /* The call to produce_glyphs will get the metrics of the
8820 display element IT is loaded with. Record the x-position
8821 before this display element, in case it doesn't fit on the
8822 line. */
8823 x = it->current_x;
8825 PRODUCE_GLYPHS (it);
8827 if (it->area != TEXT_AREA)
8829 prev_method = it->method;
8830 if (it->method == GET_FROM_BUFFER)
8831 prev_pos = IT_CHARPOS (*it);
8832 set_iterator_to_next (it, true);
8833 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8834 SET_TEXT_POS (this_line_min_pos,
8835 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8836 if (it->bidi_p
8837 && (op & MOVE_TO_POS)
8838 && IT_CHARPOS (*it) > to_charpos
8839 && IT_CHARPOS (*it) < closest_pos)
8840 closest_pos = IT_CHARPOS (*it);
8841 continue;
8844 /* The number of glyphs we get back in IT->nglyphs will normally
8845 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8846 character on a terminal frame, or (iii) a line end. For the
8847 second case, IT->nglyphs - 1 padding glyphs will be present.
8848 (On X frames, there is only one glyph produced for a
8849 composite character.)
8851 The behavior implemented below means, for continuation lines,
8852 that as many spaces of a TAB as fit on the current line are
8853 displayed there. For terminal frames, as many glyphs of a
8854 multi-glyph character are displayed in the current line, too.
8855 This is what the old redisplay code did, and we keep it that
8856 way. Under X, the whole shape of a complex character must
8857 fit on the line or it will be completely displayed in the
8858 next line.
8860 Note that both for tabs and padding glyphs, all glyphs have
8861 the same width. */
8862 if (it->nglyphs)
8864 /* More than one glyph or glyph doesn't fit on line. All
8865 glyphs have the same width. */
8866 int single_glyph_width = it->pixel_width / it->nglyphs;
8867 int new_x;
8868 int x_before_this_char = x;
8869 int hpos_before_this_char = it->hpos;
8871 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8873 new_x = x + single_glyph_width;
8875 /* We want to leave anything reaching TO_X to the caller. */
8876 if ((op & MOVE_TO_X) && new_x > to_x)
8878 if (BUFFER_POS_REACHED_P ())
8880 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8881 goto buffer_pos_reached;
8882 if (atpos_it.sp < 0)
8884 SAVE_IT (atpos_it, *it, atpos_data);
8885 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8888 else
8890 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8892 it->current_x = x;
8893 result = MOVE_X_REACHED;
8894 break;
8896 if (atx_it.sp < 0)
8898 SAVE_IT (atx_it, *it, atx_data);
8899 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8904 if (/* Lines are continued. */
8905 it->line_wrap != TRUNCATE
8906 && (/* And glyph doesn't fit on the line. */
8907 new_x > it->last_visible_x
8908 /* Or it fits exactly and we're on a window
8909 system frame. */
8910 || (new_x == it->last_visible_x
8911 && FRAME_WINDOW_P (it->f)
8912 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8913 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8914 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8916 bool moved_forward = false;
8918 if (/* IT->hpos == 0 means the very first glyph
8919 doesn't fit on the line, e.g. a wide image. */
8920 it->hpos == 0
8921 || (new_x == it->last_visible_x
8922 && FRAME_WINDOW_P (it->f)))
8924 ++it->hpos;
8925 it->current_x = new_x;
8927 /* The character's last glyph just barely fits
8928 in this row. */
8929 if (i == it->nglyphs - 1)
8931 /* If this is the destination position,
8932 return a position *before* it in this row,
8933 now that we know it fits in this row. */
8934 if (BUFFER_POS_REACHED_P ())
8936 bool can_wrap = true;
8938 /* If we are at a whitespace character
8939 that barely fits on this screen line,
8940 but the next character is also
8941 whitespace, we cannot wrap here. */
8942 if (it->line_wrap == WORD_WRAP
8943 && wrap_it.sp >= 0
8944 && may_wrap
8945 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8947 struct it tem_it;
8948 void *tem_data = NULL;
8950 SAVE_IT (tem_it, *it, tem_data);
8951 set_iterator_to_next (it, true);
8952 if (get_next_display_element (it)
8953 && IT_DISPLAYING_WHITESPACE (it))
8954 can_wrap = false;
8955 RESTORE_IT (it, &tem_it, tem_data);
8957 if (it->line_wrap != WORD_WRAP
8958 || wrap_it.sp < 0
8959 /* If we've just found whitespace
8960 where we can wrap, effectively
8961 ignore the previous wrap point --
8962 it is no longer relevant, but we
8963 won't have an opportunity to
8964 update it, since we've reached
8965 the edge of this screen line. */
8966 || (may_wrap && can_wrap
8967 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8969 it->hpos = hpos_before_this_char;
8970 it->current_x = x_before_this_char;
8971 result = MOVE_POS_MATCH_OR_ZV;
8972 break;
8974 if (it->line_wrap == WORD_WRAP
8975 && atpos_it.sp < 0)
8977 SAVE_IT (atpos_it, *it, atpos_data);
8978 atpos_it.current_x = x_before_this_char;
8979 atpos_it.hpos = hpos_before_this_char;
8983 prev_method = it->method;
8984 if (it->method == GET_FROM_BUFFER)
8985 prev_pos = IT_CHARPOS (*it);
8986 set_iterator_to_next (it, true);
8987 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8988 SET_TEXT_POS (this_line_min_pos,
8989 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8990 /* On graphical terminals, newlines may
8991 "overflow" into the fringe if
8992 overflow-newline-into-fringe is non-nil.
8993 On text terminals, and on graphical
8994 terminals with no right margin, newlines
8995 may overflow into the last glyph on the
8996 display line.*/
8997 if (!FRAME_WINDOW_P (it->f)
8998 || ((it->bidi_p
8999 && it->bidi_it.paragraph_dir == R2L)
9000 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9001 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9002 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9004 if (!get_next_display_element (it))
9006 result = MOVE_POS_MATCH_OR_ZV;
9007 break;
9009 moved_forward = true;
9010 if (BUFFER_POS_REACHED_P ())
9012 if (ITERATOR_AT_END_OF_LINE_P (it))
9013 result = MOVE_POS_MATCH_OR_ZV;
9014 else
9015 result = MOVE_LINE_CONTINUED;
9016 break;
9018 if (ITERATOR_AT_END_OF_LINE_P (it)
9019 && (it->line_wrap != WORD_WRAP
9020 || wrap_it.sp < 0
9021 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9023 result = MOVE_NEWLINE_OR_CR;
9024 break;
9029 else
9030 IT_RESET_X_ASCENT_DESCENT (it);
9032 /* If the screen line ends with whitespace, and we
9033 are under word-wrap, don't use wrap_it: it is no
9034 longer relevant, but we won't have an opportunity
9035 to update it, since we are done with this screen
9036 line. */
9037 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9038 /* If the character after the one which set the
9039 may_wrap flag is also whitespace, we can't
9040 wrap here, since the screen line cannot be
9041 wrapped in the middle of whitespace.
9042 Therefore, wrap_it _is_ relevant in that
9043 case. */
9044 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9046 /* If we've found TO_X, go back there, as we now
9047 know the last word fits on this screen line. */
9048 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9049 && atx_it.sp >= 0)
9051 RESTORE_IT (it, &atx_it, atx_data);
9052 atpos_it.sp = -1;
9053 atx_it.sp = -1;
9054 result = MOVE_X_REACHED;
9055 break;
9058 else if (wrap_it.sp >= 0)
9060 RESTORE_IT (it, &wrap_it, wrap_data);
9061 atpos_it.sp = -1;
9062 atx_it.sp = -1;
9065 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9066 IT_CHARPOS (*it)));
9067 result = MOVE_LINE_CONTINUED;
9068 break;
9071 if (BUFFER_POS_REACHED_P ())
9073 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9074 goto buffer_pos_reached;
9075 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9077 SAVE_IT (atpos_it, *it, atpos_data);
9078 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9082 if (new_x > it->first_visible_x)
9084 /* If we have reached the visible portion of the
9085 screen line, produce the line number if needed. */
9086 if (line_number_pending)
9088 line_number_pending = false;
9089 it->current_x = it->first_visible_x;
9090 maybe_produce_line_number (it);
9091 it->current_x += new_x - it->first_visible_x;
9093 /* Glyph is visible. Increment number of glyphs that
9094 would be displayed. */
9095 ++it->hpos;
9099 if (result != MOVE_UNDEFINED)
9100 break;
9102 else if (BUFFER_POS_REACHED_P ())
9104 buffer_pos_reached:
9105 IT_RESET_X_ASCENT_DESCENT (it);
9106 result = MOVE_POS_MATCH_OR_ZV;
9107 break;
9109 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9111 /* Stop when TO_X specified and reached. This check is
9112 necessary here because of lines consisting of a line end,
9113 only. The line end will not produce any glyphs and we
9114 would never get MOVE_X_REACHED. */
9115 eassert (it->nglyphs == 0);
9116 result = MOVE_X_REACHED;
9117 break;
9120 /* Is this a line end? If yes, we're done. */
9121 if (ITERATOR_AT_END_OF_LINE_P (it))
9123 /* If we are past TO_CHARPOS, but never saw any character
9124 positions smaller than TO_CHARPOS, return
9125 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9126 did. */
9127 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9129 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9131 if (closest_pos < ZV)
9133 RESTORE_IT (it, &ppos_it, ppos_data);
9134 /* Don't recurse if closest_pos is equal to
9135 to_charpos, since we have just tried that. */
9136 if (closest_pos != to_charpos)
9137 move_it_in_display_line_to (it, closest_pos, -1,
9138 MOVE_TO_POS);
9139 result = MOVE_POS_MATCH_OR_ZV;
9141 else
9142 goto buffer_pos_reached;
9144 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9145 && IT_CHARPOS (*it) > to_charpos)
9146 goto buffer_pos_reached;
9147 else
9148 result = MOVE_NEWLINE_OR_CR;
9150 else
9151 result = MOVE_NEWLINE_OR_CR;
9152 /* If we've processed the newline, make sure this flag is
9153 reset, as it must only be set when the newline itself is
9154 processed. */
9155 if (result == MOVE_NEWLINE_OR_CR)
9156 it->constrain_row_ascent_descent_p = false;
9157 break;
9160 prev_method = it->method;
9161 if (it->method == GET_FROM_BUFFER)
9162 prev_pos = IT_CHARPOS (*it);
9163 /* The current display element has been consumed. Advance
9164 to the next. */
9165 set_iterator_to_next (it, true);
9166 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9167 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9168 if (IT_CHARPOS (*it) < to_charpos)
9169 saw_smaller_pos = true;
9170 if (it->bidi_p
9171 && (op & MOVE_TO_POS)
9172 && IT_CHARPOS (*it) >= to_charpos
9173 && IT_CHARPOS (*it) < closest_pos)
9174 closest_pos = IT_CHARPOS (*it);
9176 /* Stop if lines are truncated and IT's current x-position is
9177 past the right edge of the window now. */
9178 if (it->line_wrap == TRUNCATE
9179 && it->current_x >= it->last_visible_x)
9181 if (!FRAME_WINDOW_P (it->f)
9182 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9183 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9184 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9185 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9187 bool at_eob_p = false;
9189 if ((at_eob_p = !get_next_display_element (it))
9190 || BUFFER_POS_REACHED_P ()
9191 /* If we are past TO_CHARPOS, but never saw any
9192 character positions smaller than TO_CHARPOS,
9193 return MOVE_POS_MATCH_OR_ZV, like the
9194 unidirectional display did. */
9195 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9196 && !saw_smaller_pos
9197 && IT_CHARPOS (*it) > to_charpos))
9199 if (it->bidi_p
9200 && !BUFFER_POS_REACHED_P ()
9201 && !at_eob_p && closest_pos < ZV)
9203 RESTORE_IT (it, &ppos_it, ppos_data);
9204 if (closest_pos != to_charpos)
9205 move_it_in_display_line_to (it, closest_pos, -1,
9206 MOVE_TO_POS);
9208 result = MOVE_POS_MATCH_OR_ZV;
9209 break;
9211 if (ITERATOR_AT_END_OF_LINE_P (it))
9213 result = MOVE_NEWLINE_OR_CR;
9214 break;
9217 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9218 && !saw_smaller_pos
9219 && IT_CHARPOS (*it) > to_charpos)
9221 if (closest_pos < ZV)
9223 RESTORE_IT (it, &ppos_it, ppos_data);
9224 if (closest_pos != to_charpos)
9225 move_it_in_display_line_to (it, closest_pos, -1,
9226 MOVE_TO_POS);
9228 result = MOVE_POS_MATCH_OR_ZV;
9229 break;
9231 result = MOVE_LINE_TRUNCATED;
9232 break;
9234 #undef IT_RESET_X_ASCENT_DESCENT
9237 #undef BUFFER_POS_REACHED_P
9239 /* If we scanned beyond TO_POS, restore the saved iterator either to
9240 the wrap point (if found), or to atpos/atx location. We decide which
9241 data to use to restore the saved iterator state by their X coordinates,
9242 since buffer positions might increase non-monotonically with screen
9243 coordinates due to bidi reordering. */
9244 if (result == MOVE_LINE_CONTINUED
9245 && it->line_wrap == WORD_WRAP
9246 && wrap_it.sp >= 0
9247 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9248 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9249 RESTORE_IT (it, &wrap_it, wrap_data);
9250 else if (atpos_it.sp >= 0)
9251 RESTORE_IT (it, &atpos_it, atpos_data);
9252 else if (atx_it.sp >= 0)
9253 RESTORE_IT (it, &atx_it, atx_data);
9255 done:
9257 if (atpos_data)
9258 bidi_unshelve_cache (atpos_data, true);
9259 if (atx_data)
9260 bidi_unshelve_cache (atx_data, true);
9261 if (wrap_data)
9262 bidi_unshelve_cache (wrap_data, true);
9263 if (ppos_data)
9264 bidi_unshelve_cache (ppos_data, true);
9266 /* Restore the iterator settings altered at the beginning of this
9267 function. */
9268 it->glyph_row = saved_glyph_row;
9269 return result;
9272 /* For external use. */
9273 void
9274 move_it_in_display_line (struct it *it,
9275 ptrdiff_t to_charpos, int to_x,
9276 enum move_operation_enum op)
9278 if (it->line_wrap == WORD_WRAP
9279 && (op & MOVE_TO_X))
9281 struct it save_it;
9282 void *save_data = NULL;
9283 int skip;
9285 SAVE_IT (save_it, *it, save_data);
9286 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9287 /* When word-wrap is on, TO_X may lie past the end
9288 of a wrapped line. Then it->current is the
9289 character on the next line, so backtrack to the
9290 space before the wrap point. */
9291 if (skip == MOVE_LINE_CONTINUED)
9293 int prev_x = max (it->current_x - 1, 0);
9294 RESTORE_IT (it, &save_it, save_data);
9295 move_it_in_display_line_to
9296 (it, -1, prev_x, MOVE_TO_X);
9298 else
9299 bidi_unshelve_cache (save_data, true);
9301 else
9302 move_it_in_display_line_to (it, to_charpos, to_x, op);
9306 /* Move IT forward until it satisfies one or more of the criteria in
9307 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9309 OP is a bit-mask that specifies where to stop, and in particular,
9310 which of those four position arguments makes a difference. See the
9311 description of enum move_operation_enum.
9313 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9314 screen line, this function will set IT to the next position that is
9315 displayed to the right of TO_CHARPOS on the screen.
9317 Return the maximum pixel length of any line scanned but never more
9318 than it.last_visible_x. */
9321 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9323 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9324 int line_height, line_start_x = 0, reached = 0;
9325 int max_current_x = 0;
9326 void *backup_data = NULL;
9328 for (;;)
9330 if (op & MOVE_TO_VPOS)
9332 /* If no TO_CHARPOS and no TO_X specified, stop at the
9333 start of the line TO_VPOS. */
9334 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9336 if (it->vpos == to_vpos)
9338 reached = 1;
9339 break;
9341 else
9342 skip = move_it_in_display_line_to (it, -1, -1, 0);
9344 else
9346 /* TO_VPOS >= 0 means stop at TO_X in the line at
9347 TO_VPOS, or at TO_POS, whichever comes first. */
9348 if (it->vpos == to_vpos)
9350 reached = 2;
9351 break;
9354 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9356 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9358 reached = 3;
9359 break;
9361 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9363 /* We have reached TO_X but not in the line we want. */
9364 skip = move_it_in_display_line_to (it, to_charpos,
9365 -1, MOVE_TO_POS);
9366 if (skip == MOVE_POS_MATCH_OR_ZV)
9368 reached = 4;
9369 break;
9374 else if (op & MOVE_TO_Y)
9376 struct it it_backup;
9378 if (it->line_wrap == WORD_WRAP)
9379 SAVE_IT (it_backup, *it, backup_data);
9381 /* TO_Y specified means stop at TO_X in the line containing
9382 TO_Y---or at TO_CHARPOS if this is reached first. The
9383 problem is that we can't really tell whether the line
9384 contains TO_Y before we have completely scanned it, and
9385 this may skip past TO_X. What we do is to first scan to
9386 TO_X.
9388 If TO_X is not specified, use a TO_X of zero. The reason
9389 is to make the outcome of this function more predictable.
9390 If we didn't use TO_X == 0, we would stop at the end of
9391 the line which is probably not what a caller would expect
9392 to happen. */
9393 skip = move_it_in_display_line_to
9394 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9395 (MOVE_TO_X | (op & MOVE_TO_POS)));
9397 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9398 if (skip == MOVE_POS_MATCH_OR_ZV)
9399 reached = 5;
9400 else if (skip == MOVE_X_REACHED)
9402 /* If TO_X was reached, we want to know whether TO_Y is
9403 in the line. We know this is the case if the already
9404 scanned glyphs make the line tall enough. Otherwise,
9405 we must check by scanning the rest of the line. */
9406 line_height = it->max_ascent + it->max_descent;
9407 if (to_y >= it->current_y
9408 && to_y < it->current_y + line_height)
9410 reached = 6;
9411 break;
9413 SAVE_IT (it_backup, *it, backup_data);
9414 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9415 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9416 op & MOVE_TO_POS);
9417 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9418 line_height = it->max_ascent + it->max_descent;
9419 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9421 if (to_y >= it->current_y
9422 && to_y < it->current_y + line_height)
9424 /* If TO_Y is in this line and TO_X was reached
9425 above, we scanned too far. We have to restore
9426 IT's settings to the ones before skipping. But
9427 keep the more accurate values of max_ascent and
9428 max_descent we've found while skipping the rest
9429 of the line, for the sake of callers, such as
9430 pos_visible_p, that need to know the line
9431 height. */
9432 int max_ascent = it->max_ascent;
9433 int max_descent = it->max_descent;
9435 RESTORE_IT (it, &it_backup, backup_data);
9436 it->max_ascent = max_ascent;
9437 it->max_descent = max_descent;
9438 reached = 6;
9440 else
9442 skip = skip2;
9443 if (skip == MOVE_POS_MATCH_OR_ZV)
9444 reached = 7;
9447 else
9449 /* Check whether TO_Y is in this line. */
9450 line_height = it->max_ascent + it->max_descent;
9451 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9453 if (to_y >= it->current_y
9454 && to_y < it->current_y + line_height)
9456 if (to_y > it->current_y)
9457 max_current_x = max (it->current_x, max_current_x);
9459 /* When word-wrap is on, TO_X may lie past the end
9460 of a wrapped line. Then it->current is the
9461 character on the next line, so backtrack to the
9462 space before the wrap point. */
9463 if (skip == MOVE_LINE_CONTINUED
9464 && it->line_wrap == WORD_WRAP)
9466 int prev_x = max (it->current_x - 1, 0);
9467 RESTORE_IT (it, &it_backup, backup_data);
9468 skip = move_it_in_display_line_to
9469 (it, -1, prev_x, MOVE_TO_X);
9472 reached = 6;
9476 if (reached)
9478 max_current_x = max (it->current_x, max_current_x);
9479 break;
9482 else if (BUFFERP (it->object)
9483 && (it->method == GET_FROM_BUFFER
9484 || it->method == GET_FROM_STRETCH)
9485 && IT_CHARPOS (*it) >= to_charpos
9486 /* Under bidi iteration, a call to set_iterator_to_next
9487 can scan far beyond to_charpos if the initial
9488 portion of the next line needs to be reordered. In
9489 that case, give move_it_in_display_line_to another
9490 chance below. */
9491 && !(it->bidi_p
9492 && it->bidi_it.scan_dir == -1))
9493 skip = MOVE_POS_MATCH_OR_ZV;
9494 else
9495 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9497 switch (skip)
9499 case MOVE_POS_MATCH_OR_ZV:
9500 max_current_x = max (it->current_x, max_current_x);
9501 reached = 8;
9502 goto out;
9504 case MOVE_NEWLINE_OR_CR:
9505 max_current_x = max (it->current_x, max_current_x);
9506 set_iterator_to_next (it, true);
9507 it->continuation_lines_width = 0;
9508 break;
9510 case MOVE_LINE_TRUNCATED:
9511 max_current_x = it->last_visible_x;
9512 it->continuation_lines_width = 0;
9513 reseat_at_next_visible_line_start (it, false);
9514 if ((op & MOVE_TO_POS) != 0
9515 && IT_CHARPOS (*it) > to_charpos)
9517 reached = 9;
9518 goto out;
9520 break;
9522 case MOVE_LINE_CONTINUED:
9523 max_current_x = it->last_visible_x;
9524 /* For continued lines ending in a tab, some of the glyphs
9525 associated with the tab are displayed on the current
9526 line. Since it->current_x does not include these glyphs,
9527 we use it->last_visible_x instead. */
9528 if (it->c == '\t')
9530 it->continuation_lines_width += it->last_visible_x;
9531 /* When moving by vpos, ensure that the iterator really
9532 advances to the next line (bug#847, bug#969). Fixme:
9533 do we need to do this in other circumstances? */
9534 if (it->current_x != it->last_visible_x
9535 && (op & MOVE_TO_VPOS)
9536 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9538 line_start_x = it->current_x + it->pixel_width
9539 - it->last_visible_x;
9540 if (FRAME_WINDOW_P (it->f))
9542 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9543 struct font *face_font = face->font;
9545 /* When display_line produces a continued line
9546 that ends in a TAB, it skips a tab stop that
9547 is closer than the font's space character
9548 width (see x_produce_glyphs where it produces
9549 the stretch glyph which represents a TAB).
9550 We need to reproduce the same logic here. */
9551 eassert (face_font);
9552 if (face_font)
9554 if (line_start_x < face_font->space_width)
9555 line_start_x
9556 += it->tab_width * face_font->space_width;
9559 set_iterator_to_next (it, false);
9562 else
9563 it->continuation_lines_width += it->current_x;
9564 break;
9566 default:
9567 emacs_abort ();
9570 /* Reset/increment for the next run. */
9571 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9572 it->current_x = line_start_x;
9573 line_start_x = 0;
9574 it->hpos = 0;
9575 it->current_y += it->max_ascent + it->max_descent;
9576 ++it->vpos;
9577 last_height = it->max_ascent + it->max_descent;
9578 it->max_ascent = it->max_descent = 0;
9581 out:
9583 /* On text terminals, we may stop at the end of a line in the middle
9584 of a multi-character glyph. If the glyph itself is continued,
9585 i.e. it is actually displayed on the next line, don't treat this
9586 stopping point as valid; move to the next line instead (unless
9587 that brings us offscreen). */
9588 if (!FRAME_WINDOW_P (it->f)
9589 && op & MOVE_TO_POS
9590 && IT_CHARPOS (*it) == to_charpos
9591 && it->what == IT_CHARACTER
9592 && it->nglyphs > 1
9593 && it->line_wrap == WINDOW_WRAP
9594 && it->current_x == it->last_visible_x - 1
9595 && it->c != '\n'
9596 && it->c != '\t'
9597 && it->w->window_end_valid
9598 && it->vpos < it->w->window_end_vpos)
9600 it->continuation_lines_width += it->current_x;
9601 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9602 it->current_y += it->max_ascent + it->max_descent;
9603 ++it->vpos;
9604 last_height = it->max_ascent + it->max_descent;
9607 if (backup_data)
9608 bidi_unshelve_cache (backup_data, true);
9610 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9612 return max_current_x;
9616 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9618 If DY > 0, move IT backward at least that many pixels. DY = 0
9619 means move IT backward to the preceding line start or BEGV. This
9620 function may move over more than DY pixels if IT->current_y - DY
9621 ends up in the middle of a line; in this case IT->current_y will be
9622 set to the top of the line moved to. */
9624 void
9625 move_it_vertically_backward (struct it *it, int dy)
9627 int nlines, h;
9628 struct it it2, it3;
9629 void *it2data = NULL, *it3data = NULL;
9630 ptrdiff_t start_pos;
9631 int nchars_per_row
9632 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9633 ptrdiff_t pos_limit;
9635 move_further_back:
9636 eassert (dy >= 0);
9638 start_pos = IT_CHARPOS (*it);
9640 /* Estimate how many newlines we must move back. */
9641 nlines = max (1, dy / default_line_pixel_height (it->w));
9642 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9643 pos_limit = BEGV;
9644 else
9645 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9647 /* Set the iterator's position that many lines back. But don't go
9648 back more than NLINES full screen lines -- this wins a day with
9649 buffers which have very long lines. */
9650 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9651 back_to_previous_visible_line_start (it);
9653 /* Reseat the iterator here. When moving backward, we don't want
9654 reseat to skip forward over invisible text, set up the iterator
9655 to deliver from overlay strings at the new position etc. So,
9656 use reseat_1 here. */
9657 reseat_1 (it, it->current.pos, true);
9659 /* We are now surely at a line start. */
9660 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9661 reordering is in effect. */
9662 it->continuation_lines_width = 0;
9664 /* Move forward and see what y-distance we moved. First move to the
9665 start of the next line so that we get its height. We need this
9666 height to be able to tell whether we reached the specified
9667 y-distance. */
9668 SAVE_IT (it2, *it, it2data);
9669 it2.max_ascent = it2.max_descent = 0;
9672 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9673 MOVE_TO_POS | MOVE_TO_VPOS);
9675 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9676 /* If we are in a display string which starts at START_POS,
9677 and that display string includes a newline, and we are
9678 right after that newline (i.e. at the beginning of a
9679 display line), exit the loop, because otherwise we will
9680 infloop, since move_it_to will see that it is already at
9681 START_POS and will not move. */
9682 || (it2.method == GET_FROM_STRING
9683 && IT_CHARPOS (it2) == start_pos
9684 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9685 eassert (IT_CHARPOS (*it) >= BEGV);
9686 SAVE_IT (it3, it2, it3data);
9688 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9689 eassert (IT_CHARPOS (*it) >= BEGV);
9690 /* H is the actual vertical distance from the position in *IT
9691 and the starting position. */
9692 h = it2.current_y - it->current_y;
9693 /* NLINES is the distance in number of lines. */
9694 nlines = it2.vpos - it->vpos;
9696 /* Correct IT's y and vpos position
9697 so that they are relative to the starting point. */
9698 it->vpos -= nlines;
9699 it->current_y -= h;
9701 if (dy == 0)
9703 /* DY == 0 means move to the start of the screen line. The
9704 value of nlines is > 0 if continuation lines were involved,
9705 or if the original IT position was at start of a line. */
9706 RESTORE_IT (it, it, it2data);
9707 if (nlines > 0)
9708 move_it_by_lines (it, nlines);
9709 /* The above code moves us to some position NLINES down,
9710 usually to its first glyph (leftmost in an L2R line), but
9711 that's not necessarily the start of the line, under bidi
9712 reordering. We want to get to the character position
9713 that is immediately after the newline of the previous
9714 line. */
9715 if (it->bidi_p
9716 && !it->continuation_lines_width
9717 && !STRINGP (it->string)
9718 && IT_CHARPOS (*it) > BEGV
9719 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9721 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9723 DEC_BOTH (cp, bp);
9724 cp = find_newline_no_quit (cp, bp, -1, NULL);
9725 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9727 bidi_unshelve_cache (it3data, true);
9729 else
9731 /* The y-position we try to reach, relative to *IT.
9732 Note that H has been subtracted in front of the if-statement. */
9733 int target_y = it->current_y + h - dy;
9734 int y0 = it3.current_y;
9735 int y1;
9736 int line_height;
9738 RESTORE_IT (&it3, &it3, it3data);
9739 y1 = line_bottom_y (&it3);
9740 line_height = y1 - y0;
9741 RESTORE_IT (it, it, it2data);
9742 /* If we did not reach target_y, try to move further backward if
9743 we can. If we moved too far backward, try to move forward. */
9744 if (target_y < it->current_y
9745 /* This is heuristic. In a window that's 3 lines high, with
9746 a line height of 13 pixels each, recentering with point
9747 on the bottom line will try to move -39/2 = 19 pixels
9748 backward. Try to avoid moving into the first line. */
9749 && (it->current_y - target_y
9750 > min (window_box_height (it->w), line_height * 2 / 3))
9751 && IT_CHARPOS (*it) > BEGV)
9753 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9754 target_y - it->current_y));
9755 dy = it->current_y - target_y;
9756 goto move_further_back;
9758 else if (target_y >= it->current_y + line_height
9759 && IT_CHARPOS (*it) < ZV)
9761 /* Should move forward by at least one line, maybe more.
9763 Note: Calling move_it_by_lines can be expensive on
9764 terminal frames, where compute_motion is used (via
9765 vmotion) to do the job, when there are very long lines
9766 and truncate-lines is nil. That's the reason for
9767 treating terminal frames specially here. */
9769 if (!FRAME_WINDOW_P (it->f))
9770 move_it_vertically (it, target_y - it->current_y);
9771 else
9775 move_it_by_lines (it, 1);
9777 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9784 /* Move IT by a specified amount of pixel lines DY. DY negative means
9785 move backwards. DY = 0 means move to start of screen line. At the
9786 end, IT will be on the start of a screen line. */
9788 void
9789 move_it_vertically (struct it *it, int dy)
9791 if (dy <= 0)
9792 move_it_vertically_backward (it, -dy);
9793 else
9795 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9796 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9797 MOVE_TO_POS | MOVE_TO_Y);
9798 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9800 /* If buffer ends in ZV without a newline, move to the start of
9801 the line to satisfy the post-condition. */
9802 if (IT_CHARPOS (*it) == ZV
9803 && ZV > BEGV
9804 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9805 move_it_by_lines (it, 0);
9810 /* Move iterator IT past the end of the text line it is in. */
9812 void
9813 move_it_past_eol (struct it *it)
9815 enum move_it_result rc;
9817 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9818 if (rc == MOVE_NEWLINE_OR_CR)
9819 set_iterator_to_next (it, false);
9823 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9824 negative means move up. DVPOS == 0 means move to the start of the
9825 screen line.
9827 Optimization idea: If we would know that IT->f doesn't use
9828 a face with proportional font, we could be faster for
9829 truncate-lines nil. */
9831 void
9832 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9835 /* The commented-out optimization uses vmotion on terminals. This
9836 gives bad results, because elements like it->what, on which
9837 callers such as pos_visible_p rely, aren't updated. */
9838 /* struct position pos;
9839 if (!FRAME_WINDOW_P (it->f))
9841 struct text_pos textpos;
9843 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9844 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9845 reseat (it, textpos, true);
9846 it->vpos += pos.vpos;
9847 it->current_y += pos.vpos;
9849 else */
9851 if (dvpos == 0)
9853 /* DVPOS == 0 means move to the start of the screen line. */
9854 move_it_vertically_backward (it, 0);
9855 /* Let next call to line_bottom_y calculate real line height. */
9856 last_height = 0;
9858 else if (dvpos > 0)
9860 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9861 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9863 /* Only move to the next buffer position if we ended up in a
9864 string from display property, not in an overlay string
9865 (before-string or after-string). That is because the
9866 latter don't conceal the underlying buffer position, so
9867 we can ask to move the iterator to the exact position we
9868 are interested in. Note that, even if we are already at
9869 IT_CHARPOS (*it), the call below is not a no-op, as it
9870 will detect that we are at the end of the string, pop the
9871 iterator, and compute it->current_x and it->hpos
9872 correctly. */
9873 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9874 -1, -1, -1, MOVE_TO_POS);
9877 else
9879 struct it it2;
9880 void *it2data = NULL;
9881 ptrdiff_t start_charpos, i;
9882 int nchars_per_row
9883 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9884 bool hit_pos_limit = false;
9885 ptrdiff_t pos_limit;
9887 /* Start at the beginning of the screen line containing IT's
9888 position. This may actually move vertically backwards,
9889 in case of overlays, so adjust dvpos accordingly. */
9890 dvpos += it->vpos;
9891 move_it_vertically_backward (it, 0);
9892 dvpos -= it->vpos;
9894 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9895 screen lines, and reseat the iterator there. */
9896 start_charpos = IT_CHARPOS (*it);
9897 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9898 pos_limit = BEGV;
9899 else
9900 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9902 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9903 back_to_previous_visible_line_start (it);
9904 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9905 hit_pos_limit = true;
9906 reseat (it, it->current.pos, true);
9908 /* Move further back if we end up in a string or an image. */
9909 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9911 /* First try to move to start of display line. */
9912 dvpos += it->vpos;
9913 move_it_vertically_backward (it, 0);
9914 dvpos -= it->vpos;
9915 if (IT_POS_VALID_AFTER_MOVE_P (it))
9916 break;
9917 /* If start of line is still in string or image,
9918 move further back. */
9919 back_to_previous_visible_line_start (it);
9920 reseat (it, it->current.pos, true);
9921 dvpos--;
9924 it->current_x = it->hpos = 0;
9926 /* Above call may have moved too far if continuation lines
9927 are involved. Scan forward and see if it did. */
9928 SAVE_IT (it2, *it, it2data);
9929 it2.vpos = it2.current_y = 0;
9930 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9931 it->vpos -= it2.vpos;
9932 it->current_y -= it2.current_y;
9933 it->current_x = it->hpos = 0;
9935 /* If we moved too far back, move IT some lines forward. */
9936 if (it2.vpos > -dvpos)
9938 int delta = it2.vpos + dvpos;
9940 RESTORE_IT (&it2, &it2, it2data);
9941 SAVE_IT (it2, *it, it2data);
9942 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9943 /* Move back again if we got too far ahead. */
9944 if (IT_CHARPOS (*it) >= start_charpos)
9945 RESTORE_IT (it, &it2, it2data);
9946 else
9947 bidi_unshelve_cache (it2data, true);
9949 else if (hit_pos_limit && pos_limit > BEGV
9950 && dvpos < 0 && it2.vpos < -dvpos)
9952 /* If we hit the limit, but still didn't make it far enough
9953 back, that means there's a display string with a newline
9954 covering a large chunk of text, and that caused
9955 back_to_previous_visible_line_start try to go too far.
9956 Punish those who commit such atrocities by going back
9957 until we've reached DVPOS, after lifting the limit, which
9958 could make it slow for very long lines. "If it hurts,
9959 don't do that!" */
9960 dvpos += it2.vpos;
9961 RESTORE_IT (it, it, it2data);
9962 for (i = -dvpos; i > 0; --i)
9964 back_to_previous_visible_line_start (it);
9965 it->vpos--;
9967 reseat_1 (it, it->current.pos, true);
9969 else
9970 RESTORE_IT (it, it, it2data);
9975 partial_line_height (struct it *it_origin)
9977 int partial_height;
9978 void *it_data = NULL;
9979 struct it it;
9980 SAVE_IT (it, *it_origin, it_data);
9981 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9982 MOVE_TO_POS | MOVE_TO_Y);
9983 if (it.what == IT_EOB)
9985 int vis_height = it.last_visible_y - it.current_y;
9986 int height = it.ascent + it.descent;
9987 partial_height = (vis_height < height) ? vis_height : 0;
9989 else
9991 int last_line_y = it.current_y;
9992 move_it_by_lines (&it, 1);
9993 partial_height = (it.current_y > it.last_visible_y)
9994 ? it.last_visible_y - last_line_y : 0;
9996 RESTORE_IT (&it, &it, it_data);
9997 return partial_height;
10000 /* Return true if IT points into the middle of a display vector. */
10002 bool
10003 in_display_vector_p (struct it *it)
10005 return (it->method == GET_FROM_DISPLAY_VECTOR
10006 && it->current.dpvec_index > 0
10007 && it->dpvec + it->current.dpvec_index != it->dpend);
10010 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
10011 doc: /* Return the size of the text of WINDOW's buffer in pixels.
10012 WINDOW must be a live window and defaults to the selected one. The
10013 return value is a cons of the maximum pixel-width of any text line and
10014 the maximum pixel-height of all text lines.
10016 The optional argument FROM, if non-nil, specifies the first text
10017 position and defaults to the minimum accessible position of the buffer.
10018 If FROM is t, use the minimum accessible position that starts a
10019 non-empty line. TO, if non-nil, specifies the last text position and
10020 defaults to the maximum accessible position of the buffer. If TO is t,
10021 use the maximum accessible position that ends a non-empty line.
10023 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10024 width that can be returned. X-LIMIT nil or omitted, means to use the
10025 pixel-width of WINDOW's body; use this if you want to know how high
10026 WINDOW should be become in order to fit all of its buffer's text with
10027 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10028 if you intend to change WINDOW's width. In any case, text whose
10029 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10030 of long lines can take some time, it's always a good idea to make this
10031 argument as small as possible; in particular, if the buffer contains
10032 long lines that shall be truncated anyway.
10034 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10035 height (excluding the height of the mode- or header-line, if any) that
10036 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10037 ignored. Since calculating the text height of a large buffer can take
10038 some time, it makes sense to specify this argument if the size of the
10039 buffer is large or unknown.
10041 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10042 include the height of the mode- or header-line of WINDOW in the return
10043 value. If it is either the symbol `mode-line' or `header-line', include
10044 only the height of that line, if present, in the return value. If t,
10045 include the height of both, if present, in the return value. */)
10046 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10047 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10049 struct window *w = decode_live_window (window);
10050 Lisp_Object buffer = w->contents;
10051 struct buffer *b;
10052 struct it it;
10053 struct buffer *old_b = NULL;
10054 ptrdiff_t start, end, pos;
10055 struct text_pos startp;
10056 void *itdata = NULL;
10057 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10059 CHECK_BUFFER (buffer);
10060 b = XBUFFER (buffer);
10062 if (b != current_buffer)
10064 old_b = current_buffer;
10065 set_buffer_internal (b);
10068 if (NILP (from))
10069 start = BEGV;
10070 else if (EQ (from, Qt))
10072 start = pos = BEGV;
10073 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10074 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10075 start = pos;
10076 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10077 start = pos;
10079 else
10081 CHECK_NUMBER_COERCE_MARKER (from);
10082 start = min (max (XINT (from), BEGV), ZV);
10085 if (NILP (to))
10086 end = ZV;
10087 else if (EQ (to, Qt))
10089 end = pos = ZV;
10090 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10091 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10092 end = pos;
10093 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10094 end = pos;
10096 else
10098 CHECK_NUMBER_COERCE_MARKER (to);
10099 end = max (start, min (XINT (to), ZV));
10102 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10103 max_x = XINT (x_limit);
10105 if (NILP (y_limit))
10106 max_y = INT_MAX;
10107 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10108 max_y = XINT (y_limit);
10110 itdata = bidi_shelve_cache ();
10111 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10112 start_display (&it, w, startp);
10114 if (NILP (x_limit))
10115 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10116 else
10118 it.last_visible_x = max_x;
10119 /* Actually, we never want move_it_to stop at to_x. But to make
10120 sure that move_it_in_display_line_to always moves far enough,
10121 we set it to INT_MAX and specify MOVE_TO_X. */
10122 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10123 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10124 /* Don't return more than X-LIMIT. */
10125 if (x > max_x)
10126 x = max_x;
10129 /* Subtract height of header-line which was counted automatically by
10130 start_display. */
10131 y = it.current_y + it.max_ascent + it.max_descent
10132 - WINDOW_HEADER_LINE_HEIGHT (w);
10133 /* Don't return more than Y-LIMIT. */
10134 if (y > max_y)
10135 y = max_y;
10137 if (EQ (mode_and_header_line, Qheader_line)
10138 || EQ (mode_and_header_line, Qt))
10139 /* Re-add height of header-line as requested. */
10140 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10142 if (EQ (mode_and_header_line, Qmode_line)
10143 || EQ (mode_and_header_line, Qt))
10144 /* Add height of mode-line as requested. */
10145 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10147 bidi_unshelve_cache (itdata, false);
10149 if (old_b)
10150 set_buffer_internal (old_b);
10152 return Fcons (make_number (x), make_number (y));
10155 /***********************************************************************
10156 Messages
10157 ***********************************************************************/
10159 /* Return the number of arguments the format string FORMAT needs. */
10161 static ptrdiff_t
10162 format_nargs (char const *format)
10164 ptrdiff_t nargs = 0;
10165 for (char const *p = format; (p = strchr (p, '%')); p++)
10166 if (p[1] == '%')
10167 p++;
10168 else
10169 nargs++;
10170 return nargs;
10173 /* Add a message with format string FORMAT and formatted arguments
10174 to *Messages*. */
10176 void
10177 add_to_log (const char *format, ...)
10179 va_list ap;
10180 va_start (ap, format);
10181 vadd_to_log (format, ap);
10182 va_end (ap);
10185 void
10186 vadd_to_log (char const *format, va_list ap)
10188 ptrdiff_t form_nargs = format_nargs (format);
10189 ptrdiff_t nargs = 1 + form_nargs;
10190 Lisp_Object args[10];
10191 eassert (nargs <= ARRAYELTS (args));
10192 AUTO_STRING (args0, format);
10193 args[0] = args0;
10194 for (ptrdiff_t i = 1; i <= nargs; i++)
10195 args[i] = va_arg (ap, Lisp_Object);
10196 Lisp_Object msg = Qnil;
10197 msg = Fformat_message (nargs, args);
10199 ptrdiff_t len = SBYTES (msg) + 1;
10200 USE_SAFE_ALLOCA;
10201 char *buffer = SAFE_ALLOCA (len);
10202 memcpy (buffer, SDATA (msg), len);
10204 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10205 SAFE_FREE ();
10209 /* Output a newline in the *Messages* buffer if "needs" one. */
10211 void
10212 message_log_maybe_newline (void)
10214 if (message_log_need_newline)
10215 message_dolog ("", 0, true, false);
10219 /* Add a string M of length NBYTES to the message log, optionally
10220 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10221 true, means interpret the contents of M as multibyte. This
10222 function calls low-level routines in order to bypass text property
10223 hooks, etc. which might not be safe to run.
10225 This may GC (insert may run before/after change hooks),
10226 so the buffer M must NOT point to a Lisp string. */
10228 void
10229 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10231 const unsigned char *msg = (const unsigned char *) m;
10233 if (!NILP (Vmemory_full))
10234 return;
10236 if (!NILP (Vmessage_log_max))
10238 struct buffer *oldbuf;
10239 Lisp_Object oldpoint, oldbegv, oldzv;
10240 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10241 ptrdiff_t point_at_end = 0;
10242 ptrdiff_t zv_at_end = 0;
10243 Lisp_Object old_deactivate_mark;
10245 old_deactivate_mark = Vdeactivate_mark;
10246 oldbuf = current_buffer;
10248 /* Ensure the Messages buffer exists, and switch to it.
10249 If we created it, set the major-mode. */
10250 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10251 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10252 if (newbuffer
10253 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10254 call0 (intern ("messages-buffer-mode"));
10256 bset_undo_list (current_buffer, Qt);
10257 bset_cache_long_scans (current_buffer, Qnil);
10259 oldpoint = message_dolog_marker1;
10260 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10261 oldbegv = message_dolog_marker2;
10262 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10263 oldzv = message_dolog_marker3;
10264 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10266 if (PT == Z)
10267 point_at_end = 1;
10268 if (ZV == Z)
10269 zv_at_end = 1;
10271 BEGV = BEG;
10272 BEGV_BYTE = BEG_BYTE;
10273 ZV = Z;
10274 ZV_BYTE = Z_BYTE;
10275 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10277 /* Insert the string--maybe converting multibyte to single byte
10278 or vice versa, so that all the text fits the buffer. */
10279 if (multibyte
10280 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10282 ptrdiff_t i;
10283 int c, char_bytes;
10284 char work[1];
10286 /* Convert a multibyte string to single-byte
10287 for the *Message* buffer. */
10288 for (i = 0; i < nbytes; i += char_bytes)
10290 c = string_char_and_length (msg + i, &char_bytes);
10291 work[0] = CHAR_TO_BYTE8 (c);
10292 insert_1_both (work, 1, 1, true, false, false);
10295 else if (! multibyte
10296 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10298 ptrdiff_t i;
10299 int c, char_bytes;
10300 unsigned char str[MAX_MULTIBYTE_LENGTH];
10301 /* Convert a single-byte string to multibyte
10302 for the *Message* buffer. */
10303 for (i = 0; i < nbytes; i++)
10305 c = msg[i];
10306 MAKE_CHAR_MULTIBYTE (c);
10307 char_bytes = CHAR_STRING (c, str);
10308 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10311 else if (nbytes)
10312 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10313 true, false, false);
10315 if (nlflag)
10317 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10318 printmax_t dups;
10320 insert_1_both ("\n", 1, 1, true, false, false);
10322 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10323 this_bol = PT;
10324 this_bol_byte = PT_BYTE;
10326 /* See if this line duplicates the previous one.
10327 If so, combine duplicates. */
10328 if (this_bol > BEG)
10330 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10331 prev_bol = PT;
10332 prev_bol_byte = PT_BYTE;
10334 dups = message_log_check_duplicate (prev_bol_byte,
10335 this_bol_byte);
10336 if (dups)
10338 del_range_both (prev_bol, prev_bol_byte,
10339 this_bol, this_bol_byte, false);
10340 if (dups > 1)
10342 char dupstr[sizeof " [ times]"
10343 + INT_STRLEN_BOUND (printmax_t)];
10345 /* If you change this format, don't forget to also
10346 change message_log_check_duplicate. */
10347 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10348 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10349 insert_1_both (dupstr, duplen, duplen,
10350 true, false, true);
10355 /* If we have more than the desired maximum number of lines
10356 in the *Messages* buffer now, delete the oldest ones.
10357 This is safe because we don't have undo in this buffer. */
10359 if (NATNUMP (Vmessage_log_max))
10361 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10362 -XFASTINT (Vmessage_log_max) - 1, false);
10363 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10366 BEGV = marker_position (oldbegv);
10367 BEGV_BYTE = marker_byte_position (oldbegv);
10369 if (zv_at_end)
10371 ZV = Z;
10372 ZV_BYTE = Z_BYTE;
10374 else
10376 ZV = marker_position (oldzv);
10377 ZV_BYTE = marker_byte_position (oldzv);
10380 if (point_at_end)
10381 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10382 else
10383 /* We can't do Fgoto_char (oldpoint) because it will run some
10384 Lisp code. */
10385 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10386 marker_byte_position (oldpoint));
10388 unchain_marker (XMARKER (oldpoint));
10389 unchain_marker (XMARKER (oldbegv));
10390 unchain_marker (XMARKER (oldzv));
10392 /* We called insert_1_both above with its 5th argument (PREPARE)
10393 false, which prevents insert_1_both from calling
10394 prepare_to_modify_buffer, which in turns prevents us from
10395 incrementing windows_or_buffers_changed even if *Messages* is
10396 shown in some window. So we must manually set
10397 windows_or_buffers_changed here to make up for that. */
10398 windows_or_buffers_changed = old_windows_or_buffers_changed;
10399 bset_redisplay (current_buffer);
10401 set_buffer_internal (oldbuf);
10403 message_log_need_newline = !nlflag;
10404 Vdeactivate_mark = old_deactivate_mark;
10409 /* We are at the end of the buffer after just having inserted a newline.
10410 (Note: We depend on the fact we won't be crossing the gap.)
10411 Check to see if the most recent message looks a lot like the previous one.
10412 Return 0 if different, 1 if the new one should just replace it, or a
10413 value N > 1 if we should also append " [N times]". */
10415 static intmax_t
10416 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10418 ptrdiff_t i;
10419 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10420 bool seen_dots = false;
10421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10424 for (i = 0; i < len; i++)
10426 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10427 seen_dots = true;
10428 if (p1[i] != p2[i])
10429 return seen_dots;
10431 p1 += len;
10432 if (*p1 == '\n')
10433 return 2;
10434 if (*p1++ == ' ' && *p1++ == '[')
10436 char *pend;
10437 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10438 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10439 return n + 1;
10441 return 0;
10445 /* Display an echo area message M with a specified length of NBYTES
10446 bytes. The string may include null characters. If M is not a
10447 string, clear out any existing message, and let the mini-buffer
10448 text show through.
10450 This function cancels echoing. */
10452 void
10453 message3 (Lisp_Object m)
10455 clear_message (true, true);
10456 cancel_echoing ();
10458 /* First flush out any partial line written with print. */
10459 message_log_maybe_newline ();
10460 if (STRINGP (m))
10462 ptrdiff_t nbytes = SBYTES (m);
10463 bool multibyte = STRING_MULTIBYTE (m);
10464 char *buffer;
10465 USE_SAFE_ALLOCA;
10466 SAFE_ALLOCA_STRING (buffer, m);
10467 message_dolog (buffer, nbytes, true, multibyte);
10468 SAFE_FREE ();
10470 if (! inhibit_message)
10471 message3_nolog (m);
10474 /* Log the message M to stderr. Log an empty line if M is not a string. */
10476 static void
10477 message_to_stderr (Lisp_Object m)
10479 if (noninteractive_need_newline)
10481 noninteractive_need_newline = false;
10482 fputc ('\n', stderr);
10484 if (STRINGP (m))
10486 Lisp_Object coding_system = Vlocale_coding_system;
10487 Lisp_Object s;
10489 if (!NILP (Vcoding_system_for_write))
10490 coding_system = Vcoding_system_for_write;
10491 if (!NILP (coding_system))
10492 s = code_convert_string_norecord (m, coding_system, true);
10493 else
10494 s = m;
10496 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10498 if (!cursor_in_echo_area)
10499 fputc ('\n', stderr);
10500 fflush (stderr);
10503 /* The non-logging version of message3.
10504 This does not cancel echoing, because it is used for echoing.
10505 Perhaps we need to make a separate function for echoing
10506 and make this cancel echoing. */
10508 void
10509 message3_nolog (Lisp_Object m)
10511 struct frame *sf = SELECTED_FRAME ();
10513 if (FRAME_INITIAL_P (sf))
10514 message_to_stderr (m);
10515 /* Error messages get reported properly by cmd_error, so this must be just an
10516 informative message; if the frame hasn't really been initialized yet, just
10517 toss it. */
10518 else if (INTERACTIVE && sf->glyphs_initialized_p)
10520 /* Get the frame containing the mini-buffer
10521 that the selected frame is using. */
10522 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10523 Lisp_Object frame = XWINDOW (mini_window)->frame;
10524 struct frame *f = XFRAME (frame);
10526 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10527 Fmake_frame_visible (frame);
10529 if (STRINGP (m) && SCHARS (m) > 0)
10531 set_message (m);
10532 if (minibuffer_auto_raise)
10533 Fraise_frame (frame);
10534 /* Assume we are not echoing.
10535 (If we are, echo_now will override this.) */
10536 echo_message_buffer = Qnil;
10538 else
10539 clear_message (true, true);
10541 do_pending_window_change (false);
10542 echo_area_display (true);
10543 do_pending_window_change (false);
10544 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10545 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10550 /* Display a null-terminated echo area message M. If M is 0, clear
10551 out any existing message, and let the mini-buffer text show through.
10553 The buffer M must continue to exist until after the echo area gets
10554 cleared or some other message gets displayed there. Do not pass
10555 text that is stored in a Lisp string. Do not pass text in a buffer
10556 that was alloca'd. */
10558 void
10559 message1 (const char *m)
10561 message3 (m ? build_unibyte_string (m) : Qnil);
10565 /* The non-logging counterpart of message1. */
10567 void
10568 message1_nolog (const char *m)
10570 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10573 /* Display a message M which contains a single %s
10574 which gets replaced with STRING. */
10576 void
10577 message_with_string (const char *m, Lisp_Object string, bool log)
10579 CHECK_STRING (string);
10581 bool need_message;
10582 if (noninteractive)
10583 need_message = !!m;
10584 else if (!INTERACTIVE)
10585 need_message = false;
10586 else
10588 /* The frame whose minibuffer we're going to display the message on.
10589 It may be larger than the selected frame, so we need
10590 to use its buffer, not the selected frame's buffer. */
10591 Lisp_Object mini_window;
10592 struct frame *f, *sf = SELECTED_FRAME ();
10594 /* Get the frame containing the minibuffer
10595 that the selected frame is using. */
10596 mini_window = FRAME_MINIBUF_WINDOW (sf);
10597 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10599 /* Error messages get reported properly by cmd_error, so this must be
10600 just an informative message; if the frame hasn't really been
10601 initialized yet, just toss it. */
10602 need_message = f->glyphs_initialized_p;
10605 if (need_message)
10607 AUTO_STRING (fmt, m);
10608 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10610 if (noninteractive)
10611 message_to_stderr (msg);
10612 else
10614 if (log)
10615 message3 (msg);
10616 else
10617 message3_nolog (msg);
10619 /* Print should start at the beginning of the message
10620 buffer next time. */
10621 message_buf_print = false;
10627 /* Dump an informative message to the minibuf. If M is 0, clear out
10628 any existing message, and let the mini-buffer text show through.
10630 The message must be safe ASCII (because when Emacs is
10631 non-interactive the message is sent straight to stderr without
10632 encoding first) and the format must not contain ` or ' (because
10633 this function does not account for `text-quoting-style'). If your
10634 message and format do not fit into this category, convert your
10635 arguments to Lisp objects and use Fmessage instead. */
10637 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10638 vmessage (const char *m, va_list ap)
10640 if (noninteractive)
10642 if (m)
10644 if (noninteractive_need_newline)
10645 putc ('\n', stderr);
10646 noninteractive_need_newline = false;
10647 vfprintf (stderr, m, ap);
10648 if (!cursor_in_echo_area)
10649 fprintf (stderr, "\n");
10650 fflush (stderr);
10653 else if (INTERACTIVE)
10655 /* The frame whose mini-buffer we're going to display the message
10656 on. It may be larger than the selected frame, so we need to
10657 use its buffer, not the selected frame's buffer. */
10658 Lisp_Object mini_window;
10659 struct frame *f, *sf = SELECTED_FRAME ();
10661 /* Get the frame containing the mini-buffer
10662 that the selected frame is using. */
10663 mini_window = FRAME_MINIBUF_WINDOW (sf);
10664 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10666 /* Error messages get reported properly by cmd_error, so this must be
10667 just an informative message; if the frame hasn't really been
10668 initialized yet, just toss it. */
10669 if (f->glyphs_initialized_p)
10671 if (m)
10673 ptrdiff_t len;
10674 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10675 USE_SAFE_ALLOCA;
10676 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10678 len = doprnt (message_buf, maxsize, m, 0, ap);
10680 message3 (make_string (message_buf, len));
10681 SAFE_FREE ();
10683 else
10684 message1 (0);
10686 /* Print should start at the beginning of the message
10687 buffer next time. */
10688 message_buf_print = false;
10693 /* See vmessage for restrictions on the text of the message. */
10694 void
10695 message (const char *m, ...)
10697 va_list ap;
10698 va_start (ap, m);
10699 vmessage (m, ap);
10700 va_end (ap);
10704 /* Display the current message in the current mini-buffer. This is
10705 only called from error handlers in process.c, and is not time
10706 critical. */
10708 void
10709 update_echo_area (void)
10711 if (!NILP (echo_area_buffer[0]))
10713 Lisp_Object string;
10714 string = Fcurrent_message ();
10715 message3 (string);
10720 /* Make sure echo area buffers in `echo_buffers' are live.
10721 If they aren't, make new ones. */
10723 static void
10724 ensure_echo_area_buffers (void)
10726 for (int i = 0; i < 2; i++)
10727 if (!BUFFERP (echo_buffer[i])
10728 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10730 Lisp_Object old_buffer = echo_buffer[i];
10731 static char const name_fmt[] = " *Echo Area %d*";
10732 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10733 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10734 echo_buffer[i] = Fget_buffer_create (lname);
10735 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10736 /* to force word wrap in echo area -
10737 it was decided to postpone this*/
10738 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10740 for (int j = 0; j < 2; j++)
10741 if (EQ (old_buffer, echo_area_buffer[j]))
10742 echo_area_buffer[j] = echo_buffer[i];
10747 /* Call FN with args A1..A2 with either the current or last displayed
10748 echo_area_buffer as current buffer.
10750 WHICH zero means use the current message buffer
10751 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10752 from echo_buffer[] and clear it.
10754 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10755 suitable buffer from echo_buffer[] and clear it.
10757 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10758 that the current message becomes the last displayed one, choose a
10759 suitable buffer for echo_area_buffer[0], and clear it.
10761 Value is what FN returns. */
10763 static bool
10764 with_echo_area_buffer (struct window *w, int which,
10765 bool (*fn) (ptrdiff_t, Lisp_Object),
10766 ptrdiff_t a1, Lisp_Object a2)
10768 Lisp_Object buffer;
10769 bool this_one, the_other, clear_buffer_p, rc;
10770 ptrdiff_t count = SPECPDL_INDEX ();
10772 /* If buffers aren't live, make new ones. */
10773 ensure_echo_area_buffers ();
10775 clear_buffer_p = false;
10777 if (which == 0)
10778 this_one = false, the_other = true;
10779 else if (which > 0)
10780 this_one = true, the_other = false;
10781 else
10783 this_one = false, the_other = true;
10784 clear_buffer_p = true;
10786 /* We need a fresh one in case the current echo buffer equals
10787 the one containing the last displayed echo area message. */
10788 if (!NILP (echo_area_buffer[this_one])
10789 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10790 echo_area_buffer[this_one] = Qnil;
10793 /* Choose a suitable buffer from echo_buffer[] if we don't
10794 have one. */
10795 if (NILP (echo_area_buffer[this_one]))
10797 echo_area_buffer[this_one]
10798 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10799 ? echo_buffer[the_other]
10800 : echo_buffer[this_one]);
10801 clear_buffer_p = true;
10804 buffer = echo_area_buffer[this_one];
10806 /* Don't get confused by reusing the buffer used for echoing
10807 for a different purpose. */
10808 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10809 cancel_echoing ();
10811 record_unwind_protect (unwind_with_echo_area_buffer,
10812 with_echo_area_buffer_unwind_data (w));
10814 /* Make the echo area buffer current. Note that for display
10815 purposes, it is not necessary that the displayed window's buffer
10816 == current_buffer, except for text property lookup. So, let's
10817 only set that buffer temporarily here without doing a full
10818 Fset_window_buffer. We must also change w->pointm, though,
10819 because otherwise an assertions in unshow_buffer fails, and Emacs
10820 aborts. */
10821 set_buffer_internal_1 (XBUFFER (buffer));
10822 if (w)
10824 wset_buffer (w, buffer);
10825 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10826 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10829 bset_undo_list (current_buffer, Qt);
10830 bset_read_only (current_buffer, Qnil);
10831 specbind (Qinhibit_read_only, Qt);
10832 specbind (Qinhibit_modification_hooks, Qt);
10834 if (clear_buffer_p && Z > BEG)
10835 del_range (BEG, Z);
10837 eassert (BEGV >= BEG);
10838 eassert (ZV <= Z && ZV >= BEGV);
10840 rc = fn (a1, a2);
10842 eassert (BEGV >= BEG);
10843 eassert (ZV <= Z && ZV >= BEGV);
10845 unbind_to (count, Qnil);
10846 return rc;
10850 /* Save state that should be preserved around the call to the function
10851 FN called in with_echo_area_buffer. */
10853 static Lisp_Object
10854 with_echo_area_buffer_unwind_data (struct window *w)
10856 int i = 0;
10857 Lisp_Object vector, tmp;
10859 /* Reduce consing by keeping one vector in
10860 Vwith_echo_area_save_vector. */
10861 vector = Vwith_echo_area_save_vector;
10862 Vwith_echo_area_save_vector = Qnil;
10864 if (NILP (vector))
10865 vector = Fmake_vector (make_number (11), Qnil);
10867 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10868 ASET (vector, i, Vdeactivate_mark); ++i;
10869 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10871 if (w)
10873 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10874 ASET (vector, i, w->contents); ++i;
10875 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10876 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10877 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10878 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10879 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10880 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10882 else
10884 int end = i + 8;
10885 for (; i < end; ++i)
10886 ASET (vector, i, Qnil);
10889 eassert (i == ASIZE (vector));
10890 return vector;
10894 /* Restore global state from VECTOR which was created by
10895 with_echo_area_buffer_unwind_data. */
10897 static void
10898 unwind_with_echo_area_buffer (Lisp_Object vector)
10900 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10901 Vdeactivate_mark = AREF (vector, 1);
10902 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10904 if (WINDOWP (AREF (vector, 3)))
10906 struct window *w;
10907 Lisp_Object buffer;
10909 w = XWINDOW (AREF (vector, 3));
10910 buffer = AREF (vector, 4);
10912 wset_buffer (w, buffer);
10913 set_marker_both (w->pointm, buffer,
10914 XFASTINT (AREF (vector, 5)),
10915 XFASTINT (AREF (vector, 6)));
10916 set_marker_both (w->old_pointm, buffer,
10917 XFASTINT (AREF (vector, 7)),
10918 XFASTINT (AREF (vector, 8)));
10919 set_marker_both (w->start, buffer,
10920 XFASTINT (AREF (vector, 9)),
10921 XFASTINT (AREF (vector, 10)));
10924 Vwith_echo_area_save_vector = vector;
10928 /* Set up the echo area for use by print functions. MULTIBYTE_P
10929 means we will print multibyte. */
10931 void
10932 setup_echo_area_for_printing (bool multibyte_p)
10934 /* If we can't find an echo area any more, exit. */
10935 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10936 Fkill_emacs (Qnil);
10938 ensure_echo_area_buffers ();
10940 if (!message_buf_print)
10942 /* A message has been output since the last time we printed.
10943 Choose a fresh echo area buffer. */
10944 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10945 echo_area_buffer[0] = echo_buffer[1];
10946 else
10947 echo_area_buffer[0] = echo_buffer[0];
10949 /* Switch to that buffer and clear it. */
10950 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10951 bset_truncate_lines (current_buffer, Qnil);
10953 if (Z > BEG)
10955 ptrdiff_t count = SPECPDL_INDEX ();
10956 specbind (Qinhibit_read_only, Qt);
10957 /* Note that undo recording is always disabled. */
10958 del_range (BEG, Z);
10959 unbind_to (count, Qnil);
10961 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10963 /* Set up the buffer for the multibyteness we need. */
10964 if (multibyte_p
10965 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10966 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10968 /* Raise the frame containing the echo area. */
10969 if (minibuffer_auto_raise)
10971 struct frame *sf = SELECTED_FRAME ();
10972 Lisp_Object mini_window;
10973 mini_window = FRAME_MINIBUF_WINDOW (sf);
10974 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10977 message_log_maybe_newline ();
10978 message_buf_print = true;
10980 else
10982 if (NILP (echo_area_buffer[0]))
10984 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10985 echo_area_buffer[0] = echo_buffer[1];
10986 else
10987 echo_area_buffer[0] = echo_buffer[0];
10990 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10992 /* Someone switched buffers between print requests. */
10993 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10994 bset_truncate_lines (current_buffer, Qnil);
11000 /* Display an echo area message in window W. Value is true if W's
11001 height is changed. If display_last_displayed_message_p,
11002 display the message that was last displayed, otherwise
11003 display the current message. */
11005 static bool
11006 display_echo_area (struct window *w)
11008 bool no_message_p, window_height_changed_p;
11010 /* Temporarily disable garbage collections while displaying the echo
11011 area. This is done because a GC can print a message itself.
11012 That message would modify the echo area buffer's contents while a
11013 redisplay of the buffer is going on, and seriously confuse
11014 redisplay. */
11015 ptrdiff_t count = inhibit_garbage_collection ();
11017 /* If there is no message, we must call display_echo_area_1
11018 nevertheless because it resizes the window. But we will have to
11019 reset the echo_area_buffer in question to nil at the end because
11020 with_echo_area_buffer will sets it to an empty buffer. */
11021 bool i = display_last_displayed_message_p;
11022 /* According to the C99, C11 and C++11 standards, the integral value
11023 of a "bool" is always 0 or 1, so this array access is safe here,
11024 if oddly typed. */
11025 no_message_p = NILP (echo_area_buffer[i]);
11027 window_height_changed_p
11028 = with_echo_area_buffer (w, display_last_displayed_message_p,
11029 display_echo_area_1,
11030 (intptr_t) w, Qnil);
11032 if (no_message_p)
11033 echo_area_buffer[i] = Qnil;
11035 unbind_to (count, Qnil);
11036 return window_height_changed_p;
11040 /* Helper for display_echo_area. Display the current buffer which
11041 contains the current echo area message in window W, a mini-window,
11042 a pointer to which is passed in A1. A2..A4 are currently not used.
11043 Change the height of W so that all of the message is displayed.
11044 Value is true if height of W was changed. */
11046 static bool
11047 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11049 intptr_t i1 = a1;
11050 struct window *w = (struct window *) i1;
11051 Lisp_Object window;
11052 struct text_pos start;
11054 /* We are about to enter redisplay without going through
11055 redisplay_internal, so we need to forget these faces by hand
11056 here. */
11057 forget_escape_and_glyphless_faces ();
11059 /* Do this before displaying, so that we have a large enough glyph
11060 matrix for the display. If we can't get enough space for the
11061 whole text, display the last N lines. That works by setting w->start. */
11062 bool window_height_changed_p = resize_mini_window (w, false);
11064 /* Use the starting position chosen by resize_mini_window. */
11065 SET_TEXT_POS_FROM_MARKER (start, w->start);
11067 /* Display. */
11068 clear_glyph_matrix (w->desired_matrix);
11069 XSETWINDOW (window, w);
11070 try_window (window, start, 0);
11072 return window_height_changed_p;
11076 /* Resize the echo area window to exactly the size needed for the
11077 currently displayed message, if there is one. If a mini-buffer
11078 is active, don't shrink it. */
11080 void
11081 resize_echo_area_exactly (void)
11083 if (BUFFERP (echo_area_buffer[0])
11084 && WINDOWP (echo_area_window))
11086 struct window *w = XWINDOW (echo_area_window);
11087 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11088 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11089 (intptr_t) w, resize_exactly);
11090 if (resized_p)
11092 windows_or_buffers_changed = 42;
11093 update_mode_lines = 30;
11094 redisplay_internal ();
11100 /* Callback function for with_echo_area_buffer, when used from
11101 resize_echo_area_exactly. A1 contains a pointer to the window to
11102 resize, EXACTLY non-nil means resize the mini-window exactly to the
11103 size of the text displayed. A3 and A4 are not used. Value is what
11104 resize_mini_window returns. */
11106 static bool
11107 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11109 intptr_t i1 = a1;
11110 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11114 /* Resize mini-window W to fit the size of its contents. EXACT_P
11115 means size the window exactly to the size needed. Otherwise, it's
11116 only enlarged until W's buffer is empty.
11118 Set W->start to the right place to begin display. If the whole
11119 contents fit, start at the beginning. Otherwise, start so as
11120 to make the end of the contents appear. This is particularly
11121 important for y-or-n-p, but seems desirable generally.
11123 Value is true if the window height has been changed. */
11125 bool
11126 resize_mini_window (struct window *w, bool exact_p)
11128 struct frame *f = XFRAME (w->frame);
11129 bool window_height_changed_p = false;
11131 eassert (MINI_WINDOW_P (w));
11133 /* By default, start display at the beginning. */
11134 set_marker_both (w->start, w->contents,
11135 BUF_BEGV (XBUFFER (w->contents)),
11136 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11138 /* Don't resize windows while redisplaying a window; it would
11139 confuse redisplay functions when the size of the window they are
11140 displaying changes from under them. Such a resizing can happen,
11141 for instance, when which-func prints a long message while
11142 we are running fontification-functions. We're running these
11143 functions with safe_call which binds inhibit-redisplay to t. */
11144 if (!NILP (Vinhibit_redisplay))
11145 return false;
11147 /* Nil means don't try to resize. */
11148 if (NILP (Vresize_mini_windows)
11149 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11150 return false;
11152 if (!FRAME_MINIBUF_ONLY_P (f))
11154 struct it it;
11155 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11156 + WINDOW_PIXEL_HEIGHT (w));
11157 int unit = FRAME_LINE_HEIGHT (f);
11158 int height, max_height;
11159 struct text_pos start;
11160 struct buffer *old_current_buffer = NULL;
11162 if (current_buffer != XBUFFER (w->contents))
11164 old_current_buffer = current_buffer;
11165 set_buffer_internal (XBUFFER (w->contents));
11168 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11170 /* Compute the max. number of lines specified by the user. */
11171 if (FLOATP (Vmax_mini_window_height))
11172 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11173 else if (INTEGERP (Vmax_mini_window_height))
11174 max_height = XINT (Vmax_mini_window_height) * unit;
11175 else
11176 max_height = total_height / 4;
11178 /* Correct that max. height if it's bogus. */
11179 max_height = clip_to_bounds (unit, max_height, total_height);
11181 /* Find out the height of the text in the window. */
11182 if (it.line_wrap == TRUNCATE)
11183 height = unit;
11184 else
11186 last_height = 0;
11187 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11188 if (it.max_ascent == 0 && it.max_descent == 0)
11189 height = it.current_y + last_height;
11190 else
11191 height = it.current_y + it.max_ascent + it.max_descent;
11192 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11195 /* Compute a suitable window start. */
11196 if (height > max_height)
11198 height = (max_height / unit) * unit;
11199 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11200 move_it_vertically_backward (&it, height - unit);
11201 start = it.current.pos;
11203 else
11204 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11205 SET_MARKER_FROM_TEXT_POS (w->start, start);
11207 if (EQ (Vresize_mini_windows, Qgrow_only))
11209 /* Let it grow only, until we display an empty message, in which
11210 case the window shrinks again. */
11211 if (height > WINDOW_PIXEL_HEIGHT (w))
11213 int old_height = WINDOW_PIXEL_HEIGHT (w);
11215 FRAME_WINDOWS_FROZEN (f) = true;
11216 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11217 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11219 else if (height < WINDOW_PIXEL_HEIGHT (w)
11220 && (exact_p || BEGV == ZV))
11222 int old_height = WINDOW_PIXEL_HEIGHT (w);
11224 FRAME_WINDOWS_FROZEN (f) = false;
11225 shrink_mini_window (w, true);
11226 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11229 else
11231 /* Always resize to exact size needed. */
11232 if (height > WINDOW_PIXEL_HEIGHT (w))
11234 int old_height = WINDOW_PIXEL_HEIGHT (w);
11236 FRAME_WINDOWS_FROZEN (f) = true;
11237 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11238 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11240 else if (height < WINDOW_PIXEL_HEIGHT (w))
11242 int old_height = WINDOW_PIXEL_HEIGHT (w);
11244 FRAME_WINDOWS_FROZEN (f) = false;
11245 shrink_mini_window (w, true);
11247 if (height)
11249 FRAME_WINDOWS_FROZEN (f) = true;
11250 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11253 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11257 if (old_current_buffer)
11258 set_buffer_internal (old_current_buffer);
11261 return window_height_changed_p;
11265 /* Value is the current message, a string, or nil if there is no
11266 current message. */
11268 Lisp_Object
11269 current_message (void)
11271 Lisp_Object msg;
11273 if (!BUFFERP (echo_area_buffer[0]))
11274 msg = Qnil;
11275 else
11277 with_echo_area_buffer (0, 0, current_message_1,
11278 (intptr_t) &msg, Qnil);
11279 if (NILP (msg))
11280 echo_area_buffer[0] = Qnil;
11283 return msg;
11287 static bool
11288 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11290 intptr_t i1 = a1;
11291 Lisp_Object *msg = (Lisp_Object *) i1;
11293 if (Z > BEG)
11294 *msg = make_buffer_string (BEG, Z, true);
11295 else
11296 *msg = Qnil;
11297 return false;
11301 /* Push the current message on Vmessage_stack for later restoration
11302 by restore_message. Value is true if the current message isn't
11303 empty. This is a relatively infrequent operation, so it's not
11304 worth optimizing. */
11306 bool
11307 push_message (void)
11309 Lisp_Object msg = current_message ();
11310 Vmessage_stack = Fcons (msg, Vmessage_stack);
11311 return STRINGP (msg);
11315 /* Restore message display from the top of Vmessage_stack. */
11317 void
11318 restore_message (void)
11320 eassert (CONSP (Vmessage_stack));
11321 message3_nolog (XCAR (Vmessage_stack));
11325 /* Handler for unwind-protect calling pop_message. */
11327 void
11328 pop_message_unwind (void)
11330 /* Pop the top-most entry off Vmessage_stack. */
11331 eassert (CONSP (Vmessage_stack));
11332 Vmessage_stack = XCDR (Vmessage_stack);
11336 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11337 exits. If the stack is not empty, we have a missing pop_message
11338 somewhere. */
11340 void
11341 check_message_stack (void)
11343 if (!NILP (Vmessage_stack))
11344 emacs_abort ();
11348 /* Truncate to NCHARS what will be displayed in the echo area the next
11349 time we display it---but don't redisplay it now. */
11351 void
11352 truncate_echo_area (ptrdiff_t nchars)
11354 if (nchars == 0)
11355 echo_area_buffer[0] = Qnil;
11356 else if (!noninteractive
11357 && INTERACTIVE
11358 && !NILP (echo_area_buffer[0]))
11360 struct frame *sf = SELECTED_FRAME ();
11361 /* Error messages get reported properly by cmd_error, so this must be
11362 just an informative message; if the frame hasn't really been
11363 initialized yet, just toss it. */
11364 if (sf->glyphs_initialized_p)
11365 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11370 /* Helper function for truncate_echo_area. Truncate the current
11371 message to at most NCHARS characters. */
11373 static bool
11374 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11376 if (BEG + nchars < Z)
11377 del_range (BEG + nchars, Z);
11378 if (Z == BEG)
11379 echo_area_buffer[0] = Qnil;
11380 return false;
11383 /* Set the current message to STRING. */
11385 static void
11386 set_message (Lisp_Object string)
11388 eassert (STRINGP (string));
11390 message_enable_multibyte = STRING_MULTIBYTE (string);
11392 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11393 message_buf_print = false;
11394 help_echo_showing_p = false;
11396 if (STRINGP (Vdebug_on_message)
11397 && STRINGP (string)
11398 && fast_string_match (Vdebug_on_message, string) >= 0)
11399 call_debugger (list2 (Qerror, string));
11403 /* Helper function for set_message. First argument is ignored and second
11404 argument has the same meaning as for set_message.
11405 This function is called with the echo area buffer being current. */
11407 static bool
11408 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11410 eassert (STRINGP (string));
11412 /* Change multibyteness of the echo buffer appropriately. */
11413 if (message_enable_multibyte
11414 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11415 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11417 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11418 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11419 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11421 /* Insert new message at BEG. */
11422 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11424 /* This function takes care of single/multibyte conversion.
11425 We just have to ensure that the echo area buffer has the right
11426 setting of enable_multibyte_characters. */
11427 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11429 return false;
11433 /* Clear messages. CURRENT_P means clear the current message.
11434 LAST_DISPLAYED_P means clear the message last displayed. */
11436 void
11437 clear_message (bool current_p, bool last_displayed_p)
11439 if (current_p)
11441 echo_area_buffer[0] = Qnil;
11442 message_cleared_p = true;
11445 if (last_displayed_p)
11446 echo_area_buffer[1] = Qnil;
11448 message_buf_print = false;
11451 /* Clear garbaged frames.
11453 This function is used where the old redisplay called
11454 redraw_garbaged_frames which in turn called redraw_frame which in
11455 turn called clear_frame. The call to clear_frame was a source of
11456 flickering. I believe a clear_frame is not necessary. It should
11457 suffice in the new redisplay to invalidate all current matrices,
11458 and ensure a complete redisplay of all windows. */
11460 static void
11461 clear_garbaged_frames (void)
11463 if (frame_garbaged)
11465 Lisp_Object tail, frame;
11466 struct frame *sf = SELECTED_FRAME ();
11468 FOR_EACH_FRAME (tail, frame)
11470 struct frame *f = XFRAME (frame);
11472 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11474 if (f->resized_p
11475 /* It makes no sense to redraw a non-selected TTY
11476 frame, since that will actually clear the
11477 selected frame, and might leave the selected
11478 frame with corrupted display, if it happens not
11479 to be marked garbaged. */
11480 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11481 redraw_frame (f);
11482 else
11483 clear_current_matrices (f);
11485 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11486 x_clear_under_internal_border (f);
11487 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11489 fset_redisplay (f);
11490 f->garbaged = false;
11491 f->resized_p = false;
11495 frame_garbaged = false;
11500 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11501 selected_frame. */
11503 static void
11504 echo_area_display (bool update_frame_p)
11506 Lisp_Object mini_window;
11507 struct window *w;
11508 struct frame *f;
11509 bool window_height_changed_p = false;
11510 struct frame *sf = SELECTED_FRAME ();
11512 mini_window = FRAME_MINIBUF_WINDOW (sf);
11513 w = XWINDOW (mini_window);
11514 f = XFRAME (WINDOW_FRAME (w));
11516 /* Don't display if frame is invisible or not yet initialized. */
11517 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11518 return;
11520 #ifdef HAVE_WINDOW_SYSTEM
11521 /* When Emacs starts, selected_frame may be the initial terminal
11522 frame. If we let this through, a message would be displayed on
11523 the terminal. */
11524 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11525 return;
11526 #endif /* HAVE_WINDOW_SYSTEM */
11528 /* Redraw garbaged frames. */
11529 clear_garbaged_frames ();
11531 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11533 echo_area_window = mini_window;
11534 window_height_changed_p = display_echo_area (w);
11535 w->must_be_updated_p = true;
11537 /* Update the display, unless called from redisplay_internal.
11538 Also don't update the screen during redisplay itself. The
11539 update will happen at the end of redisplay, and an update
11540 here could cause confusion. */
11541 if (update_frame_p && !redisplaying_p)
11543 int n = 0;
11545 /* If the display update has been interrupted by pending
11546 input, update mode lines in the frame. Due to the
11547 pending input, it might have been that redisplay hasn't
11548 been called, so that mode lines above the echo area are
11549 garbaged. This looks odd, so we prevent it here. */
11550 if (!display_completed)
11552 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11554 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11555 x_clear_under_internal_border (f);
11556 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11560 if (window_height_changed_p
11561 /* Don't do this if Emacs is shutting down. Redisplay
11562 needs to run hooks. */
11563 && !NILP (Vrun_hooks))
11565 /* Must update other windows. Likewise as in other
11566 cases, don't let this update be interrupted by
11567 pending input. */
11568 ptrdiff_t count = SPECPDL_INDEX ();
11569 specbind (Qredisplay_dont_pause, Qt);
11570 fset_redisplay (f);
11571 redisplay_internal ();
11572 unbind_to (count, Qnil);
11574 else if (FRAME_WINDOW_P (f) && n == 0)
11576 /* Window configuration is the same as before.
11577 Can do with a display update of the echo area,
11578 unless we displayed some mode lines. */
11579 update_single_window (w);
11580 flush_frame (f);
11582 else
11583 update_frame (f, true, true);
11585 /* If cursor is in the echo area, make sure that the next
11586 redisplay displays the minibuffer, so that the cursor will
11587 be replaced with what the minibuffer wants. */
11588 if (cursor_in_echo_area)
11589 wset_redisplay (XWINDOW (mini_window));
11592 else if (!EQ (mini_window, selected_window))
11593 wset_redisplay (XWINDOW (mini_window));
11595 /* Last displayed message is now the current message. */
11596 echo_area_buffer[1] = echo_area_buffer[0];
11597 /* Inform read_char that we're not echoing. */
11598 echo_message_buffer = Qnil;
11600 /* Prevent redisplay optimization in redisplay_internal by resetting
11601 this_line_start_pos. This is done because the mini-buffer now
11602 displays the message instead of its buffer text. */
11603 if (EQ (mini_window, selected_window))
11604 CHARPOS (this_line_start_pos) = 0;
11606 if (window_height_changed_p)
11608 fset_redisplay (f);
11610 /* If window configuration was changed, frames may have been
11611 marked garbaged. Clear them or we will experience
11612 surprises wrt scrolling.
11613 FIXME: How/why/when? */
11614 clear_garbaged_frames ();
11618 /* True if W's buffer was changed but not saved. */
11620 static bool
11621 window_buffer_changed (struct window *w)
11623 struct buffer *b = XBUFFER (w->contents);
11625 eassert (BUFFER_LIVE_P (b));
11627 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11630 /* True if W has %c or %C in its mode line and mode line should be updated. */
11632 static bool
11633 mode_line_update_needed (struct window *w)
11635 return (w->column_number_displayed != -1
11636 && !(PT == w->last_point && !window_outdated (w))
11637 && (w->column_number_displayed != current_column ()));
11640 /* True if window start of W is frozen and may not be changed during
11641 redisplay. */
11643 static bool
11644 window_frozen_p (struct window *w)
11646 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11648 Lisp_Object window;
11650 XSETWINDOW (window, w);
11651 if (MINI_WINDOW_P (w))
11652 return false;
11653 else if (EQ (window, selected_window))
11654 return false;
11655 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11656 && EQ (window, Vminibuf_scroll_window))
11657 /* This special window can't be frozen too. */
11658 return false;
11659 else
11660 return true;
11662 return false;
11665 /***********************************************************************
11666 Mode Lines and Frame Titles
11667 ***********************************************************************/
11669 /* A buffer for constructing non-propertized mode-line strings and
11670 frame titles in it; allocated from the heap in init_xdisp and
11671 resized as needed in store_mode_line_noprop_char. */
11673 static char *mode_line_noprop_buf;
11675 /* The buffer's end, and a current output position in it. */
11677 static char *mode_line_noprop_buf_end;
11678 static char *mode_line_noprop_ptr;
11680 #define MODE_LINE_NOPROP_LEN(start) \
11681 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11683 static enum {
11684 MODE_LINE_DISPLAY = 0,
11685 MODE_LINE_TITLE,
11686 MODE_LINE_NOPROP,
11687 MODE_LINE_STRING
11688 } mode_line_target;
11690 /* Alist that caches the results of :propertize.
11691 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11692 static Lisp_Object mode_line_proptrans_alist;
11694 /* List of strings making up the mode-line. */
11695 static Lisp_Object mode_line_string_list;
11697 /* Base face property when building propertized mode line string. */
11698 static Lisp_Object mode_line_string_face;
11699 static Lisp_Object mode_line_string_face_prop;
11702 /* Unwind data for mode line strings */
11704 static Lisp_Object Vmode_line_unwind_vector;
11706 static Lisp_Object
11707 format_mode_line_unwind_data (struct frame *target_frame,
11708 struct buffer *obuf,
11709 Lisp_Object owin,
11710 bool save_proptrans)
11712 Lisp_Object vector, tmp;
11714 /* Reduce consing by keeping one vector in
11715 Vwith_echo_area_save_vector. */
11716 vector = Vmode_line_unwind_vector;
11717 Vmode_line_unwind_vector = Qnil;
11719 if (NILP (vector))
11720 vector = Fmake_vector (make_number (10), Qnil);
11722 ASET (vector, 0, make_number (mode_line_target));
11723 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11724 ASET (vector, 2, mode_line_string_list);
11725 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11726 ASET (vector, 4, mode_line_string_face);
11727 ASET (vector, 5, mode_line_string_face_prop);
11729 if (obuf)
11730 XSETBUFFER (tmp, obuf);
11731 else
11732 tmp = Qnil;
11733 ASET (vector, 6, tmp);
11734 ASET (vector, 7, owin);
11735 if (target_frame)
11737 /* Similarly to `with-selected-window', if the operation selects
11738 a window on another frame, we must restore that frame's
11739 selected window, and (for a tty) the top-frame. */
11740 ASET (vector, 8, target_frame->selected_window);
11741 if (FRAME_TERMCAP_P (target_frame))
11742 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11745 return vector;
11748 static void
11749 unwind_format_mode_line (Lisp_Object vector)
11751 Lisp_Object old_window = AREF (vector, 7);
11752 Lisp_Object target_frame_window = AREF (vector, 8);
11753 Lisp_Object old_top_frame = AREF (vector, 9);
11755 mode_line_target = XINT (AREF (vector, 0));
11756 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11757 mode_line_string_list = AREF (vector, 2);
11758 if (! EQ (AREF (vector, 3), Qt))
11759 mode_line_proptrans_alist = AREF (vector, 3);
11760 mode_line_string_face = AREF (vector, 4);
11761 mode_line_string_face_prop = AREF (vector, 5);
11763 /* Select window before buffer, since it may change the buffer. */
11764 if (!NILP (old_window))
11766 /* If the operation that we are unwinding had selected a window
11767 on a different frame, reset its frame-selected-window. For a
11768 text terminal, reset its top-frame if necessary. */
11769 if (!NILP (target_frame_window))
11771 Lisp_Object frame
11772 = WINDOW_FRAME (XWINDOW (target_frame_window));
11774 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11775 Fselect_window (target_frame_window, Qt);
11777 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11778 Fselect_frame (old_top_frame, Qt);
11781 Fselect_window (old_window, Qt);
11784 if (!NILP (AREF (vector, 6)))
11786 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11787 ASET (vector, 6, Qnil);
11790 Vmode_line_unwind_vector = vector;
11794 /* Store a single character C for the frame title in mode_line_noprop_buf.
11795 Re-allocate mode_line_noprop_buf if necessary. */
11797 static void
11798 store_mode_line_noprop_char (char c)
11800 /* If output position has reached the end of the allocated buffer,
11801 increase the buffer's size. */
11802 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11804 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11805 ptrdiff_t size = len;
11806 mode_line_noprop_buf =
11807 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11808 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11809 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11812 *mode_line_noprop_ptr++ = c;
11816 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11817 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11818 characters that yield more columns than PRECISION; PRECISION <= 0
11819 means copy the whole string. Pad with spaces until FIELD_WIDTH
11820 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11821 pad. Called from display_mode_element when it is used to build a
11822 frame title. */
11824 static int
11825 store_mode_line_noprop (const char *string, int field_width, int precision)
11827 const unsigned char *str = (const unsigned char *) string;
11828 int n = 0;
11829 ptrdiff_t dummy, nbytes;
11831 /* Copy at most PRECISION chars from STR. */
11832 nbytes = strlen (string);
11833 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11834 while (nbytes--)
11835 store_mode_line_noprop_char (*str++);
11837 /* Fill up with spaces until FIELD_WIDTH reached. */
11838 while (field_width > 0
11839 && n < field_width)
11841 store_mode_line_noprop_char (' ');
11842 ++n;
11845 return n;
11848 /***********************************************************************
11849 Frame Titles
11850 ***********************************************************************/
11852 #ifdef HAVE_WINDOW_SYSTEM
11854 /* Set the title of FRAME, if it has changed. The title format is
11855 Vicon_title_format if FRAME is iconified, otherwise it is
11856 frame_title_format. */
11858 static void
11859 x_consider_frame_title (Lisp_Object frame)
11861 struct frame *f = XFRAME (frame);
11863 if ((FRAME_WINDOW_P (f)
11864 || FRAME_MINIBUF_ONLY_P (f)
11865 || f->explicit_name)
11866 && NILP (Fframe_parameter (frame, Qtooltip)))
11868 /* Do we have more than one visible frame on this X display? */
11869 Lisp_Object tail, other_frame, fmt;
11870 ptrdiff_t title_start;
11871 char *title;
11872 ptrdiff_t len;
11873 struct it it;
11874 ptrdiff_t count = SPECPDL_INDEX ();
11876 FOR_EACH_FRAME (tail, other_frame)
11878 struct frame *tf = XFRAME (other_frame);
11880 if (tf != f
11881 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11882 && !FRAME_MINIBUF_ONLY_P (tf)
11883 && !EQ (other_frame, tip_frame)
11884 && !FRAME_PARENT_FRAME (tf)
11885 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11886 break;
11889 /* Set global variable indicating that multiple frames exist. */
11890 multiple_frames = CONSP (tail);
11892 /* Switch to the buffer of selected window of the frame. Set up
11893 mode_line_target so that display_mode_element will output into
11894 mode_line_noprop_buf; then display the title. */
11895 record_unwind_protect (unwind_format_mode_line,
11896 format_mode_line_unwind_data
11897 (f, current_buffer, selected_window, false));
11898 /* select-frame calls resize_mini_window, which could resize the
11899 mini-window and by that undo the effect of this redisplay
11900 cycle wrt minibuffer and echo-area display. Binding
11901 inhibit-redisplay to t makes the call to resize_mini_window a
11902 no-op, thus avoiding the adverse side effects. */
11903 specbind (Qinhibit_redisplay, Qt);
11905 Fselect_window (f->selected_window, Qt);
11906 set_buffer_internal_1
11907 (XBUFFER (XWINDOW (f->selected_window)->contents));
11908 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11910 mode_line_target = MODE_LINE_TITLE;
11911 title_start = MODE_LINE_NOPROP_LEN (0);
11912 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11913 NULL, DEFAULT_FACE_ID);
11914 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11915 len = MODE_LINE_NOPROP_LEN (title_start);
11916 title = mode_line_noprop_buf + title_start;
11917 unbind_to (count, Qnil);
11919 /* Set the title only if it's changed. This avoids consing in
11920 the common case where it hasn't. (If it turns out that we've
11921 already wasted too much time by walking through the list with
11922 display_mode_element, then we might need to optimize at a
11923 higher level than this.) */
11924 if (! STRINGP (f->name)
11925 || SBYTES (f->name) != len
11926 || memcmp (title, SDATA (f->name), len) != 0)
11927 x_implicitly_set_name (f, make_string (title, len), Qnil);
11931 #endif /* not HAVE_WINDOW_SYSTEM */
11934 /***********************************************************************
11935 Menu Bars
11936 ***********************************************************************/
11938 /* True if we will not redisplay all visible windows. */
11939 #define REDISPLAY_SOME_P() \
11940 ((windows_or_buffers_changed == 0 \
11941 || windows_or_buffers_changed == REDISPLAY_SOME) \
11942 && (update_mode_lines == 0 \
11943 || update_mode_lines == REDISPLAY_SOME))
11945 /* Prepare for redisplay by updating menu-bar item lists when
11946 appropriate. This can call eval. */
11948 static void
11949 prepare_menu_bars (void)
11951 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11952 bool some_windows = REDISPLAY_SOME_P ();
11953 Lisp_Object tooltip_frame;
11955 #ifdef HAVE_WINDOW_SYSTEM
11956 tooltip_frame = tip_frame;
11957 #else
11958 tooltip_frame = Qnil;
11959 #endif
11961 if (FUNCTIONP (Vpre_redisplay_function))
11963 Lisp_Object windows = all_windows ? Qt : Qnil;
11964 if (all_windows && some_windows)
11966 Lisp_Object ws = window_list ();
11967 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11969 Lisp_Object this = XCAR (ws);
11970 struct window *w = XWINDOW (this);
11971 if (w->redisplay
11972 || XFRAME (w->frame)->redisplay
11973 || XBUFFER (w->contents)->text->redisplay)
11975 windows = Fcons (this, windows);
11979 safe__call1 (true, Vpre_redisplay_function, windows);
11982 /* Update all frame titles based on their buffer names, etc. We do
11983 this before the menu bars so that the buffer-menu will show the
11984 up-to-date frame titles. */
11985 #ifdef HAVE_WINDOW_SYSTEM
11986 if (all_windows)
11988 Lisp_Object tail, frame;
11990 FOR_EACH_FRAME (tail, frame)
11992 struct frame *f = XFRAME (frame);
11993 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11994 if (some_windows
11995 && !f->redisplay
11996 && !w->redisplay
11997 && !XBUFFER (w->contents)->text->redisplay)
11998 continue;
12000 if (!EQ (frame, tooltip_frame)
12001 && !FRAME_PARENT_FRAME (f)
12002 && (FRAME_ICONIFIED_P (f)
12003 || FRAME_VISIBLE_P (f) == 1
12004 /* Exclude TTY frames that are obscured because they
12005 are not the top frame on their console. This is
12006 because x_consider_frame_title actually switches
12007 to the frame, which for TTY frames means it is
12008 marked as garbaged, and will be completely
12009 redrawn on the next redisplay cycle. This causes
12010 TTY frames to be completely redrawn, when there
12011 are more than one of them, even though nothing
12012 should be changed on display. */
12013 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12014 x_consider_frame_title (frame);
12017 #endif /* HAVE_WINDOW_SYSTEM */
12019 /* Update the menu bar item lists, if appropriate. This has to be
12020 done before any actual redisplay or generation of display lines. */
12022 if (all_windows)
12024 Lisp_Object tail, frame;
12025 ptrdiff_t count = SPECPDL_INDEX ();
12026 /* True means that update_menu_bar has run its hooks
12027 so any further calls to update_menu_bar shouldn't do so again. */
12028 bool menu_bar_hooks_run = false;
12030 record_unwind_save_match_data ();
12032 FOR_EACH_FRAME (tail, frame)
12034 struct frame *f = XFRAME (frame);
12035 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12037 /* Ignore tooltip frame. */
12038 if (EQ (frame, tooltip_frame))
12039 continue;
12041 if (some_windows
12042 && !f->redisplay
12043 && !w->redisplay
12044 && !XBUFFER (w->contents)->text->redisplay)
12045 continue;
12047 run_window_size_change_functions (frame);
12049 if (FRAME_PARENT_FRAME (f))
12050 continue;
12052 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12053 #ifdef HAVE_WINDOW_SYSTEM
12054 update_tool_bar (f, false);
12055 #endif
12058 unbind_to (count, Qnil);
12060 else
12062 struct frame *sf = SELECTED_FRAME ();
12063 update_menu_bar (sf, true, false);
12064 #ifdef HAVE_WINDOW_SYSTEM
12065 update_tool_bar (sf, true);
12066 #endif
12071 /* Update the menu bar item list for frame F. This has to be done
12072 before we start to fill in any display lines, because it can call
12073 eval.
12075 If SAVE_MATCH_DATA, we must save and restore it here.
12077 If HOOKS_RUN, a previous call to update_menu_bar
12078 already ran the menu bar hooks for this redisplay, so there
12079 is no need to run them again. The return value is the
12080 updated value of this flag, to pass to the next call. */
12082 static bool
12083 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12085 Lisp_Object window;
12086 struct window *w;
12088 /* If called recursively during a menu update, do nothing. This can
12089 happen when, for instance, an activate-menubar-hook causes a
12090 redisplay. */
12091 if (inhibit_menubar_update)
12092 return hooks_run;
12094 window = FRAME_SELECTED_WINDOW (f);
12095 w = XWINDOW (window);
12097 if (FRAME_WINDOW_P (f)
12099 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12100 || defined (HAVE_NS) || defined (USE_GTK)
12101 FRAME_EXTERNAL_MENU_BAR (f)
12102 #else
12103 FRAME_MENU_BAR_LINES (f) > 0
12104 #endif
12105 : FRAME_MENU_BAR_LINES (f) > 0)
12107 /* If the user has switched buffers or windows, we need to
12108 recompute to reflect the new bindings. But we'll
12109 recompute when update_mode_lines is set too; that means
12110 that people can use force-mode-line-update to request
12111 that the menu bar be recomputed. The adverse effect on
12112 the rest of the redisplay algorithm is about the same as
12113 windows_or_buffers_changed anyway. */
12114 if (windows_or_buffers_changed
12115 /* This used to test w->update_mode_line, but we believe
12116 there is no need to recompute the menu in that case. */
12117 || update_mode_lines
12118 || window_buffer_changed (w))
12120 struct buffer *prev = current_buffer;
12121 ptrdiff_t count = SPECPDL_INDEX ();
12123 specbind (Qinhibit_menubar_update, Qt);
12125 set_buffer_internal_1 (XBUFFER (w->contents));
12126 if (save_match_data)
12127 record_unwind_save_match_data ();
12128 if (NILP (Voverriding_local_map_menu_flag))
12130 specbind (Qoverriding_terminal_local_map, Qnil);
12131 specbind (Qoverriding_local_map, Qnil);
12134 if (!hooks_run)
12136 /* Run the Lucid hook. */
12137 safe_run_hooks (Qactivate_menubar_hook);
12139 /* If it has changed current-menubar from previous value,
12140 really recompute the menu-bar from the value. */
12141 if (! NILP (Vlucid_menu_bar_dirty_flag))
12142 call0 (Qrecompute_lucid_menubar);
12144 safe_run_hooks (Qmenu_bar_update_hook);
12146 hooks_run = true;
12149 XSETFRAME (Vmenu_updating_frame, f);
12150 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12152 /* Redisplay the menu bar in case we changed it. */
12153 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12154 || defined (HAVE_NS) || defined (USE_GTK)
12155 if (FRAME_WINDOW_P (f))
12157 #if defined (HAVE_NS)
12158 /* All frames on Mac OS share the same menubar. So only
12159 the selected frame should be allowed to set it. */
12160 if (f == SELECTED_FRAME ())
12161 #endif
12162 set_frame_menubar (f, false, false);
12164 else
12165 /* On a terminal screen, the menu bar is an ordinary screen
12166 line, and this makes it get updated. */
12167 w->update_mode_line = true;
12168 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12169 /* In the non-toolkit version, the menu bar is an ordinary screen
12170 line, and this makes it get updated. */
12171 w->update_mode_line = true;
12172 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12174 unbind_to (count, Qnil);
12175 set_buffer_internal_1 (prev);
12179 return hooks_run;
12182 /***********************************************************************
12183 Tool-bars
12184 ***********************************************************************/
12186 #ifdef HAVE_WINDOW_SYSTEM
12188 /* Select `frame' temporarily without running all the code in
12189 do_switch_frame.
12190 FIXME: Maybe do_switch_frame should be trimmed down similarly
12191 when `norecord' is set. */
12192 static void
12193 fast_set_selected_frame (Lisp_Object frame)
12195 if (!EQ (selected_frame, frame))
12197 selected_frame = frame;
12198 selected_window = XFRAME (frame)->selected_window;
12202 /* Update the tool-bar item list for frame F. This has to be done
12203 before we start to fill in any display lines. Called from
12204 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12205 and restore it here. */
12207 static void
12208 update_tool_bar (struct frame *f, bool save_match_data)
12210 #if defined (USE_GTK) || defined (HAVE_NS)
12211 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12212 #else
12213 bool do_update = (WINDOWP (f->tool_bar_window)
12214 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12215 #endif
12217 if (do_update)
12219 Lisp_Object window;
12220 struct window *w;
12222 window = FRAME_SELECTED_WINDOW (f);
12223 w = XWINDOW (window);
12225 /* If the user has switched buffers or windows, we need to
12226 recompute to reflect the new bindings. But we'll
12227 recompute when update_mode_lines is set too; that means
12228 that people can use force-mode-line-update to request
12229 that the menu bar be recomputed. The adverse effect on
12230 the rest of the redisplay algorithm is about the same as
12231 windows_or_buffers_changed anyway. */
12232 if (windows_or_buffers_changed
12233 || w->update_mode_line
12234 || update_mode_lines
12235 || window_buffer_changed (w))
12237 struct buffer *prev = current_buffer;
12238 ptrdiff_t count = SPECPDL_INDEX ();
12239 Lisp_Object frame, new_tool_bar;
12240 int new_n_tool_bar;
12242 /* Set current_buffer to the buffer of the selected
12243 window of the frame, so that we get the right local
12244 keymaps. */
12245 set_buffer_internal_1 (XBUFFER (w->contents));
12247 /* Save match data, if we must. */
12248 if (save_match_data)
12249 record_unwind_save_match_data ();
12251 /* Make sure that we don't accidentally use bogus keymaps. */
12252 if (NILP (Voverriding_local_map_menu_flag))
12254 specbind (Qoverriding_terminal_local_map, Qnil);
12255 specbind (Qoverriding_local_map, Qnil);
12258 /* We must temporarily set the selected frame to this frame
12259 before calling tool_bar_items, because the calculation of
12260 the tool-bar keymap uses the selected frame (see
12261 `tool-bar-make-keymap' in tool-bar.el). */
12262 eassert (EQ (selected_window,
12263 /* Since we only explicitly preserve selected_frame,
12264 check that selected_window would be redundant. */
12265 XFRAME (selected_frame)->selected_window));
12266 record_unwind_protect (fast_set_selected_frame, selected_frame);
12267 XSETFRAME (frame, f);
12268 fast_set_selected_frame (frame);
12270 /* Build desired tool-bar items from keymaps. */
12271 new_tool_bar
12272 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12273 &new_n_tool_bar);
12275 /* Redisplay the tool-bar if we changed it. */
12276 if (new_n_tool_bar != f->n_tool_bar_items
12277 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12279 /* Redisplay that happens asynchronously due to an expose event
12280 may access f->tool_bar_items. Make sure we update both
12281 variables within BLOCK_INPUT so no such event interrupts. */
12282 block_input ();
12283 fset_tool_bar_items (f, new_tool_bar);
12284 f->n_tool_bar_items = new_n_tool_bar;
12285 w->update_mode_line = true;
12286 unblock_input ();
12289 unbind_to (count, Qnil);
12290 set_buffer_internal_1 (prev);
12295 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12297 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12298 F's desired tool-bar contents. F->tool_bar_items must have
12299 been set up previously by calling prepare_menu_bars. */
12301 static void
12302 build_desired_tool_bar_string (struct frame *f)
12304 int i, size, size_needed;
12305 Lisp_Object image, plist;
12307 image = plist = Qnil;
12309 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12310 Otherwise, make a new string. */
12312 /* The size of the string we might be able to reuse. */
12313 size = (STRINGP (f->desired_tool_bar_string)
12314 ? SCHARS (f->desired_tool_bar_string)
12315 : 0);
12317 /* We need one space in the string for each image. */
12318 size_needed = f->n_tool_bar_items;
12320 /* Reuse f->desired_tool_bar_string, if possible. */
12321 if (size < size_needed || NILP (f->desired_tool_bar_string))
12322 fset_desired_tool_bar_string
12323 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12324 else
12326 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12327 Fremove_text_properties (make_number (0), make_number (size),
12328 props, f->desired_tool_bar_string);
12331 /* Put a `display' property on the string for the images to display,
12332 put a `menu_item' property on tool-bar items with a value that
12333 is the index of the item in F's tool-bar item vector. */
12334 for (i = 0; i < f->n_tool_bar_items; ++i)
12336 #define PROP(IDX) \
12337 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12339 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12340 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12341 int hmargin, vmargin, relief, idx, end;
12343 /* If image is a vector, choose the image according to the
12344 button state. */
12345 image = PROP (TOOL_BAR_ITEM_IMAGES);
12346 if (VECTORP (image))
12348 if (enabled_p)
12349 idx = (selected_p
12350 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12351 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12352 else
12353 idx = (selected_p
12354 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12355 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12357 eassert (ASIZE (image) >= idx);
12358 image = AREF (image, idx);
12360 else
12361 idx = -1;
12363 /* Ignore invalid image specifications. */
12364 if (!valid_image_p (image))
12365 continue;
12367 /* Display the tool-bar button pressed, or depressed. */
12368 plist = Fcopy_sequence (XCDR (image));
12370 /* Compute margin and relief to draw. */
12371 relief = (tool_bar_button_relief >= 0
12372 ? tool_bar_button_relief
12373 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12374 hmargin = vmargin = relief;
12376 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12377 INT_MAX - max (hmargin, vmargin)))
12379 hmargin += XFASTINT (Vtool_bar_button_margin);
12380 vmargin += XFASTINT (Vtool_bar_button_margin);
12382 else if (CONSP (Vtool_bar_button_margin))
12384 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12385 INT_MAX - hmargin))
12386 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12388 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12389 INT_MAX - vmargin))
12390 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12393 if (auto_raise_tool_bar_buttons_p)
12395 /* Add a `:relief' property to the image spec if the item is
12396 selected. */
12397 if (selected_p)
12399 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12400 hmargin -= relief;
12401 vmargin -= relief;
12404 else
12406 /* If image is selected, display it pressed, i.e. with a
12407 negative relief. If it's not selected, display it with a
12408 raised relief. */
12409 plist = Fplist_put (plist, QCrelief,
12410 (selected_p
12411 ? make_number (-relief)
12412 : make_number (relief)));
12413 hmargin -= relief;
12414 vmargin -= relief;
12417 /* Put a margin around the image. */
12418 if (hmargin || vmargin)
12420 if (hmargin == vmargin)
12421 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12422 else
12423 plist = Fplist_put (plist, QCmargin,
12424 Fcons (make_number (hmargin),
12425 make_number (vmargin)));
12428 /* If button is not enabled, and we don't have special images
12429 for the disabled state, make the image appear disabled by
12430 applying an appropriate algorithm to it. */
12431 if (!enabled_p && idx < 0)
12432 plist = Fplist_put (plist, QCconversion, Qdisabled);
12434 /* Put a `display' text property on the string for the image to
12435 display. Put a `menu-item' property on the string that gives
12436 the start of this item's properties in the tool-bar items
12437 vector. */
12438 image = Fcons (Qimage, plist);
12439 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12440 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12442 /* Let the last image hide all remaining spaces in the tool bar
12443 string. The string can be longer than needed when we reuse a
12444 previous string. */
12445 if (i + 1 == f->n_tool_bar_items)
12446 end = SCHARS (f->desired_tool_bar_string);
12447 else
12448 end = i + 1;
12449 Fadd_text_properties (make_number (i), make_number (end),
12450 props, f->desired_tool_bar_string);
12451 #undef PROP
12456 /* Display one line of the tool-bar of frame IT->f.
12458 HEIGHT specifies the desired height of the tool-bar line.
12459 If the actual height of the glyph row is less than HEIGHT, the
12460 row's height is increased to HEIGHT, and the icons are centered
12461 vertically in the new height.
12463 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12464 count a final empty row in case the tool-bar width exactly matches
12465 the window width.
12468 static void
12469 display_tool_bar_line (struct it *it, int height)
12471 struct glyph_row *row = it->glyph_row;
12472 int max_x = it->last_visible_x;
12473 struct glyph *last;
12475 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12476 clear_glyph_row (row);
12477 row->enabled_p = true;
12478 row->y = it->current_y;
12480 /* Note that this isn't made use of if the face hasn't a box,
12481 so there's no need to check the face here. */
12482 it->start_of_box_run_p = true;
12484 while (it->current_x < max_x)
12486 int x, n_glyphs_before, i, nglyphs;
12487 struct it it_before;
12489 /* Get the next display element. */
12490 if (!get_next_display_element (it))
12492 /* Don't count empty row if we are counting needed tool-bar lines. */
12493 if (height < 0 && !it->hpos)
12494 return;
12495 break;
12498 /* Produce glyphs. */
12499 n_glyphs_before = row->used[TEXT_AREA];
12500 it_before = *it;
12502 PRODUCE_GLYPHS (it);
12504 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12505 i = 0;
12506 x = it_before.current_x;
12507 while (i < nglyphs)
12509 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12511 if (x + glyph->pixel_width > max_x)
12513 /* Glyph doesn't fit on line. Backtrack. */
12514 row->used[TEXT_AREA] = n_glyphs_before;
12515 *it = it_before;
12516 /* If this is the only glyph on this line, it will never fit on the
12517 tool-bar, so skip it. But ensure there is at least one glyph,
12518 so we don't accidentally disable the tool-bar. */
12519 if (n_glyphs_before == 0
12520 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12521 break;
12522 goto out;
12525 ++it->hpos;
12526 x += glyph->pixel_width;
12527 ++i;
12530 /* Stop at line end. */
12531 if (ITERATOR_AT_END_OF_LINE_P (it))
12532 break;
12534 set_iterator_to_next (it, true);
12537 out:;
12539 row->displays_text_p = row->used[TEXT_AREA] != 0;
12541 /* Use default face for the border below the tool bar.
12543 FIXME: When auto-resize-tool-bars is grow-only, there is
12544 no additional border below the possibly empty tool-bar lines.
12545 So to make the extra empty lines look "normal", we have to
12546 use the tool-bar face for the border too. */
12547 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12548 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12549 it->face_id = DEFAULT_FACE_ID;
12551 extend_face_to_end_of_line (it);
12552 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12553 last->right_box_line_p = true;
12554 if (last == row->glyphs[TEXT_AREA])
12555 last->left_box_line_p = true;
12557 /* Make line the desired height and center it vertically. */
12558 if ((height -= it->max_ascent + it->max_descent) > 0)
12560 /* Don't add more than one line height. */
12561 height %= FRAME_LINE_HEIGHT (it->f);
12562 it->max_ascent += height / 2;
12563 it->max_descent += (height + 1) / 2;
12566 compute_line_metrics (it);
12568 /* If line is empty, make it occupy the rest of the tool-bar. */
12569 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12571 row->height = row->phys_height = it->last_visible_y - row->y;
12572 row->visible_height = row->height;
12573 row->ascent = row->phys_ascent = 0;
12574 row->extra_line_spacing = 0;
12577 row->full_width_p = true;
12578 row->continued_p = false;
12579 row->truncated_on_left_p = false;
12580 row->truncated_on_right_p = false;
12582 it->current_x = it->hpos = 0;
12583 it->current_y += row->height;
12584 ++it->vpos;
12585 ++it->glyph_row;
12589 /* Value is the number of pixels needed to make all tool-bar items of
12590 frame F visible. The actual number of glyph rows needed is
12591 returned in *N_ROWS if non-NULL. */
12592 static int
12593 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12595 struct window *w = XWINDOW (f->tool_bar_window);
12596 struct it it;
12597 /* tool_bar_height is called from redisplay_tool_bar after building
12598 the desired matrix, so use (unused) mode-line row as temporary row to
12599 avoid destroying the first tool-bar row. */
12600 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12602 /* Initialize an iterator for iteration over
12603 F->desired_tool_bar_string in the tool-bar window of frame F. */
12604 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12605 temp_row->reversed_p = false;
12606 it.first_visible_x = 0;
12607 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12608 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12609 it.paragraph_embedding = L2R;
12611 while (!ITERATOR_AT_END_P (&it))
12613 clear_glyph_row (temp_row);
12614 it.glyph_row = temp_row;
12615 display_tool_bar_line (&it, -1);
12617 clear_glyph_row (temp_row);
12619 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12620 if (n_rows)
12621 *n_rows = it.vpos > 0 ? it.vpos : -1;
12623 if (pixelwise)
12624 return it.current_y;
12625 else
12626 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12629 #endif /* !USE_GTK && !HAVE_NS */
12631 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12632 0, 2, 0,
12633 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12634 If FRAME is nil or omitted, use the selected frame. Optional argument
12635 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12636 (Lisp_Object frame, Lisp_Object pixelwise)
12638 int height = 0;
12640 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12641 struct frame *f = decode_any_frame (frame);
12643 if (WINDOWP (f->tool_bar_window)
12644 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12646 update_tool_bar (f, true);
12647 if (f->n_tool_bar_items)
12649 build_desired_tool_bar_string (f);
12650 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12653 #endif
12655 return make_number (height);
12659 /* Display the tool-bar of frame F. Value is true if tool-bar's
12660 height should be changed. */
12661 static bool
12662 redisplay_tool_bar (struct frame *f)
12664 f->tool_bar_redisplayed = true;
12665 #if defined (USE_GTK) || defined (HAVE_NS)
12667 if (FRAME_EXTERNAL_TOOL_BAR (f))
12668 update_frame_tool_bar (f);
12669 return false;
12671 #else /* !USE_GTK && !HAVE_NS */
12673 struct window *w;
12674 struct it it;
12675 struct glyph_row *row;
12677 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12678 do anything. This means you must start with tool-bar-lines
12679 non-zero to get the auto-sizing effect. Or in other words, you
12680 can turn off tool-bars by specifying tool-bar-lines zero. */
12681 if (!WINDOWP (f->tool_bar_window)
12682 || (w = XWINDOW (f->tool_bar_window),
12683 WINDOW_TOTAL_LINES (w) == 0))
12684 return false;
12686 /* Set up an iterator for the tool-bar window. */
12687 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12688 it.first_visible_x = 0;
12689 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12690 row = it.glyph_row;
12691 row->reversed_p = false;
12693 /* Build a string that represents the contents of the tool-bar. */
12694 build_desired_tool_bar_string (f);
12695 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12696 /* FIXME: This should be controlled by a user option. But it
12697 doesn't make sense to have an R2L tool bar if the menu bar cannot
12698 be drawn also R2L, and making the menu bar R2L is tricky due
12699 toolkit-specific code that implements it. If an R2L tool bar is
12700 ever supported, display_tool_bar_line should also be augmented to
12701 call unproduce_glyphs like display_line and display_string
12702 do. */
12703 it.paragraph_embedding = L2R;
12705 if (f->n_tool_bar_rows == 0)
12707 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12709 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12711 x_change_tool_bar_height (f, new_height);
12712 frame_default_tool_bar_height = new_height;
12713 /* Always do that now. */
12714 clear_glyph_matrix (w->desired_matrix);
12715 f->fonts_changed = true;
12716 return true;
12720 /* Display as many lines as needed to display all tool-bar items. */
12722 if (f->n_tool_bar_rows > 0)
12724 int border, rows, height, extra;
12726 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12727 border = XINT (Vtool_bar_border);
12728 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12729 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12730 else if (EQ (Vtool_bar_border, Qborder_width))
12731 border = f->border_width;
12732 else
12733 border = 0;
12734 if (border < 0)
12735 border = 0;
12737 rows = f->n_tool_bar_rows;
12738 height = max (1, (it.last_visible_y - border) / rows);
12739 extra = it.last_visible_y - border - height * rows;
12741 while (it.current_y < it.last_visible_y)
12743 int h = 0;
12744 if (extra > 0 && rows-- > 0)
12746 h = (extra + rows - 1) / rows;
12747 extra -= h;
12749 display_tool_bar_line (&it, height + h);
12752 else
12754 while (it.current_y < it.last_visible_y)
12755 display_tool_bar_line (&it, 0);
12758 /* It doesn't make much sense to try scrolling in the tool-bar
12759 window, so don't do it. */
12760 w->desired_matrix->no_scrolling_p = true;
12761 w->must_be_updated_p = true;
12763 if (!NILP (Vauto_resize_tool_bars))
12765 bool change_height_p = true;
12767 /* If we couldn't display everything, change the tool-bar's
12768 height if there is room for more. */
12769 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12770 change_height_p = true;
12772 /* We subtract 1 because display_tool_bar_line advances the
12773 glyph_row pointer before returning to its caller. We want to
12774 examine the last glyph row produced by
12775 display_tool_bar_line. */
12776 row = it.glyph_row - 1;
12778 /* If there are blank lines at the end, except for a partially
12779 visible blank line at the end that is smaller than
12780 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12781 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12782 && row->height >= FRAME_LINE_HEIGHT (f))
12783 change_height_p = true;
12785 /* If row displays tool-bar items, but is partially visible,
12786 change the tool-bar's height. */
12787 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12788 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12789 change_height_p = true;
12791 /* Resize windows as needed by changing the `tool-bar-lines'
12792 frame parameter. */
12793 if (change_height_p)
12795 int nrows;
12796 int new_height = tool_bar_height (f, &nrows, true);
12798 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12799 && !f->minimize_tool_bar_window_p)
12800 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12801 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12802 f->minimize_tool_bar_window_p = false;
12804 if (change_height_p)
12806 x_change_tool_bar_height (f, new_height);
12807 frame_default_tool_bar_height = new_height;
12808 clear_glyph_matrix (w->desired_matrix);
12809 f->n_tool_bar_rows = nrows;
12810 f->fonts_changed = true;
12812 return true;
12817 f->minimize_tool_bar_window_p = false;
12818 return false;
12820 #endif /* USE_GTK || HAVE_NS */
12823 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12825 /* Get information about the tool-bar item which is displayed in GLYPH
12826 on frame F. Return in *PROP_IDX the index where tool-bar item
12827 properties start in F->tool_bar_items. Value is false if
12828 GLYPH doesn't display a tool-bar item. */
12830 static bool
12831 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12833 Lisp_Object prop;
12834 int charpos;
12836 /* This function can be called asynchronously, which means we must
12837 exclude any possibility that Fget_text_property signals an
12838 error. */
12839 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12840 charpos = max (0, charpos);
12842 /* Get the text property `menu-item' at pos. The value of that
12843 property is the start index of this item's properties in
12844 F->tool_bar_items. */
12845 prop = Fget_text_property (make_number (charpos),
12846 Qmenu_item, f->current_tool_bar_string);
12847 if (! INTEGERP (prop))
12848 return false;
12849 *prop_idx = XINT (prop);
12850 return true;
12854 /* Get information about the tool-bar item at position X/Y on frame F.
12855 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12856 the current matrix of the tool-bar window of F, or NULL if not
12857 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12858 item in F->tool_bar_items. Value is
12860 -1 if X/Y is not on a tool-bar item
12861 0 if X/Y is on the same item that was highlighted before.
12862 1 otherwise. */
12864 static int
12865 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12866 int *hpos, int *vpos, int *prop_idx)
12868 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12869 struct window *w = XWINDOW (f->tool_bar_window);
12870 int area;
12872 /* Find the glyph under X/Y. */
12873 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12874 if (*glyph == NULL)
12875 return -1;
12877 /* Get the start of this tool-bar item's properties in
12878 f->tool_bar_items. */
12879 if (!tool_bar_item_info (f, *glyph, prop_idx))
12880 return -1;
12882 /* Is mouse on the highlighted item? */
12883 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12884 && *vpos >= hlinfo->mouse_face_beg_row
12885 && *vpos <= hlinfo->mouse_face_end_row
12886 && (*vpos > hlinfo->mouse_face_beg_row
12887 || *hpos >= hlinfo->mouse_face_beg_col)
12888 && (*vpos < hlinfo->mouse_face_end_row
12889 || *hpos < hlinfo->mouse_face_end_col
12890 || hlinfo->mouse_face_past_end))
12891 return 0;
12893 return 1;
12897 /* EXPORT:
12898 Handle mouse button event on the tool-bar of frame F, at
12899 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12900 false for button release. MODIFIERS is event modifiers for button
12901 release. */
12903 void
12904 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12905 int modifiers)
12907 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12908 struct window *w = XWINDOW (f->tool_bar_window);
12909 int hpos, vpos, prop_idx;
12910 struct glyph *glyph;
12911 Lisp_Object enabled_p;
12912 int ts;
12914 /* If not on the highlighted tool-bar item, and mouse-highlight is
12915 non-nil, return. This is so we generate the tool-bar button
12916 click only when the mouse button is released on the same item as
12917 where it was pressed. However, when mouse-highlight is disabled,
12918 generate the click when the button is released regardless of the
12919 highlight, since tool-bar items are not highlighted in that
12920 case. */
12921 frame_to_window_pixel_xy (w, &x, &y);
12922 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12923 if (ts == -1
12924 || (ts != 0 && !NILP (Vmouse_highlight)))
12925 return;
12927 /* When mouse-highlight is off, generate the click for the item
12928 where the button was pressed, disregarding where it was
12929 released. */
12930 if (NILP (Vmouse_highlight) && !down_p)
12931 prop_idx = f->last_tool_bar_item;
12933 /* If item is disabled, do nothing. */
12934 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12935 if (NILP (enabled_p))
12936 return;
12938 if (down_p)
12940 /* Show item in pressed state. */
12941 if (!NILP (Vmouse_highlight))
12942 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12943 f->last_tool_bar_item = prop_idx;
12945 else
12947 Lisp_Object key, frame;
12948 struct input_event event;
12949 EVENT_INIT (event);
12951 /* Show item in released state. */
12952 if (!NILP (Vmouse_highlight))
12953 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12955 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12957 XSETFRAME (frame, f);
12958 event.kind = TOOL_BAR_EVENT;
12959 event.frame_or_window = frame;
12960 event.arg = frame;
12961 kbd_buffer_store_event (&event);
12963 event.kind = TOOL_BAR_EVENT;
12964 event.frame_or_window = frame;
12965 event.arg = key;
12966 event.modifiers = modifiers;
12967 kbd_buffer_store_event (&event);
12968 f->last_tool_bar_item = -1;
12973 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12974 tool-bar window-relative coordinates X/Y. Called from
12975 note_mouse_highlight. */
12977 static void
12978 note_tool_bar_highlight (struct frame *f, int x, int y)
12980 Lisp_Object window = f->tool_bar_window;
12981 struct window *w = XWINDOW (window);
12982 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12983 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12984 int hpos, vpos;
12985 struct glyph *glyph;
12986 struct glyph_row *row;
12987 int i;
12988 Lisp_Object enabled_p;
12989 int prop_idx;
12990 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12991 bool mouse_down_p;
12992 int rc;
12994 /* Function note_mouse_highlight is called with negative X/Y
12995 values when mouse moves outside of the frame. */
12996 if (x <= 0 || y <= 0)
12998 clear_mouse_face (hlinfo);
12999 return;
13002 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13003 if (rc < 0)
13005 /* Not on tool-bar item. */
13006 clear_mouse_face (hlinfo);
13007 return;
13009 else if (rc == 0)
13010 /* On same tool-bar item as before. */
13011 goto set_help_echo;
13013 clear_mouse_face (hlinfo);
13015 /* Mouse is down, but on different tool-bar item? */
13016 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13017 && f == dpyinfo->last_mouse_frame);
13019 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13020 return;
13022 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13024 /* If tool-bar item is not enabled, don't highlight it. */
13025 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13026 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13028 /* Compute the x-position of the glyph. In front and past the
13029 image is a space. We include this in the highlighted area. */
13030 row = MATRIX_ROW (w->current_matrix, vpos);
13031 for (i = x = 0; i < hpos; ++i)
13032 x += row->glyphs[TEXT_AREA][i].pixel_width;
13034 /* Record this as the current active region. */
13035 hlinfo->mouse_face_beg_col = hpos;
13036 hlinfo->mouse_face_beg_row = vpos;
13037 hlinfo->mouse_face_beg_x = x;
13038 hlinfo->mouse_face_past_end = false;
13040 hlinfo->mouse_face_end_col = hpos + 1;
13041 hlinfo->mouse_face_end_row = vpos;
13042 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13043 hlinfo->mouse_face_window = window;
13044 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13046 /* Display it as active. */
13047 show_mouse_face (hlinfo, draw);
13050 set_help_echo:
13052 /* Set help_echo_string to a help string to display for this tool-bar item.
13053 XTread_socket does the rest. */
13054 help_echo_object = help_echo_window = Qnil;
13055 help_echo_pos = -1;
13056 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13057 if (NILP (help_echo_string))
13058 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13061 #endif /* !USE_GTK && !HAVE_NS */
13063 #endif /* HAVE_WINDOW_SYSTEM */
13067 /************************************************************************
13068 Horizontal scrolling
13069 ************************************************************************/
13071 /* For all leaf windows in the window tree rooted at WINDOW, set their
13072 hscroll value so that PT is (i) visible in the window, and (ii) so
13073 that it is not within a certain margin at the window's left and
13074 right border. Value is true if any window's hscroll has been
13075 changed. */
13077 static bool
13078 hscroll_window_tree (Lisp_Object window)
13080 bool hscrolled_p = false;
13081 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13082 int hscroll_step_abs = 0;
13083 double hscroll_step_rel = 0;
13085 if (hscroll_relative_p)
13087 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13088 if (hscroll_step_rel < 0)
13090 hscroll_relative_p = false;
13091 hscroll_step_abs = 0;
13094 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13096 hscroll_step_abs = XINT (Vhscroll_step);
13097 if (hscroll_step_abs < 0)
13098 hscroll_step_abs = 0;
13100 else
13101 hscroll_step_abs = 0;
13103 while (WINDOWP (window))
13105 struct window *w = XWINDOW (window);
13107 if (WINDOWP (w->contents))
13108 hscrolled_p |= hscroll_window_tree (w->contents);
13109 else if (w->cursor.vpos >= 0)
13111 int h_margin;
13112 int text_area_width;
13113 struct glyph_row *cursor_row;
13114 struct glyph_row *bottom_row;
13116 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13117 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13118 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13119 else
13120 cursor_row = bottom_row - 1;
13122 if (!cursor_row->enabled_p)
13124 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13125 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13126 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13127 else
13128 cursor_row = bottom_row - 1;
13130 bool row_r2l_p = cursor_row->reversed_p;
13131 bool hscl = hscrolling_current_line_p (w);
13132 int x_offset = 0;
13133 /* When line numbers are displayed, we need to account for
13134 the horizontal space they consume. */
13135 if (!NILP (Vdisplay_line_numbers))
13137 struct glyph *g;
13138 if (!row_r2l_p)
13140 for (g = cursor_row->glyphs[TEXT_AREA];
13141 g < cursor_row->glyphs[TEXT_AREA]
13142 + cursor_row->used[TEXT_AREA];
13143 g++)
13145 if (!(NILP (g->object) && g->charpos < 0))
13146 break;
13147 x_offset += g->pixel_width;
13150 else
13152 for (g = cursor_row->glyphs[TEXT_AREA]
13153 + cursor_row->used[TEXT_AREA];
13154 g > cursor_row->glyphs[TEXT_AREA];
13155 g--)
13157 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13158 break;
13159 x_offset += (g - 1)->pixel_width;
13163 if (cursor_row->truncated_on_left_p)
13165 /* On TTY frames, don't count the left truncation glyph. */
13166 struct frame *f = XFRAME (WINDOW_FRAME (w));
13167 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13170 text_area_width = window_box_width (w, TEXT_AREA);
13172 /* Scroll when cursor is inside this scroll margin. */
13173 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13175 /* If the position of this window's point has explicitly
13176 changed, no more suspend auto hscrolling. */
13177 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13178 w->suspend_auto_hscroll = false;
13180 /* Remember window point. */
13181 Fset_marker (w->old_pointm,
13182 ((w == XWINDOW (selected_window))
13183 ? make_number (BUF_PT (XBUFFER (w->contents)))
13184 : Fmarker_position (w->pointm)),
13185 w->contents);
13187 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13188 && !w->suspend_auto_hscroll
13189 /* In some pathological cases, like restoring a window
13190 configuration into a frame that is much smaller than
13191 the one from which the configuration was saved, we
13192 get glyph rows whose start and end have zero buffer
13193 positions, which we cannot handle below. Just skip
13194 such windows. */
13195 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13196 /* For left-to-right rows, hscroll when cursor is either
13197 (i) inside the right hscroll margin, or (ii) if it is
13198 inside the left margin and the window is already
13199 hscrolled. */
13200 && ((!row_r2l_p
13201 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13202 || (cursor_row->enabled_p
13203 && cursor_row->truncated_on_right_p
13204 && (w->cursor.x >= text_area_width - h_margin))))
13205 /* For right-to-left rows, the logic is similar,
13206 except that rules for scrolling to left and right
13207 are reversed. E.g., if cursor.x <= h_margin, we
13208 need to hscroll "to the right" unconditionally,
13209 and that will scroll the screen to the left so as
13210 to reveal the next portion of the row. */
13211 || (row_r2l_p
13212 && ((cursor_row->enabled_p
13213 /* FIXME: It is confusing to set the
13214 truncated_on_right_p flag when R2L rows
13215 are actually truncated on the left. */
13216 && cursor_row->truncated_on_right_p
13217 && w->cursor.x <= h_margin)
13218 || (w->hscroll
13219 && (w->cursor.x >= (text_area_width - h_margin
13220 - x_offset)))))
13221 /* This last condition is needed when moving
13222 vertically from an hscrolled line to a short line
13223 that doesn't need to be hscrolled. If we omit
13224 this condition, the line from which we move will
13225 remain hscrolled. */
13226 || (hscl
13227 && w->hscroll != w->min_hscroll
13228 && !cursor_row->truncated_on_left_p)))
13230 struct it it;
13231 ptrdiff_t hscroll;
13232 struct buffer *saved_current_buffer;
13233 ptrdiff_t pt;
13234 int wanted_x;
13236 /* Find point in a display of infinite width. */
13237 saved_current_buffer = current_buffer;
13238 current_buffer = XBUFFER (w->contents);
13240 if (w == XWINDOW (selected_window))
13241 pt = PT;
13242 else
13243 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13245 /* Move iterator to pt starting at cursor_row->start in
13246 a line with infinite width. */
13247 init_to_row_start (&it, w, cursor_row);
13248 if (hscl)
13249 it.first_visible_x = window_hscroll_limited (w, it.f)
13250 * FRAME_COLUMN_WIDTH (it.f);
13251 it.last_visible_x = DISP_INFINITY;
13252 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13253 /* If the line ends in an overlay string with a newline,
13254 we might infloop, because displaying the window will
13255 want to put the cursor after the overlay, i.e. at X
13256 coordinate of zero on the next screen line. So we
13257 use the buffer position prior to the overlay string
13258 instead. */
13259 if (it.method == GET_FROM_STRING && pt > 1)
13261 init_to_row_start (&it, w, cursor_row);
13262 if (hscl)
13263 it.first_visible_x = (window_hscroll_limited (w, it.f)
13264 * FRAME_COLUMN_WIDTH (it.f));
13265 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13267 current_buffer = saved_current_buffer;
13269 /* Position cursor in window. */
13270 if (!hscroll_relative_p && hscroll_step_abs == 0)
13271 hscroll = max (0, (it.current_x
13272 - (ITERATOR_AT_END_OF_LINE_P (&it)
13273 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13274 : (text_area_width / 2))))
13275 / FRAME_COLUMN_WIDTH (it.f);
13276 else if ((!row_r2l_p
13277 && w->cursor.x >= text_area_width - h_margin)
13278 || (row_r2l_p && w->cursor.x <= h_margin))
13280 if (hscroll_relative_p)
13281 wanted_x = text_area_width * (1 - hscroll_step_rel)
13282 - h_margin;
13283 else
13284 wanted_x = text_area_width
13285 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13286 - h_margin;
13287 hscroll
13288 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13290 else
13292 if (hscroll_relative_p)
13293 wanted_x = text_area_width * hscroll_step_rel
13294 + h_margin;
13295 else
13296 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13297 + h_margin;
13298 hscroll
13299 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13301 hscroll = max (hscroll, w->min_hscroll);
13303 /* Don't prevent redisplay optimizations if hscroll
13304 hasn't changed, as it will unnecessarily slow down
13305 redisplay. */
13306 if (w->hscroll != hscroll
13307 /* When hscrolling only the current line, we need to
13308 report hscroll even if its value is equal to the
13309 previous one, because the new line might need a
13310 different value. */
13311 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13313 struct buffer *b = XBUFFER (w->contents);
13314 b->prevent_redisplay_optimizations_p = true;
13315 w->hscroll = hscroll;
13316 hscrolled_p = true;
13321 window = w->next;
13324 /* Value is true if hscroll of any leaf window has been changed. */
13325 return hscrolled_p;
13329 /* Set hscroll so that cursor is visible and not inside horizontal
13330 scroll margins for all windows in the tree rooted at WINDOW. See
13331 also hscroll_window_tree above. Value is true if any window's
13332 hscroll has been changed. If it has, desired matrices on the frame
13333 of WINDOW are cleared. */
13335 static bool
13336 hscroll_windows (Lisp_Object window)
13338 bool hscrolled_p = hscroll_window_tree (window);
13339 if (hscrolled_p)
13340 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13341 return hscrolled_p;
13346 /************************************************************************
13347 Redisplay
13348 ************************************************************************/
13350 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13351 This is sometimes handy to have in a debugger session. */
13353 #ifdef GLYPH_DEBUG
13355 /* First and last unchanged row for try_window_id. */
13357 static int debug_first_unchanged_at_end_vpos;
13358 static int debug_last_unchanged_at_beg_vpos;
13360 /* Delta vpos and y. */
13362 static int debug_dvpos, debug_dy;
13364 /* Delta in characters and bytes for try_window_id. */
13366 static ptrdiff_t debug_delta, debug_delta_bytes;
13368 /* Values of window_end_pos and window_end_vpos at the end of
13369 try_window_id. */
13371 static ptrdiff_t debug_end_vpos;
13373 /* Append a string to W->desired_matrix->method. FMT is a printf
13374 format string. If trace_redisplay_p is true also printf the
13375 resulting string to stderr. */
13377 static void debug_method_add (struct window *, char const *, ...)
13378 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13380 static void
13381 debug_method_add (struct window *w, char const *fmt, ...)
13383 void *ptr = w;
13384 char *method = w->desired_matrix->method;
13385 int len = strlen (method);
13386 int size = sizeof w->desired_matrix->method;
13387 int remaining = size - len - 1;
13388 va_list ap;
13390 if (len && remaining)
13392 method[len] = '|';
13393 --remaining, ++len;
13396 va_start (ap, fmt);
13397 vsnprintf (method + len, remaining + 1, fmt, ap);
13398 va_end (ap);
13400 if (trace_redisplay_p)
13401 fprintf (stderr, "%p (%s): %s\n",
13402 ptr,
13403 ((BUFFERP (w->contents)
13404 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13405 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13406 : "no buffer"),
13407 method + len);
13410 #endif /* GLYPH_DEBUG */
13413 /* Value is true if all changes in window W, which displays
13414 current_buffer, are in the text between START and END. START is a
13415 buffer position, END is given as a distance from Z. Used in
13416 redisplay_internal for display optimization. */
13418 static bool
13419 text_outside_line_unchanged_p (struct window *w,
13420 ptrdiff_t start, ptrdiff_t end)
13422 bool unchanged_p = true;
13424 /* If text or overlays have changed, see where. */
13425 if (window_outdated (w))
13427 /* Gap in the line? */
13428 if (GPT < start || Z - GPT < end)
13429 unchanged_p = false;
13431 /* Changes start in front of the line, or end after it? */
13432 if (unchanged_p
13433 && (BEG_UNCHANGED < start - 1
13434 || END_UNCHANGED < end))
13435 unchanged_p = false;
13437 /* If selective display, can't optimize if changes start at the
13438 beginning of the line. */
13439 if (unchanged_p
13440 && INTEGERP (BVAR (current_buffer, selective_display))
13441 && XINT (BVAR (current_buffer, selective_display)) > 0
13442 && (BEG_UNCHANGED < start || GPT <= start))
13443 unchanged_p = false;
13445 /* If there are overlays at the start or end of the line, these
13446 may have overlay strings with newlines in them. A change at
13447 START, for instance, may actually concern the display of such
13448 overlay strings as well, and they are displayed on different
13449 lines. So, quickly rule out this case. (For the future, it
13450 might be desirable to implement something more telling than
13451 just BEG/END_UNCHANGED.) */
13452 if (unchanged_p)
13454 if (BEG + BEG_UNCHANGED == start
13455 && overlay_touches_p (start))
13456 unchanged_p = false;
13457 if (END_UNCHANGED == end
13458 && overlay_touches_p (Z - end))
13459 unchanged_p = false;
13462 /* Under bidi reordering, adding or deleting a character in the
13463 beginning of a paragraph, before the first strong directional
13464 character, can change the base direction of the paragraph (unless
13465 the buffer specifies a fixed paragraph direction), which will
13466 require redisplaying the whole paragraph. It might be worthwhile
13467 to find the paragraph limits and widen the range of redisplayed
13468 lines to that, but for now just give up this optimization. */
13469 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13470 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13471 unchanged_p = false;
13474 return unchanged_p;
13478 /* Do a frame update, taking possible shortcuts into account. This is
13479 the main external entry point for redisplay.
13481 If the last redisplay displayed an echo area message and that message
13482 is no longer requested, we clear the echo area or bring back the
13483 mini-buffer if that is in use. */
13485 void
13486 redisplay (void)
13488 redisplay_internal ();
13492 static Lisp_Object
13493 overlay_arrow_string_or_property (Lisp_Object var)
13495 Lisp_Object val;
13497 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13498 return val;
13500 return Voverlay_arrow_string;
13503 /* Return true if there are any overlay-arrows in current_buffer. */
13504 static bool
13505 overlay_arrow_in_current_buffer_p (void)
13507 Lisp_Object vlist;
13509 for (vlist = Voverlay_arrow_variable_list;
13510 CONSP (vlist);
13511 vlist = XCDR (vlist))
13513 Lisp_Object var = XCAR (vlist);
13514 Lisp_Object val;
13516 if (!SYMBOLP (var))
13517 continue;
13518 val = find_symbol_value (var);
13519 if (MARKERP (val)
13520 && current_buffer == XMARKER (val)->buffer)
13521 return true;
13523 return false;
13527 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13528 has changed.
13529 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13530 buffers that are affected. */
13532 static bool
13533 overlay_arrows_changed_p (bool set_redisplay)
13535 Lisp_Object vlist;
13536 bool changed = false;
13538 for (vlist = Voverlay_arrow_variable_list;
13539 CONSP (vlist);
13540 vlist = XCDR (vlist))
13542 Lisp_Object var = XCAR (vlist);
13543 Lisp_Object val, pstr;
13545 if (!SYMBOLP (var))
13546 continue;
13547 val = find_symbol_value (var);
13548 if (!MARKERP (val))
13549 continue;
13550 if (! EQ (COERCE_MARKER (val),
13551 /* FIXME: Don't we have a problem, using such a global
13552 * "last-position" if the variable is buffer-local? */
13553 Fget (var, Qlast_arrow_position))
13554 || ! (pstr = overlay_arrow_string_or_property (var),
13555 EQ (pstr, Fget (var, Qlast_arrow_string))))
13557 struct buffer *buf = XMARKER (val)->buffer;
13559 if (set_redisplay)
13561 if (buf)
13562 bset_redisplay (buf);
13563 changed = true;
13565 else
13566 return true;
13569 return changed;
13572 /* Mark overlay arrows to be updated on next redisplay. */
13574 static void
13575 update_overlay_arrows (int up_to_date)
13577 Lisp_Object vlist;
13579 for (vlist = Voverlay_arrow_variable_list;
13580 CONSP (vlist);
13581 vlist = XCDR (vlist))
13583 Lisp_Object var = XCAR (vlist);
13585 if (!SYMBOLP (var))
13586 continue;
13588 if (up_to_date > 0)
13590 Lisp_Object val = find_symbol_value (var);
13591 if (!MARKERP (val))
13592 continue;
13593 Fput (var, Qlast_arrow_position,
13594 COERCE_MARKER (val));
13595 Fput (var, Qlast_arrow_string,
13596 overlay_arrow_string_or_property (var));
13598 else if (up_to_date < 0
13599 || !NILP (Fget (var, Qlast_arrow_position)))
13601 Fput (var, Qlast_arrow_position, Qt);
13602 Fput (var, Qlast_arrow_string, Qt);
13608 /* Return overlay arrow string to display at row.
13609 Return integer (bitmap number) for arrow bitmap in left fringe.
13610 Return nil if no overlay arrow. */
13612 static Lisp_Object
13613 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13615 Lisp_Object vlist;
13617 for (vlist = Voverlay_arrow_variable_list;
13618 CONSP (vlist);
13619 vlist = XCDR (vlist))
13621 Lisp_Object var = XCAR (vlist);
13622 Lisp_Object val;
13624 if (!SYMBOLP (var))
13625 continue;
13627 val = find_symbol_value (var);
13629 if (MARKERP (val)
13630 && current_buffer == XMARKER (val)->buffer
13631 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13633 if (FRAME_WINDOW_P (it->f)
13634 /* FIXME: if ROW->reversed_p is set, this should test
13635 the right fringe, not the left one. */
13636 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13638 #ifdef HAVE_WINDOW_SYSTEM
13639 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13641 int fringe_bitmap = lookup_fringe_bitmap (val);
13642 if (fringe_bitmap != 0)
13643 return make_number (fringe_bitmap);
13645 #endif
13646 return make_number (-1); /* Use default arrow bitmap. */
13648 return overlay_arrow_string_or_property (var);
13652 return Qnil;
13655 /* Return true if point moved out of or into a composition. Otherwise
13656 return false. PREV_BUF and PREV_PT are the last point buffer and
13657 position. BUF and PT are the current point buffer and position. */
13659 static bool
13660 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13661 struct buffer *buf, ptrdiff_t pt)
13663 ptrdiff_t start, end;
13664 Lisp_Object prop;
13665 Lisp_Object buffer;
13667 XSETBUFFER (buffer, buf);
13668 /* Check a composition at the last point if point moved within the
13669 same buffer. */
13670 if (prev_buf == buf)
13672 if (prev_pt == pt)
13673 /* Point didn't move. */
13674 return false;
13676 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13677 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13678 && composition_valid_p (start, end, prop)
13679 && start < prev_pt && end > prev_pt)
13680 /* The last point was within the composition. Return true iff
13681 point moved out of the composition. */
13682 return (pt <= start || pt >= end);
13685 /* Check a composition at the current point. */
13686 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13687 && find_composition (pt, -1, &start, &end, &prop, buffer)
13688 && composition_valid_p (start, end, prop)
13689 && start < pt && end > pt);
13692 /* Reconsider the clip changes of buffer which is displayed in W. */
13694 static void
13695 reconsider_clip_changes (struct window *w)
13697 struct buffer *b = XBUFFER (w->contents);
13699 if (b->clip_changed
13700 && w->window_end_valid
13701 && w->current_matrix->buffer == b
13702 && w->current_matrix->zv == BUF_ZV (b)
13703 && w->current_matrix->begv == BUF_BEGV (b))
13704 b->clip_changed = false;
13706 /* If display wasn't paused, and W is not a tool bar window, see if
13707 point has been moved into or out of a composition. In that case,
13708 set b->clip_changed to force updating the screen. If
13709 b->clip_changed has already been set, skip this check. */
13710 if (!b->clip_changed && w->window_end_valid)
13712 ptrdiff_t pt = (w == XWINDOW (selected_window)
13713 ? PT : marker_position (w->pointm));
13715 if ((w->current_matrix->buffer != b || pt != w->last_point)
13716 && check_point_in_composition (w->current_matrix->buffer,
13717 w->last_point, b, pt))
13718 b->clip_changed = true;
13722 static void
13723 propagate_buffer_redisplay (void)
13724 { /* Resetting b->text->redisplay is problematic!
13725 We can't just reset it in the case that some window that displays
13726 it has not been redisplayed; and such a window can stay
13727 unredisplayed for a long time if it's currently invisible.
13728 But we do want to reset it at the end of redisplay otherwise
13729 its displayed windows will keep being redisplayed over and over
13730 again.
13731 So we copy all b->text->redisplay flags up to their windows here,
13732 such that mark_window_display_accurate can safely reset
13733 b->text->redisplay. */
13734 Lisp_Object ws = window_list ();
13735 for (; CONSP (ws); ws = XCDR (ws))
13737 struct window *thisw = XWINDOW (XCAR (ws));
13738 struct buffer *thisb = XBUFFER (thisw->contents);
13739 if (thisb->text->redisplay)
13740 thisw->redisplay = true;
13744 #define STOP_POLLING \
13745 do { if (! polling_stopped_here) stop_polling (); \
13746 polling_stopped_here = true; } while (false)
13748 #define RESUME_POLLING \
13749 do { if (polling_stopped_here) start_polling (); \
13750 polling_stopped_here = false; } while (false)
13753 /* Perhaps in the future avoid recentering windows if it
13754 is not necessary; currently that causes some problems. */
13756 static void
13757 redisplay_internal (void)
13759 struct window *w = XWINDOW (selected_window);
13760 struct window *sw;
13761 struct frame *fr;
13762 bool pending;
13763 bool must_finish = false, match_p;
13764 struct text_pos tlbufpos, tlendpos;
13765 int number_of_visible_frames;
13766 ptrdiff_t count;
13767 struct frame *sf;
13768 bool polling_stopped_here = false;
13769 Lisp_Object tail, frame;
13771 /* Set a limit to the number of retries we perform due to horizontal
13772 scrolling, this avoids getting stuck in an uninterruptible
13773 infinite loop (Bug #24633). */
13774 enum { MAX_HSCROLL_RETRIES = 16 };
13775 int hscroll_retries = 0;
13777 /* Limit the number of retries for when frame(s) become garbaged as
13778 result of redisplaying them. Some packages set various redisplay
13779 hooks, such as window-scroll-functions, to run Lisp that always
13780 calls APIs which cause the frame's garbaged flag to become set,
13781 so we loop indefinitely. */
13782 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13783 int garbaged_frame_retries = 0;
13785 /* True means redisplay has to consider all windows on all
13786 frames. False, only selected_window is considered. */
13787 bool consider_all_windows_p;
13789 /* True means redisplay has to redisplay the miniwindow. */
13790 bool update_miniwindow_p = false;
13792 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13794 /* No redisplay if running in batch mode or frame is not yet fully
13795 initialized, or redisplay is explicitly turned off by setting
13796 Vinhibit_redisplay. */
13797 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13798 || !NILP (Vinhibit_redisplay))
13799 return;
13801 /* Don't examine these until after testing Vinhibit_redisplay.
13802 When Emacs is shutting down, perhaps because its connection to
13803 X has dropped, we should not look at them at all. */
13804 fr = XFRAME (w->frame);
13805 sf = SELECTED_FRAME ();
13807 if (!fr->glyphs_initialized_p)
13808 return;
13810 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13811 if (popup_activated ())
13812 return;
13813 #endif
13815 /* I don't think this happens but let's be paranoid. */
13816 if (redisplaying_p)
13817 return;
13819 /* Record a function that clears redisplaying_p
13820 when we leave this function. */
13821 count = SPECPDL_INDEX ();
13822 record_unwind_protect_void (unwind_redisplay);
13823 redisplaying_p = true;
13824 block_buffer_flips ();
13825 specbind (Qinhibit_free_realized_faces, Qnil);
13827 /* Record this function, so it appears on the profiler's backtraces. */
13828 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13830 FOR_EACH_FRAME (tail, frame)
13831 XFRAME (frame)->already_hscrolled_p = false;
13833 retry:
13834 /* Remember the currently selected window. */
13835 sw = w;
13837 pending = false;
13838 forget_escape_and_glyphless_faces ();
13840 inhibit_free_realized_faces = false;
13842 /* If face_change, init_iterator will free all realized faces, which
13843 includes the faces referenced from current matrices. So, we
13844 can't reuse current matrices in this case. */
13845 if (face_change)
13846 windows_or_buffers_changed = 47;
13848 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13849 && FRAME_TTY (sf)->previous_frame != sf)
13851 /* Since frames on a single ASCII terminal share the same
13852 display area, displaying a different frame means redisplay
13853 the whole thing. */
13854 SET_FRAME_GARBAGED (sf);
13855 #ifndef DOS_NT
13856 set_tty_color_mode (FRAME_TTY (sf), sf);
13857 #endif
13858 FRAME_TTY (sf)->previous_frame = sf;
13861 /* Set the visible flags for all frames. Do this before checking for
13862 resized or garbaged frames; they want to know if their frames are
13863 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13864 number_of_visible_frames = 0;
13866 FOR_EACH_FRAME (tail, frame)
13868 struct frame *f = XFRAME (frame);
13870 if (FRAME_VISIBLE_P (f))
13872 ++number_of_visible_frames;
13873 /* Adjust matrices for visible frames only. */
13874 if (f->fonts_changed)
13876 adjust_frame_glyphs (f);
13877 /* Disable all redisplay optimizations for this frame.
13878 This is because adjust_frame_glyphs resets the
13879 enabled_p flag for all glyph rows of all windows, so
13880 many optimizations will fail anyway, and some might
13881 fail to test that flag and do bogus things as
13882 result. */
13883 SET_FRAME_GARBAGED (f);
13884 f->fonts_changed = false;
13886 /* If cursor type has been changed on the frame
13887 other than selected, consider all frames. */
13888 if (f != sf && f->cursor_type_changed)
13889 fset_redisplay (f);
13891 clear_desired_matrices (f);
13894 /* Notice any pending interrupt request to change frame size. */
13895 do_pending_window_change (true);
13897 /* do_pending_window_change could change the selected_window due to
13898 frame resizing which makes the selected window too small. */
13899 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13900 sw = w;
13902 /* Clear frames marked as garbaged. */
13903 clear_garbaged_frames ();
13905 /* Build menubar and tool-bar items. */
13906 if (NILP (Vmemory_full))
13907 prepare_menu_bars ();
13909 reconsider_clip_changes (w);
13911 /* In most cases selected window displays current buffer. */
13912 match_p = XBUFFER (w->contents) == current_buffer;
13913 if (match_p)
13915 /* Detect case that we need to write or remove a star in the mode line. */
13916 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13917 w->update_mode_line = true;
13919 if (mode_line_update_needed (w))
13920 w->update_mode_line = true;
13922 /* If reconsider_clip_changes above decided that the narrowing
13923 in the current buffer changed, make sure all other windows
13924 showing that buffer will be redisplayed. */
13925 if (current_buffer->clip_changed)
13926 bset_update_mode_line (current_buffer);
13929 /* Normally the message* functions will have already displayed and
13930 updated the echo area, but the frame may have been trashed, or
13931 the update may have been preempted, so display the echo area
13932 again here. Checking message_cleared_p captures the case that
13933 the echo area should be cleared. */
13934 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13935 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13936 || (message_cleared_p
13937 && minibuf_level == 0
13938 /* If the mini-window is currently selected, this means the
13939 echo-area doesn't show through. */
13940 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13942 echo_area_display (false);
13944 /* If echo_area_display resizes the mini-window, the redisplay and
13945 window_sizes_changed flags of the selected frame are set, but
13946 it's too late for the hooks in window-size-change-functions,
13947 which have been examined already in prepare_menu_bars. So in
13948 that case we call the hooks here only for the selected frame. */
13949 if (sf->redisplay)
13951 ptrdiff_t count1 = SPECPDL_INDEX ();
13953 record_unwind_save_match_data ();
13954 run_window_size_change_functions (selected_frame);
13955 unbind_to (count1, Qnil);
13958 if (message_cleared_p)
13959 update_miniwindow_p = true;
13961 must_finish = true;
13963 /* If we don't display the current message, don't clear the
13964 message_cleared_p flag, because, if we did, we wouldn't clear
13965 the echo area in the next redisplay which doesn't preserve
13966 the echo area. */
13967 if (!display_last_displayed_message_p)
13968 message_cleared_p = false;
13970 else if (EQ (selected_window, minibuf_window)
13971 && (current_buffer->clip_changed || window_outdated (w))
13972 && resize_mini_window (w, false))
13974 if (sf->redisplay)
13976 ptrdiff_t count1 = SPECPDL_INDEX ();
13978 record_unwind_save_match_data ();
13979 run_window_size_change_functions (selected_frame);
13980 unbind_to (count1, Qnil);
13983 /* Resized active mini-window to fit the size of what it is
13984 showing if its contents might have changed. */
13985 must_finish = true;
13987 /* If window configuration was changed, frames may have been
13988 marked garbaged. Clear them or we will experience
13989 surprises wrt scrolling. */
13990 clear_garbaged_frames ();
13993 if (windows_or_buffers_changed && !update_mode_lines)
13994 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13995 only the windows's contents needs to be refreshed, or whether the
13996 mode-lines also need a refresh. */
13997 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13998 ? REDISPLAY_SOME : 32);
14000 /* If specs for an arrow have changed, do thorough redisplay
14001 to ensure we remove any arrow that should no longer exist. */
14002 /* Apparently, this is the only case where we update other windows,
14003 without updating other mode-lines. */
14004 overlay_arrows_changed_p (true);
14006 consider_all_windows_p = (update_mode_lines
14007 || windows_or_buffers_changed);
14009 #define AINC(a,i) \
14011 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14012 if (INTEGERP (entry)) \
14013 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14016 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14017 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14019 /* Optimize the case that only the line containing the cursor in the
14020 selected window has changed. Variables starting with this_ are
14021 set in display_line and record information about the line
14022 containing the cursor. */
14023 tlbufpos = this_line_start_pos;
14024 tlendpos = this_line_end_pos;
14025 if (!consider_all_windows_p
14026 && CHARPOS (tlbufpos) > 0
14027 && !w->update_mode_line
14028 && !current_buffer->clip_changed
14029 && !current_buffer->prevent_redisplay_optimizations_p
14030 && FRAME_VISIBLE_P (XFRAME (w->frame))
14031 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14032 && !XFRAME (w->frame)->cursor_type_changed
14033 && !XFRAME (w->frame)->face_change
14034 /* Make sure recorded data applies to current buffer, etc. */
14035 && this_line_buffer == current_buffer
14036 && match_p
14037 && !w->force_start
14038 && !w->optional_new_start
14039 /* Point must be on the line that we have info recorded about. */
14040 && PT >= CHARPOS (tlbufpos)
14041 && PT <= Z - CHARPOS (tlendpos)
14042 /* All text outside that line, including its final newline,
14043 must be unchanged. */
14044 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14045 CHARPOS (tlendpos)))
14047 if (CHARPOS (tlbufpos) > BEGV
14048 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14049 && (CHARPOS (tlbufpos) == ZV
14050 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14051 /* Former continuation line has disappeared by becoming empty. */
14052 goto cancel;
14053 else if (window_outdated (w) || MINI_WINDOW_P (w))
14055 /* We have to handle the case of continuation around a
14056 wide-column character (see the comment in indent.c around
14057 line 1340).
14059 For instance, in the following case:
14061 -------- Insert --------
14062 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14063 J_I_ ==> J_I_ `^^' are cursors.
14064 ^^ ^^
14065 -------- --------
14067 As we have to redraw the line above, we cannot use this
14068 optimization. */
14070 struct it it;
14071 int line_height_before = this_line_pixel_height;
14073 /* Note that start_display will handle the case that the
14074 line starting at tlbufpos is a continuation line. */
14075 start_display (&it, w, tlbufpos);
14077 /* Implementation note: It this still necessary? */
14078 if (it.current_x != this_line_start_x)
14079 goto cancel;
14081 TRACE ((stderr, "trying display optimization 1\n"));
14082 w->cursor.vpos = -1;
14083 overlay_arrow_seen = false;
14084 it.vpos = this_line_vpos;
14085 it.current_y = this_line_y;
14086 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14087 display_line (&it, -1);
14089 /* If line contains point, is not continued,
14090 and ends at same distance from eob as before, we win. */
14091 if (w->cursor.vpos >= 0
14092 /* Line is not continued, otherwise this_line_start_pos
14093 would have been set to 0 in display_line. */
14094 && CHARPOS (this_line_start_pos)
14095 /* Line ends as before. */
14096 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14097 /* Line has same height as before. Otherwise other lines
14098 would have to be shifted up or down. */
14099 && this_line_pixel_height == line_height_before)
14101 /* If this is not the window's last line, we must adjust
14102 the charstarts of the lines below. */
14103 if (it.current_y < it.last_visible_y)
14105 struct glyph_row *row
14106 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14107 ptrdiff_t delta, delta_bytes;
14109 /* We used to distinguish between two cases here,
14110 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14111 when the line ends in a newline or the end of the
14112 buffer's accessible portion. But both cases did
14113 the same, so they were collapsed. */
14114 delta = (Z
14115 - CHARPOS (tlendpos)
14116 - MATRIX_ROW_START_CHARPOS (row));
14117 delta_bytes = (Z_BYTE
14118 - BYTEPOS (tlendpos)
14119 - MATRIX_ROW_START_BYTEPOS (row));
14121 increment_matrix_positions (w->current_matrix,
14122 this_line_vpos + 1,
14123 w->current_matrix->nrows,
14124 delta, delta_bytes);
14127 /* If this row displays text now but previously didn't,
14128 or vice versa, w->window_end_vpos may have to be
14129 adjusted. */
14130 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14132 if (w->window_end_vpos < this_line_vpos)
14133 w->window_end_vpos = this_line_vpos;
14135 else if (w->window_end_vpos == this_line_vpos
14136 && this_line_vpos > 0)
14137 w->window_end_vpos = this_line_vpos - 1;
14138 w->window_end_valid = false;
14140 /* Update hint: No need to try to scroll in update_window. */
14141 w->desired_matrix->no_scrolling_p = true;
14143 #ifdef GLYPH_DEBUG
14144 *w->desired_matrix->method = 0;
14145 debug_method_add (w, "optimization 1");
14146 #endif
14147 #ifdef HAVE_WINDOW_SYSTEM
14148 update_window_fringes (w, false);
14149 #endif
14150 goto update;
14152 else
14153 goto cancel;
14155 else if (/* Cursor position hasn't changed. */
14156 PT == w->last_point
14157 /* Make sure the cursor was last displayed
14158 in this window. Otherwise we have to reposition it. */
14160 /* PXW: Must be converted to pixels, probably. */
14161 && 0 <= w->cursor.vpos
14162 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14164 if (!must_finish)
14166 do_pending_window_change (true);
14167 /* If selected_window changed, redisplay again. */
14168 if (WINDOWP (selected_window)
14169 && (w = XWINDOW (selected_window)) != sw)
14170 goto retry;
14172 /* We used to always goto end_of_redisplay here, but this
14173 isn't enough if we have a blinking cursor. */
14174 if (w->cursor_off_p == w->last_cursor_off_p)
14175 goto end_of_redisplay;
14177 goto update;
14179 /* If highlighting the region, or if the cursor is in the echo area,
14180 then we can't just move the cursor. */
14181 else if (NILP (Vshow_trailing_whitespace)
14182 && !cursor_in_echo_area)
14184 struct it it;
14185 struct glyph_row *row;
14187 /* Skip from tlbufpos to PT and see where it is. Note that
14188 PT may be in invisible text. If so, we will end at the
14189 next visible position. */
14190 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14191 NULL, DEFAULT_FACE_ID);
14192 it.current_x = this_line_start_x;
14193 it.current_y = this_line_y;
14194 it.vpos = this_line_vpos;
14196 /* The call to move_it_to stops in front of PT, but
14197 moves over before-strings. */
14198 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14200 if (it.vpos == this_line_vpos
14201 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14202 row->enabled_p))
14204 eassert (this_line_vpos == it.vpos);
14205 eassert (this_line_y == it.current_y);
14206 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14207 if (cursor_row_fully_visible_p (w, false, true))
14209 #ifdef GLYPH_DEBUG
14210 *w->desired_matrix->method = 0;
14211 debug_method_add (w, "optimization 3");
14212 #endif
14213 goto update;
14215 else
14216 goto cancel;
14218 else
14219 goto cancel;
14222 cancel:
14223 /* Text changed drastically or point moved off of line. */
14224 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14227 CHARPOS (this_line_start_pos) = 0;
14228 ++clear_face_cache_count;
14229 #ifdef HAVE_WINDOW_SYSTEM
14230 ++clear_image_cache_count;
14231 #endif
14233 /* Build desired matrices, and update the display. If
14234 consider_all_windows_p, do it for all windows on all frames that
14235 require redisplay, as specified by their 'redisplay' flag.
14236 Otherwise do it for selected_window, only. */
14238 if (consider_all_windows_p)
14240 FOR_EACH_FRAME (tail, frame)
14241 XFRAME (frame)->updated_p = false;
14243 propagate_buffer_redisplay ();
14245 FOR_EACH_FRAME (tail, frame)
14247 struct frame *f = XFRAME (frame);
14249 /* We don't have to do anything for unselected terminal
14250 frames. */
14251 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14252 && !EQ (FRAME_TTY (f)->top_frame, frame))
14253 continue;
14255 retry_frame:
14256 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14258 bool gcscrollbars
14259 /* Only GC scrollbars when we redisplay the whole frame. */
14260 = f->redisplay || !REDISPLAY_SOME_P ();
14261 bool f_redisplay_flag = f->redisplay;
14262 /* Mark all the scroll bars to be removed; we'll redeem
14263 the ones we want when we redisplay their windows. */
14264 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14265 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14267 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14268 redisplay_windows (FRAME_ROOT_WINDOW (f));
14269 /* Remember that the invisible frames need to be redisplayed next
14270 time they're visible. */
14271 else if (!REDISPLAY_SOME_P ())
14272 f->redisplay = true;
14274 /* The X error handler may have deleted that frame. */
14275 if (!FRAME_LIVE_P (f))
14276 continue;
14278 /* Any scroll bars which redisplay_windows should have
14279 nuked should now go away. */
14280 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14281 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14283 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14285 /* If fonts changed on visible frame, display again. */
14286 if (f->fonts_changed)
14288 adjust_frame_glyphs (f);
14289 /* Disable all redisplay optimizations for this
14290 frame. For the reasons, see the comment near
14291 the previous call to adjust_frame_glyphs above. */
14292 SET_FRAME_GARBAGED (f);
14293 f->fonts_changed = false;
14294 goto retry_frame;
14297 /* See if we have to hscroll. */
14298 if (!f->already_hscrolled_p)
14300 f->already_hscrolled_p = true;
14301 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14302 && hscroll_windows (f->root_window))
14304 hscroll_retries++;
14305 goto retry_frame;
14309 /* If the frame's redisplay flag was not set before
14310 we went about redisplaying its windows, but it is
14311 set now, that means we employed some redisplay
14312 optimizations inside redisplay_windows, and
14313 bypassed producing some screen lines. But if
14314 f->redisplay is now set, it might mean the old
14315 faces are no longer valid (e.g., if redisplaying
14316 some window called some Lisp which defined a new
14317 face or redefined an existing face), so trying to
14318 use them in update_frame will segfault.
14319 Therefore, we must redisplay this frame. */
14320 if (!f_redisplay_flag && f->redisplay)
14321 goto retry_frame;
14322 /* In some case (e.g., window resize), we notice
14323 only during window updating that the window
14324 content changed unpredictably (e.g., a GTK
14325 scrollbar moved, or some Lisp hook that winds up
14326 calling adjust_frame_glyphs) and that our
14327 previous estimation of the frame content was
14328 garbage. We have to start over. These cases
14329 should be rare, so going all the way back to the
14330 top of redisplay should be good enough. */
14331 if (FRAME_GARBAGED_P (f)
14332 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14333 goto retry;
14335 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14336 x_clear_under_internal_border (f);
14337 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14339 /* Prevent various kinds of signals during display
14340 update. stdio is not robust about handling
14341 signals, which can cause an apparent I/O error. */
14342 if (interrupt_input)
14343 unrequest_sigio ();
14344 STOP_POLLING;
14346 pending |= update_frame (f, false, false);
14347 f->cursor_type_changed = false;
14348 f->updated_p = true;
14353 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14355 if (!pending)
14357 /* Do the mark_window_display_accurate after all windows have
14358 been redisplayed because this call resets flags in buffers
14359 which are needed for proper redisplay. */
14360 FOR_EACH_FRAME (tail, frame)
14362 struct frame *f = XFRAME (frame);
14363 if (f->updated_p)
14365 f->redisplay = false;
14366 f->garbaged = false;
14367 mark_window_display_accurate (f->root_window, true);
14368 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14369 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14374 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14376 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14377 /* Use list_of_error, not Qerror, so that
14378 we catch only errors and don't run the debugger. */
14379 internal_condition_case_1 (redisplay_window_1, selected_window,
14380 list_of_error,
14381 redisplay_window_error);
14382 if (update_miniwindow_p)
14383 internal_condition_case_1 (redisplay_window_1,
14384 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14385 redisplay_window_error);
14387 /* Compare desired and current matrices, perform output. */
14389 update:
14390 /* If fonts changed, display again. Likewise if redisplay_window_1
14391 above caused some change (e.g., a change in faces) that requires
14392 considering the entire frame again. */
14393 if (sf->fonts_changed || sf->redisplay)
14395 if (sf->redisplay)
14397 /* Set this to force a more thorough redisplay.
14398 Otherwise, we might immediately loop back to the
14399 above "else-if" clause (since all the conditions that
14400 led here might still be true), and we will then
14401 infloop, because the selected-frame's redisplay flag
14402 is not (and cannot be) reset. */
14403 windows_or_buffers_changed = 50;
14405 goto retry;
14408 /* Prevent freeing of realized faces, since desired matrices are
14409 pending that reference the faces we computed and cached. */
14410 inhibit_free_realized_faces = true;
14412 /* Prevent various kinds of signals during display update.
14413 stdio is not robust about handling signals,
14414 which can cause an apparent I/O error. */
14415 if (interrupt_input)
14416 unrequest_sigio ();
14417 STOP_POLLING;
14419 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14421 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14422 && hscroll_windows (selected_window))
14424 hscroll_retries++;
14425 goto retry;
14428 XWINDOW (selected_window)->must_be_updated_p = true;
14429 pending = update_frame (sf, false, false);
14430 sf->cursor_type_changed = false;
14433 /* We may have called echo_area_display at the top of this
14434 function. If the echo area is on another frame, that may
14435 have put text on a frame other than the selected one, so the
14436 above call to update_frame would not have caught it. Catch
14437 it here. */
14438 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14439 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14441 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14443 XWINDOW (mini_window)->must_be_updated_p = true;
14444 pending |= update_frame (mini_frame, false, false);
14445 mini_frame->cursor_type_changed = false;
14446 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14447 && hscroll_windows (mini_window))
14449 hscroll_retries++;
14450 goto retry;
14455 /* If display was paused because of pending input, make sure we do a
14456 thorough update the next time. */
14457 if (pending)
14459 /* Prevent the optimization at the beginning of
14460 redisplay_internal that tries a single-line update of the
14461 line containing the cursor in the selected window. */
14462 CHARPOS (this_line_start_pos) = 0;
14464 /* Let the overlay arrow be updated the next time. */
14465 update_overlay_arrows (0);
14467 /* If we pause after scrolling, some rows in the current
14468 matrices of some windows are not valid. */
14469 if (!WINDOW_FULL_WIDTH_P (w)
14470 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14471 update_mode_lines = 36;
14473 else
14475 if (!consider_all_windows_p)
14477 /* This has already been done above if
14478 consider_all_windows_p is set. */
14479 if (XBUFFER (w->contents)->text->redisplay
14480 && buffer_window_count (XBUFFER (w->contents)) > 1)
14481 /* This can happen if b->text->redisplay was set during
14482 jit-lock. */
14483 propagate_buffer_redisplay ();
14484 mark_window_display_accurate_1 (w, true);
14486 /* Say overlay arrows are up to date. */
14487 update_overlay_arrows (1);
14489 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14490 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14493 update_mode_lines = 0;
14494 windows_or_buffers_changed = 0;
14497 /* Start SIGIO interrupts coming again. Having them off during the
14498 code above makes it less likely one will discard output, but not
14499 impossible, since there might be stuff in the system buffer here.
14500 But it is much hairier to try to do anything about that. */
14501 if (interrupt_input)
14502 request_sigio ();
14503 RESUME_POLLING;
14505 /* If a frame has become visible which was not before, redisplay
14506 again, so that we display it. Expose events for such a frame
14507 (which it gets when becoming visible) don't call the parts of
14508 redisplay constructing glyphs, so simply exposing a frame won't
14509 display anything in this case. So, we have to display these
14510 frames here explicitly. */
14511 if (!pending)
14513 int new_count = 0;
14515 FOR_EACH_FRAME (tail, frame)
14517 if (XFRAME (frame)->visible)
14518 new_count++;
14521 if (new_count != number_of_visible_frames)
14522 windows_or_buffers_changed = 52;
14525 /* Change frame size now if a change is pending. */
14526 do_pending_window_change (true);
14528 /* If we just did a pending size change, or have additional
14529 visible frames, or selected_window changed, redisplay again. */
14530 if ((windows_or_buffers_changed && !pending)
14531 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14532 goto retry;
14534 /* Clear the face and image caches.
14536 We used to do this only if consider_all_windows_p. But the cache
14537 needs to be cleared if a timer creates images in the current
14538 buffer (e.g. the test case in Bug#6230). */
14540 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14542 clear_face_cache (false);
14543 clear_face_cache_count = 0;
14546 #ifdef HAVE_WINDOW_SYSTEM
14547 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14549 clear_image_caches (Qnil);
14550 clear_image_cache_count = 0;
14552 #endif /* HAVE_WINDOW_SYSTEM */
14554 end_of_redisplay:
14555 #ifdef HAVE_NS
14556 ns_set_doc_edited ();
14557 #endif
14558 if (interrupt_input && interrupts_deferred)
14559 request_sigio ();
14561 unbind_to (count, Qnil);
14562 RESUME_POLLING;
14565 static void
14566 unwind_redisplay_preserve_echo_area (void)
14568 unblock_buffer_flips ();
14571 /* Redisplay, but leave alone any recent echo area message unless
14572 another message has been requested in its place.
14574 This is useful in situations where you need to redisplay but no
14575 user action has occurred, making it inappropriate for the message
14576 area to be cleared. See tracking_off and
14577 wait_reading_process_output for examples of these situations.
14579 FROM_WHERE is an integer saying from where this function was
14580 called. This is useful for debugging. */
14582 void
14583 redisplay_preserve_echo_area (int from_where)
14585 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14587 block_input ();
14588 ptrdiff_t count = SPECPDL_INDEX ();
14589 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14590 block_buffer_flips ();
14591 unblock_input ();
14593 if (!NILP (echo_area_buffer[1]))
14595 /* We have a previously displayed message, but no current
14596 message. Redisplay the previous message. */
14597 display_last_displayed_message_p = true;
14598 redisplay_internal ();
14599 display_last_displayed_message_p = false;
14601 else
14602 redisplay_internal ();
14604 flush_frame (SELECTED_FRAME ());
14605 unbind_to (count, Qnil);
14609 /* Function registered with record_unwind_protect in redisplay_internal. */
14611 static void
14612 unwind_redisplay (void)
14614 redisplaying_p = false;
14615 unblock_buffer_flips ();
14619 /* Mark the display of leaf window W as accurate or inaccurate.
14620 If ACCURATE_P, mark display of W as accurate.
14621 If !ACCURATE_P, arrange for W to be redisplayed the next
14622 time redisplay_internal is called. */
14624 static void
14625 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14627 struct buffer *b = XBUFFER (w->contents);
14629 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14630 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14631 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14633 if (accurate_p)
14635 b->clip_changed = false;
14636 b->prevent_redisplay_optimizations_p = false;
14637 eassert (buffer_window_count (b) > 0);
14638 /* Resetting b->text->redisplay is problematic!
14639 In order to make it safer to do it here, redisplay_internal must
14640 have copied all b->text->redisplay to their respective windows. */
14641 b->text->redisplay = false;
14643 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14644 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14645 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14646 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14648 w->current_matrix->buffer = b;
14649 w->current_matrix->begv = BUF_BEGV (b);
14650 w->current_matrix->zv = BUF_ZV (b);
14652 w->last_cursor_vpos = w->cursor.vpos;
14653 w->last_cursor_off_p = w->cursor_off_p;
14655 if (w == XWINDOW (selected_window))
14656 w->last_point = BUF_PT (b);
14657 else
14658 w->last_point = marker_position (w->pointm);
14660 w->window_end_valid = true;
14661 w->update_mode_line = false;
14664 w->redisplay = !accurate_p;
14668 /* Mark the display of windows in the window tree rooted at WINDOW as
14669 accurate or inaccurate. If ACCURATE_P, mark display of
14670 windows as accurate. If !ACCURATE_P, arrange for windows to
14671 be redisplayed the next time redisplay_internal is called. */
14673 void
14674 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14676 struct window *w;
14678 for (; !NILP (window); window = w->next)
14680 w = XWINDOW (window);
14681 if (WINDOWP (w->contents))
14682 mark_window_display_accurate (w->contents, accurate_p);
14683 else
14684 mark_window_display_accurate_1 (w, accurate_p);
14687 if (accurate_p)
14688 update_overlay_arrows (1);
14689 else
14690 /* Force a thorough redisplay the next time by setting
14691 last_arrow_position and last_arrow_string to t, which is
14692 unequal to any useful value of Voverlay_arrow_... */
14693 update_overlay_arrows (-1);
14697 /* Return value in display table DP (Lisp_Char_Table *) for character
14698 C. Since a display table doesn't have any parent, we don't have to
14699 follow parent. Do not call this function directly but use the
14700 macro DISP_CHAR_VECTOR. */
14702 Lisp_Object
14703 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14705 Lisp_Object val;
14707 if (ASCII_CHAR_P (c))
14709 val = dp->ascii;
14710 if (SUB_CHAR_TABLE_P (val))
14711 val = XSUB_CHAR_TABLE (val)->contents[c];
14713 else
14715 Lisp_Object table;
14717 XSETCHAR_TABLE (table, dp);
14718 val = char_table_ref (table, c);
14720 if (NILP (val))
14721 val = dp->defalt;
14722 return val;
14725 static int buffer_flip_blocked_depth;
14727 static void
14728 block_buffer_flips (void)
14730 eassert (buffer_flip_blocked_depth >= 0);
14731 buffer_flip_blocked_depth++;
14734 static void
14735 unblock_buffer_flips (void)
14737 eassert (buffer_flip_blocked_depth > 0);
14738 if (--buffer_flip_blocked_depth == 0)
14740 Lisp_Object tail, frame;
14741 block_input ();
14742 FOR_EACH_FRAME (tail, frame)
14744 struct frame *f = XFRAME (frame);
14745 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14746 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14748 unblock_input ();
14752 bool
14753 buffer_flipping_blocked_p (void)
14755 return buffer_flip_blocked_depth > 0;
14759 /***********************************************************************
14760 Window Redisplay
14761 ***********************************************************************/
14763 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14765 static void
14766 redisplay_windows (Lisp_Object window)
14768 while (!NILP (window))
14770 struct window *w = XWINDOW (window);
14772 if (WINDOWP (w->contents))
14773 redisplay_windows (w->contents);
14774 else if (BUFFERP (w->contents))
14776 displayed_buffer = XBUFFER (w->contents);
14777 /* Use list_of_error, not Qerror, so that
14778 we catch only errors and don't run the debugger. */
14779 internal_condition_case_1 (redisplay_window_0, window,
14780 list_of_error,
14781 redisplay_window_error);
14784 window = w->next;
14788 static Lisp_Object
14789 redisplay_window_error (Lisp_Object ignore)
14791 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14792 return Qnil;
14795 static Lisp_Object
14796 redisplay_window_0 (Lisp_Object window)
14798 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14799 redisplay_window (window, false);
14800 return Qnil;
14803 static Lisp_Object
14804 redisplay_window_1 (Lisp_Object window)
14806 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14807 redisplay_window (window, true);
14808 return Qnil;
14812 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14813 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14814 which positions recorded in ROW differ from current buffer
14815 positions.
14817 Return true iff cursor is on this row. */
14819 static bool
14820 set_cursor_from_row (struct window *w, struct glyph_row *row,
14821 struct glyph_matrix *matrix,
14822 ptrdiff_t delta, ptrdiff_t delta_bytes,
14823 int dy, int dvpos)
14825 struct glyph *glyph = row->glyphs[TEXT_AREA];
14826 struct glyph *end = glyph + row->used[TEXT_AREA];
14827 struct glyph *cursor = NULL;
14828 /* The last known character position in row. */
14829 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14830 int x = row->x;
14831 ptrdiff_t pt_old = PT - delta;
14832 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14833 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14834 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14835 /* A glyph beyond the edge of TEXT_AREA which we should never
14836 touch. */
14837 struct glyph *glyphs_end = end;
14838 /* True means we've found a match for cursor position, but that
14839 glyph has the avoid_cursor_p flag set. */
14840 bool match_with_avoid_cursor = false;
14841 /* True means we've seen at least one glyph that came from a
14842 display string. */
14843 bool string_seen = false;
14844 /* Largest and smallest buffer positions seen so far during scan of
14845 glyph row. */
14846 ptrdiff_t bpos_max = pos_before;
14847 ptrdiff_t bpos_min = pos_after;
14848 /* Last buffer position covered by an overlay string with an integer
14849 `cursor' property. */
14850 ptrdiff_t bpos_covered = 0;
14851 /* True means the display string on which to display the cursor
14852 comes from a text property, not from an overlay. */
14853 bool string_from_text_prop = false;
14855 /* Don't even try doing anything if called for a mode-line or
14856 header-line row, since the rest of the code isn't prepared to
14857 deal with such calamities. */
14858 eassert (!row->mode_line_p);
14859 if (row->mode_line_p)
14860 return false;
14862 /* Skip over glyphs not having an object at the start and the end of
14863 the row. These are special glyphs like truncation marks on
14864 terminal frames. */
14865 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14867 if (!row->reversed_p)
14869 while (glyph < end
14870 && NILP (glyph->object)
14871 && glyph->charpos < 0)
14873 x += glyph->pixel_width;
14874 ++glyph;
14876 while (end > glyph
14877 && NILP ((end - 1)->object)
14878 /* CHARPOS is zero for blanks and stretch glyphs
14879 inserted by extend_face_to_end_of_line. */
14880 && (end - 1)->charpos <= 0)
14881 --end;
14882 glyph_before = glyph - 1;
14883 glyph_after = end;
14885 else
14887 struct glyph *g;
14889 /* If the glyph row is reversed, we need to process it from back
14890 to front, so swap the edge pointers. */
14891 glyphs_end = end = glyph - 1;
14892 glyph += row->used[TEXT_AREA] - 1;
14894 while (glyph > end + 1
14895 && NILP (glyph->object)
14896 && glyph->charpos < 0)
14897 --glyph;
14898 if (NILP (glyph->object) && glyph->charpos < 0)
14899 --glyph;
14900 /* By default, in reversed rows we put the cursor on the
14901 rightmost (first in the reading order) glyph. */
14902 for (x = 0, g = end + 1; g < glyph; g++)
14903 x += g->pixel_width;
14904 while (end < glyph
14905 && NILP ((end + 1)->object)
14906 && (end + 1)->charpos <= 0)
14907 ++end;
14908 glyph_before = glyph + 1;
14909 glyph_after = end;
14912 else if (row->reversed_p)
14914 /* In R2L rows that don't display text, put the cursor on the
14915 rightmost glyph. Case in point: an empty last line that is
14916 part of an R2L paragraph. */
14917 cursor = end - 1;
14918 /* Avoid placing the cursor on the last glyph of the row, where
14919 on terminal frames we hold the vertical border between
14920 adjacent windows. */
14921 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14922 && !WINDOW_RIGHTMOST_P (w)
14923 && cursor == row->glyphs[LAST_AREA] - 1)
14924 cursor--;
14925 x = -1; /* will be computed below, at label compute_x */
14928 /* Step 1: Try to find the glyph whose character position
14929 corresponds to point. If that's not possible, find 2 glyphs
14930 whose character positions are the closest to point, one before
14931 point, the other after it. */
14932 if (!row->reversed_p)
14933 while (/* not marched to end of glyph row */
14934 glyph < end
14935 /* glyph was not inserted by redisplay for internal purposes */
14936 && !NILP (glyph->object))
14938 if (BUFFERP (glyph->object))
14940 ptrdiff_t dpos = glyph->charpos - pt_old;
14942 if (glyph->charpos > bpos_max)
14943 bpos_max = glyph->charpos;
14944 if (glyph->charpos < bpos_min)
14945 bpos_min = glyph->charpos;
14946 if (!glyph->avoid_cursor_p)
14948 /* If we hit point, we've found the glyph on which to
14949 display the cursor. */
14950 if (dpos == 0)
14952 match_with_avoid_cursor = false;
14953 break;
14955 /* See if we've found a better approximation to
14956 POS_BEFORE or to POS_AFTER. */
14957 if (0 > dpos && dpos > pos_before - pt_old)
14959 pos_before = glyph->charpos;
14960 glyph_before = glyph;
14962 else if (0 < dpos && dpos < pos_after - pt_old)
14964 pos_after = glyph->charpos;
14965 glyph_after = glyph;
14968 else if (dpos == 0)
14969 match_with_avoid_cursor = true;
14971 else if (STRINGP (glyph->object))
14973 Lisp_Object chprop;
14974 ptrdiff_t glyph_pos = glyph->charpos;
14976 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14977 glyph->object);
14978 if (!NILP (chprop))
14980 /* If the string came from a `display' text property,
14981 look up the buffer position of that property and
14982 use that position to update bpos_max, as if we
14983 actually saw such a position in one of the row's
14984 glyphs. This helps with supporting integer values
14985 of `cursor' property on the display string in
14986 situations where most or all of the row's buffer
14987 text is completely covered by display properties,
14988 so that no glyph with valid buffer positions is
14989 ever seen in the row. */
14990 ptrdiff_t prop_pos =
14991 string_buffer_position_lim (glyph->object, pos_before,
14992 pos_after, false);
14994 if (prop_pos >= pos_before)
14995 bpos_max = prop_pos;
14997 if (INTEGERP (chprop))
14999 bpos_covered = bpos_max + XINT (chprop);
15000 /* If the `cursor' property covers buffer positions up
15001 to and including point, we should display cursor on
15002 this glyph. Note that, if a `cursor' property on one
15003 of the string's characters has an integer value, we
15004 will break out of the loop below _before_ we get to
15005 the position match above. IOW, integer values of
15006 the `cursor' property override the "exact match for
15007 point" strategy of positioning the cursor. */
15008 /* Implementation note: bpos_max == pt_old when, e.g.,
15009 we are in an empty line, where bpos_max is set to
15010 MATRIX_ROW_START_CHARPOS, see above. */
15011 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15013 cursor = glyph;
15014 break;
15018 string_seen = true;
15020 x += glyph->pixel_width;
15021 ++glyph;
15023 else if (glyph > end) /* row is reversed */
15024 while (!NILP (glyph->object))
15026 if (BUFFERP (glyph->object))
15028 ptrdiff_t dpos = glyph->charpos - pt_old;
15030 if (glyph->charpos > bpos_max)
15031 bpos_max = glyph->charpos;
15032 if (glyph->charpos < bpos_min)
15033 bpos_min = glyph->charpos;
15034 if (!glyph->avoid_cursor_p)
15036 if (dpos == 0)
15038 match_with_avoid_cursor = false;
15039 break;
15041 if (0 > dpos && dpos > pos_before - pt_old)
15043 pos_before = glyph->charpos;
15044 glyph_before = glyph;
15046 else if (0 < dpos && dpos < pos_after - pt_old)
15048 pos_after = glyph->charpos;
15049 glyph_after = glyph;
15052 else if (dpos == 0)
15053 match_with_avoid_cursor = true;
15055 else if (STRINGP (glyph->object))
15057 Lisp_Object chprop;
15058 ptrdiff_t glyph_pos = glyph->charpos;
15060 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15061 glyph->object);
15062 if (!NILP (chprop))
15064 ptrdiff_t prop_pos =
15065 string_buffer_position_lim (glyph->object, pos_before,
15066 pos_after, false);
15068 if (prop_pos >= pos_before)
15069 bpos_max = prop_pos;
15071 if (INTEGERP (chprop))
15073 bpos_covered = bpos_max + XINT (chprop);
15074 /* If the `cursor' property covers buffer positions up
15075 to and including point, we should display cursor on
15076 this glyph. */
15077 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15079 cursor = glyph;
15080 break;
15083 string_seen = true;
15085 --glyph;
15086 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15088 x--; /* can't use any pixel_width */
15089 break;
15091 x -= glyph->pixel_width;
15094 /* Step 2: If we didn't find an exact match for point, we need to
15095 look for a proper place to put the cursor among glyphs between
15096 GLYPH_BEFORE and GLYPH_AFTER. */
15097 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15098 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15099 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15101 /* An empty line has a single glyph whose OBJECT is nil and
15102 whose CHARPOS is the position of a newline on that line.
15103 Note that on a TTY, there are more glyphs after that, which
15104 were produced by extend_face_to_end_of_line, but their
15105 CHARPOS is zero or negative. */
15106 bool empty_line_p =
15107 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15108 && NILP (glyph->object) && glyph->charpos > 0
15109 /* On a TTY, continued and truncated rows also have a glyph at
15110 their end whose OBJECT is nil and whose CHARPOS is
15111 positive (the continuation and truncation glyphs), but such
15112 rows are obviously not "empty". */
15113 && !(row->continued_p || row->truncated_on_right_p));
15115 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15117 ptrdiff_t ellipsis_pos;
15119 /* Scan back over the ellipsis glyphs. */
15120 if (!row->reversed_p)
15122 ellipsis_pos = (glyph - 1)->charpos;
15123 while (glyph > row->glyphs[TEXT_AREA]
15124 && (glyph - 1)->charpos == ellipsis_pos)
15125 glyph--, x -= glyph->pixel_width;
15126 /* That loop always goes one position too far, including
15127 the glyph before the ellipsis. So scan forward over
15128 that one. */
15129 x += glyph->pixel_width;
15130 glyph++;
15132 else /* row is reversed */
15134 ellipsis_pos = (glyph + 1)->charpos;
15135 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15136 && (glyph + 1)->charpos == ellipsis_pos)
15137 glyph++, x += glyph->pixel_width;
15138 x -= glyph->pixel_width;
15139 glyph--;
15142 else if (match_with_avoid_cursor)
15144 cursor = glyph_after;
15145 x = -1;
15147 else if (string_seen)
15149 int incr = row->reversed_p ? -1 : +1;
15151 /* Need to find the glyph that came out of a string which is
15152 present at point. That glyph is somewhere between
15153 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15154 positioned between POS_BEFORE and POS_AFTER in the
15155 buffer. */
15156 struct glyph *start, *stop;
15157 ptrdiff_t pos = pos_before;
15159 x = -1;
15161 /* If the row ends in a newline from a display string,
15162 reordering could have moved the glyphs belonging to the
15163 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15164 in this case we extend the search to the last glyph in
15165 the row that was not inserted by redisplay. */
15166 if (row->ends_in_newline_from_string_p)
15168 glyph_after = end;
15169 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15172 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15173 correspond to POS_BEFORE and POS_AFTER, respectively. We
15174 need START and STOP in the order that corresponds to the
15175 row's direction as given by its reversed_p flag. If the
15176 directionality of characters between POS_BEFORE and
15177 POS_AFTER is the opposite of the row's base direction,
15178 these characters will have been reordered for display,
15179 and we need to reverse START and STOP. */
15180 if (!row->reversed_p)
15182 start = min (glyph_before, glyph_after);
15183 stop = max (glyph_before, glyph_after);
15185 else
15187 start = max (glyph_before, glyph_after);
15188 stop = min (glyph_before, glyph_after);
15190 for (glyph = start + incr;
15191 row->reversed_p ? glyph > stop : glyph < stop; )
15194 /* Any glyphs that come from the buffer are here because
15195 of bidi reordering. Skip them, and only pay
15196 attention to glyphs that came from some string. */
15197 if (STRINGP (glyph->object))
15199 Lisp_Object str;
15200 ptrdiff_t tem;
15201 /* If the display property covers the newline, we
15202 need to search for it one position farther. */
15203 ptrdiff_t lim = pos_after
15204 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15206 string_from_text_prop = false;
15207 str = glyph->object;
15208 tem = string_buffer_position_lim (str, pos, lim, false);
15209 if (tem == 0 /* from overlay */
15210 || pos <= tem)
15212 /* If the string from which this glyph came is
15213 found in the buffer at point, or at position
15214 that is closer to point than pos_after, then
15215 we've found the glyph we've been looking for.
15216 If it comes from an overlay (tem == 0), and
15217 it has the `cursor' property on one of its
15218 glyphs, record that glyph as a candidate for
15219 displaying the cursor. (As in the
15220 unidirectional version, we will display the
15221 cursor on the last candidate we find.) */
15222 if (tem == 0
15223 || tem == pt_old
15224 || (tem - pt_old > 0 && tem < pos_after))
15226 /* The glyphs from this string could have
15227 been reordered. Find the one with the
15228 smallest string position. Or there could
15229 be a character in the string with the
15230 `cursor' property, which means display
15231 cursor on that character's glyph. */
15232 ptrdiff_t strpos = glyph->charpos;
15234 if (tem)
15236 cursor = glyph;
15237 string_from_text_prop = true;
15239 for ( ;
15240 (row->reversed_p ? glyph > stop : glyph < stop)
15241 && EQ (glyph->object, str);
15242 glyph += incr)
15244 Lisp_Object cprop;
15245 ptrdiff_t gpos = glyph->charpos;
15247 cprop = Fget_char_property (make_number (gpos),
15248 Qcursor,
15249 glyph->object);
15250 if (!NILP (cprop))
15252 cursor = glyph;
15253 break;
15255 if (tem && glyph->charpos < strpos)
15257 strpos = glyph->charpos;
15258 cursor = glyph;
15262 if (tem == pt_old
15263 || (tem - pt_old > 0 && tem < pos_after))
15264 goto compute_x;
15266 if (tem)
15267 pos = tem + 1; /* don't find previous instances */
15269 /* This string is not what we want; skip all of the
15270 glyphs that came from it. */
15271 while ((row->reversed_p ? glyph > stop : glyph < stop)
15272 && EQ (glyph->object, str))
15273 glyph += incr;
15275 else
15276 glyph += incr;
15279 /* If we reached the end of the line, and END was from a string,
15280 the cursor is not on this line. */
15281 if (cursor == NULL
15282 && (row->reversed_p ? glyph <= end : glyph >= end)
15283 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15284 && STRINGP (end->object)
15285 && row->continued_p)
15286 return false;
15288 /* A truncated row may not include PT among its character positions.
15289 Setting the cursor inside the scroll margin will trigger
15290 recalculation of hscroll in hscroll_window_tree. But if a
15291 display string covers point, defer to the string-handling
15292 code below to figure this out. */
15293 else if (row->truncated_on_left_p && pt_old < bpos_min)
15295 cursor = glyph_before;
15296 x = -1;
15298 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15299 /* Zero-width characters produce no glyphs. */
15300 || (!empty_line_p
15301 && (row->reversed_p
15302 ? glyph_after > glyphs_end
15303 : glyph_after < glyphs_end)))
15305 cursor = glyph_after;
15306 x = -1;
15310 compute_x:
15311 if (cursor != NULL)
15312 glyph = cursor;
15313 else if (glyph == glyphs_end
15314 && pos_before == pos_after
15315 && STRINGP ((row->reversed_p
15316 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15317 : row->glyphs[TEXT_AREA])->object))
15319 /* If all the glyphs of this row came from strings, put the
15320 cursor on the first glyph of the row. This avoids having the
15321 cursor outside of the text area in this very rare and hard
15322 use case. */
15323 glyph =
15324 row->reversed_p
15325 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15326 : row->glyphs[TEXT_AREA];
15328 if (x < 0)
15330 struct glyph *g;
15332 /* Need to compute x that corresponds to GLYPH. */
15333 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15335 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15336 emacs_abort ();
15337 x += g->pixel_width;
15341 /* ROW could be part of a continued line, which, under bidi
15342 reordering, might have other rows whose start and end charpos
15343 occlude point. Only set w->cursor if we found a better
15344 approximation to the cursor position than we have from previously
15345 examined candidate rows belonging to the same continued line. */
15346 if (/* We already have a candidate row. */
15347 w->cursor.vpos >= 0
15348 /* That candidate is not the row we are processing. */
15349 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15350 /* Make sure cursor.vpos specifies a row whose start and end
15351 charpos occlude point, and it is valid candidate for being a
15352 cursor-row. This is because some callers of this function
15353 leave cursor.vpos at the row where the cursor was displayed
15354 during the last redisplay cycle. */
15355 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15356 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15357 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15359 struct glyph *g1
15360 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15362 /* Don't consider glyphs that are outside TEXT_AREA. */
15363 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15364 return false;
15365 /* Keep the candidate whose buffer position is the closest to
15366 point or has the `cursor' property. */
15367 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15368 w->cursor.hpos >= 0
15369 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15370 && ((BUFFERP (g1->object)
15371 && (g1->charpos == pt_old /* An exact match always wins. */
15372 || (BUFFERP (glyph->object)
15373 && eabs (g1->charpos - pt_old)
15374 < eabs (glyph->charpos - pt_old))))
15375 /* Previous candidate is a glyph from a string that has
15376 a non-nil `cursor' property. */
15377 || (STRINGP (g1->object)
15378 && (!NILP (Fget_char_property (make_number (g1->charpos),
15379 Qcursor, g1->object))
15380 /* Previous candidate is from the same display
15381 string as this one, and the display string
15382 came from a text property. */
15383 || (EQ (g1->object, glyph->object)
15384 && string_from_text_prop)
15385 /* this candidate is from newline and its
15386 position is not an exact match */
15387 || (NILP (glyph->object)
15388 && glyph->charpos != pt_old)))))
15389 return false;
15390 /* If this candidate gives an exact match, use that. */
15391 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15392 /* If this candidate is a glyph created for the
15393 terminating newline of a line, and point is on that
15394 newline, it wins because it's an exact match. */
15395 || (!row->continued_p
15396 && NILP (glyph->object)
15397 && glyph->charpos == 0
15398 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15399 /* Otherwise, keep the candidate that comes from a row
15400 spanning less buffer positions. This may win when one or
15401 both candidate positions are on glyphs that came from
15402 display strings, for which we cannot compare buffer
15403 positions. */
15404 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15405 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15406 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15407 return false;
15409 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15410 w->cursor.x = x;
15411 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15412 w->cursor.y = row->y + dy;
15414 if (w == XWINDOW (selected_window))
15416 if (!row->continued_p
15417 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15418 && row->x == 0)
15420 this_line_buffer = XBUFFER (w->contents);
15422 CHARPOS (this_line_start_pos)
15423 = MATRIX_ROW_START_CHARPOS (row) + delta;
15424 BYTEPOS (this_line_start_pos)
15425 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15427 CHARPOS (this_line_end_pos)
15428 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15429 BYTEPOS (this_line_end_pos)
15430 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15432 this_line_y = w->cursor.y;
15433 this_line_pixel_height = row->height;
15434 this_line_vpos = w->cursor.vpos;
15435 this_line_start_x = row->x;
15437 else
15438 CHARPOS (this_line_start_pos) = 0;
15441 return true;
15445 /* Run window scroll functions, if any, for WINDOW with new window
15446 start STARTP. Sets the window start of WINDOW to that position.
15448 We assume that the window's buffer is really current. */
15450 static struct text_pos
15451 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15453 struct window *w = XWINDOW (window);
15454 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15456 eassert (current_buffer == XBUFFER (w->contents));
15458 if (!NILP (Vwindow_scroll_functions))
15460 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15461 make_number (CHARPOS (startp)));
15462 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15463 /* In case the hook functions switch buffers. */
15464 set_buffer_internal (XBUFFER (w->contents));
15467 return startp;
15471 /* Make sure the line containing the cursor is fully visible.
15472 A value of true means there is nothing to be done.
15473 (Either the line is fully visible, or it cannot be made so,
15474 or we cannot tell.)
15476 If FORCE_P, return false even if partial visible cursor row
15477 is higher than window.
15479 If CURRENT_MATRIX_P, use the information from the
15480 window's current glyph matrix; otherwise use the desired glyph
15481 matrix.
15483 A value of false means the caller should do scrolling
15484 as if point had gone off the screen. */
15486 static bool
15487 cursor_row_fully_visible_p (struct window *w, bool force_p,
15488 bool current_matrix_p)
15490 struct glyph_matrix *matrix;
15491 struct glyph_row *row;
15492 int window_height;
15494 if (!make_cursor_line_fully_visible_p)
15495 return true;
15497 /* It's not always possible to find the cursor, e.g, when a window
15498 is full of overlay strings. Don't do anything in that case. */
15499 if (w->cursor.vpos < 0)
15500 return true;
15502 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15503 row = MATRIX_ROW (matrix, w->cursor.vpos);
15505 /* If the cursor row is not partially visible, there's nothing to do. */
15506 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15507 return true;
15509 /* If the row the cursor is in is taller than the window's height,
15510 it's not clear what to do, so do nothing. */
15511 window_height = window_box_height (w);
15512 if (row->height >= window_height)
15514 if (!force_p || MINI_WINDOW_P (w)
15515 || w->vscroll || w->cursor.vpos == 0)
15516 return true;
15518 return false;
15522 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15523 means only WINDOW is redisplayed in redisplay_internal.
15524 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15525 in redisplay_window to bring a partially visible line into view in
15526 the case that only the cursor has moved.
15528 LAST_LINE_MISFIT should be true if we're scrolling because the
15529 last screen line's vertical height extends past the end of the screen.
15531 Value is
15533 1 if scrolling succeeded
15535 0 if scrolling didn't find point.
15537 -1 if new fonts have been loaded so that we must interrupt
15538 redisplay, adjust glyph matrices, and try again. */
15540 enum
15542 SCROLLING_SUCCESS,
15543 SCROLLING_FAILED,
15544 SCROLLING_NEED_LARGER_MATRICES
15547 /* If scroll-conservatively is more than this, never recenter.
15549 If you change this, don't forget to update the doc string of
15550 `scroll-conservatively' and the Emacs manual. */
15551 #define SCROLL_LIMIT 100
15553 static int
15554 try_scrolling (Lisp_Object window, bool just_this_one_p,
15555 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15556 bool temp_scroll_step, bool last_line_misfit)
15558 struct window *w = XWINDOW (window);
15559 struct text_pos pos, startp;
15560 struct it it;
15561 int this_scroll_margin, scroll_max, rc, height;
15562 int dy = 0, amount_to_scroll = 0;
15563 bool scroll_down_p = false;
15564 int extra_scroll_margin_lines = last_line_misfit;
15565 Lisp_Object aggressive;
15566 /* We will never try scrolling more than this number of lines. */
15567 int scroll_limit = SCROLL_LIMIT;
15568 int frame_line_height = default_line_pixel_height (w);
15570 #ifdef GLYPH_DEBUG
15571 debug_method_add (w, "try_scrolling");
15572 #endif
15574 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15576 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15578 /* Force arg_scroll_conservatively to have a reasonable value, to
15579 avoid scrolling too far away with slow move_it_* functions. Note
15580 that the user can supply scroll-conservatively equal to
15581 `most-positive-fixnum', which can be larger than INT_MAX. */
15582 if (arg_scroll_conservatively > scroll_limit)
15584 arg_scroll_conservatively = scroll_limit + 1;
15585 scroll_max = scroll_limit * frame_line_height;
15587 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15588 /* Compute how much we should try to scroll maximally to bring
15589 point into view. */
15590 scroll_max = (max (scroll_step,
15591 max (arg_scroll_conservatively, temp_scroll_step))
15592 * frame_line_height);
15593 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15594 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15595 /* We're trying to scroll because of aggressive scrolling but no
15596 scroll_step is set. Choose an arbitrary one. */
15597 scroll_max = 10 * frame_line_height;
15598 else
15599 scroll_max = 0;
15601 too_near_end:
15603 /* Decide whether to scroll down. */
15604 if (PT > CHARPOS (startp))
15606 int scroll_margin_y;
15608 /* Compute the pixel ypos of the scroll margin, then move IT to
15609 either that ypos or PT, whichever comes first. */
15610 start_display (&it, w, startp);
15611 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15612 - this_scroll_margin
15613 - frame_line_height * extra_scroll_margin_lines;
15614 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15615 (MOVE_TO_POS | MOVE_TO_Y));
15617 if (PT > CHARPOS (it.current.pos))
15619 int y0 = line_bottom_y (&it);
15620 /* Compute how many pixels below window bottom to stop searching
15621 for PT. This avoids costly search for PT that is far away if
15622 the user limited scrolling by a small number of lines, but
15623 always finds PT if scroll_conservatively is set to a large
15624 number, such as most-positive-fixnum. */
15625 int slack = max (scroll_max, 10 * frame_line_height);
15626 int y_to_move = it.last_visible_y + slack;
15628 /* Compute the distance from the scroll margin to PT or to
15629 the scroll limit, whichever comes first. This should
15630 include the height of the cursor line, to make that line
15631 fully visible. */
15632 move_it_to (&it, PT, -1, y_to_move,
15633 -1, MOVE_TO_POS | MOVE_TO_Y);
15634 dy = line_bottom_y (&it) - y0;
15636 if (dy > scroll_max)
15637 return SCROLLING_FAILED;
15639 if (dy > 0)
15640 scroll_down_p = true;
15642 else if (PT == IT_CHARPOS (it)
15643 && IT_CHARPOS (it) < ZV
15644 && it.method == GET_FROM_STRING
15645 && arg_scroll_conservatively > scroll_limit
15646 && it.current_x == 0)
15648 enum move_it_result skip;
15649 int y1 = it.current_y;
15650 int vpos;
15652 /* A before-string that includes newlines and is displayed
15653 on the last visible screen line could fail us under
15654 scroll-conservatively > 100, because we will be unable to
15655 position the cursor on that last visible line. Try to
15656 recover by finding the first screen line that has some
15657 glyphs coming from the buffer text. */
15658 do {
15659 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15660 if (skip != MOVE_NEWLINE_OR_CR
15661 || IT_CHARPOS (it) != PT
15662 || it.method == GET_FROM_BUFFER)
15663 break;
15664 vpos = it.vpos;
15665 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15666 } while (it.vpos > vpos);
15668 dy = it.current_y - y1;
15670 if (dy > scroll_max)
15671 return SCROLLING_FAILED;
15673 if (dy > 0)
15674 scroll_down_p = true;
15678 if (scroll_down_p)
15680 /* Point is in or below the bottom scroll margin, so move the
15681 window start down. If scrolling conservatively, move it just
15682 enough down to make point visible. If scroll_step is set,
15683 move it down by scroll_step. */
15684 if (arg_scroll_conservatively)
15685 amount_to_scroll
15686 = min (max (dy, frame_line_height),
15687 frame_line_height * arg_scroll_conservatively);
15688 else if (scroll_step || temp_scroll_step)
15689 amount_to_scroll = scroll_max;
15690 else
15692 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15693 height = WINDOW_BOX_TEXT_HEIGHT (w);
15694 if (NUMBERP (aggressive))
15696 double float_amount = XFLOATINT (aggressive) * height;
15697 int aggressive_scroll = float_amount;
15698 if (aggressive_scroll == 0 && float_amount > 0)
15699 aggressive_scroll = 1;
15700 /* Don't let point enter the scroll margin near top of
15701 the window. This could happen if the value of
15702 scroll_up_aggressively is too large and there are
15703 non-zero margins, because scroll_up_aggressively
15704 means put point that fraction of window height
15705 _from_the_bottom_margin_. */
15706 if (aggressive_scroll + 2 * this_scroll_margin > height)
15707 aggressive_scroll = height - 2 * this_scroll_margin;
15708 amount_to_scroll = dy + aggressive_scroll;
15712 if (amount_to_scroll <= 0)
15713 return SCROLLING_FAILED;
15715 start_display (&it, w, startp);
15716 if (arg_scroll_conservatively <= scroll_limit)
15717 move_it_vertically (&it, amount_to_scroll);
15718 else
15720 /* Extra precision for users who set scroll-conservatively
15721 to a large number: make sure the amount we scroll
15722 the window start is never less than amount_to_scroll,
15723 which was computed as distance from window bottom to
15724 point. This matters when lines at window top and lines
15725 below window bottom have different height. */
15726 struct it it1;
15727 void *it1data = NULL;
15728 /* We use a temporary it1 because line_bottom_y can modify
15729 its argument, if it moves one line down; see there. */
15730 int start_y;
15732 SAVE_IT (it1, it, it1data);
15733 start_y = line_bottom_y (&it1);
15734 do {
15735 RESTORE_IT (&it, &it, it1data);
15736 move_it_by_lines (&it, 1);
15737 SAVE_IT (it1, it, it1data);
15738 } while (IT_CHARPOS (it) < ZV
15739 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15740 bidi_unshelve_cache (it1data, true);
15743 /* If STARTP is unchanged, move it down another screen line. */
15744 if (IT_CHARPOS (it) == CHARPOS (startp))
15745 move_it_by_lines (&it, 1);
15746 startp = it.current.pos;
15748 else
15750 struct text_pos scroll_margin_pos = startp;
15751 int y_offset = 0;
15753 /* See if point is inside the scroll margin at the top of the
15754 window. */
15755 if (this_scroll_margin)
15757 int y_start;
15759 start_display (&it, w, startp);
15760 y_start = it.current_y;
15761 move_it_vertically (&it, this_scroll_margin);
15762 scroll_margin_pos = it.current.pos;
15763 /* If we didn't move enough before hitting ZV, request
15764 additional amount of scroll, to move point out of the
15765 scroll margin. */
15766 if (IT_CHARPOS (it) == ZV
15767 && it.current_y - y_start < this_scroll_margin)
15768 y_offset = this_scroll_margin - (it.current_y - y_start);
15771 if (PT < CHARPOS (scroll_margin_pos))
15773 /* Point is in the scroll margin at the top of the window or
15774 above what is displayed in the window. */
15775 int y0, y_to_move;
15777 /* Compute the vertical distance from PT to the scroll
15778 margin position. Move as far as scroll_max allows, or
15779 one screenful, or 10 screen lines, whichever is largest.
15780 Give up if distance is greater than scroll_max or if we
15781 didn't reach the scroll margin position. */
15782 SET_TEXT_POS (pos, PT, PT_BYTE);
15783 start_display (&it, w, pos);
15784 y0 = it.current_y;
15785 y_to_move = max (it.last_visible_y,
15786 max (scroll_max, 10 * frame_line_height));
15787 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15788 y_to_move, -1,
15789 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15790 dy = it.current_y - y0;
15791 if (dy > scroll_max
15792 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15793 return SCROLLING_FAILED;
15795 /* Additional scroll for when ZV was too close to point. */
15796 dy += y_offset;
15798 /* Compute new window start. */
15799 start_display (&it, w, startp);
15801 if (arg_scroll_conservatively)
15802 amount_to_scroll = max (dy, frame_line_height
15803 * max (scroll_step, temp_scroll_step));
15804 else if (scroll_step || temp_scroll_step)
15805 amount_to_scroll = scroll_max;
15806 else
15808 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15809 height = WINDOW_BOX_TEXT_HEIGHT (w);
15810 if (NUMBERP (aggressive))
15812 double float_amount = XFLOATINT (aggressive) * height;
15813 int aggressive_scroll = float_amount;
15814 if (aggressive_scroll == 0 && float_amount > 0)
15815 aggressive_scroll = 1;
15816 /* Don't let point enter the scroll margin near
15817 bottom of the window, if the value of
15818 scroll_down_aggressively happens to be too
15819 large. */
15820 if (aggressive_scroll + 2 * this_scroll_margin > height)
15821 aggressive_scroll = height - 2 * this_scroll_margin;
15822 amount_to_scroll = dy + aggressive_scroll;
15826 if (amount_to_scroll <= 0)
15827 return SCROLLING_FAILED;
15829 move_it_vertically_backward (&it, amount_to_scroll);
15830 startp = it.current.pos;
15834 /* Run window scroll functions. */
15835 startp = run_window_scroll_functions (window, startp);
15837 /* Display the window. Give up if new fonts are loaded, or if point
15838 doesn't appear. */
15839 if (!try_window (window, startp, 0))
15840 rc = SCROLLING_NEED_LARGER_MATRICES;
15841 else if (w->cursor.vpos < 0)
15843 clear_glyph_matrix (w->desired_matrix);
15844 rc = SCROLLING_FAILED;
15846 else
15848 /* Maybe forget recorded base line for line number display. */
15849 if (!just_this_one_p
15850 || current_buffer->clip_changed
15851 || BEG_UNCHANGED < CHARPOS (startp))
15852 w->base_line_number = 0;
15854 /* If cursor ends up on a partially visible line,
15855 treat that as being off the bottom of the screen. */
15856 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15857 false)
15858 /* It's possible that the cursor is on the first line of the
15859 buffer, which is partially obscured due to a vscroll
15860 (Bug#7537). In that case, avoid looping forever. */
15861 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15863 clear_glyph_matrix (w->desired_matrix);
15864 ++extra_scroll_margin_lines;
15865 goto too_near_end;
15867 rc = SCROLLING_SUCCESS;
15870 return rc;
15874 /* Compute a suitable window start for window W if display of W starts
15875 on a continuation line. Value is true if a new window start
15876 was computed.
15878 The new window start will be computed, based on W's width, starting
15879 from the start of the continued line. It is the start of the
15880 screen line with the minimum distance from the old start W->start,
15881 which is still before point (otherwise point will definitely not
15882 be visible in the window). */
15884 static bool
15885 compute_window_start_on_continuation_line (struct window *w)
15887 struct text_pos pos, start_pos, pos_before_pt;
15888 bool window_start_changed_p = false;
15890 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15892 /* If window start is on a continuation line... Window start may be
15893 < BEGV in case there's invisible text at the start of the
15894 buffer (M-x rmail, for example). */
15895 if (CHARPOS (start_pos) > BEGV
15896 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15898 struct it it;
15899 struct glyph_row *row;
15901 /* Handle the case that the window start is out of range. */
15902 if (CHARPOS (start_pos) < BEGV)
15903 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15904 else if (CHARPOS (start_pos) > ZV)
15905 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15907 /* Find the start of the continued line. This should be fast
15908 because find_newline is fast (newline cache). */
15909 row = w->desired_matrix->rows + window_wants_header_line (w);
15910 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15911 row, DEFAULT_FACE_ID);
15912 reseat_at_previous_visible_line_start (&it);
15914 /* If the line start is "too far" away from the window start,
15915 say it takes too much time to compute a new window start.
15916 Also, give up if the line start is after point, as in that
15917 case point will not be visible with any window start we
15918 compute. */
15919 if (IT_CHARPOS (it) <= PT
15920 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15921 /* PXW: Do we need upper bounds here? */
15922 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15924 int min_distance, distance;
15926 /* Move forward by display lines to find the new window
15927 start. If window width was enlarged, the new start can
15928 be expected to be > the old start. If window width was
15929 decreased, the new window start will be < the old start.
15930 So, we're looking for the display line start with the
15931 minimum distance from the old window start. */
15932 pos_before_pt = pos = it.current.pos;
15933 min_distance = DISP_INFINITY;
15934 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15935 distance < min_distance)
15937 min_distance = distance;
15938 if (CHARPOS (pos) <= PT)
15939 pos_before_pt = pos;
15940 pos = it.current.pos;
15941 if (it.line_wrap == WORD_WRAP)
15943 /* Under WORD_WRAP, move_it_by_lines is likely to
15944 overshoot and stop not at the first, but the
15945 second character from the left margin. So in
15946 that case, we need a more tight control on the X
15947 coordinate of the iterator than move_it_by_lines
15948 promises in its contract. The method is to first
15949 go to the last (rightmost) visible character of a
15950 line, then move to the leftmost character on the
15951 next line in a separate call. */
15952 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15953 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15954 move_it_to (&it, ZV, 0,
15955 it.current_y + it.max_ascent + it.max_descent, -1,
15956 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15958 else
15959 move_it_by_lines (&it, 1);
15962 /* It makes very little sense to make the new window start
15963 after point, as point won't be visible. If that's what
15964 the loop above finds, fall back on the candidate before
15965 or at point that is closest to the old window start. */
15966 if (CHARPOS (pos) > PT)
15967 pos = pos_before_pt;
15969 /* Set the window start there. */
15970 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15971 window_start_changed_p = true;
15975 return window_start_changed_p;
15979 /* Try cursor movement in case text has not changed in window WINDOW,
15980 with window start STARTP. Value is
15982 CURSOR_MOVEMENT_SUCCESS if successful
15984 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15986 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15987 display. *SCROLL_STEP is set to true, under certain circumstances, if
15988 we want to scroll as if scroll-step were set to 1. See the code.
15990 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15991 which case we have to abort this redisplay, and adjust matrices
15992 first. */
15994 enum
15996 CURSOR_MOVEMENT_SUCCESS,
15997 CURSOR_MOVEMENT_CANNOT_BE_USED,
15998 CURSOR_MOVEMENT_MUST_SCROLL,
15999 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16002 static int
16003 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16004 bool *scroll_step)
16006 struct window *w = XWINDOW (window);
16007 struct frame *f = XFRAME (w->frame);
16008 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16010 #ifdef GLYPH_DEBUG
16011 if (inhibit_try_cursor_movement)
16012 return rc;
16013 #endif
16015 /* Previously, there was a check for Lisp integer in the
16016 if-statement below. Now, this field is converted to
16017 ptrdiff_t, thus zero means invalid position in a buffer. */
16018 eassert (w->last_point > 0);
16019 /* Likewise there was a check whether window_end_vpos is nil or larger
16020 than the window. Now window_end_vpos is int and so never nil, but
16021 let's leave eassert to check whether it fits in the window. */
16022 eassert (!w->window_end_valid
16023 || w->window_end_vpos < w->current_matrix->nrows);
16025 /* Handle case where text has not changed, only point, and it has
16026 not moved off the frame. */
16027 if (/* Point may be in this window. */
16028 PT >= CHARPOS (startp)
16029 /* Selective display hasn't changed. */
16030 && !current_buffer->clip_changed
16031 /* Function force-mode-line-update is used to force a thorough
16032 redisplay. It sets either windows_or_buffers_changed or
16033 update_mode_lines. So don't take a shortcut here for these
16034 cases. */
16035 && !update_mode_lines
16036 && !windows_or_buffers_changed
16037 && !f->cursor_type_changed
16038 && NILP (Vshow_trailing_whitespace)
16039 /* When display-line-numbers is in relative mode, moving point
16040 requires to redraw the entire window. */
16041 && !EQ (Vdisplay_line_numbers, Qrelative)
16042 && !EQ (Vdisplay_line_numbers, Qvisual)
16043 /* When the current line number should be displayed in a
16044 distinct face, moving point cannot be handled in optimized
16045 way as below. */
16046 && !(!NILP (Vdisplay_line_numbers)
16047 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16048 Qline_number_current_line,
16049 w->frame)))
16050 /* This code is not used for mini-buffer for the sake of the case
16051 of redisplaying to replace an echo area message; since in
16052 that case the mini-buffer contents per se are usually
16053 unchanged. This code is of no real use in the mini-buffer
16054 since the handling of this_line_start_pos, etc., in redisplay
16055 handles the same cases. */
16056 && !EQ (window, minibuf_window)
16057 && (FRAME_WINDOW_P (f)
16058 || !overlay_arrow_in_current_buffer_p ()))
16060 int this_scroll_margin, top_scroll_margin;
16061 struct glyph_row *row = NULL;
16063 #ifdef GLYPH_DEBUG
16064 debug_method_add (w, "cursor movement");
16065 #endif
16067 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16069 top_scroll_margin = this_scroll_margin;
16070 if (window_wants_header_line (w))
16071 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16073 /* Start with the row the cursor was displayed during the last
16074 not paused redisplay. Give up if that row is not valid. */
16075 if (w->last_cursor_vpos < 0
16076 || w->last_cursor_vpos >= w->current_matrix->nrows)
16077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16078 else
16080 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16081 if (row->mode_line_p)
16082 ++row;
16083 if (!row->enabled_p)
16084 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16087 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16089 bool scroll_p = false, must_scroll = false;
16090 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16092 if (PT > w->last_point)
16094 /* Point has moved forward. */
16095 while (MATRIX_ROW_END_CHARPOS (row) < PT
16096 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16098 eassert (row->enabled_p);
16099 ++row;
16102 /* If the end position of a row equals the start
16103 position of the next row, and PT is at that position,
16104 we would rather display cursor in the next line. */
16105 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16106 && MATRIX_ROW_END_CHARPOS (row) == PT
16107 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16108 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16109 && !cursor_row_p (row))
16110 ++row;
16112 /* If within the scroll margin, scroll. Note that
16113 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16114 the next line would be drawn, and that
16115 this_scroll_margin can be zero. */
16116 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16117 || PT > MATRIX_ROW_END_CHARPOS (row)
16118 /* Line is completely visible last line in window
16119 and PT is to be set in the next line. */
16120 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16121 && PT == MATRIX_ROW_END_CHARPOS (row)
16122 && !row->ends_at_zv_p
16123 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16124 scroll_p = true;
16126 else if (PT < w->last_point)
16128 /* Cursor has to be moved backward. Note that PT >=
16129 CHARPOS (startp) because of the outer if-statement. */
16130 while (!row->mode_line_p
16131 && (MATRIX_ROW_START_CHARPOS (row) > PT
16132 || (MATRIX_ROW_START_CHARPOS (row) == PT
16133 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16134 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16135 row > w->current_matrix->rows
16136 && (row-1)->ends_in_newline_from_string_p))))
16137 && (row->y > top_scroll_margin
16138 || CHARPOS (startp) == BEGV))
16140 eassert (row->enabled_p);
16141 --row;
16144 /* Consider the following case: Window starts at BEGV,
16145 there is invisible, intangible text at BEGV, so that
16146 display starts at some point START > BEGV. It can
16147 happen that we are called with PT somewhere between
16148 BEGV and START. Try to handle that case. */
16149 if (row < w->current_matrix->rows
16150 || row->mode_line_p)
16152 row = w->current_matrix->rows;
16153 if (row->mode_line_p)
16154 ++row;
16157 /* Due to newlines in overlay strings, we may have to
16158 skip forward over overlay strings. */
16159 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16160 && MATRIX_ROW_END_CHARPOS (row) == PT
16161 && !cursor_row_p (row))
16162 ++row;
16164 /* If within the scroll margin, scroll. */
16165 if (row->y < top_scroll_margin
16166 && CHARPOS (startp) != BEGV)
16167 scroll_p = true;
16169 else
16171 /* Cursor did not move. So don't scroll even if cursor line
16172 is partially visible, as it was so before. */
16173 rc = CURSOR_MOVEMENT_SUCCESS;
16176 if (PT < MATRIX_ROW_START_CHARPOS (row)
16177 || PT > MATRIX_ROW_END_CHARPOS (row))
16179 /* if PT is not in the glyph row, give up. */
16180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16181 must_scroll = true;
16183 else if (rc != CURSOR_MOVEMENT_SUCCESS
16184 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16186 struct glyph_row *row1;
16188 /* If rows are bidi-reordered and point moved, back up
16189 until we find a row that does not belong to a
16190 continuation line. This is because we must consider
16191 all rows of a continued line as candidates for the
16192 new cursor positioning, since row start and end
16193 positions change non-linearly with vertical position
16194 in such rows. */
16195 /* FIXME: Revisit this when glyph ``spilling'' in
16196 continuation lines' rows is implemented for
16197 bidi-reordered rows. */
16198 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16199 MATRIX_ROW_CONTINUATION_LINE_P (row);
16200 --row)
16202 /* If we hit the beginning of the displayed portion
16203 without finding the first row of a continued
16204 line, give up. */
16205 if (row <= row1)
16207 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16208 break;
16210 eassert (row->enabled_p);
16213 if (must_scroll)
16215 else if (rc != CURSOR_MOVEMENT_SUCCESS
16216 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16217 /* Make sure this isn't a header line by any chance, since
16218 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16219 && !row->mode_line_p
16220 && make_cursor_line_fully_visible_p)
16222 if (PT == MATRIX_ROW_END_CHARPOS (row)
16223 && !row->ends_at_zv_p
16224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16225 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16226 else if (row->height > window_box_height (w))
16228 /* If we end up in a partially visible line, let's
16229 make it fully visible, except when it's taller
16230 than the window, in which case we can't do much
16231 about it. */
16232 *scroll_step = true;
16233 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16235 else
16237 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16238 if (!cursor_row_fully_visible_p (w, false, true))
16239 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16240 else
16241 rc = CURSOR_MOVEMENT_SUCCESS;
16244 else if (scroll_p)
16245 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16246 else if (rc != CURSOR_MOVEMENT_SUCCESS
16247 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16249 /* With bidi-reordered rows, there could be more than
16250 one candidate row whose start and end positions
16251 occlude point. We need to let set_cursor_from_row
16252 find the best candidate. */
16253 /* FIXME: Revisit this when glyph ``spilling'' in
16254 continuation lines' rows is implemented for
16255 bidi-reordered rows. */
16256 bool rv = false;
16260 bool at_zv_p = false, exact_match_p = false;
16262 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16263 && PT <= MATRIX_ROW_END_CHARPOS (row)
16264 && cursor_row_p (row))
16265 rv |= set_cursor_from_row (w, row, w->current_matrix,
16266 0, 0, 0, 0);
16267 /* As soon as we've found the exact match for point,
16268 or the first suitable row whose ends_at_zv_p flag
16269 is set, we are done. */
16270 if (rv)
16272 at_zv_p = MATRIX_ROW (w->current_matrix,
16273 w->cursor.vpos)->ends_at_zv_p;
16274 if (!at_zv_p
16275 && w->cursor.hpos >= 0
16276 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16277 w->cursor.vpos))
16279 struct glyph_row *candidate =
16280 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16281 struct glyph *g =
16282 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16283 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16285 exact_match_p =
16286 (BUFFERP (g->object) && g->charpos == PT)
16287 || (NILP (g->object)
16288 && (g->charpos == PT
16289 || (g->charpos == 0 && endpos - 1 == PT)));
16291 if (at_zv_p || exact_match_p)
16293 rc = CURSOR_MOVEMENT_SUCCESS;
16294 break;
16297 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16298 break;
16299 ++row;
16301 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16302 || row->continued_p)
16303 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16304 || (MATRIX_ROW_START_CHARPOS (row) == PT
16305 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16306 /* If we didn't find any candidate rows, or exited the
16307 loop before all the candidates were examined, signal
16308 to the caller that this method failed. */
16309 if (rc != CURSOR_MOVEMENT_SUCCESS
16310 && !(rv
16311 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16312 && !row->continued_p))
16313 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16314 else if (rv)
16315 rc = CURSOR_MOVEMENT_SUCCESS;
16317 else
16321 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16323 rc = CURSOR_MOVEMENT_SUCCESS;
16324 break;
16326 ++row;
16328 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16329 && MATRIX_ROW_START_CHARPOS (row) == PT
16330 && cursor_row_p (row));
16335 return rc;
16339 void
16340 set_vertical_scroll_bar (struct window *w)
16342 ptrdiff_t start, end, whole;
16344 /* Calculate the start and end positions for the current window.
16345 At some point, it would be nice to choose between scrollbars
16346 which reflect the whole buffer size, with special markers
16347 indicating narrowing, and scrollbars which reflect only the
16348 visible region.
16350 Note that mini-buffers sometimes aren't displaying any text. */
16351 if (!MINI_WINDOW_P (w)
16352 || (w == XWINDOW (minibuf_window)
16353 && NILP (echo_area_buffer[0])))
16355 struct buffer *buf = XBUFFER (w->contents);
16356 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16357 start = marker_position (w->start) - BUF_BEGV (buf);
16358 /* I don't think this is guaranteed to be right. For the
16359 moment, we'll pretend it is. */
16360 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16362 if (end < start)
16363 end = start;
16364 if (whole < (end - start))
16365 whole = end - start;
16367 else
16368 start = end = whole = 0;
16370 /* Indicate what this scroll bar ought to be displaying now. */
16371 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16372 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16373 (w, end - start, whole, start);
16377 void
16378 set_horizontal_scroll_bar (struct window *w)
16380 int start, end, whole, portion;
16382 if (!MINI_WINDOW_P (w)
16383 || (w == XWINDOW (minibuf_window)
16384 && NILP (echo_area_buffer[0])))
16386 struct buffer *b = XBUFFER (w->contents);
16387 struct buffer *old_buffer = NULL;
16388 struct it it;
16389 struct text_pos startp;
16391 if (b != current_buffer)
16393 old_buffer = current_buffer;
16394 set_buffer_internal (b);
16397 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16398 start_display (&it, w, startp);
16399 it.last_visible_x = INT_MAX;
16400 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16401 MOVE_TO_X | MOVE_TO_Y);
16402 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16403 window_box_height (w), -1,
16404 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16406 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16407 end = start + window_box_width (w, TEXT_AREA);
16408 portion = end - start;
16409 /* After enlarging a horizontally scrolled window such that it
16410 gets at least as wide as the text it contains, make sure that
16411 the thumb doesn't fill the entire scroll bar so we can still
16412 drag it back to see the entire text. */
16413 whole = max (whole, end);
16415 if (it.bidi_p)
16417 Lisp_Object pdir;
16419 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16420 if (EQ (pdir, Qright_to_left))
16422 start = whole - end;
16423 end = start + portion;
16427 if (old_buffer)
16428 set_buffer_internal (old_buffer);
16430 else
16431 start = end = whole = portion = 0;
16433 w->hscroll_whole = whole;
16435 /* Indicate what this scroll bar ought to be displaying now. */
16436 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16437 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16438 (w, portion, whole, start);
16442 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16443 selected_window is redisplayed.
16445 We can return without actually redisplaying the window if fonts has been
16446 changed on window's frame. In that case, redisplay_internal will retry.
16448 As one of the important parts of redisplaying a window, we need to
16449 decide whether the previous window-start position (stored in the
16450 window's w->start marker position) is still valid, and if it isn't,
16451 recompute it. Some details about that:
16453 . The previous window-start could be in a continuation line, in
16454 which case we need to recompute it when the window width
16455 changes. See compute_window_start_on_continuation_line and its
16456 call below.
16458 . The text that changed since last redisplay could include the
16459 previous window-start position. In that case, we try to salvage
16460 what we can from the current glyph matrix by calling
16461 try_scrolling, which see.
16463 . Some Emacs command could force us to use a specific window-start
16464 position by setting the window's force_start flag, or gently
16465 propose doing that by setting the window's optional_new_start
16466 flag. In these cases, we try using the specified start point if
16467 that succeeds (i.e. the window desired matrix is successfully
16468 recomputed, and point location is within the window). In case
16469 of optional_new_start, we first check if the specified start
16470 position is feasible, i.e. if it will allow point to be
16471 displayed in the window. If using the specified start point
16472 fails, e.g., if new fonts are needed to be loaded, we abort the
16473 redisplay cycle and leave it up to the next cycle to figure out
16474 things.
16476 . Note that the window's force_start flag is sometimes set by
16477 redisplay itself, when it decides that the previous window start
16478 point is fine and should be kept. Search for "goto force_start"
16479 below to see the details. Like the values of window-start
16480 specified outside of redisplay, these internally-deduced values
16481 are tested for feasibility, and ignored if found to be
16482 unfeasible.
16484 . Note that the function try_window, used to completely redisplay
16485 a window, accepts the window's start point as its argument.
16486 This is used several times in the redisplay code to control
16487 where the window start will be, according to user options such
16488 as scroll-conservatively, and also to ensure the screen line
16489 showing point will be fully (as opposed to partially) visible on
16490 display. */
16492 static void
16493 redisplay_window (Lisp_Object window, bool just_this_one_p)
16495 struct window *w = XWINDOW (window);
16496 struct frame *f = XFRAME (w->frame);
16497 struct buffer *buffer = XBUFFER (w->contents);
16498 struct buffer *old = current_buffer;
16499 struct text_pos lpoint, opoint, startp;
16500 bool update_mode_line;
16501 int tem;
16502 struct it it;
16503 /* Record it now because it's overwritten. */
16504 bool current_matrix_up_to_date_p = false;
16505 bool used_current_matrix_p = false;
16506 /* This is less strict than current_matrix_up_to_date_p.
16507 It indicates that the buffer contents and narrowing are unchanged. */
16508 bool buffer_unchanged_p = false;
16509 bool temp_scroll_step = false;
16510 ptrdiff_t count = SPECPDL_INDEX ();
16511 int rc;
16512 int centering_position = -1;
16513 bool last_line_misfit = false;
16514 ptrdiff_t beg_unchanged, end_unchanged;
16515 int frame_line_height, margin;
16516 bool use_desired_matrix;
16517 void *itdata = NULL;
16519 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16520 opoint = lpoint;
16522 #ifdef GLYPH_DEBUG
16523 *w->desired_matrix->method = 0;
16524 #endif
16526 if (!just_this_one_p
16527 && REDISPLAY_SOME_P ()
16528 && !w->redisplay
16529 && !w->update_mode_line
16530 && !f->face_change
16531 && !f->redisplay
16532 && !buffer->text->redisplay
16533 && BUF_PT (buffer) == w->last_point)
16534 return;
16536 /* Make sure that both W's markers are valid. */
16537 eassert (XMARKER (w->start)->buffer == buffer);
16538 eassert (XMARKER (w->pointm)->buffer == buffer);
16540 reconsider_clip_changes (w);
16541 frame_line_height = default_line_pixel_height (w);
16542 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16545 /* Has the mode line to be updated? */
16546 update_mode_line = (w->update_mode_line
16547 || update_mode_lines
16548 || buffer->clip_changed
16549 || buffer->prevent_redisplay_optimizations_p);
16551 if (!just_this_one_p)
16552 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16553 cleverly elsewhere. */
16554 w->must_be_updated_p = true;
16556 if (MINI_WINDOW_P (w))
16558 if (w == XWINDOW (echo_area_window)
16559 && !NILP (echo_area_buffer[0]))
16561 if (update_mode_line)
16562 /* We may have to update a tty frame's menu bar or a
16563 tool-bar. Example `M-x C-h C-h C-g'. */
16564 goto finish_menu_bars;
16565 else
16566 /* We've already displayed the echo area glyphs in this window. */
16567 goto finish_scroll_bars;
16569 else if ((w != XWINDOW (minibuf_window)
16570 || minibuf_level == 0)
16571 /* When buffer is nonempty, redisplay window normally. */
16572 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16573 /* Quail displays non-mini buffers in minibuffer window.
16574 In that case, redisplay the window normally. */
16575 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16577 /* W is a mini-buffer window, but it's not active, so clear
16578 it. */
16579 int yb = window_text_bottom_y (w);
16580 struct glyph_row *row;
16581 int y;
16583 for (y = 0, row = w->desired_matrix->rows;
16584 y < yb;
16585 y += row->height, ++row)
16586 blank_row (w, row, y);
16587 goto finish_scroll_bars;
16590 clear_glyph_matrix (w->desired_matrix);
16593 /* Otherwise set up data on this window; select its buffer and point
16594 value. */
16595 /* Really select the buffer, for the sake of buffer-local
16596 variables. */
16597 set_buffer_internal_1 (XBUFFER (w->contents));
16599 current_matrix_up_to_date_p
16600 = (w->window_end_valid
16601 && !current_buffer->clip_changed
16602 && !current_buffer->prevent_redisplay_optimizations_p
16603 && !window_outdated (w)
16604 && !hscrolling_current_line_p (w));
16606 beg_unchanged = BEG_UNCHANGED;
16607 end_unchanged = END_UNCHANGED;
16609 SET_TEXT_POS (opoint, PT, PT_BYTE);
16611 specbind (Qinhibit_point_motion_hooks, Qt);
16613 buffer_unchanged_p
16614 = (w->window_end_valid
16615 && !current_buffer->clip_changed
16616 && !window_outdated (w));
16618 /* When windows_or_buffers_changed is non-zero, we can't rely
16619 on the window end being valid, so set it to zero there. */
16620 if (windows_or_buffers_changed)
16622 /* If window starts on a continuation line, maybe adjust the
16623 window start in case the window's width changed. */
16624 if (XMARKER (w->start)->buffer == current_buffer)
16625 compute_window_start_on_continuation_line (w);
16627 w->window_end_valid = false;
16628 /* If so, we also can't rely on current matrix
16629 and should not fool try_cursor_movement below. */
16630 current_matrix_up_to_date_p = false;
16633 /* Some sanity checks. */
16634 CHECK_WINDOW_END (w);
16635 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16636 emacs_abort ();
16637 if (BYTEPOS (opoint) < CHARPOS (opoint))
16638 emacs_abort ();
16640 if (mode_line_update_needed (w))
16641 update_mode_line = true;
16643 /* Point refers normally to the selected window. For any other
16644 window, set up appropriate value. */
16645 if (!EQ (window, selected_window))
16647 ptrdiff_t new_pt = marker_position (w->pointm);
16648 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16650 if (new_pt < BEGV)
16652 new_pt = BEGV;
16653 new_pt_byte = BEGV_BYTE;
16654 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16656 else if (new_pt > (ZV - 1))
16658 new_pt = ZV;
16659 new_pt_byte = ZV_BYTE;
16660 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16663 /* We don't use SET_PT so that the point-motion hooks don't run. */
16664 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16667 /* If any of the character widths specified in the display table
16668 have changed, invalidate the width run cache. It's true that
16669 this may be a bit late to catch such changes, but the rest of
16670 redisplay goes (non-fatally) haywire when the display table is
16671 changed, so why should we worry about doing any better? */
16672 if (current_buffer->width_run_cache
16673 || (current_buffer->base_buffer
16674 && current_buffer->base_buffer->width_run_cache))
16676 struct Lisp_Char_Table *disptab = buffer_display_table ();
16678 if (! disptab_matches_widthtab
16679 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16681 struct buffer *buf = current_buffer;
16683 if (buf->base_buffer)
16684 buf = buf->base_buffer;
16685 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16686 recompute_width_table (current_buffer, disptab);
16690 /* If window-start is screwed up, choose a new one. */
16691 if (XMARKER (w->start)->buffer != current_buffer)
16692 goto recenter;
16694 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16696 /* If someone specified a new starting point but did not insist,
16697 check whether it can be used. */
16698 if ((w->optional_new_start || window_frozen_p (w))
16699 && CHARPOS (startp) >= BEGV
16700 && CHARPOS (startp) <= ZV)
16702 ptrdiff_t it_charpos;
16704 w->optional_new_start = false;
16705 start_display (&it, w, startp);
16706 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16707 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16708 /* Record IT's position now, since line_bottom_y might change
16709 that. */
16710 it_charpos = IT_CHARPOS (it);
16711 /* Make sure we set the force_start flag only if the cursor row
16712 will be fully visible. Otherwise, the code under force_start
16713 label below will try to move point back into view, which is
16714 not what the code which sets optional_new_start wants. */
16715 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16716 && !w->force_start)
16718 if (it_charpos == PT)
16719 w->force_start = true;
16720 /* IT may overshoot PT if text at PT is invisible. */
16721 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16722 w->force_start = true;
16723 #ifdef GLYPH_DEBUG
16724 if (w->force_start)
16726 if (window_frozen_p (w))
16727 debug_method_add (w, "set force_start from frozen window start");
16728 else
16729 debug_method_add (w, "set force_start from optional_new_start");
16731 #endif
16735 force_start:
16737 /* Handle case where place to start displaying has been specified,
16738 unless the specified location is outside the accessible range. */
16739 if (w->force_start)
16741 /* We set this later on if we have to adjust point. */
16742 int new_vpos = -1;
16744 w->force_start = false;
16745 w->vscroll = 0;
16746 w->window_end_valid = false;
16748 /* Forget any recorded base line for line number display. */
16749 if (!buffer_unchanged_p)
16750 w->base_line_number = 0;
16752 /* Redisplay the mode line. Select the buffer properly for that.
16753 Also, run the hook window-scroll-functions
16754 because we have scrolled. */
16755 /* Note, we do this after clearing force_start because
16756 if there's an error, it is better to forget about force_start
16757 than to get into an infinite loop calling the hook functions
16758 and having them get more errors. */
16759 if (!update_mode_line
16760 || ! NILP (Vwindow_scroll_functions))
16762 update_mode_line = true;
16763 w->update_mode_line = true;
16764 startp = run_window_scroll_functions (window, startp);
16767 if (CHARPOS (startp) < BEGV)
16768 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16769 else if (CHARPOS (startp) > ZV)
16770 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16772 /* Redisplay, then check if cursor has been set during the
16773 redisplay. Give up if new fonts were loaded. */
16774 /* We used to issue a CHECK_MARGINS argument to try_window here,
16775 but this causes scrolling to fail when point begins inside
16776 the scroll margin (bug#148) -- cyd */
16777 if (!try_window (window, startp, 0))
16779 w->force_start = true;
16780 clear_glyph_matrix (w->desired_matrix);
16781 goto need_larger_matrices;
16784 if (w->cursor.vpos < 0)
16786 /* If point does not appear, try to move point so it does
16787 appear. The desired matrix has been built above, so we
16788 can use it here. First see if point is in invisible
16789 text, and if so, move it to the first visible buffer
16790 position past that. */
16791 struct glyph_row *r = NULL;
16792 Lisp_Object invprop =
16793 get_char_property_and_overlay (make_number (PT), Qinvisible,
16794 Qnil, NULL);
16796 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16798 ptrdiff_t alt_pt;
16799 Lisp_Object invprop_end =
16800 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16801 Qnil, Qnil);
16803 if (NATNUMP (invprop_end))
16804 alt_pt = XFASTINT (invprop_end);
16805 else
16806 alt_pt = ZV;
16807 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16808 NULL, 0);
16810 if (r)
16811 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16812 else /* Give up and just move to the middle of the window. */
16813 new_vpos = window_box_height (w) / 2;
16816 if (!cursor_row_fully_visible_p (w, false, false))
16818 /* Point does appear, but on a line partly visible at end of window.
16819 Move it back to a fully-visible line. */
16820 new_vpos = window_box_height (w);
16821 /* But if window_box_height suggests a Y coordinate that is
16822 not less than we already have, that line will clearly not
16823 be fully visible, so give up and scroll the display.
16824 This can happen when the default face uses a font whose
16825 dimensions are different from the frame's default
16826 font. */
16827 if (new_vpos >= w->cursor.y)
16829 w->cursor.vpos = -1;
16830 clear_glyph_matrix (w->desired_matrix);
16831 goto try_to_scroll;
16834 else if (w->cursor.vpos >= 0)
16836 /* Some people insist on not letting point enter the scroll
16837 margin, even though this part handles windows that didn't
16838 scroll at all. */
16839 int pixel_margin = margin * frame_line_height;
16840 bool header_line = window_wants_header_line (w);
16842 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16843 below, which finds the row to move point to, advances by
16844 the Y coordinate of the _next_ row, see the definition of
16845 MATRIX_ROW_BOTTOM_Y. */
16846 if (w->cursor.vpos < margin + header_line)
16848 w->cursor.vpos = -1;
16849 clear_glyph_matrix (w->desired_matrix);
16850 goto try_to_scroll;
16852 else
16854 int window_height = window_box_height (w);
16856 if (header_line)
16857 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16858 if (w->cursor.y >= window_height - pixel_margin)
16860 w->cursor.vpos = -1;
16861 clear_glyph_matrix (w->desired_matrix);
16862 goto try_to_scroll;
16867 /* If we need to move point for either of the above reasons,
16868 now actually do it. */
16869 if (new_vpos >= 0)
16871 struct glyph_row *row;
16873 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16874 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16875 ++row;
16877 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16878 MATRIX_ROW_START_BYTEPOS (row));
16880 if (w != XWINDOW (selected_window))
16881 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16882 else if (current_buffer == old)
16883 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16885 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16887 /* Re-run pre-redisplay-function so it can update the region
16888 according to the new position of point. */
16889 /* Other than the cursor, w's redisplay is done so we can set its
16890 redisplay to false. Also the buffer's redisplay can be set to
16891 false, since propagate_buffer_redisplay should have already
16892 propagated its info to `w' anyway. */
16893 w->redisplay = false;
16894 XBUFFER (w->contents)->text->redisplay = false;
16895 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16897 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16898 || ((EQ (Vdisplay_line_numbers, Qrelative)
16899 || EQ (Vdisplay_line_numbers, Qvisual))
16900 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16902 /* Either pre-redisplay-function made changes (e.g. move
16903 the region), or we moved point in a window that is
16904 under display-line-numbers = relative mode. We need
16905 another round of redisplay. */
16906 clear_glyph_matrix (w->desired_matrix);
16907 if (!try_window (window, startp, 0))
16908 goto need_larger_matrices;
16911 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16913 clear_glyph_matrix (w->desired_matrix);
16914 goto try_to_scroll;
16917 #ifdef GLYPH_DEBUG
16918 debug_method_add (w, "forced window start");
16919 #endif
16920 goto done;
16923 /* Handle case where text has not changed, only point, and it has
16924 not moved off the frame, and we are not retrying after hscroll.
16925 (current_matrix_up_to_date_p is true when retrying.) */
16926 if (current_matrix_up_to_date_p
16927 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16928 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16930 switch (rc)
16932 case CURSOR_MOVEMENT_SUCCESS:
16933 used_current_matrix_p = true;
16934 goto done;
16936 case CURSOR_MOVEMENT_MUST_SCROLL:
16937 goto try_to_scroll;
16939 default:
16940 emacs_abort ();
16943 /* If current starting point was originally the beginning of a line
16944 but no longer is, find a new starting point. */
16945 else if (w->start_at_line_beg
16946 && !(CHARPOS (startp) <= BEGV
16947 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16949 #ifdef GLYPH_DEBUG
16950 debug_method_add (w, "recenter 1");
16951 #endif
16952 goto recenter;
16955 /* Try scrolling with try_window_id. Value is > 0 if update has
16956 been done, it is -1 if we know that the same window start will
16957 not work. It is 0 if unsuccessful for some other reason. */
16958 else if ((tem = try_window_id (w)) != 0)
16960 #ifdef GLYPH_DEBUG
16961 debug_method_add (w, "try_window_id %d", tem);
16962 #endif
16964 if (f->fonts_changed)
16965 goto need_larger_matrices;
16966 if (tem > 0)
16967 goto done;
16969 /* Otherwise try_window_id has returned -1 which means that we
16970 don't want the alternative below this comment to execute. */
16972 else if (CHARPOS (startp) >= BEGV
16973 && CHARPOS (startp) <= ZV
16974 && PT >= CHARPOS (startp)
16975 && (CHARPOS (startp) < ZV
16976 /* Avoid starting at end of buffer. */
16977 || CHARPOS (startp) == BEGV
16978 || !window_outdated (w)))
16980 int d1, d2, d5, d6;
16981 int rtop, rbot;
16983 /* If first window line is a continuation line, and window start
16984 is inside the modified region, but the first change is before
16985 current window start, we must select a new window start.
16987 However, if this is the result of a down-mouse event (e.g. by
16988 extending the mouse-drag-overlay), we don't want to select a
16989 new window start, since that would change the position under
16990 the mouse, resulting in an unwanted mouse-movement rather
16991 than a simple mouse-click. */
16992 if (!w->start_at_line_beg
16993 && NILP (do_mouse_tracking)
16994 && CHARPOS (startp) > BEGV
16995 && CHARPOS (startp) > BEG + beg_unchanged
16996 && CHARPOS (startp) <= Z - end_unchanged
16997 /* Even if w->start_at_line_beg is nil, a new window may
16998 start at a line_beg, since that's how set_buffer_window
16999 sets it. So, we need to check the return value of
17000 compute_window_start_on_continuation_line. (See also
17001 bug#197). */
17002 && XMARKER (w->start)->buffer == current_buffer
17003 && compute_window_start_on_continuation_line (w)
17004 /* It doesn't make sense to force the window start like we
17005 do at label force_start if it is already known that point
17006 will not be fully visible in the resulting window, because
17007 doing so will move point from its correct position
17008 instead of scrolling the window to bring point into view.
17009 See bug#9324. */
17010 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17011 /* A very tall row could need more than the window height,
17012 in which case we accept that it is partially visible. */
17013 && (rtop != 0) == (rbot != 0))
17015 w->force_start = true;
17016 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17017 #ifdef GLYPH_DEBUG
17018 debug_method_add (w, "recomputed window start in continuation line");
17019 #endif
17020 goto force_start;
17023 #ifdef GLYPH_DEBUG
17024 debug_method_add (w, "same window start");
17025 #endif
17027 /* Try to redisplay starting at same place as before.
17028 If point has not moved off frame, accept the results. */
17029 if (!current_matrix_up_to_date_p
17030 /* Don't use try_window_reusing_current_matrix in this case
17031 because a window scroll function can have changed the
17032 buffer. */
17033 || !NILP (Vwindow_scroll_functions)
17034 || MINI_WINDOW_P (w)
17035 || !(used_current_matrix_p
17036 = try_window_reusing_current_matrix (w)))
17038 IF_DEBUG (debug_method_add (w, "1"));
17039 clear_glyph_matrix (w->desired_matrix);
17040 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17041 /* -1 means we need to scroll.
17042 0 means we need new matrices, but fonts_changed
17043 is set in that case, so we will detect it below. */
17044 goto try_to_scroll;
17047 if (f->fonts_changed)
17048 goto need_larger_matrices;
17050 if (w->cursor.vpos >= 0)
17052 if (!just_this_one_p
17053 || current_buffer->clip_changed
17054 || BEG_UNCHANGED < CHARPOS (startp))
17055 /* Forget any recorded base line for line number display. */
17056 w->base_line_number = 0;
17058 if (!cursor_row_fully_visible_p (w, true, false))
17060 clear_glyph_matrix (w->desired_matrix);
17061 last_line_misfit = true;
17063 /* Drop through and scroll. */
17064 else
17065 goto done;
17067 else
17068 clear_glyph_matrix (w->desired_matrix);
17071 try_to_scroll:
17073 /* Redisplay the mode line. Select the buffer properly for that. */
17074 if (!update_mode_line)
17076 update_mode_line = true;
17077 w->update_mode_line = true;
17080 /* Try to scroll by specified few lines. */
17081 if ((scroll_conservatively
17082 || emacs_scroll_step
17083 || temp_scroll_step
17084 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17085 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17086 && CHARPOS (startp) >= BEGV
17087 && CHARPOS (startp) <= ZV)
17089 /* The function returns -1 if new fonts were loaded, 1 if
17090 successful, 0 if not successful. */
17091 int ss = try_scrolling (window, just_this_one_p,
17092 scroll_conservatively,
17093 emacs_scroll_step,
17094 temp_scroll_step, last_line_misfit);
17095 switch (ss)
17097 case SCROLLING_SUCCESS:
17098 goto done;
17100 case SCROLLING_NEED_LARGER_MATRICES:
17101 goto need_larger_matrices;
17103 case SCROLLING_FAILED:
17104 break;
17106 default:
17107 emacs_abort ();
17111 /* Finally, just choose a place to start which positions point
17112 according to user preferences. */
17114 recenter:
17116 #ifdef GLYPH_DEBUG
17117 debug_method_add (w, "recenter");
17118 #endif
17120 /* Forget any previously recorded base line for line number display. */
17121 if (!buffer_unchanged_p)
17122 w->base_line_number = 0;
17124 /* Determine the window start relative to point. */
17125 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17126 it.current_y = it.last_visible_y;
17127 if (centering_position < 0)
17129 ptrdiff_t margin_pos = CHARPOS (startp);
17130 Lisp_Object aggressive;
17131 bool scrolling_up;
17133 /* If there is a scroll margin at the top of the window, find
17134 its character position. */
17135 if (margin
17136 /* Cannot call start_display if startp is not in the
17137 accessible region of the buffer. This can happen when we
17138 have just switched to a different buffer and/or changed
17139 its restriction. In that case, startp is initialized to
17140 the character position 1 (BEGV) because we did not yet
17141 have chance to display the buffer even once. */
17142 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17144 struct it it1;
17145 void *it1data = NULL;
17147 SAVE_IT (it1, it, it1data);
17148 start_display (&it1, w, startp);
17149 move_it_vertically (&it1, margin * frame_line_height);
17150 margin_pos = IT_CHARPOS (it1);
17151 RESTORE_IT (&it, &it, it1data);
17153 scrolling_up = PT > margin_pos;
17154 aggressive =
17155 scrolling_up
17156 ? BVAR (current_buffer, scroll_up_aggressively)
17157 : BVAR (current_buffer, scroll_down_aggressively);
17159 if (!MINI_WINDOW_P (w)
17160 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17162 int pt_offset = 0;
17164 /* Setting scroll-conservatively overrides
17165 scroll-*-aggressively. */
17166 if (!scroll_conservatively && NUMBERP (aggressive))
17168 double float_amount = XFLOATINT (aggressive);
17170 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17171 if (pt_offset == 0 && float_amount > 0)
17172 pt_offset = 1;
17173 if (pt_offset && margin > 0)
17174 margin -= 1;
17176 /* Compute how much to move the window start backward from
17177 point so that point will be displayed where the user
17178 wants it. */
17179 if (scrolling_up)
17181 centering_position = it.last_visible_y;
17182 if (pt_offset)
17183 centering_position -= pt_offset;
17184 centering_position -=
17185 (frame_line_height * (1 + margin + last_line_misfit)
17186 + WINDOW_HEADER_LINE_HEIGHT (w));
17187 /* Don't let point enter the scroll margin near top of
17188 the window. */
17189 if (centering_position < margin * frame_line_height)
17190 centering_position = margin * frame_line_height;
17192 else
17193 centering_position = margin * frame_line_height + pt_offset;
17195 else
17196 /* Set the window start half the height of the window backward
17197 from point. */
17198 centering_position = window_box_height (w) / 2;
17200 move_it_vertically_backward (&it, centering_position);
17202 eassert (IT_CHARPOS (it) >= BEGV);
17204 /* The function move_it_vertically_backward may move over more
17205 than the specified y-distance. If it->w is small, e.g. a
17206 mini-buffer window, we may end up in front of the window's
17207 display area. Start displaying at the start of the line
17208 containing PT in this case. */
17209 if (it.current_y <= 0)
17211 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17212 move_it_vertically_backward (&it, 0);
17213 it.current_y = 0;
17216 it.current_x = it.hpos = 0;
17218 /* Set the window start position here explicitly, to avoid an
17219 infinite loop in case the functions in window-scroll-functions
17220 get errors. */
17221 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17223 /* Run scroll hooks. */
17224 startp = run_window_scroll_functions (window, it.current.pos);
17226 /* We invoke try_window and try_window_reusing_current_matrix below,
17227 and they manipulate the bidi cache. Save and restore the cache
17228 state of our iterator, so we could continue using it after that. */
17229 itdata = bidi_shelve_cache ();
17231 /* Redisplay the window. */
17232 use_desired_matrix = false;
17233 if (!current_matrix_up_to_date_p
17234 || windows_or_buffers_changed
17235 || f->cursor_type_changed
17236 /* Don't use try_window_reusing_current_matrix in this case
17237 because it can have changed the buffer. */
17238 || !NILP (Vwindow_scroll_functions)
17239 || !just_this_one_p
17240 || MINI_WINDOW_P (w)
17241 || !(used_current_matrix_p
17242 = try_window_reusing_current_matrix (w)))
17243 use_desired_matrix = (try_window (window, startp, 0) == 1);
17245 bidi_unshelve_cache (itdata, false);
17247 /* If new fonts have been loaded (due to fontsets), give up. We
17248 have to start a new redisplay since we need to re-adjust glyph
17249 matrices. */
17250 if (f->fonts_changed)
17251 goto need_larger_matrices;
17253 /* If cursor did not appear assume that the middle of the window is
17254 in the first line of the window. Do it again with the next line.
17255 (Imagine a window of height 100, displaying two lines of height
17256 60. Moving back 50 from it->last_visible_y will end in the first
17257 line.) */
17258 if (w->cursor.vpos < 0)
17260 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17262 clear_glyph_matrix (w->desired_matrix);
17263 move_it_by_lines (&it, 1);
17264 try_window (window, it.current.pos, 0);
17266 else if (PT < IT_CHARPOS (it))
17268 clear_glyph_matrix (w->desired_matrix);
17269 move_it_by_lines (&it, -1);
17270 try_window (window, it.current.pos, 0);
17272 else if (scroll_conservatively > SCROLL_LIMIT
17273 && (it.method == GET_FROM_STRING
17274 || overlay_touches_p (IT_CHARPOS (it)))
17275 && IT_CHARPOS (it) < ZV)
17277 /* If the window starts with a before-string that spans more
17278 than one screen line, using that position to display the
17279 window might fail to bring point into the view, because
17280 start_display will always start by displaying the string,
17281 whereas the code above determines where to set w->start
17282 by the buffer position of the place where it takes screen
17283 coordinates. Try to recover by finding the next screen
17284 line that displays buffer text. */
17285 ptrdiff_t pos0 = IT_CHARPOS (it);
17287 clear_glyph_matrix (w->desired_matrix);
17288 do {
17289 move_it_by_lines (&it, 1);
17290 } while (IT_CHARPOS (it) == pos0);
17291 try_window (window, it.current.pos, 0);
17293 else
17295 /* Not much we can do about it. */
17299 /* Consider the following case: Window starts at BEGV, there is
17300 invisible, intangible text at BEGV, so that display starts at
17301 some point START > BEGV. It can happen that we are called with
17302 PT somewhere between BEGV and START. Try to handle that case,
17303 and similar ones. */
17304 if (w->cursor.vpos < 0)
17306 /* Prefer the desired matrix to the current matrix, if possible,
17307 in the fallback calculations below. This is because using
17308 the current matrix might completely goof, e.g. if its first
17309 row is after point. */
17310 struct glyph_matrix *matrix =
17311 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17312 /* First, try locating the proper glyph row for PT. */
17313 struct glyph_row *row =
17314 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17316 /* Sometimes point is at the beginning of invisible text that is
17317 before the 1st character displayed in the row. In that case,
17318 row_containing_pos fails to find the row, because no glyphs
17319 with appropriate buffer positions are present in the row.
17320 Therefore, we next try to find the row which shows the 1st
17321 position after the invisible text. */
17322 if (!row)
17324 Lisp_Object val =
17325 get_char_property_and_overlay (make_number (PT), Qinvisible,
17326 Qnil, NULL);
17328 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17330 ptrdiff_t alt_pos;
17331 Lisp_Object invis_end =
17332 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17333 Qnil, Qnil);
17335 if (NATNUMP (invis_end))
17336 alt_pos = XFASTINT (invis_end);
17337 else
17338 alt_pos = ZV;
17339 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17342 /* Finally, fall back on the first row of the window after the
17343 header line (if any). This is slightly better than not
17344 displaying the cursor at all. */
17345 if (!row)
17347 row = matrix->rows;
17348 if (row->mode_line_p)
17349 ++row;
17351 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17354 if (!cursor_row_fully_visible_p (w, false, false))
17356 /* If vscroll is enabled, disable it and try again. */
17357 if (w->vscroll)
17359 w->vscroll = 0;
17360 clear_glyph_matrix (w->desired_matrix);
17361 goto recenter;
17364 /* Users who set scroll-conservatively to a large number want
17365 point just above/below the scroll margin. If we ended up
17366 with point's row partially visible, move the window start to
17367 make that row fully visible and out of the margin. */
17368 if (scroll_conservatively > SCROLL_LIMIT)
17370 int window_total_lines
17371 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17372 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17374 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17375 clear_glyph_matrix (w->desired_matrix);
17376 if (1 == try_window (window, it.current.pos,
17377 TRY_WINDOW_CHECK_MARGINS))
17378 goto done;
17381 /* If centering point failed to make the whole line visible,
17382 put point at the top instead. That has to make the whole line
17383 visible, if it can be done. */
17384 if (centering_position == 0)
17385 goto done;
17387 clear_glyph_matrix (w->desired_matrix);
17388 centering_position = 0;
17389 goto recenter;
17392 done:
17394 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17395 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17396 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17398 /* Display the mode line, if we must. */
17399 if ((update_mode_line
17400 /* If window not full width, must redo its mode line
17401 if (a) the window to its side is being redone and
17402 (b) we do a frame-based redisplay. This is a consequence
17403 of how inverted lines are drawn in frame-based redisplay. */
17404 || (!just_this_one_p
17405 && !FRAME_WINDOW_P (f)
17406 && !WINDOW_FULL_WIDTH_P (w))
17407 /* Line number to display. */
17408 || w->base_line_pos > 0
17409 /* Column number is displayed and different from the one displayed. */
17410 || (w->column_number_displayed != -1
17411 && (w->column_number_displayed != current_column ())))
17412 /* This means that the window has a mode line. */
17413 && (window_wants_mode_line (w)
17414 || window_wants_header_line (w)))
17417 display_mode_lines (w);
17419 /* If mode line height has changed, arrange for a thorough
17420 immediate redisplay using the correct mode line height. */
17421 if (window_wants_mode_line (w)
17422 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17424 f->fonts_changed = true;
17425 w->mode_line_height = -1;
17426 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17427 = DESIRED_MODE_LINE_HEIGHT (w);
17430 /* If header line height has changed, arrange for a thorough
17431 immediate redisplay using the correct header line height. */
17432 if (window_wants_header_line (w)
17433 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17435 f->fonts_changed = true;
17436 w->header_line_height = -1;
17437 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17438 = DESIRED_HEADER_LINE_HEIGHT (w);
17441 if (f->fonts_changed)
17442 goto need_larger_matrices;
17445 if (!line_number_displayed && w->base_line_pos != -1)
17447 w->base_line_pos = 0;
17448 w->base_line_number = 0;
17451 finish_menu_bars:
17453 /* When we reach a frame's selected window, redo the frame's menu
17454 bar and the frame's title. */
17455 if (update_mode_line
17456 && EQ (FRAME_SELECTED_WINDOW (f), window))
17458 bool redisplay_menu_p;
17460 if (FRAME_WINDOW_P (f))
17462 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17463 || defined (HAVE_NS) || defined (USE_GTK)
17464 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17465 #else
17466 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17467 #endif
17469 else
17470 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17472 if (redisplay_menu_p)
17473 display_menu_bar (w);
17475 #ifdef HAVE_WINDOW_SYSTEM
17476 if (FRAME_WINDOW_P (f))
17478 #if defined (USE_GTK) || defined (HAVE_NS)
17479 if (FRAME_EXTERNAL_TOOL_BAR (f))
17480 redisplay_tool_bar (f);
17481 #else
17482 if (WINDOWP (f->tool_bar_window)
17483 && (FRAME_TOOL_BAR_LINES (f) > 0
17484 || !NILP (Vauto_resize_tool_bars))
17485 && redisplay_tool_bar (f))
17486 ignore_mouse_drag_p = true;
17487 #endif
17489 x_consider_frame_title (w->frame);
17490 #endif
17493 #ifdef HAVE_WINDOW_SYSTEM
17494 if (FRAME_WINDOW_P (f)
17495 && update_window_fringes (w, (just_this_one_p
17496 || (!used_current_matrix_p && !overlay_arrow_seen)
17497 || w->pseudo_window_p)))
17499 update_begin (f);
17500 block_input ();
17501 if (draw_window_fringes (w, true))
17503 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17504 x_draw_right_divider (w);
17505 else
17506 x_draw_vertical_border (w);
17508 unblock_input ();
17509 update_end (f);
17512 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17513 x_draw_bottom_divider (w);
17514 #endif /* HAVE_WINDOW_SYSTEM */
17516 /* We go to this label, with fonts_changed set, if it is
17517 necessary to try again using larger glyph matrices.
17518 We have to redeem the scroll bar even in this case,
17519 because the loop in redisplay_internal expects that. */
17520 need_larger_matrices:
17522 finish_scroll_bars:
17524 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17526 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17527 /* Set the thumb's position and size. */
17528 set_vertical_scroll_bar (w);
17530 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17531 /* Set the thumb's position and size. */
17532 set_horizontal_scroll_bar (w);
17534 /* Note that we actually used the scroll bar attached to this
17535 window, so it shouldn't be deleted at the end of redisplay. */
17536 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17537 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17540 /* Restore current_buffer and value of point in it. The window
17541 update may have changed the buffer, so first make sure `opoint'
17542 is still valid (Bug#6177). */
17543 if (CHARPOS (opoint) < BEGV)
17544 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17545 else if (CHARPOS (opoint) > ZV)
17546 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17547 else
17548 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17550 set_buffer_internal_1 (old);
17551 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17552 shorter. This can be caused by log truncation in *Messages*. */
17553 if (CHARPOS (lpoint) <= ZV)
17554 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17556 unbind_to (count, Qnil);
17560 /* Build the complete desired matrix of WINDOW with a window start
17561 buffer position POS.
17563 Value is 1 if successful. It is zero if fonts were loaded during
17564 redisplay which makes re-adjusting glyph matrices necessary, and -1
17565 if point would appear in the scroll margins.
17566 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17567 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17568 set in FLAGS.) */
17571 try_window (Lisp_Object window, struct text_pos pos, int flags)
17573 struct window *w = XWINDOW (window);
17574 struct it it;
17575 struct glyph_row *last_text_row = NULL;
17576 struct frame *f = XFRAME (w->frame);
17577 int cursor_vpos = w->cursor.vpos;
17579 /* Make POS the new window start. */
17580 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17582 /* Mark cursor position as unknown. No overlay arrow seen. */
17583 w->cursor.vpos = -1;
17584 overlay_arrow_seen = false;
17586 /* Initialize iterator and info to start at POS. */
17587 start_display (&it, w, pos);
17588 it.glyph_row->reversed_p = false;
17590 /* Display all lines of W. */
17591 while (it.current_y < it.last_visible_y)
17593 if (display_line (&it, cursor_vpos))
17594 last_text_row = it.glyph_row - 1;
17595 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17596 return 0;
17599 /* Save the character position of 'it' before we call
17600 'start_display' again. */
17601 ptrdiff_t it_charpos = IT_CHARPOS (it);
17603 /* Don't let the cursor end in the scroll margins. */
17604 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17605 && !MINI_WINDOW_P (w))
17607 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17608 start_display (&it, w, pos);
17610 if ((w->cursor.y >= 0 /* not vscrolled */
17611 && w->cursor.y < this_scroll_margin
17612 && CHARPOS (pos) > BEGV
17613 && it_charpos < ZV)
17614 /* rms: considering make_cursor_line_fully_visible_p here
17615 seems to give wrong results. We don't want to recenter
17616 when the last line is partly visible, we want to allow
17617 that case to be handled in the usual way. */
17618 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17619 - this_scroll_margin - 1))
17621 w->cursor.vpos = -1;
17622 clear_glyph_matrix (w->desired_matrix);
17623 return -1;
17627 /* If bottom moved off end of frame, change mode line percentage. */
17628 if (w->window_end_pos <= 0 && Z != it_charpos)
17629 w->update_mode_line = true;
17631 /* Set window_end_pos to the offset of the last character displayed
17632 on the window from the end of current_buffer. Set
17633 window_end_vpos to its row number. */
17634 if (last_text_row)
17636 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17637 adjust_window_ends (w, last_text_row, false);
17638 eassert
17639 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17640 w->window_end_vpos)));
17642 else
17644 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17645 w->window_end_pos = Z - ZV;
17646 w->window_end_vpos = 0;
17649 /* But that is not valid info until redisplay finishes. */
17650 w->window_end_valid = false;
17651 return 1;
17656 /************************************************************************
17657 Window redisplay reusing current matrix when buffer has not changed
17658 ************************************************************************/
17660 /* Try redisplay of window W showing an unchanged buffer with a
17661 different window start than the last time it was displayed by
17662 reusing its current matrix. Value is true if successful.
17663 W->start is the new window start. */
17665 static bool
17666 try_window_reusing_current_matrix (struct window *w)
17668 struct frame *f = XFRAME (w->frame);
17669 struct glyph_row *bottom_row;
17670 struct it it;
17671 struct run run;
17672 struct text_pos start, new_start;
17673 int nrows_scrolled, i;
17674 struct glyph_row *last_text_row;
17675 struct glyph_row *last_reused_text_row;
17676 struct glyph_row *start_row;
17677 int start_vpos, min_y, max_y;
17679 #ifdef GLYPH_DEBUG
17680 if (inhibit_try_window_reusing)
17681 return false;
17682 #endif
17684 if (/* This function doesn't handle terminal frames. */
17685 !FRAME_WINDOW_P (f)
17686 /* Don't try to reuse the display if windows have been split
17687 or such. */
17688 || windows_or_buffers_changed
17689 || f->cursor_type_changed)
17690 return false;
17692 /* Can't do this if showing trailing whitespace. */
17693 if (!NILP (Vshow_trailing_whitespace))
17694 return false;
17696 /* If top-line visibility has changed, give up. */
17697 if (window_wants_header_line (w)
17698 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17699 return false;
17701 /* Give up if old or new display is scrolled vertically. We could
17702 make this function handle this, but right now it doesn't. */
17703 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17704 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17705 return false;
17707 /* Clear the desired matrix for the display below. */
17708 clear_glyph_matrix (w->desired_matrix);
17710 /* Give up if line numbers are being displayed, because reusing the
17711 current matrix might use the wrong width for line-number
17712 display. */
17713 if (!NILP (Vdisplay_line_numbers))
17714 return false;
17716 /* The variable new_start now holds the new window start. The old
17717 start `start' can be determined from the current matrix. */
17718 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17719 start = start_row->minpos;
17720 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17722 if (CHARPOS (new_start) <= CHARPOS (start))
17724 /* Don't use this method if the display starts with an ellipsis
17725 displayed for invisible text. It's not easy to handle that case
17726 below, and it's certainly not worth the effort since this is
17727 not a frequent case. */
17728 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17729 return false;
17731 IF_DEBUG (debug_method_add (w, "twu1"));
17733 /* Display up to a row that can be reused. The variable
17734 last_text_row is set to the last row displayed that displays
17735 text. Note that it.vpos == 0 if or if not there is a
17736 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17737 start_display (&it, w, new_start);
17738 w->cursor.vpos = -1;
17739 last_text_row = last_reused_text_row = NULL;
17741 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17743 /* If we have reached into the characters in the START row,
17744 that means the line boundaries have changed. So we
17745 can't start copying with the row START. Maybe it will
17746 work to start copying with the following row. */
17747 while (IT_CHARPOS (it) > CHARPOS (start))
17749 /* Advance to the next row as the "start". */
17750 start_row++;
17751 start = start_row->minpos;
17752 /* If there are no more rows to try, or just one, give up. */
17753 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17754 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17755 || CHARPOS (start) == ZV)
17757 clear_glyph_matrix (w->desired_matrix);
17758 return false;
17761 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17763 /* If we have reached alignment, we can copy the rest of the
17764 rows. */
17765 if (IT_CHARPOS (it) == CHARPOS (start)
17766 /* Don't accept "alignment" inside a display vector,
17767 since start_row could have started in the middle of
17768 that same display vector (thus their character
17769 positions match), and we have no way of telling if
17770 that is the case. */
17771 && it.current.dpvec_index < 0)
17772 break;
17774 it.glyph_row->reversed_p = false;
17775 if (display_line (&it, -1))
17776 last_text_row = it.glyph_row - 1;
17780 /* A value of current_y < last_visible_y means that we stopped
17781 at the previous window start, which in turn means that we
17782 have at least one reusable row. */
17783 if (it.current_y < it.last_visible_y)
17785 struct glyph_row *row;
17787 /* IT.vpos always starts from 0; it counts text lines. */
17788 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17790 /* Find PT if not already found in the lines displayed. */
17791 if (w->cursor.vpos < 0)
17793 int dy = it.current_y - start_row->y;
17795 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17796 row = row_containing_pos (w, PT, row, NULL, dy);
17797 if (row)
17798 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17799 dy, nrows_scrolled);
17800 else
17802 clear_glyph_matrix (w->desired_matrix);
17803 return false;
17807 /* Scroll the display. Do it before the current matrix is
17808 changed. The problem here is that update has not yet
17809 run, i.e. part of the current matrix is not up to date.
17810 scroll_run_hook will clear the cursor, and use the
17811 current matrix to get the height of the row the cursor is
17812 in. */
17813 run.current_y = start_row->y;
17814 run.desired_y = it.current_y;
17815 run.height = it.last_visible_y - it.current_y;
17817 if (run.height > 0 && run.current_y != run.desired_y)
17819 update_begin (f);
17820 FRAME_RIF (f)->update_window_begin_hook (w);
17821 FRAME_RIF (f)->clear_window_mouse_face (w);
17822 FRAME_RIF (f)->scroll_run_hook (w, &run);
17823 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17824 update_end (f);
17827 /* Shift current matrix down by nrows_scrolled lines. */
17828 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17829 rotate_matrix (w->current_matrix,
17830 start_vpos,
17831 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17832 nrows_scrolled);
17834 /* Disable lines that must be updated. */
17835 for (i = 0; i < nrows_scrolled; ++i)
17836 (start_row + i)->enabled_p = false;
17838 /* Re-compute Y positions. */
17839 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17840 max_y = it.last_visible_y;
17841 for (row = start_row + nrows_scrolled;
17842 row < bottom_row;
17843 ++row)
17845 row->y = it.current_y;
17846 row->visible_height = row->height;
17848 if (row->y < min_y)
17849 row->visible_height -= min_y - row->y;
17850 if (row->y + row->height > max_y)
17851 row->visible_height -= row->y + row->height - max_y;
17852 if (row->fringe_bitmap_periodic_p)
17853 row->redraw_fringe_bitmaps_p = true;
17855 it.current_y += row->height;
17857 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17858 last_reused_text_row = row;
17859 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17860 break;
17863 /* Disable lines in the current matrix which are now
17864 below the window. */
17865 for (++row; row < bottom_row; ++row)
17866 row->enabled_p = row->mode_line_p = false;
17869 /* Update window_end_pos etc.; last_reused_text_row is the last
17870 reused row from the current matrix containing text, if any.
17871 The value of last_text_row is the last displayed line
17872 containing text. */
17873 if (last_reused_text_row)
17874 adjust_window_ends (w, last_reused_text_row, true);
17875 else if (last_text_row)
17876 adjust_window_ends (w, last_text_row, false);
17877 else
17879 /* This window must be completely empty. */
17880 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17881 w->window_end_pos = Z - ZV;
17882 w->window_end_vpos = 0;
17884 w->window_end_valid = false;
17886 /* Update hint: don't try scrolling again in update_window. */
17887 w->desired_matrix->no_scrolling_p = true;
17889 #ifdef GLYPH_DEBUG
17890 debug_method_add (w, "try_window_reusing_current_matrix 1");
17891 #endif
17892 return true;
17894 else if (CHARPOS (new_start) > CHARPOS (start))
17896 struct glyph_row *pt_row, *row;
17897 struct glyph_row *first_reusable_row;
17898 struct glyph_row *first_row_to_display;
17899 int dy;
17900 int yb = window_text_bottom_y (w);
17902 /* Find the row starting at new_start, if there is one. Don't
17903 reuse a partially visible line at the end. */
17904 first_reusable_row = start_row;
17905 while (first_reusable_row->enabled_p
17906 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17907 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17908 < CHARPOS (new_start)))
17909 ++first_reusable_row;
17911 /* Give up if there is no row to reuse. */
17912 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17913 || !first_reusable_row->enabled_p
17914 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17915 != CHARPOS (new_start)))
17916 return false;
17918 /* We can reuse fully visible rows beginning with
17919 first_reusable_row to the end of the window. Set
17920 first_row_to_display to the first row that cannot be reused.
17921 Set pt_row to the row containing point, if there is any. */
17922 pt_row = NULL;
17923 for (first_row_to_display = first_reusable_row;
17924 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17925 ++first_row_to_display)
17927 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17928 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17929 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17930 && first_row_to_display->ends_at_zv_p
17931 && pt_row == NULL)))
17932 pt_row = first_row_to_display;
17935 /* Start displaying at the start of first_row_to_display. */
17936 eassert (first_row_to_display->y < yb);
17937 init_to_row_start (&it, w, first_row_to_display);
17939 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17940 - start_vpos);
17941 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17942 - nrows_scrolled);
17943 it.current_y = (first_row_to_display->y - first_reusable_row->y
17944 + WINDOW_HEADER_LINE_HEIGHT (w));
17946 /* Display lines beginning with first_row_to_display in the
17947 desired matrix. Set last_text_row to the last row displayed
17948 that displays text. */
17949 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17950 if (pt_row == NULL)
17951 w->cursor.vpos = -1;
17952 last_text_row = NULL;
17953 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17954 if (display_line (&it, w->cursor.vpos))
17955 last_text_row = it.glyph_row - 1;
17957 /* If point is in a reused row, adjust y and vpos of the cursor
17958 position. */
17959 if (pt_row)
17961 w->cursor.vpos -= nrows_scrolled;
17962 w->cursor.y -= first_reusable_row->y - start_row->y;
17965 /* Give up if point isn't in a row displayed or reused. (This
17966 also handles the case where w->cursor.vpos < nrows_scrolled
17967 after the calls to display_line, which can happen with scroll
17968 margins. See bug#1295.) */
17969 if (w->cursor.vpos < 0)
17971 clear_glyph_matrix (w->desired_matrix);
17972 return false;
17975 /* Scroll the display. */
17976 run.current_y = first_reusable_row->y;
17977 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17978 run.height = it.last_visible_y - run.current_y;
17979 dy = run.current_y - run.desired_y;
17981 if (run.height)
17983 update_begin (f);
17984 FRAME_RIF (f)->update_window_begin_hook (w);
17985 FRAME_RIF (f)->clear_window_mouse_face (w);
17986 FRAME_RIF (f)->scroll_run_hook (w, &run);
17987 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17988 update_end (f);
17991 /* Adjust Y positions of reused rows. */
17992 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17993 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17994 max_y = it.last_visible_y;
17995 for (row = first_reusable_row; row < first_row_to_display; ++row)
17997 row->y -= dy;
17998 row->visible_height = row->height;
17999 if (row->y < min_y)
18000 row->visible_height -= min_y - row->y;
18001 if (row->y + row->height > max_y)
18002 row->visible_height -= row->y + row->height - max_y;
18003 if (row->fringe_bitmap_periodic_p)
18004 row->redraw_fringe_bitmaps_p = true;
18007 /* Scroll the current matrix. */
18008 eassert (nrows_scrolled > 0);
18009 rotate_matrix (w->current_matrix,
18010 start_vpos,
18011 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18012 -nrows_scrolled);
18014 /* Disable rows not reused. */
18015 for (row -= nrows_scrolled; row < bottom_row; ++row)
18016 row->enabled_p = false;
18018 /* Point may have moved to a different line, so we cannot assume that
18019 the previous cursor position is valid; locate the correct row. */
18020 if (pt_row)
18022 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18023 row < bottom_row
18024 && PT >= MATRIX_ROW_END_CHARPOS (row)
18025 && !row->ends_at_zv_p;
18026 row++)
18028 w->cursor.vpos++;
18029 w->cursor.y = row->y;
18031 if (row < bottom_row)
18033 /* Can't simply scan the row for point with
18034 bidi-reordered glyph rows. Let set_cursor_from_row
18035 figure out where to put the cursor, and if it fails,
18036 give up. */
18037 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18039 if (!set_cursor_from_row (w, row, w->current_matrix,
18040 0, 0, 0, 0))
18042 clear_glyph_matrix (w->desired_matrix);
18043 return false;
18046 else
18048 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18049 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18051 for (; glyph < end
18052 && (!BUFFERP (glyph->object)
18053 || glyph->charpos < PT);
18054 glyph++)
18056 w->cursor.hpos++;
18057 w->cursor.x += glyph->pixel_width;
18063 /* Adjust window end. A null value of last_text_row means that
18064 the window end is in reused rows which in turn means that
18065 only its vpos can have changed. */
18066 if (last_text_row)
18067 adjust_window_ends (w, last_text_row, false);
18068 else
18069 w->window_end_vpos -= nrows_scrolled;
18071 w->window_end_valid = false;
18072 w->desired_matrix->no_scrolling_p = true;
18074 #ifdef GLYPH_DEBUG
18075 debug_method_add (w, "try_window_reusing_current_matrix 2");
18076 #endif
18077 return true;
18080 return false;
18085 /************************************************************************
18086 Window redisplay reusing current matrix when buffer has changed
18087 ************************************************************************/
18089 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18090 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18091 ptrdiff_t *, ptrdiff_t *);
18092 static struct glyph_row *
18093 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18094 struct glyph_row *);
18097 /* Return the last row in MATRIX displaying text. If row START is
18098 non-null, start searching with that row. IT gives the dimensions
18099 of the display. Value is null if matrix is empty; otherwise it is
18100 a pointer to the row found. */
18102 static struct glyph_row *
18103 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18104 struct glyph_row *start)
18106 struct glyph_row *row, *row_found;
18108 /* Set row_found to the last row in IT->w's current matrix
18109 displaying text. The loop looks funny but think of partially
18110 visible lines. */
18111 row_found = NULL;
18112 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18113 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18115 eassert (row->enabled_p);
18116 row_found = row;
18117 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18118 break;
18119 ++row;
18122 return row_found;
18126 /* Return the last row in the current matrix of W that is not affected
18127 by changes at the start of current_buffer that occurred since W's
18128 current matrix was built. Value is null if no such row exists.
18130 BEG_UNCHANGED us the number of characters unchanged at the start of
18131 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18132 first changed character in current_buffer. Characters at positions <
18133 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18134 when the current matrix was built. */
18136 static struct glyph_row *
18137 find_last_unchanged_at_beg_row (struct window *w)
18139 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18140 struct glyph_row *row;
18141 struct glyph_row *row_found = NULL;
18142 int yb = window_text_bottom_y (w);
18144 /* Find the last row displaying unchanged text. */
18145 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18146 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18147 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18148 ++row)
18150 if (/* If row ends before first_changed_pos, it is unchanged,
18151 except in some case. */
18152 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18153 /* When row ends in ZV and we write at ZV it is not
18154 unchanged. */
18155 && !row->ends_at_zv_p
18156 /* When first_changed_pos is the end of a continued line,
18157 row is not unchanged because it may be no longer
18158 continued. */
18159 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18160 && (row->continued_p
18161 || row->exact_window_width_line_p))
18162 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18163 needs to be recomputed, so don't consider this row as
18164 unchanged. This happens when the last line was
18165 bidi-reordered and was killed immediately before this
18166 redisplay cycle. In that case, ROW->end stores the
18167 buffer position of the first visual-order character of
18168 the killed text, which is now beyond ZV. */
18169 && CHARPOS (row->end.pos) <= ZV)
18170 row_found = row;
18172 /* Stop if last visible row. */
18173 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18174 break;
18177 return row_found;
18181 /* Find the first glyph row in the current matrix of W that is not
18182 affected by changes at the end of current_buffer since the
18183 time W's current matrix was built.
18185 Return in *DELTA the number of chars by which buffer positions in
18186 unchanged text at the end of current_buffer must be adjusted.
18188 Return in *DELTA_BYTES the corresponding number of bytes.
18190 Value is null if no such row exists, i.e. all rows are affected by
18191 changes. */
18193 static struct glyph_row *
18194 find_first_unchanged_at_end_row (struct window *w,
18195 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18197 struct glyph_row *row;
18198 struct glyph_row *row_found = NULL;
18200 *delta = *delta_bytes = 0;
18202 /* Display must not have been paused, otherwise the current matrix
18203 is not up to date. */
18204 eassert (w->window_end_valid);
18206 /* A value of window_end_pos >= END_UNCHANGED means that the window
18207 end is in the range of changed text. If so, there is no
18208 unchanged row at the end of W's current matrix. */
18209 if (w->window_end_pos >= END_UNCHANGED)
18210 return NULL;
18212 /* Set row to the last row in W's current matrix displaying text. */
18213 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18215 /* If matrix is entirely empty, no unchanged row exists. */
18216 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18218 /* The value of row is the last glyph row in the matrix having a
18219 meaningful buffer position in it. The end position of row
18220 corresponds to window_end_pos. This allows us to translate
18221 buffer positions in the current matrix to current buffer
18222 positions for characters not in changed text. */
18223 ptrdiff_t Z_old =
18224 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18225 ptrdiff_t Z_BYTE_old =
18226 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18227 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18228 struct glyph_row *first_text_row
18229 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18231 *delta = Z - Z_old;
18232 *delta_bytes = Z_BYTE - Z_BYTE_old;
18234 /* Set last_unchanged_pos to the buffer position of the last
18235 character in the buffer that has not been changed. Z is the
18236 index + 1 of the last character in current_buffer, i.e. by
18237 subtracting END_UNCHANGED we get the index of the last
18238 unchanged character, and we have to add BEG to get its buffer
18239 position. */
18240 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18241 last_unchanged_pos_old = last_unchanged_pos - *delta;
18243 /* Search backward from ROW for a row displaying a line that
18244 starts at a minimum position >= last_unchanged_pos_old. */
18245 for (; row > first_text_row; --row)
18247 /* This used to abort, but it can happen.
18248 It is ok to just stop the search instead here. KFS. */
18249 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18250 break;
18252 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18253 row_found = row;
18257 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18259 return row_found;
18263 /* Make sure that glyph rows in the current matrix of window W
18264 reference the same glyph memory as corresponding rows in the
18265 frame's frame matrix. This function is called after scrolling W's
18266 current matrix on a terminal frame in try_window_id and
18267 try_window_reusing_current_matrix. */
18269 static void
18270 sync_frame_with_window_matrix_rows (struct window *w)
18272 struct frame *f = XFRAME (w->frame);
18273 struct glyph_row *window_row, *window_row_end, *frame_row;
18275 /* Preconditions: W must be a leaf window and full-width. Its frame
18276 must have a frame matrix. */
18277 eassert (BUFFERP (w->contents));
18278 eassert (WINDOW_FULL_WIDTH_P (w));
18279 eassert (!FRAME_WINDOW_P (f));
18281 /* If W is a full-width window, glyph pointers in W's current matrix
18282 have, by definition, to be the same as glyph pointers in the
18283 corresponding frame matrix. Note that frame matrices have no
18284 marginal areas (see build_frame_matrix). */
18285 window_row = w->current_matrix->rows;
18286 window_row_end = window_row + w->current_matrix->nrows;
18287 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18288 while (window_row < window_row_end)
18290 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18291 struct glyph *end = window_row->glyphs[LAST_AREA];
18293 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18294 frame_row->glyphs[TEXT_AREA] = start;
18295 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18296 frame_row->glyphs[LAST_AREA] = end;
18298 /* Disable frame rows whose corresponding window rows have
18299 been disabled in try_window_id. */
18300 if (!window_row->enabled_p)
18301 frame_row->enabled_p = false;
18303 ++window_row, ++frame_row;
18308 /* Find the glyph row in window W containing CHARPOS. Consider all
18309 rows between START and END (not inclusive). END null means search
18310 all rows to the end of the display area of W. Value is the row
18311 containing CHARPOS or null. */
18313 struct glyph_row *
18314 row_containing_pos (struct window *w, ptrdiff_t charpos,
18315 struct glyph_row *start, struct glyph_row *end, int dy)
18317 struct glyph_row *row = start;
18318 struct glyph_row *best_row = NULL;
18319 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18320 int last_y;
18322 /* If we happen to start on a header-line, skip that. */
18323 if (row->mode_line_p)
18324 ++row;
18326 if ((end && row >= end) || !row->enabled_p)
18327 return NULL;
18329 last_y = window_text_bottom_y (w) - dy;
18331 while (true)
18333 /* Give up if we have gone too far. */
18334 if ((end && row >= end) || !row->enabled_p)
18335 return NULL;
18336 /* This formerly returned if they were equal.
18337 I think that both quantities are of a "last plus one" type;
18338 if so, when they are equal, the row is within the screen. -- rms. */
18339 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18340 return NULL;
18342 /* If it is in this row, return this row. */
18343 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18344 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18345 /* The end position of a row equals the start
18346 position of the next row. If CHARPOS is there, we
18347 would rather consider it displayed in the next
18348 line, except when this line ends in ZV. */
18349 && !row_for_charpos_p (row, charpos)))
18350 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18352 struct glyph *g;
18354 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18355 || (!best_row && !row->continued_p))
18356 return row;
18357 /* In bidi-reordered rows, there could be several rows whose
18358 edges surround CHARPOS, all of these rows belonging to
18359 the same continued line. We need to find the row which
18360 fits CHARPOS the best. */
18361 for (g = row->glyphs[TEXT_AREA];
18362 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18363 g++)
18365 if (!STRINGP (g->object))
18367 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18369 mindif = eabs (g->charpos - charpos);
18370 best_row = row;
18371 /* Exact match always wins. */
18372 if (mindif == 0)
18373 return best_row;
18378 else if (best_row && !row->continued_p)
18379 return best_row;
18380 ++row;
18385 /* Try to redisplay window W by reusing its existing display. W's
18386 current matrix must be up to date when this function is called,
18387 i.e., window_end_valid must be true.
18389 Value is
18391 >= 1 if successful, i.e. display has been updated
18392 specifically:
18393 1 means the changes were in front of a newline that precedes
18394 the window start, and the whole current matrix was reused
18395 2 means the changes were after the last position displayed
18396 in the window, and the whole current matrix was reused
18397 3 means portions of the current matrix were reused, while
18398 some of the screen lines were redrawn
18399 -1 if redisplay with same window start is known not to succeed
18400 0 if otherwise unsuccessful
18402 The following steps are performed:
18404 1. Find the last row in the current matrix of W that is not
18405 affected by changes at the start of current_buffer. If no such row
18406 is found, give up.
18408 2. Find the first row in W's current matrix that is not affected by
18409 changes at the end of current_buffer. Maybe there is no such row.
18411 3. Display lines beginning with the row + 1 found in step 1 to the
18412 row found in step 2 or, if step 2 didn't find a row, to the end of
18413 the window.
18415 4. If cursor is not known to appear on the window, give up.
18417 5. If display stopped at the row found in step 2, scroll the
18418 display and current matrix as needed.
18420 6. Maybe display some lines at the end of W, if we must. This can
18421 happen under various circumstances, like a partially visible line
18422 becoming fully visible, or because newly displayed lines are displayed
18423 in smaller font sizes.
18425 7. Update W's window end information. */
18427 static int
18428 try_window_id (struct window *w)
18430 struct frame *f = XFRAME (w->frame);
18431 struct glyph_matrix *current_matrix = w->current_matrix;
18432 struct glyph_matrix *desired_matrix = w->desired_matrix;
18433 struct glyph_row *last_unchanged_at_beg_row;
18434 struct glyph_row *first_unchanged_at_end_row;
18435 struct glyph_row *row;
18436 struct glyph_row *bottom_row;
18437 int bottom_vpos;
18438 struct it it;
18439 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18440 int dvpos, dy;
18441 struct text_pos start_pos;
18442 struct run run;
18443 int first_unchanged_at_end_vpos = 0;
18444 struct glyph_row *last_text_row, *last_text_row_at_end;
18445 struct text_pos start;
18446 ptrdiff_t first_changed_charpos, last_changed_charpos;
18448 #ifdef GLYPH_DEBUG
18449 if (inhibit_try_window_id)
18450 return 0;
18451 #endif
18453 /* This is handy for debugging. */
18454 #if false
18455 #define GIVE_UP(X) \
18456 do { \
18457 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18458 return 0; \
18459 } while (false)
18460 #else
18461 #define GIVE_UP(X) return 0
18462 #endif
18464 SET_TEXT_POS_FROM_MARKER (start, w->start);
18466 /* Don't use this for mini-windows because these can show
18467 messages and mini-buffers, and we don't handle that here. */
18468 if (MINI_WINDOW_P (w))
18469 GIVE_UP (1);
18471 /* This flag is used to prevent redisplay optimizations. */
18472 if (windows_or_buffers_changed || f->cursor_type_changed)
18473 GIVE_UP (2);
18475 /* This function's optimizations cannot be used if overlays have
18476 changed in the buffer displayed by the window, so give up if they
18477 have. */
18478 if (w->last_overlay_modified != OVERLAY_MODIFF)
18479 GIVE_UP (200);
18481 /* Verify that narrowing has not changed.
18482 Also verify that we were not told to prevent redisplay optimizations.
18483 It would be nice to further
18484 reduce the number of cases where this prevents try_window_id. */
18485 if (current_buffer->clip_changed
18486 || current_buffer->prevent_redisplay_optimizations_p)
18487 GIVE_UP (3);
18489 /* Window must either use window-based redisplay or be full width. */
18490 if (!FRAME_WINDOW_P (f)
18491 && (!FRAME_LINE_INS_DEL_OK (f)
18492 || !WINDOW_FULL_WIDTH_P (w)))
18493 GIVE_UP (4);
18495 /* Give up if point is known NOT to appear in W. */
18496 if (PT < CHARPOS (start))
18497 GIVE_UP (5);
18499 /* Another way to prevent redisplay optimizations. */
18500 if (w->last_modified == 0)
18501 GIVE_UP (6);
18503 /* Verify that window is not hscrolled. */
18504 if (w->hscroll != 0)
18505 GIVE_UP (7);
18507 /* Verify that display wasn't paused. */
18508 if (!w->window_end_valid)
18509 GIVE_UP (8);
18511 /* Likewise if highlighting trailing whitespace. */
18512 if (!NILP (Vshow_trailing_whitespace))
18513 GIVE_UP (11);
18515 /* Can't use this if overlay arrow position and/or string have
18516 changed. */
18517 if (overlay_arrows_changed_p (false))
18518 GIVE_UP (12);
18520 /* When word-wrap is on, adding a space to the first word of a
18521 wrapped line can change the wrap position, altering the line
18522 above it. It might be worthwhile to handle this more
18523 intelligently, but for now just redisplay from scratch. */
18524 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18525 GIVE_UP (21);
18527 /* Under bidi reordering, adding or deleting a character in the
18528 beginning of a paragraph, before the first strong directional
18529 character, can change the base direction of the paragraph (unless
18530 the buffer specifies a fixed paragraph direction), which will
18531 require redisplaying the whole paragraph. It might be worthwhile
18532 to find the paragraph limits and widen the range of redisplayed
18533 lines to that, but for now just give up this optimization and
18534 redisplay from scratch. */
18535 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18536 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18537 GIVE_UP (22);
18539 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18540 to that variable require thorough redisplay. */
18541 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18542 GIVE_UP (23);
18544 /* Give up if display-line-numbers is in relative mode, or when the
18545 current line's number needs to be displayed in a distinct face. */
18546 if (EQ (Vdisplay_line_numbers, Qrelative)
18547 || EQ (Vdisplay_line_numbers, Qvisual)
18548 || (!NILP (Vdisplay_line_numbers)
18549 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18550 Qline_number_current_line,
18551 w->frame))))
18552 GIVE_UP (24);
18554 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18555 only if buffer has really changed. The reason is that the gap is
18556 initially at Z for freshly visited files. The code below would
18557 set end_unchanged to 0 in that case. */
18558 if (MODIFF > SAVE_MODIFF
18559 /* This seems to happen sometimes after saving a buffer. */
18560 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18562 if (GPT - BEG < BEG_UNCHANGED)
18563 BEG_UNCHANGED = GPT - BEG;
18564 if (Z - GPT < END_UNCHANGED)
18565 END_UNCHANGED = Z - GPT;
18568 /* The position of the first and last character that has been changed. */
18569 first_changed_charpos = BEG + BEG_UNCHANGED;
18570 last_changed_charpos = Z - END_UNCHANGED;
18572 /* If window starts after a line end, and the last change is in
18573 front of that newline, then changes don't affect the display.
18574 This case happens with stealth-fontification. Note that although
18575 the display is unchanged, glyph positions in the matrix have to
18576 be adjusted, of course. */
18577 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18578 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18579 && ((last_changed_charpos < CHARPOS (start)
18580 && CHARPOS (start) == BEGV)
18581 || (last_changed_charpos < CHARPOS (start) - 1
18582 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18584 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18585 struct glyph_row *r0;
18587 /* Compute how many chars/bytes have been added to or removed
18588 from the buffer. */
18589 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18590 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18591 Z_delta = Z - Z_old;
18592 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18594 /* Give up if PT is not in the window. Note that it already has
18595 been checked at the start of try_window_id that PT is not in
18596 front of the window start. */
18597 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18598 GIVE_UP (13);
18600 /* If window start is unchanged, we can reuse the whole matrix
18601 as is, after adjusting glyph positions. No need to compute
18602 the window end again, since its offset from Z hasn't changed. */
18603 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18604 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18605 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18606 /* PT must not be in a partially visible line. */
18607 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18608 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18610 /* Adjust positions in the glyph matrix. */
18611 if (Z_delta || Z_delta_bytes)
18613 struct glyph_row *r1
18614 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18615 increment_matrix_positions (w->current_matrix,
18616 MATRIX_ROW_VPOS (r0, current_matrix),
18617 MATRIX_ROW_VPOS (r1, current_matrix),
18618 Z_delta, Z_delta_bytes);
18621 /* Set the cursor. */
18622 row = row_containing_pos (w, PT, r0, NULL, 0);
18623 if (row)
18624 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18625 return 1;
18629 /* Handle the case that changes are all below what is displayed in
18630 the window, and that PT is in the window. This shortcut cannot
18631 be taken if ZV is visible in the window, and text has been added
18632 there that is visible in the window. */
18633 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18634 /* ZV is not visible in the window, or there are no
18635 changes at ZV, actually. */
18636 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18637 || first_changed_charpos == last_changed_charpos))
18639 struct glyph_row *r0;
18641 /* Give up if PT is not in the window. Note that it already has
18642 been checked at the start of try_window_id that PT is not in
18643 front of the window start. */
18644 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18645 GIVE_UP (14);
18647 /* If window start is unchanged, we can reuse the whole matrix
18648 as is, without changing glyph positions since no text has
18649 been added/removed in front of the window end. */
18650 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18651 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18652 /* PT must not be in a partially visible line. */
18653 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18654 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18656 /* We have to compute the window end anew since text
18657 could have been added/removed after it. */
18658 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18659 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18661 /* Set the cursor. */
18662 row = row_containing_pos (w, PT, r0, NULL, 0);
18663 if (row)
18664 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18665 return 2;
18669 /* Give up if window start is in the changed area.
18671 The condition used to read
18673 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18675 but why that was tested escapes me at the moment. */
18676 if (CHARPOS (start) >= first_changed_charpos
18677 && CHARPOS (start) <= last_changed_charpos)
18678 GIVE_UP (15);
18680 /* Check that window start agrees with the start of the first glyph
18681 row in its current matrix. Check this after we know the window
18682 start is not in changed text, otherwise positions would not be
18683 comparable. */
18684 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18685 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18686 GIVE_UP (16);
18688 /* Give up if the window ends in strings. Overlay strings
18689 at the end are difficult to handle, so don't try. */
18690 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18691 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18692 GIVE_UP (20);
18694 /* Compute the position at which we have to start displaying new
18695 lines. Some of the lines at the top of the window might be
18696 reusable because they are not displaying changed text. Find the
18697 last row in W's current matrix not affected by changes at the
18698 start of current_buffer. Value is null if changes start in the
18699 first line of window. */
18700 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18701 if (last_unchanged_at_beg_row)
18703 /* Avoid starting to display in the middle of a character, a TAB
18704 for instance. This is easier than to set up the iterator
18705 exactly, and it's not a frequent case, so the additional
18706 effort wouldn't really pay off. */
18707 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18708 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18709 && last_unchanged_at_beg_row > w->current_matrix->rows)
18710 --last_unchanged_at_beg_row;
18712 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18713 GIVE_UP (17);
18715 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18716 GIVE_UP (18);
18717 start_pos = it.current.pos;
18719 /* Start displaying new lines in the desired matrix at the same
18720 vpos we would use in the current matrix, i.e. below
18721 last_unchanged_at_beg_row. */
18722 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18723 current_matrix);
18724 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18725 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18727 eassert (it.hpos == 0 && it.current_x == 0);
18729 else
18731 /* There are no reusable lines at the start of the window.
18732 Start displaying in the first text line. */
18733 start_display (&it, w, start);
18734 it.vpos = it.first_vpos;
18735 start_pos = it.current.pos;
18738 /* Find the first row that is not affected by changes at the end of
18739 the buffer. Value will be null if there is no unchanged row, in
18740 which case we must redisplay to the end of the window. delta
18741 will be set to the value by which buffer positions beginning with
18742 first_unchanged_at_end_row have to be adjusted due to text
18743 changes. */
18744 first_unchanged_at_end_row
18745 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18746 IF_DEBUG (debug_delta = delta);
18747 IF_DEBUG (debug_delta_bytes = delta_bytes);
18749 /* Set stop_pos to the buffer position up to which we will have to
18750 display new lines. If first_unchanged_at_end_row != NULL, this
18751 is the buffer position of the start of the line displayed in that
18752 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18753 that we don't stop at a buffer position. */
18754 stop_pos = 0;
18755 if (first_unchanged_at_end_row)
18757 eassert (last_unchanged_at_beg_row == NULL
18758 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18760 /* If this is a continuation line, move forward to the next one
18761 that isn't. Changes in lines above affect this line.
18762 Caution: this may move first_unchanged_at_end_row to a row
18763 not displaying text. */
18764 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18765 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18766 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18767 < it.last_visible_y))
18768 ++first_unchanged_at_end_row;
18770 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18771 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18772 >= it.last_visible_y))
18773 first_unchanged_at_end_row = NULL;
18774 else
18776 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18777 + delta);
18778 first_unchanged_at_end_vpos
18779 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18780 eassert (stop_pos >= Z - END_UNCHANGED);
18783 else if (last_unchanged_at_beg_row == NULL)
18784 GIVE_UP (19);
18787 #ifdef GLYPH_DEBUG
18789 /* Either there is no unchanged row at the end, or the one we have
18790 now displays text. This is a necessary condition for the window
18791 end pos calculation at the end of this function. */
18792 eassert (first_unchanged_at_end_row == NULL
18793 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18795 debug_last_unchanged_at_beg_vpos
18796 = (last_unchanged_at_beg_row
18797 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18798 : -1);
18799 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18801 #endif /* GLYPH_DEBUG */
18804 /* Display new lines. Set last_text_row to the last new line
18805 displayed which has text on it, i.e. might end up as being the
18806 line where the window_end_vpos is. */
18807 w->cursor.vpos = -1;
18808 last_text_row = NULL;
18809 overlay_arrow_seen = false;
18810 if (it.current_y < it.last_visible_y
18811 && !f->fonts_changed
18812 && (first_unchanged_at_end_row == NULL
18813 || IT_CHARPOS (it) < stop_pos))
18814 it.glyph_row->reversed_p = false;
18815 while (it.current_y < it.last_visible_y
18816 && !f->fonts_changed
18817 && (first_unchanged_at_end_row == NULL
18818 || IT_CHARPOS (it) < stop_pos))
18820 if (display_line (&it, -1))
18821 last_text_row = it.glyph_row - 1;
18824 if (f->fonts_changed)
18825 return -1;
18827 /* The redisplay iterations in display_line above could have
18828 triggered font-lock, which could have done something that
18829 invalidates IT->w window's end-point information, on which we
18830 rely below. E.g., one package, which will remain unnamed, used
18831 to install a font-lock-fontify-region-function that called
18832 bury-buffer, whose side effect is to switch the buffer displayed
18833 by IT->w, and that predictably resets IT->w's window_end_valid
18834 flag, which we already tested at the entry to this function.
18835 Amply punish such packages/modes by giving up on this
18836 optimization in those cases. */
18837 if (!w->window_end_valid)
18839 clear_glyph_matrix (w->desired_matrix);
18840 return -1;
18843 /* Compute differences in buffer positions, y-positions etc. for
18844 lines reused at the bottom of the window. Compute what we can
18845 scroll. */
18846 if (first_unchanged_at_end_row
18847 /* No lines reused because we displayed everything up to the
18848 bottom of the window. */
18849 && it.current_y < it.last_visible_y)
18851 dvpos = (it.vpos
18852 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18853 current_matrix));
18854 dy = it.current_y - first_unchanged_at_end_row->y;
18855 run.current_y = first_unchanged_at_end_row->y;
18856 run.desired_y = run.current_y + dy;
18857 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18859 else
18861 delta = delta_bytes = dvpos = dy
18862 = run.current_y = run.desired_y = run.height = 0;
18863 first_unchanged_at_end_row = NULL;
18865 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18868 /* Find the cursor if not already found. We have to decide whether
18869 PT will appear on this window (it sometimes doesn't, but this is
18870 not a very frequent case.) This decision has to be made before
18871 the current matrix is altered. A value of cursor.vpos < 0 means
18872 that PT is either in one of the lines beginning at
18873 first_unchanged_at_end_row or below the window. Don't care for
18874 lines that might be displayed later at the window end; as
18875 mentioned, this is not a frequent case. */
18876 if (w->cursor.vpos < 0)
18878 /* Cursor in unchanged rows at the top? */
18879 if (PT < CHARPOS (start_pos)
18880 && last_unchanged_at_beg_row)
18882 row = row_containing_pos (w, PT,
18883 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18884 last_unchanged_at_beg_row + 1, 0);
18885 if (row)
18886 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18889 /* Start from first_unchanged_at_end_row looking for PT. */
18890 else if (first_unchanged_at_end_row)
18892 row = row_containing_pos (w, PT - delta,
18893 first_unchanged_at_end_row, NULL, 0);
18894 if (row)
18895 set_cursor_from_row (w, row, w->current_matrix, delta,
18896 delta_bytes, dy, dvpos);
18899 /* Give up if cursor was not found. */
18900 if (w->cursor.vpos < 0)
18902 clear_glyph_matrix (w->desired_matrix);
18903 return -1;
18907 /* Don't let the cursor end in the scroll margins. */
18909 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18910 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18912 if ((w->cursor.y < this_scroll_margin
18913 && CHARPOS (start) > BEGV)
18914 /* Old redisplay didn't take scroll margin into account at the bottom,
18915 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18916 || (w->cursor.y + (make_cursor_line_fully_visible_p
18917 ? cursor_height + this_scroll_margin
18918 : 1)) > it.last_visible_y)
18920 w->cursor.vpos = -1;
18921 clear_glyph_matrix (w->desired_matrix);
18922 return -1;
18926 /* Scroll the display. Do it before changing the current matrix so
18927 that xterm.c doesn't get confused about where the cursor glyph is
18928 found. */
18929 if (dy && run.height)
18931 update_begin (f);
18933 if (FRAME_WINDOW_P (f))
18935 FRAME_RIF (f)->update_window_begin_hook (w);
18936 FRAME_RIF (f)->clear_window_mouse_face (w);
18937 FRAME_RIF (f)->scroll_run_hook (w, &run);
18938 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18940 else
18942 /* Terminal frame. In this case, dvpos gives the number of
18943 lines to scroll by; dvpos < 0 means scroll up. */
18944 int from_vpos
18945 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18946 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18947 int end = (WINDOW_TOP_EDGE_LINE (w)
18948 + window_wants_header_line (w)
18949 + window_internal_height (w));
18951 #if defined (HAVE_GPM) || defined (MSDOS)
18952 x_clear_window_mouse_face (w);
18953 #endif
18954 /* Perform the operation on the screen. */
18955 if (dvpos > 0)
18957 /* Scroll last_unchanged_at_beg_row to the end of the
18958 window down dvpos lines. */
18959 set_terminal_window (f, end);
18961 /* On dumb terminals delete dvpos lines at the end
18962 before inserting dvpos empty lines. */
18963 if (!FRAME_SCROLL_REGION_OK (f))
18964 ins_del_lines (f, end - dvpos, -dvpos);
18966 /* Insert dvpos empty lines in front of
18967 last_unchanged_at_beg_row. */
18968 ins_del_lines (f, from, dvpos);
18970 else if (dvpos < 0)
18972 /* Scroll up last_unchanged_at_beg_vpos to the end of
18973 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18974 set_terminal_window (f, end);
18976 /* Delete dvpos lines in front of
18977 last_unchanged_at_beg_vpos. ins_del_lines will set
18978 the cursor to the given vpos and emit |dvpos| delete
18979 line sequences. */
18980 ins_del_lines (f, from + dvpos, dvpos);
18982 /* On a dumb terminal insert dvpos empty lines at the
18983 end. */
18984 if (!FRAME_SCROLL_REGION_OK (f))
18985 ins_del_lines (f, end + dvpos, -dvpos);
18988 set_terminal_window (f, 0);
18991 update_end (f);
18994 /* Shift reused rows of the current matrix to the right position.
18995 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18996 text. */
18997 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18998 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18999 if (dvpos < 0)
19001 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19002 bottom_vpos, dvpos);
19003 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19004 bottom_vpos);
19006 else if (dvpos > 0)
19008 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19009 bottom_vpos, dvpos);
19010 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19011 first_unchanged_at_end_vpos + dvpos);
19014 /* For frame-based redisplay, make sure that current frame and window
19015 matrix are in sync with respect to glyph memory. */
19016 if (!FRAME_WINDOW_P (f))
19017 sync_frame_with_window_matrix_rows (w);
19019 /* Adjust buffer positions in reused rows. */
19020 if (delta || delta_bytes)
19021 increment_matrix_positions (current_matrix,
19022 first_unchanged_at_end_vpos + dvpos,
19023 bottom_vpos, delta, delta_bytes);
19025 /* Adjust Y positions. */
19026 if (dy)
19027 shift_glyph_matrix (w, current_matrix,
19028 first_unchanged_at_end_vpos + dvpos,
19029 bottom_vpos, dy);
19031 if (first_unchanged_at_end_row)
19033 first_unchanged_at_end_row += dvpos;
19034 if (first_unchanged_at_end_row->y >= it.last_visible_y
19035 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19036 first_unchanged_at_end_row = NULL;
19039 /* If scrolling up, there may be some lines to display at the end of
19040 the window. */
19041 last_text_row_at_end = NULL;
19042 if (dy < 0)
19044 /* Scrolling up can leave for example a partially visible line
19045 at the end of the window to be redisplayed. */
19046 /* Set last_row to the glyph row in the current matrix where the
19047 window end line is found. It has been moved up or down in
19048 the matrix by dvpos. */
19049 int last_vpos = w->window_end_vpos + dvpos;
19050 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19052 /* If last_row is the window end line, it should display text. */
19053 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19055 /* If window end line was partially visible before, begin
19056 displaying at that line. Otherwise begin displaying with the
19057 line following it. */
19058 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19060 init_to_row_start (&it, w, last_row);
19061 it.vpos = last_vpos;
19062 it.current_y = last_row->y;
19064 else
19066 init_to_row_end (&it, w, last_row);
19067 it.vpos = 1 + last_vpos;
19068 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19069 ++last_row;
19072 /* We may start in a continuation line. If so, we have to
19073 get the right continuation_lines_width and current_x. */
19074 it.continuation_lines_width = last_row->continuation_lines_width;
19075 it.hpos = it.current_x = 0;
19077 /* Display the rest of the lines at the window end. */
19078 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19079 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19081 /* Is it always sure that the display agrees with lines in
19082 the current matrix? I don't think so, so we mark rows
19083 displayed invalid in the current matrix by setting their
19084 enabled_p flag to false. */
19085 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19086 if (display_line (&it, w->cursor.vpos))
19087 last_text_row_at_end = it.glyph_row - 1;
19091 /* Update window_end_pos and window_end_vpos. */
19092 if (first_unchanged_at_end_row && !last_text_row_at_end)
19094 /* Window end line if one of the preserved rows from the current
19095 matrix. Set row to the last row displaying text in current
19096 matrix starting at first_unchanged_at_end_row, after
19097 scrolling. */
19098 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19099 row = find_last_row_displaying_text (w->current_matrix, &it,
19100 first_unchanged_at_end_row);
19101 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19102 adjust_window_ends (w, row, true);
19103 eassert (w->window_end_bytepos >= 0);
19104 IF_DEBUG (debug_method_add (w, "A"));
19106 else if (last_text_row_at_end)
19108 adjust_window_ends (w, last_text_row_at_end, false);
19109 eassert (w->window_end_bytepos >= 0);
19110 IF_DEBUG (debug_method_add (w, "B"));
19112 else if (last_text_row)
19114 /* We have displayed either to the end of the window or at the
19115 end of the window, i.e. the last row with text is to be found
19116 in the desired matrix. */
19117 adjust_window_ends (w, last_text_row, false);
19118 eassert (w->window_end_bytepos >= 0);
19120 else if (first_unchanged_at_end_row == NULL
19121 && last_text_row == NULL
19122 && last_text_row_at_end == NULL)
19124 /* Displayed to end of window, but no line containing text was
19125 displayed. Lines were deleted at the end of the window. */
19126 bool first_vpos = window_wants_header_line (w);
19127 int vpos = w->window_end_vpos;
19128 struct glyph_row *current_row = current_matrix->rows + vpos;
19129 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19131 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19133 eassert (first_vpos <= vpos);
19134 if (desired_row->enabled_p)
19136 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19137 row = desired_row;
19139 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19140 row = current_row;
19143 w->window_end_vpos = vpos + 1;
19144 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19145 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19146 eassert (w->window_end_bytepos >= 0);
19147 IF_DEBUG (debug_method_add (w, "C"));
19149 else
19150 emacs_abort ();
19152 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19153 debug_end_vpos = w->window_end_vpos));
19155 /* Record that display has not been completed. */
19156 w->window_end_valid = false;
19157 w->desired_matrix->no_scrolling_p = true;
19158 return 3;
19160 #undef GIVE_UP
19165 /***********************************************************************
19166 More debugging support
19167 ***********************************************************************/
19169 #ifdef GLYPH_DEBUG
19171 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19172 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19173 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19176 /* Dump the contents of glyph matrix MATRIX on stderr.
19178 GLYPHS 0 means don't show glyph contents.
19179 GLYPHS 1 means show glyphs in short form
19180 GLYPHS > 1 means show glyphs in long form. */
19182 void
19183 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19185 int i;
19186 for (i = 0; i < matrix->nrows; ++i)
19187 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19191 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19192 the glyph row and area where the glyph comes from. */
19194 void
19195 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19197 if (glyph->type == CHAR_GLYPH
19198 || glyph->type == GLYPHLESS_GLYPH)
19200 fprintf (stderr,
19201 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19202 glyph - row->glyphs[TEXT_AREA],
19203 (glyph->type == CHAR_GLYPH
19204 ? 'C'
19205 : 'G'),
19206 glyph->charpos,
19207 (BUFFERP (glyph->object)
19208 ? 'B'
19209 : (STRINGP (glyph->object)
19210 ? 'S'
19211 : (NILP (glyph->object)
19212 ? '0'
19213 : '-'))),
19214 glyph->pixel_width,
19215 glyph->u.ch,
19216 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19217 ? (int) glyph->u.ch
19218 : '.'),
19219 glyph->face_id,
19220 glyph->left_box_line_p,
19221 glyph->right_box_line_p);
19223 else if (glyph->type == STRETCH_GLYPH)
19225 fprintf (stderr,
19226 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19227 glyph - row->glyphs[TEXT_AREA],
19228 'S',
19229 glyph->charpos,
19230 (BUFFERP (glyph->object)
19231 ? 'B'
19232 : (STRINGP (glyph->object)
19233 ? 'S'
19234 : (NILP (glyph->object)
19235 ? '0'
19236 : '-'))),
19237 glyph->pixel_width,
19239 ' ',
19240 glyph->face_id,
19241 glyph->left_box_line_p,
19242 glyph->right_box_line_p);
19244 else if (glyph->type == IMAGE_GLYPH)
19246 fprintf (stderr,
19247 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19248 glyph - row->glyphs[TEXT_AREA],
19249 'I',
19250 glyph->charpos,
19251 (BUFFERP (glyph->object)
19252 ? 'B'
19253 : (STRINGP (glyph->object)
19254 ? 'S'
19255 : (NILP (glyph->object)
19256 ? '0'
19257 : '-'))),
19258 glyph->pixel_width,
19259 (unsigned int) glyph->u.img_id,
19260 '.',
19261 glyph->face_id,
19262 glyph->left_box_line_p,
19263 glyph->right_box_line_p);
19265 else if (glyph->type == COMPOSITE_GLYPH)
19267 fprintf (stderr,
19268 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19269 glyph - row->glyphs[TEXT_AREA],
19270 '+',
19271 glyph->charpos,
19272 (BUFFERP (glyph->object)
19273 ? 'B'
19274 : (STRINGP (glyph->object)
19275 ? 'S'
19276 : (NILP (glyph->object)
19277 ? '0'
19278 : '-'))),
19279 glyph->pixel_width,
19280 (unsigned int) glyph->u.cmp.id);
19281 if (glyph->u.cmp.automatic)
19282 fprintf (stderr,
19283 "[%d-%d]",
19284 glyph->slice.cmp.from, glyph->slice.cmp.to);
19285 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19286 glyph->face_id,
19287 glyph->left_box_line_p,
19288 glyph->right_box_line_p);
19290 else if (glyph->type == XWIDGET_GLYPH)
19292 #ifndef HAVE_XWIDGETS
19293 eassume (false);
19294 #else
19295 fprintf (stderr,
19296 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19297 glyph - row->glyphs[TEXT_AREA],
19298 'X',
19299 glyph->charpos,
19300 (BUFFERP (glyph->object)
19301 ? 'B'
19302 : (STRINGP (glyph->object)
19303 ? 'S'
19304 : '-')),
19305 glyph->pixel_width,
19306 glyph->u.xwidget,
19307 '.',
19308 glyph->face_id,
19309 glyph->left_box_line_p,
19310 glyph->right_box_line_p);
19311 #endif
19316 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19317 GLYPHS 0 means don't show glyph contents.
19318 GLYPHS 1 means show glyphs in short form
19319 GLYPHS > 1 means show glyphs in long form. */
19321 void
19322 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19324 if (glyphs != 1)
19326 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19327 fprintf (stderr, "==============================================================================\n");
19329 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19330 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19331 vpos,
19332 MATRIX_ROW_START_CHARPOS (row),
19333 MATRIX_ROW_END_CHARPOS (row),
19334 row->used[TEXT_AREA],
19335 row->contains_overlapping_glyphs_p,
19336 row->enabled_p,
19337 row->truncated_on_left_p,
19338 row->truncated_on_right_p,
19339 row->continued_p,
19340 MATRIX_ROW_CONTINUATION_LINE_P (row),
19341 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19342 row->ends_at_zv_p,
19343 row->fill_line_p,
19344 row->ends_in_middle_of_char_p,
19345 row->starts_in_middle_of_char_p,
19346 row->mouse_face_p,
19347 row->x,
19348 row->y,
19349 row->pixel_width,
19350 row->height,
19351 row->visible_height,
19352 row->ascent,
19353 row->phys_ascent);
19354 /* The next 3 lines should align to "Start" in the header. */
19355 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19356 row->end.overlay_string_index,
19357 row->continuation_lines_width);
19358 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19359 CHARPOS (row->start.string_pos),
19360 CHARPOS (row->end.string_pos));
19361 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19362 row->end.dpvec_index);
19365 if (glyphs > 1)
19367 int area;
19369 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19371 struct glyph *glyph = row->glyphs[area];
19372 struct glyph *glyph_end = glyph + row->used[area];
19374 /* Glyph for a line end in text. */
19375 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19376 ++glyph_end;
19378 if (glyph < glyph_end)
19379 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19381 for (; glyph < glyph_end; ++glyph)
19382 dump_glyph (row, glyph, area);
19385 else if (glyphs == 1)
19387 int area;
19388 char s[SHRT_MAX + 4];
19390 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19392 int i;
19394 for (i = 0; i < row->used[area]; ++i)
19396 struct glyph *glyph = row->glyphs[area] + i;
19397 if (i == row->used[area] - 1
19398 && area == TEXT_AREA
19399 && NILP (glyph->object)
19400 && glyph->type == CHAR_GLYPH
19401 && glyph->u.ch == ' ')
19403 strcpy (&s[i], "[\\n]");
19404 i += 4;
19406 else if (glyph->type == CHAR_GLYPH
19407 && glyph->u.ch < 0x80
19408 && glyph->u.ch >= ' ')
19409 s[i] = glyph->u.ch;
19410 else
19411 s[i] = '.';
19414 s[i] = '\0';
19415 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19421 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19422 Sdump_glyph_matrix, 0, 1, "p",
19423 doc: /* Dump the current matrix of the selected window to stderr.
19424 Shows contents of glyph row structures. With non-nil
19425 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19426 glyphs in short form, otherwise show glyphs in long form.
19428 Interactively, no argument means show glyphs in short form;
19429 with numeric argument, its value is passed as the GLYPHS flag. */)
19430 (Lisp_Object glyphs)
19432 struct window *w = XWINDOW (selected_window);
19433 struct buffer *buffer = XBUFFER (w->contents);
19435 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19436 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19437 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19438 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19439 fprintf (stderr, "=============================================\n");
19440 dump_glyph_matrix (w->current_matrix,
19441 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19442 return Qnil;
19446 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19447 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19448 Only text-mode frames have frame glyph matrices. */)
19449 (void)
19451 struct frame *f = XFRAME (selected_frame);
19453 if (f->current_matrix)
19454 dump_glyph_matrix (f->current_matrix, 1);
19455 else
19456 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19457 return Qnil;
19461 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19462 doc: /* Dump glyph row ROW to stderr.
19463 GLYPH 0 means don't dump glyphs.
19464 GLYPH 1 means dump glyphs in short form.
19465 GLYPH > 1 or omitted means dump glyphs in long form. */)
19466 (Lisp_Object row, Lisp_Object glyphs)
19468 struct glyph_matrix *matrix;
19469 EMACS_INT vpos;
19471 CHECK_NUMBER (row);
19472 matrix = XWINDOW (selected_window)->current_matrix;
19473 vpos = XINT (row);
19474 if (vpos >= 0 && vpos < matrix->nrows)
19475 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19476 vpos,
19477 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19478 return Qnil;
19482 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19483 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19484 GLYPH 0 means don't dump glyphs.
19485 GLYPH 1 means dump glyphs in short form.
19486 GLYPH > 1 or omitted means dump glyphs in long form.
19488 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19489 do nothing. */)
19490 (Lisp_Object row, Lisp_Object glyphs)
19492 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19493 struct frame *sf = SELECTED_FRAME ();
19494 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19495 EMACS_INT vpos;
19497 CHECK_NUMBER (row);
19498 vpos = XINT (row);
19499 if (vpos >= 0 && vpos < m->nrows)
19500 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19501 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19502 #endif
19503 return Qnil;
19507 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19508 doc: /* Toggle tracing of redisplay.
19509 With ARG, turn tracing on if and only if ARG is positive. */)
19510 (Lisp_Object arg)
19512 if (NILP (arg))
19513 trace_redisplay_p = !trace_redisplay_p;
19514 else
19516 arg = Fprefix_numeric_value (arg);
19517 trace_redisplay_p = XINT (arg) > 0;
19520 return Qnil;
19524 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19525 doc: /* Like `format', but print result to stderr.
19526 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19527 (ptrdiff_t nargs, Lisp_Object *args)
19529 Lisp_Object s = Fformat (nargs, args);
19530 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19531 return Qnil;
19534 #endif /* GLYPH_DEBUG */
19538 /***********************************************************************
19539 Building Desired Matrix Rows
19540 ***********************************************************************/
19542 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19543 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19545 static struct glyph_row *
19546 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19548 struct frame *f = XFRAME (WINDOW_FRAME (w));
19549 struct buffer *buffer = XBUFFER (w->contents);
19550 struct buffer *old = current_buffer;
19551 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19552 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19553 const unsigned char *arrow_end = arrow_string + arrow_len;
19554 const unsigned char *p;
19555 struct it it;
19556 bool multibyte_p;
19557 int n_glyphs_before;
19559 set_buffer_temp (buffer);
19560 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19561 scratch_glyph_row.reversed_p = false;
19562 it.glyph_row->used[TEXT_AREA] = 0;
19563 SET_TEXT_POS (it.position, 0, 0);
19565 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19566 p = arrow_string;
19567 while (p < arrow_end)
19569 Lisp_Object face, ilisp;
19571 /* Get the next character. */
19572 if (multibyte_p)
19573 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19574 else
19576 it.c = it.char_to_display = *p, it.len = 1;
19577 if (! ASCII_CHAR_P (it.c))
19578 it.char_to_display = BYTE8_TO_CHAR (it.c);
19580 p += it.len;
19582 /* Get its face. */
19583 ilisp = make_number (p - arrow_string);
19584 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19585 it.face_id = compute_char_face (f, it.char_to_display, face);
19587 /* Compute its width, get its glyphs. */
19588 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19589 SET_TEXT_POS (it.position, -1, -1);
19590 PRODUCE_GLYPHS (&it);
19592 /* If this character doesn't fit any more in the line, we have
19593 to remove some glyphs. */
19594 if (it.current_x > it.last_visible_x)
19596 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19597 break;
19601 set_buffer_temp (old);
19602 return it.glyph_row;
19606 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19607 glyphs to insert is determined by produce_special_glyphs. */
19609 static void
19610 insert_left_trunc_glyphs (struct it *it)
19612 struct it truncate_it;
19613 struct glyph *from, *end, *to, *toend;
19615 eassert (!FRAME_WINDOW_P (it->f)
19616 || (!it->glyph_row->reversed_p
19617 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19618 || (it->glyph_row->reversed_p
19619 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19621 /* Get the truncation glyphs. */
19622 truncate_it = *it;
19623 truncate_it.current_x = 0;
19624 truncate_it.face_id = DEFAULT_FACE_ID;
19625 truncate_it.glyph_row = &scratch_glyph_row;
19626 truncate_it.area = TEXT_AREA;
19627 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19628 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19629 truncate_it.object = Qnil;
19630 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19632 /* Overwrite glyphs from IT with truncation glyphs. */
19633 if (!it->glyph_row->reversed_p)
19635 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19637 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19638 end = from + tused;
19639 to = it->glyph_row->glyphs[TEXT_AREA];
19640 toend = to + it->glyph_row->used[TEXT_AREA];
19641 if (FRAME_WINDOW_P (it->f))
19643 /* On GUI frames, when variable-size fonts are displayed,
19644 the truncation glyphs may need more pixels than the row's
19645 glyphs they overwrite. We overwrite more glyphs to free
19646 enough screen real estate, and enlarge the stretch glyph
19647 on the right (see display_line), if there is one, to
19648 preserve the screen position of the truncation glyphs on
19649 the right. */
19650 int w = 0;
19651 struct glyph *g = to;
19652 short used;
19654 /* The first glyph could be partially visible, in which case
19655 it->glyph_row->x will be negative. But we want the left
19656 truncation glyphs to be aligned at the left margin of the
19657 window, so we override the x coordinate at which the row
19658 will begin. */
19659 it->glyph_row->x = 0;
19660 while (g < toend && w < it->truncation_pixel_width)
19662 w += g->pixel_width;
19663 ++g;
19665 if (g - to - tused > 0)
19667 memmove (to + tused, g, (toend - g) * sizeof(*g));
19668 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19670 used = it->glyph_row->used[TEXT_AREA];
19671 if (it->glyph_row->truncated_on_right_p
19672 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19673 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19674 == STRETCH_GLYPH)
19676 int extra = w - it->truncation_pixel_width;
19678 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19682 while (from < end)
19683 *to++ = *from++;
19685 /* There may be padding glyphs left over. Overwrite them too. */
19686 if (!FRAME_WINDOW_P (it->f))
19688 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19690 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19691 while (from < end)
19692 *to++ = *from++;
19696 if (to > toend)
19697 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19699 else
19701 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19703 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19704 that back to front. */
19705 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19706 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19707 toend = it->glyph_row->glyphs[TEXT_AREA];
19708 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19709 if (FRAME_WINDOW_P (it->f))
19711 int w = 0;
19712 struct glyph *g = to;
19714 while (g >= toend && w < it->truncation_pixel_width)
19716 w += g->pixel_width;
19717 --g;
19719 if (to - g - tused > 0)
19720 to = g + tused;
19721 if (it->glyph_row->truncated_on_right_p
19722 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19723 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19725 int extra = w - it->truncation_pixel_width;
19727 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19731 while (from >= end && to >= toend)
19732 *to-- = *from--;
19733 if (!FRAME_WINDOW_P (it->f))
19735 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19737 from =
19738 truncate_it.glyph_row->glyphs[TEXT_AREA]
19739 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19740 while (from >= end && to >= toend)
19741 *to-- = *from--;
19744 if (from >= end)
19746 /* Need to free some room before prepending additional
19747 glyphs. */
19748 int move_by = from - end + 1;
19749 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19750 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19752 for ( ; g >= g0; g--)
19753 g[move_by] = *g;
19754 while (from >= end)
19755 *to-- = *from--;
19756 it->glyph_row->used[TEXT_AREA] += move_by;
19761 /* Compute the hash code for ROW. */
19762 unsigned
19763 row_hash (struct glyph_row *row)
19765 int area, k;
19766 unsigned hashval = 0;
19768 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19769 for (k = 0; k < row->used[area]; ++k)
19770 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19771 + row->glyphs[area][k].u.val
19772 + row->glyphs[area][k].face_id
19773 + row->glyphs[area][k].padding_p
19774 + (row->glyphs[area][k].type << 2));
19776 return hashval;
19779 /* Compute the pixel height and width of IT->glyph_row.
19781 Most of the time, ascent and height of a display line will be equal
19782 to the max_ascent and max_height values of the display iterator
19783 structure. This is not the case if
19785 1. We hit ZV without displaying anything. In this case, max_ascent
19786 and max_height will be zero.
19788 2. We have some glyphs that don't contribute to the line height.
19789 (The glyph row flag contributes_to_line_height_p is for future
19790 pixmap extensions).
19792 The first case is easily covered by using default values because in
19793 these cases, the line height does not really matter, except that it
19794 must not be zero. */
19796 static void
19797 compute_line_metrics (struct it *it)
19799 struct glyph_row *row = it->glyph_row;
19801 if (FRAME_WINDOW_P (it->f))
19803 int i, min_y, max_y;
19805 /* The line may consist of one space only, that was added to
19806 place the cursor on it. If so, the row's height hasn't been
19807 computed yet. */
19808 if (row->height == 0)
19810 if (it->max_ascent + it->max_descent == 0)
19811 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19812 row->ascent = it->max_ascent;
19813 row->height = it->max_ascent + it->max_descent;
19814 row->phys_ascent = it->max_phys_ascent;
19815 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19816 row->extra_line_spacing = it->max_extra_line_spacing;
19819 /* Compute the width of this line. */
19820 row->pixel_width = row->x;
19821 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19822 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19824 eassert (row->pixel_width >= 0);
19825 eassert (row->ascent >= 0 && row->height > 0);
19827 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19828 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19830 /* If first line's physical ascent is larger than its logical
19831 ascent, use the physical ascent, and make the row taller.
19832 This makes accented characters fully visible. */
19833 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19834 && row->phys_ascent > row->ascent)
19836 row->height += row->phys_ascent - row->ascent;
19837 row->ascent = row->phys_ascent;
19840 /* Compute how much of the line is visible. */
19841 row->visible_height = row->height;
19843 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19844 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19846 if (row->y < min_y)
19847 row->visible_height -= min_y - row->y;
19848 if (row->y + row->height > max_y)
19849 row->visible_height -= row->y + row->height - max_y;
19851 else
19853 row->pixel_width = row->used[TEXT_AREA];
19854 if (row->continued_p)
19855 row->pixel_width -= it->continuation_pixel_width;
19856 else if (row->truncated_on_right_p)
19857 row->pixel_width -= it->truncation_pixel_width;
19858 row->ascent = row->phys_ascent = 0;
19859 row->height = row->phys_height = row->visible_height = 1;
19860 row->extra_line_spacing = 0;
19863 /* Compute a hash code for this row. */
19864 row->hash = row_hash (row);
19866 it->max_ascent = it->max_descent = 0;
19867 it->max_phys_ascent = it->max_phys_descent = 0;
19871 /* Append one space to the glyph row of iterator IT if doing a
19872 window-based redisplay. The space has the same face as
19873 IT->face_id. Value is true if a space was added.
19875 This function is called to make sure that there is always one glyph
19876 at the end of a glyph row that the cursor can be set on under
19877 window-systems. (If there weren't such a glyph we would not know
19878 how wide and tall a box cursor should be displayed).
19880 At the same time this space let's a nicely handle clearing to the
19881 end of the line if the row ends in italic text. */
19883 static bool
19884 append_space_for_newline (struct it *it, bool default_face_p)
19886 if (FRAME_WINDOW_P (it->f))
19888 int n = it->glyph_row->used[TEXT_AREA];
19890 if (it->glyph_row->glyphs[TEXT_AREA] + n
19891 < it->glyph_row->glyphs[1 + TEXT_AREA])
19893 /* Save some values that must not be changed.
19894 Must save IT->c and IT->len because otherwise
19895 ITERATOR_AT_END_P wouldn't work anymore after
19896 append_space_for_newline has been called. */
19897 enum display_element_type saved_what = it->what;
19898 int saved_c = it->c, saved_len = it->len;
19899 int saved_char_to_display = it->char_to_display;
19900 int saved_x = it->current_x;
19901 int saved_face_id = it->face_id;
19902 bool saved_box_end = it->end_of_box_run_p;
19903 struct text_pos saved_pos;
19904 Lisp_Object saved_object;
19905 struct face *face;
19907 saved_object = it->object;
19908 saved_pos = it->position;
19910 it->what = IT_CHARACTER;
19911 memset (&it->position, 0, sizeof it->position);
19912 it->object = Qnil;
19913 it->c = it->char_to_display = ' ';
19914 it->len = 1;
19916 /* If the default face was remapped, be sure to use the
19917 remapped face for the appended newline. */
19918 if (default_face_p)
19919 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19920 else if (it->face_before_selective_p)
19921 it->face_id = it->saved_face_id;
19922 face = FACE_FROM_ID (it->f, it->face_id);
19923 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19924 /* In R2L rows, we will prepend a stretch glyph that will
19925 have the end_of_box_run_p flag set for it, so there's no
19926 need for the appended newline glyph to have that flag
19927 set. */
19928 if (it->glyph_row->reversed_p
19929 /* But if the appended newline glyph goes all the way to
19930 the end of the row, there will be no stretch glyph,
19931 so leave the box flag set. */
19932 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19933 it->end_of_box_run_p = false;
19935 PRODUCE_GLYPHS (it);
19937 #ifdef HAVE_WINDOW_SYSTEM
19938 /* Make sure this space glyph has the right ascent and
19939 descent values, or else cursor at end of line will look
19940 funny, and height of empty lines will be incorrect. */
19941 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19942 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19943 if (n == 0)
19945 Lisp_Object height, total_height;
19946 int extra_line_spacing = it->extra_line_spacing;
19947 int boff = font->baseline_offset;
19949 if (font->vertical_centering)
19950 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19952 it->object = saved_object; /* get_it_property needs this */
19953 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19954 /* Must do a subset of line height processing from
19955 x_produce_glyph for newline characters. */
19956 height = get_it_property (it, Qline_height);
19957 if (CONSP (height)
19958 && CONSP (XCDR (height))
19959 && NILP (XCDR (XCDR (height))))
19961 total_height = XCAR (XCDR (height));
19962 height = XCAR (height);
19964 else
19965 total_height = Qnil;
19966 height = calc_line_height_property (it, height, font, boff, true);
19968 if (it->override_ascent >= 0)
19970 it->ascent = it->override_ascent;
19971 it->descent = it->override_descent;
19972 boff = it->override_boff;
19974 if (EQ (height, Qt))
19975 extra_line_spacing = 0;
19976 else
19978 Lisp_Object spacing;
19980 it->phys_ascent = it->ascent;
19981 it->phys_descent = it->descent;
19982 if (!NILP (height)
19983 && XINT (height) > it->ascent + it->descent)
19984 it->ascent = XINT (height) - it->descent;
19986 if (!NILP (total_height))
19987 spacing = calc_line_height_property (it, total_height, font,
19988 boff, false);
19989 else
19991 spacing = get_it_property (it, Qline_spacing);
19992 spacing = calc_line_height_property (it, spacing, font,
19993 boff, false);
19995 if (INTEGERP (spacing))
19997 extra_line_spacing = XINT (spacing);
19998 if (!NILP (total_height))
19999 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20002 if (extra_line_spacing > 0)
20004 it->descent += extra_line_spacing;
20005 if (extra_line_spacing > it->max_extra_line_spacing)
20006 it->max_extra_line_spacing = extra_line_spacing;
20008 it->max_ascent = it->ascent;
20009 it->max_descent = it->descent;
20010 /* Make sure compute_line_metrics recomputes the row height. */
20011 it->glyph_row->height = 0;
20014 g->ascent = it->max_ascent;
20015 g->descent = it->max_descent;
20016 #endif
20018 it->override_ascent = -1;
20019 it->constrain_row_ascent_descent_p = false;
20020 it->current_x = saved_x;
20021 it->object = saved_object;
20022 it->position = saved_pos;
20023 it->what = saved_what;
20024 it->face_id = saved_face_id;
20025 it->len = saved_len;
20026 it->c = saved_c;
20027 it->char_to_display = saved_char_to_display;
20028 it->end_of_box_run_p = saved_box_end;
20029 return true;
20033 return false;
20037 /* Extend the face of the last glyph in the text area of IT->glyph_row
20038 to the end of the display line. Called from display_line. If the
20039 glyph row is empty, add a space glyph to it so that we know the
20040 face to draw. Set the glyph row flag fill_line_p. If the glyph
20041 row is R2L, prepend a stretch glyph to cover the empty space to the
20042 left of the leftmost glyph. */
20044 static void
20045 extend_face_to_end_of_line (struct it *it)
20047 struct face *face, *default_face;
20048 struct frame *f = it->f;
20050 /* If line is already filled, do nothing. Non window-system frames
20051 get a grace of one more ``pixel'' because their characters are
20052 1-``pixel'' wide, so they hit the equality too early. This grace
20053 is needed only for R2L rows that are not continued, to produce
20054 one extra blank where we could display the cursor. */
20055 if ((it->current_x >= it->last_visible_x
20056 + (!FRAME_WINDOW_P (f)
20057 && it->glyph_row->reversed_p
20058 && !it->glyph_row->continued_p))
20059 /* If the window has display margins, we will need to extend
20060 their face even if the text area is filled. */
20061 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20062 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20063 return;
20065 /* The default face, possibly remapped. */
20066 default_face = FACE_FROM_ID_OR_NULL (f,
20067 lookup_basic_face (f, DEFAULT_FACE_ID));
20069 /* Face extension extends the background and box of IT->face_id
20070 to the end of the line. If the background equals the background
20071 of the frame, we don't have to do anything. */
20072 face = FACE_FROM_ID (f, (it->face_before_selective_p
20073 ? it->saved_face_id
20074 : it->face_id));
20076 if (FRAME_WINDOW_P (f)
20077 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20078 && face->box == FACE_NO_BOX
20079 && face->background == FRAME_BACKGROUND_PIXEL (f)
20080 #ifdef HAVE_WINDOW_SYSTEM
20081 && !face->stipple
20082 #endif
20083 && !it->glyph_row->reversed_p)
20084 return;
20086 /* Set the glyph row flag indicating that the face of the last glyph
20087 in the text area has to be drawn to the end of the text area. */
20088 it->glyph_row->fill_line_p = true;
20090 /* If current character of IT is not ASCII, make sure we have the
20091 ASCII face. This will be automatically undone the next time
20092 get_next_display_element returns a multibyte character. Note
20093 that the character will always be single byte in unibyte
20094 text. */
20095 if (!ASCII_CHAR_P (it->c))
20097 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20100 if (FRAME_WINDOW_P (f))
20102 /* If the row is empty, add a space with the current face of IT,
20103 so that we know which face to draw. */
20104 if (it->glyph_row->used[TEXT_AREA] == 0)
20106 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20107 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20108 it->glyph_row->used[TEXT_AREA] = 1;
20110 /* Mode line and the header line don't have margins, and
20111 likewise the frame's tool-bar window, if there is any. */
20112 if (!(it->glyph_row->mode_line_p
20113 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20114 || (WINDOWP (f->tool_bar_window)
20115 && it->w == XWINDOW (f->tool_bar_window))
20116 #endif
20119 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20120 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20122 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20123 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20124 default_face->id;
20125 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20127 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20128 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20130 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20131 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20132 default_face->id;
20133 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20136 #ifdef HAVE_WINDOW_SYSTEM
20137 if (it->glyph_row->reversed_p)
20139 /* Prepend a stretch glyph to the row, such that the
20140 rightmost glyph will be drawn flushed all the way to the
20141 right margin of the window. The stretch glyph that will
20142 occupy the empty space, if any, to the left of the
20143 glyphs. */
20144 struct font *font = face->font ? face->font : FRAME_FONT (f);
20145 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20146 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20147 struct glyph *g;
20148 int row_width, stretch_ascent, stretch_width;
20149 struct text_pos saved_pos;
20150 int saved_face_id;
20151 bool saved_avoid_cursor, saved_box_start;
20153 for (row_width = 0, g = row_start; g < row_end; g++)
20154 row_width += g->pixel_width;
20156 /* FIXME: There are various minor display glitches in R2L
20157 rows when only one of the fringes is missing. The
20158 strange condition below produces the least bad effect. */
20159 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20160 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20161 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20162 stretch_width = window_box_width (it->w, TEXT_AREA);
20163 else
20164 stretch_width = it->last_visible_x - it->first_visible_x;
20165 stretch_width -= row_width;
20167 if (stretch_width > 0)
20169 stretch_ascent =
20170 (((it->ascent + it->descent)
20171 * FONT_BASE (font)) / FONT_HEIGHT (font));
20172 saved_pos = it->position;
20173 memset (&it->position, 0, sizeof it->position);
20174 saved_avoid_cursor = it->avoid_cursor_p;
20175 it->avoid_cursor_p = true;
20176 saved_face_id = it->face_id;
20177 saved_box_start = it->start_of_box_run_p;
20178 /* The last row's stretch glyph should get the default
20179 face, to avoid painting the rest of the window with
20180 the region face, if the region ends at ZV. */
20181 if (it->glyph_row->ends_at_zv_p)
20182 it->face_id = default_face->id;
20183 else
20184 it->face_id = face->id;
20185 it->start_of_box_run_p = false;
20186 append_stretch_glyph (it, Qnil, stretch_width,
20187 it->ascent + it->descent, stretch_ascent);
20188 it->position = saved_pos;
20189 it->avoid_cursor_p = saved_avoid_cursor;
20190 it->face_id = saved_face_id;
20191 it->start_of_box_run_p = saved_box_start;
20193 /* If stretch_width comes out negative, it means that the
20194 last glyph is only partially visible. In R2L rows, we
20195 want the leftmost glyph to be partially visible, so we
20196 need to give the row the corresponding left offset. */
20197 if (stretch_width < 0)
20198 it->glyph_row->x = stretch_width;
20200 #endif /* HAVE_WINDOW_SYSTEM */
20202 else
20204 /* Save some values that must not be changed. */
20205 int saved_x = it->current_x;
20206 struct text_pos saved_pos;
20207 Lisp_Object saved_object;
20208 enum display_element_type saved_what = it->what;
20209 int saved_face_id = it->face_id;
20211 saved_object = it->object;
20212 saved_pos = it->position;
20214 it->what = IT_CHARACTER;
20215 memset (&it->position, 0, sizeof it->position);
20216 it->object = Qnil;
20217 it->c = it->char_to_display = ' ';
20218 it->len = 1;
20220 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20221 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20222 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20223 && !it->glyph_row->mode_line_p
20224 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20226 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20227 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20229 for (it->current_x = 0; g < e; g++)
20230 it->current_x += g->pixel_width;
20232 it->area = LEFT_MARGIN_AREA;
20233 it->face_id = default_face->id;
20234 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20235 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20237 PRODUCE_GLYPHS (it);
20238 /* term.c:produce_glyphs advances it->current_x only for
20239 TEXT_AREA. */
20240 it->current_x += it->pixel_width;
20243 it->current_x = saved_x;
20244 it->area = TEXT_AREA;
20247 /* The last row's blank glyphs should get the default face, to
20248 avoid painting the rest of the window with the region face,
20249 if the region ends at ZV. */
20250 if (it->glyph_row->ends_at_zv_p)
20251 it->face_id = default_face->id;
20252 else
20253 it->face_id = face->id;
20254 PRODUCE_GLYPHS (it);
20256 while (it->current_x <= it->last_visible_x)
20257 PRODUCE_GLYPHS (it);
20259 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20260 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20261 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20262 && !it->glyph_row->mode_line_p
20263 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20265 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20266 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20268 for ( ; g < e; g++)
20269 it->current_x += g->pixel_width;
20271 it->area = RIGHT_MARGIN_AREA;
20272 it->face_id = default_face->id;
20273 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20274 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20276 PRODUCE_GLYPHS (it);
20277 it->current_x += it->pixel_width;
20280 it->area = TEXT_AREA;
20283 /* Don't count these blanks really. It would let us insert a left
20284 truncation glyph below and make us set the cursor on them, maybe. */
20285 it->current_x = saved_x;
20286 it->object = saved_object;
20287 it->position = saved_pos;
20288 it->what = saved_what;
20289 it->face_id = saved_face_id;
20294 /* Value is true if text starting at CHARPOS in current_buffer is
20295 trailing whitespace. */
20297 static bool
20298 trailing_whitespace_p (ptrdiff_t charpos)
20300 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20301 int c = 0;
20303 while (bytepos < ZV_BYTE
20304 && (c = FETCH_CHAR (bytepos),
20305 c == ' ' || c == '\t'))
20306 ++bytepos;
20308 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20310 if (bytepos != PT_BYTE)
20311 return true;
20313 return false;
20317 /* Highlight trailing whitespace, if any, in ROW. */
20319 static void
20320 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20322 int used = row->used[TEXT_AREA];
20324 if (used)
20326 struct glyph *start = row->glyphs[TEXT_AREA];
20327 struct glyph *glyph = start + used - 1;
20329 if (row->reversed_p)
20331 /* Right-to-left rows need to be processed in the opposite
20332 direction, so swap the edge pointers. */
20333 glyph = start;
20334 start = row->glyphs[TEXT_AREA] + used - 1;
20337 /* Skip over glyphs inserted to display the cursor at the
20338 end of a line, for extending the face of the last glyph
20339 to the end of the line on terminals, and for truncation
20340 and continuation glyphs. */
20341 if (!row->reversed_p)
20343 while (glyph >= start
20344 && glyph->type == CHAR_GLYPH
20345 && NILP (glyph->object))
20346 --glyph;
20348 else
20350 while (glyph <= start
20351 && glyph->type == CHAR_GLYPH
20352 && NILP (glyph->object))
20353 ++glyph;
20356 /* If last glyph is a space or stretch, and it's trailing
20357 whitespace, set the face of all trailing whitespace glyphs in
20358 IT->glyph_row to `trailing-whitespace'. */
20359 if ((row->reversed_p ? glyph <= start : glyph >= start)
20360 && BUFFERP (glyph->object)
20361 && (glyph->type == STRETCH_GLYPH
20362 || (glyph->type == CHAR_GLYPH
20363 && glyph->u.ch == ' '))
20364 && trailing_whitespace_p (glyph->charpos))
20366 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20367 if (face_id < 0)
20368 return;
20370 if (!row->reversed_p)
20372 while (glyph >= start
20373 && BUFFERP (glyph->object)
20374 && (glyph->type == STRETCH_GLYPH
20375 || (glyph->type == CHAR_GLYPH
20376 && glyph->u.ch == ' ')))
20377 (glyph--)->face_id = face_id;
20379 else
20381 while (glyph <= start
20382 && BUFFERP (glyph->object)
20383 && (glyph->type == STRETCH_GLYPH
20384 || (glyph->type == CHAR_GLYPH
20385 && glyph->u.ch == ' ')))
20386 (glyph++)->face_id = face_id;
20393 /* Value is true if glyph row ROW should be
20394 considered to hold the buffer position CHARPOS. */
20396 static bool
20397 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20399 bool result = true;
20401 if (charpos == CHARPOS (row->end.pos)
20402 || charpos == MATRIX_ROW_END_CHARPOS (row))
20404 /* Suppose the row ends on a string.
20405 Unless the row is continued, that means it ends on a newline
20406 in the string. If it's anything other than a display string
20407 (e.g., a before-string from an overlay), we don't want the
20408 cursor there. (This heuristic seems to give the optimal
20409 behavior for the various types of multi-line strings.)
20410 One exception: if the string has `cursor' property on one of
20411 its characters, we _do_ want the cursor there. */
20412 if (CHARPOS (row->end.string_pos) >= 0)
20414 if (row->continued_p)
20415 result = true;
20416 else
20418 /* Check for `display' property. */
20419 struct glyph *beg = row->glyphs[TEXT_AREA];
20420 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20421 struct glyph *glyph;
20423 result = false;
20424 for (glyph = end; glyph >= beg; --glyph)
20425 if (STRINGP (glyph->object))
20427 Lisp_Object prop
20428 = Fget_char_property (make_number (charpos),
20429 Qdisplay, Qnil);
20430 result =
20431 (!NILP (prop)
20432 && display_prop_string_p (prop, glyph->object));
20433 /* If there's a `cursor' property on one of the
20434 string's characters, this row is a cursor row,
20435 even though this is not a display string. */
20436 if (!result)
20438 Lisp_Object s = glyph->object;
20440 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20442 ptrdiff_t gpos = glyph->charpos;
20444 if (!NILP (Fget_char_property (make_number (gpos),
20445 Qcursor, s)))
20447 result = true;
20448 break;
20452 break;
20456 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20458 /* If the row ends in middle of a real character,
20459 and the line is continued, we want the cursor here.
20460 That's because CHARPOS (ROW->end.pos) would equal
20461 PT if PT is before the character. */
20462 if (!row->ends_in_ellipsis_p)
20463 result = row->continued_p;
20464 else
20465 /* If the row ends in an ellipsis, then
20466 CHARPOS (ROW->end.pos) will equal point after the
20467 invisible text. We want that position to be displayed
20468 after the ellipsis. */
20469 result = false;
20471 /* If the row ends at ZV, display the cursor at the end of that
20472 row instead of at the start of the row below. */
20473 else
20474 result = row->ends_at_zv_p;
20477 return result;
20480 /* Value is true if glyph row ROW should be
20481 used to hold the cursor. */
20483 static bool
20484 cursor_row_p (struct glyph_row *row)
20486 return row_for_charpos_p (row, PT);
20491 /* Push the property PROP so that it will be rendered at the current
20492 position in IT. Return true if PROP was successfully pushed, false
20493 otherwise. Called from handle_line_prefix to handle the
20494 `line-prefix' and `wrap-prefix' properties. */
20496 static bool
20497 push_prefix_prop (struct it *it, Lisp_Object prop)
20499 struct text_pos pos =
20500 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20502 eassert (it->method == GET_FROM_BUFFER
20503 || it->method == GET_FROM_DISPLAY_VECTOR
20504 || it->method == GET_FROM_STRING
20505 || it->method == GET_FROM_IMAGE);
20507 /* We need to save the current buffer/string position, so it will be
20508 restored by pop_it, because iterate_out_of_display_property
20509 depends on that being set correctly, but some situations leave
20510 it->position not yet set when this function is called. */
20511 push_it (it, &pos);
20513 if (STRINGP (prop))
20515 if (SCHARS (prop) == 0)
20517 pop_it (it);
20518 return false;
20521 it->string = prop;
20522 it->string_from_prefix_prop_p = true;
20523 it->multibyte_p = STRING_MULTIBYTE (it->string);
20524 it->current.overlay_string_index = -1;
20525 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20526 it->end_charpos = it->string_nchars = SCHARS (it->string);
20527 it->method = GET_FROM_STRING;
20528 it->stop_charpos = 0;
20529 it->prev_stop = 0;
20530 it->base_level_stop = 0;
20531 it->cmp_it.id = -1;
20533 /* Force paragraph direction to be that of the parent
20534 buffer/string. */
20535 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20536 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20537 else
20538 it->paragraph_embedding = L2R;
20540 /* Set up the bidi iterator for this display string. */
20541 if (it->bidi_p)
20543 it->bidi_it.string.lstring = it->string;
20544 it->bidi_it.string.s = NULL;
20545 it->bidi_it.string.schars = it->end_charpos;
20546 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20547 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20548 it->bidi_it.string.unibyte = !it->multibyte_p;
20549 it->bidi_it.w = it->w;
20550 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20553 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20555 it->method = GET_FROM_STRETCH;
20556 it->object = prop;
20558 #ifdef HAVE_WINDOW_SYSTEM
20559 else if (IMAGEP (prop))
20561 it->what = IT_IMAGE;
20562 it->image_id = lookup_image (it->f, prop);
20563 it->method = GET_FROM_IMAGE;
20565 #endif /* HAVE_WINDOW_SYSTEM */
20566 else
20568 pop_it (it); /* bogus display property, give up */
20569 return false;
20572 return true;
20575 /* Return the character-property PROP at the current position in IT. */
20577 static Lisp_Object
20578 get_it_property (struct it *it, Lisp_Object prop)
20580 Lisp_Object position, object = it->object;
20582 if (STRINGP (object))
20583 position = make_number (IT_STRING_CHARPOS (*it));
20584 else if (BUFFERP (object))
20586 position = make_number (IT_CHARPOS (*it));
20587 object = it->window;
20589 else
20590 return Qnil;
20592 return Fget_char_property (position, prop, object);
20595 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20597 static void
20598 handle_line_prefix (struct it *it)
20600 Lisp_Object prefix;
20602 if (it->continuation_lines_width > 0)
20604 prefix = get_it_property (it, Qwrap_prefix);
20605 if (NILP (prefix))
20606 prefix = Vwrap_prefix;
20608 else
20610 prefix = get_it_property (it, Qline_prefix);
20611 if (NILP (prefix))
20612 prefix = Vline_prefix;
20614 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20616 /* If the prefix is wider than the window, and we try to wrap
20617 it, it would acquire its own wrap prefix, and so on till the
20618 iterator stack overflows. So, don't wrap the prefix. */
20619 it->line_wrap = TRUNCATE;
20620 it->avoid_cursor_p = true;
20626 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20627 only for R2L lines from display_line and display_string, when they
20628 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20629 the line/string needs to be continued on the next glyph row. */
20630 static void
20631 unproduce_glyphs (struct it *it, int n)
20633 struct glyph *glyph, *end;
20635 eassert (it->glyph_row);
20636 eassert (it->glyph_row->reversed_p);
20637 eassert (it->area == TEXT_AREA);
20638 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20640 if (n > it->glyph_row->used[TEXT_AREA])
20641 n = it->glyph_row->used[TEXT_AREA];
20642 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20643 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20644 for ( ; glyph < end; glyph++)
20645 glyph[-n] = *glyph;
20648 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20649 and ROW->maxpos. */
20650 static void
20651 find_row_edges (struct it *it, struct glyph_row *row,
20652 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20653 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20655 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20656 lines' rows is implemented for bidi-reordered rows. */
20658 /* ROW->minpos is the value of min_pos, the minimal buffer position
20659 we have in ROW, or ROW->start.pos if that is smaller. */
20660 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20661 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20662 else
20663 /* We didn't find buffer positions smaller than ROW->start, or
20664 didn't find _any_ valid buffer positions in any of the glyphs,
20665 so we must trust the iterator's computed positions. */
20666 row->minpos = row->start.pos;
20667 if (max_pos <= 0)
20669 max_pos = CHARPOS (it->current.pos);
20670 max_bpos = BYTEPOS (it->current.pos);
20673 /* Here are the various use-cases for ending the row, and the
20674 corresponding values for ROW->maxpos:
20676 Line ends in a newline from buffer eol_pos + 1
20677 Line is continued from buffer max_pos + 1
20678 Line is truncated on right it->current.pos
20679 Line ends in a newline from string max_pos + 1(*)
20680 (*) + 1 only when line ends in a forward scan
20681 Line is continued from string max_pos
20682 Line is continued from display vector max_pos
20683 Line is entirely from a string min_pos == max_pos
20684 Line is entirely from a display vector min_pos == max_pos
20685 Line that ends at ZV ZV
20687 If you discover other use-cases, please add them here as
20688 appropriate. */
20689 if (row->ends_at_zv_p)
20690 row->maxpos = it->current.pos;
20691 else if (row->used[TEXT_AREA])
20693 bool seen_this_string = false;
20694 struct glyph_row *r1 = row - 1;
20696 /* Did we see the same display string on the previous row? */
20697 if (STRINGP (it->object)
20698 /* this is not the first row */
20699 && row > it->w->desired_matrix->rows
20700 /* previous row is not the header line */
20701 && !r1->mode_line_p
20702 /* previous row also ends in a newline from a string */
20703 && r1->ends_in_newline_from_string_p)
20705 struct glyph *start, *end;
20707 /* Search for the last glyph of the previous row that came
20708 from buffer or string. Depending on whether the row is
20709 L2R or R2L, we need to process it front to back or the
20710 other way round. */
20711 if (!r1->reversed_p)
20713 start = r1->glyphs[TEXT_AREA];
20714 end = start + r1->used[TEXT_AREA];
20715 /* Glyphs inserted by redisplay have nil as their object. */
20716 while (end > start
20717 && NILP ((end - 1)->object)
20718 && (end - 1)->charpos <= 0)
20719 --end;
20720 if (end > start)
20722 if (EQ ((end - 1)->object, it->object))
20723 seen_this_string = true;
20725 else
20726 /* If all the glyphs of the previous row were inserted
20727 by redisplay, it means the previous row was
20728 produced from a single newline, which is only
20729 possible if that newline came from the same string
20730 as the one which produced this ROW. */
20731 seen_this_string = true;
20733 else
20735 end = r1->glyphs[TEXT_AREA] - 1;
20736 start = end + r1->used[TEXT_AREA];
20737 while (end < start
20738 && NILP ((end + 1)->object)
20739 && (end + 1)->charpos <= 0)
20740 ++end;
20741 if (end < start)
20743 if (EQ ((end + 1)->object, it->object))
20744 seen_this_string = true;
20746 else
20747 seen_this_string = true;
20750 /* Take note of each display string that covers a newline only
20751 once, the first time we see it. This is for when a display
20752 string includes more than one newline in it. */
20753 if (row->ends_in_newline_from_string_p && !seen_this_string)
20755 /* If we were scanning the buffer forward when we displayed
20756 the string, we want to account for at least one buffer
20757 position that belongs to this row (position covered by
20758 the display string), so that cursor positioning will
20759 consider this row as a candidate when point is at the end
20760 of the visual line represented by this row. This is not
20761 required when scanning back, because max_pos will already
20762 have a much larger value. */
20763 if (CHARPOS (row->end.pos) > max_pos)
20764 INC_BOTH (max_pos, max_bpos);
20765 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20767 else if (CHARPOS (it->eol_pos) > 0)
20768 SET_TEXT_POS (row->maxpos,
20769 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20770 else if (row->continued_p)
20772 /* If max_pos is different from IT's current position, it
20773 means IT->method does not belong to the display element
20774 at max_pos. However, it also means that the display
20775 element at max_pos was displayed in its entirety on this
20776 line, which is equivalent to saying that the next line
20777 starts at the next buffer position. */
20778 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20779 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20780 else
20782 INC_BOTH (max_pos, max_bpos);
20783 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20786 else if (row->truncated_on_right_p)
20787 /* display_line already called reseat_at_next_visible_line_start,
20788 which puts the iterator at the beginning of the next line, in
20789 the logical order. */
20790 row->maxpos = it->current.pos;
20791 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20792 /* A line that is entirely from a string/image/stretch... */
20793 row->maxpos = row->minpos;
20794 else
20795 emacs_abort ();
20797 else
20798 row->maxpos = it->current.pos;
20801 /* Like display_count_lines, but capable of counting outside of the
20802 current narrowed region. */
20803 static ptrdiff_t
20804 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20805 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20807 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20808 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20810 ptrdiff_t val;
20811 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20812 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20813 Fwiden ();
20814 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20815 unbind_to (pdl_count, Qnil);
20816 return val;
20819 /* Count the number of screen lines in window IT->w between character
20820 position IT_CHARPOS(*IT) and the line showing that window's point. */
20821 static ptrdiff_t
20822 display_count_lines_visually (struct it *it)
20824 struct it tem_it;
20825 ptrdiff_t to;
20826 struct text_pos from;
20828 /* If we already calculated a relative line number, use that. This
20829 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20830 are laid out sequentially, one by one, for each sequence of calls
20831 to display_line or other similar function that follows a call to
20832 init_iterator. */
20833 if (it->lnum_bytepos > 0)
20834 return it->lnum + 1;
20835 else
20837 ptrdiff_t count = SPECPDL_INDEX ();
20839 if (IT_CHARPOS (*it) <= PT)
20841 from = it->current.pos;
20842 to = PT;
20844 else
20846 SET_TEXT_POS (from, PT, PT_BYTE);
20847 to = IT_CHARPOS (*it);
20849 start_display (&tem_it, it->w, from);
20850 /* Need to disable visual mode temporarily, since otherwise the
20851 call to move_it_to will cause infinite recursion. */
20852 specbind (Qdisplay_line_numbers, Qrelative);
20853 /* Some redisplay optimizations could invoke us very far from
20854 PT, which will make the caller painfully slow. There should
20855 be no need to go too far beyond the window's bottom, as any
20856 such optimization will fail to show point anyway. */
20857 move_it_to (&tem_it, to, -1,
20858 tem_it.last_visible_y
20859 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20860 -1, MOVE_TO_POS | MOVE_TO_Y);
20861 unbind_to (count, Qnil);
20862 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20866 /* Produce the line-number glyphs for the current glyph_row. If
20867 IT->glyph_row is non-NULL, populate the row with the produced
20868 glyphs. */
20869 static void
20870 maybe_produce_line_number (struct it *it)
20872 ptrdiff_t last_line = it->lnum;
20873 ptrdiff_t start_from, bytepos;
20874 ptrdiff_t this_line;
20875 bool first_time = false;
20876 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20877 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20878 void *itdata = bidi_shelve_cache ();
20880 if (EQ (Vdisplay_line_numbers, Qvisual))
20881 this_line = display_count_lines_visually (it);
20882 else
20884 if (!last_line)
20886 /* If possible, reuse data cached by line-number-mode. */
20887 if (it->w->base_line_number > 0
20888 && it->w->base_line_pos > 0
20889 && it->w->base_line_pos <= IT_CHARPOS (*it)
20890 /* line-number-mode always displays narrowed line
20891 numbers, so we cannot use its data if the user wants
20892 line numbers that disregard narrowing, or if the
20893 buffer's narrowing has just changed. */
20894 && !(display_line_numbers_widen
20895 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
20896 && !current_buffer->clip_changed)
20898 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20899 last_line = it->w->base_line_number - 1;
20901 else
20902 start_from = beg_byte;
20903 if (!it->lnum_bytepos)
20904 first_time = true;
20906 else
20907 start_from = it->lnum_bytepos;
20909 /* Paranoia: what if someone changes the narrowing since the
20910 last time display_line was called? Shouldn't really happen,
20911 but who knows what some crazy Lisp invoked by :eval could do? */
20912 if (!(beg_byte <= start_from && start_from <= z_byte))
20914 last_line = 0;
20915 start_from = beg_byte;
20918 this_line =
20919 last_line + display_count_lines_logically (start_from,
20920 IT_BYTEPOS (*it),
20921 IT_CHARPOS (*it), &bytepos);
20922 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20923 eassert (bytepos == IT_BYTEPOS (*it));
20926 /* Record the line number information. */
20927 if (this_line != last_line || !it->lnum_bytepos)
20929 it->lnum = this_line;
20930 it->lnum_bytepos = IT_BYTEPOS (*it);
20933 /* Produce the glyphs for the line number. */
20934 struct it tem_it;
20935 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20936 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20937 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20938 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20939 int current_lnum_face_id
20940 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20941 /* Compute point's line number if needed. */
20942 if ((EQ (Vdisplay_line_numbers, Qrelative)
20943 || EQ (Vdisplay_line_numbers, Qvisual)
20944 || lnum_face_id != current_lnum_face_id)
20945 && !it->pt_lnum)
20947 ptrdiff_t ignored;
20948 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20949 it->pt_lnum =
20950 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20951 PT, &ignored);
20952 else
20953 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20954 &ignored);
20956 /* Compute the required width if needed. */
20957 if (!it->lnum_width)
20959 if (NATNUMP (Vdisplay_line_numbers_width))
20960 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
20962 /* Max line number to be displayed cannot be more than the one
20963 corresponding to the last row of the desired matrix. */
20964 ptrdiff_t max_lnum;
20966 if (NILP (Vdisplay_line_numbers_current_absolute)
20967 && (EQ (Vdisplay_line_numbers, Qrelative)
20968 || EQ (Vdisplay_line_numbers, Qvisual)))
20969 /* We subtract one more because the current line is always
20970 zero in this mode. */
20971 max_lnum = it->w->desired_matrix->nrows - 2;
20972 else if (EQ (Vdisplay_line_numbers, Qvisual))
20973 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
20974 else
20975 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20976 max_lnum = max (1, max_lnum);
20977 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
20978 eassert (it->lnum_width > 0);
20980 if (EQ (Vdisplay_line_numbers, Qrelative))
20981 lnum_offset = it->pt_lnum;
20982 else if (EQ (Vdisplay_line_numbers, Qvisual))
20983 lnum_offset = 0;
20985 /* Under 'relative', display the absolute line number for the
20986 current line, unless the user requests otherwise. */
20987 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
20988 if ((EQ (Vdisplay_line_numbers, Qrelative)
20989 || EQ (Vdisplay_line_numbers, Qvisual))
20990 && lnum_to_display == 0
20991 && !NILP (Vdisplay_line_numbers_current_absolute))
20992 lnum_to_display = it->pt_lnum + 1;
20993 /* In L2R rows we need to append the blank separator, in R2L
20994 rows we need to prepend it. But this function is usually
20995 called when no display elements were produced from the
20996 following line, so the paragraph direction might be unknown.
20997 Therefore we cheat and add 2 blanks, one on either side. */
20998 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
20999 strcat (lnum_buf, " ");
21001 /* Setup for producing the glyphs. */
21002 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21003 /* FIXME: Use specialized face. */
21004 DEFAULT_FACE_ID);
21005 scratch_glyph_row.reversed_p = false;
21006 scratch_glyph_row.used[TEXT_AREA] = 0;
21007 SET_TEXT_POS (tem_it.position, 0, 0);
21008 tem_it.avoid_cursor_p = true;
21009 tem_it.bidi_p = true;
21010 tem_it.bidi_it.type = WEAK_EN;
21011 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21012 1 level in R2L paragraphs. Emulate that, assuming we are in
21013 an L2R paragraph. */
21014 tem_it.bidi_it.resolved_level = 2;
21016 /* Produce glyphs for the line number in a scratch glyph_row. */
21017 int n_glyphs_before;
21018 for (const char *p = lnum_buf; *p; p++)
21020 /* For continuation lines and lines after ZV, instead of a line
21021 number, produce a blank prefix of the same width. Use the
21022 default face for the blank field beyond ZV. */
21023 if (beyond_zv)
21024 tem_it.face_id = it->base_face_id;
21025 else if (lnum_face_id != current_lnum_face_id
21026 && (EQ (Vdisplay_line_numbers, Qvisual)
21027 ? this_line == 0
21028 : this_line == it->pt_lnum))
21029 tem_it.face_id = current_lnum_face_id;
21030 else
21031 tem_it.face_id = lnum_face_id;
21032 if (beyond_zv
21033 /* Don't display the same line number more than once. */
21034 || (!EQ (Vdisplay_line_numbers, Qvisual)
21035 && (it->continuation_lines_width > 0
21036 || (this_line == last_line && !first_time))))
21037 tem_it.c = tem_it.char_to_display = ' ';
21038 else
21039 tem_it.c = tem_it.char_to_display = *p;
21040 tem_it.len = 1;
21041 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21042 /* Make sure these glyphs will have a "position" of -1. */
21043 SET_TEXT_POS (tem_it.position, -1, -1);
21044 PRODUCE_GLYPHS (&tem_it);
21046 /* Stop producing glyphs if we don't have enough space on
21047 this line. FIXME: should we refrain from producing the
21048 line number at all in that case? */
21049 if (tem_it.current_x > tem_it.last_visible_x)
21051 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21052 break;
21056 /* Record the width in pixels we need for the line number display. */
21057 it->lnum_pixel_width = tem_it.current_x;
21058 /* Copy the produced glyphs into IT's glyph_row. */
21059 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21060 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21061 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21062 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21064 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21066 for ( ; g < e; g++)
21068 it->current_x += g->pixel_width;
21069 /* The following is important when this function is called
21070 from move_it_in_display_line_to: HPOS is incremented only
21071 when we are in the visible portion of the glyph row. */
21072 if (it->current_x > it->first_visible_x)
21073 it->hpos++;
21074 if (p)
21076 *p++ = *g;
21077 (*u)++;
21081 /* Update IT's metrics due to glyphs produced for line numbers. */
21082 if (it->glyph_row)
21084 struct glyph_row *row = it->glyph_row;
21086 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21087 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21088 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21089 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21090 tem_it.max_phys_descent);
21092 else
21094 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21095 it->max_descent = max (it->max_descent, tem_it.max_descent);
21096 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21097 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21100 bidi_unshelve_cache (itdata, false);
21103 /* Return true if this glyph row needs a line number to be produced
21104 for it. */
21105 static bool
21106 should_produce_line_number (struct it *it)
21108 if (NILP (Vdisplay_line_numbers))
21109 return false;
21111 /* Don't display line numbers in minibuffer windows. */
21112 if (MINI_WINDOW_P (it->w))
21113 return false;
21115 #ifdef HAVE_WINDOW_SYSTEM
21116 /* Don't display line number in tooltip frames. */
21117 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame))
21118 return false;
21119 #endif
21121 /* If the character at current position has a non-nil special
21122 property, disable line numbers for this row. This is for
21123 packages such as company-mode, which need this for their tricky
21124 layout, where line numbers get in the way. */
21125 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21126 Qdisplay_line_numbers_disable,
21127 it->window);
21128 /* For ZV, we need to also look in empty overlays at that point,
21129 because get-char-property always returns nil for ZV, except if
21130 the property is in 'default-text-properties'. */
21131 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21132 val = disable_line_numbers_overlay_at_eob ();
21133 return NILP (val) ? true : false;
21136 /* Return true if ROW has no glyphs except those inserted by the
21137 display engine. This is needed for indicate-empty-lines and
21138 similar features when the glyph row starts with glyphs which didn't
21139 come from buffer or string. */
21140 static bool
21141 row_text_area_empty (struct glyph_row *row)
21143 if (!row->reversed_p)
21145 for (struct glyph *g = row->glyphs[TEXT_AREA];
21146 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21147 g++)
21148 if (!NILP (g->object) || g->charpos > 0)
21149 return false;
21151 else
21153 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21154 g > row->glyphs[TEXT_AREA];
21155 g--)
21156 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21157 return false;
21160 return true;
21163 /* Construct the glyph row IT->glyph_row in the desired matrix of
21164 IT->w from text at the current position of IT. See dispextern.h
21165 for an overview of struct it. Value is true if
21166 IT->glyph_row displays text, as opposed to a line displaying ZV
21167 only. CURSOR_VPOS is the window-relative vertical position of
21168 the glyph row displaying the cursor, or -1 if unknown. */
21170 static bool
21171 display_line (struct it *it, int cursor_vpos)
21173 struct glyph_row *row = it->glyph_row;
21174 Lisp_Object overlay_arrow_string;
21175 struct it wrap_it;
21176 void *wrap_data = NULL;
21177 bool may_wrap = false;
21178 int wrap_x UNINIT;
21179 int wrap_row_used = -1;
21180 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21181 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21182 int wrap_row_extra_line_spacing UNINIT;
21183 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21184 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21185 int cvpos;
21186 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21187 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21188 bool pending_handle_line_prefix = false;
21189 int header_line = window_wants_header_line (it->w);
21190 bool hscroll_this_line = (cursor_vpos >= 0
21191 && it->vpos == cursor_vpos - header_line
21192 && hscrolling_current_line_p (it->w));
21193 int first_visible_x = it->first_visible_x;
21194 int last_visible_x = it->last_visible_x;
21195 int x_incr = 0;
21197 /* We always start displaying at hpos zero even if hscrolled. */
21198 eassert (it->hpos == 0 && it->current_x == 0);
21200 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21201 >= it->w->desired_matrix->nrows)
21203 it->w->nrows_scale_factor++;
21204 it->f->fonts_changed = true;
21205 return false;
21208 /* Clear the result glyph row and enable it. */
21209 prepare_desired_row (it->w, row, false);
21211 row->y = it->current_y;
21212 row->start = it->start;
21213 row->continuation_lines_width = it->continuation_lines_width;
21214 row->displays_text_p = true;
21215 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21216 it->starts_in_middle_of_char_p = false;
21218 /* Arrange the overlays nicely for our purposes. Usually, we call
21219 display_line on only one line at a time, in which case this
21220 can't really hurt too much, or we call it on lines which appear
21221 one after another in the buffer, in which case all calls to
21222 recenter_overlay_lists but the first will be pretty cheap. */
21223 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21225 /* If we are going to display the cursor's line, account for the
21226 hscroll of that line. We subtract the window's min_hscroll,
21227 because that was already accounted for in init_iterator. */
21228 if (hscroll_this_line)
21229 x_incr =
21230 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21231 * FRAME_COLUMN_WIDTH (it->f);
21233 bool line_number_needed = should_produce_line_number (it);
21235 /* Move over display elements that are not visible because we are
21236 hscrolled. This may stop at an x-position < first_visible_x
21237 if the first glyph is partially visible or if we hit a line end. */
21238 if (it->current_x < it->first_visible_x + x_incr)
21240 enum move_it_result move_result;
21242 this_line_min_pos = row->start.pos;
21243 if (hscroll_this_line)
21245 it->first_visible_x += x_incr;
21246 it->last_visible_x += x_incr;
21248 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21249 MOVE_TO_POS | MOVE_TO_X);
21250 /* If we are under a large hscroll, move_it_in_display_line_to
21251 could hit the end of the line without reaching
21252 first_visible_x. Pretend that we did reach it. This is
21253 especially important on a TTY, where we will call
21254 extend_face_to_end_of_line, which needs to know how many
21255 blank glyphs to produce. */
21256 if (it->current_x < it->first_visible_x
21257 && (move_result == MOVE_NEWLINE_OR_CR
21258 || move_result == MOVE_POS_MATCH_OR_ZV))
21259 it->current_x = it->first_visible_x;
21261 /* Record the smallest positions seen while we moved over
21262 display elements that are not visible. This is needed by
21263 redisplay_internal for optimizing the case where the cursor
21264 stays inside the same line. The rest of this function only
21265 considers positions that are actually displayed, so
21266 RECORD_MAX_MIN_POS will not otherwise record positions that
21267 are hscrolled to the left of the left edge of the window. */
21268 min_pos = CHARPOS (this_line_min_pos);
21269 min_bpos = BYTEPOS (this_line_min_pos);
21271 /* Produce line number, if needed. */
21272 if (line_number_needed)
21273 maybe_produce_line_number (it);
21275 else if (it->area == TEXT_AREA)
21277 /* Line numbers should precede the line-prefix or wrap-prefix. */
21278 if (line_number_needed)
21279 maybe_produce_line_number (it);
21281 /* We only do this when not calling move_it_in_display_line_to
21282 above, because that function calls itself handle_line_prefix. */
21283 handle_line_prefix (it);
21285 else
21287 /* Line-prefix and wrap-prefix are always displayed in the text
21288 area. But if this is the first call to display_line after
21289 init_iterator, the iterator might have been set up to write
21290 into a marginal area, e.g. if the line begins with some
21291 display property that writes to the margins. So we need to
21292 wait with the call to handle_line_prefix until whatever
21293 writes to the margin has done its job. */
21294 pending_handle_line_prefix = true;
21297 /* Get the initial row height. This is either the height of the
21298 text hscrolled, if there is any, or zero. */
21299 row->ascent = it->max_ascent;
21300 row->height = it->max_ascent + it->max_descent;
21301 row->phys_ascent = it->max_phys_ascent;
21302 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21303 row->extra_line_spacing = it->max_extra_line_spacing;
21305 /* Utility macro to record max and min buffer positions seen until now. */
21306 #define RECORD_MAX_MIN_POS(IT) \
21307 do \
21309 bool composition_p \
21310 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21311 ptrdiff_t current_pos = \
21312 composition_p ? (IT)->cmp_it.charpos \
21313 : IT_CHARPOS (*(IT)); \
21314 ptrdiff_t current_bpos = \
21315 composition_p ? CHAR_TO_BYTE (current_pos) \
21316 : IT_BYTEPOS (*(IT)); \
21317 if (current_pos < min_pos) \
21319 min_pos = current_pos; \
21320 min_bpos = current_bpos; \
21322 if (IT_CHARPOS (*it) > max_pos) \
21324 max_pos = IT_CHARPOS (*it); \
21325 max_bpos = IT_BYTEPOS (*it); \
21328 while (false)
21330 /* Loop generating characters. The loop is left with IT on the next
21331 character to display. */
21332 while (true)
21334 int n_glyphs_before, hpos_before, x_before;
21335 int x, nglyphs;
21336 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21338 /* Retrieve the next thing to display. Value is false if end of
21339 buffer reached. */
21340 if (!get_next_display_element (it))
21342 bool row_has_glyphs = false;
21343 /* Maybe add a space at the end of this line that is used to
21344 display the cursor there under X. Set the charpos of the
21345 first glyph of blank lines not corresponding to any text
21346 to -1. */
21347 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21348 row->exact_window_width_line_p = true;
21349 else if ((append_space_for_newline (it, true)
21350 && row->used[TEXT_AREA] == 1)
21351 || row->used[TEXT_AREA] == 0
21352 || (row_has_glyphs = row_text_area_empty (row)))
21354 row->glyphs[TEXT_AREA]->charpos = -1;
21355 /* Don't reset the displays_text_p flag if we are
21356 displaying line numbers or line-prefix. */
21357 if (!row_has_glyphs)
21358 row->displays_text_p = false;
21360 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21361 && (!MINI_WINDOW_P (it->w)))
21362 row->indicate_empty_line_p = true;
21365 it->continuation_lines_width = 0;
21366 /* Reset those iterator values set from display property
21367 values. This is for the case when the display property
21368 ends at ZV, and is not a replacing property, so pop_it is
21369 not called. */
21370 it->font_height = Qnil;
21371 it->voffset = 0;
21372 row->ends_at_zv_p = true;
21373 /* A row that displays right-to-left text must always have
21374 its last face extended all the way to the end of line,
21375 even if this row ends in ZV, because we still write to
21376 the screen left to right. We also need to extend the
21377 last face if the default face is remapped to some
21378 different face, otherwise the functions that clear
21379 portions of the screen will clear with the default face's
21380 background color. */
21381 if (row->reversed_p
21382 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21383 extend_face_to_end_of_line (it);
21384 break;
21387 /* Now, get the metrics of what we want to display. This also
21388 generates glyphs in `row' (which is IT->glyph_row). */
21389 n_glyphs_before = row->used[TEXT_AREA];
21390 x = it->current_x;
21392 /* Remember the line height so far in case the next element doesn't
21393 fit on the line. */
21394 if (it->line_wrap != TRUNCATE)
21396 ascent = it->max_ascent;
21397 descent = it->max_descent;
21398 phys_ascent = it->max_phys_ascent;
21399 phys_descent = it->max_phys_descent;
21401 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21403 if (IT_DISPLAYING_WHITESPACE (it))
21404 may_wrap = true;
21405 else if (may_wrap)
21407 SAVE_IT (wrap_it, *it, wrap_data);
21408 wrap_x = x;
21409 wrap_row_used = row->used[TEXT_AREA];
21410 wrap_row_ascent = row->ascent;
21411 wrap_row_height = row->height;
21412 wrap_row_phys_ascent = row->phys_ascent;
21413 wrap_row_phys_height = row->phys_height;
21414 wrap_row_extra_line_spacing = row->extra_line_spacing;
21415 wrap_row_min_pos = min_pos;
21416 wrap_row_min_bpos = min_bpos;
21417 wrap_row_max_pos = max_pos;
21418 wrap_row_max_bpos = max_bpos;
21419 may_wrap = false;
21424 PRODUCE_GLYPHS (it);
21426 /* If this display element was in marginal areas, continue with
21427 the next one. */
21428 if (it->area != TEXT_AREA)
21430 row->ascent = max (row->ascent, it->max_ascent);
21431 row->height = max (row->height, it->max_ascent + it->max_descent);
21432 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21433 row->phys_height = max (row->phys_height,
21434 it->max_phys_ascent + it->max_phys_descent);
21435 row->extra_line_spacing = max (row->extra_line_spacing,
21436 it->max_extra_line_spacing);
21437 set_iterator_to_next (it, true);
21438 /* If we didn't handle the line/wrap prefix above, and the
21439 call to set_iterator_to_next just switched to TEXT_AREA,
21440 process the prefix now. */
21441 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21443 /* Line numbers should precede the line-prefix or wrap-prefix. */
21444 if (line_number_needed)
21445 maybe_produce_line_number (it);
21447 pending_handle_line_prefix = false;
21448 handle_line_prefix (it);
21450 continue;
21453 /* Does the display element fit on the line? If we truncate
21454 lines, we should draw past the right edge of the window. If
21455 we don't truncate, we want to stop so that we can display the
21456 continuation glyph before the right margin. If lines are
21457 continued, there are two possible strategies for characters
21458 resulting in more than 1 glyph (e.g. tabs): Display as many
21459 glyphs as possible in this line and leave the rest for the
21460 continuation line, or display the whole element in the next
21461 line. Original redisplay did the former, so we do it also. */
21462 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21463 hpos_before = it->hpos;
21464 x_before = x;
21466 if (/* Not a newline. */
21467 nglyphs > 0
21468 /* Glyphs produced fit entirely in the line. */
21469 && it->current_x < it->last_visible_x)
21471 it->hpos += nglyphs;
21472 row->ascent = max (row->ascent, it->max_ascent);
21473 row->height = max (row->height, it->max_ascent + it->max_descent);
21474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21475 row->phys_height = max (row->phys_height,
21476 it->max_phys_ascent + it->max_phys_descent);
21477 row->extra_line_spacing = max (row->extra_line_spacing,
21478 it->max_extra_line_spacing);
21479 if (it->current_x - it->pixel_width < it->first_visible_x
21480 /* In R2L rows, we arrange in extend_face_to_end_of_line
21481 to add a right offset to the line, by a suitable
21482 change to the stretch glyph that is the leftmost
21483 glyph of the line. */
21484 && !row->reversed_p)
21485 row->x = x - it->first_visible_x;
21486 /* Record the maximum and minimum buffer positions seen so
21487 far in glyphs that will be displayed by this row. */
21488 if (it->bidi_p)
21489 RECORD_MAX_MIN_POS (it);
21491 else
21493 int i, new_x;
21494 struct glyph *glyph;
21496 for (i = 0; i < nglyphs; ++i, x = new_x)
21498 /* Identify the glyphs added by the last call to
21499 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21500 the previous glyphs. */
21501 if (!row->reversed_p)
21502 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21503 else
21504 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21505 new_x = x + glyph->pixel_width;
21507 if (/* Lines are continued. */
21508 it->line_wrap != TRUNCATE
21509 && (/* Glyph doesn't fit on the line. */
21510 new_x > it->last_visible_x
21511 /* Or it fits exactly on a window system frame. */
21512 || (new_x == it->last_visible_x
21513 && FRAME_WINDOW_P (it->f)
21514 && (row->reversed_p
21515 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21516 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21518 /* End of a continued line. */
21520 if (it->hpos == 0
21521 || (new_x == it->last_visible_x
21522 && FRAME_WINDOW_P (it->f)
21523 && (row->reversed_p
21524 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21525 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21527 /* Current glyph is the only one on the line or
21528 fits exactly on the line. We must continue
21529 the line because we can't draw the cursor
21530 after the glyph. */
21531 row->continued_p = true;
21532 it->current_x = new_x;
21533 it->continuation_lines_width += new_x;
21534 ++it->hpos;
21535 if (i == nglyphs - 1)
21537 /* If line-wrap is on, check if a previous
21538 wrap point was found. */
21539 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21540 && wrap_row_used > 0
21541 /* Even if there is a previous wrap
21542 point, continue the line here as
21543 usual, if (i) the previous character
21544 was a space or tab AND (ii) the
21545 current character is not. */
21546 && (!may_wrap
21547 || IT_DISPLAYING_WHITESPACE (it)))
21548 goto back_to_wrap;
21550 /* Record the maximum and minimum buffer
21551 positions seen so far in glyphs that will be
21552 displayed by this row. */
21553 if (it->bidi_p)
21554 RECORD_MAX_MIN_POS (it);
21555 set_iterator_to_next (it, true);
21556 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21558 if (!get_next_display_element (it))
21560 row->exact_window_width_line_p = true;
21561 it->continuation_lines_width = 0;
21562 it->font_height = Qnil;
21563 it->voffset = 0;
21564 row->continued_p = false;
21565 row->ends_at_zv_p = true;
21567 else if (ITERATOR_AT_END_OF_LINE_P (it))
21569 row->continued_p = false;
21570 row->exact_window_width_line_p = true;
21572 /* If line-wrap is on, check if a
21573 previous wrap point was found. */
21574 else if (wrap_row_used > 0
21575 /* Even if there is a previous wrap
21576 point, continue the line here as
21577 usual, if (i) the previous character
21578 was a space or tab AND (ii) the
21579 current character is not. */
21580 && (!may_wrap
21581 || IT_DISPLAYING_WHITESPACE (it)))
21582 goto back_to_wrap;
21586 else if (it->bidi_p)
21587 RECORD_MAX_MIN_POS (it);
21588 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21589 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21590 extend_face_to_end_of_line (it);
21592 else if (CHAR_GLYPH_PADDING_P (*glyph)
21593 && !FRAME_WINDOW_P (it->f))
21595 /* A padding glyph that doesn't fit on this line.
21596 This means the whole character doesn't fit
21597 on the line. */
21598 if (row->reversed_p)
21599 unproduce_glyphs (it, row->used[TEXT_AREA]
21600 - n_glyphs_before);
21601 row->used[TEXT_AREA] = n_glyphs_before;
21603 /* Fill the rest of the row with continuation
21604 glyphs like in 20.x. */
21605 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21606 < row->glyphs[1 + TEXT_AREA])
21607 produce_special_glyphs (it, IT_CONTINUATION);
21609 row->continued_p = true;
21610 it->current_x = x_before;
21611 it->continuation_lines_width += x_before;
21613 /* Restore the height to what it was before the
21614 element not fitting on the line. */
21615 it->max_ascent = ascent;
21616 it->max_descent = descent;
21617 it->max_phys_ascent = phys_ascent;
21618 it->max_phys_descent = phys_descent;
21619 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21620 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21621 extend_face_to_end_of_line (it);
21623 else if (wrap_row_used > 0)
21625 back_to_wrap:
21626 if (row->reversed_p)
21627 unproduce_glyphs (it,
21628 row->used[TEXT_AREA] - wrap_row_used);
21629 RESTORE_IT (it, &wrap_it, wrap_data);
21630 it->continuation_lines_width += wrap_x;
21631 row->used[TEXT_AREA] = wrap_row_used;
21632 row->ascent = wrap_row_ascent;
21633 row->height = wrap_row_height;
21634 row->phys_ascent = wrap_row_phys_ascent;
21635 row->phys_height = wrap_row_phys_height;
21636 row->extra_line_spacing = wrap_row_extra_line_spacing;
21637 min_pos = wrap_row_min_pos;
21638 min_bpos = wrap_row_min_bpos;
21639 max_pos = wrap_row_max_pos;
21640 max_bpos = wrap_row_max_bpos;
21641 row->continued_p = true;
21642 row->ends_at_zv_p = false;
21643 row->exact_window_width_line_p = false;
21644 it->continuation_lines_width += x;
21646 /* Make sure that a non-default face is extended
21647 up to the right margin of the window. */
21648 extend_face_to_end_of_line (it);
21650 else if ((it->what == IT_CHARACTER
21651 || it->what == IT_STRETCH
21652 || it->what == IT_COMPOSITION)
21653 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21655 /* A TAB that extends past the right edge of the
21656 window. This produces a single glyph on
21657 window system frames. We leave the glyph in
21658 this row and let it fill the row, but don't
21659 consume the TAB. */
21660 if ((row->reversed_p
21661 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21662 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21663 produce_special_glyphs (it, IT_CONTINUATION);
21664 it->continuation_lines_width += it->last_visible_x;
21665 row->ends_in_middle_of_char_p = true;
21666 row->continued_p = true;
21667 glyph->pixel_width = it->last_visible_x - x;
21668 it->starts_in_middle_of_char_p = true;
21669 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21670 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21671 extend_face_to_end_of_line (it);
21673 else
21675 /* Something other than a TAB that draws past
21676 the right edge of the window. Restore
21677 positions to values before the element. */
21678 if (row->reversed_p)
21679 unproduce_glyphs (it, row->used[TEXT_AREA]
21680 - (n_glyphs_before + i));
21681 row->used[TEXT_AREA] = n_glyphs_before + i;
21683 /* Display continuation glyphs. */
21684 it->current_x = x_before;
21685 it->continuation_lines_width += x;
21686 if (!FRAME_WINDOW_P (it->f)
21687 || (row->reversed_p
21688 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21689 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21690 produce_special_glyphs (it, IT_CONTINUATION);
21691 row->continued_p = true;
21693 extend_face_to_end_of_line (it);
21695 if (nglyphs > 1 && i > 0)
21697 row->ends_in_middle_of_char_p = true;
21698 it->starts_in_middle_of_char_p = true;
21701 /* Restore the height to what it was before the
21702 element not fitting on the line. */
21703 it->max_ascent = ascent;
21704 it->max_descent = descent;
21705 it->max_phys_ascent = phys_ascent;
21706 it->max_phys_descent = phys_descent;
21709 break;
21711 else if (new_x > it->first_visible_x)
21713 /* Increment number of glyphs actually displayed. */
21714 ++it->hpos;
21716 /* Record the maximum and minimum buffer positions
21717 seen so far in glyphs that will be displayed by
21718 this row. */
21719 if (it->bidi_p)
21720 RECORD_MAX_MIN_POS (it);
21722 if (x < it->first_visible_x && !row->reversed_p)
21723 /* Glyph is partially visible, i.e. row starts at
21724 negative X position. Don't do that in R2L
21725 rows, where we arrange to add a right offset to
21726 the line in extend_face_to_end_of_line, by a
21727 suitable change to the stretch glyph that is
21728 the leftmost glyph of the line. */
21729 row->x = x - it->first_visible_x;
21730 /* When the last glyph of an R2L row only fits
21731 partially on the line, we need to set row->x to a
21732 negative offset, so that the leftmost glyph is
21733 the one that is partially visible. But if we are
21734 going to produce the truncation glyph, this will
21735 be taken care of in produce_special_glyphs. */
21736 if (row->reversed_p
21737 && new_x > it->last_visible_x
21738 && !(it->line_wrap == TRUNCATE
21739 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21741 eassert (FRAME_WINDOW_P (it->f));
21742 row->x = it->last_visible_x - new_x;
21745 else
21747 /* Glyph is completely off the left margin of the
21748 window. This should not happen because of the
21749 move_it_in_display_line at the start of this
21750 function, unless the text display area of the
21751 window is empty. */
21752 eassert (it->first_visible_x <= it->last_visible_x);
21755 /* Even if this display element produced no glyphs at all,
21756 we want to record its position. */
21757 if (it->bidi_p && nglyphs == 0)
21758 RECORD_MAX_MIN_POS (it);
21760 row->ascent = max (row->ascent, it->max_ascent);
21761 row->height = max (row->height, it->max_ascent + it->max_descent);
21762 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21763 row->phys_height = max (row->phys_height,
21764 it->max_phys_ascent + it->max_phys_descent);
21765 row->extra_line_spacing = max (row->extra_line_spacing,
21766 it->max_extra_line_spacing);
21768 /* End of this display line if row is continued. */
21769 if (row->continued_p || row->ends_at_zv_p)
21770 break;
21773 at_end_of_line:
21774 /* Is this a line end? If yes, we're also done, after making
21775 sure that a non-default face is extended up to the right
21776 margin of the window. */
21777 if (ITERATOR_AT_END_OF_LINE_P (it))
21779 int used_before = row->used[TEXT_AREA];
21781 row->ends_in_newline_from_string_p = STRINGP (it->object);
21783 /* Add a space at the end of the line that is used to
21784 display the cursor there. */
21785 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21786 append_space_for_newline (it, false);
21788 /* Extend the face to the end of the line. */
21789 extend_face_to_end_of_line (it);
21791 /* Make sure we have the position. */
21792 if (used_before == 0)
21793 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21795 /* Record the position of the newline, for use in
21796 find_row_edges. */
21797 it->eol_pos = it->current.pos;
21799 /* Consume the line end. This skips over invisible lines. */
21800 set_iterator_to_next (it, true);
21801 it->continuation_lines_width = 0;
21802 break;
21805 /* Proceed with next display element. Note that this skips
21806 over lines invisible because of selective display. */
21807 set_iterator_to_next (it, true);
21809 /* If we truncate lines, we are done when the last displayed
21810 glyphs reach past the right margin of the window. */
21811 if (it->line_wrap == TRUNCATE
21812 && ((FRAME_WINDOW_P (it->f)
21813 /* Images are preprocessed in produce_image_glyph such
21814 that they are cropped at the right edge of the
21815 window, so an image glyph will always end exactly at
21816 last_visible_x, even if there's no right fringe. */
21817 && ((row->reversed_p
21818 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21819 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21820 || it->what == IT_IMAGE))
21821 ? (it->current_x >= it->last_visible_x)
21822 : (it->current_x > it->last_visible_x)))
21824 /* Maybe add truncation glyphs. */
21825 if (!FRAME_WINDOW_P (it->f)
21826 || (row->reversed_p
21827 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21828 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21830 int i, n;
21832 if (!row->reversed_p)
21834 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21835 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21836 break;
21838 else
21840 for (i = 0; i < row->used[TEXT_AREA]; i++)
21841 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21842 break;
21843 /* Remove any padding glyphs at the front of ROW, to
21844 make room for the truncation glyphs we will be
21845 adding below. The loop below always inserts at
21846 least one truncation glyph, so also remove the
21847 last glyph added to ROW. */
21848 unproduce_glyphs (it, i + 1);
21849 /* Adjust i for the loop below. */
21850 i = row->used[TEXT_AREA] - (i + 1);
21853 /* produce_special_glyphs overwrites the last glyph, so
21854 we don't want that if we want to keep that last
21855 glyph, which means it's an image. */
21856 if (it->current_x > it->last_visible_x)
21858 it->current_x = x_before;
21859 if (!FRAME_WINDOW_P (it->f))
21861 for (n = row->used[TEXT_AREA]; i < n; ++i)
21863 row->used[TEXT_AREA] = i;
21864 produce_special_glyphs (it, IT_TRUNCATION);
21867 else
21869 row->used[TEXT_AREA] = i;
21870 produce_special_glyphs (it, IT_TRUNCATION);
21872 it->hpos = hpos_before;
21875 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21877 /* Don't truncate if we can overflow newline into fringe. */
21878 if (!get_next_display_element (it))
21880 it->continuation_lines_width = 0;
21881 it->font_height = Qnil;
21882 it->voffset = 0;
21883 row->ends_at_zv_p = true;
21884 row->exact_window_width_line_p = true;
21885 break;
21887 if (ITERATOR_AT_END_OF_LINE_P (it))
21889 row->exact_window_width_line_p = true;
21890 goto at_end_of_line;
21892 it->current_x = x_before;
21893 it->hpos = hpos_before;
21896 row->truncated_on_right_p = true;
21897 it->continuation_lines_width = 0;
21898 reseat_at_next_visible_line_start (it, false);
21899 /* We insist below that IT's position be at ZV because in
21900 bidi-reordered lines the character at visible line start
21901 might not be the character that follows the newline in
21902 the logical order. */
21903 if (IT_BYTEPOS (*it) > BEG_BYTE)
21904 row->ends_at_zv_p =
21905 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21906 else
21907 row->ends_at_zv_p = false;
21908 break;
21912 if (wrap_data)
21913 bidi_unshelve_cache (wrap_data, true);
21915 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21916 at the left window margin. */
21917 if (it->first_visible_x
21918 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21920 if (!FRAME_WINDOW_P (it->f)
21921 || (((row->reversed_p
21922 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21923 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21924 /* Don't let insert_left_trunc_glyphs overwrite the
21925 first glyph of the row if it is an image. */
21926 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21927 insert_left_trunc_glyphs (it);
21928 row->truncated_on_left_p = true;
21931 /* Remember the position at which this line ends.
21933 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21934 cannot be before the call to find_row_edges below, since that is
21935 where these positions are determined. */
21936 row->end = it->current;
21937 if (!it->bidi_p)
21939 row->minpos = row->start.pos;
21940 row->maxpos = row->end.pos;
21942 else
21944 /* ROW->minpos and ROW->maxpos must be the smallest and
21945 `1 + the largest' buffer positions in ROW. But if ROW was
21946 bidi-reordered, these two positions can be anywhere in the
21947 row, so we must determine them now. */
21948 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21951 /* If the start of this line is the overlay arrow-position, then
21952 mark this glyph row as the one containing the overlay arrow.
21953 This is clearly a mess with variable size fonts. It would be
21954 better to let it be displayed like cursors under X. */
21955 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21956 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21957 !NILP (overlay_arrow_string)))
21959 /* Overlay arrow in window redisplay is a fringe bitmap. */
21960 if (STRINGP (overlay_arrow_string))
21962 struct glyph_row *arrow_row
21963 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21964 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21965 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21966 struct glyph *p = row->glyphs[TEXT_AREA];
21967 struct glyph *p2, *end;
21969 /* Copy the arrow glyphs. */
21970 while (glyph < arrow_end)
21971 *p++ = *glyph++;
21973 /* Throw away padding glyphs. */
21974 p2 = p;
21975 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21976 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21977 ++p2;
21978 if (p2 > p)
21980 while (p2 < end)
21981 *p++ = *p2++;
21982 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21985 else
21987 eassert (INTEGERP (overlay_arrow_string));
21988 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21990 overlay_arrow_seen = true;
21993 /* Highlight trailing whitespace. */
21994 if (!NILP (Vshow_trailing_whitespace))
21995 highlight_trailing_whitespace (it->f, it->glyph_row);
21997 /* Compute pixel dimensions of this line. */
21998 compute_line_metrics (it);
22000 /* Implementation note: No changes in the glyphs of ROW or in their
22001 faces can be done past this point, because compute_line_metrics
22002 computes ROW's hash value and stores it within the glyph_row
22003 structure. */
22005 /* Record whether this row ends inside an ellipsis. */
22006 row->ends_in_ellipsis_p
22007 = (it->method == GET_FROM_DISPLAY_VECTOR
22008 && it->ellipsis_p);
22010 /* Save fringe bitmaps in this row. */
22011 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22012 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22013 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22014 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22016 it->left_user_fringe_bitmap = 0;
22017 it->left_user_fringe_face_id = 0;
22018 it->right_user_fringe_bitmap = 0;
22019 it->right_user_fringe_face_id = 0;
22021 /* Maybe set the cursor. */
22022 cvpos = it->w->cursor.vpos;
22023 if ((cvpos < 0
22024 /* In bidi-reordered rows, keep checking for proper cursor
22025 position even if one has been found already, because buffer
22026 positions in such rows change non-linearly with ROW->VPOS,
22027 when a line is continued. One exception: when we are at ZV,
22028 display cursor on the first suitable glyph row, since all
22029 the empty rows after that also have their position set to ZV. */
22030 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22031 lines' rows is implemented for bidi-reordered rows. */
22032 || (it->bidi_p
22033 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22034 && PT >= MATRIX_ROW_START_CHARPOS (row)
22035 && PT <= MATRIX_ROW_END_CHARPOS (row)
22036 && cursor_row_p (row))
22037 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22039 /* Prepare for the next line. This line starts horizontally at (X
22040 HPOS) = (0 0). Vertical positions are incremented. As a
22041 convenience for the caller, IT->glyph_row is set to the next
22042 row to be used. */
22043 it->current_x = it->hpos = 0;
22044 it->current_y += row->height;
22045 /* Restore the first and last visible X if we adjusted them for
22046 current-line hscrolling. */
22047 if (hscroll_this_line)
22049 it->first_visible_x = first_visible_x;
22050 it->last_visible_x = last_visible_x;
22052 SET_TEXT_POS (it->eol_pos, 0, 0);
22053 ++it->vpos;
22054 ++it->glyph_row;
22055 /* The next row should by default use the same value of the
22056 reversed_p flag as this one. set_iterator_to_next decides when
22057 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22058 the flag accordingly. */
22059 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22060 it->glyph_row->reversed_p = row->reversed_p;
22061 it->start = row->end;
22062 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22064 #undef RECORD_MAX_MIN_POS
22067 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22068 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22069 doc: /* Return paragraph direction at point in BUFFER.
22070 Value is either `left-to-right' or `right-to-left'.
22071 If BUFFER is omitted or nil, it defaults to the current buffer.
22073 Paragraph direction determines how the text in the paragraph is displayed.
22074 In left-to-right paragraphs, text begins at the left margin of the window
22075 and the reading direction is generally left to right. In right-to-left
22076 paragraphs, text begins at the right margin and is read from right to left.
22078 See also `bidi-paragraph-direction'. */)
22079 (Lisp_Object buffer)
22081 struct buffer *buf = current_buffer;
22082 struct buffer *old = buf;
22084 if (! NILP (buffer))
22086 CHECK_BUFFER (buffer);
22087 buf = XBUFFER (buffer);
22090 if (NILP (BVAR (buf, bidi_display_reordering))
22091 || NILP (BVAR (buf, enable_multibyte_characters))
22092 /* When we are loading loadup.el, the character property tables
22093 needed for bidi iteration are not yet available. */
22094 || redisplay__inhibit_bidi)
22095 return Qleft_to_right;
22096 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22097 return BVAR (buf, bidi_paragraph_direction);
22098 else
22100 /* Determine the direction from buffer text. We could try to
22101 use current_matrix if it is up to date, but this seems fast
22102 enough as it is. */
22103 struct bidi_it itb;
22104 ptrdiff_t pos = BUF_PT (buf);
22105 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22106 int c;
22107 void *itb_data = bidi_shelve_cache ();
22109 set_buffer_temp (buf);
22110 /* bidi_paragraph_init finds the base direction of the paragraph
22111 by searching forward from paragraph start. We need the base
22112 direction of the current or _previous_ paragraph, so we need
22113 to make sure we are within that paragraph. To that end, find
22114 the previous non-empty line. */
22115 if (pos >= ZV && pos > BEGV)
22116 DEC_BOTH (pos, bytepos);
22117 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22118 if (fast_looking_at (trailing_white_space,
22119 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22121 while ((c = FETCH_BYTE (bytepos)) == '\n'
22122 || c == ' ' || c == '\t' || c == '\f')
22124 if (bytepos <= BEGV_BYTE)
22125 break;
22126 bytepos--;
22127 pos--;
22129 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22130 bytepos--;
22132 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22133 itb.paragraph_dir = NEUTRAL_DIR;
22134 itb.string.s = NULL;
22135 itb.string.lstring = Qnil;
22136 itb.string.bufpos = 0;
22137 itb.string.from_disp_str = false;
22138 itb.string.unibyte = false;
22139 /* We have no window to use here for ignoring window-specific
22140 overlays. Using NULL for window pointer will cause
22141 compute_display_string_pos to use the current buffer. */
22142 itb.w = NULL;
22143 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22144 bidi_unshelve_cache (itb_data, false);
22145 set_buffer_temp (old);
22146 switch (itb.paragraph_dir)
22148 case L2R:
22149 return Qleft_to_right;
22150 break;
22151 case R2L:
22152 return Qright_to_left;
22153 break;
22154 default:
22155 emacs_abort ();
22160 DEFUN ("bidi-find-overridden-directionality",
22161 Fbidi_find_overridden_directionality,
22162 Sbidi_find_overridden_directionality, 2, 3, 0,
22163 doc: /* Return position between FROM and TO where directionality was overridden.
22165 This function returns the first character position in the specified
22166 region of OBJECT where there is a character whose `bidi-class' property
22167 is `L', but which was forced to display as `R' by a directional
22168 override, and likewise with characters whose `bidi-class' is `R'
22169 or `AL' that were forced to display as `L'.
22171 If no such character is found, the function returns nil.
22173 OBJECT is a Lisp string or buffer to search for overridden
22174 directionality, and defaults to the current buffer if nil or omitted.
22175 OBJECT can also be a window, in which case the function will search
22176 the buffer displayed in that window. Passing the window instead of
22177 a buffer is preferable when the buffer is displayed in some window,
22178 because this function will then be able to correctly account for
22179 window-specific overlays, which can affect the results.
22181 Strong directional characters `L', `R', and `AL' can have their
22182 intrinsic directionality overridden by directional override
22183 control characters RLO (u+202e) and LRO (u+202d). See the
22184 function `get-char-code-property' for a way to inquire about
22185 the `bidi-class' property of a character. */)
22186 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22188 struct buffer *buf = current_buffer;
22189 struct buffer *old = buf;
22190 struct window *w = NULL;
22191 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22192 struct bidi_it itb;
22193 ptrdiff_t from_pos, to_pos, from_bpos;
22194 void *itb_data;
22196 if (!NILP (object))
22198 if (BUFFERP (object))
22199 buf = XBUFFER (object);
22200 else if (WINDOWP (object))
22202 w = decode_live_window (object);
22203 buf = XBUFFER (w->contents);
22204 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22206 else
22207 CHECK_STRING (object);
22210 if (STRINGP (object))
22212 /* Characters in unibyte strings are always treated by bidi.c as
22213 strong LTR. */
22214 if (!STRING_MULTIBYTE (object)
22215 /* When we are loading loadup.el, the character property
22216 tables needed for bidi iteration are not yet
22217 available. */
22218 || redisplay__inhibit_bidi)
22219 return Qnil;
22221 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22222 if (from_pos >= SCHARS (object))
22223 return Qnil;
22225 /* Set up the bidi iterator. */
22226 itb_data = bidi_shelve_cache ();
22227 itb.paragraph_dir = NEUTRAL_DIR;
22228 itb.string.lstring = object;
22229 itb.string.s = NULL;
22230 itb.string.schars = SCHARS (object);
22231 itb.string.bufpos = 0;
22232 itb.string.from_disp_str = false;
22233 itb.string.unibyte = false;
22234 itb.w = w;
22235 bidi_init_it (0, 0, frame_window_p, &itb);
22237 else
22239 /* Nothing this fancy can happen in unibyte buffers, or in a
22240 buffer that disabled reordering, or if FROM is at EOB. */
22241 if (NILP (BVAR (buf, bidi_display_reordering))
22242 || NILP (BVAR (buf, enable_multibyte_characters))
22243 /* When we are loading loadup.el, the character property
22244 tables needed for bidi iteration are not yet
22245 available. */
22246 || redisplay__inhibit_bidi)
22247 return Qnil;
22249 set_buffer_temp (buf);
22250 validate_region (&from, &to);
22251 from_pos = XINT (from);
22252 to_pos = XINT (to);
22253 if (from_pos >= ZV)
22254 return Qnil;
22256 /* Set up the bidi iterator. */
22257 itb_data = bidi_shelve_cache ();
22258 from_bpos = CHAR_TO_BYTE (from_pos);
22259 if (from_pos == BEGV)
22261 itb.charpos = BEGV;
22262 itb.bytepos = BEGV_BYTE;
22264 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22266 itb.charpos = from_pos;
22267 itb.bytepos = from_bpos;
22269 else
22270 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22271 -1, &itb.bytepos);
22272 itb.paragraph_dir = NEUTRAL_DIR;
22273 itb.string.s = NULL;
22274 itb.string.lstring = Qnil;
22275 itb.string.bufpos = 0;
22276 itb.string.from_disp_str = false;
22277 itb.string.unibyte = false;
22278 itb.w = w;
22279 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22282 ptrdiff_t found;
22283 do {
22284 /* For the purposes of this function, the actual base direction of
22285 the paragraph doesn't matter, so just set it to L2R. */
22286 bidi_paragraph_init (L2R, &itb, false);
22287 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22289 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22291 bidi_unshelve_cache (itb_data, false);
22292 set_buffer_temp (old);
22294 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22297 DEFUN ("move-point-visually", Fmove_point_visually,
22298 Smove_point_visually, 1, 1, 0,
22299 doc: /* Move point in the visual order in the specified DIRECTION.
22300 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22301 left.
22303 Value is the new character position of point. */)
22304 (Lisp_Object direction)
22306 struct window *w = XWINDOW (selected_window);
22307 struct buffer *b = XBUFFER (w->contents);
22308 struct glyph_row *row;
22309 int dir;
22310 Lisp_Object paragraph_dir;
22312 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22313 (!(ROW)->continued_p \
22314 && NILP ((GLYPH)->object) \
22315 && (GLYPH)->type == CHAR_GLYPH \
22316 && (GLYPH)->u.ch == ' ' \
22317 && (GLYPH)->charpos >= 0 \
22318 && !(GLYPH)->avoid_cursor_p)
22320 CHECK_NUMBER (direction);
22321 dir = XINT (direction);
22322 if (dir > 0)
22323 dir = 1;
22324 else
22325 dir = -1;
22327 /* If current matrix is up-to-date, we can use the information
22328 recorded in the glyphs, at least as long as the goal is on the
22329 screen. */
22330 if (w->window_end_valid
22331 && !windows_or_buffers_changed
22332 && b
22333 && !b->clip_changed
22334 && !b->prevent_redisplay_optimizations_p
22335 && !window_outdated (w)
22336 /* We rely below on the cursor coordinates to be up to date, but
22337 we cannot trust them if some command moved point since the
22338 last complete redisplay. */
22339 && w->last_point == BUF_PT (b)
22340 && w->cursor.vpos >= 0
22341 && w->cursor.vpos < w->current_matrix->nrows
22342 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22344 struct glyph *g = row->glyphs[TEXT_AREA];
22345 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22346 struct glyph *gpt = g + w->cursor.hpos;
22348 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22350 if (BUFFERP (g->object) && g->charpos != PT)
22352 SET_PT (g->charpos);
22353 w->cursor.vpos = -1;
22354 return make_number (PT);
22356 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22358 ptrdiff_t new_pos;
22360 if (BUFFERP (gpt->object))
22362 new_pos = PT;
22363 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22364 new_pos += (row->reversed_p ? -dir : dir);
22365 else
22366 new_pos -= (row->reversed_p ? -dir : dir);
22368 else if (BUFFERP (g->object))
22369 new_pos = g->charpos;
22370 else
22371 break;
22372 SET_PT (new_pos);
22373 w->cursor.vpos = -1;
22374 return make_number (PT);
22376 else if (ROW_GLYPH_NEWLINE_P (row, g))
22378 /* Glyphs inserted at the end of a non-empty line for
22379 positioning the cursor have zero charpos, so we must
22380 deduce the value of point by other means. */
22381 if (g->charpos > 0)
22382 SET_PT (g->charpos);
22383 else if (row->ends_at_zv_p && PT != ZV)
22384 SET_PT (ZV);
22385 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22386 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22387 else
22388 break;
22389 w->cursor.vpos = -1;
22390 return make_number (PT);
22393 if (g == e || NILP (g->object))
22395 if (row->truncated_on_left_p || row->truncated_on_right_p)
22396 goto simulate_display;
22397 if (!row->reversed_p)
22398 row += dir;
22399 else
22400 row -= dir;
22401 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22402 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22403 goto simulate_display;
22405 if (dir > 0)
22407 if (row->reversed_p && !row->continued_p)
22409 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22410 w->cursor.vpos = -1;
22411 return make_number (PT);
22413 g = row->glyphs[TEXT_AREA];
22414 e = g + row->used[TEXT_AREA];
22415 for ( ; g < e; g++)
22417 if (BUFFERP (g->object)
22418 /* Empty lines have only one glyph, which stands
22419 for the newline, and whose charpos is the
22420 buffer position of the newline. */
22421 || ROW_GLYPH_NEWLINE_P (row, g)
22422 /* When the buffer ends in a newline, the line at
22423 EOB also has one glyph, but its charpos is -1. */
22424 || (row->ends_at_zv_p
22425 && !row->reversed_p
22426 && NILP (g->object)
22427 && g->type == CHAR_GLYPH
22428 && g->u.ch == ' '))
22430 if (g->charpos > 0)
22431 SET_PT (g->charpos);
22432 else if (!row->reversed_p
22433 && row->ends_at_zv_p
22434 && PT != ZV)
22435 SET_PT (ZV);
22436 else
22437 continue;
22438 w->cursor.vpos = -1;
22439 return make_number (PT);
22443 else
22445 if (!row->reversed_p && !row->continued_p)
22447 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22448 w->cursor.vpos = -1;
22449 return make_number (PT);
22451 e = row->glyphs[TEXT_AREA];
22452 g = e + row->used[TEXT_AREA] - 1;
22453 for ( ; g >= e; g--)
22455 if (BUFFERP (g->object)
22456 || (ROW_GLYPH_NEWLINE_P (row, g)
22457 && g->charpos > 0)
22458 /* Empty R2L lines on GUI frames have the buffer
22459 position of the newline stored in the stretch
22460 glyph. */
22461 || g->type == STRETCH_GLYPH
22462 || (row->ends_at_zv_p
22463 && row->reversed_p
22464 && NILP (g->object)
22465 && g->type == CHAR_GLYPH
22466 && g->u.ch == ' '))
22468 if (g->charpos > 0)
22469 SET_PT (g->charpos);
22470 else if (row->reversed_p
22471 && row->ends_at_zv_p
22472 && PT != ZV)
22473 SET_PT (ZV);
22474 else
22475 continue;
22476 w->cursor.vpos = -1;
22477 return make_number (PT);
22484 simulate_display:
22486 /* If we wind up here, we failed to move by using the glyphs, so we
22487 need to simulate display instead. */
22489 if (b)
22490 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22491 else
22492 paragraph_dir = Qleft_to_right;
22493 if (EQ (paragraph_dir, Qright_to_left))
22494 dir = -dir;
22495 if (PT <= BEGV && dir < 0)
22496 xsignal0 (Qbeginning_of_buffer);
22497 else if (PT >= ZV && dir > 0)
22498 xsignal0 (Qend_of_buffer);
22499 else
22501 struct text_pos pt;
22502 struct it it;
22503 int pt_x, target_x, pixel_width, pt_vpos;
22504 bool at_eol_p;
22505 bool overshoot_expected = false;
22506 bool target_is_eol_p = false;
22508 /* Setup the arena. */
22509 SET_TEXT_POS (pt, PT, PT_BYTE);
22510 start_display (&it, w, pt);
22511 /* When lines are truncated, we could be called with point
22512 outside of the windows edges, in which case move_it_*
22513 functions either prematurely stop at window's edge or jump to
22514 the next screen line, whereas we rely below on our ability to
22515 reach point, in order to start from its X coordinate. So we
22516 need to disregard the window's horizontal extent in that case. */
22517 if (it.line_wrap == TRUNCATE)
22518 it.last_visible_x = DISP_INFINITY;
22520 if (it.cmp_it.id < 0
22521 && it.method == GET_FROM_STRING
22522 && it.area == TEXT_AREA
22523 && it.string_from_display_prop_p
22524 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22525 overshoot_expected = true;
22527 /* Find the X coordinate of point. We start from the beginning
22528 of this or previous line to make sure we are before point in
22529 the logical order (since the move_it_* functions can only
22530 move forward). */
22531 reseat:
22532 reseat_at_previous_visible_line_start (&it);
22533 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22534 if (IT_CHARPOS (it) != PT)
22536 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22537 -1, -1, -1, MOVE_TO_POS);
22538 /* If we missed point because the character there is
22539 displayed out of a display vector that has more than one
22540 glyph, retry expecting overshoot. */
22541 if (it.method == GET_FROM_DISPLAY_VECTOR
22542 && it.current.dpvec_index > 0
22543 && !overshoot_expected)
22545 overshoot_expected = true;
22546 goto reseat;
22548 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22549 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22551 pt_x = it.current_x;
22552 pt_vpos = it.vpos;
22553 if (dir > 0 || overshoot_expected)
22555 struct glyph_row *row = it.glyph_row;
22557 /* When point is at beginning of line, we don't have
22558 information about the glyph there loaded into struct
22559 it. Calling get_next_display_element fixes that. */
22560 if (pt_x == 0)
22561 get_next_display_element (&it);
22562 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22563 it.glyph_row = NULL;
22564 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22565 it.glyph_row = row;
22566 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22567 it, lest it will become out of sync with it's buffer
22568 position. */
22569 it.current_x = pt_x;
22571 else
22572 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22573 pixel_width = it.pixel_width;
22574 if (overshoot_expected && at_eol_p)
22575 pixel_width = 0;
22576 else if (pixel_width <= 0)
22577 pixel_width = 1;
22579 /* If there's a display string (or something similar) at point,
22580 we are actually at the glyph to the left of point, so we need
22581 to correct the X coordinate. */
22582 if (overshoot_expected)
22584 if (it.bidi_p)
22585 pt_x += pixel_width * it.bidi_it.scan_dir;
22586 else
22587 pt_x += pixel_width;
22590 /* Compute target X coordinate, either to the left or to the
22591 right of point. On TTY frames, all characters have the same
22592 pixel width of 1, so we can use that. On GUI frames we don't
22593 have an easy way of getting at the pixel width of the
22594 character to the left of point, so we use a different method
22595 of getting to that place. */
22596 if (dir > 0)
22597 target_x = pt_x + pixel_width;
22598 else
22599 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22601 /* Target X coordinate could be one line above or below the line
22602 of point, in which case we need to adjust the target X
22603 coordinate. Also, if moving to the left, we need to begin at
22604 the left edge of the point's screen line. */
22605 if (dir < 0)
22607 if (pt_x > 0)
22609 start_display (&it, w, pt);
22610 if (it.line_wrap == TRUNCATE)
22611 it.last_visible_x = DISP_INFINITY;
22612 reseat_at_previous_visible_line_start (&it);
22613 it.current_x = it.current_y = it.hpos = 0;
22614 if (pt_vpos != 0)
22615 move_it_by_lines (&it, pt_vpos);
22617 else
22619 move_it_by_lines (&it, -1);
22620 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22621 target_is_eol_p = true;
22622 /* Under word-wrap, we don't know the x coordinate of
22623 the last character displayed on the previous line,
22624 which immediately precedes the wrap point. To find
22625 out its x coordinate, we try moving to the right
22626 margin of the window, which will stop at the wrap
22627 point, and then reset target_x to point at the
22628 character that precedes the wrap point. This is not
22629 needed on GUI frames, because (see below) there we
22630 move from the left margin one grapheme cluster at a
22631 time, and stop when we hit the wrap point. */
22632 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22634 void *it_data = NULL;
22635 struct it it2;
22637 SAVE_IT (it2, it, it_data);
22638 move_it_in_display_line_to (&it, ZV, target_x,
22639 MOVE_TO_POS | MOVE_TO_X);
22640 /* If we arrived at target_x, that _is_ the last
22641 character on the previous line. */
22642 if (it.current_x != target_x)
22643 target_x = it.current_x - 1;
22644 RESTORE_IT (&it, &it2, it_data);
22648 else
22650 if (at_eol_p
22651 || (target_x >= it.last_visible_x
22652 && it.line_wrap != TRUNCATE))
22654 if (pt_x > 0)
22655 move_it_by_lines (&it, 0);
22656 move_it_by_lines (&it, 1);
22657 target_x = 0;
22661 /* Move to the target X coordinate. */
22662 /* On GUI frames, as we don't know the X coordinate of the
22663 character to the left of point, moving point to the left
22664 requires walking, one grapheme cluster at a time, until we
22665 find ourself at a place immediately to the left of the
22666 character at point. */
22667 if (FRAME_WINDOW_P (it.f) && dir < 0)
22669 struct text_pos new_pos;
22670 enum move_it_result rc = MOVE_X_REACHED;
22672 if (it.current_x == 0)
22673 get_next_display_element (&it);
22674 if (it.what == IT_COMPOSITION)
22676 new_pos.charpos = it.cmp_it.charpos;
22677 new_pos.bytepos = -1;
22679 else
22680 new_pos = it.current.pos;
22682 while (it.current_x + it.pixel_width <= target_x
22683 && (rc == MOVE_X_REACHED
22684 /* Under word-wrap, move_it_in_display_line_to
22685 stops at correct coordinates, but sometimes
22686 returns MOVE_POS_MATCH_OR_ZV. */
22687 || (it.line_wrap == WORD_WRAP
22688 && rc == MOVE_POS_MATCH_OR_ZV)))
22690 int new_x = it.current_x + it.pixel_width;
22692 /* For composed characters, we want the position of the
22693 first character in the grapheme cluster (usually, the
22694 composition's base character), whereas it.current
22695 might give us the position of the _last_ one, e.g. if
22696 the composition is rendered in reverse due to bidi
22697 reordering. */
22698 if (it.what == IT_COMPOSITION)
22700 new_pos.charpos = it.cmp_it.charpos;
22701 new_pos.bytepos = -1;
22703 else
22704 new_pos = it.current.pos;
22705 if (new_x == it.current_x)
22706 new_x++;
22707 rc = move_it_in_display_line_to (&it, ZV, new_x,
22708 MOVE_TO_POS | MOVE_TO_X);
22709 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22710 break;
22712 /* The previous position we saw in the loop is the one we
22713 want. */
22714 if (new_pos.bytepos == -1)
22715 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22716 it.current.pos = new_pos;
22718 else if (it.current_x != target_x)
22719 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22721 /* If we ended up in a display string that covers point, move to
22722 buffer position to the right in the visual order. */
22723 if (dir > 0)
22725 while (IT_CHARPOS (it) == PT)
22727 set_iterator_to_next (&it, false);
22728 if (!get_next_display_element (&it))
22729 break;
22733 /* Move point to that position. */
22734 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22737 return make_number (PT);
22739 #undef ROW_GLYPH_NEWLINE_P
22742 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22743 Sbidi_resolved_levels, 0, 1, 0,
22744 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22746 The resolved levels are produced by the Emacs bidi reordering engine
22747 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22748 read the Unicode Standard Annex 9 (UAX#9) for background information
22749 about these levels.
22751 VPOS is the zero-based number of the current window's screen line
22752 for which to produce the resolved levels. If VPOS is nil or omitted,
22753 it defaults to the screen line of point. If the window displays a
22754 header line, VPOS of zero will report on the header line, and first
22755 line of text in the window will have VPOS of 1.
22757 Value is an array of resolved levels, indexed by glyph number.
22758 Glyphs are numbered from zero starting from the beginning of the
22759 screen line, i.e. the left edge of the window for left-to-right lines
22760 and from the right edge for right-to-left lines. The resolved levels
22761 are produced only for the window's text area; text in display margins
22762 is not included.
22764 If the selected window's display is not up-to-date, or if the specified
22765 screen line does not display text, this function returns nil. It is
22766 highly recommended to bind this function to some simple key, like F8,
22767 in order to avoid these problems.
22769 This function exists mainly for testing the correctness of the
22770 Emacs UBA implementation, in particular with the test suite. */)
22771 (Lisp_Object vpos)
22773 struct window *w = XWINDOW (selected_window);
22774 struct buffer *b = XBUFFER (w->contents);
22775 int nrow;
22776 struct glyph_row *row;
22778 if (NILP (vpos))
22780 int d1, d2, d3, d4, d5;
22782 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22784 else
22786 CHECK_NUMBER_COERCE_MARKER (vpos);
22787 nrow = XINT (vpos);
22790 /* We require up-to-date glyph matrix for this window. */
22791 if (w->window_end_valid
22792 && !windows_or_buffers_changed
22793 && b
22794 && !b->clip_changed
22795 && !b->prevent_redisplay_optimizations_p
22796 && !window_outdated (w)
22797 && nrow >= 0
22798 && nrow < w->current_matrix->nrows
22799 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22800 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22802 struct glyph *g, *e, *g1;
22803 int nglyphs, i;
22804 Lisp_Object levels;
22806 if (!row->reversed_p) /* Left-to-right glyph row. */
22808 g = g1 = row->glyphs[TEXT_AREA];
22809 e = g + row->used[TEXT_AREA];
22811 /* Skip over glyphs at the start of the row that was
22812 generated by redisplay for its own needs. */
22813 while (g < e
22814 && NILP (g->object)
22815 && g->charpos < 0)
22816 g++;
22817 g1 = g;
22819 /* Count the "interesting" glyphs in this row. */
22820 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22821 nglyphs++;
22823 /* Create and fill the array. */
22824 levels = make_uninit_vector (nglyphs);
22825 for (i = 0; g1 < g; i++, g1++)
22826 ASET (levels, i, make_number (g1->resolved_level));
22828 else /* Right-to-left glyph row. */
22830 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22831 e = row->glyphs[TEXT_AREA] - 1;
22832 while (g > e
22833 && NILP (g->object)
22834 && g->charpos < 0)
22835 g--;
22836 g1 = g;
22837 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22838 nglyphs++;
22839 levels = make_uninit_vector (nglyphs);
22840 for (i = 0; g1 > g; i++, g1--)
22841 ASET (levels, i, make_number (g1->resolved_level));
22843 return levels;
22845 else
22846 return Qnil;
22851 /***********************************************************************
22852 Menu Bar
22853 ***********************************************************************/
22855 /* Redisplay the menu bar in the frame for window W.
22857 The menu bar of X frames that don't have X toolkit support is
22858 displayed in a special window W->frame->menu_bar_window.
22860 The menu bar of terminal frames is treated specially as far as
22861 glyph matrices are concerned. Menu bar lines are not part of
22862 windows, so the update is done directly on the frame matrix rows
22863 for the menu bar. */
22865 static void
22866 display_menu_bar (struct window *w)
22868 struct frame *f = XFRAME (WINDOW_FRAME (w));
22869 struct it it;
22870 Lisp_Object items;
22871 int i;
22873 /* Don't do all this for graphical frames. */
22874 #ifdef HAVE_NTGUI
22875 if (FRAME_W32_P (f))
22876 return;
22877 #endif
22878 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22879 if (FRAME_X_P (f))
22880 return;
22881 #endif
22883 #ifdef HAVE_NS
22884 if (FRAME_NS_P (f))
22885 return;
22886 #endif /* HAVE_NS */
22888 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22889 eassert (!FRAME_WINDOW_P (f));
22890 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22891 it.first_visible_x = 0;
22892 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22893 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22894 if (FRAME_WINDOW_P (f))
22896 /* Menu bar lines are displayed in the desired matrix of the
22897 dummy window menu_bar_window. */
22898 struct window *menu_w;
22899 menu_w = XWINDOW (f->menu_bar_window);
22900 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22901 MENU_FACE_ID);
22902 it.first_visible_x = 0;
22903 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22905 else
22906 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22908 /* This is a TTY frame, i.e. character hpos/vpos are used as
22909 pixel x/y. */
22910 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22911 MENU_FACE_ID);
22912 it.first_visible_x = 0;
22913 it.last_visible_x = FRAME_COLS (f);
22916 /* FIXME: This should be controlled by a user option. See the
22917 comments in redisplay_tool_bar and display_mode_line about
22918 this. */
22919 it.paragraph_embedding = L2R;
22921 /* Clear all rows of the menu bar. */
22922 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22924 struct glyph_row *row = it.glyph_row + i;
22925 clear_glyph_row (row);
22926 row->enabled_p = true;
22927 row->full_width_p = true;
22928 row->reversed_p = false;
22931 /* Display all items of the menu bar. */
22932 items = FRAME_MENU_BAR_ITEMS (it.f);
22933 for (i = 0; i < ASIZE (items); i += 4)
22935 Lisp_Object string;
22937 /* Stop at nil string. */
22938 string = AREF (items, i + 1);
22939 if (NILP (string))
22940 break;
22942 /* Remember where item was displayed. */
22943 ASET (items, i + 3, make_number (it.hpos));
22945 /* Display the item, pad with one space. */
22946 if (it.current_x < it.last_visible_x)
22947 display_string (NULL, string, Qnil, 0, 0, &it,
22948 SCHARS (string) + 1, 0, 0, -1);
22951 /* Fill out the line with spaces. */
22952 if (it.current_x < it.last_visible_x)
22953 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22955 /* Compute the total height of the lines. */
22956 compute_line_metrics (&it);
22959 /* Deep copy of a glyph row, including the glyphs. */
22960 static void
22961 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22963 struct glyph *pointers[1 + LAST_AREA];
22964 int to_used = to->used[TEXT_AREA];
22966 /* Save glyph pointers of TO. */
22967 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22969 /* Do a structure assignment. */
22970 *to = *from;
22972 /* Restore original glyph pointers of TO. */
22973 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22975 /* Copy the glyphs. */
22976 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22977 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22979 /* If we filled only part of the TO row, fill the rest with
22980 space_glyph (which will display as empty space). */
22981 if (to_used > from->used[TEXT_AREA])
22982 fill_up_frame_row_with_spaces (to, to_used);
22985 /* Display one menu item on a TTY, by overwriting the glyphs in the
22986 frame F's desired glyph matrix with glyphs produced from the menu
22987 item text. Called from term.c to display TTY drop-down menus one
22988 item at a time.
22990 ITEM_TEXT is the menu item text as a C string.
22992 FACE_ID is the face ID to be used for this menu item. FACE_ID
22993 could specify one of 3 faces: a face for an enabled item, a face
22994 for a disabled item, or a face for a selected item.
22996 X and Y are coordinates of the first glyph in the frame's desired
22997 matrix to be overwritten by the menu item. Since this is a TTY, Y
22998 is the zero-based number of the glyph row and X is the zero-based
22999 glyph number in the row, starting from left, where to start
23000 displaying the item.
23002 SUBMENU means this menu item drops down a submenu, which
23003 should be indicated by displaying a proper visual cue after the
23004 item text. */
23006 void
23007 display_tty_menu_item (const char *item_text, int width, int face_id,
23008 int x, int y, bool submenu)
23010 struct it it;
23011 struct frame *f = SELECTED_FRAME ();
23012 struct window *w = XWINDOW (f->selected_window);
23013 struct glyph_row *row;
23014 size_t item_len = strlen (item_text);
23016 eassert (FRAME_TERMCAP_P (f));
23018 /* Don't write beyond the matrix's last row. This can happen for
23019 TTY screens that are not high enough to show the entire menu.
23020 (This is actually a bit of defensive programming, as
23021 tty_menu_display already limits the number of menu items to one
23022 less than the number of screen lines.) */
23023 if (y >= f->desired_matrix->nrows)
23024 return;
23026 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23027 it.first_visible_x = 0;
23028 it.last_visible_x = FRAME_COLS (f) - 1;
23029 row = it.glyph_row;
23030 /* Start with the row contents from the current matrix. */
23031 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23032 bool saved_width = row->full_width_p;
23033 row->full_width_p = true;
23034 bool saved_reversed = row->reversed_p;
23035 row->reversed_p = false;
23036 row->enabled_p = true;
23038 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23039 desired face. */
23040 eassert (x < f->desired_matrix->matrix_w);
23041 it.current_x = it.hpos = x;
23042 it.current_y = it.vpos = y;
23043 int saved_used = row->used[TEXT_AREA];
23044 bool saved_truncated = row->truncated_on_right_p;
23045 row->used[TEXT_AREA] = x;
23046 it.face_id = face_id;
23047 it.line_wrap = TRUNCATE;
23049 /* FIXME: This should be controlled by a user option. See the
23050 comments in redisplay_tool_bar and display_mode_line about this.
23051 Also, if paragraph_embedding could ever be R2L, changes will be
23052 needed to avoid shifting to the right the row characters in
23053 term.c:append_glyph. */
23054 it.paragraph_embedding = L2R;
23056 /* Pad with a space on the left. */
23057 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23058 width--;
23059 /* Display the menu item, pad with spaces to WIDTH. */
23060 if (submenu)
23062 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23063 item_len, 0, FRAME_COLS (f) - 1, -1);
23064 width -= item_len;
23065 /* Indicate with " >" that there's a submenu. */
23066 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23067 FRAME_COLS (f) - 1, -1);
23069 else
23070 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23071 width, 0, FRAME_COLS (f) - 1, -1);
23073 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23074 row->truncated_on_right_p = saved_truncated;
23075 row->hash = row_hash (row);
23076 row->full_width_p = saved_width;
23077 row->reversed_p = saved_reversed;
23080 /***********************************************************************
23081 Mode Line
23082 ***********************************************************************/
23084 /* Redisplay mode lines in the window tree whose root is WINDOW.
23085 If FORCE, redisplay mode lines unconditionally.
23086 Otherwise, redisplay only mode lines that are garbaged. Value is
23087 the number of windows whose mode lines were redisplayed. */
23089 static int
23090 redisplay_mode_lines (Lisp_Object window, bool force)
23092 int nwindows = 0;
23094 while (!NILP (window))
23096 struct window *w = XWINDOW (window);
23098 if (WINDOWP (w->contents))
23099 nwindows += redisplay_mode_lines (w->contents, force);
23100 else if (force
23101 || FRAME_GARBAGED_P (XFRAME (w->frame))
23102 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23104 struct text_pos lpoint;
23105 struct buffer *old = current_buffer;
23107 /* Set the window's buffer for the mode line display. */
23108 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23109 set_buffer_internal_1 (XBUFFER (w->contents));
23111 /* Point refers normally to the selected window. For any
23112 other window, set up appropriate value. */
23113 if (!EQ (window, selected_window))
23115 struct text_pos pt;
23117 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23118 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23121 /* Display mode lines. */
23122 clear_glyph_matrix (w->desired_matrix);
23123 if (display_mode_lines (w))
23124 ++nwindows;
23126 /* Restore old settings. */
23127 set_buffer_internal_1 (old);
23128 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23131 window = w->next;
23134 return nwindows;
23138 /* Display the mode and/or header line of window W. Value is the
23139 sum number of mode lines and header lines displayed. */
23141 static int
23142 display_mode_lines (struct window *w)
23144 Lisp_Object old_selected_window = selected_window;
23145 Lisp_Object old_selected_frame = selected_frame;
23146 Lisp_Object new_frame = w->frame;
23147 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23148 int n = 0;
23150 selected_frame = new_frame;
23151 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23152 or window's point, then we'd need select_window_1 here as well. */
23153 XSETWINDOW (selected_window, w);
23154 XFRAME (new_frame)->selected_window = selected_window;
23156 /* These will be set while the mode line specs are processed. */
23157 line_number_displayed = false;
23158 w->column_number_displayed = -1;
23160 if (window_wants_mode_line (w))
23162 Lisp_Object window_mode_line_format
23163 = window_parameter (w, Qmode_line_format);
23165 struct window *sel_w = XWINDOW (old_selected_window);
23167 /* Select mode line face based on the real selected window. */
23168 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23169 NILP (window_mode_line_format)
23170 ? BVAR (current_buffer, mode_line_format)
23171 : window_mode_line_format);
23172 ++n;
23175 if (window_wants_header_line (w))
23177 Lisp_Object window_header_line_format
23178 = window_parameter (w, Qheader_line_format);
23180 display_mode_line (w, HEADER_LINE_FACE_ID,
23181 NILP (window_header_line_format)
23182 ? BVAR (current_buffer, header_line_format)
23183 : window_header_line_format);
23184 ++n;
23187 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23188 selected_frame = old_selected_frame;
23189 selected_window = old_selected_window;
23190 if (n > 0)
23191 w->must_be_updated_p = true;
23192 return n;
23196 /* Display mode or header line of window W. FACE_ID specifies which
23197 line to display; it is either MODE_LINE_FACE_ID or
23198 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23199 display. Value is the pixel height of the mode/header line
23200 displayed. */
23202 static int
23203 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23205 struct it it;
23206 struct face *face;
23207 ptrdiff_t count = SPECPDL_INDEX ();
23209 init_iterator (&it, w, -1, -1, NULL, face_id);
23210 /* Don't extend on a previously drawn mode-line.
23211 This may happen if called from pos_visible_p. */
23212 it.glyph_row->enabled_p = false;
23213 prepare_desired_row (w, it.glyph_row, true);
23215 it.glyph_row->mode_line_p = true;
23217 /* FIXME: This should be controlled by a user option. But
23218 supporting such an option is not trivial, since the mode line is
23219 made up of many separate strings. */
23220 it.paragraph_embedding = L2R;
23222 record_unwind_protect (unwind_format_mode_line,
23223 format_mode_line_unwind_data (NULL, NULL,
23224 Qnil, false));
23226 mode_line_target = MODE_LINE_DISPLAY;
23228 /* Temporarily make frame's keyboard the current kboard so that
23229 kboard-local variables in the mode_line_format will get the right
23230 values. */
23231 push_kboard (FRAME_KBOARD (it.f));
23232 record_unwind_save_match_data ();
23233 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23234 pop_kboard ();
23236 unbind_to (count, Qnil);
23238 /* Fill up with spaces. */
23239 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23241 compute_line_metrics (&it);
23242 it.glyph_row->full_width_p = true;
23243 it.glyph_row->continued_p = false;
23244 it.glyph_row->truncated_on_left_p = false;
23245 it.glyph_row->truncated_on_right_p = false;
23247 /* Make a 3D mode-line have a shadow at its right end. */
23248 face = FACE_FROM_ID (it.f, face_id);
23249 extend_face_to_end_of_line (&it);
23250 if (face->box != FACE_NO_BOX)
23252 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23253 + it.glyph_row->used[TEXT_AREA] - 1);
23254 last->right_box_line_p = true;
23257 return it.glyph_row->height;
23260 /* Move element ELT in LIST to the front of LIST.
23261 Return the updated list. */
23263 static Lisp_Object
23264 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23266 register Lisp_Object tail, prev;
23267 register Lisp_Object tem;
23269 tail = list;
23270 prev = Qnil;
23271 while (CONSP (tail))
23273 tem = XCAR (tail);
23275 if (EQ (elt, tem))
23277 /* Splice out the link TAIL. */
23278 if (NILP (prev))
23279 list = XCDR (tail);
23280 else
23281 Fsetcdr (prev, XCDR (tail));
23283 /* Now make it the first. */
23284 Fsetcdr (tail, list);
23285 return tail;
23287 else
23288 prev = tail;
23289 tail = XCDR (tail);
23290 maybe_quit ();
23293 /* Not found--return unchanged LIST. */
23294 return list;
23297 /* Contribute ELT to the mode line for window IT->w. How it
23298 translates into text depends on its data type.
23300 IT describes the display environment in which we display, as usual.
23302 DEPTH is the depth in recursion. It is used to prevent
23303 infinite recursion here.
23305 FIELD_WIDTH is the number of characters the display of ELT should
23306 occupy in the mode line, and PRECISION is the maximum number of
23307 characters to display from ELT's representation. See
23308 display_string for details.
23310 Returns the hpos of the end of the text generated by ELT.
23312 PROPS is a property list to add to any string we encounter.
23314 If RISKY, remove (disregard) any properties in any string
23315 we encounter, and ignore :eval and :propertize.
23317 The global variable `mode_line_target' determines whether the
23318 output is passed to `store_mode_line_noprop',
23319 `store_mode_line_string', or `display_string'. */
23321 static int
23322 display_mode_element (struct it *it, int depth, int field_width, int precision,
23323 Lisp_Object elt, Lisp_Object props, bool risky)
23325 int n = 0, field, prec;
23326 bool literal = false;
23328 tail_recurse:
23329 if (depth > 100)
23330 elt = build_string ("*too-deep*");
23332 depth++;
23334 switch (XTYPE (elt))
23336 case Lisp_String:
23338 /* A string: output it and check for %-constructs within it. */
23339 unsigned char c;
23340 ptrdiff_t offset = 0;
23342 if (SCHARS (elt) > 0
23343 && (!NILP (props) || risky))
23345 Lisp_Object oprops, aelt;
23346 oprops = Ftext_properties_at (make_number (0), elt);
23348 /* If the starting string's properties are not what
23349 we want, translate the string. Also, if the string
23350 is risky, do that anyway. */
23352 if (NILP (Fequal (props, oprops)) || risky)
23354 /* If the starting string has properties,
23355 merge the specified ones onto the existing ones. */
23356 if (! NILP (oprops) && !risky)
23358 Lisp_Object tem;
23360 oprops = Fcopy_sequence (oprops);
23361 tem = props;
23362 while (CONSP (tem))
23364 oprops = Fplist_put (oprops, XCAR (tem),
23365 XCAR (XCDR (tem)));
23366 tem = XCDR (XCDR (tem));
23368 props = oprops;
23371 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23372 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23374 /* AELT is what we want. Move it to the front
23375 without consing. */
23376 elt = XCAR (aelt);
23377 mode_line_proptrans_alist
23378 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23380 else
23382 Lisp_Object tem;
23384 /* If AELT has the wrong props, it is useless.
23385 so get rid of it. */
23386 if (! NILP (aelt))
23387 mode_line_proptrans_alist
23388 = Fdelq (aelt, mode_line_proptrans_alist);
23390 elt = Fcopy_sequence (elt);
23391 Fset_text_properties (make_number (0), Flength (elt),
23392 props, elt);
23393 /* Add this item to mode_line_proptrans_alist. */
23394 mode_line_proptrans_alist
23395 = Fcons (Fcons (elt, props),
23396 mode_line_proptrans_alist);
23397 /* Truncate mode_line_proptrans_alist
23398 to at most 50 elements. */
23399 tem = Fnthcdr (make_number (50),
23400 mode_line_proptrans_alist);
23401 if (! NILP (tem))
23402 XSETCDR (tem, Qnil);
23407 offset = 0;
23409 if (literal)
23411 prec = precision - n;
23412 switch (mode_line_target)
23414 case MODE_LINE_NOPROP:
23415 case MODE_LINE_TITLE:
23416 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23417 break;
23418 case MODE_LINE_STRING:
23419 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23420 break;
23421 case MODE_LINE_DISPLAY:
23422 n += display_string (NULL, elt, Qnil, 0, 0, it,
23423 0, prec, 0, STRING_MULTIBYTE (elt));
23424 break;
23427 break;
23430 /* Handle the non-literal case. */
23432 while ((precision <= 0 || n < precision)
23433 && SREF (elt, offset) != 0
23434 && (mode_line_target != MODE_LINE_DISPLAY
23435 || it->current_x < it->last_visible_x))
23437 ptrdiff_t last_offset = offset;
23439 /* Advance to end of string or next format specifier. */
23440 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23443 if (offset - 1 != last_offset)
23445 ptrdiff_t nchars, nbytes;
23447 /* Output to end of string or up to '%'. Field width
23448 is length of string. Don't output more than
23449 PRECISION allows us. */
23450 offset--;
23452 prec = c_string_width (SDATA (elt) + last_offset,
23453 offset - last_offset, precision - n,
23454 &nchars, &nbytes);
23456 switch (mode_line_target)
23458 case MODE_LINE_NOPROP:
23459 case MODE_LINE_TITLE:
23460 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23461 break;
23462 case MODE_LINE_STRING:
23464 ptrdiff_t bytepos = last_offset;
23465 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23466 ptrdiff_t endpos = (precision <= 0
23467 ? string_byte_to_char (elt, offset)
23468 : charpos + nchars);
23469 Lisp_Object mode_string
23470 = Fsubstring (elt, make_number (charpos),
23471 make_number (endpos));
23472 n += store_mode_line_string (NULL, mode_string, false,
23473 0, 0, Qnil);
23475 break;
23476 case MODE_LINE_DISPLAY:
23478 ptrdiff_t bytepos = last_offset;
23479 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23481 if (precision <= 0)
23482 nchars = string_byte_to_char (elt, offset) - charpos;
23483 n += display_string (NULL, elt, Qnil, 0, charpos,
23484 it, 0, nchars, 0,
23485 STRING_MULTIBYTE (elt));
23487 break;
23490 else /* c == '%' */
23492 ptrdiff_t percent_position = offset;
23494 /* Get the specified minimum width. Zero means
23495 don't pad. */
23496 field = 0;
23497 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23498 field = field * 10 + c - '0';
23500 /* Don't pad beyond the total padding allowed. */
23501 if (field_width - n > 0 && field > field_width - n)
23502 field = field_width - n;
23504 /* Note that either PRECISION <= 0 or N < PRECISION. */
23505 prec = precision - n;
23507 if (c == 'M')
23508 n += display_mode_element (it, depth, field, prec,
23509 Vglobal_mode_string, props,
23510 risky);
23511 else if (c != 0)
23513 bool multibyte;
23514 ptrdiff_t bytepos, charpos;
23515 const char *spec;
23516 Lisp_Object string;
23518 bytepos = percent_position;
23519 charpos = (STRING_MULTIBYTE (elt)
23520 ? string_byte_to_char (elt, bytepos)
23521 : bytepos);
23522 spec = decode_mode_spec (it->w, c, field, &string);
23523 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23525 switch (mode_line_target)
23527 case MODE_LINE_NOPROP:
23528 case MODE_LINE_TITLE:
23529 n += store_mode_line_noprop (spec, field, prec);
23530 break;
23531 case MODE_LINE_STRING:
23533 Lisp_Object tem = build_string (spec);
23534 props = Ftext_properties_at (make_number (charpos), elt);
23535 /* Should only keep face property in props */
23536 n += store_mode_line_string (NULL, tem, false,
23537 field, prec, props);
23539 break;
23540 case MODE_LINE_DISPLAY:
23542 int nglyphs_before, nwritten;
23544 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23545 nwritten = display_string (spec, string, elt,
23546 charpos, 0, it,
23547 field, prec, 0,
23548 multibyte);
23550 /* Assign to the glyphs written above the
23551 string where the `%x' came from, position
23552 of the `%'. */
23553 if (nwritten > 0)
23555 struct glyph *glyph
23556 = (it->glyph_row->glyphs[TEXT_AREA]
23557 + nglyphs_before);
23558 int i;
23560 for (i = 0; i < nwritten; ++i)
23562 glyph[i].object = elt;
23563 glyph[i].charpos = charpos;
23566 n += nwritten;
23569 break;
23572 else /* c == 0 */
23573 break;
23577 break;
23579 case Lisp_Symbol:
23580 /* A symbol: process the value of the symbol recursively
23581 as if it appeared here directly. Avoid error if symbol void.
23582 Special case: if value of symbol is a string, output the string
23583 literally. */
23585 register Lisp_Object tem;
23587 /* If the variable is not marked as risky to set
23588 then its contents are risky to use. */
23589 if (NILP (Fget (elt, Qrisky_local_variable)))
23590 risky = true;
23592 tem = Fboundp (elt);
23593 if (!NILP (tem))
23595 tem = Fsymbol_value (elt);
23596 /* If value is a string, output that string literally:
23597 don't check for % within it. */
23598 if (STRINGP (tem))
23599 literal = true;
23601 if (!EQ (tem, elt))
23603 /* Give up right away for nil or t. */
23604 elt = tem;
23605 goto tail_recurse;
23609 break;
23611 case Lisp_Cons:
23613 register Lisp_Object car, tem;
23615 /* A cons cell: five distinct cases.
23616 If first element is :eval or :propertize, do something special.
23617 If first element is a string or a cons, process all the elements
23618 and effectively concatenate them.
23619 If first element is a negative number, truncate displaying cdr to
23620 at most that many characters. If positive, pad (with spaces)
23621 to at least that many characters.
23622 If first element is a symbol, process the cadr or caddr recursively
23623 according to whether the symbol's value is non-nil or nil. */
23624 car = XCAR (elt);
23625 if (EQ (car, QCeval))
23627 /* An element of the form (:eval FORM) means evaluate FORM
23628 and use the result as mode line elements. */
23630 if (risky)
23631 break;
23633 if (CONSP (XCDR (elt)))
23635 Lisp_Object spec;
23636 spec = safe__eval (true, XCAR (XCDR (elt)));
23637 n += display_mode_element (it, depth, field_width - n,
23638 precision - n, spec, props,
23639 risky);
23642 else if (EQ (car, QCpropertize))
23644 /* An element of the form (:propertize ELT PROPS...)
23645 means display ELT but applying properties PROPS. */
23647 if (risky)
23648 break;
23650 if (CONSP (XCDR (elt)))
23651 n += display_mode_element (it, depth, field_width - n,
23652 precision - n, XCAR (XCDR (elt)),
23653 XCDR (XCDR (elt)), risky);
23655 else if (SYMBOLP (car))
23657 tem = Fboundp (car);
23658 elt = XCDR (elt);
23659 if (!CONSP (elt))
23660 goto invalid;
23661 /* elt is now the cdr, and we know it is a cons cell.
23662 Use its car if CAR has a non-nil value. */
23663 if (!NILP (tem))
23665 tem = Fsymbol_value (car);
23666 if (!NILP (tem))
23668 elt = XCAR (elt);
23669 goto tail_recurse;
23672 /* Symbol's value is nil (or symbol is unbound)
23673 Get the cddr of the original list
23674 and if possible find the caddr and use that. */
23675 elt = XCDR (elt);
23676 if (NILP (elt))
23677 break;
23678 else if (!CONSP (elt))
23679 goto invalid;
23680 elt = XCAR (elt);
23681 goto tail_recurse;
23683 else if (INTEGERP (car))
23685 register int lim = XINT (car);
23686 elt = XCDR (elt);
23687 if (lim < 0)
23689 /* Negative int means reduce maximum width. */
23690 if (precision <= 0)
23691 precision = -lim;
23692 else
23693 precision = min (precision, -lim);
23695 else if (lim > 0)
23697 /* Padding specified. Don't let it be more than
23698 current maximum. */
23699 if (precision > 0)
23700 lim = min (precision, lim);
23702 /* If that's more padding than already wanted, queue it.
23703 But don't reduce padding already specified even if
23704 that is beyond the current truncation point. */
23705 field_width = max (lim, field_width);
23707 goto tail_recurse;
23709 else if (STRINGP (car) || CONSP (car))
23710 FOR_EACH_TAIL_SAFE (elt)
23712 if (0 < precision && precision <= n)
23713 break;
23714 n += display_mode_element (it, depth,
23715 /* Pad after only the last
23716 list element. */
23717 (! CONSP (XCDR (elt))
23718 ? field_width - n
23719 : 0),
23720 precision - n, XCAR (elt),
23721 props, risky);
23724 break;
23726 default:
23727 invalid:
23728 elt = build_string ("*invalid*");
23729 goto tail_recurse;
23732 /* Pad to FIELD_WIDTH. */
23733 if (field_width > 0 && n < field_width)
23735 switch (mode_line_target)
23737 case MODE_LINE_NOPROP:
23738 case MODE_LINE_TITLE:
23739 n += store_mode_line_noprop ("", field_width - n, 0);
23740 break;
23741 case MODE_LINE_STRING:
23742 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23743 Qnil);
23744 break;
23745 case MODE_LINE_DISPLAY:
23746 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23747 0, 0, 0);
23748 break;
23752 return n;
23755 /* Store a mode-line string element in mode_line_string_list.
23757 If STRING is non-null, display that C string. Otherwise, the Lisp
23758 string LISP_STRING is displayed.
23760 FIELD_WIDTH is the minimum number of output glyphs to produce.
23761 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23762 with spaces. FIELD_WIDTH <= 0 means don't pad.
23764 PRECISION is the maximum number of characters to output from
23765 STRING. PRECISION <= 0 means don't truncate the string.
23767 If COPY_STRING, make a copy of LISP_STRING before adding
23768 properties to the string.
23770 PROPS are the properties to add to the string.
23771 The mode_line_string_face face property is always added to the string.
23774 static int
23775 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23776 bool copy_string,
23777 int field_width, int precision, Lisp_Object props)
23779 ptrdiff_t len;
23780 int n = 0;
23782 if (string != NULL)
23784 len = strlen (string);
23785 if (precision > 0 && len > precision)
23786 len = precision;
23787 lisp_string = make_string (string, len);
23788 if (NILP (props))
23789 props = mode_line_string_face_prop;
23790 else if (!NILP (mode_line_string_face))
23792 Lisp_Object face = Fplist_get (props, Qface);
23793 props = Fcopy_sequence (props);
23794 if (NILP (face))
23795 face = mode_line_string_face;
23796 else
23797 face = list2 (face, mode_line_string_face);
23798 props = Fplist_put (props, Qface, face);
23800 Fadd_text_properties (make_number (0), make_number (len),
23801 props, lisp_string);
23803 else
23805 len = XFASTINT (Flength (lisp_string));
23806 if (precision > 0 && len > precision)
23808 len = precision;
23809 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23810 precision = -1;
23812 if (!NILP (mode_line_string_face))
23814 Lisp_Object face;
23815 if (NILP (props))
23816 props = Ftext_properties_at (make_number (0), lisp_string);
23817 face = Fplist_get (props, Qface);
23818 if (NILP (face))
23819 face = mode_line_string_face;
23820 else
23821 face = list2 (face, mode_line_string_face);
23822 props = list2 (Qface, face);
23823 if (copy_string)
23824 lisp_string = Fcopy_sequence (lisp_string);
23826 if (!NILP (props))
23827 Fadd_text_properties (make_number (0), make_number (len),
23828 props, lisp_string);
23831 if (len > 0)
23833 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23834 n += len;
23837 if (field_width > len)
23839 field_width -= len;
23840 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23841 if (!NILP (props))
23842 Fadd_text_properties (make_number (0), make_number (field_width),
23843 props, lisp_string);
23844 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23845 n += field_width;
23848 return n;
23852 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23853 1, 4, 0,
23854 doc: /* Format a string out of a mode line format specification.
23855 First arg FORMAT specifies the mode line format (see `mode-line-format'
23856 for details) to use.
23858 By default, the format is evaluated for the currently selected window.
23860 Optional second arg FACE specifies the face property to put on all
23861 characters for which no face is specified. The value nil means the
23862 default face. The value t means whatever face the window's mode line
23863 currently uses (either `mode-line' or `mode-line-inactive',
23864 depending on whether the window is the selected window or not).
23865 An integer value means the value string has no text
23866 properties.
23868 Optional third and fourth args WINDOW and BUFFER specify the window
23869 and buffer to use as the context for the formatting (defaults
23870 are the selected window and the WINDOW's buffer). */)
23871 (Lisp_Object format, Lisp_Object face,
23872 Lisp_Object window, Lisp_Object buffer)
23874 struct it it;
23875 int len;
23876 struct window *w;
23877 struct buffer *old_buffer = NULL;
23878 int face_id;
23879 bool no_props = INTEGERP (face);
23880 ptrdiff_t count = SPECPDL_INDEX ();
23881 Lisp_Object str;
23882 int string_start = 0;
23884 w = decode_any_window (window);
23885 XSETWINDOW (window, w);
23887 if (NILP (buffer))
23888 buffer = w->contents;
23889 CHECK_BUFFER (buffer);
23891 /* Make formatting the modeline a non-op when noninteractive, otherwise
23892 there will be problems later caused by a partially initialized frame. */
23893 if (NILP (format) || noninteractive)
23894 return empty_unibyte_string;
23896 if (no_props)
23897 face = Qnil;
23899 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23900 : EQ (face, Qt) ? (EQ (window, selected_window)
23901 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23902 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23903 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23904 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23905 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23906 : DEFAULT_FACE_ID;
23908 old_buffer = current_buffer;
23910 /* Save things including mode_line_proptrans_alist,
23911 and set that to nil so that we don't alter the outer value. */
23912 record_unwind_protect (unwind_format_mode_line,
23913 format_mode_line_unwind_data
23914 (XFRAME (WINDOW_FRAME (w)),
23915 old_buffer, selected_window, true));
23916 mode_line_proptrans_alist = Qnil;
23918 Fselect_window (window, Qt);
23919 set_buffer_internal_1 (XBUFFER (buffer));
23921 init_iterator (&it, w, -1, -1, NULL, face_id);
23923 if (no_props)
23925 mode_line_target = MODE_LINE_NOPROP;
23926 mode_line_string_face_prop = Qnil;
23927 mode_line_string_list = Qnil;
23928 string_start = MODE_LINE_NOPROP_LEN (0);
23930 else
23932 mode_line_target = MODE_LINE_STRING;
23933 mode_line_string_list = Qnil;
23934 mode_line_string_face = face;
23935 mode_line_string_face_prop
23936 = NILP (face) ? Qnil : list2 (Qface, face);
23939 push_kboard (FRAME_KBOARD (it.f));
23940 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23941 pop_kboard ();
23943 if (no_props)
23945 len = MODE_LINE_NOPROP_LEN (string_start);
23946 str = make_string (mode_line_noprop_buf + string_start, len);
23948 else
23950 mode_line_string_list = Fnreverse (mode_line_string_list);
23951 str = Fmapconcat (Qidentity, mode_line_string_list,
23952 empty_unibyte_string);
23955 unbind_to (count, Qnil);
23956 return str;
23959 /* Write a null-terminated, right justified decimal representation of
23960 the positive integer D to BUF using a minimal field width WIDTH. */
23962 static void
23963 pint2str (register char *buf, register int width, register ptrdiff_t d)
23965 register char *p = buf;
23967 if (d <= 0)
23968 *p++ = '0';
23969 else
23971 while (d > 0)
23973 *p++ = d % 10 + '0';
23974 d /= 10;
23978 for (width -= (int) (p - buf); width > 0; --width)
23979 *p++ = ' ';
23980 *p-- = '\0';
23981 while (p > buf)
23983 d = *buf;
23984 *buf++ = *p;
23985 *p-- = d;
23989 /* Write a null-terminated, right justified decimal and "human
23990 readable" representation of the nonnegative integer D to BUF using
23991 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23993 static const char power_letter[] =
23995 0, /* no letter */
23996 'k', /* kilo */
23997 'M', /* mega */
23998 'G', /* giga */
23999 'T', /* tera */
24000 'P', /* peta */
24001 'E', /* exa */
24002 'Z', /* zetta */
24003 'Y' /* yotta */
24006 static void
24007 pint2hrstr (char *buf, int width, ptrdiff_t d)
24009 /* We aim to represent the nonnegative integer D as
24010 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24011 ptrdiff_t quotient = d;
24012 int remainder = 0;
24013 /* -1 means: do not use TENTHS. */
24014 int tenths = -1;
24015 int exponent = 0;
24017 /* Length of QUOTIENT.TENTHS as a string. */
24018 int length;
24020 char * psuffix;
24021 char * p;
24023 if (quotient >= 1000)
24025 /* Scale to the appropriate EXPONENT. */
24028 remainder = quotient % 1000;
24029 quotient /= 1000;
24030 exponent++;
24032 while (quotient >= 1000);
24034 /* Round to nearest and decide whether to use TENTHS or not. */
24035 if (quotient <= 9)
24037 tenths = remainder / 100;
24038 if (remainder % 100 >= 50)
24040 if (tenths < 9)
24041 tenths++;
24042 else
24044 quotient++;
24045 if (quotient == 10)
24046 tenths = -1;
24047 else
24048 tenths = 0;
24052 else
24053 if (remainder >= 500)
24055 if (quotient < 999)
24056 quotient++;
24057 else
24059 quotient = 1;
24060 exponent++;
24061 tenths = 0;
24066 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24067 if (tenths == -1 && quotient <= 99)
24068 if (quotient <= 9)
24069 length = 1;
24070 else
24071 length = 2;
24072 else
24073 length = 3;
24074 p = psuffix = buf + max (width, length);
24076 /* Print EXPONENT. */
24077 *psuffix++ = power_letter[exponent];
24078 *psuffix = '\0';
24080 /* Print TENTHS. */
24081 if (tenths >= 0)
24083 *--p = '0' + tenths;
24084 *--p = '.';
24087 /* Print QUOTIENT. */
24090 int digit = quotient % 10;
24091 *--p = '0' + digit;
24093 while ((quotient /= 10) != 0);
24095 /* Print leading spaces. */
24096 while (buf < p)
24097 *--p = ' ';
24100 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24101 If EOL_FLAG, set also a mnemonic character for end-of-line
24102 type of CODING_SYSTEM. Return updated pointer into BUF. */
24104 static unsigned char invalid_eol_type[] = "(*invalid*)";
24106 static char *
24107 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24109 Lisp_Object val;
24110 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24111 const unsigned char *eol_str;
24112 int eol_str_len;
24113 /* The EOL conversion we are using. */
24114 Lisp_Object eoltype;
24116 val = CODING_SYSTEM_SPEC (coding_system);
24117 eoltype = Qnil;
24119 if (!VECTORP (val)) /* Not yet decided. */
24121 *buf++ = multibyte ? '-' : ' ';
24122 if (eol_flag)
24123 eoltype = eol_mnemonic_undecided;
24124 /* Don't mention EOL conversion if it isn't decided. */
24126 else
24128 Lisp_Object attrs;
24129 Lisp_Object eolvalue;
24131 attrs = AREF (val, 0);
24132 eolvalue = AREF (val, 2);
24134 *buf++ = multibyte
24135 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24136 : ' ';
24138 if (eol_flag)
24140 /* The EOL conversion that is normal on this system. */
24142 if (NILP (eolvalue)) /* Not yet decided. */
24143 eoltype = eol_mnemonic_undecided;
24144 else if (VECTORP (eolvalue)) /* Not yet decided. */
24145 eoltype = eol_mnemonic_undecided;
24146 else /* eolvalue is Qunix, Qdos, or Qmac. */
24147 eoltype = (EQ (eolvalue, Qunix)
24148 ? eol_mnemonic_unix
24149 : EQ (eolvalue, Qdos)
24150 ? eol_mnemonic_dos : eol_mnemonic_mac);
24154 if (eol_flag)
24156 /* Mention the EOL conversion if it is not the usual one. */
24157 if (STRINGP (eoltype))
24159 eol_str = SDATA (eoltype);
24160 eol_str_len = SBYTES (eoltype);
24162 else if (CHARACTERP (eoltype))
24164 int c = XFASTINT (eoltype);
24165 return buf + CHAR_STRING (c, (unsigned char *) buf);
24167 else
24169 eol_str = invalid_eol_type;
24170 eol_str_len = sizeof (invalid_eol_type) - 1;
24172 memcpy (buf, eol_str, eol_str_len);
24173 buf += eol_str_len;
24176 return buf;
24179 /* Return the approximate percentage N is of D (rounding upward), or 99,
24180 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24182 static int
24183 percent99 (ptrdiff_t n, ptrdiff_t d)
24185 int percent = (d - 1 + 100.0 * n) / d;
24186 return min (percent, 99);
24189 /* Return a string for the output of a mode line %-spec for window W,
24190 generated by character C. FIELD_WIDTH > 0 means pad the string
24191 returned with spaces to that value. Return a Lisp string in
24192 *STRING if the resulting string is taken from that Lisp string.
24194 Note we operate on the current buffer for most purposes. */
24196 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24198 static const char *
24199 decode_mode_spec (struct window *w, register int c, int field_width,
24200 Lisp_Object *string)
24202 Lisp_Object obj;
24203 struct frame *f = XFRAME (WINDOW_FRAME (w));
24204 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24205 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24206 produce strings from numerical values, so limit preposterously
24207 large values of FIELD_WIDTH to avoid overrunning the buffer's
24208 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24209 bytes plus the terminating null. */
24210 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24211 struct buffer *b = current_buffer;
24213 obj = Qnil;
24214 *string = Qnil;
24216 switch (c)
24218 case '*':
24219 if (!NILP (BVAR (b, read_only)))
24220 return "%";
24221 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24222 return "*";
24223 return "-";
24225 case '+':
24226 /* This differs from %* only for a modified read-only buffer. */
24227 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24228 return "*";
24229 if (!NILP (BVAR (b, read_only)))
24230 return "%";
24231 return "-";
24233 case '&':
24234 /* This differs from %* in ignoring read-only-ness. */
24235 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24236 return "*";
24237 return "-";
24239 case '%':
24240 return "%";
24242 case '[':
24244 int i;
24245 char *p;
24247 if (command_loop_level > 5)
24248 return "[[[... ";
24249 p = decode_mode_spec_buf;
24250 for (i = 0; i < command_loop_level; i++)
24251 *p++ = '[';
24252 *p = 0;
24253 return decode_mode_spec_buf;
24256 case ']':
24258 int i;
24259 char *p;
24261 if (command_loop_level > 5)
24262 return " ...]]]";
24263 p = decode_mode_spec_buf;
24264 for (i = 0; i < command_loop_level; i++)
24265 *p++ = ']';
24266 *p = 0;
24267 return decode_mode_spec_buf;
24270 case '-':
24272 register int i;
24274 /* Let lots_of_dashes be a string of infinite length. */
24275 if (mode_line_target == MODE_LINE_NOPROP
24276 || mode_line_target == MODE_LINE_STRING)
24277 return "--";
24278 if (field_width <= 0
24279 || field_width > sizeof (lots_of_dashes))
24281 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24282 decode_mode_spec_buf[i] = '-';
24283 decode_mode_spec_buf[i] = '\0';
24284 return decode_mode_spec_buf;
24286 else
24287 return lots_of_dashes;
24290 case 'b':
24291 obj = BVAR (b, name);
24292 break;
24294 case 'c':
24295 case 'C':
24296 /* %c, %C, and %l are ignored in `frame-title-format'.
24297 (In redisplay_internal, the frame title is drawn _before_ the
24298 windows are updated, so the stuff which depends on actual
24299 window contents (such as %l) may fail to render properly, or
24300 even crash emacs.) */
24301 if (mode_line_target == MODE_LINE_TITLE)
24302 return "";
24303 else
24305 ptrdiff_t col = current_column ();
24306 int disp_col = (c == 'C') ? col + 1 : col;
24307 w->column_number_displayed = col;
24308 pint2str (decode_mode_spec_buf, width, disp_col);
24309 return decode_mode_spec_buf;
24312 case 'e':
24313 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24315 if (NILP (Vmemory_full))
24316 return "";
24317 else
24318 return "!MEM FULL! ";
24320 #else
24321 return "";
24322 #endif
24324 case 'F':
24325 /* %F displays the frame name. */
24326 if (!NILP (f->title))
24327 return SSDATA (f->title);
24328 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24329 return SSDATA (f->name);
24330 return "Emacs";
24332 case 'f':
24333 obj = BVAR (b, filename);
24334 break;
24336 case 'i':
24338 ptrdiff_t size = ZV - BEGV;
24339 pint2str (decode_mode_spec_buf, width, size);
24340 return decode_mode_spec_buf;
24343 case 'I':
24345 ptrdiff_t size = ZV - BEGV;
24346 pint2hrstr (decode_mode_spec_buf, width, size);
24347 return decode_mode_spec_buf;
24350 case 'l':
24352 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24353 ptrdiff_t topline, nlines, height;
24354 ptrdiff_t junk;
24356 /* %c, %C, and %l are ignored in `frame-title-format'. */
24357 if (mode_line_target == MODE_LINE_TITLE)
24358 return "";
24360 startpos = marker_position (w->start);
24361 startpos_byte = marker_byte_position (w->start);
24362 height = WINDOW_TOTAL_LINES (w);
24364 /* If we decided that this buffer isn't suitable for line numbers,
24365 don't forget that too fast. */
24366 if (w->base_line_pos == -1)
24367 goto no_value;
24369 /* If the buffer is very big, don't waste time. */
24370 if (INTEGERP (Vline_number_display_limit)
24371 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24373 w->base_line_pos = 0;
24374 w->base_line_number = 0;
24375 goto no_value;
24378 if (w->base_line_number > 0
24379 && w->base_line_pos > 0
24380 && w->base_line_pos <= startpos)
24382 line = w->base_line_number;
24383 linepos = w->base_line_pos;
24384 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24386 else
24388 line = 1;
24389 linepos = BUF_BEGV (b);
24390 linepos_byte = BUF_BEGV_BYTE (b);
24393 /* Count lines from base line to window start position. */
24394 nlines = display_count_lines (linepos_byte,
24395 startpos_byte,
24396 startpos, &junk);
24398 topline = nlines + line;
24400 /* Determine a new base line, if the old one is too close
24401 or too far away, or if we did not have one.
24402 "Too close" means it's plausible a scroll-down would
24403 go back past it. */
24404 if (startpos == BUF_BEGV (b))
24406 w->base_line_number = topline;
24407 w->base_line_pos = BUF_BEGV (b);
24409 else if (nlines < height + 25 || nlines > height * 3 + 50
24410 || linepos == BUF_BEGV (b))
24412 ptrdiff_t limit = BUF_BEGV (b);
24413 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24414 ptrdiff_t position;
24415 ptrdiff_t distance =
24416 (height * 2 + 30) * line_number_display_limit_width;
24418 if (startpos - distance > limit)
24420 limit = startpos - distance;
24421 limit_byte = CHAR_TO_BYTE (limit);
24424 nlines = display_count_lines (startpos_byte,
24425 limit_byte,
24426 - (height * 2 + 30),
24427 &position);
24428 /* If we couldn't find the lines we wanted within
24429 line_number_display_limit_width chars per line,
24430 give up on line numbers for this window. */
24431 if (position == limit_byte && limit == startpos - distance)
24433 w->base_line_pos = -1;
24434 w->base_line_number = 0;
24435 goto no_value;
24438 w->base_line_number = topline - nlines;
24439 w->base_line_pos = BYTE_TO_CHAR (position);
24442 /* Now count lines from the start pos to point. */
24443 nlines = display_count_lines (startpos_byte,
24444 PT_BYTE, PT, &junk);
24446 /* Record that we did display the line number. */
24447 line_number_displayed = true;
24449 /* Make the string to show. */
24450 pint2str (decode_mode_spec_buf, width, topline + nlines);
24451 return decode_mode_spec_buf;
24452 no_value:
24454 char *p = decode_mode_spec_buf;
24455 int pad = width - 2;
24456 while (pad-- > 0)
24457 *p++ = ' ';
24458 *p++ = '?';
24459 *p++ = '?';
24460 *p = '\0';
24461 return decode_mode_spec_buf;
24464 break;
24466 case 'm':
24467 obj = BVAR (b, mode_name);
24468 break;
24470 case 'n':
24471 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24472 return " Narrow";
24473 break;
24475 /* Display the "degree of travel" of the window through the buffer. */
24476 case 'o':
24478 ptrdiff_t toppos = marker_position (w->start);
24479 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24480 ptrdiff_t begv = BUF_BEGV (b);
24481 ptrdiff_t zv = BUF_ZV (b);
24483 if (zv <= botpos)
24484 return toppos <= begv ? "All" : "Bottom";
24485 else if (toppos <= begv)
24486 return "Top";
24487 else
24489 sprintf (decode_mode_spec_buf, "%2d%%",
24490 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24491 return decode_mode_spec_buf;
24495 /* Display percentage of buffer above the top of the screen. */
24496 case 'p':
24498 ptrdiff_t pos = marker_position (w->start);
24499 ptrdiff_t begv = BUF_BEGV (b);
24500 ptrdiff_t zv = BUF_ZV (b);
24502 if (w->window_end_pos <= BUF_Z (b) - zv)
24503 return pos <= begv ? "All" : "Bottom";
24504 else if (pos <= begv)
24505 return "Top";
24506 else
24508 sprintf (decode_mode_spec_buf, "%2d%%",
24509 percent99 (pos - begv, zv - begv));
24510 return decode_mode_spec_buf;
24514 /* Display percentage of size above the bottom of the screen. */
24515 case 'P':
24517 ptrdiff_t toppos = marker_position (w->start);
24518 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24519 ptrdiff_t begv = BUF_BEGV (b);
24520 ptrdiff_t zv = BUF_ZV (b);
24522 if (zv <= botpos)
24523 return toppos <= begv ? "All" : "Bottom";
24524 else
24526 sprintf (decode_mode_spec_buf,
24527 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24528 percent99 (botpos - begv, zv - begv));
24529 return decode_mode_spec_buf;
24533 /* Display percentage offsets of top and bottom of the window,
24534 using "All" (but not "Top" or "Bottom") where appropriate. */
24535 case 'q':
24537 ptrdiff_t toppos = marker_position (w->start);
24538 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24539 ptrdiff_t begv = BUF_BEGV (b);
24540 ptrdiff_t zv = BUF_ZV (b);
24541 int top_perc, bot_perc;
24543 if ((toppos <= begv) && (zv <= botpos))
24544 return "All ";
24546 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24547 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24549 if (top_perc == bot_perc)
24550 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24551 else
24552 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24554 return decode_mode_spec_buf;
24557 case 's':
24558 /* status of process */
24559 obj = Fget_buffer_process (Fcurrent_buffer ());
24560 if (NILP (obj))
24561 return "no process";
24562 #ifndef MSDOS
24563 obj = Fsymbol_name (Fprocess_status (obj));
24564 #endif
24565 break;
24567 case '@':
24569 ptrdiff_t count = inhibit_garbage_collection ();
24570 Lisp_Object curdir = BVAR (current_buffer, directory);
24571 Lisp_Object val = Qnil;
24573 if (STRINGP (curdir))
24574 val = call1 (intern ("file-remote-p"), curdir);
24576 unbind_to (count, Qnil);
24578 if (NILP (val))
24579 return "-";
24580 else
24581 return "@";
24584 case 'z':
24585 /* coding-system (not including end-of-line format) */
24586 case 'Z':
24587 /* coding-system (including end-of-line type) */
24589 bool eol_flag = (c == 'Z');
24590 char *p = decode_mode_spec_buf;
24592 if (! FRAME_WINDOW_P (f))
24594 /* No need to mention EOL here--the terminal never needs
24595 to do EOL conversion. */
24596 p = decode_mode_spec_coding (CODING_ID_NAME
24597 (FRAME_KEYBOARD_CODING (f)->id),
24598 p, false);
24599 p = decode_mode_spec_coding (CODING_ID_NAME
24600 (FRAME_TERMINAL_CODING (f)->id),
24601 p, false);
24603 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24604 p, eol_flag);
24606 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24607 #ifdef subprocesses
24608 obj = Fget_buffer_process (Fcurrent_buffer ());
24609 if (PROCESSP (obj))
24611 p = decode_mode_spec_coding
24612 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24613 p = decode_mode_spec_coding
24614 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24616 #endif /* subprocesses */
24617 #endif /* false */
24618 *p = 0;
24619 return decode_mode_spec_buf;
24623 if (STRINGP (obj))
24625 *string = obj;
24626 return SSDATA (obj);
24628 else
24629 return "";
24633 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24634 means count lines back from START_BYTE. But don't go beyond
24635 LIMIT_BYTE. Return the number of lines thus found (always
24636 nonnegative).
24638 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24639 either the position COUNT lines after/before START_BYTE, if we
24640 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24641 COUNT lines. */
24643 static ptrdiff_t
24644 display_count_lines (ptrdiff_t start_byte,
24645 ptrdiff_t limit_byte, ptrdiff_t count,
24646 ptrdiff_t *byte_pos_ptr)
24648 register unsigned char *cursor;
24649 unsigned char *base;
24651 register ptrdiff_t ceiling;
24652 register unsigned char *ceiling_addr;
24653 ptrdiff_t orig_count = count;
24655 /* If we are not in selective display mode,
24656 check only for newlines. */
24657 bool selective_display
24658 = (!NILP (BVAR (current_buffer, selective_display))
24659 && !INTEGERP (BVAR (current_buffer, selective_display)));
24661 if (count > 0)
24663 while (start_byte < limit_byte)
24665 ceiling = BUFFER_CEILING_OF (start_byte);
24666 ceiling = min (limit_byte - 1, ceiling);
24667 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24668 base = (cursor = BYTE_POS_ADDR (start_byte));
24672 if (selective_display)
24674 while (*cursor != '\n' && *cursor != 015
24675 && ++cursor != ceiling_addr)
24676 continue;
24677 if (cursor == ceiling_addr)
24678 break;
24680 else
24682 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24683 if (! cursor)
24684 break;
24687 cursor++;
24689 if (--count == 0)
24691 start_byte += cursor - base;
24692 *byte_pos_ptr = start_byte;
24693 return orig_count;
24696 while (cursor < ceiling_addr);
24698 start_byte += ceiling_addr - base;
24701 else
24703 while (start_byte > limit_byte)
24705 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24706 ceiling = max (limit_byte, ceiling);
24707 ceiling_addr = BYTE_POS_ADDR (ceiling);
24708 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24709 while (true)
24711 if (selective_display)
24713 while (--cursor >= ceiling_addr
24714 && *cursor != '\n' && *cursor != 015)
24715 continue;
24716 if (cursor < ceiling_addr)
24717 break;
24719 else
24721 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24722 if (! cursor)
24723 break;
24726 if (++count == 0)
24728 start_byte += cursor - base + 1;
24729 *byte_pos_ptr = start_byte;
24730 /* When scanning backwards, we should
24731 not count the newline posterior to which we stop. */
24732 return - orig_count - 1;
24735 start_byte += ceiling_addr - base;
24739 *byte_pos_ptr = limit_byte;
24741 if (count < 0)
24742 return - orig_count + count;
24743 return orig_count - count;
24749 /***********************************************************************
24750 Displaying strings
24751 ***********************************************************************/
24753 /* Display a NUL-terminated string, starting with index START.
24755 If STRING is non-null, display that C string. Otherwise, the Lisp
24756 string LISP_STRING is displayed. There's a case that STRING is
24757 non-null and LISP_STRING is not nil. It means STRING is a string
24758 data of LISP_STRING. In that case, we display LISP_STRING while
24759 ignoring its text properties.
24761 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24762 FACE_STRING. Display STRING or LISP_STRING with the face at
24763 FACE_STRING_POS in FACE_STRING:
24765 Display the string in the environment given by IT, but use the
24766 standard display table, temporarily.
24768 FIELD_WIDTH is the minimum number of output glyphs to produce.
24769 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24770 with spaces. If STRING has more characters, more than FIELD_WIDTH
24771 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24773 PRECISION is the maximum number of characters to output from
24774 STRING. PRECISION < 0 means don't truncate the string.
24776 This is roughly equivalent to printf format specifiers:
24778 FIELD_WIDTH PRECISION PRINTF
24779 ----------------------------------------
24780 -1 -1 %s
24781 -1 10 %.10s
24782 10 -1 %10s
24783 20 10 %20.10s
24785 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24786 display them, and < 0 means obey the current buffer's value of
24787 enable_multibyte_characters.
24789 Value is the number of columns displayed. */
24791 static int
24792 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24793 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24794 int field_width, int precision, int max_x, int multibyte)
24796 int hpos_at_start = it->hpos;
24797 int saved_face_id = it->face_id;
24798 struct glyph_row *row = it->glyph_row;
24799 ptrdiff_t it_charpos;
24801 /* Initialize the iterator IT for iteration over STRING beginning
24802 with index START. */
24803 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24804 precision, field_width, multibyte);
24805 if (string && STRINGP (lisp_string))
24806 /* LISP_STRING is the one returned by decode_mode_spec. We should
24807 ignore its text properties. */
24808 it->stop_charpos = it->end_charpos;
24810 /* If displaying STRING, set up the face of the iterator from
24811 FACE_STRING, if that's given. */
24812 if (STRINGP (face_string))
24814 ptrdiff_t endptr;
24815 struct face *face;
24817 it->face_id
24818 = face_at_string_position (it->w, face_string, face_string_pos,
24819 0, &endptr, it->base_face_id, false);
24820 face = FACE_FROM_ID (it->f, it->face_id);
24821 it->face_box_p = face->box != FACE_NO_BOX;
24824 /* Set max_x to the maximum allowed X position. Don't let it go
24825 beyond the right edge of the window. */
24826 if (max_x <= 0)
24827 max_x = it->last_visible_x;
24828 else
24829 max_x = min (max_x, it->last_visible_x);
24831 /* Skip over display elements that are not visible. because IT->w is
24832 hscrolled. */
24833 if (it->current_x < it->first_visible_x)
24834 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24835 MOVE_TO_POS | MOVE_TO_X);
24837 row->ascent = it->max_ascent;
24838 row->height = it->max_ascent + it->max_descent;
24839 row->phys_ascent = it->max_phys_ascent;
24840 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24841 row->extra_line_spacing = it->max_extra_line_spacing;
24843 if (STRINGP (it->string))
24844 it_charpos = IT_STRING_CHARPOS (*it);
24845 else
24846 it_charpos = IT_CHARPOS (*it);
24848 /* This condition is for the case that we are called with current_x
24849 past last_visible_x. */
24850 while (it->current_x < max_x)
24852 int x_before, x, n_glyphs_before, i, nglyphs;
24854 /* Get the next display element. */
24855 if (!get_next_display_element (it))
24856 break;
24858 /* Produce glyphs. */
24859 x_before = it->current_x;
24860 n_glyphs_before = row->used[TEXT_AREA];
24861 PRODUCE_GLYPHS (it);
24863 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24864 i = 0;
24865 x = x_before;
24866 while (i < nglyphs)
24868 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24870 if (it->line_wrap != TRUNCATE
24871 && x + glyph->pixel_width > max_x)
24873 /* End of continued line or max_x reached. */
24874 if (CHAR_GLYPH_PADDING_P (*glyph))
24876 /* A wide character is unbreakable. */
24877 if (row->reversed_p)
24878 unproduce_glyphs (it, row->used[TEXT_AREA]
24879 - n_glyphs_before);
24880 row->used[TEXT_AREA] = n_glyphs_before;
24881 it->current_x = x_before;
24883 else
24885 if (row->reversed_p)
24886 unproduce_glyphs (it, row->used[TEXT_AREA]
24887 - (n_glyphs_before + i));
24888 row->used[TEXT_AREA] = n_glyphs_before + i;
24889 it->current_x = x;
24891 break;
24893 else if (x + glyph->pixel_width >= it->first_visible_x)
24895 /* Glyph is at least partially visible. */
24896 ++it->hpos;
24897 if (x < it->first_visible_x)
24898 row->x = x - it->first_visible_x;
24900 else
24902 /* Glyph is off the left margin of the display area.
24903 Should not happen. */
24904 emacs_abort ();
24907 row->ascent = max (row->ascent, it->max_ascent);
24908 row->height = max (row->height, it->max_ascent + it->max_descent);
24909 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24910 row->phys_height = max (row->phys_height,
24911 it->max_phys_ascent + it->max_phys_descent);
24912 row->extra_line_spacing = max (row->extra_line_spacing,
24913 it->max_extra_line_spacing);
24914 x += glyph->pixel_width;
24915 ++i;
24918 /* Stop if max_x reached. */
24919 if (i < nglyphs)
24920 break;
24922 /* Stop at line ends. */
24923 if (ITERATOR_AT_END_OF_LINE_P (it))
24925 it->continuation_lines_width = 0;
24926 break;
24929 set_iterator_to_next (it, true);
24930 if (STRINGP (it->string))
24931 it_charpos = IT_STRING_CHARPOS (*it);
24932 else
24933 it_charpos = IT_CHARPOS (*it);
24935 /* Stop if truncating at the right edge. */
24936 if (it->line_wrap == TRUNCATE
24937 && it->current_x >= it->last_visible_x)
24939 /* Add truncation mark, but don't do it if the line is
24940 truncated at a padding space. */
24941 if (it_charpos < it->string_nchars)
24943 if (!FRAME_WINDOW_P (it->f))
24945 int ii, n;
24947 if (it->current_x > it->last_visible_x)
24949 if (!row->reversed_p)
24951 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24952 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24953 break;
24955 else
24957 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24958 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24959 break;
24960 unproduce_glyphs (it, ii + 1);
24961 ii = row->used[TEXT_AREA] - (ii + 1);
24963 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24965 row->used[TEXT_AREA] = ii;
24966 produce_special_glyphs (it, IT_TRUNCATION);
24969 produce_special_glyphs (it, IT_TRUNCATION);
24971 row->truncated_on_right_p = true;
24973 break;
24977 /* Maybe insert a truncation at the left. */
24978 if (it->first_visible_x
24979 && it_charpos > 0)
24981 if (!FRAME_WINDOW_P (it->f)
24982 || (row->reversed_p
24983 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24984 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24985 insert_left_trunc_glyphs (it);
24986 row->truncated_on_left_p = true;
24989 it->face_id = saved_face_id;
24991 /* Value is number of columns displayed. */
24992 return it->hpos - hpos_at_start;
24997 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24998 appears as an element of LIST or as the car of an element of LIST.
24999 If PROPVAL is a list, compare each element against LIST in that
25000 way, and return 1/2 if any element of PROPVAL is found in LIST.
25001 Otherwise return 0. This function cannot quit.
25002 The return value is 2 if the text is invisible but with an ellipsis
25003 and 1 if it's invisible and without an ellipsis. */
25006 invisible_prop (Lisp_Object propval, Lisp_Object list)
25008 Lisp_Object tail, proptail;
25010 for (tail = list; CONSP (tail); tail = XCDR (tail))
25012 register Lisp_Object tem;
25013 tem = XCAR (tail);
25014 if (EQ (propval, tem))
25015 return 1;
25016 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25017 return NILP (XCDR (tem)) ? 1 : 2;
25020 if (CONSP (propval))
25022 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25024 Lisp_Object propelt;
25025 propelt = XCAR (proptail);
25026 for (tail = list; CONSP (tail); tail = XCDR (tail))
25028 register Lisp_Object tem;
25029 tem = XCAR (tail);
25030 if (EQ (propelt, tem))
25031 return 1;
25032 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25033 return NILP (XCDR (tem)) ? 1 : 2;
25038 return 0;
25041 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25042 doc: /* Non-nil if the property makes the text invisible.
25043 POS-OR-PROP can be a marker or number, in which case it is taken to be
25044 a position in the current buffer and the value of the `invisible' property
25045 is checked; or it can be some other value, which is then presumed to be the
25046 value of the `invisible' property of the text of interest.
25047 The non-nil value returned can be t for truly invisible text or something
25048 else if the text is replaced by an ellipsis. */)
25049 (Lisp_Object pos_or_prop)
25051 Lisp_Object prop
25052 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
25053 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
25054 : pos_or_prop);
25055 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25056 return (invis == 0 ? Qnil
25057 : invis == 1 ? Qt
25058 : make_number (invis));
25061 /* Calculate a width or height in pixels from a specification using
25062 the following elements:
25064 SPEC ::=
25065 NUM - a (fractional) multiple of the default font width/height
25066 (NUM) - specifies exactly NUM pixels
25067 UNIT - a fixed number of pixels, see below.
25068 ELEMENT - size of a display element in pixels, see below.
25069 (NUM . SPEC) - equals NUM * SPEC
25070 (+ SPEC SPEC ...) - add pixel values
25071 (- SPEC SPEC ...) - subtract pixel values
25072 (- SPEC) - negate pixel value
25074 NUM ::=
25075 INT or FLOAT - a number constant
25076 SYMBOL - use symbol's (buffer local) variable binding.
25078 UNIT ::=
25079 in - pixels per inch *)
25080 mm - pixels per 1/1000 meter *)
25081 cm - pixels per 1/100 meter *)
25082 width - width of current font in pixels.
25083 height - height of current font in pixels.
25085 *) using the ratio(s) defined in display-pixels-per-inch.
25087 ELEMENT ::=
25089 left-fringe - left fringe width in pixels
25090 right-fringe - right fringe width in pixels
25092 left-margin - left margin width in pixels
25093 right-margin - right margin width in pixels
25095 scroll-bar - scroll-bar area width in pixels
25097 Examples:
25099 Pixels corresponding to 5 inches:
25100 (5 . in)
25102 Total width of non-text areas on left side of window (if scroll-bar is on left):
25103 '(space :width (+ left-fringe left-margin scroll-bar))
25105 Align to first text column (in header line):
25106 '(space :align-to 0)
25108 Align to middle of text area minus half the width of variable `my-image'
25109 containing a loaded image:
25110 '(space :align-to (0.5 . (- text my-image)))
25112 Width of left margin minus width of 1 character in the default font:
25113 '(space :width (- left-margin 1))
25115 Width of left margin minus width of 2 characters in the current font:
25116 '(space :width (- left-margin (2 . width)))
25118 Center 1 character over left-margin (in header line):
25119 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25121 Different ways to express width of left fringe plus left margin minus one pixel:
25122 '(space :width (- (+ left-fringe left-margin) (1)))
25123 '(space :width (+ left-fringe left-margin (- (1))))
25124 '(space :width (+ left-fringe left-margin (-1)))
25126 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25127 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25128 coordinate, and *RES is the additional pixel width from that point
25129 till the end of the stretch glyph.
25131 WIDTH_P non-zero means take the width dimension or X coordinate of
25132 the object specified by PROP, WIDTH_P zero means take the height
25133 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25134 non-NULL, WIDTH_P should be non-zero.)
25136 FONT is the font of the face of the surrounding text.
25138 The return value is non-zero if width or height were successfully
25139 calculated, i.e. if PROP is a valid spec. */
25141 static bool
25142 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25143 struct font *font, bool width_p, int *align_to)
25145 double pixels;
25147 # define OK_PIXELS(val) (*res = (val), true)
25148 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25150 if (NILP (prop))
25151 return OK_PIXELS (0);
25153 eassert (FRAME_LIVE_P (it->f));
25155 if (SYMBOLP (prop))
25157 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25159 char *unit = SSDATA (SYMBOL_NAME (prop));
25161 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25162 if (unit[0] == 'i' && unit[1] == 'n')
25163 pixels = 1.0;
25164 else if (unit[0] == 'm' && unit[1] == 'm')
25165 pixels = 25.4;
25166 else if (unit[0] == 'c' && unit[1] == 'm')
25167 pixels = 2.54;
25168 else
25169 pixels = 0;
25170 if (pixels > 0)
25172 double ppi = (width_p ? FRAME_RES_X (it->f)
25173 : FRAME_RES_Y (it->f));
25175 if (ppi > 0)
25176 return OK_PIXELS (ppi / pixels);
25177 return false;
25181 #ifdef HAVE_WINDOW_SYSTEM
25182 /* 'height': the height of FONT. */
25183 if (EQ (prop, Qheight))
25184 return OK_PIXELS (font
25185 ? normal_char_height (font, -1)
25186 : FRAME_LINE_HEIGHT (it->f));
25187 /* 'width': the width of FONT. */
25188 if (EQ (prop, Qwidth))
25189 return OK_PIXELS (font
25190 ? FONT_WIDTH (font)
25191 : FRAME_COLUMN_WIDTH (it->f));
25192 #else
25193 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25194 return OK_PIXELS (1);
25195 #endif
25197 /* 'text': the width or height of the text area. */
25198 if (EQ (prop, Qtext))
25199 return OK_PIXELS (width_p
25200 ? (window_box_width (it->w, TEXT_AREA)
25201 - it->lnum_pixel_width)
25202 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25204 /* ':align_to'. First time we compute the value, window
25205 elements are interpreted as the position of the element's
25206 left edge. */
25207 if (align_to && *align_to < 0)
25209 *res = 0;
25210 /* 'left': left edge of the text area. */
25211 if (EQ (prop, Qleft))
25212 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25213 + it->lnum_pixel_width);
25214 /* 'right': right edge of the text area. */
25215 if (EQ (prop, Qright))
25216 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25217 /* 'center': the center of the text area. */
25218 if (EQ (prop, Qcenter))
25219 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25220 + it->lnum_pixel_width
25221 + window_box_width (it->w, TEXT_AREA) / 2);
25222 /* 'left-fringe': left edge of the left fringe. */
25223 if (EQ (prop, Qleft_fringe))
25224 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25225 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25226 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25227 /* 'right-fringe': left edge of the right fringe. */
25228 if (EQ (prop, Qright_fringe))
25229 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25230 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25231 : window_box_right_offset (it->w, TEXT_AREA));
25232 /* 'left-margin': left edge of the left display margin. */
25233 if (EQ (prop, Qleft_margin))
25234 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25235 /* 'right-margin': left edge of the right display margin. */
25236 if (EQ (prop, Qright_margin))
25237 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25238 /* 'scroll-bar': left edge of the vertical scroll bar. */
25239 if (EQ (prop, Qscroll_bar))
25240 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25242 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25243 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25244 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25245 : 0)));
25247 else
25249 /* Otherwise, the elements stand for their width. */
25250 if (EQ (prop, Qleft_fringe))
25251 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25252 if (EQ (prop, Qright_fringe))
25253 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25254 if (EQ (prop, Qleft_margin))
25255 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25256 if (EQ (prop, Qright_margin))
25257 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25258 if (EQ (prop, Qscroll_bar))
25259 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25262 prop = buffer_local_value (prop, it->w->contents);
25263 if (EQ (prop, Qunbound))
25264 prop = Qnil;
25267 if (NUMBERP (prop))
25269 int base_unit = (width_p
25270 ? FRAME_COLUMN_WIDTH (it->f)
25271 : FRAME_LINE_HEIGHT (it->f));
25272 if (width_p && align_to && *align_to < 0)
25273 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25274 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25277 if (CONSP (prop))
25279 Lisp_Object car = XCAR (prop);
25280 Lisp_Object cdr = XCDR (prop);
25282 if (SYMBOLP (car))
25284 #ifdef HAVE_WINDOW_SYSTEM
25285 /* '(image PROPS...)': width or height of the specified image. */
25286 if (FRAME_WINDOW_P (it->f)
25287 && valid_image_p (prop))
25289 ptrdiff_t id = lookup_image (it->f, prop);
25290 struct image *img = IMAGE_FROM_ID (it->f, id);
25292 return OK_PIXELS (width_p ? img->width : img->height);
25294 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25295 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25297 /* TODO: Don't return dummy size. */
25298 return OK_PIXELS (100);
25300 #endif
25301 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25302 recursively calculated values. */
25303 if (EQ (car, Qplus) || EQ (car, Qminus))
25305 bool first = true;
25306 double px;
25308 pixels = 0;
25309 while (CONSP (cdr))
25311 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25312 font, width_p, align_to))
25313 return false;
25314 if (first)
25315 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25316 else
25317 pixels += px;
25318 cdr = XCDR (cdr);
25320 if (EQ (car, Qminus))
25321 pixels = -pixels;
25322 return OK_PIXELS (pixels);
25325 car = buffer_local_value (car, it->w->contents);
25326 if (EQ (car, Qunbound))
25327 car = Qnil;
25330 /* '(NUM)': absolute number of pixels. */
25331 if (NUMBERP (car))
25333 double fact;
25334 int offset =
25335 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25336 pixels = XFLOATINT (car);
25337 if (NILP (cdr))
25338 return OK_PIXELS (pixels + offset);
25339 if (calc_pixel_width_or_height (&fact, it, cdr,
25340 font, width_p, align_to))
25341 return OK_PIXELS (pixels * fact + offset);
25342 return false;
25345 return false;
25348 return false;
25351 void
25352 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25354 #ifdef HAVE_WINDOW_SYSTEM
25355 normal_char_ascent_descent (font, -1, ascent, descent);
25356 #else
25357 *ascent = 1;
25358 *descent = 0;
25359 #endif
25363 /***********************************************************************
25364 Glyph Display
25365 ***********************************************************************/
25367 #ifdef HAVE_WINDOW_SYSTEM
25369 #ifdef GLYPH_DEBUG
25371 void
25372 dump_glyph_string (struct glyph_string *s)
25374 fprintf (stderr, "glyph string\n");
25375 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25376 s->x, s->y, s->width, s->height);
25377 fprintf (stderr, " ybase = %d\n", s->ybase);
25378 fprintf (stderr, " hl = %u\n", s->hl);
25379 fprintf (stderr, " left overhang = %d, right = %d\n",
25380 s->left_overhang, s->right_overhang);
25381 fprintf (stderr, " nchars = %d\n", s->nchars);
25382 fprintf (stderr, " extends to end of line = %d\n",
25383 s->extends_to_end_of_line_p);
25384 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25385 fprintf (stderr, " bg width = %d\n", s->background_width);
25388 #endif /* GLYPH_DEBUG */
25390 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25391 of XChar2b structures for S; it can't be allocated in
25392 init_glyph_string because it must be allocated via `alloca'. W
25393 is the window on which S is drawn. ROW and AREA are the glyph row
25394 and area within the row from which S is constructed. START is the
25395 index of the first glyph structure covered by S. HL is a
25396 face-override for drawing S. */
25398 #ifdef HAVE_NTGUI
25399 #define OPTIONAL_HDC(hdc) HDC hdc,
25400 #define DECLARE_HDC(hdc) HDC hdc;
25401 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25402 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25403 #endif
25405 #ifndef OPTIONAL_HDC
25406 #define OPTIONAL_HDC(hdc)
25407 #define DECLARE_HDC(hdc)
25408 #define ALLOCATE_HDC(hdc, f)
25409 #define RELEASE_HDC(hdc, f)
25410 #endif
25412 static void
25413 init_glyph_string (struct glyph_string *s,
25414 OPTIONAL_HDC (hdc)
25415 XChar2b *char2b, struct window *w, struct glyph_row *row,
25416 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25418 memset (s, 0, sizeof *s);
25419 s->w = w;
25420 s->f = XFRAME (w->frame);
25421 #ifdef HAVE_NTGUI
25422 s->hdc = hdc;
25423 #endif
25424 s->display = FRAME_X_DISPLAY (s->f);
25425 s->char2b = char2b;
25426 s->hl = hl;
25427 s->row = row;
25428 s->area = area;
25429 s->first_glyph = row->glyphs[area] + start;
25430 s->height = row->height;
25431 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25432 s->ybase = s->y + row->ascent;
25436 /* Append the list of glyph strings with head H and tail T to the list
25437 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25439 static void
25440 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25441 struct glyph_string *h, struct glyph_string *t)
25443 if (h)
25445 if (*head)
25446 (*tail)->next = h;
25447 else
25448 *head = h;
25449 h->prev = *tail;
25450 *tail = t;
25455 /* Prepend the list of glyph strings with head H and tail T to the
25456 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25457 result. */
25459 static void
25460 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25461 struct glyph_string *h, struct glyph_string *t)
25463 if (h)
25465 if (*head)
25466 (*head)->prev = t;
25467 else
25468 *tail = t;
25469 t->next = *head;
25470 *head = h;
25475 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25476 Set *HEAD and *TAIL to the resulting list. */
25478 static void
25479 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25480 struct glyph_string *s)
25482 s->next = s->prev = NULL;
25483 append_glyph_string_lists (head, tail, s, s);
25487 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25488 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25489 make sure that X resources for the face returned are allocated.
25490 Value is a pointer to a realized face that is ready for display if
25491 DISPLAY_P. */
25493 static struct face *
25494 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25495 XChar2b *char2b, bool display_p)
25497 struct face *face = FACE_FROM_ID (f, face_id);
25498 unsigned code = 0;
25500 if (face->font)
25502 code = face->font->driver->encode_char (face->font, c);
25504 if (code == FONT_INVALID_CODE)
25505 code = 0;
25507 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25509 /* Make sure X resources of the face are allocated. */
25510 #ifdef HAVE_X_WINDOWS
25511 if (display_p)
25512 #endif
25514 eassert (face != NULL);
25515 prepare_face_for_display (f, face);
25518 return face;
25522 /* Get face and two-byte form of character glyph GLYPH on frame F.
25523 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25524 a pointer to a realized face that is ready for display. */
25526 static struct face *
25527 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25528 XChar2b *char2b)
25530 struct face *face;
25531 unsigned code = 0;
25533 eassert (glyph->type == CHAR_GLYPH);
25534 face = FACE_FROM_ID (f, glyph->face_id);
25536 /* Make sure X resources of the face are allocated. */
25537 prepare_face_for_display (f, face);
25539 if (face->font)
25541 if (CHAR_BYTE8_P (glyph->u.ch))
25542 code = CHAR_TO_BYTE8 (glyph->u.ch);
25543 else
25544 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25546 if (code == FONT_INVALID_CODE)
25547 code = 0;
25550 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25551 return face;
25555 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25556 Return true iff FONT has a glyph for C. */
25558 static bool
25559 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25561 unsigned code;
25563 if (CHAR_BYTE8_P (c))
25564 code = CHAR_TO_BYTE8 (c);
25565 else
25566 code = font->driver->encode_char (font, c);
25568 if (code == FONT_INVALID_CODE)
25569 return false;
25570 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25571 return true;
25575 /* Fill glyph string S with composition components specified by S->cmp.
25577 BASE_FACE is the base face of the composition.
25578 S->cmp_from is the index of the first component for S.
25580 OVERLAPS non-zero means S should draw the foreground only, and use
25581 its physical height for clipping. See also draw_glyphs.
25583 Value is the index of a component not in S. */
25585 static int
25586 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25587 int overlaps)
25589 int i;
25590 /* For all glyphs of this composition, starting at the offset
25591 S->cmp_from, until we reach the end of the definition or encounter a
25592 glyph that requires the different face, add it to S. */
25593 struct face *face;
25595 eassert (s);
25597 s->for_overlaps = overlaps;
25598 s->face = NULL;
25599 s->font = NULL;
25600 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25602 int c = COMPOSITION_GLYPH (s->cmp, i);
25604 /* TAB in a composition means display glyphs with padding space
25605 on the left or right. */
25606 if (c != '\t')
25608 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25609 -1, Qnil);
25611 face = get_char_face_and_encoding (s->f, c, face_id,
25612 s->char2b + i, true);
25613 if (face)
25615 if (! s->face)
25617 s->face = face;
25618 s->font = s->face->font;
25620 else if (s->face != face)
25621 break;
25624 ++s->nchars;
25626 s->cmp_to = i;
25628 if (s->face == NULL)
25630 s->face = base_face->ascii_face;
25631 s->font = s->face->font;
25634 /* All glyph strings for the same composition has the same width,
25635 i.e. the width set for the first component of the composition. */
25636 s->width = s->first_glyph->pixel_width;
25638 /* If the specified font could not be loaded, use the frame's
25639 default font, but record the fact that we couldn't load it in
25640 the glyph string so that we can draw rectangles for the
25641 characters of the glyph string. */
25642 if (s->font == NULL)
25644 s->font_not_found_p = true;
25645 s->font = FRAME_FONT (s->f);
25648 /* Adjust base line for subscript/superscript text. */
25649 s->ybase += s->first_glyph->voffset;
25651 return s->cmp_to;
25654 static int
25655 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25656 int start, int end, int overlaps)
25658 struct glyph *glyph, *last;
25659 Lisp_Object lgstring;
25660 int i;
25662 s->for_overlaps = overlaps;
25663 glyph = s->row->glyphs[s->area] + start;
25664 last = s->row->glyphs[s->area] + end;
25665 s->cmp_id = glyph->u.cmp.id;
25666 s->cmp_from = glyph->slice.cmp.from;
25667 s->cmp_to = glyph->slice.cmp.to + 1;
25668 s->face = FACE_FROM_ID (s->f, face_id);
25669 lgstring = composition_gstring_from_id (s->cmp_id);
25670 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25671 glyph++;
25672 while (glyph < last
25673 && glyph->u.cmp.automatic
25674 && glyph->u.cmp.id == s->cmp_id
25675 && s->cmp_to == glyph->slice.cmp.from)
25676 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25678 for (i = s->cmp_from; i < s->cmp_to; i++)
25680 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25681 unsigned code = LGLYPH_CODE (lglyph);
25683 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25685 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25686 return glyph - s->row->glyphs[s->area];
25690 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25691 See the comment of fill_glyph_string for arguments.
25692 Value is the index of the first glyph not in S. */
25695 static int
25696 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25697 int start, int end, int overlaps)
25699 struct glyph *glyph, *last;
25700 int voffset;
25702 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25703 s->for_overlaps = overlaps;
25704 glyph = s->row->glyphs[s->area] + start;
25705 last = s->row->glyphs[s->area] + end;
25706 voffset = glyph->voffset;
25707 s->face = FACE_FROM_ID (s->f, face_id);
25708 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25709 s->nchars = 1;
25710 s->width = glyph->pixel_width;
25711 glyph++;
25712 while (glyph < last
25713 && glyph->type == GLYPHLESS_GLYPH
25714 && glyph->voffset == voffset
25715 && glyph->face_id == face_id)
25717 s->nchars++;
25718 s->width += glyph->pixel_width;
25719 glyph++;
25721 s->ybase += voffset;
25722 return glyph - s->row->glyphs[s->area];
25726 /* Fill glyph string S from a sequence of character glyphs.
25728 FACE_ID is the face id of the string. START is the index of the
25729 first glyph to consider, END is the index of the last + 1.
25730 OVERLAPS non-zero means S should draw the foreground only, and use
25731 its physical height for clipping. See also draw_glyphs.
25733 Value is the index of the first glyph not in S. */
25735 static int
25736 fill_glyph_string (struct glyph_string *s, int face_id,
25737 int start, int end, int overlaps)
25739 struct glyph *glyph, *last;
25740 int voffset;
25741 bool glyph_not_available_p;
25743 eassert (s->f == XFRAME (s->w->frame));
25744 eassert (s->nchars == 0);
25745 eassert (start >= 0 && end > start);
25747 s->for_overlaps = overlaps;
25748 glyph = s->row->glyphs[s->area] + start;
25749 last = s->row->glyphs[s->area] + end;
25750 voffset = glyph->voffset;
25751 s->padding_p = glyph->padding_p;
25752 glyph_not_available_p = glyph->glyph_not_available_p;
25754 while (glyph < last
25755 && glyph->type == CHAR_GLYPH
25756 && glyph->voffset == voffset
25757 /* Same face id implies same font, nowadays. */
25758 && glyph->face_id == face_id
25759 && glyph->glyph_not_available_p == glyph_not_available_p)
25761 s->face = get_glyph_face_and_encoding (s->f, glyph,
25762 s->char2b + s->nchars);
25763 ++s->nchars;
25764 eassert (s->nchars <= end - start);
25765 s->width += glyph->pixel_width;
25766 if (glyph++->padding_p != s->padding_p)
25767 break;
25770 s->font = s->face->font;
25772 /* If the specified font could not be loaded, use the frame's font,
25773 but record the fact that we couldn't load it in
25774 S->font_not_found_p so that we can draw rectangles for the
25775 characters of the glyph string. */
25776 if (s->font == NULL || glyph_not_available_p)
25778 s->font_not_found_p = true;
25779 s->font = FRAME_FONT (s->f);
25782 /* Adjust base line for subscript/superscript text. */
25783 s->ybase += voffset;
25785 eassert (s->face && s->face->gc);
25786 return glyph - s->row->glyphs[s->area];
25790 /* Fill glyph string S from image glyph S->first_glyph. */
25792 static void
25793 fill_image_glyph_string (struct glyph_string *s)
25795 eassert (s->first_glyph->type == IMAGE_GLYPH);
25796 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25797 eassert (s->img);
25798 s->slice = s->first_glyph->slice.img;
25799 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25800 s->font = s->face->font;
25801 s->width = s->first_glyph->pixel_width;
25803 /* Adjust base line for subscript/superscript text. */
25804 s->ybase += s->first_glyph->voffset;
25808 #ifdef HAVE_XWIDGETS
25809 static void
25810 fill_xwidget_glyph_string (struct glyph_string *s)
25812 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25813 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25814 s->font = s->face->font;
25815 s->width = s->first_glyph->pixel_width;
25816 s->ybase += s->first_glyph->voffset;
25817 s->xwidget = s->first_glyph->u.xwidget;
25819 #endif
25820 /* Fill glyph string S from a sequence of stretch glyphs.
25822 START is the index of the first glyph to consider,
25823 END is the index of the last + 1.
25825 Value is the index of the first glyph not in S. */
25827 static int
25828 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25830 struct glyph *glyph, *last;
25831 int voffset, face_id;
25833 eassert (s->first_glyph->type == STRETCH_GLYPH);
25835 glyph = s->row->glyphs[s->area] + start;
25836 last = s->row->glyphs[s->area] + end;
25837 face_id = glyph->face_id;
25838 s->face = FACE_FROM_ID (s->f, face_id);
25839 s->font = s->face->font;
25840 s->width = glyph->pixel_width;
25841 s->nchars = 1;
25842 voffset = glyph->voffset;
25844 for (++glyph;
25845 (glyph < last
25846 && glyph->type == STRETCH_GLYPH
25847 && glyph->voffset == voffset
25848 && glyph->face_id == face_id);
25849 ++glyph)
25850 s->width += glyph->pixel_width;
25852 /* Adjust base line for subscript/superscript text. */
25853 s->ybase += voffset;
25855 /* The case that face->gc == 0 is handled when drawing the glyph
25856 string by calling prepare_face_for_display. */
25857 eassert (s->face);
25858 return glyph - s->row->glyphs[s->area];
25861 static struct font_metrics *
25862 get_per_char_metric (struct font *font, XChar2b *char2b)
25864 static struct font_metrics metrics;
25865 unsigned code;
25867 if (! font)
25868 return NULL;
25869 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25870 if (code == FONT_INVALID_CODE)
25871 return NULL;
25872 font->driver->text_extents (font, &code, 1, &metrics);
25873 return &metrics;
25876 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25877 for FONT. Values are taken from font-global ones, except for fonts
25878 that claim preposterously large values, but whose glyphs actually
25879 have reasonable dimensions. C is the character to use for metrics
25880 if the font-global values are too large; if C is negative, the
25881 function selects a default character. */
25882 static void
25883 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25885 *ascent = FONT_BASE (font);
25886 *descent = FONT_DESCENT (font);
25888 if (FONT_TOO_HIGH (font))
25890 XChar2b char2b;
25892 /* Get metrics of C, defaulting to a reasonably sized ASCII
25893 character. */
25894 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25896 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25898 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25900 /* We add 1 pixel to character dimensions as heuristics
25901 that produces nicer display, e.g. when the face has
25902 the box attribute. */
25903 *ascent = pcm->ascent + 1;
25904 *descent = pcm->descent + 1;
25910 /* A subroutine that computes a reasonable "normal character height"
25911 for fonts that claim preposterously large vertical dimensions, but
25912 whose glyphs are actually reasonably sized. C is the character
25913 whose metrics to use for those fonts, or -1 for default
25914 character. */
25915 static int
25916 normal_char_height (struct font *font, int c)
25918 int ascent, descent;
25920 normal_char_ascent_descent (font, c, &ascent, &descent);
25922 return ascent + descent;
25925 /* EXPORT for RIF:
25926 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25927 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25928 assumed to be zero. */
25930 void
25931 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25933 *left = *right = 0;
25935 if (glyph->type == CHAR_GLYPH)
25937 XChar2b char2b;
25938 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25939 if (face->font)
25941 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25942 if (pcm)
25944 if (pcm->rbearing > pcm->width)
25945 *right = pcm->rbearing - pcm->width;
25946 if (pcm->lbearing < 0)
25947 *left = -pcm->lbearing;
25951 else if (glyph->type == COMPOSITE_GLYPH)
25953 if (! glyph->u.cmp.automatic)
25955 struct composition *cmp = composition_table[glyph->u.cmp.id];
25957 if (cmp->rbearing > cmp->pixel_width)
25958 *right = cmp->rbearing - cmp->pixel_width;
25959 if (cmp->lbearing < 0)
25960 *left = - cmp->lbearing;
25962 else
25964 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25965 struct font_metrics metrics;
25967 composition_gstring_width (gstring, glyph->slice.cmp.from,
25968 glyph->slice.cmp.to + 1, &metrics);
25969 if (metrics.rbearing > metrics.width)
25970 *right = metrics.rbearing - metrics.width;
25971 if (metrics.lbearing < 0)
25972 *left = - metrics.lbearing;
25978 /* Return the index of the first glyph preceding glyph string S that
25979 is overwritten by S because of S's left overhang. Value is -1
25980 if no glyphs are overwritten. */
25982 static int
25983 left_overwritten (struct glyph_string *s)
25985 int k;
25987 if (s->left_overhang)
25989 int x = 0, i;
25990 struct glyph *glyphs = s->row->glyphs[s->area];
25991 int first = s->first_glyph - glyphs;
25993 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25994 x -= glyphs[i].pixel_width;
25996 k = i + 1;
25998 else
25999 k = -1;
26001 return k;
26005 /* Return the index of the first glyph preceding glyph string S that
26006 is overwriting S because of its right overhang. Value is -1 if no
26007 glyph in front of S overwrites S. */
26009 static int
26010 left_overwriting (struct glyph_string *s)
26012 int i, k, x;
26013 struct glyph *glyphs = s->row->glyphs[s->area];
26014 int first = s->first_glyph - glyphs;
26016 k = -1;
26017 x = 0;
26018 for (i = first - 1; i >= 0; --i)
26020 int left, right;
26021 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26022 if (x + right > 0)
26023 k = i;
26024 x -= glyphs[i].pixel_width;
26027 return k;
26031 /* Return the index of the last glyph following glyph string S that is
26032 overwritten by S because of S's right overhang. Value is -1 if
26033 no such glyph is found. */
26035 static int
26036 right_overwritten (struct glyph_string *s)
26038 int k = -1;
26040 if (s->right_overhang)
26042 int x = 0, i;
26043 struct glyph *glyphs = s->row->glyphs[s->area];
26044 int first = (s->first_glyph - glyphs
26045 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26046 int end = s->row->used[s->area];
26048 for (i = first; i < end && s->right_overhang > x; ++i)
26049 x += glyphs[i].pixel_width;
26051 k = i;
26054 return k;
26058 /* Return the index of the last glyph following glyph string S that
26059 overwrites S because of its left overhang. Value is negative
26060 if no such glyph is found. */
26062 static int
26063 right_overwriting (struct glyph_string *s)
26065 int i, k, x;
26066 int end = s->row->used[s->area];
26067 struct glyph *glyphs = s->row->glyphs[s->area];
26068 int first = (s->first_glyph - glyphs
26069 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26071 k = -1;
26072 x = 0;
26073 for (i = first; i < end; ++i)
26075 int left, right;
26076 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26077 if (x - left < 0)
26078 k = i;
26079 x += glyphs[i].pixel_width;
26082 return k;
26086 /* Set background width of glyph string S. START is the index of the
26087 first glyph following S. LAST_X is the right-most x-position + 1
26088 in the drawing area. */
26090 static void
26091 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26093 /* If the face of this glyph string has to be drawn to the end of
26094 the drawing area, set S->extends_to_end_of_line_p. */
26096 if (start == s->row->used[s->area]
26097 && ((s->row->fill_line_p
26098 && (s->hl == DRAW_NORMAL_TEXT
26099 || s->hl == DRAW_IMAGE_RAISED
26100 || s->hl == DRAW_IMAGE_SUNKEN))
26101 || s->hl == DRAW_MOUSE_FACE))
26102 s->extends_to_end_of_line_p = true;
26104 /* If S extends its face to the end of the line, set its
26105 background_width to the distance to the right edge of the drawing
26106 area. */
26107 if (s->extends_to_end_of_line_p)
26108 s->background_width = last_x - s->x + 1;
26109 else
26110 s->background_width = s->width;
26114 /* Return glyph string that shares background with glyph string S and
26115 whose `background_width' member has been set. */
26117 static struct glyph_string *
26118 glyph_string_containing_background_width (struct glyph_string *s)
26120 if (s->cmp)
26121 while (s->cmp_from)
26122 s = s->prev;
26124 return s;
26128 /* Compute overhangs and x-positions for glyph string S and its
26129 predecessors, or successors. X is the starting x-position for S.
26130 BACKWARD_P means process predecessors. */
26132 static void
26133 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26135 if (backward_p)
26137 while (s)
26139 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26140 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26141 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26142 x -= s->width;
26143 s->x = x;
26144 s = s->prev;
26147 else
26149 while (s)
26151 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26152 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26153 s->x = x;
26154 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26155 x += s->width;
26156 s = s->next;
26163 /* The following macros are only called from draw_glyphs below.
26164 They reference the following parameters of that function directly:
26165 `w', `row', `area', and `overlap_p'
26166 as well as the following local variables:
26167 `s', `f', and `hdc' (in W32) */
26169 #ifdef HAVE_NTGUI
26170 /* On W32, silently add local `hdc' variable to argument list of
26171 init_glyph_string. */
26172 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26173 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26174 #else
26175 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26176 init_glyph_string (s, char2b, w, row, area, start, hl)
26177 #endif
26179 /* Add a glyph string for a stretch glyph to the list of strings
26180 between HEAD and TAIL. START is the index of the stretch glyph in
26181 row area AREA of glyph row ROW. END is the index of the last glyph
26182 in that glyph row area. X is the current output position assigned
26183 to the new glyph string constructed. HL overrides that face of the
26184 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26185 is the right-most x-position of the drawing area. */
26187 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26188 and below -- keep them on one line. */
26189 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26190 do \
26192 s = alloca (sizeof *s); \
26193 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26194 START = fill_stretch_glyph_string (s, START, END); \
26195 append_glyph_string (&HEAD, &TAIL, s); \
26196 s->x = (X); \
26198 while (false)
26201 /* Add a glyph string for an image glyph to the list of strings
26202 between HEAD and TAIL. START is the index of the image glyph in
26203 row area AREA of glyph row ROW. END is the index of the last glyph
26204 in that glyph row area. X is the current output position assigned
26205 to the new glyph string constructed. HL overrides that face of the
26206 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26207 is the right-most x-position of the drawing area. */
26209 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26210 do \
26212 s = alloca (sizeof *s); \
26213 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26214 fill_image_glyph_string (s); \
26215 append_glyph_string (&HEAD, &TAIL, s); \
26216 ++START; \
26217 s->x = (X); \
26219 while (false)
26221 #ifndef HAVE_XWIDGETS
26222 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26223 eassume (false)
26224 #else
26225 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26226 do \
26228 s = alloca (sizeof *s); \
26229 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26230 fill_xwidget_glyph_string (s); \
26231 append_glyph_string (&(HEAD), &(TAIL), s); \
26232 ++(START); \
26233 s->x = (X); \
26235 while (false)
26236 #endif
26238 /* Add a glyph string for a sequence of character glyphs to the list
26239 of strings between HEAD and TAIL. START is the index of the first
26240 glyph in row area AREA of glyph row ROW that is part of the new
26241 glyph string. END is the index of the last glyph in that glyph row
26242 area. X is the current output position assigned to the new glyph
26243 string constructed. HL overrides that face of the glyph; e.g. it
26244 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26245 right-most x-position of the drawing area. */
26247 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26248 do \
26250 int face_id; \
26251 XChar2b *char2b; \
26253 face_id = (row)->glyphs[area][START].face_id; \
26255 s = alloca (sizeof *s); \
26256 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26257 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26258 append_glyph_string (&HEAD, &TAIL, s); \
26259 s->x = (X); \
26260 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26262 while (false)
26265 /* Add a glyph string for a composite sequence to the list of strings
26266 between HEAD and TAIL. START is the index of the first glyph in
26267 row area AREA of glyph row ROW that is part of the new glyph
26268 string. END is the index of the last glyph in that glyph row area.
26269 X is the current output position assigned to the new glyph string
26270 constructed. HL overrides that face of the glyph; e.g. it is
26271 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26272 x-position of the drawing area. */
26274 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26275 do { \
26276 int face_id = (row)->glyphs[area][START].face_id; \
26277 struct face *base_face = FACE_FROM_ID (f, face_id); \
26278 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26279 struct composition *cmp = composition_table[cmp_id]; \
26280 XChar2b *char2b; \
26281 struct glyph_string *first_s = NULL; \
26282 int n; \
26284 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26286 /* Make glyph_strings for each glyph sequence that is drawable by \
26287 the same face, and append them to HEAD/TAIL. */ \
26288 for (n = 0; n < cmp->glyph_len;) \
26290 s = alloca (sizeof *s); \
26291 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26292 append_glyph_string (&(HEAD), &(TAIL), s); \
26293 s->cmp = cmp; \
26294 s->cmp_from = n; \
26295 s->x = (X); \
26296 if (n == 0) \
26297 first_s = s; \
26298 n = fill_composite_glyph_string (s, base_face, overlaps); \
26301 ++START; \
26302 s = first_s; \
26303 } while (false)
26306 /* Add a glyph string for a glyph-string sequence to the list of strings
26307 between HEAD and TAIL. */
26309 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26310 do { \
26311 int face_id; \
26312 XChar2b *char2b; \
26313 Lisp_Object gstring; \
26315 face_id = (row)->glyphs[area][START].face_id; \
26316 gstring = (composition_gstring_from_id \
26317 ((row)->glyphs[area][START].u.cmp.id)); \
26318 s = alloca (sizeof *s); \
26319 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26320 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26321 append_glyph_string (&(HEAD), &(TAIL), s); \
26322 s->x = (X); \
26323 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26324 } while (false)
26327 /* Add a glyph string for a sequence of glyphless character's glyphs
26328 to the list of strings between HEAD and TAIL. The meanings of
26329 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26331 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26332 do \
26334 int face_id; \
26336 face_id = (row)->glyphs[area][START].face_id; \
26338 s = alloca (sizeof *s); \
26339 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26340 append_glyph_string (&HEAD, &TAIL, s); \
26341 s->x = (X); \
26342 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26343 overlaps); \
26345 while (false)
26348 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26349 of AREA of glyph row ROW on window W between indices START and END.
26350 HL overrides the face for drawing glyph strings, e.g. it is
26351 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26352 x-positions of the drawing area.
26354 This is an ugly monster macro construct because we must use alloca
26355 to allocate glyph strings (because draw_glyphs can be called
26356 asynchronously). */
26358 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26359 do \
26361 HEAD = TAIL = NULL; \
26362 while (START < END) \
26364 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26365 switch (first_glyph->type) \
26367 case CHAR_GLYPH: \
26368 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26369 HL, X, LAST_X); \
26370 break; \
26372 case COMPOSITE_GLYPH: \
26373 if (first_glyph->u.cmp.automatic) \
26374 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26375 HL, X, LAST_X); \
26376 else \
26377 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26378 HL, X, LAST_X); \
26379 break; \
26381 case STRETCH_GLYPH: \
26382 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26383 HL, X, LAST_X); \
26384 break; \
26386 case IMAGE_GLYPH: \
26387 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26388 HL, X, LAST_X); \
26389 break;
26391 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26392 case XWIDGET_GLYPH: \
26393 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26394 HL, X, LAST_X); \
26395 break;
26397 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26398 case GLYPHLESS_GLYPH: \
26399 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26400 HL, X, LAST_X); \
26401 break; \
26403 default: \
26404 emacs_abort (); \
26407 if (s) \
26409 set_glyph_string_background_width (s, START, LAST_X); \
26410 (X) += s->width; \
26413 } while (false)
26416 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26417 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26418 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26419 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26422 /* Draw glyphs between START and END in AREA of ROW on window W,
26423 starting at x-position X. X is relative to AREA in W. HL is a
26424 face-override with the following meaning:
26426 DRAW_NORMAL_TEXT draw normally
26427 DRAW_CURSOR draw in cursor face
26428 DRAW_MOUSE_FACE draw in mouse face.
26429 DRAW_INVERSE_VIDEO draw in mode line face
26430 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26431 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26433 If OVERLAPS is non-zero, draw only the foreground of characters and
26434 clip to the physical height of ROW. Non-zero value also defines
26435 the overlapping part to be drawn:
26437 OVERLAPS_PRED overlap with preceding rows
26438 OVERLAPS_SUCC overlap with succeeding rows
26439 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26440 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26442 Value is the x-position reached, relative to AREA of W. */
26444 static int
26445 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26446 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26447 enum draw_glyphs_face hl, int overlaps)
26449 struct glyph_string *head, *tail;
26450 struct glyph_string *s;
26451 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26452 int i, j, x_reached, last_x, area_left = 0;
26453 struct frame *f = XFRAME (WINDOW_FRAME (w));
26454 DECLARE_HDC (hdc);
26456 ALLOCATE_HDC (hdc, f);
26458 /* Let's rather be paranoid than getting a SEGV. */
26459 end = min (end, row->used[area]);
26460 start = clip_to_bounds (0, start, end);
26462 /* Translate X to frame coordinates. Set last_x to the right
26463 end of the drawing area. */
26464 if (row->full_width_p)
26466 /* X is relative to the left edge of W, without scroll bars
26467 or fringes. */
26468 area_left = WINDOW_LEFT_EDGE_X (w);
26469 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26470 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26472 else
26474 area_left = window_box_left (w, area);
26475 last_x = area_left + window_box_width (w, area);
26477 x += area_left;
26479 /* Build a doubly-linked list of glyph_string structures between
26480 head and tail from what we have to draw. Note that the macro
26481 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26482 the reason we use a separate variable `i'. */
26483 i = start;
26484 USE_SAFE_ALLOCA;
26485 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26486 if (tail)
26488 s = glyph_string_containing_background_width (tail);
26489 x_reached = s->x + s->background_width;
26491 else
26492 x_reached = x;
26494 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26495 the row, redraw some glyphs in front or following the glyph
26496 strings built above. */
26497 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26499 struct glyph_string *h, *t;
26500 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26501 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26502 bool check_mouse_face = false;
26503 int dummy_x = 0;
26505 /* If mouse highlighting is on, we may need to draw adjacent
26506 glyphs using mouse-face highlighting. */
26507 if (area == TEXT_AREA && row->mouse_face_p
26508 && hlinfo->mouse_face_beg_row >= 0
26509 && hlinfo->mouse_face_end_row >= 0)
26511 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26513 if (row_vpos >= hlinfo->mouse_face_beg_row
26514 && row_vpos <= hlinfo->mouse_face_end_row)
26516 check_mouse_face = true;
26517 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26518 ? hlinfo->mouse_face_beg_col : 0;
26519 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26520 ? hlinfo->mouse_face_end_col
26521 : row->used[TEXT_AREA];
26525 /* Compute overhangs for all glyph strings. */
26526 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26527 for (s = head; s; s = s->next)
26528 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26530 /* Prepend glyph strings for glyphs in front of the first glyph
26531 string that are overwritten because of the first glyph
26532 string's left overhang. The background of all strings
26533 prepended must be drawn because the first glyph string
26534 draws over it. */
26535 i = left_overwritten (head);
26536 if (i >= 0)
26538 enum draw_glyphs_face overlap_hl;
26540 /* If this row contains mouse highlighting, attempt to draw
26541 the overlapped glyphs with the correct highlight. This
26542 code fails if the overlap encompasses more than one glyph
26543 and mouse-highlight spans only some of these glyphs.
26544 However, making it work perfectly involves a lot more
26545 code, and I don't know if the pathological case occurs in
26546 practice, so we'll stick to this for now. --- cyd */
26547 if (check_mouse_face
26548 && mouse_beg_col < start && mouse_end_col > i)
26549 overlap_hl = DRAW_MOUSE_FACE;
26550 else
26551 overlap_hl = DRAW_NORMAL_TEXT;
26553 if (hl != overlap_hl)
26554 clip_head = head;
26555 j = i;
26556 BUILD_GLYPH_STRINGS (j, start, h, t,
26557 overlap_hl, dummy_x, last_x);
26558 start = i;
26559 compute_overhangs_and_x (t, head->x, true);
26560 prepend_glyph_string_lists (&head, &tail, h, t);
26561 if (clip_head == NULL)
26562 clip_head = head;
26565 /* Prepend glyph strings for glyphs in front of the first glyph
26566 string that overwrite that glyph string because of their
26567 right overhang. For these strings, only the foreground must
26568 be drawn, because it draws over the glyph string at `head'.
26569 The background must not be drawn because this would overwrite
26570 right overhangs of preceding glyphs for which no glyph
26571 strings exist. */
26572 i = left_overwriting (head);
26573 if (i >= 0)
26575 enum draw_glyphs_face overlap_hl;
26577 if (check_mouse_face
26578 && mouse_beg_col < start && mouse_end_col > i)
26579 overlap_hl = DRAW_MOUSE_FACE;
26580 else
26581 overlap_hl = DRAW_NORMAL_TEXT;
26583 if (hl == overlap_hl || clip_head == NULL)
26584 clip_head = head;
26585 BUILD_GLYPH_STRINGS (i, start, h, t,
26586 overlap_hl, dummy_x, last_x);
26587 for (s = h; s; s = s->next)
26588 s->background_filled_p = true;
26589 compute_overhangs_and_x (t, head->x, true);
26590 prepend_glyph_string_lists (&head, &tail, h, t);
26593 /* Append glyphs strings for glyphs following the last glyph
26594 string tail that are overwritten by tail. The background of
26595 these strings has to be drawn because tail's foreground draws
26596 over it. */
26597 i = right_overwritten (tail);
26598 if (i >= 0)
26600 enum draw_glyphs_face overlap_hl;
26602 if (check_mouse_face
26603 && mouse_beg_col < i && mouse_end_col > end)
26604 overlap_hl = DRAW_MOUSE_FACE;
26605 else
26606 overlap_hl = DRAW_NORMAL_TEXT;
26608 if (hl != overlap_hl)
26609 clip_tail = tail;
26610 BUILD_GLYPH_STRINGS (end, i, h, t,
26611 overlap_hl, x, last_x);
26612 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26613 we don't have `end = i;' here. */
26614 compute_overhangs_and_x (h, tail->x + tail->width, false);
26615 append_glyph_string_lists (&head, &tail, h, t);
26616 if (clip_tail == NULL)
26617 clip_tail = tail;
26620 /* Append glyph strings for glyphs following the last glyph
26621 string tail that overwrite tail. The foreground of such
26622 glyphs has to be drawn because it writes into the background
26623 of tail. The background must not be drawn because it could
26624 paint over the foreground of following glyphs. */
26625 i = right_overwriting (tail);
26626 if (i >= 0)
26628 enum draw_glyphs_face overlap_hl;
26629 if (check_mouse_face
26630 && mouse_beg_col < i && mouse_end_col > end)
26631 overlap_hl = DRAW_MOUSE_FACE;
26632 else
26633 overlap_hl = DRAW_NORMAL_TEXT;
26635 if (hl == overlap_hl || clip_tail == NULL)
26636 clip_tail = tail;
26637 i++; /* We must include the Ith glyph. */
26638 BUILD_GLYPH_STRINGS (end, i, h, t,
26639 overlap_hl, x, last_x);
26640 for (s = h; s; s = s->next)
26641 s->background_filled_p = true;
26642 compute_overhangs_and_x (h, tail->x + tail->width, false);
26643 append_glyph_string_lists (&head, &tail, h, t);
26645 tail = glyph_string_containing_background_width (tail);
26646 if (clip_tail)
26647 clip_tail = glyph_string_containing_background_width (clip_tail);
26648 if (clip_head || clip_tail)
26649 for (s = head; s; s = s->next)
26651 s->clip_head = clip_head;
26652 s->clip_tail = clip_tail;
26656 /* Draw all strings. */
26657 for (s = head; s; s = s->next)
26658 FRAME_RIF (f)->draw_glyph_string (s);
26660 #ifndef HAVE_NS
26661 /* When focus a sole frame and move horizontally, this clears on_p
26662 causing a failure to erase prev cursor position. */
26663 if (area == TEXT_AREA
26664 && !row->full_width_p
26665 /* When drawing overlapping rows, only the glyph strings'
26666 foreground is drawn, which doesn't erase a cursor
26667 completely. */
26668 && !overlaps)
26670 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26671 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26672 : (tail ? tail->x + tail->background_width : x));
26673 x0 -= area_left;
26674 x1 -= area_left;
26676 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26677 row->y, MATRIX_ROW_BOTTOM_Y (row));
26679 #endif
26681 /* Value is the x-position up to which drawn, relative to AREA of W.
26682 This doesn't include parts drawn because of overhangs. */
26683 if (row->full_width_p)
26684 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26685 else
26686 x_reached -= area_left;
26688 RELEASE_HDC (hdc, f);
26690 SAFE_FREE ();
26691 return x_reached;
26694 /* Find the first glyph in the run of underlined glyphs preceding the
26695 beginning of glyph string S, and return its font (which could be
26696 NULL). This is needed because that font determines the underline
26697 position and thickness for the entire run of the underlined glyphs.
26698 This function is called from the draw_glyph_string method of GUI
26699 frame's redisplay interface (RIF) when it needs to draw in an
26700 underlined face. */
26701 struct font *
26702 font_for_underline_metrics (struct glyph_string *s)
26704 struct glyph *g0 = s->row->glyphs[s->area], *g;
26706 for (g = s->first_glyph - 1; g >= g0; g--)
26708 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26709 if (!(prev_face && prev_face->underline_p))
26710 break;
26713 /* If preceding glyphs are not underlined, use the font of S. */
26714 if (g == s->first_glyph - 1)
26715 return s->font;
26716 else
26718 /* Otherwise use the font of the last glyph we saw in the above
26719 loop whose face had the underline_p flag set. */
26720 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26724 /* Expand row matrix if too narrow. Don't expand if area
26725 is not present. */
26727 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26729 if (!it->f->fonts_changed \
26730 && (it->glyph_row->glyphs[area] \
26731 < it->glyph_row->glyphs[area + 1])) \
26733 it->w->ncols_scale_factor++; \
26734 it->f->fonts_changed = true; \
26738 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26739 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26741 static void
26742 append_glyph (struct it *it)
26744 struct glyph *glyph;
26745 enum glyph_row_area area = it->area;
26747 eassert (it->glyph_row);
26748 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26750 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26751 if (glyph < it->glyph_row->glyphs[area + 1])
26753 /* If the glyph row is reversed, we need to prepend the glyph
26754 rather than append it. */
26755 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26757 struct glyph *g;
26759 /* Make room for the additional glyph. */
26760 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26761 g[1] = *g;
26762 glyph = it->glyph_row->glyphs[area];
26764 glyph->charpos = CHARPOS (it->position);
26765 glyph->object = it->object;
26766 if (it->pixel_width > 0)
26768 eassert (it->pixel_width <= SHRT_MAX);
26769 glyph->pixel_width = it->pixel_width;
26770 glyph->padding_p = false;
26772 else
26774 /* Assure at least 1-pixel width. Otherwise, cursor can't
26775 be displayed correctly. */
26776 glyph->pixel_width = 1;
26777 glyph->padding_p = true;
26779 glyph->ascent = it->ascent;
26780 glyph->descent = it->descent;
26781 glyph->voffset = it->voffset;
26782 glyph->type = CHAR_GLYPH;
26783 glyph->avoid_cursor_p = it->avoid_cursor_p;
26784 glyph->multibyte_p = it->multibyte_p;
26785 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26787 /* In R2L rows, the left and the right box edges need to be
26788 drawn in reverse direction. */
26789 glyph->right_box_line_p = it->start_of_box_run_p;
26790 glyph->left_box_line_p = it->end_of_box_run_p;
26792 else
26794 glyph->left_box_line_p = it->start_of_box_run_p;
26795 glyph->right_box_line_p = it->end_of_box_run_p;
26797 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26798 || it->phys_descent > it->descent);
26799 glyph->glyph_not_available_p = it->glyph_not_available_p;
26800 glyph->face_id = it->face_id;
26801 glyph->u.ch = it->char_to_display;
26802 glyph->slice.img = null_glyph_slice;
26803 glyph->font_type = FONT_TYPE_UNKNOWN;
26804 if (it->bidi_p)
26806 glyph->resolved_level = it->bidi_it.resolved_level;
26807 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26808 glyph->bidi_type = it->bidi_it.type;
26810 else
26812 glyph->resolved_level = 0;
26813 glyph->bidi_type = UNKNOWN_BT;
26815 ++it->glyph_row->used[area];
26817 else
26818 IT_EXPAND_MATRIX_WIDTH (it, area);
26821 /* Store one glyph for the composition IT->cmp_it.id in
26822 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26823 non-null. */
26825 static void
26826 append_composite_glyph (struct it *it)
26828 struct glyph *glyph;
26829 enum glyph_row_area area = it->area;
26831 eassert (it->glyph_row);
26833 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26834 if (glyph < it->glyph_row->glyphs[area + 1])
26836 /* If the glyph row is reversed, we need to prepend the glyph
26837 rather than append it. */
26838 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26840 struct glyph *g;
26842 /* Make room for the new glyph. */
26843 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26844 g[1] = *g;
26845 glyph = it->glyph_row->glyphs[it->area];
26847 glyph->charpos = it->cmp_it.charpos;
26848 glyph->object = it->object;
26849 eassert (it->pixel_width <= SHRT_MAX);
26850 glyph->pixel_width = it->pixel_width;
26851 glyph->ascent = it->ascent;
26852 glyph->descent = it->descent;
26853 glyph->voffset = it->voffset;
26854 glyph->type = COMPOSITE_GLYPH;
26855 if (it->cmp_it.ch < 0)
26857 glyph->u.cmp.automatic = false;
26858 glyph->u.cmp.id = it->cmp_it.id;
26859 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26861 else
26863 glyph->u.cmp.automatic = true;
26864 glyph->u.cmp.id = it->cmp_it.id;
26865 glyph->slice.cmp.from = it->cmp_it.from;
26866 glyph->slice.cmp.to = it->cmp_it.to - 1;
26868 glyph->avoid_cursor_p = it->avoid_cursor_p;
26869 glyph->multibyte_p = it->multibyte_p;
26870 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26872 /* In R2L rows, the left and the right box edges need to be
26873 drawn in reverse direction. */
26874 glyph->right_box_line_p = it->start_of_box_run_p;
26875 glyph->left_box_line_p = it->end_of_box_run_p;
26877 else
26879 glyph->left_box_line_p = it->start_of_box_run_p;
26880 glyph->right_box_line_p = it->end_of_box_run_p;
26882 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26883 || it->phys_descent > it->descent);
26884 glyph->padding_p = false;
26885 glyph->glyph_not_available_p = false;
26886 glyph->face_id = it->face_id;
26887 glyph->font_type = FONT_TYPE_UNKNOWN;
26888 if (it->bidi_p)
26890 glyph->resolved_level = it->bidi_it.resolved_level;
26891 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26892 glyph->bidi_type = it->bidi_it.type;
26894 ++it->glyph_row->used[area];
26896 else
26897 IT_EXPAND_MATRIX_WIDTH (it, area);
26901 /* Change IT->ascent and IT->height according to the setting of
26902 IT->voffset. */
26904 static void
26905 take_vertical_position_into_account (struct it *it)
26907 if (it->voffset)
26909 if (it->voffset < 0)
26910 /* Increase the ascent so that we can display the text higher
26911 in the line. */
26912 it->ascent -= it->voffset;
26913 else
26914 /* Increase the descent so that we can display the text lower
26915 in the line. */
26916 it->descent += it->voffset;
26921 /* Produce glyphs/get display metrics for the image IT is loaded with.
26922 See the description of struct display_iterator in dispextern.h for
26923 an overview of struct display_iterator. */
26925 static void
26926 produce_image_glyph (struct it *it)
26928 struct image *img;
26929 struct face *face;
26930 int glyph_ascent, crop;
26931 struct glyph_slice slice;
26933 eassert (it->what == IT_IMAGE);
26935 face = FACE_FROM_ID (it->f, it->face_id);
26936 /* Make sure X resources of the face is loaded. */
26937 prepare_face_for_display (it->f, face);
26939 if (it->image_id < 0)
26941 /* Fringe bitmap. */
26942 it->ascent = it->phys_ascent = 0;
26943 it->descent = it->phys_descent = 0;
26944 it->pixel_width = 0;
26945 it->nglyphs = 0;
26946 return;
26949 img = IMAGE_FROM_ID (it->f, it->image_id);
26950 /* Make sure X resources of the image is loaded. */
26951 prepare_image_for_display (it->f, img);
26953 slice.x = slice.y = 0;
26954 slice.width = img->width;
26955 slice.height = img->height;
26957 if (INTEGERP (it->slice.x))
26958 slice.x = XINT (it->slice.x);
26959 else if (FLOATP (it->slice.x))
26960 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26962 if (INTEGERP (it->slice.y))
26963 slice.y = XINT (it->slice.y);
26964 else if (FLOATP (it->slice.y))
26965 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26967 if (INTEGERP (it->slice.width))
26968 slice.width = XINT (it->slice.width);
26969 else if (FLOATP (it->slice.width))
26970 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26972 if (INTEGERP (it->slice.height))
26973 slice.height = XINT (it->slice.height);
26974 else if (FLOATP (it->slice.height))
26975 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26977 if (slice.x >= img->width)
26978 slice.x = img->width;
26979 if (slice.y >= img->height)
26980 slice.y = img->height;
26981 if (slice.x + slice.width >= img->width)
26982 slice.width = img->width - slice.x;
26983 if (slice.y + slice.height > img->height)
26984 slice.height = img->height - slice.y;
26986 if (slice.width == 0 || slice.height == 0)
26987 return;
26989 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26991 it->descent = slice.height - glyph_ascent;
26992 if (slice.y == 0)
26993 it->descent += img->vmargin;
26994 if (slice.y + slice.height == img->height)
26995 it->descent += img->vmargin;
26996 it->phys_descent = it->descent;
26998 it->pixel_width = slice.width;
26999 if (slice.x == 0)
27000 it->pixel_width += img->hmargin;
27001 if (slice.x + slice.width == img->width)
27002 it->pixel_width += img->hmargin;
27004 /* It's quite possible for images to have an ascent greater than
27005 their height, so don't get confused in that case. */
27006 if (it->descent < 0)
27007 it->descent = 0;
27009 it->nglyphs = 1;
27011 if (face->box != FACE_NO_BOX)
27013 if (face->box_line_width > 0)
27015 if (slice.y == 0)
27016 it->ascent += face->box_line_width;
27017 if (slice.y + slice.height == img->height)
27018 it->descent += face->box_line_width;
27021 if (it->start_of_box_run_p && slice.x == 0)
27022 it->pixel_width += eabs (face->box_line_width);
27023 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27024 it->pixel_width += eabs (face->box_line_width);
27027 take_vertical_position_into_account (it);
27029 /* Automatically crop wide image glyphs at right edge so we can
27030 draw the cursor on same display row. */
27031 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27032 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27034 it->pixel_width -= crop;
27035 slice.width -= crop;
27038 if (it->glyph_row)
27040 struct glyph *glyph;
27041 enum glyph_row_area area = it->area;
27043 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27044 if (it->glyph_row->reversed_p)
27046 struct glyph *g;
27048 /* Make room for the new glyph. */
27049 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27050 g[1] = *g;
27051 glyph = it->glyph_row->glyphs[it->area];
27053 if (glyph < it->glyph_row->glyphs[area + 1])
27055 glyph->charpos = CHARPOS (it->position);
27056 glyph->object = it->object;
27057 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27058 glyph->ascent = glyph_ascent;
27059 glyph->descent = it->descent;
27060 glyph->voffset = it->voffset;
27061 glyph->type = IMAGE_GLYPH;
27062 glyph->avoid_cursor_p = it->avoid_cursor_p;
27063 glyph->multibyte_p = it->multibyte_p;
27064 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27066 /* In R2L rows, the left and the right box edges need to be
27067 drawn in reverse direction. */
27068 glyph->right_box_line_p = it->start_of_box_run_p;
27069 glyph->left_box_line_p = it->end_of_box_run_p;
27071 else
27073 glyph->left_box_line_p = it->start_of_box_run_p;
27074 glyph->right_box_line_p = it->end_of_box_run_p;
27076 glyph->overlaps_vertically_p = false;
27077 glyph->padding_p = false;
27078 glyph->glyph_not_available_p = false;
27079 glyph->face_id = it->face_id;
27080 glyph->u.img_id = img->id;
27081 glyph->slice.img = slice;
27082 glyph->font_type = FONT_TYPE_UNKNOWN;
27083 if (it->bidi_p)
27085 glyph->resolved_level = it->bidi_it.resolved_level;
27086 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27087 glyph->bidi_type = it->bidi_it.type;
27089 ++it->glyph_row->used[area];
27091 else
27092 IT_EXPAND_MATRIX_WIDTH (it, area);
27096 static void
27097 produce_xwidget_glyph (struct it *it)
27099 #ifdef HAVE_XWIDGETS
27100 struct xwidget *xw;
27101 int glyph_ascent, crop;
27102 eassert (it->what == IT_XWIDGET);
27104 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27105 /* Make sure X resources of the face is loaded. */
27106 prepare_face_for_display (it->f, face);
27108 xw = it->xwidget;
27109 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27110 it->descent = xw->height/2;
27111 it->phys_descent = it->descent;
27112 it->pixel_width = xw->width;
27113 /* It's quite possible for images to have an ascent greater than
27114 their height, so don't get confused in that case. */
27115 if (it->descent < 0)
27116 it->descent = 0;
27118 it->nglyphs = 1;
27120 if (face->box != FACE_NO_BOX)
27122 if (face->box_line_width > 0)
27124 it->ascent += face->box_line_width;
27125 it->descent += face->box_line_width;
27128 if (it->start_of_box_run_p)
27129 it->pixel_width += eabs (face->box_line_width);
27130 it->pixel_width += eabs (face->box_line_width);
27133 take_vertical_position_into_account (it);
27135 /* Automatically crop wide image glyphs at right edge so we can
27136 draw the cursor on same display row. */
27137 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27138 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27139 it->pixel_width -= crop;
27141 if (it->glyph_row)
27143 enum glyph_row_area area = it->area;
27144 struct glyph *glyph
27145 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27147 if (it->glyph_row->reversed_p)
27149 struct glyph *g;
27151 /* Make room for the new glyph. */
27152 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27153 g[1] = *g;
27154 glyph = it->glyph_row->glyphs[it->area];
27156 if (glyph < it->glyph_row->glyphs[area + 1])
27158 glyph->charpos = CHARPOS (it->position);
27159 glyph->object = it->object;
27160 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27161 glyph->ascent = glyph_ascent;
27162 glyph->descent = it->descent;
27163 glyph->voffset = it->voffset;
27164 glyph->type = XWIDGET_GLYPH;
27165 glyph->avoid_cursor_p = it->avoid_cursor_p;
27166 glyph->multibyte_p = it->multibyte_p;
27167 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27169 /* In R2L rows, the left and the right box edges need to be
27170 drawn in reverse direction. */
27171 glyph->right_box_line_p = it->start_of_box_run_p;
27172 glyph->left_box_line_p = it->end_of_box_run_p;
27174 else
27176 glyph->left_box_line_p = it->start_of_box_run_p;
27177 glyph->right_box_line_p = it->end_of_box_run_p;
27179 glyph->overlaps_vertically_p = 0;
27180 glyph->padding_p = 0;
27181 glyph->glyph_not_available_p = 0;
27182 glyph->face_id = it->face_id;
27183 glyph->u.xwidget = it->xwidget;
27184 glyph->font_type = FONT_TYPE_UNKNOWN;
27185 if (it->bidi_p)
27187 glyph->resolved_level = it->bidi_it.resolved_level;
27188 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27189 glyph->bidi_type = it->bidi_it.type;
27191 ++it->glyph_row->used[area];
27193 else
27194 IT_EXPAND_MATRIX_WIDTH (it, area);
27196 #endif
27199 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27200 of the glyph, WIDTH and HEIGHT are the width and height of the
27201 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27203 static void
27204 append_stretch_glyph (struct it *it, Lisp_Object object,
27205 int width, int height, int ascent)
27207 struct glyph *glyph;
27208 enum glyph_row_area area = it->area;
27210 eassert (ascent >= 0 && ascent <= height);
27212 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27213 if (glyph < it->glyph_row->glyphs[area + 1])
27215 /* If the glyph row is reversed, we need to prepend the glyph
27216 rather than append it. */
27217 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27219 struct glyph *g;
27221 /* Make room for the additional glyph. */
27222 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27223 g[1] = *g;
27224 glyph = it->glyph_row->glyphs[area];
27226 /* Decrease the width of the first glyph of the row that
27227 begins before first_visible_x (e.g., due to hscroll).
27228 This is so the overall width of the row becomes smaller
27229 by the scroll amount, and the stretch glyph appended by
27230 extend_face_to_end_of_line will be wider, to shift the
27231 row glyphs to the right. (In L2R rows, the corresponding
27232 left-shift effect is accomplished by setting row->x to a
27233 negative value, which won't work with R2L rows.)
27235 This must leave us with a positive value of WIDTH, since
27236 otherwise the call to move_it_in_display_line_to at the
27237 beginning of display_line would have got past the entire
27238 first glyph, and then it->current_x would have been
27239 greater or equal to it->first_visible_x. */
27240 if (it->current_x < it->first_visible_x)
27241 width -= it->first_visible_x - it->current_x;
27242 eassert (width > 0);
27244 glyph->charpos = CHARPOS (it->position);
27245 glyph->object = object;
27246 /* FIXME: It would be better to use TYPE_MAX here, but
27247 __typeof__ is not portable enough... */
27248 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27249 glyph->ascent = ascent;
27250 glyph->descent = height - ascent;
27251 glyph->voffset = it->voffset;
27252 glyph->type = STRETCH_GLYPH;
27253 glyph->avoid_cursor_p = it->avoid_cursor_p;
27254 glyph->multibyte_p = it->multibyte_p;
27255 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27257 /* In R2L rows, the left and the right box edges need to be
27258 drawn in reverse direction. */
27259 glyph->right_box_line_p = it->start_of_box_run_p;
27260 glyph->left_box_line_p = it->end_of_box_run_p;
27262 else
27264 glyph->left_box_line_p = it->start_of_box_run_p;
27265 glyph->right_box_line_p = it->end_of_box_run_p;
27267 glyph->overlaps_vertically_p = false;
27268 glyph->padding_p = false;
27269 glyph->glyph_not_available_p = false;
27270 glyph->face_id = it->face_id;
27271 glyph->u.stretch.ascent = ascent;
27272 glyph->u.stretch.height = height;
27273 glyph->slice.img = null_glyph_slice;
27274 glyph->font_type = FONT_TYPE_UNKNOWN;
27275 if (it->bidi_p)
27277 glyph->resolved_level = it->bidi_it.resolved_level;
27278 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27279 glyph->bidi_type = it->bidi_it.type;
27281 else
27283 glyph->resolved_level = 0;
27284 glyph->bidi_type = UNKNOWN_BT;
27286 ++it->glyph_row->used[area];
27288 else
27289 IT_EXPAND_MATRIX_WIDTH (it, area);
27292 #endif /* HAVE_WINDOW_SYSTEM */
27294 /* Produce a stretch glyph for iterator IT. IT->object is the value
27295 of the glyph property displayed. The value must be a list
27296 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27297 being recognized:
27299 1. `:width WIDTH' specifies that the space should be WIDTH *
27300 canonical char width wide. WIDTH may be an integer or floating
27301 point number.
27303 2. `:relative-width FACTOR' specifies that the width of the stretch
27304 should be computed from the width of the first character having the
27305 `glyph' property, and should be FACTOR times that width.
27307 3. `:align-to HPOS' specifies that the space should be wide enough
27308 to reach HPOS, a value in canonical character units.
27310 Exactly one of the above pairs must be present.
27312 4. `:height HEIGHT' specifies that the height of the stretch produced
27313 should be HEIGHT, measured in canonical character units.
27315 5. `:relative-height FACTOR' specifies that the height of the
27316 stretch should be FACTOR times the height of the characters having
27317 the glyph property.
27319 Either none or exactly one of 4 or 5 must be present.
27321 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27322 of the stretch should be used for the ascent of the stretch.
27323 ASCENT must be in the range 0 <= ASCENT <= 100. */
27325 void
27326 produce_stretch_glyph (struct it *it)
27328 /* (space :width WIDTH :height HEIGHT ...) */
27329 Lisp_Object prop, plist;
27330 int width = 0, height = 0, align_to = -1;
27331 bool zero_width_ok_p = false;
27332 double tem;
27333 struct font *font = NULL;
27335 #ifdef HAVE_WINDOW_SYSTEM
27336 int ascent = 0;
27337 bool zero_height_ok_p = false;
27339 if (FRAME_WINDOW_P (it->f))
27341 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27342 font = face->font ? face->font : FRAME_FONT (it->f);
27343 prepare_face_for_display (it->f, face);
27345 #endif
27347 /* List should start with `space'. */
27348 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27349 plist = XCDR (it->object);
27351 /* Compute the width of the stretch. */
27352 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27353 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27355 /* Absolute width `:width WIDTH' specified and valid. */
27356 zero_width_ok_p = true;
27357 width = (int)tem;
27359 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27361 /* Relative width `:relative-width FACTOR' specified and valid.
27362 Compute the width of the characters having the `glyph'
27363 property. */
27364 struct it it2;
27365 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27367 it2 = *it;
27368 if (it->multibyte_p)
27369 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27370 else
27372 it2.c = it2.char_to_display = *p, it2.len = 1;
27373 if (! ASCII_CHAR_P (it2.c))
27374 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27377 it2.glyph_row = NULL;
27378 it2.what = IT_CHARACTER;
27379 PRODUCE_GLYPHS (&it2);
27380 width = NUMVAL (prop) * it2.pixel_width;
27382 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27383 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27384 &align_to))
27386 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27387 align_to = (align_to < 0
27389 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27390 else if (align_to < 0)
27391 align_to = window_box_left_offset (it->w, TEXT_AREA);
27392 width = max (0, (int)tem + align_to - it->current_x);
27393 zero_width_ok_p = true;
27395 else
27396 /* Nothing specified -> width defaults to canonical char width. */
27397 width = FRAME_COLUMN_WIDTH (it->f);
27399 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27400 width = 1;
27402 #ifdef HAVE_WINDOW_SYSTEM
27403 /* Compute height. */
27404 if (FRAME_WINDOW_P (it->f))
27406 int default_height = normal_char_height (font, ' ');
27408 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27409 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27411 height = (int)tem;
27412 zero_height_ok_p = true;
27414 else if (prop = Fplist_get (plist, QCrelative_height),
27415 NUMVAL (prop) > 0)
27416 height = default_height * NUMVAL (prop);
27417 else
27418 height = default_height;
27420 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27421 height = 1;
27423 /* Compute percentage of height used for ascent. If
27424 `:ascent ASCENT' is present and valid, use that. Otherwise,
27425 derive the ascent from the font in use. */
27426 if (prop = Fplist_get (plist, QCascent),
27427 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27428 ascent = height * NUMVAL (prop) / 100.0;
27429 else if (!NILP (prop)
27430 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27431 ascent = min (max (0, (int)tem), height);
27432 else
27433 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27435 else
27436 #endif /* HAVE_WINDOW_SYSTEM */
27437 height = 1;
27439 if (width > 0 && it->line_wrap != TRUNCATE
27440 && it->current_x + width > it->last_visible_x)
27442 width = it->last_visible_x - it->current_x;
27443 #ifdef HAVE_WINDOW_SYSTEM
27444 /* Subtract one more pixel from the stretch width, but only on
27445 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27446 width -= FRAME_WINDOW_P (it->f);
27447 #endif
27450 if (width > 0 && height > 0 && it->glyph_row)
27452 Lisp_Object o_object = it->object;
27453 Lisp_Object object = it->stack[it->sp - 1].string;
27454 int n = width;
27456 if (!STRINGP (object))
27457 object = it->w->contents;
27458 #ifdef HAVE_WINDOW_SYSTEM
27459 if (FRAME_WINDOW_P (it->f))
27460 append_stretch_glyph (it, object, width, height, ascent);
27461 else
27462 #endif
27464 it->object = object;
27465 it->char_to_display = ' ';
27466 it->pixel_width = it->len = 1;
27467 while (n--)
27468 tty_append_glyph (it);
27469 it->object = o_object;
27473 it->pixel_width = width;
27474 #ifdef HAVE_WINDOW_SYSTEM
27475 if (FRAME_WINDOW_P (it->f))
27477 it->ascent = it->phys_ascent = ascent;
27478 it->descent = it->phys_descent = height - it->ascent;
27479 it->nglyphs = width > 0 && height > 0;
27480 take_vertical_position_into_account (it);
27482 else
27483 #endif
27484 it->nglyphs = width;
27487 /* Get information about special display element WHAT in an
27488 environment described by IT. WHAT is one of IT_TRUNCATION or
27489 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27490 non-null glyph_row member. This function ensures that fields like
27491 face_id, c, len of IT are left untouched. */
27493 static void
27494 produce_special_glyphs (struct it *it, enum display_element_type what)
27496 struct it temp_it;
27497 Lisp_Object gc;
27498 GLYPH glyph;
27500 temp_it = *it;
27501 temp_it.object = Qnil;
27502 memset (&temp_it.current, 0, sizeof temp_it.current);
27504 if (what == IT_CONTINUATION)
27506 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27507 if (it->bidi_it.paragraph_dir == R2L)
27508 SET_GLYPH_FROM_CHAR (glyph, '/');
27509 else
27510 SET_GLYPH_FROM_CHAR (glyph, '\\');
27511 if (it->dp
27512 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27514 /* FIXME: Should we mirror GC for R2L lines? */
27515 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27516 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27519 else if (what == IT_TRUNCATION)
27521 /* Truncation glyph. */
27522 SET_GLYPH_FROM_CHAR (glyph, '$');
27523 if (it->dp
27524 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27526 /* FIXME: Should we mirror GC for R2L lines? */
27527 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27528 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27531 else
27532 emacs_abort ();
27534 #ifdef HAVE_WINDOW_SYSTEM
27535 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27536 is turned off, we precede the truncation/continuation glyphs by a
27537 stretch glyph whose width is computed such that these special
27538 glyphs are aligned at the window margin, even when very different
27539 fonts are used in different glyph rows. */
27540 if (FRAME_WINDOW_P (temp_it.f)
27541 /* init_iterator calls this with it->glyph_row == NULL, and it
27542 wants only the pixel width of the truncation/continuation
27543 glyphs. */
27544 && temp_it.glyph_row
27545 /* insert_left_trunc_glyphs calls us at the beginning of the
27546 row, and it has its own calculation of the stretch glyph
27547 width. */
27548 && temp_it.glyph_row->used[TEXT_AREA] > 0
27549 && (temp_it.glyph_row->reversed_p
27550 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27551 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27553 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27555 if (stretch_width > 0)
27557 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27558 struct font *font =
27559 face->font ? face->font : FRAME_FONT (temp_it.f);
27560 int stretch_ascent =
27561 (((temp_it.ascent + temp_it.descent)
27562 * FONT_BASE (font)) / FONT_HEIGHT (font));
27564 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27565 temp_it.ascent + temp_it.descent,
27566 stretch_ascent);
27569 #endif
27571 temp_it.dp = NULL;
27572 temp_it.what = IT_CHARACTER;
27573 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27574 temp_it.face_id = GLYPH_FACE (glyph);
27575 temp_it.len = CHAR_BYTES (temp_it.c);
27577 PRODUCE_GLYPHS (&temp_it);
27578 it->pixel_width = temp_it.pixel_width;
27579 it->nglyphs = temp_it.nglyphs;
27582 #ifdef HAVE_WINDOW_SYSTEM
27584 /* Calculate line-height and line-spacing properties.
27585 An integer value specifies explicit pixel value.
27586 A float value specifies relative value to current face height.
27587 A cons (float . face-name) specifies relative value to
27588 height of specified face font.
27590 Returns height in pixels, or nil. */
27592 static Lisp_Object
27593 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27594 int boff, bool override)
27596 Lisp_Object face_name = Qnil;
27597 int ascent, descent, height;
27599 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27600 return val;
27602 if (CONSP (val))
27604 face_name = XCAR (val);
27605 val = XCDR (val);
27606 if (!NUMBERP (val))
27607 val = make_number (1);
27608 if (NILP (face_name))
27610 height = it->ascent + it->descent;
27611 goto scale;
27615 if (NILP (face_name))
27617 font = FRAME_FONT (it->f);
27618 boff = FRAME_BASELINE_OFFSET (it->f);
27620 else if (EQ (face_name, Qt))
27622 override = false;
27624 else
27626 int face_id;
27627 struct face *face;
27629 face_id = lookup_named_face (it->f, face_name, false);
27630 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27631 if (face == NULL || ((font = face->font) == NULL))
27632 return make_number (-1);
27633 boff = font->baseline_offset;
27634 if (font->vertical_centering)
27635 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27638 normal_char_ascent_descent (font, -1, &ascent, &descent);
27640 if (override)
27642 it->override_ascent = ascent;
27643 it->override_descent = descent;
27644 it->override_boff = boff;
27647 height = ascent + descent;
27649 scale:
27650 if (FLOATP (val))
27651 height = (int)(XFLOAT_DATA (val) * height);
27652 else if (INTEGERP (val))
27653 height *= XINT (val);
27655 return make_number (height);
27659 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27660 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27661 and only if this is for a character for which no font was found.
27663 If the display method (it->glyphless_method) is
27664 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27665 length of the acronym or the hexadecimal string, UPPER_XOFF and
27666 UPPER_YOFF are pixel offsets for the upper part of the string,
27667 LOWER_XOFF and LOWER_YOFF are for the lower part.
27669 For the other display methods, LEN through LOWER_YOFF are zero. */
27671 static void
27672 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27673 short upper_xoff, short upper_yoff,
27674 short lower_xoff, short lower_yoff)
27676 struct glyph *glyph;
27677 enum glyph_row_area area = it->area;
27679 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27680 if (glyph < it->glyph_row->glyphs[area + 1])
27682 /* If the glyph row is reversed, we need to prepend the glyph
27683 rather than append it. */
27684 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27686 struct glyph *g;
27688 /* Make room for the additional glyph. */
27689 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27690 g[1] = *g;
27691 glyph = it->glyph_row->glyphs[area];
27693 glyph->charpos = CHARPOS (it->position);
27694 glyph->object = it->object;
27695 eassert (it->pixel_width <= SHRT_MAX);
27696 glyph->pixel_width = it->pixel_width;
27697 glyph->ascent = it->ascent;
27698 glyph->descent = it->descent;
27699 glyph->voffset = it->voffset;
27700 glyph->type = GLYPHLESS_GLYPH;
27701 glyph->u.glyphless.method = it->glyphless_method;
27702 glyph->u.glyphless.for_no_font = for_no_font;
27703 glyph->u.glyphless.len = len;
27704 glyph->u.glyphless.ch = it->c;
27705 glyph->slice.glyphless.upper_xoff = upper_xoff;
27706 glyph->slice.glyphless.upper_yoff = upper_yoff;
27707 glyph->slice.glyphless.lower_xoff = lower_xoff;
27708 glyph->slice.glyphless.lower_yoff = lower_yoff;
27709 glyph->avoid_cursor_p = it->avoid_cursor_p;
27710 glyph->multibyte_p = it->multibyte_p;
27711 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27713 /* In R2L rows, the left and the right box edges need to be
27714 drawn in reverse direction. */
27715 glyph->right_box_line_p = it->start_of_box_run_p;
27716 glyph->left_box_line_p = it->end_of_box_run_p;
27718 else
27720 glyph->left_box_line_p = it->start_of_box_run_p;
27721 glyph->right_box_line_p = it->end_of_box_run_p;
27723 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27724 || it->phys_descent > it->descent);
27725 glyph->padding_p = false;
27726 glyph->glyph_not_available_p = false;
27727 glyph->face_id = face_id;
27728 glyph->font_type = FONT_TYPE_UNKNOWN;
27729 if (it->bidi_p)
27731 glyph->resolved_level = it->bidi_it.resolved_level;
27732 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27733 glyph->bidi_type = it->bidi_it.type;
27735 ++it->glyph_row->used[area];
27737 else
27738 IT_EXPAND_MATRIX_WIDTH (it, area);
27742 /* Produce a glyph for a glyphless character for iterator IT.
27743 IT->glyphless_method specifies which method to use for displaying
27744 the character. See the description of enum
27745 glyphless_display_method in dispextern.h for the detail.
27747 FOR_NO_FONT is true if and only if this is for a character for
27748 which no font was found. ACRONYM, if non-nil, is an acronym string
27749 for the character. */
27751 static void
27752 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27754 int face_id;
27755 struct face *face;
27756 struct font *font;
27757 int base_width, base_height, width, height;
27758 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27759 int len;
27761 /* Get the metrics of the base font. We always refer to the current
27762 ASCII face. */
27763 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27764 font = face->font ? face->font : FRAME_FONT (it->f);
27765 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27766 it->ascent += font->baseline_offset;
27767 it->descent -= font->baseline_offset;
27768 base_height = it->ascent + it->descent;
27769 base_width = font->average_width;
27771 face_id = merge_glyphless_glyph_face (it);
27773 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27775 it->pixel_width = THIN_SPACE_WIDTH;
27776 len = 0;
27777 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27779 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27781 width = CHARACTER_WIDTH (it->c);
27782 if (width == 0)
27783 width = 1;
27784 else if (width > 4)
27785 width = 4;
27786 it->pixel_width = base_width * width;
27787 len = 0;
27788 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27790 else
27792 char buf[7];
27793 const char *str;
27794 unsigned int code[6];
27795 int upper_len;
27796 int ascent, descent;
27797 struct font_metrics metrics_upper, metrics_lower;
27799 face = FACE_FROM_ID (it->f, face_id);
27800 font = face->font ? face->font : FRAME_FONT (it->f);
27801 prepare_face_for_display (it->f, face);
27803 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27805 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27806 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27807 if (CONSP (acronym))
27808 acronym = XCAR (acronym);
27809 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27811 else
27813 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27814 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27815 str = buf;
27817 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27818 code[len] = font->driver->encode_char (font, str[len]);
27819 upper_len = (len + 1) / 2;
27820 font->driver->text_extents (font, code, upper_len,
27821 &metrics_upper);
27822 font->driver->text_extents (font, code + upper_len, len - upper_len,
27823 &metrics_lower);
27827 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27828 width = max (metrics_upper.width, metrics_lower.width) + 4;
27829 upper_xoff = upper_yoff = 2; /* the typical case */
27830 if (base_width >= width)
27832 /* Align the upper to the left, the lower to the right. */
27833 it->pixel_width = base_width;
27834 lower_xoff = base_width - 2 - metrics_lower.width;
27836 else
27838 /* Center the shorter one. */
27839 it->pixel_width = width;
27840 if (metrics_upper.width >= metrics_lower.width)
27841 lower_xoff = (width - metrics_lower.width) / 2;
27842 else
27844 /* FIXME: This code doesn't look right. It formerly was
27845 missing the "lower_xoff = 0;", which couldn't have
27846 been right since it left lower_xoff uninitialized. */
27847 lower_xoff = 0;
27848 upper_xoff = (width - metrics_upper.width) / 2;
27852 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27853 top, bottom, and between upper and lower strings. */
27854 height = (metrics_upper.ascent + metrics_upper.descent
27855 + metrics_lower.ascent + metrics_lower.descent) + 5;
27856 /* Center vertically.
27857 H:base_height, D:base_descent
27858 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27860 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27861 descent = D - H/2 + h/2;
27862 lower_yoff = descent - 2 - ld;
27863 upper_yoff = lower_yoff - la - 1 - ud; */
27864 ascent = - (it->descent - (base_height + height + 1) / 2);
27865 descent = it->descent - (base_height - height) / 2;
27866 lower_yoff = descent - 2 - metrics_lower.descent;
27867 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27868 - metrics_upper.descent);
27869 /* Don't make the height shorter than the base height. */
27870 if (height > base_height)
27872 it->ascent = ascent;
27873 it->descent = descent;
27877 it->phys_ascent = it->ascent;
27878 it->phys_descent = it->descent;
27879 if (it->glyph_row)
27880 append_glyphless_glyph (it, face_id, for_no_font, len,
27881 upper_xoff, upper_yoff,
27882 lower_xoff, lower_yoff);
27883 it->nglyphs = 1;
27884 take_vertical_position_into_account (it);
27888 /* RIF:
27889 Produce glyphs/get display metrics for the display element IT is
27890 loaded with. See the description of struct it in dispextern.h
27891 for an overview of struct it. */
27893 void
27894 x_produce_glyphs (struct it *it)
27896 int extra_line_spacing = it->extra_line_spacing;
27898 it->glyph_not_available_p = false;
27900 if (it->what == IT_CHARACTER)
27902 XChar2b char2b;
27903 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27904 struct font *font = face->font;
27905 struct font_metrics *pcm = NULL;
27906 int boff; /* Baseline offset. */
27908 if (font == NULL)
27910 /* When no suitable font is found, display this character by
27911 the method specified in the first extra slot of
27912 Vglyphless_char_display. */
27913 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27915 eassert (it->what == IT_GLYPHLESS);
27916 produce_glyphless_glyph (it, true,
27917 STRINGP (acronym) ? acronym : Qnil);
27918 goto done;
27921 boff = font->baseline_offset;
27922 if (font->vertical_centering)
27923 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27925 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27927 it->nglyphs = 1;
27929 if (it->override_ascent >= 0)
27931 it->ascent = it->override_ascent;
27932 it->descent = it->override_descent;
27933 boff = it->override_boff;
27935 else
27937 it->ascent = FONT_BASE (font) + boff;
27938 it->descent = FONT_DESCENT (font) - boff;
27941 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27943 pcm = get_per_char_metric (font, &char2b);
27944 if (pcm->width == 0
27945 && pcm->rbearing == 0 && pcm->lbearing == 0)
27946 pcm = NULL;
27949 if (pcm)
27951 it->phys_ascent = pcm->ascent + boff;
27952 it->phys_descent = pcm->descent - boff;
27953 it->pixel_width = pcm->width;
27954 /* Don't use font-global values for ascent and descent
27955 if they result in an exceedingly large line height. */
27956 if (it->override_ascent < 0)
27958 if (FONT_TOO_HIGH (font))
27960 it->ascent = it->phys_ascent;
27961 it->descent = it->phys_descent;
27962 /* These limitations are enforced by an
27963 assertion near the end of this function. */
27964 if (it->ascent < 0)
27965 it->ascent = 0;
27966 if (it->descent < 0)
27967 it->descent = 0;
27971 else
27973 it->glyph_not_available_p = true;
27974 it->phys_ascent = it->ascent;
27975 it->phys_descent = it->descent;
27976 it->pixel_width = font->space_width;
27979 if (it->constrain_row_ascent_descent_p)
27981 if (it->descent > it->max_descent)
27983 it->ascent += it->descent - it->max_descent;
27984 it->descent = it->max_descent;
27986 if (it->ascent > it->max_ascent)
27988 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27989 it->ascent = it->max_ascent;
27991 it->phys_ascent = min (it->phys_ascent, it->ascent);
27992 it->phys_descent = min (it->phys_descent, it->descent);
27993 extra_line_spacing = 0;
27996 /* If this is a space inside a region of text with
27997 `space-width' property, change its width. */
27998 bool stretched_p
27999 = it->char_to_display == ' ' && !NILP (it->space_width);
28000 if (stretched_p)
28001 it->pixel_width *= XFLOATINT (it->space_width);
28003 /* If face has a box, add the box thickness to the character
28004 height. If character has a box line to the left and/or
28005 right, add the box line width to the character's width. */
28006 if (face->box != FACE_NO_BOX)
28008 int thick = face->box_line_width;
28010 if (thick > 0)
28012 it->ascent += thick;
28013 it->descent += thick;
28015 else
28016 thick = -thick;
28018 if (it->start_of_box_run_p)
28019 it->pixel_width += thick;
28020 if (it->end_of_box_run_p)
28021 it->pixel_width += thick;
28024 /* If face has an overline, add the height of the overline
28025 (1 pixel) and a 1 pixel margin to the character height. */
28026 if (face->overline_p)
28027 it->ascent += overline_margin;
28029 if (it->constrain_row_ascent_descent_p)
28031 if (it->ascent > it->max_ascent)
28032 it->ascent = it->max_ascent;
28033 if (it->descent > it->max_descent)
28034 it->descent = it->max_descent;
28037 take_vertical_position_into_account (it);
28039 /* If we have to actually produce glyphs, do it. */
28040 if (it->glyph_row)
28042 if (stretched_p)
28044 /* Translate a space with a `space-width' property
28045 into a stretch glyph. */
28046 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28047 / FONT_HEIGHT (font));
28048 append_stretch_glyph (it, it->object, it->pixel_width,
28049 it->ascent + it->descent, ascent);
28051 else
28052 append_glyph (it);
28054 /* If characters with lbearing or rbearing are displayed
28055 in this line, record that fact in a flag of the
28056 glyph row. This is used to optimize X output code. */
28057 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28058 it->glyph_row->contains_overlapping_glyphs_p = true;
28060 if (! stretched_p && it->pixel_width == 0)
28061 /* We assure that all visible glyphs have at least 1-pixel
28062 width. */
28063 it->pixel_width = 1;
28065 else if (it->char_to_display == '\n')
28067 /* A newline has no width, but we need the height of the
28068 line. But if previous part of the line sets a height,
28069 don't increase that height. */
28071 Lisp_Object height;
28072 Lisp_Object total_height = Qnil;
28074 it->override_ascent = -1;
28075 it->pixel_width = 0;
28076 it->nglyphs = 0;
28078 height = get_it_property (it, Qline_height);
28079 /* Split (line-height total-height) list. */
28080 if (CONSP (height)
28081 && CONSP (XCDR (height))
28082 && NILP (XCDR (XCDR (height))))
28084 total_height = XCAR (XCDR (height));
28085 height = XCAR (height);
28087 height = calc_line_height_property (it, height, font, boff, true);
28089 if (it->override_ascent >= 0)
28091 it->ascent = it->override_ascent;
28092 it->descent = it->override_descent;
28093 boff = it->override_boff;
28095 else
28097 if (FONT_TOO_HIGH (font))
28099 it->ascent = font->pixel_size + boff - 1;
28100 it->descent = -boff + 1;
28101 if (it->descent < 0)
28102 it->descent = 0;
28104 else
28106 it->ascent = FONT_BASE (font) + boff;
28107 it->descent = FONT_DESCENT (font) - boff;
28111 if (EQ (height, Qt))
28113 if (it->descent > it->max_descent)
28115 it->ascent += it->descent - it->max_descent;
28116 it->descent = it->max_descent;
28118 if (it->ascent > it->max_ascent)
28120 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28121 it->ascent = it->max_ascent;
28123 it->phys_ascent = min (it->phys_ascent, it->ascent);
28124 it->phys_descent = min (it->phys_descent, it->descent);
28125 it->constrain_row_ascent_descent_p = true;
28126 extra_line_spacing = 0;
28128 else
28130 Lisp_Object spacing;
28132 it->phys_ascent = it->ascent;
28133 it->phys_descent = it->descent;
28135 if ((it->max_ascent > 0 || it->max_descent > 0)
28136 && face->box != FACE_NO_BOX
28137 && face->box_line_width > 0)
28139 it->ascent += face->box_line_width;
28140 it->descent += face->box_line_width;
28142 if (!NILP (height)
28143 && XINT (height) > it->ascent + it->descent)
28144 it->ascent = XINT (height) - it->descent;
28146 if (!NILP (total_height))
28147 spacing = calc_line_height_property (it, total_height, font,
28148 boff, false);
28149 else
28151 spacing = get_it_property (it, Qline_spacing);
28152 spacing = calc_line_height_property (it, spacing, font,
28153 boff, false);
28155 if (INTEGERP (spacing))
28157 extra_line_spacing = XINT (spacing);
28158 if (!NILP (total_height))
28159 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28163 else /* i.e. (it->char_to_display == '\t') */
28165 if (font->space_width > 0)
28167 int tab_width = it->tab_width * font->space_width;
28168 int x = it->current_x + it->continuation_lines_width;
28169 int x0 = x;
28170 /* Adjust for line numbers, if needed. */
28171 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28172 x -= it->lnum_pixel_width;
28173 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28175 /* If the distance from the current position to the next tab
28176 stop is less than a space character width, use the
28177 tab stop after that. */
28178 if (next_tab_x - x < font->space_width)
28179 next_tab_x += tab_width;
28180 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28181 next_tab_x += (it->lnum_pixel_width
28182 - ((it->w->hscroll * font->space_width)
28183 % tab_width));
28185 it->pixel_width = next_tab_x - x0;
28186 it->nglyphs = 1;
28187 if (FONT_TOO_HIGH (font))
28189 if (get_char_glyph_code (' ', font, &char2b))
28191 pcm = get_per_char_metric (font, &char2b);
28192 if (pcm->width == 0
28193 && pcm->rbearing == 0 && pcm->lbearing == 0)
28194 pcm = NULL;
28197 if (pcm)
28199 it->ascent = pcm->ascent + boff;
28200 it->descent = pcm->descent - boff;
28202 else
28204 it->ascent = font->pixel_size + boff - 1;
28205 it->descent = -boff + 1;
28207 if (it->ascent < 0)
28208 it->ascent = 0;
28209 if (it->descent < 0)
28210 it->descent = 0;
28212 else
28214 it->ascent = FONT_BASE (font) + boff;
28215 it->descent = FONT_DESCENT (font) - boff;
28217 it->phys_ascent = it->ascent;
28218 it->phys_descent = it->descent;
28220 if (it->glyph_row)
28222 append_stretch_glyph (it, it->object, it->pixel_width,
28223 it->ascent + it->descent, it->ascent);
28226 else
28228 it->pixel_width = 0;
28229 it->nglyphs = 1;
28233 if (FONT_TOO_HIGH (font))
28235 int font_ascent, font_descent;
28237 /* For very large fonts, where we ignore the declared font
28238 dimensions, and go by per-character metrics instead,
28239 don't let the row ascent and descent values (and the row
28240 height computed from them) be smaller than the "normal"
28241 character metrics. This avoids unpleasant effects
28242 whereby lines on display would change their height
28243 depending on which characters are shown. */
28244 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28245 it->max_ascent = max (it->max_ascent, font_ascent);
28246 it->max_descent = max (it->max_descent, font_descent);
28249 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28251 /* A static composition.
28253 Note: A composition is represented as one glyph in the
28254 glyph matrix. There are no padding glyphs.
28256 Important note: pixel_width, ascent, and descent are the
28257 values of what is drawn by draw_glyphs (i.e. the values of
28258 the overall glyphs composed). */
28259 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28260 int boff; /* baseline offset */
28261 struct composition *cmp = composition_table[it->cmp_it.id];
28262 int glyph_len = cmp->glyph_len;
28263 struct font *font = face->font;
28265 it->nglyphs = 1;
28267 /* If we have not yet calculated pixel size data of glyphs of
28268 the composition for the current face font, calculate them
28269 now. Theoretically, we have to check all fonts for the
28270 glyphs, but that requires much time and memory space. So,
28271 here we check only the font of the first glyph. This may
28272 lead to incorrect display, but it's very rare, and C-l
28273 (recenter-top-bottom) can correct the display anyway. */
28274 if (! cmp->font || cmp->font != font)
28276 /* Ascent and descent of the font of the first character
28277 of this composition (adjusted by baseline offset).
28278 Ascent and descent of overall glyphs should not be less
28279 than these, respectively. */
28280 int font_ascent, font_descent, font_height;
28281 /* Bounding box of the overall glyphs. */
28282 int leftmost, rightmost, lowest, highest;
28283 int lbearing, rbearing;
28284 int i, width, ascent, descent;
28285 int c;
28286 XChar2b char2b;
28287 struct font_metrics *pcm;
28288 ptrdiff_t pos;
28290 eassume (0 < glyph_len); /* See Bug#8512. */
28292 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28293 while (c == '\t' && 0 < --glyph_len);
28295 bool right_padded = glyph_len < cmp->glyph_len;
28296 for (i = 0; i < glyph_len; i++)
28298 c = COMPOSITION_GLYPH (cmp, i);
28299 if (c != '\t')
28300 break;
28301 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28303 bool left_padded = i > 0;
28305 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28306 : IT_CHARPOS (*it));
28307 /* If no suitable font is found, use the default font. */
28308 bool font_not_found_p = font == NULL;
28309 if (font_not_found_p)
28311 face = face->ascii_face;
28312 font = face->font;
28314 boff = font->baseline_offset;
28315 if (font->vertical_centering)
28316 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28317 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28318 font_ascent += boff;
28319 font_descent -= boff;
28320 font_height = font_ascent + font_descent;
28322 cmp->font = font;
28324 pcm = NULL;
28325 if (! font_not_found_p)
28327 get_char_face_and_encoding (it->f, c, it->face_id,
28328 &char2b, false);
28329 pcm = get_per_char_metric (font, &char2b);
28332 /* Initialize the bounding box. */
28333 if (pcm)
28335 width = cmp->glyph_len > 0 ? pcm->width : 0;
28336 ascent = pcm->ascent;
28337 descent = pcm->descent;
28338 lbearing = pcm->lbearing;
28339 rbearing = pcm->rbearing;
28341 else
28343 width = cmp->glyph_len > 0 ? font->space_width : 0;
28344 ascent = FONT_BASE (font);
28345 descent = FONT_DESCENT (font);
28346 lbearing = 0;
28347 rbearing = width;
28350 rightmost = width;
28351 leftmost = 0;
28352 lowest = - descent + boff;
28353 highest = ascent + boff;
28355 if (! font_not_found_p
28356 && font->default_ascent
28357 && CHAR_TABLE_P (Vuse_default_ascent)
28358 && !NILP (Faref (Vuse_default_ascent,
28359 make_number (it->char_to_display))))
28360 highest = font->default_ascent + boff;
28362 /* Draw the first glyph at the normal position. It may be
28363 shifted to right later if some other glyphs are drawn
28364 at the left. */
28365 cmp->offsets[i * 2] = 0;
28366 cmp->offsets[i * 2 + 1] = boff;
28367 cmp->lbearing = lbearing;
28368 cmp->rbearing = rbearing;
28370 /* Set cmp->offsets for the remaining glyphs. */
28371 for (i++; i < glyph_len; i++)
28373 int left, right, btm, top;
28374 int ch = COMPOSITION_GLYPH (cmp, i);
28375 int face_id;
28376 struct face *this_face;
28378 if (ch == '\t')
28379 ch = ' ';
28380 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28381 this_face = FACE_FROM_ID (it->f, face_id);
28382 font = this_face->font;
28384 if (font == NULL)
28385 pcm = NULL;
28386 else
28388 get_char_face_and_encoding (it->f, ch, face_id,
28389 &char2b, false);
28390 pcm = get_per_char_metric (font, &char2b);
28392 if (! pcm)
28393 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28394 else
28396 width = pcm->width;
28397 ascent = pcm->ascent;
28398 descent = pcm->descent;
28399 lbearing = pcm->lbearing;
28400 rbearing = pcm->rbearing;
28401 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28403 /* Relative composition with or without
28404 alternate chars. */
28405 left = (leftmost + rightmost - width) / 2;
28406 btm = - descent + boff;
28407 if (font->relative_compose
28408 && (! CHAR_TABLE_P (Vignore_relative_composition)
28409 || NILP (Faref (Vignore_relative_composition,
28410 make_number (ch)))))
28413 if (- descent >= font->relative_compose)
28414 /* One extra pixel between two glyphs. */
28415 btm = highest + 1;
28416 else if (ascent <= 0)
28417 /* One extra pixel between two glyphs. */
28418 btm = lowest - 1 - ascent - descent;
28421 else
28423 /* A composition rule is specified by an integer
28424 value that encodes global and new reference
28425 points (GREF and NREF). GREF and NREF are
28426 specified by numbers as below:
28428 0---1---2 -- ascent
28432 9--10--11 -- center
28434 ---3---4---5--- baseline
28436 6---7---8 -- descent
28438 int rule = COMPOSITION_RULE (cmp, i);
28439 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28441 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28442 grefx = gref % 3, nrefx = nref % 3;
28443 grefy = gref / 3, nrefy = nref / 3;
28444 if (xoff)
28445 xoff = font_height * (xoff - 128) / 256;
28446 if (yoff)
28447 yoff = font_height * (yoff - 128) / 256;
28449 left = (leftmost
28450 + grefx * (rightmost - leftmost) / 2
28451 - nrefx * width / 2
28452 + xoff);
28454 btm = ((grefy == 0 ? highest
28455 : grefy == 1 ? 0
28456 : grefy == 2 ? lowest
28457 : (highest + lowest) / 2)
28458 - (nrefy == 0 ? ascent + descent
28459 : nrefy == 1 ? descent - boff
28460 : nrefy == 2 ? 0
28461 : (ascent + descent) / 2)
28462 + yoff);
28465 cmp->offsets[i * 2] = left;
28466 cmp->offsets[i * 2 + 1] = btm + descent;
28468 /* Update the bounding box of the overall glyphs. */
28469 if (width > 0)
28471 right = left + width;
28472 if (left < leftmost)
28473 leftmost = left;
28474 if (right > rightmost)
28475 rightmost = right;
28477 top = btm + descent + ascent;
28478 if (top > highest)
28479 highest = top;
28480 if (btm < lowest)
28481 lowest = btm;
28483 if (cmp->lbearing > left + lbearing)
28484 cmp->lbearing = left + lbearing;
28485 if (cmp->rbearing < left + rbearing)
28486 cmp->rbearing = left + rbearing;
28490 /* If there are glyphs whose x-offsets are negative,
28491 shift all glyphs to the right and make all x-offsets
28492 non-negative. */
28493 if (leftmost < 0)
28495 for (i = 0; i < cmp->glyph_len; i++)
28496 cmp->offsets[i * 2] -= leftmost;
28497 rightmost -= leftmost;
28498 cmp->lbearing -= leftmost;
28499 cmp->rbearing -= leftmost;
28502 if (left_padded && cmp->lbearing < 0)
28504 for (i = 0; i < cmp->glyph_len; i++)
28505 cmp->offsets[i * 2] -= cmp->lbearing;
28506 rightmost -= cmp->lbearing;
28507 cmp->rbearing -= cmp->lbearing;
28508 cmp->lbearing = 0;
28510 if (right_padded && rightmost < cmp->rbearing)
28512 rightmost = cmp->rbearing;
28515 cmp->pixel_width = rightmost;
28516 cmp->ascent = highest;
28517 cmp->descent = - lowest;
28518 if (cmp->ascent < font_ascent)
28519 cmp->ascent = font_ascent;
28520 if (cmp->descent < font_descent)
28521 cmp->descent = font_descent;
28524 if (it->glyph_row
28525 && (cmp->lbearing < 0
28526 || cmp->rbearing > cmp->pixel_width))
28527 it->glyph_row->contains_overlapping_glyphs_p = true;
28529 it->pixel_width = cmp->pixel_width;
28530 it->ascent = it->phys_ascent = cmp->ascent;
28531 it->descent = it->phys_descent = cmp->descent;
28532 if (face->box != FACE_NO_BOX)
28534 int thick = face->box_line_width;
28536 if (thick > 0)
28538 it->ascent += thick;
28539 it->descent += thick;
28541 else
28542 thick = - thick;
28544 if (it->start_of_box_run_p)
28545 it->pixel_width += thick;
28546 if (it->end_of_box_run_p)
28547 it->pixel_width += thick;
28550 /* If face has an overline, add the height of the overline
28551 (1 pixel) and a 1 pixel margin to the character height. */
28552 if (face->overline_p)
28553 it->ascent += overline_margin;
28555 take_vertical_position_into_account (it);
28556 if (it->ascent < 0)
28557 it->ascent = 0;
28558 if (it->descent < 0)
28559 it->descent = 0;
28561 if (it->glyph_row && cmp->glyph_len > 0)
28562 append_composite_glyph (it);
28564 else if (it->what == IT_COMPOSITION)
28566 /* A dynamic (automatic) composition. */
28567 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28568 Lisp_Object gstring;
28569 struct font_metrics metrics;
28571 it->nglyphs = 1;
28573 gstring = composition_gstring_from_id (it->cmp_it.id);
28574 it->pixel_width
28575 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28576 &metrics);
28577 if (it->glyph_row
28578 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28579 it->glyph_row->contains_overlapping_glyphs_p = true;
28580 it->ascent = it->phys_ascent = metrics.ascent;
28581 it->descent = it->phys_descent = metrics.descent;
28582 if (face->box != FACE_NO_BOX)
28584 int thick = face->box_line_width;
28586 if (thick > 0)
28588 it->ascent += thick;
28589 it->descent += thick;
28591 else
28592 thick = - thick;
28594 if (it->start_of_box_run_p)
28595 it->pixel_width += thick;
28596 if (it->end_of_box_run_p)
28597 it->pixel_width += thick;
28599 /* If face has an overline, add the height of the overline
28600 (1 pixel) and a 1 pixel margin to the character height. */
28601 if (face->overline_p)
28602 it->ascent += overline_margin;
28603 take_vertical_position_into_account (it);
28604 if (it->ascent < 0)
28605 it->ascent = 0;
28606 if (it->descent < 0)
28607 it->descent = 0;
28609 if (it->glyph_row)
28610 append_composite_glyph (it);
28612 else if (it->what == IT_GLYPHLESS)
28613 produce_glyphless_glyph (it, false, Qnil);
28614 else if (it->what == IT_IMAGE)
28615 produce_image_glyph (it);
28616 else if (it->what == IT_STRETCH)
28617 produce_stretch_glyph (it);
28618 else if (it->what == IT_XWIDGET)
28619 produce_xwidget_glyph (it);
28621 done:
28622 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28623 because this isn't true for images with `:ascent 100'. */
28624 eassert (it->ascent >= 0 && it->descent >= 0);
28625 if (it->area == TEXT_AREA)
28626 it->current_x += it->pixel_width;
28628 if (extra_line_spacing > 0)
28630 it->descent += extra_line_spacing;
28631 if (extra_line_spacing > it->max_extra_line_spacing)
28632 it->max_extra_line_spacing = extra_line_spacing;
28635 it->max_ascent = max (it->max_ascent, it->ascent);
28636 it->max_descent = max (it->max_descent, it->descent);
28637 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28638 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28641 /* EXPORT for RIF:
28642 Output LEN glyphs starting at START at the nominal cursor position.
28643 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28644 being updated, and UPDATED_AREA is the area of that row being updated. */
28646 void
28647 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28648 struct glyph *start, enum glyph_row_area updated_area, int len)
28650 int x, hpos, chpos = w->phys_cursor.hpos;
28652 eassert (updated_row);
28653 /* When the window is hscrolled, cursor hpos can legitimately be out
28654 of bounds, but we draw the cursor at the corresponding window
28655 margin in that case. */
28656 if (!updated_row->reversed_p && chpos < 0)
28657 chpos = 0;
28658 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28659 chpos = updated_row->used[TEXT_AREA] - 1;
28661 block_input ();
28663 /* Write glyphs. */
28665 hpos = start - updated_row->glyphs[updated_area];
28666 x = draw_glyphs (w, w->output_cursor.x,
28667 updated_row, updated_area,
28668 hpos, hpos + len,
28669 DRAW_NORMAL_TEXT, 0);
28671 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28672 if (updated_area == TEXT_AREA
28673 && w->phys_cursor_on_p
28674 && w->phys_cursor.vpos == w->output_cursor.vpos
28675 && chpos >= hpos
28676 && chpos < hpos + len)
28677 w->phys_cursor_on_p = false;
28679 unblock_input ();
28681 /* Advance the output cursor. */
28682 w->output_cursor.hpos += len;
28683 w->output_cursor.x = x;
28687 /* EXPORT for RIF:
28688 Insert LEN glyphs from START at the nominal cursor position. */
28690 void
28691 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28692 struct glyph *start, enum glyph_row_area updated_area, int len)
28694 struct frame *f;
28695 int line_height, shift_by_width, shifted_region_width;
28696 struct glyph_row *row;
28697 struct glyph *glyph;
28698 int frame_x, frame_y;
28699 ptrdiff_t hpos;
28701 eassert (updated_row);
28702 block_input ();
28703 f = XFRAME (WINDOW_FRAME (w));
28705 /* Get the height of the line we are in. */
28706 row = updated_row;
28707 line_height = row->height;
28709 /* Get the width of the glyphs to insert. */
28710 shift_by_width = 0;
28711 for (glyph = start; glyph < start + len; ++glyph)
28712 shift_by_width += glyph->pixel_width;
28714 /* Get the width of the region to shift right. */
28715 shifted_region_width = (window_box_width (w, updated_area)
28716 - w->output_cursor.x
28717 - shift_by_width);
28719 /* Shift right. */
28720 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28721 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28723 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28724 line_height, shift_by_width);
28726 /* Write the glyphs. */
28727 hpos = start - row->glyphs[updated_area];
28728 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28729 hpos, hpos + len,
28730 DRAW_NORMAL_TEXT, 0);
28732 /* Advance the output cursor. */
28733 w->output_cursor.hpos += len;
28734 w->output_cursor.x += shift_by_width;
28735 unblock_input ();
28739 /* EXPORT for RIF:
28740 Erase the current text line from the nominal cursor position
28741 (inclusive) to pixel column TO_X (exclusive). The idea is that
28742 everything from TO_X onward is already erased.
28744 TO_X is a pixel position relative to UPDATED_AREA of currently
28745 updated window W. TO_X == -1 means clear to the end of this area. */
28747 void
28748 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28749 enum glyph_row_area updated_area, int to_x)
28751 struct frame *f;
28752 int max_x, min_y, max_y;
28753 int from_x, from_y, to_y;
28755 eassert (updated_row);
28756 f = XFRAME (w->frame);
28758 if (updated_row->full_width_p)
28759 max_x = (WINDOW_PIXEL_WIDTH (w)
28760 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28761 else
28762 max_x = window_box_width (w, updated_area);
28763 max_y = window_text_bottom_y (w);
28765 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28766 of window. For TO_X > 0, truncate to end of drawing area. */
28767 if (to_x == 0)
28768 return;
28769 else if (to_x < 0)
28770 to_x = max_x;
28771 else
28772 to_x = min (to_x, max_x);
28774 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28776 /* Notice if the cursor will be cleared by this operation. */
28777 if (!updated_row->full_width_p)
28778 notice_overwritten_cursor (w, updated_area,
28779 w->output_cursor.x, -1,
28780 updated_row->y,
28781 MATRIX_ROW_BOTTOM_Y (updated_row));
28783 from_x = w->output_cursor.x;
28785 /* Translate to frame coordinates. */
28786 if (updated_row->full_width_p)
28788 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28789 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28791 else
28793 int area_left = window_box_left (w, updated_area);
28794 from_x += area_left;
28795 to_x += area_left;
28798 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28799 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28800 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28802 /* Prevent inadvertently clearing to end of the X window. */
28803 if (to_x > from_x && to_y > from_y)
28805 block_input ();
28806 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28807 to_x - from_x, to_y - from_y);
28808 unblock_input ();
28812 #endif /* HAVE_WINDOW_SYSTEM */
28816 /***********************************************************************
28817 Cursor types
28818 ***********************************************************************/
28820 /* Value is the internal representation of the specified cursor type
28821 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28822 of the bar cursor. */
28824 static enum text_cursor_kinds
28825 get_specified_cursor_type (Lisp_Object arg, int *width)
28827 enum text_cursor_kinds type;
28829 if (NILP (arg))
28830 return NO_CURSOR;
28832 if (EQ (arg, Qbox))
28833 return FILLED_BOX_CURSOR;
28835 if (EQ (arg, Qhollow))
28836 return HOLLOW_BOX_CURSOR;
28838 if (EQ (arg, Qbar))
28840 *width = 2;
28841 return BAR_CURSOR;
28844 if (CONSP (arg)
28845 && EQ (XCAR (arg), Qbar)
28846 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28848 *width = XINT (XCDR (arg));
28849 return BAR_CURSOR;
28852 if (EQ (arg, Qhbar))
28854 *width = 2;
28855 return HBAR_CURSOR;
28858 if (CONSP (arg)
28859 && EQ (XCAR (arg), Qhbar)
28860 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28862 *width = XINT (XCDR (arg));
28863 return HBAR_CURSOR;
28866 /* Treat anything unknown as "hollow box cursor".
28867 It was bad to signal an error; people have trouble fixing
28868 .Xdefaults with Emacs, when it has something bad in it. */
28869 type = HOLLOW_BOX_CURSOR;
28871 return type;
28874 /* Set the default cursor types for specified frame. */
28875 void
28876 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28878 int width = 1;
28879 Lisp_Object tem;
28881 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28882 FRAME_CURSOR_WIDTH (f) = width;
28884 /* By default, set up the blink-off state depending on the on-state. */
28886 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28887 if (!NILP (tem))
28889 FRAME_BLINK_OFF_CURSOR (f)
28890 = get_specified_cursor_type (XCDR (tem), &width);
28891 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28893 else
28894 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28896 /* Make sure the cursor gets redrawn. */
28897 f->cursor_type_changed = true;
28901 #ifdef HAVE_WINDOW_SYSTEM
28903 /* Return the cursor we want to be displayed in window W. Return
28904 width of bar/hbar cursor through WIDTH arg. Return with
28905 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28906 (i.e. if the `system caret' should track this cursor).
28908 In a mini-buffer window, we want the cursor only to appear if we
28909 are reading input from this window. For the selected window, we
28910 want the cursor type given by the frame parameter or buffer local
28911 setting of cursor-type. If explicitly marked off, draw no cursor.
28912 In all other cases, we want a hollow box cursor. */
28914 static enum text_cursor_kinds
28915 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28916 bool *active_cursor)
28918 struct frame *f = XFRAME (w->frame);
28919 struct buffer *b = XBUFFER (w->contents);
28920 int cursor_type = DEFAULT_CURSOR;
28921 Lisp_Object alt_cursor;
28922 bool non_selected = false;
28924 *active_cursor = true;
28926 /* Echo area */
28927 if (cursor_in_echo_area
28928 && FRAME_HAS_MINIBUF_P (f)
28929 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28931 if (w == XWINDOW (echo_area_window))
28933 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28935 *width = FRAME_CURSOR_WIDTH (f);
28936 return FRAME_DESIRED_CURSOR (f);
28938 else
28939 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28942 *active_cursor = false;
28943 non_selected = true;
28946 /* Detect a nonselected window or nonselected frame. */
28947 else if (w != XWINDOW (f->selected_window)
28948 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28950 *active_cursor = false;
28952 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28953 return NO_CURSOR;
28955 non_selected = true;
28958 /* Never display a cursor in a window in which cursor-type is nil. */
28959 if (NILP (BVAR (b, cursor_type)))
28960 return NO_CURSOR;
28962 /* Get the normal cursor type for this window. */
28963 if (EQ (BVAR (b, cursor_type), Qt))
28965 cursor_type = FRAME_DESIRED_CURSOR (f);
28966 *width = FRAME_CURSOR_WIDTH (f);
28968 else
28969 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28971 /* Use cursor-in-non-selected-windows instead
28972 for non-selected window or frame. */
28973 if (non_selected)
28975 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28976 if (!EQ (Qt, alt_cursor))
28977 return get_specified_cursor_type (alt_cursor, width);
28978 /* t means modify the normal cursor type. */
28979 if (cursor_type == FILLED_BOX_CURSOR)
28980 cursor_type = HOLLOW_BOX_CURSOR;
28981 else if (cursor_type == BAR_CURSOR && *width > 1)
28982 --*width;
28983 return cursor_type;
28986 /* Use normal cursor if not blinked off. */
28987 if (!w->cursor_off_p)
28989 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28990 return NO_CURSOR;
28991 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28993 if (cursor_type == FILLED_BOX_CURSOR)
28995 /* Using a block cursor on large images can be very annoying.
28996 So use a hollow cursor for "large" images.
28997 If image is not transparent (no mask), also use hollow cursor. */
28998 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28999 if (img != NULL && IMAGEP (img->spec))
29001 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29002 where N = size of default frame font size.
29003 This should cover most of the "tiny" icons people may use. */
29004 if (!img->mask
29005 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29006 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29007 cursor_type = HOLLOW_BOX_CURSOR;
29010 else if (cursor_type != NO_CURSOR)
29012 /* Display current only supports BOX and HOLLOW cursors for images.
29013 So for now, unconditionally use a HOLLOW cursor when cursor is
29014 not a solid box cursor. */
29015 cursor_type = HOLLOW_BOX_CURSOR;
29018 return cursor_type;
29021 /* Cursor is blinked off, so determine how to "toggle" it. */
29023 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29024 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29025 return get_specified_cursor_type (XCDR (alt_cursor), width);
29027 /* Then see if frame has specified a specific blink off cursor type. */
29028 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29030 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29031 return FRAME_BLINK_OFF_CURSOR (f);
29034 #if false
29035 /* Some people liked having a permanently visible blinking cursor,
29036 while others had very strong opinions against it. So it was
29037 decided to remove it. KFS 2003-09-03 */
29039 /* Finally perform built-in cursor blinking:
29040 filled box <-> hollow box
29041 wide [h]bar <-> narrow [h]bar
29042 narrow [h]bar <-> no cursor
29043 other type <-> no cursor */
29045 if (cursor_type == FILLED_BOX_CURSOR)
29046 return HOLLOW_BOX_CURSOR;
29048 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29050 *width = 1;
29051 return cursor_type;
29053 #endif
29055 return NO_CURSOR;
29059 /* Notice when the text cursor of window W has been completely
29060 overwritten by a drawing operation that outputs glyphs in AREA
29061 starting at X0 and ending at X1 in the line starting at Y0 and
29062 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29063 the rest of the line after X0 has been written. Y coordinates
29064 are window-relative. */
29066 static void
29067 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29068 int x0, int x1, int y0, int y1)
29070 int cx0, cx1, cy0, cy1;
29071 struct glyph_row *row;
29073 if (!w->phys_cursor_on_p)
29074 return;
29075 if (area != TEXT_AREA)
29076 return;
29078 if (w->phys_cursor.vpos < 0
29079 || w->phys_cursor.vpos >= w->current_matrix->nrows
29080 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29081 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29082 return;
29084 if (row->cursor_in_fringe_p)
29086 row->cursor_in_fringe_p = false;
29087 draw_fringe_bitmap (w, row, row->reversed_p);
29088 w->phys_cursor_on_p = false;
29089 return;
29092 cx0 = w->phys_cursor.x;
29093 cx1 = cx0 + w->phys_cursor_width;
29094 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29095 return;
29097 /* The cursor image will be completely removed from the
29098 screen if the output area intersects the cursor area in
29099 y-direction. When we draw in [y0 y1[, and some part of
29100 the cursor is at y < y0, that part must have been drawn
29101 before. When scrolling, the cursor is erased before
29102 actually scrolling, so we don't come here. When not
29103 scrolling, the rows above the old cursor row must have
29104 changed, and in this case these rows must have written
29105 over the cursor image.
29107 Likewise if part of the cursor is below y1, with the
29108 exception of the cursor being in the first blank row at
29109 the buffer and window end because update_text_area
29110 doesn't draw that row. (Except when it does, but
29111 that's handled in update_text_area.) */
29113 cy0 = w->phys_cursor.y;
29114 cy1 = cy0 + w->phys_cursor_height;
29115 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29116 return;
29118 w->phys_cursor_on_p = false;
29121 #endif /* HAVE_WINDOW_SYSTEM */
29124 /************************************************************************
29125 Mouse Face
29126 ************************************************************************/
29128 #ifdef HAVE_WINDOW_SYSTEM
29130 /* EXPORT for RIF:
29131 Fix the display of area AREA of overlapping row ROW in window W
29132 with respect to the overlapping part OVERLAPS. */
29134 void
29135 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29136 enum glyph_row_area area, int overlaps)
29138 int i, x;
29140 block_input ();
29142 x = 0;
29143 for (i = 0; i < row->used[area];)
29145 if (row->glyphs[area][i].overlaps_vertically_p)
29147 int start = i, start_x = x;
29151 x += row->glyphs[area][i].pixel_width;
29152 ++i;
29154 while (i < row->used[area]
29155 && row->glyphs[area][i].overlaps_vertically_p);
29157 draw_glyphs (w, start_x, row, area,
29158 start, i,
29159 DRAW_NORMAL_TEXT, overlaps);
29161 else
29163 x += row->glyphs[area][i].pixel_width;
29164 ++i;
29168 unblock_input ();
29172 /* EXPORT:
29173 Draw the cursor glyph of window W in glyph row ROW. See the
29174 comment of draw_glyphs for the meaning of HL. */
29176 void
29177 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29178 enum draw_glyphs_face hl)
29180 /* If cursor hpos is out of bounds, don't draw garbage. This can
29181 happen in mini-buffer windows when switching between echo area
29182 glyphs and mini-buffer. */
29183 if ((row->reversed_p
29184 ? (w->phys_cursor.hpos >= 0)
29185 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29187 bool on_p = w->phys_cursor_on_p;
29188 int x1;
29189 int hpos = w->phys_cursor.hpos;
29191 /* When the window is hscrolled, cursor hpos can legitimately be
29192 out of bounds, but we draw the cursor at the corresponding
29193 window margin in that case. */
29194 if (!row->reversed_p && hpos < 0)
29195 hpos = 0;
29196 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29197 hpos = row->used[TEXT_AREA] - 1;
29199 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29200 hl, 0);
29201 w->phys_cursor_on_p = on_p;
29203 if (hl == DRAW_CURSOR)
29204 w->phys_cursor_width = x1 - w->phys_cursor.x;
29205 /* When we erase the cursor, and ROW is overlapped by other
29206 rows, make sure that these overlapping parts of other rows
29207 are redrawn. */
29208 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29210 w->phys_cursor_width = x1 - w->phys_cursor.x;
29212 if (row > w->current_matrix->rows
29213 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29214 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29215 OVERLAPS_ERASED_CURSOR);
29217 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29218 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29219 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29220 OVERLAPS_ERASED_CURSOR);
29226 /* Erase the image of a cursor of window W from the screen. */
29228 void
29229 erase_phys_cursor (struct window *w)
29231 struct frame *f = XFRAME (w->frame);
29232 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29233 int hpos = w->phys_cursor.hpos;
29234 int vpos = w->phys_cursor.vpos;
29235 bool mouse_face_here_p = false;
29236 struct glyph_matrix *active_glyphs = w->current_matrix;
29237 struct glyph_row *cursor_row;
29238 struct glyph *cursor_glyph;
29239 enum draw_glyphs_face hl;
29241 /* No cursor displayed or row invalidated => nothing to do on the
29242 screen. */
29243 if (w->phys_cursor_type == NO_CURSOR)
29244 goto mark_cursor_off;
29246 /* VPOS >= active_glyphs->nrows means that window has been resized.
29247 Don't bother to erase the cursor. */
29248 if (vpos >= active_glyphs->nrows)
29249 goto mark_cursor_off;
29251 /* If row containing cursor is marked invalid, there is nothing we
29252 can do. */
29253 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29254 if (!cursor_row->enabled_p)
29255 goto mark_cursor_off;
29257 /* If line spacing is > 0, old cursor may only be partially visible in
29258 window after split-window. So adjust visible height. */
29259 cursor_row->visible_height = min (cursor_row->visible_height,
29260 window_text_bottom_y (w) - cursor_row->y);
29262 /* If row is completely invisible, don't attempt to delete a cursor which
29263 isn't there. This can happen if cursor is at top of a window, and
29264 we switch to a buffer with a header line in that window. */
29265 if (cursor_row->visible_height <= 0)
29266 goto mark_cursor_off;
29268 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29269 if (cursor_row->cursor_in_fringe_p)
29271 cursor_row->cursor_in_fringe_p = false;
29272 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29273 goto mark_cursor_off;
29276 /* This can happen when the new row is shorter than the old one.
29277 In this case, either draw_glyphs or clear_end_of_line
29278 should have cleared the cursor. Note that we wouldn't be
29279 able to erase the cursor in this case because we don't have a
29280 cursor glyph at hand. */
29281 if ((cursor_row->reversed_p
29282 ? (w->phys_cursor.hpos < 0)
29283 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29284 goto mark_cursor_off;
29286 /* When the window is hscrolled, cursor hpos can legitimately be out
29287 of bounds, but we draw the cursor at the corresponding window
29288 margin in that case. */
29289 if (!cursor_row->reversed_p && hpos < 0)
29290 hpos = 0;
29291 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29292 hpos = cursor_row->used[TEXT_AREA] - 1;
29294 /* If the cursor is in the mouse face area, redisplay that when
29295 we clear the cursor. */
29296 if (! NILP (hlinfo->mouse_face_window)
29297 && coords_in_mouse_face_p (w, hpos, vpos)
29298 /* Don't redraw the cursor's spot in mouse face if it is at the
29299 end of a line (on a newline). The cursor appears there, but
29300 mouse highlighting does not. */
29301 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29302 mouse_face_here_p = true;
29304 /* Maybe clear the display under the cursor. */
29305 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29307 int x, y;
29308 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29309 int width;
29311 cursor_glyph = get_phys_cursor_glyph (w);
29312 if (cursor_glyph == NULL)
29313 goto mark_cursor_off;
29315 width = cursor_glyph->pixel_width;
29316 x = w->phys_cursor.x;
29317 if (x < 0)
29319 width += x;
29320 x = 0;
29322 width = min (width, window_box_width (w, TEXT_AREA) - x);
29323 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29324 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29326 if (width > 0)
29327 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29330 /* Erase the cursor by redrawing the character underneath it. */
29331 if (mouse_face_here_p)
29332 hl = DRAW_MOUSE_FACE;
29333 else
29334 hl = DRAW_NORMAL_TEXT;
29335 draw_phys_cursor_glyph (w, cursor_row, hl);
29337 mark_cursor_off:
29338 w->phys_cursor_on_p = false;
29339 w->phys_cursor_type = NO_CURSOR;
29343 /* Display or clear cursor of window W. If !ON, clear the cursor.
29344 If ON, display the cursor; where to put the cursor is specified by
29345 HPOS, VPOS, X and Y. */
29347 void
29348 display_and_set_cursor (struct window *w, bool on,
29349 int hpos, int vpos, int x, int y)
29351 struct frame *f = XFRAME (w->frame);
29352 int new_cursor_type;
29353 int new_cursor_width;
29354 bool active_cursor;
29355 struct glyph_row *glyph_row;
29356 struct glyph *glyph;
29358 /* This is pointless on invisible frames, and dangerous on garbaged
29359 windows and frames; in the latter case, the frame or window may
29360 be in the midst of changing its size, and x and y may be off the
29361 window. */
29362 if (! FRAME_VISIBLE_P (f)
29363 || vpos >= w->current_matrix->nrows
29364 || hpos >= w->current_matrix->matrix_w)
29365 return;
29367 /* If cursor is off and we want it off, return quickly. */
29368 if (!on && !w->phys_cursor_on_p)
29369 return;
29371 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29372 /* If cursor row is not enabled, we don't really know where to
29373 display the cursor. */
29374 if (!glyph_row->enabled_p)
29376 w->phys_cursor_on_p = false;
29377 return;
29380 /* A frame might be marked garbaged even though its cursor position
29381 is correct, and will not change upon subsequent redisplay. This
29382 happens in some rare situations, like toggling the sort order in
29383 Dired windows. We've already established that VPOS is valid, so
29384 it shouldn't do any harm to record the cursor position, as we are
29385 going to return without acting on it anyway. Otherwise, expose
29386 events might come in and call update_window_cursor, which will
29387 blindly use outdated values in w->phys_cursor. */
29388 if (FRAME_GARBAGED_P (f))
29390 if (on)
29392 w->phys_cursor.x = x;
29393 w->phys_cursor.y = glyph_row->y;
29394 w->phys_cursor.hpos = hpos;
29395 w->phys_cursor.vpos = vpos;
29397 return;
29400 glyph = NULL;
29401 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29402 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29404 eassert (input_blocked_p ());
29406 /* Set new_cursor_type to the cursor we want to be displayed. */
29407 new_cursor_type = get_window_cursor_type (w, glyph,
29408 &new_cursor_width, &active_cursor);
29410 /* If cursor is currently being shown and we don't want it to be or
29411 it is in the wrong place, or the cursor type is not what we want,
29412 erase it. */
29413 if (w->phys_cursor_on_p
29414 && (!on
29415 || w->phys_cursor.x != x
29416 || w->phys_cursor.y != y
29417 /* HPOS can be negative in R2L rows whose
29418 exact_window_width_line_p flag is set (i.e. their newline
29419 would "overflow into the fringe"). */
29420 || hpos < 0
29421 || new_cursor_type != w->phys_cursor_type
29422 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29423 && new_cursor_width != w->phys_cursor_width)))
29424 erase_phys_cursor (w);
29426 /* Don't check phys_cursor_on_p here because that flag is only set
29427 to false in some cases where we know that the cursor has been
29428 completely erased, to avoid the extra work of erasing the cursor
29429 twice. In other words, phys_cursor_on_p can be true and the cursor
29430 still not be visible, or it has only been partly erased. */
29431 if (on)
29433 w->phys_cursor_ascent = glyph_row->ascent;
29434 w->phys_cursor_height = glyph_row->height;
29436 /* Set phys_cursor_.* before x_draw_.* is called because some
29437 of them may need the information. */
29438 w->phys_cursor.x = x;
29439 w->phys_cursor.y = glyph_row->y;
29440 w->phys_cursor.hpos = hpos;
29441 w->phys_cursor.vpos = vpos;
29444 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29445 new_cursor_type, new_cursor_width,
29446 on, active_cursor);
29450 /* Switch the display of W's cursor on or off, according to the value
29451 of ON. */
29453 static void
29454 update_window_cursor (struct window *w, bool on)
29456 /* Don't update cursor in windows whose frame is in the process
29457 of being deleted. */
29458 if (w->current_matrix)
29460 int hpos = w->phys_cursor.hpos;
29461 int vpos = w->phys_cursor.vpos;
29462 struct glyph_row *row;
29464 if (vpos >= w->current_matrix->nrows
29465 || hpos >= w->current_matrix->matrix_w)
29466 return;
29468 row = MATRIX_ROW (w->current_matrix, vpos);
29470 /* When the window is hscrolled, cursor hpos can legitimately be
29471 out of bounds, but we draw the cursor at the corresponding
29472 window margin in that case. */
29473 if (!row->reversed_p && hpos < 0)
29474 hpos = 0;
29475 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29476 hpos = row->used[TEXT_AREA] - 1;
29478 block_input ();
29479 display_and_set_cursor (w, on, hpos, vpos,
29480 w->phys_cursor.x, w->phys_cursor.y);
29481 unblock_input ();
29486 /* Call update_window_cursor with parameter ON_P on all leaf windows
29487 in the window tree rooted at W. */
29489 static void
29490 update_cursor_in_window_tree (struct window *w, bool on_p)
29492 while (w)
29494 if (WINDOWP (w->contents))
29495 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29496 else
29497 update_window_cursor (w, on_p);
29499 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29504 /* EXPORT:
29505 Display the cursor on window W, or clear it, according to ON_P.
29506 Don't change the cursor's position. */
29508 void
29509 x_update_cursor (struct frame *f, bool on_p)
29511 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29515 /* EXPORT:
29516 Clear the cursor of window W to background color, and mark the
29517 cursor as not shown. This is used when the text where the cursor
29518 is about to be rewritten. */
29520 void
29521 x_clear_cursor (struct window *w)
29523 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29524 update_window_cursor (w, false);
29527 #endif /* HAVE_WINDOW_SYSTEM */
29529 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29530 and MSDOS. */
29531 static void
29532 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29533 int start_hpos, int end_hpos,
29534 enum draw_glyphs_face draw)
29536 #ifdef HAVE_WINDOW_SYSTEM
29537 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29539 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29540 return;
29542 #endif
29543 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29544 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29545 #endif
29548 /* Display the active region described by mouse_face_* according to DRAW. */
29550 static void
29551 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29553 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29554 struct frame *f = XFRAME (WINDOW_FRAME (w));
29556 if (/* If window is in the process of being destroyed, don't bother
29557 to do anything. */
29558 w->current_matrix != NULL
29559 /* Don't update mouse highlight if hidden. */
29560 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29561 /* Recognize when we are called to operate on rows that don't exist
29562 anymore. This can happen when a window is split. */
29563 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29565 bool phys_cursor_on_p = w->phys_cursor_on_p;
29566 struct glyph_row *row, *first, *last;
29568 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29569 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29571 for (row = first; row <= last && row->enabled_p; ++row)
29573 int start_hpos, end_hpos, start_x;
29575 /* For all but the first row, the highlight starts at column 0. */
29576 if (row == first)
29578 /* R2L rows have BEG and END in reversed order, but the
29579 screen drawing geometry is always left to right. So
29580 we need to mirror the beginning and end of the
29581 highlighted area in R2L rows. */
29582 if (!row->reversed_p)
29584 start_hpos = hlinfo->mouse_face_beg_col;
29585 start_x = hlinfo->mouse_face_beg_x;
29587 else if (row == last)
29589 start_hpos = hlinfo->mouse_face_end_col;
29590 start_x = hlinfo->mouse_face_end_x;
29592 else
29594 start_hpos = 0;
29595 start_x = 0;
29598 else if (row->reversed_p && row == last)
29600 start_hpos = hlinfo->mouse_face_end_col;
29601 start_x = hlinfo->mouse_face_end_x;
29603 else
29605 start_hpos = 0;
29606 start_x = 0;
29609 if (row == last)
29611 if (!row->reversed_p)
29612 end_hpos = hlinfo->mouse_face_end_col;
29613 else if (row == first)
29614 end_hpos = hlinfo->mouse_face_beg_col;
29615 else
29617 end_hpos = row->used[TEXT_AREA];
29618 if (draw == DRAW_NORMAL_TEXT)
29619 row->fill_line_p = true; /* Clear to end of line. */
29622 else if (row->reversed_p && row == first)
29623 end_hpos = hlinfo->mouse_face_beg_col;
29624 else
29626 end_hpos = row->used[TEXT_AREA];
29627 if (draw == DRAW_NORMAL_TEXT)
29628 row->fill_line_p = true; /* Clear to end of line. */
29631 if (end_hpos > start_hpos)
29633 draw_row_with_mouse_face (w, start_x, row,
29634 start_hpos, end_hpos, draw);
29636 row->mouse_face_p
29637 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29641 /* When we've written over the cursor, arrange for it to
29642 be displayed again. */
29643 if (FRAME_WINDOW_P (f)
29644 && phys_cursor_on_p && !w->phys_cursor_on_p)
29646 #ifdef HAVE_WINDOW_SYSTEM
29647 int hpos = w->phys_cursor.hpos;
29649 /* When the window is hscrolled, cursor hpos can legitimately be
29650 out of bounds, but we draw the cursor at the corresponding
29651 window margin in that case. */
29652 if (!row->reversed_p && hpos < 0)
29653 hpos = 0;
29654 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29655 hpos = row->used[TEXT_AREA] - 1;
29657 block_input ();
29658 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29659 w->phys_cursor.x, w->phys_cursor.y);
29660 unblock_input ();
29661 #endif /* HAVE_WINDOW_SYSTEM */
29665 #ifdef HAVE_WINDOW_SYSTEM
29666 /* Change the mouse cursor. */
29667 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29669 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29670 if (draw == DRAW_NORMAL_TEXT
29671 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29672 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29673 else
29674 #endif
29675 if (draw == DRAW_MOUSE_FACE)
29676 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29677 else
29678 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29680 #endif /* HAVE_WINDOW_SYSTEM */
29683 /* EXPORT:
29684 Clear out the mouse-highlighted active region.
29685 Redraw it un-highlighted first. Value is true if mouse
29686 face was actually drawn unhighlighted. */
29688 bool
29689 clear_mouse_face (Mouse_HLInfo *hlinfo)
29691 bool cleared
29692 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29693 if (cleared)
29694 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29695 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29696 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29697 hlinfo->mouse_face_window = Qnil;
29698 hlinfo->mouse_face_overlay = Qnil;
29699 return cleared;
29702 /* Return true if the coordinates HPOS and VPOS on windows W are
29703 within the mouse face on that window. */
29704 static bool
29705 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29707 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29709 /* Quickly resolve the easy cases. */
29710 if (!(WINDOWP (hlinfo->mouse_face_window)
29711 && XWINDOW (hlinfo->mouse_face_window) == w))
29712 return false;
29713 if (vpos < hlinfo->mouse_face_beg_row
29714 || vpos > hlinfo->mouse_face_end_row)
29715 return false;
29716 if (vpos > hlinfo->mouse_face_beg_row
29717 && vpos < hlinfo->mouse_face_end_row)
29718 return true;
29720 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29722 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29724 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29725 return true;
29727 else if ((vpos == hlinfo->mouse_face_beg_row
29728 && hpos >= hlinfo->mouse_face_beg_col)
29729 || (vpos == hlinfo->mouse_face_end_row
29730 && hpos < hlinfo->mouse_face_end_col))
29731 return true;
29733 else
29735 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29737 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29738 return true;
29740 else if ((vpos == hlinfo->mouse_face_beg_row
29741 && hpos <= hlinfo->mouse_face_beg_col)
29742 || (vpos == hlinfo->mouse_face_end_row
29743 && hpos > hlinfo->mouse_face_end_col))
29744 return true;
29746 return false;
29750 /* EXPORT:
29751 True if physical cursor of window W is within mouse face. */
29753 bool
29754 cursor_in_mouse_face_p (struct window *w)
29756 int hpos = w->phys_cursor.hpos;
29757 int vpos = w->phys_cursor.vpos;
29758 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29760 /* When the window is hscrolled, cursor hpos can legitimately be out
29761 of bounds, but we draw the cursor at the corresponding window
29762 margin in that case. */
29763 if (!row->reversed_p && hpos < 0)
29764 hpos = 0;
29765 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29766 hpos = row->used[TEXT_AREA] - 1;
29768 return coords_in_mouse_face_p (w, hpos, vpos);
29773 /* Find the glyph rows START_ROW and END_ROW of window W that display
29774 characters between buffer positions START_CHARPOS and END_CHARPOS
29775 (excluding END_CHARPOS). DISP_STRING is a display string that
29776 covers these buffer positions. This is similar to
29777 row_containing_pos, but is more accurate when bidi reordering makes
29778 buffer positions change non-linearly with glyph rows. */
29779 static void
29780 rows_from_pos_range (struct window *w,
29781 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29782 Lisp_Object disp_string,
29783 struct glyph_row **start, struct glyph_row **end)
29785 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29786 int last_y = window_text_bottom_y (w);
29787 struct glyph_row *row;
29789 *start = NULL;
29790 *end = NULL;
29792 while (!first->enabled_p
29793 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29794 first++;
29796 /* Find the START row. */
29797 for (row = first;
29798 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29799 row++)
29801 /* A row can potentially be the START row if the range of the
29802 characters it displays intersects the range
29803 [START_CHARPOS..END_CHARPOS). */
29804 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29805 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29806 /* See the commentary in row_containing_pos, for the
29807 explanation of the complicated way to check whether
29808 some position is beyond the end of the characters
29809 displayed by a row. */
29810 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29811 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29812 && !row->ends_at_zv_p
29813 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29814 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29815 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29816 && !row->ends_at_zv_p
29817 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29819 /* Found a candidate row. Now make sure at least one of the
29820 glyphs it displays has a charpos from the range
29821 [START_CHARPOS..END_CHARPOS).
29823 This is not obvious because bidi reordering could make
29824 buffer positions of a row be 1,2,3,102,101,100, and if we
29825 want to highlight characters in [50..60), we don't want
29826 this row, even though [50..60) does intersect [1..103),
29827 the range of character positions given by the row's start
29828 and end positions. */
29829 struct glyph *g = row->glyphs[TEXT_AREA];
29830 struct glyph *e = g + row->used[TEXT_AREA];
29832 while (g < e)
29834 if (((BUFFERP (g->object) || NILP (g->object))
29835 && start_charpos <= g->charpos && g->charpos < end_charpos)
29836 /* A glyph that comes from DISP_STRING is by
29837 definition to be highlighted. */
29838 || EQ (g->object, disp_string))
29839 *start = row;
29840 g++;
29842 if (*start)
29843 break;
29847 /* Find the END row. */
29848 if (!*start
29849 /* If the last row is partially visible, start looking for END
29850 from that row, instead of starting from FIRST. */
29851 && !(row->enabled_p
29852 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29853 row = first;
29854 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29856 struct glyph_row *next = row + 1;
29857 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29859 if (!next->enabled_p
29860 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29861 /* The first row >= START whose range of displayed characters
29862 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29863 is the row END + 1. */
29864 || (start_charpos < next_start
29865 && end_charpos < next_start)
29866 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29867 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29868 && !next->ends_at_zv_p
29869 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29870 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29871 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29872 && !next->ends_at_zv_p
29873 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29875 *end = row;
29876 break;
29878 else
29880 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29881 but none of the characters it displays are in the range, it is
29882 also END + 1. */
29883 struct glyph *g = next->glyphs[TEXT_AREA];
29884 struct glyph *s = g;
29885 struct glyph *e = g + next->used[TEXT_AREA];
29887 while (g < e)
29889 if (((BUFFERP (g->object) || NILP (g->object))
29890 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29891 /* If the buffer position of the first glyph in
29892 the row is equal to END_CHARPOS, it means
29893 the last character to be highlighted is the
29894 newline of ROW, and we must consider NEXT as
29895 END, not END+1. */
29896 || (((!next->reversed_p && g == s)
29897 || (next->reversed_p && g == e - 1))
29898 && (g->charpos == end_charpos
29899 /* Special case for when NEXT is an
29900 empty line at ZV. */
29901 || (g->charpos == -1
29902 && !row->ends_at_zv_p
29903 && next_start == end_charpos)))))
29904 /* A glyph that comes from DISP_STRING is by
29905 definition to be highlighted. */
29906 || EQ (g->object, disp_string))
29907 break;
29908 g++;
29910 if (g == e)
29912 *end = row;
29913 break;
29915 /* The first row that ends at ZV must be the last to be
29916 highlighted. */
29917 else if (next->ends_at_zv_p)
29919 *end = next;
29920 break;
29926 /* This function sets the mouse_face_* elements of HLINFO, assuming
29927 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29928 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29929 for the overlay or run of text properties specifying the mouse
29930 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29931 before-string and after-string that must also be highlighted.
29932 DISP_STRING, if non-nil, is a display string that may cover some
29933 or all of the highlighted text. */
29935 static void
29936 mouse_face_from_buffer_pos (Lisp_Object window,
29937 Mouse_HLInfo *hlinfo,
29938 ptrdiff_t mouse_charpos,
29939 ptrdiff_t start_charpos,
29940 ptrdiff_t end_charpos,
29941 Lisp_Object before_string,
29942 Lisp_Object after_string,
29943 Lisp_Object disp_string)
29945 struct window *w = XWINDOW (window);
29946 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29947 struct glyph_row *r1, *r2;
29948 struct glyph *glyph, *end;
29949 ptrdiff_t ignore, pos;
29950 int x;
29952 eassert (NILP (disp_string) || STRINGP (disp_string));
29953 eassert (NILP (before_string) || STRINGP (before_string));
29954 eassert (NILP (after_string) || STRINGP (after_string));
29956 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29957 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29958 if (r1 == NULL)
29959 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29960 /* If the before-string or display-string contains newlines,
29961 rows_from_pos_range skips to its last row. Move back. */
29962 if (!NILP (before_string) || !NILP (disp_string))
29964 struct glyph_row *prev;
29965 while ((prev = r1 - 1, prev >= first)
29966 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29967 && prev->used[TEXT_AREA] > 0)
29969 struct glyph *beg = prev->glyphs[TEXT_AREA];
29970 glyph = beg + prev->used[TEXT_AREA];
29971 while (--glyph >= beg && NILP (glyph->object));
29972 if (glyph < beg
29973 || !(EQ (glyph->object, before_string)
29974 || EQ (glyph->object, disp_string)))
29975 break;
29976 r1 = prev;
29979 if (r2 == NULL)
29981 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29982 hlinfo->mouse_face_past_end = true;
29984 else if (!NILP (after_string))
29986 /* If the after-string has newlines, advance to its last row. */
29987 struct glyph_row *next;
29988 struct glyph_row *last
29989 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29991 for (next = r2 + 1;
29992 next <= last
29993 && next->used[TEXT_AREA] > 0
29994 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29995 ++next)
29996 r2 = next;
29998 /* The rest of the display engine assumes that mouse_face_beg_row is
29999 either above mouse_face_end_row or identical to it. But with
30000 bidi-reordered continued lines, the row for START_CHARPOS could
30001 be below the row for END_CHARPOS. If so, swap the rows and store
30002 them in correct order. */
30003 if (r1->y > r2->y)
30005 struct glyph_row *tem = r2;
30007 r2 = r1;
30008 r1 = tem;
30011 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30012 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30014 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30015 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30016 could be anywhere in the row and in any order. The strategy
30017 below is to find the leftmost and the rightmost glyph that
30018 belongs to either of these 3 strings, or whose position is
30019 between START_CHARPOS and END_CHARPOS, and highlight all the
30020 glyphs between those two. This may cover more than just the text
30021 between START_CHARPOS and END_CHARPOS if the range of characters
30022 strides the bidi level boundary, e.g. if the beginning is in R2L
30023 text while the end is in L2R text or vice versa. */
30024 if (!r1->reversed_p)
30026 /* This row is in a left to right paragraph. Scan it left to
30027 right. */
30028 glyph = r1->glyphs[TEXT_AREA];
30029 end = glyph + r1->used[TEXT_AREA];
30030 x = r1->x;
30032 /* Skip truncation glyphs at the start of the glyph row. */
30033 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30034 for (; glyph < end
30035 && NILP (glyph->object)
30036 && glyph->charpos < 0;
30037 ++glyph)
30038 x += glyph->pixel_width;
30040 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30041 or DISP_STRING, and the first glyph from buffer whose
30042 position is between START_CHARPOS and END_CHARPOS. */
30043 for (; glyph < end
30044 && !NILP (glyph->object)
30045 && !EQ (glyph->object, disp_string)
30046 && !(BUFFERP (glyph->object)
30047 && (glyph->charpos >= start_charpos
30048 && glyph->charpos < end_charpos));
30049 ++glyph)
30051 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30052 are present at buffer positions between START_CHARPOS and
30053 END_CHARPOS, or if they come from an overlay. */
30054 if (EQ (glyph->object, before_string))
30056 pos = string_buffer_position (before_string,
30057 start_charpos);
30058 /* If pos == 0, it means before_string came from an
30059 overlay, not from a buffer position. */
30060 if (!pos || (pos >= start_charpos && pos < end_charpos))
30061 break;
30063 else if (EQ (glyph->object, after_string))
30065 pos = string_buffer_position (after_string, end_charpos);
30066 if (!pos || (pos >= start_charpos && pos < end_charpos))
30067 break;
30069 x += glyph->pixel_width;
30071 hlinfo->mouse_face_beg_x = x;
30072 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30074 else
30076 /* This row is in a right to left paragraph. Scan it right to
30077 left. */
30078 struct glyph *g;
30080 end = r1->glyphs[TEXT_AREA] - 1;
30081 glyph = end + r1->used[TEXT_AREA];
30083 /* Skip truncation glyphs at the start of the glyph row. */
30084 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30085 for (; glyph > end
30086 && NILP (glyph->object)
30087 && glyph->charpos < 0;
30088 --glyph)
30091 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30092 or DISP_STRING, and the first glyph from buffer whose
30093 position is between START_CHARPOS and END_CHARPOS. */
30094 for (; glyph > end
30095 && !NILP (glyph->object)
30096 && !EQ (glyph->object, disp_string)
30097 && !(BUFFERP (glyph->object)
30098 && (glyph->charpos >= start_charpos
30099 && glyph->charpos < end_charpos));
30100 --glyph)
30102 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30103 are present at buffer positions between START_CHARPOS and
30104 END_CHARPOS, or if they come from an overlay. */
30105 if (EQ (glyph->object, before_string))
30107 pos = string_buffer_position (before_string, start_charpos);
30108 /* If pos == 0, it means before_string came from an
30109 overlay, not from a buffer position. */
30110 if (!pos || (pos >= start_charpos && pos < end_charpos))
30111 break;
30113 else if (EQ (glyph->object, after_string))
30115 pos = string_buffer_position (after_string, end_charpos);
30116 if (!pos || (pos >= start_charpos && pos < end_charpos))
30117 break;
30121 glyph++; /* first glyph to the right of the highlighted area */
30122 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30123 x += g->pixel_width;
30124 hlinfo->mouse_face_beg_x = x;
30125 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30128 /* If the highlight ends in a different row, compute GLYPH and END
30129 for the end row. Otherwise, reuse the values computed above for
30130 the row where the highlight begins. */
30131 if (r2 != r1)
30133 if (!r2->reversed_p)
30135 glyph = r2->glyphs[TEXT_AREA];
30136 end = glyph + r2->used[TEXT_AREA];
30137 x = r2->x;
30139 else
30141 end = r2->glyphs[TEXT_AREA] - 1;
30142 glyph = end + r2->used[TEXT_AREA];
30146 if (!r2->reversed_p)
30148 /* Skip truncation and continuation glyphs near the end of the
30149 row, and also blanks and stretch glyphs inserted by
30150 extend_face_to_end_of_line. */
30151 while (end > glyph
30152 && NILP ((end - 1)->object))
30153 --end;
30154 /* Scan the rest of the glyph row from the end, looking for the
30155 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30156 DISP_STRING, or whose position is between START_CHARPOS
30157 and END_CHARPOS */
30158 for (--end;
30159 end > glyph
30160 && !NILP (end->object)
30161 && !EQ (end->object, disp_string)
30162 && !(BUFFERP (end->object)
30163 && (end->charpos >= start_charpos
30164 && end->charpos < end_charpos));
30165 --end)
30167 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30168 are present at buffer positions between START_CHARPOS and
30169 END_CHARPOS, or if they come from an overlay. */
30170 if (EQ (end->object, before_string))
30172 pos = string_buffer_position (before_string, start_charpos);
30173 if (!pos || (pos >= start_charpos && pos < end_charpos))
30174 break;
30176 else if (EQ (end->object, after_string))
30178 pos = string_buffer_position (after_string, end_charpos);
30179 if (!pos || (pos >= start_charpos && pos < end_charpos))
30180 break;
30183 /* Find the X coordinate of the last glyph to be highlighted. */
30184 for (; glyph <= end; ++glyph)
30185 x += glyph->pixel_width;
30187 hlinfo->mouse_face_end_x = x;
30188 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30190 else
30192 /* Skip truncation and continuation glyphs near the end of the
30193 row, and also blanks and stretch glyphs inserted by
30194 extend_face_to_end_of_line. */
30195 x = r2->x;
30196 end++;
30197 while (end < glyph
30198 && NILP (end->object))
30200 x += end->pixel_width;
30201 ++end;
30203 /* Scan the rest of the glyph row from the end, looking for the
30204 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30205 DISP_STRING, or whose position is between START_CHARPOS
30206 and END_CHARPOS */
30207 for ( ;
30208 end < glyph
30209 && !NILP (end->object)
30210 && !EQ (end->object, disp_string)
30211 && !(BUFFERP (end->object)
30212 && (end->charpos >= start_charpos
30213 && end->charpos < end_charpos));
30214 ++end)
30216 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30217 are present at buffer positions between START_CHARPOS and
30218 END_CHARPOS, or if they come from an overlay. */
30219 if (EQ (end->object, before_string))
30221 pos = string_buffer_position (before_string, start_charpos);
30222 if (!pos || (pos >= start_charpos && pos < end_charpos))
30223 break;
30225 else if (EQ (end->object, after_string))
30227 pos = string_buffer_position (after_string, end_charpos);
30228 if (!pos || (pos >= start_charpos && pos < end_charpos))
30229 break;
30231 x += end->pixel_width;
30233 /* If we exited the above loop because we arrived at the last
30234 glyph of the row, and its buffer position is still not in
30235 range, it means the last character in range is the preceding
30236 newline. Bump the end column and x values to get past the
30237 last glyph. */
30238 if (end == glyph
30239 && BUFFERP (end->object)
30240 && (end->charpos < start_charpos
30241 || end->charpos >= end_charpos))
30243 x += end->pixel_width;
30244 ++end;
30246 hlinfo->mouse_face_end_x = x;
30247 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30250 hlinfo->mouse_face_window = window;
30251 hlinfo->mouse_face_face_id
30252 = face_at_buffer_position (w, mouse_charpos, &ignore,
30253 mouse_charpos + 1,
30254 !hlinfo->mouse_face_hidden, -1);
30255 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30258 /* The following function is not used anymore (replaced with
30259 mouse_face_from_string_pos), but I leave it here for the time
30260 being, in case someone would. */
30262 #if false /* not used */
30264 /* Find the position of the glyph for position POS in OBJECT in
30265 window W's current matrix, and return in *X, *Y the pixel
30266 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30268 RIGHT_P means return the position of the right edge of the glyph.
30269 !RIGHT_P means return the left edge position.
30271 If no glyph for POS exists in the matrix, return the position of
30272 the glyph with the next smaller position that is in the matrix, if
30273 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30274 exists in the matrix, return the position of the glyph with the
30275 next larger position in OBJECT.
30277 Value is true if a glyph was found. */
30279 static bool
30280 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30281 int *hpos, int *vpos, int *x, int *y, bool right_p)
30283 int yb = window_text_bottom_y (w);
30284 struct glyph_row *r;
30285 struct glyph *best_glyph = NULL;
30286 struct glyph_row *best_row = NULL;
30287 int best_x = 0;
30289 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30290 r->enabled_p && r->y < yb;
30291 ++r)
30293 struct glyph *g = r->glyphs[TEXT_AREA];
30294 struct glyph *e = g + r->used[TEXT_AREA];
30295 int gx;
30297 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30298 if (EQ (g->object, object))
30300 if (g->charpos == pos)
30302 best_glyph = g;
30303 best_x = gx;
30304 best_row = r;
30305 goto found;
30307 else if (best_glyph == NULL
30308 || ((eabs (g->charpos - pos)
30309 < eabs (best_glyph->charpos - pos))
30310 && (right_p
30311 ? g->charpos < pos
30312 : g->charpos > pos)))
30314 best_glyph = g;
30315 best_x = gx;
30316 best_row = r;
30321 found:
30323 if (best_glyph)
30325 *x = best_x;
30326 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30328 if (right_p)
30330 *x += best_glyph->pixel_width;
30331 ++*hpos;
30334 *y = best_row->y;
30335 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30338 return best_glyph != NULL;
30340 #endif /* not used */
30342 /* Find the positions of the first and the last glyphs in window W's
30343 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30344 (assumed to be a string), and return in HLINFO's mouse_face_*
30345 members the pixel and column/row coordinates of those glyphs. */
30347 static void
30348 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30349 Lisp_Object object,
30350 ptrdiff_t startpos, ptrdiff_t endpos)
30352 int yb = window_text_bottom_y (w);
30353 struct glyph_row *r;
30354 struct glyph *g, *e;
30355 int gx;
30356 bool found = false;
30358 /* Find the glyph row with at least one position in the range
30359 [STARTPOS..ENDPOS), and the first glyph in that row whose
30360 position belongs to that range. */
30361 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30362 r->enabled_p && r->y < yb;
30363 ++r)
30365 if (!r->reversed_p)
30367 g = r->glyphs[TEXT_AREA];
30368 e = g + r->used[TEXT_AREA];
30369 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30370 if (EQ (g->object, object)
30371 && startpos <= g->charpos && g->charpos < endpos)
30373 hlinfo->mouse_face_beg_row
30374 = MATRIX_ROW_VPOS (r, w->current_matrix);
30375 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30376 hlinfo->mouse_face_beg_x = gx;
30377 found = true;
30378 break;
30381 else
30383 struct glyph *g1;
30385 e = r->glyphs[TEXT_AREA];
30386 g = e + r->used[TEXT_AREA];
30387 for ( ; g > e; --g)
30388 if (EQ ((g-1)->object, object)
30389 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30391 hlinfo->mouse_face_beg_row
30392 = MATRIX_ROW_VPOS (r, w->current_matrix);
30393 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30394 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30395 gx += g1->pixel_width;
30396 hlinfo->mouse_face_beg_x = gx;
30397 found = true;
30398 break;
30401 if (found)
30402 break;
30405 if (!found)
30406 return;
30408 /* Starting with the next row, look for the first row which does NOT
30409 include any glyphs whose positions are in the range. */
30410 for (++r; r->enabled_p && r->y < yb; ++r)
30412 g = r->glyphs[TEXT_AREA];
30413 e = g + r->used[TEXT_AREA];
30414 found = false;
30415 for ( ; g < e; ++g)
30416 if (EQ (g->object, object)
30417 && startpos <= g->charpos && g->charpos < endpos)
30419 found = true;
30420 break;
30422 if (!found)
30423 break;
30426 /* The highlighted region ends on the previous row. */
30427 r--;
30429 /* Set the end row. */
30430 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30432 /* Compute and set the end column and the end column's horizontal
30433 pixel coordinate. */
30434 if (!r->reversed_p)
30436 g = r->glyphs[TEXT_AREA];
30437 e = g + r->used[TEXT_AREA];
30438 for ( ; e > g; --e)
30439 if (EQ ((e-1)->object, object)
30440 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30441 break;
30442 hlinfo->mouse_face_end_col = e - g;
30444 for (gx = r->x; g < e; ++g)
30445 gx += g->pixel_width;
30446 hlinfo->mouse_face_end_x = gx;
30448 else
30450 e = r->glyphs[TEXT_AREA];
30451 g = e + r->used[TEXT_AREA];
30452 for (gx = r->x ; e < g; ++e)
30454 if (EQ (e->object, object)
30455 && startpos <= e->charpos && e->charpos < endpos)
30456 break;
30457 gx += e->pixel_width;
30459 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30460 hlinfo->mouse_face_end_x = gx;
30464 #ifdef HAVE_WINDOW_SYSTEM
30466 /* See if position X, Y is within a hot-spot of an image. */
30468 static bool
30469 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30471 if (!CONSP (hot_spot))
30472 return false;
30474 if (EQ (XCAR (hot_spot), Qrect))
30476 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30477 Lisp_Object rect = XCDR (hot_spot);
30478 Lisp_Object tem;
30479 if (!CONSP (rect))
30480 return false;
30481 if (!CONSP (XCAR (rect)))
30482 return false;
30483 if (!CONSP (XCDR (rect)))
30484 return false;
30485 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30486 return false;
30487 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30488 return false;
30489 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30490 return false;
30491 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30492 return false;
30493 return true;
30495 else if (EQ (XCAR (hot_spot), Qcircle))
30497 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30498 Lisp_Object circ = XCDR (hot_spot);
30499 Lisp_Object lr, lx0, ly0;
30500 if (CONSP (circ)
30501 && CONSP (XCAR (circ))
30502 && (lr = XCDR (circ), NUMBERP (lr))
30503 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30504 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30506 double r = XFLOATINT (lr);
30507 double dx = XINT (lx0) - x;
30508 double dy = XINT (ly0) - y;
30509 return (dx * dx + dy * dy <= r * r);
30512 else if (EQ (XCAR (hot_spot), Qpoly))
30514 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30515 if (VECTORP (XCDR (hot_spot)))
30517 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30518 Lisp_Object *poly = v->contents;
30519 ptrdiff_t n = v->header.size;
30520 ptrdiff_t i;
30521 bool inside = false;
30522 Lisp_Object lx, ly;
30523 int x0, y0;
30525 /* Need an even number of coordinates, and at least 3 edges. */
30526 if (n < 6 || n & 1)
30527 return false;
30529 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30530 If count is odd, we are inside polygon. Pixels on edges
30531 may or may not be included depending on actual geometry of the
30532 polygon. */
30533 if ((lx = poly[n-2], !INTEGERP (lx))
30534 || (ly = poly[n-1], !INTEGERP (lx)))
30535 return false;
30536 x0 = XINT (lx), y0 = XINT (ly);
30537 for (i = 0; i < n; i += 2)
30539 int x1 = x0, y1 = y0;
30540 if ((lx = poly[i], !INTEGERP (lx))
30541 || (ly = poly[i+1], !INTEGERP (ly)))
30542 return false;
30543 x0 = XINT (lx), y0 = XINT (ly);
30545 /* Does this segment cross the X line? */
30546 if (x0 >= x)
30548 if (x1 >= x)
30549 continue;
30551 else if (x1 < x)
30552 continue;
30553 if (y > y0 && y > y1)
30554 continue;
30555 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30556 inside = !inside;
30558 return inside;
30561 return false;
30564 Lisp_Object
30565 find_hot_spot (Lisp_Object map, int x, int y)
30567 while (CONSP (map))
30569 if (CONSP (XCAR (map))
30570 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30571 return XCAR (map);
30572 map = XCDR (map);
30575 return Qnil;
30578 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30579 3, 3, 0,
30580 doc: /* Lookup in image map MAP coordinates X and Y.
30581 An image map is an alist where each element has the format (AREA ID PLIST).
30582 An AREA is specified as either a rectangle, a circle, or a polygon:
30583 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30584 pixel coordinates of the upper left and bottom right corners.
30585 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30586 and the radius of the circle; r may be a float or integer.
30587 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30588 vector describes one corner in the polygon.
30589 Returns the alist element for the first matching AREA in MAP. */)
30590 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30592 if (NILP (map))
30593 return Qnil;
30595 CHECK_NUMBER (x);
30596 CHECK_NUMBER (y);
30598 return find_hot_spot (map,
30599 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30600 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30602 #endif /* HAVE_WINDOW_SYSTEM */
30605 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30606 static void
30607 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30609 #ifdef HAVE_WINDOW_SYSTEM
30610 if (!FRAME_WINDOW_P (f))
30611 return;
30613 /* Do not change cursor shape while dragging mouse. */
30614 if (EQ (do_mouse_tracking, Qdragging))
30615 return;
30617 if (!NILP (pointer))
30619 if (EQ (pointer, Qarrow))
30620 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30621 else if (EQ (pointer, Qhand))
30622 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30623 else if (EQ (pointer, Qtext))
30624 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30625 else if (EQ (pointer, intern ("hdrag")))
30626 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30627 else if (EQ (pointer, intern ("nhdrag")))
30628 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30629 # ifdef HAVE_X_WINDOWS
30630 else if (EQ (pointer, intern ("vdrag")))
30631 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30632 # endif
30633 else if (EQ (pointer, intern ("hourglass")))
30634 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30635 else if (EQ (pointer, Qmodeline))
30636 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30637 else
30638 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30641 if (cursor != No_Cursor)
30642 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30643 #endif
30646 /* Take proper action when mouse has moved to the mode or header line
30647 or marginal area AREA of window W, x-position X and y-position Y.
30648 X is relative to the start of the text display area of W, so the
30649 width of bitmap areas and scroll bars must be subtracted to get a
30650 position relative to the start of the mode line. */
30652 static void
30653 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30654 enum window_part area)
30656 struct window *w = XWINDOW (window);
30657 struct frame *f = XFRAME (w->frame);
30658 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30659 #ifdef HAVE_WINDOW_SYSTEM
30660 Display_Info *dpyinfo;
30661 #endif
30662 Cursor cursor = No_Cursor;
30663 Lisp_Object pointer = Qnil;
30664 int dx, dy, width, height;
30665 ptrdiff_t charpos;
30666 Lisp_Object string, object = Qnil;
30667 Lisp_Object pos UNINIT;
30668 Lisp_Object mouse_face;
30669 int original_x_pixel = x;
30670 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30671 struct glyph_row *row UNINIT;
30673 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30675 int x0;
30676 struct glyph *end;
30678 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30679 returns them in row/column units! */
30680 string = mode_line_string (w, area, &x, &y, &charpos,
30681 &object, &dx, &dy, &width, &height);
30683 row = (area == ON_MODE_LINE
30684 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30685 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30687 /* Find the glyph under the mouse pointer. */
30688 if (row->mode_line_p && row->enabled_p)
30690 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30691 end = glyph + row->used[TEXT_AREA];
30693 for (x0 = original_x_pixel;
30694 glyph < end && x0 >= glyph->pixel_width;
30695 ++glyph)
30696 x0 -= glyph->pixel_width;
30698 if (glyph >= end)
30699 glyph = NULL;
30702 else
30704 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30705 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30706 returns them in row/column units! */
30707 string = marginal_area_string (w, area, &x, &y, &charpos,
30708 &object, &dx, &dy, &width, &height);
30711 Lisp_Object help = Qnil;
30713 #ifdef HAVE_WINDOW_SYSTEM
30714 if (IMAGEP (object))
30716 Lisp_Object image_map, hotspot;
30717 if ((image_map = Fplist_get (XCDR (object), QCmap),
30718 !NILP (image_map))
30719 && (hotspot = find_hot_spot (image_map, dx, dy),
30720 CONSP (hotspot))
30721 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30723 Lisp_Object plist;
30725 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30726 If so, we could look for mouse-enter, mouse-leave
30727 properties in PLIST (and do something...). */
30728 hotspot = XCDR (hotspot);
30729 if (CONSP (hotspot)
30730 && (plist = XCAR (hotspot), CONSP (plist)))
30732 pointer = Fplist_get (plist, Qpointer);
30733 if (NILP (pointer))
30734 pointer = Qhand;
30735 help = Fplist_get (plist, Qhelp_echo);
30736 if (!NILP (help))
30738 help_echo_string = help;
30739 XSETWINDOW (help_echo_window, w);
30740 help_echo_object = w->contents;
30741 help_echo_pos = charpos;
30745 if (NILP (pointer))
30746 pointer = Fplist_get (XCDR (object), QCpointer);
30748 #endif /* HAVE_WINDOW_SYSTEM */
30750 if (STRINGP (string))
30751 pos = make_number (charpos);
30753 /* Set the help text and mouse pointer. If the mouse is on a part
30754 of the mode line without any text (e.g. past the right edge of
30755 the mode line text), use the default help text and pointer. */
30756 if (STRINGP (string) || area == ON_MODE_LINE)
30758 /* Arrange to display the help by setting the global variables
30759 help_echo_string, help_echo_object, and help_echo_pos. */
30760 if (NILP (help))
30762 if (STRINGP (string))
30763 help = Fget_text_property (pos, Qhelp_echo, string);
30765 if (!NILP (help))
30767 help_echo_string = help;
30768 XSETWINDOW (help_echo_window, w);
30769 help_echo_object = string;
30770 help_echo_pos = charpos;
30772 else if (area == ON_MODE_LINE)
30774 Lisp_Object default_help
30775 = buffer_local_value (Qmode_line_default_help_echo,
30776 w->contents);
30778 if (STRINGP (default_help))
30780 help_echo_string = default_help;
30781 XSETWINDOW (help_echo_window, w);
30782 help_echo_object = Qnil;
30783 help_echo_pos = -1;
30788 #ifdef HAVE_WINDOW_SYSTEM
30789 /* Change the mouse pointer according to what is under it. */
30790 if (FRAME_WINDOW_P (f))
30792 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30793 || minibuf_level
30794 || NILP (Vresize_mini_windows));
30796 dpyinfo = FRAME_DISPLAY_INFO (f);
30797 if (STRINGP (string))
30799 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30801 if (NILP (pointer))
30802 pointer = Fget_text_property (pos, Qpointer, string);
30804 /* Change the mouse pointer according to what is under X/Y. */
30805 if (NILP (pointer)
30806 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30808 Lisp_Object map;
30809 map = Fget_text_property (pos, Qlocal_map, string);
30810 if (!KEYMAPP (map))
30811 map = Fget_text_property (pos, Qkeymap, string);
30812 if (!KEYMAPP (map) && draggable)
30813 cursor = dpyinfo->vertical_scroll_bar_cursor;
30816 else if (draggable)
30817 /* Default mode-line pointer. */
30818 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30820 #endif
30823 /* Change the mouse face according to what is under X/Y. */
30824 bool mouse_face_shown = false;
30825 if (STRINGP (string))
30827 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30828 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30829 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30830 && glyph)
30832 Lisp_Object b, e;
30834 struct glyph * tmp_glyph;
30836 int gpos;
30837 int gseq_length;
30838 int total_pixel_width;
30839 ptrdiff_t begpos, endpos, ignore;
30841 int vpos, hpos;
30843 b = Fprevious_single_property_change (make_number (charpos + 1),
30844 Qmouse_face, string, Qnil);
30845 if (NILP (b))
30846 begpos = 0;
30847 else
30848 begpos = XINT (b);
30850 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30851 if (NILP (e))
30852 endpos = SCHARS (string);
30853 else
30854 endpos = XINT (e);
30856 /* Calculate the glyph position GPOS of GLYPH in the
30857 displayed string, relative to the beginning of the
30858 highlighted part of the string.
30860 Note: GPOS is different from CHARPOS. CHARPOS is the
30861 position of GLYPH in the internal string object. A mode
30862 line string format has structures which are converted to
30863 a flattened string by the Emacs Lisp interpreter. The
30864 internal string is an element of those structures. The
30865 displayed string is the flattened string. */
30866 tmp_glyph = row_start_glyph;
30867 while (tmp_glyph < glyph
30868 && (!(EQ (tmp_glyph->object, glyph->object)
30869 && begpos <= tmp_glyph->charpos
30870 && tmp_glyph->charpos < endpos)))
30871 tmp_glyph++;
30872 gpos = glyph - tmp_glyph;
30874 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30875 the highlighted part of the displayed string to which
30876 GLYPH belongs. Note: GSEQ_LENGTH is different from
30877 SCHARS (STRING), because the latter returns the length of
30878 the internal string. */
30879 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30880 tmp_glyph > glyph
30881 && (!(EQ (tmp_glyph->object, glyph->object)
30882 && begpos <= tmp_glyph->charpos
30883 && tmp_glyph->charpos < endpos));
30884 tmp_glyph--)
30886 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30888 /* Calculate the total pixel width of all the glyphs between
30889 the beginning of the highlighted area and GLYPH. */
30890 total_pixel_width = 0;
30891 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30892 total_pixel_width += tmp_glyph->pixel_width;
30894 /* Pre calculation of re-rendering position. Note: X is in
30895 column units here, after the call to mode_line_string or
30896 marginal_area_string. */
30897 hpos = x - gpos;
30898 vpos = (area == ON_MODE_LINE
30899 ? (w->current_matrix)->nrows - 1
30900 : 0);
30902 /* If GLYPH's position is included in the region that is
30903 already drawn in mouse face, we have nothing to do. */
30904 if ( EQ (window, hlinfo->mouse_face_window)
30905 && (!row->reversed_p
30906 ? (hlinfo->mouse_face_beg_col <= hpos
30907 && hpos < hlinfo->mouse_face_end_col)
30908 /* In R2L rows we swap BEG and END, see below. */
30909 : (hlinfo->mouse_face_end_col <= hpos
30910 && hpos < hlinfo->mouse_face_beg_col))
30911 && hlinfo->mouse_face_beg_row == vpos )
30912 return;
30914 if (clear_mouse_face (hlinfo))
30915 cursor = No_Cursor;
30917 if (!row->reversed_p)
30919 hlinfo->mouse_face_beg_col = hpos;
30920 hlinfo->mouse_face_beg_x = original_x_pixel
30921 - (total_pixel_width + dx);
30922 hlinfo->mouse_face_end_col = hpos + gseq_length;
30923 hlinfo->mouse_face_end_x = 0;
30925 else
30927 /* In R2L rows, show_mouse_face expects BEG and END
30928 coordinates to be swapped. */
30929 hlinfo->mouse_face_end_col = hpos;
30930 hlinfo->mouse_face_end_x = original_x_pixel
30931 - (total_pixel_width + dx);
30932 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30933 hlinfo->mouse_face_beg_x = 0;
30936 hlinfo->mouse_face_beg_row = vpos;
30937 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30938 hlinfo->mouse_face_past_end = false;
30939 hlinfo->mouse_face_window = window;
30941 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30942 charpos,
30943 0, &ignore,
30944 glyph->face_id,
30945 true);
30946 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30947 mouse_face_shown = true;
30949 if (NILP (pointer))
30950 pointer = Qhand;
30954 /* If mouse-face doesn't need to be shown, clear any existing
30955 mouse-face. */
30956 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30957 clear_mouse_face (hlinfo);
30959 define_frame_cursor1 (f, cursor, pointer);
30963 /* EXPORT:
30964 Take proper action when the mouse has moved to position X, Y on
30965 frame F with regards to highlighting portions of display that have
30966 mouse-face properties. Also de-highlight portions of display where
30967 the mouse was before, set the mouse pointer shape as appropriate
30968 for the mouse coordinates, and activate help echo (tooltips).
30969 X and Y can be negative or out of range. */
30971 void
30972 note_mouse_highlight (struct frame *f, int x, int y)
30974 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30975 enum window_part part = ON_NOTHING;
30976 Lisp_Object window;
30977 struct window *w;
30978 Cursor cursor = No_Cursor;
30979 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30980 struct buffer *b;
30982 /* When a menu is active, don't highlight because this looks odd. */
30983 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30984 if (popup_activated ())
30985 return;
30986 #endif
30988 if (!f->glyphs_initialized_p
30989 || f->pointer_invisible)
30990 return;
30992 hlinfo->mouse_face_mouse_x = x;
30993 hlinfo->mouse_face_mouse_y = y;
30994 hlinfo->mouse_face_mouse_frame = f;
30996 if (hlinfo->mouse_face_defer)
30997 return;
30999 /* Which window is that in? */
31000 window = window_from_coordinates (f, x, y, &part, true);
31002 /* If displaying active text in another window, clear that. */
31003 if (! EQ (window, hlinfo->mouse_face_window)
31004 /* Also clear if we move out of text area in same window. */
31005 || (!NILP (hlinfo->mouse_face_window)
31006 && !NILP (window)
31007 && part != ON_TEXT
31008 && part != ON_MODE_LINE
31009 && part != ON_HEADER_LINE))
31010 clear_mouse_face (hlinfo);
31012 /* Reset help_echo_string. It will get recomputed below. */
31013 help_echo_string = Qnil;
31015 #ifdef HAVE_WINDOW_SYSTEM
31016 /* If the cursor is on the internal border of FRAME and FRAME's
31017 internal border is draggable, provide some visual feedback. */
31018 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31019 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31021 enum internal_border_part part = frame_internal_border_part (f, x, y);
31023 switch (part)
31025 case INTERNAL_BORDER_NONE:
31026 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31027 /* Reset cursor. */
31028 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31029 break;
31030 case INTERNAL_BORDER_LEFT_EDGE:
31031 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31032 break;
31033 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31034 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31035 break;
31036 case INTERNAL_BORDER_TOP_EDGE:
31037 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31038 break;
31039 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31040 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31041 break;
31042 case INTERNAL_BORDER_RIGHT_EDGE:
31043 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31044 break;
31045 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31046 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31047 break;
31048 case INTERNAL_BORDER_BOTTOM_EDGE:
31049 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31050 break;
31051 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31052 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31053 break;
31054 default:
31055 /* This should not happen. */
31056 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31057 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31060 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31062 /* Do we really want a help echo here? */
31063 help_echo_string = build_string ("drag-mouse-1: resize frame");
31064 goto set_cursor;
31067 #endif /* HAVE_WINDOW_SYSTEM */
31069 /* Not on a window -> return. */
31070 if (!WINDOWP (window))
31071 return;
31073 /* Convert to window-relative pixel coordinates. */
31074 w = XWINDOW (window);
31075 frame_to_window_pixel_xy (w, &x, &y);
31077 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31078 /* Handle tool-bar window differently since it doesn't display a
31079 buffer. */
31080 if (EQ (window, f->tool_bar_window))
31082 note_tool_bar_highlight (f, x, y);
31083 return;
31085 #endif
31087 /* Mouse is on the mode, header line or margin? */
31088 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31089 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31091 note_mode_line_or_margin_highlight (window, x, y, part);
31093 #ifdef HAVE_WINDOW_SYSTEM
31094 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31096 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31097 /* Show non-text cursor (Bug#16647). */
31098 goto set_cursor;
31100 else
31101 #endif
31102 return;
31105 #ifdef HAVE_WINDOW_SYSTEM
31106 if (part == ON_VERTICAL_BORDER)
31108 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31109 help_echo_string = build_string ("drag-mouse-1: resize");
31110 goto set_cursor;
31112 else if (part == ON_RIGHT_DIVIDER)
31114 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31115 help_echo_string = build_string ("drag-mouse-1: resize");
31116 goto set_cursor;
31118 else if (part == ON_BOTTOM_DIVIDER)
31119 if (! WINDOW_BOTTOMMOST_P (w)
31120 || minibuf_level
31121 || NILP (Vresize_mini_windows))
31123 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31124 help_echo_string = build_string ("drag-mouse-1: resize");
31125 goto set_cursor;
31127 else
31128 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31129 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31130 || part == ON_VERTICAL_SCROLL_BAR
31131 || part == ON_HORIZONTAL_SCROLL_BAR)
31132 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31133 else
31134 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31135 #endif
31137 /* Are we in a window whose display is up to date?
31138 And verify the buffer's text has not changed. */
31139 b = XBUFFER (w->contents);
31140 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31142 int hpos, vpos, dx, dy, area = LAST_AREA;
31143 ptrdiff_t pos;
31144 struct glyph *glyph;
31145 Lisp_Object object;
31146 Lisp_Object mouse_face = Qnil, position;
31147 Lisp_Object *overlay_vec = NULL;
31148 ptrdiff_t i, noverlays;
31149 struct buffer *obuf;
31150 ptrdiff_t obegv, ozv;
31151 bool same_region;
31153 /* Find the glyph under X/Y. */
31154 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31156 #ifdef HAVE_WINDOW_SYSTEM
31157 /* Look for :pointer property on image. */
31158 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31160 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31161 if (img != NULL && IMAGEP (img->spec))
31163 Lisp_Object image_map, hotspot;
31164 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31165 !NILP (image_map))
31166 && (hotspot = find_hot_spot (image_map,
31167 glyph->slice.img.x + dx,
31168 glyph->slice.img.y + dy),
31169 CONSP (hotspot))
31170 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31172 Lisp_Object plist;
31174 /* Could check XCAR (hotspot) to see if we enter/leave
31175 this hot-spot.
31176 If so, we could look for mouse-enter, mouse-leave
31177 properties in PLIST (and do something...). */
31178 hotspot = XCDR (hotspot);
31179 if (CONSP (hotspot)
31180 && (plist = XCAR (hotspot), CONSP (plist)))
31182 pointer = Fplist_get (plist, Qpointer);
31183 if (NILP (pointer))
31184 pointer = Qhand;
31185 help_echo_string = Fplist_get (plist, Qhelp_echo);
31186 if (!NILP (help_echo_string))
31188 help_echo_window = window;
31189 help_echo_object = glyph->object;
31190 help_echo_pos = glyph->charpos;
31194 if (NILP (pointer))
31195 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31198 #endif /* HAVE_WINDOW_SYSTEM */
31200 /* Clear mouse face if X/Y not over text. */
31201 if (glyph == NULL
31202 || area != TEXT_AREA
31203 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31204 /* Glyph's OBJECT is nil for glyphs inserted by the
31205 display engine for its internal purposes, like truncation
31206 and continuation glyphs and blanks beyond the end of
31207 line's text on text terminals. If we are over such a
31208 glyph, we are not over any text. */
31209 || NILP (glyph->object)
31210 /* R2L rows have a stretch glyph at their front, which
31211 stands for no text, whereas L2R rows have no glyphs at
31212 all beyond the end of text. Treat such stretch glyphs
31213 like we do with NULL glyphs in L2R rows. */
31214 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31215 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31216 && glyph->type == STRETCH_GLYPH
31217 && glyph->avoid_cursor_p))
31219 if (clear_mouse_face (hlinfo))
31220 cursor = No_Cursor;
31221 if (FRAME_WINDOW_P (f) && NILP (pointer))
31223 #ifdef HAVE_WINDOW_SYSTEM
31224 if (area != TEXT_AREA)
31225 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31226 else
31227 pointer = Vvoid_text_area_pointer;
31228 #endif
31230 goto set_cursor;
31233 pos = glyph->charpos;
31234 object = glyph->object;
31235 if (!STRINGP (object) && !BUFFERP (object))
31236 goto set_cursor;
31238 /* If we get an out-of-range value, return now; avoid an error. */
31239 if (BUFFERP (object) && pos > BUF_Z (b))
31240 goto set_cursor;
31242 /* Make the window's buffer temporarily current for
31243 overlays_at and compute_char_face. */
31244 obuf = current_buffer;
31245 current_buffer = b;
31246 obegv = BEGV;
31247 ozv = ZV;
31248 BEGV = BEG;
31249 ZV = Z;
31251 /* Is this char mouse-active or does it have help-echo? */
31252 position = make_number (pos);
31254 USE_SAFE_ALLOCA;
31256 if (BUFFERP (object))
31258 /* Put all the overlays we want in a vector in overlay_vec. */
31259 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31260 /* Sort overlays into increasing priority order. */
31261 noverlays = sort_overlays (overlay_vec, noverlays, w);
31263 else
31264 noverlays = 0;
31266 if (NILP (Vmouse_highlight))
31268 clear_mouse_face (hlinfo);
31269 goto check_help_echo;
31272 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31274 if (same_region)
31275 cursor = No_Cursor;
31277 /* Check mouse-face highlighting. */
31278 if (! same_region
31279 /* If there exists an overlay with mouse-face overlapping
31280 the one we are currently highlighting, we have to
31281 check if we enter the overlapping overlay, and then
31282 highlight only that. */
31283 || (OVERLAYP (hlinfo->mouse_face_overlay)
31284 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31286 /* Find the highest priority overlay with a mouse-face. */
31287 Lisp_Object overlay = Qnil;
31288 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31290 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31291 if (!NILP (mouse_face))
31292 overlay = overlay_vec[i];
31295 /* If we're highlighting the same overlay as before, there's
31296 no need to do that again. */
31297 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31298 goto check_help_echo;
31300 /* Clear the display of the old active region, if any. */
31301 if (clear_mouse_face (hlinfo))
31302 cursor = No_Cursor;
31304 /* Record the overlay, if any, to be highlighted. */
31305 hlinfo->mouse_face_overlay = overlay;
31307 /* If no overlay applies, get a text property. */
31308 if (NILP (overlay))
31309 mouse_face = Fget_text_property (position, Qmouse_face, object);
31311 /* Next, compute the bounds of the mouse highlighting and
31312 display it. */
31313 if (!NILP (mouse_face) && STRINGP (object))
31315 /* The mouse-highlighting comes from a display string
31316 with a mouse-face. */
31317 Lisp_Object s, e;
31318 ptrdiff_t ignore;
31320 s = Fprevious_single_property_change
31321 (make_number (pos + 1), Qmouse_face, object, Qnil);
31322 e = Fnext_single_property_change
31323 (position, Qmouse_face, object, Qnil);
31324 if (NILP (s))
31325 s = make_number (0);
31326 if (NILP (e))
31327 e = make_number (SCHARS (object));
31328 mouse_face_from_string_pos (w, hlinfo, object,
31329 XINT (s), XINT (e));
31330 hlinfo->mouse_face_past_end = false;
31331 hlinfo->mouse_face_window = window;
31332 hlinfo->mouse_face_face_id
31333 = face_at_string_position (w, object, pos, 0, &ignore,
31334 glyph->face_id, true);
31335 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31336 cursor = No_Cursor;
31338 else
31340 /* The mouse-highlighting, if any, comes from an overlay
31341 or text property in the buffer. */
31342 Lisp_Object buffer UNINIT;
31343 Lisp_Object disp_string UNINIT;
31345 if (STRINGP (object))
31347 /* If we are on a display string with no mouse-face,
31348 check if the text under it has one. */
31349 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31350 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31351 pos = string_buffer_position (object, start);
31352 if (pos > 0)
31354 mouse_face = get_char_property_and_overlay
31355 (make_number (pos), Qmouse_face, w->contents, &overlay);
31356 buffer = w->contents;
31357 disp_string = object;
31360 else
31362 buffer = object;
31363 disp_string = Qnil;
31366 if (!NILP (mouse_face))
31368 Lisp_Object before, after;
31369 Lisp_Object before_string, after_string;
31370 /* To correctly find the limits of mouse highlight
31371 in a bidi-reordered buffer, we must not use the
31372 optimization of limiting the search in
31373 previous-single-property-change and
31374 next-single-property-change, because
31375 rows_from_pos_range needs the real start and end
31376 positions to DTRT in this case. That's because
31377 the first row visible in a window does not
31378 necessarily display the character whose position
31379 is the smallest. */
31380 Lisp_Object lim1
31381 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31382 ? Fmarker_position (w->start)
31383 : Qnil;
31384 Lisp_Object lim2
31385 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31386 ? make_number (BUF_Z (XBUFFER (buffer))
31387 - w->window_end_pos)
31388 : Qnil;
31390 if (NILP (overlay))
31392 /* Handle the text property case. */
31393 before = Fprevious_single_property_change
31394 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31395 after = Fnext_single_property_change
31396 (make_number (pos), Qmouse_face, buffer, lim2);
31397 before_string = after_string = Qnil;
31399 else
31401 /* Handle the overlay case. */
31402 before = Foverlay_start (overlay);
31403 after = Foverlay_end (overlay);
31404 before_string = Foverlay_get (overlay, Qbefore_string);
31405 after_string = Foverlay_get (overlay, Qafter_string);
31407 if (!STRINGP (before_string)) before_string = Qnil;
31408 if (!STRINGP (after_string)) after_string = Qnil;
31411 mouse_face_from_buffer_pos (window, hlinfo, pos,
31412 NILP (before)
31414 : XFASTINT (before),
31415 NILP (after)
31416 ? BUF_Z (XBUFFER (buffer))
31417 : XFASTINT (after),
31418 before_string, after_string,
31419 disp_string);
31420 cursor = No_Cursor;
31425 check_help_echo:
31427 /* Look for a `help-echo' property. */
31428 if (NILP (help_echo_string)) {
31429 Lisp_Object help, overlay;
31431 /* Check overlays first. */
31432 help = overlay = Qnil;
31433 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31435 overlay = overlay_vec[i];
31436 help = Foverlay_get (overlay, Qhelp_echo);
31439 if (!NILP (help))
31441 help_echo_string = help;
31442 help_echo_window = window;
31443 help_echo_object = overlay;
31444 help_echo_pos = pos;
31446 else
31448 Lisp_Object obj = glyph->object;
31449 ptrdiff_t charpos = glyph->charpos;
31451 /* Try text properties. */
31452 if (STRINGP (obj)
31453 && charpos >= 0
31454 && charpos < SCHARS (obj))
31456 help = Fget_text_property (make_number (charpos),
31457 Qhelp_echo, obj);
31458 if (NILP (help))
31460 /* If the string itself doesn't specify a help-echo,
31461 see if the buffer text ``under'' it does. */
31462 struct glyph_row *r
31463 = MATRIX_ROW (w->current_matrix, vpos);
31464 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31465 ptrdiff_t p = string_buffer_position (obj, start);
31466 if (p > 0)
31468 help = Fget_char_property (make_number (p),
31469 Qhelp_echo, w->contents);
31470 if (!NILP (help))
31472 charpos = p;
31473 obj = w->contents;
31478 else if (BUFFERP (obj)
31479 && charpos >= BEGV
31480 && charpos < ZV)
31481 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31482 obj);
31484 if (!NILP (help))
31486 help_echo_string = help;
31487 help_echo_window = window;
31488 help_echo_object = obj;
31489 help_echo_pos = charpos;
31494 #ifdef HAVE_WINDOW_SYSTEM
31495 /* Look for a `pointer' property. */
31496 if (FRAME_WINDOW_P (f) && NILP (pointer))
31498 /* Check overlays first. */
31499 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31500 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31502 if (NILP (pointer))
31504 Lisp_Object obj = glyph->object;
31505 ptrdiff_t charpos = glyph->charpos;
31507 /* Try text properties. */
31508 if (STRINGP (obj)
31509 && charpos >= 0
31510 && charpos < SCHARS (obj))
31512 pointer = Fget_text_property (make_number (charpos),
31513 Qpointer, obj);
31514 if (NILP (pointer))
31516 /* If the string itself doesn't specify a pointer,
31517 see if the buffer text ``under'' it does. */
31518 struct glyph_row *r
31519 = MATRIX_ROW (w->current_matrix, vpos);
31520 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31521 ptrdiff_t p = string_buffer_position (obj, start);
31522 if (p > 0)
31523 pointer = Fget_char_property (make_number (p),
31524 Qpointer, w->contents);
31527 else if (BUFFERP (obj)
31528 && charpos >= BEGV
31529 && charpos < ZV)
31530 pointer = Fget_text_property (make_number (charpos),
31531 Qpointer, obj);
31534 #endif /* HAVE_WINDOW_SYSTEM */
31536 BEGV = obegv;
31537 ZV = ozv;
31538 current_buffer = obuf;
31539 SAFE_FREE ();
31542 set_cursor:
31543 define_frame_cursor1 (f, cursor, pointer);
31547 /* EXPORT for RIF:
31548 Clear any mouse-face on window W. This function is part of the
31549 redisplay interface, and is called from try_window_id and similar
31550 functions to ensure the mouse-highlight is off. */
31552 void
31553 x_clear_window_mouse_face (struct window *w)
31555 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31556 Lisp_Object window;
31558 block_input ();
31559 XSETWINDOW (window, w);
31560 if (EQ (window, hlinfo->mouse_face_window))
31561 clear_mouse_face (hlinfo);
31562 unblock_input ();
31566 /* EXPORT:
31567 Just discard the mouse face information for frame F, if any.
31568 This is used when the size of F is changed. */
31570 void
31571 cancel_mouse_face (struct frame *f)
31573 Lisp_Object window;
31574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31576 window = hlinfo->mouse_face_window;
31577 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31578 reset_mouse_highlight (hlinfo);
31583 /***********************************************************************
31584 Exposure Events
31585 ***********************************************************************/
31587 #ifdef HAVE_WINDOW_SYSTEM
31589 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31590 which intersects rectangle R. R is in window-relative coordinates. */
31592 static void
31593 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31594 enum glyph_row_area area)
31596 struct glyph *first = row->glyphs[area];
31597 struct glyph *end = row->glyphs[area] + row->used[area];
31598 struct glyph *last;
31599 int first_x, start_x, x;
31601 if (area == TEXT_AREA && row->fill_line_p)
31602 /* If row extends face to end of line write the whole line. */
31603 draw_glyphs (w, 0, row, area,
31604 0, row->used[area],
31605 DRAW_NORMAL_TEXT, 0);
31606 else
31608 /* Set START_X to the window-relative start position for drawing glyphs of
31609 AREA. The first glyph of the text area can be partially visible.
31610 The first glyphs of other areas cannot. */
31611 start_x = window_box_left_offset (w, area);
31612 x = start_x;
31613 if (area == TEXT_AREA)
31614 x += row->x;
31616 /* Find the first glyph that must be redrawn. */
31617 while (first < end
31618 && x + first->pixel_width < r->x)
31620 x += first->pixel_width;
31621 ++first;
31624 /* Find the last one. */
31625 last = first;
31626 first_x = x;
31627 /* Use a signed int intermediate value to avoid catastrophic
31628 failures due to comparison between signed and unsigned, when
31629 x is negative (can happen for wide images that are hscrolled). */
31630 int r_end = r->x + r->width;
31631 while (last < end && x < r_end)
31633 x += last->pixel_width;
31634 ++last;
31637 /* Repaint. */
31638 if (last > first)
31639 draw_glyphs (w, first_x - start_x, row, area,
31640 first - row->glyphs[area], last - row->glyphs[area],
31641 DRAW_NORMAL_TEXT, 0);
31646 /* Redraw the parts of the glyph row ROW on window W intersecting
31647 rectangle R. R is in window-relative coordinates. Value is
31648 true if mouse-face was overwritten. */
31650 static bool
31651 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31653 eassert (row->enabled_p);
31655 if (row->mode_line_p || w->pseudo_window_p)
31656 draw_glyphs (w, 0, row, TEXT_AREA,
31657 0, row->used[TEXT_AREA],
31658 DRAW_NORMAL_TEXT, 0);
31659 else
31661 if (row->used[LEFT_MARGIN_AREA])
31662 expose_area (w, row, r, LEFT_MARGIN_AREA);
31663 if (row->used[TEXT_AREA])
31664 expose_area (w, row, r, TEXT_AREA);
31665 if (row->used[RIGHT_MARGIN_AREA])
31666 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31667 draw_row_fringe_bitmaps (w, row);
31670 return row->mouse_face_p;
31674 /* Redraw those parts of glyphs rows during expose event handling that
31675 overlap other rows. Redrawing of an exposed line writes over parts
31676 of lines overlapping that exposed line; this function fixes that.
31678 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31679 row in W's current matrix that is exposed and overlaps other rows.
31680 LAST_OVERLAPPING_ROW is the last such row. */
31682 static void
31683 expose_overlaps (struct window *w,
31684 struct glyph_row *first_overlapping_row,
31685 struct glyph_row *last_overlapping_row,
31686 XRectangle *r)
31688 struct glyph_row *row;
31690 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31691 if (row->overlapping_p)
31693 eassert (row->enabled_p && !row->mode_line_p);
31695 row->clip = r;
31696 if (row->used[LEFT_MARGIN_AREA])
31697 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31699 if (row->used[TEXT_AREA])
31700 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31702 if (row->used[RIGHT_MARGIN_AREA])
31703 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31704 row->clip = NULL;
31709 /* Return true if W's cursor intersects rectangle R. */
31711 static bool
31712 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31714 XRectangle cr, result;
31715 struct glyph *cursor_glyph;
31716 struct glyph_row *row;
31718 if (w->phys_cursor.vpos >= 0
31719 && w->phys_cursor.vpos < w->current_matrix->nrows
31720 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31721 row->enabled_p)
31722 && row->cursor_in_fringe_p)
31724 /* Cursor is in the fringe. */
31725 cr.x = window_box_right_offset (w,
31726 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31727 ? RIGHT_MARGIN_AREA
31728 : TEXT_AREA));
31729 cr.y = row->y;
31730 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31731 cr.height = row->height;
31732 return x_intersect_rectangles (&cr, r, &result);
31735 cursor_glyph = get_phys_cursor_glyph (w);
31736 if (cursor_glyph)
31738 /* r is relative to W's box, but w->phys_cursor.x is relative
31739 to left edge of W's TEXT area. Adjust it. */
31740 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31741 cr.y = w->phys_cursor.y;
31742 cr.width = cursor_glyph->pixel_width;
31743 cr.height = w->phys_cursor_height;
31744 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31745 I assume the effect is the same -- and this is portable. */
31746 return x_intersect_rectangles (&cr, r, &result);
31748 /* If we don't understand the format, pretend we're not in the hot-spot. */
31749 return false;
31753 /* EXPORT:
31754 Draw a vertical window border to the right of window W if W doesn't
31755 have vertical scroll bars. */
31757 void
31758 x_draw_vertical_border (struct window *w)
31760 struct frame *f = XFRAME (WINDOW_FRAME (w));
31762 /* We could do better, if we knew what type of scroll-bar the adjacent
31763 windows (on either side) have... But we don't :-(
31764 However, I think this works ok. ++KFS 2003-04-25 */
31766 /* Redraw borders between horizontally adjacent windows. Don't
31767 do it for frames with vertical scroll bars because either the
31768 right scroll bar of a window, or the left scroll bar of its
31769 neighbor will suffice as a border. */
31770 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31771 return;
31773 /* Note: It is necessary to redraw both the left and the right
31774 borders, for when only this single window W is being
31775 redisplayed. */
31776 if (!WINDOW_RIGHTMOST_P (w)
31777 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31779 int x0, x1, y0, y1;
31781 window_box_edges (w, &x0, &y0, &x1, &y1);
31782 y1 -= 1;
31784 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31785 x1 -= 1;
31787 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31790 if (!WINDOW_LEFTMOST_P (w)
31791 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31793 int x0, x1, y0, y1;
31795 window_box_edges (w, &x0, &y0, &x1, &y1);
31796 y1 -= 1;
31798 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31799 x0 -= 1;
31801 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31806 /* Draw window dividers for window W. */
31808 void
31809 x_draw_right_divider (struct window *w)
31811 struct frame *f = WINDOW_XFRAME (w);
31813 if (w->mini || w->pseudo_window_p)
31814 return;
31815 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31817 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31818 int x1 = WINDOW_RIGHT_EDGE_X (w);
31819 int y0 = WINDOW_TOP_EDGE_Y (w);
31820 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31822 /* If W is horizontally combined and has a right sibling, don't
31823 draw over any bottom divider. */
31824 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31825 && !NILP (w->parent)
31826 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31827 && !NILP (w->next))
31828 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31830 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31834 static void
31835 x_draw_bottom_divider (struct window *w)
31837 struct frame *f = XFRAME (WINDOW_FRAME (w));
31839 if (w->mini || w->pseudo_window_p)
31840 return;
31841 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31843 int x0 = WINDOW_LEFT_EDGE_X (w);
31844 int x1 = WINDOW_RIGHT_EDGE_X (w);
31845 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31846 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31847 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31849 /* If W is vertically combined and has a sibling below, don't draw
31850 over any right divider. */
31851 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31852 && p
31853 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31854 && !NILP (w->next))
31855 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31856 && NILP (w->next)
31857 && !NILP (p->parent)
31858 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31859 && !NILP (XWINDOW (p->parent)->next))))
31860 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31862 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31866 /* Redraw the part of window W intersection rectangle FR. Pixel
31867 coordinates in FR are frame-relative. Call this function with
31868 input blocked. Value is true if the exposure overwrites
31869 mouse-face. */
31871 static bool
31872 expose_window (struct window *w, XRectangle *fr)
31874 struct frame *f = XFRAME (w->frame);
31875 XRectangle wr, r;
31876 bool mouse_face_overwritten_p = false;
31878 /* If window is not yet fully initialized, do nothing. This can
31879 happen when toolkit scroll bars are used and a window is split.
31880 Reconfiguring the scroll bar will generate an expose for a newly
31881 created window. */
31882 if (w->current_matrix == NULL)
31883 return false;
31885 /* When we're currently updating the window, display and current
31886 matrix usually don't agree. Arrange for a thorough display
31887 later. */
31888 if (w->must_be_updated_p)
31890 SET_FRAME_GARBAGED (f);
31891 return false;
31894 /* Frame-relative pixel rectangle of W. */
31895 wr.x = WINDOW_LEFT_EDGE_X (w);
31896 wr.y = WINDOW_TOP_EDGE_Y (w);
31897 wr.width = WINDOW_PIXEL_WIDTH (w);
31898 wr.height = WINDOW_PIXEL_HEIGHT (w);
31900 if (x_intersect_rectangles (fr, &wr, &r))
31902 int yb = window_text_bottom_y (w);
31903 struct glyph_row *row;
31904 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31906 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31907 r.x, r.y, r.width, r.height));
31909 /* Convert to window coordinates. */
31910 r.x -= WINDOW_LEFT_EDGE_X (w);
31911 r.y -= WINDOW_TOP_EDGE_Y (w);
31913 /* Turn off the cursor. */
31914 bool cursor_cleared_p = (!w->pseudo_window_p
31915 && phys_cursor_in_rect_p (w, &r));
31916 if (cursor_cleared_p)
31917 x_clear_cursor (w);
31919 /* If the row containing the cursor extends face to end of line,
31920 then expose_area might overwrite the cursor outside the
31921 rectangle and thus notice_overwritten_cursor might clear
31922 w->phys_cursor_on_p. We remember the original value and
31923 check later if it is changed. */
31924 bool phys_cursor_on_p = w->phys_cursor_on_p;
31926 /* Use a signed int intermediate value to avoid catastrophic
31927 failures due to comparison between signed and unsigned, when
31928 y0 or y1 is negative (can happen for tall images). */
31929 int r_bottom = r.y + r.height;
31931 /* Update lines intersecting rectangle R. */
31932 first_overlapping_row = last_overlapping_row = NULL;
31933 for (row = w->current_matrix->rows;
31934 row->enabled_p;
31935 ++row)
31937 int y0 = row->y;
31938 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31940 if ((y0 >= r.y && y0 < r_bottom)
31941 || (y1 > r.y && y1 < r_bottom)
31942 || (r.y >= y0 && r.y < y1)
31943 || (r_bottom > y0 && r_bottom < y1))
31945 /* A header line may be overlapping, but there is no need
31946 to fix overlapping areas for them. KFS 2005-02-12 */
31947 if (row->overlapping_p && !row->mode_line_p)
31949 if (first_overlapping_row == NULL)
31950 first_overlapping_row = row;
31951 last_overlapping_row = row;
31954 row->clip = fr;
31955 if (expose_line (w, row, &r))
31956 mouse_face_overwritten_p = true;
31957 row->clip = NULL;
31959 else if (row->overlapping_p)
31961 /* We must redraw a row overlapping the exposed area. */
31962 if (y0 < r.y
31963 ? y0 + row->phys_height > r.y
31964 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31966 if (first_overlapping_row == NULL)
31967 first_overlapping_row = row;
31968 last_overlapping_row = row;
31972 if (y1 >= yb)
31973 break;
31976 /* Display the mode line if there is one. */
31977 if (window_wants_mode_line (w)
31978 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31979 row->enabled_p)
31980 && row->y < r_bottom)
31982 if (expose_line (w, row, &r))
31983 mouse_face_overwritten_p = true;
31986 if (!w->pseudo_window_p)
31988 /* Fix the display of overlapping rows. */
31989 if (first_overlapping_row)
31990 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31991 fr);
31993 /* Draw border between windows. */
31994 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31995 x_draw_right_divider (w);
31996 else
31997 x_draw_vertical_border (w);
31999 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32000 x_draw_bottom_divider (w);
32002 /* Turn the cursor on again. */
32003 if (cursor_cleared_p
32004 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32005 update_window_cursor (w, true);
32009 return mouse_face_overwritten_p;
32014 /* Redraw (parts) of all windows in the window tree rooted at W that
32015 intersect R. R contains frame pixel coordinates. Value is
32016 true if the exposure overwrites mouse-face. */
32018 static bool
32019 expose_window_tree (struct window *w, XRectangle *r)
32021 struct frame *f = XFRAME (w->frame);
32022 bool mouse_face_overwritten_p = false;
32024 while (w && !FRAME_GARBAGED_P (f))
32026 mouse_face_overwritten_p
32027 |= (WINDOWP (w->contents)
32028 ? expose_window_tree (XWINDOW (w->contents), r)
32029 : expose_window (w, r));
32031 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32034 return mouse_face_overwritten_p;
32038 /* EXPORT:
32039 Redisplay an exposed area of frame F. X and Y are the upper-left
32040 corner of the exposed rectangle. W and H are width and height of
32041 the exposed area. All are pixel values. W or H zero means redraw
32042 the entire frame. */
32044 void
32045 expose_frame (struct frame *f, int x, int y, int w, int h)
32047 XRectangle r;
32048 bool mouse_face_overwritten_p = false;
32050 TRACE ((stderr, "expose_frame "));
32052 /* No need to redraw if frame will be redrawn soon. */
32053 if (FRAME_GARBAGED_P (f))
32055 TRACE ((stderr, " garbaged\n"));
32056 return;
32059 /* If basic faces haven't been realized yet, there is no point in
32060 trying to redraw anything. This can happen when we get an expose
32061 event while Emacs is starting, e.g. by moving another window. */
32062 if (FRAME_FACE_CACHE (f) == NULL
32063 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32065 TRACE ((stderr, " no faces\n"));
32066 return;
32069 if (w == 0 || h == 0)
32071 r.x = r.y = 0;
32072 r.width = FRAME_TEXT_WIDTH (f);
32073 r.height = FRAME_TEXT_HEIGHT (f);
32075 else
32077 r.x = x;
32078 r.y = y;
32079 r.width = w;
32080 r.height = h;
32083 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32084 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32086 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32087 if (WINDOWP (f->tool_bar_window))
32088 mouse_face_overwritten_p
32089 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32090 #endif
32092 #ifdef HAVE_X_WINDOWS
32093 #ifndef MSDOS
32094 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32095 if (WINDOWP (f->menu_bar_window))
32096 mouse_face_overwritten_p
32097 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32098 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32099 #endif
32100 #endif
32102 /* Some window managers support a focus-follows-mouse style with
32103 delayed raising of frames. Imagine a partially obscured frame,
32104 and moving the mouse into partially obscured mouse-face on that
32105 frame. The visible part of the mouse-face will be highlighted,
32106 then the WM raises the obscured frame. With at least one WM, KDE
32107 2.1, Emacs is not getting any event for the raising of the frame
32108 (even tried with SubstructureRedirectMask), only Expose events.
32109 These expose events will draw text normally, i.e. not
32110 highlighted. Which means we must redo the highlight here.
32111 Subsume it under ``we love X''. --gerd 2001-08-15 */
32112 /* Included in Windows version because Windows most likely does not
32113 do the right thing if any third party tool offers
32114 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32115 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32117 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32118 if (f == hlinfo->mouse_face_mouse_frame)
32120 int mouse_x = hlinfo->mouse_face_mouse_x;
32121 int mouse_y = hlinfo->mouse_face_mouse_y;
32122 clear_mouse_face (hlinfo);
32123 note_mouse_highlight (f, mouse_x, mouse_y);
32129 /* EXPORT:
32130 Determine the intersection of two rectangles R1 and R2. Return
32131 the intersection in *RESULT. Value is true if RESULT is not
32132 empty. */
32134 bool
32135 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32137 XRectangle *left, *right;
32138 XRectangle *upper, *lower;
32139 bool intersection_p = false;
32141 /* Rearrange so that R1 is the left-most rectangle. */
32142 if (r1->x < r2->x)
32143 left = r1, right = r2;
32144 else
32145 left = r2, right = r1;
32147 /* X0 of the intersection is right.x0, if this is inside R1,
32148 otherwise there is no intersection. */
32149 if (right->x <= left->x + left->width)
32151 result->x = right->x;
32153 /* The right end of the intersection is the minimum of
32154 the right ends of left and right. */
32155 result->width = (min (left->x + left->width, right->x + right->width)
32156 - result->x);
32158 /* Same game for Y. */
32159 if (r1->y < r2->y)
32160 upper = r1, lower = r2;
32161 else
32162 upper = r2, lower = r1;
32164 /* The upper end of the intersection is lower.y0, if this is inside
32165 of upper. Otherwise, there is no intersection. */
32166 if (lower->y <= upper->y + upper->height)
32168 result->y = lower->y;
32170 /* The lower end of the intersection is the minimum of the lower
32171 ends of upper and lower. */
32172 result->height = (min (lower->y + lower->height,
32173 upper->y + upper->height)
32174 - result->y);
32175 intersection_p = true;
32179 return intersection_p;
32182 #endif /* HAVE_WINDOW_SYSTEM */
32185 /***********************************************************************
32186 Initialization
32187 ***********************************************************************/
32189 void
32190 syms_of_xdisp (void)
32192 Vwith_echo_area_save_vector = Qnil;
32193 staticpro (&Vwith_echo_area_save_vector);
32195 Vmessage_stack = Qnil;
32196 staticpro (&Vmessage_stack);
32198 /* Non-nil means don't actually do any redisplay. */
32199 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32201 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32203 DEFVAR_BOOL("inhibit-message", inhibit_message,
32204 doc: /* Non-nil means calls to `message' are not displayed.
32205 They are still logged to the *Messages* buffer. */);
32206 inhibit_message = 0;
32208 message_dolog_marker1 = Fmake_marker ();
32209 staticpro (&message_dolog_marker1);
32210 message_dolog_marker2 = Fmake_marker ();
32211 staticpro (&message_dolog_marker2);
32212 message_dolog_marker3 = Fmake_marker ();
32213 staticpro (&message_dolog_marker3);
32215 defsubr (&Sset_buffer_redisplay);
32216 #ifdef GLYPH_DEBUG
32217 defsubr (&Sdump_frame_glyph_matrix);
32218 defsubr (&Sdump_glyph_matrix);
32219 defsubr (&Sdump_glyph_row);
32220 defsubr (&Sdump_tool_bar_row);
32221 defsubr (&Strace_redisplay);
32222 defsubr (&Strace_to_stderr);
32223 #endif
32224 #ifdef HAVE_WINDOW_SYSTEM
32225 defsubr (&Stool_bar_height);
32226 defsubr (&Slookup_image_map);
32227 #endif
32228 defsubr (&Sline_pixel_height);
32229 defsubr (&Sformat_mode_line);
32230 defsubr (&Sinvisible_p);
32231 defsubr (&Scurrent_bidi_paragraph_direction);
32232 defsubr (&Swindow_text_pixel_size);
32233 defsubr (&Smove_point_visually);
32234 defsubr (&Sbidi_find_overridden_directionality);
32236 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32237 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32238 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32239 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32240 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32241 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32242 DEFSYM (Qeval, "eval");
32243 DEFSYM (QCdata, ":data");
32245 /* Names of text properties relevant for redisplay. */
32246 DEFSYM (Qdisplay, "display");
32247 DEFSYM (Qspace_width, "space-width");
32248 DEFSYM (Qraise, "raise");
32249 DEFSYM (Qslice, "slice");
32250 DEFSYM (Qspace, "space");
32251 DEFSYM (Qmargin, "margin");
32252 DEFSYM (Qpointer, "pointer");
32253 DEFSYM (Qleft_margin, "left-margin");
32254 DEFSYM (Qright_margin, "right-margin");
32255 DEFSYM (Qcenter, "center");
32256 DEFSYM (Qline_height, "line-height");
32257 DEFSYM (QCalign_to, ":align-to");
32258 DEFSYM (QCrelative_width, ":relative-width");
32259 DEFSYM (QCrelative_height, ":relative-height");
32260 DEFSYM (QCeval, ":eval");
32261 DEFSYM (QCpropertize, ":propertize");
32262 DEFSYM (QCfile, ":file");
32263 DEFSYM (Qfontified, "fontified");
32264 DEFSYM (Qfontification_functions, "fontification-functions");
32266 /* Name of the symbol which disables Lisp evaluation in 'display'
32267 properties. This is used by enriched.el. */
32268 DEFSYM (Qdisable_eval, "disable-eval");
32270 /* Name of the face used to highlight trailing whitespace. */
32271 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32273 /* Names of the faces used to display line numbers. */
32274 DEFSYM (Qline_number, "line-number");
32275 DEFSYM (Qline_number_current_line, "line-number-current-line");
32276 /* Name of a text property which disables line-number display. */
32277 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32279 /* Name and number of the face used to highlight escape glyphs. */
32280 DEFSYM (Qescape_glyph, "escape-glyph");
32282 /* Name and number of the face used to highlight non-breaking
32283 spaces/hyphens. */
32284 DEFSYM (Qnobreak_space, "nobreak-space");
32285 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32287 /* The symbol 'image' which is the car of the lists used to represent
32288 images in Lisp. Also a tool bar style. */
32289 DEFSYM (Qimage, "image");
32291 /* Tool bar styles. */
32292 DEFSYM (Qtext, "text");
32293 DEFSYM (Qboth, "both");
32294 DEFSYM (Qboth_horiz, "both-horiz");
32295 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32297 /* The image map types. */
32298 DEFSYM (QCmap, ":map");
32299 DEFSYM (QCpointer, ":pointer");
32300 DEFSYM (Qrect, "rect");
32301 DEFSYM (Qcircle, "circle");
32302 DEFSYM (Qpoly, "poly");
32304 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32306 DEFSYM (Qgrow_only, "grow-only");
32307 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32308 DEFSYM (Qposition, "position");
32309 DEFSYM (Qbuffer_position, "buffer-position");
32310 DEFSYM (Qobject, "object");
32312 /* Cursor shapes. */
32313 DEFSYM (Qbar, "bar");
32314 DEFSYM (Qhbar, "hbar");
32315 DEFSYM (Qbox, "box");
32316 DEFSYM (Qhollow, "hollow");
32318 /* Pointer shapes. */
32319 DEFSYM (Qhand, "hand");
32320 DEFSYM (Qarrow, "arrow");
32321 /* also Qtext */
32323 DEFSYM (Qdragging, "dragging");
32325 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32327 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32328 staticpro (&list_of_error);
32330 /* Values of those variables at last redisplay are stored as
32331 properties on 'overlay-arrow-position' symbol. However, if
32332 Voverlay_arrow_position is a marker, last-arrow-position is its
32333 numerical position. */
32334 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32335 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32337 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32338 properties on a symbol in overlay-arrow-variable-list. */
32339 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32340 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32342 echo_buffer[0] = echo_buffer[1] = Qnil;
32343 staticpro (&echo_buffer[0]);
32344 staticpro (&echo_buffer[1]);
32346 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32347 staticpro (&echo_area_buffer[0]);
32348 staticpro (&echo_area_buffer[1]);
32350 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32351 staticpro (&Vmessages_buffer_name);
32353 mode_line_proptrans_alist = Qnil;
32354 staticpro (&mode_line_proptrans_alist);
32355 mode_line_string_list = Qnil;
32356 staticpro (&mode_line_string_list);
32357 mode_line_string_face = Qnil;
32358 staticpro (&mode_line_string_face);
32359 mode_line_string_face_prop = Qnil;
32360 staticpro (&mode_line_string_face_prop);
32361 Vmode_line_unwind_vector = Qnil;
32362 staticpro (&Vmode_line_unwind_vector);
32364 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32366 help_echo_string = Qnil;
32367 staticpro (&help_echo_string);
32368 help_echo_object = Qnil;
32369 staticpro (&help_echo_object);
32370 help_echo_window = Qnil;
32371 staticpro (&help_echo_window);
32372 previous_help_echo_string = Qnil;
32373 staticpro (&previous_help_echo_string);
32374 help_echo_pos = -1;
32376 DEFSYM (Qright_to_left, "right-to-left");
32377 DEFSYM (Qleft_to_right, "left-to-right");
32378 defsubr (&Sbidi_resolved_levels);
32380 #ifdef HAVE_WINDOW_SYSTEM
32381 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32382 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32383 For example, if a block cursor is over a tab, it will be drawn as
32384 wide as that tab on the display. */);
32385 x_stretch_cursor_p = 0;
32386 #endif
32388 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32389 doc: /* Non-nil means highlight trailing whitespace.
32390 The face used for trailing whitespace is `trailing-whitespace'. */);
32391 Vshow_trailing_whitespace = Qnil;
32393 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32394 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32395 If the value is t, Emacs highlights non-ASCII chars which have the
32396 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32397 or `nobreak-hyphen' face respectively.
32399 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32400 U+2011 (non-breaking hyphen) are affected.
32402 Any other non-nil value means to display these characters as a escape
32403 glyph followed by an ordinary space or hyphen.
32405 A value of nil means no special handling of these characters. */);
32406 Vnobreak_char_display = Qt;
32408 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32409 doc: /* The pointer shape to show in void text areas.
32410 A value of nil means to show the text pointer. Other options are
32411 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32412 `hourglass'. */);
32413 Vvoid_text_area_pointer = Qarrow;
32415 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32416 doc: /* Non-nil means don't actually do any redisplay.
32417 This is used for internal purposes. */);
32418 Vinhibit_redisplay = Qnil;
32420 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32421 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32422 Vglobal_mode_string = Qnil;
32424 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32425 doc: /* Marker for where to display an arrow on top of the buffer text.
32426 This must be the beginning of a line in order to work.
32427 See also `overlay-arrow-string'. */);
32428 Voverlay_arrow_position = Qnil;
32430 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32431 doc: /* String to display as an arrow in non-window frames.
32432 See also `overlay-arrow-position'. */);
32433 Voverlay_arrow_string = build_pure_c_string ("=>");
32435 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32436 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32437 The symbols on this list are examined during redisplay to determine
32438 where to display overlay arrows. */);
32439 Voverlay_arrow_variable_list
32440 = list1 (intern_c_string ("overlay-arrow-position"));
32442 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32443 doc: /* The number of lines to try scrolling a window by when point moves out.
32444 If that fails to bring point back on frame, point is centered instead.
32445 If this is zero, point is always centered after it moves off frame.
32446 If you want scrolling to always be a line at a time, you should set
32447 `scroll-conservatively' to a large value rather than set this to 1. */);
32449 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32450 doc: /* Scroll up to this many lines, to bring point back on screen.
32451 If point moves off-screen, redisplay will scroll by up to
32452 `scroll-conservatively' lines in order to bring point just barely
32453 onto the screen again. If that cannot be done, then redisplay
32454 recenters point as usual.
32456 If the value is greater than 100, redisplay will never recenter point,
32457 but will always scroll just enough text to bring point into view, even
32458 if you move far away.
32460 A value of zero means always recenter point if it moves off screen. */);
32461 scroll_conservatively = 0;
32463 DEFVAR_INT ("scroll-margin", scroll_margin,
32464 doc: /* Number of lines of margin at the top and bottom of a window.
32465 Recenter the window whenever point gets within this many lines
32466 of the top or bottom of the window. */);
32467 scroll_margin = 0;
32469 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32470 doc: /* Maximum effective value of `scroll-margin'.
32471 Given as a fraction of the current window's lines. The value should
32472 be a floating point number between 0.0 and 0.5. The effective maximum
32473 is limited to (/ (1- window-lines) 2). Non-float values for this
32474 variable are ignored and the default 0.25 is used instead. */);
32475 Vmaximum_scroll_margin = make_float (0.25);
32477 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32478 doc: /* Pixels per inch value for non-window system displays.
32479 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32480 Vdisplay_pixels_per_inch = make_float (72.0);
32482 #ifdef GLYPH_DEBUG
32483 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32484 #endif
32486 DEFVAR_LISP ("truncate-partial-width-windows",
32487 Vtruncate_partial_width_windows,
32488 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32489 For an integer value, truncate lines in each window narrower than the
32490 full frame width, provided the total window width in column units is less
32491 than that integer; otherwise, respect the value of `truncate-lines'.
32492 The total width of the window is as returned by `window-total-width', it
32493 includes the fringes, the continuation and truncation glyphs, the
32494 display margins (if any), and the scroll bar
32496 For any other non-nil value, truncate lines in all windows that do
32497 not span the full frame width.
32499 A value of nil means to respect the value of `truncate-lines'.
32501 If `word-wrap' is enabled, you might want to reduce this. */);
32502 Vtruncate_partial_width_windows = make_number (50);
32504 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32505 doc: /* Maximum buffer size for which line number should be displayed.
32506 If the buffer is bigger than this, the line number does not appear
32507 in the mode line. A value of nil means no limit. */);
32508 Vline_number_display_limit = Qnil;
32510 DEFVAR_INT ("line-number-display-limit-width",
32511 line_number_display_limit_width,
32512 doc: /* Maximum line width (in characters) for line number display.
32513 If the average length of the lines near point is bigger than this, then the
32514 line number may be omitted from the mode line. */);
32515 line_number_display_limit_width = 200;
32517 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32518 doc: /* Non-nil means highlight region even in nonselected windows. */);
32519 highlight_nonselected_windows = false;
32521 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32522 doc: /* Non-nil if more than one frame is visible on this display.
32523 Minibuffer-only frames don't count, but iconified frames do.
32524 This variable is not guaranteed to be accurate except while processing
32525 `frame-title-format' and `icon-title-format'. */);
32527 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32528 doc: /* Template for displaying the title bar of visible frames.
32529 \(Assuming the window manager supports this feature.)
32531 This variable has the same structure as `mode-line-format', except that
32532 the %c, %C, and %l constructs are ignored. It is used only on frames for
32533 which no explicit name has been set (see `modify-frame-parameters'). */);
32535 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32536 doc: /* Template for displaying the title bar of an iconified frame.
32537 \(Assuming the window manager supports this feature.)
32538 This variable has the same structure as `mode-line-format' (which see),
32539 and is used only on frames for which no explicit name has been set
32540 \(see `modify-frame-parameters'). */);
32541 Vicon_title_format
32542 = Vframe_title_format
32543 = listn (CONSTYPE_PURE, 3,
32544 intern_c_string ("multiple-frames"),
32545 build_pure_c_string ("%b"),
32546 listn (CONSTYPE_PURE, 4,
32547 empty_unibyte_string,
32548 intern_c_string ("invocation-name"),
32549 build_pure_c_string ("@"),
32550 intern_c_string ("system-name")));
32552 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32553 doc: /* Maximum number of lines to keep in the message log buffer.
32554 If nil, disable message logging. If t, log messages but don't truncate
32555 the buffer when it becomes large. */);
32556 Vmessage_log_max = make_number (1000);
32558 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32559 doc: /* List of functions to call before redisplaying a window with scrolling.
32560 Each function is called with two arguments, the window and its new
32561 display-start position.
32562 These functions are called whenever the `window-start' marker is modified,
32563 either to point into another buffer (e.g. via `set-window-buffer') or another
32564 place in the same buffer.
32565 Note that the value of `window-end' is not valid when these functions are
32566 called.
32568 Warning: Do not use this feature to alter the way the window
32569 is scrolled. It is not designed for that, and such use probably won't
32570 work. */);
32571 Vwindow_scroll_functions = Qnil;
32573 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32574 doc: /* Functions called when redisplay of a window reaches the end trigger.
32575 Each function is called with two arguments, the window and the end trigger value.
32576 See `set-window-redisplay-end-trigger'. */);
32577 Vredisplay_end_trigger_functions = Qnil;
32579 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32580 doc: /* Non-nil means autoselect window with mouse pointer.
32581 If nil, do not autoselect windows.
32582 A positive number means delay autoselection by that many seconds: a
32583 window is autoselected only after the mouse has remained in that
32584 window for the duration of the delay.
32585 A negative number has a similar effect, but causes windows to be
32586 autoselected only after the mouse has stopped moving. (Because of
32587 the way Emacs compares mouse events, you will occasionally wait twice
32588 that time before the window gets selected.)
32589 Any other value means to autoselect window instantaneously when the
32590 mouse pointer enters it.
32592 Autoselection selects the minibuffer only if it is active, and never
32593 unselects the minibuffer if it is active.
32595 When customizing this variable make sure that the actual value of
32596 `focus-follows-mouse' matches the behavior of your window manager. */);
32597 Vmouse_autoselect_window = Qnil;
32599 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32600 doc: /* Non-nil means automatically resize tool-bars.
32601 This dynamically changes the tool-bar's height to the minimum height
32602 that is needed to make all tool-bar items visible.
32603 If value is `grow-only', the tool-bar's height is only increased
32604 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32605 Vauto_resize_tool_bars = Qt;
32607 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32608 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32609 auto_raise_tool_bar_buttons_p = true;
32611 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32612 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32613 make_cursor_line_fully_visible_p = true;
32615 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32616 doc: /* Border below tool-bar in pixels.
32617 If an integer, use it as the height of the border.
32618 If it is one of `internal-border-width' or `border-width', use the
32619 value of the corresponding frame parameter.
32620 Otherwise, no border is added below the tool-bar. */);
32621 Vtool_bar_border = Qinternal_border_width;
32623 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32624 doc: /* Margin around tool-bar buttons in pixels.
32625 If an integer, use that for both horizontal and vertical margins.
32626 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32627 HORZ specifying the horizontal margin, and VERT specifying the
32628 vertical margin. */);
32629 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32631 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32632 doc: /* Relief thickness of tool-bar buttons. */);
32633 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32635 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32636 doc: /* Tool bar style to use.
32637 It can be one of
32638 image - show images only
32639 text - show text only
32640 both - show both, text below image
32641 both-horiz - show text to the right of the image
32642 text-image-horiz - show text to the left of the image
32643 any other - use system default or image if no system default.
32645 This variable only affects the GTK+ toolkit version of Emacs. */);
32646 Vtool_bar_style = Qnil;
32648 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32649 doc: /* Maximum number of characters a label can have to be shown.
32650 The tool bar style must also show labels for this to have any effect, see
32651 `tool-bar-style'. */);
32652 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32654 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32655 doc: /* List of functions to call to fontify regions of text.
32656 Each function is called with one argument POS. Functions must
32657 fontify a region starting at POS in the current buffer, and give
32658 fontified regions the property `fontified'. */);
32659 Vfontification_functions = Qnil;
32660 Fmake_variable_buffer_local (Qfontification_functions);
32662 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32663 unibyte_display_via_language_environment,
32664 doc: /* Non-nil means display unibyte text according to language environment.
32665 Specifically, this means that raw bytes in the range 160-255 decimal
32666 are displayed by converting them to the equivalent multibyte characters
32667 according to the current language environment. As a result, they are
32668 displayed according to the current fontset.
32670 Note that this variable affects only how these bytes are displayed,
32671 but does not change the fact they are interpreted as raw bytes. */);
32672 unibyte_display_via_language_environment = false;
32674 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32675 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32676 If a float, it specifies a fraction of the mini-window frame's height.
32677 If an integer, it specifies a number of lines. */);
32678 Vmax_mini_window_height = make_float (0.25);
32680 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32681 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32682 A value of nil means don't automatically resize mini-windows.
32683 A value of t means resize them to fit the text displayed in them.
32684 A value of `grow-only', the default, means let mini-windows grow only;
32685 they return to their normal size when the minibuffer is closed, or the
32686 echo area becomes empty. */);
32687 /* Contrary to the doc string, we initialize this to nil, so that
32688 loading loadup.el won't try to resize windows before loading
32689 window.el, where some functions we need to call for this live.
32690 We assign the 'grow-only' value right after loading window.el
32691 during loadup. */
32692 Vresize_mini_windows = Qnil;
32694 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32695 doc: /* Alist specifying how to blink the cursor off.
32696 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32697 `cursor-type' frame-parameter or variable equals ON-STATE,
32698 comparing using `equal', Emacs uses OFF-STATE to specify
32699 how to blink it off. ON-STATE and OFF-STATE are values for
32700 the `cursor-type' frame parameter.
32702 If a frame's ON-STATE has no entry in this list,
32703 the frame's other specifications determine how to blink the cursor off. */);
32704 Vblink_cursor_alist = Qnil;
32706 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32707 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32708 The value `current-line' means the line displaying point in each window
32709 is automatically scrolled horizontally to make point visible.
32710 Any other non-nil value means all the lines in a window are automatically
32711 scrolled horizontally to make point visible. */);
32712 automatic_hscrolling = Qt;
32713 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32714 DEFSYM (Qcurrent_line, "current-line");
32716 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32717 doc: /* How many columns away from the window edge point is allowed to get
32718 before automatic hscrolling will horizontally scroll the window. */);
32719 hscroll_margin = 5;
32721 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32722 doc: /* How many columns to scroll the window when point gets too close to the edge.
32723 When point is less than `hscroll-margin' columns from the window
32724 edge, automatic hscrolling will scroll the window by the amount of columns
32725 determined by this variable. If its value is a positive integer, scroll that
32726 many columns. If it's a positive floating-point number, it specifies the
32727 fraction of the window's width to scroll. If it's nil or zero, point will be
32728 centered horizontally after the scroll. Any other value, including negative
32729 numbers, are treated as if the value were zero.
32731 Automatic hscrolling always moves point outside the scroll margin, so if
32732 point was more than scroll step columns inside the margin, the window will
32733 scroll more than the value given by the scroll step.
32735 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32736 and `scroll-right' overrides this variable's effect. */);
32737 Vhscroll_step = make_number (0);
32739 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32740 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32741 Bind this around calls to `message' to let it take effect. */);
32742 message_truncate_lines = false;
32744 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32745 doc: /* Normal hook run to update the menu bar definitions.
32746 Redisplay runs this hook before it redisplays the menu bar.
32747 This is used to update menus such as Buffers, whose contents depend on
32748 various data. */);
32749 Vmenu_bar_update_hook = Qnil;
32751 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32752 doc: /* Frame for which we are updating a menu.
32753 The enable predicate for a menu binding should check this variable. */);
32754 Vmenu_updating_frame = Qnil;
32756 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32757 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32758 inhibit_menubar_update = false;
32760 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32761 doc: /* Prefix prepended to all continuation lines at display time.
32762 The value may be a string, an image, or a stretch-glyph; it is
32763 interpreted in the same way as the value of a `display' text property.
32765 This variable is overridden by any `wrap-prefix' text or overlay
32766 property.
32768 To add a prefix to non-continuation lines, use `line-prefix'. */);
32769 Vwrap_prefix = Qnil;
32770 DEFSYM (Qwrap_prefix, "wrap-prefix");
32771 Fmake_variable_buffer_local (Qwrap_prefix);
32773 DEFVAR_LISP ("line-prefix", Vline_prefix,
32774 doc: /* Prefix prepended to all non-continuation lines at display time.
32775 The value may be a string, an image, or a stretch-glyph; it is
32776 interpreted in the same way as the value of a `display' text property.
32778 This variable is overridden by any `line-prefix' text or overlay
32779 property.
32781 To add a prefix to continuation lines, use `wrap-prefix'. */);
32782 Vline_prefix = Qnil;
32783 DEFSYM (Qline_prefix, "line-prefix");
32784 Fmake_variable_buffer_local (Qline_prefix);
32786 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32787 doc: /* Non-nil means display line numbers.
32788 If the value is t, display the absolute number of each line of a buffer
32789 shown in a window. Absolute line numbers count from the beginning of
32790 the current narrowing, or from buffer beginning. If the value is
32791 `relative', display for each line not containing the window's point its
32792 relative number instead, i.e. the number of the line relative to the
32793 line showing the window's point.
32795 In either case, line numbers are displayed at the beginning of each
32796 non-continuation line that displays buffer text, i.e. after each newline
32797 character that comes from the buffer. The value `visual' is like
32798 `relative' but counts screen lines instead of buffer lines. In practice
32799 this means that continuation lines count as well when calculating the
32800 relative number of a line.
32802 Lisp programs can disable display of a line number of a particular
32803 buffer line by putting the `display-line-numbers-disable' text property
32804 or overlay property on the first visible character of that line. */);
32805 Vdisplay_line_numbers = Qnil;
32806 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32807 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32808 DEFSYM (Qrelative, "relative");
32809 DEFSYM (Qvisual, "visual");
32811 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32812 doc: /* Minimum width of space reserved for line number display.
32813 A positive number means reserve that many columns for line numbers,
32814 even if the actual number needs less space.
32815 The default value of nil means compute the space dynamically.
32816 Any other value is treated as nil. */);
32817 Vdisplay_line_numbers_width = Qnil;
32818 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32819 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32821 DEFVAR_LISP ("display-line-numbers-current-absolute",
32822 Vdisplay_line_numbers_current_absolute,
32823 doc: /* Non-nil means display absolute number of current line.
32824 This variable has effect only when `display-line-numbers' is
32825 either `relative' or `visual'. */);
32826 Vdisplay_line_numbers_current_absolute = Qt;
32828 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32829 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32830 display_line_numbers_widen = false;
32831 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32832 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32834 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32835 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32836 inhibit_eval_during_redisplay = false;
32838 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32839 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32840 inhibit_free_realized_faces = false;
32842 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32843 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32844 Intended for use during debugging and for testing bidi display;
32845 see biditest.el in the test suite. */);
32846 inhibit_bidi_mirroring = false;
32848 #ifdef GLYPH_DEBUG
32849 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32850 doc: /* Inhibit try_window_id display optimization. */);
32851 inhibit_try_window_id = false;
32853 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32854 doc: /* Inhibit try_window_reusing display optimization. */);
32855 inhibit_try_window_reusing = false;
32857 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32858 doc: /* Inhibit try_cursor_movement display optimization. */);
32859 inhibit_try_cursor_movement = false;
32860 #endif /* GLYPH_DEBUG */
32862 DEFVAR_INT ("overline-margin", overline_margin,
32863 doc: /* Space between overline and text, in pixels.
32864 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32865 margin to the character height. */);
32866 overline_margin = 2;
32868 DEFVAR_INT ("underline-minimum-offset",
32869 underline_minimum_offset,
32870 doc: /* Minimum distance between baseline and underline.
32871 This can improve legibility of underlined text at small font sizes,
32872 particularly when using variable `x-use-underline-position-properties'
32873 with fonts that specify an UNDERLINE_POSITION relatively close to the
32874 baseline. The default value is 1. */);
32875 underline_minimum_offset = 1;
32877 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32878 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32879 This feature only works when on a window system that can change
32880 cursor shapes. */);
32881 display_hourglass_p = true;
32883 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32884 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32885 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32887 #ifdef HAVE_WINDOW_SYSTEM
32888 hourglass_atimer = NULL;
32889 hourglass_shown_p = false;
32890 #endif /* HAVE_WINDOW_SYSTEM */
32892 /* Name of the face used to display glyphless characters. */
32893 DEFSYM (Qglyphless_char, "glyphless-char");
32895 /* Method symbols for Vglyphless_char_display. */
32896 DEFSYM (Qhex_code, "hex-code");
32897 DEFSYM (Qempty_box, "empty-box");
32898 DEFSYM (Qthin_space, "thin-space");
32899 DEFSYM (Qzero_width, "zero-width");
32901 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32902 doc: /* Function run just before redisplay.
32903 It is called with one argument, which is the set of windows that are to
32904 be redisplayed. This set can be nil (meaning, only the selected window),
32905 or t (meaning all windows). */);
32906 Vpre_redisplay_function = intern ("ignore");
32908 /* Symbol for the purpose of Vglyphless_char_display. */
32909 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32910 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32912 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32913 doc: /* Char-table defining glyphless characters.
32914 Each element, if non-nil, should be one of the following:
32915 an ASCII acronym string: display this string in a box
32916 `hex-code': display the hexadecimal code of a character in a box
32917 `empty-box': display as an empty box
32918 `thin-space': display as 1-pixel width space
32919 `zero-width': don't display
32920 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32921 display method for graphical terminals and text terminals respectively.
32922 GRAPHICAL and TEXT should each have one of the values listed above.
32924 The char-table has one extra slot to control the display of a character for
32925 which no font is found. This slot only takes effect on graphical terminals.
32926 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32927 `thin-space'. The default is `empty-box'.
32929 If a character has a non-nil entry in an active display table, the
32930 display table takes effect; in this case, Emacs does not consult
32931 `glyphless-char-display' at all. */);
32932 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32933 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32934 Qempty_box);
32936 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32937 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32938 Vdebug_on_message = Qnil;
32940 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32941 doc: /* */);
32942 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32944 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32945 doc: /* */);
32946 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32948 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32949 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32950 /* Initialize to t, since we need to disable reordering until
32951 loadup.el successfully loads charprop.el. */
32952 redisplay__inhibit_bidi = true;
32954 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32955 doc: /* Non-nil means display raw bytes in hexadecimal format.
32956 The default is to use octal format (\200) whereas hexadecimal (\x80)
32957 may be more familiar to users. */);
32958 display_raw_bytes_as_hex = false;
32963 /* Initialize this module when Emacs starts. */
32965 void
32966 init_xdisp (void)
32968 CHARPOS (this_line_start_pos) = 0;
32970 if (!noninteractive)
32972 struct window *m = XWINDOW (minibuf_window);
32973 Lisp_Object frame = m->frame;
32974 struct frame *f = XFRAME (frame);
32975 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32976 struct window *r = XWINDOW (root);
32977 int i;
32979 echo_area_window = minibuf_window;
32981 r->top_line = FRAME_TOP_MARGIN (f);
32982 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32983 r->total_cols = FRAME_COLS (f);
32984 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32985 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32986 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32988 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32989 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32990 m->total_cols = FRAME_COLS (f);
32991 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32992 m->total_lines = 1;
32993 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32995 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32996 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32997 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32999 /* The default ellipsis glyphs `...'. */
33000 for (i = 0; i < 3; ++i)
33001 default_invis_vector[i] = make_number ('.');
33005 /* Allocate the buffer for frame titles.
33006 Also used for `format-mode-line'. */
33007 int size = 100;
33008 mode_line_noprop_buf = xmalloc (size);
33009 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33010 mode_line_noprop_ptr = mode_line_noprop_buf;
33011 mode_line_target = MODE_LINE_DISPLAY;
33014 help_echo_showing_p = false;
33017 #ifdef HAVE_WINDOW_SYSTEM
33019 /* Platform-independent portion of hourglass implementation. */
33021 /* Timer function of hourglass_atimer. */
33023 static void
33024 show_hourglass (struct atimer *timer)
33026 /* The timer implementation will cancel this timer automatically
33027 after this function has run. Set hourglass_atimer to null
33028 so that we know the timer doesn't have to be canceled. */
33029 hourglass_atimer = NULL;
33031 if (!hourglass_shown_p)
33033 Lisp_Object tail, frame;
33035 block_input ();
33037 FOR_EACH_FRAME (tail, frame)
33039 struct frame *f = XFRAME (frame);
33041 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33042 && FRAME_RIF (f)->show_hourglass)
33043 FRAME_RIF (f)->show_hourglass (f);
33046 hourglass_shown_p = true;
33047 unblock_input ();
33051 /* Cancel a currently active hourglass timer, and start a new one. */
33053 void
33054 start_hourglass (void)
33056 struct timespec delay;
33058 cancel_hourglass ();
33060 if (INTEGERP (Vhourglass_delay)
33061 && XINT (Vhourglass_delay) > 0)
33062 delay = make_timespec (min (XINT (Vhourglass_delay),
33063 TYPE_MAXIMUM (time_t)),
33065 else if (FLOATP (Vhourglass_delay)
33066 && XFLOAT_DATA (Vhourglass_delay) > 0)
33067 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33068 else
33069 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33071 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33072 show_hourglass, NULL);
33075 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33076 shown. */
33078 void
33079 cancel_hourglass (void)
33081 if (hourglass_atimer)
33083 cancel_atimer (hourglass_atimer);
33084 hourglass_atimer = NULL;
33087 if (hourglass_shown_p)
33089 Lisp_Object tail, frame;
33091 block_input ();
33093 FOR_EACH_FRAME (tail, frame)
33095 struct frame *f = XFRAME (frame);
33097 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33098 && FRAME_RIF (f)->hide_hourglass)
33099 FRAME_RIF (f)->hide_hourglass (f);
33100 #ifdef HAVE_NTGUI
33101 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33102 else if (!FRAME_W32_P (f))
33103 w32_arrow_cursor ();
33104 #endif
33107 hourglass_shown_p = false;
33108 unblock_input ();
33112 #endif /* HAVE_WINDOW_SYSTEM */