Fix unlikely overflows with wd length
[emacs.git] / src / xdisp.c
blobb14b7daf2b0c4f0b78ead5c83ba69df05153e8aa
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. */
20893 && !(display_line_numbers_widen
20894 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE)))
20896 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20897 last_line = it->w->base_line_number - 1;
20899 else
20900 start_from = beg_byte;
20901 if (!it->lnum_bytepos)
20902 first_time = true;
20904 else
20905 start_from = it->lnum_bytepos;
20907 /* Paranoia: what if someone changes the narrowing since the
20908 last time display_line was called? Shouldn't really happen,
20909 but who knows what some crazy Lisp invoked by :eval could do? */
20910 if (!(beg_byte <= start_from && start_from <= z_byte))
20912 last_line = 0;
20913 start_from = beg_byte;
20916 this_line =
20917 last_line + display_count_lines_logically (start_from,
20918 IT_BYTEPOS (*it),
20919 IT_CHARPOS (*it), &bytepos);
20920 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20921 eassert (bytepos == IT_BYTEPOS (*it));
20924 /* Record the line number information. */
20925 if (this_line != last_line || !it->lnum_bytepos)
20927 it->lnum = this_line;
20928 it->lnum_bytepos = IT_BYTEPOS (*it);
20931 /* Produce the glyphs for the line number. */
20932 struct it tem_it;
20933 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20934 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20935 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20936 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20937 int current_lnum_face_id
20938 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20939 /* Compute point's line number if needed. */
20940 if ((EQ (Vdisplay_line_numbers, Qrelative)
20941 || EQ (Vdisplay_line_numbers, Qvisual)
20942 || lnum_face_id != current_lnum_face_id)
20943 && !it->pt_lnum)
20945 ptrdiff_t ignored;
20946 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20947 it->pt_lnum =
20948 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20949 PT, &ignored);
20950 else
20951 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20952 &ignored);
20954 /* Compute the required width if needed. */
20955 if (!it->lnum_width)
20957 if (NATNUMP (Vdisplay_line_numbers_width))
20958 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
20960 /* Max line number to be displayed cannot be more than the one
20961 corresponding to the last row of the desired matrix. */
20962 ptrdiff_t max_lnum;
20964 if (NILP (Vdisplay_line_numbers_current_absolute)
20965 && (EQ (Vdisplay_line_numbers, Qrelative)
20966 || EQ (Vdisplay_line_numbers, Qvisual)))
20967 /* We subtract one more because the current line is always
20968 zero in this mode. */
20969 max_lnum = it->w->desired_matrix->nrows - 2;
20970 else if (EQ (Vdisplay_line_numbers, Qvisual))
20971 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
20972 else
20973 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20974 max_lnum = max (1, max_lnum);
20975 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
20976 eassert (it->lnum_width > 0);
20978 if (EQ (Vdisplay_line_numbers, Qrelative))
20979 lnum_offset = it->pt_lnum;
20980 else if (EQ (Vdisplay_line_numbers, Qvisual))
20981 lnum_offset = 0;
20983 /* Under 'relative', display the absolute line number for the
20984 current line, unless the user requests otherwise. */
20985 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
20986 if ((EQ (Vdisplay_line_numbers, Qrelative)
20987 || EQ (Vdisplay_line_numbers, Qvisual))
20988 && lnum_to_display == 0
20989 && !NILP (Vdisplay_line_numbers_current_absolute))
20990 lnum_to_display = it->pt_lnum + 1;
20991 /* In L2R rows we need to append the blank separator, in R2L
20992 rows we need to prepend it. But this function is usually
20993 called when no display elements were produced from the
20994 following line, so the paragraph direction might be unknown.
20995 Therefore we cheat and add 2 blanks, one on either side. */
20996 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
20997 strcat (lnum_buf, " ");
20999 /* Setup for producing the glyphs. */
21000 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21001 /* FIXME: Use specialized face. */
21002 DEFAULT_FACE_ID);
21003 scratch_glyph_row.reversed_p = false;
21004 scratch_glyph_row.used[TEXT_AREA] = 0;
21005 SET_TEXT_POS (tem_it.position, 0, 0);
21006 tem_it.avoid_cursor_p = true;
21007 tem_it.bidi_p = true;
21008 tem_it.bidi_it.type = WEAK_EN;
21009 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21010 1 level in R2L paragraphs. Emulate that, assuming we are in
21011 an L2R paragraph. */
21012 tem_it.bidi_it.resolved_level = 2;
21014 /* Produce glyphs for the line number in a scratch glyph_row. */
21015 int n_glyphs_before;
21016 for (const char *p = lnum_buf; *p; p++)
21018 /* For continuation lines and lines after ZV, instead of a line
21019 number, produce a blank prefix of the same width. Use the
21020 default face for the blank field beyond ZV. */
21021 if (beyond_zv)
21022 tem_it.face_id = it->base_face_id;
21023 else if (lnum_face_id != current_lnum_face_id
21024 && (EQ (Vdisplay_line_numbers, Qvisual)
21025 ? this_line == 0
21026 : this_line == it->pt_lnum))
21027 tem_it.face_id = current_lnum_face_id;
21028 else
21029 tem_it.face_id = lnum_face_id;
21030 if (beyond_zv
21031 /* Don't display the same line number more than once. */
21032 || (!EQ (Vdisplay_line_numbers, Qvisual)
21033 && (it->continuation_lines_width > 0
21034 || (this_line == last_line && !first_time))))
21035 tem_it.c = tem_it.char_to_display = ' ';
21036 else
21037 tem_it.c = tem_it.char_to_display = *p;
21038 tem_it.len = 1;
21039 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21040 /* Make sure these glyphs will have a "position" of -1. */
21041 SET_TEXT_POS (tem_it.position, -1, -1);
21042 PRODUCE_GLYPHS (&tem_it);
21044 /* Stop producing glyphs if we don't have enough space on
21045 this line. FIXME: should we refrain from producing the
21046 line number at all in that case? */
21047 if (tem_it.current_x > tem_it.last_visible_x)
21049 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21050 break;
21054 /* Record the width in pixels we need for the line number display. */
21055 it->lnum_pixel_width = tem_it.current_x;
21056 /* Copy the produced glyphs into IT's glyph_row. */
21057 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21058 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21059 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21060 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21062 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21064 for ( ; g < e; g++)
21066 it->current_x += g->pixel_width;
21067 /* The following is important when this function is called
21068 from move_it_in_display_line_to: HPOS is incremented only
21069 when we are in the visible portion of the glyph row. */
21070 if (it->current_x > it->first_visible_x)
21071 it->hpos++;
21072 if (p)
21074 *p++ = *g;
21075 (*u)++;
21079 /* Update IT's metrics due to glyphs produced for line numbers. */
21080 if (it->glyph_row)
21082 struct glyph_row *row = it->glyph_row;
21084 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21085 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21086 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21087 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21088 tem_it.max_phys_descent);
21090 else
21092 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21093 it->max_descent = max (it->max_descent, tem_it.max_descent);
21094 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21095 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21098 bidi_unshelve_cache (itdata, false);
21101 /* Return true if this glyph row needs a line number to be produced
21102 for it. */
21103 static bool
21104 should_produce_line_number (struct it *it)
21106 if (NILP (Vdisplay_line_numbers))
21107 return false;
21109 /* Don't display line numbers in minibuffer windows. */
21110 if (MINI_WINDOW_P (it->w))
21111 return false;
21113 #ifdef HAVE_WINDOW_SYSTEM
21114 /* Don't display line number in tooltip frames. */
21115 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame))
21116 return false;
21117 #endif
21119 /* If the character at current position has a non-nil special
21120 property, disable line numbers for this row. This is for
21121 packages such as company-mode, which need this for their tricky
21122 layout, where line numbers get in the way. */
21123 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21124 Qdisplay_line_numbers_disable,
21125 it->window);
21126 /* For ZV, we need to also look in empty overlays at that point,
21127 because get-char-property always returns nil for ZV, except if
21128 the property is in 'default-text-properties'. */
21129 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21130 val = disable_line_numbers_overlay_at_eob ();
21131 return NILP (val) ? true : false;
21134 /* Return true if ROW has no glyphs except those inserted by the
21135 display engine. This is needed for indicate-empty-lines and
21136 similar features when the glyph row starts with glyphs which didn't
21137 come from buffer or string. */
21138 static bool
21139 row_text_area_empty (struct glyph_row *row)
21141 if (!row->reversed_p)
21143 for (struct glyph *g = row->glyphs[TEXT_AREA];
21144 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21145 g++)
21146 if (!NILP (g->object) || g->charpos > 0)
21147 return false;
21149 else
21151 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21152 g > row->glyphs[TEXT_AREA];
21153 g--)
21154 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21155 return false;
21158 return true;
21161 /* Construct the glyph row IT->glyph_row in the desired matrix of
21162 IT->w from text at the current position of IT. See dispextern.h
21163 for an overview of struct it. Value is true if
21164 IT->glyph_row displays text, as opposed to a line displaying ZV
21165 only. CURSOR_VPOS is the window-relative vertical position of
21166 the glyph row displaying the cursor, or -1 if unknown. */
21168 static bool
21169 display_line (struct it *it, int cursor_vpos)
21171 struct glyph_row *row = it->glyph_row;
21172 Lisp_Object overlay_arrow_string;
21173 struct it wrap_it;
21174 void *wrap_data = NULL;
21175 bool may_wrap = false;
21176 int wrap_x UNINIT;
21177 int wrap_row_used = -1;
21178 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21179 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21180 int wrap_row_extra_line_spacing UNINIT;
21181 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21182 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21183 int cvpos;
21184 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21185 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21186 bool pending_handle_line_prefix = false;
21187 int header_line = window_wants_header_line (it->w);
21188 bool hscroll_this_line = (cursor_vpos >= 0
21189 && it->vpos == cursor_vpos - header_line
21190 && hscrolling_current_line_p (it->w));
21191 int first_visible_x = it->first_visible_x;
21192 int last_visible_x = it->last_visible_x;
21193 int x_incr = 0;
21195 /* We always start displaying at hpos zero even if hscrolled. */
21196 eassert (it->hpos == 0 && it->current_x == 0);
21198 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21199 >= it->w->desired_matrix->nrows)
21201 it->w->nrows_scale_factor++;
21202 it->f->fonts_changed = true;
21203 return false;
21206 /* Clear the result glyph row and enable it. */
21207 prepare_desired_row (it->w, row, false);
21209 row->y = it->current_y;
21210 row->start = it->start;
21211 row->continuation_lines_width = it->continuation_lines_width;
21212 row->displays_text_p = true;
21213 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21214 it->starts_in_middle_of_char_p = false;
21216 /* Arrange the overlays nicely for our purposes. Usually, we call
21217 display_line on only one line at a time, in which case this
21218 can't really hurt too much, or we call it on lines which appear
21219 one after another in the buffer, in which case all calls to
21220 recenter_overlay_lists but the first will be pretty cheap. */
21221 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21223 /* If we are going to display the cursor's line, account for the
21224 hscroll of that line. We subtract the window's min_hscroll,
21225 because that was already accounted for in init_iterator. */
21226 if (hscroll_this_line)
21227 x_incr =
21228 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21229 * FRAME_COLUMN_WIDTH (it->f);
21231 bool line_number_needed = should_produce_line_number (it);
21233 /* Move over display elements that are not visible because we are
21234 hscrolled. This may stop at an x-position < first_visible_x
21235 if the first glyph is partially visible or if we hit a line end. */
21236 if (it->current_x < it->first_visible_x + x_incr)
21238 enum move_it_result move_result;
21240 this_line_min_pos = row->start.pos;
21241 if (hscroll_this_line)
21243 it->first_visible_x += x_incr;
21244 it->last_visible_x += x_incr;
21246 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21247 MOVE_TO_POS | MOVE_TO_X);
21248 /* If we are under a large hscroll, move_it_in_display_line_to
21249 could hit the end of the line without reaching
21250 first_visible_x. Pretend that we did reach it. This is
21251 especially important on a TTY, where we will call
21252 extend_face_to_end_of_line, which needs to know how many
21253 blank glyphs to produce. */
21254 if (it->current_x < it->first_visible_x
21255 && (move_result == MOVE_NEWLINE_OR_CR
21256 || move_result == MOVE_POS_MATCH_OR_ZV))
21257 it->current_x = it->first_visible_x;
21259 /* Record the smallest positions seen while we moved over
21260 display elements that are not visible. This is needed by
21261 redisplay_internal for optimizing the case where the cursor
21262 stays inside the same line. The rest of this function only
21263 considers positions that are actually displayed, so
21264 RECORD_MAX_MIN_POS will not otherwise record positions that
21265 are hscrolled to the left of the left edge of the window. */
21266 min_pos = CHARPOS (this_line_min_pos);
21267 min_bpos = BYTEPOS (this_line_min_pos);
21269 /* Produce line number, if needed. */
21270 if (line_number_needed)
21271 maybe_produce_line_number (it);
21273 else if (it->area == TEXT_AREA)
21275 /* Line numbers should precede the line-prefix or wrap-prefix. */
21276 if (line_number_needed)
21277 maybe_produce_line_number (it);
21279 /* We only do this when not calling move_it_in_display_line_to
21280 above, because that function calls itself handle_line_prefix. */
21281 handle_line_prefix (it);
21283 else
21285 /* Line-prefix and wrap-prefix are always displayed in the text
21286 area. But if this is the first call to display_line after
21287 init_iterator, the iterator might have been set up to write
21288 into a marginal area, e.g. if the line begins with some
21289 display property that writes to the margins. So we need to
21290 wait with the call to handle_line_prefix until whatever
21291 writes to the margin has done its job. */
21292 pending_handle_line_prefix = true;
21295 /* Get the initial row height. This is either the height of the
21296 text hscrolled, if there is any, or zero. */
21297 row->ascent = it->max_ascent;
21298 row->height = it->max_ascent + it->max_descent;
21299 row->phys_ascent = it->max_phys_ascent;
21300 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21301 row->extra_line_spacing = it->max_extra_line_spacing;
21303 /* Utility macro to record max and min buffer positions seen until now. */
21304 #define RECORD_MAX_MIN_POS(IT) \
21305 do \
21307 bool composition_p \
21308 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21309 ptrdiff_t current_pos = \
21310 composition_p ? (IT)->cmp_it.charpos \
21311 : IT_CHARPOS (*(IT)); \
21312 ptrdiff_t current_bpos = \
21313 composition_p ? CHAR_TO_BYTE (current_pos) \
21314 : IT_BYTEPOS (*(IT)); \
21315 if (current_pos < min_pos) \
21317 min_pos = current_pos; \
21318 min_bpos = current_bpos; \
21320 if (IT_CHARPOS (*it) > max_pos) \
21322 max_pos = IT_CHARPOS (*it); \
21323 max_bpos = IT_BYTEPOS (*it); \
21326 while (false)
21328 /* Loop generating characters. The loop is left with IT on the next
21329 character to display. */
21330 while (true)
21332 int n_glyphs_before, hpos_before, x_before;
21333 int x, nglyphs;
21334 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21336 /* Retrieve the next thing to display. Value is false if end of
21337 buffer reached. */
21338 if (!get_next_display_element (it))
21340 bool row_has_glyphs = false;
21341 /* Maybe add a space at the end of this line that is used to
21342 display the cursor there under X. Set the charpos of the
21343 first glyph of blank lines not corresponding to any text
21344 to -1. */
21345 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21346 row->exact_window_width_line_p = true;
21347 else if ((append_space_for_newline (it, true)
21348 && row->used[TEXT_AREA] == 1)
21349 || row->used[TEXT_AREA] == 0
21350 || (row_has_glyphs = row_text_area_empty (row)))
21352 row->glyphs[TEXT_AREA]->charpos = -1;
21353 /* Don't reset the displays_text_p flag if we are
21354 displaying line numbers or line-prefix. */
21355 if (!row_has_glyphs)
21356 row->displays_text_p = false;
21358 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21359 && (!MINI_WINDOW_P (it->w)))
21360 row->indicate_empty_line_p = true;
21363 it->continuation_lines_width = 0;
21364 /* Reset those iterator values set from display property
21365 values. This is for the case when the display property
21366 ends at ZV, and is not a replacing property, so pop_it is
21367 not called. */
21368 it->font_height = Qnil;
21369 it->voffset = 0;
21370 row->ends_at_zv_p = true;
21371 /* A row that displays right-to-left text must always have
21372 its last face extended all the way to the end of line,
21373 even if this row ends in ZV, because we still write to
21374 the screen left to right. We also need to extend the
21375 last face if the default face is remapped to some
21376 different face, otherwise the functions that clear
21377 portions of the screen will clear with the default face's
21378 background color. */
21379 if (row->reversed_p
21380 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21381 extend_face_to_end_of_line (it);
21382 break;
21385 /* Now, get the metrics of what we want to display. This also
21386 generates glyphs in `row' (which is IT->glyph_row). */
21387 n_glyphs_before = row->used[TEXT_AREA];
21388 x = it->current_x;
21390 /* Remember the line height so far in case the next element doesn't
21391 fit on the line. */
21392 if (it->line_wrap != TRUNCATE)
21394 ascent = it->max_ascent;
21395 descent = it->max_descent;
21396 phys_ascent = it->max_phys_ascent;
21397 phys_descent = it->max_phys_descent;
21399 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21401 if (IT_DISPLAYING_WHITESPACE (it))
21402 may_wrap = true;
21403 else if (may_wrap)
21405 SAVE_IT (wrap_it, *it, wrap_data);
21406 wrap_x = x;
21407 wrap_row_used = row->used[TEXT_AREA];
21408 wrap_row_ascent = row->ascent;
21409 wrap_row_height = row->height;
21410 wrap_row_phys_ascent = row->phys_ascent;
21411 wrap_row_phys_height = row->phys_height;
21412 wrap_row_extra_line_spacing = row->extra_line_spacing;
21413 wrap_row_min_pos = min_pos;
21414 wrap_row_min_bpos = min_bpos;
21415 wrap_row_max_pos = max_pos;
21416 wrap_row_max_bpos = max_bpos;
21417 may_wrap = false;
21422 PRODUCE_GLYPHS (it);
21424 /* If this display element was in marginal areas, continue with
21425 the next one. */
21426 if (it->area != TEXT_AREA)
21428 row->ascent = max (row->ascent, it->max_ascent);
21429 row->height = max (row->height, it->max_ascent + it->max_descent);
21430 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21431 row->phys_height = max (row->phys_height,
21432 it->max_phys_ascent + it->max_phys_descent);
21433 row->extra_line_spacing = max (row->extra_line_spacing,
21434 it->max_extra_line_spacing);
21435 set_iterator_to_next (it, true);
21436 /* If we didn't handle the line/wrap prefix above, and the
21437 call to set_iterator_to_next just switched to TEXT_AREA,
21438 process the prefix now. */
21439 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21441 /* Line numbers should precede the line-prefix or wrap-prefix. */
21442 if (line_number_needed)
21443 maybe_produce_line_number (it);
21445 pending_handle_line_prefix = false;
21446 handle_line_prefix (it);
21448 continue;
21451 /* Does the display element fit on the line? If we truncate
21452 lines, we should draw past the right edge of the window. If
21453 we don't truncate, we want to stop so that we can display the
21454 continuation glyph before the right margin. If lines are
21455 continued, there are two possible strategies for characters
21456 resulting in more than 1 glyph (e.g. tabs): Display as many
21457 glyphs as possible in this line and leave the rest for the
21458 continuation line, or display the whole element in the next
21459 line. Original redisplay did the former, so we do it also. */
21460 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21461 hpos_before = it->hpos;
21462 x_before = x;
21464 if (/* Not a newline. */
21465 nglyphs > 0
21466 /* Glyphs produced fit entirely in the line. */
21467 && it->current_x < it->last_visible_x)
21469 it->hpos += nglyphs;
21470 row->ascent = max (row->ascent, it->max_ascent);
21471 row->height = max (row->height, it->max_ascent + it->max_descent);
21472 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21473 row->phys_height = max (row->phys_height,
21474 it->max_phys_ascent + it->max_phys_descent);
21475 row->extra_line_spacing = max (row->extra_line_spacing,
21476 it->max_extra_line_spacing);
21477 if (it->current_x - it->pixel_width < it->first_visible_x
21478 /* In R2L rows, we arrange in extend_face_to_end_of_line
21479 to add a right offset to the line, by a suitable
21480 change to the stretch glyph that is the leftmost
21481 glyph of the line. */
21482 && !row->reversed_p)
21483 row->x = x - it->first_visible_x;
21484 /* Record the maximum and minimum buffer positions seen so
21485 far in glyphs that will be displayed by this row. */
21486 if (it->bidi_p)
21487 RECORD_MAX_MIN_POS (it);
21489 else
21491 int i, new_x;
21492 struct glyph *glyph;
21494 for (i = 0; i < nglyphs; ++i, x = new_x)
21496 /* Identify the glyphs added by the last call to
21497 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21498 the previous glyphs. */
21499 if (!row->reversed_p)
21500 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21501 else
21502 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21503 new_x = x + glyph->pixel_width;
21505 if (/* Lines are continued. */
21506 it->line_wrap != TRUNCATE
21507 && (/* Glyph doesn't fit on the line. */
21508 new_x > it->last_visible_x
21509 /* Or it fits exactly on a window system frame. */
21510 || (new_x == it->last_visible_x
21511 && FRAME_WINDOW_P (it->f)
21512 && (row->reversed_p
21513 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21514 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21516 /* End of a continued line. */
21518 if (it->hpos == 0
21519 || (new_x == it->last_visible_x
21520 && FRAME_WINDOW_P (it->f)
21521 && (row->reversed_p
21522 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21523 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21525 /* Current glyph is the only one on the line or
21526 fits exactly on the line. We must continue
21527 the line because we can't draw the cursor
21528 after the glyph. */
21529 row->continued_p = true;
21530 it->current_x = new_x;
21531 it->continuation_lines_width += new_x;
21532 ++it->hpos;
21533 if (i == nglyphs - 1)
21535 /* If line-wrap is on, check if a previous
21536 wrap point was found. */
21537 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21538 && wrap_row_used > 0
21539 /* Even if there is a previous wrap
21540 point, continue the line here as
21541 usual, if (i) the previous character
21542 was a space or tab AND (ii) the
21543 current character is not. */
21544 && (!may_wrap
21545 || IT_DISPLAYING_WHITESPACE (it)))
21546 goto back_to_wrap;
21548 /* Record the maximum and minimum buffer
21549 positions seen so far in glyphs that will be
21550 displayed by this row. */
21551 if (it->bidi_p)
21552 RECORD_MAX_MIN_POS (it);
21553 set_iterator_to_next (it, true);
21554 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21556 if (!get_next_display_element (it))
21558 row->exact_window_width_line_p = true;
21559 it->continuation_lines_width = 0;
21560 it->font_height = Qnil;
21561 it->voffset = 0;
21562 row->continued_p = false;
21563 row->ends_at_zv_p = true;
21565 else if (ITERATOR_AT_END_OF_LINE_P (it))
21567 row->continued_p = false;
21568 row->exact_window_width_line_p = true;
21570 /* If line-wrap is on, check if a
21571 previous wrap point was found. */
21572 else if (wrap_row_used > 0
21573 /* Even if there is a previous wrap
21574 point, continue the line here as
21575 usual, if (i) the previous character
21576 was a space or tab AND (ii) the
21577 current character is not. */
21578 && (!may_wrap
21579 || IT_DISPLAYING_WHITESPACE (it)))
21580 goto back_to_wrap;
21584 else if (it->bidi_p)
21585 RECORD_MAX_MIN_POS (it);
21586 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21587 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21588 extend_face_to_end_of_line (it);
21590 else if (CHAR_GLYPH_PADDING_P (*glyph)
21591 && !FRAME_WINDOW_P (it->f))
21593 /* A padding glyph that doesn't fit on this line.
21594 This means the whole character doesn't fit
21595 on the line. */
21596 if (row->reversed_p)
21597 unproduce_glyphs (it, row->used[TEXT_AREA]
21598 - n_glyphs_before);
21599 row->used[TEXT_AREA] = n_glyphs_before;
21601 /* Fill the rest of the row with continuation
21602 glyphs like in 20.x. */
21603 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21604 < row->glyphs[1 + TEXT_AREA])
21605 produce_special_glyphs (it, IT_CONTINUATION);
21607 row->continued_p = true;
21608 it->current_x = x_before;
21609 it->continuation_lines_width += x_before;
21611 /* Restore the height to what it was before the
21612 element not fitting on the line. */
21613 it->max_ascent = ascent;
21614 it->max_descent = descent;
21615 it->max_phys_ascent = phys_ascent;
21616 it->max_phys_descent = phys_descent;
21617 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21618 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21619 extend_face_to_end_of_line (it);
21621 else if (wrap_row_used > 0)
21623 back_to_wrap:
21624 if (row->reversed_p)
21625 unproduce_glyphs (it,
21626 row->used[TEXT_AREA] - wrap_row_used);
21627 RESTORE_IT (it, &wrap_it, wrap_data);
21628 it->continuation_lines_width += wrap_x;
21629 row->used[TEXT_AREA] = wrap_row_used;
21630 row->ascent = wrap_row_ascent;
21631 row->height = wrap_row_height;
21632 row->phys_ascent = wrap_row_phys_ascent;
21633 row->phys_height = wrap_row_phys_height;
21634 row->extra_line_spacing = wrap_row_extra_line_spacing;
21635 min_pos = wrap_row_min_pos;
21636 min_bpos = wrap_row_min_bpos;
21637 max_pos = wrap_row_max_pos;
21638 max_bpos = wrap_row_max_bpos;
21639 row->continued_p = true;
21640 row->ends_at_zv_p = false;
21641 row->exact_window_width_line_p = false;
21642 it->continuation_lines_width += x;
21644 /* Make sure that a non-default face is extended
21645 up to the right margin of the window. */
21646 extend_face_to_end_of_line (it);
21648 else if ((it->what == IT_CHARACTER
21649 || it->what == IT_STRETCH
21650 || it->what == IT_COMPOSITION)
21651 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21653 /* A TAB that extends past the right edge of the
21654 window. This produces a single glyph on
21655 window system frames. We leave the glyph in
21656 this row and let it fill the row, but don't
21657 consume the TAB. */
21658 if ((row->reversed_p
21659 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21660 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21661 produce_special_glyphs (it, IT_CONTINUATION);
21662 it->continuation_lines_width += it->last_visible_x;
21663 row->ends_in_middle_of_char_p = true;
21664 row->continued_p = true;
21665 glyph->pixel_width = it->last_visible_x - x;
21666 it->starts_in_middle_of_char_p = true;
21667 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21668 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21669 extend_face_to_end_of_line (it);
21671 else
21673 /* Something other than a TAB that draws past
21674 the right edge of the window. Restore
21675 positions to values before the element. */
21676 if (row->reversed_p)
21677 unproduce_glyphs (it, row->used[TEXT_AREA]
21678 - (n_glyphs_before + i));
21679 row->used[TEXT_AREA] = n_glyphs_before + i;
21681 /* Display continuation glyphs. */
21682 it->current_x = x_before;
21683 it->continuation_lines_width += x;
21684 if (!FRAME_WINDOW_P (it->f)
21685 || (row->reversed_p
21686 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21687 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21688 produce_special_glyphs (it, IT_CONTINUATION);
21689 row->continued_p = true;
21691 extend_face_to_end_of_line (it);
21693 if (nglyphs > 1 && i > 0)
21695 row->ends_in_middle_of_char_p = true;
21696 it->starts_in_middle_of_char_p = true;
21699 /* Restore the height to what it was before the
21700 element not fitting on the line. */
21701 it->max_ascent = ascent;
21702 it->max_descent = descent;
21703 it->max_phys_ascent = phys_ascent;
21704 it->max_phys_descent = phys_descent;
21707 break;
21709 else if (new_x > it->first_visible_x)
21711 /* Increment number of glyphs actually displayed. */
21712 ++it->hpos;
21714 /* Record the maximum and minimum buffer positions
21715 seen so far in glyphs that will be displayed by
21716 this row. */
21717 if (it->bidi_p)
21718 RECORD_MAX_MIN_POS (it);
21720 if (x < it->first_visible_x && !row->reversed_p)
21721 /* Glyph is partially visible, i.e. row starts at
21722 negative X position. Don't do that in R2L
21723 rows, where we arrange to add a right offset to
21724 the line in extend_face_to_end_of_line, by a
21725 suitable change to the stretch glyph that is
21726 the leftmost glyph of the line. */
21727 row->x = x - it->first_visible_x;
21728 /* When the last glyph of an R2L row only fits
21729 partially on the line, we need to set row->x to a
21730 negative offset, so that the leftmost glyph is
21731 the one that is partially visible. But if we are
21732 going to produce the truncation glyph, this will
21733 be taken care of in produce_special_glyphs. */
21734 if (row->reversed_p
21735 && new_x > it->last_visible_x
21736 && !(it->line_wrap == TRUNCATE
21737 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21739 eassert (FRAME_WINDOW_P (it->f));
21740 row->x = it->last_visible_x - new_x;
21743 else
21745 /* Glyph is completely off the left margin of the
21746 window. This should not happen because of the
21747 move_it_in_display_line at the start of this
21748 function, unless the text display area of the
21749 window is empty. */
21750 eassert (it->first_visible_x <= it->last_visible_x);
21753 /* Even if this display element produced no glyphs at all,
21754 we want to record its position. */
21755 if (it->bidi_p && nglyphs == 0)
21756 RECORD_MAX_MIN_POS (it);
21758 row->ascent = max (row->ascent, it->max_ascent);
21759 row->height = max (row->height, it->max_ascent + it->max_descent);
21760 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21761 row->phys_height = max (row->phys_height,
21762 it->max_phys_ascent + it->max_phys_descent);
21763 row->extra_line_spacing = max (row->extra_line_spacing,
21764 it->max_extra_line_spacing);
21766 /* End of this display line if row is continued. */
21767 if (row->continued_p || row->ends_at_zv_p)
21768 break;
21771 at_end_of_line:
21772 /* Is this a line end? If yes, we're also done, after making
21773 sure that a non-default face is extended up to the right
21774 margin of the window. */
21775 if (ITERATOR_AT_END_OF_LINE_P (it))
21777 int used_before = row->used[TEXT_AREA];
21779 row->ends_in_newline_from_string_p = STRINGP (it->object);
21781 /* Add a space at the end of the line that is used to
21782 display the cursor there. */
21783 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21784 append_space_for_newline (it, false);
21786 /* Extend the face to the end of the line. */
21787 extend_face_to_end_of_line (it);
21789 /* Make sure we have the position. */
21790 if (used_before == 0)
21791 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21793 /* Record the position of the newline, for use in
21794 find_row_edges. */
21795 it->eol_pos = it->current.pos;
21797 /* Consume the line end. This skips over invisible lines. */
21798 set_iterator_to_next (it, true);
21799 it->continuation_lines_width = 0;
21800 break;
21803 /* Proceed with next display element. Note that this skips
21804 over lines invisible because of selective display. */
21805 set_iterator_to_next (it, true);
21807 /* If we truncate lines, we are done when the last displayed
21808 glyphs reach past the right margin of the window. */
21809 if (it->line_wrap == TRUNCATE
21810 && ((FRAME_WINDOW_P (it->f)
21811 /* Images are preprocessed in produce_image_glyph such
21812 that they are cropped at the right edge of the
21813 window, so an image glyph will always end exactly at
21814 last_visible_x, even if there's no right fringe. */
21815 && ((row->reversed_p
21816 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21817 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21818 || it->what == IT_IMAGE))
21819 ? (it->current_x >= it->last_visible_x)
21820 : (it->current_x > it->last_visible_x)))
21822 /* Maybe add truncation glyphs. */
21823 if (!FRAME_WINDOW_P (it->f)
21824 || (row->reversed_p
21825 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21826 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21828 int i, n;
21830 if (!row->reversed_p)
21832 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21833 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21834 break;
21836 else
21838 for (i = 0; i < row->used[TEXT_AREA]; i++)
21839 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21840 break;
21841 /* Remove any padding glyphs at the front of ROW, to
21842 make room for the truncation glyphs we will be
21843 adding below. The loop below always inserts at
21844 least one truncation glyph, so also remove the
21845 last glyph added to ROW. */
21846 unproduce_glyphs (it, i + 1);
21847 /* Adjust i for the loop below. */
21848 i = row->used[TEXT_AREA] - (i + 1);
21851 /* produce_special_glyphs overwrites the last glyph, so
21852 we don't want that if we want to keep that last
21853 glyph, which means it's an image. */
21854 if (it->current_x > it->last_visible_x)
21856 it->current_x = x_before;
21857 if (!FRAME_WINDOW_P (it->f))
21859 for (n = row->used[TEXT_AREA]; i < n; ++i)
21861 row->used[TEXT_AREA] = i;
21862 produce_special_glyphs (it, IT_TRUNCATION);
21865 else
21867 row->used[TEXT_AREA] = i;
21868 produce_special_glyphs (it, IT_TRUNCATION);
21870 it->hpos = hpos_before;
21873 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21875 /* Don't truncate if we can overflow newline into fringe. */
21876 if (!get_next_display_element (it))
21878 it->continuation_lines_width = 0;
21879 it->font_height = Qnil;
21880 it->voffset = 0;
21881 row->ends_at_zv_p = true;
21882 row->exact_window_width_line_p = true;
21883 break;
21885 if (ITERATOR_AT_END_OF_LINE_P (it))
21887 row->exact_window_width_line_p = true;
21888 goto at_end_of_line;
21890 it->current_x = x_before;
21891 it->hpos = hpos_before;
21894 row->truncated_on_right_p = true;
21895 it->continuation_lines_width = 0;
21896 reseat_at_next_visible_line_start (it, false);
21897 /* We insist below that IT's position be at ZV because in
21898 bidi-reordered lines the character at visible line start
21899 might not be the character that follows the newline in
21900 the logical order. */
21901 if (IT_BYTEPOS (*it) > BEG_BYTE)
21902 row->ends_at_zv_p =
21903 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21904 else
21905 row->ends_at_zv_p = false;
21906 break;
21910 if (wrap_data)
21911 bidi_unshelve_cache (wrap_data, true);
21913 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21914 at the left window margin. */
21915 if (it->first_visible_x
21916 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21918 if (!FRAME_WINDOW_P (it->f)
21919 || (((row->reversed_p
21920 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21921 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21922 /* Don't let insert_left_trunc_glyphs overwrite the
21923 first glyph of the row if it is an image. */
21924 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21925 insert_left_trunc_glyphs (it);
21926 row->truncated_on_left_p = true;
21929 /* Remember the position at which this line ends.
21931 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21932 cannot be before the call to find_row_edges below, since that is
21933 where these positions are determined. */
21934 row->end = it->current;
21935 if (!it->bidi_p)
21937 row->minpos = row->start.pos;
21938 row->maxpos = row->end.pos;
21940 else
21942 /* ROW->minpos and ROW->maxpos must be the smallest and
21943 `1 + the largest' buffer positions in ROW. But if ROW was
21944 bidi-reordered, these two positions can be anywhere in the
21945 row, so we must determine them now. */
21946 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21949 /* If the start of this line is the overlay arrow-position, then
21950 mark this glyph row as the one containing the overlay arrow.
21951 This is clearly a mess with variable size fonts. It would be
21952 better to let it be displayed like cursors under X. */
21953 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21954 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21955 !NILP (overlay_arrow_string)))
21957 /* Overlay arrow in window redisplay is a fringe bitmap. */
21958 if (STRINGP (overlay_arrow_string))
21960 struct glyph_row *arrow_row
21961 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21962 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21963 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21964 struct glyph *p = row->glyphs[TEXT_AREA];
21965 struct glyph *p2, *end;
21967 /* Copy the arrow glyphs. */
21968 while (glyph < arrow_end)
21969 *p++ = *glyph++;
21971 /* Throw away padding glyphs. */
21972 p2 = p;
21973 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21974 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21975 ++p2;
21976 if (p2 > p)
21978 while (p2 < end)
21979 *p++ = *p2++;
21980 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21983 else
21985 eassert (INTEGERP (overlay_arrow_string));
21986 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21988 overlay_arrow_seen = true;
21991 /* Highlight trailing whitespace. */
21992 if (!NILP (Vshow_trailing_whitespace))
21993 highlight_trailing_whitespace (it->f, it->glyph_row);
21995 /* Compute pixel dimensions of this line. */
21996 compute_line_metrics (it);
21998 /* Implementation note: No changes in the glyphs of ROW or in their
21999 faces can be done past this point, because compute_line_metrics
22000 computes ROW's hash value and stores it within the glyph_row
22001 structure. */
22003 /* Record whether this row ends inside an ellipsis. */
22004 row->ends_in_ellipsis_p
22005 = (it->method == GET_FROM_DISPLAY_VECTOR
22006 && it->ellipsis_p);
22008 /* Save fringe bitmaps in this row. */
22009 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22010 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22011 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22012 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22014 it->left_user_fringe_bitmap = 0;
22015 it->left_user_fringe_face_id = 0;
22016 it->right_user_fringe_bitmap = 0;
22017 it->right_user_fringe_face_id = 0;
22019 /* Maybe set the cursor. */
22020 cvpos = it->w->cursor.vpos;
22021 if ((cvpos < 0
22022 /* In bidi-reordered rows, keep checking for proper cursor
22023 position even if one has been found already, because buffer
22024 positions in such rows change non-linearly with ROW->VPOS,
22025 when a line is continued. One exception: when we are at ZV,
22026 display cursor on the first suitable glyph row, since all
22027 the empty rows after that also have their position set to ZV. */
22028 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22029 lines' rows is implemented for bidi-reordered rows. */
22030 || (it->bidi_p
22031 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22032 && PT >= MATRIX_ROW_START_CHARPOS (row)
22033 && PT <= MATRIX_ROW_END_CHARPOS (row)
22034 && cursor_row_p (row))
22035 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22037 /* Prepare for the next line. This line starts horizontally at (X
22038 HPOS) = (0 0). Vertical positions are incremented. As a
22039 convenience for the caller, IT->glyph_row is set to the next
22040 row to be used. */
22041 it->current_x = it->hpos = 0;
22042 it->current_y += row->height;
22043 /* Restore the first and last visible X if we adjusted them for
22044 current-line hscrolling. */
22045 if (hscroll_this_line)
22047 it->first_visible_x = first_visible_x;
22048 it->last_visible_x = last_visible_x;
22050 SET_TEXT_POS (it->eol_pos, 0, 0);
22051 ++it->vpos;
22052 ++it->glyph_row;
22053 /* The next row should by default use the same value of the
22054 reversed_p flag as this one. set_iterator_to_next decides when
22055 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22056 the flag accordingly. */
22057 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22058 it->glyph_row->reversed_p = row->reversed_p;
22059 it->start = row->end;
22060 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22062 #undef RECORD_MAX_MIN_POS
22065 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22066 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22067 doc: /* Return paragraph direction at point in BUFFER.
22068 Value is either `left-to-right' or `right-to-left'.
22069 If BUFFER is omitted or nil, it defaults to the current buffer.
22071 Paragraph direction determines how the text in the paragraph is displayed.
22072 In left-to-right paragraphs, text begins at the left margin of the window
22073 and the reading direction is generally left to right. In right-to-left
22074 paragraphs, text begins at the right margin and is read from right to left.
22076 See also `bidi-paragraph-direction'. */)
22077 (Lisp_Object buffer)
22079 struct buffer *buf = current_buffer;
22080 struct buffer *old = buf;
22082 if (! NILP (buffer))
22084 CHECK_BUFFER (buffer);
22085 buf = XBUFFER (buffer);
22088 if (NILP (BVAR (buf, bidi_display_reordering))
22089 || NILP (BVAR (buf, enable_multibyte_characters))
22090 /* When we are loading loadup.el, the character property tables
22091 needed for bidi iteration are not yet available. */
22092 || redisplay__inhibit_bidi)
22093 return Qleft_to_right;
22094 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22095 return BVAR (buf, bidi_paragraph_direction);
22096 else
22098 /* Determine the direction from buffer text. We could try to
22099 use current_matrix if it is up to date, but this seems fast
22100 enough as it is. */
22101 struct bidi_it itb;
22102 ptrdiff_t pos = BUF_PT (buf);
22103 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22104 int c;
22105 void *itb_data = bidi_shelve_cache ();
22107 set_buffer_temp (buf);
22108 /* bidi_paragraph_init finds the base direction of the paragraph
22109 by searching forward from paragraph start. We need the base
22110 direction of the current or _previous_ paragraph, so we need
22111 to make sure we are within that paragraph. To that end, find
22112 the previous non-empty line. */
22113 if (pos >= ZV && pos > BEGV)
22114 DEC_BOTH (pos, bytepos);
22115 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22116 if (fast_looking_at (trailing_white_space,
22117 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22119 while ((c = FETCH_BYTE (bytepos)) == '\n'
22120 || c == ' ' || c == '\t' || c == '\f')
22122 if (bytepos <= BEGV_BYTE)
22123 break;
22124 bytepos--;
22125 pos--;
22127 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22128 bytepos--;
22130 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22131 itb.paragraph_dir = NEUTRAL_DIR;
22132 itb.string.s = NULL;
22133 itb.string.lstring = Qnil;
22134 itb.string.bufpos = 0;
22135 itb.string.from_disp_str = false;
22136 itb.string.unibyte = false;
22137 /* We have no window to use here for ignoring window-specific
22138 overlays. Using NULL for window pointer will cause
22139 compute_display_string_pos to use the current buffer. */
22140 itb.w = NULL;
22141 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22142 bidi_unshelve_cache (itb_data, false);
22143 set_buffer_temp (old);
22144 switch (itb.paragraph_dir)
22146 case L2R:
22147 return Qleft_to_right;
22148 break;
22149 case R2L:
22150 return Qright_to_left;
22151 break;
22152 default:
22153 emacs_abort ();
22158 DEFUN ("bidi-find-overridden-directionality",
22159 Fbidi_find_overridden_directionality,
22160 Sbidi_find_overridden_directionality, 2, 3, 0,
22161 doc: /* Return position between FROM and TO where directionality was overridden.
22163 This function returns the first character position in the specified
22164 region of OBJECT where there is a character whose `bidi-class' property
22165 is `L', but which was forced to display as `R' by a directional
22166 override, and likewise with characters whose `bidi-class' is `R'
22167 or `AL' that were forced to display as `L'.
22169 If no such character is found, the function returns nil.
22171 OBJECT is a Lisp string or buffer to search for overridden
22172 directionality, and defaults to the current buffer if nil or omitted.
22173 OBJECT can also be a window, in which case the function will search
22174 the buffer displayed in that window. Passing the window instead of
22175 a buffer is preferable when the buffer is displayed in some window,
22176 because this function will then be able to correctly account for
22177 window-specific overlays, which can affect the results.
22179 Strong directional characters `L', `R', and `AL' can have their
22180 intrinsic directionality overridden by directional override
22181 control characters RLO (u+202e) and LRO (u+202d). See the
22182 function `get-char-code-property' for a way to inquire about
22183 the `bidi-class' property of a character. */)
22184 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22186 struct buffer *buf = current_buffer;
22187 struct buffer *old = buf;
22188 struct window *w = NULL;
22189 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22190 struct bidi_it itb;
22191 ptrdiff_t from_pos, to_pos, from_bpos;
22192 void *itb_data;
22194 if (!NILP (object))
22196 if (BUFFERP (object))
22197 buf = XBUFFER (object);
22198 else if (WINDOWP (object))
22200 w = decode_live_window (object);
22201 buf = XBUFFER (w->contents);
22202 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22204 else
22205 CHECK_STRING (object);
22208 if (STRINGP (object))
22210 /* Characters in unibyte strings are always treated by bidi.c as
22211 strong LTR. */
22212 if (!STRING_MULTIBYTE (object)
22213 /* When we are loading loadup.el, the character property
22214 tables needed for bidi iteration are not yet
22215 available. */
22216 || redisplay__inhibit_bidi)
22217 return Qnil;
22219 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22220 if (from_pos >= SCHARS (object))
22221 return Qnil;
22223 /* Set up the bidi iterator. */
22224 itb_data = bidi_shelve_cache ();
22225 itb.paragraph_dir = NEUTRAL_DIR;
22226 itb.string.lstring = object;
22227 itb.string.s = NULL;
22228 itb.string.schars = SCHARS (object);
22229 itb.string.bufpos = 0;
22230 itb.string.from_disp_str = false;
22231 itb.string.unibyte = false;
22232 itb.w = w;
22233 bidi_init_it (0, 0, frame_window_p, &itb);
22235 else
22237 /* Nothing this fancy can happen in unibyte buffers, or in a
22238 buffer that disabled reordering, or if FROM is at EOB. */
22239 if (NILP (BVAR (buf, bidi_display_reordering))
22240 || NILP (BVAR (buf, enable_multibyte_characters))
22241 /* When we are loading loadup.el, the character property
22242 tables needed for bidi iteration are not yet
22243 available. */
22244 || redisplay__inhibit_bidi)
22245 return Qnil;
22247 set_buffer_temp (buf);
22248 validate_region (&from, &to);
22249 from_pos = XINT (from);
22250 to_pos = XINT (to);
22251 if (from_pos >= ZV)
22252 return Qnil;
22254 /* Set up the bidi iterator. */
22255 itb_data = bidi_shelve_cache ();
22256 from_bpos = CHAR_TO_BYTE (from_pos);
22257 if (from_pos == BEGV)
22259 itb.charpos = BEGV;
22260 itb.bytepos = BEGV_BYTE;
22262 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22264 itb.charpos = from_pos;
22265 itb.bytepos = from_bpos;
22267 else
22268 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22269 -1, &itb.bytepos);
22270 itb.paragraph_dir = NEUTRAL_DIR;
22271 itb.string.s = NULL;
22272 itb.string.lstring = Qnil;
22273 itb.string.bufpos = 0;
22274 itb.string.from_disp_str = false;
22275 itb.string.unibyte = false;
22276 itb.w = w;
22277 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22280 ptrdiff_t found;
22281 do {
22282 /* For the purposes of this function, the actual base direction of
22283 the paragraph doesn't matter, so just set it to L2R. */
22284 bidi_paragraph_init (L2R, &itb, false);
22285 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22287 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22289 bidi_unshelve_cache (itb_data, false);
22290 set_buffer_temp (old);
22292 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22295 DEFUN ("move-point-visually", Fmove_point_visually,
22296 Smove_point_visually, 1, 1, 0,
22297 doc: /* Move point in the visual order in the specified DIRECTION.
22298 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22299 left.
22301 Value is the new character position of point. */)
22302 (Lisp_Object direction)
22304 struct window *w = XWINDOW (selected_window);
22305 struct buffer *b = XBUFFER (w->contents);
22306 struct glyph_row *row;
22307 int dir;
22308 Lisp_Object paragraph_dir;
22310 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22311 (!(ROW)->continued_p \
22312 && NILP ((GLYPH)->object) \
22313 && (GLYPH)->type == CHAR_GLYPH \
22314 && (GLYPH)->u.ch == ' ' \
22315 && (GLYPH)->charpos >= 0 \
22316 && !(GLYPH)->avoid_cursor_p)
22318 CHECK_NUMBER (direction);
22319 dir = XINT (direction);
22320 if (dir > 0)
22321 dir = 1;
22322 else
22323 dir = -1;
22325 /* If current matrix is up-to-date, we can use the information
22326 recorded in the glyphs, at least as long as the goal is on the
22327 screen. */
22328 if (w->window_end_valid
22329 && !windows_or_buffers_changed
22330 && b
22331 && !b->clip_changed
22332 && !b->prevent_redisplay_optimizations_p
22333 && !window_outdated (w)
22334 /* We rely below on the cursor coordinates to be up to date, but
22335 we cannot trust them if some command moved point since the
22336 last complete redisplay. */
22337 && w->last_point == BUF_PT (b)
22338 && w->cursor.vpos >= 0
22339 && w->cursor.vpos < w->current_matrix->nrows
22340 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22342 struct glyph *g = row->glyphs[TEXT_AREA];
22343 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22344 struct glyph *gpt = g + w->cursor.hpos;
22346 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22348 if (BUFFERP (g->object) && g->charpos != PT)
22350 SET_PT (g->charpos);
22351 w->cursor.vpos = -1;
22352 return make_number (PT);
22354 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22356 ptrdiff_t new_pos;
22358 if (BUFFERP (gpt->object))
22360 new_pos = PT;
22361 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22362 new_pos += (row->reversed_p ? -dir : dir);
22363 else
22364 new_pos -= (row->reversed_p ? -dir : dir);
22366 else if (BUFFERP (g->object))
22367 new_pos = g->charpos;
22368 else
22369 break;
22370 SET_PT (new_pos);
22371 w->cursor.vpos = -1;
22372 return make_number (PT);
22374 else if (ROW_GLYPH_NEWLINE_P (row, g))
22376 /* Glyphs inserted at the end of a non-empty line for
22377 positioning the cursor have zero charpos, so we must
22378 deduce the value of point by other means. */
22379 if (g->charpos > 0)
22380 SET_PT (g->charpos);
22381 else if (row->ends_at_zv_p && PT != ZV)
22382 SET_PT (ZV);
22383 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22384 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22385 else
22386 break;
22387 w->cursor.vpos = -1;
22388 return make_number (PT);
22391 if (g == e || NILP (g->object))
22393 if (row->truncated_on_left_p || row->truncated_on_right_p)
22394 goto simulate_display;
22395 if (!row->reversed_p)
22396 row += dir;
22397 else
22398 row -= dir;
22399 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22400 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22401 goto simulate_display;
22403 if (dir > 0)
22405 if (row->reversed_p && !row->continued_p)
22407 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22408 w->cursor.vpos = -1;
22409 return make_number (PT);
22411 g = row->glyphs[TEXT_AREA];
22412 e = g + row->used[TEXT_AREA];
22413 for ( ; g < e; g++)
22415 if (BUFFERP (g->object)
22416 /* Empty lines have only one glyph, which stands
22417 for the newline, and whose charpos is the
22418 buffer position of the newline. */
22419 || ROW_GLYPH_NEWLINE_P (row, g)
22420 /* When the buffer ends in a newline, the line at
22421 EOB also has one glyph, but its charpos is -1. */
22422 || (row->ends_at_zv_p
22423 && !row->reversed_p
22424 && NILP (g->object)
22425 && g->type == CHAR_GLYPH
22426 && g->u.ch == ' '))
22428 if (g->charpos > 0)
22429 SET_PT (g->charpos);
22430 else if (!row->reversed_p
22431 && row->ends_at_zv_p
22432 && PT != ZV)
22433 SET_PT (ZV);
22434 else
22435 continue;
22436 w->cursor.vpos = -1;
22437 return make_number (PT);
22441 else
22443 if (!row->reversed_p && !row->continued_p)
22445 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22446 w->cursor.vpos = -1;
22447 return make_number (PT);
22449 e = row->glyphs[TEXT_AREA];
22450 g = e + row->used[TEXT_AREA] - 1;
22451 for ( ; g >= e; g--)
22453 if (BUFFERP (g->object)
22454 || (ROW_GLYPH_NEWLINE_P (row, g)
22455 && g->charpos > 0)
22456 /* Empty R2L lines on GUI frames have the buffer
22457 position of the newline stored in the stretch
22458 glyph. */
22459 || g->type == STRETCH_GLYPH
22460 || (row->ends_at_zv_p
22461 && row->reversed_p
22462 && NILP (g->object)
22463 && g->type == CHAR_GLYPH
22464 && g->u.ch == ' '))
22466 if (g->charpos > 0)
22467 SET_PT (g->charpos);
22468 else if (row->reversed_p
22469 && row->ends_at_zv_p
22470 && PT != ZV)
22471 SET_PT (ZV);
22472 else
22473 continue;
22474 w->cursor.vpos = -1;
22475 return make_number (PT);
22482 simulate_display:
22484 /* If we wind up here, we failed to move by using the glyphs, so we
22485 need to simulate display instead. */
22487 if (b)
22488 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22489 else
22490 paragraph_dir = Qleft_to_right;
22491 if (EQ (paragraph_dir, Qright_to_left))
22492 dir = -dir;
22493 if (PT <= BEGV && dir < 0)
22494 xsignal0 (Qbeginning_of_buffer);
22495 else if (PT >= ZV && dir > 0)
22496 xsignal0 (Qend_of_buffer);
22497 else
22499 struct text_pos pt;
22500 struct it it;
22501 int pt_x, target_x, pixel_width, pt_vpos;
22502 bool at_eol_p;
22503 bool overshoot_expected = false;
22504 bool target_is_eol_p = false;
22506 /* Setup the arena. */
22507 SET_TEXT_POS (pt, PT, PT_BYTE);
22508 start_display (&it, w, pt);
22509 /* When lines are truncated, we could be called with point
22510 outside of the windows edges, in which case move_it_*
22511 functions either prematurely stop at window's edge or jump to
22512 the next screen line, whereas we rely below on our ability to
22513 reach point, in order to start from its X coordinate. So we
22514 need to disregard the window's horizontal extent in that case. */
22515 if (it.line_wrap == TRUNCATE)
22516 it.last_visible_x = DISP_INFINITY;
22518 if (it.cmp_it.id < 0
22519 && it.method == GET_FROM_STRING
22520 && it.area == TEXT_AREA
22521 && it.string_from_display_prop_p
22522 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22523 overshoot_expected = true;
22525 /* Find the X coordinate of point. We start from the beginning
22526 of this or previous line to make sure we are before point in
22527 the logical order (since the move_it_* functions can only
22528 move forward). */
22529 reseat:
22530 reseat_at_previous_visible_line_start (&it);
22531 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22532 if (IT_CHARPOS (it) != PT)
22534 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22535 -1, -1, -1, MOVE_TO_POS);
22536 /* If we missed point because the character there is
22537 displayed out of a display vector that has more than one
22538 glyph, retry expecting overshoot. */
22539 if (it.method == GET_FROM_DISPLAY_VECTOR
22540 && it.current.dpvec_index > 0
22541 && !overshoot_expected)
22543 overshoot_expected = true;
22544 goto reseat;
22546 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22547 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22549 pt_x = it.current_x;
22550 pt_vpos = it.vpos;
22551 if (dir > 0 || overshoot_expected)
22553 struct glyph_row *row = it.glyph_row;
22555 /* When point is at beginning of line, we don't have
22556 information about the glyph there loaded into struct
22557 it. Calling get_next_display_element fixes that. */
22558 if (pt_x == 0)
22559 get_next_display_element (&it);
22560 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22561 it.glyph_row = NULL;
22562 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22563 it.glyph_row = row;
22564 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22565 it, lest it will become out of sync with it's buffer
22566 position. */
22567 it.current_x = pt_x;
22569 else
22570 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22571 pixel_width = it.pixel_width;
22572 if (overshoot_expected && at_eol_p)
22573 pixel_width = 0;
22574 else if (pixel_width <= 0)
22575 pixel_width = 1;
22577 /* If there's a display string (or something similar) at point,
22578 we are actually at the glyph to the left of point, so we need
22579 to correct the X coordinate. */
22580 if (overshoot_expected)
22582 if (it.bidi_p)
22583 pt_x += pixel_width * it.bidi_it.scan_dir;
22584 else
22585 pt_x += pixel_width;
22588 /* Compute target X coordinate, either to the left or to the
22589 right of point. On TTY frames, all characters have the same
22590 pixel width of 1, so we can use that. On GUI frames we don't
22591 have an easy way of getting at the pixel width of the
22592 character to the left of point, so we use a different method
22593 of getting to that place. */
22594 if (dir > 0)
22595 target_x = pt_x + pixel_width;
22596 else
22597 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22599 /* Target X coordinate could be one line above or below the line
22600 of point, in which case we need to adjust the target X
22601 coordinate. Also, if moving to the left, we need to begin at
22602 the left edge of the point's screen line. */
22603 if (dir < 0)
22605 if (pt_x > 0)
22607 start_display (&it, w, pt);
22608 if (it.line_wrap == TRUNCATE)
22609 it.last_visible_x = DISP_INFINITY;
22610 reseat_at_previous_visible_line_start (&it);
22611 it.current_x = it.current_y = it.hpos = 0;
22612 if (pt_vpos != 0)
22613 move_it_by_lines (&it, pt_vpos);
22615 else
22617 move_it_by_lines (&it, -1);
22618 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22619 target_is_eol_p = true;
22620 /* Under word-wrap, we don't know the x coordinate of
22621 the last character displayed on the previous line,
22622 which immediately precedes the wrap point. To find
22623 out its x coordinate, we try moving to the right
22624 margin of the window, which will stop at the wrap
22625 point, and then reset target_x to point at the
22626 character that precedes the wrap point. This is not
22627 needed on GUI frames, because (see below) there we
22628 move from the left margin one grapheme cluster at a
22629 time, and stop when we hit the wrap point. */
22630 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22632 void *it_data = NULL;
22633 struct it it2;
22635 SAVE_IT (it2, it, it_data);
22636 move_it_in_display_line_to (&it, ZV, target_x,
22637 MOVE_TO_POS | MOVE_TO_X);
22638 /* If we arrived at target_x, that _is_ the last
22639 character on the previous line. */
22640 if (it.current_x != target_x)
22641 target_x = it.current_x - 1;
22642 RESTORE_IT (&it, &it2, it_data);
22646 else
22648 if (at_eol_p
22649 || (target_x >= it.last_visible_x
22650 && it.line_wrap != TRUNCATE))
22652 if (pt_x > 0)
22653 move_it_by_lines (&it, 0);
22654 move_it_by_lines (&it, 1);
22655 target_x = 0;
22659 /* Move to the target X coordinate. */
22660 /* On GUI frames, as we don't know the X coordinate of the
22661 character to the left of point, moving point to the left
22662 requires walking, one grapheme cluster at a time, until we
22663 find ourself at a place immediately to the left of the
22664 character at point. */
22665 if (FRAME_WINDOW_P (it.f) && dir < 0)
22667 struct text_pos new_pos;
22668 enum move_it_result rc = MOVE_X_REACHED;
22670 if (it.current_x == 0)
22671 get_next_display_element (&it);
22672 if (it.what == IT_COMPOSITION)
22674 new_pos.charpos = it.cmp_it.charpos;
22675 new_pos.bytepos = -1;
22677 else
22678 new_pos = it.current.pos;
22680 while (it.current_x + it.pixel_width <= target_x
22681 && (rc == MOVE_X_REACHED
22682 /* Under word-wrap, move_it_in_display_line_to
22683 stops at correct coordinates, but sometimes
22684 returns MOVE_POS_MATCH_OR_ZV. */
22685 || (it.line_wrap == WORD_WRAP
22686 && rc == MOVE_POS_MATCH_OR_ZV)))
22688 int new_x = it.current_x + it.pixel_width;
22690 /* For composed characters, we want the position of the
22691 first character in the grapheme cluster (usually, the
22692 composition's base character), whereas it.current
22693 might give us the position of the _last_ one, e.g. if
22694 the composition is rendered in reverse due to bidi
22695 reordering. */
22696 if (it.what == IT_COMPOSITION)
22698 new_pos.charpos = it.cmp_it.charpos;
22699 new_pos.bytepos = -1;
22701 else
22702 new_pos = it.current.pos;
22703 if (new_x == it.current_x)
22704 new_x++;
22705 rc = move_it_in_display_line_to (&it, ZV, new_x,
22706 MOVE_TO_POS | MOVE_TO_X);
22707 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22708 break;
22710 /* The previous position we saw in the loop is the one we
22711 want. */
22712 if (new_pos.bytepos == -1)
22713 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22714 it.current.pos = new_pos;
22716 else if (it.current_x != target_x)
22717 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22719 /* If we ended up in a display string that covers point, move to
22720 buffer position to the right in the visual order. */
22721 if (dir > 0)
22723 while (IT_CHARPOS (it) == PT)
22725 set_iterator_to_next (&it, false);
22726 if (!get_next_display_element (&it))
22727 break;
22731 /* Move point to that position. */
22732 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22735 return make_number (PT);
22737 #undef ROW_GLYPH_NEWLINE_P
22740 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22741 Sbidi_resolved_levels, 0, 1, 0,
22742 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22744 The resolved levels are produced by the Emacs bidi reordering engine
22745 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22746 read the Unicode Standard Annex 9 (UAX#9) for background information
22747 about these levels.
22749 VPOS is the zero-based number of the current window's screen line
22750 for which to produce the resolved levels. If VPOS is nil or omitted,
22751 it defaults to the screen line of point. If the window displays a
22752 header line, VPOS of zero will report on the header line, and first
22753 line of text in the window will have VPOS of 1.
22755 Value is an array of resolved levels, indexed by glyph number.
22756 Glyphs are numbered from zero starting from the beginning of the
22757 screen line, i.e. the left edge of the window for left-to-right lines
22758 and from the right edge for right-to-left lines. The resolved levels
22759 are produced only for the window's text area; text in display margins
22760 is not included.
22762 If the selected window's display is not up-to-date, or if the specified
22763 screen line does not display text, this function returns nil. It is
22764 highly recommended to bind this function to some simple key, like F8,
22765 in order to avoid these problems.
22767 This function exists mainly for testing the correctness of the
22768 Emacs UBA implementation, in particular with the test suite. */)
22769 (Lisp_Object vpos)
22771 struct window *w = XWINDOW (selected_window);
22772 struct buffer *b = XBUFFER (w->contents);
22773 int nrow;
22774 struct glyph_row *row;
22776 if (NILP (vpos))
22778 int d1, d2, d3, d4, d5;
22780 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22782 else
22784 CHECK_NUMBER_COERCE_MARKER (vpos);
22785 nrow = XINT (vpos);
22788 /* We require up-to-date glyph matrix for this window. */
22789 if (w->window_end_valid
22790 && !windows_or_buffers_changed
22791 && b
22792 && !b->clip_changed
22793 && !b->prevent_redisplay_optimizations_p
22794 && !window_outdated (w)
22795 && nrow >= 0
22796 && nrow < w->current_matrix->nrows
22797 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22798 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22800 struct glyph *g, *e, *g1;
22801 int nglyphs, i;
22802 Lisp_Object levels;
22804 if (!row->reversed_p) /* Left-to-right glyph row. */
22806 g = g1 = row->glyphs[TEXT_AREA];
22807 e = g + row->used[TEXT_AREA];
22809 /* Skip over glyphs at the start of the row that was
22810 generated by redisplay for its own needs. */
22811 while (g < e
22812 && NILP (g->object)
22813 && g->charpos < 0)
22814 g++;
22815 g1 = g;
22817 /* Count the "interesting" glyphs in this row. */
22818 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22819 nglyphs++;
22821 /* Create and fill the array. */
22822 levels = make_uninit_vector (nglyphs);
22823 for (i = 0; g1 < g; i++, g1++)
22824 ASET (levels, i, make_number (g1->resolved_level));
22826 else /* Right-to-left glyph row. */
22828 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22829 e = row->glyphs[TEXT_AREA] - 1;
22830 while (g > e
22831 && NILP (g->object)
22832 && g->charpos < 0)
22833 g--;
22834 g1 = g;
22835 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22836 nglyphs++;
22837 levels = make_uninit_vector (nglyphs);
22838 for (i = 0; g1 > g; i++, g1--)
22839 ASET (levels, i, make_number (g1->resolved_level));
22841 return levels;
22843 else
22844 return Qnil;
22849 /***********************************************************************
22850 Menu Bar
22851 ***********************************************************************/
22853 /* Redisplay the menu bar in the frame for window W.
22855 The menu bar of X frames that don't have X toolkit support is
22856 displayed in a special window W->frame->menu_bar_window.
22858 The menu bar of terminal frames is treated specially as far as
22859 glyph matrices are concerned. Menu bar lines are not part of
22860 windows, so the update is done directly on the frame matrix rows
22861 for the menu bar. */
22863 static void
22864 display_menu_bar (struct window *w)
22866 struct frame *f = XFRAME (WINDOW_FRAME (w));
22867 struct it it;
22868 Lisp_Object items;
22869 int i;
22871 /* Don't do all this for graphical frames. */
22872 #ifdef HAVE_NTGUI
22873 if (FRAME_W32_P (f))
22874 return;
22875 #endif
22876 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22877 if (FRAME_X_P (f))
22878 return;
22879 #endif
22881 #ifdef HAVE_NS
22882 if (FRAME_NS_P (f))
22883 return;
22884 #endif /* HAVE_NS */
22886 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22887 eassert (!FRAME_WINDOW_P (f));
22888 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22889 it.first_visible_x = 0;
22890 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22891 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22892 if (FRAME_WINDOW_P (f))
22894 /* Menu bar lines are displayed in the desired matrix of the
22895 dummy window menu_bar_window. */
22896 struct window *menu_w;
22897 menu_w = XWINDOW (f->menu_bar_window);
22898 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22899 MENU_FACE_ID);
22900 it.first_visible_x = 0;
22901 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22903 else
22904 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22906 /* This is a TTY frame, i.e. character hpos/vpos are used as
22907 pixel x/y. */
22908 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22909 MENU_FACE_ID);
22910 it.first_visible_x = 0;
22911 it.last_visible_x = FRAME_COLS (f);
22914 /* FIXME: This should be controlled by a user option. See the
22915 comments in redisplay_tool_bar and display_mode_line about
22916 this. */
22917 it.paragraph_embedding = L2R;
22919 /* Clear all rows of the menu bar. */
22920 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22922 struct glyph_row *row = it.glyph_row + i;
22923 clear_glyph_row (row);
22924 row->enabled_p = true;
22925 row->full_width_p = true;
22926 row->reversed_p = false;
22929 /* Display all items of the menu bar. */
22930 items = FRAME_MENU_BAR_ITEMS (it.f);
22931 for (i = 0; i < ASIZE (items); i += 4)
22933 Lisp_Object string;
22935 /* Stop at nil string. */
22936 string = AREF (items, i + 1);
22937 if (NILP (string))
22938 break;
22940 /* Remember where item was displayed. */
22941 ASET (items, i + 3, make_number (it.hpos));
22943 /* Display the item, pad with one space. */
22944 if (it.current_x < it.last_visible_x)
22945 display_string (NULL, string, Qnil, 0, 0, &it,
22946 SCHARS (string) + 1, 0, 0, -1);
22949 /* Fill out the line with spaces. */
22950 if (it.current_x < it.last_visible_x)
22951 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22953 /* Compute the total height of the lines. */
22954 compute_line_metrics (&it);
22957 /* Deep copy of a glyph row, including the glyphs. */
22958 static void
22959 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22961 struct glyph *pointers[1 + LAST_AREA];
22962 int to_used = to->used[TEXT_AREA];
22964 /* Save glyph pointers of TO. */
22965 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22967 /* Do a structure assignment. */
22968 *to = *from;
22970 /* Restore original glyph pointers of TO. */
22971 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22973 /* Copy the glyphs. */
22974 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22975 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22977 /* If we filled only part of the TO row, fill the rest with
22978 space_glyph (which will display as empty space). */
22979 if (to_used > from->used[TEXT_AREA])
22980 fill_up_frame_row_with_spaces (to, to_used);
22983 /* Display one menu item on a TTY, by overwriting the glyphs in the
22984 frame F's desired glyph matrix with glyphs produced from the menu
22985 item text. Called from term.c to display TTY drop-down menus one
22986 item at a time.
22988 ITEM_TEXT is the menu item text as a C string.
22990 FACE_ID is the face ID to be used for this menu item. FACE_ID
22991 could specify one of 3 faces: a face for an enabled item, a face
22992 for a disabled item, or a face for a selected item.
22994 X and Y are coordinates of the first glyph in the frame's desired
22995 matrix to be overwritten by the menu item. Since this is a TTY, Y
22996 is the zero-based number of the glyph row and X is the zero-based
22997 glyph number in the row, starting from left, where to start
22998 displaying the item.
23000 SUBMENU means this menu item drops down a submenu, which
23001 should be indicated by displaying a proper visual cue after the
23002 item text. */
23004 void
23005 display_tty_menu_item (const char *item_text, int width, int face_id,
23006 int x, int y, bool submenu)
23008 struct it it;
23009 struct frame *f = SELECTED_FRAME ();
23010 struct window *w = XWINDOW (f->selected_window);
23011 struct glyph_row *row;
23012 size_t item_len = strlen (item_text);
23014 eassert (FRAME_TERMCAP_P (f));
23016 /* Don't write beyond the matrix's last row. This can happen for
23017 TTY screens that are not high enough to show the entire menu.
23018 (This is actually a bit of defensive programming, as
23019 tty_menu_display already limits the number of menu items to one
23020 less than the number of screen lines.) */
23021 if (y >= f->desired_matrix->nrows)
23022 return;
23024 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23025 it.first_visible_x = 0;
23026 it.last_visible_x = FRAME_COLS (f) - 1;
23027 row = it.glyph_row;
23028 /* Start with the row contents from the current matrix. */
23029 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23030 bool saved_width = row->full_width_p;
23031 row->full_width_p = true;
23032 bool saved_reversed = row->reversed_p;
23033 row->reversed_p = false;
23034 row->enabled_p = true;
23036 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23037 desired face. */
23038 eassert (x < f->desired_matrix->matrix_w);
23039 it.current_x = it.hpos = x;
23040 it.current_y = it.vpos = y;
23041 int saved_used = row->used[TEXT_AREA];
23042 bool saved_truncated = row->truncated_on_right_p;
23043 row->used[TEXT_AREA] = x;
23044 it.face_id = face_id;
23045 it.line_wrap = TRUNCATE;
23047 /* FIXME: This should be controlled by a user option. See the
23048 comments in redisplay_tool_bar and display_mode_line about this.
23049 Also, if paragraph_embedding could ever be R2L, changes will be
23050 needed to avoid shifting to the right the row characters in
23051 term.c:append_glyph. */
23052 it.paragraph_embedding = L2R;
23054 /* Pad with a space on the left. */
23055 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23056 width--;
23057 /* Display the menu item, pad with spaces to WIDTH. */
23058 if (submenu)
23060 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23061 item_len, 0, FRAME_COLS (f) - 1, -1);
23062 width -= item_len;
23063 /* Indicate with " >" that there's a submenu. */
23064 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23065 FRAME_COLS (f) - 1, -1);
23067 else
23068 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23069 width, 0, FRAME_COLS (f) - 1, -1);
23071 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23072 row->truncated_on_right_p = saved_truncated;
23073 row->hash = row_hash (row);
23074 row->full_width_p = saved_width;
23075 row->reversed_p = saved_reversed;
23078 /***********************************************************************
23079 Mode Line
23080 ***********************************************************************/
23082 /* Redisplay mode lines in the window tree whose root is WINDOW.
23083 If FORCE, redisplay mode lines unconditionally.
23084 Otherwise, redisplay only mode lines that are garbaged. Value is
23085 the number of windows whose mode lines were redisplayed. */
23087 static int
23088 redisplay_mode_lines (Lisp_Object window, bool force)
23090 int nwindows = 0;
23092 while (!NILP (window))
23094 struct window *w = XWINDOW (window);
23096 if (WINDOWP (w->contents))
23097 nwindows += redisplay_mode_lines (w->contents, force);
23098 else if (force
23099 || FRAME_GARBAGED_P (XFRAME (w->frame))
23100 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23102 struct text_pos lpoint;
23103 struct buffer *old = current_buffer;
23105 /* Set the window's buffer for the mode line display. */
23106 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23107 set_buffer_internal_1 (XBUFFER (w->contents));
23109 /* Point refers normally to the selected window. For any
23110 other window, set up appropriate value. */
23111 if (!EQ (window, selected_window))
23113 struct text_pos pt;
23115 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23116 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23119 /* Display mode lines. */
23120 clear_glyph_matrix (w->desired_matrix);
23121 if (display_mode_lines (w))
23122 ++nwindows;
23124 /* Restore old settings. */
23125 set_buffer_internal_1 (old);
23126 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23129 window = w->next;
23132 return nwindows;
23136 /* Display the mode and/or header line of window W. Value is the
23137 sum number of mode lines and header lines displayed. */
23139 static int
23140 display_mode_lines (struct window *w)
23142 Lisp_Object old_selected_window = selected_window;
23143 Lisp_Object old_selected_frame = selected_frame;
23144 Lisp_Object new_frame = w->frame;
23145 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23146 int n = 0;
23148 selected_frame = new_frame;
23149 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23150 or window's point, then we'd need select_window_1 here as well. */
23151 XSETWINDOW (selected_window, w);
23152 XFRAME (new_frame)->selected_window = selected_window;
23154 /* These will be set while the mode line specs are processed. */
23155 line_number_displayed = false;
23156 w->column_number_displayed = -1;
23158 if (window_wants_mode_line (w))
23160 Lisp_Object window_mode_line_format
23161 = window_parameter (w, Qmode_line_format);
23163 struct window *sel_w = XWINDOW (old_selected_window);
23165 /* Select mode line face based on the real selected window. */
23166 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23167 NILP (window_mode_line_format)
23168 ? BVAR (current_buffer, mode_line_format)
23169 : window_mode_line_format);
23170 ++n;
23173 if (window_wants_header_line (w))
23175 Lisp_Object window_header_line_format
23176 = window_parameter (w, Qheader_line_format);
23178 display_mode_line (w, HEADER_LINE_FACE_ID,
23179 NILP (window_header_line_format)
23180 ? BVAR (current_buffer, header_line_format)
23181 : window_header_line_format);
23182 ++n;
23185 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23186 selected_frame = old_selected_frame;
23187 selected_window = old_selected_window;
23188 if (n > 0)
23189 w->must_be_updated_p = true;
23190 return n;
23194 /* Display mode or header line of window W. FACE_ID specifies which
23195 line to display; it is either MODE_LINE_FACE_ID or
23196 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23197 display. Value is the pixel height of the mode/header line
23198 displayed. */
23200 static int
23201 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23203 struct it it;
23204 struct face *face;
23205 ptrdiff_t count = SPECPDL_INDEX ();
23207 init_iterator (&it, w, -1, -1, NULL, face_id);
23208 /* Don't extend on a previously drawn mode-line.
23209 This may happen if called from pos_visible_p. */
23210 it.glyph_row->enabled_p = false;
23211 prepare_desired_row (w, it.glyph_row, true);
23213 it.glyph_row->mode_line_p = true;
23215 /* FIXME: This should be controlled by a user option. But
23216 supporting such an option is not trivial, since the mode line is
23217 made up of many separate strings. */
23218 it.paragraph_embedding = L2R;
23220 record_unwind_protect (unwind_format_mode_line,
23221 format_mode_line_unwind_data (NULL, NULL,
23222 Qnil, false));
23224 mode_line_target = MODE_LINE_DISPLAY;
23226 /* Temporarily make frame's keyboard the current kboard so that
23227 kboard-local variables in the mode_line_format will get the right
23228 values. */
23229 push_kboard (FRAME_KBOARD (it.f));
23230 record_unwind_save_match_data ();
23231 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23232 pop_kboard ();
23234 unbind_to (count, Qnil);
23236 /* Fill up with spaces. */
23237 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23239 compute_line_metrics (&it);
23240 it.glyph_row->full_width_p = true;
23241 it.glyph_row->continued_p = false;
23242 it.glyph_row->truncated_on_left_p = false;
23243 it.glyph_row->truncated_on_right_p = false;
23245 /* Make a 3D mode-line have a shadow at its right end. */
23246 face = FACE_FROM_ID (it.f, face_id);
23247 extend_face_to_end_of_line (&it);
23248 if (face->box != FACE_NO_BOX)
23250 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23251 + it.glyph_row->used[TEXT_AREA] - 1);
23252 last->right_box_line_p = true;
23255 return it.glyph_row->height;
23258 /* Move element ELT in LIST to the front of LIST.
23259 Return the updated list. */
23261 static Lisp_Object
23262 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23264 register Lisp_Object tail, prev;
23265 register Lisp_Object tem;
23267 tail = list;
23268 prev = Qnil;
23269 while (CONSP (tail))
23271 tem = XCAR (tail);
23273 if (EQ (elt, tem))
23275 /* Splice out the link TAIL. */
23276 if (NILP (prev))
23277 list = XCDR (tail);
23278 else
23279 Fsetcdr (prev, XCDR (tail));
23281 /* Now make it the first. */
23282 Fsetcdr (tail, list);
23283 return tail;
23285 else
23286 prev = tail;
23287 tail = XCDR (tail);
23288 maybe_quit ();
23291 /* Not found--return unchanged LIST. */
23292 return list;
23295 /* Contribute ELT to the mode line for window IT->w. How it
23296 translates into text depends on its data type.
23298 IT describes the display environment in which we display, as usual.
23300 DEPTH is the depth in recursion. It is used to prevent
23301 infinite recursion here.
23303 FIELD_WIDTH is the number of characters the display of ELT should
23304 occupy in the mode line, and PRECISION is the maximum number of
23305 characters to display from ELT's representation. See
23306 display_string for details.
23308 Returns the hpos of the end of the text generated by ELT.
23310 PROPS is a property list to add to any string we encounter.
23312 If RISKY, remove (disregard) any properties in any string
23313 we encounter, and ignore :eval and :propertize.
23315 The global variable `mode_line_target' determines whether the
23316 output is passed to `store_mode_line_noprop',
23317 `store_mode_line_string', or `display_string'. */
23319 static int
23320 display_mode_element (struct it *it, int depth, int field_width, int precision,
23321 Lisp_Object elt, Lisp_Object props, bool risky)
23323 int n = 0, field, prec;
23324 bool literal = false;
23326 tail_recurse:
23327 if (depth > 100)
23328 elt = build_string ("*too-deep*");
23330 depth++;
23332 switch (XTYPE (elt))
23334 case Lisp_String:
23336 /* A string: output it and check for %-constructs within it. */
23337 unsigned char c;
23338 ptrdiff_t offset = 0;
23340 if (SCHARS (elt) > 0
23341 && (!NILP (props) || risky))
23343 Lisp_Object oprops, aelt;
23344 oprops = Ftext_properties_at (make_number (0), elt);
23346 /* If the starting string's properties are not what
23347 we want, translate the string. Also, if the string
23348 is risky, do that anyway. */
23350 if (NILP (Fequal (props, oprops)) || risky)
23352 /* If the starting string has properties,
23353 merge the specified ones onto the existing ones. */
23354 if (! NILP (oprops) && !risky)
23356 Lisp_Object tem;
23358 oprops = Fcopy_sequence (oprops);
23359 tem = props;
23360 while (CONSP (tem))
23362 oprops = Fplist_put (oprops, XCAR (tem),
23363 XCAR (XCDR (tem)));
23364 tem = XCDR (XCDR (tem));
23366 props = oprops;
23369 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23370 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23372 /* AELT is what we want. Move it to the front
23373 without consing. */
23374 elt = XCAR (aelt);
23375 mode_line_proptrans_alist
23376 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23378 else
23380 Lisp_Object tem;
23382 /* If AELT has the wrong props, it is useless.
23383 so get rid of it. */
23384 if (! NILP (aelt))
23385 mode_line_proptrans_alist
23386 = Fdelq (aelt, mode_line_proptrans_alist);
23388 elt = Fcopy_sequence (elt);
23389 Fset_text_properties (make_number (0), Flength (elt),
23390 props, elt);
23391 /* Add this item to mode_line_proptrans_alist. */
23392 mode_line_proptrans_alist
23393 = Fcons (Fcons (elt, props),
23394 mode_line_proptrans_alist);
23395 /* Truncate mode_line_proptrans_alist
23396 to at most 50 elements. */
23397 tem = Fnthcdr (make_number (50),
23398 mode_line_proptrans_alist);
23399 if (! NILP (tem))
23400 XSETCDR (tem, Qnil);
23405 offset = 0;
23407 if (literal)
23409 prec = precision - n;
23410 switch (mode_line_target)
23412 case MODE_LINE_NOPROP:
23413 case MODE_LINE_TITLE:
23414 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23415 break;
23416 case MODE_LINE_STRING:
23417 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23418 break;
23419 case MODE_LINE_DISPLAY:
23420 n += display_string (NULL, elt, Qnil, 0, 0, it,
23421 0, prec, 0, STRING_MULTIBYTE (elt));
23422 break;
23425 break;
23428 /* Handle the non-literal case. */
23430 while ((precision <= 0 || n < precision)
23431 && SREF (elt, offset) != 0
23432 && (mode_line_target != MODE_LINE_DISPLAY
23433 || it->current_x < it->last_visible_x))
23435 ptrdiff_t last_offset = offset;
23437 /* Advance to end of string or next format specifier. */
23438 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23441 if (offset - 1 != last_offset)
23443 ptrdiff_t nchars, nbytes;
23445 /* Output to end of string or up to '%'. Field width
23446 is length of string. Don't output more than
23447 PRECISION allows us. */
23448 offset--;
23450 prec = c_string_width (SDATA (elt) + last_offset,
23451 offset - last_offset, precision - n,
23452 &nchars, &nbytes);
23454 switch (mode_line_target)
23456 case MODE_LINE_NOPROP:
23457 case MODE_LINE_TITLE:
23458 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23459 break;
23460 case MODE_LINE_STRING:
23462 ptrdiff_t bytepos = last_offset;
23463 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23464 ptrdiff_t endpos = (precision <= 0
23465 ? string_byte_to_char (elt, offset)
23466 : charpos + nchars);
23467 Lisp_Object mode_string
23468 = Fsubstring (elt, make_number (charpos),
23469 make_number (endpos));
23470 n += store_mode_line_string (NULL, mode_string, false,
23471 0, 0, Qnil);
23473 break;
23474 case MODE_LINE_DISPLAY:
23476 ptrdiff_t bytepos = last_offset;
23477 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23479 if (precision <= 0)
23480 nchars = string_byte_to_char (elt, offset) - charpos;
23481 n += display_string (NULL, elt, Qnil, 0, charpos,
23482 it, 0, nchars, 0,
23483 STRING_MULTIBYTE (elt));
23485 break;
23488 else /* c == '%' */
23490 ptrdiff_t percent_position = offset;
23492 /* Get the specified minimum width. Zero means
23493 don't pad. */
23494 field = 0;
23495 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23496 field = field * 10 + c - '0';
23498 /* Don't pad beyond the total padding allowed. */
23499 if (field_width - n > 0 && field > field_width - n)
23500 field = field_width - n;
23502 /* Note that either PRECISION <= 0 or N < PRECISION. */
23503 prec = precision - n;
23505 if (c == 'M')
23506 n += display_mode_element (it, depth, field, prec,
23507 Vglobal_mode_string, props,
23508 risky);
23509 else if (c != 0)
23511 bool multibyte;
23512 ptrdiff_t bytepos, charpos;
23513 const char *spec;
23514 Lisp_Object string;
23516 bytepos = percent_position;
23517 charpos = (STRING_MULTIBYTE (elt)
23518 ? string_byte_to_char (elt, bytepos)
23519 : bytepos);
23520 spec = decode_mode_spec (it->w, c, field, &string);
23521 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23523 switch (mode_line_target)
23525 case MODE_LINE_NOPROP:
23526 case MODE_LINE_TITLE:
23527 n += store_mode_line_noprop (spec, field, prec);
23528 break;
23529 case MODE_LINE_STRING:
23531 Lisp_Object tem = build_string (spec);
23532 props = Ftext_properties_at (make_number (charpos), elt);
23533 /* Should only keep face property in props */
23534 n += store_mode_line_string (NULL, tem, false,
23535 field, prec, props);
23537 break;
23538 case MODE_LINE_DISPLAY:
23540 int nglyphs_before, nwritten;
23542 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23543 nwritten = display_string (spec, string, elt,
23544 charpos, 0, it,
23545 field, prec, 0,
23546 multibyte);
23548 /* Assign to the glyphs written above the
23549 string where the `%x' came from, position
23550 of the `%'. */
23551 if (nwritten > 0)
23553 struct glyph *glyph
23554 = (it->glyph_row->glyphs[TEXT_AREA]
23555 + nglyphs_before);
23556 int i;
23558 for (i = 0; i < nwritten; ++i)
23560 glyph[i].object = elt;
23561 glyph[i].charpos = charpos;
23564 n += nwritten;
23567 break;
23570 else /* c == 0 */
23571 break;
23575 break;
23577 case Lisp_Symbol:
23578 /* A symbol: process the value of the symbol recursively
23579 as if it appeared here directly. Avoid error if symbol void.
23580 Special case: if value of symbol is a string, output the string
23581 literally. */
23583 register Lisp_Object tem;
23585 /* If the variable is not marked as risky to set
23586 then its contents are risky to use. */
23587 if (NILP (Fget (elt, Qrisky_local_variable)))
23588 risky = true;
23590 tem = Fboundp (elt);
23591 if (!NILP (tem))
23593 tem = Fsymbol_value (elt);
23594 /* If value is a string, output that string literally:
23595 don't check for % within it. */
23596 if (STRINGP (tem))
23597 literal = true;
23599 if (!EQ (tem, elt))
23601 /* Give up right away for nil or t. */
23602 elt = tem;
23603 goto tail_recurse;
23607 break;
23609 case Lisp_Cons:
23611 register Lisp_Object car, tem;
23613 /* A cons cell: five distinct cases.
23614 If first element is :eval or :propertize, do something special.
23615 If first element is a string or a cons, process all the elements
23616 and effectively concatenate them.
23617 If first element is a negative number, truncate displaying cdr to
23618 at most that many characters. If positive, pad (with spaces)
23619 to at least that many characters.
23620 If first element is a symbol, process the cadr or caddr recursively
23621 according to whether the symbol's value is non-nil or nil. */
23622 car = XCAR (elt);
23623 if (EQ (car, QCeval))
23625 /* An element of the form (:eval FORM) means evaluate FORM
23626 and use the result as mode line elements. */
23628 if (risky)
23629 break;
23631 if (CONSP (XCDR (elt)))
23633 Lisp_Object spec;
23634 spec = safe__eval (true, XCAR (XCDR (elt)));
23635 n += display_mode_element (it, depth, field_width - n,
23636 precision - n, spec, props,
23637 risky);
23640 else if (EQ (car, QCpropertize))
23642 /* An element of the form (:propertize ELT PROPS...)
23643 means display ELT but applying properties PROPS. */
23645 if (risky)
23646 break;
23648 if (CONSP (XCDR (elt)))
23649 n += display_mode_element (it, depth, field_width - n,
23650 precision - n, XCAR (XCDR (elt)),
23651 XCDR (XCDR (elt)), risky);
23653 else if (SYMBOLP (car))
23655 tem = Fboundp (car);
23656 elt = XCDR (elt);
23657 if (!CONSP (elt))
23658 goto invalid;
23659 /* elt is now the cdr, and we know it is a cons cell.
23660 Use its car if CAR has a non-nil value. */
23661 if (!NILP (tem))
23663 tem = Fsymbol_value (car);
23664 if (!NILP (tem))
23666 elt = XCAR (elt);
23667 goto tail_recurse;
23670 /* Symbol's value is nil (or symbol is unbound)
23671 Get the cddr of the original list
23672 and if possible find the caddr and use that. */
23673 elt = XCDR (elt);
23674 if (NILP (elt))
23675 break;
23676 else if (!CONSP (elt))
23677 goto invalid;
23678 elt = XCAR (elt);
23679 goto tail_recurse;
23681 else if (INTEGERP (car))
23683 register int lim = XINT (car);
23684 elt = XCDR (elt);
23685 if (lim < 0)
23687 /* Negative int means reduce maximum width. */
23688 if (precision <= 0)
23689 precision = -lim;
23690 else
23691 precision = min (precision, -lim);
23693 else if (lim > 0)
23695 /* Padding specified. Don't let it be more than
23696 current maximum. */
23697 if (precision > 0)
23698 lim = min (precision, lim);
23700 /* If that's more padding than already wanted, queue it.
23701 But don't reduce padding already specified even if
23702 that is beyond the current truncation point. */
23703 field_width = max (lim, field_width);
23705 goto tail_recurse;
23707 else if (STRINGP (car) || CONSP (car))
23708 FOR_EACH_TAIL_SAFE (elt)
23710 if (0 < precision && precision <= n)
23711 break;
23712 n += display_mode_element (it, depth,
23713 /* Pad after only the last
23714 list element. */
23715 (! CONSP (XCDR (elt))
23716 ? field_width - n
23717 : 0),
23718 precision - n, XCAR (elt),
23719 props, risky);
23722 break;
23724 default:
23725 invalid:
23726 elt = build_string ("*invalid*");
23727 goto tail_recurse;
23730 /* Pad to FIELD_WIDTH. */
23731 if (field_width > 0 && n < field_width)
23733 switch (mode_line_target)
23735 case MODE_LINE_NOPROP:
23736 case MODE_LINE_TITLE:
23737 n += store_mode_line_noprop ("", field_width - n, 0);
23738 break;
23739 case MODE_LINE_STRING:
23740 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23741 Qnil);
23742 break;
23743 case MODE_LINE_DISPLAY:
23744 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23745 0, 0, 0);
23746 break;
23750 return n;
23753 /* Store a mode-line string element in mode_line_string_list.
23755 If STRING is non-null, display that C string. Otherwise, the Lisp
23756 string LISP_STRING is displayed.
23758 FIELD_WIDTH is the minimum number of output glyphs to produce.
23759 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23760 with spaces. FIELD_WIDTH <= 0 means don't pad.
23762 PRECISION is the maximum number of characters to output from
23763 STRING. PRECISION <= 0 means don't truncate the string.
23765 If COPY_STRING, make a copy of LISP_STRING before adding
23766 properties to the string.
23768 PROPS are the properties to add to the string.
23769 The mode_line_string_face face property is always added to the string.
23772 static int
23773 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23774 bool copy_string,
23775 int field_width, int precision, Lisp_Object props)
23777 ptrdiff_t len;
23778 int n = 0;
23780 if (string != NULL)
23782 len = strlen (string);
23783 if (precision > 0 && len > precision)
23784 len = precision;
23785 lisp_string = make_string (string, len);
23786 if (NILP (props))
23787 props = mode_line_string_face_prop;
23788 else if (!NILP (mode_line_string_face))
23790 Lisp_Object face = Fplist_get (props, Qface);
23791 props = Fcopy_sequence (props);
23792 if (NILP (face))
23793 face = mode_line_string_face;
23794 else
23795 face = list2 (face, mode_line_string_face);
23796 props = Fplist_put (props, Qface, face);
23798 Fadd_text_properties (make_number (0), make_number (len),
23799 props, lisp_string);
23801 else
23803 len = XFASTINT (Flength (lisp_string));
23804 if (precision > 0 && len > precision)
23806 len = precision;
23807 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23808 precision = -1;
23810 if (!NILP (mode_line_string_face))
23812 Lisp_Object face;
23813 if (NILP (props))
23814 props = Ftext_properties_at (make_number (0), lisp_string);
23815 face = Fplist_get (props, Qface);
23816 if (NILP (face))
23817 face = mode_line_string_face;
23818 else
23819 face = list2 (face, mode_line_string_face);
23820 props = list2 (Qface, face);
23821 if (copy_string)
23822 lisp_string = Fcopy_sequence (lisp_string);
23824 if (!NILP (props))
23825 Fadd_text_properties (make_number (0), make_number (len),
23826 props, lisp_string);
23829 if (len > 0)
23831 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23832 n += len;
23835 if (field_width > len)
23837 field_width -= len;
23838 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23839 if (!NILP (props))
23840 Fadd_text_properties (make_number (0), make_number (field_width),
23841 props, lisp_string);
23842 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23843 n += field_width;
23846 return n;
23850 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23851 1, 4, 0,
23852 doc: /* Format a string out of a mode line format specification.
23853 First arg FORMAT specifies the mode line format (see `mode-line-format'
23854 for details) to use.
23856 By default, the format is evaluated for the currently selected window.
23858 Optional second arg FACE specifies the face property to put on all
23859 characters for which no face is specified. The value nil means the
23860 default face. The value t means whatever face the window's mode line
23861 currently uses (either `mode-line' or `mode-line-inactive',
23862 depending on whether the window is the selected window or not).
23863 An integer value means the value string has no text
23864 properties.
23866 Optional third and fourth args WINDOW and BUFFER specify the window
23867 and buffer to use as the context for the formatting (defaults
23868 are the selected window and the WINDOW's buffer). */)
23869 (Lisp_Object format, Lisp_Object face,
23870 Lisp_Object window, Lisp_Object buffer)
23872 struct it it;
23873 int len;
23874 struct window *w;
23875 struct buffer *old_buffer = NULL;
23876 int face_id;
23877 bool no_props = INTEGERP (face);
23878 ptrdiff_t count = SPECPDL_INDEX ();
23879 Lisp_Object str;
23880 int string_start = 0;
23882 w = decode_any_window (window);
23883 XSETWINDOW (window, w);
23885 if (NILP (buffer))
23886 buffer = w->contents;
23887 CHECK_BUFFER (buffer);
23889 /* Make formatting the modeline a non-op when noninteractive, otherwise
23890 there will be problems later caused by a partially initialized frame. */
23891 if (NILP (format) || noninteractive)
23892 return empty_unibyte_string;
23894 if (no_props)
23895 face = Qnil;
23897 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23898 : EQ (face, Qt) ? (EQ (window, selected_window)
23899 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23900 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23901 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23902 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23903 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23904 : DEFAULT_FACE_ID;
23906 old_buffer = current_buffer;
23908 /* Save things including mode_line_proptrans_alist,
23909 and set that to nil so that we don't alter the outer value. */
23910 record_unwind_protect (unwind_format_mode_line,
23911 format_mode_line_unwind_data
23912 (XFRAME (WINDOW_FRAME (w)),
23913 old_buffer, selected_window, true));
23914 mode_line_proptrans_alist = Qnil;
23916 Fselect_window (window, Qt);
23917 set_buffer_internal_1 (XBUFFER (buffer));
23919 init_iterator (&it, w, -1, -1, NULL, face_id);
23921 if (no_props)
23923 mode_line_target = MODE_LINE_NOPROP;
23924 mode_line_string_face_prop = Qnil;
23925 mode_line_string_list = Qnil;
23926 string_start = MODE_LINE_NOPROP_LEN (0);
23928 else
23930 mode_line_target = MODE_LINE_STRING;
23931 mode_line_string_list = Qnil;
23932 mode_line_string_face = face;
23933 mode_line_string_face_prop
23934 = NILP (face) ? Qnil : list2 (Qface, face);
23937 push_kboard (FRAME_KBOARD (it.f));
23938 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23939 pop_kboard ();
23941 if (no_props)
23943 len = MODE_LINE_NOPROP_LEN (string_start);
23944 str = make_string (mode_line_noprop_buf + string_start, len);
23946 else
23948 mode_line_string_list = Fnreverse (mode_line_string_list);
23949 str = Fmapconcat (Qidentity, mode_line_string_list,
23950 empty_unibyte_string);
23953 unbind_to (count, Qnil);
23954 return str;
23957 /* Write a null-terminated, right justified decimal representation of
23958 the positive integer D to BUF using a minimal field width WIDTH. */
23960 static void
23961 pint2str (register char *buf, register int width, register ptrdiff_t d)
23963 register char *p = buf;
23965 if (d <= 0)
23966 *p++ = '0';
23967 else
23969 while (d > 0)
23971 *p++ = d % 10 + '0';
23972 d /= 10;
23976 for (width -= (int) (p - buf); width > 0; --width)
23977 *p++ = ' ';
23978 *p-- = '\0';
23979 while (p > buf)
23981 d = *buf;
23982 *buf++ = *p;
23983 *p-- = d;
23987 /* Write a null-terminated, right justified decimal and "human
23988 readable" representation of the nonnegative integer D to BUF using
23989 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23991 static const char power_letter[] =
23993 0, /* no letter */
23994 'k', /* kilo */
23995 'M', /* mega */
23996 'G', /* giga */
23997 'T', /* tera */
23998 'P', /* peta */
23999 'E', /* exa */
24000 'Z', /* zetta */
24001 'Y' /* yotta */
24004 static void
24005 pint2hrstr (char *buf, int width, ptrdiff_t d)
24007 /* We aim to represent the nonnegative integer D as
24008 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24009 ptrdiff_t quotient = d;
24010 int remainder = 0;
24011 /* -1 means: do not use TENTHS. */
24012 int tenths = -1;
24013 int exponent = 0;
24015 /* Length of QUOTIENT.TENTHS as a string. */
24016 int length;
24018 char * psuffix;
24019 char * p;
24021 if (quotient >= 1000)
24023 /* Scale to the appropriate EXPONENT. */
24026 remainder = quotient % 1000;
24027 quotient /= 1000;
24028 exponent++;
24030 while (quotient >= 1000);
24032 /* Round to nearest and decide whether to use TENTHS or not. */
24033 if (quotient <= 9)
24035 tenths = remainder / 100;
24036 if (remainder % 100 >= 50)
24038 if (tenths < 9)
24039 tenths++;
24040 else
24042 quotient++;
24043 if (quotient == 10)
24044 tenths = -1;
24045 else
24046 tenths = 0;
24050 else
24051 if (remainder >= 500)
24053 if (quotient < 999)
24054 quotient++;
24055 else
24057 quotient = 1;
24058 exponent++;
24059 tenths = 0;
24064 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24065 if (tenths == -1 && quotient <= 99)
24066 if (quotient <= 9)
24067 length = 1;
24068 else
24069 length = 2;
24070 else
24071 length = 3;
24072 p = psuffix = buf + max (width, length);
24074 /* Print EXPONENT. */
24075 *psuffix++ = power_letter[exponent];
24076 *psuffix = '\0';
24078 /* Print TENTHS. */
24079 if (tenths >= 0)
24081 *--p = '0' + tenths;
24082 *--p = '.';
24085 /* Print QUOTIENT. */
24088 int digit = quotient % 10;
24089 *--p = '0' + digit;
24091 while ((quotient /= 10) != 0);
24093 /* Print leading spaces. */
24094 while (buf < p)
24095 *--p = ' ';
24098 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24099 If EOL_FLAG, set also a mnemonic character for end-of-line
24100 type of CODING_SYSTEM. Return updated pointer into BUF. */
24102 static unsigned char invalid_eol_type[] = "(*invalid*)";
24104 static char *
24105 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24107 Lisp_Object val;
24108 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24109 const unsigned char *eol_str;
24110 int eol_str_len;
24111 /* The EOL conversion we are using. */
24112 Lisp_Object eoltype;
24114 val = CODING_SYSTEM_SPEC (coding_system);
24115 eoltype = Qnil;
24117 if (!VECTORP (val)) /* Not yet decided. */
24119 *buf++ = multibyte ? '-' : ' ';
24120 if (eol_flag)
24121 eoltype = eol_mnemonic_undecided;
24122 /* Don't mention EOL conversion if it isn't decided. */
24124 else
24126 Lisp_Object attrs;
24127 Lisp_Object eolvalue;
24129 attrs = AREF (val, 0);
24130 eolvalue = AREF (val, 2);
24132 *buf++ = multibyte
24133 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24134 : ' ';
24136 if (eol_flag)
24138 /* The EOL conversion that is normal on this system. */
24140 if (NILP (eolvalue)) /* Not yet decided. */
24141 eoltype = eol_mnemonic_undecided;
24142 else if (VECTORP (eolvalue)) /* Not yet decided. */
24143 eoltype = eol_mnemonic_undecided;
24144 else /* eolvalue is Qunix, Qdos, or Qmac. */
24145 eoltype = (EQ (eolvalue, Qunix)
24146 ? eol_mnemonic_unix
24147 : EQ (eolvalue, Qdos)
24148 ? eol_mnemonic_dos : eol_mnemonic_mac);
24152 if (eol_flag)
24154 /* Mention the EOL conversion if it is not the usual one. */
24155 if (STRINGP (eoltype))
24157 eol_str = SDATA (eoltype);
24158 eol_str_len = SBYTES (eoltype);
24160 else if (CHARACTERP (eoltype))
24162 int c = XFASTINT (eoltype);
24163 return buf + CHAR_STRING (c, (unsigned char *) buf);
24165 else
24167 eol_str = invalid_eol_type;
24168 eol_str_len = sizeof (invalid_eol_type) - 1;
24170 memcpy (buf, eol_str, eol_str_len);
24171 buf += eol_str_len;
24174 return buf;
24177 /* Return the approximate percentage N is of D (rounding upward), or 99,
24178 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24180 static int
24181 percent99 (ptrdiff_t n, ptrdiff_t d)
24183 int percent = (d - 1 + 100.0 * n) / d;
24184 return min (percent, 99);
24187 /* Return a string for the output of a mode line %-spec for window W,
24188 generated by character C. FIELD_WIDTH > 0 means pad the string
24189 returned with spaces to that value. Return a Lisp string in
24190 *STRING if the resulting string is taken from that Lisp string.
24192 Note we operate on the current buffer for most purposes. */
24194 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24196 static const char *
24197 decode_mode_spec (struct window *w, register int c, int field_width,
24198 Lisp_Object *string)
24200 Lisp_Object obj;
24201 struct frame *f = XFRAME (WINDOW_FRAME (w));
24202 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24203 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24204 produce strings from numerical values, so limit preposterously
24205 large values of FIELD_WIDTH to avoid overrunning the buffer's
24206 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24207 bytes plus the terminating null. */
24208 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24209 struct buffer *b = current_buffer;
24211 obj = Qnil;
24212 *string = Qnil;
24214 switch (c)
24216 case '*':
24217 if (!NILP (BVAR (b, read_only)))
24218 return "%";
24219 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24220 return "*";
24221 return "-";
24223 case '+':
24224 /* This differs from %* only for a modified read-only buffer. */
24225 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24226 return "*";
24227 if (!NILP (BVAR (b, read_only)))
24228 return "%";
24229 return "-";
24231 case '&':
24232 /* This differs from %* in ignoring read-only-ness. */
24233 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24234 return "*";
24235 return "-";
24237 case '%':
24238 return "%";
24240 case '[':
24242 int i;
24243 char *p;
24245 if (command_loop_level > 5)
24246 return "[[[... ";
24247 p = decode_mode_spec_buf;
24248 for (i = 0; i < command_loop_level; i++)
24249 *p++ = '[';
24250 *p = 0;
24251 return decode_mode_spec_buf;
24254 case ']':
24256 int i;
24257 char *p;
24259 if (command_loop_level > 5)
24260 return " ...]]]";
24261 p = decode_mode_spec_buf;
24262 for (i = 0; i < command_loop_level; i++)
24263 *p++ = ']';
24264 *p = 0;
24265 return decode_mode_spec_buf;
24268 case '-':
24270 register int i;
24272 /* Let lots_of_dashes be a string of infinite length. */
24273 if (mode_line_target == MODE_LINE_NOPROP
24274 || mode_line_target == MODE_LINE_STRING)
24275 return "--";
24276 if (field_width <= 0
24277 || field_width > sizeof (lots_of_dashes))
24279 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24280 decode_mode_spec_buf[i] = '-';
24281 decode_mode_spec_buf[i] = '\0';
24282 return decode_mode_spec_buf;
24284 else
24285 return lots_of_dashes;
24288 case 'b':
24289 obj = BVAR (b, name);
24290 break;
24292 case 'c':
24293 case 'C':
24294 /* %c, %C, and %l are ignored in `frame-title-format'.
24295 (In redisplay_internal, the frame title is drawn _before_ the
24296 windows are updated, so the stuff which depends on actual
24297 window contents (such as %l) may fail to render properly, or
24298 even crash emacs.) */
24299 if (mode_line_target == MODE_LINE_TITLE)
24300 return "";
24301 else
24303 ptrdiff_t col = current_column ();
24304 int disp_col = (c == 'C') ? col + 1 : col;
24305 w->column_number_displayed = col;
24306 pint2str (decode_mode_spec_buf, width, disp_col);
24307 return decode_mode_spec_buf;
24310 case 'e':
24311 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24313 if (NILP (Vmemory_full))
24314 return "";
24315 else
24316 return "!MEM FULL! ";
24318 #else
24319 return "";
24320 #endif
24322 case 'F':
24323 /* %F displays the frame name. */
24324 if (!NILP (f->title))
24325 return SSDATA (f->title);
24326 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24327 return SSDATA (f->name);
24328 return "Emacs";
24330 case 'f':
24331 obj = BVAR (b, filename);
24332 break;
24334 case 'i':
24336 ptrdiff_t size = ZV - BEGV;
24337 pint2str (decode_mode_spec_buf, width, size);
24338 return decode_mode_spec_buf;
24341 case 'I':
24343 ptrdiff_t size = ZV - BEGV;
24344 pint2hrstr (decode_mode_spec_buf, width, size);
24345 return decode_mode_spec_buf;
24348 case 'l':
24350 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24351 ptrdiff_t topline, nlines, height;
24352 ptrdiff_t junk;
24354 /* %c, %C, and %l are ignored in `frame-title-format'. */
24355 if (mode_line_target == MODE_LINE_TITLE)
24356 return "";
24358 startpos = marker_position (w->start);
24359 startpos_byte = marker_byte_position (w->start);
24360 height = WINDOW_TOTAL_LINES (w);
24362 /* If we decided that this buffer isn't suitable for line numbers,
24363 don't forget that too fast. */
24364 if (w->base_line_pos == -1)
24365 goto no_value;
24367 /* If the buffer is very big, don't waste time. */
24368 if (INTEGERP (Vline_number_display_limit)
24369 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24371 w->base_line_pos = 0;
24372 w->base_line_number = 0;
24373 goto no_value;
24376 if (w->base_line_number > 0
24377 && w->base_line_pos > 0
24378 && w->base_line_pos <= startpos)
24380 line = w->base_line_number;
24381 linepos = w->base_line_pos;
24382 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24384 else
24386 line = 1;
24387 linepos = BUF_BEGV (b);
24388 linepos_byte = BUF_BEGV_BYTE (b);
24391 /* Count lines from base line to window start position. */
24392 nlines = display_count_lines (linepos_byte,
24393 startpos_byte,
24394 startpos, &junk);
24396 topline = nlines + line;
24398 /* Determine a new base line, if the old one is too close
24399 or too far away, or if we did not have one.
24400 "Too close" means it's plausible a scroll-down would
24401 go back past it. */
24402 if (startpos == BUF_BEGV (b))
24404 w->base_line_number = topline;
24405 w->base_line_pos = BUF_BEGV (b);
24407 else if (nlines < height + 25 || nlines > height * 3 + 50
24408 || linepos == BUF_BEGV (b))
24410 ptrdiff_t limit = BUF_BEGV (b);
24411 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24412 ptrdiff_t position;
24413 ptrdiff_t distance =
24414 (height * 2 + 30) * line_number_display_limit_width;
24416 if (startpos - distance > limit)
24418 limit = startpos - distance;
24419 limit_byte = CHAR_TO_BYTE (limit);
24422 nlines = display_count_lines (startpos_byte,
24423 limit_byte,
24424 - (height * 2 + 30),
24425 &position);
24426 /* If we couldn't find the lines we wanted within
24427 line_number_display_limit_width chars per line,
24428 give up on line numbers for this window. */
24429 if (position == limit_byte && limit == startpos - distance)
24431 w->base_line_pos = -1;
24432 w->base_line_number = 0;
24433 goto no_value;
24436 w->base_line_number = topline - nlines;
24437 w->base_line_pos = BYTE_TO_CHAR (position);
24440 /* Now count lines from the start pos to point. */
24441 nlines = display_count_lines (startpos_byte,
24442 PT_BYTE, PT, &junk);
24444 /* Record that we did display the line number. */
24445 line_number_displayed = true;
24447 /* Make the string to show. */
24448 pint2str (decode_mode_spec_buf, width, topline + nlines);
24449 return decode_mode_spec_buf;
24450 no_value:
24452 char *p = decode_mode_spec_buf;
24453 int pad = width - 2;
24454 while (pad-- > 0)
24455 *p++ = ' ';
24456 *p++ = '?';
24457 *p++ = '?';
24458 *p = '\0';
24459 return decode_mode_spec_buf;
24462 break;
24464 case 'm':
24465 obj = BVAR (b, mode_name);
24466 break;
24468 case 'n':
24469 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24470 return " Narrow";
24471 break;
24473 /* Display the "degree of travel" of the window through the buffer. */
24474 case 'o':
24476 ptrdiff_t toppos = marker_position (w->start);
24477 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24478 ptrdiff_t begv = BUF_BEGV (b);
24479 ptrdiff_t zv = BUF_ZV (b);
24481 if (zv <= botpos)
24482 return toppos <= begv ? "All" : "Bottom";
24483 else if (toppos <= begv)
24484 return "Top";
24485 else
24487 sprintf (decode_mode_spec_buf, "%2d%%",
24488 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24489 return decode_mode_spec_buf;
24493 /* Display percentage of buffer above the top of the screen. */
24494 case 'p':
24496 ptrdiff_t pos = marker_position (w->start);
24497 ptrdiff_t begv = BUF_BEGV (b);
24498 ptrdiff_t zv = BUF_ZV (b);
24500 if (w->window_end_pos <= BUF_Z (b) - zv)
24501 return pos <= begv ? "All" : "Bottom";
24502 else if (pos <= begv)
24503 return "Top";
24504 else
24506 sprintf (decode_mode_spec_buf, "%2d%%",
24507 percent99 (pos - begv, zv - begv));
24508 return decode_mode_spec_buf;
24512 /* Display percentage of size above the bottom of the screen. */
24513 case 'P':
24515 ptrdiff_t toppos = marker_position (w->start);
24516 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24517 ptrdiff_t begv = BUF_BEGV (b);
24518 ptrdiff_t zv = BUF_ZV (b);
24520 if (zv <= botpos)
24521 return toppos <= begv ? "All" : "Bottom";
24522 else
24524 sprintf (decode_mode_spec_buf,
24525 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24526 percent99 (botpos - begv, zv - begv));
24527 return decode_mode_spec_buf;
24531 /* Display percentage offsets of top and bottom of the window,
24532 using "All" (but not "Top" or "Bottom") where appropriate. */
24533 case 'q':
24535 ptrdiff_t toppos = marker_position (w->start);
24536 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24537 ptrdiff_t begv = BUF_BEGV (b);
24538 ptrdiff_t zv = BUF_ZV (b);
24539 int top_perc, bot_perc;
24541 if ((toppos <= begv) && (zv <= botpos))
24542 return "All ";
24544 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24545 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24547 if (top_perc == bot_perc)
24548 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24549 else
24550 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24552 return decode_mode_spec_buf;
24555 case 's':
24556 /* status of process */
24557 obj = Fget_buffer_process (Fcurrent_buffer ());
24558 if (NILP (obj))
24559 return "no process";
24560 #ifndef MSDOS
24561 obj = Fsymbol_name (Fprocess_status (obj));
24562 #endif
24563 break;
24565 case '@':
24567 ptrdiff_t count = inhibit_garbage_collection ();
24568 Lisp_Object curdir = BVAR (current_buffer, directory);
24569 Lisp_Object val = Qnil;
24571 if (STRINGP (curdir))
24572 val = call1 (intern ("file-remote-p"), curdir);
24574 unbind_to (count, Qnil);
24576 if (NILP (val))
24577 return "-";
24578 else
24579 return "@";
24582 case 'z':
24583 /* coding-system (not including end-of-line format) */
24584 case 'Z':
24585 /* coding-system (including end-of-line type) */
24587 bool eol_flag = (c == 'Z');
24588 char *p = decode_mode_spec_buf;
24590 if (! FRAME_WINDOW_P (f))
24592 /* No need to mention EOL here--the terminal never needs
24593 to do EOL conversion. */
24594 p = decode_mode_spec_coding (CODING_ID_NAME
24595 (FRAME_KEYBOARD_CODING (f)->id),
24596 p, false);
24597 p = decode_mode_spec_coding (CODING_ID_NAME
24598 (FRAME_TERMINAL_CODING (f)->id),
24599 p, false);
24601 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24602 p, eol_flag);
24604 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24605 #ifdef subprocesses
24606 obj = Fget_buffer_process (Fcurrent_buffer ());
24607 if (PROCESSP (obj))
24609 p = decode_mode_spec_coding
24610 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24611 p = decode_mode_spec_coding
24612 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24614 #endif /* subprocesses */
24615 #endif /* false */
24616 *p = 0;
24617 return decode_mode_spec_buf;
24621 if (STRINGP (obj))
24623 *string = obj;
24624 return SSDATA (obj);
24626 else
24627 return "";
24631 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24632 means count lines back from START_BYTE. But don't go beyond
24633 LIMIT_BYTE. Return the number of lines thus found (always
24634 nonnegative).
24636 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24637 either the position COUNT lines after/before START_BYTE, if we
24638 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24639 COUNT lines. */
24641 static ptrdiff_t
24642 display_count_lines (ptrdiff_t start_byte,
24643 ptrdiff_t limit_byte, ptrdiff_t count,
24644 ptrdiff_t *byte_pos_ptr)
24646 register unsigned char *cursor;
24647 unsigned char *base;
24649 register ptrdiff_t ceiling;
24650 register unsigned char *ceiling_addr;
24651 ptrdiff_t orig_count = count;
24653 /* If we are not in selective display mode,
24654 check only for newlines. */
24655 bool selective_display
24656 = (!NILP (BVAR (current_buffer, selective_display))
24657 && !INTEGERP (BVAR (current_buffer, selective_display)));
24659 if (count > 0)
24661 while (start_byte < limit_byte)
24663 ceiling = BUFFER_CEILING_OF (start_byte);
24664 ceiling = min (limit_byte - 1, ceiling);
24665 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24666 base = (cursor = BYTE_POS_ADDR (start_byte));
24670 if (selective_display)
24672 while (*cursor != '\n' && *cursor != 015
24673 && ++cursor != ceiling_addr)
24674 continue;
24675 if (cursor == ceiling_addr)
24676 break;
24678 else
24680 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24681 if (! cursor)
24682 break;
24685 cursor++;
24687 if (--count == 0)
24689 start_byte += cursor - base;
24690 *byte_pos_ptr = start_byte;
24691 return orig_count;
24694 while (cursor < ceiling_addr);
24696 start_byte += ceiling_addr - base;
24699 else
24701 while (start_byte > limit_byte)
24703 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24704 ceiling = max (limit_byte, ceiling);
24705 ceiling_addr = BYTE_POS_ADDR (ceiling);
24706 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24707 while (true)
24709 if (selective_display)
24711 while (--cursor >= ceiling_addr
24712 && *cursor != '\n' && *cursor != 015)
24713 continue;
24714 if (cursor < ceiling_addr)
24715 break;
24717 else
24719 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24720 if (! cursor)
24721 break;
24724 if (++count == 0)
24726 start_byte += cursor - base + 1;
24727 *byte_pos_ptr = start_byte;
24728 /* When scanning backwards, we should
24729 not count the newline posterior to which we stop. */
24730 return - orig_count - 1;
24733 start_byte += ceiling_addr - base;
24737 *byte_pos_ptr = limit_byte;
24739 if (count < 0)
24740 return - orig_count + count;
24741 return orig_count - count;
24747 /***********************************************************************
24748 Displaying strings
24749 ***********************************************************************/
24751 /* Display a NUL-terminated string, starting with index START.
24753 If STRING is non-null, display that C string. Otherwise, the Lisp
24754 string LISP_STRING is displayed. There's a case that STRING is
24755 non-null and LISP_STRING is not nil. It means STRING is a string
24756 data of LISP_STRING. In that case, we display LISP_STRING while
24757 ignoring its text properties.
24759 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24760 FACE_STRING. Display STRING or LISP_STRING with the face at
24761 FACE_STRING_POS in FACE_STRING:
24763 Display the string in the environment given by IT, but use the
24764 standard display table, temporarily.
24766 FIELD_WIDTH is the minimum number of output glyphs to produce.
24767 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24768 with spaces. If STRING has more characters, more than FIELD_WIDTH
24769 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24771 PRECISION is the maximum number of characters to output from
24772 STRING. PRECISION < 0 means don't truncate the string.
24774 This is roughly equivalent to printf format specifiers:
24776 FIELD_WIDTH PRECISION PRINTF
24777 ----------------------------------------
24778 -1 -1 %s
24779 -1 10 %.10s
24780 10 -1 %10s
24781 20 10 %20.10s
24783 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24784 display them, and < 0 means obey the current buffer's value of
24785 enable_multibyte_characters.
24787 Value is the number of columns displayed. */
24789 static int
24790 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24791 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24792 int field_width, int precision, int max_x, int multibyte)
24794 int hpos_at_start = it->hpos;
24795 int saved_face_id = it->face_id;
24796 struct glyph_row *row = it->glyph_row;
24797 ptrdiff_t it_charpos;
24799 /* Initialize the iterator IT for iteration over STRING beginning
24800 with index START. */
24801 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24802 precision, field_width, multibyte);
24803 if (string && STRINGP (lisp_string))
24804 /* LISP_STRING is the one returned by decode_mode_spec. We should
24805 ignore its text properties. */
24806 it->stop_charpos = it->end_charpos;
24808 /* If displaying STRING, set up the face of the iterator from
24809 FACE_STRING, if that's given. */
24810 if (STRINGP (face_string))
24812 ptrdiff_t endptr;
24813 struct face *face;
24815 it->face_id
24816 = face_at_string_position (it->w, face_string, face_string_pos,
24817 0, &endptr, it->base_face_id, false);
24818 face = FACE_FROM_ID (it->f, it->face_id);
24819 it->face_box_p = face->box != FACE_NO_BOX;
24822 /* Set max_x to the maximum allowed X position. Don't let it go
24823 beyond the right edge of the window. */
24824 if (max_x <= 0)
24825 max_x = it->last_visible_x;
24826 else
24827 max_x = min (max_x, it->last_visible_x);
24829 /* Skip over display elements that are not visible. because IT->w is
24830 hscrolled. */
24831 if (it->current_x < it->first_visible_x)
24832 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24833 MOVE_TO_POS | MOVE_TO_X);
24835 row->ascent = it->max_ascent;
24836 row->height = it->max_ascent + it->max_descent;
24837 row->phys_ascent = it->max_phys_ascent;
24838 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24839 row->extra_line_spacing = it->max_extra_line_spacing;
24841 if (STRINGP (it->string))
24842 it_charpos = IT_STRING_CHARPOS (*it);
24843 else
24844 it_charpos = IT_CHARPOS (*it);
24846 /* This condition is for the case that we are called with current_x
24847 past last_visible_x. */
24848 while (it->current_x < max_x)
24850 int x_before, x, n_glyphs_before, i, nglyphs;
24852 /* Get the next display element. */
24853 if (!get_next_display_element (it))
24854 break;
24856 /* Produce glyphs. */
24857 x_before = it->current_x;
24858 n_glyphs_before = row->used[TEXT_AREA];
24859 PRODUCE_GLYPHS (it);
24861 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24862 i = 0;
24863 x = x_before;
24864 while (i < nglyphs)
24866 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24868 if (it->line_wrap != TRUNCATE
24869 && x + glyph->pixel_width > max_x)
24871 /* End of continued line or max_x reached. */
24872 if (CHAR_GLYPH_PADDING_P (*glyph))
24874 /* A wide character is unbreakable. */
24875 if (row->reversed_p)
24876 unproduce_glyphs (it, row->used[TEXT_AREA]
24877 - n_glyphs_before);
24878 row->used[TEXT_AREA] = n_glyphs_before;
24879 it->current_x = x_before;
24881 else
24883 if (row->reversed_p)
24884 unproduce_glyphs (it, row->used[TEXT_AREA]
24885 - (n_glyphs_before + i));
24886 row->used[TEXT_AREA] = n_glyphs_before + i;
24887 it->current_x = x;
24889 break;
24891 else if (x + glyph->pixel_width >= it->first_visible_x)
24893 /* Glyph is at least partially visible. */
24894 ++it->hpos;
24895 if (x < it->first_visible_x)
24896 row->x = x - it->first_visible_x;
24898 else
24900 /* Glyph is off the left margin of the display area.
24901 Should not happen. */
24902 emacs_abort ();
24905 row->ascent = max (row->ascent, it->max_ascent);
24906 row->height = max (row->height, it->max_ascent + it->max_descent);
24907 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24908 row->phys_height = max (row->phys_height,
24909 it->max_phys_ascent + it->max_phys_descent);
24910 row->extra_line_spacing = max (row->extra_line_spacing,
24911 it->max_extra_line_spacing);
24912 x += glyph->pixel_width;
24913 ++i;
24916 /* Stop if max_x reached. */
24917 if (i < nglyphs)
24918 break;
24920 /* Stop at line ends. */
24921 if (ITERATOR_AT_END_OF_LINE_P (it))
24923 it->continuation_lines_width = 0;
24924 break;
24927 set_iterator_to_next (it, true);
24928 if (STRINGP (it->string))
24929 it_charpos = IT_STRING_CHARPOS (*it);
24930 else
24931 it_charpos = IT_CHARPOS (*it);
24933 /* Stop if truncating at the right edge. */
24934 if (it->line_wrap == TRUNCATE
24935 && it->current_x >= it->last_visible_x)
24937 /* Add truncation mark, but don't do it if the line is
24938 truncated at a padding space. */
24939 if (it_charpos < it->string_nchars)
24941 if (!FRAME_WINDOW_P (it->f))
24943 int ii, n;
24945 if (it->current_x > it->last_visible_x)
24947 if (!row->reversed_p)
24949 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24950 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24951 break;
24953 else
24955 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24956 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24957 break;
24958 unproduce_glyphs (it, ii + 1);
24959 ii = row->used[TEXT_AREA] - (ii + 1);
24961 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24963 row->used[TEXT_AREA] = ii;
24964 produce_special_glyphs (it, IT_TRUNCATION);
24967 produce_special_glyphs (it, IT_TRUNCATION);
24969 row->truncated_on_right_p = true;
24971 break;
24975 /* Maybe insert a truncation at the left. */
24976 if (it->first_visible_x
24977 && it_charpos > 0)
24979 if (!FRAME_WINDOW_P (it->f)
24980 || (row->reversed_p
24981 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24982 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24983 insert_left_trunc_glyphs (it);
24984 row->truncated_on_left_p = true;
24987 it->face_id = saved_face_id;
24989 /* Value is number of columns displayed. */
24990 return it->hpos - hpos_at_start;
24995 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24996 appears as an element of LIST or as the car of an element of LIST.
24997 If PROPVAL is a list, compare each element against LIST in that
24998 way, and return 1/2 if any element of PROPVAL is found in LIST.
24999 Otherwise return 0. This function cannot quit.
25000 The return value is 2 if the text is invisible but with an ellipsis
25001 and 1 if it's invisible and without an ellipsis. */
25004 invisible_prop (Lisp_Object propval, Lisp_Object list)
25006 Lisp_Object tail, proptail;
25008 for (tail = list; CONSP (tail); tail = XCDR (tail))
25010 register Lisp_Object tem;
25011 tem = XCAR (tail);
25012 if (EQ (propval, tem))
25013 return 1;
25014 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25015 return NILP (XCDR (tem)) ? 1 : 2;
25018 if (CONSP (propval))
25020 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25022 Lisp_Object propelt;
25023 propelt = XCAR (proptail);
25024 for (tail = list; CONSP (tail); tail = XCDR (tail))
25026 register Lisp_Object tem;
25027 tem = XCAR (tail);
25028 if (EQ (propelt, tem))
25029 return 1;
25030 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25031 return NILP (XCDR (tem)) ? 1 : 2;
25036 return 0;
25039 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25040 doc: /* Non-nil if the property makes the text invisible.
25041 POS-OR-PROP can be a marker or number, in which case it is taken to be
25042 a position in the current buffer and the value of the `invisible' property
25043 is checked; or it can be some other value, which is then presumed to be the
25044 value of the `invisible' property of the text of interest.
25045 The non-nil value returned can be t for truly invisible text or something
25046 else if the text is replaced by an ellipsis. */)
25047 (Lisp_Object pos_or_prop)
25049 Lisp_Object prop
25050 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
25051 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
25052 : pos_or_prop);
25053 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25054 return (invis == 0 ? Qnil
25055 : invis == 1 ? Qt
25056 : make_number (invis));
25059 /* Calculate a width or height in pixels from a specification using
25060 the following elements:
25062 SPEC ::=
25063 NUM - a (fractional) multiple of the default font width/height
25064 (NUM) - specifies exactly NUM pixels
25065 UNIT - a fixed number of pixels, see below.
25066 ELEMENT - size of a display element in pixels, see below.
25067 (NUM . SPEC) - equals NUM * SPEC
25068 (+ SPEC SPEC ...) - add pixel values
25069 (- SPEC SPEC ...) - subtract pixel values
25070 (- SPEC) - negate pixel value
25072 NUM ::=
25073 INT or FLOAT - a number constant
25074 SYMBOL - use symbol's (buffer local) variable binding.
25076 UNIT ::=
25077 in - pixels per inch *)
25078 mm - pixels per 1/1000 meter *)
25079 cm - pixels per 1/100 meter *)
25080 width - width of current font in pixels.
25081 height - height of current font in pixels.
25083 *) using the ratio(s) defined in display-pixels-per-inch.
25085 ELEMENT ::=
25087 left-fringe - left fringe width in pixels
25088 right-fringe - right fringe width in pixels
25090 left-margin - left margin width in pixels
25091 right-margin - right margin width in pixels
25093 scroll-bar - scroll-bar area width in pixels
25095 Examples:
25097 Pixels corresponding to 5 inches:
25098 (5 . in)
25100 Total width of non-text areas on left side of window (if scroll-bar is on left):
25101 '(space :width (+ left-fringe left-margin scroll-bar))
25103 Align to first text column (in header line):
25104 '(space :align-to 0)
25106 Align to middle of text area minus half the width of variable `my-image'
25107 containing a loaded image:
25108 '(space :align-to (0.5 . (- text my-image)))
25110 Width of left margin minus width of 1 character in the default font:
25111 '(space :width (- left-margin 1))
25113 Width of left margin minus width of 2 characters in the current font:
25114 '(space :width (- left-margin (2 . width)))
25116 Center 1 character over left-margin (in header line):
25117 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25119 Different ways to express width of left fringe plus left margin minus one pixel:
25120 '(space :width (- (+ left-fringe left-margin) (1)))
25121 '(space :width (+ left-fringe left-margin (- (1))))
25122 '(space :width (+ left-fringe left-margin (-1)))
25126 static bool
25127 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25128 struct font *font, bool width_p, int *align_to)
25130 double pixels;
25132 # define OK_PIXELS(val) (*res = (val), true)
25133 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25135 if (NILP (prop))
25136 return OK_PIXELS (0);
25138 eassert (FRAME_LIVE_P (it->f));
25140 if (SYMBOLP (prop))
25142 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25144 char *unit = SSDATA (SYMBOL_NAME (prop));
25146 if (unit[0] == 'i' && unit[1] == 'n')
25147 pixels = 1.0;
25148 else if (unit[0] == 'm' && unit[1] == 'm')
25149 pixels = 25.4;
25150 else if (unit[0] == 'c' && unit[1] == 'm')
25151 pixels = 2.54;
25152 else
25153 pixels = 0;
25154 if (pixels > 0)
25156 double ppi = (width_p ? FRAME_RES_X (it->f)
25157 : FRAME_RES_Y (it->f));
25159 if (ppi > 0)
25160 return OK_PIXELS (ppi / pixels);
25161 return false;
25165 #ifdef HAVE_WINDOW_SYSTEM
25166 if (EQ (prop, Qheight))
25167 return OK_PIXELS (font
25168 ? normal_char_height (font, -1)
25169 : FRAME_LINE_HEIGHT (it->f));
25170 if (EQ (prop, Qwidth))
25171 return OK_PIXELS (font
25172 ? FONT_WIDTH (font)
25173 : FRAME_COLUMN_WIDTH (it->f));
25174 #else
25175 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25176 return OK_PIXELS (1);
25177 #endif
25179 if (EQ (prop, Qtext))
25180 return OK_PIXELS (width_p
25181 ? window_box_width (it->w, TEXT_AREA)
25182 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25184 if (align_to && *align_to < 0)
25186 *res = 0;
25187 if (EQ (prop, Qleft))
25188 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
25189 if (EQ (prop, Qright))
25190 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25191 if (EQ (prop, Qcenter))
25192 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25193 + window_box_width (it->w, TEXT_AREA) / 2);
25194 if (EQ (prop, Qleft_fringe))
25195 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25196 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25197 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25198 if (EQ (prop, Qright_fringe))
25199 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25200 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25201 : window_box_right_offset (it->w, TEXT_AREA));
25202 if (EQ (prop, Qleft_margin))
25203 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25204 if (EQ (prop, Qright_margin))
25205 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25206 if (EQ (prop, Qscroll_bar))
25207 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25209 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25210 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25211 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25212 : 0)));
25214 else
25216 if (EQ (prop, Qleft_fringe))
25217 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25218 if (EQ (prop, Qright_fringe))
25219 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25220 if (EQ (prop, Qleft_margin))
25221 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25222 if (EQ (prop, Qright_margin))
25223 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25224 if (EQ (prop, Qscroll_bar))
25225 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25228 prop = buffer_local_value (prop, it->w->contents);
25229 if (EQ (prop, Qunbound))
25230 prop = Qnil;
25233 if (NUMBERP (prop))
25235 int base_unit = (width_p
25236 ? FRAME_COLUMN_WIDTH (it->f)
25237 : FRAME_LINE_HEIGHT (it->f));
25238 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25241 if (CONSP (prop))
25243 Lisp_Object car = XCAR (prop);
25244 Lisp_Object cdr = XCDR (prop);
25246 if (SYMBOLP (car))
25248 #ifdef HAVE_WINDOW_SYSTEM
25249 if (FRAME_WINDOW_P (it->f)
25250 && valid_image_p (prop))
25252 ptrdiff_t id = lookup_image (it->f, prop);
25253 struct image *img = IMAGE_FROM_ID (it->f, id);
25255 return OK_PIXELS (width_p ? img->width : img->height);
25257 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25259 /* TODO: Don't return dummy size. */
25260 return OK_PIXELS (100);
25262 #endif
25263 if (EQ (car, Qplus) || EQ (car, Qminus))
25265 bool first = true;
25266 double px;
25268 pixels = 0;
25269 while (CONSP (cdr))
25271 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25272 font, width_p, align_to))
25273 return false;
25274 if (first)
25275 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25276 else
25277 pixels += px;
25278 cdr = XCDR (cdr);
25280 if (EQ (car, Qminus))
25281 pixels = -pixels;
25282 return OK_PIXELS (pixels);
25285 car = buffer_local_value (car, it->w->contents);
25286 if (EQ (car, Qunbound))
25287 car = Qnil;
25290 if (NUMBERP (car))
25292 double fact;
25293 pixels = XFLOATINT (car);
25294 if (NILP (cdr))
25295 return OK_PIXELS (pixels);
25296 if (calc_pixel_width_or_height (&fact, it, cdr,
25297 font, width_p, align_to))
25298 return OK_PIXELS (pixels * fact);
25299 return false;
25302 return false;
25305 return false;
25308 void
25309 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25311 #ifdef HAVE_WINDOW_SYSTEM
25312 normal_char_ascent_descent (font, -1, ascent, descent);
25313 #else
25314 *ascent = 1;
25315 *descent = 0;
25316 #endif
25320 /***********************************************************************
25321 Glyph Display
25322 ***********************************************************************/
25324 #ifdef HAVE_WINDOW_SYSTEM
25326 #ifdef GLYPH_DEBUG
25328 void
25329 dump_glyph_string (struct glyph_string *s)
25331 fprintf (stderr, "glyph string\n");
25332 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25333 s->x, s->y, s->width, s->height);
25334 fprintf (stderr, " ybase = %d\n", s->ybase);
25335 fprintf (stderr, " hl = %u\n", s->hl);
25336 fprintf (stderr, " left overhang = %d, right = %d\n",
25337 s->left_overhang, s->right_overhang);
25338 fprintf (stderr, " nchars = %d\n", s->nchars);
25339 fprintf (stderr, " extends to end of line = %d\n",
25340 s->extends_to_end_of_line_p);
25341 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25342 fprintf (stderr, " bg width = %d\n", s->background_width);
25345 #endif /* GLYPH_DEBUG */
25347 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25348 of XChar2b structures for S; it can't be allocated in
25349 init_glyph_string because it must be allocated via `alloca'. W
25350 is the window on which S is drawn. ROW and AREA are the glyph row
25351 and area within the row from which S is constructed. START is the
25352 index of the first glyph structure covered by S. HL is a
25353 face-override for drawing S. */
25355 #ifdef HAVE_NTGUI
25356 #define OPTIONAL_HDC(hdc) HDC hdc,
25357 #define DECLARE_HDC(hdc) HDC hdc;
25358 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25359 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25360 #endif
25362 #ifndef OPTIONAL_HDC
25363 #define OPTIONAL_HDC(hdc)
25364 #define DECLARE_HDC(hdc)
25365 #define ALLOCATE_HDC(hdc, f)
25366 #define RELEASE_HDC(hdc, f)
25367 #endif
25369 static void
25370 init_glyph_string (struct glyph_string *s,
25371 OPTIONAL_HDC (hdc)
25372 XChar2b *char2b, struct window *w, struct glyph_row *row,
25373 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25375 memset (s, 0, sizeof *s);
25376 s->w = w;
25377 s->f = XFRAME (w->frame);
25378 #ifdef HAVE_NTGUI
25379 s->hdc = hdc;
25380 #endif
25381 s->display = FRAME_X_DISPLAY (s->f);
25382 s->char2b = char2b;
25383 s->hl = hl;
25384 s->row = row;
25385 s->area = area;
25386 s->first_glyph = row->glyphs[area] + start;
25387 s->height = row->height;
25388 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25389 s->ybase = s->y + row->ascent;
25393 /* Append the list of glyph strings with head H and tail T to the list
25394 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25396 static void
25397 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25398 struct glyph_string *h, struct glyph_string *t)
25400 if (h)
25402 if (*head)
25403 (*tail)->next = h;
25404 else
25405 *head = h;
25406 h->prev = *tail;
25407 *tail = t;
25412 /* Prepend the list of glyph strings with head H and tail T to the
25413 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25414 result. */
25416 static void
25417 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25418 struct glyph_string *h, struct glyph_string *t)
25420 if (h)
25422 if (*head)
25423 (*head)->prev = t;
25424 else
25425 *tail = t;
25426 t->next = *head;
25427 *head = h;
25432 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25433 Set *HEAD and *TAIL to the resulting list. */
25435 static void
25436 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25437 struct glyph_string *s)
25439 s->next = s->prev = NULL;
25440 append_glyph_string_lists (head, tail, s, s);
25444 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25445 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25446 make sure that X resources for the face returned are allocated.
25447 Value is a pointer to a realized face that is ready for display if
25448 DISPLAY_P. */
25450 static struct face *
25451 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25452 XChar2b *char2b, bool display_p)
25454 struct face *face = FACE_FROM_ID (f, face_id);
25455 unsigned code = 0;
25457 if (face->font)
25459 code = face->font->driver->encode_char (face->font, c);
25461 if (code == FONT_INVALID_CODE)
25462 code = 0;
25464 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25466 /* Make sure X resources of the face are allocated. */
25467 #ifdef HAVE_X_WINDOWS
25468 if (display_p)
25469 #endif
25471 eassert (face != NULL);
25472 prepare_face_for_display (f, face);
25475 return face;
25479 /* Get face and two-byte form of character glyph GLYPH on frame F.
25480 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25481 a pointer to a realized face that is ready for display. */
25483 static struct face *
25484 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25485 XChar2b *char2b)
25487 struct face *face;
25488 unsigned code = 0;
25490 eassert (glyph->type == CHAR_GLYPH);
25491 face = FACE_FROM_ID (f, glyph->face_id);
25493 /* Make sure X resources of the face are allocated. */
25494 prepare_face_for_display (f, face);
25496 if (face->font)
25498 if (CHAR_BYTE8_P (glyph->u.ch))
25499 code = CHAR_TO_BYTE8 (glyph->u.ch);
25500 else
25501 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25503 if (code == FONT_INVALID_CODE)
25504 code = 0;
25507 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25508 return face;
25512 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25513 Return true iff FONT has a glyph for C. */
25515 static bool
25516 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25518 unsigned code;
25520 if (CHAR_BYTE8_P (c))
25521 code = CHAR_TO_BYTE8 (c);
25522 else
25523 code = font->driver->encode_char (font, c);
25525 if (code == FONT_INVALID_CODE)
25526 return false;
25527 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25528 return true;
25532 /* Fill glyph string S with composition components specified by S->cmp.
25534 BASE_FACE is the base face of the composition.
25535 S->cmp_from is the index of the first component for S.
25537 OVERLAPS non-zero means S should draw the foreground only, and use
25538 its physical height for clipping. See also draw_glyphs.
25540 Value is the index of a component not in S. */
25542 static int
25543 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25544 int overlaps)
25546 int i;
25547 /* For all glyphs of this composition, starting at the offset
25548 S->cmp_from, until we reach the end of the definition or encounter a
25549 glyph that requires the different face, add it to S. */
25550 struct face *face;
25552 eassert (s);
25554 s->for_overlaps = overlaps;
25555 s->face = NULL;
25556 s->font = NULL;
25557 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25559 int c = COMPOSITION_GLYPH (s->cmp, i);
25561 /* TAB in a composition means display glyphs with padding space
25562 on the left or right. */
25563 if (c != '\t')
25565 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25566 -1, Qnil);
25568 face = get_char_face_and_encoding (s->f, c, face_id,
25569 s->char2b + i, true);
25570 if (face)
25572 if (! s->face)
25574 s->face = face;
25575 s->font = s->face->font;
25577 else if (s->face != face)
25578 break;
25581 ++s->nchars;
25583 s->cmp_to = i;
25585 if (s->face == NULL)
25587 s->face = base_face->ascii_face;
25588 s->font = s->face->font;
25591 /* All glyph strings for the same composition has the same width,
25592 i.e. the width set for the first component of the composition. */
25593 s->width = s->first_glyph->pixel_width;
25595 /* If the specified font could not be loaded, use the frame's
25596 default font, but record the fact that we couldn't load it in
25597 the glyph string so that we can draw rectangles for the
25598 characters of the glyph string. */
25599 if (s->font == NULL)
25601 s->font_not_found_p = true;
25602 s->font = FRAME_FONT (s->f);
25605 /* Adjust base line for subscript/superscript text. */
25606 s->ybase += s->first_glyph->voffset;
25608 return s->cmp_to;
25611 static int
25612 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25613 int start, int end, int overlaps)
25615 struct glyph *glyph, *last;
25616 Lisp_Object lgstring;
25617 int i;
25619 s->for_overlaps = overlaps;
25620 glyph = s->row->glyphs[s->area] + start;
25621 last = s->row->glyphs[s->area] + end;
25622 s->cmp_id = glyph->u.cmp.id;
25623 s->cmp_from = glyph->slice.cmp.from;
25624 s->cmp_to = glyph->slice.cmp.to + 1;
25625 s->face = FACE_FROM_ID (s->f, face_id);
25626 lgstring = composition_gstring_from_id (s->cmp_id);
25627 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25628 glyph++;
25629 while (glyph < last
25630 && glyph->u.cmp.automatic
25631 && glyph->u.cmp.id == s->cmp_id
25632 && s->cmp_to == glyph->slice.cmp.from)
25633 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25635 for (i = s->cmp_from; i < s->cmp_to; i++)
25637 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25638 unsigned code = LGLYPH_CODE (lglyph);
25640 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25642 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25643 return glyph - s->row->glyphs[s->area];
25647 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25648 See the comment of fill_glyph_string for arguments.
25649 Value is the index of the first glyph not in S. */
25652 static int
25653 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25654 int start, int end, int overlaps)
25656 struct glyph *glyph, *last;
25657 int voffset;
25659 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25660 s->for_overlaps = overlaps;
25661 glyph = s->row->glyphs[s->area] + start;
25662 last = s->row->glyphs[s->area] + end;
25663 voffset = glyph->voffset;
25664 s->face = FACE_FROM_ID (s->f, face_id);
25665 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25666 s->nchars = 1;
25667 s->width = glyph->pixel_width;
25668 glyph++;
25669 while (glyph < last
25670 && glyph->type == GLYPHLESS_GLYPH
25671 && glyph->voffset == voffset
25672 && glyph->face_id == face_id)
25674 s->nchars++;
25675 s->width += glyph->pixel_width;
25676 glyph++;
25678 s->ybase += voffset;
25679 return glyph - s->row->glyphs[s->area];
25683 /* Fill glyph string S from a sequence of character glyphs.
25685 FACE_ID is the face id of the string. START is the index of the
25686 first glyph to consider, END is the index of the last + 1.
25687 OVERLAPS non-zero means S should draw the foreground only, and use
25688 its physical height for clipping. See also draw_glyphs.
25690 Value is the index of the first glyph not in S. */
25692 static int
25693 fill_glyph_string (struct glyph_string *s, int face_id,
25694 int start, int end, int overlaps)
25696 struct glyph *glyph, *last;
25697 int voffset;
25698 bool glyph_not_available_p;
25700 eassert (s->f == XFRAME (s->w->frame));
25701 eassert (s->nchars == 0);
25702 eassert (start >= 0 && end > start);
25704 s->for_overlaps = overlaps;
25705 glyph = s->row->glyphs[s->area] + start;
25706 last = s->row->glyphs[s->area] + end;
25707 voffset = glyph->voffset;
25708 s->padding_p = glyph->padding_p;
25709 glyph_not_available_p = glyph->glyph_not_available_p;
25711 while (glyph < last
25712 && glyph->type == CHAR_GLYPH
25713 && glyph->voffset == voffset
25714 /* Same face id implies same font, nowadays. */
25715 && glyph->face_id == face_id
25716 && glyph->glyph_not_available_p == glyph_not_available_p)
25718 s->face = get_glyph_face_and_encoding (s->f, glyph,
25719 s->char2b + s->nchars);
25720 ++s->nchars;
25721 eassert (s->nchars <= end - start);
25722 s->width += glyph->pixel_width;
25723 if (glyph++->padding_p != s->padding_p)
25724 break;
25727 s->font = s->face->font;
25729 /* If the specified font could not be loaded, use the frame's font,
25730 but record the fact that we couldn't load it in
25731 S->font_not_found_p so that we can draw rectangles for the
25732 characters of the glyph string. */
25733 if (s->font == NULL || glyph_not_available_p)
25735 s->font_not_found_p = true;
25736 s->font = FRAME_FONT (s->f);
25739 /* Adjust base line for subscript/superscript text. */
25740 s->ybase += voffset;
25742 eassert (s->face && s->face->gc);
25743 return glyph - s->row->glyphs[s->area];
25747 /* Fill glyph string S from image glyph S->first_glyph. */
25749 static void
25750 fill_image_glyph_string (struct glyph_string *s)
25752 eassert (s->first_glyph->type == IMAGE_GLYPH);
25753 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25754 eassert (s->img);
25755 s->slice = s->first_glyph->slice.img;
25756 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25757 s->font = s->face->font;
25758 s->width = s->first_glyph->pixel_width;
25760 /* Adjust base line for subscript/superscript text. */
25761 s->ybase += s->first_glyph->voffset;
25765 #ifdef HAVE_XWIDGETS
25766 static void
25767 fill_xwidget_glyph_string (struct glyph_string *s)
25769 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25770 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25771 s->font = s->face->font;
25772 s->width = s->first_glyph->pixel_width;
25773 s->ybase += s->first_glyph->voffset;
25774 s->xwidget = s->first_glyph->u.xwidget;
25776 #endif
25777 /* Fill glyph string S from a sequence of stretch glyphs.
25779 START is the index of the first glyph to consider,
25780 END is the index of the last + 1.
25782 Value is the index of the first glyph not in S. */
25784 static int
25785 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25787 struct glyph *glyph, *last;
25788 int voffset, face_id;
25790 eassert (s->first_glyph->type == STRETCH_GLYPH);
25792 glyph = s->row->glyphs[s->area] + start;
25793 last = s->row->glyphs[s->area] + end;
25794 face_id = glyph->face_id;
25795 s->face = FACE_FROM_ID (s->f, face_id);
25796 s->font = s->face->font;
25797 s->width = glyph->pixel_width;
25798 s->nchars = 1;
25799 voffset = glyph->voffset;
25801 for (++glyph;
25802 (glyph < last
25803 && glyph->type == STRETCH_GLYPH
25804 && glyph->voffset == voffset
25805 && glyph->face_id == face_id);
25806 ++glyph)
25807 s->width += glyph->pixel_width;
25809 /* Adjust base line for subscript/superscript text. */
25810 s->ybase += voffset;
25812 /* The case that face->gc == 0 is handled when drawing the glyph
25813 string by calling prepare_face_for_display. */
25814 eassert (s->face);
25815 return glyph - s->row->glyphs[s->area];
25818 static struct font_metrics *
25819 get_per_char_metric (struct font *font, XChar2b *char2b)
25821 static struct font_metrics metrics;
25822 unsigned code;
25824 if (! font)
25825 return NULL;
25826 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25827 if (code == FONT_INVALID_CODE)
25828 return NULL;
25829 font->driver->text_extents (font, &code, 1, &metrics);
25830 return &metrics;
25833 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25834 for FONT. Values are taken from font-global ones, except for fonts
25835 that claim preposterously large values, but whose glyphs actually
25836 have reasonable dimensions. C is the character to use for metrics
25837 if the font-global values are too large; if C is negative, the
25838 function selects a default character. */
25839 static void
25840 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25842 *ascent = FONT_BASE (font);
25843 *descent = FONT_DESCENT (font);
25845 if (FONT_TOO_HIGH (font))
25847 XChar2b char2b;
25849 /* Get metrics of C, defaulting to a reasonably sized ASCII
25850 character. */
25851 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25853 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25855 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25857 /* We add 1 pixel to character dimensions as heuristics
25858 that produces nicer display, e.g. when the face has
25859 the box attribute. */
25860 *ascent = pcm->ascent + 1;
25861 *descent = pcm->descent + 1;
25867 /* A subroutine that computes a reasonable "normal character height"
25868 for fonts that claim preposterously large vertical dimensions, but
25869 whose glyphs are actually reasonably sized. C is the character
25870 whose metrics to use for those fonts, or -1 for default
25871 character. */
25872 static int
25873 normal_char_height (struct font *font, int c)
25875 int ascent, descent;
25877 normal_char_ascent_descent (font, c, &ascent, &descent);
25879 return ascent + descent;
25882 /* EXPORT for RIF:
25883 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25884 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25885 assumed to be zero. */
25887 void
25888 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25890 *left = *right = 0;
25892 if (glyph->type == CHAR_GLYPH)
25894 XChar2b char2b;
25895 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25896 if (face->font)
25898 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25899 if (pcm)
25901 if (pcm->rbearing > pcm->width)
25902 *right = pcm->rbearing - pcm->width;
25903 if (pcm->lbearing < 0)
25904 *left = -pcm->lbearing;
25908 else if (glyph->type == COMPOSITE_GLYPH)
25910 if (! glyph->u.cmp.automatic)
25912 struct composition *cmp = composition_table[glyph->u.cmp.id];
25914 if (cmp->rbearing > cmp->pixel_width)
25915 *right = cmp->rbearing - cmp->pixel_width;
25916 if (cmp->lbearing < 0)
25917 *left = - cmp->lbearing;
25919 else
25921 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25922 struct font_metrics metrics;
25924 composition_gstring_width (gstring, glyph->slice.cmp.from,
25925 glyph->slice.cmp.to + 1, &metrics);
25926 if (metrics.rbearing > metrics.width)
25927 *right = metrics.rbearing - metrics.width;
25928 if (metrics.lbearing < 0)
25929 *left = - metrics.lbearing;
25935 /* Return the index of the first glyph preceding glyph string S that
25936 is overwritten by S because of S's left overhang. Value is -1
25937 if no glyphs are overwritten. */
25939 static int
25940 left_overwritten (struct glyph_string *s)
25942 int k;
25944 if (s->left_overhang)
25946 int x = 0, i;
25947 struct glyph *glyphs = s->row->glyphs[s->area];
25948 int first = s->first_glyph - glyphs;
25950 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25951 x -= glyphs[i].pixel_width;
25953 k = i + 1;
25955 else
25956 k = -1;
25958 return k;
25962 /* Return the index of the first glyph preceding glyph string S that
25963 is overwriting S because of its right overhang. Value is -1 if no
25964 glyph in front of S overwrites S. */
25966 static int
25967 left_overwriting (struct glyph_string *s)
25969 int i, k, x;
25970 struct glyph *glyphs = s->row->glyphs[s->area];
25971 int first = s->first_glyph - glyphs;
25973 k = -1;
25974 x = 0;
25975 for (i = first - 1; i >= 0; --i)
25977 int left, right;
25978 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25979 if (x + right > 0)
25980 k = i;
25981 x -= glyphs[i].pixel_width;
25984 return k;
25988 /* Return the index of the last glyph following glyph string S that is
25989 overwritten by S because of S's right overhang. Value is -1 if
25990 no such glyph is found. */
25992 static int
25993 right_overwritten (struct glyph_string *s)
25995 int k = -1;
25997 if (s->right_overhang)
25999 int x = 0, i;
26000 struct glyph *glyphs = s->row->glyphs[s->area];
26001 int first = (s->first_glyph - glyphs
26002 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26003 int end = s->row->used[s->area];
26005 for (i = first; i < end && s->right_overhang > x; ++i)
26006 x += glyphs[i].pixel_width;
26008 k = i;
26011 return k;
26015 /* Return the index of the last glyph following glyph string S that
26016 overwrites S because of its left overhang. Value is negative
26017 if no such glyph is found. */
26019 static int
26020 right_overwriting (struct glyph_string *s)
26022 int i, k, x;
26023 int end = s->row->used[s->area];
26024 struct glyph *glyphs = s->row->glyphs[s->area];
26025 int first = (s->first_glyph - glyphs
26026 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26028 k = -1;
26029 x = 0;
26030 for (i = first; i < end; ++i)
26032 int left, right;
26033 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26034 if (x - left < 0)
26035 k = i;
26036 x += glyphs[i].pixel_width;
26039 return k;
26043 /* Set background width of glyph string S. START is the index of the
26044 first glyph following S. LAST_X is the right-most x-position + 1
26045 in the drawing area. */
26047 static void
26048 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26050 /* If the face of this glyph string has to be drawn to the end of
26051 the drawing area, set S->extends_to_end_of_line_p. */
26053 if (start == s->row->used[s->area]
26054 && ((s->row->fill_line_p
26055 && (s->hl == DRAW_NORMAL_TEXT
26056 || s->hl == DRAW_IMAGE_RAISED
26057 || s->hl == DRAW_IMAGE_SUNKEN))
26058 || s->hl == DRAW_MOUSE_FACE))
26059 s->extends_to_end_of_line_p = true;
26061 /* If S extends its face to the end of the line, set its
26062 background_width to the distance to the right edge of the drawing
26063 area. */
26064 if (s->extends_to_end_of_line_p)
26065 s->background_width = last_x - s->x + 1;
26066 else
26067 s->background_width = s->width;
26071 /* Return glyph string that shares background with glyph string S and
26072 whose `background_width' member has been set. */
26074 static struct glyph_string *
26075 glyph_string_containing_background_width (struct glyph_string *s)
26077 if (s->cmp)
26078 while (s->cmp_from)
26079 s = s->prev;
26081 return s;
26085 /* Compute overhangs and x-positions for glyph string S and its
26086 predecessors, or successors. X is the starting x-position for S.
26087 BACKWARD_P means process predecessors. */
26089 static void
26090 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26092 if (backward_p)
26094 while (s)
26096 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26097 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26098 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26099 x -= s->width;
26100 s->x = x;
26101 s = s->prev;
26104 else
26106 while (s)
26108 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26109 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26110 s->x = x;
26111 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26112 x += s->width;
26113 s = s->next;
26120 /* The following macros are only called from draw_glyphs below.
26121 They reference the following parameters of that function directly:
26122 `w', `row', `area', and `overlap_p'
26123 as well as the following local variables:
26124 `s', `f', and `hdc' (in W32) */
26126 #ifdef HAVE_NTGUI
26127 /* On W32, silently add local `hdc' variable to argument list of
26128 init_glyph_string. */
26129 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26130 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26131 #else
26132 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26133 init_glyph_string (s, char2b, w, row, area, start, hl)
26134 #endif
26136 /* Add a glyph string for a stretch glyph to the list of strings
26137 between HEAD and TAIL. START is the index of the stretch glyph in
26138 row area AREA of glyph row ROW. END is the index of the last glyph
26139 in that glyph row area. X is the current output position assigned
26140 to the new glyph string constructed. HL overrides that face of the
26141 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26142 is the right-most x-position of the drawing area. */
26144 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26145 and below -- keep them on one line. */
26146 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26147 do \
26149 s = alloca (sizeof *s); \
26150 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26151 START = fill_stretch_glyph_string (s, START, END); \
26152 append_glyph_string (&HEAD, &TAIL, s); \
26153 s->x = (X); \
26155 while (false)
26158 /* Add a glyph string for an image glyph to the list of strings
26159 between HEAD and TAIL. START is the index of the image glyph in
26160 row area AREA of glyph row ROW. END is the index of the last glyph
26161 in that glyph row area. X is the current output position assigned
26162 to the new glyph string constructed. HL overrides that face of the
26163 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26164 is the right-most x-position of the drawing area. */
26166 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26167 do \
26169 s = alloca (sizeof *s); \
26170 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26171 fill_image_glyph_string (s); \
26172 append_glyph_string (&HEAD, &TAIL, s); \
26173 ++START; \
26174 s->x = (X); \
26176 while (false)
26178 #ifndef HAVE_XWIDGETS
26179 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26180 eassume (false)
26181 #else
26182 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26183 do \
26185 s = alloca (sizeof *s); \
26186 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26187 fill_xwidget_glyph_string (s); \
26188 append_glyph_string (&(HEAD), &(TAIL), s); \
26189 ++(START); \
26190 s->x = (X); \
26192 while (false)
26193 #endif
26195 /* Add a glyph string for a sequence of character glyphs to the list
26196 of strings between HEAD and TAIL. START is the index of the first
26197 glyph in row area AREA of glyph row ROW that is part of the new
26198 glyph string. END is the index of the last glyph in that glyph row
26199 area. X is the current output position assigned to the new glyph
26200 string constructed. HL overrides that face of the glyph; e.g. it
26201 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26202 right-most x-position of the drawing area. */
26204 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26205 do \
26207 int face_id; \
26208 XChar2b *char2b; \
26210 face_id = (row)->glyphs[area][START].face_id; \
26212 s = alloca (sizeof *s); \
26213 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26214 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26215 append_glyph_string (&HEAD, &TAIL, s); \
26216 s->x = (X); \
26217 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26219 while (false)
26222 /* Add a glyph string for a composite sequence to the list of strings
26223 between HEAD and TAIL. START is the index of the first glyph in
26224 row area AREA of glyph row ROW that is part of the new glyph
26225 string. END is the index of the last glyph in that glyph row area.
26226 X is the current output position assigned to the new glyph string
26227 constructed. HL overrides that face of the glyph; e.g. it is
26228 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26229 x-position of the drawing area. */
26231 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26232 do { \
26233 int face_id = (row)->glyphs[area][START].face_id; \
26234 struct face *base_face = FACE_FROM_ID (f, face_id); \
26235 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26236 struct composition *cmp = composition_table[cmp_id]; \
26237 XChar2b *char2b; \
26238 struct glyph_string *first_s = NULL; \
26239 int n; \
26241 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26243 /* Make glyph_strings for each glyph sequence that is drawable by \
26244 the same face, and append them to HEAD/TAIL. */ \
26245 for (n = 0; n < cmp->glyph_len;) \
26247 s = alloca (sizeof *s); \
26248 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26249 append_glyph_string (&(HEAD), &(TAIL), s); \
26250 s->cmp = cmp; \
26251 s->cmp_from = n; \
26252 s->x = (X); \
26253 if (n == 0) \
26254 first_s = s; \
26255 n = fill_composite_glyph_string (s, base_face, overlaps); \
26258 ++START; \
26259 s = first_s; \
26260 } while (false)
26263 /* Add a glyph string for a glyph-string sequence to the list of strings
26264 between HEAD and TAIL. */
26266 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26267 do { \
26268 int face_id; \
26269 XChar2b *char2b; \
26270 Lisp_Object gstring; \
26272 face_id = (row)->glyphs[area][START].face_id; \
26273 gstring = (composition_gstring_from_id \
26274 ((row)->glyphs[area][START].u.cmp.id)); \
26275 s = alloca (sizeof *s); \
26276 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26277 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26278 append_glyph_string (&(HEAD), &(TAIL), s); \
26279 s->x = (X); \
26280 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26281 } while (false)
26284 /* Add a glyph string for a sequence of glyphless character's glyphs
26285 to the list of strings between HEAD and TAIL. The meanings of
26286 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26288 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26289 do \
26291 int face_id; \
26293 face_id = (row)->glyphs[area][START].face_id; \
26295 s = alloca (sizeof *s); \
26296 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26297 append_glyph_string (&HEAD, &TAIL, s); \
26298 s->x = (X); \
26299 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26300 overlaps); \
26302 while (false)
26305 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26306 of AREA of glyph row ROW on window W between indices START and END.
26307 HL overrides the face for drawing glyph strings, e.g. it is
26308 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26309 x-positions of the drawing area.
26311 This is an ugly monster macro construct because we must use alloca
26312 to allocate glyph strings (because draw_glyphs can be called
26313 asynchronously). */
26315 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26316 do \
26318 HEAD = TAIL = NULL; \
26319 while (START < END) \
26321 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26322 switch (first_glyph->type) \
26324 case CHAR_GLYPH: \
26325 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26326 HL, X, LAST_X); \
26327 break; \
26329 case COMPOSITE_GLYPH: \
26330 if (first_glyph->u.cmp.automatic) \
26331 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26332 HL, X, LAST_X); \
26333 else \
26334 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26335 HL, X, LAST_X); \
26336 break; \
26338 case STRETCH_GLYPH: \
26339 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26340 HL, X, LAST_X); \
26341 break; \
26343 case IMAGE_GLYPH: \
26344 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26345 HL, X, LAST_X); \
26346 break;
26348 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26349 case XWIDGET_GLYPH: \
26350 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26351 HL, X, LAST_X); \
26352 break;
26354 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26355 case GLYPHLESS_GLYPH: \
26356 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26357 HL, X, LAST_X); \
26358 break; \
26360 default: \
26361 emacs_abort (); \
26364 if (s) \
26366 set_glyph_string_background_width (s, START, LAST_X); \
26367 (X) += s->width; \
26370 } while (false)
26373 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26374 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26375 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26376 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26379 /* Draw glyphs between START and END in AREA of ROW on window W,
26380 starting at x-position X. X is relative to AREA in W. HL is a
26381 face-override with the following meaning:
26383 DRAW_NORMAL_TEXT draw normally
26384 DRAW_CURSOR draw in cursor face
26385 DRAW_MOUSE_FACE draw in mouse face.
26386 DRAW_INVERSE_VIDEO draw in mode line face
26387 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26388 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26390 If OVERLAPS is non-zero, draw only the foreground of characters and
26391 clip to the physical height of ROW. Non-zero value also defines
26392 the overlapping part to be drawn:
26394 OVERLAPS_PRED overlap with preceding rows
26395 OVERLAPS_SUCC overlap with succeeding rows
26396 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26397 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26399 Value is the x-position reached, relative to AREA of W. */
26401 static int
26402 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26403 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26404 enum draw_glyphs_face hl, int overlaps)
26406 struct glyph_string *head, *tail;
26407 struct glyph_string *s;
26408 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26409 int i, j, x_reached, last_x, area_left = 0;
26410 struct frame *f = XFRAME (WINDOW_FRAME (w));
26411 DECLARE_HDC (hdc);
26413 ALLOCATE_HDC (hdc, f);
26415 /* Let's rather be paranoid than getting a SEGV. */
26416 end = min (end, row->used[area]);
26417 start = clip_to_bounds (0, start, end);
26419 /* Translate X to frame coordinates. Set last_x to the right
26420 end of the drawing area. */
26421 if (row->full_width_p)
26423 /* X is relative to the left edge of W, without scroll bars
26424 or fringes. */
26425 area_left = WINDOW_LEFT_EDGE_X (w);
26426 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26427 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26429 else
26431 area_left = window_box_left (w, area);
26432 last_x = area_left + window_box_width (w, area);
26434 x += area_left;
26436 /* Build a doubly-linked list of glyph_string structures between
26437 head and tail from what we have to draw. Note that the macro
26438 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26439 the reason we use a separate variable `i'. */
26440 i = start;
26441 USE_SAFE_ALLOCA;
26442 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26443 if (tail)
26445 s = glyph_string_containing_background_width (tail);
26446 x_reached = s->x + s->background_width;
26448 else
26449 x_reached = x;
26451 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26452 the row, redraw some glyphs in front or following the glyph
26453 strings built above. */
26454 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26456 struct glyph_string *h, *t;
26457 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26458 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26459 bool check_mouse_face = false;
26460 int dummy_x = 0;
26462 /* If mouse highlighting is on, we may need to draw adjacent
26463 glyphs using mouse-face highlighting. */
26464 if (area == TEXT_AREA && row->mouse_face_p
26465 && hlinfo->mouse_face_beg_row >= 0
26466 && hlinfo->mouse_face_end_row >= 0)
26468 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26470 if (row_vpos >= hlinfo->mouse_face_beg_row
26471 && row_vpos <= hlinfo->mouse_face_end_row)
26473 check_mouse_face = true;
26474 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26475 ? hlinfo->mouse_face_beg_col : 0;
26476 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26477 ? hlinfo->mouse_face_end_col
26478 : row->used[TEXT_AREA];
26482 /* Compute overhangs for all glyph strings. */
26483 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26484 for (s = head; s; s = s->next)
26485 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26487 /* Prepend glyph strings for glyphs in front of the first glyph
26488 string that are overwritten because of the first glyph
26489 string's left overhang. The background of all strings
26490 prepended must be drawn because the first glyph string
26491 draws over it. */
26492 i = left_overwritten (head);
26493 if (i >= 0)
26495 enum draw_glyphs_face overlap_hl;
26497 /* If this row contains mouse highlighting, attempt to draw
26498 the overlapped glyphs with the correct highlight. This
26499 code fails if the overlap encompasses more than one glyph
26500 and mouse-highlight spans only some of these glyphs.
26501 However, making it work perfectly involves a lot more
26502 code, and I don't know if the pathological case occurs in
26503 practice, so we'll stick to this for now. --- cyd */
26504 if (check_mouse_face
26505 && mouse_beg_col < start && mouse_end_col > i)
26506 overlap_hl = DRAW_MOUSE_FACE;
26507 else
26508 overlap_hl = DRAW_NORMAL_TEXT;
26510 if (hl != overlap_hl)
26511 clip_head = head;
26512 j = i;
26513 BUILD_GLYPH_STRINGS (j, start, h, t,
26514 overlap_hl, dummy_x, last_x);
26515 start = i;
26516 compute_overhangs_and_x (t, head->x, true);
26517 prepend_glyph_string_lists (&head, &tail, h, t);
26518 if (clip_head == NULL)
26519 clip_head = head;
26522 /* Prepend glyph strings for glyphs in front of the first glyph
26523 string that overwrite that glyph string because of their
26524 right overhang. For these strings, only the foreground must
26525 be drawn, because it draws over the glyph string at `head'.
26526 The background must not be drawn because this would overwrite
26527 right overhangs of preceding glyphs for which no glyph
26528 strings exist. */
26529 i = left_overwriting (head);
26530 if (i >= 0)
26532 enum draw_glyphs_face overlap_hl;
26534 if (check_mouse_face
26535 && mouse_beg_col < start && mouse_end_col > i)
26536 overlap_hl = DRAW_MOUSE_FACE;
26537 else
26538 overlap_hl = DRAW_NORMAL_TEXT;
26540 if (hl == overlap_hl || clip_head == NULL)
26541 clip_head = head;
26542 BUILD_GLYPH_STRINGS (i, start, h, t,
26543 overlap_hl, dummy_x, last_x);
26544 for (s = h; s; s = s->next)
26545 s->background_filled_p = true;
26546 compute_overhangs_and_x (t, head->x, true);
26547 prepend_glyph_string_lists (&head, &tail, h, t);
26550 /* Append glyphs strings for glyphs following the last glyph
26551 string tail that are overwritten by tail. The background of
26552 these strings has to be drawn because tail's foreground draws
26553 over it. */
26554 i = right_overwritten (tail);
26555 if (i >= 0)
26557 enum draw_glyphs_face overlap_hl;
26559 if (check_mouse_face
26560 && mouse_beg_col < i && mouse_end_col > end)
26561 overlap_hl = DRAW_MOUSE_FACE;
26562 else
26563 overlap_hl = DRAW_NORMAL_TEXT;
26565 if (hl != overlap_hl)
26566 clip_tail = tail;
26567 BUILD_GLYPH_STRINGS (end, i, h, t,
26568 overlap_hl, x, last_x);
26569 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26570 we don't have `end = i;' here. */
26571 compute_overhangs_and_x (h, tail->x + tail->width, false);
26572 append_glyph_string_lists (&head, &tail, h, t);
26573 if (clip_tail == NULL)
26574 clip_tail = tail;
26577 /* Append glyph strings for glyphs following the last glyph
26578 string tail that overwrite tail. The foreground of such
26579 glyphs has to be drawn because it writes into the background
26580 of tail. The background must not be drawn because it could
26581 paint over the foreground of following glyphs. */
26582 i = right_overwriting (tail);
26583 if (i >= 0)
26585 enum draw_glyphs_face overlap_hl;
26586 if (check_mouse_face
26587 && mouse_beg_col < i && mouse_end_col > end)
26588 overlap_hl = DRAW_MOUSE_FACE;
26589 else
26590 overlap_hl = DRAW_NORMAL_TEXT;
26592 if (hl == overlap_hl || clip_tail == NULL)
26593 clip_tail = tail;
26594 i++; /* We must include the Ith glyph. */
26595 BUILD_GLYPH_STRINGS (end, i, h, t,
26596 overlap_hl, x, last_x);
26597 for (s = h; s; s = s->next)
26598 s->background_filled_p = true;
26599 compute_overhangs_and_x (h, tail->x + tail->width, false);
26600 append_glyph_string_lists (&head, &tail, h, t);
26602 tail = glyph_string_containing_background_width (tail);
26603 if (clip_tail)
26604 clip_tail = glyph_string_containing_background_width (clip_tail);
26605 if (clip_head || clip_tail)
26606 for (s = head; s; s = s->next)
26608 s->clip_head = clip_head;
26609 s->clip_tail = clip_tail;
26613 /* Draw all strings. */
26614 for (s = head; s; s = s->next)
26615 FRAME_RIF (f)->draw_glyph_string (s);
26617 #ifndef HAVE_NS
26618 /* When focus a sole frame and move horizontally, this clears on_p
26619 causing a failure to erase prev cursor position. */
26620 if (area == TEXT_AREA
26621 && !row->full_width_p
26622 /* When drawing overlapping rows, only the glyph strings'
26623 foreground is drawn, which doesn't erase a cursor
26624 completely. */
26625 && !overlaps)
26627 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26628 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26629 : (tail ? tail->x + tail->background_width : x));
26630 x0 -= area_left;
26631 x1 -= area_left;
26633 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26634 row->y, MATRIX_ROW_BOTTOM_Y (row));
26636 #endif
26638 /* Value is the x-position up to which drawn, relative to AREA of W.
26639 This doesn't include parts drawn because of overhangs. */
26640 if (row->full_width_p)
26641 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26642 else
26643 x_reached -= area_left;
26645 RELEASE_HDC (hdc, f);
26647 SAFE_FREE ();
26648 return x_reached;
26651 /* Find the first glyph in the run of underlined glyphs preceding the
26652 beginning of glyph string S, and return its font (which could be
26653 NULL). This is needed because that font determines the underline
26654 position and thickness for the entire run of the underlined glyphs.
26655 This function is called from the draw_glyph_string method of GUI
26656 frame's redisplay interface (RIF) when it needs to draw in an
26657 underlined face. */
26658 struct font *
26659 font_for_underline_metrics (struct glyph_string *s)
26661 struct glyph *g0 = s->row->glyphs[s->area], *g;
26663 for (g = s->first_glyph - 1; g >= g0; g--)
26665 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26666 if (!(prev_face && prev_face->underline_p))
26667 break;
26670 /* If preceding glyphs are not underlined, use the font of S. */
26671 if (g == s->first_glyph - 1)
26672 return s->font;
26673 else
26675 /* Otherwise use the font of the last glyph we saw in the above
26676 loop whose face had the underline_p flag set. */
26677 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26681 /* Expand row matrix if too narrow. Don't expand if area
26682 is not present. */
26684 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26686 if (!it->f->fonts_changed \
26687 && (it->glyph_row->glyphs[area] \
26688 < it->glyph_row->glyphs[area + 1])) \
26690 it->w->ncols_scale_factor++; \
26691 it->f->fonts_changed = true; \
26695 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26696 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26698 static void
26699 append_glyph (struct it *it)
26701 struct glyph *glyph;
26702 enum glyph_row_area area = it->area;
26704 eassert (it->glyph_row);
26705 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26707 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26708 if (glyph < it->glyph_row->glyphs[area + 1])
26710 /* If the glyph row is reversed, we need to prepend the glyph
26711 rather than append it. */
26712 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26714 struct glyph *g;
26716 /* Make room for the additional glyph. */
26717 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26718 g[1] = *g;
26719 glyph = it->glyph_row->glyphs[area];
26721 glyph->charpos = CHARPOS (it->position);
26722 glyph->object = it->object;
26723 if (it->pixel_width > 0)
26725 eassert (it->pixel_width <= SHRT_MAX);
26726 glyph->pixel_width = it->pixel_width;
26727 glyph->padding_p = false;
26729 else
26731 /* Assure at least 1-pixel width. Otherwise, cursor can't
26732 be displayed correctly. */
26733 glyph->pixel_width = 1;
26734 glyph->padding_p = true;
26736 glyph->ascent = it->ascent;
26737 glyph->descent = it->descent;
26738 glyph->voffset = it->voffset;
26739 glyph->type = CHAR_GLYPH;
26740 glyph->avoid_cursor_p = it->avoid_cursor_p;
26741 glyph->multibyte_p = it->multibyte_p;
26742 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26744 /* In R2L rows, the left and the right box edges need to be
26745 drawn in reverse direction. */
26746 glyph->right_box_line_p = it->start_of_box_run_p;
26747 glyph->left_box_line_p = it->end_of_box_run_p;
26749 else
26751 glyph->left_box_line_p = it->start_of_box_run_p;
26752 glyph->right_box_line_p = it->end_of_box_run_p;
26754 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26755 || it->phys_descent > it->descent);
26756 glyph->glyph_not_available_p = it->glyph_not_available_p;
26757 glyph->face_id = it->face_id;
26758 glyph->u.ch = it->char_to_display;
26759 glyph->slice.img = null_glyph_slice;
26760 glyph->font_type = FONT_TYPE_UNKNOWN;
26761 if (it->bidi_p)
26763 glyph->resolved_level = it->bidi_it.resolved_level;
26764 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26765 glyph->bidi_type = it->bidi_it.type;
26767 else
26769 glyph->resolved_level = 0;
26770 glyph->bidi_type = UNKNOWN_BT;
26772 ++it->glyph_row->used[area];
26774 else
26775 IT_EXPAND_MATRIX_WIDTH (it, area);
26778 /* Store one glyph for the composition IT->cmp_it.id in
26779 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26780 non-null. */
26782 static void
26783 append_composite_glyph (struct it *it)
26785 struct glyph *glyph;
26786 enum glyph_row_area area = it->area;
26788 eassert (it->glyph_row);
26790 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26791 if (glyph < it->glyph_row->glyphs[area + 1])
26793 /* If the glyph row is reversed, we need to prepend the glyph
26794 rather than append it. */
26795 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26797 struct glyph *g;
26799 /* Make room for the new glyph. */
26800 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26801 g[1] = *g;
26802 glyph = it->glyph_row->glyphs[it->area];
26804 glyph->charpos = it->cmp_it.charpos;
26805 glyph->object = it->object;
26806 eassert (it->pixel_width <= SHRT_MAX);
26807 glyph->pixel_width = it->pixel_width;
26808 glyph->ascent = it->ascent;
26809 glyph->descent = it->descent;
26810 glyph->voffset = it->voffset;
26811 glyph->type = COMPOSITE_GLYPH;
26812 if (it->cmp_it.ch < 0)
26814 glyph->u.cmp.automatic = false;
26815 glyph->u.cmp.id = it->cmp_it.id;
26816 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26818 else
26820 glyph->u.cmp.automatic = true;
26821 glyph->u.cmp.id = it->cmp_it.id;
26822 glyph->slice.cmp.from = it->cmp_it.from;
26823 glyph->slice.cmp.to = it->cmp_it.to - 1;
26825 glyph->avoid_cursor_p = it->avoid_cursor_p;
26826 glyph->multibyte_p = it->multibyte_p;
26827 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26829 /* In R2L rows, the left and the right box edges need to be
26830 drawn in reverse direction. */
26831 glyph->right_box_line_p = it->start_of_box_run_p;
26832 glyph->left_box_line_p = it->end_of_box_run_p;
26834 else
26836 glyph->left_box_line_p = it->start_of_box_run_p;
26837 glyph->right_box_line_p = it->end_of_box_run_p;
26839 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26840 || it->phys_descent > it->descent);
26841 glyph->padding_p = false;
26842 glyph->glyph_not_available_p = false;
26843 glyph->face_id = it->face_id;
26844 glyph->font_type = FONT_TYPE_UNKNOWN;
26845 if (it->bidi_p)
26847 glyph->resolved_level = it->bidi_it.resolved_level;
26848 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26849 glyph->bidi_type = it->bidi_it.type;
26851 ++it->glyph_row->used[area];
26853 else
26854 IT_EXPAND_MATRIX_WIDTH (it, area);
26858 /* Change IT->ascent and IT->height according to the setting of
26859 IT->voffset. */
26861 static void
26862 take_vertical_position_into_account (struct it *it)
26864 if (it->voffset)
26866 if (it->voffset < 0)
26867 /* Increase the ascent so that we can display the text higher
26868 in the line. */
26869 it->ascent -= it->voffset;
26870 else
26871 /* Increase the descent so that we can display the text lower
26872 in the line. */
26873 it->descent += it->voffset;
26878 /* Produce glyphs/get display metrics for the image IT is loaded with.
26879 See the description of struct display_iterator in dispextern.h for
26880 an overview of struct display_iterator. */
26882 static void
26883 produce_image_glyph (struct it *it)
26885 struct image *img;
26886 struct face *face;
26887 int glyph_ascent, crop;
26888 struct glyph_slice slice;
26890 eassert (it->what == IT_IMAGE);
26892 face = FACE_FROM_ID (it->f, it->face_id);
26893 /* Make sure X resources of the face is loaded. */
26894 prepare_face_for_display (it->f, face);
26896 if (it->image_id < 0)
26898 /* Fringe bitmap. */
26899 it->ascent = it->phys_ascent = 0;
26900 it->descent = it->phys_descent = 0;
26901 it->pixel_width = 0;
26902 it->nglyphs = 0;
26903 return;
26906 img = IMAGE_FROM_ID (it->f, it->image_id);
26907 /* Make sure X resources of the image is loaded. */
26908 prepare_image_for_display (it->f, img);
26910 slice.x = slice.y = 0;
26911 slice.width = img->width;
26912 slice.height = img->height;
26914 if (INTEGERP (it->slice.x))
26915 slice.x = XINT (it->slice.x);
26916 else if (FLOATP (it->slice.x))
26917 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26919 if (INTEGERP (it->slice.y))
26920 slice.y = XINT (it->slice.y);
26921 else if (FLOATP (it->slice.y))
26922 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26924 if (INTEGERP (it->slice.width))
26925 slice.width = XINT (it->slice.width);
26926 else if (FLOATP (it->slice.width))
26927 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26929 if (INTEGERP (it->slice.height))
26930 slice.height = XINT (it->slice.height);
26931 else if (FLOATP (it->slice.height))
26932 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26934 if (slice.x >= img->width)
26935 slice.x = img->width;
26936 if (slice.y >= img->height)
26937 slice.y = img->height;
26938 if (slice.x + slice.width >= img->width)
26939 slice.width = img->width - slice.x;
26940 if (slice.y + slice.height > img->height)
26941 slice.height = img->height - slice.y;
26943 if (slice.width == 0 || slice.height == 0)
26944 return;
26946 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26948 it->descent = slice.height - glyph_ascent;
26949 if (slice.y == 0)
26950 it->descent += img->vmargin;
26951 if (slice.y + slice.height == img->height)
26952 it->descent += img->vmargin;
26953 it->phys_descent = it->descent;
26955 it->pixel_width = slice.width;
26956 if (slice.x == 0)
26957 it->pixel_width += img->hmargin;
26958 if (slice.x + slice.width == img->width)
26959 it->pixel_width += img->hmargin;
26961 /* It's quite possible for images to have an ascent greater than
26962 their height, so don't get confused in that case. */
26963 if (it->descent < 0)
26964 it->descent = 0;
26966 it->nglyphs = 1;
26968 if (face->box != FACE_NO_BOX)
26970 if (face->box_line_width > 0)
26972 if (slice.y == 0)
26973 it->ascent += face->box_line_width;
26974 if (slice.y + slice.height == img->height)
26975 it->descent += face->box_line_width;
26978 if (it->start_of_box_run_p && slice.x == 0)
26979 it->pixel_width += eabs (face->box_line_width);
26980 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26981 it->pixel_width += eabs (face->box_line_width);
26984 take_vertical_position_into_account (it);
26986 /* Automatically crop wide image glyphs at right edge so we can
26987 draw the cursor on same display row. */
26988 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26989 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26991 it->pixel_width -= crop;
26992 slice.width -= crop;
26995 if (it->glyph_row)
26997 struct glyph *glyph;
26998 enum glyph_row_area area = it->area;
27000 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27001 if (it->glyph_row->reversed_p)
27003 struct glyph *g;
27005 /* Make room for the new glyph. */
27006 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27007 g[1] = *g;
27008 glyph = it->glyph_row->glyphs[it->area];
27010 if (glyph < it->glyph_row->glyphs[area + 1])
27012 glyph->charpos = CHARPOS (it->position);
27013 glyph->object = it->object;
27014 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27015 glyph->ascent = glyph_ascent;
27016 glyph->descent = it->descent;
27017 glyph->voffset = it->voffset;
27018 glyph->type = IMAGE_GLYPH;
27019 glyph->avoid_cursor_p = it->avoid_cursor_p;
27020 glyph->multibyte_p = it->multibyte_p;
27021 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27023 /* In R2L rows, the left and the right box edges need to be
27024 drawn in reverse direction. */
27025 glyph->right_box_line_p = it->start_of_box_run_p;
27026 glyph->left_box_line_p = it->end_of_box_run_p;
27028 else
27030 glyph->left_box_line_p = it->start_of_box_run_p;
27031 glyph->right_box_line_p = it->end_of_box_run_p;
27033 glyph->overlaps_vertically_p = false;
27034 glyph->padding_p = false;
27035 glyph->glyph_not_available_p = false;
27036 glyph->face_id = it->face_id;
27037 glyph->u.img_id = img->id;
27038 glyph->slice.img = slice;
27039 glyph->font_type = FONT_TYPE_UNKNOWN;
27040 if (it->bidi_p)
27042 glyph->resolved_level = it->bidi_it.resolved_level;
27043 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27044 glyph->bidi_type = it->bidi_it.type;
27046 ++it->glyph_row->used[area];
27048 else
27049 IT_EXPAND_MATRIX_WIDTH (it, area);
27053 static void
27054 produce_xwidget_glyph (struct it *it)
27056 #ifdef HAVE_XWIDGETS
27057 struct xwidget *xw;
27058 int glyph_ascent, crop;
27059 eassert (it->what == IT_XWIDGET);
27061 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27062 /* Make sure X resources of the face is loaded. */
27063 prepare_face_for_display (it->f, face);
27065 xw = it->xwidget;
27066 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27067 it->descent = xw->height/2;
27068 it->phys_descent = it->descent;
27069 it->pixel_width = xw->width;
27070 /* It's quite possible for images to have an ascent greater than
27071 their height, so don't get confused in that case. */
27072 if (it->descent < 0)
27073 it->descent = 0;
27075 it->nglyphs = 1;
27077 if (face->box != FACE_NO_BOX)
27079 if (face->box_line_width > 0)
27081 it->ascent += face->box_line_width;
27082 it->descent += face->box_line_width;
27085 if (it->start_of_box_run_p)
27086 it->pixel_width += eabs (face->box_line_width);
27087 it->pixel_width += eabs (face->box_line_width);
27090 take_vertical_position_into_account (it);
27092 /* Automatically crop wide image glyphs at right edge so we can
27093 draw the cursor on same display row. */
27094 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27095 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27096 it->pixel_width -= crop;
27098 if (it->glyph_row)
27100 enum glyph_row_area area = it->area;
27101 struct glyph *glyph
27102 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27104 if (it->glyph_row->reversed_p)
27106 struct glyph *g;
27108 /* Make room for the new glyph. */
27109 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27110 g[1] = *g;
27111 glyph = it->glyph_row->glyphs[it->area];
27113 if (glyph < it->glyph_row->glyphs[area + 1])
27115 glyph->charpos = CHARPOS (it->position);
27116 glyph->object = it->object;
27117 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27118 glyph->ascent = glyph_ascent;
27119 glyph->descent = it->descent;
27120 glyph->voffset = it->voffset;
27121 glyph->type = XWIDGET_GLYPH;
27122 glyph->avoid_cursor_p = it->avoid_cursor_p;
27123 glyph->multibyte_p = it->multibyte_p;
27124 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27126 /* In R2L rows, the left and the right box edges need to be
27127 drawn in reverse direction. */
27128 glyph->right_box_line_p = it->start_of_box_run_p;
27129 glyph->left_box_line_p = it->end_of_box_run_p;
27131 else
27133 glyph->left_box_line_p = it->start_of_box_run_p;
27134 glyph->right_box_line_p = it->end_of_box_run_p;
27136 glyph->overlaps_vertically_p = 0;
27137 glyph->padding_p = 0;
27138 glyph->glyph_not_available_p = 0;
27139 glyph->face_id = it->face_id;
27140 glyph->u.xwidget = it->xwidget;
27141 glyph->font_type = FONT_TYPE_UNKNOWN;
27142 if (it->bidi_p)
27144 glyph->resolved_level = it->bidi_it.resolved_level;
27145 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27146 glyph->bidi_type = it->bidi_it.type;
27148 ++it->glyph_row->used[area];
27150 else
27151 IT_EXPAND_MATRIX_WIDTH (it, area);
27153 #endif
27156 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27157 of the glyph, WIDTH and HEIGHT are the width and height of the
27158 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27160 static void
27161 append_stretch_glyph (struct it *it, Lisp_Object object,
27162 int width, int height, int ascent)
27164 struct glyph *glyph;
27165 enum glyph_row_area area = it->area;
27167 eassert (ascent >= 0 && ascent <= height);
27169 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27170 if (glyph < it->glyph_row->glyphs[area + 1])
27172 /* If the glyph row is reversed, we need to prepend the glyph
27173 rather than append it. */
27174 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27176 struct glyph *g;
27178 /* Make room for the additional glyph. */
27179 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27180 g[1] = *g;
27181 glyph = it->glyph_row->glyphs[area];
27183 /* Decrease the width of the first glyph of the row that
27184 begins before first_visible_x (e.g., due to hscroll).
27185 This is so the overall width of the row becomes smaller
27186 by the scroll amount, and the stretch glyph appended by
27187 extend_face_to_end_of_line will be wider, to shift the
27188 row glyphs to the right. (In L2R rows, the corresponding
27189 left-shift effect is accomplished by setting row->x to a
27190 negative value, which won't work with R2L rows.)
27192 This must leave us with a positive value of WIDTH, since
27193 otherwise the call to move_it_in_display_line_to at the
27194 beginning of display_line would have got past the entire
27195 first glyph, and then it->current_x would have been
27196 greater or equal to it->first_visible_x. */
27197 if (it->current_x < it->first_visible_x)
27198 width -= it->first_visible_x - it->current_x;
27199 eassert (width > 0);
27201 glyph->charpos = CHARPOS (it->position);
27202 glyph->object = object;
27203 /* FIXME: It would be better to use TYPE_MAX here, but
27204 __typeof__ is not portable enough... */
27205 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27206 glyph->ascent = ascent;
27207 glyph->descent = height - ascent;
27208 glyph->voffset = it->voffset;
27209 glyph->type = STRETCH_GLYPH;
27210 glyph->avoid_cursor_p = it->avoid_cursor_p;
27211 glyph->multibyte_p = it->multibyte_p;
27212 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27214 /* In R2L rows, the left and the right box edges need to be
27215 drawn in reverse direction. */
27216 glyph->right_box_line_p = it->start_of_box_run_p;
27217 glyph->left_box_line_p = it->end_of_box_run_p;
27219 else
27221 glyph->left_box_line_p = it->start_of_box_run_p;
27222 glyph->right_box_line_p = it->end_of_box_run_p;
27224 glyph->overlaps_vertically_p = false;
27225 glyph->padding_p = false;
27226 glyph->glyph_not_available_p = false;
27227 glyph->face_id = it->face_id;
27228 glyph->u.stretch.ascent = ascent;
27229 glyph->u.stretch.height = height;
27230 glyph->slice.img = null_glyph_slice;
27231 glyph->font_type = FONT_TYPE_UNKNOWN;
27232 if (it->bidi_p)
27234 glyph->resolved_level = it->bidi_it.resolved_level;
27235 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27236 glyph->bidi_type = it->bidi_it.type;
27238 else
27240 glyph->resolved_level = 0;
27241 glyph->bidi_type = UNKNOWN_BT;
27243 ++it->glyph_row->used[area];
27245 else
27246 IT_EXPAND_MATRIX_WIDTH (it, area);
27249 #endif /* HAVE_WINDOW_SYSTEM */
27251 /* Produce a stretch glyph for iterator IT. IT->object is the value
27252 of the glyph property displayed. The value must be a list
27253 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27254 being recognized:
27256 1. `:width WIDTH' specifies that the space should be WIDTH *
27257 canonical char width wide. WIDTH may be an integer or floating
27258 point number.
27260 2. `:relative-width FACTOR' specifies that the width of the stretch
27261 should be computed from the width of the first character having the
27262 `glyph' property, and should be FACTOR times that width.
27264 3. `:align-to HPOS' specifies that the space should be wide enough
27265 to reach HPOS, a value in canonical character units.
27267 Exactly one of the above pairs must be present.
27269 4. `:height HEIGHT' specifies that the height of the stretch produced
27270 should be HEIGHT, measured in canonical character units.
27272 5. `:relative-height FACTOR' specifies that the height of the
27273 stretch should be FACTOR times the height of the characters having
27274 the glyph property.
27276 Either none or exactly one of 4 or 5 must be present.
27278 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27279 of the stretch should be used for the ascent of the stretch.
27280 ASCENT must be in the range 0 <= ASCENT <= 100. */
27282 void
27283 produce_stretch_glyph (struct it *it)
27285 /* (space :width WIDTH :height HEIGHT ...) */
27286 Lisp_Object prop, plist;
27287 int width = 0, height = 0, align_to = -1;
27288 bool zero_width_ok_p = false;
27289 double tem;
27290 struct font *font = NULL;
27292 #ifdef HAVE_WINDOW_SYSTEM
27293 int ascent = 0;
27294 bool zero_height_ok_p = false;
27296 if (FRAME_WINDOW_P (it->f))
27298 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27299 font = face->font ? face->font : FRAME_FONT (it->f);
27300 prepare_face_for_display (it->f, face);
27302 #endif
27304 /* List should start with `space'. */
27305 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27306 plist = XCDR (it->object);
27308 /* Compute the width of the stretch. */
27309 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27310 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27312 /* Absolute width `:width WIDTH' specified and valid. */
27313 zero_width_ok_p = true;
27314 width = (int)tem;
27316 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27318 /* Relative width `:relative-width FACTOR' specified and valid.
27319 Compute the width of the characters having the `glyph'
27320 property. */
27321 struct it it2;
27322 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27324 it2 = *it;
27325 if (it->multibyte_p)
27326 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27327 else
27329 it2.c = it2.char_to_display = *p, it2.len = 1;
27330 if (! ASCII_CHAR_P (it2.c))
27331 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27334 it2.glyph_row = NULL;
27335 it2.what = IT_CHARACTER;
27336 PRODUCE_GLYPHS (&it2);
27337 width = NUMVAL (prop) * it2.pixel_width;
27339 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27340 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27341 &align_to))
27343 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27344 align_to = (align_to < 0
27346 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27347 else if (align_to < 0)
27348 align_to = window_box_left_offset (it->w, TEXT_AREA);
27349 width = max (0, (int)tem + align_to - it->current_x);
27350 zero_width_ok_p = true;
27352 else
27353 /* Nothing specified -> width defaults to canonical char width. */
27354 width = FRAME_COLUMN_WIDTH (it->f);
27356 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27357 width = 1;
27359 #ifdef HAVE_WINDOW_SYSTEM
27360 /* Compute height. */
27361 if (FRAME_WINDOW_P (it->f))
27363 int default_height = normal_char_height (font, ' ');
27365 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27366 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27368 height = (int)tem;
27369 zero_height_ok_p = true;
27371 else if (prop = Fplist_get (plist, QCrelative_height),
27372 NUMVAL (prop) > 0)
27373 height = default_height * NUMVAL (prop);
27374 else
27375 height = default_height;
27377 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27378 height = 1;
27380 /* Compute percentage of height used for ascent. If
27381 `:ascent ASCENT' is present and valid, use that. Otherwise,
27382 derive the ascent from the font in use. */
27383 if (prop = Fplist_get (plist, QCascent),
27384 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27385 ascent = height * NUMVAL (prop) / 100.0;
27386 else if (!NILP (prop)
27387 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27388 ascent = min (max (0, (int)tem), height);
27389 else
27390 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27392 else
27393 #endif /* HAVE_WINDOW_SYSTEM */
27394 height = 1;
27396 if (width > 0 && it->line_wrap != TRUNCATE
27397 && it->current_x + width > it->last_visible_x)
27399 width = it->last_visible_x - it->current_x;
27400 #ifdef HAVE_WINDOW_SYSTEM
27401 /* Subtract one more pixel from the stretch width, but only on
27402 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27403 width -= FRAME_WINDOW_P (it->f);
27404 #endif
27407 if (width > 0 && height > 0 && it->glyph_row)
27409 Lisp_Object o_object = it->object;
27410 Lisp_Object object = it->stack[it->sp - 1].string;
27411 int n = width;
27413 if (!STRINGP (object))
27414 object = it->w->contents;
27415 #ifdef HAVE_WINDOW_SYSTEM
27416 if (FRAME_WINDOW_P (it->f))
27417 append_stretch_glyph (it, object, width, height, ascent);
27418 else
27419 #endif
27421 it->object = object;
27422 it->char_to_display = ' ';
27423 it->pixel_width = it->len = 1;
27424 while (n--)
27425 tty_append_glyph (it);
27426 it->object = o_object;
27430 it->pixel_width = width;
27431 #ifdef HAVE_WINDOW_SYSTEM
27432 if (FRAME_WINDOW_P (it->f))
27434 it->ascent = it->phys_ascent = ascent;
27435 it->descent = it->phys_descent = height - it->ascent;
27436 it->nglyphs = width > 0 && height > 0;
27437 take_vertical_position_into_account (it);
27439 else
27440 #endif
27441 it->nglyphs = width;
27444 /* Get information about special display element WHAT in an
27445 environment described by IT. WHAT is one of IT_TRUNCATION or
27446 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27447 non-null glyph_row member. This function ensures that fields like
27448 face_id, c, len of IT are left untouched. */
27450 static void
27451 produce_special_glyphs (struct it *it, enum display_element_type what)
27453 struct it temp_it;
27454 Lisp_Object gc;
27455 GLYPH glyph;
27457 temp_it = *it;
27458 temp_it.object = Qnil;
27459 memset (&temp_it.current, 0, sizeof temp_it.current);
27461 if (what == IT_CONTINUATION)
27463 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27464 if (it->bidi_it.paragraph_dir == R2L)
27465 SET_GLYPH_FROM_CHAR (glyph, '/');
27466 else
27467 SET_GLYPH_FROM_CHAR (glyph, '\\');
27468 if (it->dp
27469 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27471 /* FIXME: Should we mirror GC for R2L lines? */
27472 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27473 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27476 else if (what == IT_TRUNCATION)
27478 /* Truncation glyph. */
27479 SET_GLYPH_FROM_CHAR (glyph, '$');
27480 if (it->dp
27481 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27483 /* FIXME: Should we mirror GC for R2L lines? */
27484 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27485 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27488 else
27489 emacs_abort ();
27491 #ifdef HAVE_WINDOW_SYSTEM
27492 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27493 is turned off, we precede the truncation/continuation glyphs by a
27494 stretch glyph whose width is computed such that these special
27495 glyphs are aligned at the window margin, even when very different
27496 fonts are used in different glyph rows. */
27497 if (FRAME_WINDOW_P (temp_it.f)
27498 /* init_iterator calls this with it->glyph_row == NULL, and it
27499 wants only the pixel width of the truncation/continuation
27500 glyphs. */
27501 && temp_it.glyph_row
27502 /* insert_left_trunc_glyphs calls us at the beginning of the
27503 row, and it has its own calculation of the stretch glyph
27504 width. */
27505 && temp_it.glyph_row->used[TEXT_AREA] > 0
27506 && (temp_it.glyph_row->reversed_p
27507 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27508 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27510 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27512 if (stretch_width > 0)
27514 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27515 struct font *font =
27516 face->font ? face->font : FRAME_FONT (temp_it.f);
27517 int stretch_ascent =
27518 (((temp_it.ascent + temp_it.descent)
27519 * FONT_BASE (font)) / FONT_HEIGHT (font));
27521 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27522 temp_it.ascent + temp_it.descent,
27523 stretch_ascent);
27526 #endif
27528 temp_it.dp = NULL;
27529 temp_it.what = IT_CHARACTER;
27530 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27531 temp_it.face_id = GLYPH_FACE (glyph);
27532 temp_it.len = CHAR_BYTES (temp_it.c);
27534 PRODUCE_GLYPHS (&temp_it);
27535 it->pixel_width = temp_it.pixel_width;
27536 it->nglyphs = temp_it.nglyphs;
27539 #ifdef HAVE_WINDOW_SYSTEM
27541 /* Calculate line-height and line-spacing properties.
27542 An integer value specifies explicit pixel value.
27543 A float value specifies relative value to current face height.
27544 A cons (float . face-name) specifies relative value to
27545 height of specified face font.
27547 Returns height in pixels, or nil. */
27549 static Lisp_Object
27550 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27551 int boff, bool override)
27553 Lisp_Object face_name = Qnil;
27554 int ascent, descent, height;
27556 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27557 return val;
27559 if (CONSP (val))
27561 face_name = XCAR (val);
27562 val = XCDR (val);
27563 if (!NUMBERP (val))
27564 val = make_number (1);
27565 if (NILP (face_name))
27567 height = it->ascent + it->descent;
27568 goto scale;
27572 if (NILP (face_name))
27574 font = FRAME_FONT (it->f);
27575 boff = FRAME_BASELINE_OFFSET (it->f);
27577 else if (EQ (face_name, Qt))
27579 override = false;
27581 else
27583 int face_id;
27584 struct face *face;
27586 face_id = lookup_named_face (it->f, face_name, false);
27587 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27588 if (face == NULL || ((font = face->font) == NULL))
27589 return make_number (-1);
27590 boff = font->baseline_offset;
27591 if (font->vertical_centering)
27592 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27595 normal_char_ascent_descent (font, -1, &ascent, &descent);
27597 if (override)
27599 it->override_ascent = ascent;
27600 it->override_descent = descent;
27601 it->override_boff = boff;
27604 height = ascent + descent;
27606 scale:
27607 if (FLOATP (val))
27608 height = (int)(XFLOAT_DATA (val) * height);
27609 else if (INTEGERP (val))
27610 height *= XINT (val);
27612 return make_number (height);
27616 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27617 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27618 and only if this is for a character for which no font was found.
27620 If the display method (it->glyphless_method) is
27621 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27622 length of the acronym or the hexadecimal string, UPPER_XOFF and
27623 UPPER_YOFF are pixel offsets for the upper part of the string,
27624 LOWER_XOFF and LOWER_YOFF are for the lower part.
27626 For the other display methods, LEN through LOWER_YOFF are zero. */
27628 static void
27629 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27630 short upper_xoff, short upper_yoff,
27631 short lower_xoff, short lower_yoff)
27633 struct glyph *glyph;
27634 enum glyph_row_area area = it->area;
27636 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27637 if (glyph < it->glyph_row->glyphs[area + 1])
27639 /* If the glyph row is reversed, we need to prepend the glyph
27640 rather than append it. */
27641 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27643 struct glyph *g;
27645 /* Make room for the additional glyph. */
27646 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27647 g[1] = *g;
27648 glyph = it->glyph_row->glyphs[area];
27650 glyph->charpos = CHARPOS (it->position);
27651 glyph->object = it->object;
27652 eassert (it->pixel_width <= SHRT_MAX);
27653 glyph->pixel_width = it->pixel_width;
27654 glyph->ascent = it->ascent;
27655 glyph->descent = it->descent;
27656 glyph->voffset = it->voffset;
27657 glyph->type = GLYPHLESS_GLYPH;
27658 glyph->u.glyphless.method = it->glyphless_method;
27659 glyph->u.glyphless.for_no_font = for_no_font;
27660 glyph->u.glyphless.len = len;
27661 glyph->u.glyphless.ch = it->c;
27662 glyph->slice.glyphless.upper_xoff = upper_xoff;
27663 glyph->slice.glyphless.upper_yoff = upper_yoff;
27664 glyph->slice.glyphless.lower_xoff = lower_xoff;
27665 glyph->slice.glyphless.lower_yoff = lower_yoff;
27666 glyph->avoid_cursor_p = it->avoid_cursor_p;
27667 glyph->multibyte_p = it->multibyte_p;
27668 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27670 /* In R2L rows, the left and the right box edges need to be
27671 drawn in reverse direction. */
27672 glyph->right_box_line_p = it->start_of_box_run_p;
27673 glyph->left_box_line_p = it->end_of_box_run_p;
27675 else
27677 glyph->left_box_line_p = it->start_of_box_run_p;
27678 glyph->right_box_line_p = it->end_of_box_run_p;
27680 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27681 || it->phys_descent > it->descent);
27682 glyph->padding_p = false;
27683 glyph->glyph_not_available_p = false;
27684 glyph->face_id = face_id;
27685 glyph->font_type = FONT_TYPE_UNKNOWN;
27686 if (it->bidi_p)
27688 glyph->resolved_level = it->bidi_it.resolved_level;
27689 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27690 glyph->bidi_type = it->bidi_it.type;
27692 ++it->glyph_row->used[area];
27694 else
27695 IT_EXPAND_MATRIX_WIDTH (it, area);
27699 /* Produce a glyph for a glyphless character for iterator IT.
27700 IT->glyphless_method specifies which method to use for displaying
27701 the character. See the description of enum
27702 glyphless_display_method in dispextern.h for the detail.
27704 FOR_NO_FONT is true if and only if this is for a character for
27705 which no font was found. ACRONYM, if non-nil, is an acronym string
27706 for the character. */
27708 static void
27709 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27711 int face_id;
27712 struct face *face;
27713 struct font *font;
27714 int base_width, base_height, width, height;
27715 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27716 int len;
27718 /* Get the metrics of the base font. We always refer to the current
27719 ASCII face. */
27720 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27721 font = face->font ? face->font : FRAME_FONT (it->f);
27722 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27723 it->ascent += font->baseline_offset;
27724 it->descent -= font->baseline_offset;
27725 base_height = it->ascent + it->descent;
27726 base_width = font->average_width;
27728 face_id = merge_glyphless_glyph_face (it);
27730 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27732 it->pixel_width = THIN_SPACE_WIDTH;
27733 len = 0;
27734 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27736 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27738 width = CHARACTER_WIDTH (it->c);
27739 if (width == 0)
27740 width = 1;
27741 else if (width > 4)
27742 width = 4;
27743 it->pixel_width = base_width * width;
27744 len = 0;
27745 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27747 else
27749 char buf[7];
27750 const char *str;
27751 unsigned int code[6];
27752 int upper_len;
27753 int ascent, descent;
27754 struct font_metrics metrics_upper, metrics_lower;
27756 face = FACE_FROM_ID (it->f, face_id);
27757 font = face->font ? face->font : FRAME_FONT (it->f);
27758 prepare_face_for_display (it->f, face);
27760 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27762 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27763 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27764 if (CONSP (acronym))
27765 acronym = XCAR (acronym);
27766 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27768 else
27770 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27771 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27772 str = buf;
27774 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27775 code[len] = font->driver->encode_char (font, str[len]);
27776 upper_len = (len + 1) / 2;
27777 font->driver->text_extents (font, code, upper_len,
27778 &metrics_upper);
27779 font->driver->text_extents (font, code + upper_len, len - upper_len,
27780 &metrics_lower);
27784 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27785 width = max (metrics_upper.width, metrics_lower.width) + 4;
27786 upper_xoff = upper_yoff = 2; /* the typical case */
27787 if (base_width >= width)
27789 /* Align the upper to the left, the lower to the right. */
27790 it->pixel_width = base_width;
27791 lower_xoff = base_width - 2 - metrics_lower.width;
27793 else
27795 /* Center the shorter one. */
27796 it->pixel_width = width;
27797 if (metrics_upper.width >= metrics_lower.width)
27798 lower_xoff = (width - metrics_lower.width) / 2;
27799 else
27801 /* FIXME: This code doesn't look right. It formerly was
27802 missing the "lower_xoff = 0;", which couldn't have
27803 been right since it left lower_xoff uninitialized. */
27804 lower_xoff = 0;
27805 upper_xoff = (width - metrics_upper.width) / 2;
27809 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27810 top, bottom, and between upper and lower strings. */
27811 height = (metrics_upper.ascent + metrics_upper.descent
27812 + metrics_lower.ascent + metrics_lower.descent) + 5;
27813 /* Center vertically.
27814 H:base_height, D:base_descent
27815 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27817 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27818 descent = D - H/2 + h/2;
27819 lower_yoff = descent - 2 - ld;
27820 upper_yoff = lower_yoff - la - 1 - ud; */
27821 ascent = - (it->descent - (base_height + height + 1) / 2);
27822 descent = it->descent - (base_height - height) / 2;
27823 lower_yoff = descent - 2 - metrics_lower.descent;
27824 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27825 - metrics_upper.descent);
27826 /* Don't make the height shorter than the base height. */
27827 if (height > base_height)
27829 it->ascent = ascent;
27830 it->descent = descent;
27834 it->phys_ascent = it->ascent;
27835 it->phys_descent = it->descent;
27836 if (it->glyph_row)
27837 append_glyphless_glyph (it, face_id, for_no_font, len,
27838 upper_xoff, upper_yoff,
27839 lower_xoff, lower_yoff);
27840 it->nglyphs = 1;
27841 take_vertical_position_into_account (it);
27845 /* RIF:
27846 Produce glyphs/get display metrics for the display element IT is
27847 loaded with. See the description of struct it in dispextern.h
27848 for an overview of struct it. */
27850 void
27851 x_produce_glyphs (struct it *it)
27853 int extra_line_spacing = it->extra_line_spacing;
27855 it->glyph_not_available_p = false;
27857 if (it->what == IT_CHARACTER)
27859 XChar2b char2b;
27860 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27861 struct font *font = face->font;
27862 struct font_metrics *pcm = NULL;
27863 int boff; /* Baseline offset. */
27865 if (font == NULL)
27867 /* When no suitable font is found, display this character by
27868 the method specified in the first extra slot of
27869 Vglyphless_char_display. */
27870 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27872 eassert (it->what == IT_GLYPHLESS);
27873 produce_glyphless_glyph (it, true,
27874 STRINGP (acronym) ? acronym : Qnil);
27875 goto done;
27878 boff = font->baseline_offset;
27879 if (font->vertical_centering)
27880 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27882 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27884 it->nglyphs = 1;
27886 if (it->override_ascent >= 0)
27888 it->ascent = it->override_ascent;
27889 it->descent = it->override_descent;
27890 boff = it->override_boff;
27892 else
27894 it->ascent = FONT_BASE (font) + boff;
27895 it->descent = FONT_DESCENT (font) - boff;
27898 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27900 pcm = get_per_char_metric (font, &char2b);
27901 if (pcm->width == 0
27902 && pcm->rbearing == 0 && pcm->lbearing == 0)
27903 pcm = NULL;
27906 if (pcm)
27908 it->phys_ascent = pcm->ascent + boff;
27909 it->phys_descent = pcm->descent - boff;
27910 it->pixel_width = pcm->width;
27911 /* Don't use font-global values for ascent and descent
27912 if they result in an exceedingly large line height. */
27913 if (it->override_ascent < 0)
27915 if (FONT_TOO_HIGH (font))
27917 it->ascent = it->phys_ascent;
27918 it->descent = it->phys_descent;
27919 /* These limitations are enforced by an
27920 assertion near the end of this function. */
27921 if (it->ascent < 0)
27922 it->ascent = 0;
27923 if (it->descent < 0)
27924 it->descent = 0;
27928 else
27930 it->glyph_not_available_p = true;
27931 it->phys_ascent = it->ascent;
27932 it->phys_descent = it->descent;
27933 it->pixel_width = font->space_width;
27936 if (it->constrain_row_ascent_descent_p)
27938 if (it->descent > it->max_descent)
27940 it->ascent += it->descent - it->max_descent;
27941 it->descent = it->max_descent;
27943 if (it->ascent > it->max_ascent)
27945 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27946 it->ascent = it->max_ascent;
27948 it->phys_ascent = min (it->phys_ascent, it->ascent);
27949 it->phys_descent = min (it->phys_descent, it->descent);
27950 extra_line_spacing = 0;
27953 /* If this is a space inside a region of text with
27954 `space-width' property, change its width. */
27955 bool stretched_p
27956 = it->char_to_display == ' ' && !NILP (it->space_width);
27957 if (stretched_p)
27958 it->pixel_width *= XFLOATINT (it->space_width);
27960 /* If face has a box, add the box thickness to the character
27961 height. If character has a box line to the left and/or
27962 right, add the box line width to the character's width. */
27963 if (face->box != FACE_NO_BOX)
27965 int thick = face->box_line_width;
27967 if (thick > 0)
27969 it->ascent += thick;
27970 it->descent += thick;
27972 else
27973 thick = -thick;
27975 if (it->start_of_box_run_p)
27976 it->pixel_width += thick;
27977 if (it->end_of_box_run_p)
27978 it->pixel_width += thick;
27981 /* If face has an overline, add the height of the overline
27982 (1 pixel) and a 1 pixel margin to the character height. */
27983 if (face->overline_p)
27984 it->ascent += overline_margin;
27986 if (it->constrain_row_ascent_descent_p)
27988 if (it->ascent > it->max_ascent)
27989 it->ascent = it->max_ascent;
27990 if (it->descent > it->max_descent)
27991 it->descent = it->max_descent;
27994 take_vertical_position_into_account (it);
27996 /* If we have to actually produce glyphs, do it. */
27997 if (it->glyph_row)
27999 if (stretched_p)
28001 /* Translate a space with a `space-width' property
28002 into a stretch glyph. */
28003 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28004 / FONT_HEIGHT (font));
28005 append_stretch_glyph (it, it->object, it->pixel_width,
28006 it->ascent + it->descent, ascent);
28008 else
28009 append_glyph (it);
28011 /* If characters with lbearing or rbearing are displayed
28012 in this line, record that fact in a flag of the
28013 glyph row. This is used to optimize X output code. */
28014 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28015 it->glyph_row->contains_overlapping_glyphs_p = true;
28017 if (! stretched_p && it->pixel_width == 0)
28018 /* We assure that all visible glyphs have at least 1-pixel
28019 width. */
28020 it->pixel_width = 1;
28022 else if (it->char_to_display == '\n')
28024 /* A newline has no width, but we need the height of the
28025 line. But if previous part of the line sets a height,
28026 don't increase that height. */
28028 Lisp_Object height;
28029 Lisp_Object total_height = Qnil;
28031 it->override_ascent = -1;
28032 it->pixel_width = 0;
28033 it->nglyphs = 0;
28035 height = get_it_property (it, Qline_height);
28036 /* Split (line-height total-height) list. */
28037 if (CONSP (height)
28038 && CONSP (XCDR (height))
28039 && NILP (XCDR (XCDR (height))))
28041 total_height = XCAR (XCDR (height));
28042 height = XCAR (height);
28044 height = calc_line_height_property (it, height, font, boff, true);
28046 if (it->override_ascent >= 0)
28048 it->ascent = it->override_ascent;
28049 it->descent = it->override_descent;
28050 boff = it->override_boff;
28052 else
28054 if (FONT_TOO_HIGH (font))
28056 it->ascent = font->pixel_size + boff - 1;
28057 it->descent = -boff + 1;
28058 if (it->descent < 0)
28059 it->descent = 0;
28061 else
28063 it->ascent = FONT_BASE (font) + boff;
28064 it->descent = FONT_DESCENT (font) - boff;
28068 if (EQ (height, Qt))
28070 if (it->descent > it->max_descent)
28072 it->ascent += it->descent - it->max_descent;
28073 it->descent = it->max_descent;
28075 if (it->ascent > it->max_ascent)
28077 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28078 it->ascent = it->max_ascent;
28080 it->phys_ascent = min (it->phys_ascent, it->ascent);
28081 it->phys_descent = min (it->phys_descent, it->descent);
28082 it->constrain_row_ascent_descent_p = true;
28083 extra_line_spacing = 0;
28085 else
28087 Lisp_Object spacing;
28089 it->phys_ascent = it->ascent;
28090 it->phys_descent = it->descent;
28092 if ((it->max_ascent > 0 || it->max_descent > 0)
28093 && face->box != FACE_NO_BOX
28094 && face->box_line_width > 0)
28096 it->ascent += face->box_line_width;
28097 it->descent += face->box_line_width;
28099 if (!NILP (height)
28100 && XINT (height) > it->ascent + it->descent)
28101 it->ascent = XINT (height) - it->descent;
28103 if (!NILP (total_height))
28104 spacing = calc_line_height_property (it, total_height, font,
28105 boff, false);
28106 else
28108 spacing = get_it_property (it, Qline_spacing);
28109 spacing = calc_line_height_property (it, spacing, font,
28110 boff, false);
28112 if (INTEGERP (spacing))
28114 extra_line_spacing = XINT (spacing);
28115 if (!NILP (total_height))
28116 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28120 else /* i.e. (it->char_to_display == '\t') */
28122 if (font->space_width > 0)
28124 int tab_width = it->tab_width * font->space_width;
28125 int x = it->current_x + it->continuation_lines_width;
28126 int x0 = x;
28127 /* Adjust for line numbers, if needed. */
28128 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28129 x -= it->lnum_pixel_width;
28130 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28132 /* If the distance from the current position to the next tab
28133 stop is less than a space character width, use the
28134 tab stop after that. */
28135 if (next_tab_x - x < font->space_width)
28136 next_tab_x += tab_width;
28137 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28138 next_tab_x += (it->lnum_pixel_width
28139 - ((it->w->hscroll * font->space_width)
28140 % tab_width));
28142 it->pixel_width = next_tab_x - x0;
28143 it->nglyphs = 1;
28144 if (FONT_TOO_HIGH (font))
28146 if (get_char_glyph_code (' ', font, &char2b))
28148 pcm = get_per_char_metric (font, &char2b);
28149 if (pcm->width == 0
28150 && pcm->rbearing == 0 && pcm->lbearing == 0)
28151 pcm = NULL;
28154 if (pcm)
28156 it->ascent = pcm->ascent + boff;
28157 it->descent = pcm->descent - boff;
28159 else
28161 it->ascent = font->pixel_size + boff - 1;
28162 it->descent = -boff + 1;
28164 if (it->ascent < 0)
28165 it->ascent = 0;
28166 if (it->descent < 0)
28167 it->descent = 0;
28169 else
28171 it->ascent = FONT_BASE (font) + boff;
28172 it->descent = FONT_DESCENT (font) - boff;
28174 it->phys_ascent = it->ascent;
28175 it->phys_descent = it->descent;
28177 if (it->glyph_row)
28179 append_stretch_glyph (it, it->object, it->pixel_width,
28180 it->ascent + it->descent, it->ascent);
28183 else
28185 it->pixel_width = 0;
28186 it->nglyphs = 1;
28190 if (FONT_TOO_HIGH (font))
28192 int font_ascent, font_descent;
28194 /* For very large fonts, where we ignore the declared font
28195 dimensions, and go by per-character metrics instead,
28196 don't let the row ascent and descent values (and the row
28197 height computed from them) be smaller than the "normal"
28198 character metrics. This avoids unpleasant effects
28199 whereby lines on display would change their height
28200 depending on which characters are shown. */
28201 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28202 it->max_ascent = max (it->max_ascent, font_ascent);
28203 it->max_descent = max (it->max_descent, font_descent);
28206 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28208 /* A static composition.
28210 Note: A composition is represented as one glyph in the
28211 glyph matrix. There are no padding glyphs.
28213 Important note: pixel_width, ascent, and descent are the
28214 values of what is drawn by draw_glyphs (i.e. the values of
28215 the overall glyphs composed). */
28216 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28217 int boff; /* baseline offset */
28218 struct composition *cmp = composition_table[it->cmp_it.id];
28219 int glyph_len = cmp->glyph_len;
28220 struct font *font = face->font;
28222 it->nglyphs = 1;
28224 /* If we have not yet calculated pixel size data of glyphs of
28225 the composition for the current face font, calculate them
28226 now. Theoretically, we have to check all fonts for the
28227 glyphs, but that requires much time and memory space. So,
28228 here we check only the font of the first glyph. This may
28229 lead to incorrect display, but it's very rare, and C-l
28230 (recenter-top-bottom) can correct the display anyway. */
28231 if (! cmp->font || cmp->font != font)
28233 /* Ascent and descent of the font of the first character
28234 of this composition (adjusted by baseline offset).
28235 Ascent and descent of overall glyphs should not be less
28236 than these, respectively. */
28237 int font_ascent, font_descent, font_height;
28238 /* Bounding box of the overall glyphs. */
28239 int leftmost, rightmost, lowest, highest;
28240 int lbearing, rbearing;
28241 int i, width, ascent, descent;
28242 int c;
28243 XChar2b char2b;
28244 struct font_metrics *pcm;
28245 ptrdiff_t pos;
28247 eassume (0 < glyph_len); /* See Bug#8512. */
28249 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28250 while (c == '\t' && 0 < --glyph_len);
28252 bool right_padded = glyph_len < cmp->glyph_len;
28253 for (i = 0; i < glyph_len; i++)
28255 c = COMPOSITION_GLYPH (cmp, i);
28256 if (c != '\t')
28257 break;
28258 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28260 bool left_padded = i > 0;
28262 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28263 : IT_CHARPOS (*it));
28264 /* If no suitable font is found, use the default font. */
28265 bool font_not_found_p = font == NULL;
28266 if (font_not_found_p)
28268 face = face->ascii_face;
28269 font = face->font;
28271 boff = font->baseline_offset;
28272 if (font->vertical_centering)
28273 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28274 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28275 font_ascent += boff;
28276 font_descent -= boff;
28277 font_height = font_ascent + font_descent;
28279 cmp->font = font;
28281 pcm = NULL;
28282 if (! font_not_found_p)
28284 get_char_face_and_encoding (it->f, c, it->face_id,
28285 &char2b, false);
28286 pcm = get_per_char_metric (font, &char2b);
28289 /* Initialize the bounding box. */
28290 if (pcm)
28292 width = cmp->glyph_len > 0 ? pcm->width : 0;
28293 ascent = pcm->ascent;
28294 descent = pcm->descent;
28295 lbearing = pcm->lbearing;
28296 rbearing = pcm->rbearing;
28298 else
28300 width = cmp->glyph_len > 0 ? font->space_width : 0;
28301 ascent = FONT_BASE (font);
28302 descent = FONT_DESCENT (font);
28303 lbearing = 0;
28304 rbearing = width;
28307 rightmost = width;
28308 leftmost = 0;
28309 lowest = - descent + boff;
28310 highest = ascent + boff;
28312 if (! font_not_found_p
28313 && font->default_ascent
28314 && CHAR_TABLE_P (Vuse_default_ascent)
28315 && !NILP (Faref (Vuse_default_ascent,
28316 make_number (it->char_to_display))))
28317 highest = font->default_ascent + boff;
28319 /* Draw the first glyph at the normal position. It may be
28320 shifted to right later if some other glyphs are drawn
28321 at the left. */
28322 cmp->offsets[i * 2] = 0;
28323 cmp->offsets[i * 2 + 1] = boff;
28324 cmp->lbearing = lbearing;
28325 cmp->rbearing = rbearing;
28327 /* Set cmp->offsets for the remaining glyphs. */
28328 for (i++; i < glyph_len; i++)
28330 int left, right, btm, top;
28331 int ch = COMPOSITION_GLYPH (cmp, i);
28332 int face_id;
28333 struct face *this_face;
28335 if (ch == '\t')
28336 ch = ' ';
28337 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28338 this_face = FACE_FROM_ID (it->f, face_id);
28339 font = this_face->font;
28341 if (font == NULL)
28342 pcm = NULL;
28343 else
28345 get_char_face_and_encoding (it->f, ch, face_id,
28346 &char2b, false);
28347 pcm = get_per_char_metric (font, &char2b);
28349 if (! pcm)
28350 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28351 else
28353 width = pcm->width;
28354 ascent = pcm->ascent;
28355 descent = pcm->descent;
28356 lbearing = pcm->lbearing;
28357 rbearing = pcm->rbearing;
28358 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28360 /* Relative composition with or without
28361 alternate chars. */
28362 left = (leftmost + rightmost - width) / 2;
28363 btm = - descent + boff;
28364 if (font->relative_compose
28365 && (! CHAR_TABLE_P (Vignore_relative_composition)
28366 || NILP (Faref (Vignore_relative_composition,
28367 make_number (ch)))))
28370 if (- descent >= font->relative_compose)
28371 /* One extra pixel between two glyphs. */
28372 btm = highest + 1;
28373 else if (ascent <= 0)
28374 /* One extra pixel between two glyphs. */
28375 btm = lowest - 1 - ascent - descent;
28378 else
28380 /* A composition rule is specified by an integer
28381 value that encodes global and new reference
28382 points (GREF and NREF). GREF and NREF are
28383 specified by numbers as below:
28385 0---1---2 -- ascent
28389 9--10--11 -- center
28391 ---3---4---5--- baseline
28393 6---7---8 -- descent
28395 int rule = COMPOSITION_RULE (cmp, i);
28396 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28398 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28399 grefx = gref % 3, nrefx = nref % 3;
28400 grefy = gref / 3, nrefy = nref / 3;
28401 if (xoff)
28402 xoff = font_height * (xoff - 128) / 256;
28403 if (yoff)
28404 yoff = font_height * (yoff - 128) / 256;
28406 left = (leftmost
28407 + grefx * (rightmost - leftmost) / 2
28408 - nrefx * width / 2
28409 + xoff);
28411 btm = ((grefy == 0 ? highest
28412 : grefy == 1 ? 0
28413 : grefy == 2 ? lowest
28414 : (highest + lowest) / 2)
28415 - (nrefy == 0 ? ascent + descent
28416 : nrefy == 1 ? descent - boff
28417 : nrefy == 2 ? 0
28418 : (ascent + descent) / 2)
28419 + yoff);
28422 cmp->offsets[i * 2] = left;
28423 cmp->offsets[i * 2 + 1] = btm + descent;
28425 /* Update the bounding box of the overall glyphs. */
28426 if (width > 0)
28428 right = left + width;
28429 if (left < leftmost)
28430 leftmost = left;
28431 if (right > rightmost)
28432 rightmost = right;
28434 top = btm + descent + ascent;
28435 if (top > highest)
28436 highest = top;
28437 if (btm < lowest)
28438 lowest = btm;
28440 if (cmp->lbearing > left + lbearing)
28441 cmp->lbearing = left + lbearing;
28442 if (cmp->rbearing < left + rbearing)
28443 cmp->rbearing = left + rbearing;
28447 /* If there are glyphs whose x-offsets are negative,
28448 shift all glyphs to the right and make all x-offsets
28449 non-negative. */
28450 if (leftmost < 0)
28452 for (i = 0; i < cmp->glyph_len; i++)
28453 cmp->offsets[i * 2] -= leftmost;
28454 rightmost -= leftmost;
28455 cmp->lbearing -= leftmost;
28456 cmp->rbearing -= leftmost;
28459 if (left_padded && cmp->lbearing < 0)
28461 for (i = 0; i < cmp->glyph_len; i++)
28462 cmp->offsets[i * 2] -= cmp->lbearing;
28463 rightmost -= cmp->lbearing;
28464 cmp->rbearing -= cmp->lbearing;
28465 cmp->lbearing = 0;
28467 if (right_padded && rightmost < cmp->rbearing)
28469 rightmost = cmp->rbearing;
28472 cmp->pixel_width = rightmost;
28473 cmp->ascent = highest;
28474 cmp->descent = - lowest;
28475 if (cmp->ascent < font_ascent)
28476 cmp->ascent = font_ascent;
28477 if (cmp->descent < font_descent)
28478 cmp->descent = font_descent;
28481 if (it->glyph_row
28482 && (cmp->lbearing < 0
28483 || cmp->rbearing > cmp->pixel_width))
28484 it->glyph_row->contains_overlapping_glyphs_p = true;
28486 it->pixel_width = cmp->pixel_width;
28487 it->ascent = it->phys_ascent = cmp->ascent;
28488 it->descent = it->phys_descent = cmp->descent;
28489 if (face->box != FACE_NO_BOX)
28491 int thick = face->box_line_width;
28493 if (thick > 0)
28495 it->ascent += thick;
28496 it->descent += thick;
28498 else
28499 thick = - thick;
28501 if (it->start_of_box_run_p)
28502 it->pixel_width += thick;
28503 if (it->end_of_box_run_p)
28504 it->pixel_width += thick;
28507 /* If face has an overline, add the height of the overline
28508 (1 pixel) and a 1 pixel margin to the character height. */
28509 if (face->overline_p)
28510 it->ascent += overline_margin;
28512 take_vertical_position_into_account (it);
28513 if (it->ascent < 0)
28514 it->ascent = 0;
28515 if (it->descent < 0)
28516 it->descent = 0;
28518 if (it->glyph_row && cmp->glyph_len > 0)
28519 append_composite_glyph (it);
28521 else if (it->what == IT_COMPOSITION)
28523 /* A dynamic (automatic) composition. */
28524 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28525 Lisp_Object gstring;
28526 struct font_metrics metrics;
28528 it->nglyphs = 1;
28530 gstring = composition_gstring_from_id (it->cmp_it.id);
28531 it->pixel_width
28532 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28533 &metrics);
28534 if (it->glyph_row
28535 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28536 it->glyph_row->contains_overlapping_glyphs_p = true;
28537 it->ascent = it->phys_ascent = metrics.ascent;
28538 it->descent = it->phys_descent = metrics.descent;
28539 if (face->box != FACE_NO_BOX)
28541 int thick = face->box_line_width;
28543 if (thick > 0)
28545 it->ascent += thick;
28546 it->descent += thick;
28548 else
28549 thick = - thick;
28551 if (it->start_of_box_run_p)
28552 it->pixel_width += thick;
28553 if (it->end_of_box_run_p)
28554 it->pixel_width += thick;
28556 /* If face has an overline, add the height of the overline
28557 (1 pixel) and a 1 pixel margin to the character height. */
28558 if (face->overline_p)
28559 it->ascent += overline_margin;
28560 take_vertical_position_into_account (it);
28561 if (it->ascent < 0)
28562 it->ascent = 0;
28563 if (it->descent < 0)
28564 it->descent = 0;
28566 if (it->glyph_row)
28567 append_composite_glyph (it);
28569 else if (it->what == IT_GLYPHLESS)
28570 produce_glyphless_glyph (it, false, Qnil);
28571 else if (it->what == IT_IMAGE)
28572 produce_image_glyph (it);
28573 else if (it->what == IT_STRETCH)
28574 produce_stretch_glyph (it);
28575 else if (it->what == IT_XWIDGET)
28576 produce_xwidget_glyph (it);
28578 done:
28579 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28580 because this isn't true for images with `:ascent 100'. */
28581 eassert (it->ascent >= 0 && it->descent >= 0);
28582 if (it->area == TEXT_AREA)
28583 it->current_x += it->pixel_width;
28585 if (extra_line_spacing > 0)
28587 it->descent += extra_line_spacing;
28588 if (extra_line_spacing > it->max_extra_line_spacing)
28589 it->max_extra_line_spacing = extra_line_spacing;
28592 it->max_ascent = max (it->max_ascent, it->ascent);
28593 it->max_descent = max (it->max_descent, it->descent);
28594 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28595 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28598 /* EXPORT for RIF:
28599 Output LEN glyphs starting at START at the nominal cursor position.
28600 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28601 being updated, and UPDATED_AREA is the area of that row being updated. */
28603 void
28604 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28605 struct glyph *start, enum glyph_row_area updated_area, int len)
28607 int x, hpos, chpos = w->phys_cursor.hpos;
28609 eassert (updated_row);
28610 /* When the window is hscrolled, cursor hpos can legitimately be out
28611 of bounds, but we draw the cursor at the corresponding window
28612 margin in that case. */
28613 if (!updated_row->reversed_p && chpos < 0)
28614 chpos = 0;
28615 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28616 chpos = updated_row->used[TEXT_AREA] - 1;
28618 block_input ();
28620 /* Write glyphs. */
28622 hpos = start - updated_row->glyphs[updated_area];
28623 x = draw_glyphs (w, w->output_cursor.x,
28624 updated_row, updated_area,
28625 hpos, hpos + len,
28626 DRAW_NORMAL_TEXT, 0);
28628 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28629 if (updated_area == TEXT_AREA
28630 && w->phys_cursor_on_p
28631 && w->phys_cursor.vpos == w->output_cursor.vpos
28632 && chpos >= hpos
28633 && chpos < hpos + len)
28634 w->phys_cursor_on_p = false;
28636 unblock_input ();
28638 /* Advance the output cursor. */
28639 w->output_cursor.hpos += len;
28640 w->output_cursor.x = x;
28644 /* EXPORT for RIF:
28645 Insert LEN glyphs from START at the nominal cursor position. */
28647 void
28648 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28649 struct glyph *start, enum glyph_row_area updated_area, int len)
28651 struct frame *f;
28652 int line_height, shift_by_width, shifted_region_width;
28653 struct glyph_row *row;
28654 struct glyph *glyph;
28655 int frame_x, frame_y;
28656 ptrdiff_t hpos;
28658 eassert (updated_row);
28659 block_input ();
28660 f = XFRAME (WINDOW_FRAME (w));
28662 /* Get the height of the line we are in. */
28663 row = updated_row;
28664 line_height = row->height;
28666 /* Get the width of the glyphs to insert. */
28667 shift_by_width = 0;
28668 for (glyph = start; glyph < start + len; ++glyph)
28669 shift_by_width += glyph->pixel_width;
28671 /* Get the width of the region to shift right. */
28672 shifted_region_width = (window_box_width (w, updated_area)
28673 - w->output_cursor.x
28674 - shift_by_width);
28676 /* Shift right. */
28677 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28678 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28680 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28681 line_height, shift_by_width);
28683 /* Write the glyphs. */
28684 hpos = start - row->glyphs[updated_area];
28685 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28686 hpos, hpos + len,
28687 DRAW_NORMAL_TEXT, 0);
28689 /* Advance the output cursor. */
28690 w->output_cursor.hpos += len;
28691 w->output_cursor.x += shift_by_width;
28692 unblock_input ();
28696 /* EXPORT for RIF:
28697 Erase the current text line from the nominal cursor position
28698 (inclusive) to pixel column TO_X (exclusive). The idea is that
28699 everything from TO_X onward is already erased.
28701 TO_X is a pixel position relative to UPDATED_AREA of currently
28702 updated window W. TO_X == -1 means clear to the end of this area. */
28704 void
28705 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28706 enum glyph_row_area updated_area, int to_x)
28708 struct frame *f;
28709 int max_x, min_y, max_y;
28710 int from_x, from_y, to_y;
28712 eassert (updated_row);
28713 f = XFRAME (w->frame);
28715 if (updated_row->full_width_p)
28716 max_x = (WINDOW_PIXEL_WIDTH (w)
28717 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28718 else
28719 max_x = window_box_width (w, updated_area);
28720 max_y = window_text_bottom_y (w);
28722 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28723 of window. For TO_X > 0, truncate to end of drawing area. */
28724 if (to_x == 0)
28725 return;
28726 else if (to_x < 0)
28727 to_x = max_x;
28728 else
28729 to_x = min (to_x, max_x);
28731 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28733 /* Notice if the cursor will be cleared by this operation. */
28734 if (!updated_row->full_width_p)
28735 notice_overwritten_cursor (w, updated_area,
28736 w->output_cursor.x, -1,
28737 updated_row->y,
28738 MATRIX_ROW_BOTTOM_Y (updated_row));
28740 from_x = w->output_cursor.x;
28742 /* Translate to frame coordinates. */
28743 if (updated_row->full_width_p)
28745 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28746 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28748 else
28750 int area_left = window_box_left (w, updated_area);
28751 from_x += area_left;
28752 to_x += area_left;
28755 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28756 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28757 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28759 /* Prevent inadvertently clearing to end of the X window. */
28760 if (to_x > from_x && to_y > from_y)
28762 block_input ();
28763 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28764 to_x - from_x, to_y - from_y);
28765 unblock_input ();
28769 #endif /* HAVE_WINDOW_SYSTEM */
28773 /***********************************************************************
28774 Cursor types
28775 ***********************************************************************/
28777 /* Value is the internal representation of the specified cursor type
28778 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28779 of the bar cursor. */
28781 static enum text_cursor_kinds
28782 get_specified_cursor_type (Lisp_Object arg, int *width)
28784 enum text_cursor_kinds type;
28786 if (NILP (arg))
28787 return NO_CURSOR;
28789 if (EQ (arg, Qbox))
28790 return FILLED_BOX_CURSOR;
28792 if (EQ (arg, Qhollow))
28793 return HOLLOW_BOX_CURSOR;
28795 if (EQ (arg, Qbar))
28797 *width = 2;
28798 return BAR_CURSOR;
28801 if (CONSP (arg)
28802 && EQ (XCAR (arg), Qbar)
28803 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28805 *width = XINT (XCDR (arg));
28806 return BAR_CURSOR;
28809 if (EQ (arg, Qhbar))
28811 *width = 2;
28812 return HBAR_CURSOR;
28815 if (CONSP (arg)
28816 && EQ (XCAR (arg), Qhbar)
28817 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28819 *width = XINT (XCDR (arg));
28820 return HBAR_CURSOR;
28823 /* Treat anything unknown as "hollow box cursor".
28824 It was bad to signal an error; people have trouble fixing
28825 .Xdefaults with Emacs, when it has something bad in it. */
28826 type = HOLLOW_BOX_CURSOR;
28828 return type;
28831 /* Set the default cursor types for specified frame. */
28832 void
28833 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28835 int width = 1;
28836 Lisp_Object tem;
28838 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28839 FRAME_CURSOR_WIDTH (f) = width;
28841 /* By default, set up the blink-off state depending on the on-state. */
28843 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28844 if (!NILP (tem))
28846 FRAME_BLINK_OFF_CURSOR (f)
28847 = get_specified_cursor_type (XCDR (tem), &width);
28848 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28850 else
28851 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28853 /* Make sure the cursor gets redrawn. */
28854 f->cursor_type_changed = true;
28858 #ifdef HAVE_WINDOW_SYSTEM
28860 /* Return the cursor we want to be displayed in window W. Return
28861 width of bar/hbar cursor through WIDTH arg. Return with
28862 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28863 (i.e. if the `system caret' should track this cursor).
28865 In a mini-buffer window, we want the cursor only to appear if we
28866 are reading input from this window. For the selected window, we
28867 want the cursor type given by the frame parameter or buffer local
28868 setting of cursor-type. If explicitly marked off, draw no cursor.
28869 In all other cases, we want a hollow box cursor. */
28871 static enum text_cursor_kinds
28872 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28873 bool *active_cursor)
28875 struct frame *f = XFRAME (w->frame);
28876 struct buffer *b = XBUFFER (w->contents);
28877 int cursor_type = DEFAULT_CURSOR;
28878 Lisp_Object alt_cursor;
28879 bool non_selected = false;
28881 *active_cursor = true;
28883 /* Echo area */
28884 if (cursor_in_echo_area
28885 && FRAME_HAS_MINIBUF_P (f)
28886 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28888 if (w == XWINDOW (echo_area_window))
28890 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28892 *width = FRAME_CURSOR_WIDTH (f);
28893 return FRAME_DESIRED_CURSOR (f);
28895 else
28896 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28899 *active_cursor = false;
28900 non_selected = true;
28903 /* Detect a nonselected window or nonselected frame. */
28904 else if (w != XWINDOW (f->selected_window)
28905 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28907 *active_cursor = false;
28909 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28910 return NO_CURSOR;
28912 non_selected = true;
28915 /* Never display a cursor in a window in which cursor-type is nil. */
28916 if (NILP (BVAR (b, cursor_type)))
28917 return NO_CURSOR;
28919 /* Get the normal cursor type for this window. */
28920 if (EQ (BVAR (b, cursor_type), Qt))
28922 cursor_type = FRAME_DESIRED_CURSOR (f);
28923 *width = FRAME_CURSOR_WIDTH (f);
28925 else
28926 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28928 /* Use cursor-in-non-selected-windows instead
28929 for non-selected window or frame. */
28930 if (non_selected)
28932 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28933 if (!EQ (Qt, alt_cursor))
28934 return get_specified_cursor_type (alt_cursor, width);
28935 /* t means modify the normal cursor type. */
28936 if (cursor_type == FILLED_BOX_CURSOR)
28937 cursor_type = HOLLOW_BOX_CURSOR;
28938 else if (cursor_type == BAR_CURSOR && *width > 1)
28939 --*width;
28940 return cursor_type;
28943 /* Use normal cursor if not blinked off. */
28944 if (!w->cursor_off_p)
28946 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28947 return NO_CURSOR;
28948 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28950 if (cursor_type == FILLED_BOX_CURSOR)
28952 /* Using a block cursor on large images can be very annoying.
28953 So use a hollow cursor for "large" images.
28954 If image is not transparent (no mask), also use hollow cursor. */
28955 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28956 if (img != NULL && IMAGEP (img->spec))
28958 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28959 where N = size of default frame font size.
28960 This should cover most of the "tiny" icons people may use. */
28961 if (!img->mask
28962 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28963 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28964 cursor_type = HOLLOW_BOX_CURSOR;
28967 else if (cursor_type != NO_CURSOR)
28969 /* Display current only supports BOX and HOLLOW cursors for images.
28970 So for now, unconditionally use a HOLLOW cursor when cursor is
28971 not a solid box cursor. */
28972 cursor_type = HOLLOW_BOX_CURSOR;
28975 return cursor_type;
28978 /* Cursor is blinked off, so determine how to "toggle" it. */
28980 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28981 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
28982 return get_specified_cursor_type (XCDR (alt_cursor), width);
28984 /* Then see if frame has specified a specific blink off cursor type. */
28985 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28987 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28988 return FRAME_BLINK_OFF_CURSOR (f);
28991 #if false
28992 /* Some people liked having a permanently visible blinking cursor,
28993 while others had very strong opinions against it. So it was
28994 decided to remove it. KFS 2003-09-03 */
28996 /* Finally perform built-in cursor blinking:
28997 filled box <-> hollow box
28998 wide [h]bar <-> narrow [h]bar
28999 narrow [h]bar <-> no cursor
29000 other type <-> no cursor */
29002 if (cursor_type == FILLED_BOX_CURSOR)
29003 return HOLLOW_BOX_CURSOR;
29005 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29007 *width = 1;
29008 return cursor_type;
29010 #endif
29012 return NO_CURSOR;
29016 /* Notice when the text cursor of window W has been completely
29017 overwritten by a drawing operation that outputs glyphs in AREA
29018 starting at X0 and ending at X1 in the line starting at Y0 and
29019 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29020 the rest of the line after X0 has been written. Y coordinates
29021 are window-relative. */
29023 static void
29024 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29025 int x0, int x1, int y0, int y1)
29027 int cx0, cx1, cy0, cy1;
29028 struct glyph_row *row;
29030 if (!w->phys_cursor_on_p)
29031 return;
29032 if (area != TEXT_AREA)
29033 return;
29035 if (w->phys_cursor.vpos < 0
29036 || w->phys_cursor.vpos >= w->current_matrix->nrows
29037 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29038 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29039 return;
29041 if (row->cursor_in_fringe_p)
29043 row->cursor_in_fringe_p = false;
29044 draw_fringe_bitmap (w, row, row->reversed_p);
29045 w->phys_cursor_on_p = false;
29046 return;
29049 cx0 = w->phys_cursor.x;
29050 cx1 = cx0 + w->phys_cursor_width;
29051 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29052 return;
29054 /* The cursor image will be completely removed from the
29055 screen if the output area intersects the cursor area in
29056 y-direction. When we draw in [y0 y1[, and some part of
29057 the cursor is at y < y0, that part must have been drawn
29058 before. When scrolling, the cursor is erased before
29059 actually scrolling, so we don't come here. When not
29060 scrolling, the rows above the old cursor row must have
29061 changed, and in this case these rows must have written
29062 over the cursor image.
29064 Likewise if part of the cursor is below y1, with the
29065 exception of the cursor being in the first blank row at
29066 the buffer and window end because update_text_area
29067 doesn't draw that row. (Except when it does, but
29068 that's handled in update_text_area.) */
29070 cy0 = w->phys_cursor.y;
29071 cy1 = cy0 + w->phys_cursor_height;
29072 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29073 return;
29075 w->phys_cursor_on_p = false;
29078 #endif /* HAVE_WINDOW_SYSTEM */
29081 /************************************************************************
29082 Mouse Face
29083 ************************************************************************/
29085 #ifdef HAVE_WINDOW_SYSTEM
29087 /* EXPORT for RIF:
29088 Fix the display of area AREA of overlapping row ROW in window W
29089 with respect to the overlapping part OVERLAPS. */
29091 void
29092 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29093 enum glyph_row_area area, int overlaps)
29095 int i, x;
29097 block_input ();
29099 x = 0;
29100 for (i = 0; i < row->used[area];)
29102 if (row->glyphs[area][i].overlaps_vertically_p)
29104 int start = i, start_x = x;
29108 x += row->glyphs[area][i].pixel_width;
29109 ++i;
29111 while (i < row->used[area]
29112 && row->glyphs[area][i].overlaps_vertically_p);
29114 draw_glyphs (w, start_x, row, area,
29115 start, i,
29116 DRAW_NORMAL_TEXT, overlaps);
29118 else
29120 x += row->glyphs[area][i].pixel_width;
29121 ++i;
29125 unblock_input ();
29129 /* EXPORT:
29130 Draw the cursor glyph of window W in glyph row ROW. See the
29131 comment of draw_glyphs for the meaning of HL. */
29133 void
29134 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29135 enum draw_glyphs_face hl)
29137 /* If cursor hpos is out of bounds, don't draw garbage. This can
29138 happen in mini-buffer windows when switching between echo area
29139 glyphs and mini-buffer. */
29140 if ((row->reversed_p
29141 ? (w->phys_cursor.hpos >= 0)
29142 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29144 bool on_p = w->phys_cursor_on_p;
29145 int x1;
29146 int hpos = w->phys_cursor.hpos;
29148 /* When the window is hscrolled, cursor hpos can legitimately be
29149 out of bounds, but we draw the cursor at the corresponding
29150 window margin in that case. */
29151 if (!row->reversed_p && hpos < 0)
29152 hpos = 0;
29153 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29154 hpos = row->used[TEXT_AREA] - 1;
29156 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29157 hl, 0);
29158 w->phys_cursor_on_p = on_p;
29160 if (hl == DRAW_CURSOR)
29161 w->phys_cursor_width = x1 - w->phys_cursor.x;
29162 /* When we erase the cursor, and ROW is overlapped by other
29163 rows, make sure that these overlapping parts of other rows
29164 are redrawn. */
29165 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29167 w->phys_cursor_width = x1 - w->phys_cursor.x;
29169 if (row > w->current_matrix->rows
29170 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29171 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29172 OVERLAPS_ERASED_CURSOR);
29174 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29175 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29176 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29177 OVERLAPS_ERASED_CURSOR);
29183 /* Erase the image of a cursor of window W from the screen. */
29185 void
29186 erase_phys_cursor (struct window *w)
29188 struct frame *f = XFRAME (w->frame);
29189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29190 int hpos = w->phys_cursor.hpos;
29191 int vpos = w->phys_cursor.vpos;
29192 bool mouse_face_here_p = false;
29193 struct glyph_matrix *active_glyphs = w->current_matrix;
29194 struct glyph_row *cursor_row;
29195 struct glyph *cursor_glyph;
29196 enum draw_glyphs_face hl;
29198 /* No cursor displayed or row invalidated => nothing to do on the
29199 screen. */
29200 if (w->phys_cursor_type == NO_CURSOR)
29201 goto mark_cursor_off;
29203 /* VPOS >= active_glyphs->nrows means that window has been resized.
29204 Don't bother to erase the cursor. */
29205 if (vpos >= active_glyphs->nrows)
29206 goto mark_cursor_off;
29208 /* If row containing cursor is marked invalid, there is nothing we
29209 can do. */
29210 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29211 if (!cursor_row->enabled_p)
29212 goto mark_cursor_off;
29214 /* If line spacing is > 0, old cursor may only be partially visible in
29215 window after split-window. So adjust visible height. */
29216 cursor_row->visible_height = min (cursor_row->visible_height,
29217 window_text_bottom_y (w) - cursor_row->y);
29219 /* If row is completely invisible, don't attempt to delete a cursor which
29220 isn't there. This can happen if cursor is at top of a window, and
29221 we switch to a buffer with a header line in that window. */
29222 if (cursor_row->visible_height <= 0)
29223 goto mark_cursor_off;
29225 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29226 if (cursor_row->cursor_in_fringe_p)
29228 cursor_row->cursor_in_fringe_p = false;
29229 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29230 goto mark_cursor_off;
29233 /* This can happen when the new row is shorter than the old one.
29234 In this case, either draw_glyphs or clear_end_of_line
29235 should have cleared the cursor. Note that we wouldn't be
29236 able to erase the cursor in this case because we don't have a
29237 cursor glyph at hand. */
29238 if ((cursor_row->reversed_p
29239 ? (w->phys_cursor.hpos < 0)
29240 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29241 goto mark_cursor_off;
29243 /* When the window is hscrolled, cursor hpos can legitimately be out
29244 of bounds, but we draw the cursor at the corresponding window
29245 margin in that case. */
29246 if (!cursor_row->reversed_p && hpos < 0)
29247 hpos = 0;
29248 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29249 hpos = cursor_row->used[TEXT_AREA] - 1;
29251 /* If the cursor is in the mouse face area, redisplay that when
29252 we clear the cursor. */
29253 if (! NILP (hlinfo->mouse_face_window)
29254 && coords_in_mouse_face_p (w, hpos, vpos)
29255 /* Don't redraw the cursor's spot in mouse face if it is at the
29256 end of a line (on a newline). The cursor appears there, but
29257 mouse highlighting does not. */
29258 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29259 mouse_face_here_p = true;
29261 /* Maybe clear the display under the cursor. */
29262 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29264 int x, y;
29265 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29266 int width;
29268 cursor_glyph = get_phys_cursor_glyph (w);
29269 if (cursor_glyph == NULL)
29270 goto mark_cursor_off;
29272 width = cursor_glyph->pixel_width;
29273 x = w->phys_cursor.x;
29274 if (x < 0)
29276 width += x;
29277 x = 0;
29279 width = min (width, window_box_width (w, TEXT_AREA) - x);
29280 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29281 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29283 if (width > 0)
29284 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29287 /* Erase the cursor by redrawing the character underneath it. */
29288 if (mouse_face_here_p)
29289 hl = DRAW_MOUSE_FACE;
29290 else
29291 hl = DRAW_NORMAL_TEXT;
29292 draw_phys_cursor_glyph (w, cursor_row, hl);
29294 mark_cursor_off:
29295 w->phys_cursor_on_p = false;
29296 w->phys_cursor_type = NO_CURSOR;
29300 /* Display or clear cursor of window W. If !ON, clear the cursor.
29301 If ON, display the cursor; where to put the cursor is specified by
29302 HPOS, VPOS, X and Y. */
29304 void
29305 display_and_set_cursor (struct window *w, bool on,
29306 int hpos, int vpos, int x, int y)
29308 struct frame *f = XFRAME (w->frame);
29309 int new_cursor_type;
29310 int new_cursor_width;
29311 bool active_cursor;
29312 struct glyph_row *glyph_row;
29313 struct glyph *glyph;
29315 /* This is pointless on invisible frames, and dangerous on garbaged
29316 windows and frames; in the latter case, the frame or window may
29317 be in the midst of changing its size, and x and y may be off the
29318 window. */
29319 if (! FRAME_VISIBLE_P (f)
29320 || vpos >= w->current_matrix->nrows
29321 || hpos >= w->current_matrix->matrix_w)
29322 return;
29324 /* If cursor is off and we want it off, return quickly. */
29325 if (!on && !w->phys_cursor_on_p)
29326 return;
29328 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29329 /* If cursor row is not enabled, we don't really know where to
29330 display the cursor. */
29331 if (!glyph_row->enabled_p)
29333 w->phys_cursor_on_p = false;
29334 return;
29337 /* A frame might be marked garbaged even though its cursor position
29338 is correct, and will not change upon subsequent redisplay. This
29339 happens in some rare situations, like toggling the sort order in
29340 Dired windows. We've already established that VPOS is valid, so
29341 it shouldn't do any harm to record the cursor position, as we are
29342 going to return without acting on it anyway. Otherwise, expose
29343 events might come in and call update_window_cursor, which will
29344 blindly use outdated values in w->phys_cursor. */
29345 if (FRAME_GARBAGED_P (f))
29347 if (on)
29349 w->phys_cursor.x = x;
29350 w->phys_cursor.y = glyph_row->y;
29351 w->phys_cursor.hpos = hpos;
29352 w->phys_cursor.vpos = vpos;
29354 return;
29357 glyph = NULL;
29358 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29359 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29361 eassert (input_blocked_p ());
29363 /* Set new_cursor_type to the cursor we want to be displayed. */
29364 new_cursor_type = get_window_cursor_type (w, glyph,
29365 &new_cursor_width, &active_cursor);
29367 /* If cursor is currently being shown and we don't want it to be or
29368 it is in the wrong place, or the cursor type is not what we want,
29369 erase it. */
29370 if (w->phys_cursor_on_p
29371 && (!on
29372 || w->phys_cursor.x != x
29373 || w->phys_cursor.y != y
29374 /* HPOS can be negative in R2L rows whose
29375 exact_window_width_line_p flag is set (i.e. their newline
29376 would "overflow into the fringe"). */
29377 || hpos < 0
29378 || new_cursor_type != w->phys_cursor_type
29379 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29380 && new_cursor_width != w->phys_cursor_width)))
29381 erase_phys_cursor (w);
29383 /* Don't check phys_cursor_on_p here because that flag is only set
29384 to false in some cases where we know that the cursor has been
29385 completely erased, to avoid the extra work of erasing the cursor
29386 twice. In other words, phys_cursor_on_p can be true and the cursor
29387 still not be visible, or it has only been partly erased. */
29388 if (on)
29390 w->phys_cursor_ascent = glyph_row->ascent;
29391 w->phys_cursor_height = glyph_row->height;
29393 /* Set phys_cursor_.* before x_draw_.* is called because some
29394 of them may need the information. */
29395 w->phys_cursor.x = x;
29396 w->phys_cursor.y = glyph_row->y;
29397 w->phys_cursor.hpos = hpos;
29398 w->phys_cursor.vpos = vpos;
29401 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29402 new_cursor_type, new_cursor_width,
29403 on, active_cursor);
29407 /* Switch the display of W's cursor on or off, according to the value
29408 of ON. */
29410 static void
29411 update_window_cursor (struct window *w, bool on)
29413 /* Don't update cursor in windows whose frame is in the process
29414 of being deleted. */
29415 if (w->current_matrix)
29417 int hpos = w->phys_cursor.hpos;
29418 int vpos = w->phys_cursor.vpos;
29419 struct glyph_row *row;
29421 if (vpos >= w->current_matrix->nrows
29422 || hpos >= w->current_matrix->matrix_w)
29423 return;
29425 row = MATRIX_ROW (w->current_matrix, vpos);
29427 /* When the window is hscrolled, cursor hpos can legitimately be
29428 out of bounds, but we draw the cursor at the corresponding
29429 window margin in that case. */
29430 if (!row->reversed_p && hpos < 0)
29431 hpos = 0;
29432 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29433 hpos = row->used[TEXT_AREA] - 1;
29435 block_input ();
29436 display_and_set_cursor (w, on, hpos, vpos,
29437 w->phys_cursor.x, w->phys_cursor.y);
29438 unblock_input ();
29443 /* Call update_window_cursor with parameter ON_P on all leaf windows
29444 in the window tree rooted at W. */
29446 static void
29447 update_cursor_in_window_tree (struct window *w, bool on_p)
29449 while (w)
29451 if (WINDOWP (w->contents))
29452 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29453 else
29454 update_window_cursor (w, on_p);
29456 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29461 /* EXPORT:
29462 Display the cursor on window W, or clear it, according to ON_P.
29463 Don't change the cursor's position. */
29465 void
29466 x_update_cursor (struct frame *f, bool on_p)
29468 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29472 /* EXPORT:
29473 Clear the cursor of window W to background color, and mark the
29474 cursor as not shown. This is used when the text where the cursor
29475 is about to be rewritten. */
29477 void
29478 x_clear_cursor (struct window *w)
29480 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29481 update_window_cursor (w, false);
29484 #endif /* HAVE_WINDOW_SYSTEM */
29486 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29487 and MSDOS. */
29488 static void
29489 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29490 int start_hpos, int end_hpos,
29491 enum draw_glyphs_face draw)
29493 #ifdef HAVE_WINDOW_SYSTEM
29494 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29496 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29497 return;
29499 #endif
29500 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29501 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29502 #endif
29505 /* Display the active region described by mouse_face_* according to DRAW. */
29507 static void
29508 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29510 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29511 struct frame *f = XFRAME (WINDOW_FRAME (w));
29513 if (/* If window is in the process of being destroyed, don't bother
29514 to do anything. */
29515 w->current_matrix != NULL
29516 /* Don't update mouse highlight if hidden. */
29517 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29518 /* Recognize when we are called to operate on rows that don't exist
29519 anymore. This can happen when a window is split. */
29520 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29522 bool phys_cursor_on_p = w->phys_cursor_on_p;
29523 struct glyph_row *row, *first, *last;
29525 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29526 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29528 for (row = first; row <= last && row->enabled_p; ++row)
29530 int start_hpos, end_hpos, start_x;
29532 /* For all but the first row, the highlight starts at column 0. */
29533 if (row == first)
29535 /* R2L rows have BEG and END in reversed order, but the
29536 screen drawing geometry is always left to right. So
29537 we need to mirror the beginning and end of the
29538 highlighted area in R2L rows. */
29539 if (!row->reversed_p)
29541 start_hpos = hlinfo->mouse_face_beg_col;
29542 start_x = hlinfo->mouse_face_beg_x;
29544 else if (row == last)
29546 start_hpos = hlinfo->mouse_face_end_col;
29547 start_x = hlinfo->mouse_face_end_x;
29549 else
29551 start_hpos = 0;
29552 start_x = 0;
29555 else if (row->reversed_p && row == last)
29557 start_hpos = hlinfo->mouse_face_end_col;
29558 start_x = hlinfo->mouse_face_end_x;
29560 else
29562 start_hpos = 0;
29563 start_x = 0;
29566 if (row == last)
29568 if (!row->reversed_p)
29569 end_hpos = hlinfo->mouse_face_end_col;
29570 else if (row == first)
29571 end_hpos = hlinfo->mouse_face_beg_col;
29572 else
29574 end_hpos = row->used[TEXT_AREA];
29575 if (draw == DRAW_NORMAL_TEXT)
29576 row->fill_line_p = true; /* Clear to end of line. */
29579 else if (row->reversed_p && row == first)
29580 end_hpos = hlinfo->mouse_face_beg_col;
29581 else
29583 end_hpos = row->used[TEXT_AREA];
29584 if (draw == DRAW_NORMAL_TEXT)
29585 row->fill_line_p = true; /* Clear to end of line. */
29588 if (end_hpos > start_hpos)
29590 draw_row_with_mouse_face (w, start_x, row,
29591 start_hpos, end_hpos, draw);
29593 row->mouse_face_p
29594 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29598 /* When we've written over the cursor, arrange for it to
29599 be displayed again. */
29600 if (FRAME_WINDOW_P (f)
29601 && phys_cursor_on_p && !w->phys_cursor_on_p)
29603 #ifdef HAVE_WINDOW_SYSTEM
29604 int hpos = w->phys_cursor.hpos;
29606 /* When the window is hscrolled, cursor hpos can legitimately be
29607 out of bounds, but we draw the cursor at the corresponding
29608 window margin in that case. */
29609 if (!row->reversed_p && hpos < 0)
29610 hpos = 0;
29611 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29612 hpos = row->used[TEXT_AREA] - 1;
29614 block_input ();
29615 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29616 w->phys_cursor.x, w->phys_cursor.y);
29617 unblock_input ();
29618 #endif /* HAVE_WINDOW_SYSTEM */
29622 #ifdef HAVE_WINDOW_SYSTEM
29623 /* Change the mouse cursor. */
29624 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29626 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29627 if (draw == DRAW_NORMAL_TEXT
29628 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29629 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29630 else
29631 #endif
29632 if (draw == DRAW_MOUSE_FACE)
29633 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29634 else
29635 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29637 #endif /* HAVE_WINDOW_SYSTEM */
29640 /* EXPORT:
29641 Clear out the mouse-highlighted active region.
29642 Redraw it un-highlighted first. Value is true if mouse
29643 face was actually drawn unhighlighted. */
29645 bool
29646 clear_mouse_face (Mouse_HLInfo *hlinfo)
29648 bool cleared
29649 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29650 if (cleared)
29651 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29652 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29653 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29654 hlinfo->mouse_face_window = Qnil;
29655 hlinfo->mouse_face_overlay = Qnil;
29656 return cleared;
29659 /* Return true if the coordinates HPOS and VPOS on windows W are
29660 within the mouse face on that window. */
29661 static bool
29662 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29664 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29666 /* Quickly resolve the easy cases. */
29667 if (!(WINDOWP (hlinfo->mouse_face_window)
29668 && XWINDOW (hlinfo->mouse_face_window) == w))
29669 return false;
29670 if (vpos < hlinfo->mouse_face_beg_row
29671 || vpos > hlinfo->mouse_face_end_row)
29672 return false;
29673 if (vpos > hlinfo->mouse_face_beg_row
29674 && vpos < hlinfo->mouse_face_end_row)
29675 return true;
29677 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29679 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29681 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29682 return true;
29684 else if ((vpos == hlinfo->mouse_face_beg_row
29685 && hpos >= hlinfo->mouse_face_beg_col)
29686 || (vpos == hlinfo->mouse_face_end_row
29687 && hpos < hlinfo->mouse_face_end_col))
29688 return true;
29690 else
29692 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29694 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29695 return true;
29697 else if ((vpos == hlinfo->mouse_face_beg_row
29698 && hpos <= hlinfo->mouse_face_beg_col)
29699 || (vpos == hlinfo->mouse_face_end_row
29700 && hpos > hlinfo->mouse_face_end_col))
29701 return true;
29703 return false;
29707 /* EXPORT:
29708 True if physical cursor of window W is within mouse face. */
29710 bool
29711 cursor_in_mouse_face_p (struct window *w)
29713 int hpos = w->phys_cursor.hpos;
29714 int vpos = w->phys_cursor.vpos;
29715 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29717 /* When the window is hscrolled, cursor hpos can legitimately be out
29718 of bounds, but we draw the cursor at the corresponding window
29719 margin in that case. */
29720 if (!row->reversed_p && hpos < 0)
29721 hpos = 0;
29722 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29723 hpos = row->used[TEXT_AREA] - 1;
29725 return coords_in_mouse_face_p (w, hpos, vpos);
29730 /* Find the glyph rows START_ROW and END_ROW of window W that display
29731 characters between buffer positions START_CHARPOS and END_CHARPOS
29732 (excluding END_CHARPOS). DISP_STRING is a display string that
29733 covers these buffer positions. This is similar to
29734 row_containing_pos, but is more accurate when bidi reordering makes
29735 buffer positions change non-linearly with glyph rows. */
29736 static void
29737 rows_from_pos_range (struct window *w,
29738 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29739 Lisp_Object disp_string,
29740 struct glyph_row **start, struct glyph_row **end)
29742 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29743 int last_y = window_text_bottom_y (w);
29744 struct glyph_row *row;
29746 *start = NULL;
29747 *end = NULL;
29749 while (!first->enabled_p
29750 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29751 first++;
29753 /* Find the START row. */
29754 for (row = first;
29755 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29756 row++)
29758 /* A row can potentially be the START row if the range of the
29759 characters it displays intersects the range
29760 [START_CHARPOS..END_CHARPOS). */
29761 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29762 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29763 /* See the commentary in row_containing_pos, for the
29764 explanation of the complicated way to check whether
29765 some position is beyond the end of the characters
29766 displayed by a row. */
29767 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29768 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29769 && !row->ends_at_zv_p
29770 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29771 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29772 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29773 && !row->ends_at_zv_p
29774 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29776 /* Found a candidate row. Now make sure at least one of the
29777 glyphs it displays has a charpos from the range
29778 [START_CHARPOS..END_CHARPOS).
29780 This is not obvious because bidi reordering could make
29781 buffer positions of a row be 1,2,3,102,101,100, and if we
29782 want to highlight characters in [50..60), we don't want
29783 this row, even though [50..60) does intersect [1..103),
29784 the range of character positions given by the row's start
29785 and end positions. */
29786 struct glyph *g = row->glyphs[TEXT_AREA];
29787 struct glyph *e = g + row->used[TEXT_AREA];
29789 while (g < e)
29791 if (((BUFFERP (g->object) || NILP (g->object))
29792 && start_charpos <= g->charpos && g->charpos < end_charpos)
29793 /* A glyph that comes from DISP_STRING is by
29794 definition to be highlighted. */
29795 || EQ (g->object, disp_string))
29796 *start = row;
29797 g++;
29799 if (*start)
29800 break;
29804 /* Find the END row. */
29805 if (!*start
29806 /* If the last row is partially visible, start looking for END
29807 from that row, instead of starting from FIRST. */
29808 && !(row->enabled_p
29809 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29810 row = first;
29811 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29813 struct glyph_row *next = row + 1;
29814 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29816 if (!next->enabled_p
29817 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29818 /* The first row >= START whose range of displayed characters
29819 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29820 is the row END + 1. */
29821 || (start_charpos < next_start
29822 && end_charpos < next_start)
29823 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29824 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29825 && !next->ends_at_zv_p
29826 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29827 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29828 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29829 && !next->ends_at_zv_p
29830 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29832 *end = row;
29833 break;
29835 else
29837 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29838 but none of the characters it displays are in the range, it is
29839 also END + 1. */
29840 struct glyph *g = next->glyphs[TEXT_AREA];
29841 struct glyph *s = g;
29842 struct glyph *e = g + next->used[TEXT_AREA];
29844 while (g < e)
29846 if (((BUFFERP (g->object) || NILP (g->object))
29847 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29848 /* If the buffer position of the first glyph in
29849 the row is equal to END_CHARPOS, it means
29850 the last character to be highlighted is the
29851 newline of ROW, and we must consider NEXT as
29852 END, not END+1. */
29853 || (((!next->reversed_p && g == s)
29854 || (next->reversed_p && g == e - 1))
29855 && (g->charpos == end_charpos
29856 /* Special case for when NEXT is an
29857 empty line at ZV. */
29858 || (g->charpos == -1
29859 && !row->ends_at_zv_p
29860 && next_start == end_charpos)))))
29861 /* A glyph that comes from DISP_STRING is by
29862 definition to be highlighted. */
29863 || EQ (g->object, disp_string))
29864 break;
29865 g++;
29867 if (g == e)
29869 *end = row;
29870 break;
29872 /* The first row that ends at ZV must be the last to be
29873 highlighted. */
29874 else if (next->ends_at_zv_p)
29876 *end = next;
29877 break;
29883 /* This function sets the mouse_face_* elements of HLINFO, assuming
29884 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29885 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29886 for the overlay or run of text properties specifying the mouse
29887 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29888 before-string and after-string that must also be highlighted.
29889 DISP_STRING, if non-nil, is a display string that may cover some
29890 or all of the highlighted text. */
29892 static void
29893 mouse_face_from_buffer_pos (Lisp_Object window,
29894 Mouse_HLInfo *hlinfo,
29895 ptrdiff_t mouse_charpos,
29896 ptrdiff_t start_charpos,
29897 ptrdiff_t end_charpos,
29898 Lisp_Object before_string,
29899 Lisp_Object after_string,
29900 Lisp_Object disp_string)
29902 struct window *w = XWINDOW (window);
29903 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29904 struct glyph_row *r1, *r2;
29905 struct glyph *glyph, *end;
29906 ptrdiff_t ignore, pos;
29907 int x;
29909 eassert (NILP (disp_string) || STRINGP (disp_string));
29910 eassert (NILP (before_string) || STRINGP (before_string));
29911 eassert (NILP (after_string) || STRINGP (after_string));
29913 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29914 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29915 if (r1 == NULL)
29916 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29917 /* If the before-string or display-string contains newlines,
29918 rows_from_pos_range skips to its last row. Move back. */
29919 if (!NILP (before_string) || !NILP (disp_string))
29921 struct glyph_row *prev;
29922 while ((prev = r1 - 1, prev >= first)
29923 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29924 && prev->used[TEXT_AREA] > 0)
29926 struct glyph *beg = prev->glyphs[TEXT_AREA];
29927 glyph = beg + prev->used[TEXT_AREA];
29928 while (--glyph >= beg && NILP (glyph->object));
29929 if (glyph < beg
29930 || !(EQ (glyph->object, before_string)
29931 || EQ (glyph->object, disp_string)))
29932 break;
29933 r1 = prev;
29936 if (r2 == NULL)
29938 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29939 hlinfo->mouse_face_past_end = true;
29941 else if (!NILP (after_string))
29943 /* If the after-string has newlines, advance to its last row. */
29944 struct glyph_row *next;
29945 struct glyph_row *last
29946 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29948 for (next = r2 + 1;
29949 next <= last
29950 && next->used[TEXT_AREA] > 0
29951 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29952 ++next)
29953 r2 = next;
29955 /* The rest of the display engine assumes that mouse_face_beg_row is
29956 either above mouse_face_end_row or identical to it. But with
29957 bidi-reordered continued lines, the row for START_CHARPOS could
29958 be below the row for END_CHARPOS. If so, swap the rows and store
29959 them in correct order. */
29960 if (r1->y > r2->y)
29962 struct glyph_row *tem = r2;
29964 r2 = r1;
29965 r1 = tem;
29968 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29969 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29971 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29972 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29973 could be anywhere in the row and in any order. The strategy
29974 below is to find the leftmost and the rightmost glyph that
29975 belongs to either of these 3 strings, or whose position is
29976 between START_CHARPOS and END_CHARPOS, and highlight all the
29977 glyphs between those two. This may cover more than just the text
29978 between START_CHARPOS and END_CHARPOS if the range of characters
29979 strides the bidi level boundary, e.g. if the beginning is in R2L
29980 text while the end is in L2R text or vice versa. */
29981 if (!r1->reversed_p)
29983 /* This row is in a left to right paragraph. Scan it left to
29984 right. */
29985 glyph = r1->glyphs[TEXT_AREA];
29986 end = glyph + r1->used[TEXT_AREA];
29987 x = r1->x;
29989 /* Skip truncation glyphs at the start of the glyph row. */
29990 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29991 for (; glyph < end
29992 && NILP (glyph->object)
29993 && glyph->charpos < 0;
29994 ++glyph)
29995 x += glyph->pixel_width;
29997 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29998 or DISP_STRING, and the first glyph from buffer whose
29999 position is between START_CHARPOS and END_CHARPOS. */
30000 for (; glyph < end
30001 && !NILP (glyph->object)
30002 && !EQ (glyph->object, disp_string)
30003 && !(BUFFERP (glyph->object)
30004 && (glyph->charpos >= start_charpos
30005 && glyph->charpos < end_charpos));
30006 ++glyph)
30008 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30009 are present at buffer positions between START_CHARPOS and
30010 END_CHARPOS, or if they come from an overlay. */
30011 if (EQ (glyph->object, before_string))
30013 pos = string_buffer_position (before_string,
30014 start_charpos);
30015 /* If pos == 0, it means before_string came from an
30016 overlay, not from a buffer position. */
30017 if (!pos || (pos >= start_charpos && pos < end_charpos))
30018 break;
30020 else if (EQ (glyph->object, after_string))
30022 pos = string_buffer_position (after_string, end_charpos);
30023 if (!pos || (pos >= start_charpos && pos < end_charpos))
30024 break;
30026 x += glyph->pixel_width;
30028 hlinfo->mouse_face_beg_x = x;
30029 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30031 else
30033 /* This row is in a right to left paragraph. Scan it right to
30034 left. */
30035 struct glyph *g;
30037 end = r1->glyphs[TEXT_AREA] - 1;
30038 glyph = end + r1->used[TEXT_AREA];
30040 /* Skip truncation glyphs at the start of the glyph row. */
30041 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30042 for (; glyph > end
30043 && NILP (glyph->object)
30044 && glyph->charpos < 0;
30045 --glyph)
30048 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30049 or DISP_STRING, and the first glyph from buffer whose
30050 position is between START_CHARPOS and END_CHARPOS. */
30051 for (; glyph > end
30052 && !NILP (glyph->object)
30053 && !EQ (glyph->object, disp_string)
30054 && !(BUFFERP (glyph->object)
30055 && (glyph->charpos >= start_charpos
30056 && glyph->charpos < end_charpos));
30057 --glyph)
30059 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30060 are present at buffer positions between START_CHARPOS and
30061 END_CHARPOS, or if they come from an overlay. */
30062 if (EQ (glyph->object, before_string))
30064 pos = string_buffer_position (before_string, start_charpos);
30065 /* If pos == 0, it means before_string came from an
30066 overlay, not from a buffer position. */
30067 if (!pos || (pos >= start_charpos && pos < end_charpos))
30068 break;
30070 else if (EQ (glyph->object, after_string))
30072 pos = string_buffer_position (after_string, end_charpos);
30073 if (!pos || (pos >= start_charpos && pos < end_charpos))
30074 break;
30078 glyph++; /* first glyph to the right of the highlighted area */
30079 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30080 x += g->pixel_width;
30081 hlinfo->mouse_face_beg_x = x;
30082 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30085 /* If the highlight ends in a different row, compute GLYPH and END
30086 for the end row. Otherwise, reuse the values computed above for
30087 the row where the highlight begins. */
30088 if (r2 != r1)
30090 if (!r2->reversed_p)
30092 glyph = r2->glyphs[TEXT_AREA];
30093 end = glyph + r2->used[TEXT_AREA];
30094 x = r2->x;
30096 else
30098 end = r2->glyphs[TEXT_AREA] - 1;
30099 glyph = end + r2->used[TEXT_AREA];
30103 if (!r2->reversed_p)
30105 /* Skip truncation and continuation glyphs near the end of the
30106 row, and also blanks and stretch glyphs inserted by
30107 extend_face_to_end_of_line. */
30108 while (end > glyph
30109 && NILP ((end - 1)->object))
30110 --end;
30111 /* Scan the rest of the glyph row from the end, looking for the
30112 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30113 DISP_STRING, or whose position is between START_CHARPOS
30114 and END_CHARPOS */
30115 for (--end;
30116 end > glyph
30117 && !NILP (end->object)
30118 && !EQ (end->object, disp_string)
30119 && !(BUFFERP (end->object)
30120 && (end->charpos >= start_charpos
30121 && end->charpos < end_charpos));
30122 --end)
30124 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30125 are present at buffer positions between START_CHARPOS and
30126 END_CHARPOS, or if they come from an overlay. */
30127 if (EQ (end->object, before_string))
30129 pos = string_buffer_position (before_string, start_charpos);
30130 if (!pos || (pos >= start_charpos && pos < end_charpos))
30131 break;
30133 else if (EQ (end->object, after_string))
30135 pos = string_buffer_position (after_string, end_charpos);
30136 if (!pos || (pos >= start_charpos && pos < end_charpos))
30137 break;
30140 /* Find the X coordinate of the last glyph to be highlighted. */
30141 for (; glyph <= end; ++glyph)
30142 x += glyph->pixel_width;
30144 hlinfo->mouse_face_end_x = x;
30145 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30147 else
30149 /* Skip truncation and continuation glyphs near the end of the
30150 row, and also blanks and stretch glyphs inserted by
30151 extend_face_to_end_of_line. */
30152 x = r2->x;
30153 end++;
30154 while (end < glyph
30155 && NILP (end->object))
30157 x += end->pixel_width;
30158 ++end;
30160 /* Scan the rest of the glyph row from the end, looking for the
30161 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30162 DISP_STRING, or whose position is between START_CHARPOS
30163 and END_CHARPOS */
30164 for ( ;
30165 end < glyph
30166 && !NILP (end->object)
30167 && !EQ (end->object, disp_string)
30168 && !(BUFFERP (end->object)
30169 && (end->charpos >= start_charpos
30170 && end->charpos < end_charpos));
30171 ++end)
30173 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30174 are present at buffer positions between START_CHARPOS and
30175 END_CHARPOS, or if they come from an overlay. */
30176 if (EQ (end->object, before_string))
30178 pos = string_buffer_position (before_string, start_charpos);
30179 if (!pos || (pos >= start_charpos && pos < end_charpos))
30180 break;
30182 else if (EQ (end->object, after_string))
30184 pos = string_buffer_position (after_string, end_charpos);
30185 if (!pos || (pos >= start_charpos && pos < end_charpos))
30186 break;
30188 x += end->pixel_width;
30190 /* If we exited the above loop because we arrived at the last
30191 glyph of the row, and its buffer position is still not in
30192 range, it means the last character in range is the preceding
30193 newline. Bump the end column and x values to get past the
30194 last glyph. */
30195 if (end == glyph
30196 && BUFFERP (end->object)
30197 && (end->charpos < start_charpos
30198 || end->charpos >= end_charpos))
30200 x += end->pixel_width;
30201 ++end;
30203 hlinfo->mouse_face_end_x = x;
30204 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30207 hlinfo->mouse_face_window = window;
30208 hlinfo->mouse_face_face_id
30209 = face_at_buffer_position (w, mouse_charpos, &ignore,
30210 mouse_charpos + 1,
30211 !hlinfo->mouse_face_hidden, -1);
30212 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30215 /* The following function is not used anymore (replaced with
30216 mouse_face_from_string_pos), but I leave it here for the time
30217 being, in case someone would. */
30219 #if false /* not used */
30221 /* Find the position of the glyph for position POS in OBJECT in
30222 window W's current matrix, and return in *X, *Y the pixel
30223 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30225 RIGHT_P means return the position of the right edge of the glyph.
30226 !RIGHT_P means return the left edge position.
30228 If no glyph for POS exists in the matrix, return the position of
30229 the glyph with the next smaller position that is in the matrix, if
30230 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30231 exists in the matrix, return the position of the glyph with the
30232 next larger position in OBJECT.
30234 Value is true if a glyph was found. */
30236 static bool
30237 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30238 int *hpos, int *vpos, int *x, int *y, bool right_p)
30240 int yb = window_text_bottom_y (w);
30241 struct glyph_row *r;
30242 struct glyph *best_glyph = NULL;
30243 struct glyph_row *best_row = NULL;
30244 int best_x = 0;
30246 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30247 r->enabled_p && r->y < yb;
30248 ++r)
30250 struct glyph *g = r->glyphs[TEXT_AREA];
30251 struct glyph *e = g + r->used[TEXT_AREA];
30252 int gx;
30254 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30255 if (EQ (g->object, object))
30257 if (g->charpos == pos)
30259 best_glyph = g;
30260 best_x = gx;
30261 best_row = r;
30262 goto found;
30264 else if (best_glyph == NULL
30265 || ((eabs (g->charpos - pos)
30266 < eabs (best_glyph->charpos - pos))
30267 && (right_p
30268 ? g->charpos < pos
30269 : g->charpos > pos)))
30271 best_glyph = g;
30272 best_x = gx;
30273 best_row = r;
30278 found:
30280 if (best_glyph)
30282 *x = best_x;
30283 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30285 if (right_p)
30287 *x += best_glyph->pixel_width;
30288 ++*hpos;
30291 *y = best_row->y;
30292 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30295 return best_glyph != NULL;
30297 #endif /* not used */
30299 /* Find the positions of the first and the last glyphs in window W's
30300 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30301 (assumed to be a string), and return in HLINFO's mouse_face_*
30302 members the pixel and column/row coordinates of those glyphs. */
30304 static void
30305 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30306 Lisp_Object object,
30307 ptrdiff_t startpos, ptrdiff_t endpos)
30309 int yb = window_text_bottom_y (w);
30310 struct glyph_row *r;
30311 struct glyph *g, *e;
30312 int gx;
30313 bool found = false;
30315 /* Find the glyph row with at least one position in the range
30316 [STARTPOS..ENDPOS), and the first glyph in that row whose
30317 position belongs to that range. */
30318 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30319 r->enabled_p && r->y < yb;
30320 ++r)
30322 if (!r->reversed_p)
30324 g = r->glyphs[TEXT_AREA];
30325 e = g + r->used[TEXT_AREA];
30326 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30327 if (EQ (g->object, object)
30328 && startpos <= g->charpos && g->charpos < endpos)
30330 hlinfo->mouse_face_beg_row
30331 = MATRIX_ROW_VPOS (r, w->current_matrix);
30332 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30333 hlinfo->mouse_face_beg_x = gx;
30334 found = true;
30335 break;
30338 else
30340 struct glyph *g1;
30342 e = r->glyphs[TEXT_AREA];
30343 g = e + r->used[TEXT_AREA];
30344 for ( ; g > e; --g)
30345 if (EQ ((g-1)->object, object)
30346 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30348 hlinfo->mouse_face_beg_row
30349 = MATRIX_ROW_VPOS (r, w->current_matrix);
30350 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30351 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30352 gx += g1->pixel_width;
30353 hlinfo->mouse_face_beg_x = gx;
30354 found = true;
30355 break;
30358 if (found)
30359 break;
30362 if (!found)
30363 return;
30365 /* Starting with the next row, look for the first row which does NOT
30366 include any glyphs whose positions are in the range. */
30367 for (++r; r->enabled_p && r->y < yb; ++r)
30369 g = r->glyphs[TEXT_AREA];
30370 e = g + r->used[TEXT_AREA];
30371 found = false;
30372 for ( ; g < e; ++g)
30373 if (EQ (g->object, object)
30374 && startpos <= g->charpos && g->charpos < endpos)
30376 found = true;
30377 break;
30379 if (!found)
30380 break;
30383 /* The highlighted region ends on the previous row. */
30384 r--;
30386 /* Set the end row. */
30387 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30389 /* Compute and set the end column and the end column's horizontal
30390 pixel coordinate. */
30391 if (!r->reversed_p)
30393 g = r->glyphs[TEXT_AREA];
30394 e = g + r->used[TEXT_AREA];
30395 for ( ; e > g; --e)
30396 if (EQ ((e-1)->object, object)
30397 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30398 break;
30399 hlinfo->mouse_face_end_col = e - g;
30401 for (gx = r->x; g < e; ++g)
30402 gx += g->pixel_width;
30403 hlinfo->mouse_face_end_x = gx;
30405 else
30407 e = r->glyphs[TEXT_AREA];
30408 g = e + r->used[TEXT_AREA];
30409 for (gx = r->x ; e < g; ++e)
30411 if (EQ (e->object, object)
30412 && startpos <= e->charpos && e->charpos < endpos)
30413 break;
30414 gx += e->pixel_width;
30416 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30417 hlinfo->mouse_face_end_x = gx;
30421 #ifdef HAVE_WINDOW_SYSTEM
30423 /* See if position X, Y is within a hot-spot of an image. */
30425 static bool
30426 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30428 if (!CONSP (hot_spot))
30429 return false;
30431 if (EQ (XCAR (hot_spot), Qrect))
30433 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30434 Lisp_Object rect = XCDR (hot_spot);
30435 Lisp_Object tem;
30436 if (!CONSP (rect))
30437 return false;
30438 if (!CONSP (XCAR (rect)))
30439 return false;
30440 if (!CONSP (XCDR (rect)))
30441 return false;
30442 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30443 return false;
30444 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30445 return false;
30446 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30447 return false;
30448 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30449 return false;
30450 return true;
30452 else if (EQ (XCAR (hot_spot), Qcircle))
30454 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30455 Lisp_Object circ = XCDR (hot_spot);
30456 Lisp_Object lr, lx0, ly0;
30457 if (CONSP (circ)
30458 && CONSP (XCAR (circ))
30459 && (lr = XCDR (circ), NUMBERP (lr))
30460 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30461 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30463 double r = XFLOATINT (lr);
30464 double dx = XINT (lx0) - x;
30465 double dy = XINT (ly0) - y;
30466 return (dx * dx + dy * dy <= r * r);
30469 else if (EQ (XCAR (hot_spot), Qpoly))
30471 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30472 if (VECTORP (XCDR (hot_spot)))
30474 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30475 Lisp_Object *poly = v->contents;
30476 ptrdiff_t n = v->header.size;
30477 ptrdiff_t i;
30478 bool inside = false;
30479 Lisp_Object lx, ly;
30480 int x0, y0;
30482 /* Need an even number of coordinates, and at least 3 edges. */
30483 if (n < 6 || n & 1)
30484 return false;
30486 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30487 If count is odd, we are inside polygon. Pixels on edges
30488 may or may not be included depending on actual geometry of the
30489 polygon. */
30490 if ((lx = poly[n-2], !INTEGERP (lx))
30491 || (ly = poly[n-1], !INTEGERP (lx)))
30492 return false;
30493 x0 = XINT (lx), y0 = XINT (ly);
30494 for (i = 0; i < n; i += 2)
30496 int x1 = x0, y1 = y0;
30497 if ((lx = poly[i], !INTEGERP (lx))
30498 || (ly = poly[i+1], !INTEGERP (ly)))
30499 return false;
30500 x0 = XINT (lx), y0 = XINT (ly);
30502 /* Does this segment cross the X line? */
30503 if (x0 >= x)
30505 if (x1 >= x)
30506 continue;
30508 else if (x1 < x)
30509 continue;
30510 if (y > y0 && y > y1)
30511 continue;
30512 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30513 inside = !inside;
30515 return inside;
30518 return false;
30521 Lisp_Object
30522 find_hot_spot (Lisp_Object map, int x, int y)
30524 while (CONSP (map))
30526 if (CONSP (XCAR (map))
30527 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30528 return XCAR (map);
30529 map = XCDR (map);
30532 return Qnil;
30535 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30536 3, 3, 0,
30537 doc: /* Lookup in image map MAP coordinates X and Y.
30538 An image map is an alist where each element has the format (AREA ID PLIST).
30539 An AREA is specified as either a rectangle, a circle, or a polygon:
30540 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30541 pixel coordinates of the upper left and bottom right corners.
30542 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30543 and the radius of the circle; r may be a float or integer.
30544 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30545 vector describes one corner in the polygon.
30546 Returns the alist element for the first matching AREA in MAP. */)
30547 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30549 if (NILP (map))
30550 return Qnil;
30552 CHECK_NUMBER (x);
30553 CHECK_NUMBER (y);
30555 return find_hot_spot (map,
30556 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30557 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30559 #endif /* HAVE_WINDOW_SYSTEM */
30562 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30563 static void
30564 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30566 #ifdef HAVE_WINDOW_SYSTEM
30567 if (!FRAME_WINDOW_P (f))
30568 return;
30570 /* Do not change cursor shape while dragging mouse. */
30571 if (EQ (do_mouse_tracking, Qdragging))
30572 return;
30574 if (!NILP (pointer))
30576 if (EQ (pointer, Qarrow))
30577 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30578 else if (EQ (pointer, Qhand))
30579 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30580 else if (EQ (pointer, Qtext))
30581 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30582 else if (EQ (pointer, intern ("hdrag")))
30583 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30584 else if (EQ (pointer, intern ("nhdrag")))
30585 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30586 # ifdef HAVE_X_WINDOWS
30587 else if (EQ (pointer, intern ("vdrag")))
30588 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30589 # endif
30590 else if (EQ (pointer, intern ("hourglass")))
30591 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30592 else if (EQ (pointer, Qmodeline))
30593 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30594 else
30595 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30598 if (cursor != No_Cursor)
30599 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30600 #endif
30603 /* Take proper action when mouse has moved to the mode or header line
30604 or marginal area AREA of window W, x-position X and y-position Y.
30605 X is relative to the start of the text display area of W, so the
30606 width of bitmap areas and scroll bars must be subtracted to get a
30607 position relative to the start of the mode line. */
30609 static void
30610 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30611 enum window_part area)
30613 struct window *w = XWINDOW (window);
30614 struct frame *f = XFRAME (w->frame);
30615 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30616 #ifdef HAVE_WINDOW_SYSTEM
30617 Display_Info *dpyinfo;
30618 #endif
30619 Cursor cursor = No_Cursor;
30620 Lisp_Object pointer = Qnil;
30621 int dx, dy, width, height;
30622 ptrdiff_t charpos;
30623 Lisp_Object string, object = Qnil;
30624 Lisp_Object pos UNINIT;
30625 Lisp_Object mouse_face;
30626 int original_x_pixel = x;
30627 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30628 struct glyph_row *row UNINIT;
30630 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30632 int x0;
30633 struct glyph *end;
30635 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30636 returns them in row/column units! */
30637 string = mode_line_string (w, area, &x, &y, &charpos,
30638 &object, &dx, &dy, &width, &height);
30640 row = (area == ON_MODE_LINE
30641 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30642 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30644 /* Find the glyph under the mouse pointer. */
30645 if (row->mode_line_p && row->enabled_p)
30647 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30648 end = glyph + row->used[TEXT_AREA];
30650 for (x0 = original_x_pixel;
30651 glyph < end && x0 >= glyph->pixel_width;
30652 ++glyph)
30653 x0 -= glyph->pixel_width;
30655 if (glyph >= end)
30656 glyph = NULL;
30659 else
30661 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30662 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30663 returns them in row/column units! */
30664 string = marginal_area_string (w, area, &x, &y, &charpos,
30665 &object, &dx, &dy, &width, &height);
30668 Lisp_Object help = Qnil;
30670 #ifdef HAVE_WINDOW_SYSTEM
30671 if (IMAGEP (object))
30673 Lisp_Object image_map, hotspot;
30674 if ((image_map = Fplist_get (XCDR (object), QCmap),
30675 !NILP (image_map))
30676 && (hotspot = find_hot_spot (image_map, dx, dy),
30677 CONSP (hotspot))
30678 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30680 Lisp_Object plist;
30682 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30683 If so, we could look for mouse-enter, mouse-leave
30684 properties in PLIST (and do something...). */
30685 hotspot = XCDR (hotspot);
30686 if (CONSP (hotspot)
30687 && (plist = XCAR (hotspot), CONSP (plist)))
30689 pointer = Fplist_get (plist, Qpointer);
30690 if (NILP (pointer))
30691 pointer = Qhand;
30692 help = Fplist_get (plist, Qhelp_echo);
30693 if (!NILP (help))
30695 help_echo_string = help;
30696 XSETWINDOW (help_echo_window, w);
30697 help_echo_object = w->contents;
30698 help_echo_pos = charpos;
30702 if (NILP (pointer))
30703 pointer = Fplist_get (XCDR (object), QCpointer);
30705 #endif /* HAVE_WINDOW_SYSTEM */
30707 if (STRINGP (string))
30708 pos = make_number (charpos);
30710 /* Set the help text and mouse pointer. If the mouse is on a part
30711 of the mode line without any text (e.g. past the right edge of
30712 the mode line text), use the default help text and pointer. */
30713 if (STRINGP (string) || area == ON_MODE_LINE)
30715 /* Arrange to display the help by setting the global variables
30716 help_echo_string, help_echo_object, and help_echo_pos. */
30717 if (NILP (help))
30719 if (STRINGP (string))
30720 help = Fget_text_property (pos, Qhelp_echo, string);
30722 if (!NILP (help))
30724 help_echo_string = help;
30725 XSETWINDOW (help_echo_window, w);
30726 help_echo_object = string;
30727 help_echo_pos = charpos;
30729 else if (area == ON_MODE_LINE)
30731 Lisp_Object default_help
30732 = buffer_local_value (Qmode_line_default_help_echo,
30733 w->contents);
30735 if (STRINGP (default_help))
30737 help_echo_string = default_help;
30738 XSETWINDOW (help_echo_window, w);
30739 help_echo_object = Qnil;
30740 help_echo_pos = -1;
30745 #ifdef HAVE_WINDOW_SYSTEM
30746 /* Change the mouse pointer according to what is under it. */
30747 if (FRAME_WINDOW_P (f))
30749 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30750 || minibuf_level
30751 || NILP (Vresize_mini_windows));
30753 dpyinfo = FRAME_DISPLAY_INFO (f);
30754 if (STRINGP (string))
30756 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30758 if (NILP (pointer))
30759 pointer = Fget_text_property (pos, Qpointer, string);
30761 /* Change the mouse pointer according to what is under X/Y. */
30762 if (NILP (pointer)
30763 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30765 Lisp_Object map;
30766 map = Fget_text_property (pos, Qlocal_map, string);
30767 if (!KEYMAPP (map))
30768 map = Fget_text_property (pos, Qkeymap, string);
30769 if (!KEYMAPP (map) && draggable)
30770 cursor = dpyinfo->vertical_scroll_bar_cursor;
30773 else if (draggable)
30774 /* Default mode-line pointer. */
30775 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30777 #endif
30780 /* Change the mouse face according to what is under X/Y. */
30781 bool mouse_face_shown = false;
30782 if (STRINGP (string))
30784 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30785 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30786 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30787 && glyph)
30789 Lisp_Object b, e;
30791 struct glyph * tmp_glyph;
30793 int gpos;
30794 int gseq_length;
30795 int total_pixel_width;
30796 ptrdiff_t begpos, endpos, ignore;
30798 int vpos, hpos;
30800 b = Fprevious_single_property_change (make_number (charpos + 1),
30801 Qmouse_face, string, Qnil);
30802 if (NILP (b))
30803 begpos = 0;
30804 else
30805 begpos = XINT (b);
30807 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30808 if (NILP (e))
30809 endpos = SCHARS (string);
30810 else
30811 endpos = XINT (e);
30813 /* Calculate the glyph position GPOS of GLYPH in the
30814 displayed string, relative to the beginning of the
30815 highlighted part of the string.
30817 Note: GPOS is different from CHARPOS. CHARPOS is the
30818 position of GLYPH in the internal string object. A mode
30819 line string format has structures which are converted to
30820 a flattened string by the Emacs Lisp interpreter. The
30821 internal string is an element of those structures. The
30822 displayed string is the flattened string. */
30823 tmp_glyph = row_start_glyph;
30824 while (tmp_glyph < glyph
30825 && (!(EQ (tmp_glyph->object, glyph->object)
30826 && begpos <= tmp_glyph->charpos
30827 && tmp_glyph->charpos < endpos)))
30828 tmp_glyph++;
30829 gpos = glyph - tmp_glyph;
30831 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30832 the highlighted part of the displayed string to which
30833 GLYPH belongs. Note: GSEQ_LENGTH is different from
30834 SCHARS (STRING), because the latter returns the length of
30835 the internal string. */
30836 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30837 tmp_glyph > glyph
30838 && (!(EQ (tmp_glyph->object, glyph->object)
30839 && begpos <= tmp_glyph->charpos
30840 && tmp_glyph->charpos < endpos));
30841 tmp_glyph--)
30843 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30845 /* Calculate the total pixel width of all the glyphs between
30846 the beginning of the highlighted area and GLYPH. */
30847 total_pixel_width = 0;
30848 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30849 total_pixel_width += tmp_glyph->pixel_width;
30851 /* Pre calculation of re-rendering position. Note: X is in
30852 column units here, after the call to mode_line_string or
30853 marginal_area_string. */
30854 hpos = x - gpos;
30855 vpos = (area == ON_MODE_LINE
30856 ? (w->current_matrix)->nrows - 1
30857 : 0);
30859 /* If GLYPH's position is included in the region that is
30860 already drawn in mouse face, we have nothing to do. */
30861 if ( EQ (window, hlinfo->mouse_face_window)
30862 && (!row->reversed_p
30863 ? (hlinfo->mouse_face_beg_col <= hpos
30864 && hpos < hlinfo->mouse_face_end_col)
30865 /* In R2L rows we swap BEG and END, see below. */
30866 : (hlinfo->mouse_face_end_col <= hpos
30867 && hpos < hlinfo->mouse_face_beg_col))
30868 && hlinfo->mouse_face_beg_row == vpos )
30869 return;
30871 if (clear_mouse_face (hlinfo))
30872 cursor = No_Cursor;
30874 if (!row->reversed_p)
30876 hlinfo->mouse_face_beg_col = hpos;
30877 hlinfo->mouse_face_beg_x = original_x_pixel
30878 - (total_pixel_width + dx);
30879 hlinfo->mouse_face_end_col = hpos + gseq_length;
30880 hlinfo->mouse_face_end_x = 0;
30882 else
30884 /* In R2L rows, show_mouse_face expects BEG and END
30885 coordinates to be swapped. */
30886 hlinfo->mouse_face_end_col = hpos;
30887 hlinfo->mouse_face_end_x = original_x_pixel
30888 - (total_pixel_width + dx);
30889 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30890 hlinfo->mouse_face_beg_x = 0;
30893 hlinfo->mouse_face_beg_row = vpos;
30894 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30895 hlinfo->mouse_face_past_end = false;
30896 hlinfo->mouse_face_window = window;
30898 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30899 charpos,
30900 0, &ignore,
30901 glyph->face_id,
30902 true);
30903 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30904 mouse_face_shown = true;
30906 if (NILP (pointer))
30907 pointer = Qhand;
30911 /* If mouse-face doesn't need to be shown, clear any existing
30912 mouse-face. */
30913 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30914 clear_mouse_face (hlinfo);
30916 define_frame_cursor1 (f, cursor, pointer);
30920 /* EXPORT:
30921 Take proper action when the mouse has moved to position X, Y on
30922 frame F with regards to highlighting portions of display that have
30923 mouse-face properties. Also de-highlight portions of display where
30924 the mouse was before, set the mouse pointer shape as appropriate
30925 for the mouse coordinates, and activate help echo (tooltips).
30926 X and Y can be negative or out of range. */
30928 void
30929 note_mouse_highlight (struct frame *f, int x, int y)
30931 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30932 enum window_part part = ON_NOTHING;
30933 Lisp_Object window;
30934 struct window *w;
30935 Cursor cursor = No_Cursor;
30936 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30937 struct buffer *b;
30939 /* When a menu is active, don't highlight because this looks odd. */
30940 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30941 if (popup_activated ())
30942 return;
30943 #endif
30945 if (!f->glyphs_initialized_p
30946 || f->pointer_invisible)
30947 return;
30949 hlinfo->mouse_face_mouse_x = x;
30950 hlinfo->mouse_face_mouse_y = y;
30951 hlinfo->mouse_face_mouse_frame = f;
30953 if (hlinfo->mouse_face_defer)
30954 return;
30956 /* Which window is that in? */
30957 window = window_from_coordinates (f, x, y, &part, true);
30959 /* If displaying active text in another window, clear that. */
30960 if (! EQ (window, hlinfo->mouse_face_window)
30961 /* Also clear if we move out of text area in same window. */
30962 || (!NILP (hlinfo->mouse_face_window)
30963 && !NILP (window)
30964 && part != ON_TEXT
30965 && part != ON_MODE_LINE
30966 && part != ON_HEADER_LINE))
30967 clear_mouse_face (hlinfo);
30969 /* Reset help_echo_string. It will get recomputed below. */
30970 help_echo_string = Qnil;
30972 #ifdef HAVE_WINDOW_SYSTEM
30973 /* If the cursor is on the internal border of FRAME and FRAME's
30974 internal border is draggable, provide some visual feedback. */
30975 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
30976 && !NILP (get_frame_param (f, Qdrag_internal_border)))
30978 enum internal_border_part part = frame_internal_border_part (f, x, y);
30980 switch (part)
30982 case INTERNAL_BORDER_NONE:
30983 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30984 /* Reset cursor. */
30985 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30986 break;
30987 case INTERNAL_BORDER_LEFT_EDGE:
30988 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
30989 break;
30990 case INTERNAL_BORDER_TOP_LEFT_CORNER:
30991 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
30992 break;
30993 case INTERNAL_BORDER_TOP_EDGE:
30994 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
30995 break;
30996 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
30997 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
30998 break;
30999 case INTERNAL_BORDER_RIGHT_EDGE:
31000 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31001 break;
31002 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31003 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31004 break;
31005 case INTERNAL_BORDER_BOTTOM_EDGE:
31006 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31007 break;
31008 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31009 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31010 break;
31011 default:
31012 /* This should not happen. */
31013 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31014 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31017 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31019 /* Do we really want a help echo here? */
31020 help_echo_string = build_string ("drag-mouse-1: resize frame");
31021 goto set_cursor;
31024 #endif /* HAVE_WINDOW_SYSTEM */
31026 /* Not on a window -> return. */
31027 if (!WINDOWP (window))
31028 return;
31030 /* Convert to window-relative pixel coordinates. */
31031 w = XWINDOW (window);
31032 frame_to_window_pixel_xy (w, &x, &y);
31034 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31035 /* Handle tool-bar window differently since it doesn't display a
31036 buffer. */
31037 if (EQ (window, f->tool_bar_window))
31039 note_tool_bar_highlight (f, x, y);
31040 return;
31042 #endif
31044 /* Mouse is on the mode, header line or margin? */
31045 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31046 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31048 note_mode_line_or_margin_highlight (window, x, y, part);
31050 #ifdef HAVE_WINDOW_SYSTEM
31051 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31053 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31054 /* Show non-text cursor (Bug#16647). */
31055 goto set_cursor;
31057 else
31058 #endif
31059 return;
31062 #ifdef HAVE_WINDOW_SYSTEM
31063 if (part == ON_VERTICAL_BORDER)
31065 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31066 help_echo_string = build_string ("drag-mouse-1: resize");
31067 goto set_cursor;
31069 else if (part == ON_RIGHT_DIVIDER)
31071 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31072 help_echo_string = build_string ("drag-mouse-1: resize");
31073 goto set_cursor;
31075 else if (part == ON_BOTTOM_DIVIDER)
31076 if (! WINDOW_BOTTOMMOST_P (w)
31077 || minibuf_level
31078 || NILP (Vresize_mini_windows))
31080 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31081 help_echo_string = build_string ("drag-mouse-1: resize");
31082 goto set_cursor;
31084 else
31085 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31086 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31087 || part == ON_VERTICAL_SCROLL_BAR
31088 || part == ON_HORIZONTAL_SCROLL_BAR)
31089 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31090 else
31091 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31092 #endif
31094 /* Are we in a window whose display is up to date?
31095 And verify the buffer's text has not changed. */
31096 b = XBUFFER (w->contents);
31097 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31099 int hpos, vpos, dx, dy, area = LAST_AREA;
31100 ptrdiff_t pos;
31101 struct glyph *glyph;
31102 Lisp_Object object;
31103 Lisp_Object mouse_face = Qnil, position;
31104 Lisp_Object *overlay_vec = NULL;
31105 ptrdiff_t i, noverlays;
31106 struct buffer *obuf;
31107 ptrdiff_t obegv, ozv;
31108 bool same_region;
31110 /* Find the glyph under X/Y. */
31111 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31113 #ifdef HAVE_WINDOW_SYSTEM
31114 /* Look for :pointer property on image. */
31115 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31117 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31118 if (img != NULL && IMAGEP (img->spec))
31120 Lisp_Object image_map, hotspot;
31121 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31122 !NILP (image_map))
31123 && (hotspot = find_hot_spot (image_map,
31124 glyph->slice.img.x + dx,
31125 glyph->slice.img.y + dy),
31126 CONSP (hotspot))
31127 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31129 Lisp_Object plist;
31131 /* Could check XCAR (hotspot) to see if we enter/leave
31132 this hot-spot.
31133 If so, we could look for mouse-enter, mouse-leave
31134 properties in PLIST (and do something...). */
31135 hotspot = XCDR (hotspot);
31136 if (CONSP (hotspot)
31137 && (plist = XCAR (hotspot), CONSP (plist)))
31139 pointer = Fplist_get (plist, Qpointer);
31140 if (NILP (pointer))
31141 pointer = Qhand;
31142 help_echo_string = Fplist_get (plist, Qhelp_echo);
31143 if (!NILP (help_echo_string))
31145 help_echo_window = window;
31146 help_echo_object = glyph->object;
31147 help_echo_pos = glyph->charpos;
31151 if (NILP (pointer))
31152 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31155 #endif /* HAVE_WINDOW_SYSTEM */
31157 /* Clear mouse face if X/Y not over text. */
31158 if (glyph == NULL
31159 || area != TEXT_AREA
31160 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31161 /* Glyph's OBJECT is nil for glyphs inserted by the
31162 display engine for its internal purposes, like truncation
31163 and continuation glyphs and blanks beyond the end of
31164 line's text on text terminals. If we are over such a
31165 glyph, we are not over any text. */
31166 || NILP (glyph->object)
31167 /* R2L rows have a stretch glyph at their front, which
31168 stands for no text, whereas L2R rows have no glyphs at
31169 all beyond the end of text. Treat such stretch glyphs
31170 like we do with NULL glyphs in L2R rows. */
31171 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31172 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31173 && glyph->type == STRETCH_GLYPH
31174 && glyph->avoid_cursor_p))
31176 if (clear_mouse_face (hlinfo))
31177 cursor = No_Cursor;
31178 if (FRAME_WINDOW_P (f) && NILP (pointer))
31180 #ifdef HAVE_WINDOW_SYSTEM
31181 if (area != TEXT_AREA)
31182 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31183 else
31184 pointer = Vvoid_text_area_pointer;
31185 #endif
31187 goto set_cursor;
31190 pos = glyph->charpos;
31191 object = glyph->object;
31192 if (!STRINGP (object) && !BUFFERP (object))
31193 goto set_cursor;
31195 /* If we get an out-of-range value, return now; avoid an error. */
31196 if (BUFFERP (object) && pos > BUF_Z (b))
31197 goto set_cursor;
31199 /* Make the window's buffer temporarily current for
31200 overlays_at and compute_char_face. */
31201 obuf = current_buffer;
31202 current_buffer = b;
31203 obegv = BEGV;
31204 ozv = ZV;
31205 BEGV = BEG;
31206 ZV = Z;
31208 /* Is this char mouse-active or does it have help-echo? */
31209 position = make_number (pos);
31211 USE_SAFE_ALLOCA;
31213 if (BUFFERP (object))
31215 /* Put all the overlays we want in a vector in overlay_vec. */
31216 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31217 /* Sort overlays into increasing priority order. */
31218 noverlays = sort_overlays (overlay_vec, noverlays, w);
31220 else
31221 noverlays = 0;
31223 if (NILP (Vmouse_highlight))
31225 clear_mouse_face (hlinfo);
31226 goto check_help_echo;
31229 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31231 if (same_region)
31232 cursor = No_Cursor;
31234 /* Check mouse-face highlighting. */
31235 if (! same_region
31236 /* If there exists an overlay with mouse-face overlapping
31237 the one we are currently highlighting, we have to
31238 check if we enter the overlapping overlay, and then
31239 highlight only that. */
31240 || (OVERLAYP (hlinfo->mouse_face_overlay)
31241 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31243 /* Find the highest priority overlay with a mouse-face. */
31244 Lisp_Object overlay = Qnil;
31245 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31247 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31248 if (!NILP (mouse_face))
31249 overlay = overlay_vec[i];
31252 /* If we're highlighting the same overlay as before, there's
31253 no need to do that again. */
31254 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31255 goto check_help_echo;
31257 /* Clear the display of the old active region, if any. */
31258 if (clear_mouse_face (hlinfo))
31259 cursor = No_Cursor;
31261 /* Record the overlay, if any, to be highlighted. */
31262 hlinfo->mouse_face_overlay = overlay;
31264 /* If no overlay applies, get a text property. */
31265 if (NILP (overlay))
31266 mouse_face = Fget_text_property (position, Qmouse_face, object);
31268 /* Next, compute the bounds of the mouse highlighting and
31269 display it. */
31270 if (!NILP (mouse_face) && STRINGP (object))
31272 /* The mouse-highlighting comes from a display string
31273 with a mouse-face. */
31274 Lisp_Object s, e;
31275 ptrdiff_t ignore;
31277 s = Fprevious_single_property_change
31278 (make_number (pos + 1), Qmouse_face, object, Qnil);
31279 e = Fnext_single_property_change
31280 (position, Qmouse_face, object, Qnil);
31281 if (NILP (s))
31282 s = make_number (0);
31283 if (NILP (e))
31284 e = make_number (SCHARS (object));
31285 mouse_face_from_string_pos (w, hlinfo, object,
31286 XINT (s), XINT (e));
31287 hlinfo->mouse_face_past_end = false;
31288 hlinfo->mouse_face_window = window;
31289 hlinfo->mouse_face_face_id
31290 = face_at_string_position (w, object, pos, 0, &ignore,
31291 glyph->face_id, true);
31292 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31293 cursor = No_Cursor;
31295 else
31297 /* The mouse-highlighting, if any, comes from an overlay
31298 or text property in the buffer. */
31299 Lisp_Object buffer UNINIT;
31300 Lisp_Object disp_string UNINIT;
31302 if (STRINGP (object))
31304 /* If we are on a display string with no mouse-face,
31305 check if the text under it has one. */
31306 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31307 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31308 pos = string_buffer_position (object, start);
31309 if (pos > 0)
31311 mouse_face = get_char_property_and_overlay
31312 (make_number (pos), Qmouse_face, w->contents, &overlay);
31313 buffer = w->contents;
31314 disp_string = object;
31317 else
31319 buffer = object;
31320 disp_string = Qnil;
31323 if (!NILP (mouse_face))
31325 Lisp_Object before, after;
31326 Lisp_Object before_string, after_string;
31327 /* To correctly find the limits of mouse highlight
31328 in a bidi-reordered buffer, we must not use the
31329 optimization of limiting the search in
31330 previous-single-property-change and
31331 next-single-property-change, because
31332 rows_from_pos_range needs the real start and end
31333 positions to DTRT in this case. That's because
31334 the first row visible in a window does not
31335 necessarily display the character whose position
31336 is the smallest. */
31337 Lisp_Object lim1
31338 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31339 ? Fmarker_position (w->start)
31340 : Qnil;
31341 Lisp_Object lim2
31342 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31343 ? make_number (BUF_Z (XBUFFER (buffer))
31344 - w->window_end_pos)
31345 : Qnil;
31347 if (NILP (overlay))
31349 /* Handle the text property case. */
31350 before = Fprevious_single_property_change
31351 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31352 after = Fnext_single_property_change
31353 (make_number (pos), Qmouse_face, buffer, lim2);
31354 before_string = after_string = Qnil;
31356 else
31358 /* Handle the overlay case. */
31359 before = Foverlay_start (overlay);
31360 after = Foverlay_end (overlay);
31361 before_string = Foverlay_get (overlay, Qbefore_string);
31362 after_string = Foverlay_get (overlay, Qafter_string);
31364 if (!STRINGP (before_string)) before_string = Qnil;
31365 if (!STRINGP (after_string)) after_string = Qnil;
31368 mouse_face_from_buffer_pos (window, hlinfo, pos,
31369 NILP (before)
31371 : XFASTINT (before),
31372 NILP (after)
31373 ? BUF_Z (XBUFFER (buffer))
31374 : XFASTINT (after),
31375 before_string, after_string,
31376 disp_string);
31377 cursor = No_Cursor;
31382 check_help_echo:
31384 /* Look for a `help-echo' property. */
31385 if (NILP (help_echo_string)) {
31386 Lisp_Object help, overlay;
31388 /* Check overlays first. */
31389 help = overlay = Qnil;
31390 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31392 overlay = overlay_vec[i];
31393 help = Foverlay_get (overlay, Qhelp_echo);
31396 if (!NILP (help))
31398 help_echo_string = help;
31399 help_echo_window = window;
31400 help_echo_object = overlay;
31401 help_echo_pos = pos;
31403 else
31405 Lisp_Object obj = glyph->object;
31406 ptrdiff_t charpos = glyph->charpos;
31408 /* Try text properties. */
31409 if (STRINGP (obj)
31410 && charpos >= 0
31411 && charpos < SCHARS (obj))
31413 help = Fget_text_property (make_number (charpos),
31414 Qhelp_echo, obj);
31415 if (NILP (help))
31417 /* If the string itself doesn't specify a help-echo,
31418 see if the buffer text ``under'' it does. */
31419 struct glyph_row *r
31420 = MATRIX_ROW (w->current_matrix, vpos);
31421 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31422 ptrdiff_t p = string_buffer_position (obj, start);
31423 if (p > 0)
31425 help = Fget_char_property (make_number (p),
31426 Qhelp_echo, w->contents);
31427 if (!NILP (help))
31429 charpos = p;
31430 obj = w->contents;
31435 else if (BUFFERP (obj)
31436 && charpos >= BEGV
31437 && charpos < ZV)
31438 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31439 obj);
31441 if (!NILP (help))
31443 help_echo_string = help;
31444 help_echo_window = window;
31445 help_echo_object = obj;
31446 help_echo_pos = charpos;
31451 #ifdef HAVE_WINDOW_SYSTEM
31452 /* Look for a `pointer' property. */
31453 if (FRAME_WINDOW_P (f) && NILP (pointer))
31455 /* Check overlays first. */
31456 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31457 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31459 if (NILP (pointer))
31461 Lisp_Object obj = glyph->object;
31462 ptrdiff_t charpos = glyph->charpos;
31464 /* Try text properties. */
31465 if (STRINGP (obj)
31466 && charpos >= 0
31467 && charpos < SCHARS (obj))
31469 pointer = Fget_text_property (make_number (charpos),
31470 Qpointer, obj);
31471 if (NILP (pointer))
31473 /* If the string itself doesn't specify a pointer,
31474 see if the buffer text ``under'' it does. */
31475 struct glyph_row *r
31476 = MATRIX_ROW (w->current_matrix, vpos);
31477 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31478 ptrdiff_t p = string_buffer_position (obj, start);
31479 if (p > 0)
31480 pointer = Fget_char_property (make_number (p),
31481 Qpointer, w->contents);
31484 else if (BUFFERP (obj)
31485 && charpos >= BEGV
31486 && charpos < ZV)
31487 pointer = Fget_text_property (make_number (charpos),
31488 Qpointer, obj);
31491 #endif /* HAVE_WINDOW_SYSTEM */
31493 BEGV = obegv;
31494 ZV = ozv;
31495 current_buffer = obuf;
31496 SAFE_FREE ();
31499 set_cursor:
31500 define_frame_cursor1 (f, cursor, pointer);
31504 /* EXPORT for RIF:
31505 Clear any mouse-face on window W. This function is part of the
31506 redisplay interface, and is called from try_window_id and similar
31507 functions to ensure the mouse-highlight is off. */
31509 void
31510 x_clear_window_mouse_face (struct window *w)
31512 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31513 Lisp_Object window;
31515 block_input ();
31516 XSETWINDOW (window, w);
31517 if (EQ (window, hlinfo->mouse_face_window))
31518 clear_mouse_face (hlinfo);
31519 unblock_input ();
31523 /* EXPORT:
31524 Just discard the mouse face information for frame F, if any.
31525 This is used when the size of F is changed. */
31527 void
31528 cancel_mouse_face (struct frame *f)
31530 Lisp_Object window;
31531 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31533 window = hlinfo->mouse_face_window;
31534 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31535 reset_mouse_highlight (hlinfo);
31540 /***********************************************************************
31541 Exposure Events
31542 ***********************************************************************/
31544 #ifdef HAVE_WINDOW_SYSTEM
31546 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31547 which intersects rectangle R. R is in window-relative coordinates. */
31549 static void
31550 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31551 enum glyph_row_area area)
31553 struct glyph *first = row->glyphs[area];
31554 struct glyph *end = row->glyphs[area] + row->used[area];
31555 struct glyph *last;
31556 int first_x, start_x, x;
31558 if (area == TEXT_AREA && row->fill_line_p)
31559 /* If row extends face to end of line write the whole line. */
31560 draw_glyphs (w, 0, row, area,
31561 0, row->used[area],
31562 DRAW_NORMAL_TEXT, 0);
31563 else
31565 /* Set START_X to the window-relative start position for drawing glyphs of
31566 AREA. The first glyph of the text area can be partially visible.
31567 The first glyphs of other areas cannot. */
31568 start_x = window_box_left_offset (w, area);
31569 x = start_x;
31570 if (area == TEXT_AREA)
31571 x += row->x;
31573 /* Find the first glyph that must be redrawn. */
31574 while (first < end
31575 && x + first->pixel_width < r->x)
31577 x += first->pixel_width;
31578 ++first;
31581 /* Find the last one. */
31582 last = first;
31583 first_x = x;
31584 /* Use a signed int intermediate value to avoid catastrophic
31585 failures due to comparison between signed and unsigned, when
31586 x is negative (can happen for wide images that are hscrolled). */
31587 int r_end = r->x + r->width;
31588 while (last < end && x < r_end)
31590 x += last->pixel_width;
31591 ++last;
31594 /* Repaint. */
31595 if (last > first)
31596 draw_glyphs (w, first_x - start_x, row, area,
31597 first - row->glyphs[area], last - row->glyphs[area],
31598 DRAW_NORMAL_TEXT, 0);
31603 /* Redraw the parts of the glyph row ROW on window W intersecting
31604 rectangle R. R is in window-relative coordinates. Value is
31605 true if mouse-face was overwritten. */
31607 static bool
31608 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31610 eassert (row->enabled_p);
31612 if (row->mode_line_p || w->pseudo_window_p)
31613 draw_glyphs (w, 0, row, TEXT_AREA,
31614 0, row->used[TEXT_AREA],
31615 DRAW_NORMAL_TEXT, 0);
31616 else
31618 if (row->used[LEFT_MARGIN_AREA])
31619 expose_area (w, row, r, LEFT_MARGIN_AREA);
31620 if (row->used[TEXT_AREA])
31621 expose_area (w, row, r, TEXT_AREA);
31622 if (row->used[RIGHT_MARGIN_AREA])
31623 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31624 draw_row_fringe_bitmaps (w, row);
31627 return row->mouse_face_p;
31631 /* Redraw those parts of glyphs rows during expose event handling that
31632 overlap other rows. Redrawing of an exposed line writes over parts
31633 of lines overlapping that exposed line; this function fixes that.
31635 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31636 row in W's current matrix that is exposed and overlaps other rows.
31637 LAST_OVERLAPPING_ROW is the last such row. */
31639 static void
31640 expose_overlaps (struct window *w,
31641 struct glyph_row *first_overlapping_row,
31642 struct glyph_row *last_overlapping_row,
31643 XRectangle *r)
31645 struct glyph_row *row;
31647 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31648 if (row->overlapping_p)
31650 eassert (row->enabled_p && !row->mode_line_p);
31652 row->clip = r;
31653 if (row->used[LEFT_MARGIN_AREA])
31654 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31656 if (row->used[TEXT_AREA])
31657 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31659 if (row->used[RIGHT_MARGIN_AREA])
31660 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31661 row->clip = NULL;
31666 /* Return true if W's cursor intersects rectangle R. */
31668 static bool
31669 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31671 XRectangle cr, result;
31672 struct glyph *cursor_glyph;
31673 struct glyph_row *row;
31675 if (w->phys_cursor.vpos >= 0
31676 && w->phys_cursor.vpos < w->current_matrix->nrows
31677 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31678 row->enabled_p)
31679 && row->cursor_in_fringe_p)
31681 /* Cursor is in the fringe. */
31682 cr.x = window_box_right_offset (w,
31683 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31684 ? RIGHT_MARGIN_AREA
31685 : TEXT_AREA));
31686 cr.y = row->y;
31687 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31688 cr.height = row->height;
31689 return x_intersect_rectangles (&cr, r, &result);
31692 cursor_glyph = get_phys_cursor_glyph (w);
31693 if (cursor_glyph)
31695 /* r is relative to W's box, but w->phys_cursor.x is relative
31696 to left edge of W's TEXT area. Adjust it. */
31697 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31698 cr.y = w->phys_cursor.y;
31699 cr.width = cursor_glyph->pixel_width;
31700 cr.height = w->phys_cursor_height;
31701 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31702 I assume the effect is the same -- and this is portable. */
31703 return x_intersect_rectangles (&cr, r, &result);
31705 /* If we don't understand the format, pretend we're not in the hot-spot. */
31706 return false;
31710 /* EXPORT:
31711 Draw a vertical window border to the right of window W if W doesn't
31712 have vertical scroll bars. */
31714 void
31715 x_draw_vertical_border (struct window *w)
31717 struct frame *f = XFRAME (WINDOW_FRAME (w));
31719 /* We could do better, if we knew what type of scroll-bar the adjacent
31720 windows (on either side) have... But we don't :-(
31721 However, I think this works ok. ++KFS 2003-04-25 */
31723 /* Redraw borders between horizontally adjacent windows. Don't
31724 do it for frames with vertical scroll bars because either the
31725 right scroll bar of a window, or the left scroll bar of its
31726 neighbor will suffice as a border. */
31727 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31728 return;
31730 /* Note: It is necessary to redraw both the left and the right
31731 borders, for when only this single window W is being
31732 redisplayed. */
31733 if (!WINDOW_RIGHTMOST_P (w)
31734 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31736 int x0, x1, y0, y1;
31738 window_box_edges (w, &x0, &y0, &x1, &y1);
31739 y1 -= 1;
31741 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31742 x1 -= 1;
31744 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31747 if (!WINDOW_LEFTMOST_P (w)
31748 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31750 int x0, x1, y0, y1;
31752 window_box_edges (w, &x0, &y0, &x1, &y1);
31753 y1 -= 1;
31755 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31756 x0 -= 1;
31758 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31763 /* Draw window dividers for window W. */
31765 void
31766 x_draw_right_divider (struct window *w)
31768 struct frame *f = WINDOW_XFRAME (w);
31770 if (w->mini || w->pseudo_window_p)
31771 return;
31772 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31774 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31775 int x1 = WINDOW_RIGHT_EDGE_X (w);
31776 int y0 = WINDOW_TOP_EDGE_Y (w);
31777 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31779 /* If W is horizontally combined and has a right sibling, don't
31780 draw over any bottom divider. */
31781 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31782 && !NILP (w->parent)
31783 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31784 && !NILP (w->next))
31785 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31787 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31791 static void
31792 x_draw_bottom_divider (struct window *w)
31794 struct frame *f = XFRAME (WINDOW_FRAME (w));
31796 if (w->mini || w->pseudo_window_p)
31797 return;
31798 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31800 int x0 = WINDOW_LEFT_EDGE_X (w);
31801 int x1 = WINDOW_RIGHT_EDGE_X (w);
31802 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31803 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31804 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31806 /* If W is vertically combined and has a sibling below, don't draw
31807 over any right divider. */
31808 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31809 && p
31810 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31811 && !NILP (w->next))
31812 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31813 && NILP (w->next)
31814 && !NILP (p->parent)
31815 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31816 && !NILP (XWINDOW (p->parent)->next))))
31817 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31819 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31823 /* Redraw the part of window W intersection rectangle FR. Pixel
31824 coordinates in FR are frame-relative. Call this function with
31825 input blocked. Value is true if the exposure overwrites
31826 mouse-face. */
31828 static bool
31829 expose_window (struct window *w, XRectangle *fr)
31831 struct frame *f = XFRAME (w->frame);
31832 XRectangle wr, r;
31833 bool mouse_face_overwritten_p = false;
31835 /* If window is not yet fully initialized, do nothing. This can
31836 happen when toolkit scroll bars are used and a window is split.
31837 Reconfiguring the scroll bar will generate an expose for a newly
31838 created window. */
31839 if (w->current_matrix == NULL)
31840 return false;
31842 /* When we're currently updating the window, display and current
31843 matrix usually don't agree. Arrange for a thorough display
31844 later. */
31845 if (w->must_be_updated_p)
31847 SET_FRAME_GARBAGED (f);
31848 return false;
31851 /* Frame-relative pixel rectangle of W. */
31852 wr.x = WINDOW_LEFT_EDGE_X (w);
31853 wr.y = WINDOW_TOP_EDGE_Y (w);
31854 wr.width = WINDOW_PIXEL_WIDTH (w);
31855 wr.height = WINDOW_PIXEL_HEIGHT (w);
31857 if (x_intersect_rectangles (fr, &wr, &r))
31859 int yb = window_text_bottom_y (w);
31860 struct glyph_row *row;
31861 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31863 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31864 r.x, r.y, r.width, r.height));
31866 /* Convert to window coordinates. */
31867 r.x -= WINDOW_LEFT_EDGE_X (w);
31868 r.y -= WINDOW_TOP_EDGE_Y (w);
31870 /* Turn off the cursor. */
31871 bool cursor_cleared_p = (!w->pseudo_window_p
31872 && phys_cursor_in_rect_p (w, &r));
31873 if (cursor_cleared_p)
31874 x_clear_cursor (w);
31876 /* If the row containing the cursor extends face to end of line,
31877 then expose_area might overwrite the cursor outside the
31878 rectangle and thus notice_overwritten_cursor might clear
31879 w->phys_cursor_on_p. We remember the original value and
31880 check later if it is changed. */
31881 bool phys_cursor_on_p = w->phys_cursor_on_p;
31883 /* Use a signed int intermediate value to avoid catastrophic
31884 failures due to comparison between signed and unsigned, when
31885 y0 or y1 is negative (can happen for tall images). */
31886 int r_bottom = r.y + r.height;
31888 /* Update lines intersecting rectangle R. */
31889 first_overlapping_row = last_overlapping_row = NULL;
31890 for (row = w->current_matrix->rows;
31891 row->enabled_p;
31892 ++row)
31894 int y0 = row->y;
31895 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31897 if ((y0 >= r.y && y0 < r_bottom)
31898 || (y1 > r.y && y1 < r_bottom)
31899 || (r.y >= y0 && r.y < y1)
31900 || (r_bottom > y0 && r_bottom < y1))
31902 /* A header line may be overlapping, but there is no need
31903 to fix overlapping areas for them. KFS 2005-02-12 */
31904 if (row->overlapping_p && !row->mode_line_p)
31906 if (first_overlapping_row == NULL)
31907 first_overlapping_row = row;
31908 last_overlapping_row = row;
31911 row->clip = fr;
31912 if (expose_line (w, row, &r))
31913 mouse_face_overwritten_p = true;
31914 row->clip = NULL;
31916 else if (row->overlapping_p)
31918 /* We must redraw a row overlapping the exposed area. */
31919 if (y0 < r.y
31920 ? y0 + row->phys_height > r.y
31921 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31923 if (first_overlapping_row == NULL)
31924 first_overlapping_row = row;
31925 last_overlapping_row = row;
31929 if (y1 >= yb)
31930 break;
31933 /* Display the mode line if there is one. */
31934 if (window_wants_mode_line (w)
31935 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31936 row->enabled_p)
31937 && row->y < r_bottom)
31939 if (expose_line (w, row, &r))
31940 mouse_face_overwritten_p = true;
31943 if (!w->pseudo_window_p)
31945 /* Fix the display of overlapping rows. */
31946 if (first_overlapping_row)
31947 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31948 fr);
31950 /* Draw border between windows. */
31951 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31952 x_draw_right_divider (w);
31953 else
31954 x_draw_vertical_border (w);
31956 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31957 x_draw_bottom_divider (w);
31959 /* Turn the cursor on again. */
31960 if (cursor_cleared_p
31961 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31962 update_window_cursor (w, true);
31966 return mouse_face_overwritten_p;
31971 /* Redraw (parts) of all windows in the window tree rooted at W that
31972 intersect R. R contains frame pixel coordinates. Value is
31973 true if the exposure overwrites mouse-face. */
31975 static bool
31976 expose_window_tree (struct window *w, XRectangle *r)
31978 struct frame *f = XFRAME (w->frame);
31979 bool mouse_face_overwritten_p = false;
31981 while (w && !FRAME_GARBAGED_P (f))
31983 mouse_face_overwritten_p
31984 |= (WINDOWP (w->contents)
31985 ? expose_window_tree (XWINDOW (w->contents), r)
31986 : expose_window (w, r));
31988 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31991 return mouse_face_overwritten_p;
31995 /* EXPORT:
31996 Redisplay an exposed area of frame F. X and Y are the upper-left
31997 corner of the exposed rectangle. W and H are width and height of
31998 the exposed area. All are pixel values. W or H zero means redraw
31999 the entire frame. */
32001 void
32002 expose_frame (struct frame *f, int x, int y, int w, int h)
32004 XRectangle r;
32005 bool mouse_face_overwritten_p = false;
32007 TRACE ((stderr, "expose_frame "));
32009 /* No need to redraw if frame will be redrawn soon. */
32010 if (FRAME_GARBAGED_P (f))
32012 TRACE ((stderr, " garbaged\n"));
32013 return;
32016 /* If basic faces haven't been realized yet, there is no point in
32017 trying to redraw anything. This can happen when we get an expose
32018 event while Emacs is starting, e.g. by moving another window. */
32019 if (FRAME_FACE_CACHE (f) == NULL
32020 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32022 TRACE ((stderr, " no faces\n"));
32023 return;
32026 if (w == 0 || h == 0)
32028 r.x = r.y = 0;
32029 r.width = FRAME_TEXT_WIDTH (f);
32030 r.height = FRAME_TEXT_HEIGHT (f);
32032 else
32034 r.x = x;
32035 r.y = y;
32036 r.width = w;
32037 r.height = h;
32040 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32041 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32043 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32044 if (WINDOWP (f->tool_bar_window))
32045 mouse_face_overwritten_p
32046 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32047 #endif
32049 #ifdef HAVE_X_WINDOWS
32050 #ifndef MSDOS
32051 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32052 if (WINDOWP (f->menu_bar_window))
32053 mouse_face_overwritten_p
32054 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32055 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32056 #endif
32057 #endif
32059 /* Some window managers support a focus-follows-mouse style with
32060 delayed raising of frames. Imagine a partially obscured frame,
32061 and moving the mouse into partially obscured mouse-face on that
32062 frame. The visible part of the mouse-face will be highlighted,
32063 then the WM raises the obscured frame. With at least one WM, KDE
32064 2.1, Emacs is not getting any event for the raising of the frame
32065 (even tried with SubstructureRedirectMask), only Expose events.
32066 These expose events will draw text normally, i.e. not
32067 highlighted. Which means we must redo the highlight here.
32068 Subsume it under ``we love X''. --gerd 2001-08-15 */
32069 /* Included in Windows version because Windows most likely does not
32070 do the right thing if any third party tool offers
32071 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32072 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32074 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32075 if (f == hlinfo->mouse_face_mouse_frame)
32077 int mouse_x = hlinfo->mouse_face_mouse_x;
32078 int mouse_y = hlinfo->mouse_face_mouse_y;
32079 clear_mouse_face (hlinfo);
32080 note_mouse_highlight (f, mouse_x, mouse_y);
32086 /* EXPORT:
32087 Determine the intersection of two rectangles R1 and R2. Return
32088 the intersection in *RESULT. Value is true if RESULT is not
32089 empty. */
32091 bool
32092 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32094 XRectangle *left, *right;
32095 XRectangle *upper, *lower;
32096 bool intersection_p = false;
32098 /* Rearrange so that R1 is the left-most rectangle. */
32099 if (r1->x < r2->x)
32100 left = r1, right = r2;
32101 else
32102 left = r2, right = r1;
32104 /* X0 of the intersection is right.x0, if this is inside R1,
32105 otherwise there is no intersection. */
32106 if (right->x <= left->x + left->width)
32108 result->x = right->x;
32110 /* The right end of the intersection is the minimum of
32111 the right ends of left and right. */
32112 result->width = (min (left->x + left->width, right->x + right->width)
32113 - result->x);
32115 /* Same game for Y. */
32116 if (r1->y < r2->y)
32117 upper = r1, lower = r2;
32118 else
32119 upper = r2, lower = r1;
32121 /* The upper end of the intersection is lower.y0, if this is inside
32122 of upper. Otherwise, there is no intersection. */
32123 if (lower->y <= upper->y + upper->height)
32125 result->y = lower->y;
32127 /* The lower end of the intersection is the minimum of the lower
32128 ends of upper and lower. */
32129 result->height = (min (lower->y + lower->height,
32130 upper->y + upper->height)
32131 - result->y);
32132 intersection_p = true;
32136 return intersection_p;
32139 #endif /* HAVE_WINDOW_SYSTEM */
32142 /***********************************************************************
32143 Initialization
32144 ***********************************************************************/
32146 void
32147 syms_of_xdisp (void)
32149 Vwith_echo_area_save_vector = Qnil;
32150 staticpro (&Vwith_echo_area_save_vector);
32152 Vmessage_stack = Qnil;
32153 staticpro (&Vmessage_stack);
32155 /* Non-nil means don't actually do any redisplay. */
32156 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32158 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32160 DEFVAR_BOOL("inhibit-message", inhibit_message,
32161 doc: /* Non-nil means calls to `message' are not displayed.
32162 They are still logged to the *Messages* buffer. */);
32163 inhibit_message = 0;
32165 message_dolog_marker1 = Fmake_marker ();
32166 staticpro (&message_dolog_marker1);
32167 message_dolog_marker2 = Fmake_marker ();
32168 staticpro (&message_dolog_marker2);
32169 message_dolog_marker3 = Fmake_marker ();
32170 staticpro (&message_dolog_marker3);
32172 defsubr (&Sset_buffer_redisplay);
32173 #ifdef GLYPH_DEBUG
32174 defsubr (&Sdump_frame_glyph_matrix);
32175 defsubr (&Sdump_glyph_matrix);
32176 defsubr (&Sdump_glyph_row);
32177 defsubr (&Sdump_tool_bar_row);
32178 defsubr (&Strace_redisplay);
32179 defsubr (&Strace_to_stderr);
32180 #endif
32181 #ifdef HAVE_WINDOW_SYSTEM
32182 defsubr (&Stool_bar_height);
32183 defsubr (&Slookup_image_map);
32184 #endif
32185 defsubr (&Sline_pixel_height);
32186 defsubr (&Sformat_mode_line);
32187 defsubr (&Sinvisible_p);
32188 defsubr (&Scurrent_bidi_paragraph_direction);
32189 defsubr (&Swindow_text_pixel_size);
32190 defsubr (&Smove_point_visually);
32191 defsubr (&Sbidi_find_overridden_directionality);
32193 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32194 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32195 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32196 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32197 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32198 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32199 DEFSYM (Qeval, "eval");
32200 DEFSYM (QCdata, ":data");
32202 /* Names of text properties relevant for redisplay. */
32203 DEFSYM (Qdisplay, "display");
32204 DEFSYM (Qspace_width, "space-width");
32205 DEFSYM (Qraise, "raise");
32206 DEFSYM (Qslice, "slice");
32207 DEFSYM (Qspace, "space");
32208 DEFSYM (Qmargin, "margin");
32209 DEFSYM (Qpointer, "pointer");
32210 DEFSYM (Qleft_margin, "left-margin");
32211 DEFSYM (Qright_margin, "right-margin");
32212 DEFSYM (Qcenter, "center");
32213 DEFSYM (Qline_height, "line-height");
32214 DEFSYM (QCalign_to, ":align-to");
32215 DEFSYM (QCrelative_width, ":relative-width");
32216 DEFSYM (QCrelative_height, ":relative-height");
32217 DEFSYM (QCeval, ":eval");
32218 DEFSYM (QCpropertize, ":propertize");
32219 DEFSYM (QCfile, ":file");
32220 DEFSYM (Qfontified, "fontified");
32221 DEFSYM (Qfontification_functions, "fontification-functions");
32223 /* Name of the symbol which disables Lisp evaluation in 'display'
32224 properties. This is used by enriched.el. */
32225 DEFSYM (Qdisable_eval, "disable-eval");
32227 /* Name of the face used to highlight trailing whitespace. */
32228 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32230 /* Names of the faces used to display line numbers. */
32231 DEFSYM (Qline_number, "line-number");
32232 DEFSYM (Qline_number_current_line, "line-number-current-line");
32233 /* Name of a text property which disables line-number display. */
32234 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32236 /* Name and number of the face used to highlight escape glyphs. */
32237 DEFSYM (Qescape_glyph, "escape-glyph");
32239 /* Name and number of the face used to highlight non-breaking
32240 spaces/hyphens. */
32241 DEFSYM (Qnobreak_space, "nobreak-space");
32242 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32244 /* The symbol 'image' which is the car of the lists used to represent
32245 images in Lisp. Also a tool bar style. */
32246 DEFSYM (Qimage, "image");
32248 /* Tool bar styles. */
32249 DEFSYM (Qtext, "text");
32250 DEFSYM (Qboth, "both");
32251 DEFSYM (Qboth_horiz, "both-horiz");
32252 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32254 /* The image map types. */
32255 DEFSYM (QCmap, ":map");
32256 DEFSYM (QCpointer, ":pointer");
32257 DEFSYM (Qrect, "rect");
32258 DEFSYM (Qcircle, "circle");
32259 DEFSYM (Qpoly, "poly");
32261 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32263 DEFSYM (Qgrow_only, "grow-only");
32264 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32265 DEFSYM (Qposition, "position");
32266 DEFSYM (Qbuffer_position, "buffer-position");
32267 DEFSYM (Qobject, "object");
32269 /* Cursor shapes. */
32270 DEFSYM (Qbar, "bar");
32271 DEFSYM (Qhbar, "hbar");
32272 DEFSYM (Qbox, "box");
32273 DEFSYM (Qhollow, "hollow");
32275 /* Pointer shapes. */
32276 DEFSYM (Qhand, "hand");
32277 DEFSYM (Qarrow, "arrow");
32278 /* also Qtext */
32280 DEFSYM (Qdragging, "dragging");
32282 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32284 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32285 staticpro (&list_of_error);
32287 /* Values of those variables at last redisplay are stored as
32288 properties on 'overlay-arrow-position' symbol. However, if
32289 Voverlay_arrow_position is a marker, last-arrow-position is its
32290 numerical position. */
32291 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32292 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32294 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32295 properties on a symbol in overlay-arrow-variable-list. */
32296 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32297 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32299 echo_buffer[0] = echo_buffer[1] = Qnil;
32300 staticpro (&echo_buffer[0]);
32301 staticpro (&echo_buffer[1]);
32303 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32304 staticpro (&echo_area_buffer[0]);
32305 staticpro (&echo_area_buffer[1]);
32307 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32308 staticpro (&Vmessages_buffer_name);
32310 mode_line_proptrans_alist = Qnil;
32311 staticpro (&mode_line_proptrans_alist);
32312 mode_line_string_list = Qnil;
32313 staticpro (&mode_line_string_list);
32314 mode_line_string_face = Qnil;
32315 staticpro (&mode_line_string_face);
32316 mode_line_string_face_prop = Qnil;
32317 staticpro (&mode_line_string_face_prop);
32318 Vmode_line_unwind_vector = Qnil;
32319 staticpro (&Vmode_line_unwind_vector);
32321 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32323 help_echo_string = Qnil;
32324 staticpro (&help_echo_string);
32325 help_echo_object = Qnil;
32326 staticpro (&help_echo_object);
32327 help_echo_window = Qnil;
32328 staticpro (&help_echo_window);
32329 previous_help_echo_string = Qnil;
32330 staticpro (&previous_help_echo_string);
32331 help_echo_pos = -1;
32333 DEFSYM (Qright_to_left, "right-to-left");
32334 DEFSYM (Qleft_to_right, "left-to-right");
32335 defsubr (&Sbidi_resolved_levels);
32337 #ifdef HAVE_WINDOW_SYSTEM
32338 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32339 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32340 For example, if a block cursor is over a tab, it will be drawn as
32341 wide as that tab on the display. */);
32342 x_stretch_cursor_p = 0;
32343 #endif
32345 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32346 doc: /* Non-nil means highlight trailing whitespace.
32347 The face used for trailing whitespace is `trailing-whitespace'. */);
32348 Vshow_trailing_whitespace = Qnil;
32350 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32351 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32352 If the value is t, Emacs highlights non-ASCII chars which have the
32353 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32354 or `nobreak-hyphen' face respectively.
32356 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32357 U+2011 (non-breaking hyphen) are affected.
32359 Any other non-nil value means to display these characters as a escape
32360 glyph followed by an ordinary space or hyphen.
32362 A value of nil means no special handling of these characters. */);
32363 Vnobreak_char_display = Qt;
32365 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32366 doc: /* The pointer shape to show in void text areas.
32367 A value of nil means to show the text pointer. Other options are
32368 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32369 `hourglass'. */);
32370 Vvoid_text_area_pointer = Qarrow;
32372 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32373 doc: /* Non-nil means don't actually do any redisplay.
32374 This is used for internal purposes. */);
32375 Vinhibit_redisplay = Qnil;
32377 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32378 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32379 Vglobal_mode_string = Qnil;
32381 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32382 doc: /* Marker for where to display an arrow on top of the buffer text.
32383 This must be the beginning of a line in order to work.
32384 See also `overlay-arrow-string'. */);
32385 Voverlay_arrow_position = Qnil;
32387 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32388 doc: /* String to display as an arrow in non-window frames.
32389 See also `overlay-arrow-position'. */);
32390 Voverlay_arrow_string = build_pure_c_string ("=>");
32392 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32393 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32394 The symbols on this list are examined during redisplay to determine
32395 where to display overlay arrows. */);
32396 Voverlay_arrow_variable_list
32397 = list1 (intern_c_string ("overlay-arrow-position"));
32399 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32400 doc: /* The number of lines to try scrolling a window by when point moves out.
32401 If that fails to bring point back on frame, point is centered instead.
32402 If this is zero, point is always centered after it moves off frame.
32403 If you want scrolling to always be a line at a time, you should set
32404 `scroll-conservatively' to a large value rather than set this to 1. */);
32406 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32407 doc: /* Scroll up to this many lines, to bring point back on screen.
32408 If point moves off-screen, redisplay will scroll by up to
32409 `scroll-conservatively' lines in order to bring point just barely
32410 onto the screen again. If that cannot be done, then redisplay
32411 recenters point as usual.
32413 If the value is greater than 100, redisplay will never recenter point,
32414 but will always scroll just enough text to bring point into view, even
32415 if you move far away.
32417 A value of zero means always recenter point if it moves off screen. */);
32418 scroll_conservatively = 0;
32420 DEFVAR_INT ("scroll-margin", scroll_margin,
32421 doc: /* Number of lines of margin at the top and bottom of a window.
32422 Recenter the window whenever point gets within this many lines
32423 of the top or bottom of the window. */);
32424 scroll_margin = 0;
32426 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32427 doc: /* Maximum effective value of `scroll-margin'.
32428 Given as a fraction of the current window's lines. The value should
32429 be a floating point number between 0.0 and 0.5. The effective maximum
32430 is limited to (/ (1- window-lines) 2). Non-float values for this
32431 variable are ignored and the default 0.25 is used instead. */);
32432 Vmaximum_scroll_margin = make_float (0.25);
32434 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32435 doc: /* Pixels per inch value for non-window system displays.
32436 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32437 Vdisplay_pixels_per_inch = make_float (72.0);
32439 #ifdef GLYPH_DEBUG
32440 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32441 #endif
32443 DEFVAR_LISP ("truncate-partial-width-windows",
32444 Vtruncate_partial_width_windows,
32445 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32446 For an integer value, truncate lines in each window narrower than the
32447 full frame width, provided the total window width in column units is less
32448 than that integer; otherwise, respect the value of `truncate-lines'.
32449 The total width of the window is as returned by `window-total-width', it
32450 includes the fringes, the continuation and truncation glyphs, the
32451 display margins (if any), and the scroll bar
32453 For any other non-nil value, truncate lines in all windows that do
32454 not span the full frame width.
32456 A value of nil means to respect the value of `truncate-lines'.
32458 If `word-wrap' is enabled, you might want to reduce this. */);
32459 Vtruncate_partial_width_windows = make_number (50);
32461 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32462 doc: /* Maximum buffer size for which line number should be displayed.
32463 If the buffer is bigger than this, the line number does not appear
32464 in the mode line. A value of nil means no limit. */);
32465 Vline_number_display_limit = Qnil;
32467 DEFVAR_INT ("line-number-display-limit-width",
32468 line_number_display_limit_width,
32469 doc: /* Maximum line width (in characters) for line number display.
32470 If the average length of the lines near point is bigger than this, then the
32471 line number may be omitted from the mode line. */);
32472 line_number_display_limit_width = 200;
32474 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32475 doc: /* Non-nil means highlight region even in nonselected windows. */);
32476 highlight_nonselected_windows = false;
32478 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32479 doc: /* Non-nil if more than one frame is visible on this display.
32480 Minibuffer-only frames don't count, but iconified frames do.
32481 This variable is not guaranteed to be accurate except while processing
32482 `frame-title-format' and `icon-title-format'. */);
32484 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32485 doc: /* Template for displaying the title bar of visible frames.
32486 \(Assuming the window manager supports this feature.)
32488 This variable has the same structure as `mode-line-format', except that
32489 the %c, %C, and %l constructs are ignored. It is used only on frames for
32490 which no explicit name has been set (see `modify-frame-parameters'). */);
32492 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32493 doc: /* Template for displaying the title bar of an iconified frame.
32494 \(Assuming the window manager supports this feature.)
32495 This variable has the same structure as `mode-line-format' (which see),
32496 and is used only on frames for which no explicit name has been set
32497 \(see `modify-frame-parameters'). */);
32498 Vicon_title_format
32499 = Vframe_title_format
32500 = listn (CONSTYPE_PURE, 3,
32501 intern_c_string ("multiple-frames"),
32502 build_pure_c_string ("%b"),
32503 listn (CONSTYPE_PURE, 4,
32504 empty_unibyte_string,
32505 intern_c_string ("invocation-name"),
32506 build_pure_c_string ("@"),
32507 intern_c_string ("system-name")));
32509 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32510 doc: /* Maximum number of lines to keep in the message log buffer.
32511 If nil, disable message logging. If t, log messages but don't truncate
32512 the buffer when it becomes large. */);
32513 Vmessage_log_max = make_number (1000);
32515 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32516 doc: /* List of functions to call before redisplaying a window with scrolling.
32517 Each function is called with two arguments, the window and its new
32518 display-start position.
32519 These functions are called whenever the `window-start' marker is modified,
32520 either to point into another buffer (e.g. via `set-window-buffer') or another
32521 place in the same buffer.
32522 Note that the value of `window-end' is not valid when these functions are
32523 called.
32525 Warning: Do not use this feature to alter the way the window
32526 is scrolled. It is not designed for that, and such use probably won't
32527 work. */);
32528 Vwindow_scroll_functions = Qnil;
32530 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32531 doc: /* Functions called when redisplay of a window reaches the end trigger.
32532 Each function is called with two arguments, the window and the end trigger value.
32533 See `set-window-redisplay-end-trigger'. */);
32534 Vredisplay_end_trigger_functions = Qnil;
32536 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32537 doc: /* Non-nil means autoselect window with mouse pointer.
32538 If nil, do not autoselect windows.
32539 A positive number means delay autoselection by that many seconds: a
32540 window is autoselected only after the mouse has remained in that
32541 window for the duration of the delay.
32542 A negative number has a similar effect, but causes windows to be
32543 autoselected only after the mouse has stopped moving. (Because of
32544 the way Emacs compares mouse events, you will occasionally wait twice
32545 that time before the window gets selected.)
32546 Any other value means to autoselect window instantaneously when the
32547 mouse pointer enters it.
32549 Autoselection selects the minibuffer only if it is active, and never
32550 unselects the minibuffer if it is active.
32552 When customizing this variable make sure that the actual value of
32553 `focus-follows-mouse' matches the behavior of your window manager. */);
32554 Vmouse_autoselect_window = Qnil;
32556 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32557 doc: /* Non-nil means automatically resize tool-bars.
32558 This dynamically changes the tool-bar's height to the minimum height
32559 that is needed to make all tool-bar items visible.
32560 If value is `grow-only', the tool-bar's height is only increased
32561 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32562 Vauto_resize_tool_bars = Qt;
32564 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32565 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32566 auto_raise_tool_bar_buttons_p = true;
32568 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32569 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32570 make_cursor_line_fully_visible_p = true;
32572 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32573 doc: /* Border below tool-bar in pixels.
32574 If an integer, use it as the height of the border.
32575 If it is one of `internal-border-width' or `border-width', use the
32576 value of the corresponding frame parameter.
32577 Otherwise, no border is added below the tool-bar. */);
32578 Vtool_bar_border = Qinternal_border_width;
32580 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32581 doc: /* Margin around tool-bar buttons in pixels.
32582 If an integer, use that for both horizontal and vertical margins.
32583 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32584 HORZ specifying the horizontal margin, and VERT specifying the
32585 vertical margin. */);
32586 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32588 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32589 doc: /* Relief thickness of tool-bar buttons. */);
32590 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32592 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32593 doc: /* Tool bar style to use.
32594 It can be one of
32595 image - show images only
32596 text - show text only
32597 both - show both, text below image
32598 both-horiz - show text to the right of the image
32599 text-image-horiz - show text to the left of the image
32600 any other - use system default or image if no system default.
32602 This variable only affects the GTK+ toolkit version of Emacs. */);
32603 Vtool_bar_style = Qnil;
32605 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32606 doc: /* Maximum number of characters a label can have to be shown.
32607 The tool bar style must also show labels for this to have any effect, see
32608 `tool-bar-style'. */);
32609 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32611 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32612 doc: /* List of functions to call to fontify regions of text.
32613 Each function is called with one argument POS. Functions must
32614 fontify a region starting at POS in the current buffer, and give
32615 fontified regions the property `fontified'. */);
32616 Vfontification_functions = Qnil;
32617 Fmake_variable_buffer_local (Qfontification_functions);
32619 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32620 unibyte_display_via_language_environment,
32621 doc: /* Non-nil means display unibyte text according to language environment.
32622 Specifically, this means that raw bytes in the range 160-255 decimal
32623 are displayed by converting them to the equivalent multibyte characters
32624 according to the current language environment. As a result, they are
32625 displayed according to the current fontset.
32627 Note that this variable affects only how these bytes are displayed,
32628 but does not change the fact they are interpreted as raw bytes. */);
32629 unibyte_display_via_language_environment = false;
32631 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32632 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32633 If a float, it specifies a fraction of the mini-window frame's height.
32634 If an integer, it specifies a number of lines. */);
32635 Vmax_mini_window_height = make_float (0.25);
32637 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32638 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32639 A value of nil means don't automatically resize mini-windows.
32640 A value of t means resize them to fit the text displayed in them.
32641 A value of `grow-only', the default, means let mini-windows grow only;
32642 they return to their normal size when the minibuffer is closed, or the
32643 echo area becomes empty. */);
32644 /* Contrary to the doc string, we initialize this to nil, so that
32645 loading loadup.el won't try to resize windows before loading
32646 window.el, where some functions we need to call for this live.
32647 We assign the 'grow-only' value right after loading window.el
32648 during loadup. */
32649 Vresize_mini_windows = Qnil;
32651 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32652 doc: /* Alist specifying how to blink the cursor off.
32653 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32654 `cursor-type' frame-parameter or variable equals ON-STATE,
32655 comparing using `equal', Emacs uses OFF-STATE to specify
32656 how to blink it off. ON-STATE and OFF-STATE are values for
32657 the `cursor-type' frame parameter.
32659 If a frame's ON-STATE has no entry in this list,
32660 the frame's other specifications determine how to blink the cursor off. */);
32661 Vblink_cursor_alist = Qnil;
32663 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32664 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32665 The value `current-line' means the line displaying point in each window
32666 is automatically scrolled horizontally to make point visible.
32667 Any other non-nil value means all the lines in a window are automatically
32668 scrolled horizontally to make point visible. */);
32669 automatic_hscrolling = Qt;
32670 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32671 DEFSYM (Qcurrent_line, "current-line");
32673 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32674 doc: /* How many columns away from the window edge point is allowed to get
32675 before automatic hscrolling will horizontally scroll the window. */);
32676 hscroll_margin = 5;
32678 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32679 doc: /* How many columns to scroll the window when point gets too close to the edge.
32680 When point is less than `hscroll-margin' columns from the window
32681 edge, automatic hscrolling will scroll the window by the amount of columns
32682 determined by this variable. If its value is a positive integer, scroll that
32683 many columns. If it's a positive floating-point number, it specifies the
32684 fraction of the window's width to scroll. If it's nil or zero, point will be
32685 centered horizontally after the scroll. Any other value, including negative
32686 numbers, are treated as if the value were zero.
32688 Automatic hscrolling always moves point outside the scroll margin, so if
32689 point was more than scroll step columns inside the margin, the window will
32690 scroll more than the value given by the scroll step.
32692 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32693 and `scroll-right' overrides this variable's effect. */);
32694 Vhscroll_step = make_number (0);
32696 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32697 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32698 Bind this around calls to `message' to let it take effect. */);
32699 message_truncate_lines = false;
32701 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32702 doc: /* Normal hook run to update the menu bar definitions.
32703 Redisplay runs this hook before it redisplays the menu bar.
32704 This is used to update menus such as Buffers, whose contents depend on
32705 various data. */);
32706 Vmenu_bar_update_hook = Qnil;
32708 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32709 doc: /* Frame for which we are updating a menu.
32710 The enable predicate for a menu binding should check this variable. */);
32711 Vmenu_updating_frame = Qnil;
32713 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32714 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32715 inhibit_menubar_update = false;
32717 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32718 doc: /* Prefix prepended to all continuation lines at display time.
32719 The value may be a string, an image, or a stretch-glyph; it is
32720 interpreted in the same way as the value of a `display' text property.
32722 This variable is overridden by any `wrap-prefix' text or overlay
32723 property.
32725 To add a prefix to non-continuation lines, use `line-prefix'. */);
32726 Vwrap_prefix = Qnil;
32727 DEFSYM (Qwrap_prefix, "wrap-prefix");
32728 Fmake_variable_buffer_local (Qwrap_prefix);
32730 DEFVAR_LISP ("line-prefix", Vline_prefix,
32731 doc: /* Prefix prepended to all non-continuation lines at display time.
32732 The value may be a string, an image, or a stretch-glyph; it is
32733 interpreted in the same way as the value of a `display' text property.
32735 This variable is overridden by any `line-prefix' text or overlay
32736 property.
32738 To add a prefix to continuation lines, use `wrap-prefix'. */);
32739 Vline_prefix = Qnil;
32740 DEFSYM (Qline_prefix, "line-prefix");
32741 Fmake_variable_buffer_local (Qline_prefix);
32743 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32744 doc: /* Non-nil means display line numbers.
32745 If the value is t, display the absolute number of each line of a buffer
32746 shown in a window. Absolute line numbers count from the beginning of
32747 the current narrowing, or from buffer beginning. If the value is
32748 `relative', display for each line not containing the window's point its
32749 relative number instead, i.e. the number of the line relative to the
32750 line showing the window's point.
32752 In either case, line numbers are displayed at the beginning of each
32753 non-continuation line that displays buffer text, i.e. after each newline
32754 character that comes from the buffer. The value `visual' is like
32755 `relative' but counts screen lines instead of buffer lines. In practice
32756 this means that continuation lines count as well when calculating the
32757 relative number of a line.
32759 Lisp programs can disable display of a line number of a particular
32760 buffer line by putting the `display-line-numbers-disable' text property
32761 or overlay property on the first visible character of that line. */);
32762 Vdisplay_line_numbers = Qnil;
32763 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32764 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32765 DEFSYM (Qrelative, "relative");
32766 DEFSYM (Qvisual, "visual");
32768 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32769 doc: /* Minimum width of space reserved for line number display.
32770 A positive number means reserve that many columns for line numbers,
32771 even if the actual number needs less space.
32772 The default value of nil means compute the space dynamically.
32773 Any other value is treated as nil. */);
32774 Vdisplay_line_numbers_width = Qnil;
32775 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32776 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32778 DEFVAR_LISP ("display-line-numbers-current-absolute",
32779 Vdisplay_line_numbers_current_absolute,
32780 doc: /* Non-nil means display absolute number of current line.
32781 This variable has effect only when `display-line-numbers' is
32782 either `relative' or `visual'. */);
32783 Vdisplay_line_numbers_current_absolute = Qt;
32785 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32786 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32787 display_line_numbers_widen = false;
32788 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32789 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32791 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32792 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32793 inhibit_eval_during_redisplay = false;
32795 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32796 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32797 inhibit_free_realized_faces = false;
32799 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32800 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32801 Intended for use during debugging and for testing bidi display;
32802 see biditest.el in the test suite. */);
32803 inhibit_bidi_mirroring = false;
32805 #ifdef GLYPH_DEBUG
32806 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32807 doc: /* Inhibit try_window_id display optimization. */);
32808 inhibit_try_window_id = false;
32810 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32811 doc: /* Inhibit try_window_reusing display optimization. */);
32812 inhibit_try_window_reusing = false;
32814 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32815 doc: /* Inhibit try_cursor_movement display optimization. */);
32816 inhibit_try_cursor_movement = false;
32817 #endif /* GLYPH_DEBUG */
32819 DEFVAR_INT ("overline-margin", overline_margin,
32820 doc: /* Space between overline and text, in pixels.
32821 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32822 margin to the character height. */);
32823 overline_margin = 2;
32825 DEFVAR_INT ("underline-minimum-offset",
32826 underline_minimum_offset,
32827 doc: /* Minimum distance between baseline and underline.
32828 This can improve legibility of underlined text at small font sizes,
32829 particularly when using variable `x-use-underline-position-properties'
32830 with fonts that specify an UNDERLINE_POSITION relatively close to the
32831 baseline. The default value is 1. */);
32832 underline_minimum_offset = 1;
32834 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32835 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32836 This feature only works when on a window system that can change
32837 cursor shapes. */);
32838 display_hourglass_p = true;
32840 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32841 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32842 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32844 #ifdef HAVE_WINDOW_SYSTEM
32845 hourglass_atimer = NULL;
32846 hourglass_shown_p = false;
32847 #endif /* HAVE_WINDOW_SYSTEM */
32849 /* Name of the face used to display glyphless characters. */
32850 DEFSYM (Qglyphless_char, "glyphless-char");
32852 /* Method symbols for Vglyphless_char_display. */
32853 DEFSYM (Qhex_code, "hex-code");
32854 DEFSYM (Qempty_box, "empty-box");
32855 DEFSYM (Qthin_space, "thin-space");
32856 DEFSYM (Qzero_width, "zero-width");
32858 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32859 doc: /* Function run just before redisplay.
32860 It is called with one argument, which is the set of windows that are to
32861 be redisplayed. This set can be nil (meaning, only the selected window),
32862 or t (meaning all windows). */);
32863 Vpre_redisplay_function = intern ("ignore");
32865 /* Symbol for the purpose of Vglyphless_char_display. */
32866 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32867 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32869 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32870 doc: /* Char-table defining glyphless characters.
32871 Each element, if non-nil, should be one of the following:
32872 an ASCII acronym string: display this string in a box
32873 `hex-code': display the hexadecimal code of a character in a box
32874 `empty-box': display as an empty box
32875 `thin-space': display as 1-pixel width space
32876 `zero-width': don't display
32877 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32878 display method for graphical terminals and text terminals respectively.
32879 GRAPHICAL and TEXT should each have one of the values listed above.
32881 The char-table has one extra slot to control the display of a character for
32882 which no font is found. This slot only takes effect on graphical terminals.
32883 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32884 `thin-space'. The default is `empty-box'.
32886 If a character has a non-nil entry in an active display table, the
32887 display table takes effect; in this case, Emacs does not consult
32888 `glyphless-char-display' at all. */);
32889 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32890 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32891 Qempty_box);
32893 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32894 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32895 Vdebug_on_message = Qnil;
32897 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32898 doc: /* */);
32899 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32901 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32902 doc: /* */);
32903 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32905 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32906 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32907 /* Initialize to t, since we need to disable reordering until
32908 loadup.el successfully loads charprop.el. */
32909 redisplay__inhibit_bidi = true;
32911 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32912 doc: /* Non-nil means display raw bytes in hexadecimal format.
32913 The default is to use octal format (\200) whereas hexadecimal (\x80)
32914 may be more familiar to users. */);
32915 display_raw_bytes_as_hex = false;
32920 /* Initialize this module when Emacs starts. */
32922 void
32923 init_xdisp (void)
32925 CHARPOS (this_line_start_pos) = 0;
32927 if (!noninteractive)
32929 struct window *m = XWINDOW (minibuf_window);
32930 Lisp_Object frame = m->frame;
32931 struct frame *f = XFRAME (frame);
32932 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32933 struct window *r = XWINDOW (root);
32934 int i;
32936 echo_area_window = minibuf_window;
32938 r->top_line = FRAME_TOP_MARGIN (f);
32939 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32940 r->total_cols = FRAME_COLS (f);
32941 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32942 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32943 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32945 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32946 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32947 m->total_cols = FRAME_COLS (f);
32948 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32949 m->total_lines = 1;
32950 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32952 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32953 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32954 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32956 /* The default ellipsis glyphs `...'. */
32957 for (i = 0; i < 3; ++i)
32958 default_invis_vector[i] = make_number ('.');
32962 /* Allocate the buffer for frame titles.
32963 Also used for `format-mode-line'. */
32964 int size = 100;
32965 mode_line_noprop_buf = xmalloc (size);
32966 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32967 mode_line_noprop_ptr = mode_line_noprop_buf;
32968 mode_line_target = MODE_LINE_DISPLAY;
32971 help_echo_showing_p = false;
32974 #ifdef HAVE_WINDOW_SYSTEM
32976 /* Platform-independent portion of hourglass implementation. */
32978 /* Timer function of hourglass_atimer. */
32980 static void
32981 show_hourglass (struct atimer *timer)
32983 /* The timer implementation will cancel this timer automatically
32984 after this function has run. Set hourglass_atimer to null
32985 so that we know the timer doesn't have to be canceled. */
32986 hourglass_atimer = NULL;
32988 if (!hourglass_shown_p)
32990 Lisp_Object tail, frame;
32992 block_input ();
32994 FOR_EACH_FRAME (tail, frame)
32996 struct frame *f = XFRAME (frame);
32998 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32999 && FRAME_RIF (f)->show_hourglass)
33000 FRAME_RIF (f)->show_hourglass (f);
33003 hourglass_shown_p = true;
33004 unblock_input ();
33008 /* Cancel a currently active hourglass timer, and start a new one. */
33010 void
33011 start_hourglass (void)
33013 struct timespec delay;
33015 cancel_hourglass ();
33017 if (INTEGERP (Vhourglass_delay)
33018 && XINT (Vhourglass_delay) > 0)
33019 delay = make_timespec (min (XINT (Vhourglass_delay),
33020 TYPE_MAXIMUM (time_t)),
33022 else if (FLOATP (Vhourglass_delay)
33023 && XFLOAT_DATA (Vhourglass_delay) > 0)
33024 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33025 else
33026 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33028 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33029 show_hourglass, NULL);
33032 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33033 shown. */
33035 void
33036 cancel_hourglass (void)
33038 if (hourglass_atimer)
33040 cancel_atimer (hourglass_atimer);
33041 hourglass_atimer = NULL;
33044 if (hourglass_shown_p)
33046 Lisp_Object tail, frame;
33048 block_input ();
33050 FOR_EACH_FRAME (tail, frame)
33052 struct frame *f = XFRAME (frame);
33054 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33055 && FRAME_RIF (f)->hide_hourglass)
33056 FRAME_RIF (f)->hide_hourglass (f);
33057 #ifdef HAVE_NTGUI
33058 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33059 else if (!FRAME_W32_P (f))
33060 w32_arrow_cursor ();
33061 #endif
33064 hourglass_shown_p = false;
33065 unblock_input ();
33069 #endif /* HAVE_WINDOW_SYSTEM */