Be more conservative in link time optimization doc
[emacs.git] / src / xdisp.c
blobad9b29835e70f83c6067e7193176578b4c8c8d2e
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 <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
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,
880 Lisp_Object, Lisp_Object,
881 struct text_pos *, ptrdiff_t, int, 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 it.vpos = it.current_y = 0;
1199 last_height = 0;
1200 result = make_number (line_bottom_y (&it));
1201 if (old_buffer)
1202 set_buffer_internal_1 (old_buffer);
1204 return result;
1207 /* Return the default pixel height of text lines in window W. The
1208 value is the canonical height of the W frame's default font, plus
1209 any extra space required by the line-spacing variable or frame
1210 parameter.
1212 Implementation note: this ignores any line-spacing text properties
1213 put on the newline characters. This is because those properties
1214 only affect the _screen_ line ending in the newline (i.e., in a
1215 continued line, only the last screen line will be affected), which
1216 means only a small number of lines in a buffer can ever use this
1217 feature. Since this function is used to compute the default pixel
1218 equivalent of text lines in a window, we can safely ignore those
1219 few lines. For the same reasons, we ignore the line-height
1220 properties. */
1222 default_line_pixel_height (struct window *w)
1224 struct frame *f = WINDOW_XFRAME (w);
1225 int height = FRAME_LINE_HEIGHT (f);
1227 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1229 struct buffer *b = XBUFFER (w->contents);
1230 Lisp_Object val = BVAR (b, extra_line_spacing);
1232 if (NILP (val))
1233 val = BVAR (&buffer_defaults, extra_line_spacing);
1234 if (!NILP (val))
1236 if (RANGED_INTEGERP (0, val, INT_MAX))
1237 height += XFASTINT (val);
1238 else if (FLOATP (val))
1240 int addon = XFLOAT_DATA (val) * height + 0.5;
1242 if (addon >= 0)
1243 height += addon;
1246 else
1247 height += f->extra_line_spacing;
1250 return height;
1253 /* Subroutine of pos_visible_p below. Extracts a display string, if
1254 any, from the display spec given as its argument. */
1255 static Lisp_Object
1256 string_from_display_spec (Lisp_Object spec)
1258 if (VECTORP (spec))
1260 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1261 if (STRINGP (AREF (spec, i)))
1262 return AREF (spec, i);
1264 else
1266 for (; CONSP (spec); spec = XCDR (spec))
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1270 return spec;
1274 /* Limit insanely large values of W->hscroll on frame F to the largest
1275 value that will still prevent first_visible_x and last_visible_x of
1276 'struct it' from overflowing an int. */
1277 static int
1278 window_hscroll_limited (struct window *w, struct frame *f)
1280 ptrdiff_t window_hscroll = w->hscroll;
1281 int window_text_width = window_box_width (w, TEXT_AREA);
1282 int colwidth = FRAME_COLUMN_WIDTH (f);
1284 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1285 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1287 return window_hscroll;
1290 /* Return true if position CHARPOS is visible in window W.
1291 CHARPOS < 0 means return info about WINDOW_END position.
1292 If visible, set *X and *Y to pixel coordinates of top left corner.
1293 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1294 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1296 bool
1297 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1298 int *rtop, int *rbot, int *rowh, int *vpos)
1300 struct it it;
1301 void *itdata = bidi_shelve_cache ();
1302 struct text_pos top;
1303 bool visible_p = false;
1304 struct buffer *old_buffer = NULL;
1305 bool r2l = false;
1307 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1308 return visible_p;
1310 if (XBUFFER (w->contents) != current_buffer)
1312 old_buffer = current_buffer;
1313 set_buffer_internal_1 (XBUFFER (w->contents));
1316 SET_TEXT_POS_FROM_MARKER (top, w->start);
1317 /* Scrolling a minibuffer window via scroll bar when the echo area
1318 shows long text sometimes resets the minibuffer contents behind
1319 our backs. Also, someone might narrow-to-region and immediately
1320 call a scroll function. */
1321 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1322 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1324 /* If the top of the window is after CHARPOS, the latter is surely
1325 not visible. */
1326 if (charpos >= 0 && CHARPOS (top) > charpos)
1327 return visible_p;
1329 /* Some Lisp hook could call us in the middle of redisplaying this
1330 very window. If, by some bad luck, we are retrying redisplay
1331 because we found that the mode-line height and/or header-line
1332 height needs to be updated, the assignment of mode_line_height
1333 and header_line_height below could disrupt that, due to the
1334 selected/nonselected window dance during mode-line display, and
1335 we could infloop. Avoid that. */
1336 int prev_mode_line_height = w->mode_line_height;
1337 int prev_header_line_height = w->header_line_height;
1338 /* Compute exact mode line heights. */
1339 if (window_wants_mode_line (w))
1341 Lisp_Object window_mode_line_format
1342 = window_parameter (w, Qmode_line_format);
1344 w->mode_line_height
1345 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1346 NILP (window_mode_line_format)
1347 ? BVAR (current_buffer, mode_line_format)
1348 : window_mode_line_format);
1351 if (window_wants_header_line (w))
1353 Lisp_Object window_header_line_format
1354 = window_parameter (w, Qheader_line_format);
1356 w->header_line_height
1357 = display_mode_line (w, HEADER_LINE_FACE_ID,
1358 NILP (window_header_line_format)
1359 ? BVAR (current_buffer, header_line_format)
1360 : window_header_line_format);
1363 start_display (&it, w, top);
1364 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1365 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1367 if (charpos >= 0
1368 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1369 && IT_CHARPOS (it) >= charpos)
1370 /* When scanning backwards under bidi iteration, move_it_to
1371 stops at or _before_ CHARPOS, because it stops at or to
1372 the _right_ of the character at CHARPOS. */
1373 || (it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) <= charpos)))
1376 /* We have reached CHARPOS, or passed it. How the call to
1377 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1378 or covered by a display property, move_it_to stops at the end
1379 of the invisible text, to the right of CHARPOS. (ii) If
1380 CHARPOS is in a display vector, move_it_to stops on its last
1381 glyph. */
1382 int top_x = it.current_x;
1383 int top_y = it.current_y;
1384 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1385 int bottom_y;
1386 struct it save_it;
1387 void *save_it_data = NULL;
1389 /* Calling line_bottom_y may change it.method, it.position, etc. */
1390 SAVE_IT (save_it, it, save_it_data);
1391 last_height = 0;
1392 bottom_y = line_bottom_y (&it);
1393 if (top_y < window_top_y)
1394 visible_p = bottom_y > window_top_y;
1395 else if (top_y < it.last_visible_y)
1396 visible_p = true;
1397 if (bottom_y >= it.last_visible_y
1398 && it.bidi_p && it.bidi_it.scan_dir == -1
1399 && IT_CHARPOS (it) < charpos)
1401 /* When the last line of the window is scanned backwards
1402 under bidi iteration, we could be duped into thinking
1403 that we have passed CHARPOS, when in fact move_it_to
1404 simply stopped short of CHARPOS because it reached
1405 last_visible_y. To see if that's what happened, we call
1406 move_it_to again with a slightly larger vertical limit,
1407 and see if it actually moved vertically; if it did, we
1408 didn't really reach CHARPOS, which is beyond window end. */
1409 /* Why 10? because we don't know how many canonical lines
1410 will the height of the next line(s) be. So we guess. */
1411 int ten_more_lines = 10 * default_line_pixel_height (w);
1413 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1414 MOVE_TO_POS | MOVE_TO_Y);
1415 if (it.current_y > top_y)
1416 visible_p = false;
1419 RESTORE_IT (&it, &save_it, save_it_data);
1420 if (visible_p)
1422 if (it.method == GET_FROM_DISPLAY_VECTOR)
1424 /* We stopped on the last glyph of a display vector.
1425 Try and recompute. Hack alert! */
1426 if (charpos < 2 || top.charpos >= charpos)
1427 top_x = it.glyph_row->x;
1428 else
1430 struct it it2, it2_prev;
1431 /* The idea is to get to the previous buffer
1432 position, consume the character there, and use
1433 the pixel coordinates we get after that. But if
1434 the previous buffer position is also displayed
1435 from a display vector, we need to consume all of
1436 the glyphs from that display vector. */
1437 start_display (&it2, w, top);
1438 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1439 /* If we didn't get to CHARPOS - 1, there's some
1440 replacing display property at that position, and
1441 we stopped after it. That is exactly the place
1442 whose coordinates we want. */
1443 if (IT_CHARPOS (it2) != charpos - 1)
1444 it2_prev = it2;
1445 else
1447 /* Iterate until we get out of the display
1448 vector that displays the character at
1449 CHARPOS - 1. */
1450 do {
1451 get_next_display_element (&it2);
1452 PRODUCE_GLYPHS (&it2);
1453 it2_prev = it2;
1454 set_iterator_to_next (&it2, true);
1455 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1456 && IT_CHARPOS (it2) < charpos);
1458 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1459 || it2_prev.current_x > it2_prev.last_visible_x)
1460 top_x = it.glyph_row->x;
1461 else
1463 top_x = it2_prev.current_x;
1464 top_y = it2_prev.current_y;
1468 else if (IT_CHARPOS (it) != charpos)
1470 Lisp_Object cpos = make_number (charpos);
1471 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1472 Lisp_Object string = string_from_display_spec (spec);
1473 struct text_pos tpos;
1474 bool newline_in_string
1475 = (STRINGP (string)
1476 && memchr (SDATA (string), '\n', SBYTES (string)));
1478 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1479 bool replacing_spec_p
1480 = (!NILP (spec)
1481 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1482 charpos, FRAME_WINDOW_P (it.f)));
1483 /* The tricky code below is needed because there's a
1484 discrepancy between move_it_to and how we set cursor
1485 when PT is at the beginning of a portion of text
1486 covered by a display property or an overlay with a
1487 display property, or the display line ends in a
1488 newline from a display string. move_it_to will stop
1489 _after_ such display strings, whereas
1490 set_cursor_from_row conspires with cursor_row_p to
1491 place the cursor on the first glyph produced from the
1492 display string. */
1494 /* We have overshoot PT because it is covered by a
1495 display property that replaces the text it covers.
1496 If the string includes embedded newlines, we are also
1497 in the wrong display line. Backtrack to the correct
1498 line, where the display property begins. */
1499 if (replacing_spec_p)
1501 Lisp_Object startpos, endpos;
1502 EMACS_INT start, end;
1503 struct it it3;
1505 /* Find the first and the last buffer positions
1506 covered by the display string. */
1507 endpos =
1508 Fnext_single_char_property_change (cpos, Qdisplay,
1509 Qnil, Qnil);
1510 startpos =
1511 Fprevious_single_char_property_change (endpos, Qdisplay,
1512 Qnil, Qnil);
1513 start = XFASTINT (startpos);
1514 end = XFASTINT (endpos);
1515 /* Move to the last buffer position before the
1516 display property. */
1517 start_display (&it3, w, top);
1518 if (start > CHARPOS (top))
1519 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1520 /* Move forward one more line if the position before
1521 the display string is a newline or if it is the
1522 rightmost character on a line that is
1523 continued or word-wrapped. */
1524 if (it3.method == GET_FROM_BUFFER
1525 && (it3.c == '\n'
1526 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1527 move_it_by_lines (&it3, 1);
1528 else if (move_it_in_display_line_to (&it3, -1,
1529 it3.current_x
1530 + it3.pixel_width,
1531 MOVE_TO_X)
1532 == MOVE_LINE_CONTINUED)
1534 move_it_by_lines (&it3, 1);
1535 /* When we are under word-wrap, the #$@%!
1536 move_it_by_lines moves 2 lines, so we need to
1537 fix that up. */
1538 if (it3.line_wrap == WORD_WRAP)
1539 move_it_by_lines (&it3, -1);
1542 /* Record the vertical coordinate of the display
1543 line where we wound up. */
1544 top_y = it3.current_y;
1545 if (it3.bidi_p)
1547 /* When characters are reordered for display,
1548 the character displayed to the left of the
1549 display string could be _after_ the display
1550 property in the logical order. Use the
1551 smallest vertical position of these two. */
1552 start_display (&it3, w, top);
1553 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1554 if (it3.current_y < top_y)
1555 top_y = it3.current_y;
1557 /* Move from the top of the window to the beginning
1558 of the display line where the display string
1559 begins. */
1560 start_display (&it3, w, top);
1561 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1562 /* If it3_moved stays false after the 'while' loop
1563 below, that means we already were at a newline
1564 before the loop (e.g., the display string begins
1565 with a newline), so we don't need to (and cannot)
1566 inspect the glyphs of it3.glyph_row, because
1567 PRODUCE_GLYPHS will not produce anything for a
1568 newline, and thus it3.glyph_row stays at its
1569 stale content it got at top of the window. */
1570 bool it3_moved = false;
1571 /* Finally, advance the iterator until we hit the
1572 first display element whose character position is
1573 CHARPOS, or until the first newline from the
1574 display string, which signals the end of the
1575 display line. */
1576 while (get_next_display_element (&it3))
1578 PRODUCE_GLYPHS (&it3);
1579 if (IT_CHARPOS (it3) == charpos
1580 || ITERATOR_AT_END_OF_LINE_P (&it3))
1581 break;
1582 it3_moved = true;
1583 set_iterator_to_next (&it3, false);
1585 top_x = it3.current_x - it3.pixel_width;
1586 /* Normally, we would exit the above loop because we
1587 found the display element whose character
1588 position is CHARPOS. For the contingency that we
1589 didn't, and stopped at the first newline from the
1590 display string, move back over the glyphs
1591 produced from the string, until we find the
1592 rightmost glyph not from the string. */
1593 if (it3_moved
1594 && newline_in_string
1595 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1597 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1598 + it3.glyph_row->used[TEXT_AREA];
1600 while (EQ ((g - 1)->object, string))
1602 --g;
1603 top_x -= g->pixel_width;
1605 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1606 + it3.glyph_row->used[TEXT_AREA]);
1611 *x = top_x;
1612 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1613 *rtop = max (0, window_top_y - top_y);
1614 *rbot = max (0, bottom_y - it.last_visible_y);
1615 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1616 - max (top_y, window_top_y)));
1617 *vpos = it.vpos;
1618 if (it.bidi_it.paragraph_dir == R2L)
1619 r2l = true;
1622 else
1624 /* Either we were asked to provide info about WINDOW_END, or
1625 CHARPOS is in the partially visible glyph row at end of
1626 window. */
1627 struct it it2;
1628 void *it2data = NULL;
1630 SAVE_IT (it2, it, it2data);
1631 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1632 move_it_by_lines (&it, 1);
1633 if (charpos < IT_CHARPOS (it)
1634 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1636 visible_p = true;
1637 RESTORE_IT (&it2, &it2, it2data);
1638 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1639 *x = it2.current_x;
1640 *y = it2.current_y + it2.max_ascent - it2.ascent;
1641 *rtop = max (0, -it2.current_y);
1642 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1643 - it.last_visible_y));
1644 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1645 it.last_visible_y)
1646 - max (it2.current_y,
1647 WINDOW_HEADER_LINE_HEIGHT (w))));
1648 *vpos = it2.vpos;
1649 if (it2.bidi_it.paragraph_dir == R2L)
1650 r2l = true;
1652 else
1653 bidi_unshelve_cache (it2data, true);
1655 bidi_unshelve_cache (itdata, false);
1657 if (old_buffer)
1658 set_buffer_internal_1 (old_buffer);
1660 if (visible_p)
1662 if (w->hscroll > 0)
1663 *x -=
1664 window_hscroll_limited (w, WINDOW_XFRAME (w))
1665 * WINDOW_FRAME_COLUMN_WIDTH (w);
1666 /* For lines in an R2L paragraph, we need to mirror the X pixel
1667 coordinate wrt the text area. For the reasons, see the
1668 commentary in buffer_posn_from_coords and the explanation of
1669 the geometry used by the move_it_* functions at the end of
1670 the large commentary near the beginning of this file. */
1671 if (r2l)
1672 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1675 #if false
1676 /* Debugging code. */
1677 if (visible_p)
1678 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1679 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1680 else
1681 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1682 #endif
1684 /* Restore potentially overwritten values. */
1685 w->mode_line_height = prev_mode_line_height;
1686 w->header_line_height = prev_header_line_height;
1688 return visible_p;
1692 /* Return the next character from STR. Return in *LEN the length of
1693 the character. This is like STRING_CHAR_AND_LENGTH but never
1694 returns an invalid character. If we find one, we return a `?', but
1695 with the length of the invalid character. */
1697 static int
1698 string_char_and_length (const unsigned char *str, int *len)
1700 int c;
1702 c = STRING_CHAR_AND_LENGTH (str, *len);
1703 if (!CHAR_VALID_P (c))
1704 /* We may not change the length here because other places in Emacs
1705 don't use this function, i.e. they silently accept invalid
1706 characters. */
1707 c = '?';
1709 return c;
1714 /* Given a position POS containing a valid character and byte position
1715 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1717 static struct text_pos
1718 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1720 eassert (STRINGP (string) && nchars >= 0);
1722 if (STRING_MULTIBYTE (string))
1724 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1725 int len;
1727 while (nchars--)
1729 string_char_and_length (p, &len);
1730 p += len;
1731 CHARPOS (pos) += 1;
1732 BYTEPOS (pos) += len;
1735 else
1736 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1738 return pos;
1742 /* Value is the text position, i.e. character and byte position,
1743 for character position CHARPOS in STRING. */
1745 static struct text_pos
1746 string_pos (ptrdiff_t charpos, Lisp_Object string)
1748 struct text_pos pos;
1749 eassert (STRINGP (string));
1750 eassert (charpos >= 0);
1751 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1752 return pos;
1756 /* Value is a text position, i.e. character and byte position, for
1757 character position CHARPOS in C string S. MULTIBYTE_P
1758 means recognize multibyte characters. */
1760 static struct text_pos
1761 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1763 struct text_pos pos;
1765 eassert (s != NULL);
1766 eassert (charpos >= 0);
1768 if (multibyte_p)
1770 int len;
1772 SET_TEXT_POS (pos, 0, 0);
1773 while (charpos--)
1775 string_char_and_length ((const unsigned char *) s, &len);
1776 s += len;
1777 CHARPOS (pos) += 1;
1778 BYTEPOS (pos) += len;
1781 else
1782 SET_TEXT_POS (pos, charpos, charpos);
1784 return pos;
1788 /* Value is the number of characters in C string S. MULTIBYTE_P
1789 means recognize multibyte characters. */
1791 static ptrdiff_t
1792 number_of_chars (const char *s, bool multibyte_p)
1794 ptrdiff_t nchars;
1796 if (multibyte_p)
1798 ptrdiff_t rest = strlen (s);
1799 int len;
1800 const unsigned char *p = (const unsigned char *) s;
1802 for (nchars = 0; rest > 0; ++nchars)
1804 string_char_and_length (p, &len);
1805 rest -= len, p += len;
1808 else
1809 nchars = strlen (s);
1811 return nchars;
1815 /* Compute byte position NEWPOS->bytepos corresponding to
1816 NEWPOS->charpos. POS is a known position in string STRING.
1817 NEWPOS->charpos must be >= POS.charpos. */
1819 static void
1820 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1822 eassert (STRINGP (string));
1823 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1825 if (STRING_MULTIBYTE (string))
1826 *newpos = string_pos_nchars_ahead (pos, string,
1827 CHARPOS (*newpos) - CHARPOS (pos));
1828 else
1829 BYTEPOS (*newpos) = CHARPOS (*newpos);
1832 /* EXPORT:
1833 Return an estimation of the pixel height of mode or header lines on
1834 frame F. FACE_ID specifies what line's height to estimate. */
1837 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1839 #ifdef HAVE_WINDOW_SYSTEM
1840 if (FRAME_WINDOW_P (f))
1842 int height = FONT_HEIGHT (FRAME_FONT (f));
1844 /* This function is called so early when Emacs starts that the face
1845 cache and mode line face are not yet initialized. */
1846 if (FRAME_FACE_CACHE (f))
1848 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1849 if (face)
1851 if (face->font)
1852 height = normal_char_height (face->font, -1);
1853 if (face->box_line_width > 0)
1854 height += 2 * face->box_line_width;
1858 return height;
1860 #endif
1862 return 1;
1865 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1866 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1867 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1868 not force the value into range. */
1870 void
1871 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1872 NativeRectangle *bounds, bool noclip)
1875 #ifdef HAVE_WINDOW_SYSTEM
1876 if (FRAME_WINDOW_P (f))
1878 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1879 even for negative values. */
1880 if (pix_x < 0)
1881 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1882 if (pix_y < 0)
1883 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1885 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1886 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1888 if (bounds)
1889 STORE_NATIVE_RECT (*bounds,
1890 FRAME_COL_TO_PIXEL_X (f, pix_x),
1891 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1892 FRAME_COLUMN_WIDTH (f) - 1,
1893 FRAME_LINE_HEIGHT (f) - 1);
1895 /* PXW: Should we clip pixels before converting to columns/lines? */
1896 if (!noclip)
1898 if (pix_x < 0)
1899 pix_x = 0;
1900 else if (pix_x > FRAME_TOTAL_COLS (f))
1901 pix_x = FRAME_TOTAL_COLS (f);
1903 if (pix_y < 0)
1904 pix_y = 0;
1905 else if (pix_y > FRAME_TOTAL_LINES (f))
1906 pix_y = FRAME_TOTAL_LINES (f);
1909 #endif
1911 *x = pix_x;
1912 *y = pix_y;
1916 /* Find the glyph under window-relative coordinates X/Y in window W.
1917 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1918 strings. Return in *HPOS and *VPOS the row and column number of
1919 the glyph found. Return in *AREA the glyph area containing X.
1920 Value is a pointer to the glyph found or null if X/Y is not on
1921 text, or we can't tell because W's current matrix is not up to
1922 date. */
1924 static struct glyph *
1925 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1926 int *dx, int *dy, int *area)
1928 struct glyph *glyph, *end;
1929 struct glyph_row *row = NULL;
1930 int x0, i;
1932 /* Find row containing Y. Give up if some row is not enabled. */
1933 for (i = 0; i < w->current_matrix->nrows; ++i)
1935 row = MATRIX_ROW (w->current_matrix, i);
1936 if (!row->enabled_p)
1937 return NULL;
1938 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1939 break;
1942 *vpos = i;
1943 *hpos = 0;
1945 /* Give up if Y is not in the window. */
1946 if (i == w->current_matrix->nrows)
1947 return NULL;
1949 /* Get the glyph area containing X. */
1950 if (w->pseudo_window_p)
1952 *area = TEXT_AREA;
1953 x0 = 0;
1955 else
1957 if (x < window_box_left_offset (w, TEXT_AREA))
1959 *area = LEFT_MARGIN_AREA;
1960 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1962 else if (x < window_box_right_offset (w, TEXT_AREA))
1964 *area = TEXT_AREA;
1965 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1967 else
1969 *area = RIGHT_MARGIN_AREA;
1970 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1974 /* Find glyph containing X. */
1975 glyph = row->glyphs[*area];
1976 end = glyph + row->used[*area];
1977 x -= x0;
1978 while (glyph < end && x >= glyph->pixel_width)
1980 x -= glyph->pixel_width;
1981 ++glyph;
1984 if (glyph == end)
1985 return NULL;
1987 if (dx)
1989 *dx = x;
1990 *dy = y - (row->y + row->ascent - glyph->ascent);
1993 *hpos = glyph - row->glyphs[*area];
1994 return glyph;
1997 /* Convert frame-relative x/y to coordinates relative to window W.
1998 Takes pseudo-windows into account. */
2000 static void
2001 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2003 if (w->pseudo_window_p)
2005 /* A pseudo-window is always full-width, and starts at the
2006 left edge of the frame, plus a frame border. */
2007 struct frame *f = XFRAME (w->frame);
2008 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2009 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2011 else
2013 *x -= WINDOW_LEFT_EDGE_X (w);
2014 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2018 #ifdef HAVE_WINDOW_SYSTEM
2020 /* EXPORT:
2021 Return in RECTS[] at most N clipping rectangles for glyph string S.
2022 Return the number of stored rectangles. */
2025 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2027 XRectangle r;
2029 if (n <= 0)
2030 return 0;
2032 if (s->row->full_width_p)
2034 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2035 r.x = WINDOW_LEFT_EDGE_X (s->w);
2036 if (s->row->mode_line_p)
2037 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2038 else
2039 r.width = WINDOW_PIXEL_WIDTH (s->w);
2041 /* Unless displaying a mode or menu bar line, which are always
2042 fully visible, clip to the visible part of the row. */
2043 if (s->w->pseudo_window_p)
2044 r.height = s->row->visible_height;
2045 else
2046 r.height = s->height;
2048 else
2050 /* This is a text line that may be partially visible. */
2051 r.x = window_box_left (s->w, s->area);
2052 r.width = window_box_width (s->w, s->area);
2053 r.height = s->row->visible_height;
2056 if (s->clip_head)
2057 if (r.x < s->clip_head->x)
2059 if (r.width >= s->clip_head->x - r.x)
2060 r.width -= s->clip_head->x - r.x;
2061 else
2062 r.width = 0;
2063 r.x = s->clip_head->x;
2065 if (s->clip_tail)
2066 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2068 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2069 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2070 else
2071 r.width = 0;
2074 /* If S draws overlapping rows, it's sufficient to use the top and
2075 bottom of the window for clipping because this glyph string
2076 intentionally draws over other lines. */
2077 if (s->for_overlaps)
2079 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2080 r.height = window_text_bottom_y (s->w) - r.y;
2082 /* Alas, the above simple strategy does not work for the
2083 environments with anti-aliased text: if the same text is
2084 drawn onto the same place multiple times, it gets thicker.
2085 If the overlap we are processing is for the erased cursor, we
2086 take the intersection with the rectangle of the cursor. */
2087 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2089 XRectangle rc, r_save = r;
2091 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2092 rc.y = s->w->phys_cursor.y;
2093 rc.width = s->w->phys_cursor_width;
2094 rc.height = s->w->phys_cursor_height;
2096 x_intersect_rectangles (&r_save, &rc, &r);
2099 else
2101 /* Don't use S->y for clipping because it doesn't take partially
2102 visible lines into account. For example, it can be negative for
2103 partially visible lines at the top of a window. */
2104 if (!s->row->full_width_p
2105 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2106 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2107 else
2108 r.y = max (0, s->row->y);
2111 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2113 /* If drawing the cursor, don't let glyph draw outside its
2114 advertised boundaries. Cleartype does this under some circumstances. */
2115 if (s->hl == DRAW_CURSOR)
2117 struct glyph *glyph = s->first_glyph;
2118 int height, max_y;
2120 if (s->x > r.x)
2122 if (r.width >= s->x - r.x)
2123 r.width -= s->x - r.x;
2124 else /* R2L hscrolled row with cursor outside text area */
2125 r.width = 0;
2126 r.x = s->x;
2128 r.width = min (r.width, glyph->pixel_width);
2130 /* If r.y is below window bottom, ensure that we still see a cursor. */
2131 height = min (glyph->ascent + glyph->descent,
2132 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2133 max_y = window_text_bottom_y (s->w) - height;
2134 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2135 if (s->ybase - glyph->ascent > max_y)
2137 r.y = max_y;
2138 r.height = height;
2140 else
2142 /* Don't draw cursor glyph taller than our actual glyph. */
2143 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2144 if (height < r.height)
2146 max_y = r.y + r.height;
2147 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2148 r.height = min (max_y - r.y, height);
2153 if (s->row->clip)
2155 XRectangle r_save = r;
2157 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2158 r.width = 0;
2161 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2162 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2164 #ifdef CONVERT_FROM_XRECT
2165 CONVERT_FROM_XRECT (r, *rects);
2166 #else
2167 *rects = r;
2168 #endif
2169 return 1;
2171 else
2173 /* If we are processing overlapping and allowed to return
2174 multiple clipping rectangles, we exclude the row of the glyph
2175 string from the clipping rectangle. This is to avoid drawing
2176 the same text on the environment with anti-aliasing. */
2177 #ifdef CONVERT_FROM_XRECT
2178 XRectangle rs[2];
2179 #else
2180 XRectangle *rs = rects;
2181 #endif
2182 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2184 if (s->for_overlaps & OVERLAPS_PRED)
2186 rs[i] = r;
2187 if (r.y + r.height > row_y)
2189 if (r.y < row_y)
2190 rs[i].height = row_y - r.y;
2191 else
2192 rs[i].height = 0;
2194 i++;
2196 if (s->for_overlaps & OVERLAPS_SUCC)
2198 rs[i] = r;
2199 if (r.y < row_y + s->row->visible_height)
2201 if (r.y + r.height > row_y + s->row->visible_height)
2203 rs[i].y = row_y + s->row->visible_height;
2204 rs[i].height = r.y + r.height - rs[i].y;
2206 else
2207 rs[i].height = 0;
2209 i++;
2212 n = i;
2213 #ifdef CONVERT_FROM_XRECT
2214 for (i = 0; i < n; i++)
2215 CONVERT_FROM_XRECT (rs[i], rects[i]);
2216 #endif
2217 return n;
2221 /* EXPORT:
2222 Return in *NR the clipping rectangle for glyph string S. */
2224 void
2225 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2227 get_glyph_string_clip_rects (s, nr, 1);
2231 /* EXPORT:
2232 Return the position and height of the phys cursor in window W.
2233 Set w->phys_cursor_width to width of phys cursor.
2236 void
2237 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2238 struct glyph *glyph, int *xp, int *yp, int *heightp)
2240 struct frame *f = XFRAME (WINDOW_FRAME (w));
2241 int x, y, wd, h, h0, y0, ascent;
2243 /* Compute the width of the rectangle to draw. If on a stretch
2244 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2245 rectangle as wide as the glyph, but use a canonical character
2246 width instead. */
2247 wd = glyph->pixel_width;
2249 x = w->phys_cursor.x;
2250 if (x < 0)
2252 wd += x;
2253 x = 0;
2256 if (glyph->type == STRETCH_GLYPH
2257 && !x_stretch_cursor_p)
2258 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2259 w->phys_cursor_width = wd;
2261 /* Don't let the hollow cursor glyph descend below the glyph row's
2262 ascent value, lest the hollow cursor looks funny. */
2263 y = w->phys_cursor.y;
2264 ascent = row->ascent;
2265 if (row->ascent < glyph->ascent)
2267 y -= glyph->ascent - row->ascent;
2268 ascent = glyph->ascent;
2271 /* If y is below window bottom, ensure that we still see a cursor. */
2272 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2274 h = max (h0, ascent + glyph->descent);
2275 h0 = min (h0, ascent + glyph->descent);
2277 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2278 if (y < y0)
2280 h = max (h - (y0 - y) + 1, h0);
2281 y = y0 - 1;
2283 else
2285 y0 = window_text_bottom_y (w) - h0;
2286 if (y > y0)
2288 h += y - y0;
2289 y = y0;
2293 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2294 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2295 *heightp = h;
2299 * Remember which glyph the mouse is over.
2302 void
2303 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2305 Lisp_Object window;
2306 struct window *w;
2307 struct glyph_row *r, *gr, *end_row;
2308 enum window_part part;
2309 enum glyph_row_area area;
2310 int x, y, width, height;
2312 /* Try to determine frame pixel position and size of the glyph under
2313 frame pixel coordinates X/Y on frame F. */
2315 if (window_resize_pixelwise)
2317 width = height = 1;
2318 goto virtual_glyph;
2320 else if (!f->glyphs_initialized_p
2321 || (window = window_from_coordinates (f, gx, gy, &part, false),
2322 NILP (window)))
2324 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2325 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2326 goto virtual_glyph;
2329 w = XWINDOW (window);
2330 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2331 height = WINDOW_FRAME_LINE_HEIGHT (w);
2333 x = window_relative_x_coord (w, part, gx);
2334 y = gy - WINDOW_TOP_EDGE_Y (w);
2336 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2337 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2339 if (w->pseudo_window_p)
2341 area = TEXT_AREA;
2342 part = ON_MODE_LINE; /* Don't adjust margin. */
2343 goto text_glyph;
2346 switch (part)
2348 case ON_LEFT_MARGIN:
2349 area = LEFT_MARGIN_AREA;
2350 goto text_glyph;
2352 case ON_RIGHT_MARGIN:
2353 area = RIGHT_MARGIN_AREA;
2354 goto text_glyph;
2356 case ON_HEADER_LINE:
2357 case ON_MODE_LINE:
2358 gr = (part == ON_HEADER_LINE
2359 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2360 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2361 gy = gr->y;
2362 area = TEXT_AREA;
2363 goto text_glyph_row_found;
2365 case ON_TEXT:
2366 area = TEXT_AREA;
2368 text_glyph:
2369 gr = 0; gy = 0;
2370 for (; r <= end_row && r->enabled_p; ++r)
2371 if (r->y + r->height > y)
2373 gr = r; gy = r->y;
2374 break;
2377 text_glyph_row_found:
2378 if (gr && gy <= y)
2380 struct glyph *g = gr->glyphs[area];
2381 struct glyph *end = g + gr->used[area];
2383 height = gr->height;
2384 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2385 if (gx + g->pixel_width > x)
2386 break;
2388 if (g < end)
2390 if (g->type == IMAGE_GLYPH)
2392 /* Don't remember when mouse is over image, as
2393 image may have hot-spots. */
2394 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2395 return;
2397 width = g->pixel_width;
2399 else
2401 /* Use nominal char spacing at end of line. */
2402 x -= gx;
2403 gx += (x / width) * width;
2406 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2408 gx += window_box_left_offset (w, area);
2409 /* Don't expand over the modeline to make sure the vertical
2410 drag cursor is shown early enough. */
2411 height = min (height,
2412 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2415 else
2417 /* Use nominal line height at end of window. */
2418 gx = (x / width) * width;
2419 y -= gy;
2420 gy += (y / height) * height;
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2422 /* See comment above. */
2423 height = min (height,
2424 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2426 break;
2428 case ON_LEFT_FRINGE:
2429 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2431 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2432 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2433 goto row_glyph;
2435 case ON_RIGHT_FRINGE:
2436 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 : window_box_right_offset (w, TEXT_AREA));
2439 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2440 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2441 && !WINDOW_RIGHTMOST_P (w))
2442 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2443 /* Make sure the vertical border can get her own glyph to the
2444 right of the one we build here. */
2445 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2446 else
2447 width = WINDOW_PIXEL_WIDTH (w) - gx;
2448 else
2449 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2451 goto row_glyph;
2453 case ON_VERTICAL_BORDER:
2454 gx = WINDOW_PIXEL_WIDTH (w) - width;
2455 goto row_glyph;
2457 case ON_VERTICAL_SCROLL_BAR:
2458 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2460 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2461 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2462 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2463 : 0)));
2464 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2466 row_glyph:
2467 gr = 0, gy = 0;
2468 for (; r <= end_row && r->enabled_p; ++r)
2469 if (r->y + r->height > y)
2471 gr = r; gy = r->y;
2472 break;
2475 if (gr && gy <= y)
2476 height = gr->height;
2477 else
2479 /* Use nominal line height at end of window. */
2480 y -= gy;
2481 gy += (y / height) * height;
2483 break;
2485 case ON_RIGHT_DIVIDER:
2486 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2487 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2488 gy = 0;
2489 /* The bottom divider prevails. */
2490 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2491 goto add_edge;
2493 case ON_BOTTOM_DIVIDER:
2494 gx = 0;
2495 width = WINDOW_PIXEL_WIDTH (w);
2496 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2497 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2498 goto add_edge;
2500 default:
2502 virtual_glyph:
2503 /* If there is no glyph under the mouse, then we divide the screen
2504 into a grid of the smallest glyph in the frame, and use that
2505 as our "glyph". */
2507 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2508 round down even for negative values. */
2509 if (gx < 0)
2510 gx -= width - 1;
2511 if (gy < 0)
2512 gy -= height - 1;
2514 gx = (gx / width) * width;
2515 gy = (gy / height) * height;
2517 goto store_rect;
2520 add_edge:
2521 gx += WINDOW_LEFT_EDGE_X (w);
2522 gy += WINDOW_TOP_EDGE_Y (w);
2524 store_rect:
2525 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2527 /* Visible feedback for debugging. */
2528 #if false && defined HAVE_X_WINDOWS
2529 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2530 f->output_data.x->normal_gc,
2531 gx, gy, width, height);
2532 #endif
2536 #endif /* HAVE_WINDOW_SYSTEM */
2538 static void
2539 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2541 eassert (w);
2542 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2543 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2544 w->window_end_vpos
2545 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2548 static bool
2549 hscrolling_current_line_p (struct window *w)
2551 return (!w->suspend_auto_hscroll
2552 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2553 Qcurrent_line));
2556 /***********************************************************************
2557 Lisp form evaluation
2558 ***********************************************************************/
2560 /* Error handler for safe_eval and safe_call. */
2562 static Lisp_Object
2563 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2565 add_to_log ("Error during redisplay: %S signaled %S",
2566 Flist (nargs, args), arg);
2567 return Qnil;
2570 /* Call function FUNC with the rest of NARGS - 1 arguments
2571 following. Return the result, or nil if something went
2572 wrong. Prevent redisplay during the evaluation. */
2574 static Lisp_Object
2575 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2577 Lisp_Object val;
2579 if (inhibit_eval_during_redisplay)
2580 val = Qnil;
2581 else
2583 ptrdiff_t i;
2584 ptrdiff_t count = SPECPDL_INDEX ();
2585 Lisp_Object *args;
2586 USE_SAFE_ALLOCA;
2587 SAFE_ALLOCA_LISP (args, nargs);
2589 args[0] = func;
2590 for (i = 1; i < nargs; i++)
2591 args[i] = va_arg (ap, Lisp_Object);
2593 specbind (Qinhibit_redisplay, Qt);
2594 if (inhibit_quit)
2595 specbind (Qinhibit_quit, Qt);
2596 /* Use Qt to ensure debugger does not run,
2597 so there is no possibility of wanting to redisplay. */
2598 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2599 safe_eval_handler);
2600 SAFE_FREE ();
2601 val = unbind_to (count, val);
2604 return val;
2607 Lisp_Object
2608 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2610 Lisp_Object retval;
2611 va_list ap;
2613 va_start (ap, func);
2614 retval = safe__call (false, nargs, func, ap);
2615 va_end (ap);
2616 return retval;
2619 /* Call function FN with one argument ARG.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2625 return safe_call (2, fn, arg);
2628 static Lisp_Object
2629 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2631 Lisp_Object retval;
2632 va_list ap;
2634 va_start (ap, fn);
2635 retval = safe__call (inhibit_quit, 2, fn, ap);
2636 va_end (ap);
2637 return retval;
2640 Lisp_Object
2641 safe_eval (Lisp_Object sexpr)
2643 return safe__call1 (false, Qeval, sexpr);
2646 static Lisp_Object
2647 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2649 return safe__call1 (inhibit_quit, Qeval, sexpr);
2652 /* Call function FN with two arguments ARG1 and ARG2.
2653 Return the result, or nil if something went wrong. */
2655 Lisp_Object
2656 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2658 return safe_call (3, fn, arg1, arg2);
2663 /***********************************************************************
2664 Debugging
2665 ***********************************************************************/
2667 /* Define CHECK_IT to perform sanity checks on iterators.
2668 This is for debugging. It is too slow to do unconditionally. */
2670 static void
2671 CHECK_IT (struct it *it)
2673 #if false
2674 if (it->method == GET_FROM_STRING)
2676 eassert (STRINGP (it->string));
2677 eassert (IT_STRING_CHARPOS (*it) >= 0);
2679 else
2681 eassert (IT_STRING_CHARPOS (*it) < 0);
2682 if (it->method == GET_FROM_BUFFER)
2684 /* Check that character and byte positions agree. */
2685 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2689 if (it->dpvec)
2690 eassert (it->current.dpvec_index >= 0);
2691 else
2692 eassert (it->current.dpvec_index < 0);
2693 #endif
2697 /* Check that the window end of window W is what we expect it
2698 to be---the last row in the current matrix displaying text. */
2700 static void
2701 CHECK_WINDOW_END (struct window *w)
2703 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2704 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2706 struct glyph_row *row;
2707 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2708 !row->enabled_p
2709 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2710 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2712 #endif
2715 /***********************************************************************
2716 Iterator initialization
2717 ***********************************************************************/
2719 /* Initialize IT for displaying current_buffer in window W, starting
2720 at character position CHARPOS. CHARPOS < 0 means that no buffer
2721 position is specified which is useful when the iterator is assigned
2722 a position later. BYTEPOS is the byte position corresponding to
2723 CHARPOS.
2725 If ROW is not null, calls to produce_glyphs with IT as parameter
2726 will produce glyphs in that row.
2728 BASE_FACE_ID is the id of a base face to use. It must be one of
2729 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2730 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2731 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2733 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2735 will be initialized to use the corresponding mode line glyph row of
2736 the desired matrix of W. */
2738 void
2739 init_iterator (struct it *it, struct window *w,
2740 ptrdiff_t charpos, ptrdiff_t bytepos,
2741 struct glyph_row *row, enum face_id base_face_id)
2743 enum face_id remapped_base_face_id = base_face_id;
2745 /* Some precondition checks. */
2746 eassert (w != NULL && it != NULL);
2747 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2748 && charpos <= ZV));
2750 /* If face attributes have been changed since the last redisplay,
2751 free realized faces now because they depend on face definitions
2752 that might have changed. Don't free faces while there might be
2753 desired matrices pending which reference these faces. */
2754 if (!inhibit_free_realized_faces)
2756 if (face_change)
2758 face_change = false;
2759 XFRAME (w->frame)->face_change = 0;
2760 free_all_realized_faces (Qnil);
2762 else if (XFRAME (w->frame)->face_change)
2764 XFRAME (w->frame)->face_change = 0;
2765 free_all_realized_faces (w->frame);
2769 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2770 if (! NILP (Vface_remapping_alist))
2771 remapped_base_face_id
2772 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2774 /* Use one of the mode line rows of W's desired matrix if
2775 appropriate. */
2776 if (row == NULL)
2778 if (base_face_id == MODE_LINE_FACE_ID
2779 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2780 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2781 else if (base_face_id == HEADER_LINE_FACE_ID)
2782 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2785 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2786 Other parts of redisplay rely on that. */
2787 memclear (it, sizeof *it);
2788 it->current.overlay_string_index = -1;
2789 it->current.dpvec_index = -1;
2790 it->base_face_id = remapped_base_face_id;
2791 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2792 it->paragraph_embedding = L2R;
2793 it->bidi_it.w = w;
2795 /* The window in which we iterate over current_buffer: */
2796 XSETWINDOW (it->window, w);
2797 it->w = w;
2798 it->f = XFRAME (w->frame);
2800 it->cmp_it.id = -1;
2802 /* Extra space between lines (on window systems only). */
2803 if (base_face_id == DEFAULT_FACE_ID
2804 && FRAME_WINDOW_P (it->f))
2806 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2807 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2808 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2809 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2810 * FRAME_LINE_HEIGHT (it->f));
2811 else if (it->f->extra_line_spacing > 0)
2812 it->extra_line_spacing = it->f->extra_line_spacing;
2815 /* If realized faces have been removed, e.g. because of face
2816 attribute changes of named faces, recompute them. When running
2817 in batch mode, the face cache of the initial frame is null. If
2818 we happen to get called, make a dummy face cache. */
2819 if (FRAME_FACE_CACHE (it->f) == NULL)
2820 init_frame_faces (it->f);
2821 if (FRAME_FACE_CACHE (it->f)->used == 0)
2822 recompute_basic_faces (it->f);
2824 it->override_ascent = -1;
2826 /* Are control characters displayed as `^C'? */
2827 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2829 /* -1 means everything between a CR and the following line end
2830 is invisible. >0 means lines indented more than this value are
2831 invisible. */
2832 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2833 ? (clip_to_bounds
2834 (-1, XINT (BVAR (current_buffer, selective_display)),
2835 PTRDIFF_MAX))
2836 : (!NILP (BVAR (current_buffer, selective_display))
2837 ? -1 : 0));
2838 it->selective_display_ellipsis_p
2839 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2841 /* Display table to use. */
2842 it->dp = window_display_table (w);
2844 /* Are multibyte characters enabled in current_buffer? */
2845 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2847 /* Get the position at which the redisplay_end_trigger hook should
2848 be run, if it is to be run at all. */
2849 if (MARKERP (w->redisplay_end_trigger)
2850 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2851 it->redisplay_end_trigger_charpos
2852 = marker_position (w->redisplay_end_trigger);
2853 else if (INTEGERP (w->redisplay_end_trigger))
2854 it->redisplay_end_trigger_charpos
2855 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2856 PTRDIFF_MAX);
2858 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2860 /* Are lines in the display truncated? */
2861 if (TRUNCATE != 0)
2862 it->line_wrap = TRUNCATE;
2863 if (base_face_id == DEFAULT_FACE_ID
2864 && !it->w->hscroll
2865 && (WINDOW_FULL_WIDTH_P (it->w)
2866 || NILP (Vtruncate_partial_width_windows)
2867 || (INTEGERP (Vtruncate_partial_width_windows)
2868 /* PXW: Shall we do something about this? */
2869 && (XINT (Vtruncate_partial_width_windows)
2870 <= WINDOW_TOTAL_COLS (it->w))))
2871 && NILP (BVAR (current_buffer, truncate_lines)))
2872 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2873 ? WINDOW_WRAP : WORD_WRAP;
2875 /* Get dimensions of truncation and continuation glyphs. These are
2876 displayed as fringe bitmaps under X, but we need them for such
2877 frames when the fringes are turned off. The no_special_glyphs slot
2878 of the iterator's frame, when set, suppresses their display - by
2879 default for tooltip frames and when set via the 'no-special-glyphs'
2880 frame parameter. */
2881 #ifdef HAVE_WINDOW_SYSTEM
2882 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2883 #endif
2885 if (it->line_wrap == TRUNCATE)
2887 /* We will need the truncation glyph. */
2888 eassert (it->glyph_row == NULL);
2889 produce_special_glyphs (it, IT_TRUNCATION);
2890 it->truncation_pixel_width = it->pixel_width;
2892 else
2894 /* We will need the continuation glyph. */
2895 eassert (it->glyph_row == NULL);
2896 produce_special_glyphs (it, IT_CONTINUATION);
2897 it->continuation_pixel_width = it->pixel_width;
2901 /* Reset these values to zero because the produce_special_glyphs
2902 above has changed them. */
2903 it->pixel_width = it->ascent = it->descent = 0;
2904 it->phys_ascent = it->phys_descent = 0;
2906 /* Set this after getting the dimensions of truncation and
2907 continuation glyphs, so that we don't produce glyphs when calling
2908 produce_special_glyphs, above. */
2909 it->glyph_row = row;
2910 it->area = TEXT_AREA;
2912 /* Get the dimensions of the display area. The display area
2913 consists of the visible window area plus a horizontally scrolled
2914 part to the left of the window. All x-values are relative to the
2915 start of this total display area. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2918 /* Mode lines, menu bar in terminal frames. */
2919 it->first_visible_x = 0;
2920 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2922 else
2924 /* When hscrolling only the current line, don't apply the
2925 hscroll here, it will be applied by display_line when it gets
2926 to laying out the line showing point. However, if the
2927 window's min_hscroll is positive, the user specified a lower
2928 bound for automatic hscrolling, so they expect the
2929 non-current lines to obey that hscroll amount. */
2930 if (hscrolling_current_line_p (w))
2932 if (w->min_hscroll > 0)
2933 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2934 else
2935 it->first_visible_x = 0;
2937 else
2938 it->first_visible_x =
2939 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2940 it->last_visible_x = (it->first_visible_x
2941 + window_box_width (w, TEXT_AREA));
2943 /* If we truncate lines, leave room for the truncation glyph(s) at
2944 the right margin. Otherwise, leave room for the continuation
2945 glyph(s). Done only if the window has no right fringe. */
2946 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2948 if (it->line_wrap == TRUNCATE)
2949 it->last_visible_x -= it->truncation_pixel_width;
2950 else
2951 it->last_visible_x -= it->continuation_pixel_width;
2954 it->header_line_p = window_wants_header_line (w);
2955 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2958 /* Leave room for a border glyph. */
2959 if (!FRAME_WINDOW_P (it->f)
2960 && !WINDOW_RIGHTMOST_P (it->w))
2961 it->last_visible_x -= 1;
2963 it->last_visible_y = window_text_bottom_y (w);
2965 /* For mode lines and alike, arrange for the first glyph having a
2966 left box line if the face specifies a box. */
2967 if (base_face_id != DEFAULT_FACE_ID)
2969 struct face *face;
2971 it->face_id = remapped_base_face_id;
2973 /* If we have a boxed mode line, make the first character appear
2974 with a left box line. */
2975 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2976 if (face && face->box != FACE_NO_BOX)
2977 it->start_of_box_run_p = true;
2980 /* If a buffer position was specified, set the iterator there,
2981 getting overlays and face properties from that position. */
2982 if (charpos >= BUF_BEG (current_buffer))
2984 it->stop_charpos = charpos;
2985 it->end_charpos = ZV;
2986 eassert (charpos == BYTE_TO_CHAR (bytepos));
2987 IT_CHARPOS (*it) = charpos;
2988 IT_BYTEPOS (*it) = bytepos;
2990 /* We will rely on `reseat' to set this up properly, via
2991 handle_face_prop. */
2992 it->face_id = it->base_face_id;
2994 it->start = it->current;
2995 /* Do we need to reorder bidirectional text? Not if this is a
2996 unibyte buffer: by definition, none of the single-byte
2997 characters are strong R2L, so no reordering is needed. And
2998 bidi.c doesn't support unibyte buffers anyway. Also, don't
2999 reorder while we are loading loadup.el, since the tables of
3000 character properties needed for reordering are not yet
3001 available. */
3002 it->bidi_p =
3003 !redisplay__inhibit_bidi
3004 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3005 && it->multibyte_p;
3007 /* If we are to reorder bidirectional text, init the bidi
3008 iterator. */
3009 if (it->bidi_p)
3011 /* Since we don't know at this point whether there will be
3012 any R2L lines in the window, we reserve space for
3013 truncation/continuation glyphs even if only the left
3014 fringe is absent. */
3015 if (base_face_id == DEFAULT_FACE_ID
3016 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3017 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3019 if (it->line_wrap == TRUNCATE)
3020 it->last_visible_x -= it->truncation_pixel_width;
3021 else
3022 it->last_visible_x -= it->continuation_pixel_width;
3024 /* Note the paragraph direction that this buffer wants to
3025 use. */
3026 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3027 Qleft_to_right))
3028 it->paragraph_embedding = L2R;
3029 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3030 Qright_to_left))
3031 it->paragraph_embedding = R2L;
3032 else
3033 it->paragraph_embedding = NEUTRAL_DIR;
3034 bidi_unshelve_cache (NULL, false);
3035 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3036 &it->bidi_it);
3039 /* Compute faces etc. */
3040 reseat (it, it->current.pos, true);
3043 CHECK_IT (it);
3047 /* Initialize IT for the display of window W with window start POS. */
3049 void
3050 start_display (struct it *it, struct window *w, struct text_pos pos)
3052 struct glyph_row *row;
3053 bool first_vpos = window_wants_header_line (w);
3055 row = w->desired_matrix->rows + first_vpos;
3056 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3057 it->first_vpos = first_vpos;
3059 /* Don't reseat to previous visible line start if current start
3060 position is in a string or image. */
3061 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3063 int first_y = it->current_y;
3065 /* If window start is not at a line start, skip forward to POS to
3066 get the correct continuation lines width. */
3067 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3068 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3069 if (!start_at_line_beg_p)
3071 int new_x;
3073 reseat_at_previous_visible_line_start (it);
3074 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3076 new_x = it->current_x + it->pixel_width;
3078 /* If lines are continued, this line may end in the middle
3079 of a multi-glyph character (e.g. a control character
3080 displayed as \003, or in the middle of an overlay
3081 string). In this case move_it_to above will not have
3082 taken us to the start of the continuation line but to the
3083 end of the continued line. */
3084 if (it->current_x > 0
3085 && it->line_wrap != TRUNCATE /* Lines are continued. */
3086 && (/* And glyph doesn't fit on the line. */
3087 new_x > it->last_visible_x
3088 /* Or it fits exactly and we're on a window
3089 system frame. */
3090 || (new_x == it->last_visible_x
3091 && FRAME_WINDOW_P (it->f)
3092 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3093 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3094 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3096 if ((it->current.dpvec_index >= 0
3097 || it->current.overlay_string_index >= 0)
3098 /* If we are on a newline from a display vector or
3099 overlay string, then we are already at the end of
3100 a screen line; no need to go to the next line in
3101 that case, as this line is not really continued.
3102 (If we do go to the next line, C-e will not DTRT.) */
3103 && it->c != '\n')
3105 set_iterator_to_next (it, true);
3106 move_it_in_display_line_to (it, -1, -1, 0);
3109 it->continuation_lines_width += it->current_x;
3111 /* If the character at POS is displayed via a display
3112 vector, move_it_to above stops at the final glyph of
3113 IT->dpvec. To make the caller redisplay that character
3114 again (a.k.a. start at POS), we need to reset the
3115 dpvec_index to the beginning of IT->dpvec. */
3116 else if (it->current.dpvec_index >= 0)
3117 it->current.dpvec_index = 0;
3119 /* We're starting a new display line, not affected by the
3120 height of the continued line, so clear the appropriate
3121 fields in the iterator structure. */
3122 it->max_ascent = it->max_descent = 0;
3123 it->max_phys_ascent = it->max_phys_descent = 0;
3125 it->current_y = first_y;
3126 it->vpos = 0;
3127 it->current_x = it->hpos = 0;
3133 /* Return true if POS is a position in ellipses displayed for invisible
3134 text. W is the window we display, for text property lookup. */
3136 static bool
3137 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3139 Lisp_Object prop, window;
3140 bool ellipses_p = false;
3141 ptrdiff_t charpos = CHARPOS (pos->pos);
3143 /* If POS specifies a position in a display vector, this might
3144 be for an ellipsis displayed for invisible text. We won't
3145 get the iterator set up for delivering that ellipsis unless
3146 we make sure that it gets aware of the invisible text. */
3147 if (pos->dpvec_index >= 0
3148 && pos->overlay_string_index < 0
3149 && CHARPOS (pos->string_pos) < 0
3150 && charpos > BEGV
3151 && (XSETWINDOW (window, w),
3152 prop = Fget_char_property (make_number (charpos),
3153 Qinvisible, window),
3154 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3156 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3157 window);
3158 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3161 return ellipses_p;
3165 /* Initialize IT for stepping through current_buffer in window W,
3166 starting at position POS that includes overlay string and display
3167 vector/ control character translation position information. Value
3168 is false if there are overlay strings with newlines at POS. */
3170 static bool
3171 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3173 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3174 int i;
3175 bool overlay_strings_with_newlines = false;
3177 /* If POS specifies a position in a display vector, this might
3178 be for an ellipsis displayed for invisible text. We won't
3179 get the iterator set up for delivering that ellipsis unless
3180 we make sure that it gets aware of the invisible text. */
3181 if (in_ellipses_for_invisible_text_p (pos, w))
3183 --charpos;
3184 bytepos = 0;
3187 /* Keep in mind: the call to reseat in init_iterator skips invisible
3188 text, so we might end up at a position different from POS. This
3189 is only a problem when POS is a row start after a newline and an
3190 overlay starts there with an after-string, and the overlay has an
3191 invisible property. Since we don't skip invisible text in
3192 display_line and elsewhere immediately after consuming the
3193 newline before the row start, such a POS will not be in a string,
3194 but the call to init_iterator below will move us to the
3195 after-string. */
3196 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3198 /* This only scans the current chunk -- it should scan all chunks.
3199 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3200 to 16 in 22.1 to make this a lesser problem. */
3201 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3203 const char *s = SSDATA (it->overlay_strings[i]);
3204 const char *e = s + SBYTES (it->overlay_strings[i]);
3206 while (s < e && *s != '\n')
3207 ++s;
3209 if (s < e)
3211 overlay_strings_with_newlines = true;
3212 break;
3216 /* If position is within an overlay string, set up IT to the right
3217 overlay string. */
3218 if (pos->overlay_string_index >= 0)
3220 int relative_index;
3222 /* If the first overlay string happens to have a `display'
3223 property for an image, the iterator will be set up for that
3224 image, and we have to undo that setup first before we can
3225 correct the overlay string index. */
3226 if (it->method == GET_FROM_IMAGE)
3227 pop_it (it);
3229 /* We already have the first chunk of overlay strings in
3230 IT->overlay_strings. Load more until the one for
3231 pos->overlay_string_index is in IT->overlay_strings. */
3232 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3234 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3235 it->current.overlay_string_index = 0;
3236 while (n--)
3238 load_overlay_strings (it, 0);
3239 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3243 it->current.overlay_string_index = pos->overlay_string_index;
3244 relative_index = (it->current.overlay_string_index
3245 % OVERLAY_STRING_CHUNK_SIZE);
3246 it->string = it->overlay_strings[relative_index];
3247 eassert (STRINGP (it->string));
3248 it->current.string_pos = pos->string_pos;
3249 it->method = GET_FROM_STRING;
3250 it->end_charpos = SCHARS (it->string);
3251 /* Set up the bidi iterator for this overlay string. */
3252 if (it->bidi_p)
3254 it->bidi_it.string.lstring = it->string;
3255 it->bidi_it.string.s = NULL;
3256 it->bidi_it.string.schars = SCHARS (it->string);
3257 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3258 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3259 it->bidi_it.string.unibyte = !it->multibyte_p;
3260 it->bidi_it.w = it->w;
3261 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3262 FRAME_WINDOW_P (it->f), &it->bidi_it);
3264 /* Synchronize the state of the bidi iterator with
3265 pos->string_pos. For any string position other than
3266 zero, this will be done automagically when we resume
3267 iteration over the string and get_visually_first_element
3268 is called. But if string_pos is zero, and the string is
3269 to be reordered for display, we need to resync manually,
3270 since it could be that the iteration state recorded in
3271 pos ended at string_pos of 0 moving backwards in string. */
3272 if (CHARPOS (pos->string_pos) == 0)
3274 get_visually_first_element (it);
3275 if (IT_STRING_CHARPOS (*it) != 0)
3276 do {
3277 /* Paranoia. */
3278 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3279 bidi_move_to_visually_next (&it->bidi_it);
3280 } while (it->bidi_it.charpos != 0);
3282 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3283 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3287 if (CHARPOS (pos->string_pos) >= 0)
3289 /* Recorded position is not in an overlay string, but in another
3290 string. This can only be a string from a `display' property.
3291 IT should already be filled with that string. */
3292 it->current.string_pos = pos->string_pos;
3293 eassert (STRINGP (it->string));
3294 if (it->bidi_p)
3295 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3296 FRAME_WINDOW_P (it->f), &it->bidi_it);
3299 /* Restore position in display vector translations, control
3300 character translations or ellipses. */
3301 if (pos->dpvec_index >= 0)
3303 if (it->dpvec == NULL)
3304 get_next_display_element (it);
3305 eassert (it->dpvec && it->current.dpvec_index == 0);
3306 it->current.dpvec_index = pos->dpvec_index;
3309 CHECK_IT (it);
3310 return !overlay_strings_with_newlines;
3314 /* Initialize IT for stepping through current_buffer in window W
3315 starting at ROW->start. */
3317 static void
3318 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3320 init_from_display_pos (it, w, &row->start);
3321 it->start = row->start;
3322 it->continuation_lines_width = row->continuation_lines_width;
3323 CHECK_IT (it);
3327 /* Initialize IT for stepping through current_buffer in window W
3328 starting in the line following ROW, i.e. starting at ROW->end.
3329 Value is false if there are overlay strings with newlines at ROW's
3330 end position. */
3332 static bool
3333 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3335 bool success = false;
3337 if (init_from_display_pos (it, w, &row->end))
3339 if (row->continued_p)
3340 it->continuation_lines_width
3341 = row->continuation_lines_width + row->pixel_width;
3342 CHECK_IT (it);
3343 success = true;
3346 return success;
3352 /***********************************************************************
3353 Text properties
3354 ***********************************************************************/
3356 /* Called when IT reaches IT->stop_charpos. Handle text property and
3357 overlay changes. Set IT->stop_charpos to the next position where
3358 to stop. */
3360 static void
3361 handle_stop (struct it *it)
3363 enum prop_handled handled;
3364 bool handle_overlay_change_p;
3365 struct props *p;
3367 it->dpvec = NULL;
3368 it->current.dpvec_index = -1;
3369 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3370 it->ellipsis_p = false;
3372 /* Use face of preceding text for ellipsis (if invisible) */
3373 if (it->selective_display_ellipsis_p)
3374 it->saved_face_id = it->face_id;
3376 /* Here's the description of the semantics of, and the logic behind,
3377 the various HANDLED_* statuses:
3379 HANDLED_NORMALLY means the handler did its job, and the loop
3380 should proceed to calling the next handler in order.
3382 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3383 change in the properties and overlays at current position, so the
3384 loop should be restarted, to re-invoke the handlers that were
3385 already called. This happens when fontification-functions were
3386 called by handle_fontified_prop, and actually fontified
3387 something. Another case where HANDLED_RECOMPUTE_PROPS is
3388 returned is when we discover overlay strings that need to be
3389 displayed right away. The loop below will continue for as long
3390 as the status is HANDLED_RECOMPUTE_PROPS.
3392 HANDLED_RETURN means return immediately to the caller, to
3393 continue iteration without calling any further handlers. This is
3394 used when we need to act on some property right away, for example
3395 when we need to display the ellipsis or a replacing display
3396 property, such as display string or image.
3398 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3399 consumed, and the handler switched to the next overlay string.
3400 This signals the loop below to refrain from looking for more
3401 overlays before all the overlay strings of the current overlay
3402 are processed.
3404 Some of the handlers called by the loop push the iterator state
3405 onto the stack (see 'push_it'), and arrange for the iteration to
3406 continue with another object, such as an image, a display string,
3407 or an overlay string. In most such cases, it->stop_charpos is
3408 set to the first character of the string, so that when the
3409 iteration resumes, this function will immediately be called
3410 again, to examine the properties at the beginning of the string.
3412 When a display or overlay string is exhausted, the iterator state
3413 is popped (see 'pop_it'), and iteration continues with the
3414 previous object. Again, in many such cases this function is
3415 called again to find the next position where properties might
3416 change. */
3420 handled = HANDLED_NORMALLY;
3422 /* Call text property handlers. */
3423 for (p = it_props; p->handler; ++p)
3425 handled = p->handler (it);
3427 if (handled == HANDLED_RECOMPUTE_PROPS)
3428 break;
3429 else if (handled == HANDLED_RETURN)
3431 /* We still want to show before and after strings from
3432 overlays even if the actual buffer text is replaced. */
3433 if (!handle_overlay_change_p
3434 || it->sp > 1
3435 /* Don't call get_overlay_strings_1 if we already
3436 have overlay strings loaded, because doing so
3437 will load them again and push the iterator state
3438 onto the stack one more time, which is not
3439 expected by the rest of the code that processes
3440 overlay strings. */
3441 || (it->current.overlay_string_index < 0
3442 && !get_overlay_strings_1 (it, 0, false)))
3444 if (it->ellipsis_p)
3445 setup_for_ellipsis (it, 0);
3446 /* When handling a display spec, we might load an
3447 empty string. In that case, discard it here. We
3448 used to discard it in handle_single_display_spec,
3449 but that causes get_overlay_strings_1, above, to
3450 ignore overlay strings that we must check. */
3451 if (STRINGP (it->string) && !SCHARS (it->string))
3452 pop_it (it);
3453 return;
3455 else if (STRINGP (it->string) && !SCHARS (it->string))
3456 pop_it (it);
3457 else
3459 it->string_from_display_prop_p = false;
3460 it->from_disp_prop_p = false;
3461 handle_overlay_change_p = false;
3463 handled = HANDLED_RECOMPUTE_PROPS;
3464 break;
3466 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3467 handle_overlay_change_p = false;
3470 if (handled != HANDLED_RECOMPUTE_PROPS)
3472 /* Don't check for overlay strings below when set to deliver
3473 characters from a display vector. */
3474 if (it->method == GET_FROM_DISPLAY_VECTOR)
3475 handle_overlay_change_p = false;
3477 /* Handle overlay changes.
3478 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3479 if it finds overlays. */
3480 if (handle_overlay_change_p)
3481 handled = handle_overlay_change (it);
3484 if (it->ellipsis_p)
3486 setup_for_ellipsis (it, 0);
3487 break;
3490 while (handled == HANDLED_RECOMPUTE_PROPS);
3492 /* Determine where to stop next. */
3493 if (handled == HANDLED_NORMALLY)
3494 compute_stop_pos (it);
3498 /* Compute IT->stop_charpos from text property and overlay change
3499 information for IT's current position. */
3501 static void
3502 compute_stop_pos (struct it *it)
3504 register INTERVAL iv, next_iv;
3505 Lisp_Object object, limit, position;
3506 ptrdiff_t charpos, bytepos;
3508 if (STRINGP (it->string))
3510 /* Strings are usually short, so don't limit the search for
3511 properties. */
3512 it->stop_charpos = it->end_charpos;
3513 object = it->string;
3514 limit = Qnil;
3515 charpos = IT_STRING_CHARPOS (*it);
3516 bytepos = IT_STRING_BYTEPOS (*it);
3518 else
3520 ptrdiff_t pos;
3522 /* If end_charpos is out of range for some reason, such as a
3523 misbehaving display function, rationalize it (Bug#5984). */
3524 if (it->end_charpos > ZV)
3525 it->end_charpos = ZV;
3526 it->stop_charpos = it->end_charpos;
3528 /* If next overlay change is in front of the current stop pos
3529 (which is IT->end_charpos), stop there. Note: value of
3530 next_overlay_change is point-max if no overlay change
3531 follows. */
3532 charpos = IT_CHARPOS (*it);
3533 bytepos = IT_BYTEPOS (*it);
3534 pos = next_overlay_change (charpos);
3535 if (pos < it->stop_charpos)
3536 it->stop_charpos = pos;
3538 /* Set up variables for computing the stop position from text
3539 property changes. */
3540 XSETBUFFER (object, current_buffer);
3541 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3544 /* Get the interval containing IT's position. Value is a null
3545 interval if there isn't such an interval. */
3546 position = make_number (charpos);
3547 iv = validate_interval_range (object, &position, &position, false);
3548 if (iv)
3550 Lisp_Object values_here[LAST_PROP_IDX];
3551 struct props *p;
3553 /* Get properties here. */
3554 for (p = it_props; p->handler; ++p)
3555 values_here[p->idx] = textget (iv->plist,
3556 builtin_lisp_symbol (p->name));
3558 /* Look for an interval following iv that has different
3559 properties. */
3560 for (next_iv = next_interval (iv);
3561 (next_iv
3562 && (NILP (limit)
3563 || XFASTINT (limit) > next_iv->position));
3564 next_iv = next_interval (next_iv))
3566 for (p = it_props; p->handler; ++p)
3568 Lisp_Object new_value = textget (next_iv->plist,
3569 builtin_lisp_symbol (p->name));
3570 if (!EQ (values_here[p->idx], new_value))
3571 break;
3574 if (p->handler)
3575 break;
3578 if (next_iv)
3580 if (INTEGERP (limit)
3581 && next_iv->position >= XFASTINT (limit))
3582 /* No text property change up to limit. */
3583 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3584 else
3585 /* Text properties change in next_iv. */
3586 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3590 if (it->cmp_it.id < 0)
3592 ptrdiff_t stoppos = it->end_charpos;
3594 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3595 stoppos = -1;
3596 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3597 stoppos, it->string);
3600 eassert (STRINGP (it->string)
3601 || (it->stop_charpos >= BEGV
3602 && it->stop_charpos >= IT_CHARPOS (*it)));
3606 /* Return the position of the next overlay change after POS in
3607 current_buffer. Value is point-max if no overlay change
3608 follows. This is like `next-overlay-change' but doesn't use
3609 xmalloc. */
3611 static ptrdiff_t
3612 next_overlay_change (ptrdiff_t pos)
3614 ptrdiff_t i, noverlays;
3615 ptrdiff_t endpos;
3616 Lisp_Object *overlays;
3617 USE_SAFE_ALLOCA;
3619 /* Get all overlays at the given position. */
3620 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3622 /* If any of these overlays ends before endpos,
3623 use its ending point instead. */
3624 for (i = 0; i < noverlays; ++i)
3626 Lisp_Object oend;
3627 ptrdiff_t oendpos;
3629 oend = OVERLAY_END (overlays[i]);
3630 oendpos = OVERLAY_POSITION (oend);
3631 endpos = min (endpos, oendpos);
3634 SAFE_FREE ();
3635 return endpos;
3638 /* How many characters forward to search for a display property or
3639 display string. Searching too far forward makes the bidi display
3640 sluggish, especially in small windows. */
3641 #define MAX_DISP_SCAN 250
3643 /* Return the character position of a display string at or after
3644 position specified by POSITION. If no display string exists at or
3645 after POSITION, return ZV. A display string is either an overlay
3646 with `display' property whose value is a string, or a `display'
3647 text property whose value is a string. STRING is data about the
3648 string to iterate; if STRING->lstring is nil, we are iterating a
3649 buffer. FRAME_WINDOW_P is true when we are displaying a window
3650 on a GUI frame. DISP_PROP is set to zero if we searched
3651 MAX_DISP_SCAN characters forward without finding any display
3652 strings, non-zero otherwise. It is set to 2 if the display string
3653 uses any kind of `(space ...)' spec that will produce a stretch of
3654 white space in the text area. */
3655 ptrdiff_t
3656 compute_display_string_pos (struct text_pos *position,
3657 struct bidi_string_data *string,
3658 struct window *w,
3659 bool frame_window_p, int *disp_prop)
3661 /* OBJECT = nil means current buffer. */
3662 Lisp_Object object, object1;
3663 Lisp_Object pos, spec, limpos;
3664 bool string_p = string && (STRINGP (string->lstring) || string->s);
3665 ptrdiff_t eob = string_p ? string->schars : ZV;
3666 ptrdiff_t begb = string_p ? 0 : BEGV;
3667 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3668 ptrdiff_t lim =
3669 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3670 struct text_pos tpos;
3671 int rv = 0;
3673 if (string && STRINGP (string->lstring))
3674 object1 = object = string->lstring;
3675 else if (w && !string_p)
3677 XSETWINDOW (object, w);
3678 object1 = Qnil;
3680 else
3681 object1 = object = Qnil;
3683 *disp_prop = 1;
3685 if (charpos >= eob
3686 /* We don't support display properties whose values are strings
3687 that have display string properties. */
3688 || string->from_disp_str
3689 /* C strings cannot have display properties. */
3690 || (string->s && !STRINGP (object)))
3692 *disp_prop = 0;
3693 return eob;
3696 /* If the character at CHARPOS is where the display string begins,
3697 return CHARPOS. */
3698 pos = make_number (charpos);
3699 if (STRINGP (object))
3700 bufpos = string->bufpos;
3701 else
3702 bufpos = charpos;
3703 tpos = *position;
3704 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3705 && (charpos <= begb
3706 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3707 object),
3708 spec))
3709 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3710 frame_window_p)))
3712 if (rv == 2)
3713 *disp_prop = 2;
3714 return charpos;
3717 /* Look forward for the first character with a `display' property
3718 that will replace the underlying text when displayed. */
3719 limpos = make_number (lim);
3720 do {
3721 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3722 CHARPOS (tpos) = XFASTINT (pos);
3723 if (CHARPOS (tpos) >= lim)
3725 *disp_prop = 0;
3726 break;
3728 if (STRINGP (object))
3729 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3730 else
3731 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3732 spec = Fget_char_property (pos, Qdisplay, object);
3733 if (!STRINGP (object))
3734 bufpos = CHARPOS (tpos);
3735 } while (NILP (spec)
3736 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3737 bufpos, frame_window_p)));
3738 if (rv == 2)
3739 *disp_prop = 2;
3741 return CHARPOS (tpos);
3744 /* Return the character position of the end of the display string that
3745 started at CHARPOS. If there's no display string at CHARPOS,
3746 return -1. A display string is either an overlay with `display'
3747 property whose value is a string or a `display' text property whose
3748 value is a string. */
3749 ptrdiff_t
3750 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3752 /* OBJECT = nil means current buffer. */
3753 Lisp_Object object =
3754 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3755 Lisp_Object pos = make_number (charpos);
3756 ptrdiff_t eob =
3757 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3759 if (charpos >= eob || (string->s && !STRINGP (object)))
3760 return eob;
3762 /* It could happen that the display property or overlay was removed
3763 since we found it in compute_display_string_pos above. One way
3764 this can happen is if JIT font-lock was called (through
3765 handle_fontified_prop), and jit-lock-functions remove text
3766 properties or overlays from the portion of buffer that includes
3767 CHARPOS. Muse mode is known to do that, for example. In this
3768 case, we return -1 to the caller, to signal that no display
3769 string is actually present at CHARPOS. See bidi_fetch_char for
3770 how this is handled.
3772 An alternative would be to never look for display properties past
3773 it->stop_charpos. But neither compute_display_string_pos nor
3774 bidi_fetch_char that calls it know or care where the next
3775 stop_charpos is. */
3776 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3777 return -1;
3779 /* Look forward for the first character where the `display' property
3780 changes. */
3781 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3783 return XFASTINT (pos);
3788 /***********************************************************************
3789 Fontification
3790 ***********************************************************************/
3792 /* Handle changes in the `fontified' property of the current buffer by
3793 calling hook functions from Qfontification_functions to fontify
3794 regions of text. */
3796 static enum prop_handled
3797 handle_fontified_prop (struct it *it)
3799 Lisp_Object prop, pos;
3800 enum prop_handled handled = HANDLED_NORMALLY;
3802 if (!NILP (Vmemory_full))
3803 return handled;
3805 /* Get the value of the `fontified' property at IT's current buffer
3806 position. (The `fontified' property doesn't have a special
3807 meaning in strings.) If the value is nil, call functions from
3808 Qfontification_functions. */
3809 if (!STRINGP (it->string)
3810 && it->s == NULL
3811 && !NILP (Vfontification_functions)
3812 && !NILP (Vrun_hooks)
3813 && (pos = make_number (IT_CHARPOS (*it)),
3814 prop = Fget_char_property (pos, Qfontified, Qnil),
3815 /* Ignore the special cased nil value always present at EOB since
3816 no amount of fontifying will be able to change it. */
3817 NILP (prop) && IT_CHARPOS (*it) < Z))
3819 ptrdiff_t count = SPECPDL_INDEX ();
3820 Lisp_Object val;
3821 struct buffer *obuf = current_buffer;
3822 ptrdiff_t begv = BEGV, zv = ZV;
3823 bool old_clip_changed = current_buffer->clip_changed;
3825 val = Vfontification_functions;
3826 specbind (Qfontification_functions, Qnil);
3828 eassert (it->end_charpos == ZV);
3830 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3831 safe_call1 (val, pos);
3832 else
3834 Lisp_Object fns, fn;
3836 fns = Qnil;
3838 for (; CONSP (val); val = XCDR (val))
3840 fn = XCAR (val);
3842 if (EQ (fn, Qt))
3844 /* A value of t indicates this hook has a local
3845 binding; it means to run the global binding too.
3846 In a global value, t should not occur. If it
3847 does, we must ignore it to avoid an endless
3848 loop. */
3849 for (fns = Fdefault_value (Qfontification_functions);
3850 CONSP (fns);
3851 fns = XCDR (fns))
3853 fn = XCAR (fns);
3854 if (!EQ (fn, Qt))
3855 safe_call1 (fn, pos);
3858 else
3859 safe_call1 (fn, pos);
3863 unbind_to (count, Qnil);
3865 /* Fontification functions routinely call `save-restriction'.
3866 Normally, this tags clip_changed, which can confuse redisplay
3867 (see discussion in Bug#6671). Since we don't perform any
3868 special handling of fontification changes in the case where
3869 `save-restriction' isn't called, there's no point doing so in
3870 this case either. So, if the buffer's restrictions are
3871 actually left unchanged, reset clip_changed. */
3872 if (obuf == current_buffer)
3874 if (begv == BEGV && zv == ZV)
3875 current_buffer->clip_changed = old_clip_changed;
3877 /* There isn't much we can reasonably do to protect against
3878 misbehaving fontification, but here's a fig leaf. */
3879 else if (BUFFER_LIVE_P (obuf))
3880 set_buffer_internal_1 (obuf);
3882 /* The fontification code may have added/removed text.
3883 It could do even a lot worse, but let's at least protect against
3884 the most obvious case where only the text past `pos' gets changed',
3885 as is/was done in grep.el where some escapes sequences are turned
3886 into face properties (bug#7876). */
3887 it->end_charpos = ZV;
3889 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3890 something. This avoids an endless loop if they failed to
3891 fontify the text for which reason ever. */
3892 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3893 handled = HANDLED_RECOMPUTE_PROPS;
3896 return handled;
3901 /***********************************************************************
3902 Faces
3903 ***********************************************************************/
3905 /* Set up iterator IT from face properties at its current position.
3906 Called from handle_stop. */
3908 static enum prop_handled
3909 handle_face_prop (struct it *it)
3911 int new_face_id;
3912 ptrdiff_t next_stop;
3914 if (!STRINGP (it->string))
3916 new_face_id
3917 = face_at_buffer_position (it->w,
3918 IT_CHARPOS (*it),
3919 &next_stop,
3920 (IT_CHARPOS (*it)
3921 + TEXT_PROP_DISTANCE_LIMIT),
3922 false, it->base_face_id);
3924 /* Is this a start of a run of characters with box face?
3925 Caveat: this can be called for a freshly initialized
3926 iterator; face_id is -1 in this case. We know that the new
3927 face will not change until limit, i.e. if the new face has a
3928 box, all characters up to limit will have one. But, as
3929 usual, we don't know whether limit is really the end. */
3930 if (new_face_id != it->face_id)
3932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3933 /* If it->face_id is -1, old_face below will be NULL, see
3934 the definition of FACE_FROM_ID_OR_NULL. This will happen
3935 if this is the initial call that gets the face. */
3936 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3938 /* If the value of face_id of the iterator is -1, we have to
3939 look in front of IT's position and see whether there is a
3940 face there that's different from new_face_id. */
3941 if (!old_face && IT_CHARPOS (*it) > BEG)
3943 int prev_face_id = face_before_it_pos (it);
3945 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3948 /* If the new face has a box, but the old face does not,
3949 this is the start of a run of characters with box face,
3950 i.e. this character has a shadow on the left side. */
3951 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3952 && (old_face == NULL || !old_face->box));
3953 it->face_box_p = new_face->box != FACE_NO_BOX;
3956 else
3958 int base_face_id;
3959 ptrdiff_t bufpos;
3960 int i;
3961 Lisp_Object from_overlay
3962 = (it->current.overlay_string_index >= 0
3963 ? it->string_overlays[it->current.overlay_string_index
3964 % OVERLAY_STRING_CHUNK_SIZE]
3965 : Qnil);
3967 /* See if we got to this string directly or indirectly from
3968 an overlay property. That includes the before-string or
3969 after-string of an overlay, strings in display properties
3970 provided by an overlay, their text properties, etc.
3972 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3973 if (! NILP (from_overlay))
3974 for (i = it->sp - 1; i >= 0; i--)
3976 if (it->stack[i].current.overlay_string_index >= 0)
3977 from_overlay
3978 = it->string_overlays[it->stack[i].current.overlay_string_index
3979 % OVERLAY_STRING_CHUNK_SIZE];
3980 else if (! NILP (it->stack[i].from_overlay))
3981 from_overlay = it->stack[i].from_overlay;
3983 if (!NILP (from_overlay))
3984 break;
3987 if (! NILP (from_overlay))
3989 bufpos = IT_CHARPOS (*it);
3990 /* For a string from an overlay, the base face depends
3991 only on text properties and ignores overlays. */
3992 base_face_id
3993 = face_for_overlay_string (it->w,
3994 IT_CHARPOS (*it),
3995 &next_stop,
3996 (IT_CHARPOS (*it)
3997 + TEXT_PROP_DISTANCE_LIMIT),
3998 false,
3999 from_overlay);
4001 else
4003 bufpos = 0;
4005 /* For strings from a `display' property, use the face at
4006 IT's current buffer position as the base face to merge
4007 with, so that overlay strings appear in the same face as
4008 surrounding text, unless they specify their own faces.
4009 For strings from wrap-prefix and line-prefix properties,
4010 use the default face, possibly remapped via
4011 Vface_remapping_alist. */
4012 /* Note that the fact that we use the face at _buffer_
4013 position means that a 'display' property on an overlay
4014 string will not inherit the face of that overlay string,
4015 but will instead revert to the face of buffer text
4016 covered by the overlay. This is visible, e.g., when the
4017 overlay specifies a box face, but neither the buffer nor
4018 the display string do. This sounds like a design bug,
4019 but Emacs always did that since v21.1, so changing that
4020 might be a big deal. */
4021 base_face_id = it->string_from_prefix_prop_p
4022 ? (!NILP (Vface_remapping_alist)
4023 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4024 : DEFAULT_FACE_ID)
4025 : underlying_face_id (it);
4028 new_face_id = face_at_string_position (it->w,
4029 it->string,
4030 IT_STRING_CHARPOS (*it),
4031 bufpos,
4032 &next_stop,
4033 base_face_id, false);
4035 /* Is this a start of a run of characters with box? Caveat:
4036 this can be called for a freshly allocated iterator; face_id
4037 is -1 is this case. We know that the new face will not
4038 change until the next check pos, i.e. if the new face has a
4039 box, all characters up to that position will have a
4040 box. But, as usual, we don't know whether that position
4041 is really the end. */
4042 if (new_face_id != it->face_id)
4044 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4045 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4047 /* If new face has a box but old face hasn't, this is the
4048 start of a run of characters with box, i.e. it has a
4049 shadow on the left side. */
4050 it->start_of_box_run_p
4051 = new_face->box && (old_face == NULL || !old_face->box);
4052 it->face_box_p = new_face->box != FACE_NO_BOX;
4056 it->face_id = new_face_id;
4057 return HANDLED_NORMALLY;
4061 /* Return the ID of the face ``underlying'' IT's current position,
4062 which is in a string. If the iterator is associated with a
4063 buffer, return the face at IT's current buffer position.
4064 Otherwise, use the iterator's base_face_id. */
4066 static int
4067 underlying_face_id (struct it *it)
4069 int face_id = it->base_face_id, i;
4071 eassert (STRINGP (it->string));
4073 for (i = it->sp - 1; i >= 0; --i)
4074 if (NILP (it->stack[i].string))
4075 face_id = it->stack[i].face_id;
4077 return face_id;
4081 /* Compute the face one character before or after the current position
4082 of IT, in the visual order. BEFORE_P means get the face
4083 in front (to the left in L2R paragraphs, to the right in R2L
4084 paragraphs) of IT's screen position. Value is the ID of the face. */
4086 static int
4087 face_before_or_after_it_pos (struct it *it, bool before_p)
4089 int face_id, limit;
4090 ptrdiff_t next_check_charpos;
4091 struct it it_copy;
4092 void *it_copy_data = NULL;
4094 eassert (it->s == NULL);
4096 if (STRINGP (it->string))
4098 ptrdiff_t bufpos, charpos;
4099 int base_face_id;
4101 /* No face change past the end of the string (for the case
4102 we are padding with spaces). No face change before the
4103 string start. */
4104 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4105 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4106 return it->face_id;
4108 if (!it->bidi_p)
4110 /* Set charpos to the position before or after IT's current
4111 position, in the logical order, which in the non-bidi
4112 case is the same as the visual order. */
4113 if (before_p)
4114 charpos = IT_STRING_CHARPOS (*it) - 1;
4115 else if (it->what == IT_COMPOSITION)
4116 /* For composition, we must check the character after the
4117 composition. */
4118 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4119 else
4120 charpos = IT_STRING_CHARPOS (*it) + 1;
4122 else
4124 if (before_p)
4126 /* With bidi iteration, the character before the current
4127 in the visual order cannot be found by simple
4128 iteration, because "reverse" reordering is not
4129 supported. Instead, we need to start from the string
4130 beginning and go all the way to the current string
4131 position, remembering the previous position. */
4132 /* Ignore face changes before the first visible
4133 character on this display line. */
4134 if (it->current_x <= it->first_visible_x)
4135 return it->face_id;
4136 SAVE_IT (it_copy, *it, it_copy_data);
4137 IT_STRING_CHARPOS (it_copy) = 0;
4138 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4142 charpos = IT_STRING_CHARPOS (it_copy);
4143 if (charpos >= SCHARS (it->string))
4144 break;
4145 bidi_move_to_visually_next (&it_copy.bidi_it);
4147 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4149 RESTORE_IT (it, it, it_copy_data);
4151 else
4153 /* Set charpos to the string position of the character
4154 that comes after IT's current position in the visual
4155 order. */
4156 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4158 it_copy = *it;
4159 while (n--)
4160 bidi_move_to_visually_next (&it_copy.bidi_it);
4162 charpos = it_copy.bidi_it.charpos;
4165 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4167 if (it->current.overlay_string_index >= 0)
4168 bufpos = IT_CHARPOS (*it);
4169 else
4170 bufpos = 0;
4172 base_face_id = underlying_face_id (it);
4174 /* Get the face for ASCII, or unibyte. */
4175 face_id = face_at_string_position (it->w,
4176 it->string,
4177 charpos,
4178 bufpos,
4179 &next_check_charpos,
4180 base_face_id, false);
4182 /* Correct the face for charsets different from ASCII. Do it
4183 for the multibyte case only. The face returned above is
4184 suitable for unibyte text if IT->string is unibyte. */
4185 if (STRING_MULTIBYTE (it->string))
4187 struct text_pos pos1 = string_pos (charpos, it->string);
4188 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4189 int c, len;
4190 struct face *face = FACE_FROM_ID (it->f, face_id);
4192 c = string_char_and_length (p, &len);
4193 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4196 else
4198 struct text_pos pos;
4200 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4201 || (IT_CHARPOS (*it) <= BEGV && before_p))
4202 return it->face_id;
4204 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4205 pos = it->current.pos;
4207 if (!it->bidi_p)
4209 if (before_p)
4210 DEC_TEXT_POS (pos, it->multibyte_p);
4211 else
4213 if (it->what == IT_COMPOSITION)
4215 /* For composition, we must check the position after
4216 the composition. */
4217 pos.charpos += it->cmp_it.nchars;
4218 pos.bytepos += it->len;
4220 else
4221 INC_TEXT_POS (pos, it->multibyte_p);
4224 else
4226 if (before_p)
4228 int current_x;
4230 /* With bidi iteration, the character before the current
4231 in the visual order cannot be found by simple
4232 iteration, because "reverse" reordering is not
4233 supported. Instead, we need to use the move_it_*
4234 family of functions, and move to the previous
4235 character starting from the beginning of the visual
4236 line. */
4237 /* Ignore face changes before the first visible
4238 character on this display line. */
4239 if (it->current_x <= it->first_visible_x)
4240 return it->face_id;
4241 SAVE_IT (it_copy, *it, it_copy_data);
4242 /* Implementation note: Since move_it_in_display_line
4243 works in the iterator geometry, and thinks the first
4244 character is always the leftmost, even in R2L lines,
4245 we don't need to distinguish between the R2L and L2R
4246 cases here. */
4247 current_x = it_copy.current_x;
4248 move_it_vertically_backward (&it_copy, 0);
4249 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4250 pos = it_copy.current.pos;
4251 RESTORE_IT (it, it, it_copy_data);
4253 else
4255 /* Set charpos to the buffer position of the character
4256 that comes after IT's current position in the visual
4257 order. */
4258 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4260 it_copy = *it;
4261 while (n--)
4262 bidi_move_to_visually_next (&it_copy.bidi_it);
4264 SET_TEXT_POS (pos,
4265 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4268 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4270 /* Determine face for CHARSET_ASCII, or unibyte. */
4271 face_id = face_at_buffer_position (it->w,
4272 CHARPOS (pos),
4273 &next_check_charpos,
4274 limit, false, -1);
4276 /* Correct the face for charsets different from ASCII. Do it
4277 for the multibyte case only. The face returned above is
4278 suitable for unibyte text if current_buffer is unibyte. */
4279 if (it->multibyte_p)
4281 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4282 struct face *face = FACE_FROM_ID (it->f, face_id);
4283 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4287 return face_id;
4292 /***********************************************************************
4293 Invisible text
4294 ***********************************************************************/
4296 /* Set up iterator IT from invisible properties at its current
4297 position. Called from handle_stop. */
4299 static enum prop_handled
4300 handle_invisible_prop (struct it *it)
4302 enum prop_handled handled = HANDLED_NORMALLY;
4303 int invis;
4304 Lisp_Object prop;
4306 if (STRINGP (it->string))
4308 Lisp_Object end_charpos, limit;
4310 /* Get the value of the invisible text property at the
4311 current position. Value will be nil if there is no such
4312 property. */
4313 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4314 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4315 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4317 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4319 /* Record whether we have to display an ellipsis for the
4320 invisible text. */
4321 bool display_ellipsis_p = (invis == 2);
4322 ptrdiff_t len, endpos;
4324 handled = HANDLED_RECOMPUTE_PROPS;
4326 /* Get the position at which the next visible text can be
4327 found in IT->string, if any. */
4328 endpos = len = SCHARS (it->string);
4329 XSETINT (limit, len);
4332 end_charpos
4333 = Fnext_single_property_change (end_charpos, Qinvisible,
4334 it->string, limit);
4335 /* Since LIMIT is always an integer, so should be the
4336 value returned by Fnext_single_property_change. */
4337 eassert (INTEGERP (end_charpos));
4338 if (INTEGERP (end_charpos))
4340 endpos = XFASTINT (end_charpos);
4341 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4342 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4343 if (invis == 2)
4344 display_ellipsis_p = true;
4346 else /* Should never happen; but if it does, exit the loop. */
4347 endpos = len;
4349 while (invis != 0 && endpos < len);
4351 if (display_ellipsis_p)
4352 it->ellipsis_p = true;
4354 if (endpos < len)
4356 /* Text at END_CHARPOS is visible. Move IT there. */
4357 struct text_pos old;
4358 ptrdiff_t oldpos;
4360 old = it->current.string_pos;
4361 oldpos = CHARPOS (old);
4362 if (it->bidi_p)
4364 if (it->bidi_it.first_elt
4365 && it->bidi_it.charpos < SCHARS (it->string))
4366 bidi_paragraph_init (it->paragraph_embedding,
4367 &it->bidi_it, true);
4368 /* Bidi-iterate out of the invisible text. */
4371 bidi_move_to_visually_next (&it->bidi_it);
4373 while (oldpos <= it->bidi_it.charpos
4374 && it->bidi_it.charpos < endpos
4375 && it->bidi_it.charpos < it->bidi_it.string.schars);
4377 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4378 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4379 if (IT_CHARPOS (*it) >= endpos)
4380 it->prev_stop = endpos;
4382 else
4384 IT_STRING_CHARPOS (*it) = endpos;
4385 compute_string_pos (&it->current.string_pos, old, it->string);
4388 else
4390 /* The rest of the string is invisible. If this is an
4391 overlay string, proceed with the next overlay string
4392 or whatever comes and return a character from there. */
4393 if (it->current.overlay_string_index >= 0
4394 && !display_ellipsis_p)
4396 next_overlay_string (it);
4397 /* Don't check for overlay strings when we just
4398 finished processing them. */
4399 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4401 else
4403 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4404 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4409 else
4411 ptrdiff_t newpos, next_stop, start_charpos, tem;
4412 Lisp_Object pos, overlay;
4414 /* First of all, is there invisible text at this position? */
4415 tem = start_charpos = IT_CHARPOS (*it);
4416 pos = make_number (tem);
4417 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4418 &overlay);
4419 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4421 /* If we are on invisible text, skip over it. */
4422 if (invis != 0 && start_charpos < it->end_charpos)
4424 /* Record whether we have to display an ellipsis for the
4425 invisible text. */
4426 bool display_ellipsis_p = invis == 2;
4428 handled = HANDLED_RECOMPUTE_PROPS;
4430 /* Loop skipping over invisible text. The loop is left at
4431 ZV or with IT on the first char being visible again. */
4434 /* Try to skip some invisible text. Return value is the
4435 position reached which can be equal to where we start
4436 if there is nothing invisible there. This skips both
4437 over invisible text properties and overlays with
4438 invisible property. */
4439 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4441 /* If we skipped nothing at all we weren't at invisible
4442 text in the first place. If everything to the end of
4443 the buffer was skipped, end the loop. */
4444 if (newpos == tem || newpos >= ZV)
4445 invis = 0;
4446 else
4448 /* We skipped some characters but not necessarily
4449 all there are. Check if we ended up on visible
4450 text. Fget_char_property returns the property of
4451 the char before the given position, i.e. if we
4452 get invis = 0, this means that the char at
4453 newpos is visible. */
4454 pos = make_number (newpos);
4455 prop = Fget_char_property (pos, Qinvisible, it->window);
4456 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4459 /* If we ended up on invisible text, proceed to
4460 skip starting with next_stop. */
4461 if (invis != 0)
4462 tem = next_stop;
4464 /* If there are adjacent invisible texts, don't lose the
4465 second one's ellipsis. */
4466 if (invis == 2)
4467 display_ellipsis_p = true;
4469 while (invis != 0);
4471 /* The position newpos is now either ZV or on visible text. */
4472 if (it->bidi_p)
4474 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4475 bool on_newline
4476 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4477 bool after_newline
4478 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4480 /* If the invisible text ends on a newline or on a
4481 character after a newline, we can avoid the costly,
4482 character by character, bidi iteration to NEWPOS, and
4483 instead simply reseat the iterator there. That's
4484 because all bidi reordering information is tossed at
4485 the newline. This is a big win for modes that hide
4486 complete lines, like Outline, Org, etc. */
4487 if (on_newline || after_newline)
4489 struct text_pos tpos;
4490 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4492 SET_TEXT_POS (tpos, newpos, bpos);
4493 reseat_1 (it, tpos, false);
4494 /* If we reseat on a newline/ZV, we need to prep the
4495 bidi iterator for advancing to the next character
4496 after the newline/EOB, keeping the current paragraph
4497 direction (so that PRODUCE_GLYPHS does TRT wrt
4498 prepending/appending glyphs to a glyph row). */
4499 if (on_newline)
4501 it->bidi_it.first_elt = false;
4502 it->bidi_it.paragraph_dir = pdir;
4503 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4504 it->bidi_it.nchars = 1;
4505 it->bidi_it.ch_len = 1;
4508 else /* Must use the slow method. */
4510 /* With bidi iteration, the region of invisible text
4511 could start and/or end in the middle of a
4512 non-base embedding level. Therefore, we need to
4513 skip invisible text using the bidi iterator,
4514 starting at IT's current position, until we find
4515 ourselves outside of the invisible text.
4516 Skipping invisible text _after_ bidi iteration
4517 avoids affecting the visual order of the
4518 displayed text when invisible properties are
4519 added or removed. */
4520 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4522 /* If we were `reseat'ed to a new paragraph,
4523 determine the paragraph base direction. We
4524 need to do it now because
4525 next_element_from_buffer may not have a
4526 chance to do it, if we are going to skip any
4527 text at the beginning, which resets the
4528 FIRST_ELT flag. */
4529 bidi_paragraph_init (it->paragraph_embedding,
4530 &it->bidi_it, true);
4534 bidi_move_to_visually_next (&it->bidi_it);
4536 while (it->stop_charpos <= it->bidi_it.charpos
4537 && it->bidi_it.charpos < newpos);
4538 IT_CHARPOS (*it) = it->bidi_it.charpos;
4539 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4540 /* If we overstepped NEWPOS, record its position in
4541 the iterator, so that we skip invisible text if
4542 later the bidi iteration lands us in the
4543 invisible region again. */
4544 if (IT_CHARPOS (*it) >= newpos)
4545 it->prev_stop = newpos;
4548 else
4550 IT_CHARPOS (*it) = newpos;
4551 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4554 if (display_ellipsis_p)
4556 /* Make sure that the glyphs of the ellipsis will get
4557 correct `charpos' values. If we would not update
4558 it->position here, the glyphs would belong to the
4559 last visible character _before_ the invisible
4560 text, which confuses `set_cursor_from_row'.
4562 We use the last invisible position instead of the
4563 first because this way the cursor is always drawn on
4564 the first "." of the ellipsis, whenever PT is inside
4565 the invisible text. Otherwise the cursor would be
4566 placed _after_ the ellipsis when the point is after the
4567 first invisible character. */
4568 if (!STRINGP (it->object))
4570 it->position.charpos = newpos - 1;
4571 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4575 /* If there are before-strings at the start of invisible
4576 text, and the text is invisible because of a text
4577 property, arrange to show before-strings because 20.x did
4578 it that way. (If the text is invisible because of an
4579 overlay property instead of a text property, this is
4580 already handled in the overlay code.) */
4581 if (NILP (overlay)
4582 && get_overlay_strings (it, it->stop_charpos))
4584 handled = HANDLED_RECOMPUTE_PROPS;
4585 if (it->sp > 0)
4587 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4588 /* The call to get_overlay_strings above recomputes
4589 it->stop_charpos, but it only considers changes
4590 in properties and overlays beyond iterator's
4591 current position. This causes us to miss changes
4592 that happen exactly where the invisible property
4593 ended. So we play it safe here and force the
4594 iterator to check for potential stop positions
4595 immediately after the invisible text. Note that
4596 if get_overlay_strings returns true, it
4597 normally also pushed the iterator stack, so we
4598 need to update the stop position in the slot
4599 below the current one. */
4600 it->stack[it->sp - 1].stop_charpos
4601 = CHARPOS (it->stack[it->sp - 1].current.pos);
4604 else if (display_ellipsis_p)
4606 it->ellipsis_p = true;
4607 /* Let the ellipsis display before
4608 considering any properties of the following char.
4609 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4610 handled = HANDLED_RETURN;
4615 return handled;
4619 /* Make iterator IT return `...' next.
4620 Replaces LEN characters from buffer. */
4622 static void
4623 setup_for_ellipsis (struct it *it, int len)
4625 /* Use the display table definition for `...'. Invalid glyphs
4626 will be handled by the method returning elements from dpvec. */
4627 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4629 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4630 it->dpvec = v->contents;
4631 it->dpend = v->contents + v->header.size;
4633 else
4635 /* Default `...'. */
4636 it->dpvec = default_invis_vector;
4637 it->dpend = default_invis_vector + 3;
4640 it->dpvec_char_len = len;
4641 it->current.dpvec_index = 0;
4642 it->dpvec_face_id = -1;
4644 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4645 face as the preceding text. IT->saved_face_id was set in
4646 handle_stop to the face of the preceding character, and will be
4647 different from IT->face_id only if the invisible text skipped in
4648 handle_invisible_prop has some non-default face on its first
4649 character. We thus ignore the face of the invisible text when we
4650 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4651 if (it->saved_face_id >= 0)
4652 it->face_id = it->saved_face_id;
4654 /* If the ellipsis represents buffer text, it means we advanced in
4655 the buffer, so we should no longer ignore overlay strings. */
4656 if (it->method == GET_FROM_BUFFER)
4657 it->ignore_overlay_strings_at_pos_p = false;
4659 it->method = GET_FROM_DISPLAY_VECTOR;
4660 it->ellipsis_p = true;
4665 /***********************************************************************
4666 'display' property
4667 ***********************************************************************/
4669 /* Set up iterator IT from `display' property at its current position.
4670 Called from handle_stop.
4671 We return HANDLED_RETURN if some part of the display property
4672 overrides the display of the buffer text itself.
4673 Otherwise we return HANDLED_NORMALLY. */
4675 static enum prop_handled
4676 handle_display_prop (struct it *it)
4678 Lisp_Object propval, object, overlay;
4679 struct text_pos *position;
4680 ptrdiff_t bufpos;
4681 /* Nonzero if some property replaces the display of the text itself. */
4682 int display_replaced = 0;
4684 if (STRINGP (it->string))
4686 object = it->string;
4687 position = &it->current.string_pos;
4688 bufpos = CHARPOS (it->current.pos);
4690 else
4692 XSETWINDOW (object, it->w);
4693 position = &it->current.pos;
4694 bufpos = CHARPOS (*position);
4697 /* Reset those iterator values set from display property values. */
4698 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4699 it->space_width = Qnil;
4700 it->font_height = Qnil;
4701 it->voffset = 0;
4703 /* We don't support recursive `display' properties, i.e. string
4704 values that have a string `display' property, that have a string
4705 `display' property etc. */
4706 if (!it->string_from_display_prop_p)
4707 it->area = TEXT_AREA;
4709 propval = get_char_property_and_overlay (make_number (position->charpos),
4710 Qdisplay, object, &overlay);
4711 if (NILP (propval))
4712 return HANDLED_NORMALLY;
4713 /* Now OVERLAY is the overlay that gave us this property, or nil
4714 if it was a text property. */
4716 if (!STRINGP (it->string))
4717 object = it->w->contents;
4719 display_replaced = handle_display_spec (it, propval, object, overlay,
4720 position, bufpos,
4721 FRAME_WINDOW_P (it->f));
4722 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4725 /* Subroutine of handle_display_prop. Returns non-zero if the display
4726 specification in SPEC is a replacing specification, i.e. it would
4727 replace the text covered by `display' property with something else,
4728 such as an image or a display string. If SPEC includes any kind or
4729 `(space ...) specification, the value is 2; this is used by
4730 compute_display_string_pos, which see.
4732 See handle_single_display_spec for documentation of arguments.
4733 FRAME_WINDOW_P is true if the window being redisplayed is on a
4734 GUI frame; this argument is used only if IT is NULL, see below.
4736 IT can be NULL, if this is called by the bidi reordering code
4737 through compute_display_string_pos, which see. In that case, this
4738 function only examines SPEC, but does not otherwise "handle" it, in
4739 the sense that it doesn't set up members of IT from the display
4740 spec. */
4741 static int
4742 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4743 Lisp_Object overlay, struct text_pos *position,
4744 ptrdiff_t bufpos, bool frame_window_p)
4746 int replacing = 0;
4748 if (CONSP (spec)
4749 /* Simple specifications. */
4750 && !EQ (XCAR (spec), Qimage)
4751 #ifdef HAVE_XWIDGETS
4752 && !EQ (XCAR (spec), Qxwidget)
4753 #endif
4754 && !EQ (XCAR (spec), Qspace)
4755 && !EQ (XCAR (spec), Qwhen)
4756 && !EQ (XCAR (spec), Qslice)
4757 && !EQ (XCAR (spec), Qspace_width)
4758 && !EQ (XCAR (spec), Qheight)
4759 && !EQ (XCAR (spec), Qraise)
4760 /* Marginal area specifications. */
4761 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4762 && !EQ (XCAR (spec), Qleft_fringe)
4763 && !EQ (XCAR (spec), Qright_fringe)
4764 && !NILP (XCAR (spec)))
4766 for (; CONSP (spec); spec = XCDR (spec))
4768 int rv = handle_single_display_spec (it, XCAR (spec), object,
4769 overlay, position, bufpos,
4770 replacing, frame_window_p);
4771 if (rv != 0)
4773 replacing = rv;
4774 /* If some text in a string is replaced, `position' no
4775 longer points to the position of `object'. */
4776 if (!it || STRINGP (object))
4777 break;
4781 else if (VECTORP (spec))
4783 ptrdiff_t i;
4784 for (i = 0; i < ASIZE (spec); ++i)
4786 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4787 overlay, position, bufpos,
4788 replacing, frame_window_p);
4789 if (rv != 0)
4791 replacing = rv;
4792 /* If some text in a string is replaced, `position' no
4793 longer points to the position of `object'. */
4794 if (!it || STRINGP (object))
4795 break;
4799 else
4800 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4801 bufpos, 0, frame_window_p);
4802 return replacing;
4805 /* Value is the position of the end of the `display' property starting
4806 at START_POS in OBJECT. */
4808 static struct text_pos
4809 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4811 Lisp_Object end;
4812 struct text_pos end_pos;
4814 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4815 Qdisplay, object, Qnil);
4816 CHARPOS (end_pos) = XFASTINT (end);
4817 if (STRINGP (object))
4818 compute_string_pos (&end_pos, start_pos, it->string);
4819 else
4820 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4822 return end_pos;
4826 /* Set up IT from a single `display' property specification SPEC. OBJECT
4827 is the object in which the `display' property was found. *POSITION
4828 is the position in OBJECT at which the `display' property was found.
4829 BUFPOS is the buffer position of OBJECT (different from POSITION if
4830 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4831 previously saw a display specification which already replaced text
4832 display with something else, for example an image; we ignore such
4833 properties after the first one has been processed.
4835 OVERLAY is the overlay this `display' property came from,
4836 or nil if it was a text property.
4838 If SPEC is a `space' or `image' specification, and in some other
4839 cases too, set *POSITION to the position where the `display'
4840 property ends.
4842 If IT is NULL, only examine the property specification in SPEC, but
4843 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4844 is intended to be displayed in a window on a GUI frame.
4846 Value is non-zero if something was found which replaces the display
4847 of buffer or string text. */
4849 static int
4850 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4851 Lisp_Object overlay, struct text_pos *position,
4852 ptrdiff_t bufpos, int display_replaced,
4853 bool frame_window_p)
4855 Lisp_Object form;
4856 Lisp_Object location, value;
4857 struct text_pos start_pos = *position;
4859 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4860 If the result is non-nil, use VALUE instead of SPEC. */
4861 form = Qt;
4862 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4864 spec = XCDR (spec);
4865 if (!CONSP (spec))
4866 return 0;
4867 form = XCAR (spec);
4868 spec = XCDR (spec);
4871 if (!NILP (form) && !EQ (form, Qt))
4873 ptrdiff_t count = SPECPDL_INDEX ();
4875 /* Bind `object' to the object having the `display' property, a
4876 buffer or string. Bind `position' to the position in the
4877 object where the property was found, and `buffer-position'
4878 to the current position in the buffer. */
4880 if (NILP (object))
4881 XSETBUFFER (object, current_buffer);
4882 specbind (Qobject, object);
4883 specbind (Qposition, make_number (CHARPOS (*position)));
4884 specbind (Qbuffer_position, make_number (bufpos));
4885 form = safe_eval (form);
4886 unbind_to (count, Qnil);
4889 if (NILP (form))
4890 return 0;
4892 /* Handle `(height HEIGHT)' specifications. */
4893 if (CONSP (spec)
4894 && EQ (XCAR (spec), Qheight)
4895 && CONSP (XCDR (spec)))
4897 if (it)
4899 if (!FRAME_WINDOW_P (it->f))
4900 return 0;
4902 it->font_height = XCAR (XCDR (spec));
4903 if (!NILP (it->font_height))
4905 int new_height = -1;
4907 if (CONSP (it->font_height)
4908 && (EQ (XCAR (it->font_height), Qplus)
4909 || EQ (XCAR (it->font_height), Qminus))
4910 && CONSP (XCDR (it->font_height))
4911 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4913 /* `(+ N)' or `(- N)' where N is an integer. */
4914 int steps = XINT (XCAR (XCDR (it->font_height)));
4915 if (EQ (XCAR (it->font_height), Qplus))
4916 steps = - steps;
4917 it->face_id = smaller_face (it->f, it->face_id, steps);
4919 else if (FUNCTIONP (it->font_height))
4921 /* Call function with current height as argument.
4922 Value is the new height. */
4923 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4924 Lisp_Object height;
4925 height = safe_call1 (it->font_height,
4926 face->lface[LFACE_HEIGHT_INDEX]);
4927 if (NUMBERP (height))
4928 new_height = XFLOATINT (height);
4930 else if (NUMBERP (it->font_height))
4932 /* Value is a multiple of the canonical char height. */
4933 struct face *f;
4935 f = FACE_FROM_ID (it->f,
4936 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4937 new_height = (XFLOATINT (it->font_height)
4938 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4940 else
4942 /* Evaluate IT->font_height with `height' bound to the
4943 current specified height to get the new height. */
4944 ptrdiff_t count = SPECPDL_INDEX ();
4945 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4947 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4948 value = safe_eval (it->font_height);
4949 unbind_to (count, Qnil);
4951 if (NUMBERP (value))
4952 new_height = XFLOATINT (value);
4955 if (new_height > 0)
4956 it->face_id = face_with_height (it->f, it->face_id, new_height);
4960 return 0;
4963 /* Handle `(space-width WIDTH)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qspace_width)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 value = XCAR (XCDR (spec));
4974 if (NUMBERP (value) && XFLOATINT (value) > 0)
4975 it->space_width = value;
4978 return 0;
4981 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4982 if (CONSP (spec)
4983 && EQ (XCAR (spec), Qslice))
4985 Lisp_Object tem;
4987 if (it)
4989 if (!FRAME_WINDOW_P (it->f))
4990 return 0;
4992 if (tem = XCDR (spec), CONSP (tem))
4994 it->slice.x = XCAR (tem);
4995 if (tem = XCDR (tem), CONSP (tem))
4997 it->slice.y = XCAR (tem);
4998 if (tem = XCDR (tem), CONSP (tem))
5000 it->slice.width = XCAR (tem);
5001 if (tem = XCDR (tem), CONSP (tem))
5002 it->slice.height = XCAR (tem);
5008 return 0;
5011 /* Handle `(raise FACTOR)'. */
5012 if (CONSP (spec)
5013 && EQ (XCAR (spec), Qraise)
5014 && CONSP (XCDR (spec)))
5016 if (it)
5018 if (!FRAME_WINDOW_P (it->f))
5019 return 0;
5021 #ifdef HAVE_WINDOW_SYSTEM
5022 value = XCAR (XCDR (spec));
5023 if (NUMBERP (value))
5025 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5026 it->voffset = - (XFLOATINT (value)
5027 * (normal_char_height (face->font, -1)));
5029 #endif /* HAVE_WINDOW_SYSTEM */
5032 return 0;
5035 /* Don't handle the other kinds of display specifications
5036 inside a string that we got from a `display' property. */
5037 if (it && it->string_from_display_prop_p)
5038 return 0;
5040 /* Characters having this form of property are not displayed, so
5041 we have to find the end of the property. */
5042 if (it)
5044 start_pos = *position;
5045 *position = display_prop_end (it, object, start_pos);
5046 /* If the display property comes from an overlay, don't consider
5047 any potential stop_charpos values before the end of that
5048 overlay. Since display_prop_end will happily find another
5049 'display' property coming from some other overlay or text
5050 property on buffer positions before this overlay's end, we
5051 need to ignore them, or else we risk displaying this
5052 overlay's display string/image twice. */
5053 if (!NILP (overlay))
5055 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5057 /* Some borderline-sane Lisp might call us with the current
5058 buffer narrowed so that overlay-end is outside the
5059 POINT_MIN..POINT_MAX region, which will then cause
5060 various assertion violations and crashes down the road,
5061 starting with pop_it when it will attempt to use POSITION
5062 set below. Prevent that. */
5063 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5065 if (ovendpos > CHARPOS (*position))
5066 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5069 value = Qnil;
5071 /* Stop the scan at that end position--we assume that all
5072 text properties change there. */
5073 if (it)
5074 it->stop_charpos = position->charpos;
5076 /* Handle `(left-fringe BITMAP [FACE])'
5077 and `(right-fringe BITMAP [FACE])'. */
5078 if (CONSP (spec)
5079 && (EQ (XCAR (spec), Qleft_fringe)
5080 || EQ (XCAR (spec), Qright_fringe))
5081 && CONSP (XCDR (spec)))
5083 if (it)
5085 if (!FRAME_WINDOW_P (it->f))
5086 /* If we return here, POSITION has been advanced
5087 across the text with this property. */
5089 /* Synchronize the bidi iterator with POSITION. This is
5090 needed because we are not going to push the iterator
5091 on behalf of this display property, so there will be
5092 no pop_it call to do this synchronization for us. */
5093 if (it->bidi_p)
5095 it->position = *position;
5096 iterate_out_of_display_property (it);
5097 *position = it->position;
5099 return 1;
5102 else if (!frame_window_p)
5103 return 1;
5105 #ifdef HAVE_WINDOW_SYSTEM
5106 value = XCAR (XCDR (spec));
5107 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5108 if (! fringe_bitmap)
5109 /* If we return here, POSITION has been advanced
5110 across the text with this property. */
5112 if (it && it->bidi_p)
5114 it->position = *position;
5115 iterate_out_of_display_property (it);
5116 *position = it->position;
5118 return 1;
5121 if (it)
5123 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5125 if (CONSP (XCDR (XCDR (spec))))
5127 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5128 int face_id2 = lookup_derived_face (it->f, face_name,
5129 FRINGE_FACE_ID, false);
5130 if (face_id2 >= 0)
5131 face_id = face_id2;
5134 /* Save current settings of IT so that we can restore them
5135 when we are finished with the glyph property value. */
5136 push_it (it, position);
5138 it->area = TEXT_AREA;
5139 it->what = IT_IMAGE;
5140 it->image_id = -1; /* no image */
5141 it->position = start_pos;
5142 it->object = NILP (object) ? it->w->contents : object;
5143 it->method = GET_FROM_IMAGE;
5144 it->from_overlay = Qnil;
5145 it->face_id = face_id;
5146 it->from_disp_prop_p = true;
5148 /* Say that we haven't consumed the characters with
5149 `display' property yet. The call to pop_it in
5150 set_iterator_to_next will clean this up. */
5151 *position = start_pos;
5153 if (EQ (XCAR (spec), Qleft_fringe))
5155 it->left_user_fringe_bitmap = fringe_bitmap;
5156 it->left_user_fringe_face_id = face_id;
5158 else
5160 it->right_user_fringe_bitmap = fringe_bitmap;
5161 it->right_user_fringe_face_id = face_id;
5164 #endif /* HAVE_WINDOW_SYSTEM */
5165 return 1;
5168 /* Prepare to handle `((margin left-margin) ...)',
5169 `((margin right-margin) ...)' and `((margin nil) ...)'
5170 prefixes for display specifications. */
5171 location = Qunbound;
5172 if (CONSP (spec) && CONSP (XCAR (spec)))
5174 Lisp_Object tem;
5176 value = XCDR (spec);
5177 if (CONSP (value))
5178 value = XCAR (value);
5180 tem = XCAR (spec);
5181 if (EQ (XCAR (tem), Qmargin)
5182 && (tem = XCDR (tem),
5183 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5184 (NILP (tem)
5185 || EQ (tem, Qleft_margin)
5186 || EQ (tem, Qright_margin))))
5187 location = tem;
5190 if (EQ (location, Qunbound))
5192 location = Qnil;
5193 value = spec;
5196 /* After this point, VALUE is the property after any
5197 margin prefix has been stripped. It must be a string,
5198 an image specification, or `(space ...)'.
5200 LOCATION specifies where to display: `left-margin',
5201 `right-margin' or nil. */
5203 bool valid_p = (STRINGP (value)
5204 #ifdef HAVE_WINDOW_SYSTEM
5205 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5206 && valid_image_p (value))
5207 #endif /* not HAVE_WINDOW_SYSTEM */
5208 || (CONSP (value) && EQ (XCAR (value), Qspace))
5209 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5210 && valid_xwidget_spec_p (value)));
5212 if (valid_p && display_replaced == 0)
5214 int retval = 1;
5216 if (!it)
5218 /* Callers need to know whether the display spec is any kind
5219 of `(space ...)' spec that is about to affect text-area
5220 display. */
5221 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5222 retval = 2;
5223 return retval;
5226 /* Save current settings of IT so that we can restore them
5227 when we are finished with the glyph property value. */
5228 push_it (it, position);
5229 it->from_overlay = overlay;
5230 it->from_disp_prop_p = true;
5232 if (NILP (location))
5233 it->area = TEXT_AREA;
5234 else if (EQ (location, Qleft_margin))
5235 it->area = LEFT_MARGIN_AREA;
5236 else
5237 it->area = RIGHT_MARGIN_AREA;
5239 if (STRINGP (value))
5241 it->string = value;
5242 it->multibyte_p = STRING_MULTIBYTE (it->string);
5243 it->current.overlay_string_index = -1;
5244 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5245 it->end_charpos = it->string_nchars = SCHARS (it->string);
5246 it->method = GET_FROM_STRING;
5247 it->stop_charpos = 0;
5248 it->prev_stop = 0;
5249 it->base_level_stop = 0;
5250 it->string_from_display_prop_p = true;
5251 it->cmp_it.id = -1;
5252 /* Say that we haven't consumed the characters with
5253 `display' property yet. The call to pop_it in
5254 set_iterator_to_next will clean this up. */
5255 if (BUFFERP (object))
5256 *position = start_pos;
5258 /* Force paragraph direction to be that of the parent
5259 object. If the parent object's paragraph direction is
5260 not yet determined, default to L2R. */
5261 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5262 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5263 else
5264 it->paragraph_embedding = L2R;
5266 /* Set up the bidi iterator for this display string. */
5267 if (it->bidi_p)
5269 it->bidi_it.string.lstring = it->string;
5270 it->bidi_it.string.s = NULL;
5271 it->bidi_it.string.schars = it->end_charpos;
5272 it->bidi_it.string.bufpos = bufpos;
5273 it->bidi_it.string.from_disp_str = true;
5274 it->bidi_it.string.unibyte = !it->multibyte_p;
5275 it->bidi_it.w = it->w;
5276 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5279 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5281 it->method = GET_FROM_STRETCH;
5282 it->object = value;
5283 *position = it->position = start_pos;
5284 retval = 1 + (it->area == TEXT_AREA);
5286 else if (valid_xwidget_spec_p (value))
5288 it->what = IT_XWIDGET;
5289 it->method = GET_FROM_XWIDGET;
5290 it->position = start_pos;
5291 it->object = NILP (object) ? it->w->contents : object;
5292 *position = start_pos;
5293 it->xwidget = lookup_xwidget (value);
5295 #ifdef HAVE_WINDOW_SYSTEM
5296 else
5298 it->what = IT_IMAGE;
5299 it->image_id = lookup_image (it->f, value);
5300 it->position = start_pos;
5301 it->object = NILP (object) ? it->w->contents : object;
5302 it->method = GET_FROM_IMAGE;
5304 /* Say that we haven't consumed the characters with
5305 `display' property yet. The call to pop_it in
5306 set_iterator_to_next will clean this up. */
5307 *position = start_pos;
5309 #endif /* HAVE_WINDOW_SYSTEM */
5311 return retval;
5314 /* Invalid property or property not supported. Restore
5315 POSITION to what it was before. */
5316 *position = start_pos;
5317 return 0;
5320 /* Check if PROP is a display property value whose text should be
5321 treated as intangible. OVERLAY is the overlay from which PROP
5322 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5323 specify the buffer position covered by PROP. */
5325 bool
5326 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5327 ptrdiff_t charpos, ptrdiff_t bytepos)
5329 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5330 struct text_pos position;
5332 SET_TEXT_POS (position, charpos, bytepos);
5333 return (handle_display_spec (NULL, prop, Qnil, overlay,
5334 &position, charpos, frame_window_p)
5335 != 0);
5339 /* Return true if PROP is a display sub-property value containing STRING.
5341 Implementation note: this and the following function are really
5342 special cases of handle_display_spec and
5343 handle_single_display_spec, and should ideally use the same code.
5344 Until they do, these two pairs must be consistent and must be
5345 modified in sync. */
5347 static bool
5348 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5350 if (EQ (string, prop))
5351 return true;
5353 /* Skip over `when FORM'. */
5354 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5356 prop = XCDR (prop);
5357 if (!CONSP (prop))
5358 return false;
5359 /* Actually, the condition following `when' should be eval'ed,
5360 like handle_single_display_spec does, and we should return
5361 false if it evaluates to nil. However, this function is
5362 called only when the buffer was already displayed and some
5363 glyph in the glyph matrix was found to come from a display
5364 string. Therefore, the condition was already evaluated, and
5365 the result was non-nil, otherwise the display string wouldn't
5366 have been displayed and we would have never been called for
5367 this property. Thus, we can skip the evaluation and assume
5368 its result is non-nil. */
5369 prop = XCDR (prop);
5372 if (CONSP (prop))
5373 /* Skip over `margin LOCATION'. */
5374 if (EQ (XCAR (prop), Qmargin))
5376 prop = XCDR (prop);
5377 if (!CONSP (prop))
5378 return false;
5380 prop = XCDR (prop);
5381 if (!CONSP (prop))
5382 return false;
5385 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5389 /* Return true if STRING appears in the `display' property PROP. */
5391 static bool
5392 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5394 if (CONSP (prop)
5395 && !EQ (XCAR (prop), Qwhen)
5396 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5398 /* A list of sub-properties. */
5399 while (CONSP (prop))
5401 if (single_display_spec_string_p (XCAR (prop), string))
5402 return true;
5403 prop = XCDR (prop);
5406 else if (VECTORP (prop))
5408 /* A vector of sub-properties. */
5409 ptrdiff_t i;
5410 for (i = 0; i < ASIZE (prop); ++i)
5411 if (single_display_spec_string_p (AREF (prop, i), string))
5412 return true;
5414 else
5415 return single_display_spec_string_p (prop, string);
5417 return false;
5420 /* Look for STRING in overlays and text properties in the current
5421 buffer, between character positions FROM and TO (excluding TO).
5422 BACK_P means look back (in this case, TO is supposed to be
5423 less than FROM).
5424 Value is the first character position where STRING was found, or
5425 zero if it wasn't found before hitting TO.
5427 This function may only use code that doesn't eval because it is
5428 called asynchronously from note_mouse_highlight. */
5430 static ptrdiff_t
5431 string_buffer_position_lim (Lisp_Object string,
5432 ptrdiff_t from, ptrdiff_t to, bool back_p)
5434 Lisp_Object limit, prop, pos;
5435 bool found = false;
5437 pos = make_number (max (from, BEGV));
5439 if (!back_p) /* looking forward */
5441 limit = make_number (min (to, ZV));
5442 while (!found && !EQ (pos, limit))
5444 prop = Fget_char_property (pos, Qdisplay, Qnil);
5445 if (!NILP (prop) && display_prop_string_p (prop, string))
5446 found = true;
5447 else
5448 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5449 limit);
5452 else /* looking back */
5454 limit = make_number (max (to, BEGV));
5455 while (!found && !EQ (pos, limit))
5457 prop = Fget_char_property (pos, Qdisplay, Qnil);
5458 if (!NILP (prop) && display_prop_string_p (prop, string))
5459 found = true;
5460 else
5461 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5462 limit);
5466 return found ? XINT (pos) : 0;
5469 /* Determine which buffer position in current buffer STRING comes from.
5470 AROUND_CHARPOS is an approximate position where it could come from.
5471 Value is the buffer position or 0 if it couldn't be determined.
5473 This function is necessary because we don't record buffer positions
5474 in glyphs generated from strings (to keep struct glyph small).
5475 This function may only use code that doesn't eval because it is
5476 called asynchronously from note_mouse_highlight. */
5478 static ptrdiff_t
5479 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5481 const int MAX_DISTANCE = 1000;
5482 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5483 around_charpos + MAX_DISTANCE,
5484 false);
5486 if (!found)
5487 found = string_buffer_position_lim (string, around_charpos,
5488 around_charpos - MAX_DISTANCE, true);
5489 return found;
5494 /***********************************************************************
5495 `composition' property
5496 ***********************************************************************/
5498 /* Set up iterator IT from `composition' property at its current
5499 position. Called from handle_stop. */
5501 static enum prop_handled
5502 handle_composition_prop (struct it *it)
5504 Lisp_Object prop, string;
5505 ptrdiff_t pos, pos_byte, start, end;
5507 if (STRINGP (it->string))
5509 unsigned char *s;
5511 pos = IT_STRING_CHARPOS (*it);
5512 pos_byte = IT_STRING_BYTEPOS (*it);
5513 string = it->string;
5514 s = SDATA (string) + pos_byte;
5515 it->c = STRING_CHAR (s);
5517 else
5519 pos = IT_CHARPOS (*it);
5520 pos_byte = IT_BYTEPOS (*it);
5521 string = Qnil;
5522 it->c = FETCH_CHAR (pos_byte);
5525 /* If there's a valid composition and point is not inside of the
5526 composition (in the case that the composition is from the current
5527 buffer), draw a glyph composed from the composition components. */
5528 if (find_composition (pos, -1, &start, &end, &prop, string)
5529 && composition_valid_p (start, end, prop)
5530 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5532 if (start < pos)
5533 /* As we can't handle this situation (perhaps font-lock added
5534 a new composition), we just return here hoping that next
5535 redisplay will detect this composition much earlier. */
5536 return HANDLED_NORMALLY;
5537 if (start != pos)
5539 if (STRINGP (it->string))
5540 pos_byte = string_char_to_byte (it->string, start);
5541 else
5542 pos_byte = CHAR_TO_BYTE (start);
5544 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5545 prop, string);
5547 if (it->cmp_it.id >= 0)
5549 it->cmp_it.ch = -1;
5550 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5551 it->cmp_it.nglyphs = -1;
5555 return HANDLED_NORMALLY;
5560 /***********************************************************************
5561 Overlay strings
5562 ***********************************************************************/
5564 /* The following structure is used to record overlay strings for
5565 later sorting in load_overlay_strings. */
5567 struct overlay_entry
5569 Lisp_Object overlay;
5570 Lisp_Object string;
5571 EMACS_INT priority;
5572 bool after_string_p;
5576 /* Set up iterator IT from overlay strings at its current position.
5577 Called from handle_stop. */
5579 static enum prop_handled
5580 handle_overlay_change (struct it *it)
5582 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5583 return HANDLED_RECOMPUTE_PROPS;
5584 else
5585 return HANDLED_NORMALLY;
5589 /* Set up the next overlay string for delivery by IT, if there is an
5590 overlay string to deliver. Called by set_iterator_to_next when the
5591 end of the current overlay string is reached. If there are more
5592 overlay strings to display, IT->string and
5593 IT->current.overlay_string_index are set appropriately here.
5594 Otherwise IT->string is set to nil. */
5596 static void
5597 next_overlay_string (struct it *it)
5599 ++it->current.overlay_string_index;
5600 if (it->current.overlay_string_index == it->n_overlay_strings)
5602 /* No more overlay strings. Restore IT's settings to what
5603 they were before overlay strings were processed, and
5604 continue to deliver from current_buffer. */
5606 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5607 pop_it (it);
5608 eassert (it->sp > 0
5609 || (NILP (it->string)
5610 && it->method == GET_FROM_BUFFER
5611 && it->stop_charpos >= BEGV
5612 && it->stop_charpos <= it->end_charpos));
5613 it->current.overlay_string_index = -1;
5614 it->n_overlay_strings = 0;
5615 /* If there's an empty display string on the stack, pop the
5616 stack, to resync the bidi iterator with IT's position. Such
5617 empty strings are pushed onto the stack in
5618 get_overlay_strings_1. */
5619 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5620 pop_it (it);
5622 /* Since we've exhausted overlay strings at this buffer
5623 position, set the flag to ignore overlays until we move to
5624 another position. (The flag will be reset in
5625 next_element_from_buffer.) But don't do that if the overlay
5626 strings were loaded at position other than the current one,
5627 which could happen if we called pop_it above, or if the
5628 overlay strings were loaded by handle_invisible_prop at the
5629 beginning of invisible text. */
5630 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5631 it->ignore_overlay_strings_at_pos_p = true;
5633 /* If we're at the end of the buffer, record that we have
5634 processed the overlay strings there already, so that
5635 next_element_from_buffer doesn't try it again. */
5636 if (NILP (it->string)
5637 && IT_CHARPOS (*it) >= it->end_charpos
5638 && it->overlay_strings_charpos >= it->end_charpos)
5639 it->overlay_strings_at_end_processed_p = true;
5640 /* Note: we reset overlay_strings_charpos only here, to make
5641 sure the just-processed overlays were indeed at EOB.
5642 Otherwise, overlays on text with invisible text property,
5643 which are processed with IT's position past the invisible
5644 text, might fool us into thinking the overlays at EOB were
5645 already processed (linum-mode can cause this, for
5646 example). */
5647 it->overlay_strings_charpos = -1;
5649 else
5651 /* There are more overlay strings to process. If
5652 IT->current.overlay_string_index has advanced to a position
5653 where we must load IT->overlay_strings with more strings, do
5654 it. We must load at the IT->overlay_strings_charpos where
5655 IT->n_overlay_strings was originally computed; when invisible
5656 text is present, this might not be IT_CHARPOS (Bug#7016). */
5657 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5659 if (it->current.overlay_string_index && i == 0)
5660 load_overlay_strings (it, it->overlay_strings_charpos);
5662 /* Initialize IT to deliver display elements from the overlay
5663 string. */
5664 it->string = it->overlay_strings[i];
5665 it->multibyte_p = STRING_MULTIBYTE (it->string);
5666 SET_TEXT_POS (it->current.string_pos, 0, 0);
5667 it->method = GET_FROM_STRING;
5668 it->stop_charpos = 0;
5669 it->end_charpos = SCHARS (it->string);
5670 if (it->cmp_it.stop_pos >= 0)
5671 it->cmp_it.stop_pos = 0;
5672 it->prev_stop = 0;
5673 it->base_level_stop = 0;
5675 /* Set up the bidi iterator for this overlay string. */
5676 if (it->bidi_p)
5678 it->bidi_it.string.lstring = it->string;
5679 it->bidi_it.string.s = NULL;
5680 it->bidi_it.string.schars = SCHARS (it->string);
5681 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5682 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5683 it->bidi_it.string.unibyte = !it->multibyte_p;
5684 it->bidi_it.w = it->w;
5685 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5689 CHECK_IT (it);
5693 /* Compare two overlay_entry structures E1 and E2. Used as a
5694 comparison function for qsort in load_overlay_strings. Overlay
5695 strings for the same position are sorted so that
5697 1. All after-strings come in front of before-strings, except
5698 when they come from the same overlay.
5700 2. Within after-strings, strings are sorted so that overlay strings
5701 from overlays with higher priorities come first.
5703 2. Within before-strings, strings are sorted so that overlay
5704 strings from overlays with higher priorities come last.
5706 Value is analogous to strcmp. */
5709 static int
5710 compare_overlay_entries (const void *e1, const void *e2)
5712 struct overlay_entry const *entry1 = e1;
5713 struct overlay_entry const *entry2 = e2;
5714 int result;
5716 if (entry1->after_string_p != entry2->after_string_p)
5718 /* Let after-strings appear in front of before-strings if
5719 they come from different overlays. */
5720 if (EQ (entry1->overlay, entry2->overlay))
5721 result = entry1->after_string_p ? 1 : -1;
5722 else
5723 result = entry1->after_string_p ? -1 : 1;
5725 else if (entry1->priority != entry2->priority)
5727 if (entry1->after_string_p)
5728 /* After-strings sorted in order of decreasing priority. */
5729 result = entry2->priority < entry1->priority ? -1 : 1;
5730 else
5731 /* Before-strings sorted in order of increasing priority. */
5732 result = entry1->priority < entry2->priority ? -1 : 1;
5734 else
5735 result = 0;
5737 return result;
5741 /* Load the vector IT->overlay_strings with overlay strings from IT's
5742 current buffer position, or from CHARPOS if that is > 0. Set
5743 IT->n_overlays to the total number of overlay strings found.
5745 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5746 a time. On entry into load_overlay_strings,
5747 IT->current.overlay_string_index gives the number of overlay
5748 strings that have already been loaded by previous calls to this
5749 function.
5751 IT->add_overlay_start contains an additional overlay start
5752 position to consider for taking overlay strings from, if non-zero.
5753 This position comes into play when the overlay has an `invisible'
5754 property, and both before and after-strings. When we've skipped to
5755 the end of the overlay, because of its `invisible' property, we
5756 nevertheless want its before-string to appear.
5757 IT->add_overlay_start will contain the overlay start position
5758 in this case.
5760 Overlay strings are sorted so that after-string strings come in
5761 front of before-string strings. Within before and after-strings,
5762 strings are sorted by overlay priority. See also function
5763 compare_overlay_entries. */
5765 static void
5766 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5768 Lisp_Object overlay, window, str, invisible;
5769 struct Lisp_Overlay *ov;
5770 ptrdiff_t start, end;
5771 ptrdiff_t n = 0, i, j;
5772 int invis;
5773 struct overlay_entry entriesbuf[20];
5774 ptrdiff_t size = ARRAYELTS (entriesbuf);
5775 struct overlay_entry *entries = entriesbuf;
5776 USE_SAFE_ALLOCA;
5778 if (charpos <= 0)
5779 charpos = IT_CHARPOS (*it);
5781 /* Append the overlay string STRING of overlay OVERLAY to vector
5782 `entries' which has size `size' and currently contains `n'
5783 elements. AFTER_P means STRING is an after-string of
5784 OVERLAY. */
5785 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5786 do \
5788 Lisp_Object priority; \
5790 if (n == size) \
5792 struct overlay_entry *old = entries; \
5793 SAFE_NALLOCA (entries, 2, size); \
5794 memcpy (entries, old, size * sizeof *entries); \
5795 size *= 2; \
5798 entries[n].string = (STRING); \
5799 entries[n].overlay = (OVERLAY); \
5800 priority = Foverlay_get ((OVERLAY), Qpriority); \
5801 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5802 entries[n].after_string_p = (AFTER_P); \
5803 ++n; \
5805 while (false)
5807 /* Process overlay before the overlay center. */
5808 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5810 XSETMISC (overlay, ov);
5811 eassert (OVERLAYP (overlay));
5812 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5813 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5815 if (end < charpos)
5816 break;
5818 /* Skip this overlay if it doesn't start or end at IT's current
5819 position. */
5820 if (end != charpos && start != charpos)
5821 continue;
5823 /* Skip this overlay if it doesn't apply to IT->w. */
5824 window = Foverlay_get (overlay, Qwindow);
5825 if (WINDOWP (window) && XWINDOW (window) != it->w)
5826 continue;
5828 /* If the text ``under'' the overlay is invisible, both before-
5829 and after-strings from this overlay are visible; start and
5830 end position are indistinguishable. */
5831 invisible = Foverlay_get (overlay, Qinvisible);
5832 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5834 /* If overlay has a non-empty before-string, record it. */
5835 if ((start == charpos || (end == charpos && invis != 0))
5836 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5837 && SCHARS (str))
5838 RECORD_OVERLAY_STRING (overlay, str, false);
5840 /* If overlay has a non-empty after-string, record it. */
5841 if ((end == charpos || (start == charpos && invis != 0))
5842 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5843 && SCHARS (str))
5844 RECORD_OVERLAY_STRING (overlay, str, true);
5847 /* Process overlays after the overlay center. */
5848 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5850 XSETMISC (overlay, ov);
5851 eassert (OVERLAYP (overlay));
5852 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5853 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5855 if (start > charpos)
5856 break;
5858 /* Skip this overlay if it doesn't start or end at IT's current
5859 position. */
5860 if (end != charpos && start != charpos)
5861 continue;
5863 /* Skip this overlay if it doesn't apply to IT->w. */
5864 window = Foverlay_get (overlay, Qwindow);
5865 if (WINDOWP (window) && XWINDOW (window) != it->w)
5866 continue;
5868 /* If the text ``under'' the overlay is invisible, it has a zero
5869 dimension, and both before- and after-strings apply. */
5870 invisible = Foverlay_get (overlay, Qinvisible);
5871 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5873 /* If overlay has a non-empty before-string, record it. */
5874 if ((start == charpos || (end == charpos && invis != 0))
5875 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5876 && SCHARS (str))
5877 RECORD_OVERLAY_STRING (overlay, str, false);
5879 /* If overlay has a non-empty after-string, record it. */
5880 if ((end == charpos || (start == charpos && invis != 0))
5881 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5882 && SCHARS (str))
5883 RECORD_OVERLAY_STRING (overlay, str, true);
5886 #undef RECORD_OVERLAY_STRING
5888 /* Sort entries. */
5889 if (n > 1)
5890 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5892 /* Record number of overlay strings, and where we computed it. */
5893 it->n_overlay_strings = n;
5894 it->overlay_strings_charpos = charpos;
5896 /* IT->current.overlay_string_index is the number of overlay strings
5897 that have already been consumed by IT. Copy some of the
5898 remaining overlay strings to IT->overlay_strings. */
5899 i = 0;
5900 j = it->current.overlay_string_index;
5901 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5903 it->overlay_strings[i] = entries[j].string;
5904 it->string_overlays[i++] = entries[j++].overlay;
5907 CHECK_IT (it);
5908 SAFE_FREE ();
5912 /* Get the first chunk of overlay strings at IT's current buffer
5913 position, or at CHARPOS if that is > 0. Value is true if at
5914 least one overlay string was found. */
5916 static bool
5917 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5919 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5920 process. This fills IT->overlay_strings with strings, and sets
5921 IT->n_overlay_strings to the total number of strings to process.
5922 IT->pos.overlay_string_index has to be set temporarily to zero
5923 because load_overlay_strings needs this; it must be set to -1
5924 when no overlay strings are found because a zero value would
5925 indicate a position in the first overlay string. */
5926 it->current.overlay_string_index = 0;
5927 load_overlay_strings (it, charpos);
5929 /* If we found overlay strings, set up IT to deliver display
5930 elements from the first one. Otherwise set up IT to deliver
5931 from current_buffer. */
5932 if (it->n_overlay_strings)
5934 /* Make sure we know settings in current_buffer, so that we can
5935 restore meaningful values when we're done with the overlay
5936 strings. */
5937 if (compute_stop_p)
5938 compute_stop_pos (it);
5939 eassert (it->face_id >= 0);
5941 /* Save IT's settings. They are restored after all overlay
5942 strings have been processed. */
5943 eassert (!compute_stop_p || it->sp == 0);
5945 /* When called from handle_stop, there might be an empty display
5946 string loaded. In that case, don't bother saving it. But
5947 don't use this optimization with the bidi iterator, since we
5948 need the corresponding pop_it call to resync the bidi
5949 iterator's position with IT's position, after we are done
5950 with the overlay strings. (The corresponding call to pop_it
5951 in case of an empty display string is in
5952 next_overlay_string.) */
5953 if (!(!it->bidi_p
5954 && STRINGP (it->string) && !SCHARS (it->string)))
5955 push_it (it, NULL);
5957 /* Set up IT to deliver display elements from the first overlay
5958 string. */
5959 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5960 it->string = it->overlay_strings[0];
5961 it->from_overlay = Qnil;
5962 it->stop_charpos = 0;
5963 eassert (STRINGP (it->string));
5964 it->end_charpos = SCHARS (it->string);
5965 it->prev_stop = 0;
5966 it->base_level_stop = 0;
5967 it->multibyte_p = STRING_MULTIBYTE (it->string);
5968 it->method = GET_FROM_STRING;
5969 it->from_disp_prop_p = 0;
5970 it->cmp_it.id = -1;
5972 /* Force paragraph direction to be that of the parent
5973 buffer. */
5974 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5975 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5976 else
5977 it->paragraph_embedding = L2R;
5979 /* Set up the bidi iterator for this overlay string. */
5980 if (it->bidi_p)
5982 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5984 it->bidi_it.string.lstring = it->string;
5985 it->bidi_it.string.s = NULL;
5986 it->bidi_it.string.schars = SCHARS (it->string);
5987 it->bidi_it.string.bufpos = pos;
5988 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5989 it->bidi_it.string.unibyte = !it->multibyte_p;
5990 it->bidi_it.w = it->w;
5991 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5993 return true;
5996 it->current.overlay_string_index = -1;
5997 return false;
6000 static bool
6001 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6003 it->string = Qnil;
6004 it->method = GET_FROM_BUFFER;
6006 get_overlay_strings_1 (it, charpos, true);
6008 CHECK_IT (it);
6010 /* Value is true if we found at least one overlay string. */
6011 return STRINGP (it->string);
6016 /***********************************************************************
6017 Saving and restoring state
6018 ***********************************************************************/
6020 /* Save current settings of IT on IT->stack. Called, for example,
6021 before setting up IT for an overlay string, to be able to restore
6022 IT's settings to what they were after the overlay string has been
6023 processed. If POSITION is non-NULL, it is the position to save on
6024 the stack instead of IT->position. */
6026 static void
6027 push_it (struct it *it, struct text_pos *position)
6029 struct iterator_stack_entry *p;
6031 eassert (it->sp < IT_STACK_SIZE);
6032 p = it->stack + it->sp;
6034 p->stop_charpos = it->stop_charpos;
6035 p->prev_stop = it->prev_stop;
6036 p->base_level_stop = it->base_level_stop;
6037 p->cmp_it = it->cmp_it;
6038 eassert (it->face_id >= 0);
6039 p->face_id = it->face_id;
6040 p->string = it->string;
6041 p->method = it->method;
6042 p->from_overlay = it->from_overlay;
6043 switch (p->method)
6045 case GET_FROM_IMAGE:
6046 p->u.image.object = it->object;
6047 p->u.image.image_id = it->image_id;
6048 p->u.image.slice = it->slice;
6049 break;
6050 case GET_FROM_STRETCH:
6051 p->u.stretch.object = it->object;
6052 break;
6053 case GET_FROM_XWIDGET:
6054 p->u.xwidget.object = it->object;
6055 break;
6056 case GET_FROM_BUFFER:
6057 case GET_FROM_DISPLAY_VECTOR:
6058 case GET_FROM_STRING:
6059 case GET_FROM_C_STRING:
6060 break;
6061 default:
6062 emacs_abort ();
6064 p->position = position ? *position : it->position;
6065 p->current = it->current;
6066 p->end_charpos = it->end_charpos;
6067 p->string_nchars = it->string_nchars;
6068 p->area = it->area;
6069 p->multibyte_p = it->multibyte_p;
6070 p->avoid_cursor_p = it->avoid_cursor_p;
6071 p->space_width = it->space_width;
6072 p->font_height = it->font_height;
6073 p->voffset = it->voffset;
6074 p->string_from_display_prop_p = it->string_from_display_prop_p;
6075 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6076 p->display_ellipsis_p = false;
6077 p->line_wrap = it->line_wrap;
6078 p->bidi_p = it->bidi_p;
6079 p->paragraph_embedding = it->paragraph_embedding;
6080 p->from_disp_prop_p = it->from_disp_prop_p;
6081 ++it->sp;
6083 /* Save the state of the bidi iterator as well. */
6084 if (it->bidi_p)
6085 bidi_push_it (&it->bidi_it);
6088 static void
6089 iterate_out_of_display_property (struct it *it)
6091 bool buffer_p = !STRINGP (it->string);
6092 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6093 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6095 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6097 /* Maybe initialize paragraph direction. If we are at the beginning
6098 of a new paragraph, next_element_from_buffer may not have a
6099 chance to do that. */
6100 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6101 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6102 /* prev_stop can be zero, so check against BEGV as well. */
6103 while (it->bidi_it.charpos >= bob
6104 && it->prev_stop <= it->bidi_it.charpos
6105 && it->bidi_it.charpos < CHARPOS (it->position)
6106 && it->bidi_it.charpos < eob)
6107 bidi_move_to_visually_next (&it->bidi_it);
6108 /* Record the stop_pos we just crossed, for when we cross it
6109 back, maybe. */
6110 if (it->bidi_it.charpos > CHARPOS (it->position))
6111 it->prev_stop = CHARPOS (it->position);
6112 /* If we ended up not where pop_it put us, resync IT's
6113 positional members with the bidi iterator. */
6114 if (it->bidi_it.charpos != CHARPOS (it->position))
6115 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6116 if (buffer_p)
6117 it->current.pos = it->position;
6118 else
6119 it->current.string_pos = it->position;
6122 /* Restore IT's settings from IT->stack. Called, for example, when no
6123 more overlay strings must be processed, and we return to delivering
6124 display elements from a buffer, or when the end of a string from a
6125 `display' property is reached and we return to delivering display
6126 elements from an overlay string, or from a buffer. */
6128 static void
6129 pop_it (struct it *it)
6131 struct iterator_stack_entry *p;
6132 bool from_display_prop = it->from_disp_prop_p;
6133 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6135 eassert (it->sp > 0);
6136 --it->sp;
6137 p = it->stack + it->sp;
6138 it->stop_charpos = p->stop_charpos;
6139 it->prev_stop = p->prev_stop;
6140 it->base_level_stop = p->base_level_stop;
6141 it->cmp_it = p->cmp_it;
6142 it->face_id = p->face_id;
6143 it->current = p->current;
6144 it->position = p->position;
6145 it->string = p->string;
6146 it->from_overlay = p->from_overlay;
6147 if (NILP (it->string))
6148 SET_TEXT_POS (it->current.string_pos, -1, -1);
6149 it->method = p->method;
6150 switch (it->method)
6152 case GET_FROM_IMAGE:
6153 it->image_id = p->u.image.image_id;
6154 it->object = p->u.image.object;
6155 it->slice = p->u.image.slice;
6156 break;
6157 case GET_FROM_XWIDGET:
6158 it->object = p->u.xwidget.object;
6159 break;
6160 case GET_FROM_STRETCH:
6161 it->object = p->u.stretch.object;
6162 break;
6163 case GET_FROM_BUFFER:
6164 it->object = it->w->contents;
6165 break;
6166 case GET_FROM_STRING:
6168 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6170 /* Restore the face_box_p flag, since it could have been
6171 overwritten by the face of the object that we just finished
6172 displaying. */
6173 if (face)
6174 it->face_box_p = face->box != FACE_NO_BOX;
6175 it->object = it->string;
6177 break;
6178 case GET_FROM_DISPLAY_VECTOR:
6179 if (it->s)
6180 it->method = GET_FROM_C_STRING;
6181 else if (STRINGP (it->string))
6182 it->method = GET_FROM_STRING;
6183 else
6185 it->method = GET_FROM_BUFFER;
6186 it->object = it->w->contents;
6188 break;
6189 case GET_FROM_C_STRING:
6190 break;
6191 default:
6192 emacs_abort ();
6194 it->end_charpos = p->end_charpos;
6195 it->string_nchars = p->string_nchars;
6196 it->area = p->area;
6197 it->multibyte_p = p->multibyte_p;
6198 it->avoid_cursor_p = p->avoid_cursor_p;
6199 it->space_width = p->space_width;
6200 it->font_height = p->font_height;
6201 it->voffset = p->voffset;
6202 it->string_from_display_prop_p = p->string_from_display_prop_p;
6203 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6204 it->line_wrap = p->line_wrap;
6205 it->bidi_p = p->bidi_p;
6206 it->paragraph_embedding = p->paragraph_embedding;
6207 it->from_disp_prop_p = p->from_disp_prop_p;
6208 if (it->bidi_p)
6210 bidi_pop_it (&it->bidi_it);
6211 /* Bidi-iterate until we get out of the portion of text, if any,
6212 covered by a `display' text property or by an overlay with
6213 `display' property. (We cannot just jump there, because the
6214 internal coherency of the bidi iterator state can not be
6215 preserved across such jumps.) We also must determine the
6216 paragraph base direction if the overlay we just processed is
6217 at the beginning of a new paragraph. */
6218 if (from_display_prop
6219 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6220 iterate_out_of_display_property (it);
6222 eassert ((BUFFERP (it->object)
6223 && IT_CHARPOS (*it) == it->bidi_it.charpos
6224 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6225 || (STRINGP (it->object)
6226 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6227 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6228 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6230 /* If we move the iterator over text covered by a display property
6231 to a new buffer position, any info about previously seen overlays
6232 is no longer valid. */
6233 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6234 it->ignore_overlay_strings_at_pos_p = false;
6239 /***********************************************************************
6240 Moving over lines
6241 ***********************************************************************/
6243 /* Set IT's current position to the previous line start. */
6245 static void
6246 back_to_previous_line_start (struct it *it)
6248 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6250 DEC_BOTH (cp, bp);
6251 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6255 /* Move IT to the next line start.
6257 Value is true if a newline was found. Set *SKIPPED_P to true if
6258 we skipped over part of the text (as opposed to moving the iterator
6259 continuously over the text). Otherwise, don't change the value
6260 of *SKIPPED_P.
6262 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6263 iterator on the newline, if it was found.
6265 Newlines may come from buffer text, overlay strings, or strings
6266 displayed via the `display' property. That's the reason we can't
6267 simply use find_newline_no_quit.
6269 Note that this function may not skip over invisible text that is so
6270 because of text properties and immediately follows a newline. If
6271 it would, function reseat_at_next_visible_line_start, when called
6272 from set_iterator_to_next, would effectively make invisible
6273 characters following a newline part of the wrong glyph row, which
6274 leads to wrong cursor motion. */
6276 static bool
6277 forward_to_next_line_start (struct it *it, bool *skipped_p,
6278 struct bidi_it *bidi_it_prev)
6280 ptrdiff_t old_selective;
6281 bool newline_found_p = false;
6282 int n;
6283 const int MAX_NEWLINE_DISTANCE = 500;
6285 /* If already on a newline, just consume it to avoid unintended
6286 skipping over invisible text below. */
6287 if (it->what == IT_CHARACTER
6288 && it->c == '\n'
6289 && CHARPOS (it->position) == IT_CHARPOS (*it))
6291 if (it->bidi_p && bidi_it_prev)
6292 *bidi_it_prev = it->bidi_it;
6293 set_iterator_to_next (it, false);
6294 it->c = 0;
6295 return true;
6298 /* Don't handle selective display in the following. It's (a)
6299 unnecessary because it's done by the caller, and (b) leads to an
6300 infinite recursion because next_element_from_ellipsis indirectly
6301 calls this function. */
6302 old_selective = it->selective;
6303 it->selective = 0;
6305 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6306 from buffer text. */
6307 for (n = 0;
6308 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6309 n += !STRINGP (it->string))
6311 if (!get_next_display_element (it))
6312 return false;
6313 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6314 if (newline_found_p && it->bidi_p && bidi_it_prev)
6315 *bidi_it_prev = it->bidi_it;
6316 set_iterator_to_next (it, false);
6319 /* If we didn't find a newline near enough, see if we can use a
6320 short-cut. */
6321 if (!newline_found_p)
6323 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6324 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6325 1, &bytepos);
6326 Lisp_Object pos;
6328 eassert (!STRINGP (it->string));
6330 /* If there isn't any `display' property in sight, and no
6331 overlays, we can just use the position of the newline in
6332 buffer text. */
6333 if (it->stop_charpos >= limit
6334 || ((pos = Fnext_single_property_change (make_number (start),
6335 Qdisplay, Qnil,
6336 make_number (limit)),
6337 NILP (pos))
6338 && next_overlay_change (start) == ZV))
6340 if (!it->bidi_p)
6342 IT_CHARPOS (*it) = limit;
6343 IT_BYTEPOS (*it) = bytepos;
6345 else
6347 struct bidi_it bprev;
6349 /* Help bidi.c avoid expensive searches for display
6350 properties and overlays, by telling it that there are
6351 none up to `limit'. */
6352 if (it->bidi_it.disp_pos < limit)
6354 it->bidi_it.disp_pos = limit;
6355 it->bidi_it.disp_prop = 0;
6357 do {
6358 bprev = it->bidi_it;
6359 bidi_move_to_visually_next (&it->bidi_it);
6360 } while (it->bidi_it.charpos != limit);
6361 IT_CHARPOS (*it) = limit;
6362 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6363 if (bidi_it_prev)
6364 *bidi_it_prev = bprev;
6366 *skipped_p = newline_found_p = true;
6368 else
6370 while (!newline_found_p)
6372 if (!get_next_display_element (it))
6373 break;
6374 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6375 if (newline_found_p && it->bidi_p && bidi_it_prev)
6376 *bidi_it_prev = it->bidi_it;
6377 set_iterator_to_next (it, false);
6382 it->selective = old_selective;
6383 return newline_found_p;
6387 /* Set IT's current position to the previous visible line start. Skip
6388 invisible text that is so either due to text properties or due to
6389 selective display. Caution: this does not change IT->current_x and
6390 IT->hpos. */
6392 static void
6393 back_to_previous_visible_line_start (struct it *it)
6395 while (IT_CHARPOS (*it) > BEGV)
6397 back_to_previous_line_start (it);
6399 if (IT_CHARPOS (*it) <= BEGV)
6400 break;
6402 /* If selective > 0, then lines indented more than its value are
6403 invisible. */
6404 if (it->selective > 0
6405 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6406 it->selective))
6407 continue;
6409 /* Check the newline before point for invisibility. */
6411 Lisp_Object prop;
6412 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6413 Qinvisible, it->window);
6414 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6415 continue;
6418 if (IT_CHARPOS (*it) <= BEGV)
6419 break;
6422 struct it it2;
6423 void *it2data = NULL;
6424 ptrdiff_t pos;
6425 ptrdiff_t beg, end;
6426 Lisp_Object val, overlay;
6428 SAVE_IT (it2, *it, it2data);
6430 /* If newline is part of a composition, continue from start of composition */
6431 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6432 && beg < IT_CHARPOS (*it))
6433 goto replaced;
6435 /* If newline is replaced by a display property, find start of overlay
6436 or interval and continue search from that point. */
6437 pos = --IT_CHARPOS (it2);
6438 --IT_BYTEPOS (it2);
6439 it2.sp = 0;
6440 bidi_unshelve_cache (NULL, false);
6441 it2.string_from_display_prop_p = false;
6442 it2.from_disp_prop_p = false;
6443 if (handle_display_prop (&it2) == HANDLED_RETURN
6444 && !NILP (val = get_char_property_and_overlay
6445 (make_number (pos), Qdisplay, Qnil, &overlay))
6446 && (OVERLAYP (overlay)
6447 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6448 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6450 RESTORE_IT (it, it, it2data);
6451 goto replaced;
6454 /* Newline is not replaced by anything -- so we are done. */
6455 RESTORE_IT (it, it, it2data);
6456 break;
6458 replaced:
6459 if (beg < BEGV)
6460 beg = BEGV;
6461 IT_CHARPOS (*it) = beg;
6462 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6466 it->continuation_lines_width = 0;
6468 eassert (IT_CHARPOS (*it) >= BEGV);
6469 eassert (IT_CHARPOS (*it) == BEGV
6470 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6471 CHECK_IT (it);
6475 /* Reseat iterator IT at the previous visible line start. Skip
6476 invisible text that is so either due to text properties or due to
6477 selective display. At the end, update IT's overlay information,
6478 face information etc. */
6480 void
6481 reseat_at_previous_visible_line_start (struct it *it)
6483 back_to_previous_visible_line_start (it);
6484 reseat (it, it->current.pos, true);
6485 CHECK_IT (it);
6489 /* Reseat iterator IT on the next visible line start in the current
6490 buffer. ON_NEWLINE_P means position IT on the newline
6491 preceding the line start. Skip over invisible text that is so
6492 because of selective display. Compute faces, overlays etc at the
6493 new position. Note that this function does not skip over text that
6494 is invisible because of text properties. */
6496 static void
6497 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6499 bool skipped_p = false;
6500 struct bidi_it bidi_it_prev;
6501 bool newline_found_p
6502 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6504 /* Skip over lines that are invisible because they are indented
6505 more than the value of IT->selective. */
6506 if (it->selective > 0)
6507 while (IT_CHARPOS (*it) < ZV
6508 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6509 it->selective))
6511 eassert (IT_BYTEPOS (*it) == BEGV
6512 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6513 newline_found_p =
6514 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6517 /* Position on the newline if that's what's requested. */
6518 if (on_newline_p && newline_found_p)
6520 if (STRINGP (it->string))
6522 if (IT_STRING_CHARPOS (*it) > 0)
6524 if (!it->bidi_p)
6526 --IT_STRING_CHARPOS (*it);
6527 --IT_STRING_BYTEPOS (*it);
6529 else
6531 /* We need to restore the bidi iterator to the state
6532 it had on the newline, and resync the IT's
6533 position with that. */
6534 it->bidi_it = bidi_it_prev;
6535 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6536 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6540 else if (IT_CHARPOS (*it) > BEGV)
6542 if (!it->bidi_p)
6544 --IT_CHARPOS (*it);
6545 --IT_BYTEPOS (*it);
6547 else
6549 /* We need to restore the bidi iterator to the state it
6550 had on the newline and resync IT with that. */
6551 it->bidi_it = bidi_it_prev;
6552 IT_CHARPOS (*it) = it->bidi_it.charpos;
6553 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6555 reseat (it, it->current.pos, false);
6558 else if (skipped_p)
6559 reseat (it, it->current.pos, false);
6561 CHECK_IT (it);
6566 /***********************************************************************
6567 Changing an iterator's position
6568 ***********************************************************************/
6570 /* Change IT's current position to POS in current_buffer.
6571 If FORCE_P, always check for text properties at the new position.
6572 Otherwise, text properties are only looked up if POS >=
6573 IT->check_charpos of a property. */
6575 static void
6576 reseat (struct it *it, struct text_pos pos, bool force_p)
6578 ptrdiff_t original_pos = IT_CHARPOS (*it);
6580 reseat_1 (it, pos, false);
6582 /* Determine where to check text properties. Avoid doing it
6583 where possible because text property lookup is very expensive. */
6584 if (force_p
6585 || CHARPOS (pos) > it->stop_charpos
6586 || CHARPOS (pos) < original_pos)
6588 if (it->bidi_p)
6590 /* For bidi iteration, we need to prime prev_stop and
6591 base_level_stop with our best estimations. */
6592 /* Implementation note: Of course, POS is not necessarily a
6593 stop position, so assigning prev_pos to it is a lie; we
6594 should have called compute_stop_backwards. However, if
6595 the current buffer does not include any R2L characters,
6596 that call would be a waste of cycles, because the
6597 iterator will never move back, and thus never cross this
6598 "fake" stop position. So we delay that backward search
6599 until the time we really need it, in next_element_from_buffer. */
6600 if (CHARPOS (pos) != it->prev_stop)
6601 it->prev_stop = CHARPOS (pos);
6602 if (CHARPOS (pos) < it->base_level_stop)
6603 it->base_level_stop = 0; /* meaning it's unknown */
6604 handle_stop (it);
6606 else
6608 handle_stop (it);
6609 it->prev_stop = it->base_level_stop = 0;
6614 CHECK_IT (it);
6618 /* Change IT's buffer position to POS. SET_STOP_P means set
6619 IT->stop_pos to POS, also. */
6621 static void
6622 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6624 /* Don't call this function when scanning a C string. */
6625 eassert (it->s == NULL);
6627 /* POS must be a reasonable value. */
6628 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6630 it->current.pos = it->position = pos;
6631 it->end_charpos = ZV;
6632 it->dpvec = NULL;
6633 it->current.dpvec_index = -1;
6634 it->current.overlay_string_index = -1;
6635 IT_STRING_CHARPOS (*it) = -1;
6636 IT_STRING_BYTEPOS (*it) = -1;
6637 it->string = Qnil;
6638 it->method = GET_FROM_BUFFER;
6639 it->object = it->w->contents;
6640 it->area = TEXT_AREA;
6641 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6642 it->sp = 0;
6643 it->string_from_display_prop_p = false;
6644 it->string_from_prefix_prop_p = false;
6646 it->from_disp_prop_p = false;
6647 it->face_before_selective_p = false;
6648 if (it->bidi_p)
6650 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6651 &it->bidi_it);
6652 bidi_unshelve_cache (NULL, false);
6653 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6654 it->bidi_it.string.s = NULL;
6655 it->bidi_it.string.lstring = Qnil;
6656 it->bidi_it.string.bufpos = 0;
6657 it->bidi_it.string.from_disp_str = false;
6658 it->bidi_it.string.unibyte = false;
6659 it->bidi_it.w = it->w;
6662 if (set_stop_p)
6664 it->stop_charpos = CHARPOS (pos);
6665 it->base_level_stop = CHARPOS (pos);
6667 /* This make the information stored in it->cmp_it invalidate. */
6668 it->cmp_it.id = -1;
6672 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6673 If S is non-null, it is a C string to iterate over. Otherwise,
6674 STRING gives a Lisp string to iterate over.
6676 If PRECISION > 0, don't return more then PRECISION number of
6677 characters from the string.
6679 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6680 characters have been returned. FIELD_WIDTH < 0 means an infinite
6681 field width.
6683 MULTIBYTE = 0 means disable processing of multibyte characters,
6684 MULTIBYTE > 0 means enable it,
6685 MULTIBYTE < 0 means use IT->multibyte_p.
6687 IT must be initialized via a prior call to init_iterator before
6688 calling this function. */
6690 static void
6691 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6692 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6693 int multibyte)
6695 /* No text property checks performed by default, but see below. */
6696 it->stop_charpos = -1;
6698 /* Set iterator position and end position. */
6699 memset (&it->current, 0, sizeof it->current);
6700 it->current.overlay_string_index = -1;
6701 it->current.dpvec_index = -1;
6702 eassert (charpos >= 0);
6704 /* If STRING is specified, use its multibyteness, otherwise use the
6705 setting of MULTIBYTE, if specified. */
6706 if (multibyte >= 0)
6707 it->multibyte_p = multibyte > 0;
6709 /* Bidirectional reordering of strings is controlled by the default
6710 value of bidi-display-reordering. Don't try to reorder while
6711 loading loadup.el, as the necessary character property tables are
6712 not yet available. */
6713 it->bidi_p =
6714 !redisplay__inhibit_bidi
6715 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6717 if (s == NULL)
6719 eassert (STRINGP (string));
6720 it->string = string;
6721 it->s = NULL;
6722 it->end_charpos = it->string_nchars = SCHARS (string);
6723 it->method = GET_FROM_STRING;
6724 it->current.string_pos = string_pos (charpos, string);
6726 if (it->bidi_p)
6728 it->bidi_it.string.lstring = string;
6729 it->bidi_it.string.s = NULL;
6730 it->bidi_it.string.schars = it->end_charpos;
6731 it->bidi_it.string.bufpos = 0;
6732 it->bidi_it.string.from_disp_str = false;
6733 it->bidi_it.string.unibyte = !it->multibyte_p;
6734 it->bidi_it.w = it->w;
6735 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6736 FRAME_WINDOW_P (it->f), &it->bidi_it);
6739 else
6741 it->s = (const unsigned char *) s;
6742 it->string = Qnil;
6744 /* Note that we use IT->current.pos, not it->current.string_pos,
6745 for displaying C strings. */
6746 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6747 if (it->multibyte_p)
6749 it->current.pos = c_string_pos (charpos, s, true);
6750 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6752 else
6754 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6755 it->end_charpos = it->string_nchars = strlen (s);
6758 if (it->bidi_p)
6760 it->bidi_it.string.lstring = Qnil;
6761 it->bidi_it.string.s = (const unsigned char *) s;
6762 it->bidi_it.string.schars = it->end_charpos;
6763 it->bidi_it.string.bufpos = 0;
6764 it->bidi_it.string.from_disp_str = false;
6765 it->bidi_it.string.unibyte = !it->multibyte_p;
6766 it->bidi_it.w = it->w;
6767 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6768 &it->bidi_it);
6770 it->method = GET_FROM_C_STRING;
6773 /* PRECISION > 0 means don't return more than PRECISION characters
6774 from the string. */
6775 if (precision > 0 && it->end_charpos - charpos > precision)
6777 it->end_charpos = it->string_nchars = charpos + precision;
6778 if (it->bidi_p)
6779 it->bidi_it.string.schars = it->end_charpos;
6782 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6783 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6784 FIELD_WIDTH < 0 means infinite field width. This is useful for
6785 padding with `-' at the end of a mode line. */
6786 if (field_width < 0)
6787 field_width = DISP_INFINITY;
6788 /* Implementation note: We deliberately don't enlarge
6789 it->bidi_it.string.schars here to fit it->end_charpos, because
6790 the bidi iterator cannot produce characters out of thin air. */
6791 if (field_width > it->end_charpos - charpos)
6792 it->end_charpos = charpos + field_width;
6794 /* Use the standard display table for displaying strings. */
6795 if (DISP_TABLE_P (Vstandard_display_table))
6796 it->dp = XCHAR_TABLE (Vstandard_display_table);
6798 it->stop_charpos = charpos;
6799 it->prev_stop = charpos;
6800 it->base_level_stop = 0;
6801 if (it->bidi_p)
6803 it->bidi_it.first_elt = true;
6804 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6805 it->bidi_it.disp_pos = -1;
6807 if (s == NULL && it->multibyte_p)
6809 ptrdiff_t endpos = SCHARS (it->string);
6810 if (endpos > it->end_charpos)
6811 endpos = it->end_charpos;
6812 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6813 it->string);
6815 CHECK_IT (it);
6820 /***********************************************************************
6821 Iteration
6822 ***********************************************************************/
6824 /* Map enum it_method value to corresponding next_element_from_* function. */
6826 typedef bool (*next_element_function) (struct it *);
6828 static next_element_function const get_next_element[NUM_IT_METHODS] =
6830 next_element_from_buffer,
6831 next_element_from_display_vector,
6832 next_element_from_string,
6833 next_element_from_c_string,
6834 next_element_from_image,
6835 next_element_from_stretch,
6836 next_element_from_xwidget,
6839 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6842 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6843 (possibly with the following characters). */
6845 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6846 ((IT)->cmp_it.id >= 0 \
6847 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6848 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6849 END_CHARPOS, (IT)->w, \
6850 FACE_FROM_ID_OR_NULL ((IT)->f, \
6851 (IT)->face_id), \
6852 (IT)->string)))
6855 /* Lookup the char-table Vglyphless_char_display for character C (-1
6856 if we want information for no-font case), and return the display
6857 method symbol. By side-effect, update it->what and
6858 it->glyphless_method. This function is called from
6859 get_next_display_element for each character element, and from
6860 x_produce_glyphs when no suitable font was found. */
6862 Lisp_Object
6863 lookup_glyphless_char_display (int c, struct it *it)
6865 Lisp_Object glyphless_method = Qnil;
6867 if (CHAR_TABLE_P (Vglyphless_char_display)
6868 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6870 if (c >= 0)
6872 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6873 if (CONSP (glyphless_method))
6874 glyphless_method = FRAME_WINDOW_P (it->f)
6875 ? XCAR (glyphless_method)
6876 : XCDR (glyphless_method);
6878 else
6879 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6882 retry:
6883 if (NILP (glyphless_method))
6885 if (c >= 0)
6886 /* The default is to display the character by a proper font. */
6887 return Qnil;
6888 /* The default for the no-font case is to display an empty box. */
6889 glyphless_method = Qempty_box;
6891 if (EQ (glyphless_method, Qzero_width))
6893 if (c >= 0)
6894 return glyphless_method;
6895 /* This method can't be used for the no-font case. */
6896 glyphless_method = Qempty_box;
6898 if (EQ (glyphless_method, Qthin_space))
6899 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6900 else if (EQ (glyphless_method, Qempty_box))
6901 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6902 else if (EQ (glyphless_method, Qhex_code))
6903 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6904 else if (STRINGP (glyphless_method))
6905 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6906 else
6908 /* Invalid value. We use the default method. */
6909 glyphless_method = Qnil;
6910 goto retry;
6912 it->what = IT_GLYPHLESS;
6913 return glyphless_method;
6916 /* Merge escape glyph face and cache the result. */
6918 static struct frame *last_escape_glyph_frame = NULL;
6919 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6920 static int last_escape_glyph_merged_face_id = 0;
6922 static int
6923 merge_escape_glyph_face (struct it *it)
6925 int face_id;
6927 if (it->f == last_escape_glyph_frame
6928 && it->face_id == last_escape_glyph_face_id)
6929 face_id = last_escape_glyph_merged_face_id;
6930 else
6932 /* Merge the `escape-glyph' face into the current face. */
6933 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6934 last_escape_glyph_frame = it->f;
6935 last_escape_glyph_face_id = it->face_id;
6936 last_escape_glyph_merged_face_id = face_id;
6938 return face_id;
6941 /* Likewise for glyphless glyph face. */
6943 static struct frame *last_glyphless_glyph_frame = NULL;
6944 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6945 static int last_glyphless_glyph_merged_face_id = 0;
6948 merge_glyphless_glyph_face (struct it *it)
6950 int face_id;
6952 if (it->f == last_glyphless_glyph_frame
6953 && it->face_id == last_glyphless_glyph_face_id)
6954 face_id = last_glyphless_glyph_merged_face_id;
6955 else
6957 /* Merge the `glyphless-char' face into the current face. */
6958 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6959 last_glyphless_glyph_frame = it->f;
6960 last_glyphless_glyph_face_id = it->face_id;
6961 last_glyphless_glyph_merged_face_id = face_id;
6963 return face_id;
6966 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6967 be called before redisplaying windows, and when the frame's face
6968 cache is freed. */
6969 void
6970 forget_escape_and_glyphless_faces (void)
6972 last_escape_glyph_frame = NULL;
6973 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6974 last_glyphless_glyph_frame = NULL;
6975 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6978 /* Load IT's display element fields with information about the next
6979 display element from the current position of IT. Value is false if
6980 end of buffer (or C string) is reached. */
6982 static bool
6983 get_next_display_element (struct it *it)
6985 /* True means that we found a display element. False means that
6986 we hit the end of what we iterate over. Performance note: the
6987 function pointer `method' used here turns out to be faster than
6988 using a sequence of if-statements. */
6989 bool success_p;
6991 get_next:
6992 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6994 if (it->what == IT_CHARACTER)
6996 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6997 and only if (a) the resolved directionality of that character
6998 is R..." */
6999 /* FIXME: Do we need an exception for characters from display
7000 tables? */
7001 if (it->bidi_p && it->bidi_it.type == STRONG_R
7002 && !inhibit_bidi_mirroring)
7003 it->c = bidi_mirror_char (it->c);
7004 /* Map via display table or translate control characters.
7005 IT->c, IT->len etc. have been set to the next character by
7006 the function call above. If we have a display table, and it
7007 contains an entry for IT->c, translate it. Don't do this if
7008 IT->c itself comes from a display table, otherwise we could
7009 end up in an infinite recursion. (An alternative could be to
7010 count the recursion depth of this function and signal an
7011 error when a certain maximum depth is reached.) Is it worth
7012 it? */
7013 if (success_p && it->dpvec == NULL)
7015 Lisp_Object dv;
7016 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7017 bool nonascii_space_p = false;
7018 bool nonascii_hyphen_p = false;
7019 int c = it->c; /* This is the character to display. */
7021 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7023 eassert (SINGLE_BYTE_CHAR_P (c));
7024 if (unibyte_display_via_language_environment)
7026 c = DECODE_CHAR (unibyte, c);
7027 if (c < 0)
7028 c = BYTE8_TO_CHAR (it->c);
7030 else
7031 c = BYTE8_TO_CHAR (it->c);
7034 if (it->dp
7035 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7036 VECTORP (dv)))
7038 struct Lisp_Vector *v = XVECTOR (dv);
7040 /* Return the first character from the display table
7041 entry, if not empty. If empty, don't display the
7042 current character. */
7043 if (v->header.size)
7045 it->dpvec_char_len = it->len;
7046 it->dpvec = v->contents;
7047 it->dpend = v->contents + v->header.size;
7048 it->current.dpvec_index = 0;
7049 it->dpvec_face_id = -1;
7050 it->saved_face_id = it->face_id;
7051 it->method = GET_FROM_DISPLAY_VECTOR;
7052 it->ellipsis_p = false;
7054 else
7056 set_iterator_to_next (it, false);
7058 goto get_next;
7061 if (! NILP (lookup_glyphless_char_display (c, it)))
7063 if (it->what == IT_GLYPHLESS)
7064 goto done;
7065 /* Don't display this character. */
7066 set_iterator_to_next (it, false);
7067 goto get_next;
7070 /* If `nobreak-char-display' is non-nil, we display
7071 non-ASCII spaces and hyphens specially. */
7072 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7074 if (c == NO_BREAK_SPACE)
7075 nonascii_space_p = true;
7076 else if (c == SOFT_HYPHEN || c == HYPHEN
7077 || c == NON_BREAKING_HYPHEN)
7078 nonascii_hyphen_p = true;
7081 /* Translate control characters into `\003' or `^C' form.
7082 Control characters coming from a display table entry are
7083 currently not translated because we use IT->dpvec to hold
7084 the translation. This could easily be changed but I
7085 don't believe that it is worth doing.
7087 The characters handled by `nobreak-char-display' must be
7088 translated too.
7090 Non-printable characters and raw-byte characters are also
7091 translated to octal or hexadecimal form. */
7092 if (((c < ' ' || c == 127) /* ASCII control chars. */
7093 ? (it->area != TEXT_AREA
7094 /* In mode line, treat \n, \t like other crl chars. */
7095 || (c != '\t'
7096 && it->glyph_row
7097 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7098 || (c != '\n' && c != '\t'))
7099 : (nonascii_space_p
7100 || nonascii_hyphen_p
7101 || CHAR_BYTE8_P (c)
7102 || ! CHAR_PRINTABLE_P (c))))
7104 /* C is a control character, non-ASCII space/hyphen,
7105 raw-byte, or a non-printable character which must be
7106 displayed either as '\003' or as `^C' where the '\\'
7107 and '^' can be defined in the display table. Fill
7108 IT->ctl_chars with glyphs for what we have to
7109 display. Then, set IT->dpvec to these glyphs. */
7110 Lisp_Object gc;
7111 int ctl_len;
7112 int face_id;
7113 int lface_id = 0;
7114 int escape_glyph;
7116 /* Handle control characters with ^. */
7118 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7120 int g;
7122 g = '^'; /* default glyph for Control */
7123 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7124 if (it->dp
7125 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7127 g = GLYPH_CODE_CHAR (gc);
7128 lface_id = GLYPH_CODE_FACE (gc);
7131 face_id = (lface_id
7132 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7133 : merge_escape_glyph_face (it));
7135 XSETINT (it->ctl_chars[0], g);
7136 XSETINT (it->ctl_chars[1], c ^ 0100);
7137 ctl_len = 2;
7138 goto display_control;
7141 /* Handle non-ascii space in the mode where it only gets
7142 highlighting. */
7144 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7146 /* Merge `nobreak-space' into the current face. */
7147 face_id = merge_faces (it->f, Qnobreak_space, 0,
7148 it->face_id);
7149 XSETINT (it->ctl_chars[0], ' ');
7150 ctl_len = 1;
7151 goto display_control;
7154 /* Handle non-ascii hyphens in the mode where it only
7155 gets highlighting. */
7157 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7159 /* Merge `nobreak-space' into the current face. */
7160 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7161 it->face_id);
7162 XSETINT (it->ctl_chars[0], '-');
7163 ctl_len = 1;
7164 goto display_control;
7167 /* Handle sequences that start with the "escape glyph". */
7169 /* the default escape glyph is \. */
7170 escape_glyph = '\\';
7172 if (it->dp
7173 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7175 escape_glyph = GLYPH_CODE_CHAR (gc);
7176 lface_id = GLYPH_CODE_FACE (gc);
7179 face_id = (lface_id
7180 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7181 : merge_escape_glyph_face (it));
7183 /* Draw non-ASCII space/hyphen with escape glyph: */
7185 if (nonascii_space_p || nonascii_hyphen_p)
7187 XSETINT (it->ctl_chars[0], escape_glyph);
7188 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7189 ctl_len = 2;
7190 goto display_control;
7194 char str[10];
7195 int len, i;
7197 if (CHAR_BYTE8_P (c))
7198 /* Display \200 or \x80 instead of \17777600. */
7199 c = CHAR_TO_BYTE8 (c);
7200 const char *format_string = display_raw_bytes_as_hex
7201 ? "x%02x"
7202 : "%03o";
7203 len = sprintf (str, format_string, c + 0u);
7205 XSETINT (it->ctl_chars[0], escape_glyph);
7206 for (i = 0; i < len; i++)
7207 XSETINT (it->ctl_chars[i + 1], str[i]);
7208 ctl_len = len + 1;
7211 display_control:
7212 /* Set up IT->dpvec and return first character from it. */
7213 it->dpvec_char_len = it->len;
7214 it->dpvec = it->ctl_chars;
7215 it->dpend = it->dpvec + ctl_len;
7216 it->current.dpvec_index = 0;
7217 it->dpvec_face_id = face_id;
7218 it->saved_face_id = it->face_id;
7219 it->method = GET_FROM_DISPLAY_VECTOR;
7220 it->ellipsis_p = false;
7221 goto get_next;
7223 it->char_to_display = c;
7225 else if (success_p)
7227 it->char_to_display = it->c;
7231 #ifdef HAVE_WINDOW_SYSTEM
7232 /* Adjust face id for a multibyte character. There are no multibyte
7233 character in unibyte text. */
7234 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7235 && it->multibyte_p
7236 && success_p
7237 && FRAME_WINDOW_P (it->f))
7239 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7241 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7243 /* Automatic composition with glyph-string. */
7244 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7246 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7248 else
7250 ptrdiff_t pos = (it->s ? -1
7251 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7252 : IT_CHARPOS (*it));
7253 int c;
7255 if (it->what == IT_CHARACTER)
7256 c = it->char_to_display;
7257 else
7259 struct composition *cmp = composition_table[it->cmp_it.id];
7260 int i;
7262 c = ' ';
7263 for (i = 0; i < cmp->glyph_len; i++)
7264 /* TAB in a composition means display glyphs with
7265 padding space on the left or right. */
7266 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7267 break;
7269 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7272 #endif /* HAVE_WINDOW_SYSTEM */
7274 done:
7275 /* Is this character the last one of a run of characters with
7276 box? If yes, set IT->end_of_box_run_p to true. */
7277 if (it->face_box_p
7278 && it->s == NULL)
7280 if (it->method == GET_FROM_STRING && it->sp)
7282 int face_id = underlying_face_id (it);
7283 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7285 if (face)
7287 if (face->box == FACE_NO_BOX)
7289 /* If the box comes from face properties in a
7290 display string, check faces in that string. */
7291 int string_face_id = face_after_it_pos (it);
7292 it->end_of_box_run_p
7293 = (FACE_FROM_ID (it->f, string_face_id)->box
7294 == FACE_NO_BOX);
7296 /* Otherwise, the box comes from the underlying face.
7297 If this is the last string character displayed, check
7298 the next buffer location. */
7299 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7300 /* n_overlay_strings is unreliable unless
7301 overlay_string_index is non-negative. */
7302 && ((it->current.overlay_string_index >= 0
7303 && (it->current.overlay_string_index
7304 == it->n_overlay_strings - 1))
7305 /* A string from display property. */
7306 || it->from_disp_prop_p))
7308 ptrdiff_t ignore;
7309 int next_face_id;
7310 bool text_from_string = false;
7311 /* Normally, the next buffer location is stored in
7312 IT->current.pos... */
7313 struct text_pos pos = it->current.pos;
7315 /* ...but for a string from a display property, the
7316 next buffer position is stored in the 'position'
7317 member of the iteration stack slot below the
7318 current one, see handle_single_display_spec. By
7319 contrast, it->current.pos was not yet updated to
7320 point to that buffer position; that will happen
7321 in pop_it, after we finish displaying the current
7322 string. Note that we already checked above that
7323 it->sp is positive, so subtracting one from it is
7324 safe. */
7325 if (it->from_disp_prop_p)
7327 int stackp = it->sp - 1;
7329 /* Find the stack level with data from buffer. */
7330 while (stackp >= 0
7331 && STRINGP ((it->stack + stackp)->string))
7332 stackp--;
7333 if (stackp < 0)
7335 /* If no stack slot was found for iterating
7336 a buffer, we are displaying text from a
7337 string, most probably the mode line or
7338 the header line, and that string has a
7339 display string on some of its
7340 characters. */
7341 text_from_string = true;
7342 pos = it->stack[it->sp - 1].position;
7344 else
7345 pos = (it->stack + stackp)->position;
7347 else
7348 INC_TEXT_POS (pos, it->multibyte_p);
7350 if (text_from_string)
7352 Lisp_Object base_string = it->stack[it->sp - 1].string;
7354 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7355 it->end_of_box_run_p = true;
7356 else
7358 next_face_id
7359 = face_at_string_position (it->w, base_string,
7360 CHARPOS (pos), 0,
7361 &ignore, face_id, false);
7362 it->end_of_box_run_p
7363 = (FACE_FROM_ID (it->f, next_face_id)->box
7364 == FACE_NO_BOX);
7367 else if (CHARPOS (pos) >= ZV)
7368 it->end_of_box_run_p = true;
7369 else
7371 next_face_id =
7372 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7373 CHARPOS (pos)
7374 + TEXT_PROP_DISTANCE_LIMIT,
7375 false, -1);
7376 it->end_of_box_run_p
7377 = (FACE_FROM_ID (it->f, next_face_id)->box
7378 == FACE_NO_BOX);
7383 /* next_element_from_display_vector sets this flag according to
7384 faces of the display vector glyphs, see there. */
7385 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7387 int face_id = face_after_it_pos (it);
7388 it->end_of_box_run_p
7389 = (face_id != it->face_id
7390 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7393 /* If we reached the end of the object we've been iterating (e.g., a
7394 display string or an overlay string), and there's something on
7395 IT->stack, proceed with what's on the stack. It doesn't make
7396 sense to return false if there's unprocessed stuff on the stack,
7397 because otherwise that stuff will never be displayed. */
7398 if (!success_p && it->sp > 0)
7400 set_iterator_to_next (it, false);
7401 success_p = get_next_display_element (it);
7404 /* Value is false if end of buffer or string reached. */
7405 return success_p;
7409 /* Move IT to the next display element.
7411 RESEAT_P means if called on a newline in buffer text,
7412 skip to the next visible line start.
7414 Functions get_next_display_element and set_iterator_to_next are
7415 separate because I find this arrangement easier to handle than a
7416 get_next_display_element function that also increments IT's
7417 position. The way it is we can first look at an iterator's current
7418 display element, decide whether it fits on a line, and if it does,
7419 increment the iterator position. The other way around we probably
7420 would either need a flag indicating whether the iterator has to be
7421 incremented the next time, or we would have to implement a
7422 decrement position function which would not be easy to write. */
7424 void
7425 set_iterator_to_next (struct it *it, bool reseat_p)
7427 /* Reset flags indicating start and end of a sequence of characters
7428 with box. Reset them at the start of this function because
7429 moving the iterator to a new position might set them. */
7430 it->start_of_box_run_p = it->end_of_box_run_p = false;
7432 switch (it->method)
7434 case GET_FROM_BUFFER:
7435 /* The current display element of IT is a character from
7436 current_buffer. Advance in the buffer, and maybe skip over
7437 invisible lines that are so because of selective display. */
7438 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7439 reseat_at_next_visible_line_start (it, false);
7440 else if (it->cmp_it.id >= 0)
7442 /* We are currently getting glyphs from a composition. */
7443 if (! it->bidi_p)
7445 IT_CHARPOS (*it) += it->cmp_it.nchars;
7446 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7448 else
7450 int i;
7452 /* Update IT's char/byte positions to point to the first
7453 character of the next grapheme cluster, or to the
7454 character visually after the current composition. */
7455 for (i = 0; i < it->cmp_it.nchars; i++)
7456 bidi_move_to_visually_next (&it->bidi_it);
7457 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7458 IT_CHARPOS (*it) = it->bidi_it.charpos;
7461 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7462 && it->cmp_it.to < it->cmp_it.nglyphs)
7464 /* Composition created while scanning forward. Proceed
7465 to the next grapheme cluster. */
7466 it->cmp_it.from = it->cmp_it.to;
7468 else if ((it->bidi_p && it->cmp_it.reversed_p)
7469 && it->cmp_it.from > 0)
7471 /* Composition created while scanning backward. Proceed
7472 to the previous grapheme cluster. */
7473 it->cmp_it.to = it->cmp_it.from;
7475 else
7477 /* No more grapheme clusters in this composition.
7478 Find the next stop position. */
7479 ptrdiff_t stop = it->end_charpos;
7481 if (it->bidi_it.scan_dir < 0)
7482 /* Now we are scanning backward and don't know
7483 where to stop. */
7484 stop = -1;
7485 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7486 IT_BYTEPOS (*it), stop, Qnil);
7489 else
7491 eassert (it->len != 0);
7493 if (!it->bidi_p)
7495 IT_BYTEPOS (*it) += it->len;
7496 IT_CHARPOS (*it) += 1;
7498 else
7500 int prev_scan_dir = it->bidi_it.scan_dir;
7501 /* If this is a new paragraph, determine its base
7502 direction (a.k.a. its base embedding level). */
7503 if (it->bidi_it.new_paragraph)
7504 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7505 false);
7506 bidi_move_to_visually_next (&it->bidi_it);
7507 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7508 IT_CHARPOS (*it) = it->bidi_it.charpos;
7509 if (prev_scan_dir != it->bidi_it.scan_dir)
7511 /* As the scan direction was changed, we must
7512 re-compute the stop position for composition. */
7513 ptrdiff_t stop = it->end_charpos;
7514 if (it->bidi_it.scan_dir < 0)
7515 stop = -1;
7516 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7517 IT_BYTEPOS (*it), stop, Qnil);
7520 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7522 break;
7524 case GET_FROM_C_STRING:
7525 /* Current display element of IT is from a C string. */
7526 if (!it->bidi_p
7527 /* If the string position is beyond string's end, it means
7528 next_element_from_c_string is padding the string with
7529 blanks, in which case we bypass the bidi iterator,
7530 because it cannot deal with such virtual characters. */
7531 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7533 IT_BYTEPOS (*it) += it->len;
7534 IT_CHARPOS (*it) += 1;
7536 else
7538 bidi_move_to_visually_next (&it->bidi_it);
7539 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7540 IT_CHARPOS (*it) = it->bidi_it.charpos;
7542 break;
7544 case GET_FROM_DISPLAY_VECTOR:
7545 /* Current display element of IT is from a display table entry.
7546 Advance in the display table definition. Reset it to null if
7547 end reached, and continue with characters from buffers/
7548 strings. */
7549 ++it->current.dpvec_index;
7551 /* Restore face of the iterator to what they were before the
7552 display vector entry (these entries may contain faces). */
7553 it->face_id = it->saved_face_id;
7555 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7557 bool recheck_faces = it->ellipsis_p;
7559 if (it->s)
7560 it->method = GET_FROM_C_STRING;
7561 else if (STRINGP (it->string))
7562 it->method = GET_FROM_STRING;
7563 else
7565 it->method = GET_FROM_BUFFER;
7566 it->object = it->w->contents;
7569 it->dpvec = NULL;
7570 it->current.dpvec_index = -1;
7572 /* Skip over characters which were displayed via IT->dpvec. */
7573 if (it->dpvec_char_len < 0)
7574 reseat_at_next_visible_line_start (it, true);
7575 else if (it->dpvec_char_len > 0)
7577 it->len = it->dpvec_char_len;
7578 set_iterator_to_next (it, reseat_p);
7581 /* Maybe recheck faces after display vector. */
7582 if (recheck_faces)
7584 if (it->method == GET_FROM_STRING)
7585 it->stop_charpos = IT_STRING_CHARPOS (*it);
7586 else
7587 it->stop_charpos = IT_CHARPOS (*it);
7590 break;
7592 case GET_FROM_STRING:
7593 /* Current display element is a character from a Lisp string. */
7594 eassert (it->s == NULL && STRINGP (it->string));
7595 /* Don't advance past string end. These conditions are true
7596 when set_iterator_to_next is called at the end of
7597 get_next_display_element, in which case the Lisp string is
7598 already exhausted, and all we want is pop the iterator
7599 stack. */
7600 if (it->current.overlay_string_index >= 0)
7602 /* This is an overlay string, so there's no padding with
7603 spaces, and the number of characters in the string is
7604 where the string ends. */
7605 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7606 goto consider_string_end;
7608 else
7610 /* Not an overlay string. There could be padding, so test
7611 against it->end_charpos. */
7612 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7613 goto consider_string_end;
7615 if (it->cmp_it.id >= 0)
7617 /* We are delivering display elements from a composition.
7618 Update the string position past the grapheme cluster
7619 we've just processed. */
7620 if (! it->bidi_p)
7622 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7623 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7625 else
7627 int i;
7629 for (i = 0; i < it->cmp_it.nchars; i++)
7630 bidi_move_to_visually_next (&it->bidi_it);
7631 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7632 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7635 /* Did we exhaust all the grapheme clusters of this
7636 composition? */
7637 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7638 && (it->cmp_it.to < it->cmp_it.nglyphs))
7640 /* Not all the grapheme clusters were processed yet;
7641 advance to the next cluster. */
7642 it->cmp_it.from = it->cmp_it.to;
7644 else if ((it->bidi_p && it->cmp_it.reversed_p)
7645 && it->cmp_it.from > 0)
7647 /* Likewise: advance to the next cluster, but going in
7648 the reverse direction. */
7649 it->cmp_it.to = it->cmp_it.from;
7651 else
7653 /* This composition was fully processed; find the next
7654 candidate place for checking for composed
7655 characters. */
7656 /* Always limit string searches to the string length;
7657 any padding spaces are not part of the string, and
7658 there cannot be any compositions in that padding. */
7659 ptrdiff_t stop = SCHARS (it->string);
7661 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7662 stop = -1;
7663 else if (it->end_charpos < stop)
7665 /* Cf. PRECISION in reseat_to_string: we might be
7666 limited in how many of the string characters we
7667 need to deliver. */
7668 stop = it->end_charpos;
7670 composition_compute_stop_pos (&it->cmp_it,
7671 IT_STRING_CHARPOS (*it),
7672 IT_STRING_BYTEPOS (*it), stop,
7673 it->string);
7676 else
7678 if (!it->bidi_p
7679 /* If the string position is beyond string's end, it
7680 means next_element_from_string is padding the string
7681 with blanks, in which case we bypass the bidi
7682 iterator, because it cannot deal with such virtual
7683 characters. */
7684 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7686 IT_STRING_BYTEPOS (*it) += it->len;
7687 IT_STRING_CHARPOS (*it) += 1;
7689 else
7691 int prev_scan_dir = it->bidi_it.scan_dir;
7693 bidi_move_to_visually_next (&it->bidi_it);
7694 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7695 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7696 /* If the scan direction changes, we may need to update
7697 the place where to check for composed characters. */
7698 if (prev_scan_dir != it->bidi_it.scan_dir)
7700 ptrdiff_t stop = SCHARS (it->string);
7702 if (it->bidi_it.scan_dir < 0)
7703 stop = -1;
7704 else if (it->end_charpos < stop)
7705 stop = it->end_charpos;
7707 composition_compute_stop_pos (&it->cmp_it,
7708 IT_STRING_CHARPOS (*it),
7709 IT_STRING_BYTEPOS (*it), stop,
7710 it->string);
7715 consider_string_end:
7717 if (it->current.overlay_string_index >= 0)
7719 /* IT->string is an overlay string. Advance to the
7720 next, if there is one. */
7721 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7723 it->ellipsis_p = false;
7724 next_overlay_string (it);
7725 if (it->ellipsis_p)
7726 setup_for_ellipsis (it, 0);
7729 else
7731 /* IT->string is not an overlay string. If we reached
7732 its end, and there is something on IT->stack, proceed
7733 with what is on the stack. This can be either another
7734 string, this time an overlay string, or a buffer. */
7735 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7736 && it->sp > 0)
7738 pop_it (it);
7739 if (it->method == GET_FROM_STRING)
7740 goto consider_string_end;
7743 break;
7745 case GET_FROM_IMAGE:
7746 case GET_FROM_STRETCH:
7747 case GET_FROM_XWIDGET:
7749 /* The position etc with which we have to proceed are on
7750 the stack. The position may be at the end of a string,
7751 if the `display' property takes up the whole string. */
7752 eassert (it->sp > 0);
7753 pop_it (it);
7754 if (it->method == GET_FROM_STRING)
7755 goto consider_string_end;
7756 break;
7758 default:
7759 /* There are no other methods defined, so this should be a bug. */
7760 emacs_abort ();
7763 eassert (it->method != GET_FROM_STRING
7764 || (STRINGP (it->string)
7765 && IT_STRING_CHARPOS (*it) >= 0));
7768 /* Load IT's display element fields with information about the next
7769 display element which comes from a display table entry or from the
7770 result of translating a control character to one of the forms `^C'
7771 or `\003'.
7773 IT->dpvec holds the glyphs to return as characters.
7774 IT->saved_face_id holds the face id before the display vector--it
7775 is restored into IT->face_id in set_iterator_to_next. */
7777 static bool
7778 next_element_from_display_vector (struct it *it)
7780 Lisp_Object gc;
7781 int prev_face_id = it->face_id;
7782 int next_face_id;
7784 /* Precondition. */
7785 eassert (it->dpvec && it->current.dpvec_index >= 0);
7787 it->face_id = it->saved_face_id;
7789 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7790 That seemed totally bogus - so I changed it... */
7791 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7792 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7794 struct face *this_face, *prev_face, *next_face;
7796 it->c = GLYPH_CODE_CHAR (gc);
7797 it->len = CHAR_BYTES (it->c);
7799 /* The entry may contain a face id to use. Such a face id is
7800 the id of a Lisp face, not a realized face. A face id of
7801 zero means no face is specified. */
7802 if (it->dpvec_face_id >= 0)
7803 it->face_id = it->dpvec_face_id;
7804 else
7806 int lface_id = GLYPH_CODE_FACE (gc);
7807 if (lface_id > 0)
7808 it->face_id = merge_faces (it->f, Qt, lface_id,
7809 it->saved_face_id);
7812 /* Glyphs in the display vector could have the box face, so we
7813 need to set the related flags in the iterator, as
7814 appropriate. */
7815 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7816 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7818 /* Is this character the first character of a box-face run? */
7819 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7820 && (!prev_face
7821 || prev_face->box == FACE_NO_BOX));
7823 /* For the last character of the box-face run, we need to look
7824 either at the next glyph from the display vector, or at the
7825 face we saw before the display vector. */
7826 next_face_id = it->saved_face_id;
7827 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7829 if (it->dpvec_face_id >= 0)
7830 next_face_id = it->dpvec_face_id;
7831 else
7833 int lface_id =
7834 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7836 if (lface_id > 0)
7837 next_face_id = merge_faces (it->f, Qt, lface_id,
7838 it->saved_face_id);
7841 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7842 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7843 && (!next_face
7844 || next_face->box == FACE_NO_BOX));
7845 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7847 else
7848 /* Display table entry is invalid. Return a space. */
7849 it->c = ' ', it->len = 1;
7851 /* Don't change position and object of the iterator here. They are
7852 still the values of the character that had this display table
7853 entry or was translated, and that's what we want. */
7854 it->what = IT_CHARACTER;
7855 return true;
7858 /* Get the first element of string/buffer in the visual order, after
7859 being reseated to a new position in a string or a buffer. */
7860 static void
7861 get_visually_first_element (struct it *it)
7863 bool string_p = STRINGP (it->string) || it->s;
7864 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7865 ptrdiff_t bob = (string_p ? 0 : BEGV);
7867 if (STRINGP (it->string))
7869 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7870 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7872 else
7874 it->bidi_it.charpos = IT_CHARPOS (*it);
7875 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7878 if (it->bidi_it.charpos == eob)
7880 /* Nothing to do, but reset the FIRST_ELT flag, like
7881 bidi_paragraph_init does, because we are not going to
7882 call it. */
7883 it->bidi_it.first_elt = false;
7885 else if (it->bidi_it.charpos == bob
7886 || (!string_p
7887 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7888 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7890 /* If we are at the beginning of a line/string, we can produce
7891 the next element right away. */
7892 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7893 bidi_move_to_visually_next (&it->bidi_it);
7895 else
7897 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7899 /* We need to prime the bidi iterator starting at the line's or
7900 string's beginning, before we will be able to produce the
7901 next element. */
7902 if (string_p)
7903 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7904 else
7905 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7906 IT_BYTEPOS (*it), -1,
7907 &it->bidi_it.bytepos);
7908 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7911 /* Now return to buffer/string position where we were asked
7912 to get the next display element, and produce that. */
7913 bidi_move_to_visually_next (&it->bidi_it);
7915 while (it->bidi_it.bytepos != orig_bytepos
7916 && it->bidi_it.charpos < eob);
7919 /* Adjust IT's position information to where we ended up. */
7920 if (STRINGP (it->string))
7922 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7923 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7925 else
7927 IT_CHARPOS (*it) = it->bidi_it.charpos;
7928 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7931 if (STRINGP (it->string) || !it->s)
7933 ptrdiff_t stop, charpos, bytepos;
7935 if (STRINGP (it->string))
7937 eassert (!it->s);
7938 stop = SCHARS (it->string);
7939 if (stop > it->end_charpos)
7940 stop = it->end_charpos;
7941 charpos = IT_STRING_CHARPOS (*it);
7942 bytepos = IT_STRING_BYTEPOS (*it);
7944 else
7946 stop = it->end_charpos;
7947 charpos = IT_CHARPOS (*it);
7948 bytepos = IT_BYTEPOS (*it);
7950 if (it->bidi_it.scan_dir < 0)
7951 stop = -1;
7952 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7953 it->string);
7957 /* Load IT with the next display element from Lisp string IT->string.
7958 IT->current.string_pos is the current position within the string.
7959 If IT->current.overlay_string_index >= 0, the Lisp string is an
7960 overlay string. */
7962 static bool
7963 next_element_from_string (struct it *it)
7965 struct text_pos position;
7967 eassert (STRINGP (it->string));
7968 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7969 eassert (IT_STRING_CHARPOS (*it) >= 0);
7970 position = it->current.string_pos;
7972 /* With bidi reordering, the character to display might not be the
7973 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7974 that we were reseat()ed to a new string, whose paragraph
7975 direction is not known. */
7976 if (it->bidi_p && it->bidi_it.first_elt)
7978 get_visually_first_element (it);
7979 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7982 /* Time to check for invisible text? */
7983 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7985 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7987 if (!(!it->bidi_p
7988 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7989 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7991 /* With bidi non-linear iteration, we could find
7992 ourselves far beyond the last computed stop_charpos,
7993 with several other stop positions in between that we
7994 missed. Scan them all now, in buffer's logical
7995 order, until we find and handle the last stop_charpos
7996 that precedes our current position. */
7997 handle_stop_backwards (it, it->stop_charpos);
7998 return GET_NEXT_DISPLAY_ELEMENT (it);
8000 else
8002 if (it->bidi_p)
8004 /* Take note of the stop position we just moved
8005 across, for when we will move back across it. */
8006 it->prev_stop = it->stop_charpos;
8007 /* If we are at base paragraph embedding level, take
8008 note of the last stop position seen at this
8009 level. */
8010 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8011 it->base_level_stop = it->stop_charpos;
8013 handle_stop (it);
8015 /* Since a handler may have changed IT->method, we must
8016 recurse here. */
8017 return GET_NEXT_DISPLAY_ELEMENT (it);
8020 else if (it->bidi_p
8021 /* If we are before prev_stop, we may have overstepped
8022 on our way backwards a stop_pos, and if so, we need
8023 to handle that stop_pos. */
8024 && IT_STRING_CHARPOS (*it) < it->prev_stop
8025 /* We can sometimes back up for reasons that have nothing
8026 to do with bidi reordering. E.g., compositions. The
8027 code below is only needed when we are above the base
8028 embedding level, so test for that explicitly. */
8029 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8031 /* If we lost track of base_level_stop, we have no better
8032 place for handle_stop_backwards to start from than string
8033 beginning. This happens, e.g., when we were reseated to
8034 the previous screenful of text by vertical-motion. */
8035 if (it->base_level_stop <= 0
8036 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8037 it->base_level_stop = 0;
8038 handle_stop_backwards (it, it->base_level_stop);
8039 return GET_NEXT_DISPLAY_ELEMENT (it);
8043 if (it->current.overlay_string_index >= 0)
8045 /* Get the next character from an overlay string. In overlay
8046 strings, there is no field width or padding with spaces to
8047 do. */
8048 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8050 it->what = IT_EOB;
8051 return false;
8053 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8054 IT_STRING_BYTEPOS (*it),
8055 it->bidi_it.scan_dir < 0
8056 ? -1
8057 : SCHARS (it->string))
8058 && next_element_from_composition (it))
8060 return true;
8062 else if (STRING_MULTIBYTE (it->string))
8064 const unsigned char *s = (SDATA (it->string)
8065 + IT_STRING_BYTEPOS (*it));
8066 it->c = string_char_and_length (s, &it->len);
8068 else
8070 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8071 it->len = 1;
8074 else
8076 /* Get the next character from a Lisp string that is not an
8077 overlay string. Such strings come from the mode line, for
8078 example. We may have to pad with spaces, or truncate the
8079 string. See also next_element_from_c_string. */
8080 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8082 it->what = IT_EOB;
8083 return false;
8085 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8087 /* Pad with spaces. */
8088 it->c = ' ', it->len = 1;
8089 CHARPOS (position) = BYTEPOS (position) = -1;
8091 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8092 IT_STRING_BYTEPOS (*it),
8093 it->bidi_it.scan_dir < 0
8094 ? -1
8095 : it->string_nchars)
8096 && next_element_from_composition (it))
8098 return true;
8100 else if (STRING_MULTIBYTE (it->string))
8102 const unsigned char *s = (SDATA (it->string)
8103 + IT_STRING_BYTEPOS (*it));
8104 it->c = string_char_and_length (s, &it->len);
8106 else
8108 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8109 it->len = 1;
8113 /* Record what we have and where it came from. */
8114 it->what = IT_CHARACTER;
8115 it->object = it->string;
8116 it->position = position;
8117 return true;
8121 /* Load IT with next display element from C string IT->s.
8122 IT->string_nchars is the maximum number of characters to return
8123 from the string. IT->end_charpos may be greater than
8124 IT->string_nchars when this function is called, in which case we
8125 may have to return padding spaces. Value is false if end of string
8126 reached, including padding spaces. */
8128 static bool
8129 next_element_from_c_string (struct it *it)
8131 bool success_p = true;
8133 eassert (it->s);
8134 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8135 it->what = IT_CHARACTER;
8136 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8137 it->object = make_number (0);
8139 /* With bidi reordering, the character to display might not be the
8140 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8141 we were reseated to a new string, whose paragraph direction is
8142 not known. */
8143 if (it->bidi_p && it->bidi_it.first_elt)
8144 get_visually_first_element (it);
8146 /* IT's position can be greater than IT->string_nchars in case a
8147 field width or precision has been specified when the iterator was
8148 initialized. */
8149 if (IT_CHARPOS (*it) >= it->end_charpos)
8151 /* End of the game. */
8152 it->what = IT_EOB;
8153 success_p = false;
8155 else if (IT_CHARPOS (*it) >= it->string_nchars)
8157 /* Pad with spaces. */
8158 it->c = ' ', it->len = 1;
8159 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8161 else if (it->multibyte_p)
8162 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8163 else
8164 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8166 return success_p;
8170 /* Set up IT to return characters from an ellipsis, if appropriate.
8171 The definition of the ellipsis glyphs may come from a display table
8172 entry. This function fills IT with the first glyph from the
8173 ellipsis if an ellipsis is to be displayed. */
8175 static bool
8176 next_element_from_ellipsis (struct it *it)
8178 if (it->selective_display_ellipsis_p)
8179 setup_for_ellipsis (it, it->len);
8180 else
8182 /* The face at the current position may be different from the
8183 face we find after the invisible text. Remember what it
8184 was in IT->saved_face_id, and signal that it's there by
8185 setting face_before_selective_p. */
8186 it->saved_face_id = it->face_id;
8187 it->method = GET_FROM_BUFFER;
8188 it->object = it->w->contents;
8189 reseat_at_next_visible_line_start (it, true);
8190 it->face_before_selective_p = true;
8193 return GET_NEXT_DISPLAY_ELEMENT (it);
8197 /* Deliver an image display element. The iterator IT is already
8198 filled with image information (done in handle_display_prop). Value
8199 is always true. */
8202 static bool
8203 next_element_from_image (struct it *it)
8205 it->what = IT_IMAGE;
8206 return true;
8209 static bool
8210 next_element_from_xwidget (struct it *it)
8212 it->what = IT_XWIDGET;
8213 return true;
8217 /* Fill iterator IT with next display element from a stretch glyph
8218 property. IT->object is the value of the text property. Value is
8219 always true. */
8221 static bool
8222 next_element_from_stretch (struct it *it)
8224 it->what = IT_STRETCH;
8225 return true;
8228 /* Scan backwards from IT's current position until we find a stop
8229 position, or until BEGV. This is called when we find ourself
8230 before both the last known prev_stop and base_level_stop while
8231 reordering bidirectional text. */
8233 static void
8234 compute_stop_pos_backwards (struct it *it)
8236 const int SCAN_BACK_LIMIT = 1000;
8237 struct text_pos pos;
8238 struct display_pos save_current = it->current;
8239 struct text_pos save_position = it->position;
8240 ptrdiff_t charpos = IT_CHARPOS (*it);
8241 ptrdiff_t where_we_are = charpos;
8242 ptrdiff_t save_stop_pos = it->stop_charpos;
8243 ptrdiff_t save_end_pos = it->end_charpos;
8245 eassert (NILP (it->string) && !it->s);
8246 eassert (it->bidi_p);
8247 it->bidi_p = false;
8250 it->end_charpos = min (charpos + 1, ZV);
8251 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8252 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8253 reseat_1 (it, pos, false);
8254 compute_stop_pos (it);
8255 /* We must advance forward, right? */
8256 if (it->stop_charpos <= charpos)
8257 emacs_abort ();
8259 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8261 if (it->stop_charpos <= where_we_are)
8262 it->prev_stop = it->stop_charpos;
8263 else
8264 it->prev_stop = BEGV;
8265 it->bidi_p = true;
8266 it->current = save_current;
8267 it->position = save_position;
8268 it->stop_charpos = save_stop_pos;
8269 it->end_charpos = save_end_pos;
8272 /* Scan forward from CHARPOS in the current buffer/string, until we
8273 find a stop position > current IT's position. Then handle the stop
8274 position before that. This is called when we bump into a stop
8275 position while reordering bidirectional text. CHARPOS should be
8276 the last previously processed stop_pos (or BEGV/0, if none were
8277 processed yet) whose position is less that IT's current
8278 position. */
8280 static void
8281 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8283 bool bufp = !STRINGP (it->string);
8284 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8285 struct display_pos save_current = it->current;
8286 struct text_pos save_position = it->position;
8287 struct text_pos pos1;
8288 ptrdiff_t next_stop;
8290 /* Scan in strict logical order. */
8291 eassert (it->bidi_p);
8292 it->bidi_p = false;
8295 it->prev_stop = charpos;
8296 if (bufp)
8298 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8299 reseat_1 (it, pos1, false);
8301 else
8302 it->current.string_pos = string_pos (charpos, it->string);
8303 compute_stop_pos (it);
8304 /* We must advance forward, right? */
8305 if (it->stop_charpos <= it->prev_stop)
8306 emacs_abort ();
8307 charpos = it->stop_charpos;
8309 while (charpos <= where_we_are);
8311 it->bidi_p = true;
8312 it->current = save_current;
8313 it->position = save_position;
8314 next_stop = it->stop_charpos;
8315 it->stop_charpos = it->prev_stop;
8316 handle_stop (it);
8317 it->stop_charpos = next_stop;
8320 /* Load IT with the next display element from current_buffer. Value
8321 is false if end of buffer reached. IT->stop_charpos is the next
8322 position at which to stop and check for text properties or buffer
8323 end. */
8325 static bool
8326 next_element_from_buffer (struct it *it)
8328 bool success_p = true;
8330 eassert (IT_CHARPOS (*it) >= BEGV);
8331 eassert (NILP (it->string) && !it->s);
8332 eassert (!it->bidi_p
8333 || (EQ (it->bidi_it.string.lstring, Qnil)
8334 && it->bidi_it.string.s == NULL));
8336 /* With bidi reordering, the character to display might not be the
8337 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8338 we were reseat()ed to a new buffer position, which is potentially
8339 a different paragraph. */
8340 if (it->bidi_p && it->bidi_it.first_elt)
8342 get_visually_first_element (it);
8343 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8346 if (IT_CHARPOS (*it) >= it->stop_charpos)
8348 if (IT_CHARPOS (*it) >= it->end_charpos)
8350 bool overlay_strings_follow_p;
8352 /* End of the game, except when overlay strings follow that
8353 haven't been returned yet. */
8354 if (it->overlay_strings_at_end_processed_p)
8355 overlay_strings_follow_p = false;
8356 else
8358 it->overlay_strings_at_end_processed_p = true;
8359 overlay_strings_follow_p = get_overlay_strings (it, 0);
8362 if (overlay_strings_follow_p)
8363 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8364 else
8366 it->what = IT_EOB;
8367 it->position = it->current.pos;
8368 success_p = false;
8371 else if (!(!it->bidi_p
8372 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8373 || IT_CHARPOS (*it) == it->stop_charpos))
8375 /* With bidi non-linear iteration, we could find ourselves
8376 far beyond the last computed stop_charpos, with several
8377 other stop positions in between that we missed. Scan
8378 them all now, in buffer's logical order, until we find
8379 and handle the last stop_charpos that precedes our
8380 current position. */
8381 handle_stop_backwards (it, it->stop_charpos);
8382 it->ignore_overlay_strings_at_pos_p = false;
8383 return GET_NEXT_DISPLAY_ELEMENT (it);
8385 else
8387 if (it->bidi_p)
8389 /* Take note of the stop position we just moved across,
8390 for when we will move back across it. */
8391 it->prev_stop = it->stop_charpos;
8392 /* If we are at base paragraph embedding level, take
8393 note of the last stop position seen at this
8394 level. */
8395 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8396 it->base_level_stop = it->stop_charpos;
8398 handle_stop (it);
8399 it->ignore_overlay_strings_at_pos_p = false;
8400 return GET_NEXT_DISPLAY_ELEMENT (it);
8403 else if (it->bidi_p
8404 /* If we are before prev_stop, we may have overstepped on
8405 our way backwards a stop_pos, and if so, we need to
8406 handle that stop_pos. */
8407 && IT_CHARPOS (*it) < it->prev_stop
8408 /* We can sometimes back up for reasons that have nothing
8409 to do with bidi reordering. E.g., compositions. The
8410 code below is only needed when we are above the base
8411 embedding level, so test for that explicitly. */
8412 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8414 if (it->base_level_stop <= 0
8415 || IT_CHARPOS (*it) < it->base_level_stop)
8417 /* If we lost track of base_level_stop, we need to find
8418 prev_stop by looking backwards. This happens, e.g., when
8419 we were reseated to the previous screenful of text by
8420 vertical-motion. */
8421 it->base_level_stop = BEGV;
8422 compute_stop_pos_backwards (it);
8423 handle_stop_backwards (it, it->prev_stop);
8425 else
8426 handle_stop_backwards (it, it->base_level_stop);
8427 it->ignore_overlay_strings_at_pos_p = false;
8428 return GET_NEXT_DISPLAY_ELEMENT (it);
8430 else
8432 /* No face changes, overlays etc. in sight, so just return a
8433 character from current_buffer. */
8434 unsigned char *p;
8435 ptrdiff_t stop;
8437 /* We moved to the next buffer position, so any info about
8438 previously seen overlays is no longer valid. */
8439 it->ignore_overlay_strings_at_pos_p = false;
8441 /* Maybe run the redisplay end trigger hook. Performance note:
8442 This doesn't seem to cost measurable time. */
8443 if (it->redisplay_end_trigger_charpos
8444 && it->glyph_row
8445 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8446 run_redisplay_end_trigger_hook (it);
8448 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8449 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8450 stop)
8451 && next_element_from_composition (it))
8453 return true;
8456 /* Get the next character, maybe multibyte. */
8457 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8458 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8459 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8460 else
8461 it->c = *p, it->len = 1;
8463 /* Record what we have and where it came from. */
8464 it->what = IT_CHARACTER;
8465 it->object = it->w->contents;
8466 it->position = it->current.pos;
8468 /* Normally we return the character found above, except when we
8469 really want to return an ellipsis for selective display. */
8470 if (it->selective)
8472 if (it->c == '\n')
8474 /* A value of selective > 0 means hide lines indented more
8475 than that number of columns. */
8476 if (it->selective > 0
8477 && IT_CHARPOS (*it) + 1 < ZV
8478 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8479 IT_BYTEPOS (*it) + 1,
8480 it->selective))
8482 success_p = next_element_from_ellipsis (it);
8483 it->dpvec_char_len = -1;
8486 else if (it->c == '\r' && it->selective == -1)
8488 /* A value of selective == -1 means that everything from the
8489 CR to the end of the line is invisible, with maybe an
8490 ellipsis displayed for it. */
8491 success_p = next_element_from_ellipsis (it);
8492 it->dpvec_char_len = -1;
8497 /* Value is false if end of buffer reached. */
8498 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8499 return success_p;
8503 /* Run the redisplay end trigger hook for IT. */
8505 static void
8506 run_redisplay_end_trigger_hook (struct it *it)
8508 /* IT->glyph_row should be non-null, i.e. we should be actually
8509 displaying something, or otherwise we should not run the hook. */
8510 eassert (it->glyph_row);
8512 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8513 it->redisplay_end_trigger_charpos = 0;
8515 /* Since we are *trying* to run these functions, don't try to run
8516 them again, even if they get an error. */
8517 wset_redisplay_end_trigger (it->w, Qnil);
8518 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8519 make_number (charpos));
8521 /* Notice if it changed the face of the character we are on. */
8522 handle_face_prop (it);
8526 /* Deliver a composition display element. Unlike the other
8527 next_element_from_XXX, this function is not registered in the array
8528 get_next_element[]. It is called from next_element_from_buffer and
8529 next_element_from_string when necessary. */
8531 static bool
8532 next_element_from_composition (struct it *it)
8534 it->what = IT_COMPOSITION;
8535 it->len = it->cmp_it.nbytes;
8536 if (STRINGP (it->string))
8538 if (it->c < 0)
8540 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8541 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8542 return false;
8544 it->position = it->current.string_pos;
8545 it->object = it->string;
8546 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8547 IT_STRING_BYTEPOS (*it), it->string);
8549 else
8551 if (it->c < 0)
8553 IT_CHARPOS (*it) += it->cmp_it.nchars;
8554 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8555 if (it->bidi_p)
8557 if (it->bidi_it.new_paragraph)
8558 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8559 false);
8560 /* Resync the bidi iterator with IT's new position.
8561 FIXME: this doesn't support bidirectional text. */
8562 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8563 bidi_move_to_visually_next (&it->bidi_it);
8565 return false;
8567 it->position = it->current.pos;
8568 it->object = it->w->contents;
8569 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8570 IT_BYTEPOS (*it), Qnil);
8572 return true;
8577 /***********************************************************************
8578 Moving an iterator without producing glyphs
8579 ***********************************************************************/
8581 /* Check if iterator is at a position corresponding to a valid buffer
8582 position after some move_it_ call. */
8584 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8585 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8588 /* Move iterator IT to a specified buffer or X position within one
8589 line on the display without producing glyphs.
8591 OP should be a bit mask including some or all of these bits:
8592 MOVE_TO_X: Stop upon reaching x-position TO_X.
8593 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8594 Regardless of OP's value, stop upon reaching the end of the display line.
8596 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8597 This means, in particular, that TO_X includes window's horizontal
8598 scroll amount.
8600 The return value has several possible values that
8601 say what condition caused the scan to stop:
8603 MOVE_POS_MATCH_OR_ZV
8604 - when TO_POS or ZV was reached.
8606 MOVE_X_REACHED
8607 -when TO_X was reached before TO_POS or ZV were reached.
8609 MOVE_LINE_CONTINUED
8610 - when we reached the end of the display area and the line must
8611 be continued.
8613 MOVE_LINE_TRUNCATED
8614 - when we reached the end of the display area and the line is
8615 truncated.
8617 MOVE_NEWLINE_OR_CR
8618 - when we stopped at a line end, i.e. a newline or a CR and selective
8619 display is on. */
8621 static enum move_it_result
8622 move_it_in_display_line_to (struct it *it,
8623 ptrdiff_t to_charpos, int to_x,
8624 enum move_operation_enum op)
8626 enum move_it_result result = MOVE_UNDEFINED;
8627 struct glyph_row *saved_glyph_row;
8628 struct it wrap_it, atpos_it, atx_it, ppos_it;
8629 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8630 void *ppos_data = NULL;
8631 bool may_wrap = false;
8632 enum it_method prev_method = it->method;
8633 ptrdiff_t closest_pos UNINIT;
8634 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8635 bool saw_smaller_pos = prev_pos < to_charpos;
8636 bool line_number_pending = false;
8638 /* Don't produce glyphs in produce_glyphs. */
8639 saved_glyph_row = it->glyph_row;
8640 it->glyph_row = NULL;
8642 /* Use wrap_it to save a copy of IT wherever a word wrap could
8643 occur. Use atpos_it to save a copy of IT at the desired buffer
8644 position, if found, so that we can scan ahead and check if the
8645 word later overshoots the window edge. Use atx_it similarly, for
8646 pixel positions. */
8647 wrap_it.sp = -1;
8648 atpos_it.sp = -1;
8649 atx_it.sp = -1;
8651 /* Use ppos_it under bidi reordering to save a copy of IT for the
8652 initial position. We restore that position in IT when we have
8653 scanned the entire display line without finding a match for
8654 TO_CHARPOS and all the character positions are greater than
8655 TO_CHARPOS. We then restart the scan from the initial position,
8656 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8657 the closest to TO_CHARPOS. */
8658 if (it->bidi_p)
8660 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8662 SAVE_IT (ppos_it, *it, ppos_data);
8663 closest_pos = IT_CHARPOS (*it);
8665 else
8666 closest_pos = ZV;
8669 #define BUFFER_POS_REACHED_P() \
8670 ((op & MOVE_TO_POS) != 0 \
8671 && BUFFERP (it->object) \
8672 && (IT_CHARPOS (*it) == to_charpos \
8673 || ((!it->bidi_p \
8674 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8675 && IT_CHARPOS (*it) > to_charpos) \
8676 || (it->what == IT_COMPOSITION \
8677 && ((IT_CHARPOS (*it) > to_charpos \
8678 && to_charpos >= it->cmp_it.charpos) \
8679 || (IT_CHARPOS (*it) < to_charpos \
8680 && to_charpos <= it->cmp_it.charpos)))) \
8681 && (it->method == GET_FROM_BUFFER \
8682 || (it->method == GET_FROM_DISPLAY_VECTOR \
8683 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8685 if (it->hpos == 0)
8687 /* If line numbers are being displayed, produce a line number. */
8688 if (should_produce_line_number (it))
8690 if (it->current_x == it->first_visible_x)
8691 maybe_produce_line_number (it);
8692 else
8693 line_number_pending = true;
8695 /* If there's a line-/wrap-prefix, handle it. */
8696 if (it->method == GET_FROM_BUFFER)
8697 handle_line_prefix (it);
8700 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8701 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8703 while (true)
8705 int x, i, ascent = 0, descent = 0;
8707 /* Utility macro to reset an iterator with x, ascent, and descent. */
8708 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8709 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8710 (IT)->max_descent = descent)
8712 /* Stop if we move beyond TO_CHARPOS (after an image or a
8713 display string or stretch glyph). */
8714 if ((op & MOVE_TO_POS) != 0
8715 && BUFFERP (it->object)
8716 && it->method == GET_FROM_BUFFER
8717 && (((!it->bidi_p
8718 /* When the iterator is at base embedding level, we
8719 are guaranteed that characters are delivered for
8720 display in strictly increasing order of their
8721 buffer positions. */
8722 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8723 && IT_CHARPOS (*it) > to_charpos)
8724 || (it->bidi_p
8725 && (prev_method == GET_FROM_IMAGE
8726 || prev_method == GET_FROM_STRETCH
8727 || prev_method == GET_FROM_STRING)
8728 /* Passed TO_CHARPOS from left to right. */
8729 && ((prev_pos < to_charpos
8730 && IT_CHARPOS (*it) > to_charpos)
8731 /* Passed TO_CHARPOS from right to left. */
8732 || (prev_pos > to_charpos
8733 && IT_CHARPOS (*it) < to_charpos)))))
8735 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8737 result = MOVE_POS_MATCH_OR_ZV;
8738 break;
8740 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8741 /* If wrap_it is valid, the current position might be in a
8742 word that is wrapped. So, save the iterator in
8743 atpos_it and continue to see if wrapping happens. */
8744 SAVE_IT (atpos_it, *it, atpos_data);
8747 /* Stop when ZV reached.
8748 We used to stop here when TO_CHARPOS reached as well, but that is
8749 too soon if this glyph does not fit on this line. So we handle it
8750 explicitly below. */
8751 if (!get_next_display_element (it))
8753 result = MOVE_POS_MATCH_OR_ZV;
8754 break;
8757 if (it->line_wrap == TRUNCATE)
8759 if (BUFFER_POS_REACHED_P ())
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8765 else
8767 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8769 if (IT_DISPLAYING_WHITESPACE (it))
8770 may_wrap = true;
8771 else if (may_wrap)
8773 /* We have reached a glyph that follows one or more
8774 whitespace characters. If the position is
8775 already found, we are done. */
8776 if (atpos_it.sp >= 0)
8778 RESTORE_IT (it, &atpos_it, atpos_data);
8779 result = MOVE_POS_MATCH_OR_ZV;
8780 goto done;
8782 if (atx_it.sp >= 0)
8784 RESTORE_IT (it, &atx_it, atx_data);
8785 result = MOVE_X_REACHED;
8786 goto done;
8788 /* Otherwise, we can wrap here. */
8789 SAVE_IT (wrap_it, *it, wrap_data);
8790 may_wrap = false;
8795 /* Remember the line height for the current line, in case
8796 the next element doesn't fit on the line. */
8797 ascent = it->max_ascent;
8798 descent = it->max_descent;
8800 /* The call to produce_glyphs will get the metrics of the
8801 display element IT is loaded with. Record the x-position
8802 before this display element, in case it doesn't fit on the
8803 line. */
8804 x = it->current_x;
8806 PRODUCE_GLYPHS (it);
8808 if (it->area != TEXT_AREA)
8810 prev_method = it->method;
8811 if (it->method == GET_FROM_BUFFER)
8812 prev_pos = IT_CHARPOS (*it);
8813 set_iterator_to_next (it, true);
8814 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8815 SET_TEXT_POS (this_line_min_pos,
8816 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8817 if (it->bidi_p
8818 && (op & MOVE_TO_POS)
8819 && IT_CHARPOS (*it) > to_charpos
8820 && IT_CHARPOS (*it) < closest_pos)
8821 closest_pos = IT_CHARPOS (*it);
8822 continue;
8825 /* The number of glyphs we get back in IT->nglyphs will normally
8826 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8827 character on a terminal frame, or (iii) a line end. For the
8828 second case, IT->nglyphs - 1 padding glyphs will be present.
8829 (On X frames, there is only one glyph produced for a
8830 composite character.)
8832 The behavior implemented below means, for continuation lines,
8833 that as many spaces of a TAB as fit on the current line are
8834 displayed there. For terminal frames, as many glyphs of a
8835 multi-glyph character are displayed in the current line, too.
8836 This is what the old redisplay code did, and we keep it that
8837 way. Under X, the whole shape of a complex character must
8838 fit on the line or it will be completely displayed in the
8839 next line.
8841 Note that both for tabs and padding glyphs, all glyphs have
8842 the same width. */
8843 if (it->nglyphs)
8845 /* More than one glyph or glyph doesn't fit on line. All
8846 glyphs have the same width. */
8847 int single_glyph_width = it->pixel_width / it->nglyphs;
8848 int new_x;
8849 int x_before_this_char = x;
8850 int hpos_before_this_char = it->hpos;
8852 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8854 new_x = x + single_glyph_width;
8856 /* We want to leave anything reaching TO_X to the caller. */
8857 if ((op & MOVE_TO_X) && new_x > to_x)
8859 if (BUFFER_POS_REACHED_P ())
8861 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8862 goto buffer_pos_reached;
8863 if (atpos_it.sp < 0)
8865 SAVE_IT (atpos_it, *it, atpos_data);
8866 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8869 else
8871 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8873 it->current_x = x;
8874 result = MOVE_X_REACHED;
8875 break;
8877 if (atx_it.sp < 0)
8879 SAVE_IT (atx_it, *it, atx_data);
8880 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8885 if (/* Lines are continued. */
8886 it->line_wrap != TRUNCATE
8887 && (/* And glyph doesn't fit on the line. */
8888 new_x > it->last_visible_x
8889 /* Or it fits exactly and we're on a window
8890 system frame. */
8891 || (new_x == it->last_visible_x
8892 && FRAME_WINDOW_P (it->f)
8893 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8894 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8895 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8897 bool moved_forward = false;
8899 if (/* IT->hpos == 0 means the very first glyph
8900 doesn't fit on the line, e.g. a wide image. */
8901 it->hpos == 0
8902 || (new_x == it->last_visible_x
8903 && FRAME_WINDOW_P (it->f)))
8905 ++it->hpos;
8906 it->current_x = new_x;
8908 /* The character's last glyph just barely fits
8909 in this row. */
8910 if (i == it->nglyphs - 1)
8912 /* If this is the destination position,
8913 return a position *before* it in this row,
8914 now that we know it fits in this row. */
8915 if (BUFFER_POS_REACHED_P ())
8917 bool can_wrap = true;
8919 /* If we are at a whitespace character
8920 that barely fits on this screen line,
8921 but the next character is also
8922 whitespace, we cannot wrap here. */
8923 if (it->line_wrap == WORD_WRAP
8924 && wrap_it.sp >= 0
8925 && may_wrap
8926 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8928 struct it tem_it;
8929 void *tem_data = NULL;
8931 SAVE_IT (tem_it, *it, tem_data);
8932 set_iterator_to_next (it, true);
8933 if (get_next_display_element (it)
8934 && IT_DISPLAYING_WHITESPACE (it))
8935 can_wrap = false;
8936 RESTORE_IT (it, &tem_it, tem_data);
8938 if (it->line_wrap != WORD_WRAP
8939 || wrap_it.sp < 0
8940 /* If we've just found whitespace
8941 where we can wrap, effectively
8942 ignore the previous wrap point --
8943 it is no longer relevant, but we
8944 won't have an opportunity to
8945 update it, since we've reached
8946 the edge of this screen line. */
8947 || (may_wrap && can_wrap
8948 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8950 it->hpos = hpos_before_this_char;
8951 it->current_x = x_before_this_char;
8952 result = MOVE_POS_MATCH_OR_ZV;
8953 break;
8955 if (it->line_wrap == WORD_WRAP
8956 && atpos_it.sp < 0)
8958 SAVE_IT (atpos_it, *it, atpos_data);
8959 atpos_it.current_x = x_before_this_char;
8960 atpos_it.hpos = hpos_before_this_char;
8964 prev_method = it->method;
8965 if (it->method == GET_FROM_BUFFER)
8966 prev_pos = IT_CHARPOS (*it);
8967 set_iterator_to_next (it, true);
8968 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8969 SET_TEXT_POS (this_line_min_pos,
8970 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8971 /* On graphical terminals, newlines may
8972 "overflow" into the fringe if
8973 overflow-newline-into-fringe is non-nil.
8974 On text terminals, and on graphical
8975 terminals with no right margin, newlines
8976 may overflow into the last glyph on the
8977 display line.*/
8978 if (!FRAME_WINDOW_P (it->f)
8979 || ((it->bidi_p
8980 && it->bidi_it.paragraph_dir == R2L)
8981 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8982 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8983 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8985 if (!get_next_display_element (it))
8987 result = MOVE_POS_MATCH_OR_ZV;
8988 break;
8990 moved_forward = true;
8991 if (BUFFER_POS_REACHED_P ())
8993 if (ITERATOR_AT_END_OF_LINE_P (it))
8994 result = MOVE_POS_MATCH_OR_ZV;
8995 else
8996 result = MOVE_LINE_CONTINUED;
8997 break;
8999 if (ITERATOR_AT_END_OF_LINE_P (it)
9000 && (it->line_wrap != WORD_WRAP
9001 || wrap_it.sp < 0
9002 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9004 result = MOVE_NEWLINE_OR_CR;
9005 break;
9010 else
9011 IT_RESET_X_ASCENT_DESCENT (it);
9013 /* If the screen line ends with whitespace, and we
9014 are under word-wrap, don't use wrap_it: it is no
9015 longer relevant, but we won't have an opportunity
9016 to update it, since we are done with this screen
9017 line. */
9018 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9019 /* If the character after the one which set the
9020 may_wrap flag is also whitespace, we can't
9021 wrap here, since the screen line cannot be
9022 wrapped in the middle of whitespace.
9023 Therefore, wrap_it _is_ relevant in that
9024 case. */
9025 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9027 /* If we've found TO_X, go back there, as we now
9028 know the last word fits on this screen line. */
9029 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9030 && atx_it.sp >= 0)
9032 RESTORE_IT (it, &atx_it, atx_data);
9033 atpos_it.sp = -1;
9034 atx_it.sp = -1;
9035 result = MOVE_X_REACHED;
9036 break;
9039 else if (wrap_it.sp >= 0)
9041 RESTORE_IT (it, &wrap_it, wrap_data);
9042 atpos_it.sp = -1;
9043 atx_it.sp = -1;
9046 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9047 IT_CHARPOS (*it)));
9048 result = MOVE_LINE_CONTINUED;
9049 break;
9052 if (BUFFER_POS_REACHED_P ())
9054 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9055 goto buffer_pos_reached;
9056 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9058 SAVE_IT (atpos_it, *it, atpos_data);
9059 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9063 if (new_x > it->first_visible_x)
9065 /* If we have reached the visible portion of the
9066 screen line, produce the line number if needed. */
9067 if (line_number_pending)
9069 line_number_pending = false;
9070 it->current_x = it->first_visible_x;
9071 maybe_produce_line_number (it);
9072 it->current_x += new_x - it->first_visible_x;
9074 /* Glyph is visible. Increment number of glyphs that
9075 would be displayed. */
9076 ++it->hpos;
9080 if (result != MOVE_UNDEFINED)
9081 break;
9083 else if (BUFFER_POS_REACHED_P ())
9085 buffer_pos_reached:
9086 IT_RESET_X_ASCENT_DESCENT (it);
9087 result = MOVE_POS_MATCH_OR_ZV;
9088 break;
9090 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9092 /* Stop when TO_X specified and reached. This check is
9093 necessary here because of lines consisting of a line end,
9094 only. The line end will not produce any glyphs and we
9095 would never get MOVE_X_REACHED. */
9096 eassert (it->nglyphs == 0);
9097 result = MOVE_X_REACHED;
9098 break;
9101 /* Is this a line end? If yes, we're done. */
9102 if (ITERATOR_AT_END_OF_LINE_P (it))
9104 /* If we are past TO_CHARPOS, but never saw any character
9105 positions smaller than TO_CHARPOS, return
9106 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9107 did. */
9108 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9110 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9112 if (closest_pos < ZV)
9114 RESTORE_IT (it, &ppos_it, ppos_data);
9115 /* Don't recurse if closest_pos is equal to
9116 to_charpos, since we have just tried that. */
9117 if (closest_pos != to_charpos)
9118 move_it_in_display_line_to (it, closest_pos, -1,
9119 MOVE_TO_POS);
9120 result = MOVE_POS_MATCH_OR_ZV;
9122 else
9123 goto buffer_pos_reached;
9125 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9126 && IT_CHARPOS (*it) > to_charpos)
9127 goto buffer_pos_reached;
9128 else
9129 result = MOVE_NEWLINE_OR_CR;
9131 else
9132 result = MOVE_NEWLINE_OR_CR;
9133 /* If we've processed the newline, make sure this flag is
9134 reset, as it must only be set when the newline itself is
9135 processed. */
9136 if (result == MOVE_NEWLINE_OR_CR)
9137 it->constrain_row_ascent_descent_p = false;
9138 break;
9141 prev_method = it->method;
9142 if (it->method == GET_FROM_BUFFER)
9143 prev_pos = IT_CHARPOS (*it);
9144 /* The current display element has been consumed. Advance
9145 to the next. */
9146 set_iterator_to_next (it, true);
9147 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9148 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9149 if (IT_CHARPOS (*it) < to_charpos)
9150 saw_smaller_pos = true;
9151 if (it->bidi_p
9152 && (op & MOVE_TO_POS)
9153 && IT_CHARPOS (*it) >= to_charpos
9154 && IT_CHARPOS (*it) < closest_pos)
9155 closest_pos = IT_CHARPOS (*it);
9157 /* Stop if lines are truncated and IT's current x-position is
9158 past the right edge of the window now. */
9159 if (it->line_wrap == TRUNCATE
9160 && it->current_x >= it->last_visible_x)
9162 if (!FRAME_WINDOW_P (it->f)
9163 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9164 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9165 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9166 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9168 bool at_eob_p = false;
9170 if ((at_eob_p = !get_next_display_element (it))
9171 || BUFFER_POS_REACHED_P ()
9172 /* If we are past TO_CHARPOS, but never saw any
9173 character positions smaller than TO_CHARPOS,
9174 return MOVE_POS_MATCH_OR_ZV, like the
9175 unidirectional display did. */
9176 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9177 && !saw_smaller_pos
9178 && IT_CHARPOS (*it) > to_charpos))
9180 if (it->bidi_p
9181 && !BUFFER_POS_REACHED_P ()
9182 && !at_eob_p && closest_pos < ZV)
9184 RESTORE_IT (it, &ppos_it, ppos_data);
9185 if (closest_pos != to_charpos)
9186 move_it_in_display_line_to (it, closest_pos, -1,
9187 MOVE_TO_POS);
9189 result = MOVE_POS_MATCH_OR_ZV;
9190 break;
9192 if (ITERATOR_AT_END_OF_LINE_P (it))
9194 result = MOVE_NEWLINE_OR_CR;
9195 break;
9198 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9199 && !saw_smaller_pos
9200 && IT_CHARPOS (*it) > to_charpos)
9202 if (closest_pos < ZV)
9204 RESTORE_IT (it, &ppos_it, ppos_data);
9205 if (closest_pos != to_charpos)
9206 move_it_in_display_line_to (it, closest_pos, -1,
9207 MOVE_TO_POS);
9209 result = MOVE_POS_MATCH_OR_ZV;
9210 break;
9212 result = MOVE_LINE_TRUNCATED;
9213 break;
9215 #undef IT_RESET_X_ASCENT_DESCENT
9218 #undef BUFFER_POS_REACHED_P
9220 /* If we scanned beyond TO_POS, restore the saved iterator either to
9221 the wrap point (if found), or to atpos/atx location. We decide which
9222 data to use to restore the saved iterator state by their X coordinates,
9223 since buffer positions might increase non-monotonically with screen
9224 coordinates due to bidi reordering. */
9225 if (result == MOVE_LINE_CONTINUED
9226 && it->line_wrap == WORD_WRAP
9227 && wrap_it.sp >= 0
9228 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9229 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9230 RESTORE_IT (it, &wrap_it, wrap_data);
9231 else if (atpos_it.sp >= 0)
9232 RESTORE_IT (it, &atpos_it, atpos_data);
9233 else if (atx_it.sp >= 0)
9234 RESTORE_IT (it, &atx_it, atx_data);
9236 done:
9238 if (atpos_data)
9239 bidi_unshelve_cache (atpos_data, true);
9240 if (atx_data)
9241 bidi_unshelve_cache (atx_data, true);
9242 if (wrap_data)
9243 bidi_unshelve_cache (wrap_data, true);
9244 if (ppos_data)
9245 bidi_unshelve_cache (ppos_data, true);
9247 /* Restore the iterator settings altered at the beginning of this
9248 function. */
9249 it->glyph_row = saved_glyph_row;
9250 return result;
9253 /* For external use. */
9254 void
9255 move_it_in_display_line (struct it *it,
9256 ptrdiff_t to_charpos, int to_x,
9257 enum move_operation_enum op)
9259 if (it->line_wrap == WORD_WRAP
9260 && (op & MOVE_TO_X))
9262 struct it save_it;
9263 void *save_data = NULL;
9264 int skip;
9266 SAVE_IT (save_it, *it, save_data);
9267 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9268 /* When word-wrap is on, TO_X may lie past the end
9269 of a wrapped line. Then it->current is the
9270 character on the next line, so backtrack to the
9271 space before the wrap point. */
9272 if (skip == MOVE_LINE_CONTINUED)
9274 int prev_x = max (it->current_x - 1, 0);
9275 RESTORE_IT (it, &save_it, save_data);
9276 move_it_in_display_line_to
9277 (it, -1, prev_x, MOVE_TO_X);
9279 else
9280 bidi_unshelve_cache (save_data, true);
9282 else
9283 move_it_in_display_line_to (it, to_charpos, to_x, op);
9287 /* Move IT forward until it satisfies one or more of the criteria in
9288 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9290 OP is a bit-mask that specifies where to stop, and in particular,
9291 which of those four position arguments makes a difference. See the
9292 description of enum move_operation_enum.
9294 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9295 screen line, this function will set IT to the next position that is
9296 displayed to the right of TO_CHARPOS on the screen.
9298 Return the maximum pixel length of any line scanned but never more
9299 than it.last_visible_x. */
9302 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9304 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9305 int line_height, line_start_x = 0, reached = 0;
9306 int max_current_x = 0;
9307 void *backup_data = NULL;
9309 for (;;)
9311 if (op & MOVE_TO_VPOS)
9313 /* If no TO_CHARPOS and no TO_X specified, stop at the
9314 start of the line TO_VPOS. */
9315 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9317 if (it->vpos == to_vpos)
9319 reached = 1;
9320 break;
9322 else
9323 skip = move_it_in_display_line_to (it, -1, -1, 0);
9325 else
9327 /* TO_VPOS >= 0 means stop at TO_X in the line at
9328 TO_VPOS, or at TO_POS, whichever comes first. */
9329 if (it->vpos == to_vpos)
9331 reached = 2;
9332 break;
9335 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9337 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9339 reached = 3;
9340 break;
9342 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9344 /* We have reached TO_X but not in the line we want. */
9345 skip = move_it_in_display_line_to (it, to_charpos,
9346 -1, MOVE_TO_POS);
9347 if (skip == MOVE_POS_MATCH_OR_ZV)
9349 reached = 4;
9350 break;
9355 else if (op & MOVE_TO_Y)
9357 struct it it_backup;
9359 if (it->line_wrap == WORD_WRAP)
9360 SAVE_IT (it_backup, *it, backup_data);
9362 /* TO_Y specified means stop at TO_X in the line containing
9363 TO_Y---or at TO_CHARPOS if this is reached first. The
9364 problem is that we can't really tell whether the line
9365 contains TO_Y before we have completely scanned it, and
9366 this may skip past TO_X. What we do is to first scan to
9367 TO_X.
9369 If TO_X is not specified, use a TO_X of zero. The reason
9370 is to make the outcome of this function more predictable.
9371 If we didn't use TO_X == 0, we would stop at the end of
9372 the line which is probably not what a caller would expect
9373 to happen. */
9374 skip = move_it_in_display_line_to
9375 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9376 (MOVE_TO_X | (op & MOVE_TO_POS)));
9378 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9379 if (skip == MOVE_POS_MATCH_OR_ZV)
9380 reached = 5;
9381 else if (skip == MOVE_X_REACHED)
9383 /* If TO_X was reached, we want to know whether TO_Y is
9384 in the line. We know this is the case if the already
9385 scanned glyphs make the line tall enough. Otherwise,
9386 we must check by scanning the rest of the line. */
9387 line_height = it->max_ascent + it->max_descent;
9388 if (to_y >= it->current_y
9389 && to_y < it->current_y + line_height)
9391 reached = 6;
9392 break;
9394 SAVE_IT (it_backup, *it, backup_data);
9395 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9396 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9397 op & MOVE_TO_POS);
9398 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9399 line_height = it->max_ascent + it->max_descent;
9400 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9402 if (to_y >= it->current_y
9403 && to_y < it->current_y + line_height)
9405 /* If TO_Y is in this line and TO_X was reached
9406 above, we scanned too far. We have to restore
9407 IT's settings to the ones before skipping. But
9408 keep the more accurate values of max_ascent and
9409 max_descent we've found while skipping the rest
9410 of the line, for the sake of callers, such as
9411 pos_visible_p, that need to know the line
9412 height. */
9413 int max_ascent = it->max_ascent;
9414 int max_descent = it->max_descent;
9416 RESTORE_IT (it, &it_backup, backup_data);
9417 it->max_ascent = max_ascent;
9418 it->max_descent = max_descent;
9419 reached = 6;
9421 else
9423 skip = skip2;
9424 if (skip == MOVE_POS_MATCH_OR_ZV)
9425 reached = 7;
9428 else
9430 /* Check whether TO_Y is in this line. */
9431 line_height = it->max_ascent + it->max_descent;
9432 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9434 if (to_y >= it->current_y
9435 && to_y < it->current_y + line_height)
9437 if (to_y > it->current_y)
9438 max_current_x = max (it->current_x, max_current_x);
9440 /* When word-wrap is on, TO_X may lie past the end
9441 of a wrapped line. Then it->current is the
9442 character on the next line, so backtrack to the
9443 space before the wrap point. */
9444 if (skip == MOVE_LINE_CONTINUED
9445 && it->line_wrap == WORD_WRAP)
9447 int prev_x = max (it->current_x - 1, 0);
9448 RESTORE_IT (it, &it_backup, backup_data);
9449 skip = move_it_in_display_line_to
9450 (it, -1, prev_x, MOVE_TO_X);
9453 reached = 6;
9457 if (reached)
9459 max_current_x = max (it->current_x, max_current_x);
9460 break;
9463 else if (BUFFERP (it->object)
9464 && (it->method == GET_FROM_BUFFER
9465 || it->method == GET_FROM_STRETCH)
9466 && IT_CHARPOS (*it) >= to_charpos
9467 /* Under bidi iteration, a call to set_iterator_to_next
9468 can scan far beyond to_charpos if the initial
9469 portion of the next line needs to be reordered. In
9470 that case, give move_it_in_display_line_to another
9471 chance below. */
9472 && !(it->bidi_p
9473 && it->bidi_it.scan_dir == -1))
9474 skip = MOVE_POS_MATCH_OR_ZV;
9475 else
9476 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9478 switch (skip)
9480 case MOVE_POS_MATCH_OR_ZV:
9481 max_current_x = max (it->current_x, max_current_x);
9482 reached = 8;
9483 goto out;
9485 case MOVE_NEWLINE_OR_CR:
9486 max_current_x = max (it->current_x, max_current_x);
9487 set_iterator_to_next (it, true);
9488 it->continuation_lines_width = 0;
9489 break;
9491 case MOVE_LINE_TRUNCATED:
9492 max_current_x = it->last_visible_x;
9493 it->continuation_lines_width = 0;
9494 reseat_at_next_visible_line_start (it, false);
9495 if ((op & MOVE_TO_POS) != 0
9496 && IT_CHARPOS (*it) > to_charpos)
9498 reached = 9;
9499 goto out;
9501 break;
9503 case MOVE_LINE_CONTINUED:
9504 max_current_x = it->last_visible_x;
9505 /* For continued lines ending in a tab, some of the glyphs
9506 associated with the tab are displayed on the current
9507 line. Since it->current_x does not include these glyphs,
9508 we use it->last_visible_x instead. */
9509 if (it->c == '\t')
9511 it->continuation_lines_width += it->last_visible_x;
9512 /* When moving by vpos, ensure that the iterator really
9513 advances to the next line (bug#847, bug#969). Fixme:
9514 do we need to do this in other circumstances? */
9515 if (it->current_x != it->last_visible_x
9516 && (op & MOVE_TO_VPOS)
9517 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9519 line_start_x = it->current_x + it->pixel_width
9520 - it->last_visible_x;
9521 if (FRAME_WINDOW_P (it->f))
9523 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9524 struct font *face_font = face->font;
9526 /* When display_line produces a continued line
9527 that ends in a TAB, it skips a tab stop that
9528 is closer than the font's space character
9529 width (see x_produce_glyphs where it produces
9530 the stretch glyph which represents a TAB).
9531 We need to reproduce the same logic here. */
9532 eassert (face_font);
9533 if (face_font)
9535 if (line_start_x < face_font->space_width)
9536 line_start_x
9537 += it->tab_width * face_font->space_width;
9540 set_iterator_to_next (it, false);
9543 else
9544 it->continuation_lines_width += it->current_x;
9545 break;
9547 default:
9548 emacs_abort ();
9551 /* Reset/increment for the next run. */
9552 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9553 it->current_x = line_start_x;
9554 line_start_x = 0;
9555 it->hpos = 0;
9556 it->current_y += it->max_ascent + it->max_descent;
9557 ++it->vpos;
9558 last_height = it->max_ascent + it->max_descent;
9559 it->max_ascent = it->max_descent = 0;
9562 out:
9564 /* On text terminals, we may stop at the end of a line in the middle
9565 of a multi-character glyph. If the glyph itself is continued,
9566 i.e. it is actually displayed on the next line, don't treat this
9567 stopping point as valid; move to the next line instead (unless
9568 that brings us offscreen). */
9569 if (!FRAME_WINDOW_P (it->f)
9570 && op & MOVE_TO_POS
9571 && IT_CHARPOS (*it) == to_charpos
9572 && it->what == IT_CHARACTER
9573 && it->nglyphs > 1
9574 && it->line_wrap == WINDOW_WRAP
9575 && it->current_x == it->last_visible_x - 1
9576 && it->c != '\n'
9577 && it->c != '\t'
9578 && it->w->window_end_valid
9579 && it->vpos < it->w->window_end_vpos)
9581 it->continuation_lines_width += it->current_x;
9582 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9583 it->current_y += it->max_ascent + it->max_descent;
9584 ++it->vpos;
9585 last_height = it->max_ascent + it->max_descent;
9588 if (backup_data)
9589 bidi_unshelve_cache (backup_data, true);
9591 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9593 return max_current_x;
9597 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9599 If DY > 0, move IT backward at least that many pixels. DY = 0
9600 means move IT backward to the preceding line start or BEGV. This
9601 function may move over more than DY pixels if IT->current_y - DY
9602 ends up in the middle of a line; in this case IT->current_y will be
9603 set to the top of the line moved to. */
9605 void
9606 move_it_vertically_backward (struct it *it, int dy)
9608 int nlines, h;
9609 struct it it2, it3;
9610 void *it2data = NULL, *it3data = NULL;
9611 ptrdiff_t start_pos;
9612 int nchars_per_row
9613 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9614 ptrdiff_t pos_limit;
9616 move_further_back:
9617 eassert (dy >= 0);
9619 start_pos = IT_CHARPOS (*it);
9621 /* Estimate how many newlines we must move back. */
9622 nlines = max (1, dy / default_line_pixel_height (it->w));
9623 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9624 pos_limit = BEGV;
9625 else
9626 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9628 /* Set the iterator's position that many lines back. But don't go
9629 back more than NLINES full screen lines -- this wins a day with
9630 buffers which have very long lines. */
9631 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9632 back_to_previous_visible_line_start (it);
9634 /* Reseat the iterator here. When moving backward, we don't want
9635 reseat to skip forward over invisible text, set up the iterator
9636 to deliver from overlay strings at the new position etc. So,
9637 use reseat_1 here. */
9638 reseat_1 (it, it->current.pos, true);
9640 /* We are now surely at a line start. */
9641 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9642 reordering is in effect. */
9643 it->continuation_lines_width = 0;
9645 /* Move forward and see what y-distance we moved. First move to the
9646 start of the next line so that we get its height. We need this
9647 height to be able to tell whether we reached the specified
9648 y-distance. */
9649 SAVE_IT (it2, *it, it2data);
9650 it2.max_ascent = it2.max_descent = 0;
9653 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9654 MOVE_TO_POS | MOVE_TO_VPOS);
9656 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9657 /* If we are in a display string which starts at START_POS,
9658 and that display string includes a newline, and we are
9659 right after that newline (i.e. at the beginning of a
9660 display line), exit the loop, because otherwise we will
9661 infloop, since move_it_to will see that it is already at
9662 START_POS and will not move. */
9663 || (it2.method == GET_FROM_STRING
9664 && IT_CHARPOS (it2) == start_pos
9665 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9666 eassert (IT_CHARPOS (*it) >= BEGV);
9667 SAVE_IT (it3, it2, it3data);
9669 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9670 eassert (IT_CHARPOS (*it) >= BEGV);
9671 /* H is the actual vertical distance from the position in *IT
9672 and the starting position. */
9673 h = it2.current_y - it->current_y;
9674 /* NLINES is the distance in number of lines. */
9675 nlines = it2.vpos - it->vpos;
9677 /* Correct IT's y and vpos position
9678 so that they are relative to the starting point. */
9679 it->vpos -= nlines;
9680 it->current_y -= h;
9682 if (dy == 0)
9684 /* DY == 0 means move to the start of the screen line. The
9685 value of nlines is > 0 if continuation lines were involved,
9686 or if the original IT position was at start of a line. */
9687 RESTORE_IT (it, it, it2data);
9688 if (nlines > 0)
9689 move_it_by_lines (it, nlines);
9690 /* The above code moves us to some position NLINES down,
9691 usually to its first glyph (leftmost in an L2R line), but
9692 that's not necessarily the start of the line, under bidi
9693 reordering. We want to get to the character position
9694 that is immediately after the newline of the previous
9695 line. */
9696 if (it->bidi_p
9697 && !it->continuation_lines_width
9698 && !STRINGP (it->string)
9699 && IT_CHARPOS (*it) > BEGV
9700 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9702 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9704 DEC_BOTH (cp, bp);
9705 cp = find_newline_no_quit (cp, bp, -1, NULL);
9706 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9708 bidi_unshelve_cache (it3data, true);
9710 else
9712 /* The y-position we try to reach, relative to *IT.
9713 Note that H has been subtracted in front of the if-statement. */
9714 int target_y = it->current_y + h - dy;
9715 int y0 = it3.current_y;
9716 int y1;
9717 int line_height;
9719 RESTORE_IT (&it3, &it3, it3data);
9720 y1 = line_bottom_y (&it3);
9721 line_height = y1 - y0;
9722 RESTORE_IT (it, it, it2data);
9723 /* If we did not reach target_y, try to move further backward if
9724 we can. If we moved too far backward, try to move forward. */
9725 if (target_y < it->current_y
9726 /* This is heuristic. In a window that's 3 lines high, with
9727 a line height of 13 pixels each, recentering with point
9728 on the bottom line will try to move -39/2 = 19 pixels
9729 backward. Try to avoid moving into the first line. */
9730 && (it->current_y - target_y
9731 > min (window_box_height (it->w), line_height * 2 / 3))
9732 && IT_CHARPOS (*it) > BEGV)
9734 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9735 target_y - it->current_y));
9736 dy = it->current_y - target_y;
9737 goto move_further_back;
9739 else if (target_y >= it->current_y + line_height
9740 && IT_CHARPOS (*it) < ZV)
9742 /* Should move forward by at least one line, maybe more.
9744 Note: Calling move_it_by_lines can be expensive on
9745 terminal frames, where compute_motion is used (via
9746 vmotion) to do the job, when there are very long lines
9747 and truncate-lines is nil. That's the reason for
9748 treating terminal frames specially here. */
9750 if (!FRAME_WINDOW_P (it->f))
9751 move_it_vertically (it, target_y - it->current_y);
9752 else
9756 move_it_by_lines (it, 1);
9758 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9765 /* Move IT by a specified amount of pixel lines DY. DY negative means
9766 move backwards. DY = 0 means move to start of screen line. At the
9767 end, IT will be on the start of a screen line. */
9769 void
9770 move_it_vertically (struct it *it, int dy)
9772 if (dy <= 0)
9773 move_it_vertically_backward (it, -dy);
9774 else
9776 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9777 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9778 MOVE_TO_POS | MOVE_TO_Y);
9779 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9781 /* If buffer ends in ZV without a newline, move to the start of
9782 the line to satisfy the post-condition. */
9783 if (IT_CHARPOS (*it) == ZV
9784 && ZV > BEGV
9785 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9786 move_it_by_lines (it, 0);
9791 /* Move iterator IT past the end of the text line it is in. */
9793 void
9794 move_it_past_eol (struct it *it)
9796 enum move_it_result rc;
9798 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9799 if (rc == MOVE_NEWLINE_OR_CR)
9800 set_iterator_to_next (it, false);
9804 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9805 negative means move up. DVPOS == 0 means move to the start of the
9806 screen line.
9808 Optimization idea: If we would know that IT->f doesn't use
9809 a face with proportional font, we could be faster for
9810 truncate-lines nil. */
9812 void
9813 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9816 /* The commented-out optimization uses vmotion on terminals. This
9817 gives bad results, because elements like it->what, on which
9818 callers such as pos_visible_p rely, aren't updated. */
9819 /* struct position pos;
9820 if (!FRAME_WINDOW_P (it->f))
9822 struct text_pos textpos;
9824 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9825 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9826 reseat (it, textpos, true);
9827 it->vpos += pos.vpos;
9828 it->current_y += pos.vpos;
9830 else */
9832 if (dvpos == 0)
9834 /* DVPOS == 0 means move to the start of the screen line. */
9835 move_it_vertically_backward (it, 0);
9836 /* Let next call to line_bottom_y calculate real line height. */
9837 last_height = 0;
9839 else if (dvpos > 0)
9841 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9842 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9844 /* Only move to the next buffer position if we ended up in a
9845 string from display property, not in an overlay string
9846 (before-string or after-string). That is because the
9847 latter don't conceal the underlying buffer position, so
9848 we can ask to move the iterator to the exact position we
9849 are interested in. Note that, even if we are already at
9850 IT_CHARPOS (*it), the call below is not a no-op, as it
9851 will detect that we are at the end of the string, pop the
9852 iterator, and compute it->current_x and it->hpos
9853 correctly. */
9854 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9855 -1, -1, -1, MOVE_TO_POS);
9858 else
9860 struct it it2;
9861 void *it2data = NULL;
9862 ptrdiff_t start_charpos, i;
9863 int nchars_per_row
9864 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9865 bool hit_pos_limit = false;
9866 ptrdiff_t pos_limit;
9868 /* Start at the beginning of the screen line containing IT's
9869 position. This may actually move vertically backwards,
9870 in case of overlays, so adjust dvpos accordingly. */
9871 dvpos += it->vpos;
9872 move_it_vertically_backward (it, 0);
9873 dvpos -= it->vpos;
9875 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9876 screen lines, and reseat the iterator there. */
9877 start_charpos = IT_CHARPOS (*it);
9878 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9879 pos_limit = BEGV;
9880 else
9881 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9883 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9884 back_to_previous_visible_line_start (it);
9885 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9886 hit_pos_limit = true;
9887 reseat (it, it->current.pos, true);
9889 /* Move further back if we end up in a string or an image. */
9890 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9892 /* First try to move to start of display line. */
9893 dvpos += it->vpos;
9894 move_it_vertically_backward (it, 0);
9895 dvpos -= it->vpos;
9896 if (IT_POS_VALID_AFTER_MOVE_P (it))
9897 break;
9898 /* If start of line is still in string or image,
9899 move further back. */
9900 back_to_previous_visible_line_start (it);
9901 reseat (it, it->current.pos, true);
9902 dvpos--;
9905 it->current_x = it->hpos = 0;
9907 /* Above call may have moved too far if continuation lines
9908 are involved. Scan forward and see if it did. */
9909 SAVE_IT (it2, *it, it2data);
9910 it2.vpos = it2.current_y = 0;
9911 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9912 it->vpos -= it2.vpos;
9913 it->current_y -= it2.current_y;
9914 it->current_x = it->hpos = 0;
9916 /* If we moved too far back, move IT some lines forward. */
9917 if (it2.vpos > -dvpos)
9919 int delta = it2.vpos + dvpos;
9921 RESTORE_IT (&it2, &it2, it2data);
9922 SAVE_IT (it2, *it, it2data);
9923 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9924 /* Move back again if we got too far ahead. */
9925 if (IT_CHARPOS (*it) >= start_charpos)
9926 RESTORE_IT (it, &it2, it2data);
9927 else
9928 bidi_unshelve_cache (it2data, true);
9930 else if (hit_pos_limit && pos_limit > BEGV
9931 && dvpos < 0 && it2.vpos < -dvpos)
9933 /* If we hit the limit, but still didn't make it far enough
9934 back, that means there's a display string with a newline
9935 covering a large chunk of text, and that caused
9936 back_to_previous_visible_line_start try to go too far.
9937 Punish those who commit such atrocities by going back
9938 until we've reached DVPOS, after lifting the limit, which
9939 could make it slow for very long lines. "If it hurts,
9940 don't do that!" */
9941 dvpos += it2.vpos;
9942 RESTORE_IT (it, it, it2data);
9943 for (i = -dvpos; i > 0; --i)
9945 back_to_previous_visible_line_start (it);
9946 it->vpos--;
9948 reseat_1 (it, it->current.pos, true);
9950 else
9951 RESTORE_IT (it, it, it2data);
9956 partial_line_height (struct it *it_origin)
9958 int partial_height;
9959 void *it_data = NULL;
9960 struct it it;
9961 SAVE_IT (it, *it_origin, it_data);
9962 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9963 MOVE_TO_POS | MOVE_TO_Y);
9964 if (it.what == IT_EOB)
9966 int vis_height = it.last_visible_y - it.current_y;
9967 int height = it.ascent + it.descent;
9968 partial_height = (vis_height < height) ? vis_height : 0;
9970 else
9972 int last_line_y = it.current_y;
9973 move_it_by_lines (&it, 1);
9974 partial_height = (it.current_y > it.last_visible_y)
9975 ? it.last_visible_y - last_line_y : 0;
9977 RESTORE_IT (&it, &it, it_data);
9978 return partial_height;
9981 /* Return true if IT points into the middle of a display vector. */
9983 bool
9984 in_display_vector_p (struct it *it)
9986 return (it->method == GET_FROM_DISPLAY_VECTOR
9987 && it->current.dpvec_index > 0
9988 && it->dpvec + it->current.dpvec_index != it->dpend);
9991 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9992 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9993 WINDOW must be a live window and defaults to the selected one. The
9994 return value is a cons of the maximum pixel-width of any text line and
9995 the maximum pixel-height of all text lines.
9997 The optional argument FROM, if non-nil, specifies the first text
9998 position and defaults to the minimum accessible position of the buffer.
9999 If FROM is t, use the minimum accessible position that starts a
10000 non-empty line. TO, if non-nil, specifies the last text position and
10001 defaults to the maximum accessible position of the buffer. If TO is t,
10002 use the maximum accessible position that ends a non-empty line.
10004 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10005 width that can be returned. X-LIMIT nil or omitted, means to use the
10006 pixel-width of WINDOW's body; use this if you want to know how high
10007 WINDOW should be become in order to fit all of its buffer's text with
10008 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10009 if you intend to change WINDOW's width. In any case, text whose
10010 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10011 of long lines can take some time, it's always a good idea to make this
10012 argument as small as possible; in particular, if the buffer contains
10013 long lines that shall be truncated anyway.
10015 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10016 height (excluding the height of the mode- or header-line, if any) that
10017 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10018 ignored. Since calculating the text height of a large buffer can take
10019 some time, it makes sense to specify this argument if the size of the
10020 buffer is large or unknown.
10022 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10023 include the height of the mode- or header-line of WINDOW in the return
10024 value. If it is either the symbol `mode-line' or `header-line', include
10025 only the height of that line, if present, in the return value. If t,
10026 include the height of both, if present, in the return value. */)
10027 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10028 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10030 struct window *w = decode_live_window (window);
10031 Lisp_Object buffer = w->contents;
10032 struct buffer *b;
10033 struct it it;
10034 struct buffer *old_b = NULL;
10035 ptrdiff_t start, end, pos;
10036 struct text_pos startp;
10037 void *itdata = NULL;
10038 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10040 CHECK_BUFFER (buffer);
10041 b = XBUFFER (buffer);
10043 if (b != current_buffer)
10045 old_b = current_buffer;
10046 set_buffer_internal (b);
10049 if (NILP (from))
10050 start = BEGV;
10051 else if (EQ (from, Qt))
10053 start = pos = BEGV;
10054 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10055 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10056 start = pos;
10057 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10058 start = pos;
10060 else
10062 CHECK_NUMBER_COERCE_MARKER (from);
10063 start = min (max (XINT (from), BEGV), ZV);
10066 if (NILP (to))
10067 end = ZV;
10068 else if (EQ (to, Qt))
10070 end = pos = ZV;
10071 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10072 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10073 end = pos;
10074 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10075 end = pos;
10077 else
10079 CHECK_NUMBER_COERCE_MARKER (to);
10080 end = max (start, min (XINT (to), ZV));
10083 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10084 max_x = XINT (x_limit);
10086 if (NILP (y_limit))
10087 max_y = INT_MAX;
10088 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10089 max_y = XINT (y_limit);
10091 itdata = bidi_shelve_cache ();
10092 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10093 start_display (&it, w, startp);
10095 if (NILP (x_limit))
10096 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10097 else
10099 it.last_visible_x = max_x;
10100 /* Actually, we never want move_it_to stop at to_x. But to make
10101 sure that move_it_in_display_line_to always moves far enough,
10102 we set it to INT_MAX and specify MOVE_TO_X. */
10103 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10104 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10105 /* Don't return more than X-LIMIT. */
10106 if (x > max_x)
10107 x = max_x;
10110 /* Subtract height of header-line which was counted automatically by
10111 start_display. */
10112 y = it.current_y + it.max_ascent + it.max_descent
10113 - WINDOW_HEADER_LINE_HEIGHT (w);
10114 /* Don't return more than Y-LIMIT. */
10115 if (y > max_y)
10116 y = max_y;
10118 if (EQ (mode_and_header_line, Qheader_line)
10119 || EQ (mode_and_header_line, Qt))
10120 /* Re-add height of header-line as requested. */
10121 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10123 if (EQ (mode_and_header_line, Qmode_line)
10124 || EQ (mode_and_header_line, Qt))
10125 /* Add height of mode-line as requested. */
10126 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10128 bidi_unshelve_cache (itdata, false);
10130 if (old_b)
10131 set_buffer_internal (old_b);
10133 return Fcons (make_number (x), make_number (y));
10136 /***********************************************************************
10137 Messages
10138 ***********************************************************************/
10140 /* Return the number of arguments the format string FORMAT needs. */
10142 static ptrdiff_t
10143 format_nargs (char const *format)
10145 ptrdiff_t nargs = 0;
10146 for (char const *p = format; (p = strchr (p, '%')); p++)
10147 if (p[1] == '%')
10148 p++;
10149 else
10150 nargs++;
10151 return nargs;
10154 /* Add a message with format string FORMAT and formatted arguments
10155 to *Messages*. */
10157 void
10158 add_to_log (const char *format, ...)
10160 va_list ap;
10161 va_start (ap, format);
10162 vadd_to_log (format, ap);
10163 va_end (ap);
10166 void
10167 vadd_to_log (char const *format, va_list ap)
10169 ptrdiff_t form_nargs = format_nargs (format);
10170 ptrdiff_t nargs = 1 + form_nargs;
10171 Lisp_Object args[10];
10172 eassert (nargs <= ARRAYELTS (args));
10173 AUTO_STRING (args0, format);
10174 args[0] = args0;
10175 for (ptrdiff_t i = 1; i <= nargs; i++)
10176 args[i] = va_arg (ap, Lisp_Object);
10177 Lisp_Object msg = Qnil;
10178 msg = Fformat_message (nargs, args);
10180 ptrdiff_t len = SBYTES (msg) + 1;
10181 USE_SAFE_ALLOCA;
10182 char *buffer = SAFE_ALLOCA (len);
10183 memcpy (buffer, SDATA (msg), len);
10185 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10186 SAFE_FREE ();
10190 /* Output a newline in the *Messages* buffer if "needs" one. */
10192 void
10193 message_log_maybe_newline (void)
10195 if (message_log_need_newline)
10196 message_dolog ("", 0, true, false);
10200 /* Add a string M of length NBYTES to the message log, optionally
10201 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10202 true, means interpret the contents of M as multibyte. This
10203 function calls low-level routines in order to bypass text property
10204 hooks, etc. which might not be safe to run.
10206 This may GC (insert may run before/after change hooks),
10207 so the buffer M must NOT point to a Lisp string. */
10209 void
10210 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10212 const unsigned char *msg = (const unsigned char *) m;
10214 if (!NILP (Vmemory_full))
10215 return;
10217 if (!NILP (Vmessage_log_max))
10219 struct buffer *oldbuf;
10220 Lisp_Object oldpoint, oldbegv, oldzv;
10221 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10222 ptrdiff_t point_at_end = 0;
10223 ptrdiff_t zv_at_end = 0;
10224 Lisp_Object old_deactivate_mark;
10226 old_deactivate_mark = Vdeactivate_mark;
10227 oldbuf = current_buffer;
10229 /* Ensure the Messages buffer exists, and switch to it.
10230 If we created it, set the major-mode. */
10231 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10232 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10233 if (newbuffer
10234 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10235 call0 (intern ("messages-buffer-mode"));
10237 bset_undo_list (current_buffer, Qt);
10238 bset_cache_long_scans (current_buffer, Qnil);
10240 oldpoint = message_dolog_marker1;
10241 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10242 oldbegv = message_dolog_marker2;
10243 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10244 oldzv = message_dolog_marker3;
10245 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10247 if (PT == Z)
10248 point_at_end = 1;
10249 if (ZV == Z)
10250 zv_at_end = 1;
10252 BEGV = BEG;
10253 BEGV_BYTE = BEG_BYTE;
10254 ZV = Z;
10255 ZV_BYTE = Z_BYTE;
10256 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10258 /* Insert the string--maybe converting multibyte to single byte
10259 or vice versa, so that all the text fits the buffer. */
10260 if (multibyte
10261 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10263 ptrdiff_t i;
10264 int c, char_bytes;
10265 char work[1];
10267 /* Convert a multibyte string to single-byte
10268 for the *Message* buffer. */
10269 for (i = 0; i < nbytes; i += char_bytes)
10271 c = string_char_and_length (msg + i, &char_bytes);
10272 work[0] = CHAR_TO_BYTE8 (c);
10273 insert_1_both (work, 1, 1, true, false, false);
10276 else if (! multibyte
10277 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10279 ptrdiff_t i;
10280 int c, char_bytes;
10281 unsigned char str[MAX_MULTIBYTE_LENGTH];
10282 /* Convert a single-byte string to multibyte
10283 for the *Message* buffer. */
10284 for (i = 0; i < nbytes; i++)
10286 c = msg[i];
10287 MAKE_CHAR_MULTIBYTE (c);
10288 char_bytes = CHAR_STRING (c, str);
10289 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10292 else if (nbytes)
10293 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10294 true, false, false);
10296 if (nlflag)
10298 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10299 printmax_t dups;
10301 insert_1_both ("\n", 1, 1, true, false, false);
10303 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10304 this_bol = PT;
10305 this_bol_byte = PT_BYTE;
10307 /* See if this line duplicates the previous one.
10308 If so, combine duplicates. */
10309 if (this_bol > BEG)
10311 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10312 prev_bol = PT;
10313 prev_bol_byte = PT_BYTE;
10315 dups = message_log_check_duplicate (prev_bol_byte,
10316 this_bol_byte);
10317 if (dups)
10319 del_range_both (prev_bol, prev_bol_byte,
10320 this_bol, this_bol_byte, false);
10321 if (dups > 1)
10323 char dupstr[sizeof " [ times]"
10324 + INT_STRLEN_BOUND (printmax_t)];
10326 /* If you change this format, don't forget to also
10327 change message_log_check_duplicate. */
10328 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10329 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10330 insert_1_both (dupstr, duplen, duplen,
10331 true, false, true);
10336 /* If we have more than the desired maximum number of lines
10337 in the *Messages* buffer now, delete the oldest ones.
10338 This is safe because we don't have undo in this buffer. */
10340 if (NATNUMP (Vmessage_log_max))
10342 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10343 -XFASTINT (Vmessage_log_max) - 1, false);
10344 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10347 BEGV = marker_position (oldbegv);
10348 BEGV_BYTE = marker_byte_position (oldbegv);
10350 if (zv_at_end)
10352 ZV = Z;
10353 ZV_BYTE = Z_BYTE;
10355 else
10357 ZV = marker_position (oldzv);
10358 ZV_BYTE = marker_byte_position (oldzv);
10361 if (point_at_end)
10362 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10363 else
10364 /* We can't do Fgoto_char (oldpoint) because it will run some
10365 Lisp code. */
10366 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10367 marker_byte_position (oldpoint));
10369 unchain_marker (XMARKER (oldpoint));
10370 unchain_marker (XMARKER (oldbegv));
10371 unchain_marker (XMARKER (oldzv));
10373 /* We called insert_1_both above with its 5th argument (PREPARE)
10374 false, which prevents insert_1_both from calling
10375 prepare_to_modify_buffer, which in turns prevents us from
10376 incrementing windows_or_buffers_changed even if *Messages* is
10377 shown in some window. So we must manually set
10378 windows_or_buffers_changed here to make up for that. */
10379 windows_or_buffers_changed = old_windows_or_buffers_changed;
10380 bset_redisplay (current_buffer);
10382 set_buffer_internal (oldbuf);
10384 message_log_need_newline = !nlflag;
10385 Vdeactivate_mark = old_deactivate_mark;
10390 /* We are at the end of the buffer after just having inserted a newline.
10391 (Note: We depend on the fact we won't be crossing the gap.)
10392 Check to see if the most recent message looks a lot like the previous one.
10393 Return 0 if different, 1 if the new one should just replace it, or a
10394 value N > 1 if we should also append " [N times]". */
10396 static intmax_t
10397 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10399 ptrdiff_t i;
10400 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10401 bool seen_dots = false;
10402 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10403 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10405 for (i = 0; i < len; i++)
10407 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10408 seen_dots = true;
10409 if (p1[i] != p2[i])
10410 return seen_dots;
10412 p1 += len;
10413 if (*p1 == '\n')
10414 return 2;
10415 if (*p1++ == ' ' && *p1++ == '[')
10417 char *pend;
10418 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10419 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10420 return n + 1;
10422 return 0;
10426 /* Display an echo area message M with a specified length of NBYTES
10427 bytes. The string may include null characters. If M is not a
10428 string, clear out any existing message, and let the mini-buffer
10429 text show through.
10431 This function cancels echoing. */
10433 void
10434 message3 (Lisp_Object m)
10436 clear_message (true, true);
10437 cancel_echoing ();
10439 /* First flush out any partial line written with print. */
10440 message_log_maybe_newline ();
10441 if (STRINGP (m))
10443 ptrdiff_t nbytes = SBYTES (m);
10444 bool multibyte = STRING_MULTIBYTE (m);
10445 char *buffer;
10446 USE_SAFE_ALLOCA;
10447 SAFE_ALLOCA_STRING (buffer, m);
10448 message_dolog (buffer, nbytes, true, multibyte);
10449 SAFE_FREE ();
10451 if (! inhibit_message)
10452 message3_nolog (m);
10455 /* Log the message M to stderr. Log an empty line if M is not a string. */
10457 static void
10458 message_to_stderr (Lisp_Object m)
10460 if (noninteractive_need_newline)
10462 noninteractive_need_newline = false;
10463 fputc ('\n', stderr);
10465 if (STRINGP (m))
10467 Lisp_Object coding_system = Vlocale_coding_system;
10468 Lisp_Object s;
10470 if (!NILP (Vcoding_system_for_write))
10471 coding_system = Vcoding_system_for_write;
10472 if (!NILP (coding_system))
10473 s = code_convert_string_norecord (m, coding_system, true);
10474 else
10475 s = m;
10477 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10479 if (!cursor_in_echo_area)
10480 fputc ('\n', stderr);
10481 fflush (stderr);
10484 /* The non-logging version of message3.
10485 This does not cancel echoing, because it is used for echoing.
10486 Perhaps we need to make a separate function for echoing
10487 and make this cancel echoing. */
10489 void
10490 message3_nolog (Lisp_Object m)
10492 struct frame *sf = SELECTED_FRAME ();
10494 if (FRAME_INITIAL_P (sf))
10495 message_to_stderr (m);
10496 /* Error messages get reported properly by cmd_error, so this must be just an
10497 informative message; if the frame hasn't really been initialized yet, just
10498 toss it. */
10499 else if (INTERACTIVE && sf->glyphs_initialized_p)
10501 /* Get the frame containing the mini-buffer
10502 that the selected frame is using. */
10503 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10504 Lisp_Object frame = XWINDOW (mini_window)->frame;
10505 struct frame *f = XFRAME (frame);
10507 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10508 Fmake_frame_visible (frame);
10510 if (STRINGP (m) && SCHARS (m) > 0)
10512 set_message (m);
10513 if (minibuffer_auto_raise)
10514 Fraise_frame (frame);
10515 /* Assume we are not echoing.
10516 (If we are, echo_now will override this.) */
10517 echo_message_buffer = Qnil;
10519 else
10520 clear_message (true, true);
10522 do_pending_window_change (false);
10523 echo_area_display (true);
10524 do_pending_window_change (false);
10525 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10526 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10531 /* Display a null-terminated echo area message M. If M is 0, clear
10532 out any existing message, and let the mini-buffer text show through.
10534 The buffer M must continue to exist until after the echo area gets
10535 cleared or some other message gets displayed there. Do not pass
10536 text that is stored in a Lisp string. Do not pass text in a buffer
10537 that was alloca'd. */
10539 void
10540 message1 (const char *m)
10542 message3 (m ? build_unibyte_string (m) : Qnil);
10546 /* The non-logging counterpart of message1. */
10548 void
10549 message1_nolog (const char *m)
10551 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10554 /* Display a message M which contains a single %s
10555 which gets replaced with STRING. */
10557 void
10558 message_with_string (const char *m, Lisp_Object string, bool log)
10560 CHECK_STRING (string);
10562 bool need_message;
10563 if (noninteractive)
10564 need_message = !!m;
10565 else if (!INTERACTIVE)
10566 need_message = false;
10567 else
10569 /* The frame whose minibuffer we're going to display the message on.
10570 It may be larger than the selected frame, so we need
10571 to use its buffer, not the selected frame's buffer. */
10572 Lisp_Object mini_window;
10573 struct frame *f, *sf = SELECTED_FRAME ();
10575 /* Get the frame containing the minibuffer
10576 that the selected frame is using. */
10577 mini_window = FRAME_MINIBUF_WINDOW (sf);
10578 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10580 /* Error messages get reported properly by cmd_error, so this must be
10581 just an informative message; if the frame hasn't really been
10582 initialized yet, just toss it. */
10583 need_message = f->glyphs_initialized_p;
10586 if (need_message)
10588 AUTO_STRING (fmt, m);
10589 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10591 if (noninteractive)
10592 message_to_stderr (msg);
10593 else
10595 if (log)
10596 message3 (msg);
10597 else
10598 message3_nolog (msg);
10600 /* Print should start at the beginning of the message
10601 buffer next time. */
10602 message_buf_print = false;
10608 /* Dump an informative message to the minibuf. If M is 0, clear out
10609 any existing message, and let the mini-buffer text show through.
10611 The message must be safe ASCII (because when Emacs is
10612 non-interactive the message is sent straight to stderr without
10613 encoding first) and the format must not contain ` or ' (because
10614 this function does not account for `text-quoting-style'). If your
10615 message and format do not fit into this category, convert your
10616 arguments to Lisp objects and use Fmessage instead. */
10618 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10619 vmessage (const char *m, va_list ap)
10621 if (noninteractive)
10623 if (m)
10625 if (noninteractive_need_newline)
10626 putc ('\n', stderr);
10627 noninteractive_need_newline = false;
10628 vfprintf (stderr, m, ap);
10629 if (!cursor_in_echo_area)
10630 fprintf (stderr, "\n");
10631 fflush (stderr);
10634 else if (INTERACTIVE)
10636 /* The frame whose mini-buffer we're going to display the message
10637 on. It may be larger than the selected frame, so we need to
10638 use its buffer, not the selected frame's buffer. */
10639 Lisp_Object mini_window;
10640 struct frame *f, *sf = SELECTED_FRAME ();
10642 /* Get the frame containing the mini-buffer
10643 that the selected frame is using. */
10644 mini_window = FRAME_MINIBUF_WINDOW (sf);
10645 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10647 /* Error messages get reported properly by cmd_error, so this must be
10648 just an informative message; if the frame hasn't really been
10649 initialized yet, just toss it. */
10650 if (f->glyphs_initialized_p)
10652 if (m)
10654 ptrdiff_t len;
10655 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10656 USE_SAFE_ALLOCA;
10657 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10659 len = doprnt (message_buf, maxsize, m, 0, ap);
10661 message3 (make_string (message_buf, len));
10662 SAFE_FREE ();
10664 else
10665 message1 (0);
10667 /* Print should start at the beginning of the message
10668 buffer next time. */
10669 message_buf_print = false;
10674 /* See vmessage for restrictions on the text of the message. */
10675 void
10676 message (const char *m, ...)
10678 va_list ap;
10679 va_start (ap, m);
10680 vmessage (m, ap);
10681 va_end (ap);
10685 /* Display the current message in the current mini-buffer. This is
10686 only called from error handlers in process.c, and is not time
10687 critical. */
10689 void
10690 update_echo_area (void)
10692 if (!NILP (echo_area_buffer[0]))
10694 Lisp_Object string;
10695 string = Fcurrent_message ();
10696 message3 (string);
10701 /* Make sure echo area buffers in `echo_buffers' are live.
10702 If they aren't, make new ones. */
10704 static void
10705 ensure_echo_area_buffers (void)
10707 for (int i = 0; i < 2; i++)
10708 if (!BUFFERP (echo_buffer[i])
10709 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10711 Lisp_Object old_buffer = echo_buffer[i];
10712 static char const name_fmt[] = " *Echo Area %d*";
10713 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10714 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10715 echo_buffer[i] = Fget_buffer_create (lname);
10716 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10717 /* to force word wrap in echo area -
10718 it was decided to postpone this*/
10719 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10721 for (int j = 0; j < 2; j++)
10722 if (EQ (old_buffer, echo_area_buffer[j]))
10723 echo_area_buffer[j] = echo_buffer[i];
10728 /* Call FN with args A1..A2 with either the current or last displayed
10729 echo_area_buffer as current buffer.
10731 WHICH zero means use the current message buffer
10732 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10733 from echo_buffer[] and clear it.
10735 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10736 suitable buffer from echo_buffer[] and clear it.
10738 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10739 that the current message becomes the last displayed one, choose a
10740 suitable buffer for echo_area_buffer[0], and clear it.
10742 Value is what FN returns. */
10744 static bool
10745 with_echo_area_buffer (struct window *w, int which,
10746 bool (*fn) (ptrdiff_t, Lisp_Object),
10747 ptrdiff_t a1, Lisp_Object a2)
10749 Lisp_Object buffer;
10750 bool this_one, the_other, clear_buffer_p, rc;
10751 ptrdiff_t count = SPECPDL_INDEX ();
10753 /* If buffers aren't live, make new ones. */
10754 ensure_echo_area_buffers ();
10756 clear_buffer_p = false;
10758 if (which == 0)
10759 this_one = false, the_other = true;
10760 else if (which > 0)
10761 this_one = true, the_other = false;
10762 else
10764 this_one = false, the_other = true;
10765 clear_buffer_p = true;
10767 /* We need a fresh one in case the current echo buffer equals
10768 the one containing the last displayed echo area message. */
10769 if (!NILP (echo_area_buffer[this_one])
10770 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10771 echo_area_buffer[this_one] = Qnil;
10774 /* Choose a suitable buffer from echo_buffer[] if we don't
10775 have one. */
10776 if (NILP (echo_area_buffer[this_one]))
10778 echo_area_buffer[this_one]
10779 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10780 ? echo_buffer[the_other]
10781 : echo_buffer[this_one]);
10782 clear_buffer_p = true;
10785 buffer = echo_area_buffer[this_one];
10787 /* Don't get confused by reusing the buffer used for echoing
10788 for a different purpose. */
10789 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10790 cancel_echoing ();
10792 record_unwind_protect (unwind_with_echo_area_buffer,
10793 with_echo_area_buffer_unwind_data (w));
10795 /* Make the echo area buffer current. Note that for display
10796 purposes, it is not necessary that the displayed window's buffer
10797 == current_buffer, except for text property lookup. So, let's
10798 only set that buffer temporarily here without doing a full
10799 Fset_window_buffer. We must also change w->pointm, though,
10800 because otherwise an assertions in unshow_buffer fails, and Emacs
10801 aborts. */
10802 set_buffer_internal_1 (XBUFFER (buffer));
10803 if (w)
10805 wset_buffer (w, buffer);
10806 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10807 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10810 bset_undo_list (current_buffer, Qt);
10811 bset_read_only (current_buffer, Qnil);
10812 specbind (Qinhibit_read_only, Qt);
10813 specbind (Qinhibit_modification_hooks, Qt);
10815 if (clear_buffer_p && Z > BEG)
10816 del_range (BEG, Z);
10818 eassert (BEGV >= BEG);
10819 eassert (ZV <= Z && ZV >= BEGV);
10821 rc = fn (a1, a2);
10823 eassert (BEGV >= BEG);
10824 eassert (ZV <= Z && ZV >= BEGV);
10826 unbind_to (count, Qnil);
10827 return rc;
10831 /* Save state that should be preserved around the call to the function
10832 FN called in with_echo_area_buffer. */
10834 static Lisp_Object
10835 with_echo_area_buffer_unwind_data (struct window *w)
10837 int i = 0;
10838 Lisp_Object vector, tmp;
10840 /* Reduce consing by keeping one vector in
10841 Vwith_echo_area_save_vector. */
10842 vector = Vwith_echo_area_save_vector;
10843 Vwith_echo_area_save_vector = Qnil;
10845 if (NILP (vector))
10846 vector = Fmake_vector (make_number (11), Qnil);
10848 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10849 ASET (vector, i, Vdeactivate_mark); ++i;
10850 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10852 if (w)
10854 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10855 ASET (vector, i, w->contents); ++i;
10856 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10857 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10858 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10859 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10860 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10861 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10863 else
10865 int end = i + 8;
10866 for (; i < end; ++i)
10867 ASET (vector, i, Qnil);
10870 eassert (i == ASIZE (vector));
10871 return vector;
10875 /* Restore global state from VECTOR which was created by
10876 with_echo_area_buffer_unwind_data. */
10878 static void
10879 unwind_with_echo_area_buffer (Lisp_Object vector)
10881 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10882 Vdeactivate_mark = AREF (vector, 1);
10883 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10885 if (WINDOWP (AREF (vector, 3)))
10887 struct window *w;
10888 Lisp_Object buffer;
10890 w = XWINDOW (AREF (vector, 3));
10891 buffer = AREF (vector, 4);
10893 wset_buffer (w, buffer);
10894 set_marker_both (w->pointm, buffer,
10895 XFASTINT (AREF (vector, 5)),
10896 XFASTINT (AREF (vector, 6)));
10897 set_marker_both (w->old_pointm, buffer,
10898 XFASTINT (AREF (vector, 7)),
10899 XFASTINT (AREF (vector, 8)));
10900 set_marker_both (w->start, buffer,
10901 XFASTINT (AREF (vector, 9)),
10902 XFASTINT (AREF (vector, 10)));
10905 Vwith_echo_area_save_vector = vector;
10909 /* Set up the echo area for use by print functions. MULTIBYTE_P
10910 means we will print multibyte. */
10912 void
10913 setup_echo_area_for_printing (bool multibyte_p)
10915 /* If we can't find an echo area any more, exit. */
10916 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10917 Fkill_emacs (Qnil);
10919 ensure_echo_area_buffers ();
10921 if (!message_buf_print)
10923 /* A message has been output since the last time we printed.
10924 Choose a fresh echo area buffer. */
10925 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10926 echo_area_buffer[0] = echo_buffer[1];
10927 else
10928 echo_area_buffer[0] = echo_buffer[0];
10930 /* Switch to that buffer and clear it. */
10931 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10932 bset_truncate_lines (current_buffer, Qnil);
10934 if (Z > BEG)
10936 ptrdiff_t count = SPECPDL_INDEX ();
10937 specbind (Qinhibit_read_only, Qt);
10938 /* Note that undo recording is always disabled. */
10939 del_range (BEG, Z);
10940 unbind_to (count, Qnil);
10942 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10944 /* Set up the buffer for the multibyteness we need. */
10945 if (multibyte_p
10946 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10947 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10949 /* Raise the frame containing the echo area. */
10950 if (minibuffer_auto_raise)
10952 struct frame *sf = SELECTED_FRAME ();
10953 Lisp_Object mini_window;
10954 mini_window = FRAME_MINIBUF_WINDOW (sf);
10955 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10958 message_log_maybe_newline ();
10959 message_buf_print = true;
10961 else
10963 if (NILP (echo_area_buffer[0]))
10965 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10966 echo_area_buffer[0] = echo_buffer[1];
10967 else
10968 echo_area_buffer[0] = echo_buffer[0];
10971 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10973 /* Someone switched buffers between print requests. */
10974 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10975 bset_truncate_lines (current_buffer, Qnil);
10981 /* Display an echo area message in window W. Value is true if W's
10982 height is changed. If display_last_displayed_message_p,
10983 display the message that was last displayed, otherwise
10984 display the current message. */
10986 static bool
10987 display_echo_area (struct window *w)
10989 bool no_message_p, window_height_changed_p;
10991 /* Temporarily disable garbage collections while displaying the echo
10992 area. This is done because a GC can print a message itself.
10993 That message would modify the echo area buffer's contents while a
10994 redisplay of the buffer is going on, and seriously confuse
10995 redisplay. */
10996 ptrdiff_t count = inhibit_garbage_collection ();
10998 /* If there is no message, we must call display_echo_area_1
10999 nevertheless because it resizes the window. But we will have to
11000 reset the echo_area_buffer in question to nil at the end because
11001 with_echo_area_buffer will sets it to an empty buffer. */
11002 bool i = display_last_displayed_message_p;
11003 /* According to the C99, C11 and C++11 standards, the integral value
11004 of a "bool" is always 0 or 1, so this array access is safe here,
11005 if oddly typed. */
11006 no_message_p = NILP (echo_area_buffer[i]);
11008 window_height_changed_p
11009 = with_echo_area_buffer (w, display_last_displayed_message_p,
11010 display_echo_area_1,
11011 (intptr_t) w, Qnil);
11013 if (no_message_p)
11014 echo_area_buffer[i] = Qnil;
11016 unbind_to (count, Qnil);
11017 return window_height_changed_p;
11021 /* Helper for display_echo_area. Display the current buffer which
11022 contains the current echo area message in window W, a mini-window,
11023 a pointer to which is passed in A1. A2..A4 are currently not used.
11024 Change the height of W so that all of the message is displayed.
11025 Value is true if height of W was changed. */
11027 static bool
11028 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11030 intptr_t i1 = a1;
11031 struct window *w = (struct window *) i1;
11032 Lisp_Object window;
11033 struct text_pos start;
11035 /* We are about to enter redisplay without going through
11036 redisplay_internal, so we need to forget these faces by hand
11037 here. */
11038 forget_escape_and_glyphless_faces ();
11040 /* Do this before displaying, so that we have a large enough glyph
11041 matrix for the display. If we can't get enough space for the
11042 whole text, display the last N lines. That works by setting w->start. */
11043 bool window_height_changed_p = resize_mini_window (w, false);
11045 /* Use the starting position chosen by resize_mini_window. */
11046 SET_TEXT_POS_FROM_MARKER (start, w->start);
11048 /* Display. */
11049 clear_glyph_matrix (w->desired_matrix);
11050 XSETWINDOW (window, w);
11051 try_window (window, start, 0);
11053 return window_height_changed_p;
11057 /* Resize the echo area window to exactly the size needed for the
11058 currently displayed message, if there is one. If a mini-buffer
11059 is active, don't shrink it. */
11061 void
11062 resize_echo_area_exactly (void)
11064 if (BUFFERP (echo_area_buffer[0])
11065 && WINDOWP (echo_area_window))
11067 struct window *w = XWINDOW (echo_area_window);
11068 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11069 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11070 (intptr_t) w, resize_exactly);
11071 if (resized_p)
11073 windows_or_buffers_changed = 42;
11074 update_mode_lines = 30;
11075 redisplay_internal ();
11081 /* Callback function for with_echo_area_buffer, when used from
11082 resize_echo_area_exactly. A1 contains a pointer to the window to
11083 resize, EXACTLY non-nil means resize the mini-window exactly to the
11084 size of the text displayed. A3 and A4 are not used. Value is what
11085 resize_mini_window returns. */
11087 static bool
11088 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11090 intptr_t i1 = a1;
11091 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11095 /* Resize mini-window W to fit the size of its contents. EXACT_P
11096 means size the window exactly to the size needed. Otherwise, it's
11097 only enlarged until W's buffer is empty.
11099 Set W->start to the right place to begin display. If the whole
11100 contents fit, start at the beginning. Otherwise, start so as
11101 to make the end of the contents appear. This is particularly
11102 important for y-or-n-p, but seems desirable generally.
11104 Value is true if the window height has been changed. */
11106 bool
11107 resize_mini_window (struct window *w, bool exact_p)
11109 struct frame *f = XFRAME (w->frame);
11110 bool window_height_changed_p = false;
11112 eassert (MINI_WINDOW_P (w));
11114 /* By default, start display at the beginning. */
11115 set_marker_both (w->start, w->contents,
11116 BUF_BEGV (XBUFFER (w->contents)),
11117 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11119 /* Don't resize windows while redisplaying a window; it would
11120 confuse redisplay functions when the size of the window they are
11121 displaying changes from under them. Such a resizing can happen,
11122 for instance, when which-func prints a long message while
11123 we are running fontification-functions. We're running these
11124 functions with safe_call which binds inhibit-redisplay to t. */
11125 if (!NILP (Vinhibit_redisplay))
11126 return false;
11128 /* Nil means don't try to resize. */
11129 if (NILP (Vresize_mini_windows)
11130 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11131 return false;
11133 if (!FRAME_MINIBUF_ONLY_P (f))
11135 struct it it;
11136 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11137 + WINDOW_PIXEL_HEIGHT (w));
11138 int unit = FRAME_LINE_HEIGHT (f);
11139 int height, max_height;
11140 struct text_pos start;
11141 struct buffer *old_current_buffer = NULL;
11143 if (current_buffer != XBUFFER (w->contents))
11145 old_current_buffer = current_buffer;
11146 set_buffer_internal (XBUFFER (w->contents));
11149 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11151 /* Compute the max. number of lines specified by the user. */
11152 if (FLOATP (Vmax_mini_window_height))
11153 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11154 else if (INTEGERP (Vmax_mini_window_height))
11155 max_height = XINT (Vmax_mini_window_height) * unit;
11156 else
11157 max_height = total_height / 4;
11159 /* Correct that max. height if it's bogus. */
11160 max_height = clip_to_bounds (unit, max_height, total_height);
11162 /* Find out the height of the text in the window. */
11163 if (it.line_wrap == TRUNCATE)
11164 height = unit;
11165 else
11167 last_height = 0;
11168 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11169 if (it.max_ascent == 0 && it.max_descent == 0)
11170 height = it.current_y + last_height;
11171 else
11172 height = it.current_y + it.max_ascent + it.max_descent;
11173 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11176 /* Compute a suitable window start. */
11177 if (height > max_height)
11179 height = (max_height / unit) * unit;
11180 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11181 move_it_vertically_backward (&it, height - unit);
11182 start = it.current.pos;
11184 else
11185 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11186 SET_MARKER_FROM_TEXT_POS (w->start, start);
11188 if (EQ (Vresize_mini_windows, Qgrow_only))
11190 /* Let it grow only, until we display an empty message, in which
11191 case the window shrinks again. */
11192 if (height > WINDOW_PIXEL_HEIGHT (w))
11194 int old_height = WINDOW_PIXEL_HEIGHT (w);
11196 FRAME_WINDOWS_FROZEN (f) = true;
11197 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11198 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11200 else if (height < WINDOW_PIXEL_HEIGHT (w)
11201 && (exact_p || BEGV == ZV))
11203 int old_height = WINDOW_PIXEL_HEIGHT (w);
11205 FRAME_WINDOWS_FROZEN (f) = false;
11206 shrink_mini_window (w, true);
11207 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11210 else
11212 /* Always resize to exact size needed. */
11213 if (height > WINDOW_PIXEL_HEIGHT (w))
11215 int old_height = WINDOW_PIXEL_HEIGHT (w);
11217 FRAME_WINDOWS_FROZEN (f) = true;
11218 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11219 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11221 else if (height < WINDOW_PIXEL_HEIGHT (w))
11223 int old_height = WINDOW_PIXEL_HEIGHT (w);
11225 FRAME_WINDOWS_FROZEN (f) = false;
11226 shrink_mini_window (w, true);
11228 if (height)
11230 FRAME_WINDOWS_FROZEN (f) = true;
11231 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11234 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11238 if (old_current_buffer)
11239 set_buffer_internal (old_current_buffer);
11242 return window_height_changed_p;
11246 /* Value is the current message, a string, or nil if there is no
11247 current message. */
11249 Lisp_Object
11250 current_message (void)
11252 Lisp_Object msg;
11254 if (!BUFFERP (echo_area_buffer[0]))
11255 msg = Qnil;
11256 else
11258 with_echo_area_buffer (0, 0, current_message_1,
11259 (intptr_t) &msg, Qnil);
11260 if (NILP (msg))
11261 echo_area_buffer[0] = Qnil;
11264 return msg;
11268 static bool
11269 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11271 intptr_t i1 = a1;
11272 Lisp_Object *msg = (Lisp_Object *) i1;
11274 if (Z > BEG)
11275 *msg = make_buffer_string (BEG, Z, true);
11276 else
11277 *msg = Qnil;
11278 return false;
11282 /* Push the current message on Vmessage_stack for later restoration
11283 by restore_message. Value is true if the current message isn't
11284 empty. This is a relatively infrequent operation, so it's not
11285 worth optimizing. */
11287 bool
11288 push_message (void)
11290 Lisp_Object msg = current_message ();
11291 Vmessage_stack = Fcons (msg, Vmessage_stack);
11292 return STRINGP (msg);
11296 /* Restore message display from the top of Vmessage_stack. */
11298 void
11299 restore_message (void)
11301 eassert (CONSP (Vmessage_stack));
11302 message3_nolog (XCAR (Vmessage_stack));
11306 /* Handler for unwind-protect calling pop_message. */
11308 void
11309 pop_message_unwind (void)
11311 /* Pop the top-most entry off Vmessage_stack. */
11312 eassert (CONSP (Vmessage_stack));
11313 Vmessage_stack = XCDR (Vmessage_stack);
11317 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11318 exits. If the stack is not empty, we have a missing pop_message
11319 somewhere. */
11321 void
11322 check_message_stack (void)
11324 if (!NILP (Vmessage_stack))
11325 emacs_abort ();
11329 /* Truncate to NCHARS what will be displayed in the echo area the next
11330 time we display it---but don't redisplay it now. */
11332 void
11333 truncate_echo_area (ptrdiff_t nchars)
11335 if (nchars == 0)
11336 echo_area_buffer[0] = Qnil;
11337 else if (!noninteractive
11338 && INTERACTIVE
11339 && !NILP (echo_area_buffer[0]))
11341 struct frame *sf = SELECTED_FRAME ();
11342 /* Error messages get reported properly by cmd_error, so this must be
11343 just an informative message; if the frame hasn't really been
11344 initialized yet, just toss it. */
11345 if (sf->glyphs_initialized_p)
11346 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11351 /* Helper function for truncate_echo_area. Truncate the current
11352 message to at most NCHARS characters. */
11354 static bool
11355 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11357 if (BEG + nchars < Z)
11358 del_range (BEG + nchars, Z);
11359 if (Z == BEG)
11360 echo_area_buffer[0] = Qnil;
11361 return false;
11364 /* Set the current message to STRING. */
11366 static void
11367 set_message (Lisp_Object string)
11369 eassert (STRINGP (string));
11371 message_enable_multibyte = STRING_MULTIBYTE (string);
11373 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11374 message_buf_print = false;
11375 help_echo_showing_p = false;
11377 if (STRINGP (Vdebug_on_message)
11378 && STRINGP (string)
11379 && fast_string_match (Vdebug_on_message, string) >= 0)
11380 call_debugger (list2 (Qerror, string));
11384 /* Helper function for set_message. First argument is ignored and second
11385 argument has the same meaning as for set_message.
11386 This function is called with the echo area buffer being current. */
11388 static bool
11389 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11391 eassert (STRINGP (string));
11393 /* Change multibyteness of the echo buffer appropriately. */
11394 if (message_enable_multibyte
11395 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11396 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11398 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11399 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11400 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11402 /* Insert new message at BEG. */
11403 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11405 /* This function takes care of single/multibyte conversion.
11406 We just have to ensure that the echo area buffer has the right
11407 setting of enable_multibyte_characters. */
11408 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11410 return false;
11414 /* Clear messages. CURRENT_P means clear the current message.
11415 LAST_DISPLAYED_P means clear the message last displayed. */
11417 void
11418 clear_message (bool current_p, bool last_displayed_p)
11420 if (current_p)
11422 echo_area_buffer[0] = Qnil;
11423 message_cleared_p = true;
11426 if (last_displayed_p)
11427 echo_area_buffer[1] = Qnil;
11429 message_buf_print = false;
11432 /* Clear garbaged frames.
11434 This function is used where the old redisplay called
11435 redraw_garbaged_frames which in turn called redraw_frame which in
11436 turn called clear_frame. The call to clear_frame was a source of
11437 flickering. I believe a clear_frame is not necessary. It should
11438 suffice in the new redisplay to invalidate all current matrices,
11439 and ensure a complete redisplay of all windows. */
11441 static void
11442 clear_garbaged_frames (void)
11444 if (frame_garbaged)
11446 Lisp_Object tail, frame;
11447 struct frame *sf = SELECTED_FRAME ();
11449 FOR_EACH_FRAME (tail, frame)
11451 struct frame *f = XFRAME (frame);
11453 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11455 if (f->resized_p
11456 /* It makes no sense to redraw a non-selected TTY
11457 frame, since that will actually clear the
11458 selected frame, and might leave the selected
11459 frame with corrupted display, if it happens not
11460 to be marked garbaged. */
11461 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11462 redraw_frame (f);
11463 else
11464 clear_current_matrices (f);
11466 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11467 x_clear_under_internal_border (f);
11468 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11470 fset_redisplay (f);
11471 f->garbaged = false;
11472 f->resized_p = false;
11476 frame_garbaged = false;
11481 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11482 selected_frame. */
11484 static void
11485 echo_area_display (bool update_frame_p)
11487 Lisp_Object mini_window;
11488 struct window *w;
11489 struct frame *f;
11490 bool window_height_changed_p = false;
11491 struct frame *sf = SELECTED_FRAME ();
11493 mini_window = FRAME_MINIBUF_WINDOW (sf);
11494 w = XWINDOW (mini_window);
11495 f = XFRAME (WINDOW_FRAME (w));
11497 /* Don't display if frame is invisible or not yet initialized. */
11498 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11499 return;
11501 #ifdef HAVE_WINDOW_SYSTEM
11502 /* When Emacs starts, selected_frame may be the initial terminal
11503 frame. If we let this through, a message would be displayed on
11504 the terminal. */
11505 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11506 return;
11507 #endif /* HAVE_WINDOW_SYSTEM */
11509 /* Redraw garbaged frames. */
11510 clear_garbaged_frames ();
11512 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11514 echo_area_window = mini_window;
11515 window_height_changed_p = display_echo_area (w);
11516 w->must_be_updated_p = true;
11518 /* Update the display, unless called from redisplay_internal.
11519 Also don't update the screen during redisplay itself. The
11520 update will happen at the end of redisplay, and an update
11521 here could cause confusion. */
11522 if (update_frame_p && !redisplaying_p)
11524 int n = 0;
11526 /* If the display update has been interrupted by pending
11527 input, update mode lines in the frame. Due to the
11528 pending input, it might have been that redisplay hasn't
11529 been called, so that mode lines above the echo area are
11530 garbaged. This looks odd, so we prevent it here. */
11531 if (!display_completed)
11533 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11535 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11536 x_clear_under_internal_border (f);
11537 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11541 if (window_height_changed_p
11542 /* Don't do this if Emacs is shutting down. Redisplay
11543 needs to run hooks. */
11544 && !NILP (Vrun_hooks))
11546 /* Must update other windows. Likewise as in other
11547 cases, don't let this update be interrupted by
11548 pending input. */
11549 ptrdiff_t count = SPECPDL_INDEX ();
11550 specbind (Qredisplay_dont_pause, Qt);
11551 fset_redisplay (f);
11552 redisplay_internal ();
11553 unbind_to (count, Qnil);
11555 else if (FRAME_WINDOW_P (f) && n == 0)
11557 /* Window configuration is the same as before.
11558 Can do with a display update of the echo area,
11559 unless we displayed some mode lines. */
11560 update_single_window (w);
11561 flush_frame (f);
11563 else
11564 update_frame (f, true, true);
11566 /* If cursor is in the echo area, make sure that the next
11567 redisplay displays the minibuffer, so that the cursor will
11568 be replaced with what the minibuffer wants. */
11569 if (cursor_in_echo_area)
11570 wset_redisplay (XWINDOW (mini_window));
11573 else if (!EQ (mini_window, selected_window))
11574 wset_redisplay (XWINDOW (mini_window));
11576 /* Last displayed message is now the current message. */
11577 echo_area_buffer[1] = echo_area_buffer[0];
11578 /* Inform read_char that we're not echoing. */
11579 echo_message_buffer = Qnil;
11581 /* Prevent redisplay optimization in redisplay_internal by resetting
11582 this_line_start_pos. This is done because the mini-buffer now
11583 displays the message instead of its buffer text. */
11584 if (EQ (mini_window, selected_window))
11585 CHARPOS (this_line_start_pos) = 0;
11587 if (window_height_changed_p)
11589 fset_redisplay (f);
11591 /* If window configuration was changed, frames may have been
11592 marked garbaged. Clear them or we will experience
11593 surprises wrt scrolling.
11594 FIXME: How/why/when? */
11595 clear_garbaged_frames ();
11599 /* True if W's buffer was changed but not saved. */
11601 static bool
11602 window_buffer_changed (struct window *w)
11604 struct buffer *b = XBUFFER (w->contents);
11606 eassert (BUFFER_LIVE_P (b));
11608 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11611 /* True if W has %c or %C in its mode line and mode line should be updated. */
11613 static bool
11614 mode_line_update_needed (struct window *w)
11616 return (w->column_number_displayed != -1
11617 && !(PT == w->last_point && !window_outdated (w))
11618 && (w->column_number_displayed != current_column ()));
11621 /* True if window start of W is frozen and may not be changed during
11622 redisplay. */
11624 static bool
11625 window_frozen_p (struct window *w)
11627 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11629 Lisp_Object window;
11631 XSETWINDOW (window, w);
11632 if (MINI_WINDOW_P (w))
11633 return false;
11634 else if (EQ (window, selected_window))
11635 return false;
11636 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11637 && EQ (window, Vminibuf_scroll_window))
11638 /* This special window can't be frozen too. */
11639 return false;
11640 else
11641 return true;
11643 return false;
11646 /***********************************************************************
11647 Mode Lines and Frame Titles
11648 ***********************************************************************/
11650 /* A buffer for constructing non-propertized mode-line strings and
11651 frame titles in it; allocated from the heap in init_xdisp and
11652 resized as needed in store_mode_line_noprop_char. */
11654 static char *mode_line_noprop_buf;
11656 /* The buffer's end, and a current output position in it. */
11658 static char *mode_line_noprop_buf_end;
11659 static char *mode_line_noprop_ptr;
11661 #define MODE_LINE_NOPROP_LEN(start) \
11662 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11664 static enum {
11665 MODE_LINE_DISPLAY = 0,
11666 MODE_LINE_TITLE,
11667 MODE_LINE_NOPROP,
11668 MODE_LINE_STRING
11669 } mode_line_target;
11671 /* Alist that caches the results of :propertize.
11672 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11673 static Lisp_Object mode_line_proptrans_alist;
11675 /* List of strings making up the mode-line. */
11676 static Lisp_Object mode_line_string_list;
11678 /* Base face property when building propertized mode line string. */
11679 static Lisp_Object mode_line_string_face;
11680 static Lisp_Object mode_line_string_face_prop;
11683 /* Unwind data for mode line strings */
11685 static Lisp_Object Vmode_line_unwind_vector;
11687 static Lisp_Object
11688 format_mode_line_unwind_data (struct frame *target_frame,
11689 struct buffer *obuf,
11690 Lisp_Object owin,
11691 bool save_proptrans)
11693 Lisp_Object vector, tmp;
11695 /* Reduce consing by keeping one vector in
11696 Vwith_echo_area_save_vector. */
11697 vector = Vmode_line_unwind_vector;
11698 Vmode_line_unwind_vector = Qnil;
11700 if (NILP (vector))
11701 vector = Fmake_vector (make_number (10), Qnil);
11703 ASET (vector, 0, make_number (mode_line_target));
11704 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11705 ASET (vector, 2, mode_line_string_list);
11706 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11707 ASET (vector, 4, mode_line_string_face);
11708 ASET (vector, 5, mode_line_string_face_prop);
11710 if (obuf)
11711 XSETBUFFER (tmp, obuf);
11712 else
11713 tmp = Qnil;
11714 ASET (vector, 6, tmp);
11715 ASET (vector, 7, owin);
11716 if (target_frame)
11718 /* Similarly to `with-selected-window', if the operation selects
11719 a window on another frame, we must restore that frame's
11720 selected window, and (for a tty) the top-frame. */
11721 ASET (vector, 8, target_frame->selected_window);
11722 if (FRAME_TERMCAP_P (target_frame))
11723 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11726 return vector;
11729 static void
11730 unwind_format_mode_line (Lisp_Object vector)
11732 Lisp_Object old_window = AREF (vector, 7);
11733 Lisp_Object target_frame_window = AREF (vector, 8);
11734 Lisp_Object old_top_frame = AREF (vector, 9);
11736 mode_line_target = XINT (AREF (vector, 0));
11737 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11738 mode_line_string_list = AREF (vector, 2);
11739 if (! EQ (AREF (vector, 3), Qt))
11740 mode_line_proptrans_alist = AREF (vector, 3);
11741 mode_line_string_face = AREF (vector, 4);
11742 mode_line_string_face_prop = AREF (vector, 5);
11744 /* Select window before buffer, since it may change the buffer. */
11745 if (!NILP (old_window))
11747 /* If the operation that we are unwinding had selected a window
11748 on a different frame, reset its frame-selected-window. For a
11749 text terminal, reset its top-frame if necessary. */
11750 if (!NILP (target_frame_window))
11752 Lisp_Object frame
11753 = WINDOW_FRAME (XWINDOW (target_frame_window));
11755 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11756 Fselect_window (target_frame_window, Qt);
11758 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11759 Fselect_frame (old_top_frame, Qt);
11762 Fselect_window (old_window, Qt);
11765 if (!NILP (AREF (vector, 6)))
11767 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11768 ASET (vector, 6, Qnil);
11771 Vmode_line_unwind_vector = vector;
11775 /* Store a single character C for the frame title in mode_line_noprop_buf.
11776 Re-allocate mode_line_noprop_buf if necessary. */
11778 static void
11779 store_mode_line_noprop_char (char c)
11781 /* If output position has reached the end of the allocated buffer,
11782 increase the buffer's size. */
11783 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11785 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11786 ptrdiff_t size = len;
11787 mode_line_noprop_buf =
11788 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11789 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11790 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11793 *mode_line_noprop_ptr++ = c;
11797 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11798 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11799 characters that yield more columns than PRECISION; PRECISION <= 0
11800 means copy the whole string. Pad with spaces until FIELD_WIDTH
11801 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11802 pad. Called from display_mode_element when it is used to build a
11803 frame title. */
11805 static int
11806 store_mode_line_noprop (const char *string, int field_width, int precision)
11808 const unsigned char *str = (const unsigned char *) string;
11809 int n = 0;
11810 ptrdiff_t dummy, nbytes;
11812 /* Copy at most PRECISION chars from STR. */
11813 nbytes = strlen (string);
11814 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11815 while (nbytes--)
11816 store_mode_line_noprop_char (*str++);
11818 /* Fill up with spaces until FIELD_WIDTH reached. */
11819 while (field_width > 0
11820 && n < field_width)
11822 store_mode_line_noprop_char (' ');
11823 ++n;
11826 return n;
11829 /***********************************************************************
11830 Frame Titles
11831 ***********************************************************************/
11833 #ifdef HAVE_WINDOW_SYSTEM
11835 /* Set the title of FRAME, if it has changed. The title format is
11836 Vicon_title_format if FRAME is iconified, otherwise it is
11837 frame_title_format. */
11839 static void
11840 x_consider_frame_title (Lisp_Object frame)
11842 struct frame *f = XFRAME (frame);
11844 if ((FRAME_WINDOW_P (f)
11845 || FRAME_MINIBUF_ONLY_P (f)
11846 || f->explicit_name)
11847 && NILP (Fframe_parameter (frame, Qtooltip)))
11849 /* Do we have more than one visible frame on this X display? */
11850 Lisp_Object tail, other_frame, fmt;
11851 ptrdiff_t title_start;
11852 char *title;
11853 ptrdiff_t len;
11854 struct it it;
11855 ptrdiff_t count = SPECPDL_INDEX ();
11857 FOR_EACH_FRAME (tail, other_frame)
11859 struct frame *tf = XFRAME (other_frame);
11861 if (tf != f
11862 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11863 && !FRAME_MINIBUF_ONLY_P (tf)
11864 && !EQ (other_frame, tip_frame)
11865 && !FRAME_PARENT_FRAME (tf)
11866 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11867 break;
11870 /* Set global variable indicating that multiple frames exist. */
11871 multiple_frames = CONSP (tail);
11873 /* Switch to the buffer of selected window of the frame. Set up
11874 mode_line_target so that display_mode_element will output into
11875 mode_line_noprop_buf; then display the title. */
11876 record_unwind_protect (unwind_format_mode_line,
11877 format_mode_line_unwind_data
11878 (f, current_buffer, selected_window, false));
11879 /* select-frame calls resize_mini_window, which could resize the
11880 mini-window and by that undo the effect of this redisplay
11881 cycle wrt minibuffer and echo-area display. Binding
11882 inhibit-redisplay to t makes the call to resize_mini_window a
11883 no-op, thus avoiding the adverse side effects. */
11884 specbind (Qinhibit_redisplay, Qt);
11886 Fselect_window (f->selected_window, Qt);
11887 set_buffer_internal_1
11888 (XBUFFER (XWINDOW (f->selected_window)->contents));
11889 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11891 mode_line_target = MODE_LINE_TITLE;
11892 title_start = MODE_LINE_NOPROP_LEN (0);
11893 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11894 NULL, DEFAULT_FACE_ID);
11895 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11896 len = MODE_LINE_NOPROP_LEN (title_start);
11897 title = mode_line_noprop_buf + title_start;
11898 unbind_to (count, Qnil);
11900 /* Set the title only if it's changed. This avoids consing in
11901 the common case where it hasn't. (If it turns out that we've
11902 already wasted too much time by walking through the list with
11903 display_mode_element, then we might need to optimize at a
11904 higher level than this.) */
11905 if (! STRINGP (f->name)
11906 || SBYTES (f->name) != len
11907 || memcmp (title, SDATA (f->name), len) != 0)
11908 x_implicitly_set_name (f, make_string (title, len), Qnil);
11912 #endif /* not HAVE_WINDOW_SYSTEM */
11915 /***********************************************************************
11916 Menu Bars
11917 ***********************************************************************/
11919 /* True if we will not redisplay all visible windows. */
11920 #define REDISPLAY_SOME_P() \
11921 ((windows_or_buffers_changed == 0 \
11922 || windows_or_buffers_changed == REDISPLAY_SOME) \
11923 && (update_mode_lines == 0 \
11924 || update_mode_lines == REDISPLAY_SOME))
11926 /* Prepare for redisplay by updating menu-bar item lists when
11927 appropriate. This can call eval. */
11929 static void
11930 prepare_menu_bars (void)
11932 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11933 bool some_windows = REDISPLAY_SOME_P ();
11934 Lisp_Object tooltip_frame;
11936 #ifdef HAVE_WINDOW_SYSTEM
11937 tooltip_frame = tip_frame;
11938 #else
11939 tooltip_frame = Qnil;
11940 #endif
11942 if (FUNCTIONP (Vpre_redisplay_function))
11944 Lisp_Object windows = all_windows ? Qt : Qnil;
11945 if (all_windows && some_windows)
11947 Lisp_Object ws = window_list ();
11948 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11950 Lisp_Object this = XCAR (ws);
11951 struct window *w = XWINDOW (this);
11952 if (w->redisplay
11953 || XFRAME (w->frame)->redisplay
11954 || XBUFFER (w->contents)->text->redisplay)
11956 windows = Fcons (this, windows);
11960 safe__call1 (true, Vpre_redisplay_function, windows);
11963 /* Update all frame titles based on their buffer names, etc. We do
11964 this before the menu bars so that the buffer-menu will show the
11965 up-to-date frame titles. */
11966 #ifdef HAVE_WINDOW_SYSTEM
11967 if (all_windows)
11969 Lisp_Object tail, frame;
11971 FOR_EACH_FRAME (tail, frame)
11973 struct frame *f = XFRAME (frame);
11974 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11975 if (some_windows
11976 && !f->redisplay
11977 && !w->redisplay
11978 && !XBUFFER (w->contents)->text->redisplay)
11979 continue;
11981 if (!EQ (frame, tooltip_frame)
11982 && !FRAME_PARENT_FRAME (f)
11983 && (FRAME_ICONIFIED_P (f)
11984 || FRAME_VISIBLE_P (f) == 1
11985 /* Exclude TTY frames that are obscured because they
11986 are not the top frame on their console. This is
11987 because x_consider_frame_title actually switches
11988 to the frame, which for TTY frames means it is
11989 marked as garbaged, and will be completely
11990 redrawn on the next redisplay cycle. This causes
11991 TTY frames to be completely redrawn, when there
11992 are more than one of them, even though nothing
11993 should be changed on display. */
11994 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11995 x_consider_frame_title (frame);
11998 #endif /* HAVE_WINDOW_SYSTEM */
12000 /* Update the menu bar item lists, if appropriate. This has to be
12001 done before any actual redisplay or generation of display lines. */
12003 if (all_windows)
12005 Lisp_Object tail, frame;
12006 ptrdiff_t count = SPECPDL_INDEX ();
12007 /* True means that update_menu_bar has run its hooks
12008 so any further calls to update_menu_bar shouldn't do so again. */
12009 bool menu_bar_hooks_run = false;
12011 record_unwind_save_match_data ();
12013 FOR_EACH_FRAME (tail, frame)
12015 struct frame *f = XFRAME (frame);
12016 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12018 /* Ignore tooltip frame. */
12019 if (EQ (frame, tooltip_frame))
12020 continue;
12022 if (some_windows
12023 && !f->redisplay
12024 && !w->redisplay
12025 && !XBUFFER (w->contents)->text->redisplay)
12026 continue;
12028 run_window_size_change_functions (frame);
12030 if (FRAME_PARENT_FRAME (f))
12031 continue;
12033 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12034 #ifdef HAVE_WINDOW_SYSTEM
12035 update_tool_bar (f, false);
12036 #endif
12039 unbind_to (count, Qnil);
12041 else
12043 struct frame *sf = SELECTED_FRAME ();
12044 update_menu_bar (sf, true, false);
12045 #ifdef HAVE_WINDOW_SYSTEM
12046 update_tool_bar (sf, true);
12047 #endif
12052 /* Update the menu bar item list for frame F. This has to be done
12053 before we start to fill in any display lines, because it can call
12054 eval.
12056 If SAVE_MATCH_DATA, we must save and restore it here.
12058 If HOOKS_RUN, a previous call to update_menu_bar
12059 already ran the menu bar hooks for this redisplay, so there
12060 is no need to run them again. The return value is the
12061 updated value of this flag, to pass to the next call. */
12063 static bool
12064 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12066 Lisp_Object window;
12067 struct window *w;
12069 /* If called recursively during a menu update, do nothing. This can
12070 happen when, for instance, an activate-menubar-hook causes a
12071 redisplay. */
12072 if (inhibit_menubar_update)
12073 return hooks_run;
12075 window = FRAME_SELECTED_WINDOW (f);
12076 w = XWINDOW (window);
12078 if (FRAME_WINDOW_P (f)
12080 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12081 || defined (HAVE_NS) || defined (USE_GTK)
12082 FRAME_EXTERNAL_MENU_BAR (f)
12083 #else
12084 FRAME_MENU_BAR_LINES (f) > 0
12085 #endif
12086 : FRAME_MENU_BAR_LINES (f) > 0)
12088 /* If the user has switched buffers or windows, we need to
12089 recompute to reflect the new bindings. But we'll
12090 recompute when update_mode_lines is set too; that means
12091 that people can use force-mode-line-update to request
12092 that the menu bar be recomputed. The adverse effect on
12093 the rest of the redisplay algorithm is about the same as
12094 windows_or_buffers_changed anyway. */
12095 if (windows_or_buffers_changed
12096 /* This used to test w->update_mode_line, but we believe
12097 there is no need to recompute the menu in that case. */
12098 || update_mode_lines
12099 || window_buffer_changed (w))
12101 struct buffer *prev = current_buffer;
12102 ptrdiff_t count = SPECPDL_INDEX ();
12104 specbind (Qinhibit_menubar_update, Qt);
12106 set_buffer_internal_1 (XBUFFER (w->contents));
12107 if (save_match_data)
12108 record_unwind_save_match_data ();
12109 if (NILP (Voverriding_local_map_menu_flag))
12111 specbind (Qoverriding_terminal_local_map, Qnil);
12112 specbind (Qoverriding_local_map, Qnil);
12115 if (!hooks_run)
12117 /* Run the Lucid hook. */
12118 safe_run_hooks (Qactivate_menubar_hook);
12120 /* If it has changed current-menubar from previous value,
12121 really recompute the menu-bar from the value. */
12122 if (! NILP (Vlucid_menu_bar_dirty_flag))
12123 call0 (Qrecompute_lucid_menubar);
12125 safe_run_hooks (Qmenu_bar_update_hook);
12127 hooks_run = true;
12130 XSETFRAME (Vmenu_updating_frame, f);
12131 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12133 /* Redisplay the menu bar in case we changed it. */
12134 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12135 || defined (HAVE_NS) || defined (USE_GTK)
12136 if (FRAME_WINDOW_P (f))
12138 #if defined (HAVE_NS)
12139 /* All frames on Mac OS share the same menubar. So only
12140 the selected frame should be allowed to set it. */
12141 if (f == SELECTED_FRAME ())
12142 #endif
12143 set_frame_menubar (f, false, false);
12145 else
12146 /* On a terminal screen, the menu bar is an ordinary screen
12147 line, and this makes it get updated. */
12148 w->update_mode_line = true;
12149 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12150 /* In the non-toolkit version, the menu bar is an ordinary screen
12151 line, and this makes it get updated. */
12152 w->update_mode_line = true;
12153 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12155 unbind_to (count, Qnil);
12156 set_buffer_internal_1 (prev);
12160 return hooks_run;
12163 /***********************************************************************
12164 Tool-bars
12165 ***********************************************************************/
12167 #ifdef HAVE_WINDOW_SYSTEM
12169 /* Select `frame' temporarily without running all the code in
12170 do_switch_frame.
12171 FIXME: Maybe do_switch_frame should be trimmed down similarly
12172 when `norecord' is set. */
12173 static void
12174 fast_set_selected_frame (Lisp_Object frame)
12176 if (!EQ (selected_frame, frame))
12178 selected_frame = frame;
12179 selected_window = XFRAME (frame)->selected_window;
12183 /* Update the tool-bar item list for frame F. This has to be done
12184 before we start to fill in any display lines. Called from
12185 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12186 and restore it here. */
12188 static void
12189 update_tool_bar (struct frame *f, bool save_match_data)
12191 #if defined (USE_GTK) || defined (HAVE_NS)
12192 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12193 #else
12194 bool do_update = (WINDOWP (f->tool_bar_window)
12195 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12196 #endif
12198 if (do_update)
12200 Lisp_Object window;
12201 struct window *w;
12203 window = FRAME_SELECTED_WINDOW (f);
12204 w = XWINDOW (window);
12206 /* If the user has switched buffers or windows, we need to
12207 recompute to reflect the new bindings. But we'll
12208 recompute when update_mode_lines is set too; that means
12209 that people can use force-mode-line-update to request
12210 that the menu bar be recomputed. The adverse effect on
12211 the rest of the redisplay algorithm is about the same as
12212 windows_or_buffers_changed anyway. */
12213 if (windows_or_buffers_changed
12214 || w->update_mode_line
12215 || update_mode_lines
12216 || window_buffer_changed (w))
12218 struct buffer *prev = current_buffer;
12219 ptrdiff_t count = SPECPDL_INDEX ();
12220 Lisp_Object frame, new_tool_bar;
12221 int new_n_tool_bar;
12223 /* Set current_buffer to the buffer of the selected
12224 window of the frame, so that we get the right local
12225 keymaps. */
12226 set_buffer_internal_1 (XBUFFER (w->contents));
12228 /* Save match data, if we must. */
12229 if (save_match_data)
12230 record_unwind_save_match_data ();
12232 /* Make sure that we don't accidentally use bogus keymaps. */
12233 if (NILP (Voverriding_local_map_menu_flag))
12235 specbind (Qoverriding_terminal_local_map, Qnil);
12236 specbind (Qoverriding_local_map, Qnil);
12239 /* We must temporarily set the selected frame to this frame
12240 before calling tool_bar_items, because the calculation of
12241 the tool-bar keymap uses the selected frame (see
12242 `tool-bar-make-keymap' in tool-bar.el). */
12243 eassert (EQ (selected_window,
12244 /* Since we only explicitly preserve selected_frame,
12245 check that selected_window would be redundant. */
12246 XFRAME (selected_frame)->selected_window));
12247 record_unwind_protect (fast_set_selected_frame, selected_frame);
12248 XSETFRAME (frame, f);
12249 fast_set_selected_frame (frame);
12251 /* Build desired tool-bar items from keymaps. */
12252 new_tool_bar
12253 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12254 &new_n_tool_bar);
12256 /* Redisplay the tool-bar if we changed it. */
12257 if (new_n_tool_bar != f->n_tool_bar_items
12258 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12260 /* Redisplay that happens asynchronously due to an expose event
12261 may access f->tool_bar_items. Make sure we update both
12262 variables within BLOCK_INPUT so no such event interrupts. */
12263 block_input ();
12264 fset_tool_bar_items (f, new_tool_bar);
12265 f->n_tool_bar_items = new_n_tool_bar;
12266 w->update_mode_line = true;
12267 unblock_input ();
12270 unbind_to (count, Qnil);
12271 set_buffer_internal_1 (prev);
12276 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12278 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12279 F's desired tool-bar contents. F->tool_bar_items must have
12280 been set up previously by calling prepare_menu_bars. */
12282 static void
12283 build_desired_tool_bar_string (struct frame *f)
12285 int i, size, size_needed;
12286 Lisp_Object image, plist;
12288 image = plist = Qnil;
12290 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12291 Otherwise, make a new string. */
12293 /* The size of the string we might be able to reuse. */
12294 size = (STRINGP (f->desired_tool_bar_string)
12295 ? SCHARS (f->desired_tool_bar_string)
12296 : 0);
12298 /* We need one space in the string for each image. */
12299 size_needed = f->n_tool_bar_items;
12301 /* Reuse f->desired_tool_bar_string, if possible. */
12302 if (size < size_needed || NILP (f->desired_tool_bar_string))
12303 fset_desired_tool_bar_string
12304 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12305 else
12307 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12308 Fremove_text_properties (make_number (0), make_number (size),
12309 props, f->desired_tool_bar_string);
12312 /* Put a `display' property on the string for the images to display,
12313 put a `menu_item' property on tool-bar items with a value that
12314 is the index of the item in F's tool-bar item vector. */
12315 for (i = 0; i < f->n_tool_bar_items; ++i)
12317 #define PROP(IDX) \
12318 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12320 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12321 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12322 int hmargin, vmargin, relief, idx, end;
12324 /* If image is a vector, choose the image according to the
12325 button state. */
12326 image = PROP (TOOL_BAR_ITEM_IMAGES);
12327 if (VECTORP (image))
12329 if (enabled_p)
12330 idx = (selected_p
12331 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12332 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12333 else
12334 idx = (selected_p
12335 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12336 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12338 eassert (ASIZE (image) >= idx);
12339 image = AREF (image, idx);
12341 else
12342 idx = -1;
12344 /* Ignore invalid image specifications. */
12345 if (!valid_image_p (image))
12346 continue;
12348 /* Display the tool-bar button pressed, or depressed. */
12349 plist = Fcopy_sequence (XCDR (image));
12351 /* Compute margin and relief to draw. */
12352 relief = (tool_bar_button_relief >= 0
12353 ? tool_bar_button_relief
12354 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12355 hmargin = vmargin = relief;
12357 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12358 INT_MAX - max (hmargin, vmargin)))
12360 hmargin += XFASTINT (Vtool_bar_button_margin);
12361 vmargin += XFASTINT (Vtool_bar_button_margin);
12363 else if (CONSP (Vtool_bar_button_margin))
12365 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12366 INT_MAX - hmargin))
12367 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12369 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12370 INT_MAX - vmargin))
12371 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12374 if (auto_raise_tool_bar_buttons_p)
12376 /* Add a `:relief' property to the image spec if the item is
12377 selected. */
12378 if (selected_p)
12380 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12381 hmargin -= relief;
12382 vmargin -= relief;
12385 else
12387 /* If image is selected, display it pressed, i.e. with a
12388 negative relief. If it's not selected, display it with a
12389 raised relief. */
12390 plist = Fplist_put (plist, QCrelief,
12391 (selected_p
12392 ? make_number (-relief)
12393 : make_number (relief)));
12394 hmargin -= relief;
12395 vmargin -= relief;
12398 /* Put a margin around the image. */
12399 if (hmargin || vmargin)
12401 if (hmargin == vmargin)
12402 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12403 else
12404 plist = Fplist_put (plist, QCmargin,
12405 Fcons (make_number (hmargin),
12406 make_number (vmargin)));
12409 /* If button is not enabled, and we don't have special images
12410 for the disabled state, make the image appear disabled by
12411 applying an appropriate algorithm to it. */
12412 if (!enabled_p && idx < 0)
12413 plist = Fplist_put (plist, QCconversion, Qdisabled);
12415 /* Put a `display' text property on the string for the image to
12416 display. Put a `menu-item' property on the string that gives
12417 the start of this item's properties in the tool-bar items
12418 vector. */
12419 image = Fcons (Qimage, plist);
12420 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12421 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12423 /* Let the last image hide all remaining spaces in the tool bar
12424 string. The string can be longer than needed when we reuse a
12425 previous string. */
12426 if (i + 1 == f->n_tool_bar_items)
12427 end = SCHARS (f->desired_tool_bar_string);
12428 else
12429 end = i + 1;
12430 Fadd_text_properties (make_number (i), make_number (end),
12431 props, f->desired_tool_bar_string);
12432 #undef PROP
12437 /* Display one line of the tool-bar of frame IT->f.
12439 HEIGHT specifies the desired height of the tool-bar line.
12440 If the actual height of the glyph row is less than HEIGHT, the
12441 row's height is increased to HEIGHT, and the icons are centered
12442 vertically in the new height.
12444 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12445 count a final empty row in case the tool-bar width exactly matches
12446 the window width.
12449 static void
12450 display_tool_bar_line (struct it *it, int height)
12452 struct glyph_row *row = it->glyph_row;
12453 int max_x = it->last_visible_x;
12454 struct glyph *last;
12456 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12457 clear_glyph_row (row);
12458 row->enabled_p = true;
12459 row->y = it->current_y;
12461 /* Note that this isn't made use of if the face hasn't a box,
12462 so there's no need to check the face here. */
12463 it->start_of_box_run_p = true;
12465 while (it->current_x < max_x)
12467 int x, n_glyphs_before, i, nglyphs;
12468 struct it it_before;
12470 /* Get the next display element. */
12471 if (!get_next_display_element (it))
12473 /* Don't count empty row if we are counting needed tool-bar lines. */
12474 if (height < 0 && !it->hpos)
12475 return;
12476 break;
12479 /* Produce glyphs. */
12480 n_glyphs_before = row->used[TEXT_AREA];
12481 it_before = *it;
12483 PRODUCE_GLYPHS (it);
12485 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12486 i = 0;
12487 x = it_before.current_x;
12488 while (i < nglyphs)
12490 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12492 if (x + glyph->pixel_width > max_x)
12494 /* Glyph doesn't fit on line. Backtrack. */
12495 row->used[TEXT_AREA] = n_glyphs_before;
12496 *it = it_before;
12497 /* If this is the only glyph on this line, it will never fit on the
12498 tool-bar, so skip it. But ensure there is at least one glyph,
12499 so we don't accidentally disable the tool-bar. */
12500 if (n_glyphs_before == 0
12501 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12502 break;
12503 goto out;
12506 ++it->hpos;
12507 x += glyph->pixel_width;
12508 ++i;
12511 /* Stop at line end. */
12512 if (ITERATOR_AT_END_OF_LINE_P (it))
12513 break;
12515 set_iterator_to_next (it, true);
12518 out:;
12520 row->displays_text_p = row->used[TEXT_AREA] != 0;
12522 /* Use default face for the border below the tool bar.
12524 FIXME: When auto-resize-tool-bars is grow-only, there is
12525 no additional border below the possibly empty tool-bar lines.
12526 So to make the extra empty lines look "normal", we have to
12527 use the tool-bar face for the border too. */
12528 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12529 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12530 it->face_id = DEFAULT_FACE_ID;
12532 extend_face_to_end_of_line (it);
12533 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12534 last->right_box_line_p = true;
12535 if (last == row->glyphs[TEXT_AREA])
12536 last->left_box_line_p = true;
12538 /* Make line the desired height and center it vertically. */
12539 if ((height -= it->max_ascent + it->max_descent) > 0)
12541 /* Don't add more than one line height. */
12542 height %= FRAME_LINE_HEIGHT (it->f);
12543 it->max_ascent += height / 2;
12544 it->max_descent += (height + 1) / 2;
12547 compute_line_metrics (it);
12549 /* If line is empty, make it occupy the rest of the tool-bar. */
12550 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12552 row->height = row->phys_height = it->last_visible_y - row->y;
12553 row->visible_height = row->height;
12554 row->ascent = row->phys_ascent = 0;
12555 row->extra_line_spacing = 0;
12558 row->full_width_p = true;
12559 row->continued_p = false;
12560 row->truncated_on_left_p = false;
12561 row->truncated_on_right_p = false;
12563 it->current_x = it->hpos = 0;
12564 it->current_y += row->height;
12565 ++it->vpos;
12566 ++it->glyph_row;
12570 /* Value is the number of pixels needed to make all tool-bar items of
12571 frame F visible. The actual number of glyph rows needed is
12572 returned in *N_ROWS if non-NULL. */
12573 static int
12574 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12576 struct window *w = XWINDOW (f->tool_bar_window);
12577 struct it it;
12578 /* tool_bar_height is called from redisplay_tool_bar after building
12579 the desired matrix, so use (unused) mode-line row as temporary row to
12580 avoid destroying the first tool-bar row. */
12581 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12583 /* Initialize an iterator for iteration over
12584 F->desired_tool_bar_string in the tool-bar window of frame F. */
12585 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12586 temp_row->reversed_p = false;
12587 it.first_visible_x = 0;
12588 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12589 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12590 it.paragraph_embedding = L2R;
12592 while (!ITERATOR_AT_END_P (&it))
12594 clear_glyph_row (temp_row);
12595 it.glyph_row = temp_row;
12596 display_tool_bar_line (&it, -1);
12598 clear_glyph_row (temp_row);
12600 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12601 if (n_rows)
12602 *n_rows = it.vpos > 0 ? it.vpos : -1;
12604 if (pixelwise)
12605 return it.current_y;
12606 else
12607 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12610 #endif /* !USE_GTK && !HAVE_NS */
12612 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12613 0, 2, 0,
12614 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12615 If FRAME is nil or omitted, use the selected frame. Optional argument
12616 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12617 (Lisp_Object frame, Lisp_Object pixelwise)
12619 int height = 0;
12621 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12622 struct frame *f = decode_any_frame (frame);
12624 if (WINDOWP (f->tool_bar_window)
12625 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12627 update_tool_bar (f, true);
12628 if (f->n_tool_bar_items)
12630 build_desired_tool_bar_string (f);
12631 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12634 #endif
12636 return make_number (height);
12640 /* Display the tool-bar of frame F. Value is true if tool-bar's
12641 height should be changed. */
12642 static bool
12643 redisplay_tool_bar (struct frame *f)
12645 f->tool_bar_redisplayed = true;
12646 #if defined (USE_GTK) || defined (HAVE_NS)
12648 if (FRAME_EXTERNAL_TOOL_BAR (f))
12649 update_frame_tool_bar (f);
12650 return false;
12652 #else /* !USE_GTK && !HAVE_NS */
12654 struct window *w;
12655 struct it it;
12656 struct glyph_row *row;
12658 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12659 do anything. This means you must start with tool-bar-lines
12660 non-zero to get the auto-sizing effect. Or in other words, you
12661 can turn off tool-bars by specifying tool-bar-lines zero. */
12662 if (!WINDOWP (f->tool_bar_window)
12663 || (w = XWINDOW (f->tool_bar_window),
12664 WINDOW_TOTAL_LINES (w) == 0))
12665 return false;
12667 /* Set up an iterator for the tool-bar window. */
12668 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12669 it.first_visible_x = 0;
12670 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12671 row = it.glyph_row;
12672 row->reversed_p = false;
12674 /* Build a string that represents the contents of the tool-bar. */
12675 build_desired_tool_bar_string (f);
12676 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12677 /* FIXME: This should be controlled by a user option. But it
12678 doesn't make sense to have an R2L tool bar if the menu bar cannot
12679 be drawn also R2L, and making the menu bar R2L is tricky due
12680 toolkit-specific code that implements it. If an R2L tool bar is
12681 ever supported, display_tool_bar_line should also be augmented to
12682 call unproduce_glyphs like display_line and display_string
12683 do. */
12684 it.paragraph_embedding = L2R;
12686 if (f->n_tool_bar_rows == 0)
12688 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12690 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12692 x_change_tool_bar_height (f, new_height);
12693 frame_default_tool_bar_height = new_height;
12694 /* Always do that now. */
12695 clear_glyph_matrix (w->desired_matrix);
12696 f->fonts_changed = true;
12697 return true;
12701 /* Display as many lines as needed to display all tool-bar items. */
12703 if (f->n_tool_bar_rows > 0)
12705 int border, rows, height, extra;
12707 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12708 border = XINT (Vtool_bar_border);
12709 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12710 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12711 else if (EQ (Vtool_bar_border, Qborder_width))
12712 border = f->border_width;
12713 else
12714 border = 0;
12715 if (border < 0)
12716 border = 0;
12718 rows = f->n_tool_bar_rows;
12719 height = max (1, (it.last_visible_y - border) / rows);
12720 extra = it.last_visible_y - border - height * rows;
12722 while (it.current_y < it.last_visible_y)
12724 int h = 0;
12725 if (extra > 0 && rows-- > 0)
12727 h = (extra + rows - 1) / rows;
12728 extra -= h;
12730 display_tool_bar_line (&it, height + h);
12733 else
12735 while (it.current_y < it.last_visible_y)
12736 display_tool_bar_line (&it, 0);
12739 /* It doesn't make much sense to try scrolling in the tool-bar
12740 window, so don't do it. */
12741 w->desired_matrix->no_scrolling_p = true;
12742 w->must_be_updated_p = true;
12744 if (!NILP (Vauto_resize_tool_bars))
12746 bool change_height_p = true;
12748 /* If we couldn't display everything, change the tool-bar's
12749 height if there is room for more. */
12750 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12751 change_height_p = true;
12753 /* We subtract 1 because display_tool_bar_line advances the
12754 glyph_row pointer before returning to its caller. We want to
12755 examine the last glyph row produced by
12756 display_tool_bar_line. */
12757 row = it.glyph_row - 1;
12759 /* If there are blank lines at the end, except for a partially
12760 visible blank line at the end that is smaller than
12761 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12762 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12763 && row->height >= FRAME_LINE_HEIGHT (f))
12764 change_height_p = true;
12766 /* If row displays tool-bar items, but is partially visible,
12767 change the tool-bar's height. */
12768 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12769 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12770 change_height_p = true;
12772 /* Resize windows as needed by changing the `tool-bar-lines'
12773 frame parameter. */
12774 if (change_height_p)
12776 int nrows;
12777 int new_height = tool_bar_height (f, &nrows, true);
12779 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12780 && !f->minimize_tool_bar_window_p)
12781 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12782 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12783 f->minimize_tool_bar_window_p = false;
12785 if (change_height_p)
12787 x_change_tool_bar_height (f, new_height);
12788 frame_default_tool_bar_height = new_height;
12789 clear_glyph_matrix (w->desired_matrix);
12790 f->n_tool_bar_rows = nrows;
12791 f->fonts_changed = true;
12793 return true;
12798 f->minimize_tool_bar_window_p = false;
12799 return false;
12801 #endif /* USE_GTK || HAVE_NS */
12804 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12806 /* Get information about the tool-bar item which is displayed in GLYPH
12807 on frame F. Return in *PROP_IDX the index where tool-bar item
12808 properties start in F->tool_bar_items. Value is false if
12809 GLYPH doesn't display a tool-bar item. */
12811 static bool
12812 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12814 Lisp_Object prop;
12815 int charpos;
12817 /* This function can be called asynchronously, which means we must
12818 exclude any possibility that Fget_text_property signals an
12819 error. */
12820 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12821 charpos = max (0, charpos);
12823 /* Get the text property `menu-item' at pos. The value of that
12824 property is the start index of this item's properties in
12825 F->tool_bar_items. */
12826 prop = Fget_text_property (make_number (charpos),
12827 Qmenu_item, f->current_tool_bar_string);
12828 if (! INTEGERP (prop))
12829 return false;
12830 *prop_idx = XINT (prop);
12831 return true;
12835 /* Get information about the tool-bar item at position X/Y on frame F.
12836 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12837 the current matrix of the tool-bar window of F, or NULL if not
12838 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12839 item in F->tool_bar_items. Value is
12841 -1 if X/Y is not on a tool-bar item
12842 0 if X/Y is on the same item that was highlighted before.
12843 1 otherwise. */
12845 static int
12846 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12847 int *hpos, int *vpos, int *prop_idx)
12849 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12850 struct window *w = XWINDOW (f->tool_bar_window);
12851 int area;
12853 /* Find the glyph under X/Y. */
12854 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12855 if (*glyph == NULL)
12856 return -1;
12858 /* Get the start of this tool-bar item's properties in
12859 f->tool_bar_items. */
12860 if (!tool_bar_item_info (f, *glyph, prop_idx))
12861 return -1;
12863 /* Is mouse on the highlighted item? */
12864 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12865 && *vpos >= hlinfo->mouse_face_beg_row
12866 && *vpos <= hlinfo->mouse_face_end_row
12867 && (*vpos > hlinfo->mouse_face_beg_row
12868 || *hpos >= hlinfo->mouse_face_beg_col)
12869 && (*vpos < hlinfo->mouse_face_end_row
12870 || *hpos < hlinfo->mouse_face_end_col
12871 || hlinfo->mouse_face_past_end))
12872 return 0;
12874 return 1;
12878 /* EXPORT:
12879 Handle mouse button event on the tool-bar of frame F, at
12880 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12881 false for button release. MODIFIERS is event modifiers for button
12882 release. */
12884 void
12885 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12886 int modifiers)
12888 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12889 struct window *w = XWINDOW (f->tool_bar_window);
12890 int hpos, vpos, prop_idx;
12891 struct glyph *glyph;
12892 Lisp_Object enabled_p;
12893 int ts;
12895 /* If not on the highlighted tool-bar item, and mouse-highlight is
12896 non-nil, return. This is so we generate the tool-bar button
12897 click only when the mouse button is released on the same item as
12898 where it was pressed. However, when mouse-highlight is disabled,
12899 generate the click when the button is released regardless of the
12900 highlight, since tool-bar items are not highlighted in that
12901 case. */
12902 frame_to_window_pixel_xy (w, &x, &y);
12903 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12904 if (ts == -1
12905 || (ts != 0 && !NILP (Vmouse_highlight)))
12906 return;
12908 /* When mouse-highlight is off, generate the click for the item
12909 where the button was pressed, disregarding where it was
12910 released. */
12911 if (NILP (Vmouse_highlight) && !down_p)
12912 prop_idx = f->last_tool_bar_item;
12914 /* If item is disabled, do nothing. */
12915 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12916 if (NILP (enabled_p))
12917 return;
12919 if (down_p)
12921 /* Show item in pressed state. */
12922 if (!NILP (Vmouse_highlight))
12923 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12924 f->last_tool_bar_item = prop_idx;
12926 else
12928 Lisp_Object key, frame;
12929 struct input_event event;
12930 EVENT_INIT (event);
12932 /* Show item in released state. */
12933 if (!NILP (Vmouse_highlight))
12934 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12936 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12938 XSETFRAME (frame, f);
12939 event.kind = TOOL_BAR_EVENT;
12940 event.frame_or_window = frame;
12941 event.arg = frame;
12942 kbd_buffer_store_event (&event);
12944 event.kind = TOOL_BAR_EVENT;
12945 event.frame_or_window = frame;
12946 event.arg = key;
12947 event.modifiers = modifiers;
12948 kbd_buffer_store_event (&event);
12949 f->last_tool_bar_item = -1;
12954 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12955 tool-bar window-relative coordinates X/Y. Called from
12956 note_mouse_highlight. */
12958 static void
12959 note_tool_bar_highlight (struct frame *f, int x, int y)
12961 Lisp_Object window = f->tool_bar_window;
12962 struct window *w = XWINDOW (window);
12963 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12964 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12965 int hpos, vpos;
12966 struct glyph *glyph;
12967 struct glyph_row *row;
12968 int i;
12969 Lisp_Object enabled_p;
12970 int prop_idx;
12971 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12972 bool mouse_down_p;
12973 int rc;
12975 /* Function note_mouse_highlight is called with negative X/Y
12976 values when mouse moves outside of the frame. */
12977 if (x <= 0 || y <= 0)
12979 clear_mouse_face (hlinfo);
12980 return;
12983 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12984 if (rc < 0)
12986 /* Not on tool-bar item. */
12987 clear_mouse_face (hlinfo);
12988 return;
12990 else if (rc == 0)
12991 /* On same tool-bar item as before. */
12992 goto set_help_echo;
12994 clear_mouse_face (hlinfo);
12996 /* Mouse is down, but on different tool-bar item? */
12997 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12998 && f == dpyinfo->last_mouse_frame);
13000 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13001 return;
13003 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13005 /* If tool-bar item is not enabled, don't highlight it. */
13006 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13007 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13009 /* Compute the x-position of the glyph. In front and past the
13010 image is a space. We include this in the highlighted area. */
13011 row = MATRIX_ROW (w->current_matrix, vpos);
13012 for (i = x = 0; i < hpos; ++i)
13013 x += row->glyphs[TEXT_AREA][i].pixel_width;
13015 /* Record this as the current active region. */
13016 hlinfo->mouse_face_beg_col = hpos;
13017 hlinfo->mouse_face_beg_row = vpos;
13018 hlinfo->mouse_face_beg_x = x;
13019 hlinfo->mouse_face_past_end = false;
13021 hlinfo->mouse_face_end_col = hpos + 1;
13022 hlinfo->mouse_face_end_row = vpos;
13023 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13024 hlinfo->mouse_face_window = window;
13025 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13027 /* Display it as active. */
13028 show_mouse_face (hlinfo, draw);
13031 set_help_echo:
13033 /* Set help_echo_string to a help string to display for this tool-bar item.
13034 XTread_socket does the rest. */
13035 help_echo_object = help_echo_window = Qnil;
13036 help_echo_pos = -1;
13037 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13038 if (NILP (help_echo_string))
13039 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13042 #endif /* !USE_GTK && !HAVE_NS */
13044 #endif /* HAVE_WINDOW_SYSTEM */
13048 /************************************************************************
13049 Horizontal scrolling
13050 ************************************************************************/
13052 /* For all leaf windows in the window tree rooted at WINDOW, set their
13053 hscroll value so that PT is (i) visible in the window, and (ii) so
13054 that it is not within a certain margin at the window's left and
13055 right border. Value is true if any window's hscroll has been
13056 changed. */
13058 static bool
13059 hscroll_window_tree (Lisp_Object window)
13061 bool hscrolled_p = false;
13062 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13063 int hscroll_step_abs = 0;
13064 double hscroll_step_rel = 0;
13066 if (hscroll_relative_p)
13068 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13069 if (hscroll_step_rel < 0)
13071 hscroll_relative_p = false;
13072 hscroll_step_abs = 0;
13075 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13077 hscroll_step_abs = XINT (Vhscroll_step);
13078 if (hscroll_step_abs < 0)
13079 hscroll_step_abs = 0;
13081 else
13082 hscroll_step_abs = 0;
13084 while (WINDOWP (window))
13086 struct window *w = XWINDOW (window);
13088 if (WINDOWP (w->contents))
13089 hscrolled_p |= hscroll_window_tree (w->contents);
13090 else if (w->cursor.vpos >= 0)
13092 int h_margin;
13093 int text_area_width;
13094 struct glyph_row *cursor_row;
13095 struct glyph_row *bottom_row;
13097 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13098 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13099 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13100 else
13101 cursor_row = bottom_row - 1;
13103 if (!cursor_row->enabled_p)
13105 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13106 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13107 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13108 else
13109 cursor_row = bottom_row - 1;
13111 bool row_r2l_p = cursor_row->reversed_p;
13112 bool hscl = hscrolling_current_line_p (w);
13113 int x_offset = 0;
13114 /* When line numbers are displayed, we need to account for
13115 the horizontal space they consume. */
13116 if (!NILP (Vdisplay_line_numbers))
13118 struct glyph *g;
13119 if (!row_r2l_p)
13121 for (g = cursor_row->glyphs[TEXT_AREA];
13122 g < cursor_row->glyphs[TEXT_AREA]
13123 + cursor_row->used[TEXT_AREA];
13124 g++)
13126 if (!(NILP (g->object) && g->charpos < 0))
13127 break;
13128 x_offset += g->pixel_width;
13131 else
13133 for (g = cursor_row->glyphs[TEXT_AREA]
13134 + cursor_row->used[TEXT_AREA];
13135 g > cursor_row->glyphs[TEXT_AREA];
13136 g--)
13138 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13139 break;
13140 x_offset += (g - 1)->pixel_width;
13144 if (cursor_row->truncated_on_left_p)
13146 /* On TTY frames, don't count the left truncation glyph. */
13147 struct frame *f = XFRAME (WINDOW_FRAME (w));
13148 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13151 text_area_width = window_box_width (w, TEXT_AREA);
13153 /* Scroll when cursor is inside this scroll margin. */
13154 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13156 /* If the position of this window's point has explicitly
13157 changed, no more suspend auto hscrolling. */
13158 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13159 w->suspend_auto_hscroll = false;
13161 /* Remember window point. */
13162 Fset_marker (w->old_pointm,
13163 ((w == XWINDOW (selected_window))
13164 ? make_number (BUF_PT (XBUFFER (w->contents)))
13165 : Fmarker_position (w->pointm)),
13166 w->contents);
13168 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13169 && !w->suspend_auto_hscroll
13170 /* In some pathological cases, like restoring a window
13171 configuration into a frame that is much smaller than
13172 the one from which the configuration was saved, we
13173 get glyph rows whose start and end have zero buffer
13174 positions, which we cannot handle below. Just skip
13175 such windows. */
13176 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13177 /* For left-to-right rows, hscroll when cursor is either
13178 (i) inside the right hscroll margin, or (ii) if it is
13179 inside the left margin and the window is already
13180 hscrolled. */
13181 && ((!row_r2l_p
13182 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13183 || (cursor_row->enabled_p
13184 && cursor_row->truncated_on_right_p
13185 && (w->cursor.x >= text_area_width - h_margin))))
13186 /* For right-to-left rows, the logic is similar,
13187 except that rules for scrolling to left and right
13188 are reversed. E.g., if cursor.x <= h_margin, we
13189 need to hscroll "to the right" unconditionally,
13190 and that will scroll the screen to the left so as
13191 to reveal the next portion of the row. */
13192 || (row_r2l_p
13193 && ((cursor_row->enabled_p
13194 /* FIXME: It is confusing to set the
13195 truncated_on_right_p flag when R2L rows
13196 are actually truncated on the left. */
13197 && cursor_row->truncated_on_right_p
13198 && w->cursor.x <= h_margin)
13199 || (w->hscroll
13200 && (w->cursor.x >= (text_area_width - h_margin
13201 - x_offset)))))
13202 /* This last condition is needed when moving
13203 vertically from an hscrolled line to a short line
13204 that doesn't need to be hscrolled. If we omit
13205 this condition, the line from which we move will
13206 remain hscrolled. */
13207 || (hscl
13208 && w->hscroll != w->min_hscroll
13209 && !cursor_row->truncated_on_left_p)))
13211 struct it it;
13212 ptrdiff_t hscroll;
13213 struct buffer *saved_current_buffer;
13214 ptrdiff_t pt;
13215 int wanted_x;
13217 /* Find point in a display of infinite width. */
13218 saved_current_buffer = current_buffer;
13219 current_buffer = XBUFFER (w->contents);
13221 if (w == XWINDOW (selected_window))
13222 pt = PT;
13223 else
13224 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13226 /* Move iterator to pt starting at cursor_row->start in
13227 a line with infinite width. */
13228 init_to_row_start (&it, w, cursor_row);
13229 if (hscl)
13230 it.first_visible_x = window_hscroll_limited (w, it.f)
13231 * FRAME_COLUMN_WIDTH (it.f);
13232 it.last_visible_x = DISP_INFINITY;
13233 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13234 /* If the line ends in an overlay string with a newline,
13235 we might infloop, because displaying the window will
13236 want to put the cursor after the overlay, i.e. at X
13237 coordinate of zero on the next screen line. So we
13238 use the buffer position prior to the overlay string
13239 instead. */
13240 if (it.method == GET_FROM_STRING && pt > 1)
13242 init_to_row_start (&it, w, cursor_row);
13243 if (hscl)
13244 it.first_visible_x = (window_hscroll_limited (w, it.f)
13245 * FRAME_COLUMN_WIDTH (it.f));
13246 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13248 current_buffer = saved_current_buffer;
13250 /* Position cursor in window. */
13251 if (!hscroll_relative_p && hscroll_step_abs == 0)
13252 hscroll = max (0, (it.current_x
13253 - (ITERATOR_AT_END_OF_LINE_P (&it)
13254 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13255 : (text_area_width / 2))))
13256 / FRAME_COLUMN_WIDTH (it.f);
13257 else if ((!row_r2l_p
13258 && w->cursor.x >= text_area_width - h_margin)
13259 || (row_r2l_p && w->cursor.x <= h_margin))
13261 if (hscroll_relative_p)
13262 wanted_x = text_area_width * (1 - hscroll_step_rel)
13263 - h_margin;
13264 else
13265 wanted_x = text_area_width
13266 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13267 - h_margin;
13268 hscroll
13269 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13271 else
13273 if (hscroll_relative_p)
13274 wanted_x = text_area_width * hscroll_step_rel
13275 + h_margin;
13276 else
13277 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13278 + h_margin;
13279 hscroll
13280 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13282 hscroll = max (hscroll, w->min_hscroll);
13284 /* Don't prevent redisplay optimizations if hscroll
13285 hasn't changed, as it will unnecessarily slow down
13286 redisplay. */
13287 if (w->hscroll != hscroll
13288 /* When hscrolling only the current line, we need to
13289 report hscroll even if its value is equal to the
13290 previous one, because the new line might need a
13291 different value. */
13292 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13294 struct buffer *b = XBUFFER (w->contents);
13295 b->prevent_redisplay_optimizations_p = true;
13296 w->hscroll = hscroll;
13297 hscrolled_p = true;
13302 window = w->next;
13305 /* Value is true if hscroll of any leaf window has been changed. */
13306 return hscrolled_p;
13310 /* Set hscroll so that cursor is visible and not inside horizontal
13311 scroll margins for all windows in the tree rooted at WINDOW. See
13312 also hscroll_window_tree above. Value is true if any window's
13313 hscroll has been changed. If it has, desired matrices on the frame
13314 of WINDOW are cleared. */
13316 static bool
13317 hscroll_windows (Lisp_Object window)
13319 bool hscrolled_p = hscroll_window_tree (window);
13320 if (hscrolled_p)
13321 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13322 return hscrolled_p;
13327 /************************************************************************
13328 Redisplay
13329 ************************************************************************/
13331 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13332 This is sometimes handy to have in a debugger session. */
13334 #ifdef GLYPH_DEBUG
13336 /* First and last unchanged row for try_window_id. */
13338 static int debug_first_unchanged_at_end_vpos;
13339 static int debug_last_unchanged_at_beg_vpos;
13341 /* Delta vpos and y. */
13343 static int debug_dvpos, debug_dy;
13345 /* Delta in characters and bytes for try_window_id. */
13347 static ptrdiff_t debug_delta, debug_delta_bytes;
13349 /* Values of window_end_pos and window_end_vpos at the end of
13350 try_window_id. */
13352 static ptrdiff_t debug_end_vpos;
13354 /* Append a string to W->desired_matrix->method. FMT is a printf
13355 format string. If trace_redisplay_p is true also printf the
13356 resulting string to stderr. */
13358 static void debug_method_add (struct window *, char const *, ...)
13359 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13361 static void
13362 debug_method_add (struct window *w, char const *fmt, ...)
13364 void *ptr = w;
13365 char *method = w->desired_matrix->method;
13366 int len = strlen (method);
13367 int size = sizeof w->desired_matrix->method;
13368 int remaining = size - len - 1;
13369 va_list ap;
13371 if (len && remaining)
13373 method[len] = '|';
13374 --remaining, ++len;
13377 va_start (ap, fmt);
13378 vsnprintf (method + len, remaining + 1, fmt, ap);
13379 va_end (ap);
13381 if (trace_redisplay_p)
13382 fprintf (stderr, "%p (%s): %s\n",
13383 ptr,
13384 ((BUFFERP (w->contents)
13385 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13386 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13387 : "no buffer"),
13388 method + len);
13391 #endif /* GLYPH_DEBUG */
13394 /* Value is true if all changes in window W, which displays
13395 current_buffer, are in the text between START and END. START is a
13396 buffer position, END is given as a distance from Z. Used in
13397 redisplay_internal for display optimization. */
13399 static bool
13400 text_outside_line_unchanged_p (struct window *w,
13401 ptrdiff_t start, ptrdiff_t end)
13403 bool unchanged_p = true;
13405 /* If text or overlays have changed, see where. */
13406 if (window_outdated (w))
13408 /* Gap in the line? */
13409 if (GPT < start || Z - GPT < end)
13410 unchanged_p = false;
13412 /* Changes start in front of the line, or end after it? */
13413 if (unchanged_p
13414 && (BEG_UNCHANGED < start - 1
13415 || END_UNCHANGED < end))
13416 unchanged_p = false;
13418 /* If selective display, can't optimize if changes start at the
13419 beginning of the line. */
13420 if (unchanged_p
13421 && INTEGERP (BVAR (current_buffer, selective_display))
13422 && XINT (BVAR (current_buffer, selective_display)) > 0
13423 && (BEG_UNCHANGED < start || GPT <= start))
13424 unchanged_p = false;
13426 /* If there are overlays at the start or end of the line, these
13427 may have overlay strings with newlines in them. A change at
13428 START, for instance, may actually concern the display of such
13429 overlay strings as well, and they are displayed on different
13430 lines. So, quickly rule out this case. (For the future, it
13431 might be desirable to implement something more telling than
13432 just BEG/END_UNCHANGED.) */
13433 if (unchanged_p)
13435 if (BEG + BEG_UNCHANGED == start
13436 && overlay_touches_p (start))
13437 unchanged_p = false;
13438 if (END_UNCHANGED == end
13439 && overlay_touches_p (Z - end))
13440 unchanged_p = false;
13443 /* Under bidi reordering, adding or deleting a character in the
13444 beginning of a paragraph, before the first strong directional
13445 character, can change the base direction of the paragraph (unless
13446 the buffer specifies a fixed paragraph direction), which will
13447 require redisplaying the whole paragraph. It might be worthwhile
13448 to find the paragraph limits and widen the range of redisplayed
13449 lines to that, but for now just give up this optimization. */
13450 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13451 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13452 unchanged_p = false;
13455 return unchanged_p;
13459 /* Do a frame update, taking possible shortcuts into account. This is
13460 the main external entry point for redisplay.
13462 If the last redisplay displayed an echo area message and that message
13463 is no longer requested, we clear the echo area or bring back the
13464 mini-buffer if that is in use. */
13466 void
13467 redisplay (void)
13469 redisplay_internal ();
13473 static Lisp_Object
13474 overlay_arrow_string_or_property (Lisp_Object var)
13476 Lisp_Object val;
13478 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13479 return val;
13481 return Voverlay_arrow_string;
13484 /* Return true if there are any overlay-arrows in current_buffer. */
13485 static bool
13486 overlay_arrow_in_current_buffer_p (void)
13488 Lisp_Object vlist;
13490 for (vlist = Voverlay_arrow_variable_list;
13491 CONSP (vlist);
13492 vlist = XCDR (vlist))
13494 Lisp_Object var = XCAR (vlist);
13495 Lisp_Object val;
13497 if (!SYMBOLP (var))
13498 continue;
13499 val = find_symbol_value (var);
13500 if (MARKERP (val)
13501 && current_buffer == XMARKER (val)->buffer)
13502 return true;
13504 return false;
13508 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13509 has changed.
13510 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13511 buffers that are affected. */
13513 static bool
13514 overlay_arrows_changed_p (bool set_redisplay)
13516 Lisp_Object vlist;
13517 bool changed = false;
13519 for (vlist = Voverlay_arrow_variable_list;
13520 CONSP (vlist);
13521 vlist = XCDR (vlist))
13523 Lisp_Object var = XCAR (vlist);
13524 Lisp_Object val, pstr;
13526 if (!SYMBOLP (var))
13527 continue;
13528 val = find_symbol_value (var);
13529 if (!MARKERP (val))
13530 continue;
13531 if (! EQ (COERCE_MARKER (val),
13532 /* FIXME: Don't we have a problem, using such a global
13533 * "last-position" if the variable is buffer-local? */
13534 Fget (var, Qlast_arrow_position))
13535 || ! (pstr = overlay_arrow_string_or_property (var),
13536 EQ (pstr, Fget (var, Qlast_arrow_string))))
13538 struct buffer *buf = XMARKER (val)->buffer;
13540 if (set_redisplay)
13542 if (buf)
13543 bset_redisplay (buf);
13544 changed = true;
13546 else
13547 return true;
13550 return changed;
13553 /* Mark overlay arrows to be updated on next redisplay. */
13555 static void
13556 update_overlay_arrows (int up_to_date)
13558 Lisp_Object vlist;
13560 for (vlist = Voverlay_arrow_variable_list;
13561 CONSP (vlist);
13562 vlist = XCDR (vlist))
13564 Lisp_Object var = XCAR (vlist);
13566 if (!SYMBOLP (var))
13567 continue;
13569 if (up_to_date > 0)
13571 Lisp_Object val = find_symbol_value (var);
13572 if (!MARKERP (val))
13573 continue;
13574 Fput (var, Qlast_arrow_position,
13575 COERCE_MARKER (val));
13576 Fput (var, Qlast_arrow_string,
13577 overlay_arrow_string_or_property (var));
13579 else if (up_to_date < 0
13580 || !NILP (Fget (var, Qlast_arrow_position)))
13582 Fput (var, Qlast_arrow_position, Qt);
13583 Fput (var, Qlast_arrow_string, Qt);
13589 /* Return overlay arrow string to display at row.
13590 Return integer (bitmap number) for arrow bitmap in left fringe.
13591 Return nil if no overlay arrow. */
13593 static Lisp_Object
13594 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13596 Lisp_Object vlist;
13598 for (vlist = Voverlay_arrow_variable_list;
13599 CONSP (vlist);
13600 vlist = XCDR (vlist))
13602 Lisp_Object var = XCAR (vlist);
13603 Lisp_Object val;
13605 if (!SYMBOLP (var))
13606 continue;
13608 val = find_symbol_value (var);
13610 if (MARKERP (val)
13611 && current_buffer == XMARKER (val)->buffer
13612 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13614 if (FRAME_WINDOW_P (it->f)
13615 /* FIXME: if ROW->reversed_p is set, this should test
13616 the right fringe, not the left one. */
13617 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13619 #ifdef HAVE_WINDOW_SYSTEM
13620 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13622 int fringe_bitmap = lookup_fringe_bitmap (val);
13623 if (fringe_bitmap != 0)
13624 return make_number (fringe_bitmap);
13626 #endif
13627 return make_number (-1); /* Use default arrow bitmap. */
13629 return overlay_arrow_string_or_property (var);
13633 return Qnil;
13636 /* Return true if point moved out of or into a composition. Otherwise
13637 return false. PREV_BUF and PREV_PT are the last point buffer and
13638 position. BUF and PT are the current point buffer and position. */
13640 static bool
13641 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13642 struct buffer *buf, ptrdiff_t pt)
13644 ptrdiff_t start, end;
13645 Lisp_Object prop;
13646 Lisp_Object buffer;
13648 XSETBUFFER (buffer, buf);
13649 /* Check a composition at the last point if point moved within the
13650 same buffer. */
13651 if (prev_buf == buf)
13653 if (prev_pt == pt)
13654 /* Point didn't move. */
13655 return false;
13657 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13658 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13659 && composition_valid_p (start, end, prop)
13660 && start < prev_pt && end > prev_pt)
13661 /* The last point was within the composition. Return true iff
13662 point moved out of the composition. */
13663 return (pt <= start || pt >= end);
13666 /* Check a composition at the current point. */
13667 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13668 && find_composition (pt, -1, &start, &end, &prop, buffer)
13669 && composition_valid_p (start, end, prop)
13670 && start < pt && end > pt);
13673 /* Reconsider the clip changes of buffer which is displayed in W. */
13675 static void
13676 reconsider_clip_changes (struct window *w)
13678 struct buffer *b = XBUFFER (w->contents);
13680 if (b->clip_changed
13681 && w->window_end_valid
13682 && w->current_matrix->buffer == b
13683 && w->current_matrix->zv == BUF_ZV (b)
13684 && w->current_matrix->begv == BUF_BEGV (b))
13685 b->clip_changed = false;
13687 /* If display wasn't paused, and W is not a tool bar window, see if
13688 point has been moved into or out of a composition. In that case,
13689 set b->clip_changed to force updating the screen. If
13690 b->clip_changed has already been set, skip this check. */
13691 if (!b->clip_changed && w->window_end_valid)
13693 ptrdiff_t pt = (w == XWINDOW (selected_window)
13694 ? PT : marker_position (w->pointm));
13696 if ((w->current_matrix->buffer != b || pt != w->last_point)
13697 && check_point_in_composition (w->current_matrix->buffer,
13698 w->last_point, b, pt))
13699 b->clip_changed = true;
13703 static void
13704 propagate_buffer_redisplay (void)
13705 { /* Resetting b->text->redisplay is problematic!
13706 We can't just reset it in the case that some window that displays
13707 it has not been redisplayed; and such a window can stay
13708 unredisplayed for a long time if it's currently invisible.
13709 But we do want to reset it at the end of redisplay otherwise
13710 its displayed windows will keep being redisplayed over and over
13711 again.
13712 So we copy all b->text->redisplay flags up to their windows here,
13713 such that mark_window_display_accurate can safely reset
13714 b->text->redisplay. */
13715 Lisp_Object ws = window_list ();
13716 for (; CONSP (ws); ws = XCDR (ws))
13718 struct window *thisw = XWINDOW (XCAR (ws));
13719 struct buffer *thisb = XBUFFER (thisw->contents);
13720 if (thisb->text->redisplay)
13721 thisw->redisplay = true;
13725 #define STOP_POLLING \
13726 do { if (! polling_stopped_here) stop_polling (); \
13727 polling_stopped_here = true; } while (false)
13729 #define RESUME_POLLING \
13730 do { if (polling_stopped_here) start_polling (); \
13731 polling_stopped_here = false; } while (false)
13734 /* Perhaps in the future avoid recentering windows if it
13735 is not necessary; currently that causes some problems. */
13737 static void
13738 redisplay_internal (void)
13740 struct window *w = XWINDOW (selected_window);
13741 struct window *sw;
13742 struct frame *fr;
13743 bool pending;
13744 bool must_finish = false, match_p;
13745 struct text_pos tlbufpos, tlendpos;
13746 int number_of_visible_frames;
13747 ptrdiff_t count;
13748 struct frame *sf;
13749 bool polling_stopped_here = false;
13750 Lisp_Object tail, frame;
13752 /* Set a limit to the number of retries we perform due to horizontal
13753 scrolling, this avoids getting stuck in an uninterruptible
13754 infinite loop (Bug #24633). */
13755 enum { MAX_HSCROLL_RETRIES = 16 };
13756 int hscroll_retries = 0;
13758 /* Limit the number of retries for when frame(s) become garbaged as
13759 result of redisplaying them. Some packages set various redisplay
13760 hooks, such as window-scroll-functions, to run Lisp that always
13761 calls APIs which cause the frame's garbaged flag to become set,
13762 so we loop indefinitely. */
13763 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13764 int garbaged_frame_retries = 0;
13766 /* True means redisplay has to consider all windows on all
13767 frames. False, only selected_window is considered. */
13768 bool consider_all_windows_p;
13770 /* True means redisplay has to redisplay the miniwindow. */
13771 bool update_miniwindow_p = false;
13773 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13775 /* No redisplay if running in batch mode or frame is not yet fully
13776 initialized, or redisplay is explicitly turned off by setting
13777 Vinhibit_redisplay. */
13778 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13779 || !NILP (Vinhibit_redisplay))
13780 return;
13782 /* Don't examine these until after testing Vinhibit_redisplay.
13783 When Emacs is shutting down, perhaps because its connection to
13784 X has dropped, we should not look at them at all. */
13785 fr = XFRAME (w->frame);
13786 sf = SELECTED_FRAME ();
13788 if (!fr->glyphs_initialized_p)
13789 return;
13791 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13792 if (popup_activated ())
13793 return;
13794 #endif
13796 /* I don't think this happens but let's be paranoid. */
13797 if (redisplaying_p)
13798 return;
13800 /* Record a function that clears redisplaying_p
13801 when we leave this function. */
13802 count = SPECPDL_INDEX ();
13803 record_unwind_protect_void (unwind_redisplay);
13804 redisplaying_p = true;
13805 block_buffer_flips ();
13806 specbind (Qinhibit_free_realized_faces, Qnil);
13808 /* Record this function, so it appears on the profiler's backtraces. */
13809 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13811 FOR_EACH_FRAME (tail, frame)
13812 XFRAME (frame)->already_hscrolled_p = false;
13814 retry:
13815 /* Remember the currently selected window. */
13816 sw = w;
13818 pending = false;
13819 forget_escape_and_glyphless_faces ();
13821 inhibit_free_realized_faces = false;
13823 /* If face_change, init_iterator will free all realized faces, which
13824 includes the faces referenced from current matrices. So, we
13825 can't reuse current matrices in this case. */
13826 if (face_change)
13827 windows_or_buffers_changed = 47;
13829 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13830 && FRAME_TTY (sf)->previous_frame != sf)
13832 /* Since frames on a single ASCII terminal share the same
13833 display area, displaying a different frame means redisplay
13834 the whole thing. */
13835 SET_FRAME_GARBAGED (sf);
13836 #ifndef DOS_NT
13837 set_tty_color_mode (FRAME_TTY (sf), sf);
13838 #endif
13839 FRAME_TTY (sf)->previous_frame = sf;
13842 /* Set the visible flags for all frames. Do this before checking for
13843 resized or garbaged frames; they want to know if their frames are
13844 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13845 number_of_visible_frames = 0;
13847 FOR_EACH_FRAME (tail, frame)
13849 struct frame *f = XFRAME (frame);
13851 if (FRAME_VISIBLE_P (f))
13853 ++number_of_visible_frames;
13854 /* Adjust matrices for visible frames only. */
13855 if (f->fonts_changed)
13857 adjust_frame_glyphs (f);
13858 /* Disable all redisplay optimizations for this frame.
13859 This is because adjust_frame_glyphs resets the
13860 enabled_p flag for all glyph rows of all windows, so
13861 many optimizations will fail anyway, and some might
13862 fail to test that flag and do bogus things as
13863 result. */
13864 SET_FRAME_GARBAGED (f);
13865 f->fonts_changed = false;
13867 /* If cursor type has been changed on the frame
13868 other than selected, consider all frames. */
13869 if (f != sf && f->cursor_type_changed)
13870 fset_redisplay (f);
13872 clear_desired_matrices (f);
13875 /* Notice any pending interrupt request to change frame size. */
13876 do_pending_window_change (true);
13878 /* do_pending_window_change could change the selected_window due to
13879 frame resizing which makes the selected window too small. */
13880 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13881 sw = w;
13883 /* Clear frames marked as garbaged. */
13884 clear_garbaged_frames ();
13886 /* Build menubar and tool-bar items. */
13887 if (NILP (Vmemory_full))
13888 prepare_menu_bars ();
13890 reconsider_clip_changes (w);
13892 /* In most cases selected window displays current buffer. */
13893 match_p = XBUFFER (w->contents) == current_buffer;
13894 if (match_p)
13896 /* Detect case that we need to write or remove a star in the mode line. */
13897 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13898 w->update_mode_line = true;
13900 if (mode_line_update_needed (w))
13901 w->update_mode_line = true;
13903 /* If reconsider_clip_changes above decided that the narrowing
13904 in the current buffer changed, make sure all other windows
13905 showing that buffer will be redisplayed. */
13906 if (current_buffer->clip_changed)
13907 bset_update_mode_line (current_buffer);
13910 /* Normally the message* functions will have already displayed and
13911 updated the echo area, but the frame may have been trashed, or
13912 the update may have been preempted, so display the echo area
13913 again here. Checking message_cleared_p captures the case that
13914 the echo area should be cleared. */
13915 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13916 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13917 || (message_cleared_p
13918 && minibuf_level == 0
13919 /* If the mini-window is currently selected, this means the
13920 echo-area doesn't show through. */
13921 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13923 echo_area_display (false);
13925 /* If echo_area_display resizes the mini-window, the redisplay and
13926 window_sizes_changed flags of the selected frame are set, but
13927 it's too late for the hooks in window-size-change-functions,
13928 which have been examined already in prepare_menu_bars. So in
13929 that case we call the hooks here only for the selected frame. */
13930 if (sf->redisplay)
13932 ptrdiff_t count1 = SPECPDL_INDEX ();
13934 record_unwind_save_match_data ();
13935 run_window_size_change_functions (selected_frame);
13936 unbind_to (count1, Qnil);
13939 if (message_cleared_p)
13940 update_miniwindow_p = true;
13942 must_finish = true;
13944 /* If we don't display the current message, don't clear the
13945 message_cleared_p flag, because, if we did, we wouldn't clear
13946 the echo area in the next redisplay which doesn't preserve
13947 the echo area. */
13948 if (!display_last_displayed_message_p)
13949 message_cleared_p = false;
13951 else if (EQ (selected_window, minibuf_window)
13952 && (current_buffer->clip_changed || window_outdated (w))
13953 && resize_mini_window (w, false))
13955 if (sf->redisplay)
13957 ptrdiff_t count1 = SPECPDL_INDEX ();
13959 record_unwind_save_match_data ();
13960 run_window_size_change_functions (selected_frame);
13961 unbind_to (count1, Qnil);
13964 /* Resized active mini-window to fit the size of what it is
13965 showing if its contents might have changed. */
13966 must_finish = true;
13968 /* If window configuration was changed, frames may have been
13969 marked garbaged. Clear them or we will experience
13970 surprises wrt scrolling. */
13971 clear_garbaged_frames ();
13974 if (windows_or_buffers_changed && !update_mode_lines)
13975 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13976 only the windows's contents needs to be refreshed, or whether the
13977 mode-lines also need a refresh. */
13978 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13979 ? REDISPLAY_SOME : 32);
13981 /* If specs for an arrow have changed, do thorough redisplay
13982 to ensure we remove any arrow that should no longer exist. */
13983 /* Apparently, this is the only case where we update other windows,
13984 without updating other mode-lines. */
13985 overlay_arrows_changed_p (true);
13987 consider_all_windows_p = (update_mode_lines
13988 || windows_or_buffers_changed);
13990 #define AINC(a,i) \
13992 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13993 if (INTEGERP (entry)) \
13994 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13997 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13998 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14000 /* Optimize the case that only the line containing the cursor in the
14001 selected window has changed. Variables starting with this_ are
14002 set in display_line and record information about the line
14003 containing the cursor. */
14004 tlbufpos = this_line_start_pos;
14005 tlendpos = this_line_end_pos;
14006 if (!consider_all_windows_p
14007 && CHARPOS (tlbufpos) > 0
14008 && !w->update_mode_line
14009 && !current_buffer->clip_changed
14010 && !current_buffer->prevent_redisplay_optimizations_p
14011 && FRAME_VISIBLE_P (XFRAME (w->frame))
14012 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14013 && !XFRAME (w->frame)->cursor_type_changed
14014 && !XFRAME (w->frame)->face_change
14015 /* Make sure recorded data applies to current buffer, etc. */
14016 && this_line_buffer == current_buffer
14017 && match_p
14018 && !w->force_start
14019 && !w->optional_new_start
14020 /* Point must be on the line that we have info recorded about. */
14021 && PT >= CHARPOS (tlbufpos)
14022 && PT <= Z - CHARPOS (tlendpos)
14023 /* All text outside that line, including its final newline,
14024 must be unchanged. */
14025 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14026 CHARPOS (tlendpos)))
14028 if (CHARPOS (tlbufpos) > BEGV
14029 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14030 && (CHARPOS (tlbufpos) == ZV
14031 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14032 /* Former continuation line has disappeared by becoming empty. */
14033 goto cancel;
14034 else if (window_outdated (w) || MINI_WINDOW_P (w))
14036 /* We have to handle the case of continuation around a
14037 wide-column character (see the comment in indent.c around
14038 line 1340).
14040 For instance, in the following case:
14042 -------- Insert --------
14043 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14044 J_I_ ==> J_I_ `^^' are cursors.
14045 ^^ ^^
14046 -------- --------
14048 As we have to redraw the line above, we cannot use this
14049 optimization. */
14051 struct it it;
14052 int line_height_before = this_line_pixel_height;
14054 /* Note that start_display will handle the case that the
14055 line starting at tlbufpos is a continuation line. */
14056 start_display (&it, w, tlbufpos);
14058 /* Implementation note: It this still necessary? */
14059 if (it.current_x != this_line_start_x)
14060 goto cancel;
14062 TRACE ((stderr, "trying display optimization 1\n"));
14063 w->cursor.vpos = -1;
14064 overlay_arrow_seen = false;
14065 it.vpos = this_line_vpos;
14066 it.current_y = this_line_y;
14067 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14068 display_line (&it, -1);
14070 /* If line contains point, is not continued,
14071 and ends at same distance from eob as before, we win. */
14072 if (w->cursor.vpos >= 0
14073 /* Line is not continued, otherwise this_line_start_pos
14074 would have been set to 0 in display_line. */
14075 && CHARPOS (this_line_start_pos)
14076 /* Line ends as before. */
14077 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14078 /* Line has same height as before. Otherwise other lines
14079 would have to be shifted up or down. */
14080 && this_line_pixel_height == line_height_before)
14082 /* If this is not the window's last line, we must adjust
14083 the charstarts of the lines below. */
14084 if (it.current_y < it.last_visible_y)
14086 struct glyph_row *row
14087 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14088 ptrdiff_t delta, delta_bytes;
14090 /* We used to distinguish between two cases here,
14091 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14092 when the line ends in a newline or the end of the
14093 buffer's accessible portion. But both cases did
14094 the same, so they were collapsed. */
14095 delta = (Z
14096 - CHARPOS (tlendpos)
14097 - MATRIX_ROW_START_CHARPOS (row));
14098 delta_bytes = (Z_BYTE
14099 - BYTEPOS (tlendpos)
14100 - MATRIX_ROW_START_BYTEPOS (row));
14102 increment_matrix_positions (w->current_matrix,
14103 this_line_vpos + 1,
14104 w->current_matrix->nrows,
14105 delta, delta_bytes);
14108 /* If this row displays text now but previously didn't,
14109 or vice versa, w->window_end_vpos may have to be
14110 adjusted. */
14111 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14113 if (w->window_end_vpos < this_line_vpos)
14114 w->window_end_vpos = this_line_vpos;
14116 else if (w->window_end_vpos == this_line_vpos
14117 && this_line_vpos > 0)
14118 w->window_end_vpos = this_line_vpos - 1;
14119 w->window_end_valid = false;
14121 /* Update hint: No need to try to scroll in update_window. */
14122 w->desired_matrix->no_scrolling_p = true;
14124 #ifdef GLYPH_DEBUG
14125 *w->desired_matrix->method = 0;
14126 debug_method_add (w, "optimization 1");
14127 #endif
14128 #ifdef HAVE_WINDOW_SYSTEM
14129 update_window_fringes (w, false);
14130 #endif
14131 goto update;
14133 else
14134 goto cancel;
14136 else if (/* Cursor position hasn't changed. */
14137 PT == w->last_point
14138 /* Make sure the cursor was last displayed
14139 in this window. Otherwise we have to reposition it. */
14141 /* PXW: Must be converted to pixels, probably. */
14142 && 0 <= w->cursor.vpos
14143 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14145 if (!must_finish)
14147 do_pending_window_change (true);
14148 /* If selected_window changed, redisplay again. */
14149 if (WINDOWP (selected_window)
14150 && (w = XWINDOW (selected_window)) != sw)
14151 goto retry;
14153 /* We used to always goto end_of_redisplay here, but this
14154 isn't enough if we have a blinking cursor. */
14155 if (w->cursor_off_p == w->last_cursor_off_p)
14156 goto end_of_redisplay;
14158 goto update;
14160 /* If highlighting the region, or if the cursor is in the echo area,
14161 then we can't just move the cursor. */
14162 else if (NILP (Vshow_trailing_whitespace)
14163 && !cursor_in_echo_area)
14165 struct it it;
14166 struct glyph_row *row;
14168 /* Skip from tlbufpos to PT and see where it is. Note that
14169 PT may be in invisible text. If so, we will end at the
14170 next visible position. */
14171 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14172 NULL, DEFAULT_FACE_ID);
14173 it.current_x = this_line_start_x;
14174 it.current_y = this_line_y;
14175 it.vpos = this_line_vpos;
14177 /* The call to move_it_to stops in front of PT, but
14178 moves over before-strings. */
14179 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14181 if (it.vpos == this_line_vpos
14182 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14183 row->enabled_p))
14185 eassert (this_line_vpos == it.vpos);
14186 eassert (this_line_y == it.current_y);
14187 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14188 if (cursor_row_fully_visible_p (w, false, true))
14190 #ifdef GLYPH_DEBUG
14191 *w->desired_matrix->method = 0;
14192 debug_method_add (w, "optimization 3");
14193 #endif
14194 goto update;
14196 else
14197 goto cancel;
14199 else
14200 goto cancel;
14203 cancel:
14204 /* Text changed drastically or point moved off of line. */
14205 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14208 CHARPOS (this_line_start_pos) = 0;
14209 ++clear_face_cache_count;
14210 #ifdef HAVE_WINDOW_SYSTEM
14211 ++clear_image_cache_count;
14212 #endif
14214 /* Build desired matrices, and update the display. If
14215 consider_all_windows_p, do it for all windows on all frames that
14216 require redisplay, as specified by their 'redisplay' flag.
14217 Otherwise do it for selected_window, only. */
14219 if (consider_all_windows_p)
14221 FOR_EACH_FRAME (tail, frame)
14222 XFRAME (frame)->updated_p = false;
14224 propagate_buffer_redisplay ();
14226 FOR_EACH_FRAME (tail, frame)
14228 struct frame *f = XFRAME (frame);
14230 /* We don't have to do anything for unselected terminal
14231 frames. */
14232 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14233 && !EQ (FRAME_TTY (f)->top_frame, frame))
14234 continue;
14236 retry_frame:
14237 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14239 bool gcscrollbars
14240 /* Only GC scrollbars when we redisplay the whole frame. */
14241 = f->redisplay || !REDISPLAY_SOME_P ();
14242 bool f_redisplay_flag = f->redisplay;
14243 /* Mark all the scroll bars to be removed; we'll redeem
14244 the ones we want when we redisplay their windows. */
14245 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14246 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14248 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14249 redisplay_windows (FRAME_ROOT_WINDOW (f));
14250 /* Remember that the invisible frames need to be redisplayed next
14251 time they're visible. */
14252 else if (!REDISPLAY_SOME_P ())
14253 f->redisplay = true;
14255 /* The X error handler may have deleted that frame. */
14256 if (!FRAME_LIVE_P (f))
14257 continue;
14259 /* Any scroll bars which redisplay_windows should have
14260 nuked should now go away. */
14261 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14262 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14264 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14266 /* If fonts changed on visible frame, display again. */
14267 if (f->fonts_changed)
14269 adjust_frame_glyphs (f);
14270 /* Disable all redisplay optimizations for this
14271 frame. For the reasons, see the comment near
14272 the previous call to adjust_frame_glyphs above. */
14273 SET_FRAME_GARBAGED (f);
14274 f->fonts_changed = false;
14275 goto retry_frame;
14278 /* See if we have to hscroll. */
14279 if (!f->already_hscrolled_p)
14281 f->already_hscrolled_p = true;
14282 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14283 && hscroll_windows (f->root_window))
14285 hscroll_retries++;
14286 goto retry_frame;
14290 /* If the frame's redisplay flag was not set before
14291 we went about redisplaying its windows, but it is
14292 set now, that means we employed some redisplay
14293 optimizations inside redisplay_windows, and
14294 bypassed producing some screen lines. But if
14295 f->redisplay is now set, it might mean the old
14296 faces are no longer valid (e.g., if redisplaying
14297 some window called some Lisp which defined a new
14298 face or redefined an existing face), so trying to
14299 use them in update_frame will segfault.
14300 Therefore, we must redisplay this frame. */
14301 if (!f_redisplay_flag && f->redisplay)
14302 goto retry_frame;
14303 /* In some case (e.g., window resize), we notice
14304 only during window updating that the window
14305 content changed unpredictably (e.g., a GTK
14306 scrollbar moved, or some Lisp hook that winds up
14307 calling adjust_frame_glyphs) and that our
14308 previous estimation of the frame content was
14309 garbage. We have to start over. These cases
14310 should be rare, so going all the way back to the
14311 top of redisplay should be good enough. */
14312 if (FRAME_GARBAGED_P (f)
14313 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14314 goto retry;
14316 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14317 x_clear_under_internal_border (f);
14318 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14320 /* Prevent various kinds of signals during display
14321 update. stdio is not robust about handling
14322 signals, which can cause an apparent I/O error. */
14323 if (interrupt_input)
14324 unrequest_sigio ();
14325 STOP_POLLING;
14327 pending |= update_frame (f, false, false);
14328 f->cursor_type_changed = false;
14329 f->updated_p = true;
14334 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14336 if (!pending)
14338 /* Do the mark_window_display_accurate after all windows have
14339 been redisplayed because this call resets flags in buffers
14340 which are needed for proper redisplay. */
14341 FOR_EACH_FRAME (tail, frame)
14343 struct frame *f = XFRAME (frame);
14344 if (f->updated_p)
14346 f->redisplay = false;
14347 f->garbaged = false;
14348 mark_window_display_accurate (f->root_window, true);
14349 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14350 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14355 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14357 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14358 /* Use list_of_error, not Qerror, so that
14359 we catch only errors and don't run the debugger. */
14360 internal_condition_case_1 (redisplay_window_1, selected_window,
14361 list_of_error,
14362 redisplay_window_error);
14363 if (update_miniwindow_p)
14364 internal_condition_case_1 (redisplay_window_1,
14365 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14366 redisplay_window_error);
14368 /* Compare desired and current matrices, perform output. */
14370 update:
14371 /* If fonts changed, display again. Likewise if redisplay_window_1
14372 above caused some change (e.g., a change in faces) that requires
14373 considering the entire frame again. */
14374 if (sf->fonts_changed || sf->redisplay)
14376 if (sf->redisplay)
14378 /* Set this to force a more thorough redisplay.
14379 Otherwise, we might immediately loop back to the
14380 above "else-if" clause (since all the conditions that
14381 led here might still be true), and we will then
14382 infloop, because the selected-frame's redisplay flag
14383 is not (and cannot be) reset. */
14384 windows_or_buffers_changed = 50;
14386 goto retry;
14389 /* Prevent freeing of realized faces, since desired matrices are
14390 pending that reference the faces we computed and cached. */
14391 inhibit_free_realized_faces = true;
14393 /* Prevent various kinds of signals during display update.
14394 stdio is not robust about handling signals,
14395 which can cause an apparent I/O error. */
14396 if (interrupt_input)
14397 unrequest_sigio ();
14398 STOP_POLLING;
14400 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14402 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14403 && hscroll_windows (selected_window))
14405 hscroll_retries++;
14406 goto retry;
14409 XWINDOW (selected_window)->must_be_updated_p = true;
14410 pending = update_frame (sf, false, false);
14411 sf->cursor_type_changed = false;
14414 /* We may have called echo_area_display at the top of this
14415 function. If the echo area is on another frame, that may
14416 have put text on a frame other than the selected one, so the
14417 above call to update_frame would not have caught it. Catch
14418 it here. */
14419 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14420 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14422 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14424 XWINDOW (mini_window)->must_be_updated_p = true;
14425 pending |= update_frame (mini_frame, false, false);
14426 mini_frame->cursor_type_changed = false;
14427 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14428 && hscroll_windows (mini_window))
14430 hscroll_retries++;
14431 goto retry;
14436 /* If display was paused because of pending input, make sure we do a
14437 thorough update the next time. */
14438 if (pending)
14440 /* Prevent the optimization at the beginning of
14441 redisplay_internal that tries a single-line update of the
14442 line containing the cursor in the selected window. */
14443 CHARPOS (this_line_start_pos) = 0;
14445 /* Let the overlay arrow be updated the next time. */
14446 update_overlay_arrows (0);
14448 /* If we pause after scrolling, some rows in the current
14449 matrices of some windows are not valid. */
14450 if (!WINDOW_FULL_WIDTH_P (w)
14451 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14452 update_mode_lines = 36;
14454 else
14456 if (!consider_all_windows_p)
14458 /* This has already been done above if
14459 consider_all_windows_p is set. */
14460 if (XBUFFER (w->contents)->text->redisplay
14461 && buffer_window_count (XBUFFER (w->contents)) > 1)
14462 /* This can happen if b->text->redisplay was set during
14463 jit-lock. */
14464 propagate_buffer_redisplay ();
14465 mark_window_display_accurate_1 (w, true);
14467 /* Say overlay arrows are up to date. */
14468 update_overlay_arrows (1);
14470 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14471 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14474 update_mode_lines = 0;
14475 windows_or_buffers_changed = 0;
14478 /* Start SIGIO interrupts coming again. Having them off during the
14479 code above makes it less likely one will discard output, but not
14480 impossible, since there might be stuff in the system buffer here.
14481 But it is much hairier to try to do anything about that. */
14482 if (interrupt_input)
14483 request_sigio ();
14484 RESUME_POLLING;
14486 /* If a frame has become visible which was not before, redisplay
14487 again, so that we display it. Expose events for such a frame
14488 (which it gets when becoming visible) don't call the parts of
14489 redisplay constructing glyphs, so simply exposing a frame won't
14490 display anything in this case. So, we have to display these
14491 frames here explicitly. */
14492 if (!pending)
14494 int new_count = 0;
14496 FOR_EACH_FRAME (tail, frame)
14498 if (XFRAME (frame)->visible)
14499 new_count++;
14502 if (new_count != number_of_visible_frames)
14503 windows_or_buffers_changed = 52;
14506 /* Change frame size now if a change is pending. */
14507 do_pending_window_change (true);
14509 /* If we just did a pending size change, or have additional
14510 visible frames, or selected_window changed, redisplay again. */
14511 if ((windows_or_buffers_changed && !pending)
14512 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14513 goto retry;
14515 /* Clear the face and image caches.
14517 We used to do this only if consider_all_windows_p. But the cache
14518 needs to be cleared if a timer creates images in the current
14519 buffer (e.g. the test case in Bug#6230). */
14521 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14523 clear_face_cache (false);
14524 clear_face_cache_count = 0;
14527 #ifdef HAVE_WINDOW_SYSTEM
14528 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14530 clear_image_caches (Qnil);
14531 clear_image_cache_count = 0;
14533 #endif /* HAVE_WINDOW_SYSTEM */
14535 end_of_redisplay:
14536 #ifdef HAVE_NS
14537 ns_set_doc_edited ();
14538 #endif
14539 if (interrupt_input && interrupts_deferred)
14540 request_sigio ();
14542 unbind_to (count, Qnil);
14543 RESUME_POLLING;
14546 static void
14547 unwind_redisplay_preserve_echo_area (void)
14549 unblock_buffer_flips ();
14552 /* Redisplay, but leave alone any recent echo area message unless
14553 another message has been requested in its place.
14555 This is useful in situations where you need to redisplay but no
14556 user action has occurred, making it inappropriate for the message
14557 area to be cleared. See tracking_off and
14558 wait_reading_process_output for examples of these situations.
14560 FROM_WHERE is an integer saying from where this function was
14561 called. This is useful for debugging. */
14563 void
14564 redisplay_preserve_echo_area (int from_where)
14566 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14568 block_input ();
14569 ptrdiff_t count = SPECPDL_INDEX ();
14570 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14571 block_buffer_flips ();
14572 unblock_input ();
14574 if (!NILP (echo_area_buffer[1]))
14576 /* We have a previously displayed message, but no current
14577 message. Redisplay the previous message. */
14578 display_last_displayed_message_p = true;
14579 redisplay_internal ();
14580 display_last_displayed_message_p = false;
14582 else
14583 redisplay_internal ();
14585 flush_frame (SELECTED_FRAME ());
14586 unbind_to (count, Qnil);
14590 /* Function registered with record_unwind_protect in redisplay_internal. */
14592 static void
14593 unwind_redisplay (void)
14595 redisplaying_p = false;
14596 unblock_buffer_flips ();
14600 /* Mark the display of leaf window W as accurate or inaccurate.
14601 If ACCURATE_P, mark display of W as accurate.
14602 If !ACCURATE_P, arrange for W to be redisplayed the next
14603 time redisplay_internal is called. */
14605 static void
14606 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14608 struct buffer *b = XBUFFER (w->contents);
14610 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14611 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14612 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14614 if (accurate_p)
14616 b->clip_changed = false;
14617 b->prevent_redisplay_optimizations_p = false;
14618 eassert (buffer_window_count (b) > 0);
14619 /* Resetting b->text->redisplay is problematic!
14620 In order to make it safer to do it here, redisplay_internal must
14621 have copied all b->text->redisplay to their respective windows. */
14622 b->text->redisplay = false;
14624 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14625 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14626 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14627 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14629 w->current_matrix->buffer = b;
14630 w->current_matrix->begv = BUF_BEGV (b);
14631 w->current_matrix->zv = BUF_ZV (b);
14633 w->last_cursor_vpos = w->cursor.vpos;
14634 w->last_cursor_off_p = w->cursor_off_p;
14636 if (w == XWINDOW (selected_window))
14637 w->last_point = BUF_PT (b);
14638 else
14639 w->last_point = marker_position (w->pointm);
14641 w->window_end_valid = true;
14642 w->update_mode_line = false;
14645 w->redisplay = !accurate_p;
14649 /* Mark the display of windows in the window tree rooted at WINDOW as
14650 accurate or inaccurate. If ACCURATE_P, mark display of
14651 windows as accurate. If !ACCURATE_P, arrange for windows to
14652 be redisplayed the next time redisplay_internal is called. */
14654 void
14655 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14657 struct window *w;
14659 for (; !NILP (window); window = w->next)
14661 w = XWINDOW (window);
14662 if (WINDOWP (w->contents))
14663 mark_window_display_accurate (w->contents, accurate_p);
14664 else
14665 mark_window_display_accurate_1 (w, accurate_p);
14668 if (accurate_p)
14669 update_overlay_arrows (1);
14670 else
14671 /* Force a thorough redisplay the next time by setting
14672 last_arrow_position and last_arrow_string to t, which is
14673 unequal to any useful value of Voverlay_arrow_... */
14674 update_overlay_arrows (-1);
14678 /* Return value in display table DP (Lisp_Char_Table *) for character
14679 C. Since a display table doesn't have any parent, we don't have to
14680 follow parent. Do not call this function directly but use the
14681 macro DISP_CHAR_VECTOR. */
14683 Lisp_Object
14684 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14686 Lisp_Object val;
14688 if (ASCII_CHAR_P (c))
14690 val = dp->ascii;
14691 if (SUB_CHAR_TABLE_P (val))
14692 val = XSUB_CHAR_TABLE (val)->contents[c];
14694 else
14696 Lisp_Object table;
14698 XSETCHAR_TABLE (table, dp);
14699 val = char_table_ref (table, c);
14701 if (NILP (val))
14702 val = dp->defalt;
14703 return val;
14706 static int buffer_flip_blocked_depth;
14708 static void
14709 block_buffer_flips (void)
14711 eassert (buffer_flip_blocked_depth >= 0);
14712 buffer_flip_blocked_depth++;
14715 static void
14716 unblock_buffer_flips (void)
14718 eassert (buffer_flip_blocked_depth > 0);
14719 if (--buffer_flip_blocked_depth == 0)
14721 Lisp_Object tail, frame;
14722 block_input ();
14723 FOR_EACH_FRAME (tail, frame)
14725 struct frame *f = XFRAME (frame);
14726 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14727 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14729 unblock_input ();
14733 bool
14734 buffer_flipping_blocked_p (void)
14736 return buffer_flip_blocked_depth > 0;
14740 /***********************************************************************
14741 Window Redisplay
14742 ***********************************************************************/
14744 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14746 static void
14747 redisplay_windows (Lisp_Object window)
14749 while (!NILP (window))
14751 struct window *w = XWINDOW (window);
14753 if (WINDOWP (w->contents))
14754 redisplay_windows (w->contents);
14755 else if (BUFFERP (w->contents))
14757 displayed_buffer = XBUFFER (w->contents);
14758 /* Use list_of_error, not Qerror, so that
14759 we catch only errors and don't run the debugger. */
14760 internal_condition_case_1 (redisplay_window_0, window,
14761 list_of_error,
14762 redisplay_window_error);
14765 window = w->next;
14769 static Lisp_Object
14770 redisplay_window_error (Lisp_Object ignore)
14772 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14773 return Qnil;
14776 static Lisp_Object
14777 redisplay_window_0 (Lisp_Object window)
14779 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14780 redisplay_window (window, false);
14781 return Qnil;
14784 static Lisp_Object
14785 redisplay_window_1 (Lisp_Object window)
14787 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14788 redisplay_window (window, true);
14789 return Qnil;
14793 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14794 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14795 which positions recorded in ROW differ from current buffer
14796 positions.
14798 Return true iff cursor is on this row. */
14800 static bool
14801 set_cursor_from_row (struct window *w, struct glyph_row *row,
14802 struct glyph_matrix *matrix,
14803 ptrdiff_t delta, ptrdiff_t delta_bytes,
14804 int dy, int dvpos)
14806 struct glyph *glyph = row->glyphs[TEXT_AREA];
14807 struct glyph *end = glyph + row->used[TEXT_AREA];
14808 struct glyph *cursor = NULL;
14809 /* The last known character position in row. */
14810 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14811 int x = row->x;
14812 ptrdiff_t pt_old = PT - delta;
14813 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14814 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14815 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14816 /* A glyph beyond the edge of TEXT_AREA which we should never
14817 touch. */
14818 struct glyph *glyphs_end = end;
14819 /* True means we've found a match for cursor position, but that
14820 glyph has the avoid_cursor_p flag set. */
14821 bool match_with_avoid_cursor = false;
14822 /* True means we've seen at least one glyph that came from a
14823 display string. */
14824 bool string_seen = false;
14825 /* Largest and smallest buffer positions seen so far during scan of
14826 glyph row. */
14827 ptrdiff_t bpos_max = pos_before;
14828 ptrdiff_t bpos_min = pos_after;
14829 /* Last buffer position covered by an overlay string with an integer
14830 `cursor' property. */
14831 ptrdiff_t bpos_covered = 0;
14832 /* True means the display string on which to display the cursor
14833 comes from a text property, not from an overlay. */
14834 bool string_from_text_prop = false;
14836 /* Don't even try doing anything if called for a mode-line or
14837 header-line row, since the rest of the code isn't prepared to
14838 deal with such calamities. */
14839 eassert (!row->mode_line_p);
14840 if (row->mode_line_p)
14841 return false;
14843 /* Skip over glyphs not having an object at the start and the end of
14844 the row. These are special glyphs like truncation marks on
14845 terminal frames. */
14846 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14848 if (!row->reversed_p)
14850 while (glyph < end
14851 && NILP (glyph->object)
14852 && glyph->charpos < 0)
14854 x += glyph->pixel_width;
14855 ++glyph;
14857 while (end > glyph
14858 && NILP ((end - 1)->object)
14859 /* CHARPOS is zero for blanks and stretch glyphs
14860 inserted by extend_face_to_end_of_line. */
14861 && (end - 1)->charpos <= 0)
14862 --end;
14863 glyph_before = glyph - 1;
14864 glyph_after = end;
14866 else
14868 struct glyph *g;
14870 /* If the glyph row is reversed, we need to process it from back
14871 to front, so swap the edge pointers. */
14872 glyphs_end = end = glyph - 1;
14873 glyph += row->used[TEXT_AREA] - 1;
14875 while (glyph > end + 1
14876 && NILP (glyph->object)
14877 && glyph->charpos < 0)
14878 --glyph;
14879 if (NILP (glyph->object) && glyph->charpos < 0)
14880 --glyph;
14881 /* By default, in reversed rows we put the cursor on the
14882 rightmost (first in the reading order) glyph. */
14883 for (x = 0, g = end + 1; g < glyph; g++)
14884 x += g->pixel_width;
14885 while (end < glyph
14886 && NILP ((end + 1)->object)
14887 && (end + 1)->charpos <= 0)
14888 ++end;
14889 glyph_before = glyph + 1;
14890 glyph_after = end;
14893 else if (row->reversed_p)
14895 /* In R2L rows that don't display text, put the cursor on the
14896 rightmost glyph. Case in point: an empty last line that is
14897 part of an R2L paragraph. */
14898 cursor = end - 1;
14899 /* Avoid placing the cursor on the last glyph of the row, where
14900 on terminal frames we hold the vertical border between
14901 adjacent windows. */
14902 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14903 && !WINDOW_RIGHTMOST_P (w)
14904 && cursor == row->glyphs[LAST_AREA] - 1)
14905 cursor--;
14906 x = -1; /* will be computed below, at label compute_x */
14909 /* Step 1: Try to find the glyph whose character position
14910 corresponds to point. If that's not possible, find 2 glyphs
14911 whose character positions are the closest to point, one before
14912 point, the other after it. */
14913 if (!row->reversed_p)
14914 while (/* not marched to end of glyph row */
14915 glyph < end
14916 /* glyph was not inserted by redisplay for internal purposes */
14917 && !NILP (glyph->object))
14919 if (BUFFERP (glyph->object))
14921 ptrdiff_t dpos = glyph->charpos - pt_old;
14923 if (glyph->charpos > bpos_max)
14924 bpos_max = glyph->charpos;
14925 if (glyph->charpos < bpos_min)
14926 bpos_min = glyph->charpos;
14927 if (!glyph->avoid_cursor_p)
14929 /* If we hit point, we've found the glyph on which to
14930 display the cursor. */
14931 if (dpos == 0)
14933 match_with_avoid_cursor = false;
14934 break;
14936 /* See if we've found a better approximation to
14937 POS_BEFORE or to POS_AFTER. */
14938 if (0 > dpos && dpos > pos_before - pt_old)
14940 pos_before = glyph->charpos;
14941 glyph_before = glyph;
14943 else if (0 < dpos && dpos < pos_after - pt_old)
14945 pos_after = glyph->charpos;
14946 glyph_after = glyph;
14949 else if (dpos == 0)
14950 match_with_avoid_cursor = true;
14952 else if (STRINGP (glyph->object))
14954 Lisp_Object chprop;
14955 ptrdiff_t glyph_pos = glyph->charpos;
14957 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14958 glyph->object);
14959 if (!NILP (chprop))
14961 /* If the string came from a `display' text property,
14962 look up the buffer position of that property and
14963 use that position to update bpos_max, as if we
14964 actually saw such a position in one of the row's
14965 glyphs. This helps with supporting integer values
14966 of `cursor' property on the display string in
14967 situations where most or all of the row's buffer
14968 text is completely covered by display properties,
14969 so that no glyph with valid buffer positions is
14970 ever seen in the row. */
14971 ptrdiff_t prop_pos =
14972 string_buffer_position_lim (glyph->object, pos_before,
14973 pos_after, false);
14975 if (prop_pos >= pos_before)
14976 bpos_max = prop_pos;
14978 if (INTEGERP (chprop))
14980 bpos_covered = bpos_max + XINT (chprop);
14981 /* If the `cursor' property covers buffer positions up
14982 to and including point, we should display cursor on
14983 this glyph. Note that, if a `cursor' property on one
14984 of the string's characters has an integer value, we
14985 will break out of the loop below _before_ we get to
14986 the position match above. IOW, integer values of
14987 the `cursor' property override the "exact match for
14988 point" strategy of positioning the cursor. */
14989 /* Implementation note: bpos_max == pt_old when, e.g.,
14990 we are in an empty line, where bpos_max is set to
14991 MATRIX_ROW_START_CHARPOS, see above. */
14992 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14994 cursor = glyph;
14995 break;
14999 string_seen = true;
15001 x += glyph->pixel_width;
15002 ++glyph;
15004 else if (glyph > end) /* row is reversed */
15005 while (!NILP (glyph->object))
15007 if (BUFFERP (glyph->object))
15009 ptrdiff_t dpos = glyph->charpos - pt_old;
15011 if (glyph->charpos > bpos_max)
15012 bpos_max = glyph->charpos;
15013 if (glyph->charpos < bpos_min)
15014 bpos_min = glyph->charpos;
15015 if (!glyph->avoid_cursor_p)
15017 if (dpos == 0)
15019 match_with_avoid_cursor = false;
15020 break;
15022 if (0 > dpos && dpos > pos_before - pt_old)
15024 pos_before = glyph->charpos;
15025 glyph_before = glyph;
15027 else if (0 < dpos && dpos < pos_after - pt_old)
15029 pos_after = glyph->charpos;
15030 glyph_after = glyph;
15033 else if (dpos == 0)
15034 match_with_avoid_cursor = true;
15036 else if (STRINGP (glyph->object))
15038 Lisp_Object chprop;
15039 ptrdiff_t glyph_pos = glyph->charpos;
15041 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15042 glyph->object);
15043 if (!NILP (chprop))
15045 ptrdiff_t prop_pos =
15046 string_buffer_position_lim (glyph->object, pos_before,
15047 pos_after, false);
15049 if (prop_pos >= pos_before)
15050 bpos_max = prop_pos;
15052 if (INTEGERP (chprop))
15054 bpos_covered = bpos_max + XINT (chprop);
15055 /* If the `cursor' property covers buffer positions up
15056 to and including point, we should display cursor on
15057 this glyph. */
15058 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15060 cursor = glyph;
15061 break;
15064 string_seen = true;
15066 --glyph;
15067 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15069 x--; /* can't use any pixel_width */
15070 break;
15072 x -= glyph->pixel_width;
15075 /* Step 2: If we didn't find an exact match for point, we need to
15076 look for a proper place to put the cursor among glyphs between
15077 GLYPH_BEFORE and GLYPH_AFTER. */
15078 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15079 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15080 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15082 /* An empty line has a single glyph whose OBJECT is nil and
15083 whose CHARPOS is the position of a newline on that line.
15084 Note that on a TTY, there are more glyphs after that, which
15085 were produced by extend_face_to_end_of_line, but their
15086 CHARPOS is zero or negative. */
15087 bool empty_line_p =
15088 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15089 && NILP (glyph->object) && glyph->charpos > 0
15090 /* On a TTY, continued and truncated rows also have a glyph at
15091 their end whose OBJECT is nil and whose CHARPOS is
15092 positive (the continuation and truncation glyphs), but such
15093 rows are obviously not "empty". */
15094 && !(row->continued_p || row->truncated_on_right_p));
15096 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15098 ptrdiff_t ellipsis_pos;
15100 /* Scan back over the ellipsis glyphs. */
15101 if (!row->reversed_p)
15103 ellipsis_pos = (glyph - 1)->charpos;
15104 while (glyph > row->glyphs[TEXT_AREA]
15105 && (glyph - 1)->charpos == ellipsis_pos)
15106 glyph--, x -= glyph->pixel_width;
15107 /* That loop always goes one position too far, including
15108 the glyph before the ellipsis. So scan forward over
15109 that one. */
15110 x += glyph->pixel_width;
15111 glyph++;
15113 else /* row is reversed */
15115 ellipsis_pos = (glyph + 1)->charpos;
15116 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15117 && (glyph + 1)->charpos == ellipsis_pos)
15118 glyph++, x += glyph->pixel_width;
15119 x -= glyph->pixel_width;
15120 glyph--;
15123 else if (match_with_avoid_cursor)
15125 cursor = glyph_after;
15126 x = -1;
15128 else if (string_seen)
15130 int incr = row->reversed_p ? -1 : +1;
15132 /* Need to find the glyph that came out of a string which is
15133 present at point. That glyph is somewhere between
15134 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15135 positioned between POS_BEFORE and POS_AFTER in the
15136 buffer. */
15137 struct glyph *start, *stop;
15138 ptrdiff_t pos = pos_before;
15140 x = -1;
15142 /* If the row ends in a newline from a display string,
15143 reordering could have moved the glyphs belonging to the
15144 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15145 in this case we extend the search to the last glyph in
15146 the row that was not inserted by redisplay. */
15147 if (row->ends_in_newline_from_string_p)
15149 glyph_after = end;
15150 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15153 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15154 correspond to POS_BEFORE and POS_AFTER, respectively. We
15155 need START and STOP in the order that corresponds to the
15156 row's direction as given by its reversed_p flag. If the
15157 directionality of characters between POS_BEFORE and
15158 POS_AFTER is the opposite of the row's base direction,
15159 these characters will have been reordered for display,
15160 and we need to reverse START and STOP. */
15161 if (!row->reversed_p)
15163 start = min (glyph_before, glyph_after);
15164 stop = max (glyph_before, glyph_after);
15166 else
15168 start = max (glyph_before, glyph_after);
15169 stop = min (glyph_before, glyph_after);
15171 for (glyph = start + incr;
15172 row->reversed_p ? glyph > stop : glyph < stop; )
15175 /* Any glyphs that come from the buffer are here because
15176 of bidi reordering. Skip them, and only pay
15177 attention to glyphs that came from some string. */
15178 if (STRINGP (glyph->object))
15180 Lisp_Object str;
15181 ptrdiff_t tem;
15182 /* If the display property covers the newline, we
15183 need to search for it one position farther. */
15184 ptrdiff_t lim = pos_after
15185 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15187 string_from_text_prop = false;
15188 str = glyph->object;
15189 tem = string_buffer_position_lim (str, pos, lim, false);
15190 if (tem == 0 /* from overlay */
15191 || pos <= tem)
15193 /* If the string from which this glyph came is
15194 found in the buffer at point, or at position
15195 that is closer to point than pos_after, then
15196 we've found the glyph we've been looking for.
15197 If it comes from an overlay (tem == 0), and
15198 it has the `cursor' property on one of its
15199 glyphs, record that glyph as a candidate for
15200 displaying the cursor. (As in the
15201 unidirectional version, we will display the
15202 cursor on the last candidate we find.) */
15203 if (tem == 0
15204 || tem == pt_old
15205 || (tem - pt_old > 0 && tem < pos_after))
15207 /* The glyphs from this string could have
15208 been reordered. Find the one with the
15209 smallest string position. Or there could
15210 be a character in the string with the
15211 `cursor' property, which means display
15212 cursor on that character's glyph. */
15213 ptrdiff_t strpos = glyph->charpos;
15215 if (tem)
15217 cursor = glyph;
15218 string_from_text_prop = true;
15220 for ( ;
15221 (row->reversed_p ? glyph > stop : glyph < stop)
15222 && EQ (glyph->object, str);
15223 glyph += incr)
15225 Lisp_Object cprop;
15226 ptrdiff_t gpos = glyph->charpos;
15228 cprop = Fget_char_property (make_number (gpos),
15229 Qcursor,
15230 glyph->object);
15231 if (!NILP (cprop))
15233 cursor = glyph;
15234 break;
15236 if (tem && glyph->charpos < strpos)
15238 strpos = glyph->charpos;
15239 cursor = glyph;
15243 if (tem == pt_old
15244 || (tem - pt_old > 0 && tem < pos_after))
15245 goto compute_x;
15247 if (tem)
15248 pos = tem + 1; /* don't find previous instances */
15250 /* This string is not what we want; skip all of the
15251 glyphs that came from it. */
15252 while ((row->reversed_p ? glyph > stop : glyph < stop)
15253 && EQ (glyph->object, str))
15254 glyph += incr;
15256 else
15257 glyph += incr;
15260 /* If we reached the end of the line, and END was from a string,
15261 the cursor is not on this line. */
15262 if (cursor == NULL
15263 && (row->reversed_p ? glyph <= end : glyph >= end)
15264 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15265 && STRINGP (end->object)
15266 && row->continued_p)
15267 return false;
15269 /* A truncated row may not include PT among its character positions.
15270 Setting the cursor inside the scroll margin will trigger
15271 recalculation of hscroll in hscroll_window_tree. But if a
15272 display string covers point, defer to the string-handling
15273 code below to figure this out. */
15274 else if (row->truncated_on_left_p && pt_old < bpos_min)
15276 cursor = glyph_before;
15277 x = -1;
15279 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15280 /* Zero-width characters produce no glyphs. */
15281 || (!empty_line_p
15282 && (row->reversed_p
15283 ? glyph_after > glyphs_end
15284 : glyph_after < glyphs_end)))
15286 cursor = glyph_after;
15287 x = -1;
15291 compute_x:
15292 if (cursor != NULL)
15293 glyph = cursor;
15294 else if (glyph == glyphs_end
15295 && pos_before == pos_after
15296 && STRINGP ((row->reversed_p
15297 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15298 : row->glyphs[TEXT_AREA])->object))
15300 /* If all the glyphs of this row came from strings, put the
15301 cursor on the first glyph of the row. This avoids having the
15302 cursor outside of the text area in this very rare and hard
15303 use case. */
15304 glyph =
15305 row->reversed_p
15306 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15307 : row->glyphs[TEXT_AREA];
15309 if (x < 0)
15311 struct glyph *g;
15313 /* Need to compute x that corresponds to GLYPH. */
15314 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15316 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15317 emacs_abort ();
15318 x += g->pixel_width;
15322 /* ROW could be part of a continued line, which, under bidi
15323 reordering, might have other rows whose start and end charpos
15324 occlude point. Only set w->cursor if we found a better
15325 approximation to the cursor position than we have from previously
15326 examined candidate rows belonging to the same continued line. */
15327 if (/* We already have a candidate row. */
15328 w->cursor.vpos >= 0
15329 /* That candidate is not the row we are processing. */
15330 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15331 /* Make sure cursor.vpos specifies a row whose start and end
15332 charpos occlude point, and it is valid candidate for being a
15333 cursor-row. This is because some callers of this function
15334 leave cursor.vpos at the row where the cursor was displayed
15335 during the last redisplay cycle. */
15336 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15337 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15338 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15340 struct glyph *g1
15341 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15343 /* Don't consider glyphs that are outside TEXT_AREA. */
15344 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15345 return false;
15346 /* Keep the candidate whose buffer position is the closest to
15347 point or has the `cursor' property. */
15348 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15349 w->cursor.hpos >= 0
15350 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15351 && ((BUFFERP (g1->object)
15352 && (g1->charpos == pt_old /* An exact match always wins. */
15353 || (BUFFERP (glyph->object)
15354 && eabs (g1->charpos - pt_old)
15355 < eabs (glyph->charpos - pt_old))))
15356 /* Previous candidate is a glyph from a string that has
15357 a non-nil `cursor' property. */
15358 || (STRINGP (g1->object)
15359 && (!NILP (Fget_char_property (make_number (g1->charpos),
15360 Qcursor, g1->object))
15361 /* Previous candidate is from the same display
15362 string as this one, and the display string
15363 came from a text property. */
15364 || (EQ (g1->object, glyph->object)
15365 && string_from_text_prop)
15366 /* this candidate is from newline and its
15367 position is not an exact match */
15368 || (NILP (glyph->object)
15369 && glyph->charpos != pt_old)))))
15370 return false;
15371 /* If this candidate gives an exact match, use that. */
15372 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15373 /* If this candidate is a glyph created for the
15374 terminating newline of a line, and point is on that
15375 newline, it wins because it's an exact match. */
15376 || (!row->continued_p
15377 && NILP (glyph->object)
15378 && glyph->charpos == 0
15379 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15380 /* Otherwise, keep the candidate that comes from a row
15381 spanning less buffer positions. This may win when one or
15382 both candidate positions are on glyphs that came from
15383 display strings, for which we cannot compare buffer
15384 positions. */
15385 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15386 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15387 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15388 return false;
15390 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15391 w->cursor.x = x;
15392 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15393 w->cursor.y = row->y + dy;
15395 if (w == XWINDOW (selected_window))
15397 if (!row->continued_p
15398 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15399 && row->x == 0)
15401 this_line_buffer = XBUFFER (w->contents);
15403 CHARPOS (this_line_start_pos)
15404 = MATRIX_ROW_START_CHARPOS (row) + delta;
15405 BYTEPOS (this_line_start_pos)
15406 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15408 CHARPOS (this_line_end_pos)
15409 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15410 BYTEPOS (this_line_end_pos)
15411 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15413 this_line_y = w->cursor.y;
15414 this_line_pixel_height = row->height;
15415 this_line_vpos = w->cursor.vpos;
15416 this_line_start_x = row->x;
15418 else
15419 CHARPOS (this_line_start_pos) = 0;
15422 return true;
15426 /* Run window scroll functions, if any, for WINDOW with new window
15427 start STARTP. Sets the window start of WINDOW to that position.
15429 We assume that the window's buffer is really current. */
15431 static struct text_pos
15432 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15434 struct window *w = XWINDOW (window);
15435 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15437 eassert (current_buffer == XBUFFER (w->contents));
15439 if (!NILP (Vwindow_scroll_functions))
15441 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15442 make_number (CHARPOS (startp)));
15443 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15444 /* In case the hook functions switch buffers. */
15445 set_buffer_internal (XBUFFER (w->contents));
15448 return startp;
15452 /* Make sure the line containing the cursor is fully visible.
15453 A value of true means there is nothing to be done.
15454 (Either the line is fully visible, or it cannot be made so,
15455 or we cannot tell.)
15457 If FORCE_P, return false even if partial visible cursor row
15458 is higher than window.
15460 If CURRENT_MATRIX_P, use the information from the
15461 window's current glyph matrix; otherwise use the desired glyph
15462 matrix.
15464 A value of false means the caller should do scrolling
15465 as if point had gone off the screen. */
15467 static bool
15468 cursor_row_fully_visible_p (struct window *w, bool force_p,
15469 bool current_matrix_p)
15471 struct glyph_matrix *matrix;
15472 struct glyph_row *row;
15473 int window_height;
15475 if (!make_cursor_line_fully_visible_p)
15476 return true;
15478 /* It's not always possible to find the cursor, e.g, when a window
15479 is full of overlay strings. Don't do anything in that case. */
15480 if (w->cursor.vpos < 0)
15481 return true;
15483 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15484 row = MATRIX_ROW (matrix, w->cursor.vpos);
15486 /* If the cursor row is not partially visible, there's nothing to do. */
15487 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15488 return true;
15490 /* If the row the cursor is in is taller than the window's height,
15491 it's not clear what to do, so do nothing. */
15492 window_height = window_box_height (w);
15493 if (row->height >= window_height)
15495 if (!force_p || MINI_WINDOW_P (w)
15496 || w->vscroll || w->cursor.vpos == 0)
15497 return true;
15499 return false;
15503 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15504 means only WINDOW is redisplayed in redisplay_internal.
15505 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15506 in redisplay_window to bring a partially visible line into view in
15507 the case that only the cursor has moved.
15509 LAST_LINE_MISFIT should be true if we're scrolling because the
15510 last screen line's vertical height extends past the end of the screen.
15512 Value is
15514 1 if scrolling succeeded
15516 0 if scrolling didn't find point.
15518 -1 if new fonts have been loaded so that we must interrupt
15519 redisplay, adjust glyph matrices, and try again. */
15521 enum
15523 SCROLLING_SUCCESS,
15524 SCROLLING_FAILED,
15525 SCROLLING_NEED_LARGER_MATRICES
15528 /* If scroll-conservatively is more than this, never recenter.
15530 If you change this, don't forget to update the doc string of
15531 `scroll-conservatively' and the Emacs manual. */
15532 #define SCROLL_LIMIT 100
15534 static int
15535 try_scrolling (Lisp_Object window, bool just_this_one_p,
15536 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15537 bool temp_scroll_step, bool last_line_misfit)
15539 struct window *w = XWINDOW (window);
15540 struct text_pos pos, startp;
15541 struct it it;
15542 int this_scroll_margin, scroll_max, rc, height;
15543 int dy = 0, amount_to_scroll = 0;
15544 bool scroll_down_p = false;
15545 int extra_scroll_margin_lines = last_line_misfit;
15546 Lisp_Object aggressive;
15547 /* We will never try scrolling more than this number of lines. */
15548 int scroll_limit = SCROLL_LIMIT;
15549 int frame_line_height = default_line_pixel_height (w);
15551 #ifdef GLYPH_DEBUG
15552 debug_method_add (w, "try_scrolling");
15553 #endif
15555 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15557 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15559 /* Force arg_scroll_conservatively to have a reasonable value, to
15560 avoid scrolling too far away with slow move_it_* functions. Note
15561 that the user can supply scroll-conservatively equal to
15562 `most-positive-fixnum', which can be larger than INT_MAX. */
15563 if (arg_scroll_conservatively > scroll_limit)
15565 arg_scroll_conservatively = scroll_limit + 1;
15566 scroll_max = scroll_limit * frame_line_height;
15568 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15569 /* Compute how much we should try to scroll maximally to bring
15570 point into view. */
15571 scroll_max = (max (scroll_step,
15572 max (arg_scroll_conservatively, temp_scroll_step))
15573 * frame_line_height);
15574 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15575 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15576 /* We're trying to scroll because of aggressive scrolling but no
15577 scroll_step is set. Choose an arbitrary one. */
15578 scroll_max = 10 * frame_line_height;
15579 else
15580 scroll_max = 0;
15582 too_near_end:
15584 /* Decide whether to scroll down. */
15585 if (PT > CHARPOS (startp))
15587 int scroll_margin_y;
15589 /* Compute the pixel ypos of the scroll margin, then move IT to
15590 either that ypos or PT, whichever comes first. */
15591 start_display (&it, w, startp);
15592 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15593 - this_scroll_margin
15594 - frame_line_height * extra_scroll_margin_lines;
15595 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15596 (MOVE_TO_POS | MOVE_TO_Y));
15598 if (PT > CHARPOS (it.current.pos))
15600 int y0 = line_bottom_y (&it);
15601 /* Compute how many pixels below window bottom to stop searching
15602 for PT. This avoids costly search for PT that is far away if
15603 the user limited scrolling by a small number of lines, but
15604 always finds PT if scroll_conservatively is set to a large
15605 number, such as most-positive-fixnum. */
15606 int slack = max (scroll_max, 10 * frame_line_height);
15607 int y_to_move = it.last_visible_y + slack;
15609 /* Compute the distance from the scroll margin to PT or to
15610 the scroll limit, whichever comes first. This should
15611 include the height of the cursor line, to make that line
15612 fully visible. */
15613 move_it_to (&it, PT, -1, y_to_move,
15614 -1, MOVE_TO_POS | MOVE_TO_Y);
15615 dy = line_bottom_y (&it) - y0;
15617 if (dy > scroll_max)
15618 return SCROLLING_FAILED;
15620 if (dy > 0)
15621 scroll_down_p = true;
15623 else if (PT == IT_CHARPOS (it)
15624 && IT_CHARPOS (it) < ZV
15625 && it.method == GET_FROM_STRING
15626 && arg_scroll_conservatively > scroll_limit
15627 && it.current_x == 0)
15629 enum move_it_result skip;
15630 int y1 = it.current_y;
15631 int vpos;
15633 /* A before-string that includes newlines and is displayed
15634 on the last visible screen line could fail us under
15635 scroll-conservatively > 100, because we will be unable to
15636 position the cursor on that last visible line. Try to
15637 recover by finding the first screen line that has some
15638 glyphs coming from the buffer text. */
15639 do {
15640 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15641 if (skip != MOVE_NEWLINE_OR_CR
15642 || IT_CHARPOS (it) != PT
15643 || it.method == GET_FROM_BUFFER)
15644 break;
15645 vpos = it.vpos;
15646 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15647 } while (it.vpos > vpos);
15649 dy = it.current_y - y1;
15651 if (dy > scroll_max)
15652 return SCROLLING_FAILED;
15654 if (dy > 0)
15655 scroll_down_p = true;
15659 if (scroll_down_p)
15661 /* Point is in or below the bottom scroll margin, so move the
15662 window start down. If scrolling conservatively, move it just
15663 enough down to make point visible. If scroll_step is set,
15664 move it down by scroll_step. */
15665 if (arg_scroll_conservatively)
15666 amount_to_scroll
15667 = min (max (dy, frame_line_height),
15668 frame_line_height * arg_scroll_conservatively);
15669 else if (scroll_step || temp_scroll_step)
15670 amount_to_scroll = scroll_max;
15671 else
15673 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15674 height = WINDOW_BOX_TEXT_HEIGHT (w);
15675 if (NUMBERP (aggressive))
15677 double float_amount = XFLOATINT (aggressive) * height;
15678 int aggressive_scroll = float_amount;
15679 if (aggressive_scroll == 0 && float_amount > 0)
15680 aggressive_scroll = 1;
15681 /* Don't let point enter the scroll margin near top of
15682 the window. This could happen if the value of
15683 scroll_up_aggressively is too large and there are
15684 non-zero margins, because scroll_up_aggressively
15685 means put point that fraction of window height
15686 _from_the_bottom_margin_. */
15687 if (aggressive_scroll + 2 * this_scroll_margin > height)
15688 aggressive_scroll = height - 2 * this_scroll_margin;
15689 amount_to_scroll = dy + aggressive_scroll;
15693 if (amount_to_scroll <= 0)
15694 return SCROLLING_FAILED;
15696 start_display (&it, w, startp);
15697 if (arg_scroll_conservatively <= scroll_limit)
15698 move_it_vertically (&it, amount_to_scroll);
15699 else
15701 /* Extra precision for users who set scroll-conservatively
15702 to a large number: make sure the amount we scroll
15703 the window start is never less than amount_to_scroll,
15704 which was computed as distance from window bottom to
15705 point. This matters when lines at window top and lines
15706 below window bottom have different height. */
15707 struct it it1;
15708 void *it1data = NULL;
15709 /* We use a temporary it1 because line_bottom_y can modify
15710 its argument, if it moves one line down; see there. */
15711 int start_y;
15713 SAVE_IT (it1, it, it1data);
15714 start_y = line_bottom_y (&it1);
15715 do {
15716 RESTORE_IT (&it, &it, it1data);
15717 move_it_by_lines (&it, 1);
15718 SAVE_IT (it1, it, it1data);
15719 } while (IT_CHARPOS (it) < ZV
15720 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15721 bidi_unshelve_cache (it1data, true);
15724 /* If STARTP is unchanged, move it down another screen line. */
15725 if (IT_CHARPOS (it) == CHARPOS (startp))
15726 move_it_by_lines (&it, 1);
15727 startp = it.current.pos;
15729 else
15731 struct text_pos scroll_margin_pos = startp;
15732 int y_offset = 0;
15734 /* See if point is inside the scroll margin at the top of the
15735 window. */
15736 if (this_scroll_margin)
15738 int y_start;
15740 start_display (&it, w, startp);
15741 y_start = it.current_y;
15742 move_it_vertically (&it, this_scroll_margin);
15743 scroll_margin_pos = it.current.pos;
15744 /* If we didn't move enough before hitting ZV, request
15745 additional amount of scroll, to move point out of the
15746 scroll margin. */
15747 if (IT_CHARPOS (it) == ZV
15748 && it.current_y - y_start < this_scroll_margin)
15749 y_offset = this_scroll_margin - (it.current_y - y_start);
15752 if (PT < CHARPOS (scroll_margin_pos))
15754 /* Point is in the scroll margin at the top of the window or
15755 above what is displayed in the window. */
15756 int y0, y_to_move;
15758 /* Compute the vertical distance from PT to the scroll
15759 margin position. Move as far as scroll_max allows, or
15760 one screenful, or 10 screen lines, whichever is largest.
15761 Give up if distance is greater than scroll_max or if we
15762 didn't reach the scroll margin position. */
15763 SET_TEXT_POS (pos, PT, PT_BYTE);
15764 start_display (&it, w, pos);
15765 y0 = it.current_y;
15766 y_to_move = max (it.last_visible_y,
15767 max (scroll_max, 10 * frame_line_height));
15768 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15769 y_to_move, -1,
15770 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15771 dy = it.current_y - y0;
15772 if (dy > scroll_max
15773 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15774 return SCROLLING_FAILED;
15776 /* Additional scroll for when ZV was too close to point. */
15777 dy += y_offset;
15779 /* Compute new window start. */
15780 start_display (&it, w, startp);
15782 if (arg_scroll_conservatively)
15783 amount_to_scroll = max (dy, frame_line_height
15784 * max (scroll_step, temp_scroll_step));
15785 else if (scroll_step || temp_scroll_step)
15786 amount_to_scroll = scroll_max;
15787 else
15789 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15790 height = WINDOW_BOX_TEXT_HEIGHT (w);
15791 if (NUMBERP (aggressive))
15793 double float_amount = XFLOATINT (aggressive) * height;
15794 int aggressive_scroll = float_amount;
15795 if (aggressive_scroll == 0 && float_amount > 0)
15796 aggressive_scroll = 1;
15797 /* Don't let point enter the scroll margin near
15798 bottom of the window, if the value of
15799 scroll_down_aggressively happens to be too
15800 large. */
15801 if (aggressive_scroll + 2 * this_scroll_margin > height)
15802 aggressive_scroll = height - 2 * this_scroll_margin;
15803 amount_to_scroll = dy + aggressive_scroll;
15807 if (amount_to_scroll <= 0)
15808 return SCROLLING_FAILED;
15810 move_it_vertically_backward (&it, amount_to_scroll);
15811 startp = it.current.pos;
15815 /* Run window scroll functions. */
15816 startp = run_window_scroll_functions (window, startp);
15818 /* Display the window. Give up if new fonts are loaded, or if point
15819 doesn't appear. */
15820 if (!try_window (window, startp, 0))
15821 rc = SCROLLING_NEED_LARGER_MATRICES;
15822 else if (w->cursor.vpos < 0)
15824 clear_glyph_matrix (w->desired_matrix);
15825 rc = SCROLLING_FAILED;
15827 else
15829 /* Maybe forget recorded base line for line number display. */
15830 if (!just_this_one_p
15831 || current_buffer->clip_changed
15832 || BEG_UNCHANGED < CHARPOS (startp))
15833 w->base_line_number = 0;
15835 /* If cursor ends up on a partially visible line,
15836 treat that as being off the bottom of the screen. */
15837 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15838 false)
15839 /* It's possible that the cursor is on the first line of the
15840 buffer, which is partially obscured due to a vscroll
15841 (Bug#7537). In that case, avoid looping forever. */
15842 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15844 clear_glyph_matrix (w->desired_matrix);
15845 ++extra_scroll_margin_lines;
15846 goto too_near_end;
15848 rc = SCROLLING_SUCCESS;
15851 return rc;
15855 /* Compute a suitable window start for window W if display of W starts
15856 on a continuation line. Value is true if a new window start
15857 was computed.
15859 The new window start will be computed, based on W's width, starting
15860 from the start of the continued line. It is the start of the
15861 screen line with the minimum distance from the old start W->start,
15862 which is still before point (otherwise point will definitely not
15863 be visible in the window). */
15865 static bool
15866 compute_window_start_on_continuation_line (struct window *w)
15868 struct text_pos pos, start_pos, pos_before_pt;
15869 bool window_start_changed_p = false;
15871 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15873 /* If window start is on a continuation line... Window start may be
15874 < BEGV in case there's invisible text at the start of the
15875 buffer (M-x rmail, for example). */
15876 if (CHARPOS (start_pos) > BEGV
15877 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15879 struct it it;
15880 struct glyph_row *row;
15882 /* Handle the case that the window start is out of range. */
15883 if (CHARPOS (start_pos) < BEGV)
15884 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15885 else if (CHARPOS (start_pos) > ZV)
15886 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15888 /* Find the start of the continued line. This should be fast
15889 because find_newline is fast (newline cache). */
15890 row = w->desired_matrix->rows + window_wants_header_line (w);
15891 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15892 row, DEFAULT_FACE_ID);
15893 reseat_at_previous_visible_line_start (&it);
15895 /* If the line start is "too far" away from the window start,
15896 say it takes too much time to compute a new window start.
15897 Also, give up if the line start is after point, as in that
15898 case point will not be visible with any window start we
15899 compute. */
15900 if (IT_CHARPOS (it) <= PT
15901 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15902 /* PXW: Do we need upper bounds here? */
15903 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15905 int min_distance, distance;
15907 /* Move forward by display lines to find the new window
15908 start. If window width was enlarged, the new start can
15909 be expected to be > the old start. If window width was
15910 decreased, the new window start will be < the old start.
15911 So, we're looking for the display line start with the
15912 minimum distance from the old window start. */
15913 pos_before_pt = pos = it.current.pos;
15914 min_distance = DISP_INFINITY;
15915 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15916 distance < min_distance)
15918 min_distance = distance;
15919 if (CHARPOS (pos) <= PT)
15920 pos_before_pt = pos;
15921 pos = it.current.pos;
15922 if (it.line_wrap == WORD_WRAP)
15924 /* Under WORD_WRAP, move_it_by_lines is likely to
15925 overshoot and stop not at the first, but the
15926 second character from the left margin. So in
15927 that case, we need a more tight control on the X
15928 coordinate of the iterator than move_it_by_lines
15929 promises in its contract. The method is to first
15930 go to the last (rightmost) visible character of a
15931 line, then move to the leftmost character on the
15932 next line in a separate call. */
15933 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15934 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15935 move_it_to (&it, ZV, 0,
15936 it.current_y + it.max_ascent + it.max_descent, -1,
15937 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15939 else
15940 move_it_by_lines (&it, 1);
15943 /* It makes very little sense to make the new window start
15944 after point, as point won't be visible. If that's what
15945 the loop above finds, fall back on the candidate before
15946 or at point that is closest to the old window start. */
15947 if (CHARPOS (pos) > PT)
15948 pos = pos_before_pt;
15950 /* Set the window start there. */
15951 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15952 window_start_changed_p = true;
15956 return window_start_changed_p;
15960 /* Try cursor movement in case text has not changed in window WINDOW,
15961 with window start STARTP. Value is
15963 CURSOR_MOVEMENT_SUCCESS if successful
15965 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15967 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15968 display. *SCROLL_STEP is set to true, under certain circumstances, if
15969 we want to scroll as if scroll-step were set to 1. See the code.
15971 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15972 which case we have to abort this redisplay, and adjust matrices
15973 first. */
15975 enum
15977 CURSOR_MOVEMENT_SUCCESS,
15978 CURSOR_MOVEMENT_CANNOT_BE_USED,
15979 CURSOR_MOVEMENT_MUST_SCROLL,
15980 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15983 static int
15984 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15985 bool *scroll_step)
15987 struct window *w = XWINDOW (window);
15988 struct frame *f = XFRAME (w->frame);
15989 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15991 #ifdef GLYPH_DEBUG
15992 if (inhibit_try_cursor_movement)
15993 return rc;
15994 #endif
15996 /* Previously, there was a check for Lisp integer in the
15997 if-statement below. Now, this field is converted to
15998 ptrdiff_t, thus zero means invalid position in a buffer. */
15999 eassert (w->last_point > 0);
16000 /* Likewise there was a check whether window_end_vpos is nil or larger
16001 than the window. Now window_end_vpos is int and so never nil, but
16002 let's leave eassert to check whether it fits in the window. */
16003 eassert (!w->window_end_valid
16004 || w->window_end_vpos < w->current_matrix->nrows);
16006 /* Handle case where text has not changed, only point, and it has
16007 not moved off the frame. */
16008 if (/* Point may be in this window. */
16009 PT >= CHARPOS (startp)
16010 /* Selective display hasn't changed. */
16011 && !current_buffer->clip_changed
16012 /* Function force-mode-line-update is used to force a thorough
16013 redisplay. It sets either windows_or_buffers_changed or
16014 update_mode_lines. So don't take a shortcut here for these
16015 cases. */
16016 && !update_mode_lines
16017 && !windows_or_buffers_changed
16018 && !f->cursor_type_changed
16019 && NILP (Vshow_trailing_whitespace)
16020 /* When display-line-numbers is in relative mode, moving point
16021 requires to redraw the entire window. */
16022 && !EQ (Vdisplay_line_numbers, Qrelative)
16023 && !EQ (Vdisplay_line_numbers, Qvisual)
16024 /* When the current line number should be displayed in a
16025 distinct face, moving point cannot be handled in optimized
16026 way as below. */
16027 && !(!NILP (Vdisplay_line_numbers)
16028 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16029 Qline_number_current_line,
16030 w->frame)))
16031 /* This code is not used for mini-buffer for the sake of the case
16032 of redisplaying to replace an echo area message; since in
16033 that case the mini-buffer contents per se are usually
16034 unchanged. This code is of no real use in the mini-buffer
16035 since the handling of this_line_start_pos, etc., in redisplay
16036 handles the same cases. */
16037 && !EQ (window, minibuf_window)
16038 && (FRAME_WINDOW_P (f)
16039 || !overlay_arrow_in_current_buffer_p ()))
16041 int this_scroll_margin, top_scroll_margin;
16042 struct glyph_row *row = NULL;
16044 #ifdef GLYPH_DEBUG
16045 debug_method_add (w, "cursor movement");
16046 #endif
16048 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16050 top_scroll_margin = this_scroll_margin;
16051 if (window_wants_header_line (w))
16052 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16054 /* Start with the row the cursor was displayed during the last
16055 not paused redisplay. Give up if that row is not valid. */
16056 if (w->last_cursor_vpos < 0
16057 || w->last_cursor_vpos >= w->current_matrix->nrows)
16058 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16059 else
16061 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16062 if (row->mode_line_p)
16063 ++row;
16064 if (!row->enabled_p)
16065 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16068 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16070 bool scroll_p = false, must_scroll = false;
16071 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16073 if (PT > w->last_point)
16075 /* Point has moved forward. */
16076 while (MATRIX_ROW_END_CHARPOS (row) < PT
16077 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16079 eassert (row->enabled_p);
16080 ++row;
16083 /* If the end position of a row equals the start
16084 position of the next row, and PT is at that position,
16085 we would rather display cursor in the next line. */
16086 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16087 && MATRIX_ROW_END_CHARPOS (row) == PT
16088 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16089 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16090 && !cursor_row_p (row))
16091 ++row;
16093 /* If within the scroll margin, scroll. Note that
16094 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16095 the next line would be drawn, and that
16096 this_scroll_margin can be zero. */
16097 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16098 || PT > MATRIX_ROW_END_CHARPOS (row)
16099 /* Line is completely visible last line in window
16100 and PT is to be set in the next line. */
16101 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16102 && PT == MATRIX_ROW_END_CHARPOS (row)
16103 && !row->ends_at_zv_p
16104 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16105 scroll_p = true;
16107 else if (PT < w->last_point)
16109 /* Cursor has to be moved backward. Note that PT >=
16110 CHARPOS (startp) because of the outer if-statement. */
16111 while (!row->mode_line_p
16112 && (MATRIX_ROW_START_CHARPOS (row) > PT
16113 || (MATRIX_ROW_START_CHARPOS (row) == PT
16114 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16115 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16116 row > w->current_matrix->rows
16117 && (row-1)->ends_in_newline_from_string_p))))
16118 && (row->y > top_scroll_margin
16119 || CHARPOS (startp) == BEGV))
16121 eassert (row->enabled_p);
16122 --row;
16125 /* Consider the following case: Window starts at BEGV,
16126 there is invisible, intangible text at BEGV, so that
16127 display starts at some point START > BEGV. It can
16128 happen that we are called with PT somewhere between
16129 BEGV and START. Try to handle that case. */
16130 if (row < w->current_matrix->rows
16131 || row->mode_line_p)
16133 row = w->current_matrix->rows;
16134 if (row->mode_line_p)
16135 ++row;
16138 /* Due to newlines in overlay strings, we may have to
16139 skip forward over overlay strings. */
16140 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16141 && MATRIX_ROW_END_CHARPOS (row) == PT
16142 && !cursor_row_p (row))
16143 ++row;
16145 /* If within the scroll margin, scroll. */
16146 if (row->y < top_scroll_margin
16147 && CHARPOS (startp) != BEGV)
16148 scroll_p = true;
16150 else
16152 /* Cursor did not move. So don't scroll even if cursor line
16153 is partially visible, as it was so before. */
16154 rc = CURSOR_MOVEMENT_SUCCESS;
16157 if (PT < MATRIX_ROW_START_CHARPOS (row)
16158 || PT > MATRIX_ROW_END_CHARPOS (row))
16160 /* if PT is not in the glyph row, give up. */
16161 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16162 must_scroll = true;
16164 else if (rc != CURSOR_MOVEMENT_SUCCESS
16165 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16167 struct glyph_row *row1;
16169 /* If rows are bidi-reordered and point moved, back up
16170 until we find a row that does not belong to a
16171 continuation line. This is because we must consider
16172 all rows of a continued line as candidates for the
16173 new cursor positioning, since row start and end
16174 positions change non-linearly with vertical position
16175 in such rows. */
16176 /* FIXME: Revisit this when glyph ``spilling'' in
16177 continuation lines' rows is implemented for
16178 bidi-reordered rows. */
16179 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16180 MATRIX_ROW_CONTINUATION_LINE_P (row);
16181 --row)
16183 /* If we hit the beginning of the displayed portion
16184 without finding the first row of a continued
16185 line, give up. */
16186 if (row <= row1)
16188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16189 break;
16191 eassert (row->enabled_p);
16194 if (must_scroll)
16196 else if (rc != CURSOR_MOVEMENT_SUCCESS
16197 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16198 /* Make sure this isn't a header line by any chance, since
16199 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16200 && !row->mode_line_p
16201 && make_cursor_line_fully_visible_p)
16203 if (PT == MATRIX_ROW_END_CHARPOS (row)
16204 && !row->ends_at_zv_p
16205 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16206 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16207 else if (row->height > window_box_height (w))
16209 /* If we end up in a partially visible line, let's
16210 make it fully visible, except when it's taller
16211 than the window, in which case we can't do much
16212 about it. */
16213 *scroll_step = true;
16214 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16216 else
16218 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16219 if (!cursor_row_fully_visible_p (w, false, true))
16220 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16221 else
16222 rc = CURSOR_MOVEMENT_SUCCESS;
16225 else if (scroll_p)
16226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16227 else if (rc != CURSOR_MOVEMENT_SUCCESS
16228 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16230 /* With bidi-reordered rows, there could be more than
16231 one candidate row whose start and end positions
16232 occlude point. We need to let set_cursor_from_row
16233 find the best candidate. */
16234 /* FIXME: Revisit this when glyph ``spilling'' in
16235 continuation lines' rows is implemented for
16236 bidi-reordered rows. */
16237 bool rv = false;
16241 bool at_zv_p = false, exact_match_p = false;
16243 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16244 && PT <= MATRIX_ROW_END_CHARPOS (row)
16245 && cursor_row_p (row))
16246 rv |= set_cursor_from_row (w, row, w->current_matrix,
16247 0, 0, 0, 0);
16248 /* As soon as we've found the exact match for point,
16249 or the first suitable row whose ends_at_zv_p flag
16250 is set, we are done. */
16251 if (rv)
16253 at_zv_p = MATRIX_ROW (w->current_matrix,
16254 w->cursor.vpos)->ends_at_zv_p;
16255 if (!at_zv_p
16256 && w->cursor.hpos >= 0
16257 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16258 w->cursor.vpos))
16260 struct glyph_row *candidate =
16261 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16262 struct glyph *g =
16263 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16264 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16266 exact_match_p =
16267 (BUFFERP (g->object) && g->charpos == PT)
16268 || (NILP (g->object)
16269 && (g->charpos == PT
16270 || (g->charpos == 0 && endpos - 1 == PT)));
16272 if (at_zv_p || exact_match_p)
16274 rc = CURSOR_MOVEMENT_SUCCESS;
16275 break;
16278 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16279 break;
16280 ++row;
16282 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16283 || row->continued_p)
16284 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16285 || (MATRIX_ROW_START_CHARPOS (row) == PT
16286 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16287 /* If we didn't find any candidate rows, or exited the
16288 loop before all the candidates were examined, signal
16289 to the caller that this method failed. */
16290 if (rc != CURSOR_MOVEMENT_SUCCESS
16291 && !(rv
16292 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16293 && !row->continued_p))
16294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16295 else if (rv)
16296 rc = CURSOR_MOVEMENT_SUCCESS;
16298 else
16302 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16304 rc = CURSOR_MOVEMENT_SUCCESS;
16305 break;
16307 ++row;
16309 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16310 && MATRIX_ROW_START_CHARPOS (row) == PT
16311 && cursor_row_p (row));
16316 return rc;
16320 void
16321 set_vertical_scroll_bar (struct window *w)
16323 ptrdiff_t start, end, whole;
16325 /* Calculate the start and end positions for the current window.
16326 At some point, it would be nice to choose between scrollbars
16327 which reflect the whole buffer size, with special markers
16328 indicating narrowing, and scrollbars which reflect only the
16329 visible region.
16331 Note that mini-buffers sometimes aren't displaying any text. */
16332 if (!MINI_WINDOW_P (w)
16333 || (w == XWINDOW (minibuf_window)
16334 && NILP (echo_area_buffer[0])))
16336 struct buffer *buf = XBUFFER (w->contents);
16337 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16338 start = marker_position (w->start) - BUF_BEGV (buf);
16339 /* I don't think this is guaranteed to be right. For the
16340 moment, we'll pretend it is. */
16341 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16343 if (end < start)
16344 end = start;
16345 if (whole < (end - start))
16346 whole = end - start;
16348 else
16349 start = end = whole = 0;
16351 /* Indicate what this scroll bar ought to be displaying now. */
16352 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16353 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16354 (w, end - start, whole, start);
16358 void
16359 set_horizontal_scroll_bar (struct window *w)
16361 int start, end, whole, portion;
16363 if (!MINI_WINDOW_P (w)
16364 || (w == XWINDOW (minibuf_window)
16365 && NILP (echo_area_buffer[0])))
16367 struct buffer *b = XBUFFER (w->contents);
16368 struct buffer *old_buffer = NULL;
16369 struct it it;
16370 struct text_pos startp;
16372 if (b != current_buffer)
16374 old_buffer = current_buffer;
16375 set_buffer_internal (b);
16378 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16379 start_display (&it, w, startp);
16380 it.last_visible_x = INT_MAX;
16381 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16382 MOVE_TO_X | MOVE_TO_Y);
16383 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16384 window_box_height (w), -1,
16385 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16387 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16388 end = start + window_box_width (w, TEXT_AREA);
16389 portion = end - start;
16390 /* After enlarging a horizontally scrolled window such that it
16391 gets at least as wide as the text it contains, make sure that
16392 the thumb doesn't fill the entire scroll bar so we can still
16393 drag it back to see the entire text. */
16394 whole = max (whole, end);
16396 if (it.bidi_p)
16398 Lisp_Object pdir;
16400 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16401 if (EQ (pdir, Qright_to_left))
16403 start = whole - end;
16404 end = start + portion;
16408 if (old_buffer)
16409 set_buffer_internal (old_buffer);
16411 else
16412 start = end = whole = portion = 0;
16414 w->hscroll_whole = whole;
16416 /* Indicate what this scroll bar ought to be displaying now. */
16417 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16418 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16419 (w, portion, whole, start);
16423 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16424 selected_window is redisplayed.
16426 We can return without actually redisplaying the window if fonts has been
16427 changed on window's frame. In that case, redisplay_internal will retry.
16429 As one of the important parts of redisplaying a window, we need to
16430 decide whether the previous window-start position (stored in the
16431 window's w->start marker position) is still valid, and if it isn't,
16432 recompute it. Some details about that:
16434 . The previous window-start could be in a continuation line, in
16435 which case we need to recompute it when the window width
16436 changes. See compute_window_start_on_continuation_line and its
16437 call below.
16439 . The text that changed since last redisplay could include the
16440 previous window-start position. In that case, we try to salvage
16441 what we can from the current glyph matrix by calling
16442 try_scrolling, which see.
16444 . Some Emacs command could force us to use a specific window-start
16445 position by setting the window's force_start flag, or gently
16446 propose doing that by setting the window's optional_new_start
16447 flag. In these cases, we try using the specified start point if
16448 that succeeds (i.e. the window desired matrix is successfully
16449 recomputed, and point location is within the window). In case
16450 of optional_new_start, we first check if the specified start
16451 position is feasible, i.e. if it will allow point to be
16452 displayed in the window. If using the specified start point
16453 fails, e.g., if new fonts are needed to be loaded, we abort the
16454 redisplay cycle and leave it up to the next cycle to figure out
16455 things.
16457 . Note that the window's force_start flag is sometimes set by
16458 redisplay itself, when it decides that the previous window start
16459 point is fine and should be kept. Search for "goto force_start"
16460 below to see the details. Like the values of window-start
16461 specified outside of redisplay, these internally-deduced values
16462 are tested for feasibility, and ignored if found to be
16463 unfeasible.
16465 . Note that the function try_window, used to completely redisplay
16466 a window, accepts the window's start point as its argument.
16467 This is used several times in the redisplay code to control
16468 where the window start will be, according to user options such
16469 as scroll-conservatively, and also to ensure the screen line
16470 showing point will be fully (as opposed to partially) visible on
16471 display. */
16473 static void
16474 redisplay_window (Lisp_Object window, bool just_this_one_p)
16476 struct window *w = XWINDOW (window);
16477 struct frame *f = XFRAME (w->frame);
16478 struct buffer *buffer = XBUFFER (w->contents);
16479 struct buffer *old = current_buffer;
16480 struct text_pos lpoint, opoint, startp;
16481 bool update_mode_line;
16482 int tem;
16483 struct it it;
16484 /* Record it now because it's overwritten. */
16485 bool current_matrix_up_to_date_p = false;
16486 bool used_current_matrix_p = false;
16487 /* This is less strict than current_matrix_up_to_date_p.
16488 It indicates that the buffer contents and narrowing are unchanged. */
16489 bool buffer_unchanged_p = false;
16490 bool temp_scroll_step = false;
16491 ptrdiff_t count = SPECPDL_INDEX ();
16492 int rc;
16493 int centering_position = -1;
16494 bool last_line_misfit = false;
16495 ptrdiff_t beg_unchanged, end_unchanged;
16496 int frame_line_height, margin;
16497 bool use_desired_matrix;
16498 void *itdata = NULL;
16500 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16501 opoint = lpoint;
16503 #ifdef GLYPH_DEBUG
16504 *w->desired_matrix->method = 0;
16505 #endif
16507 if (!just_this_one_p
16508 && REDISPLAY_SOME_P ()
16509 && !w->redisplay
16510 && !w->update_mode_line
16511 && !f->face_change
16512 && !f->redisplay
16513 && !buffer->text->redisplay
16514 && BUF_PT (buffer) == w->last_point)
16515 return;
16517 /* Make sure that both W's markers are valid. */
16518 eassert (XMARKER (w->start)->buffer == buffer);
16519 eassert (XMARKER (w->pointm)->buffer == buffer);
16521 reconsider_clip_changes (w);
16522 frame_line_height = default_line_pixel_height (w);
16523 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16526 /* Has the mode line to be updated? */
16527 update_mode_line = (w->update_mode_line
16528 || update_mode_lines
16529 || buffer->clip_changed
16530 || buffer->prevent_redisplay_optimizations_p);
16532 if (!just_this_one_p)
16533 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16534 cleverly elsewhere. */
16535 w->must_be_updated_p = true;
16537 if (MINI_WINDOW_P (w))
16539 if (w == XWINDOW (echo_area_window)
16540 && !NILP (echo_area_buffer[0]))
16542 if (update_mode_line)
16543 /* We may have to update a tty frame's menu bar or a
16544 tool-bar. Example `M-x C-h C-h C-g'. */
16545 goto finish_menu_bars;
16546 else
16547 /* We've already displayed the echo area glyphs in this window. */
16548 goto finish_scroll_bars;
16550 else if ((w != XWINDOW (minibuf_window)
16551 || minibuf_level == 0)
16552 /* When buffer is nonempty, redisplay window normally. */
16553 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16554 /* Quail displays non-mini buffers in minibuffer window.
16555 In that case, redisplay the window normally. */
16556 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16558 /* W is a mini-buffer window, but it's not active, so clear
16559 it. */
16560 int yb = window_text_bottom_y (w);
16561 struct glyph_row *row;
16562 int y;
16564 for (y = 0, row = w->desired_matrix->rows;
16565 y < yb;
16566 y += row->height, ++row)
16567 blank_row (w, row, y);
16568 goto finish_scroll_bars;
16571 clear_glyph_matrix (w->desired_matrix);
16574 /* Otherwise set up data on this window; select its buffer and point
16575 value. */
16576 /* Really select the buffer, for the sake of buffer-local
16577 variables. */
16578 set_buffer_internal_1 (XBUFFER (w->contents));
16580 current_matrix_up_to_date_p
16581 = (w->window_end_valid
16582 && !current_buffer->clip_changed
16583 && !current_buffer->prevent_redisplay_optimizations_p
16584 && !window_outdated (w)
16585 && !hscrolling_current_line_p (w));
16587 beg_unchanged = BEG_UNCHANGED;
16588 end_unchanged = END_UNCHANGED;
16590 SET_TEXT_POS (opoint, PT, PT_BYTE);
16592 specbind (Qinhibit_point_motion_hooks, Qt);
16594 buffer_unchanged_p
16595 = (w->window_end_valid
16596 && !current_buffer->clip_changed
16597 && !window_outdated (w));
16599 /* When windows_or_buffers_changed is non-zero, we can't rely
16600 on the window end being valid, so set it to zero there. */
16601 if (windows_or_buffers_changed)
16603 /* If window starts on a continuation line, maybe adjust the
16604 window start in case the window's width changed. */
16605 if (XMARKER (w->start)->buffer == current_buffer)
16606 compute_window_start_on_continuation_line (w);
16608 w->window_end_valid = false;
16609 /* If so, we also can't rely on current matrix
16610 and should not fool try_cursor_movement below. */
16611 current_matrix_up_to_date_p = false;
16614 /* Some sanity checks. */
16615 CHECK_WINDOW_END (w);
16616 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16617 emacs_abort ();
16618 if (BYTEPOS (opoint) < CHARPOS (opoint))
16619 emacs_abort ();
16621 if (mode_line_update_needed (w))
16622 update_mode_line = true;
16624 /* Point refers normally to the selected window. For any other
16625 window, set up appropriate value. */
16626 if (!EQ (window, selected_window))
16628 ptrdiff_t new_pt = marker_position (w->pointm);
16629 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16631 if (new_pt < BEGV)
16633 new_pt = BEGV;
16634 new_pt_byte = BEGV_BYTE;
16635 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16637 else if (new_pt > (ZV - 1))
16639 new_pt = ZV;
16640 new_pt_byte = ZV_BYTE;
16641 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16644 /* We don't use SET_PT so that the point-motion hooks don't run. */
16645 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16648 /* If any of the character widths specified in the display table
16649 have changed, invalidate the width run cache. It's true that
16650 this may be a bit late to catch such changes, but the rest of
16651 redisplay goes (non-fatally) haywire when the display table is
16652 changed, so why should we worry about doing any better? */
16653 if (current_buffer->width_run_cache
16654 || (current_buffer->base_buffer
16655 && current_buffer->base_buffer->width_run_cache))
16657 struct Lisp_Char_Table *disptab = buffer_display_table ();
16659 if (! disptab_matches_widthtab
16660 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16662 struct buffer *buf = current_buffer;
16664 if (buf->base_buffer)
16665 buf = buf->base_buffer;
16666 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16667 recompute_width_table (current_buffer, disptab);
16671 /* If window-start is screwed up, choose a new one. */
16672 if (XMARKER (w->start)->buffer != current_buffer)
16673 goto recenter;
16675 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16677 /* If someone specified a new starting point but did not insist,
16678 check whether it can be used. */
16679 if ((w->optional_new_start || window_frozen_p (w))
16680 && CHARPOS (startp) >= BEGV
16681 && CHARPOS (startp) <= ZV)
16683 ptrdiff_t it_charpos;
16685 w->optional_new_start = false;
16686 start_display (&it, w, startp);
16687 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16688 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16689 /* Record IT's position now, since line_bottom_y might change
16690 that. */
16691 it_charpos = IT_CHARPOS (it);
16692 /* Make sure we set the force_start flag only if the cursor row
16693 will be fully visible. Otherwise, the code under force_start
16694 label below will try to move point back into view, which is
16695 not what the code which sets optional_new_start wants. */
16696 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16697 && !w->force_start)
16699 if (it_charpos == PT)
16700 w->force_start = true;
16701 /* IT may overshoot PT if text at PT is invisible. */
16702 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16703 w->force_start = true;
16704 #ifdef GLYPH_DEBUG
16705 if (w->force_start)
16707 if (window_frozen_p (w))
16708 debug_method_add (w, "set force_start from frozen window start");
16709 else
16710 debug_method_add (w, "set force_start from optional_new_start");
16712 #endif
16716 force_start:
16718 /* Handle case where place to start displaying has been specified,
16719 unless the specified location is outside the accessible range. */
16720 if (w->force_start)
16722 /* We set this later on if we have to adjust point. */
16723 int new_vpos = -1;
16725 w->force_start = false;
16726 w->vscroll = 0;
16727 w->window_end_valid = false;
16729 /* Forget any recorded base line for line number display. */
16730 if (!buffer_unchanged_p)
16731 w->base_line_number = 0;
16733 /* Redisplay the mode line. Select the buffer properly for that.
16734 Also, run the hook window-scroll-functions
16735 because we have scrolled. */
16736 /* Note, we do this after clearing force_start because
16737 if there's an error, it is better to forget about force_start
16738 than to get into an infinite loop calling the hook functions
16739 and having them get more errors. */
16740 if (!update_mode_line
16741 || ! NILP (Vwindow_scroll_functions))
16743 update_mode_line = true;
16744 w->update_mode_line = true;
16745 startp = run_window_scroll_functions (window, startp);
16748 if (CHARPOS (startp) < BEGV)
16749 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16750 else if (CHARPOS (startp) > ZV)
16751 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16753 /* Redisplay, then check if cursor has been set during the
16754 redisplay. Give up if new fonts were loaded. */
16755 /* We used to issue a CHECK_MARGINS argument to try_window here,
16756 but this causes scrolling to fail when point begins inside
16757 the scroll margin (bug#148) -- cyd */
16758 if (!try_window (window, startp, 0))
16760 w->force_start = true;
16761 clear_glyph_matrix (w->desired_matrix);
16762 goto need_larger_matrices;
16765 if (w->cursor.vpos < 0)
16767 /* If point does not appear, try to move point so it does
16768 appear. The desired matrix has been built above, so we
16769 can use it here. First see if point is in invisible
16770 text, and if so, move it to the first visible buffer
16771 position past that. */
16772 struct glyph_row *r = NULL;
16773 Lisp_Object invprop =
16774 get_char_property_and_overlay (make_number (PT), Qinvisible,
16775 Qnil, NULL);
16777 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16779 ptrdiff_t alt_pt;
16780 Lisp_Object invprop_end =
16781 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16782 Qnil, Qnil);
16784 if (NATNUMP (invprop_end))
16785 alt_pt = XFASTINT (invprop_end);
16786 else
16787 alt_pt = ZV;
16788 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16789 NULL, 0);
16791 if (r)
16792 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16793 else /* Give up and just move to the middle of the window. */
16794 new_vpos = window_box_height (w) / 2;
16797 if (!cursor_row_fully_visible_p (w, false, false))
16799 /* Point does appear, but on a line partly visible at end of window.
16800 Move it back to a fully-visible line. */
16801 new_vpos = window_box_height (w);
16802 /* But if window_box_height suggests a Y coordinate that is
16803 not less than we already have, that line will clearly not
16804 be fully visible, so give up and scroll the display.
16805 This can happen when the default face uses a font whose
16806 dimensions are different from the frame's default
16807 font. */
16808 if (new_vpos >= w->cursor.y)
16810 w->cursor.vpos = -1;
16811 clear_glyph_matrix (w->desired_matrix);
16812 goto try_to_scroll;
16815 else if (w->cursor.vpos >= 0)
16817 /* Some people insist on not letting point enter the scroll
16818 margin, even though this part handles windows that didn't
16819 scroll at all. */
16820 int pixel_margin = margin * frame_line_height;
16821 bool header_line = window_wants_header_line (w);
16823 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16824 below, which finds the row to move point to, advances by
16825 the Y coordinate of the _next_ row, see the definition of
16826 MATRIX_ROW_BOTTOM_Y. */
16827 if (w->cursor.vpos < margin + header_line)
16829 w->cursor.vpos = -1;
16830 clear_glyph_matrix (w->desired_matrix);
16831 goto try_to_scroll;
16833 else
16835 int window_height = window_box_height (w);
16837 if (header_line)
16838 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16839 if (w->cursor.y >= window_height - pixel_margin)
16841 w->cursor.vpos = -1;
16842 clear_glyph_matrix (w->desired_matrix);
16843 goto try_to_scroll;
16848 /* If we need to move point for either of the above reasons,
16849 now actually do it. */
16850 if (new_vpos >= 0)
16852 struct glyph_row *row;
16854 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16855 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16856 ++row;
16858 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16859 MATRIX_ROW_START_BYTEPOS (row));
16861 if (w != XWINDOW (selected_window))
16862 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16863 else if (current_buffer == old)
16864 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16866 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16868 /* Re-run pre-redisplay-function so it can update the region
16869 according to the new position of point. */
16870 /* Other than the cursor, w's redisplay is done so we can set its
16871 redisplay to false. Also the buffer's redisplay can be set to
16872 false, since propagate_buffer_redisplay should have already
16873 propagated its info to `w' anyway. */
16874 w->redisplay = false;
16875 XBUFFER (w->contents)->text->redisplay = false;
16876 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16878 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16879 || ((EQ (Vdisplay_line_numbers, Qrelative)
16880 || EQ (Vdisplay_line_numbers, Qvisual))
16881 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16883 /* Either pre-redisplay-function made changes (e.g. move
16884 the region), or we moved point in a window that is
16885 under display-line-numbers = relative mode. We need
16886 another round of redisplay. */
16887 clear_glyph_matrix (w->desired_matrix);
16888 if (!try_window (window, startp, 0))
16889 goto need_larger_matrices;
16892 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16894 clear_glyph_matrix (w->desired_matrix);
16895 goto try_to_scroll;
16898 #ifdef GLYPH_DEBUG
16899 debug_method_add (w, "forced window start");
16900 #endif
16901 goto done;
16904 /* Handle case where text has not changed, only point, and it has
16905 not moved off the frame, and we are not retrying after hscroll.
16906 (current_matrix_up_to_date_p is true when retrying.) */
16907 if (current_matrix_up_to_date_p
16908 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16909 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16911 switch (rc)
16913 case CURSOR_MOVEMENT_SUCCESS:
16914 used_current_matrix_p = true;
16915 goto done;
16917 case CURSOR_MOVEMENT_MUST_SCROLL:
16918 goto try_to_scroll;
16920 default:
16921 emacs_abort ();
16924 /* If current starting point was originally the beginning of a line
16925 but no longer is, find a new starting point. */
16926 else if (w->start_at_line_beg
16927 && !(CHARPOS (startp) <= BEGV
16928 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16930 #ifdef GLYPH_DEBUG
16931 debug_method_add (w, "recenter 1");
16932 #endif
16933 goto recenter;
16936 /* Try scrolling with try_window_id. Value is > 0 if update has
16937 been done, it is -1 if we know that the same window start will
16938 not work. It is 0 if unsuccessful for some other reason. */
16939 else if ((tem = try_window_id (w)) != 0)
16941 #ifdef GLYPH_DEBUG
16942 debug_method_add (w, "try_window_id %d", tem);
16943 #endif
16945 if (f->fonts_changed)
16946 goto need_larger_matrices;
16947 if (tem > 0)
16948 goto done;
16950 /* Otherwise try_window_id has returned -1 which means that we
16951 don't want the alternative below this comment to execute. */
16953 else if (CHARPOS (startp) >= BEGV
16954 && CHARPOS (startp) <= ZV
16955 && PT >= CHARPOS (startp)
16956 && (CHARPOS (startp) < ZV
16957 /* Avoid starting at end of buffer. */
16958 || CHARPOS (startp) == BEGV
16959 || !window_outdated (w)))
16961 int d1, d2, d5, d6;
16962 int rtop, rbot;
16964 /* If first window line is a continuation line, and window start
16965 is inside the modified region, but the first change is before
16966 current window start, we must select a new window start.
16968 However, if this is the result of a down-mouse event (e.g. by
16969 extending the mouse-drag-overlay), we don't want to select a
16970 new window start, since that would change the position under
16971 the mouse, resulting in an unwanted mouse-movement rather
16972 than a simple mouse-click. */
16973 if (!w->start_at_line_beg
16974 && NILP (do_mouse_tracking)
16975 && CHARPOS (startp) > BEGV
16976 && CHARPOS (startp) > BEG + beg_unchanged
16977 && CHARPOS (startp) <= Z - end_unchanged
16978 /* Even if w->start_at_line_beg is nil, a new window may
16979 start at a line_beg, since that's how set_buffer_window
16980 sets it. So, we need to check the return value of
16981 compute_window_start_on_continuation_line. (See also
16982 bug#197). */
16983 && XMARKER (w->start)->buffer == current_buffer
16984 && compute_window_start_on_continuation_line (w)
16985 /* It doesn't make sense to force the window start like we
16986 do at label force_start if it is already known that point
16987 will not be fully visible in the resulting window, because
16988 doing so will move point from its correct position
16989 instead of scrolling the window to bring point into view.
16990 See bug#9324. */
16991 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16992 /* A very tall row could need more than the window height,
16993 in which case we accept that it is partially visible. */
16994 && (rtop != 0) == (rbot != 0))
16996 w->force_start = true;
16997 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16998 #ifdef GLYPH_DEBUG
16999 debug_method_add (w, "recomputed window start in continuation line");
17000 #endif
17001 goto force_start;
17004 #ifdef GLYPH_DEBUG
17005 debug_method_add (w, "same window start");
17006 #endif
17008 /* Try to redisplay starting at same place as before.
17009 If point has not moved off frame, accept the results. */
17010 if (!current_matrix_up_to_date_p
17011 /* Don't use try_window_reusing_current_matrix in this case
17012 because a window scroll function can have changed the
17013 buffer. */
17014 || !NILP (Vwindow_scroll_functions)
17015 || MINI_WINDOW_P (w)
17016 || !(used_current_matrix_p
17017 = try_window_reusing_current_matrix (w)))
17019 IF_DEBUG (debug_method_add (w, "1"));
17020 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17021 /* -1 means we need to scroll.
17022 0 means we need new matrices, but fonts_changed
17023 is set in that case, so we will detect it below. */
17024 goto try_to_scroll;
17027 if (f->fonts_changed)
17028 goto need_larger_matrices;
17030 if (w->cursor.vpos >= 0)
17032 if (!just_this_one_p
17033 || current_buffer->clip_changed
17034 || BEG_UNCHANGED < CHARPOS (startp))
17035 /* Forget any recorded base line for line number display. */
17036 w->base_line_number = 0;
17038 if (!cursor_row_fully_visible_p (w, true, false))
17040 clear_glyph_matrix (w->desired_matrix);
17041 last_line_misfit = true;
17043 /* Drop through and scroll. */
17044 else
17045 goto done;
17047 else
17048 clear_glyph_matrix (w->desired_matrix);
17051 try_to_scroll:
17053 /* Redisplay the mode line. Select the buffer properly for that. */
17054 if (!update_mode_line)
17056 update_mode_line = true;
17057 w->update_mode_line = true;
17060 /* Try to scroll by specified few lines. */
17061 if ((scroll_conservatively
17062 || emacs_scroll_step
17063 || temp_scroll_step
17064 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17065 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17066 && CHARPOS (startp) >= BEGV
17067 && CHARPOS (startp) <= ZV)
17069 /* The function returns -1 if new fonts were loaded, 1 if
17070 successful, 0 if not successful. */
17071 int ss = try_scrolling (window, just_this_one_p,
17072 scroll_conservatively,
17073 emacs_scroll_step,
17074 temp_scroll_step, last_line_misfit);
17075 switch (ss)
17077 case SCROLLING_SUCCESS:
17078 goto done;
17080 case SCROLLING_NEED_LARGER_MATRICES:
17081 goto need_larger_matrices;
17083 case SCROLLING_FAILED:
17084 break;
17086 default:
17087 emacs_abort ();
17091 /* Finally, just choose a place to start which positions point
17092 according to user preferences. */
17094 recenter:
17096 #ifdef GLYPH_DEBUG
17097 debug_method_add (w, "recenter");
17098 #endif
17100 /* Forget any previously recorded base line for line number display. */
17101 if (!buffer_unchanged_p)
17102 w->base_line_number = 0;
17104 /* Determine the window start relative to point. */
17105 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17106 it.current_y = it.last_visible_y;
17107 if (centering_position < 0)
17109 ptrdiff_t margin_pos = CHARPOS (startp);
17110 Lisp_Object aggressive;
17111 bool scrolling_up;
17113 /* If there is a scroll margin at the top of the window, find
17114 its character position. */
17115 if (margin
17116 /* Cannot call start_display if startp is not in the
17117 accessible region of the buffer. This can happen when we
17118 have just switched to a different buffer and/or changed
17119 its restriction. In that case, startp is initialized to
17120 the character position 1 (BEGV) because we did not yet
17121 have chance to display the buffer even once. */
17122 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17124 struct it it1;
17125 void *it1data = NULL;
17127 SAVE_IT (it1, it, it1data);
17128 start_display (&it1, w, startp);
17129 move_it_vertically (&it1, margin * frame_line_height);
17130 margin_pos = IT_CHARPOS (it1);
17131 RESTORE_IT (&it, &it, it1data);
17133 scrolling_up = PT > margin_pos;
17134 aggressive =
17135 scrolling_up
17136 ? BVAR (current_buffer, scroll_up_aggressively)
17137 : BVAR (current_buffer, scroll_down_aggressively);
17139 if (!MINI_WINDOW_P (w)
17140 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17142 int pt_offset = 0;
17144 /* Setting scroll-conservatively overrides
17145 scroll-*-aggressively. */
17146 if (!scroll_conservatively && NUMBERP (aggressive))
17148 double float_amount = XFLOATINT (aggressive);
17150 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17151 if (pt_offset == 0 && float_amount > 0)
17152 pt_offset = 1;
17153 if (pt_offset && margin > 0)
17154 margin -= 1;
17156 /* Compute how much to move the window start backward from
17157 point so that point will be displayed where the user
17158 wants it. */
17159 if (scrolling_up)
17161 centering_position = it.last_visible_y;
17162 if (pt_offset)
17163 centering_position -= pt_offset;
17164 centering_position -=
17165 (frame_line_height * (1 + margin + last_line_misfit)
17166 + WINDOW_HEADER_LINE_HEIGHT (w));
17167 /* Don't let point enter the scroll margin near top of
17168 the window. */
17169 if (centering_position < margin * frame_line_height)
17170 centering_position = margin * frame_line_height;
17172 else
17173 centering_position = margin * frame_line_height + pt_offset;
17175 else
17176 /* Set the window start half the height of the window backward
17177 from point. */
17178 centering_position = window_box_height (w) / 2;
17180 move_it_vertically_backward (&it, centering_position);
17182 eassert (IT_CHARPOS (it) >= BEGV);
17184 /* The function move_it_vertically_backward may move over more
17185 than the specified y-distance. If it->w is small, e.g. a
17186 mini-buffer window, we may end up in front of the window's
17187 display area. Start displaying at the start of the line
17188 containing PT in this case. */
17189 if (it.current_y <= 0)
17191 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17192 move_it_vertically_backward (&it, 0);
17193 it.current_y = 0;
17196 it.current_x = it.hpos = 0;
17198 /* Set the window start position here explicitly, to avoid an
17199 infinite loop in case the functions in window-scroll-functions
17200 get errors. */
17201 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17203 /* Run scroll hooks. */
17204 startp = run_window_scroll_functions (window, it.current.pos);
17206 /* We invoke try_window and try_window_reusing_current_matrix below,
17207 and they manipulate the bidi cache. Save and restore the cache
17208 state of our iterator, so we could continue using it after that. */
17209 itdata = bidi_shelve_cache ();
17211 /* Redisplay the window. */
17212 use_desired_matrix = false;
17213 if (!current_matrix_up_to_date_p
17214 || windows_or_buffers_changed
17215 || f->cursor_type_changed
17216 /* Don't use try_window_reusing_current_matrix in this case
17217 because it can have changed the buffer. */
17218 || !NILP (Vwindow_scroll_functions)
17219 || !just_this_one_p
17220 || MINI_WINDOW_P (w)
17221 || !(used_current_matrix_p
17222 = try_window_reusing_current_matrix (w)))
17223 use_desired_matrix = (try_window (window, startp, 0) == 1);
17225 bidi_unshelve_cache (itdata, false);
17227 /* If new fonts have been loaded (due to fontsets), give up. We
17228 have to start a new redisplay since we need to re-adjust glyph
17229 matrices. */
17230 if (f->fonts_changed)
17231 goto need_larger_matrices;
17233 /* If cursor did not appear assume that the middle of the window is
17234 in the first line of the window. Do it again with the next line.
17235 (Imagine a window of height 100, displaying two lines of height
17236 60. Moving back 50 from it->last_visible_y will end in the first
17237 line.) */
17238 if (w->cursor.vpos < 0)
17240 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17242 clear_glyph_matrix (w->desired_matrix);
17243 move_it_by_lines (&it, 1);
17244 try_window (window, it.current.pos, 0);
17246 else if (PT < IT_CHARPOS (it))
17248 clear_glyph_matrix (w->desired_matrix);
17249 move_it_by_lines (&it, -1);
17250 try_window (window, it.current.pos, 0);
17252 else if (scroll_conservatively > SCROLL_LIMIT
17253 && (it.method == GET_FROM_STRING
17254 || overlay_touches_p (IT_CHARPOS (it)))
17255 && IT_CHARPOS (it) < ZV)
17257 /* If the window starts with a before-string that spans more
17258 than one screen line, using that position to display the
17259 window might fail to bring point into the view, because
17260 start_display will always start by displaying the string,
17261 whereas the code above determines where to set w->start
17262 by the buffer position of the place where it takes screen
17263 coordinates. Try to recover by finding the next screen
17264 line that displays buffer text. */
17265 ptrdiff_t pos0 = IT_CHARPOS (it);
17267 clear_glyph_matrix (w->desired_matrix);
17268 do {
17269 move_it_by_lines (&it, 1);
17270 } while (IT_CHARPOS (it) == pos0);
17271 try_window (window, it.current.pos, 0);
17273 else
17275 /* Not much we can do about it. */
17279 /* Consider the following case: Window starts at BEGV, there is
17280 invisible, intangible text at BEGV, so that display starts at
17281 some point START > BEGV. It can happen that we are called with
17282 PT somewhere between BEGV and START. Try to handle that case,
17283 and similar ones. */
17284 if (w->cursor.vpos < 0)
17286 /* Prefer the desired matrix to the current matrix, if possible,
17287 in the fallback calculations below. This is because using
17288 the current matrix might completely goof, e.g. if its first
17289 row is after point. */
17290 struct glyph_matrix *matrix =
17291 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17292 /* First, try locating the proper glyph row for PT. */
17293 struct glyph_row *row =
17294 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17296 /* Sometimes point is at the beginning of invisible text that is
17297 before the 1st character displayed in the row. In that case,
17298 row_containing_pos fails to find the row, because no glyphs
17299 with appropriate buffer positions are present in the row.
17300 Therefore, we next try to find the row which shows the 1st
17301 position after the invisible text. */
17302 if (!row)
17304 Lisp_Object val =
17305 get_char_property_and_overlay (make_number (PT), Qinvisible,
17306 Qnil, NULL);
17308 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17310 ptrdiff_t alt_pos;
17311 Lisp_Object invis_end =
17312 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17313 Qnil, Qnil);
17315 if (NATNUMP (invis_end))
17316 alt_pos = XFASTINT (invis_end);
17317 else
17318 alt_pos = ZV;
17319 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17322 /* Finally, fall back on the first row of the window after the
17323 header line (if any). This is slightly better than not
17324 displaying the cursor at all. */
17325 if (!row)
17327 row = matrix->rows;
17328 if (row->mode_line_p)
17329 ++row;
17331 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17334 if (!cursor_row_fully_visible_p (w, false, false))
17336 /* If vscroll is enabled, disable it and try again. */
17337 if (w->vscroll)
17339 w->vscroll = 0;
17340 clear_glyph_matrix (w->desired_matrix);
17341 goto recenter;
17344 /* Users who set scroll-conservatively to a large number want
17345 point just above/below the scroll margin. If we ended up
17346 with point's row partially visible, move the window start to
17347 make that row fully visible and out of the margin. */
17348 if (scroll_conservatively > SCROLL_LIMIT)
17350 int window_total_lines
17351 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17352 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17354 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17355 clear_glyph_matrix (w->desired_matrix);
17356 if (1 == try_window (window, it.current.pos,
17357 TRY_WINDOW_CHECK_MARGINS))
17358 goto done;
17361 /* If centering point failed to make the whole line visible,
17362 put point at the top instead. That has to make the whole line
17363 visible, if it can be done. */
17364 if (centering_position == 0)
17365 goto done;
17367 clear_glyph_matrix (w->desired_matrix);
17368 centering_position = 0;
17369 goto recenter;
17372 done:
17374 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17375 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17376 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17378 /* Display the mode line, if we must. */
17379 if ((update_mode_line
17380 /* If window not full width, must redo its mode line
17381 if (a) the window to its side is being redone and
17382 (b) we do a frame-based redisplay. This is a consequence
17383 of how inverted lines are drawn in frame-based redisplay. */
17384 || (!just_this_one_p
17385 && !FRAME_WINDOW_P (f)
17386 && !WINDOW_FULL_WIDTH_P (w))
17387 /* Line number to display. */
17388 || w->base_line_pos > 0
17389 /* Column number is displayed and different from the one displayed. */
17390 || (w->column_number_displayed != -1
17391 && (w->column_number_displayed != current_column ())))
17392 /* This means that the window has a mode line. */
17393 && (window_wants_mode_line (w)
17394 || window_wants_header_line (w)))
17397 display_mode_lines (w);
17399 /* If mode line height has changed, arrange for a thorough
17400 immediate redisplay using the correct mode line height. */
17401 if (window_wants_mode_line (w)
17402 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17404 f->fonts_changed = true;
17405 w->mode_line_height = -1;
17406 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17407 = DESIRED_MODE_LINE_HEIGHT (w);
17410 /* If header line height has changed, arrange for a thorough
17411 immediate redisplay using the correct header line height. */
17412 if (window_wants_header_line (w)
17413 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17415 f->fonts_changed = true;
17416 w->header_line_height = -1;
17417 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17418 = DESIRED_HEADER_LINE_HEIGHT (w);
17421 if (f->fonts_changed)
17422 goto need_larger_matrices;
17425 if (!line_number_displayed && w->base_line_pos != -1)
17427 w->base_line_pos = 0;
17428 w->base_line_number = 0;
17431 finish_menu_bars:
17433 /* When we reach a frame's selected window, redo the frame's menu
17434 bar and the frame's title. */
17435 if (update_mode_line
17436 && EQ (FRAME_SELECTED_WINDOW (f), window))
17438 bool redisplay_menu_p;
17440 if (FRAME_WINDOW_P (f))
17442 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17443 || defined (HAVE_NS) || defined (USE_GTK)
17444 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17445 #else
17446 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17447 #endif
17449 else
17450 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17452 if (redisplay_menu_p)
17453 display_menu_bar (w);
17455 #ifdef HAVE_WINDOW_SYSTEM
17456 if (FRAME_WINDOW_P (f))
17458 #if defined (USE_GTK) || defined (HAVE_NS)
17459 if (FRAME_EXTERNAL_TOOL_BAR (f))
17460 redisplay_tool_bar (f);
17461 #else
17462 if (WINDOWP (f->tool_bar_window)
17463 && (FRAME_TOOL_BAR_LINES (f) > 0
17464 || !NILP (Vauto_resize_tool_bars))
17465 && redisplay_tool_bar (f))
17466 ignore_mouse_drag_p = true;
17467 #endif
17469 x_consider_frame_title (w->frame);
17470 #endif
17473 #ifdef HAVE_WINDOW_SYSTEM
17474 if (FRAME_WINDOW_P (f)
17475 && update_window_fringes (w, (just_this_one_p
17476 || (!used_current_matrix_p && !overlay_arrow_seen)
17477 || w->pseudo_window_p)))
17479 update_begin (f);
17480 block_input ();
17481 if (draw_window_fringes (w, true))
17483 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17484 x_draw_right_divider (w);
17485 else
17486 x_draw_vertical_border (w);
17488 unblock_input ();
17489 update_end (f);
17492 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17493 x_draw_bottom_divider (w);
17494 #endif /* HAVE_WINDOW_SYSTEM */
17496 /* We go to this label, with fonts_changed set, if it is
17497 necessary to try again using larger glyph matrices.
17498 We have to redeem the scroll bar even in this case,
17499 because the loop in redisplay_internal expects that. */
17500 need_larger_matrices:
17502 finish_scroll_bars:
17504 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17506 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17507 /* Set the thumb's position and size. */
17508 set_vertical_scroll_bar (w);
17510 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17511 /* Set the thumb's position and size. */
17512 set_horizontal_scroll_bar (w);
17514 /* Note that we actually used the scroll bar attached to this
17515 window, so it shouldn't be deleted at the end of redisplay. */
17516 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17517 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17520 /* Restore current_buffer and value of point in it. The window
17521 update may have changed the buffer, so first make sure `opoint'
17522 is still valid (Bug#6177). */
17523 if (CHARPOS (opoint) < BEGV)
17524 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17525 else if (CHARPOS (opoint) > ZV)
17526 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17527 else
17528 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17530 set_buffer_internal_1 (old);
17531 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17532 shorter. This can be caused by log truncation in *Messages*. */
17533 if (CHARPOS (lpoint) <= ZV)
17534 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17536 unbind_to (count, Qnil);
17540 /* Build the complete desired matrix of WINDOW with a window start
17541 buffer position POS.
17543 Value is 1 if successful. It is zero if fonts were loaded during
17544 redisplay which makes re-adjusting glyph matrices necessary, and -1
17545 if point would appear in the scroll margins.
17546 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17547 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17548 set in FLAGS.) */
17551 try_window (Lisp_Object window, struct text_pos pos, int flags)
17553 struct window *w = XWINDOW (window);
17554 struct it it;
17555 struct glyph_row *last_text_row = NULL;
17556 struct frame *f = XFRAME (w->frame);
17557 int cursor_vpos = w->cursor.vpos;
17559 /* Make POS the new window start. */
17560 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17562 /* Mark cursor position as unknown. No overlay arrow seen. */
17563 w->cursor.vpos = -1;
17564 overlay_arrow_seen = false;
17566 /* Initialize iterator and info to start at POS. */
17567 start_display (&it, w, pos);
17568 it.glyph_row->reversed_p = false;
17570 /* Display all lines of W. */
17571 while (it.current_y < it.last_visible_y)
17573 if (display_line (&it, cursor_vpos))
17574 last_text_row = it.glyph_row - 1;
17575 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17576 return 0;
17579 /* Save the character position of 'it' before we call
17580 'start_display' again. */
17581 ptrdiff_t it_charpos = IT_CHARPOS (it);
17583 /* Don't let the cursor end in the scroll margins. */
17584 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17585 && !MINI_WINDOW_P (w))
17587 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17588 start_display (&it, w, pos);
17590 if ((w->cursor.y >= 0 /* not vscrolled */
17591 && w->cursor.y < this_scroll_margin
17592 && CHARPOS (pos) > BEGV
17593 && it_charpos < ZV)
17594 /* rms: considering make_cursor_line_fully_visible_p here
17595 seems to give wrong results. We don't want to recenter
17596 when the last line is partly visible, we want to allow
17597 that case to be handled in the usual way. */
17598 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17599 - this_scroll_margin - 1))
17601 w->cursor.vpos = -1;
17602 clear_glyph_matrix (w->desired_matrix);
17603 return -1;
17607 /* If bottom moved off end of frame, change mode line percentage. */
17608 if (w->window_end_pos <= 0 && Z != it_charpos)
17609 w->update_mode_line = true;
17611 /* Set window_end_pos to the offset of the last character displayed
17612 on the window from the end of current_buffer. Set
17613 window_end_vpos to its row number. */
17614 if (last_text_row)
17616 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17617 adjust_window_ends (w, last_text_row, false);
17618 eassert
17619 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17620 w->window_end_vpos)));
17622 else
17624 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17625 w->window_end_pos = Z - ZV;
17626 w->window_end_vpos = 0;
17629 /* But that is not valid info until redisplay finishes. */
17630 w->window_end_valid = false;
17631 return 1;
17636 /************************************************************************
17637 Window redisplay reusing current matrix when buffer has not changed
17638 ************************************************************************/
17640 /* Try redisplay of window W showing an unchanged buffer with a
17641 different window start than the last time it was displayed by
17642 reusing its current matrix. Value is true if successful.
17643 W->start is the new window start. */
17645 static bool
17646 try_window_reusing_current_matrix (struct window *w)
17648 struct frame *f = XFRAME (w->frame);
17649 struct glyph_row *bottom_row;
17650 struct it it;
17651 struct run run;
17652 struct text_pos start, new_start;
17653 int nrows_scrolled, i;
17654 struct glyph_row *last_text_row;
17655 struct glyph_row *last_reused_text_row;
17656 struct glyph_row *start_row;
17657 int start_vpos, min_y, max_y;
17659 #ifdef GLYPH_DEBUG
17660 if (inhibit_try_window_reusing)
17661 return false;
17662 #endif
17664 if (/* This function doesn't handle terminal frames. */
17665 !FRAME_WINDOW_P (f)
17666 /* Don't try to reuse the display if windows have been split
17667 or such. */
17668 || windows_or_buffers_changed
17669 || f->cursor_type_changed)
17670 return false;
17672 /* Can't do this if showing trailing whitespace. */
17673 if (!NILP (Vshow_trailing_whitespace))
17674 return false;
17676 /* If top-line visibility has changed, give up. */
17677 if (window_wants_header_line (w)
17678 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17679 return false;
17681 /* Give up if old or new display is scrolled vertically. We could
17682 make this function handle this, but right now it doesn't. */
17683 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17684 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17685 return false;
17687 /* Clear the desired matrix for the display below. */
17688 clear_glyph_matrix (w->desired_matrix);
17690 /* Give up if line numbers are being displayed, because reusing the
17691 current matrix might use the wrong width for line-number
17692 display. */
17693 if (!NILP (Vdisplay_line_numbers))
17694 return false;
17696 /* The variable new_start now holds the new window start. The old
17697 start `start' can be determined from the current matrix. */
17698 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17699 start = start_row->minpos;
17700 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17702 if (CHARPOS (new_start) <= CHARPOS (start))
17704 /* Don't use this method if the display starts with an ellipsis
17705 displayed for invisible text. It's not easy to handle that case
17706 below, and it's certainly not worth the effort since this is
17707 not a frequent case. */
17708 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17709 return false;
17711 IF_DEBUG (debug_method_add (w, "twu1"));
17713 /* Display up to a row that can be reused. The variable
17714 last_text_row is set to the last row displayed that displays
17715 text. Note that it.vpos == 0 if or if not there is a
17716 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17717 start_display (&it, w, new_start);
17718 w->cursor.vpos = -1;
17719 last_text_row = last_reused_text_row = NULL;
17721 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17723 /* If we have reached into the characters in the START row,
17724 that means the line boundaries have changed. So we
17725 can't start copying with the row START. Maybe it will
17726 work to start copying with the following row. */
17727 while (IT_CHARPOS (it) > CHARPOS (start))
17729 /* Advance to the next row as the "start". */
17730 start_row++;
17731 start = start_row->minpos;
17732 /* If there are no more rows to try, or just one, give up. */
17733 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17734 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17735 || CHARPOS (start) == ZV)
17737 clear_glyph_matrix (w->desired_matrix);
17738 return false;
17741 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17743 /* If we have reached alignment, we can copy the rest of the
17744 rows. */
17745 if (IT_CHARPOS (it) == CHARPOS (start)
17746 /* Don't accept "alignment" inside a display vector,
17747 since start_row could have started in the middle of
17748 that same display vector (thus their character
17749 positions match), and we have no way of telling if
17750 that is the case. */
17751 && it.current.dpvec_index < 0)
17752 break;
17754 it.glyph_row->reversed_p = false;
17755 if (display_line (&it, -1))
17756 last_text_row = it.glyph_row - 1;
17760 /* A value of current_y < last_visible_y means that we stopped
17761 at the previous window start, which in turn means that we
17762 have at least one reusable row. */
17763 if (it.current_y < it.last_visible_y)
17765 struct glyph_row *row;
17767 /* IT.vpos always starts from 0; it counts text lines. */
17768 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17770 /* Find PT if not already found in the lines displayed. */
17771 if (w->cursor.vpos < 0)
17773 int dy = it.current_y - start_row->y;
17775 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17776 row = row_containing_pos (w, PT, row, NULL, dy);
17777 if (row)
17778 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17779 dy, nrows_scrolled);
17780 else
17782 clear_glyph_matrix (w->desired_matrix);
17783 return false;
17787 /* Scroll the display. Do it before the current matrix is
17788 changed. The problem here is that update has not yet
17789 run, i.e. part of the current matrix is not up to date.
17790 scroll_run_hook will clear the cursor, and use the
17791 current matrix to get the height of the row the cursor is
17792 in. */
17793 run.current_y = start_row->y;
17794 run.desired_y = it.current_y;
17795 run.height = it.last_visible_y - it.current_y;
17797 if (run.height > 0 && run.current_y != run.desired_y)
17799 update_begin (f);
17800 FRAME_RIF (f)->update_window_begin_hook (w);
17801 FRAME_RIF (f)->clear_window_mouse_face (w);
17802 FRAME_RIF (f)->scroll_run_hook (w, &run);
17803 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17804 update_end (f);
17807 /* Shift current matrix down by nrows_scrolled lines. */
17808 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17809 rotate_matrix (w->current_matrix,
17810 start_vpos,
17811 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17812 nrows_scrolled);
17814 /* Disable lines that must be updated. */
17815 for (i = 0; i < nrows_scrolled; ++i)
17816 (start_row + i)->enabled_p = false;
17818 /* Re-compute Y positions. */
17819 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17820 max_y = it.last_visible_y;
17821 for (row = start_row + nrows_scrolled;
17822 row < bottom_row;
17823 ++row)
17825 row->y = it.current_y;
17826 row->visible_height = row->height;
17828 if (row->y < min_y)
17829 row->visible_height -= min_y - row->y;
17830 if (row->y + row->height > max_y)
17831 row->visible_height -= row->y + row->height - max_y;
17832 if (row->fringe_bitmap_periodic_p)
17833 row->redraw_fringe_bitmaps_p = true;
17835 it.current_y += row->height;
17837 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17838 last_reused_text_row = row;
17839 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17840 break;
17843 /* Disable lines in the current matrix which are now
17844 below the window. */
17845 for (++row; row < bottom_row; ++row)
17846 row->enabled_p = row->mode_line_p = false;
17849 /* Update window_end_pos etc.; last_reused_text_row is the last
17850 reused row from the current matrix containing text, if any.
17851 The value of last_text_row is the last displayed line
17852 containing text. */
17853 if (last_reused_text_row)
17854 adjust_window_ends (w, last_reused_text_row, true);
17855 else if (last_text_row)
17856 adjust_window_ends (w, last_text_row, false);
17857 else
17859 /* This window must be completely empty. */
17860 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17861 w->window_end_pos = Z - ZV;
17862 w->window_end_vpos = 0;
17864 w->window_end_valid = false;
17866 /* Update hint: don't try scrolling again in update_window. */
17867 w->desired_matrix->no_scrolling_p = true;
17869 #ifdef GLYPH_DEBUG
17870 debug_method_add (w, "try_window_reusing_current_matrix 1");
17871 #endif
17872 return true;
17874 else if (CHARPOS (new_start) > CHARPOS (start))
17876 struct glyph_row *pt_row, *row;
17877 struct glyph_row *first_reusable_row;
17878 struct glyph_row *first_row_to_display;
17879 int dy;
17880 int yb = window_text_bottom_y (w);
17882 /* Find the row starting at new_start, if there is one. Don't
17883 reuse a partially visible line at the end. */
17884 first_reusable_row = start_row;
17885 while (first_reusable_row->enabled_p
17886 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17887 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17888 < CHARPOS (new_start)))
17889 ++first_reusable_row;
17891 /* Give up if there is no row to reuse. */
17892 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17893 || !first_reusable_row->enabled_p
17894 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17895 != CHARPOS (new_start)))
17896 return false;
17898 /* We can reuse fully visible rows beginning with
17899 first_reusable_row to the end of the window. Set
17900 first_row_to_display to the first row that cannot be reused.
17901 Set pt_row to the row containing point, if there is any. */
17902 pt_row = NULL;
17903 for (first_row_to_display = first_reusable_row;
17904 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17905 ++first_row_to_display)
17907 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17908 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17909 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17910 && first_row_to_display->ends_at_zv_p
17911 && pt_row == NULL)))
17912 pt_row = first_row_to_display;
17915 /* Start displaying at the start of first_row_to_display. */
17916 eassert (first_row_to_display->y < yb);
17917 init_to_row_start (&it, w, first_row_to_display);
17919 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17920 - start_vpos);
17921 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17922 - nrows_scrolled);
17923 it.current_y = (first_row_to_display->y - first_reusable_row->y
17924 + WINDOW_HEADER_LINE_HEIGHT (w));
17926 /* Display lines beginning with first_row_to_display in the
17927 desired matrix. Set last_text_row to the last row displayed
17928 that displays text. */
17929 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17930 if (pt_row == NULL)
17931 w->cursor.vpos = -1;
17932 last_text_row = NULL;
17933 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17934 if (display_line (&it, w->cursor.vpos))
17935 last_text_row = it.glyph_row - 1;
17937 /* If point is in a reused row, adjust y and vpos of the cursor
17938 position. */
17939 if (pt_row)
17941 w->cursor.vpos -= nrows_scrolled;
17942 w->cursor.y -= first_reusable_row->y - start_row->y;
17945 /* Give up if point isn't in a row displayed or reused. (This
17946 also handles the case where w->cursor.vpos < nrows_scrolled
17947 after the calls to display_line, which can happen with scroll
17948 margins. See bug#1295.) */
17949 if (w->cursor.vpos < 0)
17951 clear_glyph_matrix (w->desired_matrix);
17952 return false;
17955 /* Scroll the display. */
17956 run.current_y = first_reusable_row->y;
17957 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17958 run.height = it.last_visible_y - run.current_y;
17959 dy = run.current_y - run.desired_y;
17961 if (run.height)
17963 update_begin (f);
17964 FRAME_RIF (f)->update_window_begin_hook (w);
17965 FRAME_RIF (f)->clear_window_mouse_face (w);
17966 FRAME_RIF (f)->scroll_run_hook (w, &run);
17967 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17968 update_end (f);
17971 /* Adjust Y positions of reused rows. */
17972 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17973 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17974 max_y = it.last_visible_y;
17975 for (row = first_reusable_row; row < first_row_to_display; ++row)
17977 row->y -= dy;
17978 row->visible_height = row->height;
17979 if (row->y < min_y)
17980 row->visible_height -= min_y - row->y;
17981 if (row->y + row->height > max_y)
17982 row->visible_height -= row->y + row->height - max_y;
17983 if (row->fringe_bitmap_periodic_p)
17984 row->redraw_fringe_bitmaps_p = true;
17987 /* Scroll the current matrix. */
17988 eassert (nrows_scrolled > 0);
17989 rotate_matrix (w->current_matrix,
17990 start_vpos,
17991 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17992 -nrows_scrolled);
17994 /* Disable rows not reused. */
17995 for (row -= nrows_scrolled; row < bottom_row; ++row)
17996 row->enabled_p = false;
17998 /* Point may have moved to a different line, so we cannot assume that
17999 the previous cursor position is valid; locate the correct row. */
18000 if (pt_row)
18002 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18003 row < bottom_row
18004 && PT >= MATRIX_ROW_END_CHARPOS (row)
18005 && !row->ends_at_zv_p;
18006 row++)
18008 w->cursor.vpos++;
18009 w->cursor.y = row->y;
18011 if (row < bottom_row)
18013 /* Can't simply scan the row for point with
18014 bidi-reordered glyph rows. Let set_cursor_from_row
18015 figure out where to put the cursor, and if it fails,
18016 give up. */
18017 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18019 if (!set_cursor_from_row (w, row, w->current_matrix,
18020 0, 0, 0, 0))
18022 clear_glyph_matrix (w->desired_matrix);
18023 return false;
18026 else
18028 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18029 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18031 for (; glyph < end
18032 && (!BUFFERP (glyph->object)
18033 || glyph->charpos < PT);
18034 glyph++)
18036 w->cursor.hpos++;
18037 w->cursor.x += glyph->pixel_width;
18043 /* Adjust window end. A null value of last_text_row means that
18044 the window end is in reused rows which in turn means that
18045 only its vpos can have changed. */
18046 if (last_text_row)
18047 adjust_window_ends (w, last_text_row, false);
18048 else
18049 w->window_end_vpos -= nrows_scrolled;
18051 w->window_end_valid = false;
18052 w->desired_matrix->no_scrolling_p = true;
18054 #ifdef GLYPH_DEBUG
18055 debug_method_add (w, "try_window_reusing_current_matrix 2");
18056 #endif
18057 return true;
18060 return false;
18065 /************************************************************************
18066 Window redisplay reusing current matrix when buffer has changed
18067 ************************************************************************/
18069 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18070 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18071 ptrdiff_t *, ptrdiff_t *);
18072 static struct glyph_row *
18073 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18074 struct glyph_row *);
18077 /* Return the last row in MATRIX displaying text. If row START is
18078 non-null, start searching with that row. IT gives the dimensions
18079 of the display. Value is null if matrix is empty; otherwise it is
18080 a pointer to the row found. */
18082 static struct glyph_row *
18083 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18084 struct glyph_row *start)
18086 struct glyph_row *row, *row_found;
18088 /* Set row_found to the last row in IT->w's current matrix
18089 displaying text. The loop looks funny but think of partially
18090 visible lines. */
18091 row_found = NULL;
18092 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18093 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18095 eassert (row->enabled_p);
18096 row_found = row;
18097 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18098 break;
18099 ++row;
18102 return row_found;
18106 /* Return the last row in the current matrix of W that is not affected
18107 by changes at the start of current_buffer that occurred since W's
18108 current matrix was built. Value is null if no such row exists.
18110 BEG_UNCHANGED us the number of characters unchanged at the start of
18111 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18112 first changed character in current_buffer. Characters at positions <
18113 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18114 when the current matrix was built. */
18116 static struct glyph_row *
18117 find_last_unchanged_at_beg_row (struct window *w)
18119 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18120 struct glyph_row *row;
18121 struct glyph_row *row_found = NULL;
18122 int yb = window_text_bottom_y (w);
18124 /* Find the last row displaying unchanged text. */
18125 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18126 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18127 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18128 ++row)
18130 if (/* If row ends before first_changed_pos, it is unchanged,
18131 except in some case. */
18132 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18133 /* When row ends in ZV and we write at ZV it is not
18134 unchanged. */
18135 && !row->ends_at_zv_p
18136 /* When first_changed_pos is the end of a continued line,
18137 row is not unchanged because it may be no longer
18138 continued. */
18139 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18140 && (row->continued_p
18141 || row->exact_window_width_line_p))
18142 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18143 needs to be recomputed, so don't consider this row as
18144 unchanged. This happens when the last line was
18145 bidi-reordered and was killed immediately before this
18146 redisplay cycle. In that case, ROW->end stores the
18147 buffer position of the first visual-order character of
18148 the killed text, which is now beyond ZV. */
18149 && CHARPOS (row->end.pos) <= ZV)
18150 row_found = row;
18152 /* Stop if last visible row. */
18153 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18154 break;
18157 return row_found;
18161 /* Find the first glyph row in the current matrix of W that is not
18162 affected by changes at the end of current_buffer since the
18163 time W's current matrix was built.
18165 Return in *DELTA the number of chars by which buffer positions in
18166 unchanged text at the end of current_buffer must be adjusted.
18168 Return in *DELTA_BYTES the corresponding number of bytes.
18170 Value is null if no such row exists, i.e. all rows are affected by
18171 changes. */
18173 static struct glyph_row *
18174 find_first_unchanged_at_end_row (struct window *w,
18175 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18177 struct glyph_row *row;
18178 struct glyph_row *row_found = NULL;
18180 *delta = *delta_bytes = 0;
18182 /* Display must not have been paused, otherwise the current matrix
18183 is not up to date. */
18184 eassert (w->window_end_valid);
18186 /* A value of window_end_pos >= END_UNCHANGED means that the window
18187 end is in the range of changed text. If so, there is no
18188 unchanged row at the end of W's current matrix. */
18189 if (w->window_end_pos >= END_UNCHANGED)
18190 return NULL;
18192 /* Set row to the last row in W's current matrix displaying text. */
18193 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18195 /* If matrix is entirely empty, no unchanged row exists. */
18196 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18198 /* The value of row is the last glyph row in the matrix having a
18199 meaningful buffer position in it. The end position of row
18200 corresponds to window_end_pos. This allows us to translate
18201 buffer positions in the current matrix to current buffer
18202 positions for characters not in changed text. */
18203 ptrdiff_t Z_old =
18204 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18205 ptrdiff_t Z_BYTE_old =
18206 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18207 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18208 struct glyph_row *first_text_row
18209 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18211 *delta = Z - Z_old;
18212 *delta_bytes = Z_BYTE - Z_BYTE_old;
18214 /* Set last_unchanged_pos to the buffer position of the last
18215 character in the buffer that has not been changed. Z is the
18216 index + 1 of the last character in current_buffer, i.e. by
18217 subtracting END_UNCHANGED we get the index of the last
18218 unchanged character, and we have to add BEG to get its buffer
18219 position. */
18220 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18221 last_unchanged_pos_old = last_unchanged_pos - *delta;
18223 /* Search backward from ROW for a row displaying a line that
18224 starts at a minimum position >= last_unchanged_pos_old. */
18225 for (; row > first_text_row; --row)
18227 /* This used to abort, but it can happen.
18228 It is ok to just stop the search instead here. KFS. */
18229 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18230 break;
18232 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18233 row_found = row;
18237 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18239 return row_found;
18243 /* Make sure that glyph rows in the current matrix of window W
18244 reference the same glyph memory as corresponding rows in the
18245 frame's frame matrix. This function is called after scrolling W's
18246 current matrix on a terminal frame in try_window_id and
18247 try_window_reusing_current_matrix. */
18249 static void
18250 sync_frame_with_window_matrix_rows (struct window *w)
18252 struct frame *f = XFRAME (w->frame);
18253 struct glyph_row *window_row, *window_row_end, *frame_row;
18255 /* Preconditions: W must be a leaf window and full-width. Its frame
18256 must have a frame matrix. */
18257 eassert (BUFFERP (w->contents));
18258 eassert (WINDOW_FULL_WIDTH_P (w));
18259 eassert (!FRAME_WINDOW_P (f));
18261 /* If W is a full-width window, glyph pointers in W's current matrix
18262 have, by definition, to be the same as glyph pointers in the
18263 corresponding frame matrix. Note that frame matrices have no
18264 marginal areas (see build_frame_matrix). */
18265 window_row = w->current_matrix->rows;
18266 window_row_end = window_row + w->current_matrix->nrows;
18267 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18268 while (window_row < window_row_end)
18270 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18271 struct glyph *end = window_row->glyphs[LAST_AREA];
18273 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18274 frame_row->glyphs[TEXT_AREA] = start;
18275 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18276 frame_row->glyphs[LAST_AREA] = end;
18278 /* Disable frame rows whose corresponding window rows have
18279 been disabled in try_window_id. */
18280 if (!window_row->enabled_p)
18281 frame_row->enabled_p = false;
18283 ++window_row, ++frame_row;
18288 /* Find the glyph row in window W containing CHARPOS. Consider all
18289 rows between START and END (not inclusive). END null means search
18290 all rows to the end of the display area of W. Value is the row
18291 containing CHARPOS or null. */
18293 struct glyph_row *
18294 row_containing_pos (struct window *w, ptrdiff_t charpos,
18295 struct glyph_row *start, struct glyph_row *end, int dy)
18297 struct glyph_row *row = start;
18298 struct glyph_row *best_row = NULL;
18299 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18300 int last_y;
18302 /* If we happen to start on a header-line, skip that. */
18303 if (row->mode_line_p)
18304 ++row;
18306 if ((end && row >= end) || !row->enabled_p)
18307 return NULL;
18309 last_y = window_text_bottom_y (w) - dy;
18311 while (true)
18313 /* Give up if we have gone too far. */
18314 if ((end && row >= end) || !row->enabled_p)
18315 return NULL;
18316 /* This formerly returned if they were equal.
18317 I think that both quantities are of a "last plus one" type;
18318 if so, when they are equal, the row is within the screen. -- rms. */
18319 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18320 return NULL;
18322 /* If it is in this row, return this row. */
18323 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18324 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18325 /* The end position of a row equals the start
18326 position of the next row. If CHARPOS is there, we
18327 would rather consider it displayed in the next
18328 line, except when this line ends in ZV. */
18329 && !row_for_charpos_p (row, charpos)))
18330 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18332 struct glyph *g;
18334 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18335 || (!best_row && !row->continued_p))
18336 return row;
18337 /* In bidi-reordered rows, there could be several rows whose
18338 edges surround CHARPOS, all of these rows belonging to
18339 the same continued line. We need to find the row which
18340 fits CHARPOS the best. */
18341 for (g = row->glyphs[TEXT_AREA];
18342 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18343 g++)
18345 if (!STRINGP (g->object))
18347 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18349 mindif = eabs (g->charpos - charpos);
18350 best_row = row;
18351 /* Exact match always wins. */
18352 if (mindif == 0)
18353 return best_row;
18358 else if (best_row && !row->continued_p)
18359 return best_row;
18360 ++row;
18365 /* Try to redisplay window W by reusing its existing display. W's
18366 current matrix must be up to date when this function is called,
18367 i.e., window_end_valid must be true.
18369 Value is
18371 >= 1 if successful, i.e. display has been updated
18372 specifically:
18373 1 means the changes were in front of a newline that precedes
18374 the window start, and the whole current matrix was reused
18375 2 means the changes were after the last position displayed
18376 in the window, and the whole current matrix was reused
18377 3 means portions of the current matrix were reused, while
18378 some of the screen lines were redrawn
18379 -1 if redisplay with same window start is known not to succeed
18380 0 if otherwise unsuccessful
18382 The following steps are performed:
18384 1. Find the last row in the current matrix of W that is not
18385 affected by changes at the start of current_buffer. If no such row
18386 is found, give up.
18388 2. Find the first row in W's current matrix that is not affected by
18389 changes at the end of current_buffer. Maybe there is no such row.
18391 3. Display lines beginning with the row + 1 found in step 1 to the
18392 row found in step 2 or, if step 2 didn't find a row, to the end of
18393 the window.
18395 4. If cursor is not known to appear on the window, give up.
18397 5. If display stopped at the row found in step 2, scroll the
18398 display and current matrix as needed.
18400 6. Maybe display some lines at the end of W, if we must. This can
18401 happen under various circumstances, like a partially visible line
18402 becoming fully visible, or because newly displayed lines are displayed
18403 in smaller font sizes.
18405 7. Update W's window end information. */
18407 static int
18408 try_window_id (struct window *w)
18410 struct frame *f = XFRAME (w->frame);
18411 struct glyph_matrix *current_matrix = w->current_matrix;
18412 struct glyph_matrix *desired_matrix = w->desired_matrix;
18413 struct glyph_row *last_unchanged_at_beg_row;
18414 struct glyph_row *first_unchanged_at_end_row;
18415 struct glyph_row *row;
18416 struct glyph_row *bottom_row;
18417 int bottom_vpos;
18418 struct it it;
18419 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18420 int dvpos, dy;
18421 struct text_pos start_pos;
18422 struct run run;
18423 int first_unchanged_at_end_vpos = 0;
18424 struct glyph_row *last_text_row, *last_text_row_at_end;
18425 struct text_pos start;
18426 ptrdiff_t first_changed_charpos, last_changed_charpos;
18428 #ifdef GLYPH_DEBUG
18429 if (inhibit_try_window_id)
18430 return 0;
18431 #endif
18433 /* This is handy for debugging. */
18434 #if false
18435 #define GIVE_UP(X) \
18436 do { \
18437 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18438 return 0; \
18439 } while (false)
18440 #else
18441 #define GIVE_UP(X) return 0
18442 #endif
18444 SET_TEXT_POS_FROM_MARKER (start, w->start);
18446 /* Don't use this for mini-windows because these can show
18447 messages and mini-buffers, and we don't handle that here. */
18448 if (MINI_WINDOW_P (w))
18449 GIVE_UP (1);
18451 /* This flag is used to prevent redisplay optimizations. */
18452 if (windows_or_buffers_changed || f->cursor_type_changed)
18453 GIVE_UP (2);
18455 /* This function's optimizations cannot be used if overlays have
18456 changed in the buffer displayed by the window, so give up if they
18457 have. */
18458 if (w->last_overlay_modified != OVERLAY_MODIFF)
18459 GIVE_UP (200);
18461 /* Verify that narrowing has not changed.
18462 Also verify that we were not told to prevent redisplay optimizations.
18463 It would be nice to further
18464 reduce the number of cases where this prevents try_window_id. */
18465 if (current_buffer->clip_changed
18466 || current_buffer->prevent_redisplay_optimizations_p)
18467 GIVE_UP (3);
18469 /* Window must either use window-based redisplay or be full width. */
18470 if (!FRAME_WINDOW_P (f)
18471 && (!FRAME_LINE_INS_DEL_OK (f)
18472 || !WINDOW_FULL_WIDTH_P (w)))
18473 GIVE_UP (4);
18475 /* Give up if point is known NOT to appear in W. */
18476 if (PT < CHARPOS (start))
18477 GIVE_UP (5);
18479 /* Another way to prevent redisplay optimizations. */
18480 if (w->last_modified == 0)
18481 GIVE_UP (6);
18483 /* Verify that window is not hscrolled. */
18484 if (w->hscroll != 0)
18485 GIVE_UP (7);
18487 /* Verify that display wasn't paused. */
18488 if (!w->window_end_valid)
18489 GIVE_UP (8);
18491 /* Likewise if highlighting trailing whitespace. */
18492 if (!NILP (Vshow_trailing_whitespace))
18493 GIVE_UP (11);
18495 /* Can't use this if overlay arrow position and/or string have
18496 changed. */
18497 if (overlay_arrows_changed_p (false))
18498 GIVE_UP (12);
18500 /* When word-wrap is on, adding a space to the first word of a
18501 wrapped line can change the wrap position, altering the line
18502 above it. It might be worthwhile to handle this more
18503 intelligently, but for now just redisplay from scratch. */
18504 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18505 GIVE_UP (21);
18507 /* Under bidi reordering, adding or deleting a character in the
18508 beginning of a paragraph, before the first strong directional
18509 character, can change the base direction of the paragraph (unless
18510 the buffer specifies a fixed paragraph direction), which will
18511 require redisplaying the whole paragraph. It might be worthwhile
18512 to find the paragraph limits and widen the range of redisplayed
18513 lines to that, but for now just give up this optimization and
18514 redisplay from scratch. */
18515 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18516 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18517 GIVE_UP (22);
18519 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18520 to that variable require thorough redisplay. */
18521 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18522 GIVE_UP (23);
18524 /* Give up if display-line-numbers is in relative mode, or when the
18525 current line's number needs to be displayed in a distinct face. */
18526 if (EQ (Vdisplay_line_numbers, Qrelative)
18527 || EQ (Vdisplay_line_numbers, Qvisual)
18528 || (!NILP (Vdisplay_line_numbers)
18529 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18530 Qline_number_current_line,
18531 w->frame))))
18532 GIVE_UP (24);
18534 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18535 only if buffer has really changed. The reason is that the gap is
18536 initially at Z for freshly visited files. The code below would
18537 set end_unchanged to 0 in that case. */
18538 if (MODIFF > SAVE_MODIFF
18539 /* This seems to happen sometimes after saving a buffer. */
18540 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18542 if (GPT - BEG < BEG_UNCHANGED)
18543 BEG_UNCHANGED = GPT - BEG;
18544 if (Z - GPT < END_UNCHANGED)
18545 END_UNCHANGED = Z - GPT;
18548 /* The position of the first and last character that has been changed. */
18549 first_changed_charpos = BEG + BEG_UNCHANGED;
18550 last_changed_charpos = Z - END_UNCHANGED;
18552 /* If window starts after a line end, and the last change is in
18553 front of that newline, then changes don't affect the display.
18554 This case happens with stealth-fontification. Note that although
18555 the display is unchanged, glyph positions in the matrix have to
18556 be adjusted, of course. */
18557 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18558 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18559 && ((last_changed_charpos < CHARPOS (start)
18560 && CHARPOS (start) == BEGV)
18561 || (last_changed_charpos < CHARPOS (start) - 1
18562 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18564 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18565 struct glyph_row *r0;
18567 /* Compute how many chars/bytes have been added to or removed
18568 from the buffer. */
18569 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18570 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18571 Z_delta = Z - Z_old;
18572 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18574 /* Give up if PT is not in the window. Note that it already has
18575 been checked at the start of try_window_id that PT is not in
18576 front of the window start. */
18577 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18578 GIVE_UP (13);
18580 /* If window start is unchanged, we can reuse the whole matrix
18581 as is, after adjusting glyph positions. No need to compute
18582 the window end again, since its offset from Z hasn't changed. */
18583 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18584 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18585 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18586 /* PT must not be in a partially visible line. */
18587 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18588 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18590 /* Adjust positions in the glyph matrix. */
18591 if (Z_delta || Z_delta_bytes)
18593 struct glyph_row *r1
18594 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18595 increment_matrix_positions (w->current_matrix,
18596 MATRIX_ROW_VPOS (r0, current_matrix),
18597 MATRIX_ROW_VPOS (r1, current_matrix),
18598 Z_delta, Z_delta_bytes);
18601 /* Set the cursor. */
18602 row = row_containing_pos (w, PT, r0, NULL, 0);
18603 if (row)
18604 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18605 return 1;
18609 /* Handle the case that changes are all below what is displayed in
18610 the window, and that PT is in the window. This shortcut cannot
18611 be taken if ZV is visible in the window, and text has been added
18612 there that is visible in the window. */
18613 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18614 /* ZV is not visible in the window, or there are no
18615 changes at ZV, actually. */
18616 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18617 || first_changed_charpos == last_changed_charpos))
18619 struct glyph_row *r0;
18621 /* Give up if PT is not in the window. Note that it already has
18622 been checked at the start of try_window_id that PT is not in
18623 front of the window start. */
18624 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18625 GIVE_UP (14);
18627 /* If window start is unchanged, we can reuse the whole matrix
18628 as is, without changing glyph positions since no text has
18629 been added/removed in front of the window end. */
18630 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18631 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18632 /* PT must not be in a partially visible line. */
18633 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18634 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18636 /* We have to compute the window end anew since text
18637 could have been added/removed after it. */
18638 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18639 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18641 /* Set the cursor. */
18642 row = row_containing_pos (w, PT, r0, NULL, 0);
18643 if (row)
18644 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18645 return 2;
18649 /* Give up if window start is in the changed area.
18651 The condition used to read
18653 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18655 but why that was tested escapes me at the moment. */
18656 if (CHARPOS (start) >= first_changed_charpos
18657 && CHARPOS (start) <= last_changed_charpos)
18658 GIVE_UP (15);
18660 /* Check that window start agrees with the start of the first glyph
18661 row in its current matrix. Check this after we know the window
18662 start is not in changed text, otherwise positions would not be
18663 comparable. */
18664 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18665 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18666 GIVE_UP (16);
18668 /* Give up if the window ends in strings. Overlay strings
18669 at the end are difficult to handle, so don't try. */
18670 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18671 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18672 GIVE_UP (20);
18674 /* Compute the position at which we have to start displaying new
18675 lines. Some of the lines at the top of the window might be
18676 reusable because they are not displaying changed text. Find the
18677 last row in W's current matrix not affected by changes at the
18678 start of current_buffer. Value is null if changes start in the
18679 first line of window. */
18680 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18681 if (last_unchanged_at_beg_row)
18683 /* Avoid starting to display in the middle of a character, a TAB
18684 for instance. This is easier than to set up the iterator
18685 exactly, and it's not a frequent case, so the additional
18686 effort wouldn't really pay off. */
18687 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18688 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18689 && last_unchanged_at_beg_row > w->current_matrix->rows)
18690 --last_unchanged_at_beg_row;
18692 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18693 GIVE_UP (17);
18695 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18696 GIVE_UP (18);
18697 start_pos = it.current.pos;
18699 /* Start displaying new lines in the desired matrix at the same
18700 vpos we would use in the current matrix, i.e. below
18701 last_unchanged_at_beg_row. */
18702 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18703 current_matrix);
18704 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18705 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18707 eassert (it.hpos == 0 && it.current_x == 0);
18709 else
18711 /* There are no reusable lines at the start of the window.
18712 Start displaying in the first text line. */
18713 start_display (&it, w, start);
18714 it.vpos = it.first_vpos;
18715 start_pos = it.current.pos;
18718 /* Find the first row that is not affected by changes at the end of
18719 the buffer. Value will be null if there is no unchanged row, in
18720 which case we must redisplay to the end of the window. delta
18721 will be set to the value by which buffer positions beginning with
18722 first_unchanged_at_end_row have to be adjusted due to text
18723 changes. */
18724 first_unchanged_at_end_row
18725 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18726 IF_DEBUG (debug_delta = delta);
18727 IF_DEBUG (debug_delta_bytes = delta_bytes);
18729 /* Set stop_pos to the buffer position up to which we will have to
18730 display new lines. If first_unchanged_at_end_row != NULL, this
18731 is the buffer position of the start of the line displayed in that
18732 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18733 that we don't stop at a buffer position. */
18734 stop_pos = 0;
18735 if (first_unchanged_at_end_row)
18737 eassert (last_unchanged_at_beg_row == NULL
18738 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18740 /* If this is a continuation line, move forward to the next one
18741 that isn't. Changes in lines above affect this line.
18742 Caution: this may move first_unchanged_at_end_row to a row
18743 not displaying text. */
18744 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18745 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18746 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18747 < it.last_visible_y))
18748 ++first_unchanged_at_end_row;
18750 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18751 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18752 >= it.last_visible_y))
18753 first_unchanged_at_end_row = NULL;
18754 else
18756 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18757 + delta);
18758 first_unchanged_at_end_vpos
18759 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18760 eassert (stop_pos >= Z - END_UNCHANGED);
18763 else if (last_unchanged_at_beg_row == NULL)
18764 GIVE_UP (19);
18767 #ifdef GLYPH_DEBUG
18769 /* Either there is no unchanged row at the end, or the one we have
18770 now displays text. This is a necessary condition for the window
18771 end pos calculation at the end of this function. */
18772 eassert (first_unchanged_at_end_row == NULL
18773 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18775 debug_last_unchanged_at_beg_vpos
18776 = (last_unchanged_at_beg_row
18777 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18778 : -1);
18779 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18781 #endif /* GLYPH_DEBUG */
18784 /* Display new lines. Set last_text_row to the last new line
18785 displayed which has text on it, i.e. might end up as being the
18786 line where the window_end_vpos is. */
18787 w->cursor.vpos = -1;
18788 last_text_row = NULL;
18789 overlay_arrow_seen = false;
18790 if (it.current_y < it.last_visible_y
18791 && !f->fonts_changed
18792 && (first_unchanged_at_end_row == NULL
18793 || IT_CHARPOS (it) < stop_pos))
18794 it.glyph_row->reversed_p = false;
18795 while (it.current_y < it.last_visible_y
18796 && !f->fonts_changed
18797 && (first_unchanged_at_end_row == NULL
18798 || IT_CHARPOS (it) < stop_pos))
18800 if (display_line (&it, -1))
18801 last_text_row = it.glyph_row - 1;
18804 if (f->fonts_changed)
18805 return -1;
18807 /* The redisplay iterations in display_line above could have
18808 triggered font-lock, which could have done something that
18809 invalidates IT->w window's end-point information, on which we
18810 rely below. E.g., one package, which will remain unnamed, used
18811 to install a font-lock-fontify-region-function that called
18812 bury-buffer, whose side effect is to switch the buffer displayed
18813 by IT->w, and that predictably resets IT->w's window_end_valid
18814 flag, which we already tested at the entry to this function.
18815 Amply punish such packages/modes by giving up on this
18816 optimization in those cases. */
18817 if (!w->window_end_valid)
18819 clear_glyph_matrix (w->desired_matrix);
18820 return -1;
18823 /* Compute differences in buffer positions, y-positions etc. for
18824 lines reused at the bottom of the window. Compute what we can
18825 scroll. */
18826 if (first_unchanged_at_end_row
18827 /* No lines reused because we displayed everything up to the
18828 bottom of the window. */
18829 && it.current_y < it.last_visible_y)
18831 dvpos = (it.vpos
18832 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18833 current_matrix));
18834 dy = it.current_y - first_unchanged_at_end_row->y;
18835 run.current_y = first_unchanged_at_end_row->y;
18836 run.desired_y = run.current_y + dy;
18837 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18839 else
18841 delta = delta_bytes = dvpos = dy
18842 = run.current_y = run.desired_y = run.height = 0;
18843 first_unchanged_at_end_row = NULL;
18845 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18848 /* Find the cursor if not already found. We have to decide whether
18849 PT will appear on this window (it sometimes doesn't, but this is
18850 not a very frequent case.) This decision has to be made before
18851 the current matrix is altered. A value of cursor.vpos < 0 means
18852 that PT is either in one of the lines beginning at
18853 first_unchanged_at_end_row or below the window. Don't care for
18854 lines that might be displayed later at the window end; as
18855 mentioned, this is not a frequent case. */
18856 if (w->cursor.vpos < 0)
18858 /* Cursor in unchanged rows at the top? */
18859 if (PT < CHARPOS (start_pos)
18860 && last_unchanged_at_beg_row)
18862 row = row_containing_pos (w, PT,
18863 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18864 last_unchanged_at_beg_row + 1, 0);
18865 if (row)
18866 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18869 /* Start from first_unchanged_at_end_row looking for PT. */
18870 else if (first_unchanged_at_end_row)
18872 row = row_containing_pos (w, PT - delta,
18873 first_unchanged_at_end_row, NULL, 0);
18874 if (row)
18875 set_cursor_from_row (w, row, w->current_matrix, delta,
18876 delta_bytes, dy, dvpos);
18879 /* Give up if cursor was not found. */
18880 if (w->cursor.vpos < 0)
18882 clear_glyph_matrix (w->desired_matrix);
18883 return -1;
18887 /* Don't let the cursor end in the scroll margins. */
18889 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18890 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18892 if ((w->cursor.y < this_scroll_margin
18893 && CHARPOS (start) > BEGV)
18894 /* Old redisplay didn't take scroll margin into account at the bottom,
18895 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18896 || (w->cursor.y + (make_cursor_line_fully_visible_p
18897 ? cursor_height + this_scroll_margin
18898 : 1)) > it.last_visible_y)
18900 w->cursor.vpos = -1;
18901 clear_glyph_matrix (w->desired_matrix);
18902 return -1;
18906 /* Scroll the display. Do it before changing the current matrix so
18907 that xterm.c doesn't get confused about where the cursor glyph is
18908 found. */
18909 if (dy && run.height)
18911 update_begin (f);
18913 if (FRAME_WINDOW_P (f))
18915 FRAME_RIF (f)->update_window_begin_hook (w);
18916 FRAME_RIF (f)->clear_window_mouse_face (w);
18917 FRAME_RIF (f)->scroll_run_hook (w, &run);
18918 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18920 else
18922 /* Terminal frame. In this case, dvpos gives the number of
18923 lines to scroll by; dvpos < 0 means scroll up. */
18924 int from_vpos
18925 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18926 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18927 int end = (WINDOW_TOP_EDGE_LINE (w)
18928 + window_wants_header_line (w)
18929 + window_internal_height (w));
18931 #if defined (HAVE_GPM) || defined (MSDOS)
18932 x_clear_window_mouse_face (w);
18933 #endif
18934 /* Perform the operation on the screen. */
18935 if (dvpos > 0)
18937 /* Scroll last_unchanged_at_beg_row to the end of the
18938 window down dvpos lines. */
18939 set_terminal_window (f, end);
18941 /* On dumb terminals delete dvpos lines at the end
18942 before inserting dvpos empty lines. */
18943 if (!FRAME_SCROLL_REGION_OK (f))
18944 ins_del_lines (f, end - dvpos, -dvpos);
18946 /* Insert dvpos empty lines in front of
18947 last_unchanged_at_beg_row. */
18948 ins_del_lines (f, from, dvpos);
18950 else if (dvpos < 0)
18952 /* Scroll up last_unchanged_at_beg_vpos to the end of
18953 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18954 set_terminal_window (f, end);
18956 /* Delete dvpos lines in front of
18957 last_unchanged_at_beg_vpos. ins_del_lines will set
18958 the cursor to the given vpos and emit |dvpos| delete
18959 line sequences. */
18960 ins_del_lines (f, from + dvpos, dvpos);
18962 /* On a dumb terminal insert dvpos empty lines at the
18963 end. */
18964 if (!FRAME_SCROLL_REGION_OK (f))
18965 ins_del_lines (f, end + dvpos, -dvpos);
18968 set_terminal_window (f, 0);
18971 update_end (f);
18974 /* Shift reused rows of the current matrix to the right position.
18975 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18976 text. */
18977 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18978 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18979 if (dvpos < 0)
18981 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18982 bottom_vpos, dvpos);
18983 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18984 bottom_vpos);
18986 else if (dvpos > 0)
18988 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18989 bottom_vpos, dvpos);
18990 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18991 first_unchanged_at_end_vpos + dvpos);
18994 /* For frame-based redisplay, make sure that current frame and window
18995 matrix are in sync with respect to glyph memory. */
18996 if (!FRAME_WINDOW_P (f))
18997 sync_frame_with_window_matrix_rows (w);
18999 /* Adjust buffer positions in reused rows. */
19000 if (delta || delta_bytes)
19001 increment_matrix_positions (current_matrix,
19002 first_unchanged_at_end_vpos + dvpos,
19003 bottom_vpos, delta, delta_bytes);
19005 /* Adjust Y positions. */
19006 if (dy)
19007 shift_glyph_matrix (w, current_matrix,
19008 first_unchanged_at_end_vpos + dvpos,
19009 bottom_vpos, dy);
19011 if (first_unchanged_at_end_row)
19013 first_unchanged_at_end_row += dvpos;
19014 if (first_unchanged_at_end_row->y >= it.last_visible_y
19015 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19016 first_unchanged_at_end_row = NULL;
19019 /* If scrolling up, there may be some lines to display at the end of
19020 the window. */
19021 last_text_row_at_end = NULL;
19022 if (dy < 0)
19024 /* Scrolling up can leave for example a partially visible line
19025 at the end of the window to be redisplayed. */
19026 /* Set last_row to the glyph row in the current matrix where the
19027 window end line is found. It has been moved up or down in
19028 the matrix by dvpos. */
19029 int last_vpos = w->window_end_vpos + dvpos;
19030 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19032 /* If last_row is the window end line, it should display text. */
19033 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19035 /* If window end line was partially visible before, begin
19036 displaying at that line. Otherwise begin displaying with the
19037 line following it. */
19038 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19040 init_to_row_start (&it, w, last_row);
19041 it.vpos = last_vpos;
19042 it.current_y = last_row->y;
19044 else
19046 init_to_row_end (&it, w, last_row);
19047 it.vpos = 1 + last_vpos;
19048 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19049 ++last_row;
19052 /* We may start in a continuation line. If so, we have to
19053 get the right continuation_lines_width and current_x. */
19054 it.continuation_lines_width = last_row->continuation_lines_width;
19055 it.hpos = it.current_x = 0;
19057 /* Display the rest of the lines at the window end. */
19058 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19059 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19061 /* Is it always sure that the display agrees with lines in
19062 the current matrix? I don't think so, so we mark rows
19063 displayed invalid in the current matrix by setting their
19064 enabled_p flag to false. */
19065 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19066 if (display_line (&it, w->cursor.vpos))
19067 last_text_row_at_end = it.glyph_row - 1;
19071 /* Update window_end_pos and window_end_vpos. */
19072 if (first_unchanged_at_end_row && !last_text_row_at_end)
19074 /* Window end line if one of the preserved rows from the current
19075 matrix. Set row to the last row displaying text in current
19076 matrix starting at first_unchanged_at_end_row, after
19077 scrolling. */
19078 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19079 row = find_last_row_displaying_text (w->current_matrix, &it,
19080 first_unchanged_at_end_row);
19081 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19082 adjust_window_ends (w, row, true);
19083 eassert (w->window_end_bytepos >= 0);
19084 IF_DEBUG (debug_method_add (w, "A"));
19086 else if (last_text_row_at_end)
19088 adjust_window_ends (w, last_text_row_at_end, false);
19089 eassert (w->window_end_bytepos >= 0);
19090 IF_DEBUG (debug_method_add (w, "B"));
19092 else if (last_text_row)
19094 /* We have displayed either to the end of the window or at the
19095 end of the window, i.e. the last row with text is to be found
19096 in the desired matrix. */
19097 adjust_window_ends (w, last_text_row, false);
19098 eassert (w->window_end_bytepos >= 0);
19100 else if (first_unchanged_at_end_row == NULL
19101 && last_text_row == NULL
19102 && last_text_row_at_end == NULL)
19104 /* Displayed to end of window, but no line containing text was
19105 displayed. Lines were deleted at the end of the window. */
19106 bool first_vpos = window_wants_header_line (w);
19107 int vpos = w->window_end_vpos;
19108 struct glyph_row *current_row = current_matrix->rows + vpos;
19109 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19111 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19113 eassert (first_vpos <= vpos);
19114 if (desired_row->enabled_p)
19116 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19117 row = desired_row;
19119 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19120 row = current_row;
19123 w->window_end_vpos = vpos + 1;
19124 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19125 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19126 eassert (w->window_end_bytepos >= 0);
19127 IF_DEBUG (debug_method_add (w, "C"));
19129 else
19130 emacs_abort ();
19132 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19133 debug_end_vpos = w->window_end_vpos));
19135 /* Record that display has not been completed. */
19136 w->window_end_valid = false;
19137 w->desired_matrix->no_scrolling_p = true;
19138 return 3;
19140 #undef GIVE_UP
19145 /***********************************************************************
19146 More debugging support
19147 ***********************************************************************/
19149 #ifdef GLYPH_DEBUG
19151 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19152 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19153 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19156 /* Dump the contents of glyph matrix MATRIX on stderr.
19158 GLYPHS 0 means don't show glyph contents.
19159 GLYPHS 1 means show glyphs in short form
19160 GLYPHS > 1 means show glyphs in long form. */
19162 void
19163 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19165 int i;
19166 for (i = 0; i < matrix->nrows; ++i)
19167 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19171 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19172 the glyph row and area where the glyph comes from. */
19174 void
19175 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19177 if (glyph->type == CHAR_GLYPH
19178 || glyph->type == GLYPHLESS_GLYPH)
19180 fprintf (stderr,
19181 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19182 glyph - row->glyphs[TEXT_AREA],
19183 (glyph->type == CHAR_GLYPH
19184 ? 'C'
19185 : 'G'),
19186 glyph->charpos,
19187 (BUFFERP (glyph->object)
19188 ? 'B'
19189 : (STRINGP (glyph->object)
19190 ? 'S'
19191 : (NILP (glyph->object)
19192 ? '0'
19193 : '-'))),
19194 glyph->pixel_width,
19195 glyph->u.ch,
19196 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19197 ? (int) glyph->u.ch
19198 : '.'),
19199 glyph->face_id,
19200 glyph->left_box_line_p,
19201 glyph->right_box_line_p);
19203 else if (glyph->type == STRETCH_GLYPH)
19205 fprintf (stderr,
19206 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19207 glyph - row->glyphs[TEXT_AREA],
19208 'S',
19209 glyph->charpos,
19210 (BUFFERP (glyph->object)
19211 ? 'B'
19212 : (STRINGP (glyph->object)
19213 ? 'S'
19214 : (NILP (glyph->object)
19215 ? '0'
19216 : '-'))),
19217 glyph->pixel_width,
19219 ' ',
19220 glyph->face_id,
19221 glyph->left_box_line_p,
19222 glyph->right_box_line_p);
19224 else if (glyph->type == IMAGE_GLYPH)
19226 fprintf (stderr,
19227 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19228 glyph - row->glyphs[TEXT_AREA],
19229 'I',
19230 glyph->charpos,
19231 (BUFFERP (glyph->object)
19232 ? 'B'
19233 : (STRINGP (glyph->object)
19234 ? 'S'
19235 : (NILP (glyph->object)
19236 ? '0'
19237 : '-'))),
19238 glyph->pixel_width,
19239 (unsigned int) glyph->u.img_id,
19240 '.',
19241 glyph->face_id,
19242 glyph->left_box_line_p,
19243 glyph->right_box_line_p);
19245 else if (glyph->type == COMPOSITE_GLYPH)
19247 fprintf (stderr,
19248 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19249 glyph - row->glyphs[TEXT_AREA],
19250 '+',
19251 glyph->charpos,
19252 (BUFFERP (glyph->object)
19253 ? 'B'
19254 : (STRINGP (glyph->object)
19255 ? 'S'
19256 : (NILP (glyph->object)
19257 ? '0'
19258 : '-'))),
19259 glyph->pixel_width,
19260 (unsigned int) glyph->u.cmp.id);
19261 if (glyph->u.cmp.automatic)
19262 fprintf (stderr,
19263 "[%d-%d]",
19264 glyph->slice.cmp.from, glyph->slice.cmp.to);
19265 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19266 glyph->face_id,
19267 glyph->left_box_line_p,
19268 glyph->right_box_line_p);
19270 else if (glyph->type == XWIDGET_GLYPH)
19272 #ifndef HAVE_XWIDGETS
19273 eassume (false);
19274 #else
19275 fprintf (stderr,
19276 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19277 glyph - row->glyphs[TEXT_AREA],
19278 'X',
19279 glyph->charpos,
19280 (BUFFERP (glyph->object)
19281 ? 'B'
19282 : (STRINGP (glyph->object)
19283 ? 'S'
19284 : '-')),
19285 glyph->pixel_width,
19286 glyph->u.xwidget,
19287 '.',
19288 glyph->face_id,
19289 glyph->left_box_line_p,
19290 glyph->right_box_line_p);
19291 #endif
19296 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19297 GLYPHS 0 means don't show glyph contents.
19298 GLYPHS 1 means show glyphs in short form
19299 GLYPHS > 1 means show glyphs in long form. */
19301 void
19302 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19304 if (glyphs != 1)
19306 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19307 fprintf (stderr, "==============================================================================\n");
19309 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19310 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19311 vpos,
19312 MATRIX_ROW_START_CHARPOS (row),
19313 MATRIX_ROW_END_CHARPOS (row),
19314 row->used[TEXT_AREA],
19315 row->contains_overlapping_glyphs_p,
19316 row->enabled_p,
19317 row->truncated_on_left_p,
19318 row->truncated_on_right_p,
19319 row->continued_p,
19320 MATRIX_ROW_CONTINUATION_LINE_P (row),
19321 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19322 row->ends_at_zv_p,
19323 row->fill_line_p,
19324 row->ends_in_middle_of_char_p,
19325 row->starts_in_middle_of_char_p,
19326 row->mouse_face_p,
19327 row->x,
19328 row->y,
19329 row->pixel_width,
19330 row->height,
19331 row->visible_height,
19332 row->ascent,
19333 row->phys_ascent);
19334 /* The next 3 lines should align to "Start" in the header. */
19335 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19336 row->end.overlay_string_index,
19337 row->continuation_lines_width);
19338 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19339 CHARPOS (row->start.string_pos),
19340 CHARPOS (row->end.string_pos));
19341 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19342 row->end.dpvec_index);
19345 if (glyphs > 1)
19347 int area;
19349 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19351 struct glyph *glyph = row->glyphs[area];
19352 struct glyph *glyph_end = glyph + row->used[area];
19354 /* Glyph for a line end in text. */
19355 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19356 ++glyph_end;
19358 if (glyph < glyph_end)
19359 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19361 for (; glyph < glyph_end; ++glyph)
19362 dump_glyph (row, glyph, area);
19365 else if (glyphs == 1)
19367 int area;
19368 char s[SHRT_MAX + 4];
19370 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19372 int i;
19374 for (i = 0; i < row->used[area]; ++i)
19376 struct glyph *glyph = row->glyphs[area] + i;
19377 if (i == row->used[area] - 1
19378 && area == TEXT_AREA
19379 && NILP (glyph->object)
19380 && glyph->type == CHAR_GLYPH
19381 && glyph->u.ch == ' ')
19383 strcpy (&s[i], "[\\n]");
19384 i += 4;
19386 else if (glyph->type == CHAR_GLYPH
19387 && glyph->u.ch < 0x80
19388 && glyph->u.ch >= ' ')
19389 s[i] = glyph->u.ch;
19390 else
19391 s[i] = '.';
19394 s[i] = '\0';
19395 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19401 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19402 Sdump_glyph_matrix, 0, 1, "p",
19403 doc: /* Dump the current matrix of the selected window to stderr.
19404 Shows contents of glyph row structures. With non-nil
19405 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19406 glyphs in short form, otherwise show glyphs in long form.
19408 Interactively, no argument means show glyphs in short form;
19409 with numeric argument, its value is passed as the GLYPHS flag. */)
19410 (Lisp_Object glyphs)
19412 struct window *w = XWINDOW (selected_window);
19413 struct buffer *buffer = XBUFFER (w->contents);
19415 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19416 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19417 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19418 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19419 fprintf (stderr, "=============================================\n");
19420 dump_glyph_matrix (w->current_matrix,
19421 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19422 return Qnil;
19426 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19427 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19428 Only text-mode frames have frame glyph matrices. */)
19429 (void)
19431 struct frame *f = XFRAME (selected_frame);
19433 if (f->current_matrix)
19434 dump_glyph_matrix (f->current_matrix, 1);
19435 else
19436 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19437 return Qnil;
19441 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19442 doc: /* Dump glyph row ROW to stderr.
19443 GLYPH 0 means don't dump glyphs.
19444 GLYPH 1 means dump glyphs in short form.
19445 GLYPH > 1 or omitted means dump glyphs in long form. */)
19446 (Lisp_Object row, Lisp_Object glyphs)
19448 struct glyph_matrix *matrix;
19449 EMACS_INT vpos;
19451 CHECK_NUMBER (row);
19452 matrix = XWINDOW (selected_window)->current_matrix;
19453 vpos = XINT (row);
19454 if (vpos >= 0 && vpos < matrix->nrows)
19455 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19456 vpos,
19457 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19458 return Qnil;
19462 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19463 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19464 GLYPH 0 means don't dump glyphs.
19465 GLYPH 1 means dump glyphs in short form.
19466 GLYPH > 1 or omitted means dump glyphs in long form.
19468 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19469 do nothing. */)
19470 (Lisp_Object row, Lisp_Object glyphs)
19472 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19473 struct frame *sf = SELECTED_FRAME ();
19474 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19475 EMACS_INT vpos;
19477 CHECK_NUMBER (row);
19478 vpos = XINT (row);
19479 if (vpos >= 0 && vpos < m->nrows)
19480 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19481 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19482 #endif
19483 return Qnil;
19487 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19488 doc: /* Toggle tracing of redisplay.
19489 With ARG, turn tracing on if and only if ARG is positive. */)
19490 (Lisp_Object arg)
19492 if (NILP (arg))
19493 trace_redisplay_p = !trace_redisplay_p;
19494 else
19496 arg = Fprefix_numeric_value (arg);
19497 trace_redisplay_p = XINT (arg) > 0;
19500 return Qnil;
19504 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19505 doc: /* Like `format', but print result to stderr.
19506 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19507 (ptrdiff_t nargs, Lisp_Object *args)
19509 Lisp_Object s = Fformat (nargs, args);
19510 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19511 return Qnil;
19514 #endif /* GLYPH_DEBUG */
19518 /***********************************************************************
19519 Building Desired Matrix Rows
19520 ***********************************************************************/
19522 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19523 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19525 static struct glyph_row *
19526 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19528 struct frame *f = XFRAME (WINDOW_FRAME (w));
19529 struct buffer *buffer = XBUFFER (w->contents);
19530 struct buffer *old = current_buffer;
19531 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19532 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19533 const unsigned char *arrow_end = arrow_string + arrow_len;
19534 const unsigned char *p;
19535 struct it it;
19536 bool multibyte_p;
19537 int n_glyphs_before;
19539 set_buffer_temp (buffer);
19540 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19541 scratch_glyph_row.reversed_p = false;
19542 it.glyph_row->used[TEXT_AREA] = 0;
19543 SET_TEXT_POS (it.position, 0, 0);
19545 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19546 p = arrow_string;
19547 while (p < arrow_end)
19549 Lisp_Object face, ilisp;
19551 /* Get the next character. */
19552 if (multibyte_p)
19553 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19554 else
19556 it.c = it.char_to_display = *p, it.len = 1;
19557 if (! ASCII_CHAR_P (it.c))
19558 it.char_to_display = BYTE8_TO_CHAR (it.c);
19560 p += it.len;
19562 /* Get its face. */
19563 ilisp = make_number (p - arrow_string);
19564 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19565 it.face_id = compute_char_face (f, it.char_to_display, face);
19567 /* Compute its width, get its glyphs. */
19568 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19569 SET_TEXT_POS (it.position, -1, -1);
19570 PRODUCE_GLYPHS (&it);
19572 /* If this character doesn't fit any more in the line, we have
19573 to remove some glyphs. */
19574 if (it.current_x > it.last_visible_x)
19576 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19577 break;
19581 set_buffer_temp (old);
19582 return it.glyph_row;
19586 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19587 glyphs to insert is determined by produce_special_glyphs. */
19589 static void
19590 insert_left_trunc_glyphs (struct it *it)
19592 struct it truncate_it;
19593 struct glyph *from, *end, *to, *toend;
19595 eassert (!FRAME_WINDOW_P (it->f)
19596 || (!it->glyph_row->reversed_p
19597 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19598 || (it->glyph_row->reversed_p
19599 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19601 /* Get the truncation glyphs. */
19602 truncate_it = *it;
19603 truncate_it.current_x = 0;
19604 truncate_it.face_id = DEFAULT_FACE_ID;
19605 truncate_it.glyph_row = &scratch_glyph_row;
19606 truncate_it.area = TEXT_AREA;
19607 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19608 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19609 truncate_it.object = Qnil;
19610 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19612 /* Overwrite glyphs from IT with truncation glyphs. */
19613 if (!it->glyph_row->reversed_p)
19615 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19617 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19618 end = from + tused;
19619 to = it->glyph_row->glyphs[TEXT_AREA];
19620 toend = to + it->glyph_row->used[TEXT_AREA];
19621 if (FRAME_WINDOW_P (it->f))
19623 /* On GUI frames, when variable-size fonts are displayed,
19624 the truncation glyphs may need more pixels than the row's
19625 glyphs they overwrite. We overwrite more glyphs to free
19626 enough screen real estate, and enlarge the stretch glyph
19627 on the right (see display_line), if there is one, to
19628 preserve the screen position of the truncation glyphs on
19629 the right. */
19630 int w = 0;
19631 struct glyph *g = to;
19632 short used;
19634 /* The first glyph could be partially visible, in which case
19635 it->glyph_row->x will be negative. But we want the left
19636 truncation glyphs to be aligned at the left margin of the
19637 window, so we override the x coordinate at which the row
19638 will begin. */
19639 it->glyph_row->x = 0;
19640 while (g < toend && w < it->truncation_pixel_width)
19642 w += g->pixel_width;
19643 ++g;
19645 if (g - to - tused > 0)
19647 memmove (to + tused, g, (toend - g) * sizeof(*g));
19648 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19650 used = it->glyph_row->used[TEXT_AREA];
19651 if (it->glyph_row->truncated_on_right_p
19652 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19653 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19654 == STRETCH_GLYPH)
19656 int extra = w - it->truncation_pixel_width;
19658 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19662 while (from < end)
19663 *to++ = *from++;
19665 /* There may be padding glyphs left over. Overwrite them too. */
19666 if (!FRAME_WINDOW_P (it->f))
19668 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19670 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19671 while (from < end)
19672 *to++ = *from++;
19676 if (to > toend)
19677 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19679 else
19681 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19683 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19684 that back to front. */
19685 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19686 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19687 toend = it->glyph_row->glyphs[TEXT_AREA];
19688 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19689 if (FRAME_WINDOW_P (it->f))
19691 int w = 0;
19692 struct glyph *g = to;
19694 while (g >= toend && w < it->truncation_pixel_width)
19696 w += g->pixel_width;
19697 --g;
19699 if (to - g - tused > 0)
19700 to = g + tused;
19701 if (it->glyph_row->truncated_on_right_p
19702 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19703 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19705 int extra = w - it->truncation_pixel_width;
19707 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19711 while (from >= end && to >= toend)
19712 *to-- = *from--;
19713 if (!FRAME_WINDOW_P (it->f))
19715 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19717 from =
19718 truncate_it.glyph_row->glyphs[TEXT_AREA]
19719 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19720 while (from >= end && to >= toend)
19721 *to-- = *from--;
19724 if (from >= end)
19726 /* Need to free some room before prepending additional
19727 glyphs. */
19728 int move_by = from - end + 1;
19729 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19730 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19732 for ( ; g >= g0; g--)
19733 g[move_by] = *g;
19734 while (from >= end)
19735 *to-- = *from--;
19736 it->glyph_row->used[TEXT_AREA] += move_by;
19741 /* Compute the hash code for ROW. */
19742 unsigned
19743 row_hash (struct glyph_row *row)
19745 int area, k;
19746 unsigned hashval = 0;
19748 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19749 for (k = 0; k < row->used[area]; ++k)
19750 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19751 + row->glyphs[area][k].u.val
19752 + row->glyphs[area][k].face_id
19753 + row->glyphs[area][k].padding_p
19754 + (row->glyphs[area][k].type << 2));
19756 return hashval;
19759 /* Compute the pixel height and width of IT->glyph_row.
19761 Most of the time, ascent and height of a display line will be equal
19762 to the max_ascent and max_height values of the display iterator
19763 structure. This is not the case if
19765 1. We hit ZV without displaying anything. In this case, max_ascent
19766 and max_height will be zero.
19768 2. We have some glyphs that don't contribute to the line height.
19769 (The glyph row flag contributes_to_line_height_p is for future
19770 pixmap extensions).
19772 The first case is easily covered by using default values because in
19773 these cases, the line height does not really matter, except that it
19774 must not be zero. */
19776 static void
19777 compute_line_metrics (struct it *it)
19779 struct glyph_row *row = it->glyph_row;
19781 if (FRAME_WINDOW_P (it->f))
19783 int i, min_y, max_y;
19785 /* The line may consist of one space only, that was added to
19786 place the cursor on it. If so, the row's height hasn't been
19787 computed yet. */
19788 if (row->height == 0)
19790 if (it->max_ascent + it->max_descent == 0)
19791 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19792 row->ascent = it->max_ascent;
19793 row->height = it->max_ascent + it->max_descent;
19794 row->phys_ascent = it->max_phys_ascent;
19795 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19796 row->extra_line_spacing = it->max_extra_line_spacing;
19799 /* Compute the width of this line. */
19800 row->pixel_width = row->x;
19801 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19802 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19804 eassert (row->pixel_width >= 0);
19805 eassert (row->ascent >= 0 && row->height > 0);
19807 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19808 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19810 /* If first line's physical ascent is larger than its logical
19811 ascent, use the physical ascent, and make the row taller.
19812 This makes accented characters fully visible. */
19813 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19814 && row->phys_ascent > row->ascent)
19816 row->height += row->phys_ascent - row->ascent;
19817 row->ascent = row->phys_ascent;
19820 /* Compute how much of the line is visible. */
19821 row->visible_height = row->height;
19823 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19824 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19826 if (row->y < min_y)
19827 row->visible_height -= min_y - row->y;
19828 if (row->y + row->height > max_y)
19829 row->visible_height -= row->y + row->height - max_y;
19831 else
19833 row->pixel_width = row->used[TEXT_AREA];
19834 if (row->continued_p)
19835 row->pixel_width -= it->continuation_pixel_width;
19836 else if (row->truncated_on_right_p)
19837 row->pixel_width -= it->truncation_pixel_width;
19838 row->ascent = row->phys_ascent = 0;
19839 row->height = row->phys_height = row->visible_height = 1;
19840 row->extra_line_spacing = 0;
19843 /* Compute a hash code for this row. */
19844 row->hash = row_hash (row);
19846 it->max_ascent = it->max_descent = 0;
19847 it->max_phys_ascent = it->max_phys_descent = 0;
19851 /* Append one space to the glyph row of iterator IT if doing a
19852 window-based redisplay. The space has the same face as
19853 IT->face_id. Value is true if a space was added.
19855 This function is called to make sure that there is always one glyph
19856 at the end of a glyph row that the cursor can be set on under
19857 window-systems. (If there weren't such a glyph we would not know
19858 how wide and tall a box cursor should be displayed).
19860 At the same time this space let's a nicely handle clearing to the
19861 end of the line if the row ends in italic text. */
19863 static bool
19864 append_space_for_newline (struct it *it, bool default_face_p)
19866 if (FRAME_WINDOW_P (it->f))
19868 int n = it->glyph_row->used[TEXT_AREA];
19870 if (it->glyph_row->glyphs[TEXT_AREA] + n
19871 < it->glyph_row->glyphs[1 + TEXT_AREA])
19873 /* Save some values that must not be changed.
19874 Must save IT->c and IT->len because otherwise
19875 ITERATOR_AT_END_P wouldn't work anymore after
19876 append_space_for_newline has been called. */
19877 enum display_element_type saved_what = it->what;
19878 int saved_c = it->c, saved_len = it->len;
19879 int saved_char_to_display = it->char_to_display;
19880 int saved_x = it->current_x;
19881 int saved_face_id = it->face_id;
19882 bool saved_box_end = it->end_of_box_run_p;
19883 struct text_pos saved_pos;
19884 Lisp_Object saved_object;
19885 struct face *face;
19887 saved_object = it->object;
19888 saved_pos = it->position;
19890 it->what = IT_CHARACTER;
19891 memset (&it->position, 0, sizeof it->position);
19892 it->object = Qnil;
19893 it->c = it->char_to_display = ' ';
19894 it->len = 1;
19896 /* If the default face was remapped, be sure to use the
19897 remapped face for the appended newline. */
19898 if (default_face_p)
19899 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19900 else if (it->face_before_selective_p)
19901 it->face_id = it->saved_face_id;
19902 face = FACE_FROM_ID (it->f, it->face_id);
19903 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19904 /* In R2L rows, we will prepend a stretch glyph that will
19905 have the end_of_box_run_p flag set for it, so there's no
19906 need for the appended newline glyph to have that flag
19907 set. */
19908 if (it->glyph_row->reversed_p
19909 /* But if the appended newline glyph goes all the way to
19910 the end of the row, there will be no stretch glyph,
19911 so leave the box flag set. */
19912 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19913 it->end_of_box_run_p = false;
19915 PRODUCE_GLYPHS (it);
19917 #ifdef HAVE_WINDOW_SYSTEM
19918 /* Make sure this space glyph has the right ascent and
19919 descent values, or else cursor at end of line will look
19920 funny, and height of empty lines will be incorrect. */
19921 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19922 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19923 if (n == 0)
19925 Lisp_Object height, total_height;
19926 int extra_line_spacing = it->extra_line_spacing;
19927 int boff = font->baseline_offset;
19929 if (font->vertical_centering)
19930 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19932 it->object = saved_object; /* get_it_property needs this */
19933 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19934 /* Must do a subset of line height processing from
19935 x_produce_glyph for newline characters. */
19936 height = get_it_property (it, Qline_height);
19937 if (CONSP (height)
19938 && CONSP (XCDR (height))
19939 && NILP (XCDR (XCDR (height))))
19941 total_height = XCAR (XCDR (height));
19942 height = XCAR (height);
19944 else
19945 total_height = Qnil;
19946 height = calc_line_height_property (it, height, font, boff, true);
19948 if (it->override_ascent >= 0)
19950 it->ascent = it->override_ascent;
19951 it->descent = it->override_descent;
19952 boff = it->override_boff;
19954 if (EQ (height, Qt))
19955 extra_line_spacing = 0;
19956 else
19958 Lisp_Object spacing;
19960 it->phys_ascent = it->ascent;
19961 it->phys_descent = it->descent;
19962 if (!NILP (height)
19963 && XINT (height) > it->ascent + it->descent)
19964 it->ascent = XINT (height) - it->descent;
19966 if (!NILP (total_height))
19967 spacing = calc_line_height_property (it, total_height, font,
19968 boff, false);
19969 else
19971 spacing = get_it_property (it, Qline_spacing);
19972 spacing = calc_line_height_property (it, spacing, font,
19973 boff, false);
19975 if (INTEGERP (spacing))
19977 extra_line_spacing = XINT (spacing);
19978 if (!NILP (total_height))
19979 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19982 if (extra_line_spacing > 0)
19984 it->descent += extra_line_spacing;
19985 if (extra_line_spacing > it->max_extra_line_spacing)
19986 it->max_extra_line_spacing = extra_line_spacing;
19988 it->max_ascent = it->ascent;
19989 it->max_descent = it->descent;
19990 /* Make sure compute_line_metrics recomputes the row height. */
19991 it->glyph_row->height = 0;
19994 g->ascent = it->max_ascent;
19995 g->descent = it->max_descent;
19996 #endif
19998 it->override_ascent = -1;
19999 it->constrain_row_ascent_descent_p = false;
20000 it->current_x = saved_x;
20001 it->object = saved_object;
20002 it->position = saved_pos;
20003 it->what = saved_what;
20004 it->face_id = saved_face_id;
20005 it->len = saved_len;
20006 it->c = saved_c;
20007 it->char_to_display = saved_char_to_display;
20008 it->end_of_box_run_p = saved_box_end;
20009 return true;
20013 return false;
20017 /* Extend the face of the last glyph in the text area of IT->glyph_row
20018 to the end of the display line. Called from display_line. If the
20019 glyph row is empty, add a space glyph to it so that we know the
20020 face to draw. Set the glyph row flag fill_line_p. If the glyph
20021 row is R2L, prepend a stretch glyph to cover the empty space to the
20022 left of the leftmost glyph. */
20024 static void
20025 extend_face_to_end_of_line (struct it *it)
20027 struct face *face, *default_face;
20028 struct frame *f = it->f;
20030 /* If line is already filled, do nothing. Non window-system frames
20031 get a grace of one more ``pixel'' because their characters are
20032 1-``pixel'' wide, so they hit the equality too early. This grace
20033 is needed only for R2L rows that are not continued, to produce
20034 one extra blank where we could display the cursor. */
20035 if ((it->current_x >= it->last_visible_x
20036 + (!FRAME_WINDOW_P (f)
20037 && it->glyph_row->reversed_p
20038 && !it->glyph_row->continued_p))
20039 /* If the window has display margins, we will need to extend
20040 their face even if the text area is filled. */
20041 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20042 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20043 return;
20045 /* The default face, possibly remapped. */
20046 default_face = FACE_FROM_ID_OR_NULL (f,
20047 lookup_basic_face (f, DEFAULT_FACE_ID));
20049 /* Face extension extends the background and box of IT->face_id
20050 to the end of the line. If the background equals the background
20051 of the frame, we don't have to do anything. */
20052 face = FACE_FROM_ID (f, (it->face_before_selective_p
20053 ? it->saved_face_id
20054 : it->face_id));
20056 if (FRAME_WINDOW_P (f)
20057 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20058 && face->box == FACE_NO_BOX
20059 && face->background == FRAME_BACKGROUND_PIXEL (f)
20060 #ifdef HAVE_WINDOW_SYSTEM
20061 && !face->stipple
20062 #endif
20063 && !it->glyph_row->reversed_p)
20064 return;
20066 /* Set the glyph row flag indicating that the face of the last glyph
20067 in the text area has to be drawn to the end of the text area. */
20068 it->glyph_row->fill_line_p = true;
20070 /* If current character of IT is not ASCII, make sure we have the
20071 ASCII face. This will be automatically undone the next time
20072 get_next_display_element returns a multibyte character. Note
20073 that the character will always be single byte in unibyte
20074 text. */
20075 if (!ASCII_CHAR_P (it->c))
20077 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20080 if (FRAME_WINDOW_P (f))
20082 /* If the row is empty, add a space with the current face of IT,
20083 so that we know which face to draw. */
20084 if (it->glyph_row->used[TEXT_AREA] == 0)
20086 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20087 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20088 it->glyph_row->used[TEXT_AREA] = 1;
20090 /* Mode line and the header line don't have margins, and
20091 likewise the frame's tool-bar window, if there is any. */
20092 if (!(it->glyph_row->mode_line_p
20093 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20094 || (WINDOWP (f->tool_bar_window)
20095 && it->w == XWINDOW (f->tool_bar_window))
20096 #endif
20099 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20100 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20102 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20103 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20104 default_face->id;
20105 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20107 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20108 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20110 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20111 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20112 default_face->id;
20113 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20116 #ifdef HAVE_WINDOW_SYSTEM
20117 if (it->glyph_row->reversed_p)
20119 /* Prepend a stretch glyph to the row, such that the
20120 rightmost glyph will be drawn flushed all the way to the
20121 right margin of the window. The stretch glyph that will
20122 occupy the empty space, if any, to the left of the
20123 glyphs. */
20124 struct font *font = face->font ? face->font : FRAME_FONT (f);
20125 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20126 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20127 struct glyph *g;
20128 int row_width, stretch_ascent, stretch_width;
20129 struct text_pos saved_pos;
20130 int saved_face_id;
20131 bool saved_avoid_cursor, saved_box_start;
20133 for (row_width = 0, g = row_start; g < row_end; g++)
20134 row_width += g->pixel_width;
20136 /* FIXME: There are various minor display glitches in R2L
20137 rows when only one of the fringes is missing. The
20138 strange condition below produces the least bad effect. */
20139 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20140 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20141 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20142 stretch_width = window_box_width (it->w, TEXT_AREA);
20143 else
20144 stretch_width = it->last_visible_x - it->first_visible_x;
20145 stretch_width -= row_width;
20147 if (stretch_width > 0)
20149 stretch_ascent =
20150 (((it->ascent + it->descent)
20151 * FONT_BASE (font)) / FONT_HEIGHT (font));
20152 saved_pos = it->position;
20153 memset (&it->position, 0, sizeof it->position);
20154 saved_avoid_cursor = it->avoid_cursor_p;
20155 it->avoid_cursor_p = true;
20156 saved_face_id = it->face_id;
20157 saved_box_start = it->start_of_box_run_p;
20158 /* The last row's stretch glyph should get the default
20159 face, to avoid painting the rest of the window with
20160 the region face, if the region ends at ZV. */
20161 if (it->glyph_row->ends_at_zv_p)
20162 it->face_id = default_face->id;
20163 else
20164 it->face_id = face->id;
20165 it->start_of_box_run_p = false;
20166 append_stretch_glyph (it, Qnil, stretch_width,
20167 it->ascent + it->descent, stretch_ascent);
20168 it->position = saved_pos;
20169 it->avoid_cursor_p = saved_avoid_cursor;
20170 it->face_id = saved_face_id;
20171 it->start_of_box_run_p = saved_box_start;
20173 /* If stretch_width comes out negative, it means that the
20174 last glyph is only partially visible. In R2L rows, we
20175 want the leftmost glyph to be partially visible, so we
20176 need to give the row the corresponding left offset. */
20177 if (stretch_width < 0)
20178 it->glyph_row->x = stretch_width;
20180 #endif /* HAVE_WINDOW_SYSTEM */
20182 else
20184 /* Save some values that must not be changed. */
20185 int saved_x = it->current_x;
20186 struct text_pos saved_pos;
20187 Lisp_Object saved_object;
20188 enum display_element_type saved_what = it->what;
20189 int saved_face_id = it->face_id;
20191 saved_object = it->object;
20192 saved_pos = it->position;
20194 it->what = IT_CHARACTER;
20195 memset (&it->position, 0, sizeof it->position);
20196 it->object = Qnil;
20197 it->c = it->char_to_display = ' ';
20198 it->len = 1;
20200 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20201 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20202 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20203 && !it->glyph_row->mode_line_p
20204 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20206 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20207 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20209 for (it->current_x = 0; g < e; g++)
20210 it->current_x += g->pixel_width;
20212 it->area = LEFT_MARGIN_AREA;
20213 it->face_id = default_face->id;
20214 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20215 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20217 PRODUCE_GLYPHS (it);
20218 /* term.c:produce_glyphs advances it->current_x only for
20219 TEXT_AREA. */
20220 it->current_x += it->pixel_width;
20223 it->current_x = saved_x;
20224 it->area = TEXT_AREA;
20227 /* The last row's blank glyphs should get the default face, to
20228 avoid painting the rest of the window with the region face,
20229 if the region ends at ZV. */
20230 if (it->glyph_row->ends_at_zv_p)
20231 it->face_id = default_face->id;
20232 else
20233 it->face_id = face->id;
20234 PRODUCE_GLYPHS (it);
20236 while (it->current_x <= it->last_visible_x)
20237 PRODUCE_GLYPHS (it);
20239 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20240 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20241 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20242 && !it->glyph_row->mode_line_p
20243 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20245 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20246 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20248 for ( ; g < e; g++)
20249 it->current_x += g->pixel_width;
20251 it->area = RIGHT_MARGIN_AREA;
20252 it->face_id = default_face->id;
20253 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20254 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20256 PRODUCE_GLYPHS (it);
20257 it->current_x += it->pixel_width;
20260 it->area = TEXT_AREA;
20263 /* Don't count these blanks really. It would let us insert a left
20264 truncation glyph below and make us set the cursor on them, maybe. */
20265 it->current_x = saved_x;
20266 it->object = saved_object;
20267 it->position = saved_pos;
20268 it->what = saved_what;
20269 it->face_id = saved_face_id;
20274 /* Value is true if text starting at CHARPOS in current_buffer is
20275 trailing whitespace. */
20277 static bool
20278 trailing_whitespace_p (ptrdiff_t charpos)
20280 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20281 int c = 0;
20283 while (bytepos < ZV_BYTE
20284 && (c = FETCH_CHAR (bytepos),
20285 c == ' ' || c == '\t'))
20286 ++bytepos;
20288 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20290 if (bytepos != PT_BYTE)
20291 return true;
20293 return false;
20297 /* Highlight trailing whitespace, if any, in ROW. */
20299 static void
20300 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20302 int used = row->used[TEXT_AREA];
20304 if (used)
20306 struct glyph *start = row->glyphs[TEXT_AREA];
20307 struct glyph *glyph = start + used - 1;
20309 if (row->reversed_p)
20311 /* Right-to-left rows need to be processed in the opposite
20312 direction, so swap the edge pointers. */
20313 glyph = start;
20314 start = row->glyphs[TEXT_AREA] + used - 1;
20317 /* Skip over glyphs inserted to display the cursor at the
20318 end of a line, for extending the face of the last glyph
20319 to the end of the line on terminals, and for truncation
20320 and continuation glyphs. */
20321 if (!row->reversed_p)
20323 while (glyph >= start
20324 && glyph->type == CHAR_GLYPH
20325 && NILP (glyph->object))
20326 --glyph;
20328 else
20330 while (glyph <= start
20331 && glyph->type == CHAR_GLYPH
20332 && NILP (glyph->object))
20333 ++glyph;
20336 /* If last glyph is a space or stretch, and it's trailing
20337 whitespace, set the face of all trailing whitespace glyphs in
20338 IT->glyph_row to `trailing-whitespace'. */
20339 if ((row->reversed_p ? glyph <= start : glyph >= start)
20340 && BUFFERP (glyph->object)
20341 && (glyph->type == STRETCH_GLYPH
20342 || (glyph->type == CHAR_GLYPH
20343 && glyph->u.ch == ' '))
20344 && trailing_whitespace_p (glyph->charpos))
20346 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20347 if (face_id < 0)
20348 return;
20350 if (!row->reversed_p)
20352 while (glyph >= start
20353 && BUFFERP (glyph->object)
20354 && (glyph->type == STRETCH_GLYPH
20355 || (glyph->type == CHAR_GLYPH
20356 && glyph->u.ch == ' ')))
20357 (glyph--)->face_id = face_id;
20359 else
20361 while (glyph <= start
20362 && BUFFERP (glyph->object)
20363 && (glyph->type == STRETCH_GLYPH
20364 || (glyph->type == CHAR_GLYPH
20365 && glyph->u.ch == ' ')))
20366 (glyph++)->face_id = face_id;
20373 /* Value is true if glyph row ROW should be
20374 considered to hold the buffer position CHARPOS. */
20376 static bool
20377 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20379 bool result = true;
20381 if (charpos == CHARPOS (row->end.pos)
20382 || charpos == MATRIX_ROW_END_CHARPOS (row))
20384 /* Suppose the row ends on a string.
20385 Unless the row is continued, that means it ends on a newline
20386 in the string. If it's anything other than a display string
20387 (e.g., a before-string from an overlay), we don't want the
20388 cursor there. (This heuristic seems to give the optimal
20389 behavior for the various types of multi-line strings.)
20390 One exception: if the string has `cursor' property on one of
20391 its characters, we _do_ want the cursor there. */
20392 if (CHARPOS (row->end.string_pos) >= 0)
20394 if (row->continued_p)
20395 result = true;
20396 else
20398 /* Check for `display' property. */
20399 struct glyph *beg = row->glyphs[TEXT_AREA];
20400 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20401 struct glyph *glyph;
20403 result = false;
20404 for (glyph = end; glyph >= beg; --glyph)
20405 if (STRINGP (glyph->object))
20407 Lisp_Object prop
20408 = Fget_char_property (make_number (charpos),
20409 Qdisplay, Qnil);
20410 result =
20411 (!NILP (prop)
20412 && display_prop_string_p (prop, glyph->object));
20413 /* If there's a `cursor' property on one of the
20414 string's characters, this row is a cursor row,
20415 even though this is not a display string. */
20416 if (!result)
20418 Lisp_Object s = glyph->object;
20420 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20422 ptrdiff_t gpos = glyph->charpos;
20424 if (!NILP (Fget_char_property (make_number (gpos),
20425 Qcursor, s)))
20427 result = true;
20428 break;
20432 break;
20436 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20438 /* If the row ends in middle of a real character,
20439 and the line is continued, we want the cursor here.
20440 That's because CHARPOS (ROW->end.pos) would equal
20441 PT if PT is before the character. */
20442 if (!row->ends_in_ellipsis_p)
20443 result = row->continued_p;
20444 else
20445 /* If the row ends in an ellipsis, then
20446 CHARPOS (ROW->end.pos) will equal point after the
20447 invisible text. We want that position to be displayed
20448 after the ellipsis. */
20449 result = false;
20451 /* If the row ends at ZV, display the cursor at the end of that
20452 row instead of at the start of the row below. */
20453 else
20454 result = row->ends_at_zv_p;
20457 return result;
20460 /* Value is true if glyph row ROW should be
20461 used to hold the cursor. */
20463 static bool
20464 cursor_row_p (struct glyph_row *row)
20466 return row_for_charpos_p (row, PT);
20471 /* Push the property PROP so that it will be rendered at the current
20472 position in IT. Return true if PROP was successfully pushed, false
20473 otherwise. Called from handle_line_prefix to handle the
20474 `line-prefix' and `wrap-prefix' properties. */
20476 static bool
20477 push_prefix_prop (struct it *it, Lisp_Object prop)
20479 struct text_pos pos =
20480 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20482 eassert (it->method == GET_FROM_BUFFER
20483 || it->method == GET_FROM_DISPLAY_VECTOR
20484 || it->method == GET_FROM_STRING
20485 || it->method == GET_FROM_IMAGE);
20487 /* We need to save the current buffer/string position, so it will be
20488 restored by pop_it, because iterate_out_of_display_property
20489 depends on that being set correctly, but some situations leave
20490 it->position not yet set when this function is called. */
20491 push_it (it, &pos);
20493 if (STRINGP (prop))
20495 if (SCHARS (prop) == 0)
20497 pop_it (it);
20498 return false;
20501 it->string = prop;
20502 it->string_from_prefix_prop_p = true;
20503 it->multibyte_p = STRING_MULTIBYTE (it->string);
20504 it->current.overlay_string_index = -1;
20505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20506 it->end_charpos = it->string_nchars = SCHARS (it->string);
20507 it->method = GET_FROM_STRING;
20508 it->stop_charpos = 0;
20509 it->prev_stop = 0;
20510 it->base_level_stop = 0;
20511 it->cmp_it.id = -1;
20513 /* Force paragraph direction to be that of the parent
20514 buffer/string. */
20515 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20516 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20517 else
20518 it->paragraph_embedding = L2R;
20520 /* Set up the bidi iterator for this display string. */
20521 if (it->bidi_p)
20523 it->bidi_it.string.lstring = it->string;
20524 it->bidi_it.string.s = NULL;
20525 it->bidi_it.string.schars = it->end_charpos;
20526 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20527 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20528 it->bidi_it.string.unibyte = !it->multibyte_p;
20529 it->bidi_it.w = it->w;
20530 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20533 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20535 it->method = GET_FROM_STRETCH;
20536 it->object = prop;
20538 #ifdef HAVE_WINDOW_SYSTEM
20539 else if (IMAGEP (prop))
20541 it->what = IT_IMAGE;
20542 it->image_id = lookup_image (it->f, prop);
20543 it->method = GET_FROM_IMAGE;
20545 #endif /* HAVE_WINDOW_SYSTEM */
20546 else
20548 pop_it (it); /* bogus display property, give up */
20549 return false;
20552 return true;
20555 /* Return the character-property PROP at the current position in IT. */
20557 static Lisp_Object
20558 get_it_property (struct it *it, Lisp_Object prop)
20560 Lisp_Object position, object = it->object;
20562 if (STRINGP (object))
20563 position = make_number (IT_STRING_CHARPOS (*it));
20564 else if (BUFFERP (object))
20566 position = make_number (IT_CHARPOS (*it));
20567 object = it->window;
20569 else
20570 return Qnil;
20572 return Fget_char_property (position, prop, object);
20575 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20577 static void
20578 handle_line_prefix (struct it *it)
20580 Lisp_Object prefix;
20582 if (it->continuation_lines_width > 0)
20584 prefix = get_it_property (it, Qwrap_prefix);
20585 if (NILP (prefix))
20586 prefix = Vwrap_prefix;
20588 else
20590 prefix = get_it_property (it, Qline_prefix);
20591 if (NILP (prefix))
20592 prefix = Vline_prefix;
20594 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20596 /* If the prefix is wider than the window, and we try to wrap
20597 it, it would acquire its own wrap prefix, and so on till the
20598 iterator stack overflows. So, don't wrap the prefix. */
20599 it->line_wrap = TRUNCATE;
20600 it->avoid_cursor_p = true;
20606 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20607 only for R2L lines from display_line and display_string, when they
20608 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20609 the line/string needs to be continued on the next glyph row. */
20610 static void
20611 unproduce_glyphs (struct it *it, int n)
20613 struct glyph *glyph, *end;
20615 eassert (it->glyph_row);
20616 eassert (it->glyph_row->reversed_p);
20617 eassert (it->area == TEXT_AREA);
20618 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20620 if (n > it->glyph_row->used[TEXT_AREA])
20621 n = it->glyph_row->used[TEXT_AREA];
20622 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20623 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20624 for ( ; glyph < end; glyph++)
20625 glyph[-n] = *glyph;
20628 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20629 and ROW->maxpos. */
20630 static void
20631 find_row_edges (struct it *it, struct glyph_row *row,
20632 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20633 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20635 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20636 lines' rows is implemented for bidi-reordered rows. */
20638 /* ROW->minpos is the value of min_pos, the minimal buffer position
20639 we have in ROW, or ROW->start.pos if that is smaller. */
20640 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20641 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20642 else
20643 /* We didn't find buffer positions smaller than ROW->start, or
20644 didn't find _any_ valid buffer positions in any of the glyphs,
20645 so we must trust the iterator's computed positions. */
20646 row->minpos = row->start.pos;
20647 if (max_pos <= 0)
20649 max_pos = CHARPOS (it->current.pos);
20650 max_bpos = BYTEPOS (it->current.pos);
20653 /* Here are the various use-cases for ending the row, and the
20654 corresponding values for ROW->maxpos:
20656 Line ends in a newline from buffer eol_pos + 1
20657 Line is continued from buffer max_pos + 1
20658 Line is truncated on right it->current.pos
20659 Line ends in a newline from string max_pos + 1(*)
20660 (*) + 1 only when line ends in a forward scan
20661 Line is continued from string max_pos
20662 Line is continued from display vector max_pos
20663 Line is entirely from a string min_pos == max_pos
20664 Line is entirely from a display vector min_pos == max_pos
20665 Line that ends at ZV ZV
20667 If you discover other use-cases, please add them here as
20668 appropriate. */
20669 if (row->ends_at_zv_p)
20670 row->maxpos = it->current.pos;
20671 else if (row->used[TEXT_AREA])
20673 bool seen_this_string = false;
20674 struct glyph_row *r1 = row - 1;
20676 /* Did we see the same display string on the previous row? */
20677 if (STRINGP (it->object)
20678 /* this is not the first row */
20679 && row > it->w->desired_matrix->rows
20680 /* previous row is not the header line */
20681 && !r1->mode_line_p
20682 /* previous row also ends in a newline from a string */
20683 && r1->ends_in_newline_from_string_p)
20685 struct glyph *start, *end;
20687 /* Search for the last glyph of the previous row that came
20688 from buffer or string. Depending on whether the row is
20689 L2R or R2L, we need to process it front to back or the
20690 other way round. */
20691 if (!r1->reversed_p)
20693 start = r1->glyphs[TEXT_AREA];
20694 end = start + r1->used[TEXT_AREA];
20695 /* Glyphs inserted by redisplay have nil as their object. */
20696 while (end > start
20697 && NILP ((end - 1)->object)
20698 && (end - 1)->charpos <= 0)
20699 --end;
20700 if (end > start)
20702 if (EQ ((end - 1)->object, it->object))
20703 seen_this_string = true;
20705 else
20706 /* If all the glyphs of the previous row were inserted
20707 by redisplay, it means the previous row was
20708 produced from a single newline, which is only
20709 possible if that newline came from the same string
20710 as the one which produced this ROW. */
20711 seen_this_string = true;
20713 else
20715 end = r1->glyphs[TEXT_AREA] - 1;
20716 start = end + r1->used[TEXT_AREA];
20717 while (end < start
20718 && NILP ((end + 1)->object)
20719 && (end + 1)->charpos <= 0)
20720 ++end;
20721 if (end < start)
20723 if (EQ ((end + 1)->object, it->object))
20724 seen_this_string = true;
20726 else
20727 seen_this_string = true;
20730 /* Take note of each display string that covers a newline only
20731 once, the first time we see it. This is for when a display
20732 string includes more than one newline in it. */
20733 if (row->ends_in_newline_from_string_p && !seen_this_string)
20735 /* If we were scanning the buffer forward when we displayed
20736 the string, we want to account for at least one buffer
20737 position that belongs to this row (position covered by
20738 the display string), so that cursor positioning will
20739 consider this row as a candidate when point is at the end
20740 of the visual line represented by this row. This is not
20741 required when scanning back, because max_pos will already
20742 have a much larger value. */
20743 if (CHARPOS (row->end.pos) > max_pos)
20744 INC_BOTH (max_pos, max_bpos);
20745 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20747 else if (CHARPOS (it->eol_pos) > 0)
20748 SET_TEXT_POS (row->maxpos,
20749 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20750 else if (row->continued_p)
20752 /* If max_pos is different from IT's current position, it
20753 means IT->method does not belong to the display element
20754 at max_pos. However, it also means that the display
20755 element at max_pos was displayed in its entirety on this
20756 line, which is equivalent to saying that the next line
20757 starts at the next buffer position. */
20758 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20759 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20760 else
20762 INC_BOTH (max_pos, max_bpos);
20763 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20766 else if (row->truncated_on_right_p)
20767 /* display_line already called reseat_at_next_visible_line_start,
20768 which puts the iterator at the beginning of the next line, in
20769 the logical order. */
20770 row->maxpos = it->current.pos;
20771 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20772 /* A line that is entirely from a string/image/stretch... */
20773 row->maxpos = row->minpos;
20774 else
20775 emacs_abort ();
20777 else
20778 row->maxpos = it->current.pos;
20781 /* Like display_count_lines, but capable of counting outside of the
20782 current narrowed region. */
20783 static ptrdiff_t
20784 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20785 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20787 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20788 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20790 ptrdiff_t val;
20791 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20792 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20793 Fwiden ();
20794 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20795 unbind_to (pdl_count, Qnil);
20796 return val;
20799 /* Count the number of screen lines in window IT->w between character
20800 position IT_CHARPOS(*IT) and the line showing that window's point. */
20801 static ptrdiff_t
20802 display_count_lines_visually (struct it *it)
20804 struct it tem_it;
20805 ptrdiff_t to;
20806 struct text_pos from;
20808 /* If we already calculated a relative line number, use that. This
20809 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20810 are laid out sequentially, one by one, for each sequence of calls
20811 to display_line or other similar function that follows a call to
20812 init_iterator. */
20813 if (it->lnum_bytepos > 0)
20814 return it->lnum + 1;
20815 else
20817 ptrdiff_t count = SPECPDL_INDEX ();
20819 if (IT_CHARPOS (*it) <= PT)
20821 from = it->current.pos;
20822 to = PT;
20824 else
20826 SET_TEXT_POS (from, PT, PT_BYTE);
20827 to = IT_CHARPOS (*it);
20829 start_display (&tem_it, it->w, from);
20830 /* Need to disable visual mode temporarily, since otherwise the
20831 call to move_it_to will cause infinite recursion. */
20832 specbind (Qdisplay_line_numbers, Qrelative);
20833 /* Some redisplay optimizations could invoke us very far from
20834 PT, which will make the caller painfully slow. There should
20835 be no need to go too far beyond the window's bottom, as any
20836 such optimization will fail to show point anyway. */
20837 move_it_to (&tem_it, to, -1,
20838 tem_it.last_visible_y
20839 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20840 -1, MOVE_TO_POS | MOVE_TO_Y);
20841 unbind_to (count, Qnil);
20842 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20846 /* Produce the line-number glyphs for the current glyph_row. If
20847 IT->glyph_row is non-NULL, populate the row with the produced
20848 glyphs. */
20849 static void
20850 maybe_produce_line_number (struct it *it)
20852 ptrdiff_t last_line = it->lnum;
20853 ptrdiff_t start_from, bytepos;
20854 ptrdiff_t this_line;
20855 bool first_time = false;
20856 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20857 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20858 void *itdata = bidi_shelve_cache ();
20860 if (EQ (Vdisplay_line_numbers, Qvisual))
20861 this_line = display_count_lines_visually (it);
20862 else
20864 if (!last_line)
20866 /* If possible, reuse data cached by line-number-mode. */
20867 if (it->w->base_line_number > 0
20868 && it->w->base_line_pos > 0
20869 && it->w->base_line_pos <= IT_CHARPOS (*it)
20870 /* line-number-mode always displays narrowed line
20871 numbers, so we cannot use its data if the user wants
20872 line numbers that disregard narrowing. */
20873 && !(display_line_numbers_widen
20874 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE)))
20876 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20877 last_line = it->w->base_line_number - 1;
20879 else
20880 start_from = beg_byte;
20881 if (!it->lnum_bytepos)
20882 first_time = true;
20884 else
20885 start_from = it->lnum_bytepos;
20887 /* Paranoia: what if someone changes the narrowing since the
20888 last time display_line was called? Shouldn't really happen,
20889 but who knows what some crazy Lisp invoked by :eval could do? */
20890 if (!(beg_byte <= start_from && start_from <= z_byte))
20892 last_line = 0;
20893 start_from = beg_byte;
20896 this_line =
20897 last_line + display_count_lines_logically (start_from,
20898 IT_BYTEPOS (*it),
20899 IT_CHARPOS (*it), &bytepos);
20900 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20901 eassert (bytepos == IT_BYTEPOS (*it));
20904 /* Record the line number information. */
20905 if (this_line != last_line || !it->lnum_bytepos)
20907 it->lnum = this_line;
20908 it->lnum_bytepos = IT_BYTEPOS (*it);
20911 /* Produce the glyphs for the line number. */
20912 struct it tem_it;
20913 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20914 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20915 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20916 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20917 int current_lnum_face_id
20918 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20919 /* Compute point's line number if needed. */
20920 if ((EQ (Vdisplay_line_numbers, Qrelative)
20921 || EQ (Vdisplay_line_numbers, Qvisual)
20922 || lnum_face_id != current_lnum_face_id)
20923 && !it->pt_lnum)
20925 ptrdiff_t ignored;
20926 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20927 it->pt_lnum =
20928 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20929 PT, &ignored);
20930 else
20931 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20932 &ignored);
20934 /* Compute the required width if needed. */
20935 if (!it->lnum_width)
20937 if (NATNUMP (Vdisplay_line_numbers_width))
20938 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
20940 /* Max line number to be displayed cannot be more than the one
20941 corresponding to the last row of the desired matrix. */
20942 ptrdiff_t max_lnum;
20944 if (NILP (Vdisplay_line_numbers_current_absolute)
20945 && (EQ (Vdisplay_line_numbers, Qrelative)
20946 || EQ (Vdisplay_line_numbers, Qvisual)))
20947 /* We subtract one more because the current line is always
20948 zero in this mode. */
20949 max_lnum = it->w->desired_matrix->nrows - 2;
20950 else if (EQ (Vdisplay_line_numbers, Qvisual))
20951 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
20952 else
20953 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20954 max_lnum = max (1, max_lnum);
20955 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
20956 eassert (it->lnum_width > 0);
20958 if (EQ (Vdisplay_line_numbers, Qrelative))
20959 lnum_offset = it->pt_lnum;
20960 else if (EQ (Vdisplay_line_numbers, Qvisual))
20961 lnum_offset = 0;
20963 /* Under 'relative', display the absolute line number for the
20964 current line, unless the user requests otherwise. */
20965 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
20966 if ((EQ (Vdisplay_line_numbers, Qrelative)
20967 || EQ (Vdisplay_line_numbers, Qvisual))
20968 && lnum_to_display == 0
20969 && !NILP (Vdisplay_line_numbers_current_absolute))
20970 lnum_to_display = it->pt_lnum + 1;
20971 /* In L2R rows we need to append the blank separator, in R2L
20972 rows we need to prepend it. But this function is usually
20973 called when no display elements were produced from the
20974 following line, so the paragraph direction might be unknown.
20975 Therefore we cheat and add 2 blanks, one on either side. */
20976 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
20977 strcat (lnum_buf, " ");
20979 /* Setup for producing the glyphs. */
20980 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
20981 /* FIXME: Use specialized face. */
20982 DEFAULT_FACE_ID);
20983 scratch_glyph_row.reversed_p = false;
20984 scratch_glyph_row.used[TEXT_AREA] = 0;
20985 SET_TEXT_POS (tem_it.position, 0, 0);
20986 tem_it.avoid_cursor_p = true;
20987 tem_it.bidi_p = true;
20988 tem_it.bidi_it.type = WEAK_EN;
20989 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
20990 1 level in R2L paragraphs. Emulate that, assuming we are in
20991 an L2R paragraph. */
20992 tem_it.bidi_it.resolved_level = 2;
20994 /* Produce glyphs for the line number in a scratch glyph_row. */
20995 int n_glyphs_before;
20996 for (const char *p = lnum_buf; *p; p++)
20998 /* For continuation lines and lines after ZV, instead of a line
20999 number, produce a blank prefix of the same width. Use the
21000 default face for the blank field beyond ZV. */
21001 if (beyond_zv)
21002 tem_it.face_id = it->base_face_id;
21003 else if (lnum_face_id != current_lnum_face_id
21004 && (EQ (Vdisplay_line_numbers, Qvisual)
21005 ? this_line == 0
21006 : this_line == it->pt_lnum))
21007 tem_it.face_id = current_lnum_face_id;
21008 else
21009 tem_it.face_id = lnum_face_id;
21010 if (beyond_zv
21011 /* Don't display the same line number more than once. */
21012 || (!EQ (Vdisplay_line_numbers, Qvisual)
21013 && (it->continuation_lines_width > 0
21014 || (this_line == last_line && !first_time))))
21015 tem_it.c = tem_it.char_to_display = ' ';
21016 else
21017 tem_it.c = tem_it.char_to_display = *p;
21018 tem_it.len = 1;
21019 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21020 /* Make sure these glyphs will have a "position" of -1. */
21021 SET_TEXT_POS (tem_it.position, -1, -1);
21022 PRODUCE_GLYPHS (&tem_it);
21024 /* Stop producing glyphs if we don't have enough space on
21025 this line. FIXME: should we refrain from producing the
21026 line number at all in that case? */
21027 if (tem_it.current_x > tem_it.last_visible_x)
21029 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21030 break;
21034 /* Record the width in pixels we need for the line number display. */
21035 it->lnum_pixel_width = tem_it.current_x;
21036 /* Copy the produced glyphs into IT's glyph_row. */
21037 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21038 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21039 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21040 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21042 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21044 for ( ; g < e; g++)
21046 it->current_x += g->pixel_width;
21047 /* The following is important when this function is called
21048 from move_it_in_display_line_to: HPOS is incremented only
21049 when we are in the visible portion of the glyph row. */
21050 if (it->current_x > it->first_visible_x)
21051 it->hpos++;
21052 if (p)
21054 *p++ = *g;
21055 (*u)++;
21059 /* Update IT's metrics due to glyphs produced for line numbers. */
21060 if (it->glyph_row)
21062 struct glyph_row *row = it->glyph_row;
21064 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21065 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21066 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21067 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21068 tem_it.max_phys_descent);
21070 else
21072 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21073 it->max_descent = max (it->max_descent, tem_it.max_descent);
21074 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21075 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21078 bidi_unshelve_cache (itdata, false);
21081 /* Return true if this glyph row needs a line number to be produced
21082 for it. */
21083 static bool
21084 should_produce_line_number (struct it *it)
21086 if (NILP (Vdisplay_line_numbers))
21087 return false;
21089 /* Don't display line numbers in minibuffer windows. */
21090 if (MINI_WINDOW_P (it->w))
21091 return false;
21093 #ifdef HAVE_WINDOW_SYSTEM
21094 /* Don't display line number in tooltip frames. */
21095 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame))
21096 return false;
21097 #endif
21099 /* If the character at current position has a non-nil special
21100 property, disable line numbers for this row. This is for
21101 packages such as company-mode, which need this for their tricky
21102 layout, where line numbers get in the way. */
21103 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21104 Qdisplay_line_numbers_disable,
21105 it->window);
21106 /* For ZV, we need to also look in empty overlays at that point,
21107 because get-char-property always returns nil for ZV, except if
21108 the property is in 'default-text-properties'. */
21109 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21110 val = disable_line_numbers_overlay_at_eob ();
21111 return NILP (val) ? true : false;
21114 /* Return true if ROW has no glyphs except those inserted by the
21115 display engine. This is needed for indicate-empty-lines and
21116 similar features when the glyph row starts with glyphs which didn't
21117 come from buffer or string. */
21118 static bool
21119 row_text_area_empty (struct glyph_row *row)
21121 if (!row->reversed_p)
21123 for (struct glyph *g = row->glyphs[TEXT_AREA];
21124 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21125 g++)
21126 if (!NILP (g->object) || g->charpos > 0)
21127 return false;
21129 else
21131 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21132 g > row->glyphs[TEXT_AREA];
21133 g--)
21134 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21135 return false;
21138 return true;
21141 /* Construct the glyph row IT->glyph_row in the desired matrix of
21142 IT->w from text at the current position of IT. See dispextern.h
21143 for an overview of struct it. Value is true if
21144 IT->glyph_row displays text, as opposed to a line displaying ZV
21145 only. CURSOR_VPOS is the window-relative vertical position of
21146 the glyph row displaying the cursor, or -1 if unknown. */
21148 static bool
21149 display_line (struct it *it, int cursor_vpos)
21151 struct glyph_row *row = it->glyph_row;
21152 Lisp_Object overlay_arrow_string;
21153 struct it wrap_it;
21154 void *wrap_data = NULL;
21155 bool may_wrap = false;
21156 int wrap_x UNINIT;
21157 int wrap_row_used = -1;
21158 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21159 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21160 int wrap_row_extra_line_spacing UNINIT;
21161 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21162 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21163 int cvpos;
21164 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21165 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21166 bool pending_handle_line_prefix = false;
21167 int header_line = window_wants_header_line (it->w);
21168 bool hscroll_this_line = (cursor_vpos >= 0
21169 && it->vpos == cursor_vpos - header_line
21170 && hscrolling_current_line_p (it->w));
21171 int first_visible_x = it->first_visible_x;
21172 int last_visible_x = it->last_visible_x;
21173 int x_incr = 0;
21175 /* We always start displaying at hpos zero even if hscrolled. */
21176 eassert (it->hpos == 0 && it->current_x == 0);
21178 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21179 >= it->w->desired_matrix->nrows)
21181 it->w->nrows_scale_factor++;
21182 it->f->fonts_changed = true;
21183 return false;
21186 /* Clear the result glyph row and enable it. */
21187 prepare_desired_row (it->w, row, false);
21189 row->y = it->current_y;
21190 row->start = it->start;
21191 row->continuation_lines_width = it->continuation_lines_width;
21192 row->displays_text_p = true;
21193 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21194 it->starts_in_middle_of_char_p = false;
21196 /* Arrange the overlays nicely for our purposes. Usually, we call
21197 display_line on only one line at a time, in which case this
21198 can't really hurt too much, or we call it on lines which appear
21199 one after another in the buffer, in which case all calls to
21200 recenter_overlay_lists but the first will be pretty cheap. */
21201 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21203 /* If we are going to display the cursor's line, account for the
21204 hscroll of that line. We subtract the window's min_hscroll,
21205 because that was already accounted for in init_iterator. */
21206 if (hscroll_this_line)
21207 x_incr =
21208 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21209 * FRAME_COLUMN_WIDTH (it->f);
21211 bool line_number_needed = should_produce_line_number (it);
21213 /* Move over display elements that are not visible because we are
21214 hscrolled. This may stop at an x-position < first_visible_x
21215 if the first glyph is partially visible or if we hit a line end. */
21216 if (it->current_x < it->first_visible_x + x_incr)
21218 enum move_it_result move_result;
21220 this_line_min_pos = row->start.pos;
21221 if (hscroll_this_line)
21223 it->first_visible_x += x_incr;
21224 it->last_visible_x += x_incr;
21226 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21227 MOVE_TO_POS | MOVE_TO_X);
21228 /* If we are under a large hscroll, move_it_in_display_line_to
21229 could hit the end of the line without reaching
21230 first_visible_x. Pretend that we did reach it. This is
21231 especially important on a TTY, where we will call
21232 extend_face_to_end_of_line, which needs to know how many
21233 blank glyphs to produce. */
21234 if (it->current_x < it->first_visible_x
21235 && (move_result == MOVE_NEWLINE_OR_CR
21236 || move_result == MOVE_POS_MATCH_OR_ZV))
21237 it->current_x = it->first_visible_x;
21239 /* Record the smallest positions seen while we moved over
21240 display elements that are not visible. This is needed by
21241 redisplay_internal for optimizing the case where the cursor
21242 stays inside the same line. The rest of this function only
21243 considers positions that are actually displayed, so
21244 RECORD_MAX_MIN_POS will not otherwise record positions that
21245 are hscrolled to the left of the left edge of the window. */
21246 min_pos = CHARPOS (this_line_min_pos);
21247 min_bpos = BYTEPOS (this_line_min_pos);
21249 /* Produce line number, if needed. */
21250 if (line_number_needed)
21251 maybe_produce_line_number (it);
21253 else if (it->area == TEXT_AREA)
21255 /* Line numbers should precede the line-prefix or wrap-prefix. */
21256 if (line_number_needed)
21257 maybe_produce_line_number (it);
21259 /* We only do this when not calling move_it_in_display_line_to
21260 above, because that function calls itself handle_line_prefix. */
21261 handle_line_prefix (it);
21263 else
21265 /* Line-prefix and wrap-prefix are always displayed in the text
21266 area. But if this is the first call to display_line after
21267 init_iterator, the iterator might have been set up to write
21268 into a marginal area, e.g. if the line begins with some
21269 display property that writes to the margins. So we need to
21270 wait with the call to handle_line_prefix until whatever
21271 writes to the margin has done its job. */
21272 pending_handle_line_prefix = true;
21275 /* Get the initial row height. This is either the height of the
21276 text hscrolled, if there is any, or zero. */
21277 row->ascent = it->max_ascent;
21278 row->height = it->max_ascent + it->max_descent;
21279 row->phys_ascent = it->max_phys_ascent;
21280 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21281 row->extra_line_spacing = it->max_extra_line_spacing;
21283 /* Utility macro to record max and min buffer positions seen until now. */
21284 #define RECORD_MAX_MIN_POS(IT) \
21285 do \
21287 bool composition_p \
21288 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21289 ptrdiff_t current_pos = \
21290 composition_p ? (IT)->cmp_it.charpos \
21291 : IT_CHARPOS (*(IT)); \
21292 ptrdiff_t current_bpos = \
21293 composition_p ? CHAR_TO_BYTE (current_pos) \
21294 : IT_BYTEPOS (*(IT)); \
21295 if (current_pos < min_pos) \
21297 min_pos = current_pos; \
21298 min_bpos = current_bpos; \
21300 if (IT_CHARPOS (*it) > max_pos) \
21302 max_pos = IT_CHARPOS (*it); \
21303 max_bpos = IT_BYTEPOS (*it); \
21306 while (false)
21308 /* Loop generating characters. The loop is left with IT on the next
21309 character to display. */
21310 while (true)
21312 int n_glyphs_before, hpos_before, x_before;
21313 int x, nglyphs;
21314 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21316 /* Retrieve the next thing to display. Value is false if end of
21317 buffer reached. */
21318 if (!get_next_display_element (it))
21320 bool row_has_glyphs = false;
21321 /* Maybe add a space at the end of this line that is used to
21322 display the cursor there under X. Set the charpos of the
21323 first glyph of blank lines not corresponding to any text
21324 to -1. */
21325 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21326 row->exact_window_width_line_p = true;
21327 else if ((append_space_for_newline (it, true)
21328 && row->used[TEXT_AREA] == 1)
21329 || row->used[TEXT_AREA] == 0
21330 || (row_has_glyphs = row_text_area_empty (row)))
21332 row->glyphs[TEXT_AREA]->charpos = -1;
21333 /* Don't reset the displays_text_p flag if we are
21334 displaying line numbers or line-prefix. */
21335 if (!row_has_glyphs)
21336 row->displays_text_p = false;
21338 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21339 && (!MINI_WINDOW_P (it->w)))
21340 row->indicate_empty_line_p = true;
21343 it->continuation_lines_width = 0;
21344 /* Reset those iterator values set from display property
21345 values. This is for the case when the display property
21346 ends at ZV, and is not a replacing property, so pop_it is
21347 not called. */
21348 it->font_height = Qnil;
21349 it->voffset = 0;
21350 row->ends_at_zv_p = true;
21351 /* A row that displays right-to-left text must always have
21352 its last face extended all the way to the end of line,
21353 even if this row ends in ZV, because we still write to
21354 the screen left to right. We also need to extend the
21355 last face if the default face is remapped to some
21356 different face, otherwise the functions that clear
21357 portions of the screen will clear with the default face's
21358 background color. */
21359 if (row->reversed_p
21360 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21361 extend_face_to_end_of_line (it);
21362 break;
21365 /* Now, get the metrics of what we want to display. This also
21366 generates glyphs in `row' (which is IT->glyph_row). */
21367 n_glyphs_before = row->used[TEXT_AREA];
21368 x = it->current_x;
21370 /* Remember the line height so far in case the next element doesn't
21371 fit on the line. */
21372 if (it->line_wrap != TRUNCATE)
21374 ascent = it->max_ascent;
21375 descent = it->max_descent;
21376 phys_ascent = it->max_phys_ascent;
21377 phys_descent = it->max_phys_descent;
21379 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21381 if (IT_DISPLAYING_WHITESPACE (it))
21382 may_wrap = true;
21383 else if (may_wrap)
21385 SAVE_IT (wrap_it, *it, wrap_data);
21386 wrap_x = x;
21387 wrap_row_used = row->used[TEXT_AREA];
21388 wrap_row_ascent = row->ascent;
21389 wrap_row_height = row->height;
21390 wrap_row_phys_ascent = row->phys_ascent;
21391 wrap_row_phys_height = row->phys_height;
21392 wrap_row_extra_line_spacing = row->extra_line_spacing;
21393 wrap_row_min_pos = min_pos;
21394 wrap_row_min_bpos = min_bpos;
21395 wrap_row_max_pos = max_pos;
21396 wrap_row_max_bpos = max_bpos;
21397 may_wrap = false;
21402 PRODUCE_GLYPHS (it);
21404 /* If this display element was in marginal areas, continue with
21405 the next one. */
21406 if (it->area != TEXT_AREA)
21408 row->ascent = max (row->ascent, it->max_ascent);
21409 row->height = max (row->height, it->max_ascent + it->max_descent);
21410 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21411 row->phys_height = max (row->phys_height,
21412 it->max_phys_ascent + it->max_phys_descent);
21413 row->extra_line_spacing = max (row->extra_line_spacing,
21414 it->max_extra_line_spacing);
21415 set_iterator_to_next (it, true);
21416 /* If we didn't handle the line/wrap prefix above, and the
21417 call to set_iterator_to_next just switched to TEXT_AREA,
21418 process the prefix now. */
21419 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21421 /* Line numbers should precede the line-prefix or wrap-prefix. */
21422 if (line_number_needed)
21423 maybe_produce_line_number (it);
21425 pending_handle_line_prefix = false;
21426 handle_line_prefix (it);
21428 continue;
21431 /* Does the display element fit on the line? If we truncate
21432 lines, we should draw past the right edge of the window. If
21433 we don't truncate, we want to stop so that we can display the
21434 continuation glyph before the right margin. If lines are
21435 continued, there are two possible strategies for characters
21436 resulting in more than 1 glyph (e.g. tabs): Display as many
21437 glyphs as possible in this line and leave the rest for the
21438 continuation line, or display the whole element in the next
21439 line. Original redisplay did the former, so we do it also. */
21440 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21441 hpos_before = it->hpos;
21442 x_before = x;
21444 if (/* Not a newline. */
21445 nglyphs > 0
21446 /* Glyphs produced fit entirely in the line. */
21447 && it->current_x < it->last_visible_x)
21449 it->hpos += nglyphs;
21450 row->ascent = max (row->ascent, it->max_ascent);
21451 row->height = max (row->height, it->max_ascent + it->max_descent);
21452 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21453 row->phys_height = max (row->phys_height,
21454 it->max_phys_ascent + it->max_phys_descent);
21455 row->extra_line_spacing = max (row->extra_line_spacing,
21456 it->max_extra_line_spacing);
21457 if (it->current_x - it->pixel_width < it->first_visible_x
21458 /* In R2L rows, we arrange in extend_face_to_end_of_line
21459 to add a right offset to the line, by a suitable
21460 change to the stretch glyph that is the leftmost
21461 glyph of the line. */
21462 && !row->reversed_p)
21463 row->x = x - it->first_visible_x;
21464 /* Record the maximum and minimum buffer positions seen so
21465 far in glyphs that will be displayed by this row. */
21466 if (it->bidi_p)
21467 RECORD_MAX_MIN_POS (it);
21469 else
21471 int i, new_x;
21472 struct glyph *glyph;
21474 for (i = 0; i < nglyphs; ++i, x = new_x)
21476 /* Identify the glyphs added by the last call to
21477 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21478 the previous glyphs. */
21479 if (!row->reversed_p)
21480 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21481 else
21482 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21483 new_x = x + glyph->pixel_width;
21485 if (/* Lines are continued. */
21486 it->line_wrap != TRUNCATE
21487 && (/* Glyph doesn't fit on the line. */
21488 new_x > it->last_visible_x
21489 /* Or it fits exactly on a window system frame. */
21490 || (new_x == it->last_visible_x
21491 && FRAME_WINDOW_P (it->f)
21492 && (row->reversed_p
21493 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21494 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21496 /* End of a continued line. */
21498 if (it->hpos == 0
21499 || (new_x == it->last_visible_x
21500 && FRAME_WINDOW_P (it->f)
21501 && (row->reversed_p
21502 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21503 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21505 /* Current glyph is the only one on the line or
21506 fits exactly on the line. We must continue
21507 the line because we can't draw the cursor
21508 after the glyph. */
21509 row->continued_p = true;
21510 it->current_x = new_x;
21511 it->continuation_lines_width += new_x;
21512 ++it->hpos;
21513 if (i == nglyphs - 1)
21515 /* If line-wrap is on, check if a previous
21516 wrap point was found. */
21517 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21518 && wrap_row_used > 0
21519 /* Even if there is a previous wrap
21520 point, continue the line here as
21521 usual, if (i) the previous character
21522 was a space or tab AND (ii) the
21523 current character is not. */
21524 && (!may_wrap
21525 || IT_DISPLAYING_WHITESPACE (it)))
21526 goto back_to_wrap;
21528 /* Record the maximum and minimum buffer
21529 positions seen so far in glyphs that will be
21530 displayed by this row. */
21531 if (it->bidi_p)
21532 RECORD_MAX_MIN_POS (it);
21533 set_iterator_to_next (it, true);
21534 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21536 if (!get_next_display_element (it))
21538 row->exact_window_width_line_p = true;
21539 it->continuation_lines_width = 0;
21540 it->font_height = Qnil;
21541 it->voffset = 0;
21542 row->continued_p = false;
21543 row->ends_at_zv_p = true;
21545 else if (ITERATOR_AT_END_OF_LINE_P (it))
21547 row->continued_p = false;
21548 row->exact_window_width_line_p = true;
21550 /* If line-wrap is on, check if a
21551 previous wrap point was found. */
21552 else if (wrap_row_used > 0
21553 /* Even if there is a previous wrap
21554 point, continue the line here as
21555 usual, if (i) the previous character
21556 was a space or tab AND (ii) the
21557 current character is not. */
21558 && (!may_wrap
21559 || IT_DISPLAYING_WHITESPACE (it)))
21560 goto back_to_wrap;
21564 else if (it->bidi_p)
21565 RECORD_MAX_MIN_POS (it);
21566 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21567 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21568 extend_face_to_end_of_line (it);
21570 else if (CHAR_GLYPH_PADDING_P (*glyph)
21571 && !FRAME_WINDOW_P (it->f))
21573 /* A padding glyph that doesn't fit on this line.
21574 This means the whole character doesn't fit
21575 on the line. */
21576 if (row->reversed_p)
21577 unproduce_glyphs (it, row->used[TEXT_AREA]
21578 - n_glyphs_before);
21579 row->used[TEXT_AREA] = n_glyphs_before;
21581 /* Fill the rest of the row with continuation
21582 glyphs like in 20.x. */
21583 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21584 < row->glyphs[1 + TEXT_AREA])
21585 produce_special_glyphs (it, IT_CONTINUATION);
21587 row->continued_p = true;
21588 it->current_x = x_before;
21589 it->continuation_lines_width += x_before;
21591 /* Restore the height to what it was before the
21592 element not fitting on the line. */
21593 it->max_ascent = ascent;
21594 it->max_descent = descent;
21595 it->max_phys_ascent = phys_ascent;
21596 it->max_phys_descent = phys_descent;
21597 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21598 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21599 extend_face_to_end_of_line (it);
21601 else if (wrap_row_used > 0)
21603 back_to_wrap:
21604 if (row->reversed_p)
21605 unproduce_glyphs (it,
21606 row->used[TEXT_AREA] - wrap_row_used);
21607 RESTORE_IT (it, &wrap_it, wrap_data);
21608 it->continuation_lines_width += wrap_x;
21609 row->used[TEXT_AREA] = wrap_row_used;
21610 row->ascent = wrap_row_ascent;
21611 row->height = wrap_row_height;
21612 row->phys_ascent = wrap_row_phys_ascent;
21613 row->phys_height = wrap_row_phys_height;
21614 row->extra_line_spacing = wrap_row_extra_line_spacing;
21615 min_pos = wrap_row_min_pos;
21616 min_bpos = wrap_row_min_bpos;
21617 max_pos = wrap_row_max_pos;
21618 max_bpos = wrap_row_max_bpos;
21619 row->continued_p = true;
21620 row->ends_at_zv_p = false;
21621 row->exact_window_width_line_p = false;
21622 it->continuation_lines_width += x;
21624 /* Make sure that a non-default face is extended
21625 up to the right margin of the window. */
21626 extend_face_to_end_of_line (it);
21628 else if ((it->what == IT_CHARACTER
21629 || it->what == IT_STRETCH
21630 || it->what == IT_COMPOSITION)
21631 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21633 /* A TAB that extends past the right edge of the
21634 window. This produces a single glyph on
21635 window system frames. We leave the glyph in
21636 this row and let it fill the row, but don't
21637 consume the TAB. */
21638 if ((row->reversed_p
21639 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21640 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21641 produce_special_glyphs (it, IT_CONTINUATION);
21642 it->continuation_lines_width += it->last_visible_x;
21643 row->ends_in_middle_of_char_p = true;
21644 row->continued_p = true;
21645 glyph->pixel_width = it->last_visible_x - x;
21646 it->starts_in_middle_of_char_p = true;
21647 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21648 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21649 extend_face_to_end_of_line (it);
21651 else
21653 /* Something other than a TAB that draws past
21654 the right edge of the window. Restore
21655 positions to values before the element. */
21656 if (row->reversed_p)
21657 unproduce_glyphs (it, row->used[TEXT_AREA]
21658 - (n_glyphs_before + i));
21659 row->used[TEXT_AREA] = n_glyphs_before + i;
21661 /* Display continuation glyphs. */
21662 it->current_x = x_before;
21663 it->continuation_lines_width += x;
21664 if (!FRAME_WINDOW_P (it->f)
21665 || (row->reversed_p
21666 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21667 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21668 produce_special_glyphs (it, IT_CONTINUATION);
21669 row->continued_p = true;
21671 extend_face_to_end_of_line (it);
21673 if (nglyphs > 1 && i > 0)
21675 row->ends_in_middle_of_char_p = true;
21676 it->starts_in_middle_of_char_p = true;
21679 /* Restore the height to what it was before the
21680 element not fitting on the line. */
21681 it->max_ascent = ascent;
21682 it->max_descent = descent;
21683 it->max_phys_ascent = phys_ascent;
21684 it->max_phys_descent = phys_descent;
21687 break;
21689 else if (new_x > it->first_visible_x)
21691 /* Increment number of glyphs actually displayed. */
21692 ++it->hpos;
21694 /* Record the maximum and minimum buffer positions
21695 seen so far in glyphs that will be displayed by
21696 this row. */
21697 if (it->bidi_p)
21698 RECORD_MAX_MIN_POS (it);
21700 if (x < it->first_visible_x && !row->reversed_p)
21701 /* Glyph is partially visible, i.e. row starts at
21702 negative X position. Don't do that in R2L
21703 rows, where we arrange to add a right offset to
21704 the line in extend_face_to_end_of_line, by a
21705 suitable change to the stretch glyph that is
21706 the leftmost glyph of the line. */
21707 row->x = x - it->first_visible_x;
21708 /* When the last glyph of an R2L row only fits
21709 partially on the line, we need to set row->x to a
21710 negative offset, so that the leftmost glyph is
21711 the one that is partially visible. But if we are
21712 going to produce the truncation glyph, this will
21713 be taken care of in produce_special_glyphs. */
21714 if (row->reversed_p
21715 && new_x > it->last_visible_x
21716 && !(it->line_wrap == TRUNCATE
21717 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21719 eassert (FRAME_WINDOW_P (it->f));
21720 row->x = it->last_visible_x - new_x;
21723 else
21725 /* Glyph is completely off the left margin of the
21726 window. This should not happen because of the
21727 move_it_in_display_line at the start of this
21728 function, unless the text display area of the
21729 window is empty. */
21730 eassert (it->first_visible_x <= it->last_visible_x);
21733 /* Even if this display element produced no glyphs at all,
21734 we want to record its position. */
21735 if (it->bidi_p && nglyphs == 0)
21736 RECORD_MAX_MIN_POS (it);
21738 row->ascent = max (row->ascent, it->max_ascent);
21739 row->height = max (row->height, it->max_ascent + it->max_descent);
21740 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21741 row->phys_height = max (row->phys_height,
21742 it->max_phys_ascent + it->max_phys_descent);
21743 row->extra_line_spacing = max (row->extra_line_spacing,
21744 it->max_extra_line_spacing);
21746 /* End of this display line if row is continued. */
21747 if (row->continued_p || row->ends_at_zv_p)
21748 break;
21751 at_end_of_line:
21752 /* Is this a line end? If yes, we're also done, after making
21753 sure that a non-default face is extended up to the right
21754 margin of the window. */
21755 if (ITERATOR_AT_END_OF_LINE_P (it))
21757 int used_before = row->used[TEXT_AREA];
21759 row->ends_in_newline_from_string_p = STRINGP (it->object);
21761 /* Add a space at the end of the line that is used to
21762 display the cursor there. */
21763 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21764 append_space_for_newline (it, false);
21766 /* Extend the face to the end of the line. */
21767 extend_face_to_end_of_line (it);
21769 /* Make sure we have the position. */
21770 if (used_before == 0)
21771 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21773 /* Record the position of the newline, for use in
21774 find_row_edges. */
21775 it->eol_pos = it->current.pos;
21777 /* Consume the line end. This skips over invisible lines. */
21778 set_iterator_to_next (it, true);
21779 it->continuation_lines_width = 0;
21780 break;
21783 /* Proceed with next display element. Note that this skips
21784 over lines invisible because of selective display. */
21785 set_iterator_to_next (it, true);
21787 /* If we truncate lines, we are done when the last displayed
21788 glyphs reach past the right margin of the window. */
21789 if (it->line_wrap == TRUNCATE
21790 && ((FRAME_WINDOW_P (it->f)
21791 /* Images are preprocessed in produce_image_glyph such
21792 that they are cropped at the right edge of the
21793 window, so an image glyph will always end exactly at
21794 last_visible_x, even if there's no right fringe. */
21795 && ((row->reversed_p
21796 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21797 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21798 || it->what == IT_IMAGE))
21799 ? (it->current_x >= it->last_visible_x)
21800 : (it->current_x > it->last_visible_x)))
21802 /* Maybe add truncation glyphs. */
21803 if (!FRAME_WINDOW_P (it->f)
21804 || (row->reversed_p
21805 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21806 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21808 int i, n;
21810 if (!row->reversed_p)
21812 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21813 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21814 break;
21816 else
21818 for (i = 0; i < row->used[TEXT_AREA]; i++)
21819 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21820 break;
21821 /* Remove any padding glyphs at the front of ROW, to
21822 make room for the truncation glyphs we will be
21823 adding below. The loop below always inserts at
21824 least one truncation glyph, so also remove the
21825 last glyph added to ROW. */
21826 unproduce_glyphs (it, i + 1);
21827 /* Adjust i for the loop below. */
21828 i = row->used[TEXT_AREA] - (i + 1);
21831 /* produce_special_glyphs overwrites the last glyph, so
21832 we don't want that if we want to keep that last
21833 glyph, which means it's an image. */
21834 if (it->current_x > it->last_visible_x)
21836 it->current_x = x_before;
21837 if (!FRAME_WINDOW_P (it->f))
21839 for (n = row->used[TEXT_AREA]; i < n; ++i)
21841 row->used[TEXT_AREA] = i;
21842 produce_special_glyphs (it, IT_TRUNCATION);
21845 else
21847 row->used[TEXT_AREA] = i;
21848 produce_special_glyphs (it, IT_TRUNCATION);
21850 it->hpos = hpos_before;
21853 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21855 /* Don't truncate if we can overflow newline into fringe. */
21856 if (!get_next_display_element (it))
21858 it->continuation_lines_width = 0;
21859 it->font_height = Qnil;
21860 it->voffset = 0;
21861 row->ends_at_zv_p = true;
21862 row->exact_window_width_line_p = true;
21863 break;
21865 if (ITERATOR_AT_END_OF_LINE_P (it))
21867 row->exact_window_width_line_p = true;
21868 goto at_end_of_line;
21870 it->current_x = x_before;
21871 it->hpos = hpos_before;
21874 row->truncated_on_right_p = true;
21875 it->continuation_lines_width = 0;
21876 reseat_at_next_visible_line_start (it, false);
21877 /* We insist below that IT's position be at ZV because in
21878 bidi-reordered lines the character at visible line start
21879 might not be the character that follows the newline in
21880 the logical order. */
21881 if (IT_BYTEPOS (*it) > BEG_BYTE)
21882 row->ends_at_zv_p =
21883 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21884 else
21885 row->ends_at_zv_p = false;
21886 break;
21890 if (wrap_data)
21891 bidi_unshelve_cache (wrap_data, true);
21893 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21894 at the left window margin. */
21895 if (it->first_visible_x
21896 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21898 if (!FRAME_WINDOW_P (it->f)
21899 || (((row->reversed_p
21900 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21901 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21902 /* Don't let insert_left_trunc_glyphs overwrite the
21903 first glyph of the row if it is an image. */
21904 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21905 insert_left_trunc_glyphs (it);
21906 row->truncated_on_left_p = true;
21909 /* Remember the position at which this line ends.
21911 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21912 cannot be before the call to find_row_edges below, since that is
21913 where these positions are determined. */
21914 row->end = it->current;
21915 if (!it->bidi_p)
21917 row->minpos = row->start.pos;
21918 row->maxpos = row->end.pos;
21920 else
21922 /* ROW->minpos and ROW->maxpos must be the smallest and
21923 `1 + the largest' buffer positions in ROW. But if ROW was
21924 bidi-reordered, these two positions can be anywhere in the
21925 row, so we must determine them now. */
21926 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21929 /* If the start of this line is the overlay arrow-position, then
21930 mark this glyph row as the one containing the overlay arrow.
21931 This is clearly a mess with variable size fonts. It would be
21932 better to let it be displayed like cursors under X. */
21933 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21934 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21935 !NILP (overlay_arrow_string)))
21937 /* Overlay arrow in window redisplay is a fringe bitmap. */
21938 if (STRINGP (overlay_arrow_string))
21940 struct glyph_row *arrow_row
21941 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21942 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21943 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21944 struct glyph *p = row->glyphs[TEXT_AREA];
21945 struct glyph *p2, *end;
21947 /* Copy the arrow glyphs. */
21948 while (glyph < arrow_end)
21949 *p++ = *glyph++;
21951 /* Throw away padding glyphs. */
21952 p2 = p;
21953 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21954 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21955 ++p2;
21956 if (p2 > p)
21958 while (p2 < end)
21959 *p++ = *p2++;
21960 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21963 else
21965 eassert (INTEGERP (overlay_arrow_string));
21966 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21968 overlay_arrow_seen = true;
21971 /* Highlight trailing whitespace. */
21972 if (!NILP (Vshow_trailing_whitespace))
21973 highlight_trailing_whitespace (it->f, it->glyph_row);
21975 /* Compute pixel dimensions of this line. */
21976 compute_line_metrics (it);
21978 /* Implementation note: No changes in the glyphs of ROW or in their
21979 faces can be done past this point, because compute_line_metrics
21980 computes ROW's hash value and stores it within the glyph_row
21981 structure. */
21983 /* Record whether this row ends inside an ellipsis. */
21984 row->ends_in_ellipsis_p
21985 = (it->method == GET_FROM_DISPLAY_VECTOR
21986 && it->ellipsis_p);
21988 /* Save fringe bitmaps in this row. */
21989 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21990 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21991 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21992 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21994 it->left_user_fringe_bitmap = 0;
21995 it->left_user_fringe_face_id = 0;
21996 it->right_user_fringe_bitmap = 0;
21997 it->right_user_fringe_face_id = 0;
21999 /* Maybe set the cursor. */
22000 cvpos = it->w->cursor.vpos;
22001 if ((cvpos < 0
22002 /* In bidi-reordered rows, keep checking for proper cursor
22003 position even if one has been found already, because buffer
22004 positions in such rows change non-linearly with ROW->VPOS,
22005 when a line is continued. One exception: when we are at ZV,
22006 display cursor on the first suitable glyph row, since all
22007 the empty rows after that also have their position set to ZV. */
22008 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22009 lines' rows is implemented for bidi-reordered rows. */
22010 || (it->bidi_p
22011 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22012 && PT >= MATRIX_ROW_START_CHARPOS (row)
22013 && PT <= MATRIX_ROW_END_CHARPOS (row)
22014 && cursor_row_p (row))
22015 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22017 /* Prepare for the next line. This line starts horizontally at (X
22018 HPOS) = (0 0). Vertical positions are incremented. As a
22019 convenience for the caller, IT->glyph_row is set to the next
22020 row to be used. */
22021 it->current_x = it->hpos = 0;
22022 it->current_y += row->height;
22023 /* Restore the first and last visible X if we adjusted them for
22024 current-line hscrolling. */
22025 if (hscroll_this_line)
22027 it->first_visible_x = first_visible_x;
22028 it->last_visible_x = last_visible_x;
22030 SET_TEXT_POS (it->eol_pos, 0, 0);
22031 ++it->vpos;
22032 ++it->glyph_row;
22033 /* The next row should by default use the same value of the
22034 reversed_p flag as this one. set_iterator_to_next decides when
22035 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22036 the flag accordingly. */
22037 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22038 it->glyph_row->reversed_p = row->reversed_p;
22039 it->start = row->end;
22040 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22042 #undef RECORD_MAX_MIN_POS
22045 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22046 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22047 doc: /* Return paragraph direction at point in BUFFER.
22048 Value is either `left-to-right' or `right-to-left'.
22049 If BUFFER is omitted or nil, it defaults to the current buffer.
22051 Paragraph direction determines how the text in the paragraph is displayed.
22052 In left-to-right paragraphs, text begins at the left margin of the window
22053 and the reading direction is generally left to right. In right-to-left
22054 paragraphs, text begins at the right margin and is read from right to left.
22056 See also `bidi-paragraph-direction'. */)
22057 (Lisp_Object buffer)
22059 struct buffer *buf = current_buffer;
22060 struct buffer *old = buf;
22062 if (! NILP (buffer))
22064 CHECK_BUFFER (buffer);
22065 buf = XBUFFER (buffer);
22068 if (NILP (BVAR (buf, bidi_display_reordering))
22069 || NILP (BVAR (buf, enable_multibyte_characters))
22070 /* When we are loading loadup.el, the character property tables
22071 needed for bidi iteration are not yet available. */
22072 || redisplay__inhibit_bidi)
22073 return Qleft_to_right;
22074 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22075 return BVAR (buf, bidi_paragraph_direction);
22076 else
22078 /* Determine the direction from buffer text. We could try to
22079 use current_matrix if it is up to date, but this seems fast
22080 enough as it is. */
22081 struct bidi_it itb;
22082 ptrdiff_t pos = BUF_PT (buf);
22083 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22084 int c;
22085 void *itb_data = bidi_shelve_cache ();
22087 set_buffer_temp (buf);
22088 /* bidi_paragraph_init finds the base direction of the paragraph
22089 by searching forward from paragraph start. We need the base
22090 direction of the current or _previous_ paragraph, so we need
22091 to make sure we are within that paragraph. To that end, find
22092 the previous non-empty line. */
22093 if (pos >= ZV && pos > BEGV)
22094 DEC_BOTH (pos, bytepos);
22095 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22096 if (fast_looking_at (trailing_white_space,
22097 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22099 while ((c = FETCH_BYTE (bytepos)) == '\n'
22100 || c == ' ' || c == '\t' || c == '\f')
22102 if (bytepos <= BEGV_BYTE)
22103 break;
22104 bytepos--;
22105 pos--;
22107 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22108 bytepos--;
22110 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22111 itb.paragraph_dir = NEUTRAL_DIR;
22112 itb.string.s = NULL;
22113 itb.string.lstring = Qnil;
22114 itb.string.bufpos = 0;
22115 itb.string.from_disp_str = false;
22116 itb.string.unibyte = false;
22117 /* We have no window to use here for ignoring window-specific
22118 overlays. Using NULL for window pointer will cause
22119 compute_display_string_pos to use the current buffer. */
22120 itb.w = NULL;
22121 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22122 bidi_unshelve_cache (itb_data, false);
22123 set_buffer_temp (old);
22124 switch (itb.paragraph_dir)
22126 case L2R:
22127 return Qleft_to_right;
22128 break;
22129 case R2L:
22130 return Qright_to_left;
22131 break;
22132 default:
22133 emacs_abort ();
22138 DEFUN ("bidi-find-overridden-directionality",
22139 Fbidi_find_overridden_directionality,
22140 Sbidi_find_overridden_directionality, 2, 3, 0,
22141 doc: /* Return position between FROM and TO where directionality was overridden.
22143 This function returns the first character position in the specified
22144 region of OBJECT where there is a character whose `bidi-class' property
22145 is `L', but which was forced to display as `R' by a directional
22146 override, and likewise with characters whose `bidi-class' is `R'
22147 or `AL' that were forced to display as `L'.
22149 If no such character is found, the function returns nil.
22151 OBJECT is a Lisp string or buffer to search for overridden
22152 directionality, and defaults to the current buffer if nil or omitted.
22153 OBJECT can also be a window, in which case the function will search
22154 the buffer displayed in that window. Passing the window instead of
22155 a buffer is preferable when the buffer is displayed in some window,
22156 because this function will then be able to correctly account for
22157 window-specific overlays, which can affect the results.
22159 Strong directional characters `L', `R', and `AL' can have their
22160 intrinsic directionality overridden by directional override
22161 control characters RLO (u+202e) and LRO (u+202d). See the
22162 function `get-char-code-property' for a way to inquire about
22163 the `bidi-class' property of a character. */)
22164 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22166 struct buffer *buf = current_buffer;
22167 struct buffer *old = buf;
22168 struct window *w = NULL;
22169 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22170 struct bidi_it itb;
22171 ptrdiff_t from_pos, to_pos, from_bpos;
22172 void *itb_data;
22174 if (!NILP (object))
22176 if (BUFFERP (object))
22177 buf = XBUFFER (object);
22178 else if (WINDOWP (object))
22180 w = decode_live_window (object);
22181 buf = XBUFFER (w->contents);
22182 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22184 else
22185 CHECK_STRING (object);
22188 if (STRINGP (object))
22190 /* Characters in unibyte strings are always treated by bidi.c as
22191 strong LTR. */
22192 if (!STRING_MULTIBYTE (object)
22193 /* When we are loading loadup.el, the character property
22194 tables needed for bidi iteration are not yet
22195 available. */
22196 || redisplay__inhibit_bidi)
22197 return Qnil;
22199 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22200 if (from_pos >= SCHARS (object))
22201 return Qnil;
22203 /* Set up the bidi iterator. */
22204 itb_data = bidi_shelve_cache ();
22205 itb.paragraph_dir = NEUTRAL_DIR;
22206 itb.string.lstring = object;
22207 itb.string.s = NULL;
22208 itb.string.schars = SCHARS (object);
22209 itb.string.bufpos = 0;
22210 itb.string.from_disp_str = false;
22211 itb.string.unibyte = false;
22212 itb.w = w;
22213 bidi_init_it (0, 0, frame_window_p, &itb);
22215 else
22217 /* Nothing this fancy can happen in unibyte buffers, or in a
22218 buffer that disabled reordering, or if FROM is at EOB. */
22219 if (NILP (BVAR (buf, bidi_display_reordering))
22220 || NILP (BVAR (buf, enable_multibyte_characters))
22221 /* When we are loading loadup.el, the character property
22222 tables needed for bidi iteration are not yet
22223 available. */
22224 || redisplay__inhibit_bidi)
22225 return Qnil;
22227 set_buffer_temp (buf);
22228 validate_region (&from, &to);
22229 from_pos = XINT (from);
22230 to_pos = XINT (to);
22231 if (from_pos >= ZV)
22232 return Qnil;
22234 /* Set up the bidi iterator. */
22235 itb_data = bidi_shelve_cache ();
22236 from_bpos = CHAR_TO_BYTE (from_pos);
22237 if (from_pos == BEGV)
22239 itb.charpos = BEGV;
22240 itb.bytepos = BEGV_BYTE;
22242 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22244 itb.charpos = from_pos;
22245 itb.bytepos = from_bpos;
22247 else
22248 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22249 -1, &itb.bytepos);
22250 itb.paragraph_dir = NEUTRAL_DIR;
22251 itb.string.s = NULL;
22252 itb.string.lstring = Qnil;
22253 itb.string.bufpos = 0;
22254 itb.string.from_disp_str = false;
22255 itb.string.unibyte = false;
22256 itb.w = w;
22257 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22260 ptrdiff_t found;
22261 do {
22262 /* For the purposes of this function, the actual base direction of
22263 the paragraph doesn't matter, so just set it to L2R. */
22264 bidi_paragraph_init (L2R, &itb, false);
22265 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22267 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22269 bidi_unshelve_cache (itb_data, false);
22270 set_buffer_temp (old);
22272 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22275 DEFUN ("move-point-visually", Fmove_point_visually,
22276 Smove_point_visually, 1, 1, 0,
22277 doc: /* Move point in the visual order in the specified DIRECTION.
22278 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22279 left.
22281 Value is the new character position of point. */)
22282 (Lisp_Object direction)
22284 struct window *w = XWINDOW (selected_window);
22285 struct buffer *b = XBUFFER (w->contents);
22286 struct glyph_row *row;
22287 int dir;
22288 Lisp_Object paragraph_dir;
22290 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22291 (!(ROW)->continued_p \
22292 && NILP ((GLYPH)->object) \
22293 && (GLYPH)->type == CHAR_GLYPH \
22294 && (GLYPH)->u.ch == ' ' \
22295 && (GLYPH)->charpos >= 0 \
22296 && !(GLYPH)->avoid_cursor_p)
22298 CHECK_NUMBER (direction);
22299 dir = XINT (direction);
22300 if (dir > 0)
22301 dir = 1;
22302 else
22303 dir = -1;
22305 /* If current matrix is up-to-date, we can use the information
22306 recorded in the glyphs, at least as long as the goal is on the
22307 screen. */
22308 if (w->window_end_valid
22309 && !windows_or_buffers_changed
22310 && b
22311 && !b->clip_changed
22312 && !b->prevent_redisplay_optimizations_p
22313 && !window_outdated (w)
22314 /* We rely below on the cursor coordinates to be up to date, but
22315 we cannot trust them if some command moved point since the
22316 last complete redisplay. */
22317 && w->last_point == BUF_PT (b)
22318 && w->cursor.vpos >= 0
22319 && w->cursor.vpos < w->current_matrix->nrows
22320 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22322 struct glyph *g = row->glyphs[TEXT_AREA];
22323 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22324 struct glyph *gpt = g + w->cursor.hpos;
22326 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22328 if (BUFFERP (g->object) && g->charpos != PT)
22330 SET_PT (g->charpos);
22331 w->cursor.vpos = -1;
22332 return make_number (PT);
22334 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22336 ptrdiff_t new_pos;
22338 if (BUFFERP (gpt->object))
22340 new_pos = PT;
22341 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22342 new_pos += (row->reversed_p ? -dir : dir);
22343 else
22344 new_pos -= (row->reversed_p ? -dir : dir);
22346 else if (BUFFERP (g->object))
22347 new_pos = g->charpos;
22348 else
22349 break;
22350 SET_PT (new_pos);
22351 w->cursor.vpos = -1;
22352 return make_number (PT);
22354 else if (ROW_GLYPH_NEWLINE_P (row, g))
22356 /* Glyphs inserted at the end of a non-empty line for
22357 positioning the cursor have zero charpos, so we must
22358 deduce the value of point by other means. */
22359 if (g->charpos > 0)
22360 SET_PT (g->charpos);
22361 else if (row->ends_at_zv_p && PT != ZV)
22362 SET_PT (ZV);
22363 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22364 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22365 else
22366 break;
22367 w->cursor.vpos = -1;
22368 return make_number (PT);
22371 if (g == e || NILP (g->object))
22373 if (row->truncated_on_left_p || row->truncated_on_right_p)
22374 goto simulate_display;
22375 if (!row->reversed_p)
22376 row += dir;
22377 else
22378 row -= dir;
22379 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
22380 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
22381 goto simulate_display;
22383 if (dir > 0)
22385 if (row->reversed_p && !row->continued_p)
22387 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22388 w->cursor.vpos = -1;
22389 return make_number (PT);
22391 g = row->glyphs[TEXT_AREA];
22392 e = g + row->used[TEXT_AREA];
22393 for ( ; g < e; g++)
22395 if (BUFFERP (g->object)
22396 /* Empty lines have only one glyph, which stands
22397 for the newline, and whose charpos is the
22398 buffer position of the newline. */
22399 || ROW_GLYPH_NEWLINE_P (row, g)
22400 /* When the buffer ends in a newline, the line at
22401 EOB also has one glyph, but its charpos is -1. */
22402 || (row->ends_at_zv_p
22403 && !row->reversed_p
22404 && NILP (g->object)
22405 && g->type == CHAR_GLYPH
22406 && g->u.ch == ' '))
22408 if (g->charpos > 0)
22409 SET_PT (g->charpos);
22410 else if (!row->reversed_p
22411 && row->ends_at_zv_p
22412 && PT != ZV)
22413 SET_PT (ZV);
22414 else
22415 continue;
22416 w->cursor.vpos = -1;
22417 return make_number (PT);
22421 else
22423 if (!row->reversed_p && !row->continued_p)
22425 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22426 w->cursor.vpos = -1;
22427 return make_number (PT);
22429 e = row->glyphs[TEXT_AREA];
22430 g = e + row->used[TEXT_AREA] - 1;
22431 for ( ; g >= e; g--)
22433 if (BUFFERP (g->object)
22434 || (ROW_GLYPH_NEWLINE_P (row, g)
22435 && g->charpos > 0)
22436 /* Empty R2L lines on GUI frames have the buffer
22437 position of the newline stored in the stretch
22438 glyph. */
22439 || g->type == STRETCH_GLYPH
22440 || (row->ends_at_zv_p
22441 && row->reversed_p
22442 && NILP (g->object)
22443 && g->type == CHAR_GLYPH
22444 && g->u.ch == ' '))
22446 if (g->charpos > 0)
22447 SET_PT (g->charpos);
22448 else if (row->reversed_p
22449 && row->ends_at_zv_p
22450 && PT != ZV)
22451 SET_PT (ZV);
22452 else
22453 continue;
22454 w->cursor.vpos = -1;
22455 return make_number (PT);
22462 simulate_display:
22464 /* If we wind up here, we failed to move by using the glyphs, so we
22465 need to simulate display instead. */
22467 if (b)
22468 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22469 else
22470 paragraph_dir = Qleft_to_right;
22471 if (EQ (paragraph_dir, Qright_to_left))
22472 dir = -dir;
22473 if (PT <= BEGV && dir < 0)
22474 xsignal0 (Qbeginning_of_buffer);
22475 else if (PT >= ZV && dir > 0)
22476 xsignal0 (Qend_of_buffer);
22477 else
22479 struct text_pos pt;
22480 struct it it;
22481 int pt_x, target_x, pixel_width, pt_vpos;
22482 bool at_eol_p;
22483 bool overshoot_expected = false;
22484 bool target_is_eol_p = false;
22486 /* Setup the arena. */
22487 SET_TEXT_POS (pt, PT, PT_BYTE);
22488 start_display (&it, w, pt);
22489 /* When lines are truncated, we could be called with point
22490 outside of the windows edges, in which case move_it_*
22491 functions either prematurely stop at window's edge or jump to
22492 the next screen line, whereas we rely below on our ability to
22493 reach point, in order to start from its X coordinate. So we
22494 need to disregard the window's horizontal extent in that case. */
22495 if (it.line_wrap == TRUNCATE)
22496 it.last_visible_x = DISP_INFINITY;
22498 if (it.cmp_it.id < 0
22499 && it.method == GET_FROM_STRING
22500 && it.area == TEXT_AREA
22501 && it.string_from_display_prop_p
22502 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22503 overshoot_expected = true;
22505 /* Find the X coordinate of point. We start from the beginning
22506 of this or previous line to make sure we are before point in
22507 the logical order (since the move_it_* functions can only
22508 move forward). */
22509 reseat:
22510 reseat_at_previous_visible_line_start (&it);
22511 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22512 if (IT_CHARPOS (it) != PT)
22514 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22515 -1, -1, -1, MOVE_TO_POS);
22516 /* If we missed point because the character there is
22517 displayed out of a display vector that has more than one
22518 glyph, retry expecting overshoot. */
22519 if (it.method == GET_FROM_DISPLAY_VECTOR
22520 && it.current.dpvec_index > 0
22521 && !overshoot_expected)
22523 overshoot_expected = true;
22524 goto reseat;
22526 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22527 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22529 pt_x = it.current_x;
22530 pt_vpos = it.vpos;
22531 if (dir > 0 || overshoot_expected)
22533 struct glyph_row *row = it.glyph_row;
22535 /* When point is at beginning of line, we don't have
22536 information about the glyph there loaded into struct
22537 it. Calling get_next_display_element fixes that. */
22538 if (pt_x == 0)
22539 get_next_display_element (&it);
22540 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22541 it.glyph_row = NULL;
22542 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22543 it.glyph_row = row;
22544 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22545 it, lest it will become out of sync with it's buffer
22546 position. */
22547 it.current_x = pt_x;
22549 else
22550 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22551 pixel_width = it.pixel_width;
22552 if (overshoot_expected && at_eol_p)
22553 pixel_width = 0;
22554 else if (pixel_width <= 0)
22555 pixel_width = 1;
22557 /* If there's a display string (or something similar) at point,
22558 we are actually at the glyph to the left of point, so we need
22559 to correct the X coordinate. */
22560 if (overshoot_expected)
22562 if (it.bidi_p)
22563 pt_x += pixel_width * it.bidi_it.scan_dir;
22564 else
22565 pt_x += pixel_width;
22568 /* Compute target X coordinate, either to the left or to the
22569 right of point. On TTY frames, all characters have the same
22570 pixel width of 1, so we can use that. On GUI frames we don't
22571 have an easy way of getting at the pixel width of the
22572 character to the left of point, so we use a different method
22573 of getting to that place. */
22574 if (dir > 0)
22575 target_x = pt_x + pixel_width;
22576 else
22577 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22579 /* Target X coordinate could be one line above or below the line
22580 of point, in which case we need to adjust the target X
22581 coordinate. Also, if moving to the left, we need to begin at
22582 the left edge of the point's screen line. */
22583 if (dir < 0)
22585 if (pt_x > 0)
22587 start_display (&it, w, pt);
22588 if (it.line_wrap == TRUNCATE)
22589 it.last_visible_x = DISP_INFINITY;
22590 reseat_at_previous_visible_line_start (&it);
22591 it.current_x = it.current_y = it.hpos = 0;
22592 if (pt_vpos != 0)
22593 move_it_by_lines (&it, pt_vpos);
22595 else
22597 move_it_by_lines (&it, -1);
22598 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22599 target_is_eol_p = true;
22600 /* Under word-wrap, we don't know the x coordinate of
22601 the last character displayed on the previous line,
22602 which immediately precedes the wrap point. To find
22603 out its x coordinate, we try moving to the right
22604 margin of the window, which will stop at the wrap
22605 point, and then reset target_x to point at the
22606 character that precedes the wrap point. This is not
22607 needed on GUI frames, because (see below) there we
22608 move from the left margin one grapheme cluster at a
22609 time, and stop when we hit the wrap point. */
22610 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22612 void *it_data = NULL;
22613 struct it it2;
22615 SAVE_IT (it2, it, it_data);
22616 move_it_in_display_line_to (&it, ZV, target_x,
22617 MOVE_TO_POS | MOVE_TO_X);
22618 /* If we arrived at target_x, that _is_ the last
22619 character on the previous line. */
22620 if (it.current_x != target_x)
22621 target_x = it.current_x - 1;
22622 RESTORE_IT (&it, &it2, it_data);
22626 else
22628 if (at_eol_p
22629 || (target_x >= it.last_visible_x
22630 && it.line_wrap != TRUNCATE))
22632 if (pt_x > 0)
22633 move_it_by_lines (&it, 0);
22634 move_it_by_lines (&it, 1);
22635 target_x = 0;
22639 /* Move to the target X coordinate. */
22640 /* On GUI frames, as we don't know the X coordinate of the
22641 character to the left of point, moving point to the left
22642 requires walking, one grapheme cluster at a time, until we
22643 find ourself at a place immediately to the left of the
22644 character at point. */
22645 if (FRAME_WINDOW_P (it.f) && dir < 0)
22647 struct text_pos new_pos;
22648 enum move_it_result rc = MOVE_X_REACHED;
22650 if (it.current_x == 0)
22651 get_next_display_element (&it);
22652 if (it.what == IT_COMPOSITION)
22654 new_pos.charpos = it.cmp_it.charpos;
22655 new_pos.bytepos = -1;
22657 else
22658 new_pos = it.current.pos;
22660 while (it.current_x + it.pixel_width <= target_x
22661 && (rc == MOVE_X_REACHED
22662 /* Under word-wrap, move_it_in_display_line_to
22663 stops at correct coordinates, but sometimes
22664 returns MOVE_POS_MATCH_OR_ZV. */
22665 || (it.line_wrap == WORD_WRAP
22666 && rc == MOVE_POS_MATCH_OR_ZV)))
22668 int new_x = it.current_x + it.pixel_width;
22670 /* For composed characters, we want the position of the
22671 first character in the grapheme cluster (usually, the
22672 composition's base character), whereas it.current
22673 might give us the position of the _last_ one, e.g. if
22674 the composition is rendered in reverse due to bidi
22675 reordering. */
22676 if (it.what == IT_COMPOSITION)
22678 new_pos.charpos = it.cmp_it.charpos;
22679 new_pos.bytepos = -1;
22681 else
22682 new_pos = it.current.pos;
22683 if (new_x == it.current_x)
22684 new_x++;
22685 rc = move_it_in_display_line_to (&it, ZV, new_x,
22686 MOVE_TO_POS | MOVE_TO_X);
22687 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22688 break;
22690 /* The previous position we saw in the loop is the one we
22691 want. */
22692 if (new_pos.bytepos == -1)
22693 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22694 it.current.pos = new_pos;
22696 else if (it.current_x != target_x)
22697 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22699 /* If we ended up in a display string that covers point, move to
22700 buffer position to the right in the visual order. */
22701 if (dir > 0)
22703 while (IT_CHARPOS (it) == PT)
22705 set_iterator_to_next (&it, false);
22706 if (!get_next_display_element (&it))
22707 break;
22711 /* Move point to that position. */
22712 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22715 return make_number (PT);
22717 #undef ROW_GLYPH_NEWLINE_P
22720 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22721 Sbidi_resolved_levels, 0, 1, 0,
22722 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22724 The resolved levels are produced by the Emacs bidi reordering engine
22725 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22726 read the Unicode Standard Annex 9 (UAX#9) for background information
22727 about these levels.
22729 VPOS is the zero-based number of the current window's screen line
22730 for which to produce the resolved levels. If VPOS is nil or omitted,
22731 it defaults to the screen line of point. If the window displays a
22732 header line, VPOS of zero will report on the header line, and first
22733 line of text in the window will have VPOS of 1.
22735 Value is an array of resolved levels, indexed by glyph number.
22736 Glyphs are numbered from zero starting from the beginning of the
22737 screen line, i.e. the left edge of the window for left-to-right lines
22738 and from the right edge for right-to-left lines. The resolved levels
22739 are produced only for the window's text area; text in display margins
22740 is not included.
22742 If the selected window's display is not up-to-date, or if the specified
22743 screen line does not display text, this function returns nil. It is
22744 highly recommended to bind this function to some simple key, like F8,
22745 in order to avoid these problems.
22747 This function exists mainly for testing the correctness of the
22748 Emacs UBA implementation, in particular with the test suite. */)
22749 (Lisp_Object vpos)
22751 struct window *w = XWINDOW (selected_window);
22752 struct buffer *b = XBUFFER (w->contents);
22753 int nrow;
22754 struct glyph_row *row;
22756 if (NILP (vpos))
22758 int d1, d2, d3, d4, d5;
22760 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22762 else
22764 CHECK_NUMBER_COERCE_MARKER (vpos);
22765 nrow = XINT (vpos);
22768 /* We require up-to-date glyph matrix for this window. */
22769 if (w->window_end_valid
22770 && !windows_or_buffers_changed
22771 && b
22772 && !b->clip_changed
22773 && !b->prevent_redisplay_optimizations_p
22774 && !window_outdated (w)
22775 && nrow >= 0
22776 && nrow < w->current_matrix->nrows
22777 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22778 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22780 struct glyph *g, *e, *g1;
22781 int nglyphs, i;
22782 Lisp_Object levels;
22784 if (!row->reversed_p) /* Left-to-right glyph row. */
22786 g = g1 = row->glyphs[TEXT_AREA];
22787 e = g + row->used[TEXT_AREA];
22789 /* Skip over glyphs at the start of the row that was
22790 generated by redisplay for its own needs. */
22791 while (g < e
22792 && NILP (g->object)
22793 && g->charpos < 0)
22794 g++;
22795 g1 = g;
22797 /* Count the "interesting" glyphs in this row. */
22798 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22799 nglyphs++;
22801 /* Create and fill the array. */
22802 levels = make_uninit_vector (nglyphs);
22803 for (i = 0; g1 < g; i++, g1++)
22804 ASET (levels, i, make_number (g1->resolved_level));
22806 else /* Right-to-left glyph row. */
22808 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22809 e = row->glyphs[TEXT_AREA] - 1;
22810 while (g > e
22811 && NILP (g->object)
22812 && g->charpos < 0)
22813 g--;
22814 g1 = g;
22815 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22816 nglyphs++;
22817 levels = make_uninit_vector (nglyphs);
22818 for (i = 0; g1 > g; i++, g1--)
22819 ASET (levels, i, make_number (g1->resolved_level));
22821 return levels;
22823 else
22824 return Qnil;
22829 /***********************************************************************
22830 Menu Bar
22831 ***********************************************************************/
22833 /* Redisplay the menu bar in the frame for window W.
22835 The menu bar of X frames that don't have X toolkit support is
22836 displayed in a special window W->frame->menu_bar_window.
22838 The menu bar of terminal frames is treated specially as far as
22839 glyph matrices are concerned. Menu bar lines are not part of
22840 windows, so the update is done directly on the frame matrix rows
22841 for the menu bar. */
22843 static void
22844 display_menu_bar (struct window *w)
22846 struct frame *f = XFRAME (WINDOW_FRAME (w));
22847 struct it it;
22848 Lisp_Object items;
22849 int i;
22851 /* Don't do all this for graphical frames. */
22852 #ifdef HAVE_NTGUI
22853 if (FRAME_W32_P (f))
22854 return;
22855 #endif
22856 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22857 if (FRAME_X_P (f))
22858 return;
22859 #endif
22861 #ifdef HAVE_NS
22862 if (FRAME_NS_P (f))
22863 return;
22864 #endif /* HAVE_NS */
22866 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22867 eassert (!FRAME_WINDOW_P (f));
22868 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22869 it.first_visible_x = 0;
22870 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22871 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22872 if (FRAME_WINDOW_P (f))
22874 /* Menu bar lines are displayed in the desired matrix of the
22875 dummy window menu_bar_window. */
22876 struct window *menu_w;
22877 menu_w = XWINDOW (f->menu_bar_window);
22878 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22879 MENU_FACE_ID);
22880 it.first_visible_x = 0;
22881 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22883 else
22884 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22886 /* This is a TTY frame, i.e. character hpos/vpos are used as
22887 pixel x/y. */
22888 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22889 MENU_FACE_ID);
22890 it.first_visible_x = 0;
22891 it.last_visible_x = FRAME_COLS (f);
22894 /* FIXME: This should be controlled by a user option. See the
22895 comments in redisplay_tool_bar and display_mode_line about
22896 this. */
22897 it.paragraph_embedding = L2R;
22899 /* Clear all rows of the menu bar. */
22900 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22902 struct glyph_row *row = it.glyph_row + i;
22903 clear_glyph_row (row);
22904 row->enabled_p = true;
22905 row->full_width_p = true;
22906 row->reversed_p = false;
22909 /* Display all items of the menu bar. */
22910 items = FRAME_MENU_BAR_ITEMS (it.f);
22911 for (i = 0; i < ASIZE (items); i += 4)
22913 Lisp_Object string;
22915 /* Stop at nil string. */
22916 string = AREF (items, i + 1);
22917 if (NILP (string))
22918 break;
22920 /* Remember where item was displayed. */
22921 ASET (items, i + 3, make_number (it.hpos));
22923 /* Display the item, pad with one space. */
22924 if (it.current_x < it.last_visible_x)
22925 display_string (NULL, string, Qnil, 0, 0, &it,
22926 SCHARS (string) + 1, 0, 0, -1);
22929 /* Fill out the line with spaces. */
22930 if (it.current_x < it.last_visible_x)
22931 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22933 /* Compute the total height of the lines. */
22934 compute_line_metrics (&it);
22937 /* Deep copy of a glyph row, including the glyphs. */
22938 static void
22939 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22941 struct glyph *pointers[1 + LAST_AREA];
22942 int to_used = to->used[TEXT_AREA];
22944 /* Save glyph pointers of TO. */
22945 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22947 /* Do a structure assignment. */
22948 *to = *from;
22950 /* Restore original glyph pointers of TO. */
22951 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22953 /* Copy the glyphs. */
22954 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22955 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22957 /* If we filled only part of the TO row, fill the rest with
22958 space_glyph (which will display as empty space). */
22959 if (to_used > from->used[TEXT_AREA])
22960 fill_up_frame_row_with_spaces (to, to_used);
22963 /* Display one menu item on a TTY, by overwriting the glyphs in the
22964 frame F's desired glyph matrix with glyphs produced from the menu
22965 item text. Called from term.c to display TTY drop-down menus one
22966 item at a time.
22968 ITEM_TEXT is the menu item text as a C string.
22970 FACE_ID is the face ID to be used for this menu item. FACE_ID
22971 could specify one of 3 faces: a face for an enabled item, a face
22972 for a disabled item, or a face for a selected item.
22974 X and Y are coordinates of the first glyph in the frame's desired
22975 matrix to be overwritten by the menu item. Since this is a TTY, Y
22976 is the zero-based number of the glyph row and X is the zero-based
22977 glyph number in the row, starting from left, where to start
22978 displaying the item.
22980 SUBMENU means this menu item drops down a submenu, which
22981 should be indicated by displaying a proper visual cue after the
22982 item text. */
22984 void
22985 display_tty_menu_item (const char *item_text, int width, int face_id,
22986 int x, int y, bool submenu)
22988 struct it it;
22989 struct frame *f = SELECTED_FRAME ();
22990 struct window *w = XWINDOW (f->selected_window);
22991 struct glyph_row *row;
22992 size_t item_len = strlen (item_text);
22994 eassert (FRAME_TERMCAP_P (f));
22996 /* Don't write beyond the matrix's last row. This can happen for
22997 TTY screens that are not high enough to show the entire menu.
22998 (This is actually a bit of defensive programming, as
22999 tty_menu_display already limits the number of menu items to one
23000 less than the number of screen lines.) */
23001 if (y >= f->desired_matrix->nrows)
23002 return;
23004 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23005 it.first_visible_x = 0;
23006 it.last_visible_x = FRAME_COLS (f) - 1;
23007 row = it.glyph_row;
23008 /* Start with the row contents from the current matrix. */
23009 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23010 bool saved_width = row->full_width_p;
23011 row->full_width_p = true;
23012 bool saved_reversed = row->reversed_p;
23013 row->reversed_p = false;
23014 row->enabled_p = true;
23016 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23017 desired face. */
23018 eassert (x < f->desired_matrix->matrix_w);
23019 it.current_x = it.hpos = x;
23020 it.current_y = it.vpos = y;
23021 int saved_used = row->used[TEXT_AREA];
23022 bool saved_truncated = row->truncated_on_right_p;
23023 row->used[TEXT_AREA] = x;
23024 it.face_id = face_id;
23025 it.line_wrap = TRUNCATE;
23027 /* FIXME: This should be controlled by a user option. See the
23028 comments in redisplay_tool_bar and display_mode_line about this.
23029 Also, if paragraph_embedding could ever be R2L, changes will be
23030 needed to avoid shifting to the right the row characters in
23031 term.c:append_glyph. */
23032 it.paragraph_embedding = L2R;
23034 /* Pad with a space on the left. */
23035 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23036 width--;
23037 /* Display the menu item, pad with spaces to WIDTH. */
23038 if (submenu)
23040 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23041 item_len, 0, FRAME_COLS (f) - 1, -1);
23042 width -= item_len;
23043 /* Indicate with " >" that there's a submenu. */
23044 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23045 FRAME_COLS (f) - 1, -1);
23047 else
23048 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23049 width, 0, FRAME_COLS (f) - 1, -1);
23051 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23052 row->truncated_on_right_p = saved_truncated;
23053 row->hash = row_hash (row);
23054 row->full_width_p = saved_width;
23055 row->reversed_p = saved_reversed;
23058 /***********************************************************************
23059 Mode Line
23060 ***********************************************************************/
23062 /* Redisplay mode lines in the window tree whose root is WINDOW.
23063 If FORCE, redisplay mode lines unconditionally.
23064 Otherwise, redisplay only mode lines that are garbaged. Value is
23065 the number of windows whose mode lines were redisplayed. */
23067 static int
23068 redisplay_mode_lines (Lisp_Object window, bool force)
23070 int nwindows = 0;
23072 while (!NILP (window))
23074 struct window *w = XWINDOW (window);
23076 if (WINDOWP (w->contents))
23077 nwindows += redisplay_mode_lines (w->contents, force);
23078 else if (force
23079 || FRAME_GARBAGED_P (XFRAME (w->frame))
23080 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23082 struct text_pos lpoint;
23083 struct buffer *old = current_buffer;
23085 /* Set the window's buffer for the mode line display. */
23086 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23087 set_buffer_internal_1 (XBUFFER (w->contents));
23089 /* Point refers normally to the selected window. For any
23090 other window, set up appropriate value. */
23091 if (!EQ (window, selected_window))
23093 struct text_pos pt;
23095 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23096 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23099 /* Display mode lines. */
23100 clear_glyph_matrix (w->desired_matrix);
23101 if (display_mode_lines (w))
23102 ++nwindows;
23104 /* Restore old settings. */
23105 set_buffer_internal_1 (old);
23106 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23109 window = w->next;
23112 return nwindows;
23116 /* Display the mode and/or header line of window W. Value is the
23117 sum number of mode lines and header lines displayed. */
23119 static int
23120 display_mode_lines (struct window *w)
23122 Lisp_Object old_selected_window = selected_window;
23123 Lisp_Object old_selected_frame = selected_frame;
23124 Lisp_Object new_frame = w->frame;
23125 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23126 int n = 0;
23128 selected_frame = new_frame;
23129 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23130 or window's point, then we'd need select_window_1 here as well. */
23131 XSETWINDOW (selected_window, w);
23132 XFRAME (new_frame)->selected_window = selected_window;
23134 /* These will be set while the mode line specs are processed. */
23135 line_number_displayed = false;
23136 w->column_number_displayed = -1;
23138 if (window_wants_mode_line (w))
23140 Lisp_Object window_mode_line_format
23141 = window_parameter (w, Qmode_line_format);
23143 struct window *sel_w = XWINDOW (old_selected_window);
23145 /* Select mode line face based on the real selected window. */
23146 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23147 NILP (window_mode_line_format)
23148 ? BVAR (current_buffer, mode_line_format)
23149 : window_mode_line_format);
23150 ++n;
23153 if (window_wants_header_line (w))
23155 Lisp_Object window_header_line_format
23156 = window_parameter (w, Qheader_line_format);
23158 display_mode_line (w, HEADER_LINE_FACE_ID,
23159 NILP (window_header_line_format)
23160 ? BVAR (current_buffer, header_line_format)
23161 : window_header_line_format);
23162 ++n;
23165 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23166 selected_frame = old_selected_frame;
23167 selected_window = old_selected_window;
23168 if (n > 0)
23169 w->must_be_updated_p = true;
23170 return n;
23174 /* Display mode or header line of window W. FACE_ID specifies which
23175 line to display; it is either MODE_LINE_FACE_ID or
23176 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23177 display. Value is the pixel height of the mode/header line
23178 displayed. */
23180 static int
23181 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23183 struct it it;
23184 struct face *face;
23185 ptrdiff_t count = SPECPDL_INDEX ();
23187 init_iterator (&it, w, -1, -1, NULL, face_id);
23188 /* Don't extend on a previously drawn mode-line.
23189 This may happen if called from pos_visible_p. */
23190 it.glyph_row->enabled_p = false;
23191 prepare_desired_row (w, it.glyph_row, true);
23193 it.glyph_row->mode_line_p = true;
23195 /* FIXME: This should be controlled by a user option. But
23196 supporting such an option is not trivial, since the mode line is
23197 made up of many separate strings. */
23198 it.paragraph_embedding = L2R;
23200 record_unwind_protect (unwind_format_mode_line,
23201 format_mode_line_unwind_data (NULL, NULL,
23202 Qnil, false));
23204 mode_line_target = MODE_LINE_DISPLAY;
23206 /* Temporarily make frame's keyboard the current kboard so that
23207 kboard-local variables in the mode_line_format will get the right
23208 values. */
23209 push_kboard (FRAME_KBOARD (it.f));
23210 record_unwind_save_match_data ();
23211 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23212 pop_kboard ();
23214 unbind_to (count, Qnil);
23216 /* Fill up with spaces. */
23217 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23219 compute_line_metrics (&it);
23220 it.glyph_row->full_width_p = true;
23221 it.glyph_row->continued_p = false;
23222 it.glyph_row->truncated_on_left_p = false;
23223 it.glyph_row->truncated_on_right_p = false;
23225 /* Make a 3D mode-line have a shadow at its right end. */
23226 face = FACE_FROM_ID (it.f, face_id);
23227 extend_face_to_end_of_line (&it);
23228 if (face->box != FACE_NO_BOX)
23230 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23231 + it.glyph_row->used[TEXT_AREA] - 1);
23232 last->right_box_line_p = true;
23235 return it.glyph_row->height;
23238 /* Move element ELT in LIST to the front of LIST.
23239 Return the updated list. */
23241 static Lisp_Object
23242 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23244 register Lisp_Object tail, prev;
23245 register Lisp_Object tem;
23247 tail = list;
23248 prev = Qnil;
23249 while (CONSP (tail))
23251 tem = XCAR (tail);
23253 if (EQ (elt, tem))
23255 /* Splice out the link TAIL. */
23256 if (NILP (prev))
23257 list = XCDR (tail);
23258 else
23259 Fsetcdr (prev, XCDR (tail));
23261 /* Now make it the first. */
23262 Fsetcdr (tail, list);
23263 return tail;
23265 else
23266 prev = tail;
23267 tail = XCDR (tail);
23268 maybe_quit ();
23271 /* Not found--return unchanged LIST. */
23272 return list;
23275 /* Contribute ELT to the mode line for window IT->w. How it
23276 translates into text depends on its data type.
23278 IT describes the display environment in which we display, as usual.
23280 DEPTH is the depth in recursion. It is used to prevent
23281 infinite recursion here.
23283 FIELD_WIDTH is the number of characters the display of ELT should
23284 occupy in the mode line, and PRECISION is the maximum number of
23285 characters to display from ELT's representation. See
23286 display_string for details.
23288 Returns the hpos of the end of the text generated by ELT.
23290 PROPS is a property list to add to any string we encounter.
23292 If RISKY, remove (disregard) any properties in any string
23293 we encounter, and ignore :eval and :propertize.
23295 The global variable `mode_line_target' determines whether the
23296 output is passed to `store_mode_line_noprop',
23297 `store_mode_line_string', or `display_string'. */
23299 static int
23300 display_mode_element (struct it *it, int depth, int field_width, int precision,
23301 Lisp_Object elt, Lisp_Object props, bool risky)
23303 int n = 0, field, prec;
23304 bool literal = false;
23306 tail_recurse:
23307 if (depth > 100)
23308 elt = build_string ("*too-deep*");
23310 depth++;
23312 switch (XTYPE (elt))
23314 case Lisp_String:
23316 /* A string: output it and check for %-constructs within it. */
23317 unsigned char c;
23318 ptrdiff_t offset = 0;
23320 if (SCHARS (elt) > 0
23321 && (!NILP (props) || risky))
23323 Lisp_Object oprops, aelt;
23324 oprops = Ftext_properties_at (make_number (0), elt);
23326 /* If the starting string's properties are not what
23327 we want, translate the string. Also, if the string
23328 is risky, do that anyway. */
23330 if (NILP (Fequal (props, oprops)) || risky)
23332 /* If the starting string has properties,
23333 merge the specified ones onto the existing ones. */
23334 if (! NILP (oprops) && !risky)
23336 Lisp_Object tem;
23338 oprops = Fcopy_sequence (oprops);
23339 tem = props;
23340 while (CONSP (tem))
23342 oprops = Fplist_put (oprops, XCAR (tem),
23343 XCAR (XCDR (tem)));
23344 tem = XCDR (XCDR (tem));
23346 props = oprops;
23349 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23350 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23352 /* AELT is what we want. Move it to the front
23353 without consing. */
23354 elt = XCAR (aelt);
23355 mode_line_proptrans_alist
23356 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23358 else
23360 Lisp_Object tem;
23362 /* If AELT has the wrong props, it is useless.
23363 so get rid of it. */
23364 if (! NILP (aelt))
23365 mode_line_proptrans_alist
23366 = Fdelq (aelt, mode_line_proptrans_alist);
23368 elt = Fcopy_sequence (elt);
23369 Fset_text_properties (make_number (0), Flength (elt),
23370 props, elt);
23371 /* Add this item to mode_line_proptrans_alist. */
23372 mode_line_proptrans_alist
23373 = Fcons (Fcons (elt, props),
23374 mode_line_proptrans_alist);
23375 /* Truncate mode_line_proptrans_alist
23376 to at most 50 elements. */
23377 tem = Fnthcdr (make_number (50),
23378 mode_line_proptrans_alist);
23379 if (! NILP (tem))
23380 XSETCDR (tem, Qnil);
23385 offset = 0;
23387 if (literal)
23389 prec = precision - n;
23390 switch (mode_line_target)
23392 case MODE_LINE_NOPROP:
23393 case MODE_LINE_TITLE:
23394 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23395 break;
23396 case MODE_LINE_STRING:
23397 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23398 break;
23399 case MODE_LINE_DISPLAY:
23400 n += display_string (NULL, elt, Qnil, 0, 0, it,
23401 0, prec, 0, STRING_MULTIBYTE (elt));
23402 break;
23405 break;
23408 /* Handle the non-literal case. */
23410 while ((precision <= 0 || n < precision)
23411 && SREF (elt, offset) != 0
23412 && (mode_line_target != MODE_LINE_DISPLAY
23413 || it->current_x < it->last_visible_x))
23415 ptrdiff_t last_offset = offset;
23417 /* Advance to end of string or next format specifier. */
23418 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23421 if (offset - 1 != last_offset)
23423 ptrdiff_t nchars, nbytes;
23425 /* Output to end of string or up to '%'. Field width
23426 is length of string. Don't output more than
23427 PRECISION allows us. */
23428 offset--;
23430 prec = c_string_width (SDATA (elt) + last_offset,
23431 offset - last_offset, precision - n,
23432 &nchars, &nbytes);
23434 switch (mode_line_target)
23436 case MODE_LINE_NOPROP:
23437 case MODE_LINE_TITLE:
23438 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23439 break;
23440 case MODE_LINE_STRING:
23442 ptrdiff_t bytepos = last_offset;
23443 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23444 ptrdiff_t endpos = (precision <= 0
23445 ? string_byte_to_char (elt, offset)
23446 : charpos + nchars);
23447 Lisp_Object mode_string
23448 = Fsubstring (elt, make_number (charpos),
23449 make_number (endpos));
23450 n += store_mode_line_string (NULL, mode_string, false,
23451 0, 0, Qnil);
23453 break;
23454 case MODE_LINE_DISPLAY:
23456 ptrdiff_t bytepos = last_offset;
23457 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23459 if (precision <= 0)
23460 nchars = string_byte_to_char (elt, offset) - charpos;
23461 n += display_string (NULL, elt, Qnil, 0, charpos,
23462 it, 0, nchars, 0,
23463 STRING_MULTIBYTE (elt));
23465 break;
23468 else /* c == '%' */
23470 ptrdiff_t percent_position = offset;
23472 /* Get the specified minimum width. Zero means
23473 don't pad. */
23474 field = 0;
23475 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23476 field = field * 10 + c - '0';
23478 /* Don't pad beyond the total padding allowed. */
23479 if (field_width - n > 0 && field > field_width - n)
23480 field = field_width - n;
23482 /* Note that either PRECISION <= 0 or N < PRECISION. */
23483 prec = precision - n;
23485 if (c == 'M')
23486 n += display_mode_element (it, depth, field, prec,
23487 Vglobal_mode_string, props,
23488 risky);
23489 else if (c != 0)
23491 bool multibyte;
23492 ptrdiff_t bytepos, charpos;
23493 const char *spec;
23494 Lisp_Object string;
23496 bytepos = percent_position;
23497 charpos = (STRING_MULTIBYTE (elt)
23498 ? string_byte_to_char (elt, bytepos)
23499 : bytepos);
23500 spec = decode_mode_spec (it->w, c, field, &string);
23501 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23503 switch (mode_line_target)
23505 case MODE_LINE_NOPROP:
23506 case MODE_LINE_TITLE:
23507 n += store_mode_line_noprop (spec, field, prec);
23508 break;
23509 case MODE_LINE_STRING:
23511 Lisp_Object tem = build_string (spec);
23512 props = Ftext_properties_at (make_number (charpos), elt);
23513 /* Should only keep face property in props */
23514 n += store_mode_line_string (NULL, tem, false,
23515 field, prec, props);
23517 break;
23518 case MODE_LINE_DISPLAY:
23520 int nglyphs_before, nwritten;
23522 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23523 nwritten = display_string (spec, string, elt,
23524 charpos, 0, it,
23525 field, prec, 0,
23526 multibyte);
23528 /* Assign to the glyphs written above the
23529 string where the `%x' came from, position
23530 of the `%'. */
23531 if (nwritten > 0)
23533 struct glyph *glyph
23534 = (it->glyph_row->glyphs[TEXT_AREA]
23535 + nglyphs_before);
23536 int i;
23538 for (i = 0; i < nwritten; ++i)
23540 glyph[i].object = elt;
23541 glyph[i].charpos = charpos;
23544 n += nwritten;
23547 break;
23550 else /* c == 0 */
23551 break;
23555 break;
23557 case Lisp_Symbol:
23558 /* A symbol: process the value of the symbol recursively
23559 as if it appeared here directly. Avoid error if symbol void.
23560 Special case: if value of symbol is a string, output the string
23561 literally. */
23563 register Lisp_Object tem;
23565 /* If the variable is not marked as risky to set
23566 then its contents are risky to use. */
23567 if (NILP (Fget (elt, Qrisky_local_variable)))
23568 risky = true;
23570 tem = Fboundp (elt);
23571 if (!NILP (tem))
23573 tem = Fsymbol_value (elt);
23574 /* If value is a string, output that string literally:
23575 don't check for % within it. */
23576 if (STRINGP (tem))
23577 literal = true;
23579 if (!EQ (tem, elt))
23581 /* Give up right away for nil or t. */
23582 elt = tem;
23583 goto tail_recurse;
23587 break;
23589 case Lisp_Cons:
23591 register Lisp_Object car, tem;
23593 /* A cons cell: five distinct cases.
23594 If first element is :eval or :propertize, do something special.
23595 If first element is a string or a cons, process all the elements
23596 and effectively concatenate them.
23597 If first element is a negative number, truncate displaying cdr to
23598 at most that many characters. If positive, pad (with spaces)
23599 to at least that many characters.
23600 If first element is a symbol, process the cadr or caddr recursively
23601 according to whether the symbol's value is non-nil or nil. */
23602 car = XCAR (elt);
23603 if (EQ (car, QCeval))
23605 /* An element of the form (:eval FORM) means evaluate FORM
23606 and use the result as mode line elements. */
23608 if (risky)
23609 break;
23611 if (CONSP (XCDR (elt)))
23613 Lisp_Object spec;
23614 spec = safe__eval (true, XCAR (XCDR (elt)));
23615 n += display_mode_element (it, depth, field_width - n,
23616 precision - n, spec, props,
23617 risky);
23620 else if (EQ (car, QCpropertize))
23622 /* An element of the form (:propertize ELT PROPS...)
23623 means display ELT but applying properties PROPS. */
23625 if (risky)
23626 break;
23628 if (CONSP (XCDR (elt)))
23629 n += display_mode_element (it, depth, field_width - n,
23630 precision - n, XCAR (XCDR (elt)),
23631 XCDR (XCDR (elt)), risky);
23633 else if (SYMBOLP (car))
23635 tem = Fboundp (car);
23636 elt = XCDR (elt);
23637 if (!CONSP (elt))
23638 goto invalid;
23639 /* elt is now the cdr, and we know it is a cons cell.
23640 Use its car if CAR has a non-nil value. */
23641 if (!NILP (tem))
23643 tem = Fsymbol_value (car);
23644 if (!NILP (tem))
23646 elt = XCAR (elt);
23647 goto tail_recurse;
23650 /* Symbol's value is nil (or symbol is unbound)
23651 Get the cddr of the original list
23652 and if possible find the caddr and use that. */
23653 elt = XCDR (elt);
23654 if (NILP (elt))
23655 break;
23656 else if (!CONSP (elt))
23657 goto invalid;
23658 elt = XCAR (elt);
23659 goto tail_recurse;
23661 else if (INTEGERP (car))
23663 register int lim = XINT (car);
23664 elt = XCDR (elt);
23665 if (lim < 0)
23667 /* Negative int means reduce maximum width. */
23668 if (precision <= 0)
23669 precision = -lim;
23670 else
23671 precision = min (precision, -lim);
23673 else if (lim > 0)
23675 /* Padding specified. Don't let it be more than
23676 current maximum. */
23677 if (precision > 0)
23678 lim = min (precision, lim);
23680 /* If that's more padding than already wanted, queue it.
23681 But don't reduce padding already specified even if
23682 that is beyond the current truncation point. */
23683 field_width = max (lim, field_width);
23685 goto tail_recurse;
23687 else if (STRINGP (car) || CONSP (car))
23688 FOR_EACH_TAIL_SAFE (elt)
23690 if (0 < precision && precision <= n)
23691 break;
23692 n += display_mode_element (it, depth,
23693 /* Pad after only the last
23694 list element. */
23695 (! CONSP (XCDR (elt))
23696 ? field_width - n
23697 : 0),
23698 precision - n, XCAR (elt),
23699 props, risky);
23702 break;
23704 default:
23705 invalid:
23706 elt = build_string ("*invalid*");
23707 goto tail_recurse;
23710 /* Pad to FIELD_WIDTH. */
23711 if (field_width > 0 && n < field_width)
23713 switch (mode_line_target)
23715 case MODE_LINE_NOPROP:
23716 case MODE_LINE_TITLE:
23717 n += store_mode_line_noprop ("", field_width - n, 0);
23718 break;
23719 case MODE_LINE_STRING:
23720 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23721 Qnil);
23722 break;
23723 case MODE_LINE_DISPLAY:
23724 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23725 0, 0, 0);
23726 break;
23730 return n;
23733 /* Store a mode-line string element in mode_line_string_list.
23735 If STRING is non-null, display that C string. Otherwise, the Lisp
23736 string LISP_STRING is displayed.
23738 FIELD_WIDTH is the minimum number of output glyphs to produce.
23739 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23740 with spaces. FIELD_WIDTH <= 0 means don't pad.
23742 PRECISION is the maximum number of characters to output from
23743 STRING. PRECISION <= 0 means don't truncate the string.
23745 If COPY_STRING, make a copy of LISP_STRING before adding
23746 properties to the string.
23748 PROPS are the properties to add to the string.
23749 The mode_line_string_face face property is always added to the string.
23752 static int
23753 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23754 bool copy_string,
23755 int field_width, int precision, Lisp_Object props)
23757 ptrdiff_t len;
23758 int n = 0;
23760 if (string != NULL)
23762 len = strlen (string);
23763 if (precision > 0 && len > precision)
23764 len = precision;
23765 lisp_string = make_string (string, len);
23766 if (NILP (props))
23767 props = mode_line_string_face_prop;
23768 else if (!NILP (mode_line_string_face))
23770 Lisp_Object face = Fplist_get (props, Qface);
23771 props = Fcopy_sequence (props);
23772 if (NILP (face))
23773 face = mode_line_string_face;
23774 else
23775 face = list2 (face, mode_line_string_face);
23776 props = Fplist_put (props, Qface, face);
23778 Fadd_text_properties (make_number (0), make_number (len),
23779 props, lisp_string);
23781 else
23783 len = XFASTINT (Flength (lisp_string));
23784 if (precision > 0 && len > precision)
23786 len = precision;
23787 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23788 precision = -1;
23790 if (!NILP (mode_line_string_face))
23792 Lisp_Object face;
23793 if (NILP (props))
23794 props = Ftext_properties_at (make_number (0), lisp_string);
23795 face = Fplist_get (props, Qface);
23796 if (NILP (face))
23797 face = mode_line_string_face;
23798 else
23799 face = list2 (face, mode_line_string_face);
23800 props = list2 (Qface, face);
23801 if (copy_string)
23802 lisp_string = Fcopy_sequence (lisp_string);
23804 if (!NILP (props))
23805 Fadd_text_properties (make_number (0), make_number (len),
23806 props, lisp_string);
23809 if (len > 0)
23811 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23812 n += len;
23815 if (field_width > len)
23817 field_width -= len;
23818 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23819 if (!NILP (props))
23820 Fadd_text_properties (make_number (0), make_number (field_width),
23821 props, lisp_string);
23822 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23823 n += field_width;
23826 return n;
23830 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23831 1, 4, 0,
23832 doc: /* Format a string out of a mode line format specification.
23833 First arg FORMAT specifies the mode line format (see `mode-line-format'
23834 for details) to use.
23836 By default, the format is evaluated for the currently selected window.
23838 Optional second arg FACE specifies the face property to put on all
23839 characters for which no face is specified. The value nil means the
23840 default face. The value t means whatever face the window's mode line
23841 currently uses (either `mode-line' or `mode-line-inactive',
23842 depending on whether the window is the selected window or not).
23843 An integer value means the value string has no text
23844 properties.
23846 Optional third and fourth args WINDOW and BUFFER specify the window
23847 and buffer to use as the context for the formatting (defaults
23848 are the selected window and the WINDOW's buffer). */)
23849 (Lisp_Object format, Lisp_Object face,
23850 Lisp_Object window, Lisp_Object buffer)
23852 struct it it;
23853 int len;
23854 struct window *w;
23855 struct buffer *old_buffer = NULL;
23856 int face_id;
23857 bool no_props = INTEGERP (face);
23858 ptrdiff_t count = SPECPDL_INDEX ();
23859 Lisp_Object str;
23860 int string_start = 0;
23862 w = decode_any_window (window);
23863 XSETWINDOW (window, w);
23865 if (NILP (buffer))
23866 buffer = w->contents;
23867 CHECK_BUFFER (buffer);
23869 /* Make formatting the modeline a non-op when noninteractive, otherwise
23870 there will be problems later caused by a partially initialized frame. */
23871 if (NILP (format) || noninteractive)
23872 return empty_unibyte_string;
23874 if (no_props)
23875 face = Qnil;
23877 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23878 : EQ (face, Qt) ? (EQ (window, selected_window)
23879 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23880 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23881 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23882 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23883 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23884 : DEFAULT_FACE_ID;
23886 old_buffer = current_buffer;
23888 /* Save things including mode_line_proptrans_alist,
23889 and set that to nil so that we don't alter the outer value. */
23890 record_unwind_protect (unwind_format_mode_line,
23891 format_mode_line_unwind_data
23892 (XFRAME (WINDOW_FRAME (w)),
23893 old_buffer, selected_window, true));
23894 mode_line_proptrans_alist = Qnil;
23896 Fselect_window (window, Qt);
23897 set_buffer_internal_1 (XBUFFER (buffer));
23899 init_iterator (&it, w, -1, -1, NULL, face_id);
23901 if (no_props)
23903 mode_line_target = MODE_LINE_NOPROP;
23904 mode_line_string_face_prop = Qnil;
23905 mode_line_string_list = Qnil;
23906 string_start = MODE_LINE_NOPROP_LEN (0);
23908 else
23910 mode_line_target = MODE_LINE_STRING;
23911 mode_line_string_list = Qnil;
23912 mode_line_string_face = face;
23913 mode_line_string_face_prop
23914 = NILP (face) ? Qnil : list2 (Qface, face);
23917 push_kboard (FRAME_KBOARD (it.f));
23918 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23919 pop_kboard ();
23921 if (no_props)
23923 len = MODE_LINE_NOPROP_LEN (string_start);
23924 str = make_string (mode_line_noprop_buf + string_start, len);
23926 else
23928 mode_line_string_list = Fnreverse (mode_line_string_list);
23929 str = Fmapconcat (Qidentity, mode_line_string_list,
23930 empty_unibyte_string);
23933 unbind_to (count, Qnil);
23934 return str;
23937 /* Write a null-terminated, right justified decimal representation of
23938 the positive integer D to BUF using a minimal field width WIDTH. */
23940 static void
23941 pint2str (register char *buf, register int width, register ptrdiff_t d)
23943 register char *p = buf;
23945 if (d <= 0)
23946 *p++ = '0';
23947 else
23949 while (d > 0)
23951 *p++ = d % 10 + '0';
23952 d /= 10;
23956 for (width -= (int) (p - buf); width > 0; --width)
23957 *p++ = ' ';
23958 *p-- = '\0';
23959 while (p > buf)
23961 d = *buf;
23962 *buf++ = *p;
23963 *p-- = d;
23967 /* Write a null-terminated, right justified decimal and "human
23968 readable" representation of the nonnegative integer D to BUF using
23969 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23971 static const char power_letter[] =
23973 0, /* no letter */
23974 'k', /* kilo */
23975 'M', /* mega */
23976 'G', /* giga */
23977 'T', /* tera */
23978 'P', /* peta */
23979 'E', /* exa */
23980 'Z', /* zetta */
23981 'Y' /* yotta */
23984 static void
23985 pint2hrstr (char *buf, int width, ptrdiff_t d)
23987 /* We aim to represent the nonnegative integer D as
23988 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23989 ptrdiff_t quotient = d;
23990 int remainder = 0;
23991 /* -1 means: do not use TENTHS. */
23992 int tenths = -1;
23993 int exponent = 0;
23995 /* Length of QUOTIENT.TENTHS as a string. */
23996 int length;
23998 char * psuffix;
23999 char * p;
24001 if (quotient >= 1000)
24003 /* Scale to the appropriate EXPONENT. */
24006 remainder = quotient % 1000;
24007 quotient /= 1000;
24008 exponent++;
24010 while (quotient >= 1000);
24012 /* Round to nearest and decide whether to use TENTHS or not. */
24013 if (quotient <= 9)
24015 tenths = remainder / 100;
24016 if (remainder % 100 >= 50)
24018 if (tenths < 9)
24019 tenths++;
24020 else
24022 quotient++;
24023 if (quotient == 10)
24024 tenths = -1;
24025 else
24026 tenths = 0;
24030 else
24031 if (remainder >= 500)
24033 if (quotient < 999)
24034 quotient++;
24035 else
24037 quotient = 1;
24038 exponent++;
24039 tenths = 0;
24044 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24045 if (tenths == -1 && quotient <= 99)
24046 if (quotient <= 9)
24047 length = 1;
24048 else
24049 length = 2;
24050 else
24051 length = 3;
24052 p = psuffix = buf + max (width, length);
24054 /* Print EXPONENT. */
24055 *psuffix++ = power_letter[exponent];
24056 *psuffix = '\0';
24058 /* Print TENTHS. */
24059 if (tenths >= 0)
24061 *--p = '0' + tenths;
24062 *--p = '.';
24065 /* Print QUOTIENT. */
24068 int digit = quotient % 10;
24069 *--p = '0' + digit;
24071 while ((quotient /= 10) != 0);
24073 /* Print leading spaces. */
24074 while (buf < p)
24075 *--p = ' ';
24078 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24079 If EOL_FLAG, set also a mnemonic character for end-of-line
24080 type of CODING_SYSTEM. Return updated pointer into BUF. */
24082 static unsigned char invalid_eol_type[] = "(*invalid*)";
24084 static char *
24085 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24087 Lisp_Object val;
24088 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24089 const unsigned char *eol_str;
24090 int eol_str_len;
24091 /* The EOL conversion we are using. */
24092 Lisp_Object eoltype;
24094 val = CODING_SYSTEM_SPEC (coding_system);
24095 eoltype = Qnil;
24097 if (!VECTORP (val)) /* Not yet decided. */
24099 *buf++ = multibyte ? '-' : ' ';
24100 if (eol_flag)
24101 eoltype = eol_mnemonic_undecided;
24102 /* Don't mention EOL conversion if it isn't decided. */
24104 else
24106 Lisp_Object attrs;
24107 Lisp_Object eolvalue;
24109 attrs = AREF (val, 0);
24110 eolvalue = AREF (val, 2);
24112 *buf++ = multibyte
24113 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24114 : ' ';
24116 if (eol_flag)
24118 /* The EOL conversion that is normal on this system. */
24120 if (NILP (eolvalue)) /* Not yet decided. */
24121 eoltype = eol_mnemonic_undecided;
24122 else if (VECTORP (eolvalue)) /* Not yet decided. */
24123 eoltype = eol_mnemonic_undecided;
24124 else /* eolvalue is Qunix, Qdos, or Qmac. */
24125 eoltype = (EQ (eolvalue, Qunix)
24126 ? eol_mnemonic_unix
24127 : EQ (eolvalue, Qdos)
24128 ? eol_mnemonic_dos : eol_mnemonic_mac);
24132 if (eol_flag)
24134 /* Mention the EOL conversion if it is not the usual one. */
24135 if (STRINGP (eoltype))
24137 eol_str = SDATA (eoltype);
24138 eol_str_len = SBYTES (eoltype);
24140 else if (CHARACTERP (eoltype))
24142 int c = XFASTINT (eoltype);
24143 return buf + CHAR_STRING (c, (unsigned char *) buf);
24145 else
24147 eol_str = invalid_eol_type;
24148 eol_str_len = sizeof (invalid_eol_type) - 1;
24150 memcpy (buf, eol_str, eol_str_len);
24151 buf += eol_str_len;
24154 return buf;
24157 /* Return the approximate percentage N is of D (rounding upward), or 99,
24158 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24160 static int
24161 percent99 (ptrdiff_t n, ptrdiff_t d)
24163 int percent = (d - 1 + 100.0 * n) / d;
24164 return min (percent, 99);
24167 /* Return a string for the output of a mode line %-spec for window W,
24168 generated by character C. FIELD_WIDTH > 0 means pad the string
24169 returned with spaces to that value. Return a Lisp string in
24170 *STRING if the resulting string is taken from that Lisp string.
24172 Note we operate on the current buffer for most purposes. */
24174 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24176 static const char *
24177 decode_mode_spec (struct window *w, register int c, int field_width,
24178 Lisp_Object *string)
24180 Lisp_Object obj;
24181 struct frame *f = XFRAME (WINDOW_FRAME (w));
24182 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24183 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24184 produce strings from numerical values, so limit preposterously
24185 large values of FIELD_WIDTH to avoid overrunning the buffer's
24186 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24187 bytes plus the terminating null. */
24188 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24189 struct buffer *b = current_buffer;
24191 obj = Qnil;
24192 *string = Qnil;
24194 switch (c)
24196 case '*':
24197 if (!NILP (BVAR (b, read_only)))
24198 return "%";
24199 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24200 return "*";
24201 return "-";
24203 case '+':
24204 /* This differs from %* only for a modified read-only buffer. */
24205 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24206 return "*";
24207 if (!NILP (BVAR (b, read_only)))
24208 return "%";
24209 return "-";
24211 case '&':
24212 /* This differs from %* in ignoring read-only-ness. */
24213 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24214 return "*";
24215 return "-";
24217 case '%':
24218 return "%";
24220 case '[':
24222 int i;
24223 char *p;
24225 if (command_loop_level > 5)
24226 return "[[[... ";
24227 p = decode_mode_spec_buf;
24228 for (i = 0; i < command_loop_level; i++)
24229 *p++ = '[';
24230 *p = 0;
24231 return decode_mode_spec_buf;
24234 case ']':
24236 int i;
24237 char *p;
24239 if (command_loop_level > 5)
24240 return " ...]]]";
24241 p = decode_mode_spec_buf;
24242 for (i = 0; i < command_loop_level; i++)
24243 *p++ = ']';
24244 *p = 0;
24245 return decode_mode_spec_buf;
24248 case '-':
24250 register int i;
24252 /* Let lots_of_dashes be a string of infinite length. */
24253 if (mode_line_target == MODE_LINE_NOPROP
24254 || mode_line_target == MODE_LINE_STRING)
24255 return "--";
24256 if (field_width <= 0
24257 || field_width > sizeof (lots_of_dashes))
24259 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24260 decode_mode_spec_buf[i] = '-';
24261 decode_mode_spec_buf[i] = '\0';
24262 return decode_mode_spec_buf;
24264 else
24265 return lots_of_dashes;
24268 case 'b':
24269 obj = BVAR (b, name);
24270 break;
24272 case 'c':
24273 case 'C':
24274 /* %c, %C, and %l are ignored in `frame-title-format'.
24275 (In redisplay_internal, the frame title is drawn _before_ the
24276 windows are updated, so the stuff which depends on actual
24277 window contents (such as %l) may fail to render properly, or
24278 even crash emacs.) */
24279 if (mode_line_target == MODE_LINE_TITLE)
24280 return "";
24281 else
24283 ptrdiff_t col = current_column ();
24284 int disp_col = (c == 'C') ? col + 1 : col;
24285 w->column_number_displayed = col;
24286 pint2str (decode_mode_spec_buf, width, disp_col);
24287 return decode_mode_spec_buf;
24290 case 'e':
24291 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24293 if (NILP (Vmemory_full))
24294 return "";
24295 else
24296 return "!MEM FULL! ";
24298 #else
24299 return "";
24300 #endif
24302 case 'F':
24303 /* %F displays the frame name. */
24304 if (!NILP (f->title))
24305 return SSDATA (f->title);
24306 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24307 return SSDATA (f->name);
24308 return "Emacs";
24310 case 'f':
24311 obj = BVAR (b, filename);
24312 break;
24314 case 'i':
24316 ptrdiff_t size = ZV - BEGV;
24317 pint2str (decode_mode_spec_buf, width, size);
24318 return decode_mode_spec_buf;
24321 case 'I':
24323 ptrdiff_t size = ZV - BEGV;
24324 pint2hrstr (decode_mode_spec_buf, width, size);
24325 return decode_mode_spec_buf;
24328 case 'l':
24330 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24331 ptrdiff_t topline, nlines, height;
24332 ptrdiff_t junk;
24334 /* %c, %C, and %l are ignored in `frame-title-format'. */
24335 if (mode_line_target == MODE_LINE_TITLE)
24336 return "";
24338 startpos = marker_position (w->start);
24339 startpos_byte = marker_byte_position (w->start);
24340 height = WINDOW_TOTAL_LINES (w);
24342 /* If we decided that this buffer isn't suitable for line numbers,
24343 don't forget that too fast. */
24344 if (w->base_line_pos == -1)
24345 goto no_value;
24347 /* If the buffer is very big, don't waste time. */
24348 if (INTEGERP (Vline_number_display_limit)
24349 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24351 w->base_line_pos = 0;
24352 w->base_line_number = 0;
24353 goto no_value;
24356 if (w->base_line_number > 0
24357 && w->base_line_pos > 0
24358 && w->base_line_pos <= startpos)
24360 line = w->base_line_number;
24361 linepos = w->base_line_pos;
24362 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24364 else
24366 line = 1;
24367 linepos = BUF_BEGV (b);
24368 linepos_byte = BUF_BEGV_BYTE (b);
24371 /* Count lines from base line to window start position. */
24372 nlines = display_count_lines (linepos_byte,
24373 startpos_byte,
24374 startpos, &junk);
24376 topline = nlines + line;
24378 /* Determine a new base line, if the old one is too close
24379 or too far away, or if we did not have one.
24380 "Too close" means it's plausible a scroll-down would
24381 go back past it. */
24382 if (startpos == BUF_BEGV (b))
24384 w->base_line_number = topline;
24385 w->base_line_pos = BUF_BEGV (b);
24387 else if (nlines < height + 25 || nlines > height * 3 + 50
24388 || linepos == BUF_BEGV (b))
24390 ptrdiff_t limit = BUF_BEGV (b);
24391 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24392 ptrdiff_t position;
24393 ptrdiff_t distance =
24394 (height * 2 + 30) * line_number_display_limit_width;
24396 if (startpos - distance > limit)
24398 limit = startpos - distance;
24399 limit_byte = CHAR_TO_BYTE (limit);
24402 nlines = display_count_lines (startpos_byte,
24403 limit_byte,
24404 - (height * 2 + 30),
24405 &position);
24406 /* If we couldn't find the lines we wanted within
24407 line_number_display_limit_width chars per line,
24408 give up on line numbers for this window. */
24409 if (position == limit_byte && limit == startpos - distance)
24411 w->base_line_pos = -1;
24412 w->base_line_number = 0;
24413 goto no_value;
24416 w->base_line_number = topline - nlines;
24417 w->base_line_pos = BYTE_TO_CHAR (position);
24420 /* Now count lines from the start pos to point. */
24421 nlines = display_count_lines (startpos_byte,
24422 PT_BYTE, PT, &junk);
24424 /* Record that we did display the line number. */
24425 line_number_displayed = true;
24427 /* Make the string to show. */
24428 pint2str (decode_mode_spec_buf, width, topline + nlines);
24429 return decode_mode_spec_buf;
24430 no_value:
24432 char *p = decode_mode_spec_buf;
24433 int pad = width - 2;
24434 while (pad-- > 0)
24435 *p++ = ' ';
24436 *p++ = '?';
24437 *p++ = '?';
24438 *p = '\0';
24439 return decode_mode_spec_buf;
24442 break;
24444 case 'm':
24445 obj = BVAR (b, mode_name);
24446 break;
24448 case 'n':
24449 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24450 return " Narrow";
24451 break;
24453 /* Display the "degree of travel" of the window through the buffer. */
24454 case 'o':
24456 ptrdiff_t toppos = marker_position (w->start);
24457 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24458 ptrdiff_t begv = BUF_BEGV (b);
24459 ptrdiff_t zv = BUF_ZV (b);
24461 if (zv <= botpos)
24462 return toppos <= begv ? "All" : "Bottom";
24463 else if (toppos <= begv)
24464 return "Top";
24465 else
24467 sprintf (decode_mode_spec_buf, "%2d%%",
24468 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24469 return decode_mode_spec_buf;
24473 /* Display percentage of buffer above the top of the screen. */
24474 case 'p':
24476 ptrdiff_t pos = marker_position (w->start);
24477 ptrdiff_t begv = BUF_BEGV (b);
24478 ptrdiff_t zv = BUF_ZV (b);
24480 if (w->window_end_pos <= BUF_Z (b) - zv)
24481 return pos <= begv ? "All" : "Bottom";
24482 else if (pos <= begv)
24483 return "Top";
24484 else
24486 sprintf (decode_mode_spec_buf, "%2d%%",
24487 percent99 (pos - begv, zv - begv));
24488 return decode_mode_spec_buf;
24492 /* Display percentage of size above the bottom of the screen. */
24493 case 'P':
24495 ptrdiff_t toppos = marker_position (w->start);
24496 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24497 ptrdiff_t begv = BUF_BEGV (b);
24498 ptrdiff_t zv = BUF_ZV (b);
24500 if (zv <= botpos)
24501 return toppos <= begv ? "All" : "Bottom";
24502 else
24504 sprintf (decode_mode_spec_buf,
24505 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24506 percent99 (botpos - begv, zv - begv));
24507 return decode_mode_spec_buf;
24511 /* Display percentage offsets of top and bottom of the window,
24512 using "All" (but not "Top" or "Bottom") where appropriate. */
24513 case 'q':
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);
24519 int top_perc, bot_perc;
24521 if ((toppos <= begv) && (zv <= botpos))
24522 return "All ";
24524 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24525 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24527 if (top_perc == bot_perc)
24528 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24529 else
24530 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24532 return decode_mode_spec_buf;
24535 case 's':
24536 /* status of process */
24537 obj = Fget_buffer_process (Fcurrent_buffer ());
24538 if (NILP (obj))
24539 return "no process";
24540 #ifndef MSDOS
24541 obj = Fsymbol_name (Fprocess_status (obj));
24542 #endif
24543 break;
24545 case '@':
24547 ptrdiff_t count = inhibit_garbage_collection ();
24548 Lisp_Object curdir = BVAR (current_buffer, directory);
24549 Lisp_Object val = Qnil;
24551 if (STRINGP (curdir))
24552 val = call1 (intern ("file-remote-p"), curdir);
24554 unbind_to (count, Qnil);
24556 if (NILP (val))
24557 return "-";
24558 else
24559 return "@";
24562 case 'z':
24563 /* coding-system (not including end-of-line format) */
24564 case 'Z':
24565 /* coding-system (including end-of-line type) */
24567 bool eol_flag = (c == 'Z');
24568 char *p = decode_mode_spec_buf;
24570 if (! FRAME_WINDOW_P (f))
24572 /* No need to mention EOL here--the terminal never needs
24573 to do EOL conversion. */
24574 p = decode_mode_spec_coding (CODING_ID_NAME
24575 (FRAME_KEYBOARD_CODING (f)->id),
24576 p, false);
24577 p = decode_mode_spec_coding (CODING_ID_NAME
24578 (FRAME_TERMINAL_CODING (f)->id),
24579 p, false);
24581 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24582 p, eol_flag);
24584 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24585 #ifdef subprocesses
24586 obj = Fget_buffer_process (Fcurrent_buffer ());
24587 if (PROCESSP (obj))
24589 p = decode_mode_spec_coding
24590 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24591 p = decode_mode_spec_coding
24592 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24594 #endif /* subprocesses */
24595 #endif /* false */
24596 *p = 0;
24597 return decode_mode_spec_buf;
24601 if (STRINGP (obj))
24603 *string = obj;
24604 return SSDATA (obj);
24606 else
24607 return "";
24611 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24612 means count lines back from START_BYTE. But don't go beyond
24613 LIMIT_BYTE. Return the number of lines thus found (always
24614 nonnegative).
24616 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24617 either the position COUNT lines after/before START_BYTE, if we
24618 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24619 COUNT lines. */
24621 static ptrdiff_t
24622 display_count_lines (ptrdiff_t start_byte,
24623 ptrdiff_t limit_byte, ptrdiff_t count,
24624 ptrdiff_t *byte_pos_ptr)
24626 register unsigned char *cursor;
24627 unsigned char *base;
24629 register ptrdiff_t ceiling;
24630 register unsigned char *ceiling_addr;
24631 ptrdiff_t orig_count = count;
24633 /* If we are not in selective display mode,
24634 check only for newlines. */
24635 bool selective_display
24636 = (!NILP (BVAR (current_buffer, selective_display))
24637 && !INTEGERP (BVAR (current_buffer, selective_display)));
24639 if (count > 0)
24641 while (start_byte < limit_byte)
24643 ceiling = BUFFER_CEILING_OF (start_byte);
24644 ceiling = min (limit_byte - 1, ceiling);
24645 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24646 base = (cursor = BYTE_POS_ADDR (start_byte));
24650 if (selective_display)
24652 while (*cursor != '\n' && *cursor != 015
24653 && ++cursor != ceiling_addr)
24654 continue;
24655 if (cursor == ceiling_addr)
24656 break;
24658 else
24660 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24661 if (! cursor)
24662 break;
24665 cursor++;
24667 if (--count == 0)
24669 start_byte += cursor - base;
24670 *byte_pos_ptr = start_byte;
24671 return orig_count;
24674 while (cursor < ceiling_addr);
24676 start_byte += ceiling_addr - base;
24679 else
24681 while (start_byte > limit_byte)
24683 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24684 ceiling = max (limit_byte, ceiling);
24685 ceiling_addr = BYTE_POS_ADDR (ceiling);
24686 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24687 while (true)
24689 if (selective_display)
24691 while (--cursor >= ceiling_addr
24692 && *cursor != '\n' && *cursor != 015)
24693 continue;
24694 if (cursor < ceiling_addr)
24695 break;
24697 else
24699 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24700 if (! cursor)
24701 break;
24704 if (++count == 0)
24706 start_byte += cursor - base + 1;
24707 *byte_pos_ptr = start_byte;
24708 /* When scanning backwards, we should
24709 not count the newline posterior to which we stop. */
24710 return - orig_count - 1;
24713 start_byte += ceiling_addr - base;
24717 *byte_pos_ptr = limit_byte;
24719 if (count < 0)
24720 return - orig_count + count;
24721 return orig_count - count;
24727 /***********************************************************************
24728 Displaying strings
24729 ***********************************************************************/
24731 /* Display a NUL-terminated string, starting with index START.
24733 If STRING is non-null, display that C string. Otherwise, the Lisp
24734 string LISP_STRING is displayed. There's a case that STRING is
24735 non-null and LISP_STRING is not nil. It means STRING is a string
24736 data of LISP_STRING. In that case, we display LISP_STRING while
24737 ignoring its text properties.
24739 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24740 FACE_STRING. Display STRING or LISP_STRING with the face at
24741 FACE_STRING_POS in FACE_STRING:
24743 Display the string in the environment given by IT, but use the
24744 standard display table, temporarily.
24746 FIELD_WIDTH is the minimum number of output glyphs to produce.
24747 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24748 with spaces. If STRING has more characters, more than FIELD_WIDTH
24749 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24751 PRECISION is the maximum number of characters to output from
24752 STRING. PRECISION < 0 means don't truncate the string.
24754 This is roughly equivalent to printf format specifiers:
24756 FIELD_WIDTH PRECISION PRINTF
24757 ----------------------------------------
24758 -1 -1 %s
24759 -1 10 %.10s
24760 10 -1 %10s
24761 20 10 %20.10s
24763 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24764 display them, and < 0 means obey the current buffer's value of
24765 enable_multibyte_characters.
24767 Value is the number of columns displayed. */
24769 static int
24770 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24771 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24772 int field_width, int precision, int max_x, int multibyte)
24774 int hpos_at_start = it->hpos;
24775 int saved_face_id = it->face_id;
24776 struct glyph_row *row = it->glyph_row;
24777 ptrdiff_t it_charpos;
24779 /* Initialize the iterator IT for iteration over STRING beginning
24780 with index START. */
24781 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24782 precision, field_width, multibyte);
24783 if (string && STRINGP (lisp_string))
24784 /* LISP_STRING is the one returned by decode_mode_spec. We should
24785 ignore its text properties. */
24786 it->stop_charpos = it->end_charpos;
24788 /* If displaying STRING, set up the face of the iterator from
24789 FACE_STRING, if that's given. */
24790 if (STRINGP (face_string))
24792 ptrdiff_t endptr;
24793 struct face *face;
24795 it->face_id
24796 = face_at_string_position (it->w, face_string, face_string_pos,
24797 0, &endptr, it->base_face_id, false);
24798 face = FACE_FROM_ID (it->f, it->face_id);
24799 it->face_box_p = face->box != FACE_NO_BOX;
24802 /* Set max_x to the maximum allowed X position. Don't let it go
24803 beyond the right edge of the window. */
24804 if (max_x <= 0)
24805 max_x = it->last_visible_x;
24806 else
24807 max_x = min (max_x, it->last_visible_x);
24809 /* Skip over display elements that are not visible. because IT->w is
24810 hscrolled. */
24811 if (it->current_x < it->first_visible_x)
24812 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24813 MOVE_TO_POS | MOVE_TO_X);
24815 row->ascent = it->max_ascent;
24816 row->height = it->max_ascent + it->max_descent;
24817 row->phys_ascent = it->max_phys_ascent;
24818 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24819 row->extra_line_spacing = it->max_extra_line_spacing;
24821 if (STRINGP (it->string))
24822 it_charpos = IT_STRING_CHARPOS (*it);
24823 else
24824 it_charpos = IT_CHARPOS (*it);
24826 /* This condition is for the case that we are called with current_x
24827 past last_visible_x. */
24828 while (it->current_x < max_x)
24830 int x_before, x, n_glyphs_before, i, nglyphs;
24832 /* Get the next display element. */
24833 if (!get_next_display_element (it))
24834 break;
24836 /* Produce glyphs. */
24837 x_before = it->current_x;
24838 n_glyphs_before = row->used[TEXT_AREA];
24839 PRODUCE_GLYPHS (it);
24841 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24842 i = 0;
24843 x = x_before;
24844 while (i < nglyphs)
24846 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24848 if (it->line_wrap != TRUNCATE
24849 && x + glyph->pixel_width > max_x)
24851 /* End of continued line or max_x reached. */
24852 if (CHAR_GLYPH_PADDING_P (*glyph))
24854 /* A wide character is unbreakable. */
24855 if (row->reversed_p)
24856 unproduce_glyphs (it, row->used[TEXT_AREA]
24857 - n_glyphs_before);
24858 row->used[TEXT_AREA] = n_glyphs_before;
24859 it->current_x = x_before;
24861 else
24863 if (row->reversed_p)
24864 unproduce_glyphs (it, row->used[TEXT_AREA]
24865 - (n_glyphs_before + i));
24866 row->used[TEXT_AREA] = n_glyphs_before + i;
24867 it->current_x = x;
24869 break;
24871 else if (x + glyph->pixel_width >= it->first_visible_x)
24873 /* Glyph is at least partially visible. */
24874 ++it->hpos;
24875 if (x < it->first_visible_x)
24876 row->x = x - it->first_visible_x;
24878 else
24880 /* Glyph is off the left margin of the display area.
24881 Should not happen. */
24882 emacs_abort ();
24885 row->ascent = max (row->ascent, it->max_ascent);
24886 row->height = max (row->height, it->max_ascent + it->max_descent);
24887 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24888 row->phys_height = max (row->phys_height,
24889 it->max_phys_ascent + it->max_phys_descent);
24890 row->extra_line_spacing = max (row->extra_line_spacing,
24891 it->max_extra_line_spacing);
24892 x += glyph->pixel_width;
24893 ++i;
24896 /* Stop if max_x reached. */
24897 if (i < nglyphs)
24898 break;
24900 /* Stop at line ends. */
24901 if (ITERATOR_AT_END_OF_LINE_P (it))
24903 it->continuation_lines_width = 0;
24904 break;
24907 set_iterator_to_next (it, true);
24908 if (STRINGP (it->string))
24909 it_charpos = IT_STRING_CHARPOS (*it);
24910 else
24911 it_charpos = IT_CHARPOS (*it);
24913 /* Stop if truncating at the right edge. */
24914 if (it->line_wrap == TRUNCATE
24915 && it->current_x >= it->last_visible_x)
24917 /* Add truncation mark, but don't do it if the line is
24918 truncated at a padding space. */
24919 if (it_charpos < it->string_nchars)
24921 if (!FRAME_WINDOW_P (it->f))
24923 int ii, n;
24925 if (it->current_x > it->last_visible_x)
24927 if (!row->reversed_p)
24929 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24930 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24931 break;
24933 else
24935 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24936 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24937 break;
24938 unproduce_glyphs (it, ii + 1);
24939 ii = row->used[TEXT_AREA] - (ii + 1);
24941 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24943 row->used[TEXT_AREA] = ii;
24944 produce_special_glyphs (it, IT_TRUNCATION);
24947 produce_special_glyphs (it, IT_TRUNCATION);
24949 row->truncated_on_right_p = true;
24951 break;
24955 /* Maybe insert a truncation at the left. */
24956 if (it->first_visible_x
24957 && it_charpos > 0)
24959 if (!FRAME_WINDOW_P (it->f)
24960 || (row->reversed_p
24961 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24962 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24963 insert_left_trunc_glyphs (it);
24964 row->truncated_on_left_p = true;
24967 it->face_id = saved_face_id;
24969 /* Value is number of columns displayed. */
24970 return it->hpos - hpos_at_start;
24975 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24976 appears as an element of LIST or as the car of an element of LIST.
24977 If PROPVAL is a list, compare each element against LIST in that
24978 way, and return 1/2 if any element of PROPVAL is found in LIST.
24979 Otherwise return 0. This function cannot quit.
24980 The return value is 2 if the text is invisible but with an ellipsis
24981 and 1 if it's invisible and without an ellipsis. */
24984 invisible_prop (Lisp_Object propval, Lisp_Object list)
24986 Lisp_Object tail, proptail;
24988 for (tail = list; CONSP (tail); tail = XCDR (tail))
24990 register Lisp_Object tem;
24991 tem = XCAR (tail);
24992 if (EQ (propval, tem))
24993 return 1;
24994 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24995 return NILP (XCDR (tem)) ? 1 : 2;
24998 if (CONSP (propval))
25000 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25002 Lisp_Object propelt;
25003 propelt = XCAR (proptail);
25004 for (tail = list; CONSP (tail); tail = XCDR (tail))
25006 register Lisp_Object tem;
25007 tem = XCAR (tail);
25008 if (EQ (propelt, tem))
25009 return 1;
25010 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25011 return NILP (XCDR (tem)) ? 1 : 2;
25016 return 0;
25019 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25020 doc: /* Non-nil if the property makes the text invisible.
25021 POS-OR-PROP can be a marker or number, in which case it is taken to be
25022 a position in the current buffer and the value of the `invisible' property
25023 is checked; or it can be some other value, which is then presumed to be the
25024 value of the `invisible' property of the text of interest.
25025 The non-nil value returned can be t for truly invisible text or something
25026 else if the text is replaced by an ellipsis. */)
25027 (Lisp_Object pos_or_prop)
25029 Lisp_Object prop
25030 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
25031 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
25032 : pos_or_prop);
25033 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25034 return (invis == 0 ? Qnil
25035 : invis == 1 ? Qt
25036 : make_number (invis));
25039 /* Calculate a width or height in pixels from a specification using
25040 the following elements:
25042 SPEC ::=
25043 NUM - a (fractional) multiple of the default font width/height
25044 (NUM) - specifies exactly NUM pixels
25045 UNIT - a fixed number of pixels, see below.
25046 ELEMENT - size of a display element in pixels, see below.
25047 (NUM . SPEC) - equals NUM * SPEC
25048 (+ SPEC SPEC ...) - add pixel values
25049 (- SPEC SPEC ...) - subtract pixel values
25050 (- SPEC) - negate pixel value
25052 NUM ::=
25053 INT or FLOAT - a number constant
25054 SYMBOL - use symbol's (buffer local) variable binding.
25056 UNIT ::=
25057 in - pixels per inch *)
25058 mm - pixels per 1/1000 meter *)
25059 cm - pixels per 1/100 meter *)
25060 width - width of current font in pixels.
25061 height - height of current font in pixels.
25063 *) using the ratio(s) defined in display-pixels-per-inch.
25065 ELEMENT ::=
25067 left-fringe - left fringe width in pixels
25068 right-fringe - right fringe width in pixels
25070 left-margin - left margin width in pixels
25071 right-margin - right margin width in pixels
25073 scroll-bar - scroll-bar area width in pixels
25075 Examples:
25077 Pixels corresponding to 5 inches:
25078 (5 . in)
25080 Total width of non-text areas on left side of window (if scroll-bar is on left):
25081 '(space :width (+ left-fringe left-margin scroll-bar))
25083 Align to first text column (in header line):
25084 '(space :align-to 0)
25086 Align to middle of text area minus half the width of variable `my-image'
25087 containing a loaded image:
25088 '(space :align-to (0.5 . (- text my-image)))
25090 Width of left margin minus width of 1 character in the default font:
25091 '(space :width (- left-margin 1))
25093 Width of left margin minus width of 2 characters in the current font:
25094 '(space :width (- left-margin (2 . width)))
25096 Center 1 character over left-margin (in header line):
25097 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25099 Different ways to express width of left fringe plus left margin minus one pixel:
25100 '(space :width (- (+ left-fringe left-margin) (1)))
25101 '(space :width (+ left-fringe left-margin (- (1))))
25102 '(space :width (+ left-fringe left-margin (-1)))
25106 static bool
25107 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25108 struct font *font, bool width_p, int *align_to)
25110 double pixels;
25112 # define OK_PIXELS(val) (*res = (val), true)
25113 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25115 if (NILP (prop))
25116 return OK_PIXELS (0);
25118 eassert (FRAME_LIVE_P (it->f));
25120 if (SYMBOLP (prop))
25122 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25124 char *unit = SSDATA (SYMBOL_NAME (prop));
25126 if (unit[0] == 'i' && unit[1] == 'n')
25127 pixels = 1.0;
25128 else if (unit[0] == 'm' && unit[1] == 'm')
25129 pixels = 25.4;
25130 else if (unit[0] == 'c' && unit[1] == 'm')
25131 pixels = 2.54;
25132 else
25133 pixels = 0;
25134 if (pixels > 0)
25136 double ppi = (width_p ? FRAME_RES_X (it->f)
25137 : FRAME_RES_Y (it->f));
25139 if (ppi > 0)
25140 return OK_PIXELS (ppi / pixels);
25141 return false;
25145 #ifdef HAVE_WINDOW_SYSTEM
25146 if (EQ (prop, Qheight))
25147 return OK_PIXELS (font
25148 ? normal_char_height (font, -1)
25149 : FRAME_LINE_HEIGHT (it->f));
25150 if (EQ (prop, Qwidth))
25151 return OK_PIXELS (font
25152 ? FONT_WIDTH (font)
25153 : FRAME_COLUMN_WIDTH (it->f));
25154 #else
25155 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25156 return OK_PIXELS (1);
25157 #endif
25159 if (EQ (prop, Qtext))
25160 return OK_PIXELS (width_p
25161 ? window_box_width (it->w, TEXT_AREA)
25162 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25164 if (align_to && *align_to < 0)
25166 *res = 0;
25167 if (EQ (prop, Qleft))
25168 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
25169 if (EQ (prop, Qright))
25170 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25171 if (EQ (prop, Qcenter))
25172 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25173 + window_box_width (it->w, TEXT_AREA) / 2);
25174 if (EQ (prop, Qleft_fringe))
25175 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25176 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25177 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25178 if (EQ (prop, Qright_fringe))
25179 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25180 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25181 : window_box_right_offset (it->w, TEXT_AREA));
25182 if (EQ (prop, Qleft_margin))
25183 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25184 if (EQ (prop, Qright_margin))
25185 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25186 if (EQ (prop, Qscroll_bar))
25187 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25189 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25190 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25191 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25192 : 0)));
25194 else
25196 if (EQ (prop, Qleft_fringe))
25197 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25198 if (EQ (prop, Qright_fringe))
25199 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25200 if (EQ (prop, Qleft_margin))
25201 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25202 if (EQ (prop, Qright_margin))
25203 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25204 if (EQ (prop, Qscroll_bar))
25205 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25208 prop = buffer_local_value (prop, it->w->contents);
25209 if (EQ (prop, Qunbound))
25210 prop = Qnil;
25213 if (NUMBERP (prop))
25215 int base_unit = (width_p
25216 ? FRAME_COLUMN_WIDTH (it->f)
25217 : FRAME_LINE_HEIGHT (it->f));
25218 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25221 if (CONSP (prop))
25223 Lisp_Object car = XCAR (prop);
25224 Lisp_Object cdr = XCDR (prop);
25226 if (SYMBOLP (car))
25228 #ifdef HAVE_WINDOW_SYSTEM
25229 if (FRAME_WINDOW_P (it->f)
25230 && valid_image_p (prop))
25232 ptrdiff_t id = lookup_image (it->f, prop);
25233 struct image *img = IMAGE_FROM_ID (it->f, id);
25235 return OK_PIXELS (width_p ? img->width : img->height);
25237 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25239 /* TODO: Don't return dummy size. */
25240 return OK_PIXELS (100);
25242 #endif
25243 if (EQ (car, Qplus) || EQ (car, Qminus))
25245 bool first = true;
25246 double px;
25248 pixels = 0;
25249 while (CONSP (cdr))
25251 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25252 font, width_p, align_to))
25253 return false;
25254 if (first)
25255 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25256 else
25257 pixels += px;
25258 cdr = XCDR (cdr);
25260 if (EQ (car, Qminus))
25261 pixels = -pixels;
25262 return OK_PIXELS (pixels);
25265 car = buffer_local_value (car, it->w->contents);
25266 if (EQ (car, Qunbound))
25267 car = Qnil;
25270 if (NUMBERP (car))
25272 double fact;
25273 pixels = XFLOATINT (car);
25274 if (NILP (cdr))
25275 return OK_PIXELS (pixels);
25276 if (calc_pixel_width_or_height (&fact, it, cdr,
25277 font, width_p, align_to))
25278 return OK_PIXELS (pixels * fact);
25279 return false;
25282 return false;
25285 return false;
25288 void
25289 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25291 #ifdef HAVE_WINDOW_SYSTEM
25292 normal_char_ascent_descent (font, -1, ascent, descent);
25293 #else
25294 *ascent = 1;
25295 *descent = 0;
25296 #endif
25300 /***********************************************************************
25301 Glyph Display
25302 ***********************************************************************/
25304 #ifdef HAVE_WINDOW_SYSTEM
25306 #ifdef GLYPH_DEBUG
25308 void
25309 dump_glyph_string (struct glyph_string *s)
25311 fprintf (stderr, "glyph string\n");
25312 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25313 s->x, s->y, s->width, s->height);
25314 fprintf (stderr, " ybase = %d\n", s->ybase);
25315 fprintf (stderr, " hl = %u\n", s->hl);
25316 fprintf (stderr, " left overhang = %d, right = %d\n",
25317 s->left_overhang, s->right_overhang);
25318 fprintf (stderr, " nchars = %d\n", s->nchars);
25319 fprintf (stderr, " extends to end of line = %d\n",
25320 s->extends_to_end_of_line_p);
25321 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25322 fprintf (stderr, " bg width = %d\n", s->background_width);
25325 #endif /* GLYPH_DEBUG */
25327 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25328 of XChar2b structures for S; it can't be allocated in
25329 init_glyph_string because it must be allocated via `alloca'. W
25330 is the window on which S is drawn. ROW and AREA are the glyph row
25331 and area within the row from which S is constructed. START is the
25332 index of the first glyph structure covered by S. HL is a
25333 face-override for drawing S. */
25335 #ifdef HAVE_NTGUI
25336 #define OPTIONAL_HDC(hdc) HDC hdc,
25337 #define DECLARE_HDC(hdc) HDC hdc;
25338 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25339 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25340 #endif
25342 #ifndef OPTIONAL_HDC
25343 #define OPTIONAL_HDC(hdc)
25344 #define DECLARE_HDC(hdc)
25345 #define ALLOCATE_HDC(hdc, f)
25346 #define RELEASE_HDC(hdc, f)
25347 #endif
25349 static void
25350 init_glyph_string (struct glyph_string *s,
25351 OPTIONAL_HDC (hdc)
25352 XChar2b *char2b, struct window *w, struct glyph_row *row,
25353 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25355 memset (s, 0, sizeof *s);
25356 s->w = w;
25357 s->f = XFRAME (w->frame);
25358 #ifdef HAVE_NTGUI
25359 s->hdc = hdc;
25360 #endif
25361 s->display = FRAME_X_DISPLAY (s->f);
25362 s->char2b = char2b;
25363 s->hl = hl;
25364 s->row = row;
25365 s->area = area;
25366 s->first_glyph = row->glyphs[area] + start;
25367 s->height = row->height;
25368 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25369 s->ybase = s->y + row->ascent;
25373 /* Append the list of glyph strings with head H and tail T to the list
25374 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25376 static void
25377 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25378 struct glyph_string *h, struct glyph_string *t)
25380 if (h)
25382 if (*head)
25383 (*tail)->next = h;
25384 else
25385 *head = h;
25386 h->prev = *tail;
25387 *tail = t;
25392 /* Prepend the list of glyph strings with head H and tail T to the
25393 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25394 result. */
25396 static void
25397 prepend_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 (*head)->prev = t;
25404 else
25405 *tail = t;
25406 t->next = *head;
25407 *head = h;
25412 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25413 Set *HEAD and *TAIL to the resulting list. */
25415 static void
25416 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25417 struct glyph_string *s)
25419 s->next = s->prev = NULL;
25420 append_glyph_string_lists (head, tail, s, s);
25424 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25425 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25426 make sure that X resources for the face returned are allocated.
25427 Value is a pointer to a realized face that is ready for display if
25428 DISPLAY_P. */
25430 static struct face *
25431 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25432 XChar2b *char2b, bool display_p)
25434 struct face *face = FACE_FROM_ID (f, face_id);
25435 unsigned code = 0;
25437 if (face->font)
25439 code = face->font->driver->encode_char (face->font, c);
25441 if (code == FONT_INVALID_CODE)
25442 code = 0;
25444 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25446 /* Make sure X resources of the face are allocated. */
25447 #ifdef HAVE_X_WINDOWS
25448 if (display_p)
25449 #endif
25451 eassert (face != NULL);
25452 prepare_face_for_display (f, face);
25455 return face;
25459 /* Get face and two-byte form of character glyph GLYPH on frame F.
25460 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25461 a pointer to a realized face that is ready for display. */
25463 static struct face *
25464 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25465 XChar2b *char2b)
25467 struct face *face;
25468 unsigned code = 0;
25470 eassert (glyph->type == CHAR_GLYPH);
25471 face = FACE_FROM_ID (f, glyph->face_id);
25473 /* Make sure X resources of the face are allocated. */
25474 prepare_face_for_display (f, face);
25476 if (face->font)
25478 if (CHAR_BYTE8_P (glyph->u.ch))
25479 code = CHAR_TO_BYTE8 (glyph->u.ch);
25480 else
25481 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25483 if (code == FONT_INVALID_CODE)
25484 code = 0;
25487 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25488 return face;
25492 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25493 Return true iff FONT has a glyph for C. */
25495 static bool
25496 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25498 unsigned code;
25500 if (CHAR_BYTE8_P (c))
25501 code = CHAR_TO_BYTE8 (c);
25502 else
25503 code = font->driver->encode_char (font, c);
25505 if (code == FONT_INVALID_CODE)
25506 return false;
25507 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25508 return true;
25512 /* Fill glyph string S with composition components specified by S->cmp.
25514 BASE_FACE is the base face of the composition.
25515 S->cmp_from is the index of the first component for S.
25517 OVERLAPS non-zero means S should draw the foreground only, and use
25518 its physical height for clipping. See also draw_glyphs.
25520 Value is the index of a component not in S. */
25522 static int
25523 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25524 int overlaps)
25526 int i;
25527 /* For all glyphs of this composition, starting at the offset
25528 S->cmp_from, until we reach the end of the definition or encounter a
25529 glyph that requires the different face, add it to S. */
25530 struct face *face;
25532 eassert (s);
25534 s->for_overlaps = overlaps;
25535 s->face = NULL;
25536 s->font = NULL;
25537 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25539 int c = COMPOSITION_GLYPH (s->cmp, i);
25541 /* TAB in a composition means display glyphs with padding space
25542 on the left or right. */
25543 if (c != '\t')
25545 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25546 -1, Qnil);
25548 face = get_char_face_and_encoding (s->f, c, face_id,
25549 s->char2b + i, true);
25550 if (face)
25552 if (! s->face)
25554 s->face = face;
25555 s->font = s->face->font;
25557 else if (s->face != face)
25558 break;
25561 ++s->nchars;
25563 s->cmp_to = i;
25565 if (s->face == NULL)
25567 s->face = base_face->ascii_face;
25568 s->font = s->face->font;
25571 /* All glyph strings for the same composition has the same width,
25572 i.e. the width set for the first component of the composition. */
25573 s->width = s->first_glyph->pixel_width;
25575 /* If the specified font could not be loaded, use the frame's
25576 default font, but record the fact that we couldn't load it in
25577 the glyph string so that we can draw rectangles for the
25578 characters of the glyph string. */
25579 if (s->font == NULL)
25581 s->font_not_found_p = true;
25582 s->font = FRAME_FONT (s->f);
25585 /* Adjust base line for subscript/superscript text. */
25586 s->ybase += s->first_glyph->voffset;
25588 return s->cmp_to;
25591 static int
25592 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25593 int start, int end, int overlaps)
25595 struct glyph *glyph, *last;
25596 Lisp_Object lgstring;
25597 int i;
25599 s->for_overlaps = overlaps;
25600 glyph = s->row->glyphs[s->area] + start;
25601 last = s->row->glyphs[s->area] + end;
25602 s->cmp_id = glyph->u.cmp.id;
25603 s->cmp_from = glyph->slice.cmp.from;
25604 s->cmp_to = glyph->slice.cmp.to + 1;
25605 s->face = FACE_FROM_ID (s->f, face_id);
25606 lgstring = composition_gstring_from_id (s->cmp_id);
25607 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25608 glyph++;
25609 while (glyph < last
25610 && glyph->u.cmp.automatic
25611 && glyph->u.cmp.id == s->cmp_id
25612 && s->cmp_to == glyph->slice.cmp.from)
25613 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25615 for (i = s->cmp_from; i < s->cmp_to; i++)
25617 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25618 unsigned code = LGLYPH_CODE (lglyph);
25620 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25622 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25623 return glyph - s->row->glyphs[s->area];
25627 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25628 See the comment of fill_glyph_string for arguments.
25629 Value is the index of the first glyph not in S. */
25632 static int
25633 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25634 int start, int end, int overlaps)
25636 struct glyph *glyph, *last;
25637 int voffset;
25639 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25640 s->for_overlaps = overlaps;
25641 glyph = s->row->glyphs[s->area] + start;
25642 last = s->row->glyphs[s->area] + end;
25643 voffset = glyph->voffset;
25644 s->face = FACE_FROM_ID (s->f, face_id);
25645 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25646 s->nchars = 1;
25647 s->width = glyph->pixel_width;
25648 glyph++;
25649 while (glyph < last
25650 && glyph->type == GLYPHLESS_GLYPH
25651 && glyph->voffset == voffset
25652 && glyph->face_id == face_id)
25654 s->nchars++;
25655 s->width += glyph->pixel_width;
25656 glyph++;
25658 s->ybase += voffset;
25659 return glyph - s->row->glyphs[s->area];
25663 /* Fill glyph string S from a sequence of character glyphs.
25665 FACE_ID is the face id of the string. START is the index of the
25666 first glyph to consider, END is the index of the last + 1.
25667 OVERLAPS non-zero means S should draw the foreground only, and use
25668 its physical height for clipping. See also draw_glyphs.
25670 Value is the index of the first glyph not in S. */
25672 static int
25673 fill_glyph_string (struct glyph_string *s, int face_id,
25674 int start, int end, int overlaps)
25676 struct glyph *glyph, *last;
25677 int voffset;
25678 bool glyph_not_available_p;
25680 eassert (s->f == XFRAME (s->w->frame));
25681 eassert (s->nchars == 0);
25682 eassert (start >= 0 && end > start);
25684 s->for_overlaps = overlaps;
25685 glyph = s->row->glyphs[s->area] + start;
25686 last = s->row->glyphs[s->area] + end;
25687 voffset = glyph->voffset;
25688 s->padding_p = glyph->padding_p;
25689 glyph_not_available_p = glyph->glyph_not_available_p;
25691 while (glyph < last
25692 && glyph->type == CHAR_GLYPH
25693 && glyph->voffset == voffset
25694 /* Same face id implies same font, nowadays. */
25695 && glyph->face_id == face_id
25696 && glyph->glyph_not_available_p == glyph_not_available_p)
25698 s->face = get_glyph_face_and_encoding (s->f, glyph,
25699 s->char2b + s->nchars);
25700 ++s->nchars;
25701 eassert (s->nchars <= end - start);
25702 s->width += glyph->pixel_width;
25703 if (glyph++->padding_p != s->padding_p)
25704 break;
25707 s->font = s->face->font;
25709 /* If the specified font could not be loaded, use the frame's font,
25710 but record the fact that we couldn't load it in
25711 S->font_not_found_p so that we can draw rectangles for the
25712 characters of the glyph string. */
25713 if (s->font == NULL || glyph_not_available_p)
25715 s->font_not_found_p = true;
25716 s->font = FRAME_FONT (s->f);
25719 /* Adjust base line for subscript/superscript text. */
25720 s->ybase += voffset;
25722 eassert (s->face && s->face->gc);
25723 return glyph - s->row->glyphs[s->area];
25727 /* Fill glyph string S from image glyph S->first_glyph. */
25729 static void
25730 fill_image_glyph_string (struct glyph_string *s)
25732 eassert (s->first_glyph->type == IMAGE_GLYPH);
25733 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25734 eassert (s->img);
25735 s->slice = s->first_glyph->slice.img;
25736 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25737 s->font = s->face->font;
25738 s->width = s->first_glyph->pixel_width;
25740 /* Adjust base line for subscript/superscript text. */
25741 s->ybase += s->first_glyph->voffset;
25745 #ifdef HAVE_XWIDGETS
25746 static void
25747 fill_xwidget_glyph_string (struct glyph_string *s)
25749 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25750 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25751 s->font = s->face->font;
25752 s->width = s->first_glyph->pixel_width;
25753 s->ybase += s->first_glyph->voffset;
25754 s->xwidget = s->first_glyph->u.xwidget;
25756 #endif
25757 /* Fill glyph string S from a sequence of stretch glyphs.
25759 START is the index of the first glyph to consider,
25760 END is the index of the last + 1.
25762 Value is the index of the first glyph not in S. */
25764 static int
25765 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25767 struct glyph *glyph, *last;
25768 int voffset, face_id;
25770 eassert (s->first_glyph->type == STRETCH_GLYPH);
25772 glyph = s->row->glyphs[s->area] + start;
25773 last = s->row->glyphs[s->area] + end;
25774 face_id = glyph->face_id;
25775 s->face = FACE_FROM_ID (s->f, face_id);
25776 s->font = s->face->font;
25777 s->width = glyph->pixel_width;
25778 s->nchars = 1;
25779 voffset = glyph->voffset;
25781 for (++glyph;
25782 (glyph < last
25783 && glyph->type == STRETCH_GLYPH
25784 && glyph->voffset == voffset
25785 && glyph->face_id == face_id);
25786 ++glyph)
25787 s->width += glyph->pixel_width;
25789 /* Adjust base line for subscript/superscript text. */
25790 s->ybase += voffset;
25792 /* The case that face->gc == 0 is handled when drawing the glyph
25793 string by calling prepare_face_for_display. */
25794 eassert (s->face);
25795 return glyph - s->row->glyphs[s->area];
25798 static struct font_metrics *
25799 get_per_char_metric (struct font *font, XChar2b *char2b)
25801 static struct font_metrics metrics;
25802 unsigned code;
25804 if (! font)
25805 return NULL;
25806 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25807 if (code == FONT_INVALID_CODE)
25808 return NULL;
25809 font->driver->text_extents (font, &code, 1, &metrics);
25810 return &metrics;
25813 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25814 for FONT. Values are taken from font-global ones, except for fonts
25815 that claim preposterously large values, but whose glyphs actually
25816 have reasonable dimensions. C is the character to use for metrics
25817 if the font-global values are too large; if C is negative, the
25818 function selects a default character. */
25819 static void
25820 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25822 *ascent = FONT_BASE (font);
25823 *descent = FONT_DESCENT (font);
25825 if (FONT_TOO_HIGH (font))
25827 XChar2b char2b;
25829 /* Get metrics of C, defaulting to a reasonably sized ASCII
25830 character. */
25831 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25833 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25835 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25837 /* We add 1 pixel to character dimensions as heuristics
25838 that produces nicer display, e.g. when the face has
25839 the box attribute. */
25840 *ascent = pcm->ascent + 1;
25841 *descent = pcm->descent + 1;
25847 /* A subroutine that computes a reasonable "normal character height"
25848 for fonts that claim preposterously large vertical dimensions, but
25849 whose glyphs are actually reasonably sized. C is the character
25850 whose metrics to use for those fonts, or -1 for default
25851 character. */
25852 static int
25853 normal_char_height (struct font *font, int c)
25855 int ascent, descent;
25857 normal_char_ascent_descent (font, c, &ascent, &descent);
25859 return ascent + descent;
25862 /* EXPORT for RIF:
25863 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25864 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25865 assumed to be zero. */
25867 void
25868 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25870 *left = *right = 0;
25872 if (glyph->type == CHAR_GLYPH)
25874 XChar2b char2b;
25875 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25876 if (face->font)
25878 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25879 if (pcm)
25881 if (pcm->rbearing > pcm->width)
25882 *right = pcm->rbearing - pcm->width;
25883 if (pcm->lbearing < 0)
25884 *left = -pcm->lbearing;
25888 else if (glyph->type == COMPOSITE_GLYPH)
25890 if (! glyph->u.cmp.automatic)
25892 struct composition *cmp = composition_table[glyph->u.cmp.id];
25894 if (cmp->rbearing > cmp->pixel_width)
25895 *right = cmp->rbearing - cmp->pixel_width;
25896 if (cmp->lbearing < 0)
25897 *left = - cmp->lbearing;
25899 else
25901 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25902 struct font_metrics metrics;
25904 composition_gstring_width (gstring, glyph->slice.cmp.from,
25905 glyph->slice.cmp.to + 1, &metrics);
25906 if (metrics.rbearing > metrics.width)
25907 *right = metrics.rbearing - metrics.width;
25908 if (metrics.lbearing < 0)
25909 *left = - metrics.lbearing;
25915 /* Return the index of the first glyph preceding glyph string S that
25916 is overwritten by S because of S's left overhang. Value is -1
25917 if no glyphs are overwritten. */
25919 static int
25920 left_overwritten (struct glyph_string *s)
25922 int k;
25924 if (s->left_overhang)
25926 int x = 0, i;
25927 struct glyph *glyphs = s->row->glyphs[s->area];
25928 int first = s->first_glyph - glyphs;
25930 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25931 x -= glyphs[i].pixel_width;
25933 k = i + 1;
25935 else
25936 k = -1;
25938 return k;
25942 /* Return the index of the first glyph preceding glyph string S that
25943 is overwriting S because of its right overhang. Value is -1 if no
25944 glyph in front of S overwrites S. */
25946 static int
25947 left_overwriting (struct glyph_string *s)
25949 int i, k, x;
25950 struct glyph *glyphs = s->row->glyphs[s->area];
25951 int first = s->first_glyph - glyphs;
25953 k = -1;
25954 x = 0;
25955 for (i = first - 1; i >= 0; --i)
25957 int left, right;
25958 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25959 if (x + right > 0)
25960 k = i;
25961 x -= glyphs[i].pixel_width;
25964 return k;
25968 /* Return the index of the last glyph following glyph string S that is
25969 overwritten by S because of S's right overhang. Value is -1 if
25970 no such glyph is found. */
25972 static int
25973 right_overwritten (struct glyph_string *s)
25975 int k = -1;
25977 if (s->right_overhang)
25979 int x = 0, i;
25980 struct glyph *glyphs = s->row->glyphs[s->area];
25981 int first = (s->first_glyph - glyphs
25982 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25983 int end = s->row->used[s->area];
25985 for (i = first; i < end && s->right_overhang > x; ++i)
25986 x += glyphs[i].pixel_width;
25988 k = i;
25991 return k;
25995 /* Return the index of the last glyph following glyph string S that
25996 overwrites S because of its left overhang. Value is negative
25997 if no such glyph is found. */
25999 static int
26000 right_overwriting (struct glyph_string *s)
26002 int i, k, x;
26003 int end = s->row->used[s->area];
26004 struct glyph *glyphs = s->row->glyphs[s->area];
26005 int first = (s->first_glyph - glyphs
26006 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26008 k = -1;
26009 x = 0;
26010 for (i = first; i < end; ++i)
26012 int left, right;
26013 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26014 if (x - left < 0)
26015 k = i;
26016 x += glyphs[i].pixel_width;
26019 return k;
26023 /* Set background width of glyph string S. START is the index of the
26024 first glyph following S. LAST_X is the right-most x-position + 1
26025 in the drawing area. */
26027 static void
26028 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26030 /* If the face of this glyph string has to be drawn to the end of
26031 the drawing area, set S->extends_to_end_of_line_p. */
26033 if (start == s->row->used[s->area]
26034 && ((s->row->fill_line_p
26035 && (s->hl == DRAW_NORMAL_TEXT
26036 || s->hl == DRAW_IMAGE_RAISED
26037 || s->hl == DRAW_IMAGE_SUNKEN))
26038 || s->hl == DRAW_MOUSE_FACE))
26039 s->extends_to_end_of_line_p = true;
26041 /* If S extends its face to the end of the line, set its
26042 background_width to the distance to the right edge of the drawing
26043 area. */
26044 if (s->extends_to_end_of_line_p)
26045 s->background_width = last_x - s->x + 1;
26046 else
26047 s->background_width = s->width;
26051 /* Return glyph string that shares background with glyph string S and
26052 whose `background_width' member has been set. */
26054 static struct glyph_string *
26055 glyph_string_containing_background_width (struct glyph_string *s)
26057 if (s->cmp)
26058 while (s->cmp_from)
26059 s = s->prev;
26061 return s;
26065 /* Compute overhangs and x-positions for glyph string S and its
26066 predecessors, or successors. X is the starting x-position for S.
26067 BACKWARD_P means process predecessors. */
26069 static void
26070 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26072 if (backward_p)
26074 while (s)
26076 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26077 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26078 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26079 x -= s->width;
26080 s->x = x;
26081 s = s->prev;
26084 else
26086 while (s)
26088 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26089 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26090 s->x = x;
26091 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26092 x += s->width;
26093 s = s->next;
26100 /* The following macros are only called from draw_glyphs below.
26101 They reference the following parameters of that function directly:
26102 `w', `row', `area', and `overlap_p'
26103 as well as the following local variables:
26104 `s', `f', and `hdc' (in W32) */
26106 #ifdef HAVE_NTGUI
26107 /* On W32, silently add local `hdc' variable to argument list of
26108 init_glyph_string. */
26109 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26110 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26111 #else
26112 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26113 init_glyph_string (s, char2b, w, row, area, start, hl)
26114 #endif
26116 /* Add a glyph string for a stretch glyph to the list of strings
26117 between HEAD and TAIL. START is the index of the stretch glyph in
26118 row area AREA of glyph row ROW. END is the index of the last glyph
26119 in that glyph row area. X is the current output position assigned
26120 to the new glyph string constructed. HL overrides that face of the
26121 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26122 is the right-most x-position of the drawing area. */
26124 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26125 and below -- keep them on one line. */
26126 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26127 do \
26129 s = alloca (sizeof *s); \
26130 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26131 START = fill_stretch_glyph_string (s, START, END); \
26132 append_glyph_string (&HEAD, &TAIL, s); \
26133 s->x = (X); \
26135 while (false)
26138 /* Add a glyph string for an image glyph to the list of strings
26139 between HEAD and TAIL. START is the index of the image glyph in
26140 row area AREA of glyph row ROW. END is the index of the last glyph
26141 in that glyph row area. X is the current output position assigned
26142 to the new glyph string constructed. HL overrides that face of the
26143 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26144 is the right-most x-position of the drawing area. */
26146 #define BUILD_IMAGE_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 fill_image_glyph_string (s); \
26152 append_glyph_string (&HEAD, &TAIL, s); \
26153 ++START; \
26154 s->x = (X); \
26156 while (false)
26158 #ifndef HAVE_XWIDGETS
26159 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26160 eassume (false)
26161 #else
26162 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26163 do \
26165 s = alloca (sizeof *s); \
26166 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26167 fill_xwidget_glyph_string (s); \
26168 append_glyph_string (&(HEAD), &(TAIL), s); \
26169 ++(START); \
26170 s->x = (X); \
26172 while (false)
26173 #endif
26175 /* Add a glyph string for a sequence of character glyphs to the list
26176 of strings between HEAD and TAIL. START is the index of the first
26177 glyph in row area AREA of glyph row ROW that is part of the new
26178 glyph string. END is the index of the last glyph in that glyph row
26179 area. X is the current output position assigned to the new glyph
26180 string constructed. HL overrides that face of the glyph; e.g. it
26181 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26182 right-most x-position of the drawing area. */
26184 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26185 do \
26187 int face_id; \
26188 XChar2b *char2b; \
26190 face_id = (row)->glyphs[area][START].face_id; \
26192 s = alloca (sizeof *s); \
26193 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26194 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26195 append_glyph_string (&HEAD, &TAIL, s); \
26196 s->x = (X); \
26197 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26199 while (false)
26202 /* Add a glyph string for a composite sequence to the list of strings
26203 between HEAD and TAIL. START is the index of the first glyph in
26204 row area AREA of glyph row ROW that is part of the new glyph
26205 string. END is the index of the last glyph in that glyph row area.
26206 X is the current output position assigned to the new glyph string
26207 constructed. HL overrides that face of the glyph; e.g. it is
26208 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26209 x-position of the drawing area. */
26211 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26212 do { \
26213 int face_id = (row)->glyphs[area][START].face_id; \
26214 struct face *base_face = FACE_FROM_ID (f, face_id); \
26215 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26216 struct composition *cmp = composition_table[cmp_id]; \
26217 XChar2b *char2b; \
26218 struct glyph_string *first_s = NULL; \
26219 int n; \
26221 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26223 /* Make glyph_strings for each glyph sequence that is drawable by \
26224 the same face, and append them to HEAD/TAIL. */ \
26225 for (n = 0; n < cmp->glyph_len;) \
26227 s = alloca (sizeof *s); \
26228 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26229 append_glyph_string (&(HEAD), &(TAIL), s); \
26230 s->cmp = cmp; \
26231 s->cmp_from = n; \
26232 s->x = (X); \
26233 if (n == 0) \
26234 first_s = s; \
26235 n = fill_composite_glyph_string (s, base_face, overlaps); \
26238 ++START; \
26239 s = first_s; \
26240 } while (false)
26243 /* Add a glyph string for a glyph-string sequence to the list of strings
26244 between HEAD and TAIL. */
26246 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26247 do { \
26248 int face_id; \
26249 XChar2b *char2b; \
26250 Lisp_Object gstring; \
26252 face_id = (row)->glyphs[area][START].face_id; \
26253 gstring = (composition_gstring_from_id \
26254 ((row)->glyphs[area][START].u.cmp.id)); \
26255 s = alloca (sizeof *s); \
26256 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26257 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26258 append_glyph_string (&(HEAD), &(TAIL), s); \
26259 s->x = (X); \
26260 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26261 } while (false)
26264 /* Add a glyph string for a sequence of glyphless character's glyphs
26265 to the list of strings between HEAD and TAIL. The meanings of
26266 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26268 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26269 do \
26271 int face_id; \
26273 face_id = (row)->glyphs[area][START].face_id; \
26275 s = alloca (sizeof *s); \
26276 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26277 append_glyph_string (&HEAD, &TAIL, s); \
26278 s->x = (X); \
26279 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26280 overlaps); \
26282 while (false)
26285 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26286 of AREA of glyph row ROW on window W between indices START and END.
26287 HL overrides the face for drawing glyph strings, e.g. it is
26288 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26289 x-positions of the drawing area.
26291 This is an ugly monster macro construct because we must use alloca
26292 to allocate glyph strings (because draw_glyphs can be called
26293 asynchronously). */
26295 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26296 do \
26298 HEAD = TAIL = NULL; \
26299 while (START < END) \
26301 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26302 switch (first_glyph->type) \
26304 case CHAR_GLYPH: \
26305 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26306 HL, X, LAST_X); \
26307 break; \
26309 case COMPOSITE_GLYPH: \
26310 if (first_glyph->u.cmp.automatic) \
26311 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26312 HL, X, LAST_X); \
26313 else \
26314 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26315 HL, X, LAST_X); \
26316 break; \
26318 case STRETCH_GLYPH: \
26319 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26320 HL, X, LAST_X); \
26321 break; \
26323 case IMAGE_GLYPH: \
26324 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26325 HL, X, LAST_X); \
26326 break;
26328 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26329 case XWIDGET_GLYPH: \
26330 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26331 HL, X, LAST_X); \
26332 break;
26334 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26335 case GLYPHLESS_GLYPH: \
26336 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26337 HL, X, LAST_X); \
26338 break; \
26340 default: \
26341 emacs_abort (); \
26344 if (s) \
26346 set_glyph_string_background_width (s, START, LAST_X); \
26347 (X) += s->width; \
26350 } while (false)
26353 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26354 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26355 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26356 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26359 /* Draw glyphs between START and END in AREA of ROW on window W,
26360 starting at x-position X. X is relative to AREA in W. HL is a
26361 face-override with the following meaning:
26363 DRAW_NORMAL_TEXT draw normally
26364 DRAW_CURSOR draw in cursor face
26365 DRAW_MOUSE_FACE draw in mouse face.
26366 DRAW_INVERSE_VIDEO draw in mode line face
26367 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26368 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26370 If OVERLAPS is non-zero, draw only the foreground of characters and
26371 clip to the physical height of ROW. Non-zero value also defines
26372 the overlapping part to be drawn:
26374 OVERLAPS_PRED overlap with preceding rows
26375 OVERLAPS_SUCC overlap with succeeding rows
26376 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26377 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26379 Value is the x-position reached, relative to AREA of W. */
26381 static int
26382 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26383 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26384 enum draw_glyphs_face hl, int overlaps)
26386 struct glyph_string *head, *tail;
26387 struct glyph_string *s;
26388 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26389 int i, j, x_reached, last_x, area_left = 0;
26390 struct frame *f = XFRAME (WINDOW_FRAME (w));
26391 DECLARE_HDC (hdc);
26393 ALLOCATE_HDC (hdc, f);
26395 /* Let's rather be paranoid than getting a SEGV. */
26396 end = min (end, row->used[area]);
26397 start = clip_to_bounds (0, start, end);
26399 /* Translate X to frame coordinates. Set last_x to the right
26400 end of the drawing area. */
26401 if (row->full_width_p)
26403 /* X is relative to the left edge of W, without scroll bars
26404 or fringes. */
26405 area_left = WINDOW_LEFT_EDGE_X (w);
26406 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26407 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26409 else
26411 area_left = window_box_left (w, area);
26412 last_x = area_left + window_box_width (w, area);
26414 x += area_left;
26416 /* Build a doubly-linked list of glyph_string structures between
26417 head and tail from what we have to draw. Note that the macro
26418 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26419 the reason we use a separate variable `i'. */
26420 i = start;
26421 USE_SAFE_ALLOCA;
26422 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26423 if (tail)
26425 s = glyph_string_containing_background_width (tail);
26426 x_reached = s->x + s->background_width;
26428 else
26429 x_reached = x;
26431 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26432 the row, redraw some glyphs in front or following the glyph
26433 strings built above. */
26434 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26436 struct glyph_string *h, *t;
26437 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26438 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26439 bool check_mouse_face = false;
26440 int dummy_x = 0;
26442 /* If mouse highlighting is on, we may need to draw adjacent
26443 glyphs using mouse-face highlighting. */
26444 if (area == TEXT_AREA && row->mouse_face_p
26445 && hlinfo->mouse_face_beg_row >= 0
26446 && hlinfo->mouse_face_end_row >= 0)
26448 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26450 if (row_vpos >= hlinfo->mouse_face_beg_row
26451 && row_vpos <= hlinfo->mouse_face_end_row)
26453 check_mouse_face = true;
26454 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26455 ? hlinfo->mouse_face_beg_col : 0;
26456 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26457 ? hlinfo->mouse_face_end_col
26458 : row->used[TEXT_AREA];
26462 /* Compute overhangs for all glyph strings. */
26463 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26464 for (s = head; s; s = s->next)
26465 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26467 /* Prepend glyph strings for glyphs in front of the first glyph
26468 string that are overwritten because of the first glyph
26469 string's left overhang. The background of all strings
26470 prepended must be drawn because the first glyph string
26471 draws over it. */
26472 i = left_overwritten (head);
26473 if (i >= 0)
26475 enum draw_glyphs_face overlap_hl;
26477 /* If this row contains mouse highlighting, attempt to draw
26478 the overlapped glyphs with the correct highlight. This
26479 code fails if the overlap encompasses more than one glyph
26480 and mouse-highlight spans only some of these glyphs.
26481 However, making it work perfectly involves a lot more
26482 code, and I don't know if the pathological case occurs in
26483 practice, so we'll stick to this for now. --- cyd */
26484 if (check_mouse_face
26485 && mouse_beg_col < start && mouse_end_col > i)
26486 overlap_hl = DRAW_MOUSE_FACE;
26487 else
26488 overlap_hl = DRAW_NORMAL_TEXT;
26490 if (hl != overlap_hl)
26491 clip_head = head;
26492 j = i;
26493 BUILD_GLYPH_STRINGS (j, start, h, t,
26494 overlap_hl, dummy_x, last_x);
26495 start = i;
26496 compute_overhangs_and_x (t, head->x, true);
26497 prepend_glyph_string_lists (&head, &tail, h, t);
26498 if (clip_head == NULL)
26499 clip_head = head;
26502 /* Prepend glyph strings for glyphs in front of the first glyph
26503 string that overwrite that glyph string because of their
26504 right overhang. For these strings, only the foreground must
26505 be drawn, because it draws over the glyph string at `head'.
26506 The background must not be drawn because this would overwrite
26507 right overhangs of preceding glyphs for which no glyph
26508 strings exist. */
26509 i = left_overwriting (head);
26510 if (i >= 0)
26512 enum draw_glyphs_face overlap_hl;
26514 if (check_mouse_face
26515 && mouse_beg_col < start && mouse_end_col > i)
26516 overlap_hl = DRAW_MOUSE_FACE;
26517 else
26518 overlap_hl = DRAW_NORMAL_TEXT;
26520 if (hl == overlap_hl || clip_head == NULL)
26521 clip_head = head;
26522 BUILD_GLYPH_STRINGS (i, start, h, t,
26523 overlap_hl, dummy_x, last_x);
26524 for (s = h; s; s = s->next)
26525 s->background_filled_p = true;
26526 compute_overhangs_and_x (t, head->x, true);
26527 prepend_glyph_string_lists (&head, &tail, h, t);
26530 /* Append glyphs strings for glyphs following the last glyph
26531 string tail that are overwritten by tail. The background of
26532 these strings has to be drawn because tail's foreground draws
26533 over it. */
26534 i = right_overwritten (tail);
26535 if (i >= 0)
26537 enum draw_glyphs_face overlap_hl;
26539 if (check_mouse_face
26540 && mouse_beg_col < i && mouse_end_col > end)
26541 overlap_hl = DRAW_MOUSE_FACE;
26542 else
26543 overlap_hl = DRAW_NORMAL_TEXT;
26545 if (hl != overlap_hl)
26546 clip_tail = tail;
26547 BUILD_GLYPH_STRINGS (end, i, h, t,
26548 overlap_hl, x, last_x);
26549 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26550 we don't have `end = i;' here. */
26551 compute_overhangs_and_x (h, tail->x + tail->width, false);
26552 append_glyph_string_lists (&head, &tail, h, t);
26553 if (clip_tail == NULL)
26554 clip_tail = tail;
26557 /* Append glyph strings for glyphs following the last glyph
26558 string tail that overwrite tail. The foreground of such
26559 glyphs has to be drawn because it writes into the background
26560 of tail. The background must not be drawn because it could
26561 paint over the foreground of following glyphs. */
26562 i = right_overwriting (tail);
26563 if (i >= 0)
26565 enum draw_glyphs_face overlap_hl;
26566 if (check_mouse_face
26567 && mouse_beg_col < i && mouse_end_col > end)
26568 overlap_hl = DRAW_MOUSE_FACE;
26569 else
26570 overlap_hl = DRAW_NORMAL_TEXT;
26572 if (hl == overlap_hl || clip_tail == NULL)
26573 clip_tail = tail;
26574 i++; /* We must include the Ith glyph. */
26575 BUILD_GLYPH_STRINGS (end, i, h, t,
26576 overlap_hl, x, last_x);
26577 for (s = h; s; s = s->next)
26578 s->background_filled_p = true;
26579 compute_overhangs_and_x (h, tail->x + tail->width, false);
26580 append_glyph_string_lists (&head, &tail, h, t);
26582 tail = glyph_string_containing_background_width (tail);
26583 if (clip_tail)
26584 clip_tail = glyph_string_containing_background_width (clip_tail);
26585 if (clip_head || clip_tail)
26586 for (s = head; s; s = s->next)
26588 s->clip_head = clip_head;
26589 s->clip_tail = clip_tail;
26593 /* Draw all strings. */
26594 for (s = head; s; s = s->next)
26595 FRAME_RIF (f)->draw_glyph_string (s);
26597 #ifndef HAVE_NS
26598 /* When focus a sole frame and move horizontally, this clears on_p
26599 causing a failure to erase prev cursor position. */
26600 if (area == TEXT_AREA
26601 && !row->full_width_p
26602 /* When drawing overlapping rows, only the glyph strings'
26603 foreground is drawn, which doesn't erase a cursor
26604 completely. */
26605 && !overlaps)
26607 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26608 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26609 : (tail ? tail->x + tail->background_width : x));
26610 x0 -= area_left;
26611 x1 -= area_left;
26613 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26614 row->y, MATRIX_ROW_BOTTOM_Y (row));
26616 #endif
26618 /* Value is the x-position up to which drawn, relative to AREA of W.
26619 This doesn't include parts drawn because of overhangs. */
26620 if (row->full_width_p)
26621 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26622 else
26623 x_reached -= area_left;
26625 RELEASE_HDC (hdc, f);
26627 SAFE_FREE ();
26628 return x_reached;
26631 /* Find the first glyph in the run of underlined glyphs preceding the
26632 beginning of glyph string S, and return its font (which could be
26633 NULL). This is needed because that font determines the underline
26634 position and thickness for the entire run of the underlined glyphs.
26635 This function is called from the draw_glyph_string method of GUI
26636 frame's redisplay interface (RIF) when it needs to draw in an
26637 underlined face. */
26638 struct font *
26639 font_for_underline_metrics (struct glyph_string *s)
26641 struct glyph *g0 = s->row->glyphs[s->area], *g;
26643 for (g = s->first_glyph - 1; g >= g0; g--)
26645 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26646 if (!(prev_face && prev_face->underline_p))
26647 break;
26650 /* If preceding glyphs are not underlined, use the font of S. */
26651 if (g == s->first_glyph - 1)
26652 return s->font;
26653 else
26655 /* Otherwise use the font of the last glyph we saw in the above
26656 loop whose face had the underline_p flag set. */
26657 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26661 /* Expand row matrix if too narrow. Don't expand if area
26662 is not present. */
26664 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26666 if (!it->f->fonts_changed \
26667 && (it->glyph_row->glyphs[area] \
26668 < it->glyph_row->glyphs[area + 1])) \
26670 it->w->ncols_scale_factor++; \
26671 it->f->fonts_changed = true; \
26675 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26676 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26678 static void
26679 append_glyph (struct it *it)
26681 struct glyph *glyph;
26682 enum glyph_row_area area = it->area;
26684 eassert (it->glyph_row);
26685 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26687 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26688 if (glyph < it->glyph_row->glyphs[area + 1])
26690 /* If the glyph row is reversed, we need to prepend the glyph
26691 rather than append it. */
26692 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26694 struct glyph *g;
26696 /* Make room for the additional glyph. */
26697 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26698 g[1] = *g;
26699 glyph = it->glyph_row->glyphs[area];
26701 glyph->charpos = CHARPOS (it->position);
26702 glyph->object = it->object;
26703 if (it->pixel_width > 0)
26705 eassert (it->pixel_width <= SHRT_MAX);
26706 glyph->pixel_width = it->pixel_width;
26707 glyph->padding_p = false;
26709 else
26711 /* Assure at least 1-pixel width. Otherwise, cursor can't
26712 be displayed correctly. */
26713 glyph->pixel_width = 1;
26714 glyph->padding_p = true;
26716 glyph->ascent = it->ascent;
26717 glyph->descent = it->descent;
26718 glyph->voffset = it->voffset;
26719 glyph->type = CHAR_GLYPH;
26720 glyph->avoid_cursor_p = it->avoid_cursor_p;
26721 glyph->multibyte_p = it->multibyte_p;
26722 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26724 /* In R2L rows, the left and the right box edges need to be
26725 drawn in reverse direction. */
26726 glyph->right_box_line_p = it->start_of_box_run_p;
26727 glyph->left_box_line_p = it->end_of_box_run_p;
26729 else
26731 glyph->left_box_line_p = it->start_of_box_run_p;
26732 glyph->right_box_line_p = it->end_of_box_run_p;
26734 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26735 || it->phys_descent > it->descent);
26736 glyph->glyph_not_available_p = it->glyph_not_available_p;
26737 glyph->face_id = it->face_id;
26738 glyph->u.ch = it->char_to_display;
26739 glyph->slice.img = null_glyph_slice;
26740 glyph->font_type = FONT_TYPE_UNKNOWN;
26741 if (it->bidi_p)
26743 glyph->resolved_level = it->bidi_it.resolved_level;
26744 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26745 glyph->bidi_type = it->bidi_it.type;
26747 else
26749 glyph->resolved_level = 0;
26750 glyph->bidi_type = UNKNOWN_BT;
26752 ++it->glyph_row->used[area];
26754 else
26755 IT_EXPAND_MATRIX_WIDTH (it, area);
26758 /* Store one glyph for the composition IT->cmp_it.id in
26759 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26760 non-null. */
26762 static void
26763 append_composite_glyph (struct it *it)
26765 struct glyph *glyph;
26766 enum glyph_row_area area = it->area;
26768 eassert (it->glyph_row);
26770 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26771 if (glyph < it->glyph_row->glyphs[area + 1])
26773 /* If the glyph row is reversed, we need to prepend the glyph
26774 rather than append it. */
26775 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26777 struct glyph *g;
26779 /* Make room for the new glyph. */
26780 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26781 g[1] = *g;
26782 glyph = it->glyph_row->glyphs[it->area];
26784 glyph->charpos = it->cmp_it.charpos;
26785 glyph->object = it->object;
26786 eassert (it->pixel_width <= SHRT_MAX);
26787 glyph->pixel_width = it->pixel_width;
26788 glyph->ascent = it->ascent;
26789 glyph->descent = it->descent;
26790 glyph->voffset = it->voffset;
26791 glyph->type = COMPOSITE_GLYPH;
26792 if (it->cmp_it.ch < 0)
26794 glyph->u.cmp.automatic = false;
26795 glyph->u.cmp.id = it->cmp_it.id;
26796 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26798 else
26800 glyph->u.cmp.automatic = true;
26801 glyph->u.cmp.id = it->cmp_it.id;
26802 glyph->slice.cmp.from = it->cmp_it.from;
26803 glyph->slice.cmp.to = it->cmp_it.to - 1;
26805 glyph->avoid_cursor_p = it->avoid_cursor_p;
26806 glyph->multibyte_p = it->multibyte_p;
26807 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26809 /* In R2L rows, the left and the right box edges need to be
26810 drawn in reverse direction. */
26811 glyph->right_box_line_p = it->start_of_box_run_p;
26812 glyph->left_box_line_p = it->end_of_box_run_p;
26814 else
26816 glyph->left_box_line_p = it->start_of_box_run_p;
26817 glyph->right_box_line_p = it->end_of_box_run_p;
26819 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26820 || it->phys_descent > it->descent);
26821 glyph->padding_p = false;
26822 glyph->glyph_not_available_p = false;
26823 glyph->face_id = it->face_id;
26824 glyph->font_type = FONT_TYPE_UNKNOWN;
26825 if (it->bidi_p)
26827 glyph->resolved_level = it->bidi_it.resolved_level;
26828 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26829 glyph->bidi_type = it->bidi_it.type;
26831 ++it->glyph_row->used[area];
26833 else
26834 IT_EXPAND_MATRIX_WIDTH (it, area);
26838 /* Change IT->ascent and IT->height according to the setting of
26839 IT->voffset. */
26841 static void
26842 take_vertical_position_into_account (struct it *it)
26844 if (it->voffset)
26846 if (it->voffset < 0)
26847 /* Increase the ascent so that we can display the text higher
26848 in the line. */
26849 it->ascent -= it->voffset;
26850 else
26851 /* Increase the descent so that we can display the text lower
26852 in the line. */
26853 it->descent += it->voffset;
26858 /* Produce glyphs/get display metrics for the image IT is loaded with.
26859 See the description of struct display_iterator in dispextern.h for
26860 an overview of struct display_iterator. */
26862 static void
26863 produce_image_glyph (struct it *it)
26865 struct image *img;
26866 struct face *face;
26867 int glyph_ascent, crop;
26868 struct glyph_slice slice;
26870 eassert (it->what == IT_IMAGE);
26872 face = FACE_FROM_ID (it->f, it->face_id);
26873 /* Make sure X resources of the face is loaded. */
26874 prepare_face_for_display (it->f, face);
26876 if (it->image_id < 0)
26878 /* Fringe bitmap. */
26879 it->ascent = it->phys_ascent = 0;
26880 it->descent = it->phys_descent = 0;
26881 it->pixel_width = 0;
26882 it->nglyphs = 0;
26883 return;
26886 img = IMAGE_FROM_ID (it->f, it->image_id);
26887 /* Make sure X resources of the image is loaded. */
26888 prepare_image_for_display (it->f, img);
26890 slice.x = slice.y = 0;
26891 slice.width = img->width;
26892 slice.height = img->height;
26894 if (INTEGERP (it->slice.x))
26895 slice.x = XINT (it->slice.x);
26896 else if (FLOATP (it->slice.x))
26897 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26899 if (INTEGERP (it->slice.y))
26900 slice.y = XINT (it->slice.y);
26901 else if (FLOATP (it->slice.y))
26902 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26904 if (INTEGERP (it->slice.width))
26905 slice.width = XINT (it->slice.width);
26906 else if (FLOATP (it->slice.width))
26907 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26909 if (INTEGERP (it->slice.height))
26910 slice.height = XINT (it->slice.height);
26911 else if (FLOATP (it->slice.height))
26912 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26914 if (slice.x >= img->width)
26915 slice.x = img->width;
26916 if (slice.y >= img->height)
26917 slice.y = img->height;
26918 if (slice.x + slice.width >= img->width)
26919 slice.width = img->width - slice.x;
26920 if (slice.y + slice.height > img->height)
26921 slice.height = img->height - slice.y;
26923 if (slice.width == 0 || slice.height == 0)
26924 return;
26926 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26928 it->descent = slice.height - glyph_ascent;
26929 if (slice.y == 0)
26930 it->descent += img->vmargin;
26931 if (slice.y + slice.height == img->height)
26932 it->descent += img->vmargin;
26933 it->phys_descent = it->descent;
26935 it->pixel_width = slice.width;
26936 if (slice.x == 0)
26937 it->pixel_width += img->hmargin;
26938 if (slice.x + slice.width == img->width)
26939 it->pixel_width += img->hmargin;
26941 /* It's quite possible for images to have an ascent greater than
26942 their height, so don't get confused in that case. */
26943 if (it->descent < 0)
26944 it->descent = 0;
26946 it->nglyphs = 1;
26948 if (face->box != FACE_NO_BOX)
26950 if (face->box_line_width > 0)
26952 if (slice.y == 0)
26953 it->ascent += face->box_line_width;
26954 if (slice.y + slice.height == img->height)
26955 it->descent += face->box_line_width;
26958 if (it->start_of_box_run_p && slice.x == 0)
26959 it->pixel_width += eabs (face->box_line_width);
26960 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26961 it->pixel_width += eabs (face->box_line_width);
26964 take_vertical_position_into_account (it);
26966 /* Automatically crop wide image glyphs at right edge so we can
26967 draw the cursor on same display row. */
26968 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26969 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26971 it->pixel_width -= crop;
26972 slice.width -= crop;
26975 if (it->glyph_row)
26977 struct glyph *glyph;
26978 enum glyph_row_area area = it->area;
26980 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26981 if (it->glyph_row->reversed_p)
26983 struct glyph *g;
26985 /* Make room for the new glyph. */
26986 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26987 g[1] = *g;
26988 glyph = it->glyph_row->glyphs[it->area];
26990 if (glyph < it->glyph_row->glyphs[area + 1])
26992 glyph->charpos = CHARPOS (it->position);
26993 glyph->object = it->object;
26994 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26995 glyph->ascent = glyph_ascent;
26996 glyph->descent = it->descent;
26997 glyph->voffset = it->voffset;
26998 glyph->type = IMAGE_GLYPH;
26999 glyph->avoid_cursor_p = it->avoid_cursor_p;
27000 glyph->multibyte_p = it->multibyte_p;
27001 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27003 /* In R2L rows, the left and the right box edges need to be
27004 drawn in reverse direction. */
27005 glyph->right_box_line_p = it->start_of_box_run_p;
27006 glyph->left_box_line_p = it->end_of_box_run_p;
27008 else
27010 glyph->left_box_line_p = it->start_of_box_run_p;
27011 glyph->right_box_line_p = it->end_of_box_run_p;
27013 glyph->overlaps_vertically_p = false;
27014 glyph->padding_p = false;
27015 glyph->glyph_not_available_p = false;
27016 glyph->face_id = it->face_id;
27017 glyph->u.img_id = img->id;
27018 glyph->slice.img = slice;
27019 glyph->font_type = FONT_TYPE_UNKNOWN;
27020 if (it->bidi_p)
27022 glyph->resolved_level = it->bidi_it.resolved_level;
27023 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27024 glyph->bidi_type = it->bidi_it.type;
27026 ++it->glyph_row->used[area];
27028 else
27029 IT_EXPAND_MATRIX_WIDTH (it, area);
27033 static void
27034 produce_xwidget_glyph (struct it *it)
27036 #ifdef HAVE_XWIDGETS
27037 struct xwidget *xw;
27038 int glyph_ascent, crop;
27039 eassert (it->what == IT_XWIDGET);
27041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27042 /* Make sure X resources of the face is loaded. */
27043 prepare_face_for_display (it->f, face);
27045 xw = it->xwidget;
27046 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27047 it->descent = xw->height/2;
27048 it->phys_descent = it->descent;
27049 it->pixel_width = xw->width;
27050 /* It's quite possible for images to have an ascent greater than
27051 their height, so don't get confused in that case. */
27052 if (it->descent < 0)
27053 it->descent = 0;
27055 it->nglyphs = 1;
27057 if (face->box != FACE_NO_BOX)
27059 if (face->box_line_width > 0)
27061 it->ascent += face->box_line_width;
27062 it->descent += face->box_line_width;
27065 if (it->start_of_box_run_p)
27066 it->pixel_width += eabs (face->box_line_width);
27067 it->pixel_width += eabs (face->box_line_width);
27070 take_vertical_position_into_account (it);
27072 /* Automatically crop wide image glyphs at right edge so we can
27073 draw the cursor on same display row. */
27074 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27075 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27076 it->pixel_width -= crop;
27078 if (it->glyph_row)
27080 enum glyph_row_area area = it->area;
27081 struct glyph *glyph
27082 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27084 if (it->glyph_row->reversed_p)
27086 struct glyph *g;
27088 /* Make room for the new glyph. */
27089 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27090 g[1] = *g;
27091 glyph = it->glyph_row->glyphs[it->area];
27093 if (glyph < it->glyph_row->glyphs[area + 1])
27095 glyph->charpos = CHARPOS (it->position);
27096 glyph->object = it->object;
27097 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27098 glyph->ascent = glyph_ascent;
27099 glyph->descent = it->descent;
27100 glyph->voffset = it->voffset;
27101 glyph->type = XWIDGET_GLYPH;
27102 glyph->avoid_cursor_p = it->avoid_cursor_p;
27103 glyph->multibyte_p = it->multibyte_p;
27104 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27106 /* In R2L rows, the left and the right box edges need to be
27107 drawn in reverse direction. */
27108 glyph->right_box_line_p = it->start_of_box_run_p;
27109 glyph->left_box_line_p = it->end_of_box_run_p;
27111 else
27113 glyph->left_box_line_p = it->start_of_box_run_p;
27114 glyph->right_box_line_p = it->end_of_box_run_p;
27116 glyph->overlaps_vertically_p = 0;
27117 glyph->padding_p = 0;
27118 glyph->glyph_not_available_p = 0;
27119 glyph->face_id = it->face_id;
27120 glyph->u.xwidget = it->xwidget;
27121 glyph->font_type = FONT_TYPE_UNKNOWN;
27122 if (it->bidi_p)
27124 glyph->resolved_level = it->bidi_it.resolved_level;
27125 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27126 glyph->bidi_type = it->bidi_it.type;
27128 ++it->glyph_row->used[area];
27130 else
27131 IT_EXPAND_MATRIX_WIDTH (it, area);
27133 #endif
27136 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27137 of the glyph, WIDTH and HEIGHT are the width and height of the
27138 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27140 static void
27141 append_stretch_glyph (struct it *it, Lisp_Object object,
27142 int width, int height, int ascent)
27144 struct glyph *glyph;
27145 enum glyph_row_area area = it->area;
27147 eassert (ascent >= 0 && ascent <= height);
27149 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27150 if (glyph < it->glyph_row->glyphs[area + 1])
27152 /* If the glyph row is reversed, we need to prepend the glyph
27153 rather than append it. */
27154 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27156 struct glyph *g;
27158 /* Make room for the additional glyph. */
27159 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27160 g[1] = *g;
27161 glyph = it->glyph_row->glyphs[area];
27163 /* Decrease the width of the first glyph of the row that
27164 begins before first_visible_x (e.g., due to hscroll).
27165 This is so the overall width of the row becomes smaller
27166 by the scroll amount, and the stretch glyph appended by
27167 extend_face_to_end_of_line will be wider, to shift the
27168 row glyphs to the right. (In L2R rows, the corresponding
27169 left-shift effect is accomplished by setting row->x to a
27170 negative value, which won't work with R2L rows.)
27172 This must leave us with a positive value of WIDTH, since
27173 otherwise the call to move_it_in_display_line_to at the
27174 beginning of display_line would have got past the entire
27175 first glyph, and then it->current_x would have been
27176 greater or equal to it->first_visible_x. */
27177 if (it->current_x < it->first_visible_x)
27178 width -= it->first_visible_x - it->current_x;
27179 eassert (width > 0);
27181 glyph->charpos = CHARPOS (it->position);
27182 glyph->object = object;
27183 /* FIXME: It would be better to use TYPE_MAX here, but
27184 __typeof__ is not portable enough... */
27185 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27186 glyph->ascent = ascent;
27187 glyph->descent = height - ascent;
27188 glyph->voffset = it->voffset;
27189 glyph->type = STRETCH_GLYPH;
27190 glyph->avoid_cursor_p = it->avoid_cursor_p;
27191 glyph->multibyte_p = it->multibyte_p;
27192 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27194 /* In R2L rows, the left and the right box edges need to be
27195 drawn in reverse direction. */
27196 glyph->right_box_line_p = it->start_of_box_run_p;
27197 glyph->left_box_line_p = it->end_of_box_run_p;
27199 else
27201 glyph->left_box_line_p = it->start_of_box_run_p;
27202 glyph->right_box_line_p = it->end_of_box_run_p;
27204 glyph->overlaps_vertically_p = false;
27205 glyph->padding_p = false;
27206 glyph->glyph_not_available_p = false;
27207 glyph->face_id = it->face_id;
27208 glyph->u.stretch.ascent = ascent;
27209 glyph->u.stretch.height = height;
27210 glyph->slice.img = null_glyph_slice;
27211 glyph->font_type = FONT_TYPE_UNKNOWN;
27212 if (it->bidi_p)
27214 glyph->resolved_level = it->bidi_it.resolved_level;
27215 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27216 glyph->bidi_type = it->bidi_it.type;
27218 else
27220 glyph->resolved_level = 0;
27221 glyph->bidi_type = UNKNOWN_BT;
27223 ++it->glyph_row->used[area];
27225 else
27226 IT_EXPAND_MATRIX_WIDTH (it, area);
27229 #endif /* HAVE_WINDOW_SYSTEM */
27231 /* Produce a stretch glyph for iterator IT. IT->object is the value
27232 of the glyph property displayed. The value must be a list
27233 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27234 being recognized:
27236 1. `:width WIDTH' specifies that the space should be WIDTH *
27237 canonical char width wide. WIDTH may be an integer or floating
27238 point number.
27240 2. `:relative-width FACTOR' specifies that the width of the stretch
27241 should be computed from the width of the first character having the
27242 `glyph' property, and should be FACTOR times that width.
27244 3. `:align-to HPOS' specifies that the space should be wide enough
27245 to reach HPOS, a value in canonical character units.
27247 Exactly one of the above pairs must be present.
27249 4. `:height HEIGHT' specifies that the height of the stretch produced
27250 should be HEIGHT, measured in canonical character units.
27252 5. `:relative-height FACTOR' specifies that the height of the
27253 stretch should be FACTOR times the height of the characters having
27254 the glyph property.
27256 Either none or exactly one of 4 or 5 must be present.
27258 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27259 of the stretch should be used for the ascent of the stretch.
27260 ASCENT must be in the range 0 <= ASCENT <= 100. */
27262 void
27263 produce_stretch_glyph (struct it *it)
27265 /* (space :width WIDTH :height HEIGHT ...) */
27266 Lisp_Object prop, plist;
27267 int width = 0, height = 0, align_to = -1;
27268 bool zero_width_ok_p = false;
27269 double tem;
27270 struct font *font = NULL;
27272 #ifdef HAVE_WINDOW_SYSTEM
27273 int ascent = 0;
27274 bool zero_height_ok_p = false;
27276 if (FRAME_WINDOW_P (it->f))
27278 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27279 font = face->font ? face->font : FRAME_FONT (it->f);
27280 prepare_face_for_display (it->f, face);
27282 #endif
27284 /* List should start with `space'. */
27285 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27286 plist = XCDR (it->object);
27288 /* Compute the width of the stretch. */
27289 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27290 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27292 /* Absolute width `:width WIDTH' specified and valid. */
27293 zero_width_ok_p = true;
27294 width = (int)tem;
27296 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27298 /* Relative width `:relative-width FACTOR' specified and valid.
27299 Compute the width of the characters having the `glyph'
27300 property. */
27301 struct it it2;
27302 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27304 it2 = *it;
27305 if (it->multibyte_p)
27306 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27307 else
27309 it2.c = it2.char_to_display = *p, it2.len = 1;
27310 if (! ASCII_CHAR_P (it2.c))
27311 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27314 it2.glyph_row = NULL;
27315 it2.what = IT_CHARACTER;
27316 PRODUCE_GLYPHS (&it2);
27317 width = NUMVAL (prop) * it2.pixel_width;
27319 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27320 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27321 &align_to))
27323 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27324 align_to = (align_to < 0
27326 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27327 else if (align_to < 0)
27328 align_to = window_box_left_offset (it->w, TEXT_AREA);
27329 width = max (0, (int)tem + align_to - it->current_x);
27330 zero_width_ok_p = true;
27332 else
27333 /* Nothing specified -> width defaults to canonical char width. */
27334 width = FRAME_COLUMN_WIDTH (it->f);
27336 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27337 width = 1;
27339 #ifdef HAVE_WINDOW_SYSTEM
27340 /* Compute height. */
27341 if (FRAME_WINDOW_P (it->f))
27343 int default_height = normal_char_height (font, ' ');
27345 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27346 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27348 height = (int)tem;
27349 zero_height_ok_p = true;
27351 else if (prop = Fplist_get (plist, QCrelative_height),
27352 NUMVAL (prop) > 0)
27353 height = default_height * NUMVAL (prop);
27354 else
27355 height = default_height;
27357 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27358 height = 1;
27360 /* Compute percentage of height used for ascent. If
27361 `:ascent ASCENT' is present and valid, use that. Otherwise,
27362 derive the ascent from the font in use. */
27363 if (prop = Fplist_get (plist, QCascent),
27364 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27365 ascent = height * NUMVAL (prop) / 100.0;
27366 else if (!NILP (prop)
27367 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27368 ascent = min (max (0, (int)tem), height);
27369 else
27370 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27372 else
27373 #endif /* HAVE_WINDOW_SYSTEM */
27374 height = 1;
27376 if (width > 0 && it->line_wrap != TRUNCATE
27377 && it->current_x + width > it->last_visible_x)
27379 width = it->last_visible_x - it->current_x;
27380 #ifdef HAVE_WINDOW_SYSTEM
27381 /* Subtract one more pixel from the stretch width, but only on
27382 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27383 width -= FRAME_WINDOW_P (it->f);
27384 #endif
27387 if (width > 0 && height > 0 && it->glyph_row)
27389 Lisp_Object o_object = it->object;
27390 Lisp_Object object = it->stack[it->sp - 1].string;
27391 int n = width;
27393 if (!STRINGP (object))
27394 object = it->w->contents;
27395 #ifdef HAVE_WINDOW_SYSTEM
27396 if (FRAME_WINDOW_P (it->f))
27397 append_stretch_glyph (it, object, width, height, ascent);
27398 else
27399 #endif
27401 it->object = object;
27402 it->char_to_display = ' ';
27403 it->pixel_width = it->len = 1;
27404 while (n--)
27405 tty_append_glyph (it);
27406 it->object = o_object;
27410 it->pixel_width = width;
27411 #ifdef HAVE_WINDOW_SYSTEM
27412 if (FRAME_WINDOW_P (it->f))
27414 it->ascent = it->phys_ascent = ascent;
27415 it->descent = it->phys_descent = height - it->ascent;
27416 it->nglyphs = width > 0 && height > 0;
27417 take_vertical_position_into_account (it);
27419 else
27420 #endif
27421 it->nglyphs = width;
27424 /* Get information about special display element WHAT in an
27425 environment described by IT. WHAT is one of IT_TRUNCATION or
27426 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27427 non-null glyph_row member. This function ensures that fields like
27428 face_id, c, len of IT are left untouched. */
27430 static void
27431 produce_special_glyphs (struct it *it, enum display_element_type what)
27433 struct it temp_it;
27434 Lisp_Object gc;
27435 GLYPH glyph;
27437 temp_it = *it;
27438 temp_it.object = Qnil;
27439 memset (&temp_it.current, 0, sizeof temp_it.current);
27441 if (what == IT_CONTINUATION)
27443 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27444 if (it->bidi_it.paragraph_dir == R2L)
27445 SET_GLYPH_FROM_CHAR (glyph, '/');
27446 else
27447 SET_GLYPH_FROM_CHAR (glyph, '\\');
27448 if (it->dp
27449 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27451 /* FIXME: Should we mirror GC for R2L lines? */
27452 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27453 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27456 else if (what == IT_TRUNCATION)
27458 /* Truncation glyph. */
27459 SET_GLYPH_FROM_CHAR (glyph, '$');
27460 if (it->dp
27461 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27463 /* FIXME: Should we mirror GC for R2L lines? */
27464 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27465 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27468 else
27469 emacs_abort ();
27471 #ifdef HAVE_WINDOW_SYSTEM
27472 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27473 is turned off, we precede the truncation/continuation glyphs by a
27474 stretch glyph whose width is computed such that these special
27475 glyphs are aligned at the window margin, even when very different
27476 fonts are used in different glyph rows. */
27477 if (FRAME_WINDOW_P (temp_it.f)
27478 /* init_iterator calls this with it->glyph_row == NULL, and it
27479 wants only the pixel width of the truncation/continuation
27480 glyphs. */
27481 && temp_it.glyph_row
27482 /* insert_left_trunc_glyphs calls us at the beginning of the
27483 row, and it has its own calculation of the stretch glyph
27484 width. */
27485 && temp_it.glyph_row->used[TEXT_AREA] > 0
27486 && (temp_it.glyph_row->reversed_p
27487 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27488 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27490 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27492 if (stretch_width > 0)
27494 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27495 struct font *font =
27496 face->font ? face->font : FRAME_FONT (temp_it.f);
27497 int stretch_ascent =
27498 (((temp_it.ascent + temp_it.descent)
27499 * FONT_BASE (font)) / FONT_HEIGHT (font));
27501 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27502 temp_it.ascent + temp_it.descent,
27503 stretch_ascent);
27506 #endif
27508 temp_it.dp = NULL;
27509 temp_it.what = IT_CHARACTER;
27510 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27511 temp_it.face_id = GLYPH_FACE (glyph);
27512 temp_it.len = CHAR_BYTES (temp_it.c);
27514 PRODUCE_GLYPHS (&temp_it);
27515 it->pixel_width = temp_it.pixel_width;
27516 it->nglyphs = temp_it.nglyphs;
27519 #ifdef HAVE_WINDOW_SYSTEM
27521 /* Calculate line-height and line-spacing properties.
27522 An integer value specifies explicit pixel value.
27523 A float value specifies relative value to current face height.
27524 A cons (float . face-name) specifies relative value to
27525 height of specified face font.
27527 Returns height in pixels, or nil. */
27529 static Lisp_Object
27530 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27531 int boff, bool override)
27533 Lisp_Object face_name = Qnil;
27534 int ascent, descent, height;
27536 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27537 return val;
27539 if (CONSP (val))
27541 face_name = XCAR (val);
27542 val = XCDR (val);
27543 if (!NUMBERP (val))
27544 val = make_number (1);
27545 if (NILP (face_name))
27547 height = it->ascent + it->descent;
27548 goto scale;
27552 if (NILP (face_name))
27554 font = FRAME_FONT (it->f);
27555 boff = FRAME_BASELINE_OFFSET (it->f);
27557 else if (EQ (face_name, Qt))
27559 override = false;
27561 else
27563 int face_id;
27564 struct face *face;
27566 face_id = lookup_named_face (it->f, face_name, false);
27567 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27568 if (face == NULL || ((font = face->font) == NULL))
27569 return make_number (-1);
27570 boff = font->baseline_offset;
27571 if (font->vertical_centering)
27572 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27575 normal_char_ascent_descent (font, -1, &ascent, &descent);
27577 if (override)
27579 it->override_ascent = ascent;
27580 it->override_descent = descent;
27581 it->override_boff = boff;
27584 height = ascent + descent;
27586 scale:
27587 if (FLOATP (val))
27588 height = (int)(XFLOAT_DATA (val) * height);
27589 else if (INTEGERP (val))
27590 height *= XINT (val);
27592 return make_number (height);
27596 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27597 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27598 and only if this is for a character for which no font was found.
27600 If the display method (it->glyphless_method) is
27601 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27602 length of the acronym or the hexadecimal string, UPPER_XOFF and
27603 UPPER_YOFF are pixel offsets for the upper part of the string,
27604 LOWER_XOFF and LOWER_YOFF are for the lower part.
27606 For the other display methods, LEN through LOWER_YOFF are zero. */
27608 static void
27609 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27610 short upper_xoff, short upper_yoff,
27611 short lower_xoff, short lower_yoff)
27613 struct glyph *glyph;
27614 enum glyph_row_area area = it->area;
27616 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27617 if (glyph < it->glyph_row->glyphs[area + 1])
27619 /* If the glyph row is reversed, we need to prepend the glyph
27620 rather than append it. */
27621 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27623 struct glyph *g;
27625 /* Make room for the additional glyph. */
27626 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27627 g[1] = *g;
27628 glyph = it->glyph_row->glyphs[area];
27630 glyph->charpos = CHARPOS (it->position);
27631 glyph->object = it->object;
27632 eassert (it->pixel_width <= SHRT_MAX);
27633 glyph->pixel_width = it->pixel_width;
27634 glyph->ascent = it->ascent;
27635 glyph->descent = it->descent;
27636 glyph->voffset = it->voffset;
27637 glyph->type = GLYPHLESS_GLYPH;
27638 glyph->u.glyphless.method = it->glyphless_method;
27639 glyph->u.glyphless.for_no_font = for_no_font;
27640 glyph->u.glyphless.len = len;
27641 glyph->u.glyphless.ch = it->c;
27642 glyph->slice.glyphless.upper_xoff = upper_xoff;
27643 glyph->slice.glyphless.upper_yoff = upper_yoff;
27644 glyph->slice.glyphless.lower_xoff = lower_xoff;
27645 glyph->slice.glyphless.lower_yoff = lower_yoff;
27646 glyph->avoid_cursor_p = it->avoid_cursor_p;
27647 glyph->multibyte_p = it->multibyte_p;
27648 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27650 /* In R2L rows, the left and the right box edges need to be
27651 drawn in reverse direction. */
27652 glyph->right_box_line_p = it->start_of_box_run_p;
27653 glyph->left_box_line_p = it->end_of_box_run_p;
27655 else
27657 glyph->left_box_line_p = it->start_of_box_run_p;
27658 glyph->right_box_line_p = it->end_of_box_run_p;
27660 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27661 || it->phys_descent > it->descent);
27662 glyph->padding_p = false;
27663 glyph->glyph_not_available_p = false;
27664 glyph->face_id = face_id;
27665 glyph->font_type = FONT_TYPE_UNKNOWN;
27666 if (it->bidi_p)
27668 glyph->resolved_level = it->bidi_it.resolved_level;
27669 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27670 glyph->bidi_type = it->bidi_it.type;
27672 ++it->glyph_row->used[area];
27674 else
27675 IT_EXPAND_MATRIX_WIDTH (it, area);
27679 /* Produce a glyph for a glyphless character for iterator IT.
27680 IT->glyphless_method specifies which method to use for displaying
27681 the character. See the description of enum
27682 glyphless_display_method in dispextern.h for the detail.
27684 FOR_NO_FONT is true if and only if this is for a character for
27685 which no font was found. ACRONYM, if non-nil, is an acronym string
27686 for the character. */
27688 static void
27689 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27691 int face_id;
27692 struct face *face;
27693 struct font *font;
27694 int base_width, base_height, width, height;
27695 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27696 int len;
27698 /* Get the metrics of the base font. We always refer to the current
27699 ASCII face. */
27700 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27701 font = face->font ? face->font : FRAME_FONT (it->f);
27702 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27703 it->ascent += font->baseline_offset;
27704 it->descent -= font->baseline_offset;
27705 base_height = it->ascent + it->descent;
27706 base_width = font->average_width;
27708 face_id = merge_glyphless_glyph_face (it);
27710 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27712 it->pixel_width = THIN_SPACE_WIDTH;
27713 len = 0;
27714 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27716 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27718 width = CHARACTER_WIDTH (it->c);
27719 if (width == 0)
27720 width = 1;
27721 else if (width > 4)
27722 width = 4;
27723 it->pixel_width = base_width * width;
27724 len = 0;
27725 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27727 else
27729 char buf[7];
27730 const char *str;
27731 unsigned int code[6];
27732 int upper_len;
27733 int ascent, descent;
27734 struct font_metrics metrics_upper, metrics_lower;
27736 face = FACE_FROM_ID (it->f, face_id);
27737 font = face->font ? face->font : FRAME_FONT (it->f);
27738 prepare_face_for_display (it->f, face);
27740 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27742 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27743 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27744 if (CONSP (acronym))
27745 acronym = XCAR (acronym);
27746 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27748 else
27750 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27751 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27752 str = buf;
27754 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27755 code[len] = font->driver->encode_char (font, str[len]);
27756 upper_len = (len + 1) / 2;
27757 font->driver->text_extents (font, code, upper_len,
27758 &metrics_upper);
27759 font->driver->text_extents (font, code + upper_len, len - upper_len,
27760 &metrics_lower);
27764 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27765 width = max (metrics_upper.width, metrics_lower.width) + 4;
27766 upper_xoff = upper_yoff = 2; /* the typical case */
27767 if (base_width >= width)
27769 /* Align the upper to the left, the lower to the right. */
27770 it->pixel_width = base_width;
27771 lower_xoff = base_width - 2 - metrics_lower.width;
27773 else
27775 /* Center the shorter one. */
27776 it->pixel_width = width;
27777 if (metrics_upper.width >= metrics_lower.width)
27778 lower_xoff = (width - metrics_lower.width) / 2;
27779 else
27781 /* FIXME: This code doesn't look right. It formerly was
27782 missing the "lower_xoff = 0;", which couldn't have
27783 been right since it left lower_xoff uninitialized. */
27784 lower_xoff = 0;
27785 upper_xoff = (width - metrics_upper.width) / 2;
27789 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27790 top, bottom, and between upper and lower strings. */
27791 height = (metrics_upper.ascent + metrics_upper.descent
27792 + metrics_lower.ascent + metrics_lower.descent) + 5;
27793 /* Center vertically.
27794 H:base_height, D:base_descent
27795 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27797 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27798 descent = D - H/2 + h/2;
27799 lower_yoff = descent - 2 - ld;
27800 upper_yoff = lower_yoff - la - 1 - ud; */
27801 ascent = - (it->descent - (base_height + height + 1) / 2);
27802 descent = it->descent - (base_height - height) / 2;
27803 lower_yoff = descent - 2 - metrics_lower.descent;
27804 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27805 - metrics_upper.descent);
27806 /* Don't make the height shorter than the base height. */
27807 if (height > base_height)
27809 it->ascent = ascent;
27810 it->descent = descent;
27814 it->phys_ascent = it->ascent;
27815 it->phys_descent = it->descent;
27816 if (it->glyph_row)
27817 append_glyphless_glyph (it, face_id, for_no_font, len,
27818 upper_xoff, upper_yoff,
27819 lower_xoff, lower_yoff);
27820 it->nglyphs = 1;
27821 take_vertical_position_into_account (it);
27825 /* RIF:
27826 Produce glyphs/get display metrics for the display element IT is
27827 loaded with. See the description of struct it in dispextern.h
27828 for an overview of struct it. */
27830 void
27831 x_produce_glyphs (struct it *it)
27833 int extra_line_spacing = it->extra_line_spacing;
27835 it->glyph_not_available_p = false;
27837 if (it->what == IT_CHARACTER)
27839 XChar2b char2b;
27840 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27841 struct font *font = face->font;
27842 struct font_metrics *pcm = NULL;
27843 int boff; /* Baseline offset. */
27845 if (font == NULL)
27847 /* When no suitable font is found, display this character by
27848 the method specified in the first extra slot of
27849 Vglyphless_char_display. */
27850 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27852 eassert (it->what == IT_GLYPHLESS);
27853 produce_glyphless_glyph (it, true,
27854 STRINGP (acronym) ? acronym : Qnil);
27855 goto done;
27858 boff = font->baseline_offset;
27859 if (font->vertical_centering)
27860 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27862 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27864 it->nglyphs = 1;
27866 if (it->override_ascent >= 0)
27868 it->ascent = it->override_ascent;
27869 it->descent = it->override_descent;
27870 boff = it->override_boff;
27872 else
27874 it->ascent = FONT_BASE (font) + boff;
27875 it->descent = FONT_DESCENT (font) - boff;
27878 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27880 pcm = get_per_char_metric (font, &char2b);
27881 if (pcm->width == 0
27882 && pcm->rbearing == 0 && pcm->lbearing == 0)
27883 pcm = NULL;
27886 if (pcm)
27888 it->phys_ascent = pcm->ascent + boff;
27889 it->phys_descent = pcm->descent - boff;
27890 it->pixel_width = pcm->width;
27891 /* Don't use font-global values for ascent and descent
27892 if they result in an exceedingly large line height. */
27893 if (it->override_ascent < 0)
27895 if (FONT_TOO_HIGH (font))
27897 it->ascent = it->phys_ascent;
27898 it->descent = it->phys_descent;
27899 /* These limitations are enforced by an
27900 assertion near the end of this function. */
27901 if (it->ascent < 0)
27902 it->ascent = 0;
27903 if (it->descent < 0)
27904 it->descent = 0;
27908 else
27910 it->glyph_not_available_p = true;
27911 it->phys_ascent = it->ascent;
27912 it->phys_descent = it->descent;
27913 it->pixel_width = font->space_width;
27916 if (it->constrain_row_ascent_descent_p)
27918 if (it->descent > it->max_descent)
27920 it->ascent += it->descent - it->max_descent;
27921 it->descent = it->max_descent;
27923 if (it->ascent > it->max_ascent)
27925 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27926 it->ascent = it->max_ascent;
27928 it->phys_ascent = min (it->phys_ascent, it->ascent);
27929 it->phys_descent = min (it->phys_descent, it->descent);
27930 extra_line_spacing = 0;
27933 /* If this is a space inside a region of text with
27934 `space-width' property, change its width. */
27935 bool stretched_p
27936 = it->char_to_display == ' ' && !NILP (it->space_width);
27937 if (stretched_p)
27938 it->pixel_width *= XFLOATINT (it->space_width);
27940 /* If face has a box, add the box thickness to the character
27941 height. If character has a box line to the left and/or
27942 right, add the box line width to the character's width. */
27943 if (face->box != FACE_NO_BOX)
27945 int thick = face->box_line_width;
27947 if (thick > 0)
27949 it->ascent += thick;
27950 it->descent += thick;
27952 else
27953 thick = -thick;
27955 if (it->start_of_box_run_p)
27956 it->pixel_width += thick;
27957 if (it->end_of_box_run_p)
27958 it->pixel_width += thick;
27961 /* If face has an overline, add the height of the overline
27962 (1 pixel) and a 1 pixel margin to the character height. */
27963 if (face->overline_p)
27964 it->ascent += overline_margin;
27966 if (it->constrain_row_ascent_descent_p)
27968 if (it->ascent > it->max_ascent)
27969 it->ascent = it->max_ascent;
27970 if (it->descent > it->max_descent)
27971 it->descent = it->max_descent;
27974 take_vertical_position_into_account (it);
27976 /* If we have to actually produce glyphs, do it. */
27977 if (it->glyph_row)
27979 if (stretched_p)
27981 /* Translate a space with a `space-width' property
27982 into a stretch glyph. */
27983 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27984 / FONT_HEIGHT (font));
27985 append_stretch_glyph (it, it->object, it->pixel_width,
27986 it->ascent + it->descent, ascent);
27988 else
27989 append_glyph (it);
27991 /* If characters with lbearing or rbearing are displayed
27992 in this line, record that fact in a flag of the
27993 glyph row. This is used to optimize X output code. */
27994 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27995 it->glyph_row->contains_overlapping_glyphs_p = true;
27997 if (! stretched_p && it->pixel_width == 0)
27998 /* We assure that all visible glyphs have at least 1-pixel
27999 width. */
28000 it->pixel_width = 1;
28002 else if (it->char_to_display == '\n')
28004 /* A newline has no width, but we need the height of the
28005 line. But if previous part of the line sets a height,
28006 don't increase that height. */
28008 Lisp_Object height;
28009 Lisp_Object total_height = Qnil;
28011 it->override_ascent = -1;
28012 it->pixel_width = 0;
28013 it->nglyphs = 0;
28015 height = get_it_property (it, Qline_height);
28016 /* Split (line-height total-height) list. */
28017 if (CONSP (height)
28018 && CONSP (XCDR (height))
28019 && NILP (XCDR (XCDR (height))))
28021 total_height = XCAR (XCDR (height));
28022 height = XCAR (height);
28024 height = calc_line_height_property (it, height, font, boff, true);
28026 if (it->override_ascent >= 0)
28028 it->ascent = it->override_ascent;
28029 it->descent = it->override_descent;
28030 boff = it->override_boff;
28032 else
28034 if (FONT_TOO_HIGH (font))
28036 it->ascent = font->pixel_size + boff - 1;
28037 it->descent = -boff + 1;
28038 if (it->descent < 0)
28039 it->descent = 0;
28041 else
28043 it->ascent = FONT_BASE (font) + boff;
28044 it->descent = FONT_DESCENT (font) - boff;
28048 if (EQ (height, Qt))
28050 if (it->descent > it->max_descent)
28052 it->ascent += it->descent - it->max_descent;
28053 it->descent = it->max_descent;
28055 if (it->ascent > it->max_ascent)
28057 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28058 it->ascent = it->max_ascent;
28060 it->phys_ascent = min (it->phys_ascent, it->ascent);
28061 it->phys_descent = min (it->phys_descent, it->descent);
28062 it->constrain_row_ascent_descent_p = true;
28063 extra_line_spacing = 0;
28065 else
28067 Lisp_Object spacing;
28069 it->phys_ascent = it->ascent;
28070 it->phys_descent = it->descent;
28072 if ((it->max_ascent > 0 || it->max_descent > 0)
28073 && face->box != FACE_NO_BOX
28074 && face->box_line_width > 0)
28076 it->ascent += face->box_line_width;
28077 it->descent += face->box_line_width;
28079 if (!NILP (height)
28080 && XINT (height) > it->ascent + it->descent)
28081 it->ascent = XINT (height) - it->descent;
28083 if (!NILP (total_height))
28084 spacing = calc_line_height_property (it, total_height, font,
28085 boff, false);
28086 else
28088 spacing = get_it_property (it, Qline_spacing);
28089 spacing = calc_line_height_property (it, spacing, font,
28090 boff, false);
28092 if (INTEGERP (spacing))
28094 extra_line_spacing = XINT (spacing);
28095 if (!NILP (total_height))
28096 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28100 else /* i.e. (it->char_to_display == '\t') */
28102 if (font->space_width > 0)
28104 int tab_width = it->tab_width * font->space_width;
28105 int x = it->current_x + it->continuation_lines_width;
28106 int x0 = x;
28107 /* Adjust for line numbers, if needed. */
28108 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28109 x -= it->lnum_pixel_width;
28110 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28112 /* If the distance from the current position to the next tab
28113 stop is less than a space character width, use the
28114 tab stop after that. */
28115 if (next_tab_x - x < font->space_width)
28116 next_tab_x += tab_width;
28117 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28118 next_tab_x += (it->lnum_pixel_width
28119 - ((it->w->hscroll * font->space_width)
28120 % tab_width));
28122 it->pixel_width = next_tab_x - x0;
28123 it->nglyphs = 1;
28124 if (FONT_TOO_HIGH (font))
28126 if (get_char_glyph_code (' ', font, &char2b))
28128 pcm = get_per_char_metric (font, &char2b);
28129 if (pcm->width == 0
28130 && pcm->rbearing == 0 && pcm->lbearing == 0)
28131 pcm = NULL;
28134 if (pcm)
28136 it->ascent = pcm->ascent + boff;
28137 it->descent = pcm->descent - boff;
28139 else
28141 it->ascent = font->pixel_size + boff - 1;
28142 it->descent = -boff + 1;
28144 if (it->ascent < 0)
28145 it->ascent = 0;
28146 if (it->descent < 0)
28147 it->descent = 0;
28149 else
28151 it->ascent = FONT_BASE (font) + boff;
28152 it->descent = FONT_DESCENT (font) - boff;
28154 it->phys_ascent = it->ascent;
28155 it->phys_descent = it->descent;
28157 if (it->glyph_row)
28159 append_stretch_glyph (it, it->object, it->pixel_width,
28160 it->ascent + it->descent, it->ascent);
28163 else
28165 it->pixel_width = 0;
28166 it->nglyphs = 1;
28170 if (FONT_TOO_HIGH (font))
28172 int font_ascent, font_descent;
28174 /* For very large fonts, where we ignore the declared font
28175 dimensions, and go by per-character metrics instead,
28176 don't let the row ascent and descent values (and the row
28177 height computed from them) be smaller than the "normal"
28178 character metrics. This avoids unpleasant effects
28179 whereby lines on display would change their height
28180 depending on which characters are shown. */
28181 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28182 it->max_ascent = max (it->max_ascent, font_ascent);
28183 it->max_descent = max (it->max_descent, font_descent);
28186 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28188 /* A static composition.
28190 Note: A composition is represented as one glyph in the
28191 glyph matrix. There are no padding glyphs.
28193 Important note: pixel_width, ascent, and descent are the
28194 values of what is drawn by draw_glyphs (i.e. the values of
28195 the overall glyphs composed). */
28196 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28197 int boff; /* baseline offset */
28198 struct composition *cmp = composition_table[it->cmp_it.id];
28199 int glyph_len = cmp->glyph_len;
28200 struct font *font = face->font;
28202 it->nglyphs = 1;
28204 /* If we have not yet calculated pixel size data of glyphs of
28205 the composition for the current face font, calculate them
28206 now. Theoretically, we have to check all fonts for the
28207 glyphs, but that requires much time and memory space. So,
28208 here we check only the font of the first glyph. This may
28209 lead to incorrect display, but it's very rare, and C-l
28210 (recenter-top-bottom) can correct the display anyway. */
28211 if (! cmp->font || cmp->font != font)
28213 /* Ascent and descent of the font of the first character
28214 of this composition (adjusted by baseline offset).
28215 Ascent and descent of overall glyphs should not be less
28216 than these, respectively. */
28217 int font_ascent, font_descent, font_height;
28218 /* Bounding box of the overall glyphs. */
28219 int leftmost, rightmost, lowest, highest;
28220 int lbearing, rbearing;
28221 int i, width, ascent, descent;
28222 int c;
28223 XChar2b char2b;
28224 struct font_metrics *pcm;
28225 ptrdiff_t pos;
28227 eassume (0 < glyph_len); /* See Bug#8512. */
28229 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28230 while (c == '\t' && 0 < --glyph_len);
28232 bool right_padded = glyph_len < cmp->glyph_len;
28233 for (i = 0; i < glyph_len; i++)
28235 c = COMPOSITION_GLYPH (cmp, i);
28236 if (c != '\t')
28237 break;
28238 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28240 bool left_padded = i > 0;
28242 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28243 : IT_CHARPOS (*it));
28244 /* If no suitable font is found, use the default font. */
28245 bool font_not_found_p = font == NULL;
28246 if (font_not_found_p)
28248 face = face->ascii_face;
28249 font = face->font;
28251 boff = font->baseline_offset;
28252 if (font->vertical_centering)
28253 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28254 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28255 font_ascent += boff;
28256 font_descent -= boff;
28257 font_height = font_ascent + font_descent;
28259 cmp->font = font;
28261 pcm = NULL;
28262 if (! font_not_found_p)
28264 get_char_face_and_encoding (it->f, c, it->face_id,
28265 &char2b, false);
28266 pcm = get_per_char_metric (font, &char2b);
28269 /* Initialize the bounding box. */
28270 if (pcm)
28272 width = cmp->glyph_len > 0 ? pcm->width : 0;
28273 ascent = pcm->ascent;
28274 descent = pcm->descent;
28275 lbearing = pcm->lbearing;
28276 rbearing = pcm->rbearing;
28278 else
28280 width = cmp->glyph_len > 0 ? font->space_width : 0;
28281 ascent = FONT_BASE (font);
28282 descent = FONT_DESCENT (font);
28283 lbearing = 0;
28284 rbearing = width;
28287 rightmost = width;
28288 leftmost = 0;
28289 lowest = - descent + boff;
28290 highest = ascent + boff;
28292 if (! font_not_found_p
28293 && font->default_ascent
28294 && CHAR_TABLE_P (Vuse_default_ascent)
28295 && !NILP (Faref (Vuse_default_ascent,
28296 make_number (it->char_to_display))))
28297 highest = font->default_ascent + boff;
28299 /* Draw the first glyph at the normal position. It may be
28300 shifted to right later if some other glyphs are drawn
28301 at the left. */
28302 cmp->offsets[i * 2] = 0;
28303 cmp->offsets[i * 2 + 1] = boff;
28304 cmp->lbearing = lbearing;
28305 cmp->rbearing = rbearing;
28307 /* Set cmp->offsets for the remaining glyphs. */
28308 for (i++; i < glyph_len; i++)
28310 int left, right, btm, top;
28311 int ch = COMPOSITION_GLYPH (cmp, i);
28312 int face_id;
28313 struct face *this_face;
28315 if (ch == '\t')
28316 ch = ' ';
28317 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28318 this_face = FACE_FROM_ID (it->f, face_id);
28319 font = this_face->font;
28321 if (font == NULL)
28322 pcm = NULL;
28323 else
28325 get_char_face_and_encoding (it->f, ch, face_id,
28326 &char2b, false);
28327 pcm = get_per_char_metric (font, &char2b);
28329 if (! pcm)
28330 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28331 else
28333 width = pcm->width;
28334 ascent = pcm->ascent;
28335 descent = pcm->descent;
28336 lbearing = pcm->lbearing;
28337 rbearing = pcm->rbearing;
28338 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28340 /* Relative composition with or without
28341 alternate chars. */
28342 left = (leftmost + rightmost - width) / 2;
28343 btm = - descent + boff;
28344 if (font->relative_compose
28345 && (! CHAR_TABLE_P (Vignore_relative_composition)
28346 || NILP (Faref (Vignore_relative_composition,
28347 make_number (ch)))))
28350 if (- descent >= font->relative_compose)
28351 /* One extra pixel between two glyphs. */
28352 btm = highest + 1;
28353 else if (ascent <= 0)
28354 /* One extra pixel between two glyphs. */
28355 btm = lowest - 1 - ascent - descent;
28358 else
28360 /* A composition rule is specified by an integer
28361 value that encodes global and new reference
28362 points (GREF and NREF). GREF and NREF are
28363 specified by numbers as below:
28365 0---1---2 -- ascent
28369 9--10--11 -- center
28371 ---3---4---5--- baseline
28373 6---7---8 -- descent
28375 int rule = COMPOSITION_RULE (cmp, i);
28376 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28378 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28379 grefx = gref % 3, nrefx = nref % 3;
28380 grefy = gref / 3, nrefy = nref / 3;
28381 if (xoff)
28382 xoff = font_height * (xoff - 128) / 256;
28383 if (yoff)
28384 yoff = font_height * (yoff - 128) / 256;
28386 left = (leftmost
28387 + grefx * (rightmost - leftmost) / 2
28388 - nrefx * width / 2
28389 + xoff);
28391 btm = ((grefy == 0 ? highest
28392 : grefy == 1 ? 0
28393 : grefy == 2 ? lowest
28394 : (highest + lowest) / 2)
28395 - (nrefy == 0 ? ascent + descent
28396 : nrefy == 1 ? descent - boff
28397 : nrefy == 2 ? 0
28398 : (ascent + descent) / 2)
28399 + yoff);
28402 cmp->offsets[i * 2] = left;
28403 cmp->offsets[i * 2 + 1] = btm + descent;
28405 /* Update the bounding box of the overall glyphs. */
28406 if (width > 0)
28408 right = left + width;
28409 if (left < leftmost)
28410 leftmost = left;
28411 if (right > rightmost)
28412 rightmost = right;
28414 top = btm + descent + ascent;
28415 if (top > highest)
28416 highest = top;
28417 if (btm < lowest)
28418 lowest = btm;
28420 if (cmp->lbearing > left + lbearing)
28421 cmp->lbearing = left + lbearing;
28422 if (cmp->rbearing < left + rbearing)
28423 cmp->rbearing = left + rbearing;
28427 /* If there are glyphs whose x-offsets are negative,
28428 shift all glyphs to the right and make all x-offsets
28429 non-negative. */
28430 if (leftmost < 0)
28432 for (i = 0; i < cmp->glyph_len; i++)
28433 cmp->offsets[i * 2] -= leftmost;
28434 rightmost -= leftmost;
28435 cmp->lbearing -= leftmost;
28436 cmp->rbearing -= leftmost;
28439 if (left_padded && cmp->lbearing < 0)
28441 for (i = 0; i < cmp->glyph_len; i++)
28442 cmp->offsets[i * 2] -= cmp->lbearing;
28443 rightmost -= cmp->lbearing;
28444 cmp->rbearing -= cmp->lbearing;
28445 cmp->lbearing = 0;
28447 if (right_padded && rightmost < cmp->rbearing)
28449 rightmost = cmp->rbearing;
28452 cmp->pixel_width = rightmost;
28453 cmp->ascent = highest;
28454 cmp->descent = - lowest;
28455 if (cmp->ascent < font_ascent)
28456 cmp->ascent = font_ascent;
28457 if (cmp->descent < font_descent)
28458 cmp->descent = font_descent;
28461 if (it->glyph_row
28462 && (cmp->lbearing < 0
28463 || cmp->rbearing > cmp->pixel_width))
28464 it->glyph_row->contains_overlapping_glyphs_p = true;
28466 it->pixel_width = cmp->pixel_width;
28467 it->ascent = it->phys_ascent = cmp->ascent;
28468 it->descent = it->phys_descent = cmp->descent;
28469 if (face->box != FACE_NO_BOX)
28471 int thick = face->box_line_width;
28473 if (thick > 0)
28475 it->ascent += thick;
28476 it->descent += thick;
28478 else
28479 thick = - thick;
28481 if (it->start_of_box_run_p)
28482 it->pixel_width += thick;
28483 if (it->end_of_box_run_p)
28484 it->pixel_width += thick;
28487 /* If face has an overline, add the height of the overline
28488 (1 pixel) and a 1 pixel margin to the character height. */
28489 if (face->overline_p)
28490 it->ascent += overline_margin;
28492 take_vertical_position_into_account (it);
28493 if (it->ascent < 0)
28494 it->ascent = 0;
28495 if (it->descent < 0)
28496 it->descent = 0;
28498 if (it->glyph_row && cmp->glyph_len > 0)
28499 append_composite_glyph (it);
28501 else if (it->what == IT_COMPOSITION)
28503 /* A dynamic (automatic) composition. */
28504 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28505 Lisp_Object gstring;
28506 struct font_metrics metrics;
28508 it->nglyphs = 1;
28510 gstring = composition_gstring_from_id (it->cmp_it.id);
28511 it->pixel_width
28512 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28513 &metrics);
28514 if (it->glyph_row
28515 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28516 it->glyph_row->contains_overlapping_glyphs_p = true;
28517 it->ascent = it->phys_ascent = metrics.ascent;
28518 it->descent = it->phys_descent = metrics.descent;
28519 if (face->box != FACE_NO_BOX)
28521 int thick = face->box_line_width;
28523 if (thick > 0)
28525 it->ascent += thick;
28526 it->descent += thick;
28528 else
28529 thick = - thick;
28531 if (it->start_of_box_run_p)
28532 it->pixel_width += thick;
28533 if (it->end_of_box_run_p)
28534 it->pixel_width += thick;
28536 /* If face has an overline, add the height of the overline
28537 (1 pixel) and a 1 pixel margin to the character height. */
28538 if (face->overline_p)
28539 it->ascent += overline_margin;
28540 take_vertical_position_into_account (it);
28541 if (it->ascent < 0)
28542 it->ascent = 0;
28543 if (it->descent < 0)
28544 it->descent = 0;
28546 if (it->glyph_row)
28547 append_composite_glyph (it);
28549 else if (it->what == IT_GLYPHLESS)
28550 produce_glyphless_glyph (it, false, Qnil);
28551 else if (it->what == IT_IMAGE)
28552 produce_image_glyph (it);
28553 else if (it->what == IT_STRETCH)
28554 produce_stretch_glyph (it);
28555 else if (it->what == IT_XWIDGET)
28556 produce_xwidget_glyph (it);
28558 done:
28559 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28560 because this isn't true for images with `:ascent 100'. */
28561 eassert (it->ascent >= 0 && it->descent >= 0);
28562 if (it->area == TEXT_AREA)
28563 it->current_x += it->pixel_width;
28565 if (extra_line_spacing > 0)
28567 it->descent += extra_line_spacing;
28568 if (extra_line_spacing > it->max_extra_line_spacing)
28569 it->max_extra_line_spacing = extra_line_spacing;
28572 it->max_ascent = max (it->max_ascent, it->ascent);
28573 it->max_descent = max (it->max_descent, it->descent);
28574 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28575 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28578 /* EXPORT for RIF:
28579 Output LEN glyphs starting at START at the nominal cursor position.
28580 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28581 being updated, and UPDATED_AREA is the area of that row being updated. */
28583 void
28584 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28585 struct glyph *start, enum glyph_row_area updated_area, int len)
28587 int x, hpos, chpos = w->phys_cursor.hpos;
28589 eassert (updated_row);
28590 /* When the window is hscrolled, cursor hpos can legitimately be out
28591 of bounds, but we draw the cursor at the corresponding window
28592 margin in that case. */
28593 if (!updated_row->reversed_p && chpos < 0)
28594 chpos = 0;
28595 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28596 chpos = updated_row->used[TEXT_AREA] - 1;
28598 block_input ();
28600 /* Write glyphs. */
28602 hpos = start - updated_row->glyphs[updated_area];
28603 x = draw_glyphs (w, w->output_cursor.x,
28604 updated_row, updated_area,
28605 hpos, hpos + len,
28606 DRAW_NORMAL_TEXT, 0);
28608 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28609 if (updated_area == TEXT_AREA
28610 && w->phys_cursor_on_p
28611 && w->phys_cursor.vpos == w->output_cursor.vpos
28612 && chpos >= hpos
28613 && chpos < hpos + len)
28614 w->phys_cursor_on_p = false;
28616 unblock_input ();
28618 /* Advance the output cursor. */
28619 w->output_cursor.hpos += len;
28620 w->output_cursor.x = x;
28624 /* EXPORT for RIF:
28625 Insert LEN glyphs from START at the nominal cursor position. */
28627 void
28628 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28629 struct glyph *start, enum glyph_row_area updated_area, int len)
28631 struct frame *f;
28632 int line_height, shift_by_width, shifted_region_width;
28633 struct glyph_row *row;
28634 struct glyph *glyph;
28635 int frame_x, frame_y;
28636 ptrdiff_t hpos;
28638 eassert (updated_row);
28639 block_input ();
28640 f = XFRAME (WINDOW_FRAME (w));
28642 /* Get the height of the line we are in. */
28643 row = updated_row;
28644 line_height = row->height;
28646 /* Get the width of the glyphs to insert. */
28647 shift_by_width = 0;
28648 for (glyph = start; glyph < start + len; ++glyph)
28649 shift_by_width += glyph->pixel_width;
28651 /* Get the width of the region to shift right. */
28652 shifted_region_width = (window_box_width (w, updated_area)
28653 - w->output_cursor.x
28654 - shift_by_width);
28656 /* Shift right. */
28657 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28658 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28660 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28661 line_height, shift_by_width);
28663 /* Write the glyphs. */
28664 hpos = start - row->glyphs[updated_area];
28665 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28666 hpos, hpos + len,
28667 DRAW_NORMAL_TEXT, 0);
28669 /* Advance the output cursor. */
28670 w->output_cursor.hpos += len;
28671 w->output_cursor.x += shift_by_width;
28672 unblock_input ();
28676 /* EXPORT for RIF:
28677 Erase the current text line from the nominal cursor position
28678 (inclusive) to pixel column TO_X (exclusive). The idea is that
28679 everything from TO_X onward is already erased.
28681 TO_X is a pixel position relative to UPDATED_AREA of currently
28682 updated window W. TO_X == -1 means clear to the end of this area. */
28684 void
28685 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28686 enum glyph_row_area updated_area, int to_x)
28688 struct frame *f;
28689 int max_x, min_y, max_y;
28690 int from_x, from_y, to_y;
28692 eassert (updated_row);
28693 f = XFRAME (w->frame);
28695 if (updated_row->full_width_p)
28696 max_x = (WINDOW_PIXEL_WIDTH (w)
28697 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28698 else
28699 max_x = window_box_width (w, updated_area);
28700 max_y = window_text_bottom_y (w);
28702 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28703 of window. For TO_X > 0, truncate to end of drawing area. */
28704 if (to_x == 0)
28705 return;
28706 else if (to_x < 0)
28707 to_x = max_x;
28708 else
28709 to_x = min (to_x, max_x);
28711 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28713 /* Notice if the cursor will be cleared by this operation. */
28714 if (!updated_row->full_width_p)
28715 notice_overwritten_cursor (w, updated_area,
28716 w->output_cursor.x, -1,
28717 updated_row->y,
28718 MATRIX_ROW_BOTTOM_Y (updated_row));
28720 from_x = w->output_cursor.x;
28722 /* Translate to frame coordinates. */
28723 if (updated_row->full_width_p)
28725 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28726 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28728 else
28730 int area_left = window_box_left (w, updated_area);
28731 from_x += area_left;
28732 to_x += area_left;
28735 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28736 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28737 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28739 /* Prevent inadvertently clearing to end of the X window. */
28740 if (to_x > from_x && to_y > from_y)
28742 block_input ();
28743 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28744 to_x - from_x, to_y - from_y);
28745 unblock_input ();
28749 #endif /* HAVE_WINDOW_SYSTEM */
28753 /***********************************************************************
28754 Cursor types
28755 ***********************************************************************/
28757 /* Value is the internal representation of the specified cursor type
28758 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28759 of the bar cursor. */
28761 static enum text_cursor_kinds
28762 get_specified_cursor_type (Lisp_Object arg, int *width)
28764 enum text_cursor_kinds type;
28766 if (NILP (arg))
28767 return NO_CURSOR;
28769 if (EQ (arg, Qbox))
28770 return FILLED_BOX_CURSOR;
28772 if (EQ (arg, Qhollow))
28773 return HOLLOW_BOX_CURSOR;
28775 if (EQ (arg, Qbar))
28777 *width = 2;
28778 return BAR_CURSOR;
28781 if (CONSP (arg)
28782 && EQ (XCAR (arg), Qbar)
28783 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28785 *width = XINT (XCDR (arg));
28786 return BAR_CURSOR;
28789 if (EQ (arg, Qhbar))
28791 *width = 2;
28792 return HBAR_CURSOR;
28795 if (CONSP (arg)
28796 && EQ (XCAR (arg), Qhbar)
28797 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28799 *width = XINT (XCDR (arg));
28800 return HBAR_CURSOR;
28803 /* Treat anything unknown as "hollow box cursor".
28804 It was bad to signal an error; people have trouble fixing
28805 .Xdefaults with Emacs, when it has something bad in it. */
28806 type = HOLLOW_BOX_CURSOR;
28808 return type;
28811 /* Set the default cursor types for specified frame. */
28812 void
28813 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28815 int width = 1;
28816 Lisp_Object tem;
28818 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28819 FRAME_CURSOR_WIDTH (f) = width;
28821 /* By default, set up the blink-off state depending on the on-state. */
28823 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28824 if (!NILP (tem))
28826 FRAME_BLINK_OFF_CURSOR (f)
28827 = get_specified_cursor_type (XCDR (tem), &width);
28828 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28830 else
28831 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28833 /* Make sure the cursor gets redrawn. */
28834 f->cursor_type_changed = true;
28838 #ifdef HAVE_WINDOW_SYSTEM
28840 /* Return the cursor we want to be displayed in window W. Return
28841 width of bar/hbar cursor through WIDTH arg. Return with
28842 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28843 (i.e. if the `system caret' should track this cursor).
28845 In a mini-buffer window, we want the cursor only to appear if we
28846 are reading input from this window. For the selected window, we
28847 want the cursor type given by the frame parameter or buffer local
28848 setting of cursor-type. If explicitly marked off, draw no cursor.
28849 In all other cases, we want a hollow box cursor. */
28851 static enum text_cursor_kinds
28852 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28853 bool *active_cursor)
28855 struct frame *f = XFRAME (w->frame);
28856 struct buffer *b = XBUFFER (w->contents);
28857 int cursor_type = DEFAULT_CURSOR;
28858 Lisp_Object alt_cursor;
28859 bool non_selected = false;
28861 *active_cursor = true;
28863 /* Echo area */
28864 if (cursor_in_echo_area
28865 && FRAME_HAS_MINIBUF_P (f)
28866 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28868 if (w == XWINDOW (echo_area_window))
28870 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28872 *width = FRAME_CURSOR_WIDTH (f);
28873 return FRAME_DESIRED_CURSOR (f);
28875 else
28876 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28879 *active_cursor = false;
28880 non_selected = true;
28883 /* Detect a nonselected window or nonselected frame. */
28884 else if (w != XWINDOW (f->selected_window)
28885 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28887 *active_cursor = false;
28889 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28890 return NO_CURSOR;
28892 non_selected = true;
28895 /* Never display a cursor in a window in which cursor-type is nil. */
28896 if (NILP (BVAR (b, cursor_type)))
28897 return NO_CURSOR;
28899 /* Get the normal cursor type for this window. */
28900 if (EQ (BVAR (b, cursor_type), Qt))
28902 cursor_type = FRAME_DESIRED_CURSOR (f);
28903 *width = FRAME_CURSOR_WIDTH (f);
28905 else
28906 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28908 /* Use cursor-in-non-selected-windows instead
28909 for non-selected window or frame. */
28910 if (non_selected)
28912 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28913 if (!EQ (Qt, alt_cursor))
28914 return get_specified_cursor_type (alt_cursor, width);
28915 /* t means modify the normal cursor type. */
28916 if (cursor_type == FILLED_BOX_CURSOR)
28917 cursor_type = HOLLOW_BOX_CURSOR;
28918 else if (cursor_type == BAR_CURSOR && *width > 1)
28919 --*width;
28920 return cursor_type;
28923 /* Use normal cursor if not blinked off. */
28924 if (!w->cursor_off_p)
28926 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28927 return NO_CURSOR;
28928 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28930 if (cursor_type == FILLED_BOX_CURSOR)
28932 /* Using a block cursor on large images can be very annoying.
28933 So use a hollow cursor for "large" images.
28934 If image is not transparent (no mask), also use hollow cursor. */
28935 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28936 if (img != NULL && IMAGEP (img->spec))
28938 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28939 where N = size of default frame font size.
28940 This should cover most of the "tiny" icons people may use. */
28941 if (!img->mask
28942 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28943 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28944 cursor_type = HOLLOW_BOX_CURSOR;
28947 else if (cursor_type != NO_CURSOR)
28949 /* Display current only supports BOX and HOLLOW cursors for images.
28950 So for now, unconditionally use a HOLLOW cursor when cursor is
28951 not a solid box cursor. */
28952 cursor_type = HOLLOW_BOX_CURSOR;
28955 return cursor_type;
28958 /* Cursor is blinked off, so determine how to "toggle" it. */
28960 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28961 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
28962 return get_specified_cursor_type (XCDR (alt_cursor), width);
28964 /* Then see if frame has specified a specific blink off cursor type. */
28965 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28967 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28968 return FRAME_BLINK_OFF_CURSOR (f);
28971 #if false
28972 /* Some people liked having a permanently visible blinking cursor,
28973 while others had very strong opinions against it. So it was
28974 decided to remove it. KFS 2003-09-03 */
28976 /* Finally perform built-in cursor blinking:
28977 filled box <-> hollow box
28978 wide [h]bar <-> narrow [h]bar
28979 narrow [h]bar <-> no cursor
28980 other type <-> no cursor */
28982 if (cursor_type == FILLED_BOX_CURSOR)
28983 return HOLLOW_BOX_CURSOR;
28985 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28987 *width = 1;
28988 return cursor_type;
28990 #endif
28992 return NO_CURSOR;
28996 /* Notice when the text cursor of window W has been completely
28997 overwritten by a drawing operation that outputs glyphs in AREA
28998 starting at X0 and ending at X1 in the line starting at Y0 and
28999 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29000 the rest of the line after X0 has been written. Y coordinates
29001 are window-relative. */
29003 static void
29004 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29005 int x0, int x1, int y0, int y1)
29007 int cx0, cx1, cy0, cy1;
29008 struct glyph_row *row;
29010 if (!w->phys_cursor_on_p)
29011 return;
29012 if (area != TEXT_AREA)
29013 return;
29015 if (w->phys_cursor.vpos < 0
29016 || w->phys_cursor.vpos >= w->current_matrix->nrows
29017 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29018 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29019 return;
29021 if (row->cursor_in_fringe_p)
29023 row->cursor_in_fringe_p = false;
29024 draw_fringe_bitmap (w, row, row->reversed_p);
29025 w->phys_cursor_on_p = false;
29026 return;
29029 cx0 = w->phys_cursor.x;
29030 cx1 = cx0 + w->phys_cursor_width;
29031 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29032 return;
29034 /* The cursor image will be completely removed from the
29035 screen if the output area intersects the cursor area in
29036 y-direction. When we draw in [y0 y1[, and some part of
29037 the cursor is at y < y0, that part must have been drawn
29038 before. When scrolling, the cursor is erased before
29039 actually scrolling, so we don't come here. When not
29040 scrolling, the rows above the old cursor row must have
29041 changed, and in this case these rows must have written
29042 over the cursor image.
29044 Likewise if part of the cursor is below y1, with the
29045 exception of the cursor being in the first blank row at
29046 the buffer and window end because update_text_area
29047 doesn't draw that row. (Except when it does, but
29048 that's handled in update_text_area.) */
29050 cy0 = w->phys_cursor.y;
29051 cy1 = cy0 + w->phys_cursor_height;
29052 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29053 return;
29055 w->phys_cursor_on_p = false;
29058 #endif /* HAVE_WINDOW_SYSTEM */
29061 /************************************************************************
29062 Mouse Face
29063 ************************************************************************/
29065 #ifdef HAVE_WINDOW_SYSTEM
29067 /* EXPORT for RIF:
29068 Fix the display of area AREA of overlapping row ROW in window W
29069 with respect to the overlapping part OVERLAPS. */
29071 void
29072 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29073 enum glyph_row_area area, int overlaps)
29075 int i, x;
29077 block_input ();
29079 x = 0;
29080 for (i = 0; i < row->used[area];)
29082 if (row->glyphs[area][i].overlaps_vertically_p)
29084 int start = i, start_x = x;
29088 x += row->glyphs[area][i].pixel_width;
29089 ++i;
29091 while (i < row->used[area]
29092 && row->glyphs[area][i].overlaps_vertically_p);
29094 draw_glyphs (w, start_x, row, area,
29095 start, i,
29096 DRAW_NORMAL_TEXT, overlaps);
29098 else
29100 x += row->glyphs[area][i].pixel_width;
29101 ++i;
29105 unblock_input ();
29109 /* EXPORT:
29110 Draw the cursor glyph of window W in glyph row ROW. See the
29111 comment of draw_glyphs for the meaning of HL. */
29113 void
29114 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29115 enum draw_glyphs_face hl)
29117 /* If cursor hpos is out of bounds, don't draw garbage. This can
29118 happen in mini-buffer windows when switching between echo area
29119 glyphs and mini-buffer. */
29120 if ((row->reversed_p
29121 ? (w->phys_cursor.hpos >= 0)
29122 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29124 bool on_p = w->phys_cursor_on_p;
29125 int x1;
29126 int hpos = w->phys_cursor.hpos;
29128 /* When the window is hscrolled, cursor hpos can legitimately be
29129 out of bounds, but we draw the cursor at the corresponding
29130 window margin in that case. */
29131 if (!row->reversed_p && hpos < 0)
29132 hpos = 0;
29133 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29134 hpos = row->used[TEXT_AREA] - 1;
29136 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29137 hl, 0);
29138 w->phys_cursor_on_p = on_p;
29140 if (hl == DRAW_CURSOR)
29141 w->phys_cursor_width = x1 - w->phys_cursor.x;
29142 /* When we erase the cursor, and ROW is overlapped by other
29143 rows, make sure that these overlapping parts of other rows
29144 are redrawn. */
29145 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29147 w->phys_cursor_width = x1 - w->phys_cursor.x;
29149 if (row > w->current_matrix->rows
29150 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29151 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29152 OVERLAPS_ERASED_CURSOR);
29154 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29155 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29156 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29157 OVERLAPS_ERASED_CURSOR);
29163 /* Erase the image of a cursor of window W from the screen. */
29165 void
29166 erase_phys_cursor (struct window *w)
29168 struct frame *f = XFRAME (w->frame);
29169 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29170 int hpos = w->phys_cursor.hpos;
29171 int vpos = w->phys_cursor.vpos;
29172 bool mouse_face_here_p = false;
29173 struct glyph_matrix *active_glyphs = w->current_matrix;
29174 struct glyph_row *cursor_row;
29175 struct glyph *cursor_glyph;
29176 enum draw_glyphs_face hl;
29178 /* No cursor displayed or row invalidated => nothing to do on the
29179 screen. */
29180 if (w->phys_cursor_type == NO_CURSOR)
29181 goto mark_cursor_off;
29183 /* VPOS >= active_glyphs->nrows means that window has been resized.
29184 Don't bother to erase the cursor. */
29185 if (vpos >= active_glyphs->nrows)
29186 goto mark_cursor_off;
29188 /* If row containing cursor is marked invalid, there is nothing we
29189 can do. */
29190 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29191 if (!cursor_row->enabled_p)
29192 goto mark_cursor_off;
29194 /* If line spacing is > 0, old cursor may only be partially visible in
29195 window after split-window. So adjust visible height. */
29196 cursor_row->visible_height = min (cursor_row->visible_height,
29197 window_text_bottom_y (w) - cursor_row->y);
29199 /* If row is completely invisible, don't attempt to delete a cursor which
29200 isn't there. This can happen if cursor is at top of a window, and
29201 we switch to a buffer with a header line in that window. */
29202 if (cursor_row->visible_height <= 0)
29203 goto mark_cursor_off;
29205 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29206 if (cursor_row->cursor_in_fringe_p)
29208 cursor_row->cursor_in_fringe_p = false;
29209 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29210 goto mark_cursor_off;
29213 /* This can happen when the new row is shorter than the old one.
29214 In this case, either draw_glyphs or clear_end_of_line
29215 should have cleared the cursor. Note that we wouldn't be
29216 able to erase the cursor in this case because we don't have a
29217 cursor glyph at hand. */
29218 if ((cursor_row->reversed_p
29219 ? (w->phys_cursor.hpos < 0)
29220 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29221 goto mark_cursor_off;
29223 /* When the window is hscrolled, cursor hpos can legitimately be out
29224 of bounds, but we draw the cursor at the corresponding window
29225 margin in that case. */
29226 if (!cursor_row->reversed_p && hpos < 0)
29227 hpos = 0;
29228 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29229 hpos = cursor_row->used[TEXT_AREA] - 1;
29231 /* If the cursor is in the mouse face area, redisplay that when
29232 we clear the cursor. */
29233 if (! NILP (hlinfo->mouse_face_window)
29234 && coords_in_mouse_face_p (w, hpos, vpos)
29235 /* Don't redraw the cursor's spot in mouse face if it is at the
29236 end of a line (on a newline). The cursor appears there, but
29237 mouse highlighting does not. */
29238 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29239 mouse_face_here_p = true;
29241 /* Maybe clear the display under the cursor. */
29242 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29244 int x, y;
29245 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29246 int width;
29248 cursor_glyph = get_phys_cursor_glyph (w);
29249 if (cursor_glyph == NULL)
29250 goto mark_cursor_off;
29252 width = cursor_glyph->pixel_width;
29253 x = w->phys_cursor.x;
29254 if (x < 0)
29256 width += x;
29257 x = 0;
29259 width = min (width, window_box_width (w, TEXT_AREA) - x);
29260 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29261 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29263 if (width > 0)
29264 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29267 /* Erase the cursor by redrawing the character underneath it. */
29268 if (mouse_face_here_p)
29269 hl = DRAW_MOUSE_FACE;
29270 else
29271 hl = DRAW_NORMAL_TEXT;
29272 draw_phys_cursor_glyph (w, cursor_row, hl);
29274 mark_cursor_off:
29275 w->phys_cursor_on_p = false;
29276 w->phys_cursor_type = NO_CURSOR;
29280 /* Display or clear cursor of window W. If !ON, clear the cursor.
29281 If ON, display the cursor; where to put the cursor is specified by
29282 HPOS, VPOS, X and Y. */
29284 void
29285 display_and_set_cursor (struct window *w, bool on,
29286 int hpos, int vpos, int x, int y)
29288 struct frame *f = XFRAME (w->frame);
29289 int new_cursor_type;
29290 int new_cursor_width;
29291 bool active_cursor;
29292 struct glyph_row *glyph_row;
29293 struct glyph *glyph;
29295 /* This is pointless on invisible frames, and dangerous on garbaged
29296 windows and frames; in the latter case, the frame or window may
29297 be in the midst of changing its size, and x and y may be off the
29298 window. */
29299 if (! FRAME_VISIBLE_P (f)
29300 || vpos >= w->current_matrix->nrows
29301 || hpos >= w->current_matrix->matrix_w)
29302 return;
29304 /* If cursor is off and we want it off, return quickly. */
29305 if (!on && !w->phys_cursor_on_p)
29306 return;
29308 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29309 /* If cursor row is not enabled, we don't really know where to
29310 display the cursor. */
29311 if (!glyph_row->enabled_p)
29313 w->phys_cursor_on_p = false;
29314 return;
29317 /* A frame might be marked garbaged even though its cursor position
29318 is correct, and will not change upon subsequent redisplay. This
29319 happens in some rare situations, like toggling the sort order in
29320 Dired windows. We've already established that VPOS is valid, so
29321 it shouldn't do any harm to record the cursor position, as we are
29322 going to return without acting on it anyway. Otherwise, expose
29323 events might come in and call update_window_cursor, which will
29324 blindly use outdated values in w->phys_cursor. */
29325 if (FRAME_GARBAGED_P (f))
29327 if (on)
29329 w->phys_cursor.x = x;
29330 w->phys_cursor.y = glyph_row->y;
29331 w->phys_cursor.hpos = hpos;
29332 w->phys_cursor.vpos = vpos;
29334 return;
29337 glyph = NULL;
29338 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29339 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29341 eassert (input_blocked_p ());
29343 /* Set new_cursor_type to the cursor we want to be displayed. */
29344 new_cursor_type = get_window_cursor_type (w, glyph,
29345 &new_cursor_width, &active_cursor);
29347 /* If cursor is currently being shown and we don't want it to be or
29348 it is in the wrong place, or the cursor type is not what we want,
29349 erase it. */
29350 if (w->phys_cursor_on_p
29351 && (!on
29352 || w->phys_cursor.x != x
29353 || w->phys_cursor.y != y
29354 /* HPOS can be negative in R2L rows whose
29355 exact_window_width_line_p flag is set (i.e. their newline
29356 would "overflow into the fringe"). */
29357 || hpos < 0
29358 || new_cursor_type != w->phys_cursor_type
29359 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29360 && new_cursor_width != w->phys_cursor_width)))
29361 erase_phys_cursor (w);
29363 /* Don't check phys_cursor_on_p here because that flag is only set
29364 to false in some cases where we know that the cursor has been
29365 completely erased, to avoid the extra work of erasing the cursor
29366 twice. In other words, phys_cursor_on_p can be true and the cursor
29367 still not be visible, or it has only been partly erased. */
29368 if (on)
29370 w->phys_cursor_ascent = glyph_row->ascent;
29371 w->phys_cursor_height = glyph_row->height;
29373 /* Set phys_cursor_.* before x_draw_.* is called because some
29374 of them may need the information. */
29375 w->phys_cursor.x = x;
29376 w->phys_cursor.y = glyph_row->y;
29377 w->phys_cursor.hpos = hpos;
29378 w->phys_cursor.vpos = vpos;
29381 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29382 new_cursor_type, new_cursor_width,
29383 on, active_cursor);
29387 /* Switch the display of W's cursor on or off, according to the value
29388 of ON. */
29390 static void
29391 update_window_cursor (struct window *w, bool on)
29393 /* Don't update cursor in windows whose frame is in the process
29394 of being deleted. */
29395 if (w->current_matrix)
29397 int hpos = w->phys_cursor.hpos;
29398 int vpos = w->phys_cursor.vpos;
29399 struct glyph_row *row;
29401 if (vpos >= w->current_matrix->nrows
29402 || hpos >= w->current_matrix->matrix_w)
29403 return;
29405 row = MATRIX_ROW (w->current_matrix, vpos);
29407 /* When the window is hscrolled, cursor hpos can legitimately be
29408 out of bounds, but we draw the cursor at the corresponding
29409 window margin in that case. */
29410 if (!row->reversed_p && hpos < 0)
29411 hpos = 0;
29412 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29413 hpos = row->used[TEXT_AREA] - 1;
29415 block_input ();
29416 display_and_set_cursor (w, on, hpos, vpos,
29417 w->phys_cursor.x, w->phys_cursor.y);
29418 unblock_input ();
29423 /* Call update_window_cursor with parameter ON_P on all leaf windows
29424 in the window tree rooted at W. */
29426 static void
29427 update_cursor_in_window_tree (struct window *w, bool on_p)
29429 while (w)
29431 if (WINDOWP (w->contents))
29432 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29433 else
29434 update_window_cursor (w, on_p);
29436 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29441 /* EXPORT:
29442 Display the cursor on window W, or clear it, according to ON_P.
29443 Don't change the cursor's position. */
29445 void
29446 x_update_cursor (struct frame *f, bool on_p)
29448 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29452 /* EXPORT:
29453 Clear the cursor of window W to background color, and mark the
29454 cursor as not shown. This is used when the text where the cursor
29455 is about to be rewritten. */
29457 void
29458 x_clear_cursor (struct window *w)
29460 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29461 update_window_cursor (w, false);
29464 #endif /* HAVE_WINDOW_SYSTEM */
29466 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29467 and MSDOS. */
29468 static void
29469 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29470 int start_hpos, int end_hpos,
29471 enum draw_glyphs_face draw)
29473 #ifdef HAVE_WINDOW_SYSTEM
29474 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29476 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29477 return;
29479 #endif
29480 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29481 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29482 #endif
29485 /* Display the active region described by mouse_face_* according to DRAW. */
29487 static void
29488 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29490 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29491 struct frame *f = XFRAME (WINDOW_FRAME (w));
29493 if (/* If window is in the process of being destroyed, don't bother
29494 to do anything. */
29495 w->current_matrix != NULL
29496 /* Don't update mouse highlight if hidden. */
29497 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29498 /* Recognize when we are called to operate on rows that don't exist
29499 anymore. This can happen when a window is split. */
29500 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29502 bool phys_cursor_on_p = w->phys_cursor_on_p;
29503 struct glyph_row *row, *first, *last;
29505 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29506 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29508 for (row = first; row <= last && row->enabled_p; ++row)
29510 int start_hpos, end_hpos, start_x;
29512 /* For all but the first row, the highlight starts at column 0. */
29513 if (row == first)
29515 /* R2L rows have BEG and END in reversed order, but the
29516 screen drawing geometry is always left to right. So
29517 we need to mirror the beginning and end of the
29518 highlighted area in R2L rows. */
29519 if (!row->reversed_p)
29521 start_hpos = hlinfo->mouse_face_beg_col;
29522 start_x = hlinfo->mouse_face_beg_x;
29524 else if (row == last)
29526 start_hpos = hlinfo->mouse_face_end_col;
29527 start_x = hlinfo->mouse_face_end_x;
29529 else
29531 start_hpos = 0;
29532 start_x = 0;
29535 else if (row->reversed_p && row == last)
29537 start_hpos = hlinfo->mouse_face_end_col;
29538 start_x = hlinfo->mouse_face_end_x;
29540 else
29542 start_hpos = 0;
29543 start_x = 0;
29546 if (row == last)
29548 if (!row->reversed_p)
29549 end_hpos = hlinfo->mouse_face_end_col;
29550 else if (row == first)
29551 end_hpos = hlinfo->mouse_face_beg_col;
29552 else
29554 end_hpos = row->used[TEXT_AREA];
29555 if (draw == DRAW_NORMAL_TEXT)
29556 row->fill_line_p = true; /* Clear to end of line. */
29559 else if (row->reversed_p && row == first)
29560 end_hpos = hlinfo->mouse_face_beg_col;
29561 else
29563 end_hpos = row->used[TEXT_AREA];
29564 if (draw == DRAW_NORMAL_TEXT)
29565 row->fill_line_p = true; /* Clear to end of line. */
29568 if (end_hpos > start_hpos)
29570 draw_row_with_mouse_face (w, start_x, row,
29571 start_hpos, end_hpos, draw);
29573 row->mouse_face_p
29574 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29578 /* When we've written over the cursor, arrange for it to
29579 be displayed again. */
29580 if (FRAME_WINDOW_P (f)
29581 && phys_cursor_on_p && !w->phys_cursor_on_p)
29583 #ifdef HAVE_WINDOW_SYSTEM
29584 int hpos = w->phys_cursor.hpos;
29586 /* When the window is hscrolled, cursor hpos can legitimately be
29587 out of bounds, but we draw the cursor at the corresponding
29588 window margin in that case. */
29589 if (!row->reversed_p && hpos < 0)
29590 hpos = 0;
29591 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29592 hpos = row->used[TEXT_AREA] - 1;
29594 block_input ();
29595 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29596 w->phys_cursor.x, w->phys_cursor.y);
29597 unblock_input ();
29598 #endif /* HAVE_WINDOW_SYSTEM */
29602 #ifdef HAVE_WINDOW_SYSTEM
29603 /* Change the mouse cursor. */
29604 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29606 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29607 if (draw == DRAW_NORMAL_TEXT
29608 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29609 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29610 else
29611 #endif
29612 if (draw == DRAW_MOUSE_FACE)
29613 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29614 else
29615 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29617 #endif /* HAVE_WINDOW_SYSTEM */
29620 /* EXPORT:
29621 Clear out the mouse-highlighted active region.
29622 Redraw it un-highlighted first. Value is true if mouse
29623 face was actually drawn unhighlighted. */
29625 bool
29626 clear_mouse_face (Mouse_HLInfo *hlinfo)
29628 bool cleared
29629 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29630 if (cleared)
29631 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29632 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29633 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29634 hlinfo->mouse_face_window = Qnil;
29635 hlinfo->mouse_face_overlay = Qnil;
29636 return cleared;
29639 /* Return true if the coordinates HPOS and VPOS on windows W are
29640 within the mouse face on that window. */
29641 static bool
29642 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29644 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29646 /* Quickly resolve the easy cases. */
29647 if (!(WINDOWP (hlinfo->mouse_face_window)
29648 && XWINDOW (hlinfo->mouse_face_window) == w))
29649 return false;
29650 if (vpos < hlinfo->mouse_face_beg_row
29651 || vpos > hlinfo->mouse_face_end_row)
29652 return false;
29653 if (vpos > hlinfo->mouse_face_beg_row
29654 && vpos < hlinfo->mouse_face_end_row)
29655 return true;
29657 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29659 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29661 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29662 return true;
29664 else if ((vpos == hlinfo->mouse_face_beg_row
29665 && hpos >= hlinfo->mouse_face_beg_col)
29666 || (vpos == hlinfo->mouse_face_end_row
29667 && hpos < hlinfo->mouse_face_end_col))
29668 return true;
29670 else
29672 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29674 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29675 return true;
29677 else if ((vpos == hlinfo->mouse_face_beg_row
29678 && hpos <= hlinfo->mouse_face_beg_col)
29679 || (vpos == hlinfo->mouse_face_end_row
29680 && hpos > hlinfo->mouse_face_end_col))
29681 return true;
29683 return false;
29687 /* EXPORT:
29688 True if physical cursor of window W is within mouse face. */
29690 bool
29691 cursor_in_mouse_face_p (struct window *w)
29693 int hpos = w->phys_cursor.hpos;
29694 int vpos = w->phys_cursor.vpos;
29695 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29697 /* When the window is hscrolled, cursor hpos can legitimately be out
29698 of bounds, but we draw the cursor at the corresponding window
29699 margin in that case. */
29700 if (!row->reversed_p && hpos < 0)
29701 hpos = 0;
29702 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29703 hpos = row->used[TEXT_AREA] - 1;
29705 return coords_in_mouse_face_p (w, hpos, vpos);
29710 /* Find the glyph rows START_ROW and END_ROW of window W that display
29711 characters between buffer positions START_CHARPOS and END_CHARPOS
29712 (excluding END_CHARPOS). DISP_STRING is a display string that
29713 covers these buffer positions. This is similar to
29714 row_containing_pos, but is more accurate when bidi reordering makes
29715 buffer positions change non-linearly with glyph rows. */
29716 static void
29717 rows_from_pos_range (struct window *w,
29718 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29719 Lisp_Object disp_string,
29720 struct glyph_row **start, struct glyph_row **end)
29722 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29723 int last_y = window_text_bottom_y (w);
29724 struct glyph_row *row;
29726 *start = NULL;
29727 *end = NULL;
29729 while (!first->enabled_p
29730 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29731 first++;
29733 /* Find the START row. */
29734 for (row = first;
29735 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29736 row++)
29738 /* A row can potentially be the START row if the range of the
29739 characters it displays intersects the range
29740 [START_CHARPOS..END_CHARPOS). */
29741 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29742 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29743 /* See the commentary in row_containing_pos, for the
29744 explanation of the complicated way to check whether
29745 some position is beyond the end of the characters
29746 displayed by a row. */
29747 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29748 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29749 && !row->ends_at_zv_p
29750 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29751 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29752 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29753 && !row->ends_at_zv_p
29754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29756 /* Found a candidate row. Now make sure at least one of the
29757 glyphs it displays has a charpos from the range
29758 [START_CHARPOS..END_CHARPOS).
29760 This is not obvious because bidi reordering could make
29761 buffer positions of a row be 1,2,3,102,101,100, and if we
29762 want to highlight characters in [50..60), we don't want
29763 this row, even though [50..60) does intersect [1..103),
29764 the range of character positions given by the row's start
29765 and end positions. */
29766 struct glyph *g = row->glyphs[TEXT_AREA];
29767 struct glyph *e = g + row->used[TEXT_AREA];
29769 while (g < e)
29771 if (((BUFFERP (g->object) || NILP (g->object))
29772 && start_charpos <= g->charpos && g->charpos < end_charpos)
29773 /* A glyph that comes from DISP_STRING is by
29774 definition to be highlighted. */
29775 || EQ (g->object, disp_string))
29776 *start = row;
29777 g++;
29779 if (*start)
29780 break;
29784 /* Find the END row. */
29785 if (!*start
29786 /* If the last row is partially visible, start looking for END
29787 from that row, instead of starting from FIRST. */
29788 && !(row->enabled_p
29789 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29790 row = first;
29791 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29793 struct glyph_row *next = row + 1;
29794 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29796 if (!next->enabled_p
29797 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29798 /* The first row >= START whose range of displayed characters
29799 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29800 is the row END + 1. */
29801 || (start_charpos < next_start
29802 && end_charpos < next_start)
29803 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29804 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29805 && !next->ends_at_zv_p
29806 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29807 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29808 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29809 && !next->ends_at_zv_p
29810 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29812 *end = row;
29813 break;
29815 else
29817 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29818 but none of the characters it displays are in the range, it is
29819 also END + 1. */
29820 struct glyph *g = next->glyphs[TEXT_AREA];
29821 struct glyph *s = g;
29822 struct glyph *e = g + next->used[TEXT_AREA];
29824 while (g < e)
29826 if (((BUFFERP (g->object) || NILP (g->object))
29827 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29828 /* If the buffer position of the first glyph in
29829 the row is equal to END_CHARPOS, it means
29830 the last character to be highlighted is the
29831 newline of ROW, and we must consider NEXT as
29832 END, not END+1. */
29833 || (((!next->reversed_p && g == s)
29834 || (next->reversed_p && g == e - 1))
29835 && (g->charpos == end_charpos
29836 /* Special case for when NEXT is an
29837 empty line at ZV. */
29838 || (g->charpos == -1
29839 && !row->ends_at_zv_p
29840 && next_start == end_charpos)))))
29841 /* A glyph that comes from DISP_STRING is by
29842 definition to be highlighted. */
29843 || EQ (g->object, disp_string))
29844 break;
29845 g++;
29847 if (g == e)
29849 *end = row;
29850 break;
29852 /* The first row that ends at ZV must be the last to be
29853 highlighted. */
29854 else if (next->ends_at_zv_p)
29856 *end = next;
29857 break;
29863 /* This function sets the mouse_face_* elements of HLINFO, assuming
29864 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29865 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29866 for the overlay or run of text properties specifying the mouse
29867 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29868 before-string and after-string that must also be highlighted.
29869 DISP_STRING, if non-nil, is a display string that may cover some
29870 or all of the highlighted text. */
29872 static void
29873 mouse_face_from_buffer_pos (Lisp_Object window,
29874 Mouse_HLInfo *hlinfo,
29875 ptrdiff_t mouse_charpos,
29876 ptrdiff_t start_charpos,
29877 ptrdiff_t end_charpos,
29878 Lisp_Object before_string,
29879 Lisp_Object after_string,
29880 Lisp_Object disp_string)
29882 struct window *w = XWINDOW (window);
29883 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29884 struct glyph_row *r1, *r2;
29885 struct glyph *glyph, *end;
29886 ptrdiff_t ignore, pos;
29887 int x;
29889 eassert (NILP (disp_string) || STRINGP (disp_string));
29890 eassert (NILP (before_string) || STRINGP (before_string));
29891 eassert (NILP (after_string) || STRINGP (after_string));
29893 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29894 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29895 if (r1 == NULL)
29896 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29897 /* If the before-string or display-string contains newlines,
29898 rows_from_pos_range skips to its last row. Move back. */
29899 if (!NILP (before_string) || !NILP (disp_string))
29901 struct glyph_row *prev;
29902 while ((prev = r1 - 1, prev >= first)
29903 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29904 && prev->used[TEXT_AREA] > 0)
29906 struct glyph *beg = prev->glyphs[TEXT_AREA];
29907 glyph = beg + prev->used[TEXT_AREA];
29908 while (--glyph >= beg && NILP (glyph->object));
29909 if (glyph < beg
29910 || !(EQ (glyph->object, before_string)
29911 || EQ (glyph->object, disp_string)))
29912 break;
29913 r1 = prev;
29916 if (r2 == NULL)
29918 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29919 hlinfo->mouse_face_past_end = true;
29921 else if (!NILP (after_string))
29923 /* If the after-string has newlines, advance to its last row. */
29924 struct glyph_row *next;
29925 struct glyph_row *last
29926 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29928 for (next = r2 + 1;
29929 next <= last
29930 && next->used[TEXT_AREA] > 0
29931 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29932 ++next)
29933 r2 = next;
29935 /* The rest of the display engine assumes that mouse_face_beg_row is
29936 either above mouse_face_end_row or identical to it. But with
29937 bidi-reordered continued lines, the row for START_CHARPOS could
29938 be below the row for END_CHARPOS. If so, swap the rows and store
29939 them in correct order. */
29940 if (r1->y > r2->y)
29942 struct glyph_row *tem = r2;
29944 r2 = r1;
29945 r1 = tem;
29948 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29949 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29951 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29952 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29953 could be anywhere in the row and in any order. The strategy
29954 below is to find the leftmost and the rightmost glyph that
29955 belongs to either of these 3 strings, or whose position is
29956 between START_CHARPOS and END_CHARPOS, and highlight all the
29957 glyphs between those two. This may cover more than just the text
29958 between START_CHARPOS and END_CHARPOS if the range of characters
29959 strides the bidi level boundary, e.g. if the beginning is in R2L
29960 text while the end is in L2R text or vice versa. */
29961 if (!r1->reversed_p)
29963 /* This row is in a left to right paragraph. Scan it left to
29964 right. */
29965 glyph = r1->glyphs[TEXT_AREA];
29966 end = glyph + r1->used[TEXT_AREA];
29967 x = r1->x;
29969 /* Skip truncation glyphs at the start of the glyph row. */
29970 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29971 for (; glyph < end
29972 && NILP (glyph->object)
29973 && glyph->charpos < 0;
29974 ++glyph)
29975 x += glyph->pixel_width;
29977 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29978 or DISP_STRING, and the first glyph from buffer whose
29979 position is between START_CHARPOS and END_CHARPOS. */
29980 for (; glyph < end
29981 && !NILP (glyph->object)
29982 && !EQ (glyph->object, disp_string)
29983 && !(BUFFERP (glyph->object)
29984 && (glyph->charpos >= start_charpos
29985 && glyph->charpos < end_charpos));
29986 ++glyph)
29988 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29989 are present at buffer positions between START_CHARPOS and
29990 END_CHARPOS, or if they come from an overlay. */
29991 if (EQ (glyph->object, before_string))
29993 pos = string_buffer_position (before_string,
29994 start_charpos);
29995 /* If pos == 0, it means before_string came from an
29996 overlay, not from a buffer position. */
29997 if (!pos || (pos >= start_charpos && pos < end_charpos))
29998 break;
30000 else if (EQ (glyph->object, after_string))
30002 pos = string_buffer_position (after_string, end_charpos);
30003 if (!pos || (pos >= start_charpos && pos < end_charpos))
30004 break;
30006 x += glyph->pixel_width;
30008 hlinfo->mouse_face_beg_x = x;
30009 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30011 else
30013 /* This row is in a right to left paragraph. Scan it right to
30014 left. */
30015 struct glyph *g;
30017 end = r1->glyphs[TEXT_AREA] - 1;
30018 glyph = end + r1->used[TEXT_AREA];
30020 /* Skip truncation glyphs at the start of the glyph row. */
30021 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30022 for (; glyph > end
30023 && NILP (glyph->object)
30024 && glyph->charpos < 0;
30025 --glyph)
30028 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30029 or DISP_STRING, and the first glyph from buffer whose
30030 position is between START_CHARPOS and END_CHARPOS. */
30031 for (; glyph > end
30032 && !NILP (glyph->object)
30033 && !EQ (glyph->object, disp_string)
30034 && !(BUFFERP (glyph->object)
30035 && (glyph->charpos >= start_charpos
30036 && glyph->charpos < end_charpos));
30037 --glyph)
30039 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30040 are present at buffer positions between START_CHARPOS and
30041 END_CHARPOS, or if they come from an overlay. */
30042 if (EQ (glyph->object, before_string))
30044 pos = string_buffer_position (before_string, start_charpos);
30045 /* If pos == 0, it means before_string came from an
30046 overlay, not from a buffer position. */
30047 if (!pos || (pos >= start_charpos && pos < end_charpos))
30048 break;
30050 else if (EQ (glyph->object, after_string))
30052 pos = string_buffer_position (after_string, end_charpos);
30053 if (!pos || (pos >= start_charpos && pos < end_charpos))
30054 break;
30058 glyph++; /* first glyph to the right of the highlighted area */
30059 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30060 x += g->pixel_width;
30061 hlinfo->mouse_face_beg_x = x;
30062 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30065 /* If the highlight ends in a different row, compute GLYPH and END
30066 for the end row. Otherwise, reuse the values computed above for
30067 the row where the highlight begins. */
30068 if (r2 != r1)
30070 if (!r2->reversed_p)
30072 glyph = r2->glyphs[TEXT_AREA];
30073 end = glyph + r2->used[TEXT_AREA];
30074 x = r2->x;
30076 else
30078 end = r2->glyphs[TEXT_AREA] - 1;
30079 glyph = end + r2->used[TEXT_AREA];
30083 if (!r2->reversed_p)
30085 /* Skip truncation and continuation glyphs near the end of the
30086 row, and also blanks and stretch glyphs inserted by
30087 extend_face_to_end_of_line. */
30088 while (end > glyph
30089 && NILP ((end - 1)->object))
30090 --end;
30091 /* Scan the rest of the glyph row from the end, looking for the
30092 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30093 DISP_STRING, or whose position is between START_CHARPOS
30094 and END_CHARPOS */
30095 for (--end;
30096 end > glyph
30097 && !NILP (end->object)
30098 && !EQ (end->object, disp_string)
30099 && !(BUFFERP (end->object)
30100 && (end->charpos >= start_charpos
30101 && end->charpos < end_charpos));
30102 --end)
30104 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30105 are present at buffer positions between START_CHARPOS and
30106 END_CHARPOS, or if they come from an overlay. */
30107 if (EQ (end->object, before_string))
30109 pos = string_buffer_position (before_string, start_charpos);
30110 if (!pos || (pos >= start_charpos && pos < end_charpos))
30111 break;
30113 else if (EQ (end->object, after_string))
30115 pos = string_buffer_position (after_string, end_charpos);
30116 if (!pos || (pos >= start_charpos && pos < end_charpos))
30117 break;
30120 /* Find the X coordinate of the last glyph to be highlighted. */
30121 for (; glyph <= end; ++glyph)
30122 x += glyph->pixel_width;
30124 hlinfo->mouse_face_end_x = x;
30125 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30127 else
30129 /* Skip truncation and continuation glyphs near the end of the
30130 row, and also blanks and stretch glyphs inserted by
30131 extend_face_to_end_of_line. */
30132 x = r2->x;
30133 end++;
30134 while (end < glyph
30135 && NILP (end->object))
30137 x += end->pixel_width;
30138 ++end;
30140 /* Scan the rest of the glyph row from the end, looking for the
30141 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30142 DISP_STRING, or whose position is between START_CHARPOS
30143 and END_CHARPOS */
30144 for ( ;
30145 end < glyph
30146 && !NILP (end->object)
30147 && !EQ (end->object, disp_string)
30148 && !(BUFFERP (end->object)
30149 && (end->charpos >= start_charpos
30150 && end->charpos < end_charpos));
30151 ++end)
30153 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30154 are present at buffer positions between START_CHARPOS and
30155 END_CHARPOS, or if they come from an overlay. */
30156 if (EQ (end->object, before_string))
30158 pos = string_buffer_position (before_string, start_charpos);
30159 if (!pos || (pos >= start_charpos && pos < end_charpos))
30160 break;
30162 else if (EQ (end->object, after_string))
30164 pos = string_buffer_position (after_string, end_charpos);
30165 if (!pos || (pos >= start_charpos && pos < end_charpos))
30166 break;
30168 x += end->pixel_width;
30170 /* If we exited the above loop because we arrived at the last
30171 glyph of the row, and its buffer position is still not in
30172 range, it means the last character in range is the preceding
30173 newline. Bump the end column and x values to get past the
30174 last glyph. */
30175 if (end == glyph
30176 && BUFFERP (end->object)
30177 && (end->charpos < start_charpos
30178 || end->charpos >= end_charpos))
30180 x += end->pixel_width;
30181 ++end;
30183 hlinfo->mouse_face_end_x = x;
30184 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30187 hlinfo->mouse_face_window = window;
30188 hlinfo->mouse_face_face_id
30189 = face_at_buffer_position (w, mouse_charpos, &ignore,
30190 mouse_charpos + 1,
30191 !hlinfo->mouse_face_hidden, -1);
30192 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30195 /* The following function is not used anymore (replaced with
30196 mouse_face_from_string_pos), but I leave it here for the time
30197 being, in case someone would. */
30199 #if false /* not used */
30201 /* Find the position of the glyph for position POS in OBJECT in
30202 window W's current matrix, and return in *X, *Y the pixel
30203 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30205 RIGHT_P means return the position of the right edge of the glyph.
30206 !RIGHT_P means return the left edge position.
30208 If no glyph for POS exists in the matrix, return the position of
30209 the glyph with the next smaller position that is in the matrix, if
30210 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30211 exists in the matrix, return the position of the glyph with the
30212 next larger position in OBJECT.
30214 Value is true if a glyph was found. */
30216 static bool
30217 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30218 int *hpos, int *vpos, int *x, int *y, bool right_p)
30220 int yb = window_text_bottom_y (w);
30221 struct glyph_row *r;
30222 struct glyph *best_glyph = NULL;
30223 struct glyph_row *best_row = NULL;
30224 int best_x = 0;
30226 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30227 r->enabled_p && r->y < yb;
30228 ++r)
30230 struct glyph *g = r->glyphs[TEXT_AREA];
30231 struct glyph *e = g + r->used[TEXT_AREA];
30232 int gx;
30234 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30235 if (EQ (g->object, object))
30237 if (g->charpos == pos)
30239 best_glyph = g;
30240 best_x = gx;
30241 best_row = r;
30242 goto found;
30244 else if (best_glyph == NULL
30245 || ((eabs (g->charpos - pos)
30246 < eabs (best_glyph->charpos - pos))
30247 && (right_p
30248 ? g->charpos < pos
30249 : g->charpos > pos)))
30251 best_glyph = g;
30252 best_x = gx;
30253 best_row = r;
30258 found:
30260 if (best_glyph)
30262 *x = best_x;
30263 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30265 if (right_p)
30267 *x += best_glyph->pixel_width;
30268 ++*hpos;
30271 *y = best_row->y;
30272 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30275 return best_glyph != NULL;
30277 #endif /* not used */
30279 /* Find the positions of the first and the last glyphs in window W's
30280 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30281 (assumed to be a string), and return in HLINFO's mouse_face_*
30282 members the pixel and column/row coordinates of those glyphs. */
30284 static void
30285 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30286 Lisp_Object object,
30287 ptrdiff_t startpos, ptrdiff_t endpos)
30289 int yb = window_text_bottom_y (w);
30290 struct glyph_row *r;
30291 struct glyph *g, *e;
30292 int gx;
30293 bool found = false;
30295 /* Find the glyph row with at least one position in the range
30296 [STARTPOS..ENDPOS), and the first glyph in that row whose
30297 position belongs to that range. */
30298 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30299 r->enabled_p && r->y < yb;
30300 ++r)
30302 if (!r->reversed_p)
30304 g = r->glyphs[TEXT_AREA];
30305 e = g + r->used[TEXT_AREA];
30306 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30307 if (EQ (g->object, object)
30308 && startpos <= g->charpos && g->charpos < endpos)
30310 hlinfo->mouse_face_beg_row
30311 = MATRIX_ROW_VPOS (r, w->current_matrix);
30312 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30313 hlinfo->mouse_face_beg_x = gx;
30314 found = true;
30315 break;
30318 else
30320 struct glyph *g1;
30322 e = r->glyphs[TEXT_AREA];
30323 g = e + r->used[TEXT_AREA];
30324 for ( ; g > e; --g)
30325 if (EQ ((g-1)->object, object)
30326 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30328 hlinfo->mouse_face_beg_row
30329 = MATRIX_ROW_VPOS (r, w->current_matrix);
30330 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30331 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30332 gx += g1->pixel_width;
30333 hlinfo->mouse_face_beg_x = gx;
30334 found = true;
30335 break;
30338 if (found)
30339 break;
30342 if (!found)
30343 return;
30345 /* Starting with the next row, look for the first row which does NOT
30346 include any glyphs whose positions are in the range. */
30347 for (++r; r->enabled_p && r->y < yb; ++r)
30349 g = r->glyphs[TEXT_AREA];
30350 e = g + r->used[TEXT_AREA];
30351 found = false;
30352 for ( ; g < e; ++g)
30353 if (EQ (g->object, object)
30354 && startpos <= g->charpos && g->charpos < endpos)
30356 found = true;
30357 break;
30359 if (!found)
30360 break;
30363 /* The highlighted region ends on the previous row. */
30364 r--;
30366 /* Set the end row. */
30367 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30369 /* Compute and set the end column and the end column's horizontal
30370 pixel coordinate. */
30371 if (!r->reversed_p)
30373 g = r->glyphs[TEXT_AREA];
30374 e = g + r->used[TEXT_AREA];
30375 for ( ; e > g; --e)
30376 if (EQ ((e-1)->object, object)
30377 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30378 break;
30379 hlinfo->mouse_face_end_col = e - g;
30381 for (gx = r->x; g < e; ++g)
30382 gx += g->pixel_width;
30383 hlinfo->mouse_face_end_x = gx;
30385 else
30387 e = r->glyphs[TEXT_AREA];
30388 g = e + r->used[TEXT_AREA];
30389 for (gx = r->x ; e < g; ++e)
30391 if (EQ (e->object, object)
30392 && startpos <= e->charpos && e->charpos < endpos)
30393 break;
30394 gx += e->pixel_width;
30396 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30397 hlinfo->mouse_face_end_x = gx;
30401 #ifdef HAVE_WINDOW_SYSTEM
30403 /* See if position X, Y is within a hot-spot of an image. */
30405 static bool
30406 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30408 if (!CONSP (hot_spot))
30409 return false;
30411 if (EQ (XCAR (hot_spot), Qrect))
30413 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30414 Lisp_Object rect = XCDR (hot_spot);
30415 Lisp_Object tem;
30416 if (!CONSP (rect))
30417 return false;
30418 if (!CONSP (XCAR (rect)))
30419 return false;
30420 if (!CONSP (XCDR (rect)))
30421 return false;
30422 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30423 return false;
30424 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30425 return false;
30426 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30427 return false;
30428 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30429 return false;
30430 return true;
30432 else if (EQ (XCAR (hot_spot), Qcircle))
30434 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30435 Lisp_Object circ = XCDR (hot_spot);
30436 Lisp_Object lr, lx0, ly0;
30437 if (CONSP (circ)
30438 && CONSP (XCAR (circ))
30439 && (lr = XCDR (circ), NUMBERP (lr))
30440 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30441 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30443 double r = XFLOATINT (lr);
30444 double dx = XINT (lx0) - x;
30445 double dy = XINT (ly0) - y;
30446 return (dx * dx + dy * dy <= r * r);
30449 else if (EQ (XCAR (hot_spot), Qpoly))
30451 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30452 if (VECTORP (XCDR (hot_spot)))
30454 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30455 Lisp_Object *poly = v->contents;
30456 ptrdiff_t n = v->header.size;
30457 ptrdiff_t i;
30458 bool inside = false;
30459 Lisp_Object lx, ly;
30460 int x0, y0;
30462 /* Need an even number of coordinates, and at least 3 edges. */
30463 if (n < 6 || n & 1)
30464 return false;
30466 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30467 If count is odd, we are inside polygon. Pixels on edges
30468 may or may not be included depending on actual geometry of the
30469 polygon. */
30470 if ((lx = poly[n-2], !INTEGERP (lx))
30471 || (ly = poly[n-1], !INTEGERP (lx)))
30472 return false;
30473 x0 = XINT (lx), y0 = XINT (ly);
30474 for (i = 0; i < n; i += 2)
30476 int x1 = x0, y1 = y0;
30477 if ((lx = poly[i], !INTEGERP (lx))
30478 || (ly = poly[i+1], !INTEGERP (ly)))
30479 return false;
30480 x0 = XINT (lx), y0 = XINT (ly);
30482 /* Does this segment cross the X line? */
30483 if (x0 >= x)
30485 if (x1 >= x)
30486 continue;
30488 else if (x1 < x)
30489 continue;
30490 if (y > y0 && y > y1)
30491 continue;
30492 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30493 inside = !inside;
30495 return inside;
30498 return false;
30501 Lisp_Object
30502 find_hot_spot (Lisp_Object map, int x, int y)
30504 while (CONSP (map))
30506 if (CONSP (XCAR (map))
30507 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30508 return XCAR (map);
30509 map = XCDR (map);
30512 return Qnil;
30515 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30516 3, 3, 0,
30517 doc: /* Lookup in image map MAP coordinates X and Y.
30518 An image map is an alist where each element has the format (AREA ID PLIST).
30519 An AREA is specified as either a rectangle, a circle, or a polygon:
30520 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30521 pixel coordinates of the upper left and bottom right corners.
30522 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30523 and the radius of the circle; r may be a float or integer.
30524 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30525 vector describes one corner in the polygon.
30526 Returns the alist element for the first matching AREA in MAP. */)
30527 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30529 if (NILP (map))
30530 return Qnil;
30532 CHECK_NUMBER (x);
30533 CHECK_NUMBER (y);
30535 return find_hot_spot (map,
30536 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30537 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30539 #endif /* HAVE_WINDOW_SYSTEM */
30542 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30543 static void
30544 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30546 #ifdef HAVE_WINDOW_SYSTEM
30547 if (!FRAME_WINDOW_P (f))
30548 return;
30550 /* Do not change cursor shape while dragging mouse. */
30551 if (EQ (do_mouse_tracking, Qdragging))
30552 return;
30554 if (!NILP (pointer))
30556 if (EQ (pointer, Qarrow))
30557 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30558 else if (EQ (pointer, Qhand))
30559 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30560 else if (EQ (pointer, Qtext))
30561 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30562 else if (EQ (pointer, intern ("hdrag")))
30563 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30564 else if (EQ (pointer, intern ("nhdrag")))
30565 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30566 # ifdef HAVE_X_WINDOWS
30567 else if (EQ (pointer, intern ("vdrag")))
30568 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30569 # endif
30570 else if (EQ (pointer, intern ("hourglass")))
30571 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30572 else if (EQ (pointer, Qmodeline))
30573 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30574 else
30575 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30578 if (cursor != No_Cursor)
30579 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30580 #endif
30583 /* Take proper action when mouse has moved to the mode or header line
30584 or marginal area AREA of window W, x-position X and y-position Y.
30585 X is relative to the start of the text display area of W, so the
30586 width of bitmap areas and scroll bars must be subtracted to get a
30587 position relative to the start of the mode line. */
30589 static void
30590 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30591 enum window_part area)
30593 struct window *w = XWINDOW (window);
30594 struct frame *f = XFRAME (w->frame);
30595 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30596 #ifdef HAVE_WINDOW_SYSTEM
30597 Display_Info *dpyinfo;
30598 #endif
30599 Cursor cursor = No_Cursor;
30600 Lisp_Object pointer = Qnil;
30601 int dx, dy, width, height;
30602 ptrdiff_t charpos;
30603 Lisp_Object string, object = Qnil;
30604 Lisp_Object pos UNINIT;
30605 Lisp_Object mouse_face;
30606 int original_x_pixel = x;
30607 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30608 struct glyph_row *row UNINIT;
30610 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30612 int x0;
30613 struct glyph *end;
30615 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30616 returns them in row/column units! */
30617 string = mode_line_string (w, area, &x, &y, &charpos,
30618 &object, &dx, &dy, &width, &height);
30620 row = (area == ON_MODE_LINE
30621 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30622 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30624 /* Find the glyph under the mouse pointer. */
30625 if (row->mode_line_p && row->enabled_p)
30627 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30628 end = glyph + row->used[TEXT_AREA];
30630 for (x0 = original_x_pixel;
30631 glyph < end && x0 >= glyph->pixel_width;
30632 ++glyph)
30633 x0 -= glyph->pixel_width;
30635 if (glyph >= end)
30636 glyph = NULL;
30639 else
30641 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30642 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30643 returns them in row/column units! */
30644 string = marginal_area_string (w, area, &x, &y, &charpos,
30645 &object, &dx, &dy, &width, &height);
30648 Lisp_Object help = Qnil;
30650 #ifdef HAVE_WINDOW_SYSTEM
30651 if (IMAGEP (object))
30653 Lisp_Object image_map, hotspot;
30654 if ((image_map = Fplist_get (XCDR (object), QCmap),
30655 !NILP (image_map))
30656 && (hotspot = find_hot_spot (image_map, dx, dy),
30657 CONSP (hotspot))
30658 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30660 Lisp_Object plist;
30662 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30663 If so, we could look for mouse-enter, mouse-leave
30664 properties in PLIST (and do something...). */
30665 hotspot = XCDR (hotspot);
30666 if (CONSP (hotspot)
30667 && (plist = XCAR (hotspot), CONSP (plist)))
30669 pointer = Fplist_get (plist, Qpointer);
30670 if (NILP (pointer))
30671 pointer = Qhand;
30672 help = Fplist_get (plist, Qhelp_echo);
30673 if (!NILP (help))
30675 help_echo_string = help;
30676 XSETWINDOW (help_echo_window, w);
30677 help_echo_object = w->contents;
30678 help_echo_pos = charpos;
30682 if (NILP (pointer))
30683 pointer = Fplist_get (XCDR (object), QCpointer);
30685 #endif /* HAVE_WINDOW_SYSTEM */
30687 if (STRINGP (string))
30688 pos = make_number (charpos);
30690 /* Set the help text and mouse pointer. If the mouse is on a part
30691 of the mode line without any text (e.g. past the right edge of
30692 the mode line text), use the default help text and pointer. */
30693 if (STRINGP (string) || area == ON_MODE_LINE)
30695 /* Arrange to display the help by setting the global variables
30696 help_echo_string, help_echo_object, and help_echo_pos. */
30697 if (NILP (help))
30699 if (STRINGP (string))
30700 help = Fget_text_property (pos, Qhelp_echo, string);
30702 if (!NILP (help))
30704 help_echo_string = help;
30705 XSETWINDOW (help_echo_window, w);
30706 help_echo_object = string;
30707 help_echo_pos = charpos;
30709 else if (area == ON_MODE_LINE)
30711 Lisp_Object default_help
30712 = buffer_local_value (Qmode_line_default_help_echo,
30713 w->contents);
30715 if (STRINGP (default_help))
30717 help_echo_string = default_help;
30718 XSETWINDOW (help_echo_window, w);
30719 help_echo_object = Qnil;
30720 help_echo_pos = -1;
30725 #ifdef HAVE_WINDOW_SYSTEM
30726 /* Change the mouse pointer according to what is under it. */
30727 if (FRAME_WINDOW_P (f))
30729 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30730 || minibuf_level
30731 || NILP (Vresize_mini_windows));
30733 dpyinfo = FRAME_DISPLAY_INFO (f);
30734 if (STRINGP (string))
30736 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30738 if (NILP (pointer))
30739 pointer = Fget_text_property (pos, Qpointer, string);
30741 /* Change the mouse pointer according to what is under X/Y. */
30742 if (NILP (pointer)
30743 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30745 Lisp_Object map;
30746 map = Fget_text_property (pos, Qlocal_map, string);
30747 if (!KEYMAPP (map))
30748 map = Fget_text_property (pos, Qkeymap, string);
30749 if (!KEYMAPP (map) && draggable)
30750 cursor = dpyinfo->vertical_scroll_bar_cursor;
30753 else if (draggable)
30754 /* Default mode-line pointer. */
30755 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30757 #endif
30760 /* Change the mouse face according to what is under X/Y. */
30761 bool mouse_face_shown = false;
30762 if (STRINGP (string))
30764 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30765 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30766 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30767 && glyph)
30769 Lisp_Object b, e;
30771 struct glyph * tmp_glyph;
30773 int gpos;
30774 int gseq_length;
30775 int total_pixel_width;
30776 ptrdiff_t begpos, endpos, ignore;
30778 int vpos, hpos;
30780 b = Fprevious_single_property_change (make_number (charpos + 1),
30781 Qmouse_face, string, Qnil);
30782 if (NILP (b))
30783 begpos = 0;
30784 else
30785 begpos = XINT (b);
30787 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30788 if (NILP (e))
30789 endpos = SCHARS (string);
30790 else
30791 endpos = XINT (e);
30793 /* Calculate the glyph position GPOS of GLYPH in the
30794 displayed string, relative to the beginning of the
30795 highlighted part of the string.
30797 Note: GPOS is different from CHARPOS. CHARPOS is the
30798 position of GLYPH in the internal string object. A mode
30799 line string format has structures which are converted to
30800 a flattened string by the Emacs Lisp interpreter. The
30801 internal string is an element of those structures. The
30802 displayed string is the flattened string. */
30803 tmp_glyph = row_start_glyph;
30804 while (tmp_glyph < glyph
30805 && (!(EQ (tmp_glyph->object, glyph->object)
30806 && begpos <= tmp_glyph->charpos
30807 && tmp_glyph->charpos < endpos)))
30808 tmp_glyph++;
30809 gpos = glyph - tmp_glyph;
30811 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30812 the highlighted part of the displayed string to which
30813 GLYPH belongs. Note: GSEQ_LENGTH is different from
30814 SCHARS (STRING), because the latter returns the length of
30815 the internal string. */
30816 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30817 tmp_glyph > glyph
30818 && (!(EQ (tmp_glyph->object, glyph->object)
30819 && begpos <= tmp_glyph->charpos
30820 && tmp_glyph->charpos < endpos));
30821 tmp_glyph--)
30823 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30825 /* Calculate the total pixel width of all the glyphs between
30826 the beginning of the highlighted area and GLYPH. */
30827 total_pixel_width = 0;
30828 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30829 total_pixel_width += tmp_glyph->pixel_width;
30831 /* Pre calculation of re-rendering position. Note: X is in
30832 column units here, after the call to mode_line_string or
30833 marginal_area_string. */
30834 hpos = x - gpos;
30835 vpos = (area == ON_MODE_LINE
30836 ? (w->current_matrix)->nrows - 1
30837 : 0);
30839 /* If GLYPH's position is included in the region that is
30840 already drawn in mouse face, we have nothing to do. */
30841 if ( EQ (window, hlinfo->mouse_face_window)
30842 && (!row->reversed_p
30843 ? (hlinfo->mouse_face_beg_col <= hpos
30844 && hpos < hlinfo->mouse_face_end_col)
30845 /* In R2L rows we swap BEG and END, see below. */
30846 : (hlinfo->mouse_face_end_col <= hpos
30847 && hpos < hlinfo->mouse_face_beg_col))
30848 && hlinfo->mouse_face_beg_row == vpos )
30849 return;
30851 if (clear_mouse_face (hlinfo))
30852 cursor = No_Cursor;
30854 if (!row->reversed_p)
30856 hlinfo->mouse_face_beg_col = hpos;
30857 hlinfo->mouse_face_beg_x = original_x_pixel
30858 - (total_pixel_width + dx);
30859 hlinfo->mouse_face_end_col = hpos + gseq_length;
30860 hlinfo->mouse_face_end_x = 0;
30862 else
30864 /* In R2L rows, show_mouse_face expects BEG and END
30865 coordinates to be swapped. */
30866 hlinfo->mouse_face_end_col = hpos;
30867 hlinfo->mouse_face_end_x = original_x_pixel
30868 - (total_pixel_width + dx);
30869 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30870 hlinfo->mouse_face_beg_x = 0;
30873 hlinfo->mouse_face_beg_row = vpos;
30874 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30875 hlinfo->mouse_face_past_end = false;
30876 hlinfo->mouse_face_window = window;
30878 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30879 charpos,
30880 0, &ignore,
30881 glyph->face_id,
30882 true);
30883 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30884 mouse_face_shown = true;
30886 if (NILP (pointer))
30887 pointer = Qhand;
30891 /* If mouse-face doesn't need to be shown, clear any existing
30892 mouse-face. */
30893 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30894 clear_mouse_face (hlinfo);
30896 define_frame_cursor1 (f, cursor, pointer);
30900 /* EXPORT:
30901 Take proper action when the mouse has moved to position X, Y on
30902 frame F with regards to highlighting portions of display that have
30903 mouse-face properties. Also de-highlight portions of display where
30904 the mouse was before, set the mouse pointer shape as appropriate
30905 for the mouse coordinates, and activate help echo (tooltips).
30906 X and Y can be negative or out of range. */
30908 void
30909 note_mouse_highlight (struct frame *f, int x, int y)
30911 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30912 enum window_part part = ON_NOTHING;
30913 Lisp_Object window;
30914 struct window *w;
30915 Cursor cursor = No_Cursor;
30916 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30917 struct buffer *b;
30919 /* When a menu is active, don't highlight because this looks odd. */
30920 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30921 if (popup_activated ())
30922 return;
30923 #endif
30925 if (!f->glyphs_initialized_p
30926 || f->pointer_invisible)
30927 return;
30929 hlinfo->mouse_face_mouse_x = x;
30930 hlinfo->mouse_face_mouse_y = y;
30931 hlinfo->mouse_face_mouse_frame = f;
30933 if (hlinfo->mouse_face_defer)
30934 return;
30936 /* Which window is that in? */
30937 window = window_from_coordinates (f, x, y, &part, true);
30939 /* If displaying active text in another window, clear that. */
30940 if (! EQ (window, hlinfo->mouse_face_window)
30941 /* Also clear if we move out of text area in same window. */
30942 || (!NILP (hlinfo->mouse_face_window)
30943 && !NILP (window)
30944 && part != ON_TEXT
30945 && part != ON_MODE_LINE
30946 && part != ON_HEADER_LINE))
30947 clear_mouse_face (hlinfo);
30949 /* Reset help_echo_string. It will get recomputed below. */
30950 help_echo_string = Qnil;
30952 #ifdef HAVE_WINDOW_SYSTEM
30953 /* If the cursor is on the internal border of FRAME and FRAME's
30954 internal border is draggable, provide some visual feedback. */
30955 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
30956 && !NILP (get_frame_param (f, Qdrag_internal_border)))
30958 enum internal_border_part part = frame_internal_border_part (f, x, y);
30960 switch (part)
30962 case INTERNAL_BORDER_NONE:
30963 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30964 /* Reset cursor. */
30965 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30966 break;
30967 case INTERNAL_BORDER_LEFT_EDGE:
30968 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
30969 break;
30970 case INTERNAL_BORDER_TOP_LEFT_CORNER:
30971 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
30972 break;
30973 case INTERNAL_BORDER_TOP_EDGE:
30974 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
30975 break;
30976 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
30977 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
30978 break;
30979 case INTERNAL_BORDER_RIGHT_EDGE:
30980 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
30981 break;
30982 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
30983 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
30984 break;
30985 case INTERNAL_BORDER_BOTTOM_EDGE:
30986 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
30987 break;
30988 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
30989 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
30990 break;
30991 default:
30992 /* This should not happen. */
30993 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30994 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30997 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30999 /* Do we really want a help echo here? */
31000 help_echo_string = build_string ("drag-mouse-1: resize frame");
31001 goto set_cursor;
31004 #endif /* HAVE_WINDOW_SYSTEM */
31006 /* Not on a window -> return. */
31007 if (!WINDOWP (window))
31008 return;
31010 /* Convert to window-relative pixel coordinates. */
31011 w = XWINDOW (window);
31012 frame_to_window_pixel_xy (w, &x, &y);
31014 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31015 /* Handle tool-bar window differently since it doesn't display a
31016 buffer. */
31017 if (EQ (window, f->tool_bar_window))
31019 note_tool_bar_highlight (f, x, y);
31020 return;
31022 #endif
31024 /* Mouse is on the mode, header line or margin? */
31025 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31026 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31028 note_mode_line_or_margin_highlight (window, x, y, part);
31030 #ifdef HAVE_WINDOW_SYSTEM
31031 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31033 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31034 /* Show non-text cursor (Bug#16647). */
31035 goto set_cursor;
31037 else
31038 #endif
31039 return;
31042 #ifdef HAVE_WINDOW_SYSTEM
31043 if (part == ON_VERTICAL_BORDER)
31045 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31046 help_echo_string = build_string ("drag-mouse-1: resize");
31047 goto set_cursor;
31049 else if (part == ON_RIGHT_DIVIDER)
31051 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31052 help_echo_string = build_string ("drag-mouse-1: resize");
31053 goto set_cursor;
31055 else if (part == ON_BOTTOM_DIVIDER)
31056 if (! WINDOW_BOTTOMMOST_P (w)
31057 || minibuf_level
31058 || NILP (Vresize_mini_windows))
31060 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31061 help_echo_string = build_string ("drag-mouse-1: resize");
31062 goto set_cursor;
31064 else
31065 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31066 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31067 || part == ON_VERTICAL_SCROLL_BAR
31068 || part == ON_HORIZONTAL_SCROLL_BAR)
31069 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31070 else
31071 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31072 #endif
31074 /* Are we in a window whose display is up to date?
31075 And verify the buffer's text has not changed. */
31076 b = XBUFFER (w->contents);
31077 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31079 int hpos, vpos, dx, dy, area = LAST_AREA;
31080 ptrdiff_t pos;
31081 struct glyph *glyph;
31082 Lisp_Object object;
31083 Lisp_Object mouse_face = Qnil, position;
31084 Lisp_Object *overlay_vec = NULL;
31085 ptrdiff_t i, noverlays;
31086 struct buffer *obuf;
31087 ptrdiff_t obegv, ozv;
31088 bool same_region;
31090 /* Find the glyph under X/Y. */
31091 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31093 #ifdef HAVE_WINDOW_SYSTEM
31094 /* Look for :pointer property on image. */
31095 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31097 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31098 if (img != NULL && IMAGEP (img->spec))
31100 Lisp_Object image_map, hotspot;
31101 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31102 !NILP (image_map))
31103 && (hotspot = find_hot_spot (image_map,
31104 glyph->slice.img.x + dx,
31105 glyph->slice.img.y + dy),
31106 CONSP (hotspot))
31107 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31109 Lisp_Object plist;
31111 /* Could check XCAR (hotspot) to see if we enter/leave
31112 this hot-spot.
31113 If so, we could look for mouse-enter, mouse-leave
31114 properties in PLIST (and do something...). */
31115 hotspot = XCDR (hotspot);
31116 if (CONSP (hotspot)
31117 && (plist = XCAR (hotspot), CONSP (plist)))
31119 pointer = Fplist_get (plist, Qpointer);
31120 if (NILP (pointer))
31121 pointer = Qhand;
31122 help_echo_string = Fplist_get (plist, Qhelp_echo);
31123 if (!NILP (help_echo_string))
31125 help_echo_window = window;
31126 help_echo_object = glyph->object;
31127 help_echo_pos = glyph->charpos;
31131 if (NILP (pointer))
31132 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31135 #endif /* HAVE_WINDOW_SYSTEM */
31137 /* Clear mouse face if X/Y not over text. */
31138 if (glyph == NULL
31139 || area != TEXT_AREA
31140 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31141 /* Glyph's OBJECT is nil for glyphs inserted by the
31142 display engine for its internal purposes, like truncation
31143 and continuation glyphs and blanks beyond the end of
31144 line's text on text terminals. If we are over such a
31145 glyph, we are not over any text. */
31146 || NILP (glyph->object)
31147 /* R2L rows have a stretch glyph at their front, which
31148 stands for no text, whereas L2R rows have no glyphs at
31149 all beyond the end of text. Treat such stretch glyphs
31150 like we do with NULL glyphs in L2R rows. */
31151 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31152 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31153 && glyph->type == STRETCH_GLYPH
31154 && glyph->avoid_cursor_p))
31156 if (clear_mouse_face (hlinfo))
31157 cursor = No_Cursor;
31158 if (FRAME_WINDOW_P (f) && NILP (pointer))
31160 #ifdef HAVE_WINDOW_SYSTEM
31161 if (area != TEXT_AREA)
31162 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31163 else
31164 pointer = Vvoid_text_area_pointer;
31165 #endif
31167 goto set_cursor;
31170 pos = glyph->charpos;
31171 object = glyph->object;
31172 if (!STRINGP (object) && !BUFFERP (object))
31173 goto set_cursor;
31175 /* If we get an out-of-range value, return now; avoid an error. */
31176 if (BUFFERP (object) && pos > BUF_Z (b))
31177 goto set_cursor;
31179 /* Make the window's buffer temporarily current for
31180 overlays_at and compute_char_face. */
31181 obuf = current_buffer;
31182 current_buffer = b;
31183 obegv = BEGV;
31184 ozv = ZV;
31185 BEGV = BEG;
31186 ZV = Z;
31188 /* Is this char mouse-active or does it have help-echo? */
31189 position = make_number (pos);
31191 USE_SAFE_ALLOCA;
31193 if (BUFFERP (object))
31195 /* Put all the overlays we want in a vector in overlay_vec. */
31196 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31197 /* Sort overlays into increasing priority order. */
31198 noverlays = sort_overlays (overlay_vec, noverlays, w);
31200 else
31201 noverlays = 0;
31203 if (NILP (Vmouse_highlight))
31205 clear_mouse_face (hlinfo);
31206 goto check_help_echo;
31209 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31211 if (same_region)
31212 cursor = No_Cursor;
31214 /* Check mouse-face highlighting. */
31215 if (! same_region
31216 /* If there exists an overlay with mouse-face overlapping
31217 the one we are currently highlighting, we have to
31218 check if we enter the overlapping overlay, and then
31219 highlight only that. */
31220 || (OVERLAYP (hlinfo->mouse_face_overlay)
31221 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31223 /* Find the highest priority overlay with a mouse-face. */
31224 Lisp_Object overlay = Qnil;
31225 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31227 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31228 if (!NILP (mouse_face))
31229 overlay = overlay_vec[i];
31232 /* If we're highlighting the same overlay as before, there's
31233 no need to do that again. */
31234 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31235 goto check_help_echo;
31237 /* Clear the display of the old active region, if any. */
31238 if (clear_mouse_face (hlinfo))
31239 cursor = No_Cursor;
31241 /* Record the overlay, if any, to be highlighted. */
31242 hlinfo->mouse_face_overlay = overlay;
31244 /* If no overlay applies, get a text property. */
31245 if (NILP (overlay))
31246 mouse_face = Fget_text_property (position, Qmouse_face, object);
31248 /* Next, compute the bounds of the mouse highlighting and
31249 display it. */
31250 if (!NILP (mouse_face) && STRINGP (object))
31252 /* The mouse-highlighting comes from a display string
31253 with a mouse-face. */
31254 Lisp_Object s, e;
31255 ptrdiff_t ignore;
31257 s = Fprevious_single_property_change
31258 (make_number (pos + 1), Qmouse_face, object, Qnil);
31259 e = Fnext_single_property_change
31260 (position, Qmouse_face, object, Qnil);
31261 if (NILP (s))
31262 s = make_number (0);
31263 if (NILP (e))
31264 e = make_number (SCHARS (object));
31265 mouse_face_from_string_pos (w, hlinfo, object,
31266 XINT (s), XINT (e));
31267 hlinfo->mouse_face_past_end = false;
31268 hlinfo->mouse_face_window = window;
31269 hlinfo->mouse_face_face_id
31270 = face_at_string_position (w, object, pos, 0, &ignore,
31271 glyph->face_id, true);
31272 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31273 cursor = No_Cursor;
31275 else
31277 /* The mouse-highlighting, if any, comes from an overlay
31278 or text property in the buffer. */
31279 Lisp_Object buffer UNINIT;
31280 Lisp_Object disp_string UNINIT;
31282 if (STRINGP (object))
31284 /* If we are on a display string with no mouse-face,
31285 check if the text under it has one. */
31286 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31287 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31288 pos = string_buffer_position (object, start);
31289 if (pos > 0)
31291 mouse_face = get_char_property_and_overlay
31292 (make_number (pos), Qmouse_face, w->contents, &overlay);
31293 buffer = w->contents;
31294 disp_string = object;
31297 else
31299 buffer = object;
31300 disp_string = Qnil;
31303 if (!NILP (mouse_face))
31305 Lisp_Object before, after;
31306 Lisp_Object before_string, after_string;
31307 /* To correctly find the limits of mouse highlight
31308 in a bidi-reordered buffer, we must not use the
31309 optimization of limiting the search in
31310 previous-single-property-change and
31311 next-single-property-change, because
31312 rows_from_pos_range needs the real start and end
31313 positions to DTRT in this case. That's because
31314 the first row visible in a window does not
31315 necessarily display the character whose position
31316 is the smallest. */
31317 Lisp_Object lim1
31318 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31319 ? Fmarker_position (w->start)
31320 : Qnil;
31321 Lisp_Object lim2
31322 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31323 ? make_number (BUF_Z (XBUFFER (buffer))
31324 - w->window_end_pos)
31325 : Qnil;
31327 if (NILP (overlay))
31329 /* Handle the text property case. */
31330 before = Fprevious_single_property_change
31331 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31332 after = Fnext_single_property_change
31333 (make_number (pos), Qmouse_face, buffer, lim2);
31334 before_string = after_string = Qnil;
31336 else
31338 /* Handle the overlay case. */
31339 before = Foverlay_start (overlay);
31340 after = Foverlay_end (overlay);
31341 before_string = Foverlay_get (overlay, Qbefore_string);
31342 after_string = Foverlay_get (overlay, Qafter_string);
31344 if (!STRINGP (before_string)) before_string = Qnil;
31345 if (!STRINGP (after_string)) after_string = Qnil;
31348 mouse_face_from_buffer_pos (window, hlinfo, pos,
31349 NILP (before)
31351 : XFASTINT (before),
31352 NILP (after)
31353 ? BUF_Z (XBUFFER (buffer))
31354 : XFASTINT (after),
31355 before_string, after_string,
31356 disp_string);
31357 cursor = No_Cursor;
31362 check_help_echo:
31364 /* Look for a `help-echo' property. */
31365 if (NILP (help_echo_string)) {
31366 Lisp_Object help, overlay;
31368 /* Check overlays first. */
31369 help = overlay = Qnil;
31370 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31372 overlay = overlay_vec[i];
31373 help = Foverlay_get (overlay, Qhelp_echo);
31376 if (!NILP (help))
31378 help_echo_string = help;
31379 help_echo_window = window;
31380 help_echo_object = overlay;
31381 help_echo_pos = pos;
31383 else
31385 Lisp_Object obj = glyph->object;
31386 ptrdiff_t charpos = glyph->charpos;
31388 /* Try text properties. */
31389 if (STRINGP (obj)
31390 && charpos >= 0
31391 && charpos < SCHARS (obj))
31393 help = Fget_text_property (make_number (charpos),
31394 Qhelp_echo, obj);
31395 if (NILP (help))
31397 /* If the string itself doesn't specify a help-echo,
31398 see if the buffer text ``under'' it does. */
31399 struct glyph_row *r
31400 = MATRIX_ROW (w->current_matrix, vpos);
31401 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31402 ptrdiff_t p = string_buffer_position (obj, start);
31403 if (p > 0)
31405 help = Fget_char_property (make_number (p),
31406 Qhelp_echo, w->contents);
31407 if (!NILP (help))
31409 charpos = p;
31410 obj = w->contents;
31415 else if (BUFFERP (obj)
31416 && charpos >= BEGV
31417 && charpos < ZV)
31418 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31419 obj);
31421 if (!NILP (help))
31423 help_echo_string = help;
31424 help_echo_window = window;
31425 help_echo_object = obj;
31426 help_echo_pos = charpos;
31431 #ifdef HAVE_WINDOW_SYSTEM
31432 /* Look for a `pointer' property. */
31433 if (FRAME_WINDOW_P (f) && NILP (pointer))
31435 /* Check overlays first. */
31436 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31437 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31439 if (NILP (pointer))
31441 Lisp_Object obj = glyph->object;
31442 ptrdiff_t charpos = glyph->charpos;
31444 /* Try text properties. */
31445 if (STRINGP (obj)
31446 && charpos >= 0
31447 && charpos < SCHARS (obj))
31449 pointer = Fget_text_property (make_number (charpos),
31450 Qpointer, obj);
31451 if (NILP (pointer))
31453 /* If the string itself doesn't specify a pointer,
31454 see if the buffer text ``under'' it does. */
31455 struct glyph_row *r
31456 = MATRIX_ROW (w->current_matrix, vpos);
31457 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31458 ptrdiff_t p = string_buffer_position (obj, start);
31459 if (p > 0)
31460 pointer = Fget_char_property (make_number (p),
31461 Qpointer, w->contents);
31464 else if (BUFFERP (obj)
31465 && charpos >= BEGV
31466 && charpos < ZV)
31467 pointer = Fget_text_property (make_number (charpos),
31468 Qpointer, obj);
31471 #endif /* HAVE_WINDOW_SYSTEM */
31473 BEGV = obegv;
31474 ZV = ozv;
31475 current_buffer = obuf;
31476 SAFE_FREE ();
31479 set_cursor:
31480 define_frame_cursor1 (f, cursor, pointer);
31484 /* EXPORT for RIF:
31485 Clear any mouse-face on window W. This function is part of the
31486 redisplay interface, and is called from try_window_id and similar
31487 functions to ensure the mouse-highlight is off. */
31489 void
31490 x_clear_window_mouse_face (struct window *w)
31492 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31493 Lisp_Object window;
31495 block_input ();
31496 XSETWINDOW (window, w);
31497 if (EQ (window, hlinfo->mouse_face_window))
31498 clear_mouse_face (hlinfo);
31499 unblock_input ();
31503 /* EXPORT:
31504 Just discard the mouse face information for frame F, if any.
31505 This is used when the size of F is changed. */
31507 void
31508 cancel_mouse_face (struct frame *f)
31510 Lisp_Object window;
31511 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31513 window = hlinfo->mouse_face_window;
31514 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31515 reset_mouse_highlight (hlinfo);
31520 /***********************************************************************
31521 Exposure Events
31522 ***********************************************************************/
31524 #ifdef HAVE_WINDOW_SYSTEM
31526 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31527 which intersects rectangle R. R is in window-relative coordinates. */
31529 static void
31530 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31531 enum glyph_row_area area)
31533 struct glyph *first = row->glyphs[area];
31534 struct glyph *end = row->glyphs[area] + row->used[area];
31535 struct glyph *last;
31536 int first_x, start_x, x;
31538 if (area == TEXT_AREA && row->fill_line_p)
31539 /* If row extends face to end of line write the whole line. */
31540 draw_glyphs (w, 0, row, area,
31541 0, row->used[area],
31542 DRAW_NORMAL_TEXT, 0);
31543 else
31545 /* Set START_X to the window-relative start position for drawing glyphs of
31546 AREA. The first glyph of the text area can be partially visible.
31547 The first glyphs of other areas cannot. */
31548 start_x = window_box_left_offset (w, area);
31549 x = start_x;
31550 if (area == TEXT_AREA)
31551 x += row->x;
31553 /* Find the first glyph that must be redrawn. */
31554 while (first < end
31555 && x + first->pixel_width < r->x)
31557 x += first->pixel_width;
31558 ++first;
31561 /* Find the last one. */
31562 last = first;
31563 first_x = x;
31564 /* Use a signed int intermediate value to avoid catastrophic
31565 failures due to comparison between signed and unsigned, when
31566 x is negative (can happen for wide images that are hscrolled). */
31567 int r_end = r->x + r->width;
31568 while (last < end && x < r_end)
31570 x += last->pixel_width;
31571 ++last;
31574 /* Repaint. */
31575 if (last > first)
31576 draw_glyphs (w, first_x - start_x, row, area,
31577 first - row->glyphs[area], last - row->glyphs[area],
31578 DRAW_NORMAL_TEXT, 0);
31583 /* Redraw the parts of the glyph row ROW on window W intersecting
31584 rectangle R. R is in window-relative coordinates. Value is
31585 true if mouse-face was overwritten. */
31587 static bool
31588 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31590 eassert (row->enabled_p);
31592 if (row->mode_line_p || w->pseudo_window_p)
31593 draw_glyphs (w, 0, row, TEXT_AREA,
31594 0, row->used[TEXT_AREA],
31595 DRAW_NORMAL_TEXT, 0);
31596 else
31598 if (row->used[LEFT_MARGIN_AREA])
31599 expose_area (w, row, r, LEFT_MARGIN_AREA);
31600 if (row->used[TEXT_AREA])
31601 expose_area (w, row, r, TEXT_AREA);
31602 if (row->used[RIGHT_MARGIN_AREA])
31603 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31604 draw_row_fringe_bitmaps (w, row);
31607 return row->mouse_face_p;
31611 /* Redraw those parts of glyphs rows during expose event handling that
31612 overlap other rows. Redrawing of an exposed line writes over parts
31613 of lines overlapping that exposed line; this function fixes that.
31615 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31616 row in W's current matrix that is exposed and overlaps other rows.
31617 LAST_OVERLAPPING_ROW is the last such row. */
31619 static void
31620 expose_overlaps (struct window *w,
31621 struct glyph_row *first_overlapping_row,
31622 struct glyph_row *last_overlapping_row,
31623 XRectangle *r)
31625 struct glyph_row *row;
31627 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31628 if (row->overlapping_p)
31630 eassert (row->enabled_p && !row->mode_line_p);
31632 row->clip = r;
31633 if (row->used[LEFT_MARGIN_AREA])
31634 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31636 if (row->used[TEXT_AREA])
31637 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31639 if (row->used[RIGHT_MARGIN_AREA])
31640 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31641 row->clip = NULL;
31646 /* Return true if W's cursor intersects rectangle R. */
31648 static bool
31649 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31651 XRectangle cr, result;
31652 struct glyph *cursor_glyph;
31653 struct glyph_row *row;
31655 if (w->phys_cursor.vpos >= 0
31656 && w->phys_cursor.vpos < w->current_matrix->nrows
31657 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31658 row->enabled_p)
31659 && row->cursor_in_fringe_p)
31661 /* Cursor is in the fringe. */
31662 cr.x = window_box_right_offset (w,
31663 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31664 ? RIGHT_MARGIN_AREA
31665 : TEXT_AREA));
31666 cr.y = row->y;
31667 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31668 cr.height = row->height;
31669 return x_intersect_rectangles (&cr, r, &result);
31672 cursor_glyph = get_phys_cursor_glyph (w);
31673 if (cursor_glyph)
31675 /* r is relative to W's box, but w->phys_cursor.x is relative
31676 to left edge of W's TEXT area. Adjust it. */
31677 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31678 cr.y = w->phys_cursor.y;
31679 cr.width = cursor_glyph->pixel_width;
31680 cr.height = w->phys_cursor_height;
31681 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31682 I assume the effect is the same -- and this is portable. */
31683 return x_intersect_rectangles (&cr, r, &result);
31685 /* If we don't understand the format, pretend we're not in the hot-spot. */
31686 return false;
31690 /* EXPORT:
31691 Draw a vertical window border to the right of window W if W doesn't
31692 have vertical scroll bars. */
31694 void
31695 x_draw_vertical_border (struct window *w)
31697 struct frame *f = XFRAME (WINDOW_FRAME (w));
31699 /* We could do better, if we knew what type of scroll-bar the adjacent
31700 windows (on either side) have... But we don't :-(
31701 However, I think this works ok. ++KFS 2003-04-25 */
31703 /* Redraw borders between horizontally adjacent windows. Don't
31704 do it for frames with vertical scroll bars because either the
31705 right scroll bar of a window, or the left scroll bar of its
31706 neighbor will suffice as a border. */
31707 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31708 return;
31710 /* Note: It is necessary to redraw both the left and the right
31711 borders, for when only this single window W is being
31712 redisplayed. */
31713 if (!WINDOW_RIGHTMOST_P (w)
31714 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31716 int x0, x1, y0, y1;
31718 window_box_edges (w, &x0, &y0, &x1, &y1);
31719 y1 -= 1;
31721 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31722 x1 -= 1;
31724 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31727 if (!WINDOW_LEFTMOST_P (w)
31728 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31730 int x0, x1, y0, y1;
31732 window_box_edges (w, &x0, &y0, &x1, &y1);
31733 y1 -= 1;
31735 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31736 x0 -= 1;
31738 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31743 /* Draw window dividers for window W. */
31745 void
31746 x_draw_right_divider (struct window *w)
31748 struct frame *f = WINDOW_XFRAME (w);
31750 if (w->mini || w->pseudo_window_p)
31751 return;
31752 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31754 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31755 int x1 = WINDOW_RIGHT_EDGE_X (w);
31756 int y0 = WINDOW_TOP_EDGE_Y (w);
31757 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31759 /* If W is horizontally combined and has a right sibling, don't
31760 draw over any bottom divider. */
31761 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31762 && !NILP (w->parent)
31763 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31764 && !NILP (w->next))
31765 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31767 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31771 static void
31772 x_draw_bottom_divider (struct window *w)
31774 struct frame *f = XFRAME (WINDOW_FRAME (w));
31776 if (w->mini || w->pseudo_window_p)
31777 return;
31778 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31780 int x0 = WINDOW_LEFT_EDGE_X (w);
31781 int x1 = WINDOW_RIGHT_EDGE_X (w);
31782 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31783 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31784 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31786 /* If W is vertically combined and has a sibling below, don't draw
31787 over any right divider. */
31788 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31789 && p
31790 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31791 && !NILP (w->next))
31792 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31793 && NILP (w->next)
31794 && !NILP (p->parent)
31795 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31796 && !NILP (XWINDOW (p->parent)->next))))
31797 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31799 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31803 /* Redraw the part of window W intersection rectangle FR. Pixel
31804 coordinates in FR are frame-relative. Call this function with
31805 input blocked. Value is true if the exposure overwrites
31806 mouse-face. */
31808 static bool
31809 expose_window (struct window *w, XRectangle *fr)
31811 struct frame *f = XFRAME (w->frame);
31812 XRectangle wr, r;
31813 bool mouse_face_overwritten_p = false;
31815 /* If window is not yet fully initialized, do nothing. This can
31816 happen when toolkit scroll bars are used and a window is split.
31817 Reconfiguring the scroll bar will generate an expose for a newly
31818 created window. */
31819 if (w->current_matrix == NULL)
31820 return false;
31822 /* When we're currently updating the window, display and current
31823 matrix usually don't agree. Arrange for a thorough display
31824 later. */
31825 if (w->must_be_updated_p)
31827 SET_FRAME_GARBAGED (f);
31828 return false;
31831 /* Frame-relative pixel rectangle of W. */
31832 wr.x = WINDOW_LEFT_EDGE_X (w);
31833 wr.y = WINDOW_TOP_EDGE_Y (w);
31834 wr.width = WINDOW_PIXEL_WIDTH (w);
31835 wr.height = WINDOW_PIXEL_HEIGHT (w);
31837 if (x_intersect_rectangles (fr, &wr, &r))
31839 int yb = window_text_bottom_y (w);
31840 struct glyph_row *row;
31841 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31843 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31844 r.x, r.y, r.width, r.height));
31846 /* Convert to window coordinates. */
31847 r.x -= WINDOW_LEFT_EDGE_X (w);
31848 r.y -= WINDOW_TOP_EDGE_Y (w);
31850 /* Turn off the cursor. */
31851 bool cursor_cleared_p = (!w->pseudo_window_p
31852 && phys_cursor_in_rect_p (w, &r));
31853 if (cursor_cleared_p)
31854 x_clear_cursor (w);
31856 /* If the row containing the cursor extends face to end of line,
31857 then expose_area might overwrite the cursor outside the
31858 rectangle and thus notice_overwritten_cursor might clear
31859 w->phys_cursor_on_p. We remember the original value and
31860 check later if it is changed. */
31861 bool phys_cursor_on_p = w->phys_cursor_on_p;
31863 /* Use a signed int intermediate value to avoid catastrophic
31864 failures due to comparison between signed and unsigned, when
31865 y0 or y1 is negative (can happen for tall images). */
31866 int r_bottom = r.y + r.height;
31868 /* Update lines intersecting rectangle R. */
31869 first_overlapping_row = last_overlapping_row = NULL;
31870 for (row = w->current_matrix->rows;
31871 row->enabled_p;
31872 ++row)
31874 int y0 = row->y;
31875 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31877 if ((y0 >= r.y && y0 < r_bottom)
31878 || (y1 > r.y && y1 < r_bottom)
31879 || (r.y >= y0 && r.y < y1)
31880 || (r_bottom > y0 && r_bottom < y1))
31882 /* A header line may be overlapping, but there is no need
31883 to fix overlapping areas for them. KFS 2005-02-12 */
31884 if (row->overlapping_p && !row->mode_line_p)
31886 if (first_overlapping_row == NULL)
31887 first_overlapping_row = row;
31888 last_overlapping_row = row;
31891 row->clip = fr;
31892 if (expose_line (w, row, &r))
31893 mouse_face_overwritten_p = true;
31894 row->clip = NULL;
31896 else if (row->overlapping_p)
31898 /* We must redraw a row overlapping the exposed area. */
31899 if (y0 < r.y
31900 ? y0 + row->phys_height > r.y
31901 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31903 if (first_overlapping_row == NULL)
31904 first_overlapping_row = row;
31905 last_overlapping_row = row;
31909 if (y1 >= yb)
31910 break;
31913 /* Display the mode line if there is one. */
31914 if (window_wants_mode_line (w)
31915 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31916 row->enabled_p)
31917 && row->y < r_bottom)
31919 if (expose_line (w, row, &r))
31920 mouse_face_overwritten_p = true;
31923 if (!w->pseudo_window_p)
31925 /* Fix the display of overlapping rows. */
31926 if (first_overlapping_row)
31927 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31928 fr);
31930 /* Draw border between windows. */
31931 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31932 x_draw_right_divider (w);
31933 else
31934 x_draw_vertical_border (w);
31936 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31937 x_draw_bottom_divider (w);
31939 /* Turn the cursor on again. */
31940 if (cursor_cleared_p
31941 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31942 update_window_cursor (w, true);
31946 return mouse_face_overwritten_p;
31951 /* Redraw (parts) of all windows in the window tree rooted at W that
31952 intersect R. R contains frame pixel coordinates. Value is
31953 true if the exposure overwrites mouse-face. */
31955 static bool
31956 expose_window_tree (struct window *w, XRectangle *r)
31958 struct frame *f = XFRAME (w->frame);
31959 bool mouse_face_overwritten_p = false;
31961 while (w && !FRAME_GARBAGED_P (f))
31963 mouse_face_overwritten_p
31964 |= (WINDOWP (w->contents)
31965 ? expose_window_tree (XWINDOW (w->contents), r)
31966 : expose_window (w, r));
31968 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31971 return mouse_face_overwritten_p;
31975 /* EXPORT:
31976 Redisplay an exposed area of frame F. X and Y are the upper-left
31977 corner of the exposed rectangle. W and H are width and height of
31978 the exposed area. All are pixel values. W or H zero means redraw
31979 the entire frame. */
31981 void
31982 expose_frame (struct frame *f, int x, int y, int w, int h)
31984 XRectangle r;
31985 bool mouse_face_overwritten_p = false;
31987 TRACE ((stderr, "expose_frame "));
31989 /* No need to redraw if frame will be redrawn soon. */
31990 if (FRAME_GARBAGED_P (f))
31992 TRACE ((stderr, " garbaged\n"));
31993 return;
31996 /* If basic faces haven't been realized yet, there is no point in
31997 trying to redraw anything. This can happen when we get an expose
31998 event while Emacs is starting, e.g. by moving another window. */
31999 if (FRAME_FACE_CACHE (f) == NULL
32000 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32002 TRACE ((stderr, " no faces\n"));
32003 return;
32006 if (w == 0 || h == 0)
32008 r.x = r.y = 0;
32009 r.width = FRAME_TEXT_WIDTH (f);
32010 r.height = FRAME_TEXT_HEIGHT (f);
32012 else
32014 r.x = x;
32015 r.y = y;
32016 r.width = w;
32017 r.height = h;
32020 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32021 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32023 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32024 if (WINDOWP (f->tool_bar_window))
32025 mouse_face_overwritten_p
32026 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32027 #endif
32029 #ifdef HAVE_X_WINDOWS
32030 #ifndef MSDOS
32031 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32032 if (WINDOWP (f->menu_bar_window))
32033 mouse_face_overwritten_p
32034 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32035 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32036 #endif
32037 #endif
32039 /* Some window managers support a focus-follows-mouse style with
32040 delayed raising of frames. Imagine a partially obscured frame,
32041 and moving the mouse into partially obscured mouse-face on that
32042 frame. The visible part of the mouse-face will be highlighted,
32043 then the WM raises the obscured frame. With at least one WM, KDE
32044 2.1, Emacs is not getting any event for the raising of the frame
32045 (even tried with SubstructureRedirectMask), only Expose events.
32046 These expose events will draw text normally, i.e. not
32047 highlighted. Which means we must redo the highlight here.
32048 Subsume it under ``we love X''. --gerd 2001-08-15 */
32049 /* Included in Windows version because Windows most likely does not
32050 do the right thing if any third party tool offers
32051 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32052 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32054 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32055 if (f == hlinfo->mouse_face_mouse_frame)
32057 int mouse_x = hlinfo->mouse_face_mouse_x;
32058 int mouse_y = hlinfo->mouse_face_mouse_y;
32059 clear_mouse_face (hlinfo);
32060 note_mouse_highlight (f, mouse_x, mouse_y);
32066 /* EXPORT:
32067 Determine the intersection of two rectangles R1 and R2. Return
32068 the intersection in *RESULT. Value is true if RESULT is not
32069 empty. */
32071 bool
32072 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32074 XRectangle *left, *right;
32075 XRectangle *upper, *lower;
32076 bool intersection_p = false;
32078 /* Rearrange so that R1 is the left-most rectangle. */
32079 if (r1->x < r2->x)
32080 left = r1, right = r2;
32081 else
32082 left = r2, right = r1;
32084 /* X0 of the intersection is right.x0, if this is inside R1,
32085 otherwise there is no intersection. */
32086 if (right->x <= left->x + left->width)
32088 result->x = right->x;
32090 /* The right end of the intersection is the minimum of
32091 the right ends of left and right. */
32092 result->width = (min (left->x + left->width, right->x + right->width)
32093 - result->x);
32095 /* Same game for Y. */
32096 if (r1->y < r2->y)
32097 upper = r1, lower = r2;
32098 else
32099 upper = r2, lower = r1;
32101 /* The upper end of the intersection is lower.y0, if this is inside
32102 of upper. Otherwise, there is no intersection. */
32103 if (lower->y <= upper->y + upper->height)
32105 result->y = lower->y;
32107 /* The lower end of the intersection is the minimum of the lower
32108 ends of upper and lower. */
32109 result->height = (min (lower->y + lower->height,
32110 upper->y + upper->height)
32111 - result->y);
32112 intersection_p = true;
32116 return intersection_p;
32119 #endif /* HAVE_WINDOW_SYSTEM */
32122 /***********************************************************************
32123 Initialization
32124 ***********************************************************************/
32126 void
32127 syms_of_xdisp (void)
32129 Vwith_echo_area_save_vector = Qnil;
32130 staticpro (&Vwith_echo_area_save_vector);
32132 Vmessage_stack = Qnil;
32133 staticpro (&Vmessage_stack);
32135 /* Non-nil means don't actually do any redisplay. */
32136 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32138 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32140 DEFVAR_BOOL("inhibit-message", inhibit_message,
32141 doc: /* Non-nil means calls to `message' are not displayed.
32142 They are still logged to the *Messages* buffer. */);
32143 inhibit_message = 0;
32145 message_dolog_marker1 = Fmake_marker ();
32146 staticpro (&message_dolog_marker1);
32147 message_dolog_marker2 = Fmake_marker ();
32148 staticpro (&message_dolog_marker2);
32149 message_dolog_marker3 = Fmake_marker ();
32150 staticpro (&message_dolog_marker3);
32152 defsubr (&Sset_buffer_redisplay);
32153 #ifdef GLYPH_DEBUG
32154 defsubr (&Sdump_frame_glyph_matrix);
32155 defsubr (&Sdump_glyph_matrix);
32156 defsubr (&Sdump_glyph_row);
32157 defsubr (&Sdump_tool_bar_row);
32158 defsubr (&Strace_redisplay);
32159 defsubr (&Strace_to_stderr);
32160 #endif
32161 #ifdef HAVE_WINDOW_SYSTEM
32162 defsubr (&Stool_bar_height);
32163 defsubr (&Slookup_image_map);
32164 #endif
32165 defsubr (&Sline_pixel_height);
32166 defsubr (&Sformat_mode_line);
32167 defsubr (&Sinvisible_p);
32168 defsubr (&Scurrent_bidi_paragraph_direction);
32169 defsubr (&Swindow_text_pixel_size);
32170 defsubr (&Smove_point_visually);
32171 defsubr (&Sbidi_find_overridden_directionality);
32173 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32174 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32175 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32176 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32177 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32178 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32179 DEFSYM (Qeval, "eval");
32180 DEFSYM (QCdata, ":data");
32182 /* Names of text properties relevant for redisplay. */
32183 DEFSYM (Qdisplay, "display");
32184 DEFSYM (Qspace_width, "space-width");
32185 DEFSYM (Qraise, "raise");
32186 DEFSYM (Qslice, "slice");
32187 DEFSYM (Qspace, "space");
32188 DEFSYM (Qmargin, "margin");
32189 DEFSYM (Qpointer, "pointer");
32190 DEFSYM (Qleft_margin, "left-margin");
32191 DEFSYM (Qright_margin, "right-margin");
32192 DEFSYM (Qcenter, "center");
32193 DEFSYM (Qline_height, "line-height");
32194 DEFSYM (QCalign_to, ":align-to");
32195 DEFSYM (QCrelative_width, ":relative-width");
32196 DEFSYM (QCrelative_height, ":relative-height");
32197 DEFSYM (QCeval, ":eval");
32198 DEFSYM (QCpropertize, ":propertize");
32199 DEFSYM (QCfile, ":file");
32200 DEFSYM (Qfontified, "fontified");
32201 DEFSYM (Qfontification_functions, "fontification-functions");
32203 /* Name of the face used to highlight trailing whitespace. */
32204 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32206 /* Names of the faces used to display line numbers. */
32207 DEFSYM (Qline_number, "line-number");
32208 DEFSYM (Qline_number_current_line, "line-number-current-line");
32209 /* Name of a text property which disables line-number display. */
32210 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32212 /* Name and number of the face used to highlight escape glyphs. */
32213 DEFSYM (Qescape_glyph, "escape-glyph");
32215 /* Name and number of the face used to highlight non-breaking
32216 spaces/hyphens. */
32217 DEFSYM (Qnobreak_space, "nobreak-space");
32218 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32220 /* The symbol 'image' which is the car of the lists used to represent
32221 images in Lisp. Also a tool bar style. */
32222 DEFSYM (Qimage, "image");
32224 /* Tool bar styles. */
32225 DEFSYM (Qtext, "text");
32226 DEFSYM (Qboth, "both");
32227 DEFSYM (Qboth_horiz, "both-horiz");
32228 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32230 /* The image map types. */
32231 DEFSYM (QCmap, ":map");
32232 DEFSYM (QCpointer, ":pointer");
32233 DEFSYM (Qrect, "rect");
32234 DEFSYM (Qcircle, "circle");
32235 DEFSYM (Qpoly, "poly");
32237 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32239 DEFSYM (Qgrow_only, "grow-only");
32240 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32241 DEFSYM (Qposition, "position");
32242 DEFSYM (Qbuffer_position, "buffer-position");
32243 DEFSYM (Qobject, "object");
32245 /* Cursor shapes. */
32246 DEFSYM (Qbar, "bar");
32247 DEFSYM (Qhbar, "hbar");
32248 DEFSYM (Qbox, "box");
32249 DEFSYM (Qhollow, "hollow");
32251 /* Pointer shapes. */
32252 DEFSYM (Qhand, "hand");
32253 DEFSYM (Qarrow, "arrow");
32254 /* also Qtext */
32256 DEFSYM (Qdragging, "dragging");
32258 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32260 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32261 staticpro (&list_of_error);
32263 /* Values of those variables at last redisplay are stored as
32264 properties on 'overlay-arrow-position' symbol. However, if
32265 Voverlay_arrow_position is a marker, last-arrow-position is its
32266 numerical position. */
32267 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32268 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32270 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32271 properties on a symbol in overlay-arrow-variable-list. */
32272 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32273 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32275 echo_buffer[0] = echo_buffer[1] = Qnil;
32276 staticpro (&echo_buffer[0]);
32277 staticpro (&echo_buffer[1]);
32279 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32280 staticpro (&echo_area_buffer[0]);
32281 staticpro (&echo_area_buffer[1]);
32283 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32284 staticpro (&Vmessages_buffer_name);
32286 mode_line_proptrans_alist = Qnil;
32287 staticpro (&mode_line_proptrans_alist);
32288 mode_line_string_list = Qnil;
32289 staticpro (&mode_line_string_list);
32290 mode_line_string_face = Qnil;
32291 staticpro (&mode_line_string_face);
32292 mode_line_string_face_prop = Qnil;
32293 staticpro (&mode_line_string_face_prop);
32294 Vmode_line_unwind_vector = Qnil;
32295 staticpro (&Vmode_line_unwind_vector);
32297 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32299 help_echo_string = Qnil;
32300 staticpro (&help_echo_string);
32301 help_echo_object = Qnil;
32302 staticpro (&help_echo_object);
32303 help_echo_window = Qnil;
32304 staticpro (&help_echo_window);
32305 previous_help_echo_string = Qnil;
32306 staticpro (&previous_help_echo_string);
32307 help_echo_pos = -1;
32309 DEFSYM (Qright_to_left, "right-to-left");
32310 DEFSYM (Qleft_to_right, "left-to-right");
32311 defsubr (&Sbidi_resolved_levels);
32313 #ifdef HAVE_WINDOW_SYSTEM
32314 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32315 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32316 For example, if a block cursor is over a tab, it will be drawn as
32317 wide as that tab on the display. */);
32318 x_stretch_cursor_p = 0;
32319 #endif
32321 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32322 doc: /* Non-nil means highlight trailing whitespace.
32323 The face used for trailing whitespace is `trailing-whitespace'. */);
32324 Vshow_trailing_whitespace = Qnil;
32326 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32327 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32328 If the value is t, Emacs highlights non-ASCII chars which have the
32329 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32330 or `nobreak-hyphen' face respectively.
32332 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32333 U+2011 (non-breaking hyphen) are affected.
32335 Any other non-nil value means to display these characters as a escape
32336 glyph followed by an ordinary space or hyphen.
32338 A value of nil means no special handling of these characters. */);
32339 Vnobreak_char_display = Qt;
32341 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32342 doc: /* The pointer shape to show in void text areas.
32343 A value of nil means to show the text pointer. Other options are
32344 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32345 `hourglass'. */);
32346 Vvoid_text_area_pointer = Qarrow;
32348 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32349 doc: /* Non-nil means don't actually do any redisplay.
32350 This is used for internal purposes. */);
32351 Vinhibit_redisplay = Qnil;
32353 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32354 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32355 Vglobal_mode_string = Qnil;
32357 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32358 doc: /* Marker for where to display an arrow on top of the buffer text.
32359 This must be the beginning of a line in order to work.
32360 See also `overlay-arrow-string'. */);
32361 Voverlay_arrow_position = Qnil;
32363 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32364 doc: /* String to display as an arrow in non-window frames.
32365 See also `overlay-arrow-position'. */);
32366 Voverlay_arrow_string = build_pure_c_string ("=>");
32368 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32369 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32370 The symbols on this list are examined during redisplay to determine
32371 where to display overlay arrows. */);
32372 Voverlay_arrow_variable_list
32373 = list1 (intern_c_string ("overlay-arrow-position"));
32375 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32376 doc: /* The number of lines to try scrolling a window by when point moves out.
32377 If that fails to bring point back on frame, point is centered instead.
32378 If this is zero, point is always centered after it moves off frame.
32379 If you want scrolling to always be a line at a time, you should set
32380 `scroll-conservatively' to a large value rather than set this to 1. */);
32382 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32383 doc: /* Scroll up to this many lines, to bring point back on screen.
32384 If point moves off-screen, redisplay will scroll by up to
32385 `scroll-conservatively' lines in order to bring point just barely
32386 onto the screen again. If that cannot be done, then redisplay
32387 recenters point as usual.
32389 If the value is greater than 100, redisplay will never recenter point,
32390 but will always scroll just enough text to bring point into view, even
32391 if you move far away.
32393 A value of zero means always recenter point if it moves off screen. */);
32394 scroll_conservatively = 0;
32396 DEFVAR_INT ("scroll-margin", scroll_margin,
32397 doc: /* Number of lines of margin at the top and bottom of a window.
32398 Recenter the window whenever point gets within this many lines
32399 of the top or bottom of the window. */);
32400 scroll_margin = 0;
32402 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32403 doc: /* Maximum effective value of `scroll-margin'.
32404 Given as a fraction of the current window's lines. The value should
32405 be a floating point number between 0.0 and 0.5. The effective maximum
32406 is limited to (/ (1- window-lines) 2). Non-float values for this
32407 variable are ignored and the default 0.25 is used instead. */);
32408 Vmaximum_scroll_margin = make_float (0.25);
32410 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32411 doc: /* Pixels per inch value for non-window system displays.
32412 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32413 Vdisplay_pixels_per_inch = make_float (72.0);
32415 #ifdef GLYPH_DEBUG
32416 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32417 #endif
32419 DEFVAR_LISP ("truncate-partial-width-windows",
32420 Vtruncate_partial_width_windows,
32421 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32422 For an integer value, truncate lines in each window narrower than the
32423 full frame width, provided the total window width in column units is less
32424 than that integer; otherwise, respect the value of `truncate-lines'.
32425 The total width of the window is as returned by `window-total-width', it
32426 includes the fringes, the continuation and truncation glyphs, the
32427 display margins (if any), and the scroll bar
32429 For any other non-nil value, truncate lines in all windows that do
32430 not span the full frame width.
32432 A value of nil means to respect the value of `truncate-lines'.
32434 If `word-wrap' is enabled, you might want to reduce this. */);
32435 Vtruncate_partial_width_windows = make_number (50);
32437 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32438 doc: /* Maximum buffer size for which line number should be displayed.
32439 If the buffer is bigger than this, the line number does not appear
32440 in the mode line. A value of nil means no limit. */);
32441 Vline_number_display_limit = Qnil;
32443 DEFVAR_INT ("line-number-display-limit-width",
32444 line_number_display_limit_width,
32445 doc: /* Maximum line width (in characters) for line number display.
32446 If the average length of the lines near point is bigger than this, then the
32447 line number may be omitted from the mode line. */);
32448 line_number_display_limit_width = 200;
32450 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32451 doc: /* Non-nil means highlight region even in nonselected windows. */);
32452 highlight_nonselected_windows = false;
32454 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32455 doc: /* Non-nil if more than one frame is visible on this display.
32456 Minibuffer-only frames don't count, but iconified frames do.
32457 This variable is not guaranteed to be accurate except while processing
32458 `frame-title-format' and `icon-title-format'. */);
32460 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32461 doc: /* Template for displaying the title bar of visible frames.
32462 \(Assuming the window manager supports this feature.)
32464 This variable has the same structure as `mode-line-format', except that
32465 the %c, %C, and %l constructs are ignored. It is used only on frames for
32466 which no explicit name has been set (see `modify-frame-parameters'). */);
32468 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32469 doc: /* Template for displaying the title bar of an iconified frame.
32470 \(Assuming the window manager supports this feature.)
32471 This variable has the same structure as `mode-line-format' (which see),
32472 and is used only on frames for which no explicit name has been set
32473 \(see `modify-frame-parameters'). */);
32474 Vicon_title_format
32475 = Vframe_title_format
32476 = listn (CONSTYPE_PURE, 3,
32477 intern_c_string ("multiple-frames"),
32478 build_pure_c_string ("%b"),
32479 listn (CONSTYPE_PURE, 4,
32480 empty_unibyte_string,
32481 intern_c_string ("invocation-name"),
32482 build_pure_c_string ("@"),
32483 intern_c_string ("system-name")));
32485 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32486 doc: /* Maximum number of lines to keep in the message log buffer.
32487 If nil, disable message logging. If t, log messages but don't truncate
32488 the buffer when it becomes large. */);
32489 Vmessage_log_max = make_number (1000);
32491 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32492 doc: /* List of functions to call before redisplaying a window with scrolling.
32493 Each function is called with two arguments, the window and its new
32494 display-start position.
32495 These functions are called whenever the `window-start' marker is modified,
32496 either to point into another buffer (e.g. via `set-window-buffer') or another
32497 place in the same buffer.
32498 Note that the value of `window-end' is not valid when these functions are
32499 called.
32501 Warning: Do not use this feature to alter the way the window
32502 is scrolled. It is not designed for that, and such use probably won't
32503 work. */);
32504 Vwindow_scroll_functions = Qnil;
32506 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32507 doc: /* Functions called when redisplay of a window reaches the end trigger.
32508 Each function is called with two arguments, the window and the end trigger value.
32509 See `set-window-redisplay-end-trigger'. */);
32510 Vredisplay_end_trigger_functions = Qnil;
32512 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32513 doc: /* Non-nil means autoselect window with mouse pointer.
32514 If nil, do not autoselect windows.
32515 A positive number means delay autoselection by that many seconds: a
32516 window is autoselected only after the mouse has remained in that
32517 window for the duration of the delay.
32518 A negative number has a similar effect, but causes windows to be
32519 autoselected only after the mouse has stopped moving. (Because of
32520 the way Emacs compares mouse events, you will occasionally wait twice
32521 that time before the window gets selected.)
32522 Any other value means to autoselect window instantaneously when the
32523 mouse pointer enters it.
32525 Autoselection selects the minibuffer only if it is active, and never
32526 unselects the minibuffer if it is active.
32528 When customizing this variable make sure that the actual value of
32529 `focus-follows-mouse' matches the behavior of your window manager. */);
32530 Vmouse_autoselect_window = Qnil;
32532 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32533 doc: /* Non-nil means automatically resize tool-bars.
32534 This dynamically changes the tool-bar's height to the minimum height
32535 that is needed to make all tool-bar items visible.
32536 If value is `grow-only', the tool-bar's height is only increased
32537 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32538 Vauto_resize_tool_bars = Qt;
32540 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32541 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32542 auto_raise_tool_bar_buttons_p = true;
32544 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32545 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32546 make_cursor_line_fully_visible_p = true;
32548 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32549 doc: /* Border below tool-bar in pixels.
32550 If an integer, use it as the height of the border.
32551 If it is one of `internal-border-width' or `border-width', use the
32552 value of the corresponding frame parameter.
32553 Otherwise, no border is added below the tool-bar. */);
32554 Vtool_bar_border = Qinternal_border_width;
32556 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32557 doc: /* Margin around tool-bar buttons in pixels.
32558 If an integer, use that for both horizontal and vertical margins.
32559 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32560 HORZ specifying the horizontal margin, and VERT specifying the
32561 vertical margin. */);
32562 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32564 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32565 doc: /* Relief thickness of tool-bar buttons. */);
32566 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32568 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32569 doc: /* Tool bar style to use.
32570 It can be one of
32571 image - show images only
32572 text - show text only
32573 both - show both, text below image
32574 both-horiz - show text to the right of the image
32575 text-image-horiz - show text to the left of the image
32576 any other - use system default or image if no system default.
32578 This variable only affects the GTK+ toolkit version of Emacs. */);
32579 Vtool_bar_style = Qnil;
32581 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32582 doc: /* Maximum number of characters a label can have to be shown.
32583 The tool bar style must also show labels for this to have any effect, see
32584 `tool-bar-style'. */);
32585 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32587 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32588 doc: /* List of functions to call to fontify regions of text.
32589 Each function is called with one argument POS. Functions must
32590 fontify a region starting at POS in the current buffer, and give
32591 fontified regions the property `fontified'. */);
32592 Vfontification_functions = Qnil;
32593 Fmake_variable_buffer_local (Qfontification_functions);
32595 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32596 unibyte_display_via_language_environment,
32597 doc: /* Non-nil means display unibyte text according to language environment.
32598 Specifically, this means that raw bytes in the range 160-255 decimal
32599 are displayed by converting them to the equivalent multibyte characters
32600 according to the current language environment. As a result, they are
32601 displayed according to the current fontset.
32603 Note that this variable affects only how these bytes are displayed,
32604 but does not change the fact they are interpreted as raw bytes. */);
32605 unibyte_display_via_language_environment = false;
32607 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32608 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32609 If a float, it specifies a fraction of the mini-window frame's height.
32610 If an integer, it specifies a number of lines. */);
32611 Vmax_mini_window_height = make_float (0.25);
32613 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32614 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32615 A value of nil means don't automatically resize mini-windows.
32616 A value of t means resize them to fit the text displayed in them.
32617 A value of `grow-only', the default, means let mini-windows grow only;
32618 they return to their normal size when the minibuffer is closed, or the
32619 echo area becomes empty. */);
32620 /* Contrary to the doc string, we initialize this to nil, so that
32621 loading loadup.el won't try to resize windows before loading
32622 window.el, where some functions we need to call for this live.
32623 We assign the 'grow-only' value right after loading window.el
32624 during loadup. */
32625 Vresize_mini_windows = Qnil;
32627 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32628 doc: /* Alist specifying how to blink the cursor off.
32629 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32630 `cursor-type' frame-parameter or variable equals ON-STATE,
32631 comparing using `equal', Emacs uses OFF-STATE to specify
32632 how to blink it off. ON-STATE and OFF-STATE are values for
32633 the `cursor-type' frame parameter.
32635 If a frame's ON-STATE has no entry in this list,
32636 the frame's other specifications determine how to blink the cursor off. */);
32637 Vblink_cursor_alist = Qnil;
32639 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32640 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32641 The value `current-line' means the line displaying point in each window
32642 is automatically scrolled horizontally to make point visible.
32643 Any other non-nil value means all the lines in a window are automatically
32644 scrolled horizontally to make point visible. */);
32645 automatic_hscrolling = Qt;
32646 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32647 DEFSYM (Qcurrent_line, "current-line");
32649 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32650 doc: /* How many columns away from the window edge point is allowed to get
32651 before automatic hscrolling will horizontally scroll the window. */);
32652 hscroll_margin = 5;
32654 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32655 doc: /* How many columns to scroll the window when point gets too close to the edge.
32656 When point is less than `hscroll-margin' columns from the window
32657 edge, automatic hscrolling will scroll the window by the amount of columns
32658 determined by this variable. If its value is a positive integer, scroll that
32659 many columns. If it's a positive floating-point number, it specifies the
32660 fraction of the window's width to scroll. If it's nil or zero, point will be
32661 centered horizontally after the scroll. Any other value, including negative
32662 numbers, are treated as if the value were zero.
32664 Automatic hscrolling always moves point outside the scroll margin, so if
32665 point was more than scroll step columns inside the margin, the window will
32666 scroll more than the value given by the scroll step.
32668 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32669 and `scroll-right' overrides this variable's effect. */);
32670 Vhscroll_step = make_number (0);
32672 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32673 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32674 Bind this around calls to `message' to let it take effect. */);
32675 message_truncate_lines = false;
32677 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32678 doc: /* Normal hook run to update the menu bar definitions.
32679 Redisplay runs this hook before it redisplays the menu bar.
32680 This is used to update menus such as Buffers, whose contents depend on
32681 various data. */);
32682 Vmenu_bar_update_hook = Qnil;
32684 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32685 doc: /* Frame for which we are updating a menu.
32686 The enable predicate for a menu binding should check this variable. */);
32687 Vmenu_updating_frame = Qnil;
32689 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32690 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32691 inhibit_menubar_update = false;
32693 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32694 doc: /* Prefix prepended to all continuation lines at display time.
32695 The value may be a string, an image, or a stretch-glyph; it is
32696 interpreted in the same way as the value of a `display' text property.
32698 This variable is overridden by any `wrap-prefix' text or overlay
32699 property.
32701 To add a prefix to non-continuation lines, use `line-prefix'. */);
32702 Vwrap_prefix = Qnil;
32703 DEFSYM (Qwrap_prefix, "wrap-prefix");
32704 Fmake_variable_buffer_local (Qwrap_prefix);
32706 DEFVAR_LISP ("line-prefix", Vline_prefix,
32707 doc: /* Prefix prepended to all non-continuation lines at display time.
32708 The value may be a string, an image, or a stretch-glyph; it is
32709 interpreted in the same way as the value of a `display' text property.
32711 This variable is overridden by any `line-prefix' text or overlay
32712 property.
32714 To add a prefix to continuation lines, use `wrap-prefix'. */);
32715 Vline_prefix = Qnil;
32716 DEFSYM (Qline_prefix, "line-prefix");
32717 Fmake_variable_buffer_local (Qline_prefix);
32719 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32720 doc: /* Non-nil means display line numbers.
32721 If the value is t, display the absolute number of each line of a buffer
32722 shown in a window. Absolute line numbers count from the beginning of
32723 the current narrowing, or from buffer beginning. If the value is
32724 `relative', display for each line not containing the window's point its
32725 relative number instead, i.e. the number of the line relative to the
32726 line showing the window's point.
32728 In either case, line numbers are displayed at the beginning of each
32729 non-continuation line that displays buffer text, i.e. after each newline
32730 character that comes from the buffer. The value `visual' is like
32731 `relative' but counts screen lines instead of buffer lines. In practice
32732 this means that continuation lines count as well when calculating the
32733 relative number of a line.
32735 Lisp programs can disable display of a line number of a particular
32736 buffer line by putting the `display-line-numbers-disable' text property
32737 or overlay property on the first visible character of that line. */);
32738 Vdisplay_line_numbers = Qnil;
32739 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32740 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32741 DEFSYM (Qrelative, "relative");
32742 DEFSYM (Qvisual, "visual");
32744 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32745 doc: /* Minimum width of space reserved for line number display.
32746 A positive number means reserve that many columns for line numbers,
32747 even if the actual number needs less space.
32748 The default value of nil means compute the space dynamically.
32749 Any other value is treated as nil. */);
32750 Vdisplay_line_numbers_width = Qnil;
32751 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32752 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32754 DEFVAR_LISP ("display-line-numbers-current-absolute",
32755 Vdisplay_line_numbers_current_absolute,
32756 doc: /* Non-nil means display absolute number of current line.
32757 This variable has effect only when `display-line-numbers' is
32758 either `relative' or `visual'. */);
32759 Vdisplay_line_numbers_current_absolute = Qt;
32761 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32762 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32763 display_line_numbers_widen = false;
32764 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32765 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32767 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32768 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32769 inhibit_eval_during_redisplay = false;
32771 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32772 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32773 inhibit_free_realized_faces = false;
32775 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32776 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32777 Intended for use during debugging and for testing bidi display;
32778 see biditest.el in the test suite. */);
32779 inhibit_bidi_mirroring = false;
32781 #ifdef GLYPH_DEBUG
32782 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32783 doc: /* Inhibit try_window_id display optimization. */);
32784 inhibit_try_window_id = false;
32786 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32787 doc: /* Inhibit try_window_reusing display optimization. */);
32788 inhibit_try_window_reusing = false;
32790 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32791 doc: /* Inhibit try_cursor_movement display optimization. */);
32792 inhibit_try_cursor_movement = false;
32793 #endif /* GLYPH_DEBUG */
32795 DEFVAR_INT ("overline-margin", overline_margin,
32796 doc: /* Space between overline and text, in pixels.
32797 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32798 margin to the character height. */);
32799 overline_margin = 2;
32801 DEFVAR_INT ("underline-minimum-offset",
32802 underline_minimum_offset,
32803 doc: /* Minimum distance between baseline and underline.
32804 This can improve legibility of underlined text at small font sizes,
32805 particularly when using variable `x-use-underline-position-properties'
32806 with fonts that specify an UNDERLINE_POSITION relatively close to the
32807 baseline. The default value is 1. */);
32808 underline_minimum_offset = 1;
32810 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32811 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32812 This feature only works when on a window system that can change
32813 cursor shapes. */);
32814 display_hourglass_p = true;
32816 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32817 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32818 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32820 #ifdef HAVE_WINDOW_SYSTEM
32821 hourglass_atimer = NULL;
32822 hourglass_shown_p = false;
32823 #endif /* HAVE_WINDOW_SYSTEM */
32825 /* Name of the face used to display glyphless characters. */
32826 DEFSYM (Qglyphless_char, "glyphless-char");
32828 /* Method symbols for Vglyphless_char_display. */
32829 DEFSYM (Qhex_code, "hex-code");
32830 DEFSYM (Qempty_box, "empty-box");
32831 DEFSYM (Qthin_space, "thin-space");
32832 DEFSYM (Qzero_width, "zero-width");
32834 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32835 doc: /* Function run just before redisplay.
32836 It is called with one argument, which is the set of windows that are to
32837 be redisplayed. This set can be nil (meaning, only the selected window),
32838 or t (meaning all windows). */);
32839 Vpre_redisplay_function = intern ("ignore");
32841 /* Symbol for the purpose of Vglyphless_char_display. */
32842 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32843 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32845 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32846 doc: /* Char-table defining glyphless characters.
32847 Each element, if non-nil, should be one of the following:
32848 an ASCII acronym string: display this string in a box
32849 `hex-code': display the hexadecimal code of a character in a box
32850 `empty-box': display as an empty box
32851 `thin-space': display as 1-pixel width space
32852 `zero-width': don't display
32853 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32854 display method for graphical terminals and text terminals respectively.
32855 GRAPHICAL and TEXT should each have one of the values listed above.
32857 The char-table has one extra slot to control the display of a character for
32858 which no font is found. This slot only takes effect on graphical terminals.
32859 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32860 `thin-space'. The default is `empty-box'.
32862 If a character has a non-nil entry in an active display table, the
32863 display table takes effect; in this case, Emacs does not consult
32864 `glyphless-char-display' at all. */);
32865 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32866 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32867 Qempty_box);
32869 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32870 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32871 Vdebug_on_message = Qnil;
32873 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32874 doc: /* */);
32875 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32877 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32878 doc: /* */);
32879 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32881 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32882 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32883 /* Initialize to t, since we need to disable reordering until
32884 loadup.el successfully loads charprop.el. */
32885 redisplay__inhibit_bidi = true;
32887 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32888 doc: /* Non-nil means display raw bytes in hexadecimal format.
32889 The default is to use octal format (\200) whereas hexadecimal (\x80)
32890 may be more familiar to users. */);
32891 display_raw_bytes_as_hex = false;
32896 /* Initialize this module when Emacs starts. */
32898 void
32899 init_xdisp (void)
32901 CHARPOS (this_line_start_pos) = 0;
32903 if (!noninteractive)
32905 struct window *m = XWINDOW (minibuf_window);
32906 Lisp_Object frame = m->frame;
32907 struct frame *f = XFRAME (frame);
32908 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32909 struct window *r = XWINDOW (root);
32910 int i;
32912 echo_area_window = minibuf_window;
32914 r->top_line = FRAME_TOP_MARGIN (f);
32915 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32916 r->total_cols = FRAME_COLS (f);
32917 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32918 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32919 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32921 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32922 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32923 m->total_cols = FRAME_COLS (f);
32924 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32925 m->total_lines = 1;
32926 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32928 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32929 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32930 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32932 /* The default ellipsis glyphs `...'. */
32933 for (i = 0; i < 3; ++i)
32934 default_invis_vector[i] = make_number ('.');
32938 /* Allocate the buffer for frame titles.
32939 Also used for `format-mode-line'. */
32940 int size = 100;
32941 mode_line_noprop_buf = xmalloc (size);
32942 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32943 mode_line_noprop_ptr = mode_line_noprop_buf;
32944 mode_line_target = MODE_LINE_DISPLAY;
32947 help_echo_showing_p = false;
32950 #ifdef HAVE_WINDOW_SYSTEM
32952 /* Platform-independent portion of hourglass implementation. */
32954 /* Timer function of hourglass_atimer. */
32956 static void
32957 show_hourglass (struct atimer *timer)
32959 /* The timer implementation will cancel this timer automatically
32960 after this function has run. Set hourglass_atimer to null
32961 so that we know the timer doesn't have to be canceled. */
32962 hourglass_atimer = NULL;
32964 if (!hourglass_shown_p)
32966 Lisp_Object tail, frame;
32968 block_input ();
32970 FOR_EACH_FRAME (tail, frame)
32972 struct frame *f = XFRAME (frame);
32974 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32975 && FRAME_RIF (f)->show_hourglass)
32976 FRAME_RIF (f)->show_hourglass (f);
32979 hourglass_shown_p = true;
32980 unblock_input ();
32984 /* Cancel a currently active hourglass timer, and start a new one. */
32986 void
32987 start_hourglass (void)
32989 struct timespec delay;
32991 cancel_hourglass ();
32993 if (INTEGERP (Vhourglass_delay)
32994 && XINT (Vhourglass_delay) > 0)
32995 delay = make_timespec (min (XINT (Vhourglass_delay),
32996 TYPE_MAXIMUM (time_t)),
32998 else if (FLOATP (Vhourglass_delay)
32999 && XFLOAT_DATA (Vhourglass_delay) > 0)
33000 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33001 else
33002 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33004 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33005 show_hourglass, NULL);
33008 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33009 shown. */
33011 void
33012 cancel_hourglass (void)
33014 if (hourglass_atimer)
33016 cancel_atimer (hourglass_atimer);
33017 hourglass_atimer = NULL;
33020 if (hourglass_shown_p)
33022 Lisp_Object tail, frame;
33024 block_input ();
33026 FOR_EACH_FRAME (tail, frame)
33028 struct frame *f = XFRAME (frame);
33030 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33031 && FRAME_RIF (f)->hide_hourglass)
33032 FRAME_RIF (f)->hide_hourglass (f);
33033 #ifdef HAVE_NTGUI
33034 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33035 else if (!FRAME_W32_P (f))
33036 w32_arrow_cursor ();
33037 #endif
33040 hourglass_shown_p = false;
33041 unblock_input ();
33045 #endif /* HAVE_WINDOW_SYSTEM */