Initial version of native display of line numbers
[emacs.git] / src / xdisp.c
blobdcef242966ee4fa127e01fa4509f69a66e09e55a
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 bool display_line (struct it *, int);
837 static int display_mode_lines (struct window *);
838 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
839 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
840 Lisp_Object, bool);
841 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
842 Lisp_Object);
843 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
844 static void display_menu_bar (struct window *);
845 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
846 ptrdiff_t *);
847 static void pint2str (register char *, register int, register ptrdiff_t);
849 static int display_string (const char *, Lisp_Object, Lisp_Object,
850 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
851 static void compute_line_metrics (struct it *);
852 static void run_redisplay_end_trigger_hook (struct it *);
853 static bool get_overlay_strings (struct it *, ptrdiff_t);
854 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
855 static void next_overlay_string (struct it *);
856 static void reseat (struct it *, struct text_pos, bool);
857 static void reseat_1 (struct it *, struct text_pos, bool);
858 static bool next_element_from_display_vector (struct it *);
859 static bool next_element_from_string (struct it *);
860 static bool next_element_from_c_string (struct it *);
861 static bool next_element_from_buffer (struct it *);
862 static bool next_element_from_composition (struct it *);
863 static bool next_element_from_image (struct it *);
864 static bool next_element_from_stretch (struct it *);
865 static bool next_element_from_xwidget (struct it *);
866 static void load_overlay_strings (struct it *, ptrdiff_t);
867 static bool get_next_display_element (struct it *);
868 static enum move_it_result
869 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
870 enum move_operation_enum);
871 static void get_visually_first_element (struct it *);
872 static void compute_stop_pos (struct it *);
873 static int face_before_or_after_it_pos (struct it *, bool);
874 static ptrdiff_t next_overlay_change (ptrdiff_t);
875 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
876 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
877 static int handle_single_display_spec (struct it *, Lisp_Object,
878 Lisp_Object, Lisp_Object,
879 struct text_pos *, ptrdiff_t, int, bool);
880 static int underlying_face_id (struct it *);
882 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
883 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
885 #ifdef HAVE_WINDOW_SYSTEM
887 static void update_tool_bar (struct frame *, bool);
888 static void x_draw_bottom_divider (struct window *w);
889 static void notice_overwritten_cursor (struct window *,
890 enum glyph_row_area,
891 int, int, int, int);
892 static int normal_char_height (struct font *, int);
893 static void normal_char_ascent_descent (struct font *, int, int *, int *);
895 static void append_stretch_glyph (struct it *, Lisp_Object,
896 int, int, int);
898 static Lisp_Object get_it_property (struct it *, Lisp_Object);
899 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
900 struct font *, int, bool);
902 #endif /* HAVE_WINDOW_SYSTEM */
904 static void produce_special_glyphs (struct it *, enum display_element_type);
905 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
906 static bool coords_in_mouse_face_p (struct window *, int, int);
910 /***********************************************************************
911 Window display dimensions
912 ***********************************************************************/
914 /* Return the bottom boundary y-position for text lines in window W.
915 This is the first y position at which a line cannot start.
916 It is relative to the top of the window.
918 This is the height of W minus the height of a mode line, if any. */
921 window_text_bottom_y (struct window *w)
923 int height = WINDOW_PIXEL_HEIGHT (w);
925 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
927 if (WINDOW_WANTS_MODELINE_P (w))
928 height -= CURRENT_MODE_LINE_HEIGHT (w);
930 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
932 return height;
935 /* Return the pixel width of display area AREA of window W.
936 ANY_AREA means return the total width of W, not including
937 fringes to the left and right of the window. */
940 window_box_width (struct window *w, enum glyph_row_area area)
942 int width = w->pixel_width;
944 if (!w->pseudo_window_p)
946 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
947 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
949 if (area == TEXT_AREA)
950 width -= (WINDOW_MARGINS_WIDTH (w)
951 + WINDOW_FRINGES_WIDTH (w));
952 else if (area == LEFT_MARGIN_AREA)
953 width = WINDOW_LEFT_MARGIN_WIDTH (w);
954 else if (area == RIGHT_MARGIN_AREA)
955 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
958 /* With wide margins, fringes, etc. we might end up with a negative
959 width, correct that here. */
960 return max (0, width);
964 /* Return the pixel height of the display area of window W, not
965 including mode lines of W, if any. */
968 window_box_height (struct window *w)
970 struct frame *f = XFRAME (w->frame);
971 int height = WINDOW_PIXEL_HEIGHT (w);
973 eassert (height >= 0);
975 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
976 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
978 /* Note: the code below that determines the mode-line/header-line
979 height is essentially the same as that contained in the macro
980 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
981 the appropriate glyph row has its `mode_line_p' flag set,
982 and if it doesn't, uses estimate_mode_line_height instead. */
984 if (WINDOW_WANTS_MODELINE_P (w))
986 struct glyph_row *ml_row
987 = (w->current_matrix && w->current_matrix->rows
988 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
989 : 0);
990 if (ml_row && ml_row->mode_line_p)
991 height -= ml_row->height;
992 else
993 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
996 if (WINDOW_WANTS_HEADER_LINE_P (w))
998 struct glyph_row *hl_row
999 = (w->current_matrix && w->current_matrix->rows
1000 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1001 : 0);
1002 if (hl_row && hl_row->mode_line_p)
1003 height -= hl_row->height;
1004 else
1005 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1008 /* With a very small font and a mode-line that's taller than
1009 default, we might end up with a negative height. */
1010 return max (0, height);
1013 /* Return the window-relative coordinate of the left edge of display
1014 area AREA of window W. ANY_AREA means return the left edge of the
1015 whole window, to the right of the left fringe of W. */
1018 window_box_left_offset (struct window *w, enum glyph_row_area area)
1020 int x;
1022 if (w->pseudo_window_p)
1023 return 0;
1025 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1027 if (area == TEXT_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA));
1030 else if (area == RIGHT_MARGIN_AREA)
1031 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1032 + window_box_width (w, LEFT_MARGIN_AREA)
1033 + window_box_width (w, TEXT_AREA)
1034 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1036 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1037 else if (area == LEFT_MARGIN_AREA
1038 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1039 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1041 /* Don't return more than the window's pixel width. */
1042 return min (x, w->pixel_width);
1046 /* Return the window-relative coordinate of the right edge of display
1047 area AREA of window W. ANY_AREA means return the right edge of the
1048 whole window, to the left of the right fringe of W. */
1050 static int
1051 window_box_right_offset (struct window *w, enum glyph_row_area area)
1053 /* Don't return more than the window's pixel width. */
1054 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1055 w->pixel_width);
1058 /* Return the frame-relative coordinate of the left edge of display
1059 area AREA of window W. ANY_AREA means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1063 window_box_left (struct window *w, enum glyph_row_area area)
1065 struct frame *f = XFRAME (w->frame);
1066 int x;
1068 if (w->pseudo_window_p)
1069 return FRAME_INTERNAL_BORDER_WIDTH (f);
1071 x = (WINDOW_LEFT_EDGE_X (w)
1072 + window_box_left_offset (w, area));
1074 return x;
1078 /* Return the frame-relative coordinate of the right edge of display
1079 area AREA of window W. ANY_AREA means return the right edge of the
1080 whole window, to the left of the right fringe of W. */
1083 window_box_right (struct window *w, enum glyph_row_area area)
1085 return window_box_left (w, area) + window_box_width (w, area);
1088 /* Get the bounding box of the display area AREA of window W, without
1089 mode lines, in frame-relative coordinates. ANY_AREA means the
1090 whole window, not including the left and right fringes of
1091 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1092 coordinates of the upper-left corner of the box. Return in
1093 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1095 void
1096 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1097 int *box_y, int *box_width, int *box_height)
1099 if (box_width)
1100 *box_width = window_box_width (w, area);
1101 if (box_height)
1102 *box_height = window_box_height (w);
1103 if (box_x)
1104 *box_x = window_box_left (w, area);
1105 if (box_y)
1107 *box_y = WINDOW_TOP_EDGE_Y (w);
1108 if (WINDOW_WANTS_HEADER_LINE_P (w))
1109 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1113 #ifdef HAVE_WINDOW_SYSTEM
1115 /* Get the bounding box of the display area AREA of window W, without
1116 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1117 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1118 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1119 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1120 box. */
1122 static void
1123 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1124 int *bottom_right_x, int *bottom_right_y)
1126 window_box (w, ANY_AREA, top_left_x, top_left_y,
1127 bottom_right_x, bottom_right_y);
1128 *bottom_right_x += *top_left_x;
1129 *bottom_right_y += *top_left_y;
1132 #endif /* HAVE_WINDOW_SYSTEM */
1134 /***********************************************************************
1135 Utilities
1136 ***********************************************************************/
1138 /* Return the bottom y-position of the line the iterator IT is in.
1139 This can modify IT's settings. */
1142 line_bottom_y (struct it *it)
1144 int line_height = it->max_ascent + it->max_descent;
1145 int line_top_y = it->current_y;
1147 if (line_height == 0)
1149 if (last_height)
1150 line_height = last_height;
1151 else if (IT_CHARPOS (*it) < ZV)
1153 move_it_by_lines (it, 1);
1154 line_height = (it->max_ascent || it->max_descent
1155 ? it->max_ascent + it->max_descent
1156 : last_height);
1158 else
1160 struct glyph_row *row = it->glyph_row;
1162 /* Use the default character height. */
1163 it->glyph_row = NULL;
1164 it->what = IT_CHARACTER;
1165 it->c = ' ';
1166 it->len = 1;
1167 PRODUCE_GLYPHS (it);
1168 line_height = it->ascent + it->descent;
1169 it->glyph_row = row;
1173 return line_top_y + line_height;
1176 DEFUN ("line-pixel-height", Fline_pixel_height,
1177 Sline_pixel_height, 0, 0, 0,
1178 doc: /* Return height in pixels of text line in the selected window.
1180 Value is the height in pixels of the line at point. */)
1181 (void)
1183 struct it it;
1184 struct text_pos pt;
1185 struct window *w = XWINDOW (selected_window);
1186 struct buffer *old_buffer = NULL;
1187 Lisp_Object result;
1189 if (XBUFFER (w->contents) != current_buffer)
1191 old_buffer = current_buffer;
1192 set_buffer_internal_1 (XBUFFER (w->contents));
1194 SET_TEXT_POS (pt, PT, PT_BYTE);
1195 start_display (&it, w, pt);
1196 it.vpos = it.current_y = 0;
1197 last_height = 0;
1198 result = make_number (line_bottom_y (&it));
1199 if (old_buffer)
1200 set_buffer_internal_1 (old_buffer);
1202 return result;
1205 /* Return the default pixel height of text lines in window W. The
1206 value is the canonical height of the W frame's default font, plus
1207 any extra space required by the line-spacing variable or frame
1208 parameter.
1210 Implementation note: this ignores any line-spacing text properties
1211 put on the newline characters. This is because those properties
1212 only affect the _screen_ line ending in the newline (i.e., in a
1213 continued line, only the last screen line will be affected), which
1214 means only a small number of lines in a buffer can ever use this
1215 feature. Since this function is used to compute the default pixel
1216 equivalent of text lines in a window, we can safely ignore those
1217 few lines. For the same reasons, we ignore the line-height
1218 properties. */
1220 default_line_pixel_height (struct window *w)
1222 struct frame *f = WINDOW_XFRAME (w);
1223 int height = FRAME_LINE_HEIGHT (f);
1225 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1227 struct buffer *b = XBUFFER (w->contents);
1228 Lisp_Object val = BVAR (b, extra_line_spacing);
1230 if (NILP (val))
1231 val = BVAR (&buffer_defaults, extra_line_spacing);
1232 if (!NILP (val))
1234 if (RANGED_INTEGERP (0, val, INT_MAX))
1235 height += XFASTINT (val);
1236 else if (FLOATP (val))
1238 int addon = XFLOAT_DATA (val) * height + 0.5;
1240 if (addon >= 0)
1241 height += addon;
1244 else
1245 height += f->extra_line_spacing;
1248 return height;
1251 /* Subroutine of pos_visible_p below. Extracts a display string, if
1252 any, from the display spec given as its argument. */
1253 static Lisp_Object
1254 string_from_display_spec (Lisp_Object spec)
1256 if (VECTORP (spec))
1258 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1259 if (STRINGP (AREF (spec, i)))
1260 return AREF (spec, i);
1262 else
1264 for (; CONSP (spec); spec = XCDR (spec))
1265 if (STRINGP (XCAR (spec)))
1266 return XCAR (spec);
1268 return spec;
1272 /* Limit insanely large values of W->hscroll on frame F to the largest
1273 value that will still prevent first_visible_x and last_visible_x of
1274 'struct it' from overflowing an int. */
1275 static int
1276 window_hscroll_limited (struct window *w, struct frame *f)
1278 ptrdiff_t window_hscroll = w->hscroll;
1279 int window_text_width = window_box_width (w, TEXT_AREA);
1280 int colwidth = FRAME_COLUMN_WIDTH (f);
1282 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1283 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1285 return window_hscroll;
1288 /* Return true if position CHARPOS is visible in window W.
1289 CHARPOS < 0 means return info about WINDOW_END position.
1290 If visible, set *X and *Y to pixel coordinates of top left corner.
1291 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1292 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1294 bool
1295 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1296 int *rtop, int *rbot, int *rowh, int *vpos)
1298 struct it it;
1299 void *itdata = bidi_shelve_cache ();
1300 struct text_pos top;
1301 bool visible_p = false;
1302 struct buffer *old_buffer = NULL;
1303 bool r2l = false;
1305 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1306 return visible_p;
1308 if (XBUFFER (w->contents) != current_buffer)
1310 old_buffer = current_buffer;
1311 set_buffer_internal_1 (XBUFFER (w->contents));
1314 SET_TEXT_POS_FROM_MARKER (top, w->start);
1315 /* Scrolling a minibuffer window via scroll bar when the echo area
1316 shows long text sometimes resets the minibuffer contents behind
1317 our backs. Also, someone might narrow-to-region and immediately
1318 call a scroll function. */
1319 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1320 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1322 /* If the top of the window is after CHARPOS, the latter is surely
1323 not visible. */
1324 if (charpos >= 0 && CHARPOS (top) > charpos)
1325 return visible_p;
1327 /* Compute exact mode line heights. */
1328 if (WINDOW_WANTS_MODELINE_P (w))
1329 w->mode_line_height
1330 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1331 BVAR (current_buffer, mode_line_format));
1333 if (WINDOW_WANTS_HEADER_LINE_P (w))
1334 w->header_line_height
1335 = display_mode_line (w, HEADER_LINE_FACE_ID,
1336 BVAR (current_buffer, header_line_format));
1338 start_display (&it, w, top);
1339 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1340 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1342 if (charpos >= 0
1343 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1344 && IT_CHARPOS (it) >= charpos)
1345 /* When scanning backwards under bidi iteration, move_it_to
1346 stops at or _before_ CHARPOS, because it stops at or to
1347 the _right_ of the character at CHARPOS. */
1348 || (it.bidi_p && it.bidi_it.scan_dir == -1
1349 && IT_CHARPOS (it) <= charpos)))
1351 /* We have reached CHARPOS, or passed it. How the call to
1352 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1353 or covered by a display property, move_it_to stops at the end
1354 of the invisible text, to the right of CHARPOS. (ii) If
1355 CHARPOS is in a display vector, move_it_to stops on its last
1356 glyph. */
1357 int top_x = it.current_x;
1358 int top_y = it.current_y;
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1360 int bottom_y;
1361 struct it save_it;
1362 void *save_it_data = NULL;
1364 /* Calling line_bottom_y may change it.method, it.position, etc. */
1365 SAVE_IT (save_it, it, save_it_data);
1366 last_height = 0;
1367 bottom_y = line_bottom_y (&it);
1368 if (top_y < window_top_y)
1369 visible_p = bottom_y > window_top_y;
1370 else if (top_y < it.last_visible_y)
1371 visible_p = true;
1372 if (bottom_y >= it.last_visible_y
1373 && it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) < charpos)
1376 /* When the last line of the window is scanned backwards
1377 under bidi iteration, we could be duped into thinking
1378 that we have passed CHARPOS, when in fact move_it_to
1379 simply stopped short of CHARPOS because it reached
1380 last_visible_y. To see if that's what happened, we call
1381 move_it_to again with a slightly larger vertical limit,
1382 and see if it actually moved vertically; if it did, we
1383 didn't really reach CHARPOS, which is beyond window end. */
1384 /* Why 10? because we don't know how many canonical lines
1385 will the height of the next line(s) be. So we guess. */
1386 int ten_more_lines = 10 * default_line_pixel_height (w);
1388 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1389 MOVE_TO_POS | MOVE_TO_Y);
1390 if (it.current_y > top_y)
1391 visible_p = false;
1394 RESTORE_IT (&it, &save_it, save_it_data);
1395 if (visible_p)
1397 if (it.method == GET_FROM_DISPLAY_VECTOR)
1399 /* We stopped on the last glyph of a display vector.
1400 Try and recompute. Hack alert! */
1401 if (charpos < 2 || top.charpos >= charpos)
1402 top_x = it.glyph_row->x;
1403 else
1405 struct it it2, it2_prev;
1406 /* The idea is to get to the previous buffer
1407 position, consume the character there, and use
1408 the pixel coordinates we get after that. But if
1409 the previous buffer position is also displayed
1410 from a display vector, we need to consume all of
1411 the glyphs from that display vector. */
1412 start_display (&it2, w, top);
1413 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1414 /* If we didn't get to CHARPOS - 1, there's some
1415 replacing display property at that position, and
1416 we stopped after it. That is exactly the place
1417 whose coordinates we want. */
1418 if (IT_CHARPOS (it2) != charpos - 1)
1419 it2_prev = it2;
1420 else
1422 /* Iterate until we get out of the display
1423 vector that displays the character at
1424 CHARPOS - 1. */
1425 do {
1426 get_next_display_element (&it2);
1427 PRODUCE_GLYPHS (&it2);
1428 it2_prev = it2;
1429 set_iterator_to_next (&it2, true);
1430 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1431 && IT_CHARPOS (it2) < charpos);
1433 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1434 || it2_prev.current_x > it2_prev.last_visible_x)
1435 top_x = it.glyph_row->x;
1436 else
1438 top_x = it2_prev.current_x;
1439 top_y = it2_prev.current_y;
1443 else if (IT_CHARPOS (it) != charpos)
1445 Lisp_Object cpos = make_number (charpos);
1446 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1447 Lisp_Object string = string_from_display_spec (spec);
1448 struct text_pos tpos;
1449 bool newline_in_string
1450 = (STRINGP (string)
1451 && memchr (SDATA (string), '\n', SBYTES (string)));
1453 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1454 bool replacing_spec_p
1455 = (!NILP (spec)
1456 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1457 charpos, FRAME_WINDOW_P (it.f)));
1458 /* The tricky code below is needed because there's a
1459 discrepancy between move_it_to and how we set cursor
1460 when PT is at the beginning of a portion of text
1461 covered by a display property or an overlay with a
1462 display property, or the display line ends in a
1463 newline from a display string. move_it_to will stop
1464 _after_ such display strings, whereas
1465 set_cursor_from_row conspires with cursor_row_p to
1466 place the cursor on the first glyph produced from the
1467 display string. */
1469 /* We have overshoot PT because it is covered by a
1470 display property that replaces the text it covers.
1471 If the string includes embedded newlines, we are also
1472 in the wrong display line. Backtrack to the correct
1473 line, where the display property begins. */
1474 if (replacing_spec_p)
1476 Lisp_Object startpos, endpos;
1477 EMACS_INT start, end;
1478 struct it it3;
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays false after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 bool it3_moved = false;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = true;
1558 set_iterator_to_next (&it3, false);
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1575 while (EQ ((g - 1)->object, string))
1577 --g;
1578 top_x -= g->pixel_width;
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1597 else
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1627 else
1628 bidi_unshelve_cache (it2data, true);
1630 bidi_unshelve_cache (itdata, false);
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1635 if (visible_p)
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 #if false
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1659 return visible_p;
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1671 int c;
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1680 return c;
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1691 eassert (STRINGP (string) && nchars >= 0);
1693 if (STRING_MULTIBYTE (string))
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1698 while (nchars--)
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1709 return pos;
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P
1729 means recognize multibyte characters. */
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1734 struct text_pos pos;
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1739 if (multibyte_p)
1741 int len;
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1755 return pos;
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 means recognize multibyte characters. */
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1765 ptrdiff_t nchars;
1767 if (multibyte_p)
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1773 for (nchars = 0; rest > 0; ++nchars)
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1779 else
1780 nchars = strlen (s);
1782 return nchars;
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1819 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1820 if (face)
1822 if (face->font)
1823 height = normal_char_height (face->font, -1);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1829 return height;
1831 #endif
1833 return 1;
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1839 not force the value into range. */
1841 void
1842 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1843 NativeRectangle *bounds, bool noclip)
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1880 #endif
1882 *x = pix_x;
1883 *y = pix_y;
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1913 *vpos = i;
1914 *hpos = 0;
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1923 *area = TEXT_AREA;
1924 x0 = 0;
1926 else
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1938 else
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1951 x -= glyph->pixel_width;
1952 ++glyph;
1955 if (glyph == end)
1956 return NULL;
1958 if (dx)
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1974 if (w->pseudo_window_p)
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 else
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1989 #ifdef HAVE_WINDOW_SYSTEM
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1998 XRectangle r;
2000 if (n <= 0)
2001 return 0;
2003 if (s->row->full_width_p)
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2019 else
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2060 XRectangle rc, r_save = r;
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2067 x_intersect_rectangles (&r_save, &rc, &r);
2070 else
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2091 if (s->x > r.x)
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2099 r.width = min (r.width, glyph->pixel_width);
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2108 r.y = max_y;
2109 r.height = height;
2111 else
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2124 if (s->row->clip)
2126 XRectangle r_save = r;
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2142 else
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2155 if (s->for_overlaps & OVERLAPS_PRED)
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2172 if (r.y + r.height > row_y + s->row->visible_height)
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2177 else
2178 rs[i].height = 0;
2180 i++;
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2198 get_glyph_string_clip_rects (s, nr, 1);
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0, ascent;
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2223 wd += x;
2224 x = 0;
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2232 /* Don't let the hollow cursor glyph descend below the glyph row's
2233 ascent value, lest the hollow cursor looks funny. */
2234 y = w->phys_cursor.y;
2235 ascent = row->ascent;
2236 if (row->ascent < glyph->ascent)
2238 y -= glyph->ascent - row->ascent;
2239 ascent = glyph->ascent;
2242 /* If y is below window bottom, ensure that we still see a cursor. */
2243 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2245 h = max (h0, ascent + glyph->descent);
2246 h0 = min (h0, ascent + glyph->descent);
2248 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2249 if (y < y0)
2251 h = max (h - (y0 - y) + 1, h0);
2252 y = y0 - 1;
2254 else
2256 y0 = window_text_bottom_y (w) - h0;
2257 if (y > y0)
2259 h += y - y0;
2260 y = y0;
2264 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2265 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2266 *heightp = h;
2270 * Remember which glyph the mouse is over.
2273 void
2274 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2276 Lisp_Object window;
2277 struct window *w;
2278 struct glyph_row *r, *gr, *end_row;
2279 enum window_part part;
2280 enum glyph_row_area area;
2281 int x, y, width, height;
2283 /* Try to determine frame pixel position and size of the glyph under
2284 frame pixel coordinates X/Y on frame F. */
2286 if (window_resize_pixelwise)
2288 width = height = 1;
2289 goto virtual_glyph;
2291 else if (!f->glyphs_initialized_p
2292 || (window = window_from_coordinates (f, gx, gy, &part, false),
2293 NILP (window)))
2295 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2296 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2297 goto virtual_glyph;
2300 w = XWINDOW (window);
2301 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2302 height = WINDOW_FRAME_LINE_HEIGHT (w);
2304 x = window_relative_x_coord (w, part, gx);
2305 y = gy - WINDOW_TOP_EDGE_Y (w);
2307 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2308 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2310 if (w->pseudo_window_p)
2312 area = TEXT_AREA;
2313 part = ON_MODE_LINE; /* Don't adjust margin. */
2314 goto text_glyph;
2317 switch (part)
2319 case ON_LEFT_MARGIN:
2320 area = LEFT_MARGIN_AREA;
2321 goto text_glyph;
2323 case ON_RIGHT_MARGIN:
2324 area = RIGHT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_HEADER_LINE:
2328 case ON_MODE_LINE:
2329 gr = (part == ON_HEADER_LINE
2330 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2331 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2332 gy = gr->y;
2333 area = TEXT_AREA;
2334 goto text_glyph_row_found;
2336 case ON_TEXT:
2337 area = TEXT_AREA;
2339 text_glyph:
2340 gr = 0; gy = 0;
2341 for (; r <= end_row && r->enabled_p; ++r)
2342 if (r->y + r->height > y)
2344 gr = r; gy = r->y;
2345 break;
2348 text_glyph_row_found:
2349 if (gr && gy <= y)
2351 struct glyph *g = gr->glyphs[area];
2352 struct glyph *end = g + gr->used[area];
2354 height = gr->height;
2355 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2356 if (gx + g->pixel_width > x)
2357 break;
2359 if (g < end)
2361 if (g->type == IMAGE_GLYPH)
2363 /* Don't remember when mouse is over image, as
2364 image may have hot-spots. */
2365 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2366 return;
2368 width = g->pixel_width;
2370 else
2372 /* Use nominal char spacing at end of line. */
2373 x -= gx;
2374 gx += (x / width) * width;
2377 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2379 gx += window_box_left_offset (w, area);
2380 /* Don't expand over the modeline to make sure the vertical
2381 drag cursor is shown early enough. */
2382 height = min (height,
2383 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2386 else
2388 /* Use nominal line height at end of window. */
2389 gx = (x / width) * width;
2390 y -= gy;
2391 gy += (y / height) * height;
2392 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2393 /* See comment above. */
2394 height = min (height,
2395 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2397 break;
2399 case ON_LEFT_FRINGE:
2400 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2401 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2402 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2403 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2404 goto row_glyph;
2406 case ON_RIGHT_FRINGE:
2407 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2408 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2409 : window_box_right_offset (w, TEXT_AREA));
2410 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2411 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2412 && !WINDOW_RIGHTMOST_P (w))
2413 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2414 /* Make sure the vertical border can get her own glyph to the
2415 right of the one we build here. */
2416 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2417 else
2418 width = WINDOW_PIXEL_WIDTH (w) - gx;
2419 else
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2422 goto row_glyph;
2424 case ON_VERTICAL_BORDER:
2425 gx = WINDOW_PIXEL_WIDTH (w) - width;
2426 goto row_glyph;
2428 case ON_VERTICAL_SCROLL_BAR:
2429 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2431 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2432 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2433 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2434 : 0)));
2435 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2437 row_glyph:
2438 gr = 0, gy = 0;
2439 for (; r <= end_row && r->enabled_p; ++r)
2440 if (r->y + r->height > y)
2442 gr = r; gy = r->y;
2443 break;
2446 if (gr && gy <= y)
2447 height = gr->height;
2448 else
2450 /* Use nominal line height at end of window. */
2451 y -= gy;
2452 gy += (y / height) * height;
2454 break;
2456 case ON_RIGHT_DIVIDER:
2457 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2458 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2459 gy = 0;
2460 /* The bottom divider prevails. */
2461 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2462 goto add_edge;
2464 case ON_BOTTOM_DIVIDER:
2465 gx = 0;
2466 width = WINDOW_PIXEL_WIDTH (w);
2467 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2468 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2469 goto add_edge;
2471 default:
2473 virtual_glyph:
2474 /* If there is no glyph under the mouse, then we divide the screen
2475 into a grid of the smallest glyph in the frame, and use that
2476 as our "glyph". */
2478 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2479 round down even for negative values. */
2480 if (gx < 0)
2481 gx -= width - 1;
2482 if (gy < 0)
2483 gy -= height - 1;
2485 gx = (gx / width) * width;
2486 gy = (gy / height) * height;
2488 goto store_rect;
2491 add_edge:
2492 gx += WINDOW_LEFT_EDGE_X (w);
2493 gy += WINDOW_TOP_EDGE_Y (w);
2495 store_rect:
2496 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2498 /* Visible feedback for debugging. */
2499 #if false && defined HAVE_X_WINDOWS
2500 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2501 f->output_data.x->normal_gc,
2502 gx, gy, width, height);
2503 #endif
2507 #endif /* HAVE_WINDOW_SYSTEM */
2509 static void
2510 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2512 eassert (w);
2513 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2514 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2515 w->window_end_vpos
2516 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2519 static bool
2520 hscrolling_current_line_p (struct window *w)
2522 return (!w->suspend_auto_hscroll
2523 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2524 Qcurrent_line));
2527 /***********************************************************************
2528 Lisp form evaluation
2529 ***********************************************************************/
2531 /* Error handler for safe_eval and safe_call. */
2533 static Lisp_Object
2534 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2536 add_to_log ("Error during redisplay: %S signaled %S",
2537 Flist (nargs, args), arg);
2538 return Qnil;
2541 /* Call function FUNC with the rest of NARGS - 1 arguments
2542 following. Return the result, or nil if something went
2543 wrong. Prevent redisplay during the evaluation. */
2545 static Lisp_Object
2546 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2548 Lisp_Object val;
2550 if (inhibit_eval_during_redisplay)
2551 val = Qnil;
2552 else
2554 ptrdiff_t i;
2555 ptrdiff_t count = SPECPDL_INDEX ();
2556 Lisp_Object *args;
2557 USE_SAFE_ALLOCA;
2558 SAFE_ALLOCA_LISP (args, nargs);
2560 args[0] = func;
2561 for (i = 1; i < nargs; i++)
2562 args[i] = va_arg (ap, Lisp_Object);
2564 specbind (Qinhibit_redisplay, Qt);
2565 if (inhibit_quit)
2566 specbind (Qinhibit_quit, Qt);
2567 /* Use Qt to ensure debugger does not run,
2568 so there is no possibility of wanting to redisplay. */
2569 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2570 safe_eval_handler);
2571 SAFE_FREE ();
2572 val = unbind_to (count, val);
2575 return val;
2578 Lisp_Object
2579 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2581 Lisp_Object retval;
2582 va_list ap;
2584 va_start (ap, func);
2585 retval = safe__call (false, nargs, func, ap);
2586 va_end (ap);
2587 return retval;
2590 /* Call function FN with one argument ARG.
2591 Return the result, or nil if something went wrong. */
2593 Lisp_Object
2594 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2596 return safe_call (2, fn, arg);
2599 static Lisp_Object
2600 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2602 Lisp_Object retval;
2603 va_list ap;
2605 va_start (ap, fn);
2606 retval = safe__call (inhibit_quit, 2, fn, ap);
2607 va_end (ap);
2608 return retval;
2611 Lisp_Object
2612 safe_eval (Lisp_Object sexpr)
2614 return safe__call1 (false, Qeval, sexpr);
2617 static Lisp_Object
2618 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2620 return safe__call1 (inhibit_quit, Qeval, sexpr);
2623 /* Call function FN with two arguments ARG1 and ARG2.
2624 Return the result, or nil if something went wrong. */
2626 Lisp_Object
2627 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2629 return safe_call (3, fn, arg1, arg2);
2634 /***********************************************************************
2635 Debugging
2636 ***********************************************************************/
2638 /* Define CHECK_IT to perform sanity checks on iterators.
2639 This is for debugging. It is too slow to do unconditionally. */
2641 static void
2642 CHECK_IT (struct it *it)
2644 #if false
2645 if (it->method == GET_FROM_STRING)
2647 eassert (STRINGP (it->string));
2648 eassert (IT_STRING_CHARPOS (*it) >= 0);
2650 else
2652 eassert (IT_STRING_CHARPOS (*it) < 0);
2653 if (it->method == GET_FROM_BUFFER)
2655 /* Check that character and byte positions agree. */
2656 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2660 if (it->dpvec)
2661 eassert (it->current.dpvec_index >= 0);
2662 else
2663 eassert (it->current.dpvec_index < 0);
2664 #endif
2668 /* Check that the window end of window W is what we expect it
2669 to be---the last row in the current matrix displaying text. */
2671 static void
2672 CHECK_WINDOW_END (struct window *w)
2674 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2675 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2677 struct glyph_row *row;
2678 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2679 !row->enabled_p
2680 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2681 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2683 #endif
2686 /***********************************************************************
2687 Iterator initialization
2688 ***********************************************************************/
2690 /* Initialize IT for displaying current_buffer in window W, starting
2691 at character position CHARPOS. CHARPOS < 0 means that no buffer
2692 position is specified which is useful when the iterator is assigned
2693 a position later. BYTEPOS is the byte position corresponding to
2694 CHARPOS.
2696 If ROW is not null, calls to produce_glyphs with IT as parameter
2697 will produce glyphs in that row.
2699 BASE_FACE_ID is the id of a base face to use. It must be one of
2700 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2701 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2702 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2704 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2705 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2706 will be initialized to use the corresponding mode line glyph row of
2707 the desired matrix of W. */
2709 void
2710 init_iterator (struct it *it, struct window *w,
2711 ptrdiff_t charpos, ptrdiff_t bytepos,
2712 struct glyph_row *row, enum face_id base_face_id)
2714 enum face_id remapped_base_face_id = base_face_id;
2716 /* Some precondition checks. */
2717 eassert (w != NULL && it != NULL);
2718 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2719 && charpos <= ZV));
2721 /* If face attributes have been changed since the last redisplay,
2722 free realized faces now because they depend on face definitions
2723 that might have changed. Don't free faces while there might be
2724 desired matrices pending which reference these faces. */
2725 if (!inhibit_free_realized_faces)
2727 if (face_change)
2729 face_change = false;
2730 XFRAME (w->frame)->face_change = 0;
2731 free_all_realized_faces (Qnil);
2733 else if (XFRAME (w->frame)->face_change)
2735 XFRAME (w->frame)->face_change = 0;
2736 free_all_realized_faces (w->frame);
2740 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2741 if (! NILP (Vface_remapping_alist))
2742 remapped_base_face_id
2743 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2745 /* Use one of the mode line rows of W's desired matrix if
2746 appropriate. */
2747 if (row == NULL)
2749 if (base_face_id == MODE_LINE_FACE_ID
2750 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2751 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2752 else if (base_face_id == HEADER_LINE_FACE_ID)
2753 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2756 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2757 Other parts of redisplay rely on that. */
2758 memclear (it, sizeof *it);
2759 it->current.overlay_string_index = -1;
2760 it->current.dpvec_index = -1;
2761 it->base_face_id = remapped_base_face_id;
2762 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2763 it->paragraph_embedding = L2R;
2764 it->bidi_it.w = w;
2766 /* The window in which we iterate over current_buffer: */
2767 XSETWINDOW (it->window, w);
2768 it->w = w;
2769 it->f = XFRAME (w->frame);
2771 it->cmp_it.id = -1;
2773 /* Extra space between lines (on window systems only). */
2774 if (base_face_id == DEFAULT_FACE_ID
2775 && FRAME_WINDOW_P (it->f))
2777 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2778 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2779 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2780 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2781 * FRAME_LINE_HEIGHT (it->f));
2782 else if (it->f->extra_line_spacing > 0)
2783 it->extra_line_spacing = it->f->extra_line_spacing;
2786 /* If realized faces have been removed, e.g. because of face
2787 attribute changes of named faces, recompute them. When running
2788 in batch mode, the face cache of the initial frame is null. If
2789 we happen to get called, make a dummy face cache. */
2790 if (FRAME_FACE_CACHE (it->f) == NULL)
2791 init_frame_faces (it->f);
2792 if (FRAME_FACE_CACHE (it->f)->used == 0)
2793 recompute_basic_faces (it->f);
2795 it->override_ascent = -1;
2797 /* Are control characters displayed as `^C'? */
2798 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2800 /* -1 means everything between a CR and the following line end
2801 is invisible. >0 means lines indented more than this value are
2802 invisible. */
2803 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2804 ? (clip_to_bounds
2805 (-1, XINT (BVAR (current_buffer, selective_display)),
2806 PTRDIFF_MAX))
2807 : (!NILP (BVAR (current_buffer, selective_display))
2808 ? -1 : 0));
2809 it->selective_display_ellipsis_p
2810 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2812 /* Display table to use. */
2813 it->dp = window_display_table (w);
2815 /* Are multibyte characters enabled in current_buffer? */
2816 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2818 /* Get the position at which the redisplay_end_trigger hook should
2819 be run, if it is to be run at all. */
2820 if (MARKERP (w->redisplay_end_trigger)
2821 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2822 it->redisplay_end_trigger_charpos
2823 = marker_position (w->redisplay_end_trigger);
2824 else if (INTEGERP (w->redisplay_end_trigger))
2825 it->redisplay_end_trigger_charpos
2826 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2827 PTRDIFF_MAX);
2829 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2831 /* Are lines in the display truncated? */
2832 if (TRUNCATE != 0)
2833 it->line_wrap = TRUNCATE;
2834 if (base_face_id == DEFAULT_FACE_ID
2835 && !it->w->hscroll
2836 && (WINDOW_FULL_WIDTH_P (it->w)
2837 || NILP (Vtruncate_partial_width_windows)
2838 || (INTEGERP (Vtruncate_partial_width_windows)
2839 /* PXW: Shall we do something about this? */
2840 && (XINT (Vtruncate_partial_width_windows)
2841 <= WINDOW_TOTAL_COLS (it->w))))
2842 && NILP (BVAR (current_buffer, truncate_lines)))
2843 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2844 ? WINDOW_WRAP : WORD_WRAP;
2846 /* Get dimensions of truncation and continuation glyphs. These are
2847 displayed as fringe bitmaps under X, but we need them for such
2848 frames when the fringes are turned off. But leave the dimensions
2849 zero for tooltip frames, as these glyphs look ugly there and also
2850 sabotage calculations of tooltip dimensions in x-show-tip. */
2851 #ifdef HAVE_WINDOW_SYSTEM
2852 if (!(FRAME_WINDOW_P (it->f)
2853 && FRAMEP (tip_frame)
2854 && it->f == XFRAME (tip_frame)))
2855 #endif
2857 if (it->line_wrap == TRUNCATE)
2859 /* We will need the truncation glyph. */
2860 eassert (it->glyph_row == NULL);
2861 produce_special_glyphs (it, IT_TRUNCATION);
2862 it->truncation_pixel_width = it->pixel_width;
2864 else
2866 /* We will need the continuation glyph. */
2867 eassert (it->glyph_row == NULL);
2868 produce_special_glyphs (it, IT_CONTINUATION);
2869 it->continuation_pixel_width = it->pixel_width;
2873 /* Reset these values to zero because the produce_special_glyphs
2874 above has changed them. */
2875 it->pixel_width = it->ascent = it->descent = 0;
2876 it->phys_ascent = it->phys_descent = 0;
2878 /* Set this after getting the dimensions of truncation and
2879 continuation glyphs, so that we don't produce glyphs when calling
2880 produce_special_glyphs, above. */
2881 it->glyph_row = row;
2882 it->area = TEXT_AREA;
2884 /* Get the dimensions of the display area. The display area
2885 consists of the visible window area plus a horizontally scrolled
2886 part to the left of the window. All x-values are relative to the
2887 start of this total display area. */
2888 if (base_face_id != DEFAULT_FACE_ID)
2890 /* Mode lines, menu bar in terminal frames. */
2891 it->first_visible_x = 0;
2892 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2894 else
2896 /* When hscrolling only the current line, don't apply the
2897 hscroll here, it will be applied by display_line when it gets
2898 to laying out the line showing point. However, if the
2899 window's min_hscroll is positive, the user specified a lower
2900 bound for automatic hscrolling, so they expect the
2901 non-current lines to obey that hscroll amount. */
2902 if (hscrolling_current_line_p (w))
2904 if (w->min_hscroll > 0)
2905 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2906 else
2907 it->first_visible_x = 0;
2909 else
2910 it->first_visible_x =
2911 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2912 it->last_visible_x = (it->first_visible_x
2913 + window_box_width (w, TEXT_AREA));
2915 /* If we truncate lines, leave room for the truncation glyph(s) at
2916 the right margin. Otherwise, leave room for the continuation
2917 glyph(s). Done only if the window has no right fringe. */
2918 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2920 if (it->line_wrap == TRUNCATE)
2921 it->last_visible_x -= it->truncation_pixel_width;
2922 else
2923 it->last_visible_x -= it->continuation_pixel_width;
2926 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2927 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2930 /* Leave room for a border glyph. */
2931 if (!FRAME_WINDOW_P (it->f)
2932 && !WINDOW_RIGHTMOST_P (it->w))
2933 it->last_visible_x -= 1;
2935 it->last_visible_y = window_text_bottom_y (w);
2937 /* For mode lines and alike, arrange for the first glyph having a
2938 left box line if the face specifies a box. */
2939 if (base_face_id != DEFAULT_FACE_ID)
2941 struct face *face;
2943 it->face_id = remapped_base_face_id;
2945 /* If we have a boxed mode line, make the first character appear
2946 with a left box line. */
2947 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2948 if (face && face->box != FACE_NO_BOX)
2949 it->start_of_box_run_p = true;
2952 /* If a buffer position was specified, set the iterator there,
2953 getting overlays and face properties from that position. */
2954 if (charpos >= BUF_BEG (current_buffer))
2956 it->stop_charpos = charpos;
2957 it->end_charpos = ZV;
2958 eassert (charpos == BYTE_TO_CHAR (bytepos));
2959 IT_CHARPOS (*it) = charpos;
2960 IT_BYTEPOS (*it) = bytepos;
2962 /* We will rely on `reseat' to set this up properly, via
2963 handle_face_prop. */
2964 it->face_id = it->base_face_id;
2966 it->start = it->current;
2967 /* Do we need to reorder bidirectional text? Not if this is a
2968 unibyte buffer: by definition, none of the single-byte
2969 characters are strong R2L, so no reordering is needed. And
2970 bidi.c doesn't support unibyte buffers anyway. Also, don't
2971 reorder while we are loading loadup.el, since the tables of
2972 character properties needed for reordering are not yet
2973 available. */
2974 it->bidi_p =
2975 !redisplay__inhibit_bidi
2976 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2977 && it->multibyte_p;
2979 /* If we are to reorder bidirectional text, init the bidi
2980 iterator. */
2981 if (it->bidi_p)
2983 /* Since we don't know at this point whether there will be
2984 any R2L lines in the window, we reserve space for
2985 truncation/continuation glyphs even if only the left
2986 fringe is absent. */
2987 if (base_face_id == DEFAULT_FACE_ID
2988 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2989 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2991 if (it->line_wrap == TRUNCATE)
2992 it->last_visible_x -= it->truncation_pixel_width;
2993 else
2994 it->last_visible_x -= it->continuation_pixel_width;
2996 /* Note the paragraph direction that this buffer wants to
2997 use. */
2998 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2999 Qleft_to_right))
3000 it->paragraph_embedding = L2R;
3001 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3002 Qright_to_left))
3003 it->paragraph_embedding = R2L;
3004 else
3005 it->paragraph_embedding = NEUTRAL_DIR;
3006 bidi_unshelve_cache (NULL, false);
3007 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3008 &it->bidi_it);
3011 /* Compute faces etc. */
3012 reseat (it, it->current.pos, true);
3015 CHECK_IT (it);
3019 /* Initialize IT for the display of window W with window start POS. */
3021 void
3022 start_display (struct it *it, struct window *w, struct text_pos pos)
3024 struct glyph_row *row;
3025 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3027 row = w->desired_matrix->rows + first_vpos;
3028 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3029 it->first_vpos = first_vpos;
3031 /* Don't reseat to previous visible line start if current start
3032 position is in a string or image. */
3033 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3035 int first_y = it->current_y;
3037 /* If window start is not at a line start, skip forward to POS to
3038 get the correct continuation lines width. */
3039 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3040 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3041 if (!start_at_line_beg_p)
3043 int new_x;
3045 reseat_at_previous_visible_line_start (it);
3046 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3048 new_x = it->current_x + it->pixel_width;
3050 /* If lines are continued, this line may end in the middle
3051 of a multi-glyph character (e.g. a control character
3052 displayed as \003, or in the middle of an overlay
3053 string). In this case move_it_to above will not have
3054 taken us to the start of the continuation line but to the
3055 end of the continued line. */
3056 if (it->current_x > 0
3057 && it->line_wrap != TRUNCATE /* Lines are continued. */
3058 && (/* And glyph doesn't fit on the line. */
3059 new_x > it->last_visible_x
3060 /* Or it fits exactly and we're on a window
3061 system frame. */
3062 || (new_x == it->last_visible_x
3063 && FRAME_WINDOW_P (it->f)
3064 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3065 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3066 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3068 if ((it->current.dpvec_index >= 0
3069 || it->current.overlay_string_index >= 0)
3070 /* If we are on a newline from a display vector or
3071 overlay string, then we are already at the end of
3072 a screen line; no need to go to the next line in
3073 that case, as this line is not really continued.
3074 (If we do go to the next line, C-e will not DTRT.) */
3075 && it->c != '\n')
3077 set_iterator_to_next (it, true);
3078 move_it_in_display_line_to (it, -1, -1, 0);
3081 it->continuation_lines_width += it->current_x;
3083 /* If the character at POS is displayed via a display
3084 vector, move_it_to above stops at the final glyph of
3085 IT->dpvec. To make the caller redisplay that character
3086 again (a.k.a. start at POS), we need to reset the
3087 dpvec_index to the beginning of IT->dpvec. */
3088 else if (it->current.dpvec_index >= 0)
3089 it->current.dpvec_index = 0;
3091 /* We're starting a new display line, not affected by the
3092 height of the continued line, so clear the appropriate
3093 fields in the iterator structure. */
3094 it->max_ascent = it->max_descent = 0;
3095 it->max_phys_ascent = it->max_phys_descent = 0;
3097 it->current_y = first_y;
3098 it->vpos = 0;
3099 it->current_x = it->hpos = 0;
3105 /* Return true if POS is a position in ellipses displayed for invisible
3106 text. W is the window we display, for text property lookup. */
3108 static bool
3109 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3111 Lisp_Object prop, window;
3112 bool ellipses_p = false;
3113 ptrdiff_t charpos = CHARPOS (pos->pos);
3115 /* If POS specifies a position in a display vector, this might
3116 be for an ellipsis displayed for invisible text. We won't
3117 get the iterator set up for delivering that ellipsis unless
3118 we make sure that it gets aware of the invisible text. */
3119 if (pos->dpvec_index >= 0
3120 && pos->overlay_string_index < 0
3121 && CHARPOS (pos->string_pos) < 0
3122 && charpos > BEGV
3123 && (XSETWINDOW (window, w),
3124 prop = Fget_char_property (make_number (charpos),
3125 Qinvisible, window),
3126 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3128 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3129 window);
3130 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3133 return ellipses_p;
3137 /* Initialize IT for stepping through current_buffer in window W,
3138 starting at position POS that includes overlay string and display
3139 vector/ control character translation position information. Value
3140 is false if there are overlay strings with newlines at POS. */
3142 static bool
3143 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3145 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3146 int i;
3147 bool overlay_strings_with_newlines = false;
3149 /* If POS specifies a position in a display vector, this might
3150 be for an ellipsis displayed for invisible text. We won't
3151 get the iterator set up for delivering that ellipsis unless
3152 we make sure that it gets aware of the invisible text. */
3153 if (in_ellipses_for_invisible_text_p (pos, w))
3155 --charpos;
3156 bytepos = 0;
3159 /* Keep in mind: the call to reseat in init_iterator skips invisible
3160 text, so we might end up at a position different from POS. This
3161 is only a problem when POS is a row start after a newline and an
3162 overlay starts there with an after-string, and the overlay has an
3163 invisible property. Since we don't skip invisible text in
3164 display_line and elsewhere immediately after consuming the
3165 newline before the row start, such a POS will not be in a string,
3166 but the call to init_iterator below will move us to the
3167 after-string. */
3168 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3170 /* This only scans the current chunk -- it should scan all chunks.
3171 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3172 to 16 in 22.1 to make this a lesser problem. */
3173 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3175 const char *s = SSDATA (it->overlay_strings[i]);
3176 const char *e = s + SBYTES (it->overlay_strings[i]);
3178 while (s < e && *s != '\n')
3179 ++s;
3181 if (s < e)
3183 overlay_strings_with_newlines = true;
3184 break;
3188 /* If position is within an overlay string, set up IT to the right
3189 overlay string. */
3190 if (pos->overlay_string_index >= 0)
3192 int relative_index;
3194 /* If the first overlay string happens to have a `display'
3195 property for an image, the iterator will be set up for that
3196 image, and we have to undo that setup first before we can
3197 correct the overlay string index. */
3198 if (it->method == GET_FROM_IMAGE)
3199 pop_it (it);
3201 /* We already have the first chunk of overlay strings in
3202 IT->overlay_strings. Load more until the one for
3203 pos->overlay_string_index is in IT->overlay_strings. */
3204 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3206 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3207 it->current.overlay_string_index = 0;
3208 while (n--)
3210 load_overlay_strings (it, 0);
3211 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3215 it->current.overlay_string_index = pos->overlay_string_index;
3216 relative_index = (it->current.overlay_string_index
3217 % OVERLAY_STRING_CHUNK_SIZE);
3218 it->string = it->overlay_strings[relative_index];
3219 eassert (STRINGP (it->string));
3220 it->current.string_pos = pos->string_pos;
3221 it->method = GET_FROM_STRING;
3222 it->end_charpos = SCHARS (it->string);
3223 /* Set up the bidi iterator for this overlay string. */
3224 if (it->bidi_p)
3226 it->bidi_it.string.lstring = it->string;
3227 it->bidi_it.string.s = NULL;
3228 it->bidi_it.string.schars = SCHARS (it->string);
3229 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3230 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3231 it->bidi_it.string.unibyte = !it->multibyte_p;
3232 it->bidi_it.w = it->w;
3233 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3234 FRAME_WINDOW_P (it->f), &it->bidi_it);
3236 /* Synchronize the state of the bidi iterator with
3237 pos->string_pos. For any string position other than
3238 zero, this will be done automagically when we resume
3239 iteration over the string and get_visually_first_element
3240 is called. But if string_pos is zero, and the string is
3241 to be reordered for display, we need to resync manually,
3242 since it could be that the iteration state recorded in
3243 pos ended at string_pos of 0 moving backwards in string. */
3244 if (CHARPOS (pos->string_pos) == 0)
3246 get_visually_first_element (it);
3247 if (IT_STRING_CHARPOS (*it) != 0)
3248 do {
3249 /* Paranoia. */
3250 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3251 bidi_move_to_visually_next (&it->bidi_it);
3252 } while (it->bidi_it.charpos != 0);
3254 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3255 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3259 if (CHARPOS (pos->string_pos) >= 0)
3261 /* Recorded position is not in an overlay string, but in another
3262 string. This can only be a string from a `display' property.
3263 IT should already be filled with that string. */
3264 it->current.string_pos = pos->string_pos;
3265 eassert (STRINGP (it->string));
3266 if (it->bidi_p)
3267 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3268 FRAME_WINDOW_P (it->f), &it->bidi_it);
3271 /* Restore position in display vector translations, control
3272 character translations or ellipses. */
3273 if (pos->dpvec_index >= 0)
3275 if (it->dpvec == NULL)
3276 get_next_display_element (it);
3277 eassert (it->dpvec && it->current.dpvec_index == 0);
3278 it->current.dpvec_index = pos->dpvec_index;
3281 CHECK_IT (it);
3282 return !overlay_strings_with_newlines;
3286 /* Initialize IT for stepping through current_buffer in window W
3287 starting at ROW->start. */
3289 static void
3290 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3292 init_from_display_pos (it, w, &row->start);
3293 it->start = row->start;
3294 it->continuation_lines_width = row->continuation_lines_width;
3295 CHECK_IT (it);
3299 /* Initialize IT for stepping through current_buffer in window W
3300 starting in the line following ROW, i.e. starting at ROW->end.
3301 Value is false if there are overlay strings with newlines at ROW's
3302 end position. */
3304 static bool
3305 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3307 bool success = false;
3309 if (init_from_display_pos (it, w, &row->end))
3311 if (row->continued_p)
3312 it->continuation_lines_width
3313 = row->continuation_lines_width + row->pixel_width;
3314 CHECK_IT (it);
3315 success = true;
3318 return success;
3324 /***********************************************************************
3325 Text properties
3326 ***********************************************************************/
3328 /* Called when IT reaches IT->stop_charpos. Handle text property and
3329 overlay changes. Set IT->stop_charpos to the next position where
3330 to stop. */
3332 static void
3333 handle_stop (struct it *it)
3335 enum prop_handled handled;
3336 bool handle_overlay_change_p;
3337 struct props *p;
3339 it->dpvec = NULL;
3340 it->current.dpvec_index = -1;
3341 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3342 it->ellipsis_p = false;
3344 /* Use face of preceding text for ellipsis (if invisible) */
3345 if (it->selective_display_ellipsis_p)
3346 it->saved_face_id = it->face_id;
3348 /* Here's the description of the semantics of, and the logic behind,
3349 the various HANDLED_* statuses:
3351 HANDLED_NORMALLY means the handler did its job, and the loop
3352 should proceed to calling the next handler in order.
3354 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3355 change in the properties and overlays at current position, so the
3356 loop should be restarted, to re-invoke the handlers that were
3357 already called. This happens when fontification-functions were
3358 called by handle_fontified_prop, and actually fontified
3359 something. Another case where HANDLED_RECOMPUTE_PROPS is
3360 returned is when we discover overlay strings that need to be
3361 displayed right away. The loop below will continue for as long
3362 as the status is HANDLED_RECOMPUTE_PROPS.
3364 HANDLED_RETURN means return immediately to the caller, to
3365 continue iteration without calling any further handlers. This is
3366 used when we need to act on some property right away, for example
3367 when we need to display the ellipsis or a replacing display
3368 property, such as display string or image.
3370 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3371 consumed, and the handler switched to the next overlay string.
3372 This signals the loop below to refrain from looking for more
3373 overlays before all the overlay strings of the current overlay
3374 are processed.
3376 Some of the handlers called by the loop push the iterator state
3377 onto the stack (see 'push_it'), and arrange for the iteration to
3378 continue with another object, such as an image, a display string,
3379 or an overlay string. In most such cases, it->stop_charpos is
3380 set to the first character of the string, so that when the
3381 iteration resumes, this function will immediately be called
3382 again, to examine the properties at the beginning of the string.
3384 When a display or overlay string is exhausted, the iterator state
3385 is popped (see 'pop_it'), and iteration continues with the
3386 previous object. Again, in many such cases this function is
3387 called again to find the next position where properties might
3388 change. */
3392 handled = HANDLED_NORMALLY;
3394 /* Call text property handlers. */
3395 for (p = it_props; p->handler; ++p)
3397 handled = p->handler (it);
3399 if (handled == HANDLED_RECOMPUTE_PROPS)
3400 break;
3401 else if (handled == HANDLED_RETURN)
3403 /* We still want to show before and after strings from
3404 overlays even if the actual buffer text is replaced. */
3405 if (!handle_overlay_change_p
3406 || it->sp > 1
3407 /* Don't call get_overlay_strings_1 if we already
3408 have overlay strings loaded, because doing so
3409 will load them again and push the iterator state
3410 onto the stack one more time, which is not
3411 expected by the rest of the code that processes
3412 overlay strings. */
3413 || (it->current.overlay_string_index < 0
3414 && !get_overlay_strings_1 (it, 0, false)))
3416 if (it->ellipsis_p)
3417 setup_for_ellipsis (it, 0);
3418 /* When handling a display spec, we might load an
3419 empty string. In that case, discard it here. We
3420 used to discard it in handle_single_display_spec,
3421 but that causes get_overlay_strings_1, above, to
3422 ignore overlay strings that we must check. */
3423 if (STRINGP (it->string) && !SCHARS (it->string))
3424 pop_it (it);
3425 return;
3427 else if (STRINGP (it->string) && !SCHARS (it->string))
3428 pop_it (it);
3429 else
3431 it->string_from_display_prop_p = false;
3432 it->from_disp_prop_p = false;
3433 handle_overlay_change_p = false;
3435 handled = HANDLED_RECOMPUTE_PROPS;
3436 break;
3438 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3439 handle_overlay_change_p = false;
3442 if (handled != HANDLED_RECOMPUTE_PROPS)
3444 /* Don't check for overlay strings below when set to deliver
3445 characters from a display vector. */
3446 if (it->method == GET_FROM_DISPLAY_VECTOR)
3447 handle_overlay_change_p = false;
3449 /* Handle overlay changes.
3450 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3451 if it finds overlays. */
3452 if (handle_overlay_change_p)
3453 handled = handle_overlay_change (it);
3456 if (it->ellipsis_p)
3458 setup_for_ellipsis (it, 0);
3459 break;
3462 while (handled == HANDLED_RECOMPUTE_PROPS);
3464 /* Determine where to stop next. */
3465 if (handled == HANDLED_NORMALLY)
3466 compute_stop_pos (it);
3470 /* Compute IT->stop_charpos from text property and overlay change
3471 information for IT's current position. */
3473 static void
3474 compute_stop_pos (struct it *it)
3476 register INTERVAL iv, next_iv;
3477 Lisp_Object object, limit, position;
3478 ptrdiff_t charpos, bytepos;
3480 if (STRINGP (it->string))
3482 /* Strings are usually short, so don't limit the search for
3483 properties. */
3484 it->stop_charpos = it->end_charpos;
3485 object = it->string;
3486 limit = Qnil;
3487 charpos = IT_STRING_CHARPOS (*it);
3488 bytepos = IT_STRING_BYTEPOS (*it);
3490 else
3492 ptrdiff_t pos;
3494 /* If end_charpos is out of range for some reason, such as a
3495 misbehaving display function, rationalize it (Bug#5984). */
3496 if (it->end_charpos > ZV)
3497 it->end_charpos = ZV;
3498 it->stop_charpos = it->end_charpos;
3500 /* If next overlay change is in front of the current stop pos
3501 (which is IT->end_charpos), stop there. Note: value of
3502 next_overlay_change is point-max if no overlay change
3503 follows. */
3504 charpos = IT_CHARPOS (*it);
3505 bytepos = IT_BYTEPOS (*it);
3506 pos = next_overlay_change (charpos);
3507 if (pos < it->stop_charpos)
3508 it->stop_charpos = pos;
3510 /* Set up variables for computing the stop position from text
3511 property changes. */
3512 XSETBUFFER (object, current_buffer);
3513 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3516 /* Get the interval containing IT's position. Value is a null
3517 interval if there isn't such an interval. */
3518 position = make_number (charpos);
3519 iv = validate_interval_range (object, &position, &position, false);
3520 if (iv)
3522 Lisp_Object values_here[LAST_PROP_IDX];
3523 struct props *p;
3525 /* Get properties here. */
3526 for (p = it_props; p->handler; ++p)
3527 values_here[p->idx] = textget (iv->plist,
3528 builtin_lisp_symbol (p->name));
3530 /* Look for an interval following iv that has different
3531 properties. */
3532 for (next_iv = next_interval (iv);
3533 (next_iv
3534 && (NILP (limit)
3535 || XFASTINT (limit) > next_iv->position));
3536 next_iv = next_interval (next_iv))
3538 for (p = it_props; p->handler; ++p)
3540 Lisp_Object new_value = textget (next_iv->plist,
3541 builtin_lisp_symbol (p->name));
3542 if (!EQ (values_here[p->idx], new_value))
3543 break;
3546 if (p->handler)
3547 break;
3550 if (next_iv)
3552 if (INTEGERP (limit)
3553 && next_iv->position >= XFASTINT (limit))
3554 /* No text property change up to limit. */
3555 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3556 else
3557 /* Text properties change in next_iv. */
3558 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3562 if (it->cmp_it.id < 0)
3564 ptrdiff_t stoppos = it->end_charpos;
3566 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3567 stoppos = -1;
3568 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3569 stoppos, it->string);
3572 eassert (STRINGP (it->string)
3573 || (it->stop_charpos >= BEGV
3574 && it->stop_charpos >= IT_CHARPOS (*it)));
3578 /* Return the position of the next overlay change after POS in
3579 current_buffer. Value is point-max if no overlay change
3580 follows. This is like `next-overlay-change' but doesn't use
3581 xmalloc. */
3583 static ptrdiff_t
3584 next_overlay_change (ptrdiff_t pos)
3586 ptrdiff_t i, noverlays;
3587 ptrdiff_t endpos;
3588 Lisp_Object *overlays;
3589 USE_SAFE_ALLOCA;
3591 /* Get all overlays at the given position. */
3592 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3594 /* If any of these overlays ends before endpos,
3595 use its ending point instead. */
3596 for (i = 0; i < noverlays; ++i)
3598 Lisp_Object oend;
3599 ptrdiff_t oendpos;
3601 oend = OVERLAY_END (overlays[i]);
3602 oendpos = OVERLAY_POSITION (oend);
3603 endpos = min (endpos, oendpos);
3606 SAFE_FREE ();
3607 return endpos;
3610 /* How many characters forward to search for a display property or
3611 display string. Searching too far forward makes the bidi display
3612 sluggish, especially in small windows. */
3613 #define MAX_DISP_SCAN 250
3615 /* Return the character position of a display string at or after
3616 position specified by POSITION. If no display string exists at or
3617 after POSITION, return ZV. A display string is either an overlay
3618 with `display' property whose value is a string, or a `display'
3619 text property whose value is a string. STRING is data about the
3620 string to iterate; if STRING->lstring is nil, we are iterating a
3621 buffer. FRAME_WINDOW_P is true when we are displaying a window
3622 on a GUI frame. DISP_PROP is set to zero if we searched
3623 MAX_DISP_SCAN characters forward without finding any display
3624 strings, non-zero otherwise. It is set to 2 if the display string
3625 uses any kind of `(space ...)' spec that will produce a stretch of
3626 white space in the text area. */
3627 ptrdiff_t
3628 compute_display_string_pos (struct text_pos *position,
3629 struct bidi_string_data *string,
3630 struct window *w,
3631 bool frame_window_p, int *disp_prop)
3633 /* OBJECT = nil means current buffer. */
3634 Lisp_Object object, object1;
3635 Lisp_Object pos, spec, limpos;
3636 bool string_p = string && (STRINGP (string->lstring) || string->s);
3637 ptrdiff_t eob = string_p ? string->schars : ZV;
3638 ptrdiff_t begb = string_p ? 0 : BEGV;
3639 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3640 ptrdiff_t lim =
3641 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3642 struct text_pos tpos;
3643 int rv = 0;
3645 if (string && STRINGP (string->lstring))
3646 object1 = object = string->lstring;
3647 else if (w && !string_p)
3649 XSETWINDOW (object, w);
3650 object1 = Qnil;
3652 else
3653 object1 = object = Qnil;
3655 *disp_prop = 1;
3657 if (charpos >= eob
3658 /* We don't support display properties whose values are strings
3659 that have display string properties. */
3660 || string->from_disp_str
3661 /* C strings cannot have display properties. */
3662 || (string->s && !STRINGP (object)))
3664 *disp_prop = 0;
3665 return eob;
3668 /* If the character at CHARPOS is where the display string begins,
3669 return CHARPOS. */
3670 pos = make_number (charpos);
3671 if (STRINGP (object))
3672 bufpos = string->bufpos;
3673 else
3674 bufpos = charpos;
3675 tpos = *position;
3676 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3677 && (charpos <= begb
3678 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3679 object),
3680 spec))
3681 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3682 frame_window_p)))
3684 if (rv == 2)
3685 *disp_prop = 2;
3686 return charpos;
3689 /* Look forward for the first character with a `display' property
3690 that will replace the underlying text when displayed. */
3691 limpos = make_number (lim);
3692 do {
3693 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3694 CHARPOS (tpos) = XFASTINT (pos);
3695 if (CHARPOS (tpos) >= lim)
3697 *disp_prop = 0;
3698 break;
3700 if (STRINGP (object))
3701 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3702 else
3703 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3704 spec = Fget_char_property (pos, Qdisplay, object);
3705 if (!STRINGP (object))
3706 bufpos = CHARPOS (tpos);
3707 } while (NILP (spec)
3708 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3709 bufpos, frame_window_p)));
3710 if (rv == 2)
3711 *disp_prop = 2;
3713 return CHARPOS (tpos);
3716 /* Return the character position of the end of the display string that
3717 started at CHARPOS. If there's no display string at CHARPOS,
3718 return -1. A display string is either an overlay with `display'
3719 property whose value is a string or a `display' text property whose
3720 value is a string. */
3721 ptrdiff_t
3722 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3724 /* OBJECT = nil means current buffer. */
3725 Lisp_Object object =
3726 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3727 Lisp_Object pos = make_number (charpos);
3728 ptrdiff_t eob =
3729 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3731 if (charpos >= eob || (string->s && !STRINGP (object)))
3732 return eob;
3734 /* It could happen that the display property or overlay was removed
3735 since we found it in compute_display_string_pos above. One way
3736 this can happen is if JIT font-lock was called (through
3737 handle_fontified_prop), and jit-lock-functions remove text
3738 properties or overlays from the portion of buffer that includes
3739 CHARPOS. Muse mode is known to do that, for example. In this
3740 case, we return -1 to the caller, to signal that no display
3741 string is actually present at CHARPOS. See bidi_fetch_char for
3742 how this is handled.
3744 An alternative would be to never look for display properties past
3745 it->stop_charpos. But neither compute_display_string_pos nor
3746 bidi_fetch_char that calls it know or care where the next
3747 stop_charpos is. */
3748 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3749 return -1;
3751 /* Look forward for the first character where the `display' property
3752 changes. */
3753 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3755 return XFASTINT (pos);
3760 /***********************************************************************
3761 Fontification
3762 ***********************************************************************/
3764 /* Handle changes in the `fontified' property of the current buffer by
3765 calling hook functions from Qfontification_functions to fontify
3766 regions of text. */
3768 static enum prop_handled
3769 handle_fontified_prop (struct it *it)
3771 Lisp_Object prop, pos;
3772 enum prop_handled handled = HANDLED_NORMALLY;
3774 if (!NILP (Vmemory_full))
3775 return handled;
3777 /* Get the value of the `fontified' property at IT's current buffer
3778 position. (The `fontified' property doesn't have a special
3779 meaning in strings.) If the value is nil, call functions from
3780 Qfontification_functions. */
3781 if (!STRINGP (it->string)
3782 && it->s == NULL
3783 && !NILP (Vfontification_functions)
3784 && !NILP (Vrun_hooks)
3785 && (pos = make_number (IT_CHARPOS (*it)),
3786 prop = Fget_char_property (pos, Qfontified, Qnil),
3787 /* Ignore the special cased nil value always present at EOB since
3788 no amount of fontifying will be able to change it. */
3789 NILP (prop) && IT_CHARPOS (*it) < Z))
3791 ptrdiff_t count = SPECPDL_INDEX ();
3792 Lisp_Object val;
3793 struct buffer *obuf = current_buffer;
3794 ptrdiff_t begv = BEGV, zv = ZV;
3795 bool old_clip_changed = current_buffer->clip_changed;
3797 val = Vfontification_functions;
3798 specbind (Qfontification_functions, Qnil);
3800 eassert (it->end_charpos == ZV);
3802 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3803 safe_call1 (val, pos);
3804 else
3806 Lisp_Object fns, fn;
3808 fns = Qnil;
3810 for (; CONSP (val); val = XCDR (val))
3812 fn = XCAR (val);
3814 if (EQ (fn, Qt))
3816 /* A value of t indicates this hook has a local
3817 binding; it means to run the global binding too.
3818 In a global value, t should not occur. If it
3819 does, we must ignore it to avoid an endless
3820 loop. */
3821 for (fns = Fdefault_value (Qfontification_functions);
3822 CONSP (fns);
3823 fns = XCDR (fns))
3825 fn = XCAR (fns);
3826 if (!EQ (fn, Qt))
3827 safe_call1 (fn, pos);
3830 else
3831 safe_call1 (fn, pos);
3835 unbind_to (count, Qnil);
3837 /* Fontification functions routinely call `save-restriction'.
3838 Normally, this tags clip_changed, which can confuse redisplay
3839 (see discussion in Bug#6671). Since we don't perform any
3840 special handling of fontification changes in the case where
3841 `save-restriction' isn't called, there's no point doing so in
3842 this case either. So, if the buffer's restrictions are
3843 actually left unchanged, reset clip_changed. */
3844 if (obuf == current_buffer)
3846 if (begv == BEGV && zv == ZV)
3847 current_buffer->clip_changed = old_clip_changed;
3849 /* There isn't much we can reasonably do to protect against
3850 misbehaving fontification, but here's a fig leaf. */
3851 else if (BUFFER_LIVE_P (obuf))
3852 set_buffer_internal_1 (obuf);
3854 /* The fontification code may have added/removed text.
3855 It could do even a lot worse, but let's at least protect against
3856 the most obvious case where only the text past `pos' gets changed',
3857 as is/was done in grep.el where some escapes sequences are turned
3858 into face properties (bug#7876). */
3859 it->end_charpos = ZV;
3861 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3862 something. This avoids an endless loop if they failed to
3863 fontify the text for which reason ever. */
3864 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3865 handled = HANDLED_RECOMPUTE_PROPS;
3868 return handled;
3873 /***********************************************************************
3874 Faces
3875 ***********************************************************************/
3877 /* Set up iterator IT from face properties at its current position.
3878 Called from handle_stop. */
3880 static enum prop_handled
3881 handle_face_prop (struct it *it)
3883 int new_face_id;
3884 ptrdiff_t next_stop;
3886 if (!STRINGP (it->string))
3888 new_face_id
3889 = face_at_buffer_position (it->w,
3890 IT_CHARPOS (*it),
3891 &next_stop,
3892 (IT_CHARPOS (*it)
3893 + TEXT_PROP_DISTANCE_LIMIT),
3894 false, it->base_face_id);
3896 /* Is this a start of a run of characters with box face?
3897 Caveat: this can be called for a freshly initialized
3898 iterator; face_id is -1 in this case. We know that the new
3899 face will not change until limit, i.e. if the new face has a
3900 box, all characters up to limit will have one. But, as
3901 usual, we don't know whether limit is really the end. */
3902 if (new_face_id != it->face_id)
3904 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3905 /* If it->face_id is -1, old_face below will be NULL, see
3906 the definition of FACE_FROM_ID_OR_NULL. This will happen
3907 if this is the initial call that gets the face. */
3908 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3910 /* If the value of face_id of the iterator is -1, we have to
3911 look in front of IT's position and see whether there is a
3912 face there that's different from new_face_id. */
3913 if (!old_face && IT_CHARPOS (*it) > BEG)
3915 int prev_face_id = face_before_it_pos (it);
3917 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3920 /* If the new face has a box, but the old face does not,
3921 this is the start of a run of characters with box face,
3922 i.e. this character has a shadow on the left side. */
3923 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3924 && (old_face == NULL || !old_face->box));
3925 it->face_box_p = new_face->box != FACE_NO_BOX;
3928 else
3930 int base_face_id;
3931 ptrdiff_t bufpos;
3932 int i;
3933 Lisp_Object from_overlay
3934 = (it->current.overlay_string_index >= 0
3935 ? it->string_overlays[it->current.overlay_string_index
3936 % OVERLAY_STRING_CHUNK_SIZE]
3937 : Qnil);
3939 /* See if we got to this string directly or indirectly from
3940 an overlay property. That includes the before-string or
3941 after-string of an overlay, strings in display properties
3942 provided by an overlay, their text properties, etc.
3944 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3945 if (! NILP (from_overlay))
3946 for (i = it->sp - 1; i >= 0; i--)
3948 if (it->stack[i].current.overlay_string_index >= 0)
3949 from_overlay
3950 = it->string_overlays[it->stack[i].current.overlay_string_index
3951 % OVERLAY_STRING_CHUNK_SIZE];
3952 else if (! NILP (it->stack[i].from_overlay))
3953 from_overlay = it->stack[i].from_overlay;
3955 if (!NILP (from_overlay))
3956 break;
3959 if (! NILP (from_overlay))
3961 bufpos = IT_CHARPOS (*it);
3962 /* For a string from an overlay, the base face depends
3963 only on text properties and ignores overlays. */
3964 base_face_id
3965 = face_for_overlay_string (it->w,
3966 IT_CHARPOS (*it),
3967 &next_stop,
3968 (IT_CHARPOS (*it)
3969 + TEXT_PROP_DISTANCE_LIMIT),
3970 false,
3971 from_overlay);
3973 else
3975 bufpos = 0;
3977 /* For strings from a `display' property, use the face at
3978 IT's current buffer position as the base face to merge
3979 with, so that overlay strings appear in the same face as
3980 surrounding text, unless they specify their own faces.
3981 For strings from wrap-prefix and line-prefix properties,
3982 use the default face, possibly remapped via
3983 Vface_remapping_alist. */
3984 /* Note that the fact that we use the face at _buffer_
3985 position means that a 'display' property on an overlay
3986 string will not inherit the face of that overlay string,
3987 but will instead revert to the face of buffer text
3988 covered by the overlay. This is visible, e.g., when the
3989 overlay specifies a box face, but neither the buffer nor
3990 the display string do. This sounds like a design bug,
3991 but Emacs always did that since v21.1, so changing that
3992 might be a big deal. */
3993 base_face_id = it->string_from_prefix_prop_p
3994 ? (!NILP (Vface_remapping_alist)
3995 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3996 : DEFAULT_FACE_ID)
3997 : underlying_face_id (it);
4000 new_face_id = face_at_string_position (it->w,
4001 it->string,
4002 IT_STRING_CHARPOS (*it),
4003 bufpos,
4004 &next_stop,
4005 base_face_id, false);
4007 /* Is this a start of a run of characters with box? Caveat:
4008 this can be called for a freshly allocated iterator; face_id
4009 is -1 is this case. We know that the new face will not
4010 change until the next check pos, i.e. if the new face has a
4011 box, all characters up to that position will have a
4012 box. But, as usual, we don't know whether that position
4013 is really the end. */
4014 if (new_face_id != it->face_id)
4016 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4017 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4019 /* If new face has a box but old face hasn't, this is the
4020 start of a run of characters with box, i.e. it has a
4021 shadow on the left side. */
4022 it->start_of_box_run_p
4023 = new_face->box && (old_face == NULL || !old_face->box);
4024 it->face_box_p = new_face->box != FACE_NO_BOX;
4028 it->face_id = new_face_id;
4029 return HANDLED_NORMALLY;
4033 /* Return the ID of the face ``underlying'' IT's current position,
4034 which is in a string. If the iterator is associated with a
4035 buffer, return the face at IT's current buffer position.
4036 Otherwise, use the iterator's base_face_id. */
4038 static int
4039 underlying_face_id (struct it *it)
4041 int face_id = it->base_face_id, i;
4043 eassert (STRINGP (it->string));
4045 for (i = it->sp - 1; i >= 0; --i)
4046 if (NILP (it->stack[i].string))
4047 face_id = it->stack[i].face_id;
4049 return face_id;
4053 /* Compute the face one character before or after the current position
4054 of IT, in the visual order. BEFORE_P means get the face
4055 in front (to the left in L2R paragraphs, to the right in R2L
4056 paragraphs) of IT's screen position. Value is the ID of the face. */
4058 static int
4059 face_before_or_after_it_pos (struct it *it, bool before_p)
4061 int face_id, limit;
4062 ptrdiff_t next_check_charpos;
4063 struct it it_copy;
4064 void *it_copy_data = NULL;
4066 eassert (it->s == NULL);
4068 if (STRINGP (it->string))
4070 ptrdiff_t bufpos, charpos;
4071 int base_face_id;
4073 /* No face change past the end of the string (for the case
4074 we are padding with spaces). No face change before the
4075 string start. */
4076 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4077 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4078 return it->face_id;
4080 if (!it->bidi_p)
4082 /* Set charpos to the position before or after IT's current
4083 position, in the logical order, which in the non-bidi
4084 case is the same as the visual order. */
4085 if (before_p)
4086 charpos = IT_STRING_CHARPOS (*it) - 1;
4087 else if (it->what == IT_COMPOSITION)
4088 /* For composition, we must check the character after the
4089 composition. */
4090 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4091 else
4092 charpos = IT_STRING_CHARPOS (*it) + 1;
4094 else
4096 if (before_p)
4098 /* With bidi iteration, the character before the current
4099 in the visual order cannot be found by simple
4100 iteration, because "reverse" reordering is not
4101 supported. Instead, we need to start from the string
4102 beginning and go all the way to the current string
4103 position, remembering the previous position. */
4104 /* Ignore face changes before the first visible
4105 character on this display line. */
4106 if (it->current_x <= it->first_visible_x)
4107 return it->face_id;
4108 SAVE_IT (it_copy, *it, it_copy_data);
4109 IT_STRING_CHARPOS (it_copy) = 0;
4110 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4114 charpos = IT_STRING_CHARPOS (it_copy);
4115 if (charpos >= SCHARS (it->string))
4116 break;
4117 bidi_move_to_visually_next (&it_copy.bidi_it);
4119 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4121 RESTORE_IT (it, it, it_copy_data);
4123 else
4125 /* Set charpos to the string position of the character
4126 that comes after IT's current position in the visual
4127 order. */
4128 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4130 it_copy = *it;
4131 while (n--)
4132 bidi_move_to_visually_next (&it_copy.bidi_it);
4134 charpos = it_copy.bidi_it.charpos;
4137 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4139 if (it->current.overlay_string_index >= 0)
4140 bufpos = IT_CHARPOS (*it);
4141 else
4142 bufpos = 0;
4144 base_face_id = underlying_face_id (it);
4146 /* Get the face for ASCII, or unibyte. */
4147 face_id = face_at_string_position (it->w,
4148 it->string,
4149 charpos,
4150 bufpos,
4151 &next_check_charpos,
4152 base_face_id, false);
4154 /* Correct the face for charsets different from ASCII. Do it
4155 for the multibyte case only. The face returned above is
4156 suitable for unibyte text if IT->string is unibyte. */
4157 if (STRING_MULTIBYTE (it->string))
4159 struct text_pos pos1 = string_pos (charpos, it->string);
4160 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4161 int c, len;
4162 struct face *face = FACE_FROM_ID (it->f, face_id);
4164 c = string_char_and_length (p, &len);
4165 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4168 else
4170 struct text_pos pos;
4172 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4173 || (IT_CHARPOS (*it) <= BEGV && before_p))
4174 return it->face_id;
4176 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4177 pos = it->current.pos;
4179 if (!it->bidi_p)
4181 if (before_p)
4182 DEC_TEXT_POS (pos, it->multibyte_p);
4183 else
4185 if (it->what == IT_COMPOSITION)
4187 /* For composition, we must check the position after
4188 the composition. */
4189 pos.charpos += it->cmp_it.nchars;
4190 pos.bytepos += it->len;
4192 else
4193 INC_TEXT_POS (pos, it->multibyte_p);
4196 else
4198 if (before_p)
4200 int current_x;
4202 /* With bidi iteration, the character before the current
4203 in the visual order cannot be found by simple
4204 iteration, because "reverse" reordering is not
4205 supported. Instead, we need to use the move_it_*
4206 family of functions, and move to the previous
4207 character starting from the beginning of the visual
4208 line. */
4209 /* Ignore face changes before the first visible
4210 character on this display line. */
4211 if (it->current_x <= it->first_visible_x)
4212 return it->face_id;
4213 SAVE_IT (it_copy, *it, it_copy_data);
4214 /* Implementation note: Since move_it_in_display_line
4215 works in the iterator geometry, and thinks the first
4216 character is always the leftmost, even in R2L lines,
4217 we don't need to distinguish between the R2L and L2R
4218 cases here. */
4219 current_x = it_copy.current_x;
4220 move_it_vertically_backward (&it_copy, 0);
4221 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4222 pos = it_copy.current.pos;
4223 RESTORE_IT (it, it, it_copy_data);
4225 else
4227 /* Set charpos to the buffer position of the character
4228 that comes after IT's current position in the visual
4229 order. */
4230 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4232 it_copy = *it;
4233 while (n--)
4234 bidi_move_to_visually_next (&it_copy.bidi_it);
4236 SET_TEXT_POS (pos,
4237 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4240 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4242 /* Determine face for CHARSET_ASCII, or unibyte. */
4243 face_id = face_at_buffer_position (it->w,
4244 CHARPOS (pos),
4245 &next_check_charpos,
4246 limit, false, -1);
4248 /* Correct the face for charsets different from ASCII. Do it
4249 for the multibyte case only. The face returned above is
4250 suitable for unibyte text if current_buffer is unibyte. */
4251 if (it->multibyte_p)
4253 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4254 struct face *face = FACE_FROM_ID (it->f, face_id);
4255 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4259 return face_id;
4264 /***********************************************************************
4265 Invisible text
4266 ***********************************************************************/
4268 /* Set up iterator IT from invisible properties at its current
4269 position. Called from handle_stop. */
4271 static enum prop_handled
4272 handle_invisible_prop (struct it *it)
4274 enum prop_handled handled = HANDLED_NORMALLY;
4275 int invis;
4276 Lisp_Object prop;
4278 if (STRINGP (it->string))
4280 Lisp_Object end_charpos, limit;
4282 /* Get the value of the invisible text property at the
4283 current position. Value will be nil if there is no such
4284 property. */
4285 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4286 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4287 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4289 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4291 /* Record whether we have to display an ellipsis for the
4292 invisible text. */
4293 bool display_ellipsis_p = (invis == 2);
4294 ptrdiff_t len, endpos;
4296 handled = HANDLED_RECOMPUTE_PROPS;
4298 /* Get the position at which the next visible text can be
4299 found in IT->string, if any. */
4300 endpos = len = SCHARS (it->string);
4301 XSETINT (limit, len);
4304 end_charpos
4305 = Fnext_single_property_change (end_charpos, Qinvisible,
4306 it->string, limit);
4307 /* Since LIMIT is always an integer, so should be the
4308 value returned by Fnext_single_property_change. */
4309 eassert (INTEGERP (end_charpos));
4310 if (INTEGERP (end_charpos))
4312 endpos = XFASTINT (end_charpos);
4313 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4314 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4315 if (invis == 2)
4316 display_ellipsis_p = true;
4318 else /* Should never happen; but if it does, exit the loop. */
4319 endpos = len;
4321 while (invis != 0 && endpos < len);
4323 if (display_ellipsis_p)
4324 it->ellipsis_p = true;
4326 if (endpos < len)
4328 /* Text at END_CHARPOS is visible. Move IT there. */
4329 struct text_pos old;
4330 ptrdiff_t oldpos;
4332 old = it->current.string_pos;
4333 oldpos = CHARPOS (old);
4334 if (it->bidi_p)
4336 if (it->bidi_it.first_elt
4337 && it->bidi_it.charpos < SCHARS (it->string))
4338 bidi_paragraph_init (it->paragraph_embedding,
4339 &it->bidi_it, true);
4340 /* Bidi-iterate out of the invisible text. */
4343 bidi_move_to_visually_next (&it->bidi_it);
4345 while (oldpos <= it->bidi_it.charpos
4346 && it->bidi_it.charpos < endpos
4347 && it->bidi_it.charpos < it->bidi_it.string.schars);
4349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4351 if (IT_CHARPOS (*it) >= endpos)
4352 it->prev_stop = endpos;
4354 else
4356 IT_STRING_CHARPOS (*it) = endpos;
4357 compute_string_pos (&it->current.string_pos, old, it->string);
4360 else
4362 /* The rest of the string is invisible. If this is an
4363 overlay string, proceed with the next overlay string
4364 or whatever comes and return a character from there. */
4365 if (it->current.overlay_string_index >= 0
4366 && !display_ellipsis_p)
4368 next_overlay_string (it);
4369 /* Don't check for overlay strings when we just
4370 finished processing them. */
4371 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4373 else
4375 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4376 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4381 else
4383 ptrdiff_t newpos, next_stop, start_charpos, tem;
4384 Lisp_Object pos, overlay;
4386 /* First of all, is there invisible text at this position? */
4387 tem = start_charpos = IT_CHARPOS (*it);
4388 pos = make_number (tem);
4389 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4390 &overlay);
4391 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4393 /* If we are on invisible text, skip over it. */
4394 if (invis != 0 && start_charpos < it->end_charpos)
4396 /* Record whether we have to display an ellipsis for the
4397 invisible text. */
4398 bool display_ellipsis_p = invis == 2;
4400 handled = HANDLED_RECOMPUTE_PROPS;
4402 /* Loop skipping over invisible text. The loop is left at
4403 ZV or with IT on the first char being visible again. */
4406 /* Try to skip some invisible text. Return value is the
4407 position reached which can be equal to where we start
4408 if there is nothing invisible there. This skips both
4409 over invisible text properties and overlays with
4410 invisible property. */
4411 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4413 /* If we skipped nothing at all we weren't at invisible
4414 text in the first place. If everything to the end of
4415 the buffer was skipped, end the loop. */
4416 if (newpos == tem || newpos >= ZV)
4417 invis = 0;
4418 else
4420 /* We skipped some characters but not necessarily
4421 all there are. Check if we ended up on visible
4422 text. Fget_char_property returns the property of
4423 the char before the given position, i.e. if we
4424 get invis = 0, this means that the char at
4425 newpos is visible. */
4426 pos = make_number (newpos);
4427 prop = Fget_char_property (pos, Qinvisible, it->window);
4428 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4431 /* If we ended up on invisible text, proceed to
4432 skip starting with next_stop. */
4433 if (invis != 0)
4434 tem = next_stop;
4436 /* If there are adjacent invisible texts, don't lose the
4437 second one's ellipsis. */
4438 if (invis == 2)
4439 display_ellipsis_p = true;
4441 while (invis != 0);
4443 /* The position newpos is now either ZV or on visible text. */
4444 if (it->bidi_p)
4446 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4447 bool on_newline
4448 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4449 bool after_newline
4450 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4452 /* If the invisible text ends on a newline or on a
4453 character after a newline, we can avoid the costly,
4454 character by character, bidi iteration to NEWPOS, and
4455 instead simply reseat the iterator there. That's
4456 because all bidi reordering information is tossed at
4457 the newline. This is a big win for modes that hide
4458 complete lines, like Outline, Org, etc. */
4459 if (on_newline || after_newline)
4461 struct text_pos tpos;
4462 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4464 SET_TEXT_POS (tpos, newpos, bpos);
4465 reseat_1 (it, tpos, false);
4466 /* If we reseat on a newline/ZV, we need to prep the
4467 bidi iterator for advancing to the next character
4468 after the newline/EOB, keeping the current paragraph
4469 direction (so that PRODUCE_GLYPHS does TRT wrt
4470 prepending/appending glyphs to a glyph row). */
4471 if (on_newline)
4473 it->bidi_it.first_elt = false;
4474 it->bidi_it.paragraph_dir = pdir;
4475 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4476 it->bidi_it.nchars = 1;
4477 it->bidi_it.ch_len = 1;
4480 else /* Must use the slow method. */
4482 /* With bidi iteration, the region of invisible text
4483 could start and/or end in the middle of a
4484 non-base embedding level. Therefore, we need to
4485 skip invisible text using the bidi iterator,
4486 starting at IT's current position, until we find
4487 ourselves outside of the invisible text.
4488 Skipping invisible text _after_ bidi iteration
4489 avoids affecting the visual order of the
4490 displayed text when invisible properties are
4491 added or removed. */
4492 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4494 /* If we were `reseat'ed to a new paragraph,
4495 determine the paragraph base direction. We
4496 need to do it now because
4497 next_element_from_buffer may not have a
4498 chance to do it, if we are going to skip any
4499 text at the beginning, which resets the
4500 FIRST_ELT flag. */
4501 bidi_paragraph_init (it->paragraph_embedding,
4502 &it->bidi_it, true);
4506 bidi_move_to_visually_next (&it->bidi_it);
4508 while (it->stop_charpos <= it->bidi_it.charpos
4509 && it->bidi_it.charpos < newpos);
4510 IT_CHARPOS (*it) = it->bidi_it.charpos;
4511 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4512 /* If we overstepped NEWPOS, record its position in
4513 the iterator, so that we skip invisible text if
4514 later the bidi iteration lands us in the
4515 invisible region again. */
4516 if (IT_CHARPOS (*it) >= newpos)
4517 it->prev_stop = newpos;
4520 else
4522 IT_CHARPOS (*it) = newpos;
4523 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4526 if (display_ellipsis_p)
4528 /* Make sure that the glyphs of the ellipsis will get
4529 correct `charpos' values. If we would not update
4530 it->position here, the glyphs would belong to the
4531 last visible character _before_ the invisible
4532 text, which confuses `set_cursor_from_row'.
4534 We use the last invisible position instead of the
4535 first because this way the cursor is always drawn on
4536 the first "." of the ellipsis, whenever PT is inside
4537 the invisible text. Otherwise the cursor would be
4538 placed _after_ the ellipsis when the point is after the
4539 first invisible character. */
4540 if (!STRINGP (it->object))
4542 it->position.charpos = newpos - 1;
4543 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4547 /* If there are before-strings at the start of invisible
4548 text, and the text is invisible because of a text
4549 property, arrange to show before-strings because 20.x did
4550 it that way. (If the text is invisible because of an
4551 overlay property instead of a text property, this is
4552 already handled in the overlay code.) */
4553 if (NILP (overlay)
4554 && get_overlay_strings (it, it->stop_charpos))
4556 handled = HANDLED_RECOMPUTE_PROPS;
4557 if (it->sp > 0)
4559 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4560 /* The call to get_overlay_strings above recomputes
4561 it->stop_charpos, but it only considers changes
4562 in properties and overlays beyond iterator's
4563 current position. This causes us to miss changes
4564 that happen exactly where the invisible property
4565 ended. So we play it safe here and force the
4566 iterator to check for potential stop positions
4567 immediately after the invisible text. Note that
4568 if get_overlay_strings returns true, it
4569 normally also pushed the iterator stack, so we
4570 need to update the stop position in the slot
4571 below the current one. */
4572 it->stack[it->sp - 1].stop_charpos
4573 = CHARPOS (it->stack[it->sp - 1].current.pos);
4576 else if (display_ellipsis_p)
4578 it->ellipsis_p = true;
4579 /* Let the ellipsis display before
4580 considering any properties of the following char.
4581 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4582 handled = HANDLED_RETURN;
4587 return handled;
4591 /* Make iterator IT return `...' next.
4592 Replaces LEN characters from buffer. */
4594 static void
4595 setup_for_ellipsis (struct it *it, int len)
4597 /* Use the display table definition for `...'. Invalid glyphs
4598 will be handled by the method returning elements from dpvec. */
4599 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4601 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4602 it->dpvec = v->contents;
4603 it->dpend = v->contents + v->header.size;
4605 else
4607 /* Default `...'. */
4608 it->dpvec = default_invis_vector;
4609 it->dpend = default_invis_vector + 3;
4612 it->dpvec_char_len = len;
4613 it->current.dpvec_index = 0;
4614 it->dpvec_face_id = -1;
4616 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4617 face as the preceding text. IT->saved_face_id was set in
4618 handle_stop to the face of the preceding character, and will be
4619 different from IT->face_id only if the invisible text skipped in
4620 handle_invisible_prop has some non-default face on its first
4621 character. We thus ignore the face of the invisible text when we
4622 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4623 if (it->saved_face_id >= 0)
4624 it->face_id = it->saved_face_id;
4626 /* If the ellipsis represents buffer text, it means we advanced in
4627 the buffer, so we should no longer ignore overlay strings. */
4628 if (it->method == GET_FROM_BUFFER)
4629 it->ignore_overlay_strings_at_pos_p = false;
4631 it->method = GET_FROM_DISPLAY_VECTOR;
4632 it->ellipsis_p = true;
4637 /***********************************************************************
4638 'display' property
4639 ***********************************************************************/
4641 /* Set up iterator IT from `display' property at its current position.
4642 Called from handle_stop.
4643 We return HANDLED_RETURN if some part of the display property
4644 overrides the display of the buffer text itself.
4645 Otherwise we return HANDLED_NORMALLY. */
4647 static enum prop_handled
4648 handle_display_prop (struct it *it)
4650 Lisp_Object propval, object, overlay;
4651 struct text_pos *position;
4652 ptrdiff_t bufpos;
4653 /* Nonzero if some property replaces the display of the text itself. */
4654 int display_replaced = 0;
4656 if (STRINGP (it->string))
4658 object = it->string;
4659 position = &it->current.string_pos;
4660 bufpos = CHARPOS (it->current.pos);
4662 else
4664 XSETWINDOW (object, it->w);
4665 position = &it->current.pos;
4666 bufpos = CHARPOS (*position);
4669 /* Reset those iterator values set from display property values. */
4670 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4671 it->space_width = Qnil;
4672 it->font_height = Qnil;
4673 it->voffset = 0;
4675 /* We don't support recursive `display' properties, i.e. string
4676 values that have a string `display' property, that have a string
4677 `display' property etc. */
4678 if (!it->string_from_display_prop_p)
4679 it->area = TEXT_AREA;
4681 propval = get_char_property_and_overlay (make_number (position->charpos),
4682 Qdisplay, object, &overlay);
4683 if (NILP (propval))
4684 return HANDLED_NORMALLY;
4685 /* Now OVERLAY is the overlay that gave us this property, or nil
4686 if it was a text property. */
4688 if (!STRINGP (it->string))
4689 object = it->w->contents;
4691 display_replaced = handle_display_spec (it, propval, object, overlay,
4692 position, bufpos,
4693 FRAME_WINDOW_P (it->f));
4694 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4697 /* Subroutine of handle_display_prop. Returns non-zero if the display
4698 specification in SPEC is a replacing specification, i.e. it would
4699 replace the text covered by `display' property with something else,
4700 such as an image or a display string. If SPEC includes any kind or
4701 `(space ...) specification, the value is 2; this is used by
4702 compute_display_string_pos, which see.
4704 See handle_single_display_spec for documentation of arguments.
4705 FRAME_WINDOW_P is true if the window being redisplayed is on a
4706 GUI frame; this argument is used only if IT is NULL, see below.
4708 IT can be NULL, if this is called by the bidi reordering code
4709 through compute_display_string_pos, which see. In that case, this
4710 function only examines SPEC, but does not otherwise "handle" it, in
4711 the sense that it doesn't set up members of IT from the display
4712 spec. */
4713 static int
4714 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4715 Lisp_Object overlay, struct text_pos *position,
4716 ptrdiff_t bufpos, bool frame_window_p)
4718 int replacing = 0;
4720 if (CONSP (spec)
4721 /* Simple specifications. */
4722 && !EQ (XCAR (spec), Qimage)
4723 #ifdef HAVE_XWIDGETS
4724 && !EQ (XCAR (spec), Qxwidget)
4725 #endif
4726 && !EQ (XCAR (spec), Qspace)
4727 && !EQ (XCAR (spec), Qwhen)
4728 && !EQ (XCAR (spec), Qslice)
4729 && !EQ (XCAR (spec), Qspace_width)
4730 && !EQ (XCAR (spec), Qheight)
4731 && !EQ (XCAR (spec), Qraise)
4732 /* Marginal area specifications. */
4733 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4734 && !EQ (XCAR (spec), Qleft_fringe)
4735 && !EQ (XCAR (spec), Qright_fringe)
4736 && !NILP (XCAR (spec)))
4738 for (; CONSP (spec); spec = XCDR (spec))
4740 int rv = handle_single_display_spec (it, XCAR (spec), object,
4741 overlay, position, bufpos,
4742 replacing, frame_window_p);
4743 if (rv != 0)
4745 replacing = rv;
4746 /* If some text in a string is replaced, `position' no
4747 longer points to the position of `object'. */
4748 if (!it || STRINGP (object))
4749 break;
4753 else if (VECTORP (spec))
4755 ptrdiff_t i;
4756 for (i = 0; i < ASIZE (spec); ++i)
4758 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4759 overlay, position, bufpos,
4760 replacing, frame_window_p);
4761 if (rv != 0)
4763 replacing = rv;
4764 /* If some text in a string is replaced, `position' no
4765 longer points to the position of `object'. */
4766 if (!it || STRINGP (object))
4767 break;
4771 else
4772 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4773 bufpos, 0, frame_window_p);
4774 return replacing;
4777 /* Value is the position of the end of the `display' property starting
4778 at START_POS in OBJECT. */
4780 static struct text_pos
4781 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4783 Lisp_Object end;
4784 struct text_pos end_pos;
4786 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4787 Qdisplay, object, Qnil);
4788 CHARPOS (end_pos) = XFASTINT (end);
4789 if (STRINGP (object))
4790 compute_string_pos (&end_pos, start_pos, it->string);
4791 else
4792 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4794 return end_pos;
4798 /* Set up IT from a single `display' property specification SPEC. OBJECT
4799 is the object in which the `display' property was found. *POSITION
4800 is the position in OBJECT at which the `display' property was found.
4801 BUFPOS is the buffer position of OBJECT (different from POSITION if
4802 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4803 previously saw a display specification which already replaced text
4804 display with something else, for example an image; we ignore such
4805 properties after the first one has been processed.
4807 OVERLAY is the overlay this `display' property came from,
4808 or nil if it was a text property.
4810 If SPEC is a `space' or `image' specification, and in some other
4811 cases too, set *POSITION to the position where the `display'
4812 property ends.
4814 If IT is NULL, only examine the property specification in SPEC, but
4815 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4816 is intended to be displayed in a window on a GUI frame.
4818 Value is non-zero if something was found which replaces the display
4819 of buffer or string text. */
4821 static int
4822 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4823 Lisp_Object overlay, struct text_pos *position,
4824 ptrdiff_t bufpos, int display_replaced,
4825 bool frame_window_p)
4827 Lisp_Object form;
4828 Lisp_Object location, value;
4829 struct text_pos start_pos = *position;
4831 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4832 If the result is non-nil, use VALUE instead of SPEC. */
4833 form = Qt;
4834 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4836 spec = XCDR (spec);
4837 if (!CONSP (spec))
4838 return 0;
4839 form = XCAR (spec);
4840 spec = XCDR (spec);
4843 if (!NILP (form) && !EQ (form, Qt))
4845 ptrdiff_t count = SPECPDL_INDEX ();
4847 /* Bind `object' to the object having the `display' property, a
4848 buffer or string. Bind `position' to the position in the
4849 object where the property was found, and `buffer-position'
4850 to the current position in the buffer. */
4852 if (NILP (object))
4853 XSETBUFFER (object, current_buffer);
4854 specbind (Qobject, object);
4855 specbind (Qposition, make_number (CHARPOS (*position)));
4856 specbind (Qbuffer_position, make_number (bufpos));
4857 form = safe_eval (form);
4858 unbind_to (count, Qnil);
4861 if (NILP (form))
4862 return 0;
4864 /* Handle `(height HEIGHT)' specifications. */
4865 if (CONSP (spec)
4866 && EQ (XCAR (spec), Qheight)
4867 && CONSP (XCDR (spec)))
4869 if (it)
4871 if (!FRAME_WINDOW_P (it->f))
4872 return 0;
4874 it->font_height = XCAR (XCDR (spec));
4875 if (!NILP (it->font_height))
4877 int new_height = -1;
4879 if (CONSP (it->font_height)
4880 && (EQ (XCAR (it->font_height), Qplus)
4881 || EQ (XCAR (it->font_height), Qminus))
4882 && CONSP (XCDR (it->font_height))
4883 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4885 /* `(+ N)' or `(- N)' where N is an integer. */
4886 int steps = XINT (XCAR (XCDR (it->font_height)));
4887 if (EQ (XCAR (it->font_height), Qplus))
4888 steps = - steps;
4889 it->face_id = smaller_face (it->f, it->face_id, steps);
4891 else if (FUNCTIONP (it->font_height))
4893 /* Call function with current height as argument.
4894 Value is the new height. */
4895 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4896 Lisp_Object height;
4897 height = safe_call1 (it->font_height,
4898 face->lface[LFACE_HEIGHT_INDEX]);
4899 if (NUMBERP (height))
4900 new_height = XFLOATINT (height);
4902 else if (NUMBERP (it->font_height))
4904 /* Value is a multiple of the canonical char height. */
4905 struct face *f;
4907 f = FACE_FROM_ID (it->f,
4908 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4909 new_height = (XFLOATINT (it->font_height)
4910 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4912 else
4914 /* Evaluate IT->font_height with `height' bound to the
4915 current specified height to get the new height. */
4916 ptrdiff_t count = SPECPDL_INDEX ();
4917 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4919 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4920 value = safe_eval (it->font_height);
4921 unbind_to (count, Qnil);
4923 if (NUMBERP (value))
4924 new_height = XFLOATINT (value);
4927 if (new_height > 0)
4928 it->face_id = face_with_height (it->f, it->face_id, new_height);
4932 return 0;
4935 /* Handle `(space-width WIDTH)'. */
4936 if (CONSP (spec)
4937 && EQ (XCAR (spec), Qspace_width)
4938 && CONSP (XCDR (spec)))
4940 if (it)
4942 if (!FRAME_WINDOW_P (it->f))
4943 return 0;
4945 value = XCAR (XCDR (spec));
4946 if (NUMBERP (value) && XFLOATINT (value) > 0)
4947 it->space_width = value;
4950 return 0;
4953 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4954 if (CONSP (spec)
4955 && EQ (XCAR (spec), Qslice))
4957 Lisp_Object tem;
4959 if (it)
4961 if (!FRAME_WINDOW_P (it->f))
4962 return 0;
4964 if (tem = XCDR (spec), CONSP (tem))
4966 it->slice.x = XCAR (tem);
4967 if (tem = XCDR (tem), CONSP (tem))
4969 it->slice.y = XCAR (tem);
4970 if (tem = XCDR (tem), CONSP (tem))
4972 it->slice.width = XCAR (tem);
4973 if (tem = XCDR (tem), CONSP (tem))
4974 it->slice.height = XCAR (tem);
4980 return 0;
4983 /* Handle `(raise FACTOR)'. */
4984 if (CONSP (spec)
4985 && EQ (XCAR (spec), Qraise)
4986 && CONSP (XCDR (spec)))
4988 if (it)
4990 if (!FRAME_WINDOW_P (it->f))
4991 return 0;
4993 #ifdef HAVE_WINDOW_SYSTEM
4994 value = XCAR (XCDR (spec));
4995 if (NUMBERP (value))
4997 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4998 it->voffset = - (XFLOATINT (value)
4999 * (normal_char_height (face->font, -1)));
5001 #endif /* HAVE_WINDOW_SYSTEM */
5004 return 0;
5007 /* Don't handle the other kinds of display specifications
5008 inside a string that we got from a `display' property. */
5009 if (it && it->string_from_display_prop_p)
5010 return 0;
5012 /* Characters having this form of property are not displayed, so
5013 we have to find the end of the property. */
5014 if (it)
5016 start_pos = *position;
5017 *position = display_prop_end (it, object, start_pos);
5018 /* If the display property comes from an overlay, don't consider
5019 any potential stop_charpos values before the end of that
5020 overlay. Since display_prop_end will happily find another
5021 'display' property coming from some other overlay or text
5022 property on buffer positions before this overlay's end, we
5023 need to ignore them, or else we risk displaying this
5024 overlay's display string/image twice. */
5025 if (!NILP (overlay))
5027 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5029 /* Some borderline-sane Lisp might call us with the current
5030 buffer narrowed so that overlay-end is outside the
5031 POINT_MIN..POINT_MAX region, which will then cause
5032 various assertion violations and crashes down the road,
5033 starting with pop_it when it will attempt to use POSITION
5034 set below. Prevent that. */
5035 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5037 if (ovendpos > CHARPOS (*position))
5038 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5041 value = Qnil;
5043 /* Stop the scan at that end position--we assume that all
5044 text properties change there. */
5045 if (it)
5046 it->stop_charpos = position->charpos;
5048 /* Handle `(left-fringe BITMAP [FACE])'
5049 and `(right-fringe BITMAP [FACE])'. */
5050 if (CONSP (spec)
5051 && (EQ (XCAR (spec), Qleft_fringe)
5052 || EQ (XCAR (spec), Qright_fringe))
5053 && CONSP (XCDR (spec)))
5055 if (it)
5057 if (!FRAME_WINDOW_P (it->f))
5058 /* If we return here, POSITION has been advanced
5059 across the text with this property. */
5061 /* Synchronize the bidi iterator with POSITION. This is
5062 needed because we are not going to push the iterator
5063 on behalf of this display property, so there will be
5064 no pop_it call to do this synchronization for us. */
5065 if (it->bidi_p)
5067 it->position = *position;
5068 iterate_out_of_display_property (it);
5069 *position = it->position;
5071 return 1;
5074 else if (!frame_window_p)
5075 return 1;
5077 #ifdef HAVE_WINDOW_SYSTEM
5078 value = XCAR (XCDR (spec));
5079 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5080 if (! fringe_bitmap)
5081 /* If we return here, POSITION has been advanced
5082 across the text with this property. */
5084 if (it && it->bidi_p)
5086 it->position = *position;
5087 iterate_out_of_display_property (it);
5088 *position = it->position;
5090 return 1;
5093 if (it)
5095 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5097 if (CONSP (XCDR (XCDR (spec))))
5099 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5100 int face_id2 = lookup_derived_face (it->f, face_name,
5101 FRINGE_FACE_ID, false);
5102 if (face_id2 >= 0)
5103 face_id = face_id2;
5106 /* Save current settings of IT so that we can restore them
5107 when we are finished with the glyph property value. */
5108 push_it (it, position);
5110 it->area = TEXT_AREA;
5111 it->what = IT_IMAGE;
5112 it->image_id = -1; /* no image */
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5116 it->from_overlay = Qnil;
5117 it->face_id = face_id;
5118 it->from_disp_prop_p = true;
5120 /* Say that we haven't consumed the characters with
5121 `display' property yet. The call to pop_it in
5122 set_iterator_to_next will clean this up. */
5123 *position = start_pos;
5125 if (EQ (XCAR (spec), Qleft_fringe))
5127 it->left_user_fringe_bitmap = fringe_bitmap;
5128 it->left_user_fringe_face_id = face_id;
5130 else
5132 it->right_user_fringe_bitmap = fringe_bitmap;
5133 it->right_user_fringe_face_id = face_id;
5136 #endif /* HAVE_WINDOW_SYSTEM */
5137 return 1;
5140 /* Prepare to handle `((margin left-margin) ...)',
5141 `((margin right-margin) ...)' and `((margin nil) ...)'
5142 prefixes for display specifications. */
5143 location = Qunbound;
5144 if (CONSP (spec) && CONSP (XCAR (spec)))
5146 Lisp_Object tem;
5148 value = XCDR (spec);
5149 if (CONSP (value))
5150 value = XCAR (value);
5152 tem = XCAR (spec);
5153 if (EQ (XCAR (tem), Qmargin)
5154 && (tem = XCDR (tem),
5155 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5156 (NILP (tem)
5157 || EQ (tem, Qleft_margin)
5158 || EQ (tem, Qright_margin))))
5159 location = tem;
5162 if (EQ (location, Qunbound))
5164 location = Qnil;
5165 value = spec;
5168 /* After this point, VALUE is the property after any
5169 margin prefix has been stripped. It must be a string,
5170 an image specification, or `(space ...)'.
5172 LOCATION specifies where to display: `left-margin',
5173 `right-margin' or nil. */
5175 bool valid_p = (STRINGP (value)
5176 #ifdef HAVE_WINDOW_SYSTEM
5177 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5178 && valid_image_p (value))
5179 #endif /* not HAVE_WINDOW_SYSTEM */
5180 || (CONSP (value) && EQ (XCAR (value), Qspace))
5181 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5182 && valid_xwidget_spec_p (value)));
5184 if (valid_p && display_replaced == 0)
5186 int retval = 1;
5188 if (!it)
5190 /* Callers need to know whether the display spec is any kind
5191 of `(space ...)' spec that is about to affect text-area
5192 display. */
5193 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5194 retval = 2;
5195 return retval;
5198 /* Save current settings of IT so that we can restore them
5199 when we are finished with the glyph property value. */
5200 push_it (it, position);
5201 it->from_overlay = overlay;
5202 it->from_disp_prop_p = true;
5204 if (NILP (location))
5205 it->area = TEXT_AREA;
5206 else if (EQ (location, Qleft_margin))
5207 it->area = LEFT_MARGIN_AREA;
5208 else
5209 it->area = RIGHT_MARGIN_AREA;
5211 if (STRINGP (value))
5213 it->string = value;
5214 it->multibyte_p = STRING_MULTIBYTE (it->string);
5215 it->current.overlay_string_index = -1;
5216 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5217 it->end_charpos = it->string_nchars = SCHARS (it->string);
5218 it->method = GET_FROM_STRING;
5219 it->stop_charpos = 0;
5220 it->prev_stop = 0;
5221 it->base_level_stop = 0;
5222 it->string_from_display_prop_p = true;
5223 /* Say that we haven't consumed the characters with
5224 `display' property yet. The call to pop_it in
5225 set_iterator_to_next will clean this up. */
5226 if (BUFFERP (object))
5227 *position = start_pos;
5229 /* Force paragraph direction to be that of the parent
5230 object. If the parent object's paragraph direction is
5231 not yet determined, default to L2R. */
5232 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5233 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5234 else
5235 it->paragraph_embedding = L2R;
5237 /* Set up the bidi iterator for this display string. */
5238 if (it->bidi_p)
5240 it->bidi_it.string.lstring = it->string;
5241 it->bidi_it.string.s = NULL;
5242 it->bidi_it.string.schars = it->end_charpos;
5243 it->bidi_it.string.bufpos = bufpos;
5244 it->bidi_it.string.from_disp_str = true;
5245 it->bidi_it.string.unibyte = !it->multibyte_p;
5246 it->bidi_it.w = it->w;
5247 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5250 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5252 it->method = GET_FROM_STRETCH;
5253 it->object = value;
5254 *position = it->position = start_pos;
5255 retval = 1 + (it->area == TEXT_AREA);
5257 else if (valid_xwidget_spec_p (value))
5259 it->what = IT_XWIDGET;
5260 it->method = GET_FROM_XWIDGET;
5261 it->position = start_pos;
5262 it->object = NILP (object) ? it->w->contents : object;
5263 *position = start_pos;
5264 it->xwidget = lookup_xwidget (value);
5266 #ifdef HAVE_WINDOW_SYSTEM
5267 else
5269 it->what = IT_IMAGE;
5270 it->image_id = lookup_image (it->f, value);
5271 it->position = start_pos;
5272 it->object = NILP (object) ? it->w->contents : object;
5273 it->method = GET_FROM_IMAGE;
5275 /* Say that we haven't consumed the characters with
5276 `display' property yet. The call to pop_it in
5277 set_iterator_to_next will clean this up. */
5278 *position = start_pos;
5280 #endif /* HAVE_WINDOW_SYSTEM */
5282 return retval;
5285 /* Invalid property or property not supported. Restore
5286 POSITION to what it was before. */
5287 *position = start_pos;
5288 return 0;
5291 /* Check if PROP is a display property value whose text should be
5292 treated as intangible. OVERLAY is the overlay from which PROP
5293 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5294 specify the buffer position covered by PROP. */
5296 bool
5297 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5298 ptrdiff_t charpos, ptrdiff_t bytepos)
5300 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5301 struct text_pos position;
5303 SET_TEXT_POS (position, charpos, bytepos);
5304 return (handle_display_spec (NULL, prop, Qnil, overlay,
5305 &position, charpos, frame_window_p)
5306 != 0);
5310 /* Return true if PROP is a display sub-property value containing STRING.
5312 Implementation note: this and the following function are really
5313 special cases of handle_display_spec and
5314 handle_single_display_spec, and should ideally use the same code.
5315 Until they do, these two pairs must be consistent and must be
5316 modified in sync. */
5318 static bool
5319 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5321 if (EQ (string, prop))
5322 return true;
5324 /* Skip over `when FORM'. */
5325 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5327 prop = XCDR (prop);
5328 if (!CONSP (prop))
5329 return false;
5330 /* Actually, the condition following `when' should be eval'ed,
5331 like handle_single_display_spec does, and we should return
5332 false if it evaluates to nil. However, this function is
5333 called only when the buffer was already displayed and some
5334 glyph in the glyph matrix was found to come from a display
5335 string. Therefore, the condition was already evaluated, and
5336 the result was non-nil, otherwise the display string wouldn't
5337 have been displayed and we would have never been called for
5338 this property. Thus, we can skip the evaluation and assume
5339 its result is non-nil. */
5340 prop = XCDR (prop);
5343 if (CONSP (prop))
5344 /* Skip over `margin LOCATION'. */
5345 if (EQ (XCAR (prop), Qmargin))
5347 prop = XCDR (prop);
5348 if (!CONSP (prop))
5349 return false;
5351 prop = XCDR (prop);
5352 if (!CONSP (prop))
5353 return false;
5356 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5360 /* Return true if STRING appears in the `display' property PROP. */
5362 static bool
5363 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5365 if (CONSP (prop)
5366 && !EQ (XCAR (prop), Qwhen)
5367 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5369 /* A list of sub-properties. */
5370 while (CONSP (prop))
5372 if (single_display_spec_string_p (XCAR (prop), string))
5373 return true;
5374 prop = XCDR (prop);
5377 else if (VECTORP (prop))
5379 /* A vector of sub-properties. */
5380 ptrdiff_t i;
5381 for (i = 0; i < ASIZE (prop); ++i)
5382 if (single_display_spec_string_p (AREF (prop, i), string))
5383 return true;
5385 else
5386 return single_display_spec_string_p (prop, string);
5388 return false;
5391 /* Look for STRING in overlays and text properties in the current
5392 buffer, between character positions FROM and TO (excluding TO).
5393 BACK_P means look back (in this case, TO is supposed to be
5394 less than FROM).
5395 Value is the first character position where STRING was found, or
5396 zero if it wasn't found before hitting TO.
5398 This function may only use code that doesn't eval because it is
5399 called asynchronously from note_mouse_highlight. */
5401 static ptrdiff_t
5402 string_buffer_position_lim (Lisp_Object string,
5403 ptrdiff_t from, ptrdiff_t to, bool back_p)
5405 Lisp_Object limit, prop, pos;
5406 bool found = false;
5408 pos = make_number (max (from, BEGV));
5410 if (!back_p) /* looking forward */
5412 limit = make_number (min (to, ZV));
5413 while (!found && !EQ (pos, limit))
5415 prop = Fget_char_property (pos, Qdisplay, Qnil);
5416 if (!NILP (prop) && display_prop_string_p (prop, string))
5417 found = true;
5418 else
5419 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5420 limit);
5423 else /* looking back */
5425 limit = make_number (max (to, BEGV));
5426 while (!found && !EQ (pos, limit))
5428 prop = Fget_char_property (pos, Qdisplay, Qnil);
5429 if (!NILP (prop) && display_prop_string_p (prop, string))
5430 found = true;
5431 else
5432 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5433 limit);
5437 return found ? XINT (pos) : 0;
5440 /* Determine which buffer position in current buffer STRING comes from.
5441 AROUND_CHARPOS is an approximate position where it could come from.
5442 Value is the buffer position or 0 if it couldn't be determined.
5444 This function is necessary because we don't record buffer positions
5445 in glyphs generated from strings (to keep struct glyph small).
5446 This function may only use code that doesn't eval because it is
5447 called asynchronously from note_mouse_highlight. */
5449 static ptrdiff_t
5450 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5452 const int MAX_DISTANCE = 1000;
5453 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5454 around_charpos + MAX_DISTANCE,
5455 false);
5457 if (!found)
5458 found = string_buffer_position_lim (string, around_charpos,
5459 around_charpos - MAX_DISTANCE, true);
5460 return found;
5465 /***********************************************************************
5466 `composition' property
5467 ***********************************************************************/
5469 /* Set up iterator IT from `composition' property at its current
5470 position. Called from handle_stop. */
5472 static enum prop_handled
5473 handle_composition_prop (struct it *it)
5475 Lisp_Object prop, string;
5476 ptrdiff_t pos, pos_byte, start, end;
5478 if (STRINGP (it->string))
5480 unsigned char *s;
5482 pos = IT_STRING_CHARPOS (*it);
5483 pos_byte = IT_STRING_BYTEPOS (*it);
5484 string = it->string;
5485 s = SDATA (string) + pos_byte;
5486 it->c = STRING_CHAR (s);
5488 else
5490 pos = IT_CHARPOS (*it);
5491 pos_byte = IT_BYTEPOS (*it);
5492 string = Qnil;
5493 it->c = FETCH_CHAR (pos_byte);
5496 /* If there's a valid composition and point is not inside of the
5497 composition (in the case that the composition is from the current
5498 buffer), draw a glyph composed from the composition components. */
5499 if (find_composition (pos, -1, &start, &end, &prop, string)
5500 && composition_valid_p (start, end, prop)
5501 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5503 if (start < pos)
5504 /* As we can't handle this situation (perhaps font-lock added
5505 a new composition), we just return here hoping that next
5506 redisplay will detect this composition much earlier. */
5507 return HANDLED_NORMALLY;
5508 if (start != pos)
5510 if (STRINGP (it->string))
5511 pos_byte = string_char_to_byte (it->string, start);
5512 else
5513 pos_byte = CHAR_TO_BYTE (start);
5515 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5516 prop, string);
5518 if (it->cmp_it.id >= 0)
5520 it->cmp_it.ch = -1;
5521 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5522 it->cmp_it.nglyphs = -1;
5526 return HANDLED_NORMALLY;
5531 /***********************************************************************
5532 Overlay strings
5533 ***********************************************************************/
5535 /* The following structure is used to record overlay strings for
5536 later sorting in load_overlay_strings. */
5538 struct overlay_entry
5540 Lisp_Object overlay;
5541 Lisp_Object string;
5542 EMACS_INT priority;
5543 bool after_string_p;
5547 /* Set up iterator IT from overlay strings at its current position.
5548 Called from handle_stop. */
5550 static enum prop_handled
5551 handle_overlay_change (struct it *it)
5553 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5554 return HANDLED_RECOMPUTE_PROPS;
5555 else
5556 return HANDLED_NORMALLY;
5560 /* Set up the next overlay string for delivery by IT, if there is an
5561 overlay string to deliver. Called by set_iterator_to_next when the
5562 end of the current overlay string is reached. If there are more
5563 overlay strings to display, IT->string and
5564 IT->current.overlay_string_index are set appropriately here.
5565 Otherwise IT->string is set to nil. */
5567 static void
5568 next_overlay_string (struct it *it)
5570 ++it->current.overlay_string_index;
5571 if (it->current.overlay_string_index == it->n_overlay_strings)
5573 /* No more overlay strings. Restore IT's settings to what
5574 they were before overlay strings were processed, and
5575 continue to deliver from current_buffer. */
5577 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5578 pop_it (it);
5579 eassert (it->sp > 0
5580 || (NILP (it->string)
5581 && it->method == GET_FROM_BUFFER
5582 && it->stop_charpos >= BEGV
5583 && it->stop_charpos <= it->end_charpos));
5584 it->current.overlay_string_index = -1;
5585 it->n_overlay_strings = 0;
5586 /* If there's an empty display string on the stack, pop the
5587 stack, to resync the bidi iterator with IT's position. Such
5588 empty strings are pushed onto the stack in
5589 get_overlay_strings_1. */
5590 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5591 pop_it (it);
5593 /* Since we've exhausted overlay strings at this buffer
5594 position, set the flag to ignore overlays until we move to
5595 another position. (The flag will be reset in
5596 next_element_from_buffer.) But don't do that if the overlay
5597 strings were loaded at position other than the current one,
5598 which could happen if we called pop_it above, or if the
5599 overlay strings were loaded by handle_invisible_prop at the
5600 beginning of invisible text. */
5601 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5602 it->ignore_overlay_strings_at_pos_p = true;
5604 /* If we're at the end of the buffer, record that we have
5605 processed the overlay strings there already, so that
5606 next_element_from_buffer doesn't try it again. */
5607 if (NILP (it->string)
5608 && IT_CHARPOS (*it) >= it->end_charpos
5609 && it->overlay_strings_charpos >= it->end_charpos)
5610 it->overlay_strings_at_end_processed_p = true;
5611 /* Note: we reset overlay_strings_charpos only here, to make
5612 sure the just-processed overlays were indeed at EOB.
5613 Otherwise, overlays on text with invisible text property,
5614 which are processed with IT's position past the invisible
5615 text, might fool us into thinking the overlays at EOB were
5616 already processed (linum-mode can cause this, for
5617 example). */
5618 it->overlay_strings_charpos = -1;
5620 else
5622 /* There are more overlay strings to process. If
5623 IT->current.overlay_string_index has advanced to a position
5624 where we must load IT->overlay_strings with more strings, do
5625 it. We must load at the IT->overlay_strings_charpos where
5626 IT->n_overlay_strings was originally computed; when invisible
5627 text is present, this might not be IT_CHARPOS (Bug#7016). */
5628 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5630 if (it->current.overlay_string_index && i == 0)
5631 load_overlay_strings (it, it->overlay_strings_charpos);
5633 /* Initialize IT to deliver display elements from the overlay
5634 string. */
5635 it->string = it->overlay_strings[i];
5636 it->multibyte_p = STRING_MULTIBYTE (it->string);
5637 SET_TEXT_POS (it->current.string_pos, 0, 0);
5638 it->method = GET_FROM_STRING;
5639 it->stop_charpos = 0;
5640 it->end_charpos = SCHARS (it->string);
5641 if (it->cmp_it.stop_pos >= 0)
5642 it->cmp_it.stop_pos = 0;
5643 it->prev_stop = 0;
5644 it->base_level_stop = 0;
5646 /* Set up the bidi iterator for this overlay string. */
5647 if (it->bidi_p)
5649 it->bidi_it.string.lstring = it->string;
5650 it->bidi_it.string.s = NULL;
5651 it->bidi_it.string.schars = SCHARS (it->string);
5652 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5653 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5654 it->bidi_it.string.unibyte = !it->multibyte_p;
5655 it->bidi_it.w = it->w;
5656 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5660 CHECK_IT (it);
5664 /* Compare two overlay_entry structures E1 and E2. Used as a
5665 comparison function for qsort in load_overlay_strings. Overlay
5666 strings for the same position are sorted so that
5668 1. All after-strings come in front of before-strings, except
5669 when they come from the same overlay.
5671 2. Within after-strings, strings are sorted so that overlay strings
5672 from overlays with higher priorities come first.
5674 2. Within before-strings, strings are sorted so that overlay
5675 strings from overlays with higher priorities come last.
5677 Value is analogous to strcmp. */
5680 static int
5681 compare_overlay_entries (const void *e1, const void *e2)
5683 struct overlay_entry const *entry1 = e1;
5684 struct overlay_entry const *entry2 = e2;
5685 int result;
5687 if (entry1->after_string_p != entry2->after_string_p)
5689 /* Let after-strings appear in front of before-strings if
5690 they come from different overlays. */
5691 if (EQ (entry1->overlay, entry2->overlay))
5692 result = entry1->after_string_p ? 1 : -1;
5693 else
5694 result = entry1->after_string_p ? -1 : 1;
5696 else if (entry1->priority != entry2->priority)
5698 if (entry1->after_string_p)
5699 /* After-strings sorted in order of decreasing priority. */
5700 result = entry2->priority < entry1->priority ? -1 : 1;
5701 else
5702 /* Before-strings sorted in order of increasing priority. */
5703 result = entry1->priority < entry2->priority ? -1 : 1;
5705 else
5706 result = 0;
5708 return result;
5712 /* Load the vector IT->overlay_strings with overlay strings from IT's
5713 current buffer position, or from CHARPOS if that is > 0. Set
5714 IT->n_overlays to the total number of overlay strings found.
5716 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5717 a time. On entry into load_overlay_strings,
5718 IT->current.overlay_string_index gives the number of overlay
5719 strings that have already been loaded by previous calls to this
5720 function.
5722 IT->add_overlay_start contains an additional overlay start
5723 position to consider for taking overlay strings from, if non-zero.
5724 This position comes into play when the overlay has an `invisible'
5725 property, and both before and after-strings. When we've skipped to
5726 the end of the overlay, because of its `invisible' property, we
5727 nevertheless want its before-string to appear.
5728 IT->add_overlay_start will contain the overlay start position
5729 in this case.
5731 Overlay strings are sorted so that after-string strings come in
5732 front of before-string strings. Within before and after-strings,
5733 strings are sorted by overlay priority. See also function
5734 compare_overlay_entries. */
5736 static void
5737 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5739 Lisp_Object overlay, window, str, invisible;
5740 struct Lisp_Overlay *ov;
5741 ptrdiff_t start, end;
5742 ptrdiff_t n = 0, i, j;
5743 int invis;
5744 struct overlay_entry entriesbuf[20];
5745 ptrdiff_t size = ARRAYELTS (entriesbuf);
5746 struct overlay_entry *entries = entriesbuf;
5747 USE_SAFE_ALLOCA;
5749 if (charpos <= 0)
5750 charpos = IT_CHARPOS (*it);
5752 /* Append the overlay string STRING of overlay OVERLAY to vector
5753 `entries' which has size `size' and currently contains `n'
5754 elements. AFTER_P means STRING is an after-string of
5755 OVERLAY. */
5756 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5757 do \
5759 Lisp_Object priority; \
5761 if (n == size) \
5763 struct overlay_entry *old = entries; \
5764 SAFE_NALLOCA (entries, 2, size); \
5765 memcpy (entries, old, size * sizeof *entries); \
5766 size *= 2; \
5769 entries[n].string = (STRING); \
5770 entries[n].overlay = (OVERLAY); \
5771 priority = Foverlay_get ((OVERLAY), Qpriority); \
5772 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5773 entries[n].after_string_p = (AFTER_P); \
5774 ++n; \
5776 while (false)
5778 /* Process overlay before the overlay center. */
5779 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5781 XSETMISC (overlay, ov);
5782 eassert (OVERLAYP (overlay));
5783 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5784 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5786 if (end < charpos)
5787 break;
5789 /* Skip this overlay if it doesn't start or end at IT's current
5790 position. */
5791 if (end != charpos && start != charpos)
5792 continue;
5794 /* Skip this overlay if it doesn't apply to IT->w. */
5795 window = Foverlay_get (overlay, Qwindow);
5796 if (WINDOWP (window) && XWINDOW (window) != it->w)
5797 continue;
5799 /* If the text ``under'' the overlay is invisible, both before-
5800 and after-strings from this overlay are visible; start and
5801 end position are indistinguishable. */
5802 invisible = Foverlay_get (overlay, Qinvisible);
5803 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5805 /* If overlay has a non-empty before-string, record it. */
5806 if ((start == charpos || (end == charpos && invis != 0))
5807 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5808 && SCHARS (str))
5809 RECORD_OVERLAY_STRING (overlay, str, false);
5811 /* If overlay has a non-empty after-string, record it. */
5812 if ((end == charpos || (start == charpos && invis != 0))
5813 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5814 && SCHARS (str))
5815 RECORD_OVERLAY_STRING (overlay, str, true);
5818 /* Process overlays after the overlay center. */
5819 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5821 XSETMISC (overlay, ov);
5822 eassert (OVERLAYP (overlay));
5823 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5824 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5826 if (start > charpos)
5827 break;
5829 /* Skip this overlay if it doesn't start or end at IT's current
5830 position. */
5831 if (end != charpos && start != charpos)
5832 continue;
5834 /* Skip this overlay if it doesn't apply to IT->w. */
5835 window = Foverlay_get (overlay, Qwindow);
5836 if (WINDOWP (window) && XWINDOW (window) != it->w)
5837 continue;
5839 /* If the text ``under'' the overlay is invisible, it has a zero
5840 dimension, and both before- and after-strings apply. */
5841 invisible = Foverlay_get (overlay, Qinvisible);
5842 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5844 /* If overlay has a non-empty before-string, record it. */
5845 if ((start == charpos || (end == charpos && invis != 0))
5846 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5847 && SCHARS (str))
5848 RECORD_OVERLAY_STRING (overlay, str, false);
5850 /* If overlay has a non-empty after-string, record it. */
5851 if ((end == charpos || (start == charpos && invis != 0))
5852 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5853 && SCHARS (str))
5854 RECORD_OVERLAY_STRING (overlay, str, true);
5857 #undef RECORD_OVERLAY_STRING
5859 /* Sort entries. */
5860 if (n > 1)
5861 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5863 /* Record number of overlay strings, and where we computed it. */
5864 it->n_overlay_strings = n;
5865 it->overlay_strings_charpos = charpos;
5867 /* IT->current.overlay_string_index is the number of overlay strings
5868 that have already been consumed by IT. Copy some of the
5869 remaining overlay strings to IT->overlay_strings. */
5870 i = 0;
5871 j = it->current.overlay_string_index;
5872 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5874 it->overlay_strings[i] = entries[j].string;
5875 it->string_overlays[i++] = entries[j++].overlay;
5878 CHECK_IT (it);
5879 SAFE_FREE ();
5883 /* Get the first chunk of overlay strings at IT's current buffer
5884 position, or at CHARPOS if that is > 0. Value is true if at
5885 least one overlay string was found. */
5887 static bool
5888 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5890 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5891 process. This fills IT->overlay_strings with strings, and sets
5892 IT->n_overlay_strings to the total number of strings to process.
5893 IT->pos.overlay_string_index has to be set temporarily to zero
5894 because load_overlay_strings needs this; it must be set to -1
5895 when no overlay strings are found because a zero value would
5896 indicate a position in the first overlay string. */
5897 it->current.overlay_string_index = 0;
5898 load_overlay_strings (it, charpos);
5900 /* If we found overlay strings, set up IT to deliver display
5901 elements from the first one. Otherwise set up IT to deliver
5902 from current_buffer. */
5903 if (it->n_overlay_strings)
5905 /* Make sure we know settings in current_buffer, so that we can
5906 restore meaningful values when we're done with the overlay
5907 strings. */
5908 if (compute_stop_p)
5909 compute_stop_pos (it);
5910 eassert (it->face_id >= 0);
5912 /* Save IT's settings. They are restored after all overlay
5913 strings have been processed. */
5914 eassert (!compute_stop_p || it->sp == 0);
5916 /* When called from handle_stop, there might be an empty display
5917 string loaded. In that case, don't bother saving it. But
5918 don't use this optimization with the bidi iterator, since we
5919 need the corresponding pop_it call to resync the bidi
5920 iterator's position with IT's position, after we are done
5921 with the overlay strings. (The corresponding call to pop_it
5922 in case of an empty display string is in
5923 next_overlay_string.) */
5924 if (!(!it->bidi_p
5925 && STRINGP (it->string) && !SCHARS (it->string)))
5926 push_it (it, NULL);
5928 /* Set up IT to deliver display elements from the first overlay
5929 string. */
5930 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5931 it->string = it->overlay_strings[0];
5932 it->from_overlay = Qnil;
5933 it->stop_charpos = 0;
5934 eassert (STRINGP (it->string));
5935 it->end_charpos = SCHARS (it->string);
5936 it->prev_stop = 0;
5937 it->base_level_stop = 0;
5938 it->multibyte_p = STRING_MULTIBYTE (it->string);
5939 it->method = GET_FROM_STRING;
5940 it->from_disp_prop_p = 0;
5942 /* Force paragraph direction to be that of the parent
5943 buffer. */
5944 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5945 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5946 else
5947 it->paragraph_embedding = L2R;
5949 /* Set up the bidi iterator for this overlay string. */
5950 if (it->bidi_p)
5952 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5954 it->bidi_it.string.lstring = it->string;
5955 it->bidi_it.string.s = NULL;
5956 it->bidi_it.string.schars = SCHARS (it->string);
5957 it->bidi_it.string.bufpos = pos;
5958 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5959 it->bidi_it.string.unibyte = !it->multibyte_p;
5960 it->bidi_it.w = it->w;
5961 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5963 return true;
5966 it->current.overlay_string_index = -1;
5967 return false;
5970 static bool
5971 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5973 it->string = Qnil;
5974 it->method = GET_FROM_BUFFER;
5976 get_overlay_strings_1 (it, charpos, true);
5978 CHECK_IT (it);
5980 /* Value is true if we found at least one overlay string. */
5981 return STRINGP (it->string);
5986 /***********************************************************************
5987 Saving and restoring state
5988 ***********************************************************************/
5990 /* Save current settings of IT on IT->stack. Called, for example,
5991 before setting up IT for an overlay string, to be able to restore
5992 IT's settings to what they were after the overlay string has been
5993 processed. If POSITION is non-NULL, it is the position to save on
5994 the stack instead of IT->position. */
5996 static void
5997 push_it (struct it *it, struct text_pos *position)
5999 struct iterator_stack_entry *p;
6001 eassert (it->sp < IT_STACK_SIZE);
6002 p = it->stack + it->sp;
6004 p->stop_charpos = it->stop_charpos;
6005 p->prev_stop = it->prev_stop;
6006 p->base_level_stop = it->base_level_stop;
6007 p->cmp_it = it->cmp_it;
6008 eassert (it->face_id >= 0);
6009 p->face_id = it->face_id;
6010 p->string = it->string;
6011 p->method = it->method;
6012 p->from_overlay = it->from_overlay;
6013 switch (p->method)
6015 case GET_FROM_IMAGE:
6016 p->u.image.object = it->object;
6017 p->u.image.image_id = it->image_id;
6018 p->u.image.slice = it->slice;
6019 break;
6020 case GET_FROM_STRETCH:
6021 p->u.stretch.object = it->object;
6022 break;
6023 case GET_FROM_XWIDGET:
6024 p->u.xwidget.object = it->object;
6025 break;
6026 case GET_FROM_BUFFER:
6027 case GET_FROM_DISPLAY_VECTOR:
6028 case GET_FROM_STRING:
6029 case GET_FROM_C_STRING:
6030 break;
6031 default:
6032 emacs_abort ();
6034 p->position = position ? *position : it->position;
6035 p->current = it->current;
6036 p->end_charpos = it->end_charpos;
6037 p->string_nchars = it->string_nchars;
6038 p->area = it->area;
6039 p->multibyte_p = it->multibyte_p;
6040 p->avoid_cursor_p = it->avoid_cursor_p;
6041 p->space_width = it->space_width;
6042 p->font_height = it->font_height;
6043 p->voffset = it->voffset;
6044 p->string_from_display_prop_p = it->string_from_display_prop_p;
6045 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6046 p->display_ellipsis_p = false;
6047 p->line_wrap = it->line_wrap;
6048 p->bidi_p = it->bidi_p;
6049 p->paragraph_embedding = it->paragraph_embedding;
6050 p->from_disp_prop_p = it->from_disp_prop_p;
6051 ++it->sp;
6053 /* Save the state of the bidi iterator as well. */
6054 if (it->bidi_p)
6055 bidi_push_it (&it->bidi_it);
6058 static void
6059 iterate_out_of_display_property (struct it *it)
6061 bool buffer_p = !STRINGP (it->string);
6062 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6063 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6065 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6067 /* Maybe initialize paragraph direction. If we are at the beginning
6068 of a new paragraph, next_element_from_buffer may not have a
6069 chance to do that. */
6070 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6071 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6072 /* prev_stop can be zero, so check against BEGV as well. */
6073 while (it->bidi_it.charpos >= bob
6074 && it->prev_stop <= it->bidi_it.charpos
6075 && it->bidi_it.charpos < CHARPOS (it->position)
6076 && it->bidi_it.charpos < eob)
6077 bidi_move_to_visually_next (&it->bidi_it);
6078 /* Record the stop_pos we just crossed, for when we cross it
6079 back, maybe. */
6080 if (it->bidi_it.charpos > CHARPOS (it->position))
6081 it->prev_stop = CHARPOS (it->position);
6082 /* If we ended up not where pop_it put us, resync IT's
6083 positional members with the bidi iterator. */
6084 if (it->bidi_it.charpos != CHARPOS (it->position))
6085 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6086 if (buffer_p)
6087 it->current.pos = it->position;
6088 else
6089 it->current.string_pos = it->position;
6092 /* Restore IT's settings from IT->stack. Called, for example, when no
6093 more overlay strings must be processed, and we return to delivering
6094 display elements from a buffer, or when the end of a string from a
6095 `display' property is reached and we return to delivering display
6096 elements from an overlay string, or from a buffer. */
6098 static void
6099 pop_it (struct it *it)
6101 struct iterator_stack_entry *p;
6102 bool from_display_prop = it->from_disp_prop_p;
6103 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6105 eassert (it->sp > 0);
6106 --it->sp;
6107 p = it->stack + it->sp;
6108 it->stop_charpos = p->stop_charpos;
6109 it->prev_stop = p->prev_stop;
6110 it->base_level_stop = p->base_level_stop;
6111 it->cmp_it = p->cmp_it;
6112 it->face_id = p->face_id;
6113 it->current = p->current;
6114 it->position = p->position;
6115 it->string = p->string;
6116 it->from_overlay = p->from_overlay;
6117 if (NILP (it->string))
6118 SET_TEXT_POS (it->current.string_pos, -1, -1);
6119 it->method = p->method;
6120 switch (it->method)
6122 case GET_FROM_IMAGE:
6123 it->image_id = p->u.image.image_id;
6124 it->object = p->u.image.object;
6125 it->slice = p->u.image.slice;
6126 break;
6127 case GET_FROM_XWIDGET:
6128 it->object = p->u.xwidget.object;
6129 break;
6130 case GET_FROM_STRETCH:
6131 it->object = p->u.stretch.object;
6132 break;
6133 case GET_FROM_BUFFER:
6134 it->object = it->w->contents;
6135 break;
6136 case GET_FROM_STRING:
6138 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6140 /* Restore the face_box_p flag, since it could have been
6141 overwritten by the face of the object that we just finished
6142 displaying. */
6143 if (face)
6144 it->face_box_p = face->box != FACE_NO_BOX;
6145 it->object = it->string;
6147 break;
6148 case GET_FROM_DISPLAY_VECTOR:
6149 if (it->s)
6150 it->method = GET_FROM_C_STRING;
6151 else if (STRINGP (it->string))
6152 it->method = GET_FROM_STRING;
6153 else
6155 it->method = GET_FROM_BUFFER;
6156 it->object = it->w->contents;
6158 break;
6159 case GET_FROM_C_STRING:
6160 break;
6161 default:
6162 emacs_abort ();
6164 it->end_charpos = p->end_charpos;
6165 it->string_nchars = p->string_nchars;
6166 it->area = p->area;
6167 it->multibyte_p = p->multibyte_p;
6168 it->avoid_cursor_p = p->avoid_cursor_p;
6169 it->space_width = p->space_width;
6170 it->font_height = p->font_height;
6171 it->voffset = p->voffset;
6172 it->string_from_display_prop_p = p->string_from_display_prop_p;
6173 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6174 it->line_wrap = p->line_wrap;
6175 it->bidi_p = p->bidi_p;
6176 it->paragraph_embedding = p->paragraph_embedding;
6177 it->from_disp_prop_p = p->from_disp_prop_p;
6178 if (it->bidi_p)
6180 bidi_pop_it (&it->bidi_it);
6181 /* Bidi-iterate until we get out of the portion of text, if any,
6182 covered by a `display' text property or by an overlay with
6183 `display' property. (We cannot just jump there, because the
6184 internal coherency of the bidi iterator state can not be
6185 preserved across such jumps.) We also must determine the
6186 paragraph base direction if the overlay we just processed is
6187 at the beginning of a new paragraph. */
6188 if (from_display_prop
6189 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6190 iterate_out_of_display_property (it);
6192 eassert ((BUFFERP (it->object)
6193 && IT_CHARPOS (*it) == it->bidi_it.charpos
6194 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6195 || (STRINGP (it->object)
6196 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6197 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6198 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6200 /* If we move the iterator over text covered by a display property
6201 to a new buffer position, any info about previously seen overlays
6202 is no longer valid. */
6203 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6204 it->ignore_overlay_strings_at_pos_p = false;
6209 /***********************************************************************
6210 Moving over lines
6211 ***********************************************************************/
6213 /* Set IT's current position to the previous line start. */
6215 static void
6216 back_to_previous_line_start (struct it *it)
6218 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6220 DEC_BOTH (cp, bp);
6221 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6225 /* Move IT to the next line start.
6227 Value is true if a newline was found. Set *SKIPPED_P to true if
6228 we skipped over part of the text (as opposed to moving the iterator
6229 continuously over the text). Otherwise, don't change the value
6230 of *SKIPPED_P.
6232 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6233 iterator on the newline, if it was found.
6235 Newlines may come from buffer text, overlay strings, or strings
6236 displayed via the `display' property. That's the reason we can't
6237 simply use find_newline_no_quit.
6239 Note that this function may not skip over invisible text that is so
6240 because of text properties and immediately follows a newline. If
6241 it would, function reseat_at_next_visible_line_start, when called
6242 from set_iterator_to_next, would effectively make invisible
6243 characters following a newline part of the wrong glyph row, which
6244 leads to wrong cursor motion. */
6246 static bool
6247 forward_to_next_line_start (struct it *it, bool *skipped_p,
6248 struct bidi_it *bidi_it_prev)
6250 ptrdiff_t old_selective;
6251 bool newline_found_p = false;
6252 int n;
6253 const int MAX_NEWLINE_DISTANCE = 500;
6255 /* If already on a newline, just consume it to avoid unintended
6256 skipping over invisible text below. */
6257 if (it->what == IT_CHARACTER
6258 && it->c == '\n'
6259 && CHARPOS (it->position) == IT_CHARPOS (*it))
6261 if (it->bidi_p && bidi_it_prev)
6262 *bidi_it_prev = it->bidi_it;
6263 set_iterator_to_next (it, false);
6264 it->c = 0;
6265 return true;
6268 /* Don't handle selective display in the following. It's (a)
6269 unnecessary because it's done by the caller, and (b) leads to an
6270 infinite recursion because next_element_from_ellipsis indirectly
6271 calls this function. */
6272 old_selective = it->selective;
6273 it->selective = 0;
6275 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6276 from buffer text. */
6277 for (n = 0;
6278 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6279 n += !STRINGP (it->string))
6281 if (!get_next_display_element (it))
6282 return false;
6283 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6284 if (newline_found_p && it->bidi_p && bidi_it_prev)
6285 *bidi_it_prev = it->bidi_it;
6286 set_iterator_to_next (it, false);
6289 /* If we didn't find a newline near enough, see if we can use a
6290 short-cut. */
6291 if (!newline_found_p)
6293 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6294 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6295 1, &bytepos);
6296 Lisp_Object pos;
6298 eassert (!STRINGP (it->string));
6300 /* If there isn't any `display' property in sight, and no
6301 overlays, we can just use the position of the newline in
6302 buffer text. */
6303 if (it->stop_charpos >= limit
6304 || ((pos = Fnext_single_property_change (make_number (start),
6305 Qdisplay, Qnil,
6306 make_number (limit)),
6307 NILP (pos))
6308 && next_overlay_change (start) == ZV))
6310 if (!it->bidi_p)
6312 IT_CHARPOS (*it) = limit;
6313 IT_BYTEPOS (*it) = bytepos;
6315 else
6317 struct bidi_it bprev;
6319 /* Help bidi.c avoid expensive searches for display
6320 properties and overlays, by telling it that there are
6321 none up to `limit'. */
6322 if (it->bidi_it.disp_pos < limit)
6324 it->bidi_it.disp_pos = limit;
6325 it->bidi_it.disp_prop = 0;
6327 do {
6328 bprev = it->bidi_it;
6329 bidi_move_to_visually_next (&it->bidi_it);
6330 } while (it->bidi_it.charpos != limit);
6331 IT_CHARPOS (*it) = limit;
6332 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6333 if (bidi_it_prev)
6334 *bidi_it_prev = bprev;
6336 *skipped_p = newline_found_p = true;
6338 else
6340 while (!newline_found_p)
6342 if (!get_next_display_element (it))
6343 break;
6344 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6345 if (newline_found_p && it->bidi_p && bidi_it_prev)
6346 *bidi_it_prev = it->bidi_it;
6347 set_iterator_to_next (it, false);
6352 it->selective = old_selective;
6353 return newline_found_p;
6357 /* Set IT's current position to the previous visible line start. Skip
6358 invisible text that is so either due to text properties or due to
6359 selective display. Caution: this does not change IT->current_x and
6360 IT->hpos. */
6362 static void
6363 back_to_previous_visible_line_start (struct it *it)
6365 while (IT_CHARPOS (*it) > BEGV)
6367 back_to_previous_line_start (it);
6369 if (IT_CHARPOS (*it) <= BEGV)
6370 break;
6372 /* If selective > 0, then lines indented more than its value are
6373 invisible. */
6374 if (it->selective > 0
6375 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6376 it->selective))
6377 continue;
6379 /* Check the newline before point for invisibility. */
6381 Lisp_Object prop;
6382 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6383 Qinvisible, it->window);
6384 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6385 continue;
6388 if (IT_CHARPOS (*it) <= BEGV)
6389 break;
6392 struct it it2;
6393 void *it2data = NULL;
6394 ptrdiff_t pos;
6395 ptrdiff_t beg, end;
6396 Lisp_Object val, overlay;
6398 SAVE_IT (it2, *it, it2data);
6400 /* If newline is part of a composition, continue from start of composition */
6401 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6402 && beg < IT_CHARPOS (*it))
6403 goto replaced;
6405 /* If newline is replaced by a display property, find start of overlay
6406 or interval and continue search from that point. */
6407 pos = --IT_CHARPOS (it2);
6408 --IT_BYTEPOS (it2);
6409 it2.sp = 0;
6410 bidi_unshelve_cache (NULL, false);
6411 it2.string_from_display_prop_p = false;
6412 it2.from_disp_prop_p = false;
6413 if (handle_display_prop (&it2) == HANDLED_RETURN
6414 && !NILP (val = get_char_property_and_overlay
6415 (make_number (pos), Qdisplay, Qnil, &overlay))
6416 && (OVERLAYP (overlay)
6417 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6418 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6420 RESTORE_IT (it, it, it2data);
6421 goto replaced;
6424 /* Newline is not replaced by anything -- so we are done. */
6425 RESTORE_IT (it, it, it2data);
6426 break;
6428 replaced:
6429 if (beg < BEGV)
6430 beg = BEGV;
6431 IT_CHARPOS (*it) = beg;
6432 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6436 it->continuation_lines_width = 0;
6438 eassert (IT_CHARPOS (*it) >= BEGV);
6439 eassert (IT_CHARPOS (*it) == BEGV
6440 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6441 CHECK_IT (it);
6445 /* Reseat iterator IT at the previous visible line start. Skip
6446 invisible text that is so either due to text properties or due to
6447 selective display. At the end, update IT's overlay information,
6448 face information etc. */
6450 void
6451 reseat_at_previous_visible_line_start (struct it *it)
6453 back_to_previous_visible_line_start (it);
6454 reseat (it, it->current.pos, true);
6455 CHECK_IT (it);
6459 /* Reseat iterator IT on the next visible line start in the current
6460 buffer. ON_NEWLINE_P means position IT on the newline
6461 preceding the line start. Skip over invisible text that is so
6462 because of selective display. Compute faces, overlays etc at the
6463 new position. Note that this function does not skip over text that
6464 is invisible because of text properties. */
6466 static void
6467 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6469 bool skipped_p = false;
6470 struct bidi_it bidi_it_prev;
6471 bool newline_found_p
6472 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6474 /* Skip over lines that are invisible because they are indented
6475 more than the value of IT->selective. */
6476 if (it->selective > 0)
6477 while (IT_CHARPOS (*it) < ZV
6478 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6479 it->selective))
6481 eassert (IT_BYTEPOS (*it) == BEGV
6482 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6483 newline_found_p =
6484 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6487 /* Position on the newline if that's what's requested. */
6488 if (on_newline_p && newline_found_p)
6490 if (STRINGP (it->string))
6492 if (IT_STRING_CHARPOS (*it) > 0)
6494 if (!it->bidi_p)
6496 --IT_STRING_CHARPOS (*it);
6497 --IT_STRING_BYTEPOS (*it);
6499 else
6501 /* We need to restore the bidi iterator to the state
6502 it had on the newline, and resync the IT's
6503 position with that. */
6504 it->bidi_it = bidi_it_prev;
6505 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6506 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6510 else if (IT_CHARPOS (*it) > BEGV)
6512 if (!it->bidi_p)
6514 --IT_CHARPOS (*it);
6515 --IT_BYTEPOS (*it);
6517 else
6519 /* We need to restore the bidi iterator to the state it
6520 had on the newline and resync IT with that. */
6521 it->bidi_it = bidi_it_prev;
6522 IT_CHARPOS (*it) = it->bidi_it.charpos;
6523 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6525 reseat (it, it->current.pos, false);
6528 else if (skipped_p)
6529 reseat (it, it->current.pos, false);
6531 CHECK_IT (it);
6536 /***********************************************************************
6537 Changing an iterator's position
6538 ***********************************************************************/
6540 /* Change IT's current position to POS in current_buffer.
6541 If FORCE_P, always check for text properties at the new position.
6542 Otherwise, text properties are only looked up if POS >=
6543 IT->check_charpos of a property. */
6545 static void
6546 reseat (struct it *it, struct text_pos pos, bool force_p)
6548 ptrdiff_t original_pos = IT_CHARPOS (*it);
6550 reseat_1 (it, pos, false);
6552 /* Determine where to check text properties. Avoid doing it
6553 where possible because text property lookup is very expensive. */
6554 if (force_p
6555 || CHARPOS (pos) > it->stop_charpos
6556 || CHARPOS (pos) < original_pos)
6558 if (it->bidi_p)
6560 /* For bidi iteration, we need to prime prev_stop and
6561 base_level_stop with our best estimations. */
6562 /* Implementation note: Of course, POS is not necessarily a
6563 stop position, so assigning prev_pos to it is a lie; we
6564 should have called compute_stop_backwards. However, if
6565 the current buffer does not include any R2L characters,
6566 that call would be a waste of cycles, because the
6567 iterator will never move back, and thus never cross this
6568 "fake" stop position. So we delay that backward search
6569 until the time we really need it, in next_element_from_buffer. */
6570 if (CHARPOS (pos) != it->prev_stop)
6571 it->prev_stop = CHARPOS (pos);
6572 if (CHARPOS (pos) < it->base_level_stop)
6573 it->base_level_stop = 0; /* meaning it's unknown */
6574 handle_stop (it);
6576 else
6578 handle_stop (it);
6579 it->prev_stop = it->base_level_stop = 0;
6584 CHECK_IT (it);
6588 /* Change IT's buffer position to POS. SET_STOP_P means set
6589 IT->stop_pos to POS, also. */
6591 static void
6592 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6594 /* Don't call this function when scanning a C string. */
6595 eassert (it->s == NULL);
6597 /* POS must be a reasonable value. */
6598 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6600 it->current.pos = it->position = pos;
6601 it->end_charpos = ZV;
6602 it->dpvec = NULL;
6603 it->current.dpvec_index = -1;
6604 it->current.overlay_string_index = -1;
6605 IT_STRING_CHARPOS (*it) = -1;
6606 IT_STRING_BYTEPOS (*it) = -1;
6607 it->string = Qnil;
6608 it->method = GET_FROM_BUFFER;
6609 it->object = it->w->contents;
6610 it->area = TEXT_AREA;
6611 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6612 it->sp = 0;
6613 it->string_from_display_prop_p = false;
6614 it->string_from_prefix_prop_p = false;
6616 it->from_disp_prop_p = false;
6617 it->face_before_selective_p = false;
6618 if (it->bidi_p)
6620 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6621 &it->bidi_it);
6622 bidi_unshelve_cache (NULL, false);
6623 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6624 it->bidi_it.string.s = NULL;
6625 it->bidi_it.string.lstring = Qnil;
6626 it->bidi_it.string.bufpos = 0;
6627 it->bidi_it.string.from_disp_str = false;
6628 it->bidi_it.string.unibyte = false;
6629 it->bidi_it.w = it->w;
6632 if (set_stop_p)
6634 it->stop_charpos = CHARPOS (pos);
6635 it->base_level_stop = CHARPOS (pos);
6637 /* This make the information stored in it->cmp_it invalidate. */
6638 it->cmp_it.id = -1;
6642 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6643 If S is non-null, it is a C string to iterate over. Otherwise,
6644 STRING gives a Lisp string to iterate over.
6646 If PRECISION > 0, don't return more then PRECISION number of
6647 characters from the string.
6649 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6650 characters have been returned. FIELD_WIDTH < 0 means an infinite
6651 field width.
6653 MULTIBYTE = 0 means disable processing of multibyte characters,
6654 MULTIBYTE > 0 means enable it,
6655 MULTIBYTE < 0 means use IT->multibyte_p.
6657 IT must be initialized via a prior call to init_iterator before
6658 calling this function. */
6660 static void
6661 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6662 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6663 int multibyte)
6665 /* No text property checks performed by default, but see below. */
6666 it->stop_charpos = -1;
6668 /* Set iterator position and end position. */
6669 memset (&it->current, 0, sizeof it->current);
6670 it->current.overlay_string_index = -1;
6671 it->current.dpvec_index = -1;
6672 eassert (charpos >= 0);
6674 /* If STRING is specified, use its multibyteness, otherwise use the
6675 setting of MULTIBYTE, if specified. */
6676 if (multibyte >= 0)
6677 it->multibyte_p = multibyte > 0;
6679 /* Bidirectional reordering of strings is controlled by the default
6680 value of bidi-display-reordering. Don't try to reorder while
6681 loading loadup.el, as the necessary character property tables are
6682 not yet available. */
6683 it->bidi_p =
6684 !redisplay__inhibit_bidi
6685 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6687 if (s == NULL)
6689 eassert (STRINGP (string));
6690 it->string = string;
6691 it->s = NULL;
6692 it->end_charpos = it->string_nchars = SCHARS (string);
6693 it->method = GET_FROM_STRING;
6694 it->current.string_pos = string_pos (charpos, string);
6696 if (it->bidi_p)
6698 it->bidi_it.string.lstring = string;
6699 it->bidi_it.string.s = NULL;
6700 it->bidi_it.string.schars = it->end_charpos;
6701 it->bidi_it.string.bufpos = 0;
6702 it->bidi_it.string.from_disp_str = false;
6703 it->bidi_it.string.unibyte = !it->multibyte_p;
6704 it->bidi_it.w = it->w;
6705 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6706 FRAME_WINDOW_P (it->f), &it->bidi_it);
6709 else
6711 it->s = (const unsigned char *) s;
6712 it->string = Qnil;
6714 /* Note that we use IT->current.pos, not it->current.string_pos,
6715 for displaying C strings. */
6716 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6717 if (it->multibyte_p)
6719 it->current.pos = c_string_pos (charpos, s, true);
6720 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6722 else
6724 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6725 it->end_charpos = it->string_nchars = strlen (s);
6728 if (it->bidi_p)
6730 it->bidi_it.string.lstring = Qnil;
6731 it->bidi_it.string.s = (const unsigned char *) s;
6732 it->bidi_it.string.schars = it->end_charpos;
6733 it->bidi_it.string.bufpos = 0;
6734 it->bidi_it.string.from_disp_str = false;
6735 it->bidi_it.string.unibyte = !it->multibyte_p;
6736 it->bidi_it.w = it->w;
6737 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6738 &it->bidi_it);
6740 it->method = GET_FROM_C_STRING;
6743 /* PRECISION > 0 means don't return more than PRECISION characters
6744 from the string. */
6745 if (precision > 0 && it->end_charpos - charpos > precision)
6747 it->end_charpos = it->string_nchars = charpos + precision;
6748 if (it->bidi_p)
6749 it->bidi_it.string.schars = it->end_charpos;
6752 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6753 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6754 FIELD_WIDTH < 0 means infinite field width. This is useful for
6755 padding with `-' at the end of a mode line. */
6756 if (field_width < 0)
6757 field_width = DISP_INFINITY;
6758 /* Implementation note: We deliberately don't enlarge
6759 it->bidi_it.string.schars here to fit it->end_charpos, because
6760 the bidi iterator cannot produce characters out of thin air. */
6761 if (field_width > it->end_charpos - charpos)
6762 it->end_charpos = charpos + field_width;
6764 /* Use the standard display table for displaying strings. */
6765 if (DISP_TABLE_P (Vstandard_display_table))
6766 it->dp = XCHAR_TABLE (Vstandard_display_table);
6768 it->stop_charpos = charpos;
6769 it->prev_stop = charpos;
6770 it->base_level_stop = 0;
6771 if (it->bidi_p)
6773 it->bidi_it.first_elt = true;
6774 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6775 it->bidi_it.disp_pos = -1;
6777 if (s == NULL && it->multibyte_p)
6779 ptrdiff_t endpos = SCHARS (it->string);
6780 if (endpos > it->end_charpos)
6781 endpos = it->end_charpos;
6782 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6783 it->string);
6785 CHECK_IT (it);
6790 /***********************************************************************
6791 Iteration
6792 ***********************************************************************/
6794 /* Map enum it_method value to corresponding next_element_from_* function. */
6796 typedef bool (*next_element_function) (struct it *);
6798 static next_element_function const get_next_element[NUM_IT_METHODS] =
6800 next_element_from_buffer,
6801 next_element_from_display_vector,
6802 next_element_from_string,
6803 next_element_from_c_string,
6804 next_element_from_image,
6805 next_element_from_stretch,
6806 next_element_from_xwidget,
6809 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6812 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6813 (possibly with the following characters). */
6815 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6816 ((IT)->cmp_it.id >= 0 \
6817 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6818 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6819 END_CHARPOS, (IT)->w, \
6820 FACE_FROM_ID_OR_NULL ((IT)->f, \
6821 (IT)->face_id), \
6822 (IT)->string)))
6825 /* Lookup the char-table Vglyphless_char_display for character C (-1
6826 if we want information for no-font case), and return the display
6827 method symbol. By side-effect, update it->what and
6828 it->glyphless_method. This function is called from
6829 get_next_display_element for each character element, and from
6830 x_produce_glyphs when no suitable font was found. */
6832 Lisp_Object
6833 lookup_glyphless_char_display (int c, struct it *it)
6835 Lisp_Object glyphless_method = Qnil;
6837 if (CHAR_TABLE_P (Vglyphless_char_display)
6838 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6840 if (c >= 0)
6842 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6843 if (CONSP (glyphless_method))
6844 glyphless_method = FRAME_WINDOW_P (it->f)
6845 ? XCAR (glyphless_method)
6846 : XCDR (glyphless_method);
6848 else
6849 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6852 retry:
6853 if (NILP (glyphless_method))
6855 if (c >= 0)
6856 /* The default is to display the character by a proper font. */
6857 return Qnil;
6858 /* The default for the no-font case is to display an empty box. */
6859 glyphless_method = Qempty_box;
6861 if (EQ (glyphless_method, Qzero_width))
6863 if (c >= 0)
6864 return glyphless_method;
6865 /* This method can't be used for the no-font case. */
6866 glyphless_method = Qempty_box;
6868 if (EQ (glyphless_method, Qthin_space))
6869 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6870 else if (EQ (glyphless_method, Qempty_box))
6871 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6872 else if (EQ (glyphless_method, Qhex_code))
6873 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6874 else if (STRINGP (glyphless_method))
6875 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6876 else
6878 /* Invalid value. We use the default method. */
6879 glyphless_method = Qnil;
6880 goto retry;
6882 it->what = IT_GLYPHLESS;
6883 return glyphless_method;
6886 /* Merge escape glyph face and cache the result. */
6888 static struct frame *last_escape_glyph_frame = NULL;
6889 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6890 static int last_escape_glyph_merged_face_id = 0;
6892 static int
6893 merge_escape_glyph_face (struct it *it)
6895 int face_id;
6897 if (it->f == last_escape_glyph_frame
6898 && it->face_id == last_escape_glyph_face_id)
6899 face_id = last_escape_glyph_merged_face_id;
6900 else
6902 /* Merge the `escape-glyph' face into the current face. */
6903 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6904 last_escape_glyph_frame = it->f;
6905 last_escape_glyph_face_id = it->face_id;
6906 last_escape_glyph_merged_face_id = face_id;
6908 return face_id;
6911 /* Likewise for glyphless glyph face. */
6913 static struct frame *last_glyphless_glyph_frame = NULL;
6914 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6915 static int last_glyphless_glyph_merged_face_id = 0;
6918 merge_glyphless_glyph_face (struct it *it)
6920 int face_id;
6922 if (it->f == last_glyphless_glyph_frame
6923 && it->face_id == last_glyphless_glyph_face_id)
6924 face_id = last_glyphless_glyph_merged_face_id;
6925 else
6927 /* Merge the `glyphless-char' face into the current face. */
6928 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6929 last_glyphless_glyph_frame = it->f;
6930 last_glyphless_glyph_face_id = it->face_id;
6931 last_glyphless_glyph_merged_face_id = face_id;
6933 return face_id;
6936 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6937 be called before redisplaying windows, and when the frame's face
6938 cache is freed. */
6939 void
6940 forget_escape_and_glyphless_faces (void)
6942 last_escape_glyph_frame = NULL;
6943 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6944 last_glyphless_glyph_frame = NULL;
6945 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6948 /* Load IT's display element fields with information about the next
6949 display element from the current position of IT. Value is false if
6950 end of buffer (or C string) is reached. */
6952 static bool
6953 get_next_display_element (struct it *it)
6955 /* True means that we found a display element. False means that
6956 we hit the end of what we iterate over. Performance note: the
6957 function pointer `method' used here turns out to be faster than
6958 using a sequence of if-statements. */
6959 bool success_p;
6961 get_next:
6962 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6964 if (it->what == IT_CHARACTER)
6966 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6967 and only if (a) the resolved directionality of that character
6968 is R..." */
6969 /* FIXME: Do we need an exception for characters from display
6970 tables? */
6971 if (it->bidi_p && it->bidi_it.type == STRONG_R
6972 && !inhibit_bidi_mirroring)
6973 it->c = bidi_mirror_char (it->c);
6974 /* Map via display table or translate control characters.
6975 IT->c, IT->len etc. have been set to the next character by
6976 the function call above. If we have a display table, and it
6977 contains an entry for IT->c, translate it. Don't do this if
6978 IT->c itself comes from a display table, otherwise we could
6979 end up in an infinite recursion. (An alternative could be to
6980 count the recursion depth of this function and signal an
6981 error when a certain maximum depth is reached.) Is it worth
6982 it? */
6983 if (success_p && it->dpvec == NULL)
6985 Lisp_Object dv;
6986 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6987 bool nonascii_space_p = false;
6988 bool nonascii_hyphen_p = false;
6989 int c = it->c; /* This is the character to display. */
6991 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6993 eassert (SINGLE_BYTE_CHAR_P (c));
6994 if (unibyte_display_via_language_environment)
6996 c = DECODE_CHAR (unibyte, c);
6997 if (c < 0)
6998 c = BYTE8_TO_CHAR (it->c);
7000 else
7001 c = BYTE8_TO_CHAR (it->c);
7004 if (it->dp
7005 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7006 VECTORP (dv)))
7008 struct Lisp_Vector *v = XVECTOR (dv);
7010 /* Return the first character from the display table
7011 entry, if not empty. If empty, don't display the
7012 current character. */
7013 if (v->header.size)
7015 it->dpvec_char_len = it->len;
7016 it->dpvec = v->contents;
7017 it->dpend = v->contents + v->header.size;
7018 it->current.dpvec_index = 0;
7019 it->dpvec_face_id = -1;
7020 it->saved_face_id = it->face_id;
7021 it->method = GET_FROM_DISPLAY_VECTOR;
7022 it->ellipsis_p = false;
7024 else
7026 set_iterator_to_next (it, false);
7028 goto get_next;
7031 if (! NILP (lookup_glyphless_char_display (c, it)))
7033 if (it->what == IT_GLYPHLESS)
7034 goto done;
7035 /* Don't display this character. */
7036 set_iterator_to_next (it, false);
7037 goto get_next;
7040 /* If `nobreak-char-display' is non-nil, we display
7041 non-ASCII spaces and hyphens specially. */
7042 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7044 if (c == NO_BREAK_SPACE)
7045 nonascii_space_p = true;
7046 else if (c == SOFT_HYPHEN || c == HYPHEN
7047 || c == NON_BREAKING_HYPHEN)
7048 nonascii_hyphen_p = true;
7051 /* Translate control characters into `\003' or `^C' form.
7052 Control characters coming from a display table entry are
7053 currently not translated because we use IT->dpvec to hold
7054 the translation. This could easily be changed but I
7055 don't believe that it is worth doing.
7057 The characters handled by `nobreak-char-display' must be
7058 translated too.
7060 Non-printable characters and raw-byte characters are also
7061 translated to octal or hexadecimal form. */
7062 if (((c < ' ' || c == 127) /* ASCII control chars. */
7063 ? (it->area != TEXT_AREA
7064 /* In mode line, treat \n, \t like other crl chars. */
7065 || (c != '\t'
7066 && it->glyph_row
7067 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7068 || (c != '\n' && c != '\t'))
7069 : (nonascii_space_p
7070 || nonascii_hyphen_p
7071 || CHAR_BYTE8_P (c)
7072 || ! CHAR_PRINTABLE_P (c))))
7074 /* C is a control character, non-ASCII space/hyphen,
7075 raw-byte, or a non-printable character which must be
7076 displayed either as '\003' or as `^C' where the '\\'
7077 and '^' can be defined in the display table. Fill
7078 IT->ctl_chars with glyphs for what we have to
7079 display. Then, set IT->dpvec to these glyphs. */
7080 Lisp_Object gc;
7081 int ctl_len;
7082 int face_id;
7083 int lface_id = 0;
7084 int escape_glyph;
7086 /* Handle control characters with ^. */
7088 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7090 int g;
7092 g = '^'; /* default glyph for Control */
7093 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7094 if (it->dp
7095 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7097 g = GLYPH_CODE_CHAR (gc);
7098 lface_id = GLYPH_CODE_FACE (gc);
7101 face_id = (lface_id
7102 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7103 : merge_escape_glyph_face (it));
7105 XSETINT (it->ctl_chars[0], g);
7106 XSETINT (it->ctl_chars[1], c ^ 0100);
7107 ctl_len = 2;
7108 goto display_control;
7111 /* Handle non-ascii space in the mode where it only gets
7112 highlighting. */
7114 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7116 /* Merge `nobreak-space' into the current face. */
7117 face_id = merge_faces (it->f, Qnobreak_space, 0,
7118 it->face_id);
7119 XSETINT (it->ctl_chars[0], ' ');
7120 ctl_len = 1;
7121 goto display_control;
7124 /* Handle non-ascii hyphens in the mode where it only
7125 gets highlighting. */
7127 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7129 /* Merge `nobreak-space' into the current face. */
7130 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7131 it->face_id);
7132 XSETINT (it->ctl_chars[0], '-');
7133 ctl_len = 1;
7134 goto display_control;
7137 /* Handle sequences that start with the "escape glyph". */
7139 /* the default escape glyph is \. */
7140 escape_glyph = '\\';
7142 if (it->dp
7143 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7145 escape_glyph = GLYPH_CODE_CHAR (gc);
7146 lface_id = GLYPH_CODE_FACE (gc);
7149 face_id = (lface_id
7150 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7151 : merge_escape_glyph_face (it));
7153 /* Draw non-ASCII space/hyphen with escape glyph: */
7155 if (nonascii_space_p || nonascii_hyphen_p)
7157 XSETINT (it->ctl_chars[0], escape_glyph);
7158 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7159 ctl_len = 2;
7160 goto display_control;
7164 char str[10];
7165 int len, i;
7167 if (CHAR_BYTE8_P (c))
7168 /* Display \200 or \x80 instead of \17777600. */
7169 c = CHAR_TO_BYTE8 (c);
7170 const char *format_string = display_raw_bytes_as_hex
7171 ? "x%02x"
7172 : "%03o";
7173 len = sprintf (str, format_string, c + 0u);
7175 XSETINT (it->ctl_chars[0], escape_glyph);
7176 for (i = 0; i < len; i++)
7177 XSETINT (it->ctl_chars[i + 1], str[i]);
7178 ctl_len = len + 1;
7181 display_control:
7182 /* Set up IT->dpvec and return first character from it. */
7183 it->dpvec_char_len = it->len;
7184 it->dpvec = it->ctl_chars;
7185 it->dpend = it->dpvec + ctl_len;
7186 it->current.dpvec_index = 0;
7187 it->dpvec_face_id = face_id;
7188 it->saved_face_id = it->face_id;
7189 it->method = GET_FROM_DISPLAY_VECTOR;
7190 it->ellipsis_p = false;
7191 goto get_next;
7193 it->char_to_display = c;
7195 else if (success_p)
7197 it->char_to_display = it->c;
7201 #ifdef HAVE_WINDOW_SYSTEM
7202 /* Adjust face id for a multibyte character. There are no multibyte
7203 character in unibyte text. */
7204 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7205 && it->multibyte_p
7206 && success_p
7207 && FRAME_WINDOW_P (it->f))
7209 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7211 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7213 /* Automatic composition with glyph-string. */
7214 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7216 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7218 else
7220 ptrdiff_t pos = (it->s ? -1
7221 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7222 : IT_CHARPOS (*it));
7223 int c;
7225 if (it->what == IT_CHARACTER)
7226 c = it->char_to_display;
7227 else
7229 struct composition *cmp = composition_table[it->cmp_it.id];
7230 int i;
7232 c = ' ';
7233 for (i = 0; i < cmp->glyph_len; i++)
7234 /* TAB in a composition means display glyphs with
7235 padding space on the left or right. */
7236 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7237 break;
7239 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7242 #endif /* HAVE_WINDOW_SYSTEM */
7244 done:
7245 /* Is this character the last one of a run of characters with
7246 box? If yes, set IT->end_of_box_run_p to true. */
7247 if (it->face_box_p
7248 && it->s == NULL)
7250 if (it->method == GET_FROM_STRING && it->sp)
7252 int face_id = underlying_face_id (it);
7253 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7255 if (face)
7257 if (face->box == FACE_NO_BOX)
7259 /* If the box comes from face properties in a
7260 display string, check faces in that string. */
7261 int string_face_id = face_after_it_pos (it);
7262 it->end_of_box_run_p
7263 = (FACE_FROM_ID (it->f, string_face_id)->box
7264 == FACE_NO_BOX);
7266 /* Otherwise, the box comes from the underlying face.
7267 If this is the last string character displayed, check
7268 the next buffer location. */
7269 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7270 /* n_overlay_strings is unreliable unless
7271 overlay_string_index is non-negative. */
7272 && ((it->current.overlay_string_index >= 0
7273 && (it->current.overlay_string_index
7274 == it->n_overlay_strings - 1))
7275 /* A string from display property. */
7276 || it->from_disp_prop_p))
7278 ptrdiff_t ignore;
7279 int next_face_id;
7280 bool text_from_string = false;
7281 /* Normally, the next buffer location is stored in
7282 IT->current.pos... */
7283 struct text_pos pos = it->current.pos;
7285 /* ...but for a string from a display property, the
7286 next buffer position is stored in the 'position'
7287 member of the iteration stack slot below the
7288 current one, see handle_single_display_spec. By
7289 contrast, it->current.pos was not yet updated to
7290 point to that buffer position; that will happen
7291 in pop_it, after we finish displaying the current
7292 string. Note that we already checked above that
7293 it->sp is positive, so subtracting one from it is
7294 safe. */
7295 if (it->from_disp_prop_p)
7297 int stackp = it->sp - 1;
7299 /* Find the stack level with data from buffer. */
7300 while (stackp >= 0
7301 && STRINGP ((it->stack + stackp)->string))
7302 stackp--;
7303 if (stackp < 0)
7305 /* If no stack slot was found for iterating
7306 a buffer, we are displaying text from a
7307 string, most probably the mode line or
7308 the header line, and that string has a
7309 display string on some of its
7310 characters. */
7311 text_from_string = true;
7312 pos = it->stack[it->sp - 1].position;
7314 else
7315 pos = (it->stack + stackp)->position;
7317 else
7318 INC_TEXT_POS (pos, it->multibyte_p);
7320 if (text_from_string)
7322 Lisp_Object base_string = it->stack[it->sp - 1].string;
7324 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7325 it->end_of_box_run_p = true;
7326 else
7328 next_face_id
7329 = face_at_string_position (it->w, base_string,
7330 CHARPOS (pos), 0,
7331 &ignore, face_id, false);
7332 it->end_of_box_run_p
7333 = (FACE_FROM_ID (it->f, next_face_id)->box
7334 == FACE_NO_BOX);
7337 else if (CHARPOS (pos) >= ZV)
7338 it->end_of_box_run_p = true;
7339 else
7341 next_face_id =
7342 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7343 CHARPOS (pos)
7344 + TEXT_PROP_DISTANCE_LIMIT,
7345 false, -1);
7346 it->end_of_box_run_p
7347 = (FACE_FROM_ID (it->f, next_face_id)->box
7348 == FACE_NO_BOX);
7353 /* next_element_from_display_vector sets this flag according to
7354 faces of the display vector glyphs, see there. */
7355 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7357 int face_id = face_after_it_pos (it);
7358 it->end_of_box_run_p
7359 = (face_id != it->face_id
7360 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7363 /* If we reached the end of the object we've been iterating (e.g., a
7364 display string or an overlay string), and there's something on
7365 IT->stack, proceed with what's on the stack. It doesn't make
7366 sense to return false if there's unprocessed stuff on the stack,
7367 because otherwise that stuff will never be displayed. */
7368 if (!success_p && it->sp > 0)
7370 set_iterator_to_next (it, false);
7371 success_p = get_next_display_element (it);
7374 /* Value is false if end of buffer or string reached. */
7375 return success_p;
7379 /* Move IT to the next display element.
7381 RESEAT_P means if called on a newline in buffer text,
7382 skip to the next visible line start.
7384 Functions get_next_display_element and set_iterator_to_next are
7385 separate because I find this arrangement easier to handle than a
7386 get_next_display_element function that also increments IT's
7387 position. The way it is we can first look at an iterator's current
7388 display element, decide whether it fits on a line, and if it does,
7389 increment the iterator position. The other way around we probably
7390 would either need a flag indicating whether the iterator has to be
7391 incremented the next time, or we would have to implement a
7392 decrement position function which would not be easy to write. */
7394 void
7395 set_iterator_to_next (struct it *it, bool reseat_p)
7397 /* Reset flags indicating start and end of a sequence of characters
7398 with box. Reset them at the start of this function because
7399 moving the iterator to a new position might set them. */
7400 it->start_of_box_run_p = it->end_of_box_run_p = false;
7402 switch (it->method)
7404 case GET_FROM_BUFFER:
7405 /* The current display element of IT is a character from
7406 current_buffer. Advance in the buffer, and maybe skip over
7407 invisible lines that are so because of selective display. */
7408 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7409 reseat_at_next_visible_line_start (it, false);
7410 else if (it->cmp_it.id >= 0)
7412 /* We are currently getting glyphs from a composition. */
7413 if (! it->bidi_p)
7415 IT_CHARPOS (*it) += it->cmp_it.nchars;
7416 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7418 else
7420 int i;
7422 /* Update IT's char/byte positions to point to the first
7423 character of the next grapheme cluster, or to the
7424 character visually after the current composition. */
7425 for (i = 0; i < it->cmp_it.nchars; i++)
7426 bidi_move_to_visually_next (&it->bidi_it);
7427 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7428 IT_CHARPOS (*it) = it->bidi_it.charpos;
7431 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7432 && it->cmp_it.to < it->cmp_it.nglyphs)
7434 /* Composition created while scanning forward. Proceed
7435 to the next grapheme cluster. */
7436 it->cmp_it.from = it->cmp_it.to;
7438 else if ((it->bidi_p && it->cmp_it.reversed_p)
7439 && it->cmp_it.from > 0)
7441 /* Composition created while scanning backward. Proceed
7442 to the previous grapheme cluster. */
7443 it->cmp_it.to = it->cmp_it.from;
7445 else
7447 /* No more grapheme clusters in this composition.
7448 Find the next stop position. */
7449 ptrdiff_t stop = it->end_charpos;
7451 if (it->bidi_it.scan_dir < 0)
7452 /* Now we are scanning backward and don't know
7453 where to stop. */
7454 stop = -1;
7455 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7456 IT_BYTEPOS (*it), stop, Qnil);
7459 else
7461 eassert (it->len != 0);
7463 if (!it->bidi_p)
7465 IT_BYTEPOS (*it) += it->len;
7466 IT_CHARPOS (*it) += 1;
7468 else
7470 int prev_scan_dir = it->bidi_it.scan_dir;
7471 /* If this is a new paragraph, determine its base
7472 direction (a.k.a. its base embedding level). */
7473 if (it->bidi_it.new_paragraph)
7474 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7475 false);
7476 bidi_move_to_visually_next (&it->bidi_it);
7477 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7478 IT_CHARPOS (*it) = it->bidi_it.charpos;
7479 if (prev_scan_dir != it->bidi_it.scan_dir)
7481 /* As the scan direction was changed, we must
7482 re-compute the stop position for composition. */
7483 ptrdiff_t stop = it->end_charpos;
7484 if (it->bidi_it.scan_dir < 0)
7485 stop = -1;
7486 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7487 IT_BYTEPOS (*it), stop, Qnil);
7490 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7492 break;
7494 case GET_FROM_C_STRING:
7495 /* Current display element of IT is from a C string. */
7496 if (!it->bidi_p
7497 /* If the string position is beyond string's end, it means
7498 next_element_from_c_string is padding the string with
7499 blanks, in which case we bypass the bidi iterator,
7500 because it cannot deal with such virtual characters. */
7501 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7503 IT_BYTEPOS (*it) += it->len;
7504 IT_CHARPOS (*it) += 1;
7506 else
7508 bidi_move_to_visually_next (&it->bidi_it);
7509 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7510 IT_CHARPOS (*it) = it->bidi_it.charpos;
7512 break;
7514 case GET_FROM_DISPLAY_VECTOR:
7515 /* Current display element of IT is from a display table entry.
7516 Advance in the display table definition. Reset it to null if
7517 end reached, and continue with characters from buffers/
7518 strings. */
7519 ++it->current.dpvec_index;
7521 /* Restore face of the iterator to what they were before the
7522 display vector entry (these entries may contain faces). */
7523 it->face_id = it->saved_face_id;
7525 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7527 bool recheck_faces = it->ellipsis_p;
7529 if (it->s)
7530 it->method = GET_FROM_C_STRING;
7531 else if (STRINGP (it->string))
7532 it->method = GET_FROM_STRING;
7533 else
7535 it->method = GET_FROM_BUFFER;
7536 it->object = it->w->contents;
7539 it->dpvec = NULL;
7540 it->current.dpvec_index = -1;
7542 /* Skip over characters which were displayed via IT->dpvec. */
7543 if (it->dpvec_char_len < 0)
7544 reseat_at_next_visible_line_start (it, true);
7545 else if (it->dpvec_char_len > 0)
7547 it->len = it->dpvec_char_len;
7548 set_iterator_to_next (it, reseat_p);
7551 /* Maybe recheck faces after display vector. */
7552 if (recheck_faces)
7554 if (it->method == GET_FROM_STRING)
7555 it->stop_charpos = IT_STRING_CHARPOS (*it);
7556 else
7557 it->stop_charpos = IT_CHARPOS (*it);
7560 break;
7562 case GET_FROM_STRING:
7563 /* Current display element is a character from a Lisp string. */
7564 eassert (it->s == NULL && STRINGP (it->string));
7565 /* Don't advance past string end. These conditions are true
7566 when set_iterator_to_next is called at the end of
7567 get_next_display_element, in which case the Lisp string is
7568 already exhausted, and all we want is pop the iterator
7569 stack. */
7570 if (it->current.overlay_string_index >= 0)
7572 /* This is an overlay string, so there's no padding with
7573 spaces, and the number of characters in the string is
7574 where the string ends. */
7575 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7576 goto consider_string_end;
7578 else
7580 /* Not an overlay string. There could be padding, so test
7581 against it->end_charpos. */
7582 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7583 goto consider_string_end;
7585 if (it->cmp_it.id >= 0)
7587 /* We are delivering display elements from a composition.
7588 Update the string position past the grapheme cluster
7589 we've just processed. */
7590 if (! it->bidi_p)
7592 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7593 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7595 else
7597 int i;
7599 for (i = 0; i < it->cmp_it.nchars; i++)
7600 bidi_move_to_visually_next (&it->bidi_it);
7601 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7602 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7605 /* Did we exhaust all the grapheme clusters of this
7606 composition? */
7607 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7608 && (it->cmp_it.to < it->cmp_it.nglyphs))
7610 /* Not all the grapheme clusters were processed yet;
7611 advance to the next cluster. */
7612 it->cmp_it.from = it->cmp_it.to;
7614 else if ((it->bidi_p && it->cmp_it.reversed_p)
7615 && it->cmp_it.from > 0)
7617 /* Likewise: advance to the next cluster, but going in
7618 the reverse direction. */
7619 it->cmp_it.to = it->cmp_it.from;
7621 else
7623 /* This composition was fully processed; find the next
7624 candidate place for checking for composed
7625 characters. */
7626 /* Always limit string searches to the string length;
7627 any padding spaces are not part of the string, and
7628 there cannot be any compositions in that padding. */
7629 ptrdiff_t stop = SCHARS (it->string);
7631 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7632 stop = -1;
7633 else if (it->end_charpos < stop)
7635 /* Cf. PRECISION in reseat_to_string: we might be
7636 limited in how many of the string characters we
7637 need to deliver. */
7638 stop = it->end_charpos;
7640 composition_compute_stop_pos (&it->cmp_it,
7641 IT_STRING_CHARPOS (*it),
7642 IT_STRING_BYTEPOS (*it), stop,
7643 it->string);
7646 else
7648 if (!it->bidi_p
7649 /* If the string position is beyond string's end, it
7650 means next_element_from_string is padding the string
7651 with blanks, in which case we bypass the bidi
7652 iterator, because it cannot deal with such virtual
7653 characters. */
7654 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7656 IT_STRING_BYTEPOS (*it) += it->len;
7657 IT_STRING_CHARPOS (*it) += 1;
7659 else
7661 int prev_scan_dir = it->bidi_it.scan_dir;
7663 bidi_move_to_visually_next (&it->bidi_it);
7664 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7665 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7666 /* If the scan direction changes, we may need to update
7667 the place where to check for composed characters. */
7668 if (prev_scan_dir != it->bidi_it.scan_dir)
7670 ptrdiff_t stop = SCHARS (it->string);
7672 if (it->bidi_it.scan_dir < 0)
7673 stop = -1;
7674 else if (it->end_charpos < stop)
7675 stop = it->end_charpos;
7677 composition_compute_stop_pos (&it->cmp_it,
7678 IT_STRING_CHARPOS (*it),
7679 IT_STRING_BYTEPOS (*it), stop,
7680 it->string);
7685 consider_string_end:
7687 if (it->current.overlay_string_index >= 0)
7689 /* IT->string is an overlay string. Advance to the
7690 next, if there is one. */
7691 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7693 it->ellipsis_p = false;
7694 next_overlay_string (it);
7695 if (it->ellipsis_p)
7696 setup_for_ellipsis (it, 0);
7699 else
7701 /* IT->string is not an overlay string. If we reached
7702 its end, and there is something on IT->stack, proceed
7703 with what is on the stack. This can be either another
7704 string, this time an overlay string, or a buffer. */
7705 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7706 && it->sp > 0)
7708 pop_it (it);
7709 if (it->method == GET_FROM_STRING)
7710 goto consider_string_end;
7713 break;
7715 case GET_FROM_IMAGE:
7716 case GET_FROM_STRETCH:
7717 case GET_FROM_XWIDGET:
7719 /* The position etc with which we have to proceed are on
7720 the stack. The position may be at the end of a string,
7721 if the `display' property takes up the whole string. */
7722 eassert (it->sp > 0);
7723 pop_it (it);
7724 if (it->method == GET_FROM_STRING)
7725 goto consider_string_end;
7726 break;
7728 default:
7729 /* There are no other methods defined, so this should be a bug. */
7730 emacs_abort ();
7733 eassert (it->method != GET_FROM_STRING
7734 || (STRINGP (it->string)
7735 && IT_STRING_CHARPOS (*it) >= 0));
7738 /* Load IT's display element fields with information about the next
7739 display element which comes from a display table entry or from the
7740 result of translating a control character to one of the forms `^C'
7741 or `\003'.
7743 IT->dpvec holds the glyphs to return as characters.
7744 IT->saved_face_id holds the face id before the display vector--it
7745 is restored into IT->face_id in set_iterator_to_next. */
7747 static bool
7748 next_element_from_display_vector (struct it *it)
7750 Lisp_Object gc;
7751 int prev_face_id = it->face_id;
7752 int next_face_id;
7754 /* Precondition. */
7755 eassert (it->dpvec && it->current.dpvec_index >= 0);
7757 it->face_id = it->saved_face_id;
7759 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7760 That seemed totally bogus - so I changed it... */
7761 gc = it->dpvec[it->current.dpvec_index];
7763 if (GLYPH_CODE_P (gc))
7765 struct face *this_face, *prev_face, *next_face;
7767 it->c = GLYPH_CODE_CHAR (gc);
7768 it->len = CHAR_BYTES (it->c);
7770 /* The entry may contain a face id to use. Such a face id is
7771 the id of a Lisp face, not a realized face. A face id of
7772 zero means no face is specified. */
7773 if (it->dpvec_face_id >= 0)
7774 it->face_id = it->dpvec_face_id;
7775 else
7777 int lface_id = GLYPH_CODE_FACE (gc);
7778 if (lface_id > 0)
7779 it->face_id = merge_faces (it->f, Qt, lface_id,
7780 it->saved_face_id);
7783 /* Glyphs in the display vector could have the box face, so we
7784 need to set the related flags in the iterator, as
7785 appropriate. */
7786 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7787 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7789 /* Is this character the first character of a box-face run? */
7790 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7791 && (!prev_face
7792 || prev_face->box == FACE_NO_BOX));
7794 /* For the last character of the box-face run, we need to look
7795 either at the next glyph from the display vector, or at the
7796 face we saw before the display vector. */
7797 next_face_id = it->saved_face_id;
7798 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7800 if (it->dpvec_face_id >= 0)
7801 next_face_id = it->dpvec_face_id;
7802 else
7804 int lface_id =
7805 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7807 if (lface_id > 0)
7808 next_face_id = merge_faces (it->f, Qt, lface_id,
7809 it->saved_face_id);
7812 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7813 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7814 && (!next_face
7815 || next_face->box == FACE_NO_BOX));
7816 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7818 else
7819 /* Display table entry is invalid. Return a space. */
7820 it->c = ' ', it->len = 1;
7822 /* Don't change position and object of the iterator here. They are
7823 still the values of the character that had this display table
7824 entry or was translated, and that's what we want. */
7825 it->what = IT_CHARACTER;
7826 return true;
7829 /* Get the first element of string/buffer in the visual order, after
7830 being reseated to a new position in a string or a buffer. */
7831 static void
7832 get_visually_first_element (struct it *it)
7834 bool string_p = STRINGP (it->string) || it->s;
7835 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7836 ptrdiff_t bob = (string_p ? 0 : BEGV);
7838 if (STRINGP (it->string))
7840 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7841 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7843 else
7845 it->bidi_it.charpos = IT_CHARPOS (*it);
7846 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7849 if (it->bidi_it.charpos == eob)
7851 /* Nothing to do, but reset the FIRST_ELT flag, like
7852 bidi_paragraph_init does, because we are not going to
7853 call it. */
7854 it->bidi_it.first_elt = false;
7856 else if (it->bidi_it.charpos == bob
7857 || (!string_p
7858 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7859 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7861 /* If we are at the beginning of a line/string, we can produce
7862 the next element right away. */
7863 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7864 bidi_move_to_visually_next (&it->bidi_it);
7866 else
7868 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7870 /* We need to prime the bidi iterator starting at the line's or
7871 string's beginning, before we will be able to produce the
7872 next element. */
7873 if (string_p)
7874 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7875 else
7876 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7877 IT_BYTEPOS (*it), -1,
7878 &it->bidi_it.bytepos);
7879 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7882 /* Now return to buffer/string position where we were asked
7883 to get the next display element, and produce that. */
7884 bidi_move_to_visually_next (&it->bidi_it);
7886 while (it->bidi_it.bytepos != orig_bytepos
7887 && it->bidi_it.charpos < eob);
7890 /* Adjust IT's position information to where we ended up. */
7891 if (STRINGP (it->string))
7893 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7894 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7896 else
7898 IT_CHARPOS (*it) = it->bidi_it.charpos;
7899 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7902 if (STRINGP (it->string) || !it->s)
7904 ptrdiff_t stop, charpos, bytepos;
7906 if (STRINGP (it->string))
7908 eassert (!it->s);
7909 stop = SCHARS (it->string);
7910 if (stop > it->end_charpos)
7911 stop = it->end_charpos;
7912 charpos = IT_STRING_CHARPOS (*it);
7913 bytepos = IT_STRING_BYTEPOS (*it);
7915 else
7917 stop = it->end_charpos;
7918 charpos = IT_CHARPOS (*it);
7919 bytepos = IT_BYTEPOS (*it);
7921 if (it->bidi_it.scan_dir < 0)
7922 stop = -1;
7923 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7924 it->string);
7928 /* Load IT with the next display element from Lisp string IT->string.
7929 IT->current.string_pos is the current position within the string.
7930 If IT->current.overlay_string_index >= 0, the Lisp string is an
7931 overlay string. */
7933 static bool
7934 next_element_from_string (struct it *it)
7936 struct text_pos position;
7938 eassert (STRINGP (it->string));
7939 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7940 eassert (IT_STRING_CHARPOS (*it) >= 0);
7941 position = it->current.string_pos;
7943 /* With bidi reordering, the character to display might not be the
7944 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7945 that we were reseat()ed to a new string, whose paragraph
7946 direction is not known. */
7947 if (it->bidi_p && it->bidi_it.first_elt)
7949 get_visually_first_element (it);
7950 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7953 /* Time to check for invisible text? */
7954 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7956 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7958 if (!(!it->bidi_p
7959 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7960 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7962 /* With bidi non-linear iteration, we could find
7963 ourselves far beyond the last computed stop_charpos,
7964 with several other stop positions in between that we
7965 missed. Scan them all now, in buffer's logical
7966 order, until we find and handle the last stop_charpos
7967 that precedes our current position. */
7968 handle_stop_backwards (it, it->stop_charpos);
7969 return GET_NEXT_DISPLAY_ELEMENT (it);
7971 else
7973 if (it->bidi_p)
7975 /* Take note of the stop position we just moved
7976 across, for when we will move back across it. */
7977 it->prev_stop = it->stop_charpos;
7978 /* If we are at base paragraph embedding level, take
7979 note of the last stop position seen at this
7980 level. */
7981 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7982 it->base_level_stop = it->stop_charpos;
7984 handle_stop (it);
7986 /* Since a handler may have changed IT->method, we must
7987 recurse here. */
7988 return GET_NEXT_DISPLAY_ELEMENT (it);
7991 else if (it->bidi_p
7992 /* If we are before prev_stop, we may have overstepped
7993 on our way backwards a stop_pos, and if so, we need
7994 to handle that stop_pos. */
7995 && IT_STRING_CHARPOS (*it) < it->prev_stop
7996 /* We can sometimes back up for reasons that have nothing
7997 to do with bidi reordering. E.g., compositions. The
7998 code below is only needed when we are above the base
7999 embedding level, so test for that explicitly. */
8000 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8002 /* If we lost track of base_level_stop, we have no better
8003 place for handle_stop_backwards to start from than string
8004 beginning. This happens, e.g., when we were reseated to
8005 the previous screenful of text by vertical-motion. */
8006 if (it->base_level_stop <= 0
8007 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8008 it->base_level_stop = 0;
8009 handle_stop_backwards (it, it->base_level_stop);
8010 return GET_NEXT_DISPLAY_ELEMENT (it);
8014 if (it->current.overlay_string_index >= 0)
8016 /* Get the next character from an overlay string. In overlay
8017 strings, there is no field width or padding with spaces to
8018 do. */
8019 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8021 it->what = IT_EOB;
8022 return false;
8024 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8025 IT_STRING_BYTEPOS (*it),
8026 it->bidi_it.scan_dir < 0
8027 ? -1
8028 : SCHARS (it->string))
8029 && next_element_from_composition (it))
8031 return true;
8033 else if (STRING_MULTIBYTE (it->string))
8035 const unsigned char *s = (SDATA (it->string)
8036 + IT_STRING_BYTEPOS (*it));
8037 it->c = string_char_and_length (s, &it->len);
8039 else
8041 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8042 it->len = 1;
8045 else
8047 /* Get the next character from a Lisp string that is not an
8048 overlay string. Such strings come from the mode line, for
8049 example. We may have to pad with spaces, or truncate the
8050 string. See also next_element_from_c_string. */
8051 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8053 it->what = IT_EOB;
8054 return false;
8056 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8058 /* Pad with spaces. */
8059 it->c = ' ', it->len = 1;
8060 CHARPOS (position) = BYTEPOS (position) = -1;
8062 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8063 IT_STRING_BYTEPOS (*it),
8064 it->bidi_it.scan_dir < 0
8065 ? -1
8066 : it->string_nchars)
8067 && next_element_from_composition (it))
8069 return true;
8071 else if (STRING_MULTIBYTE (it->string))
8073 const unsigned char *s = (SDATA (it->string)
8074 + IT_STRING_BYTEPOS (*it));
8075 it->c = string_char_and_length (s, &it->len);
8077 else
8079 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8080 it->len = 1;
8084 /* Record what we have and where it came from. */
8085 it->what = IT_CHARACTER;
8086 it->object = it->string;
8087 it->position = position;
8088 return true;
8092 /* Load IT with next display element from C string IT->s.
8093 IT->string_nchars is the maximum number of characters to return
8094 from the string. IT->end_charpos may be greater than
8095 IT->string_nchars when this function is called, in which case we
8096 may have to return padding spaces. Value is false if end of string
8097 reached, including padding spaces. */
8099 static bool
8100 next_element_from_c_string (struct it *it)
8102 bool success_p = true;
8104 eassert (it->s);
8105 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8106 it->what = IT_CHARACTER;
8107 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8108 it->object = make_number (0);
8110 /* With bidi reordering, the character to display might not be the
8111 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8112 we were reseated to a new string, whose paragraph direction is
8113 not known. */
8114 if (it->bidi_p && it->bidi_it.first_elt)
8115 get_visually_first_element (it);
8117 /* IT's position can be greater than IT->string_nchars in case a
8118 field width or precision has been specified when the iterator was
8119 initialized. */
8120 if (IT_CHARPOS (*it) >= it->end_charpos)
8122 /* End of the game. */
8123 it->what = IT_EOB;
8124 success_p = false;
8126 else if (IT_CHARPOS (*it) >= it->string_nchars)
8128 /* Pad with spaces. */
8129 it->c = ' ', it->len = 1;
8130 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8132 else if (it->multibyte_p)
8133 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8134 else
8135 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8137 return success_p;
8141 /* Set up IT to return characters from an ellipsis, if appropriate.
8142 The definition of the ellipsis glyphs may come from a display table
8143 entry. This function fills IT with the first glyph from the
8144 ellipsis if an ellipsis is to be displayed. */
8146 static bool
8147 next_element_from_ellipsis (struct it *it)
8149 if (it->selective_display_ellipsis_p)
8150 setup_for_ellipsis (it, it->len);
8151 else
8153 /* The face at the current position may be different from the
8154 face we find after the invisible text. Remember what it
8155 was in IT->saved_face_id, and signal that it's there by
8156 setting face_before_selective_p. */
8157 it->saved_face_id = it->face_id;
8158 it->method = GET_FROM_BUFFER;
8159 it->object = it->w->contents;
8160 reseat_at_next_visible_line_start (it, true);
8161 it->face_before_selective_p = true;
8164 return GET_NEXT_DISPLAY_ELEMENT (it);
8168 /* Deliver an image display element. The iterator IT is already
8169 filled with image information (done in handle_display_prop). Value
8170 is always true. */
8173 static bool
8174 next_element_from_image (struct it *it)
8176 it->what = IT_IMAGE;
8177 return true;
8180 static bool
8181 next_element_from_xwidget (struct it *it)
8183 it->what = IT_XWIDGET;
8184 return true;
8188 /* Fill iterator IT with next display element from a stretch glyph
8189 property. IT->object is the value of the text property. Value is
8190 always true. */
8192 static bool
8193 next_element_from_stretch (struct it *it)
8195 it->what = IT_STRETCH;
8196 return true;
8199 /* Scan backwards from IT's current position until we find a stop
8200 position, or until BEGV. This is called when we find ourself
8201 before both the last known prev_stop and base_level_stop while
8202 reordering bidirectional text. */
8204 static void
8205 compute_stop_pos_backwards (struct it *it)
8207 const int SCAN_BACK_LIMIT = 1000;
8208 struct text_pos pos;
8209 struct display_pos save_current = it->current;
8210 struct text_pos save_position = it->position;
8211 ptrdiff_t charpos = IT_CHARPOS (*it);
8212 ptrdiff_t where_we_are = charpos;
8213 ptrdiff_t save_stop_pos = it->stop_charpos;
8214 ptrdiff_t save_end_pos = it->end_charpos;
8216 eassert (NILP (it->string) && !it->s);
8217 eassert (it->bidi_p);
8218 it->bidi_p = false;
8221 it->end_charpos = min (charpos + 1, ZV);
8222 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8223 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8224 reseat_1 (it, pos, false);
8225 compute_stop_pos (it);
8226 /* We must advance forward, right? */
8227 if (it->stop_charpos <= charpos)
8228 emacs_abort ();
8230 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8232 if (it->stop_charpos <= where_we_are)
8233 it->prev_stop = it->stop_charpos;
8234 else
8235 it->prev_stop = BEGV;
8236 it->bidi_p = true;
8237 it->current = save_current;
8238 it->position = save_position;
8239 it->stop_charpos = save_stop_pos;
8240 it->end_charpos = save_end_pos;
8243 /* Scan forward from CHARPOS in the current buffer/string, until we
8244 find a stop position > current IT's position. Then handle the stop
8245 position before that. This is called when we bump into a stop
8246 position while reordering bidirectional text. CHARPOS should be
8247 the last previously processed stop_pos (or BEGV/0, if none were
8248 processed yet) whose position is less that IT's current
8249 position. */
8251 static void
8252 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8254 bool bufp = !STRINGP (it->string);
8255 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8256 struct display_pos save_current = it->current;
8257 struct text_pos save_position = it->position;
8258 struct text_pos pos1;
8259 ptrdiff_t next_stop;
8261 /* Scan in strict logical order. */
8262 eassert (it->bidi_p);
8263 it->bidi_p = false;
8266 it->prev_stop = charpos;
8267 if (bufp)
8269 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8270 reseat_1 (it, pos1, false);
8272 else
8273 it->current.string_pos = string_pos (charpos, it->string);
8274 compute_stop_pos (it);
8275 /* We must advance forward, right? */
8276 if (it->stop_charpos <= it->prev_stop)
8277 emacs_abort ();
8278 charpos = it->stop_charpos;
8280 while (charpos <= where_we_are);
8282 it->bidi_p = true;
8283 it->current = save_current;
8284 it->position = save_position;
8285 next_stop = it->stop_charpos;
8286 it->stop_charpos = it->prev_stop;
8287 handle_stop (it);
8288 it->stop_charpos = next_stop;
8291 /* Load IT with the next display element from current_buffer. Value
8292 is false if end of buffer reached. IT->stop_charpos is the next
8293 position at which to stop and check for text properties or buffer
8294 end. */
8296 static bool
8297 next_element_from_buffer (struct it *it)
8299 bool success_p = true;
8301 eassert (IT_CHARPOS (*it) >= BEGV);
8302 eassert (NILP (it->string) && !it->s);
8303 eassert (!it->bidi_p
8304 || (EQ (it->bidi_it.string.lstring, Qnil)
8305 && it->bidi_it.string.s == NULL));
8307 /* With bidi reordering, the character to display might not be the
8308 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8309 we were reseat()ed to a new buffer position, which is potentially
8310 a different paragraph. */
8311 if (it->bidi_p && it->bidi_it.first_elt)
8313 get_visually_first_element (it);
8314 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8317 if (IT_CHARPOS (*it) >= it->stop_charpos)
8319 if (IT_CHARPOS (*it) >= it->end_charpos)
8321 bool overlay_strings_follow_p;
8323 /* End of the game, except when overlay strings follow that
8324 haven't been returned yet. */
8325 if (it->overlay_strings_at_end_processed_p)
8326 overlay_strings_follow_p = false;
8327 else
8329 it->overlay_strings_at_end_processed_p = true;
8330 overlay_strings_follow_p = get_overlay_strings (it, 0);
8333 if (overlay_strings_follow_p)
8334 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8335 else
8337 it->what = IT_EOB;
8338 it->position = it->current.pos;
8339 success_p = false;
8342 else if (!(!it->bidi_p
8343 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8344 || IT_CHARPOS (*it) == it->stop_charpos))
8346 /* With bidi non-linear iteration, we could find ourselves
8347 far beyond the last computed stop_charpos, with several
8348 other stop positions in between that we missed. Scan
8349 them all now, in buffer's logical order, until we find
8350 and handle the last stop_charpos that precedes our
8351 current position. */
8352 handle_stop_backwards (it, it->stop_charpos);
8353 it->ignore_overlay_strings_at_pos_p = false;
8354 return GET_NEXT_DISPLAY_ELEMENT (it);
8356 else
8358 if (it->bidi_p)
8360 /* Take note of the stop position we just moved across,
8361 for when we will move back across it. */
8362 it->prev_stop = it->stop_charpos;
8363 /* If we are at base paragraph embedding level, take
8364 note of the last stop position seen at this
8365 level. */
8366 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8367 it->base_level_stop = it->stop_charpos;
8369 handle_stop (it);
8370 it->ignore_overlay_strings_at_pos_p = false;
8371 return GET_NEXT_DISPLAY_ELEMENT (it);
8374 else if (it->bidi_p
8375 /* If we are before prev_stop, we may have overstepped on
8376 our way backwards a stop_pos, and if so, we need to
8377 handle that stop_pos. */
8378 && IT_CHARPOS (*it) < it->prev_stop
8379 /* We can sometimes back up for reasons that have nothing
8380 to do with bidi reordering. E.g., compositions. The
8381 code below is only needed when we are above the base
8382 embedding level, so test for that explicitly. */
8383 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8385 if (it->base_level_stop <= 0
8386 || IT_CHARPOS (*it) < it->base_level_stop)
8388 /* If we lost track of base_level_stop, we need to find
8389 prev_stop by looking backwards. This happens, e.g., when
8390 we were reseated to the previous screenful of text by
8391 vertical-motion. */
8392 it->base_level_stop = BEGV;
8393 compute_stop_pos_backwards (it);
8394 handle_stop_backwards (it, it->prev_stop);
8396 else
8397 handle_stop_backwards (it, it->base_level_stop);
8398 it->ignore_overlay_strings_at_pos_p = false;
8399 return GET_NEXT_DISPLAY_ELEMENT (it);
8401 else
8403 /* No face changes, overlays etc. in sight, so just return a
8404 character from current_buffer. */
8405 unsigned char *p;
8406 ptrdiff_t stop;
8408 /* We moved to the next buffer position, so any info about
8409 previously seen overlays is no longer valid. */
8410 it->ignore_overlay_strings_at_pos_p = false;
8412 /* Maybe run the redisplay end trigger hook. Performance note:
8413 This doesn't seem to cost measurable time. */
8414 if (it->redisplay_end_trigger_charpos
8415 && it->glyph_row
8416 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8417 run_redisplay_end_trigger_hook (it);
8419 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8420 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8421 stop)
8422 && next_element_from_composition (it))
8424 return true;
8427 /* Get the next character, maybe multibyte. */
8428 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8429 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8430 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8431 else
8432 it->c = *p, it->len = 1;
8434 /* Record what we have and where it came from. */
8435 it->what = IT_CHARACTER;
8436 it->object = it->w->contents;
8437 it->position = it->current.pos;
8439 /* Normally we return the character found above, except when we
8440 really want to return an ellipsis for selective display. */
8441 if (it->selective)
8443 if (it->c == '\n')
8445 /* A value of selective > 0 means hide lines indented more
8446 than that number of columns. */
8447 if (it->selective > 0
8448 && IT_CHARPOS (*it) + 1 < ZV
8449 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8450 IT_BYTEPOS (*it) + 1,
8451 it->selective))
8453 success_p = next_element_from_ellipsis (it);
8454 it->dpvec_char_len = -1;
8457 else if (it->c == '\r' && it->selective == -1)
8459 /* A value of selective == -1 means that everything from the
8460 CR to the end of the line is invisible, with maybe an
8461 ellipsis displayed for it. */
8462 success_p = next_element_from_ellipsis (it);
8463 it->dpvec_char_len = -1;
8468 /* Value is false if end of buffer reached. */
8469 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8470 return success_p;
8474 /* Run the redisplay end trigger hook for IT. */
8476 static void
8477 run_redisplay_end_trigger_hook (struct it *it)
8479 /* IT->glyph_row should be non-null, i.e. we should be actually
8480 displaying something, or otherwise we should not run the hook. */
8481 eassert (it->glyph_row);
8483 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8484 it->redisplay_end_trigger_charpos = 0;
8486 /* Since we are *trying* to run these functions, don't try to run
8487 them again, even if they get an error. */
8488 wset_redisplay_end_trigger (it->w, Qnil);
8489 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8490 make_number (charpos));
8492 /* Notice if it changed the face of the character we are on. */
8493 handle_face_prop (it);
8497 /* Deliver a composition display element. Unlike the other
8498 next_element_from_XXX, this function is not registered in the array
8499 get_next_element[]. It is called from next_element_from_buffer and
8500 next_element_from_string when necessary. */
8502 static bool
8503 next_element_from_composition (struct it *it)
8505 it->what = IT_COMPOSITION;
8506 it->len = it->cmp_it.nbytes;
8507 if (STRINGP (it->string))
8509 if (it->c < 0)
8511 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8512 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8513 return false;
8515 it->position = it->current.string_pos;
8516 it->object = it->string;
8517 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8518 IT_STRING_BYTEPOS (*it), it->string);
8520 else
8522 if (it->c < 0)
8524 IT_CHARPOS (*it) += it->cmp_it.nchars;
8525 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8526 if (it->bidi_p)
8528 if (it->bidi_it.new_paragraph)
8529 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8530 false);
8531 /* Resync the bidi iterator with IT's new position.
8532 FIXME: this doesn't support bidirectional text. */
8533 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8534 bidi_move_to_visually_next (&it->bidi_it);
8536 return false;
8538 it->position = it->current.pos;
8539 it->object = it->w->contents;
8540 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8541 IT_BYTEPOS (*it), Qnil);
8543 return true;
8548 /***********************************************************************
8549 Moving an iterator without producing glyphs
8550 ***********************************************************************/
8552 /* Check if iterator is at a position corresponding to a valid buffer
8553 position after some move_it_ call. */
8555 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8556 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8559 /* Move iterator IT to a specified buffer or X position within one
8560 line on the display without producing glyphs.
8562 OP should be a bit mask including some or all of these bits:
8563 MOVE_TO_X: Stop upon reaching x-position TO_X.
8564 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8565 Regardless of OP's value, stop upon reaching the end of the display line.
8567 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8568 This means, in particular, that TO_X includes window's horizontal
8569 scroll amount.
8571 The return value has several possible values that
8572 say what condition caused the scan to stop:
8574 MOVE_POS_MATCH_OR_ZV
8575 - when TO_POS or ZV was reached.
8577 MOVE_X_REACHED
8578 -when TO_X was reached before TO_POS or ZV were reached.
8580 MOVE_LINE_CONTINUED
8581 - when we reached the end of the display area and the line must
8582 be continued.
8584 MOVE_LINE_TRUNCATED
8585 - when we reached the end of the display area and the line is
8586 truncated.
8588 MOVE_NEWLINE_OR_CR
8589 - when we stopped at a line end, i.e. a newline or a CR and selective
8590 display is on. */
8592 static enum move_it_result
8593 move_it_in_display_line_to (struct it *it,
8594 ptrdiff_t to_charpos, int to_x,
8595 enum move_operation_enum op)
8597 enum move_it_result result = MOVE_UNDEFINED;
8598 struct glyph_row *saved_glyph_row;
8599 struct it wrap_it, atpos_it, atx_it, ppos_it;
8600 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8601 void *ppos_data = NULL;
8602 bool may_wrap = false;
8603 enum it_method prev_method = it->method;
8604 ptrdiff_t closest_pos UNINIT;
8605 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8606 bool saw_smaller_pos = prev_pos < to_charpos;
8608 /* Don't produce glyphs in produce_glyphs. */
8609 saved_glyph_row = it->glyph_row;
8610 it->glyph_row = NULL;
8612 /* Use wrap_it to save a copy of IT wherever a word wrap could
8613 occur. Use atpos_it to save a copy of IT at the desired buffer
8614 position, if found, so that we can scan ahead and check if the
8615 word later overshoots the window edge. Use atx_it similarly, for
8616 pixel positions. */
8617 wrap_it.sp = -1;
8618 atpos_it.sp = -1;
8619 atx_it.sp = -1;
8621 /* Use ppos_it under bidi reordering to save a copy of IT for the
8622 initial position. We restore that position in IT when we have
8623 scanned the entire display line without finding a match for
8624 TO_CHARPOS and all the character positions are greater than
8625 TO_CHARPOS. We then restart the scan from the initial position,
8626 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8627 the closest to TO_CHARPOS. */
8628 if (it->bidi_p)
8630 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8632 SAVE_IT (ppos_it, *it, ppos_data);
8633 closest_pos = IT_CHARPOS (*it);
8635 else
8636 closest_pos = ZV;
8639 #define BUFFER_POS_REACHED_P() \
8640 ((op & MOVE_TO_POS) != 0 \
8641 && BUFFERP (it->object) \
8642 && (IT_CHARPOS (*it) == to_charpos \
8643 || ((!it->bidi_p \
8644 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8645 && IT_CHARPOS (*it) > to_charpos) \
8646 || (it->what == IT_COMPOSITION \
8647 && ((IT_CHARPOS (*it) > to_charpos \
8648 && to_charpos >= it->cmp_it.charpos) \
8649 || (IT_CHARPOS (*it) < to_charpos \
8650 && to_charpos <= it->cmp_it.charpos)))) \
8651 && (it->method == GET_FROM_BUFFER \
8652 || (it->method == GET_FROM_DISPLAY_VECTOR \
8653 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8655 /* If there's a line-/wrap-prefix, handle it. */
8656 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8657 handle_line_prefix (it);
8659 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8660 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8662 while (true)
8664 int x, i, ascent = 0, descent = 0;
8666 /* Utility macro to reset an iterator with x, ascent, and descent. */
8667 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8668 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8669 (IT)->max_descent = descent)
8671 /* Stop if we move beyond TO_CHARPOS (after an image or a
8672 display string or stretch glyph). */
8673 if ((op & MOVE_TO_POS) != 0
8674 && BUFFERP (it->object)
8675 && it->method == GET_FROM_BUFFER
8676 && (((!it->bidi_p
8677 /* When the iterator is at base embedding level, we
8678 are guaranteed that characters are delivered for
8679 display in strictly increasing order of their
8680 buffer positions. */
8681 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8682 && IT_CHARPOS (*it) > to_charpos)
8683 || (it->bidi_p
8684 && (prev_method == GET_FROM_IMAGE
8685 || prev_method == GET_FROM_STRETCH
8686 || prev_method == GET_FROM_STRING)
8687 /* Passed TO_CHARPOS from left to right. */
8688 && ((prev_pos < to_charpos
8689 && IT_CHARPOS (*it) > to_charpos)
8690 /* Passed TO_CHARPOS from right to left. */
8691 || (prev_pos > to_charpos
8692 && IT_CHARPOS (*it) < to_charpos)))))
8694 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8696 result = MOVE_POS_MATCH_OR_ZV;
8697 break;
8699 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8700 /* If wrap_it is valid, the current position might be in a
8701 word that is wrapped. So, save the iterator in
8702 atpos_it and continue to see if wrapping happens. */
8703 SAVE_IT (atpos_it, *it, atpos_data);
8706 /* Stop when ZV reached.
8707 We used to stop here when TO_CHARPOS reached as well, but that is
8708 too soon if this glyph does not fit on this line. So we handle it
8709 explicitly below. */
8710 if (!get_next_display_element (it))
8712 result = MOVE_POS_MATCH_OR_ZV;
8713 break;
8716 if (it->line_wrap == TRUNCATE)
8718 if (BUFFER_POS_REACHED_P ())
8720 result = MOVE_POS_MATCH_OR_ZV;
8721 break;
8724 else
8726 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8728 if (IT_DISPLAYING_WHITESPACE (it))
8729 may_wrap = true;
8730 else if (may_wrap)
8732 /* We have reached a glyph that follows one or more
8733 whitespace characters. If the position is
8734 already found, we are done. */
8735 if (atpos_it.sp >= 0)
8737 RESTORE_IT (it, &atpos_it, atpos_data);
8738 result = MOVE_POS_MATCH_OR_ZV;
8739 goto done;
8741 if (atx_it.sp >= 0)
8743 RESTORE_IT (it, &atx_it, atx_data);
8744 result = MOVE_X_REACHED;
8745 goto done;
8747 /* Otherwise, we can wrap here. */
8748 SAVE_IT (wrap_it, *it, wrap_data);
8749 may_wrap = false;
8754 /* Remember the line height for the current line, in case
8755 the next element doesn't fit on the line. */
8756 ascent = it->max_ascent;
8757 descent = it->max_descent;
8759 /* The call to produce_glyphs will get the metrics of the
8760 display element IT is loaded with. Record the x-position
8761 before this display element, in case it doesn't fit on the
8762 line. */
8763 x = it->current_x;
8765 PRODUCE_GLYPHS (it);
8767 if (it->area != TEXT_AREA)
8769 prev_method = it->method;
8770 if (it->method == GET_FROM_BUFFER)
8771 prev_pos = IT_CHARPOS (*it);
8772 set_iterator_to_next (it, true);
8773 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8774 SET_TEXT_POS (this_line_min_pos,
8775 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8776 if (it->bidi_p
8777 && (op & MOVE_TO_POS)
8778 && IT_CHARPOS (*it) > to_charpos
8779 && IT_CHARPOS (*it) < closest_pos)
8780 closest_pos = IT_CHARPOS (*it);
8781 continue;
8784 /* The number of glyphs we get back in IT->nglyphs will normally
8785 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8786 character on a terminal frame, or (iii) a line end. For the
8787 second case, IT->nglyphs - 1 padding glyphs will be present.
8788 (On X frames, there is only one glyph produced for a
8789 composite character.)
8791 The behavior implemented below means, for continuation lines,
8792 that as many spaces of a TAB as fit on the current line are
8793 displayed there. For terminal frames, as many glyphs of a
8794 multi-glyph character are displayed in the current line, too.
8795 This is what the old redisplay code did, and we keep it that
8796 way. Under X, the whole shape of a complex character must
8797 fit on the line or it will be completely displayed in the
8798 next line.
8800 Note that both for tabs and padding glyphs, all glyphs have
8801 the same width. */
8802 if (it->nglyphs)
8804 /* More than one glyph or glyph doesn't fit on line. All
8805 glyphs have the same width. */
8806 int single_glyph_width = it->pixel_width / it->nglyphs;
8807 int new_x;
8808 int x_before_this_char = x;
8809 int hpos_before_this_char = it->hpos;
8811 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8813 new_x = x + single_glyph_width;
8815 /* We want to leave anything reaching TO_X to the caller. */
8816 if ((op & MOVE_TO_X) && new_x > to_x)
8818 if (BUFFER_POS_REACHED_P ())
8820 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8821 goto buffer_pos_reached;
8822 if (atpos_it.sp < 0)
8824 SAVE_IT (atpos_it, *it, atpos_data);
8825 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8828 else
8830 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8832 it->current_x = x;
8833 result = MOVE_X_REACHED;
8834 break;
8836 if (atx_it.sp < 0)
8838 SAVE_IT (atx_it, *it, atx_data);
8839 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8844 if (/* Lines are continued. */
8845 it->line_wrap != TRUNCATE
8846 && (/* And glyph doesn't fit on the line. */
8847 new_x > it->last_visible_x
8848 /* Or it fits exactly and we're on a window
8849 system frame. */
8850 || (new_x == it->last_visible_x
8851 && FRAME_WINDOW_P (it->f)
8852 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8853 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8854 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8856 bool moved_forward = false;
8858 if (/* IT->hpos == 0 means the very first glyph
8859 doesn't fit on the line, e.g. a wide image. */
8860 it->hpos == 0
8861 || (new_x == it->last_visible_x
8862 && FRAME_WINDOW_P (it->f)))
8864 ++it->hpos;
8865 it->current_x = new_x;
8867 /* The character's last glyph just barely fits
8868 in this row. */
8869 if (i == it->nglyphs - 1)
8871 /* If this is the destination position,
8872 return a position *before* it in this row,
8873 now that we know it fits in this row. */
8874 if (BUFFER_POS_REACHED_P ())
8876 bool can_wrap = true;
8878 /* If we are at a whitespace character
8879 that barely fits on this screen line,
8880 but the next character is also
8881 whitespace, we cannot wrap here. */
8882 if (it->line_wrap == WORD_WRAP
8883 && wrap_it.sp >= 0
8884 && may_wrap
8885 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8887 struct it tem_it;
8888 void *tem_data = NULL;
8890 SAVE_IT (tem_it, *it, tem_data);
8891 set_iterator_to_next (it, true);
8892 if (get_next_display_element (it)
8893 && IT_DISPLAYING_WHITESPACE (it))
8894 can_wrap = false;
8895 RESTORE_IT (it, &tem_it, tem_data);
8897 if (it->line_wrap != WORD_WRAP
8898 || wrap_it.sp < 0
8899 /* If we've just found whitespace
8900 where we can wrap, effectively
8901 ignore the previous wrap point --
8902 it is no longer relevant, but we
8903 won't have an opportunity to
8904 update it, since we've reached
8905 the edge of this screen line. */
8906 || (may_wrap && can_wrap
8907 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8909 it->hpos = hpos_before_this_char;
8910 it->current_x = x_before_this_char;
8911 result = MOVE_POS_MATCH_OR_ZV;
8912 break;
8914 if (it->line_wrap == WORD_WRAP
8915 && atpos_it.sp < 0)
8917 SAVE_IT (atpos_it, *it, atpos_data);
8918 atpos_it.current_x = x_before_this_char;
8919 atpos_it.hpos = hpos_before_this_char;
8923 prev_method = it->method;
8924 if (it->method == GET_FROM_BUFFER)
8925 prev_pos = IT_CHARPOS (*it);
8926 set_iterator_to_next (it, true);
8927 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8928 SET_TEXT_POS (this_line_min_pos,
8929 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8930 /* On graphical terminals, newlines may
8931 "overflow" into the fringe if
8932 overflow-newline-into-fringe is non-nil.
8933 On text terminals, and on graphical
8934 terminals with no right margin, newlines
8935 may overflow into the last glyph on the
8936 display line.*/
8937 if (!FRAME_WINDOW_P (it->f)
8938 || ((it->bidi_p
8939 && it->bidi_it.paragraph_dir == R2L)
8940 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8941 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8942 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8944 if (!get_next_display_element (it))
8946 result = MOVE_POS_MATCH_OR_ZV;
8947 break;
8949 moved_forward = true;
8950 if (BUFFER_POS_REACHED_P ())
8952 if (ITERATOR_AT_END_OF_LINE_P (it))
8953 result = MOVE_POS_MATCH_OR_ZV;
8954 else
8955 result = MOVE_LINE_CONTINUED;
8956 break;
8958 if (ITERATOR_AT_END_OF_LINE_P (it)
8959 && (it->line_wrap != WORD_WRAP
8960 || wrap_it.sp < 0
8961 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8963 result = MOVE_NEWLINE_OR_CR;
8964 break;
8969 else
8970 IT_RESET_X_ASCENT_DESCENT (it);
8972 /* If the screen line ends with whitespace, and we
8973 are under word-wrap, don't use wrap_it: it is no
8974 longer relevant, but we won't have an opportunity
8975 to update it, since we are done with this screen
8976 line. */
8977 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8978 /* If the character after the one which set the
8979 may_wrap flag is also whitespace, we can't
8980 wrap here, since the screen line cannot be
8981 wrapped in the middle of whitespace.
8982 Therefore, wrap_it _is_ relevant in that
8983 case. */
8984 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8986 /* If we've found TO_X, go back there, as we now
8987 know the last word fits on this screen line. */
8988 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8989 && atx_it.sp >= 0)
8991 RESTORE_IT (it, &atx_it, atx_data);
8992 atpos_it.sp = -1;
8993 atx_it.sp = -1;
8994 result = MOVE_X_REACHED;
8995 break;
8998 else if (wrap_it.sp >= 0)
9000 RESTORE_IT (it, &wrap_it, wrap_data);
9001 atpos_it.sp = -1;
9002 atx_it.sp = -1;
9005 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9006 IT_CHARPOS (*it)));
9007 result = MOVE_LINE_CONTINUED;
9008 break;
9011 if (BUFFER_POS_REACHED_P ())
9013 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9014 goto buffer_pos_reached;
9015 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9017 SAVE_IT (atpos_it, *it, atpos_data);
9018 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9022 if (new_x > it->first_visible_x)
9024 /* Glyph is visible. Increment number of glyphs that
9025 would be displayed. */
9026 ++it->hpos;
9030 if (result != MOVE_UNDEFINED)
9031 break;
9033 else if (BUFFER_POS_REACHED_P ())
9035 buffer_pos_reached:
9036 IT_RESET_X_ASCENT_DESCENT (it);
9037 result = MOVE_POS_MATCH_OR_ZV;
9038 break;
9040 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9042 /* Stop when TO_X specified and reached. This check is
9043 necessary here because of lines consisting of a line end,
9044 only. The line end will not produce any glyphs and we
9045 would never get MOVE_X_REACHED. */
9046 eassert (it->nglyphs == 0);
9047 result = MOVE_X_REACHED;
9048 break;
9051 /* Is this a line end? If yes, we're done. */
9052 if (ITERATOR_AT_END_OF_LINE_P (it))
9054 /* If we are past TO_CHARPOS, but never saw any character
9055 positions smaller than TO_CHARPOS, return
9056 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9057 did. */
9058 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9060 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9062 if (closest_pos < ZV)
9064 RESTORE_IT (it, &ppos_it, ppos_data);
9065 /* Don't recurse if closest_pos is equal to
9066 to_charpos, since we have just tried that. */
9067 if (closest_pos != to_charpos)
9068 move_it_in_display_line_to (it, closest_pos, -1,
9069 MOVE_TO_POS);
9070 result = MOVE_POS_MATCH_OR_ZV;
9072 else
9073 goto buffer_pos_reached;
9075 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9076 && IT_CHARPOS (*it) > to_charpos)
9077 goto buffer_pos_reached;
9078 else
9079 result = MOVE_NEWLINE_OR_CR;
9081 else
9082 result = MOVE_NEWLINE_OR_CR;
9083 /* If we've processed the newline, make sure this flag is
9084 reset, as it must only be set when the newline itself is
9085 processed. */
9086 if (result == MOVE_NEWLINE_OR_CR)
9087 it->constrain_row_ascent_descent_p = false;
9088 break;
9091 prev_method = it->method;
9092 if (it->method == GET_FROM_BUFFER)
9093 prev_pos = IT_CHARPOS (*it);
9094 /* The current display element has been consumed. Advance
9095 to the next. */
9096 set_iterator_to_next (it, true);
9097 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9098 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9099 if (IT_CHARPOS (*it) < to_charpos)
9100 saw_smaller_pos = true;
9101 if (it->bidi_p
9102 && (op & MOVE_TO_POS)
9103 && IT_CHARPOS (*it) >= to_charpos
9104 && IT_CHARPOS (*it) < closest_pos)
9105 closest_pos = IT_CHARPOS (*it);
9107 /* Stop if lines are truncated and IT's current x-position is
9108 past the right edge of the window now. */
9109 if (it->line_wrap == TRUNCATE
9110 && it->current_x >= it->last_visible_x)
9112 if (!FRAME_WINDOW_P (it->f)
9113 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9114 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9115 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9116 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9118 bool at_eob_p = false;
9120 if ((at_eob_p = !get_next_display_element (it))
9121 || BUFFER_POS_REACHED_P ()
9122 /* If we are past TO_CHARPOS, but never saw any
9123 character positions smaller than TO_CHARPOS,
9124 return MOVE_POS_MATCH_OR_ZV, like the
9125 unidirectional display did. */
9126 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9127 && !saw_smaller_pos
9128 && IT_CHARPOS (*it) > to_charpos))
9130 if (it->bidi_p
9131 && !BUFFER_POS_REACHED_P ()
9132 && !at_eob_p && closest_pos < ZV)
9134 RESTORE_IT (it, &ppos_it, ppos_data);
9135 if (closest_pos != to_charpos)
9136 move_it_in_display_line_to (it, closest_pos, -1,
9137 MOVE_TO_POS);
9139 result = MOVE_POS_MATCH_OR_ZV;
9140 break;
9142 if (ITERATOR_AT_END_OF_LINE_P (it))
9144 result = MOVE_NEWLINE_OR_CR;
9145 break;
9148 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9149 && !saw_smaller_pos
9150 && IT_CHARPOS (*it) > to_charpos)
9152 if (closest_pos < ZV)
9154 RESTORE_IT (it, &ppos_it, ppos_data);
9155 if (closest_pos != to_charpos)
9156 move_it_in_display_line_to (it, closest_pos, -1,
9157 MOVE_TO_POS);
9159 result = MOVE_POS_MATCH_OR_ZV;
9160 break;
9162 result = MOVE_LINE_TRUNCATED;
9163 break;
9165 #undef IT_RESET_X_ASCENT_DESCENT
9168 #undef BUFFER_POS_REACHED_P
9170 /* If we scanned beyond TO_POS, restore the saved iterator either to
9171 the wrap point (if found), or to atpos/atx location. We decide which
9172 data to use to restore the saved iterator state by their X coordinates,
9173 since buffer positions might increase non-monotonically with screen
9174 coordinates due to bidi reordering. */
9175 if (result == MOVE_LINE_CONTINUED
9176 && it->line_wrap == WORD_WRAP
9177 && wrap_it.sp >= 0
9178 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9179 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9180 RESTORE_IT (it, &wrap_it, wrap_data);
9181 else if (atpos_it.sp >= 0)
9182 RESTORE_IT (it, &atpos_it, atpos_data);
9183 else if (atx_it.sp >= 0)
9184 RESTORE_IT (it, &atx_it, atx_data);
9186 done:
9188 if (atpos_data)
9189 bidi_unshelve_cache (atpos_data, true);
9190 if (atx_data)
9191 bidi_unshelve_cache (atx_data, true);
9192 if (wrap_data)
9193 bidi_unshelve_cache (wrap_data, true);
9194 if (ppos_data)
9195 bidi_unshelve_cache (ppos_data, true);
9197 /* Restore the iterator settings altered at the beginning of this
9198 function. */
9199 it->glyph_row = saved_glyph_row;
9200 return result;
9203 /* For external use. */
9204 void
9205 move_it_in_display_line (struct it *it,
9206 ptrdiff_t to_charpos, int to_x,
9207 enum move_operation_enum op)
9209 if (it->line_wrap == WORD_WRAP
9210 && (op & MOVE_TO_X))
9212 struct it save_it;
9213 void *save_data = NULL;
9214 int skip;
9216 SAVE_IT (save_it, *it, save_data);
9217 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9218 /* When word-wrap is on, TO_X may lie past the end
9219 of a wrapped line. Then it->current is the
9220 character on the next line, so backtrack to the
9221 space before the wrap point. */
9222 if (skip == MOVE_LINE_CONTINUED)
9224 int prev_x = max (it->current_x - 1, 0);
9225 RESTORE_IT (it, &save_it, save_data);
9226 move_it_in_display_line_to
9227 (it, -1, prev_x, MOVE_TO_X);
9229 else
9230 bidi_unshelve_cache (save_data, true);
9232 else
9233 move_it_in_display_line_to (it, to_charpos, to_x, op);
9237 /* Move IT forward until it satisfies one or more of the criteria in
9238 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9240 OP is a bit-mask that specifies where to stop, and in particular,
9241 which of those four position arguments makes a difference. See the
9242 description of enum move_operation_enum.
9244 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9245 screen line, this function will set IT to the next position that is
9246 displayed to the right of TO_CHARPOS on the screen.
9248 Return the maximum pixel length of any line scanned but never more
9249 than it.last_visible_x. */
9252 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9254 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9255 int line_height, line_start_x = 0, reached = 0;
9256 int max_current_x = 0;
9257 void *backup_data = NULL;
9259 for (;;)
9261 if (op & MOVE_TO_VPOS)
9263 /* If no TO_CHARPOS and no TO_X specified, stop at the
9264 start of the line TO_VPOS. */
9265 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9267 if (it->vpos == to_vpos)
9269 reached = 1;
9270 break;
9272 else
9273 skip = move_it_in_display_line_to (it, -1, -1, 0);
9275 else
9277 /* TO_VPOS >= 0 means stop at TO_X in the line at
9278 TO_VPOS, or at TO_POS, whichever comes first. */
9279 if (it->vpos == to_vpos)
9281 reached = 2;
9282 break;
9285 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9287 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9289 reached = 3;
9290 break;
9292 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9294 /* We have reached TO_X but not in the line we want. */
9295 skip = move_it_in_display_line_to (it, to_charpos,
9296 -1, MOVE_TO_POS);
9297 if (skip == MOVE_POS_MATCH_OR_ZV)
9299 reached = 4;
9300 break;
9305 else if (op & MOVE_TO_Y)
9307 struct it it_backup;
9309 if (it->line_wrap == WORD_WRAP)
9310 SAVE_IT (it_backup, *it, backup_data);
9312 /* TO_Y specified means stop at TO_X in the line containing
9313 TO_Y---or at TO_CHARPOS if this is reached first. The
9314 problem is that we can't really tell whether the line
9315 contains TO_Y before we have completely scanned it, and
9316 this may skip past TO_X. What we do is to first scan to
9317 TO_X.
9319 If TO_X is not specified, use a TO_X of zero. The reason
9320 is to make the outcome of this function more predictable.
9321 If we didn't use TO_X == 0, we would stop at the end of
9322 the line which is probably not what a caller would expect
9323 to happen. */
9324 skip = move_it_in_display_line_to
9325 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9326 (MOVE_TO_X | (op & MOVE_TO_POS)));
9328 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9329 if (skip == MOVE_POS_MATCH_OR_ZV)
9330 reached = 5;
9331 else if (skip == MOVE_X_REACHED)
9333 /* If TO_X was reached, we want to know whether TO_Y is
9334 in the line. We know this is the case if the already
9335 scanned glyphs make the line tall enough. Otherwise,
9336 we must check by scanning the rest of the line. */
9337 line_height = it->max_ascent + it->max_descent;
9338 if (to_y >= it->current_y
9339 && to_y < it->current_y + line_height)
9341 reached = 6;
9342 break;
9344 SAVE_IT (it_backup, *it, backup_data);
9345 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9346 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9347 op & MOVE_TO_POS);
9348 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9349 line_height = it->max_ascent + it->max_descent;
9350 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9352 if (to_y >= it->current_y
9353 && to_y < it->current_y + line_height)
9355 /* If TO_Y is in this line and TO_X was reached
9356 above, we scanned too far. We have to restore
9357 IT's settings to the ones before skipping. But
9358 keep the more accurate values of max_ascent and
9359 max_descent we've found while skipping the rest
9360 of the line, for the sake of callers, such as
9361 pos_visible_p, that need to know the line
9362 height. */
9363 int max_ascent = it->max_ascent;
9364 int max_descent = it->max_descent;
9366 RESTORE_IT (it, &it_backup, backup_data);
9367 it->max_ascent = max_ascent;
9368 it->max_descent = max_descent;
9369 reached = 6;
9371 else
9373 skip = skip2;
9374 if (skip == MOVE_POS_MATCH_OR_ZV)
9375 reached = 7;
9378 else
9380 /* Check whether TO_Y is in this line. */
9381 line_height = it->max_ascent + it->max_descent;
9382 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9384 if (to_y >= it->current_y
9385 && to_y < it->current_y + line_height)
9387 if (to_y > it->current_y)
9388 max_current_x = max (it->current_x, max_current_x);
9390 /* When word-wrap is on, TO_X may lie past the end
9391 of a wrapped line. Then it->current is the
9392 character on the next line, so backtrack to the
9393 space before the wrap point. */
9394 if (skip == MOVE_LINE_CONTINUED
9395 && it->line_wrap == WORD_WRAP)
9397 int prev_x = max (it->current_x - 1, 0);
9398 RESTORE_IT (it, &it_backup, backup_data);
9399 skip = move_it_in_display_line_to
9400 (it, -1, prev_x, MOVE_TO_X);
9403 reached = 6;
9407 if (reached)
9409 max_current_x = max (it->current_x, max_current_x);
9410 break;
9413 else if (BUFFERP (it->object)
9414 && (it->method == GET_FROM_BUFFER
9415 || it->method == GET_FROM_STRETCH)
9416 && IT_CHARPOS (*it) >= to_charpos
9417 /* Under bidi iteration, a call to set_iterator_to_next
9418 can scan far beyond to_charpos if the initial
9419 portion of the next line needs to be reordered. In
9420 that case, give move_it_in_display_line_to another
9421 chance below. */
9422 && !(it->bidi_p
9423 && it->bidi_it.scan_dir == -1))
9424 skip = MOVE_POS_MATCH_OR_ZV;
9425 else
9426 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9428 switch (skip)
9430 case MOVE_POS_MATCH_OR_ZV:
9431 max_current_x = max (it->current_x, max_current_x);
9432 reached = 8;
9433 goto out;
9435 case MOVE_NEWLINE_OR_CR:
9436 max_current_x = max (it->current_x, max_current_x);
9437 set_iterator_to_next (it, true);
9438 it->continuation_lines_width = 0;
9439 break;
9441 case MOVE_LINE_TRUNCATED:
9442 max_current_x = it->last_visible_x;
9443 it->continuation_lines_width = 0;
9444 reseat_at_next_visible_line_start (it, false);
9445 if ((op & MOVE_TO_POS) != 0
9446 && IT_CHARPOS (*it) > to_charpos)
9448 reached = 9;
9449 goto out;
9451 break;
9453 case MOVE_LINE_CONTINUED:
9454 max_current_x = it->last_visible_x;
9455 /* For continued lines ending in a tab, some of the glyphs
9456 associated with the tab are displayed on the current
9457 line. Since it->current_x does not include these glyphs,
9458 we use it->last_visible_x instead. */
9459 if (it->c == '\t')
9461 it->continuation_lines_width += it->last_visible_x;
9462 /* When moving by vpos, ensure that the iterator really
9463 advances to the next line (bug#847, bug#969). Fixme:
9464 do we need to do this in other circumstances? */
9465 if (it->current_x != it->last_visible_x
9466 && (op & MOVE_TO_VPOS)
9467 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9469 line_start_x = it->current_x + it->pixel_width
9470 - it->last_visible_x;
9471 if (FRAME_WINDOW_P (it->f))
9473 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9474 struct font *face_font = face->font;
9476 /* When display_line produces a continued line
9477 that ends in a TAB, it skips a tab stop that
9478 is closer than the font's space character
9479 width (see x_produce_glyphs where it produces
9480 the stretch glyph which represents a TAB).
9481 We need to reproduce the same logic here. */
9482 eassert (face_font);
9483 if (face_font)
9485 if (line_start_x < face_font->space_width)
9486 line_start_x
9487 += it->tab_width * face_font->space_width;
9490 set_iterator_to_next (it, false);
9493 else
9494 it->continuation_lines_width += it->current_x;
9495 break;
9497 default:
9498 emacs_abort ();
9501 /* Reset/increment for the next run. */
9502 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9503 it->current_x = line_start_x;
9504 line_start_x = 0;
9505 it->hpos = 0;
9506 it->current_y += it->max_ascent + it->max_descent;
9507 ++it->vpos;
9508 last_height = it->max_ascent + it->max_descent;
9509 it->max_ascent = it->max_descent = 0;
9512 out:
9514 /* On text terminals, we may stop at the end of a line in the middle
9515 of a multi-character glyph. If the glyph itself is continued,
9516 i.e. it is actually displayed on the next line, don't treat this
9517 stopping point as valid; move to the next line instead (unless
9518 that brings us offscreen). */
9519 if (!FRAME_WINDOW_P (it->f)
9520 && op & MOVE_TO_POS
9521 && IT_CHARPOS (*it) == to_charpos
9522 && it->what == IT_CHARACTER
9523 && it->nglyphs > 1
9524 && it->line_wrap == WINDOW_WRAP
9525 && it->current_x == it->last_visible_x - 1
9526 && it->c != '\n'
9527 && it->c != '\t'
9528 && it->w->window_end_valid
9529 && it->vpos < it->w->window_end_vpos)
9531 it->continuation_lines_width += it->current_x;
9532 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9533 it->current_y += it->max_ascent + it->max_descent;
9534 ++it->vpos;
9535 last_height = it->max_ascent + it->max_descent;
9538 if (backup_data)
9539 bidi_unshelve_cache (backup_data, true);
9541 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9543 return max_current_x;
9547 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9549 If DY > 0, move IT backward at least that many pixels. DY = 0
9550 means move IT backward to the preceding line start or BEGV. This
9551 function may move over more than DY pixels if IT->current_y - DY
9552 ends up in the middle of a line; in this case IT->current_y will be
9553 set to the top of the line moved to. */
9555 void
9556 move_it_vertically_backward (struct it *it, int dy)
9558 int nlines, h;
9559 struct it it2, it3;
9560 void *it2data = NULL, *it3data = NULL;
9561 ptrdiff_t start_pos;
9562 int nchars_per_row
9563 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9564 ptrdiff_t pos_limit;
9566 move_further_back:
9567 eassert (dy >= 0);
9569 start_pos = IT_CHARPOS (*it);
9571 /* Estimate how many newlines we must move back. */
9572 nlines = max (1, dy / default_line_pixel_height (it->w));
9573 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9574 pos_limit = BEGV;
9575 else
9576 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9578 /* Set the iterator's position that many lines back. But don't go
9579 back more than NLINES full screen lines -- this wins a day with
9580 buffers which have very long lines. */
9581 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9582 back_to_previous_visible_line_start (it);
9584 /* Reseat the iterator here. When moving backward, we don't want
9585 reseat to skip forward over invisible text, set up the iterator
9586 to deliver from overlay strings at the new position etc. So,
9587 use reseat_1 here. */
9588 reseat_1 (it, it->current.pos, true);
9590 /* We are now surely at a line start. */
9591 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9592 reordering is in effect. */
9593 it->continuation_lines_width = 0;
9595 /* Move forward and see what y-distance we moved. First move to the
9596 start of the next line so that we get its height. We need this
9597 height to be able to tell whether we reached the specified
9598 y-distance. */
9599 SAVE_IT (it2, *it, it2data);
9600 it2.max_ascent = it2.max_descent = 0;
9603 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9604 MOVE_TO_POS | MOVE_TO_VPOS);
9606 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9607 /* If we are in a display string which starts at START_POS,
9608 and that display string includes a newline, and we are
9609 right after that newline (i.e. at the beginning of a
9610 display line), exit the loop, because otherwise we will
9611 infloop, since move_it_to will see that it is already at
9612 START_POS and will not move. */
9613 || (it2.method == GET_FROM_STRING
9614 && IT_CHARPOS (it2) == start_pos
9615 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9616 eassert (IT_CHARPOS (*it) >= BEGV);
9617 SAVE_IT (it3, it2, it3data);
9619 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9620 eassert (IT_CHARPOS (*it) >= BEGV);
9621 /* H is the actual vertical distance from the position in *IT
9622 and the starting position. */
9623 h = it2.current_y - it->current_y;
9624 /* NLINES is the distance in number of lines. */
9625 nlines = it2.vpos - it->vpos;
9627 /* Correct IT's y and vpos position
9628 so that they are relative to the starting point. */
9629 it->vpos -= nlines;
9630 it->current_y -= h;
9632 if (dy == 0)
9634 /* DY == 0 means move to the start of the screen line. The
9635 value of nlines is > 0 if continuation lines were involved,
9636 or if the original IT position was at start of a line. */
9637 RESTORE_IT (it, it, it2data);
9638 if (nlines > 0)
9639 move_it_by_lines (it, nlines);
9640 /* The above code moves us to some position NLINES down,
9641 usually to its first glyph (leftmost in an L2R line), but
9642 that's not necessarily the start of the line, under bidi
9643 reordering. We want to get to the character position
9644 that is immediately after the newline of the previous
9645 line. */
9646 if (it->bidi_p
9647 && !it->continuation_lines_width
9648 && !STRINGP (it->string)
9649 && IT_CHARPOS (*it) > BEGV
9650 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9652 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9654 DEC_BOTH (cp, bp);
9655 cp = find_newline_no_quit (cp, bp, -1, NULL);
9656 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9658 bidi_unshelve_cache (it3data, true);
9660 else
9662 /* The y-position we try to reach, relative to *IT.
9663 Note that H has been subtracted in front of the if-statement. */
9664 int target_y = it->current_y + h - dy;
9665 int y0 = it3.current_y;
9666 int y1;
9667 int line_height;
9669 RESTORE_IT (&it3, &it3, it3data);
9670 y1 = line_bottom_y (&it3);
9671 line_height = y1 - y0;
9672 RESTORE_IT (it, it, it2data);
9673 /* If we did not reach target_y, try to move further backward if
9674 we can. If we moved too far backward, try to move forward. */
9675 if (target_y < it->current_y
9676 /* This is heuristic. In a window that's 3 lines high, with
9677 a line height of 13 pixels each, recentering with point
9678 on the bottom line will try to move -39/2 = 19 pixels
9679 backward. Try to avoid moving into the first line. */
9680 && (it->current_y - target_y
9681 > min (window_box_height (it->w), line_height * 2 / 3))
9682 && IT_CHARPOS (*it) > BEGV)
9684 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9685 target_y - it->current_y));
9686 dy = it->current_y - target_y;
9687 goto move_further_back;
9689 else if (target_y >= it->current_y + line_height
9690 && IT_CHARPOS (*it) < ZV)
9692 /* Should move forward by at least one line, maybe more.
9694 Note: Calling move_it_by_lines can be expensive on
9695 terminal frames, where compute_motion is used (via
9696 vmotion) to do the job, when there are very long lines
9697 and truncate-lines is nil. That's the reason for
9698 treating terminal frames specially here. */
9700 if (!FRAME_WINDOW_P (it->f))
9701 move_it_vertically (it, target_y - it->current_y);
9702 else
9706 move_it_by_lines (it, 1);
9708 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9715 /* Move IT by a specified amount of pixel lines DY. DY negative means
9716 move backwards. DY = 0 means move to start of screen line. At the
9717 end, IT will be on the start of a screen line. */
9719 void
9720 move_it_vertically (struct it *it, int dy)
9722 if (dy <= 0)
9723 move_it_vertically_backward (it, -dy);
9724 else
9726 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9727 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9728 MOVE_TO_POS | MOVE_TO_Y);
9729 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9731 /* If buffer ends in ZV without a newline, move to the start of
9732 the line to satisfy the post-condition. */
9733 if (IT_CHARPOS (*it) == ZV
9734 && ZV > BEGV
9735 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9736 move_it_by_lines (it, 0);
9741 /* Move iterator IT past the end of the text line it is in. */
9743 void
9744 move_it_past_eol (struct it *it)
9746 enum move_it_result rc;
9748 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9749 if (rc == MOVE_NEWLINE_OR_CR)
9750 set_iterator_to_next (it, false);
9754 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9755 negative means move up. DVPOS == 0 means move to the start of the
9756 screen line.
9758 Optimization idea: If we would know that IT->f doesn't use
9759 a face with proportional font, we could be faster for
9760 truncate-lines nil. */
9762 void
9763 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9766 /* The commented-out optimization uses vmotion on terminals. This
9767 gives bad results, because elements like it->what, on which
9768 callers such as pos_visible_p rely, aren't updated. */
9769 /* struct position pos;
9770 if (!FRAME_WINDOW_P (it->f))
9772 struct text_pos textpos;
9774 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9775 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9776 reseat (it, textpos, true);
9777 it->vpos += pos.vpos;
9778 it->current_y += pos.vpos;
9780 else */
9782 if (dvpos == 0)
9784 /* DVPOS == 0 means move to the start of the screen line. */
9785 move_it_vertically_backward (it, 0);
9786 /* Let next call to line_bottom_y calculate real line height. */
9787 last_height = 0;
9789 else if (dvpos > 0)
9791 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9792 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9794 /* Only move to the next buffer position if we ended up in a
9795 string from display property, not in an overlay string
9796 (before-string or after-string). That is because the
9797 latter don't conceal the underlying buffer position, so
9798 we can ask to move the iterator to the exact position we
9799 are interested in. Note that, even if we are already at
9800 IT_CHARPOS (*it), the call below is not a no-op, as it
9801 will detect that we are at the end of the string, pop the
9802 iterator, and compute it->current_x and it->hpos
9803 correctly. */
9804 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9805 -1, -1, -1, MOVE_TO_POS);
9808 else
9810 struct it it2;
9811 void *it2data = NULL;
9812 ptrdiff_t start_charpos, i;
9813 int nchars_per_row
9814 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9815 bool hit_pos_limit = false;
9816 ptrdiff_t pos_limit;
9818 /* Start at the beginning of the screen line containing IT's
9819 position. This may actually move vertically backwards,
9820 in case of overlays, so adjust dvpos accordingly. */
9821 dvpos += it->vpos;
9822 move_it_vertically_backward (it, 0);
9823 dvpos -= it->vpos;
9825 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9826 screen lines, and reseat the iterator there. */
9827 start_charpos = IT_CHARPOS (*it);
9828 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9829 pos_limit = BEGV;
9830 else
9831 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9833 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9834 back_to_previous_visible_line_start (it);
9835 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9836 hit_pos_limit = true;
9837 reseat (it, it->current.pos, true);
9839 /* Move further back if we end up in a string or an image. */
9840 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9842 /* First try to move to start of display line. */
9843 dvpos += it->vpos;
9844 move_it_vertically_backward (it, 0);
9845 dvpos -= it->vpos;
9846 if (IT_POS_VALID_AFTER_MOVE_P (it))
9847 break;
9848 /* If start of line is still in string or image,
9849 move further back. */
9850 back_to_previous_visible_line_start (it);
9851 reseat (it, it->current.pos, true);
9852 dvpos--;
9855 it->current_x = it->hpos = 0;
9857 /* Above call may have moved too far if continuation lines
9858 are involved. Scan forward and see if it did. */
9859 SAVE_IT (it2, *it, it2data);
9860 it2.vpos = it2.current_y = 0;
9861 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9862 it->vpos -= it2.vpos;
9863 it->current_y -= it2.current_y;
9864 it->current_x = it->hpos = 0;
9866 /* If we moved too far back, move IT some lines forward. */
9867 if (it2.vpos > -dvpos)
9869 int delta = it2.vpos + dvpos;
9871 RESTORE_IT (&it2, &it2, it2data);
9872 SAVE_IT (it2, *it, it2data);
9873 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9874 /* Move back again if we got too far ahead. */
9875 if (IT_CHARPOS (*it) >= start_charpos)
9876 RESTORE_IT (it, &it2, it2data);
9877 else
9878 bidi_unshelve_cache (it2data, true);
9880 else if (hit_pos_limit && pos_limit > BEGV
9881 && dvpos < 0 && it2.vpos < -dvpos)
9883 /* If we hit the limit, but still didn't make it far enough
9884 back, that means there's a display string with a newline
9885 covering a large chunk of text, and that caused
9886 back_to_previous_visible_line_start try to go too far.
9887 Punish those who commit such atrocities by going back
9888 until we've reached DVPOS, after lifting the limit, which
9889 could make it slow for very long lines. "If it hurts,
9890 don't do that!" */
9891 dvpos += it2.vpos;
9892 RESTORE_IT (it, it, it2data);
9893 for (i = -dvpos; i > 0; --i)
9895 back_to_previous_visible_line_start (it);
9896 it->vpos--;
9898 reseat_1 (it, it->current.pos, true);
9900 else
9901 RESTORE_IT (it, it, it2data);
9906 partial_line_height (struct it *it_origin)
9908 int partial_height;
9909 void *it_data = NULL;
9910 struct it it;
9911 SAVE_IT (it, *it_origin, it_data);
9912 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9913 MOVE_TO_POS | MOVE_TO_Y);
9914 if (it.what == IT_EOB)
9916 int vis_height = it.last_visible_y - it.current_y;
9917 int height = it.ascent + it.descent;
9918 partial_height = (vis_height < height) ? vis_height : 0;
9920 else
9922 int last_line_y = it.current_y;
9923 move_it_by_lines (&it, 1);
9924 partial_height = (it.current_y > it.last_visible_y)
9925 ? it.last_visible_y - last_line_y : 0;
9927 RESTORE_IT (&it, &it, it_data);
9928 return partial_height;
9931 /* Return true if IT points into the middle of a display vector. */
9933 bool
9934 in_display_vector_p (struct it *it)
9936 return (it->method == GET_FROM_DISPLAY_VECTOR
9937 && it->current.dpvec_index > 0
9938 && it->dpvec + it->current.dpvec_index != it->dpend);
9941 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9942 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9943 WINDOW must be a live window and defaults to the selected one. The
9944 return value is a cons of the maximum pixel-width of any text line and
9945 the maximum pixel-height of all text lines.
9947 The optional argument FROM, if non-nil, specifies the first text
9948 position and defaults to the minimum accessible position of the buffer.
9949 If FROM is t, use the minimum accessible position that starts a
9950 non-empty line. TO, if non-nil, specifies the last text position and
9951 defaults to the maximum accessible position of the buffer. If TO is t,
9952 use the maximum accessible position that ends a non-empty line.
9954 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9955 width that can be returned. X-LIMIT nil or omitted, means to use the
9956 pixel-width of WINDOW's body; use this if you want to know how high
9957 WINDOW should be become in order to fit all of its buffer's text with
9958 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9959 if you intend to change WINDOW's width. In any case, text whose
9960 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9961 of long lines can take some time, it's always a good idea to make this
9962 argument as small as possible; in particular, if the buffer contains
9963 long lines that shall be truncated anyway.
9965 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9966 height (excluding the height of the mode- or header-line, if any) that
9967 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9968 ignored. Since calculating the text height of a large buffer can take
9969 some time, it makes sense to specify this argument if the size of the
9970 buffer is large or unknown.
9972 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9973 include the height of the mode- or header-line of WINDOW in the return
9974 value. If it is either the symbol `mode-line' or `header-line', include
9975 only the height of that line, if present, in the return value. If t,
9976 include the height of both, if present, in the return value. */)
9977 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9978 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9980 struct window *w = decode_live_window (window);
9981 Lisp_Object buffer = w->contents;
9982 struct buffer *b;
9983 struct it it;
9984 struct buffer *old_b = NULL;
9985 ptrdiff_t start, end, pos;
9986 struct text_pos startp;
9987 void *itdata = NULL;
9988 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9990 CHECK_BUFFER (buffer);
9991 b = XBUFFER (buffer);
9993 if (b != current_buffer)
9995 old_b = current_buffer;
9996 set_buffer_internal (b);
9999 if (NILP (from))
10000 start = BEGV;
10001 else if (EQ (from, Qt))
10003 start = pos = BEGV;
10004 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10005 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10006 start = pos;
10007 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10008 start = pos;
10010 else
10012 CHECK_NUMBER_COERCE_MARKER (from);
10013 start = min (max (XINT (from), BEGV), ZV);
10016 if (NILP (to))
10017 end = ZV;
10018 else if (EQ (to, Qt))
10020 end = pos = ZV;
10021 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10022 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10023 end = pos;
10024 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10025 end = pos;
10027 else
10029 CHECK_NUMBER_COERCE_MARKER (to);
10030 end = max (start, min (XINT (to), ZV));
10033 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10034 max_x = XINT (x_limit);
10036 if (NILP (y_limit))
10037 max_y = INT_MAX;
10038 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10039 max_y = XINT (y_limit);
10041 itdata = bidi_shelve_cache ();
10042 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10043 start_display (&it, w, startp);
10045 if (NILP (x_limit))
10046 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10047 else
10049 it.last_visible_x = max_x;
10050 /* Actually, we never want move_it_to stop at to_x. But to make
10051 sure that move_it_in_display_line_to always moves far enough,
10052 we set it to INT_MAX and specify MOVE_TO_X. */
10053 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10054 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10055 /* Don't return more than X-LIMIT. */
10056 if (x > max_x)
10057 x = max_x;
10060 /* Subtract height of header-line which was counted automatically by
10061 start_display. */
10062 y = it.current_y + it.max_ascent + it.max_descent
10063 - WINDOW_HEADER_LINE_HEIGHT (w);
10064 /* Don't return more than Y-LIMIT. */
10065 if (y > max_y)
10066 y = max_y;
10068 if (EQ (mode_and_header_line, Qheader_line)
10069 || EQ (mode_and_header_line, Qt))
10070 /* Re-add height of header-line as requested. */
10071 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10073 if (EQ (mode_and_header_line, Qmode_line)
10074 || EQ (mode_and_header_line, Qt))
10075 /* Add height of mode-line as requested. */
10076 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10078 bidi_unshelve_cache (itdata, false);
10080 if (old_b)
10081 set_buffer_internal (old_b);
10083 return Fcons (make_number (x), make_number (y));
10086 /***********************************************************************
10087 Messages
10088 ***********************************************************************/
10090 /* Return the number of arguments the format string FORMAT needs. */
10092 static ptrdiff_t
10093 format_nargs (char const *format)
10095 ptrdiff_t nargs = 0;
10096 for (char const *p = format; (p = strchr (p, '%')); p++)
10097 if (p[1] == '%')
10098 p++;
10099 else
10100 nargs++;
10101 return nargs;
10104 /* Add a message with format string FORMAT and formatted arguments
10105 to *Messages*. */
10107 void
10108 add_to_log (const char *format, ...)
10110 va_list ap;
10111 va_start (ap, format);
10112 vadd_to_log (format, ap);
10113 va_end (ap);
10116 void
10117 vadd_to_log (char const *format, va_list ap)
10119 ptrdiff_t form_nargs = format_nargs (format);
10120 ptrdiff_t nargs = 1 + form_nargs;
10121 Lisp_Object args[10];
10122 eassert (nargs <= ARRAYELTS (args));
10123 AUTO_STRING (args0, format);
10124 args[0] = args0;
10125 for (ptrdiff_t i = 1; i <= nargs; i++)
10126 args[i] = va_arg (ap, Lisp_Object);
10127 Lisp_Object msg = Qnil;
10128 msg = Fformat_message (nargs, args);
10130 ptrdiff_t len = SBYTES (msg) + 1;
10131 USE_SAFE_ALLOCA;
10132 char *buffer = SAFE_ALLOCA (len);
10133 memcpy (buffer, SDATA (msg), len);
10135 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10136 SAFE_FREE ();
10140 /* Output a newline in the *Messages* buffer if "needs" one. */
10142 void
10143 message_log_maybe_newline (void)
10145 if (message_log_need_newline)
10146 message_dolog ("", 0, true, false);
10150 /* Add a string M of length NBYTES to the message log, optionally
10151 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10152 true, means interpret the contents of M as multibyte. This
10153 function calls low-level routines in order to bypass text property
10154 hooks, etc. which might not be safe to run.
10156 This may GC (insert may run before/after change hooks),
10157 so the buffer M must NOT point to a Lisp string. */
10159 void
10160 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10162 const unsigned char *msg = (const unsigned char *) m;
10164 if (!NILP (Vmemory_full))
10165 return;
10167 if (!NILP (Vmessage_log_max))
10169 struct buffer *oldbuf;
10170 Lisp_Object oldpoint, oldbegv, oldzv;
10171 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10172 ptrdiff_t point_at_end = 0;
10173 ptrdiff_t zv_at_end = 0;
10174 Lisp_Object old_deactivate_mark;
10176 old_deactivate_mark = Vdeactivate_mark;
10177 oldbuf = current_buffer;
10179 /* Ensure the Messages buffer exists, and switch to it.
10180 If we created it, set the major-mode. */
10181 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10182 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10183 if (newbuffer
10184 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10185 call0 (intern ("messages-buffer-mode"));
10187 bset_undo_list (current_buffer, Qt);
10188 bset_cache_long_scans (current_buffer, Qnil);
10190 oldpoint = message_dolog_marker1;
10191 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10192 oldbegv = message_dolog_marker2;
10193 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10194 oldzv = message_dolog_marker3;
10195 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10197 if (PT == Z)
10198 point_at_end = 1;
10199 if (ZV == Z)
10200 zv_at_end = 1;
10202 BEGV = BEG;
10203 BEGV_BYTE = BEG_BYTE;
10204 ZV = Z;
10205 ZV_BYTE = Z_BYTE;
10206 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10208 /* Insert the string--maybe converting multibyte to single byte
10209 or vice versa, so that all the text fits the buffer. */
10210 if (multibyte
10211 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10213 ptrdiff_t i;
10214 int c, char_bytes;
10215 char work[1];
10217 /* Convert a multibyte string to single-byte
10218 for the *Message* buffer. */
10219 for (i = 0; i < nbytes; i += char_bytes)
10221 c = string_char_and_length (msg + i, &char_bytes);
10222 work[0] = CHAR_TO_BYTE8 (c);
10223 insert_1_both (work, 1, 1, true, false, false);
10226 else if (! multibyte
10227 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10229 ptrdiff_t i;
10230 int c, char_bytes;
10231 unsigned char str[MAX_MULTIBYTE_LENGTH];
10232 /* Convert a single-byte string to multibyte
10233 for the *Message* buffer. */
10234 for (i = 0; i < nbytes; i++)
10236 c = msg[i];
10237 MAKE_CHAR_MULTIBYTE (c);
10238 char_bytes = CHAR_STRING (c, str);
10239 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10242 else if (nbytes)
10243 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10244 true, false, false);
10246 if (nlflag)
10248 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10249 printmax_t dups;
10251 insert_1_both ("\n", 1, 1, true, false, false);
10253 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10254 this_bol = PT;
10255 this_bol_byte = PT_BYTE;
10257 /* See if this line duplicates the previous one.
10258 If so, combine duplicates. */
10259 if (this_bol > BEG)
10261 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10262 prev_bol = PT;
10263 prev_bol_byte = PT_BYTE;
10265 dups = message_log_check_duplicate (prev_bol_byte,
10266 this_bol_byte);
10267 if (dups)
10269 del_range_both (prev_bol, prev_bol_byte,
10270 this_bol, this_bol_byte, false);
10271 if (dups > 1)
10273 char dupstr[sizeof " [ times]"
10274 + INT_STRLEN_BOUND (printmax_t)];
10276 /* If you change this format, don't forget to also
10277 change message_log_check_duplicate. */
10278 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10279 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10280 insert_1_both (dupstr, duplen, duplen,
10281 true, false, true);
10286 /* If we have more than the desired maximum number of lines
10287 in the *Messages* buffer now, delete the oldest ones.
10288 This is safe because we don't have undo in this buffer. */
10290 if (NATNUMP (Vmessage_log_max))
10292 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10293 -XFASTINT (Vmessage_log_max) - 1, false);
10294 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10297 BEGV = marker_position (oldbegv);
10298 BEGV_BYTE = marker_byte_position (oldbegv);
10300 if (zv_at_end)
10302 ZV = Z;
10303 ZV_BYTE = Z_BYTE;
10305 else
10307 ZV = marker_position (oldzv);
10308 ZV_BYTE = marker_byte_position (oldzv);
10311 if (point_at_end)
10312 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10313 else
10314 /* We can't do Fgoto_char (oldpoint) because it will run some
10315 Lisp code. */
10316 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10317 marker_byte_position (oldpoint));
10319 unchain_marker (XMARKER (oldpoint));
10320 unchain_marker (XMARKER (oldbegv));
10321 unchain_marker (XMARKER (oldzv));
10323 /* We called insert_1_both above with its 5th argument (PREPARE)
10324 false, which prevents insert_1_both from calling
10325 prepare_to_modify_buffer, which in turns prevents us from
10326 incrementing windows_or_buffers_changed even if *Messages* is
10327 shown in some window. So we must manually set
10328 windows_or_buffers_changed here to make up for that. */
10329 windows_or_buffers_changed = old_windows_or_buffers_changed;
10330 bset_redisplay (current_buffer);
10332 set_buffer_internal (oldbuf);
10334 message_log_need_newline = !nlflag;
10335 Vdeactivate_mark = old_deactivate_mark;
10340 /* We are at the end of the buffer after just having inserted a newline.
10341 (Note: We depend on the fact we won't be crossing the gap.)
10342 Check to see if the most recent message looks a lot like the previous one.
10343 Return 0 if different, 1 if the new one should just replace it, or a
10344 value N > 1 if we should also append " [N times]". */
10346 static intmax_t
10347 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10349 ptrdiff_t i;
10350 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10351 bool seen_dots = false;
10352 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10353 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10355 for (i = 0; i < len; i++)
10357 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10358 seen_dots = true;
10359 if (p1[i] != p2[i])
10360 return seen_dots;
10362 p1 += len;
10363 if (*p1 == '\n')
10364 return 2;
10365 if (*p1++ == ' ' && *p1++ == '[')
10367 char *pend;
10368 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10369 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10370 return n + 1;
10372 return 0;
10376 /* Display an echo area message M with a specified length of NBYTES
10377 bytes. The string may include null characters. If M is not a
10378 string, clear out any existing message, and let the mini-buffer
10379 text show through.
10381 This function cancels echoing. */
10383 void
10384 message3 (Lisp_Object m)
10386 clear_message (true, true);
10387 cancel_echoing ();
10389 /* First flush out any partial line written with print. */
10390 message_log_maybe_newline ();
10391 if (STRINGP (m))
10393 ptrdiff_t nbytes = SBYTES (m);
10394 bool multibyte = STRING_MULTIBYTE (m);
10395 char *buffer;
10396 USE_SAFE_ALLOCA;
10397 SAFE_ALLOCA_STRING (buffer, m);
10398 message_dolog (buffer, nbytes, true, multibyte);
10399 SAFE_FREE ();
10401 if (! inhibit_message)
10402 message3_nolog (m);
10405 /* Log the message M to stderr. Log an empty line if M is not a string. */
10407 static void
10408 message_to_stderr (Lisp_Object m)
10410 if (noninteractive_need_newline)
10412 noninteractive_need_newline = false;
10413 fputc ('\n', stderr);
10415 if (STRINGP (m))
10417 Lisp_Object coding_system = Vlocale_coding_system;
10418 Lisp_Object s;
10420 if (!NILP (Vcoding_system_for_write))
10421 coding_system = Vcoding_system_for_write;
10422 if (!NILP (coding_system))
10423 s = code_convert_string_norecord (m, coding_system, true);
10424 else
10425 s = m;
10427 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10429 if (!cursor_in_echo_area)
10430 fputc ('\n', stderr);
10431 fflush (stderr);
10434 /* The non-logging version of message3.
10435 This does not cancel echoing, because it is used for echoing.
10436 Perhaps we need to make a separate function for echoing
10437 and make this cancel echoing. */
10439 void
10440 message3_nolog (Lisp_Object m)
10442 struct frame *sf = SELECTED_FRAME ();
10444 if (FRAME_INITIAL_P (sf))
10445 message_to_stderr (m);
10446 /* Error messages get reported properly by cmd_error, so this must be just an
10447 informative message; if the frame hasn't really been initialized yet, just
10448 toss it. */
10449 else if (INTERACTIVE && sf->glyphs_initialized_p)
10451 /* Get the frame containing the mini-buffer
10452 that the selected frame is using. */
10453 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10454 Lisp_Object frame = XWINDOW (mini_window)->frame;
10455 struct frame *f = XFRAME (frame);
10457 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10458 Fmake_frame_visible (frame);
10460 if (STRINGP (m) && SCHARS (m) > 0)
10462 set_message (m);
10463 if (minibuffer_auto_raise)
10464 Fraise_frame (frame);
10465 /* Assume we are not echoing.
10466 (If we are, echo_now will override this.) */
10467 echo_message_buffer = Qnil;
10469 else
10470 clear_message (true, true);
10472 do_pending_window_change (false);
10473 echo_area_display (true);
10474 do_pending_window_change (false);
10475 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10476 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10481 /* Display a null-terminated echo area message M. If M is 0, clear
10482 out any existing message, and let the mini-buffer text show through.
10484 The buffer M must continue to exist until after the echo area gets
10485 cleared or some other message gets displayed there. Do not pass
10486 text that is stored in a Lisp string. Do not pass text in a buffer
10487 that was alloca'd. */
10489 void
10490 message1 (const char *m)
10492 message3 (m ? build_unibyte_string (m) : Qnil);
10496 /* The non-logging counterpart of message1. */
10498 void
10499 message1_nolog (const char *m)
10501 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10504 /* Display a message M which contains a single %s
10505 which gets replaced with STRING. */
10507 void
10508 message_with_string (const char *m, Lisp_Object string, bool log)
10510 CHECK_STRING (string);
10512 bool need_message;
10513 if (noninteractive)
10514 need_message = !!m;
10515 else if (!INTERACTIVE)
10516 need_message = false;
10517 else
10519 /* The frame whose minibuffer we're going to display the message on.
10520 It may be larger than the selected frame, so we need
10521 to use its buffer, not the selected frame's buffer. */
10522 Lisp_Object mini_window;
10523 struct frame *f, *sf = SELECTED_FRAME ();
10525 /* Get the frame containing the minibuffer
10526 that the selected frame is using. */
10527 mini_window = FRAME_MINIBUF_WINDOW (sf);
10528 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10530 /* Error messages get reported properly by cmd_error, so this must be
10531 just an informative message; if the frame hasn't really been
10532 initialized yet, just toss it. */
10533 need_message = f->glyphs_initialized_p;
10536 if (need_message)
10538 AUTO_STRING (fmt, m);
10539 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10541 if (noninteractive)
10542 message_to_stderr (msg);
10543 else
10545 if (log)
10546 message3 (msg);
10547 else
10548 message3_nolog (msg);
10550 /* Print should start at the beginning of the message
10551 buffer next time. */
10552 message_buf_print = false;
10558 /* Dump an informative message to the minibuf. If M is 0, clear out
10559 any existing message, and let the mini-buffer text show through.
10561 The message must be safe ASCII (because when Emacs is
10562 non-interactive the message is sent straight to stderr without
10563 encoding first) and the format must not contain ` or ' (because
10564 this function does not account for `text-quoting-style'). If your
10565 message and format do not fit into this category, convert your
10566 arguments to Lisp objects and use Fmessage instead. */
10568 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10569 vmessage (const char *m, va_list ap)
10571 if (noninteractive)
10573 if (m)
10575 if (noninteractive_need_newline)
10576 putc ('\n', stderr);
10577 noninteractive_need_newline = false;
10578 vfprintf (stderr, m, ap);
10579 if (!cursor_in_echo_area)
10580 fprintf (stderr, "\n");
10581 fflush (stderr);
10584 else if (INTERACTIVE)
10586 /* The frame whose mini-buffer we're going to display the message
10587 on. It may be larger than the selected frame, so we need to
10588 use its buffer, not the selected frame's buffer. */
10589 Lisp_Object mini_window;
10590 struct frame *f, *sf = SELECTED_FRAME ();
10592 /* Get the frame containing the mini-buffer
10593 that the selected frame is using. */
10594 mini_window = FRAME_MINIBUF_WINDOW (sf);
10595 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10597 /* Error messages get reported properly by cmd_error, so this must be
10598 just an informative message; if the frame hasn't really been
10599 initialized yet, just toss it. */
10600 if (f->glyphs_initialized_p)
10602 if (m)
10604 ptrdiff_t len;
10605 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10606 USE_SAFE_ALLOCA;
10607 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10609 len = doprnt (message_buf, maxsize, m, 0, ap);
10611 message3 (make_string (message_buf, len));
10612 SAFE_FREE ();
10614 else
10615 message1 (0);
10617 /* Print should start at the beginning of the message
10618 buffer next time. */
10619 message_buf_print = false;
10624 /* See vmessage for restrictions on the text of the message. */
10625 void
10626 message (const char *m, ...)
10628 va_list ap;
10629 va_start (ap, m);
10630 vmessage (m, ap);
10631 va_end (ap);
10635 /* Display the current message in the current mini-buffer. This is
10636 only called from error handlers in process.c, and is not time
10637 critical. */
10639 void
10640 update_echo_area (void)
10642 if (!NILP (echo_area_buffer[0]))
10644 Lisp_Object string;
10645 string = Fcurrent_message ();
10646 message3 (string);
10651 /* Make sure echo area buffers in `echo_buffers' are live.
10652 If they aren't, make new ones. */
10654 static void
10655 ensure_echo_area_buffers (void)
10657 for (int i = 0; i < 2; i++)
10658 if (!BUFFERP (echo_buffer[i])
10659 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10661 Lisp_Object old_buffer = echo_buffer[i];
10662 static char const name_fmt[] = " *Echo Area %d*";
10663 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10664 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10665 echo_buffer[i] = Fget_buffer_create (lname);
10666 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10667 /* to force word wrap in echo area -
10668 it was decided to postpone this*/
10669 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10671 for (int j = 0; j < 2; j++)
10672 if (EQ (old_buffer, echo_area_buffer[j]))
10673 echo_area_buffer[j] = echo_buffer[i];
10678 /* Call FN with args A1..A2 with either the current or last displayed
10679 echo_area_buffer as current buffer.
10681 WHICH zero means use the current message buffer
10682 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10683 from echo_buffer[] and clear it.
10685 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10686 suitable buffer from echo_buffer[] and clear it.
10688 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10689 that the current message becomes the last displayed one, choose a
10690 suitable buffer for echo_area_buffer[0], and clear it.
10692 Value is what FN returns. */
10694 static bool
10695 with_echo_area_buffer (struct window *w, int which,
10696 bool (*fn) (ptrdiff_t, Lisp_Object),
10697 ptrdiff_t a1, Lisp_Object a2)
10699 Lisp_Object buffer;
10700 bool this_one, the_other, clear_buffer_p, rc;
10701 ptrdiff_t count = SPECPDL_INDEX ();
10703 /* If buffers aren't live, make new ones. */
10704 ensure_echo_area_buffers ();
10706 clear_buffer_p = false;
10708 if (which == 0)
10709 this_one = false, the_other = true;
10710 else if (which > 0)
10711 this_one = true, the_other = false;
10712 else
10714 this_one = false, the_other = true;
10715 clear_buffer_p = true;
10717 /* We need a fresh one in case the current echo buffer equals
10718 the one containing the last displayed echo area message. */
10719 if (!NILP (echo_area_buffer[this_one])
10720 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10721 echo_area_buffer[this_one] = Qnil;
10724 /* Choose a suitable buffer from echo_buffer[] if we don't
10725 have one. */
10726 if (NILP (echo_area_buffer[this_one]))
10728 echo_area_buffer[this_one]
10729 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10730 ? echo_buffer[the_other]
10731 : echo_buffer[this_one]);
10732 clear_buffer_p = true;
10735 buffer = echo_area_buffer[this_one];
10737 /* Don't get confused by reusing the buffer used for echoing
10738 for a different purpose. */
10739 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10740 cancel_echoing ();
10742 record_unwind_protect (unwind_with_echo_area_buffer,
10743 with_echo_area_buffer_unwind_data (w));
10745 /* Make the echo area buffer current. Note that for display
10746 purposes, it is not necessary that the displayed window's buffer
10747 == current_buffer, except for text property lookup. So, let's
10748 only set that buffer temporarily here without doing a full
10749 Fset_window_buffer. We must also change w->pointm, though,
10750 because otherwise an assertions in unshow_buffer fails, and Emacs
10751 aborts. */
10752 set_buffer_internal_1 (XBUFFER (buffer));
10753 if (w)
10755 wset_buffer (w, buffer);
10756 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10757 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10760 bset_undo_list (current_buffer, Qt);
10761 bset_read_only (current_buffer, Qnil);
10762 specbind (Qinhibit_read_only, Qt);
10763 specbind (Qinhibit_modification_hooks, Qt);
10765 if (clear_buffer_p && Z > BEG)
10766 del_range (BEG, Z);
10768 eassert (BEGV >= BEG);
10769 eassert (ZV <= Z && ZV >= BEGV);
10771 rc = fn (a1, a2);
10773 eassert (BEGV >= BEG);
10774 eassert (ZV <= Z && ZV >= BEGV);
10776 unbind_to (count, Qnil);
10777 return rc;
10781 /* Save state that should be preserved around the call to the function
10782 FN called in with_echo_area_buffer. */
10784 static Lisp_Object
10785 with_echo_area_buffer_unwind_data (struct window *w)
10787 int i = 0;
10788 Lisp_Object vector, tmp;
10790 /* Reduce consing by keeping one vector in
10791 Vwith_echo_area_save_vector. */
10792 vector = Vwith_echo_area_save_vector;
10793 Vwith_echo_area_save_vector = Qnil;
10795 if (NILP (vector))
10796 vector = Fmake_vector (make_number (11), Qnil);
10798 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10799 ASET (vector, i, Vdeactivate_mark); ++i;
10800 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10802 if (w)
10804 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10805 ASET (vector, i, w->contents); ++i;
10806 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10807 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10808 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10809 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10810 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10811 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10813 else
10815 int end = i + 8;
10816 for (; i < end; ++i)
10817 ASET (vector, i, Qnil);
10820 eassert (i == ASIZE (vector));
10821 return vector;
10825 /* Restore global state from VECTOR which was created by
10826 with_echo_area_buffer_unwind_data. */
10828 static void
10829 unwind_with_echo_area_buffer (Lisp_Object vector)
10831 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10832 Vdeactivate_mark = AREF (vector, 1);
10833 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10835 if (WINDOWP (AREF (vector, 3)))
10837 struct window *w;
10838 Lisp_Object buffer;
10840 w = XWINDOW (AREF (vector, 3));
10841 buffer = AREF (vector, 4);
10843 wset_buffer (w, buffer);
10844 set_marker_both (w->pointm, buffer,
10845 XFASTINT (AREF (vector, 5)),
10846 XFASTINT (AREF (vector, 6)));
10847 set_marker_both (w->old_pointm, buffer,
10848 XFASTINT (AREF (vector, 7)),
10849 XFASTINT (AREF (vector, 8)));
10850 set_marker_both (w->start, buffer,
10851 XFASTINT (AREF (vector, 9)),
10852 XFASTINT (AREF (vector, 10)));
10855 Vwith_echo_area_save_vector = vector;
10859 /* Set up the echo area for use by print functions. MULTIBYTE_P
10860 means we will print multibyte. */
10862 void
10863 setup_echo_area_for_printing (bool multibyte_p)
10865 /* If we can't find an echo area any more, exit. */
10866 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10867 Fkill_emacs (Qnil);
10869 ensure_echo_area_buffers ();
10871 if (!message_buf_print)
10873 /* A message has been output since the last time we printed.
10874 Choose a fresh echo area buffer. */
10875 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10876 echo_area_buffer[0] = echo_buffer[1];
10877 else
10878 echo_area_buffer[0] = echo_buffer[0];
10880 /* Switch to that buffer and clear it. */
10881 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10882 bset_truncate_lines (current_buffer, Qnil);
10884 if (Z > BEG)
10886 ptrdiff_t count = SPECPDL_INDEX ();
10887 specbind (Qinhibit_read_only, Qt);
10888 /* Note that undo recording is always disabled. */
10889 del_range (BEG, Z);
10890 unbind_to (count, Qnil);
10892 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10894 /* Set up the buffer for the multibyteness we need. */
10895 if (multibyte_p
10896 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10897 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10899 /* Raise the frame containing the echo area. */
10900 if (minibuffer_auto_raise)
10902 struct frame *sf = SELECTED_FRAME ();
10903 Lisp_Object mini_window;
10904 mini_window = FRAME_MINIBUF_WINDOW (sf);
10905 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10908 message_log_maybe_newline ();
10909 message_buf_print = true;
10911 else
10913 if (NILP (echo_area_buffer[0]))
10915 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10916 echo_area_buffer[0] = echo_buffer[1];
10917 else
10918 echo_area_buffer[0] = echo_buffer[0];
10921 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10923 /* Someone switched buffers between print requests. */
10924 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10925 bset_truncate_lines (current_buffer, Qnil);
10931 /* Display an echo area message in window W. Value is true if W's
10932 height is changed. If display_last_displayed_message_p,
10933 display the message that was last displayed, otherwise
10934 display the current message. */
10936 static bool
10937 display_echo_area (struct window *w)
10939 bool no_message_p, window_height_changed_p;
10941 /* Temporarily disable garbage collections while displaying the echo
10942 area. This is done because a GC can print a message itself.
10943 That message would modify the echo area buffer's contents while a
10944 redisplay of the buffer is going on, and seriously confuse
10945 redisplay. */
10946 ptrdiff_t count = inhibit_garbage_collection ();
10948 /* If there is no message, we must call display_echo_area_1
10949 nevertheless because it resizes the window. But we will have to
10950 reset the echo_area_buffer in question to nil at the end because
10951 with_echo_area_buffer will sets it to an empty buffer. */
10952 bool i = display_last_displayed_message_p;
10953 /* According to the C99, C11 and C++11 standards, the integral value
10954 of a "bool" is always 0 or 1, so this array access is safe here,
10955 if oddly typed. */
10956 no_message_p = NILP (echo_area_buffer[i]);
10958 window_height_changed_p
10959 = with_echo_area_buffer (w, display_last_displayed_message_p,
10960 display_echo_area_1,
10961 (intptr_t) w, Qnil);
10963 if (no_message_p)
10964 echo_area_buffer[i] = Qnil;
10966 unbind_to (count, Qnil);
10967 return window_height_changed_p;
10971 /* Helper for display_echo_area. Display the current buffer which
10972 contains the current echo area message in window W, a mini-window,
10973 a pointer to which is passed in A1. A2..A4 are currently not used.
10974 Change the height of W so that all of the message is displayed.
10975 Value is true if height of W was changed. */
10977 static bool
10978 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10980 intptr_t i1 = a1;
10981 struct window *w = (struct window *) i1;
10982 Lisp_Object window;
10983 struct text_pos start;
10985 /* We are about to enter redisplay without going through
10986 redisplay_internal, so we need to forget these faces by hand
10987 here. */
10988 forget_escape_and_glyphless_faces ();
10990 /* Do this before displaying, so that we have a large enough glyph
10991 matrix for the display. If we can't get enough space for the
10992 whole text, display the last N lines. That works by setting w->start. */
10993 bool window_height_changed_p = resize_mini_window (w, false);
10995 /* Use the starting position chosen by resize_mini_window. */
10996 SET_TEXT_POS_FROM_MARKER (start, w->start);
10998 /* Display. */
10999 clear_glyph_matrix (w->desired_matrix);
11000 XSETWINDOW (window, w);
11001 try_window (window, start, 0);
11003 return window_height_changed_p;
11007 /* Resize the echo area window to exactly the size needed for the
11008 currently displayed message, if there is one. If a mini-buffer
11009 is active, don't shrink it. */
11011 void
11012 resize_echo_area_exactly (void)
11014 if (BUFFERP (echo_area_buffer[0])
11015 && WINDOWP (echo_area_window))
11017 struct window *w = XWINDOW (echo_area_window);
11018 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11019 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11020 (intptr_t) w, resize_exactly);
11021 if (resized_p)
11023 windows_or_buffers_changed = 42;
11024 update_mode_lines = 30;
11025 redisplay_internal ();
11031 /* Callback function for with_echo_area_buffer, when used from
11032 resize_echo_area_exactly. A1 contains a pointer to the window to
11033 resize, EXACTLY non-nil means resize the mini-window exactly to the
11034 size of the text displayed. A3 and A4 are not used. Value is what
11035 resize_mini_window returns. */
11037 static bool
11038 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11040 intptr_t i1 = a1;
11041 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11045 /* Resize mini-window W to fit the size of its contents. EXACT_P
11046 means size the window exactly to the size needed. Otherwise, it's
11047 only enlarged until W's buffer is empty.
11049 Set W->start to the right place to begin display. If the whole
11050 contents fit, start at the beginning. Otherwise, start so as
11051 to make the end of the contents appear. This is particularly
11052 important for y-or-n-p, but seems desirable generally.
11054 Value is true if the window height has been changed. */
11056 bool
11057 resize_mini_window (struct window *w, bool exact_p)
11059 struct frame *f = XFRAME (w->frame);
11060 bool window_height_changed_p = false;
11062 eassert (MINI_WINDOW_P (w));
11064 /* By default, start display at the beginning. */
11065 set_marker_both (w->start, w->contents,
11066 BUF_BEGV (XBUFFER (w->contents)),
11067 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11069 /* Don't resize windows while redisplaying a window; it would
11070 confuse redisplay functions when the size of the window they are
11071 displaying changes from under them. Such a resizing can happen,
11072 for instance, when which-func prints a long message while
11073 we are running fontification-functions. We're running these
11074 functions with safe_call which binds inhibit-redisplay to t. */
11075 if (!NILP (Vinhibit_redisplay))
11076 return false;
11078 /* Nil means don't try to resize. */
11079 if (NILP (Vresize_mini_windows)
11080 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11081 return false;
11083 if (!FRAME_MINIBUF_ONLY_P (f))
11085 struct it it;
11086 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11087 + WINDOW_PIXEL_HEIGHT (w));
11088 int unit = FRAME_LINE_HEIGHT (f);
11089 int height, max_height;
11090 struct text_pos start;
11091 struct buffer *old_current_buffer = NULL;
11093 if (current_buffer != XBUFFER (w->contents))
11095 old_current_buffer = current_buffer;
11096 set_buffer_internal (XBUFFER (w->contents));
11099 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11101 /* Compute the max. number of lines specified by the user. */
11102 if (FLOATP (Vmax_mini_window_height))
11103 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11104 else if (INTEGERP (Vmax_mini_window_height))
11105 max_height = XINT (Vmax_mini_window_height) * unit;
11106 else
11107 max_height = total_height / 4;
11109 /* Correct that max. height if it's bogus. */
11110 max_height = clip_to_bounds (unit, max_height, total_height);
11112 /* Find out the height of the text in the window. */
11113 if (it.line_wrap == TRUNCATE)
11114 height = unit;
11115 else
11117 last_height = 0;
11118 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11119 if (it.max_ascent == 0 && it.max_descent == 0)
11120 height = it.current_y + last_height;
11121 else
11122 height = it.current_y + it.max_ascent + it.max_descent;
11123 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11126 /* Compute a suitable window start. */
11127 if (height > max_height)
11129 height = (max_height / unit) * unit;
11130 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11131 move_it_vertically_backward (&it, height - unit);
11132 start = it.current.pos;
11134 else
11135 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11136 SET_MARKER_FROM_TEXT_POS (w->start, start);
11138 if (EQ (Vresize_mini_windows, Qgrow_only))
11140 /* Let it grow only, until we display an empty message, in which
11141 case the window shrinks again. */
11142 if (height > WINDOW_PIXEL_HEIGHT (w))
11144 int old_height = WINDOW_PIXEL_HEIGHT (w);
11146 FRAME_WINDOWS_FROZEN (f) = true;
11147 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11148 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11150 else if (height < WINDOW_PIXEL_HEIGHT (w)
11151 && (exact_p || BEGV == ZV))
11153 int old_height = WINDOW_PIXEL_HEIGHT (w);
11155 FRAME_WINDOWS_FROZEN (f) = false;
11156 shrink_mini_window (w, true);
11157 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11160 else
11162 /* Always resize to exact size needed. */
11163 if (height > WINDOW_PIXEL_HEIGHT (w))
11165 int old_height = WINDOW_PIXEL_HEIGHT (w);
11167 FRAME_WINDOWS_FROZEN (f) = true;
11168 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11169 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11171 else if (height < WINDOW_PIXEL_HEIGHT (w))
11173 int old_height = WINDOW_PIXEL_HEIGHT (w);
11175 FRAME_WINDOWS_FROZEN (f) = false;
11176 shrink_mini_window (w, true);
11178 if (height)
11180 FRAME_WINDOWS_FROZEN (f) = true;
11181 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11184 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11188 if (old_current_buffer)
11189 set_buffer_internal (old_current_buffer);
11192 return window_height_changed_p;
11196 /* Value is the current message, a string, or nil if there is no
11197 current message. */
11199 Lisp_Object
11200 current_message (void)
11202 Lisp_Object msg;
11204 if (!BUFFERP (echo_area_buffer[0]))
11205 msg = Qnil;
11206 else
11208 with_echo_area_buffer (0, 0, current_message_1,
11209 (intptr_t) &msg, Qnil);
11210 if (NILP (msg))
11211 echo_area_buffer[0] = Qnil;
11214 return msg;
11218 static bool
11219 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11221 intptr_t i1 = a1;
11222 Lisp_Object *msg = (Lisp_Object *) i1;
11224 if (Z > BEG)
11225 *msg = make_buffer_string (BEG, Z, true);
11226 else
11227 *msg = Qnil;
11228 return false;
11232 /* Push the current message on Vmessage_stack for later restoration
11233 by restore_message. Value is true if the current message isn't
11234 empty. This is a relatively infrequent operation, so it's not
11235 worth optimizing. */
11237 bool
11238 push_message (void)
11240 Lisp_Object msg = current_message ();
11241 Vmessage_stack = Fcons (msg, Vmessage_stack);
11242 return STRINGP (msg);
11246 /* Restore message display from the top of Vmessage_stack. */
11248 void
11249 restore_message (void)
11251 eassert (CONSP (Vmessage_stack));
11252 message3_nolog (XCAR (Vmessage_stack));
11256 /* Handler for unwind-protect calling pop_message. */
11258 void
11259 pop_message_unwind (void)
11261 /* Pop the top-most entry off Vmessage_stack. */
11262 eassert (CONSP (Vmessage_stack));
11263 Vmessage_stack = XCDR (Vmessage_stack);
11267 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11268 exits. If the stack is not empty, we have a missing pop_message
11269 somewhere. */
11271 void
11272 check_message_stack (void)
11274 if (!NILP (Vmessage_stack))
11275 emacs_abort ();
11279 /* Truncate to NCHARS what will be displayed in the echo area the next
11280 time we display it---but don't redisplay it now. */
11282 void
11283 truncate_echo_area (ptrdiff_t nchars)
11285 if (nchars == 0)
11286 echo_area_buffer[0] = Qnil;
11287 else if (!noninteractive
11288 && INTERACTIVE
11289 && !NILP (echo_area_buffer[0]))
11291 struct frame *sf = SELECTED_FRAME ();
11292 /* Error messages get reported properly by cmd_error, so this must be
11293 just an informative message; if the frame hasn't really been
11294 initialized yet, just toss it. */
11295 if (sf->glyphs_initialized_p)
11296 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11301 /* Helper function for truncate_echo_area. Truncate the current
11302 message to at most NCHARS characters. */
11304 static bool
11305 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11307 if (BEG + nchars < Z)
11308 del_range (BEG + nchars, Z);
11309 if (Z == BEG)
11310 echo_area_buffer[0] = Qnil;
11311 return false;
11314 /* Set the current message to STRING. */
11316 static void
11317 set_message (Lisp_Object string)
11319 eassert (STRINGP (string));
11321 message_enable_multibyte = STRING_MULTIBYTE (string);
11323 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11324 message_buf_print = false;
11325 help_echo_showing_p = false;
11327 if (STRINGP (Vdebug_on_message)
11328 && STRINGP (string)
11329 && fast_string_match (Vdebug_on_message, string) >= 0)
11330 call_debugger (list2 (Qerror, string));
11334 /* Helper function for set_message. First argument is ignored and second
11335 argument has the same meaning as for set_message.
11336 This function is called with the echo area buffer being current. */
11338 static bool
11339 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11341 eassert (STRINGP (string));
11343 /* Change multibyteness of the echo buffer appropriately. */
11344 if (message_enable_multibyte
11345 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11346 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11348 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11349 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11350 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11352 /* Insert new message at BEG. */
11353 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11355 /* This function takes care of single/multibyte conversion.
11356 We just have to ensure that the echo area buffer has the right
11357 setting of enable_multibyte_characters. */
11358 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11360 return false;
11364 /* Clear messages. CURRENT_P means clear the current message.
11365 LAST_DISPLAYED_P means clear the message last displayed. */
11367 void
11368 clear_message (bool current_p, bool last_displayed_p)
11370 if (current_p)
11372 echo_area_buffer[0] = Qnil;
11373 message_cleared_p = true;
11376 if (last_displayed_p)
11377 echo_area_buffer[1] = Qnil;
11379 message_buf_print = false;
11382 /* Clear garbaged frames.
11384 This function is used where the old redisplay called
11385 redraw_garbaged_frames which in turn called redraw_frame which in
11386 turn called clear_frame. The call to clear_frame was a source of
11387 flickering. I believe a clear_frame is not necessary. It should
11388 suffice in the new redisplay to invalidate all current matrices,
11389 and ensure a complete redisplay of all windows. */
11391 static void
11392 clear_garbaged_frames (void)
11394 if (frame_garbaged)
11396 Lisp_Object tail, frame;
11397 struct frame *sf = SELECTED_FRAME ();
11399 FOR_EACH_FRAME (tail, frame)
11401 struct frame *f = XFRAME (frame);
11403 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11405 if (f->resized_p
11406 /* It makes no sense to redraw a non-selected TTY
11407 frame, since that will actually clear the
11408 selected frame, and might leave the selected
11409 frame with corrupted display, if it happens not
11410 to be marked garbaged. */
11411 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11412 redraw_frame (f);
11413 else
11414 clear_current_matrices (f);
11416 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11417 x_clear_under_internal_border (f);
11418 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11420 fset_redisplay (f);
11421 f->garbaged = false;
11422 f->resized_p = false;
11426 frame_garbaged = false;
11431 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11432 selected_frame. */
11434 static void
11435 echo_area_display (bool update_frame_p)
11437 Lisp_Object mini_window;
11438 struct window *w;
11439 struct frame *f;
11440 bool window_height_changed_p = false;
11441 struct frame *sf = SELECTED_FRAME ();
11443 mini_window = FRAME_MINIBUF_WINDOW (sf);
11444 w = XWINDOW (mini_window);
11445 f = XFRAME (WINDOW_FRAME (w));
11447 /* Don't display if frame is invisible or not yet initialized. */
11448 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11449 return;
11451 #ifdef HAVE_WINDOW_SYSTEM
11452 /* When Emacs starts, selected_frame may be the initial terminal
11453 frame. If we let this through, a message would be displayed on
11454 the terminal. */
11455 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11456 return;
11457 #endif /* HAVE_WINDOW_SYSTEM */
11459 /* Redraw garbaged frames. */
11460 clear_garbaged_frames ();
11462 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11464 echo_area_window = mini_window;
11465 window_height_changed_p = display_echo_area (w);
11466 w->must_be_updated_p = true;
11468 /* Update the display, unless called from redisplay_internal.
11469 Also don't update the screen during redisplay itself. The
11470 update will happen at the end of redisplay, and an update
11471 here could cause confusion. */
11472 if (update_frame_p && !redisplaying_p)
11474 int n = 0;
11476 /* If the display update has been interrupted by pending
11477 input, update mode lines in the frame. Due to the
11478 pending input, it might have been that redisplay hasn't
11479 been called, so that mode lines above the echo area are
11480 garbaged. This looks odd, so we prevent it here. */
11481 if (!display_completed)
11483 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11485 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11486 x_clear_under_internal_border (f);
11487 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11491 if (window_height_changed_p
11492 /* Don't do this if Emacs is shutting down. Redisplay
11493 needs to run hooks. */
11494 && !NILP (Vrun_hooks))
11496 /* Must update other windows. Likewise as in other
11497 cases, don't let this update be interrupted by
11498 pending input. */
11499 ptrdiff_t count = SPECPDL_INDEX ();
11500 specbind (Qredisplay_dont_pause, Qt);
11501 fset_redisplay (f);
11502 redisplay_internal ();
11503 unbind_to (count, Qnil);
11505 else if (FRAME_WINDOW_P (f) && n == 0)
11507 /* Window configuration is the same as before.
11508 Can do with a display update of the echo area,
11509 unless we displayed some mode lines. */
11510 update_single_window (w);
11511 flush_frame (f);
11513 else
11514 update_frame (f, true, true);
11516 /* If cursor is in the echo area, make sure that the next
11517 redisplay displays the minibuffer, so that the cursor will
11518 be replaced with what the minibuffer wants. */
11519 if (cursor_in_echo_area)
11520 wset_redisplay (XWINDOW (mini_window));
11523 else if (!EQ (mini_window, selected_window))
11524 wset_redisplay (XWINDOW (mini_window));
11526 /* Last displayed message is now the current message. */
11527 echo_area_buffer[1] = echo_area_buffer[0];
11528 /* Inform read_char that we're not echoing. */
11529 echo_message_buffer = Qnil;
11531 /* Prevent redisplay optimization in redisplay_internal by resetting
11532 this_line_start_pos. This is done because the mini-buffer now
11533 displays the message instead of its buffer text. */
11534 if (EQ (mini_window, selected_window))
11535 CHARPOS (this_line_start_pos) = 0;
11537 if (window_height_changed_p)
11539 fset_redisplay (f);
11541 /* If window configuration was changed, frames may have been
11542 marked garbaged. Clear them or we will experience
11543 surprises wrt scrolling.
11544 FIXME: How/why/when? */
11545 clear_garbaged_frames ();
11549 /* True if W's buffer was changed but not saved. */
11551 static bool
11552 window_buffer_changed (struct window *w)
11554 struct buffer *b = XBUFFER (w->contents);
11556 eassert (BUFFER_LIVE_P (b));
11558 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11561 /* True if W has %c or %C in its mode line and mode line should be updated. */
11563 static bool
11564 mode_line_update_needed (struct window *w)
11566 return (w->column_number_displayed != -1
11567 && !(PT == w->last_point && !window_outdated (w))
11568 && (w->column_number_displayed != current_column ()));
11571 /* True if window start of W is frozen and may not be changed during
11572 redisplay. */
11574 static bool
11575 window_frozen_p (struct window *w)
11577 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11579 Lisp_Object window;
11581 XSETWINDOW (window, w);
11582 if (MINI_WINDOW_P (w))
11583 return false;
11584 else if (EQ (window, selected_window))
11585 return false;
11586 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11587 && EQ (window, Vminibuf_scroll_window))
11588 /* This special window can't be frozen too. */
11589 return false;
11590 else
11591 return true;
11593 return false;
11596 /***********************************************************************
11597 Mode Lines and Frame Titles
11598 ***********************************************************************/
11600 /* A buffer for constructing non-propertized mode-line strings and
11601 frame titles in it; allocated from the heap in init_xdisp and
11602 resized as needed in store_mode_line_noprop_char. */
11604 static char *mode_line_noprop_buf;
11606 /* The buffer's end, and a current output position in it. */
11608 static char *mode_line_noprop_buf_end;
11609 static char *mode_line_noprop_ptr;
11611 #define MODE_LINE_NOPROP_LEN(start) \
11612 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11614 static enum {
11615 MODE_LINE_DISPLAY = 0,
11616 MODE_LINE_TITLE,
11617 MODE_LINE_NOPROP,
11618 MODE_LINE_STRING
11619 } mode_line_target;
11621 /* Alist that caches the results of :propertize.
11622 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11623 static Lisp_Object mode_line_proptrans_alist;
11625 /* List of strings making up the mode-line. */
11626 static Lisp_Object mode_line_string_list;
11628 /* Base face property when building propertized mode line string. */
11629 static Lisp_Object mode_line_string_face;
11630 static Lisp_Object mode_line_string_face_prop;
11633 /* Unwind data for mode line strings */
11635 static Lisp_Object Vmode_line_unwind_vector;
11637 static Lisp_Object
11638 format_mode_line_unwind_data (struct frame *target_frame,
11639 struct buffer *obuf,
11640 Lisp_Object owin,
11641 bool save_proptrans)
11643 Lisp_Object vector, tmp;
11645 /* Reduce consing by keeping one vector in
11646 Vwith_echo_area_save_vector. */
11647 vector = Vmode_line_unwind_vector;
11648 Vmode_line_unwind_vector = Qnil;
11650 if (NILP (vector))
11651 vector = Fmake_vector (make_number (10), Qnil);
11653 ASET (vector, 0, make_number (mode_line_target));
11654 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11655 ASET (vector, 2, mode_line_string_list);
11656 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11657 ASET (vector, 4, mode_line_string_face);
11658 ASET (vector, 5, mode_line_string_face_prop);
11660 if (obuf)
11661 XSETBUFFER (tmp, obuf);
11662 else
11663 tmp = Qnil;
11664 ASET (vector, 6, tmp);
11665 ASET (vector, 7, owin);
11666 if (target_frame)
11668 /* Similarly to `with-selected-window', if the operation selects
11669 a window on another frame, we must restore that frame's
11670 selected window, and (for a tty) the top-frame. */
11671 ASET (vector, 8, target_frame->selected_window);
11672 if (FRAME_TERMCAP_P (target_frame))
11673 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11676 return vector;
11679 static void
11680 unwind_format_mode_line (Lisp_Object vector)
11682 Lisp_Object old_window = AREF (vector, 7);
11683 Lisp_Object target_frame_window = AREF (vector, 8);
11684 Lisp_Object old_top_frame = AREF (vector, 9);
11686 mode_line_target = XINT (AREF (vector, 0));
11687 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11688 mode_line_string_list = AREF (vector, 2);
11689 if (! EQ (AREF (vector, 3), Qt))
11690 mode_line_proptrans_alist = AREF (vector, 3);
11691 mode_line_string_face = AREF (vector, 4);
11692 mode_line_string_face_prop = AREF (vector, 5);
11694 /* Select window before buffer, since it may change the buffer. */
11695 if (!NILP (old_window))
11697 /* If the operation that we are unwinding had selected a window
11698 on a different frame, reset its frame-selected-window. For a
11699 text terminal, reset its top-frame if necessary. */
11700 if (!NILP (target_frame_window))
11702 Lisp_Object frame
11703 = WINDOW_FRAME (XWINDOW (target_frame_window));
11705 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11706 Fselect_window (target_frame_window, Qt);
11708 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11709 Fselect_frame (old_top_frame, Qt);
11712 Fselect_window (old_window, Qt);
11715 if (!NILP (AREF (vector, 6)))
11717 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11718 ASET (vector, 6, Qnil);
11721 Vmode_line_unwind_vector = vector;
11725 /* Store a single character C for the frame title in mode_line_noprop_buf.
11726 Re-allocate mode_line_noprop_buf if necessary. */
11728 static void
11729 store_mode_line_noprop_char (char c)
11731 /* If output position has reached the end of the allocated buffer,
11732 increase the buffer's size. */
11733 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11735 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11736 ptrdiff_t size = len;
11737 mode_line_noprop_buf =
11738 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11739 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11740 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11743 *mode_line_noprop_ptr++ = c;
11747 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11748 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11749 characters that yield more columns than PRECISION; PRECISION <= 0
11750 means copy the whole string. Pad with spaces until FIELD_WIDTH
11751 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11752 pad. Called from display_mode_element when it is used to build a
11753 frame title. */
11755 static int
11756 store_mode_line_noprop (const char *string, int field_width, int precision)
11758 const unsigned char *str = (const unsigned char *) string;
11759 int n = 0;
11760 ptrdiff_t dummy, nbytes;
11762 /* Copy at most PRECISION chars from STR. */
11763 nbytes = strlen (string);
11764 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11765 while (nbytes--)
11766 store_mode_line_noprop_char (*str++);
11768 /* Fill up with spaces until FIELD_WIDTH reached. */
11769 while (field_width > 0
11770 && n < field_width)
11772 store_mode_line_noprop_char (' ');
11773 ++n;
11776 return n;
11779 /***********************************************************************
11780 Frame Titles
11781 ***********************************************************************/
11783 #ifdef HAVE_WINDOW_SYSTEM
11785 /* Set the title of FRAME, if it has changed. The title format is
11786 Vicon_title_format if FRAME is iconified, otherwise it is
11787 frame_title_format. */
11789 static void
11790 x_consider_frame_title (Lisp_Object frame)
11792 struct frame *f = XFRAME (frame);
11794 if ((FRAME_WINDOW_P (f)
11795 || FRAME_MINIBUF_ONLY_P (f)
11796 || f->explicit_name)
11797 && NILP (Fframe_parameter (frame, Qtooltip)))
11799 /* Do we have more than one visible frame on this X display? */
11800 Lisp_Object tail, other_frame, fmt;
11801 ptrdiff_t title_start;
11802 char *title;
11803 ptrdiff_t len;
11804 struct it it;
11805 ptrdiff_t count = SPECPDL_INDEX ();
11807 FOR_EACH_FRAME (tail, other_frame)
11809 struct frame *tf = XFRAME (other_frame);
11811 if (tf != f
11812 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11813 && !FRAME_MINIBUF_ONLY_P (tf)
11814 && !EQ (other_frame, tip_frame)
11815 && !FRAME_PARENT_FRAME (tf)
11816 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11817 break;
11820 /* Set global variable indicating that multiple frames exist. */
11821 multiple_frames = CONSP (tail);
11823 /* Switch to the buffer of selected window of the frame. Set up
11824 mode_line_target so that display_mode_element will output into
11825 mode_line_noprop_buf; then display the title. */
11826 record_unwind_protect (unwind_format_mode_line,
11827 format_mode_line_unwind_data
11828 (f, current_buffer, selected_window, false));
11829 /* select-frame calls resize_mini_window, which could resize the
11830 mini-window and by that undo the effect of this redisplay
11831 cycle wrt minibuffer and echo-area display. Binding
11832 inhibit-redisplay to t makes the call to resize_mini_window a
11833 no-op, thus avoiding the adverse side effects. */
11834 specbind (Qinhibit_redisplay, Qt);
11836 Fselect_window (f->selected_window, Qt);
11837 set_buffer_internal_1
11838 (XBUFFER (XWINDOW (f->selected_window)->contents));
11839 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11841 mode_line_target = MODE_LINE_TITLE;
11842 title_start = MODE_LINE_NOPROP_LEN (0);
11843 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11844 NULL, DEFAULT_FACE_ID);
11845 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11846 len = MODE_LINE_NOPROP_LEN (title_start);
11847 title = mode_line_noprop_buf + title_start;
11848 unbind_to (count, Qnil);
11850 /* Set the title only if it's changed. This avoids consing in
11851 the common case where it hasn't. (If it turns out that we've
11852 already wasted too much time by walking through the list with
11853 display_mode_element, then we might need to optimize at a
11854 higher level than this.) */
11855 if (! STRINGP (f->name)
11856 || SBYTES (f->name) != len
11857 || memcmp (title, SDATA (f->name), len) != 0)
11858 x_implicitly_set_name (f, make_string (title, len), Qnil);
11862 #endif /* not HAVE_WINDOW_SYSTEM */
11865 /***********************************************************************
11866 Menu Bars
11867 ***********************************************************************/
11869 /* True if we will not redisplay all visible windows. */
11870 #define REDISPLAY_SOME_P() \
11871 ((windows_or_buffers_changed == 0 \
11872 || windows_or_buffers_changed == REDISPLAY_SOME) \
11873 && (update_mode_lines == 0 \
11874 || update_mode_lines == REDISPLAY_SOME))
11876 /* Prepare for redisplay by updating menu-bar item lists when
11877 appropriate. This can call eval. */
11879 static void
11880 prepare_menu_bars (void)
11882 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11883 bool some_windows = REDISPLAY_SOME_P ();
11884 Lisp_Object tooltip_frame;
11886 #ifdef HAVE_WINDOW_SYSTEM
11887 tooltip_frame = tip_frame;
11888 #else
11889 tooltip_frame = Qnil;
11890 #endif
11892 if (FUNCTIONP (Vpre_redisplay_function))
11894 Lisp_Object windows = all_windows ? Qt : Qnil;
11895 if (all_windows && some_windows)
11897 Lisp_Object ws = window_list ();
11898 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11900 Lisp_Object this = XCAR (ws);
11901 struct window *w = XWINDOW (this);
11902 if (w->redisplay
11903 || XFRAME (w->frame)->redisplay
11904 || XBUFFER (w->contents)->text->redisplay)
11906 windows = Fcons (this, windows);
11910 safe__call1 (true, Vpre_redisplay_function, windows);
11913 /* Update all frame titles based on their buffer names, etc. We do
11914 this before the menu bars so that the buffer-menu will show the
11915 up-to-date frame titles. */
11916 #ifdef HAVE_WINDOW_SYSTEM
11917 if (all_windows)
11919 Lisp_Object tail, frame;
11921 FOR_EACH_FRAME (tail, frame)
11923 struct frame *f = XFRAME (frame);
11924 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11925 if (some_windows
11926 && !f->redisplay
11927 && !w->redisplay
11928 && !XBUFFER (w->contents)->text->redisplay)
11929 continue;
11931 if (!EQ (frame, tooltip_frame)
11932 && !FRAME_PARENT_FRAME (f)
11933 && (FRAME_ICONIFIED_P (f)
11934 || FRAME_VISIBLE_P (f) == 1
11935 /* Exclude TTY frames that are obscured because they
11936 are not the top frame on their console. This is
11937 because x_consider_frame_title actually switches
11938 to the frame, which for TTY frames means it is
11939 marked as garbaged, and will be completely
11940 redrawn on the next redisplay cycle. This causes
11941 TTY frames to be completely redrawn, when there
11942 are more than one of them, even though nothing
11943 should be changed on display. */
11944 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11945 x_consider_frame_title (frame);
11948 #endif /* HAVE_WINDOW_SYSTEM */
11950 /* Update the menu bar item lists, if appropriate. This has to be
11951 done before any actual redisplay or generation of display lines. */
11953 if (all_windows)
11955 Lisp_Object tail, frame;
11956 ptrdiff_t count = SPECPDL_INDEX ();
11957 /* True means that update_menu_bar has run its hooks
11958 so any further calls to update_menu_bar shouldn't do so again. */
11959 bool menu_bar_hooks_run = false;
11961 record_unwind_save_match_data ();
11963 FOR_EACH_FRAME (tail, frame)
11965 struct frame *f = XFRAME (frame);
11966 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11968 /* Ignore tooltip frame. */
11969 if (EQ (frame, tooltip_frame))
11970 continue;
11972 if (some_windows
11973 && !f->redisplay
11974 && !w->redisplay
11975 && !XBUFFER (w->contents)->text->redisplay)
11976 continue;
11978 run_window_size_change_functions (frame);
11980 if (FRAME_PARENT_FRAME (f))
11981 continue;
11983 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11984 #ifdef HAVE_WINDOW_SYSTEM
11985 update_tool_bar (f, false);
11986 #endif
11989 unbind_to (count, Qnil);
11991 else
11993 struct frame *sf = SELECTED_FRAME ();
11994 update_menu_bar (sf, true, false);
11995 #ifdef HAVE_WINDOW_SYSTEM
11996 update_tool_bar (sf, true);
11997 #endif
12002 /* Update the menu bar item list for frame F. This has to be done
12003 before we start to fill in any display lines, because it can call
12004 eval.
12006 If SAVE_MATCH_DATA, we must save and restore it here.
12008 If HOOKS_RUN, a previous call to update_menu_bar
12009 already ran the menu bar hooks for this redisplay, so there
12010 is no need to run them again. The return value is the
12011 updated value of this flag, to pass to the next call. */
12013 static bool
12014 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12016 Lisp_Object window;
12017 struct window *w;
12019 /* If called recursively during a menu update, do nothing. This can
12020 happen when, for instance, an activate-menubar-hook causes a
12021 redisplay. */
12022 if (inhibit_menubar_update)
12023 return hooks_run;
12025 window = FRAME_SELECTED_WINDOW (f);
12026 w = XWINDOW (window);
12028 if (FRAME_WINDOW_P (f)
12030 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12031 || defined (HAVE_NS) || defined (USE_GTK)
12032 FRAME_EXTERNAL_MENU_BAR (f)
12033 #else
12034 FRAME_MENU_BAR_LINES (f) > 0
12035 #endif
12036 : FRAME_MENU_BAR_LINES (f) > 0)
12038 /* If the user has switched buffers or windows, we need to
12039 recompute to reflect the new bindings. But we'll
12040 recompute when update_mode_lines is set too; that means
12041 that people can use force-mode-line-update to request
12042 that the menu bar be recomputed. The adverse effect on
12043 the rest of the redisplay algorithm is about the same as
12044 windows_or_buffers_changed anyway. */
12045 if (windows_or_buffers_changed
12046 /* This used to test w->update_mode_line, but we believe
12047 there is no need to recompute the menu in that case. */
12048 || update_mode_lines
12049 || window_buffer_changed (w))
12051 struct buffer *prev = current_buffer;
12052 ptrdiff_t count = SPECPDL_INDEX ();
12054 specbind (Qinhibit_menubar_update, Qt);
12056 set_buffer_internal_1 (XBUFFER (w->contents));
12057 if (save_match_data)
12058 record_unwind_save_match_data ();
12059 if (NILP (Voverriding_local_map_menu_flag))
12061 specbind (Qoverriding_terminal_local_map, Qnil);
12062 specbind (Qoverriding_local_map, Qnil);
12065 if (!hooks_run)
12067 /* Run the Lucid hook. */
12068 safe_run_hooks (Qactivate_menubar_hook);
12070 /* If it has changed current-menubar from previous value,
12071 really recompute the menu-bar from the value. */
12072 if (! NILP (Vlucid_menu_bar_dirty_flag))
12073 call0 (Qrecompute_lucid_menubar);
12075 safe_run_hooks (Qmenu_bar_update_hook);
12077 hooks_run = true;
12080 XSETFRAME (Vmenu_updating_frame, f);
12081 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12083 /* Redisplay the menu bar in case we changed it. */
12084 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12085 || defined (HAVE_NS) || defined (USE_GTK)
12086 if (FRAME_WINDOW_P (f))
12088 #if defined (HAVE_NS)
12089 /* All frames on Mac OS share the same menubar. So only
12090 the selected frame should be allowed to set it. */
12091 if (f == SELECTED_FRAME ())
12092 #endif
12093 set_frame_menubar (f, false, false);
12095 else
12096 /* On a terminal screen, the menu bar is an ordinary screen
12097 line, and this makes it get updated. */
12098 w->update_mode_line = true;
12099 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12100 /* In the non-toolkit version, the menu bar is an ordinary screen
12101 line, and this makes it get updated. */
12102 w->update_mode_line = true;
12103 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12105 unbind_to (count, Qnil);
12106 set_buffer_internal_1 (prev);
12110 return hooks_run;
12113 /***********************************************************************
12114 Tool-bars
12115 ***********************************************************************/
12117 #ifdef HAVE_WINDOW_SYSTEM
12119 /* Select `frame' temporarily without running all the code in
12120 do_switch_frame.
12121 FIXME: Maybe do_switch_frame should be trimmed down similarly
12122 when `norecord' is set. */
12123 static void
12124 fast_set_selected_frame (Lisp_Object frame)
12126 if (!EQ (selected_frame, frame))
12128 selected_frame = frame;
12129 selected_window = XFRAME (frame)->selected_window;
12133 /* Update the tool-bar item list for frame F. This has to be done
12134 before we start to fill in any display lines. Called from
12135 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12136 and restore it here. */
12138 static void
12139 update_tool_bar (struct frame *f, bool save_match_data)
12141 #if defined (USE_GTK) || defined (HAVE_NS)
12142 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12143 #else
12144 bool do_update = (WINDOWP (f->tool_bar_window)
12145 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12146 #endif
12148 if (do_update)
12150 Lisp_Object window;
12151 struct window *w;
12153 window = FRAME_SELECTED_WINDOW (f);
12154 w = XWINDOW (window);
12156 /* If the user has switched buffers or windows, we need to
12157 recompute to reflect the new bindings. But we'll
12158 recompute when update_mode_lines is set too; that means
12159 that people can use force-mode-line-update to request
12160 that the menu bar be recomputed. The adverse effect on
12161 the rest of the redisplay algorithm is about the same as
12162 windows_or_buffers_changed anyway. */
12163 if (windows_or_buffers_changed
12164 || w->update_mode_line
12165 || update_mode_lines
12166 || window_buffer_changed (w))
12168 struct buffer *prev = current_buffer;
12169 ptrdiff_t count = SPECPDL_INDEX ();
12170 Lisp_Object frame, new_tool_bar;
12171 int new_n_tool_bar;
12173 /* Set current_buffer to the buffer of the selected
12174 window of the frame, so that we get the right local
12175 keymaps. */
12176 set_buffer_internal_1 (XBUFFER (w->contents));
12178 /* Save match data, if we must. */
12179 if (save_match_data)
12180 record_unwind_save_match_data ();
12182 /* Make sure that we don't accidentally use bogus keymaps. */
12183 if (NILP (Voverriding_local_map_menu_flag))
12185 specbind (Qoverriding_terminal_local_map, Qnil);
12186 specbind (Qoverriding_local_map, Qnil);
12189 /* We must temporarily set the selected frame to this frame
12190 before calling tool_bar_items, because the calculation of
12191 the tool-bar keymap uses the selected frame (see
12192 `tool-bar-make-keymap' in tool-bar.el). */
12193 eassert (EQ (selected_window,
12194 /* Since we only explicitly preserve selected_frame,
12195 check that selected_window would be redundant. */
12196 XFRAME (selected_frame)->selected_window));
12197 record_unwind_protect (fast_set_selected_frame, selected_frame);
12198 XSETFRAME (frame, f);
12199 fast_set_selected_frame (frame);
12201 /* Build desired tool-bar items from keymaps. */
12202 new_tool_bar
12203 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12204 &new_n_tool_bar);
12206 /* Redisplay the tool-bar if we changed it. */
12207 if (new_n_tool_bar != f->n_tool_bar_items
12208 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12210 /* Redisplay that happens asynchronously due to an expose event
12211 may access f->tool_bar_items. Make sure we update both
12212 variables within BLOCK_INPUT so no such event interrupts. */
12213 block_input ();
12214 fset_tool_bar_items (f, new_tool_bar);
12215 f->n_tool_bar_items = new_n_tool_bar;
12216 w->update_mode_line = true;
12217 unblock_input ();
12220 unbind_to (count, Qnil);
12221 set_buffer_internal_1 (prev);
12226 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12228 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12229 F's desired tool-bar contents. F->tool_bar_items must have
12230 been set up previously by calling prepare_menu_bars. */
12232 static void
12233 build_desired_tool_bar_string (struct frame *f)
12235 int i, size, size_needed;
12236 Lisp_Object image, plist;
12238 image = plist = Qnil;
12240 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12241 Otherwise, make a new string. */
12243 /* The size of the string we might be able to reuse. */
12244 size = (STRINGP (f->desired_tool_bar_string)
12245 ? SCHARS (f->desired_tool_bar_string)
12246 : 0);
12248 /* We need one space in the string for each image. */
12249 size_needed = f->n_tool_bar_items;
12251 /* Reuse f->desired_tool_bar_string, if possible. */
12252 if (size < size_needed || NILP (f->desired_tool_bar_string))
12253 fset_desired_tool_bar_string
12254 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12255 else
12257 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12258 Fremove_text_properties (make_number (0), make_number (size),
12259 props, f->desired_tool_bar_string);
12262 /* Put a `display' property on the string for the images to display,
12263 put a `menu_item' property on tool-bar items with a value that
12264 is the index of the item in F's tool-bar item vector. */
12265 for (i = 0; i < f->n_tool_bar_items; ++i)
12267 #define PROP(IDX) \
12268 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12270 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12271 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12272 int hmargin, vmargin, relief, idx, end;
12274 /* If image is a vector, choose the image according to the
12275 button state. */
12276 image = PROP (TOOL_BAR_ITEM_IMAGES);
12277 if (VECTORP (image))
12279 if (enabled_p)
12280 idx = (selected_p
12281 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12282 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12283 else
12284 idx = (selected_p
12285 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12286 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12288 eassert (ASIZE (image) >= idx);
12289 image = AREF (image, idx);
12291 else
12292 idx = -1;
12294 /* Ignore invalid image specifications. */
12295 if (!valid_image_p (image))
12296 continue;
12298 /* Display the tool-bar button pressed, or depressed. */
12299 plist = Fcopy_sequence (XCDR (image));
12301 /* Compute margin and relief to draw. */
12302 relief = (tool_bar_button_relief >= 0
12303 ? tool_bar_button_relief
12304 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12305 hmargin = vmargin = relief;
12307 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12308 INT_MAX - max (hmargin, vmargin)))
12310 hmargin += XFASTINT (Vtool_bar_button_margin);
12311 vmargin += XFASTINT (Vtool_bar_button_margin);
12313 else if (CONSP (Vtool_bar_button_margin))
12315 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12316 INT_MAX - hmargin))
12317 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12319 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12320 INT_MAX - vmargin))
12321 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12324 if (auto_raise_tool_bar_buttons_p)
12326 /* Add a `:relief' property to the image spec if the item is
12327 selected. */
12328 if (selected_p)
12330 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12331 hmargin -= relief;
12332 vmargin -= relief;
12335 else
12337 /* If image is selected, display it pressed, i.e. with a
12338 negative relief. If it's not selected, display it with a
12339 raised relief. */
12340 plist = Fplist_put (plist, QCrelief,
12341 (selected_p
12342 ? make_number (-relief)
12343 : make_number (relief)));
12344 hmargin -= relief;
12345 vmargin -= relief;
12348 /* Put a margin around the image. */
12349 if (hmargin || vmargin)
12351 if (hmargin == vmargin)
12352 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12353 else
12354 plist = Fplist_put (plist, QCmargin,
12355 Fcons (make_number (hmargin),
12356 make_number (vmargin)));
12359 /* If button is not enabled, and we don't have special images
12360 for the disabled state, make the image appear disabled by
12361 applying an appropriate algorithm to it. */
12362 if (!enabled_p && idx < 0)
12363 plist = Fplist_put (plist, QCconversion, Qdisabled);
12365 /* Put a `display' text property on the string for the image to
12366 display. Put a `menu-item' property on the string that gives
12367 the start of this item's properties in the tool-bar items
12368 vector. */
12369 image = Fcons (Qimage, plist);
12370 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12371 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12373 /* Let the last image hide all remaining spaces in the tool bar
12374 string. The string can be longer than needed when we reuse a
12375 previous string. */
12376 if (i + 1 == f->n_tool_bar_items)
12377 end = SCHARS (f->desired_tool_bar_string);
12378 else
12379 end = i + 1;
12380 Fadd_text_properties (make_number (i), make_number (end),
12381 props, f->desired_tool_bar_string);
12382 #undef PROP
12387 /* Display one line of the tool-bar of frame IT->f.
12389 HEIGHT specifies the desired height of the tool-bar line.
12390 If the actual height of the glyph row is less than HEIGHT, the
12391 row's height is increased to HEIGHT, and the icons are centered
12392 vertically in the new height.
12394 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12395 count a final empty row in case the tool-bar width exactly matches
12396 the window width.
12399 static void
12400 display_tool_bar_line (struct it *it, int height)
12402 struct glyph_row *row = it->glyph_row;
12403 int max_x = it->last_visible_x;
12404 struct glyph *last;
12406 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12407 clear_glyph_row (row);
12408 row->enabled_p = true;
12409 row->y = it->current_y;
12411 /* Note that this isn't made use of if the face hasn't a box,
12412 so there's no need to check the face here. */
12413 it->start_of_box_run_p = true;
12415 while (it->current_x < max_x)
12417 int x, n_glyphs_before, i, nglyphs;
12418 struct it it_before;
12420 /* Get the next display element. */
12421 if (!get_next_display_element (it))
12423 /* Don't count empty row if we are counting needed tool-bar lines. */
12424 if (height < 0 && !it->hpos)
12425 return;
12426 break;
12429 /* Produce glyphs. */
12430 n_glyphs_before = row->used[TEXT_AREA];
12431 it_before = *it;
12433 PRODUCE_GLYPHS (it);
12435 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12436 i = 0;
12437 x = it_before.current_x;
12438 while (i < nglyphs)
12440 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12442 if (x + glyph->pixel_width > max_x)
12444 /* Glyph doesn't fit on line. Backtrack. */
12445 row->used[TEXT_AREA] = n_glyphs_before;
12446 *it = it_before;
12447 /* If this is the only glyph on this line, it will never fit on the
12448 tool-bar, so skip it. But ensure there is at least one glyph,
12449 so we don't accidentally disable the tool-bar. */
12450 if (n_glyphs_before == 0
12451 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12452 break;
12453 goto out;
12456 ++it->hpos;
12457 x += glyph->pixel_width;
12458 ++i;
12461 /* Stop at line end. */
12462 if (ITERATOR_AT_END_OF_LINE_P (it))
12463 break;
12465 set_iterator_to_next (it, true);
12468 out:;
12470 row->displays_text_p = row->used[TEXT_AREA] != 0;
12472 /* Use default face for the border below the tool bar.
12474 FIXME: When auto-resize-tool-bars is grow-only, there is
12475 no additional border below the possibly empty tool-bar lines.
12476 So to make the extra empty lines look "normal", we have to
12477 use the tool-bar face for the border too. */
12478 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12479 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12480 it->face_id = DEFAULT_FACE_ID;
12482 extend_face_to_end_of_line (it);
12483 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12484 last->right_box_line_p = true;
12485 if (last == row->glyphs[TEXT_AREA])
12486 last->left_box_line_p = true;
12488 /* Make line the desired height and center it vertically. */
12489 if ((height -= it->max_ascent + it->max_descent) > 0)
12491 /* Don't add more than one line height. */
12492 height %= FRAME_LINE_HEIGHT (it->f);
12493 it->max_ascent += height / 2;
12494 it->max_descent += (height + 1) / 2;
12497 compute_line_metrics (it);
12499 /* If line is empty, make it occupy the rest of the tool-bar. */
12500 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12502 row->height = row->phys_height = it->last_visible_y - row->y;
12503 row->visible_height = row->height;
12504 row->ascent = row->phys_ascent = 0;
12505 row->extra_line_spacing = 0;
12508 row->full_width_p = true;
12509 row->continued_p = false;
12510 row->truncated_on_left_p = false;
12511 row->truncated_on_right_p = false;
12513 it->current_x = it->hpos = 0;
12514 it->current_y += row->height;
12515 ++it->vpos;
12516 ++it->glyph_row;
12520 /* Value is the number of pixels needed to make all tool-bar items of
12521 frame F visible. The actual number of glyph rows needed is
12522 returned in *N_ROWS if non-NULL. */
12523 static int
12524 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12526 struct window *w = XWINDOW (f->tool_bar_window);
12527 struct it it;
12528 /* tool_bar_height is called from redisplay_tool_bar after building
12529 the desired matrix, so use (unused) mode-line row as temporary row to
12530 avoid destroying the first tool-bar row. */
12531 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12533 /* Initialize an iterator for iteration over
12534 F->desired_tool_bar_string in the tool-bar window of frame F. */
12535 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12536 temp_row->reversed_p = false;
12537 it.first_visible_x = 0;
12538 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12539 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12540 it.paragraph_embedding = L2R;
12542 while (!ITERATOR_AT_END_P (&it))
12544 clear_glyph_row (temp_row);
12545 it.glyph_row = temp_row;
12546 display_tool_bar_line (&it, -1);
12548 clear_glyph_row (temp_row);
12550 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12551 if (n_rows)
12552 *n_rows = it.vpos > 0 ? it.vpos : -1;
12554 if (pixelwise)
12555 return it.current_y;
12556 else
12557 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12560 #endif /* !USE_GTK && !HAVE_NS */
12562 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12563 0, 2, 0,
12564 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12565 If FRAME is nil or omitted, use the selected frame. Optional argument
12566 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12567 (Lisp_Object frame, Lisp_Object pixelwise)
12569 int height = 0;
12571 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12572 struct frame *f = decode_any_frame (frame);
12574 if (WINDOWP (f->tool_bar_window)
12575 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12577 update_tool_bar (f, true);
12578 if (f->n_tool_bar_items)
12580 build_desired_tool_bar_string (f);
12581 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12584 #endif
12586 return make_number (height);
12590 /* Display the tool-bar of frame F. Value is true if tool-bar's
12591 height should be changed. */
12592 static bool
12593 redisplay_tool_bar (struct frame *f)
12595 f->tool_bar_redisplayed = true;
12596 #if defined (USE_GTK) || defined (HAVE_NS)
12598 if (FRAME_EXTERNAL_TOOL_BAR (f))
12599 update_frame_tool_bar (f);
12600 return false;
12602 #else /* !USE_GTK && !HAVE_NS */
12604 struct window *w;
12605 struct it it;
12606 struct glyph_row *row;
12608 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12609 do anything. This means you must start with tool-bar-lines
12610 non-zero to get the auto-sizing effect. Or in other words, you
12611 can turn off tool-bars by specifying tool-bar-lines zero. */
12612 if (!WINDOWP (f->tool_bar_window)
12613 || (w = XWINDOW (f->tool_bar_window),
12614 WINDOW_TOTAL_LINES (w) == 0))
12615 return false;
12617 /* Set up an iterator for the tool-bar window. */
12618 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12619 it.first_visible_x = 0;
12620 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12621 row = it.glyph_row;
12622 row->reversed_p = false;
12624 /* Build a string that represents the contents of the tool-bar. */
12625 build_desired_tool_bar_string (f);
12626 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12627 /* FIXME: This should be controlled by a user option. But it
12628 doesn't make sense to have an R2L tool bar if the menu bar cannot
12629 be drawn also R2L, and making the menu bar R2L is tricky due
12630 toolkit-specific code that implements it. If an R2L tool bar is
12631 ever supported, display_tool_bar_line should also be augmented to
12632 call unproduce_glyphs like display_line and display_string
12633 do. */
12634 it.paragraph_embedding = L2R;
12636 if (f->n_tool_bar_rows == 0)
12638 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12640 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12642 x_change_tool_bar_height (f, new_height);
12643 frame_default_tool_bar_height = new_height;
12644 /* Always do that now. */
12645 clear_glyph_matrix (w->desired_matrix);
12646 f->fonts_changed = true;
12647 return true;
12651 /* Display as many lines as needed to display all tool-bar items. */
12653 if (f->n_tool_bar_rows > 0)
12655 int border, rows, height, extra;
12657 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12658 border = XINT (Vtool_bar_border);
12659 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12660 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12661 else if (EQ (Vtool_bar_border, Qborder_width))
12662 border = f->border_width;
12663 else
12664 border = 0;
12665 if (border < 0)
12666 border = 0;
12668 rows = f->n_tool_bar_rows;
12669 height = max (1, (it.last_visible_y - border) / rows);
12670 extra = it.last_visible_y - border - height * rows;
12672 while (it.current_y < it.last_visible_y)
12674 int h = 0;
12675 if (extra > 0 && rows-- > 0)
12677 h = (extra + rows - 1) / rows;
12678 extra -= h;
12680 display_tool_bar_line (&it, height + h);
12683 else
12685 while (it.current_y < it.last_visible_y)
12686 display_tool_bar_line (&it, 0);
12689 /* It doesn't make much sense to try scrolling in the tool-bar
12690 window, so don't do it. */
12691 w->desired_matrix->no_scrolling_p = true;
12692 w->must_be_updated_p = true;
12694 if (!NILP (Vauto_resize_tool_bars))
12696 bool change_height_p = true;
12698 /* If we couldn't display everything, change the tool-bar's
12699 height if there is room for more. */
12700 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12701 change_height_p = true;
12703 /* We subtract 1 because display_tool_bar_line advances the
12704 glyph_row pointer before returning to its caller. We want to
12705 examine the last glyph row produced by
12706 display_tool_bar_line. */
12707 row = it.glyph_row - 1;
12709 /* If there are blank lines at the end, except for a partially
12710 visible blank line at the end that is smaller than
12711 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12712 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12713 && row->height >= FRAME_LINE_HEIGHT (f))
12714 change_height_p = true;
12716 /* If row displays tool-bar items, but is partially visible,
12717 change the tool-bar's height. */
12718 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12719 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12720 change_height_p = true;
12722 /* Resize windows as needed by changing the `tool-bar-lines'
12723 frame parameter. */
12724 if (change_height_p)
12726 int nrows;
12727 int new_height = tool_bar_height (f, &nrows, true);
12729 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12730 && !f->minimize_tool_bar_window_p)
12731 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12732 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12733 f->minimize_tool_bar_window_p = false;
12735 if (change_height_p)
12737 x_change_tool_bar_height (f, new_height);
12738 frame_default_tool_bar_height = new_height;
12739 clear_glyph_matrix (w->desired_matrix);
12740 f->n_tool_bar_rows = nrows;
12741 f->fonts_changed = true;
12743 return true;
12748 f->minimize_tool_bar_window_p = false;
12749 return false;
12751 #endif /* USE_GTK || HAVE_NS */
12754 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12756 /* Get information about the tool-bar item which is displayed in GLYPH
12757 on frame F. Return in *PROP_IDX the index where tool-bar item
12758 properties start in F->tool_bar_items. Value is false if
12759 GLYPH doesn't display a tool-bar item. */
12761 static bool
12762 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12764 Lisp_Object prop;
12765 int charpos;
12767 /* This function can be called asynchronously, which means we must
12768 exclude any possibility that Fget_text_property signals an
12769 error. */
12770 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12771 charpos = max (0, charpos);
12773 /* Get the text property `menu-item' at pos. The value of that
12774 property is the start index of this item's properties in
12775 F->tool_bar_items. */
12776 prop = Fget_text_property (make_number (charpos),
12777 Qmenu_item, f->current_tool_bar_string);
12778 if (! INTEGERP (prop))
12779 return false;
12780 *prop_idx = XINT (prop);
12781 return true;
12785 /* Get information about the tool-bar item at position X/Y on frame F.
12786 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12787 the current matrix of the tool-bar window of F, or NULL if not
12788 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12789 item in F->tool_bar_items. Value is
12791 -1 if X/Y is not on a tool-bar item
12792 0 if X/Y is on the same item that was highlighted before.
12793 1 otherwise. */
12795 static int
12796 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12797 int *hpos, int *vpos, int *prop_idx)
12799 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12800 struct window *w = XWINDOW (f->tool_bar_window);
12801 int area;
12803 /* Find the glyph under X/Y. */
12804 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12805 if (*glyph == NULL)
12806 return -1;
12808 /* Get the start of this tool-bar item's properties in
12809 f->tool_bar_items. */
12810 if (!tool_bar_item_info (f, *glyph, prop_idx))
12811 return -1;
12813 /* Is mouse on the highlighted item? */
12814 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12815 && *vpos >= hlinfo->mouse_face_beg_row
12816 && *vpos <= hlinfo->mouse_face_end_row
12817 && (*vpos > hlinfo->mouse_face_beg_row
12818 || *hpos >= hlinfo->mouse_face_beg_col)
12819 && (*vpos < hlinfo->mouse_face_end_row
12820 || *hpos < hlinfo->mouse_face_end_col
12821 || hlinfo->mouse_face_past_end))
12822 return 0;
12824 return 1;
12828 /* EXPORT:
12829 Handle mouse button event on the tool-bar of frame F, at
12830 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12831 false for button release. MODIFIERS is event modifiers for button
12832 release. */
12834 void
12835 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12836 int modifiers)
12838 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12839 struct window *w = XWINDOW (f->tool_bar_window);
12840 int hpos, vpos, prop_idx;
12841 struct glyph *glyph;
12842 Lisp_Object enabled_p;
12843 int ts;
12845 /* If not on the highlighted tool-bar item, and mouse-highlight is
12846 non-nil, return. This is so we generate the tool-bar button
12847 click only when the mouse button is released on the same item as
12848 where it was pressed. However, when mouse-highlight is disabled,
12849 generate the click when the button is released regardless of the
12850 highlight, since tool-bar items are not highlighted in that
12851 case. */
12852 frame_to_window_pixel_xy (w, &x, &y);
12853 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12854 if (ts == -1
12855 || (ts != 0 && !NILP (Vmouse_highlight)))
12856 return;
12858 /* When mouse-highlight is off, generate the click for the item
12859 where the button was pressed, disregarding where it was
12860 released. */
12861 if (NILP (Vmouse_highlight) && !down_p)
12862 prop_idx = f->last_tool_bar_item;
12864 /* If item is disabled, do nothing. */
12865 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12866 if (NILP (enabled_p))
12867 return;
12869 if (down_p)
12871 /* Show item in pressed state. */
12872 if (!NILP (Vmouse_highlight))
12873 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12874 f->last_tool_bar_item = prop_idx;
12876 else
12878 Lisp_Object key, frame;
12879 struct input_event event;
12880 EVENT_INIT (event);
12882 /* Show item in released state. */
12883 if (!NILP (Vmouse_highlight))
12884 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12886 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12888 XSETFRAME (frame, f);
12889 event.kind = TOOL_BAR_EVENT;
12890 event.frame_or_window = frame;
12891 event.arg = frame;
12892 kbd_buffer_store_event (&event);
12894 event.kind = TOOL_BAR_EVENT;
12895 event.frame_or_window = frame;
12896 event.arg = key;
12897 event.modifiers = modifiers;
12898 kbd_buffer_store_event (&event);
12899 f->last_tool_bar_item = -1;
12904 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12905 tool-bar window-relative coordinates X/Y. Called from
12906 note_mouse_highlight. */
12908 static void
12909 note_tool_bar_highlight (struct frame *f, int x, int y)
12911 Lisp_Object window = f->tool_bar_window;
12912 struct window *w = XWINDOW (window);
12913 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12914 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12915 int hpos, vpos;
12916 struct glyph *glyph;
12917 struct glyph_row *row;
12918 int i;
12919 Lisp_Object enabled_p;
12920 int prop_idx;
12921 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12922 bool mouse_down_p;
12923 int rc;
12925 /* Function note_mouse_highlight is called with negative X/Y
12926 values when mouse moves outside of the frame. */
12927 if (x <= 0 || y <= 0)
12929 clear_mouse_face (hlinfo);
12930 return;
12933 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12934 if (rc < 0)
12936 /* Not on tool-bar item. */
12937 clear_mouse_face (hlinfo);
12938 return;
12940 else if (rc == 0)
12941 /* On same tool-bar item as before. */
12942 goto set_help_echo;
12944 clear_mouse_face (hlinfo);
12946 /* Mouse is down, but on different tool-bar item? */
12947 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12948 && f == dpyinfo->last_mouse_frame);
12950 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12951 return;
12953 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12955 /* If tool-bar item is not enabled, don't highlight it. */
12956 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12957 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12959 /* Compute the x-position of the glyph. In front and past the
12960 image is a space. We include this in the highlighted area. */
12961 row = MATRIX_ROW (w->current_matrix, vpos);
12962 for (i = x = 0; i < hpos; ++i)
12963 x += row->glyphs[TEXT_AREA][i].pixel_width;
12965 /* Record this as the current active region. */
12966 hlinfo->mouse_face_beg_col = hpos;
12967 hlinfo->mouse_face_beg_row = vpos;
12968 hlinfo->mouse_face_beg_x = x;
12969 hlinfo->mouse_face_past_end = false;
12971 hlinfo->mouse_face_end_col = hpos + 1;
12972 hlinfo->mouse_face_end_row = vpos;
12973 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12974 hlinfo->mouse_face_window = window;
12975 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12977 /* Display it as active. */
12978 show_mouse_face (hlinfo, draw);
12981 set_help_echo:
12983 /* Set help_echo_string to a help string to display for this tool-bar item.
12984 XTread_socket does the rest. */
12985 help_echo_object = help_echo_window = Qnil;
12986 help_echo_pos = -1;
12987 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12988 if (NILP (help_echo_string))
12989 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12992 #endif /* !USE_GTK && !HAVE_NS */
12994 #endif /* HAVE_WINDOW_SYSTEM */
12998 /************************************************************************
12999 Horizontal scrolling
13000 ************************************************************************/
13002 /* For all leaf windows in the window tree rooted at WINDOW, set their
13003 hscroll value so that PT is (i) visible in the window, and (ii) so
13004 that it is not within a certain margin at the window's left and
13005 right border. Value is true if any window's hscroll has been
13006 changed. */
13008 static bool
13009 hscroll_window_tree (Lisp_Object window)
13011 bool hscrolled_p = false;
13012 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13013 int hscroll_step_abs = 0;
13014 double hscroll_step_rel = 0;
13016 if (hscroll_relative_p)
13018 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13019 if (hscroll_step_rel < 0)
13021 hscroll_relative_p = false;
13022 hscroll_step_abs = 0;
13025 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13027 hscroll_step_abs = XINT (Vhscroll_step);
13028 if (hscroll_step_abs < 0)
13029 hscroll_step_abs = 0;
13031 else
13032 hscroll_step_abs = 0;
13034 while (WINDOWP (window))
13036 struct window *w = XWINDOW (window);
13038 if (WINDOWP (w->contents))
13039 hscrolled_p |= hscroll_window_tree (w->contents);
13040 else if (w->cursor.vpos >= 0)
13042 int h_margin;
13043 int text_area_width;
13044 struct glyph_row *cursor_row;
13045 struct glyph_row *bottom_row;
13047 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13048 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13049 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13050 else
13051 cursor_row = bottom_row - 1;
13053 if (!cursor_row->enabled_p)
13055 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13056 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13057 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13058 else
13059 cursor_row = bottom_row - 1;
13061 bool row_r2l_p = cursor_row->reversed_p;
13062 bool hscl = hscrolling_current_line_p (w);
13064 text_area_width = window_box_width (w, TEXT_AREA);
13066 /* Scroll when cursor is inside this scroll margin. */
13067 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13069 /* If the position of this window's point has explicitly
13070 changed, no more suspend auto hscrolling. */
13071 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13072 w->suspend_auto_hscroll = false;
13074 /* Remember window point. */
13075 Fset_marker (w->old_pointm,
13076 ((w == XWINDOW (selected_window))
13077 ? make_number (BUF_PT (XBUFFER (w->contents)))
13078 : Fmarker_position (w->pointm)),
13079 w->contents);
13081 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13082 && !w->suspend_auto_hscroll
13083 /* In some pathological cases, like restoring a window
13084 configuration into a frame that is much smaller than
13085 the one from which the configuration was saved, we
13086 get glyph rows whose start and end have zero buffer
13087 positions, which we cannot handle below. Just skip
13088 such windows. */
13089 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13090 /* For left-to-right rows, hscroll when cursor is either
13091 (i) inside the right hscroll margin, or (ii) if it is
13092 inside the left margin and the window is already
13093 hscrolled. */
13094 && ((!row_r2l_p
13095 && ((w->hscroll && w->cursor.x <= h_margin)
13096 || (cursor_row->enabled_p
13097 && cursor_row->truncated_on_right_p
13098 && (w->cursor.x >= text_area_width - h_margin))))
13099 /* For right-to-left rows, the logic is similar,
13100 except that rules for scrolling to left and right
13101 are reversed. E.g., if cursor.x <= h_margin, we
13102 need to hscroll "to the right" unconditionally,
13103 and that will scroll the screen to the left so as
13104 to reveal the next portion of the row. */
13105 || (row_r2l_p
13106 && ((cursor_row->enabled_p
13107 /* FIXME: It is confusing to set the
13108 truncated_on_right_p flag when R2L rows
13109 are actually truncated on the left. */
13110 && cursor_row->truncated_on_right_p
13111 && w->cursor.x <= h_margin)
13112 || (w->hscroll
13113 && (w->cursor.x >= text_area_width - h_margin))))
13114 /* This last condition is needed when moving
13115 vertically from an hscrolled line to a short line
13116 that doesn't need to be hscrolled. If we omit
13117 this condition, the line from which we move will
13118 remain hscrolled. */
13119 || (hscl
13120 && w->hscroll != w->min_hscroll
13121 && !cursor_row->truncated_on_left_p)))
13123 struct it it;
13124 ptrdiff_t hscroll;
13125 struct buffer *saved_current_buffer;
13126 ptrdiff_t pt;
13127 int wanted_x;
13129 /* Find point in a display of infinite width. */
13130 saved_current_buffer = current_buffer;
13131 current_buffer = XBUFFER (w->contents);
13133 if (w == XWINDOW (selected_window))
13134 pt = PT;
13135 else
13136 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13138 /* Move iterator to pt starting at cursor_row->start in
13139 a line with infinite width. */
13140 init_to_row_start (&it, w, cursor_row);
13141 if (hscl)
13142 it.first_visible_x = window_hscroll_limited (w, it.f)
13143 * FRAME_COLUMN_WIDTH (it.f);
13144 it.last_visible_x = DISP_INFINITY;
13145 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13146 /* If the line ends in an overlay string with a newline,
13147 we might infloop, because displaying the window will
13148 want to put the cursor after the overlay, i.e. at X
13149 coordinate of zero on the next screen line. So we
13150 use the buffer position prior to the overlay string
13151 instead. */
13152 if (it.method == GET_FROM_STRING && pt > 1)
13154 init_to_row_start (&it, w, cursor_row);
13155 if (hscl)
13156 it.first_visible_x = (window_hscroll_limited (w, it.f)
13157 * FRAME_COLUMN_WIDTH (it.f));
13158 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13160 current_buffer = saved_current_buffer;
13162 /* Position cursor in window. */
13163 if (!hscroll_relative_p && hscroll_step_abs == 0)
13164 hscroll = max (0, (it.current_x
13165 - (ITERATOR_AT_END_OF_LINE_P (&it)
13166 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13167 : (text_area_width / 2))))
13168 / FRAME_COLUMN_WIDTH (it.f);
13169 else if ((!row_r2l_p
13170 && w->cursor.x >= text_area_width - h_margin)
13171 || (row_r2l_p && w->cursor.x <= h_margin))
13173 if (hscroll_relative_p)
13174 wanted_x = text_area_width * (1 - hscroll_step_rel)
13175 - h_margin;
13176 else
13177 wanted_x = text_area_width
13178 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13179 - h_margin;
13180 hscroll
13181 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13183 else
13185 if (hscroll_relative_p)
13186 wanted_x = text_area_width * hscroll_step_rel
13187 + h_margin;
13188 else
13189 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13190 + h_margin;
13191 hscroll
13192 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13194 hscroll = max (hscroll, w->min_hscroll);
13196 /* Don't prevent redisplay optimizations if hscroll
13197 hasn't changed, as it will unnecessarily slow down
13198 redisplay. */
13199 if (w->hscroll != hscroll
13200 /* When hscrolling only the current line, we need to
13201 report hscroll even if its value is equal to the
13202 previous one, because the new line might need a
13203 different value. */
13204 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13206 struct buffer *b = XBUFFER (w->contents);
13207 b->prevent_redisplay_optimizations_p = true;
13208 w->hscroll = hscroll;
13209 hscrolled_p = true;
13214 window = w->next;
13217 /* Value is true if hscroll of any leaf window has been changed. */
13218 return hscrolled_p;
13222 /* Set hscroll so that cursor is visible and not inside horizontal
13223 scroll margins for all windows in the tree rooted at WINDOW. See
13224 also hscroll_window_tree above. Value is true if any window's
13225 hscroll has been changed. If it has, desired matrices on the frame
13226 of WINDOW are cleared. */
13228 static bool
13229 hscroll_windows (Lisp_Object window)
13231 bool hscrolled_p = hscroll_window_tree (window);
13232 if (hscrolled_p)
13233 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13234 return hscrolled_p;
13239 /************************************************************************
13240 Redisplay
13241 ************************************************************************/
13243 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13244 This is sometimes handy to have in a debugger session. */
13246 #ifdef GLYPH_DEBUG
13248 /* First and last unchanged row for try_window_id. */
13250 static int debug_first_unchanged_at_end_vpos;
13251 static int debug_last_unchanged_at_beg_vpos;
13253 /* Delta vpos and y. */
13255 static int debug_dvpos, debug_dy;
13257 /* Delta in characters and bytes for try_window_id. */
13259 static ptrdiff_t debug_delta, debug_delta_bytes;
13261 /* Values of window_end_pos and window_end_vpos at the end of
13262 try_window_id. */
13264 static ptrdiff_t debug_end_vpos;
13266 /* Append a string to W->desired_matrix->method. FMT is a printf
13267 format string. If trace_redisplay_p is true also printf the
13268 resulting string to stderr. */
13270 static void debug_method_add (struct window *, char const *, ...)
13271 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13273 static void
13274 debug_method_add (struct window *w, char const *fmt, ...)
13276 void *ptr = w;
13277 char *method = w->desired_matrix->method;
13278 int len = strlen (method);
13279 int size = sizeof w->desired_matrix->method;
13280 int remaining = size - len - 1;
13281 va_list ap;
13283 if (len && remaining)
13285 method[len] = '|';
13286 --remaining, ++len;
13289 va_start (ap, fmt);
13290 vsnprintf (method + len, remaining + 1, fmt, ap);
13291 va_end (ap);
13293 if (trace_redisplay_p)
13294 fprintf (stderr, "%p (%s): %s\n",
13295 ptr,
13296 ((BUFFERP (w->contents)
13297 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13298 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13299 : "no buffer"),
13300 method + len);
13303 #endif /* GLYPH_DEBUG */
13306 /* Value is true if all changes in window W, which displays
13307 current_buffer, are in the text between START and END. START is a
13308 buffer position, END is given as a distance from Z. Used in
13309 redisplay_internal for display optimization. */
13311 static bool
13312 text_outside_line_unchanged_p (struct window *w,
13313 ptrdiff_t start, ptrdiff_t end)
13315 bool unchanged_p = true;
13317 /* If text or overlays have changed, see where. */
13318 if (window_outdated (w))
13320 /* Gap in the line? */
13321 if (GPT < start || Z - GPT < end)
13322 unchanged_p = false;
13324 /* Changes start in front of the line, or end after it? */
13325 if (unchanged_p
13326 && (BEG_UNCHANGED < start - 1
13327 || END_UNCHANGED < end))
13328 unchanged_p = false;
13330 /* If selective display, can't optimize if changes start at the
13331 beginning of the line. */
13332 if (unchanged_p
13333 && INTEGERP (BVAR (current_buffer, selective_display))
13334 && XINT (BVAR (current_buffer, selective_display)) > 0
13335 && (BEG_UNCHANGED < start || GPT <= start))
13336 unchanged_p = false;
13338 /* If there are overlays at the start or end of the line, these
13339 may have overlay strings with newlines in them. A change at
13340 START, for instance, may actually concern the display of such
13341 overlay strings as well, and they are displayed on different
13342 lines. So, quickly rule out this case. (For the future, it
13343 might be desirable to implement something more telling than
13344 just BEG/END_UNCHANGED.) */
13345 if (unchanged_p)
13347 if (BEG + BEG_UNCHANGED == start
13348 && overlay_touches_p (start))
13349 unchanged_p = false;
13350 if (END_UNCHANGED == end
13351 && overlay_touches_p (Z - end))
13352 unchanged_p = false;
13355 /* Under bidi reordering, adding or deleting a character in the
13356 beginning of a paragraph, before the first strong directional
13357 character, can change the base direction of the paragraph (unless
13358 the buffer specifies a fixed paragraph direction), which will
13359 require redisplaying the whole paragraph. It might be worthwhile
13360 to find the paragraph limits and widen the range of redisplayed
13361 lines to that, but for now just give up this optimization. */
13362 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13363 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13364 unchanged_p = false;
13367 return unchanged_p;
13371 /* Do a frame update, taking possible shortcuts into account. This is
13372 the main external entry point for redisplay.
13374 If the last redisplay displayed an echo area message and that message
13375 is no longer requested, we clear the echo area or bring back the
13376 mini-buffer if that is in use. */
13378 void
13379 redisplay (void)
13381 redisplay_internal ();
13385 static Lisp_Object
13386 overlay_arrow_string_or_property (Lisp_Object var)
13388 Lisp_Object val;
13390 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13391 return val;
13393 return Voverlay_arrow_string;
13396 /* Return true if there are any overlay-arrows in current_buffer. */
13397 static bool
13398 overlay_arrow_in_current_buffer_p (void)
13400 Lisp_Object vlist;
13402 for (vlist = Voverlay_arrow_variable_list;
13403 CONSP (vlist);
13404 vlist = XCDR (vlist))
13406 Lisp_Object var = XCAR (vlist);
13407 Lisp_Object val;
13409 if (!SYMBOLP (var))
13410 continue;
13411 val = find_symbol_value (var);
13412 if (MARKERP (val)
13413 && current_buffer == XMARKER (val)->buffer)
13414 return true;
13416 return false;
13420 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13421 has changed.
13422 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13423 buffers that are affected. */
13425 static bool
13426 overlay_arrows_changed_p (bool set_redisplay)
13428 Lisp_Object vlist;
13429 bool changed = false;
13431 for (vlist = Voverlay_arrow_variable_list;
13432 CONSP (vlist);
13433 vlist = XCDR (vlist))
13435 Lisp_Object var = XCAR (vlist);
13436 Lisp_Object val, pstr;
13438 if (!SYMBOLP (var))
13439 continue;
13440 val = find_symbol_value (var);
13441 if (!MARKERP (val))
13442 continue;
13443 if (! EQ (COERCE_MARKER (val),
13444 /* FIXME: Don't we have a problem, using such a global
13445 * "last-position" if the variable is buffer-local? */
13446 Fget (var, Qlast_arrow_position))
13447 || ! (pstr = overlay_arrow_string_or_property (var),
13448 EQ (pstr, Fget (var, Qlast_arrow_string))))
13450 struct buffer *buf = XMARKER (val)->buffer;
13452 if (set_redisplay)
13454 if (buf)
13455 bset_redisplay (buf);
13456 changed = true;
13458 else
13459 return true;
13462 return changed;
13465 /* Mark overlay arrows to be updated on next redisplay. */
13467 static void
13468 update_overlay_arrows (int up_to_date)
13470 Lisp_Object vlist;
13472 for (vlist = Voverlay_arrow_variable_list;
13473 CONSP (vlist);
13474 vlist = XCDR (vlist))
13476 Lisp_Object var = XCAR (vlist);
13478 if (!SYMBOLP (var))
13479 continue;
13481 if (up_to_date > 0)
13483 Lisp_Object val = find_symbol_value (var);
13484 if (!MARKERP (val))
13485 continue;
13486 Fput (var, Qlast_arrow_position,
13487 COERCE_MARKER (val));
13488 Fput (var, Qlast_arrow_string,
13489 overlay_arrow_string_or_property (var));
13491 else if (up_to_date < 0
13492 || !NILP (Fget (var, Qlast_arrow_position)))
13494 Fput (var, Qlast_arrow_position, Qt);
13495 Fput (var, Qlast_arrow_string, Qt);
13501 /* Return overlay arrow string to display at row.
13502 Return integer (bitmap number) for arrow bitmap in left fringe.
13503 Return nil if no overlay arrow. */
13505 static Lisp_Object
13506 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13508 Lisp_Object vlist;
13510 for (vlist = Voverlay_arrow_variable_list;
13511 CONSP (vlist);
13512 vlist = XCDR (vlist))
13514 Lisp_Object var = XCAR (vlist);
13515 Lisp_Object val;
13517 if (!SYMBOLP (var))
13518 continue;
13520 val = find_symbol_value (var);
13522 if (MARKERP (val)
13523 && current_buffer == XMARKER (val)->buffer
13524 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13526 if (FRAME_WINDOW_P (it->f)
13527 /* FIXME: if ROW->reversed_p is set, this should test
13528 the right fringe, not the left one. */
13529 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13531 #ifdef HAVE_WINDOW_SYSTEM
13532 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13534 int fringe_bitmap = lookup_fringe_bitmap (val);
13535 if (fringe_bitmap != 0)
13536 return make_number (fringe_bitmap);
13538 #endif
13539 return make_number (-1); /* Use default arrow bitmap. */
13541 return overlay_arrow_string_or_property (var);
13545 return Qnil;
13548 /* Return true if point moved out of or into a composition. Otherwise
13549 return false. PREV_BUF and PREV_PT are the last point buffer and
13550 position. BUF and PT are the current point buffer and position. */
13552 static bool
13553 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13554 struct buffer *buf, ptrdiff_t pt)
13556 ptrdiff_t start, end;
13557 Lisp_Object prop;
13558 Lisp_Object buffer;
13560 XSETBUFFER (buffer, buf);
13561 /* Check a composition at the last point if point moved within the
13562 same buffer. */
13563 if (prev_buf == buf)
13565 if (prev_pt == pt)
13566 /* Point didn't move. */
13567 return false;
13569 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13570 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13571 && composition_valid_p (start, end, prop)
13572 && start < prev_pt && end > prev_pt)
13573 /* The last point was within the composition. Return true iff
13574 point moved out of the composition. */
13575 return (pt <= start || pt >= end);
13578 /* Check a composition at the current point. */
13579 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13580 && find_composition (pt, -1, &start, &end, &prop, buffer)
13581 && composition_valid_p (start, end, prop)
13582 && start < pt && end > pt);
13585 /* Reconsider the clip changes of buffer which is displayed in W. */
13587 static void
13588 reconsider_clip_changes (struct window *w)
13590 struct buffer *b = XBUFFER (w->contents);
13592 if (b->clip_changed
13593 && w->window_end_valid
13594 && w->current_matrix->buffer == b
13595 && w->current_matrix->zv == BUF_ZV (b)
13596 && w->current_matrix->begv == BUF_BEGV (b))
13597 b->clip_changed = false;
13599 /* If display wasn't paused, and W is not a tool bar window, see if
13600 point has been moved into or out of a composition. In that case,
13601 set b->clip_changed to force updating the screen. If
13602 b->clip_changed has already been set, skip this check. */
13603 if (!b->clip_changed && w->window_end_valid)
13605 ptrdiff_t pt = (w == XWINDOW (selected_window)
13606 ? PT : marker_position (w->pointm));
13608 if ((w->current_matrix->buffer != b || pt != w->last_point)
13609 && check_point_in_composition (w->current_matrix->buffer,
13610 w->last_point, b, pt))
13611 b->clip_changed = true;
13615 static void
13616 propagate_buffer_redisplay (void)
13617 { /* Resetting b->text->redisplay is problematic!
13618 We can't just reset it in the case that some window that displays
13619 it has not been redisplayed; and such a window can stay
13620 unredisplayed for a long time if it's currently invisible.
13621 But we do want to reset it at the end of redisplay otherwise
13622 its displayed windows will keep being redisplayed over and over
13623 again.
13624 So we copy all b->text->redisplay flags up to their windows here,
13625 such that mark_window_display_accurate can safely reset
13626 b->text->redisplay. */
13627 Lisp_Object ws = window_list ();
13628 for (; CONSP (ws); ws = XCDR (ws))
13630 struct window *thisw = XWINDOW (XCAR (ws));
13631 struct buffer *thisb = XBUFFER (thisw->contents);
13632 if (thisb->text->redisplay)
13633 thisw->redisplay = true;
13637 #define STOP_POLLING \
13638 do { if (! polling_stopped_here) stop_polling (); \
13639 polling_stopped_here = true; } while (false)
13641 #define RESUME_POLLING \
13642 do { if (polling_stopped_here) start_polling (); \
13643 polling_stopped_here = false; } while (false)
13646 /* Perhaps in the future avoid recentering windows if it
13647 is not necessary; currently that causes some problems. */
13649 static void
13650 redisplay_internal (void)
13652 struct window *w = XWINDOW (selected_window);
13653 struct window *sw;
13654 struct frame *fr;
13655 bool pending;
13656 bool must_finish = false, match_p;
13657 struct text_pos tlbufpos, tlendpos;
13658 int number_of_visible_frames;
13659 ptrdiff_t count;
13660 struct frame *sf;
13661 bool polling_stopped_here = false;
13662 Lisp_Object tail, frame;
13664 /* Set a limit to the number of retries we perform due to horizontal
13665 scrolling, this avoids getting stuck in an uninterruptible
13666 infinite loop (Bug #24633). */
13667 enum { MAX_HSCROLL_RETRIES = 16 };
13668 int hscroll_retries = 0;
13670 /* Limit the number of retries for when frame(s) become garbaged as
13671 result of redisplaying them. Some packages set various redisplay
13672 hooks, such as window-scroll-functions, to run Lisp that always
13673 calls APIs which cause the frame's garbaged flag to become set,
13674 so we loop indefinitely. */
13675 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13676 int garbaged_frame_retries = 0;
13678 /* True means redisplay has to consider all windows on all
13679 frames. False, only selected_window is considered. */
13680 bool consider_all_windows_p;
13682 /* True means redisplay has to redisplay the miniwindow. */
13683 bool update_miniwindow_p = false;
13685 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13687 /* No redisplay if running in batch mode or frame is not yet fully
13688 initialized, or redisplay is explicitly turned off by setting
13689 Vinhibit_redisplay. */
13690 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13691 || !NILP (Vinhibit_redisplay))
13692 return;
13694 /* Don't examine these until after testing Vinhibit_redisplay.
13695 When Emacs is shutting down, perhaps because its connection to
13696 X has dropped, we should not look at them at all. */
13697 fr = XFRAME (w->frame);
13698 sf = SELECTED_FRAME ();
13700 if (!fr->glyphs_initialized_p)
13701 return;
13703 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13704 if (popup_activated ())
13705 return;
13706 #endif
13708 /* I don't think this happens but let's be paranoid. */
13709 if (redisplaying_p)
13710 return;
13712 /* Record a function that clears redisplaying_p
13713 when we leave this function. */
13714 count = SPECPDL_INDEX ();
13715 record_unwind_protect_void (unwind_redisplay);
13716 redisplaying_p = true;
13717 block_buffer_flips ();
13718 specbind (Qinhibit_free_realized_faces, Qnil);
13720 /* Record this function, so it appears on the profiler's backtraces. */
13721 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13723 FOR_EACH_FRAME (tail, frame)
13724 XFRAME (frame)->already_hscrolled_p = false;
13726 retry:
13727 /* Remember the currently selected window. */
13728 sw = w;
13730 pending = false;
13731 forget_escape_and_glyphless_faces ();
13733 inhibit_free_realized_faces = false;
13735 /* If face_change, init_iterator will free all realized faces, which
13736 includes the faces referenced from current matrices. So, we
13737 can't reuse current matrices in this case. */
13738 if (face_change)
13739 windows_or_buffers_changed = 47;
13741 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13742 && FRAME_TTY (sf)->previous_frame != sf)
13744 /* Since frames on a single ASCII terminal share the same
13745 display area, displaying a different frame means redisplay
13746 the whole thing. */
13747 SET_FRAME_GARBAGED (sf);
13748 #ifndef DOS_NT
13749 set_tty_color_mode (FRAME_TTY (sf), sf);
13750 #endif
13751 FRAME_TTY (sf)->previous_frame = sf;
13754 /* Set the visible flags for all frames. Do this before checking for
13755 resized or garbaged frames; they want to know if their frames are
13756 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13757 number_of_visible_frames = 0;
13759 FOR_EACH_FRAME (tail, frame)
13761 struct frame *f = XFRAME (frame);
13763 if (FRAME_VISIBLE_P (f))
13765 ++number_of_visible_frames;
13766 /* Adjust matrices for visible frames only. */
13767 if (f->fonts_changed)
13769 adjust_frame_glyphs (f);
13770 /* Disable all redisplay optimizations for this frame.
13771 This is because adjust_frame_glyphs resets the
13772 enabled_p flag for all glyph rows of all windows, so
13773 many optimizations will fail anyway, and some might
13774 fail to test that flag and do bogus things as
13775 result. */
13776 SET_FRAME_GARBAGED (f);
13777 f->fonts_changed = false;
13779 /* If cursor type has been changed on the frame
13780 other than selected, consider all frames. */
13781 if (f != sf && f->cursor_type_changed)
13782 fset_redisplay (f);
13784 clear_desired_matrices (f);
13787 /* Notice any pending interrupt request to change frame size. */
13788 do_pending_window_change (true);
13790 /* do_pending_window_change could change the selected_window due to
13791 frame resizing which makes the selected window too small. */
13792 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13793 sw = w;
13795 /* Clear frames marked as garbaged. */
13796 clear_garbaged_frames ();
13798 /* Build menubar and tool-bar items. */
13799 if (NILP (Vmemory_full))
13800 prepare_menu_bars ();
13802 reconsider_clip_changes (w);
13804 /* In most cases selected window displays current buffer. */
13805 match_p = XBUFFER (w->contents) == current_buffer;
13806 if (match_p)
13808 /* Detect case that we need to write or remove a star in the mode line. */
13809 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13810 w->update_mode_line = true;
13812 if (mode_line_update_needed (w))
13813 w->update_mode_line = true;
13815 /* If reconsider_clip_changes above decided that the narrowing
13816 in the current buffer changed, make sure all other windows
13817 showing that buffer will be redisplayed. */
13818 if (current_buffer->clip_changed)
13819 bset_update_mode_line (current_buffer);
13822 /* Normally the message* functions will have already displayed and
13823 updated the echo area, but the frame may have been trashed, or
13824 the update may have been preempted, so display the echo area
13825 again here. Checking message_cleared_p captures the case that
13826 the echo area should be cleared. */
13827 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13828 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13829 || (message_cleared_p
13830 && minibuf_level == 0
13831 /* If the mini-window is currently selected, this means the
13832 echo-area doesn't show through. */
13833 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13835 echo_area_display (false);
13837 /* If echo_area_display resizes the mini-window, the redisplay and
13838 window_sizes_changed flags of the selected frame are set, but
13839 it's too late for the hooks in window-size-change-functions,
13840 which have been examined already in prepare_menu_bars. So in
13841 that case we call the hooks here only for the selected frame. */
13842 if (sf->redisplay)
13844 ptrdiff_t count1 = SPECPDL_INDEX ();
13846 record_unwind_save_match_data ();
13847 run_window_size_change_functions (selected_frame);
13848 unbind_to (count1, Qnil);
13851 if (message_cleared_p)
13852 update_miniwindow_p = true;
13854 must_finish = true;
13856 /* If we don't display the current message, don't clear the
13857 message_cleared_p flag, because, if we did, we wouldn't clear
13858 the echo area in the next redisplay which doesn't preserve
13859 the echo area. */
13860 if (!display_last_displayed_message_p)
13861 message_cleared_p = false;
13863 else if (EQ (selected_window, minibuf_window)
13864 && (current_buffer->clip_changed || window_outdated (w))
13865 && resize_mini_window (w, false))
13867 if (sf->redisplay)
13869 ptrdiff_t count1 = SPECPDL_INDEX ();
13871 record_unwind_save_match_data ();
13872 run_window_size_change_functions (selected_frame);
13873 unbind_to (count1, Qnil);
13876 /* Resized active mini-window to fit the size of what it is
13877 showing if its contents might have changed. */
13878 must_finish = true;
13880 /* If window configuration was changed, frames may have been
13881 marked garbaged. Clear them or we will experience
13882 surprises wrt scrolling. */
13883 clear_garbaged_frames ();
13886 if (windows_or_buffers_changed && !update_mode_lines)
13887 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13888 only the windows's contents needs to be refreshed, or whether the
13889 mode-lines also need a refresh. */
13890 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13891 ? REDISPLAY_SOME : 32);
13893 /* If specs for an arrow have changed, do thorough redisplay
13894 to ensure we remove any arrow that should no longer exist. */
13895 /* Apparently, this is the only case where we update other windows,
13896 without updating other mode-lines. */
13897 overlay_arrows_changed_p (true);
13899 consider_all_windows_p = (update_mode_lines
13900 || windows_or_buffers_changed);
13902 #define AINC(a,i) \
13904 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13905 if (INTEGERP (entry)) \
13906 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13909 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13910 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13912 /* Optimize the case that only the line containing the cursor in the
13913 selected window has changed. Variables starting with this_ are
13914 set in display_line and record information about the line
13915 containing the cursor. */
13916 tlbufpos = this_line_start_pos;
13917 tlendpos = this_line_end_pos;
13918 if (!consider_all_windows_p
13919 && CHARPOS (tlbufpos) > 0
13920 && !w->update_mode_line
13921 && !current_buffer->clip_changed
13922 && !current_buffer->prevent_redisplay_optimizations_p
13923 && FRAME_VISIBLE_P (XFRAME (w->frame))
13924 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13925 && !XFRAME (w->frame)->cursor_type_changed
13926 && !XFRAME (w->frame)->face_change
13927 /* Make sure recorded data applies to current buffer, etc. */
13928 && this_line_buffer == current_buffer
13929 && match_p
13930 && !w->force_start
13931 && !w->optional_new_start
13932 /* Point must be on the line that we have info recorded about. */
13933 && PT >= CHARPOS (tlbufpos)
13934 && PT <= Z - CHARPOS (tlendpos)
13935 /* All text outside that line, including its final newline,
13936 must be unchanged. */
13937 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13938 CHARPOS (tlendpos)))
13940 if (CHARPOS (tlbufpos) > BEGV
13941 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13942 && (CHARPOS (tlbufpos) == ZV
13943 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13944 /* Former continuation line has disappeared by becoming empty. */
13945 goto cancel;
13946 else if (window_outdated (w) || MINI_WINDOW_P (w))
13948 /* We have to handle the case of continuation around a
13949 wide-column character (see the comment in indent.c around
13950 line 1340).
13952 For instance, in the following case:
13954 -------- Insert --------
13955 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13956 J_I_ ==> J_I_ `^^' are cursors.
13957 ^^ ^^
13958 -------- --------
13960 As we have to redraw the line above, we cannot use this
13961 optimization. */
13963 struct it it;
13964 int line_height_before = this_line_pixel_height;
13966 /* Note that start_display will handle the case that the
13967 line starting at tlbufpos is a continuation line. */
13968 start_display (&it, w, tlbufpos);
13970 /* Implementation note: It this still necessary? */
13971 if (it.current_x != this_line_start_x)
13972 goto cancel;
13974 TRACE ((stderr, "trying display optimization 1\n"));
13975 w->cursor.vpos = -1;
13976 overlay_arrow_seen = false;
13977 it.vpos = this_line_vpos;
13978 it.current_y = this_line_y;
13979 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13980 display_line (&it, -1);
13982 /* If line contains point, is not continued,
13983 and ends at same distance from eob as before, we win. */
13984 if (w->cursor.vpos >= 0
13985 /* Line is not continued, otherwise this_line_start_pos
13986 would have been set to 0 in display_line. */
13987 && CHARPOS (this_line_start_pos)
13988 /* Line ends as before. */
13989 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13990 /* Line has same height as before. Otherwise other lines
13991 would have to be shifted up or down. */
13992 && this_line_pixel_height == line_height_before)
13994 /* If this is not the window's last line, we must adjust
13995 the charstarts of the lines below. */
13996 if (it.current_y < it.last_visible_y)
13998 struct glyph_row *row
13999 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14000 ptrdiff_t delta, delta_bytes;
14002 /* We used to distinguish between two cases here,
14003 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14004 when the line ends in a newline or the end of the
14005 buffer's accessible portion. But both cases did
14006 the same, so they were collapsed. */
14007 delta = (Z
14008 - CHARPOS (tlendpos)
14009 - MATRIX_ROW_START_CHARPOS (row));
14010 delta_bytes = (Z_BYTE
14011 - BYTEPOS (tlendpos)
14012 - MATRIX_ROW_START_BYTEPOS (row));
14014 increment_matrix_positions (w->current_matrix,
14015 this_line_vpos + 1,
14016 w->current_matrix->nrows,
14017 delta, delta_bytes);
14020 /* If this row displays text now but previously didn't,
14021 or vice versa, w->window_end_vpos may have to be
14022 adjusted. */
14023 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14025 if (w->window_end_vpos < this_line_vpos)
14026 w->window_end_vpos = this_line_vpos;
14028 else if (w->window_end_vpos == this_line_vpos
14029 && this_line_vpos > 0)
14030 w->window_end_vpos = this_line_vpos - 1;
14031 w->window_end_valid = false;
14033 /* Update hint: No need to try to scroll in update_window. */
14034 w->desired_matrix->no_scrolling_p = true;
14036 #ifdef GLYPH_DEBUG
14037 *w->desired_matrix->method = 0;
14038 debug_method_add (w, "optimization 1");
14039 #endif
14040 #ifdef HAVE_WINDOW_SYSTEM
14041 update_window_fringes (w, false);
14042 #endif
14043 goto update;
14045 else
14046 goto cancel;
14048 else if (/* Cursor position hasn't changed. */
14049 PT == w->last_point
14050 /* Make sure the cursor was last displayed
14051 in this window. Otherwise we have to reposition it. */
14053 /* PXW: Must be converted to pixels, probably. */
14054 && 0 <= w->cursor.vpos
14055 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14057 if (!must_finish)
14059 do_pending_window_change (true);
14060 /* If selected_window changed, redisplay again. */
14061 if (WINDOWP (selected_window)
14062 && (w = XWINDOW (selected_window)) != sw)
14063 goto retry;
14065 /* We used to always goto end_of_redisplay here, but this
14066 isn't enough if we have a blinking cursor. */
14067 if (w->cursor_off_p == w->last_cursor_off_p)
14068 goto end_of_redisplay;
14070 goto update;
14072 /* If highlighting the region, or if the cursor is in the echo area,
14073 then we can't just move the cursor. */
14074 else if (NILP (Vshow_trailing_whitespace)
14075 && !cursor_in_echo_area)
14077 struct it it;
14078 struct glyph_row *row;
14080 /* Skip from tlbufpos to PT and see where it is. Note that
14081 PT may be in invisible text. If so, we will end at the
14082 next visible position. */
14083 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14084 NULL, DEFAULT_FACE_ID);
14085 it.current_x = this_line_start_x;
14086 it.current_y = this_line_y;
14087 it.vpos = this_line_vpos;
14089 /* The call to move_it_to stops in front of PT, but
14090 moves over before-strings. */
14091 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14093 if (it.vpos == this_line_vpos
14094 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14095 row->enabled_p))
14097 eassert (this_line_vpos == it.vpos);
14098 eassert (this_line_y == it.current_y);
14099 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14100 if (cursor_row_fully_visible_p (w, false, true))
14102 #ifdef GLYPH_DEBUG
14103 *w->desired_matrix->method = 0;
14104 debug_method_add (w, "optimization 3");
14105 #endif
14106 goto update;
14108 else
14109 goto cancel;
14111 else
14112 goto cancel;
14115 cancel:
14116 /* Text changed drastically or point moved off of line. */
14117 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14120 CHARPOS (this_line_start_pos) = 0;
14121 ++clear_face_cache_count;
14122 #ifdef HAVE_WINDOW_SYSTEM
14123 ++clear_image_cache_count;
14124 #endif
14126 /* Build desired matrices, and update the display. If
14127 consider_all_windows_p, do it for all windows on all frames that
14128 require redisplay, as specified by their 'redisplay' flag.
14129 Otherwise do it for selected_window, only. */
14131 if (consider_all_windows_p)
14133 FOR_EACH_FRAME (tail, frame)
14134 XFRAME (frame)->updated_p = false;
14136 propagate_buffer_redisplay ();
14138 FOR_EACH_FRAME (tail, frame)
14140 struct frame *f = XFRAME (frame);
14142 /* We don't have to do anything for unselected terminal
14143 frames. */
14144 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14145 && !EQ (FRAME_TTY (f)->top_frame, frame))
14146 continue;
14148 retry_frame:
14149 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14151 bool gcscrollbars
14152 /* Only GC scrollbars when we redisplay the whole frame. */
14153 = f->redisplay || !REDISPLAY_SOME_P ();
14154 bool f_redisplay_flag = f->redisplay;
14155 /* Mark all the scroll bars to be removed; we'll redeem
14156 the ones we want when we redisplay their windows. */
14157 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14158 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14160 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14161 redisplay_windows (FRAME_ROOT_WINDOW (f));
14162 /* Remember that the invisible frames need to be redisplayed next
14163 time they're visible. */
14164 else if (!REDISPLAY_SOME_P ())
14165 f->redisplay = true;
14167 /* The X error handler may have deleted that frame. */
14168 if (!FRAME_LIVE_P (f))
14169 continue;
14171 /* Any scroll bars which redisplay_windows should have
14172 nuked should now go away. */
14173 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14174 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14176 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14178 /* If fonts changed on visible frame, display again. */
14179 if (f->fonts_changed)
14181 adjust_frame_glyphs (f);
14182 /* Disable all redisplay optimizations for this
14183 frame. For the reasons, see the comment near
14184 the previous call to adjust_frame_glyphs above. */
14185 SET_FRAME_GARBAGED (f);
14186 f->fonts_changed = false;
14187 goto retry_frame;
14190 /* See if we have to hscroll. */
14191 if (!f->already_hscrolled_p)
14193 f->already_hscrolled_p = true;
14194 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14195 && hscroll_windows (f->root_window))
14197 hscroll_retries++;
14198 goto retry_frame;
14202 /* If the frame's redisplay flag was not set before
14203 we went about redisplaying its windows, but it is
14204 set now, that means we employed some redisplay
14205 optimizations inside redisplay_windows, and
14206 bypassed producing some screen lines. But if
14207 f->redisplay is now set, it might mean the old
14208 faces are no longer valid (e.g., if redisplaying
14209 some window called some Lisp which defined a new
14210 face or redefined an existing face), so trying to
14211 use them in update_frame will segfault.
14212 Therefore, we must redisplay this frame. */
14213 if (!f_redisplay_flag && f->redisplay)
14214 goto retry_frame;
14215 /* In some case (e.g., window resize), we notice
14216 only during window updating that the window
14217 content changed unpredictably (e.g., a GTK
14218 scrollbar moved, or some Lisp hook that winds up
14219 calling adjust_frame_glyphs) and that our
14220 previous estimation of the frame content was
14221 garbage. We have to start over. These cases
14222 should be rare, so going all the way back to the
14223 top of redisplay should be good enough. */
14224 if (FRAME_GARBAGED_P (f)
14225 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14226 goto retry;
14228 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14229 x_clear_under_internal_border (f);
14230 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14232 /* Prevent various kinds of signals during display
14233 update. stdio is not robust about handling
14234 signals, which can cause an apparent I/O error. */
14235 if (interrupt_input)
14236 unrequest_sigio ();
14237 STOP_POLLING;
14239 pending |= update_frame (f, false, false);
14240 f->cursor_type_changed = false;
14241 f->updated_p = true;
14246 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14248 if (!pending)
14250 /* Do the mark_window_display_accurate after all windows have
14251 been redisplayed because this call resets flags in buffers
14252 which are needed for proper redisplay. */
14253 FOR_EACH_FRAME (tail, frame)
14255 struct frame *f = XFRAME (frame);
14256 if (f->updated_p)
14258 f->redisplay = false;
14259 f->garbaged = false;
14260 mark_window_display_accurate (f->root_window, true);
14261 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14262 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14267 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14269 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14270 /* Use list_of_error, not Qerror, so that
14271 we catch only errors and don't run the debugger. */
14272 internal_condition_case_1 (redisplay_window_1, selected_window,
14273 list_of_error,
14274 redisplay_window_error);
14275 if (update_miniwindow_p)
14276 internal_condition_case_1 (redisplay_window_1,
14277 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14278 redisplay_window_error);
14280 /* Compare desired and current matrices, perform output. */
14282 update:
14283 /* If fonts changed, display again. Likewise if redisplay_window_1
14284 above caused some change (e.g., a change in faces) that requires
14285 considering the entire frame again. */
14286 if (sf->fonts_changed || sf->redisplay)
14288 if (sf->redisplay)
14290 /* Set this to force a more thorough redisplay.
14291 Otherwise, we might immediately loop back to the
14292 above "else-if" clause (since all the conditions that
14293 led here might still be true), and we will then
14294 infloop, because the selected-frame's redisplay flag
14295 is not (and cannot be) reset. */
14296 windows_or_buffers_changed = 50;
14298 goto retry;
14301 /* Prevent freeing of realized faces, since desired matrices are
14302 pending that reference the faces we computed and cached. */
14303 inhibit_free_realized_faces = true;
14305 /* Prevent various kinds of signals during display update.
14306 stdio is not robust about handling signals,
14307 which can cause an apparent I/O error. */
14308 if (interrupt_input)
14309 unrequest_sigio ();
14310 STOP_POLLING;
14312 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14314 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14315 && hscroll_windows (selected_window))
14317 hscroll_retries++;
14318 goto retry;
14321 XWINDOW (selected_window)->must_be_updated_p = true;
14322 pending = update_frame (sf, false, false);
14323 sf->cursor_type_changed = false;
14326 /* We may have called echo_area_display at the top of this
14327 function. If the echo area is on another frame, that may
14328 have put text on a frame other than the selected one, so the
14329 above call to update_frame would not have caught it. Catch
14330 it here. */
14331 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14332 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14334 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14336 XWINDOW (mini_window)->must_be_updated_p = true;
14337 pending |= update_frame (mini_frame, false, false);
14338 mini_frame->cursor_type_changed = false;
14339 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14340 && hscroll_windows (mini_window))
14342 hscroll_retries++;
14343 goto retry;
14348 /* If display was paused because of pending input, make sure we do a
14349 thorough update the next time. */
14350 if (pending)
14352 /* Prevent the optimization at the beginning of
14353 redisplay_internal that tries a single-line update of the
14354 line containing the cursor in the selected window. */
14355 CHARPOS (this_line_start_pos) = 0;
14357 /* Let the overlay arrow be updated the next time. */
14358 update_overlay_arrows (0);
14360 /* If we pause after scrolling, some rows in the current
14361 matrices of some windows are not valid. */
14362 if (!WINDOW_FULL_WIDTH_P (w)
14363 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14364 update_mode_lines = 36;
14366 else
14368 if (!consider_all_windows_p)
14370 /* This has already been done above if
14371 consider_all_windows_p is set. */
14372 if (XBUFFER (w->contents)->text->redisplay
14373 && buffer_window_count (XBUFFER (w->contents)) > 1)
14374 /* This can happen if b->text->redisplay was set during
14375 jit-lock. */
14376 propagate_buffer_redisplay ();
14377 mark_window_display_accurate_1 (w, true);
14379 /* Say overlay arrows are up to date. */
14380 update_overlay_arrows (1);
14382 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14383 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14386 update_mode_lines = 0;
14387 windows_or_buffers_changed = 0;
14390 /* Start SIGIO interrupts coming again. Having them off during the
14391 code above makes it less likely one will discard output, but not
14392 impossible, since there might be stuff in the system buffer here.
14393 But it is much hairier to try to do anything about that. */
14394 if (interrupt_input)
14395 request_sigio ();
14396 RESUME_POLLING;
14398 /* If a frame has become visible which was not before, redisplay
14399 again, so that we display it. Expose events for such a frame
14400 (which it gets when becoming visible) don't call the parts of
14401 redisplay constructing glyphs, so simply exposing a frame won't
14402 display anything in this case. So, we have to display these
14403 frames here explicitly. */
14404 if (!pending)
14406 int new_count = 0;
14408 FOR_EACH_FRAME (tail, frame)
14410 if (XFRAME (frame)->visible)
14411 new_count++;
14414 if (new_count != number_of_visible_frames)
14415 windows_or_buffers_changed = 52;
14418 /* Change frame size now if a change is pending. */
14419 do_pending_window_change (true);
14421 /* If we just did a pending size change, or have additional
14422 visible frames, or selected_window changed, redisplay again. */
14423 if ((windows_or_buffers_changed && !pending)
14424 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14425 goto retry;
14427 /* Clear the face and image caches.
14429 We used to do this only if consider_all_windows_p. But the cache
14430 needs to be cleared if a timer creates images in the current
14431 buffer (e.g. the test case in Bug#6230). */
14433 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14435 clear_face_cache (false);
14436 clear_face_cache_count = 0;
14439 #ifdef HAVE_WINDOW_SYSTEM
14440 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14442 clear_image_caches (Qnil);
14443 clear_image_cache_count = 0;
14445 #endif /* HAVE_WINDOW_SYSTEM */
14447 end_of_redisplay:
14448 #ifdef HAVE_NS
14449 ns_set_doc_edited ();
14450 #endif
14451 if (interrupt_input && interrupts_deferred)
14452 request_sigio ();
14454 unbind_to (count, Qnil);
14455 RESUME_POLLING;
14458 static void
14459 unwind_redisplay_preserve_echo_area (void)
14461 unblock_buffer_flips ();
14464 /* Redisplay, but leave alone any recent echo area message unless
14465 another message has been requested in its place.
14467 This is useful in situations where you need to redisplay but no
14468 user action has occurred, making it inappropriate for the message
14469 area to be cleared. See tracking_off and
14470 wait_reading_process_output for examples of these situations.
14472 FROM_WHERE is an integer saying from where this function was
14473 called. This is useful for debugging. */
14475 void
14476 redisplay_preserve_echo_area (int from_where)
14478 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14480 block_input ();
14481 ptrdiff_t count = SPECPDL_INDEX ();
14482 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14483 block_buffer_flips ();
14484 unblock_input ();
14486 if (!NILP (echo_area_buffer[1]))
14488 /* We have a previously displayed message, but no current
14489 message. Redisplay the previous message. */
14490 display_last_displayed_message_p = true;
14491 redisplay_internal ();
14492 display_last_displayed_message_p = false;
14494 else
14495 redisplay_internal ();
14497 flush_frame (SELECTED_FRAME ());
14498 unbind_to (count, Qnil);
14502 /* Function registered with record_unwind_protect in redisplay_internal. */
14504 static void
14505 unwind_redisplay (void)
14507 redisplaying_p = false;
14508 unblock_buffer_flips ();
14512 /* Mark the display of leaf window W as accurate or inaccurate.
14513 If ACCURATE_P, mark display of W as accurate.
14514 If !ACCURATE_P, arrange for W to be redisplayed the next
14515 time redisplay_internal is called. */
14517 static void
14518 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14520 struct buffer *b = XBUFFER (w->contents);
14522 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14523 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14524 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14526 if (accurate_p)
14528 b->clip_changed = false;
14529 b->prevent_redisplay_optimizations_p = false;
14530 eassert (buffer_window_count (b) > 0);
14531 /* Resetting b->text->redisplay is problematic!
14532 In order to make it safer to do it here, redisplay_internal must
14533 have copied all b->text->redisplay to their respective windows. */
14534 b->text->redisplay = false;
14536 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14537 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14538 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14539 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14541 w->current_matrix->buffer = b;
14542 w->current_matrix->begv = BUF_BEGV (b);
14543 w->current_matrix->zv = BUF_ZV (b);
14545 w->last_cursor_vpos = w->cursor.vpos;
14546 w->last_cursor_off_p = w->cursor_off_p;
14548 if (w == XWINDOW (selected_window))
14549 w->last_point = BUF_PT (b);
14550 else
14551 w->last_point = marker_position (w->pointm);
14553 w->window_end_valid = true;
14554 w->update_mode_line = false;
14557 w->redisplay = !accurate_p;
14561 /* Mark the display of windows in the window tree rooted at WINDOW as
14562 accurate or inaccurate. If ACCURATE_P, mark display of
14563 windows as accurate. If !ACCURATE_P, arrange for windows to
14564 be redisplayed the next time redisplay_internal is called. */
14566 void
14567 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14569 struct window *w;
14571 for (; !NILP (window); window = w->next)
14573 w = XWINDOW (window);
14574 if (WINDOWP (w->contents))
14575 mark_window_display_accurate (w->contents, accurate_p);
14576 else
14577 mark_window_display_accurate_1 (w, accurate_p);
14580 if (accurate_p)
14581 update_overlay_arrows (1);
14582 else
14583 /* Force a thorough redisplay the next time by setting
14584 last_arrow_position and last_arrow_string to t, which is
14585 unequal to any useful value of Voverlay_arrow_... */
14586 update_overlay_arrows (-1);
14590 /* Return value in display table DP (Lisp_Char_Table *) for character
14591 C. Since a display table doesn't have any parent, we don't have to
14592 follow parent. Do not call this function directly but use the
14593 macro DISP_CHAR_VECTOR. */
14595 Lisp_Object
14596 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14598 Lisp_Object val;
14600 if (ASCII_CHAR_P (c))
14602 val = dp->ascii;
14603 if (SUB_CHAR_TABLE_P (val))
14604 val = XSUB_CHAR_TABLE (val)->contents[c];
14606 else
14608 Lisp_Object table;
14610 XSETCHAR_TABLE (table, dp);
14611 val = char_table_ref (table, c);
14613 if (NILP (val))
14614 val = dp->defalt;
14615 return val;
14618 static int buffer_flip_blocked_depth;
14620 static void
14621 block_buffer_flips (void)
14623 eassert (buffer_flip_blocked_depth >= 0);
14624 buffer_flip_blocked_depth++;
14627 static void
14628 unblock_buffer_flips (void)
14630 eassert (buffer_flip_blocked_depth > 0);
14631 if (--buffer_flip_blocked_depth == 0)
14633 Lisp_Object tail, frame;
14634 block_input ();
14635 FOR_EACH_FRAME (tail, frame)
14637 struct frame *f = XFRAME (frame);
14638 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14639 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14641 unblock_input ();
14645 bool
14646 buffer_flipping_blocked_p (void)
14648 return buffer_flip_blocked_depth > 0;
14652 /***********************************************************************
14653 Window Redisplay
14654 ***********************************************************************/
14656 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14658 static void
14659 redisplay_windows (Lisp_Object window)
14661 while (!NILP (window))
14663 struct window *w = XWINDOW (window);
14665 if (WINDOWP (w->contents))
14666 redisplay_windows (w->contents);
14667 else if (BUFFERP (w->contents))
14669 displayed_buffer = XBUFFER (w->contents);
14670 /* Use list_of_error, not Qerror, so that
14671 we catch only errors and don't run the debugger. */
14672 internal_condition_case_1 (redisplay_window_0, window,
14673 list_of_error,
14674 redisplay_window_error);
14677 window = w->next;
14681 static Lisp_Object
14682 redisplay_window_error (Lisp_Object ignore)
14684 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14685 return Qnil;
14688 static Lisp_Object
14689 redisplay_window_0 (Lisp_Object window)
14691 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14692 redisplay_window (window, false);
14693 return Qnil;
14696 static Lisp_Object
14697 redisplay_window_1 (Lisp_Object window)
14699 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14700 redisplay_window (window, true);
14701 return Qnil;
14705 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14706 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14707 which positions recorded in ROW differ from current buffer
14708 positions.
14710 Return true iff cursor is on this row. */
14712 static bool
14713 set_cursor_from_row (struct window *w, struct glyph_row *row,
14714 struct glyph_matrix *matrix,
14715 ptrdiff_t delta, ptrdiff_t delta_bytes,
14716 int dy, int dvpos)
14718 struct glyph *glyph = row->glyphs[TEXT_AREA];
14719 struct glyph *end = glyph + row->used[TEXT_AREA];
14720 struct glyph *cursor = NULL;
14721 /* The last known character position in row. */
14722 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14723 int x = row->x;
14724 ptrdiff_t pt_old = PT - delta;
14725 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14726 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14727 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14728 /* A glyph beyond the edge of TEXT_AREA which we should never
14729 touch. */
14730 struct glyph *glyphs_end = end;
14731 /* True means we've found a match for cursor position, but that
14732 glyph has the avoid_cursor_p flag set. */
14733 bool match_with_avoid_cursor = false;
14734 /* True means we've seen at least one glyph that came from a
14735 display string. */
14736 bool string_seen = false;
14737 /* Largest and smallest buffer positions seen so far during scan of
14738 glyph row. */
14739 ptrdiff_t bpos_max = pos_before;
14740 ptrdiff_t bpos_min = pos_after;
14741 /* Last buffer position covered by an overlay string with an integer
14742 `cursor' property. */
14743 ptrdiff_t bpos_covered = 0;
14744 /* True means the display string on which to display the cursor
14745 comes from a text property, not from an overlay. */
14746 bool string_from_text_prop = false;
14748 /* Don't even try doing anything if called for a mode-line or
14749 header-line row, since the rest of the code isn't prepared to
14750 deal with such calamities. */
14751 eassert (!row->mode_line_p);
14752 if (row->mode_line_p)
14753 return false;
14755 /* Skip over glyphs not having an object at the start and the end of
14756 the row. These are special glyphs like truncation marks on
14757 terminal frames. */
14758 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14760 if (!row->reversed_p)
14762 while (glyph < end
14763 && NILP (glyph->object)
14764 && glyph->charpos < 0)
14766 x += glyph->pixel_width;
14767 ++glyph;
14769 while (end > glyph
14770 && NILP ((end - 1)->object)
14771 /* CHARPOS is zero for blanks and stretch glyphs
14772 inserted by extend_face_to_end_of_line. */
14773 && (end - 1)->charpos <= 0)
14774 --end;
14775 glyph_before = glyph - 1;
14776 glyph_after = end;
14778 else
14780 struct glyph *g;
14782 /* If the glyph row is reversed, we need to process it from back
14783 to front, so swap the edge pointers. */
14784 glyphs_end = end = glyph - 1;
14785 glyph += row->used[TEXT_AREA] - 1;
14787 while (glyph > end + 1
14788 && NILP (glyph->object)
14789 && glyph->charpos < 0)
14791 --glyph;
14792 x -= glyph->pixel_width;
14794 if (NILP (glyph->object) && glyph->charpos < 0)
14795 --glyph;
14796 /* By default, in reversed rows we put the cursor on the
14797 rightmost (first in the reading order) glyph. */
14798 for (g = end + 1; g < glyph; g++)
14799 x += g->pixel_width;
14800 while (end < glyph
14801 && NILP ((end + 1)->object)
14802 && (end + 1)->charpos <= 0)
14803 ++end;
14804 glyph_before = glyph + 1;
14805 glyph_after = end;
14808 else if (row->reversed_p)
14810 /* In R2L rows that don't display text, put the cursor on the
14811 rightmost glyph. Case in point: an empty last line that is
14812 part of an R2L paragraph. */
14813 cursor = end - 1;
14814 /* Avoid placing the cursor on the last glyph of the row, where
14815 on terminal frames we hold the vertical border between
14816 adjacent windows. */
14817 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14818 && !WINDOW_RIGHTMOST_P (w)
14819 && cursor == row->glyphs[LAST_AREA] - 1)
14820 cursor--;
14821 x = -1; /* will be computed below, at label compute_x */
14824 /* Step 1: Try to find the glyph whose character position
14825 corresponds to point. If that's not possible, find 2 glyphs
14826 whose character positions are the closest to point, one before
14827 point, the other after it. */
14828 if (!row->reversed_p)
14829 while (/* not marched to end of glyph row */
14830 glyph < end
14831 /* glyph was not inserted by redisplay for internal purposes */
14832 && !NILP (glyph->object))
14834 if (BUFFERP (glyph->object))
14836 ptrdiff_t dpos = glyph->charpos - pt_old;
14838 if (glyph->charpos > bpos_max)
14839 bpos_max = glyph->charpos;
14840 if (glyph->charpos < bpos_min)
14841 bpos_min = glyph->charpos;
14842 if (!glyph->avoid_cursor_p)
14844 /* If we hit point, we've found the glyph on which to
14845 display the cursor. */
14846 if (dpos == 0)
14848 match_with_avoid_cursor = false;
14849 break;
14851 /* See if we've found a better approximation to
14852 POS_BEFORE or to POS_AFTER. */
14853 if (0 > dpos && dpos > pos_before - pt_old)
14855 pos_before = glyph->charpos;
14856 glyph_before = glyph;
14858 else if (0 < dpos && dpos < pos_after - pt_old)
14860 pos_after = glyph->charpos;
14861 glyph_after = glyph;
14864 else if (dpos == 0)
14865 match_with_avoid_cursor = true;
14867 else if (STRINGP (glyph->object))
14869 Lisp_Object chprop;
14870 ptrdiff_t glyph_pos = glyph->charpos;
14872 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14873 glyph->object);
14874 if (!NILP (chprop))
14876 /* If the string came from a `display' text property,
14877 look up the buffer position of that property and
14878 use that position to update bpos_max, as if we
14879 actually saw such a position in one of the row's
14880 glyphs. This helps with supporting integer values
14881 of `cursor' property on the display string in
14882 situations where most or all of the row's buffer
14883 text is completely covered by display properties,
14884 so that no glyph with valid buffer positions is
14885 ever seen in the row. */
14886 ptrdiff_t prop_pos =
14887 string_buffer_position_lim (glyph->object, pos_before,
14888 pos_after, false);
14890 if (prop_pos >= pos_before)
14891 bpos_max = prop_pos;
14893 if (INTEGERP (chprop))
14895 bpos_covered = bpos_max + XINT (chprop);
14896 /* If the `cursor' property covers buffer positions up
14897 to and including point, we should display cursor on
14898 this glyph. Note that, if a `cursor' property on one
14899 of the string's characters has an integer value, we
14900 will break out of the loop below _before_ we get to
14901 the position match above. IOW, integer values of
14902 the `cursor' property override the "exact match for
14903 point" strategy of positioning the cursor. */
14904 /* Implementation note: bpos_max == pt_old when, e.g.,
14905 we are in an empty line, where bpos_max is set to
14906 MATRIX_ROW_START_CHARPOS, see above. */
14907 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14909 cursor = glyph;
14910 break;
14914 string_seen = true;
14916 x += glyph->pixel_width;
14917 ++glyph;
14919 else if (glyph > end) /* row is reversed */
14920 while (!NILP (glyph->object))
14922 if (BUFFERP (glyph->object))
14924 ptrdiff_t dpos = glyph->charpos - pt_old;
14926 if (glyph->charpos > bpos_max)
14927 bpos_max = glyph->charpos;
14928 if (glyph->charpos < bpos_min)
14929 bpos_min = glyph->charpos;
14930 if (!glyph->avoid_cursor_p)
14932 if (dpos == 0)
14934 match_with_avoid_cursor = false;
14935 break;
14937 if (0 > dpos && dpos > pos_before - pt_old)
14939 pos_before = glyph->charpos;
14940 glyph_before = glyph;
14942 else if (0 < dpos && dpos < pos_after - pt_old)
14944 pos_after = glyph->charpos;
14945 glyph_after = glyph;
14948 else if (dpos == 0)
14949 match_with_avoid_cursor = true;
14951 else if (STRINGP (glyph->object))
14953 Lisp_Object chprop;
14954 ptrdiff_t glyph_pos = glyph->charpos;
14956 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14957 glyph->object);
14958 if (!NILP (chprop))
14960 ptrdiff_t prop_pos =
14961 string_buffer_position_lim (glyph->object, pos_before,
14962 pos_after, false);
14964 if (prop_pos >= pos_before)
14965 bpos_max = prop_pos;
14967 if (INTEGERP (chprop))
14969 bpos_covered = bpos_max + XINT (chprop);
14970 /* If the `cursor' property covers buffer positions up
14971 to and including point, we should display cursor on
14972 this glyph. */
14973 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14975 cursor = glyph;
14976 break;
14979 string_seen = true;
14981 --glyph;
14982 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14984 x--; /* can't use any pixel_width */
14985 break;
14987 x -= glyph->pixel_width;
14990 /* Step 2: If we didn't find an exact match for point, we need to
14991 look for a proper place to put the cursor among glyphs between
14992 GLYPH_BEFORE and GLYPH_AFTER. */
14993 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14994 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14995 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14997 /* An empty line has a single glyph whose OBJECT is nil and
14998 whose CHARPOS is the position of a newline on that line.
14999 Note that on a TTY, there are more glyphs after that, which
15000 were produced by extend_face_to_end_of_line, but their
15001 CHARPOS is zero or negative. */
15002 bool empty_line_p =
15003 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15004 && NILP (glyph->object) && glyph->charpos > 0
15005 /* On a TTY, continued and truncated rows also have a glyph at
15006 their end whose OBJECT is nil and whose CHARPOS is
15007 positive (the continuation and truncation glyphs), but such
15008 rows are obviously not "empty". */
15009 && !(row->continued_p || row->truncated_on_right_p));
15011 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15013 ptrdiff_t ellipsis_pos;
15015 /* Scan back over the ellipsis glyphs. */
15016 if (!row->reversed_p)
15018 ellipsis_pos = (glyph - 1)->charpos;
15019 while (glyph > row->glyphs[TEXT_AREA]
15020 && (glyph - 1)->charpos == ellipsis_pos)
15021 glyph--, x -= glyph->pixel_width;
15022 /* That loop always goes one position too far, including
15023 the glyph before the ellipsis. So scan forward over
15024 that one. */
15025 x += glyph->pixel_width;
15026 glyph++;
15028 else /* row is reversed */
15030 ellipsis_pos = (glyph + 1)->charpos;
15031 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15032 && (glyph + 1)->charpos == ellipsis_pos)
15033 glyph++, x += glyph->pixel_width;
15034 x -= glyph->pixel_width;
15035 glyph--;
15038 else if (match_with_avoid_cursor)
15040 cursor = glyph_after;
15041 x = -1;
15043 else if (string_seen)
15045 int incr = row->reversed_p ? -1 : +1;
15047 /* Need to find the glyph that came out of a string which is
15048 present at point. That glyph is somewhere between
15049 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15050 positioned between POS_BEFORE and POS_AFTER in the
15051 buffer. */
15052 struct glyph *start, *stop;
15053 ptrdiff_t pos = pos_before;
15055 x = -1;
15057 /* If the row ends in a newline from a display string,
15058 reordering could have moved the glyphs belonging to the
15059 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15060 in this case we extend the search to the last glyph in
15061 the row that was not inserted by redisplay. */
15062 if (row->ends_in_newline_from_string_p)
15064 glyph_after = end;
15065 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15068 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15069 correspond to POS_BEFORE and POS_AFTER, respectively. We
15070 need START and STOP in the order that corresponds to the
15071 row's direction as given by its reversed_p flag. If the
15072 directionality of characters between POS_BEFORE and
15073 POS_AFTER is the opposite of the row's base direction,
15074 these characters will have been reordered for display,
15075 and we need to reverse START and STOP. */
15076 if (!row->reversed_p)
15078 start = min (glyph_before, glyph_after);
15079 stop = max (glyph_before, glyph_after);
15081 else
15083 start = max (glyph_before, glyph_after);
15084 stop = min (glyph_before, glyph_after);
15086 for (glyph = start + incr;
15087 row->reversed_p ? glyph > stop : glyph < stop; )
15090 /* Any glyphs that come from the buffer are here because
15091 of bidi reordering. Skip them, and only pay
15092 attention to glyphs that came from some string. */
15093 if (STRINGP (glyph->object))
15095 Lisp_Object str;
15096 ptrdiff_t tem;
15097 /* If the display property covers the newline, we
15098 need to search for it one position farther. */
15099 ptrdiff_t lim = pos_after
15100 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15102 string_from_text_prop = false;
15103 str = glyph->object;
15104 tem = string_buffer_position_lim (str, pos, lim, false);
15105 if (tem == 0 /* from overlay */
15106 || pos <= tem)
15108 /* If the string from which this glyph came is
15109 found in the buffer at point, or at position
15110 that is closer to point than pos_after, then
15111 we've found the glyph we've been looking for.
15112 If it comes from an overlay (tem == 0), and
15113 it has the `cursor' property on one of its
15114 glyphs, record that glyph as a candidate for
15115 displaying the cursor. (As in the
15116 unidirectional version, we will display the
15117 cursor on the last candidate we find.) */
15118 if (tem == 0
15119 || tem == pt_old
15120 || (tem - pt_old > 0 && tem < pos_after))
15122 /* The glyphs from this string could have
15123 been reordered. Find the one with the
15124 smallest string position. Or there could
15125 be a character in the string with the
15126 `cursor' property, which means display
15127 cursor on that character's glyph. */
15128 ptrdiff_t strpos = glyph->charpos;
15130 if (tem)
15132 cursor = glyph;
15133 string_from_text_prop = true;
15135 for ( ;
15136 (row->reversed_p ? glyph > stop : glyph < stop)
15137 && EQ (glyph->object, str);
15138 glyph += incr)
15140 Lisp_Object cprop;
15141 ptrdiff_t gpos = glyph->charpos;
15143 cprop = Fget_char_property (make_number (gpos),
15144 Qcursor,
15145 glyph->object);
15146 if (!NILP (cprop))
15148 cursor = glyph;
15149 break;
15151 if (tem && glyph->charpos < strpos)
15153 strpos = glyph->charpos;
15154 cursor = glyph;
15158 if (tem == pt_old
15159 || (tem - pt_old > 0 && tem < pos_after))
15160 goto compute_x;
15162 if (tem)
15163 pos = tem + 1; /* don't find previous instances */
15165 /* This string is not what we want; skip all of the
15166 glyphs that came from it. */
15167 while ((row->reversed_p ? glyph > stop : glyph < stop)
15168 && EQ (glyph->object, str))
15169 glyph += incr;
15171 else
15172 glyph += incr;
15175 /* If we reached the end of the line, and END was from a string,
15176 the cursor is not on this line. */
15177 if (cursor == NULL
15178 && (row->reversed_p ? glyph <= end : glyph >= end)
15179 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15180 && STRINGP (end->object)
15181 && row->continued_p)
15182 return false;
15184 /* A truncated row may not include PT among its character positions.
15185 Setting the cursor inside the scroll margin will trigger
15186 recalculation of hscroll in hscroll_window_tree. But if a
15187 display string covers point, defer to the string-handling
15188 code below to figure this out. */
15189 else if (row->truncated_on_left_p && pt_old < bpos_min)
15191 cursor = glyph_before;
15192 x = -1;
15194 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15195 /* Zero-width characters produce no glyphs. */
15196 || (!empty_line_p
15197 && (row->reversed_p
15198 ? glyph_after > glyphs_end
15199 : glyph_after < glyphs_end)))
15201 cursor = glyph_after;
15202 x = -1;
15206 compute_x:
15207 if (cursor != NULL)
15208 glyph = cursor;
15209 else if (glyph == glyphs_end
15210 && pos_before == pos_after
15211 && STRINGP ((row->reversed_p
15212 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15213 : row->glyphs[TEXT_AREA])->object))
15215 /* If all the glyphs of this row came from strings, put the
15216 cursor on the first glyph of the row. This avoids having the
15217 cursor outside of the text area in this very rare and hard
15218 use case. */
15219 glyph =
15220 row->reversed_p
15221 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15222 : row->glyphs[TEXT_AREA];
15224 if (x < 0)
15226 struct glyph *g;
15228 /* Need to compute x that corresponds to GLYPH. */
15229 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15231 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15232 emacs_abort ();
15233 x += g->pixel_width;
15237 /* ROW could be part of a continued line, which, under bidi
15238 reordering, might have other rows whose start and end charpos
15239 occlude point. Only set w->cursor if we found a better
15240 approximation to the cursor position than we have from previously
15241 examined candidate rows belonging to the same continued line. */
15242 if (/* We already have a candidate row. */
15243 w->cursor.vpos >= 0
15244 /* That candidate is not the row we are processing. */
15245 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15246 /* Make sure cursor.vpos specifies a row whose start and end
15247 charpos occlude point, and it is valid candidate for being a
15248 cursor-row. This is because some callers of this function
15249 leave cursor.vpos at the row where the cursor was displayed
15250 during the last redisplay cycle. */
15251 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15252 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15253 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15255 struct glyph *g1
15256 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15258 /* Don't consider glyphs that are outside TEXT_AREA. */
15259 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15260 return false;
15261 /* Keep the candidate whose buffer position is the closest to
15262 point or has the `cursor' property. */
15263 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15264 w->cursor.hpos >= 0
15265 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15266 && ((BUFFERP (g1->object)
15267 && (g1->charpos == pt_old /* An exact match always wins. */
15268 || (BUFFERP (glyph->object)
15269 && eabs (g1->charpos - pt_old)
15270 < eabs (glyph->charpos - pt_old))))
15271 /* Previous candidate is a glyph from a string that has
15272 a non-nil `cursor' property. */
15273 || (STRINGP (g1->object)
15274 && (!NILP (Fget_char_property (make_number (g1->charpos),
15275 Qcursor, g1->object))
15276 /* Previous candidate is from the same display
15277 string as this one, and the display string
15278 came from a text property. */
15279 || (EQ (g1->object, glyph->object)
15280 && string_from_text_prop)
15281 /* this candidate is from newline and its
15282 position is not an exact match */
15283 || (NILP (glyph->object)
15284 && glyph->charpos != pt_old)))))
15285 return false;
15286 /* If this candidate gives an exact match, use that. */
15287 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15288 /* If this candidate is a glyph created for the
15289 terminating newline of a line, and point is on that
15290 newline, it wins because it's an exact match. */
15291 || (!row->continued_p
15292 && NILP (glyph->object)
15293 && glyph->charpos == 0
15294 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15295 /* Otherwise, keep the candidate that comes from a row
15296 spanning less buffer positions. This may win when one or
15297 both candidate positions are on glyphs that came from
15298 display strings, for which we cannot compare buffer
15299 positions. */
15300 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15301 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15302 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15303 return false;
15305 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15306 w->cursor.x = x;
15307 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15308 w->cursor.y = row->y + dy;
15310 if (w == XWINDOW (selected_window))
15312 if (!row->continued_p
15313 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15314 && row->x == 0)
15316 this_line_buffer = XBUFFER (w->contents);
15318 CHARPOS (this_line_start_pos)
15319 = MATRIX_ROW_START_CHARPOS (row) + delta;
15320 BYTEPOS (this_line_start_pos)
15321 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15323 CHARPOS (this_line_end_pos)
15324 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15325 BYTEPOS (this_line_end_pos)
15326 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15328 this_line_y = w->cursor.y;
15329 this_line_pixel_height = row->height;
15330 this_line_vpos = w->cursor.vpos;
15331 this_line_start_x = row->x;
15333 else
15334 CHARPOS (this_line_start_pos) = 0;
15337 return true;
15341 /* Run window scroll functions, if any, for WINDOW with new window
15342 start STARTP. Sets the window start of WINDOW to that position.
15344 We assume that the window's buffer is really current. */
15346 static struct text_pos
15347 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15349 struct window *w = XWINDOW (window);
15350 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15352 eassert (current_buffer == XBUFFER (w->contents));
15354 if (!NILP (Vwindow_scroll_functions))
15356 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15357 make_number (CHARPOS (startp)));
15358 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15359 /* In case the hook functions switch buffers. */
15360 set_buffer_internal (XBUFFER (w->contents));
15363 return startp;
15367 /* Make sure the line containing the cursor is fully visible.
15368 A value of true means there is nothing to be done.
15369 (Either the line is fully visible, or it cannot be made so,
15370 or we cannot tell.)
15372 If FORCE_P, return false even if partial visible cursor row
15373 is higher than window.
15375 If CURRENT_MATRIX_P, use the information from the
15376 window's current glyph matrix; otherwise use the desired glyph
15377 matrix.
15379 A value of false means the caller should do scrolling
15380 as if point had gone off the screen. */
15382 static bool
15383 cursor_row_fully_visible_p (struct window *w, bool force_p,
15384 bool current_matrix_p)
15386 struct glyph_matrix *matrix;
15387 struct glyph_row *row;
15388 int window_height;
15390 if (!make_cursor_line_fully_visible_p)
15391 return true;
15393 /* It's not always possible to find the cursor, e.g, when a window
15394 is full of overlay strings. Don't do anything in that case. */
15395 if (w->cursor.vpos < 0)
15396 return true;
15398 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15399 row = MATRIX_ROW (matrix, w->cursor.vpos);
15401 /* If the cursor row is not partially visible, there's nothing to do. */
15402 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15403 return true;
15405 /* If the row the cursor is in is taller than the window's height,
15406 it's not clear what to do, so do nothing. */
15407 window_height = window_box_height (w);
15408 if (row->height >= window_height)
15410 if (!force_p || MINI_WINDOW_P (w)
15411 || w->vscroll || w->cursor.vpos == 0)
15412 return true;
15414 return false;
15418 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15419 means only WINDOW is redisplayed in redisplay_internal.
15420 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15421 in redisplay_window to bring a partially visible line into view in
15422 the case that only the cursor has moved.
15424 LAST_LINE_MISFIT should be true if we're scrolling because the
15425 last screen line's vertical height extends past the end of the screen.
15427 Value is
15429 1 if scrolling succeeded
15431 0 if scrolling didn't find point.
15433 -1 if new fonts have been loaded so that we must interrupt
15434 redisplay, adjust glyph matrices, and try again. */
15436 enum
15438 SCROLLING_SUCCESS,
15439 SCROLLING_FAILED,
15440 SCROLLING_NEED_LARGER_MATRICES
15443 /* If scroll-conservatively is more than this, never recenter.
15445 If you change this, don't forget to update the doc string of
15446 `scroll-conservatively' and the Emacs manual. */
15447 #define SCROLL_LIMIT 100
15449 static int
15450 try_scrolling (Lisp_Object window, bool just_this_one_p,
15451 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15452 bool temp_scroll_step, bool last_line_misfit)
15454 struct window *w = XWINDOW (window);
15455 struct text_pos pos, startp;
15456 struct it it;
15457 int this_scroll_margin, scroll_max, rc, height;
15458 int dy = 0, amount_to_scroll = 0;
15459 bool scroll_down_p = false;
15460 int extra_scroll_margin_lines = last_line_misfit;
15461 Lisp_Object aggressive;
15462 /* We will never try scrolling more than this number of lines. */
15463 int scroll_limit = SCROLL_LIMIT;
15464 int frame_line_height = default_line_pixel_height (w);
15466 #ifdef GLYPH_DEBUG
15467 debug_method_add (w, "try_scrolling");
15468 #endif
15470 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15472 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15474 /* Force arg_scroll_conservatively to have a reasonable value, to
15475 avoid scrolling too far away with slow move_it_* functions. Note
15476 that the user can supply scroll-conservatively equal to
15477 `most-positive-fixnum', which can be larger than INT_MAX. */
15478 if (arg_scroll_conservatively > scroll_limit)
15480 arg_scroll_conservatively = scroll_limit + 1;
15481 scroll_max = scroll_limit * frame_line_height;
15483 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15484 /* Compute how much we should try to scroll maximally to bring
15485 point into view. */
15486 scroll_max = (max (scroll_step,
15487 max (arg_scroll_conservatively, temp_scroll_step))
15488 * frame_line_height);
15489 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15490 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15491 /* We're trying to scroll because of aggressive scrolling but no
15492 scroll_step is set. Choose an arbitrary one. */
15493 scroll_max = 10 * frame_line_height;
15494 else
15495 scroll_max = 0;
15497 too_near_end:
15499 /* Decide whether to scroll down. */
15500 if (PT > CHARPOS (startp))
15502 int scroll_margin_y;
15504 /* Compute the pixel ypos of the scroll margin, then move IT to
15505 either that ypos or PT, whichever comes first. */
15506 start_display (&it, w, startp);
15507 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15508 - this_scroll_margin
15509 - frame_line_height * extra_scroll_margin_lines;
15510 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15511 (MOVE_TO_POS | MOVE_TO_Y));
15513 if (PT > CHARPOS (it.current.pos))
15515 int y0 = line_bottom_y (&it);
15516 /* Compute how many pixels below window bottom to stop searching
15517 for PT. This avoids costly search for PT that is far away if
15518 the user limited scrolling by a small number of lines, but
15519 always finds PT if scroll_conservatively is set to a large
15520 number, such as most-positive-fixnum. */
15521 int slack = max (scroll_max, 10 * frame_line_height);
15522 int y_to_move = it.last_visible_y + slack;
15524 /* Compute the distance from the scroll margin to PT or to
15525 the scroll limit, whichever comes first. This should
15526 include the height of the cursor line, to make that line
15527 fully visible. */
15528 move_it_to (&it, PT, -1, y_to_move,
15529 -1, MOVE_TO_POS | MOVE_TO_Y);
15530 dy = line_bottom_y (&it) - y0;
15532 if (dy > scroll_max)
15533 return SCROLLING_FAILED;
15535 if (dy > 0)
15536 scroll_down_p = true;
15538 else if (PT == IT_CHARPOS (it)
15539 && IT_CHARPOS (it) < ZV
15540 && it.method == GET_FROM_STRING
15541 && arg_scroll_conservatively > scroll_limit
15542 && it.current_x == 0)
15544 enum move_it_result skip;
15545 int y1 = it.current_y;
15546 int vpos;
15548 /* A before-string that includes newlines and is displayed
15549 on the last visible screen line could fail us under
15550 scroll-conservatively > 100, because we will be unable to
15551 position the cursor on that last visible line. Try to
15552 recover by finding the first screen line that has some
15553 glyphs coming from the buffer text. */
15554 do {
15555 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15556 if (skip != MOVE_NEWLINE_OR_CR
15557 || IT_CHARPOS (it) != PT
15558 || it.method == GET_FROM_BUFFER)
15559 break;
15560 vpos = it.vpos;
15561 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15562 } while (it.vpos > vpos);
15564 dy = it.current_y - y1;
15566 if (dy > scroll_max)
15567 return SCROLLING_FAILED;
15569 if (dy > 0)
15570 scroll_down_p = true;
15574 if (scroll_down_p)
15576 /* Point is in or below the bottom scroll margin, so move the
15577 window start down. If scrolling conservatively, move it just
15578 enough down to make point visible. If scroll_step is set,
15579 move it down by scroll_step. */
15580 if (arg_scroll_conservatively)
15581 amount_to_scroll
15582 = min (max (dy, frame_line_height),
15583 frame_line_height * arg_scroll_conservatively);
15584 else if (scroll_step || temp_scroll_step)
15585 amount_to_scroll = scroll_max;
15586 else
15588 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15589 height = WINDOW_BOX_TEXT_HEIGHT (w);
15590 if (NUMBERP (aggressive))
15592 double float_amount = XFLOATINT (aggressive) * height;
15593 int aggressive_scroll = float_amount;
15594 if (aggressive_scroll == 0 && float_amount > 0)
15595 aggressive_scroll = 1;
15596 /* Don't let point enter the scroll margin near top of
15597 the window. This could happen if the value of
15598 scroll_up_aggressively is too large and there are
15599 non-zero margins, because scroll_up_aggressively
15600 means put point that fraction of window height
15601 _from_the_bottom_margin_. */
15602 if (aggressive_scroll + 2 * this_scroll_margin > height)
15603 aggressive_scroll = height - 2 * this_scroll_margin;
15604 amount_to_scroll = dy + aggressive_scroll;
15608 if (amount_to_scroll <= 0)
15609 return SCROLLING_FAILED;
15611 start_display (&it, w, startp);
15612 if (arg_scroll_conservatively <= scroll_limit)
15613 move_it_vertically (&it, amount_to_scroll);
15614 else
15616 /* Extra precision for users who set scroll-conservatively
15617 to a large number: make sure the amount we scroll
15618 the window start is never less than amount_to_scroll,
15619 which was computed as distance from window bottom to
15620 point. This matters when lines at window top and lines
15621 below window bottom have different height. */
15622 struct it it1;
15623 void *it1data = NULL;
15624 /* We use a temporary it1 because line_bottom_y can modify
15625 its argument, if it moves one line down; see there. */
15626 int start_y;
15628 SAVE_IT (it1, it, it1data);
15629 start_y = line_bottom_y (&it1);
15630 do {
15631 RESTORE_IT (&it, &it, it1data);
15632 move_it_by_lines (&it, 1);
15633 SAVE_IT (it1, it, it1data);
15634 } while (IT_CHARPOS (it) < ZV
15635 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15636 bidi_unshelve_cache (it1data, true);
15639 /* If STARTP is unchanged, move it down another screen line. */
15640 if (IT_CHARPOS (it) == CHARPOS (startp))
15641 move_it_by_lines (&it, 1);
15642 startp = it.current.pos;
15644 else
15646 struct text_pos scroll_margin_pos = startp;
15647 int y_offset = 0;
15649 /* See if point is inside the scroll margin at the top of the
15650 window. */
15651 if (this_scroll_margin)
15653 int y_start;
15655 start_display (&it, w, startp);
15656 y_start = it.current_y;
15657 move_it_vertically (&it, this_scroll_margin);
15658 scroll_margin_pos = it.current.pos;
15659 /* If we didn't move enough before hitting ZV, request
15660 additional amount of scroll, to move point out of the
15661 scroll margin. */
15662 if (IT_CHARPOS (it) == ZV
15663 && it.current_y - y_start < this_scroll_margin)
15664 y_offset = this_scroll_margin - (it.current_y - y_start);
15667 if (PT < CHARPOS (scroll_margin_pos))
15669 /* Point is in the scroll margin at the top of the window or
15670 above what is displayed in the window. */
15671 int y0, y_to_move;
15673 /* Compute the vertical distance from PT to the scroll
15674 margin position. Move as far as scroll_max allows, or
15675 one screenful, or 10 screen lines, whichever is largest.
15676 Give up if distance is greater than scroll_max or if we
15677 didn't reach the scroll margin position. */
15678 SET_TEXT_POS (pos, PT, PT_BYTE);
15679 start_display (&it, w, pos);
15680 y0 = it.current_y;
15681 y_to_move = max (it.last_visible_y,
15682 max (scroll_max, 10 * frame_line_height));
15683 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15684 y_to_move, -1,
15685 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15686 dy = it.current_y - y0;
15687 if (dy > scroll_max
15688 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15689 return SCROLLING_FAILED;
15691 /* Additional scroll for when ZV was too close to point. */
15692 dy += y_offset;
15694 /* Compute new window start. */
15695 start_display (&it, w, startp);
15697 if (arg_scroll_conservatively)
15698 amount_to_scroll = max (dy, frame_line_height
15699 * max (scroll_step, temp_scroll_step));
15700 else if (scroll_step || temp_scroll_step)
15701 amount_to_scroll = scroll_max;
15702 else
15704 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15705 height = WINDOW_BOX_TEXT_HEIGHT (w);
15706 if (NUMBERP (aggressive))
15708 double float_amount = XFLOATINT (aggressive) * height;
15709 int aggressive_scroll = float_amount;
15710 if (aggressive_scroll == 0 && float_amount > 0)
15711 aggressive_scroll = 1;
15712 /* Don't let point enter the scroll margin near
15713 bottom of the window, if the value of
15714 scroll_down_aggressively happens to be too
15715 large. */
15716 if (aggressive_scroll + 2 * this_scroll_margin > height)
15717 aggressive_scroll = height - 2 * this_scroll_margin;
15718 amount_to_scroll = dy + aggressive_scroll;
15722 if (amount_to_scroll <= 0)
15723 return SCROLLING_FAILED;
15725 move_it_vertically_backward (&it, amount_to_scroll);
15726 startp = it.current.pos;
15730 /* Run window scroll functions. */
15731 startp = run_window_scroll_functions (window, startp);
15733 /* Display the window. Give up if new fonts are loaded, or if point
15734 doesn't appear. */
15735 if (!try_window (window, startp, 0))
15736 rc = SCROLLING_NEED_LARGER_MATRICES;
15737 else if (w->cursor.vpos < 0)
15739 clear_glyph_matrix (w->desired_matrix);
15740 rc = SCROLLING_FAILED;
15742 else
15744 /* Maybe forget recorded base line for line number display. */
15745 if (!just_this_one_p
15746 || current_buffer->clip_changed
15747 || BEG_UNCHANGED < CHARPOS (startp))
15748 w->base_line_number = 0;
15750 /* If cursor ends up on a partially visible line,
15751 treat that as being off the bottom of the screen. */
15752 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15753 false)
15754 /* It's possible that the cursor is on the first line of the
15755 buffer, which is partially obscured due to a vscroll
15756 (Bug#7537). In that case, avoid looping forever. */
15757 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15759 clear_glyph_matrix (w->desired_matrix);
15760 ++extra_scroll_margin_lines;
15761 goto too_near_end;
15763 rc = SCROLLING_SUCCESS;
15766 return rc;
15770 /* Compute a suitable window start for window W if display of W starts
15771 on a continuation line. Value is true if a new window start
15772 was computed.
15774 The new window start will be computed, based on W's width, starting
15775 from the start of the continued line. It is the start of the
15776 screen line with the minimum distance from the old start W->start,
15777 which is still before point (otherwise point will definitely not
15778 be visible in the window). */
15780 static bool
15781 compute_window_start_on_continuation_line (struct window *w)
15783 struct text_pos pos, start_pos, pos_before_pt;
15784 bool window_start_changed_p = false;
15786 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15788 /* If window start is on a continuation line... Window start may be
15789 < BEGV in case there's invisible text at the start of the
15790 buffer (M-x rmail, for example). */
15791 if (CHARPOS (start_pos) > BEGV
15792 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15794 struct it it;
15795 struct glyph_row *row;
15797 /* Handle the case that the window start is out of range. */
15798 if (CHARPOS (start_pos) < BEGV)
15799 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15800 else if (CHARPOS (start_pos) > ZV)
15801 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15803 /* Find the start of the continued line. This should be fast
15804 because find_newline is fast (newline cache). */
15805 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15806 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15807 row, DEFAULT_FACE_ID);
15808 reseat_at_previous_visible_line_start (&it);
15810 /* If the line start is "too far" away from the window start,
15811 say it takes too much time to compute a new window start.
15812 Also, give up if the line start is after point, as in that
15813 case point will not be visible with any window start we
15814 compute. */
15815 if (IT_CHARPOS (it) <= PT
15816 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15817 /* PXW: Do we need upper bounds here? */
15818 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15820 int min_distance, distance;
15822 /* Move forward by display lines to find the new window
15823 start. If window width was enlarged, the new start can
15824 be expected to be > the old start. If window width was
15825 decreased, the new window start will be < the old start.
15826 So, we're looking for the display line start with the
15827 minimum distance from the old window start. */
15828 pos_before_pt = pos = it.current.pos;
15829 min_distance = DISP_INFINITY;
15830 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15831 distance < min_distance)
15833 min_distance = distance;
15834 if (CHARPOS (pos) <= PT)
15835 pos_before_pt = pos;
15836 pos = it.current.pos;
15837 if (it.line_wrap == WORD_WRAP)
15839 /* Under WORD_WRAP, move_it_by_lines is likely to
15840 overshoot and stop not at the first, but the
15841 second character from the left margin. So in
15842 that case, we need a more tight control on the X
15843 coordinate of the iterator than move_it_by_lines
15844 promises in its contract. The method is to first
15845 go to the last (rightmost) visible character of a
15846 line, then move to the leftmost character on the
15847 next line in a separate call. */
15848 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15849 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15850 move_it_to (&it, ZV, 0,
15851 it.current_y + it.max_ascent + it.max_descent, -1,
15852 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15854 else
15855 move_it_by_lines (&it, 1);
15858 /* It makes very little sense to make the new window start
15859 after point, as point won't be visible. If that's what
15860 the loop above finds, fall back on the candidate before
15861 or at point that is closest to the old window start. */
15862 if (CHARPOS (pos) > PT)
15863 pos = pos_before_pt;
15865 /* Set the window start there. */
15866 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15867 window_start_changed_p = true;
15871 return window_start_changed_p;
15875 /* Try cursor movement in case text has not changed in window WINDOW,
15876 with window start STARTP. Value is
15878 CURSOR_MOVEMENT_SUCCESS if successful
15880 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15882 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15883 display. *SCROLL_STEP is set to true, under certain circumstances, if
15884 we want to scroll as if scroll-step were set to 1. See the code.
15886 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15887 which case we have to abort this redisplay, and adjust matrices
15888 first. */
15890 enum
15892 CURSOR_MOVEMENT_SUCCESS,
15893 CURSOR_MOVEMENT_CANNOT_BE_USED,
15894 CURSOR_MOVEMENT_MUST_SCROLL,
15895 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15898 static int
15899 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15900 bool *scroll_step)
15902 struct window *w = XWINDOW (window);
15903 struct frame *f = XFRAME (w->frame);
15904 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15906 #ifdef GLYPH_DEBUG
15907 if (inhibit_try_cursor_movement)
15908 return rc;
15909 #endif
15911 /* Previously, there was a check for Lisp integer in the
15912 if-statement below. Now, this field is converted to
15913 ptrdiff_t, thus zero means invalid position in a buffer. */
15914 eassert (w->last_point > 0);
15915 /* Likewise there was a check whether window_end_vpos is nil or larger
15916 than the window. Now window_end_vpos is int and so never nil, but
15917 let's leave eassert to check whether it fits in the window. */
15918 eassert (!w->window_end_valid
15919 || w->window_end_vpos < w->current_matrix->nrows);
15921 /* Handle case where text has not changed, only point, and it has
15922 not moved off the frame. */
15923 if (/* Point may be in this window. */
15924 PT >= CHARPOS (startp)
15925 /* Selective display hasn't changed. */
15926 && !current_buffer->clip_changed
15927 /* Function force-mode-line-update is used to force a thorough
15928 redisplay. It sets either windows_or_buffers_changed or
15929 update_mode_lines. So don't take a shortcut here for these
15930 cases. */
15931 && !update_mode_lines
15932 && !windows_or_buffers_changed
15933 && !f->cursor_type_changed
15934 && NILP (Vshow_trailing_whitespace)
15935 /* This code is not used for mini-buffer for the sake of the case
15936 of redisplaying to replace an echo area message; since in
15937 that case the mini-buffer contents per se are usually
15938 unchanged. This code is of no real use in the mini-buffer
15939 since the handling of this_line_start_pos, etc., in redisplay
15940 handles the same cases. */
15941 && !EQ (window, minibuf_window)
15942 && (FRAME_WINDOW_P (f)
15943 || !overlay_arrow_in_current_buffer_p ()))
15945 int this_scroll_margin, top_scroll_margin;
15946 struct glyph_row *row = NULL;
15948 #ifdef GLYPH_DEBUG
15949 debug_method_add (w, "cursor movement");
15950 #endif
15952 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15954 top_scroll_margin = this_scroll_margin;
15955 if (WINDOW_WANTS_HEADER_LINE_P (w))
15956 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15958 /* Start with the row the cursor was displayed during the last
15959 not paused redisplay. Give up if that row is not valid. */
15960 if (w->last_cursor_vpos < 0
15961 || w->last_cursor_vpos >= w->current_matrix->nrows)
15962 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15963 else
15965 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15966 if (row->mode_line_p)
15967 ++row;
15968 if (!row->enabled_p)
15969 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15972 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15974 bool scroll_p = false, must_scroll = false;
15975 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15977 if (PT > w->last_point)
15979 /* Point has moved forward. */
15980 while (MATRIX_ROW_END_CHARPOS (row) < PT
15981 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15983 eassert (row->enabled_p);
15984 ++row;
15987 /* If the end position of a row equals the start
15988 position of the next row, and PT is at that position,
15989 we would rather display cursor in the next line. */
15990 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15991 && MATRIX_ROW_END_CHARPOS (row) == PT
15992 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15993 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15994 && !cursor_row_p (row))
15995 ++row;
15997 /* If within the scroll margin, scroll. Note that
15998 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15999 the next line would be drawn, and that
16000 this_scroll_margin can be zero. */
16001 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16002 || PT > MATRIX_ROW_END_CHARPOS (row)
16003 /* Line is completely visible last line in window
16004 and PT is to be set in the next line. */
16005 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16006 && PT == MATRIX_ROW_END_CHARPOS (row)
16007 && !row->ends_at_zv_p
16008 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16009 scroll_p = true;
16011 else if (PT < w->last_point)
16013 /* Cursor has to be moved backward. Note that PT >=
16014 CHARPOS (startp) because of the outer if-statement. */
16015 while (!row->mode_line_p
16016 && (MATRIX_ROW_START_CHARPOS (row) > PT
16017 || (MATRIX_ROW_START_CHARPOS (row) == PT
16018 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16019 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16020 row > w->current_matrix->rows
16021 && (row-1)->ends_in_newline_from_string_p))))
16022 && (row->y > top_scroll_margin
16023 || CHARPOS (startp) == BEGV))
16025 eassert (row->enabled_p);
16026 --row;
16029 /* Consider the following case: Window starts at BEGV,
16030 there is invisible, intangible text at BEGV, so that
16031 display starts at some point START > BEGV. It can
16032 happen that we are called with PT somewhere between
16033 BEGV and START. Try to handle that case. */
16034 if (row < w->current_matrix->rows
16035 || row->mode_line_p)
16037 row = w->current_matrix->rows;
16038 if (row->mode_line_p)
16039 ++row;
16042 /* Due to newlines in overlay strings, we may have to
16043 skip forward over overlay strings. */
16044 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16045 && MATRIX_ROW_END_CHARPOS (row) == PT
16046 && !cursor_row_p (row))
16047 ++row;
16049 /* If within the scroll margin, scroll. */
16050 if (row->y < top_scroll_margin
16051 && CHARPOS (startp) != BEGV)
16052 scroll_p = true;
16054 else
16056 /* Cursor did not move. So don't scroll even if cursor line
16057 is partially visible, as it was so before. */
16058 rc = CURSOR_MOVEMENT_SUCCESS;
16061 if (PT < MATRIX_ROW_START_CHARPOS (row)
16062 || PT > MATRIX_ROW_END_CHARPOS (row))
16064 /* if PT is not in the glyph row, give up. */
16065 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16066 must_scroll = true;
16068 else if (rc != CURSOR_MOVEMENT_SUCCESS
16069 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16071 struct glyph_row *row1;
16073 /* If rows are bidi-reordered and point moved, back up
16074 until we find a row that does not belong to a
16075 continuation line. This is because we must consider
16076 all rows of a continued line as candidates for the
16077 new cursor positioning, since row start and end
16078 positions change non-linearly with vertical position
16079 in such rows. */
16080 /* FIXME: Revisit this when glyph ``spilling'' in
16081 continuation lines' rows is implemented for
16082 bidi-reordered rows. */
16083 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16084 MATRIX_ROW_CONTINUATION_LINE_P (row);
16085 --row)
16087 /* If we hit the beginning of the displayed portion
16088 without finding the first row of a continued
16089 line, give up. */
16090 if (row <= row1)
16092 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16093 break;
16095 eassert (row->enabled_p);
16098 if (must_scroll)
16100 else if (rc != CURSOR_MOVEMENT_SUCCESS
16101 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16102 /* Make sure this isn't a header line by any chance, since
16103 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16104 && !row->mode_line_p
16105 && make_cursor_line_fully_visible_p)
16107 if (PT == MATRIX_ROW_END_CHARPOS (row)
16108 && !row->ends_at_zv_p
16109 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16110 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16111 else if (row->height > window_box_height (w))
16113 /* If we end up in a partially visible line, let's
16114 make it fully visible, except when it's taller
16115 than the window, in which case we can't do much
16116 about it. */
16117 *scroll_step = true;
16118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16120 else
16122 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16123 if (!cursor_row_fully_visible_p (w, false, true))
16124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16125 else
16126 rc = CURSOR_MOVEMENT_SUCCESS;
16129 else if (scroll_p)
16130 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16131 else if (rc != CURSOR_MOVEMENT_SUCCESS
16132 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16134 /* With bidi-reordered rows, there could be more than
16135 one candidate row whose start and end positions
16136 occlude point. We need to let set_cursor_from_row
16137 find the best candidate. */
16138 /* FIXME: Revisit this when glyph ``spilling'' in
16139 continuation lines' rows is implemented for
16140 bidi-reordered rows. */
16141 bool rv = false;
16145 bool at_zv_p = false, exact_match_p = false;
16147 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16148 && PT <= MATRIX_ROW_END_CHARPOS (row)
16149 && cursor_row_p (row))
16150 rv |= set_cursor_from_row (w, row, w->current_matrix,
16151 0, 0, 0, 0);
16152 /* As soon as we've found the exact match for point,
16153 or the first suitable row whose ends_at_zv_p flag
16154 is set, we are done. */
16155 if (rv)
16157 at_zv_p = MATRIX_ROW (w->current_matrix,
16158 w->cursor.vpos)->ends_at_zv_p;
16159 if (!at_zv_p
16160 && w->cursor.hpos >= 0
16161 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16162 w->cursor.vpos))
16164 struct glyph_row *candidate =
16165 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16166 struct glyph *g =
16167 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16168 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16170 exact_match_p =
16171 (BUFFERP (g->object) && g->charpos == PT)
16172 || (NILP (g->object)
16173 && (g->charpos == PT
16174 || (g->charpos == 0 && endpos - 1 == PT)));
16176 if (at_zv_p || exact_match_p)
16178 rc = CURSOR_MOVEMENT_SUCCESS;
16179 break;
16182 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16183 break;
16184 ++row;
16186 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16187 || row->continued_p)
16188 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16189 || (MATRIX_ROW_START_CHARPOS (row) == PT
16190 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16191 /* If we didn't find any candidate rows, or exited the
16192 loop before all the candidates were examined, signal
16193 to the caller that this method failed. */
16194 if (rc != CURSOR_MOVEMENT_SUCCESS
16195 && !(rv
16196 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16197 && !row->continued_p))
16198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16199 else if (rv)
16200 rc = CURSOR_MOVEMENT_SUCCESS;
16202 else
16206 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16208 rc = CURSOR_MOVEMENT_SUCCESS;
16209 break;
16211 ++row;
16213 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16214 && MATRIX_ROW_START_CHARPOS (row) == PT
16215 && cursor_row_p (row));
16220 return rc;
16224 void
16225 set_vertical_scroll_bar (struct window *w)
16227 ptrdiff_t start, end, whole;
16229 /* Calculate the start and end positions for the current window.
16230 At some point, it would be nice to choose between scrollbars
16231 which reflect the whole buffer size, with special markers
16232 indicating narrowing, and scrollbars which reflect only the
16233 visible region.
16235 Note that mini-buffers sometimes aren't displaying any text. */
16236 if (!MINI_WINDOW_P (w)
16237 || (w == XWINDOW (minibuf_window)
16238 && NILP (echo_area_buffer[0])))
16240 struct buffer *buf = XBUFFER (w->contents);
16241 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16242 start = marker_position (w->start) - BUF_BEGV (buf);
16243 /* I don't think this is guaranteed to be right. For the
16244 moment, we'll pretend it is. */
16245 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16247 if (end < start)
16248 end = start;
16249 if (whole < (end - start))
16250 whole = end - start;
16252 else
16253 start = end = whole = 0;
16255 /* Indicate what this scroll bar ought to be displaying now. */
16256 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16257 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16258 (w, end - start, whole, start);
16262 void
16263 set_horizontal_scroll_bar (struct window *w)
16265 int start, end, whole, portion;
16267 if (!MINI_WINDOW_P (w)
16268 || (w == XWINDOW (minibuf_window)
16269 && NILP (echo_area_buffer[0])))
16271 struct buffer *b = XBUFFER (w->contents);
16272 struct buffer *old_buffer = NULL;
16273 struct it it;
16274 struct text_pos startp;
16276 if (b != current_buffer)
16278 old_buffer = current_buffer;
16279 set_buffer_internal (b);
16282 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16283 start_display (&it, w, startp);
16284 it.last_visible_x = INT_MAX;
16285 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16286 MOVE_TO_X | MOVE_TO_Y);
16287 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16288 window_box_height (w), -1,
16289 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16291 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16292 end = start + window_box_width (w, TEXT_AREA);
16293 portion = end - start;
16294 /* After enlarging a horizontally scrolled window such that it
16295 gets at least as wide as the text it contains, make sure that
16296 the thumb doesn't fill the entire scroll bar so we can still
16297 drag it back to see the entire text. */
16298 whole = max (whole, end);
16300 if (it.bidi_p)
16302 Lisp_Object pdir;
16304 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16305 if (EQ (pdir, Qright_to_left))
16307 start = whole - end;
16308 end = start + portion;
16312 if (old_buffer)
16313 set_buffer_internal (old_buffer);
16315 else
16316 start = end = whole = portion = 0;
16318 w->hscroll_whole = whole;
16320 /* Indicate what this scroll bar ought to be displaying now. */
16321 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16322 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16323 (w, portion, whole, start);
16327 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16328 selected_window is redisplayed.
16330 We can return without actually redisplaying the window if fonts has been
16331 changed on window's frame. In that case, redisplay_internal will retry.
16333 As one of the important parts of redisplaying a window, we need to
16334 decide whether the previous window-start position (stored in the
16335 window's w->start marker position) is still valid, and if it isn't,
16336 recompute it. Some details about that:
16338 . The previous window-start could be in a continuation line, in
16339 which case we need to recompute it when the window width
16340 changes. See compute_window_start_on_continuation_line and its
16341 call below.
16343 . The text that changed since last redisplay could include the
16344 previous window-start position. In that case, we try to salvage
16345 what we can from the current glyph matrix by calling
16346 try_scrolling, which see.
16348 . Some Emacs command could force us to use a specific window-start
16349 position by setting the window's force_start flag, or gently
16350 propose doing that by setting the window's optional_new_start
16351 flag. In these cases, we try using the specified start point if
16352 that succeeds (i.e. the window desired matrix is successfully
16353 recomputed, and point location is within the window). In case
16354 of optional_new_start, we first check if the specified start
16355 position is feasible, i.e. if it will allow point to be
16356 displayed in the window. If using the specified start point
16357 fails, e.g., if new fonts are needed to be loaded, we abort the
16358 redisplay cycle and leave it up to the next cycle to figure out
16359 things.
16361 . Note that the window's force_start flag is sometimes set by
16362 redisplay itself, when it decides that the previous window start
16363 point is fine and should be kept. Search for "goto force_start"
16364 below to see the details. Like the values of window-start
16365 specified outside of redisplay, these internally-deduced values
16366 are tested for feasibility, and ignored if found to be
16367 unfeasible.
16369 . Note that the function try_window, used to completely redisplay
16370 a window, accepts the window's start point as its argument.
16371 This is used several times in the redisplay code to control
16372 where the window start will be, according to user options such
16373 as scroll-conservatively, and also to ensure the screen line
16374 showing point will be fully (as opposed to partially) visible on
16375 display. */
16377 static void
16378 redisplay_window (Lisp_Object window, bool just_this_one_p)
16380 struct window *w = XWINDOW (window);
16381 struct frame *f = XFRAME (w->frame);
16382 struct buffer *buffer = XBUFFER (w->contents);
16383 struct buffer *old = current_buffer;
16384 struct text_pos lpoint, opoint, startp;
16385 bool update_mode_line;
16386 int tem;
16387 struct it it;
16388 /* Record it now because it's overwritten. */
16389 bool current_matrix_up_to_date_p = false;
16390 bool used_current_matrix_p = false;
16391 /* This is less strict than current_matrix_up_to_date_p.
16392 It indicates that the buffer contents and narrowing are unchanged. */
16393 bool buffer_unchanged_p = false;
16394 bool temp_scroll_step = false;
16395 ptrdiff_t count = SPECPDL_INDEX ();
16396 int rc;
16397 int centering_position = -1;
16398 bool last_line_misfit = false;
16399 ptrdiff_t beg_unchanged, end_unchanged;
16400 int frame_line_height, margin;
16401 bool use_desired_matrix;
16402 void *itdata = NULL;
16404 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16405 opoint = lpoint;
16407 #ifdef GLYPH_DEBUG
16408 *w->desired_matrix->method = 0;
16409 #endif
16411 if (!just_this_one_p
16412 && REDISPLAY_SOME_P ()
16413 && !w->redisplay
16414 && !w->update_mode_line
16415 && !f->face_change
16416 && !f->redisplay
16417 && !buffer->text->redisplay
16418 && BUF_PT (buffer) == w->last_point)
16419 return;
16421 /* Make sure that both W's markers are valid. */
16422 eassert (XMARKER (w->start)->buffer == buffer);
16423 eassert (XMARKER (w->pointm)->buffer == buffer);
16425 /* We come here again if we need to run window-text-change-functions
16426 below. */
16427 restart:
16428 reconsider_clip_changes (w);
16429 frame_line_height = default_line_pixel_height (w);
16430 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16433 /* Has the mode line to be updated? */
16434 update_mode_line = (w->update_mode_line
16435 || update_mode_lines
16436 || buffer->clip_changed
16437 || buffer->prevent_redisplay_optimizations_p);
16439 if (!just_this_one_p)
16440 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16441 cleverly elsewhere. */
16442 w->must_be_updated_p = true;
16444 if (MINI_WINDOW_P (w))
16446 if (w == XWINDOW (echo_area_window)
16447 && !NILP (echo_area_buffer[0]))
16449 if (update_mode_line)
16450 /* We may have to update a tty frame's menu bar or a
16451 tool-bar. Example `M-x C-h C-h C-g'. */
16452 goto finish_menu_bars;
16453 else
16454 /* We've already displayed the echo area glyphs in this window. */
16455 goto finish_scroll_bars;
16457 else if ((w != XWINDOW (minibuf_window)
16458 || minibuf_level == 0)
16459 /* When buffer is nonempty, redisplay window normally. */
16460 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16461 /* Quail displays non-mini buffers in minibuffer window.
16462 In that case, redisplay the window normally. */
16463 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16465 /* W is a mini-buffer window, but it's not active, so clear
16466 it. */
16467 int yb = window_text_bottom_y (w);
16468 struct glyph_row *row;
16469 int y;
16471 for (y = 0, row = w->desired_matrix->rows;
16472 y < yb;
16473 y += row->height, ++row)
16474 blank_row (w, row, y);
16475 goto finish_scroll_bars;
16478 clear_glyph_matrix (w->desired_matrix);
16481 /* Otherwise set up data on this window; select its buffer and point
16482 value. */
16483 /* Really select the buffer, for the sake of buffer-local
16484 variables. */
16485 set_buffer_internal_1 (XBUFFER (w->contents));
16487 current_matrix_up_to_date_p
16488 = (w->window_end_valid
16489 && !current_buffer->clip_changed
16490 && !current_buffer->prevent_redisplay_optimizations_p
16491 && !window_outdated (w)
16492 && !hscrolling_current_line_p (w));
16494 /* Run the window-text-change-functions
16495 if it is possible that the text on the screen has changed
16496 (either due to modification of the text, or any other reason). */
16497 if (!current_matrix_up_to_date_p
16498 && !NILP (Vwindow_text_change_functions))
16500 safe_run_hooks (Qwindow_text_change_functions);
16501 goto restart;
16504 beg_unchanged = BEG_UNCHANGED;
16505 end_unchanged = END_UNCHANGED;
16507 SET_TEXT_POS (opoint, PT, PT_BYTE);
16509 specbind (Qinhibit_point_motion_hooks, Qt);
16511 buffer_unchanged_p
16512 = (w->window_end_valid
16513 && !current_buffer->clip_changed
16514 && !window_outdated (w));
16516 /* When windows_or_buffers_changed is non-zero, we can't rely
16517 on the window end being valid, so set it to zero there. */
16518 if (windows_or_buffers_changed)
16520 /* If window starts on a continuation line, maybe adjust the
16521 window start in case the window's width changed. */
16522 if (XMARKER (w->start)->buffer == current_buffer)
16523 compute_window_start_on_continuation_line (w);
16525 w->window_end_valid = false;
16526 /* If so, we also can't rely on current matrix
16527 and should not fool try_cursor_movement below. */
16528 current_matrix_up_to_date_p = false;
16531 /* Some sanity checks. */
16532 CHECK_WINDOW_END (w);
16533 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16534 emacs_abort ();
16535 if (BYTEPOS (opoint) < CHARPOS (opoint))
16536 emacs_abort ();
16538 if (mode_line_update_needed (w))
16539 update_mode_line = true;
16541 /* Point refers normally to the selected window. For any other
16542 window, set up appropriate value. */
16543 if (!EQ (window, selected_window))
16545 ptrdiff_t new_pt = marker_position (w->pointm);
16546 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16548 if (new_pt < BEGV)
16550 new_pt = BEGV;
16551 new_pt_byte = BEGV_BYTE;
16552 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16554 else if (new_pt > (ZV - 1))
16556 new_pt = ZV;
16557 new_pt_byte = ZV_BYTE;
16558 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16561 /* We don't use SET_PT so that the point-motion hooks don't run. */
16562 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16565 /* If any of the character widths specified in the display table
16566 have changed, invalidate the width run cache. It's true that
16567 this may be a bit late to catch such changes, but the rest of
16568 redisplay goes (non-fatally) haywire when the display table is
16569 changed, so why should we worry about doing any better? */
16570 if (current_buffer->width_run_cache
16571 || (current_buffer->base_buffer
16572 && current_buffer->base_buffer->width_run_cache))
16574 struct Lisp_Char_Table *disptab = buffer_display_table ();
16576 if (! disptab_matches_widthtab
16577 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16579 struct buffer *buf = current_buffer;
16581 if (buf->base_buffer)
16582 buf = buf->base_buffer;
16583 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16584 recompute_width_table (current_buffer, disptab);
16588 /* If window-start is screwed up, choose a new one. */
16589 if (XMARKER (w->start)->buffer != current_buffer)
16590 goto recenter;
16592 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16594 /* If someone specified a new starting point but did not insist,
16595 check whether it can be used. */
16596 if ((w->optional_new_start || window_frozen_p (w))
16597 && CHARPOS (startp) >= BEGV
16598 && CHARPOS (startp) <= ZV)
16600 ptrdiff_t it_charpos;
16602 w->optional_new_start = false;
16603 start_display (&it, w, startp);
16604 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16605 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16606 /* Record IT's position now, since line_bottom_y might change
16607 that. */
16608 it_charpos = IT_CHARPOS (it);
16609 /* Make sure we set the force_start flag only if the cursor row
16610 will be fully visible. Otherwise, the code under force_start
16611 label below will try to move point back into view, which is
16612 not what the code which sets optional_new_start wants. */
16613 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16614 && !w->force_start)
16616 if (it_charpos == PT)
16617 w->force_start = true;
16618 /* IT may overshoot PT if text at PT is invisible. */
16619 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16620 w->force_start = true;
16621 #ifdef GLYPH_DEBUG
16622 if (w->force_start)
16624 if (window_frozen_p (w))
16625 debug_method_add (w, "set force_start from frozen window start");
16626 else
16627 debug_method_add (w, "set force_start from optional_new_start");
16629 #endif
16633 force_start:
16635 /* Handle case where place to start displaying has been specified,
16636 unless the specified location is outside the accessible range. */
16637 if (w->force_start)
16639 /* We set this later on if we have to adjust point. */
16640 int new_vpos = -1;
16642 w->force_start = false;
16643 w->vscroll = 0;
16644 w->window_end_valid = false;
16646 /* Forget any recorded base line for line number display. */
16647 if (!buffer_unchanged_p)
16648 w->base_line_number = 0;
16650 /* Redisplay the mode line. Select the buffer properly for that.
16651 Also, run the hook window-scroll-functions
16652 because we have scrolled. */
16653 /* Note, we do this after clearing force_start because
16654 if there's an error, it is better to forget about force_start
16655 than to get into an infinite loop calling the hook functions
16656 and having them get more errors. */
16657 if (!update_mode_line
16658 || ! NILP (Vwindow_scroll_functions))
16660 update_mode_line = true;
16661 w->update_mode_line = true;
16662 startp = run_window_scroll_functions (window, startp);
16665 if (CHARPOS (startp) < BEGV)
16666 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16667 else if (CHARPOS (startp) > ZV)
16668 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16670 /* Redisplay, then check if cursor has been set during the
16671 redisplay. Give up if new fonts were loaded. */
16672 /* We used to issue a CHECK_MARGINS argument to try_window here,
16673 but this causes scrolling to fail when point begins inside
16674 the scroll margin (bug#148) -- cyd */
16675 if (!try_window (window, startp, 0))
16677 w->force_start = true;
16678 clear_glyph_matrix (w->desired_matrix);
16679 goto need_larger_matrices;
16682 if (w->cursor.vpos < 0)
16684 /* If point does not appear, try to move point so it does
16685 appear. The desired matrix has been built above, so we
16686 can use it here. First see if point is in invisible
16687 text, and if so, move it to the first visible buffer
16688 position past that. */
16689 struct glyph_row *r = NULL;
16690 Lisp_Object invprop =
16691 get_char_property_and_overlay (make_number (PT), Qinvisible,
16692 Qnil, NULL);
16694 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16696 ptrdiff_t alt_pt;
16697 Lisp_Object invprop_end =
16698 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16699 Qnil, Qnil);
16701 if (NATNUMP (invprop_end))
16702 alt_pt = XFASTINT (invprop_end);
16703 else
16704 alt_pt = ZV;
16705 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16706 NULL, 0);
16708 if (r)
16709 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16710 else /* Give up and just move to the middle of the window. */
16711 new_vpos = window_box_height (w) / 2;
16714 if (!cursor_row_fully_visible_p (w, false, false))
16716 /* Point does appear, but on a line partly visible at end of window.
16717 Move it back to a fully-visible line. */
16718 new_vpos = window_box_height (w);
16719 /* But if window_box_height suggests a Y coordinate that is
16720 not less than we already have, that line will clearly not
16721 be fully visible, so give up and scroll the display.
16722 This can happen when the default face uses a font whose
16723 dimensions are different from the frame's default
16724 font. */
16725 if (new_vpos >= w->cursor.y)
16727 w->cursor.vpos = -1;
16728 clear_glyph_matrix (w->desired_matrix);
16729 goto try_to_scroll;
16732 else if (w->cursor.vpos >= 0)
16734 /* Some people insist on not letting point enter the scroll
16735 margin, even though this part handles windows that didn't
16736 scroll at all. */
16737 int pixel_margin = margin * frame_line_height;
16738 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16740 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16741 below, which finds the row to move point to, advances by
16742 the Y coordinate of the _next_ row, see the definition of
16743 MATRIX_ROW_BOTTOM_Y. */
16744 if (w->cursor.vpos < margin + header_line)
16746 w->cursor.vpos = -1;
16747 clear_glyph_matrix (w->desired_matrix);
16748 goto try_to_scroll;
16750 else
16752 int window_height = window_box_height (w);
16754 if (header_line)
16755 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16756 if (w->cursor.y >= window_height - pixel_margin)
16758 w->cursor.vpos = -1;
16759 clear_glyph_matrix (w->desired_matrix);
16760 goto try_to_scroll;
16765 /* If we need to move point for either of the above reasons,
16766 now actually do it. */
16767 if (new_vpos >= 0)
16769 struct glyph_row *row;
16771 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16772 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16773 ++row;
16775 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16776 MATRIX_ROW_START_BYTEPOS (row));
16778 if (w != XWINDOW (selected_window))
16779 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16780 else if (current_buffer == old)
16781 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16783 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16785 /* Re-run pre-redisplay-function so it can update the region
16786 according to the new position of point. */
16787 /* Other than the cursor, w's redisplay is done so we can set its
16788 redisplay to false. Also the buffer's redisplay can be set to
16789 false, since propagate_buffer_redisplay should have already
16790 propagated its info to `w' anyway. */
16791 w->redisplay = false;
16792 XBUFFER (w->contents)->text->redisplay = false;
16793 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16795 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16797 /* pre-redisplay-function made changes (e.g. move the region)
16798 that require another round of redisplay. */
16799 clear_glyph_matrix (w->desired_matrix);
16800 if (!try_window (window, startp, 0))
16801 goto need_larger_matrices;
16804 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16806 clear_glyph_matrix (w->desired_matrix);
16807 goto try_to_scroll;
16810 #ifdef GLYPH_DEBUG
16811 debug_method_add (w, "forced window start");
16812 #endif
16813 goto done;
16816 /* Handle case where text has not changed, only point, and it has
16817 not moved off the frame, and we are not retrying after hscroll.
16818 (current_matrix_up_to_date_p is true when retrying.) */
16819 if (current_matrix_up_to_date_p
16820 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16821 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16823 switch (rc)
16825 case CURSOR_MOVEMENT_SUCCESS:
16826 used_current_matrix_p = true;
16827 goto done;
16829 case CURSOR_MOVEMENT_MUST_SCROLL:
16830 goto try_to_scroll;
16832 default:
16833 emacs_abort ();
16836 /* If current starting point was originally the beginning of a line
16837 but no longer is, find a new starting point. */
16838 else if (w->start_at_line_beg
16839 && !(CHARPOS (startp) <= BEGV
16840 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16842 #ifdef GLYPH_DEBUG
16843 debug_method_add (w, "recenter 1");
16844 #endif
16845 goto recenter;
16848 /* Try scrolling with try_window_id. Value is > 0 if update has
16849 been done, it is -1 if we know that the same window start will
16850 not work. It is 0 if unsuccessful for some other reason. */
16851 else if ((tem = try_window_id (w)) != 0)
16853 #ifdef GLYPH_DEBUG
16854 debug_method_add (w, "try_window_id %d", tem);
16855 #endif
16857 if (f->fonts_changed)
16858 goto need_larger_matrices;
16859 if (tem > 0)
16860 goto done;
16862 /* Otherwise try_window_id has returned -1 which means that we
16863 don't want the alternative below this comment to execute. */
16865 else if (CHARPOS (startp) >= BEGV
16866 && CHARPOS (startp) <= ZV
16867 && PT >= CHARPOS (startp)
16868 && (CHARPOS (startp) < ZV
16869 /* Avoid starting at end of buffer. */
16870 || CHARPOS (startp) == BEGV
16871 || !window_outdated (w)))
16873 int d1, d2, d5, d6;
16874 int rtop, rbot;
16876 /* If first window line is a continuation line, and window start
16877 is inside the modified region, but the first change is before
16878 current window start, we must select a new window start.
16880 However, if this is the result of a down-mouse event (e.g. by
16881 extending the mouse-drag-overlay), we don't want to select a
16882 new window start, since that would change the position under
16883 the mouse, resulting in an unwanted mouse-movement rather
16884 than a simple mouse-click. */
16885 if (!w->start_at_line_beg
16886 && NILP (do_mouse_tracking)
16887 && CHARPOS (startp) > BEGV
16888 && CHARPOS (startp) > BEG + beg_unchanged
16889 && CHARPOS (startp) <= Z - end_unchanged
16890 /* Even if w->start_at_line_beg is nil, a new window may
16891 start at a line_beg, since that's how set_buffer_window
16892 sets it. So, we need to check the return value of
16893 compute_window_start_on_continuation_line. (See also
16894 bug#197). */
16895 && XMARKER (w->start)->buffer == current_buffer
16896 && compute_window_start_on_continuation_line (w)
16897 /* It doesn't make sense to force the window start like we
16898 do at label force_start if it is already known that point
16899 will not be fully visible in the resulting window, because
16900 doing so will move point from its correct position
16901 instead of scrolling the window to bring point into view.
16902 See bug#9324. */
16903 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16904 /* A very tall row could need more than the window height,
16905 in which case we accept that it is partially visible. */
16906 && (rtop != 0) == (rbot != 0))
16908 w->force_start = true;
16909 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16910 #ifdef GLYPH_DEBUG
16911 debug_method_add (w, "recomputed window start in continuation line");
16912 #endif
16913 goto force_start;
16916 #ifdef GLYPH_DEBUG
16917 debug_method_add (w, "same window start");
16918 #endif
16920 /* Try to redisplay starting at same place as before.
16921 If point has not moved off frame, accept the results. */
16922 if (!current_matrix_up_to_date_p
16923 /* Don't use try_window_reusing_current_matrix in this case
16924 because a window scroll function can have changed the
16925 buffer. */
16926 || !NILP (Vwindow_scroll_functions)
16927 || MINI_WINDOW_P (w)
16928 || !(used_current_matrix_p
16929 = try_window_reusing_current_matrix (w)))
16931 IF_DEBUG (debug_method_add (w, "1"));
16932 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16933 /* -1 means we need to scroll.
16934 0 means we need new matrices, but fonts_changed
16935 is set in that case, so we will detect it below. */
16936 goto try_to_scroll;
16939 if (f->fonts_changed)
16940 goto need_larger_matrices;
16942 if (w->cursor.vpos >= 0)
16944 if (!just_this_one_p
16945 || current_buffer->clip_changed
16946 || BEG_UNCHANGED < CHARPOS (startp))
16947 /* Forget any recorded base line for line number display. */
16948 w->base_line_number = 0;
16950 if (!cursor_row_fully_visible_p (w, true, false))
16952 clear_glyph_matrix (w->desired_matrix);
16953 last_line_misfit = true;
16955 /* Drop through and scroll. */
16956 else
16957 goto done;
16959 else
16960 clear_glyph_matrix (w->desired_matrix);
16963 try_to_scroll:
16965 /* Redisplay the mode line. Select the buffer properly for that. */
16966 if (!update_mode_line)
16968 update_mode_line = true;
16969 w->update_mode_line = true;
16972 /* Try to scroll by specified few lines. */
16973 if ((scroll_conservatively
16974 || emacs_scroll_step
16975 || temp_scroll_step
16976 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16977 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16978 && CHARPOS (startp) >= BEGV
16979 && CHARPOS (startp) <= ZV)
16981 /* The function returns -1 if new fonts were loaded, 1 if
16982 successful, 0 if not successful. */
16983 int ss = try_scrolling (window, just_this_one_p,
16984 scroll_conservatively,
16985 emacs_scroll_step,
16986 temp_scroll_step, last_line_misfit);
16987 switch (ss)
16989 case SCROLLING_SUCCESS:
16990 goto done;
16992 case SCROLLING_NEED_LARGER_MATRICES:
16993 goto need_larger_matrices;
16995 case SCROLLING_FAILED:
16996 break;
16998 default:
16999 emacs_abort ();
17003 /* Finally, just choose a place to start which positions point
17004 according to user preferences. */
17006 recenter:
17008 #ifdef GLYPH_DEBUG
17009 debug_method_add (w, "recenter");
17010 #endif
17012 /* Forget any previously recorded base line for line number display. */
17013 if (!buffer_unchanged_p)
17014 w->base_line_number = 0;
17016 /* Determine the window start relative to point. */
17017 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17018 it.current_y = it.last_visible_y;
17019 if (centering_position < 0)
17021 ptrdiff_t margin_pos = CHARPOS (startp);
17022 Lisp_Object aggressive;
17023 bool scrolling_up;
17025 /* If there is a scroll margin at the top of the window, find
17026 its character position. */
17027 if (margin
17028 /* Cannot call start_display if startp is not in the
17029 accessible region of the buffer. This can happen when we
17030 have just switched to a different buffer and/or changed
17031 its restriction. In that case, startp is initialized to
17032 the character position 1 (BEGV) because we did not yet
17033 have chance to display the buffer even once. */
17034 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17036 struct it it1;
17037 void *it1data = NULL;
17039 SAVE_IT (it1, it, it1data);
17040 start_display (&it1, w, startp);
17041 move_it_vertically (&it1, margin * frame_line_height);
17042 margin_pos = IT_CHARPOS (it1);
17043 RESTORE_IT (&it, &it, it1data);
17045 scrolling_up = PT > margin_pos;
17046 aggressive =
17047 scrolling_up
17048 ? BVAR (current_buffer, scroll_up_aggressively)
17049 : BVAR (current_buffer, scroll_down_aggressively);
17051 if (!MINI_WINDOW_P (w)
17052 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17054 int pt_offset = 0;
17056 /* Setting scroll-conservatively overrides
17057 scroll-*-aggressively. */
17058 if (!scroll_conservatively && NUMBERP (aggressive))
17060 double float_amount = XFLOATINT (aggressive);
17062 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17063 if (pt_offset == 0 && float_amount > 0)
17064 pt_offset = 1;
17065 if (pt_offset && margin > 0)
17066 margin -= 1;
17068 /* Compute how much to move the window start backward from
17069 point so that point will be displayed where the user
17070 wants it. */
17071 if (scrolling_up)
17073 centering_position = it.last_visible_y;
17074 if (pt_offset)
17075 centering_position -= pt_offset;
17076 centering_position -=
17077 (frame_line_height * (1 + margin + last_line_misfit)
17078 + WINDOW_HEADER_LINE_HEIGHT (w));
17079 /* Don't let point enter the scroll margin near top of
17080 the window. */
17081 if (centering_position < margin * frame_line_height)
17082 centering_position = margin * frame_line_height;
17084 else
17085 centering_position = margin * frame_line_height + pt_offset;
17087 else
17088 /* Set the window start half the height of the window backward
17089 from point. */
17090 centering_position = window_box_height (w) / 2;
17092 move_it_vertically_backward (&it, centering_position);
17094 eassert (IT_CHARPOS (it) >= BEGV);
17096 /* The function move_it_vertically_backward may move over more
17097 than the specified y-distance. If it->w is small, e.g. a
17098 mini-buffer window, we may end up in front of the window's
17099 display area. Start displaying at the start of the line
17100 containing PT in this case. */
17101 if (it.current_y <= 0)
17103 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17104 move_it_vertically_backward (&it, 0);
17105 it.current_y = 0;
17108 it.current_x = it.hpos = 0;
17110 /* Set the window start position here explicitly, to avoid an
17111 infinite loop in case the functions in window-scroll-functions
17112 get errors. */
17113 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17115 /* Run scroll hooks. */
17116 startp = run_window_scroll_functions (window, it.current.pos);
17118 /* We invoke try_window and try_window_reusing_current_matrix below,
17119 and they manipulate the bidi cache. Save and restore the cache
17120 state of our iterator, so we could continue using it after that. */
17121 itdata = bidi_shelve_cache ();
17123 /* Redisplay the window. */
17124 use_desired_matrix = false;
17125 if (!current_matrix_up_to_date_p
17126 || windows_or_buffers_changed
17127 || f->cursor_type_changed
17128 /* Don't use try_window_reusing_current_matrix in this case
17129 because it can have changed the buffer. */
17130 || !NILP (Vwindow_scroll_functions)
17131 || !just_this_one_p
17132 || MINI_WINDOW_P (w)
17133 || !(used_current_matrix_p
17134 = try_window_reusing_current_matrix (w)))
17135 use_desired_matrix = (try_window (window, startp, 0) == 1);
17137 bidi_unshelve_cache (itdata, false);
17139 /* If new fonts have been loaded (due to fontsets), give up. We
17140 have to start a new redisplay since we need to re-adjust glyph
17141 matrices. */
17142 if (f->fonts_changed)
17143 goto need_larger_matrices;
17145 /* If cursor did not appear assume that the middle of the window is
17146 in the first line of the window. Do it again with the next line.
17147 (Imagine a window of height 100, displaying two lines of height
17148 60. Moving back 50 from it->last_visible_y will end in the first
17149 line.) */
17150 if (w->cursor.vpos < 0)
17152 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17154 clear_glyph_matrix (w->desired_matrix);
17155 move_it_by_lines (&it, 1);
17156 try_window (window, it.current.pos, 0);
17158 else if (PT < IT_CHARPOS (it))
17160 clear_glyph_matrix (w->desired_matrix);
17161 move_it_by_lines (&it, -1);
17162 try_window (window, it.current.pos, 0);
17164 else if (scroll_conservatively > SCROLL_LIMIT
17165 && (it.method == GET_FROM_STRING
17166 || overlay_touches_p (IT_CHARPOS (it)))
17167 && IT_CHARPOS (it) < ZV)
17169 /* If the window starts with a before-string that spans more
17170 than one screen line, using that position to display the
17171 window might fail to bring point into the view, because
17172 start_display will always start by displaying the string,
17173 whereas the code above determines where to set w->start
17174 by the buffer position of the place where it takes screen
17175 coordinates. Try to recover by finding the next screen
17176 line that displays buffer text. */
17177 ptrdiff_t pos0 = IT_CHARPOS (it);
17179 clear_glyph_matrix (w->desired_matrix);
17180 do {
17181 move_it_by_lines (&it, 1);
17182 } while (IT_CHARPOS (it) == pos0);
17183 try_window (window, it.current.pos, 0);
17185 else
17187 /* Not much we can do about it. */
17191 /* Consider the following case: Window starts at BEGV, there is
17192 invisible, intangible text at BEGV, so that display starts at
17193 some point START > BEGV. It can happen that we are called with
17194 PT somewhere between BEGV and START. Try to handle that case,
17195 and similar ones. */
17196 if (w->cursor.vpos < 0)
17198 /* Prefer the desired matrix to the current matrix, if possible,
17199 in the fallback calculations below. This is because using
17200 the current matrix might completely goof, e.g. if its first
17201 row is after point. */
17202 struct glyph_matrix *matrix =
17203 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17204 /* First, try locating the proper glyph row for PT. */
17205 struct glyph_row *row =
17206 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17208 /* Sometimes point is at the beginning of invisible text that is
17209 before the 1st character displayed in the row. In that case,
17210 row_containing_pos fails to find the row, because no glyphs
17211 with appropriate buffer positions are present in the row.
17212 Therefore, we next try to find the row which shows the 1st
17213 position after the invisible text. */
17214 if (!row)
17216 Lisp_Object val =
17217 get_char_property_and_overlay (make_number (PT), Qinvisible,
17218 Qnil, NULL);
17220 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17222 ptrdiff_t alt_pos;
17223 Lisp_Object invis_end =
17224 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17225 Qnil, Qnil);
17227 if (NATNUMP (invis_end))
17228 alt_pos = XFASTINT (invis_end);
17229 else
17230 alt_pos = ZV;
17231 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17234 /* Finally, fall back on the first row of the window after the
17235 header line (if any). This is slightly better than not
17236 displaying the cursor at all. */
17237 if (!row)
17239 row = matrix->rows;
17240 if (row->mode_line_p)
17241 ++row;
17243 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17246 if (!cursor_row_fully_visible_p (w, false, false))
17248 /* If vscroll is enabled, disable it and try again. */
17249 if (w->vscroll)
17251 w->vscroll = 0;
17252 clear_glyph_matrix (w->desired_matrix);
17253 goto recenter;
17256 /* Users who set scroll-conservatively to a large number want
17257 point just above/below the scroll margin. If we ended up
17258 with point's row partially visible, move the window start to
17259 make that row fully visible and out of the margin. */
17260 if (scroll_conservatively > SCROLL_LIMIT)
17262 int window_total_lines
17263 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17264 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17266 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17267 clear_glyph_matrix (w->desired_matrix);
17268 if (1 == try_window (window, it.current.pos,
17269 TRY_WINDOW_CHECK_MARGINS))
17270 goto done;
17273 /* If centering point failed to make the whole line visible,
17274 put point at the top instead. That has to make the whole line
17275 visible, if it can be done. */
17276 if (centering_position == 0)
17277 goto done;
17279 clear_glyph_matrix (w->desired_matrix);
17280 centering_position = 0;
17281 goto recenter;
17284 done:
17286 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17287 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17288 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17290 /* Display the mode line, if we must. */
17291 if ((update_mode_line
17292 /* If window not full width, must redo its mode line
17293 if (a) the window to its side is being redone and
17294 (b) we do a frame-based redisplay. This is a consequence
17295 of how inverted lines are drawn in frame-based redisplay. */
17296 || (!just_this_one_p
17297 && !FRAME_WINDOW_P (f)
17298 && !WINDOW_FULL_WIDTH_P (w))
17299 /* Line number to display. */
17300 || w->base_line_pos > 0
17301 /* Column number is displayed and different from the one displayed. */
17302 || (w->column_number_displayed != -1
17303 && (w->column_number_displayed != current_column ())))
17304 /* This means that the window has a mode line. */
17305 && (WINDOW_WANTS_MODELINE_P (w)
17306 || WINDOW_WANTS_HEADER_LINE_P (w)))
17309 display_mode_lines (w);
17311 /* If mode line height has changed, arrange for a thorough
17312 immediate redisplay using the correct mode line height. */
17313 if (WINDOW_WANTS_MODELINE_P (w)
17314 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17316 f->fonts_changed = true;
17317 w->mode_line_height = -1;
17318 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17319 = DESIRED_MODE_LINE_HEIGHT (w);
17322 /* If header line height has changed, arrange for a thorough
17323 immediate redisplay using the correct header line height. */
17324 if (WINDOW_WANTS_HEADER_LINE_P (w)
17325 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17327 f->fonts_changed = true;
17328 w->header_line_height = -1;
17329 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17330 = DESIRED_HEADER_LINE_HEIGHT (w);
17333 if (f->fonts_changed)
17334 goto need_larger_matrices;
17337 if (!line_number_displayed && w->base_line_pos != -1)
17339 w->base_line_pos = 0;
17340 w->base_line_number = 0;
17343 finish_menu_bars:
17345 /* When we reach a frame's selected window, redo the frame's menu
17346 bar and the frame's title. */
17347 if (update_mode_line
17348 && EQ (FRAME_SELECTED_WINDOW (f), window))
17350 bool redisplay_menu_p;
17352 if (FRAME_WINDOW_P (f))
17354 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17355 || defined (HAVE_NS) || defined (USE_GTK)
17356 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17357 #else
17358 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17359 #endif
17361 else
17362 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17364 if (redisplay_menu_p)
17365 display_menu_bar (w);
17367 #ifdef HAVE_WINDOW_SYSTEM
17368 if (FRAME_WINDOW_P (f))
17370 #if defined (USE_GTK) || defined (HAVE_NS)
17371 if (FRAME_EXTERNAL_TOOL_BAR (f))
17372 redisplay_tool_bar (f);
17373 #else
17374 if (WINDOWP (f->tool_bar_window)
17375 && (FRAME_TOOL_BAR_LINES (f) > 0
17376 || !NILP (Vauto_resize_tool_bars))
17377 && redisplay_tool_bar (f))
17378 ignore_mouse_drag_p = true;
17379 #endif
17381 x_consider_frame_title (w->frame);
17382 #endif
17385 #ifdef HAVE_WINDOW_SYSTEM
17386 if (FRAME_WINDOW_P (f)
17387 && update_window_fringes (w, (just_this_one_p
17388 || (!used_current_matrix_p && !overlay_arrow_seen)
17389 || w->pseudo_window_p)))
17391 update_begin (f);
17392 block_input ();
17393 if (draw_window_fringes (w, true))
17395 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17396 x_draw_right_divider (w);
17397 else
17398 x_draw_vertical_border (w);
17400 unblock_input ();
17401 update_end (f);
17404 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17405 x_draw_bottom_divider (w);
17406 #endif /* HAVE_WINDOW_SYSTEM */
17408 /* We go to this label, with fonts_changed set, if it is
17409 necessary to try again using larger glyph matrices.
17410 We have to redeem the scroll bar even in this case,
17411 because the loop in redisplay_internal expects that. */
17412 need_larger_matrices:
17414 finish_scroll_bars:
17416 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17418 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17419 /* Set the thumb's position and size. */
17420 set_vertical_scroll_bar (w);
17422 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17423 /* Set the thumb's position and size. */
17424 set_horizontal_scroll_bar (w);
17426 /* Note that we actually used the scroll bar attached to this
17427 window, so it shouldn't be deleted at the end of redisplay. */
17428 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17429 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17432 /* Restore current_buffer and value of point in it. The window
17433 update may have changed the buffer, so first make sure `opoint'
17434 is still valid (Bug#6177). */
17435 if (CHARPOS (opoint) < BEGV)
17436 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17437 else if (CHARPOS (opoint) > ZV)
17438 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17439 else
17440 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17442 set_buffer_internal_1 (old);
17443 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17444 shorter. This can be caused by log truncation in *Messages*. */
17445 if (CHARPOS (lpoint) <= ZV)
17446 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17448 unbind_to (count, Qnil);
17452 /* Build the complete desired matrix of WINDOW with a window start
17453 buffer position POS.
17455 Value is 1 if successful. It is zero if fonts were loaded during
17456 redisplay which makes re-adjusting glyph matrices necessary, and -1
17457 if point would appear in the scroll margins.
17458 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17459 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17460 set in FLAGS.) */
17463 try_window (Lisp_Object window, struct text_pos pos, int flags)
17465 struct window *w = XWINDOW (window);
17466 struct it it;
17467 struct glyph_row *last_text_row = NULL;
17468 struct frame *f = XFRAME (w->frame);
17469 int cursor_vpos = w->cursor.vpos;
17471 /* Make POS the new window start. */
17472 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17474 /* Mark cursor position as unknown. No overlay arrow seen. */
17475 w->cursor.vpos = -1;
17476 overlay_arrow_seen = false;
17478 /* Initialize iterator and info to start at POS. */
17479 start_display (&it, w, pos);
17480 it.glyph_row->reversed_p = false;
17482 /* Display all lines of W. */
17483 while (it.current_y < it.last_visible_y)
17485 if (display_line (&it, cursor_vpos))
17486 last_text_row = it.glyph_row - 1;
17487 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17488 return 0;
17491 /* Save the character position of 'it' before we call
17492 'start_display' again. */
17493 ptrdiff_t it_charpos = IT_CHARPOS (it);
17495 /* Don't let the cursor end in the scroll margins. */
17496 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17497 && !MINI_WINDOW_P (w))
17499 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17500 start_display (&it, w, pos);
17502 if ((w->cursor.y >= 0 /* not vscrolled */
17503 && w->cursor.y < this_scroll_margin
17504 && CHARPOS (pos) > BEGV
17505 && it_charpos < ZV)
17506 /* rms: considering make_cursor_line_fully_visible_p here
17507 seems to give wrong results. We don't want to recenter
17508 when the last line is partly visible, we want to allow
17509 that case to be handled in the usual way. */
17510 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17511 - this_scroll_margin - 1))
17513 w->cursor.vpos = -1;
17514 clear_glyph_matrix (w->desired_matrix);
17515 return -1;
17519 /* If bottom moved off end of frame, change mode line percentage. */
17520 if (w->window_end_pos <= 0 && Z != it_charpos)
17521 w->update_mode_line = true;
17523 /* Set window_end_pos to the offset of the last character displayed
17524 on the window from the end of current_buffer. Set
17525 window_end_vpos to its row number. */
17526 if (last_text_row)
17528 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17529 adjust_window_ends (w, last_text_row, false);
17530 eassert
17531 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17532 w->window_end_vpos)));
17534 else
17536 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17537 w->window_end_pos = Z - ZV;
17538 w->window_end_vpos = 0;
17541 /* But that is not valid info until redisplay finishes. */
17542 w->window_end_valid = false;
17543 return 1;
17548 /************************************************************************
17549 Window redisplay reusing current matrix when buffer has not changed
17550 ************************************************************************/
17552 /* Try redisplay of window W showing an unchanged buffer with a
17553 different window start than the last time it was displayed by
17554 reusing its current matrix. Value is true if successful.
17555 W->start is the new window start. */
17557 static bool
17558 try_window_reusing_current_matrix (struct window *w)
17560 struct frame *f = XFRAME (w->frame);
17561 struct glyph_row *bottom_row;
17562 struct it it;
17563 struct run run;
17564 struct text_pos start, new_start;
17565 int nrows_scrolled, i;
17566 struct glyph_row *last_text_row;
17567 struct glyph_row *last_reused_text_row;
17568 struct glyph_row *start_row;
17569 int start_vpos, min_y, max_y;
17571 #ifdef GLYPH_DEBUG
17572 if (inhibit_try_window_reusing)
17573 return false;
17574 #endif
17576 if (/* This function doesn't handle terminal frames. */
17577 !FRAME_WINDOW_P (f)
17578 /* Don't try to reuse the display if windows have been split
17579 or such. */
17580 || windows_or_buffers_changed
17581 || f->cursor_type_changed)
17582 return false;
17584 /* Can't do this if showing trailing whitespace. */
17585 if (!NILP (Vshow_trailing_whitespace))
17586 return false;
17588 /* If top-line visibility has changed, give up. */
17589 if (WINDOW_WANTS_HEADER_LINE_P (w)
17590 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17591 return false;
17593 /* Give up if old or new display is scrolled vertically. We could
17594 make this function handle this, but right now it doesn't. */
17595 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17596 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17597 return false;
17599 /* Give up if line numbers are being displayed, because reusing the
17600 current matrix might use the wrong width for line-number
17601 display. */
17602 if (!NILP (Vdisplay_line_numbers))
17603 return false;
17605 /* The variable new_start now holds the new window start. The old
17606 start `start' can be determined from the current matrix. */
17607 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17608 start = start_row->minpos;
17609 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17611 /* Clear the desired matrix for the display below. */
17612 clear_glyph_matrix (w->desired_matrix);
17614 if (CHARPOS (new_start) <= CHARPOS (start))
17616 /* Don't use this method if the display starts with an ellipsis
17617 displayed for invisible text. It's not easy to handle that case
17618 below, and it's certainly not worth the effort since this is
17619 not a frequent case. */
17620 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17621 return false;
17623 IF_DEBUG (debug_method_add (w, "twu1"));
17625 /* Display up to a row that can be reused. The variable
17626 last_text_row is set to the last row displayed that displays
17627 text. Note that it.vpos == 0 if or if not there is a
17628 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17629 start_display (&it, w, new_start);
17630 w->cursor.vpos = -1;
17631 last_text_row = last_reused_text_row = NULL;
17633 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17635 /* If we have reached into the characters in the START row,
17636 that means the line boundaries have changed. So we
17637 can't start copying with the row START. Maybe it will
17638 work to start copying with the following row. */
17639 while (IT_CHARPOS (it) > CHARPOS (start))
17641 /* Advance to the next row as the "start". */
17642 start_row++;
17643 start = start_row->minpos;
17644 /* If there are no more rows to try, or just one, give up. */
17645 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17646 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17647 || CHARPOS (start) == ZV)
17649 clear_glyph_matrix (w->desired_matrix);
17650 return false;
17653 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17655 /* If we have reached alignment, we can copy the rest of the
17656 rows. */
17657 if (IT_CHARPOS (it) == CHARPOS (start)
17658 /* Don't accept "alignment" inside a display vector,
17659 since start_row could have started in the middle of
17660 that same display vector (thus their character
17661 positions match), and we have no way of telling if
17662 that is the case. */
17663 && it.current.dpvec_index < 0)
17664 break;
17666 it.glyph_row->reversed_p = false;
17667 if (display_line (&it, -1))
17668 last_text_row = it.glyph_row - 1;
17672 /* A value of current_y < last_visible_y means that we stopped
17673 at the previous window start, which in turn means that we
17674 have at least one reusable row. */
17675 if (it.current_y < it.last_visible_y)
17677 struct glyph_row *row;
17679 /* IT.vpos always starts from 0; it counts text lines. */
17680 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17682 /* Find PT if not already found in the lines displayed. */
17683 if (w->cursor.vpos < 0)
17685 int dy = it.current_y - start_row->y;
17687 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17688 row = row_containing_pos (w, PT, row, NULL, dy);
17689 if (row)
17690 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17691 dy, nrows_scrolled);
17692 else
17694 clear_glyph_matrix (w->desired_matrix);
17695 return false;
17699 /* Scroll the display. Do it before the current matrix is
17700 changed. The problem here is that update has not yet
17701 run, i.e. part of the current matrix is not up to date.
17702 scroll_run_hook will clear the cursor, and use the
17703 current matrix to get the height of the row the cursor is
17704 in. */
17705 run.current_y = start_row->y;
17706 run.desired_y = it.current_y;
17707 run.height = it.last_visible_y - it.current_y;
17709 if (run.height > 0 && run.current_y != run.desired_y)
17711 update_begin (f);
17712 FRAME_RIF (f)->update_window_begin_hook (w);
17713 FRAME_RIF (f)->clear_window_mouse_face (w);
17714 FRAME_RIF (f)->scroll_run_hook (w, &run);
17715 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17716 update_end (f);
17719 /* Shift current matrix down by nrows_scrolled lines. */
17720 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17721 rotate_matrix (w->current_matrix,
17722 start_vpos,
17723 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17724 nrows_scrolled);
17726 /* Disable lines that must be updated. */
17727 for (i = 0; i < nrows_scrolled; ++i)
17728 (start_row + i)->enabled_p = false;
17730 /* Re-compute Y positions. */
17731 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17732 max_y = it.last_visible_y;
17733 for (row = start_row + nrows_scrolled;
17734 row < bottom_row;
17735 ++row)
17737 row->y = it.current_y;
17738 row->visible_height = row->height;
17740 if (row->y < min_y)
17741 row->visible_height -= min_y - row->y;
17742 if (row->y + row->height > max_y)
17743 row->visible_height -= row->y + row->height - max_y;
17744 if (row->fringe_bitmap_periodic_p)
17745 row->redraw_fringe_bitmaps_p = true;
17747 it.current_y += row->height;
17749 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17750 last_reused_text_row = row;
17751 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17752 break;
17755 /* Disable lines in the current matrix which are now
17756 below the window. */
17757 for (++row; row < bottom_row; ++row)
17758 row->enabled_p = row->mode_line_p = false;
17761 /* Update window_end_pos etc.; last_reused_text_row is the last
17762 reused row from the current matrix containing text, if any.
17763 The value of last_text_row is the last displayed line
17764 containing text. */
17765 if (last_reused_text_row)
17766 adjust_window_ends (w, last_reused_text_row, true);
17767 else if (last_text_row)
17768 adjust_window_ends (w, last_text_row, false);
17769 else
17771 /* This window must be completely empty. */
17772 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17773 w->window_end_pos = Z - ZV;
17774 w->window_end_vpos = 0;
17776 w->window_end_valid = false;
17778 /* Update hint: don't try scrolling again in update_window. */
17779 w->desired_matrix->no_scrolling_p = true;
17781 #ifdef GLYPH_DEBUG
17782 debug_method_add (w, "try_window_reusing_current_matrix 1");
17783 #endif
17784 return true;
17786 else if (CHARPOS (new_start) > CHARPOS (start))
17788 struct glyph_row *pt_row, *row;
17789 struct glyph_row *first_reusable_row;
17790 struct glyph_row *first_row_to_display;
17791 int dy;
17792 int yb = window_text_bottom_y (w);
17794 /* Find the row starting at new_start, if there is one. Don't
17795 reuse a partially visible line at the end. */
17796 first_reusable_row = start_row;
17797 while (first_reusable_row->enabled_p
17798 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17799 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17800 < CHARPOS (new_start)))
17801 ++first_reusable_row;
17803 /* Give up if there is no row to reuse. */
17804 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17805 || !first_reusable_row->enabled_p
17806 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17807 != CHARPOS (new_start)))
17808 return false;
17810 /* We can reuse fully visible rows beginning with
17811 first_reusable_row to the end of the window. Set
17812 first_row_to_display to the first row that cannot be reused.
17813 Set pt_row to the row containing point, if there is any. */
17814 pt_row = NULL;
17815 for (first_row_to_display = first_reusable_row;
17816 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17817 ++first_row_to_display)
17819 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17820 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17821 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17822 && first_row_to_display->ends_at_zv_p
17823 && pt_row == NULL)))
17824 pt_row = first_row_to_display;
17827 /* Start displaying at the start of first_row_to_display. */
17828 eassert (first_row_to_display->y < yb);
17829 init_to_row_start (&it, w, first_row_to_display);
17831 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17832 - start_vpos);
17833 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17834 - nrows_scrolled);
17835 it.current_y = (first_row_to_display->y - first_reusable_row->y
17836 + WINDOW_HEADER_LINE_HEIGHT (w));
17838 /* Display lines beginning with first_row_to_display in the
17839 desired matrix. Set last_text_row to the last row displayed
17840 that displays text. */
17841 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17842 if (pt_row == NULL)
17843 w->cursor.vpos = -1;
17844 last_text_row = NULL;
17845 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17846 if (display_line (&it, w->cursor.vpos))
17847 last_text_row = it.glyph_row - 1;
17849 /* If point is in a reused row, adjust y and vpos of the cursor
17850 position. */
17851 if (pt_row)
17853 w->cursor.vpos -= nrows_scrolled;
17854 w->cursor.y -= first_reusable_row->y - start_row->y;
17857 /* Give up if point isn't in a row displayed or reused. (This
17858 also handles the case where w->cursor.vpos < nrows_scrolled
17859 after the calls to display_line, which can happen with scroll
17860 margins. See bug#1295.) */
17861 if (w->cursor.vpos < 0)
17863 clear_glyph_matrix (w->desired_matrix);
17864 return false;
17867 /* Scroll the display. */
17868 run.current_y = first_reusable_row->y;
17869 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17870 run.height = it.last_visible_y - run.current_y;
17871 dy = run.current_y - run.desired_y;
17873 if (run.height)
17875 update_begin (f);
17876 FRAME_RIF (f)->update_window_begin_hook (w);
17877 FRAME_RIF (f)->clear_window_mouse_face (w);
17878 FRAME_RIF (f)->scroll_run_hook (w, &run);
17879 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17880 update_end (f);
17883 /* Adjust Y positions of reused rows. */
17884 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17885 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17886 max_y = it.last_visible_y;
17887 for (row = first_reusable_row; row < first_row_to_display; ++row)
17889 row->y -= dy;
17890 row->visible_height = row->height;
17891 if (row->y < min_y)
17892 row->visible_height -= min_y - row->y;
17893 if (row->y + row->height > max_y)
17894 row->visible_height -= row->y + row->height - max_y;
17895 if (row->fringe_bitmap_periodic_p)
17896 row->redraw_fringe_bitmaps_p = true;
17899 /* Scroll the current matrix. */
17900 eassert (nrows_scrolled > 0);
17901 rotate_matrix (w->current_matrix,
17902 start_vpos,
17903 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17904 -nrows_scrolled);
17906 /* Disable rows not reused. */
17907 for (row -= nrows_scrolled; row < bottom_row; ++row)
17908 row->enabled_p = false;
17910 /* Point may have moved to a different line, so we cannot assume that
17911 the previous cursor position is valid; locate the correct row. */
17912 if (pt_row)
17914 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17915 row < bottom_row
17916 && PT >= MATRIX_ROW_END_CHARPOS (row)
17917 && !row->ends_at_zv_p;
17918 row++)
17920 w->cursor.vpos++;
17921 w->cursor.y = row->y;
17923 if (row < bottom_row)
17925 /* Can't simply scan the row for point with
17926 bidi-reordered glyph rows. Let set_cursor_from_row
17927 figure out where to put the cursor, and if it fails,
17928 give up. */
17929 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17931 if (!set_cursor_from_row (w, row, w->current_matrix,
17932 0, 0, 0, 0))
17934 clear_glyph_matrix (w->desired_matrix);
17935 return false;
17938 else
17940 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17941 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17943 for (; glyph < end
17944 && (!BUFFERP (glyph->object)
17945 || glyph->charpos < PT);
17946 glyph++)
17948 w->cursor.hpos++;
17949 w->cursor.x += glyph->pixel_width;
17955 /* Adjust window end. A null value of last_text_row means that
17956 the window end is in reused rows which in turn means that
17957 only its vpos can have changed. */
17958 if (last_text_row)
17959 adjust_window_ends (w, last_text_row, false);
17960 else
17961 w->window_end_vpos -= nrows_scrolled;
17963 w->window_end_valid = false;
17964 w->desired_matrix->no_scrolling_p = true;
17966 #ifdef GLYPH_DEBUG
17967 debug_method_add (w, "try_window_reusing_current_matrix 2");
17968 #endif
17969 return true;
17972 return false;
17977 /************************************************************************
17978 Window redisplay reusing current matrix when buffer has changed
17979 ************************************************************************/
17981 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17982 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17983 ptrdiff_t *, ptrdiff_t *);
17984 static struct glyph_row *
17985 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17986 struct glyph_row *);
17989 /* Return the last row in MATRIX displaying text. If row START is
17990 non-null, start searching with that row. IT gives the dimensions
17991 of the display. Value is null if matrix is empty; otherwise it is
17992 a pointer to the row found. */
17994 static struct glyph_row *
17995 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17996 struct glyph_row *start)
17998 struct glyph_row *row, *row_found;
18000 /* Set row_found to the last row in IT->w's current matrix
18001 displaying text. The loop looks funny but think of partially
18002 visible lines. */
18003 row_found = NULL;
18004 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18005 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18007 eassert (row->enabled_p);
18008 row_found = row;
18009 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18010 break;
18011 ++row;
18014 return row_found;
18018 /* Return the last row in the current matrix of W that is not affected
18019 by changes at the start of current_buffer that occurred since W's
18020 current matrix was built. Value is null if no such row exists.
18022 BEG_UNCHANGED us the number of characters unchanged at the start of
18023 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18024 first changed character in current_buffer. Characters at positions <
18025 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18026 when the current matrix was built. */
18028 static struct glyph_row *
18029 find_last_unchanged_at_beg_row (struct window *w)
18031 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18032 struct glyph_row *row;
18033 struct glyph_row *row_found = NULL;
18034 int yb = window_text_bottom_y (w);
18036 /* Find the last row displaying unchanged text. */
18037 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18038 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18039 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18040 ++row)
18042 if (/* If row ends before first_changed_pos, it is unchanged,
18043 except in some case. */
18044 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18045 /* When row ends in ZV and we write at ZV it is not
18046 unchanged. */
18047 && !row->ends_at_zv_p
18048 /* When first_changed_pos is the end of a continued line,
18049 row is not unchanged because it may be no longer
18050 continued. */
18051 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18052 && (row->continued_p
18053 || row->exact_window_width_line_p))
18054 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18055 needs to be recomputed, so don't consider this row as
18056 unchanged. This happens when the last line was
18057 bidi-reordered and was killed immediately before this
18058 redisplay cycle. In that case, ROW->end stores the
18059 buffer position of the first visual-order character of
18060 the killed text, which is now beyond ZV. */
18061 && CHARPOS (row->end.pos) <= ZV)
18062 row_found = row;
18064 /* Stop if last visible row. */
18065 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18066 break;
18069 return row_found;
18073 /* Find the first glyph row in the current matrix of W that is not
18074 affected by changes at the end of current_buffer since the
18075 time W's current matrix was built.
18077 Return in *DELTA the number of chars by which buffer positions in
18078 unchanged text at the end of current_buffer must be adjusted.
18080 Return in *DELTA_BYTES the corresponding number of bytes.
18082 Value is null if no such row exists, i.e. all rows are affected by
18083 changes. */
18085 static struct glyph_row *
18086 find_first_unchanged_at_end_row (struct window *w,
18087 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18089 struct glyph_row *row;
18090 struct glyph_row *row_found = NULL;
18092 *delta = *delta_bytes = 0;
18094 /* Display must not have been paused, otherwise the current matrix
18095 is not up to date. */
18096 eassert (w->window_end_valid);
18098 /* A value of window_end_pos >= END_UNCHANGED means that the window
18099 end is in the range of changed text. If so, there is no
18100 unchanged row at the end of W's current matrix. */
18101 if (w->window_end_pos >= END_UNCHANGED)
18102 return NULL;
18104 /* Set row to the last row in W's current matrix displaying text. */
18105 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18107 /* If matrix is entirely empty, no unchanged row exists. */
18108 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18110 /* The value of row is the last glyph row in the matrix having a
18111 meaningful buffer position in it. The end position of row
18112 corresponds to window_end_pos. This allows us to translate
18113 buffer positions in the current matrix to current buffer
18114 positions for characters not in changed text. */
18115 ptrdiff_t Z_old =
18116 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18117 ptrdiff_t Z_BYTE_old =
18118 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18119 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18120 struct glyph_row *first_text_row
18121 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18123 *delta = Z - Z_old;
18124 *delta_bytes = Z_BYTE - Z_BYTE_old;
18126 /* Set last_unchanged_pos to the buffer position of the last
18127 character in the buffer that has not been changed. Z is the
18128 index + 1 of the last character in current_buffer, i.e. by
18129 subtracting END_UNCHANGED we get the index of the last
18130 unchanged character, and we have to add BEG to get its buffer
18131 position. */
18132 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18133 last_unchanged_pos_old = last_unchanged_pos - *delta;
18135 /* Search backward from ROW for a row displaying a line that
18136 starts at a minimum position >= last_unchanged_pos_old. */
18137 for (; row > first_text_row; --row)
18139 /* This used to abort, but it can happen.
18140 It is ok to just stop the search instead here. KFS. */
18141 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18142 break;
18144 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18145 row_found = row;
18149 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18151 return row_found;
18155 /* Make sure that glyph rows in the current matrix of window W
18156 reference the same glyph memory as corresponding rows in the
18157 frame's frame matrix. This function is called after scrolling W's
18158 current matrix on a terminal frame in try_window_id and
18159 try_window_reusing_current_matrix. */
18161 static void
18162 sync_frame_with_window_matrix_rows (struct window *w)
18164 struct frame *f = XFRAME (w->frame);
18165 struct glyph_row *window_row, *window_row_end, *frame_row;
18167 /* Preconditions: W must be a leaf window and full-width. Its frame
18168 must have a frame matrix. */
18169 eassert (BUFFERP (w->contents));
18170 eassert (WINDOW_FULL_WIDTH_P (w));
18171 eassert (!FRAME_WINDOW_P (f));
18173 /* If W is a full-width window, glyph pointers in W's current matrix
18174 have, by definition, to be the same as glyph pointers in the
18175 corresponding frame matrix. Note that frame matrices have no
18176 marginal areas (see build_frame_matrix). */
18177 window_row = w->current_matrix->rows;
18178 window_row_end = window_row + w->current_matrix->nrows;
18179 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18180 while (window_row < window_row_end)
18182 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18183 struct glyph *end = window_row->glyphs[LAST_AREA];
18185 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18186 frame_row->glyphs[TEXT_AREA] = start;
18187 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18188 frame_row->glyphs[LAST_AREA] = end;
18190 /* Disable frame rows whose corresponding window rows have
18191 been disabled in try_window_id. */
18192 if (!window_row->enabled_p)
18193 frame_row->enabled_p = false;
18195 ++window_row, ++frame_row;
18200 /* Find the glyph row in window W containing CHARPOS. Consider all
18201 rows between START and END (not inclusive). END null means search
18202 all rows to the end of the display area of W. Value is the row
18203 containing CHARPOS or null. */
18205 struct glyph_row *
18206 row_containing_pos (struct window *w, ptrdiff_t charpos,
18207 struct glyph_row *start, struct glyph_row *end, int dy)
18209 struct glyph_row *row = start;
18210 struct glyph_row *best_row = NULL;
18211 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18212 int last_y;
18214 /* If we happen to start on a header-line, skip that. */
18215 if (row->mode_line_p)
18216 ++row;
18218 if ((end && row >= end) || !row->enabled_p)
18219 return NULL;
18221 last_y = window_text_bottom_y (w) - dy;
18223 while (true)
18225 /* Give up if we have gone too far. */
18226 if ((end && row >= end) || !row->enabled_p)
18227 return NULL;
18228 /* This formerly returned if they were equal.
18229 I think that both quantities are of a "last plus one" type;
18230 if so, when they are equal, the row is within the screen. -- rms. */
18231 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18232 return NULL;
18234 /* If it is in this row, return this row. */
18235 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18236 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18237 /* The end position of a row equals the start
18238 position of the next row. If CHARPOS is there, we
18239 would rather consider it displayed in the next
18240 line, except when this line ends in ZV. */
18241 && !row_for_charpos_p (row, charpos)))
18242 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18244 struct glyph *g;
18246 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18247 || (!best_row && !row->continued_p))
18248 return row;
18249 /* In bidi-reordered rows, there could be several rows whose
18250 edges surround CHARPOS, all of these rows belonging to
18251 the same continued line. We need to find the row which
18252 fits CHARPOS the best. */
18253 for (g = row->glyphs[TEXT_AREA];
18254 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18255 g++)
18257 if (!STRINGP (g->object))
18259 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18261 mindif = eabs (g->charpos - charpos);
18262 best_row = row;
18263 /* Exact match always wins. */
18264 if (mindif == 0)
18265 return best_row;
18270 else if (best_row && !row->continued_p)
18271 return best_row;
18272 ++row;
18277 /* Try to redisplay window W by reusing its existing display. W's
18278 current matrix must be up to date when this function is called,
18279 i.e., window_end_valid must be true.
18281 Value is
18283 >= 1 if successful, i.e. display has been updated
18284 specifically:
18285 1 means the changes were in front of a newline that precedes
18286 the window start, and the whole current matrix was reused
18287 2 means the changes were after the last position displayed
18288 in the window, and the whole current matrix was reused
18289 3 means portions of the current matrix were reused, while
18290 some of the screen lines were redrawn
18291 -1 if redisplay with same window start is known not to succeed
18292 0 if otherwise unsuccessful
18294 The following steps are performed:
18296 1. Find the last row in the current matrix of W that is not
18297 affected by changes at the start of current_buffer. If no such row
18298 is found, give up.
18300 2. Find the first row in W's current matrix that is not affected by
18301 changes at the end of current_buffer. Maybe there is no such row.
18303 3. Display lines beginning with the row + 1 found in step 1 to the
18304 row found in step 2 or, if step 2 didn't find a row, to the end of
18305 the window.
18307 4. If cursor is not known to appear on the window, give up.
18309 5. If display stopped at the row found in step 2, scroll the
18310 display and current matrix as needed.
18312 6. Maybe display some lines at the end of W, if we must. This can
18313 happen under various circumstances, like a partially visible line
18314 becoming fully visible, or because newly displayed lines are displayed
18315 in smaller font sizes.
18317 7. Update W's window end information. */
18319 static int
18320 try_window_id (struct window *w)
18322 struct frame *f = XFRAME (w->frame);
18323 struct glyph_matrix *current_matrix = w->current_matrix;
18324 struct glyph_matrix *desired_matrix = w->desired_matrix;
18325 struct glyph_row *last_unchanged_at_beg_row;
18326 struct glyph_row *first_unchanged_at_end_row;
18327 struct glyph_row *row;
18328 struct glyph_row *bottom_row;
18329 int bottom_vpos;
18330 struct it it;
18331 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18332 int dvpos, dy;
18333 struct text_pos start_pos;
18334 struct run run;
18335 int first_unchanged_at_end_vpos = 0;
18336 struct glyph_row *last_text_row, *last_text_row_at_end;
18337 struct text_pos start;
18338 ptrdiff_t first_changed_charpos, last_changed_charpos;
18340 #ifdef GLYPH_DEBUG
18341 if (inhibit_try_window_id)
18342 return 0;
18343 #endif
18345 /* This is handy for debugging. */
18346 #if false
18347 #define GIVE_UP(X) \
18348 do { \
18349 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18350 return 0; \
18351 } while (false)
18352 #else
18353 #define GIVE_UP(X) return 0
18354 #endif
18356 SET_TEXT_POS_FROM_MARKER (start, w->start);
18358 /* Don't use this for mini-windows because these can show
18359 messages and mini-buffers, and we don't handle that here. */
18360 if (MINI_WINDOW_P (w))
18361 GIVE_UP (1);
18363 /* This flag is used to prevent redisplay optimizations. */
18364 if (windows_or_buffers_changed || f->cursor_type_changed)
18365 GIVE_UP (2);
18367 /* This function's optimizations cannot be used if overlays have
18368 changed in the buffer displayed by the window, so give up if they
18369 have. */
18370 if (w->last_overlay_modified != OVERLAY_MODIFF)
18371 GIVE_UP (200);
18373 /* Verify that narrowing has not changed.
18374 Also verify that we were not told to prevent redisplay optimizations.
18375 It would be nice to further
18376 reduce the number of cases where this prevents try_window_id. */
18377 if (current_buffer->clip_changed
18378 || current_buffer->prevent_redisplay_optimizations_p)
18379 GIVE_UP (3);
18381 /* Window must either use window-based redisplay or be full width. */
18382 if (!FRAME_WINDOW_P (f)
18383 && (!FRAME_LINE_INS_DEL_OK (f)
18384 || !WINDOW_FULL_WIDTH_P (w)))
18385 GIVE_UP (4);
18387 /* Give up if point is known NOT to appear in W. */
18388 if (PT < CHARPOS (start))
18389 GIVE_UP (5);
18391 /* Another way to prevent redisplay optimizations. */
18392 if (w->last_modified == 0)
18393 GIVE_UP (6);
18395 /* Verify that window is not hscrolled. */
18396 if (w->hscroll != 0)
18397 GIVE_UP (7);
18399 /* Verify that display wasn't paused. */
18400 if (!w->window_end_valid)
18401 GIVE_UP (8);
18403 /* Likewise if highlighting trailing whitespace. */
18404 if (!NILP (Vshow_trailing_whitespace))
18405 GIVE_UP (11);
18407 /* Can't use this if overlay arrow position and/or string have
18408 changed. */
18409 if (overlay_arrows_changed_p (false))
18410 GIVE_UP (12);
18412 /* When word-wrap is on, adding a space to the first word of a
18413 wrapped line can change the wrap position, altering the line
18414 above it. It might be worthwhile to handle this more
18415 intelligently, but for now just redisplay from scratch. */
18416 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18417 GIVE_UP (21);
18419 /* Under bidi reordering, adding or deleting a character in the
18420 beginning of a paragraph, before the first strong directional
18421 character, can change the base direction of the paragraph (unless
18422 the buffer specifies a fixed paragraph direction), which will
18423 require redisplaying the whole paragraph. It might be worthwhile
18424 to find the paragraph limits and widen the range of redisplayed
18425 lines to that, but for now just give up this optimization and
18426 redisplay from scratch. */
18427 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18428 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18429 GIVE_UP (22);
18431 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18432 to that variable require thorough redisplay. */
18433 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18434 GIVE_UP (23);
18436 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18437 only if buffer has really changed. The reason is that the gap is
18438 initially at Z for freshly visited files. The code below would
18439 set end_unchanged to 0 in that case. */
18440 if (MODIFF > SAVE_MODIFF
18441 /* This seems to happen sometimes after saving a buffer. */
18442 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18444 if (GPT - BEG < BEG_UNCHANGED)
18445 BEG_UNCHANGED = GPT - BEG;
18446 if (Z - GPT < END_UNCHANGED)
18447 END_UNCHANGED = Z - GPT;
18450 /* The position of the first and last character that has been changed. */
18451 first_changed_charpos = BEG + BEG_UNCHANGED;
18452 last_changed_charpos = Z - END_UNCHANGED;
18454 /* If window starts after a line end, and the last change is in
18455 front of that newline, then changes don't affect the display.
18456 This case happens with stealth-fontification. Note that although
18457 the display is unchanged, glyph positions in the matrix have to
18458 be adjusted, of course. */
18459 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18460 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18461 && ((last_changed_charpos < CHARPOS (start)
18462 && CHARPOS (start) == BEGV)
18463 || (last_changed_charpos < CHARPOS (start) - 1
18464 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18466 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18467 struct glyph_row *r0;
18469 /* Compute how many chars/bytes have been added to or removed
18470 from the buffer. */
18471 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18472 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18473 Z_delta = Z - Z_old;
18474 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18476 /* Give up if PT is not in the window. Note that it already has
18477 been checked at the start of try_window_id that PT is not in
18478 front of the window start. */
18479 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18480 GIVE_UP (13);
18482 /* If window start is unchanged, we can reuse the whole matrix
18483 as is, after adjusting glyph positions. No need to compute
18484 the window end again, since its offset from Z hasn't changed. */
18485 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18486 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18487 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18488 /* PT must not be in a partially visible line. */
18489 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18490 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18492 /* Adjust positions in the glyph matrix. */
18493 if (Z_delta || Z_delta_bytes)
18495 struct glyph_row *r1
18496 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18497 increment_matrix_positions (w->current_matrix,
18498 MATRIX_ROW_VPOS (r0, current_matrix),
18499 MATRIX_ROW_VPOS (r1, current_matrix),
18500 Z_delta, Z_delta_bytes);
18503 /* Set the cursor. */
18504 row = row_containing_pos (w, PT, r0, NULL, 0);
18505 if (row)
18506 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18507 return 1;
18511 /* Handle the case that changes are all below what is displayed in
18512 the window, and that PT is in the window. This shortcut cannot
18513 be taken if ZV is visible in the window, and text has been added
18514 there that is visible in the window. */
18515 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18516 /* ZV is not visible in the window, or there are no
18517 changes at ZV, actually. */
18518 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18519 || first_changed_charpos == last_changed_charpos))
18521 struct glyph_row *r0;
18523 /* Give up if PT is not in the window. Note that it already has
18524 been checked at the start of try_window_id that PT is not in
18525 front of the window start. */
18526 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18527 GIVE_UP (14);
18529 /* If window start is unchanged, we can reuse the whole matrix
18530 as is, without changing glyph positions since no text has
18531 been added/removed in front of the window end. */
18532 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18533 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18534 /* PT must not be in a partially visible line. */
18535 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18536 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18538 /* We have to compute the window end anew since text
18539 could have been added/removed after it. */
18540 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18543 /* Set the cursor. */
18544 row = row_containing_pos (w, PT, r0, NULL, 0);
18545 if (row)
18546 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18547 return 2;
18551 /* Give up if window start is in the changed area.
18553 The condition used to read
18555 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18557 but why that was tested escapes me at the moment. */
18558 if (CHARPOS (start) >= first_changed_charpos
18559 && CHARPOS (start) <= last_changed_charpos)
18560 GIVE_UP (15);
18562 /* Check that window start agrees with the start of the first glyph
18563 row in its current matrix. Check this after we know the window
18564 start is not in changed text, otherwise positions would not be
18565 comparable. */
18566 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18567 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18568 GIVE_UP (16);
18570 /* Give up if the window ends in strings. Overlay strings
18571 at the end are difficult to handle, so don't try. */
18572 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18573 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18574 GIVE_UP (20);
18576 /* Compute the position at which we have to start displaying new
18577 lines. Some of the lines at the top of the window might be
18578 reusable because they are not displaying changed text. Find the
18579 last row in W's current matrix not affected by changes at the
18580 start of current_buffer. Value is null if changes start in the
18581 first line of window. */
18582 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18583 if (last_unchanged_at_beg_row)
18585 /* Avoid starting to display in the middle of a character, a TAB
18586 for instance. This is easier than to set up the iterator
18587 exactly, and it's not a frequent case, so the additional
18588 effort wouldn't really pay off. */
18589 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18590 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18591 && last_unchanged_at_beg_row > w->current_matrix->rows)
18592 --last_unchanged_at_beg_row;
18594 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18595 GIVE_UP (17);
18597 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18598 GIVE_UP (18);
18599 start_pos = it.current.pos;
18601 /* Start displaying new lines in the desired matrix at the same
18602 vpos we would use in the current matrix, i.e. below
18603 last_unchanged_at_beg_row. */
18604 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18605 current_matrix);
18606 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18607 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18609 eassert (it.hpos == 0 && it.current_x == 0);
18611 else
18613 /* There are no reusable lines at the start of the window.
18614 Start displaying in the first text line. */
18615 start_display (&it, w, start);
18616 it.vpos = it.first_vpos;
18617 start_pos = it.current.pos;
18620 /* Find the first row that is not affected by changes at the end of
18621 the buffer. Value will be null if there is no unchanged row, in
18622 which case we must redisplay to the end of the window. delta
18623 will be set to the value by which buffer positions beginning with
18624 first_unchanged_at_end_row have to be adjusted due to text
18625 changes. */
18626 first_unchanged_at_end_row
18627 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18628 IF_DEBUG (debug_delta = delta);
18629 IF_DEBUG (debug_delta_bytes = delta_bytes);
18631 /* Set stop_pos to the buffer position up to which we will have to
18632 display new lines. If first_unchanged_at_end_row != NULL, this
18633 is the buffer position of the start of the line displayed in that
18634 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18635 that we don't stop at a buffer position. */
18636 stop_pos = 0;
18637 if (first_unchanged_at_end_row)
18639 eassert (last_unchanged_at_beg_row == NULL
18640 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18642 /* If this is a continuation line, move forward to the next one
18643 that isn't. Changes in lines above affect this line.
18644 Caution: this may move first_unchanged_at_end_row to a row
18645 not displaying text. */
18646 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18647 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18648 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18649 < it.last_visible_y))
18650 ++first_unchanged_at_end_row;
18652 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18653 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18654 >= it.last_visible_y))
18655 first_unchanged_at_end_row = NULL;
18656 else
18658 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18659 + delta);
18660 first_unchanged_at_end_vpos
18661 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18662 eassert (stop_pos >= Z - END_UNCHANGED);
18665 else if (last_unchanged_at_beg_row == NULL)
18666 GIVE_UP (19);
18669 #ifdef GLYPH_DEBUG
18671 /* Either there is no unchanged row at the end, or the one we have
18672 now displays text. This is a necessary condition for the window
18673 end pos calculation at the end of this function. */
18674 eassert (first_unchanged_at_end_row == NULL
18675 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18677 debug_last_unchanged_at_beg_vpos
18678 = (last_unchanged_at_beg_row
18679 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18680 : -1);
18681 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18683 #endif /* GLYPH_DEBUG */
18686 /* Display new lines. Set last_text_row to the last new line
18687 displayed which has text on it, i.e. might end up as being the
18688 line where the window_end_vpos is. */
18689 w->cursor.vpos = -1;
18690 last_text_row = NULL;
18691 overlay_arrow_seen = false;
18692 if (it.current_y < it.last_visible_y
18693 && !f->fonts_changed
18694 && (first_unchanged_at_end_row == NULL
18695 || IT_CHARPOS (it) < stop_pos))
18696 it.glyph_row->reversed_p = false;
18697 while (it.current_y < it.last_visible_y
18698 && !f->fonts_changed
18699 && (first_unchanged_at_end_row == NULL
18700 || IT_CHARPOS (it) < stop_pos))
18702 if (display_line (&it, -1))
18703 last_text_row = it.glyph_row - 1;
18706 if (f->fonts_changed)
18707 return -1;
18709 /* The redisplay iterations in display_line above could have
18710 triggered font-lock, which could have done something that
18711 invalidates IT->w window's end-point information, on which we
18712 rely below. E.g., one package, which will remain unnamed, used
18713 to install a font-lock-fontify-region-function that called
18714 bury-buffer, whose side effect is to switch the buffer displayed
18715 by IT->w, and that predictably resets IT->w's window_end_valid
18716 flag, which we already tested at the entry to this function.
18717 Amply punish such packages/modes by giving up on this
18718 optimization in those cases. */
18719 if (!w->window_end_valid)
18721 clear_glyph_matrix (w->desired_matrix);
18722 return -1;
18725 /* Compute differences in buffer positions, y-positions etc. for
18726 lines reused at the bottom of the window. Compute what we can
18727 scroll. */
18728 if (first_unchanged_at_end_row
18729 /* No lines reused because we displayed everything up to the
18730 bottom of the window. */
18731 && it.current_y < it.last_visible_y)
18733 dvpos = (it.vpos
18734 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18735 current_matrix));
18736 dy = it.current_y - first_unchanged_at_end_row->y;
18737 run.current_y = first_unchanged_at_end_row->y;
18738 run.desired_y = run.current_y + dy;
18739 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18741 else
18743 delta = delta_bytes = dvpos = dy
18744 = run.current_y = run.desired_y = run.height = 0;
18745 first_unchanged_at_end_row = NULL;
18747 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18750 /* Find the cursor if not already found. We have to decide whether
18751 PT will appear on this window (it sometimes doesn't, but this is
18752 not a very frequent case.) This decision has to be made before
18753 the current matrix is altered. A value of cursor.vpos < 0 means
18754 that PT is either in one of the lines beginning at
18755 first_unchanged_at_end_row or below the window. Don't care for
18756 lines that might be displayed later at the window end; as
18757 mentioned, this is not a frequent case. */
18758 if (w->cursor.vpos < 0)
18760 /* Cursor in unchanged rows at the top? */
18761 if (PT < CHARPOS (start_pos)
18762 && last_unchanged_at_beg_row)
18764 row = row_containing_pos (w, PT,
18765 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18766 last_unchanged_at_beg_row + 1, 0);
18767 if (row)
18768 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18771 /* Start from first_unchanged_at_end_row looking for PT. */
18772 else if (first_unchanged_at_end_row)
18774 row = row_containing_pos (w, PT - delta,
18775 first_unchanged_at_end_row, NULL, 0);
18776 if (row)
18777 set_cursor_from_row (w, row, w->current_matrix, delta,
18778 delta_bytes, dy, dvpos);
18781 /* Give up if cursor was not found. */
18782 if (w->cursor.vpos < 0)
18784 clear_glyph_matrix (w->desired_matrix);
18785 return -1;
18789 /* Don't let the cursor end in the scroll margins. */
18791 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18792 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18794 if ((w->cursor.y < this_scroll_margin
18795 && CHARPOS (start) > BEGV)
18796 /* Old redisplay didn't take scroll margin into account at the bottom,
18797 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18798 || (w->cursor.y + (make_cursor_line_fully_visible_p
18799 ? cursor_height + this_scroll_margin
18800 : 1)) > it.last_visible_y)
18802 w->cursor.vpos = -1;
18803 clear_glyph_matrix (w->desired_matrix);
18804 return -1;
18808 /* Scroll the display. Do it before changing the current matrix so
18809 that xterm.c doesn't get confused about where the cursor glyph is
18810 found. */
18811 if (dy && run.height)
18813 update_begin (f);
18815 if (FRAME_WINDOW_P (f))
18817 FRAME_RIF (f)->update_window_begin_hook (w);
18818 FRAME_RIF (f)->clear_window_mouse_face (w);
18819 FRAME_RIF (f)->scroll_run_hook (w, &run);
18820 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18822 else
18824 /* Terminal frame. In this case, dvpos gives the number of
18825 lines to scroll by; dvpos < 0 means scroll up. */
18826 int from_vpos
18827 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18828 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18829 int end = (WINDOW_TOP_EDGE_LINE (w)
18830 + WINDOW_WANTS_HEADER_LINE_P (w)
18831 + window_internal_height (w));
18833 #if defined (HAVE_GPM) || defined (MSDOS)
18834 x_clear_window_mouse_face (w);
18835 #endif
18836 /* Perform the operation on the screen. */
18837 if (dvpos > 0)
18839 /* Scroll last_unchanged_at_beg_row to the end of the
18840 window down dvpos lines. */
18841 set_terminal_window (f, end);
18843 /* On dumb terminals delete dvpos lines at the end
18844 before inserting dvpos empty lines. */
18845 if (!FRAME_SCROLL_REGION_OK (f))
18846 ins_del_lines (f, end - dvpos, -dvpos);
18848 /* Insert dvpos empty lines in front of
18849 last_unchanged_at_beg_row. */
18850 ins_del_lines (f, from, dvpos);
18852 else if (dvpos < 0)
18854 /* Scroll up last_unchanged_at_beg_vpos to the end of
18855 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18856 set_terminal_window (f, end);
18858 /* Delete dvpos lines in front of
18859 last_unchanged_at_beg_vpos. ins_del_lines will set
18860 the cursor to the given vpos and emit |dvpos| delete
18861 line sequences. */
18862 ins_del_lines (f, from + dvpos, dvpos);
18864 /* On a dumb terminal insert dvpos empty lines at the
18865 end. */
18866 if (!FRAME_SCROLL_REGION_OK (f))
18867 ins_del_lines (f, end + dvpos, -dvpos);
18870 set_terminal_window (f, 0);
18873 update_end (f);
18876 /* Shift reused rows of the current matrix to the right position.
18877 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18878 text. */
18879 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18880 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18881 if (dvpos < 0)
18883 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18884 bottom_vpos, dvpos);
18885 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18886 bottom_vpos);
18888 else if (dvpos > 0)
18890 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18891 bottom_vpos, dvpos);
18892 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18893 first_unchanged_at_end_vpos + dvpos);
18896 /* For frame-based redisplay, make sure that current frame and window
18897 matrix are in sync with respect to glyph memory. */
18898 if (!FRAME_WINDOW_P (f))
18899 sync_frame_with_window_matrix_rows (w);
18901 /* Adjust buffer positions in reused rows. */
18902 if (delta || delta_bytes)
18903 increment_matrix_positions (current_matrix,
18904 first_unchanged_at_end_vpos + dvpos,
18905 bottom_vpos, delta, delta_bytes);
18907 /* Adjust Y positions. */
18908 if (dy)
18909 shift_glyph_matrix (w, current_matrix,
18910 first_unchanged_at_end_vpos + dvpos,
18911 bottom_vpos, dy);
18913 if (first_unchanged_at_end_row)
18915 first_unchanged_at_end_row += dvpos;
18916 if (first_unchanged_at_end_row->y >= it.last_visible_y
18917 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18918 first_unchanged_at_end_row = NULL;
18921 /* If scrolling up, there may be some lines to display at the end of
18922 the window. */
18923 last_text_row_at_end = NULL;
18924 if (dy < 0)
18926 /* Scrolling up can leave for example a partially visible line
18927 at the end of the window to be redisplayed. */
18928 /* Set last_row to the glyph row in the current matrix where the
18929 window end line is found. It has been moved up or down in
18930 the matrix by dvpos. */
18931 int last_vpos = w->window_end_vpos + dvpos;
18932 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18934 /* If last_row is the window end line, it should display text. */
18935 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18937 /* If window end line was partially visible before, begin
18938 displaying at that line. Otherwise begin displaying with the
18939 line following it. */
18940 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18942 init_to_row_start (&it, w, last_row);
18943 it.vpos = last_vpos;
18944 it.current_y = last_row->y;
18946 else
18948 init_to_row_end (&it, w, last_row);
18949 it.vpos = 1 + last_vpos;
18950 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18951 ++last_row;
18954 /* We may start in a continuation line. If so, we have to
18955 get the right continuation_lines_width and current_x. */
18956 it.continuation_lines_width = last_row->continuation_lines_width;
18957 it.hpos = it.current_x = 0;
18959 /* Display the rest of the lines at the window end. */
18960 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18961 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18963 /* Is it always sure that the display agrees with lines in
18964 the current matrix? I don't think so, so we mark rows
18965 displayed invalid in the current matrix by setting their
18966 enabled_p flag to false. */
18967 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18968 if (display_line (&it, w->cursor.vpos))
18969 last_text_row_at_end = it.glyph_row - 1;
18973 /* Update window_end_pos and window_end_vpos. */
18974 if (first_unchanged_at_end_row && !last_text_row_at_end)
18976 /* Window end line if one of the preserved rows from the current
18977 matrix. Set row to the last row displaying text in current
18978 matrix starting at first_unchanged_at_end_row, after
18979 scrolling. */
18980 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18981 row = find_last_row_displaying_text (w->current_matrix, &it,
18982 first_unchanged_at_end_row);
18983 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18984 adjust_window_ends (w, row, true);
18985 eassert (w->window_end_bytepos >= 0);
18986 IF_DEBUG (debug_method_add (w, "A"));
18988 else if (last_text_row_at_end)
18990 adjust_window_ends (w, last_text_row_at_end, false);
18991 eassert (w->window_end_bytepos >= 0);
18992 IF_DEBUG (debug_method_add (w, "B"));
18994 else if (last_text_row)
18996 /* We have displayed either to the end of the window or at the
18997 end of the window, i.e. the last row with text is to be found
18998 in the desired matrix. */
18999 adjust_window_ends (w, last_text_row, false);
19000 eassert (w->window_end_bytepos >= 0);
19002 else if (first_unchanged_at_end_row == NULL
19003 && last_text_row == NULL
19004 && last_text_row_at_end == NULL)
19006 /* Displayed to end of window, but no line containing text was
19007 displayed. Lines were deleted at the end of the window. */
19008 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
19009 int vpos = w->window_end_vpos;
19010 struct glyph_row *current_row = current_matrix->rows + vpos;
19011 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19013 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19015 eassert (first_vpos <= vpos);
19016 if (desired_row->enabled_p)
19018 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19019 row = desired_row;
19021 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19022 row = current_row;
19025 w->window_end_vpos = vpos + 1;
19026 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19027 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19028 eassert (w->window_end_bytepos >= 0);
19029 IF_DEBUG (debug_method_add (w, "C"));
19031 else
19032 emacs_abort ();
19034 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19035 debug_end_vpos = w->window_end_vpos));
19037 /* Record that display has not been completed. */
19038 w->window_end_valid = false;
19039 w->desired_matrix->no_scrolling_p = true;
19040 return 3;
19042 #undef GIVE_UP
19047 /***********************************************************************
19048 More debugging support
19049 ***********************************************************************/
19051 #ifdef GLYPH_DEBUG
19053 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19054 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19055 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19058 /* Dump the contents of glyph matrix MATRIX on stderr.
19060 GLYPHS 0 means don't show glyph contents.
19061 GLYPHS 1 means show glyphs in short form
19062 GLYPHS > 1 means show glyphs in long form. */
19064 void
19065 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19067 int i;
19068 for (i = 0; i < matrix->nrows; ++i)
19069 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19073 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19074 the glyph row and area where the glyph comes from. */
19076 void
19077 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19079 if (glyph->type == CHAR_GLYPH
19080 || glyph->type == GLYPHLESS_GLYPH)
19082 fprintf (stderr,
19083 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19084 glyph - row->glyphs[TEXT_AREA],
19085 (glyph->type == CHAR_GLYPH
19086 ? 'C'
19087 : 'G'),
19088 glyph->charpos,
19089 (BUFFERP (glyph->object)
19090 ? 'B'
19091 : (STRINGP (glyph->object)
19092 ? 'S'
19093 : (NILP (glyph->object)
19094 ? '0'
19095 : '-'))),
19096 glyph->pixel_width,
19097 glyph->u.ch,
19098 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19099 ? (int) glyph->u.ch
19100 : '.'),
19101 glyph->face_id,
19102 glyph->left_box_line_p,
19103 glyph->right_box_line_p);
19105 else if (glyph->type == STRETCH_GLYPH)
19107 fprintf (stderr,
19108 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19109 glyph - row->glyphs[TEXT_AREA],
19110 'S',
19111 glyph->charpos,
19112 (BUFFERP (glyph->object)
19113 ? 'B'
19114 : (STRINGP (glyph->object)
19115 ? 'S'
19116 : (NILP (glyph->object)
19117 ? '0'
19118 : '-'))),
19119 glyph->pixel_width,
19121 ' ',
19122 glyph->face_id,
19123 glyph->left_box_line_p,
19124 glyph->right_box_line_p);
19126 else if (glyph->type == IMAGE_GLYPH)
19128 fprintf (stderr,
19129 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19130 glyph - row->glyphs[TEXT_AREA],
19131 'I',
19132 glyph->charpos,
19133 (BUFFERP (glyph->object)
19134 ? 'B'
19135 : (STRINGP (glyph->object)
19136 ? 'S'
19137 : (NILP (glyph->object)
19138 ? '0'
19139 : '-'))),
19140 glyph->pixel_width,
19141 (unsigned int) glyph->u.img_id,
19142 '.',
19143 glyph->face_id,
19144 glyph->left_box_line_p,
19145 glyph->right_box_line_p);
19147 else if (glyph->type == COMPOSITE_GLYPH)
19149 fprintf (stderr,
19150 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19151 glyph - row->glyphs[TEXT_AREA],
19152 '+',
19153 glyph->charpos,
19154 (BUFFERP (glyph->object)
19155 ? 'B'
19156 : (STRINGP (glyph->object)
19157 ? 'S'
19158 : (NILP (glyph->object)
19159 ? '0'
19160 : '-'))),
19161 glyph->pixel_width,
19162 (unsigned int) glyph->u.cmp.id);
19163 if (glyph->u.cmp.automatic)
19164 fprintf (stderr,
19165 "[%d-%d]",
19166 glyph->slice.cmp.from, glyph->slice.cmp.to);
19167 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19168 glyph->face_id,
19169 glyph->left_box_line_p,
19170 glyph->right_box_line_p);
19172 else if (glyph->type == XWIDGET_GLYPH)
19174 #ifndef HAVE_XWIDGETS
19175 eassume (false);
19176 #else
19177 fprintf (stderr,
19178 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19179 glyph - row->glyphs[TEXT_AREA],
19180 'X',
19181 glyph->charpos,
19182 (BUFFERP (glyph->object)
19183 ? 'B'
19184 : (STRINGP (glyph->object)
19185 ? 'S'
19186 : '-')),
19187 glyph->pixel_width,
19188 glyph->u.xwidget,
19189 '.',
19190 glyph->face_id,
19191 glyph->left_box_line_p,
19192 glyph->right_box_line_p);
19193 #endif
19198 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19199 GLYPHS 0 means don't show glyph contents.
19200 GLYPHS 1 means show glyphs in short form
19201 GLYPHS > 1 means show glyphs in long form. */
19203 void
19204 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19206 if (glyphs != 1)
19208 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19209 fprintf (stderr, "==============================================================================\n");
19211 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19212 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19213 vpos,
19214 MATRIX_ROW_START_CHARPOS (row),
19215 MATRIX_ROW_END_CHARPOS (row),
19216 row->used[TEXT_AREA],
19217 row->contains_overlapping_glyphs_p,
19218 row->enabled_p,
19219 row->truncated_on_left_p,
19220 row->truncated_on_right_p,
19221 row->continued_p,
19222 MATRIX_ROW_CONTINUATION_LINE_P (row),
19223 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19224 row->ends_at_zv_p,
19225 row->fill_line_p,
19226 row->ends_in_middle_of_char_p,
19227 row->starts_in_middle_of_char_p,
19228 row->mouse_face_p,
19229 row->x,
19230 row->y,
19231 row->pixel_width,
19232 row->height,
19233 row->visible_height,
19234 row->ascent,
19235 row->phys_ascent);
19236 /* The next 3 lines should align to "Start" in the header. */
19237 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19238 row->end.overlay_string_index,
19239 row->continuation_lines_width);
19240 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19241 CHARPOS (row->start.string_pos),
19242 CHARPOS (row->end.string_pos));
19243 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19244 row->end.dpvec_index);
19247 if (glyphs > 1)
19249 int area;
19251 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19253 struct glyph *glyph = row->glyphs[area];
19254 struct glyph *glyph_end = glyph + row->used[area];
19256 /* Glyph for a line end in text. */
19257 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19258 ++glyph_end;
19260 if (glyph < glyph_end)
19261 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19263 for (; glyph < glyph_end; ++glyph)
19264 dump_glyph (row, glyph, area);
19267 else if (glyphs == 1)
19269 int area;
19270 char s[SHRT_MAX + 4];
19272 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19274 int i;
19276 for (i = 0; i < row->used[area]; ++i)
19278 struct glyph *glyph = row->glyphs[area] + i;
19279 if (i == row->used[area] - 1
19280 && area == TEXT_AREA
19281 && NILP (glyph->object)
19282 && glyph->type == CHAR_GLYPH
19283 && glyph->u.ch == ' ')
19285 strcpy (&s[i], "[\\n]");
19286 i += 4;
19288 else if (glyph->type == CHAR_GLYPH
19289 && glyph->u.ch < 0x80
19290 && glyph->u.ch >= ' ')
19291 s[i] = glyph->u.ch;
19292 else
19293 s[i] = '.';
19296 s[i] = '\0';
19297 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19303 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19304 Sdump_glyph_matrix, 0, 1, "p",
19305 doc: /* Dump the current matrix of the selected window to stderr.
19306 Shows contents of glyph row structures. With non-nil
19307 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19308 glyphs in short form, otherwise show glyphs in long form.
19310 Interactively, no argument means show glyphs in short form;
19311 with numeric argument, its value is passed as the GLYPHS flag. */)
19312 (Lisp_Object glyphs)
19314 struct window *w = XWINDOW (selected_window);
19315 struct buffer *buffer = XBUFFER (w->contents);
19317 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19318 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19319 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19320 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19321 fprintf (stderr, "=============================================\n");
19322 dump_glyph_matrix (w->current_matrix,
19323 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19324 return Qnil;
19328 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19329 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19330 Only text-mode frames have frame glyph matrices. */)
19331 (void)
19333 struct frame *f = XFRAME (selected_frame);
19335 if (f->current_matrix)
19336 dump_glyph_matrix (f->current_matrix, 1);
19337 else
19338 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19339 return Qnil;
19343 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19344 doc: /* Dump glyph row ROW to stderr.
19345 GLYPH 0 means don't dump glyphs.
19346 GLYPH 1 means dump glyphs in short form.
19347 GLYPH > 1 or omitted means dump glyphs in long form. */)
19348 (Lisp_Object row, Lisp_Object glyphs)
19350 struct glyph_matrix *matrix;
19351 EMACS_INT vpos;
19353 CHECK_NUMBER (row);
19354 matrix = XWINDOW (selected_window)->current_matrix;
19355 vpos = XINT (row);
19356 if (vpos >= 0 && vpos < matrix->nrows)
19357 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19358 vpos,
19359 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19360 return Qnil;
19364 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19365 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19366 GLYPH 0 means don't dump glyphs.
19367 GLYPH 1 means dump glyphs in short form.
19368 GLYPH > 1 or omitted means dump glyphs in long form.
19370 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19371 do nothing. */)
19372 (Lisp_Object row, Lisp_Object glyphs)
19374 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19375 struct frame *sf = SELECTED_FRAME ();
19376 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19377 EMACS_INT vpos;
19379 CHECK_NUMBER (row);
19380 vpos = XINT (row);
19381 if (vpos >= 0 && vpos < m->nrows)
19382 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19383 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19384 #endif
19385 return Qnil;
19389 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19390 doc: /* Toggle tracing of redisplay.
19391 With ARG, turn tracing on if and only if ARG is positive. */)
19392 (Lisp_Object arg)
19394 if (NILP (arg))
19395 trace_redisplay_p = !trace_redisplay_p;
19396 else
19398 arg = Fprefix_numeric_value (arg);
19399 trace_redisplay_p = XINT (arg) > 0;
19402 return Qnil;
19406 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19407 doc: /* Like `format', but print result to stderr.
19408 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19409 (ptrdiff_t nargs, Lisp_Object *args)
19411 Lisp_Object s = Fformat (nargs, args);
19412 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19413 return Qnil;
19416 #endif /* GLYPH_DEBUG */
19420 /***********************************************************************
19421 Building Desired Matrix Rows
19422 ***********************************************************************/
19424 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19425 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19427 static struct glyph_row *
19428 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19430 struct frame *f = XFRAME (WINDOW_FRAME (w));
19431 struct buffer *buffer = XBUFFER (w->contents);
19432 struct buffer *old = current_buffer;
19433 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19434 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19435 const unsigned char *arrow_end = arrow_string + arrow_len;
19436 const unsigned char *p;
19437 struct it it;
19438 bool multibyte_p;
19439 int n_glyphs_before;
19441 set_buffer_temp (buffer);
19442 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19443 scratch_glyph_row.reversed_p = false;
19444 it.glyph_row->used[TEXT_AREA] = 0;
19445 SET_TEXT_POS (it.position, 0, 0);
19447 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19448 p = arrow_string;
19449 while (p < arrow_end)
19451 Lisp_Object face, ilisp;
19453 /* Get the next character. */
19454 if (multibyte_p)
19455 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19456 else
19458 it.c = it.char_to_display = *p, it.len = 1;
19459 if (! ASCII_CHAR_P (it.c))
19460 it.char_to_display = BYTE8_TO_CHAR (it.c);
19462 p += it.len;
19464 /* Get its face. */
19465 ilisp = make_number (p - arrow_string);
19466 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19467 it.face_id = compute_char_face (f, it.char_to_display, face);
19469 /* Compute its width, get its glyphs. */
19470 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19471 SET_TEXT_POS (it.position, -1, -1);
19472 PRODUCE_GLYPHS (&it);
19474 /* If this character doesn't fit any more in the line, we have
19475 to remove some glyphs. */
19476 if (it.current_x > it.last_visible_x)
19478 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19479 break;
19483 set_buffer_temp (old);
19484 return it.glyph_row;
19488 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19489 glyphs to insert is determined by produce_special_glyphs. */
19491 static void
19492 insert_left_trunc_glyphs (struct it *it)
19494 struct it truncate_it;
19495 struct glyph *from, *end, *to, *toend;
19497 eassert (!FRAME_WINDOW_P (it->f)
19498 || (!it->glyph_row->reversed_p
19499 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19500 || (it->glyph_row->reversed_p
19501 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19503 /* Get the truncation glyphs. */
19504 truncate_it = *it;
19505 truncate_it.current_x = 0;
19506 truncate_it.face_id = DEFAULT_FACE_ID;
19507 truncate_it.glyph_row = &scratch_glyph_row;
19508 truncate_it.area = TEXT_AREA;
19509 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19510 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19511 truncate_it.object = Qnil;
19512 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19514 /* Overwrite glyphs from IT with truncation glyphs. */
19515 if (!it->glyph_row->reversed_p)
19517 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19519 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19520 end = from + tused;
19521 to = it->glyph_row->glyphs[TEXT_AREA];
19522 toend = to + it->glyph_row->used[TEXT_AREA];
19523 if (FRAME_WINDOW_P (it->f))
19525 /* On GUI frames, when variable-size fonts are displayed,
19526 the truncation glyphs may need more pixels than the row's
19527 glyphs they overwrite. We overwrite more glyphs to free
19528 enough screen real estate, and enlarge the stretch glyph
19529 on the right (see display_line), if there is one, to
19530 preserve the screen position of the truncation glyphs on
19531 the right. */
19532 int w = 0;
19533 struct glyph *g = to;
19534 short used;
19536 /* The first glyph could be partially visible, in which case
19537 it->glyph_row->x will be negative. But we want the left
19538 truncation glyphs to be aligned at the left margin of the
19539 window, so we override the x coordinate at which the row
19540 will begin. */
19541 it->glyph_row->x = 0;
19542 while (g < toend && w < it->truncation_pixel_width)
19544 w += g->pixel_width;
19545 ++g;
19547 if (g - to - tused > 0)
19549 memmove (to + tused, g, (toend - g) * sizeof(*g));
19550 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19552 used = it->glyph_row->used[TEXT_AREA];
19553 if (it->glyph_row->truncated_on_right_p
19554 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19555 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19556 == STRETCH_GLYPH)
19558 int extra = w - it->truncation_pixel_width;
19560 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19564 while (from < end)
19565 *to++ = *from++;
19567 /* There may be padding glyphs left over. Overwrite them too. */
19568 if (!FRAME_WINDOW_P (it->f))
19570 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19572 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19573 while (from < end)
19574 *to++ = *from++;
19578 if (to > toend)
19579 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19581 else
19583 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19585 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19586 that back to front. */
19587 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19588 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19589 toend = it->glyph_row->glyphs[TEXT_AREA];
19590 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19591 if (FRAME_WINDOW_P (it->f))
19593 int w = 0;
19594 struct glyph *g = to;
19596 while (g >= toend && w < it->truncation_pixel_width)
19598 w += g->pixel_width;
19599 --g;
19601 if (to - g - tused > 0)
19602 to = g + tused;
19603 if (it->glyph_row->truncated_on_right_p
19604 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19605 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19607 int extra = w - it->truncation_pixel_width;
19609 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19613 while (from >= end && to >= toend)
19614 *to-- = *from--;
19615 if (!FRAME_WINDOW_P (it->f))
19617 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19619 from =
19620 truncate_it.glyph_row->glyphs[TEXT_AREA]
19621 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19622 while (from >= end && to >= toend)
19623 *to-- = *from--;
19626 if (from >= end)
19628 /* Need to free some room before prepending additional
19629 glyphs. */
19630 int move_by = from - end + 1;
19631 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19632 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19634 for ( ; g >= g0; g--)
19635 g[move_by] = *g;
19636 while (from >= end)
19637 *to-- = *from--;
19638 it->glyph_row->used[TEXT_AREA] += move_by;
19643 /* Compute the hash code for ROW. */
19644 unsigned
19645 row_hash (struct glyph_row *row)
19647 int area, k;
19648 unsigned hashval = 0;
19650 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19651 for (k = 0; k < row->used[area]; ++k)
19652 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19653 + row->glyphs[area][k].u.val
19654 + row->glyphs[area][k].face_id
19655 + row->glyphs[area][k].padding_p
19656 + (row->glyphs[area][k].type << 2));
19658 return hashval;
19661 /* Compute the pixel height and width of IT->glyph_row.
19663 Most of the time, ascent and height of a display line will be equal
19664 to the max_ascent and max_height values of the display iterator
19665 structure. This is not the case if
19667 1. We hit ZV without displaying anything. In this case, max_ascent
19668 and max_height will be zero.
19670 2. We have some glyphs that don't contribute to the line height.
19671 (The glyph row flag contributes_to_line_height_p is for future
19672 pixmap extensions).
19674 The first case is easily covered by using default values because in
19675 these cases, the line height does not really matter, except that it
19676 must not be zero. */
19678 static void
19679 compute_line_metrics (struct it *it)
19681 struct glyph_row *row = it->glyph_row;
19683 if (FRAME_WINDOW_P (it->f))
19685 int i, min_y, max_y;
19687 /* The line may consist of one space only, that was added to
19688 place the cursor on it. If so, the row's height hasn't been
19689 computed yet. */
19690 if (row->height == 0)
19692 if (it->max_ascent + it->max_descent == 0)
19693 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19694 row->ascent = it->max_ascent;
19695 row->height = it->max_ascent + it->max_descent;
19696 row->phys_ascent = it->max_phys_ascent;
19697 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19698 row->extra_line_spacing = it->max_extra_line_spacing;
19701 /* Compute the width of this line. */
19702 row->pixel_width = row->x;
19703 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19704 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19706 eassert (row->pixel_width >= 0);
19707 eassert (row->ascent >= 0 && row->height > 0);
19709 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19710 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19712 /* If first line's physical ascent is larger than its logical
19713 ascent, use the physical ascent, and make the row taller.
19714 This makes accented characters fully visible. */
19715 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19716 && row->phys_ascent > row->ascent)
19718 row->height += row->phys_ascent - row->ascent;
19719 row->ascent = row->phys_ascent;
19722 /* Compute how much of the line is visible. */
19723 row->visible_height = row->height;
19725 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19726 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19728 if (row->y < min_y)
19729 row->visible_height -= min_y - row->y;
19730 if (row->y + row->height > max_y)
19731 row->visible_height -= row->y + row->height - max_y;
19733 else
19735 row->pixel_width = row->used[TEXT_AREA];
19736 if (row->continued_p)
19737 row->pixel_width -= it->continuation_pixel_width;
19738 else if (row->truncated_on_right_p)
19739 row->pixel_width -= it->truncation_pixel_width;
19740 row->ascent = row->phys_ascent = 0;
19741 row->height = row->phys_height = row->visible_height = 1;
19742 row->extra_line_spacing = 0;
19745 /* Compute a hash code for this row. */
19746 row->hash = row_hash (row);
19748 it->max_ascent = it->max_descent = 0;
19749 it->max_phys_ascent = it->max_phys_descent = 0;
19753 /* Append one space to the glyph row of iterator IT if doing a
19754 window-based redisplay. The space has the same face as
19755 IT->face_id. Value is true if a space was added.
19757 This function is called to make sure that there is always one glyph
19758 at the end of a glyph row that the cursor can be set on under
19759 window-systems. (If there weren't such a glyph we would not know
19760 how wide and tall a box cursor should be displayed).
19762 At the same time this space let's a nicely handle clearing to the
19763 end of the line if the row ends in italic text. */
19765 static bool
19766 append_space_for_newline (struct it *it, bool default_face_p)
19768 if (FRAME_WINDOW_P (it->f))
19770 int n = it->glyph_row->used[TEXT_AREA];
19772 if (it->glyph_row->glyphs[TEXT_AREA] + n
19773 < it->glyph_row->glyphs[1 + TEXT_AREA])
19775 /* Save some values that must not be changed.
19776 Must save IT->c and IT->len because otherwise
19777 ITERATOR_AT_END_P wouldn't work anymore after
19778 append_space_for_newline has been called. */
19779 enum display_element_type saved_what = it->what;
19780 int saved_c = it->c, saved_len = it->len;
19781 int saved_char_to_display = it->char_to_display;
19782 int saved_x = it->current_x;
19783 int saved_face_id = it->face_id;
19784 bool saved_box_end = it->end_of_box_run_p;
19785 struct text_pos saved_pos;
19786 Lisp_Object saved_object;
19787 struct face *face;
19789 saved_object = it->object;
19790 saved_pos = it->position;
19792 it->what = IT_CHARACTER;
19793 memset (&it->position, 0, sizeof it->position);
19794 it->object = Qnil;
19795 it->c = it->char_to_display = ' ';
19796 it->len = 1;
19798 /* If the default face was remapped, be sure to use the
19799 remapped face for the appended newline. */
19800 if (default_face_p)
19801 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19802 else if (it->face_before_selective_p)
19803 it->face_id = it->saved_face_id;
19804 face = FACE_FROM_ID (it->f, it->face_id);
19805 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19806 /* In R2L rows, we will prepend a stretch glyph that will
19807 have the end_of_box_run_p flag set for it, so there's no
19808 need for the appended newline glyph to have that flag
19809 set. */
19810 if (it->glyph_row->reversed_p
19811 /* But if the appended newline glyph goes all the way to
19812 the end of the row, there will be no stretch glyph,
19813 so leave the box flag set. */
19814 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19815 it->end_of_box_run_p = false;
19817 PRODUCE_GLYPHS (it);
19819 #ifdef HAVE_WINDOW_SYSTEM
19820 /* Make sure this space glyph has the right ascent and
19821 descent values, or else cursor at end of line will look
19822 funny, and height of empty lines will be incorrect. */
19823 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19824 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19825 if (n == 0)
19827 Lisp_Object height, total_height;
19828 int extra_line_spacing = it->extra_line_spacing;
19829 int boff = font->baseline_offset;
19831 if (font->vertical_centering)
19832 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19834 it->object = saved_object; /* get_it_property needs this */
19835 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19836 /* Must do a subset of line height processing from
19837 x_produce_glyph for newline characters. */
19838 height = get_it_property (it, Qline_height);
19839 if (CONSP (height)
19840 && CONSP (XCDR (height))
19841 && NILP (XCDR (XCDR (height))))
19843 total_height = XCAR (XCDR (height));
19844 height = XCAR (height);
19846 else
19847 total_height = Qnil;
19848 height = calc_line_height_property (it, height, font, boff, true);
19850 if (it->override_ascent >= 0)
19852 it->ascent = it->override_ascent;
19853 it->descent = it->override_descent;
19854 boff = it->override_boff;
19856 if (EQ (height, Qt))
19857 extra_line_spacing = 0;
19858 else
19860 Lisp_Object spacing;
19862 it->phys_ascent = it->ascent;
19863 it->phys_descent = it->descent;
19864 if (!NILP (height)
19865 && XINT (height) > it->ascent + it->descent)
19866 it->ascent = XINT (height) - it->descent;
19868 if (!NILP (total_height))
19869 spacing = calc_line_height_property (it, total_height, font,
19870 boff, false);
19871 else
19873 spacing = get_it_property (it, Qline_spacing);
19874 spacing = calc_line_height_property (it, spacing, font,
19875 boff, false);
19877 if (INTEGERP (spacing))
19879 extra_line_spacing = XINT (spacing);
19880 if (!NILP (total_height))
19881 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19884 if (extra_line_spacing > 0)
19886 it->descent += extra_line_spacing;
19887 if (extra_line_spacing > it->max_extra_line_spacing)
19888 it->max_extra_line_spacing = extra_line_spacing;
19890 it->max_ascent = it->ascent;
19891 it->max_descent = it->descent;
19892 /* Make sure compute_line_metrics recomputes the row height. */
19893 it->glyph_row->height = 0;
19896 g->ascent = it->max_ascent;
19897 g->descent = it->max_descent;
19898 #endif
19900 it->override_ascent = -1;
19901 it->constrain_row_ascent_descent_p = false;
19902 it->current_x = saved_x;
19903 it->object = saved_object;
19904 it->position = saved_pos;
19905 it->what = saved_what;
19906 it->face_id = saved_face_id;
19907 it->len = saved_len;
19908 it->c = saved_c;
19909 it->char_to_display = saved_char_to_display;
19910 it->end_of_box_run_p = saved_box_end;
19911 return true;
19915 return false;
19919 /* Extend the face of the last glyph in the text area of IT->glyph_row
19920 to the end of the display line. Called from display_line. If the
19921 glyph row is empty, add a space glyph to it so that we know the
19922 face to draw. Set the glyph row flag fill_line_p. If the glyph
19923 row is R2L, prepend a stretch glyph to cover the empty space to the
19924 left of the leftmost glyph. */
19926 static void
19927 extend_face_to_end_of_line (struct it *it)
19929 struct face *face, *default_face;
19930 struct frame *f = it->f;
19932 /* If line is already filled, do nothing. Non window-system frames
19933 get a grace of one more ``pixel'' because their characters are
19934 1-``pixel'' wide, so they hit the equality too early. This grace
19935 is needed only for R2L rows that are not continued, to produce
19936 one extra blank where we could display the cursor. */
19937 if ((it->current_x >= it->last_visible_x
19938 + (!FRAME_WINDOW_P (f)
19939 && it->glyph_row->reversed_p
19940 && !it->glyph_row->continued_p))
19941 /* If the window has display margins, we will need to extend
19942 their face even if the text area is filled. */
19943 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19944 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19945 return;
19947 /* The default face, possibly remapped. */
19948 default_face = FACE_FROM_ID_OR_NULL (f,
19949 lookup_basic_face (f, DEFAULT_FACE_ID));
19951 /* Face extension extends the background and box of IT->face_id
19952 to the end of the line. If the background equals the background
19953 of the frame, we don't have to do anything. */
19954 face = FACE_FROM_ID (f, (it->face_before_selective_p
19955 ? it->saved_face_id
19956 : it->face_id));
19958 if (FRAME_WINDOW_P (f)
19959 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19960 && face->box == FACE_NO_BOX
19961 && face->background == FRAME_BACKGROUND_PIXEL (f)
19962 #ifdef HAVE_WINDOW_SYSTEM
19963 && !face->stipple
19964 #endif
19965 && !it->glyph_row->reversed_p)
19966 return;
19968 /* Set the glyph row flag indicating that the face of the last glyph
19969 in the text area has to be drawn to the end of the text area. */
19970 it->glyph_row->fill_line_p = true;
19972 /* If current character of IT is not ASCII, make sure we have the
19973 ASCII face. This will be automatically undone the next time
19974 get_next_display_element returns a multibyte character. Note
19975 that the character will always be single byte in unibyte
19976 text. */
19977 if (!ASCII_CHAR_P (it->c))
19979 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19982 if (FRAME_WINDOW_P (f))
19984 /* If the row is empty, add a space with the current face of IT,
19985 so that we know which face to draw. */
19986 if (it->glyph_row->used[TEXT_AREA] == 0)
19988 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19989 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19990 it->glyph_row->used[TEXT_AREA] = 1;
19992 /* Mode line and the header line don't have margins, and
19993 likewise the frame's tool-bar window, if there is any. */
19994 if (!(it->glyph_row->mode_line_p
19995 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19996 || (WINDOWP (f->tool_bar_window)
19997 && it->w == XWINDOW (f->tool_bar_window))
19998 #endif
20001 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20002 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20004 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20005 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20006 default_face->id;
20007 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20009 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20010 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20012 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20013 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20014 default_face->id;
20015 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20018 #ifdef HAVE_WINDOW_SYSTEM
20019 if (it->glyph_row->reversed_p)
20021 /* Prepend a stretch glyph to the row, such that the
20022 rightmost glyph will be drawn flushed all the way to the
20023 right margin of the window. The stretch glyph that will
20024 occupy the empty space, if any, to the left of the
20025 glyphs. */
20026 struct font *font = face->font ? face->font : FRAME_FONT (f);
20027 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20028 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20029 struct glyph *g;
20030 int row_width, stretch_ascent, stretch_width;
20031 struct text_pos saved_pos;
20032 int saved_face_id;
20033 bool saved_avoid_cursor, saved_box_start;
20035 for (row_width = 0, g = row_start; g < row_end; g++)
20036 row_width += g->pixel_width;
20038 /* FIXME: There are various minor display glitches in R2L
20039 rows when only one of the fringes is missing. The
20040 strange condition below produces the least bad effect. */
20041 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20042 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20043 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20044 stretch_width = window_box_width (it->w, TEXT_AREA);
20045 else
20046 stretch_width = it->last_visible_x - it->first_visible_x;
20047 stretch_width -= row_width;
20049 if (stretch_width > 0)
20051 stretch_ascent =
20052 (((it->ascent + it->descent)
20053 * FONT_BASE (font)) / FONT_HEIGHT (font));
20054 saved_pos = it->position;
20055 memset (&it->position, 0, sizeof it->position);
20056 saved_avoid_cursor = it->avoid_cursor_p;
20057 it->avoid_cursor_p = true;
20058 saved_face_id = it->face_id;
20059 saved_box_start = it->start_of_box_run_p;
20060 /* The last row's stretch glyph should get the default
20061 face, to avoid painting the rest of the window with
20062 the region face, if the region ends at ZV. */
20063 if (it->glyph_row->ends_at_zv_p)
20064 it->face_id = default_face->id;
20065 else
20066 it->face_id = face->id;
20067 it->start_of_box_run_p = false;
20068 append_stretch_glyph (it, Qnil, stretch_width,
20069 it->ascent + it->descent, stretch_ascent);
20070 it->position = saved_pos;
20071 it->avoid_cursor_p = saved_avoid_cursor;
20072 it->face_id = saved_face_id;
20073 it->start_of_box_run_p = saved_box_start;
20075 /* If stretch_width comes out negative, it means that the
20076 last glyph is only partially visible. In R2L rows, we
20077 want the leftmost glyph to be partially visible, so we
20078 need to give the row the corresponding left offset. */
20079 if (stretch_width < 0)
20080 it->glyph_row->x = stretch_width;
20082 #endif /* HAVE_WINDOW_SYSTEM */
20084 else
20086 /* Save some values that must not be changed. */
20087 int saved_x = it->current_x;
20088 struct text_pos saved_pos;
20089 Lisp_Object saved_object;
20090 enum display_element_type saved_what = it->what;
20091 int saved_face_id = it->face_id;
20093 saved_object = it->object;
20094 saved_pos = it->position;
20096 it->what = IT_CHARACTER;
20097 memset (&it->position, 0, sizeof it->position);
20098 it->object = Qnil;
20099 it->c = it->char_to_display = ' ';
20100 it->len = 1;
20102 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20103 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20104 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20105 && !it->glyph_row->mode_line_p
20106 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20108 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20109 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20111 for (it->current_x = 0; g < e; g++)
20112 it->current_x += g->pixel_width;
20114 it->area = LEFT_MARGIN_AREA;
20115 it->face_id = default_face->id;
20116 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20117 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20119 PRODUCE_GLYPHS (it);
20120 /* term.c:produce_glyphs advances it->current_x only for
20121 TEXT_AREA. */
20122 it->current_x += it->pixel_width;
20125 it->current_x = saved_x;
20126 it->area = TEXT_AREA;
20129 /* The last row's blank glyphs should get the default face, to
20130 avoid painting the rest of the window with the region face,
20131 if the region ends at ZV. */
20132 if (it->glyph_row->ends_at_zv_p)
20133 it->face_id = default_face->id;
20134 else
20135 it->face_id = face->id;
20136 PRODUCE_GLYPHS (it);
20138 while (it->current_x <= it->last_visible_x)
20139 PRODUCE_GLYPHS (it);
20141 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20142 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20143 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20144 && !it->glyph_row->mode_line_p
20145 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20147 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20148 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20150 for ( ; g < e; g++)
20151 it->current_x += g->pixel_width;
20153 it->area = RIGHT_MARGIN_AREA;
20154 it->face_id = default_face->id;
20155 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20156 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20158 PRODUCE_GLYPHS (it);
20159 it->current_x += it->pixel_width;
20162 it->area = TEXT_AREA;
20165 /* Don't count these blanks really. It would let us insert a left
20166 truncation glyph below and make us set the cursor on them, maybe. */
20167 it->current_x = saved_x;
20168 it->object = saved_object;
20169 it->position = saved_pos;
20170 it->what = saved_what;
20171 it->face_id = saved_face_id;
20176 /* Value is true if text starting at CHARPOS in current_buffer is
20177 trailing whitespace. */
20179 static bool
20180 trailing_whitespace_p (ptrdiff_t charpos)
20182 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20183 int c = 0;
20185 while (bytepos < ZV_BYTE
20186 && (c = FETCH_CHAR (bytepos),
20187 c == ' ' || c == '\t'))
20188 ++bytepos;
20190 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20192 if (bytepos != PT_BYTE)
20193 return true;
20195 return false;
20199 /* Highlight trailing whitespace, if any, in ROW. */
20201 static void
20202 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20204 int used = row->used[TEXT_AREA];
20206 if (used)
20208 struct glyph *start = row->glyphs[TEXT_AREA];
20209 struct glyph *glyph = start + used - 1;
20211 if (row->reversed_p)
20213 /* Right-to-left rows need to be processed in the opposite
20214 direction, so swap the edge pointers. */
20215 glyph = start;
20216 start = row->glyphs[TEXT_AREA] + used - 1;
20219 /* Skip over glyphs inserted to display the cursor at the
20220 end of a line, for extending the face of the last glyph
20221 to the end of the line on terminals, and for truncation
20222 and continuation glyphs. */
20223 if (!row->reversed_p)
20225 while (glyph >= start
20226 && glyph->type == CHAR_GLYPH
20227 && NILP (glyph->object))
20228 --glyph;
20230 else
20232 while (glyph <= start
20233 && glyph->type == CHAR_GLYPH
20234 && NILP (glyph->object))
20235 ++glyph;
20238 /* If last glyph is a space or stretch, and it's trailing
20239 whitespace, set the face of all trailing whitespace glyphs in
20240 IT->glyph_row to `trailing-whitespace'. */
20241 if ((row->reversed_p ? glyph <= start : glyph >= start)
20242 && BUFFERP (glyph->object)
20243 && (glyph->type == STRETCH_GLYPH
20244 || (glyph->type == CHAR_GLYPH
20245 && glyph->u.ch == ' '))
20246 && trailing_whitespace_p (glyph->charpos))
20248 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20249 if (face_id < 0)
20250 return;
20252 if (!row->reversed_p)
20254 while (glyph >= start
20255 && BUFFERP (glyph->object)
20256 && (glyph->type == STRETCH_GLYPH
20257 || (glyph->type == CHAR_GLYPH
20258 && glyph->u.ch == ' ')))
20259 (glyph--)->face_id = face_id;
20261 else
20263 while (glyph <= start
20264 && BUFFERP (glyph->object)
20265 && (glyph->type == STRETCH_GLYPH
20266 || (glyph->type == CHAR_GLYPH
20267 && glyph->u.ch == ' ')))
20268 (glyph++)->face_id = face_id;
20275 /* Value is true if glyph row ROW should be
20276 considered to hold the buffer position CHARPOS. */
20278 static bool
20279 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20281 bool result = true;
20283 if (charpos == CHARPOS (row->end.pos)
20284 || charpos == MATRIX_ROW_END_CHARPOS (row))
20286 /* Suppose the row ends on a string.
20287 Unless the row is continued, that means it ends on a newline
20288 in the string. If it's anything other than a display string
20289 (e.g., a before-string from an overlay), we don't want the
20290 cursor there. (This heuristic seems to give the optimal
20291 behavior for the various types of multi-line strings.)
20292 One exception: if the string has `cursor' property on one of
20293 its characters, we _do_ want the cursor there. */
20294 if (CHARPOS (row->end.string_pos) >= 0)
20296 if (row->continued_p)
20297 result = true;
20298 else
20300 /* Check for `display' property. */
20301 struct glyph *beg = row->glyphs[TEXT_AREA];
20302 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20303 struct glyph *glyph;
20305 result = false;
20306 for (glyph = end; glyph >= beg; --glyph)
20307 if (STRINGP (glyph->object))
20309 Lisp_Object prop
20310 = Fget_char_property (make_number (charpos),
20311 Qdisplay, Qnil);
20312 result =
20313 (!NILP (prop)
20314 && display_prop_string_p (prop, glyph->object));
20315 /* If there's a `cursor' property on one of the
20316 string's characters, this row is a cursor row,
20317 even though this is not a display string. */
20318 if (!result)
20320 Lisp_Object s = glyph->object;
20322 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20324 ptrdiff_t gpos = glyph->charpos;
20326 if (!NILP (Fget_char_property (make_number (gpos),
20327 Qcursor, s)))
20329 result = true;
20330 break;
20334 break;
20338 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20340 /* If the row ends in middle of a real character,
20341 and the line is continued, we want the cursor here.
20342 That's because CHARPOS (ROW->end.pos) would equal
20343 PT if PT is before the character. */
20344 if (!row->ends_in_ellipsis_p)
20345 result = row->continued_p;
20346 else
20347 /* If the row ends in an ellipsis, then
20348 CHARPOS (ROW->end.pos) will equal point after the
20349 invisible text. We want that position to be displayed
20350 after the ellipsis. */
20351 result = false;
20353 /* If the row ends at ZV, display the cursor at the end of that
20354 row instead of at the start of the row below. */
20355 else
20356 result = row->ends_at_zv_p;
20359 return result;
20362 /* Value is true if glyph row ROW should be
20363 used to hold the cursor. */
20365 static bool
20366 cursor_row_p (struct glyph_row *row)
20368 return row_for_charpos_p (row, PT);
20373 /* Push the property PROP so that it will be rendered at the current
20374 position in IT. Return true if PROP was successfully pushed, false
20375 otherwise. Called from handle_line_prefix to handle the
20376 `line-prefix' and `wrap-prefix' properties. */
20378 static bool
20379 push_prefix_prop (struct it *it, Lisp_Object prop)
20381 struct text_pos pos =
20382 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20384 eassert (it->method == GET_FROM_BUFFER
20385 || it->method == GET_FROM_DISPLAY_VECTOR
20386 || it->method == GET_FROM_STRING
20387 || it->method == GET_FROM_IMAGE);
20389 /* We need to save the current buffer/string position, so it will be
20390 restored by pop_it, because iterate_out_of_display_property
20391 depends on that being set correctly, but some situations leave
20392 it->position not yet set when this function is called. */
20393 push_it (it, &pos);
20395 if (STRINGP (prop))
20397 if (SCHARS (prop) == 0)
20399 pop_it (it);
20400 return false;
20403 it->string = prop;
20404 it->string_from_prefix_prop_p = true;
20405 it->multibyte_p = STRING_MULTIBYTE (it->string);
20406 it->current.overlay_string_index = -1;
20407 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20408 it->end_charpos = it->string_nchars = SCHARS (it->string);
20409 it->method = GET_FROM_STRING;
20410 it->stop_charpos = 0;
20411 it->prev_stop = 0;
20412 it->base_level_stop = 0;
20414 /* Force paragraph direction to be that of the parent
20415 buffer/string. */
20416 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20417 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20418 else
20419 it->paragraph_embedding = L2R;
20421 /* Set up the bidi iterator for this display string. */
20422 if (it->bidi_p)
20424 it->bidi_it.string.lstring = it->string;
20425 it->bidi_it.string.s = NULL;
20426 it->bidi_it.string.schars = it->end_charpos;
20427 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20428 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20429 it->bidi_it.string.unibyte = !it->multibyte_p;
20430 it->bidi_it.w = it->w;
20431 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20434 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20436 it->method = GET_FROM_STRETCH;
20437 it->object = prop;
20439 #ifdef HAVE_WINDOW_SYSTEM
20440 else if (IMAGEP (prop))
20442 it->what = IT_IMAGE;
20443 it->image_id = lookup_image (it->f, prop);
20444 it->method = GET_FROM_IMAGE;
20446 #endif /* HAVE_WINDOW_SYSTEM */
20447 else
20449 pop_it (it); /* bogus display property, give up */
20450 return false;
20453 return true;
20456 /* Return the character-property PROP at the current position in IT. */
20458 static Lisp_Object
20459 get_it_property (struct it *it, Lisp_Object prop)
20461 Lisp_Object position, object = it->object;
20463 if (STRINGP (object))
20464 position = make_number (IT_STRING_CHARPOS (*it));
20465 else if (BUFFERP (object))
20467 position = make_number (IT_CHARPOS (*it));
20468 object = it->window;
20470 else
20471 return Qnil;
20473 return Fget_char_property (position, prop, object);
20476 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20478 static void
20479 handle_line_prefix (struct it *it)
20481 Lisp_Object prefix;
20483 if (it->continuation_lines_width > 0)
20485 prefix = get_it_property (it, Qwrap_prefix);
20486 if (NILP (prefix))
20487 prefix = Vwrap_prefix;
20489 else
20491 prefix = get_it_property (it, Qline_prefix);
20492 if (NILP (prefix))
20493 prefix = Vline_prefix;
20495 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20497 /* If the prefix is wider than the window, and we try to wrap
20498 it, it would acquire its own wrap prefix, and so on till the
20499 iterator stack overflows. So, don't wrap the prefix. */
20500 it->line_wrap = TRUNCATE;
20501 it->avoid_cursor_p = true;
20507 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20508 only for R2L lines from display_line and display_string, when they
20509 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20510 the line/string needs to be continued on the next glyph row. */
20511 static void
20512 unproduce_glyphs (struct it *it, int n)
20514 struct glyph *glyph, *end;
20516 eassert (it->glyph_row);
20517 eassert (it->glyph_row->reversed_p);
20518 eassert (it->area == TEXT_AREA);
20519 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20521 if (n > it->glyph_row->used[TEXT_AREA])
20522 n = it->glyph_row->used[TEXT_AREA];
20523 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20524 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20525 for ( ; glyph < end; glyph++)
20526 glyph[-n] = *glyph;
20529 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20530 and ROW->maxpos. */
20531 static void
20532 find_row_edges (struct it *it, struct glyph_row *row,
20533 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20534 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20536 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20537 lines' rows is implemented for bidi-reordered rows. */
20539 /* ROW->minpos is the value of min_pos, the minimal buffer position
20540 we have in ROW, or ROW->start.pos if that is smaller. */
20541 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20542 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20543 else
20544 /* We didn't find buffer positions smaller than ROW->start, or
20545 didn't find _any_ valid buffer positions in any of the glyphs,
20546 so we must trust the iterator's computed positions. */
20547 row->minpos = row->start.pos;
20548 if (max_pos <= 0)
20550 max_pos = CHARPOS (it->current.pos);
20551 max_bpos = BYTEPOS (it->current.pos);
20554 /* Here are the various use-cases for ending the row, and the
20555 corresponding values for ROW->maxpos:
20557 Line ends in a newline from buffer eol_pos + 1
20558 Line is continued from buffer max_pos + 1
20559 Line is truncated on right it->current.pos
20560 Line ends in a newline from string max_pos + 1(*)
20561 (*) + 1 only when line ends in a forward scan
20562 Line is continued from string max_pos
20563 Line is continued from display vector max_pos
20564 Line is entirely from a string min_pos == max_pos
20565 Line is entirely from a display vector min_pos == max_pos
20566 Line that ends at ZV ZV
20568 If you discover other use-cases, please add them here as
20569 appropriate. */
20570 if (row->ends_at_zv_p)
20571 row->maxpos = it->current.pos;
20572 else if (row->used[TEXT_AREA])
20574 bool seen_this_string = false;
20575 struct glyph_row *r1 = row - 1;
20577 /* Did we see the same display string on the previous row? */
20578 if (STRINGP (it->object)
20579 /* this is not the first row */
20580 && row > it->w->desired_matrix->rows
20581 /* previous row is not the header line */
20582 && !r1->mode_line_p
20583 /* previous row also ends in a newline from a string */
20584 && r1->ends_in_newline_from_string_p)
20586 struct glyph *start, *end;
20588 /* Search for the last glyph of the previous row that came
20589 from buffer or string. Depending on whether the row is
20590 L2R or R2L, we need to process it front to back or the
20591 other way round. */
20592 if (!r1->reversed_p)
20594 start = r1->glyphs[TEXT_AREA];
20595 end = start + r1->used[TEXT_AREA];
20596 /* Glyphs inserted by redisplay have nil as their object. */
20597 while (end > start
20598 && NILP ((end - 1)->object)
20599 && (end - 1)->charpos <= 0)
20600 --end;
20601 if (end > start)
20603 if (EQ ((end - 1)->object, it->object))
20604 seen_this_string = true;
20606 else
20607 /* If all the glyphs of the previous row were inserted
20608 by redisplay, it means the previous row was
20609 produced from a single newline, which is only
20610 possible if that newline came from the same string
20611 as the one which produced this ROW. */
20612 seen_this_string = true;
20614 else
20616 end = r1->glyphs[TEXT_AREA] - 1;
20617 start = end + r1->used[TEXT_AREA];
20618 while (end < start
20619 && NILP ((end + 1)->object)
20620 && (end + 1)->charpos <= 0)
20621 ++end;
20622 if (end < start)
20624 if (EQ ((end + 1)->object, it->object))
20625 seen_this_string = true;
20627 else
20628 seen_this_string = true;
20631 /* Take note of each display string that covers a newline only
20632 once, the first time we see it. This is for when a display
20633 string includes more than one newline in it. */
20634 if (row->ends_in_newline_from_string_p && !seen_this_string)
20636 /* If we were scanning the buffer forward when we displayed
20637 the string, we want to account for at least one buffer
20638 position that belongs to this row (position covered by
20639 the display string), so that cursor positioning will
20640 consider this row as a candidate when point is at the end
20641 of the visual line represented by this row. This is not
20642 required when scanning back, because max_pos will already
20643 have a much larger value. */
20644 if (CHARPOS (row->end.pos) > max_pos)
20645 INC_BOTH (max_pos, max_bpos);
20646 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20648 else if (CHARPOS (it->eol_pos) > 0)
20649 SET_TEXT_POS (row->maxpos,
20650 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20651 else if (row->continued_p)
20653 /* If max_pos is different from IT's current position, it
20654 means IT->method does not belong to the display element
20655 at max_pos. However, it also means that the display
20656 element at max_pos was displayed in its entirety on this
20657 line, which is equivalent to saying that the next line
20658 starts at the next buffer position. */
20659 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20660 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20661 else
20663 INC_BOTH (max_pos, max_bpos);
20664 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20667 else if (row->truncated_on_right_p)
20668 /* display_line already called reseat_at_next_visible_line_start,
20669 which puts the iterator at the beginning of the next line, in
20670 the logical order. */
20671 row->maxpos = it->current.pos;
20672 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20673 /* A line that is entirely from a string/image/stretch... */
20674 row->maxpos = row->minpos;
20675 else
20676 emacs_abort ();
20678 else
20679 row->maxpos = it->current.pos;
20682 static void
20683 maybe_produce_line_number (struct it *it)
20685 ptrdiff_t last_line = it->lnum;
20686 ptrdiff_t start_from, bytepos;
20688 /* FIXME: Maybe reuse the data in it->w->base_line_number. */
20689 if (!last_line)
20690 start_from = BEGV;
20691 else
20692 start_from = it->lnum_bytepos;
20694 /* Paranoia: what if someone changes the narrowing since the last
20695 time display_line was called? Shouldn't really happen, but who
20696 knows what some crazy Lisp invoked by :eval could do? */
20697 if (!(BEGV_BYTE <= start_from && start_from < ZV_BYTE))
20699 last_line = 0;
20700 start_from = BEGV_BYTE;
20703 ptrdiff_t this_line;
20705 this_line =
20706 last_line + display_count_lines (start_from,
20707 IT_BYTEPOS (*it), IT_CHARPOS (*it),
20708 &bytepos);
20709 eassert (this_line > 0 || (this_line == 0 && start_from == BEGV_BYTE));
20710 eassert (bytepos == IT_BYTEPOS (*it));
20712 /* If this is a new logical line, produce the glyphs for the line
20713 number. */
20714 if (this_line != last_line || !last_line || it->continuation_lines_width > 0)
20716 if (this_line != last_line || !last_line)
20718 it->lnum = this_line;
20719 it->lnum_bytepos = IT_BYTEPOS (*it);
20722 void *itdata = bidi_shelve_cache ();
20723 struct it tem_it;
20724 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20725 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20726 /* Compute the required width if needed. */
20727 if (!it->lnum_width)
20729 /* Max line number to be displayed cannot be more than the
20730 one corresponding to the last row of the desired
20731 matrix. */
20732 ptrdiff_t max_lnum =
20733 this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20734 it->lnum_width = log10 (max_lnum) + 1;
20735 eassert (it->lnum_width > 0);
20737 pint2str (lnum_buf, it->lnum_width, this_line + 1);
20738 /* Append a blank. */
20739 strcat (lnum_buf, " ");
20741 /* Setup for producing the glyphs. */
20742 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
20743 /* FIXME: Use specialized face. */
20744 DEFAULT_FACE_ID);
20745 scratch_glyph_row.reversed_p = false;
20746 scratch_glyph_row.used[TEXT_AREA] = 0;
20747 SET_TEXT_POS (tem_it.position, 0, 0);
20748 tem_it.bidi_it.type = WEAK_EN;
20749 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
20750 1 level in R2L paragraphs. Emulate that. */
20751 tem_it.bidi_it.resolved_level = 2;
20752 if (it->glyph_row && it->glyph_row->reversed_p)
20753 tem_it.bidi_it.resolved_level = 1;
20755 /* Produce glyphs for the line number in a scratch glyph_row. */
20756 int n_glyphs_before;
20757 for (const char *p = lnum_buf; *p; p++)
20759 /* For continuation lines and lines after ZV, instead of a
20760 line number, produce a blank prefix of the same width. */
20761 if (beyond_zv || it->continuation_lines_width > 0)
20762 tem_it.c = tem_it.char_to_display = ' ';
20763 else
20764 tem_it.c = tem_it.char_to_display = *p;
20765 tem_it.len = 1;
20766 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
20767 /* Make sure these glyphs will have a "position" of -1. */
20768 SET_TEXT_POS (tem_it.position, -1, -1);
20769 PRODUCE_GLYPHS (&tem_it);
20771 /* Stop producing glyphs if we don't have enough space on
20772 this line. FIXME: should we refrain from producing the
20773 line number at all in that case? */
20774 if (tem_it.current_x > tem_it.last_visible_x)
20776 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
20777 break;
20781 /* Copy the produced glyphs into IT's glyph_row. */
20782 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
20783 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
20784 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
20785 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
20787 while (g < e)
20789 it->current_x += g->pixel_width;
20790 it->hpos++;
20791 if (p)
20793 *p++ = *g++;
20794 (*u)++;
20798 /* Update IT->glyph_row's metrics. */
20799 if (it->glyph_row)
20801 struct glyph_row *row = it->glyph_row;
20803 row->ascent = max (row->ascent, tem_it.max_ascent);
20804 row->height = max (row->height,
20805 tem_it.max_ascent + tem_it.max_descent);
20806 row->phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
20807 row->phys_height = max (row->phys_height,
20808 tem_it.max_phys_ascent + tem_it.max_phys_descent);
20809 row->extra_line_spacing = max (row->extra_line_spacing,
20810 tem_it.max_extra_line_spacing);
20813 bidi_unshelve_cache (itdata, false);
20817 /* Construct the glyph row IT->glyph_row in the desired matrix of
20818 IT->w from text at the current position of IT. See dispextern.h
20819 for an overview of struct it. Value is true if
20820 IT->glyph_row displays text, as opposed to a line displaying ZV
20821 only. CURSOR_VPOS is the window-relative vertical position of
20822 the glyph row displaying the cursor, or -1 if unknown. */
20824 static bool
20825 display_line (struct it *it, int cursor_vpos)
20827 struct glyph_row *row = it->glyph_row;
20828 Lisp_Object overlay_arrow_string;
20829 struct it wrap_it;
20830 void *wrap_data = NULL;
20831 bool may_wrap = false;
20832 int wrap_x UNINIT;
20833 int wrap_row_used = -1;
20834 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20835 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20836 int wrap_row_extra_line_spacing UNINIT;
20837 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20838 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20839 int cvpos;
20840 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20841 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20842 bool pending_handle_line_prefix = false;
20843 int header_line = WINDOW_WANTS_HEADER_LINE_P (it->w);
20844 bool hscroll_this_line = (cursor_vpos >= 0
20845 && it->vpos == cursor_vpos - header_line
20846 && hscrolling_current_line_p (it->w));
20847 int first_visible_x = it->first_visible_x;
20848 int last_visible_x = it->last_visible_x;
20849 int x_incr = 0;
20851 /* We always start displaying at hpos zero even if hscrolled. */
20852 eassert (it->hpos == 0 && it->current_x == 0);
20854 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20855 >= it->w->desired_matrix->nrows)
20857 it->w->nrows_scale_factor++;
20858 it->f->fonts_changed = true;
20859 return false;
20862 /* Clear the result glyph row and enable it. */
20863 prepare_desired_row (it->w, row, false);
20865 row->y = it->current_y;
20866 row->start = it->start;
20867 row->continuation_lines_width = it->continuation_lines_width;
20868 row->displays_text_p = true;
20869 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20870 it->starts_in_middle_of_char_p = false;
20872 /* Arrange the overlays nicely for our purposes. Usually, we call
20873 display_line on only one line at a time, in which case this
20874 can't really hurt too much, or we call it on lines which appear
20875 one after another in the buffer, in which case all calls to
20876 recenter_overlay_lists but the first will be pretty cheap. */
20877 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20879 /* If we are going to display the cursor's line, account for the
20880 hscroll of that line. We subtract the window's min_hscroll,
20881 because that was already accounted for in init_iterator. */
20882 if (hscroll_this_line)
20883 x_incr =
20884 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
20885 * FRAME_COLUMN_WIDTH (it->f);
20887 /* Move over display elements that are not visible because we are
20888 hscrolled. This may stop at an x-position < first_visible_x
20889 if the first glyph is partially visible or if we hit a line end. */
20890 if (it->current_x < it->first_visible_x + x_incr)
20892 enum move_it_result move_result;
20894 this_line_min_pos = row->start.pos;
20895 if (hscroll_this_line)
20897 it->first_visible_x += x_incr;
20898 it->last_visible_x += x_incr;
20900 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20901 MOVE_TO_POS | MOVE_TO_X);
20902 /* If we are under a large hscroll, move_it_in_display_line_to
20903 could hit the end of the line without reaching
20904 first_visible_x. Pretend that we did reach it. This is
20905 especially important on a TTY, where we will call
20906 extend_face_to_end_of_line, which needs to know how many
20907 blank glyphs to produce. */
20908 if (it->current_x < it->first_visible_x
20909 && (move_result == MOVE_NEWLINE_OR_CR
20910 || move_result == MOVE_POS_MATCH_OR_ZV))
20911 it->current_x = it->first_visible_x;
20913 /* Record the smallest positions seen while we moved over
20914 display elements that are not visible. This is needed by
20915 redisplay_internal for optimizing the case where the cursor
20916 stays inside the same line. The rest of this function only
20917 considers positions that are actually displayed, so
20918 RECORD_MAX_MIN_POS will not otherwise record positions that
20919 are hscrolled to the left of the left edge of the window. */
20920 min_pos = CHARPOS (this_line_min_pos);
20921 min_bpos = BYTEPOS (this_line_min_pos);
20923 /* Produce line number, if needed. */
20924 if (!NILP (Vdisplay_line_numbers))
20925 maybe_produce_line_number (it);
20927 else if (it->area == TEXT_AREA)
20929 /* Line numbers should precede the line-prefix or wrap-prefix. */
20930 if (!NILP (Vdisplay_line_numbers))
20931 maybe_produce_line_number (it);
20933 /* We only do this when not calling move_it_in_display_line_to
20934 above, because that function calls itself handle_line_prefix. */
20935 handle_line_prefix (it);
20937 else
20939 /* Line-prefix and wrap-prefix are always displayed in the text
20940 area. But if this is the first call to display_line after
20941 init_iterator, the iterator might have been set up to write
20942 into a marginal area, e.g. if the line begins with some
20943 display property that writes to the margins. So we need to
20944 wait with the call to handle_line_prefix until whatever
20945 writes to the margin has done its job. */
20946 pending_handle_line_prefix = true;
20949 /* Get the initial row height. This is either the height of the
20950 text hscrolled, if there is any, or zero. */
20951 row->ascent = it->max_ascent;
20952 row->height = it->max_ascent + it->max_descent;
20953 row->phys_ascent = it->max_phys_ascent;
20954 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20955 row->extra_line_spacing = it->max_extra_line_spacing;
20957 /* Utility macro to record max and min buffer positions seen until now. */
20958 #define RECORD_MAX_MIN_POS(IT) \
20959 do \
20961 bool composition_p \
20962 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20963 ptrdiff_t current_pos = \
20964 composition_p ? (IT)->cmp_it.charpos \
20965 : IT_CHARPOS (*(IT)); \
20966 ptrdiff_t current_bpos = \
20967 composition_p ? CHAR_TO_BYTE (current_pos) \
20968 : IT_BYTEPOS (*(IT)); \
20969 if (current_pos < min_pos) \
20971 min_pos = current_pos; \
20972 min_bpos = current_bpos; \
20974 if (IT_CHARPOS (*it) > max_pos) \
20976 max_pos = IT_CHARPOS (*it); \
20977 max_bpos = IT_BYTEPOS (*it); \
20980 while (false)
20982 /* Loop generating characters. The loop is left with IT on the next
20983 character to display. */
20984 while (true)
20986 int n_glyphs_before, hpos_before, x_before;
20987 int x, nglyphs;
20988 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20990 /* Retrieve the next thing to display. Value is false if end of
20991 buffer reached. */
20992 if (!get_next_display_element (it))
20994 /* Maybe add a space at the end of this line that is used to
20995 display the cursor there under X. Set the charpos of the
20996 first glyph of blank lines not corresponding to any text
20997 to -1. */
20998 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20999 row->exact_window_width_line_p = true;
21000 else if ((append_space_for_newline (it, true)
21001 && row->used[TEXT_AREA] == 1)
21002 || row->used[TEXT_AREA] == 0)
21004 row->glyphs[TEXT_AREA]->charpos = -1;
21005 row->displays_text_p = false;
21007 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21008 && (!MINI_WINDOW_P (it->w)
21009 || (minibuf_level && EQ (it->window, minibuf_window))))
21010 row->indicate_empty_line_p = true;
21013 it->continuation_lines_width = 0;
21014 /* Reset those iterator values set from display property
21015 values. This is for the case when the display property
21016 ends at ZV, and is not a replacing property, so pop_it is
21017 not called. */
21018 it->font_height = Qnil;
21019 it->voffset = 0;
21020 row->ends_at_zv_p = true;
21021 /* A row that displays right-to-left text must always have
21022 its last face extended all the way to the end of line,
21023 even if this row ends in ZV, because we still write to
21024 the screen left to right. We also need to extend the
21025 last face if the default face is remapped to some
21026 different face, otherwise the functions that clear
21027 portions of the screen will clear with the default face's
21028 background color. */
21029 if (row->reversed_p
21030 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21031 extend_face_to_end_of_line (it);
21032 break;
21035 /* Now, get the metrics of what we want to display. This also
21036 generates glyphs in `row' (which is IT->glyph_row). */
21037 n_glyphs_before = row->used[TEXT_AREA];
21038 x = it->current_x;
21040 /* Remember the line height so far in case the next element doesn't
21041 fit on the line. */
21042 if (it->line_wrap != TRUNCATE)
21044 ascent = it->max_ascent;
21045 descent = it->max_descent;
21046 phys_ascent = it->max_phys_ascent;
21047 phys_descent = it->max_phys_descent;
21049 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21051 if (IT_DISPLAYING_WHITESPACE (it))
21052 may_wrap = true;
21053 else if (may_wrap)
21055 SAVE_IT (wrap_it, *it, wrap_data);
21056 wrap_x = x;
21057 wrap_row_used = row->used[TEXT_AREA];
21058 wrap_row_ascent = row->ascent;
21059 wrap_row_height = row->height;
21060 wrap_row_phys_ascent = row->phys_ascent;
21061 wrap_row_phys_height = row->phys_height;
21062 wrap_row_extra_line_spacing = row->extra_line_spacing;
21063 wrap_row_min_pos = min_pos;
21064 wrap_row_min_bpos = min_bpos;
21065 wrap_row_max_pos = max_pos;
21066 wrap_row_max_bpos = max_bpos;
21067 may_wrap = false;
21072 PRODUCE_GLYPHS (it);
21074 /* If this display element was in marginal areas, continue with
21075 the next one. */
21076 if (it->area != TEXT_AREA)
21078 row->ascent = max (row->ascent, it->max_ascent);
21079 row->height = max (row->height, it->max_ascent + it->max_descent);
21080 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21081 row->phys_height = max (row->phys_height,
21082 it->max_phys_ascent + it->max_phys_descent);
21083 row->extra_line_spacing = max (row->extra_line_spacing,
21084 it->max_extra_line_spacing);
21085 set_iterator_to_next (it, true);
21086 /* If we didn't handle the line/wrap prefix above, and the
21087 call to set_iterator_to_next just switched to TEXT_AREA,
21088 process the prefix now. */
21089 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21091 /* Line numbers should precede the line-prefix or wrap-prefix. */
21092 if (!NILP (Vdisplay_line_numbers))
21093 maybe_produce_line_number (it);
21095 pending_handle_line_prefix = false;
21096 handle_line_prefix (it);
21098 continue;
21101 /* Does the display element fit on the line? If we truncate
21102 lines, we should draw past the right edge of the window. If
21103 we don't truncate, we want to stop so that we can display the
21104 continuation glyph before the right margin. If lines are
21105 continued, there are two possible strategies for characters
21106 resulting in more than 1 glyph (e.g. tabs): Display as many
21107 glyphs as possible in this line and leave the rest for the
21108 continuation line, or display the whole element in the next
21109 line. Original redisplay did the former, so we do it also. */
21110 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21111 hpos_before = it->hpos;
21112 x_before = x;
21114 if (/* Not a newline. */
21115 nglyphs > 0
21116 /* Glyphs produced fit entirely in the line. */
21117 && it->current_x < it->last_visible_x)
21119 it->hpos += nglyphs;
21120 row->ascent = max (row->ascent, it->max_ascent);
21121 row->height = max (row->height, it->max_ascent + it->max_descent);
21122 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21123 row->phys_height = max (row->phys_height,
21124 it->max_phys_ascent + it->max_phys_descent);
21125 row->extra_line_spacing = max (row->extra_line_spacing,
21126 it->max_extra_line_spacing);
21127 if (it->current_x - it->pixel_width < it->first_visible_x
21128 /* In R2L rows, we arrange in extend_face_to_end_of_line
21129 to add a right offset to the line, by a suitable
21130 change to the stretch glyph that is the leftmost
21131 glyph of the line. */
21132 && !row->reversed_p)
21133 row->x = x - it->first_visible_x;
21134 /* Record the maximum and minimum buffer positions seen so
21135 far in glyphs that will be displayed by this row. */
21136 if (it->bidi_p)
21137 RECORD_MAX_MIN_POS (it);
21139 else
21141 int i, new_x;
21142 struct glyph *glyph;
21144 for (i = 0; i < nglyphs; ++i, x = new_x)
21146 /* Identify the glyphs added by the last call to
21147 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21148 the previous glyphs. */
21149 if (!row->reversed_p)
21150 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21151 else
21152 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21153 new_x = x + glyph->pixel_width;
21155 if (/* Lines are continued. */
21156 it->line_wrap != TRUNCATE
21157 && (/* Glyph doesn't fit on the line. */
21158 new_x > it->last_visible_x
21159 /* Or it fits exactly on a window system frame. */
21160 || (new_x == it->last_visible_x
21161 && FRAME_WINDOW_P (it->f)
21162 && (row->reversed_p
21163 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21164 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21166 /* End of a continued line. */
21168 if (it->hpos == 0
21169 || (new_x == it->last_visible_x
21170 && FRAME_WINDOW_P (it->f)
21171 && (row->reversed_p
21172 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21173 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21175 /* Current glyph is the only one on the line or
21176 fits exactly on the line. We must continue
21177 the line because we can't draw the cursor
21178 after the glyph. */
21179 row->continued_p = true;
21180 it->current_x = new_x;
21181 it->continuation_lines_width += new_x;
21182 ++it->hpos;
21183 if (i == nglyphs - 1)
21185 /* If line-wrap is on, check if a previous
21186 wrap point was found. */
21187 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21188 && wrap_row_used > 0
21189 /* Even if there is a previous wrap
21190 point, continue the line here as
21191 usual, if (i) the previous character
21192 was a space or tab AND (ii) the
21193 current character is not. */
21194 && (!may_wrap
21195 || IT_DISPLAYING_WHITESPACE (it)))
21196 goto back_to_wrap;
21198 /* Record the maximum and minimum buffer
21199 positions seen so far in glyphs that will be
21200 displayed by this row. */
21201 if (it->bidi_p)
21202 RECORD_MAX_MIN_POS (it);
21203 set_iterator_to_next (it, true);
21204 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21206 if (!get_next_display_element (it))
21208 row->exact_window_width_line_p = true;
21209 it->continuation_lines_width = 0;
21210 it->font_height = Qnil;
21211 it->voffset = 0;
21212 row->continued_p = false;
21213 row->ends_at_zv_p = true;
21215 else if (ITERATOR_AT_END_OF_LINE_P (it))
21217 row->continued_p = false;
21218 row->exact_window_width_line_p = true;
21220 /* If line-wrap is on, check if a
21221 previous wrap point was found. */
21222 else if (wrap_row_used > 0
21223 /* Even if there is a previous wrap
21224 point, continue the line here as
21225 usual, if (i) the previous character
21226 was a space or tab AND (ii) the
21227 current character is not. */
21228 && (!may_wrap
21229 || IT_DISPLAYING_WHITESPACE (it)))
21230 goto back_to_wrap;
21234 else if (it->bidi_p)
21235 RECORD_MAX_MIN_POS (it);
21236 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21237 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21238 extend_face_to_end_of_line (it);
21240 else if (CHAR_GLYPH_PADDING_P (*glyph)
21241 && !FRAME_WINDOW_P (it->f))
21243 /* A padding glyph that doesn't fit on this line.
21244 This means the whole character doesn't fit
21245 on the line. */
21246 if (row->reversed_p)
21247 unproduce_glyphs (it, row->used[TEXT_AREA]
21248 - n_glyphs_before);
21249 row->used[TEXT_AREA] = n_glyphs_before;
21251 /* Fill the rest of the row with continuation
21252 glyphs like in 20.x. */
21253 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21254 < row->glyphs[1 + TEXT_AREA])
21255 produce_special_glyphs (it, IT_CONTINUATION);
21257 row->continued_p = true;
21258 it->current_x = x_before;
21259 it->continuation_lines_width += x_before;
21261 /* Restore the height to what it was before the
21262 element not fitting on the line. */
21263 it->max_ascent = ascent;
21264 it->max_descent = descent;
21265 it->max_phys_ascent = phys_ascent;
21266 it->max_phys_descent = phys_descent;
21267 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21268 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21269 extend_face_to_end_of_line (it);
21271 else if (wrap_row_used > 0)
21273 back_to_wrap:
21274 if (row->reversed_p)
21275 unproduce_glyphs (it,
21276 row->used[TEXT_AREA] - wrap_row_used);
21277 RESTORE_IT (it, &wrap_it, wrap_data);
21278 it->continuation_lines_width += wrap_x;
21279 row->used[TEXT_AREA] = wrap_row_used;
21280 row->ascent = wrap_row_ascent;
21281 row->height = wrap_row_height;
21282 row->phys_ascent = wrap_row_phys_ascent;
21283 row->phys_height = wrap_row_phys_height;
21284 row->extra_line_spacing = wrap_row_extra_line_spacing;
21285 min_pos = wrap_row_min_pos;
21286 min_bpos = wrap_row_min_bpos;
21287 max_pos = wrap_row_max_pos;
21288 max_bpos = wrap_row_max_bpos;
21289 row->continued_p = true;
21290 row->ends_at_zv_p = false;
21291 row->exact_window_width_line_p = false;
21292 it->continuation_lines_width += x;
21294 /* Make sure that a non-default face is extended
21295 up to the right margin of the window. */
21296 extend_face_to_end_of_line (it);
21298 else if ((it->what == IT_CHARACTER
21299 || it->what == IT_STRETCH
21300 || it->what == IT_COMPOSITION)
21301 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21303 /* A TAB that extends past the right edge of the
21304 window. This produces a single glyph on
21305 window system frames. We leave the glyph in
21306 this row and let it fill the row, but don't
21307 consume the TAB. */
21308 if ((row->reversed_p
21309 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21310 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21311 produce_special_glyphs (it, IT_CONTINUATION);
21312 it->continuation_lines_width += it->last_visible_x;
21313 row->ends_in_middle_of_char_p = true;
21314 row->continued_p = true;
21315 glyph->pixel_width = it->last_visible_x - x;
21316 it->starts_in_middle_of_char_p = true;
21317 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21318 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21319 extend_face_to_end_of_line (it);
21321 else
21323 /* Something other than a TAB that draws past
21324 the right edge of the window. Restore
21325 positions to values before the element. */
21326 if (row->reversed_p)
21327 unproduce_glyphs (it, row->used[TEXT_AREA]
21328 - (n_glyphs_before + i));
21329 row->used[TEXT_AREA] = n_glyphs_before + i;
21331 /* Display continuation glyphs. */
21332 it->current_x = x_before;
21333 it->continuation_lines_width += x;
21334 if (!FRAME_WINDOW_P (it->f)
21335 || (row->reversed_p
21336 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21337 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21338 produce_special_glyphs (it, IT_CONTINUATION);
21339 row->continued_p = true;
21341 extend_face_to_end_of_line (it);
21343 if (nglyphs > 1 && i > 0)
21345 row->ends_in_middle_of_char_p = true;
21346 it->starts_in_middle_of_char_p = true;
21349 /* Restore the height to what it was before the
21350 element not fitting on the line. */
21351 it->max_ascent = ascent;
21352 it->max_descent = descent;
21353 it->max_phys_ascent = phys_ascent;
21354 it->max_phys_descent = phys_descent;
21357 break;
21359 else if (new_x > it->first_visible_x)
21361 /* Increment number of glyphs actually displayed. */
21362 ++it->hpos;
21364 /* Record the maximum and minimum buffer positions
21365 seen so far in glyphs that will be displayed by
21366 this row. */
21367 if (it->bidi_p)
21368 RECORD_MAX_MIN_POS (it);
21370 if (x < it->first_visible_x && !row->reversed_p)
21371 /* Glyph is partially visible, i.e. row starts at
21372 negative X position. Don't do that in R2L
21373 rows, where we arrange to add a right offset to
21374 the line in extend_face_to_end_of_line, by a
21375 suitable change to the stretch glyph that is
21376 the leftmost glyph of the line. */
21377 row->x = x - it->first_visible_x;
21378 /* When the last glyph of an R2L row only fits
21379 partially on the line, we need to set row->x to a
21380 negative offset, so that the leftmost glyph is
21381 the one that is partially visible. But if we are
21382 going to produce the truncation glyph, this will
21383 be taken care of in produce_special_glyphs. */
21384 if (row->reversed_p
21385 && new_x > it->last_visible_x
21386 && !(it->line_wrap == TRUNCATE
21387 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21389 eassert (FRAME_WINDOW_P (it->f));
21390 row->x = it->last_visible_x - new_x;
21393 else
21395 /* Glyph is completely off the left margin of the
21396 window. This should not happen because of the
21397 move_it_in_display_line at the start of this
21398 function, unless the text display area of the
21399 window is empty. */
21400 eassert (it->first_visible_x <= it->last_visible_x);
21403 /* Even if this display element produced no glyphs at all,
21404 we want to record its position. */
21405 if (it->bidi_p && nglyphs == 0)
21406 RECORD_MAX_MIN_POS (it);
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);
21416 /* End of this display line if row is continued. */
21417 if (row->continued_p || row->ends_at_zv_p)
21418 break;
21421 at_end_of_line:
21422 /* Is this a line end? If yes, we're also done, after making
21423 sure that a non-default face is extended up to the right
21424 margin of the window. */
21425 if (ITERATOR_AT_END_OF_LINE_P (it))
21427 int used_before = row->used[TEXT_AREA];
21429 row->ends_in_newline_from_string_p = STRINGP (it->object);
21431 /* Add a space at the end of the line that is used to
21432 display the cursor there. */
21433 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21434 append_space_for_newline (it, false);
21436 /* Extend the face to the end of the line. */
21437 extend_face_to_end_of_line (it);
21439 /* Make sure we have the position. */
21440 if (used_before == 0)
21441 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21443 /* Record the position of the newline, for use in
21444 find_row_edges. */
21445 it->eol_pos = it->current.pos;
21447 /* Consume the line end. This skips over invisible lines. */
21448 set_iterator_to_next (it, true);
21449 it->continuation_lines_width = 0;
21450 break;
21453 /* Proceed with next display element. Note that this skips
21454 over lines invisible because of selective display. */
21455 set_iterator_to_next (it, true);
21457 /* If we truncate lines, we are done when the last displayed
21458 glyphs reach past the right margin of the window. */
21459 if (it->line_wrap == TRUNCATE
21460 && ((FRAME_WINDOW_P (it->f)
21461 /* Images are preprocessed in produce_image_glyph such
21462 that they are cropped at the right edge of the
21463 window, so an image glyph will always end exactly at
21464 last_visible_x, even if there's no right fringe. */
21465 && ((row->reversed_p
21466 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21467 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21468 || it->what == IT_IMAGE))
21469 ? (it->current_x >= it->last_visible_x)
21470 : (it->current_x > it->last_visible_x)))
21472 /* Maybe add truncation glyphs. */
21473 if (!FRAME_WINDOW_P (it->f)
21474 || (row->reversed_p
21475 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21476 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21478 int i, n;
21480 if (!row->reversed_p)
21482 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21483 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21484 break;
21486 else
21488 for (i = 0; i < row->used[TEXT_AREA]; i++)
21489 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21490 break;
21491 /* Remove any padding glyphs at the front of ROW, to
21492 make room for the truncation glyphs we will be
21493 adding below. The loop below always inserts at
21494 least one truncation glyph, so also remove the
21495 last glyph added to ROW. */
21496 unproduce_glyphs (it, i + 1);
21497 /* Adjust i for the loop below. */
21498 i = row->used[TEXT_AREA] - (i + 1);
21501 /* produce_special_glyphs overwrites the last glyph, so
21502 we don't want that if we want to keep that last
21503 glyph, which means it's an image. */
21504 if (it->current_x > it->last_visible_x)
21506 it->current_x = x_before;
21507 if (!FRAME_WINDOW_P (it->f))
21509 for (n = row->used[TEXT_AREA]; i < n; ++i)
21511 row->used[TEXT_AREA] = i;
21512 produce_special_glyphs (it, IT_TRUNCATION);
21515 else
21517 row->used[TEXT_AREA] = i;
21518 produce_special_glyphs (it, IT_TRUNCATION);
21520 it->hpos = hpos_before;
21523 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21525 /* Don't truncate if we can overflow newline into fringe. */
21526 if (!get_next_display_element (it))
21528 it->continuation_lines_width = 0;
21529 it->font_height = Qnil;
21530 it->voffset = 0;
21531 row->ends_at_zv_p = true;
21532 row->exact_window_width_line_p = true;
21533 break;
21535 if (ITERATOR_AT_END_OF_LINE_P (it))
21537 row->exact_window_width_line_p = true;
21538 goto at_end_of_line;
21540 it->current_x = x_before;
21541 it->hpos = hpos_before;
21544 row->truncated_on_right_p = true;
21545 it->continuation_lines_width = 0;
21546 reseat_at_next_visible_line_start (it, false);
21547 /* We insist below that IT's position be at ZV because in
21548 bidi-reordered lines the character at visible line start
21549 might not be the character that follows the newline in
21550 the logical order. */
21551 if (IT_BYTEPOS (*it) > BEG_BYTE)
21552 row->ends_at_zv_p =
21553 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21554 else
21555 row->ends_at_zv_p = false;
21556 break;
21560 if (wrap_data)
21561 bidi_unshelve_cache (wrap_data, true);
21563 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21564 at the left window margin. */
21565 if (it->first_visible_x
21566 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21568 if (!FRAME_WINDOW_P (it->f)
21569 || (((row->reversed_p
21570 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21571 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21572 /* Don't let insert_left_trunc_glyphs overwrite the
21573 first glyph of the row if it is an image. */
21574 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21575 insert_left_trunc_glyphs (it);
21576 row->truncated_on_left_p = true;
21579 /* Remember the position at which this line ends.
21581 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21582 cannot be before the call to find_row_edges below, since that is
21583 where these positions are determined. */
21584 row->end = it->current;
21585 if (!it->bidi_p)
21587 row->minpos = row->start.pos;
21588 row->maxpos = row->end.pos;
21590 else
21592 /* ROW->minpos and ROW->maxpos must be the smallest and
21593 `1 + the largest' buffer positions in ROW. But if ROW was
21594 bidi-reordered, these two positions can be anywhere in the
21595 row, so we must determine them now. */
21596 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21599 /* If the start of this line is the overlay arrow-position, then
21600 mark this glyph row as the one containing the overlay arrow.
21601 This is clearly a mess with variable size fonts. It would be
21602 better to let it be displayed like cursors under X. */
21603 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21604 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21605 !NILP (overlay_arrow_string)))
21607 /* Overlay arrow in window redisplay is a fringe bitmap. */
21608 if (STRINGP (overlay_arrow_string))
21610 struct glyph_row *arrow_row
21611 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21612 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21613 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21614 struct glyph *p = row->glyphs[TEXT_AREA];
21615 struct glyph *p2, *end;
21617 /* Copy the arrow glyphs. */
21618 while (glyph < arrow_end)
21619 *p++ = *glyph++;
21621 /* Throw away padding glyphs. */
21622 p2 = p;
21623 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21624 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21625 ++p2;
21626 if (p2 > p)
21628 while (p2 < end)
21629 *p++ = *p2++;
21630 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21633 else
21635 eassert (INTEGERP (overlay_arrow_string));
21636 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21638 overlay_arrow_seen = true;
21641 /* Highlight trailing whitespace. */
21642 if (!NILP (Vshow_trailing_whitespace))
21643 highlight_trailing_whitespace (it->f, it->glyph_row);
21645 /* Compute pixel dimensions of this line. */
21646 compute_line_metrics (it);
21648 /* Implementation note: No changes in the glyphs of ROW or in their
21649 faces can be done past this point, because compute_line_metrics
21650 computes ROW's hash value and stores it within the glyph_row
21651 structure. */
21653 /* Record whether this row ends inside an ellipsis. */
21654 row->ends_in_ellipsis_p
21655 = (it->method == GET_FROM_DISPLAY_VECTOR
21656 && it->ellipsis_p);
21658 /* Save fringe bitmaps in this row. */
21659 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21660 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21661 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21662 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21664 it->left_user_fringe_bitmap = 0;
21665 it->left_user_fringe_face_id = 0;
21666 it->right_user_fringe_bitmap = 0;
21667 it->right_user_fringe_face_id = 0;
21669 /* Maybe set the cursor. */
21670 cvpos = it->w->cursor.vpos;
21671 if ((cvpos < 0
21672 /* In bidi-reordered rows, keep checking for proper cursor
21673 position even if one has been found already, because buffer
21674 positions in such rows change non-linearly with ROW->VPOS,
21675 when a line is continued. One exception: when we are at ZV,
21676 display cursor on the first suitable glyph row, since all
21677 the empty rows after that also have their position set to ZV. */
21678 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21679 lines' rows is implemented for bidi-reordered rows. */
21680 || (it->bidi_p
21681 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21682 && PT >= MATRIX_ROW_START_CHARPOS (row)
21683 && PT <= MATRIX_ROW_END_CHARPOS (row)
21684 && cursor_row_p (row))
21685 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21687 /* Prepare for the next line. This line starts horizontally at (X
21688 HPOS) = (0 0). Vertical positions are incremented. As a
21689 convenience for the caller, IT->glyph_row is set to the next
21690 row to be used. */
21691 it->current_x = it->hpos = 0;
21692 it->current_y += row->height;
21693 /* Restore the first and last visible X if we adjusted them for
21694 current-line hscrolling. */
21695 if (hscroll_this_line)
21697 it->first_visible_x = first_visible_x;
21698 it->last_visible_x = last_visible_x;
21700 SET_TEXT_POS (it->eol_pos, 0, 0);
21701 ++it->vpos;
21702 ++it->glyph_row;
21703 /* The next row should by default use the same value of the
21704 reversed_p flag as this one. set_iterator_to_next decides when
21705 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21706 the flag accordingly. */
21707 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21708 it->glyph_row->reversed_p = row->reversed_p;
21709 it->start = row->end;
21710 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21712 #undef RECORD_MAX_MIN_POS
21715 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21716 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21717 doc: /* Return paragraph direction at point in BUFFER.
21718 Value is either `left-to-right' or `right-to-left'.
21719 If BUFFER is omitted or nil, it defaults to the current buffer.
21721 Paragraph direction determines how the text in the paragraph is displayed.
21722 In left-to-right paragraphs, text begins at the left margin of the window
21723 and the reading direction is generally left to right. In right-to-left
21724 paragraphs, text begins at the right margin and is read from right to left.
21726 See also `bidi-paragraph-direction'. */)
21727 (Lisp_Object buffer)
21729 struct buffer *buf = current_buffer;
21730 struct buffer *old = buf;
21732 if (! NILP (buffer))
21734 CHECK_BUFFER (buffer);
21735 buf = XBUFFER (buffer);
21738 if (NILP (BVAR (buf, bidi_display_reordering))
21739 || NILP (BVAR (buf, enable_multibyte_characters))
21740 /* When we are loading loadup.el, the character property tables
21741 needed for bidi iteration are not yet available. */
21742 || redisplay__inhibit_bidi)
21743 return Qleft_to_right;
21744 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21745 return BVAR (buf, bidi_paragraph_direction);
21746 else
21748 /* Determine the direction from buffer text. We could try to
21749 use current_matrix if it is up to date, but this seems fast
21750 enough as it is. */
21751 struct bidi_it itb;
21752 ptrdiff_t pos = BUF_PT (buf);
21753 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21754 int c;
21755 void *itb_data = bidi_shelve_cache ();
21757 set_buffer_temp (buf);
21758 /* bidi_paragraph_init finds the base direction of the paragraph
21759 by searching forward from paragraph start. We need the base
21760 direction of the current or _previous_ paragraph, so we need
21761 to make sure we are within that paragraph. To that end, find
21762 the previous non-empty line. */
21763 if (pos >= ZV && pos > BEGV)
21764 DEC_BOTH (pos, bytepos);
21765 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21766 if (fast_looking_at (trailing_white_space,
21767 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21769 while ((c = FETCH_BYTE (bytepos)) == '\n'
21770 || c == ' ' || c == '\t' || c == '\f')
21772 if (bytepos <= BEGV_BYTE)
21773 break;
21774 bytepos--;
21775 pos--;
21777 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21778 bytepos--;
21780 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21781 itb.paragraph_dir = NEUTRAL_DIR;
21782 itb.string.s = NULL;
21783 itb.string.lstring = Qnil;
21784 itb.string.bufpos = 0;
21785 itb.string.from_disp_str = false;
21786 itb.string.unibyte = false;
21787 /* We have no window to use here for ignoring window-specific
21788 overlays. Using NULL for window pointer will cause
21789 compute_display_string_pos to use the current buffer. */
21790 itb.w = NULL;
21791 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21792 bidi_unshelve_cache (itb_data, false);
21793 set_buffer_temp (old);
21794 switch (itb.paragraph_dir)
21796 case L2R:
21797 return Qleft_to_right;
21798 break;
21799 case R2L:
21800 return Qright_to_left;
21801 break;
21802 default:
21803 emacs_abort ();
21808 DEFUN ("bidi-find-overridden-directionality",
21809 Fbidi_find_overridden_directionality,
21810 Sbidi_find_overridden_directionality, 2, 3, 0,
21811 doc: /* Return position between FROM and TO where directionality was overridden.
21813 This function returns the first character position in the specified
21814 region of OBJECT where there is a character whose `bidi-class' property
21815 is `L', but which was forced to display as `R' by a directional
21816 override, and likewise with characters whose `bidi-class' is `R'
21817 or `AL' that were forced to display as `L'.
21819 If no such character is found, the function returns nil.
21821 OBJECT is a Lisp string or buffer to search for overridden
21822 directionality, and defaults to the current buffer if nil or omitted.
21823 OBJECT can also be a window, in which case the function will search
21824 the buffer displayed in that window. Passing the window instead of
21825 a buffer is preferable when the buffer is displayed in some window,
21826 because this function will then be able to correctly account for
21827 window-specific overlays, which can affect the results.
21829 Strong directional characters `L', `R', and `AL' can have their
21830 intrinsic directionality overridden by directional override
21831 control characters RLO (u+202e) and LRO (u+202d). See the
21832 function `get-char-code-property' for a way to inquire about
21833 the `bidi-class' property of a character. */)
21834 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21836 struct buffer *buf = current_buffer;
21837 struct buffer *old = buf;
21838 struct window *w = NULL;
21839 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21840 struct bidi_it itb;
21841 ptrdiff_t from_pos, to_pos, from_bpos;
21842 void *itb_data;
21844 if (!NILP (object))
21846 if (BUFFERP (object))
21847 buf = XBUFFER (object);
21848 else if (WINDOWP (object))
21850 w = decode_live_window (object);
21851 buf = XBUFFER (w->contents);
21852 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21854 else
21855 CHECK_STRING (object);
21858 if (STRINGP (object))
21860 /* Characters in unibyte strings are always treated by bidi.c as
21861 strong LTR. */
21862 if (!STRING_MULTIBYTE (object)
21863 /* When we are loading loadup.el, the character property
21864 tables needed for bidi iteration are not yet
21865 available. */
21866 || redisplay__inhibit_bidi)
21867 return Qnil;
21869 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21870 if (from_pos >= SCHARS (object))
21871 return Qnil;
21873 /* Set up the bidi iterator. */
21874 itb_data = bidi_shelve_cache ();
21875 itb.paragraph_dir = NEUTRAL_DIR;
21876 itb.string.lstring = object;
21877 itb.string.s = NULL;
21878 itb.string.schars = SCHARS (object);
21879 itb.string.bufpos = 0;
21880 itb.string.from_disp_str = false;
21881 itb.string.unibyte = false;
21882 itb.w = w;
21883 bidi_init_it (0, 0, frame_window_p, &itb);
21885 else
21887 /* Nothing this fancy can happen in unibyte buffers, or in a
21888 buffer that disabled reordering, or if FROM is at EOB. */
21889 if (NILP (BVAR (buf, bidi_display_reordering))
21890 || NILP (BVAR (buf, enable_multibyte_characters))
21891 /* When we are loading loadup.el, the character property
21892 tables needed for bidi iteration are not yet
21893 available. */
21894 || redisplay__inhibit_bidi)
21895 return Qnil;
21897 set_buffer_temp (buf);
21898 validate_region (&from, &to);
21899 from_pos = XINT (from);
21900 to_pos = XINT (to);
21901 if (from_pos >= ZV)
21902 return Qnil;
21904 /* Set up the bidi iterator. */
21905 itb_data = bidi_shelve_cache ();
21906 from_bpos = CHAR_TO_BYTE (from_pos);
21907 if (from_pos == BEGV)
21909 itb.charpos = BEGV;
21910 itb.bytepos = BEGV_BYTE;
21912 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21914 itb.charpos = from_pos;
21915 itb.bytepos = from_bpos;
21917 else
21918 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21919 -1, &itb.bytepos);
21920 itb.paragraph_dir = NEUTRAL_DIR;
21921 itb.string.s = NULL;
21922 itb.string.lstring = Qnil;
21923 itb.string.bufpos = 0;
21924 itb.string.from_disp_str = false;
21925 itb.string.unibyte = false;
21926 itb.w = w;
21927 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21930 ptrdiff_t found;
21931 do {
21932 /* For the purposes of this function, the actual base direction of
21933 the paragraph doesn't matter, so just set it to L2R. */
21934 bidi_paragraph_init (L2R, &itb, false);
21935 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21937 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21939 bidi_unshelve_cache (itb_data, false);
21940 set_buffer_temp (old);
21942 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21945 DEFUN ("move-point-visually", Fmove_point_visually,
21946 Smove_point_visually, 1, 1, 0,
21947 doc: /* Move point in the visual order in the specified DIRECTION.
21948 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21949 left.
21951 Value is the new character position of point. */)
21952 (Lisp_Object direction)
21954 struct window *w = XWINDOW (selected_window);
21955 struct buffer *b = XBUFFER (w->contents);
21956 struct glyph_row *row;
21957 int dir;
21958 Lisp_Object paragraph_dir;
21960 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21961 (!(ROW)->continued_p \
21962 && NILP ((GLYPH)->object) \
21963 && (GLYPH)->type == CHAR_GLYPH \
21964 && (GLYPH)->u.ch == ' ' \
21965 && (GLYPH)->charpos >= 0 \
21966 && !(GLYPH)->avoid_cursor_p)
21968 CHECK_NUMBER (direction);
21969 dir = XINT (direction);
21970 if (dir > 0)
21971 dir = 1;
21972 else
21973 dir = -1;
21975 /* If current matrix is up-to-date, we can use the information
21976 recorded in the glyphs, at least as long as the goal is on the
21977 screen. */
21978 if (w->window_end_valid
21979 && !windows_or_buffers_changed
21980 && b
21981 && !b->clip_changed
21982 && !b->prevent_redisplay_optimizations_p
21983 && !window_outdated (w)
21984 /* We rely below on the cursor coordinates to be up to date, but
21985 we cannot trust them if some command moved point since the
21986 last complete redisplay. */
21987 && w->last_point == BUF_PT (b)
21988 && w->cursor.vpos >= 0
21989 && w->cursor.vpos < w->current_matrix->nrows
21990 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21992 struct glyph *g = row->glyphs[TEXT_AREA];
21993 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21994 struct glyph *gpt = g + w->cursor.hpos;
21996 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21998 if (BUFFERP (g->object) && g->charpos != PT)
22000 SET_PT (g->charpos);
22001 w->cursor.vpos = -1;
22002 return make_number (PT);
22004 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22006 ptrdiff_t new_pos;
22008 if (BUFFERP (gpt->object))
22010 new_pos = PT;
22011 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22012 new_pos += (row->reversed_p ? -dir : dir);
22013 else
22014 new_pos -= (row->reversed_p ? -dir : dir);
22016 else if (BUFFERP (g->object))
22017 new_pos = g->charpos;
22018 else
22019 break;
22020 SET_PT (new_pos);
22021 w->cursor.vpos = -1;
22022 return make_number (PT);
22024 else if (ROW_GLYPH_NEWLINE_P (row, g))
22026 /* Glyphs inserted at the end of a non-empty line for
22027 positioning the cursor have zero charpos, so we must
22028 deduce the value of point by other means. */
22029 if (g->charpos > 0)
22030 SET_PT (g->charpos);
22031 else if (row->ends_at_zv_p && PT != ZV)
22032 SET_PT (ZV);
22033 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22034 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22035 else
22036 break;
22037 w->cursor.vpos = -1;
22038 return make_number (PT);
22041 if (g == e || NILP (g->object))
22043 if (row->truncated_on_left_p || row->truncated_on_right_p)
22044 goto simulate_display;
22045 if (!row->reversed_p)
22046 row += dir;
22047 else
22048 row -= dir;
22049 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
22050 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
22051 goto simulate_display;
22053 if (dir > 0)
22055 if (row->reversed_p && !row->continued_p)
22057 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22058 w->cursor.vpos = -1;
22059 return make_number (PT);
22061 g = row->glyphs[TEXT_AREA];
22062 e = g + row->used[TEXT_AREA];
22063 for ( ; g < e; g++)
22065 if (BUFFERP (g->object)
22066 /* Empty lines have only one glyph, which stands
22067 for the newline, and whose charpos is the
22068 buffer position of the newline. */
22069 || ROW_GLYPH_NEWLINE_P (row, g)
22070 /* When the buffer ends in a newline, the line at
22071 EOB also has one glyph, but its charpos is -1. */
22072 || (row->ends_at_zv_p
22073 && !row->reversed_p
22074 && NILP (g->object)
22075 && g->type == CHAR_GLYPH
22076 && g->u.ch == ' '))
22078 if (g->charpos > 0)
22079 SET_PT (g->charpos);
22080 else if (!row->reversed_p
22081 && row->ends_at_zv_p
22082 && PT != ZV)
22083 SET_PT (ZV);
22084 else
22085 continue;
22086 w->cursor.vpos = -1;
22087 return make_number (PT);
22091 else
22093 if (!row->reversed_p && !row->continued_p)
22095 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22096 w->cursor.vpos = -1;
22097 return make_number (PT);
22099 e = row->glyphs[TEXT_AREA];
22100 g = e + row->used[TEXT_AREA] - 1;
22101 for ( ; g >= e; g--)
22103 if (BUFFERP (g->object)
22104 || (ROW_GLYPH_NEWLINE_P (row, g)
22105 && g->charpos > 0)
22106 /* Empty R2L lines on GUI frames have the buffer
22107 position of the newline stored in the stretch
22108 glyph. */
22109 || g->type == STRETCH_GLYPH
22110 || (row->ends_at_zv_p
22111 && row->reversed_p
22112 && NILP (g->object)
22113 && g->type == CHAR_GLYPH
22114 && g->u.ch == ' '))
22116 if (g->charpos > 0)
22117 SET_PT (g->charpos);
22118 else if (row->reversed_p
22119 && row->ends_at_zv_p
22120 && PT != ZV)
22121 SET_PT (ZV);
22122 else
22123 continue;
22124 w->cursor.vpos = -1;
22125 return make_number (PT);
22132 simulate_display:
22134 /* If we wind up here, we failed to move by using the glyphs, so we
22135 need to simulate display instead. */
22137 if (b)
22138 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22139 else
22140 paragraph_dir = Qleft_to_right;
22141 if (EQ (paragraph_dir, Qright_to_left))
22142 dir = -dir;
22143 if (PT <= BEGV && dir < 0)
22144 xsignal0 (Qbeginning_of_buffer);
22145 else if (PT >= ZV && dir > 0)
22146 xsignal0 (Qend_of_buffer);
22147 else
22149 struct text_pos pt;
22150 struct it it;
22151 int pt_x, target_x, pixel_width, pt_vpos;
22152 bool at_eol_p;
22153 bool overshoot_expected = false;
22154 bool target_is_eol_p = false;
22156 /* Setup the arena. */
22157 SET_TEXT_POS (pt, PT, PT_BYTE);
22158 start_display (&it, w, pt);
22159 /* When lines are truncated, we could be called with point
22160 outside of the windows edges, in which case move_it_*
22161 functions either prematurely stop at window's edge or jump to
22162 the next screen line, whereas we rely below on our ability to
22163 reach point, in order to start from its X coordinate. So we
22164 need to disregard the window's horizontal extent in that case. */
22165 if (it.line_wrap == TRUNCATE)
22166 it.last_visible_x = DISP_INFINITY;
22168 if (it.cmp_it.id < 0
22169 && it.method == GET_FROM_STRING
22170 && it.area == TEXT_AREA
22171 && it.string_from_display_prop_p
22172 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22173 overshoot_expected = true;
22175 /* Find the X coordinate of point. We start from the beginning
22176 of this or previous line to make sure we are before point in
22177 the logical order (since the move_it_* functions can only
22178 move forward). */
22179 reseat:
22180 reseat_at_previous_visible_line_start (&it);
22181 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22182 if (IT_CHARPOS (it) != PT)
22184 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22185 -1, -1, -1, MOVE_TO_POS);
22186 /* If we missed point because the character there is
22187 displayed out of a display vector that has more than one
22188 glyph, retry expecting overshoot. */
22189 if (it.method == GET_FROM_DISPLAY_VECTOR
22190 && it.current.dpvec_index > 0
22191 && !overshoot_expected)
22193 overshoot_expected = true;
22194 goto reseat;
22196 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22197 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22199 pt_x = it.current_x;
22200 pt_vpos = it.vpos;
22201 if (dir > 0 || overshoot_expected)
22203 struct glyph_row *row = it.glyph_row;
22205 /* When point is at beginning of line, we don't have
22206 information about the glyph there loaded into struct
22207 it. Calling get_next_display_element fixes that. */
22208 if (pt_x == 0)
22209 get_next_display_element (&it);
22210 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22211 it.glyph_row = NULL;
22212 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22213 it.glyph_row = row;
22214 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22215 it, lest it will become out of sync with it's buffer
22216 position. */
22217 it.current_x = pt_x;
22219 else
22220 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22221 pixel_width = it.pixel_width;
22222 if (overshoot_expected && at_eol_p)
22223 pixel_width = 0;
22224 else if (pixel_width <= 0)
22225 pixel_width = 1;
22227 /* If there's a display string (or something similar) at point,
22228 we are actually at the glyph to the left of point, so we need
22229 to correct the X coordinate. */
22230 if (overshoot_expected)
22232 if (it.bidi_p)
22233 pt_x += pixel_width * it.bidi_it.scan_dir;
22234 else
22235 pt_x += pixel_width;
22238 /* Compute target X coordinate, either to the left or to the
22239 right of point. On TTY frames, all characters have the same
22240 pixel width of 1, so we can use that. On GUI frames we don't
22241 have an easy way of getting at the pixel width of the
22242 character to the left of point, so we use a different method
22243 of getting to that place. */
22244 if (dir > 0)
22245 target_x = pt_x + pixel_width;
22246 else
22247 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22249 /* Target X coordinate could be one line above or below the line
22250 of point, in which case we need to adjust the target X
22251 coordinate. Also, if moving to the left, we need to begin at
22252 the left edge of the point's screen line. */
22253 if (dir < 0)
22255 if (pt_x > 0)
22257 start_display (&it, w, pt);
22258 if (it.line_wrap == TRUNCATE)
22259 it.last_visible_x = DISP_INFINITY;
22260 reseat_at_previous_visible_line_start (&it);
22261 it.current_x = it.current_y = it.hpos = 0;
22262 if (pt_vpos != 0)
22263 move_it_by_lines (&it, pt_vpos);
22265 else
22267 move_it_by_lines (&it, -1);
22268 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22269 target_is_eol_p = true;
22270 /* Under word-wrap, we don't know the x coordinate of
22271 the last character displayed on the previous line,
22272 which immediately precedes the wrap point. To find
22273 out its x coordinate, we try moving to the right
22274 margin of the window, which will stop at the wrap
22275 point, and then reset target_x to point at the
22276 character that precedes the wrap point. This is not
22277 needed on GUI frames, because (see below) there we
22278 move from the left margin one grapheme cluster at a
22279 time, and stop when we hit the wrap point. */
22280 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22282 void *it_data = NULL;
22283 struct it it2;
22285 SAVE_IT (it2, it, it_data);
22286 move_it_in_display_line_to (&it, ZV, target_x,
22287 MOVE_TO_POS | MOVE_TO_X);
22288 /* If we arrived at target_x, that _is_ the last
22289 character on the previous line. */
22290 if (it.current_x != target_x)
22291 target_x = it.current_x - 1;
22292 RESTORE_IT (&it, &it2, it_data);
22296 else
22298 if (at_eol_p
22299 || (target_x >= it.last_visible_x
22300 && it.line_wrap != TRUNCATE))
22302 if (pt_x > 0)
22303 move_it_by_lines (&it, 0);
22304 move_it_by_lines (&it, 1);
22305 target_x = 0;
22309 /* Move to the target X coordinate. */
22310 /* On GUI frames, as we don't know the X coordinate of the
22311 character to the left of point, moving point to the left
22312 requires walking, one grapheme cluster at a time, until we
22313 find ourself at a place immediately to the left of the
22314 character at point. */
22315 if (FRAME_WINDOW_P (it.f) && dir < 0)
22317 struct text_pos new_pos;
22318 enum move_it_result rc = MOVE_X_REACHED;
22320 if (it.current_x == 0)
22321 get_next_display_element (&it);
22322 if (it.what == IT_COMPOSITION)
22324 new_pos.charpos = it.cmp_it.charpos;
22325 new_pos.bytepos = -1;
22327 else
22328 new_pos = it.current.pos;
22330 while (it.current_x + it.pixel_width <= target_x
22331 && (rc == MOVE_X_REACHED
22332 /* Under word-wrap, move_it_in_display_line_to
22333 stops at correct coordinates, but sometimes
22334 returns MOVE_POS_MATCH_OR_ZV. */
22335 || (it.line_wrap == WORD_WRAP
22336 && rc == MOVE_POS_MATCH_OR_ZV)))
22338 int new_x = it.current_x + it.pixel_width;
22340 /* For composed characters, we want the position of the
22341 first character in the grapheme cluster (usually, the
22342 composition's base character), whereas it.current
22343 might give us the position of the _last_ one, e.g. if
22344 the composition is rendered in reverse due to bidi
22345 reordering. */
22346 if (it.what == IT_COMPOSITION)
22348 new_pos.charpos = it.cmp_it.charpos;
22349 new_pos.bytepos = -1;
22351 else
22352 new_pos = it.current.pos;
22353 if (new_x == it.current_x)
22354 new_x++;
22355 rc = move_it_in_display_line_to (&it, ZV, new_x,
22356 MOVE_TO_POS | MOVE_TO_X);
22357 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22358 break;
22360 /* The previous position we saw in the loop is the one we
22361 want. */
22362 if (new_pos.bytepos == -1)
22363 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22364 it.current.pos = new_pos;
22366 else if (it.current_x != target_x)
22367 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22369 /* If we ended up in a display string that covers point, move to
22370 buffer position to the right in the visual order. */
22371 if (dir > 0)
22373 while (IT_CHARPOS (it) == PT)
22375 set_iterator_to_next (&it, false);
22376 if (!get_next_display_element (&it))
22377 break;
22381 /* Move point to that position. */
22382 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22385 return make_number (PT);
22387 #undef ROW_GLYPH_NEWLINE_P
22390 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22391 Sbidi_resolved_levels, 0, 1, 0,
22392 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22394 The resolved levels are produced by the Emacs bidi reordering engine
22395 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22396 read the Unicode Standard Annex 9 (UAX#9) for background information
22397 about these levels.
22399 VPOS is the zero-based number of the current window's screen line
22400 for which to produce the resolved levels. If VPOS is nil or omitted,
22401 it defaults to the screen line of point. If the window displays a
22402 header line, VPOS of zero will report on the header line, and first
22403 line of text in the window will have VPOS of 1.
22405 Value is an array of resolved levels, indexed by glyph number.
22406 Glyphs are numbered from zero starting from the beginning of the
22407 screen line, i.e. the left edge of the window for left-to-right lines
22408 and from the right edge for right-to-left lines. The resolved levels
22409 are produced only for the window's text area; text in display margins
22410 is not included.
22412 If the selected window's display is not up-to-date, or if the specified
22413 screen line does not display text, this function returns nil. It is
22414 highly recommended to bind this function to some simple key, like F8,
22415 in order to avoid these problems.
22417 This function exists mainly for testing the correctness of the
22418 Emacs UBA implementation, in particular with the test suite. */)
22419 (Lisp_Object vpos)
22421 struct window *w = XWINDOW (selected_window);
22422 struct buffer *b = XBUFFER (w->contents);
22423 int nrow;
22424 struct glyph_row *row;
22426 if (NILP (vpos))
22428 int d1, d2, d3, d4, d5;
22430 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22432 else
22434 CHECK_NUMBER_COERCE_MARKER (vpos);
22435 nrow = XINT (vpos);
22438 /* We require up-to-date glyph matrix for this window. */
22439 if (w->window_end_valid
22440 && !windows_or_buffers_changed
22441 && b
22442 && !b->clip_changed
22443 && !b->prevent_redisplay_optimizations_p
22444 && !window_outdated (w)
22445 && nrow >= 0
22446 && nrow < w->current_matrix->nrows
22447 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22448 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22450 struct glyph *g, *e, *g1;
22451 int nglyphs, i;
22452 Lisp_Object levels;
22454 if (!row->reversed_p) /* Left-to-right glyph row. */
22456 g = g1 = row->glyphs[TEXT_AREA];
22457 e = g + row->used[TEXT_AREA];
22459 /* Skip over glyphs at the start of the row that was
22460 generated by redisplay for its own needs. */
22461 while (g < e
22462 && NILP (g->object)
22463 && g->charpos < 0)
22464 g++;
22465 g1 = g;
22467 /* Count the "interesting" glyphs in this row. */
22468 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22469 nglyphs++;
22471 /* Create and fill the array. */
22472 levels = make_uninit_vector (nglyphs);
22473 for (i = 0; g1 < g; i++, g1++)
22474 ASET (levels, i, make_number (g1->resolved_level));
22476 else /* Right-to-left glyph row. */
22478 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22479 e = row->glyphs[TEXT_AREA] - 1;
22480 while (g > e
22481 && NILP (g->object)
22482 && g->charpos < 0)
22483 g--;
22484 g1 = g;
22485 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22486 nglyphs++;
22487 levels = make_uninit_vector (nglyphs);
22488 for (i = 0; g1 > g; i++, g1--)
22489 ASET (levels, i, make_number (g1->resolved_level));
22491 return levels;
22493 else
22494 return Qnil;
22499 /***********************************************************************
22500 Menu Bar
22501 ***********************************************************************/
22503 /* Redisplay the menu bar in the frame for window W.
22505 The menu bar of X frames that don't have X toolkit support is
22506 displayed in a special window W->frame->menu_bar_window.
22508 The menu bar of terminal frames is treated specially as far as
22509 glyph matrices are concerned. Menu bar lines are not part of
22510 windows, so the update is done directly on the frame matrix rows
22511 for the menu bar. */
22513 static void
22514 display_menu_bar (struct window *w)
22516 struct frame *f = XFRAME (WINDOW_FRAME (w));
22517 struct it it;
22518 Lisp_Object items;
22519 int i;
22521 /* Don't do all this for graphical frames. */
22522 #ifdef HAVE_NTGUI
22523 if (FRAME_W32_P (f))
22524 return;
22525 #endif
22526 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22527 if (FRAME_X_P (f))
22528 return;
22529 #endif
22531 #ifdef HAVE_NS
22532 if (FRAME_NS_P (f))
22533 return;
22534 #endif /* HAVE_NS */
22536 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22537 eassert (!FRAME_WINDOW_P (f));
22538 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22539 it.first_visible_x = 0;
22540 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22541 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22542 if (FRAME_WINDOW_P (f))
22544 /* Menu bar lines are displayed in the desired matrix of the
22545 dummy window menu_bar_window. */
22546 struct window *menu_w;
22547 menu_w = XWINDOW (f->menu_bar_window);
22548 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22549 MENU_FACE_ID);
22550 it.first_visible_x = 0;
22551 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22553 else
22554 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22556 /* This is a TTY frame, i.e. character hpos/vpos are used as
22557 pixel x/y. */
22558 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22559 MENU_FACE_ID);
22560 it.first_visible_x = 0;
22561 it.last_visible_x = FRAME_COLS (f);
22564 /* FIXME: This should be controlled by a user option. See the
22565 comments in redisplay_tool_bar and display_mode_line about
22566 this. */
22567 it.paragraph_embedding = L2R;
22569 /* Clear all rows of the menu bar. */
22570 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22572 struct glyph_row *row = it.glyph_row + i;
22573 clear_glyph_row (row);
22574 row->enabled_p = true;
22575 row->full_width_p = true;
22576 row->reversed_p = false;
22579 /* Display all items of the menu bar. */
22580 items = FRAME_MENU_BAR_ITEMS (it.f);
22581 for (i = 0; i < ASIZE (items); i += 4)
22583 Lisp_Object string;
22585 /* Stop at nil string. */
22586 string = AREF (items, i + 1);
22587 if (NILP (string))
22588 break;
22590 /* Remember where item was displayed. */
22591 ASET (items, i + 3, make_number (it.hpos));
22593 /* Display the item, pad with one space. */
22594 if (it.current_x < it.last_visible_x)
22595 display_string (NULL, string, Qnil, 0, 0, &it,
22596 SCHARS (string) + 1, 0, 0, -1);
22599 /* Fill out the line with spaces. */
22600 if (it.current_x < it.last_visible_x)
22601 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22603 /* Compute the total height of the lines. */
22604 compute_line_metrics (&it);
22607 /* Deep copy of a glyph row, including the glyphs. */
22608 static void
22609 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22611 struct glyph *pointers[1 + LAST_AREA];
22612 int to_used = to->used[TEXT_AREA];
22614 /* Save glyph pointers of TO. */
22615 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22617 /* Do a structure assignment. */
22618 *to = *from;
22620 /* Restore original glyph pointers of TO. */
22621 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22623 /* Copy the glyphs. */
22624 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22625 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22627 /* If we filled only part of the TO row, fill the rest with
22628 space_glyph (which will display as empty space). */
22629 if (to_used > from->used[TEXT_AREA])
22630 fill_up_frame_row_with_spaces (to, to_used);
22633 /* Display one menu item on a TTY, by overwriting the glyphs in the
22634 frame F's desired glyph matrix with glyphs produced from the menu
22635 item text. Called from term.c to display TTY drop-down menus one
22636 item at a time.
22638 ITEM_TEXT is the menu item text as a C string.
22640 FACE_ID is the face ID to be used for this menu item. FACE_ID
22641 could specify one of 3 faces: a face for an enabled item, a face
22642 for a disabled item, or a face for a selected item.
22644 X and Y are coordinates of the first glyph in the frame's desired
22645 matrix to be overwritten by the menu item. Since this is a TTY, Y
22646 is the zero-based number of the glyph row and X is the zero-based
22647 glyph number in the row, starting from left, where to start
22648 displaying the item.
22650 SUBMENU means this menu item drops down a submenu, which
22651 should be indicated by displaying a proper visual cue after the
22652 item text. */
22654 void
22655 display_tty_menu_item (const char *item_text, int width, int face_id,
22656 int x, int y, bool submenu)
22658 struct it it;
22659 struct frame *f = SELECTED_FRAME ();
22660 struct window *w = XWINDOW (f->selected_window);
22661 struct glyph_row *row;
22662 size_t item_len = strlen (item_text);
22664 eassert (FRAME_TERMCAP_P (f));
22666 /* Don't write beyond the matrix's last row. This can happen for
22667 TTY screens that are not high enough to show the entire menu.
22668 (This is actually a bit of defensive programming, as
22669 tty_menu_display already limits the number of menu items to one
22670 less than the number of screen lines.) */
22671 if (y >= f->desired_matrix->nrows)
22672 return;
22674 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22675 it.first_visible_x = 0;
22676 it.last_visible_x = FRAME_COLS (f) - 1;
22677 row = it.glyph_row;
22678 /* Start with the row contents from the current matrix. */
22679 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22680 bool saved_width = row->full_width_p;
22681 row->full_width_p = true;
22682 bool saved_reversed = row->reversed_p;
22683 row->reversed_p = false;
22684 row->enabled_p = true;
22686 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22687 desired face. */
22688 eassert (x < f->desired_matrix->matrix_w);
22689 it.current_x = it.hpos = x;
22690 it.current_y = it.vpos = y;
22691 int saved_used = row->used[TEXT_AREA];
22692 bool saved_truncated = row->truncated_on_right_p;
22693 row->used[TEXT_AREA] = x;
22694 it.face_id = face_id;
22695 it.line_wrap = TRUNCATE;
22697 /* FIXME: This should be controlled by a user option. See the
22698 comments in redisplay_tool_bar and display_mode_line about this.
22699 Also, if paragraph_embedding could ever be R2L, changes will be
22700 needed to avoid shifting to the right the row characters in
22701 term.c:append_glyph. */
22702 it.paragraph_embedding = L2R;
22704 /* Pad with a space on the left. */
22705 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22706 width--;
22707 /* Display the menu item, pad with spaces to WIDTH. */
22708 if (submenu)
22710 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22711 item_len, 0, FRAME_COLS (f) - 1, -1);
22712 width -= item_len;
22713 /* Indicate with " >" that there's a submenu. */
22714 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22715 FRAME_COLS (f) - 1, -1);
22717 else
22718 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22719 width, 0, FRAME_COLS (f) - 1, -1);
22721 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22722 row->truncated_on_right_p = saved_truncated;
22723 row->hash = row_hash (row);
22724 row->full_width_p = saved_width;
22725 row->reversed_p = saved_reversed;
22728 /***********************************************************************
22729 Mode Line
22730 ***********************************************************************/
22732 /* Redisplay mode lines in the window tree whose root is WINDOW.
22733 If FORCE, redisplay mode lines unconditionally.
22734 Otherwise, redisplay only mode lines that are garbaged. Value is
22735 the number of windows whose mode lines were redisplayed. */
22737 static int
22738 redisplay_mode_lines (Lisp_Object window, bool force)
22740 int nwindows = 0;
22742 while (!NILP (window))
22744 struct window *w = XWINDOW (window);
22746 if (WINDOWP (w->contents))
22747 nwindows += redisplay_mode_lines (w->contents, force);
22748 else if (force
22749 || FRAME_GARBAGED_P (XFRAME (w->frame))
22750 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22752 struct text_pos lpoint;
22753 struct buffer *old = current_buffer;
22755 /* Set the window's buffer for the mode line display. */
22756 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22757 set_buffer_internal_1 (XBUFFER (w->contents));
22759 /* Point refers normally to the selected window. For any
22760 other window, set up appropriate value. */
22761 if (!EQ (window, selected_window))
22763 struct text_pos pt;
22765 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22766 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22769 /* Display mode lines. */
22770 clear_glyph_matrix (w->desired_matrix);
22771 if (display_mode_lines (w))
22772 ++nwindows;
22774 /* Restore old settings. */
22775 set_buffer_internal_1 (old);
22776 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22779 window = w->next;
22782 return nwindows;
22786 /* Display the mode and/or header line of window W. Value is the
22787 sum number of mode lines and header lines displayed. */
22789 static int
22790 display_mode_lines (struct window *w)
22792 Lisp_Object old_selected_window = selected_window;
22793 Lisp_Object old_selected_frame = selected_frame;
22794 Lisp_Object new_frame = w->frame;
22795 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22796 int n = 0;
22798 selected_frame = new_frame;
22799 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22800 or window's point, then we'd need select_window_1 here as well. */
22801 XSETWINDOW (selected_window, w);
22802 XFRAME (new_frame)->selected_window = selected_window;
22804 /* These will be set while the mode line specs are processed. */
22805 line_number_displayed = false;
22806 w->column_number_displayed = -1;
22808 if (WINDOW_WANTS_MODELINE_P (w))
22810 struct window *sel_w = XWINDOW (old_selected_window);
22812 /* Select mode line face based on the real selected window. */
22813 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22814 BVAR (current_buffer, mode_line_format));
22815 ++n;
22818 if (WINDOW_WANTS_HEADER_LINE_P (w))
22820 display_mode_line (w, HEADER_LINE_FACE_ID,
22821 BVAR (current_buffer, header_line_format));
22822 ++n;
22825 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22826 selected_frame = old_selected_frame;
22827 selected_window = old_selected_window;
22828 if (n > 0)
22829 w->must_be_updated_p = true;
22830 return n;
22834 /* Display mode or header line of window W. FACE_ID specifies which
22835 line to display; it is either MODE_LINE_FACE_ID or
22836 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22837 display. Value is the pixel height of the mode/header line
22838 displayed. */
22840 static int
22841 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22843 struct it it;
22844 struct face *face;
22845 ptrdiff_t count = SPECPDL_INDEX ();
22847 init_iterator (&it, w, -1, -1, NULL, face_id);
22848 /* Don't extend on a previously drawn mode-line.
22849 This may happen if called from pos_visible_p. */
22850 it.glyph_row->enabled_p = false;
22851 prepare_desired_row (w, it.glyph_row, true);
22853 it.glyph_row->mode_line_p = true;
22855 /* FIXME: This should be controlled by a user option. But
22856 supporting such an option is not trivial, since the mode line is
22857 made up of many separate strings. */
22858 it.paragraph_embedding = L2R;
22860 record_unwind_protect (unwind_format_mode_line,
22861 format_mode_line_unwind_data (NULL, NULL,
22862 Qnil, false));
22864 mode_line_target = MODE_LINE_DISPLAY;
22866 /* Temporarily make frame's keyboard the current kboard so that
22867 kboard-local variables in the mode_line_format will get the right
22868 values. */
22869 push_kboard (FRAME_KBOARD (it.f));
22870 record_unwind_save_match_data ();
22871 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22872 pop_kboard ();
22874 unbind_to (count, Qnil);
22876 /* Fill up with spaces. */
22877 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22879 compute_line_metrics (&it);
22880 it.glyph_row->full_width_p = true;
22881 it.glyph_row->continued_p = false;
22882 it.glyph_row->truncated_on_left_p = false;
22883 it.glyph_row->truncated_on_right_p = false;
22885 /* Make a 3D mode-line have a shadow at its right end. */
22886 face = FACE_FROM_ID (it.f, face_id);
22887 extend_face_to_end_of_line (&it);
22888 if (face->box != FACE_NO_BOX)
22890 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22891 + it.glyph_row->used[TEXT_AREA] - 1);
22892 last->right_box_line_p = true;
22895 return it.glyph_row->height;
22898 /* Move element ELT in LIST to the front of LIST.
22899 Return the updated list. */
22901 static Lisp_Object
22902 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22904 register Lisp_Object tail, prev;
22905 register Lisp_Object tem;
22907 tail = list;
22908 prev = Qnil;
22909 while (CONSP (tail))
22911 tem = XCAR (tail);
22913 if (EQ (elt, tem))
22915 /* Splice out the link TAIL. */
22916 if (NILP (prev))
22917 list = XCDR (tail);
22918 else
22919 Fsetcdr (prev, XCDR (tail));
22921 /* Now make it the first. */
22922 Fsetcdr (tail, list);
22923 return tail;
22925 else
22926 prev = tail;
22927 tail = XCDR (tail);
22928 maybe_quit ();
22931 /* Not found--return unchanged LIST. */
22932 return list;
22935 /* Contribute ELT to the mode line for window IT->w. How it
22936 translates into text depends on its data type.
22938 IT describes the display environment in which we display, as usual.
22940 DEPTH is the depth in recursion. It is used to prevent
22941 infinite recursion here.
22943 FIELD_WIDTH is the number of characters the display of ELT should
22944 occupy in the mode line, and PRECISION is the maximum number of
22945 characters to display from ELT's representation. See
22946 display_string for details.
22948 Returns the hpos of the end of the text generated by ELT.
22950 PROPS is a property list to add to any string we encounter.
22952 If RISKY, remove (disregard) any properties in any string
22953 we encounter, and ignore :eval and :propertize.
22955 The global variable `mode_line_target' determines whether the
22956 output is passed to `store_mode_line_noprop',
22957 `store_mode_line_string', or `display_string'. */
22959 static int
22960 display_mode_element (struct it *it, int depth, int field_width, int precision,
22961 Lisp_Object elt, Lisp_Object props, bool risky)
22963 int n = 0, field, prec;
22964 bool literal = false;
22966 tail_recurse:
22967 if (depth > 100)
22968 elt = build_string ("*too-deep*");
22970 depth++;
22972 switch (XTYPE (elt))
22974 case Lisp_String:
22976 /* A string: output it and check for %-constructs within it. */
22977 unsigned char c;
22978 ptrdiff_t offset = 0;
22980 if (SCHARS (elt) > 0
22981 && (!NILP (props) || risky))
22983 Lisp_Object oprops, aelt;
22984 oprops = Ftext_properties_at (make_number (0), elt);
22986 /* If the starting string's properties are not what
22987 we want, translate the string. Also, if the string
22988 is risky, do that anyway. */
22990 if (NILP (Fequal (props, oprops)) || risky)
22992 /* If the starting string has properties,
22993 merge the specified ones onto the existing ones. */
22994 if (! NILP (oprops) && !risky)
22996 Lisp_Object tem;
22998 oprops = Fcopy_sequence (oprops);
22999 tem = props;
23000 while (CONSP (tem))
23002 oprops = Fplist_put (oprops, XCAR (tem),
23003 XCAR (XCDR (tem)));
23004 tem = XCDR (XCDR (tem));
23006 props = oprops;
23009 aelt = Fassoc (elt, mode_line_proptrans_alist);
23010 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23012 /* AELT is what we want. Move it to the front
23013 without consing. */
23014 elt = XCAR (aelt);
23015 mode_line_proptrans_alist
23016 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23018 else
23020 Lisp_Object tem;
23022 /* If AELT has the wrong props, it is useless.
23023 so get rid of it. */
23024 if (! NILP (aelt))
23025 mode_line_proptrans_alist
23026 = Fdelq (aelt, mode_line_proptrans_alist);
23028 elt = Fcopy_sequence (elt);
23029 Fset_text_properties (make_number (0), Flength (elt),
23030 props, elt);
23031 /* Add this item to mode_line_proptrans_alist. */
23032 mode_line_proptrans_alist
23033 = Fcons (Fcons (elt, props),
23034 mode_line_proptrans_alist);
23035 /* Truncate mode_line_proptrans_alist
23036 to at most 50 elements. */
23037 tem = Fnthcdr (make_number (50),
23038 mode_line_proptrans_alist);
23039 if (! NILP (tem))
23040 XSETCDR (tem, Qnil);
23045 offset = 0;
23047 if (literal)
23049 prec = precision - n;
23050 switch (mode_line_target)
23052 case MODE_LINE_NOPROP:
23053 case MODE_LINE_TITLE:
23054 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23055 break;
23056 case MODE_LINE_STRING:
23057 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23058 break;
23059 case MODE_LINE_DISPLAY:
23060 n += display_string (NULL, elt, Qnil, 0, 0, it,
23061 0, prec, 0, STRING_MULTIBYTE (elt));
23062 break;
23065 break;
23068 /* Handle the non-literal case. */
23070 while ((precision <= 0 || n < precision)
23071 && SREF (elt, offset) != 0
23072 && (mode_line_target != MODE_LINE_DISPLAY
23073 || it->current_x < it->last_visible_x))
23075 ptrdiff_t last_offset = offset;
23077 /* Advance to end of string or next format specifier. */
23078 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23081 if (offset - 1 != last_offset)
23083 ptrdiff_t nchars, nbytes;
23085 /* Output to end of string or up to '%'. Field width
23086 is length of string. Don't output more than
23087 PRECISION allows us. */
23088 offset--;
23090 prec = c_string_width (SDATA (elt) + last_offset,
23091 offset - last_offset, precision - n,
23092 &nchars, &nbytes);
23094 switch (mode_line_target)
23096 case MODE_LINE_NOPROP:
23097 case MODE_LINE_TITLE:
23098 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23099 break;
23100 case MODE_LINE_STRING:
23102 ptrdiff_t bytepos = last_offset;
23103 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23104 ptrdiff_t endpos = (precision <= 0
23105 ? string_byte_to_char (elt, offset)
23106 : charpos + nchars);
23107 Lisp_Object mode_string
23108 = Fsubstring (elt, make_number (charpos),
23109 make_number (endpos));
23110 n += store_mode_line_string (NULL, mode_string, false,
23111 0, 0, Qnil);
23113 break;
23114 case MODE_LINE_DISPLAY:
23116 ptrdiff_t bytepos = last_offset;
23117 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23119 if (precision <= 0)
23120 nchars = string_byte_to_char (elt, offset) - charpos;
23121 n += display_string (NULL, elt, Qnil, 0, charpos,
23122 it, 0, nchars, 0,
23123 STRING_MULTIBYTE (elt));
23125 break;
23128 else /* c == '%' */
23130 ptrdiff_t percent_position = offset;
23132 /* Get the specified minimum width. Zero means
23133 don't pad. */
23134 field = 0;
23135 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23136 field = field * 10 + c - '0';
23138 /* Don't pad beyond the total padding allowed. */
23139 if (field_width - n > 0 && field > field_width - n)
23140 field = field_width - n;
23142 /* Note that either PRECISION <= 0 or N < PRECISION. */
23143 prec = precision - n;
23145 if (c == 'M')
23146 n += display_mode_element (it, depth, field, prec,
23147 Vglobal_mode_string, props,
23148 risky);
23149 else if (c != 0)
23151 bool multibyte;
23152 ptrdiff_t bytepos, charpos;
23153 const char *spec;
23154 Lisp_Object string;
23156 bytepos = percent_position;
23157 charpos = (STRING_MULTIBYTE (elt)
23158 ? string_byte_to_char (elt, bytepos)
23159 : bytepos);
23160 spec = decode_mode_spec (it->w, c, field, &string);
23161 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23163 switch (mode_line_target)
23165 case MODE_LINE_NOPROP:
23166 case MODE_LINE_TITLE:
23167 n += store_mode_line_noprop (spec, field, prec);
23168 break;
23169 case MODE_LINE_STRING:
23171 Lisp_Object tem = build_string (spec);
23172 props = Ftext_properties_at (make_number (charpos), elt);
23173 /* Should only keep face property in props */
23174 n += store_mode_line_string (NULL, tem, false,
23175 field, prec, props);
23177 break;
23178 case MODE_LINE_DISPLAY:
23180 int nglyphs_before, nwritten;
23182 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23183 nwritten = display_string (spec, string, elt,
23184 charpos, 0, it,
23185 field, prec, 0,
23186 multibyte);
23188 /* Assign to the glyphs written above the
23189 string where the `%x' came from, position
23190 of the `%'. */
23191 if (nwritten > 0)
23193 struct glyph *glyph
23194 = (it->glyph_row->glyphs[TEXT_AREA]
23195 + nglyphs_before);
23196 int i;
23198 for (i = 0; i < nwritten; ++i)
23200 glyph[i].object = elt;
23201 glyph[i].charpos = charpos;
23204 n += nwritten;
23207 break;
23210 else /* c == 0 */
23211 break;
23215 break;
23217 case Lisp_Symbol:
23218 /* A symbol: process the value of the symbol recursively
23219 as if it appeared here directly. Avoid error if symbol void.
23220 Special case: if value of symbol is a string, output the string
23221 literally. */
23223 register Lisp_Object tem;
23225 /* If the variable is not marked as risky to set
23226 then its contents are risky to use. */
23227 if (NILP (Fget (elt, Qrisky_local_variable)))
23228 risky = true;
23230 tem = Fboundp (elt);
23231 if (!NILP (tem))
23233 tem = Fsymbol_value (elt);
23234 /* If value is a string, output that string literally:
23235 don't check for % within it. */
23236 if (STRINGP (tem))
23237 literal = true;
23239 if (!EQ (tem, elt))
23241 /* Give up right away for nil or t. */
23242 elt = tem;
23243 goto tail_recurse;
23247 break;
23249 case Lisp_Cons:
23251 register Lisp_Object car, tem;
23253 /* A cons cell: five distinct cases.
23254 If first element is :eval or :propertize, do something special.
23255 If first element is a string or a cons, process all the elements
23256 and effectively concatenate them.
23257 If first element is a negative number, truncate displaying cdr to
23258 at most that many characters. If positive, pad (with spaces)
23259 to at least that many characters.
23260 If first element is a symbol, process the cadr or caddr recursively
23261 according to whether the symbol's value is non-nil or nil. */
23262 car = XCAR (elt);
23263 if (EQ (car, QCeval))
23265 /* An element of the form (:eval FORM) means evaluate FORM
23266 and use the result as mode line elements. */
23268 if (risky)
23269 break;
23271 if (CONSP (XCDR (elt)))
23273 Lisp_Object spec;
23274 spec = safe__eval (true, XCAR (XCDR (elt)));
23275 n += display_mode_element (it, depth, field_width - n,
23276 precision - n, spec, props,
23277 risky);
23280 else if (EQ (car, QCpropertize))
23282 /* An element of the form (:propertize ELT PROPS...)
23283 means display ELT but applying properties PROPS. */
23285 if (risky)
23286 break;
23288 if (CONSP (XCDR (elt)))
23289 n += display_mode_element (it, depth, field_width - n,
23290 precision - n, XCAR (XCDR (elt)),
23291 XCDR (XCDR (elt)), risky);
23293 else if (SYMBOLP (car))
23295 tem = Fboundp (car);
23296 elt = XCDR (elt);
23297 if (!CONSP (elt))
23298 goto invalid;
23299 /* elt is now the cdr, and we know it is a cons cell.
23300 Use its car if CAR has a non-nil value. */
23301 if (!NILP (tem))
23303 tem = Fsymbol_value (car);
23304 if (!NILP (tem))
23306 elt = XCAR (elt);
23307 goto tail_recurse;
23310 /* Symbol's value is nil (or symbol is unbound)
23311 Get the cddr of the original list
23312 and if possible find the caddr and use that. */
23313 elt = XCDR (elt);
23314 if (NILP (elt))
23315 break;
23316 else if (!CONSP (elt))
23317 goto invalid;
23318 elt = XCAR (elt);
23319 goto tail_recurse;
23321 else if (INTEGERP (car))
23323 register int lim = XINT (car);
23324 elt = XCDR (elt);
23325 if (lim < 0)
23327 /* Negative int means reduce maximum width. */
23328 if (precision <= 0)
23329 precision = -lim;
23330 else
23331 precision = min (precision, -lim);
23333 else if (lim > 0)
23335 /* Padding specified. Don't let it be more than
23336 current maximum. */
23337 if (precision > 0)
23338 lim = min (precision, lim);
23340 /* If that's more padding than already wanted, queue it.
23341 But don't reduce padding already specified even if
23342 that is beyond the current truncation point. */
23343 field_width = max (lim, field_width);
23345 goto tail_recurse;
23347 else if (STRINGP (car) || CONSP (car))
23348 FOR_EACH_TAIL_SAFE (elt)
23350 if (0 < precision && precision <= n)
23351 break;
23352 n += display_mode_element (it, depth,
23353 /* Pad after only the last
23354 list element. */
23355 (! CONSP (XCDR (elt))
23356 ? field_width - n
23357 : 0),
23358 precision - n, XCAR (elt),
23359 props, risky);
23362 break;
23364 default:
23365 invalid:
23366 elt = build_string ("*invalid*");
23367 goto tail_recurse;
23370 /* Pad to FIELD_WIDTH. */
23371 if (field_width > 0 && n < field_width)
23373 switch (mode_line_target)
23375 case MODE_LINE_NOPROP:
23376 case MODE_LINE_TITLE:
23377 n += store_mode_line_noprop ("", field_width - n, 0);
23378 break;
23379 case MODE_LINE_STRING:
23380 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23381 Qnil);
23382 break;
23383 case MODE_LINE_DISPLAY:
23384 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23385 0, 0, 0);
23386 break;
23390 return n;
23393 /* Store a mode-line string element in mode_line_string_list.
23395 If STRING is non-null, display that C string. Otherwise, the Lisp
23396 string LISP_STRING is displayed.
23398 FIELD_WIDTH is the minimum number of output glyphs to produce.
23399 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23400 with spaces. FIELD_WIDTH <= 0 means don't pad.
23402 PRECISION is the maximum number of characters to output from
23403 STRING. PRECISION <= 0 means don't truncate the string.
23405 If COPY_STRING, make a copy of LISP_STRING before adding
23406 properties to the string.
23408 PROPS are the properties to add to the string.
23409 The mode_line_string_face face property is always added to the string.
23412 static int
23413 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23414 bool copy_string,
23415 int field_width, int precision, Lisp_Object props)
23417 ptrdiff_t len;
23418 int n = 0;
23420 if (string != NULL)
23422 len = strlen (string);
23423 if (precision > 0 && len > precision)
23424 len = precision;
23425 lisp_string = make_string (string, len);
23426 if (NILP (props))
23427 props = mode_line_string_face_prop;
23428 else if (!NILP (mode_line_string_face))
23430 Lisp_Object face = Fplist_get (props, Qface);
23431 props = Fcopy_sequence (props);
23432 if (NILP (face))
23433 face = mode_line_string_face;
23434 else
23435 face = list2 (face, mode_line_string_face);
23436 props = Fplist_put (props, Qface, face);
23438 Fadd_text_properties (make_number (0), make_number (len),
23439 props, lisp_string);
23441 else
23443 len = XFASTINT (Flength (lisp_string));
23444 if (precision > 0 && len > precision)
23446 len = precision;
23447 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23448 precision = -1;
23450 if (!NILP (mode_line_string_face))
23452 Lisp_Object face;
23453 if (NILP (props))
23454 props = Ftext_properties_at (make_number (0), lisp_string);
23455 face = Fplist_get (props, Qface);
23456 if (NILP (face))
23457 face = mode_line_string_face;
23458 else
23459 face = list2 (face, mode_line_string_face);
23460 props = list2 (Qface, face);
23461 if (copy_string)
23462 lisp_string = Fcopy_sequence (lisp_string);
23464 if (!NILP (props))
23465 Fadd_text_properties (make_number (0), make_number (len),
23466 props, lisp_string);
23469 if (len > 0)
23471 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23472 n += len;
23475 if (field_width > len)
23477 field_width -= len;
23478 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23479 if (!NILP (props))
23480 Fadd_text_properties (make_number (0), make_number (field_width),
23481 props, lisp_string);
23482 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23483 n += field_width;
23486 return n;
23490 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23491 1, 4, 0,
23492 doc: /* Format a string out of a mode line format specification.
23493 First arg FORMAT specifies the mode line format (see `mode-line-format'
23494 for details) to use.
23496 By default, the format is evaluated for the currently selected window.
23498 Optional second arg FACE specifies the face property to put on all
23499 characters for which no face is specified. The value nil means the
23500 default face. The value t means whatever face the window's mode line
23501 currently uses (either `mode-line' or `mode-line-inactive',
23502 depending on whether the window is the selected window or not).
23503 An integer value means the value string has no text
23504 properties.
23506 Optional third and fourth args WINDOW and BUFFER specify the window
23507 and buffer to use as the context for the formatting (defaults
23508 are the selected window and the WINDOW's buffer). */)
23509 (Lisp_Object format, Lisp_Object face,
23510 Lisp_Object window, Lisp_Object buffer)
23512 struct it it;
23513 int len;
23514 struct window *w;
23515 struct buffer *old_buffer = NULL;
23516 int face_id;
23517 bool no_props = INTEGERP (face);
23518 ptrdiff_t count = SPECPDL_INDEX ();
23519 Lisp_Object str;
23520 int string_start = 0;
23522 w = decode_any_window (window);
23523 XSETWINDOW (window, w);
23525 if (NILP (buffer))
23526 buffer = w->contents;
23527 CHECK_BUFFER (buffer);
23529 /* Make formatting the modeline a non-op when noninteractive, otherwise
23530 there will be problems later caused by a partially initialized frame. */
23531 if (NILP (format) || noninteractive)
23532 return empty_unibyte_string;
23534 if (no_props)
23535 face = Qnil;
23537 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23538 : EQ (face, Qt) ? (EQ (window, selected_window)
23539 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23540 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23541 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23542 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23543 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23544 : DEFAULT_FACE_ID;
23546 old_buffer = current_buffer;
23548 /* Save things including mode_line_proptrans_alist,
23549 and set that to nil so that we don't alter the outer value. */
23550 record_unwind_protect (unwind_format_mode_line,
23551 format_mode_line_unwind_data
23552 (XFRAME (WINDOW_FRAME (w)),
23553 old_buffer, selected_window, true));
23554 mode_line_proptrans_alist = Qnil;
23556 Fselect_window (window, Qt);
23557 set_buffer_internal_1 (XBUFFER (buffer));
23559 init_iterator (&it, w, -1, -1, NULL, face_id);
23561 if (no_props)
23563 mode_line_target = MODE_LINE_NOPROP;
23564 mode_line_string_face_prop = Qnil;
23565 mode_line_string_list = Qnil;
23566 string_start = MODE_LINE_NOPROP_LEN (0);
23568 else
23570 mode_line_target = MODE_LINE_STRING;
23571 mode_line_string_list = Qnil;
23572 mode_line_string_face = face;
23573 mode_line_string_face_prop
23574 = NILP (face) ? Qnil : list2 (Qface, face);
23577 push_kboard (FRAME_KBOARD (it.f));
23578 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23579 pop_kboard ();
23581 if (no_props)
23583 len = MODE_LINE_NOPROP_LEN (string_start);
23584 str = make_string (mode_line_noprop_buf + string_start, len);
23586 else
23588 mode_line_string_list = Fnreverse (mode_line_string_list);
23589 str = Fmapconcat (Qidentity, mode_line_string_list,
23590 empty_unibyte_string);
23593 unbind_to (count, Qnil);
23594 return str;
23597 /* Write a null-terminated, right justified decimal representation of
23598 the positive integer D to BUF using a minimal field width WIDTH. */
23600 static void
23601 pint2str (register char *buf, register int width, register ptrdiff_t d)
23603 register char *p = buf;
23605 if (d <= 0)
23606 *p++ = '0';
23607 else
23609 while (d > 0)
23611 *p++ = d % 10 + '0';
23612 d /= 10;
23616 for (width -= (int) (p - buf); width > 0; --width)
23617 *p++ = ' ';
23618 *p-- = '\0';
23619 while (p > buf)
23621 d = *buf;
23622 *buf++ = *p;
23623 *p-- = d;
23627 /* Write a null-terminated, right justified decimal and "human
23628 readable" representation of the nonnegative integer D to BUF using
23629 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23631 static const char power_letter[] =
23633 0, /* no letter */
23634 'k', /* kilo */
23635 'M', /* mega */
23636 'G', /* giga */
23637 'T', /* tera */
23638 'P', /* peta */
23639 'E', /* exa */
23640 'Z', /* zetta */
23641 'Y' /* yotta */
23644 static void
23645 pint2hrstr (char *buf, int width, ptrdiff_t d)
23647 /* We aim to represent the nonnegative integer D as
23648 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23649 ptrdiff_t quotient = d;
23650 int remainder = 0;
23651 /* -1 means: do not use TENTHS. */
23652 int tenths = -1;
23653 int exponent = 0;
23655 /* Length of QUOTIENT.TENTHS as a string. */
23656 int length;
23658 char * psuffix;
23659 char * p;
23661 if (quotient >= 1000)
23663 /* Scale to the appropriate EXPONENT. */
23666 remainder = quotient % 1000;
23667 quotient /= 1000;
23668 exponent++;
23670 while (quotient >= 1000);
23672 /* Round to nearest and decide whether to use TENTHS or not. */
23673 if (quotient <= 9)
23675 tenths = remainder / 100;
23676 if (remainder % 100 >= 50)
23678 if (tenths < 9)
23679 tenths++;
23680 else
23682 quotient++;
23683 if (quotient == 10)
23684 tenths = -1;
23685 else
23686 tenths = 0;
23690 else
23691 if (remainder >= 500)
23693 if (quotient < 999)
23694 quotient++;
23695 else
23697 quotient = 1;
23698 exponent++;
23699 tenths = 0;
23704 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23705 if (tenths == -1 && quotient <= 99)
23706 if (quotient <= 9)
23707 length = 1;
23708 else
23709 length = 2;
23710 else
23711 length = 3;
23712 p = psuffix = buf + max (width, length);
23714 /* Print EXPONENT. */
23715 *psuffix++ = power_letter[exponent];
23716 *psuffix = '\0';
23718 /* Print TENTHS. */
23719 if (tenths >= 0)
23721 *--p = '0' + tenths;
23722 *--p = '.';
23725 /* Print QUOTIENT. */
23728 int digit = quotient % 10;
23729 *--p = '0' + digit;
23731 while ((quotient /= 10) != 0);
23733 /* Print leading spaces. */
23734 while (buf < p)
23735 *--p = ' ';
23738 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23739 If EOL_FLAG, set also a mnemonic character for end-of-line
23740 type of CODING_SYSTEM. Return updated pointer into BUF. */
23742 static unsigned char invalid_eol_type[] = "(*invalid*)";
23744 static char *
23745 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23747 Lisp_Object val;
23748 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23749 const unsigned char *eol_str;
23750 int eol_str_len;
23751 /* The EOL conversion we are using. */
23752 Lisp_Object eoltype;
23754 val = CODING_SYSTEM_SPEC (coding_system);
23755 eoltype = Qnil;
23757 if (!VECTORP (val)) /* Not yet decided. */
23759 *buf++ = multibyte ? '-' : ' ';
23760 if (eol_flag)
23761 eoltype = eol_mnemonic_undecided;
23762 /* Don't mention EOL conversion if it isn't decided. */
23764 else
23766 Lisp_Object attrs;
23767 Lisp_Object eolvalue;
23769 attrs = AREF (val, 0);
23770 eolvalue = AREF (val, 2);
23772 *buf++ = multibyte
23773 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23774 : ' ';
23776 if (eol_flag)
23778 /* The EOL conversion that is normal on this system. */
23780 if (NILP (eolvalue)) /* Not yet decided. */
23781 eoltype = eol_mnemonic_undecided;
23782 else if (VECTORP (eolvalue)) /* Not yet decided. */
23783 eoltype = eol_mnemonic_undecided;
23784 else /* eolvalue is Qunix, Qdos, or Qmac. */
23785 eoltype = (EQ (eolvalue, Qunix)
23786 ? eol_mnemonic_unix
23787 : EQ (eolvalue, Qdos)
23788 ? eol_mnemonic_dos : eol_mnemonic_mac);
23792 if (eol_flag)
23794 /* Mention the EOL conversion if it is not the usual one. */
23795 if (STRINGP (eoltype))
23797 eol_str = SDATA (eoltype);
23798 eol_str_len = SBYTES (eoltype);
23800 else if (CHARACTERP (eoltype))
23802 int c = XFASTINT (eoltype);
23803 return buf + CHAR_STRING (c, (unsigned char *) buf);
23805 else
23807 eol_str = invalid_eol_type;
23808 eol_str_len = sizeof (invalid_eol_type) - 1;
23810 memcpy (buf, eol_str, eol_str_len);
23811 buf += eol_str_len;
23814 return buf;
23817 /* Return the approximate percentage N is of D (rounding upward), or 99,
23818 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23820 static int
23821 percent99 (ptrdiff_t n, ptrdiff_t d)
23823 int percent = (d - 1 + 100.0 * n) / d;
23824 return min (percent, 99);
23827 /* Return a string for the output of a mode line %-spec for window W,
23828 generated by character C. FIELD_WIDTH > 0 means pad the string
23829 returned with spaces to that value. Return a Lisp string in
23830 *STRING if the resulting string is taken from that Lisp string.
23832 Note we operate on the current buffer for most purposes. */
23834 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23836 static const char *
23837 decode_mode_spec (struct window *w, register int c, int field_width,
23838 Lisp_Object *string)
23840 Lisp_Object obj;
23841 struct frame *f = XFRAME (WINDOW_FRAME (w));
23842 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23843 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23844 produce strings from numerical values, so limit preposterously
23845 large values of FIELD_WIDTH to avoid overrunning the buffer's
23846 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23847 bytes plus the terminating null. */
23848 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23849 struct buffer *b = current_buffer;
23851 obj = Qnil;
23852 *string = Qnil;
23854 switch (c)
23856 case '*':
23857 if (!NILP (BVAR (b, read_only)))
23858 return "%";
23859 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23860 return "*";
23861 return "-";
23863 case '+':
23864 /* This differs from %* only for a modified read-only buffer. */
23865 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23866 return "*";
23867 if (!NILP (BVAR (b, read_only)))
23868 return "%";
23869 return "-";
23871 case '&':
23872 /* This differs from %* in ignoring read-only-ness. */
23873 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23874 return "*";
23875 return "-";
23877 case '%':
23878 return "%";
23880 case '[':
23882 int i;
23883 char *p;
23885 if (command_loop_level > 5)
23886 return "[[[... ";
23887 p = decode_mode_spec_buf;
23888 for (i = 0; i < command_loop_level; i++)
23889 *p++ = '[';
23890 *p = 0;
23891 return decode_mode_spec_buf;
23894 case ']':
23896 int i;
23897 char *p;
23899 if (command_loop_level > 5)
23900 return " ...]]]";
23901 p = decode_mode_spec_buf;
23902 for (i = 0; i < command_loop_level; i++)
23903 *p++ = ']';
23904 *p = 0;
23905 return decode_mode_spec_buf;
23908 case '-':
23910 register int i;
23912 /* Let lots_of_dashes be a string of infinite length. */
23913 if (mode_line_target == MODE_LINE_NOPROP
23914 || mode_line_target == MODE_LINE_STRING)
23915 return "--";
23916 if (field_width <= 0
23917 || field_width > sizeof (lots_of_dashes))
23919 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23920 decode_mode_spec_buf[i] = '-';
23921 decode_mode_spec_buf[i] = '\0';
23922 return decode_mode_spec_buf;
23924 else
23925 return lots_of_dashes;
23928 case 'b':
23929 obj = BVAR (b, name);
23930 break;
23932 case 'c':
23933 case 'C':
23934 /* %c, %C, and %l are ignored in `frame-title-format'.
23935 (In redisplay_internal, the frame title is drawn _before_ the
23936 windows are updated, so the stuff which depends on actual
23937 window contents (such as %l) may fail to render properly, or
23938 even crash emacs.) */
23939 if (mode_line_target == MODE_LINE_TITLE)
23940 return "";
23941 else
23943 ptrdiff_t col = current_column ();
23944 int disp_col = (c == 'C') ? col + 1 : col;
23945 w->column_number_displayed = col;
23946 pint2str (decode_mode_spec_buf, width, disp_col);
23947 return decode_mode_spec_buf;
23950 case 'e':
23951 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23953 if (NILP (Vmemory_full))
23954 return "";
23955 else
23956 return "!MEM FULL! ";
23958 #else
23959 return "";
23960 #endif
23962 case 'F':
23963 /* %F displays the frame name. */
23964 if (!NILP (f->title))
23965 return SSDATA (f->title);
23966 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23967 return SSDATA (f->name);
23968 return "Emacs";
23970 case 'f':
23971 obj = BVAR (b, filename);
23972 break;
23974 case 'i':
23976 ptrdiff_t size = ZV - BEGV;
23977 pint2str (decode_mode_spec_buf, width, size);
23978 return decode_mode_spec_buf;
23981 case 'I':
23983 ptrdiff_t size = ZV - BEGV;
23984 pint2hrstr (decode_mode_spec_buf, width, size);
23985 return decode_mode_spec_buf;
23988 case 'l':
23990 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23991 ptrdiff_t topline, nlines, height;
23992 ptrdiff_t junk;
23994 /* %c, %C, and %l are ignored in `frame-title-format'. */
23995 if (mode_line_target == MODE_LINE_TITLE)
23996 return "";
23998 startpos = marker_position (w->start);
23999 startpos_byte = marker_byte_position (w->start);
24000 height = WINDOW_TOTAL_LINES (w);
24002 /* If we decided that this buffer isn't suitable for line numbers,
24003 don't forget that too fast. */
24004 if (w->base_line_pos == -1)
24005 goto no_value;
24007 /* If the buffer is very big, don't waste time. */
24008 if (INTEGERP (Vline_number_display_limit)
24009 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24011 w->base_line_pos = 0;
24012 w->base_line_number = 0;
24013 goto no_value;
24016 if (w->base_line_number > 0
24017 && w->base_line_pos > 0
24018 && w->base_line_pos <= startpos)
24020 line = w->base_line_number;
24021 linepos = w->base_line_pos;
24022 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24024 else
24026 line = 1;
24027 linepos = BUF_BEGV (b);
24028 linepos_byte = BUF_BEGV_BYTE (b);
24031 /* Count lines from base line to window start position. */
24032 nlines = display_count_lines (linepos_byte,
24033 startpos_byte,
24034 startpos, &junk);
24036 topline = nlines + line;
24038 /* Determine a new base line, if the old one is too close
24039 or too far away, or if we did not have one.
24040 "Too close" means it's plausible a scroll-down would
24041 go back past it. */
24042 if (startpos == BUF_BEGV (b))
24044 w->base_line_number = topline;
24045 w->base_line_pos = BUF_BEGV (b);
24047 else if (nlines < height + 25 || nlines > height * 3 + 50
24048 || linepos == BUF_BEGV (b))
24050 ptrdiff_t limit = BUF_BEGV (b);
24051 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24052 ptrdiff_t position;
24053 ptrdiff_t distance =
24054 (height * 2 + 30) * line_number_display_limit_width;
24056 if (startpos - distance > limit)
24058 limit = startpos - distance;
24059 limit_byte = CHAR_TO_BYTE (limit);
24062 nlines = display_count_lines (startpos_byte,
24063 limit_byte,
24064 - (height * 2 + 30),
24065 &position);
24066 /* If we couldn't find the lines we wanted within
24067 line_number_display_limit_width chars per line,
24068 give up on line numbers for this window. */
24069 if (position == limit_byte && limit == startpos - distance)
24071 w->base_line_pos = -1;
24072 w->base_line_number = 0;
24073 goto no_value;
24076 w->base_line_number = topline - nlines;
24077 w->base_line_pos = BYTE_TO_CHAR (position);
24080 /* Now count lines from the start pos to point. */
24081 nlines = display_count_lines (startpos_byte,
24082 PT_BYTE, PT, &junk);
24084 /* Record that we did display the line number. */
24085 line_number_displayed = true;
24087 /* Make the string to show. */
24088 pint2str (decode_mode_spec_buf, width, topline + nlines);
24089 return decode_mode_spec_buf;
24090 no_value:
24092 char *p = decode_mode_spec_buf;
24093 int pad = width - 2;
24094 while (pad-- > 0)
24095 *p++ = ' ';
24096 *p++ = '?';
24097 *p++ = '?';
24098 *p = '\0';
24099 return decode_mode_spec_buf;
24102 break;
24104 case 'm':
24105 obj = BVAR (b, mode_name);
24106 break;
24108 case 'n':
24109 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24110 return " Narrow";
24111 break;
24113 /* Display the "degree of travel" of the window through the buffer. */
24114 case 'o':
24116 ptrdiff_t toppos = marker_position (w->start);
24117 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24118 ptrdiff_t begv = BUF_BEGV (b);
24119 ptrdiff_t zv = BUF_ZV (b);
24121 if (zv <= botpos)
24122 return toppos <= begv ? "All" : "Bottom";
24123 else if (toppos <= begv)
24124 return "Top";
24125 else
24127 sprintf (decode_mode_spec_buf, "%2d%%",
24128 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24129 return decode_mode_spec_buf;
24133 /* Display percentage of buffer above the top of the screen. */
24134 case 'p':
24136 ptrdiff_t pos = marker_position (w->start);
24137 ptrdiff_t begv = BUF_BEGV (b);
24138 ptrdiff_t zv = BUF_ZV (b);
24140 if (w->window_end_pos <= BUF_Z (b) - zv)
24141 return pos <= begv ? "All" : "Bottom";
24142 else if (pos <= begv)
24143 return "Top";
24144 else
24146 sprintf (decode_mode_spec_buf, "%2d%%",
24147 percent99 (pos - begv, zv - begv));
24148 return decode_mode_spec_buf;
24152 /* Display percentage of size above the bottom of the screen. */
24153 case 'P':
24155 ptrdiff_t toppos = marker_position (w->start);
24156 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24157 ptrdiff_t begv = BUF_BEGV (b);
24158 ptrdiff_t zv = BUF_ZV (b);
24160 if (zv <= botpos)
24161 return toppos <= begv ? "All" : "Bottom";
24162 else
24164 sprintf (decode_mode_spec_buf,
24165 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24166 percent99 (botpos - begv, zv - begv));
24167 return decode_mode_spec_buf;
24171 /* Display percentage offsets of top and bottom of the window,
24172 using "All" (but not "Top" or "Bottom") where appropriate. */
24173 case 'q':
24175 ptrdiff_t toppos = marker_position (w->start);
24176 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24177 ptrdiff_t begv = BUF_BEGV (b);
24178 ptrdiff_t zv = BUF_ZV (b);
24179 int top_perc, bot_perc;
24181 if ((toppos <= begv) && (zv <= botpos))
24182 return "All ";
24184 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24185 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24187 if (top_perc == bot_perc)
24188 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24189 else
24190 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24192 return decode_mode_spec_buf;
24195 case 's':
24196 /* status of process */
24197 obj = Fget_buffer_process (Fcurrent_buffer ());
24198 if (NILP (obj))
24199 return "no process";
24200 #ifndef MSDOS
24201 obj = Fsymbol_name (Fprocess_status (obj));
24202 #endif
24203 break;
24205 case '@':
24207 ptrdiff_t count = inhibit_garbage_collection ();
24208 Lisp_Object curdir = BVAR (current_buffer, directory);
24209 Lisp_Object val = Qnil;
24211 if (STRINGP (curdir))
24212 val = call1 (intern ("file-remote-p"), curdir);
24214 unbind_to (count, Qnil);
24216 if (NILP (val))
24217 return "-";
24218 else
24219 return "@";
24222 case 'z':
24223 /* coding-system (not including end-of-line format) */
24224 case 'Z':
24225 /* coding-system (including end-of-line type) */
24227 bool eol_flag = (c == 'Z');
24228 char *p = decode_mode_spec_buf;
24230 if (! FRAME_WINDOW_P (f))
24232 /* No need to mention EOL here--the terminal never needs
24233 to do EOL conversion. */
24234 p = decode_mode_spec_coding (CODING_ID_NAME
24235 (FRAME_KEYBOARD_CODING (f)->id),
24236 p, false);
24237 p = decode_mode_spec_coding (CODING_ID_NAME
24238 (FRAME_TERMINAL_CODING (f)->id),
24239 p, false);
24241 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24242 p, eol_flag);
24244 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24245 #ifdef subprocesses
24246 obj = Fget_buffer_process (Fcurrent_buffer ());
24247 if (PROCESSP (obj))
24249 p = decode_mode_spec_coding
24250 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24251 p = decode_mode_spec_coding
24252 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24254 #endif /* subprocesses */
24255 #endif /* false */
24256 *p = 0;
24257 return decode_mode_spec_buf;
24261 if (STRINGP (obj))
24263 *string = obj;
24264 return SSDATA (obj);
24266 else
24267 return "";
24271 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24272 means count lines back from START_BYTE. But don't go beyond
24273 LIMIT_BYTE. Return the number of lines thus found (always
24274 nonnegative).
24276 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24277 either the position COUNT lines after/before START_BYTE, if we
24278 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24279 COUNT lines. */
24281 static ptrdiff_t
24282 display_count_lines (ptrdiff_t start_byte,
24283 ptrdiff_t limit_byte, ptrdiff_t count,
24284 ptrdiff_t *byte_pos_ptr)
24286 register unsigned char *cursor;
24287 unsigned char *base;
24289 register ptrdiff_t ceiling;
24290 register unsigned char *ceiling_addr;
24291 ptrdiff_t orig_count = count;
24293 /* If we are not in selective display mode,
24294 check only for newlines. */
24295 bool selective_display
24296 = (!NILP (BVAR (current_buffer, selective_display))
24297 && !INTEGERP (BVAR (current_buffer, selective_display)));
24299 if (count > 0)
24301 while (start_byte < limit_byte)
24303 ceiling = BUFFER_CEILING_OF (start_byte);
24304 ceiling = min (limit_byte - 1, ceiling);
24305 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24306 base = (cursor = BYTE_POS_ADDR (start_byte));
24310 if (selective_display)
24312 while (*cursor != '\n' && *cursor != 015
24313 && ++cursor != ceiling_addr)
24314 continue;
24315 if (cursor == ceiling_addr)
24316 break;
24318 else
24320 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24321 if (! cursor)
24322 break;
24325 cursor++;
24327 if (--count == 0)
24329 start_byte += cursor - base;
24330 *byte_pos_ptr = start_byte;
24331 return orig_count;
24334 while (cursor < ceiling_addr);
24336 start_byte += ceiling_addr - base;
24339 else
24341 while (start_byte > limit_byte)
24343 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24344 ceiling = max (limit_byte, ceiling);
24345 ceiling_addr = BYTE_POS_ADDR (ceiling);
24346 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24347 while (true)
24349 if (selective_display)
24351 while (--cursor >= ceiling_addr
24352 && *cursor != '\n' && *cursor != 015)
24353 continue;
24354 if (cursor < ceiling_addr)
24355 break;
24357 else
24359 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24360 if (! cursor)
24361 break;
24364 if (++count == 0)
24366 start_byte += cursor - base + 1;
24367 *byte_pos_ptr = start_byte;
24368 /* When scanning backwards, we should
24369 not count the newline posterior to which we stop. */
24370 return - orig_count - 1;
24373 start_byte += ceiling_addr - base;
24377 *byte_pos_ptr = limit_byte;
24379 if (count < 0)
24380 return - orig_count + count;
24381 return orig_count - count;
24387 /***********************************************************************
24388 Displaying strings
24389 ***********************************************************************/
24391 /* Display a NUL-terminated string, starting with index START.
24393 If STRING is non-null, display that C string. Otherwise, the Lisp
24394 string LISP_STRING is displayed. There's a case that STRING is
24395 non-null and LISP_STRING is not nil. It means STRING is a string
24396 data of LISP_STRING. In that case, we display LISP_STRING while
24397 ignoring its text properties.
24399 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24400 FACE_STRING. Display STRING or LISP_STRING with the face at
24401 FACE_STRING_POS in FACE_STRING:
24403 Display the string in the environment given by IT, but use the
24404 standard display table, temporarily.
24406 FIELD_WIDTH is the minimum number of output glyphs to produce.
24407 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24408 with spaces. If STRING has more characters, more than FIELD_WIDTH
24409 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24411 PRECISION is the maximum number of characters to output from
24412 STRING. PRECISION < 0 means don't truncate the string.
24414 This is roughly equivalent to printf format specifiers:
24416 FIELD_WIDTH PRECISION PRINTF
24417 ----------------------------------------
24418 -1 -1 %s
24419 -1 10 %.10s
24420 10 -1 %10s
24421 20 10 %20.10s
24423 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24424 display them, and < 0 means obey the current buffer's value of
24425 enable_multibyte_characters.
24427 Value is the number of columns displayed. */
24429 static int
24430 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24431 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24432 int field_width, int precision, int max_x, int multibyte)
24434 int hpos_at_start = it->hpos;
24435 int saved_face_id = it->face_id;
24436 struct glyph_row *row = it->glyph_row;
24437 ptrdiff_t it_charpos;
24439 /* Initialize the iterator IT for iteration over STRING beginning
24440 with index START. */
24441 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24442 precision, field_width, multibyte);
24443 if (string && STRINGP (lisp_string))
24444 /* LISP_STRING is the one returned by decode_mode_spec. We should
24445 ignore its text properties. */
24446 it->stop_charpos = it->end_charpos;
24448 /* If displaying STRING, set up the face of the iterator from
24449 FACE_STRING, if that's given. */
24450 if (STRINGP (face_string))
24452 ptrdiff_t endptr;
24453 struct face *face;
24455 it->face_id
24456 = face_at_string_position (it->w, face_string, face_string_pos,
24457 0, &endptr, it->base_face_id, false);
24458 face = FACE_FROM_ID (it->f, it->face_id);
24459 it->face_box_p = face->box != FACE_NO_BOX;
24462 /* Set max_x to the maximum allowed X position. Don't let it go
24463 beyond the right edge of the window. */
24464 if (max_x <= 0)
24465 max_x = it->last_visible_x;
24466 else
24467 max_x = min (max_x, it->last_visible_x);
24469 /* Skip over display elements that are not visible. because IT->w is
24470 hscrolled. */
24471 if (it->current_x < it->first_visible_x)
24472 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24473 MOVE_TO_POS | MOVE_TO_X);
24475 row->ascent = it->max_ascent;
24476 row->height = it->max_ascent + it->max_descent;
24477 row->phys_ascent = it->max_phys_ascent;
24478 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24479 row->extra_line_spacing = it->max_extra_line_spacing;
24481 if (STRINGP (it->string))
24482 it_charpos = IT_STRING_CHARPOS (*it);
24483 else
24484 it_charpos = IT_CHARPOS (*it);
24486 /* This condition is for the case that we are called with current_x
24487 past last_visible_x. */
24488 while (it->current_x < max_x)
24490 int x_before, x, n_glyphs_before, i, nglyphs;
24492 /* Get the next display element. */
24493 if (!get_next_display_element (it))
24494 break;
24496 /* Produce glyphs. */
24497 x_before = it->current_x;
24498 n_glyphs_before = row->used[TEXT_AREA];
24499 PRODUCE_GLYPHS (it);
24501 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24502 i = 0;
24503 x = x_before;
24504 while (i < nglyphs)
24506 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24508 if (it->line_wrap != TRUNCATE
24509 && x + glyph->pixel_width > max_x)
24511 /* End of continued line or max_x reached. */
24512 if (CHAR_GLYPH_PADDING_P (*glyph))
24514 /* A wide character is unbreakable. */
24515 if (row->reversed_p)
24516 unproduce_glyphs (it, row->used[TEXT_AREA]
24517 - n_glyphs_before);
24518 row->used[TEXT_AREA] = n_glyphs_before;
24519 it->current_x = x_before;
24521 else
24523 if (row->reversed_p)
24524 unproduce_glyphs (it, row->used[TEXT_AREA]
24525 - (n_glyphs_before + i));
24526 row->used[TEXT_AREA] = n_glyphs_before + i;
24527 it->current_x = x;
24529 break;
24531 else if (x + glyph->pixel_width >= it->first_visible_x)
24533 /* Glyph is at least partially visible. */
24534 ++it->hpos;
24535 if (x < it->first_visible_x)
24536 row->x = x - it->first_visible_x;
24538 else
24540 /* Glyph is off the left margin of the display area.
24541 Should not happen. */
24542 emacs_abort ();
24545 row->ascent = max (row->ascent, it->max_ascent);
24546 row->height = max (row->height, it->max_ascent + it->max_descent);
24547 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24548 row->phys_height = max (row->phys_height,
24549 it->max_phys_ascent + it->max_phys_descent);
24550 row->extra_line_spacing = max (row->extra_line_spacing,
24551 it->max_extra_line_spacing);
24552 x += glyph->pixel_width;
24553 ++i;
24556 /* Stop if max_x reached. */
24557 if (i < nglyphs)
24558 break;
24560 /* Stop at line ends. */
24561 if (ITERATOR_AT_END_OF_LINE_P (it))
24563 it->continuation_lines_width = 0;
24564 break;
24567 set_iterator_to_next (it, true);
24568 if (STRINGP (it->string))
24569 it_charpos = IT_STRING_CHARPOS (*it);
24570 else
24571 it_charpos = IT_CHARPOS (*it);
24573 /* Stop if truncating at the right edge. */
24574 if (it->line_wrap == TRUNCATE
24575 && it->current_x >= it->last_visible_x)
24577 /* Add truncation mark, but don't do it if the line is
24578 truncated at a padding space. */
24579 if (it_charpos < it->string_nchars)
24581 if (!FRAME_WINDOW_P (it->f))
24583 int ii, n;
24585 if (it->current_x > it->last_visible_x)
24587 if (!row->reversed_p)
24589 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24590 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24591 break;
24593 else
24595 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24596 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24597 break;
24598 unproduce_glyphs (it, ii + 1);
24599 ii = row->used[TEXT_AREA] - (ii + 1);
24601 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24603 row->used[TEXT_AREA] = ii;
24604 produce_special_glyphs (it, IT_TRUNCATION);
24607 produce_special_glyphs (it, IT_TRUNCATION);
24609 row->truncated_on_right_p = true;
24611 break;
24615 /* Maybe insert a truncation at the left. */
24616 if (it->first_visible_x
24617 && it_charpos > 0)
24619 if (!FRAME_WINDOW_P (it->f)
24620 || (row->reversed_p
24621 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24622 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24623 insert_left_trunc_glyphs (it);
24624 row->truncated_on_left_p = true;
24627 it->face_id = saved_face_id;
24629 /* Value is number of columns displayed. */
24630 return it->hpos - hpos_at_start;
24635 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24636 appears as an element of LIST or as the car of an element of LIST.
24637 If PROPVAL is a list, compare each element against LIST in that
24638 way, and return 1/2 if any element of PROPVAL is found in LIST.
24639 Otherwise return 0. This function cannot quit.
24640 The return value is 2 if the text is invisible but with an ellipsis
24641 and 1 if it's invisible and without an ellipsis. */
24644 invisible_prop (Lisp_Object propval, Lisp_Object list)
24646 Lisp_Object tail, proptail;
24648 for (tail = list; CONSP (tail); tail = XCDR (tail))
24650 register Lisp_Object tem;
24651 tem = XCAR (tail);
24652 if (EQ (propval, tem))
24653 return 1;
24654 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24655 return NILP (XCDR (tem)) ? 1 : 2;
24658 if (CONSP (propval))
24660 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24662 Lisp_Object propelt;
24663 propelt = XCAR (proptail);
24664 for (tail = list; CONSP (tail); tail = XCDR (tail))
24666 register Lisp_Object tem;
24667 tem = XCAR (tail);
24668 if (EQ (propelt, tem))
24669 return 1;
24670 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24671 return NILP (XCDR (tem)) ? 1 : 2;
24676 return 0;
24679 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24680 doc: /* Non-nil if the property makes the text invisible.
24681 POS-OR-PROP can be a marker or number, in which case it is taken to be
24682 a position in the current buffer and the value of the `invisible' property
24683 is checked; or it can be some other value, which is then presumed to be the
24684 value of the `invisible' property of the text of interest.
24685 The non-nil value returned can be t for truly invisible text or something
24686 else if the text is replaced by an ellipsis. */)
24687 (Lisp_Object pos_or_prop)
24689 Lisp_Object prop
24690 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24691 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24692 : pos_or_prop);
24693 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24694 return (invis == 0 ? Qnil
24695 : invis == 1 ? Qt
24696 : make_number (invis));
24699 /* Calculate a width or height in pixels from a specification using
24700 the following elements:
24702 SPEC ::=
24703 NUM - a (fractional) multiple of the default font width/height
24704 (NUM) - specifies exactly NUM pixels
24705 UNIT - a fixed number of pixels, see below.
24706 ELEMENT - size of a display element in pixels, see below.
24707 (NUM . SPEC) - equals NUM * SPEC
24708 (+ SPEC SPEC ...) - add pixel values
24709 (- SPEC SPEC ...) - subtract pixel values
24710 (- SPEC) - negate pixel value
24712 NUM ::=
24713 INT or FLOAT - a number constant
24714 SYMBOL - use symbol's (buffer local) variable binding.
24716 UNIT ::=
24717 in - pixels per inch *)
24718 mm - pixels per 1/1000 meter *)
24719 cm - pixels per 1/100 meter *)
24720 width - width of current font in pixels.
24721 height - height of current font in pixels.
24723 *) using the ratio(s) defined in display-pixels-per-inch.
24725 ELEMENT ::=
24727 left-fringe - left fringe width in pixels
24728 right-fringe - right fringe width in pixels
24730 left-margin - left margin width in pixels
24731 right-margin - right margin width in pixels
24733 scroll-bar - scroll-bar area width in pixels
24735 Examples:
24737 Pixels corresponding to 5 inches:
24738 (5 . in)
24740 Total width of non-text areas on left side of window (if scroll-bar is on left):
24741 '(space :width (+ left-fringe left-margin scroll-bar))
24743 Align to first text column (in header line):
24744 '(space :align-to 0)
24746 Align to middle of text area minus half the width of variable `my-image'
24747 containing a loaded image:
24748 '(space :align-to (0.5 . (- text my-image)))
24750 Width of left margin minus width of 1 character in the default font:
24751 '(space :width (- left-margin 1))
24753 Width of left margin minus width of 2 characters in the current font:
24754 '(space :width (- left-margin (2 . width)))
24756 Center 1 character over left-margin (in header line):
24757 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24759 Different ways to express width of left fringe plus left margin minus one pixel:
24760 '(space :width (- (+ left-fringe left-margin) (1)))
24761 '(space :width (+ left-fringe left-margin (- (1))))
24762 '(space :width (+ left-fringe left-margin (-1)))
24766 static bool
24767 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24768 struct font *font, bool width_p, int *align_to)
24770 double pixels;
24772 # define OK_PIXELS(val) (*res = (val), true)
24773 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24775 if (NILP (prop))
24776 return OK_PIXELS (0);
24778 eassert (FRAME_LIVE_P (it->f));
24780 if (SYMBOLP (prop))
24782 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24784 char *unit = SSDATA (SYMBOL_NAME (prop));
24786 if (unit[0] == 'i' && unit[1] == 'n')
24787 pixels = 1.0;
24788 else if (unit[0] == 'm' && unit[1] == 'm')
24789 pixels = 25.4;
24790 else if (unit[0] == 'c' && unit[1] == 'm')
24791 pixels = 2.54;
24792 else
24793 pixels = 0;
24794 if (pixels > 0)
24796 double ppi = (width_p ? FRAME_RES_X (it->f)
24797 : FRAME_RES_Y (it->f));
24799 if (ppi > 0)
24800 return OK_PIXELS (ppi / pixels);
24801 return false;
24805 #ifdef HAVE_WINDOW_SYSTEM
24806 if (EQ (prop, Qheight))
24807 return OK_PIXELS (font
24808 ? normal_char_height (font, -1)
24809 : FRAME_LINE_HEIGHT (it->f));
24810 if (EQ (prop, Qwidth))
24811 return OK_PIXELS (font
24812 ? FONT_WIDTH (font)
24813 : FRAME_COLUMN_WIDTH (it->f));
24814 #else
24815 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24816 return OK_PIXELS (1);
24817 #endif
24819 if (EQ (prop, Qtext))
24820 return OK_PIXELS (width_p
24821 ? window_box_width (it->w, TEXT_AREA)
24822 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24824 if (align_to && *align_to < 0)
24826 *res = 0;
24827 if (EQ (prop, Qleft))
24828 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24829 if (EQ (prop, Qright))
24830 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24831 if (EQ (prop, Qcenter))
24832 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24833 + window_box_width (it->w, TEXT_AREA) / 2);
24834 if (EQ (prop, Qleft_fringe))
24835 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24836 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24837 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24838 if (EQ (prop, Qright_fringe))
24839 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24840 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24841 : window_box_right_offset (it->w, TEXT_AREA));
24842 if (EQ (prop, Qleft_margin))
24843 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24844 if (EQ (prop, Qright_margin))
24845 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24846 if (EQ (prop, Qscroll_bar))
24847 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24849 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24850 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24851 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24852 : 0)));
24854 else
24856 if (EQ (prop, Qleft_fringe))
24857 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24858 if (EQ (prop, Qright_fringe))
24859 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24860 if (EQ (prop, Qleft_margin))
24861 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24862 if (EQ (prop, Qright_margin))
24863 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24864 if (EQ (prop, Qscroll_bar))
24865 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24868 prop = buffer_local_value (prop, it->w->contents);
24869 if (EQ (prop, Qunbound))
24870 prop = Qnil;
24873 if (NUMBERP (prop))
24875 int base_unit = (width_p
24876 ? FRAME_COLUMN_WIDTH (it->f)
24877 : FRAME_LINE_HEIGHT (it->f));
24878 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24881 if (CONSP (prop))
24883 Lisp_Object car = XCAR (prop);
24884 Lisp_Object cdr = XCDR (prop);
24886 if (SYMBOLP (car))
24888 #ifdef HAVE_WINDOW_SYSTEM
24889 if (FRAME_WINDOW_P (it->f)
24890 && valid_image_p (prop))
24892 ptrdiff_t id = lookup_image (it->f, prop);
24893 struct image *img = IMAGE_FROM_ID (it->f, id);
24895 return OK_PIXELS (width_p ? img->width : img->height);
24897 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24899 /* TODO: Don't return dummy size. */
24900 return OK_PIXELS (100);
24902 #endif
24903 if (EQ (car, Qplus) || EQ (car, Qminus))
24905 bool first = true;
24906 double px;
24908 pixels = 0;
24909 while (CONSP (cdr))
24911 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24912 font, width_p, align_to))
24913 return false;
24914 if (first)
24915 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24916 else
24917 pixels += px;
24918 cdr = XCDR (cdr);
24920 if (EQ (car, Qminus))
24921 pixels = -pixels;
24922 return OK_PIXELS (pixels);
24925 car = buffer_local_value (car, it->w->contents);
24926 if (EQ (car, Qunbound))
24927 car = Qnil;
24930 if (NUMBERP (car))
24932 double fact;
24933 pixels = XFLOATINT (car);
24934 if (NILP (cdr))
24935 return OK_PIXELS (pixels);
24936 if (calc_pixel_width_or_height (&fact, it, cdr,
24937 font, width_p, align_to))
24938 return OK_PIXELS (pixels * fact);
24939 return false;
24942 return false;
24945 return false;
24948 void
24949 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24951 #ifdef HAVE_WINDOW_SYSTEM
24952 normal_char_ascent_descent (font, -1, ascent, descent);
24953 #else
24954 *ascent = 1;
24955 *descent = 0;
24956 #endif
24960 /***********************************************************************
24961 Glyph Display
24962 ***********************************************************************/
24964 #ifdef HAVE_WINDOW_SYSTEM
24966 #ifdef GLYPH_DEBUG
24968 void
24969 dump_glyph_string (struct glyph_string *s)
24971 fprintf (stderr, "glyph string\n");
24972 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24973 s->x, s->y, s->width, s->height);
24974 fprintf (stderr, " ybase = %d\n", s->ybase);
24975 fprintf (stderr, " hl = %u\n", s->hl);
24976 fprintf (stderr, " left overhang = %d, right = %d\n",
24977 s->left_overhang, s->right_overhang);
24978 fprintf (stderr, " nchars = %d\n", s->nchars);
24979 fprintf (stderr, " extends to end of line = %d\n",
24980 s->extends_to_end_of_line_p);
24981 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24982 fprintf (stderr, " bg width = %d\n", s->background_width);
24985 #endif /* GLYPH_DEBUG */
24987 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24988 of XChar2b structures for S; it can't be allocated in
24989 init_glyph_string because it must be allocated via `alloca'. W
24990 is the window on which S is drawn. ROW and AREA are the glyph row
24991 and area within the row from which S is constructed. START is the
24992 index of the first glyph structure covered by S. HL is a
24993 face-override for drawing S. */
24995 #ifdef HAVE_NTGUI
24996 #define OPTIONAL_HDC(hdc) HDC hdc,
24997 #define DECLARE_HDC(hdc) HDC hdc;
24998 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24999 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25000 #endif
25002 #ifndef OPTIONAL_HDC
25003 #define OPTIONAL_HDC(hdc)
25004 #define DECLARE_HDC(hdc)
25005 #define ALLOCATE_HDC(hdc, f)
25006 #define RELEASE_HDC(hdc, f)
25007 #endif
25009 static void
25010 init_glyph_string (struct glyph_string *s,
25011 OPTIONAL_HDC (hdc)
25012 XChar2b *char2b, struct window *w, struct glyph_row *row,
25013 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25015 memset (s, 0, sizeof *s);
25016 s->w = w;
25017 s->f = XFRAME (w->frame);
25018 #ifdef HAVE_NTGUI
25019 s->hdc = hdc;
25020 #endif
25021 s->display = FRAME_X_DISPLAY (s->f);
25022 s->char2b = char2b;
25023 s->hl = hl;
25024 s->row = row;
25025 s->area = area;
25026 s->first_glyph = row->glyphs[area] + start;
25027 s->height = row->height;
25028 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25029 s->ybase = s->y + row->ascent;
25033 /* Append the list of glyph strings with head H and tail T to the list
25034 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25036 static void
25037 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25038 struct glyph_string *h, struct glyph_string *t)
25040 if (h)
25042 if (*head)
25043 (*tail)->next = h;
25044 else
25045 *head = h;
25046 h->prev = *tail;
25047 *tail = t;
25052 /* Prepend the list of glyph strings with head H and tail T to the
25053 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25054 result. */
25056 static void
25057 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25058 struct glyph_string *h, struct glyph_string *t)
25060 if (h)
25062 if (*head)
25063 (*head)->prev = t;
25064 else
25065 *tail = t;
25066 t->next = *head;
25067 *head = h;
25072 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25073 Set *HEAD and *TAIL to the resulting list. */
25075 static void
25076 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25077 struct glyph_string *s)
25079 s->next = s->prev = NULL;
25080 append_glyph_string_lists (head, tail, s, s);
25084 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25085 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25086 make sure that X resources for the face returned are allocated.
25087 Value is a pointer to a realized face that is ready for display if
25088 DISPLAY_P. */
25090 static struct face *
25091 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25092 XChar2b *char2b, bool display_p)
25094 struct face *face = FACE_FROM_ID (f, face_id);
25095 unsigned code = 0;
25097 if (face->font)
25099 code = face->font->driver->encode_char (face->font, c);
25101 if (code == FONT_INVALID_CODE)
25102 code = 0;
25104 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25106 /* Make sure X resources of the face are allocated. */
25107 #ifdef HAVE_X_WINDOWS
25108 if (display_p)
25109 #endif
25111 eassert (face != NULL);
25112 prepare_face_for_display (f, face);
25115 return face;
25119 /* Get face and two-byte form of character glyph GLYPH on frame F.
25120 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25121 a pointer to a realized face that is ready for display. */
25123 static struct face *
25124 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25125 XChar2b *char2b)
25127 struct face *face;
25128 unsigned code = 0;
25130 eassert (glyph->type == CHAR_GLYPH);
25131 face = FACE_FROM_ID (f, glyph->face_id);
25133 /* Make sure X resources of the face are allocated. */
25134 prepare_face_for_display (f, face);
25136 if (face->font)
25138 if (CHAR_BYTE8_P (glyph->u.ch))
25139 code = CHAR_TO_BYTE8 (glyph->u.ch);
25140 else
25141 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25143 if (code == FONT_INVALID_CODE)
25144 code = 0;
25147 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25148 return face;
25152 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25153 Return true iff FONT has a glyph for C. */
25155 static bool
25156 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25158 unsigned code;
25160 if (CHAR_BYTE8_P (c))
25161 code = CHAR_TO_BYTE8 (c);
25162 else
25163 code = font->driver->encode_char (font, c);
25165 if (code == FONT_INVALID_CODE)
25166 return false;
25167 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25168 return true;
25172 /* Fill glyph string S with composition components specified by S->cmp.
25174 BASE_FACE is the base face of the composition.
25175 S->cmp_from is the index of the first component for S.
25177 OVERLAPS non-zero means S should draw the foreground only, and use
25178 its physical height for clipping. See also draw_glyphs.
25180 Value is the index of a component not in S. */
25182 static int
25183 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25184 int overlaps)
25186 int i;
25187 /* For all glyphs of this composition, starting at the offset
25188 S->cmp_from, until we reach the end of the definition or encounter a
25189 glyph that requires the different face, add it to S. */
25190 struct face *face;
25192 eassert (s);
25194 s->for_overlaps = overlaps;
25195 s->face = NULL;
25196 s->font = NULL;
25197 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25199 int c = COMPOSITION_GLYPH (s->cmp, i);
25201 /* TAB in a composition means display glyphs with padding space
25202 on the left or right. */
25203 if (c != '\t')
25205 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25206 -1, Qnil);
25208 face = get_char_face_and_encoding (s->f, c, face_id,
25209 s->char2b + i, true);
25210 if (face)
25212 if (! s->face)
25214 s->face = face;
25215 s->font = s->face->font;
25217 else if (s->face != face)
25218 break;
25221 ++s->nchars;
25223 s->cmp_to = i;
25225 if (s->face == NULL)
25227 s->face = base_face->ascii_face;
25228 s->font = s->face->font;
25231 /* All glyph strings for the same composition has the same width,
25232 i.e. the width set for the first component of the composition. */
25233 s->width = s->first_glyph->pixel_width;
25235 /* If the specified font could not be loaded, use the frame's
25236 default font, but record the fact that we couldn't load it in
25237 the glyph string so that we can draw rectangles for the
25238 characters of the glyph string. */
25239 if (s->font == NULL)
25241 s->font_not_found_p = true;
25242 s->font = FRAME_FONT (s->f);
25245 /* Adjust base line for subscript/superscript text. */
25246 s->ybase += s->first_glyph->voffset;
25248 return s->cmp_to;
25251 static int
25252 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25253 int start, int end, int overlaps)
25255 struct glyph *glyph, *last;
25256 Lisp_Object lgstring;
25257 int i;
25259 s->for_overlaps = overlaps;
25260 glyph = s->row->glyphs[s->area] + start;
25261 last = s->row->glyphs[s->area] + end;
25262 s->cmp_id = glyph->u.cmp.id;
25263 s->cmp_from = glyph->slice.cmp.from;
25264 s->cmp_to = glyph->slice.cmp.to + 1;
25265 s->face = FACE_FROM_ID (s->f, face_id);
25266 lgstring = composition_gstring_from_id (s->cmp_id);
25267 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25268 glyph++;
25269 while (glyph < last
25270 && glyph->u.cmp.automatic
25271 && glyph->u.cmp.id == s->cmp_id
25272 && s->cmp_to == glyph->slice.cmp.from)
25273 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25275 for (i = s->cmp_from; i < s->cmp_to; i++)
25277 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25278 unsigned code = LGLYPH_CODE (lglyph);
25280 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25282 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25283 return glyph - s->row->glyphs[s->area];
25287 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25288 See the comment of fill_glyph_string for arguments.
25289 Value is the index of the first glyph not in S. */
25292 static int
25293 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25294 int start, int end, int overlaps)
25296 struct glyph *glyph, *last;
25297 int voffset;
25299 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25300 s->for_overlaps = overlaps;
25301 glyph = s->row->glyphs[s->area] + start;
25302 last = s->row->glyphs[s->area] + end;
25303 voffset = glyph->voffset;
25304 s->face = FACE_FROM_ID (s->f, face_id);
25305 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25306 s->nchars = 1;
25307 s->width = glyph->pixel_width;
25308 glyph++;
25309 while (glyph < last
25310 && glyph->type == GLYPHLESS_GLYPH
25311 && glyph->voffset == voffset
25312 && glyph->face_id == face_id)
25314 s->nchars++;
25315 s->width += glyph->pixel_width;
25316 glyph++;
25318 s->ybase += voffset;
25319 return glyph - s->row->glyphs[s->area];
25323 /* Fill glyph string S from a sequence of character glyphs.
25325 FACE_ID is the face id of the string. START is the index of the
25326 first glyph to consider, END is the index of the last + 1.
25327 OVERLAPS non-zero means S should draw the foreground only, and use
25328 its physical height for clipping. See also draw_glyphs.
25330 Value is the index of the first glyph not in S. */
25332 static int
25333 fill_glyph_string (struct glyph_string *s, int face_id,
25334 int start, int end, int overlaps)
25336 struct glyph *glyph, *last;
25337 int voffset;
25338 bool glyph_not_available_p;
25340 eassert (s->f == XFRAME (s->w->frame));
25341 eassert (s->nchars == 0);
25342 eassert (start >= 0 && end > start);
25344 s->for_overlaps = overlaps;
25345 glyph = s->row->glyphs[s->area] + start;
25346 last = s->row->glyphs[s->area] + end;
25347 voffset = glyph->voffset;
25348 s->padding_p = glyph->padding_p;
25349 glyph_not_available_p = glyph->glyph_not_available_p;
25351 while (glyph < last
25352 && glyph->type == CHAR_GLYPH
25353 && glyph->voffset == voffset
25354 /* Same face id implies same font, nowadays. */
25355 && glyph->face_id == face_id
25356 && glyph->glyph_not_available_p == glyph_not_available_p)
25358 s->face = get_glyph_face_and_encoding (s->f, glyph,
25359 s->char2b + s->nchars);
25360 ++s->nchars;
25361 eassert (s->nchars <= end - start);
25362 s->width += glyph->pixel_width;
25363 if (glyph++->padding_p != s->padding_p)
25364 break;
25367 s->font = s->face->font;
25369 /* If the specified font could not be loaded, use the frame's font,
25370 but record the fact that we couldn't load it in
25371 S->font_not_found_p so that we can draw rectangles for the
25372 characters of the glyph string. */
25373 if (s->font == NULL || glyph_not_available_p)
25375 s->font_not_found_p = true;
25376 s->font = FRAME_FONT (s->f);
25379 /* Adjust base line for subscript/superscript text. */
25380 s->ybase += voffset;
25382 eassert (s->face && s->face->gc);
25383 return glyph - s->row->glyphs[s->area];
25387 /* Fill glyph string S from image glyph S->first_glyph. */
25389 static void
25390 fill_image_glyph_string (struct glyph_string *s)
25392 eassert (s->first_glyph->type == IMAGE_GLYPH);
25393 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25394 eassert (s->img);
25395 s->slice = s->first_glyph->slice.img;
25396 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25397 s->font = s->face->font;
25398 s->width = s->first_glyph->pixel_width;
25400 /* Adjust base line for subscript/superscript text. */
25401 s->ybase += s->first_glyph->voffset;
25405 #ifdef HAVE_XWIDGETS
25406 static void
25407 fill_xwidget_glyph_string (struct glyph_string *s)
25409 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25410 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25411 s->font = s->face->font;
25412 s->width = s->first_glyph->pixel_width;
25413 s->ybase += s->first_glyph->voffset;
25414 s->xwidget = s->first_glyph->u.xwidget;
25416 #endif
25417 /* Fill glyph string S from a sequence of stretch glyphs.
25419 START is the index of the first glyph to consider,
25420 END is the index of the last + 1.
25422 Value is the index of the first glyph not in S. */
25424 static int
25425 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25427 struct glyph *glyph, *last;
25428 int voffset, face_id;
25430 eassert (s->first_glyph->type == STRETCH_GLYPH);
25432 glyph = s->row->glyphs[s->area] + start;
25433 last = s->row->glyphs[s->area] + end;
25434 face_id = glyph->face_id;
25435 s->face = FACE_FROM_ID (s->f, face_id);
25436 s->font = s->face->font;
25437 s->width = glyph->pixel_width;
25438 s->nchars = 1;
25439 voffset = glyph->voffset;
25441 for (++glyph;
25442 (glyph < last
25443 && glyph->type == STRETCH_GLYPH
25444 && glyph->voffset == voffset
25445 && glyph->face_id == face_id);
25446 ++glyph)
25447 s->width += glyph->pixel_width;
25449 /* Adjust base line for subscript/superscript text. */
25450 s->ybase += voffset;
25452 /* The case that face->gc == 0 is handled when drawing the glyph
25453 string by calling prepare_face_for_display. */
25454 eassert (s->face);
25455 return glyph - s->row->glyphs[s->area];
25458 static struct font_metrics *
25459 get_per_char_metric (struct font *font, XChar2b *char2b)
25461 static struct font_metrics metrics;
25462 unsigned code;
25464 if (! font)
25465 return NULL;
25466 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25467 if (code == FONT_INVALID_CODE)
25468 return NULL;
25469 font->driver->text_extents (font, &code, 1, &metrics);
25470 return &metrics;
25473 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25474 for FONT. Values are taken from font-global ones, except for fonts
25475 that claim preposterously large values, but whose glyphs actually
25476 have reasonable dimensions. C is the character to use for metrics
25477 if the font-global values are too large; if C is negative, the
25478 function selects a default character. */
25479 static void
25480 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25482 *ascent = FONT_BASE (font);
25483 *descent = FONT_DESCENT (font);
25485 if (FONT_TOO_HIGH (font))
25487 XChar2b char2b;
25489 /* Get metrics of C, defaulting to a reasonably sized ASCII
25490 character. */
25491 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25493 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25495 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25497 /* We add 1 pixel to character dimensions as heuristics
25498 that produces nicer display, e.g. when the face has
25499 the box attribute. */
25500 *ascent = pcm->ascent + 1;
25501 *descent = pcm->descent + 1;
25507 /* A subroutine that computes a reasonable "normal character height"
25508 for fonts that claim preposterously large vertical dimensions, but
25509 whose glyphs are actually reasonably sized. C is the character
25510 whose metrics to use for those fonts, or -1 for default
25511 character. */
25512 static int
25513 normal_char_height (struct font *font, int c)
25515 int ascent, descent;
25517 normal_char_ascent_descent (font, c, &ascent, &descent);
25519 return ascent + descent;
25522 /* EXPORT for RIF:
25523 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25524 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25525 assumed to be zero. */
25527 void
25528 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25530 *left = *right = 0;
25532 if (glyph->type == CHAR_GLYPH)
25534 XChar2b char2b;
25535 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25536 if (face->font)
25538 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25539 if (pcm)
25541 if (pcm->rbearing > pcm->width)
25542 *right = pcm->rbearing - pcm->width;
25543 if (pcm->lbearing < 0)
25544 *left = -pcm->lbearing;
25548 else if (glyph->type == COMPOSITE_GLYPH)
25550 if (! glyph->u.cmp.automatic)
25552 struct composition *cmp = composition_table[glyph->u.cmp.id];
25554 if (cmp->rbearing > cmp->pixel_width)
25555 *right = cmp->rbearing - cmp->pixel_width;
25556 if (cmp->lbearing < 0)
25557 *left = - cmp->lbearing;
25559 else
25561 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25562 struct font_metrics metrics;
25564 composition_gstring_width (gstring, glyph->slice.cmp.from,
25565 glyph->slice.cmp.to + 1, &metrics);
25566 if (metrics.rbearing > metrics.width)
25567 *right = metrics.rbearing - metrics.width;
25568 if (metrics.lbearing < 0)
25569 *left = - metrics.lbearing;
25575 /* Return the index of the first glyph preceding glyph string S that
25576 is overwritten by S because of S's left overhang. Value is -1
25577 if no glyphs are overwritten. */
25579 static int
25580 left_overwritten (struct glyph_string *s)
25582 int k;
25584 if (s->left_overhang)
25586 int x = 0, i;
25587 struct glyph *glyphs = s->row->glyphs[s->area];
25588 int first = s->first_glyph - glyphs;
25590 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25591 x -= glyphs[i].pixel_width;
25593 k = i + 1;
25595 else
25596 k = -1;
25598 return k;
25602 /* Return the index of the first glyph preceding glyph string S that
25603 is overwriting S because of its right overhang. Value is -1 if no
25604 glyph in front of S overwrites S. */
25606 static int
25607 left_overwriting (struct glyph_string *s)
25609 int i, k, x;
25610 struct glyph *glyphs = s->row->glyphs[s->area];
25611 int first = s->first_glyph - glyphs;
25613 k = -1;
25614 x = 0;
25615 for (i = first - 1; i >= 0; --i)
25617 int left, right;
25618 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25619 if (x + right > 0)
25620 k = i;
25621 x -= glyphs[i].pixel_width;
25624 return k;
25628 /* Return the index of the last glyph following glyph string S that is
25629 overwritten by S because of S's right overhang. Value is -1 if
25630 no such glyph is found. */
25632 static int
25633 right_overwritten (struct glyph_string *s)
25635 int k = -1;
25637 if (s->right_overhang)
25639 int x = 0, i;
25640 struct glyph *glyphs = s->row->glyphs[s->area];
25641 int first = (s->first_glyph - glyphs
25642 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25643 int end = s->row->used[s->area];
25645 for (i = first; i < end && s->right_overhang > x; ++i)
25646 x += glyphs[i].pixel_width;
25648 k = i;
25651 return k;
25655 /* Return the index of the last glyph following glyph string S that
25656 overwrites S because of its left overhang. Value is negative
25657 if no such glyph is found. */
25659 static int
25660 right_overwriting (struct glyph_string *s)
25662 int i, k, x;
25663 int end = s->row->used[s->area];
25664 struct glyph *glyphs = s->row->glyphs[s->area];
25665 int first = (s->first_glyph - glyphs
25666 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25668 k = -1;
25669 x = 0;
25670 for (i = first; i < end; ++i)
25672 int left, right;
25673 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25674 if (x - left < 0)
25675 k = i;
25676 x += glyphs[i].pixel_width;
25679 return k;
25683 /* Set background width of glyph string S. START is the index of the
25684 first glyph following S. LAST_X is the right-most x-position + 1
25685 in the drawing area. */
25687 static void
25688 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25690 /* If the face of this glyph string has to be drawn to the end of
25691 the drawing area, set S->extends_to_end_of_line_p. */
25693 if (start == s->row->used[s->area]
25694 && ((s->row->fill_line_p
25695 && (s->hl == DRAW_NORMAL_TEXT
25696 || s->hl == DRAW_IMAGE_RAISED
25697 || s->hl == DRAW_IMAGE_SUNKEN))
25698 || s->hl == DRAW_MOUSE_FACE))
25699 s->extends_to_end_of_line_p = true;
25701 /* If S extends its face to the end of the line, set its
25702 background_width to the distance to the right edge of the drawing
25703 area. */
25704 if (s->extends_to_end_of_line_p)
25705 s->background_width = last_x - s->x + 1;
25706 else
25707 s->background_width = s->width;
25711 /* Return glyph string that shares background with glyph string S and
25712 whose `background_width' member has been set. */
25714 static struct glyph_string *
25715 glyph_string_containing_background_width (struct glyph_string *s)
25717 if (s->cmp)
25718 while (s->cmp_from)
25719 s = s->prev;
25721 return s;
25725 /* Compute overhangs and x-positions for glyph string S and its
25726 predecessors, or successors. X is the starting x-position for S.
25727 BACKWARD_P means process predecessors. */
25729 static void
25730 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25732 if (backward_p)
25734 while (s)
25736 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25737 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25738 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25739 x -= s->width;
25740 s->x = x;
25741 s = s->prev;
25744 else
25746 while (s)
25748 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25749 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25750 s->x = x;
25751 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25752 x += s->width;
25753 s = s->next;
25760 /* The following macros are only called from draw_glyphs below.
25761 They reference the following parameters of that function directly:
25762 `w', `row', `area', and `overlap_p'
25763 as well as the following local variables:
25764 `s', `f', and `hdc' (in W32) */
25766 #ifdef HAVE_NTGUI
25767 /* On W32, silently add local `hdc' variable to argument list of
25768 init_glyph_string. */
25769 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25770 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25771 #else
25772 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25773 init_glyph_string (s, char2b, w, row, area, start, hl)
25774 #endif
25776 /* Add a glyph string for a stretch glyph to the list of strings
25777 between HEAD and TAIL. START is the index of the stretch glyph in
25778 row area AREA of glyph row ROW. END is the index of the last glyph
25779 in that glyph row area. X is the current output position assigned
25780 to the new glyph string constructed. HL overrides that face of the
25781 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25782 is the right-most x-position of the drawing area. */
25784 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25785 and below -- keep them on one line. */
25786 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25787 do \
25789 s = alloca (sizeof *s); \
25790 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25791 START = fill_stretch_glyph_string (s, START, END); \
25792 append_glyph_string (&HEAD, &TAIL, s); \
25793 s->x = (X); \
25795 while (false)
25798 /* Add a glyph string for an image glyph to the list of strings
25799 between HEAD and TAIL. START is the index of the image glyph in
25800 row area AREA of glyph row ROW. END is the index of the last glyph
25801 in that glyph row area. X is the current output position assigned
25802 to the new glyph string constructed. HL overrides that face of the
25803 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25804 is the right-most x-position of the drawing area. */
25806 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25807 do \
25809 s = alloca (sizeof *s); \
25810 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25811 fill_image_glyph_string (s); \
25812 append_glyph_string (&HEAD, &TAIL, s); \
25813 ++START; \
25814 s->x = (X); \
25816 while (false)
25818 #ifndef HAVE_XWIDGETS
25819 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25820 eassume (false)
25821 #else
25822 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25823 do \
25825 s = alloca (sizeof *s); \
25826 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25827 fill_xwidget_glyph_string (s); \
25828 append_glyph_string (&(HEAD), &(TAIL), s); \
25829 ++(START); \
25830 s->x = (X); \
25832 while (false)
25833 #endif
25835 /* Add a glyph string for a sequence of character glyphs to the list
25836 of strings between HEAD and TAIL. START is the index of the first
25837 glyph in row area AREA of glyph row ROW that is part of the new
25838 glyph string. END is the index of the last glyph in that glyph row
25839 area. X is the current output position assigned to the new glyph
25840 string constructed. HL overrides that face of the glyph; e.g. it
25841 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25842 right-most x-position of the drawing area. */
25844 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25845 do \
25847 int face_id; \
25848 XChar2b *char2b; \
25850 face_id = (row)->glyphs[area][START].face_id; \
25852 s = alloca (sizeof *s); \
25853 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25854 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25855 append_glyph_string (&HEAD, &TAIL, s); \
25856 s->x = (X); \
25857 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25859 while (false)
25862 /* Add a glyph string for a composite sequence to the list of strings
25863 between HEAD and TAIL. START is the index of the first glyph in
25864 row area AREA of glyph row ROW that is part of the new glyph
25865 string. END is the index of the last glyph in that glyph row area.
25866 X is the current output position assigned to the new glyph string
25867 constructed. HL overrides that face of the glyph; e.g. it is
25868 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25869 x-position of the drawing area. */
25871 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25872 do { \
25873 int face_id = (row)->glyphs[area][START].face_id; \
25874 struct face *base_face = FACE_FROM_ID (f, face_id); \
25875 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25876 struct composition *cmp = composition_table[cmp_id]; \
25877 XChar2b *char2b; \
25878 struct glyph_string *first_s = NULL; \
25879 int n; \
25881 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25883 /* Make glyph_strings for each glyph sequence that is drawable by \
25884 the same face, and append them to HEAD/TAIL. */ \
25885 for (n = 0; n < cmp->glyph_len;) \
25887 s = alloca (sizeof *s); \
25888 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25889 append_glyph_string (&(HEAD), &(TAIL), s); \
25890 s->cmp = cmp; \
25891 s->cmp_from = n; \
25892 s->x = (X); \
25893 if (n == 0) \
25894 first_s = s; \
25895 n = fill_composite_glyph_string (s, base_face, overlaps); \
25898 ++START; \
25899 s = first_s; \
25900 } while (false)
25903 /* Add a glyph string for a glyph-string sequence to the list of strings
25904 between HEAD and TAIL. */
25906 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25907 do { \
25908 int face_id; \
25909 XChar2b *char2b; \
25910 Lisp_Object gstring; \
25912 face_id = (row)->glyphs[area][START].face_id; \
25913 gstring = (composition_gstring_from_id \
25914 ((row)->glyphs[area][START].u.cmp.id)); \
25915 s = alloca (sizeof *s); \
25916 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25917 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25918 append_glyph_string (&(HEAD), &(TAIL), s); \
25919 s->x = (X); \
25920 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25921 } while (false)
25924 /* Add a glyph string for a sequence of glyphless character's glyphs
25925 to the list of strings between HEAD and TAIL. The meanings of
25926 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25928 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25929 do \
25931 int face_id; \
25933 face_id = (row)->glyphs[area][START].face_id; \
25935 s = alloca (sizeof *s); \
25936 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25937 append_glyph_string (&HEAD, &TAIL, s); \
25938 s->x = (X); \
25939 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25940 overlaps); \
25942 while (false)
25945 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25946 of AREA of glyph row ROW on window W between indices START and END.
25947 HL overrides the face for drawing glyph strings, e.g. it is
25948 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25949 x-positions of the drawing area.
25951 This is an ugly monster macro construct because we must use alloca
25952 to allocate glyph strings (because draw_glyphs can be called
25953 asynchronously). */
25955 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25956 do \
25958 HEAD = TAIL = NULL; \
25959 while (START < END) \
25961 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25962 switch (first_glyph->type) \
25964 case CHAR_GLYPH: \
25965 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25966 HL, X, LAST_X); \
25967 break; \
25969 case COMPOSITE_GLYPH: \
25970 if (first_glyph->u.cmp.automatic) \
25971 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25972 HL, X, LAST_X); \
25973 else \
25974 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25975 HL, X, LAST_X); \
25976 break; \
25978 case STRETCH_GLYPH: \
25979 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25980 HL, X, LAST_X); \
25981 break; \
25983 case IMAGE_GLYPH: \
25984 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25985 HL, X, LAST_X); \
25986 break;
25988 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25989 case XWIDGET_GLYPH: \
25990 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25991 HL, X, LAST_X); \
25992 break;
25994 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25995 case GLYPHLESS_GLYPH: \
25996 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25997 HL, X, LAST_X); \
25998 break; \
26000 default: \
26001 emacs_abort (); \
26004 if (s) \
26006 set_glyph_string_background_width (s, START, LAST_X); \
26007 (X) += s->width; \
26010 } while (false)
26013 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26014 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26015 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26016 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26019 /* Draw glyphs between START and END in AREA of ROW on window W,
26020 starting at x-position X. X is relative to AREA in W. HL is a
26021 face-override with the following meaning:
26023 DRAW_NORMAL_TEXT draw normally
26024 DRAW_CURSOR draw in cursor face
26025 DRAW_MOUSE_FACE draw in mouse face.
26026 DRAW_INVERSE_VIDEO draw in mode line face
26027 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26028 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26030 If OVERLAPS is non-zero, draw only the foreground of characters and
26031 clip to the physical height of ROW. Non-zero value also defines
26032 the overlapping part to be drawn:
26034 OVERLAPS_PRED overlap with preceding rows
26035 OVERLAPS_SUCC overlap with succeeding rows
26036 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26037 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26039 Value is the x-position reached, relative to AREA of W. */
26041 static int
26042 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26043 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26044 enum draw_glyphs_face hl, int overlaps)
26046 struct glyph_string *head, *tail;
26047 struct glyph_string *s;
26048 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26049 int i, j, x_reached, last_x, area_left = 0;
26050 struct frame *f = XFRAME (WINDOW_FRAME (w));
26051 DECLARE_HDC (hdc);
26053 ALLOCATE_HDC (hdc, f);
26055 /* Let's rather be paranoid than getting a SEGV. */
26056 end = min (end, row->used[area]);
26057 start = clip_to_bounds (0, start, end);
26059 /* Translate X to frame coordinates. Set last_x to the right
26060 end of the drawing area. */
26061 if (row->full_width_p)
26063 /* X is relative to the left edge of W, without scroll bars
26064 or fringes. */
26065 area_left = WINDOW_LEFT_EDGE_X (w);
26066 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26067 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26069 else
26071 area_left = window_box_left (w, area);
26072 last_x = area_left + window_box_width (w, area);
26074 x += area_left;
26076 /* Build a doubly-linked list of glyph_string structures between
26077 head and tail from what we have to draw. Note that the macro
26078 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26079 the reason we use a separate variable `i'. */
26080 i = start;
26081 USE_SAFE_ALLOCA;
26082 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26083 if (tail)
26085 s = glyph_string_containing_background_width (tail);
26086 x_reached = s->x + s->background_width;
26088 else
26089 x_reached = x;
26091 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26092 the row, redraw some glyphs in front or following the glyph
26093 strings built above. */
26094 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26096 struct glyph_string *h, *t;
26097 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26098 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26099 bool check_mouse_face = false;
26100 int dummy_x = 0;
26102 /* If mouse highlighting is on, we may need to draw adjacent
26103 glyphs using mouse-face highlighting. */
26104 if (area == TEXT_AREA && row->mouse_face_p
26105 && hlinfo->mouse_face_beg_row >= 0
26106 && hlinfo->mouse_face_end_row >= 0)
26108 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26110 if (row_vpos >= hlinfo->mouse_face_beg_row
26111 && row_vpos <= hlinfo->mouse_face_end_row)
26113 check_mouse_face = true;
26114 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26115 ? hlinfo->mouse_face_beg_col : 0;
26116 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26117 ? hlinfo->mouse_face_end_col
26118 : row->used[TEXT_AREA];
26122 /* Compute overhangs for all glyph strings. */
26123 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26124 for (s = head; s; s = s->next)
26125 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26127 /* Prepend glyph strings for glyphs in front of the first glyph
26128 string that are overwritten because of the first glyph
26129 string's left overhang. The background of all strings
26130 prepended must be drawn because the first glyph string
26131 draws over it. */
26132 i = left_overwritten (head);
26133 if (i >= 0)
26135 enum draw_glyphs_face overlap_hl;
26137 /* If this row contains mouse highlighting, attempt to draw
26138 the overlapped glyphs with the correct highlight. This
26139 code fails if the overlap encompasses more than one glyph
26140 and mouse-highlight spans only some of these glyphs.
26141 However, making it work perfectly involves a lot more
26142 code, and I don't know if the pathological case occurs in
26143 practice, so we'll stick to this for now. --- cyd */
26144 if (check_mouse_face
26145 && mouse_beg_col < start && mouse_end_col > i)
26146 overlap_hl = DRAW_MOUSE_FACE;
26147 else
26148 overlap_hl = DRAW_NORMAL_TEXT;
26150 if (hl != overlap_hl)
26151 clip_head = head;
26152 j = i;
26153 BUILD_GLYPH_STRINGS (j, start, h, t,
26154 overlap_hl, dummy_x, last_x);
26155 start = i;
26156 compute_overhangs_and_x (t, head->x, true);
26157 prepend_glyph_string_lists (&head, &tail, h, t);
26158 if (clip_head == NULL)
26159 clip_head = head;
26162 /* Prepend glyph strings for glyphs in front of the first glyph
26163 string that overwrite that glyph string because of their
26164 right overhang. For these strings, only the foreground must
26165 be drawn, because it draws over the glyph string at `head'.
26166 The background must not be drawn because this would overwrite
26167 right overhangs of preceding glyphs for which no glyph
26168 strings exist. */
26169 i = left_overwriting (head);
26170 if (i >= 0)
26172 enum draw_glyphs_face overlap_hl;
26174 if (check_mouse_face
26175 && mouse_beg_col < start && mouse_end_col > i)
26176 overlap_hl = DRAW_MOUSE_FACE;
26177 else
26178 overlap_hl = DRAW_NORMAL_TEXT;
26180 if (hl == overlap_hl || clip_head == NULL)
26181 clip_head = head;
26182 BUILD_GLYPH_STRINGS (i, start, h, t,
26183 overlap_hl, dummy_x, last_x);
26184 for (s = h; s; s = s->next)
26185 s->background_filled_p = true;
26186 compute_overhangs_and_x (t, head->x, true);
26187 prepend_glyph_string_lists (&head, &tail, h, t);
26190 /* Append glyphs strings for glyphs following the last glyph
26191 string tail that are overwritten by tail. The background of
26192 these strings has to be drawn because tail's foreground draws
26193 over it. */
26194 i = right_overwritten (tail);
26195 if (i >= 0)
26197 enum draw_glyphs_face overlap_hl;
26199 if (check_mouse_face
26200 && mouse_beg_col < i && mouse_end_col > end)
26201 overlap_hl = DRAW_MOUSE_FACE;
26202 else
26203 overlap_hl = DRAW_NORMAL_TEXT;
26205 if (hl != overlap_hl)
26206 clip_tail = tail;
26207 BUILD_GLYPH_STRINGS (end, i, h, t,
26208 overlap_hl, x, last_x);
26209 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26210 we don't have `end = i;' here. */
26211 compute_overhangs_and_x (h, tail->x + tail->width, false);
26212 append_glyph_string_lists (&head, &tail, h, t);
26213 if (clip_tail == NULL)
26214 clip_tail = tail;
26217 /* Append glyph strings for glyphs following the last glyph
26218 string tail that overwrite tail. The foreground of such
26219 glyphs has to be drawn because it writes into the background
26220 of tail. The background must not be drawn because it could
26221 paint over the foreground of following glyphs. */
26222 i = right_overwriting (tail);
26223 if (i >= 0)
26225 enum draw_glyphs_face overlap_hl;
26226 if (check_mouse_face
26227 && mouse_beg_col < i && mouse_end_col > end)
26228 overlap_hl = DRAW_MOUSE_FACE;
26229 else
26230 overlap_hl = DRAW_NORMAL_TEXT;
26232 if (hl == overlap_hl || clip_tail == NULL)
26233 clip_tail = tail;
26234 i++; /* We must include the Ith glyph. */
26235 BUILD_GLYPH_STRINGS (end, i, h, t,
26236 overlap_hl, x, last_x);
26237 for (s = h; s; s = s->next)
26238 s->background_filled_p = true;
26239 compute_overhangs_and_x (h, tail->x + tail->width, false);
26240 append_glyph_string_lists (&head, &tail, h, t);
26242 tail = glyph_string_containing_background_width (tail);
26243 if (clip_tail)
26244 clip_tail = glyph_string_containing_background_width (clip_tail);
26245 if (clip_head || clip_tail)
26246 for (s = head; s; s = s->next)
26248 s->clip_head = clip_head;
26249 s->clip_tail = clip_tail;
26253 /* Draw all strings. */
26254 for (s = head; s; s = s->next)
26255 FRAME_RIF (f)->draw_glyph_string (s);
26257 #ifndef HAVE_NS
26258 /* When focus a sole frame and move horizontally, this clears on_p
26259 causing a failure to erase prev cursor position. */
26260 if (area == TEXT_AREA
26261 && !row->full_width_p
26262 /* When drawing overlapping rows, only the glyph strings'
26263 foreground is drawn, which doesn't erase a cursor
26264 completely. */
26265 && !overlaps)
26267 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26268 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26269 : (tail ? tail->x + tail->background_width : x));
26270 x0 -= area_left;
26271 x1 -= area_left;
26273 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26274 row->y, MATRIX_ROW_BOTTOM_Y (row));
26276 #endif
26278 /* Value is the x-position up to which drawn, relative to AREA of W.
26279 This doesn't include parts drawn because of overhangs. */
26280 if (row->full_width_p)
26281 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26282 else
26283 x_reached -= area_left;
26285 RELEASE_HDC (hdc, f);
26287 SAFE_FREE ();
26288 return x_reached;
26291 /* Find the first glyph in the run of underlined glyphs preceding the
26292 beginning of glyph string S, and return its font (which could be
26293 NULL). This is needed because that font determines the underline
26294 position and thickness for the entire run of the underlined glyphs.
26295 This function is called from the draw_glyph_string method of GUI
26296 frame's redisplay interface (RIF) when it needs to draw in an
26297 underlined face. */
26298 struct font *
26299 font_for_underline_metrics (struct glyph_string *s)
26301 struct glyph *g0 = s->row->glyphs[s->area], *g;
26303 for (g = s->first_glyph - 1; g >= g0; g--)
26305 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26306 if (!(prev_face && prev_face->underline_p))
26307 break;
26310 /* If preceding glyphs are not underlined, use the font of S. */
26311 if (g == s->first_glyph - 1)
26312 return s->font;
26313 else
26315 /* Otherwise use the font of the last glyph we saw in the above
26316 loop whose face had the underline_p flag set. */
26317 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26321 /* Expand row matrix if too narrow. Don't expand if area
26322 is not present. */
26324 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26326 if (!it->f->fonts_changed \
26327 && (it->glyph_row->glyphs[area] \
26328 < it->glyph_row->glyphs[area + 1])) \
26330 it->w->ncols_scale_factor++; \
26331 it->f->fonts_changed = true; \
26335 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26336 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26338 static void
26339 append_glyph (struct it *it)
26341 struct glyph *glyph;
26342 enum glyph_row_area area = it->area;
26344 eassert (it->glyph_row);
26345 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26347 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26348 if (glyph < it->glyph_row->glyphs[area + 1])
26350 /* If the glyph row is reversed, we need to prepend the glyph
26351 rather than append it. */
26352 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26354 struct glyph *g;
26356 /* Make room for the additional glyph. */
26357 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26358 g[1] = *g;
26359 glyph = it->glyph_row->glyphs[area];
26361 glyph->charpos = CHARPOS (it->position);
26362 glyph->object = it->object;
26363 if (it->pixel_width > 0)
26365 eassert (it->pixel_width <= SHRT_MAX);
26366 glyph->pixel_width = it->pixel_width;
26367 glyph->padding_p = false;
26369 else
26371 /* Assure at least 1-pixel width. Otherwise, cursor can't
26372 be displayed correctly. */
26373 glyph->pixel_width = 1;
26374 glyph->padding_p = true;
26376 glyph->ascent = it->ascent;
26377 glyph->descent = it->descent;
26378 glyph->voffset = it->voffset;
26379 glyph->type = CHAR_GLYPH;
26380 glyph->avoid_cursor_p = it->avoid_cursor_p;
26381 glyph->multibyte_p = it->multibyte_p;
26382 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26384 /* In R2L rows, the left and the right box edges need to be
26385 drawn in reverse direction. */
26386 glyph->right_box_line_p = it->start_of_box_run_p;
26387 glyph->left_box_line_p = it->end_of_box_run_p;
26389 else
26391 glyph->left_box_line_p = it->start_of_box_run_p;
26392 glyph->right_box_line_p = it->end_of_box_run_p;
26394 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26395 || it->phys_descent > it->descent);
26396 glyph->glyph_not_available_p = it->glyph_not_available_p;
26397 glyph->face_id = it->face_id;
26398 glyph->u.ch = it->char_to_display;
26399 glyph->slice.img = null_glyph_slice;
26400 glyph->font_type = FONT_TYPE_UNKNOWN;
26401 if (it->bidi_p)
26403 glyph->resolved_level = it->bidi_it.resolved_level;
26404 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26405 glyph->bidi_type = it->bidi_it.type;
26407 else
26409 glyph->resolved_level = 0;
26410 glyph->bidi_type = UNKNOWN_BT;
26412 ++it->glyph_row->used[area];
26414 else
26415 IT_EXPAND_MATRIX_WIDTH (it, area);
26418 /* Store one glyph for the composition IT->cmp_it.id in
26419 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26420 non-null. */
26422 static void
26423 append_composite_glyph (struct it *it)
26425 struct glyph *glyph;
26426 enum glyph_row_area area = it->area;
26428 eassert (it->glyph_row);
26430 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26431 if (glyph < it->glyph_row->glyphs[area + 1])
26433 /* If the glyph row is reversed, we need to prepend the glyph
26434 rather than append it. */
26435 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26437 struct glyph *g;
26439 /* Make room for the new glyph. */
26440 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26441 g[1] = *g;
26442 glyph = it->glyph_row->glyphs[it->area];
26444 glyph->charpos = it->cmp_it.charpos;
26445 glyph->object = it->object;
26446 eassert (it->pixel_width <= SHRT_MAX);
26447 glyph->pixel_width = it->pixel_width;
26448 glyph->ascent = it->ascent;
26449 glyph->descent = it->descent;
26450 glyph->voffset = it->voffset;
26451 glyph->type = COMPOSITE_GLYPH;
26452 if (it->cmp_it.ch < 0)
26454 glyph->u.cmp.automatic = false;
26455 glyph->u.cmp.id = it->cmp_it.id;
26456 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26458 else
26460 glyph->u.cmp.automatic = true;
26461 glyph->u.cmp.id = it->cmp_it.id;
26462 glyph->slice.cmp.from = it->cmp_it.from;
26463 glyph->slice.cmp.to = it->cmp_it.to - 1;
26465 glyph->avoid_cursor_p = it->avoid_cursor_p;
26466 glyph->multibyte_p = it->multibyte_p;
26467 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26469 /* In R2L rows, the left and the right box edges need to be
26470 drawn in reverse direction. */
26471 glyph->right_box_line_p = it->start_of_box_run_p;
26472 glyph->left_box_line_p = it->end_of_box_run_p;
26474 else
26476 glyph->left_box_line_p = it->start_of_box_run_p;
26477 glyph->right_box_line_p = it->end_of_box_run_p;
26479 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26480 || it->phys_descent > it->descent);
26481 glyph->padding_p = false;
26482 glyph->glyph_not_available_p = false;
26483 glyph->face_id = it->face_id;
26484 glyph->font_type = FONT_TYPE_UNKNOWN;
26485 if (it->bidi_p)
26487 glyph->resolved_level = it->bidi_it.resolved_level;
26488 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26489 glyph->bidi_type = it->bidi_it.type;
26491 ++it->glyph_row->used[area];
26493 else
26494 IT_EXPAND_MATRIX_WIDTH (it, area);
26498 /* Change IT->ascent and IT->height according to the setting of
26499 IT->voffset. */
26501 static void
26502 take_vertical_position_into_account (struct it *it)
26504 if (it->voffset)
26506 if (it->voffset < 0)
26507 /* Increase the ascent so that we can display the text higher
26508 in the line. */
26509 it->ascent -= it->voffset;
26510 else
26511 /* Increase the descent so that we can display the text lower
26512 in the line. */
26513 it->descent += it->voffset;
26518 /* Produce glyphs/get display metrics for the image IT is loaded with.
26519 See the description of struct display_iterator in dispextern.h for
26520 an overview of struct display_iterator. */
26522 static void
26523 produce_image_glyph (struct it *it)
26525 struct image *img;
26526 struct face *face;
26527 int glyph_ascent, crop;
26528 struct glyph_slice slice;
26530 eassert (it->what == IT_IMAGE);
26532 face = FACE_FROM_ID (it->f, it->face_id);
26533 /* Make sure X resources of the face is loaded. */
26534 prepare_face_for_display (it->f, face);
26536 if (it->image_id < 0)
26538 /* Fringe bitmap. */
26539 it->ascent = it->phys_ascent = 0;
26540 it->descent = it->phys_descent = 0;
26541 it->pixel_width = 0;
26542 it->nglyphs = 0;
26543 return;
26546 img = IMAGE_FROM_ID (it->f, it->image_id);
26547 /* Make sure X resources of the image is loaded. */
26548 prepare_image_for_display (it->f, img);
26550 slice.x = slice.y = 0;
26551 slice.width = img->width;
26552 slice.height = img->height;
26554 if (INTEGERP (it->slice.x))
26555 slice.x = XINT (it->slice.x);
26556 else if (FLOATP (it->slice.x))
26557 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26559 if (INTEGERP (it->slice.y))
26560 slice.y = XINT (it->slice.y);
26561 else if (FLOATP (it->slice.y))
26562 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26564 if (INTEGERP (it->slice.width))
26565 slice.width = XINT (it->slice.width);
26566 else if (FLOATP (it->slice.width))
26567 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26569 if (INTEGERP (it->slice.height))
26570 slice.height = XINT (it->slice.height);
26571 else if (FLOATP (it->slice.height))
26572 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26574 if (slice.x >= img->width)
26575 slice.x = img->width;
26576 if (slice.y >= img->height)
26577 slice.y = img->height;
26578 if (slice.x + slice.width >= img->width)
26579 slice.width = img->width - slice.x;
26580 if (slice.y + slice.height > img->height)
26581 slice.height = img->height - slice.y;
26583 if (slice.width == 0 || slice.height == 0)
26584 return;
26586 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26588 it->descent = slice.height - glyph_ascent;
26589 if (slice.y == 0)
26590 it->descent += img->vmargin;
26591 if (slice.y + slice.height == img->height)
26592 it->descent += img->vmargin;
26593 it->phys_descent = it->descent;
26595 it->pixel_width = slice.width;
26596 if (slice.x == 0)
26597 it->pixel_width += img->hmargin;
26598 if (slice.x + slice.width == img->width)
26599 it->pixel_width += img->hmargin;
26601 /* It's quite possible for images to have an ascent greater than
26602 their height, so don't get confused in that case. */
26603 if (it->descent < 0)
26604 it->descent = 0;
26606 it->nglyphs = 1;
26608 if (face->box != FACE_NO_BOX)
26610 if (face->box_line_width > 0)
26612 if (slice.y == 0)
26613 it->ascent += face->box_line_width;
26614 if (slice.y + slice.height == img->height)
26615 it->descent += face->box_line_width;
26618 if (it->start_of_box_run_p && slice.x == 0)
26619 it->pixel_width += eabs (face->box_line_width);
26620 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26621 it->pixel_width += eabs (face->box_line_width);
26624 take_vertical_position_into_account (it);
26626 /* Automatically crop wide image glyphs at right edge so we can
26627 draw the cursor on same display row. */
26628 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26629 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26631 it->pixel_width -= crop;
26632 slice.width -= crop;
26635 if (it->glyph_row)
26637 struct glyph *glyph;
26638 enum glyph_row_area area = it->area;
26640 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26641 if (it->glyph_row->reversed_p)
26643 struct glyph *g;
26645 /* Make room for the new glyph. */
26646 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26647 g[1] = *g;
26648 glyph = it->glyph_row->glyphs[it->area];
26650 if (glyph < it->glyph_row->glyphs[area + 1])
26652 glyph->charpos = CHARPOS (it->position);
26653 glyph->object = it->object;
26654 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26655 glyph->ascent = glyph_ascent;
26656 glyph->descent = it->descent;
26657 glyph->voffset = it->voffset;
26658 glyph->type = IMAGE_GLYPH;
26659 glyph->avoid_cursor_p = it->avoid_cursor_p;
26660 glyph->multibyte_p = it->multibyte_p;
26661 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26663 /* In R2L rows, the left and the right box edges need to be
26664 drawn in reverse direction. */
26665 glyph->right_box_line_p = it->start_of_box_run_p;
26666 glyph->left_box_line_p = it->end_of_box_run_p;
26668 else
26670 glyph->left_box_line_p = it->start_of_box_run_p;
26671 glyph->right_box_line_p = it->end_of_box_run_p;
26673 glyph->overlaps_vertically_p = false;
26674 glyph->padding_p = false;
26675 glyph->glyph_not_available_p = false;
26676 glyph->face_id = it->face_id;
26677 glyph->u.img_id = img->id;
26678 glyph->slice.img = slice;
26679 glyph->font_type = FONT_TYPE_UNKNOWN;
26680 if (it->bidi_p)
26682 glyph->resolved_level = it->bidi_it.resolved_level;
26683 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26684 glyph->bidi_type = it->bidi_it.type;
26686 ++it->glyph_row->used[area];
26688 else
26689 IT_EXPAND_MATRIX_WIDTH (it, area);
26693 static void
26694 produce_xwidget_glyph (struct it *it)
26696 #ifdef HAVE_XWIDGETS
26697 struct xwidget *xw;
26698 int glyph_ascent, crop;
26699 eassert (it->what == IT_XWIDGET);
26701 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26702 /* Make sure X resources of the face is loaded. */
26703 prepare_face_for_display (it->f, face);
26705 xw = it->xwidget;
26706 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26707 it->descent = xw->height/2;
26708 it->phys_descent = it->descent;
26709 it->pixel_width = xw->width;
26710 /* It's quite possible for images to have an ascent greater than
26711 their height, so don't get confused in that case. */
26712 if (it->descent < 0)
26713 it->descent = 0;
26715 it->nglyphs = 1;
26717 if (face->box != FACE_NO_BOX)
26719 if (face->box_line_width > 0)
26721 it->ascent += face->box_line_width;
26722 it->descent += face->box_line_width;
26725 if (it->start_of_box_run_p)
26726 it->pixel_width += eabs (face->box_line_width);
26727 it->pixel_width += eabs (face->box_line_width);
26730 take_vertical_position_into_account (it);
26732 /* Automatically crop wide image glyphs at right edge so we can
26733 draw the cursor on same display row. */
26734 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26735 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26736 it->pixel_width -= crop;
26738 if (it->glyph_row)
26740 enum glyph_row_area area = it->area;
26741 struct glyph *glyph
26742 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26744 if (it->glyph_row->reversed_p)
26746 struct glyph *g;
26748 /* Make room for the new glyph. */
26749 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26750 g[1] = *g;
26751 glyph = it->glyph_row->glyphs[it->area];
26753 if (glyph < it->glyph_row->glyphs[area + 1])
26755 glyph->charpos = CHARPOS (it->position);
26756 glyph->object = it->object;
26757 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26758 glyph->ascent = glyph_ascent;
26759 glyph->descent = it->descent;
26760 glyph->voffset = it->voffset;
26761 glyph->type = XWIDGET_GLYPH;
26762 glyph->avoid_cursor_p = it->avoid_cursor_p;
26763 glyph->multibyte_p = it->multibyte_p;
26764 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26766 /* In R2L rows, the left and the right box edges need to be
26767 drawn in reverse direction. */
26768 glyph->right_box_line_p = it->start_of_box_run_p;
26769 glyph->left_box_line_p = it->end_of_box_run_p;
26771 else
26773 glyph->left_box_line_p = it->start_of_box_run_p;
26774 glyph->right_box_line_p = it->end_of_box_run_p;
26776 glyph->overlaps_vertically_p = 0;
26777 glyph->padding_p = 0;
26778 glyph->glyph_not_available_p = 0;
26779 glyph->face_id = it->face_id;
26780 glyph->u.xwidget = it->xwidget;
26781 glyph->font_type = FONT_TYPE_UNKNOWN;
26782 if (it->bidi_p)
26784 glyph->resolved_level = it->bidi_it.resolved_level;
26785 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26786 glyph->bidi_type = it->bidi_it.type;
26788 ++it->glyph_row->used[area];
26790 else
26791 IT_EXPAND_MATRIX_WIDTH (it, area);
26793 #endif
26796 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26797 of the glyph, WIDTH and HEIGHT are the width and height of the
26798 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26800 static void
26801 append_stretch_glyph (struct it *it, Lisp_Object object,
26802 int width, int height, int ascent)
26804 struct glyph *glyph;
26805 enum glyph_row_area area = it->area;
26807 eassert (ascent >= 0 && ascent <= height);
26809 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26810 if (glyph < it->glyph_row->glyphs[area + 1])
26812 /* If the glyph row is reversed, we need to prepend the glyph
26813 rather than append it. */
26814 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26816 struct glyph *g;
26818 /* Make room for the additional glyph. */
26819 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26820 g[1] = *g;
26821 glyph = it->glyph_row->glyphs[area];
26823 /* Decrease the width of the first glyph of the row that
26824 begins before first_visible_x (e.g., due to hscroll).
26825 This is so the overall width of the row becomes smaller
26826 by the scroll amount, and the stretch glyph appended by
26827 extend_face_to_end_of_line will be wider, to shift the
26828 row glyphs to the right. (In L2R rows, the corresponding
26829 left-shift effect is accomplished by setting row->x to a
26830 negative value, which won't work with R2L rows.)
26832 This must leave us with a positive value of WIDTH, since
26833 otherwise the call to move_it_in_display_line_to at the
26834 beginning of display_line would have got past the entire
26835 first glyph, and then it->current_x would have been
26836 greater or equal to it->first_visible_x. */
26837 if (it->current_x < it->first_visible_x)
26838 width -= it->first_visible_x - it->current_x;
26839 eassert (width > 0);
26841 glyph->charpos = CHARPOS (it->position);
26842 glyph->object = object;
26843 /* FIXME: It would be better to use TYPE_MAX here, but
26844 __typeof__ is not portable enough... */
26845 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26846 glyph->ascent = ascent;
26847 glyph->descent = height - ascent;
26848 glyph->voffset = it->voffset;
26849 glyph->type = STRETCH_GLYPH;
26850 glyph->avoid_cursor_p = it->avoid_cursor_p;
26851 glyph->multibyte_p = it->multibyte_p;
26852 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26854 /* In R2L rows, the left and the right box edges need to be
26855 drawn in reverse direction. */
26856 glyph->right_box_line_p = it->start_of_box_run_p;
26857 glyph->left_box_line_p = it->end_of_box_run_p;
26859 else
26861 glyph->left_box_line_p = it->start_of_box_run_p;
26862 glyph->right_box_line_p = it->end_of_box_run_p;
26864 glyph->overlaps_vertically_p = false;
26865 glyph->padding_p = false;
26866 glyph->glyph_not_available_p = false;
26867 glyph->face_id = it->face_id;
26868 glyph->u.stretch.ascent = ascent;
26869 glyph->u.stretch.height = height;
26870 glyph->slice.img = null_glyph_slice;
26871 glyph->font_type = FONT_TYPE_UNKNOWN;
26872 if (it->bidi_p)
26874 glyph->resolved_level = it->bidi_it.resolved_level;
26875 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26876 glyph->bidi_type = it->bidi_it.type;
26878 else
26880 glyph->resolved_level = 0;
26881 glyph->bidi_type = UNKNOWN_BT;
26883 ++it->glyph_row->used[area];
26885 else
26886 IT_EXPAND_MATRIX_WIDTH (it, area);
26889 #endif /* HAVE_WINDOW_SYSTEM */
26891 /* Produce a stretch glyph for iterator IT. IT->object is the value
26892 of the glyph property displayed. The value must be a list
26893 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26894 being recognized:
26896 1. `:width WIDTH' specifies that the space should be WIDTH *
26897 canonical char width wide. WIDTH may be an integer or floating
26898 point number.
26900 2. `:relative-width FACTOR' specifies that the width of the stretch
26901 should be computed from the width of the first character having the
26902 `glyph' property, and should be FACTOR times that width.
26904 3. `:align-to HPOS' specifies that the space should be wide enough
26905 to reach HPOS, a value in canonical character units.
26907 Exactly one of the above pairs must be present.
26909 4. `:height HEIGHT' specifies that the height of the stretch produced
26910 should be HEIGHT, measured in canonical character units.
26912 5. `:relative-height FACTOR' specifies that the height of the
26913 stretch should be FACTOR times the height of the characters having
26914 the glyph property.
26916 Either none or exactly one of 4 or 5 must be present.
26918 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26919 of the stretch should be used for the ascent of the stretch.
26920 ASCENT must be in the range 0 <= ASCENT <= 100. */
26922 void
26923 produce_stretch_glyph (struct it *it)
26925 /* (space :width WIDTH :height HEIGHT ...) */
26926 Lisp_Object prop, plist;
26927 int width = 0, height = 0, align_to = -1;
26928 bool zero_width_ok_p = false;
26929 double tem;
26930 struct font *font = NULL;
26932 #ifdef HAVE_WINDOW_SYSTEM
26933 int ascent = 0;
26934 bool zero_height_ok_p = false;
26936 if (FRAME_WINDOW_P (it->f))
26938 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26939 font = face->font ? face->font : FRAME_FONT (it->f);
26940 prepare_face_for_display (it->f, face);
26942 #endif
26944 /* List should start with `space'. */
26945 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26946 plist = XCDR (it->object);
26948 /* Compute the width of the stretch. */
26949 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26950 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26952 /* Absolute width `:width WIDTH' specified and valid. */
26953 zero_width_ok_p = true;
26954 width = (int)tem;
26956 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26958 /* Relative width `:relative-width FACTOR' specified and valid.
26959 Compute the width of the characters having the `glyph'
26960 property. */
26961 struct it it2;
26962 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26964 it2 = *it;
26965 if (it->multibyte_p)
26966 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26967 else
26969 it2.c = it2.char_to_display = *p, it2.len = 1;
26970 if (! ASCII_CHAR_P (it2.c))
26971 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26974 it2.glyph_row = NULL;
26975 it2.what = IT_CHARACTER;
26976 PRODUCE_GLYPHS (&it2);
26977 width = NUMVAL (prop) * it2.pixel_width;
26979 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26980 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26981 &align_to))
26983 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26984 align_to = (align_to < 0
26986 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26987 else if (align_to < 0)
26988 align_to = window_box_left_offset (it->w, TEXT_AREA);
26989 width = max (0, (int)tem + align_to - it->current_x);
26990 zero_width_ok_p = true;
26992 else
26993 /* Nothing specified -> width defaults to canonical char width. */
26994 width = FRAME_COLUMN_WIDTH (it->f);
26996 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26997 width = 1;
26999 #ifdef HAVE_WINDOW_SYSTEM
27000 /* Compute height. */
27001 if (FRAME_WINDOW_P (it->f))
27003 int default_height = normal_char_height (font, ' ');
27005 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27006 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27008 height = (int)tem;
27009 zero_height_ok_p = true;
27011 else if (prop = Fplist_get (plist, QCrelative_height),
27012 NUMVAL (prop) > 0)
27013 height = default_height * NUMVAL (prop);
27014 else
27015 height = default_height;
27017 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27018 height = 1;
27020 /* Compute percentage of height used for ascent. If
27021 `:ascent ASCENT' is present and valid, use that. Otherwise,
27022 derive the ascent from the font in use. */
27023 if (prop = Fplist_get (plist, QCascent),
27024 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27025 ascent = height * NUMVAL (prop) / 100.0;
27026 else if (!NILP (prop)
27027 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27028 ascent = min (max (0, (int)tem), height);
27029 else
27030 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27032 else
27033 #endif /* HAVE_WINDOW_SYSTEM */
27034 height = 1;
27036 if (width > 0 && it->line_wrap != TRUNCATE
27037 && it->current_x + width > it->last_visible_x)
27039 width = it->last_visible_x - it->current_x;
27040 #ifdef HAVE_WINDOW_SYSTEM
27041 /* Subtract one more pixel from the stretch width, but only on
27042 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27043 width -= FRAME_WINDOW_P (it->f);
27044 #endif
27047 if (width > 0 && height > 0 && it->glyph_row)
27049 Lisp_Object o_object = it->object;
27050 Lisp_Object object = it->stack[it->sp - 1].string;
27051 int n = width;
27053 if (!STRINGP (object))
27054 object = it->w->contents;
27055 #ifdef HAVE_WINDOW_SYSTEM
27056 if (FRAME_WINDOW_P (it->f))
27057 append_stretch_glyph (it, object, width, height, ascent);
27058 else
27059 #endif
27061 it->object = object;
27062 it->char_to_display = ' ';
27063 it->pixel_width = it->len = 1;
27064 while (n--)
27065 tty_append_glyph (it);
27066 it->object = o_object;
27070 it->pixel_width = width;
27071 #ifdef HAVE_WINDOW_SYSTEM
27072 if (FRAME_WINDOW_P (it->f))
27074 it->ascent = it->phys_ascent = ascent;
27075 it->descent = it->phys_descent = height - it->ascent;
27076 it->nglyphs = width > 0 && height > 0;
27077 take_vertical_position_into_account (it);
27079 else
27080 #endif
27081 it->nglyphs = width;
27084 /* Get information about special display element WHAT in an
27085 environment described by IT. WHAT is one of IT_TRUNCATION or
27086 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27087 non-null glyph_row member. This function ensures that fields like
27088 face_id, c, len of IT are left untouched. */
27090 static void
27091 produce_special_glyphs (struct it *it, enum display_element_type what)
27093 struct it temp_it;
27094 Lisp_Object gc;
27095 GLYPH glyph;
27097 temp_it = *it;
27098 temp_it.object = Qnil;
27099 memset (&temp_it.current, 0, sizeof temp_it.current);
27101 if (what == IT_CONTINUATION)
27103 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27104 if (it->bidi_it.paragraph_dir == R2L)
27105 SET_GLYPH_FROM_CHAR (glyph, '/');
27106 else
27107 SET_GLYPH_FROM_CHAR (glyph, '\\');
27108 if (it->dp
27109 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27111 /* FIXME: Should we mirror GC for R2L lines? */
27112 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27113 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27116 else if (what == IT_TRUNCATION)
27118 /* Truncation glyph. */
27119 SET_GLYPH_FROM_CHAR (glyph, '$');
27120 if (it->dp
27121 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27123 /* FIXME: Should we mirror GC for R2L lines? */
27124 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27125 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27128 else
27129 emacs_abort ();
27131 #ifdef HAVE_WINDOW_SYSTEM
27132 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27133 is turned off, we precede the truncation/continuation glyphs by a
27134 stretch glyph whose width is computed such that these special
27135 glyphs are aligned at the window margin, even when very different
27136 fonts are used in different glyph rows. */
27137 if (FRAME_WINDOW_P (temp_it.f)
27138 /* init_iterator calls this with it->glyph_row == NULL, and it
27139 wants only the pixel width of the truncation/continuation
27140 glyphs. */
27141 && temp_it.glyph_row
27142 /* insert_left_trunc_glyphs calls us at the beginning of the
27143 row, and it has its own calculation of the stretch glyph
27144 width. */
27145 && temp_it.glyph_row->used[TEXT_AREA] > 0
27146 && (temp_it.glyph_row->reversed_p
27147 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27148 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27150 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27152 if (stretch_width > 0)
27154 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27155 struct font *font =
27156 face->font ? face->font : FRAME_FONT (temp_it.f);
27157 int stretch_ascent =
27158 (((temp_it.ascent + temp_it.descent)
27159 * FONT_BASE (font)) / FONT_HEIGHT (font));
27161 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27162 temp_it.ascent + temp_it.descent,
27163 stretch_ascent);
27166 #endif
27168 temp_it.dp = NULL;
27169 temp_it.what = IT_CHARACTER;
27170 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27171 temp_it.face_id = GLYPH_FACE (glyph);
27172 temp_it.len = CHAR_BYTES (temp_it.c);
27174 PRODUCE_GLYPHS (&temp_it);
27175 it->pixel_width = temp_it.pixel_width;
27176 it->nglyphs = temp_it.nglyphs;
27179 #ifdef HAVE_WINDOW_SYSTEM
27181 /* Calculate line-height and line-spacing properties.
27182 An integer value specifies explicit pixel value.
27183 A float value specifies relative value to current face height.
27184 A cons (float . face-name) specifies relative value to
27185 height of specified face font.
27187 Returns height in pixels, or nil. */
27189 static Lisp_Object
27190 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27191 int boff, bool override)
27193 Lisp_Object face_name = Qnil;
27194 int ascent, descent, height;
27196 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27197 return val;
27199 if (CONSP (val))
27201 face_name = XCAR (val);
27202 val = XCDR (val);
27203 if (!NUMBERP (val))
27204 val = make_number (1);
27205 if (NILP (face_name))
27207 height = it->ascent + it->descent;
27208 goto scale;
27212 if (NILP (face_name))
27214 font = FRAME_FONT (it->f);
27215 boff = FRAME_BASELINE_OFFSET (it->f);
27217 else if (EQ (face_name, Qt))
27219 override = false;
27221 else
27223 int face_id;
27224 struct face *face;
27226 face_id = lookup_named_face (it->f, face_name, false);
27227 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27228 if (face == NULL || ((font = face->font) == NULL))
27229 return make_number (-1);
27230 boff = font->baseline_offset;
27231 if (font->vertical_centering)
27232 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27235 normal_char_ascent_descent (font, -1, &ascent, &descent);
27237 if (override)
27239 it->override_ascent = ascent;
27240 it->override_descent = descent;
27241 it->override_boff = boff;
27244 height = ascent + descent;
27246 scale:
27247 if (FLOATP (val))
27248 height = (int)(XFLOAT_DATA (val) * height);
27249 else if (INTEGERP (val))
27250 height *= XINT (val);
27252 return make_number (height);
27256 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27257 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27258 and only if this is for a character for which no font was found.
27260 If the display method (it->glyphless_method) is
27261 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27262 length of the acronym or the hexadecimal string, UPPER_XOFF and
27263 UPPER_YOFF are pixel offsets for the upper part of the string,
27264 LOWER_XOFF and LOWER_YOFF are for the lower part.
27266 For the other display methods, LEN through LOWER_YOFF are zero. */
27268 static void
27269 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27270 short upper_xoff, short upper_yoff,
27271 short lower_xoff, short lower_yoff)
27273 struct glyph *glyph;
27274 enum glyph_row_area area = it->area;
27276 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27277 if (glyph < it->glyph_row->glyphs[area + 1])
27279 /* If the glyph row is reversed, we need to prepend the glyph
27280 rather than append it. */
27281 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27283 struct glyph *g;
27285 /* Make room for the additional glyph. */
27286 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27287 g[1] = *g;
27288 glyph = it->glyph_row->glyphs[area];
27290 glyph->charpos = CHARPOS (it->position);
27291 glyph->object = it->object;
27292 eassert (it->pixel_width <= SHRT_MAX);
27293 glyph->pixel_width = it->pixel_width;
27294 glyph->ascent = it->ascent;
27295 glyph->descent = it->descent;
27296 glyph->voffset = it->voffset;
27297 glyph->type = GLYPHLESS_GLYPH;
27298 glyph->u.glyphless.method = it->glyphless_method;
27299 glyph->u.glyphless.for_no_font = for_no_font;
27300 glyph->u.glyphless.len = len;
27301 glyph->u.glyphless.ch = it->c;
27302 glyph->slice.glyphless.upper_xoff = upper_xoff;
27303 glyph->slice.glyphless.upper_yoff = upper_yoff;
27304 glyph->slice.glyphless.lower_xoff = lower_xoff;
27305 glyph->slice.glyphless.lower_yoff = lower_yoff;
27306 glyph->avoid_cursor_p = it->avoid_cursor_p;
27307 glyph->multibyte_p = it->multibyte_p;
27308 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27310 /* In R2L rows, the left and the right box edges need to be
27311 drawn in reverse direction. */
27312 glyph->right_box_line_p = it->start_of_box_run_p;
27313 glyph->left_box_line_p = it->end_of_box_run_p;
27315 else
27317 glyph->left_box_line_p = it->start_of_box_run_p;
27318 glyph->right_box_line_p = it->end_of_box_run_p;
27320 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27321 || it->phys_descent > it->descent);
27322 glyph->padding_p = false;
27323 glyph->glyph_not_available_p = false;
27324 glyph->face_id = face_id;
27325 glyph->font_type = FONT_TYPE_UNKNOWN;
27326 if (it->bidi_p)
27328 glyph->resolved_level = it->bidi_it.resolved_level;
27329 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27330 glyph->bidi_type = it->bidi_it.type;
27332 ++it->glyph_row->used[area];
27334 else
27335 IT_EXPAND_MATRIX_WIDTH (it, area);
27339 /* Produce a glyph for a glyphless character for iterator IT.
27340 IT->glyphless_method specifies which method to use for displaying
27341 the character. See the description of enum
27342 glyphless_display_method in dispextern.h for the detail.
27344 FOR_NO_FONT is true if and only if this is for a character for
27345 which no font was found. ACRONYM, if non-nil, is an acronym string
27346 for the character. */
27348 static void
27349 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27351 int face_id;
27352 struct face *face;
27353 struct font *font;
27354 int base_width, base_height, width, height;
27355 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27356 int len;
27358 /* Get the metrics of the base font. We always refer to the current
27359 ASCII face. */
27360 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27361 font = face->font ? face->font : FRAME_FONT (it->f);
27362 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27363 it->ascent += font->baseline_offset;
27364 it->descent -= font->baseline_offset;
27365 base_height = it->ascent + it->descent;
27366 base_width = font->average_width;
27368 face_id = merge_glyphless_glyph_face (it);
27370 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27372 it->pixel_width = THIN_SPACE_WIDTH;
27373 len = 0;
27374 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27376 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27378 width = CHARACTER_WIDTH (it->c);
27379 if (width == 0)
27380 width = 1;
27381 else if (width > 4)
27382 width = 4;
27383 it->pixel_width = base_width * width;
27384 len = 0;
27385 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27387 else
27389 char buf[7];
27390 const char *str;
27391 unsigned int code[6];
27392 int upper_len;
27393 int ascent, descent;
27394 struct font_metrics metrics_upper, metrics_lower;
27396 face = FACE_FROM_ID (it->f, face_id);
27397 font = face->font ? face->font : FRAME_FONT (it->f);
27398 prepare_face_for_display (it->f, face);
27400 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27402 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27403 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27404 if (CONSP (acronym))
27405 acronym = XCAR (acronym);
27406 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27408 else
27410 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27411 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27412 str = buf;
27414 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27415 code[len] = font->driver->encode_char (font, str[len]);
27416 upper_len = (len + 1) / 2;
27417 font->driver->text_extents (font, code, upper_len,
27418 &metrics_upper);
27419 font->driver->text_extents (font, code + upper_len, len - upper_len,
27420 &metrics_lower);
27424 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27425 width = max (metrics_upper.width, metrics_lower.width) + 4;
27426 upper_xoff = upper_yoff = 2; /* the typical case */
27427 if (base_width >= width)
27429 /* Align the upper to the left, the lower to the right. */
27430 it->pixel_width = base_width;
27431 lower_xoff = base_width - 2 - metrics_lower.width;
27433 else
27435 /* Center the shorter one. */
27436 it->pixel_width = width;
27437 if (metrics_upper.width >= metrics_lower.width)
27438 lower_xoff = (width - metrics_lower.width) / 2;
27439 else
27441 /* FIXME: This code doesn't look right. It formerly was
27442 missing the "lower_xoff = 0;", which couldn't have
27443 been right since it left lower_xoff uninitialized. */
27444 lower_xoff = 0;
27445 upper_xoff = (width - metrics_upper.width) / 2;
27449 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27450 top, bottom, and between upper and lower strings. */
27451 height = (metrics_upper.ascent + metrics_upper.descent
27452 + metrics_lower.ascent + metrics_lower.descent) + 5;
27453 /* Center vertically.
27454 H:base_height, D:base_descent
27455 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27457 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27458 descent = D - H/2 + h/2;
27459 lower_yoff = descent - 2 - ld;
27460 upper_yoff = lower_yoff - la - 1 - ud; */
27461 ascent = - (it->descent - (base_height + height + 1) / 2);
27462 descent = it->descent - (base_height - height) / 2;
27463 lower_yoff = descent - 2 - metrics_lower.descent;
27464 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27465 - metrics_upper.descent);
27466 /* Don't make the height shorter than the base height. */
27467 if (height > base_height)
27469 it->ascent = ascent;
27470 it->descent = descent;
27474 it->phys_ascent = it->ascent;
27475 it->phys_descent = it->descent;
27476 if (it->glyph_row)
27477 append_glyphless_glyph (it, face_id, for_no_font, len,
27478 upper_xoff, upper_yoff,
27479 lower_xoff, lower_yoff);
27480 it->nglyphs = 1;
27481 take_vertical_position_into_account (it);
27485 /* RIF:
27486 Produce glyphs/get display metrics for the display element IT is
27487 loaded with. See the description of struct it in dispextern.h
27488 for an overview of struct it. */
27490 void
27491 x_produce_glyphs (struct it *it)
27493 int extra_line_spacing = it->extra_line_spacing;
27495 it->glyph_not_available_p = false;
27497 if (it->what == IT_CHARACTER)
27499 XChar2b char2b;
27500 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27501 struct font *font = face->font;
27502 struct font_metrics *pcm = NULL;
27503 int boff; /* Baseline offset. */
27505 if (font == NULL)
27507 /* When no suitable font is found, display this character by
27508 the method specified in the first extra slot of
27509 Vglyphless_char_display. */
27510 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27512 eassert (it->what == IT_GLYPHLESS);
27513 produce_glyphless_glyph (it, true,
27514 STRINGP (acronym) ? acronym : Qnil);
27515 goto done;
27518 boff = font->baseline_offset;
27519 if (font->vertical_centering)
27520 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27522 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27524 it->nglyphs = 1;
27526 if (it->override_ascent >= 0)
27528 it->ascent = it->override_ascent;
27529 it->descent = it->override_descent;
27530 boff = it->override_boff;
27532 else
27534 it->ascent = FONT_BASE (font) + boff;
27535 it->descent = FONT_DESCENT (font) - boff;
27538 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27540 pcm = get_per_char_metric (font, &char2b);
27541 if (pcm->width == 0
27542 && pcm->rbearing == 0 && pcm->lbearing == 0)
27543 pcm = NULL;
27546 if (pcm)
27548 it->phys_ascent = pcm->ascent + boff;
27549 it->phys_descent = pcm->descent - boff;
27550 it->pixel_width = pcm->width;
27551 /* Don't use font-global values for ascent and descent
27552 if they result in an exceedingly large line height. */
27553 if (it->override_ascent < 0)
27555 if (FONT_TOO_HIGH (font))
27557 it->ascent = it->phys_ascent;
27558 it->descent = it->phys_descent;
27559 /* These limitations are enforced by an
27560 assertion near the end of this function. */
27561 if (it->ascent < 0)
27562 it->ascent = 0;
27563 if (it->descent < 0)
27564 it->descent = 0;
27568 else
27570 it->glyph_not_available_p = true;
27571 it->phys_ascent = it->ascent;
27572 it->phys_descent = it->descent;
27573 it->pixel_width = font->space_width;
27576 if (it->constrain_row_ascent_descent_p)
27578 if (it->descent > it->max_descent)
27580 it->ascent += it->descent - it->max_descent;
27581 it->descent = it->max_descent;
27583 if (it->ascent > it->max_ascent)
27585 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27586 it->ascent = it->max_ascent;
27588 it->phys_ascent = min (it->phys_ascent, it->ascent);
27589 it->phys_descent = min (it->phys_descent, it->descent);
27590 extra_line_spacing = 0;
27593 /* If this is a space inside a region of text with
27594 `space-width' property, change its width. */
27595 bool stretched_p
27596 = it->char_to_display == ' ' && !NILP (it->space_width);
27597 if (stretched_p)
27598 it->pixel_width *= XFLOATINT (it->space_width);
27600 /* If face has a box, add the box thickness to the character
27601 height. If character has a box line to the left and/or
27602 right, add the box line width to the character's width. */
27603 if (face->box != FACE_NO_BOX)
27605 int thick = face->box_line_width;
27607 if (thick > 0)
27609 it->ascent += thick;
27610 it->descent += thick;
27612 else
27613 thick = -thick;
27615 if (it->start_of_box_run_p)
27616 it->pixel_width += thick;
27617 if (it->end_of_box_run_p)
27618 it->pixel_width += thick;
27621 /* If face has an overline, add the height of the overline
27622 (1 pixel) and a 1 pixel margin to the character height. */
27623 if (face->overline_p)
27624 it->ascent += overline_margin;
27626 if (it->constrain_row_ascent_descent_p)
27628 if (it->ascent > it->max_ascent)
27629 it->ascent = it->max_ascent;
27630 if (it->descent > it->max_descent)
27631 it->descent = it->max_descent;
27634 take_vertical_position_into_account (it);
27636 /* If we have to actually produce glyphs, do it. */
27637 if (it->glyph_row)
27639 if (stretched_p)
27641 /* Translate a space with a `space-width' property
27642 into a stretch glyph. */
27643 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27644 / FONT_HEIGHT (font));
27645 append_stretch_glyph (it, it->object, it->pixel_width,
27646 it->ascent + it->descent, ascent);
27648 else
27649 append_glyph (it);
27651 /* If characters with lbearing or rbearing are displayed
27652 in this line, record that fact in a flag of the
27653 glyph row. This is used to optimize X output code. */
27654 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27655 it->glyph_row->contains_overlapping_glyphs_p = true;
27657 if (! stretched_p && it->pixel_width == 0)
27658 /* We assure that all visible glyphs have at least 1-pixel
27659 width. */
27660 it->pixel_width = 1;
27662 else if (it->char_to_display == '\n')
27664 /* A newline has no width, but we need the height of the
27665 line. But if previous part of the line sets a height,
27666 don't increase that height. */
27668 Lisp_Object height;
27669 Lisp_Object total_height = Qnil;
27671 it->override_ascent = -1;
27672 it->pixel_width = 0;
27673 it->nglyphs = 0;
27675 height = get_it_property (it, Qline_height);
27676 /* Split (line-height total-height) list. */
27677 if (CONSP (height)
27678 && CONSP (XCDR (height))
27679 && NILP (XCDR (XCDR (height))))
27681 total_height = XCAR (XCDR (height));
27682 height = XCAR (height);
27684 height = calc_line_height_property (it, height, font, boff, true);
27686 if (it->override_ascent >= 0)
27688 it->ascent = it->override_ascent;
27689 it->descent = it->override_descent;
27690 boff = it->override_boff;
27692 else
27694 if (FONT_TOO_HIGH (font))
27696 it->ascent = font->pixel_size + boff - 1;
27697 it->descent = -boff + 1;
27698 if (it->descent < 0)
27699 it->descent = 0;
27701 else
27703 it->ascent = FONT_BASE (font) + boff;
27704 it->descent = FONT_DESCENT (font) - boff;
27708 if (EQ (height, Qt))
27710 if (it->descent > it->max_descent)
27712 it->ascent += it->descent - it->max_descent;
27713 it->descent = it->max_descent;
27715 if (it->ascent > it->max_ascent)
27717 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27718 it->ascent = it->max_ascent;
27720 it->phys_ascent = min (it->phys_ascent, it->ascent);
27721 it->phys_descent = min (it->phys_descent, it->descent);
27722 it->constrain_row_ascent_descent_p = true;
27723 extra_line_spacing = 0;
27725 else
27727 Lisp_Object spacing;
27729 it->phys_ascent = it->ascent;
27730 it->phys_descent = it->descent;
27732 if ((it->max_ascent > 0 || it->max_descent > 0)
27733 && face->box != FACE_NO_BOX
27734 && face->box_line_width > 0)
27736 it->ascent += face->box_line_width;
27737 it->descent += face->box_line_width;
27739 if (!NILP (height)
27740 && XINT (height) > it->ascent + it->descent)
27741 it->ascent = XINT (height) - it->descent;
27743 if (!NILP (total_height))
27744 spacing = calc_line_height_property (it, total_height, font,
27745 boff, false);
27746 else
27748 spacing = get_it_property (it, Qline_spacing);
27749 spacing = calc_line_height_property (it, spacing, font,
27750 boff, false);
27752 if (INTEGERP (spacing))
27754 extra_line_spacing = XINT (spacing);
27755 if (!NILP (total_height))
27756 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27760 else /* i.e. (it->char_to_display == '\t') */
27762 if (font->space_width > 0)
27764 int tab_width = it->tab_width * font->space_width;
27765 int x = it->current_x + it->continuation_lines_width;
27766 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27768 /* If the distance from the current position to the next tab
27769 stop is less than a space character width, use the
27770 tab stop after that. */
27771 if (next_tab_x - x < font->space_width)
27772 next_tab_x += tab_width;
27774 it->pixel_width = next_tab_x - x;
27775 it->nglyphs = 1;
27776 if (FONT_TOO_HIGH (font))
27778 if (get_char_glyph_code (' ', font, &char2b))
27780 pcm = get_per_char_metric (font, &char2b);
27781 if (pcm->width == 0
27782 && pcm->rbearing == 0 && pcm->lbearing == 0)
27783 pcm = NULL;
27786 if (pcm)
27788 it->ascent = pcm->ascent + boff;
27789 it->descent = pcm->descent - boff;
27791 else
27793 it->ascent = font->pixel_size + boff - 1;
27794 it->descent = -boff + 1;
27796 if (it->ascent < 0)
27797 it->ascent = 0;
27798 if (it->descent < 0)
27799 it->descent = 0;
27801 else
27803 it->ascent = FONT_BASE (font) + boff;
27804 it->descent = FONT_DESCENT (font) - boff;
27806 it->phys_ascent = it->ascent;
27807 it->phys_descent = it->descent;
27809 if (it->glyph_row)
27811 append_stretch_glyph (it, it->object, it->pixel_width,
27812 it->ascent + it->descent, it->ascent);
27815 else
27817 it->pixel_width = 0;
27818 it->nglyphs = 1;
27822 if (FONT_TOO_HIGH (font))
27824 int font_ascent, font_descent;
27826 /* For very large fonts, where we ignore the declared font
27827 dimensions, and go by per-character metrics instead,
27828 don't let the row ascent and descent values (and the row
27829 height computed from them) be smaller than the "normal"
27830 character metrics. This avoids unpleasant effects
27831 whereby lines on display would change their height
27832 depending on which characters are shown. */
27833 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27834 it->max_ascent = max (it->max_ascent, font_ascent);
27835 it->max_descent = max (it->max_descent, font_descent);
27838 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27840 /* A static composition.
27842 Note: A composition is represented as one glyph in the
27843 glyph matrix. There are no padding glyphs.
27845 Important note: pixel_width, ascent, and descent are the
27846 values of what is drawn by draw_glyphs (i.e. the values of
27847 the overall glyphs composed). */
27848 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27849 int boff; /* baseline offset */
27850 struct composition *cmp = composition_table[it->cmp_it.id];
27851 int glyph_len = cmp->glyph_len;
27852 struct font *font = face->font;
27854 it->nglyphs = 1;
27856 /* If we have not yet calculated pixel size data of glyphs of
27857 the composition for the current face font, calculate them
27858 now. Theoretically, we have to check all fonts for the
27859 glyphs, but that requires much time and memory space. So,
27860 here we check only the font of the first glyph. This may
27861 lead to incorrect display, but it's very rare, and C-l
27862 (recenter-top-bottom) can correct the display anyway. */
27863 if (! cmp->font || cmp->font != font)
27865 /* Ascent and descent of the font of the first character
27866 of this composition (adjusted by baseline offset).
27867 Ascent and descent of overall glyphs should not be less
27868 than these, respectively. */
27869 int font_ascent, font_descent, font_height;
27870 /* Bounding box of the overall glyphs. */
27871 int leftmost, rightmost, lowest, highest;
27872 int lbearing, rbearing;
27873 int i, width, ascent, descent;
27874 int c;
27875 XChar2b char2b;
27876 struct font_metrics *pcm;
27877 ptrdiff_t pos;
27879 eassume (0 < glyph_len); /* See Bug#8512. */
27881 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27882 while (c == '\t' && 0 < --glyph_len);
27884 bool right_padded = glyph_len < cmp->glyph_len;
27885 for (i = 0; i < glyph_len; i++)
27887 c = COMPOSITION_GLYPH (cmp, i);
27888 if (c != '\t')
27889 break;
27890 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27892 bool left_padded = i > 0;
27894 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27895 : IT_CHARPOS (*it));
27896 /* If no suitable font is found, use the default font. */
27897 bool font_not_found_p = font == NULL;
27898 if (font_not_found_p)
27900 face = face->ascii_face;
27901 font = face->font;
27903 boff = font->baseline_offset;
27904 if (font->vertical_centering)
27905 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27906 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27907 font_ascent += boff;
27908 font_descent -= boff;
27909 font_height = font_ascent + font_descent;
27911 cmp->font = font;
27913 pcm = NULL;
27914 if (! font_not_found_p)
27916 get_char_face_and_encoding (it->f, c, it->face_id,
27917 &char2b, false);
27918 pcm = get_per_char_metric (font, &char2b);
27921 /* Initialize the bounding box. */
27922 if (pcm)
27924 width = cmp->glyph_len > 0 ? pcm->width : 0;
27925 ascent = pcm->ascent;
27926 descent = pcm->descent;
27927 lbearing = pcm->lbearing;
27928 rbearing = pcm->rbearing;
27930 else
27932 width = cmp->glyph_len > 0 ? font->space_width : 0;
27933 ascent = FONT_BASE (font);
27934 descent = FONT_DESCENT (font);
27935 lbearing = 0;
27936 rbearing = width;
27939 rightmost = width;
27940 leftmost = 0;
27941 lowest = - descent + boff;
27942 highest = ascent + boff;
27944 if (! font_not_found_p
27945 && font->default_ascent
27946 && CHAR_TABLE_P (Vuse_default_ascent)
27947 && !NILP (Faref (Vuse_default_ascent,
27948 make_number (it->char_to_display))))
27949 highest = font->default_ascent + boff;
27951 /* Draw the first glyph at the normal position. It may be
27952 shifted to right later if some other glyphs are drawn
27953 at the left. */
27954 cmp->offsets[i * 2] = 0;
27955 cmp->offsets[i * 2 + 1] = boff;
27956 cmp->lbearing = lbearing;
27957 cmp->rbearing = rbearing;
27959 /* Set cmp->offsets for the remaining glyphs. */
27960 for (i++; i < glyph_len; i++)
27962 int left, right, btm, top;
27963 int ch = COMPOSITION_GLYPH (cmp, i);
27964 int face_id;
27965 struct face *this_face;
27967 if (ch == '\t')
27968 ch = ' ';
27969 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27970 this_face = FACE_FROM_ID (it->f, face_id);
27971 font = this_face->font;
27973 if (font == NULL)
27974 pcm = NULL;
27975 else
27977 get_char_face_and_encoding (it->f, ch, face_id,
27978 &char2b, false);
27979 pcm = get_per_char_metric (font, &char2b);
27981 if (! pcm)
27982 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27983 else
27985 width = pcm->width;
27986 ascent = pcm->ascent;
27987 descent = pcm->descent;
27988 lbearing = pcm->lbearing;
27989 rbearing = pcm->rbearing;
27990 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27992 /* Relative composition with or without
27993 alternate chars. */
27994 left = (leftmost + rightmost - width) / 2;
27995 btm = - descent + boff;
27996 if (font->relative_compose
27997 && (! CHAR_TABLE_P (Vignore_relative_composition)
27998 || NILP (Faref (Vignore_relative_composition,
27999 make_number (ch)))))
28002 if (- descent >= font->relative_compose)
28003 /* One extra pixel between two glyphs. */
28004 btm = highest + 1;
28005 else if (ascent <= 0)
28006 /* One extra pixel between two glyphs. */
28007 btm = lowest - 1 - ascent - descent;
28010 else
28012 /* A composition rule is specified by an integer
28013 value that encodes global and new reference
28014 points (GREF and NREF). GREF and NREF are
28015 specified by numbers as below:
28017 0---1---2 -- ascent
28021 9--10--11 -- center
28023 ---3---4---5--- baseline
28025 6---7---8 -- descent
28027 int rule = COMPOSITION_RULE (cmp, i);
28028 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28030 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28031 grefx = gref % 3, nrefx = nref % 3;
28032 grefy = gref / 3, nrefy = nref / 3;
28033 if (xoff)
28034 xoff = font_height * (xoff - 128) / 256;
28035 if (yoff)
28036 yoff = font_height * (yoff - 128) / 256;
28038 left = (leftmost
28039 + grefx * (rightmost - leftmost) / 2
28040 - nrefx * width / 2
28041 + xoff);
28043 btm = ((grefy == 0 ? highest
28044 : grefy == 1 ? 0
28045 : grefy == 2 ? lowest
28046 : (highest + lowest) / 2)
28047 - (nrefy == 0 ? ascent + descent
28048 : nrefy == 1 ? descent - boff
28049 : nrefy == 2 ? 0
28050 : (ascent + descent) / 2)
28051 + yoff);
28054 cmp->offsets[i * 2] = left;
28055 cmp->offsets[i * 2 + 1] = btm + descent;
28057 /* Update the bounding box of the overall glyphs. */
28058 if (width > 0)
28060 right = left + width;
28061 if (left < leftmost)
28062 leftmost = left;
28063 if (right > rightmost)
28064 rightmost = right;
28066 top = btm + descent + ascent;
28067 if (top > highest)
28068 highest = top;
28069 if (btm < lowest)
28070 lowest = btm;
28072 if (cmp->lbearing > left + lbearing)
28073 cmp->lbearing = left + lbearing;
28074 if (cmp->rbearing < left + rbearing)
28075 cmp->rbearing = left + rbearing;
28079 /* If there are glyphs whose x-offsets are negative,
28080 shift all glyphs to the right and make all x-offsets
28081 non-negative. */
28082 if (leftmost < 0)
28084 for (i = 0; i < cmp->glyph_len; i++)
28085 cmp->offsets[i * 2] -= leftmost;
28086 rightmost -= leftmost;
28087 cmp->lbearing -= leftmost;
28088 cmp->rbearing -= leftmost;
28091 if (left_padded && cmp->lbearing < 0)
28093 for (i = 0; i < cmp->glyph_len; i++)
28094 cmp->offsets[i * 2] -= cmp->lbearing;
28095 rightmost -= cmp->lbearing;
28096 cmp->rbearing -= cmp->lbearing;
28097 cmp->lbearing = 0;
28099 if (right_padded && rightmost < cmp->rbearing)
28101 rightmost = cmp->rbearing;
28104 cmp->pixel_width = rightmost;
28105 cmp->ascent = highest;
28106 cmp->descent = - lowest;
28107 if (cmp->ascent < font_ascent)
28108 cmp->ascent = font_ascent;
28109 if (cmp->descent < font_descent)
28110 cmp->descent = font_descent;
28113 if (it->glyph_row
28114 && (cmp->lbearing < 0
28115 || cmp->rbearing > cmp->pixel_width))
28116 it->glyph_row->contains_overlapping_glyphs_p = true;
28118 it->pixel_width = cmp->pixel_width;
28119 it->ascent = it->phys_ascent = cmp->ascent;
28120 it->descent = it->phys_descent = cmp->descent;
28121 if (face->box != FACE_NO_BOX)
28123 int thick = face->box_line_width;
28125 if (thick > 0)
28127 it->ascent += thick;
28128 it->descent += thick;
28130 else
28131 thick = - thick;
28133 if (it->start_of_box_run_p)
28134 it->pixel_width += thick;
28135 if (it->end_of_box_run_p)
28136 it->pixel_width += thick;
28139 /* If face has an overline, add the height of the overline
28140 (1 pixel) and a 1 pixel margin to the character height. */
28141 if (face->overline_p)
28142 it->ascent += overline_margin;
28144 take_vertical_position_into_account (it);
28145 if (it->ascent < 0)
28146 it->ascent = 0;
28147 if (it->descent < 0)
28148 it->descent = 0;
28150 if (it->glyph_row && cmp->glyph_len > 0)
28151 append_composite_glyph (it);
28153 else if (it->what == IT_COMPOSITION)
28155 /* A dynamic (automatic) composition. */
28156 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28157 Lisp_Object gstring;
28158 struct font_metrics metrics;
28160 it->nglyphs = 1;
28162 gstring = composition_gstring_from_id (it->cmp_it.id);
28163 it->pixel_width
28164 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28165 &metrics);
28166 if (it->glyph_row
28167 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28168 it->glyph_row->contains_overlapping_glyphs_p = true;
28169 it->ascent = it->phys_ascent = metrics.ascent;
28170 it->descent = it->phys_descent = metrics.descent;
28171 if (face->box != FACE_NO_BOX)
28173 int thick = face->box_line_width;
28175 if (thick > 0)
28177 it->ascent += thick;
28178 it->descent += thick;
28180 else
28181 thick = - thick;
28183 if (it->start_of_box_run_p)
28184 it->pixel_width += thick;
28185 if (it->end_of_box_run_p)
28186 it->pixel_width += thick;
28188 /* If face has an overline, add the height of the overline
28189 (1 pixel) and a 1 pixel margin to the character height. */
28190 if (face->overline_p)
28191 it->ascent += overline_margin;
28192 take_vertical_position_into_account (it);
28193 if (it->ascent < 0)
28194 it->ascent = 0;
28195 if (it->descent < 0)
28196 it->descent = 0;
28198 if (it->glyph_row)
28199 append_composite_glyph (it);
28201 else if (it->what == IT_GLYPHLESS)
28202 produce_glyphless_glyph (it, false, Qnil);
28203 else if (it->what == IT_IMAGE)
28204 produce_image_glyph (it);
28205 else if (it->what == IT_STRETCH)
28206 produce_stretch_glyph (it);
28207 else if (it->what == IT_XWIDGET)
28208 produce_xwidget_glyph (it);
28210 done:
28211 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28212 because this isn't true for images with `:ascent 100'. */
28213 eassert (it->ascent >= 0 && it->descent >= 0);
28214 if (it->area == TEXT_AREA)
28215 it->current_x += it->pixel_width;
28217 if (extra_line_spacing > 0)
28219 it->descent += extra_line_spacing;
28220 if (extra_line_spacing > it->max_extra_line_spacing)
28221 it->max_extra_line_spacing = extra_line_spacing;
28224 it->max_ascent = max (it->max_ascent, it->ascent);
28225 it->max_descent = max (it->max_descent, it->descent);
28226 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28227 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28230 /* EXPORT for RIF:
28231 Output LEN glyphs starting at START at the nominal cursor position.
28232 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28233 being updated, and UPDATED_AREA is the area of that row being updated. */
28235 void
28236 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28237 struct glyph *start, enum glyph_row_area updated_area, int len)
28239 int x, hpos, chpos = w->phys_cursor.hpos;
28241 eassert (updated_row);
28242 /* When the window is hscrolled, cursor hpos can legitimately be out
28243 of bounds, but we draw the cursor at the corresponding window
28244 margin in that case. */
28245 if (!updated_row->reversed_p && chpos < 0)
28246 chpos = 0;
28247 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28248 chpos = updated_row->used[TEXT_AREA] - 1;
28250 block_input ();
28252 /* Write glyphs. */
28254 hpos = start - updated_row->glyphs[updated_area];
28255 x = draw_glyphs (w, w->output_cursor.x,
28256 updated_row, updated_area,
28257 hpos, hpos + len,
28258 DRAW_NORMAL_TEXT, 0);
28260 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28261 if (updated_area == TEXT_AREA
28262 && w->phys_cursor_on_p
28263 && w->phys_cursor.vpos == w->output_cursor.vpos
28264 && chpos >= hpos
28265 && chpos < hpos + len)
28266 w->phys_cursor_on_p = false;
28268 unblock_input ();
28270 /* Advance the output cursor. */
28271 w->output_cursor.hpos += len;
28272 w->output_cursor.x = x;
28276 /* EXPORT for RIF:
28277 Insert LEN glyphs from START at the nominal cursor position. */
28279 void
28280 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28281 struct glyph *start, enum glyph_row_area updated_area, int len)
28283 struct frame *f;
28284 int line_height, shift_by_width, shifted_region_width;
28285 struct glyph_row *row;
28286 struct glyph *glyph;
28287 int frame_x, frame_y;
28288 ptrdiff_t hpos;
28290 eassert (updated_row);
28291 block_input ();
28292 f = XFRAME (WINDOW_FRAME (w));
28294 /* Get the height of the line we are in. */
28295 row = updated_row;
28296 line_height = row->height;
28298 /* Get the width of the glyphs to insert. */
28299 shift_by_width = 0;
28300 for (glyph = start; glyph < start + len; ++glyph)
28301 shift_by_width += glyph->pixel_width;
28303 /* Get the width of the region to shift right. */
28304 shifted_region_width = (window_box_width (w, updated_area)
28305 - w->output_cursor.x
28306 - shift_by_width);
28308 /* Shift right. */
28309 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28310 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28312 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28313 line_height, shift_by_width);
28315 /* Write the glyphs. */
28316 hpos = start - row->glyphs[updated_area];
28317 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28318 hpos, hpos + len,
28319 DRAW_NORMAL_TEXT, 0);
28321 /* Advance the output cursor. */
28322 w->output_cursor.hpos += len;
28323 w->output_cursor.x += shift_by_width;
28324 unblock_input ();
28328 /* EXPORT for RIF:
28329 Erase the current text line from the nominal cursor position
28330 (inclusive) to pixel column TO_X (exclusive). The idea is that
28331 everything from TO_X onward is already erased.
28333 TO_X is a pixel position relative to UPDATED_AREA of currently
28334 updated window W. TO_X == -1 means clear to the end of this area. */
28336 void
28337 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28338 enum glyph_row_area updated_area, int to_x)
28340 struct frame *f;
28341 int max_x, min_y, max_y;
28342 int from_x, from_y, to_y;
28344 eassert (updated_row);
28345 f = XFRAME (w->frame);
28347 if (updated_row->full_width_p)
28348 max_x = (WINDOW_PIXEL_WIDTH (w)
28349 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28350 else
28351 max_x = window_box_width (w, updated_area);
28352 max_y = window_text_bottom_y (w);
28354 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28355 of window. For TO_X > 0, truncate to end of drawing area. */
28356 if (to_x == 0)
28357 return;
28358 else if (to_x < 0)
28359 to_x = max_x;
28360 else
28361 to_x = min (to_x, max_x);
28363 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28365 /* Notice if the cursor will be cleared by this operation. */
28366 if (!updated_row->full_width_p)
28367 notice_overwritten_cursor (w, updated_area,
28368 w->output_cursor.x, -1,
28369 updated_row->y,
28370 MATRIX_ROW_BOTTOM_Y (updated_row));
28372 from_x = w->output_cursor.x;
28374 /* Translate to frame coordinates. */
28375 if (updated_row->full_width_p)
28377 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28378 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28380 else
28382 int area_left = window_box_left (w, updated_area);
28383 from_x += area_left;
28384 to_x += area_left;
28387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28388 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28389 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28391 /* Prevent inadvertently clearing to end of the X window. */
28392 if (to_x > from_x && to_y > from_y)
28394 block_input ();
28395 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28396 to_x - from_x, to_y - from_y);
28397 unblock_input ();
28401 #endif /* HAVE_WINDOW_SYSTEM */
28405 /***********************************************************************
28406 Cursor types
28407 ***********************************************************************/
28409 /* Value is the internal representation of the specified cursor type
28410 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28411 of the bar cursor. */
28413 static enum text_cursor_kinds
28414 get_specified_cursor_type (Lisp_Object arg, int *width)
28416 enum text_cursor_kinds type;
28418 if (NILP (arg))
28419 return NO_CURSOR;
28421 if (EQ (arg, Qbox))
28422 return FILLED_BOX_CURSOR;
28424 if (EQ (arg, Qhollow))
28425 return HOLLOW_BOX_CURSOR;
28427 if (EQ (arg, Qbar))
28429 *width = 2;
28430 return BAR_CURSOR;
28433 if (CONSP (arg)
28434 && EQ (XCAR (arg), Qbar)
28435 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28437 *width = XINT (XCDR (arg));
28438 return BAR_CURSOR;
28441 if (EQ (arg, Qhbar))
28443 *width = 2;
28444 return HBAR_CURSOR;
28447 if (CONSP (arg)
28448 && EQ (XCAR (arg), Qhbar)
28449 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28451 *width = XINT (XCDR (arg));
28452 return HBAR_CURSOR;
28455 /* Treat anything unknown as "hollow box cursor".
28456 It was bad to signal an error; people have trouble fixing
28457 .Xdefaults with Emacs, when it has something bad in it. */
28458 type = HOLLOW_BOX_CURSOR;
28460 return type;
28463 /* Set the default cursor types for specified frame. */
28464 void
28465 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28467 int width = 1;
28468 Lisp_Object tem;
28470 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28471 FRAME_CURSOR_WIDTH (f) = width;
28473 /* By default, set up the blink-off state depending on the on-state. */
28475 tem = Fassoc (arg, Vblink_cursor_alist);
28476 if (!NILP (tem))
28478 FRAME_BLINK_OFF_CURSOR (f)
28479 = get_specified_cursor_type (XCDR (tem), &width);
28480 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28482 else
28483 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28485 /* Make sure the cursor gets redrawn. */
28486 f->cursor_type_changed = true;
28490 #ifdef HAVE_WINDOW_SYSTEM
28492 /* Return the cursor we want to be displayed in window W. Return
28493 width of bar/hbar cursor through WIDTH arg. Return with
28494 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28495 (i.e. if the `system caret' should track this cursor).
28497 In a mini-buffer window, we want the cursor only to appear if we
28498 are reading input from this window. For the selected window, we
28499 want the cursor type given by the frame parameter or buffer local
28500 setting of cursor-type. If explicitly marked off, draw no cursor.
28501 In all other cases, we want a hollow box cursor. */
28503 static enum text_cursor_kinds
28504 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28505 bool *active_cursor)
28507 struct frame *f = XFRAME (w->frame);
28508 struct buffer *b = XBUFFER (w->contents);
28509 int cursor_type = DEFAULT_CURSOR;
28510 Lisp_Object alt_cursor;
28511 bool non_selected = false;
28513 *active_cursor = true;
28515 /* Echo area */
28516 if (cursor_in_echo_area
28517 && FRAME_HAS_MINIBUF_P (f)
28518 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28520 if (w == XWINDOW (echo_area_window))
28522 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28524 *width = FRAME_CURSOR_WIDTH (f);
28525 return FRAME_DESIRED_CURSOR (f);
28527 else
28528 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28531 *active_cursor = false;
28532 non_selected = true;
28535 /* Detect a nonselected window or nonselected frame. */
28536 else if (w != XWINDOW (f->selected_window)
28537 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28539 *active_cursor = false;
28541 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28542 return NO_CURSOR;
28544 non_selected = true;
28547 /* Never display a cursor in a window in which cursor-type is nil. */
28548 if (NILP (BVAR (b, cursor_type)))
28549 return NO_CURSOR;
28551 /* Get the normal cursor type for this window. */
28552 if (EQ (BVAR (b, cursor_type), Qt))
28554 cursor_type = FRAME_DESIRED_CURSOR (f);
28555 *width = FRAME_CURSOR_WIDTH (f);
28557 else
28558 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28560 /* Use cursor-in-non-selected-windows instead
28561 for non-selected window or frame. */
28562 if (non_selected)
28564 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28565 if (!EQ (Qt, alt_cursor))
28566 return get_specified_cursor_type (alt_cursor, width);
28567 /* t means modify the normal cursor type. */
28568 if (cursor_type == FILLED_BOX_CURSOR)
28569 cursor_type = HOLLOW_BOX_CURSOR;
28570 else if (cursor_type == BAR_CURSOR && *width > 1)
28571 --*width;
28572 return cursor_type;
28575 /* Use normal cursor if not blinked off. */
28576 if (!w->cursor_off_p)
28578 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28579 return NO_CURSOR;
28580 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28582 if (cursor_type == FILLED_BOX_CURSOR)
28584 /* Using a block cursor on large images can be very annoying.
28585 So use a hollow cursor for "large" images.
28586 If image is not transparent (no mask), also use hollow cursor. */
28587 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28588 if (img != NULL && IMAGEP (img->spec))
28590 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28591 where N = size of default frame font size.
28592 This should cover most of the "tiny" icons people may use. */
28593 if (!img->mask
28594 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28595 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28596 cursor_type = HOLLOW_BOX_CURSOR;
28599 else if (cursor_type != NO_CURSOR)
28601 /* Display current only supports BOX and HOLLOW cursors for images.
28602 So for now, unconditionally use a HOLLOW cursor when cursor is
28603 not a solid box cursor. */
28604 cursor_type = HOLLOW_BOX_CURSOR;
28607 return cursor_type;
28610 /* Cursor is blinked off, so determine how to "toggle" it. */
28612 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28613 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28614 return get_specified_cursor_type (XCDR (alt_cursor), width);
28616 /* Then see if frame has specified a specific blink off cursor type. */
28617 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28619 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28620 return FRAME_BLINK_OFF_CURSOR (f);
28623 #if false
28624 /* Some people liked having a permanently visible blinking cursor,
28625 while others had very strong opinions against it. So it was
28626 decided to remove it. KFS 2003-09-03 */
28628 /* Finally perform built-in cursor blinking:
28629 filled box <-> hollow box
28630 wide [h]bar <-> narrow [h]bar
28631 narrow [h]bar <-> no cursor
28632 other type <-> no cursor */
28634 if (cursor_type == FILLED_BOX_CURSOR)
28635 return HOLLOW_BOX_CURSOR;
28637 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28639 *width = 1;
28640 return cursor_type;
28642 #endif
28644 return NO_CURSOR;
28648 /* Notice when the text cursor of window W has been completely
28649 overwritten by a drawing operation that outputs glyphs in AREA
28650 starting at X0 and ending at X1 in the line starting at Y0 and
28651 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28652 the rest of the line after X0 has been written. Y coordinates
28653 are window-relative. */
28655 static void
28656 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28657 int x0, int x1, int y0, int y1)
28659 int cx0, cx1, cy0, cy1;
28660 struct glyph_row *row;
28662 if (!w->phys_cursor_on_p)
28663 return;
28664 if (area != TEXT_AREA)
28665 return;
28667 if (w->phys_cursor.vpos < 0
28668 || w->phys_cursor.vpos >= w->current_matrix->nrows
28669 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28670 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28671 return;
28673 if (row->cursor_in_fringe_p)
28675 row->cursor_in_fringe_p = false;
28676 draw_fringe_bitmap (w, row, row->reversed_p);
28677 w->phys_cursor_on_p = false;
28678 return;
28681 cx0 = w->phys_cursor.x;
28682 cx1 = cx0 + w->phys_cursor_width;
28683 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28684 return;
28686 /* The cursor image will be completely removed from the
28687 screen if the output area intersects the cursor area in
28688 y-direction. When we draw in [y0 y1[, and some part of
28689 the cursor is at y < y0, that part must have been drawn
28690 before. When scrolling, the cursor is erased before
28691 actually scrolling, so we don't come here. When not
28692 scrolling, the rows above the old cursor row must have
28693 changed, and in this case these rows must have written
28694 over the cursor image.
28696 Likewise if part of the cursor is below y1, with the
28697 exception of the cursor being in the first blank row at
28698 the buffer and window end because update_text_area
28699 doesn't draw that row. (Except when it does, but
28700 that's handled in update_text_area.) */
28702 cy0 = w->phys_cursor.y;
28703 cy1 = cy0 + w->phys_cursor_height;
28704 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28705 return;
28707 w->phys_cursor_on_p = false;
28710 #endif /* HAVE_WINDOW_SYSTEM */
28713 /************************************************************************
28714 Mouse Face
28715 ************************************************************************/
28717 #ifdef HAVE_WINDOW_SYSTEM
28719 /* EXPORT for RIF:
28720 Fix the display of area AREA of overlapping row ROW in window W
28721 with respect to the overlapping part OVERLAPS. */
28723 void
28724 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28725 enum glyph_row_area area, int overlaps)
28727 int i, x;
28729 block_input ();
28731 x = 0;
28732 for (i = 0; i < row->used[area];)
28734 if (row->glyphs[area][i].overlaps_vertically_p)
28736 int start = i, start_x = x;
28740 x += row->glyphs[area][i].pixel_width;
28741 ++i;
28743 while (i < row->used[area]
28744 && row->glyphs[area][i].overlaps_vertically_p);
28746 draw_glyphs (w, start_x, row, area,
28747 start, i,
28748 DRAW_NORMAL_TEXT, overlaps);
28750 else
28752 x += row->glyphs[area][i].pixel_width;
28753 ++i;
28757 unblock_input ();
28761 /* EXPORT:
28762 Draw the cursor glyph of window W in glyph row ROW. See the
28763 comment of draw_glyphs for the meaning of HL. */
28765 void
28766 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28767 enum draw_glyphs_face hl)
28769 /* If cursor hpos is out of bounds, don't draw garbage. This can
28770 happen in mini-buffer windows when switching between echo area
28771 glyphs and mini-buffer. */
28772 if ((row->reversed_p
28773 ? (w->phys_cursor.hpos >= 0)
28774 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28776 bool on_p = w->phys_cursor_on_p;
28777 int x1;
28778 int hpos = w->phys_cursor.hpos;
28780 /* When the window is hscrolled, cursor hpos can legitimately be
28781 out of bounds, but we draw the cursor at the corresponding
28782 window margin in that case. */
28783 if (!row->reversed_p && hpos < 0)
28784 hpos = 0;
28785 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28786 hpos = row->used[TEXT_AREA] - 1;
28788 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28789 hl, 0);
28790 w->phys_cursor_on_p = on_p;
28792 if (hl == DRAW_CURSOR)
28793 w->phys_cursor_width = x1 - w->phys_cursor.x;
28794 /* When we erase the cursor, and ROW is overlapped by other
28795 rows, make sure that these overlapping parts of other rows
28796 are redrawn. */
28797 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28799 w->phys_cursor_width = x1 - w->phys_cursor.x;
28801 if (row > w->current_matrix->rows
28802 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28803 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28804 OVERLAPS_ERASED_CURSOR);
28806 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28807 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28808 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28809 OVERLAPS_ERASED_CURSOR);
28815 /* Erase the image of a cursor of window W from the screen. */
28817 void
28818 erase_phys_cursor (struct window *w)
28820 struct frame *f = XFRAME (w->frame);
28821 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28822 int hpos = w->phys_cursor.hpos;
28823 int vpos = w->phys_cursor.vpos;
28824 bool mouse_face_here_p = false;
28825 struct glyph_matrix *active_glyphs = w->current_matrix;
28826 struct glyph_row *cursor_row;
28827 struct glyph *cursor_glyph;
28828 enum draw_glyphs_face hl;
28830 /* No cursor displayed or row invalidated => nothing to do on the
28831 screen. */
28832 if (w->phys_cursor_type == NO_CURSOR)
28833 goto mark_cursor_off;
28835 /* VPOS >= active_glyphs->nrows means that window has been resized.
28836 Don't bother to erase the cursor. */
28837 if (vpos >= active_glyphs->nrows)
28838 goto mark_cursor_off;
28840 /* If row containing cursor is marked invalid, there is nothing we
28841 can do. */
28842 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28843 if (!cursor_row->enabled_p)
28844 goto mark_cursor_off;
28846 /* If line spacing is > 0, old cursor may only be partially visible in
28847 window after split-window. So adjust visible height. */
28848 cursor_row->visible_height = min (cursor_row->visible_height,
28849 window_text_bottom_y (w) - cursor_row->y);
28851 /* If row is completely invisible, don't attempt to delete a cursor which
28852 isn't there. This can happen if cursor is at top of a window, and
28853 we switch to a buffer with a header line in that window. */
28854 if (cursor_row->visible_height <= 0)
28855 goto mark_cursor_off;
28857 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28858 if (cursor_row->cursor_in_fringe_p)
28860 cursor_row->cursor_in_fringe_p = false;
28861 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28862 goto mark_cursor_off;
28865 /* This can happen when the new row is shorter than the old one.
28866 In this case, either draw_glyphs or clear_end_of_line
28867 should have cleared the cursor. Note that we wouldn't be
28868 able to erase the cursor in this case because we don't have a
28869 cursor glyph at hand. */
28870 if ((cursor_row->reversed_p
28871 ? (w->phys_cursor.hpos < 0)
28872 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28873 goto mark_cursor_off;
28875 /* When the window is hscrolled, cursor hpos can legitimately be out
28876 of bounds, but we draw the cursor at the corresponding window
28877 margin in that case. */
28878 if (!cursor_row->reversed_p && hpos < 0)
28879 hpos = 0;
28880 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28881 hpos = cursor_row->used[TEXT_AREA] - 1;
28883 /* If the cursor is in the mouse face area, redisplay that when
28884 we clear the cursor. */
28885 if (! NILP (hlinfo->mouse_face_window)
28886 && coords_in_mouse_face_p (w, hpos, vpos)
28887 /* Don't redraw the cursor's spot in mouse face if it is at the
28888 end of a line (on a newline). The cursor appears there, but
28889 mouse highlighting does not. */
28890 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28891 mouse_face_here_p = true;
28893 /* Maybe clear the display under the cursor. */
28894 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28896 int x, y;
28897 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28898 int width;
28900 cursor_glyph = get_phys_cursor_glyph (w);
28901 if (cursor_glyph == NULL)
28902 goto mark_cursor_off;
28904 width = cursor_glyph->pixel_width;
28905 x = w->phys_cursor.x;
28906 if (x < 0)
28908 width += x;
28909 x = 0;
28911 width = min (width, window_box_width (w, TEXT_AREA) - x);
28912 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28913 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28915 if (width > 0)
28916 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28919 /* Erase the cursor by redrawing the character underneath it. */
28920 if (mouse_face_here_p)
28921 hl = DRAW_MOUSE_FACE;
28922 else
28923 hl = DRAW_NORMAL_TEXT;
28924 draw_phys_cursor_glyph (w, cursor_row, hl);
28926 mark_cursor_off:
28927 w->phys_cursor_on_p = false;
28928 w->phys_cursor_type = NO_CURSOR;
28932 /* Display or clear cursor of window W. If !ON, clear the cursor.
28933 If ON, display the cursor; where to put the cursor is specified by
28934 HPOS, VPOS, X and Y. */
28936 void
28937 display_and_set_cursor (struct window *w, bool on,
28938 int hpos, int vpos, int x, int y)
28940 struct frame *f = XFRAME (w->frame);
28941 int new_cursor_type;
28942 int new_cursor_width;
28943 bool active_cursor;
28944 struct glyph_row *glyph_row;
28945 struct glyph *glyph;
28947 /* This is pointless on invisible frames, and dangerous on garbaged
28948 windows and frames; in the latter case, the frame or window may
28949 be in the midst of changing its size, and x and y may be off the
28950 window. */
28951 if (! FRAME_VISIBLE_P (f)
28952 || vpos >= w->current_matrix->nrows
28953 || hpos >= w->current_matrix->matrix_w)
28954 return;
28956 /* If cursor is off and we want it off, return quickly. */
28957 if (!on && !w->phys_cursor_on_p)
28958 return;
28960 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28961 /* If cursor row is not enabled, we don't really know where to
28962 display the cursor. */
28963 if (!glyph_row->enabled_p)
28965 w->phys_cursor_on_p = false;
28966 return;
28969 /* A frame might be marked garbaged even though its cursor position
28970 is correct, and will not change upon subsequent redisplay. This
28971 happens in some rare situations, like toggling the sort order in
28972 Dired windows. We've already established that VPOS is valid, so
28973 it shouldn't do any harm to record the cursor position, as we are
28974 going to return without acting on it anyway. Otherwise, expose
28975 events might come in and call update_window_cursor, which will
28976 blindly use outdated values in w->phys_cursor. */
28977 if (FRAME_GARBAGED_P (f))
28979 if (on)
28981 w->phys_cursor.x = x;
28982 w->phys_cursor.y = glyph_row->y;
28983 w->phys_cursor.hpos = hpos;
28984 w->phys_cursor.vpos = vpos;
28986 return;
28989 glyph = NULL;
28990 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28991 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28993 eassert (input_blocked_p ());
28995 /* Set new_cursor_type to the cursor we want to be displayed. */
28996 new_cursor_type = get_window_cursor_type (w, glyph,
28997 &new_cursor_width, &active_cursor);
28999 /* If cursor is currently being shown and we don't want it to be or
29000 it is in the wrong place, or the cursor type is not what we want,
29001 erase it. */
29002 if (w->phys_cursor_on_p
29003 && (!on
29004 || w->phys_cursor.x != x
29005 || w->phys_cursor.y != y
29006 /* HPOS can be negative in R2L rows whose
29007 exact_window_width_line_p flag is set (i.e. their newline
29008 would "overflow into the fringe"). */
29009 || hpos < 0
29010 || new_cursor_type != w->phys_cursor_type
29011 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29012 && new_cursor_width != w->phys_cursor_width)))
29013 erase_phys_cursor (w);
29015 /* Don't check phys_cursor_on_p here because that flag is only set
29016 to false in some cases where we know that the cursor has been
29017 completely erased, to avoid the extra work of erasing the cursor
29018 twice. In other words, phys_cursor_on_p can be true and the cursor
29019 still not be visible, or it has only been partly erased. */
29020 if (on)
29022 w->phys_cursor_ascent = glyph_row->ascent;
29023 w->phys_cursor_height = glyph_row->height;
29025 /* Set phys_cursor_.* before x_draw_.* is called because some
29026 of them may need the information. */
29027 w->phys_cursor.x = x;
29028 w->phys_cursor.y = glyph_row->y;
29029 w->phys_cursor.hpos = hpos;
29030 w->phys_cursor.vpos = vpos;
29033 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29034 new_cursor_type, new_cursor_width,
29035 on, active_cursor);
29039 /* Switch the display of W's cursor on or off, according to the value
29040 of ON. */
29042 static void
29043 update_window_cursor (struct window *w, bool on)
29045 /* Don't update cursor in windows whose frame is in the process
29046 of being deleted. */
29047 if (w->current_matrix)
29049 int hpos = w->phys_cursor.hpos;
29050 int vpos = w->phys_cursor.vpos;
29051 struct glyph_row *row;
29053 if (vpos >= w->current_matrix->nrows
29054 || hpos >= w->current_matrix->matrix_w)
29055 return;
29057 row = MATRIX_ROW (w->current_matrix, vpos);
29059 /* When the window is hscrolled, cursor hpos can legitimately be
29060 out of bounds, but we draw the cursor at the corresponding
29061 window margin in that case. */
29062 if (!row->reversed_p && hpos < 0)
29063 hpos = 0;
29064 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29065 hpos = row->used[TEXT_AREA] - 1;
29067 block_input ();
29068 display_and_set_cursor (w, on, hpos, vpos,
29069 w->phys_cursor.x, w->phys_cursor.y);
29070 unblock_input ();
29075 /* Call update_window_cursor with parameter ON_P on all leaf windows
29076 in the window tree rooted at W. */
29078 static void
29079 update_cursor_in_window_tree (struct window *w, bool on_p)
29081 while (w)
29083 if (WINDOWP (w->contents))
29084 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29085 else
29086 update_window_cursor (w, on_p);
29088 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29093 /* EXPORT:
29094 Display the cursor on window W, or clear it, according to ON_P.
29095 Don't change the cursor's position. */
29097 void
29098 x_update_cursor (struct frame *f, bool on_p)
29100 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29104 /* EXPORT:
29105 Clear the cursor of window W to background color, and mark the
29106 cursor as not shown. This is used when the text where the cursor
29107 is about to be rewritten. */
29109 void
29110 x_clear_cursor (struct window *w)
29112 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29113 update_window_cursor (w, false);
29116 #endif /* HAVE_WINDOW_SYSTEM */
29118 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29119 and MSDOS. */
29120 static void
29121 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29122 int start_hpos, int end_hpos,
29123 enum draw_glyphs_face draw)
29125 #ifdef HAVE_WINDOW_SYSTEM
29126 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29128 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29129 return;
29131 #endif
29132 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29133 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29134 #endif
29137 /* Display the active region described by mouse_face_* according to DRAW. */
29139 static void
29140 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29142 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29143 struct frame *f = XFRAME (WINDOW_FRAME (w));
29145 if (/* If window is in the process of being destroyed, don't bother
29146 to do anything. */
29147 w->current_matrix != NULL
29148 /* Don't update mouse highlight if hidden. */
29149 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29150 /* Recognize when we are called to operate on rows that don't exist
29151 anymore. This can happen when a window is split. */
29152 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29154 bool phys_cursor_on_p = w->phys_cursor_on_p;
29155 struct glyph_row *row, *first, *last;
29157 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29158 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29160 for (row = first; row <= last && row->enabled_p; ++row)
29162 int start_hpos, end_hpos, start_x;
29164 /* For all but the first row, the highlight starts at column 0. */
29165 if (row == first)
29167 /* R2L rows have BEG and END in reversed order, but the
29168 screen drawing geometry is always left to right. So
29169 we need to mirror the beginning and end of the
29170 highlighted area in R2L rows. */
29171 if (!row->reversed_p)
29173 start_hpos = hlinfo->mouse_face_beg_col;
29174 start_x = hlinfo->mouse_face_beg_x;
29176 else if (row == last)
29178 start_hpos = hlinfo->mouse_face_end_col;
29179 start_x = hlinfo->mouse_face_end_x;
29181 else
29183 start_hpos = 0;
29184 start_x = 0;
29187 else if (row->reversed_p && row == last)
29189 start_hpos = hlinfo->mouse_face_end_col;
29190 start_x = hlinfo->mouse_face_end_x;
29192 else
29194 start_hpos = 0;
29195 start_x = 0;
29198 if (row == last)
29200 if (!row->reversed_p)
29201 end_hpos = hlinfo->mouse_face_end_col;
29202 else if (row == first)
29203 end_hpos = hlinfo->mouse_face_beg_col;
29204 else
29206 end_hpos = row->used[TEXT_AREA];
29207 if (draw == DRAW_NORMAL_TEXT)
29208 row->fill_line_p = true; /* Clear to end of line. */
29211 else if (row->reversed_p && row == first)
29212 end_hpos = hlinfo->mouse_face_beg_col;
29213 else
29215 end_hpos = row->used[TEXT_AREA];
29216 if (draw == DRAW_NORMAL_TEXT)
29217 row->fill_line_p = true; /* Clear to end of line. */
29220 if (end_hpos > start_hpos)
29222 draw_row_with_mouse_face (w, start_x, row,
29223 start_hpos, end_hpos, draw);
29225 row->mouse_face_p
29226 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29230 /* When we've written over the cursor, arrange for it to
29231 be displayed again. */
29232 if (FRAME_WINDOW_P (f)
29233 && phys_cursor_on_p && !w->phys_cursor_on_p)
29235 #ifdef HAVE_WINDOW_SYSTEM
29236 int hpos = w->phys_cursor.hpos;
29238 /* When the window is hscrolled, cursor hpos can legitimately be
29239 out of bounds, but we draw the cursor at the corresponding
29240 window margin in that case. */
29241 if (!row->reversed_p && hpos < 0)
29242 hpos = 0;
29243 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29244 hpos = row->used[TEXT_AREA] - 1;
29246 block_input ();
29247 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29248 w->phys_cursor.x, w->phys_cursor.y);
29249 unblock_input ();
29250 #endif /* HAVE_WINDOW_SYSTEM */
29254 #ifdef HAVE_WINDOW_SYSTEM
29255 /* Change the mouse cursor. */
29256 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29258 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29259 if (draw == DRAW_NORMAL_TEXT
29260 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29261 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29262 else
29263 #endif
29264 if (draw == DRAW_MOUSE_FACE)
29265 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29266 else
29267 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29269 #endif /* HAVE_WINDOW_SYSTEM */
29272 /* EXPORT:
29273 Clear out the mouse-highlighted active region.
29274 Redraw it un-highlighted first. Value is true if mouse
29275 face was actually drawn unhighlighted. */
29277 bool
29278 clear_mouse_face (Mouse_HLInfo *hlinfo)
29280 bool cleared
29281 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29282 if (cleared)
29283 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29284 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29285 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29286 hlinfo->mouse_face_window = Qnil;
29287 hlinfo->mouse_face_overlay = Qnil;
29288 return cleared;
29291 /* Return true if the coordinates HPOS and VPOS on windows W are
29292 within the mouse face on that window. */
29293 static bool
29294 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29296 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29298 /* Quickly resolve the easy cases. */
29299 if (!(WINDOWP (hlinfo->mouse_face_window)
29300 && XWINDOW (hlinfo->mouse_face_window) == w))
29301 return false;
29302 if (vpos < hlinfo->mouse_face_beg_row
29303 || vpos > hlinfo->mouse_face_end_row)
29304 return false;
29305 if (vpos > hlinfo->mouse_face_beg_row
29306 && vpos < hlinfo->mouse_face_end_row)
29307 return true;
29309 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29311 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29313 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29314 return true;
29316 else if ((vpos == hlinfo->mouse_face_beg_row
29317 && hpos >= hlinfo->mouse_face_beg_col)
29318 || (vpos == hlinfo->mouse_face_end_row
29319 && hpos < hlinfo->mouse_face_end_col))
29320 return true;
29322 else
29324 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29326 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29327 return true;
29329 else if ((vpos == hlinfo->mouse_face_beg_row
29330 && hpos <= hlinfo->mouse_face_beg_col)
29331 || (vpos == hlinfo->mouse_face_end_row
29332 && hpos > hlinfo->mouse_face_end_col))
29333 return true;
29335 return false;
29339 /* EXPORT:
29340 True if physical cursor of window W is within mouse face. */
29342 bool
29343 cursor_in_mouse_face_p (struct window *w)
29345 int hpos = w->phys_cursor.hpos;
29346 int vpos = w->phys_cursor.vpos;
29347 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29349 /* When the window is hscrolled, cursor hpos can legitimately be out
29350 of bounds, but we draw the cursor at the corresponding window
29351 margin in that case. */
29352 if (!row->reversed_p && hpos < 0)
29353 hpos = 0;
29354 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29355 hpos = row->used[TEXT_AREA] - 1;
29357 return coords_in_mouse_face_p (w, hpos, vpos);
29362 /* Find the glyph rows START_ROW and END_ROW of window W that display
29363 characters between buffer positions START_CHARPOS and END_CHARPOS
29364 (excluding END_CHARPOS). DISP_STRING is a display string that
29365 covers these buffer positions. This is similar to
29366 row_containing_pos, but is more accurate when bidi reordering makes
29367 buffer positions change non-linearly with glyph rows. */
29368 static void
29369 rows_from_pos_range (struct window *w,
29370 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29371 Lisp_Object disp_string,
29372 struct glyph_row **start, struct glyph_row **end)
29374 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29375 int last_y = window_text_bottom_y (w);
29376 struct glyph_row *row;
29378 *start = NULL;
29379 *end = NULL;
29381 while (!first->enabled_p
29382 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29383 first++;
29385 /* Find the START row. */
29386 for (row = first;
29387 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29388 row++)
29390 /* A row can potentially be the START row if the range of the
29391 characters it displays intersects the range
29392 [START_CHARPOS..END_CHARPOS). */
29393 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29394 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29395 /* See the commentary in row_containing_pos, for the
29396 explanation of the complicated way to check whether
29397 some position is beyond the end of the characters
29398 displayed by a row. */
29399 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29400 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29401 && !row->ends_at_zv_p
29402 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29403 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29404 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29405 && !row->ends_at_zv_p
29406 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29408 /* Found a candidate row. Now make sure at least one of the
29409 glyphs it displays has a charpos from the range
29410 [START_CHARPOS..END_CHARPOS).
29412 This is not obvious because bidi reordering could make
29413 buffer positions of a row be 1,2,3,102,101,100, and if we
29414 want to highlight characters in [50..60), we don't want
29415 this row, even though [50..60) does intersect [1..103),
29416 the range of character positions given by the row's start
29417 and end positions. */
29418 struct glyph *g = row->glyphs[TEXT_AREA];
29419 struct glyph *e = g + row->used[TEXT_AREA];
29421 while (g < e)
29423 if (((BUFFERP (g->object) || NILP (g->object))
29424 && start_charpos <= g->charpos && g->charpos < end_charpos)
29425 /* A glyph that comes from DISP_STRING is by
29426 definition to be highlighted. */
29427 || EQ (g->object, disp_string))
29428 *start = row;
29429 g++;
29431 if (*start)
29432 break;
29436 /* Find the END row. */
29437 if (!*start
29438 /* If the last row is partially visible, start looking for END
29439 from that row, instead of starting from FIRST. */
29440 && !(row->enabled_p
29441 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29442 row = first;
29443 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29445 struct glyph_row *next = row + 1;
29446 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29448 if (!next->enabled_p
29449 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29450 /* The first row >= START whose range of displayed characters
29451 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29452 is the row END + 1. */
29453 || (start_charpos < next_start
29454 && end_charpos < next_start)
29455 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29456 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29457 && !next->ends_at_zv_p
29458 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29459 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29460 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29461 && !next->ends_at_zv_p
29462 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29464 *end = row;
29465 break;
29467 else
29469 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29470 but none of the characters it displays are in the range, it is
29471 also END + 1. */
29472 struct glyph *g = next->glyphs[TEXT_AREA];
29473 struct glyph *s = g;
29474 struct glyph *e = g + next->used[TEXT_AREA];
29476 while (g < e)
29478 if (((BUFFERP (g->object) || NILP (g->object))
29479 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29480 /* If the buffer position of the first glyph in
29481 the row is equal to END_CHARPOS, it means
29482 the last character to be highlighted is the
29483 newline of ROW, and we must consider NEXT as
29484 END, not END+1. */
29485 || (((!next->reversed_p && g == s)
29486 || (next->reversed_p && g == e - 1))
29487 && (g->charpos == end_charpos
29488 /* Special case for when NEXT is an
29489 empty line at ZV. */
29490 || (g->charpos == -1
29491 && !row->ends_at_zv_p
29492 && next_start == end_charpos)))))
29493 /* A glyph that comes from DISP_STRING is by
29494 definition to be highlighted. */
29495 || EQ (g->object, disp_string))
29496 break;
29497 g++;
29499 if (g == e)
29501 *end = row;
29502 break;
29504 /* The first row that ends at ZV must be the last to be
29505 highlighted. */
29506 else if (next->ends_at_zv_p)
29508 *end = next;
29509 break;
29515 /* This function sets the mouse_face_* elements of HLINFO, assuming
29516 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29517 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29518 for the overlay or run of text properties specifying the mouse
29519 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29520 before-string and after-string that must also be highlighted.
29521 DISP_STRING, if non-nil, is a display string that may cover some
29522 or all of the highlighted text. */
29524 static void
29525 mouse_face_from_buffer_pos (Lisp_Object window,
29526 Mouse_HLInfo *hlinfo,
29527 ptrdiff_t mouse_charpos,
29528 ptrdiff_t start_charpos,
29529 ptrdiff_t end_charpos,
29530 Lisp_Object before_string,
29531 Lisp_Object after_string,
29532 Lisp_Object disp_string)
29534 struct window *w = XWINDOW (window);
29535 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29536 struct glyph_row *r1, *r2;
29537 struct glyph *glyph, *end;
29538 ptrdiff_t ignore, pos;
29539 int x;
29541 eassert (NILP (disp_string) || STRINGP (disp_string));
29542 eassert (NILP (before_string) || STRINGP (before_string));
29543 eassert (NILP (after_string) || STRINGP (after_string));
29545 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29546 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29547 if (r1 == NULL)
29548 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29549 /* If the before-string or display-string contains newlines,
29550 rows_from_pos_range skips to its last row. Move back. */
29551 if (!NILP (before_string) || !NILP (disp_string))
29553 struct glyph_row *prev;
29554 while ((prev = r1 - 1, prev >= first)
29555 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29556 && prev->used[TEXT_AREA] > 0)
29558 struct glyph *beg = prev->glyphs[TEXT_AREA];
29559 glyph = beg + prev->used[TEXT_AREA];
29560 while (--glyph >= beg && NILP (glyph->object));
29561 if (glyph < beg
29562 || !(EQ (glyph->object, before_string)
29563 || EQ (glyph->object, disp_string)))
29564 break;
29565 r1 = prev;
29568 if (r2 == NULL)
29570 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29571 hlinfo->mouse_face_past_end = true;
29573 else if (!NILP (after_string))
29575 /* If the after-string has newlines, advance to its last row. */
29576 struct glyph_row *next;
29577 struct glyph_row *last
29578 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29580 for (next = r2 + 1;
29581 next <= last
29582 && next->used[TEXT_AREA] > 0
29583 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29584 ++next)
29585 r2 = next;
29587 /* The rest of the display engine assumes that mouse_face_beg_row is
29588 either above mouse_face_end_row or identical to it. But with
29589 bidi-reordered continued lines, the row for START_CHARPOS could
29590 be below the row for END_CHARPOS. If so, swap the rows and store
29591 them in correct order. */
29592 if (r1->y > r2->y)
29594 struct glyph_row *tem = r2;
29596 r2 = r1;
29597 r1 = tem;
29600 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29601 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29603 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29604 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29605 could be anywhere in the row and in any order. The strategy
29606 below is to find the leftmost and the rightmost glyph that
29607 belongs to either of these 3 strings, or whose position is
29608 between START_CHARPOS and END_CHARPOS, and highlight all the
29609 glyphs between those two. This may cover more than just the text
29610 between START_CHARPOS and END_CHARPOS if the range of characters
29611 strides the bidi level boundary, e.g. if the beginning is in R2L
29612 text while the end is in L2R text or vice versa. */
29613 if (!r1->reversed_p)
29615 /* This row is in a left to right paragraph. Scan it left to
29616 right. */
29617 glyph = r1->glyphs[TEXT_AREA];
29618 end = glyph + r1->used[TEXT_AREA];
29619 x = r1->x;
29621 /* Skip truncation glyphs at the start of the glyph row. */
29622 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29623 for (; glyph < end
29624 && NILP (glyph->object)
29625 && glyph->charpos < 0;
29626 ++glyph)
29627 x += glyph->pixel_width;
29629 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29630 or DISP_STRING, and the first glyph from buffer whose
29631 position is between START_CHARPOS and END_CHARPOS. */
29632 for (; glyph < end
29633 && !NILP (glyph->object)
29634 && !EQ (glyph->object, disp_string)
29635 && !(BUFFERP (glyph->object)
29636 && (glyph->charpos >= start_charpos
29637 && glyph->charpos < end_charpos));
29638 ++glyph)
29640 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29641 are present at buffer positions between START_CHARPOS and
29642 END_CHARPOS, or if they come from an overlay. */
29643 if (EQ (glyph->object, before_string))
29645 pos = string_buffer_position (before_string,
29646 start_charpos);
29647 /* If pos == 0, it means before_string came from an
29648 overlay, not from a buffer position. */
29649 if (!pos || (pos >= start_charpos && pos < end_charpos))
29650 break;
29652 else if (EQ (glyph->object, after_string))
29654 pos = string_buffer_position (after_string, end_charpos);
29655 if (!pos || (pos >= start_charpos && pos < end_charpos))
29656 break;
29658 x += glyph->pixel_width;
29660 hlinfo->mouse_face_beg_x = x;
29661 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29663 else
29665 /* This row is in a right to left paragraph. Scan it right to
29666 left. */
29667 struct glyph *g;
29669 end = r1->glyphs[TEXT_AREA] - 1;
29670 glyph = end + r1->used[TEXT_AREA];
29672 /* Skip truncation glyphs at the start of the glyph row. */
29673 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29674 for (; glyph > end
29675 && NILP (glyph->object)
29676 && glyph->charpos < 0;
29677 --glyph)
29680 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29681 or DISP_STRING, and the first glyph from buffer whose
29682 position is between START_CHARPOS and END_CHARPOS. */
29683 for (; glyph > end
29684 && !NILP (glyph->object)
29685 && !EQ (glyph->object, disp_string)
29686 && !(BUFFERP (glyph->object)
29687 && (glyph->charpos >= start_charpos
29688 && glyph->charpos < end_charpos));
29689 --glyph)
29691 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29692 are present at buffer positions between START_CHARPOS and
29693 END_CHARPOS, or if they come from an overlay. */
29694 if (EQ (glyph->object, before_string))
29696 pos = string_buffer_position (before_string, start_charpos);
29697 /* If pos == 0, it means before_string came from an
29698 overlay, not from a buffer position. */
29699 if (!pos || (pos >= start_charpos && pos < end_charpos))
29700 break;
29702 else if (EQ (glyph->object, after_string))
29704 pos = string_buffer_position (after_string, end_charpos);
29705 if (!pos || (pos >= start_charpos && pos < end_charpos))
29706 break;
29710 glyph++; /* first glyph to the right of the highlighted area */
29711 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29712 x += g->pixel_width;
29713 hlinfo->mouse_face_beg_x = x;
29714 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29717 /* If the highlight ends in a different row, compute GLYPH and END
29718 for the end row. Otherwise, reuse the values computed above for
29719 the row where the highlight begins. */
29720 if (r2 != r1)
29722 if (!r2->reversed_p)
29724 glyph = r2->glyphs[TEXT_AREA];
29725 end = glyph + r2->used[TEXT_AREA];
29726 x = r2->x;
29728 else
29730 end = r2->glyphs[TEXT_AREA] - 1;
29731 glyph = end + r2->used[TEXT_AREA];
29735 if (!r2->reversed_p)
29737 /* Skip truncation and continuation glyphs near the end of the
29738 row, and also blanks and stretch glyphs inserted by
29739 extend_face_to_end_of_line. */
29740 while (end > glyph
29741 && NILP ((end - 1)->object))
29742 --end;
29743 /* Scan the rest of the glyph row from the end, looking for the
29744 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29745 DISP_STRING, or whose position is between START_CHARPOS
29746 and END_CHARPOS */
29747 for (--end;
29748 end > glyph
29749 && !NILP (end->object)
29750 && !EQ (end->object, disp_string)
29751 && !(BUFFERP (end->object)
29752 && (end->charpos >= start_charpos
29753 && end->charpos < end_charpos));
29754 --end)
29756 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29757 are present at buffer positions between START_CHARPOS and
29758 END_CHARPOS, or if they come from an overlay. */
29759 if (EQ (end->object, before_string))
29761 pos = string_buffer_position (before_string, start_charpos);
29762 if (!pos || (pos >= start_charpos && pos < end_charpos))
29763 break;
29765 else if (EQ (end->object, after_string))
29767 pos = string_buffer_position (after_string, end_charpos);
29768 if (!pos || (pos >= start_charpos && pos < end_charpos))
29769 break;
29772 /* Find the X coordinate of the last glyph to be highlighted. */
29773 for (; glyph <= end; ++glyph)
29774 x += glyph->pixel_width;
29776 hlinfo->mouse_face_end_x = x;
29777 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29779 else
29781 /* Skip truncation and continuation glyphs near the end of the
29782 row, and also blanks and stretch glyphs inserted by
29783 extend_face_to_end_of_line. */
29784 x = r2->x;
29785 end++;
29786 while (end < glyph
29787 && NILP (end->object))
29789 x += end->pixel_width;
29790 ++end;
29792 /* Scan the rest of the glyph row from the end, looking for the
29793 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29794 DISP_STRING, or whose position is between START_CHARPOS
29795 and END_CHARPOS */
29796 for ( ;
29797 end < glyph
29798 && !NILP (end->object)
29799 && !EQ (end->object, disp_string)
29800 && !(BUFFERP (end->object)
29801 && (end->charpos >= start_charpos
29802 && end->charpos < end_charpos));
29803 ++end)
29805 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29806 are present at buffer positions between START_CHARPOS and
29807 END_CHARPOS, or if they come from an overlay. */
29808 if (EQ (end->object, before_string))
29810 pos = string_buffer_position (before_string, start_charpos);
29811 if (!pos || (pos >= start_charpos && pos < end_charpos))
29812 break;
29814 else if (EQ (end->object, after_string))
29816 pos = string_buffer_position (after_string, end_charpos);
29817 if (!pos || (pos >= start_charpos && pos < end_charpos))
29818 break;
29820 x += end->pixel_width;
29822 /* If we exited the above loop because we arrived at the last
29823 glyph of the row, and its buffer position is still not in
29824 range, it means the last character in range is the preceding
29825 newline. Bump the end column and x values to get past the
29826 last glyph. */
29827 if (end == glyph
29828 && BUFFERP (end->object)
29829 && (end->charpos < start_charpos
29830 || end->charpos >= end_charpos))
29832 x += end->pixel_width;
29833 ++end;
29835 hlinfo->mouse_face_end_x = x;
29836 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29839 hlinfo->mouse_face_window = window;
29840 hlinfo->mouse_face_face_id
29841 = face_at_buffer_position (w, mouse_charpos, &ignore,
29842 mouse_charpos + 1,
29843 !hlinfo->mouse_face_hidden, -1);
29844 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29847 /* The following function is not used anymore (replaced with
29848 mouse_face_from_string_pos), but I leave it here for the time
29849 being, in case someone would. */
29851 #if false /* not used */
29853 /* Find the position of the glyph for position POS in OBJECT in
29854 window W's current matrix, and return in *X, *Y the pixel
29855 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29857 RIGHT_P means return the position of the right edge of the glyph.
29858 !RIGHT_P means return the left edge position.
29860 If no glyph for POS exists in the matrix, return the position of
29861 the glyph with the next smaller position that is in the matrix, if
29862 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29863 exists in the matrix, return the position of the glyph with the
29864 next larger position in OBJECT.
29866 Value is true if a glyph was found. */
29868 static bool
29869 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29870 int *hpos, int *vpos, int *x, int *y, bool right_p)
29872 int yb = window_text_bottom_y (w);
29873 struct glyph_row *r;
29874 struct glyph *best_glyph = NULL;
29875 struct glyph_row *best_row = NULL;
29876 int best_x = 0;
29878 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29879 r->enabled_p && r->y < yb;
29880 ++r)
29882 struct glyph *g = r->glyphs[TEXT_AREA];
29883 struct glyph *e = g + r->used[TEXT_AREA];
29884 int gx;
29886 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29887 if (EQ (g->object, object))
29889 if (g->charpos == pos)
29891 best_glyph = g;
29892 best_x = gx;
29893 best_row = r;
29894 goto found;
29896 else if (best_glyph == NULL
29897 || ((eabs (g->charpos - pos)
29898 < eabs (best_glyph->charpos - pos))
29899 && (right_p
29900 ? g->charpos < pos
29901 : g->charpos > pos)))
29903 best_glyph = g;
29904 best_x = gx;
29905 best_row = r;
29910 found:
29912 if (best_glyph)
29914 *x = best_x;
29915 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29917 if (right_p)
29919 *x += best_glyph->pixel_width;
29920 ++*hpos;
29923 *y = best_row->y;
29924 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29927 return best_glyph != NULL;
29929 #endif /* not used */
29931 /* Find the positions of the first and the last glyphs in window W's
29932 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29933 (assumed to be a string), and return in HLINFO's mouse_face_*
29934 members the pixel and column/row coordinates of those glyphs. */
29936 static void
29937 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29938 Lisp_Object object,
29939 ptrdiff_t startpos, ptrdiff_t endpos)
29941 int yb = window_text_bottom_y (w);
29942 struct glyph_row *r;
29943 struct glyph *g, *e;
29944 int gx;
29945 bool found = false;
29947 /* Find the glyph row with at least one position in the range
29948 [STARTPOS..ENDPOS), and the first glyph in that row whose
29949 position belongs to that range. */
29950 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29951 r->enabled_p && r->y < yb;
29952 ++r)
29954 if (!r->reversed_p)
29956 g = r->glyphs[TEXT_AREA];
29957 e = g + r->used[TEXT_AREA];
29958 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29959 if (EQ (g->object, object)
29960 && startpos <= g->charpos && g->charpos < endpos)
29962 hlinfo->mouse_face_beg_row
29963 = MATRIX_ROW_VPOS (r, w->current_matrix);
29964 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29965 hlinfo->mouse_face_beg_x = gx;
29966 found = true;
29967 break;
29970 else
29972 struct glyph *g1;
29974 e = r->glyphs[TEXT_AREA];
29975 g = e + r->used[TEXT_AREA];
29976 for ( ; g > e; --g)
29977 if (EQ ((g-1)->object, object)
29978 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29980 hlinfo->mouse_face_beg_row
29981 = MATRIX_ROW_VPOS (r, w->current_matrix);
29982 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29983 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29984 gx += g1->pixel_width;
29985 hlinfo->mouse_face_beg_x = gx;
29986 found = true;
29987 break;
29990 if (found)
29991 break;
29994 if (!found)
29995 return;
29997 /* Starting with the next row, look for the first row which does NOT
29998 include any glyphs whose positions are in the range. */
29999 for (++r; r->enabled_p && r->y < yb; ++r)
30001 g = r->glyphs[TEXT_AREA];
30002 e = g + r->used[TEXT_AREA];
30003 found = false;
30004 for ( ; g < e; ++g)
30005 if (EQ (g->object, object)
30006 && startpos <= g->charpos && g->charpos < endpos)
30008 found = true;
30009 break;
30011 if (!found)
30012 break;
30015 /* The highlighted region ends on the previous row. */
30016 r--;
30018 /* Set the end row. */
30019 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30021 /* Compute and set the end column and the end column's horizontal
30022 pixel coordinate. */
30023 if (!r->reversed_p)
30025 g = r->glyphs[TEXT_AREA];
30026 e = g + r->used[TEXT_AREA];
30027 for ( ; e > g; --e)
30028 if (EQ ((e-1)->object, object)
30029 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30030 break;
30031 hlinfo->mouse_face_end_col = e - g;
30033 for (gx = r->x; g < e; ++g)
30034 gx += g->pixel_width;
30035 hlinfo->mouse_face_end_x = gx;
30037 else
30039 e = r->glyphs[TEXT_AREA];
30040 g = e + r->used[TEXT_AREA];
30041 for (gx = r->x ; e < g; ++e)
30043 if (EQ (e->object, object)
30044 && startpos <= e->charpos && e->charpos < endpos)
30045 break;
30046 gx += e->pixel_width;
30048 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30049 hlinfo->mouse_face_end_x = gx;
30053 #ifdef HAVE_WINDOW_SYSTEM
30055 /* See if position X, Y is within a hot-spot of an image. */
30057 static bool
30058 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30060 if (!CONSP (hot_spot))
30061 return false;
30063 if (EQ (XCAR (hot_spot), Qrect))
30065 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30066 Lisp_Object rect = XCDR (hot_spot);
30067 Lisp_Object tem;
30068 if (!CONSP (rect))
30069 return false;
30070 if (!CONSP (XCAR (rect)))
30071 return false;
30072 if (!CONSP (XCDR (rect)))
30073 return false;
30074 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30075 return false;
30076 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30077 return false;
30078 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30079 return false;
30080 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30081 return false;
30082 return true;
30084 else if (EQ (XCAR (hot_spot), Qcircle))
30086 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30087 Lisp_Object circ = XCDR (hot_spot);
30088 Lisp_Object lr, lx0, ly0;
30089 if (CONSP (circ)
30090 && CONSP (XCAR (circ))
30091 && (lr = XCDR (circ), NUMBERP (lr))
30092 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30093 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30095 double r = XFLOATINT (lr);
30096 double dx = XINT (lx0) - x;
30097 double dy = XINT (ly0) - y;
30098 return (dx * dx + dy * dy <= r * r);
30101 else if (EQ (XCAR (hot_spot), Qpoly))
30103 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30104 if (VECTORP (XCDR (hot_spot)))
30106 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30107 Lisp_Object *poly = v->contents;
30108 ptrdiff_t n = v->header.size;
30109 ptrdiff_t i;
30110 bool inside = false;
30111 Lisp_Object lx, ly;
30112 int x0, y0;
30114 /* Need an even number of coordinates, and at least 3 edges. */
30115 if (n < 6 || n & 1)
30116 return false;
30118 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30119 If count is odd, we are inside polygon. Pixels on edges
30120 may or may not be included depending on actual geometry of the
30121 polygon. */
30122 if ((lx = poly[n-2], !INTEGERP (lx))
30123 || (ly = poly[n-1], !INTEGERP (lx)))
30124 return false;
30125 x0 = XINT (lx), y0 = XINT (ly);
30126 for (i = 0; i < n; i += 2)
30128 int x1 = x0, y1 = y0;
30129 if ((lx = poly[i], !INTEGERP (lx))
30130 || (ly = poly[i+1], !INTEGERP (ly)))
30131 return false;
30132 x0 = XINT (lx), y0 = XINT (ly);
30134 /* Does this segment cross the X line? */
30135 if (x0 >= x)
30137 if (x1 >= x)
30138 continue;
30140 else if (x1 < x)
30141 continue;
30142 if (y > y0 && y > y1)
30143 continue;
30144 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30145 inside = !inside;
30147 return inside;
30150 return false;
30153 Lisp_Object
30154 find_hot_spot (Lisp_Object map, int x, int y)
30156 while (CONSP (map))
30158 if (CONSP (XCAR (map))
30159 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30160 return XCAR (map);
30161 map = XCDR (map);
30164 return Qnil;
30167 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30168 3, 3, 0,
30169 doc: /* Lookup in image map MAP coordinates X and Y.
30170 An image map is an alist where each element has the format (AREA ID PLIST).
30171 An AREA is specified as either a rectangle, a circle, or a polygon:
30172 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30173 pixel coordinates of the upper left and bottom right corners.
30174 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30175 and the radius of the circle; r may be a float or integer.
30176 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30177 vector describes one corner in the polygon.
30178 Returns the alist element for the first matching AREA in MAP. */)
30179 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30181 if (NILP (map))
30182 return Qnil;
30184 CHECK_NUMBER (x);
30185 CHECK_NUMBER (y);
30187 return find_hot_spot (map,
30188 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30189 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30191 #endif /* HAVE_WINDOW_SYSTEM */
30194 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30195 static void
30196 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30198 #ifdef HAVE_WINDOW_SYSTEM
30199 if (!FRAME_WINDOW_P (f))
30200 return;
30202 /* Do not change cursor shape while dragging mouse. */
30203 if (EQ (do_mouse_tracking, Qdragging))
30204 return;
30206 if (!NILP (pointer))
30208 if (EQ (pointer, Qarrow))
30209 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30210 else if (EQ (pointer, Qhand))
30211 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30212 else if (EQ (pointer, Qtext))
30213 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30214 else if (EQ (pointer, intern ("hdrag")))
30215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30216 else if (EQ (pointer, intern ("nhdrag")))
30217 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30218 # ifdef HAVE_X_WINDOWS
30219 else if (EQ (pointer, intern ("vdrag")))
30220 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30221 # endif
30222 else if (EQ (pointer, intern ("hourglass")))
30223 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30224 else if (EQ (pointer, Qmodeline))
30225 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30226 else
30227 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30230 if (cursor != No_Cursor)
30231 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30232 #endif
30235 /* Take proper action when mouse has moved to the mode or header line
30236 or marginal area AREA of window W, x-position X and y-position Y.
30237 X is relative to the start of the text display area of W, so the
30238 width of bitmap areas and scroll bars must be subtracted to get a
30239 position relative to the start of the mode line. */
30241 static void
30242 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30243 enum window_part area)
30245 struct window *w = XWINDOW (window);
30246 struct frame *f = XFRAME (w->frame);
30247 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30248 #ifdef HAVE_WINDOW_SYSTEM
30249 Display_Info *dpyinfo;
30250 #endif
30251 Cursor cursor = No_Cursor;
30252 Lisp_Object pointer = Qnil;
30253 int dx, dy, width, height;
30254 ptrdiff_t charpos;
30255 Lisp_Object string, object = Qnil;
30256 Lisp_Object pos UNINIT;
30257 Lisp_Object mouse_face;
30258 int original_x_pixel = x;
30259 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30260 struct glyph_row *row UNINIT;
30262 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30264 int x0;
30265 struct glyph *end;
30267 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30268 returns them in row/column units! */
30269 string = mode_line_string (w, area, &x, &y, &charpos,
30270 &object, &dx, &dy, &width, &height);
30272 row = (area == ON_MODE_LINE
30273 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30274 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30276 /* Find the glyph under the mouse pointer. */
30277 if (row->mode_line_p && row->enabled_p)
30279 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30280 end = glyph + row->used[TEXT_AREA];
30282 for (x0 = original_x_pixel;
30283 glyph < end && x0 >= glyph->pixel_width;
30284 ++glyph)
30285 x0 -= glyph->pixel_width;
30287 if (glyph >= end)
30288 glyph = NULL;
30291 else
30293 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30294 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30295 returns them in row/column units! */
30296 string = marginal_area_string (w, area, &x, &y, &charpos,
30297 &object, &dx, &dy, &width, &height);
30300 Lisp_Object help = Qnil;
30302 #ifdef HAVE_WINDOW_SYSTEM
30303 if (IMAGEP (object))
30305 Lisp_Object image_map, hotspot;
30306 if ((image_map = Fplist_get (XCDR (object), QCmap),
30307 !NILP (image_map))
30308 && (hotspot = find_hot_spot (image_map, dx, dy),
30309 CONSP (hotspot))
30310 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30312 Lisp_Object plist;
30314 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30315 If so, we could look for mouse-enter, mouse-leave
30316 properties in PLIST (and do something...). */
30317 hotspot = XCDR (hotspot);
30318 if (CONSP (hotspot)
30319 && (plist = XCAR (hotspot), CONSP (plist)))
30321 pointer = Fplist_get (plist, Qpointer);
30322 if (NILP (pointer))
30323 pointer = Qhand;
30324 help = Fplist_get (plist, Qhelp_echo);
30325 if (!NILP (help))
30327 help_echo_string = help;
30328 XSETWINDOW (help_echo_window, w);
30329 help_echo_object = w->contents;
30330 help_echo_pos = charpos;
30334 if (NILP (pointer))
30335 pointer = Fplist_get (XCDR (object), QCpointer);
30337 #endif /* HAVE_WINDOW_SYSTEM */
30339 if (STRINGP (string))
30340 pos = make_number (charpos);
30342 /* Set the help text and mouse pointer. If the mouse is on a part
30343 of the mode line without any text (e.g. past the right edge of
30344 the mode line text), use the default help text and pointer. */
30345 if (STRINGP (string) || area == ON_MODE_LINE)
30347 /* Arrange to display the help by setting the global variables
30348 help_echo_string, help_echo_object, and help_echo_pos. */
30349 if (NILP (help))
30351 if (STRINGP (string))
30352 help = Fget_text_property (pos, Qhelp_echo, string);
30354 if (!NILP (help))
30356 help_echo_string = help;
30357 XSETWINDOW (help_echo_window, w);
30358 help_echo_object = string;
30359 help_echo_pos = charpos;
30361 else if (area == ON_MODE_LINE)
30363 Lisp_Object default_help
30364 = buffer_local_value (Qmode_line_default_help_echo,
30365 w->contents);
30367 if (STRINGP (default_help))
30369 help_echo_string = default_help;
30370 XSETWINDOW (help_echo_window, w);
30371 help_echo_object = Qnil;
30372 help_echo_pos = -1;
30377 #ifdef HAVE_WINDOW_SYSTEM
30378 /* Change the mouse pointer according to what is under it. */
30379 if (FRAME_WINDOW_P (f))
30381 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30382 || minibuf_level
30383 || NILP (Vresize_mini_windows));
30385 dpyinfo = FRAME_DISPLAY_INFO (f);
30386 if (STRINGP (string))
30388 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30390 if (NILP (pointer))
30391 pointer = Fget_text_property (pos, Qpointer, string);
30393 /* Change the mouse pointer according to what is under X/Y. */
30394 if (NILP (pointer)
30395 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30397 Lisp_Object map;
30398 map = Fget_text_property (pos, Qlocal_map, string);
30399 if (!KEYMAPP (map))
30400 map = Fget_text_property (pos, Qkeymap, string);
30401 if (!KEYMAPP (map) && draggable)
30402 cursor = dpyinfo->vertical_scroll_bar_cursor;
30405 else if (draggable)
30406 /* Default mode-line pointer. */
30407 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30409 #endif
30412 /* Change the mouse face according to what is under X/Y. */
30413 bool mouse_face_shown = false;
30414 if (STRINGP (string))
30416 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30417 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30418 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30419 && glyph)
30421 Lisp_Object b, e;
30423 struct glyph * tmp_glyph;
30425 int gpos;
30426 int gseq_length;
30427 int total_pixel_width;
30428 ptrdiff_t begpos, endpos, ignore;
30430 int vpos, hpos;
30432 b = Fprevious_single_property_change (make_number (charpos + 1),
30433 Qmouse_face, string, Qnil);
30434 if (NILP (b))
30435 begpos = 0;
30436 else
30437 begpos = XINT (b);
30439 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30440 if (NILP (e))
30441 endpos = SCHARS (string);
30442 else
30443 endpos = XINT (e);
30445 /* Calculate the glyph position GPOS of GLYPH in the
30446 displayed string, relative to the beginning of the
30447 highlighted part of the string.
30449 Note: GPOS is different from CHARPOS. CHARPOS is the
30450 position of GLYPH in the internal string object. A mode
30451 line string format has structures which are converted to
30452 a flattened string by the Emacs Lisp interpreter. The
30453 internal string is an element of those structures. The
30454 displayed string is the flattened string. */
30455 tmp_glyph = row_start_glyph;
30456 while (tmp_glyph < glyph
30457 && (!(EQ (tmp_glyph->object, glyph->object)
30458 && begpos <= tmp_glyph->charpos
30459 && tmp_glyph->charpos < endpos)))
30460 tmp_glyph++;
30461 gpos = glyph - tmp_glyph;
30463 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30464 the highlighted part of the displayed string to which
30465 GLYPH belongs. Note: GSEQ_LENGTH is different from
30466 SCHARS (STRING), because the latter returns the length of
30467 the internal string. */
30468 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30469 tmp_glyph > glyph
30470 && (!(EQ (tmp_glyph->object, glyph->object)
30471 && begpos <= tmp_glyph->charpos
30472 && tmp_glyph->charpos < endpos));
30473 tmp_glyph--)
30475 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30477 /* Calculate the total pixel width of all the glyphs between
30478 the beginning of the highlighted area and GLYPH. */
30479 total_pixel_width = 0;
30480 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30481 total_pixel_width += tmp_glyph->pixel_width;
30483 /* Pre calculation of re-rendering position. Note: X is in
30484 column units here, after the call to mode_line_string or
30485 marginal_area_string. */
30486 hpos = x - gpos;
30487 vpos = (area == ON_MODE_LINE
30488 ? (w->current_matrix)->nrows - 1
30489 : 0);
30491 /* If GLYPH's position is included in the region that is
30492 already drawn in mouse face, we have nothing to do. */
30493 if ( EQ (window, hlinfo->mouse_face_window)
30494 && (!row->reversed_p
30495 ? (hlinfo->mouse_face_beg_col <= hpos
30496 && hpos < hlinfo->mouse_face_end_col)
30497 /* In R2L rows we swap BEG and END, see below. */
30498 : (hlinfo->mouse_face_end_col <= hpos
30499 && hpos < hlinfo->mouse_face_beg_col))
30500 && hlinfo->mouse_face_beg_row == vpos )
30501 return;
30503 if (clear_mouse_face (hlinfo))
30504 cursor = No_Cursor;
30506 if (!row->reversed_p)
30508 hlinfo->mouse_face_beg_col = hpos;
30509 hlinfo->mouse_face_beg_x = original_x_pixel
30510 - (total_pixel_width + dx);
30511 hlinfo->mouse_face_end_col = hpos + gseq_length;
30512 hlinfo->mouse_face_end_x = 0;
30514 else
30516 /* In R2L rows, show_mouse_face expects BEG and END
30517 coordinates to be swapped. */
30518 hlinfo->mouse_face_end_col = hpos;
30519 hlinfo->mouse_face_end_x = original_x_pixel
30520 - (total_pixel_width + dx);
30521 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30522 hlinfo->mouse_face_beg_x = 0;
30525 hlinfo->mouse_face_beg_row = vpos;
30526 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30527 hlinfo->mouse_face_past_end = false;
30528 hlinfo->mouse_face_window = window;
30530 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30531 charpos,
30532 0, &ignore,
30533 glyph->face_id,
30534 true);
30535 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30536 mouse_face_shown = true;
30538 if (NILP (pointer))
30539 pointer = Qhand;
30543 /* If mouse-face doesn't need to be shown, clear any existing
30544 mouse-face. */
30545 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30546 clear_mouse_face (hlinfo);
30548 define_frame_cursor1 (f, cursor, pointer);
30552 /* EXPORT:
30553 Take proper action when the mouse has moved to position X, Y on
30554 frame F with regards to highlighting portions of display that have
30555 mouse-face properties. Also de-highlight portions of display where
30556 the mouse was before, set the mouse pointer shape as appropriate
30557 for the mouse coordinates, and activate help echo (tooltips).
30558 X and Y can be negative or out of range. */
30560 void
30561 note_mouse_highlight (struct frame *f, int x, int y)
30563 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30564 enum window_part part = ON_NOTHING;
30565 Lisp_Object window;
30566 struct window *w;
30567 Cursor cursor = No_Cursor;
30568 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30569 struct buffer *b;
30571 /* When a menu is active, don't highlight because this looks odd. */
30572 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30573 if (popup_activated ())
30574 return;
30575 #endif
30577 if (!f->glyphs_initialized_p
30578 || f->pointer_invisible)
30579 return;
30581 hlinfo->mouse_face_mouse_x = x;
30582 hlinfo->mouse_face_mouse_y = y;
30583 hlinfo->mouse_face_mouse_frame = f;
30585 if (hlinfo->mouse_face_defer)
30586 return;
30588 /* Which window is that in? */
30589 window = window_from_coordinates (f, x, y, &part, true);
30591 /* If displaying active text in another window, clear that. */
30592 if (! EQ (window, hlinfo->mouse_face_window)
30593 /* Also clear if we move out of text area in same window. */
30594 || (!NILP (hlinfo->mouse_face_window)
30595 && !NILP (window)
30596 && part != ON_TEXT
30597 && part != ON_MODE_LINE
30598 && part != ON_HEADER_LINE))
30599 clear_mouse_face (hlinfo);
30601 /* Not on a window -> return. */
30602 if (!WINDOWP (window))
30603 return;
30605 /* Reset help_echo_string. It will get recomputed below. */
30606 help_echo_string = Qnil;
30608 /* Convert to window-relative pixel coordinates. */
30609 w = XWINDOW (window);
30610 frame_to_window_pixel_xy (w, &x, &y);
30612 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30613 /* Handle tool-bar window differently since it doesn't display a
30614 buffer. */
30615 if (EQ (window, f->tool_bar_window))
30617 note_tool_bar_highlight (f, x, y);
30618 return;
30620 #endif
30622 /* Mouse is on the mode, header line or margin? */
30623 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30624 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30626 note_mode_line_or_margin_highlight (window, x, y, part);
30628 #ifdef HAVE_WINDOW_SYSTEM
30629 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30631 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30632 /* Show non-text cursor (Bug#16647). */
30633 goto set_cursor;
30635 else
30636 #endif
30637 return;
30640 #ifdef HAVE_WINDOW_SYSTEM
30641 if (part == ON_VERTICAL_BORDER)
30643 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30644 help_echo_string = build_string ("drag-mouse-1: resize");
30646 else if (part == ON_RIGHT_DIVIDER)
30648 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30649 help_echo_string = build_string ("drag-mouse-1: resize");
30651 else if (part == ON_BOTTOM_DIVIDER)
30652 if (! WINDOW_BOTTOMMOST_P (w)
30653 || minibuf_level
30654 || NILP (Vresize_mini_windows))
30656 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30657 help_echo_string = build_string ("drag-mouse-1: resize");
30659 else
30660 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30661 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30662 || part == ON_VERTICAL_SCROLL_BAR
30663 || part == ON_HORIZONTAL_SCROLL_BAR)
30664 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30665 else
30666 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30667 #endif
30669 /* Are we in a window whose display is up to date?
30670 And verify the buffer's text has not changed. */
30671 b = XBUFFER (w->contents);
30672 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30674 int hpos, vpos, dx, dy, area = LAST_AREA;
30675 ptrdiff_t pos;
30676 struct glyph *glyph;
30677 Lisp_Object object;
30678 Lisp_Object mouse_face = Qnil, position;
30679 Lisp_Object *overlay_vec = NULL;
30680 ptrdiff_t i, noverlays;
30681 struct buffer *obuf;
30682 ptrdiff_t obegv, ozv;
30683 bool same_region;
30685 /* Find the glyph under X/Y. */
30686 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30688 #ifdef HAVE_WINDOW_SYSTEM
30689 /* Look for :pointer property on image. */
30690 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30692 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30693 if (img != NULL && IMAGEP (img->spec))
30695 Lisp_Object image_map, hotspot;
30696 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30697 !NILP (image_map))
30698 && (hotspot = find_hot_spot (image_map,
30699 glyph->slice.img.x + dx,
30700 glyph->slice.img.y + dy),
30701 CONSP (hotspot))
30702 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30704 Lisp_Object plist;
30706 /* Could check XCAR (hotspot) to see if we enter/leave
30707 this hot-spot.
30708 If so, we could look for mouse-enter, mouse-leave
30709 properties in PLIST (and do something...). */
30710 hotspot = XCDR (hotspot);
30711 if (CONSP (hotspot)
30712 && (plist = XCAR (hotspot), CONSP (plist)))
30714 pointer = Fplist_get (plist, Qpointer);
30715 if (NILP (pointer))
30716 pointer = Qhand;
30717 help_echo_string = Fplist_get (plist, Qhelp_echo);
30718 if (!NILP (help_echo_string))
30720 help_echo_window = window;
30721 help_echo_object = glyph->object;
30722 help_echo_pos = glyph->charpos;
30726 if (NILP (pointer))
30727 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30730 #endif /* HAVE_WINDOW_SYSTEM */
30732 /* Clear mouse face if X/Y not over text. */
30733 if (glyph == NULL
30734 || area != TEXT_AREA
30735 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30736 /* Glyph's OBJECT is nil for glyphs inserted by the
30737 display engine for its internal purposes, like truncation
30738 and continuation glyphs and blanks beyond the end of
30739 line's text on text terminals. If we are over such a
30740 glyph, we are not over any text. */
30741 || NILP (glyph->object)
30742 /* R2L rows have a stretch glyph at their front, which
30743 stands for no text, whereas L2R rows have no glyphs at
30744 all beyond the end of text. Treat such stretch glyphs
30745 like we do with NULL glyphs in L2R rows. */
30746 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30747 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30748 && glyph->type == STRETCH_GLYPH
30749 && glyph->avoid_cursor_p))
30751 if (clear_mouse_face (hlinfo))
30752 cursor = No_Cursor;
30753 if (FRAME_WINDOW_P (f) && NILP (pointer))
30755 #ifdef HAVE_WINDOW_SYSTEM
30756 if (area != TEXT_AREA)
30757 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30758 else
30759 pointer = Vvoid_text_area_pointer;
30760 #endif
30762 goto set_cursor;
30765 pos = glyph->charpos;
30766 object = glyph->object;
30767 if (!STRINGP (object) && !BUFFERP (object))
30768 goto set_cursor;
30770 /* If we get an out-of-range value, return now; avoid an error. */
30771 if (BUFFERP (object) && pos > BUF_Z (b))
30772 goto set_cursor;
30774 /* Make the window's buffer temporarily current for
30775 overlays_at and compute_char_face. */
30776 obuf = current_buffer;
30777 current_buffer = b;
30778 obegv = BEGV;
30779 ozv = ZV;
30780 BEGV = BEG;
30781 ZV = Z;
30783 /* Is this char mouse-active or does it have help-echo? */
30784 position = make_number (pos);
30786 USE_SAFE_ALLOCA;
30788 if (BUFFERP (object))
30790 /* Put all the overlays we want in a vector in overlay_vec. */
30791 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30792 /* Sort overlays into increasing priority order. */
30793 noverlays = sort_overlays (overlay_vec, noverlays, w);
30795 else
30796 noverlays = 0;
30798 if (NILP (Vmouse_highlight))
30800 clear_mouse_face (hlinfo);
30801 goto check_help_echo;
30804 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30806 if (same_region)
30807 cursor = No_Cursor;
30809 /* Check mouse-face highlighting. */
30810 if (! same_region
30811 /* If there exists an overlay with mouse-face overlapping
30812 the one we are currently highlighting, we have to
30813 check if we enter the overlapping overlay, and then
30814 highlight only that. */
30815 || (OVERLAYP (hlinfo->mouse_face_overlay)
30816 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30818 /* Find the highest priority overlay with a mouse-face. */
30819 Lisp_Object overlay = Qnil;
30820 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30822 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30823 if (!NILP (mouse_face))
30824 overlay = overlay_vec[i];
30827 /* If we're highlighting the same overlay as before, there's
30828 no need to do that again. */
30829 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30830 goto check_help_echo;
30832 /* Clear the display of the old active region, if any. */
30833 if (clear_mouse_face (hlinfo))
30834 cursor = No_Cursor;
30836 /* Record the overlay, if any, to be highlighted. */
30837 hlinfo->mouse_face_overlay = overlay;
30839 /* If no overlay applies, get a text property. */
30840 if (NILP (overlay))
30841 mouse_face = Fget_text_property (position, Qmouse_face, object);
30843 /* Next, compute the bounds of the mouse highlighting and
30844 display it. */
30845 if (!NILP (mouse_face) && STRINGP (object))
30847 /* The mouse-highlighting comes from a display string
30848 with a mouse-face. */
30849 Lisp_Object s, e;
30850 ptrdiff_t ignore;
30852 s = Fprevious_single_property_change
30853 (make_number (pos + 1), Qmouse_face, object, Qnil);
30854 e = Fnext_single_property_change
30855 (position, Qmouse_face, object, Qnil);
30856 if (NILP (s))
30857 s = make_number (0);
30858 if (NILP (e))
30859 e = make_number (SCHARS (object));
30860 mouse_face_from_string_pos (w, hlinfo, object,
30861 XINT (s), XINT (e));
30862 hlinfo->mouse_face_past_end = false;
30863 hlinfo->mouse_face_window = window;
30864 hlinfo->mouse_face_face_id
30865 = face_at_string_position (w, object, pos, 0, &ignore,
30866 glyph->face_id, true);
30867 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30868 cursor = No_Cursor;
30870 else
30872 /* The mouse-highlighting, if any, comes from an overlay
30873 or text property in the buffer. */
30874 Lisp_Object buffer UNINIT;
30875 Lisp_Object disp_string UNINIT;
30877 if (STRINGP (object))
30879 /* If we are on a display string with no mouse-face,
30880 check if the text under it has one. */
30881 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30882 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30883 pos = string_buffer_position (object, start);
30884 if (pos > 0)
30886 mouse_face = get_char_property_and_overlay
30887 (make_number (pos), Qmouse_face, w->contents, &overlay);
30888 buffer = w->contents;
30889 disp_string = object;
30892 else
30894 buffer = object;
30895 disp_string = Qnil;
30898 if (!NILP (mouse_face))
30900 Lisp_Object before, after;
30901 Lisp_Object before_string, after_string;
30902 /* To correctly find the limits of mouse highlight
30903 in a bidi-reordered buffer, we must not use the
30904 optimization of limiting the search in
30905 previous-single-property-change and
30906 next-single-property-change, because
30907 rows_from_pos_range needs the real start and end
30908 positions to DTRT in this case. That's because
30909 the first row visible in a window does not
30910 necessarily display the character whose position
30911 is the smallest. */
30912 Lisp_Object lim1
30913 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30914 ? Fmarker_position (w->start)
30915 : Qnil;
30916 Lisp_Object lim2
30917 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30918 ? make_number (BUF_Z (XBUFFER (buffer))
30919 - w->window_end_pos)
30920 : Qnil;
30922 if (NILP (overlay))
30924 /* Handle the text property case. */
30925 before = Fprevious_single_property_change
30926 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30927 after = Fnext_single_property_change
30928 (make_number (pos), Qmouse_face, buffer, lim2);
30929 before_string = after_string = Qnil;
30931 else
30933 /* Handle the overlay case. */
30934 before = Foverlay_start (overlay);
30935 after = Foverlay_end (overlay);
30936 before_string = Foverlay_get (overlay, Qbefore_string);
30937 after_string = Foverlay_get (overlay, Qafter_string);
30939 if (!STRINGP (before_string)) before_string = Qnil;
30940 if (!STRINGP (after_string)) after_string = Qnil;
30943 mouse_face_from_buffer_pos (window, hlinfo, pos,
30944 NILP (before)
30946 : XFASTINT (before),
30947 NILP (after)
30948 ? BUF_Z (XBUFFER (buffer))
30949 : XFASTINT (after),
30950 before_string, after_string,
30951 disp_string);
30952 cursor = No_Cursor;
30957 check_help_echo:
30959 /* Look for a `help-echo' property. */
30960 if (NILP (help_echo_string)) {
30961 Lisp_Object help, overlay;
30963 /* Check overlays first. */
30964 help = overlay = Qnil;
30965 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30967 overlay = overlay_vec[i];
30968 help = Foverlay_get (overlay, Qhelp_echo);
30971 if (!NILP (help))
30973 help_echo_string = help;
30974 help_echo_window = window;
30975 help_echo_object = overlay;
30976 help_echo_pos = pos;
30978 else
30980 Lisp_Object obj = glyph->object;
30981 ptrdiff_t charpos = glyph->charpos;
30983 /* Try text properties. */
30984 if (STRINGP (obj)
30985 && charpos >= 0
30986 && charpos < SCHARS (obj))
30988 help = Fget_text_property (make_number (charpos),
30989 Qhelp_echo, obj);
30990 if (NILP (help))
30992 /* If the string itself doesn't specify a help-echo,
30993 see if the buffer text ``under'' it does. */
30994 struct glyph_row *r
30995 = MATRIX_ROW (w->current_matrix, vpos);
30996 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30997 ptrdiff_t p = string_buffer_position (obj, start);
30998 if (p > 0)
31000 help = Fget_char_property (make_number (p),
31001 Qhelp_echo, w->contents);
31002 if (!NILP (help))
31004 charpos = p;
31005 obj = w->contents;
31010 else if (BUFFERP (obj)
31011 && charpos >= BEGV
31012 && charpos < ZV)
31013 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31014 obj);
31016 if (!NILP (help))
31018 help_echo_string = help;
31019 help_echo_window = window;
31020 help_echo_object = obj;
31021 help_echo_pos = charpos;
31026 #ifdef HAVE_WINDOW_SYSTEM
31027 /* Look for a `pointer' property. */
31028 if (FRAME_WINDOW_P (f) && NILP (pointer))
31030 /* Check overlays first. */
31031 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31032 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31034 if (NILP (pointer))
31036 Lisp_Object obj = glyph->object;
31037 ptrdiff_t charpos = glyph->charpos;
31039 /* Try text properties. */
31040 if (STRINGP (obj)
31041 && charpos >= 0
31042 && charpos < SCHARS (obj))
31044 pointer = Fget_text_property (make_number (charpos),
31045 Qpointer, obj);
31046 if (NILP (pointer))
31048 /* If the string itself doesn't specify a pointer,
31049 see if the buffer text ``under'' it does. */
31050 struct glyph_row *r
31051 = MATRIX_ROW (w->current_matrix, vpos);
31052 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31053 ptrdiff_t p = string_buffer_position (obj, start);
31054 if (p > 0)
31055 pointer = Fget_char_property (make_number (p),
31056 Qpointer, w->contents);
31059 else if (BUFFERP (obj)
31060 && charpos >= BEGV
31061 && charpos < ZV)
31062 pointer = Fget_text_property (make_number (charpos),
31063 Qpointer, obj);
31066 #endif /* HAVE_WINDOW_SYSTEM */
31068 BEGV = obegv;
31069 ZV = ozv;
31070 current_buffer = obuf;
31071 SAFE_FREE ();
31074 set_cursor:
31075 define_frame_cursor1 (f, cursor, pointer);
31079 /* EXPORT for RIF:
31080 Clear any mouse-face on window W. This function is part of the
31081 redisplay interface, and is called from try_window_id and similar
31082 functions to ensure the mouse-highlight is off. */
31084 void
31085 x_clear_window_mouse_face (struct window *w)
31087 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31088 Lisp_Object window;
31090 block_input ();
31091 XSETWINDOW (window, w);
31092 if (EQ (window, hlinfo->mouse_face_window))
31093 clear_mouse_face (hlinfo);
31094 unblock_input ();
31098 /* EXPORT:
31099 Just discard the mouse face information for frame F, if any.
31100 This is used when the size of F is changed. */
31102 void
31103 cancel_mouse_face (struct frame *f)
31105 Lisp_Object window;
31106 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31108 window = hlinfo->mouse_face_window;
31109 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31110 reset_mouse_highlight (hlinfo);
31115 /***********************************************************************
31116 Exposure Events
31117 ***********************************************************************/
31119 #ifdef HAVE_WINDOW_SYSTEM
31121 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31122 which intersects rectangle R. R is in window-relative coordinates. */
31124 static void
31125 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31126 enum glyph_row_area area)
31128 struct glyph *first = row->glyphs[area];
31129 struct glyph *end = row->glyphs[area] + row->used[area];
31130 struct glyph *last;
31131 int first_x, start_x, x;
31133 if (area == TEXT_AREA && row->fill_line_p)
31134 /* If row extends face to end of line write the whole line. */
31135 draw_glyphs (w, 0, row, area,
31136 0, row->used[area],
31137 DRAW_NORMAL_TEXT, 0);
31138 else
31140 /* Set START_X to the window-relative start position for drawing glyphs of
31141 AREA. The first glyph of the text area can be partially visible.
31142 The first glyphs of other areas cannot. */
31143 start_x = window_box_left_offset (w, area);
31144 x = start_x;
31145 if (area == TEXT_AREA)
31146 x += row->x;
31148 /* Find the first glyph that must be redrawn. */
31149 while (first < end
31150 && x + first->pixel_width < r->x)
31152 x += first->pixel_width;
31153 ++first;
31156 /* Find the last one. */
31157 last = first;
31158 first_x = x;
31159 /* Use a signed int intermediate value to avoid catastrophic
31160 failures due to comparison between signed and unsigned, when
31161 x is negative (can happen for wide images that are hscrolled). */
31162 int r_end = r->x + r->width;
31163 while (last < end && x < r_end)
31165 x += last->pixel_width;
31166 ++last;
31169 /* Repaint. */
31170 if (last > first)
31171 draw_glyphs (w, first_x - start_x, row, area,
31172 first - row->glyphs[area], last - row->glyphs[area],
31173 DRAW_NORMAL_TEXT, 0);
31178 /* Redraw the parts of the glyph row ROW on window W intersecting
31179 rectangle R. R is in window-relative coordinates. Value is
31180 true if mouse-face was overwritten. */
31182 static bool
31183 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31185 eassert (row->enabled_p);
31187 if (row->mode_line_p || w->pseudo_window_p)
31188 draw_glyphs (w, 0, row, TEXT_AREA,
31189 0, row->used[TEXT_AREA],
31190 DRAW_NORMAL_TEXT, 0);
31191 else
31193 if (row->used[LEFT_MARGIN_AREA])
31194 expose_area (w, row, r, LEFT_MARGIN_AREA);
31195 if (row->used[TEXT_AREA])
31196 expose_area (w, row, r, TEXT_AREA);
31197 if (row->used[RIGHT_MARGIN_AREA])
31198 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31199 draw_row_fringe_bitmaps (w, row);
31202 return row->mouse_face_p;
31206 /* Redraw those parts of glyphs rows during expose event handling that
31207 overlap other rows. Redrawing of an exposed line writes over parts
31208 of lines overlapping that exposed line; this function fixes that.
31210 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31211 row in W's current matrix that is exposed and overlaps other rows.
31212 LAST_OVERLAPPING_ROW is the last such row. */
31214 static void
31215 expose_overlaps (struct window *w,
31216 struct glyph_row *first_overlapping_row,
31217 struct glyph_row *last_overlapping_row,
31218 XRectangle *r)
31220 struct glyph_row *row;
31222 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31223 if (row->overlapping_p)
31225 eassert (row->enabled_p && !row->mode_line_p);
31227 row->clip = r;
31228 if (row->used[LEFT_MARGIN_AREA])
31229 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31231 if (row->used[TEXT_AREA])
31232 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31234 if (row->used[RIGHT_MARGIN_AREA])
31235 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31236 row->clip = NULL;
31241 /* Return true if W's cursor intersects rectangle R. */
31243 static bool
31244 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31246 XRectangle cr, result;
31247 struct glyph *cursor_glyph;
31248 struct glyph_row *row;
31250 if (w->phys_cursor.vpos >= 0
31251 && w->phys_cursor.vpos < w->current_matrix->nrows
31252 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31253 row->enabled_p)
31254 && row->cursor_in_fringe_p)
31256 /* Cursor is in the fringe. */
31257 cr.x = window_box_right_offset (w,
31258 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31259 ? RIGHT_MARGIN_AREA
31260 : TEXT_AREA));
31261 cr.y = row->y;
31262 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31263 cr.height = row->height;
31264 return x_intersect_rectangles (&cr, r, &result);
31267 cursor_glyph = get_phys_cursor_glyph (w);
31268 if (cursor_glyph)
31270 /* r is relative to W's box, but w->phys_cursor.x is relative
31271 to left edge of W's TEXT area. Adjust it. */
31272 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31273 cr.y = w->phys_cursor.y;
31274 cr.width = cursor_glyph->pixel_width;
31275 cr.height = w->phys_cursor_height;
31276 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31277 I assume the effect is the same -- and this is portable. */
31278 return x_intersect_rectangles (&cr, r, &result);
31280 /* If we don't understand the format, pretend we're not in the hot-spot. */
31281 return false;
31285 /* EXPORT:
31286 Draw a vertical window border to the right of window W if W doesn't
31287 have vertical scroll bars. */
31289 void
31290 x_draw_vertical_border (struct window *w)
31292 struct frame *f = XFRAME (WINDOW_FRAME (w));
31294 /* We could do better, if we knew what type of scroll-bar the adjacent
31295 windows (on either side) have... But we don't :-(
31296 However, I think this works ok. ++KFS 2003-04-25 */
31298 /* Redraw borders between horizontally adjacent windows. Don't
31299 do it for frames with vertical scroll bars because either the
31300 right scroll bar of a window, or the left scroll bar of its
31301 neighbor will suffice as a border. */
31302 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31303 return;
31305 /* Note: It is necessary to redraw both the left and the right
31306 borders, for when only this single window W is being
31307 redisplayed. */
31308 if (!WINDOW_RIGHTMOST_P (w)
31309 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31311 int x0, x1, y0, y1;
31313 window_box_edges (w, &x0, &y0, &x1, &y1);
31314 y1 -= 1;
31316 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31317 x1 -= 1;
31319 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31322 if (!WINDOW_LEFTMOST_P (w)
31323 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31325 int x0, x1, y0, y1;
31327 window_box_edges (w, &x0, &y0, &x1, &y1);
31328 y1 -= 1;
31330 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31331 x0 -= 1;
31333 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31338 /* Draw window dividers for window W. */
31340 void
31341 x_draw_right_divider (struct window *w)
31343 struct frame *f = WINDOW_XFRAME (w);
31345 if (w->mini || w->pseudo_window_p)
31346 return;
31347 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31349 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31350 int x1 = WINDOW_RIGHT_EDGE_X (w);
31351 int y0 = WINDOW_TOP_EDGE_Y (w);
31352 /* The bottom divider prevails. */
31353 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31355 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31359 static void
31360 x_draw_bottom_divider (struct window *w)
31362 struct frame *f = XFRAME (WINDOW_FRAME (w));
31364 if (w->mini || w->pseudo_window_p)
31365 return;
31366 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31368 int x0 = WINDOW_LEFT_EDGE_X (w);
31369 int x1 = WINDOW_RIGHT_EDGE_X (w);
31370 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31371 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31373 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31377 /* Redraw the part of window W intersection rectangle FR. Pixel
31378 coordinates in FR are frame-relative. Call this function with
31379 input blocked. Value is true if the exposure overwrites
31380 mouse-face. */
31382 static bool
31383 expose_window (struct window *w, XRectangle *fr)
31385 struct frame *f = XFRAME (w->frame);
31386 XRectangle wr, r;
31387 bool mouse_face_overwritten_p = false;
31389 /* If window is not yet fully initialized, do nothing. This can
31390 happen when toolkit scroll bars are used and a window is split.
31391 Reconfiguring the scroll bar will generate an expose for a newly
31392 created window. */
31393 if (w->current_matrix == NULL)
31394 return false;
31396 /* When we're currently updating the window, display and current
31397 matrix usually don't agree. Arrange for a thorough display
31398 later. */
31399 if (w->must_be_updated_p)
31401 SET_FRAME_GARBAGED (f);
31402 return false;
31405 /* Frame-relative pixel rectangle of W. */
31406 wr.x = WINDOW_LEFT_EDGE_X (w);
31407 wr.y = WINDOW_TOP_EDGE_Y (w);
31408 wr.width = WINDOW_PIXEL_WIDTH (w);
31409 wr.height = WINDOW_PIXEL_HEIGHT (w);
31411 if (x_intersect_rectangles (fr, &wr, &r))
31413 int yb = window_text_bottom_y (w);
31414 struct glyph_row *row;
31415 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31417 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31418 r.x, r.y, r.width, r.height));
31420 /* Convert to window coordinates. */
31421 r.x -= WINDOW_LEFT_EDGE_X (w);
31422 r.y -= WINDOW_TOP_EDGE_Y (w);
31424 /* Turn off the cursor. */
31425 bool cursor_cleared_p = (!w->pseudo_window_p
31426 && phys_cursor_in_rect_p (w, &r));
31427 if (cursor_cleared_p)
31428 x_clear_cursor (w);
31430 /* If the row containing the cursor extends face to end of line,
31431 then expose_area might overwrite the cursor outside the
31432 rectangle and thus notice_overwritten_cursor might clear
31433 w->phys_cursor_on_p. We remember the original value and
31434 check later if it is changed. */
31435 bool phys_cursor_on_p = w->phys_cursor_on_p;
31437 /* Use a signed int intermediate value to avoid catastrophic
31438 failures due to comparison between signed and unsigned, when
31439 y0 or y1 is negative (can happen for tall images). */
31440 int r_bottom = r.y + r.height;
31442 /* Update lines intersecting rectangle R. */
31443 first_overlapping_row = last_overlapping_row = NULL;
31444 for (row = w->current_matrix->rows;
31445 row->enabled_p;
31446 ++row)
31448 int y0 = row->y;
31449 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31451 if ((y0 >= r.y && y0 < r_bottom)
31452 || (y1 > r.y && y1 < r_bottom)
31453 || (r.y >= y0 && r.y < y1)
31454 || (r_bottom > y0 && r_bottom < y1))
31456 /* A header line may be overlapping, but there is no need
31457 to fix overlapping areas for them. KFS 2005-02-12 */
31458 if (row->overlapping_p && !row->mode_line_p)
31460 if (first_overlapping_row == NULL)
31461 first_overlapping_row = row;
31462 last_overlapping_row = row;
31465 row->clip = fr;
31466 if (expose_line (w, row, &r))
31467 mouse_face_overwritten_p = true;
31468 row->clip = NULL;
31470 else if (row->overlapping_p)
31472 /* We must redraw a row overlapping the exposed area. */
31473 if (y0 < r.y
31474 ? y0 + row->phys_height > r.y
31475 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31477 if (first_overlapping_row == NULL)
31478 first_overlapping_row = row;
31479 last_overlapping_row = row;
31483 if (y1 >= yb)
31484 break;
31487 /* Display the mode line if there is one. */
31488 if (WINDOW_WANTS_MODELINE_P (w)
31489 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31490 row->enabled_p)
31491 && row->y < r_bottom)
31493 if (expose_line (w, row, &r))
31494 mouse_face_overwritten_p = true;
31497 if (!w->pseudo_window_p)
31499 /* Fix the display of overlapping rows. */
31500 if (first_overlapping_row)
31501 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31502 fr);
31504 /* Draw border between windows. */
31505 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31506 x_draw_right_divider (w);
31507 else
31508 x_draw_vertical_border (w);
31510 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31511 x_draw_bottom_divider (w);
31513 /* Turn the cursor on again. */
31514 if (cursor_cleared_p
31515 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31516 update_window_cursor (w, true);
31520 return mouse_face_overwritten_p;
31525 /* Redraw (parts) of all windows in the window tree rooted at W that
31526 intersect R. R contains frame pixel coordinates. Value is
31527 true if the exposure overwrites mouse-face. */
31529 static bool
31530 expose_window_tree (struct window *w, XRectangle *r)
31532 struct frame *f = XFRAME (w->frame);
31533 bool mouse_face_overwritten_p = false;
31535 while (w && !FRAME_GARBAGED_P (f))
31537 mouse_face_overwritten_p
31538 |= (WINDOWP (w->contents)
31539 ? expose_window_tree (XWINDOW (w->contents), r)
31540 : expose_window (w, r));
31542 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31545 return mouse_face_overwritten_p;
31549 /* EXPORT:
31550 Redisplay an exposed area of frame F. X and Y are the upper-left
31551 corner of the exposed rectangle. W and H are width and height of
31552 the exposed area. All are pixel values. W or H zero means redraw
31553 the entire frame. */
31555 void
31556 expose_frame (struct frame *f, int x, int y, int w, int h)
31558 XRectangle r;
31559 bool mouse_face_overwritten_p = false;
31561 TRACE ((stderr, "expose_frame "));
31563 /* No need to redraw if frame will be redrawn soon. */
31564 if (FRAME_GARBAGED_P (f))
31566 TRACE ((stderr, " garbaged\n"));
31567 return;
31570 /* If basic faces haven't been realized yet, there is no point in
31571 trying to redraw anything. This can happen when we get an expose
31572 event while Emacs is starting, e.g. by moving another window. */
31573 if (FRAME_FACE_CACHE (f) == NULL
31574 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31576 TRACE ((stderr, " no faces\n"));
31577 return;
31580 if (w == 0 || h == 0)
31582 r.x = r.y = 0;
31583 r.width = FRAME_TEXT_WIDTH (f);
31584 r.height = FRAME_TEXT_HEIGHT (f);
31586 else
31588 r.x = x;
31589 r.y = y;
31590 r.width = w;
31591 r.height = h;
31594 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31595 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31597 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31598 if (WINDOWP (f->tool_bar_window))
31599 mouse_face_overwritten_p
31600 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31601 #endif
31603 #ifdef HAVE_X_WINDOWS
31604 #ifndef MSDOS
31605 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31606 if (WINDOWP (f->menu_bar_window))
31607 mouse_face_overwritten_p
31608 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31609 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31610 #endif
31611 #endif
31613 /* Some window managers support a focus-follows-mouse style with
31614 delayed raising of frames. Imagine a partially obscured frame,
31615 and moving the mouse into partially obscured mouse-face on that
31616 frame. The visible part of the mouse-face will be highlighted,
31617 then the WM raises the obscured frame. With at least one WM, KDE
31618 2.1, Emacs is not getting any event for the raising of the frame
31619 (even tried with SubstructureRedirectMask), only Expose events.
31620 These expose events will draw text normally, i.e. not
31621 highlighted. Which means we must redo the highlight here.
31622 Subsume it under ``we love X''. --gerd 2001-08-15 */
31623 /* Included in Windows version because Windows most likely does not
31624 do the right thing if any third party tool offers
31625 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31626 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31628 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31629 if (f == hlinfo->mouse_face_mouse_frame)
31631 int mouse_x = hlinfo->mouse_face_mouse_x;
31632 int mouse_y = hlinfo->mouse_face_mouse_y;
31633 clear_mouse_face (hlinfo);
31634 note_mouse_highlight (f, mouse_x, mouse_y);
31640 /* EXPORT:
31641 Determine the intersection of two rectangles R1 and R2. Return
31642 the intersection in *RESULT. Value is true if RESULT is not
31643 empty. */
31645 bool
31646 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31648 XRectangle *left, *right;
31649 XRectangle *upper, *lower;
31650 bool intersection_p = false;
31652 /* Rearrange so that R1 is the left-most rectangle. */
31653 if (r1->x < r2->x)
31654 left = r1, right = r2;
31655 else
31656 left = r2, right = r1;
31658 /* X0 of the intersection is right.x0, if this is inside R1,
31659 otherwise there is no intersection. */
31660 if (right->x <= left->x + left->width)
31662 result->x = right->x;
31664 /* The right end of the intersection is the minimum of
31665 the right ends of left and right. */
31666 result->width = (min (left->x + left->width, right->x + right->width)
31667 - result->x);
31669 /* Same game for Y. */
31670 if (r1->y < r2->y)
31671 upper = r1, lower = r2;
31672 else
31673 upper = r2, lower = r1;
31675 /* The upper end of the intersection is lower.y0, if this is inside
31676 of upper. Otherwise, there is no intersection. */
31677 if (lower->y <= upper->y + upper->height)
31679 result->y = lower->y;
31681 /* The lower end of the intersection is the minimum of the lower
31682 ends of upper and lower. */
31683 result->height = (min (lower->y + lower->height,
31684 upper->y + upper->height)
31685 - result->y);
31686 intersection_p = true;
31690 return intersection_p;
31693 #endif /* HAVE_WINDOW_SYSTEM */
31696 /***********************************************************************
31697 Initialization
31698 ***********************************************************************/
31700 void
31701 syms_of_xdisp (void)
31703 Vwith_echo_area_save_vector = Qnil;
31704 staticpro (&Vwith_echo_area_save_vector);
31706 Vmessage_stack = Qnil;
31707 staticpro (&Vmessage_stack);
31709 /* Non-nil means don't actually do any redisplay. */
31710 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31712 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31714 DEFVAR_BOOL("inhibit-message", inhibit_message,
31715 doc: /* Non-nil means calls to `message' are not displayed.
31716 They are still logged to the *Messages* buffer. */);
31717 inhibit_message = 0;
31719 message_dolog_marker1 = Fmake_marker ();
31720 staticpro (&message_dolog_marker1);
31721 message_dolog_marker2 = Fmake_marker ();
31722 staticpro (&message_dolog_marker2);
31723 message_dolog_marker3 = Fmake_marker ();
31724 staticpro (&message_dolog_marker3);
31726 defsubr (&Sset_buffer_redisplay);
31727 #ifdef GLYPH_DEBUG
31728 defsubr (&Sdump_frame_glyph_matrix);
31729 defsubr (&Sdump_glyph_matrix);
31730 defsubr (&Sdump_glyph_row);
31731 defsubr (&Sdump_tool_bar_row);
31732 defsubr (&Strace_redisplay);
31733 defsubr (&Strace_to_stderr);
31734 #endif
31735 #ifdef HAVE_WINDOW_SYSTEM
31736 defsubr (&Stool_bar_height);
31737 defsubr (&Slookup_image_map);
31738 #endif
31739 defsubr (&Sline_pixel_height);
31740 defsubr (&Sformat_mode_line);
31741 defsubr (&Sinvisible_p);
31742 defsubr (&Scurrent_bidi_paragraph_direction);
31743 defsubr (&Swindow_text_pixel_size);
31744 defsubr (&Smove_point_visually);
31745 defsubr (&Sbidi_find_overridden_directionality);
31747 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31748 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31749 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31750 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31751 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31752 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31753 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31754 DEFSYM (Qeval, "eval");
31755 DEFSYM (QCdata, ":data");
31757 /* Names of text properties relevant for redisplay. */
31758 DEFSYM (Qdisplay, "display");
31759 DEFSYM (Qspace_width, "space-width");
31760 DEFSYM (Qraise, "raise");
31761 DEFSYM (Qslice, "slice");
31762 DEFSYM (Qspace, "space");
31763 DEFSYM (Qmargin, "margin");
31764 DEFSYM (Qpointer, "pointer");
31765 DEFSYM (Qleft_margin, "left-margin");
31766 DEFSYM (Qright_margin, "right-margin");
31767 DEFSYM (Qcenter, "center");
31768 DEFSYM (Qline_height, "line-height");
31769 DEFSYM (QCalign_to, ":align-to");
31770 DEFSYM (QCrelative_width, ":relative-width");
31771 DEFSYM (QCrelative_height, ":relative-height");
31772 DEFSYM (QCeval, ":eval");
31773 DEFSYM (QCpropertize, ":propertize");
31774 DEFSYM (QCfile, ":file");
31775 DEFSYM (Qfontified, "fontified");
31776 DEFSYM (Qfontification_functions, "fontification-functions");
31778 /* Name of the face used to highlight trailing whitespace. */
31779 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31781 /* Name and number of the face used to highlight escape glyphs. */
31782 DEFSYM (Qescape_glyph, "escape-glyph");
31784 /* Name and number of the face used to highlight non-breaking
31785 spaces/hyphens. */
31786 DEFSYM (Qnobreak_space, "nobreak-space");
31787 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31789 /* The symbol 'image' which is the car of the lists used to represent
31790 images in Lisp. Also a tool bar style. */
31791 DEFSYM (Qimage, "image");
31793 /* Tool bar styles. */
31794 DEFSYM (Qtext, "text");
31795 DEFSYM (Qboth, "both");
31796 DEFSYM (Qboth_horiz, "both-horiz");
31797 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31799 /* The image map types. */
31800 DEFSYM (QCmap, ":map");
31801 DEFSYM (QCpointer, ":pointer");
31802 DEFSYM (Qrect, "rect");
31803 DEFSYM (Qcircle, "circle");
31804 DEFSYM (Qpoly, "poly");
31806 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31808 DEFSYM (Qgrow_only, "grow-only");
31809 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31810 DEFSYM (Qposition, "position");
31811 DEFSYM (Qbuffer_position, "buffer-position");
31812 DEFSYM (Qobject, "object");
31814 /* Cursor shapes. */
31815 DEFSYM (Qbar, "bar");
31816 DEFSYM (Qhbar, "hbar");
31817 DEFSYM (Qbox, "box");
31818 DEFSYM (Qhollow, "hollow");
31820 /* Pointer shapes. */
31821 DEFSYM (Qhand, "hand");
31822 DEFSYM (Qarrow, "arrow");
31823 /* also Qtext */
31825 DEFSYM (Qdragging, "dragging");
31827 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31829 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31830 staticpro (&list_of_error);
31832 /* Values of those variables at last redisplay are stored as
31833 properties on 'overlay-arrow-position' symbol. However, if
31834 Voverlay_arrow_position is a marker, last-arrow-position is its
31835 numerical position. */
31836 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31837 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31839 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31840 properties on a symbol in overlay-arrow-variable-list. */
31841 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31842 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31844 echo_buffer[0] = echo_buffer[1] = Qnil;
31845 staticpro (&echo_buffer[0]);
31846 staticpro (&echo_buffer[1]);
31848 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31849 staticpro (&echo_area_buffer[0]);
31850 staticpro (&echo_area_buffer[1]);
31852 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31853 staticpro (&Vmessages_buffer_name);
31855 mode_line_proptrans_alist = Qnil;
31856 staticpro (&mode_line_proptrans_alist);
31857 mode_line_string_list = Qnil;
31858 staticpro (&mode_line_string_list);
31859 mode_line_string_face = Qnil;
31860 staticpro (&mode_line_string_face);
31861 mode_line_string_face_prop = Qnil;
31862 staticpro (&mode_line_string_face_prop);
31863 Vmode_line_unwind_vector = Qnil;
31864 staticpro (&Vmode_line_unwind_vector);
31866 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31868 help_echo_string = Qnil;
31869 staticpro (&help_echo_string);
31870 help_echo_object = Qnil;
31871 staticpro (&help_echo_object);
31872 help_echo_window = Qnil;
31873 staticpro (&help_echo_window);
31874 previous_help_echo_string = Qnil;
31875 staticpro (&previous_help_echo_string);
31876 help_echo_pos = -1;
31878 DEFSYM (Qright_to_left, "right-to-left");
31879 DEFSYM (Qleft_to_right, "left-to-right");
31880 defsubr (&Sbidi_resolved_levels);
31882 #ifdef HAVE_WINDOW_SYSTEM
31883 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31884 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31885 For example, if a block cursor is over a tab, it will be drawn as
31886 wide as that tab on the display. */);
31887 x_stretch_cursor_p = 0;
31888 #endif
31890 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31891 doc: /* Non-nil means highlight trailing whitespace.
31892 The face used for trailing whitespace is `trailing-whitespace'. */);
31893 Vshow_trailing_whitespace = Qnil;
31895 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31896 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31897 If the value is t, Emacs highlights non-ASCII chars which have the
31898 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31899 or `nobreak-hyphen' face respectively.
31901 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31902 U+2011 (non-breaking hyphen) are affected.
31904 Any other non-nil value means to display these characters as a escape
31905 glyph followed by an ordinary space or hyphen.
31907 A value of nil means no special handling of these characters. */);
31908 Vnobreak_char_display = Qt;
31910 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31911 doc: /* The pointer shape to show in void text areas.
31912 A value of nil means to show the text pointer. Other options are
31913 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31914 `hourglass'. */);
31915 Vvoid_text_area_pointer = Qarrow;
31917 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31918 doc: /* Non-nil means don't actually do any redisplay.
31919 This is used for internal purposes. */);
31920 Vinhibit_redisplay = Qnil;
31922 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31923 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31924 Vglobal_mode_string = Qnil;
31926 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31927 doc: /* Marker for where to display an arrow on top of the buffer text.
31928 This must be the beginning of a line in order to work.
31929 See also `overlay-arrow-string'. */);
31930 Voverlay_arrow_position = Qnil;
31932 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31933 doc: /* String to display as an arrow in non-window frames.
31934 See also `overlay-arrow-position'. */);
31935 Voverlay_arrow_string = build_pure_c_string ("=>");
31937 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31938 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31939 The symbols on this list are examined during redisplay to determine
31940 where to display overlay arrows. */);
31941 Voverlay_arrow_variable_list
31942 = list1 (intern_c_string ("overlay-arrow-position"));
31944 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31945 doc: /* The number of lines to try scrolling a window by when point moves out.
31946 If that fails to bring point back on frame, point is centered instead.
31947 If this is zero, point is always centered after it moves off frame.
31948 If you want scrolling to always be a line at a time, you should set
31949 `scroll-conservatively' to a large value rather than set this to 1. */);
31951 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31952 doc: /* Scroll up to this many lines, to bring point back on screen.
31953 If point moves off-screen, redisplay will scroll by up to
31954 `scroll-conservatively' lines in order to bring point just barely
31955 onto the screen again. If that cannot be done, then redisplay
31956 recenters point as usual.
31958 If the value is greater than 100, redisplay will never recenter point,
31959 but will always scroll just enough text to bring point into view, even
31960 if you move far away.
31962 A value of zero means always recenter point if it moves off screen. */);
31963 scroll_conservatively = 0;
31965 DEFVAR_INT ("scroll-margin", scroll_margin,
31966 doc: /* Number of lines of margin at the top and bottom of a window.
31967 Recenter the window whenever point gets within this many lines
31968 of the top or bottom of the window. */);
31969 scroll_margin = 0;
31971 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
31972 doc: /* Maximum effective value of `scroll-margin'.
31973 Given as a fraction of the current window's lines. The value should
31974 be a floating point number between 0.0 and 0.5. The effective maximum
31975 is limited to (/ (1- window-lines) 2). Non-float values for this
31976 variable are ignored and the default 0.25 is used instead. */);
31977 Vmaximum_scroll_margin = make_float (0.25);
31979 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31980 doc: /* Pixels per inch value for non-window system displays.
31981 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31982 Vdisplay_pixels_per_inch = make_float (72.0);
31984 #ifdef GLYPH_DEBUG
31985 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31986 #endif
31988 DEFVAR_LISP ("truncate-partial-width-windows",
31989 Vtruncate_partial_width_windows,
31990 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31991 For an integer value, truncate lines in each window narrower than the
31992 full frame width, provided the total window width in column units is less
31993 than that integer; otherwise, respect the value of `truncate-lines'.
31994 The total width of the window is as returned by `window-total-width', it
31995 includes the fringes, the continuation and truncation glyphs, the
31996 display margins (if any), and the scroll bar
31998 For any other non-nil value, truncate lines in all windows that do
31999 not span the full frame width.
32001 A value of nil means to respect the value of `truncate-lines'.
32003 If `word-wrap' is enabled, you might want to reduce this. */);
32004 Vtruncate_partial_width_windows = make_number (50);
32006 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32007 doc: /* Maximum buffer size for which line number should be displayed.
32008 If the buffer is bigger than this, the line number does not appear
32009 in the mode line. A value of nil means no limit. */);
32010 Vline_number_display_limit = Qnil;
32012 DEFVAR_INT ("line-number-display-limit-width",
32013 line_number_display_limit_width,
32014 doc: /* Maximum line width (in characters) for line number display.
32015 If the average length of the lines near point is bigger than this, then the
32016 line number may be omitted from the mode line. */);
32017 line_number_display_limit_width = 200;
32019 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32020 doc: /* Non-nil means highlight region even in nonselected windows. */);
32021 highlight_nonselected_windows = false;
32023 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32024 doc: /* Non-nil if more than one frame is visible on this display.
32025 Minibuffer-only frames don't count, but iconified frames do.
32026 This variable is not guaranteed to be accurate except while processing
32027 `frame-title-format' and `icon-title-format'. */);
32029 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32030 doc: /* Template for displaying the title bar of visible frames.
32031 \(Assuming the window manager supports this feature.)
32033 This variable has the same structure as `mode-line-format', except that
32034 the %c, %C, and %l constructs are ignored. It is used only on frames for
32035 which no explicit name has been set (see `modify-frame-parameters'). */);
32037 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32038 doc: /* Template for displaying the title bar of an iconified frame.
32039 \(Assuming the window manager supports this feature.)
32040 This variable has the same structure as `mode-line-format' (which see),
32041 and is used only on frames for which no explicit name has been set
32042 \(see `modify-frame-parameters'). */);
32043 Vicon_title_format
32044 = Vframe_title_format
32045 = listn (CONSTYPE_PURE, 3,
32046 intern_c_string ("multiple-frames"),
32047 build_pure_c_string ("%b"),
32048 listn (CONSTYPE_PURE, 4,
32049 empty_unibyte_string,
32050 intern_c_string ("invocation-name"),
32051 build_pure_c_string ("@"),
32052 intern_c_string ("system-name")));
32054 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32055 doc: /* Maximum number of lines to keep in the message log buffer.
32056 If nil, disable message logging. If t, log messages but don't truncate
32057 the buffer when it becomes large. */);
32058 Vmessage_log_max = make_number (1000);
32060 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32061 doc: /* List of functions to call before redisplaying a window with scrolling.
32062 Each function is called with two arguments, the window and its new
32063 display-start position.
32064 These functions are called whenever the `window-start' marker is modified,
32065 either to point into another buffer (e.g. via `set-window-buffer') or another
32066 place in the same buffer.
32067 Note that the value of `window-end' is not valid when these functions are
32068 called.
32070 Warning: Do not use this feature to alter the way the window
32071 is scrolled. It is not designed for that, and such use probably won't
32072 work. */);
32073 Vwindow_scroll_functions = Qnil;
32075 DEFVAR_LISP ("window-text-change-functions",
32076 Vwindow_text_change_functions,
32077 doc: /* Functions to call in redisplay when text in the window might change. */);
32078 Vwindow_text_change_functions = Qnil;
32080 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32081 doc: /* Functions called when redisplay of a window reaches the end trigger.
32082 Each function is called with two arguments, the window and the end trigger value.
32083 See `set-window-redisplay-end-trigger'. */);
32084 Vredisplay_end_trigger_functions = Qnil;
32086 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32087 doc: /* Non-nil means autoselect window with mouse pointer.
32088 If nil, do not autoselect windows.
32089 A positive number means delay autoselection by that many seconds: a
32090 window is autoselected only after the mouse has remained in that
32091 window for the duration of the delay.
32092 A negative number has a similar effect, but causes windows to be
32093 autoselected only after the mouse has stopped moving. (Because of
32094 the way Emacs compares mouse events, you will occasionally wait twice
32095 that time before the window gets selected.)
32096 Any other value means to autoselect window instantaneously when the
32097 mouse pointer enters it.
32099 Autoselection selects the minibuffer only if it is active, and never
32100 unselects the minibuffer if it is active.
32102 When customizing this variable make sure that the actual value of
32103 `focus-follows-mouse' matches the behavior of your window manager. */);
32104 Vmouse_autoselect_window = Qnil;
32106 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32107 doc: /* Non-nil means automatically resize tool-bars.
32108 This dynamically changes the tool-bar's height to the minimum height
32109 that is needed to make all tool-bar items visible.
32110 If value is `grow-only', the tool-bar's height is only increased
32111 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32112 Vauto_resize_tool_bars = Qt;
32114 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32115 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32116 auto_raise_tool_bar_buttons_p = true;
32118 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32119 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32120 make_cursor_line_fully_visible_p = true;
32122 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32123 doc: /* Border below tool-bar in pixels.
32124 If an integer, use it as the height of the border.
32125 If it is one of `internal-border-width' or `border-width', use the
32126 value of the corresponding frame parameter.
32127 Otherwise, no border is added below the tool-bar. */);
32128 Vtool_bar_border = Qinternal_border_width;
32130 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32131 doc: /* Margin around tool-bar buttons in pixels.
32132 If an integer, use that for both horizontal and vertical margins.
32133 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32134 HORZ specifying the horizontal margin, and VERT specifying the
32135 vertical margin. */);
32136 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32138 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32139 doc: /* Relief thickness of tool-bar buttons. */);
32140 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32142 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32143 doc: /* Tool bar style to use.
32144 It can be one of
32145 image - show images only
32146 text - show text only
32147 both - show both, text below image
32148 both-horiz - show text to the right of the image
32149 text-image-horiz - show text to the left of the image
32150 any other - use system default or image if no system default.
32152 This variable only affects the GTK+ toolkit version of Emacs. */);
32153 Vtool_bar_style = Qnil;
32155 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32156 doc: /* Maximum number of characters a label can have to be shown.
32157 The tool bar style must also show labels for this to have any effect, see
32158 `tool-bar-style'. */);
32159 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32161 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32162 doc: /* List of functions to call to fontify regions of text.
32163 Each function is called with one argument POS. Functions must
32164 fontify a region starting at POS in the current buffer, and give
32165 fontified regions the property `fontified'. */);
32166 Vfontification_functions = Qnil;
32167 Fmake_variable_buffer_local (Qfontification_functions);
32169 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32170 unibyte_display_via_language_environment,
32171 doc: /* Non-nil means display unibyte text according to language environment.
32172 Specifically, this means that raw bytes in the range 160-255 decimal
32173 are displayed by converting them to the equivalent multibyte characters
32174 according to the current language environment. As a result, they are
32175 displayed according to the current fontset.
32177 Note that this variable affects only how these bytes are displayed,
32178 but does not change the fact they are interpreted as raw bytes. */);
32179 unibyte_display_via_language_environment = false;
32181 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32182 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32183 If a float, it specifies a fraction of the mini-window frame's height.
32184 If an integer, it specifies a number of lines. */);
32185 Vmax_mini_window_height = make_float (0.25);
32187 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32188 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32189 A value of nil means don't automatically resize mini-windows.
32190 A value of t means resize them to fit the text displayed in them.
32191 A value of `grow-only', the default, means let mini-windows grow only;
32192 they return to their normal size when the minibuffer is closed, or the
32193 echo area becomes empty. */);
32194 /* Contrary to the doc string, we initialize this to nil, so that
32195 loading loadup.el won't try to resize windows before loading
32196 window.el, where some functions we need to call for this live.
32197 We assign the 'grow-only' value right after loading window.el
32198 during loadup. */
32199 Vresize_mini_windows = Qnil;
32201 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32202 doc: /* Alist specifying how to blink the cursor off.
32203 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32204 `cursor-type' frame-parameter or variable equals ON-STATE,
32205 comparing using `equal', Emacs uses OFF-STATE to specify
32206 how to blink it off. ON-STATE and OFF-STATE are values for
32207 the `cursor-type' frame parameter.
32209 If a frame's ON-STATE has no entry in this list,
32210 the frame's other specifications determine how to blink the cursor off. */);
32211 Vblink_cursor_alist = Qnil;
32213 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32214 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32215 The value `current-line' means the line displaying point in each window
32216 is automatically scrolled horizontally to make point visible.
32217 Any other non-nil value means all the lines in a window are automatically
32218 scrolled horizontally to make point visible. */);
32219 automatic_hscrolling = Qt;
32220 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32221 DEFSYM (Qcurrent_line, "current-line");
32223 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32224 doc: /* How many columns away from the window edge point is allowed to get
32225 before automatic hscrolling will horizontally scroll the window. */);
32226 hscroll_margin = 5;
32228 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32229 doc: /* How many columns to scroll the window when point gets too close to the edge.
32230 When point is less than `hscroll-margin' columns from the window
32231 edge, automatic hscrolling will scroll the window by the amount of columns
32232 determined by this variable. If its value is a positive integer, scroll that
32233 many columns. If it's a positive floating-point number, it specifies the
32234 fraction of the window's width to scroll. If it's nil or zero, point will be
32235 centered horizontally after the scroll. Any other value, including negative
32236 numbers, are treated as if the value were zero.
32238 Automatic hscrolling always moves point outside the scroll margin, so if
32239 point was more than scroll step columns inside the margin, the window will
32240 scroll more than the value given by the scroll step.
32242 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32243 and `scroll-right' overrides this variable's effect. */);
32244 Vhscroll_step = make_number (0);
32246 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32247 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32248 Bind this around calls to `message' to let it take effect. */);
32249 message_truncate_lines = false;
32251 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32252 doc: /* Normal hook run to update the menu bar definitions.
32253 Redisplay runs this hook before it redisplays the menu bar.
32254 This is used to update menus such as Buffers, whose contents depend on
32255 various data. */);
32256 Vmenu_bar_update_hook = Qnil;
32258 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32259 doc: /* Frame for which we are updating a menu.
32260 The enable predicate for a menu binding should check this variable. */);
32261 Vmenu_updating_frame = Qnil;
32263 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32264 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32265 inhibit_menubar_update = false;
32267 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32268 doc: /* Prefix prepended to all continuation lines at display time.
32269 The value may be a string, an image, or a stretch-glyph; it is
32270 interpreted in the same way as the value of a `display' text property.
32272 This variable is overridden by any `wrap-prefix' text or overlay
32273 property.
32275 To add a prefix to non-continuation lines, use `line-prefix'. */);
32276 Vwrap_prefix = Qnil;
32277 DEFSYM (Qwrap_prefix, "wrap-prefix");
32278 Fmake_variable_buffer_local (Qwrap_prefix);
32280 DEFVAR_LISP ("line-prefix", Vline_prefix,
32281 doc: /* Prefix prepended to all non-continuation lines at display time.
32282 The value may be a string, an image, or a stretch-glyph; it is
32283 interpreted in the same way as the value of a `display' text property.
32285 This variable is overridden by any `line-prefix' text or overlay
32286 property.
32288 To add a prefix to continuation lines, use `wrap-prefix'. */);
32289 Vline_prefix = Qnil;
32290 DEFSYM (Qline_prefix, "line-prefix");
32291 Fmake_variable_buffer_local (Qline_prefix);
32293 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32294 doc: /* Non-nil means display line numbers.
32295 Line numbers are displayed before each non-continuation line, i.e.
32296 after each newline that comes from buffer text. */);
32297 Vdisplay_line_numbers = Qnil;
32298 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32299 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32301 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32302 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32303 inhibit_eval_during_redisplay = false;
32305 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32306 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32307 inhibit_free_realized_faces = false;
32309 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32310 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32311 Intended for use during debugging and for testing bidi display;
32312 see biditest.el in the test suite. */);
32313 inhibit_bidi_mirroring = false;
32315 #ifdef GLYPH_DEBUG
32316 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32317 doc: /* Inhibit try_window_id display optimization. */);
32318 inhibit_try_window_id = false;
32320 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32321 doc: /* Inhibit try_window_reusing display optimization. */);
32322 inhibit_try_window_reusing = false;
32324 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32325 doc: /* Inhibit try_cursor_movement display optimization. */);
32326 inhibit_try_cursor_movement = false;
32327 #endif /* GLYPH_DEBUG */
32329 DEFVAR_INT ("overline-margin", overline_margin,
32330 doc: /* Space between overline and text, in pixels.
32331 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32332 margin to the character height. */);
32333 overline_margin = 2;
32335 DEFVAR_INT ("underline-minimum-offset",
32336 underline_minimum_offset,
32337 doc: /* Minimum distance between baseline and underline.
32338 This can improve legibility of underlined text at small font sizes,
32339 particularly when using variable `x-use-underline-position-properties'
32340 with fonts that specify an UNDERLINE_POSITION relatively close to the
32341 baseline. The default value is 1. */);
32342 underline_minimum_offset = 1;
32344 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32345 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32346 This feature only works when on a window system that can change
32347 cursor shapes. */);
32348 display_hourglass_p = true;
32350 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32351 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32352 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32354 #ifdef HAVE_WINDOW_SYSTEM
32355 hourglass_atimer = NULL;
32356 hourglass_shown_p = false;
32357 #endif /* HAVE_WINDOW_SYSTEM */
32359 /* Name of the face used to display glyphless characters. */
32360 DEFSYM (Qglyphless_char, "glyphless-char");
32362 /* Method symbols for Vglyphless_char_display. */
32363 DEFSYM (Qhex_code, "hex-code");
32364 DEFSYM (Qempty_box, "empty-box");
32365 DEFSYM (Qthin_space, "thin-space");
32366 DEFSYM (Qzero_width, "zero-width");
32368 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32369 doc: /* Function run just before redisplay.
32370 It is called with one argument, which is the set of windows that are to
32371 be redisplayed. This set can be nil (meaning, only the selected window),
32372 or t (meaning all windows). */);
32373 Vpre_redisplay_function = intern ("ignore");
32375 /* Symbol for the purpose of Vglyphless_char_display. */
32376 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32377 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32379 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32380 doc: /* Char-table defining glyphless characters.
32381 Each element, if non-nil, should be one of the following:
32382 an ASCII acronym string: display this string in a box
32383 `hex-code': display the hexadecimal code of a character in a box
32384 `empty-box': display as an empty box
32385 `thin-space': display as 1-pixel width space
32386 `zero-width': don't display
32387 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32388 display method for graphical terminals and text terminals respectively.
32389 GRAPHICAL and TEXT should each have one of the values listed above.
32391 The char-table has one extra slot to control the display of a character for
32392 which no font is found. This slot only takes effect on graphical terminals.
32393 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32394 `thin-space'. The default is `empty-box'.
32396 If a character has a non-nil entry in an active display table, the
32397 display table takes effect; in this case, Emacs does not consult
32398 `glyphless-char-display' at all. */);
32399 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32400 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32401 Qempty_box);
32403 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32404 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32405 Vdebug_on_message = Qnil;
32407 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32408 doc: /* */);
32409 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32411 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32412 doc: /* */);
32413 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32415 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32416 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32417 /* Initialize to t, since we need to disable reordering until
32418 loadup.el successfully loads charprop.el. */
32419 redisplay__inhibit_bidi = true;
32421 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32422 doc: /* Non-nil means display raw bytes in hexadecimal format.
32423 The default is to use octal format (\200) whereas hexadecimal (\x80)
32424 may be more familiar to users. */);
32425 display_raw_bytes_as_hex = false;
32430 /* Initialize this module when Emacs starts. */
32432 void
32433 init_xdisp (void)
32435 CHARPOS (this_line_start_pos) = 0;
32437 if (!noninteractive)
32439 struct window *m = XWINDOW (minibuf_window);
32440 Lisp_Object frame = m->frame;
32441 struct frame *f = XFRAME (frame);
32442 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32443 struct window *r = XWINDOW (root);
32444 int i;
32446 echo_area_window = minibuf_window;
32448 r->top_line = FRAME_TOP_MARGIN (f);
32449 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32450 r->total_cols = FRAME_COLS (f);
32451 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32452 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32453 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32455 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32456 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32457 m->total_cols = FRAME_COLS (f);
32458 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32459 m->total_lines = 1;
32460 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32462 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32463 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32464 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32466 /* The default ellipsis glyphs `...'. */
32467 for (i = 0; i < 3; ++i)
32468 default_invis_vector[i] = make_number ('.');
32472 /* Allocate the buffer for frame titles.
32473 Also used for `format-mode-line'. */
32474 int size = 100;
32475 mode_line_noprop_buf = xmalloc (size);
32476 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32477 mode_line_noprop_ptr = mode_line_noprop_buf;
32478 mode_line_target = MODE_LINE_DISPLAY;
32481 help_echo_showing_p = false;
32484 #ifdef HAVE_WINDOW_SYSTEM
32486 /* Platform-independent portion of hourglass implementation. */
32488 /* Timer function of hourglass_atimer. */
32490 static void
32491 show_hourglass (struct atimer *timer)
32493 /* The timer implementation will cancel this timer automatically
32494 after this function has run. Set hourglass_atimer to null
32495 so that we know the timer doesn't have to be canceled. */
32496 hourglass_atimer = NULL;
32498 if (!hourglass_shown_p)
32500 Lisp_Object tail, frame;
32502 block_input ();
32504 FOR_EACH_FRAME (tail, frame)
32506 struct frame *f = XFRAME (frame);
32508 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32509 && FRAME_RIF (f)->show_hourglass)
32510 FRAME_RIF (f)->show_hourglass (f);
32513 hourglass_shown_p = true;
32514 unblock_input ();
32518 /* Cancel a currently active hourglass timer, and start a new one. */
32520 void
32521 start_hourglass (void)
32523 struct timespec delay;
32525 cancel_hourglass ();
32527 if (INTEGERP (Vhourglass_delay)
32528 && XINT (Vhourglass_delay) > 0)
32529 delay = make_timespec (min (XINT (Vhourglass_delay),
32530 TYPE_MAXIMUM (time_t)),
32532 else if (FLOATP (Vhourglass_delay)
32533 && XFLOAT_DATA (Vhourglass_delay) > 0)
32534 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32535 else
32536 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32538 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32539 show_hourglass, NULL);
32542 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32543 shown. */
32545 void
32546 cancel_hourglass (void)
32548 if (hourglass_atimer)
32550 cancel_atimer (hourglass_atimer);
32551 hourglass_atimer = NULL;
32554 if (hourglass_shown_p)
32556 Lisp_Object tail, frame;
32558 block_input ();
32560 FOR_EACH_FRAME (tail, frame)
32562 struct frame *f = XFRAME (frame);
32564 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32565 && FRAME_RIF (f)->hide_hourglass)
32566 FRAME_RIF (f)->hide_hourglass (f);
32567 #ifdef HAVE_NTGUI
32568 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32569 else if (!FRAME_W32_P (f))
32570 w32_arrow_cursor ();
32571 #endif
32574 hourglass_shown_p = false;
32575 unblock_input ();
32579 #endif /* HAVE_WINDOW_SYSTEM */