Fix multiple spaces in Emacs manual
[emacs.git] / src / xdisp.c
blob903815c6581e292b1d00e365890df0a74134e590
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2018 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
293 #include <math.h>
295 #include "lisp.h"
296 #include "atimer.h"
297 #include "composite.h"
298 #include "keyboard.h"
299 #include "systime.h"
300 #include "frame.h"
301 #include "window.h"
302 #include "termchar.h"
303 #include "dispextern.h"
304 #include "character.h"
305 #include "buffer.h"
306 #include "charset.h"
307 #include "indent.h"
308 #include "commands.h"
309 #include "keymap.h"
310 #include "disptab.h"
311 #include "termhooks.h"
312 #include "termopts.h"
313 #include "intervals.h"
314 #include "coding.h"
315 #include "region-cache.h"
316 #include "font.h"
317 #include "fontset.h"
318 #include "blockinput.h"
319 #include "xwidget.h"
320 #ifdef HAVE_WINDOW_SYSTEM
321 #include TERM_HEADER
322 #endif /* HAVE_WINDOW_SYSTEM */
324 #ifndef FRAME_X_OUTPUT
325 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
326 #endif
328 #define DISP_INFINITY 10000000
330 /* Holds the list (error). */
331 static Lisp_Object list_of_error;
333 #ifdef HAVE_WINDOW_SYSTEM
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P ((IT)->f) \
341 && ((IT)->bidi_it.paragraph_dir == R2L \
342 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
343 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
344 && (IT)->current_x == (IT)->last_visible_x)
346 #else /* !HAVE_WINDOW_SYSTEM */
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
348 #endif /* HAVE_WINDOW_SYSTEM */
350 /* Test if the display element loaded in IT, or the underlying buffer
351 or string character, is a space or a TAB character. This is used
352 to determine where word wrapping can occur. */
354 #define IT_DISPLAYING_WHITESPACE(it) \
355 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
356 || ((STRINGP (it->string) \
357 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
358 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
359 || (it->s \
360 && (it->s[IT_BYTEPOS (*it)] == ' ' \
361 || it->s[IT_BYTEPOS (*it)] == '\t')) \
362 || (IT_BYTEPOS (*it) < ZV_BYTE \
363 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
364 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
366 /* True means print newline to stdout before next mini-buffer message. */
368 bool noninteractive_need_newline;
370 /* True means print newline to message log before next message. */
372 static bool message_log_need_newline;
374 /* Three markers that message_dolog uses.
375 It could allocate them itself, but that causes trouble
376 in handling memory-full errors. */
377 static Lisp_Object message_dolog_marker1;
378 static Lisp_Object message_dolog_marker2;
379 static Lisp_Object message_dolog_marker3;
381 /* The buffer position of the first character appearing entirely or
382 partially on the line of the selected window which contains the
383 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
384 redisplay optimization in redisplay_internal. */
386 static struct text_pos this_line_start_pos;
388 /* Number of characters past the end of the line above, including the
389 terminating newline. */
391 static struct text_pos this_line_end_pos;
393 /* The vertical positions and the height of this line. */
395 static int this_line_vpos;
396 static int this_line_y;
397 static int this_line_pixel_height;
399 /* X position at which this display line starts. Usually zero;
400 negative if first character is partially visible. */
402 static int this_line_start_x;
404 /* The smallest character position seen by move_it_* functions as they
405 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
406 hscrolled lines, see display_line. */
408 static struct text_pos this_line_min_pos;
410 /* Buffer that this_line_.* variables are referring to. */
412 static struct buffer *this_line_buffer;
414 /* True if an overlay arrow has been displayed in this window. */
416 static bool overlay_arrow_seen;
418 /* Vector containing glyphs for an ellipsis `...'. */
420 static Lisp_Object default_invis_vector[3];
422 /* This is the window where the echo area message was displayed. It
423 is always a mini-buffer window, but it may not be the same window
424 currently active as a mini-buffer. */
426 Lisp_Object echo_area_window;
428 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
429 pushes the current message and the value of
430 message_enable_multibyte on the stack, the function restore_message
431 pops the stack and displays MESSAGE again. */
433 static Lisp_Object Vmessage_stack;
435 /* True means multibyte characters were enabled when the echo area
436 message was specified. */
438 static bool message_enable_multibyte;
440 /* At each redisplay cycle, we should refresh everything there is to refresh.
441 To do that efficiently, we use many optimizations that try to make sure we
442 don't waste too much time updating things that haven't changed.
443 The coarsest such optimization is that, in the most common cases, we only
444 look at the selected-window.
446 To know whether other windows should be considered for redisplay, we use the
447 variable windows_or_buffers_changed: as long as it is 0, it means that we
448 have not noticed anything that should require updating anything else than
449 the selected-window. If it is set to REDISPLAY_SOME, it means that since
450 last redisplay, some changes have been made which could impact other
451 windows. To know which ones need redisplay, every buffer, window, and frame
452 has a `redisplay' bit, which (if true) means that this object needs to be
453 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
454 looking for those `redisplay' bits (actually, there might be some such bits
455 set, but then only on objects which aren't displayed anyway).
457 OTOH if it's non-zero we wil have to loop through all windows and then check
458 the `redisplay' bit of the corresponding window, frame, and buffer, in order
459 to decide whether that window needs attention or not. Note that we can't
460 just look at the frame's redisplay bit to decide that the whole frame can be
461 skipped, since even if the frame's redisplay bit is unset, some of its
462 windows's redisplay bits may be set.
464 Mostly for historical reasons, windows_or_buffers_changed can also take
465 other non-zero values. In that case, the precise value doesn't matter (it
466 encodes the cause of the setting but is only used for debugging purposes),
467 and what it means is that we shouldn't pay attention to any `redisplay' bits
468 and we should simply try and redisplay every window out there. */
470 int windows_or_buffers_changed;
472 /* Nonzero if we should redraw the mode lines on the next redisplay.
473 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
474 then only redisplay the mode lines in those buffers/windows/frames where the
475 `redisplay' bit has been set.
476 For any other value, redisplay all mode lines (the number used is then only
477 used to track down the cause for this full-redisplay).
479 Since the frame title uses the same %-constructs as the mode line
480 (except %c, %C, and %l), if this variable is non-zero, we also consider
481 redisplaying the title of each frame, see x_consider_frame_title.
483 The `redisplay' bits are the same as those used for
484 windows_or_buffers_changed, and setting windows_or_buffers_changed also
485 causes recomputation of the mode lines of all those windows. IOW this
486 variable only has an effect if windows_or_buffers_changed is zero, in which
487 case we should only need to redisplay the mode-line of those objects with
488 a `redisplay' bit set but not the window's text content (tho we may still
489 need to refresh the text content of the selected-window). */
491 int update_mode_lines;
493 /* True after display_mode_line if %l was used and it displayed a
494 line number. */
496 static bool line_number_displayed;
498 /* The name of the *Messages* buffer, a string. */
500 static Lisp_Object Vmessages_buffer_name;
502 /* Current, index 0, and last displayed echo area message. Either
503 buffers from echo_buffers, or nil to indicate no message. */
505 Lisp_Object echo_area_buffer[2];
507 /* The buffers referenced from echo_area_buffer. */
509 static Lisp_Object echo_buffer[2];
511 /* A vector saved used in with_area_buffer to reduce consing. */
513 static Lisp_Object Vwith_echo_area_save_vector;
515 /* True means display_echo_area should display the last echo area
516 message again. Set by redisplay_preserve_echo_area. */
518 static bool display_last_displayed_message_p;
520 /* True if echo area is being used by print; false if being used by
521 message. */
523 static bool message_buf_print;
525 /* Set to true in clear_message to make redisplay_internal aware
526 of an emptied echo area. */
528 static bool message_cleared_p;
530 /* A scratch glyph row with contents used for generating truncation
531 glyphs. Also used in direct_output_for_insert. */
533 #define MAX_SCRATCH_GLYPHS 100
534 static struct glyph_row scratch_glyph_row;
535 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
537 /* Ascent and height of the last line processed by move_it_to. */
539 static int last_height;
541 /* True if there's a help-echo in the echo area. */
543 bool help_echo_showing_p;
545 /* The maximum distance to look ahead for text properties. Values
546 that are too small let us call compute_char_face and similar
547 functions too often which is expensive. Values that are too large
548 let us call compute_char_face and alike too often because we
549 might not be interested in text properties that far away. */
551 #define TEXT_PROP_DISTANCE_LIMIT 100
553 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
554 iterator state and later restore it. This is needed because the
555 bidi iterator on bidi.c keeps a stacked cache of its states, which
556 is really a singleton. When we use scratch iterator objects to
557 move around the buffer, we can cause the bidi cache to be pushed or
558 popped, and therefore we need to restore the cache state when we
559 return to the original iterator. */
560 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
561 do { \
562 if (CACHE) \
563 bidi_unshelve_cache (CACHE, true); \
564 ITCOPY = ITORIG; \
565 CACHE = bidi_shelve_cache (); \
566 } while (false)
568 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
569 do { \
570 if (pITORIG != pITCOPY) \
571 *(pITORIG) = *(pITCOPY); \
572 bidi_unshelve_cache (CACHE, false); \
573 CACHE = NULL; \
574 } while (false)
576 /* Functions to mark elements as needing redisplay. */
577 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
579 void
580 redisplay_other_windows (void)
582 if (!windows_or_buffers_changed)
583 windows_or_buffers_changed = REDISPLAY_SOME;
586 void
587 wset_redisplay (struct window *w)
589 /* Beware: selected_window can be nil during early stages. */
590 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
591 redisplay_other_windows ();
592 w->redisplay = true;
595 void
596 fset_redisplay (struct frame *f)
598 redisplay_other_windows ();
599 f->redisplay = true;
602 void
603 bset_redisplay (struct buffer *b)
605 int count = buffer_window_count (b);
606 if (count > 0)
608 /* ... it's visible in other window than selected, */
609 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
610 redisplay_other_windows ();
611 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
612 so that if we later set windows_or_buffers_changed, this buffer will
613 not be omitted. */
614 b->text->redisplay = true;
618 void
619 bset_update_mode_line (struct buffer *b)
621 if (!update_mode_lines)
622 update_mode_lines = REDISPLAY_SOME;
623 b->text->redisplay = true;
626 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
627 Sset_buffer_redisplay, 4, 4, 0,
628 doc: /* Mark the current buffer for redisplay.
629 This function may be passed to `add-variable-watcher'. */)
630 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
632 bset_update_mode_line (current_buffer);
633 current_buffer->prevent_redisplay_optimizations_p = true;
634 return Qnil;
637 #ifdef GLYPH_DEBUG
639 /* True means print traces of redisplay if compiled with
640 GLYPH_DEBUG defined. */
642 bool trace_redisplay_p;
644 #endif /* GLYPH_DEBUG */
646 #ifdef DEBUG_TRACE_MOVE
647 /* True means trace with TRACE_MOVE to stderr. */
648 static bool trace_move;
650 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
651 #else
652 #define TRACE_MOVE(x) (void) 0
653 #endif
655 /* Buffer being redisplayed -- for redisplay_window_error. */
657 static struct buffer *displayed_buffer;
659 /* Value returned from text property handlers (see below). */
661 enum prop_handled
663 HANDLED_NORMALLY,
664 HANDLED_RECOMPUTE_PROPS,
665 HANDLED_OVERLAY_STRING_CONSUMED,
666 HANDLED_RETURN
669 /* A description of text properties that redisplay is interested
670 in. */
672 struct props
674 /* The symbol index of the name of the property. */
675 short name;
677 /* A unique index for the property. */
678 enum prop_idx idx;
680 /* A handler function called to set up iterator IT from the property
681 at IT's current position. Value is used to steer handle_stop. */
682 enum prop_handled (*handler) (struct it *it);
685 static enum prop_handled handle_face_prop (struct it *);
686 static enum prop_handled handle_invisible_prop (struct it *);
687 static enum prop_handled handle_display_prop (struct it *);
688 static enum prop_handled handle_composition_prop (struct it *);
689 static enum prop_handled handle_overlay_change (struct it *);
690 static enum prop_handled handle_fontified_prop (struct it *);
692 /* Properties handled by iterators. */
694 static struct props it_props[] =
696 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
697 /* Handle `face' before `display' because some sub-properties of
698 `display' need to know the face. */
699 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
700 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
701 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
702 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
703 {0, 0, NULL}
706 /* Value is the position described by X. If X is a marker, value is
707 the marker_position of X. Otherwise, value is X. */
709 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
711 /* Enumeration returned by some move_it_.* functions internally. */
713 enum move_it_result
715 /* Not used. Undefined value. */
716 MOVE_UNDEFINED,
718 /* Move ended at the requested buffer position or ZV. */
719 MOVE_POS_MATCH_OR_ZV,
721 /* Move ended at the requested X pixel position. */
722 MOVE_X_REACHED,
724 /* Move within a line ended at the end of a line that must be
725 continued. */
726 MOVE_LINE_CONTINUED,
728 /* Move within a line ended at the end of a line that would
729 be displayed truncated. */
730 MOVE_LINE_TRUNCATED,
732 /* Move within a line ended at a line end. */
733 MOVE_NEWLINE_OR_CR
736 /* This counter is used to clear the face cache every once in a while
737 in redisplay_internal. It is incremented for each redisplay.
738 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
739 cleared. */
741 #define CLEAR_FACE_CACHE_COUNT 500
742 static int clear_face_cache_count;
744 /* Similarly for the image cache. */
746 #ifdef HAVE_WINDOW_SYSTEM
747 #define CLEAR_IMAGE_CACHE_COUNT 101
748 static int clear_image_cache_count;
750 /* Null glyph slice */
751 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
752 #endif
754 /* True while redisplay_internal is in progress. */
756 bool redisplaying_p;
758 /* If a string, XTread_socket generates an event to display that string.
759 (The display is done in read_char.) */
761 Lisp_Object help_echo_string;
762 Lisp_Object help_echo_window;
763 Lisp_Object help_echo_object;
764 ptrdiff_t help_echo_pos;
766 /* Temporary variable for XTread_socket. */
768 Lisp_Object previous_help_echo_string;
770 /* Platform-independent portion of hourglass implementation. */
772 #ifdef HAVE_WINDOW_SYSTEM
774 /* True means an hourglass cursor is currently shown. */
775 static bool hourglass_shown_p;
777 /* If non-null, an asynchronous timer that, when it expires, displays
778 an hourglass cursor on all frames. */
779 static struct atimer *hourglass_atimer;
781 #endif /* HAVE_WINDOW_SYSTEM */
783 /* Default number of seconds to wait before displaying an hourglass
784 cursor. */
785 #define DEFAULT_HOURGLASS_DELAY 1
787 #ifdef HAVE_WINDOW_SYSTEM
789 /* Default pixel width of `thin-space' display method. */
790 #define THIN_SPACE_WIDTH 1
792 #endif /* HAVE_WINDOW_SYSTEM */
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, bool);
798 static void mark_window_display_accurate_1 (struct window *, bool);
799 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
800 static bool cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, bool);
803 static void handle_line_prefix (struct it *);
805 static void handle_stop_backwards (struct it *, ptrdiff_t);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static bool current_message_1 (ptrdiff_t, Lisp_Object);
809 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static bool set_message_1 (ptrdiff_t, Lisp_Object);
812 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static void extend_face_to_end_of_line (struct it *);
816 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
817 static void push_it (struct it *, struct text_pos *);
818 static void iterate_out_of_display_property (struct it *);
819 static void pop_it (struct it *);
820 static void redisplay_internal (void);
821 static void echo_area_display (bool);
822 static void block_buffer_flips (void);
823 static void unblock_buffer_flips (void);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, bool);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static bool set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
831 int, int);
832 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
833 static bool update_menu_bar (struct frame *, bool, bool);
834 static bool try_window_reusing_current_matrix (struct window *);
835 static int try_window_id (struct window *);
836 static void maybe_produce_line_number (struct it *);
837 static bool should_produce_line_number (struct it *);
838 static bool display_line (struct it *, int);
839 static int display_mode_lines (struct window *);
840 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
841 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
842 Lisp_Object, bool);
843 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
844 Lisp_Object);
845 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
846 static void display_menu_bar (struct window *);
847 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
848 ptrdiff_t *);
849 static void pint2str (register char *, register int, register ptrdiff_t);
851 static int display_string (const char *, Lisp_Object, Lisp_Object,
852 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
853 static void compute_line_metrics (struct it *);
854 static void run_redisplay_end_trigger_hook (struct it *);
855 static bool get_overlay_strings (struct it *, ptrdiff_t);
856 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
857 static void next_overlay_string (struct it *);
858 static void reseat (struct it *, struct text_pos, bool);
859 static void reseat_1 (struct it *, struct text_pos, bool);
860 static bool next_element_from_display_vector (struct it *);
861 static bool next_element_from_string (struct it *);
862 static bool next_element_from_c_string (struct it *);
863 static bool next_element_from_buffer (struct it *);
864 static bool next_element_from_composition (struct it *);
865 static bool next_element_from_image (struct it *);
866 static bool next_element_from_stretch (struct it *);
867 static bool next_element_from_xwidget (struct it *);
868 static void load_overlay_strings (struct it *, ptrdiff_t);
869 static bool get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
872 enum move_operation_enum);
873 static void get_visually_first_element (struct it *);
874 static void compute_stop_pos (struct it *);
875 static int face_before_or_after_it_pos (struct it *, bool);
876 static ptrdiff_t next_overlay_change (ptrdiff_t);
877 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
878 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
879 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
880 Lisp_Object, struct text_pos *,
881 ptrdiff_t, int, bool, bool);
882 static int underlying_face_id (struct it *);
884 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
885 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
887 #ifdef HAVE_WINDOW_SYSTEM
889 static void update_tool_bar (struct frame *, bool);
890 static void x_draw_bottom_divider (struct window *w);
891 static void notice_overwritten_cursor (struct window *,
892 enum glyph_row_area,
893 int, int, int, int);
894 static int normal_char_height (struct font *, int);
895 static void normal_char_ascent_descent (struct font *, int, int *, int *);
897 static void append_stretch_glyph (struct it *, Lisp_Object,
898 int, int, int);
900 static Lisp_Object get_it_property (struct it *, Lisp_Object);
901 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
902 struct font *, int, bool);
904 #endif /* HAVE_WINDOW_SYSTEM */
906 static void produce_special_glyphs (struct it *, enum display_element_type);
907 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
908 static bool coords_in_mouse_face_p (struct window *, int, int);
912 /***********************************************************************
913 Window display dimensions
914 ***********************************************************************/
916 /* Return the bottom boundary y-position for text lines in window W.
917 This is the first y position at which a line cannot start.
918 It is relative to the top of the window.
920 This is the height of W minus the height of a mode line, if any. */
923 window_text_bottom_y (struct window *w)
925 int height = WINDOW_PIXEL_HEIGHT (w);
927 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
929 if (window_wants_mode_line (w))
930 height -= CURRENT_MODE_LINE_HEIGHT (w);
932 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
934 return height;
937 /* Return the pixel width of display area AREA of window W.
938 ANY_AREA means return the total width of W, not including
939 fringes to the left and right of the window. */
942 window_box_width (struct window *w, enum glyph_row_area area)
944 int width = w->pixel_width;
946 if (!w->pseudo_window_p)
948 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
949 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
951 if (area == TEXT_AREA)
952 width -= (WINDOW_MARGINS_WIDTH (w)
953 + WINDOW_FRINGES_WIDTH (w));
954 else if (area == LEFT_MARGIN_AREA)
955 width = WINDOW_LEFT_MARGIN_WIDTH (w);
956 else if (area == RIGHT_MARGIN_AREA)
957 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
960 /* With wide margins, fringes, etc. we might end up with a negative
961 width, correct that here. */
962 return max (0, width);
966 /* Return the pixel height of the display area of window W, not
967 including mode lines of W, if any. */
970 window_box_height (struct window *w)
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_PIXEL_HEIGHT (w);
975 eassert (height >= 0);
977 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
978 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
980 /* Note: the code below that determines the mode-line/header-line
981 height is essentially the same as that contained in the macro
982 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
983 the appropriate glyph row has its `mode_line_p' flag set,
984 and if it doesn't, uses estimate_mode_line_height instead. */
986 if (window_wants_mode_line (w))
988 struct glyph_row *ml_row
989 = (w->current_matrix && w->current_matrix->rows
990 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
991 : 0);
992 if (ml_row && ml_row->mode_line_p)
993 height -= ml_row->height;
994 else
995 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
998 if (window_wants_header_line (w))
1000 struct glyph_row *hl_row
1001 = (w->current_matrix && w->current_matrix->rows
1002 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1003 : 0);
1004 if (hl_row && hl_row->mode_line_p)
1005 height -= hl_row->height;
1006 else
1007 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1010 /* With a very small font and a mode-line that's taller than
1011 default, we might end up with a negative height. */
1012 return max (0, height);
1015 /* Return the window-relative coordinate of the left edge of display
1016 area AREA of window W. ANY_AREA means return the left edge of the
1017 whole window, to the right of the left fringe of W. */
1020 window_box_left_offset (struct window *w, enum glyph_row_area area)
1022 int x;
1024 if (w->pseudo_window_p)
1025 return 0;
1027 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1031 + window_box_width (w, LEFT_MARGIN_AREA));
1032 else if (area == RIGHT_MARGIN_AREA)
1033 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1034 + window_box_width (w, LEFT_MARGIN_AREA)
1035 + window_box_width (w, TEXT_AREA)
1036 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1038 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1039 else if (area == LEFT_MARGIN_AREA
1040 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1041 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1043 /* Don't return more than the window's pixel width. */
1044 return min (x, w->pixel_width);
1048 /* Return the window-relative coordinate of the right edge of display
1049 area AREA of window W. ANY_AREA means return the right edge of the
1050 whole window, to the left of the right fringe of W. */
1052 static int
1053 window_box_right_offset (struct window *w, enum glyph_row_area area)
1055 /* Don't return more than the window's pixel width. */
1056 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1057 w->pixel_width);
1060 /* Return the frame-relative coordinate of the left edge of display
1061 area AREA of window W. ANY_AREA means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left (struct window *w, enum glyph_row_area area)
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1076 return x;
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. ANY_AREA means return the right edge of the
1082 whole window, to the left of the right fringe of W. */
1085 window_box_right (struct window *w, enum glyph_row_area area)
1087 return window_box_left (w, area) + window_box_width (w, area);
1090 /* Get the bounding box of the display area AREA of window W, without
1091 mode lines, in frame-relative coordinates. ANY_AREA means the
1092 whole window, not including the left and right fringes of
1093 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1094 coordinates of the upper-left corner of the box. Return in
1095 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1097 void
1098 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1099 int *box_y, int *box_width, int *box_height)
1101 if (box_width)
1102 *box_width = window_box_width (w, area);
1103 if (box_height)
1104 *box_height = window_box_height (w);
1105 if (box_x)
1106 *box_x = window_box_left (w, area);
1107 if (box_y)
1109 *box_y = WINDOW_TOP_EDGE_Y (w);
1110 if (window_wants_header_line (w))
1111 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1115 #ifdef HAVE_WINDOW_SYSTEM
1117 /* Get the bounding box of the display area AREA of window W, without
1118 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1119 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1120 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1121 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1122 box. */
1124 static void
1125 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1126 int *bottom_right_x, int *bottom_right_y)
1128 window_box (w, ANY_AREA, top_left_x, top_left_y,
1129 bottom_right_x, bottom_right_y);
1130 *bottom_right_x += *top_left_x;
1131 *bottom_right_y += *top_left_y;
1134 #endif /* HAVE_WINDOW_SYSTEM */
1136 /***********************************************************************
1137 Utilities
1138 ***********************************************************************/
1140 /* Return the bottom y-position of the line the iterator IT is in.
1141 This can modify IT's settings. */
1144 line_bottom_y (struct it *it)
1146 int line_height = it->max_ascent + it->max_descent;
1147 int line_top_y = it->current_y;
1149 if (line_height == 0)
1151 if (last_height)
1152 line_height = last_height;
1153 else if (IT_CHARPOS (*it) < ZV)
1155 move_it_by_lines (it, 1);
1156 line_height = (it->max_ascent || it->max_descent
1157 ? it->max_ascent + it->max_descent
1158 : last_height);
1160 else
1162 struct glyph_row *row = it->glyph_row;
1164 /* Use the default character height. */
1165 it->glyph_row = NULL;
1166 it->what = IT_CHARACTER;
1167 it->c = ' ';
1168 it->len = 1;
1169 PRODUCE_GLYPHS (it);
1170 line_height = it->ascent + it->descent;
1171 it->glyph_row = row;
1175 return line_top_y + line_height;
1178 DEFUN ("line-pixel-height", Fline_pixel_height,
1179 Sline_pixel_height, 0, 0, 0,
1180 doc: /* Return height in pixels of text line in the selected window.
1182 Value is the height in pixels of the line at point. */)
1183 (void)
1185 struct it it;
1186 struct text_pos pt;
1187 struct window *w = XWINDOW (selected_window);
1188 struct buffer *old_buffer = NULL;
1189 Lisp_Object result;
1191 if (XBUFFER (w->contents) != current_buffer)
1193 old_buffer = current_buffer;
1194 set_buffer_internal_1 (XBUFFER (w->contents));
1196 SET_TEXT_POS (pt, PT, PT_BYTE);
1197 start_display (&it, w, pt);
1198 /* Start from the beginning of the screen line, to make sure we
1199 traverse all of its display elements, and thus capture the
1200 correct metrics. */
1201 move_it_by_lines (&it, 0);
1202 it.vpos = it.current_y = 0;
1203 last_height = 0;
1204 result = make_number (line_bottom_y (&it));
1205 if (old_buffer)
1206 set_buffer_internal_1 (old_buffer);
1208 return result;
1211 /* Return the default pixel height of text lines in window W. The
1212 value is the canonical height of the W frame's default font, plus
1213 any extra space required by the line-spacing variable or frame
1214 parameter.
1216 Implementation note: this ignores any line-spacing text properties
1217 put on the newline characters. This is because those properties
1218 only affect the _screen_ line ending in the newline (i.e., in a
1219 continued line, only the last screen line will be affected), which
1220 means only a small number of lines in a buffer can ever use this
1221 feature. Since this function is used to compute the default pixel
1222 equivalent of text lines in a window, we can safely ignore those
1223 few lines. For the same reasons, we ignore the line-height
1224 properties. */
1226 default_line_pixel_height (struct window *w)
1228 struct frame *f = WINDOW_XFRAME (w);
1229 int height = FRAME_LINE_HEIGHT (f);
1231 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1233 struct buffer *b = XBUFFER (w->contents);
1234 Lisp_Object val = BVAR (b, extra_line_spacing);
1236 if (NILP (val))
1237 val = BVAR (&buffer_defaults, extra_line_spacing);
1238 if (!NILP (val))
1240 if (RANGED_INTEGERP (0, val, INT_MAX))
1241 height += XFASTINT (val);
1242 else if (FLOATP (val))
1244 int addon = XFLOAT_DATA (val) * height + 0.5;
1246 if (addon >= 0)
1247 height += addon;
1250 else
1251 height += f->extra_line_spacing;
1254 return height;
1257 /* Subroutine of pos_visible_p below. Extracts a display string, if
1258 any, from the display spec given as its argument. */
1259 static Lisp_Object
1260 string_from_display_spec (Lisp_Object spec)
1262 if (VECTORP (spec))
1264 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1265 if (STRINGP (AREF (spec, i)))
1266 return AREF (spec, i);
1268 else
1270 for (; CONSP (spec); spec = XCDR (spec))
1271 if (STRINGP (XCAR (spec)))
1272 return XCAR (spec);
1274 return spec;
1278 /* Limit insanely large values of W->hscroll on frame F to the largest
1279 value that will still prevent first_visible_x and last_visible_x of
1280 'struct it' from overflowing an int. */
1281 static int
1282 window_hscroll_limited (struct window *w, struct frame *f)
1284 ptrdiff_t window_hscroll = w->hscroll;
1285 int window_text_width = window_box_width (w, TEXT_AREA);
1286 int colwidth = FRAME_COLUMN_WIDTH (f);
1288 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1289 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1291 return window_hscroll;
1294 /* Return true if position CHARPOS is visible in window W.
1295 CHARPOS < 0 means return info about WINDOW_END position.
1296 If visible, set *X and *Y to pixel coordinates of top left corner.
1297 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1298 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1300 bool
1301 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1302 int *rtop, int *rbot, int *rowh, int *vpos)
1304 struct it it;
1305 void *itdata = bidi_shelve_cache ();
1306 struct text_pos top;
1307 bool visible_p = false;
1308 struct buffer *old_buffer = NULL;
1309 bool r2l = false;
1311 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1312 return visible_p;
1314 if (XBUFFER (w->contents) != current_buffer)
1316 old_buffer = current_buffer;
1317 set_buffer_internal_1 (XBUFFER (w->contents));
1320 SET_TEXT_POS_FROM_MARKER (top, w->start);
1321 /* Scrolling a minibuffer window via scroll bar when the echo area
1322 shows long text sometimes resets the minibuffer contents behind
1323 our backs. Also, someone might narrow-to-region and immediately
1324 call a scroll function. */
1325 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1328 /* If the top of the window is after CHARPOS, the latter is surely
1329 not visible. */
1330 if (charpos >= 0 && CHARPOS (top) > charpos)
1331 return visible_p;
1333 /* Some Lisp hook could call us in the middle of redisplaying this
1334 very window. If, by some bad luck, we are retrying redisplay
1335 because we found that the mode-line height and/or header-line
1336 height needs to be updated, the assignment of mode_line_height
1337 and header_line_height below could disrupt that, due to the
1338 selected/nonselected window dance during mode-line display, and
1339 we could infloop. Avoid that. */
1340 int prev_mode_line_height = w->mode_line_height;
1341 int prev_header_line_height = w->header_line_height;
1342 /* Compute exact mode line heights. */
1343 if (window_wants_mode_line (w))
1345 Lisp_Object window_mode_line_format
1346 = window_parameter (w, Qmode_line_format);
1348 w->mode_line_height
1349 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1350 NILP (window_mode_line_format)
1351 ? BVAR (current_buffer, mode_line_format)
1352 : window_mode_line_format);
1355 if (window_wants_header_line (w))
1357 Lisp_Object window_header_line_format
1358 = window_parameter (w, Qheader_line_format);
1360 w->header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 NILP (window_header_line_format)
1363 ? BVAR (current_buffer, header_line_format)
1364 : window_header_line_format);
1367 start_display (&it, w, top);
1368 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1369 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1371 if (charpos >= 0
1372 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1373 && IT_CHARPOS (it) >= charpos)
1374 /* When scanning backwards under bidi iteration, move_it_to
1375 stops at or _before_ CHARPOS, because it stops at or to
1376 the _right_ of the character at CHARPOS. */
1377 || (it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) <= charpos)))
1380 /* We have reached CHARPOS, or passed it. How the call to
1381 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1382 or covered by a display property, move_it_to stops at the end
1383 of the invisible text, to the right of CHARPOS. (ii) If
1384 CHARPOS is in a display vector, move_it_to stops on its last
1385 glyph. */
1386 int top_x = it.current_x;
1387 int top_y = it.current_y;
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1389 int bottom_y;
1390 struct it save_it;
1391 void *save_it_data = NULL;
1393 /* Calling line_bottom_y may change it.method, it.position, etc. */
1394 SAVE_IT (save_it, it, save_it_data);
1395 last_height = 0;
1396 bottom_y = line_bottom_y (&it);
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = true;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 /* Why 10? because we don't know how many canonical lines
1414 will the height of the next line(s) be. So we guess. */
1415 int ten_more_lines = 10 * default_line_pixel_height (w);
1417 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1418 MOVE_TO_POS | MOVE_TO_Y);
1419 if (it.current_y > top_y)
1420 visible_p = false;
1423 RESTORE_IT (&it, &save_it, save_it_data);
1424 if (visible_p)
1426 if (it.method == GET_FROM_DISPLAY_VECTOR)
1428 /* We stopped on the last glyph of a display vector.
1429 Try and recompute. Hack alert! */
1430 if (charpos < 2 || top.charpos >= charpos)
1431 top_x = it.glyph_row->x;
1432 else
1434 struct it it2, it2_prev;
1435 /* The idea is to get to the previous buffer
1436 position, consume the character there, and use
1437 the pixel coordinates we get after that. But if
1438 the previous buffer position is also displayed
1439 from a display vector, we need to consume all of
1440 the glyphs from that display vector. */
1441 start_display (&it2, w, top);
1442 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* If we didn't get to CHARPOS - 1, there's some
1444 replacing display property at that position, and
1445 we stopped after it. That is exactly the place
1446 whose coordinates we want. */
1447 if (IT_CHARPOS (it2) != charpos - 1)
1448 it2_prev = it2;
1449 else
1451 /* Iterate until we get out of the display
1452 vector that displays the character at
1453 CHARPOS - 1. */
1454 do {
1455 get_next_display_element (&it2);
1456 PRODUCE_GLYPHS (&it2);
1457 it2_prev = it2;
1458 set_iterator_to_next (&it2, true);
1459 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1460 && IT_CHARPOS (it2) < charpos);
1462 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1463 || it2_prev.current_x > it2_prev.last_visible_x)
1464 top_x = it.glyph_row->x;
1465 else
1467 top_x = it2_prev.current_x;
1468 top_y = it2_prev.current_y;
1472 else if (IT_CHARPOS (it) != charpos)
1474 Lisp_Object cpos = make_number (charpos);
1475 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1476 Lisp_Object string = string_from_display_spec (spec);
1477 struct text_pos tpos;
1478 bool newline_in_string
1479 = (STRINGP (string)
1480 && memchr (SDATA (string), '\n', SBYTES (string)));
1482 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1483 bool replacing_spec_p
1484 = (!NILP (spec)
1485 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1486 charpos, FRAME_WINDOW_P (it.f)));
1487 /* The tricky code below is needed because there's a
1488 discrepancy between move_it_to and how we set cursor
1489 when PT is at the beginning of a portion of text
1490 covered by a display property or an overlay with a
1491 display property, or the display line ends in a
1492 newline from a display string. move_it_to will stop
1493 _after_ such display strings, whereas
1494 set_cursor_from_row conspires with cursor_row_p to
1495 place the cursor on the first glyph produced from the
1496 display string. */
1498 /* We have overshoot PT because it is covered by a
1499 display property that replaces the text it covers.
1500 If the string includes embedded newlines, we are also
1501 in the wrong display line. Backtrack to the correct
1502 line, where the display property begins. */
1503 if (replacing_spec_p)
1505 Lisp_Object startpos, endpos;
1506 EMACS_INT start, end;
1507 struct it it3;
1509 /* Find the first and the last buffer positions
1510 covered by the display string. */
1511 endpos =
1512 Fnext_single_char_property_change (cpos, Qdisplay,
1513 Qnil, Qnil);
1514 startpos =
1515 Fprevious_single_char_property_change (endpos, Qdisplay,
1516 Qnil, Qnil);
1517 start = XFASTINT (startpos);
1518 end = XFASTINT (endpos);
1519 /* Move to the last buffer position before the
1520 display property. */
1521 start_display (&it3, w, top);
1522 if (start > CHARPOS (top))
1523 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1524 /* Move forward one more line if the position before
1525 the display string is a newline or if it is the
1526 rightmost character on a line that is
1527 continued or word-wrapped. */
1528 if (it3.method == GET_FROM_BUFFER
1529 && (it3.c == '\n'
1530 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1531 move_it_by_lines (&it3, 1);
1532 else if (move_it_in_display_line_to (&it3, -1,
1533 it3.current_x
1534 + it3.pixel_width,
1535 MOVE_TO_X)
1536 == MOVE_LINE_CONTINUED)
1538 move_it_by_lines (&it3, 1);
1539 /* When we are under word-wrap, the #$@%!
1540 move_it_by_lines moves 2 lines, so we need to
1541 fix that up. */
1542 if (it3.line_wrap == WORD_WRAP)
1543 move_it_by_lines (&it3, -1);
1546 /* Record the vertical coordinate of the display
1547 line where we wound up. */
1548 top_y = it3.current_y;
1549 if (it3.bidi_p)
1551 /* When characters are reordered for display,
1552 the character displayed to the left of the
1553 display string could be _after_ the display
1554 property in the logical order. Use the
1555 smallest vertical position of these two. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1558 if (it3.current_y < top_y)
1559 top_y = it3.current_y;
1561 /* Move from the top of the window to the beginning
1562 of the display line where the display string
1563 begins. */
1564 start_display (&it3, w, top);
1565 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1566 /* If it3_moved stays false after the 'while' loop
1567 below, that means we already were at a newline
1568 before the loop (e.g., the display string begins
1569 with a newline), so we don't need to (and cannot)
1570 inspect the glyphs of it3.glyph_row, because
1571 PRODUCE_GLYPHS will not produce anything for a
1572 newline, and thus it3.glyph_row stays at its
1573 stale content it got at top of the window. */
1574 bool it3_moved = false;
1575 /* Finally, advance the iterator until we hit the
1576 first display element whose character position is
1577 CHARPOS, or until the first newline from the
1578 display string, which signals the end of the
1579 display line. */
1580 while (get_next_display_element (&it3))
1582 PRODUCE_GLYPHS (&it3);
1583 if (IT_CHARPOS (it3) == charpos
1584 || ITERATOR_AT_END_OF_LINE_P (&it3))
1585 break;
1586 it3_moved = true;
1587 set_iterator_to_next (&it3, false);
1589 top_x = it3.current_x - it3.pixel_width;
1590 /* Normally, we would exit the above loop because we
1591 found the display element whose character
1592 position is CHARPOS. For the contingency that we
1593 didn't, and stopped at the first newline from the
1594 display string, move back over the glyphs
1595 produced from the string, until we find the
1596 rightmost glyph not from the string. */
1597 if (it3_moved
1598 && newline_in_string
1599 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1601 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1602 + it3.glyph_row->used[TEXT_AREA];
1604 while (EQ ((g - 1)->object, string))
1606 --g;
1607 top_x -= g->pixel_width;
1609 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1610 + it3.glyph_row->used[TEXT_AREA]);
1615 *x = top_x;
1616 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1617 *rtop = max (0, window_top_y - top_y);
1618 *rbot = max (0, bottom_y - it.last_visible_y);
1619 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1620 - max (top_y, window_top_y)));
1621 *vpos = it.vpos;
1622 if (it.bidi_it.paragraph_dir == R2L)
1623 r2l = true;
1626 else
1628 /* Either we were asked to provide info about WINDOW_END, or
1629 CHARPOS is in the partially visible glyph row at end of
1630 window. */
1631 struct it it2;
1632 void *it2data = NULL;
1634 SAVE_IT (it2, it, it2data);
1635 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1636 move_it_by_lines (&it, 1);
1637 if (charpos < IT_CHARPOS (it)
1638 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1640 visible_p = true;
1641 RESTORE_IT (&it2, &it2, it2data);
1642 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1643 *x = it2.current_x;
1644 *y = it2.current_y + it2.max_ascent - it2.ascent;
1645 *rtop = max (0, -it2.current_y);
1646 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1647 - it.last_visible_y));
1648 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1649 it.last_visible_y)
1650 - max (it2.current_y,
1651 WINDOW_HEADER_LINE_HEIGHT (w))));
1652 *vpos = it2.vpos;
1653 if (it2.bidi_it.paragraph_dir == R2L)
1654 r2l = true;
1656 else
1657 bidi_unshelve_cache (it2data, true);
1659 bidi_unshelve_cache (itdata, false);
1661 if (old_buffer)
1662 set_buffer_internal_1 (old_buffer);
1664 if (visible_p)
1666 if (w->hscroll > 0)
1667 *x -=
1668 window_hscroll_limited (w, WINDOW_XFRAME (w))
1669 * WINDOW_FRAME_COLUMN_WIDTH (w);
1670 /* For lines in an R2L paragraph, we need to mirror the X pixel
1671 coordinate wrt the text area. For the reasons, see the
1672 commentary in buffer_posn_from_coords and the explanation of
1673 the geometry used by the move_it_* functions at the end of
1674 the large commentary near the beginning of this file. */
1675 if (r2l)
1676 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1679 #if false
1680 /* Debugging code. */
1681 if (visible_p)
1682 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1683 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1684 else
1685 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1686 #endif
1688 /* Restore potentially overwritten values. */
1689 w->mode_line_height = prev_mode_line_height;
1690 w->header_line_height = prev_header_line_height;
1692 return visible_p;
1696 /* Return the next character from STR. Return in *LEN the length of
1697 the character. This is like STRING_CHAR_AND_LENGTH but never
1698 returns an invalid character. If we find one, we return a `?', but
1699 with the length of the invalid character. */
1701 static int
1702 string_char_and_length (const unsigned char *str, int *len)
1704 int c;
1706 c = STRING_CHAR_AND_LENGTH (str, *len);
1707 if (!CHAR_VALID_P (c))
1708 /* We may not change the length here because other places in Emacs
1709 don't use this function, i.e. they silently accept invalid
1710 characters. */
1711 c = '?';
1713 return c;
1718 /* Given a position POS containing a valid character and byte position
1719 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1721 static struct text_pos
1722 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1724 eassert (STRINGP (string) && nchars >= 0);
1726 if (STRING_MULTIBYTE (string))
1728 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1729 int len;
1731 while (nchars--)
1733 string_char_and_length (p, &len);
1734 p += len;
1735 CHARPOS (pos) += 1;
1736 BYTEPOS (pos) += len;
1739 else
1740 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1742 return pos;
1746 /* Value is the text position, i.e. character and byte position,
1747 for character position CHARPOS in STRING. */
1749 static struct text_pos
1750 string_pos (ptrdiff_t charpos, Lisp_Object string)
1752 struct text_pos pos;
1753 eassert (STRINGP (string));
1754 eassert (charpos >= 0);
1755 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1756 return pos;
1760 /* Value is a text position, i.e. character and byte position, for
1761 character position CHARPOS in C string S. MULTIBYTE_P
1762 means recognize multibyte characters. */
1764 static struct text_pos
1765 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1767 struct text_pos pos;
1769 eassert (s != NULL);
1770 eassert (charpos >= 0);
1772 if (multibyte_p)
1774 int len;
1776 SET_TEXT_POS (pos, 0, 0);
1777 while (charpos--)
1779 string_char_and_length ((const unsigned char *) s, &len);
1780 s += len;
1781 CHARPOS (pos) += 1;
1782 BYTEPOS (pos) += len;
1785 else
1786 SET_TEXT_POS (pos, charpos, charpos);
1788 return pos;
1792 /* Value is the number of characters in C string S. MULTIBYTE_P
1793 means recognize multibyte characters. */
1795 static ptrdiff_t
1796 number_of_chars (const char *s, bool multibyte_p)
1798 ptrdiff_t nchars;
1800 if (multibyte_p)
1802 ptrdiff_t rest = strlen (s);
1803 int len;
1804 const unsigned char *p = (const unsigned char *) s;
1806 for (nchars = 0; rest > 0; ++nchars)
1808 string_char_and_length (p, &len);
1809 rest -= len, p += len;
1812 else
1813 nchars = strlen (s);
1815 return nchars;
1819 /* Compute byte position NEWPOS->bytepos corresponding to
1820 NEWPOS->charpos. POS is a known position in string STRING.
1821 NEWPOS->charpos must be >= POS.charpos. */
1823 static void
1824 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1826 eassert (STRINGP (string));
1827 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1829 if (STRING_MULTIBYTE (string))
1830 *newpos = string_pos_nchars_ahead (pos, string,
1831 CHARPOS (*newpos) - CHARPOS (pos));
1832 else
1833 BYTEPOS (*newpos) = CHARPOS (*newpos);
1836 /* EXPORT:
1837 Return an estimation of the pixel height of mode or header lines on
1838 frame F. FACE_ID specifies what line's height to estimate. */
1841 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1843 #ifdef HAVE_WINDOW_SYSTEM
1844 if (FRAME_WINDOW_P (f))
1846 int height = FONT_HEIGHT (FRAME_FONT (f));
1848 /* This function is called so early when Emacs starts that the face
1849 cache and mode line face are not yet initialized. */
1850 if (FRAME_FACE_CACHE (f))
1852 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1853 if (face)
1855 if (face->font)
1856 height = normal_char_height (face->font, -1);
1857 if (face->box_line_width > 0)
1858 height += 2 * face->box_line_width;
1862 return height;
1864 #endif
1866 return 1;
1869 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1870 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1871 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1872 not force the value into range. */
1874 void
1875 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1876 NativeRectangle *bounds, bool noclip)
1879 #ifdef HAVE_WINDOW_SYSTEM
1880 if (FRAME_WINDOW_P (f))
1882 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1883 even for negative values. */
1884 if (pix_x < 0)
1885 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1886 if (pix_y < 0)
1887 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1889 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1890 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1892 if (bounds)
1893 STORE_NATIVE_RECT (*bounds,
1894 FRAME_COL_TO_PIXEL_X (f, pix_x),
1895 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1896 FRAME_COLUMN_WIDTH (f) - 1,
1897 FRAME_LINE_HEIGHT (f) - 1);
1899 /* PXW: Should we clip pixels before converting to columns/lines? */
1900 if (!noclip)
1902 if (pix_x < 0)
1903 pix_x = 0;
1904 else if (pix_x > FRAME_TOTAL_COLS (f))
1905 pix_x = FRAME_TOTAL_COLS (f);
1907 if (pix_y < 0)
1908 pix_y = 0;
1909 else if (pix_y > FRAME_TOTAL_LINES (f))
1910 pix_y = FRAME_TOTAL_LINES (f);
1913 #endif
1915 *x = pix_x;
1916 *y = pix_y;
1920 /* Find the glyph under window-relative coordinates X/Y in window W.
1921 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1922 strings. Return in *HPOS and *VPOS the row and column number of
1923 the glyph found. Return in *AREA the glyph area containing X.
1924 Value is a pointer to the glyph found or null if X/Y is not on
1925 text, or we can't tell because W's current matrix is not up to
1926 date. */
1928 static struct glyph *
1929 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1930 int *dx, int *dy, int *area)
1932 struct glyph *glyph, *end;
1933 struct glyph_row *row = NULL;
1934 int x0, i;
1936 /* Find row containing Y. Give up if some row is not enabled. */
1937 for (i = 0; i < w->current_matrix->nrows; ++i)
1939 row = MATRIX_ROW (w->current_matrix, i);
1940 if (!row->enabled_p)
1941 return NULL;
1942 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1943 break;
1946 *vpos = i;
1947 *hpos = 0;
1949 /* Give up if Y is not in the window. */
1950 if (i == w->current_matrix->nrows)
1951 return NULL;
1953 /* Get the glyph area containing X. */
1954 if (w->pseudo_window_p)
1956 *area = TEXT_AREA;
1957 x0 = 0;
1959 else
1961 if (x < window_box_left_offset (w, TEXT_AREA))
1963 *area = LEFT_MARGIN_AREA;
1964 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1966 else if (x < window_box_right_offset (w, TEXT_AREA))
1968 *area = TEXT_AREA;
1969 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1971 else
1973 *area = RIGHT_MARGIN_AREA;
1974 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1978 /* Find glyph containing X. */
1979 glyph = row->glyphs[*area];
1980 end = glyph + row->used[*area];
1981 x -= x0;
1982 while (glyph < end && x >= glyph->pixel_width)
1984 x -= glyph->pixel_width;
1985 ++glyph;
1988 if (glyph == end)
1989 return NULL;
1991 if (dx)
1993 *dx = x;
1994 *dy = y - (row->y + row->ascent - glyph->ascent);
1997 *hpos = glyph - row->glyphs[*area];
1998 return glyph;
2001 /* Convert frame-relative x/y to coordinates relative to window W.
2002 Takes pseudo-windows into account. */
2004 static void
2005 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2007 if (w->pseudo_window_p)
2009 /* A pseudo-window is always full-width, and starts at the
2010 left edge of the frame, plus a frame border. */
2011 struct frame *f = XFRAME (w->frame);
2012 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2013 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2015 else
2017 *x -= WINDOW_LEFT_EDGE_X (w);
2018 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2022 #ifdef HAVE_WINDOW_SYSTEM
2024 /* EXPORT:
2025 Return in RECTS[] at most N clipping rectangles for glyph string S.
2026 Return the number of stored rectangles. */
2029 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2031 XRectangle r;
2033 if (n <= 0)
2034 return 0;
2036 if (s->row->full_width_p)
2038 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2039 r.x = WINDOW_LEFT_EDGE_X (s->w);
2040 if (s->row->mode_line_p)
2041 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2042 else
2043 r.width = WINDOW_PIXEL_WIDTH (s->w);
2045 /* Unless displaying a mode or menu bar line, which are always
2046 fully visible, clip to the visible part of the row. */
2047 if (s->w->pseudo_window_p)
2048 r.height = s->row->visible_height;
2049 else
2050 r.height = s->height;
2052 else
2054 /* This is a text line that may be partially visible. */
2055 r.x = window_box_left (s->w, s->area);
2056 r.width = window_box_width (s->w, s->area);
2057 r.height = s->row->visible_height;
2060 if (s->clip_head)
2061 if (r.x < s->clip_head->x)
2063 if (r.width >= s->clip_head->x - r.x)
2064 r.width -= s->clip_head->x - r.x;
2065 else
2066 r.width = 0;
2067 r.x = s->clip_head->x;
2069 if (s->clip_tail)
2070 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2072 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2073 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2074 else
2075 r.width = 0;
2078 /* If S draws overlapping rows, it's sufficient to use the top and
2079 bottom of the window for clipping because this glyph string
2080 intentionally draws over other lines. */
2081 if (s->for_overlaps)
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 r.height = window_text_bottom_y (s->w) - r.y;
2086 /* Alas, the above simple strategy does not work for the
2087 environments with anti-aliased text: if the same text is
2088 drawn onto the same place multiple times, it gets thicker.
2089 If the overlap we are processing is for the erased cursor, we
2090 take the intersection with the rectangle of the cursor. */
2091 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2093 XRectangle rc, r_save = r;
2095 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2096 rc.y = s->w->phys_cursor.y;
2097 rc.width = s->w->phys_cursor_width;
2098 rc.height = s->w->phys_cursor_height;
2100 x_intersect_rectangles (&r_save, &rc, &r);
2103 else
2105 /* Don't use S->y for clipping because it doesn't take partially
2106 visible lines into account. For example, it can be negative for
2107 partially visible lines at the top of a window. */
2108 if (!s->row->full_width_p
2109 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2110 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2111 else
2112 r.y = max (0, s->row->y);
2115 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2117 /* If drawing the cursor, don't let glyph draw outside its
2118 advertised boundaries. Cleartype does this under some circumstances. */
2119 if (s->hl == DRAW_CURSOR)
2121 struct glyph *glyph = s->first_glyph;
2122 int height, max_y;
2124 if (s->x > r.x)
2126 if (r.width >= s->x - r.x)
2127 r.width -= s->x - r.x;
2128 else /* R2L hscrolled row with cursor outside text area */
2129 r.width = 0;
2130 r.x = s->x;
2132 r.width = min (r.width, glyph->pixel_width);
2134 /* If r.y is below window bottom, ensure that we still see a cursor. */
2135 height = min (glyph->ascent + glyph->descent,
2136 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2137 max_y = window_text_bottom_y (s->w) - height;
2138 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2139 if (s->ybase - glyph->ascent > max_y)
2141 r.y = max_y;
2142 r.height = height;
2144 else
2146 /* Don't draw cursor glyph taller than our actual glyph. */
2147 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2148 if (height < r.height)
2150 max_y = r.y + r.height;
2151 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2152 r.height = min (max_y - r.y, height);
2157 if (s->row->clip)
2159 XRectangle r_save = r;
2161 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2162 r.width = 0;
2165 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2166 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2168 #ifdef CONVERT_FROM_XRECT
2169 CONVERT_FROM_XRECT (r, *rects);
2170 #else
2171 *rects = r;
2172 #endif
2173 return 1;
2175 else
2177 /* If we are processing overlapping and allowed to return
2178 multiple clipping rectangles, we exclude the row of the glyph
2179 string from the clipping rectangle. This is to avoid drawing
2180 the same text on the environment with anti-aliasing. */
2181 #ifdef CONVERT_FROM_XRECT
2182 XRectangle rs[2];
2183 #else
2184 XRectangle *rs = rects;
2185 #endif
2186 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2188 if (s->for_overlaps & OVERLAPS_PRED)
2190 rs[i] = r;
2191 if (r.y + r.height > row_y)
2193 if (r.y < row_y)
2194 rs[i].height = row_y - r.y;
2195 else
2196 rs[i].height = 0;
2198 i++;
2200 if (s->for_overlaps & OVERLAPS_SUCC)
2202 rs[i] = r;
2203 if (r.y < row_y + s->row->visible_height)
2205 if (r.y + r.height > row_y + s->row->visible_height)
2207 rs[i].y = row_y + s->row->visible_height;
2208 rs[i].height = r.y + r.height - rs[i].y;
2210 else
2211 rs[i].height = 0;
2213 i++;
2216 n = i;
2217 #ifdef CONVERT_FROM_XRECT
2218 for (i = 0; i < n; i++)
2219 CONVERT_FROM_XRECT (rs[i], rects[i]);
2220 #endif
2221 return n;
2225 /* EXPORT:
2226 Return in *NR the clipping rectangle for glyph string S. */
2228 void
2229 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2231 get_glyph_string_clip_rects (s, nr, 1);
2235 /* EXPORT:
2236 Return the position and height of the phys cursor in window W.
2237 Set w->phys_cursor_width to width of phys cursor.
2240 void
2241 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2242 struct glyph *glyph, int *xp, int *yp, int *heightp)
2244 struct frame *f = XFRAME (WINDOW_FRAME (w));
2245 int x, y, wd, h, h0, y0, ascent;
2247 /* Compute the width of the rectangle to draw. If on a stretch
2248 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2249 rectangle as wide as the glyph, but use a canonical character
2250 width instead. */
2251 wd = glyph->pixel_width;
2253 x = w->phys_cursor.x;
2254 if (x < 0)
2256 wd += x;
2257 x = 0;
2260 if (glyph->type == STRETCH_GLYPH
2261 && !x_stretch_cursor_p)
2262 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2263 w->phys_cursor_width = wd;
2265 /* Don't let the hollow cursor glyph descend below the glyph row's
2266 ascent value, lest the hollow cursor looks funny. */
2267 y = w->phys_cursor.y;
2268 ascent = row->ascent;
2269 if (row->ascent < glyph->ascent)
2271 y -= glyph->ascent - row->ascent;
2272 ascent = glyph->ascent;
2275 /* If y is below window bottom, ensure that we still see a cursor. */
2276 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2278 h = max (h0, ascent + glyph->descent);
2279 h0 = min (h0, ascent + glyph->descent);
2281 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2282 if (y < y0)
2284 h = max (h - (y0 - y) + 1, h0);
2285 y = y0 - 1;
2287 else
2289 y0 = window_text_bottom_y (w) - h0;
2290 if (y > y0)
2292 h += y - y0;
2293 y = y0;
2297 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2298 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2299 *heightp = h;
2303 * Remember which glyph the mouse is over.
2306 void
2307 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2309 Lisp_Object window;
2310 struct window *w;
2311 struct glyph_row *r, *gr, *end_row;
2312 enum window_part part;
2313 enum glyph_row_area area;
2314 int x, y, width, height;
2316 /* Try to determine frame pixel position and size of the glyph under
2317 frame pixel coordinates X/Y on frame F. */
2319 if (window_resize_pixelwise)
2321 width = height = 1;
2322 goto virtual_glyph;
2324 else if (!f->glyphs_initialized_p
2325 || (window = window_from_coordinates (f, gx, gy, &part, false),
2326 NILP (window)))
2328 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2329 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2330 goto virtual_glyph;
2333 w = XWINDOW (window);
2334 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2335 height = WINDOW_FRAME_LINE_HEIGHT (w);
2337 x = window_relative_x_coord (w, part, gx);
2338 y = gy - WINDOW_TOP_EDGE_Y (w);
2340 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2341 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2343 if (w->pseudo_window_p)
2345 area = TEXT_AREA;
2346 part = ON_MODE_LINE; /* Don't adjust margin. */
2347 goto text_glyph;
2350 switch (part)
2352 case ON_LEFT_MARGIN:
2353 area = LEFT_MARGIN_AREA;
2354 goto text_glyph;
2356 case ON_RIGHT_MARGIN:
2357 area = RIGHT_MARGIN_AREA;
2358 goto text_glyph;
2360 case ON_HEADER_LINE:
2361 case ON_MODE_LINE:
2362 gr = (part == ON_HEADER_LINE
2363 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2364 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2365 gy = gr->y;
2366 area = TEXT_AREA;
2367 goto text_glyph_row_found;
2369 case ON_TEXT:
2370 area = TEXT_AREA;
2372 text_glyph:
2373 gr = 0; gy = 0;
2374 for (; r <= end_row && r->enabled_p; ++r)
2375 if (r->y + r->height > y)
2377 gr = r; gy = r->y;
2378 break;
2381 text_glyph_row_found:
2382 if (gr && gy <= y)
2384 struct glyph *g = gr->glyphs[area];
2385 struct glyph *end = g + gr->used[area];
2387 height = gr->height;
2388 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2389 if (gx + g->pixel_width > x)
2390 break;
2392 if (g < end)
2394 if (g->type == IMAGE_GLYPH)
2396 /* Don't remember when mouse is over image, as
2397 image may have hot-spots. */
2398 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2399 return;
2401 width = g->pixel_width;
2403 else
2405 /* Use nominal char spacing at end of line. */
2406 x -= gx;
2407 gx += (x / width) * width;
2410 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2412 gx += window_box_left_offset (w, area);
2413 /* Don't expand over the modeline to make sure the vertical
2414 drag cursor is shown early enough. */
2415 height = min (height,
2416 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2419 else
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2425 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2426 /* See comment above. */
2427 height = min (height,
2428 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2430 break;
2432 case ON_LEFT_FRINGE:
2433 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2434 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2435 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2436 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2437 goto row_glyph;
2439 case ON_RIGHT_FRINGE:
2440 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2441 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2442 : window_box_right_offset (w, TEXT_AREA));
2443 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2444 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2445 && !WINDOW_RIGHTMOST_P (w))
2446 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2447 /* Make sure the vertical border can get her own glyph to the
2448 right of the one we build here. */
2449 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2450 else
2451 width = WINDOW_PIXEL_WIDTH (w) - gx;
2452 else
2453 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2455 goto row_glyph;
2457 case ON_VERTICAL_BORDER:
2458 gx = WINDOW_PIXEL_WIDTH (w) - width;
2459 goto row_glyph;
2461 case ON_VERTICAL_SCROLL_BAR:
2462 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2464 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2465 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2466 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2467 : 0)));
2468 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2470 row_glyph:
2471 gr = 0, gy = 0;
2472 for (; r <= end_row && r->enabled_p; ++r)
2473 if (r->y + r->height > y)
2475 gr = r; gy = r->y;
2476 break;
2479 if (gr && gy <= y)
2480 height = gr->height;
2481 else
2483 /* Use nominal line height at end of window. */
2484 y -= gy;
2485 gy += (y / height) * height;
2487 break;
2489 case ON_RIGHT_DIVIDER:
2490 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2491 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2492 gy = 0;
2493 /* The bottom divider prevails. */
2494 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2495 goto add_edge;
2497 case ON_BOTTOM_DIVIDER:
2498 gx = 0;
2499 width = WINDOW_PIXEL_WIDTH (w);
2500 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2501 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2502 goto add_edge;
2504 default:
2506 virtual_glyph:
2507 /* If there is no glyph under the mouse, then we divide the screen
2508 into a grid of the smallest glyph in the frame, and use that
2509 as our "glyph". */
2511 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2512 round down even for negative values. */
2513 if (gx < 0)
2514 gx -= width - 1;
2515 if (gy < 0)
2516 gy -= height - 1;
2518 gx = (gx / width) * width;
2519 gy = (gy / height) * height;
2521 goto store_rect;
2524 add_edge:
2525 gx += WINDOW_LEFT_EDGE_X (w);
2526 gy += WINDOW_TOP_EDGE_Y (w);
2528 store_rect:
2529 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2531 /* Visible feedback for debugging. */
2532 #if false && defined HAVE_X_WINDOWS
2533 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2534 f->output_data.x->normal_gc,
2535 gx, gy, width, height);
2536 #endif
2540 #endif /* HAVE_WINDOW_SYSTEM */
2542 static void
2543 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2545 eassert (w);
2546 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2547 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2548 w->window_end_vpos
2549 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2552 static bool
2553 hscrolling_current_line_p (struct window *w)
2555 return (!w->suspend_auto_hscroll
2556 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2557 Qcurrent_line));
2560 /***********************************************************************
2561 Lisp form evaluation
2562 ***********************************************************************/
2564 /* Error handler for safe_eval and safe_call. */
2566 static Lisp_Object
2567 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2569 add_to_log ("Error during redisplay: %S signaled %S",
2570 Flist (nargs, args), arg);
2571 return Qnil;
2574 /* Call function FUNC with the rest of NARGS - 1 arguments
2575 following. Return the result, or nil if something went
2576 wrong. Prevent redisplay during the evaluation. */
2578 static Lisp_Object
2579 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2581 Lisp_Object val;
2583 if (inhibit_eval_during_redisplay)
2584 val = Qnil;
2585 else
2587 ptrdiff_t i;
2588 ptrdiff_t count = SPECPDL_INDEX ();
2589 Lisp_Object *args;
2590 USE_SAFE_ALLOCA;
2591 SAFE_ALLOCA_LISP (args, nargs);
2593 args[0] = func;
2594 for (i = 1; i < nargs; i++)
2595 args[i] = va_arg (ap, Lisp_Object);
2597 specbind (Qinhibit_redisplay, Qt);
2598 if (inhibit_quit)
2599 specbind (Qinhibit_quit, Qt);
2600 /* Use Qt to ensure debugger does not run,
2601 so there is no possibility of wanting to redisplay. */
2602 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2603 safe_eval_handler);
2604 SAFE_FREE ();
2605 val = unbind_to (count, val);
2608 return val;
2611 Lisp_Object
2612 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2614 Lisp_Object retval;
2615 va_list ap;
2617 va_start (ap, func);
2618 retval = safe__call (false, nargs, func, ap);
2619 va_end (ap);
2620 return retval;
2623 /* Call function FN with one argument ARG.
2624 Return the result, or nil if something went wrong. */
2626 Lisp_Object
2627 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2629 return safe_call (2, fn, arg);
2632 static Lisp_Object
2633 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2635 Lisp_Object retval;
2636 va_list ap;
2638 va_start (ap, fn);
2639 retval = safe__call (inhibit_quit, 2, fn, ap);
2640 va_end (ap);
2641 return retval;
2644 Lisp_Object
2645 safe_eval (Lisp_Object sexpr)
2647 return safe__call1 (false, Qeval, sexpr);
2650 static Lisp_Object
2651 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2653 return safe__call1 (inhibit_quit, Qeval, sexpr);
2656 /* Call function FN with two arguments ARG1 and ARG2.
2657 Return the result, or nil if something went wrong. */
2659 Lisp_Object
2660 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2662 return safe_call (3, fn, arg1, arg2);
2667 /***********************************************************************
2668 Debugging
2669 ***********************************************************************/
2671 /* Define CHECK_IT to perform sanity checks on iterators.
2672 This is for debugging. It is too slow to do unconditionally. */
2674 static void
2675 CHECK_IT (struct it *it)
2677 #if false
2678 if (it->method == GET_FROM_STRING)
2680 eassert (STRINGP (it->string));
2681 eassert (IT_STRING_CHARPOS (*it) >= 0);
2683 else
2685 eassert (IT_STRING_CHARPOS (*it) < 0);
2686 if (it->method == GET_FROM_BUFFER)
2688 /* Check that character and byte positions agree. */
2689 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2693 if (it->dpvec)
2694 eassert (it->current.dpvec_index >= 0);
2695 else
2696 eassert (it->current.dpvec_index < 0);
2697 #endif
2701 /* Check that the window end of window W is what we expect it
2702 to be---the last row in the current matrix displaying text. */
2704 static void
2705 CHECK_WINDOW_END (struct window *w)
2707 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2708 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2710 struct glyph_row *row;
2711 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2712 !row->enabled_p
2713 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2714 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2716 #endif
2719 /***********************************************************************
2720 Iterator initialization
2721 ***********************************************************************/
2723 /* Initialize IT for displaying current_buffer in window W, starting
2724 at character position CHARPOS. CHARPOS < 0 means that no buffer
2725 position is specified which is useful when the iterator is assigned
2726 a position later. BYTEPOS is the byte position corresponding to
2727 CHARPOS.
2729 If ROW is not null, calls to produce_glyphs with IT as parameter
2730 will produce glyphs in that row.
2732 BASE_FACE_ID is the id of a base face to use. It must be one of
2733 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2735 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2737 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2738 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2739 will be initialized to use the corresponding mode line glyph row of
2740 the desired matrix of W. */
2742 void
2743 init_iterator (struct it *it, struct window *w,
2744 ptrdiff_t charpos, ptrdiff_t bytepos,
2745 struct glyph_row *row, enum face_id base_face_id)
2747 enum face_id remapped_base_face_id = base_face_id;
2749 /* Some precondition checks. */
2750 eassert (w != NULL && it != NULL);
2751 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2752 && charpos <= ZV));
2754 /* If face attributes have been changed since the last redisplay,
2755 free realized faces now because they depend on face definitions
2756 that might have changed. Don't free faces while there might be
2757 desired matrices pending which reference these faces. */
2758 if (!inhibit_free_realized_faces)
2760 if (face_change)
2762 face_change = false;
2763 XFRAME (w->frame)->face_change = 0;
2764 free_all_realized_faces (Qnil);
2766 else if (XFRAME (w->frame)->face_change)
2768 XFRAME (w->frame)->face_change = 0;
2769 free_all_realized_faces (w->frame);
2773 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2774 if (! NILP (Vface_remapping_alist))
2775 remapped_base_face_id
2776 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2778 /* Use one of the mode line rows of W's desired matrix if
2779 appropriate. */
2780 if (row == NULL)
2782 if (base_face_id == MODE_LINE_FACE_ID
2783 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2784 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2785 else if (base_face_id == HEADER_LINE_FACE_ID)
2786 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2789 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2790 Other parts of redisplay rely on that. */
2791 memclear (it, sizeof *it);
2792 it->current.overlay_string_index = -1;
2793 it->current.dpvec_index = -1;
2794 it->base_face_id = remapped_base_face_id;
2795 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2796 it->paragraph_embedding = L2R;
2797 it->bidi_it.w = w;
2799 /* The window in which we iterate over current_buffer: */
2800 XSETWINDOW (it->window, w);
2801 it->w = w;
2802 it->f = XFRAME (w->frame);
2804 it->cmp_it.id = -1;
2806 /* Extra space between lines (on window systems only). */
2807 if (base_face_id == DEFAULT_FACE_ID
2808 && FRAME_WINDOW_P (it->f))
2810 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2811 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2812 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2813 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2814 * FRAME_LINE_HEIGHT (it->f));
2815 else if (it->f->extra_line_spacing > 0)
2816 it->extra_line_spacing = it->f->extra_line_spacing;
2819 /* If realized faces have been removed, e.g. because of face
2820 attribute changes of named faces, recompute them. When running
2821 in batch mode, the face cache of the initial frame is null. If
2822 we happen to get called, make a dummy face cache. */
2823 if (FRAME_FACE_CACHE (it->f) == NULL)
2824 init_frame_faces (it->f);
2825 if (FRAME_FACE_CACHE (it->f)->used == 0)
2826 recompute_basic_faces (it->f);
2828 it->override_ascent = -1;
2830 /* Are control characters displayed as `^C'? */
2831 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2833 /* -1 means everything between a CR and the following line end
2834 is invisible. >0 means lines indented more than this value are
2835 invisible. */
2836 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2837 ? (clip_to_bounds
2838 (-1, XINT (BVAR (current_buffer, selective_display)),
2839 PTRDIFF_MAX))
2840 : (!NILP (BVAR (current_buffer, selective_display))
2841 ? -1 : 0));
2842 it->selective_display_ellipsis_p
2843 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2845 /* Display table to use. */
2846 it->dp = window_display_table (w);
2848 /* Are multibyte characters enabled in current_buffer? */
2849 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2851 /* Get the position at which the redisplay_end_trigger hook should
2852 be run, if it is to be run at all. */
2853 if (MARKERP (w->redisplay_end_trigger)
2854 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2855 it->redisplay_end_trigger_charpos
2856 = marker_position (w->redisplay_end_trigger);
2857 else if (INTEGERP (w->redisplay_end_trigger))
2858 it->redisplay_end_trigger_charpos
2859 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2860 PTRDIFF_MAX);
2862 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2864 /* Are lines in the display truncated? */
2865 if (TRUNCATE != 0)
2866 it->line_wrap = TRUNCATE;
2867 if (base_face_id == DEFAULT_FACE_ID
2868 && !it->w->hscroll
2869 && (WINDOW_FULL_WIDTH_P (it->w)
2870 || NILP (Vtruncate_partial_width_windows)
2871 || (INTEGERP (Vtruncate_partial_width_windows)
2872 /* PXW: Shall we do something about this? */
2873 && (XINT (Vtruncate_partial_width_windows)
2874 <= WINDOW_TOTAL_COLS (it->w))))
2875 && NILP (BVAR (current_buffer, truncate_lines)))
2876 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2877 ? WINDOW_WRAP : WORD_WRAP;
2879 /* Get dimensions of truncation and continuation glyphs. These are
2880 displayed as fringe bitmaps under X, but we need them for such
2881 frames when the fringes are turned off. The no_special_glyphs slot
2882 of the iterator's frame, when set, suppresses their display - by
2883 default for tooltip frames and when set via the 'no-special-glyphs'
2884 frame parameter. */
2885 #ifdef HAVE_WINDOW_SYSTEM
2886 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2887 #endif
2889 if (it->line_wrap == TRUNCATE)
2891 /* We will need the truncation glyph. */
2892 eassert (it->glyph_row == NULL);
2893 produce_special_glyphs (it, IT_TRUNCATION);
2894 it->truncation_pixel_width = it->pixel_width;
2896 else
2898 /* We will need the continuation glyph. */
2899 eassert (it->glyph_row == NULL);
2900 produce_special_glyphs (it, IT_CONTINUATION);
2901 it->continuation_pixel_width = it->pixel_width;
2905 /* Reset these values to zero because the produce_special_glyphs
2906 above has changed them. */
2907 it->pixel_width = it->ascent = it->descent = 0;
2908 it->phys_ascent = it->phys_descent = 0;
2910 /* Set this after getting the dimensions of truncation and
2911 continuation glyphs, so that we don't produce glyphs when calling
2912 produce_special_glyphs, above. */
2913 it->glyph_row = row;
2914 it->area = TEXT_AREA;
2916 /* Get the dimensions of the display area. The display area
2917 consists of the visible window area plus a horizontally scrolled
2918 part to the left of the window. All x-values are relative to the
2919 start of this total display area. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 /* Mode lines, menu bar in terminal frames. */
2923 it->first_visible_x = 0;
2924 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2926 else
2928 /* When hscrolling only the current line, don't apply the
2929 hscroll here, it will be applied by display_line when it gets
2930 to laying out the line showing point. However, if the
2931 window's min_hscroll is positive, the user specified a lower
2932 bound for automatic hscrolling, so they expect the
2933 non-current lines to obey that hscroll amount. */
2934 if (hscrolling_current_line_p (w))
2936 if (w->min_hscroll > 0)
2937 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2938 else
2939 it->first_visible_x = 0;
2941 else
2942 it->first_visible_x =
2943 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2944 it->last_visible_x = (it->first_visible_x
2945 + window_box_width (w, TEXT_AREA));
2947 /* If we truncate lines, leave room for the truncation glyph(s) at
2948 the right margin. Otherwise, leave room for the continuation
2949 glyph(s). Done only if the window has no right fringe. */
2950 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2952 if (it->line_wrap == TRUNCATE)
2953 it->last_visible_x -= it->truncation_pixel_width;
2954 else
2955 it->last_visible_x -= it->continuation_pixel_width;
2958 it->header_line_p = window_wants_header_line (w);
2959 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2962 /* Leave room for a border glyph. */
2963 if (!FRAME_WINDOW_P (it->f)
2964 && !WINDOW_RIGHTMOST_P (it->w))
2965 it->last_visible_x -= 1;
2967 it->last_visible_y = window_text_bottom_y (w);
2969 /* For mode lines and alike, arrange for the first glyph having a
2970 left box line if the face specifies a box. */
2971 if (base_face_id != DEFAULT_FACE_ID)
2973 struct face *face;
2975 it->face_id = remapped_base_face_id;
2977 /* If we have a boxed mode line, make the first character appear
2978 with a left box line. */
2979 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2980 if (face && face->box != FACE_NO_BOX)
2981 it->start_of_box_run_p = true;
2984 /* If a buffer position was specified, set the iterator there,
2985 getting overlays and face properties from that position. */
2986 if (charpos >= BUF_BEG (current_buffer))
2988 it->stop_charpos = charpos;
2989 it->end_charpos = ZV;
2990 eassert (charpos == BYTE_TO_CHAR (bytepos));
2991 IT_CHARPOS (*it) = charpos;
2992 IT_BYTEPOS (*it) = bytepos;
2994 /* We will rely on `reseat' to set this up properly, via
2995 handle_face_prop. */
2996 it->face_id = it->base_face_id;
2998 it->start = it->current;
2999 /* Do we need to reorder bidirectional text? Not if this is a
3000 unibyte buffer: by definition, none of the single-byte
3001 characters are strong R2L, so no reordering is needed. And
3002 bidi.c doesn't support unibyte buffers anyway. Also, don't
3003 reorder while we are loading loadup.el, since the tables of
3004 character properties needed for reordering are not yet
3005 available. */
3006 it->bidi_p =
3007 !redisplay__inhibit_bidi
3008 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3009 && it->multibyte_p;
3011 /* If we are to reorder bidirectional text, init the bidi
3012 iterator. */
3013 if (it->bidi_p)
3015 /* Since we don't know at this point whether there will be
3016 any R2L lines in the window, we reserve space for
3017 truncation/continuation glyphs even if only the left
3018 fringe is absent. */
3019 if (base_face_id == DEFAULT_FACE_ID
3020 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3021 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3023 if (it->line_wrap == TRUNCATE)
3024 it->last_visible_x -= it->truncation_pixel_width;
3025 else
3026 it->last_visible_x -= it->continuation_pixel_width;
3028 /* Note the paragraph direction that this buffer wants to
3029 use. */
3030 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3031 Qleft_to_right))
3032 it->paragraph_embedding = L2R;
3033 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3034 Qright_to_left))
3035 it->paragraph_embedding = R2L;
3036 else
3037 it->paragraph_embedding = NEUTRAL_DIR;
3038 bidi_unshelve_cache (NULL, false);
3039 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3040 &it->bidi_it);
3043 /* Compute faces etc. */
3044 reseat (it, it->current.pos, true);
3047 CHECK_IT (it);
3051 /* Initialize IT for the display of window W with window start POS. */
3053 void
3054 start_display (struct it *it, struct window *w, struct text_pos pos)
3056 struct glyph_row *row;
3057 bool first_vpos = window_wants_header_line (w);
3059 row = w->desired_matrix->rows + first_vpos;
3060 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3061 it->first_vpos = first_vpos;
3063 /* Don't reseat to previous visible line start if current start
3064 position is in a string or image. */
3065 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3067 int first_y = it->current_y;
3069 /* If window start is not at a line start, skip forward to POS to
3070 get the correct continuation lines width. */
3071 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3072 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3073 if (!start_at_line_beg_p)
3075 int new_x;
3077 reseat_at_previous_visible_line_start (it);
3078 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3080 new_x = it->current_x + it->pixel_width;
3082 /* If lines are continued, this line may end in the middle
3083 of a multi-glyph character (e.g. a control character
3084 displayed as \003, or in the middle of an overlay
3085 string). In this case move_it_to above will not have
3086 taken us to the start of the continuation line but to the
3087 end of the continued line. */
3088 if (it->current_x > 0
3089 && it->line_wrap != TRUNCATE /* Lines are continued. */
3090 && (/* And glyph doesn't fit on the line. */
3091 new_x > it->last_visible_x
3092 /* Or it fits exactly and we're on a window
3093 system frame. */
3094 || (new_x == it->last_visible_x
3095 && FRAME_WINDOW_P (it->f)
3096 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3097 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3098 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3100 if ((it->current.dpvec_index >= 0
3101 || it->current.overlay_string_index >= 0)
3102 /* If we are on a newline from a display vector or
3103 overlay string, then we are already at the end of
3104 a screen line; no need to go to the next line in
3105 that case, as this line is not really continued.
3106 (If we do go to the next line, C-e will not DTRT.) */
3107 && it->c != '\n')
3109 set_iterator_to_next (it, true);
3110 move_it_in_display_line_to (it, -1, -1, 0);
3113 it->continuation_lines_width += it->current_x;
3115 /* If the character at POS is displayed via a display
3116 vector, move_it_to above stops at the final glyph of
3117 IT->dpvec. To make the caller redisplay that character
3118 again (a.k.a. start at POS), we need to reset the
3119 dpvec_index to the beginning of IT->dpvec. */
3120 else if (it->current.dpvec_index >= 0)
3121 it->current.dpvec_index = 0;
3123 /* We're starting a new display line, not affected by the
3124 height of the continued line, so clear the appropriate
3125 fields in the iterator structure. */
3126 it->max_ascent = it->max_descent = 0;
3127 it->max_phys_ascent = it->max_phys_descent = 0;
3129 it->current_y = first_y;
3130 it->vpos = 0;
3131 it->current_x = it->hpos = 0;
3137 /* Return true if POS is a position in ellipses displayed for invisible
3138 text. W is the window we display, for text property lookup. */
3140 static bool
3141 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3143 Lisp_Object prop, window;
3144 bool ellipses_p = false;
3145 ptrdiff_t charpos = CHARPOS (pos->pos);
3147 /* If POS specifies a position in a display vector, this might
3148 be for an ellipsis displayed for invisible text. We won't
3149 get the iterator set up for delivering that ellipsis unless
3150 we make sure that it gets aware of the invisible text. */
3151 if (pos->dpvec_index >= 0
3152 && pos->overlay_string_index < 0
3153 && CHARPOS (pos->string_pos) < 0
3154 && charpos > BEGV
3155 && (XSETWINDOW (window, w),
3156 prop = Fget_char_property (make_number (charpos),
3157 Qinvisible, window),
3158 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3160 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3161 window);
3162 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3165 return ellipses_p;
3169 /* Initialize IT for stepping through current_buffer in window W,
3170 starting at position POS that includes overlay string and display
3171 vector/ control character translation position information. Value
3172 is false if there are overlay strings with newlines at POS. */
3174 static bool
3175 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3177 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3178 int i;
3179 bool overlay_strings_with_newlines = false;
3181 /* If POS specifies a position in a display vector, this might
3182 be for an ellipsis displayed for invisible text. We won't
3183 get the iterator set up for delivering that ellipsis unless
3184 we make sure that it gets aware of the invisible text. */
3185 if (in_ellipses_for_invisible_text_p (pos, w))
3187 --charpos;
3188 bytepos = 0;
3191 /* Keep in mind: the call to reseat in init_iterator skips invisible
3192 text, so we might end up at a position different from POS. This
3193 is only a problem when POS is a row start after a newline and an
3194 overlay starts there with an after-string, and the overlay has an
3195 invisible property. Since we don't skip invisible text in
3196 display_line and elsewhere immediately after consuming the
3197 newline before the row start, such a POS will not be in a string,
3198 but the call to init_iterator below will move us to the
3199 after-string. */
3200 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3202 /* This only scans the current chunk -- it should scan all chunks.
3203 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3204 to 16 in 22.1 to make this a lesser problem. */
3205 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3207 const char *s = SSDATA (it->overlay_strings[i]);
3208 const char *e = s + SBYTES (it->overlay_strings[i]);
3210 while (s < e && *s != '\n')
3211 ++s;
3213 if (s < e)
3215 overlay_strings_with_newlines = true;
3216 break;
3220 /* If position is within an overlay string, set up IT to the right
3221 overlay string. */
3222 if (pos->overlay_string_index >= 0)
3224 int relative_index;
3226 /* If the first overlay string happens to have a `display'
3227 property for an image, the iterator will be set up for that
3228 image, and we have to undo that setup first before we can
3229 correct the overlay string index. */
3230 if (it->method == GET_FROM_IMAGE)
3231 pop_it (it);
3233 /* We already have the first chunk of overlay strings in
3234 IT->overlay_strings. Load more until the one for
3235 pos->overlay_string_index is in IT->overlay_strings. */
3236 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3238 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3239 it->current.overlay_string_index = 0;
3240 while (n--)
3242 load_overlay_strings (it, 0);
3243 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3247 it->current.overlay_string_index = pos->overlay_string_index;
3248 relative_index = (it->current.overlay_string_index
3249 % OVERLAY_STRING_CHUNK_SIZE);
3250 it->string = it->overlay_strings[relative_index];
3251 eassert (STRINGP (it->string));
3252 it->current.string_pos = pos->string_pos;
3253 it->method = GET_FROM_STRING;
3254 it->end_charpos = SCHARS (it->string);
3255 /* Set up the bidi iterator for this overlay string. */
3256 if (it->bidi_p)
3258 it->bidi_it.string.lstring = it->string;
3259 it->bidi_it.string.s = NULL;
3260 it->bidi_it.string.schars = SCHARS (it->string);
3261 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3262 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3263 it->bidi_it.string.unibyte = !it->multibyte_p;
3264 it->bidi_it.w = it->w;
3265 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3266 FRAME_WINDOW_P (it->f), &it->bidi_it);
3268 /* Synchronize the state of the bidi iterator with
3269 pos->string_pos. For any string position other than
3270 zero, this will be done automagically when we resume
3271 iteration over the string and get_visually_first_element
3272 is called. But if string_pos is zero, and the string is
3273 to be reordered for display, we need to resync manually,
3274 since it could be that the iteration state recorded in
3275 pos ended at string_pos of 0 moving backwards in string. */
3276 if (CHARPOS (pos->string_pos) == 0)
3278 get_visually_first_element (it);
3279 if (IT_STRING_CHARPOS (*it) != 0)
3280 do {
3281 /* Paranoia. */
3282 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3283 bidi_move_to_visually_next (&it->bidi_it);
3284 } while (it->bidi_it.charpos != 0);
3286 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3287 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3291 if (CHARPOS (pos->string_pos) >= 0)
3293 /* Recorded position is not in an overlay string, but in another
3294 string. This can only be a string from a `display' property.
3295 IT should already be filled with that string. */
3296 it->current.string_pos = pos->string_pos;
3297 eassert (STRINGP (it->string));
3298 if (it->bidi_p)
3299 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3300 FRAME_WINDOW_P (it->f), &it->bidi_it);
3303 /* Restore position in display vector translations, control
3304 character translations or ellipses. */
3305 if (pos->dpvec_index >= 0)
3307 if (it->dpvec == NULL)
3308 get_next_display_element (it);
3309 eassert (it->dpvec && it->current.dpvec_index == 0);
3310 it->current.dpvec_index = pos->dpvec_index;
3313 CHECK_IT (it);
3314 return !overlay_strings_with_newlines;
3318 /* Initialize IT for stepping through current_buffer in window W
3319 starting at ROW->start. */
3321 static void
3322 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3324 init_from_display_pos (it, w, &row->start);
3325 it->start = row->start;
3326 it->continuation_lines_width = row->continuation_lines_width;
3327 CHECK_IT (it);
3331 /* Initialize IT for stepping through current_buffer in window W
3332 starting in the line following ROW, i.e. starting at ROW->end.
3333 Value is false if there are overlay strings with newlines at ROW's
3334 end position. */
3336 static bool
3337 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3339 bool success = false;
3341 if (init_from_display_pos (it, w, &row->end))
3343 if (row->continued_p)
3344 it->continuation_lines_width
3345 = row->continuation_lines_width + row->pixel_width;
3346 CHECK_IT (it);
3347 success = true;
3350 return success;
3356 /***********************************************************************
3357 Text properties
3358 ***********************************************************************/
3360 /* Called when IT reaches IT->stop_charpos. Handle text property and
3361 overlay changes. Set IT->stop_charpos to the next position where
3362 to stop. */
3364 static void
3365 handle_stop (struct it *it)
3367 enum prop_handled handled;
3368 bool handle_overlay_change_p;
3369 struct props *p;
3371 it->dpvec = NULL;
3372 it->current.dpvec_index = -1;
3373 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3374 it->ellipsis_p = false;
3376 /* Use face of preceding text for ellipsis (if invisible) */
3377 if (it->selective_display_ellipsis_p)
3378 it->saved_face_id = it->face_id;
3380 /* Here's the description of the semantics of, and the logic behind,
3381 the various HANDLED_* statuses:
3383 HANDLED_NORMALLY means the handler did its job, and the loop
3384 should proceed to calling the next handler in order.
3386 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3387 change in the properties and overlays at current position, so the
3388 loop should be restarted, to re-invoke the handlers that were
3389 already called. This happens when fontification-functions were
3390 called by handle_fontified_prop, and actually fontified
3391 something. Another case where HANDLED_RECOMPUTE_PROPS is
3392 returned is when we discover overlay strings that need to be
3393 displayed right away. The loop below will continue for as long
3394 as the status is HANDLED_RECOMPUTE_PROPS.
3396 HANDLED_RETURN means return immediately to the caller, to
3397 continue iteration without calling any further handlers. This is
3398 used when we need to act on some property right away, for example
3399 when we need to display the ellipsis or a replacing display
3400 property, such as display string or image.
3402 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3403 consumed, and the handler switched to the next overlay string.
3404 This signals the loop below to refrain from looking for more
3405 overlays before all the overlay strings of the current overlay
3406 are processed.
3408 Some of the handlers called by the loop push the iterator state
3409 onto the stack (see 'push_it'), and arrange for the iteration to
3410 continue with another object, such as an image, a display string,
3411 or an overlay string. In most such cases, it->stop_charpos is
3412 set to the first character of the string, so that when the
3413 iteration resumes, this function will immediately be called
3414 again, to examine the properties at the beginning of the string.
3416 When a display or overlay string is exhausted, the iterator state
3417 is popped (see 'pop_it'), and iteration continues with the
3418 previous object. Again, in many such cases this function is
3419 called again to find the next position where properties might
3420 change. */
3424 handled = HANDLED_NORMALLY;
3426 /* Call text property handlers. */
3427 for (p = it_props; p->handler; ++p)
3429 handled = p->handler (it);
3431 if (handled == HANDLED_RECOMPUTE_PROPS)
3432 break;
3433 else if (handled == HANDLED_RETURN)
3435 /* We still want to show before and after strings from
3436 overlays even if the actual buffer text is replaced. */
3437 if (!handle_overlay_change_p
3438 || it->sp > 1
3439 /* Don't call get_overlay_strings_1 if we already
3440 have overlay strings loaded, because doing so
3441 will load them again and push the iterator state
3442 onto the stack one more time, which is not
3443 expected by the rest of the code that processes
3444 overlay strings. */
3445 || (it->current.overlay_string_index < 0
3446 && !get_overlay_strings_1 (it, 0, false)))
3448 if (it->ellipsis_p)
3449 setup_for_ellipsis (it, 0);
3450 /* When handling a display spec, we might load an
3451 empty string. In that case, discard it here. We
3452 used to discard it in handle_single_display_spec,
3453 but that causes get_overlay_strings_1, above, to
3454 ignore overlay strings that we must check. */
3455 if (STRINGP (it->string) && !SCHARS (it->string))
3456 pop_it (it);
3457 return;
3459 else if (STRINGP (it->string) && !SCHARS (it->string))
3460 pop_it (it);
3461 else
3463 it->string_from_display_prop_p = false;
3464 it->from_disp_prop_p = false;
3465 handle_overlay_change_p = false;
3467 handled = HANDLED_RECOMPUTE_PROPS;
3468 break;
3470 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3471 handle_overlay_change_p = false;
3474 if (handled != HANDLED_RECOMPUTE_PROPS)
3476 /* Don't check for overlay strings below when set to deliver
3477 characters from a display vector. */
3478 if (it->method == GET_FROM_DISPLAY_VECTOR)
3479 handle_overlay_change_p = false;
3481 /* Handle overlay changes.
3482 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3483 if it finds overlays. */
3484 if (handle_overlay_change_p)
3485 handled = handle_overlay_change (it);
3488 if (it->ellipsis_p)
3490 setup_for_ellipsis (it, 0);
3491 break;
3494 while (handled == HANDLED_RECOMPUTE_PROPS);
3496 /* Determine where to stop next. */
3497 if (handled == HANDLED_NORMALLY)
3498 compute_stop_pos (it);
3502 /* Compute IT->stop_charpos from text property and overlay change
3503 information for IT's current position. */
3505 static void
3506 compute_stop_pos (struct it *it)
3508 register INTERVAL iv, next_iv;
3509 Lisp_Object object, limit, position;
3510 ptrdiff_t charpos, bytepos;
3512 if (STRINGP (it->string))
3514 /* Strings are usually short, so don't limit the search for
3515 properties. */
3516 it->stop_charpos = it->end_charpos;
3517 object = it->string;
3518 limit = Qnil;
3519 charpos = IT_STRING_CHARPOS (*it);
3520 bytepos = IT_STRING_BYTEPOS (*it);
3522 else
3524 ptrdiff_t pos;
3526 /* If end_charpos is out of range for some reason, such as a
3527 misbehaving display function, rationalize it (Bug#5984). */
3528 if (it->end_charpos > ZV)
3529 it->end_charpos = ZV;
3530 it->stop_charpos = it->end_charpos;
3532 /* If next overlay change is in front of the current stop pos
3533 (which is IT->end_charpos), stop there. Note: value of
3534 next_overlay_change is point-max if no overlay change
3535 follows. */
3536 charpos = IT_CHARPOS (*it);
3537 bytepos = IT_BYTEPOS (*it);
3538 pos = next_overlay_change (charpos);
3539 if (pos < it->stop_charpos)
3540 it->stop_charpos = pos;
3542 /* Set up variables for computing the stop position from text
3543 property changes. */
3544 XSETBUFFER (object, current_buffer);
3545 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3548 /* Get the interval containing IT's position. Value is a null
3549 interval if there isn't such an interval. */
3550 position = make_number (charpos);
3551 iv = validate_interval_range (object, &position, &position, false);
3552 if (iv)
3554 Lisp_Object values_here[LAST_PROP_IDX];
3555 struct props *p;
3557 /* Get properties here. */
3558 for (p = it_props; p->handler; ++p)
3559 values_here[p->idx] = textget (iv->plist,
3560 builtin_lisp_symbol (p->name));
3562 /* Look for an interval following iv that has different
3563 properties. */
3564 for (next_iv = next_interval (iv);
3565 (next_iv
3566 && (NILP (limit)
3567 || XFASTINT (limit) > next_iv->position));
3568 next_iv = next_interval (next_iv))
3570 for (p = it_props; p->handler; ++p)
3572 Lisp_Object new_value = textget (next_iv->plist,
3573 builtin_lisp_symbol (p->name));
3574 if (!EQ (values_here[p->idx], new_value))
3575 break;
3578 if (p->handler)
3579 break;
3582 if (next_iv)
3584 if (INTEGERP (limit)
3585 && next_iv->position >= XFASTINT (limit))
3586 /* No text property change up to limit. */
3587 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3588 else
3589 /* Text properties change in next_iv. */
3590 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3594 if (it->cmp_it.id < 0)
3596 ptrdiff_t stoppos = it->end_charpos;
3598 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3599 stoppos = -1;
3600 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3601 stoppos, it->string);
3604 eassert (STRINGP (it->string)
3605 || (it->stop_charpos >= BEGV
3606 && it->stop_charpos >= IT_CHARPOS (*it)));
3610 /* Return the position of the next overlay change after POS in
3611 current_buffer. Value is point-max if no overlay change
3612 follows. This is like `next-overlay-change' but doesn't use
3613 xmalloc. */
3615 static ptrdiff_t
3616 next_overlay_change (ptrdiff_t pos)
3618 ptrdiff_t i, noverlays;
3619 ptrdiff_t endpos;
3620 Lisp_Object *overlays;
3621 USE_SAFE_ALLOCA;
3623 /* Get all overlays at the given position. */
3624 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3626 /* If any of these overlays ends before endpos,
3627 use its ending point instead. */
3628 for (i = 0; i < noverlays; ++i)
3630 Lisp_Object oend;
3631 ptrdiff_t oendpos;
3633 oend = OVERLAY_END (overlays[i]);
3634 oendpos = OVERLAY_POSITION (oend);
3635 endpos = min (endpos, oendpos);
3638 SAFE_FREE ();
3639 return endpos;
3642 /* How many characters forward to search for a display property or
3643 display string. Searching too far forward makes the bidi display
3644 sluggish, especially in small windows. */
3645 #define MAX_DISP_SCAN 250
3647 /* Return the character position of a display string at or after
3648 position specified by POSITION. If no display string exists at or
3649 after POSITION, return ZV. A display string is either an overlay
3650 with `display' property whose value is a string, or a `display'
3651 text property whose value is a string. STRING is data about the
3652 string to iterate; if STRING->lstring is nil, we are iterating a
3653 buffer. FRAME_WINDOW_P is true when we are displaying a window
3654 on a GUI frame. DISP_PROP is set to zero if we searched
3655 MAX_DISP_SCAN characters forward without finding any display
3656 strings, non-zero otherwise. It is set to 2 if the display string
3657 uses any kind of `(space ...)' spec that will produce a stretch of
3658 white space in the text area. */
3659 ptrdiff_t
3660 compute_display_string_pos (struct text_pos *position,
3661 struct bidi_string_data *string,
3662 struct window *w,
3663 bool frame_window_p, int *disp_prop)
3665 /* OBJECT = nil means current buffer. */
3666 Lisp_Object object, object1;
3667 Lisp_Object pos, spec, limpos;
3668 bool string_p = string && (STRINGP (string->lstring) || string->s);
3669 ptrdiff_t eob = string_p ? string->schars : ZV;
3670 ptrdiff_t begb = string_p ? 0 : BEGV;
3671 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3672 ptrdiff_t lim =
3673 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3674 struct text_pos tpos;
3675 int rv = 0;
3677 if (string && STRINGP (string->lstring))
3678 object1 = object = string->lstring;
3679 else if (w && !string_p)
3681 XSETWINDOW (object, w);
3682 object1 = Qnil;
3684 else
3685 object1 = object = Qnil;
3687 *disp_prop = 1;
3689 if (charpos >= eob
3690 /* We don't support display properties whose values are strings
3691 that have display string properties. */
3692 || string->from_disp_str
3693 /* C strings cannot have display properties. */
3694 || (string->s && !STRINGP (object)))
3696 *disp_prop = 0;
3697 return eob;
3700 /* If the character at CHARPOS is where the display string begins,
3701 return CHARPOS. */
3702 pos = make_number (charpos);
3703 if (STRINGP (object))
3704 bufpos = string->bufpos;
3705 else
3706 bufpos = charpos;
3707 tpos = *position;
3708 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3709 && (charpos <= begb
3710 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3711 object),
3712 spec))
3713 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3714 frame_window_p)))
3716 if (rv == 2)
3717 *disp_prop = 2;
3718 return charpos;
3721 /* Look forward for the first character with a `display' property
3722 that will replace the underlying text when displayed. */
3723 limpos = make_number (lim);
3724 do {
3725 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3726 CHARPOS (tpos) = XFASTINT (pos);
3727 if (CHARPOS (tpos) >= lim)
3729 *disp_prop = 0;
3730 break;
3732 if (STRINGP (object))
3733 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3734 else
3735 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3736 spec = Fget_char_property (pos, Qdisplay, object);
3737 if (!STRINGP (object))
3738 bufpos = CHARPOS (tpos);
3739 } while (NILP (spec)
3740 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3741 bufpos, frame_window_p)));
3742 if (rv == 2)
3743 *disp_prop = 2;
3745 return CHARPOS (tpos);
3748 /* Return the character position of the end of the display string that
3749 started at CHARPOS. If there's no display string at CHARPOS,
3750 return -1. A display string is either an overlay with `display'
3751 property whose value is a string or a `display' text property whose
3752 value is a string. */
3753 ptrdiff_t
3754 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3756 /* OBJECT = nil means current buffer. */
3757 Lisp_Object object =
3758 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3759 Lisp_Object pos = make_number (charpos);
3760 ptrdiff_t eob =
3761 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3763 if (charpos >= eob || (string->s && !STRINGP (object)))
3764 return eob;
3766 /* It could happen that the display property or overlay was removed
3767 since we found it in compute_display_string_pos above. One way
3768 this can happen is if JIT font-lock was called (through
3769 handle_fontified_prop), and jit-lock-functions remove text
3770 properties or overlays from the portion of buffer that includes
3771 CHARPOS. Muse mode is known to do that, for example. In this
3772 case, we return -1 to the caller, to signal that no display
3773 string is actually present at CHARPOS. See bidi_fetch_char for
3774 how this is handled.
3776 An alternative would be to never look for display properties past
3777 it->stop_charpos. But neither compute_display_string_pos nor
3778 bidi_fetch_char that calls it know or care where the next
3779 stop_charpos is. */
3780 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3781 return -1;
3783 /* Look forward for the first character where the `display' property
3784 changes. */
3785 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3787 return XFASTINT (pos);
3792 /***********************************************************************
3793 Fontification
3794 ***********************************************************************/
3796 /* Handle changes in the `fontified' property of the current buffer by
3797 calling hook functions from Qfontification_functions to fontify
3798 regions of text. */
3800 static enum prop_handled
3801 handle_fontified_prop (struct it *it)
3803 Lisp_Object prop, pos;
3804 enum prop_handled handled = HANDLED_NORMALLY;
3806 if (!NILP (Vmemory_full))
3807 return handled;
3809 /* Get the value of the `fontified' property at IT's current buffer
3810 position. (The `fontified' property doesn't have a special
3811 meaning in strings.) If the value is nil, call functions from
3812 Qfontification_functions. */
3813 if (!STRINGP (it->string)
3814 && it->s == NULL
3815 && !NILP (Vfontification_functions)
3816 && !NILP (Vrun_hooks)
3817 && (pos = make_number (IT_CHARPOS (*it)),
3818 prop = Fget_char_property (pos, Qfontified, Qnil),
3819 /* Ignore the special cased nil value always present at EOB since
3820 no amount of fontifying will be able to change it. */
3821 NILP (prop) && IT_CHARPOS (*it) < Z))
3823 ptrdiff_t count = SPECPDL_INDEX ();
3824 Lisp_Object val;
3825 struct buffer *obuf = current_buffer;
3826 ptrdiff_t begv = BEGV, zv = ZV;
3827 bool old_clip_changed = current_buffer->clip_changed;
3829 val = Vfontification_functions;
3830 specbind (Qfontification_functions, Qnil);
3832 eassert (it->end_charpos == ZV);
3834 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3835 safe_call1 (val, pos);
3836 else
3838 Lisp_Object fns, fn;
3840 fns = Qnil;
3842 for (; CONSP (val); val = XCDR (val))
3844 fn = XCAR (val);
3846 if (EQ (fn, Qt))
3848 /* A value of t indicates this hook has a local
3849 binding; it means to run the global binding too.
3850 In a global value, t should not occur. If it
3851 does, we must ignore it to avoid an endless
3852 loop. */
3853 for (fns = Fdefault_value (Qfontification_functions);
3854 CONSP (fns);
3855 fns = XCDR (fns))
3857 fn = XCAR (fns);
3858 if (!EQ (fn, Qt))
3859 safe_call1 (fn, pos);
3862 else
3863 safe_call1 (fn, pos);
3867 unbind_to (count, Qnil);
3869 /* Fontification functions routinely call `save-restriction'.
3870 Normally, this tags clip_changed, which can confuse redisplay
3871 (see discussion in Bug#6671). Since we don't perform any
3872 special handling of fontification changes in the case where
3873 `save-restriction' isn't called, there's no point doing so in
3874 this case either. So, if the buffer's restrictions are
3875 actually left unchanged, reset clip_changed. */
3876 if (obuf == current_buffer)
3878 if (begv == BEGV && zv == ZV)
3879 current_buffer->clip_changed = old_clip_changed;
3881 /* There isn't much we can reasonably do to protect against
3882 misbehaving fontification, but here's a fig leaf. */
3883 else if (BUFFER_LIVE_P (obuf))
3884 set_buffer_internal_1 (obuf);
3886 /* The fontification code may have added/removed text.
3887 It could do even a lot worse, but let's at least protect against
3888 the most obvious case where only the text past `pos' gets changed',
3889 as is/was done in grep.el where some escapes sequences are turned
3890 into face properties (bug#7876). */
3891 it->end_charpos = ZV;
3893 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3894 something. This avoids an endless loop if they failed to
3895 fontify the text for which reason ever. */
3896 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3897 handled = HANDLED_RECOMPUTE_PROPS;
3900 return handled;
3905 /***********************************************************************
3906 Faces
3907 ***********************************************************************/
3909 /* Set up iterator IT from face properties at its current position.
3910 Called from handle_stop. */
3912 static enum prop_handled
3913 handle_face_prop (struct it *it)
3915 int new_face_id;
3916 ptrdiff_t next_stop;
3918 if (!STRINGP (it->string))
3920 new_face_id
3921 = face_at_buffer_position (it->w,
3922 IT_CHARPOS (*it),
3923 &next_stop,
3924 (IT_CHARPOS (*it)
3925 + TEXT_PROP_DISTANCE_LIMIT),
3926 false, it->base_face_id);
3928 /* Is this a start of a run of characters with box face?
3929 Caveat: this can be called for a freshly initialized
3930 iterator; face_id is -1 in this case. We know that the new
3931 face will not change until limit, i.e. if the new face has a
3932 box, all characters up to limit will have one. But, as
3933 usual, we don't know whether limit is really the end. */
3934 if (new_face_id != it->face_id)
3936 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3937 /* If it->face_id is -1, old_face below will be NULL, see
3938 the definition of FACE_FROM_ID_OR_NULL. This will happen
3939 if this is the initial call that gets the face. */
3940 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3942 /* If the value of face_id of the iterator is -1, we have to
3943 look in front of IT's position and see whether there is a
3944 face there that's different from new_face_id. */
3945 if (!old_face && IT_CHARPOS (*it) > BEG)
3947 int prev_face_id = face_before_it_pos (it);
3949 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3952 /* If the new face has a box, but the old face does not,
3953 this is the start of a run of characters with box face,
3954 i.e. this character has a shadow on the left side. */
3955 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3956 && (old_face == NULL || !old_face->box));
3957 it->face_box_p = new_face->box != FACE_NO_BOX;
3960 else
3962 int base_face_id;
3963 ptrdiff_t bufpos;
3964 int i;
3965 Lisp_Object from_overlay
3966 = (it->current.overlay_string_index >= 0
3967 ? it->string_overlays[it->current.overlay_string_index
3968 % OVERLAY_STRING_CHUNK_SIZE]
3969 : Qnil);
3971 /* See if we got to this string directly or indirectly from
3972 an overlay property. That includes the before-string or
3973 after-string of an overlay, strings in display properties
3974 provided by an overlay, their text properties, etc.
3976 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3977 if (! NILP (from_overlay))
3978 for (i = it->sp - 1; i >= 0; i--)
3980 if (it->stack[i].current.overlay_string_index >= 0)
3981 from_overlay
3982 = it->string_overlays[it->stack[i].current.overlay_string_index
3983 % OVERLAY_STRING_CHUNK_SIZE];
3984 else if (! NILP (it->stack[i].from_overlay))
3985 from_overlay = it->stack[i].from_overlay;
3987 if (!NILP (from_overlay))
3988 break;
3991 if (! NILP (from_overlay))
3993 bufpos = IT_CHARPOS (*it);
3994 /* For a string from an overlay, the base face depends
3995 only on text properties and ignores overlays. */
3996 base_face_id
3997 = face_for_overlay_string (it->w,
3998 IT_CHARPOS (*it),
3999 &next_stop,
4000 (IT_CHARPOS (*it)
4001 + TEXT_PROP_DISTANCE_LIMIT),
4002 false,
4003 from_overlay);
4005 else
4007 bufpos = 0;
4009 /* For strings from a `display' property, use the face at
4010 IT's current buffer position as the base face to merge
4011 with, so that overlay strings appear in the same face as
4012 surrounding text, unless they specify their own faces.
4013 For strings from wrap-prefix and line-prefix properties,
4014 use the default face, possibly remapped via
4015 Vface_remapping_alist. */
4016 /* Note that the fact that we use the face at _buffer_
4017 position means that a 'display' property on an overlay
4018 string will not inherit the face of that overlay string,
4019 but will instead revert to the face of buffer text
4020 covered by the overlay. This is visible, e.g., when the
4021 overlay specifies a box face, but neither the buffer nor
4022 the display string do. This sounds like a design bug,
4023 but Emacs always did that since v21.1, so changing that
4024 might be a big deal. */
4025 base_face_id = it->string_from_prefix_prop_p
4026 ? (!NILP (Vface_remapping_alist)
4027 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4028 : DEFAULT_FACE_ID)
4029 : underlying_face_id (it);
4032 new_face_id = face_at_string_position (it->w,
4033 it->string,
4034 IT_STRING_CHARPOS (*it),
4035 bufpos,
4036 &next_stop,
4037 base_face_id, false);
4039 /* Is this a start of a run of characters with box? Caveat:
4040 this can be called for a freshly allocated iterator; face_id
4041 is -1 is this case. We know that the new face will not
4042 change until the next check pos, i.e. if the new face has a
4043 box, all characters up to that position will have a
4044 box. But, as usual, we don't know whether that position
4045 is really the end. */
4046 if (new_face_id != it->face_id)
4048 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4049 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4051 /* If new face has a box but old face hasn't, this is the
4052 start of a run of characters with box, i.e. it has a
4053 shadow on the left side. */
4054 it->start_of_box_run_p
4055 = new_face->box && (old_face == NULL || !old_face->box);
4056 it->face_box_p = new_face->box != FACE_NO_BOX;
4060 it->face_id = new_face_id;
4061 return HANDLED_NORMALLY;
4065 /* Return the ID of the face ``underlying'' IT's current position,
4066 which is in a string. If the iterator is associated with a
4067 buffer, return the face at IT's current buffer position.
4068 Otherwise, use the iterator's base_face_id. */
4070 static int
4071 underlying_face_id (struct it *it)
4073 int face_id = it->base_face_id, i;
4075 eassert (STRINGP (it->string));
4077 for (i = it->sp - 1; i >= 0; --i)
4078 if (NILP (it->stack[i].string))
4079 face_id = it->stack[i].face_id;
4081 return face_id;
4085 /* Compute the face one character before or after the current position
4086 of IT, in the visual order. BEFORE_P means get the face
4087 in front (to the left in L2R paragraphs, to the right in R2L
4088 paragraphs) of IT's screen position. Value is the ID of the face. */
4090 static int
4091 face_before_or_after_it_pos (struct it *it, bool before_p)
4093 int face_id, limit;
4094 ptrdiff_t next_check_charpos;
4095 struct it it_copy;
4096 void *it_copy_data = NULL;
4098 eassert (it->s == NULL);
4100 if (STRINGP (it->string))
4102 ptrdiff_t bufpos, charpos;
4103 int base_face_id;
4105 /* No face change past the end of the string (for the case
4106 we are padding with spaces). No face change before the
4107 string start. */
4108 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4109 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4110 return it->face_id;
4112 if (!it->bidi_p)
4114 /* Set charpos to the position before or after IT's current
4115 position, in the logical order, which in the non-bidi
4116 case is the same as the visual order. */
4117 if (before_p)
4118 charpos = IT_STRING_CHARPOS (*it) - 1;
4119 else if (it->what == IT_COMPOSITION)
4120 /* For composition, we must check the character after the
4121 composition. */
4122 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4123 else
4124 charpos = IT_STRING_CHARPOS (*it) + 1;
4126 else
4128 if (before_p)
4130 /* With bidi iteration, the character before the current
4131 in the visual order cannot be found by simple
4132 iteration, because "reverse" reordering is not
4133 supported. Instead, we need to start from the string
4134 beginning and go all the way to the current string
4135 position, remembering the previous position. */
4136 /* Ignore face changes before the first visible
4137 character on this display line. */
4138 if (it->current_x <= it->first_visible_x)
4139 return it->face_id;
4140 SAVE_IT (it_copy, *it, it_copy_data);
4141 IT_STRING_CHARPOS (it_copy) = 0;
4142 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4146 charpos = IT_STRING_CHARPOS (it_copy);
4147 if (charpos >= SCHARS (it->string))
4148 break;
4149 bidi_move_to_visually_next (&it_copy.bidi_it);
4151 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4153 RESTORE_IT (it, it, it_copy_data);
4155 else
4157 /* Set charpos to the string position of the character
4158 that comes after IT's current position in the visual
4159 order. */
4160 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4162 it_copy = *it;
4163 while (n--)
4164 bidi_move_to_visually_next (&it_copy.bidi_it);
4166 charpos = it_copy.bidi_it.charpos;
4169 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4171 if (it->current.overlay_string_index >= 0)
4172 bufpos = IT_CHARPOS (*it);
4173 else
4174 bufpos = 0;
4176 base_face_id = underlying_face_id (it);
4178 /* Get the face for ASCII, or unibyte. */
4179 face_id = face_at_string_position (it->w,
4180 it->string,
4181 charpos,
4182 bufpos,
4183 &next_check_charpos,
4184 base_face_id, false);
4186 /* Correct the face for charsets different from ASCII. Do it
4187 for the multibyte case only. The face returned above is
4188 suitable for unibyte text if IT->string is unibyte. */
4189 if (STRING_MULTIBYTE (it->string))
4191 struct text_pos pos1 = string_pos (charpos, it->string);
4192 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4193 int c, len;
4194 struct face *face = FACE_FROM_ID (it->f, face_id);
4196 c = string_char_and_length (p, &len);
4197 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4200 else
4202 struct text_pos pos;
4204 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4205 || (IT_CHARPOS (*it) <= BEGV && before_p))
4206 return it->face_id;
4208 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4209 pos = it->current.pos;
4211 if (!it->bidi_p)
4213 if (before_p)
4214 DEC_TEXT_POS (pos, it->multibyte_p);
4215 else
4217 if (it->what == IT_COMPOSITION)
4219 /* For composition, we must check the position after
4220 the composition. */
4221 pos.charpos += it->cmp_it.nchars;
4222 pos.bytepos += it->len;
4224 else
4225 INC_TEXT_POS (pos, it->multibyte_p);
4228 else
4230 if (before_p)
4232 int current_x;
4234 /* With bidi iteration, the character before the current
4235 in the visual order cannot be found by simple
4236 iteration, because "reverse" reordering is not
4237 supported. Instead, we need to use the move_it_*
4238 family of functions, and move to the previous
4239 character starting from the beginning of the visual
4240 line. */
4241 /* Ignore face changes before the first visible
4242 character on this display line. */
4243 if (it->current_x <= it->first_visible_x)
4244 return it->face_id;
4245 SAVE_IT (it_copy, *it, it_copy_data);
4246 /* Implementation note: Since move_it_in_display_line
4247 works in the iterator geometry, and thinks the first
4248 character is always the leftmost, even in R2L lines,
4249 we don't need to distinguish between the R2L and L2R
4250 cases here. */
4251 current_x = it_copy.current_x;
4252 move_it_vertically_backward (&it_copy, 0);
4253 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4254 pos = it_copy.current.pos;
4255 RESTORE_IT (it, it, it_copy_data);
4257 else
4259 /* Set charpos to the buffer position of the character
4260 that comes after IT's current position in the visual
4261 order. */
4262 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4264 it_copy = *it;
4265 while (n--)
4266 bidi_move_to_visually_next (&it_copy.bidi_it);
4268 SET_TEXT_POS (pos,
4269 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4272 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4274 /* Determine face for CHARSET_ASCII, or unibyte. */
4275 face_id = face_at_buffer_position (it->w,
4276 CHARPOS (pos),
4277 &next_check_charpos,
4278 limit, false, -1);
4280 /* Correct the face for charsets different from ASCII. Do it
4281 for the multibyte case only. The face returned above is
4282 suitable for unibyte text if current_buffer is unibyte. */
4283 if (it->multibyte_p)
4285 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4286 struct face *face = FACE_FROM_ID (it->f, face_id);
4287 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4291 return face_id;
4296 /***********************************************************************
4297 Invisible text
4298 ***********************************************************************/
4300 /* Set up iterator IT from invisible properties at its current
4301 position. Called from handle_stop. */
4303 static enum prop_handled
4304 handle_invisible_prop (struct it *it)
4306 enum prop_handled handled = HANDLED_NORMALLY;
4307 int invis;
4308 Lisp_Object prop;
4310 if (STRINGP (it->string))
4312 Lisp_Object end_charpos, limit;
4314 /* Get the value of the invisible text property at the
4315 current position. Value will be nil if there is no such
4316 property. */
4317 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4318 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4319 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4321 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4323 /* Record whether we have to display an ellipsis for the
4324 invisible text. */
4325 bool display_ellipsis_p = (invis == 2);
4326 ptrdiff_t len, endpos;
4328 handled = HANDLED_RECOMPUTE_PROPS;
4330 /* Get the position at which the next visible text can be
4331 found in IT->string, if any. */
4332 endpos = len = SCHARS (it->string);
4333 XSETINT (limit, len);
4336 end_charpos
4337 = Fnext_single_property_change (end_charpos, Qinvisible,
4338 it->string, limit);
4339 /* Since LIMIT is always an integer, so should be the
4340 value returned by Fnext_single_property_change. */
4341 eassert (INTEGERP (end_charpos));
4342 if (INTEGERP (end_charpos))
4344 endpos = XFASTINT (end_charpos);
4345 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4346 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 if (invis == 2)
4348 display_ellipsis_p = true;
4350 else /* Should never happen; but if it does, exit the loop. */
4351 endpos = len;
4353 while (invis != 0 && endpos < len);
4355 if (display_ellipsis_p)
4356 it->ellipsis_p = true;
4358 if (endpos < len)
4360 /* Text at END_CHARPOS is visible. Move IT there. */
4361 struct text_pos old;
4362 ptrdiff_t oldpos;
4364 old = it->current.string_pos;
4365 oldpos = CHARPOS (old);
4366 if (it->bidi_p)
4368 if (it->bidi_it.first_elt
4369 && it->bidi_it.charpos < SCHARS (it->string))
4370 bidi_paragraph_init (it->paragraph_embedding,
4371 &it->bidi_it, true);
4372 /* Bidi-iterate out of the invisible text. */
4375 bidi_move_to_visually_next (&it->bidi_it);
4377 while (oldpos <= it->bidi_it.charpos
4378 && it->bidi_it.charpos < endpos
4379 && it->bidi_it.charpos < it->bidi_it.string.schars);
4381 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4383 if (IT_CHARPOS (*it) >= endpos)
4384 it->prev_stop = endpos;
4386 else
4388 IT_STRING_CHARPOS (*it) = endpos;
4389 compute_string_pos (&it->current.string_pos, old, it->string);
4392 else
4394 /* The rest of the string is invisible. If this is an
4395 overlay string, proceed with the next overlay string
4396 or whatever comes and return a character from there. */
4397 if (it->current.overlay_string_index >= 0
4398 && !display_ellipsis_p)
4400 next_overlay_string (it);
4401 /* Don't check for overlay strings when we just
4402 finished processing them. */
4403 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4405 else
4407 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4408 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4413 else
4415 ptrdiff_t newpos, next_stop, start_charpos, tem;
4416 Lisp_Object pos, overlay;
4418 /* First of all, is there invisible text at this position? */
4419 tem = start_charpos = IT_CHARPOS (*it);
4420 pos = make_number (tem);
4421 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4422 &overlay);
4423 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4425 /* If we are on invisible text, skip over it. */
4426 if (invis != 0 && start_charpos < it->end_charpos)
4428 /* Record whether we have to display an ellipsis for the
4429 invisible text. */
4430 bool display_ellipsis_p = invis == 2;
4432 handled = HANDLED_RECOMPUTE_PROPS;
4434 /* Loop skipping over invisible text. The loop is left at
4435 ZV or with IT on the first char being visible again. */
4438 /* Try to skip some invisible text. Return value is the
4439 position reached which can be equal to where we start
4440 if there is nothing invisible there. This skips both
4441 over invisible text properties and overlays with
4442 invisible property. */
4443 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4445 /* If we skipped nothing at all we weren't at invisible
4446 text in the first place. If everything to the end of
4447 the buffer was skipped, end the loop. */
4448 if (newpos == tem || newpos >= ZV)
4449 invis = 0;
4450 else
4452 /* We skipped some characters but not necessarily
4453 all there are. Check if we ended up on visible
4454 text. Fget_char_property returns the property of
4455 the char before the given position, i.e. if we
4456 get invis = 0, this means that the char at
4457 newpos is visible. */
4458 pos = make_number (newpos);
4459 prop = Fget_char_property (pos, Qinvisible, it->window);
4460 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4463 /* If we ended up on invisible text, proceed to
4464 skip starting with next_stop. */
4465 if (invis != 0)
4466 tem = next_stop;
4468 /* If there are adjacent invisible texts, don't lose the
4469 second one's ellipsis. */
4470 if (invis == 2)
4471 display_ellipsis_p = true;
4473 while (invis != 0);
4475 /* The position newpos is now either ZV or on visible text. */
4476 if (it->bidi_p)
4478 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4479 bool on_newline
4480 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4481 bool after_newline
4482 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4484 /* If the invisible text ends on a newline or on a
4485 character after a newline, we can avoid the costly,
4486 character by character, bidi iteration to NEWPOS, and
4487 instead simply reseat the iterator there. That's
4488 because all bidi reordering information is tossed at
4489 the newline. This is a big win for modes that hide
4490 complete lines, like Outline, Org, etc. */
4491 if (on_newline || after_newline)
4493 struct text_pos tpos;
4494 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4496 SET_TEXT_POS (tpos, newpos, bpos);
4497 reseat_1 (it, tpos, false);
4498 /* If we reseat on a newline/ZV, we need to prep the
4499 bidi iterator for advancing to the next character
4500 after the newline/EOB, keeping the current paragraph
4501 direction (so that PRODUCE_GLYPHS does TRT wrt
4502 prepending/appending glyphs to a glyph row). */
4503 if (on_newline)
4505 it->bidi_it.first_elt = false;
4506 it->bidi_it.paragraph_dir = pdir;
4507 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4508 it->bidi_it.nchars = 1;
4509 it->bidi_it.ch_len = 1;
4512 else /* Must use the slow method. */
4514 /* With bidi iteration, the region of invisible text
4515 could start and/or end in the middle of a
4516 non-base embedding level. Therefore, we need to
4517 skip invisible text using the bidi iterator,
4518 starting at IT's current position, until we find
4519 ourselves outside of the invisible text.
4520 Skipping invisible text _after_ bidi iteration
4521 avoids affecting the visual order of the
4522 displayed text when invisible properties are
4523 added or removed. */
4524 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4526 /* If we were `reseat'ed to a new paragraph,
4527 determine the paragraph base direction. We
4528 need to do it now because
4529 next_element_from_buffer may not have a
4530 chance to do it, if we are going to skip any
4531 text at the beginning, which resets the
4532 FIRST_ELT flag. */
4533 bidi_paragraph_init (it->paragraph_embedding,
4534 &it->bidi_it, true);
4538 bidi_move_to_visually_next (&it->bidi_it);
4540 while (it->stop_charpos <= it->bidi_it.charpos
4541 && it->bidi_it.charpos < newpos);
4542 IT_CHARPOS (*it) = it->bidi_it.charpos;
4543 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4544 /* If we overstepped NEWPOS, record its position in
4545 the iterator, so that we skip invisible text if
4546 later the bidi iteration lands us in the
4547 invisible region again. */
4548 if (IT_CHARPOS (*it) >= newpos)
4549 it->prev_stop = newpos;
4552 else
4554 IT_CHARPOS (*it) = newpos;
4555 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4558 if (display_ellipsis_p)
4560 /* Make sure that the glyphs of the ellipsis will get
4561 correct `charpos' values. If we would not update
4562 it->position here, the glyphs would belong to the
4563 last visible character _before_ the invisible
4564 text, which confuses `set_cursor_from_row'.
4566 We use the last invisible position instead of the
4567 first because this way the cursor is always drawn on
4568 the first "." of the ellipsis, whenever PT is inside
4569 the invisible text. Otherwise the cursor would be
4570 placed _after_ the ellipsis when the point is after the
4571 first invisible character. */
4572 if (!STRINGP (it->object))
4574 it->position.charpos = newpos - 1;
4575 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4579 /* If there are before-strings at the start of invisible
4580 text, and the text is invisible because of a text
4581 property, arrange to show before-strings because 20.x did
4582 it that way. (If the text is invisible because of an
4583 overlay property instead of a text property, this is
4584 already handled in the overlay code.) */
4585 if (NILP (overlay)
4586 && get_overlay_strings (it, it->stop_charpos))
4588 handled = HANDLED_RECOMPUTE_PROPS;
4589 if (it->sp > 0)
4591 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4592 /* The call to get_overlay_strings above recomputes
4593 it->stop_charpos, but it only considers changes
4594 in properties and overlays beyond iterator's
4595 current position. This causes us to miss changes
4596 that happen exactly where the invisible property
4597 ended. So we play it safe here and force the
4598 iterator to check for potential stop positions
4599 immediately after the invisible text. Note that
4600 if get_overlay_strings returns true, it
4601 normally also pushed the iterator stack, so we
4602 need to update the stop position in the slot
4603 below the current one. */
4604 it->stack[it->sp - 1].stop_charpos
4605 = CHARPOS (it->stack[it->sp - 1].current.pos);
4608 else if (display_ellipsis_p)
4610 it->ellipsis_p = true;
4611 /* Let the ellipsis display before
4612 considering any properties of the following char.
4613 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4614 handled = HANDLED_RETURN;
4619 return handled;
4623 /* Make iterator IT return `...' next.
4624 Replaces LEN characters from buffer. */
4626 static void
4627 setup_for_ellipsis (struct it *it, int len)
4629 /* Use the display table definition for `...'. Invalid glyphs
4630 will be handled by the method returning elements from dpvec. */
4631 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4633 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4634 it->dpvec = v->contents;
4635 it->dpend = v->contents + v->header.size;
4637 else
4639 /* Default `...'. */
4640 it->dpvec = default_invis_vector;
4641 it->dpend = default_invis_vector + 3;
4644 it->dpvec_char_len = len;
4645 it->current.dpvec_index = 0;
4646 it->dpvec_face_id = -1;
4648 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4649 face as the preceding text. IT->saved_face_id was set in
4650 handle_stop to the face of the preceding character, and will be
4651 different from IT->face_id only if the invisible text skipped in
4652 handle_invisible_prop has some non-default face on its first
4653 character. We thus ignore the face of the invisible text when we
4654 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4655 if (it->saved_face_id >= 0)
4656 it->face_id = it->saved_face_id;
4658 /* If the ellipsis represents buffer text, it means we advanced in
4659 the buffer, so we should no longer ignore overlay strings. */
4660 if (it->method == GET_FROM_BUFFER)
4661 it->ignore_overlay_strings_at_pos_p = false;
4663 it->method = GET_FROM_DISPLAY_VECTOR;
4664 it->ellipsis_p = true;
4669 /***********************************************************************
4670 'display' property
4671 ***********************************************************************/
4673 /* Set up iterator IT from `display' property at its current position.
4674 Called from handle_stop.
4675 We return HANDLED_RETURN if some part of the display property
4676 overrides the display of the buffer text itself.
4677 Otherwise we return HANDLED_NORMALLY. */
4679 static enum prop_handled
4680 handle_display_prop (struct it *it)
4682 Lisp_Object propval, object, overlay;
4683 struct text_pos *position;
4684 ptrdiff_t bufpos;
4685 /* Nonzero if some property replaces the display of the text itself. */
4686 int display_replaced = 0;
4688 if (STRINGP (it->string))
4690 object = it->string;
4691 position = &it->current.string_pos;
4692 bufpos = CHARPOS (it->current.pos);
4694 else
4696 XSETWINDOW (object, it->w);
4697 position = &it->current.pos;
4698 bufpos = CHARPOS (*position);
4701 /* Reset those iterator values set from display property values. */
4702 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4703 it->space_width = Qnil;
4704 it->font_height = Qnil;
4705 it->voffset = 0;
4707 /* We don't support recursive `display' properties, i.e. string
4708 values that have a string `display' property, that have a string
4709 `display' property etc. */
4710 if (!it->string_from_display_prop_p)
4711 it->area = TEXT_AREA;
4713 propval = get_char_property_and_overlay (make_number (position->charpos),
4714 Qdisplay, object, &overlay);
4715 if (NILP (propval))
4716 return HANDLED_NORMALLY;
4717 /* Now OVERLAY is the overlay that gave us this property, or nil
4718 if it was a text property. */
4720 if (!STRINGP (it->string))
4721 object = it->w->contents;
4723 display_replaced = handle_display_spec (it, propval, object, overlay,
4724 position, bufpos,
4725 FRAME_WINDOW_P (it->f));
4726 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4729 /* Subroutine of handle_display_prop. Returns non-zero if the display
4730 specification in SPEC is a replacing specification, i.e. it would
4731 replace the text covered by `display' property with something else,
4732 such as an image or a display string. If SPEC includes any kind or
4733 `(space ...) specification, the value is 2; this is used by
4734 compute_display_string_pos, which see.
4736 See handle_single_display_spec for documentation of arguments.
4737 FRAME_WINDOW_P is true if the window being redisplayed is on a
4738 GUI frame; this argument is used only if IT is NULL, see below.
4740 IT can be NULL, if this is called by the bidi reordering code
4741 through compute_display_string_pos, which see. In that case, this
4742 function only examines SPEC, but does not otherwise "handle" it, in
4743 the sense that it doesn't set up members of IT from the display
4744 spec. */
4745 static int
4746 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4747 Lisp_Object overlay, struct text_pos *position,
4748 ptrdiff_t bufpos, bool frame_window_p)
4750 int replacing = 0;
4751 bool enable_eval = true;
4753 /* Support (disable-eval PROP) which is used by enriched.el. */
4754 if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
4756 enable_eval = false;
4757 spec = XCAR (XCDR (spec));
4760 if (CONSP (spec)
4761 /* Simple specifications. */
4762 && !EQ (XCAR (spec), Qimage)
4763 #ifdef HAVE_XWIDGETS
4764 && !EQ (XCAR (spec), Qxwidget)
4765 #endif
4766 && !EQ (XCAR (spec), Qspace)
4767 && !EQ (XCAR (spec), Qwhen)
4768 && !EQ (XCAR (spec), Qslice)
4769 && !EQ (XCAR (spec), Qspace_width)
4770 && !EQ (XCAR (spec), Qheight)
4771 && !EQ (XCAR (spec), Qraise)
4772 /* Marginal area specifications. */
4773 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4774 && !EQ (XCAR (spec), Qleft_fringe)
4775 && !EQ (XCAR (spec), Qright_fringe)
4776 && !NILP (XCAR (spec)))
4778 for (; CONSP (spec); spec = XCDR (spec))
4780 int rv = handle_single_display_spec (it, XCAR (spec), object,
4781 overlay, position, bufpos,
4782 replacing, frame_window_p,
4783 enable_eval);
4784 if (rv != 0)
4786 replacing = rv;
4787 /* If some text in a string is replaced, `position' no
4788 longer points to the position of `object'. */
4789 if (!it || STRINGP (object))
4790 break;
4794 else if (VECTORP (spec))
4796 ptrdiff_t i;
4797 for (i = 0; i < ASIZE (spec); ++i)
4799 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4800 overlay, position, bufpos,
4801 replacing, frame_window_p,
4802 enable_eval);
4803 if (rv != 0)
4805 replacing = rv;
4806 /* If some text in a string is replaced, `position' no
4807 longer points to the position of `object'. */
4808 if (!it || STRINGP (object))
4809 break;
4813 else
4814 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4815 bufpos, 0, frame_window_p,
4816 enable_eval);
4817 return replacing;
4820 /* Value is the position of the end of the `display' property starting
4821 at START_POS in OBJECT. */
4823 static struct text_pos
4824 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4826 Lisp_Object end;
4827 struct text_pos end_pos;
4829 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4830 Qdisplay, object, Qnil);
4831 CHARPOS (end_pos) = XFASTINT (end);
4832 if (STRINGP (object))
4833 compute_string_pos (&end_pos, start_pos, it->string);
4834 else
4835 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4837 return end_pos;
4841 /* Set up IT from a single `display' property specification SPEC. OBJECT
4842 is the object in which the `display' property was found. *POSITION
4843 is the position in OBJECT at which the `display' property was found.
4844 BUFPOS is the buffer position of OBJECT (different from POSITION if
4845 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4846 previously saw a display specification which already replaced text
4847 display with something else, for example an image; we ignore such
4848 properties after the first one has been processed.
4850 OVERLAY is the overlay this `display' property came from,
4851 or nil if it was a text property.
4853 If SPEC is a `space' or `image' specification, and in some other
4854 cases too, set *POSITION to the position where the `display'
4855 property ends.
4857 If IT is NULL, only examine the property specification in SPEC, but
4858 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4859 is intended to be displayed in a window on a GUI frame.
4861 Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
4863 Value is non-zero if something was found which replaces the display
4864 of buffer or string text. */
4866 static int
4867 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4868 Lisp_Object overlay, struct text_pos *position,
4869 ptrdiff_t bufpos, int display_replaced,
4870 bool frame_window_p, bool enable_eval_p)
4872 Lisp_Object form;
4873 Lisp_Object location, value;
4874 struct text_pos start_pos = *position;
4876 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4877 If the result is non-nil, use VALUE instead of SPEC. */
4878 form = Qt;
4879 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4881 spec = XCDR (spec);
4882 if (!CONSP (spec))
4883 return 0;
4884 form = XCAR (spec);
4885 spec = XCDR (spec);
4888 if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
4889 form = Qnil;
4890 if (!NILP (form) && !EQ (form, Qt))
4892 ptrdiff_t count = SPECPDL_INDEX ();
4894 /* Bind `object' to the object having the `display' property, a
4895 buffer or string. Bind `position' to the position in the
4896 object where the property was found, and `buffer-position'
4897 to the current position in the buffer. */
4899 if (NILP (object))
4900 XSETBUFFER (object, current_buffer);
4901 specbind (Qobject, object);
4902 specbind (Qposition, make_number (CHARPOS (*position)));
4903 specbind (Qbuffer_position, make_number (bufpos));
4904 form = safe_eval (form);
4905 unbind_to (count, Qnil);
4908 if (NILP (form))
4909 return 0;
4911 /* Handle `(height HEIGHT)' specifications. */
4912 if (CONSP (spec)
4913 && EQ (XCAR (spec), Qheight)
4914 && CONSP (XCDR (spec)))
4916 if (it)
4918 if (!FRAME_WINDOW_P (it->f))
4919 return 0;
4921 it->font_height = XCAR (XCDR (spec));
4922 if (!NILP (it->font_height))
4924 int new_height = -1;
4926 if (CONSP (it->font_height)
4927 && (EQ (XCAR (it->font_height), Qplus)
4928 || EQ (XCAR (it->font_height), Qminus))
4929 && CONSP (XCDR (it->font_height))
4930 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4932 /* `(+ N)' or `(- N)' where N is an integer. */
4933 int steps = XINT (XCAR (XCDR (it->font_height)));
4934 if (EQ (XCAR (it->font_height), Qplus))
4935 steps = - steps;
4936 it->face_id = smaller_face (it->f, it->face_id, steps);
4938 else if (FUNCTIONP (it->font_height) && enable_eval_p)
4940 /* Call function with current height as argument.
4941 Value is the new height. */
4942 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4943 Lisp_Object height;
4944 height = safe_call1 (it->font_height,
4945 face->lface[LFACE_HEIGHT_INDEX]);
4946 if (NUMBERP (height))
4947 new_height = XFLOATINT (height);
4949 else if (NUMBERP (it->font_height))
4951 /* Value is a multiple of the canonical char height. */
4952 struct face *f;
4954 f = FACE_FROM_ID (it->f,
4955 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4956 new_height = (XFLOATINT (it->font_height)
4957 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4959 else if (enable_eval_p)
4961 /* Evaluate IT->font_height with `height' bound to the
4962 current specified height to get the new height. */
4963 ptrdiff_t count = SPECPDL_INDEX ();
4964 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4966 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4967 value = safe_eval (it->font_height);
4968 unbind_to (count, Qnil);
4970 if (NUMBERP (value))
4971 new_height = XFLOATINT (value);
4974 if (new_height > 0)
4975 it->face_id = face_with_height (it->f, it->face_id, new_height);
4979 return 0;
4982 /* Handle `(space-width WIDTH)'. */
4983 if (CONSP (spec)
4984 && EQ (XCAR (spec), Qspace_width)
4985 && CONSP (XCDR (spec)))
4987 if (it)
4989 if (!FRAME_WINDOW_P (it->f))
4990 return 0;
4992 value = XCAR (XCDR (spec));
4993 if (NUMBERP (value) && XFLOATINT (value) > 0)
4994 it->space_width = value;
4997 return 0;
5000 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5001 if (CONSP (spec)
5002 && EQ (XCAR (spec), Qslice))
5004 Lisp_Object tem;
5006 if (it)
5008 if (!FRAME_WINDOW_P (it->f))
5009 return 0;
5011 if (tem = XCDR (spec), CONSP (tem))
5013 it->slice.x = XCAR (tem);
5014 if (tem = XCDR (tem), CONSP (tem))
5016 it->slice.y = XCAR (tem);
5017 if (tem = XCDR (tem), CONSP (tem))
5019 it->slice.width = XCAR (tem);
5020 if (tem = XCDR (tem), CONSP (tem))
5021 it->slice.height = XCAR (tem);
5027 return 0;
5030 /* Handle `(raise FACTOR)'. */
5031 if (CONSP (spec)
5032 && EQ (XCAR (spec), Qraise)
5033 && CONSP (XCDR (spec)))
5035 if (it)
5037 if (!FRAME_WINDOW_P (it->f))
5038 return 0;
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 value = XCAR (XCDR (spec));
5042 if (NUMBERP (value))
5044 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5045 it->voffset = - (XFLOATINT (value)
5046 * (normal_char_height (face->font, -1)));
5048 #endif /* HAVE_WINDOW_SYSTEM */
5051 return 0;
5054 /* Don't handle the other kinds of display specifications
5055 inside a string that we got from a `display' property. */
5056 if (it && it->string_from_display_prop_p)
5057 return 0;
5059 /* Characters having this form of property are not displayed, so
5060 we have to find the end of the property. */
5061 if (it)
5063 start_pos = *position;
5064 *position = display_prop_end (it, object, start_pos);
5065 /* If the display property comes from an overlay, don't consider
5066 any potential stop_charpos values before the end of that
5067 overlay. Since display_prop_end will happily find another
5068 'display' property coming from some other overlay or text
5069 property on buffer positions before this overlay's end, we
5070 need to ignore them, or else we risk displaying this
5071 overlay's display string/image twice. */
5072 if (!NILP (overlay))
5074 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5076 /* Some borderline-sane Lisp might call us with the current
5077 buffer narrowed so that overlay-end is outside the
5078 POINT_MIN..POINT_MAX region, which will then cause
5079 various assertion violations and crashes down the road,
5080 starting with pop_it when it will attempt to use POSITION
5081 set below. Prevent that. */
5082 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5084 if (ovendpos > CHARPOS (*position))
5085 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5088 value = Qnil;
5090 /* Stop the scan at that end position--we assume that all
5091 text properties change there. */
5092 if (it)
5093 it->stop_charpos = position->charpos;
5095 /* Handle `(left-fringe BITMAP [FACE])'
5096 and `(right-fringe BITMAP [FACE])'. */
5097 if (CONSP (spec)
5098 && (EQ (XCAR (spec), Qleft_fringe)
5099 || EQ (XCAR (spec), Qright_fringe))
5100 && CONSP (XCDR (spec)))
5102 if (it)
5104 if (!FRAME_WINDOW_P (it->f))
5105 /* If we return here, POSITION has been advanced
5106 across the text with this property. */
5108 /* Synchronize the bidi iterator with POSITION. This is
5109 needed because we are not going to push the iterator
5110 on behalf of this display property, so there will be
5111 no pop_it call to do this synchronization for us. */
5112 if (it->bidi_p)
5114 it->position = *position;
5115 iterate_out_of_display_property (it);
5116 *position = it->position;
5118 return 1;
5121 else if (!frame_window_p)
5122 return 1;
5124 #ifdef HAVE_WINDOW_SYSTEM
5125 value = XCAR (XCDR (spec));
5126 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5127 if (! fringe_bitmap)
5128 /* If we return here, POSITION has been advanced
5129 across the text with this property. */
5131 if (it && it->bidi_p)
5133 it->position = *position;
5134 iterate_out_of_display_property (it);
5135 *position = it->position;
5137 return 1;
5140 if (it)
5142 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5144 if (CONSP (XCDR (XCDR (spec))))
5146 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5147 int face_id2 = lookup_derived_face (it->f, face_name,
5148 FRINGE_FACE_ID, false);
5149 if (face_id2 >= 0)
5150 face_id = face_id2;
5153 /* Save current settings of IT so that we can restore them
5154 when we are finished with the glyph property value. */
5155 push_it (it, position);
5157 it->area = TEXT_AREA;
5158 it->what = IT_IMAGE;
5159 it->image_id = -1; /* no image */
5160 it->position = start_pos;
5161 it->object = NILP (object) ? it->w->contents : object;
5162 it->method = GET_FROM_IMAGE;
5163 it->from_overlay = Qnil;
5164 it->face_id = face_id;
5165 it->from_disp_prop_p = true;
5167 /* Say that we haven't consumed the characters with
5168 `display' property yet. The call to pop_it in
5169 set_iterator_to_next will clean this up. */
5170 *position = start_pos;
5172 if (EQ (XCAR (spec), Qleft_fringe))
5174 it->left_user_fringe_bitmap = fringe_bitmap;
5175 it->left_user_fringe_face_id = face_id;
5177 else
5179 it->right_user_fringe_bitmap = fringe_bitmap;
5180 it->right_user_fringe_face_id = face_id;
5183 #endif /* HAVE_WINDOW_SYSTEM */
5184 return 1;
5187 /* Prepare to handle `((margin left-margin) ...)',
5188 `((margin right-margin) ...)' and `((margin nil) ...)'
5189 prefixes for display specifications. */
5190 location = Qunbound;
5191 if (CONSP (spec) && CONSP (XCAR (spec)))
5193 Lisp_Object tem;
5195 value = XCDR (spec);
5196 if (CONSP (value))
5197 value = XCAR (value);
5199 tem = XCAR (spec);
5200 if (EQ (XCAR (tem), Qmargin)
5201 && (tem = XCDR (tem),
5202 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5203 (NILP (tem)
5204 || EQ (tem, Qleft_margin)
5205 || EQ (tem, Qright_margin))))
5206 location = tem;
5209 if (EQ (location, Qunbound))
5211 location = Qnil;
5212 value = spec;
5215 /* After this point, VALUE is the property after any
5216 margin prefix has been stripped. It must be a string,
5217 an image specification, or `(space ...)'.
5219 LOCATION specifies where to display: `left-margin',
5220 `right-margin' or nil. */
5222 bool valid_p = (STRINGP (value)
5223 #ifdef HAVE_WINDOW_SYSTEM
5224 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5225 && valid_image_p (value))
5226 #endif /* not HAVE_WINDOW_SYSTEM */
5227 || (CONSP (value) && EQ (XCAR (value), Qspace))
5228 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5229 && valid_xwidget_spec_p (value)));
5231 if (valid_p && display_replaced == 0)
5233 int retval = 1;
5235 if (!it)
5237 /* Callers need to know whether the display spec is any kind
5238 of `(space ...)' spec that is about to affect text-area
5239 display. */
5240 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5241 retval = 2;
5242 return retval;
5245 /* Save current settings of IT so that we can restore them
5246 when we are finished with the glyph property value. */
5247 push_it (it, position);
5248 it->from_overlay = overlay;
5249 it->from_disp_prop_p = true;
5251 if (NILP (location))
5252 it->area = TEXT_AREA;
5253 else if (EQ (location, Qleft_margin))
5254 it->area = LEFT_MARGIN_AREA;
5255 else
5256 it->area = RIGHT_MARGIN_AREA;
5258 if (STRINGP (value))
5260 it->string = value;
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 it->current.overlay_string_index = -1;
5263 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5264 it->end_charpos = it->string_nchars = SCHARS (it->string);
5265 it->method = GET_FROM_STRING;
5266 it->stop_charpos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5269 it->string_from_display_prop_p = true;
5270 it->cmp_it.id = -1;
5271 /* Say that we haven't consumed the characters with
5272 `display' property yet. The call to pop_it in
5273 set_iterator_to_next will clean this up. */
5274 if (BUFFERP (object))
5275 *position = start_pos;
5277 /* Force paragraph direction to be that of the parent
5278 object. If the parent object's paragraph direction is
5279 not yet determined, default to L2R. */
5280 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5281 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5282 else
5283 it->paragraph_embedding = L2R;
5285 /* Set up the bidi iterator for this display string. */
5286 if (it->bidi_p)
5288 it->bidi_it.string.lstring = it->string;
5289 it->bidi_it.string.s = NULL;
5290 it->bidi_it.string.schars = it->end_charpos;
5291 it->bidi_it.string.bufpos = bufpos;
5292 it->bidi_it.string.from_disp_str = true;
5293 it->bidi_it.string.unibyte = !it->multibyte_p;
5294 it->bidi_it.w = it->w;
5295 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5298 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5300 it->method = GET_FROM_STRETCH;
5301 it->object = value;
5302 *position = it->position = start_pos;
5303 retval = 1 + (it->area == TEXT_AREA);
5305 else if (valid_xwidget_spec_p (value))
5307 it->what = IT_XWIDGET;
5308 it->method = GET_FROM_XWIDGET;
5309 it->position = start_pos;
5310 it->object = NILP (object) ? it->w->contents : object;
5311 *position = start_pos;
5312 it->xwidget = lookup_xwidget (value);
5314 #ifdef HAVE_WINDOW_SYSTEM
5315 else
5317 it->what = IT_IMAGE;
5318 it->image_id = lookup_image (it->f, value);
5319 it->position = start_pos;
5320 it->object = NILP (object) ? it->w->contents : object;
5321 it->method = GET_FROM_IMAGE;
5323 /* Say that we haven't consumed the characters with
5324 `display' property yet. The call to pop_it in
5325 set_iterator_to_next will clean this up. */
5326 *position = start_pos;
5328 #endif /* HAVE_WINDOW_SYSTEM */
5330 return retval;
5333 /* Invalid property or property not supported. Restore
5334 POSITION to what it was before. */
5335 *position = start_pos;
5336 return 0;
5339 /* Check if PROP is a display property value whose text should be
5340 treated as intangible. OVERLAY is the overlay from which PROP
5341 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5342 specify the buffer position covered by PROP. */
5344 bool
5345 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5346 ptrdiff_t charpos, ptrdiff_t bytepos)
5348 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5349 struct text_pos position;
5351 SET_TEXT_POS (position, charpos, bytepos);
5352 return (handle_display_spec (NULL, prop, Qnil, overlay,
5353 &position, charpos, frame_window_p)
5354 != 0);
5358 /* Return true if PROP is a display sub-property value containing STRING.
5360 Implementation note: this and the following function are really
5361 special cases of handle_display_spec and
5362 handle_single_display_spec, and should ideally use the same code.
5363 Until they do, these two pairs must be consistent and must be
5364 modified in sync. */
5366 static bool
5367 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5369 if (EQ (string, prop))
5370 return true;
5372 /* Skip over `when FORM'. */
5373 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5375 prop = XCDR (prop);
5376 if (!CONSP (prop))
5377 return false;
5378 /* Actually, the condition following `when' should be eval'ed,
5379 like handle_single_display_spec does, and we should return
5380 false if it evaluates to nil. However, this function is
5381 called only when the buffer was already displayed and some
5382 glyph in the glyph matrix was found to come from a display
5383 string. Therefore, the condition was already evaluated, and
5384 the result was non-nil, otherwise the display string wouldn't
5385 have been displayed and we would have never been called for
5386 this property. Thus, we can skip the evaluation and assume
5387 its result is non-nil. */
5388 prop = XCDR (prop);
5391 if (CONSP (prop))
5392 /* Skip over `margin LOCATION'. */
5393 if (EQ (XCAR (prop), Qmargin))
5395 prop = XCDR (prop);
5396 if (!CONSP (prop))
5397 return false;
5399 prop = XCDR (prop);
5400 if (!CONSP (prop))
5401 return false;
5404 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5408 /* Return true if STRING appears in the `display' property PROP. */
5410 static bool
5411 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5413 if (CONSP (prop)
5414 && !EQ (XCAR (prop), Qwhen)
5415 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5417 /* A list of sub-properties. */
5418 while (CONSP (prop))
5420 if (single_display_spec_string_p (XCAR (prop), string))
5421 return true;
5422 prop = XCDR (prop);
5425 else if (VECTORP (prop))
5427 /* A vector of sub-properties. */
5428 ptrdiff_t i;
5429 for (i = 0; i < ASIZE (prop); ++i)
5430 if (single_display_spec_string_p (AREF (prop, i), string))
5431 return true;
5433 else
5434 return single_display_spec_string_p (prop, string);
5436 return false;
5439 /* Look for STRING in overlays and text properties in the current
5440 buffer, between character positions FROM and TO (excluding TO).
5441 BACK_P means look back (in this case, TO is supposed to be
5442 less than FROM).
5443 Value is the first character position where STRING was found, or
5444 zero if it wasn't found before hitting TO.
5446 This function may only use code that doesn't eval because it is
5447 called asynchronously from note_mouse_highlight. */
5449 static ptrdiff_t
5450 string_buffer_position_lim (Lisp_Object string,
5451 ptrdiff_t from, ptrdiff_t to, bool back_p)
5453 Lisp_Object limit, prop, pos;
5454 bool found = false;
5456 pos = make_number (max (from, BEGV));
5458 if (!back_p) /* looking forward */
5460 limit = make_number (min (to, ZV));
5461 while (!found && !EQ (pos, limit))
5463 prop = Fget_char_property (pos, Qdisplay, Qnil);
5464 if (!NILP (prop) && display_prop_string_p (prop, string))
5465 found = true;
5466 else
5467 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5468 limit);
5471 else /* looking back */
5473 limit = make_number (max (to, BEGV));
5474 while (!found && !EQ (pos, limit))
5476 prop = Fget_char_property (pos, Qdisplay, Qnil);
5477 if (!NILP (prop) && display_prop_string_p (prop, string))
5478 found = true;
5479 else
5480 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5481 limit);
5485 return found ? XINT (pos) : 0;
5488 /* Determine which buffer position in current buffer STRING comes from.
5489 AROUND_CHARPOS is an approximate position where it could come from.
5490 Value is the buffer position or 0 if it couldn't be determined.
5492 This function is necessary because we don't record buffer positions
5493 in glyphs generated from strings (to keep struct glyph small).
5494 This function may only use code that doesn't eval because it is
5495 called asynchronously from note_mouse_highlight. */
5497 static ptrdiff_t
5498 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5500 const int MAX_DISTANCE = 1000;
5501 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5502 around_charpos + MAX_DISTANCE,
5503 false);
5505 if (!found)
5506 found = string_buffer_position_lim (string, around_charpos,
5507 around_charpos - MAX_DISTANCE, true);
5508 return found;
5513 /***********************************************************************
5514 `composition' property
5515 ***********************************************************************/
5517 /* Set up iterator IT from `composition' property at its current
5518 position. Called from handle_stop. */
5520 static enum prop_handled
5521 handle_composition_prop (struct it *it)
5523 Lisp_Object prop, string;
5524 ptrdiff_t pos, pos_byte, start, end;
5526 if (STRINGP (it->string))
5528 unsigned char *s;
5530 pos = IT_STRING_CHARPOS (*it);
5531 pos_byte = IT_STRING_BYTEPOS (*it);
5532 string = it->string;
5533 s = SDATA (string) + pos_byte;
5534 it->c = STRING_CHAR (s);
5536 else
5538 pos = IT_CHARPOS (*it);
5539 pos_byte = IT_BYTEPOS (*it);
5540 string = Qnil;
5541 it->c = FETCH_CHAR (pos_byte);
5544 /* If there's a valid composition and point is not inside of the
5545 composition (in the case that the composition is from the current
5546 buffer), draw a glyph composed from the composition components. */
5547 if (find_composition (pos, -1, &start, &end, &prop, string)
5548 && composition_valid_p (start, end, prop)
5549 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5551 if (start < pos)
5552 /* As we can't handle this situation (perhaps font-lock added
5553 a new composition), we just return here hoping that next
5554 redisplay will detect this composition much earlier. */
5555 return HANDLED_NORMALLY;
5556 if (start != pos)
5558 if (STRINGP (it->string))
5559 pos_byte = string_char_to_byte (it->string, start);
5560 else
5561 pos_byte = CHAR_TO_BYTE (start);
5563 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5564 prop, string);
5566 if (it->cmp_it.id >= 0)
5568 it->cmp_it.ch = -1;
5569 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5570 it->cmp_it.nglyphs = -1;
5574 return HANDLED_NORMALLY;
5579 /***********************************************************************
5580 Overlay strings
5581 ***********************************************************************/
5583 /* The following structure is used to record overlay strings for
5584 later sorting in load_overlay_strings. */
5586 struct overlay_entry
5588 Lisp_Object overlay;
5589 Lisp_Object string;
5590 EMACS_INT priority;
5591 bool after_string_p;
5595 /* Set up iterator IT from overlay strings at its current position.
5596 Called from handle_stop. */
5598 static enum prop_handled
5599 handle_overlay_change (struct it *it)
5601 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5602 return HANDLED_RECOMPUTE_PROPS;
5603 else
5604 return HANDLED_NORMALLY;
5608 /* Set up the next overlay string for delivery by IT, if there is an
5609 overlay string to deliver. Called by set_iterator_to_next when the
5610 end of the current overlay string is reached. If there are more
5611 overlay strings to display, IT->string and
5612 IT->current.overlay_string_index are set appropriately here.
5613 Otherwise IT->string is set to nil. */
5615 static void
5616 next_overlay_string (struct it *it)
5618 ++it->current.overlay_string_index;
5619 if (it->current.overlay_string_index == it->n_overlay_strings)
5621 /* No more overlay strings. Restore IT's settings to what
5622 they were before overlay strings were processed, and
5623 continue to deliver from current_buffer. */
5625 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5626 pop_it (it);
5627 eassert (it->sp > 0
5628 || (NILP (it->string)
5629 && it->method == GET_FROM_BUFFER
5630 && it->stop_charpos >= BEGV
5631 && it->stop_charpos <= it->end_charpos));
5632 it->current.overlay_string_index = -1;
5633 it->n_overlay_strings = 0;
5634 /* If there's an empty display string on the stack, pop the
5635 stack, to resync the bidi iterator with IT's position. Such
5636 empty strings are pushed onto the stack in
5637 get_overlay_strings_1. */
5638 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5639 pop_it (it);
5641 /* Since we've exhausted overlay strings at this buffer
5642 position, set the flag to ignore overlays until we move to
5643 another position. (The flag will be reset in
5644 next_element_from_buffer.) But don't do that if the overlay
5645 strings were loaded at position other than the current one,
5646 which could happen if we called pop_it above, or if the
5647 overlay strings were loaded by handle_invisible_prop at the
5648 beginning of invisible text. */
5649 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5650 it->ignore_overlay_strings_at_pos_p = true;
5652 /* If we're at the end of the buffer, record that we have
5653 processed the overlay strings there already, so that
5654 next_element_from_buffer doesn't try it again. */
5655 if (NILP (it->string)
5656 && IT_CHARPOS (*it) >= it->end_charpos
5657 && it->overlay_strings_charpos >= it->end_charpos)
5658 it->overlay_strings_at_end_processed_p = true;
5659 /* Note: we reset overlay_strings_charpos only here, to make
5660 sure the just-processed overlays were indeed at EOB.
5661 Otherwise, overlays on text with invisible text property,
5662 which are processed with IT's position past the invisible
5663 text, might fool us into thinking the overlays at EOB were
5664 already processed (linum-mode can cause this, for
5665 example). */
5666 it->overlay_strings_charpos = -1;
5668 else
5670 /* There are more overlay strings to process. If
5671 IT->current.overlay_string_index has advanced to a position
5672 where we must load IT->overlay_strings with more strings, do
5673 it. We must load at the IT->overlay_strings_charpos where
5674 IT->n_overlay_strings was originally computed; when invisible
5675 text is present, this might not be IT_CHARPOS (Bug#7016). */
5676 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5678 if (it->current.overlay_string_index && i == 0)
5679 load_overlay_strings (it, it->overlay_strings_charpos);
5681 /* Initialize IT to deliver display elements from the overlay
5682 string. */
5683 it->string = it->overlay_strings[i];
5684 it->multibyte_p = STRING_MULTIBYTE (it->string);
5685 SET_TEXT_POS (it->current.string_pos, 0, 0);
5686 it->method = GET_FROM_STRING;
5687 it->stop_charpos = 0;
5688 it->end_charpos = SCHARS (it->string);
5689 if (it->cmp_it.stop_pos >= 0)
5690 it->cmp_it.stop_pos = 0;
5691 it->prev_stop = 0;
5692 it->base_level_stop = 0;
5694 /* Set up the bidi iterator for this overlay string. */
5695 if (it->bidi_p)
5697 it->bidi_it.string.lstring = it->string;
5698 it->bidi_it.string.s = NULL;
5699 it->bidi_it.string.schars = SCHARS (it->string);
5700 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5701 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5702 it->bidi_it.string.unibyte = !it->multibyte_p;
5703 it->bidi_it.w = it->w;
5704 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5708 CHECK_IT (it);
5712 /* Compare two overlay_entry structures E1 and E2. Used as a
5713 comparison function for qsort in load_overlay_strings. Overlay
5714 strings for the same position are sorted so that
5716 1. All after-strings come in front of before-strings, except
5717 when they come from the same overlay.
5719 2. Within after-strings, strings are sorted so that overlay strings
5720 from overlays with higher priorities come first.
5722 2. Within before-strings, strings are sorted so that overlay
5723 strings from overlays with higher priorities come last.
5725 Value is analogous to strcmp. */
5728 static int
5729 compare_overlay_entries (const void *e1, const void *e2)
5731 struct overlay_entry const *entry1 = e1;
5732 struct overlay_entry const *entry2 = e2;
5733 int result;
5735 if (entry1->after_string_p != entry2->after_string_p)
5737 /* Let after-strings appear in front of before-strings if
5738 they come from different overlays. */
5739 if (EQ (entry1->overlay, entry2->overlay))
5740 result = entry1->after_string_p ? 1 : -1;
5741 else
5742 result = entry1->after_string_p ? -1 : 1;
5744 else if (entry1->priority != entry2->priority)
5746 if (entry1->after_string_p)
5747 /* After-strings sorted in order of decreasing priority. */
5748 result = entry2->priority < entry1->priority ? -1 : 1;
5749 else
5750 /* Before-strings sorted in order of increasing priority. */
5751 result = entry1->priority < entry2->priority ? -1 : 1;
5753 else
5754 result = 0;
5756 return result;
5760 /* Load the vector IT->overlay_strings with overlay strings from IT's
5761 current buffer position, or from CHARPOS if that is > 0. Set
5762 IT->n_overlays to the total number of overlay strings found.
5764 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5765 a time. On entry into load_overlay_strings,
5766 IT->current.overlay_string_index gives the number of overlay
5767 strings that have already been loaded by previous calls to this
5768 function.
5770 IT->add_overlay_start contains an additional overlay start
5771 position to consider for taking overlay strings from, if non-zero.
5772 This position comes into play when the overlay has an `invisible'
5773 property, and both before and after-strings. When we've skipped to
5774 the end of the overlay, because of its `invisible' property, we
5775 nevertheless want its before-string to appear.
5776 IT->add_overlay_start will contain the overlay start position
5777 in this case.
5779 Overlay strings are sorted so that after-string strings come in
5780 front of before-string strings. Within before and after-strings,
5781 strings are sorted by overlay priority. See also function
5782 compare_overlay_entries. */
5784 static void
5785 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5787 Lisp_Object overlay, window, str, invisible;
5788 struct Lisp_Overlay *ov;
5789 ptrdiff_t start, end;
5790 ptrdiff_t n = 0, i, j;
5791 int invis;
5792 struct overlay_entry entriesbuf[20];
5793 ptrdiff_t size = ARRAYELTS (entriesbuf);
5794 struct overlay_entry *entries = entriesbuf;
5795 USE_SAFE_ALLOCA;
5797 if (charpos <= 0)
5798 charpos = IT_CHARPOS (*it);
5800 /* Append the overlay string STRING of overlay OVERLAY to vector
5801 `entries' which has size `size' and currently contains `n'
5802 elements. AFTER_P means STRING is an after-string of
5803 OVERLAY. */
5804 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5805 do \
5807 Lisp_Object priority; \
5809 if (n == size) \
5811 struct overlay_entry *old = entries; \
5812 SAFE_NALLOCA (entries, 2, size); \
5813 memcpy (entries, old, size * sizeof *entries); \
5814 size *= 2; \
5817 entries[n].string = (STRING); \
5818 entries[n].overlay = (OVERLAY); \
5819 priority = Foverlay_get ((OVERLAY), Qpriority); \
5820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5821 entries[n].after_string_p = (AFTER_P); \
5822 ++n; \
5824 while (false)
5826 /* Process overlay before the overlay center. */
5827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5829 XSETMISC (overlay, ov);
5830 eassert (OVERLAYP (overlay));
5831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5834 if (end < charpos)
5835 break;
5837 /* Skip this overlay if it doesn't start or end at IT's current
5838 position. */
5839 if (end != charpos && start != charpos)
5840 continue;
5842 /* Skip this overlay if it doesn't apply to IT->w. */
5843 window = Foverlay_get (overlay, Qwindow);
5844 if (WINDOWP (window) && XWINDOW (window) != it->w)
5845 continue;
5847 /* If the text ``under'' the overlay is invisible, both before-
5848 and after-strings from this overlay are visible; start and
5849 end position are indistinguishable. */
5850 invisible = Foverlay_get (overlay, Qinvisible);
5851 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5853 /* If overlay has a non-empty before-string, record it. */
5854 if ((start == charpos || (end == charpos && invis != 0))
5855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5856 && SCHARS (str))
5857 RECORD_OVERLAY_STRING (overlay, str, false);
5859 /* If overlay has a non-empty after-string, record it. */
5860 if ((end == charpos || (start == charpos && invis != 0))
5861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5862 && SCHARS (str))
5863 RECORD_OVERLAY_STRING (overlay, str, true);
5866 /* Process overlays after the overlay center. */
5867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5869 XSETMISC (overlay, ov);
5870 eassert (OVERLAYP (overlay));
5871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5874 if (start > charpos)
5875 break;
5877 /* Skip this overlay if it doesn't start or end at IT's current
5878 position. */
5879 if (end != charpos && start != charpos)
5880 continue;
5882 /* Skip this overlay if it doesn't apply to IT->w. */
5883 window = Foverlay_get (overlay, Qwindow);
5884 if (WINDOWP (window) && XWINDOW (window) != it->w)
5885 continue;
5887 /* If the text ``under'' the overlay is invisible, it has a zero
5888 dimension, and both before- and after-strings apply. */
5889 invisible = Foverlay_get (overlay, Qinvisible);
5890 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5892 /* If overlay has a non-empty before-string, record it. */
5893 if ((start == charpos || (end == charpos && invis != 0))
5894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5895 && SCHARS (str))
5896 RECORD_OVERLAY_STRING (overlay, str, false);
5898 /* If overlay has a non-empty after-string, record it. */
5899 if ((end == charpos || (start == charpos && invis != 0))
5900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5901 && SCHARS (str))
5902 RECORD_OVERLAY_STRING (overlay, str, true);
5905 #undef RECORD_OVERLAY_STRING
5907 /* Sort entries. */
5908 if (n > 1)
5909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5911 /* Record number of overlay strings, and where we computed it. */
5912 it->n_overlay_strings = n;
5913 it->overlay_strings_charpos = charpos;
5915 /* IT->current.overlay_string_index is the number of overlay strings
5916 that have already been consumed by IT. Copy some of the
5917 remaining overlay strings to IT->overlay_strings. */
5918 i = 0;
5919 j = it->current.overlay_string_index;
5920 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5922 it->overlay_strings[i] = entries[j].string;
5923 it->string_overlays[i++] = entries[j++].overlay;
5926 CHECK_IT (it);
5927 SAFE_FREE ();
5931 /* Get the first chunk of overlay strings at IT's current buffer
5932 position, or at CHARPOS if that is > 0. Value is true if at
5933 least one overlay string was found. */
5935 static bool
5936 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5938 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5939 process. This fills IT->overlay_strings with strings, and sets
5940 IT->n_overlay_strings to the total number of strings to process.
5941 IT->pos.overlay_string_index has to be set temporarily to zero
5942 because load_overlay_strings needs this; it must be set to -1
5943 when no overlay strings are found because a zero value would
5944 indicate a position in the first overlay string. */
5945 it->current.overlay_string_index = 0;
5946 load_overlay_strings (it, charpos);
5948 /* If we found overlay strings, set up IT to deliver display
5949 elements from the first one. Otherwise set up IT to deliver
5950 from current_buffer. */
5951 if (it->n_overlay_strings)
5953 /* Make sure we know settings in current_buffer, so that we can
5954 restore meaningful values when we're done with the overlay
5955 strings. */
5956 if (compute_stop_p)
5957 compute_stop_pos (it);
5958 eassert (it->face_id >= 0);
5960 /* Save IT's settings. They are restored after all overlay
5961 strings have been processed. */
5962 eassert (!compute_stop_p || it->sp == 0);
5964 /* When called from handle_stop, there might be an empty display
5965 string loaded. In that case, don't bother saving it. But
5966 don't use this optimization with the bidi iterator, since we
5967 need the corresponding pop_it call to resync the bidi
5968 iterator's position with IT's position, after we are done
5969 with the overlay strings. (The corresponding call to pop_it
5970 in case of an empty display string is in
5971 next_overlay_string.) */
5972 if (!(!it->bidi_p
5973 && STRINGP (it->string) && !SCHARS (it->string)))
5974 push_it (it, NULL);
5976 /* Set up IT to deliver display elements from the first overlay
5977 string. */
5978 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5979 it->string = it->overlay_strings[0];
5980 it->from_overlay = Qnil;
5981 it->stop_charpos = 0;
5982 eassert (STRINGP (it->string));
5983 it->end_charpos = SCHARS (it->string);
5984 it->prev_stop = 0;
5985 it->base_level_stop = 0;
5986 it->multibyte_p = STRING_MULTIBYTE (it->string);
5987 it->method = GET_FROM_STRING;
5988 it->from_disp_prop_p = 0;
5989 it->cmp_it.id = -1;
5991 /* Force paragraph direction to be that of the parent
5992 buffer. */
5993 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5994 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5995 else
5996 it->paragraph_embedding = L2R;
5998 /* Set up the bidi iterator for this overlay string. */
5999 if (it->bidi_p)
6001 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
6003 it->bidi_it.string.lstring = it->string;
6004 it->bidi_it.string.s = NULL;
6005 it->bidi_it.string.schars = SCHARS (it->string);
6006 it->bidi_it.string.bufpos = pos;
6007 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6008 it->bidi_it.string.unibyte = !it->multibyte_p;
6009 it->bidi_it.w = it->w;
6010 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6012 return true;
6015 it->current.overlay_string_index = -1;
6016 return false;
6019 static bool
6020 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6022 it->string = Qnil;
6023 it->method = GET_FROM_BUFFER;
6025 get_overlay_strings_1 (it, charpos, true);
6027 CHECK_IT (it);
6029 /* Value is true if we found at least one overlay string. */
6030 return STRINGP (it->string);
6035 /***********************************************************************
6036 Saving and restoring state
6037 ***********************************************************************/
6039 /* Save current settings of IT on IT->stack. Called, for example,
6040 before setting up IT for an overlay string, to be able to restore
6041 IT's settings to what they were after the overlay string has been
6042 processed. If POSITION is non-NULL, it is the position to save on
6043 the stack instead of IT->position. */
6045 static void
6046 push_it (struct it *it, struct text_pos *position)
6048 struct iterator_stack_entry *p;
6050 eassert (it->sp < IT_STACK_SIZE);
6051 p = it->stack + it->sp;
6053 p->stop_charpos = it->stop_charpos;
6054 p->prev_stop = it->prev_stop;
6055 p->base_level_stop = it->base_level_stop;
6056 p->cmp_it = it->cmp_it;
6057 eassert (it->face_id >= 0);
6058 p->face_id = it->face_id;
6059 p->string = it->string;
6060 p->method = it->method;
6061 p->from_overlay = it->from_overlay;
6062 switch (p->method)
6064 case GET_FROM_IMAGE:
6065 p->u.image.object = it->object;
6066 p->u.image.image_id = it->image_id;
6067 p->u.image.slice = it->slice;
6068 break;
6069 case GET_FROM_STRETCH:
6070 p->u.stretch.object = it->object;
6071 break;
6072 case GET_FROM_XWIDGET:
6073 p->u.xwidget.object = it->object;
6074 break;
6075 case GET_FROM_BUFFER:
6076 case GET_FROM_DISPLAY_VECTOR:
6077 case GET_FROM_STRING:
6078 case GET_FROM_C_STRING:
6079 break;
6080 default:
6081 emacs_abort ();
6083 p->position = position ? *position : it->position;
6084 p->current = it->current;
6085 p->end_charpos = it->end_charpos;
6086 p->string_nchars = it->string_nchars;
6087 p->area = it->area;
6088 p->multibyte_p = it->multibyte_p;
6089 p->avoid_cursor_p = it->avoid_cursor_p;
6090 p->space_width = it->space_width;
6091 p->font_height = it->font_height;
6092 p->voffset = it->voffset;
6093 p->string_from_display_prop_p = it->string_from_display_prop_p;
6094 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6095 p->display_ellipsis_p = false;
6096 p->line_wrap = it->line_wrap;
6097 p->bidi_p = it->bidi_p;
6098 p->paragraph_embedding = it->paragraph_embedding;
6099 p->from_disp_prop_p = it->from_disp_prop_p;
6100 ++it->sp;
6102 /* Save the state of the bidi iterator as well. */
6103 if (it->bidi_p)
6104 bidi_push_it (&it->bidi_it);
6107 static void
6108 iterate_out_of_display_property (struct it *it)
6110 bool buffer_p = !STRINGP (it->string);
6111 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6112 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6114 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6116 /* Maybe initialize paragraph direction. If we are at the beginning
6117 of a new paragraph, next_element_from_buffer may not have a
6118 chance to do that. */
6119 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6120 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6121 /* prev_stop can be zero, so check against BEGV as well. */
6122 while (it->bidi_it.charpos >= bob
6123 && it->prev_stop <= it->bidi_it.charpos
6124 && it->bidi_it.charpos < CHARPOS (it->position)
6125 && it->bidi_it.charpos < eob)
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 /* Record the stop_pos we just crossed, for when we cross it
6128 back, maybe. */
6129 if (it->bidi_it.charpos > CHARPOS (it->position))
6130 it->prev_stop = CHARPOS (it->position);
6131 /* If we ended up not where pop_it put us, resync IT's
6132 positional members with the bidi iterator. */
6133 if (it->bidi_it.charpos != CHARPOS (it->position))
6134 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6135 if (buffer_p)
6136 it->current.pos = it->position;
6137 else
6138 it->current.string_pos = it->position;
6141 /* Restore IT's settings from IT->stack. Called, for example, when no
6142 more overlay strings must be processed, and we return to delivering
6143 display elements from a buffer, or when the end of a string from a
6144 `display' property is reached and we return to delivering display
6145 elements from an overlay string, or from a buffer. */
6147 static void
6148 pop_it (struct it *it)
6150 struct iterator_stack_entry *p;
6151 bool from_display_prop = it->from_disp_prop_p;
6152 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6154 eassert (it->sp > 0);
6155 --it->sp;
6156 p = it->stack + it->sp;
6157 it->stop_charpos = p->stop_charpos;
6158 it->prev_stop = p->prev_stop;
6159 it->base_level_stop = p->base_level_stop;
6160 it->cmp_it = p->cmp_it;
6161 it->face_id = p->face_id;
6162 it->current = p->current;
6163 it->position = p->position;
6164 it->string = p->string;
6165 it->from_overlay = p->from_overlay;
6166 if (NILP (it->string))
6167 SET_TEXT_POS (it->current.string_pos, -1, -1);
6168 it->method = p->method;
6169 switch (it->method)
6171 case GET_FROM_IMAGE:
6172 it->image_id = p->u.image.image_id;
6173 it->object = p->u.image.object;
6174 it->slice = p->u.image.slice;
6175 break;
6176 case GET_FROM_XWIDGET:
6177 it->object = p->u.xwidget.object;
6178 break;
6179 case GET_FROM_STRETCH:
6180 it->object = p->u.stretch.object;
6181 break;
6182 case GET_FROM_BUFFER:
6183 it->object = it->w->contents;
6184 break;
6185 case GET_FROM_STRING:
6187 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6189 /* Restore the face_box_p flag, since it could have been
6190 overwritten by the face of the object that we just finished
6191 displaying. */
6192 if (face)
6193 it->face_box_p = face->box != FACE_NO_BOX;
6194 it->object = it->string;
6196 break;
6197 case GET_FROM_DISPLAY_VECTOR:
6198 if (it->s)
6199 it->method = GET_FROM_C_STRING;
6200 else if (STRINGP (it->string))
6201 it->method = GET_FROM_STRING;
6202 else
6204 it->method = GET_FROM_BUFFER;
6205 it->object = it->w->contents;
6207 break;
6208 case GET_FROM_C_STRING:
6209 break;
6210 default:
6211 emacs_abort ();
6213 it->end_charpos = p->end_charpos;
6214 it->string_nchars = p->string_nchars;
6215 it->area = p->area;
6216 it->multibyte_p = p->multibyte_p;
6217 it->avoid_cursor_p = p->avoid_cursor_p;
6218 it->space_width = p->space_width;
6219 it->font_height = p->font_height;
6220 it->voffset = p->voffset;
6221 it->string_from_display_prop_p = p->string_from_display_prop_p;
6222 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6223 it->line_wrap = p->line_wrap;
6224 it->bidi_p = p->bidi_p;
6225 it->paragraph_embedding = p->paragraph_embedding;
6226 it->from_disp_prop_p = p->from_disp_prop_p;
6227 if (it->bidi_p)
6229 bidi_pop_it (&it->bidi_it);
6230 /* Bidi-iterate until we get out of the portion of text, if any,
6231 covered by a `display' text property or by an overlay with
6232 `display' property. (We cannot just jump there, because the
6233 internal coherency of the bidi iterator state can not be
6234 preserved across such jumps.) We also must determine the
6235 paragraph base direction if the overlay we just processed is
6236 at the beginning of a new paragraph. */
6237 if (from_display_prop
6238 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6239 iterate_out_of_display_property (it);
6241 eassert ((BUFFERP (it->object)
6242 && IT_CHARPOS (*it) == it->bidi_it.charpos
6243 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6244 || (STRINGP (it->object)
6245 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6246 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6247 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6249 /* If we move the iterator over text covered by a display property
6250 to a new buffer position, any info about previously seen overlays
6251 is no longer valid. */
6252 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6253 it->ignore_overlay_strings_at_pos_p = false;
6258 /***********************************************************************
6259 Moving over lines
6260 ***********************************************************************/
6262 /* Set IT's current position to the previous line start. */
6264 static void
6265 back_to_previous_line_start (struct it *it)
6267 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6269 DEC_BOTH (cp, bp);
6270 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6274 /* Move IT to the next line start.
6276 Value is true if a newline was found. Set *SKIPPED_P to true if
6277 we skipped over part of the text (as opposed to moving the iterator
6278 continuously over the text). Otherwise, don't change the value
6279 of *SKIPPED_P.
6281 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6282 iterator on the newline, if it was found.
6284 Newlines may come from buffer text, overlay strings, or strings
6285 displayed via the `display' property. That's the reason we can't
6286 simply use find_newline_no_quit.
6288 Note that this function may not skip over invisible text that is so
6289 because of text properties and immediately follows a newline. If
6290 it would, function reseat_at_next_visible_line_start, when called
6291 from set_iterator_to_next, would effectively make invisible
6292 characters following a newline part of the wrong glyph row, which
6293 leads to wrong cursor motion. */
6295 static bool
6296 forward_to_next_line_start (struct it *it, bool *skipped_p,
6297 struct bidi_it *bidi_it_prev)
6299 ptrdiff_t old_selective;
6300 bool newline_found_p = false;
6301 int n;
6302 const int MAX_NEWLINE_DISTANCE = 500;
6304 /* If already on a newline, just consume it to avoid unintended
6305 skipping over invisible text below. */
6306 if (it->what == IT_CHARACTER
6307 && it->c == '\n'
6308 && CHARPOS (it->position) == IT_CHARPOS (*it))
6310 if (it->bidi_p && bidi_it_prev)
6311 *bidi_it_prev = it->bidi_it;
6312 set_iterator_to_next (it, false);
6313 it->c = 0;
6314 return true;
6317 /* Don't handle selective display in the following. It's (a)
6318 unnecessary because it's done by the caller, and (b) leads to an
6319 infinite recursion because next_element_from_ellipsis indirectly
6320 calls this function. */
6321 old_selective = it->selective;
6322 it->selective = 0;
6324 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6325 from buffer text. */
6326 for (n = 0;
6327 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6328 n += !STRINGP (it->string))
6330 if (!get_next_display_element (it))
6331 return false;
6332 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6333 if (newline_found_p && it->bidi_p && bidi_it_prev)
6334 *bidi_it_prev = it->bidi_it;
6335 set_iterator_to_next (it, false);
6338 /* If we didn't find a newline near enough, see if we can use a
6339 short-cut. */
6340 if (!newline_found_p)
6342 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6343 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6344 1, &bytepos);
6345 Lisp_Object pos;
6347 eassert (!STRINGP (it->string));
6349 /* If there isn't any `display' property in sight, and no
6350 overlays, we can just use the position of the newline in
6351 buffer text. */
6352 if (it->stop_charpos >= limit
6353 || ((pos = Fnext_single_property_change (make_number (start),
6354 Qdisplay, Qnil,
6355 make_number (limit)),
6356 NILP (pos))
6357 && next_overlay_change (start) == ZV))
6359 if (!it->bidi_p)
6361 IT_CHARPOS (*it) = limit;
6362 IT_BYTEPOS (*it) = bytepos;
6364 else
6366 struct bidi_it bprev;
6368 /* Help bidi.c avoid expensive searches for display
6369 properties and overlays, by telling it that there are
6370 none up to `limit'. */
6371 if (it->bidi_it.disp_pos < limit)
6373 it->bidi_it.disp_pos = limit;
6374 it->bidi_it.disp_prop = 0;
6376 do {
6377 bprev = it->bidi_it;
6378 bidi_move_to_visually_next (&it->bidi_it);
6379 } while (it->bidi_it.charpos != limit);
6380 IT_CHARPOS (*it) = limit;
6381 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6382 if (bidi_it_prev)
6383 *bidi_it_prev = bprev;
6385 *skipped_p = newline_found_p = true;
6387 else
6389 while (!newline_found_p)
6391 if (!get_next_display_element (it))
6392 break;
6393 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6394 if (newline_found_p && it->bidi_p && bidi_it_prev)
6395 *bidi_it_prev = it->bidi_it;
6396 set_iterator_to_next (it, false);
6401 it->selective = old_selective;
6402 return newline_found_p;
6406 /* Set IT's current position to the previous visible line start. Skip
6407 invisible text that is so either due to text properties or due to
6408 selective display. Caution: this does not change IT->current_x and
6409 IT->hpos. */
6411 static void
6412 back_to_previous_visible_line_start (struct it *it)
6414 while (IT_CHARPOS (*it) > BEGV)
6416 back_to_previous_line_start (it);
6418 if (IT_CHARPOS (*it) <= BEGV)
6419 break;
6421 /* If selective > 0, then lines indented more than its value are
6422 invisible. */
6423 if (it->selective > 0
6424 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6425 it->selective))
6426 continue;
6428 /* Check the newline before point for invisibility. */
6430 Lisp_Object prop;
6431 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6432 Qinvisible, it->window);
6433 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6434 continue;
6437 if (IT_CHARPOS (*it) <= BEGV)
6438 break;
6441 struct it it2;
6442 void *it2data = NULL;
6443 ptrdiff_t pos;
6444 ptrdiff_t beg, end;
6445 Lisp_Object val, overlay;
6447 SAVE_IT (it2, *it, it2data);
6449 /* If newline is part of a composition, continue from start of composition */
6450 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6451 && beg < IT_CHARPOS (*it))
6452 goto replaced;
6454 /* If newline is replaced by a display property, find start of overlay
6455 or interval and continue search from that point. */
6456 pos = --IT_CHARPOS (it2);
6457 --IT_BYTEPOS (it2);
6458 it2.sp = 0;
6459 bidi_unshelve_cache (NULL, false);
6460 it2.string_from_display_prop_p = false;
6461 it2.from_disp_prop_p = false;
6462 if (handle_display_prop (&it2) == HANDLED_RETURN
6463 && !NILP (val = get_char_property_and_overlay
6464 (make_number (pos), Qdisplay, Qnil, &overlay))
6465 && (OVERLAYP (overlay)
6466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6469 RESTORE_IT (it, it, it2data);
6470 goto replaced;
6473 /* Newline is not replaced by anything -- so we are done. */
6474 RESTORE_IT (it, it, it2data);
6475 break;
6477 replaced:
6478 if (beg < BEGV)
6479 beg = BEGV;
6480 IT_CHARPOS (*it) = beg;
6481 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6485 it->continuation_lines_width = 0;
6487 eassert (IT_CHARPOS (*it) >= BEGV);
6488 eassert (IT_CHARPOS (*it) == BEGV
6489 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6490 CHECK_IT (it);
6494 /* Reseat iterator IT at the previous visible line start. Skip
6495 invisible text that is so either due to text properties or due to
6496 selective display. At the end, update IT's overlay information,
6497 face information etc. */
6499 void
6500 reseat_at_previous_visible_line_start (struct it *it)
6502 back_to_previous_visible_line_start (it);
6503 reseat (it, it->current.pos, true);
6504 CHECK_IT (it);
6508 /* Reseat iterator IT on the next visible line start in the current
6509 buffer. ON_NEWLINE_P means position IT on the newline
6510 preceding the line start. Skip over invisible text that is so
6511 because of selective display. Compute faces, overlays etc at the
6512 new position. Note that this function does not skip over text that
6513 is invisible because of text properties. */
6515 static void
6516 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6518 bool skipped_p = false;
6519 struct bidi_it bidi_it_prev;
6520 bool newline_found_p
6521 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6523 /* Skip over lines that are invisible because they are indented
6524 more than the value of IT->selective. */
6525 if (it->selective > 0)
6526 while (IT_CHARPOS (*it) < ZV
6527 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6528 it->selective))
6530 eassert (IT_BYTEPOS (*it) == BEGV
6531 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6532 newline_found_p =
6533 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6536 /* Position on the newline if that's what's requested. */
6537 if (on_newline_p && newline_found_p)
6539 if (STRINGP (it->string))
6541 if (IT_STRING_CHARPOS (*it) > 0)
6543 if (!it->bidi_p)
6545 --IT_STRING_CHARPOS (*it);
6546 --IT_STRING_BYTEPOS (*it);
6548 else
6550 /* We need to restore the bidi iterator to the state
6551 it had on the newline, and resync the IT's
6552 position with that. */
6553 it->bidi_it = bidi_it_prev;
6554 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6555 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6559 else if (IT_CHARPOS (*it) > BEGV)
6561 if (!it->bidi_p)
6563 --IT_CHARPOS (*it);
6564 --IT_BYTEPOS (*it);
6566 else
6568 /* We need to restore the bidi iterator to the state it
6569 had on the newline and resync IT with that. */
6570 it->bidi_it = bidi_it_prev;
6571 IT_CHARPOS (*it) = it->bidi_it.charpos;
6572 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6574 reseat (it, it->current.pos, false);
6577 else if (skipped_p)
6578 reseat (it, it->current.pos, false);
6580 CHECK_IT (it);
6585 /***********************************************************************
6586 Changing an iterator's position
6587 ***********************************************************************/
6589 /* Change IT's current position to POS in current_buffer.
6590 If FORCE_P, always check for text properties at the new position.
6591 Otherwise, text properties are only looked up if POS >=
6592 IT->check_charpos of a property. */
6594 static void
6595 reseat (struct it *it, struct text_pos pos, bool force_p)
6597 ptrdiff_t original_pos = IT_CHARPOS (*it);
6599 reseat_1 (it, pos, false);
6601 /* Determine where to check text properties. Avoid doing it
6602 where possible because text property lookup is very expensive. */
6603 if (force_p
6604 || CHARPOS (pos) > it->stop_charpos
6605 || CHARPOS (pos) < original_pos)
6607 if (it->bidi_p)
6609 /* For bidi iteration, we need to prime prev_stop and
6610 base_level_stop with our best estimations. */
6611 /* Implementation note: Of course, POS is not necessarily a
6612 stop position, so assigning prev_pos to it is a lie; we
6613 should have called compute_stop_backwards. However, if
6614 the current buffer does not include any R2L characters,
6615 that call would be a waste of cycles, because the
6616 iterator will never move back, and thus never cross this
6617 "fake" stop position. So we delay that backward search
6618 until the time we really need it, in next_element_from_buffer. */
6619 if (CHARPOS (pos) != it->prev_stop)
6620 it->prev_stop = CHARPOS (pos);
6621 if (CHARPOS (pos) < it->base_level_stop)
6622 it->base_level_stop = 0; /* meaning it's unknown */
6623 handle_stop (it);
6625 else
6627 handle_stop (it);
6628 it->prev_stop = it->base_level_stop = 0;
6633 CHECK_IT (it);
6637 /* Change IT's buffer position to POS. SET_STOP_P means set
6638 IT->stop_pos to POS, also. */
6640 static void
6641 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6643 /* Don't call this function when scanning a C string. */
6644 eassert (it->s == NULL);
6646 /* POS must be a reasonable value. */
6647 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6649 it->current.pos = it->position = pos;
6650 it->end_charpos = ZV;
6651 it->dpvec = NULL;
6652 it->current.dpvec_index = -1;
6653 it->current.overlay_string_index = -1;
6654 IT_STRING_CHARPOS (*it) = -1;
6655 IT_STRING_BYTEPOS (*it) = -1;
6656 it->string = Qnil;
6657 it->method = GET_FROM_BUFFER;
6658 it->object = it->w->contents;
6659 it->area = TEXT_AREA;
6660 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6661 it->sp = 0;
6662 it->string_from_display_prop_p = false;
6663 it->string_from_prefix_prop_p = false;
6665 it->from_disp_prop_p = false;
6666 it->face_before_selective_p = false;
6667 if (it->bidi_p)
6669 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6670 &it->bidi_it);
6671 bidi_unshelve_cache (NULL, false);
6672 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6673 it->bidi_it.string.s = NULL;
6674 it->bidi_it.string.lstring = Qnil;
6675 it->bidi_it.string.bufpos = 0;
6676 it->bidi_it.string.from_disp_str = false;
6677 it->bidi_it.string.unibyte = false;
6678 it->bidi_it.w = it->w;
6681 if (set_stop_p)
6683 it->stop_charpos = CHARPOS (pos);
6684 it->base_level_stop = CHARPOS (pos);
6686 /* This make the information stored in it->cmp_it invalidate. */
6687 it->cmp_it.id = -1;
6691 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6692 If S is non-null, it is a C string to iterate over. Otherwise,
6693 STRING gives a Lisp string to iterate over.
6695 If PRECISION > 0, don't return more then PRECISION number of
6696 characters from the string.
6698 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6699 characters have been returned. FIELD_WIDTH < 0 means an infinite
6700 field width.
6702 MULTIBYTE = 0 means disable processing of multibyte characters,
6703 MULTIBYTE > 0 means enable it,
6704 MULTIBYTE < 0 means use IT->multibyte_p.
6706 IT must be initialized via a prior call to init_iterator before
6707 calling this function. */
6709 static void
6710 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6711 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6712 int multibyte)
6714 /* No text property checks performed by default, but see below. */
6715 it->stop_charpos = -1;
6717 /* Set iterator position and end position. */
6718 memset (&it->current, 0, sizeof it->current);
6719 it->current.overlay_string_index = -1;
6720 it->current.dpvec_index = -1;
6721 eassert (charpos >= 0);
6723 /* If STRING is specified, use its multibyteness, otherwise use the
6724 setting of MULTIBYTE, if specified. */
6725 if (multibyte >= 0)
6726 it->multibyte_p = multibyte > 0;
6728 /* Bidirectional reordering of strings is controlled by the default
6729 value of bidi-display-reordering. Don't try to reorder while
6730 loading loadup.el, as the necessary character property tables are
6731 not yet available. */
6732 it->bidi_p =
6733 !redisplay__inhibit_bidi
6734 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6736 if (s == NULL)
6738 eassert (STRINGP (string));
6739 it->string = string;
6740 it->s = NULL;
6741 it->end_charpos = it->string_nchars = SCHARS (string);
6742 it->method = GET_FROM_STRING;
6743 it->current.string_pos = string_pos (charpos, string);
6745 if (it->bidi_p)
6747 it->bidi_it.string.lstring = string;
6748 it->bidi_it.string.s = NULL;
6749 it->bidi_it.string.schars = it->end_charpos;
6750 it->bidi_it.string.bufpos = 0;
6751 it->bidi_it.string.from_disp_str = false;
6752 it->bidi_it.string.unibyte = !it->multibyte_p;
6753 it->bidi_it.w = it->w;
6754 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6755 FRAME_WINDOW_P (it->f), &it->bidi_it);
6758 else
6760 it->s = (const unsigned char *) s;
6761 it->string = Qnil;
6763 /* Note that we use IT->current.pos, not it->current.string_pos,
6764 for displaying C strings. */
6765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6766 if (it->multibyte_p)
6768 it->current.pos = c_string_pos (charpos, s, true);
6769 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6771 else
6773 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6774 it->end_charpos = it->string_nchars = strlen (s);
6777 if (it->bidi_p)
6779 it->bidi_it.string.lstring = Qnil;
6780 it->bidi_it.string.s = (const unsigned char *) s;
6781 it->bidi_it.string.schars = it->end_charpos;
6782 it->bidi_it.string.bufpos = 0;
6783 it->bidi_it.string.from_disp_str = false;
6784 it->bidi_it.string.unibyte = !it->multibyte_p;
6785 it->bidi_it.w = it->w;
6786 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6787 &it->bidi_it);
6789 it->method = GET_FROM_C_STRING;
6792 /* PRECISION > 0 means don't return more than PRECISION characters
6793 from the string. */
6794 if (precision > 0 && it->end_charpos - charpos > precision)
6796 it->end_charpos = it->string_nchars = charpos + precision;
6797 if (it->bidi_p)
6798 it->bidi_it.string.schars = it->end_charpos;
6801 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6802 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6803 FIELD_WIDTH < 0 means infinite field width. This is useful for
6804 padding with `-' at the end of a mode line. */
6805 if (field_width < 0)
6806 field_width = DISP_INFINITY;
6807 /* Implementation note: We deliberately don't enlarge
6808 it->bidi_it.string.schars here to fit it->end_charpos, because
6809 the bidi iterator cannot produce characters out of thin air. */
6810 if (field_width > it->end_charpos - charpos)
6811 it->end_charpos = charpos + field_width;
6813 /* Use the standard display table for displaying strings. */
6814 if (DISP_TABLE_P (Vstandard_display_table))
6815 it->dp = XCHAR_TABLE (Vstandard_display_table);
6817 it->stop_charpos = charpos;
6818 it->prev_stop = charpos;
6819 it->base_level_stop = 0;
6820 if (it->bidi_p)
6822 it->bidi_it.first_elt = true;
6823 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6824 it->bidi_it.disp_pos = -1;
6826 if (s == NULL && it->multibyte_p)
6828 ptrdiff_t endpos = SCHARS (it->string);
6829 if (endpos > it->end_charpos)
6830 endpos = it->end_charpos;
6831 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6832 it->string);
6834 CHECK_IT (it);
6839 /***********************************************************************
6840 Iteration
6841 ***********************************************************************/
6843 /* Map enum it_method value to corresponding next_element_from_* function. */
6845 typedef bool (*next_element_function) (struct it *);
6847 static next_element_function const get_next_element[NUM_IT_METHODS] =
6849 next_element_from_buffer,
6850 next_element_from_display_vector,
6851 next_element_from_string,
6852 next_element_from_c_string,
6853 next_element_from_image,
6854 next_element_from_stretch,
6855 next_element_from_xwidget,
6858 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6861 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6862 (possibly with the following characters). */
6864 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6865 ((IT)->cmp_it.id >= 0 \
6866 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6867 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6868 END_CHARPOS, (IT)->w, \
6869 FACE_FROM_ID_OR_NULL ((IT)->f, \
6870 (IT)->face_id), \
6871 (IT)->string)))
6874 /* Lookup the char-table Vglyphless_char_display for character C (-1
6875 if we want information for no-font case), and return the display
6876 method symbol. By side-effect, update it->what and
6877 it->glyphless_method. This function is called from
6878 get_next_display_element for each character element, and from
6879 x_produce_glyphs when no suitable font was found. */
6881 Lisp_Object
6882 lookup_glyphless_char_display (int c, struct it *it)
6884 Lisp_Object glyphless_method = Qnil;
6886 if (CHAR_TABLE_P (Vglyphless_char_display)
6887 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6889 if (c >= 0)
6891 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6892 if (CONSP (glyphless_method))
6893 glyphless_method = FRAME_WINDOW_P (it->f)
6894 ? XCAR (glyphless_method)
6895 : XCDR (glyphless_method);
6897 else
6898 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6901 retry:
6902 if (NILP (glyphless_method))
6904 if (c >= 0)
6905 /* The default is to display the character by a proper font. */
6906 return Qnil;
6907 /* The default for the no-font case is to display an empty box. */
6908 glyphless_method = Qempty_box;
6910 if (EQ (glyphless_method, Qzero_width))
6912 if (c >= 0)
6913 return glyphless_method;
6914 /* This method can't be used for the no-font case. */
6915 glyphless_method = Qempty_box;
6917 if (EQ (glyphless_method, Qthin_space))
6918 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6919 else if (EQ (glyphless_method, Qempty_box))
6920 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6921 else if (EQ (glyphless_method, Qhex_code))
6922 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6923 else if (STRINGP (glyphless_method))
6924 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6925 else
6927 /* Invalid value. We use the default method. */
6928 glyphless_method = Qnil;
6929 goto retry;
6931 it->what = IT_GLYPHLESS;
6932 return glyphless_method;
6935 /* Merge escape glyph face and cache the result. */
6937 static struct frame *last_escape_glyph_frame = NULL;
6938 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6939 static int last_escape_glyph_merged_face_id = 0;
6941 static int
6942 merge_escape_glyph_face (struct it *it)
6944 int face_id;
6946 if (it->f == last_escape_glyph_frame
6947 && it->face_id == last_escape_glyph_face_id)
6948 face_id = last_escape_glyph_merged_face_id;
6949 else
6951 /* Merge the `escape-glyph' face into the current face. */
6952 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6953 last_escape_glyph_frame = it->f;
6954 last_escape_glyph_face_id = it->face_id;
6955 last_escape_glyph_merged_face_id = face_id;
6957 return face_id;
6960 /* Likewise for glyphless glyph face. */
6962 static struct frame *last_glyphless_glyph_frame = NULL;
6963 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6964 static int last_glyphless_glyph_merged_face_id = 0;
6967 merge_glyphless_glyph_face (struct it *it)
6969 int face_id;
6971 if (it->f == last_glyphless_glyph_frame
6972 && it->face_id == last_glyphless_glyph_face_id)
6973 face_id = last_glyphless_glyph_merged_face_id;
6974 else
6976 /* Merge the `glyphless-char' face into the current face. */
6977 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6978 last_glyphless_glyph_frame = it->f;
6979 last_glyphless_glyph_face_id = it->face_id;
6980 last_glyphless_glyph_merged_face_id = face_id;
6982 return face_id;
6985 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6986 be called before redisplaying windows, and when the frame's face
6987 cache is freed. */
6988 void
6989 forget_escape_and_glyphless_faces (void)
6991 last_escape_glyph_frame = NULL;
6992 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6993 last_glyphless_glyph_frame = NULL;
6994 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6997 /* Load IT's display element fields with information about the next
6998 display element from the current position of IT. Value is false if
6999 end of buffer (or C string) is reached. */
7001 static bool
7002 get_next_display_element (struct it *it)
7004 /* True means that we found a display element. False means that
7005 we hit the end of what we iterate over. Performance note: the
7006 function pointer `method' used here turns out to be faster than
7007 using a sequence of if-statements. */
7008 bool success_p;
7010 get_next:
7011 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7013 if (it->what == IT_CHARACTER)
7015 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7016 and only if (a) the resolved directionality of that character
7017 is R..." */
7018 /* FIXME: Do we need an exception for characters from display
7019 tables? */
7020 if (it->bidi_p && it->bidi_it.type == STRONG_R
7021 && !inhibit_bidi_mirroring)
7022 it->c = bidi_mirror_char (it->c);
7023 /* Map via display table or translate control characters.
7024 IT->c, IT->len etc. have been set to the next character by
7025 the function call above. If we have a display table, and it
7026 contains an entry for IT->c, translate it. Don't do this if
7027 IT->c itself comes from a display table, otherwise we could
7028 end up in an infinite recursion. (An alternative could be to
7029 count the recursion depth of this function and signal an
7030 error when a certain maximum depth is reached.) Is it worth
7031 it? */
7032 if (success_p && it->dpvec == NULL)
7034 Lisp_Object dv;
7035 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7036 bool nonascii_space_p = false;
7037 bool nonascii_hyphen_p = false;
7038 int c = it->c; /* This is the character to display. */
7040 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7042 eassert (SINGLE_BYTE_CHAR_P (c));
7043 if (unibyte_display_via_language_environment)
7045 c = DECODE_CHAR (unibyte, c);
7046 if (c < 0)
7047 c = BYTE8_TO_CHAR (it->c);
7049 else
7050 c = BYTE8_TO_CHAR (it->c);
7053 if (it->dp
7054 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7055 VECTORP (dv)))
7057 struct Lisp_Vector *v = XVECTOR (dv);
7059 /* Return the first character from the display table
7060 entry, if not empty. If empty, don't display the
7061 current character. */
7062 if (v->header.size)
7064 it->dpvec_char_len = it->len;
7065 it->dpvec = v->contents;
7066 it->dpend = v->contents + v->header.size;
7067 it->current.dpvec_index = 0;
7068 it->dpvec_face_id = -1;
7069 it->saved_face_id = it->face_id;
7070 it->method = GET_FROM_DISPLAY_VECTOR;
7071 it->ellipsis_p = false;
7073 else
7075 set_iterator_to_next (it, false);
7077 goto get_next;
7080 if (! NILP (lookup_glyphless_char_display (c, it)))
7082 if (it->what == IT_GLYPHLESS)
7083 goto done;
7084 /* Don't display this character. */
7085 set_iterator_to_next (it, false);
7086 goto get_next;
7089 /* If `nobreak-char-display' is non-nil, we display
7090 non-ASCII spaces and hyphens specially. */
7091 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7093 if (c == NO_BREAK_SPACE)
7094 nonascii_space_p = true;
7095 else if (c == SOFT_HYPHEN || c == HYPHEN
7096 || c == NON_BREAKING_HYPHEN)
7097 nonascii_hyphen_p = true;
7100 /* Translate control characters into `\003' or `^C' form.
7101 Control characters coming from a display table entry are
7102 currently not translated because we use IT->dpvec to hold
7103 the translation. This could easily be changed but I
7104 don't believe that it is worth doing.
7106 The characters handled by `nobreak-char-display' must be
7107 translated too.
7109 Non-printable characters and raw-byte characters are also
7110 translated to octal or hexadecimal form. */
7111 if (((c < ' ' || c == 127) /* ASCII control chars. */
7112 ? (it->area != TEXT_AREA
7113 /* In mode line, treat \n, \t like other crl chars. */
7114 || (c != '\t'
7115 && it->glyph_row
7116 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7117 || (c != '\n' && c != '\t'))
7118 : (nonascii_space_p
7119 || nonascii_hyphen_p
7120 || CHAR_BYTE8_P (c)
7121 || ! CHAR_PRINTABLE_P (c))))
7123 /* C is a control character, non-ASCII space/hyphen,
7124 raw-byte, or a non-printable character which must be
7125 displayed either as '\003' or as `^C' where the '\\'
7126 and '^' can be defined in the display table. Fill
7127 IT->ctl_chars with glyphs for what we have to
7128 display. Then, set IT->dpvec to these glyphs. */
7129 Lisp_Object gc;
7130 int ctl_len;
7131 int face_id;
7132 int lface_id = 0;
7133 int escape_glyph;
7135 /* Handle control characters with ^. */
7137 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7139 int g;
7141 g = '^'; /* default glyph for Control */
7142 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7143 if (it->dp
7144 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7146 g = GLYPH_CODE_CHAR (gc);
7147 lface_id = GLYPH_CODE_FACE (gc);
7150 face_id = (lface_id
7151 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7152 : merge_escape_glyph_face (it));
7154 XSETINT (it->ctl_chars[0], g);
7155 XSETINT (it->ctl_chars[1], c ^ 0100);
7156 ctl_len = 2;
7157 goto display_control;
7160 /* Handle non-ascii space in the mode where it only gets
7161 highlighting. */
7163 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7165 /* Merge `nobreak-space' into the current face. */
7166 face_id = merge_faces (it->f, Qnobreak_space, 0,
7167 it->face_id);
7168 XSETINT (it->ctl_chars[0], ' ');
7169 ctl_len = 1;
7170 goto display_control;
7173 /* Handle non-ascii hyphens in the mode where it only
7174 gets highlighting. */
7176 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7178 /* Merge `nobreak-space' into the current face. */
7179 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7180 it->face_id);
7181 XSETINT (it->ctl_chars[0], '-');
7182 ctl_len = 1;
7183 goto display_control;
7186 /* Handle sequences that start with the "escape glyph". */
7188 /* the default escape glyph is \. */
7189 escape_glyph = '\\';
7191 if (it->dp
7192 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7194 escape_glyph = GLYPH_CODE_CHAR (gc);
7195 lface_id = GLYPH_CODE_FACE (gc);
7198 face_id = (lface_id
7199 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7200 : merge_escape_glyph_face (it));
7202 /* Draw non-ASCII space/hyphen with escape glyph: */
7204 if (nonascii_space_p || nonascii_hyphen_p)
7206 XSETINT (it->ctl_chars[0], escape_glyph);
7207 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7208 ctl_len = 2;
7209 goto display_control;
7213 char str[10];
7214 int len, i;
7216 if (CHAR_BYTE8_P (c))
7217 /* Display \200 or \x80 instead of \17777600. */
7218 c = CHAR_TO_BYTE8 (c);
7219 const char *format_string = display_raw_bytes_as_hex
7220 ? "x%02x"
7221 : "%03o";
7222 len = sprintf (str, format_string, c + 0u);
7224 XSETINT (it->ctl_chars[0], escape_glyph);
7225 for (i = 0; i < len; i++)
7226 XSETINT (it->ctl_chars[i + 1], str[i]);
7227 ctl_len = len + 1;
7230 display_control:
7231 /* Set up IT->dpvec and return first character from it. */
7232 it->dpvec_char_len = it->len;
7233 it->dpvec = it->ctl_chars;
7234 it->dpend = it->dpvec + ctl_len;
7235 it->current.dpvec_index = 0;
7236 it->dpvec_face_id = face_id;
7237 it->saved_face_id = it->face_id;
7238 it->method = GET_FROM_DISPLAY_VECTOR;
7239 it->ellipsis_p = false;
7240 goto get_next;
7242 it->char_to_display = c;
7244 else if (success_p)
7246 it->char_to_display = it->c;
7250 #ifdef HAVE_WINDOW_SYSTEM
7251 /* Adjust face id for a multibyte character. There are no multibyte
7252 character in unibyte text. */
7253 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7254 && it->multibyte_p
7255 && success_p
7256 && FRAME_WINDOW_P (it->f))
7258 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7260 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7262 /* Automatic composition with glyph-string. */
7263 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7265 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7267 else
7269 ptrdiff_t pos = (it->s ? -1
7270 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7271 : IT_CHARPOS (*it));
7272 int c;
7274 if (it->what == IT_CHARACTER)
7275 c = it->char_to_display;
7276 else
7278 struct composition *cmp = composition_table[it->cmp_it.id];
7279 int i;
7281 c = ' ';
7282 for (i = 0; i < cmp->glyph_len; i++)
7283 /* TAB in a composition means display glyphs with
7284 padding space on the left or right. */
7285 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7286 break;
7288 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7291 #endif /* HAVE_WINDOW_SYSTEM */
7293 done:
7294 /* Is this character the last one of a run of characters with
7295 box? If yes, set IT->end_of_box_run_p to true. */
7296 if (it->face_box_p
7297 && it->s == NULL)
7299 if (it->method == GET_FROM_STRING && it->sp)
7301 int face_id = underlying_face_id (it);
7302 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7304 if (face)
7306 if (face->box == FACE_NO_BOX)
7308 /* If the box comes from face properties in a
7309 display string, check faces in that string. */
7310 int string_face_id = face_after_it_pos (it);
7311 it->end_of_box_run_p
7312 = (FACE_FROM_ID (it->f, string_face_id)->box
7313 == FACE_NO_BOX);
7315 /* Otherwise, the box comes from the underlying face.
7316 If this is the last string character displayed, check
7317 the next buffer location. */
7318 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7319 /* n_overlay_strings is unreliable unless
7320 overlay_string_index is non-negative. */
7321 && ((it->current.overlay_string_index >= 0
7322 && (it->current.overlay_string_index
7323 == it->n_overlay_strings - 1))
7324 /* A string from display property. */
7325 || it->from_disp_prop_p))
7327 ptrdiff_t ignore;
7328 int next_face_id;
7329 bool text_from_string = false;
7330 /* Normally, the next buffer location is stored in
7331 IT->current.pos... */
7332 struct text_pos pos = it->current.pos;
7334 /* ...but for a string from a display property, the
7335 next buffer position is stored in the 'position'
7336 member of the iteration stack slot below the
7337 current one, see handle_single_display_spec. By
7338 contrast, it->current.pos was not yet updated to
7339 point to that buffer position; that will happen
7340 in pop_it, after we finish displaying the current
7341 string. Note that we already checked above that
7342 it->sp is positive, so subtracting one from it is
7343 safe. */
7344 if (it->from_disp_prop_p)
7346 int stackp = it->sp - 1;
7348 /* Find the stack level with data from buffer. */
7349 while (stackp >= 0
7350 && STRINGP ((it->stack + stackp)->string))
7351 stackp--;
7352 if (stackp < 0)
7354 /* If no stack slot was found for iterating
7355 a buffer, we are displaying text from a
7356 string, most probably the mode line or
7357 the header line, and that string has a
7358 display string on some of its
7359 characters. */
7360 text_from_string = true;
7361 pos = it->stack[it->sp - 1].position;
7363 else
7364 pos = (it->stack + stackp)->position;
7366 else
7367 INC_TEXT_POS (pos, it->multibyte_p);
7369 if (text_from_string)
7371 Lisp_Object base_string = it->stack[it->sp - 1].string;
7373 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7374 it->end_of_box_run_p = true;
7375 else
7377 next_face_id
7378 = face_at_string_position (it->w, base_string,
7379 CHARPOS (pos), 0,
7380 &ignore, face_id, false);
7381 it->end_of_box_run_p
7382 = (FACE_FROM_ID (it->f, next_face_id)->box
7383 == FACE_NO_BOX);
7386 else if (CHARPOS (pos) >= ZV)
7387 it->end_of_box_run_p = true;
7388 else
7390 next_face_id =
7391 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7392 CHARPOS (pos)
7393 + TEXT_PROP_DISTANCE_LIMIT,
7394 false, -1);
7395 it->end_of_box_run_p
7396 = (FACE_FROM_ID (it->f, next_face_id)->box
7397 == FACE_NO_BOX);
7402 /* next_element_from_display_vector sets this flag according to
7403 faces of the display vector glyphs, see there. */
7404 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7406 int face_id = face_after_it_pos (it);
7407 it->end_of_box_run_p
7408 = (face_id != it->face_id
7409 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7412 /* If we reached the end of the object we've been iterating (e.g., a
7413 display string or an overlay string), and there's something on
7414 IT->stack, proceed with what's on the stack. It doesn't make
7415 sense to return false if there's unprocessed stuff on the stack,
7416 because otherwise that stuff will never be displayed. */
7417 if (!success_p && it->sp > 0)
7419 set_iterator_to_next (it, false);
7420 success_p = get_next_display_element (it);
7423 /* Value is false if end of buffer or string reached. */
7424 return success_p;
7428 /* Move IT to the next display element.
7430 RESEAT_P means if called on a newline in buffer text,
7431 skip to the next visible line start.
7433 Functions get_next_display_element and set_iterator_to_next are
7434 separate because I find this arrangement easier to handle than a
7435 get_next_display_element function that also increments IT's
7436 position. The way it is we can first look at an iterator's current
7437 display element, decide whether it fits on a line, and if it does,
7438 increment the iterator position. The other way around we probably
7439 would either need a flag indicating whether the iterator has to be
7440 incremented the next time, or we would have to implement a
7441 decrement position function which would not be easy to write. */
7443 void
7444 set_iterator_to_next (struct it *it, bool reseat_p)
7446 /* Reset flags indicating start and end of a sequence of characters
7447 with box. Reset them at the start of this function because
7448 moving the iterator to a new position might set them. */
7449 it->start_of_box_run_p = it->end_of_box_run_p = false;
7451 switch (it->method)
7453 case GET_FROM_BUFFER:
7454 /* The current display element of IT is a character from
7455 current_buffer. Advance in the buffer, and maybe skip over
7456 invisible lines that are so because of selective display. */
7457 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7458 reseat_at_next_visible_line_start (it, false);
7459 else if (it->cmp_it.id >= 0)
7461 /* We are currently getting glyphs from a composition. */
7462 if (! it->bidi_p)
7464 IT_CHARPOS (*it) += it->cmp_it.nchars;
7465 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7467 else
7469 int i;
7471 /* Update IT's char/byte positions to point to the first
7472 character of the next grapheme cluster, or to the
7473 character visually after the current composition. */
7474 for (i = 0; i < it->cmp_it.nchars; i++)
7475 bidi_move_to_visually_next (&it->bidi_it);
7476 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7477 IT_CHARPOS (*it) = it->bidi_it.charpos;
7480 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7481 && it->cmp_it.to < it->cmp_it.nglyphs)
7483 /* Composition created while scanning forward. Proceed
7484 to the next grapheme cluster. */
7485 it->cmp_it.from = it->cmp_it.to;
7487 else if ((it->bidi_p && it->cmp_it.reversed_p)
7488 && it->cmp_it.from > 0)
7490 /* Composition created while scanning backward. Proceed
7491 to the previous grapheme cluster. */
7492 it->cmp_it.to = it->cmp_it.from;
7494 else
7496 /* No more grapheme clusters in this composition.
7497 Find the next stop position. */
7498 ptrdiff_t stop = it->end_charpos;
7500 if (it->bidi_it.scan_dir < 0)
7501 /* Now we are scanning backward and don't know
7502 where to stop. */
7503 stop = -1;
7504 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7505 IT_BYTEPOS (*it), stop, Qnil);
7508 else
7510 eassert (it->len != 0);
7512 if (!it->bidi_p)
7514 IT_BYTEPOS (*it) += it->len;
7515 IT_CHARPOS (*it) += 1;
7517 else
7519 int prev_scan_dir = it->bidi_it.scan_dir;
7520 /* If this is a new paragraph, determine its base
7521 direction (a.k.a. its base embedding level). */
7522 if (it->bidi_it.new_paragraph)
7523 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7524 false);
7525 bidi_move_to_visually_next (&it->bidi_it);
7526 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7527 IT_CHARPOS (*it) = it->bidi_it.charpos;
7528 if (prev_scan_dir != it->bidi_it.scan_dir)
7530 /* As the scan direction was changed, we must
7531 re-compute the stop position for composition. */
7532 ptrdiff_t stop = it->end_charpos;
7533 if (it->bidi_it.scan_dir < 0)
7534 stop = -1;
7535 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7536 IT_BYTEPOS (*it), stop, Qnil);
7539 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7541 break;
7543 case GET_FROM_C_STRING:
7544 /* Current display element of IT is from a C string. */
7545 if (!it->bidi_p
7546 /* If the string position is beyond string's end, it means
7547 next_element_from_c_string is padding the string with
7548 blanks, in which case we bypass the bidi iterator,
7549 because it cannot deal with such virtual characters. */
7550 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7552 IT_BYTEPOS (*it) += it->len;
7553 IT_CHARPOS (*it) += 1;
7555 else
7557 bidi_move_to_visually_next (&it->bidi_it);
7558 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7559 IT_CHARPOS (*it) = it->bidi_it.charpos;
7561 break;
7563 case GET_FROM_DISPLAY_VECTOR:
7564 /* Current display element of IT is from a display table entry.
7565 Advance in the display table definition. Reset it to null if
7566 end reached, and continue with characters from buffers/
7567 strings. */
7568 ++it->current.dpvec_index;
7570 /* Restore face of the iterator to what they were before the
7571 display vector entry (these entries may contain faces). */
7572 it->face_id = it->saved_face_id;
7574 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7576 bool recheck_faces = it->ellipsis_p;
7578 if (it->s)
7579 it->method = GET_FROM_C_STRING;
7580 else if (STRINGP (it->string))
7581 it->method = GET_FROM_STRING;
7582 else
7584 it->method = GET_FROM_BUFFER;
7585 it->object = it->w->contents;
7588 it->dpvec = NULL;
7589 it->current.dpvec_index = -1;
7591 /* Skip over characters which were displayed via IT->dpvec. */
7592 if (it->dpvec_char_len < 0)
7593 reseat_at_next_visible_line_start (it, true);
7594 else if (it->dpvec_char_len > 0)
7596 it->len = it->dpvec_char_len;
7597 set_iterator_to_next (it, reseat_p);
7600 /* Maybe recheck faces after display vector. */
7601 if (recheck_faces)
7603 if (it->method == GET_FROM_STRING)
7604 it->stop_charpos = IT_STRING_CHARPOS (*it);
7605 else
7606 it->stop_charpos = IT_CHARPOS (*it);
7609 break;
7611 case GET_FROM_STRING:
7612 /* Current display element is a character from a Lisp string. */
7613 eassert (it->s == NULL && STRINGP (it->string));
7614 /* Don't advance past string end. These conditions are true
7615 when set_iterator_to_next is called at the end of
7616 get_next_display_element, in which case the Lisp string is
7617 already exhausted, and all we want is pop the iterator
7618 stack. */
7619 if (it->current.overlay_string_index >= 0)
7621 /* This is an overlay string, so there's no padding with
7622 spaces, and the number of characters in the string is
7623 where the string ends. */
7624 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7625 goto consider_string_end;
7627 else
7629 /* Not an overlay string. There could be padding, so test
7630 against it->end_charpos. */
7631 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7632 goto consider_string_end;
7634 if (it->cmp_it.id >= 0)
7636 /* We are delivering display elements from a composition.
7637 Update the string position past the grapheme cluster
7638 we've just processed. */
7639 if (! it->bidi_p)
7641 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7642 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7644 else
7646 int i;
7648 for (i = 0; i < it->cmp_it.nchars; i++)
7649 bidi_move_to_visually_next (&it->bidi_it);
7650 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7651 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7654 /* Did we exhaust all the grapheme clusters of this
7655 composition? */
7656 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7657 && (it->cmp_it.to < it->cmp_it.nglyphs))
7659 /* Not all the grapheme clusters were processed yet;
7660 advance to the next cluster. */
7661 it->cmp_it.from = it->cmp_it.to;
7663 else if ((it->bidi_p && it->cmp_it.reversed_p)
7664 && it->cmp_it.from > 0)
7666 /* Likewise: advance to the next cluster, but going in
7667 the reverse direction. */
7668 it->cmp_it.to = it->cmp_it.from;
7670 else
7672 /* This composition was fully processed; find the next
7673 candidate place for checking for composed
7674 characters. */
7675 /* Always limit string searches to the string length;
7676 any padding spaces are not part of the string, and
7677 there cannot be any compositions in that padding. */
7678 ptrdiff_t stop = SCHARS (it->string);
7680 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7681 stop = -1;
7682 else if (it->end_charpos < stop)
7684 /* Cf. PRECISION in reseat_to_string: we might be
7685 limited in how many of the string characters we
7686 need to deliver. */
7687 stop = it->end_charpos;
7689 composition_compute_stop_pos (&it->cmp_it,
7690 IT_STRING_CHARPOS (*it),
7691 IT_STRING_BYTEPOS (*it), stop,
7692 it->string);
7695 else
7697 if (!it->bidi_p
7698 /* If the string position is beyond string's end, it
7699 means next_element_from_string is padding the string
7700 with blanks, in which case we bypass the bidi
7701 iterator, because it cannot deal with such virtual
7702 characters. */
7703 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7705 IT_STRING_BYTEPOS (*it) += it->len;
7706 IT_STRING_CHARPOS (*it) += 1;
7708 else
7710 int prev_scan_dir = it->bidi_it.scan_dir;
7712 bidi_move_to_visually_next (&it->bidi_it);
7713 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7714 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7715 /* If the scan direction changes, we may need to update
7716 the place where to check for composed characters. */
7717 if (prev_scan_dir != it->bidi_it.scan_dir)
7719 ptrdiff_t stop = SCHARS (it->string);
7721 if (it->bidi_it.scan_dir < 0)
7722 stop = -1;
7723 else if (it->end_charpos < stop)
7724 stop = it->end_charpos;
7726 composition_compute_stop_pos (&it->cmp_it,
7727 IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it), stop,
7729 it->string);
7734 consider_string_end:
7736 if (it->current.overlay_string_index >= 0)
7738 /* IT->string is an overlay string. Advance to the
7739 next, if there is one. */
7740 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7742 it->ellipsis_p = false;
7743 next_overlay_string (it);
7744 if (it->ellipsis_p)
7745 setup_for_ellipsis (it, 0);
7748 else
7750 /* IT->string is not an overlay string. If we reached
7751 its end, and there is something on IT->stack, proceed
7752 with what is on the stack. This can be either another
7753 string, this time an overlay string, or a buffer. */
7754 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7755 && it->sp > 0)
7757 pop_it (it);
7758 if (it->method == GET_FROM_STRING)
7759 goto consider_string_end;
7762 break;
7764 case GET_FROM_IMAGE:
7765 case GET_FROM_STRETCH:
7766 case GET_FROM_XWIDGET:
7768 /* The position etc with which we have to proceed are on
7769 the stack. The position may be at the end of a string,
7770 if the `display' property takes up the whole string. */
7771 eassert (it->sp > 0);
7772 pop_it (it);
7773 if (it->method == GET_FROM_STRING)
7774 goto consider_string_end;
7775 break;
7777 default:
7778 /* There are no other methods defined, so this should be a bug. */
7779 emacs_abort ();
7782 eassert (it->method != GET_FROM_STRING
7783 || (STRINGP (it->string)
7784 && IT_STRING_CHARPOS (*it) >= 0));
7787 /* Load IT's display element fields with information about the next
7788 display element which comes from a display table entry or from the
7789 result of translating a control character to one of the forms `^C'
7790 or `\003'.
7792 IT->dpvec holds the glyphs to return as characters.
7793 IT->saved_face_id holds the face id before the display vector--it
7794 is restored into IT->face_id in set_iterator_to_next. */
7796 static bool
7797 next_element_from_display_vector (struct it *it)
7799 Lisp_Object gc;
7800 int prev_face_id = it->face_id;
7801 int next_face_id;
7803 /* Precondition. */
7804 eassert (it->dpvec && it->current.dpvec_index >= 0);
7806 it->face_id = it->saved_face_id;
7808 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7809 That seemed totally bogus - so I changed it... */
7810 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7811 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7813 struct face *this_face, *prev_face, *next_face;
7815 it->c = GLYPH_CODE_CHAR (gc);
7816 it->len = CHAR_BYTES (it->c);
7818 /* The entry may contain a face id to use. Such a face id is
7819 the id of a Lisp face, not a realized face. A face id of
7820 zero means no face is specified. */
7821 if (it->dpvec_face_id >= 0)
7822 it->face_id = it->dpvec_face_id;
7823 else
7825 int lface_id = GLYPH_CODE_FACE (gc);
7826 if (lface_id > 0)
7827 it->face_id = merge_faces (it->f, Qt, lface_id,
7828 it->saved_face_id);
7831 /* Glyphs in the display vector could have the box face, so we
7832 need to set the related flags in the iterator, as
7833 appropriate. */
7834 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7835 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7837 /* Is this character the first character of a box-face run? */
7838 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7839 && (!prev_face
7840 || prev_face->box == FACE_NO_BOX));
7842 /* For the last character of the box-face run, we need to look
7843 either at the next glyph from the display vector, or at the
7844 face we saw before the display vector. */
7845 next_face_id = it->saved_face_id;
7846 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7848 if (it->dpvec_face_id >= 0)
7849 next_face_id = it->dpvec_face_id;
7850 else
7852 int lface_id =
7853 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7855 if (lface_id > 0)
7856 next_face_id = merge_faces (it->f, Qt, lface_id,
7857 it->saved_face_id);
7860 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7861 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7862 && (!next_face
7863 || next_face->box == FACE_NO_BOX));
7864 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7866 else
7867 /* Display table entry is invalid. Return a space. */
7868 it->c = ' ', it->len = 1;
7870 /* Don't change position and object of the iterator here. They are
7871 still the values of the character that had this display table
7872 entry or was translated, and that's what we want. */
7873 it->what = IT_CHARACTER;
7874 return true;
7877 /* Get the first element of string/buffer in the visual order, after
7878 being reseated to a new position in a string or a buffer. */
7879 static void
7880 get_visually_first_element (struct it *it)
7882 bool string_p = STRINGP (it->string) || it->s;
7883 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7884 ptrdiff_t bob = (string_p ? 0 : BEGV);
7886 if (STRINGP (it->string))
7888 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7889 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7891 else
7893 it->bidi_it.charpos = IT_CHARPOS (*it);
7894 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7897 if (it->bidi_it.charpos == eob)
7899 /* Nothing to do, but reset the FIRST_ELT flag, like
7900 bidi_paragraph_init does, because we are not going to
7901 call it. */
7902 it->bidi_it.first_elt = false;
7904 else if (it->bidi_it.charpos == bob
7905 || (!string_p
7906 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7907 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7909 /* If we are at the beginning of a line/string, we can produce
7910 the next element right away. */
7911 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7912 bidi_move_to_visually_next (&it->bidi_it);
7914 else
7916 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7918 /* We need to prime the bidi iterator starting at the line's or
7919 string's beginning, before we will be able to produce the
7920 next element. */
7921 if (string_p)
7922 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7923 else
7924 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7925 IT_BYTEPOS (*it), -1,
7926 &it->bidi_it.bytepos);
7927 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7930 /* Now return to buffer/string position where we were asked
7931 to get the next display element, and produce that. */
7932 bidi_move_to_visually_next (&it->bidi_it);
7934 while (it->bidi_it.bytepos != orig_bytepos
7935 && it->bidi_it.charpos < eob);
7938 /* Adjust IT's position information to where we ended up. */
7939 if (STRINGP (it->string))
7941 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7942 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7944 else
7946 IT_CHARPOS (*it) = it->bidi_it.charpos;
7947 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7950 if (STRINGP (it->string) || !it->s)
7952 ptrdiff_t stop, charpos, bytepos;
7954 if (STRINGP (it->string))
7956 eassert (!it->s);
7957 stop = SCHARS (it->string);
7958 if (stop > it->end_charpos)
7959 stop = it->end_charpos;
7960 charpos = IT_STRING_CHARPOS (*it);
7961 bytepos = IT_STRING_BYTEPOS (*it);
7963 else
7965 stop = it->end_charpos;
7966 charpos = IT_CHARPOS (*it);
7967 bytepos = IT_BYTEPOS (*it);
7969 if (it->bidi_it.scan_dir < 0)
7970 stop = -1;
7971 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7972 it->string);
7976 /* Load IT with the next display element from Lisp string IT->string.
7977 IT->current.string_pos is the current position within the string.
7978 If IT->current.overlay_string_index >= 0, the Lisp string is an
7979 overlay string. */
7981 static bool
7982 next_element_from_string (struct it *it)
7984 struct text_pos position;
7986 eassert (STRINGP (it->string));
7987 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7988 eassert (IT_STRING_CHARPOS (*it) >= 0);
7989 position = it->current.string_pos;
7991 /* With bidi reordering, the character to display might not be the
7992 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7993 that we were reseat()ed to a new string, whose paragraph
7994 direction is not known. */
7995 if (it->bidi_p && it->bidi_it.first_elt)
7997 get_visually_first_element (it);
7998 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
8001 /* Time to check for invisible text? */
8002 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
8004 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
8006 if (!(!it->bidi_p
8007 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8008 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
8010 /* With bidi non-linear iteration, we could find
8011 ourselves far beyond the last computed stop_charpos,
8012 with several other stop positions in between that we
8013 missed. Scan them all now, in buffer's logical
8014 order, until we find and handle the last stop_charpos
8015 that precedes our current position. */
8016 handle_stop_backwards (it, it->stop_charpos);
8017 return GET_NEXT_DISPLAY_ELEMENT (it);
8019 else
8021 if (it->bidi_p)
8023 /* Take note of the stop position we just moved
8024 across, for when we will move back across it. */
8025 it->prev_stop = it->stop_charpos;
8026 /* If we are at base paragraph embedding level, take
8027 note of the last stop position seen at this
8028 level. */
8029 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8030 it->base_level_stop = it->stop_charpos;
8032 handle_stop (it);
8034 /* Since a handler may have changed IT->method, we must
8035 recurse here. */
8036 return GET_NEXT_DISPLAY_ELEMENT (it);
8039 else if (it->bidi_p
8040 /* If we are before prev_stop, we may have overstepped
8041 on our way backwards a stop_pos, and if so, we need
8042 to handle that stop_pos. */
8043 && IT_STRING_CHARPOS (*it) < it->prev_stop
8044 /* We can sometimes back up for reasons that have nothing
8045 to do with bidi reordering. E.g., compositions. The
8046 code below is only needed when we are above the base
8047 embedding level, so test for that explicitly. */
8048 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 /* If we lost track of base_level_stop, we have no better
8051 place for handle_stop_backwards to start from than string
8052 beginning. This happens, e.g., when we were reseated to
8053 the previous screenful of text by vertical-motion. */
8054 if (it->base_level_stop <= 0
8055 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8056 it->base_level_stop = 0;
8057 handle_stop_backwards (it, it->base_level_stop);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 if (it->current.overlay_string_index >= 0)
8064 /* Get the next character from an overlay string. In overlay
8065 strings, there is no field width or padding with spaces to
8066 do. */
8067 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8069 it->what = IT_EOB;
8070 return false;
8072 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8073 IT_STRING_BYTEPOS (*it),
8074 it->bidi_it.scan_dir < 0
8075 ? -1
8076 : SCHARS (it->string))
8077 && next_element_from_composition (it))
8079 return true;
8081 else if (STRING_MULTIBYTE (it->string))
8083 const unsigned char *s = (SDATA (it->string)
8084 + IT_STRING_BYTEPOS (*it));
8085 it->c = string_char_and_length (s, &it->len);
8087 else
8089 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8090 it->len = 1;
8093 else
8095 /* Get the next character from a Lisp string that is not an
8096 overlay string. Such strings come from the mode line, for
8097 example. We may have to pad with spaces, or truncate the
8098 string. See also next_element_from_c_string. */
8099 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8101 it->what = IT_EOB;
8102 return false;
8104 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8106 /* Pad with spaces. */
8107 it->c = ' ', it->len = 1;
8108 CHARPOS (position) = BYTEPOS (position) = -1;
8110 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8111 IT_STRING_BYTEPOS (*it),
8112 it->bidi_it.scan_dir < 0
8113 ? -1
8114 : it->string_nchars)
8115 && next_element_from_composition (it))
8117 return true;
8119 else if (STRING_MULTIBYTE (it->string))
8121 const unsigned char *s = (SDATA (it->string)
8122 + IT_STRING_BYTEPOS (*it));
8123 it->c = string_char_and_length (s, &it->len);
8125 else
8127 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8128 it->len = 1;
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->string;
8135 it->position = position;
8136 return true;
8140 /* Load IT with next display element from C string IT->s.
8141 IT->string_nchars is the maximum number of characters to return
8142 from the string. IT->end_charpos may be greater than
8143 IT->string_nchars when this function is called, in which case we
8144 may have to return padding spaces. Value is false if end of string
8145 reached, including padding spaces. */
8147 static bool
8148 next_element_from_c_string (struct it *it)
8150 bool success_p = true;
8152 eassert (it->s);
8153 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8154 it->what = IT_CHARACTER;
8155 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8156 it->object = make_number (0);
8158 /* With bidi reordering, the character to display might not be the
8159 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8160 we were reseated to a new string, whose paragraph direction is
8161 not known. */
8162 if (it->bidi_p && it->bidi_it.first_elt)
8163 get_visually_first_element (it);
8165 /* IT's position can be greater than IT->string_nchars in case a
8166 field width or precision has been specified when the iterator was
8167 initialized. */
8168 if (IT_CHARPOS (*it) >= it->end_charpos)
8170 /* End of the game. */
8171 it->what = IT_EOB;
8172 success_p = false;
8174 else if (IT_CHARPOS (*it) >= it->string_nchars)
8176 /* Pad with spaces. */
8177 it->c = ' ', it->len = 1;
8178 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8180 else if (it->multibyte_p)
8181 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8182 else
8183 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8185 return success_p;
8189 /* Set up IT to return characters from an ellipsis, if appropriate.
8190 The definition of the ellipsis glyphs may come from a display table
8191 entry. This function fills IT with the first glyph from the
8192 ellipsis if an ellipsis is to be displayed. */
8194 static bool
8195 next_element_from_ellipsis (struct it *it)
8197 if (it->selective_display_ellipsis_p)
8198 setup_for_ellipsis (it, it->len);
8199 else
8201 /* The face at the current position may be different from the
8202 face we find after the invisible text. Remember what it
8203 was in IT->saved_face_id, and signal that it's there by
8204 setting face_before_selective_p. */
8205 it->saved_face_id = it->face_id;
8206 it->method = GET_FROM_BUFFER;
8207 it->object = it->w->contents;
8208 reseat_at_next_visible_line_start (it, true);
8209 it->face_before_selective_p = true;
8212 return GET_NEXT_DISPLAY_ELEMENT (it);
8216 /* Deliver an image display element. The iterator IT is already
8217 filled with image information (done in handle_display_prop). Value
8218 is always true. */
8221 static bool
8222 next_element_from_image (struct it *it)
8224 it->what = IT_IMAGE;
8225 return true;
8228 static bool
8229 next_element_from_xwidget (struct it *it)
8231 it->what = IT_XWIDGET;
8232 return true;
8236 /* Fill iterator IT with next display element from a stretch glyph
8237 property. IT->object is the value of the text property. Value is
8238 always true. */
8240 static bool
8241 next_element_from_stretch (struct it *it)
8243 it->what = IT_STRETCH;
8244 return true;
8247 /* Scan backwards from IT's current position until we find a stop
8248 position, or until BEGV. This is called when we find ourself
8249 before both the last known prev_stop and base_level_stop while
8250 reordering bidirectional text. */
8252 static void
8253 compute_stop_pos_backwards (struct it *it)
8255 const int SCAN_BACK_LIMIT = 1000;
8256 struct text_pos pos;
8257 struct display_pos save_current = it->current;
8258 struct text_pos save_position = it->position;
8259 ptrdiff_t charpos = IT_CHARPOS (*it);
8260 ptrdiff_t where_we_are = charpos;
8261 ptrdiff_t save_stop_pos = it->stop_charpos;
8262 ptrdiff_t save_end_pos = it->end_charpos;
8264 eassert (NILP (it->string) && !it->s);
8265 eassert (it->bidi_p);
8266 it->bidi_p = false;
8269 it->end_charpos = min (charpos + 1, ZV);
8270 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8271 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8272 reseat_1 (it, pos, false);
8273 compute_stop_pos (it);
8274 /* We must advance forward, right? */
8275 if (it->stop_charpos <= charpos)
8276 emacs_abort ();
8278 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8280 if (it->stop_charpos <= where_we_are)
8281 it->prev_stop = it->stop_charpos;
8282 else
8283 it->prev_stop = BEGV;
8284 it->bidi_p = true;
8285 it->current = save_current;
8286 it->position = save_position;
8287 it->stop_charpos = save_stop_pos;
8288 it->end_charpos = save_end_pos;
8291 /* Scan forward from CHARPOS in the current buffer/string, until we
8292 find a stop position > current IT's position. Then handle the stop
8293 position before that. This is called when we bump into a stop
8294 position while reordering bidirectional text. CHARPOS should be
8295 the last previously processed stop_pos (or BEGV/0, if none were
8296 processed yet) whose position is less that IT's current
8297 position. */
8299 static void
8300 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8302 bool bufp = !STRINGP (it->string);
8303 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8304 struct display_pos save_current = it->current;
8305 struct text_pos save_position = it->position;
8306 struct text_pos pos1;
8307 ptrdiff_t next_stop;
8309 /* Scan in strict logical order. */
8310 eassert (it->bidi_p);
8311 it->bidi_p = false;
8314 it->prev_stop = charpos;
8315 if (bufp)
8317 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8318 reseat_1 (it, pos1, false);
8320 else
8321 it->current.string_pos = string_pos (charpos, it->string);
8322 compute_stop_pos (it);
8323 /* We must advance forward, right? */
8324 if (it->stop_charpos <= it->prev_stop)
8325 emacs_abort ();
8326 charpos = it->stop_charpos;
8328 while (charpos <= where_we_are);
8330 it->bidi_p = true;
8331 it->current = save_current;
8332 it->position = save_position;
8333 next_stop = it->stop_charpos;
8334 it->stop_charpos = it->prev_stop;
8335 handle_stop (it);
8336 it->stop_charpos = next_stop;
8339 /* Load IT with the next display element from current_buffer. Value
8340 is false if end of buffer reached. IT->stop_charpos is the next
8341 position at which to stop and check for text properties or buffer
8342 end. */
8344 static bool
8345 next_element_from_buffer (struct it *it)
8347 bool success_p = true;
8349 eassert (IT_CHARPOS (*it) >= BEGV);
8350 eassert (NILP (it->string) && !it->s);
8351 eassert (!it->bidi_p
8352 || (EQ (it->bidi_it.string.lstring, Qnil)
8353 && it->bidi_it.string.s == NULL));
8355 /* With bidi reordering, the character to display might not be the
8356 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8357 we were reseat()ed to a new buffer position, which is potentially
8358 a different paragraph. */
8359 if (it->bidi_p && it->bidi_it.first_elt)
8361 get_visually_first_element (it);
8362 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8365 if (IT_CHARPOS (*it) >= it->stop_charpos)
8367 if (IT_CHARPOS (*it) >= it->end_charpos)
8369 bool overlay_strings_follow_p;
8371 /* End of the game, except when overlay strings follow that
8372 haven't been returned yet. */
8373 if (it->overlay_strings_at_end_processed_p)
8374 overlay_strings_follow_p = false;
8375 else
8377 it->overlay_strings_at_end_processed_p = true;
8378 overlay_strings_follow_p = get_overlay_strings (it, 0);
8381 if (overlay_strings_follow_p)
8382 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8383 else
8385 it->what = IT_EOB;
8386 it->position = it->current.pos;
8387 success_p = false;
8390 else if (!(!it->bidi_p
8391 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8392 || IT_CHARPOS (*it) == it->stop_charpos))
8394 /* With bidi non-linear iteration, we could find ourselves
8395 far beyond the last computed stop_charpos, with several
8396 other stop positions in between that we missed. Scan
8397 them all now, in buffer's logical order, until we find
8398 and handle the last stop_charpos that precedes our
8399 current position. */
8400 handle_stop_backwards (it, it->stop_charpos);
8401 it->ignore_overlay_strings_at_pos_p = false;
8402 return GET_NEXT_DISPLAY_ELEMENT (it);
8404 else
8406 if (it->bidi_p)
8408 /* Take note of the stop position we just moved across,
8409 for when we will move back across it. */
8410 it->prev_stop = it->stop_charpos;
8411 /* If we are at base paragraph embedding level, take
8412 note of the last stop position seen at this
8413 level. */
8414 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8415 it->base_level_stop = it->stop_charpos;
8417 handle_stop (it);
8418 it->ignore_overlay_strings_at_pos_p = false;
8419 return GET_NEXT_DISPLAY_ELEMENT (it);
8422 else if (it->bidi_p
8423 /* If we are before prev_stop, we may have overstepped on
8424 our way backwards a stop_pos, and if so, we need to
8425 handle that stop_pos. */
8426 && IT_CHARPOS (*it) < it->prev_stop
8427 /* We can sometimes back up for reasons that have nothing
8428 to do with bidi reordering. E.g., compositions. The
8429 code below is only needed when we are above the base
8430 embedding level, so test for that explicitly. */
8431 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8433 if (it->base_level_stop <= 0
8434 || IT_CHARPOS (*it) < it->base_level_stop)
8436 /* If we lost track of base_level_stop, we need to find
8437 prev_stop by looking backwards. This happens, e.g., when
8438 we were reseated to the previous screenful of text by
8439 vertical-motion. */
8440 it->base_level_stop = BEGV;
8441 compute_stop_pos_backwards (it);
8442 handle_stop_backwards (it, it->prev_stop);
8444 else
8445 handle_stop_backwards (it, it->base_level_stop);
8446 it->ignore_overlay_strings_at_pos_p = false;
8447 return GET_NEXT_DISPLAY_ELEMENT (it);
8449 else
8451 /* No face changes, overlays etc. in sight, so just return a
8452 character from current_buffer. */
8453 unsigned char *p;
8454 ptrdiff_t stop;
8456 /* We moved to the next buffer position, so any info about
8457 previously seen overlays is no longer valid. */
8458 it->ignore_overlay_strings_at_pos_p = false;
8460 /* Maybe run the redisplay end trigger hook. Performance note:
8461 This doesn't seem to cost measurable time. */
8462 if (it->redisplay_end_trigger_charpos
8463 && it->glyph_row
8464 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8465 run_redisplay_end_trigger_hook (it);
8467 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8468 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8469 stop)
8470 && next_element_from_composition (it))
8472 return true;
8475 /* Get the next character, maybe multibyte. */
8476 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8477 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8478 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8479 else
8480 it->c = *p, it->len = 1;
8482 /* Record what we have and where it came from. */
8483 it->what = IT_CHARACTER;
8484 it->object = it->w->contents;
8485 it->position = it->current.pos;
8487 /* Normally we return the character found above, except when we
8488 really want to return an ellipsis for selective display. */
8489 if (it->selective)
8491 if (it->c == '\n')
8493 /* A value of selective > 0 means hide lines indented more
8494 than that number of columns. */
8495 if (it->selective > 0
8496 && IT_CHARPOS (*it) + 1 < ZV
8497 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8498 IT_BYTEPOS (*it) + 1,
8499 it->selective))
8501 success_p = next_element_from_ellipsis (it);
8502 it->dpvec_char_len = -1;
8505 else if (it->c == '\r' && it->selective == -1)
8507 /* A value of selective == -1 means that everything from the
8508 CR to the end of the line is invisible, with maybe an
8509 ellipsis displayed for it. */
8510 success_p = next_element_from_ellipsis (it);
8511 it->dpvec_char_len = -1;
8516 /* Value is false if end of buffer reached. */
8517 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8518 return success_p;
8522 /* Run the redisplay end trigger hook for IT. */
8524 static void
8525 run_redisplay_end_trigger_hook (struct it *it)
8527 /* IT->glyph_row should be non-null, i.e. we should be actually
8528 displaying something, or otherwise we should not run the hook. */
8529 eassert (it->glyph_row);
8531 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8532 it->redisplay_end_trigger_charpos = 0;
8534 /* Since we are *trying* to run these functions, don't try to run
8535 them again, even if they get an error. */
8536 wset_redisplay_end_trigger (it->w, Qnil);
8537 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8538 make_number (charpos));
8540 /* Notice if it changed the face of the character we are on. */
8541 handle_face_prop (it);
8545 /* Deliver a composition display element. Unlike the other
8546 next_element_from_XXX, this function is not registered in the array
8547 get_next_element[]. It is called from next_element_from_buffer and
8548 next_element_from_string when necessary. */
8550 static bool
8551 next_element_from_composition (struct it *it)
8553 it->what = IT_COMPOSITION;
8554 it->len = it->cmp_it.nbytes;
8555 if (STRINGP (it->string))
8557 if (it->c < 0)
8559 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8560 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8561 return false;
8563 it->position = it->current.string_pos;
8564 it->object = it->string;
8565 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8566 IT_STRING_BYTEPOS (*it), it->string);
8568 else
8570 if (it->c < 0)
8572 IT_CHARPOS (*it) += it->cmp_it.nchars;
8573 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8574 if (it->bidi_p)
8576 if (it->bidi_it.new_paragraph)
8577 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8578 false);
8579 /* Resync the bidi iterator with IT's new position.
8580 FIXME: this doesn't support bidirectional text. */
8581 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8582 bidi_move_to_visually_next (&it->bidi_it);
8584 return false;
8586 it->position = it->current.pos;
8587 it->object = it->w->contents;
8588 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8589 IT_BYTEPOS (*it), Qnil);
8591 return true;
8596 /***********************************************************************
8597 Moving an iterator without producing glyphs
8598 ***********************************************************************/
8600 /* Check if iterator is at a position corresponding to a valid buffer
8601 position after some move_it_ call. */
8603 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8604 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8607 /* Move iterator IT to a specified buffer or X position within one
8608 line on the display without producing glyphs.
8610 OP should be a bit mask including some or all of these bits:
8611 MOVE_TO_X: Stop upon reaching x-position TO_X.
8612 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8613 Regardless of OP's value, stop upon reaching the end of the display line.
8615 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8616 This means, in particular, that TO_X includes window's horizontal
8617 scroll amount.
8619 The return value has several possible values that
8620 say what condition caused the scan to stop:
8622 MOVE_POS_MATCH_OR_ZV
8623 - when TO_POS or ZV was reached.
8625 MOVE_X_REACHED
8626 -when TO_X was reached before TO_POS or ZV were reached.
8628 MOVE_LINE_CONTINUED
8629 - when we reached the end of the display area and the line must
8630 be continued.
8632 MOVE_LINE_TRUNCATED
8633 - when we reached the end of the display area and the line is
8634 truncated.
8636 MOVE_NEWLINE_OR_CR
8637 - when we stopped at a line end, i.e. a newline or a CR and selective
8638 display is on. */
8640 static enum move_it_result
8641 move_it_in_display_line_to (struct it *it,
8642 ptrdiff_t to_charpos, int to_x,
8643 enum move_operation_enum op)
8645 enum move_it_result result = MOVE_UNDEFINED;
8646 struct glyph_row *saved_glyph_row;
8647 struct it wrap_it, atpos_it, atx_it, ppos_it;
8648 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8649 void *ppos_data = NULL;
8650 bool may_wrap = false;
8651 enum it_method prev_method = it->method;
8652 ptrdiff_t closest_pos UNINIT;
8653 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8654 bool saw_smaller_pos = prev_pos < to_charpos;
8655 bool line_number_pending = false;
8657 /* Don't produce glyphs in produce_glyphs. */
8658 saved_glyph_row = it->glyph_row;
8659 it->glyph_row = NULL;
8661 /* Use wrap_it to save a copy of IT wherever a word wrap could
8662 occur. Use atpos_it to save a copy of IT at the desired buffer
8663 position, if found, so that we can scan ahead and check if the
8664 word later overshoots the window edge. Use atx_it similarly, for
8665 pixel positions. */
8666 wrap_it.sp = -1;
8667 atpos_it.sp = -1;
8668 atx_it.sp = -1;
8670 /* Use ppos_it under bidi reordering to save a copy of IT for the
8671 initial position. We restore that position in IT when we have
8672 scanned the entire display line without finding a match for
8673 TO_CHARPOS and all the character positions are greater than
8674 TO_CHARPOS. We then restart the scan from the initial position,
8675 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8676 the closest to TO_CHARPOS. */
8677 if (it->bidi_p)
8679 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8681 SAVE_IT (ppos_it, *it, ppos_data);
8682 closest_pos = IT_CHARPOS (*it);
8684 else
8685 closest_pos = ZV;
8688 #define BUFFER_POS_REACHED_P() \
8689 ((op & MOVE_TO_POS) != 0 \
8690 && BUFFERP (it->object) \
8691 && (IT_CHARPOS (*it) == to_charpos \
8692 || ((!it->bidi_p \
8693 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8694 && IT_CHARPOS (*it) > to_charpos) \
8695 || (it->what == IT_COMPOSITION \
8696 && ((IT_CHARPOS (*it) > to_charpos \
8697 && to_charpos >= it->cmp_it.charpos) \
8698 || (IT_CHARPOS (*it) < to_charpos \
8699 && to_charpos <= it->cmp_it.charpos)))) \
8700 && (it->method == GET_FROM_BUFFER \
8701 || (it->method == GET_FROM_DISPLAY_VECTOR \
8702 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8704 if (it->hpos == 0)
8706 /* If line numbers are being displayed, produce a line number. */
8707 if (should_produce_line_number (it))
8709 if (it->current_x == it->first_visible_x)
8710 maybe_produce_line_number (it);
8711 else
8712 line_number_pending = true;
8714 /* If there's a line-/wrap-prefix, handle it. */
8715 if (it->method == GET_FROM_BUFFER)
8716 handle_line_prefix (it);
8719 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8720 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8722 while (true)
8724 int x, i, ascent = 0, descent = 0;
8726 /* Utility macro to reset an iterator with x, ascent, and descent. */
8727 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8728 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8729 (IT)->max_descent = descent)
8731 /* Stop if we move beyond TO_CHARPOS (after an image or a
8732 display string or stretch glyph). */
8733 if ((op & MOVE_TO_POS) != 0
8734 && BUFFERP (it->object)
8735 && it->method == GET_FROM_BUFFER
8736 && (((!it->bidi_p
8737 /* When the iterator is at base embedding level, we
8738 are guaranteed that characters are delivered for
8739 display in strictly increasing order of their
8740 buffer positions. */
8741 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8742 && IT_CHARPOS (*it) > to_charpos)
8743 || (it->bidi_p
8744 && (prev_method == GET_FROM_IMAGE
8745 || prev_method == GET_FROM_STRETCH
8746 || prev_method == GET_FROM_STRING)
8747 /* Passed TO_CHARPOS from left to right. */
8748 && ((prev_pos < to_charpos
8749 && IT_CHARPOS (*it) > to_charpos)
8750 /* Passed TO_CHARPOS from right to left. */
8751 || (prev_pos > to_charpos
8752 && IT_CHARPOS (*it) < to_charpos)))))
8754 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8756 result = MOVE_POS_MATCH_OR_ZV;
8757 break;
8759 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8760 /* If wrap_it is valid, the current position might be in a
8761 word that is wrapped. So, save the iterator in
8762 atpos_it and continue to see if wrapping happens. */
8763 SAVE_IT (atpos_it, *it, atpos_data);
8766 /* Stop when ZV reached.
8767 We used to stop here when TO_CHARPOS reached as well, but that is
8768 too soon if this glyph does not fit on this line. So we handle it
8769 explicitly below. */
8770 if (!get_next_display_element (it))
8772 result = MOVE_POS_MATCH_OR_ZV;
8773 break;
8776 if (it->line_wrap == TRUNCATE)
8778 if (BUFFER_POS_REACHED_P ())
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8784 else
8786 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8788 if (IT_DISPLAYING_WHITESPACE (it))
8789 may_wrap = true;
8790 else if (may_wrap)
8792 /* We have reached a glyph that follows one or more
8793 whitespace characters. If the position is
8794 already found, we are done. */
8795 if (atpos_it.sp >= 0)
8797 RESTORE_IT (it, &atpos_it, atpos_data);
8798 result = MOVE_POS_MATCH_OR_ZV;
8799 goto done;
8801 if (atx_it.sp >= 0)
8803 RESTORE_IT (it, &atx_it, atx_data);
8804 result = MOVE_X_REACHED;
8805 goto done;
8807 /* Otherwise, we can wrap here. */
8808 SAVE_IT (wrap_it, *it, wrap_data);
8809 may_wrap = false;
8814 /* Remember the line height for the current line, in case
8815 the next element doesn't fit on the line. */
8816 ascent = it->max_ascent;
8817 descent = it->max_descent;
8819 /* The call to produce_glyphs will get the metrics of the
8820 display element IT is loaded with. Record the x-position
8821 before this display element, in case it doesn't fit on the
8822 line. */
8823 x = it->current_x;
8825 PRODUCE_GLYPHS (it);
8827 if (it->area != TEXT_AREA)
8829 prev_method = it->method;
8830 if (it->method == GET_FROM_BUFFER)
8831 prev_pos = IT_CHARPOS (*it);
8832 set_iterator_to_next (it, true);
8833 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8834 SET_TEXT_POS (this_line_min_pos,
8835 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8836 if (it->bidi_p
8837 && (op & MOVE_TO_POS)
8838 && IT_CHARPOS (*it) > to_charpos
8839 && IT_CHARPOS (*it) < closest_pos)
8840 closest_pos = IT_CHARPOS (*it);
8841 continue;
8844 /* The number of glyphs we get back in IT->nglyphs will normally
8845 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8846 character on a terminal frame, or (iii) a line end. For the
8847 second case, IT->nglyphs - 1 padding glyphs will be present.
8848 (On X frames, there is only one glyph produced for a
8849 composite character.)
8851 The behavior implemented below means, for continuation lines,
8852 that as many spaces of a TAB as fit on the current line are
8853 displayed there. For terminal frames, as many glyphs of a
8854 multi-glyph character are displayed in the current line, too.
8855 This is what the old redisplay code did, and we keep it that
8856 way. Under X, the whole shape of a complex character must
8857 fit on the line or it will be completely displayed in the
8858 next line.
8860 Note that both for tabs and padding glyphs, all glyphs have
8861 the same width. */
8862 if (it->nglyphs)
8864 /* More than one glyph or glyph doesn't fit on line. All
8865 glyphs have the same width. */
8866 int single_glyph_width = it->pixel_width / it->nglyphs;
8867 int new_x;
8868 int x_before_this_char = x;
8869 int hpos_before_this_char = it->hpos;
8871 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8873 new_x = x + single_glyph_width;
8875 /* We want to leave anything reaching TO_X to the caller. */
8876 if ((op & MOVE_TO_X) && new_x > to_x)
8878 if (BUFFER_POS_REACHED_P ())
8880 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8881 goto buffer_pos_reached;
8882 if (atpos_it.sp < 0)
8884 SAVE_IT (atpos_it, *it, atpos_data);
8885 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8888 else
8890 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8892 it->current_x = x;
8893 result = MOVE_X_REACHED;
8894 break;
8896 if (atx_it.sp < 0)
8898 SAVE_IT (atx_it, *it, atx_data);
8899 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8904 if (/* Lines are continued. */
8905 it->line_wrap != TRUNCATE
8906 && (/* And glyph doesn't fit on the line. */
8907 new_x > it->last_visible_x
8908 /* Or it fits exactly and we're on a window
8909 system frame. */
8910 || (new_x == it->last_visible_x
8911 && FRAME_WINDOW_P (it->f)
8912 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8913 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8914 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8916 bool moved_forward = false;
8918 if (/* IT->hpos == 0 means the very first glyph
8919 doesn't fit on the line, e.g. a wide image. */
8920 it->hpos == 0
8921 || (new_x == it->last_visible_x
8922 && FRAME_WINDOW_P (it->f)))
8924 ++it->hpos;
8925 it->current_x = new_x;
8927 /* The character's last glyph just barely fits
8928 in this row. */
8929 if (i == it->nglyphs - 1)
8931 /* If this is the destination position,
8932 return a position *before* it in this row,
8933 now that we know it fits in this row. */
8934 if (BUFFER_POS_REACHED_P ())
8936 bool can_wrap = true;
8938 /* If we are at a whitespace character
8939 that barely fits on this screen line,
8940 but the next character is also
8941 whitespace, we cannot wrap here. */
8942 if (it->line_wrap == WORD_WRAP
8943 && wrap_it.sp >= 0
8944 && may_wrap
8945 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8947 struct it tem_it;
8948 void *tem_data = NULL;
8950 SAVE_IT (tem_it, *it, tem_data);
8951 set_iterator_to_next (it, true);
8952 if (get_next_display_element (it)
8953 && IT_DISPLAYING_WHITESPACE (it))
8954 can_wrap = false;
8955 RESTORE_IT (it, &tem_it, tem_data);
8957 if (it->line_wrap != WORD_WRAP
8958 || wrap_it.sp < 0
8959 /* If we've just found whitespace
8960 where we can wrap, effectively
8961 ignore the previous wrap point --
8962 it is no longer relevant, but we
8963 won't have an opportunity to
8964 update it, since we've reached
8965 the edge of this screen line. */
8966 || (may_wrap && can_wrap
8967 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8969 it->hpos = hpos_before_this_char;
8970 it->current_x = x_before_this_char;
8971 result = MOVE_POS_MATCH_OR_ZV;
8972 break;
8974 if (it->line_wrap == WORD_WRAP
8975 && atpos_it.sp < 0)
8977 SAVE_IT (atpos_it, *it, atpos_data);
8978 atpos_it.current_x = x_before_this_char;
8979 atpos_it.hpos = hpos_before_this_char;
8983 prev_method = it->method;
8984 if (it->method == GET_FROM_BUFFER)
8985 prev_pos = IT_CHARPOS (*it);
8986 set_iterator_to_next (it, true);
8987 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8988 SET_TEXT_POS (this_line_min_pos,
8989 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8990 /* On graphical terminals, newlines may
8991 "overflow" into the fringe if
8992 overflow-newline-into-fringe is non-nil.
8993 On text terminals, and on graphical
8994 terminals with no right margin, newlines
8995 may overflow into the last glyph on the
8996 display line.*/
8997 if (!FRAME_WINDOW_P (it->f)
8998 || ((it->bidi_p
8999 && it->bidi_it.paragraph_dir == R2L)
9000 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9001 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9002 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9004 if (!get_next_display_element (it))
9006 result = MOVE_POS_MATCH_OR_ZV;
9007 break;
9009 moved_forward = true;
9010 if (BUFFER_POS_REACHED_P ())
9012 if (ITERATOR_AT_END_OF_LINE_P (it))
9013 result = MOVE_POS_MATCH_OR_ZV;
9014 else
9015 result = MOVE_LINE_CONTINUED;
9016 break;
9018 if (ITERATOR_AT_END_OF_LINE_P (it)
9019 && (it->line_wrap != WORD_WRAP
9020 || wrap_it.sp < 0
9021 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9023 result = MOVE_NEWLINE_OR_CR;
9024 break;
9029 else
9030 IT_RESET_X_ASCENT_DESCENT (it);
9032 /* If the screen line ends with whitespace, and we
9033 are under word-wrap, don't use wrap_it: it is no
9034 longer relevant, but we won't have an opportunity
9035 to update it, since we are done with this screen
9036 line. */
9037 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9038 /* If the character after the one which set the
9039 may_wrap flag is also whitespace, we can't
9040 wrap here, since the screen line cannot be
9041 wrapped in the middle of whitespace.
9042 Therefore, wrap_it _is_ relevant in that
9043 case. */
9044 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9046 /* If we've found TO_X, go back there, as we now
9047 know the last word fits on this screen line. */
9048 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9049 && atx_it.sp >= 0)
9051 RESTORE_IT (it, &atx_it, atx_data);
9052 atpos_it.sp = -1;
9053 atx_it.sp = -1;
9054 result = MOVE_X_REACHED;
9055 break;
9058 else if (wrap_it.sp >= 0)
9060 RESTORE_IT (it, &wrap_it, wrap_data);
9061 atpos_it.sp = -1;
9062 atx_it.sp = -1;
9065 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9066 IT_CHARPOS (*it)));
9067 result = MOVE_LINE_CONTINUED;
9068 break;
9071 if (BUFFER_POS_REACHED_P ())
9073 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9074 goto buffer_pos_reached;
9075 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9077 SAVE_IT (atpos_it, *it, atpos_data);
9078 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9082 if (new_x > it->first_visible_x)
9084 /* If we have reached the visible portion of the
9085 screen line, produce the line number if needed. */
9086 if (line_number_pending)
9088 line_number_pending = false;
9089 it->current_x = it->first_visible_x;
9090 maybe_produce_line_number (it);
9091 it->current_x += new_x - it->first_visible_x;
9093 /* Glyph is visible. Increment number of glyphs that
9094 would be displayed. */
9095 ++it->hpos;
9099 if (result != MOVE_UNDEFINED)
9100 break;
9102 else if (BUFFER_POS_REACHED_P ())
9104 buffer_pos_reached:
9105 IT_RESET_X_ASCENT_DESCENT (it);
9106 result = MOVE_POS_MATCH_OR_ZV;
9107 break;
9109 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9111 /* Stop when TO_X specified and reached. This check is
9112 necessary here because of lines consisting of a line end,
9113 only. The line end will not produce any glyphs and we
9114 would never get MOVE_X_REACHED. */
9115 eassert (it->nglyphs == 0);
9116 result = MOVE_X_REACHED;
9117 break;
9120 /* Is this a line end? If yes, we're done. */
9121 if (ITERATOR_AT_END_OF_LINE_P (it))
9123 /* If we are past TO_CHARPOS, but never saw any character
9124 positions smaller than TO_CHARPOS, return
9125 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9126 did. */
9127 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9129 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9131 if (closest_pos < ZV)
9133 RESTORE_IT (it, &ppos_it, ppos_data);
9134 /* Don't recurse if closest_pos is equal to
9135 to_charpos, since we have just tried that. */
9136 if (closest_pos != to_charpos)
9137 move_it_in_display_line_to (it, closest_pos, -1,
9138 MOVE_TO_POS);
9139 result = MOVE_POS_MATCH_OR_ZV;
9141 else
9142 goto buffer_pos_reached;
9144 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9145 && IT_CHARPOS (*it) > to_charpos)
9146 goto buffer_pos_reached;
9147 else
9148 result = MOVE_NEWLINE_OR_CR;
9150 else
9151 result = MOVE_NEWLINE_OR_CR;
9152 /* If we've processed the newline, make sure this flag is
9153 reset, as it must only be set when the newline itself is
9154 processed. */
9155 if (result == MOVE_NEWLINE_OR_CR)
9156 it->constrain_row_ascent_descent_p = false;
9157 break;
9160 prev_method = it->method;
9161 if (it->method == GET_FROM_BUFFER)
9162 prev_pos = IT_CHARPOS (*it);
9163 /* The current display element has been consumed. Advance
9164 to the next. */
9165 set_iterator_to_next (it, true);
9166 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9167 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9168 if (IT_CHARPOS (*it) < to_charpos)
9169 saw_smaller_pos = true;
9170 if (it->bidi_p
9171 && (op & MOVE_TO_POS)
9172 && IT_CHARPOS (*it) >= to_charpos
9173 && IT_CHARPOS (*it) < closest_pos)
9174 closest_pos = IT_CHARPOS (*it);
9176 /* Stop if lines are truncated and IT's current x-position is
9177 past the right edge of the window now. */
9178 if (it->line_wrap == TRUNCATE
9179 && it->current_x >= it->last_visible_x)
9181 if (!FRAME_WINDOW_P (it->f)
9182 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9183 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9184 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9185 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9187 bool at_eob_p = false;
9189 if ((at_eob_p = !get_next_display_element (it))
9190 || BUFFER_POS_REACHED_P ()
9191 /* If we are past TO_CHARPOS, but never saw any
9192 character positions smaller than TO_CHARPOS,
9193 return MOVE_POS_MATCH_OR_ZV, like the
9194 unidirectional display did. */
9195 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9196 && !saw_smaller_pos
9197 && IT_CHARPOS (*it) > to_charpos))
9199 if (it->bidi_p
9200 && !BUFFER_POS_REACHED_P ()
9201 && !at_eob_p && closest_pos < ZV)
9203 RESTORE_IT (it, &ppos_it, ppos_data);
9204 if (closest_pos != to_charpos)
9205 move_it_in_display_line_to (it, closest_pos, -1,
9206 MOVE_TO_POS);
9208 result = MOVE_POS_MATCH_OR_ZV;
9209 break;
9211 if (ITERATOR_AT_END_OF_LINE_P (it))
9213 result = MOVE_NEWLINE_OR_CR;
9214 break;
9217 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9218 && !saw_smaller_pos
9219 && IT_CHARPOS (*it) > to_charpos)
9221 if (closest_pos < ZV)
9223 RESTORE_IT (it, &ppos_it, ppos_data);
9224 if (closest_pos != to_charpos)
9225 move_it_in_display_line_to (it, closest_pos, -1,
9226 MOVE_TO_POS);
9228 result = MOVE_POS_MATCH_OR_ZV;
9229 break;
9231 result = MOVE_LINE_TRUNCATED;
9232 break;
9234 #undef IT_RESET_X_ASCENT_DESCENT
9237 #undef BUFFER_POS_REACHED_P
9239 /* If we scanned beyond TO_POS, restore the saved iterator either to
9240 the wrap point (if found), or to atpos/atx location. We decide which
9241 data to use to restore the saved iterator state by their X coordinates,
9242 since buffer positions might increase non-monotonically with screen
9243 coordinates due to bidi reordering. */
9244 if (result == MOVE_LINE_CONTINUED
9245 && it->line_wrap == WORD_WRAP
9246 && wrap_it.sp >= 0
9247 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9248 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9249 RESTORE_IT (it, &wrap_it, wrap_data);
9250 else if (atpos_it.sp >= 0)
9251 RESTORE_IT (it, &atpos_it, atpos_data);
9252 else if (atx_it.sp >= 0)
9253 RESTORE_IT (it, &atx_it, atx_data);
9255 done:
9257 if (atpos_data)
9258 bidi_unshelve_cache (atpos_data, true);
9259 if (atx_data)
9260 bidi_unshelve_cache (atx_data, true);
9261 if (wrap_data)
9262 bidi_unshelve_cache (wrap_data, true);
9263 if (ppos_data)
9264 bidi_unshelve_cache (ppos_data, true);
9266 /* Restore the iterator settings altered at the beginning of this
9267 function. */
9268 it->glyph_row = saved_glyph_row;
9269 return result;
9272 /* For external use. */
9273 void
9274 move_it_in_display_line (struct it *it,
9275 ptrdiff_t to_charpos, int to_x,
9276 enum move_operation_enum op)
9278 if (it->line_wrap == WORD_WRAP
9279 && (op & MOVE_TO_X))
9281 struct it save_it;
9282 void *save_data = NULL;
9283 int skip;
9285 SAVE_IT (save_it, *it, save_data);
9286 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9287 /* When word-wrap is on, TO_X may lie past the end
9288 of a wrapped line. Then it->current is the
9289 character on the next line, so backtrack to the
9290 space before the wrap point. */
9291 if (skip == MOVE_LINE_CONTINUED)
9293 int prev_x = max (it->current_x - 1, 0);
9294 RESTORE_IT (it, &save_it, save_data);
9295 move_it_in_display_line_to
9296 (it, -1, prev_x, MOVE_TO_X);
9298 else
9299 bidi_unshelve_cache (save_data, true);
9301 else
9302 move_it_in_display_line_to (it, to_charpos, to_x, op);
9306 /* Move IT forward until it satisfies one or more of the criteria in
9307 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9309 OP is a bit-mask that specifies where to stop, and in particular,
9310 which of those four position arguments makes a difference. See the
9311 description of enum move_operation_enum.
9313 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9314 screen line, this function will set IT to the next position that is
9315 displayed to the right of TO_CHARPOS on the screen.
9317 Return the maximum pixel length of any line scanned but never more
9318 than it.last_visible_x. */
9321 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9323 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9324 int line_height, line_start_x = 0, reached = 0;
9325 int max_current_x = 0;
9326 void *backup_data = NULL;
9328 for (;;)
9330 if (op & MOVE_TO_VPOS)
9332 /* If no TO_CHARPOS and no TO_X specified, stop at the
9333 start of the line TO_VPOS. */
9334 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9336 if (it->vpos == to_vpos)
9338 reached = 1;
9339 break;
9341 else
9342 skip = move_it_in_display_line_to (it, -1, -1, 0);
9344 else
9346 /* TO_VPOS >= 0 means stop at TO_X in the line at
9347 TO_VPOS, or at TO_POS, whichever comes first. */
9348 if (it->vpos == to_vpos)
9350 reached = 2;
9351 break;
9354 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9356 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9358 reached = 3;
9359 break;
9361 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9363 /* We have reached TO_X but not in the line we want. */
9364 skip = move_it_in_display_line_to (it, to_charpos,
9365 -1, MOVE_TO_POS);
9366 if (skip == MOVE_POS_MATCH_OR_ZV)
9368 reached = 4;
9369 break;
9374 else if (op & MOVE_TO_Y)
9376 struct it it_backup;
9378 if (it->line_wrap == WORD_WRAP)
9379 SAVE_IT (it_backup, *it, backup_data);
9381 /* TO_Y specified means stop at TO_X in the line containing
9382 TO_Y---or at TO_CHARPOS if this is reached first. The
9383 problem is that we can't really tell whether the line
9384 contains TO_Y before we have completely scanned it, and
9385 this may skip past TO_X. What we do is to first scan to
9386 TO_X.
9388 If TO_X is not specified, use a TO_X of zero. The reason
9389 is to make the outcome of this function more predictable.
9390 If we didn't use TO_X == 0, we would stop at the end of
9391 the line which is probably not what a caller would expect
9392 to happen. */
9393 skip = move_it_in_display_line_to
9394 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9395 (MOVE_TO_X | (op & MOVE_TO_POS)));
9397 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9398 if (skip == MOVE_POS_MATCH_OR_ZV)
9399 reached = 5;
9400 else if (skip == MOVE_X_REACHED)
9402 /* If TO_X was reached, we want to know whether TO_Y is
9403 in the line. We know this is the case if the already
9404 scanned glyphs make the line tall enough. Otherwise,
9405 we must check by scanning the rest of the line. */
9406 line_height = it->max_ascent + it->max_descent;
9407 if (to_y >= it->current_y
9408 && to_y < it->current_y + line_height)
9410 reached = 6;
9411 break;
9413 SAVE_IT (it_backup, *it, backup_data);
9414 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9415 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9416 op & MOVE_TO_POS);
9417 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9418 line_height = it->max_ascent + it->max_descent;
9419 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9421 if (to_y >= it->current_y
9422 && to_y < it->current_y + line_height)
9424 /* If TO_Y is in this line and TO_X was reached
9425 above, we scanned too far. We have to restore
9426 IT's settings to the ones before skipping. But
9427 keep the more accurate values of max_ascent and
9428 max_descent we've found while skipping the rest
9429 of the line, for the sake of callers, such as
9430 pos_visible_p, that need to know the line
9431 height. */
9432 int max_ascent = it->max_ascent;
9433 int max_descent = it->max_descent;
9435 RESTORE_IT (it, &it_backup, backup_data);
9436 it->max_ascent = max_ascent;
9437 it->max_descent = max_descent;
9438 reached = 6;
9440 else
9442 skip = skip2;
9443 if (skip == MOVE_POS_MATCH_OR_ZV)
9444 reached = 7;
9447 else
9449 /* Check whether TO_Y is in this line. */
9450 line_height = it->max_ascent + it->max_descent;
9451 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9453 if (to_y >= it->current_y
9454 && to_y < it->current_y + line_height)
9456 if (to_y > it->current_y)
9457 max_current_x = max (it->current_x, max_current_x);
9459 /* When word-wrap is on, TO_X may lie past the end
9460 of a wrapped line. Then it->current is the
9461 character on the next line, so backtrack to the
9462 space before the wrap point. */
9463 if (skip == MOVE_LINE_CONTINUED
9464 && it->line_wrap == WORD_WRAP)
9466 int prev_x = max (it->current_x - 1, 0);
9467 RESTORE_IT (it, &it_backup, backup_data);
9468 skip = move_it_in_display_line_to
9469 (it, -1, prev_x, MOVE_TO_X);
9472 reached = 6;
9476 if (reached)
9478 max_current_x = max (it->current_x, max_current_x);
9479 break;
9482 else if (BUFFERP (it->object)
9483 && (it->method == GET_FROM_BUFFER
9484 || it->method == GET_FROM_STRETCH)
9485 && IT_CHARPOS (*it) >= to_charpos
9486 /* Under bidi iteration, a call to set_iterator_to_next
9487 can scan far beyond to_charpos if the initial
9488 portion of the next line needs to be reordered. In
9489 that case, give move_it_in_display_line_to another
9490 chance below. */
9491 && !(it->bidi_p
9492 && it->bidi_it.scan_dir == -1))
9493 skip = MOVE_POS_MATCH_OR_ZV;
9494 else
9495 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9497 switch (skip)
9499 case MOVE_POS_MATCH_OR_ZV:
9500 max_current_x = max (it->current_x, max_current_x);
9501 reached = 8;
9502 goto out;
9504 case MOVE_NEWLINE_OR_CR:
9505 max_current_x = max (it->current_x, max_current_x);
9506 set_iterator_to_next (it, true);
9507 it->continuation_lines_width = 0;
9508 break;
9510 case MOVE_LINE_TRUNCATED:
9511 max_current_x = it->last_visible_x;
9512 it->continuation_lines_width = 0;
9513 reseat_at_next_visible_line_start (it, false);
9514 if ((op & MOVE_TO_POS) != 0
9515 && IT_CHARPOS (*it) > to_charpos)
9517 reached = 9;
9518 goto out;
9520 break;
9522 case MOVE_LINE_CONTINUED:
9523 max_current_x = it->last_visible_x;
9524 /* For continued lines ending in a tab, some of the glyphs
9525 associated with the tab are displayed on the current
9526 line. Since it->current_x does not include these glyphs,
9527 we use it->last_visible_x instead. */
9528 if (it->c == '\t')
9530 it->continuation_lines_width += it->last_visible_x;
9531 /* When moving by vpos, ensure that the iterator really
9532 advances to the next line (bug#847, bug#969). Fixme:
9533 do we need to do this in other circumstances? */
9534 if (it->current_x != it->last_visible_x
9535 && (op & MOVE_TO_VPOS)
9536 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9538 line_start_x = it->current_x + it->pixel_width
9539 - it->last_visible_x;
9540 if (FRAME_WINDOW_P (it->f))
9542 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9543 struct font *face_font = face->font;
9545 /* When display_line produces a continued line
9546 that ends in a TAB, it skips a tab stop that
9547 is closer than the font's space character
9548 width (see x_produce_glyphs where it produces
9549 the stretch glyph which represents a TAB).
9550 We need to reproduce the same logic here. */
9551 eassert (face_font);
9552 if (face_font)
9554 if (line_start_x < face_font->space_width)
9555 line_start_x
9556 += it->tab_width * face_font->space_width;
9559 set_iterator_to_next (it, false);
9562 else
9563 it->continuation_lines_width += it->current_x;
9564 break;
9566 default:
9567 emacs_abort ();
9570 /* Reset/increment for the next run. */
9571 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9572 it->current_x = line_start_x;
9573 line_start_x = 0;
9574 it->hpos = 0;
9575 it->current_y += it->max_ascent + it->max_descent;
9576 ++it->vpos;
9577 last_height = it->max_ascent + it->max_descent;
9578 it->max_ascent = it->max_descent = 0;
9581 out:
9583 /* On text terminals, we may stop at the end of a line in the middle
9584 of a multi-character glyph. If the glyph itself is continued,
9585 i.e. it is actually displayed on the next line, don't treat this
9586 stopping point as valid; move to the next line instead (unless
9587 that brings us offscreen). */
9588 if (!FRAME_WINDOW_P (it->f)
9589 && op & MOVE_TO_POS
9590 && IT_CHARPOS (*it) == to_charpos
9591 && it->what == IT_CHARACTER
9592 && it->nglyphs > 1
9593 && it->line_wrap == WINDOW_WRAP
9594 && it->current_x == it->last_visible_x - 1
9595 && it->c != '\n'
9596 && it->c != '\t'
9597 && it->w->window_end_valid
9598 && it->vpos < it->w->window_end_vpos)
9600 it->continuation_lines_width += it->current_x;
9601 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9602 it->current_y += it->max_ascent + it->max_descent;
9603 ++it->vpos;
9604 last_height = it->max_ascent + it->max_descent;
9607 if (backup_data)
9608 bidi_unshelve_cache (backup_data, true);
9610 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9612 return max_current_x;
9616 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9618 If DY > 0, move IT backward at least that many pixels. DY = 0
9619 means move IT backward to the preceding line start or BEGV. This
9620 function may move over more than DY pixels if IT->current_y - DY
9621 ends up in the middle of a line; in this case IT->current_y will be
9622 set to the top of the line moved to. */
9624 void
9625 move_it_vertically_backward (struct it *it, int dy)
9627 int nlines, h;
9628 struct it it2, it3;
9629 void *it2data = NULL, *it3data = NULL;
9630 ptrdiff_t start_pos;
9631 int nchars_per_row
9632 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9633 ptrdiff_t pos_limit;
9635 move_further_back:
9636 eassert (dy >= 0);
9638 start_pos = IT_CHARPOS (*it);
9640 /* Estimate how many newlines we must move back. */
9641 nlines = max (1, dy / default_line_pixel_height (it->w));
9642 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9643 pos_limit = BEGV;
9644 else
9645 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9647 /* Set the iterator's position that many lines back. But don't go
9648 back more than NLINES full screen lines -- this wins a day with
9649 buffers which have very long lines. */
9650 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9651 back_to_previous_visible_line_start (it);
9653 /* Reseat the iterator here. When moving backward, we don't want
9654 reseat to skip forward over invisible text, set up the iterator
9655 to deliver from overlay strings at the new position etc. So,
9656 use reseat_1 here. */
9657 reseat_1 (it, it->current.pos, true);
9659 /* We are now surely at a line start. */
9660 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9661 reordering is in effect. */
9662 it->continuation_lines_width = 0;
9664 /* Move forward and see what y-distance we moved. First move to the
9665 start of the next line so that we get its height. We need this
9666 height to be able to tell whether we reached the specified
9667 y-distance. */
9668 SAVE_IT (it2, *it, it2data);
9669 it2.max_ascent = it2.max_descent = 0;
9672 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9673 MOVE_TO_POS | MOVE_TO_VPOS);
9675 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9676 /* If we are in a display string which starts at START_POS,
9677 and that display string includes a newline, and we are
9678 right after that newline (i.e. at the beginning of a
9679 display line), exit the loop, because otherwise we will
9680 infloop, since move_it_to will see that it is already at
9681 START_POS and will not move. */
9682 || (it2.method == GET_FROM_STRING
9683 && IT_CHARPOS (it2) == start_pos
9684 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9685 eassert (IT_CHARPOS (*it) >= BEGV);
9686 SAVE_IT (it3, it2, it3data);
9688 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9689 eassert (IT_CHARPOS (*it) >= BEGV);
9690 /* H is the actual vertical distance from the position in *IT
9691 and the starting position. */
9692 h = it2.current_y - it->current_y;
9693 /* NLINES is the distance in number of lines. */
9694 nlines = it2.vpos - it->vpos;
9696 /* Correct IT's y and vpos position
9697 so that they are relative to the starting point. */
9698 it->vpos -= nlines;
9699 it->current_y -= h;
9701 if (dy == 0)
9703 /* DY == 0 means move to the start of the screen line. The
9704 value of nlines is > 0 if continuation lines were involved,
9705 or if the original IT position was at start of a line. */
9706 RESTORE_IT (it, it, it2data);
9707 if (nlines > 0)
9708 move_it_by_lines (it, nlines);
9709 /* The above code moves us to some position NLINES down,
9710 usually to its first glyph (leftmost in an L2R line), but
9711 that's not necessarily the start of the line, under bidi
9712 reordering. We want to get to the character position
9713 that is immediately after the newline of the previous
9714 line. */
9715 if (it->bidi_p
9716 && !it->continuation_lines_width
9717 && !STRINGP (it->string)
9718 && IT_CHARPOS (*it) > BEGV
9719 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9721 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9723 DEC_BOTH (cp, bp);
9724 cp = find_newline_no_quit (cp, bp, -1, NULL);
9725 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9727 bidi_unshelve_cache (it3data, true);
9729 else
9731 /* The y-position we try to reach, relative to *IT.
9732 Note that H has been subtracted in front of the if-statement. */
9733 int target_y = it->current_y + h - dy;
9734 int y0 = it3.current_y;
9735 int y1;
9736 int line_height;
9738 RESTORE_IT (&it3, &it3, it3data);
9739 y1 = line_bottom_y (&it3);
9740 line_height = y1 - y0;
9741 RESTORE_IT (it, it, it2data);
9742 /* If we did not reach target_y, try to move further backward if
9743 we can. If we moved too far backward, try to move forward. */
9744 if (target_y < it->current_y
9745 /* This is heuristic. In a window that's 3 lines high, with
9746 a line height of 13 pixels each, recentering with point
9747 on the bottom line will try to move -39/2 = 19 pixels
9748 backward. Try to avoid moving into the first line. */
9749 && (it->current_y - target_y
9750 > min (window_box_height (it->w), line_height * 2 / 3))
9751 && IT_CHARPOS (*it) > BEGV)
9753 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9754 target_y - it->current_y));
9755 dy = it->current_y - target_y;
9756 goto move_further_back;
9758 else if (target_y >= it->current_y + line_height
9759 && IT_CHARPOS (*it) < ZV)
9761 /* Should move forward by at least one line, maybe more.
9763 Note: Calling move_it_by_lines can be expensive on
9764 terminal frames, where compute_motion is used (via
9765 vmotion) to do the job, when there are very long lines
9766 and truncate-lines is nil. That's the reason for
9767 treating terminal frames specially here. */
9769 if (!FRAME_WINDOW_P (it->f))
9770 move_it_vertically (it, target_y - it->current_y);
9771 else
9775 move_it_by_lines (it, 1);
9777 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9784 /* Move IT by a specified amount of pixel lines DY. DY negative means
9785 move backwards. DY = 0 means move to start of screen line. At the
9786 end, IT will be on the start of a screen line. */
9788 void
9789 move_it_vertically (struct it *it, int dy)
9791 if (dy <= 0)
9792 move_it_vertically_backward (it, -dy);
9793 else
9795 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9796 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9797 MOVE_TO_POS | MOVE_TO_Y);
9798 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9800 /* If buffer ends in ZV without a newline, move to the start of
9801 the line to satisfy the post-condition. */
9802 if (IT_CHARPOS (*it) == ZV
9803 && ZV > BEGV
9804 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9805 move_it_by_lines (it, 0);
9810 /* Move iterator IT past the end of the text line it is in. */
9812 void
9813 move_it_past_eol (struct it *it)
9815 enum move_it_result rc;
9817 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9818 if (rc == MOVE_NEWLINE_OR_CR)
9819 set_iterator_to_next (it, false);
9823 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9824 negative means move up. DVPOS == 0 means move to the start of the
9825 screen line.
9827 Optimization idea: If we would know that IT->f doesn't use
9828 a face with proportional font, we could be faster for
9829 truncate-lines nil. */
9831 void
9832 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9835 /* The commented-out optimization uses vmotion on terminals. This
9836 gives bad results, because elements like it->what, on which
9837 callers such as pos_visible_p rely, aren't updated. */
9838 /* struct position pos;
9839 if (!FRAME_WINDOW_P (it->f))
9841 struct text_pos textpos;
9843 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9844 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9845 reseat (it, textpos, true);
9846 it->vpos += pos.vpos;
9847 it->current_y += pos.vpos;
9849 else */
9851 if (dvpos == 0)
9853 /* DVPOS == 0 means move to the start of the screen line. */
9854 move_it_vertically_backward (it, 0);
9855 /* Let next call to line_bottom_y calculate real line height. */
9856 last_height = 0;
9858 else if (dvpos > 0)
9860 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9861 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9863 /* Only move to the next buffer position if we ended up in a
9864 string from display property, not in an overlay string
9865 (before-string or after-string). That is because the
9866 latter don't conceal the underlying buffer position, so
9867 we can ask to move the iterator to the exact position we
9868 are interested in. Note that, even if we are already at
9869 IT_CHARPOS (*it), the call below is not a no-op, as it
9870 will detect that we are at the end of the string, pop the
9871 iterator, and compute it->current_x and it->hpos
9872 correctly. */
9873 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9874 -1, -1, -1, MOVE_TO_POS);
9877 else
9879 struct it it2;
9880 void *it2data = NULL;
9881 ptrdiff_t start_charpos, i;
9882 int nchars_per_row
9883 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9884 bool hit_pos_limit = false;
9885 ptrdiff_t pos_limit;
9887 /* Start at the beginning of the screen line containing IT's
9888 position. This may actually move vertically backwards,
9889 in case of overlays, so adjust dvpos accordingly. */
9890 dvpos += it->vpos;
9891 move_it_vertically_backward (it, 0);
9892 dvpos -= it->vpos;
9894 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9895 screen lines, and reseat the iterator there. */
9896 start_charpos = IT_CHARPOS (*it);
9897 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9898 pos_limit = BEGV;
9899 else
9900 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9902 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9903 back_to_previous_visible_line_start (it);
9904 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9905 hit_pos_limit = true;
9906 reseat (it, it->current.pos, true);
9908 /* Move further back if we end up in a string or an image. */
9909 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9911 /* First try to move to start of display line. */
9912 dvpos += it->vpos;
9913 move_it_vertically_backward (it, 0);
9914 dvpos -= it->vpos;
9915 if (IT_POS_VALID_AFTER_MOVE_P (it))
9916 break;
9917 /* If start of line is still in string or image,
9918 move further back. */
9919 back_to_previous_visible_line_start (it);
9920 reseat (it, it->current.pos, true);
9921 dvpos--;
9924 it->current_x = it->hpos = 0;
9926 /* Above call may have moved too far if continuation lines
9927 are involved. Scan forward and see if it did. */
9928 SAVE_IT (it2, *it, it2data);
9929 it2.vpos = it2.current_y = 0;
9930 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9931 it->vpos -= it2.vpos;
9932 it->current_y -= it2.current_y;
9933 it->current_x = it->hpos = 0;
9935 /* If we moved too far back, move IT some lines forward. */
9936 if (it2.vpos > -dvpos)
9938 int delta = it2.vpos + dvpos;
9940 RESTORE_IT (&it2, &it2, it2data);
9941 SAVE_IT (it2, *it, it2data);
9942 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9943 /* Move back again if we got too far ahead. */
9944 if (IT_CHARPOS (*it) >= start_charpos)
9945 RESTORE_IT (it, &it2, it2data);
9946 else
9947 bidi_unshelve_cache (it2data, true);
9949 else if (hit_pos_limit && pos_limit > BEGV
9950 && dvpos < 0 && it2.vpos < -dvpos)
9952 /* If we hit the limit, but still didn't make it far enough
9953 back, that means there's a display string with a newline
9954 covering a large chunk of text, and that caused
9955 back_to_previous_visible_line_start try to go too far.
9956 Punish those who commit such atrocities by going back
9957 until we've reached DVPOS, after lifting the limit, which
9958 could make it slow for very long lines. "If it hurts,
9959 don't do that!" */
9960 dvpos += it2.vpos;
9961 RESTORE_IT (it, it, it2data);
9962 for (i = -dvpos; i > 0; --i)
9964 back_to_previous_visible_line_start (it);
9965 it->vpos--;
9967 reseat_1 (it, it->current.pos, true);
9969 else
9970 RESTORE_IT (it, it, it2data);
9975 partial_line_height (struct it *it_origin)
9977 int partial_height;
9978 void *it_data = NULL;
9979 struct it it;
9980 SAVE_IT (it, *it_origin, it_data);
9981 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9982 MOVE_TO_POS | MOVE_TO_Y);
9983 if (it.what == IT_EOB)
9985 int vis_height = it.last_visible_y - it.current_y;
9986 int height = it.ascent + it.descent;
9987 partial_height = (vis_height < height) ? vis_height : 0;
9989 else
9991 int last_line_y = it.current_y;
9992 move_it_by_lines (&it, 1);
9993 partial_height = (it.current_y > it.last_visible_y)
9994 ? it.last_visible_y - last_line_y : 0;
9996 RESTORE_IT (&it, &it, it_data);
9997 return partial_height;
10000 /* Return true if IT points into the middle of a display vector. */
10002 bool
10003 in_display_vector_p (struct it *it)
10005 return (it->method == GET_FROM_DISPLAY_VECTOR
10006 && it->current.dpvec_index > 0
10007 && it->dpvec + it->current.dpvec_index != it->dpend);
10010 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
10011 doc: /* Return the size of the text of WINDOW's buffer in pixels.
10012 WINDOW must be a live window and defaults to the selected one. The
10013 return value is a cons of the maximum pixel-width of any text line and
10014 the maximum pixel-height of all text lines.
10016 The optional argument FROM, if non-nil, specifies the first text
10017 position and defaults to the minimum accessible position of the buffer.
10018 If FROM is t, use the minimum accessible position that starts a
10019 non-empty line. TO, if non-nil, specifies the last text position and
10020 defaults to the maximum accessible position of the buffer. If TO is t,
10021 use the maximum accessible position that ends a non-empty line.
10023 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10024 width that can be returned. X-LIMIT nil or omitted, means to use the
10025 pixel-width of WINDOW's body; use this if you want to know how high
10026 WINDOW should be become in order to fit all of its buffer's text with
10027 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10028 if you intend to change WINDOW's width. In any case, text whose
10029 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10030 of long lines can take some time, it's always a good idea to make this
10031 argument as small as possible; in particular, if the buffer contains
10032 long lines that shall be truncated anyway.
10034 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10035 height (excluding the height of the mode- or header-line, if any) that
10036 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10037 ignored. Since calculating the text height of a large buffer can take
10038 some time, it makes sense to specify this argument if the size of the
10039 buffer is large or unknown.
10041 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10042 include the height of the mode- or header-line of WINDOW in the return
10043 value. If it is either the symbol `mode-line' or `header-line', include
10044 only the height of that line, if present, in the return value. If t,
10045 include the height of both, if present, in the return value. */)
10046 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10047 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10049 struct window *w = decode_live_window (window);
10050 Lisp_Object buffer = w->contents;
10051 struct buffer *b;
10052 struct it it;
10053 struct buffer *old_b = NULL;
10054 ptrdiff_t start, end, pos;
10055 struct text_pos startp;
10056 void *itdata = NULL;
10057 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10059 CHECK_BUFFER (buffer);
10060 b = XBUFFER (buffer);
10062 if (b != current_buffer)
10064 old_b = current_buffer;
10065 set_buffer_internal (b);
10068 if (NILP (from))
10069 start = BEGV;
10070 else if (EQ (from, Qt))
10072 start = pos = BEGV;
10073 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10074 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10075 start = pos;
10076 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10077 start = pos;
10079 else
10081 CHECK_NUMBER_COERCE_MARKER (from);
10082 start = min (max (XINT (from), BEGV), ZV);
10085 if (NILP (to))
10086 end = ZV;
10087 else if (EQ (to, Qt))
10089 end = pos = ZV;
10090 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10091 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10092 end = pos;
10093 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10094 end = pos;
10096 else
10098 CHECK_NUMBER_COERCE_MARKER (to);
10099 end = max (start, min (XINT (to), ZV));
10102 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10103 max_x = XINT (x_limit);
10105 if (NILP (y_limit))
10106 max_y = INT_MAX;
10107 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10108 max_y = XINT (y_limit);
10110 itdata = bidi_shelve_cache ();
10111 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10112 start_display (&it, w, startp);
10114 if (NILP (x_limit))
10115 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10116 else
10118 it.last_visible_x = max_x;
10119 /* Actually, we never want move_it_to stop at to_x. But to make
10120 sure that move_it_in_display_line_to always moves far enough,
10121 we set it to INT_MAX and specify MOVE_TO_X. */
10122 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10123 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10124 /* Don't return more than X-LIMIT. */
10125 if (x > max_x)
10126 x = max_x;
10129 /* Subtract height of header-line which was counted automatically by
10130 start_display. */
10131 y = it.current_y + it.max_ascent + it.max_descent
10132 - WINDOW_HEADER_LINE_HEIGHT (w);
10133 /* Don't return more than Y-LIMIT. */
10134 if (y > max_y)
10135 y = max_y;
10137 if (EQ (mode_and_header_line, Qheader_line)
10138 || EQ (mode_and_header_line, Qt))
10139 /* Re-add height of header-line as requested. */
10140 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10142 if (EQ (mode_and_header_line, Qmode_line)
10143 || EQ (mode_and_header_line, Qt))
10144 /* Add height of mode-line as requested. */
10145 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10147 bidi_unshelve_cache (itdata, false);
10149 if (old_b)
10150 set_buffer_internal (old_b);
10152 return Fcons (make_number (x), make_number (y));
10155 /***********************************************************************
10156 Messages
10157 ***********************************************************************/
10159 /* Return the number of arguments the format string FORMAT needs. */
10161 static ptrdiff_t
10162 format_nargs (char const *format)
10164 ptrdiff_t nargs = 0;
10165 for (char const *p = format; (p = strchr (p, '%')); p++)
10166 if (p[1] == '%')
10167 p++;
10168 else
10169 nargs++;
10170 return nargs;
10173 /* Add a message with format string FORMAT and formatted arguments
10174 to *Messages*. */
10176 void
10177 add_to_log (const char *format, ...)
10179 va_list ap;
10180 va_start (ap, format);
10181 vadd_to_log (format, ap);
10182 va_end (ap);
10185 void
10186 vadd_to_log (char const *format, va_list ap)
10188 ptrdiff_t form_nargs = format_nargs (format);
10189 ptrdiff_t nargs = 1 + form_nargs;
10190 Lisp_Object args[10];
10191 eassert (nargs <= ARRAYELTS (args));
10192 AUTO_STRING (args0, format);
10193 args[0] = args0;
10194 for (ptrdiff_t i = 1; i <= nargs; i++)
10195 args[i] = va_arg (ap, Lisp_Object);
10196 Lisp_Object msg = Qnil;
10197 msg = Fformat_message (nargs, args);
10199 ptrdiff_t len = SBYTES (msg) + 1;
10200 USE_SAFE_ALLOCA;
10201 char *buffer = SAFE_ALLOCA (len);
10202 memcpy (buffer, SDATA (msg), len);
10204 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10205 SAFE_FREE ();
10209 /* Output a newline in the *Messages* buffer if "needs" one. */
10211 void
10212 message_log_maybe_newline (void)
10214 if (message_log_need_newline)
10215 message_dolog ("", 0, true, false);
10219 /* Add a string M of length NBYTES to the message log, optionally
10220 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10221 true, means interpret the contents of M as multibyte. This
10222 function calls low-level routines in order to bypass text property
10223 hooks, etc. which might not be safe to run.
10225 This may GC (insert may run before/after change hooks),
10226 so the buffer M must NOT point to a Lisp string. */
10228 void
10229 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10231 const unsigned char *msg = (const unsigned char *) m;
10233 if (!NILP (Vmemory_full))
10234 return;
10236 if (!NILP (Vmessage_log_max))
10238 struct buffer *oldbuf;
10239 Lisp_Object oldpoint, oldbegv, oldzv;
10240 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10241 ptrdiff_t point_at_end = 0;
10242 ptrdiff_t zv_at_end = 0;
10243 Lisp_Object old_deactivate_mark;
10245 old_deactivate_mark = Vdeactivate_mark;
10246 oldbuf = current_buffer;
10248 /* Ensure the Messages buffer exists, and switch to it.
10249 If we created it, set the major-mode. */
10250 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10251 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10252 if (newbuffer
10253 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10254 call0 (intern ("messages-buffer-mode"));
10256 bset_undo_list (current_buffer, Qt);
10257 bset_cache_long_scans (current_buffer, Qnil);
10259 oldpoint = message_dolog_marker1;
10260 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10261 oldbegv = message_dolog_marker2;
10262 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10263 oldzv = message_dolog_marker3;
10264 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10266 if (PT == Z)
10267 point_at_end = 1;
10268 if (ZV == Z)
10269 zv_at_end = 1;
10271 BEGV = BEG;
10272 BEGV_BYTE = BEG_BYTE;
10273 ZV = Z;
10274 ZV_BYTE = Z_BYTE;
10275 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10277 /* Insert the string--maybe converting multibyte to single byte
10278 or vice versa, so that all the text fits the buffer. */
10279 if (multibyte
10280 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10282 ptrdiff_t i;
10283 int c, char_bytes;
10284 char work[1];
10286 /* Convert a multibyte string to single-byte
10287 for the *Message* buffer. */
10288 for (i = 0; i < nbytes; i += char_bytes)
10290 c = string_char_and_length (msg + i, &char_bytes);
10291 work[0] = CHAR_TO_BYTE8 (c);
10292 insert_1_both (work, 1, 1, true, false, false);
10295 else if (! multibyte
10296 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10298 ptrdiff_t i;
10299 int c, char_bytes;
10300 unsigned char str[MAX_MULTIBYTE_LENGTH];
10301 /* Convert a single-byte string to multibyte
10302 for the *Message* buffer. */
10303 for (i = 0; i < nbytes; i++)
10305 c = msg[i];
10306 MAKE_CHAR_MULTIBYTE (c);
10307 char_bytes = CHAR_STRING (c, str);
10308 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10311 else if (nbytes)
10312 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10313 true, false, false);
10315 if (nlflag)
10317 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10318 printmax_t dups;
10320 insert_1_both ("\n", 1, 1, true, false, false);
10322 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10323 this_bol = PT;
10324 this_bol_byte = PT_BYTE;
10326 /* See if this line duplicates the previous one.
10327 If so, combine duplicates. */
10328 if (this_bol > BEG)
10330 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10331 prev_bol = PT;
10332 prev_bol_byte = PT_BYTE;
10334 dups = message_log_check_duplicate (prev_bol_byte,
10335 this_bol_byte);
10336 if (dups)
10338 del_range_both (prev_bol, prev_bol_byte,
10339 this_bol, this_bol_byte, false);
10340 if (dups > 1)
10342 char dupstr[sizeof " [ times]"
10343 + INT_STRLEN_BOUND (printmax_t)];
10345 /* If you change this format, don't forget to also
10346 change message_log_check_duplicate. */
10347 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10348 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10349 insert_1_both (dupstr, duplen, duplen,
10350 true, false, true);
10355 /* If we have more than the desired maximum number of lines
10356 in the *Messages* buffer now, delete the oldest ones.
10357 This is safe because we don't have undo in this buffer. */
10359 if (NATNUMP (Vmessage_log_max))
10361 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10362 -XFASTINT (Vmessage_log_max) - 1, false);
10363 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10366 BEGV = marker_position (oldbegv);
10367 BEGV_BYTE = marker_byte_position (oldbegv);
10369 if (zv_at_end)
10371 ZV = Z;
10372 ZV_BYTE = Z_BYTE;
10374 else
10376 ZV = marker_position (oldzv);
10377 ZV_BYTE = marker_byte_position (oldzv);
10380 if (point_at_end)
10381 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10382 else
10383 /* We can't do Fgoto_char (oldpoint) because it will run some
10384 Lisp code. */
10385 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10386 marker_byte_position (oldpoint));
10388 unchain_marker (XMARKER (oldpoint));
10389 unchain_marker (XMARKER (oldbegv));
10390 unchain_marker (XMARKER (oldzv));
10392 /* We called insert_1_both above with its 5th argument (PREPARE)
10393 false, which prevents insert_1_both from calling
10394 prepare_to_modify_buffer, which in turns prevents us from
10395 incrementing windows_or_buffers_changed even if *Messages* is
10396 shown in some window. So we must manually set
10397 windows_or_buffers_changed here to make up for that. */
10398 windows_or_buffers_changed = old_windows_or_buffers_changed;
10399 bset_redisplay (current_buffer);
10401 set_buffer_internal (oldbuf);
10403 message_log_need_newline = !nlflag;
10404 Vdeactivate_mark = old_deactivate_mark;
10409 /* We are at the end of the buffer after just having inserted a newline.
10410 (Note: We depend on the fact we won't be crossing the gap.)
10411 Check to see if the most recent message looks a lot like the previous one.
10412 Return 0 if different, 1 if the new one should just replace it, or a
10413 value N > 1 if we should also append " [N times]". */
10415 static intmax_t
10416 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10418 ptrdiff_t i;
10419 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10420 bool seen_dots = false;
10421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10424 for (i = 0; i < len; i++)
10426 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10427 seen_dots = true;
10428 if (p1[i] != p2[i])
10429 return seen_dots;
10431 p1 += len;
10432 if (*p1 == '\n')
10433 return 2;
10434 if (*p1++ == ' ' && *p1++ == '[')
10436 char *pend;
10437 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10438 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10439 return n + 1;
10441 return 0;
10445 /* Display an echo area message M with a specified length of NBYTES
10446 bytes. The string may include null characters. If M is not a
10447 string, clear out any existing message, and let the mini-buffer
10448 text show through.
10450 This function cancels echoing. */
10452 void
10453 message3 (Lisp_Object m)
10455 clear_message (true, true);
10456 cancel_echoing ();
10458 /* First flush out any partial line written with print. */
10459 message_log_maybe_newline ();
10460 if (STRINGP (m))
10462 ptrdiff_t nbytes = SBYTES (m);
10463 bool multibyte = STRING_MULTIBYTE (m);
10464 char *buffer;
10465 USE_SAFE_ALLOCA;
10466 SAFE_ALLOCA_STRING (buffer, m);
10467 message_dolog (buffer, nbytes, true, multibyte);
10468 SAFE_FREE ();
10470 if (! inhibit_message)
10471 message3_nolog (m);
10474 /* Log the message M to stderr. Log an empty line if M is not a string. */
10476 static void
10477 message_to_stderr (Lisp_Object m)
10479 if (noninteractive_need_newline)
10481 noninteractive_need_newline = false;
10482 fputc ('\n', stderr);
10484 if (STRINGP (m))
10486 Lisp_Object coding_system = Vlocale_coding_system;
10487 Lisp_Object s;
10489 if (!NILP (Vcoding_system_for_write))
10490 coding_system = Vcoding_system_for_write;
10491 if (!NILP (coding_system))
10492 s = code_convert_string_norecord (m, coding_system, true);
10493 else
10494 s = m;
10496 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10498 if (!cursor_in_echo_area)
10499 fputc ('\n', stderr);
10500 fflush (stderr);
10503 /* The non-logging version of message3.
10504 This does not cancel echoing, because it is used for echoing.
10505 Perhaps we need to make a separate function for echoing
10506 and make this cancel echoing. */
10508 void
10509 message3_nolog (Lisp_Object m)
10511 struct frame *sf = SELECTED_FRAME ();
10513 if (FRAME_INITIAL_P (sf))
10514 message_to_stderr (m);
10515 /* Error messages get reported properly by cmd_error, so this must be just an
10516 informative message; if the frame hasn't really been initialized yet, just
10517 toss it. */
10518 else if (INTERACTIVE && sf->glyphs_initialized_p)
10520 /* Get the frame containing the mini-buffer
10521 that the selected frame is using. */
10522 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10523 Lisp_Object frame = XWINDOW (mini_window)->frame;
10524 struct frame *f = XFRAME (frame);
10526 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10527 Fmake_frame_visible (frame);
10529 if (STRINGP (m) && SCHARS (m) > 0)
10531 set_message (m);
10532 if (minibuffer_auto_raise)
10533 Fraise_frame (frame);
10534 /* Assume we are not echoing.
10535 (If we are, echo_now will override this.) */
10536 echo_message_buffer = Qnil;
10538 else
10539 clear_message (true, true);
10541 do_pending_window_change (false);
10542 echo_area_display (true);
10543 do_pending_window_change (false);
10544 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10545 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10550 /* Display a null-terminated echo area message M. If M is 0, clear
10551 out any existing message, and let the mini-buffer text show through.
10553 The buffer M must continue to exist until after the echo area gets
10554 cleared or some other message gets displayed there. Do not pass
10555 text that is stored in a Lisp string. Do not pass text in a buffer
10556 that was alloca'd. */
10558 void
10559 message1 (const char *m)
10561 message3 (m ? build_unibyte_string (m) : Qnil);
10565 /* The non-logging counterpart of message1. */
10567 void
10568 message1_nolog (const char *m)
10570 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10573 /* Display a message M which contains a single %s
10574 which gets replaced with STRING. */
10576 void
10577 message_with_string (const char *m, Lisp_Object string, bool log)
10579 CHECK_STRING (string);
10581 bool need_message;
10582 if (noninteractive)
10583 need_message = !!m;
10584 else if (!INTERACTIVE)
10585 need_message = false;
10586 else
10588 /* The frame whose minibuffer we're going to display the message on.
10589 It may be larger than the selected frame, so we need
10590 to use its buffer, not the selected frame's buffer. */
10591 Lisp_Object mini_window;
10592 struct frame *f, *sf = SELECTED_FRAME ();
10594 /* Get the frame containing the minibuffer
10595 that the selected frame is using. */
10596 mini_window = FRAME_MINIBUF_WINDOW (sf);
10597 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10599 /* Error messages get reported properly by cmd_error, so this must be
10600 just an informative message; if the frame hasn't really been
10601 initialized yet, just toss it. */
10602 need_message = f->glyphs_initialized_p;
10605 if (need_message)
10607 AUTO_STRING (fmt, m);
10608 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10610 if (noninteractive)
10611 message_to_stderr (msg);
10612 else
10614 if (log)
10615 message3 (msg);
10616 else
10617 message3_nolog (msg);
10619 /* Print should start at the beginning of the message
10620 buffer next time. */
10621 message_buf_print = false;
10627 /* Dump an informative message to the minibuf. If M is 0, clear out
10628 any existing message, and let the mini-buffer text show through.
10630 The message must be safe ASCII (because when Emacs is
10631 non-interactive the message is sent straight to stderr without
10632 encoding first) and the format must not contain ` or ' (because
10633 this function does not account for `text-quoting-style'). If your
10634 message and format do not fit into this category, convert your
10635 arguments to Lisp objects and use Fmessage instead. */
10637 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10638 vmessage (const char *m, va_list ap)
10640 if (noninteractive)
10642 if (m)
10644 if (noninteractive_need_newline)
10645 putc ('\n', stderr);
10646 noninteractive_need_newline = false;
10647 vfprintf (stderr, m, ap);
10648 if (!cursor_in_echo_area)
10649 fprintf (stderr, "\n");
10650 fflush (stderr);
10653 else if (INTERACTIVE)
10655 /* The frame whose mini-buffer we're going to display the message
10656 on. It may be larger than the selected frame, so we need to
10657 use its buffer, not the selected frame's buffer. */
10658 Lisp_Object mini_window;
10659 struct frame *f, *sf = SELECTED_FRAME ();
10661 /* Get the frame containing the mini-buffer
10662 that the selected frame is using. */
10663 mini_window = FRAME_MINIBUF_WINDOW (sf);
10664 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10666 /* Error messages get reported properly by cmd_error, so this must be
10667 just an informative message; if the frame hasn't really been
10668 initialized yet, just toss it. */
10669 if (f->glyphs_initialized_p)
10671 if (m)
10673 ptrdiff_t len;
10674 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10675 USE_SAFE_ALLOCA;
10676 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10678 len = doprnt (message_buf, maxsize, m, 0, ap);
10680 message3 (make_string (message_buf, len));
10681 SAFE_FREE ();
10683 else
10684 message1 (0);
10686 /* Print should start at the beginning of the message
10687 buffer next time. */
10688 message_buf_print = false;
10693 /* See vmessage for restrictions on the text of the message. */
10694 void
10695 message (const char *m, ...)
10697 va_list ap;
10698 va_start (ap, m);
10699 vmessage (m, ap);
10700 va_end (ap);
10704 /* Display the current message in the current mini-buffer. This is
10705 only called from error handlers in process.c, and is not time
10706 critical. */
10708 void
10709 update_echo_area (void)
10711 if (!NILP (echo_area_buffer[0]))
10713 Lisp_Object string;
10714 string = Fcurrent_message ();
10715 message3 (string);
10720 /* Make sure echo area buffers in `echo_buffers' are live.
10721 If they aren't, make new ones. */
10723 static void
10724 ensure_echo_area_buffers (void)
10726 for (int i = 0; i < 2; i++)
10727 if (!BUFFERP (echo_buffer[i])
10728 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10730 Lisp_Object old_buffer = echo_buffer[i];
10731 static char const name_fmt[] = " *Echo Area %d*";
10732 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10733 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10734 echo_buffer[i] = Fget_buffer_create (lname);
10735 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10736 /* to force word wrap in echo area -
10737 it was decided to postpone this*/
10738 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10740 for (int j = 0; j < 2; j++)
10741 if (EQ (old_buffer, echo_area_buffer[j]))
10742 echo_area_buffer[j] = echo_buffer[i];
10747 /* Call FN with args A1..A2 with either the current or last displayed
10748 echo_area_buffer as current buffer.
10750 WHICH zero means use the current message buffer
10751 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10752 from echo_buffer[] and clear it.
10754 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10755 suitable buffer from echo_buffer[] and clear it.
10757 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10758 that the current message becomes the last displayed one, choose a
10759 suitable buffer for echo_area_buffer[0], and clear it.
10761 Value is what FN returns. */
10763 static bool
10764 with_echo_area_buffer (struct window *w, int which,
10765 bool (*fn) (ptrdiff_t, Lisp_Object),
10766 ptrdiff_t a1, Lisp_Object a2)
10768 Lisp_Object buffer;
10769 bool this_one, the_other, clear_buffer_p, rc;
10770 ptrdiff_t count = SPECPDL_INDEX ();
10772 /* If buffers aren't live, make new ones. */
10773 ensure_echo_area_buffers ();
10775 clear_buffer_p = false;
10777 if (which == 0)
10778 this_one = false, the_other = true;
10779 else if (which > 0)
10780 this_one = true, the_other = false;
10781 else
10783 this_one = false, the_other = true;
10784 clear_buffer_p = true;
10786 /* We need a fresh one in case the current echo buffer equals
10787 the one containing the last displayed echo area message. */
10788 if (!NILP (echo_area_buffer[this_one])
10789 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10790 echo_area_buffer[this_one] = Qnil;
10793 /* Choose a suitable buffer from echo_buffer[] if we don't
10794 have one. */
10795 if (NILP (echo_area_buffer[this_one]))
10797 echo_area_buffer[this_one]
10798 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10799 ? echo_buffer[the_other]
10800 : echo_buffer[this_one]);
10801 clear_buffer_p = true;
10804 buffer = echo_area_buffer[this_one];
10806 /* Don't get confused by reusing the buffer used for echoing
10807 for a different purpose. */
10808 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10809 cancel_echoing ();
10811 record_unwind_protect (unwind_with_echo_area_buffer,
10812 with_echo_area_buffer_unwind_data (w));
10814 /* Make the echo area buffer current. Note that for display
10815 purposes, it is not necessary that the displayed window's buffer
10816 == current_buffer, except for text property lookup. So, let's
10817 only set that buffer temporarily here without doing a full
10818 Fset_window_buffer. We must also change w->pointm, though,
10819 because otherwise an assertions in unshow_buffer fails, and Emacs
10820 aborts. */
10821 set_buffer_internal_1 (XBUFFER (buffer));
10822 if (w)
10824 wset_buffer (w, buffer);
10825 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10826 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10829 bset_undo_list (current_buffer, Qt);
10830 bset_read_only (current_buffer, Qnil);
10831 specbind (Qinhibit_read_only, Qt);
10832 specbind (Qinhibit_modification_hooks, Qt);
10834 if (clear_buffer_p && Z > BEG)
10835 del_range (BEG, Z);
10837 eassert (BEGV >= BEG);
10838 eassert (ZV <= Z && ZV >= BEGV);
10840 rc = fn (a1, a2);
10842 eassert (BEGV >= BEG);
10843 eassert (ZV <= Z && ZV >= BEGV);
10845 unbind_to (count, Qnil);
10846 return rc;
10850 /* Save state that should be preserved around the call to the function
10851 FN called in with_echo_area_buffer. */
10853 static Lisp_Object
10854 with_echo_area_buffer_unwind_data (struct window *w)
10856 int i = 0;
10857 Lisp_Object vector, tmp;
10859 /* Reduce consing by keeping one vector in
10860 Vwith_echo_area_save_vector. */
10861 vector = Vwith_echo_area_save_vector;
10862 Vwith_echo_area_save_vector = Qnil;
10864 if (NILP (vector))
10865 vector = Fmake_vector (make_number (11), Qnil);
10867 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10868 ASET (vector, i, Vdeactivate_mark); ++i;
10869 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10871 if (w)
10873 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10874 ASET (vector, i, w->contents); ++i;
10875 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10876 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10877 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10878 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10879 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10880 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10882 else
10884 int end = i + 8;
10885 for (; i < end; ++i)
10886 ASET (vector, i, Qnil);
10889 eassert (i == ASIZE (vector));
10890 return vector;
10894 /* Restore global state from VECTOR which was created by
10895 with_echo_area_buffer_unwind_data. */
10897 static void
10898 unwind_with_echo_area_buffer (Lisp_Object vector)
10900 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10901 Vdeactivate_mark = AREF (vector, 1);
10902 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10904 if (WINDOWP (AREF (vector, 3)))
10906 struct window *w;
10907 Lisp_Object buffer;
10909 w = XWINDOW (AREF (vector, 3));
10910 buffer = AREF (vector, 4);
10912 wset_buffer (w, buffer);
10913 set_marker_both (w->pointm, buffer,
10914 XFASTINT (AREF (vector, 5)),
10915 XFASTINT (AREF (vector, 6)));
10916 set_marker_both (w->old_pointm, buffer,
10917 XFASTINT (AREF (vector, 7)),
10918 XFASTINT (AREF (vector, 8)));
10919 set_marker_both (w->start, buffer,
10920 XFASTINT (AREF (vector, 9)),
10921 XFASTINT (AREF (vector, 10)));
10924 Vwith_echo_area_save_vector = vector;
10928 /* Set up the echo area for use by print functions. MULTIBYTE_P
10929 means we will print multibyte. */
10931 void
10932 setup_echo_area_for_printing (bool multibyte_p)
10934 /* If we can't find an echo area any more, exit. */
10935 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10936 Fkill_emacs (Qnil);
10938 ensure_echo_area_buffers ();
10940 if (!message_buf_print)
10942 /* A message has been output since the last time we printed.
10943 Choose a fresh echo area buffer. */
10944 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10945 echo_area_buffer[0] = echo_buffer[1];
10946 else
10947 echo_area_buffer[0] = echo_buffer[0];
10949 /* Switch to that buffer and clear it. */
10950 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10951 bset_truncate_lines (current_buffer, Qnil);
10953 if (Z > BEG)
10955 ptrdiff_t count = SPECPDL_INDEX ();
10956 specbind (Qinhibit_read_only, Qt);
10957 /* Note that undo recording is always disabled. */
10958 del_range (BEG, Z);
10959 unbind_to (count, Qnil);
10961 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10963 /* Set up the buffer for the multibyteness we need. */
10964 if (multibyte_p
10965 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10966 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10968 /* Raise the frame containing the echo area. */
10969 if (minibuffer_auto_raise)
10971 struct frame *sf = SELECTED_FRAME ();
10972 Lisp_Object mini_window;
10973 mini_window = FRAME_MINIBUF_WINDOW (sf);
10974 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10977 message_log_maybe_newline ();
10978 message_buf_print = true;
10980 else
10982 if (NILP (echo_area_buffer[0]))
10984 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10985 echo_area_buffer[0] = echo_buffer[1];
10986 else
10987 echo_area_buffer[0] = echo_buffer[0];
10990 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10992 /* Someone switched buffers between print requests. */
10993 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10994 bset_truncate_lines (current_buffer, Qnil);
11000 /* Display an echo area message in window W. Value is true if W's
11001 height is changed. If display_last_displayed_message_p,
11002 display the message that was last displayed, otherwise
11003 display the current message. */
11005 static bool
11006 display_echo_area (struct window *w)
11008 bool no_message_p, window_height_changed_p;
11010 /* Temporarily disable garbage collections while displaying the echo
11011 area. This is done because a GC can print a message itself.
11012 That message would modify the echo area buffer's contents while a
11013 redisplay of the buffer is going on, and seriously confuse
11014 redisplay. */
11015 ptrdiff_t count = inhibit_garbage_collection ();
11017 /* If there is no message, we must call display_echo_area_1
11018 nevertheless because it resizes the window. But we will have to
11019 reset the echo_area_buffer in question to nil at the end because
11020 with_echo_area_buffer will sets it to an empty buffer. */
11021 bool i = display_last_displayed_message_p;
11022 /* According to the C99, C11 and C++11 standards, the integral value
11023 of a "bool" is always 0 or 1, so this array access is safe here,
11024 if oddly typed. */
11025 no_message_p = NILP (echo_area_buffer[i]);
11027 window_height_changed_p
11028 = with_echo_area_buffer (w, display_last_displayed_message_p,
11029 display_echo_area_1,
11030 (intptr_t) w, Qnil);
11032 if (no_message_p)
11033 echo_area_buffer[i] = Qnil;
11035 unbind_to (count, Qnil);
11036 return window_height_changed_p;
11040 /* Helper for display_echo_area. Display the current buffer which
11041 contains the current echo area message in window W, a mini-window,
11042 a pointer to which is passed in A1. A2..A4 are currently not used.
11043 Change the height of W so that all of the message is displayed.
11044 Value is true if height of W was changed. */
11046 static bool
11047 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11049 intptr_t i1 = a1;
11050 struct window *w = (struct window *) i1;
11051 Lisp_Object window;
11052 struct text_pos start;
11054 /* We are about to enter redisplay without going through
11055 redisplay_internal, so we need to forget these faces by hand
11056 here. */
11057 forget_escape_and_glyphless_faces ();
11059 /* Do this before displaying, so that we have a large enough glyph
11060 matrix for the display. If we can't get enough space for the
11061 whole text, display the last N lines. That works by setting w->start. */
11062 bool window_height_changed_p = resize_mini_window (w, false);
11064 /* Use the starting position chosen by resize_mini_window. */
11065 SET_TEXT_POS_FROM_MARKER (start, w->start);
11067 /* Display. */
11068 clear_glyph_matrix (w->desired_matrix);
11069 XSETWINDOW (window, w);
11070 try_window (window, start, 0);
11072 return window_height_changed_p;
11076 /* Resize the echo area window to exactly the size needed for the
11077 currently displayed message, if there is one. If a mini-buffer
11078 is active, don't shrink it. */
11080 void
11081 resize_echo_area_exactly (void)
11083 if (BUFFERP (echo_area_buffer[0])
11084 && WINDOWP (echo_area_window))
11086 struct window *w = XWINDOW (echo_area_window);
11087 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11088 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11089 (intptr_t) w, resize_exactly);
11090 if (resized_p)
11092 windows_or_buffers_changed = 42;
11093 update_mode_lines = 30;
11094 redisplay_internal ();
11100 /* Callback function for with_echo_area_buffer, when used from
11101 resize_echo_area_exactly. A1 contains a pointer to the window to
11102 resize, EXACTLY non-nil means resize the mini-window exactly to the
11103 size of the text displayed. A3 and A4 are not used. Value is what
11104 resize_mini_window returns. */
11106 static bool
11107 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11109 intptr_t i1 = a1;
11110 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11114 /* Resize mini-window W to fit the size of its contents. EXACT_P
11115 means size the window exactly to the size needed. Otherwise, it's
11116 only enlarged until W's buffer is empty.
11118 Set W->start to the right place to begin display. If the whole
11119 contents fit, start at the beginning. Otherwise, start so as
11120 to make the end of the contents appear. This is particularly
11121 important for y-or-n-p, but seems desirable generally.
11123 Value is true if the window height has been changed. */
11125 bool
11126 resize_mini_window (struct window *w, bool exact_p)
11128 struct frame *f = XFRAME (w->frame);
11129 bool window_height_changed_p = false;
11131 eassert (MINI_WINDOW_P (w));
11133 /* By default, start display at the beginning. */
11134 set_marker_both (w->start, w->contents,
11135 BUF_BEGV (XBUFFER (w->contents)),
11136 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11138 /* Don't resize windows while redisplaying a window; it would
11139 confuse redisplay functions when the size of the window they are
11140 displaying changes from under them. Such a resizing can happen,
11141 for instance, when which-func prints a long message while
11142 we are running fontification-functions. We're running these
11143 functions with safe_call which binds inhibit-redisplay to t. */
11144 if (!NILP (Vinhibit_redisplay))
11145 return false;
11147 /* Nil means don't try to resize. */
11148 if (NILP (Vresize_mini_windows)
11149 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11150 return false;
11152 if (!FRAME_MINIBUF_ONLY_P (f))
11154 struct it it;
11155 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11156 + WINDOW_PIXEL_HEIGHT (w));
11157 int unit = FRAME_LINE_HEIGHT (f);
11158 int height, max_height;
11159 struct text_pos start;
11160 struct buffer *old_current_buffer = NULL;
11162 if (current_buffer != XBUFFER (w->contents))
11164 old_current_buffer = current_buffer;
11165 set_buffer_internal (XBUFFER (w->contents));
11168 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11170 /* Compute the max. number of lines specified by the user. */
11171 if (FLOATP (Vmax_mini_window_height))
11172 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11173 else if (INTEGERP (Vmax_mini_window_height))
11174 max_height = XINT (Vmax_mini_window_height) * unit;
11175 else
11176 max_height = total_height / 4;
11178 /* Correct that max. height if it's bogus. */
11179 max_height = clip_to_bounds (unit, max_height, total_height);
11181 /* Find out the height of the text in the window. */
11182 if (it.line_wrap == TRUNCATE)
11183 height = unit;
11184 else
11186 last_height = 0;
11187 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11188 if (it.max_ascent == 0 && it.max_descent == 0)
11189 height = it.current_y + last_height;
11190 else
11191 height = it.current_y + it.max_ascent + it.max_descent;
11192 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11195 /* Compute a suitable window start. */
11196 if (height > max_height)
11198 height = (max_height / unit) * unit;
11199 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11200 move_it_vertically_backward (&it, height - unit);
11201 start = it.current.pos;
11203 else
11204 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11205 SET_MARKER_FROM_TEXT_POS (w->start, start);
11207 if (EQ (Vresize_mini_windows, Qgrow_only))
11209 /* Let it grow only, until we display an empty message, in which
11210 case the window shrinks again. */
11211 if (height > WINDOW_PIXEL_HEIGHT (w))
11213 int old_height = WINDOW_PIXEL_HEIGHT (w);
11215 FRAME_WINDOWS_FROZEN (f) = true;
11216 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11217 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11219 else if (height < WINDOW_PIXEL_HEIGHT (w)
11220 && (exact_p || BEGV == ZV))
11222 int old_height = WINDOW_PIXEL_HEIGHT (w);
11224 FRAME_WINDOWS_FROZEN (f) = false;
11225 shrink_mini_window (w, true);
11226 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11229 else
11231 /* Always resize to exact size needed. */
11232 if (height > WINDOW_PIXEL_HEIGHT (w))
11234 int old_height = WINDOW_PIXEL_HEIGHT (w);
11236 FRAME_WINDOWS_FROZEN (f) = true;
11237 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11238 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11240 else if (height < WINDOW_PIXEL_HEIGHT (w))
11242 int old_height = WINDOW_PIXEL_HEIGHT (w);
11244 FRAME_WINDOWS_FROZEN (f) = false;
11245 shrink_mini_window (w, true);
11247 if (height)
11249 FRAME_WINDOWS_FROZEN (f) = true;
11250 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11253 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11257 if (old_current_buffer)
11258 set_buffer_internal (old_current_buffer);
11261 return window_height_changed_p;
11265 /* Value is the current message, a string, or nil if there is no
11266 current message. */
11268 Lisp_Object
11269 current_message (void)
11271 Lisp_Object msg;
11273 if (!BUFFERP (echo_area_buffer[0]))
11274 msg = Qnil;
11275 else
11277 with_echo_area_buffer (0, 0, current_message_1,
11278 (intptr_t) &msg, Qnil);
11279 if (NILP (msg))
11280 echo_area_buffer[0] = Qnil;
11283 return msg;
11287 static bool
11288 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11290 intptr_t i1 = a1;
11291 Lisp_Object *msg = (Lisp_Object *) i1;
11293 if (Z > BEG)
11294 *msg = make_buffer_string (BEG, Z, true);
11295 else
11296 *msg = Qnil;
11297 return false;
11301 /* Push the current message on Vmessage_stack for later restoration
11302 by restore_message. Value is true if the current message isn't
11303 empty. This is a relatively infrequent operation, so it's not
11304 worth optimizing. */
11306 bool
11307 push_message (void)
11309 Lisp_Object msg = current_message ();
11310 Vmessage_stack = Fcons (msg, Vmessage_stack);
11311 return STRINGP (msg);
11315 /* Restore message display from the top of Vmessage_stack. */
11317 void
11318 restore_message (void)
11320 eassert (CONSP (Vmessage_stack));
11321 message3_nolog (XCAR (Vmessage_stack));
11325 /* Handler for unwind-protect calling pop_message. */
11327 void
11328 pop_message_unwind (void)
11330 /* Pop the top-most entry off Vmessage_stack. */
11331 eassert (CONSP (Vmessage_stack));
11332 Vmessage_stack = XCDR (Vmessage_stack);
11336 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11337 exits. If the stack is not empty, we have a missing pop_message
11338 somewhere. */
11340 void
11341 check_message_stack (void)
11343 if (!NILP (Vmessage_stack))
11344 emacs_abort ();
11348 /* Truncate to NCHARS what will be displayed in the echo area the next
11349 time we display it---but don't redisplay it now. */
11351 void
11352 truncate_echo_area (ptrdiff_t nchars)
11354 if (nchars == 0)
11355 echo_area_buffer[0] = Qnil;
11356 else if (!noninteractive
11357 && INTERACTIVE
11358 && !NILP (echo_area_buffer[0]))
11360 struct frame *sf = SELECTED_FRAME ();
11361 /* Error messages get reported properly by cmd_error, so this must be
11362 just an informative message; if the frame hasn't really been
11363 initialized yet, just toss it. */
11364 if (sf->glyphs_initialized_p)
11365 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11370 /* Helper function for truncate_echo_area. Truncate the current
11371 message to at most NCHARS characters. */
11373 static bool
11374 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11376 if (BEG + nchars < Z)
11377 del_range (BEG + nchars, Z);
11378 if (Z == BEG)
11379 echo_area_buffer[0] = Qnil;
11380 return false;
11383 /* Set the current message to STRING. */
11385 static void
11386 set_message (Lisp_Object string)
11388 eassert (STRINGP (string));
11390 message_enable_multibyte = STRING_MULTIBYTE (string);
11392 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11393 message_buf_print = false;
11394 help_echo_showing_p = false;
11396 if (STRINGP (Vdebug_on_message)
11397 && STRINGP (string)
11398 && fast_string_match (Vdebug_on_message, string) >= 0)
11399 call_debugger (list2 (Qerror, string));
11403 /* Helper function for set_message. First argument is ignored and second
11404 argument has the same meaning as for set_message.
11405 This function is called with the echo area buffer being current. */
11407 static bool
11408 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11410 eassert (STRINGP (string));
11412 /* Change multibyteness of the echo buffer appropriately. */
11413 if (message_enable_multibyte
11414 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11415 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11417 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11418 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11419 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11421 /* Insert new message at BEG. */
11422 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11424 /* This function takes care of single/multibyte conversion.
11425 We just have to ensure that the echo area buffer has the right
11426 setting of enable_multibyte_characters. */
11427 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11429 return false;
11433 /* Clear messages. CURRENT_P means clear the current message.
11434 LAST_DISPLAYED_P means clear the message last displayed. */
11436 void
11437 clear_message (bool current_p, bool last_displayed_p)
11439 if (current_p)
11441 echo_area_buffer[0] = Qnil;
11442 message_cleared_p = true;
11445 if (last_displayed_p)
11446 echo_area_buffer[1] = Qnil;
11448 message_buf_print = false;
11451 /* Clear garbaged frames.
11453 This function is used where the old redisplay called
11454 redraw_garbaged_frames which in turn called redraw_frame which in
11455 turn called clear_frame. The call to clear_frame was a source of
11456 flickering. I believe a clear_frame is not necessary. It should
11457 suffice in the new redisplay to invalidate all current matrices,
11458 and ensure a complete redisplay of all windows. */
11460 static void
11461 clear_garbaged_frames (void)
11463 if (frame_garbaged)
11465 Lisp_Object tail, frame;
11466 struct frame *sf = SELECTED_FRAME ();
11468 FOR_EACH_FRAME (tail, frame)
11470 struct frame *f = XFRAME (frame);
11472 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11474 if (f->resized_p
11475 /* It makes no sense to redraw a non-selected TTY
11476 frame, since that will actually clear the
11477 selected frame, and might leave the selected
11478 frame with corrupted display, if it happens not
11479 to be marked garbaged. */
11480 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11481 redraw_frame (f);
11482 else
11483 clear_current_matrices (f);
11485 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11486 x_clear_under_internal_border (f);
11487 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11489 fset_redisplay (f);
11490 f->garbaged = false;
11491 f->resized_p = false;
11495 frame_garbaged = false;
11500 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11501 selected_frame. */
11503 static void
11504 echo_area_display (bool update_frame_p)
11506 Lisp_Object mini_window;
11507 struct window *w;
11508 struct frame *f;
11509 bool window_height_changed_p = false;
11510 struct frame *sf = SELECTED_FRAME ();
11512 mini_window = FRAME_MINIBUF_WINDOW (sf);
11513 if (NILP (mini_window))
11514 return;
11516 w = XWINDOW (mini_window);
11517 f = XFRAME (WINDOW_FRAME (w));
11519 /* Don't display if frame is invisible or not yet initialized. */
11520 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11521 return;
11523 #ifdef HAVE_WINDOW_SYSTEM
11524 /* When Emacs starts, selected_frame may be the initial terminal
11525 frame. If we let this through, a message would be displayed on
11526 the terminal. */
11527 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11528 return;
11529 #endif /* HAVE_WINDOW_SYSTEM */
11531 /* Redraw garbaged frames. */
11532 clear_garbaged_frames ();
11534 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11536 echo_area_window = mini_window;
11537 window_height_changed_p = display_echo_area (w);
11538 w->must_be_updated_p = true;
11540 /* Update the display, unless called from redisplay_internal.
11541 Also don't update the screen during redisplay itself. The
11542 update will happen at the end of redisplay, and an update
11543 here could cause confusion. */
11544 if (update_frame_p && !redisplaying_p)
11546 int n = 0;
11548 /* If the display update has been interrupted by pending
11549 input, update mode lines in the frame. Due to the
11550 pending input, it might have been that redisplay hasn't
11551 been called, so that mode lines above the echo area are
11552 garbaged. This looks odd, so we prevent it here. */
11553 if (!display_completed)
11555 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11557 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11558 x_clear_under_internal_border (f);
11559 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11563 if (window_height_changed_p
11564 /* Don't do this if Emacs is shutting down. Redisplay
11565 needs to run hooks. */
11566 && !NILP (Vrun_hooks))
11568 /* Must update other windows. Likewise as in other
11569 cases, don't let this update be interrupted by
11570 pending input. */
11571 ptrdiff_t count = SPECPDL_INDEX ();
11572 specbind (Qredisplay_dont_pause, Qt);
11573 fset_redisplay (f);
11574 redisplay_internal ();
11575 unbind_to (count, Qnil);
11577 else if (FRAME_WINDOW_P (f) && n == 0)
11579 /* Window configuration is the same as before.
11580 Can do with a display update of the echo area,
11581 unless we displayed some mode lines. */
11582 update_single_window (w);
11583 flush_frame (f);
11585 else
11586 update_frame (f, true, true);
11588 /* If cursor is in the echo area, make sure that the next
11589 redisplay displays the minibuffer, so that the cursor will
11590 be replaced with what the minibuffer wants. */
11591 if (cursor_in_echo_area)
11592 wset_redisplay (XWINDOW (mini_window));
11595 else if (!EQ (mini_window, selected_window))
11596 wset_redisplay (XWINDOW (mini_window));
11598 /* Last displayed message is now the current message. */
11599 echo_area_buffer[1] = echo_area_buffer[0];
11600 /* Inform read_char that we're not echoing. */
11601 echo_message_buffer = Qnil;
11603 /* Prevent redisplay optimization in redisplay_internal by resetting
11604 this_line_start_pos. This is done because the mini-buffer now
11605 displays the message instead of its buffer text. */
11606 if (EQ (mini_window, selected_window))
11607 CHARPOS (this_line_start_pos) = 0;
11609 if (window_height_changed_p)
11611 fset_redisplay (f);
11613 /* If window configuration was changed, frames may have been
11614 marked garbaged. Clear them or we will experience
11615 surprises wrt scrolling.
11616 FIXME: How/why/when? */
11617 clear_garbaged_frames ();
11621 /* True if W's buffer was changed but not saved. */
11623 static bool
11624 window_buffer_changed (struct window *w)
11626 struct buffer *b = XBUFFER (w->contents);
11628 eassert (BUFFER_LIVE_P (b));
11630 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11633 /* True if W has %c or %C in its mode line and mode line should be updated. */
11635 static bool
11636 mode_line_update_needed (struct window *w)
11638 return (w->column_number_displayed != -1
11639 && !(PT == w->last_point && !window_outdated (w))
11640 && (w->column_number_displayed != current_column ()));
11643 /* True if window start of W is frozen and may not be changed during
11644 redisplay. */
11646 static bool
11647 window_frozen_p (struct window *w)
11649 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11651 Lisp_Object window;
11653 XSETWINDOW (window, w);
11654 if (MINI_WINDOW_P (w))
11655 return false;
11656 else if (EQ (window, selected_window))
11657 return false;
11658 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11659 && EQ (window, Vminibuf_scroll_window))
11660 /* This special window can't be frozen too. */
11661 return false;
11662 else
11663 return true;
11665 return false;
11668 /***********************************************************************
11669 Mode Lines and Frame Titles
11670 ***********************************************************************/
11672 /* A buffer for constructing non-propertized mode-line strings and
11673 frame titles in it; allocated from the heap in init_xdisp and
11674 resized as needed in store_mode_line_noprop_char. */
11676 static char *mode_line_noprop_buf;
11678 /* The buffer's end, and a current output position in it. */
11680 static char *mode_line_noprop_buf_end;
11681 static char *mode_line_noprop_ptr;
11683 #define MODE_LINE_NOPROP_LEN(start) \
11684 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11686 static enum {
11687 MODE_LINE_DISPLAY = 0,
11688 MODE_LINE_TITLE,
11689 MODE_LINE_NOPROP,
11690 MODE_LINE_STRING
11691 } mode_line_target;
11693 /* Alist that caches the results of :propertize.
11694 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11695 static Lisp_Object mode_line_proptrans_alist;
11697 /* List of strings making up the mode-line. */
11698 static Lisp_Object mode_line_string_list;
11700 /* Base face property when building propertized mode line string. */
11701 static Lisp_Object mode_line_string_face;
11702 static Lisp_Object mode_line_string_face_prop;
11705 /* Unwind data for mode line strings */
11707 static Lisp_Object Vmode_line_unwind_vector;
11709 static Lisp_Object
11710 format_mode_line_unwind_data (struct frame *target_frame,
11711 struct buffer *obuf,
11712 Lisp_Object owin,
11713 bool save_proptrans)
11715 Lisp_Object vector, tmp;
11717 /* Reduce consing by keeping one vector in
11718 Vwith_echo_area_save_vector. */
11719 vector = Vmode_line_unwind_vector;
11720 Vmode_line_unwind_vector = Qnil;
11722 if (NILP (vector))
11723 vector = Fmake_vector (make_number (10), Qnil);
11725 ASET (vector, 0, make_number (mode_line_target));
11726 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11727 ASET (vector, 2, mode_line_string_list);
11728 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11729 ASET (vector, 4, mode_line_string_face);
11730 ASET (vector, 5, mode_line_string_face_prop);
11732 if (obuf)
11733 XSETBUFFER (tmp, obuf);
11734 else
11735 tmp = Qnil;
11736 ASET (vector, 6, tmp);
11737 ASET (vector, 7, owin);
11738 if (target_frame)
11740 /* Similarly to `with-selected-window', if the operation selects
11741 a window on another frame, we must restore that frame's
11742 selected window, and (for a tty) the top-frame. */
11743 ASET (vector, 8, target_frame->selected_window);
11744 if (FRAME_TERMCAP_P (target_frame))
11745 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11748 return vector;
11751 static void
11752 unwind_format_mode_line (Lisp_Object vector)
11754 Lisp_Object old_window = AREF (vector, 7);
11755 Lisp_Object target_frame_window = AREF (vector, 8);
11756 Lisp_Object old_top_frame = AREF (vector, 9);
11758 mode_line_target = XINT (AREF (vector, 0));
11759 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11760 mode_line_string_list = AREF (vector, 2);
11761 if (! EQ (AREF (vector, 3), Qt))
11762 mode_line_proptrans_alist = AREF (vector, 3);
11763 mode_line_string_face = AREF (vector, 4);
11764 mode_line_string_face_prop = AREF (vector, 5);
11766 /* Select window before buffer, since it may change the buffer. */
11767 if (!NILP (old_window))
11769 /* If the operation that we are unwinding had selected a window
11770 on a different frame, reset its frame-selected-window. For a
11771 text terminal, reset its top-frame if necessary. */
11772 if (!NILP (target_frame_window))
11774 Lisp_Object frame
11775 = WINDOW_FRAME (XWINDOW (target_frame_window));
11777 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11778 Fselect_window (target_frame_window, Qt);
11780 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11781 Fselect_frame (old_top_frame, Qt);
11784 Fselect_window (old_window, Qt);
11787 if (!NILP (AREF (vector, 6)))
11789 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11790 ASET (vector, 6, Qnil);
11793 Vmode_line_unwind_vector = vector;
11797 /* Store a single character C for the frame title in mode_line_noprop_buf.
11798 Re-allocate mode_line_noprop_buf if necessary. */
11800 static void
11801 store_mode_line_noprop_char (char c)
11803 /* If output position has reached the end of the allocated buffer,
11804 increase the buffer's size. */
11805 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11807 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11808 ptrdiff_t size = len;
11809 mode_line_noprop_buf =
11810 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11811 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11812 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11815 *mode_line_noprop_ptr++ = c;
11819 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11820 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11821 characters that yield more columns than PRECISION; PRECISION <= 0
11822 means copy the whole string. Pad with spaces until FIELD_WIDTH
11823 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11824 pad. Called from display_mode_element when it is used to build a
11825 frame title. */
11827 static int
11828 store_mode_line_noprop (const char *string, int field_width, int precision)
11830 const unsigned char *str = (const unsigned char *) string;
11831 int n = 0;
11832 ptrdiff_t dummy, nbytes;
11834 /* Copy at most PRECISION chars from STR. */
11835 nbytes = strlen (string);
11836 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11837 while (nbytes--)
11838 store_mode_line_noprop_char (*str++);
11840 /* Fill up with spaces until FIELD_WIDTH reached. */
11841 while (field_width > 0
11842 && n < field_width)
11844 store_mode_line_noprop_char (' ');
11845 ++n;
11848 return n;
11851 /***********************************************************************
11852 Frame Titles
11853 ***********************************************************************/
11855 #ifdef HAVE_WINDOW_SYSTEM
11857 /* Set the title of FRAME, if it has changed. The title format is
11858 Vicon_title_format if FRAME is iconified, otherwise it is
11859 frame_title_format. */
11861 static void
11862 x_consider_frame_title (Lisp_Object frame)
11864 struct frame *f = XFRAME (frame);
11866 if ((FRAME_WINDOW_P (f)
11867 || FRAME_MINIBUF_ONLY_P (f)
11868 || f->explicit_name)
11869 && NILP (Fframe_parameter (frame, Qtooltip)))
11871 /* Do we have more than one visible frame on this X display? */
11872 Lisp_Object tail, other_frame, fmt;
11873 ptrdiff_t title_start;
11874 char *title;
11875 ptrdiff_t len;
11876 struct it it;
11877 ptrdiff_t count = SPECPDL_INDEX ();
11879 FOR_EACH_FRAME (tail, other_frame)
11881 struct frame *tf = XFRAME (other_frame);
11883 if (tf != f
11884 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11885 && !FRAME_MINIBUF_ONLY_P (tf)
11886 && !EQ (other_frame, tip_frame)
11887 && !FRAME_PARENT_FRAME (tf)
11888 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11889 break;
11892 /* Set global variable indicating that multiple frames exist. */
11893 multiple_frames = CONSP (tail);
11895 /* Switch to the buffer of selected window of the frame. Set up
11896 mode_line_target so that display_mode_element will output into
11897 mode_line_noprop_buf; then display the title. */
11898 record_unwind_protect (unwind_format_mode_line,
11899 format_mode_line_unwind_data
11900 (f, current_buffer, selected_window, false));
11901 /* select-frame calls resize_mini_window, which could resize the
11902 mini-window and by that undo the effect of this redisplay
11903 cycle wrt minibuffer and echo-area display. Binding
11904 inhibit-redisplay to t makes the call to resize_mini_window a
11905 no-op, thus avoiding the adverse side effects. */
11906 specbind (Qinhibit_redisplay, Qt);
11908 Fselect_window (f->selected_window, Qt);
11909 set_buffer_internal_1
11910 (XBUFFER (XWINDOW (f->selected_window)->contents));
11911 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11913 mode_line_target = MODE_LINE_TITLE;
11914 title_start = MODE_LINE_NOPROP_LEN (0);
11915 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11916 NULL, DEFAULT_FACE_ID);
11917 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11918 len = MODE_LINE_NOPROP_LEN (title_start);
11919 title = mode_line_noprop_buf + title_start;
11920 unbind_to (count, Qnil);
11922 /* Set the title only if it's changed. This avoids consing in
11923 the common case where it hasn't. (If it turns out that we've
11924 already wasted too much time by walking through the list with
11925 display_mode_element, then we might need to optimize at a
11926 higher level than this.) */
11927 if (! STRINGP (f->name)
11928 || SBYTES (f->name) != len
11929 || memcmp (title, SDATA (f->name), len) != 0)
11930 x_implicitly_set_name (f, make_string (title, len), Qnil);
11934 #endif /* not HAVE_WINDOW_SYSTEM */
11937 /***********************************************************************
11938 Menu Bars
11939 ***********************************************************************/
11941 /* True if we will not redisplay all visible windows. */
11942 #define REDISPLAY_SOME_P() \
11943 ((windows_or_buffers_changed == 0 \
11944 || windows_or_buffers_changed == REDISPLAY_SOME) \
11945 && (update_mode_lines == 0 \
11946 || update_mode_lines == REDISPLAY_SOME))
11948 /* Prepare for redisplay by updating menu-bar item lists when
11949 appropriate. This can call eval. */
11951 static void
11952 prepare_menu_bars (void)
11954 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11955 bool some_windows = REDISPLAY_SOME_P ();
11956 Lisp_Object tooltip_frame;
11958 #ifdef HAVE_WINDOW_SYSTEM
11959 tooltip_frame = tip_frame;
11960 #else
11961 tooltip_frame = Qnil;
11962 #endif
11964 if (FUNCTIONP (Vpre_redisplay_function))
11966 Lisp_Object windows = all_windows ? Qt : Qnil;
11967 if (all_windows && some_windows)
11969 Lisp_Object ws = window_list ();
11970 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11972 Lisp_Object this = XCAR (ws);
11973 struct window *w = XWINDOW (this);
11974 if (w->redisplay
11975 || XFRAME (w->frame)->redisplay
11976 || XBUFFER (w->contents)->text->redisplay)
11978 windows = Fcons (this, windows);
11982 safe__call1 (true, Vpre_redisplay_function, windows);
11985 /* Update all frame titles based on their buffer names, etc. We do
11986 this before the menu bars so that the buffer-menu will show the
11987 up-to-date frame titles. */
11988 #ifdef HAVE_WINDOW_SYSTEM
11989 if (all_windows)
11991 Lisp_Object tail, frame;
11993 FOR_EACH_FRAME (tail, frame)
11995 struct frame *f = XFRAME (frame);
11996 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11997 if (some_windows
11998 && !f->redisplay
11999 && !w->redisplay
12000 && !XBUFFER (w->contents)->text->redisplay)
12001 continue;
12003 if (!EQ (frame, tooltip_frame)
12004 && !FRAME_PARENT_FRAME (f)
12005 && (FRAME_ICONIFIED_P (f)
12006 || FRAME_VISIBLE_P (f) == 1
12007 /* Exclude TTY frames that are obscured because they
12008 are not the top frame on their console. This is
12009 because x_consider_frame_title actually switches
12010 to the frame, which for TTY frames means it is
12011 marked as garbaged, and will be completely
12012 redrawn on the next redisplay cycle. This causes
12013 TTY frames to be completely redrawn, when there
12014 are more than one of them, even though nothing
12015 should be changed on display. */
12016 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12017 x_consider_frame_title (frame);
12020 #endif /* HAVE_WINDOW_SYSTEM */
12022 /* Update the menu bar item lists, if appropriate. This has to be
12023 done before any actual redisplay or generation of display lines. */
12025 if (all_windows)
12027 Lisp_Object tail, frame;
12028 ptrdiff_t count = SPECPDL_INDEX ();
12029 /* True means that update_menu_bar has run its hooks
12030 so any further calls to update_menu_bar shouldn't do so again. */
12031 bool menu_bar_hooks_run = false;
12033 record_unwind_save_match_data ();
12035 FOR_EACH_FRAME (tail, frame)
12037 struct frame *f = XFRAME (frame);
12038 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12040 /* Ignore tooltip frame. */
12041 if (EQ (frame, tooltip_frame))
12042 continue;
12044 if (some_windows
12045 && !f->redisplay
12046 && !w->redisplay
12047 && !XBUFFER (w->contents)->text->redisplay)
12048 continue;
12050 run_window_size_change_functions (frame);
12052 if (FRAME_PARENT_FRAME (f))
12053 continue;
12055 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12056 #ifdef HAVE_WINDOW_SYSTEM
12057 update_tool_bar (f, false);
12058 #endif
12061 unbind_to (count, Qnil);
12063 else
12065 struct frame *sf = SELECTED_FRAME ();
12066 update_menu_bar (sf, true, false);
12067 #ifdef HAVE_WINDOW_SYSTEM
12068 update_tool_bar (sf, true);
12069 #endif
12074 /* Update the menu bar item list for frame F. This has to be done
12075 before we start to fill in any display lines, because it can call
12076 eval.
12078 If SAVE_MATCH_DATA, we must save and restore it here.
12080 If HOOKS_RUN, a previous call to update_menu_bar
12081 already ran the menu bar hooks for this redisplay, so there
12082 is no need to run them again. The return value is the
12083 updated value of this flag, to pass to the next call. */
12085 static bool
12086 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12088 Lisp_Object window;
12089 struct window *w;
12091 /* If called recursively during a menu update, do nothing. This can
12092 happen when, for instance, an activate-menubar-hook causes a
12093 redisplay. */
12094 if (inhibit_menubar_update)
12095 return hooks_run;
12097 window = FRAME_SELECTED_WINDOW (f);
12098 w = XWINDOW (window);
12100 if (FRAME_WINDOW_P (f)
12102 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12103 || defined (HAVE_NS) || defined (USE_GTK)
12104 FRAME_EXTERNAL_MENU_BAR (f)
12105 #else
12106 FRAME_MENU_BAR_LINES (f) > 0
12107 #endif
12108 : FRAME_MENU_BAR_LINES (f) > 0)
12110 /* If the user has switched buffers or windows, we need to
12111 recompute to reflect the new bindings. But we'll
12112 recompute when update_mode_lines is set too; that means
12113 that people can use force-mode-line-update to request
12114 that the menu bar be recomputed. The adverse effect on
12115 the rest of the redisplay algorithm is about the same as
12116 windows_or_buffers_changed anyway. */
12117 if (windows_or_buffers_changed
12118 /* This used to test w->update_mode_line, but we believe
12119 there is no need to recompute the menu in that case. */
12120 || update_mode_lines
12121 || window_buffer_changed (w))
12123 struct buffer *prev = current_buffer;
12124 ptrdiff_t count = SPECPDL_INDEX ();
12126 specbind (Qinhibit_menubar_update, Qt);
12128 set_buffer_internal_1 (XBUFFER (w->contents));
12129 if (save_match_data)
12130 record_unwind_save_match_data ();
12131 if (NILP (Voverriding_local_map_menu_flag))
12133 specbind (Qoverriding_terminal_local_map, Qnil);
12134 specbind (Qoverriding_local_map, Qnil);
12137 if (!hooks_run)
12139 /* Run the Lucid hook. */
12140 safe_run_hooks (Qactivate_menubar_hook);
12142 /* If it has changed current-menubar from previous value,
12143 really recompute the menu-bar from the value. */
12144 if (! NILP (Vlucid_menu_bar_dirty_flag))
12145 call0 (Qrecompute_lucid_menubar);
12147 safe_run_hooks (Qmenu_bar_update_hook);
12149 hooks_run = true;
12152 XSETFRAME (Vmenu_updating_frame, f);
12153 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12155 /* Redisplay the menu bar in case we changed it. */
12156 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12157 || defined (HAVE_NS) || defined (USE_GTK)
12158 if (FRAME_WINDOW_P (f))
12160 #if defined (HAVE_NS)
12161 /* All frames on Mac OS share the same menubar. So only
12162 the selected frame should be allowed to set it. */
12163 if (f == SELECTED_FRAME ())
12164 #endif
12165 set_frame_menubar (f, false, false);
12167 else
12168 /* On a terminal screen, the menu bar is an ordinary screen
12169 line, and this makes it get updated. */
12170 w->update_mode_line = true;
12171 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12172 /* In the non-toolkit version, the menu bar is an ordinary screen
12173 line, and this makes it get updated. */
12174 w->update_mode_line = true;
12175 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12177 unbind_to (count, Qnil);
12178 set_buffer_internal_1 (prev);
12182 return hooks_run;
12185 /***********************************************************************
12186 Tool-bars
12187 ***********************************************************************/
12189 #ifdef HAVE_WINDOW_SYSTEM
12191 /* Select `frame' temporarily without running all the code in
12192 do_switch_frame.
12193 FIXME: Maybe do_switch_frame should be trimmed down similarly
12194 when `norecord' is set. */
12195 static void
12196 fast_set_selected_frame (Lisp_Object frame)
12198 if (!EQ (selected_frame, frame))
12200 selected_frame = frame;
12201 selected_window = XFRAME (frame)->selected_window;
12205 /* Update the tool-bar item list for frame F. This has to be done
12206 before we start to fill in any display lines. Called from
12207 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12208 and restore it here. */
12210 static void
12211 update_tool_bar (struct frame *f, bool save_match_data)
12213 #if defined (USE_GTK) || defined (HAVE_NS)
12214 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12215 #else
12216 bool do_update = (WINDOWP (f->tool_bar_window)
12217 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12218 #endif
12220 if (do_update)
12222 Lisp_Object window;
12223 struct window *w;
12225 window = FRAME_SELECTED_WINDOW (f);
12226 w = XWINDOW (window);
12228 /* If the user has switched buffers or windows, we need to
12229 recompute to reflect the new bindings. But we'll
12230 recompute when update_mode_lines is set too; that means
12231 that people can use force-mode-line-update to request
12232 that the menu bar be recomputed. The adverse effect on
12233 the rest of the redisplay algorithm is about the same as
12234 windows_or_buffers_changed anyway. */
12235 if (windows_or_buffers_changed
12236 || w->update_mode_line
12237 || update_mode_lines
12238 || window_buffer_changed (w))
12240 struct buffer *prev = current_buffer;
12241 ptrdiff_t count = SPECPDL_INDEX ();
12242 Lisp_Object frame, new_tool_bar;
12243 int new_n_tool_bar;
12245 /* Set current_buffer to the buffer of the selected
12246 window of the frame, so that we get the right local
12247 keymaps. */
12248 set_buffer_internal_1 (XBUFFER (w->contents));
12250 /* Save match data, if we must. */
12251 if (save_match_data)
12252 record_unwind_save_match_data ();
12254 /* Make sure that we don't accidentally use bogus keymaps. */
12255 if (NILP (Voverriding_local_map_menu_flag))
12257 specbind (Qoverriding_terminal_local_map, Qnil);
12258 specbind (Qoverriding_local_map, Qnil);
12261 /* We must temporarily set the selected frame to this frame
12262 before calling tool_bar_items, because the calculation of
12263 the tool-bar keymap uses the selected frame (see
12264 `tool-bar-make-keymap' in tool-bar.el). */
12265 eassert (EQ (selected_window,
12266 /* Since we only explicitly preserve selected_frame,
12267 check that selected_window would be redundant. */
12268 XFRAME (selected_frame)->selected_window));
12269 record_unwind_protect (fast_set_selected_frame, selected_frame);
12270 XSETFRAME (frame, f);
12271 fast_set_selected_frame (frame);
12273 /* Build desired tool-bar items from keymaps. */
12274 new_tool_bar
12275 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12276 &new_n_tool_bar);
12278 /* Redisplay the tool-bar if we changed it. */
12279 if (new_n_tool_bar != f->n_tool_bar_items
12280 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12282 /* Redisplay that happens asynchronously due to an expose event
12283 may access f->tool_bar_items. Make sure we update both
12284 variables within BLOCK_INPUT so no such event interrupts. */
12285 block_input ();
12286 fset_tool_bar_items (f, new_tool_bar);
12287 f->n_tool_bar_items = new_n_tool_bar;
12288 w->update_mode_line = true;
12289 unblock_input ();
12292 unbind_to (count, Qnil);
12293 set_buffer_internal_1 (prev);
12298 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12300 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12301 F's desired tool-bar contents. F->tool_bar_items must have
12302 been set up previously by calling prepare_menu_bars. */
12304 static void
12305 build_desired_tool_bar_string (struct frame *f)
12307 int i, size, size_needed;
12308 Lisp_Object image, plist;
12310 image = plist = Qnil;
12312 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12313 Otherwise, make a new string. */
12315 /* The size of the string we might be able to reuse. */
12316 size = (STRINGP (f->desired_tool_bar_string)
12317 ? SCHARS (f->desired_tool_bar_string)
12318 : 0);
12320 /* We need one space in the string for each image. */
12321 size_needed = f->n_tool_bar_items;
12323 /* Reuse f->desired_tool_bar_string, if possible. */
12324 if (size < size_needed || NILP (f->desired_tool_bar_string))
12325 fset_desired_tool_bar_string
12326 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12327 else
12329 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12330 Fremove_text_properties (make_number (0), make_number (size),
12331 props, f->desired_tool_bar_string);
12334 /* Put a `display' property on the string for the images to display,
12335 put a `menu_item' property on tool-bar items with a value that
12336 is the index of the item in F's tool-bar item vector. */
12337 for (i = 0; i < f->n_tool_bar_items; ++i)
12339 #define PROP(IDX) \
12340 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12342 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12343 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12344 int hmargin, vmargin, relief, idx, end;
12346 /* If image is a vector, choose the image according to the
12347 button state. */
12348 image = PROP (TOOL_BAR_ITEM_IMAGES);
12349 if (VECTORP (image))
12351 if (enabled_p)
12352 idx = (selected_p
12353 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12354 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12355 else
12356 idx = (selected_p
12357 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12358 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12360 eassert (ASIZE (image) >= idx);
12361 image = AREF (image, idx);
12363 else
12364 idx = -1;
12366 /* Ignore invalid image specifications. */
12367 if (!valid_image_p (image))
12368 continue;
12370 /* Display the tool-bar button pressed, or depressed. */
12371 plist = Fcopy_sequence (XCDR (image));
12373 /* Compute margin and relief to draw. */
12374 relief = (tool_bar_button_relief >= 0
12375 ? tool_bar_button_relief
12376 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12377 hmargin = vmargin = relief;
12379 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12380 INT_MAX - max (hmargin, vmargin)))
12382 hmargin += XFASTINT (Vtool_bar_button_margin);
12383 vmargin += XFASTINT (Vtool_bar_button_margin);
12385 else if (CONSP (Vtool_bar_button_margin))
12387 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12388 INT_MAX - hmargin))
12389 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12391 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12392 INT_MAX - vmargin))
12393 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12396 if (auto_raise_tool_bar_buttons_p)
12398 /* Add a `:relief' property to the image spec if the item is
12399 selected. */
12400 if (selected_p)
12402 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12403 hmargin -= relief;
12404 vmargin -= relief;
12407 else
12409 /* If image is selected, display it pressed, i.e. with a
12410 negative relief. If it's not selected, display it with a
12411 raised relief. */
12412 plist = Fplist_put (plist, QCrelief,
12413 (selected_p
12414 ? make_number (-relief)
12415 : make_number (relief)));
12416 hmargin -= relief;
12417 vmargin -= relief;
12420 /* Put a margin around the image. */
12421 if (hmargin || vmargin)
12423 if (hmargin == vmargin)
12424 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12425 else
12426 plist = Fplist_put (plist, QCmargin,
12427 Fcons (make_number (hmargin),
12428 make_number (vmargin)));
12431 /* If button is not enabled, and we don't have special images
12432 for the disabled state, make the image appear disabled by
12433 applying an appropriate algorithm to it. */
12434 if (!enabled_p && idx < 0)
12435 plist = Fplist_put (plist, QCconversion, Qdisabled);
12437 /* Put a `display' text property on the string for the image to
12438 display. Put a `menu-item' property on the string that gives
12439 the start of this item's properties in the tool-bar items
12440 vector. */
12441 image = Fcons (Qimage, plist);
12442 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12443 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12445 /* Let the last image hide all remaining spaces in the tool bar
12446 string. The string can be longer than needed when we reuse a
12447 previous string. */
12448 if (i + 1 == f->n_tool_bar_items)
12449 end = SCHARS (f->desired_tool_bar_string);
12450 else
12451 end = i + 1;
12452 Fadd_text_properties (make_number (i), make_number (end),
12453 props, f->desired_tool_bar_string);
12454 #undef PROP
12459 /* Display one line of the tool-bar of frame IT->f.
12461 HEIGHT specifies the desired height of the tool-bar line.
12462 If the actual height of the glyph row is less than HEIGHT, the
12463 row's height is increased to HEIGHT, and the icons are centered
12464 vertically in the new height.
12466 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12467 count a final empty row in case the tool-bar width exactly matches
12468 the window width.
12471 static void
12472 display_tool_bar_line (struct it *it, int height)
12474 struct glyph_row *row = it->glyph_row;
12475 int max_x = it->last_visible_x;
12476 struct glyph *last;
12478 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12479 clear_glyph_row (row);
12480 row->enabled_p = true;
12481 row->y = it->current_y;
12483 /* Note that this isn't made use of if the face hasn't a box,
12484 so there's no need to check the face here. */
12485 it->start_of_box_run_p = true;
12487 while (it->current_x < max_x)
12489 int x, n_glyphs_before, i, nglyphs;
12490 struct it it_before;
12492 /* Get the next display element. */
12493 if (!get_next_display_element (it))
12495 /* Don't count empty row if we are counting needed tool-bar lines. */
12496 if (height < 0 && !it->hpos)
12497 return;
12498 break;
12501 /* Produce glyphs. */
12502 n_glyphs_before = row->used[TEXT_AREA];
12503 it_before = *it;
12505 PRODUCE_GLYPHS (it);
12507 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12508 i = 0;
12509 x = it_before.current_x;
12510 while (i < nglyphs)
12512 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12514 if (x + glyph->pixel_width > max_x)
12516 /* Glyph doesn't fit on line. Backtrack. */
12517 row->used[TEXT_AREA] = n_glyphs_before;
12518 *it = it_before;
12519 /* If this is the only glyph on this line, it will never fit on the
12520 tool-bar, so skip it. But ensure there is at least one glyph,
12521 so we don't accidentally disable the tool-bar. */
12522 if (n_glyphs_before == 0
12523 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12524 break;
12525 goto out;
12528 ++it->hpos;
12529 x += glyph->pixel_width;
12530 ++i;
12533 /* Stop at line end. */
12534 if (ITERATOR_AT_END_OF_LINE_P (it))
12535 break;
12537 set_iterator_to_next (it, true);
12540 out:;
12542 row->displays_text_p = row->used[TEXT_AREA] != 0;
12544 /* Use default face for the border below the tool bar.
12546 FIXME: When auto-resize-tool-bars is grow-only, there is
12547 no additional border below the possibly empty tool-bar lines.
12548 So to make the extra empty lines look "normal", we have to
12549 use the tool-bar face for the border too. */
12550 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12551 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12552 it->face_id = DEFAULT_FACE_ID;
12554 extend_face_to_end_of_line (it);
12555 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12556 last->right_box_line_p = true;
12557 if (last == row->glyphs[TEXT_AREA])
12558 last->left_box_line_p = true;
12560 /* Make line the desired height and center it vertically. */
12561 if ((height -= it->max_ascent + it->max_descent) > 0)
12563 /* Don't add more than one line height. */
12564 height %= FRAME_LINE_HEIGHT (it->f);
12565 it->max_ascent += height / 2;
12566 it->max_descent += (height + 1) / 2;
12569 compute_line_metrics (it);
12571 /* If line is empty, make it occupy the rest of the tool-bar. */
12572 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12574 row->height = row->phys_height = it->last_visible_y - row->y;
12575 row->visible_height = row->height;
12576 row->ascent = row->phys_ascent = 0;
12577 row->extra_line_spacing = 0;
12580 row->full_width_p = true;
12581 row->continued_p = false;
12582 row->truncated_on_left_p = false;
12583 row->truncated_on_right_p = false;
12585 it->current_x = it->hpos = 0;
12586 it->current_y += row->height;
12587 ++it->vpos;
12588 ++it->glyph_row;
12592 /* Value is the number of pixels needed to make all tool-bar items of
12593 frame F visible. The actual number of glyph rows needed is
12594 returned in *N_ROWS if non-NULL. */
12595 static int
12596 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12598 struct window *w = XWINDOW (f->tool_bar_window);
12599 struct it it;
12600 /* tool_bar_height is called from redisplay_tool_bar after building
12601 the desired matrix, so use (unused) mode-line row as temporary row to
12602 avoid destroying the first tool-bar row. */
12603 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12605 /* Initialize an iterator for iteration over
12606 F->desired_tool_bar_string in the tool-bar window of frame F. */
12607 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12608 temp_row->reversed_p = false;
12609 it.first_visible_x = 0;
12610 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12611 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12612 it.paragraph_embedding = L2R;
12614 while (!ITERATOR_AT_END_P (&it))
12616 clear_glyph_row (temp_row);
12617 it.glyph_row = temp_row;
12618 display_tool_bar_line (&it, -1);
12620 clear_glyph_row (temp_row);
12622 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12623 if (n_rows)
12624 *n_rows = it.vpos > 0 ? it.vpos : -1;
12626 if (pixelwise)
12627 return it.current_y;
12628 else
12629 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12632 #endif /* !USE_GTK && !HAVE_NS */
12634 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12635 0, 2, 0,
12636 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12637 If FRAME is nil or omitted, use the selected frame. Optional argument
12638 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12639 (Lisp_Object frame, Lisp_Object pixelwise)
12641 int height = 0;
12643 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12644 struct frame *f = decode_any_frame (frame);
12646 if (WINDOWP (f->tool_bar_window)
12647 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12649 update_tool_bar (f, true);
12650 if (f->n_tool_bar_items)
12652 build_desired_tool_bar_string (f);
12653 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12656 #endif
12658 return make_number (height);
12662 /* Display the tool-bar of frame F. Value is true if tool-bar's
12663 height should be changed. */
12664 static bool
12665 redisplay_tool_bar (struct frame *f)
12667 f->tool_bar_redisplayed = true;
12668 #if defined (USE_GTK) || defined (HAVE_NS)
12670 if (FRAME_EXTERNAL_TOOL_BAR (f))
12671 update_frame_tool_bar (f);
12672 return false;
12674 #else /* !USE_GTK && !HAVE_NS */
12676 struct window *w;
12677 struct it it;
12678 struct glyph_row *row;
12680 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12681 do anything. This means you must start with tool-bar-lines
12682 non-zero to get the auto-sizing effect. Or in other words, you
12683 can turn off tool-bars by specifying tool-bar-lines zero. */
12684 if (!WINDOWP (f->tool_bar_window)
12685 || (w = XWINDOW (f->tool_bar_window),
12686 WINDOW_TOTAL_LINES (w) == 0))
12687 return false;
12689 /* Set up an iterator for the tool-bar window. */
12690 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12691 it.first_visible_x = 0;
12692 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12693 row = it.glyph_row;
12694 row->reversed_p = false;
12696 /* Build a string that represents the contents of the tool-bar. */
12697 build_desired_tool_bar_string (f);
12698 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12699 /* FIXME: This should be controlled by a user option. But it
12700 doesn't make sense to have an R2L tool bar if the menu bar cannot
12701 be drawn also R2L, and making the menu bar R2L is tricky due
12702 toolkit-specific code that implements it. If an R2L tool bar is
12703 ever supported, display_tool_bar_line should also be augmented to
12704 call unproduce_glyphs like display_line and display_string
12705 do. */
12706 it.paragraph_embedding = L2R;
12708 if (f->n_tool_bar_rows == 0)
12710 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12712 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12714 x_change_tool_bar_height (f, new_height);
12715 frame_default_tool_bar_height = new_height;
12716 /* Always do that now. */
12717 clear_glyph_matrix (w->desired_matrix);
12718 f->fonts_changed = true;
12719 return true;
12723 /* Display as many lines as needed to display all tool-bar items. */
12725 if (f->n_tool_bar_rows > 0)
12727 int border, rows, height, extra;
12729 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12730 border = XINT (Vtool_bar_border);
12731 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12732 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12733 else if (EQ (Vtool_bar_border, Qborder_width))
12734 border = f->border_width;
12735 else
12736 border = 0;
12737 if (border < 0)
12738 border = 0;
12740 rows = f->n_tool_bar_rows;
12741 height = max (1, (it.last_visible_y - border) / rows);
12742 extra = it.last_visible_y - border - height * rows;
12744 while (it.current_y < it.last_visible_y)
12746 int h = 0;
12747 if (extra > 0 && rows-- > 0)
12749 h = (extra + rows - 1) / rows;
12750 extra -= h;
12752 display_tool_bar_line (&it, height + h);
12755 else
12757 while (it.current_y < it.last_visible_y)
12758 display_tool_bar_line (&it, 0);
12761 /* It doesn't make much sense to try scrolling in the tool-bar
12762 window, so don't do it. */
12763 w->desired_matrix->no_scrolling_p = true;
12764 w->must_be_updated_p = true;
12766 if (!NILP (Vauto_resize_tool_bars))
12768 bool change_height_p = true;
12770 /* If we couldn't display everything, change the tool-bar's
12771 height if there is room for more. */
12772 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12773 change_height_p = true;
12775 /* We subtract 1 because display_tool_bar_line advances the
12776 glyph_row pointer before returning to its caller. We want to
12777 examine the last glyph row produced by
12778 display_tool_bar_line. */
12779 row = it.glyph_row - 1;
12781 /* If there are blank lines at the end, except for a partially
12782 visible blank line at the end that is smaller than
12783 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12784 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12785 && row->height >= FRAME_LINE_HEIGHT (f))
12786 change_height_p = true;
12788 /* If row displays tool-bar items, but is partially visible,
12789 change the tool-bar's height. */
12790 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12791 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12792 change_height_p = true;
12794 /* Resize windows as needed by changing the `tool-bar-lines'
12795 frame parameter. */
12796 if (change_height_p)
12798 int nrows;
12799 int new_height = tool_bar_height (f, &nrows, true);
12801 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12802 && !f->minimize_tool_bar_window_p)
12803 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12804 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12805 f->minimize_tool_bar_window_p = false;
12807 if (change_height_p)
12809 x_change_tool_bar_height (f, new_height);
12810 frame_default_tool_bar_height = new_height;
12811 clear_glyph_matrix (w->desired_matrix);
12812 f->n_tool_bar_rows = nrows;
12813 f->fonts_changed = true;
12815 return true;
12820 f->minimize_tool_bar_window_p = false;
12821 return false;
12823 #endif /* USE_GTK || HAVE_NS */
12826 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12828 /* Get information about the tool-bar item which is displayed in GLYPH
12829 on frame F. Return in *PROP_IDX the index where tool-bar item
12830 properties start in F->tool_bar_items. Value is false if
12831 GLYPH doesn't display a tool-bar item. */
12833 static bool
12834 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12836 Lisp_Object prop;
12837 int charpos;
12839 /* This function can be called asynchronously, which means we must
12840 exclude any possibility that Fget_text_property signals an
12841 error. */
12842 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12843 charpos = max (0, charpos);
12845 /* Get the text property `menu-item' at pos. The value of that
12846 property is the start index of this item's properties in
12847 F->tool_bar_items. */
12848 prop = Fget_text_property (make_number (charpos),
12849 Qmenu_item, f->current_tool_bar_string);
12850 if (! INTEGERP (prop))
12851 return false;
12852 *prop_idx = XINT (prop);
12853 return true;
12857 /* Get information about the tool-bar item at position X/Y on frame F.
12858 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12859 the current matrix of the tool-bar window of F, or NULL if not
12860 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12861 item in F->tool_bar_items. Value is
12863 -1 if X/Y is not on a tool-bar item
12864 0 if X/Y is on the same item that was highlighted before.
12865 1 otherwise. */
12867 static int
12868 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12869 int *hpos, int *vpos, int *prop_idx)
12871 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12872 struct window *w = XWINDOW (f->tool_bar_window);
12873 int area;
12875 /* Find the glyph under X/Y. */
12876 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12877 if (*glyph == NULL)
12878 return -1;
12880 /* Get the start of this tool-bar item's properties in
12881 f->tool_bar_items. */
12882 if (!tool_bar_item_info (f, *glyph, prop_idx))
12883 return -1;
12885 /* Is mouse on the highlighted item? */
12886 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12887 && *vpos >= hlinfo->mouse_face_beg_row
12888 && *vpos <= hlinfo->mouse_face_end_row
12889 && (*vpos > hlinfo->mouse_face_beg_row
12890 || *hpos >= hlinfo->mouse_face_beg_col)
12891 && (*vpos < hlinfo->mouse_face_end_row
12892 || *hpos < hlinfo->mouse_face_end_col
12893 || hlinfo->mouse_face_past_end))
12894 return 0;
12896 return 1;
12900 /* EXPORT:
12901 Handle mouse button event on the tool-bar of frame F, at
12902 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12903 false for button release. MODIFIERS is event modifiers for button
12904 release. */
12906 void
12907 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12908 int modifiers)
12910 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12911 struct window *w = XWINDOW (f->tool_bar_window);
12912 int hpos, vpos, prop_idx;
12913 struct glyph *glyph;
12914 Lisp_Object enabled_p;
12915 int ts;
12917 /* If not on the highlighted tool-bar item, and mouse-highlight is
12918 non-nil, return. This is so we generate the tool-bar button
12919 click only when the mouse button is released on the same item as
12920 where it was pressed. However, when mouse-highlight is disabled,
12921 generate the click when the button is released regardless of the
12922 highlight, since tool-bar items are not highlighted in that
12923 case. */
12924 frame_to_window_pixel_xy (w, &x, &y);
12925 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12926 if (ts == -1
12927 || (ts != 0 && !NILP (Vmouse_highlight)))
12928 return;
12930 /* When mouse-highlight is off, generate the click for the item
12931 where the button was pressed, disregarding where it was
12932 released. */
12933 if (NILP (Vmouse_highlight) && !down_p)
12934 prop_idx = f->last_tool_bar_item;
12936 /* If item is disabled, do nothing. */
12937 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12938 if (NILP (enabled_p))
12939 return;
12941 if (down_p)
12943 /* Show item in pressed state. */
12944 if (!NILP (Vmouse_highlight))
12945 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12946 f->last_tool_bar_item = prop_idx;
12948 else
12950 Lisp_Object key, frame;
12951 struct input_event event;
12952 EVENT_INIT (event);
12954 /* Show item in released state. */
12955 if (!NILP (Vmouse_highlight))
12956 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12958 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12960 XSETFRAME (frame, f);
12961 event.kind = TOOL_BAR_EVENT;
12962 event.frame_or_window = frame;
12963 event.arg = frame;
12964 kbd_buffer_store_event (&event);
12966 event.kind = TOOL_BAR_EVENT;
12967 event.frame_or_window = frame;
12968 event.arg = key;
12969 event.modifiers = modifiers;
12970 kbd_buffer_store_event (&event);
12971 f->last_tool_bar_item = -1;
12976 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12977 tool-bar window-relative coordinates X/Y. Called from
12978 note_mouse_highlight. */
12980 static void
12981 note_tool_bar_highlight (struct frame *f, int x, int y)
12983 Lisp_Object window = f->tool_bar_window;
12984 struct window *w = XWINDOW (window);
12985 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12986 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12987 int hpos, vpos;
12988 struct glyph *glyph;
12989 struct glyph_row *row;
12990 int i;
12991 Lisp_Object enabled_p;
12992 int prop_idx;
12993 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12994 bool mouse_down_p;
12995 int rc;
12997 /* Function note_mouse_highlight is called with negative X/Y
12998 values when mouse moves outside of the frame. */
12999 if (x <= 0 || y <= 0)
13001 clear_mouse_face (hlinfo);
13002 return;
13005 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13006 if (rc < 0)
13008 /* Not on tool-bar item. */
13009 clear_mouse_face (hlinfo);
13010 return;
13012 else if (rc == 0)
13013 /* On same tool-bar item as before. */
13014 goto set_help_echo;
13016 clear_mouse_face (hlinfo);
13018 /* Mouse is down, but on different tool-bar item? */
13019 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13020 && f == dpyinfo->last_mouse_frame);
13022 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13023 return;
13025 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13027 /* If tool-bar item is not enabled, don't highlight it. */
13028 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13029 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13031 /* Compute the x-position of the glyph. In front and past the
13032 image is a space. We include this in the highlighted area. */
13033 row = MATRIX_ROW (w->current_matrix, vpos);
13034 for (i = x = 0; i < hpos; ++i)
13035 x += row->glyphs[TEXT_AREA][i].pixel_width;
13037 /* Record this as the current active region. */
13038 hlinfo->mouse_face_beg_col = hpos;
13039 hlinfo->mouse_face_beg_row = vpos;
13040 hlinfo->mouse_face_beg_x = x;
13041 hlinfo->mouse_face_past_end = false;
13043 hlinfo->mouse_face_end_col = hpos + 1;
13044 hlinfo->mouse_face_end_row = vpos;
13045 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13046 hlinfo->mouse_face_window = window;
13047 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13049 /* Display it as active. */
13050 show_mouse_face (hlinfo, draw);
13053 set_help_echo:
13055 /* Set help_echo_string to a help string to display for this tool-bar item.
13056 XTread_socket does the rest. */
13057 help_echo_object = help_echo_window = Qnil;
13058 help_echo_pos = -1;
13059 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13060 if (NILP (help_echo_string))
13061 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13064 #endif /* !USE_GTK && !HAVE_NS */
13066 #endif /* HAVE_WINDOW_SYSTEM */
13070 /************************************************************************
13071 Horizontal scrolling
13072 ************************************************************************/
13074 /* For all leaf windows in the window tree rooted at WINDOW, set their
13075 hscroll value so that PT is (i) visible in the window, and (ii) so
13076 that it is not within a certain margin at the window's left and
13077 right border. Value is true if any window's hscroll has been
13078 changed. */
13080 static bool
13081 hscroll_window_tree (Lisp_Object window)
13083 bool hscrolled_p = false;
13084 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13085 int hscroll_step_abs = 0;
13086 double hscroll_step_rel = 0;
13088 if (hscroll_relative_p)
13090 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13091 if (hscroll_step_rel < 0)
13093 hscroll_relative_p = false;
13094 hscroll_step_abs = 0;
13097 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13099 hscroll_step_abs = XINT (Vhscroll_step);
13100 if (hscroll_step_abs < 0)
13101 hscroll_step_abs = 0;
13103 else
13104 hscroll_step_abs = 0;
13106 while (WINDOWP (window))
13108 struct window *w = XWINDOW (window);
13110 if (WINDOWP (w->contents))
13111 hscrolled_p |= hscroll_window_tree (w->contents);
13112 else if (w->cursor.vpos >= 0)
13114 int h_margin;
13115 int text_area_width;
13116 struct glyph_row *cursor_row;
13117 struct glyph_row *bottom_row;
13119 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13120 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13121 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13122 else
13123 cursor_row = bottom_row - 1;
13125 if (!cursor_row->enabled_p)
13127 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13128 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13129 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13130 else
13131 cursor_row = bottom_row - 1;
13133 bool row_r2l_p = cursor_row->reversed_p;
13134 bool hscl = hscrolling_current_line_p (w);
13135 int x_offset = 0;
13136 /* When line numbers are displayed, we need to account for
13137 the horizontal space they consume. */
13138 if (!NILP (Vdisplay_line_numbers))
13140 struct glyph *g;
13141 if (!row_r2l_p)
13143 for (g = cursor_row->glyphs[TEXT_AREA];
13144 g < cursor_row->glyphs[TEXT_AREA]
13145 + cursor_row->used[TEXT_AREA];
13146 g++)
13148 if (!(NILP (g->object) && g->charpos < 0))
13149 break;
13150 x_offset += g->pixel_width;
13153 else
13155 for (g = cursor_row->glyphs[TEXT_AREA]
13156 + cursor_row->used[TEXT_AREA];
13157 g > cursor_row->glyphs[TEXT_AREA];
13158 g--)
13160 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13161 break;
13162 x_offset += (g - 1)->pixel_width;
13166 if (cursor_row->truncated_on_left_p)
13168 /* On TTY frames, don't count the left truncation glyph. */
13169 struct frame *f = XFRAME (WINDOW_FRAME (w));
13170 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13173 text_area_width = window_box_width (w, TEXT_AREA);
13175 /* Scroll when cursor is inside this scroll margin. */
13176 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13178 /* If the position of this window's point has explicitly
13179 changed, no more suspend auto hscrolling. */
13180 if (w->suspend_auto_hscroll
13181 && NILP (Fequal (Fwindow_point (window),
13182 Fwindow_old_point (window))))
13184 w->suspend_auto_hscroll = false;
13185 /* When hscrolling just the current line, and the rest
13186 of lines were temporarily hscrolled, but no longer
13187 are, force thorough redisplay of this window, to show
13188 the effect of disabling hscroll suspension immediately. */
13189 if (w->min_hscroll == 0 && w->hscroll > 0
13190 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
13191 Qcurrent_line))
13192 SET_FRAME_GARBAGED (XFRAME (w->frame));
13195 /* Remember window point. */
13196 Fset_marker (w->old_pointm,
13197 ((w == XWINDOW (selected_window))
13198 ? make_number (BUF_PT (XBUFFER (w->contents)))
13199 : Fmarker_position (w->pointm)),
13200 w->contents);
13202 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13203 && !w->suspend_auto_hscroll
13204 /* In some pathological cases, like restoring a window
13205 configuration into a frame that is much smaller than
13206 the one from which the configuration was saved, we
13207 get glyph rows whose start and end have zero buffer
13208 positions, which we cannot handle below. Just skip
13209 such windows. */
13210 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13211 /* For left-to-right rows, hscroll when cursor is either
13212 (i) inside the right hscroll margin, or (ii) if it is
13213 inside the left margin and the window is already
13214 hscrolled. */
13215 && ((!row_r2l_p
13216 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13217 || (cursor_row->enabled_p
13218 && cursor_row->truncated_on_right_p
13219 && (w->cursor.x >= text_area_width - h_margin))))
13220 /* For right-to-left rows, the logic is similar,
13221 except that rules for scrolling to left and right
13222 are reversed. E.g., if cursor.x <= h_margin, we
13223 need to hscroll "to the right" unconditionally,
13224 and that will scroll the screen to the left so as
13225 to reveal the next portion of the row. */
13226 || (row_r2l_p
13227 && ((cursor_row->enabled_p
13228 /* FIXME: It is confusing to set the
13229 truncated_on_right_p flag when R2L rows
13230 are actually truncated on the left. */
13231 && cursor_row->truncated_on_right_p
13232 && w->cursor.x <= h_margin)
13233 || (w->hscroll
13234 && (w->cursor.x >= (text_area_width - h_margin
13235 - x_offset)))))
13236 /* This last condition is needed when moving
13237 vertically from an hscrolled line to a short line
13238 that doesn't need to be hscrolled. If we omit
13239 this condition, the line from which we move will
13240 remain hscrolled. */
13241 || (hscl
13242 && w->hscroll != w->min_hscroll
13243 && !cursor_row->truncated_on_left_p)))
13245 struct it it;
13246 ptrdiff_t hscroll;
13247 struct buffer *saved_current_buffer;
13248 ptrdiff_t pt;
13249 int wanted_x;
13251 /* Find point in a display of infinite width. */
13252 saved_current_buffer = current_buffer;
13253 current_buffer = XBUFFER (w->contents);
13255 if (w == XWINDOW (selected_window))
13256 pt = PT;
13257 else
13258 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13260 /* Move iterator to pt starting at cursor_row->start in
13261 a line with infinite width. */
13262 init_to_row_start (&it, w, cursor_row);
13263 if (hscl)
13264 it.first_visible_x = window_hscroll_limited (w, it.f)
13265 * FRAME_COLUMN_WIDTH (it.f);
13266 it.last_visible_x = DISP_INFINITY;
13267 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13268 /* If the line ends in an overlay string with a newline,
13269 we might infloop, because displaying the window will
13270 want to put the cursor after the overlay, i.e. at X
13271 coordinate of zero on the next screen line. So we
13272 use the buffer position prior to the overlay string
13273 instead. */
13274 if (it.method == GET_FROM_STRING && pt > 1)
13276 init_to_row_start (&it, w, cursor_row);
13277 if (hscl)
13278 it.first_visible_x = (window_hscroll_limited (w, it.f)
13279 * FRAME_COLUMN_WIDTH (it.f));
13280 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13282 current_buffer = saved_current_buffer;
13284 /* Position cursor in window. */
13285 if (!hscroll_relative_p && hscroll_step_abs == 0)
13286 hscroll = max (0, (it.current_x
13287 - (ITERATOR_AT_END_OF_LINE_P (&it)
13288 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13289 : (text_area_width / 2))))
13290 / FRAME_COLUMN_WIDTH (it.f);
13291 else if ((!row_r2l_p
13292 && w->cursor.x >= text_area_width - h_margin)
13293 || (row_r2l_p && w->cursor.x <= h_margin))
13295 if (hscroll_relative_p)
13296 wanted_x = text_area_width * (1 - hscroll_step_rel)
13297 - h_margin;
13298 else
13299 wanted_x = text_area_width
13300 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13301 - h_margin;
13302 hscroll
13303 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13305 else
13307 if (hscroll_relative_p)
13308 wanted_x = text_area_width * hscroll_step_rel
13309 + h_margin;
13310 else
13311 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13312 + h_margin;
13313 hscroll
13314 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13316 hscroll = max (hscroll, w->min_hscroll);
13318 /* Don't prevent redisplay optimizations if hscroll
13319 hasn't changed, as it will unnecessarily slow down
13320 redisplay. */
13321 if (w->hscroll != hscroll
13322 /* When hscrolling only the current line, we need to
13323 report hscroll even if its value is equal to the
13324 previous one, because the new line might need a
13325 different value. */
13326 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13328 struct buffer *b = XBUFFER (w->contents);
13329 b->prevent_redisplay_optimizations_p = true;
13330 w->hscroll = hscroll;
13331 hscrolled_p = true;
13336 window = w->next;
13339 /* Value is true if hscroll of any leaf window has been changed. */
13340 return hscrolled_p;
13344 /* Set hscroll so that cursor is visible and not inside horizontal
13345 scroll margins for all windows in the tree rooted at WINDOW. See
13346 also hscroll_window_tree above. Value is true if any window's
13347 hscroll has been changed. If it has, desired matrices on the frame
13348 of WINDOW are cleared. */
13350 static bool
13351 hscroll_windows (Lisp_Object window)
13353 bool hscrolled_p = hscroll_window_tree (window);
13354 if (hscrolled_p)
13355 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13356 return hscrolled_p;
13361 /************************************************************************
13362 Redisplay
13363 ************************************************************************/
13365 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13366 This is sometimes handy to have in a debugger session. */
13368 #ifdef GLYPH_DEBUG
13370 /* First and last unchanged row for try_window_id. */
13372 static int debug_first_unchanged_at_end_vpos;
13373 static int debug_last_unchanged_at_beg_vpos;
13375 /* Delta vpos and y. */
13377 static int debug_dvpos, debug_dy;
13379 /* Delta in characters and bytes for try_window_id. */
13381 static ptrdiff_t debug_delta, debug_delta_bytes;
13383 /* Values of window_end_pos and window_end_vpos at the end of
13384 try_window_id. */
13386 static ptrdiff_t debug_end_vpos;
13388 /* Append a string to W->desired_matrix->method. FMT is a printf
13389 format string. If trace_redisplay_p is true also printf the
13390 resulting string to stderr. */
13392 static void debug_method_add (struct window *, char const *, ...)
13393 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13395 static void
13396 debug_method_add (struct window *w, char const *fmt, ...)
13398 void *ptr = w;
13399 char *method = w->desired_matrix->method;
13400 int len = strlen (method);
13401 int size = sizeof w->desired_matrix->method;
13402 int remaining = size - len - 1;
13403 va_list ap;
13405 if (len && remaining)
13407 method[len] = '|';
13408 --remaining, ++len;
13411 va_start (ap, fmt);
13412 vsnprintf (method + len, remaining + 1, fmt, ap);
13413 va_end (ap);
13415 if (trace_redisplay_p)
13416 fprintf (stderr, "%p (%s): %s\n",
13417 ptr,
13418 ((BUFFERP (w->contents)
13419 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13420 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13421 : "no buffer"),
13422 method + len);
13425 #endif /* GLYPH_DEBUG */
13428 /* Value is true if all changes in window W, which displays
13429 current_buffer, are in the text between START and END. START is a
13430 buffer position, END is given as a distance from Z. Used in
13431 redisplay_internal for display optimization. */
13433 static bool
13434 text_outside_line_unchanged_p (struct window *w,
13435 ptrdiff_t start, ptrdiff_t end)
13437 bool unchanged_p = true;
13439 /* If text or overlays have changed, see where. */
13440 if (window_outdated (w))
13442 /* Gap in the line? */
13443 if (GPT < start || Z - GPT < end)
13444 unchanged_p = false;
13446 /* Changes start in front of the line, or end after it? */
13447 if (unchanged_p
13448 && (BEG_UNCHANGED < start - 1
13449 || END_UNCHANGED < end))
13450 unchanged_p = false;
13452 /* If selective display, can't optimize if changes start at the
13453 beginning of the line. */
13454 if (unchanged_p
13455 && INTEGERP (BVAR (current_buffer, selective_display))
13456 && XINT (BVAR (current_buffer, selective_display)) > 0
13457 && (BEG_UNCHANGED < start || GPT <= start))
13458 unchanged_p = false;
13460 /* If there are overlays at the start or end of the line, these
13461 may have overlay strings with newlines in them. A change at
13462 START, for instance, may actually concern the display of such
13463 overlay strings as well, and they are displayed on different
13464 lines. So, quickly rule out this case. (For the future, it
13465 might be desirable to implement something more telling than
13466 just BEG/END_UNCHANGED.) */
13467 if (unchanged_p)
13469 if (BEG + BEG_UNCHANGED == start
13470 && overlay_touches_p (start))
13471 unchanged_p = false;
13472 if (END_UNCHANGED == end
13473 && overlay_touches_p (Z - end))
13474 unchanged_p = false;
13477 /* Under bidi reordering, adding or deleting a character in the
13478 beginning of a paragraph, before the first strong directional
13479 character, can change the base direction of the paragraph (unless
13480 the buffer specifies a fixed paragraph direction), which will
13481 require redisplaying the whole paragraph. It might be worthwhile
13482 to find the paragraph limits and widen the range of redisplayed
13483 lines to that, but for now just give up this optimization. */
13484 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13485 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13486 unchanged_p = false;
13489 return unchanged_p;
13493 /* Do a frame update, taking possible shortcuts into account. This is
13494 the main external entry point for redisplay.
13496 If the last redisplay displayed an echo area message and that message
13497 is no longer requested, we clear the echo area or bring back the
13498 mini-buffer if that is in use. */
13500 void
13501 redisplay (void)
13503 redisplay_internal ();
13507 static Lisp_Object
13508 overlay_arrow_string_or_property (Lisp_Object var)
13510 Lisp_Object val;
13512 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13513 return val;
13515 return Voverlay_arrow_string;
13518 /* Return true if there are any overlay-arrows in current_buffer. */
13519 static bool
13520 overlay_arrow_in_current_buffer_p (void)
13522 Lisp_Object vlist;
13524 for (vlist = Voverlay_arrow_variable_list;
13525 CONSP (vlist);
13526 vlist = XCDR (vlist))
13528 Lisp_Object var = XCAR (vlist);
13529 Lisp_Object val;
13531 if (!SYMBOLP (var))
13532 continue;
13533 val = find_symbol_value (var);
13534 if (MARKERP (val)
13535 && current_buffer == XMARKER (val)->buffer)
13536 return true;
13538 return false;
13542 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13543 has changed.
13544 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13545 buffers that are affected. */
13547 static bool
13548 overlay_arrows_changed_p (bool set_redisplay)
13550 Lisp_Object vlist;
13551 bool changed = false;
13553 for (vlist = Voverlay_arrow_variable_list;
13554 CONSP (vlist);
13555 vlist = XCDR (vlist))
13557 Lisp_Object var = XCAR (vlist);
13558 Lisp_Object val, pstr;
13560 if (!SYMBOLP (var))
13561 continue;
13562 val = find_symbol_value (var);
13563 if (!MARKERP (val))
13564 continue;
13565 if (! EQ (COERCE_MARKER (val),
13566 /* FIXME: Don't we have a problem, using such a global
13567 * "last-position" if the variable is buffer-local? */
13568 Fget (var, Qlast_arrow_position))
13569 || ! (pstr = overlay_arrow_string_or_property (var),
13570 EQ (pstr, Fget (var, Qlast_arrow_string))))
13572 struct buffer *buf = XMARKER (val)->buffer;
13574 if (set_redisplay)
13576 if (buf)
13577 bset_redisplay (buf);
13578 changed = true;
13580 else
13581 return true;
13584 return changed;
13587 /* Mark overlay arrows to be updated on next redisplay. */
13589 static void
13590 update_overlay_arrows (int up_to_date)
13592 Lisp_Object vlist;
13594 for (vlist = Voverlay_arrow_variable_list;
13595 CONSP (vlist);
13596 vlist = XCDR (vlist))
13598 Lisp_Object var = XCAR (vlist);
13600 if (!SYMBOLP (var))
13601 continue;
13603 if (up_to_date > 0)
13605 Lisp_Object val = find_symbol_value (var);
13606 if (!MARKERP (val))
13607 continue;
13608 Fput (var, Qlast_arrow_position,
13609 COERCE_MARKER (val));
13610 Fput (var, Qlast_arrow_string,
13611 overlay_arrow_string_or_property (var));
13613 else if (up_to_date < 0
13614 || !NILP (Fget (var, Qlast_arrow_position)))
13616 Fput (var, Qlast_arrow_position, Qt);
13617 Fput (var, Qlast_arrow_string, Qt);
13623 /* Return overlay arrow string to display at row.
13624 Return integer (bitmap number) for arrow bitmap in left fringe.
13625 Return nil if no overlay arrow. */
13627 static Lisp_Object
13628 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13630 Lisp_Object vlist;
13632 for (vlist = Voverlay_arrow_variable_list;
13633 CONSP (vlist);
13634 vlist = XCDR (vlist))
13636 Lisp_Object var = XCAR (vlist);
13637 Lisp_Object val;
13639 if (!SYMBOLP (var))
13640 continue;
13642 val = find_symbol_value (var);
13644 if (MARKERP (val)
13645 && current_buffer == XMARKER (val)->buffer
13646 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13648 if (FRAME_WINDOW_P (it->f)
13649 /* FIXME: if ROW->reversed_p is set, this should test
13650 the right fringe, not the left one. */
13651 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13653 #ifdef HAVE_WINDOW_SYSTEM
13654 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13656 int fringe_bitmap = lookup_fringe_bitmap (val);
13657 if (fringe_bitmap != 0)
13658 return make_number (fringe_bitmap);
13660 #endif
13661 return make_number (-1); /* Use default arrow bitmap. */
13663 return overlay_arrow_string_or_property (var);
13667 return Qnil;
13670 /* Return true if point moved out of or into a composition. Otherwise
13671 return false. PREV_BUF and PREV_PT are the last point buffer and
13672 position. BUF and PT are the current point buffer and position. */
13674 static bool
13675 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13676 struct buffer *buf, ptrdiff_t pt)
13678 ptrdiff_t start, end;
13679 Lisp_Object prop;
13680 Lisp_Object buffer;
13682 XSETBUFFER (buffer, buf);
13683 /* Check a composition at the last point if point moved within the
13684 same buffer. */
13685 if (prev_buf == buf)
13687 if (prev_pt == pt)
13688 /* Point didn't move. */
13689 return false;
13691 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13692 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13693 && composition_valid_p (start, end, prop)
13694 && start < prev_pt && end > prev_pt)
13695 /* The last point was within the composition. Return true iff
13696 point moved out of the composition. */
13697 return (pt <= start || pt >= end);
13700 /* Check a composition at the current point. */
13701 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13702 && find_composition (pt, -1, &start, &end, &prop, buffer)
13703 && composition_valid_p (start, end, prop)
13704 && start < pt && end > pt);
13707 /* Reconsider the clip changes of buffer which is displayed in W. */
13709 static void
13710 reconsider_clip_changes (struct window *w)
13712 struct buffer *b = XBUFFER (w->contents);
13714 if (b->clip_changed
13715 && w->window_end_valid
13716 && w->current_matrix->buffer == b
13717 && w->current_matrix->zv == BUF_ZV (b)
13718 && w->current_matrix->begv == BUF_BEGV (b))
13719 b->clip_changed = false;
13721 /* If display wasn't paused, and W is not a tool bar window, see if
13722 point has been moved into or out of a composition. In that case,
13723 set b->clip_changed to force updating the screen. If
13724 b->clip_changed has already been set, skip this check. */
13725 if (!b->clip_changed && w->window_end_valid)
13727 ptrdiff_t pt = (w == XWINDOW (selected_window)
13728 ? PT : marker_position (w->pointm));
13730 if ((w->current_matrix->buffer != b || pt != w->last_point)
13731 && check_point_in_composition (w->current_matrix->buffer,
13732 w->last_point, b, pt))
13733 b->clip_changed = true;
13737 static void
13738 propagate_buffer_redisplay (void)
13739 { /* Resetting b->text->redisplay is problematic!
13740 We can't just reset it in the case that some window that displays
13741 it has not been redisplayed; and such a window can stay
13742 unredisplayed for a long time if it's currently invisible.
13743 But we do want to reset it at the end of redisplay otherwise
13744 its displayed windows will keep being redisplayed over and over
13745 again.
13746 So we copy all b->text->redisplay flags up to their windows here,
13747 such that mark_window_display_accurate can safely reset
13748 b->text->redisplay. */
13749 Lisp_Object ws = window_list ();
13750 for (; CONSP (ws); ws = XCDR (ws))
13752 struct window *thisw = XWINDOW (XCAR (ws));
13753 struct buffer *thisb = XBUFFER (thisw->contents);
13754 if (thisb->text->redisplay)
13755 thisw->redisplay = true;
13759 #define STOP_POLLING \
13760 do { if (! polling_stopped_here) stop_polling (); \
13761 polling_stopped_here = true; } while (false)
13763 #define RESUME_POLLING \
13764 do { if (polling_stopped_here) start_polling (); \
13765 polling_stopped_here = false; } while (false)
13768 /* Perhaps in the future avoid recentering windows if it
13769 is not necessary; currently that causes some problems. */
13771 static void
13772 redisplay_internal (void)
13774 struct window *w = XWINDOW (selected_window);
13775 struct window *sw;
13776 struct frame *fr;
13777 bool pending;
13778 bool must_finish = false, match_p;
13779 struct text_pos tlbufpos, tlendpos;
13780 int number_of_visible_frames;
13781 ptrdiff_t count;
13782 struct frame *sf;
13783 bool polling_stopped_here = false;
13784 Lisp_Object tail, frame;
13786 /* Set a limit to the number of retries we perform due to horizontal
13787 scrolling, this avoids getting stuck in an uninterruptible
13788 infinite loop (Bug #24633). */
13789 enum { MAX_HSCROLL_RETRIES = 16 };
13790 int hscroll_retries = 0;
13792 /* Limit the number of retries for when frame(s) become garbaged as
13793 result of redisplaying them. Some packages set various redisplay
13794 hooks, such as window-scroll-functions, to run Lisp that always
13795 calls APIs which cause the frame's garbaged flag to become set,
13796 so we loop indefinitely. */
13797 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13798 int garbaged_frame_retries = 0;
13800 /* True means redisplay has to consider all windows on all
13801 frames. False, only selected_window is considered. */
13802 bool consider_all_windows_p;
13804 /* True means redisplay has to redisplay the miniwindow. */
13805 bool update_miniwindow_p = false;
13807 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13809 /* No redisplay if running in batch mode or frame is not yet fully
13810 initialized, or redisplay is explicitly turned off by setting
13811 Vinhibit_redisplay. */
13812 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13813 || !NILP (Vinhibit_redisplay))
13814 return;
13816 /* Don't examine these until after testing Vinhibit_redisplay.
13817 When Emacs is shutting down, perhaps because its connection to
13818 X has dropped, we should not look at them at all. */
13819 fr = XFRAME (w->frame);
13820 sf = SELECTED_FRAME ();
13822 if (!fr->glyphs_initialized_p)
13823 return;
13825 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13826 if (popup_activated ())
13827 return;
13828 #endif
13830 /* I don't think this happens but let's be paranoid. */
13831 if (redisplaying_p)
13832 return;
13834 /* Record a function that clears redisplaying_p
13835 when we leave this function. */
13836 count = SPECPDL_INDEX ();
13837 record_unwind_protect_void (unwind_redisplay);
13838 redisplaying_p = true;
13839 block_buffer_flips ();
13840 specbind (Qinhibit_free_realized_faces, Qnil);
13842 /* Record this function, so it appears on the profiler's backtraces. */
13843 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13845 FOR_EACH_FRAME (tail, frame)
13846 XFRAME (frame)->already_hscrolled_p = false;
13848 retry:
13849 /* Remember the currently selected window. */
13850 sw = w;
13852 pending = false;
13853 forget_escape_and_glyphless_faces ();
13855 inhibit_free_realized_faces = false;
13857 /* If face_change, init_iterator will free all realized faces, which
13858 includes the faces referenced from current matrices. So, we
13859 can't reuse current matrices in this case. */
13860 if (face_change)
13861 windows_or_buffers_changed = 47;
13863 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13864 && FRAME_TTY (sf)->previous_frame != sf)
13866 /* Since frames on a single ASCII terminal share the same
13867 display area, displaying a different frame means redisplay
13868 the whole thing. */
13869 SET_FRAME_GARBAGED (sf);
13870 #ifndef DOS_NT
13871 set_tty_color_mode (FRAME_TTY (sf), sf);
13872 #endif
13873 FRAME_TTY (sf)->previous_frame = sf;
13876 /* Set the visible flags for all frames. Do this before checking for
13877 resized or garbaged frames; they want to know if their frames are
13878 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13879 number_of_visible_frames = 0;
13881 FOR_EACH_FRAME (tail, frame)
13883 struct frame *f = XFRAME (frame);
13885 if (FRAME_VISIBLE_P (f))
13887 ++number_of_visible_frames;
13888 /* Adjust matrices for visible frames only. */
13889 if (f->fonts_changed)
13891 adjust_frame_glyphs (f);
13892 /* Disable all redisplay optimizations for this frame.
13893 This is because adjust_frame_glyphs resets the
13894 enabled_p flag for all glyph rows of all windows, so
13895 many optimizations will fail anyway, and some might
13896 fail to test that flag and do bogus things as
13897 result. */
13898 SET_FRAME_GARBAGED (f);
13899 f->fonts_changed = false;
13901 /* If cursor type has been changed on the frame
13902 other than selected, consider all frames. */
13903 if (f != sf && f->cursor_type_changed)
13904 fset_redisplay (f);
13906 clear_desired_matrices (f);
13909 /* Notice any pending interrupt request to change frame size. */
13910 do_pending_window_change (true);
13912 /* do_pending_window_change could change the selected_window due to
13913 frame resizing which makes the selected window too small. */
13914 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13915 sw = w;
13917 /* Clear frames marked as garbaged. */
13918 clear_garbaged_frames ();
13920 /* Build menubar and tool-bar items. */
13921 if (NILP (Vmemory_full))
13922 prepare_menu_bars ();
13924 reconsider_clip_changes (w);
13926 /* In most cases selected window displays current buffer. */
13927 match_p = XBUFFER (w->contents) == current_buffer;
13928 if (match_p)
13930 /* Detect case that we need to write or remove a star in the mode line. */
13931 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13932 w->update_mode_line = true;
13934 if (mode_line_update_needed (w))
13935 w->update_mode_line = true;
13937 /* If reconsider_clip_changes above decided that the narrowing
13938 in the current buffer changed, make sure all other windows
13939 showing that buffer will be redisplayed. */
13940 if (current_buffer->clip_changed)
13941 bset_update_mode_line (current_buffer);
13944 /* Normally the message* functions will have already displayed and
13945 updated the echo area, but the frame may have been trashed, or
13946 the update may have been preempted, so display the echo area
13947 again here. Checking message_cleared_p captures the case that
13948 the echo area should be cleared. */
13949 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13950 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13951 || (message_cleared_p
13952 && minibuf_level == 0
13953 /* If the mini-window is currently selected, this means the
13954 echo-area doesn't show through. */
13955 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13957 echo_area_display (false);
13959 /* If echo_area_display resizes the mini-window, the redisplay and
13960 window_sizes_changed flags of the selected frame are set, but
13961 it's too late for the hooks in window-size-change-functions,
13962 which have been examined already in prepare_menu_bars. So in
13963 that case we call the hooks here only for the selected frame. */
13964 if (sf->redisplay)
13966 ptrdiff_t count1 = SPECPDL_INDEX ();
13968 record_unwind_save_match_data ();
13969 run_window_size_change_functions (selected_frame);
13970 unbind_to (count1, Qnil);
13973 if (message_cleared_p)
13974 update_miniwindow_p = true;
13976 must_finish = true;
13978 /* If we don't display the current message, don't clear the
13979 message_cleared_p flag, because, if we did, we wouldn't clear
13980 the echo area in the next redisplay which doesn't preserve
13981 the echo area. */
13982 if (!display_last_displayed_message_p)
13983 message_cleared_p = false;
13985 else if (EQ (selected_window, minibuf_window)
13986 && (current_buffer->clip_changed || window_outdated (w))
13987 && resize_mini_window (w, false))
13989 if (sf->redisplay)
13991 ptrdiff_t count1 = SPECPDL_INDEX ();
13993 record_unwind_save_match_data ();
13994 run_window_size_change_functions (selected_frame);
13995 unbind_to (count1, Qnil);
13998 /* Resized active mini-window to fit the size of what it is
13999 showing if its contents might have changed. */
14000 must_finish = true;
14002 /* If window configuration was changed, frames may have been
14003 marked garbaged. Clear them or we will experience
14004 surprises wrt scrolling. */
14005 clear_garbaged_frames ();
14008 if (windows_or_buffers_changed && !update_mode_lines)
14009 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14010 only the windows's contents needs to be refreshed, or whether the
14011 mode-lines also need a refresh. */
14012 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14013 ? REDISPLAY_SOME : 32);
14015 /* If specs for an arrow have changed, do thorough redisplay
14016 to ensure we remove any arrow that should no longer exist. */
14017 /* Apparently, this is the only case where we update other windows,
14018 without updating other mode-lines. */
14019 overlay_arrows_changed_p (true);
14021 consider_all_windows_p = (update_mode_lines
14022 || windows_or_buffers_changed);
14024 #define AINC(a,i) \
14026 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14027 if (INTEGERP (entry)) \
14028 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14031 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14032 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14034 /* Optimize the case that only the line containing the cursor in the
14035 selected window has changed. Variables starting with this_ are
14036 set in display_line and record information about the line
14037 containing the cursor. */
14038 tlbufpos = this_line_start_pos;
14039 tlendpos = this_line_end_pos;
14040 if (!consider_all_windows_p
14041 && CHARPOS (tlbufpos) > 0
14042 && !w->update_mode_line
14043 && !current_buffer->clip_changed
14044 && !current_buffer->prevent_redisplay_optimizations_p
14045 && FRAME_VISIBLE_P (XFRAME (w->frame))
14046 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14047 && !XFRAME (w->frame)->cursor_type_changed
14048 && !XFRAME (w->frame)->face_change
14049 /* Make sure recorded data applies to current buffer, etc. */
14050 && this_line_buffer == current_buffer
14051 && match_p
14052 && !w->force_start
14053 && !w->optional_new_start
14054 /* Point must be on the line that we have info recorded about. */
14055 && PT >= CHARPOS (tlbufpos)
14056 && PT <= Z - CHARPOS (tlendpos)
14057 /* All text outside that line, including its final newline,
14058 must be unchanged. */
14059 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14060 CHARPOS (tlendpos)))
14062 if (CHARPOS (tlbufpos) > BEGV
14063 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14064 && (CHARPOS (tlbufpos) == ZV
14065 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14066 /* Former continuation line has disappeared by becoming empty. */
14067 goto cancel;
14068 else if (window_outdated (w) || MINI_WINDOW_P (w))
14070 /* We have to handle the case of continuation around a
14071 wide-column character (see the comment in indent.c around
14072 line 1340).
14074 For instance, in the following case:
14076 -------- Insert --------
14077 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14078 J_I_ ==> J_I_ `^^' are cursors.
14079 ^^ ^^
14080 -------- --------
14082 As we have to redraw the line above, we cannot use this
14083 optimization. */
14085 struct it it;
14086 int line_height_before = this_line_pixel_height;
14088 /* Note that start_display will handle the case that the
14089 line starting at tlbufpos is a continuation line. */
14090 start_display (&it, w, tlbufpos);
14092 /* Implementation note: It this still necessary? */
14093 if (it.current_x != this_line_start_x)
14094 goto cancel;
14096 TRACE ((stderr, "trying display optimization 1\n"));
14097 w->cursor.vpos = -1;
14098 overlay_arrow_seen = false;
14099 it.vpos = this_line_vpos;
14100 it.current_y = this_line_y;
14101 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14102 display_line (&it, -1);
14104 /* If line contains point, is not continued,
14105 and ends at same distance from eob as before, we win. */
14106 if (w->cursor.vpos >= 0
14107 /* Line is not continued, otherwise this_line_start_pos
14108 would have been set to 0 in display_line. */
14109 && CHARPOS (this_line_start_pos)
14110 /* Line ends as before. */
14111 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14112 /* Line has same height as before. Otherwise other lines
14113 would have to be shifted up or down. */
14114 && this_line_pixel_height == line_height_before)
14116 /* If this is not the window's last line, we must adjust
14117 the charstarts of the lines below. */
14118 if (it.current_y < it.last_visible_y)
14120 struct glyph_row *row
14121 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14122 ptrdiff_t delta, delta_bytes;
14124 /* We used to distinguish between two cases here,
14125 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14126 when the line ends in a newline or the end of the
14127 buffer's accessible portion. But both cases did
14128 the same, so they were collapsed. */
14129 delta = (Z
14130 - CHARPOS (tlendpos)
14131 - MATRIX_ROW_START_CHARPOS (row));
14132 delta_bytes = (Z_BYTE
14133 - BYTEPOS (tlendpos)
14134 - MATRIX_ROW_START_BYTEPOS (row));
14136 increment_matrix_positions (w->current_matrix,
14137 this_line_vpos + 1,
14138 w->current_matrix->nrows,
14139 delta, delta_bytes);
14142 /* If this row displays text now but previously didn't,
14143 or vice versa, w->window_end_vpos may have to be
14144 adjusted. */
14145 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14147 if (w->window_end_vpos < this_line_vpos)
14148 w->window_end_vpos = this_line_vpos;
14150 else if (w->window_end_vpos == this_line_vpos
14151 && this_line_vpos > 0)
14152 w->window_end_vpos = this_line_vpos - 1;
14153 w->window_end_valid = false;
14155 /* Update hint: No need to try to scroll in update_window. */
14156 w->desired_matrix->no_scrolling_p = true;
14158 #ifdef GLYPH_DEBUG
14159 *w->desired_matrix->method = 0;
14160 debug_method_add (w, "optimization 1");
14161 #endif
14162 #ifdef HAVE_WINDOW_SYSTEM
14163 update_window_fringes (w, false);
14164 #endif
14165 goto update;
14167 else
14168 goto cancel;
14170 else if (/* Cursor position hasn't changed. */
14171 PT == w->last_point
14172 /* Make sure the cursor was last displayed
14173 in this window. Otherwise we have to reposition it. */
14175 /* PXW: Must be converted to pixels, probably. */
14176 && 0 <= w->cursor.vpos
14177 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14179 if (!must_finish)
14181 do_pending_window_change (true);
14182 /* If selected_window changed, redisplay again. */
14183 if (WINDOWP (selected_window)
14184 && (w = XWINDOW (selected_window)) != sw)
14185 goto retry;
14187 /* We used to always goto end_of_redisplay here, but this
14188 isn't enough if we have a blinking cursor. */
14189 if (w->cursor_off_p == w->last_cursor_off_p)
14190 goto end_of_redisplay;
14192 goto update;
14194 /* If highlighting the region, or if the cursor is in the echo area,
14195 then we can't just move the cursor. */
14196 else if (NILP (Vshow_trailing_whitespace)
14197 && !cursor_in_echo_area)
14199 struct it it;
14200 struct glyph_row *row;
14202 /* Skip from tlbufpos to PT and see where it is. Note that
14203 PT may be in invisible text. If so, we will end at the
14204 next visible position. */
14205 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14206 NULL, DEFAULT_FACE_ID);
14207 it.current_x = this_line_start_x;
14208 it.current_y = this_line_y;
14209 it.vpos = this_line_vpos;
14211 /* The call to move_it_to stops in front of PT, but
14212 moves over before-strings. */
14213 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14215 if (it.vpos == this_line_vpos
14216 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14217 row->enabled_p))
14219 eassert (this_line_vpos == it.vpos);
14220 eassert (this_line_y == it.current_y);
14221 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14222 if (cursor_row_fully_visible_p (w, false, true))
14224 #ifdef GLYPH_DEBUG
14225 *w->desired_matrix->method = 0;
14226 debug_method_add (w, "optimization 3");
14227 #endif
14228 goto update;
14230 else
14231 goto cancel;
14233 else
14234 goto cancel;
14237 cancel:
14238 /* Text changed drastically or point moved off of line. */
14239 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14242 CHARPOS (this_line_start_pos) = 0;
14243 ++clear_face_cache_count;
14244 #ifdef HAVE_WINDOW_SYSTEM
14245 ++clear_image_cache_count;
14246 #endif
14248 /* Build desired matrices, and update the display. If
14249 consider_all_windows_p, do it for all windows on all frames that
14250 require redisplay, as specified by their 'redisplay' flag.
14251 Otherwise do it for selected_window, only. */
14253 if (consider_all_windows_p)
14255 FOR_EACH_FRAME (tail, frame)
14256 XFRAME (frame)->updated_p = false;
14258 propagate_buffer_redisplay ();
14260 FOR_EACH_FRAME (tail, frame)
14262 struct frame *f = XFRAME (frame);
14264 /* We don't have to do anything for unselected terminal
14265 frames. */
14266 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14267 && !EQ (FRAME_TTY (f)->top_frame, frame))
14268 continue;
14270 retry_frame:
14271 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14273 bool gcscrollbars
14274 /* Only GC scrollbars when we redisplay the whole frame. */
14275 = f->redisplay || !REDISPLAY_SOME_P ();
14276 bool f_redisplay_flag = f->redisplay;
14277 /* Mark all the scroll bars to be removed; we'll redeem
14278 the ones we want when we redisplay their windows. */
14279 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14280 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14282 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14283 redisplay_windows (FRAME_ROOT_WINDOW (f));
14284 /* Remember that the invisible frames need to be redisplayed next
14285 time they're visible. */
14286 else if (!REDISPLAY_SOME_P ())
14287 f->redisplay = true;
14289 /* The X error handler may have deleted that frame. */
14290 if (!FRAME_LIVE_P (f))
14291 continue;
14293 /* Any scroll bars which redisplay_windows should have
14294 nuked should now go away. */
14295 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14296 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14298 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14300 /* If fonts changed on visible frame, display again. */
14301 if (f->fonts_changed)
14303 adjust_frame_glyphs (f);
14304 /* Disable all redisplay optimizations for this
14305 frame. For the reasons, see the comment near
14306 the previous call to adjust_frame_glyphs above. */
14307 SET_FRAME_GARBAGED (f);
14308 f->fonts_changed = false;
14309 goto retry_frame;
14312 /* See if we have to hscroll. */
14313 if (!f->already_hscrolled_p)
14315 f->already_hscrolled_p = true;
14316 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14317 && hscroll_windows (f->root_window))
14319 hscroll_retries++;
14320 goto retry_frame;
14324 /* If the frame's redisplay flag was not set before
14325 we went about redisplaying its windows, but it is
14326 set now, that means we employed some redisplay
14327 optimizations inside redisplay_windows, and
14328 bypassed producing some screen lines. But if
14329 f->redisplay is now set, it might mean the old
14330 faces are no longer valid (e.g., if redisplaying
14331 some window called some Lisp which defined a new
14332 face or redefined an existing face), so trying to
14333 use them in update_frame will segfault.
14334 Therefore, we must redisplay this frame. */
14335 if (!f_redisplay_flag && f->redisplay)
14336 goto retry_frame;
14337 /* In some case (e.g., window resize), we notice
14338 only during window updating that the window
14339 content changed unpredictably (e.g., a GTK
14340 scrollbar moved, or some Lisp hook that winds up
14341 calling adjust_frame_glyphs) and that our
14342 previous estimation of the frame content was
14343 garbage. We have to start over. These cases
14344 should be rare, so going all the way back to the
14345 top of redisplay should be good enough. */
14346 if (FRAME_GARBAGED_P (f)
14347 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14348 goto retry;
14350 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14351 x_clear_under_internal_border (f);
14352 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14354 /* Prevent various kinds of signals during display
14355 update. stdio is not robust about handling
14356 signals, which can cause an apparent I/O error. */
14357 if (interrupt_input)
14358 unrequest_sigio ();
14359 STOP_POLLING;
14361 pending |= update_frame (f, false, false);
14362 f->cursor_type_changed = false;
14363 f->updated_p = true;
14368 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14370 if (!pending)
14372 /* Do the mark_window_display_accurate after all windows have
14373 been redisplayed because this call resets flags in buffers
14374 which are needed for proper redisplay. */
14375 FOR_EACH_FRAME (tail, frame)
14377 struct frame *f = XFRAME (frame);
14378 if (f->updated_p)
14380 f->redisplay = false;
14381 f->garbaged = false;
14382 mark_window_display_accurate (f->root_window, true);
14383 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14384 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14389 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14391 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14392 /* Use list_of_error, not Qerror, so that
14393 we catch only errors and don't run the debugger. */
14394 internal_condition_case_1 (redisplay_window_1, selected_window,
14395 list_of_error,
14396 redisplay_window_error);
14397 if (update_miniwindow_p)
14398 internal_condition_case_1 (redisplay_window_1,
14399 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14400 redisplay_window_error);
14402 /* Compare desired and current matrices, perform output. */
14404 update:
14405 /* If fonts changed, display again. Likewise if redisplay_window_1
14406 above caused some change (e.g., a change in faces) that requires
14407 considering the entire frame again. */
14408 if (sf->fonts_changed || sf->redisplay)
14410 if (sf->redisplay)
14412 /* Set this to force a more thorough redisplay.
14413 Otherwise, we might immediately loop back to the
14414 above "else-if" clause (since all the conditions that
14415 led here might still be true), and we will then
14416 infloop, because the selected-frame's redisplay flag
14417 is not (and cannot be) reset. */
14418 windows_or_buffers_changed = 50;
14420 goto retry;
14423 /* Prevent freeing of realized faces, since desired matrices are
14424 pending that reference the faces we computed and cached. */
14425 inhibit_free_realized_faces = true;
14427 /* Prevent various kinds of signals during display update.
14428 stdio is not robust about handling signals,
14429 which can cause an apparent I/O error. */
14430 if (interrupt_input)
14431 unrequest_sigio ();
14432 STOP_POLLING;
14434 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14436 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14437 && hscroll_windows (selected_window))
14439 hscroll_retries++;
14440 goto retry;
14443 XWINDOW (selected_window)->must_be_updated_p = true;
14444 pending = update_frame (sf, false, false);
14445 sf->cursor_type_changed = false;
14448 /* We may have called echo_area_display at the top of this
14449 function. If the echo area is on another frame, that may
14450 have put text on a frame other than the selected one, so the
14451 above call to update_frame would not have caught it. Catch
14452 it here. */
14453 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14454 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14456 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14458 XWINDOW (mini_window)->must_be_updated_p = true;
14459 pending |= update_frame (mini_frame, false, false);
14460 mini_frame->cursor_type_changed = false;
14461 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14462 && hscroll_windows (mini_window))
14464 hscroll_retries++;
14465 goto retry;
14470 /* If display was paused because of pending input, make sure we do a
14471 thorough update the next time. */
14472 if (pending)
14474 /* Prevent the optimization at the beginning of
14475 redisplay_internal that tries a single-line update of the
14476 line containing the cursor in the selected window. */
14477 CHARPOS (this_line_start_pos) = 0;
14479 /* Let the overlay arrow be updated the next time. */
14480 update_overlay_arrows (0);
14482 /* If we pause after scrolling, some rows in the current
14483 matrices of some windows are not valid. */
14484 if (!WINDOW_FULL_WIDTH_P (w)
14485 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14486 update_mode_lines = 36;
14488 else
14490 if (!consider_all_windows_p)
14492 /* This has already been done above if
14493 consider_all_windows_p is set. */
14494 if (XBUFFER (w->contents)->text->redisplay
14495 && buffer_window_count (XBUFFER (w->contents)) > 1)
14496 /* This can happen if b->text->redisplay was set during
14497 jit-lock. */
14498 propagate_buffer_redisplay ();
14499 mark_window_display_accurate_1 (w, true);
14501 /* Say overlay arrows are up to date. */
14502 update_overlay_arrows (1);
14504 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14505 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14508 update_mode_lines = 0;
14509 windows_or_buffers_changed = 0;
14512 /* Start SIGIO interrupts coming again. Having them off during the
14513 code above makes it less likely one will discard output, but not
14514 impossible, since there might be stuff in the system buffer here.
14515 But it is much hairier to try to do anything about that. */
14516 if (interrupt_input)
14517 request_sigio ();
14518 RESUME_POLLING;
14520 /* If a frame has become visible which was not before, redisplay
14521 again, so that we display it. Expose events for such a frame
14522 (which it gets when becoming visible) don't call the parts of
14523 redisplay constructing glyphs, so simply exposing a frame won't
14524 display anything in this case. So, we have to display these
14525 frames here explicitly. */
14526 if (!pending)
14528 int new_count = 0;
14530 FOR_EACH_FRAME (tail, frame)
14532 if (XFRAME (frame)->visible)
14533 new_count++;
14536 if (new_count != number_of_visible_frames)
14537 windows_or_buffers_changed = 52;
14540 /* Change frame size now if a change is pending. */
14541 do_pending_window_change (true);
14543 /* If we just did a pending size change, or have additional
14544 visible frames, or selected_window changed, redisplay again. */
14545 if ((windows_or_buffers_changed && !pending)
14546 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14547 goto retry;
14549 /* Clear the face and image caches.
14551 We used to do this only if consider_all_windows_p. But the cache
14552 needs to be cleared if a timer creates images in the current
14553 buffer (e.g. the test case in Bug#6230). */
14555 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14557 clear_face_cache (false);
14558 clear_face_cache_count = 0;
14561 #ifdef HAVE_WINDOW_SYSTEM
14562 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14564 clear_image_caches (Qnil);
14565 clear_image_cache_count = 0;
14567 #endif /* HAVE_WINDOW_SYSTEM */
14569 end_of_redisplay:
14570 #ifdef HAVE_NS
14571 ns_set_doc_edited ();
14572 #endif
14573 if (interrupt_input && interrupts_deferred)
14574 request_sigio ();
14576 unbind_to (count, Qnil);
14577 RESUME_POLLING;
14580 static void
14581 unwind_redisplay_preserve_echo_area (void)
14583 unblock_buffer_flips ();
14586 /* Redisplay, but leave alone any recent echo area message unless
14587 another message has been requested in its place.
14589 This is useful in situations where you need to redisplay but no
14590 user action has occurred, making it inappropriate for the message
14591 area to be cleared. See tracking_off and
14592 wait_reading_process_output for examples of these situations.
14594 FROM_WHERE is an integer saying from where this function was
14595 called. This is useful for debugging. */
14597 void
14598 redisplay_preserve_echo_area (int from_where)
14600 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14602 block_input ();
14603 ptrdiff_t count = SPECPDL_INDEX ();
14604 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14605 block_buffer_flips ();
14606 unblock_input ();
14608 if (!NILP (echo_area_buffer[1]))
14610 /* We have a previously displayed message, but no current
14611 message. Redisplay the previous message. */
14612 display_last_displayed_message_p = true;
14613 redisplay_internal ();
14614 display_last_displayed_message_p = false;
14616 else
14617 redisplay_internal ();
14619 flush_frame (SELECTED_FRAME ());
14620 unbind_to (count, Qnil);
14624 /* Function registered with record_unwind_protect in redisplay_internal. */
14626 static void
14627 unwind_redisplay (void)
14629 redisplaying_p = false;
14630 unblock_buffer_flips ();
14634 /* Mark the display of leaf window W as accurate or inaccurate.
14635 If ACCURATE_P, mark display of W as accurate.
14636 If !ACCURATE_P, arrange for W to be redisplayed the next
14637 time redisplay_internal is called. */
14639 static void
14640 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14642 struct buffer *b = XBUFFER (w->contents);
14644 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14645 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14646 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14648 if (accurate_p)
14650 b->clip_changed = false;
14651 b->prevent_redisplay_optimizations_p = false;
14652 eassert (buffer_window_count (b) > 0);
14653 /* Resetting b->text->redisplay is problematic!
14654 In order to make it safer to do it here, redisplay_internal must
14655 have copied all b->text->redisplay to their respective windows. */
14656 b->text->redisplay = false;
14658 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14659 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14660 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14661 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14663 w->current_matrix->buffer = b;
14664 w->current_matrix->begv = BUF_BEGV (b);
14665 w->current_matrix->zv = BUF_ZV (b);
14667 w->last_cursor_vpos = w->cursor.vpos;
14668 w->last_cursor_off_p = w->cursor_off_p;
14670 if (w == XWINDOW (selected_window))
14671 w->last_point = BUF_PT (b);
14672 else
14673 w->last_point = marker_position (w->pointm);
14675 w->window_end_valid = true;
14676 w->update_mode_line = false;
14679 w->redisplay = !accurate_p;
14683 /* Mark the display of windows in the window tree rooted at WINDOW as
14684 accurate or inaccurate. If ACCURATE_P, mark display of
14685 windows as accurate. If !ACCURATE_P, arrange for windows to
14686 be redisplayed the next time redisplay_internal is called. */
14688 void
14689 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14691 struct window *w;
14693 for (; !NILP (window); window = w->next)
14695 w = XWINDOW (window);
14696 if (WINDOWP (w->contents))
14697 mark_window_display_accurate (w->contents, accurate_p);
14698 else
14699 mark_window_display_accurate_1 (w, accurate_p);
14702 if (accurate_p)
14703 update_overlay_arrows (1);
14704 else
14705 /* Force a thorough redisplay the next time by setting
14706 last_arrow_position and last_arrow_string to t, which is
14707 unequal to any useful value of Voverlay_arrow_... */
14708 update_overlay_arrows (-1);
14712 /* Return value in display table DP (Lisp_Char_Table *) for character
14713 C. Since a display table doesn't have any parent, we don't have to
14714 follow parent. Do not call this function directly but use the
14715 macro DISP_CHAR_VECTOR. */
14717 Lisp_Object
14718 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14720 Lisp_Object val;
14722 if (ASCII_CHAR_P (c))
14724 val = dp->ascii;
14725 if (SUB_CHAR_TABLE_P (val))
14726 val = XSUB_CHAR_TABLE (val)->contents[c];
14728 else
14730 Lisp_Object table;
14732 XSETCHAR_TABLE (table, dp);
14733 val = char_table_ref (table, c);
14735 if (NILP (val))
14736 val = dp->defalt;
14737 return val;
14740 static int buffer_flip_blocked_depth;
14742 static void
14743 block_buffer_flips (void)
14745 eassert (buffer_flip_blocked_depth >= 0);
14746 buffer_flip_blocked_depth++;
14749 static void
14750 unblock_buffer_flips (void)
14752 eassert (buffer_flip_blocked_depth > 0);
14753 if (--buffer_flip_blocked_depth == 0)
14755 Lisp_Object tail, frame;
14756 block_input ();
14757 FOR_EACH_FRAME (tail, frame)
14759 struct frame *f = XFRAME (frame);
14760 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14761 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14763 unblock_input ();
14767 bool
14768 buffer_flipping_blocked_p (void)
14770 return buffer_flip_blocked_depth > 0;
14774 /***********************************************************************
14775 Window Redisplay
14776 ***********************************************************************/
14778 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14780 static void
14781 redisplay_windows (Lisp_Object window)
14783 while (!NILP (window))
14785 struct window *w = XWINDOW (window);
14787 if (WINDOWP (w->contents))
14788 redisplay_windows (w->contents);
14789 else if (BUFFERP (w->contents))
14791 displayed_buffer = XBUFFER (w->contents);
14792 /* Use list_of_error, not Qerror, so that
14793 we catch only errors and don't run the debugger. */
14794 internal_condition_case_1 (redisplay_window_0, window,
14795 list_of_error,
14796 redisplay_window_error);
14799 window = w->next;
14803 static Lisp_Object
14804 redisplay_window_error (Lisp_Object ignore)
14806 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14807 return Qnil;
14810 static Lisp_Object
14811 redisplay_window_0 (Lisp_Object window)
14813 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14814 redisplay_window (window, false);
14815 return Qnil;
14818 static Lisp_Object
14819 redisplay_window_1 (Lisp_Object window)
14821 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14822 redisplay_window (window, true);
14823 return Qnil;
14827 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14828 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14829 which positions recorded in ROW differ from current buffer
14830 positions.
14832 Return true iff cursor is on this row. */
14834 static bool
14835 set_cursor_from_row (struct window *w, struct glyph_row *row,
14836 struct glyph_matrix *matrix,
14837 ptrdiff_t delta, ptrdiff_t delta_bytes,
14838 int dy, int dvpos)
14840 struct glyph *glyph = row->glyphs[TEXT_AREA];
14841 struct glyph *end = glyph + row->used[TEXT_AREA];
14842 struct glyph *cursor = NULL;
14843 /* The last known character position in row. */
14844 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14845 int x = row->x;
14846 ptrdiff_t pt_old = PT - delta;
14847 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14848 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14849 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14850 /* A glyph beyond the edge of TEXT_AREA which we should never
14851 touch. */
14852 struct glyph *glyphs_end = end;
14853 /* True means we've found a match for cursor position, but that
14854 glyph has the avoid_cursor_p flag set. */
14855 bool match_with_avoid_cursor = false;
14856 /* True means we've seen at least one glyph that came from a
14857 display string. */
14858 bool string_seen = false;
14859 /* Largest and smallest buffer positions seen so far during scan of
14860 glyph row. */
14861 ptrdiff_t bpos_max = pos_before;
14862 ptrdiff_t bpos_min = pos_after;
14863 /* Last buffer position covered by an overlay string with an integer
14864 `cursor' property. */
14865 ptrdiff_t bpos_covered = 0;
14866 /* True means the display string on which to display the cursor
14867 comes from a text property, not from an overlay. */
14868 bool string_from_text_prop = false;
14870 /* Don't even try doing anything if called for a mode-line or
14871 header-line row, since the rest of the code isn't prepared to
14872 deal with such calamities. */
14873 eassert (!row->mode_line_p);
14874 if (row->mode_line_p)
14875 return false;
14877 /* Skip over glyphs not having an object at the start and the end of
14878 the row. These are special glyphs like truncation marks on
14879 terminal frames. */
14880 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14882 if (!row->reversed_p)
14884 while (glyph < end
14885 && NILP (glyph->object)
14886 && glyph->charpos < 0)
14888 x += glyph->pixel_width;
14889 ++glyph;
14891 while (end > glyph
14892 && NILP ((end - 1)->object)
14893 /* CHARPOS is zero for blanks and stretch glyphs
14894 inserted by extend_face_to_end_of_line. */
14895 && (end - 1)->charpos <= 0)
14896 --end;
14897 glyph_before = glyph - 1;
14898 glyph_after = end;
14900 else
14902 struct glyph *g;
14904 /* If the glyph row is reversed, we need to process it from back
14905 to front, so swap the edge pointers. */
14906 glyphs_end = end = glyph - 1;
14907 glyph += row->used[TEXT_AREA] - 1;
14909 while (glyph > end + 1
14910 && NILP (glyph->object)
14911 && glyph->charpos < 0)
14912 --glyph;
14913 if (NILP (glyph->object) && glyph->charpos < 0)
14914 --glyph;
14915 /* By default, in reversed rows we put the cursor on the
14916 rightmost (first in the reading order) glyph. */
14917 for (x = 0, g = end + 1; g < glyph; g++)
14918 x += g->pixel_width;
14919 while (end < glyph
14920 && NILP ((end + 1)->object)
14921 && (end + 1)->charpos <= 0)
14922 ++end;
14923 glyph_before = glyph + 1;
14924 glyph_after = end;
14927 else if (row->reversed_p)
14929 /* In R2L rows that don't display text, put the cursor on the
14930 rightmost glyph. Case in point: an empty last line that is
14931 part of an R2L paragraph. */
14932 cursor = end - 1;
14933 /* Avoid placing the cursor on the last glyph of the row, where
14934 on terminal frames we hold the vertical border between
14935 adjacent windows. */
14936 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14937 && !WINDOW_RIGHTMOST_P (w)
14938 && cursor == row->glyphs[LAST_AREA] - 1)
14939 cursor--;
14940 x = -1; /* will be computed below, at label compute_x */
14943 /* Step 1: Try to find the glyph whose character position
14944 corresponds to point. If that's not possible, find 2 glyphs
14945 whose character positions are the closest to point, one before
14946 point, the other after it. */
14947 if (!row->reversed_p)
14948 while (/* not marched to end of glyph row */
14949 glyph < end
14950 /* glyph was not inserted by redisplay for internal purposes */
14951 && !NILP (glyph->object))
14953 if (BUFFERP (glyph->object))
14955 ptrdiff_t dpos = glyph->charpos - pt_old;
14957 if (glyph->charpos > bpos_max)
14958 bpos_max = glyph->charpos;
14959 if (glyph->charpos < bpos_min)
14960 bpos_min = glyph->charpos;
14961 if (!glyph->avoid_cursor_p)
14963 /* If we hit point, we've found the glyph on which to
14964 display the cursor. */
14965 if (dpos == 0)
14967 match_with_avoid_cursor = false;
14968 break;
14970 /* See if we've found a better approximation to
14971 POS_BEFORE or to POS_AFTER. */
14972 if (0 > dpos && dpos > pos_before - pt_old)
14974 pos_before = glyph->charpos;
14975 glyph_before = glyph;
14977 else if (0 < dpos && dpos < pos_after - pt_old)
14979 pos_after = glyph->charpos;
14980 glyph_after = glyph;
14983 else if (dpos == 0)
14984 match_with_avoid_cursor = true;
14986 else if (STRINGP (glyph->object))
14988 Lisp_Object chprop;
14989 ptrdiff_t glyph_pos = glyph->charpos;
14991 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14992 glyph->object);
14993 if (!NILP (chprop))
14995 /* If the string came from a `display' text property,
14996 look up the buffer position of that property and
14997 use that position to update bpos_max, as if we
14998 actually saw such a position in one of the row's
14999 glyphs. This helps with supporting integer values
15000 of `cursor' property on the display string in
15001 situations where most or all of the row's buffer
15002 text is completely covered by display properties,
15003 so that no glyph with valid buffer positions is
15004 ever seen in the row. */
15005 ptrdiff_t prop_pos =
15006 string_buffer_position_lim (glyph->object, pos_before,
15007 pos_after, false);
15009 if (prop_pos >= pos_before)
15010 bpos_max = prop_pos;
15012 if (INTEGERP (chprop))
15014 bpos_covered = bpos_max + XINT (chprop);
15015 /* If the `cursor' property covers buffer positions up
15016 to and including point, we should display cursor on
15017 this glyph. Note that, if a `cursor' property on one
15018 of the string's characters has an integer value, we
15019 will break out of the loop below _before_ we get to
15020 the position match above. IOW, integer values of
15021 the `cursor' property override the "exact match for
15022 point" strategy of positioning the cursor. */
15023 /* Implementation note: bpos_max == pt_old when, e.g.,
15024 we are in an empty line, where bpos_max is set to
15025 MATRIX_ROW_START_CHARPOS, see above. */
15026 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15028 cursor = glyph;
15029 break;
15033 string_seen = true;
15035 x += glyph->pixel_width;
15036 ++glyph;
15038 else if (glyph > end) /* row is reversed */
15039 while (!NILP (glyph->object))
15041 if (BUFFERP (glyph->object))
15043 ptrdiff_t dpos = glyph->charpos - pt_old;
15045 if (glyph->charpos > bpos_max)
15046 bpos_max = glyph->charpos;
15047 if (glyph->charpos < bpos_min)
15048 bpos_min = glyph->charpos;
15049 if (!glyph->avoid_cursor_p)
15051 if (dpos == 0)
15053 match_with_avoid_cursor = false;
15054 break;
15056 if (0 > dpos && dpos > pos_before - pt_old)
15058 pos_before = glyph->charpos;
15059 glyph_before = glyph;
15061 else if (0 < dpos && dpos < pos_after - pt_old)
15063 pos_after = glyph->charpos;
15064 glyph_after = glyph;
15067 else if (dpos == 0)
15068 match_with_avoid_cursor = true;
15070 else if (STRINGP (glyph->object))
15072 Lisp_Object chprop;
15073 ptrdiff_t glyph_pos = glyph->charpos;
15075 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15076 glyph->object);
15077 if (!NILP (chprop))
15079 ptrdiff_t prop_pos =
15080 string_buffer_position_lim (glyph->object, pos_before,
15081 pos_after, false);
15083 if (prop_pos >= pos_before)
15084 bpos_max = prop_pos;
15086 if (INTEGERP (chprop))
15088 bpos_covered = bpos_max + XINT (chprop);
15089 /* If the `cursor' property covers buffer positions up
15090 to and including point, we should display cursor on
15091 this glyph. */
15092 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15094 cursor = glyph;
15095 break;
15098 string_seen = true;
15100 --glyph;
15101 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15103 x--; /* can't use any pixel_width */
15104 break;
15106 x -= glyph->pixel_width;
15109 /* Step 2: If we didn't find an exact match for point, we need to
15110 look for a proper place to put the cursor among glyphs between
15111 GLYPH_BEFORE and GLYPH_AFTER. */
15112 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15113 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15114 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15116 /* An empty line has a single glyph whose OBJECT is nil and
15117 whose CHARPOS is the position of a newline on that line.
15118 Note that on a TTY, there are more glyphs after that, which
15119 were produced by extend_face_to_end_of_line, but their
15120 CHARPOS is zero or negative. */
15121 bool empty_line_p =
15122 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15123 && NILP (glyph->object) && glyph->charpos > 0
15124 /* On a TTY, continued and truncated rows also have a glyph at
15125 their end whose OBJECT is nil and whose CHARPOS is
15126 positive (the continuation and truncation glyphs), but such
15127 rows are obviously not "empty". */
15128 && !(row->continued_p || row->truncated_on_right_p));
15130 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15132 ptrdiff_t ellipsis_pos;
15134 /* Scan back over the ellipsis glyphs. */
15135 if (!row->reversed_p)
15137 ellipsis_pos = (glyph - 1)->charpos;
15138 while (glyph > row->glyphs[TEXT_AREA]
15139 && (glyph - 1)->charpos == ellipsis_pos)
15140 glyph--, x -= glyph->pixel_width;
15141 /* That loop always goes one position too far, including
15142 the glyph before the ellipsis. So scan forward over
15143 that one. */
15144 x += glyph->pixel_width;
15145 glyph++;
15147 else /* row is reversed */
15149 ellipsis_pos = (glyph + 1)->charpos;
15150 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15151 && (glyph + 1)->charpos == ellipsis_pos)
15152 glyph++, x += glyph->pixel_width;
15153 x -= glyph->pixel_width;
15154 glyph--;
15157 else if (match_with_avoid_cursor)
15159 cursor = glyph_after;
15160 x = -1;
15162 else if (string_seen)
15164 int incr = row->reversed_p ? -1 : +1;
15166 /* Need to find the glyph that came out of a string which is
15167 present at point. That glyph is somewhere between
15168 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15169 positioned between POS_BEFORE and POS_AFTER in the
15170 buffer. */
15171 struct glyph *start, *stop;
15172 ptrdiff_t pos = pos_before;
15174 x = -1;
15176 /* If the row ends in a newline from a display string,
15177 reordering could have moved the glyphs belonging to the
15178 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15179 in this case we extend the search to the last glyph in
15180 the row that was not inserted by redisplay. */
15181 if (row->ends_in_newline_from_string_p)
15183 glyph_after = end;
15184 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15187 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15188 correspond to POS_BEFORE and POS_AFTER, respectively. We
15189 need START and STOP in the order that corresponds to the
15190 row's direction as given by its reversed_p flag. If the
15191 directionality of characters between POS_BEFORE and
15192 POS_AFTER is the opposite of the row's base direction,
15193 these characters will have been reordered for display,
15194 and we need to reverse START and STOP. */
15195 if (!row->reversed_p)
15197 start = min (glyph_before, glyph_after);
15198 stop = max (glyph_before, glyph_after);
15200 else
15202 start = max (glyph_before, glyph_after);
15203 stop = min (glyph_before, glyph_after);
15205 for (glyph = start + incr;
15206 row->reversed_p ? glyph > stop : glyph < stop; )
15209 /* Any glyphs that come from the buffer are here because
15210 of bidi reordering. Skip them, and only pay
15211 attention to glyphs that came from some string. */
15212 if (STRINGP (glyph->object))
15214 Lisp_Object str;
15215 ptrdiff_t tem;
15216 /* If the display property covers the newline, we
15217 need to search for it one position farther. */
15218 ptrdiff_t lim = pos_after
15219 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15221 string_from_text_prop = false;
15222 str = glyph->object;
15223 tem = string_buffer_position_lim (str, pos, lim, false);
15224 if (tem == 0 /* from overlay */
15225 || pos <= tem)
15227 /* If the string from which this glyph came is
15228 found in the buffer at point, or at position
15229 that is closer to point than pos_after, then
15230 we've found the glyph we've been looking for.
15231 If it comes from an overlay (tem == 0), and
15232 it has the `cursor' property on one of its
15233 glyphs, record that glyph as a candidate for
15234 displaying the cursor. (As in the
15235 unidirectional version, we will display the
15236 cursor on the last candidate we find.) */
15237 if (tem == 0
15238 || tem == pt_old
15239 || (tem - pt_old > 0 && tem < pos_after))
15241 /* The glyphs from this string could have
15242 been reordered. Find the one with the
15243 smallest string position. Or there could
15244 be a character in the string with the
15245 `cursor' property, which means display
15246 cursor on that character's glyph. */
15247 ptrdiff_t strpos = glyph->charpos;
15249 if (tem)
15251 cursor = glyph;
15252 string_from_text_prop = true;
15254 for ( ;
15255 (row->reversed_p ? glyph > stop : glyph < stop)
15256 && EQ (glyph->object, str);
15257 glyph += incr)
15259 Lisp_Object cprop;
15260 ptrdiff_t gpos = glyph->charpos;
15262 cprop = Fget_char_property (make_number (gpos),
15263 Qcursor,
15264 glyph->object);
15265 if (!NILP (cprop))
15267 cursor = glyph;
15268 break;
15270 if (tem && glyph->charpos < strpos)
15272 strpos = glyph->charpos;
15273 cursor = glyph;
15277 if (tem == pt_old
15278 || (tem - pt_old > 0 && tem < pos_after))
15279 goto compute_x;
15281 if (tem)
15282 pos = tem + 1; /* don't find previous instances */
15284 /* This string is not what we want; skip all of the
15285 glyphs that came from it. */
15286 while ((row->reversed_p ? glyph > stop : glyph < stop)
15287 && EQ (glyph->object, str))
15288 glyph += incr;
15290 else
15291 glyph += incr;
15294 /* If we reached the end of the line, and END was from a string,
15295 the cursor is not on this line. */
15296 if (cursor == NULL
15297 && (row->reversed_p ? glyph <= end : glyph >= end)
15298 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15299 && STRINGP (end->object)
15300 && row->continued_p)
15301 return false;
15303 /* A truncated row may not include PT among its character positions.
15304 Setting the cursor inside the scroll margin will trigger
15305 recalculation of hscroll in hscroll_window_tree. But if a
15306 display string covers point, defer to the string-handling
15307 code below to figure this out. */
15308 else if (row->truncated_on_left_p && pt_old < bpos_min)
15310 cursor = glyph_before;
15311 x = -1;
15313 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15314 /* Zero-width characters produce no glyphs. */
15315 || (!empty_line_p
15316 && (row->reversed_p
15317 ? glyph_after > glyphs_end
15318 : glyph_after < glyphs_end)))
15320 cursor = glyph_after;
15321 x = -1;
15325 compute_x:
15326 if (cursor != NULL)
15327 glyph = cursor;
15328 else if (glyph == glyphs_end
15329 && pos_before == pos_after
15330 && STRINGP ((row->reversed_p
15331 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15332 : row->glyphs[TEXT_AREA])->object))
15334 /* If all the glyphs of this row came from strings, put the
15335 cursor on the first glyph of the row. This avoids having the
15336 cursor outside of the text area in this very rare and hard
15337 use case. */
15338 glyph =
15339 row->reversed_p
15340 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15341 : row->glyphs[TEXT_AREA];
15343 if (x < 0)
15345 struct glyph *g;
15347 /* Need to compute x that corresponds to GLYPH. */
15348 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15350 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15351 emacs_abort ();
15352 x += g->pixel_width;
15356 /* ROW could be part of a continued line, which, under bidi
15357 reordering, might have other rows whose start and end charpos
15358 occlude point. Only set w->cursor if we found a better
15359 approximation to the cursor position than we have from previously
15360 examined candidate rows belonging to the same continued line. */
15361 if (/* We already have a candidate row. */
15362 w->cursor.vpos >= 0
15363 /* That candidate is not the row we are processing. */
15364 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15365 /* Make sure cursor.vpos specifies a row whose start and end
15366 charpos occlude point, and it is valid candidate for being a
15367 cursor-row. This is because some callers of this function
15368 leave cursor.vpos at the row where the cursor was displayed
15369 during the last redisplay cycle. */
15370 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15371 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15372 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15374 struct glyph *g1
15375 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15377 /* Don't consider glyphs that are outside TEXT_AREA. */
15378 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15379 return false;
15380 /* Keep the candidate whose buffer position is the closest to
15381 point or has the `cursor' property. */
15382 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15383 w->cursor.hpos >= 0
15384 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15385 && ((BUFFERP (g1->object)
15386 && (g1->charpos == pt_old /* An exact match always wins. */
15387 || (BUFFERP (glyph->object)
15388 && eabs (g1->charpos - pt_old)
15389 < eabs (glyph->charpos - pt_old))))
15390 /* Previous candidate is a glyph from a string that has
15391 a non-nil `cursor' property. */
15392 || (STRINGP (g1->object)
15393 && (!NILP (Fget_char_property (make_number (g1->charpos),
15394 Qcursor, g1->object))
15395 /* Previous candidate is from the same display
15396 string as this one, and the display string
15397 came from a text property. */
15398 || (EQ (g1->object, glyph->object)
15399 && string_from_text_prop)
15400 /* this candidate is from newline and its
15401 position is not an exact match */
15402 || (NILP (glyph->object)
15403 && glyph->charpos != pt_old)))))
15404 return false;
15405 /* If this candidate gives an exact match, use that. */
15406 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15407 /* If this candidate is a glyph created for the
15408 terminating newline of a line, and point is on that
15409 newline, it wins because it's an exact match. */
15410 || (!row->continued_p
15411 && NILP (glyph->object)
15412 && glyph->charpos == 0
15413 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15414 /* Otherwise, keep the candidate that comes from a row
15415 spanning less buffer positions. This may win when one or
15416 both candidate positions are on glyphs that came from
15417 display strings, for which we cannot compare buffer
15418 positions. */
15419 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15420 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15421 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15422 return false;
15424 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15425 w->cursor.x = x;
15426 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15427 w->cursor.y = row->y + dy;
15429 if (w == XWINDOW (selected_window))
15431 if (!row->continued_p
15432 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15433 && row->x == 0)
15435 this_line_buffer = XBUFFER (w->contents);
15437 CHARPOS (this_line_start_pos)
15438 = MATRIX_ROW_START_CHARPOS (row) + delta;
15439 BYTEPOS (this_line_start_pos)
15440 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15442 CHARPOS (this_line_end_pos)
15443 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15444 BYTEPOS (this_line_end_pos)
15445 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15447 this_line_y = w->cursor.y;
15448 this_line_pixel_height = row->height;
15449 this_line_vpos = w->cursor.vpos;
15450 this_line_start_x = row->x;
15452 else
15453 CHARPOS (this_line_start_pos) = 0;
15456 return true;
15460 /* Run window scroll functions, if any, for WINDOW with new window
15461 start STARTP. Sets the window start of WINDOW to that position.
15463 We assume that the window's buffer is really current. */
15465 static struct text_pos
15466 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15468 struct window *w = XWINDOW (window);
15469 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15471 eassert (current_buffer == XBUFFER (w->contents));
15473 if (!NILP (Vwindow_scroll_functions))
15475 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15476 make_number (CHARPOS (startp)));
15477 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15478 /* In case the hook functions switch buffers. */
15479 set_buffer_internal (XBUFFER (w->contents));
15482 return startp;
15486 /* Make sure the line containing the cursor is fully visible.
15487 A value of true means there is nothing to be done.
15488 (Either the line is fully visible, or it cannot be made so,
15489 or we cannot tell.)
15491 If FORCE_P, return false even if partial visible cursor row
15492 is higher than window.
15494 If CURRENT_MATRIX_P, use the information from the
15495 window's current glyph matrix; otherwise use the desired glyph
15496 matrix.
15498 A value of false means the caller should do scrolling
15499 as if point had gone off the screen. */
15501 static bool
15502 cursor_row_fully_visible_p (struct window *w, bool force_p,
15503 bool current_matrix_p)
15505 struct glyph_matrix *matrix;
15506 struct glyph_row *row;
15507 int window_height;
15509 if (!make_cursor_line_fully_visible_p)
15510 return true;
15512 /* It's not always possible to find the cursor, e.g, when a window
15513 is full of overlay strings. Don't do anything in that case. */
15514 if (w->cursor.vpos < 0)
15515 return true;
15517 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15518 row = MATRIX_ROW (matrix, w->cursor.vpos);
15520 /* If the cursor row is not partially visible, there's nothing to do. */
15521 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15522 return true;
15524 /* If the row the cursor is in is taller than the window's height,
15525 it's not clear what to do, so do nothing. */
15526 window_height = window_box_height (w);
15527 if (row->height >= window_height)
15529 if (!force_p || MINI_WINDOW_P (w)
15530 || w->vscroll || w->cursor.vpos == 0)
15531 return true;
15533 return false;
15537 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15538 means only WINDOW is redisplayed in redisplay_internal.
15539 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15540 in redisplay_window to bring a partially visible line into view in
15541 the case that only the cursor has moved.
15543 LAST_LINE_MISFIT should be true if we're scrolling because the
15544 last screen line's vertical height extends past the end of the screen.
15546 Value is
15548 1 if scrolling succeeded
15550 0 if scrolling didn't find point.
15552 -1 if new fonts have been loaded so that we must interrupt
15553 redisplay, adjust glyph matrices, and try again. */
15555 enum
15557 SCROLLING_SUCCESS,
15558 SCROLLING_FAILED,
15559 SCROLLING_NEED_LARGER_MATRICES
15562 /* If scroll-conservatively is more than this, never recenter.
15564 If you change this, don't forget to update the doc string of
15565 `scroll-conservatively' and the Emacs manual. */
15566 #define SCROLL_LIMIT 100
15568 static int
15569 try_scrolling (Lisp_Object window, bool just_this_one_p,
15570 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15571 bool temp_scroll_step, bool last_line_misfit)
15573 struct window *w = XWINDOW (window);
15574 struct text_pos pos, startp;
15575 struct it it;
15576 int this_scroll_margin, scroll_max, rc, height;
15577 int dy = 0, amount_to_scroll = 0;
15578 bool scroll_down_p = false;
15579 int extra_scroll_margin_lines = last_line_misfit;
15580 Lisp_Object aggressive;
15581 /* We will never try scrolling more than this number of lines. */
15582 int scroll_limit = SCROLL_LIMIT;
15583 int frame_line_height = default_line_pixel_height (w);
15585 #ifdef GLYPH_DEBUG
15586 debug_method_add (w, "try_scrolling");
15587 #endif
15589 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15591 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15593 /* Force arg_scroll_conservatively to have a reasonable value, to
15594 avoid scrolling too far away with slow move_it_* functions. Note
15595 that the user can supply scroll-conservatively equal to
15596 `most-positive-fixnum', which can be larger than INT_MAX. */
15597 if (arg_scroll_conservatively > scroll_limit)
15599 arg_scroll_conservatively = scroll_limit + 1;
15600 scroll_max = scroll_limit * frame_line_height;
15602 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15603 /* Compute how much we should try to scroll maximally to bring
15604 point into view. */
15605 scroll_max = (max (scroll_step,
15606 max (arg_scroll_conservatively, temp_scroll_step))
15607 * frame_line_height);
15608 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15609 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15610 /* We're trying to scroll because of aggressive scrolling but no
15611 scroll_step is set. Choose an arbitrary one. */
15612 scroll_max = 10 * frame_line_height;
15613 else
15614 scroll_max = 0;
15616 too_near_end:
15618 /* Decide whether to scroll down. */
15619 if (PT > CHARPOS (startp))
15621 int scroll_margin_y;
15623 /* Compute the pixel ypos of the scroll margin, then move IT to
15624 either that ypos or PT, whichever comes first. */
15625 start_display (&it, w, startp);
15626 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15627 - this_scroll_margin
15628 - frame_line_height * extra_scroll_margin_lines;
15629 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15630 (MOVE_TO_POS | MOVE_TO_Y));
15632 if (PT > CHARPOS (it.current.pos))
15634 int y0 = line_bottom_y (&it);
15635 /* Compute how many pixels below window bottom to stop searching
15636 for PT. This avoids costly search for PT that is far away if
15637 the user limited scrolling by a small number of lines, but
15638 always finds PT if scroll_conservatively is set to a large
15639 number, such as most-positive-fixnum. */
15640 int slack = max (scroll_max, 10 * frame_line_height);
15641 int y_to_move = it.last_visible_y + slack;
15643 /* Compute the distance from the scroll margin to PT or to
15644 the scroll limit, whichever comes first. This should
15645 include the height of the cursor line, to make that line
15646 fully visible. */
15647 move_it_to (&it, PT, -1, y_to_move,
15648 -1, MOVE_TO_POS | MOVE_TO_Y);
15649 dy = line_bottom_y (&it) - y0;
15651 if (dy > scroll_max)
15652 return SCROLLING_FAILED;
15654 if (dy > 0)
15655 scroll_down_p = true;
15657 else if (PT == IT_CHARPOS (it)
15658 && IT_CHARPOS (it) < ZV
15659 && it.method == GET_FROM_STRING
15660 && arg_scroll_conservatively > scroll_limit
15661 && it.current_x == 0)
15663 enum move_it_result skip;
15664 int y1 = it.current_y;
15665 int vpos;
15667 /* A before-string that includes newlines and is displayed
15668 on the last visible screen line could fail us under
15669 scroll-conservatively > 100, because we will be unable to
15670 position the cursor on that last visible line. Try to
15671 recover by finding the first screen line that has some
15672 glyphs coming from the buffer text. */
15673 do {
15674 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15675 if (skip != MOVE_NEWLINE_OR_CR
15676 || IT_CHARPOS (it) != PT
15677 || it.method == GET_FROM_BUFFER)
15678 break;
15679 vpos = it.vpos;
15680 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15681 } while (it.vpos > vpos);
15683 dy = it.current_y - y1;
15685 if (dy > scroll_max)
15686 return SCROLLING_FAILED;
15688 if (dy > 0)
15689 scroll_down_p = true;
15693 if (scroll_down_p)
15695 /* Point is in or below the bottom scroll margin, so move the
15696 window start down. If scrolling conservatively, move it just
15697 enough down to make point visible. If scroll_step is set,
15698 move it down by scroll_step. */
15699 if (arg_scroll_conservatively)
15700 amount_to_scroll
15701 = min (max (dy, frame_line_height),
15702 frame_line_height * arg_scroll_conservatively);
15703 else if (scroll_step || temp_scroll_step)
15704 amount_to_scroll = scroll_max;
15705 else
15707 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15708 height = WINDOW_BOX_TEXT_HEIGHT (w);
15709 if (NUMBERP (aggressive))
15711 double float_amount = XFLOATINT (aggressive) * height;
15712 int aggressive_scroll = float_amount;
15713 if (aggressive_scroll == 0 && float_amount > 0)
15714 aggressive_scroll = 1;
15715 /* Don't let point enter the scroll margin near top of
15716 the window. This could happen if the value of
15717 scroll_up_aggressively is too large and there are
15718 non-zero margins, because scroll_up_aggressively
15719 means put point that fraction of window height
15720 _from_the_bottom_margin_. */
15721 if (aggressive_scroll + 2 * this_scroll_margin > height)
15722 aggressive_scroll = height - 2 * this_scroll_margin;
15723 amount_to_scroll = dy + aggressive_scroll;
15727 if (amount_to_scroll <= 0)
15728 return SCROLLING_FAILED;
15730 start_display (&it, w, startp);
15731 if (arg_scroll_conservatively <= scroll_limit)
15732 move_it_vertically (&it, amount_to_scroll);
15733 else
15735 /* Extra precision for users who set scroll-conservatively
15736 to a large number: make sure the amount we scroll
15737 the window start is never less than amount_to_scroll,
15738 which was computed as distance from window bottom to
15739 point. This matters when lines at window top and lines
15740 below window bottom have different height. */
15741 struct it it1;
15742 void *it1data = NULL;
15743 /* We use a temporary it1 because line_bottom_y can modify
15744 its argument, if it moves one line down; see there. */
15745 int start_y;
15747 SAVE_IT (it1, it, it1data);
15748 start_y = line_bottom_y (&it1);
15749 do {
15750 RESTORE_IT (&it, &it, it1data);
15751 move_it_by_lines (&it, 1);
15752 SAVE_IT (it1, it, it1data);
15753 } while (IT_CHARPOS (it) < ZV
15754 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15755 bidi_unshelve_cache (it1data, true);
15758 /* If STARTP is unchanged, move it down another screen line. */
15759 if (IT_CHARPOS (it) == CHARPOS (startp))
15760 move_it_by_lines (&it, 1);
15761 startp = it.current.pos;
15763 else
15765 struct text_pos scroll_margin_pos = startp;
15766 int y_offset = 0;
15768 /* See if point is inside the scroll margin at the top of the
15769 window. */
15770 if (this_scroll_margin)
15772 int y_start;
15774 start_display (&it, w, startp);
15775 y_start = it.current_y;
15776 move_it_vertically (&it, this_scroll_margin);
15777 scroll_margin_pos = it.current.pos;
15778 /* If we didn't move enough before hitting ZV, request
15779 additional amount of scroll, to move point out of the
15780 scroll margin. */
15781 if (IT_CHARPOS (it) == ZV
15782 && it.current_y - y_start < this_scroll_margin)
15783 y_offset = this_scroll_margin - (it.current_y - y_start);
15786 if (PT < CHARPOS (scroll_margin_pos))
15788 /* Point is in the scroll margin at the top of the window or
15789 above what is displayed in the window. */
15790 int y0, y_to_move;
15792 /* Compute the vertical distance from PT to the scroll
15793 margin position. Move as far as scroll_max allows, or
15794 one screenful, or 10 screen lines, whichever is largest.
15795 Give up if distance is greater than scroll_max or if we
15796 didn't reach the scroll margin position. */
15797 SET_TEXT_POS (pos, PT, PT_BYTE);
15798 start_display (&it, w, pos);
15799 y0 = it.current_y;
15800 y_to_move = max (it.last_visible_y,
15801 max (scroll_max, 10 * frame_line_height));
15802 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15803 y_to_move, -1,
15804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15805 dy = it.current_y - y0;
15806 if (dy > scroll_max
15807 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15808 return SCROLLING_FAILED;
15810 /* Additional scroll for when ZV was too close to point. */
15811 dy += y_offset;
15813 /* Compute new window start. */
15814 start_display (&it, w, startp);
15816 if (arg_scroll_conservatively)
15817 amount_to_scroll = max (dy, frame_line_height
15818 * max (scroll_step, temp_scroll_step));
15819 else if (scroll_step || temp_scroll_step)
15820 amount_to_scroll = scroll_max;
15821 else
15823 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15824 height = WINDOW_BOX_TEXT_HEIGHT (w);
15825 if (NUMBERP (aggressive))
15827 double float_amount = XFLOATINT (aggressive) * height;
15828 int aggressive_scroll = float_amount;
15829 if (aggressive_scroll == 0 && float_amount > 0)
15830 aggressive_scroll = 1;
15831 /* Don't let point enter the scroll margin near
15832 bottom of the window, if the value of
15833 scroll_down_aggressively happens to be too
15834 large. */
15835 if (aggressive_scroll + 2 * this_scroll_margin > height)
15836 aggressive_scroll = height - 2 * this_scroll_margin;
15837 amount_to_scroll = dy + aggressive_scroll;
15841 if (amount_to_scroll <= 0)
15842 return SCROLLING_FAILED;
15844 move_it_vertically_backward (&it, amount_to_scroll);
15845 startp = it.current.pos;
15849 /* Run window scroll functions. */
15850 startp = run_window_scroll_functions (window, startp);
15852 /* Display the window. Give up if new fonts are loaded, or if point
15853 doesn't appear. */
15854 if (!try_window (window, startp, 0))
15855 rc = SCROLLING_NEED_LARGER_MATRICES;
15856 else if (w->cursor.vpos < 0)
15858 clear_glyph_matrix (w->desired_matrix);
15859 rc = SCROLLING_FAILED;
15861 else
15863 /* Maybe forget recorded base line for line number display. */
15864 if (!just_this_one_p
15865 || current_buffer->clip_changed
15866 || BEG_UNCHANGED < CHARPOS (startp))
15867 w->base_line_number = 0;
15869 /* If cursor ends up on a partially visible line,
15870 treat that as being off the bottom of the screen. */
15871 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15872 false)
15873 /* It's possible that the cursor is on the first line of the
15874 buffer, which is partially obscured due to a vscroll
15875 (Bug#7537). In that case, avoid looping forever. */
15876 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15878 clear_glyph_matrix (w->desired_matrix);
15879 ++extra_scroll_margin_lines;
15880 goto too_near_end;
15882 rc = SCROLLING_SUCCESS;
15885 return rc;
15889 /* Compute a suitable window start for window W if display of W starts
15890 on a continuation line. Value is true if a new window start
15891 was computed.
15893 The new window start will be computed, based on W's width, starting
15894 from the start of the continued line. It is the start of the
15895 screen line with the minimum distance from the old start W->start,
15896 which is still before point (otherwise point will definitely not
15897 be visible in the window). */
15899 static bool
15900 compute_window_start_on_continuation_line (struct window *w)
15902 struct text_pos pos, start_pos, pos_before_pt;
15903 bool window_start_changed_p = false;
15905 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15907 /* If window start is on a continuation line... Window start may be
15908 < BEGV in case there's invisible text at the start of the
15909 buffer (M-x rmail, for example). */
15910 if (CHARPOS (start_pos) > BEGV
15911 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15913 struct it it;
15914 struct glyph_row *row;
15916 /* Handle the case that the window start is out of range. */
15917 if (CHARPOS (start_pos) < BEGV)
15918 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15919 else if (CHARPOS (start_pos) > ZV)
15920 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15922 /* Find the start of the continued line. This should be fast
15923 because find_newline is fast (newline cache). */
15924 row = w->desired_matrix->rows + window_wants_header_line (w);
15925 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15926 row, DEFAULT_FACE_ID);
15927 reseat_at_previous_visible_line_start (&it);
15929 /* If the line start is "too far" away from the window start,
15930 say it takes too much time to compute a new window start.
15931 Also, give up if the line start is after point, as in that
15932 case point will not be visible with any window start we
15933 compute. */
15934 if (IT_CHARPOS (it) <= PT
15935 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15936 /* PXW: Do we need upper bounds here? */
15937 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15939 int min_distance, distance;
15941 /* Move forward by display lines to find the new window
15942 start. If window width was enlarged, the new start can
15943 be expected to be > the old start. If window width was
15944 decreased, the new window start will be < the old start.
15945 So, we're looking for the display line start with the
15946 minimum distance from the old window start. */
15947 pos_before_pt = pos = it.current.pos;
15948 min_distance = DISP_INFINITY;
15949 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15950 distance < min_distance)
15952 min_distance = distance;
15953 if (CHARPOS (pos) <= PT)
15954 pos_before_pt = pos;
15955 pos = it.current.pos;
15956 if (it.line_wrap == WORD_WRAP)
15958 /* Under WORD_WRAP, move_it_by_lines is likely to
15959 overshoot and stop not at the first, but the
15960 second character from the left margin. So in
15961 that case, we need a more tight control on the X
15962 coordinate of the iterator than move_it_by_lines
15963 promises in its contract. The method is to first
15964 go to the last (rightmost) visible character of a
15965 line, then move to the leftmost character on the
15966 next line in a separate call. */
15967 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15968 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15969 move_it_to (&it, ZV, 0,
15970 it.current_y + it.max_ascent + it.max_descent, -1,
15971 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15973 else
15974 move_it_by_lines (&it, 1);
15977 /* It makes very little sense to make the new window start
15978 after point, as point won't be visible. If that's what
15979 the loop above finds, fall back on the candidate before
15980 or at point that is closest to the old window start. */
15981 if (CHARPOS (pos) > PT)
15982 pos = pos_before_pt;
15984 /* Set the window start there. */
15985 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15986 window_start_changed_p = true;
15990 return window_start_changed_p;
15994 /* Try cursor movement in case text has not changed in window WINDOW,
15995 with window start STARTP. Value is
15997 CURSOR_MOVEMENT_SUCCESS if successful
15999 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16001 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16002 display. *SCROLL_STEP is set to true, under certain circumstances, if
16003 we want to scroll as if scroll-step were set to 1. See the code.
16005 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16006 which case we have to abort this redisplay, and adjust matrices
16007 first. */
16009 enum
16011 CURSOR_MOVEMENT_SUCCESS,
16012 CURSOR_MOVEMENT_CANNOT_BE_USED,
16013 CURSOR_MOVEMENT_MUST_SCROLL,
16014 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16017 static int
16018 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16019 bool *scroll_step)
16021 struct window *w = XWINDOW (window);
16022 struct frame *f = XFRAME (w->frame);
16023 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16025 #ifdef GLYPH_DEBUG
16026 if (inhibit_try_cursor_movement)
16027 return rc;
16028 #endif
16030 /* Previously, there was a check for Lisp integer in the
16031 if-statement below. Now, this field is converted to
16032 ptrdiff_t, thus zero means invalid position in a buffer. */
16033 eassert (w->last_point > 0);
16034 /* Likewise there was a check whether window_end_vpos is nil or larger
16035 than the window. Now window_end_vpos is int and so never nil, but
16036 let's leave eassert to check whether it fits in the window. */
16037 eassert (!w->window_end_valid
16038 || w->window_end_vpos < w->current_matrix->nrows);
16040 /* Handle case where text has not changed, only point, and it has
16041 not moved off the frame. */
16042 if (/* Point may be in this window. */
16043 PT >= CHARPOS (startp)
16044 /* Selective display hasn't changed. */
16045 && !current_buffer->clip_changed
16046 /* Function force-mode-line-update is used to force a thorough
16047 redisplay. It sets either windows_or_buffers_changed or
16048 update_mode_lines. So don't take a shortcut here for these
16049 cases. */
16050 && !update_mode_lines
16051 && !windows_or_buffers_changed
16052 && !f->cursor_type_changed
16053 && NILP (Vshow_trailing_whitespace)
16054 /* When display-line-numbers is in relative mode, moving point
16055 requires to redraw the entire window. */
16056 && !EQ (Vdisplay_line_numbers, Qrelative)
16057 && !EQ (Vdisplay_line_numbers, Qvisual)
16058 /* When the current line number should be displayed in a
16059 distinct face, moving point cannot be handled in optimized
16060 way as below. */
16061 && !(!NILP (Vdisplay_line_numbers)
16062 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16063 Qline_number_current_line,
16064 w->frame)))
16065 /* This code is not used for mini-buffer for the sake of the case
16066 of redisplaying to replace an echo area message; since in
16067 that case the mini-buffer contents per se are usually
16068 unchanged. This code is of no real use in the mini-buffer
16069 since the handling of this_line_start_pos, etc., in redisplay
16070 handles the same cases. */
16071 && !EQ (window, minibuf_window)
16072 /* When overlay arrow is shown in current buffer, point movement
16073 is no longer "simple", as it typically causes the overlay
16074 arrow to move as well. */
16075 && !overlay_arrow_in_current_buffer_p ())
16077 int this_scroll_margin, top_scroll_margin;
16078 struct glyph_row *row = NULL;
16080 #ifdef GLYPH_DEBUG
16081 debug_method_add (w, "cursor movement");
16082 #endif
16084 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16086 top_scroll_margin = this_scroll_margin;
16087 if (window_wants_header_line (w))
16088 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16090 /* Start with the row the cursor was displayed during the last
16091 not paused redisplay. Give up if that row is not valid. */
16092 if (w->last_cursor_vpos < 0
16093 || w->last_cursor_vpos >= w->current_matrix->nrows)
16094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16095 else
16097 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16098 if (row->mode_line_p)
16099 ++row;
16100 if (!row->enabled_p)
16101 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16104 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16106 bool scroll_p = false, must_scroll = false;
16107 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16109 if (PT > w->last_point)
16111 /* Point has moved forward. */
16112 while (MATRIX_ROW_END_CHARPOS (row) < PT
16113 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16115 eassert (row->enabled_p);
16116 ++row;
16119 /* If the end position of a row equals the start
16120 position of the next row, and PT is at that position,
16121 we would rather display cursor in the next line. */
16122 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16123 && MATRIX_ROW_END_CHARPOS (row) == PT
16124 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16125 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16126 && !cursor_row_p (row))
16127 ++row;
16129 /* If within the scroll margin, scroll. Note that
16130 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16131 the next line would be drawn, and that
16132 this_scroll_margin can be zero. */
16133 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16134 || PT > MATRIX_ROW_END_CHARPOS (row)
16135 /* Line is completely visible last line in window
16136 and PT is to be set in the next line. */
16137 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16138 && PT == MATRIX_ROW_END_CHARPOS (row)
16139 && !row->ends_at_zv_p
16140 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16141 scroll_p = true;
16143 else if (PT < w->last_point)
16145 /* Cursor has to be moved backward. Note that PT >=
16146 CHARPOS (startp) because of the outer if-statement. */
16147 while (!row->mode_line_p
16148 && (MATRIX_ROW_START_CHARPOS (row) > PT
16149 || (MATRIX_ROW_START_CHARPOS (row) == PT
16150 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16151 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16152 row > w->current_matrix->rows
16153 && (row-1)->ends_in_newline_from_string_p))))
16154 && (row->y > top_scroll_margin
16155 || CHARPOS (startp) == BEGV))
16157 eassert (row->enabled_p);
16158 --row;
16161 /* Consider the following case: Window starts at BEGV,
16162 there is invisible, intangible text at BEGV, so that
16163 display starts at some point START > BEGV. It can
16164 happen that we are called with PT somewhere between
16165 BEGV and START. Try to handle that case. */
16166 if (row < w->current_matrix->rows
16167 || row->mode_line_p)
16169 row = w->current_matrix->rows;
16170 if (row->mode_line_p)
16171 ++row;
16174 /* Due to newlines in overlay strings, we may have to
16175 skip forward over overlay strings. */
16176 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16177 && MATRIX_ROW_END_CHARPOS (row) == PT
16178 && !cursor_row_p (row))
16179 ++row;
16181 /* If within the scroll margin, scroll. */
16182 if (row->y < top_scroll_margin
16183 && CHARPOS (startp) != BEGV)
16184 scroll_p = true;
16186 else
16188 /* Cursor did not move. So don't scroll even if cursor line
16189 is partially visible, as it was so before. */
16190 rc = CURSOR_MOVEMENT_SUCCESS;
16193 if (PT < MATRIX_ROW_START_CHARPOS (row)
16194 || PT > MATRIX_ROW_END_CHARPOS (row))
16196 /* if PT is not in the glyph row, give up. */
16197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16198 must_scroll = true;
16200 else if (rc != CURSOR_MOVEMENT_SUCCESS
16201 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16203 struct glyph_row *row1;
16205 /* If rows are bidi-reordered and point moved, back up
16206 until we find a row that does not belong to a
16207 continuation line. This is because we must consider
16208 all rows of a continued line as candidates for the
16209 new cursor positioning, since row start and end
16210 positions change non-linearly with vertical position
16211 in such rows. */
16212 /* FIXME: Revisit this when glyph ``spilling'' in
16213 continuation lines' rows is implemented for
16214 bidi-reordered rows. */
16215 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16216 MATRIX_ROW_CONTINUATION_LINE_P (row);
16217 --row)
16219 /* If we hit the beginning of the displayed portion
16220 without finding the first row of a continued
16221 line, give up. */
16222 if (row <= row1)
16224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16225 break;
16227 eassert (row->enabled_p);
16230 if (must_scroll)
16232 else if (rc != CURSOR_MOVEMENT_SUCCESS
16233 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16234 /* Make sure this isn't a header line by any chance, since
16235 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16236 && !row->mode_line_p
16237 && make_cursor_line_fully_visible_p)
16239 if (PT == MATRIX_ROW_END_CHARPOS (row)
16240 && !row->ends_at_zv_p
16241 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16242 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16243 else if (row->height > window_box_height (w))
16245 /* If we end up in a partially visible line, let's
16246 make it fully visible, except when it's taller
16247 than the window, in which case we can't do much
16248 about it. */
16249 *scroll_step = true;
16250 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16252 else
16254 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16255 if (!cursor_row_fully_visible_p (w, false, true))
16256 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16257 else
16258 rc = CURSOR_MOVEMENT_SUCCESS;
16261 else if (scroll_p)
16262 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16263 else if (rc != CURSOR_MOVEMENT_SUCCESS
16264 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16266 /* With bidi-reordered rows, there could be more than
16267 one candidate row whose start and end positions
16268 occlude point. We need to let set_cursor_from_row
16269 find the best candidate. */
16270 /* FIXME: Revisit this when glyph ``spilling'' in
16271 continuation lines' rows is implemented for
16272 bidi-reordered rows. */
16273 bool rv = false;
16277 bool at_zv_p = false, exact_match_p = false;
16279 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16280 && PT <= MATRIX_ROW_END_CHARPOS (row)
16281 && cursor_row_p (row))
16282 rv |= set_cursor_from_row (w, row, w->current_matrix,
16283 0, 0, 0, 0);
16284 /* As soon as we've found the exact match for point,
16285 or the first suitable row whose ends_at_zv_p flag
16286 is set, we are done. */
16287 if (rv)
16289 at_zv_p = MATRIX_ROW (w->current_matrix,
16290 w->cursor.vpos)->ends_at_zv_p;
16291 if (!at_zv_p
16292 && w->cursor.hpos >= 0
16293 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16294 w->cursor.vpos))
16296 struct glyph_row *candidate =
16297 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16298 struct glyph *g =
16299 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16300 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16302 exact_match_p =
16303 (BUFFERP (g->object) && g->charpos == PT)
16304 || (NILP (g->object)
16305 && (g->charpos == PT
16306 || (g->charpos == 0 && endpos - 1 == PT)));
16308 if (at_zv_p || exact_match_p)
16310 rc = CURSOR_MOVEMENT_SUCCESS;
16311 break;
16314 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16315 break;
16316 ++row;
16318 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16319 || row->continued_p)
16320 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16321 || (MATRIX_ROW_START_CHARPOS (row) == PT
16322 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16323 /* If we didn't find any candidate rows, or exited the
16324 loop before all the candidates were examined, signal
16325 to the caller that this method failed. */
16326 if (rc != CURSOR_MOVEMENT_SUCCESS
16327 && !(rv
16328 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16329 && !row->continued_p))
16330 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16331 else if (rv)
16332 rc = CURSOR_MOVEMENT_SUCCESS;
16334 else
16338 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16340 rc = CURSOR_MOVEMENT_SUCCESS;
16341 break;
16343 ++row;
16345 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16346 && MATRIX_ROW_START_CHARPOS (row) == PT
16347 && cursor_row_p (row));
16352 return rc;
16356 void
16357 set_vertical_scroll_bar (struct window *w)
16359 ptrdiff_t start, end, whole;
16361 /* Calculate the start and end positions for the current window.
16362 At some point, it would be nice to choose between scrollbars
16363 which reflect the whole buffer size, with special markers
16364 indicating narrowing, and scrollbars which reflect only the
16365 visible region.
16367 Note that mini-buffers sometimes aren't displaying any text. */
16368 if (!MINI_WINDOW_P (w)
16369 || (w == XWINDOW (minibuf_window)
16370 && NILP (echo_area_buffer[0])))
16372 struct buffer *buf = XBUFFER (w->contents);
16373 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16374 start = marker_position (w->start) - BUF_BEGV (buf);
16375 /* I don't think this is guaranteed to be right. For the
16376 moment, we'll pretend it is. */
16377 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16379 if (end < start)
16380 end = start;
16381 if (whole < (end - start))
16382 whole = end - start;
16384 else
16385 start = end = whole = 0;
16387 /* Indicate what this scroll bar ought to be displaying now. */
16388 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16389 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16390 (w, end - start, whole, start);
16394 void
16395 set_horizontal_scroll_bar (struct window *w)
16397 int start, end, whole, portion;
16399 if (!MINI_WINDOW_P (w)
16400 || (w == XWINDOW (minibuf_window)
16401 && NILP (echo_area_buffer[0])))
16403 struct buffer *b = XBUFFER (w->contents);
16404 struct buffer *old_buffer = NULL;
16405 struct it it;
16406 struct text_pos startp;
16408 if (b != current_buffer)
16410 old_buffer = current_buffer;
16411 set_buffer_internal (b);
16414 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16415 start_display (&it, w, startp);
16416 it.last_visible_x = INT_MAX;
16417 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16418 MOVE_TO_X | MOVE_TO_Y);
16419 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16420 window_box_height (w), -1,
16421 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16423 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16424 end = start + window_box_width (w, TEXT_AREA);
16425 portion = end - start;
16426 /* After enlarging a horizontally scrolled window such that it
16427 gets at least as wide as the text it contains, make sure that
16428 the thumb doesn't fill the entire scroll bar so we can still
16429 drag it back to see the entire text. */
16430 whole = max (whole, end);
16432 if (it.bidi_p)
16434 Lisp_Object pdir;
16436 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16437 if (EQ (pdir, Qright_to_left))
16439 start = whole - end;
16440 end = start + portion;
16444 if (old_buffer)
16445 set_buffer_internal (old_buffer);
16447 else
16448 start = end = whole = portion = 0;
16450 w->hscroll_whole = whole;
16452 /* Indicate what this scroll bar ought to be displaying now. */
16453 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16454 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16455 (w, portion, whole, start);
16459 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16460 selected_window is redisplayed.
16462 We can return without actually redisplaying the window if fonts has been
16463 changed on window's frame. In that case, redisplay_internal will retry.
16465 As one of the important parts of redisplaying a window, we need to
16466 decide whether the previous window-start position (stored in the
16467 window's w->start marker position) is still valid, and if it isn't,
16468 recompute it. Some details about that:
16470 . The previous window-start could be in a continuation line, in
16471 which case we need to recompute it when the window width
16472 changes. See compute_window_start_on_continuation_line and its
16473 call below.
16475 . The text that changed since last redisplay could include the
16476 previous window-start position. In that case, we try to salvage
16477 what we can from the current glyph matrix by calling
16478 try_scrolling, which see.
16480 . Some Emacs command could force us to use a specific window-start
16481 position by setting the window's force_start flag, or gently
16482 propose doing that by setting the window's optional_new_start
16483 flag. In these cases, we try using the specified start point if
16484 that succeeds (i.e. the window desired matrix is successfully
16485 recomputed, and point location is within the window). In case
16486 of optional_new_start, we first check if the specified start
16487 position is feasible, i.e. if it will allow point to be
16488 displayed in the window. If using the specified start point
16489 fails, e.g., if new fonts are needed to be loaded, we abort the
16490 redisplay cycle and leave it up to the next cycle to figure out
16491 things.
16493 . Note that the window's force_start flag is sometimes set by
16494 redisplay itself, when it decides that the previous window start
16495 point is fine and should be kept. Search for "goto force_start"
16496 below to see the details. Like the values of window-start
16497 specified outside of redisplay, these internally-deduced values
16498 are tested for feasibility, and ignored if found to be
16499 unfeasible.
16501 . Note that the function try_window, used to completely redisplay
16502 a window, accepts the window's start point as its argument.
16503 This is used several times in the redisplay code to control
16504 where the window start will be, according to user options such
16505 as scroll-conservatively, and also to ensure the screen line
16506 showing point will be fully (as opposed to partially) visible on
16507 display. */
16509 static void
16510 redisplay_window (Lisp_Object window, bool just_this_one_p)
16512 struct window *w = XWINDOW (window);
16513 struct frame *f = XFRAME (w->frame);
16514 struct buffer *buffer = XBUFFER (w->contents);
16515 struct buffer *old = current_buffer;
16516 struct text_pos lpoint, opoint, startp;
16517 bool update_mode_line;
16518 int tem;
16519 struct it it;
16520 /* Record it now because it's overwritten. */
16521 bool current_matrix_up_to_date_p = false;
16522 bool used_current_matrix_p = false;
16523 /* This is less strict than current_matrix_up_to_date_p.
16524 It indicates that the buffer contents and narrowing are unchanged. */
16525 bool buffer_unchanged_p = false;
16526 bool temp_scroll_step = false;
16527 ptrdiff_t count = SPECPDL_INDEX ();
16528 int rc;
16529 int centering_position = -1;
16530 bool last_line_misfit = false;
16531 ptrdiff_t beg_unchanged, end_unchanged;
16532 int frame_line_height, margin;
16533 bool use_desired_matrix;
16534 void *itdata = NULL;
16536 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16537 opoint = lpoint;
16539 #ifdef GLYPH_DEBUG
16540 *w->desired_matrix->method = 0;
16541 #endif
16543 if (!just_this_one_p
16544 && REDISPLAY_SOME_P ()
16545 && !w->redisplay
16546 && !w->update_mode_line
16547 && !f->face_change
16548 && !f->redisplay
16549 && !buffer->text->redisplay
16550 && BUF_PT (buffer) == w->last_point)
16551 return;
16553 /* Make sure that both W's markers are valid. */
16554 eassert (XMARKER (w->start)->buffer == buffer);
16555 eassert (XMARKER (w->pointm)->buffer == buffer);
16557 reconsider_clip_changes (w);
16558 frame_line_height = default_line_pixel_height (w);
16559 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16562 /* Has the mode line to be updated? */
16563 update_mode_line = (w->update_mode_line
16564 || update_mode_lines
16565 || buffer->clip_changed
16566 || buffer->prevent_redisplay_optimizations_p);
16568 if (!just_this_one_p)
16569 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16570 cleverly elsewhere. */
16571 w->must_be_updated_p = true;
16573 if (MINI_WINDOW_P (w))
16575 if (w == XWINDOW (echo_area_window)
16576 && !NILP (echo_area_buffer[0]))
16578 if (update_mode_line)
16579 /* We may have to update a tty frame's menu bar or a
16580 tool-bar. Example `M-x C-h C-h C-g'. */
16581 goto finish_menu_bars;
16582 else
16583 /* We've already displayed the echo area glyphs in this window. */
16584 goto finish_scroll_bars;
16586 else if ((w != XWINDOW (minibuf_window)
16587 || minibuf_level == 0)
16588 /* When buffer is nonempty, redisplay window normally. */
16589 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16590 /* Quail displays non-mini buffers in minibuffer window.
16591 In that case, redisplay the window normally. */
16592 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16594 /* W is a mini-buffer window, but it's not active, so clear
16595 it. */
16596 int yb = window_text_bottom_y (w);
16597 struct glyph_row *row;
16598 int y;
16600 for (y = 0, row = w->desired_matrix->rows;
16601 y < yb;
16602 y += row->height, ++row)
16603 blank_row (w, row, y);
16604 goto finish_scroll_bars;
16607 clear_glyph_matrix (w->desired_matrix);
16610 /* Otherwise set up data on this window; select its buffer and point
16611 value. */
16612 /* Really select the buffer, for the sake of buffer-local
16613 variables. */
16614 set_buffer_internal_1 (XBUFFER (w->contents));
16616 current_matrix_up_to_date_p
16617 = (w->window_end_valid
16618 && !current_buffer->clip_changed
16619 && !current_buffer->prevent_redisplay_optimizations_p
16620 && !window_outdated (w)
16621 && !hscrolling_current_line_p (w));
16623 beg_unchanged = BEG_UNCHANGED;
16624 end_unchanged = END_UNCHANGED;
16626 SET_TEXT_POS (opoint, PT, PT_BYTE);
16628 specbind (Qinhibit_point_motion_hooks, Qt);
16630 buffer_unchanged_p
16631 = (w->window_end_valid
16632 && !current_buffer->clip_changed
16633 && !window_outdated (w));
16635 /* When windows_or_buffers_changed is non-zero, we can't rely
16636 on the window end being valid, so set it to zero there. */
16637 if (windows_or_buffers_changed)
16639 /* If window starts on a continuation line, maybe adjust the
16640 window start in case the window's width changed. */
16641 if (XMARKER (w->start)->buffer == current_buffer)
16642 compute_window_start_on_continuation_line (w);
16644 w->window_end_valid = false;
16645 /* If so, we also can't rely on current matrix
16646 and should not fool try_cursor_movement below. */
16647 current_matrix_up_to_date_p = false;
16650 /* Some sanity checks. */
16651 CHECK_WINDOW_END (w);
16652 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16653 emacs_abort ();
16654 if (BYTEPOS (opoint) < CHARPOS (opoint))
16655 emacs_abort ();
16657 if (mode_line_update_needed (w))
16658 update_mode_line = true;
16660 /* Point refers normally to the selected window. For any other
16661 window, set up appropriate value. */
16662 if (!EQ (window, selected_window))
16664 ptrdiff_t new_pt = marker_position (w->pointm);
16665 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16667 if (new_pt < BEGV)
16669 new_pt = BEGV;
16670 new_pt_byte = BEGV_BYTE;
16671 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16673 else if (new_pt > (ZV - 1))
16675 new_pt = ZV;
16676 new_pt_byte = ZV_BYTE;
16677 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16680 /* We don't use SET_PT so that the point-motion hooks don't run. */
16681 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16684 /* If any of the character widths specified in the display table
16685 have changed, invalidate the width run cache. It's true that
16686 this may be a bit late to catch such changes, but the rest of
16687 redisplay goes (non-fatally) haywire when the display table is
16688 changed, so why should we worry about doing any better? */
16689 if (current_buffer->width_run_cache
16690 || (current_buffer->base_buffer
16691 && current_buffer->base_buffer->width_run_cache))
16693 struct Lisp_Char_Table *disptab = buffer_display_table ();
16695 if (! disptab_matches_widthtab
16696 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16698 struct buffer *buf = current_buffer;
16700 if (buf->base_buffer)
16701 buf = buf->base_buffer;
16702 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16703 recompute_width_table (current_buffer, disptab);
16707 /* If window-start is screwed up, choose a new one. */
16708 if (XMARKER (w->start)->buffer != current_buffer)
16709 goto recenter;
16711 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16713 /* If someone specified a new starting point but did not insist,
16714 check whether it can be used. */
16715 if ((w->optional_new_start || window_frozen_p (w))
16716 && CHARPOS (startp) >= BEGV
16717 && CHARPOS (startp) <= ZV)
16719 ptrdiff_t it_charpos;
16721 w->optional_new_start = false;
16722 start_display (&it, w, startp);
16723 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16724 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16725 /* Record IT's position now, since line_bottom_y might change
16726 that. */
16727 it_charpos = IT_CHARPOS (it);
16728 /* Make sure we set the force_start flag only if the cursor row
16729 will be fully visible. Otherwise, the code under force_start
16730 label below will try to move point back into view, which is
16731 not what the code which sets optional_new_start wants. */
16732 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16733 && !w->force_start)
16735 if (it_charpos == PT)
16736 w->force_start = true;
16737 /* IT may overshoot PT if text at PT is invisible. */
16738 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16739 w->force_start = true;
16740 #ifdef GLYPH_DEBUG
16741 if (w->force_start)
16743 if (window_frozen_p (w))
16744 debug_method_add (w, "set force_start from frozen window start");
16745 else
16746 debug_method_add (w, "set force_start from optional_new_start");
16748 #endif
16752 force_start:
16754 /* Handle case where place to start displaying has been specified,
16755 unless the specified location is outside the accessible range. */
16756 if (w->force_start)
16758 /* We set this later on if we have to adjust point. */
16759 int new_vpos = -1;
16761 w->force_start = false;
16762 w->vscroll = 0;
16763 w->window_end_valid = false;
16765 /* Forget any recorded base line for line number display. */
16766 if (!buffer_unchanged_p)
16767 w->base_line_number = 0;
16769 /* Redisplay the mode line. Select the buffer properly for that.
16770 Also, run the hook window-scroll-functions
16771 because we have scrolled. */
16772 /* Note, we do this after clearing force_start because
16773 if there's an error, it is better to forget about force_start
16774 than to get into an infinite loop calling the hook functions
16775 and having them get more errors. */
16776 if (!update_mode_line
16777 || ! NILP (Vwindow_scroll_functions))
16779 update_mode_line = true;
16780 w->update_mode_line = true;
16781 startp = run_window_scroll_functions (window, startp);
16784 if (CHARPOS (startp) < BEGV)
16785 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16786 else if (CHARPOS (startp) > ZV)
16787 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16789 /* Redisplay, then check if cursor has been set during the
16790 redisplay. Give up if new fonts were loaded. */
16791 /* We used to issue a CHECK_MARGINS argument to try_window here,
16792 but this causes scrolling to fail when point begins inside
16793 the scroll margin (bug#148) -- cyd */
16794 if (!try_window (window, startp, 0))
16796 w->force_start = true;
16797 clear_glyph_matrix (w->desired_matrix);
16798 goto need_larger_matrices;
16801 if (w->cursor.vpos < 0)
16803 /* If point does not appear, try to move point so it does
16804 appear. The desired matrix has been built above, so we
16805 can use it here. First see if point is in invisible
16806 text, and if so, move it to the first visible buffer
16807 position past that. */
16808 struct glyph_row *r = NULL;
16809 Lisp_Object invprop =
16810 get_char_property_and_overlay (make_number (PT), Qinvisible,
16811 Qnil, NULL);
16813 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16815 ptrdiff_t alt_pt;
16816 Lisp_Object invprop_end =
16817 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16818 Qnil, Qnil);
16820 if (NATNUMP (invprop_end))
16821 alt_pt = XFASTINT (invprop_end);
16822 else
16823 alt_pt = ZV;
16824 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16825 NULL, 0);
16827 if (r)
16828 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16829 else /* Give up and just move to the middle of the window. */
16830 new_vpos = window_box_height (w) / 2;
16833 if (!cursor_row_fully_visible_p (w, false, false))
16835 /* Point does appear, but on a line partly visible at end of window.
16836 Move it back to a fully-visible line. */
16837 new_vpos = window_box_height (w);
16838 /* But if window_box_height suggests a Y coordinate that is
16839 not less than we already have, that line will clearly not
16840 be fully visible, so give up and scroll the display.
16841 This can happen when the default face uses a font whose
16842 dimensions are different from the frame's default
16843 font. */
16844 if (new_vpos >= w->cursor.y)
16846 w->cursor.vpos = -1;
16847 clear_glyph_matrix (w->desired_matrix);
16848 goto try_to_scroll;
16851 else if (w->cursor.vpos >= 0)
16853 /* Some people insist on not letting point enter the scroll
16854 margin, even though this part handles windows that didn't
16855 scroll at all. */
16856 int pixel_margin = margin * frame_line_height;
16857 bool header_line = window_wants_header_line (w);
16859 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16860 below, which finds the row to move point to, advances by
16861 the Y coordinate of the _next_ row, see the definition of
16862 MATRIX_ROW_BOTTOM_Y. */
16863 if (w->cursor.vpos < margin + header_line)
16865 w->cursor.vpos = -1;
16866 clear_glyph_matrix (w->desired_matrix);
16867 goto try_to_scroll;
16869 else
16871 int window_height = window_box_height (w);
16873 if (header_line)
16874 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16875 if (w->cursor.y >= window_height - pixel_margin)
16877 w->cursor.vpos = -1;
16878 clear_glyph_matrix (w->desired_matrix);
16879 goto try_to_scroll;
16884 /* If we need to move point for either of the above reasons,
16885 now actually do it. */
16886 if (new_vpos >= 0)
16888 struct glyph_row *row;
16890 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16891 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16892 ++row;
16894 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16895 MATRIX_ROW_START_BYTEPOS (row));
16897 if (w != XWINDOW (selected_window))
16898 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16899 else if (current_buffer == old)
16900 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16902 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16904 /* Re-run pre-redisplay-function so it can update the region
16905 according to the new position of point. */
16906 /* Other than the cursor, w's redisplay is done so we can set its
16907 redisplay to false. Also the buffer's redisplay can be set to
16908 false, since propagate_buffer_redisplay should have already
16909 propagated its info to `w' anyway. */
16910 w->redisplay = false;
16911 XBUFFER (w->contents)->text->redisplay = false;
16912 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16914 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16915 || ((EQ (Vdisplay_line_numbers, Qrelative)
16916 || EQ (Vdisplay_line_numbers, Qvisual))
16917 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16919 /* Either pre-redisplay-function made changes (e.g. move
16920 the region), or we moved point in a window that is
16921 under display-line-numbers = relative mode. We need
16922 another round of redisplay. */
16923 clear_glyph_matrix (w->desired_matrix);
16924 if (!try_window (window, startp, 0))
16925 goto need_larger_matrices;
16928 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16930 clear_glyph_matrix (w->desired_matrix);
16931 goto try_to_scroll;
16934 #ifdef GLYPH_DEBUG
16935 debug_method_add (w, "forced window start");
16936 #endif
16937 goto done;
16940 /* Handle case where text has not changed, only point, and it has
16941 not moved off the frame, and we are not retrying after hscroll.
16942 (current_matrix_up_to_date_p is true when retrying.) */
16943 if (current_matrix_up_to_date_p
16944 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16945 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16947 switch (rc)
16949 case CURSOR_MOVEMENT_SUCCESS:
16950 used_current_matrix_p = true;
16951 goto done;
16953 case CURSOR_MOVEMENT_MUST_SCROLL:
16954 goto try_to_scroll;
16956 default:
16957 emacs_abort ();
16960 /* If current starting point was originally the beginning of a line
16961 but no longer is, find a new starting point. */
16962 else if (w->start_at_line_beg
16963 && !(CHARPOS (startp) <= BEGV
16964 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16966 #ifdef GLYPH_DEBUG
16967 debug_method_add (w, "recenter 1");
16968 #endif
16969 goto recenter;
16972 /* Try scrolling with try_window_id. Value is > 0 if update has
16973 been done, it is -1 if we know that the same window start will
16974 not work. It is 0 if unsuccessful for some other reason. */
16975 else if ((tem = try_window_id (w)) != 0)
16977 #ifdef GLYPH_DEBUG
16978 debug_method_add (w, "try_window_id %d", tem);
16979 #endif
16981 if (f->fonts_changed)
16982 goto need_larger_matrices;
16983 if (tem > 0)
16984 goto done;
16986 /* Otherwise try_window_id has returned -1 which means that we
16987 don't want the alternative below this comment to execute. */
16989 else if (CHARPOS (startp) >= BEGV
16990 && CHARPOS (startp) <= ZV
16991 && PT >= CHARPOS (startp)
16992 && (CHARPOS (startp) < ZV
16993 /* Avoid starting at end of buffer. */
16994 || CHARPOS (startp) == BEGV
16995 || !window_outdated (w)))
16997 int d1, d2, d5, d6;
16998 int rtop, rbot;
17000 /* If first window line is a continuation line, and window start
17001 is inside the modified region, but the first change is before
17002 current window start, we must select a new window start.
17004 However, if this is the result of a down-mouse event (e.g. by
17005 extending the mouse-drag-overlay), we don't want to select a
17006 new window start, since that would change the position under
17007 the mouse, resulting in an unwanted mouse-movement rather
17008 than a simple mouse-click. */
17009 if (!w->start_at_line_beg
17010 && NILP (do_mouse_tracking)
17011 && CHARPOS (startp) > BEGV
17012 && CHARPOS (startp) > BEG + beg_unchanged
17013 && CHARPOS (startp) <= Z - end_unchanged
17014 /* Even if w->start_at_line_beg is nil, a new window may
17015 start at a line_beg, since that's how set_buffer_window
17016 sets it. So, we need to check the return value of
17017 compute_window_start_on_continuation_line. (See also
17018 bug#197). */
17019 && XMARKER (w->start)->buffer == current_buffer
17020 && compute_window_start_on_continuation_line (w)
17021 /* It doesn't make sense to force the window start like we
17022 do at label force_start if it is already known that point
17023 will not be fully visible in the resulting window, because
17024 doing so will move point from its correct position
17025 instead of scrolling the window to bring point into view.
17026 See bug#9324. */
17027 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17028 /* A very tall row could need more than the window height,
17029 in which case we accept that it is partially visible. */
17030 && (rtop != 0) == (rbot != 0))
17032 w->force_start = true;
17033 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17034 #ifdef GLYPH_DEBUG
17035 debug_method_add (w, "recomputed window start in continuation line");
17036 #endif
17037 goto force_start;
17040 #ifdef GLYPH_DEBUG
17041 debug_method_add (w, "same window start");
17042 #endif
17044 /* Try to redisplay starting at same place as before.
17045 If point has not moved off frame, accept the results. */
17046 if (!current_matrix_up_to_date_p
17047 /* Don't use try_window_reusing_current_matrix in this case
17048 because a window scroll function can have changed the
17049 buffer. */
17050 || !NILP (Vwindow_scroll_functions)
17051 || MINI_WINDOW_P (w)
17052 || !(used_current_matrix_p
17053 = try_window_reusing_current_matrix (w)))
17055 IF_DEBUG (debug_method_add (w, "1"));
17056 clear_glyph_matrix (w->desired_matrix);
17057 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17058 /* -1 means we need to scroll.
17059 0 means we need new matrices, but fonts_changed
17060 is set in that case, so we will detect it below. */
17061 goto try_to_scroll;
17064 if (f->fonts_changed)
17065 goto need_larger_matrices;
17067 if (w->cursor.vpos >= 0)
17069 if (!just_this_one_p
17070 || current_buffer->clip_changed
17071 || BEG_UNCHANGED < CHARPOS (startp))
17072 /* Forget any recorded base line for line number display. */
17073 w->base_line_number = 0;
17075 if (!cursor_row_fully_visible_p (w, true, false))
17077 clear_glyph_matrix (w->desired_matrix);
17078 last_line_misfit = true;
17080 /* Drop through and scroll. */
17081 else
17082 goto done;
17084 else
17085 clear_glyph_matrix (w->desired_matrix);
17088 try_to_scroll:
17090 /* Redisplay the mode line. Select the buffer properly for that. */
17091 if (!update_mode_line)
17093 update_mode_line = true;
17094 w->update_mode_line = true;
17097 /* Try to scroll by specified few lines. */
17098 if ((scroll_conservatively
17099 || emacs_scroll_step
17100 || temp_scroll_step
17101 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17102 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17103 && CHARPOS (startp) >= BEGV
17104 && CHARPOS (startp) <= ZV)
17106 /* The function returns -1 if new fonts were loaded, 1 if
17107 successful, 0 if not successful. */
17108 int ss = try_scrolling (window, just_this_one_p,
17109 scroll_conservatively,
17110 emacs_scroll_step,
17111 temp_scroll_step, last_line_misfit);
17112 switch (ss)
17114 case SCROLLING_SUCCESS:
17115 goto done;
17117 case SCROLLING_NEED_LARGER_MATRICES:
17118 goto need_larger_matrices;
17120 case SCROLLING_FAILED:
17121 break;
17123 default:
17124 emacs_abort ();
17128 /* Finally, just choose a place to start which positions point
17129 according to user preferences. */
17131 recenter:
17133 #ifdef GLYPH_DEBUG
17134 debug_method_add (w, "recenter");
17135 #endif
17137 /* Forget any previously recorded base line for line number display. */
17138 if (!buffer_unchanged_p)
17139 w->base_line_number = 0;
17141 /* Determine the window start relative to point. */
17142 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17143 it.current_y = it.last_visible_y;
17144 if (centering_position < 0)
17146 ptrdiff_t margin_pos = CHARPOS (startp);
17147 Lisp_Object aggressive;
17148 bool scrolling_up;
17150 /* If there is a scroll margin at the top of the window, find
17151 its character position. */
17152 if (margin
17153 /* Cannot call start_display if startp is not in the
17154 accessible region of the buffer. This can happen when we
17155 have just switched to a different buffer and/or changed
17156 its restriction. In that case, startp is initialized to
17157 the character position 1 (BEGV) because we did not yet
17158 have chance to display the buffer even once. */
17159 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17161 struct it it1;
17162 void *it1data = NULL;
17164 SAVE_IT (it1, it, it1data);
17165 start_display (&it1, w, startp);
17166 move_it_vertically (&it1, margin * frame_line_height);
17167 margin_pos = IT_CHARPOS (it1);
17168 RESTORE_IT (&it, &it, it1data);
17170 scrolling_up = PT > margin_pos;
17171 aggressive =
17172 scrolling_up
17173 ? BVAR (current_buffer, scroll_up_aggressively)
17174 : BVAR (current_buffer, scroll_down_aggressively);
17176 if (!MINI_WINDOW_P (w)
17177 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17179 int pt_offset = 0;
17181 /* Setting scroll-conservatively overrides
17182 scroll-*-aggressively. */
17183 if (!scroll_conservatively && NUMBERP (aggressive))
17185 double float_amount = XFLOATINT (aggressive);
17187 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17188 if (pt_offset == 0 && float_amount > 0)
17189 pt_offset = 1;
17190 if (pt_offset && margin > 0)
17191 margin -= 1;
17193 /* Compute how much to move the window start backward from
17194 point so that point will be displayed where the user
17195 wants it. */
17196 if (scrolling_up)
17198 centering_position = it.last_visible_y;
17199 if (pt_offset)
17200 centering_position -= pt_offset;
17201 centering_position -=
17202 (frame_line_height * (1 + margin + last_line_misfit)
17203 + WINDOW_HEADER_LINE_HEIGHT (w));
17204 /* Don't let point enter the scroll margin near top of
17205 the window. */
17206 if (centering_position < margin * frame_line_height)
17207 centering_position = margin * frame_line_height;
17209 else
17210 centering_position = margin * frame_line_height + pt_offset;
17212 else
17213 /* Set the window start half the height of the window backward
17214 from point. */
17215 centering_position = window_box_height (w) / 2;
17217 move_it_vertically_backward (&it, centering_position);
17219 eassert (IT_CHARPOS (it) >= BEGV);
17221 /* The function move_it_vertically_backward may move over more
17222 than the specified y-distance. If it->w is small, e.g. a
17223 mini-buffer window, we may end up in front of the window's
17224 display area. Start displaying at the start of the line
17225 containing PT in this case. */
17226 if (it.current_y <= 0)
17228 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17229 move_it_vertically_backward (&it, 0);
17230 it.current_y = 0;
17233 it.current_x = it.hpos = 0;
17235 /* Set the window start position here explicitly, to avoid an
17236 infinite loop in case the functions in window-scroll-functions
17237 get errors. */
17238 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17240 /* Run scroll hooks. */
17241 startp = run_window_scroll_functions (window, it.current.pos);
17243 /* We invoke try_window and try_window_reusing_current_matrix below,
17244 and they manipulate the bidi cache. Save and restore the cache
17245 state of our iterator, so we could continue using it after that. */
17246 itdata = bidi_shelve_cache ();
17248 /* Redisplay the window. */
17249 use_desired_matrix = false;
17250 if (!current_matrix_up_to_date_p
17251 || windows_or_buffers_changed
17252 || f->cursor_type_changed
17253 /* Don't use try_window_reusing_current_matrix in this case
17254 because it can have changed the buffer. */
17255 || !NILP (Vwindow_scroll_functions)
17256 || !just_this_one_p
17257 || MINI_WINDOW_P (w)
17258 || !(used_current_matrix_p
17259 = try_window_reusing_current_matrix (w)))
17260 use_desired_matrix = (try_window (window, startp, 0) == 1);
17262 bidi_unshelve_cache (itdata, false);
17264 /* If new fonts have been loaded (due to fontsets), give up. We
17265 have to start a new redisplay since we need to re-adjust glyph
17266 matrices. */
17267 if (f->fonts_changed)
17268 goto need_larger_matrices;
17270 /* If cursor did not appear assume that the middle of the window is
17271 in the first line of the window. Do it again with the next line.
17272 (Imagine a window of height 100, displaying two lines of height
17273 60. Moving back 50 from it->last_visible_y will end in the first
17274 line.) */
17275 if (w->cursor.vpos < 0)
17277 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17279 clear_glyph_matrix (w->desired_matrix);
17280 move_it_by_lines (&it, 1);
17281 try_window (window, it.current.pos, 0);
17283 else if (PT < IT_CHARPOS (it))
17285 clear_glyph_matrix (w->desired_matrix);
17286 move_it_by_lines (&it, -1);
17287 try_window (window, it.current.pos, 0);
17289 else if (scroll_conservatively > SCROLL_LIMIT
17290 && (it.method == GET_FROM_STRING
17291 || overlay_touches_p (IT_CHARPOS (it)))
17292 && IT_CHARPOS (it) < ZV)
17294 /* If the window starts with a before-string that spans more
17295 than one screen line, using that position to display the
17296 window might fail to bring point into the view, because
17297 start_display will always start by displaying the string,
17298 whereas the code above determines where to set w->start
17299 by the buffer position of the place where it takes screen
17300 coordinates. Try to recover by finding the next screen
17301 line that displays buffer text. */
17302 ptrdiff_t pos0 = IT_CHARPOS (it);
17304 clear_glyph_matrix (w->desired_matrix);
17305 do {
17306 move_it_by_lines (&it, 1);
17307 } while (IT_CHARPOS (it) == pos0);
17308 try_window (window, it.current.pos, 0);
17310 else
17312 /* Not much we can do about it. */
17316 /* Consider the following case: Window starts at BEGV, there is
17317 invisible, intangible text at BEGV, so that display starts at
17318 some point START > BEGV. It can happen that we are called with
17319 PT somewhere between BEGV and START. Try to handle that case,
17320 and similar ones. */
17321 if (w->cursor.vpos < 0)
17323 /* Prefer the desired matrix to the current matrix, if possible,
17324 in the fallback calculations below. This is because using
17325 the current matrix might completely goof, e.g. if its first
17326 row is after point. */
17327 struct glyph_matrix *matrix =
17328 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17329 /* First, try locating the proper glyph row for PT. */
17330 struct glyph_row *row =
17331 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17333 /* Sometimes point is at the beginning of invisible text that is
17334 before the 1st character displayed in the row. In that case,
17335 row_containing_pos fails to find the row, because no glyphs
17336 with appropriate buffer positions are present in the row.
17337 Therefore, we next try to find the row which shows the 1st
17338 position after the invisible text. */
17339 if (!row)
17341 Lisp_Object val =
17342 get_char_property_and_overlay (make_number (PT), Qinvisible,
17343 Qnil, NULL);
17345 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17347 ptrdiff_t alt_pos;
17348 Lisp_Object invis_end =
17349 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17350 Qnil, Qnil);
17352 if (NATNUMP (invis_end))
17353 alt_pos = XFASTINT (invis_end);
17354 else
17355 alt_pos = ZV;
17356 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17359 /* Finally, fall back on the first row of the window after the
17360 header line (if any). This is slightly better than not
17361 displaying the cursor at all. */
17362 if (!row)
17364 row = matrix->rows;
17365 if (row->mode_line_p)
17366 ++row;
17368 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17371 if (!cursor_row_fully_visible_p (w, false, false))
17373 /* If vscroll is enabled, disable it and try again. */
17374 if (w->vscroll)
17376 w->vscroll = 0;
17377 clear_glyph_matrix (w->desired_matrix);
17378 goto recenter;
17381 /* Users who set scroll-conservatively to a large number want
17382 point just above/below the scroll margin. If we ended up
17383 with point's row partially visible, move the window start to
17384 make that row fully visible and out of the margin. */
17385 if (scroll_conservatively > SCROLL_LIMIT)
17387 int window_total_lines
17388 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17389 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17391 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17392 clear_glyph_matrix (w->desired_matrix);
17393 if (1 == try_window (window, it.current.pos,
17394 TRY_WINDOW_CHECK_MARGINS))
17395 goto done;
17398 /* If centering point failed to make the whole line visible,
17399 put point at the top instead. That has to make the whole line
17400 visible, if it can be done. */
17401 if (centering_position == 0)
17402 goto done;
17404 clear_glyph_matrix (w->desired_matrix);
17405 centering_position = 0;
17406 goto recenter;
17409 done:
17411 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17412 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17413 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17415 /* Display the mode line, if we must. */
17416 if ((update_mode_line
17417 /* If window not full width, must redo its mode line
17418 if (a) the window to its side is being redone and
17419 (b) we do a frame-based redisplay. This is a consequence
17420 of how inverted lines are drawn in frame-based redisplay. */
17421 || (!just_this_one_p
17422 && !FRAME_WINDOW_P (f)
17423 && !WINDOW_FULL_WIDTH_P (w))
17424 /* Line number to display. */
17425 || w->base_line_pos > 0
17426 /* Column number is displayed and different from the one displayed. */
17427 || (w->column_number_displayed != -1
17428 && (w->column_number_displayed != current_column ())))
17429 /* This means that the window has a mode line. */
17430 && (window_wants_mode_line (w)
17431 || window_wants_header_line (w)))
17434 display_mode_lines (w);
17436 /* If mode line height has changed, arrange for a thorough
17437 immediate redisplay using the correct mode line height. */
17438 if (window_wants_mode_line (w)
17439 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17441 f->fonts_changed = true;
17442 w->mode_line_height = -1;
17443 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17444 = DESIRED_MODE_LINE_HEIGHT (w);
17447 /* If header line height has changed, arrange for a thorough
17448 immediate redisplay using the correct header line height. */
17449 if (window_wants_header_line (w)
17450 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17452 f->fonts_changed = true;
17453 w->header_line_height = -1;
17454 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17455 = DESIRED_HEADER_LINE_HEIGHT (w);
17458 if (f->fonts_changed)
17459 goto need_larger_matrices;
17462 if (!line_number_displayed && w->base_line_pos != -1)
17464 w->base_line_pos = 0;
17465 w->base_line_number = 0;
17468 finish_menu_bars:
17470 /* When we reach a frame's selected window, redo the frame's menu
17471 bar and the frame's title. */
17472 if (update_mode_line
17473 && EQ (FRAME_SELECTED_WINDOW (f), window))
17475 bool redisplay_menu_p;
17477 if (FRAME_WINDOW_P (f))
17479 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17480 || defined (HAVE_NS) || defined (USE_GTK)
17481 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17482 #else
17483 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17484 #endif
17486 else
17487 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17489 if (redisplay_menu_p)
17490 display_menu_bar (w);
17492 #ifdef HAVE_WINDOW_SYSTEM
17493 if (FRAME_WINDOW_P (f))
17495 #if defined (USE_GTK) || defined (HAVE_NS)
17496 if (FRAME_EXTERNAL_TOOL_BAR (f))
17497 redisplay_tool_bar (f);
17498 #else
17499 if (WINDOWP (f->tool_bar_window)
17500 && (FRAME_TOOL_BAR_LINES (f) > 0
17501 || !NILP (Vauto_resize_tool_bars))
17502 && redisplay_tool_bar (f))
17503 ignore_mouse_drag_p = true;
17504 #endif
17506 x_consider_frame_title (w->frame);
17507 #endif
17510 #ifdef HAVE_WINDOW_SYSTEM
17511 if (FRAME_WINDOW_P (f)
17512 && update_window_fringes (w, (just_this_one_p
17513 || (!used_current_matrix_p && !overlay_arrow_seen)
17514 || w->pseudo_window_p)))
17516 update_begin (f);
17517 block_input ();
17518 if (draw_window_fringes (w, true))
17520 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17521 x_draw_right_divider (w);
17522 else
17523 x_draw_vertical_border (w);
17525 unblock_input ();
17526 update_end (f);
17529 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17530 x_draw_bottom_divider (w);
17531 #endif /* HAVE_WINDOW_SYSTEM */
17533 /* We go to this label, with fonts_changed set, if it is
17534 necessary to try again using larger glyph matrices.
17535 We have to redeem the scroll bar even in this case,
17536 because the loop in redisplay_internal expects that. */
17537 need_larger_matrices:
17539 finish_scroll_bars:
17541 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17543 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17544 /* Set the thumb's position and size. */
17545 set_vertical_scroll_bar (w);
17547 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17548 /* Set the thumb's position and size. */
17549 set_horizontal_scroll_bar (w);
17551 /* Note that we actually used the scroll bar attached to this
17552 window, so it shouldn't be deleted at the end of redisplay. */
17553 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17554 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17557 /* Restore current_buffer and value of point in it. The window
17558 update may have changed the buffer, so first make sure `opoint'
17559 is still valid (Bug#6177). */
17560 if (CHARPOS (opoint) < BEGV)
17561 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17562 else if (CHARPOS (opoint) > ZV)
17563 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17564 else
17565 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17567 set_buffer_internal_1 (old);
17568 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17569 shorter. This can be caused by log truncation in *Messages*. */
17570 if (CHARPOS (lpoint) <= ZV)
17571 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17573 unbind_to (count, Qnil);
17577 /* Build the complete desired matrix of WINDOW with a window start
17578 buffer position POS.
17580 Value is 1 if successful. It is zero if fonts were loaded during
17581 redisplay which makes re-adjusting glyph matrices necessary, and -1
17582 if point would appear in the scroll margins.
17583 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17584 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17585 set in FLAGS.) */
17588 try_window (Lisp_Object window, struct text_pos pos, int flags)
17590 struct window *w = XWINDOW (window);
17591 struct it it;
17592 struct glyph_row *last_text_row = NULL;
17593 struct frame *f = XFRAME (w->frame);
17594 int cursor_vpos = w->cursor.vpos;
17596 /* Make POS the new window start. */
17597 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17599 /* Mark cursor position as unknown. No overlay arrow seen. */
17600 w->cursor.vpos = -1;
17601 overlay_arrow_seen = false;
17603 /* Initialize iterator and info to start at POS. */
17604 start_display (&it, w, pos);
17605 it.glyph_row->reversed_p = false;
17607 /* Display all lines of W. */
17608 while (it.current_y < it.last_visible_y)
17610 if (display_line (&it, cursor_vpos))
17611 last_text_row = it.glyph_row - 1;
17612 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17613 return 0;
17616 /* Save the character position of 'it' before we call
17617 'start_display' again. */
17618 ptrdiff_t it_charpos = IT_CHARPOS (it);
17620 /* Don't let the cursor end in the scroll margins. */
17621 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17622 && !MINI_WINDOW_P (w))
17624 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17625 start_display (&it, w, pos);
17627 if ((w->cursor.y >= 0 /* not vscrolled */
17628 && w->cursor.y < this_scroll_margin
17629 && CHARPOS (pos) > BEGV
17630 && it_charpos < ZV)
17631 /* rms: considering make_cursor_line_fully_visible_p here
17632 seems to give wrong results. We don't want to recenter
17633 when the last line is partly visible, we want to allow
17634 that case to be handled in the usual way. */
17635 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17636 - this_scroll_margin - 1))
17638 w->cursor.vpos = -1;
17639 clear_glyph_matrix (w->desired_matrix);
17640 return -1;
17644 /* If bottom moved off end of frame, change mode line percentage. */
17645 if (w->window_end_pos <= 0 && Z != it_charpos)
17646 w->update_mode_line = true;
17648 /* Set window_end_pos to the offset of the last character displayed
17649 on the window from the end of current_buffer. Set
17650 window_end_vpos to its row number. */
17651 if (last_text_row)
17653 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17654 adjust_window_ends (w, last_text_row, false);
17655 eassert
17656 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17657 w->window_end_vpos)));
17659 else
17661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17662 w->window_end_pos = Z - ZV;
17663 w->window_end_vpos = 0;
17666 /* But that is not valid info until redisplay finishes. */
17667 w->window_end_valid = false;
17668 return 1;
17673 /************************************************************************
17674 Window redisplay reusing current matrix when buffer has not changed
17675 ************************************************************************/
17677 /* Try redisplay of window W showing an unchanged buffer with a
17678 different window start than the last time it was displayed by
17679 reusing its current matrix. Value is true if successful.
17680 W->start is the new window start. */
17682 static bool
17683 try_window_reusing_current_matrix (struct window *w)
17685 struct frame *f = XFRAME (w->frame);
17686 struct glyph_row *bottom_row;
17687 struct it it;
17688 struct run run;
17689 struct text_pos start, new_start;
17690 int nrows_scrolled, i;
17691 struct glyph_row *last_text_row;
17692 struct glyph_row *last_reused_text_row;
17693 struct glyph_row *start_row;
17694 int start_vpos, min_y, max_y;
17696 #ifdef GLYPH_DEBUG
17697 if (inhibit_try_window_reusing)
17698 return false;
17699 #endif
17701 if (/* This function doesn't handle terminal frames. */
17702 !FRAME_WINDOW_P (f)
17703 /* Don't try to reuse the display if windows have been split
17704 or such. */
17705 || windows_or_buffers_changed
17706 || f->cursor_type_changed
17707 /* This function cannot handle buffers where the overlay arrow
17708 is shown on the fringes, because if the arrow position
17709 changes, we cannot just reuse the current matrix. */
17710 || overlay_arrow_in_current_buffer_p ())
17711 return false;
17713 /* Can't do this if showing trailing whitespace. */
17714 if (!NILP (Vshow_trailing_whitespace))
17715 return false;
17717 /* If top-line visibility has changed, give up. */
17718 if (window_wants_header_line (w)
17719 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17720 return false;
17722 /* Give up if old or new display is scrolled vertically. We could
17723 make this function handle this, but right now it doesn't. */
17724 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17725 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17726 return false;
17728 /* Clear the desired matrix for the display below. */
17729 clear_glyph_matrix (w->desired_matrix);
17731 /* Give up if line numbers are being displayed, because reusing the
17732 current matrix might use the wrong width for line-number
17733 display. */
17734 if (!NILP (Vdisplay_line_numbers))
17735 return false;
17737 /* The variable new_start now holds the new window start. The old
17738 start `start' can be determined from the current matrix. */
17739 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17740 start = start_row->minpos;
17741 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17743 if (CHARPOS (new_start) <= CHARPOS (start))
17745 /* Don't use this method if the display starts with an ellipsis
17746 displayed for invisible text. It's not easy to handle that case
17747 below, and it's certainly not worth the effort since this is
17748 not a frequent case. */
17749 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17750 return false;
17752 IF_DEBUG (debug_method_add (w, "twu1"));
17754 /* Display up to a row that can be reused. The variable
17755 last_text_row is set to the last row displayed that displays
17756 text. Note that it.vpos == 0 if or if not there is a
17757 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17758 start_display (&it, w, new_start);
17759 w->cursor.vpos = -1;
17760 last_text_row = last_reused_text_row = NULL;
17762 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17764 /* If we have reached into the characters in the START row,
17765 that means the line boundaries have changed. So we
17766 can't start copying with the row START. Maybe it will
17767 work to start copying with the following row. */
17768 while (IT_CHARPOS (it) > CHARPOS (start))
17770 /* Advance to the next row as the "start". */
17771 start_row++;
17772 start = start_row->minpos;
17773 /* If there are no more rows to try, or just one, give up. */
17774 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17775 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17776 || CHARPOS (start) == ZV)
17778 clear_glyph_matrix (w->desired_matrix);
17779 return false;
17782 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17784 /* If we have reached alignment, we can copy the rest of the
17785 rows. */
17786 if (IT_CHARPOS (it) == CHARPOS (start)
17787 /* Don't accept "alignment" inside a display vector,
17788 since start_row could have started in the middle of
17789 that same display vector (thus their character
17790 positions match), and we have no way of telling if
17791 that is the case. */
17792 && it.current.dpvec_index < 0)
17793 break;
17795 it.glyph_row->reversed_p = false;
17796 if (display_line (&it, -1))
17797 last_text_row = it.glyph_row - 1;
17801 /* A value of current_y < last_visible_y means that we stopped
17802 at the previous window start, which in turn means that we
17803 have at least one reusable row. */
17804 if (it.current_y < it.last_visible_y)
17806 struct glyph_row *row;
17808 /* IT.vpos always starts from 0; it counts text lines. */
17809 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17811 /* Find PT if not already found in the lines displayed. */
17812 if (w->cursor.vpos < 0)
17814 int dy = it.current_y - start_row->y;
17816 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17817 row = row_containing_pos (w, PT, row, NULL, dy);
17818 if (row)
17819 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17820 dy, nrows_scrolled);
17821 else
17823 clear_glyph_matrix (w->desired_matrix);
17824 return false;
17828 /* Scroll the display. Do it before the current matrix is
17829 changed. The problem here is that update has not yet
17830 run, i.e. part of the current matrix is not up to date.
17831 scroll_run_hook will clear the cursor, and use the
17832 current matrix to get the height of the row the cursor is
17833 in. */
17834 run.current_y = start_row->y;
17835 run.desired_y = it.current_y;
17836 run.height = it.last_visible_y - it.current_y;
17838 if (run.height > 0 && run.current_y != run.desired_y)
17840 update_begin (f);
17841 FRAME_RIF (f)->update_window_begin_hook (w);
17842 FRAME_RIF (f)->clear_window_mouse_face (w);
17843 FRAME_RIF (f)->scroll_run_hook (w, &run);
17844 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17845 update_end (f);
17848 /* Shift current matrix down by nrows_scrolled lines. */
17849 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17850 rotate_matrix (w->current_matrix,
17851 start_vpos,
17852 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17853 nrows_scrolled);
17855 /* Disable lines that must be updated. */
17856 for (i = 0; i < nrows_scrolled; ++i)
17857 (start_row + i)->enabled_p = false;
17859 /* Re-compute Y positions. */
17860 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17861 max_y = it.last_visible_y;
17862 for (row = start_row + nrows_scrolled;
17863 row < bottom_row;
17864 ++row)
17866 row->y = it.current_y;
17867 row->visible_height = row->height;
17869 if (row->y < min_y)
17870 row->visible_height -= min_y - row->y;
17871 if (row->y + row->height > max_y)
17872 row->visible_height -= row->y + row->height - max_y;
17873 if (row->fringe_bitmap_periodic_p)
17874 row->redraw_fringe_bitmaps_p = true;
17876 it.current_y += row->height;
17878 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17879 last_reused_text_row = row;
17880 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17881 break;
17884 /* Disable lines in the current matrix which are now
17885 below the window. */
17886 for (++row; row < bottom_row; ++row)
17887 row->enabled_p = row->mode_line_p = false;
17890 /* Update window_end_pos etc.; last_reused_text_row is the last
17891 reused row from the current matrix containing text, if any.
17892 The value of last_text_row is the last displayed line
17893 containing text. */
17894 if (last_reused_text_row)
17895 adjust_window_ends (w, last_reused_text_row, true);
17896 else if (last_text_row)
17897 adjust_window_ends (w, last_text_row, false);
17898 else
17900 /* This window must be completely empty. */
17901 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17902 w->window_end_pos = Z - ZV;
17903 w->window_end_vpos = 0;
17905 w->window_end_valid = false;
17907 /* Update hint: don't try scrolling again in update_window. */
17908 w->desired_matrix->no_scrolling_p = true;
17910 #ifdef GLYPH_DEBUG
17911 debug_method_add (w, "try_window_reusing_current_matrix 1");
17912 #endif
17913 return true;
17915 else if (CHARPOS (new_start) > CHARPOS (start))
17917 struct glyph_row *pt_row, *row;
17918 struct glyph_row *first_reusable_row;
17919 struct glyph_row *first_row_to_display;
17920 int dy;
17921 int yb = window_text_bottom_y (w);
17923 /* Find the row starting at new_start, if there is one. Don't
17924 reuse a partially visible line at the end. */
17925 first_reusable_row = start_row;
17926 while (first_reusable_row->enabled_p
17927 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17928 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17929 < CHARPOS (new_start)))
17930 ++first_reusable_row;
17932 /* Give up if there is no row to reuse. */
17933 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17934 || !first_reusable_row->enabled_p
17935 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17936 != CHARPOS (new_start)))
17937 return false;
17939 /* We can reuse fully visible rows beginning with
17940 first_reusable_row to the end of the window. Set
17941 first_row_to_display to the first row that cannot be reused.
17942 Set pt_row to the row containing point, if there is any. */
17943 pt_row = NULL;
17944 for (first_row_to_display = first_reusable_row;
17945 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17946 ++first_row_to_display)
17948 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17949 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17950 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17951 && first_row_to_display->ends_at_zv_p
17952 && pt_row == NULL)))
17953 pt_row = first_row_to_display;
17956 /* Start displaying at the start of first_row_to_display. */
17957 eassert (first_row_to_display->y < yb);
17958 init_to_row_start (&it, w, first_row_to_display);
17960 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17961 - start_vpos);
17962 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17963 - nrows_scrolled);
17964 it.current_y = (first_row_to_display->y - first_reusable_row->y
17965 + WINDOW_HEADER_LINE_HEIGHT (w));
17967 /* Display lines beginning with first_row_to_display in the
17968 desired matrix. Set last_text_row to the last row displayed
17969 that displays text. */
17970 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17971 if (pt_row == NULL)
17972 w->cursor.vpos = -1;
17973 last_text_row = NULL;
17974 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17975 if (display_line (&it, w->cursor.vpos))
17976 last_text_row = it.glyph_row - 1;
17978 /* If point is in a reused row, adjust y and vpos of the cursor
17979 position. */
17980 if (pt_row)
17982 w->cursor.vpos -= nrows_scrolled;
17983 w->cursor.y -= first_reusable_row->y - start_row->y;
17986 /* Give up if point isn't in a row displayed or reused. (This
17987 also handles the case where w->cursor.vpos < nrows_scrolled
17988 after the calls to display_line, which can happen with scroll
17989 margins. See bug#1295.) */
17990 if (w->cursor.vpos < 0)
17992 clear_glyph_matrix (w->desired_matrix);
17993 return false;
17996 /* Scroll the display. */
17997 run.current_y = first_reusable_row->y;
17998 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17999 run.height = it.last_visible_y - run.current_y;
18000 dy = run.current_y - run.desired_y;
18002 if (run.height)
18004 update_begin (f);
18005 FRAME_RIF (f)->update_window_begin_hook (w);
18006 FRAME_RIF (f)->clear_window_mouse_face (w);
18007 FRAME_RIF (f)->scroll_run_hook (w, &run);
18008 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18009 update_end (f);
18012 /* Adjust Y positions of reused rows. */
18013 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18014 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18015 max_y = it.last_visible_y;
18016 for (row = first_reusable_row; row < first_row_to_display; ++row)
18018 row->y -= dy;
18019 row->visible_height = row->height;
18020 if (row->y < min_y)
18021 row->visible_height -= min_y - row->y;
18022 if (row->y + row->height > max_y)
18023 row->visible_height -= row->y + row->height - max_y;
18024 if (row->fringe_bitmap_periodic_p)
18025 row->redraw_fringe_bitmaps_p = true;
18028 /* Scroll the current matrix. */
18029 eassert (nrows_scrolled > 0);
18030 rotate_matrix (w->current_matrix,
18031 start_vpos,
18032 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18033 -nrows_scrolled);
18035 /* Disable rows not reused. */
18036 for (row -= nrows_scrolled; row < bottom_row; ++row)
18037 row->enabled_p = false;
18039 /* Point may have moved to a different line, so we cannot assume that
18040 the previous cursor position is valid; locate the correct row. */
18041 if (pt_row)
18043 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18044 row < bottom_row
18045 && PT >= MATRIX_ROW_END_CHARPOS (row)
18046 && !row->ends_at_zv_p;
18047 row++)
18049 w->cursor.vpos++;
18050 w->cursor.y = row->y;
18052 if (row < bottom_row)
18054 /* Can't simply scan the row for point with
18055 bidi-reordered glyph rows. Let set_cursor_from_row
18056 figure out where to put the cursor, and if it fails,
18057 give up. */
18058 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18060 if (!set_cursor_from_row (w, row, w->current_matrix,
18061 0, 0, 0, 0))
18063 clear_glyph_matrix (w->desired_matrix);
18064 return false;
18067 else
18069 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18070 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18072 for (; glyph < end
18073 && (!BUFFERP (glyph->object)
18074 || glyph->charpos < PT);
18075 glyph++)
18077 w->cursor.hpos++;
18078 w->cursor.x += glyph->pixel_width;
18084 /* Adjust window end. A null value of last_text_row means that
18085 the window end is in reused rows which in turn means that
18086 only its vpos can have changed. */
18087 if (last_text_row)
18088 adjust_window_ends (w, last_text_row, false);
18089 else
18090 w->window_end_vpos -= nrows_scrolled;
18092 w->window_end_valid = false;
18093 w->desired_matrix->no_scrolling_p = true;
18095 #ifdef GLYPH_DEBUG
18096 debug_method_add (w, "try_window_reusing_current_matrix 2");
18097 #endif
18098 return true;
18101 return false;
18106 /************************************************************************
18107 Window redisplay reusing current matrix when buffer has changed
18108 ************************************************************************/
18110 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18111 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18112 ptrdiff_t *, ptrdiff_t *);
18113 static struct glyph_row *
18114 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18115 struct glyph_row *);
18118 /* Return the last row in MATRIX displaying text. If row START is
18119 non-null, start searching with that row. IT gives the dimensions
18120 of the display. Value is null if matrix is empty; otherwise it is
18121 a pointer to the row found. */
18123 static struct glyph_row *
18124 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18125 struct glyph_row *start)
18127 struct glyph_row *row, *row_found;
18129 /* Set row_found to the last row in IT->w's current matrix
18130 displaying text. The loop looks funny but think of partially
18131 visible lines. */
18132 row_found = NULL;
18133 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18134 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18136 eassert (row->enabled_p);
18137 row_found = row;
18138 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18139 break;
18140 ++row;
18143 return row_found;
18147 /* Return the last row in the current matrix of W that is not affected
18148 by changes at the start of current_buffer that occurred since W's
18149 current matrix was built. Value is null if no such row exists.
18151 BEG_UNCHANGED us the number of characters unchanged at the start of
18152 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18153 first changed character in current_buffer. Characters at positions <
18154 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18155 when the current matrix was built. */
18157 static struct glyph_row *
18158 find_last_unchanged_at_beg_row (struct window *w)
18160 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18161 struct glyph_row *row;
18162 struct glyph_row *row_found = NULL;
18163 int yb = window_text_bottom_y (w);
18165 /* Find the last row displaying unchanged text. */
18166 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18167 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18168 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18169 ++row)
18171 if (/* If row ends before first_changed_pos, it is unchanged,
18172 except in some case. */
18173 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18174 /* When row ends in ZV and we write at ZV it is not
18175 unchanged. */
18176 && !row->ends_at_zv_p
18177 /* When first_changed_pos is the end of a continued line,
18178 row is not unchanged because it may be no longer
18179 continued. */
18180 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18181 && (row->continued_p
18182 || row->exact_window_width_line_p))
18183 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18184 needs to be recomputed, so don't consider this row as
18185 unchanged. This happens when the last line was
18186 bidi-reordered and was killed immediately before this
18187 redisplay cycle. In that case, ROW->end stores the
18188 buffer position of the first visual-order character of
18189 the killed text, which is now beyond ZV. */
18190 && CHARPOS (row->end.pos) <= ZV)
18191 row_found = row;
18193 /* Stop if last visible row. */
18194 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18195 break;
18198 return row_found;
18202 /* Find the first glyph row in the current matrix of W that is not
18203 affected by changes at the end of current_buffer since the
18204 time W's current matrix was built.
18206 Return in *DELTA the number of chars by which buffer positions in
18207 unchanged text at the end of current_buffer must be adjusted.
18209 Return in *DELTA_BYTES the corresponding number of bytes.
18211 Value is null if no such row exists, i.e. all rows are affected by
18212 changes. */
18214 static struct glyph_row *
18215 find_first_unchanged_at_end_row (struct window *w,
18216 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18218 struct glyph_row *row;
18219 struct glyph_row *row_found = NULL;
18221 *delta = *delta_bytes = 0;
18223 /* Display must not have been paused, otherwise the current matrix
18224 is not up to date. */
18225 eassert (w->window_end_valid);
18227 /* A value of window_end_pos >= END_UNCHANGED means that the window
18228 end is in the range of changed text. If so, there is no
18229 unchanged row at the end of W's current matrix. */
18230 if (w->window_end_pos >= END_UNCHANGED)
18231 return NULL;
18233 /* Set row to the last row in W's current matrix displaying text. */
18234 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18236 /* If matrix is entirely empty, no unchanged row exists. */
18237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18239 /* The value of row is the last glyph row in the matrix having a
18240 meaningful buffer position in it. The end position of row
18241 corresponds to window_end_pos. This allows us to translate
18242 buffer positions in the current matrix to current buffer
18243 positions for characters not in changed text. */
18244 ptrdiff_t Z_old =
18245 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18246 ptrdiff_t Z_BYTE_old =
18247 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18248 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18249 struct glyph_row *first_text_row
18250 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18252 *delta = Z - Z_old;
18253 *delta_bytes = Z_BYTE - Z_BYTE_old;
18255 /* Set last_unchanged_pos to the buffer position of the last
18256 character in the buffer that has not been changed. Z is the
18257 index + 1 of the last character in current_buffer, i.e. by
18258 subtracting END_UNCHANGED we get the index of the last
18259 unchanged character, and we have to add BEG to get its buffer
18260 position. */
18261 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18262 last_unchanged_pos_old = last_unchanged_pos - *delta;
18264 /* Search backward from ROW for a row displaying a line that
18265 starts at a minimum position >= last_unchanged_pos_old. */
18266 for (; row > first_text_row; --row)
18268 /* This used to abort, but it can happen.
18269 It is ok to just stop the search instead here. KFS. */
18270 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18271 break;
18273 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18274 row_found = row;
18278 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18280 return row_found;
18284 /* Make sure that glyph rows in the current matrix of window W
18285 reference the same glyph memory as corresponding rows in the
18286 frame's frame matrix. This function is called after scrolling W's
18287 current matrix on a terminal frame in try_window_id and
18288 try_window_reusing_current_matrix. */
18290 static void
18291 sync_frame_with_window_matrix_rows (struct window *w)
18293 struct frame *f = XFRAME (w->frame);
18294 struct glyph_row *window_row, *window_row_end, *frame_row;
18296 /* Preconditions: W must be a leaf window and full-width. Its frame
18297 must have a frame matrix. */
18298 eassert (BUFFERP (w->contents));
18299 eassert (WINDOW_FULL_WIDTH_P (w));
18300 eassert (!FRAME_WINDOW_P (f));
18302 /* If W is a full-width window, glyph pointers in W's current matrix
18303 have, by definition, to be the same as glyph pointers in the
18304 corresponding frame matrix. Note that frame matrices have no
18305 marginal areas (see build_frame_matrix). */
18306 window_row = w->current_matrix->rows;
18307 window_row_end = window_row + w->current_matrix->nrows;
18308 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18309 while (window_row < window_row_end)
18311 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18312 struct glyph *end = window_row->glyphs[LAST_AREA];
18314 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18315 frame_row->glyphs[TEXT_AREA] = start;
18316 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18317 frame_row->glyphs[LAST_AREA] = end;
18319 /* Disable frame rows whose corresponding window rows have
18320 been disabled in try_window_id. */
18321 if (!window_row->enabled_p)
18322 frame_row->enabled_p = false;
18324 ++window_row, ++frame_row;
18329 /* Find the glyph row in window W containing CHARPOS. Consider all
18330 rows between START and END (not inclusive). END null means search
18331 all rows to the end of the display area of W. Value is the row
18332 containing CHARPOS or null. */
18334 struct glyph_row *
18335 row_containing_pos (struct window *w, ptrdiff_t charpos,
18336 struct glyph_row *start, struct glyph_row *end, int dy)
18338 struct glyph_row *row = start;
18339 struct glyph_row *best_row = NULL;
18340 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18341 int last_y;
18343 /* If we happen to start on a header-line, skip that. */
18344 if (row->mode_line_p)
18345 ++row;
18347 if ((end && row >= end) || !row->enabled_p)
18348 return NULL;
18350 last_y = window_text_bottom_y (w) - dy;
18352 while (true)
18354 /* Give up if we have gone too far. */
18355 if ((end && row >= end) || !row->enabled_p)
18356 return NULL;
18357 /* This formerly returned if they were equal.
18358 I think that both quantities are of a "last plus one" type;
18359 if so, when they are equal, the row is within the screen. -- rms. */
18360 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18361 return NULL;
18363 /* If it is in this row, return this row. */
18364 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18365 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18366 /* The end position of a row equals the start
18367 position of the next row. If CHARPOS is there, we
18368 would rather consider it displayed in the next
18369 line, except when this line ends in ZV. */
18370 && !row_for_charpos_p (row, charpos)))
18371 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18373 struct glyph *g;
18375 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18376 || (!best_row && !row->continued_p))
18377 return row;
18378 /* In bidi-reordered rows, there could be several rows whose
18379 edges surround CHARPOS, all of these rows belonging to
18380 the same continued line. We need to find the row which
18381 fits CHARPOS the best. */
18382 for (g = row->glyphs[TEXT_AREA];
18383 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18384 g++)
18386 if (!STRINGP (g->object))
18388 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18390 mindif = eabs (g->charpos - charpos);
18391 best_row = row;
18392 /* Exact match always wins. */
18393 if (mindif == 0)
18394 return best_row;
18399 else if (best_row && !row->continued_p)
18400 return best_row;
18401 ++row;
18406 /* Try to redisplay window W by reusing its existing display. W's
18407 current matrix must be up to date when this function is called,
18408 i.e., window_end_valid must be true.
18410 Value is
18412 >= 1 if successful, i.e. display has been updated
18413 specifically:
18414 1 means the changes were in front of a newline that precedes
18415 the window start, and the whole current matrix was reused
18416 2 means the changes were after the last position displayed
18417 in the window, and the whole current matrix was reused
18418 3 means portions of the current matrix were reused, while
18419 some of the screen lines were redrawn
18420 -1 if redisplay with same window start is known not to succeed
18421 0 if otherwise unsuccessful
18423 The following steps are performed:
18425 1. Find the last row in the current matrix of W that is not
18426 affected by changes at the start of current_buffer. If no such row
18427 is found, give up.
18429 2. Find the first row in W's current matrix that is not affected by
18430 changes at the end of current_buffer. Maybe there is no such row.
18432 3. Display lines beginning with the row + 1 found in step 1 to the
18433 row found in step 2 or, if step 2 didn't find a row, to the end of
18434 the window.
18436 4. If cursor is not known to appear on the window, give up.
18438 5. If display stopped at the row found in step 2, scroll the
18439 display and current matrix as needed.
18441 6. Maybe display some lines at the end of W, if we must. This can
18442 happen under various circumstances, like a partially visible line
18443 becoming fully visible, or because newly displayed lines are displayed
18444 in smaller font sizes.
18446 7. Update W's window end information. */
18448 static int
18449 try_window_id (struct window *w)
18451 struct frame *f = XFRAME (w->frame);
18452 struct glyph_matrix *current_matrix = w->current_matrix;
18453 struct glyph_matrix *desired_matrix = w->desired_matrix;
18454 struct glyph_row *last_unchanged_at_beg_row;
18455 struct glyph_row *first_unchanged_at_end_row;
18456 struct glyph_row *row;
18457 struct glyph_row *bottom_row;
18458 int bottom_vpos;
18459 struct it it;
18460 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18461 int dvpos, dy;
18462 struct text_pos start_pos;
18463 struct run run;
18464 int first_unchanged_at_end_vpos = 0;
18465 struct glyph_row *last_text_row, *last_text_row_at_end;
18466 struct text_pos start;
18467 ptrdiff_t first_changed_charpos, last_changed_charpos;
18469 #ifdef GLYPH_DEBUG
18470 if (inhibit_try_window_id)
18471 return 0;
18472 #endif
18474 /* This is handy for debugging. */
18475 #if false
18476 #define GIVE_UP(X) \
18477 do { \
18478 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18479 return 0; \
18480 } while (false)
18481 #else
18482 #define GIVE_UP(X) return 0
18483 #endif
18485 SET_TEXT_POS_FROM_MARKER (start, w->start);
18487 /* Don't use this for mini-windows because these can show
18488 messages and mini-buffers, and we don't handle that here. */
18489 if (MINI_WINDOW_P (w))
18490 GIVE_UP (1);
18492 /* This flag is used to prevent redisplay optimizations. */
18493 if (windows_or_buffers_changed || f->cursor_type_changed)
18494 GIVE_UP (2);
18496 /* This function's optimizations cannot be used if overlays have
18497 changed in the buffer displayed by the window, so give up if they
18498 have. */
18499 if (w->last_overlay_modified != OVERLAY_MODIFF)
18500 GIVE_UP (200);
18502 /* Verify that narrowing has not changed.
18503 Also verify that we were not told to prevent redisplay optimizations.
18504 It would be nice to further
18505 reduce the number of cases where this prevents try_window_id. */
18506 if (current_buffer->clip_changed
18507 || current_buffer->prevent_redisplay_optimizations_p)
18508 GIVE_UP (3);
18510 /* Window must either use window-based redisplay or be full width. */
18511 if (!FRAME_WINDOW_P (f)
18512 && (!FRAME_LINE_INS_DEL_OK (f)
18513 || !WINDOW_FULL_WIDTH_P (w)))
18514 GIVE_UP (4);
18516 /* Give up if point is known NOT to appear in W. */
18517 if (PT < CHARPOS (start))
18518 GIVE_UP (5);
18520 /* Another way to prevent redisplay optimizations. */
18521 if (w->last_modified == 0)
18522 GIVE_UP (6);
18524 /* Verify that window is not hscrolled. */
18525 if (w->hscroll != 0)
18526 GIVE_UP (7);
18528 /* Verify that display wasn't paused. */
18529 if (!w->window_end_valid)
18530 GIVE_UP (8);
18532 /* Likewise if highlighting trailing whitespace. */
18533 if (!NILP (Vshow_trailing_whitespace))
18534 GIVE_UP (11);
18536 /* Can't use this if overlay arrow position and/or string have
18537 changed. */
18538 if (overlay_arrows_changed_p (false))
18539 GIVE_UP (12);
18541 /* When word-wrap is on, adding a space to the first word of a
18542 wrapped line can change the wrap position, altering the line
18543 above it. It might be worthwhile to handle this more
18544 intelligently, but for now just redisplay from scratch. */
18545 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18546 GIVE_UP (21);
18548 /* Under bidi reordering, adding or deleting a character in the
18549 beginning of a paragraph, before the first strong directional
18550 character, can change the base direction of the paragraph (unless
18551 the buffer specifies a fixed paragraph direction), which will
18552 require redisplaying the whole paragraph. It might be worthwhile
18553 to find the paragraph limits and widen the range of redisplayed
18554 lines to that, but for now just give up this optimization and
18555 redisplay from scratch. */
18556 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18557 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18558 GIVE_UP (22);
18560 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18561 to that variable require thorough redisplay. */
18562 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18563 GIVE_UP (23);
18565 /* Give up if display-line-numbers is in relative mode, or when the
18566 current line's number needs to be displayed in a distinct face. */
18567 if (EQ (Vdisplay_line_numbers, Qrelative)
18568 || EQ (Vdisplay_line_numbers, Qvisual)
18569 || (!NILP (Vdisplay_line_numbers)
18570 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18571 Qline_number_current_line,
18572 w->frame))))
18573 GIVE_UP (24);
18575 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18576 only if buffer has really changed. The reason is that the gap is
18577 initially at Z for freshly visited files. The code below would
18578 set end_unchanged to 0 in that case. */
18579 if (MODIFF > SAVE_MODIFF
18580 /* This seems to happen sometimes after saving a buffer. */
18581 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18583 if (GPT - BEG < BEG_UNCHANGED)
18584 BEG_UNCHANGED = GPT - BEG;
18585 if (Z - GPT < END_UNCHANGED)
18586 END_UNCHANGED = Z - GPT;
18589 /* The position of the first and last character that has been changed. */
18590 first_changed_charpos = BEG + BEG_UNCHANGED;
18591 last_changed_charpos = Z - END_UNCHANGED;
18593 /* If window starts after a line end, and the last change is in
18594 front of that newline, then changes don't affect the display.
18595 This case happens with stealth-fontification. Note that although
18596 the display is unchanged, glyph positions in the matrix have to
18597 be adjusted, of course. */
18598 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18599 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18600 && ((last_changed_charpos < CHARPOS (start)
18601 && CHARPOS (start) == BEGV)
18602 || (last_changed_charpos < CHARPOS (start) - 1
18603 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18605 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18606 struct glyph_row *r0;
18608 /* Compute how many chars/bytes have been added to or removed
18609 from the buffer. */
18610 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18611 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18612 Z_delta = Z - Z_old;
18613 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18615 /* Give up if PT is not in the window. Note that it already has
18616 been checked at the start of try_window_id that PT is not in
18617 front of the window start. */
18618 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18619 GIVE_UP (13);
18621 /* If window start is unchanged, we can reuse the whole matrix
18622 as is, after adjusting glyph positions. No need to compute
18623 the window end again, since its offset from Z hasn't changed. */
18624 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18625 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18626 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18627 /* PT must not be in a partially visible line. */
18628 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18629 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18631 /* Adjust positions in the glyph matrix. */
18632 if (Z_delta || Z_delta_bytes)
18634 struct glyph_row *r1
18635 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18636 increment_matrix_positions (w->current_matrix,
18637 MATRIX_ROW_VPOS (r0, current_matrix),
18638 MATRIX_ROW_VPOS (r1, current_matrix),
18639 Z_delta, Z_delta_bytes);
18642 /* Set the cursor. */
18643 row = row_containing_pos (w, PT, r0, NULL, 0);
18644 if (row)
18645 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18646 return 1;
18650 /* Handle the case that changes are all below what is displayed in
18651 the window, and that PT is in the window. This shortcut cannot
18652 be taken if ZV is visible in the window, and text has been added
18653 there that is visible in the window. */
18654 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18655 /* ZV is not visible in the window, or there are no
18656 changes at ZV, actually. */
18657 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18658 || first_changed_charpos == last_changed_charpos))
18660 struct glyph_row *r0;
18662 /* Give up if PT is not in the window. Note that it already has
18663 been checked at the start of try_window_id that PT is not in
18664 front of the window start. */
18665 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18666 GIVE_UP (14);
18668 /* If window start is unchanged, we can reuse the whole matrix
18669 as is, without changing glyph positions since no text has
18670 been added/removed in front of the window end. */
18671 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18672 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18673 /* PT must not be in a partially visible line. */
18674 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18675 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18677 /* We have to compute the window end anew since text
18678 could have been added/removed after it. */
18679 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18680 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18682 /* Set the cursor. */
18683 row = row_containing_pos (w, PT, r0, NULL, 0);
18684 if (row)
18685 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18686 return 2;
18690 /* Give up if window start is in the changed area.
18692 The condition used to read
18694 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18696 but why that was tested escapes me at the moment. */
18697 if (CHARPOS (start) >= first_changed_charpos
18698 && CHARPOS (start) <= last_changed_charpos)
18699 GIVE_UP (15);
18701 /* Check that window start agrees with the start of the first glyph
18702 row in its current matrix. Check this after we know the window
18703 start is not in changed text, otherwise positions would not be
18704 comparable. */
18705 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18706 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18707 GIVE_UP (16);
18709 /* Give up if the window ends in strings. Overlay strings
18710 at the end are difficult to handle, so don't try. */
18711 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18712 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18713 GIVE_UP (20);
18715 /* Compute the position at which we have to start displaying new
18716 lines. Some of the lines at the top of the window might be
18717 reusable because they are not displaying changed text. Find the
18718 last row in W's current matrix not affected by changes at the
18719 start of current_buffer. Value is null if changes start in the
18720 first line of window. */
18721 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18722 if (last_unchanged_at_beg_row)
18724 /* Avoid starting to display in the middle of a character, a TAB
18725 for instance. This is easier than to set up the iterator
18726 exactly, and it's not a frequent case, so the additional
18727 effort wouldn't really pay off. */
18728 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18729 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18730 && last_unchanged_at_beg_row > w->current_matrix->rows)
18731 --last_unchanged_at_beg_row;
18733 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18734 GIVE_UP (17);
18736 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18737 GIVE_UP (18);
18738 start_pos = it.current.pos;
18740 /* Start displaying new lines in the desired matrix at the same
18741 vpos we would use in the current matrix, i.e. below
18742 last_unchanged_at_beg_row. */
18743 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18744 current_matrix);
18745 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18746 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18748 eassert (it.hpos == 0 && it.current_x == 0);
18750 else
18752 /* There are no reusable lines at the start of the window.
18753 Start displaying in the first text line. */
18754 start_display (&it, w, start);
18755 it.vpos = it.first_vpos;
18756 start_pos = it.current.pos;
18759 /* Find the first row that is not affected by changes at the end of
18760 the buffer. Value will be null if there is no unchanged row, in
18761 which case we must redisplay to the end of the window. delta
18762 will be set to the value by which buffer positions beginning with
18763 first_unchanged_at_end_row have to be adjusted due to text
18764 changes. */
18765 first_unchanged_at_end_row
18766 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18767 IF_DEBUG (debug_delta = delta);
18768 IF_DEBUG (debug_delta_bytes = delta_bytes);
18770 /* Set stop_pos to the buffer position up to which we will have to
18771 display new lines. If first_unchanged_at_end_row != NULL, this
18772 is the buffer position of the start of the line displayed in that
18773 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18774 that we don't stop at a buffer position. */
18775 stop_pos = 0;
18776 if (first_unchanged_at_end_row)
18778 eassert (last_unchanged_at_beg_row == NULL
18779 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18781 /* If this is a continuation line, move forward to the next one
18782 that isn't. Changes in lines above affect this line.
18783 Caution: this may move first_unchanged_at_end_row to a row
18784 not displaying text. */
18785 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18786 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18787 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18788 < it.last_visible_y))
18789 ++first_unchanged_at_end_row;
18791 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18792 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18793 >= it.last_visible_y))
18794 first_unchanged_at_end_row = NULL;
18795 else
18797 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18798 + delta);
18799 first_unchanged_at_end_vpos
18800 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18801 eassert (stop_pos >= Z - END_UNCHANGED);
18804 else if (last_unchanged_at_beg_row == NULL)
18805 GIVE_UP (19);
18808 #ifdef GLYPH_DEBUG
18810 /* Either there is no unchanged row at the end, or the one we have
18811 now displays text. This is a necessary condition for the window
18812 end pos calculation at the end of this function. */
18813 eassert (first_unchanged_at_end_row == NULL
18814 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18816 debug_last_unchanged_at_beg_vpos
18817 = (last_unchanged_at_beg_row
18818 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18819 : -1);
18820 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18822 #endif /* GLYPH_DEBUG */
18825 /* Display new lines. Set last_text_row to the last new line
18826 displayed which has text on it, i.e. might end up as being the
18827 line where the window_end_vpos is. */
18828 w->cursor.vpos = -1;
18829 last_text_row = NULL;
18830 overlay_arrow_seen = false;
18831 if (it.current_y < it.last_visible_y
18832 && !f->fonts_changed
18833 && (first_unchanged_at_end_row == NULL
18834 || IT_CHARPOS (it) < stop_pos))
18835 it.glyph_row->reversed_p = false;
18836 while (it.current_y < it.last_visible_y
18837 && !f->fonts_changed
18838 && (first_unchanged_at_end_row == NULL
18839 || IT_CHARPOS (it) < stop_pos))
18841 if (display_line (&it, -1))
18842 last_text_row = it.glyph_row - 1;
18845 if (f->fonts_changed)
18846 return -1;
18848 /* The redisplay iterations in display_line above could have
18849 triggered font-lock, which could have done something that
18850 invalidates IT->w window's end-point information, on which we
18851 rely below. E.g., one package, which will remain unnamed, used
18852 to install a font-lock-fontify-region-function that called
18853 bury-buffer, whose side effect is to switch the buffer displayed
18854 by IT->w, and that predictably resets IT->w's window_end_valid
18855 flag, which we already tested at the entry to this function.
18856 Amply punish such packages/modes by giving up on this
18857 optimization in those cases. */
18858 if (!w->window_end_valid)
18860 clear_glyph_matrix (w->desired_matrix);
18861 return -1;
18864 /* Compute differences in buffer positions, y-positions etc. for
18865 lines reused at the bottom of the window. Compute what we can
18866 scroll. */
18867 if (first_unchanged_at_end_row
18868 /* No lines reused because we displayed everything up to the
18869 bottom of the window. */
18870 && it.current_y < it.last_visible_y)
18872 dvpos = (it.vpos
18873 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18874 current_matrix));
18875 dy = it.current_y - first_unchanged_at_end_row->y;
18876 run.current_y = first_unchanged_at_end_row->y;
18877 run.desired_y = run.current_y + dy;
18878 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18880 else
18882 delta = delta_bytes = dvpos = dy
18883 = run.current_y = run.desired_y = run.height = 0;
18884 first_unchanged_at_end_row = NULL;
18886 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18889 /* Find the cursor if not already found. We have to decide whether
18890 PT will appear on this window (it sometimes doesn't, but this is
18891 not a very frequent case.) This decision has to be made before
18892 the current matrix is altered. A value of cursor.vpos < 0 means
18893 that PT is either in one of the lines beginning at
18894 first_unchanged_at_end_row or below the window. Don't care for
18895 lines that might be displayed later at the window end; as
18896 mentioned, this is not a frequent case. */
18897 if (w->cursor.vpos < 0)
18899 /* Cursor in unchanged rows at the top? */
18900 if (PT < CHARPOS (start_pos)
18901 && last_unchanged_at_beg_row)
18903 row = row_containing_pos (w, PT,
18904 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18905 last_unchanged_at_beg_row + 1, 0);
18906 if (row)
18907 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18910 /* Start from first_unchanged_at_end_row looking for PT. */
18911 else if (first_unchanged_at_end_row)
18913 row = row_containing_pos (w, PT - delta,
18914 first_unchanged_at_end_row, NULL, 0);
18915 if (row)
18916 set_cursor_from_row (w, row, w->current_matrix, delta,
18917 delta_bytes, dy, dvpos);
18920 /* Give up if cursor was not found. */
18921 if (w->cursor.vpos < 0)
18923 clear_glyph_matrix (w->desired_matrix);
18924 return -1;
18928 /* Don't let the cursor end in the scroll margins. */
18930 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18931 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18933 if ((w->cursor.y < this_scroll_margin
18934 && CHARPOS (start) > BEGV)
18935 /* Old redisplay didn't take scroll margin into account at the bottom,
18936 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18937 || (w->cursor.y + (make_cursor_line_fully_visible_p
18938 ? cursor_height + this_scroll_margin
18939 : 1)) > it.last_visible_y)
18941 w->cursor.vpos = -1;
18942 clear_glyph_matrix (w->desired_matrix);
18943 return -1;
18947 /* Scroll the display. Do it before changing the current matrix so
18948 that xterm.c doesn't get confused about where the cursor glyph is
18949 found. */
18950 if (dy && run.height)
18952 update_begin (f);
18954 if (FRAME_WINDOW_P (f))
18956 FRAME_RIF (f)->update_window_begin_hook (w);
18957 FRAME_RIF (f)->clear_window_mouse_face (w);
18958 FRAME_RIF (f)->scroll_run_hook (w, &run);
18959 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18961 else
18963 /* Terminal frame. In this case, dvpos gives the number of
18964 lines to scroll by; dvpos < 0 means scroll up. */
18965 int from_vpos
18966 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18967 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18968 int end = (WINDOW_TOP_EDGE_LINE (w)
18969 + window_wants_header_line (w)
18970 + window_internal_height (w));
18972 #if defined (HAVE_GPM) || defined (MSDOS)
18973 x_clear_window_mouse_face (w);
18974 #endif
18975 /* Perform the operation on the screen. */
18976 if (dvpos > 0)
18978 /* Scroll last_unchanged_at_beg_row to the end of the
18979 window down dvpos lines. */
18980 set_terminal_window (f, end);
18982 /* On dumb terminals delete dvpos lines at the end
18983 before inserting dvpos empty lines. */
18984 if (!FRAME_SCROLL_REGION_OK (f))
18985 ins_del_lines (f, end - dvpos, -dvpos);
18987 /* Insert dvpos empty lines in front of
18988 last_unchanged_at_beg_row. */
18989 ins_del_lines (f, from, dvpos);
18991 else if (dvpos < 0)
18993 /* Scroll up last_unchanged_at_beg_vpos to the end of
18994 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18995 set_terminal_window (f, end);
18997 /* Delete dvpos lines in front of
18998 last_unchanged_at_beg_vpos. ins_del_lines will set
18999 the cursor to the given vpos and emit |dvpos| delete
19000 line sequences. */
19001 ins_del_lines (f, from + dvpos, dvpos);
19003 /* On a dumb terminal insert dvpos empty lines at the
19004 end. */
19005 if (!FRAME_SCROLL_REGION_OK (f))
19006 ins_del_lines (f, end + dvpos, -dvpos);
19009 set_terminal_window (f, 0);
19012 update_end (f);
19015 /* Shift reused rows of the current matrix to the right position.
19016 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19017 text. */
19018 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19019 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19020 if (dvpos < 0)
19022 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19023 bottom_vpos, dvpos);
19024 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19025 bottom_vpos);
19027 else if (dvpos > 0)
19029 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19030 bottom_vpos, dvpos);
19031 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19032 first_unchanged_at_end_vpos + dvpos);
19035 /* For frame-based redisplay, make sure that current frame and window
19036 matrix are in sync with respect to glyph memory. */
19037 if (!FRAME_WINDOW_P (f))
19038 sync_frame_with_window_matrix_rows (w);
19040 /* Adjust buffer positions in reused rows. */
19041 if (delta || delta_bytes)
19042 increment_matrix_positions (current_matrix,
19043 first_unchanged_at_end_vpos + dvpos,
19044 bottom_vpos, delta, delta_bytes);
19046 /* Adjust Y positions. */
19047 if (dy)
19048 shift_glyph_matrix (w, current_matrix,
19049 first_unchanged_at_end_vpos + dvpos,
19050 bottom_vpos, dy);
19052 if (first_unchanged_at_end_row)
19054 first_unchanged_at_end_row += dvpos;
19055 if (first_unchanged_at_end_row->y >= it.last_visible_y
19056 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19057 first_unchanged_at_end_row = NULL;
19060 /* If scrolling up, there may be some lines to display at the end of
19061 the window. */
19062 last_text_row_at_end = NULL;
19063 if (dy < 0)
19065 /* Scrolling up can leave for example a partially visible line
19066 at the end of the window to be redisplayed. */
19067 /* Set last_row to the glyph row in the current matrix where the
19068 window end line is found. It has been moved up or down in
19069 the matrix by dvpos. */
19070 int last_vpos = w->window_end_vpos + dvpos;
19071 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19073 /* If last_row is the window end line, it should display text. */
19074 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19076 /* If window end line was partially visible before, begin
19077 displaying at that line. Otherwise begin displaying with the
19078 line following it. */
19079 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19081 init_to_row_start (&it, w, last_row);
19082 it.vpos = last_vpos;
19083 it.current_y = last_row->y;
19085 else
19087 init_to_row_end (&it, w, last_row);
19088 it.vpos = 1 + last_vpos;
19089 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19090 ++last_row;
19093 /* We may start in a continuation line. If so, we have to
19094 get the right continuation_lines_width and current_x. */
19095 it.continuation_lines_width = last_row->continuation_lines_width;
19096 it.hpos = it.current_x = 0;
19098 /* Display the rest of the lines at the window end. */
19099 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19100 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19102 /* Is it always sure that the display agrees with lines in
19103 the current matrix? I don't think so, so we mark rows
19104 displayed invalid in the current matrix by setting their
19105 enabled_p flag to false. */
19106 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19107 if (display_line (&it, w->cursor.vpos))
19108 last_text_row_at_end = it.glyph_row - 1;
19112 /* Update window_end_pos and window_end_vpos. */
19113 if (first_unchanged_at_end_row && !last_text_row_at_end)
19115 /* Window end line if one of the preserved rows from the current
19116 matrix. Set row to the last row displaying text in current
19117 matrix starting at first_unchanged_at_end_row, after
19118 scrolling. */
19119 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19120 row = find_last_row_displaying_text (w->current_matrix, &it,
19121 first_unchanged_at_end_row);
19122 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19123 adjust_window_ends (w, row, true);
19124 eassert (w->window_end_bytepos >= 0);
19125 IF_DEBUG (debug_method_add (w, "A"));
19127 else if (last_text_row_at_end)
19129 adjust_window_ends (w, last_text_row_at_end, false);
19130 eassert (w->window_end_bytepos >= 0);
19131 IF_DEBUG (debug_method_add (w, "B"));
19133 else if (last_text_row)
19135 /* We have displayed either to the end of the window or at the
19136 end of the window, i.e. the last row with text is to be found
19137 in the desired matrix. */
19138 adjust_window_ends (w, last_text_row, false);
19139 eassert (w->window_end_bytepos >= 0);
19141 else if (first_unchanged_at_end_row == NULL
19142 && last_text_row == NULL
19143 && last_text_row_at_end == NULL)
19145 /* Displayed to end of window, but no line containing text was
19146 displayed. Lines were deleted at the end of the window. */
19147 bool first_vpos = window_wants_header_line (w);
19148 int vpos = w->window_end_vpos;
19149 struct glyph_row *current_row = current_matrix->rows + vpos;
19150 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19152 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19154 eassert (first_vpos <= vpos);
19155 if (desired_row->enabled_p)
19157 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19158 row = desired_row;
19160 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19161 row = current_row;
19164 w->window_end_vpos = vpos + 1;
19165 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19166 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19167 eassert (w->window_end_bytepos >= 0);
19168 IF_DEBUG (debug_method_add (w, "C"));
19170 else
19171 emacs_abort ();
19173 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19174 debug_end_vpos = w->window_end_vpos));
19176 /* Record that display has not been completed. */
19177 w->window_end_valid = false;
19178 w->desired_matrix->no_scrolling_p = true;
19179 return 3;
19181 #undef GIVE_UP
19186 /***********************************************************************
19187 More debugging support
19188 ***********************************************************************/
19190 #ifdef GLYPH_DEBUG
19192 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19193 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19194 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19197 /* Dump the contents of glyph matrix MATRIX on 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_matrix (struct glyph_matrix *matrix, int glyphs)
19206 int i;
19207 for (i = 0; i < matrix->nrows; ++i)
19208 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19212 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19213 the glyph row and area where the glyph comes from. */
19215 void
19216 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19218 if (glyph->type == CHAR_GLYPH
19219 || glyph->type == GLYPHLESS_GLYPH)
19221 fprintf (stderr,
19222 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19223 glyph - row->glyphs[TEXT_AREA],
19224 (glyph->type == CHAR_GLYPH
19225 ? 'C'
19226 : 'G'),
19227 glyph->charpos,
19228 (BUFFERP (glyph->object)
19229 ? 'B'
19230 : (STRINGP (glyph->object)
19231 ? 'S'
19232 : (NILP (glyph->object)
19233 ? '0'
19234 : '-'))),
19235 glyph->pixel_width,
19236 glyph->u.ch,
19237 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19238 ? (int) glyph->u.ch
19239 : '.'),
19240 glyph->face_id,
19241 glyph->left_box_line_p,
19242 glyph->right_box_line_p);
19244 else if (glyph->type == STRETCH_GLYPH)
19246 fprintf (stderr,
19247 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19248 glyph - row->glyphs[TEXT_AREA],
19249 'S',
19250 glyph->charpos,
19251 (BUFFERP (glyph->object)
19252 ? 'B'
19253 : (STRINGP (glyph->object)
19254 ? 'S'
19255 : (NILP (glyph->object)
19256 ? '0'
19257 : '-'))),
19258 glyph->pixel_width,
19260 ' ',
19261 glyph->face_id,
19262 glyph->left_box_line_p,
19263 glyph->right_box_line_p);
19265 else if (glyph->type == IMAGE_GLYPH)
19267 fprintf (stderr,
19268 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19269 glyph - row->glyphs[TEXT_AREA],
19270 'I',
19271 glyph->charpos,
19272 (BUFFERP (glyph->object)
19273 ? 'B'
19274 : (STRINGP (glyph->object)
19275 ? 'S'
19276 : (NILP (glyph->object)
19277 ? '0'
19278 : '-'))),
19279 glyph->pixel_width,
19280 (unsigned int) glyph->u.img_id,
19281 '.',
19282 glyph->face_id,
19283 glyph->left_box_line_p,
19284 glyph->right_box_line_p);
19286 else if (glyph->type == COMPOSITE_GLYPH)
19288 fprintf (stderr,
19289 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19290 glyph - row->glyphs[TEXT_AREA],
19291 '+',
19292 glyph->charpos,
19293 (BUFFERP (glyph->object)
19294 ? 'B'
19295 : (STRINGP (glyph->object)
19296 ? 'S'
19297 : (NILP (glyph->object)
19298 ? '0'
19299 : '-'))),
19300 glyph->pixel_width,
19301 (unsigned int) glyph->u.cmp.id);
19302 if (glyph->u.cmp.automatic)
19303 fprintf (stderr,
19304 "[%d-%d]",
19305 glyph->slice.cmp.from, glyph->slice.cmp.to);
19306 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19307 glyph->face_id,
19308 glyph->left_box_line_p,
19309 glyph->right_box_line_p);
19311 else if (glyph->type == XWIDGET_GLYPH)
19313 #ifndef HAVE_XWIDGETS
19314 eassume (false);
19315 #else
19316 fprintf (stderr,
19317 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19318 glyph - row->glyphs[TEXT_AREA],
19319 'X',
19320 glyph->charpos,
19321 (BUFFERP (glyph->object)
19322 ? 'B'
19323 : (STRINGP (glyph->object)
19324 ? 'S'
19325 : '-')),
19326 glyph->pixel_width,
19327 glyph->u.xwidget,
19328 '.',
19329 glyph->face_id,
19330 glyph->left_box_line_p,
19331 glyph->right_box_line_p);
19332 #endif
19337 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19338 GLYPHS 0 means don't show glyph contents.
19339 GLYPHS 1 means show glyphs in short form
19340 GLYPHS > 1 means show glyphs in long form. */
19342 void
19343 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19345 if (glyphs != 1)
19347 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19348 fprintf (stderr, "==============================================================================\n");
19350 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19351 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19352 vpos,
19353 MATRIX_ROW_START_CHARPOS (row),
19354 MATRIX_ROW_END_CHARPOS (row),
19355 row->used[TEXT_AREA],
19356 row->contains_overlapping_glyphs_p,
19357 row->enabled_p,
19358 row->truncated_on_left_p,
19359 row->truncated_on_right_p,
19360 row->continued_p,
19361 MATRIX_ROW_CONTINUATION_LINE_P (row),
19362 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19363 row->ends_at_zv_p,
19364 row->fill_line_p,
19365 row->ends_in_middle_of_char_p,
19366 row->starts_in_middle_of_char_p,
19367 row->mouse_face_p,
19368 row->x,
19369 row->y,
19370 row->pixel_width,
19371 row->height,
19372 row->visible_height,
19373 row->ascent,
19374 row->phys_ascent);
19375 /* The next 3 lines should align to "Start" in the header. */
19376 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19377 row->end.overlay_string_index,
19378 row->continuation_lines_width);
19379 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19380 CHARPOS (row->start.string_pos),
19381 CHARPOS (row->end.string_pos));
19382 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19383 row->end.dpvec_index);
19386 if (glyphs > 1)
19388 int area;
19390 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19392 struct glyph *glyph = row->glyphs[area];
19393 struct glyph *glyph_end = glyph + row->used[area];
19395 /* Glyph for a line end in text. */
19396 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19397 ++glyph_end;
19399 if (glyph < glyph_end)
19400 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19402 for (; glyph < glyph_end; ++glyph)
19403 dump_glyph (row, glyph, area);
19406 else if (glyphs == 1)
19408 int area;
19409 char s[SHRT_MAX + 4];
19411 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19413 int i;
19415 for (i = 0; i < row->used[area]; ++i)
19417 struct glyph *glyph = row->glyphs[area] + i;
19418 if (i == row->used[area] - 1
19419 && area == TEXT_AREA
19420 && NILP (glyph->object)
19421 && glyph->type == CHAR_GLYPH
19422 && glyph->u.ch == ' ')
19424 strcpy (&s[i], "[\\n]");
19425 i += 4;
19427 else if (glyph->type == CHAR_GLYPH
19428 && glyph->u.ch < 0x80
19429 && glyph->u.ch >= ' ')
19430 s[i] = glyph->u.ch;
19431 else
19432 s[i] = '.';
19435 s[i] = '\0';
19436 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19442 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19443 Sdump_glyph_matrix, 0, 1, "p",
19444 doc: /* Dump the current matrix of the selected window to stderr.
19445 Shows contents of glyph row structures. With non-nil
19446 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19447 glyphs in short form, otherwise show glyphs in long form.
19449 Interactively, no argument means show glyphs in short form;
19450 with numeric argument, its value is passed as the GLYPHS flag. */)
19451 (Lisp_Object glyphs)
19453 struct window *w = XWINDOW (selected_window);
19454 struct buffer *buffer = XBUFFER (w->contents);
19456 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19457 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19458 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19459 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19460 fprintf (stderr, "=============================================\n");
19461 dump_glyph_matrix (w->current_matrix,
19462 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19463 return Qnil;
19467 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19468 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19469 Only text-mode frames have frame glyph matrices. */)
19470 (void)
19472 struct frame *f = XFRAME (selected_frame);
19474 if (f->current_matrix)
19475 dump_glyph_matrix (f->current_matrix, 1);
19476 else
19477 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19478 return Qnil;
19482 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19483 doc: /* Dump glyph row ROW to stderr.
19484 Interactively, ROW is the prefix numeric argument and defaults to
19485 the row which displays point.
19486 Optional argument GLYPHS 0 means don't dump glyphs.
19487 GLYPHS 1 means dump glyphs in short form.
19488 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19489 (Lisp_Object row, Lisp_Object glyphs)
19491 struct glyph_matrix *matrix;
19492 EMACS_INT vpos;
19494 if (NILP (row))
19496 int d1, d2, d3, d4, d5, ypos;
19497 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19498 &d1, &d2, &d3, &d4, &d5, &ypos);
19499 if (visible_p)
19500 vpos = ypos;
19501 else
19502 vpos = 0;
19504 else
19506 CHECK_NUMBER (row);
19507 vpos = XINT (row);
19509 matrix = XWINDOW (selected_window)->current_matrix;
19510 if (vpos >= 0 && vpos < matrix->nrows)
19511 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19512 vpos,
19513 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19514 return Qnil;
19518 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19519 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19520 Interactively, ROW is the prefix numeric argument and defaults to zero.
19521 GLYPHS 0 means don't dump glyphs.
19522 GLYPHS 1 means dump glyphs in short form.
19523 GLYPHS > 1 or omitted means dump glyphs in long form.
19525 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19526 do nothing. */)
19527 (Lisp_Object row, Lisp_Object glyphs)
19529 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19530 struct frame *sf = SELECTED_FRAME ();
19531 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19532 EMACS_INT vpos;
19534 if (NILP (row))
19535 vpos = 0;
19536 else
19538 CHECK_NUMBER (row);
19539 vpos = XINT (row);
19541 if (vpos >= 0 && vpos < m->nrows)
19542 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19543 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19544 #endif
19545 return Qnil;
19549 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19550 doc: /* Toggle tracing of redisplay.
19551 With ARG, turn tracing on if and only if ARG is positive. */)
19552 (Lisp_Object arg)
19554 if (NILP (arg))
19555 trace_redisplay_p = !trace_redisplay_p;
19556 else
19558 arg = Fprefix_numeric_value (arg);
19559 trace_redisplay_p = XINT (arg) > 0;
19562 return Qnil;
19566 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19567 doc: /* Like `format', but print result to stderr.
19568 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19569 (ptrdiff_t nargs, Lisp_Object *args)
19571 Lisp_Object s = Fformat (nargs, args);
19572 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19573 return Qnil;
19576 #endif /* GLYPH_DEBUG */
19580 /***********************************************************************
19581 Building Desired Matrix Rows
19582 ***********************************************************************/
19584 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19585 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19587 static struct glyph_row *
19588 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19590 struct frame *f = XFRAME (WINDOW_FRAME (w));
19591 struct buffer *buffer = XBUFFER (w->contents);
19592 struct buffer *old = current_buffer;
19593 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19594 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19595 const unsigned char *arrow_end = arrow_string + arrow_len;
19596 const unsigned char *p;
19597 struct it it;
19598 bool multibyte_p;
19599 int n_glyphs_before;
19601 set_buffer_temp (buffer);
19602 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19603 scratch_glyph_row.reversed_p = false;
19604 it.glyph_row->used[TEXT_AREA] = 0;
19605 SET_TEXT_POS (it.position, 0, 0);
19607 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19608 p = arrow_string;
19609 while (p < arrow_end)
19611 Lisp_Object face, ilisp;
19613 /* Get the next character. */
19614 if (multibyte_p)
19615 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19616 else
19618 it.c = it.char_to_display = *p, it.len = 1;
19619 if (! ASCII_CHAR_P (it.c))
19620 it.char_to_display = BYTE8_TO_CHAR (it.c);
19622 p += it.len;
19624 /* Get its face. */
19625 ilisp = make_number (p - arrow_string);
19626 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19627 it.face_id = compute_char_face (f, it.char_to_display, face);
19629 /* Compute its width, get its glyphs. */
19630 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19631 SET_TEXT_POS (it.position, -1, -1);
19632 PRODUCE_GLYPHS (&it);
19634 /* If this character doesn't fit any more in the line, we have
19635 to remove some glyphs. */
19636 if (it.current_x > it.last_visible_x)
19638 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19639 break;
19643 set_buffer_temp (old);
19644 return it.glyph_row;
19648 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19649 glyphs to insert is determined by produce_special_glyphs. */
19651 static void
19652 insert_left_trunc_glyphs (struct it *it)
19654 struct it truncate_it;
19655 struct glyph *from, *end, *to, *toend;
19657 eassert (!FRAME_WINDOW_P (it->f)
19658 || (!it->glyph_row->reversed_p
19659 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19660 || (it->glyph_row->reversed_p
19661 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19663 /* Get the truncation glyphs. */
19664 truncate_it = *it;
19665 truncate_it.current_x = 0;
19666 truncate_it.face_id = DEFAULT_FACE_ID;
19667 truncate_it.glyph_row = &scratch_glyph_row;
19668 truncate_it.area = TEXT_AREA;
19669 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19670 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19671 truncate_it.object = Qnil;
19672 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19674 /* Overwrite glyphs from IT with truncation glyphs. */
19675 if (!it->glyph_row->reversed_p)
19677 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19679 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19680 end = from + tused;
19681 to = it->glyph_row->glyphs[TEXT_AREA];
19682 toend = to + it->glyph_row->used[TEXT_AREA];
19683 if (FRAME_WINDOW_P (it->f))
19685 /* On GUI frames, when variable-size fonts are displayed,
19686 the truncation glyphs may need more pixels than the row's
19687 glyphs they overwrite. We overwrite more glyphs to free
19688 enough screen real estate, and enlarge the stretch glyph
19689 on the right (see display_line), if there is one, to
19690 preserve the screen position of the truncation glyphs on
19691 the right. */
19692 int w = 0;
19693 struct glyph *g = to;
19694 short used;
19696 /* The first glyph could be partially visible, in which case
19697 it->glyph_row->x will be negative. But we want the left
19698 truncation glyphs to be aligned at the left margin of the
19699 window, so we override the x coordinate at which the row
19700 will begin. */
19701 it->glyph_row->x = 0;
19702 while (g < toend && w < it->truncation_pixel_width)
19704 w += g->pixel_width;
19705 ++g;
19707 if (g - to - tused > 0)
19709 memmove (to + tused, g, (toend - g) * sizeof(*g));
19710 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19712 used = it->glyph_row->used[TEXT_AREA];
19713 if (it->glyph_row->truncated_on_right_p
19714 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19715 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19716 == STRETCH_GLYPH)
19718 int extra = w - it->truncation_pixel_width;
19720 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19724 while (from < end)
19725 *to++ = *from++;
19727 /* There may be padding glyphs left over. Overwrite them too. */
19728 if (!FRAME_WINDOW_P (it->f))
19730 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19732 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19733 while (from < end)
19734 *to++ = *from++;
19738 if (to > toend)
19739 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19741 else
19743 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19745 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19746 that back to front. */
19747 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19748 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19749 toend = it->glyph_row->glyphs[TEXT_AREA];
19750 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19751 if (FRAME_WINDOW_P (it->f))
19753 int w = 0;
19754 struct glyph *g = to;
19756 while (g >= toend && w < it->truncation_pixel_width)
19758 w += g->pixel_width;
19759 --g;
19761 if (to - g - tused > 0)
19762 to = g + tused;
19763 if (it->glyph_row->truncated_on_right_p
19764 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19765 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19767 int extra = w - it->truncation_pixel_width;
19769 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19773 while (from >= end && to >= toend)
19774 *to-- = *from--;
19775 if (!FRAME_WINDOW_P (it->f))
19777 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19779 from =
19780 truncate_it.glyph_row->glyphs[TEXT_AREA]
19781 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19782 while (from >= end && to >= toend)
19783 *to-- = *from--;
19786 if (from >= end)
19788 /* Need to free some room before prepending additional
19789 glyphs. */
19790 int move_by = from - end + 1;
19791 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19792 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19794 for ( ; g >= g0; g--)
19795 g[move_by] = *g;
19796 while (from >= end)
19797 *to-- = *from--;
19798 it->glyph_row->used[TEXT_AREA] += move_by;
19803 /* Compute the hash code for ROW. */
19804 unsigned
19805 row_hash (struct glyph_row *row)
19807 int area, k;
19808 unsigned hashval = 0;
19810 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19811 for (k = 0; k < row->used[area]; ++k)
19812 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19813 + row->glyphs[area][k].u.val
19814 + row->glyphs[area][k].face_id
19815 + row->glyphs[area][k].padding_p
19816 + (row->glyphs[area][k].type << 2));
19818 return hashval;
19821 /* Compute the pixel height and width of IT->glyph_row.
19823 Most of the time, ascent and height of a display line will be equal
19824 to the max_ascent and max_height values of the display iterator
19825 structure. This is not the case if
19827 1. We hit ZV without displaying anything. In this case, max_ascent
19828 and max_height will be zero.
19830 2. We have some glyphs that don't contribute to the line height.
19831 (The glyph row flag contributes_to_line_height_p is for future
19832 pixmap extensions).
19834 The first case is easily covered by using default values because in
19835 these cases, the line height does not really matter, except that it
19836 must not be zero. */
19838 static void
19839 compute_line_metrics (struct it *it)
19841 struct glyph_row *row = it->glyph_row;
19843 if (FRAME_WINDOW_P (it->f))
19845 int i, min_y, max_y;
19847 /* The line may consist of one space only, that was added to
19848 place the cursor on it. If so, the row's height hasn't been
19849 computed yet. */
19850 if (row->height == 0)
19852 if (it->max_ascent + it->max_descent == 0)
19853 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19854 row->ascent = it->max_ascent;
19855 row->height = it->max_ascent + it->max_descent;
19856 row->phys_ascent = it->max_phys_ascent;
19857 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19858 row->extra_line_spacing = it->max_extra_line_spacing;
19861 /* Compute the width of this line. */
19862 row->pixel_width = row->x;
19863 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19864 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19866 eassert (row->pixel_width >= 0);
19867 eassert (row->ascent >= 0 && row->height > 0);
19869 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19870 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19872 /* If first line's physical ascent is larger than its logical
19873 ascent, use the physical ascent, and make the row taller.
19874 This makes accented characters fully visible. */
19875 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19876 && row->phys_ascent > row->ascent)
19878 row->height += row->phys_ascent - row->ascent;
19879 row->ascent = row->phys_ascent;
19882 /* Compute how much of the line is visible. */
19883 row->visible_height = row->height;
19885 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19886 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19888 if (row->y < min_y)
19889 row->visible_height -= min_y - row->y;
19890 if (row->y + row->height > max_y)
19891 row->visible_height -= row->y + row->height - max_y;
19893 else
19895 row->pixel_width = row->used[TEXT_AREA];
19896 if (row->continued_p)
19897 row->pixel_width -= it->continuation_pixel_width;
19898 else if (row->truncated_on_right_p)
19899 row->pixel_width -= it->truncation_pixel_width;
19900 row->ascent = row->phys_ascent = 0;
19901 row->height = row->phys_height = row->visible_height = 1;
19902 row->extra_line_spacing = 0;
19905 /* Compute a hash code for this row. */
19906 row->hash = row_hash (row);
19908 it->max_ascent = it->max_descent = 0;
19909 it->max_phys_ascent = it->max_phys_descent = 0;
19913 /* Append one space to the glyph row of iterator IT if doing a
19914 window-based redisplay. The space has the same face as
19915 IT->face_id. Value is true if a space was added.
19917 This function is called to make sure that there is always one glyph
19918 at the end of a glyph row that the cursor can be set on under
19919 window-systems. (If there weren't such a glyph we would not know
19920 how wide and tall a box cursor should be displayed).
19922 At the same time this space let's a nicely handle clearing to the
19923 end of the line if the row ends in italic text. */
19925 static bool
19926 append_space_for_newline (struct it *it, bool default_face_p)
19928 if (FRAME_WINDOW_P (it->f))
19930 int n = it->glyph_row->used[TEXT_AREA];
19932 if (it->glyph_row->glyphs[TEXT_AREA] + n
19933 < it->glyph_row->glyphs[1 + TEXT_AREA])
19935 /* Save some values that must not be changed.
19936 Must save IT->c and IT->len because otherwise
19937 ITERATOR_AT_END_P wouldn't work anymore after
19938 append_space_for_newline has been called. */
19939 enum display_element_type saved_what = it->what;
19940 int saved_c = it->c, saved_len = it->len;
19941 int saved_char_to_display = it->char_to_display;
19942 int saved_x = it->current_x;
19943 int saved_face_id = it->face_id;
19944 bool saved_box_end = it->end_of_box_run_p;
19945 struct text_pos saved_pos;
19946 Lisp_Object saved_object;
19947 struct face *face;
19949 saved_object = it->object;
19950 saved_pos = it->position;
19952 it->what = IT_CHARACTER;
19953 memset (&it->position, 0, sizeof it->position);
19954 it->object = Qnil;
19955 it->c = it->char_to_display = ' ';
19956 it->len = 1;
19958 /* If the default face was remapped, be sure to use the
19959 remapped face for the appended newline. */
19960 if (default_face_p)
19961 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19962 else if (it->face_before_selective_p)
19963 it->face_id = it->saved_face_id;
19964 face = FACE_FROM_ID (it->f, it->face_id);
19965 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19966 /* In R2L rows, we will prepend a stretch glyph that will
19967 have the end_of_box_run_p flag set for it, so there's no
19968 need for the appended newline glyph to have that flag
19969 set. */
19970 if (it->glyph_row->reversed_p
19971 /* But if the appended newline glyph goes all the way to
19972 the end of the row, there will be no stretch glyph,
19973 so leave the box flag set. */
19974 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19975 it->end_of_box_run_p = false;
19977 PRODUCE_GLYPHS (it);
19979 #ifdef HAVE_WINDOW_SYSTEM
19980 /* Make sure this space glyph has the right ascent and
19981 descent values, or else cursor at end of line will look
19982 funny, and height of empty lines will be incorrect. */
19983 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19984 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19985 if (n == 0)
19987 Lisp_Object height, total_height;
19988 int extra_line_spacing = it->extra_line_spacing;
19989 int boff = font->baseline_offset;
19991 if (font->vertical_centering)
19992 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19994 it->object = saved_object; /* get_it_property needs this */
19995 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19996 /* Must do a subset of line height processing from
19997 x_produce_glyph for newline characters. */
19998 height = get_it_property (it, Qline_height);
19999 if (CONSP (height)
20000 && CONSP (XCDR (height))
20001 && NILP (XCDR (XCDR (height))))
20003 total_height = XCAR (XCDR (height));
20004 height = XCAR (height);
20006 else
20007 total_height = Qnil;
20008 height = calc_line_height_property (it, height, font, boff, true);
20010 if (it->override_ascent >= 0)
20012 it->ascent = it->override_ascent;
20013 it->descent = it->override_descent;
20014 boff = it->override_boff;
20016 if (EQ (height, Qt))
20017 extra_line_spacing = 0;
20018 else
20020 Lisp_Object spacing;
20022 it->phys_ascent = it->ascent;
20023 it->phys_descent = it->descent;
20024 if (!NILP (height)
20025 && XINT (height) > it->ascent + it->descent)
20026 it->ascent = XINT (height) - it->descent;
20028 if (!NILP (total_height))
20029 spacing = calc_line_height_property (it, total_height, font,
20030 boff, false);
20031 else
20033 spacing = get_it_property (it, Qline_spacing);
20034 spacing = calc_line_height_property (it, spacing, font,
20035 boff, false);
20037 if (INTEGERP (spacing))
20039 extra_line_spacing = XINT (spacing);
20040 if (!NILP (total_height))
20041 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20044 if (extra_line_spacing > 0)
20046 it->descent += extra_line_spacing;
20047 if (extra_line_spacing > it->max_extra_line_spacing)
20048 it->max_extra_line_spacing = extra_line_spacing;
20050 it->max_ascent = it->ascent;
20051 it->max_descent = it->descent;
20052 /* Make sure compute_line_metrics recomputes the row height. */
20053 it->glyph_row->height = 0;
20056 g->ascent = it->max_ascent;
20057 g->descent = it->max_descent;
20058 #endif
20060 it->override_ascent = -1;
20061 it->constrain_row_ascent_descent_p = false;
20062 it->current_x = saved_x;
20063 it->object = saved_object;
20064 it->position = saved_pos;
20065 it->what = saved_what;
20066 it->face_id = saved_face_id;
20067 it->len = saved_len;
20068 it->c = saved_c;
20069 it->char_to_display = saved_char_to_display;
20070 it->end_of_box_run_p = saved_box_end;
20071 return true;
20075 return false;
20079 /* Extend the face of the last glyph in the text area of IT->glyph_row
20080 to the end of the display line. Called from display_line. If the
20081 glyph row is empty, add a space glyph to it so that we know the
20082 face to draw. Set the glyph row flag fill_line_p. If the glyph
20083 row is R2L, prepend a stretch glyph to cover the empty space to the
20084 left of the leftmost glyph. */
20086 static void
20087 extend_face_to_end_of_line (struct it *it)
20089 struct face *face, *default_face;
20090 struct frame *f = it->f;
20092 /* If line is already filled, do nothing. Non window-system frames
20093 get a grace of one more ``pixel'' because their characters are
20094 1-``pixel'' wide, so they hit the equality too early. This grace
20095 is needed only for R2L rows that are not continued, to produce
20096 one extra blank where we could display the cursor. */
20097 if ((it->current_x >= it->last_visible_x
20098 + (!FRAME_WINDOW_P (f)
20099 && it->glyph_row->reversed_p
20100 && !it->glyph_row->continued_p))
20101 /* If the window has display margins, we will need to extend
20102 their face even if the text area is filled. */
20103 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20104 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20105 return;
20107 /* The default face, possibly remapped. */
20108 default_face = FACE_FROM_ID_OR_NULL (f,
20109 lookup_basic_face (f, DEFAULT_FACE_ID));
20111 /* Face extension extends the background and box of IT->face_id
20112 to the end of the line. If the background equals the background
20113 of the frame, we don't have to do anything. */
20114 face = FACE_FROM_ID (f, (it->face_before_selective_p
20115 ? it->saved_face_id
20116 : it->face_id));
20118 if (FRAME_WINDOW_P (f)
20119 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20120 && face->box == FACE_NO_BOX
20121 && face->background == FRAME_BACKGROUND_PIXEL (f)
20122 #ifdef HAVE_WINDOW_SYSTEM
20123 && !face->stipple
20124 #endif
20125 && !it->glyph_row->reversed_p)
20126 return;
20128 /* Set the glyph row flag indicating that the face of the last glyph
20129 in the text area has to be drawn to the end of the text area. */
20130 it->glyph_row->fill_line_p = true;
20132 /* If current character of IT is not ASCII, make sure we have the
20133 ASCII face. This will be automatically undone the next time
20134 get_next_display_element returns a multibyte character. Note
20135 that the character will always be single byte in unibyte
20136 text. */
20137 if (!ASCII_CHAR_P (it->c))
20139 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20142 if (FRAME_WINDOW_P (f))
20144 /* If the row is empty, add a space with the current face of IT,
20145 so that we know which face to draw. */
20146 if (it->glyph_row->used[TEXT_AREA] == 0)
20148 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20149 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20150 it->glyph_row->used[TEXT_AREA] = 1;
20152 /* Mode line and the header line don't have margins, and
20153 likewise the frame's tool-bar window, if there is any. */
20154 if (!(it->glyph_row->mode_line_p
20155 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20156 || (WINDOWP (f->tool_bar_window)
20157 && it->w == XWINDOW (f->tool_bar_window))
20158 #endif
20161 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20162 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20164 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20165 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20166 default_face->id;
20167 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20169 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20170 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20172 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20173 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20174 default_face->id;
20175 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20178 #ifdef HAVE_WINDOW_SYSTEM
20179 if (it->glyph_row->reversed_p)
20181 /* Prepend a stretch glyph to the row, such that the
20182 rightmost glyph will be drawn flushed all the way to the
20183 right margin of the window. The stretch glyph that will
20184 occupy the empty space, if any, to the left of the
20185 glyphs. */
20186 struct font *font = face->font ? face->font : FRAME_FONT (f);
20187 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20188 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20189 struct glyph *g;
20190 int row_width, stretch_ascent, stretch_width;
20191 struct text_pos saved_pos;
20192 int saved_face_id;
20193 bool saved_avoid_cursor, saved_box_start;
20195 for (row_width = 0, g = row_start; g < row_end; g++)
20196 row_width += g->pixel_width;
20198 /* FIXME: There are various minor display glitches in R2L
20199 rows when only one of the fringes is missing. The
20200 strange condition below produces the least bad effect. */
20201 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20202 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20203 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20204 stretch_width = window_box_width (it->w, TEXT_AREA);
20205 else
20206 stretch_width = it->last_visible_x - it->first_visible_x;
20207 stretch_width -= row_width;
20209 if (stretch_width > 0)
20211 stretch_ascent =
20212 (((it->ascent + it->descent)
20213 * FONT_BASE (font)) / FONT_HEIGHT (font));
20214 saved_pos = it->position;
20215 memset (&it->position, 0, sizeof it->position);
20216 saved_avoid_cursor = it->avoid_cursor_p;
20217 it->avoid_cursor_p = true;
20218 saved_face_id = it->face_id;
20219 saved_box_start = it->start_of_box_run_p;
20220 /* The last row's stretch glyph should get the default
20221 face, to avoid painting the rest of the window with
20222 the region face, if the region ends at ZV. */
20223 if (it->glyph_row->ends_at_zv_p)
20224 it->face_id = default_face->id;
20225 else
20226 it->face_id = face->id;
20227 it->start_of_box_run_p = false;
20228 append_stretch_glyph (it, Qnil, stretch_width,
20229 it->ascent + it->descent, stretch_ascent);
20230 it->position = saved_pos;
20231 it->avoid_cursor_p = saved_avoid_cursor;
20232 it->face_id = saved_face_id;
20233 it->start_of_box_run_p = saved_box_start;
20235 /* If stretch_width comes out negative, it means that the
20236 last glyph is only partially visible. In R2L rows, we
20237 want the leftmost glyph to be partially visible, so we
20238 need to give the row the corresponding left offset. */
20239 if (stretch_width < 0)
20240 it->glyph_row->x = stretch_width;
20242 #endif /* HAVE_WINDOW_SYSTEM */
20244 else
20246 /* Save some values that must not be changed. */
20247 int saved_x = it->current_x;
20248 struct text_pos saved_pos;
20249 Lisp_Object saved_object;
20250 enum display_element_type saved_what = it->what;
20251 int saved_face_id = it->face_id;
20253 saved_object = it->object;
20254 saved_pos = it->position;
20256 it->what = IT_CHARACTER;
20257 memset (&it->position, 0, sizeof it->position);
20258 it->object = Qnil;
20259 it->c = it->char_to_display = ' ';
20260 it->len = 1;
20262 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20263 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20264 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20265 && !it->glyph_row->mode_line_p
20266 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20268 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20269 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20271 for (it->current_x = 0; g < e; g++)
20272 it->current_x += g->pixel_width;
20274 it->area = LEFT_MARGIN_AREA;
20275 it->face_id = default_face->id;
20276 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20277 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20278 && g < it->glyph_row->glyphs[TEXT_AREA])
20280 PRODUCE_GLYPHS (it);
20281 /* term.c:produce_glyphs advances it->current_x only for
20282 TEXT_AREA. */
20283 it->current_x += it->pixel_width;
20284 g++;
20287 it->current_x = saved_x;
20288 it->area = TEXT_AREA;
20291 /* The last row's blank glyphs should get the default face, to
20292 avoid painting the rest of the window with the region face,
20293 if the region ends at ZV. */
20294 if (it->glyph_row->ends_at_zv_p)
20295 it->face_id = default_face->id;
20296 else
20297 it->face_id = face->id;
20298 PRODUCE_GLYPHS (it);
20300 while (it->current_x <= it->last_visible_x)
20301 PRODUCE_GLYPHS (it);
20303 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20304 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20305 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20306 && !it->glyph_row->mode_line_p
20307 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20309 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20310 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20312 for ( ; g < e; g++)
20313 it->current_x += g->pixel_width;
20315 it->area = RIGHT_MARGIN_AREA;
20316 it->face_id = default_face->id;
20317 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20318 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20319 && g < it->glyph_row->glyphs[LAST_AREA])
20321 PRODUCE_GLYPHS (it);
20322 it->current_x += it->pixel_width;
20323 g++;
20326 it->area = TEXT_AREA;
20329 /* Don't count these blanks really. It would let us insert a left
20330 truncation glyph below and make us set the cursor on them, maybe. */
20331 it->current_x = saved_x;
20332 it->object = saved_object;
20333 it->position = saved_pos;
20334 it->what = saved_what;
20335 it->face_id = saved_face_id;
20340 /* Value is true if text starting at CHARPOS in current_buffer is
20341 trailing whitespace. */
20343 static bool
20344 trailing_whitespace_p (ptrdiff_t charpos)
20346 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20347 int c = 0;
20349 while (bytepos < ZV_BYTE
20350 && (c = FETCH_CHAR (bytepos),
20351 c == ' ' || c == '\t'))
20352 ++bytepos;
20354 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20356 if (bytepos != PT_BYTE)
20357 return true;
20359 return false;
20363 /* Highlight trailing whitespace, if any, in ROW. */
20365 static void
20366 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20368 int used = row->used[TEXT_AREA];
20370 if (used)
20372 struct glyph *start = row->glyphs[TEXT_AREA];
20373 struct glyph *glyph = start + used - 1;
20375 if (row->reversed_p)
20377 /* Right-to-left rows need to be processed in the opposite
20378 direction, so swap the edge pointers. */
20379 glyph = start;
20380 start = row->glyphs[TEXT_AREA] + used - 1;
20383 /* Skip over glyphs inserted to display the cursor at the
20384 end of a line, for extending the face of the last glyph
20385 to the end of the line on terminals, and for truncation
20386 and continuation glyphs. */
20387 if (!row->reversed_p)
20389 while (glyph >= start
20390 && glyph->type == CHAR_GLYPH
20391 && NILP (glyph->object))
20392 --glyph;
20394 else
20396 while (glyph <= start
20397 && glyph->type == CHAR_GLYPH
20398 && NILP (glyph->object))
20399 ++glyph;
20402 /* If last glyph is a space or stretch, and it's trailing
20403 whitespace, set the face of all trailing whitespace glyphs in
20404 IT->glyph_row to `trailing-whitespace'. */
20405 if ((row->reversed_p ? glyph <= start : glyph >= start)
20406 && BUFFERP (glyph->object)
20407 && (glyph->type == STRETCH_GLYPH
20408 || (glyph->type == CHAR_GLYPH
20409 && glyph->u.ch == ' '))
20410 && trailing_whitespace_p (glyph->charpos))
20412 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20413 if (face_id < 0)
20414 return;
20416 if (!row->reversed_p)
20418 while (glyph >= start
20419 && BUFFERP (glyph->object)
20420 && (glyph->type == STRETCH_GLYPH
20421 || (glyph->type == CHAR_GLYPH
20422 && glyph->u.ch == ' ')))
20423 (glyph--)->face_id = face_id;
20425 else
20427 while (glyph <= start
20428 && BUFFERP (glyph->object)
20429 && (glyph->type == STRETCH_GLYPH
20430 || (glyph->type == CHAR_GLYPH
20431 && glyph->u.ch == ' ')))
20432 (glyph++)->face_id = face_id;
20439 /* Value is true if glyph row ROW should be
20440 considered to hold the buffer position CHARPOS. */
20442 static bool
20443 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20445 bool result = true;
20447 if (charpos == CHARPOS (row->end.pos)
20448 || charpos == MATRIX_ROW_END_CHARPOS (row))
20450 /* Suppose the row ends on a string.
20451 Unless the row is continued, that means it ends on a newline
20452 in the string. If it's anything other than a display string
20453 (e.g., a before-string from an overlay), we don't want the
20454 cursor there. (This heuristic seems to give the optimal
20455 behavior for the various types of multi-line strings.)
20456 One exception: if the string has `cursor' property on one of
20457 its characters, we _do_ want the cursor there. */
20458 if (CHARPOS (row->end.string_pos) >= 0)
20460 if (row->continued_p)
20461 result = true;
20462 else
20464 /* Check for `display' property. */
20465 struct glyph *beg = row->glyphs[TEXT_AREA];
20466 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20467 struct glyph *glyph;
20469 result = false;
20470 for (glyph = end; glyph >= beg; --glyph)
20471 if (STRINGP (glyph->object))
20473 Lisp_Object prop
20474 = Fget_char_property (make_number (charpos),
20475 Qdisplay, Qnil);
20476 result =
20477 (!NILP (prop)
20478 && display_prop_string_p (prop, glyph->object));
20479 /* If there's a `cursor' property on one of the
20480 string's characters, this row is a cursor row,
20481 even though this is not a display string. */
20482 if (!result)
20484 Lisp_Object s = glyph->object;
20486 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20488 ptrdiff_t gpos = glyph->charpos;
20490 if (!NILP (Fget_char_property (make_number (gpos),
20491 Qcursor, s)))
20493 result = true;
20494 break;
20498 break;
20502 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20504 /* If the row ends in middle of a real character,
20505 and the line is continued, we want the cursor here.
20506 That's because CHARPOS (ROW->end.pos) would equal
20507 PT if PT is before the character. */
20508 if (!row->ends_in_ellipsis_p)
20509 result = row->continued_p;
20510 else
20511 /* If the row ends in an ellipsis, then
20512 CHARPOS (ROW->end.pos) will equal point after the
20513 invisible text. We want that position to be displayed
20514 after the ellipsis. */
20515 result = false;
20517 /* If the row ends at ZV, display the cursor at the end of that
20518 row instead of at the start of the row below. */
20519 else
20520 result = row->ends_at_zv_p;
20523 return result;
20526 /* Value is true if glyph row ROW should be
20527 used to hold the cursor. */
20529 static bool
20530 cursor_row_p (struct glyph_row *row)
20532 return row_for_charpos_p (row, PT);
20537 /* Push the property PROP so that it will be rendered at the current
20538 position in IT. Return true if PROP was successfully pushed, false
20539 otherwise. Called from handle_line_prefix to handle the
20540 `line-prefix' and `wrap-prefix' properties. */
20542 static bool
20543 push_prefix_prop (struct it *it, Lisp_Object prop)
20545 struct text_pos pos =
20546 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20548 eassert (it->method == GET_FROM_BUFFER
20549 || it->method == GET_FROM_DISPLAY_VECTOR
20550 || it->method == GET_FROM_STRING
20551 || it->method == GET_FROM_IMAGE);
20553 /* We need to save the current buffer/string position, so it will be
20554 restored by pop_it, because iterate_out_of_display_property
20555 depends on that being set correctly, but some situations leave
20556 it->position not yet set when this function is called. */
20557 push_it (it, &pos);
20559 if (STRINGP (prop))
20561 if (SCHARS (prop) == 0)
20563 pop_it (it);
20564 return false;
20567 it->string = prop;
20568 it->string_from_prefix_prop_p = true;
20569 it->multibyte_p = STRING_MULTIBYTE (it->string);
20570 it->current.overlay_string_index = -1;
20571 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20572 it->end_charpos = it->string_nchars = SCHARS (it->string);
20573 it->method = GET_FROM_STRING;
20574 it->stop_charpos = 0;
20575 it->prev_stop = 0;
20576 it->base_level_stop = 0;
20577 it->cmp_it.id = -1;
20579 /* Force paragraph direction to be that of the parent
20580 buffer/string. */
20581 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20582 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20583 else
20584 it->paragraph_embedding = L2R;
20586 /* Set up the bidi iterator for this display string. */
20587 if (it->bidi_p)
20589 it->bidi_it.string.lstring = it->string;
20590 it->bidi_it.string.s = NULL;
20591 it->bidi_it.string.schars = it->end_charpos;
20592 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20593 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20594 it->bidi_it.string.unibyte = !it->multibyte_p;
20595 it->bidi_it.w = it->w;
20596 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20599 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20601 it->method = GET_FROM_STRETCH;
20602 it->object = prop;
20604 #ifdef HAVE_WINDOW_SYSTEM
20605 else if (IMAGEP (prop))
20607 it->what = IT_IMAGE;
20608 it->image_id = lookup_image (it->f, prop);
20609 it->method = GET_FROM_IMAGE;
20611 #endif /* HAVE_WINDOW_SYSTEM */
20612 else
20614 pop_it (it); /* bogus display property, give up */
20615 return false;
20618 return true;
20621 /* Return the character-property PROP at the current position in IT. */
20623 static Lisp_Object
20624 get_it_property (struct it *it, Lisp_Object prop)
20626 Lisp_Object position, object = it->object;
20628 if (STRINGP (object))
20629 position = make_number (IT_STRING_CHARPOS (*it));
20630 else if (BUFFERP (object))
20632 position = make_number (IT_CHARPOS (*it));
20633 object = it->window;
20635 else
20636 return Qnil;
20638 return Fget_char_property (position, prop, object);
20641 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20643 static void
20644 handle_line_prefix (struct it *it)
20646 Lisp_Object prefix;
20648 if (it->continuation_lines_width > 0)
20650 prefix = get_it_property (it, Qwrap_prefix);
20651 if (NILP (prefix))
20652 prefix = Vwrap_prefix;
20654 else
20656 prefix = get_it_property (it, Qline_prefix);
20657 if (NILP (prefix))
20658 prefix = Vline_prefix;
20660 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20662 /* If the prefix is wider than the window, and we try to wrap
20663 it, it would acquire its own wrap prefix, and so on till the
20664 iterator stack overflows. So, don't wrap the prefix. */
20665 it->line_wrap = TRUNCATE;
20666 it->avoid_cursor_p = true;
20672 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20673 only for R2L lines from display_line and display_string, when they
20674 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20675 the line/string needs to be continued on the next glyph row. */
20676 static void
20677 unproduce_glyphs (struct it *it, int n)
20679 struct glyph *glyph, *end;
20681 eassert (it->glyph_row);
20682 eassert (it->glyph_row->reversed_p);
20683 eassert (it->area == TEXT_AREA);
20684 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20686 if (n > it->glyph_row->used[TEXT_AREA])
20687 n = it->glyph_row->used[TEXT_AREA];
20688 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20689 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20690 for ( ; glyph < end; glyph++)
20691 glyph[-n] = *glyph;
20694 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20695 and ROW->maxpos. */
20696 static void
20697 find_row_edges (struct it *it, struct glyph_row *row,
20698 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20699 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20701 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20702 lines' rows is implemented for bidi-reordered rows. */
20704 /* ROW->minpos is the value of min_pos, the minimal buffer position
20705 we have in ROW, or ROW->start.pos if that is smaller. */
20706 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20707 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20708 else
20709 /* We didn't find buffer positions smaller than ROW->start, or
20710 didn't find _any_ valid buffer positions in any of the glyphs,
20711 so we must trust the iterator's computed positions. */
20712 row->minpos = row->start.pos;
20713 if (max_pos <= 0)
20715 max_pos = CHARPOS (it->current.pos);
20716 max_bpos = BYTEPOS (it->current.pos);
20719 /* Here are the various use-cases for ending the row, and the
20720 corresponding values for ROW->maxpos:
20722 Line ends in a newline from buffer eol_pos + 1
20723 Line is continued from buffer max_pos + 1
20724 Line is truncated on right it->current.pos
20725 Line ends in a newline from string max_pos + 1(*)
20726 (*) + 1 only when line ends in a forward scan
20727 Line is continued from string max_pos
20728 Line is continued from display vector max_pos
20729 Line is entirely from a string min_pos == max_pos
20730 Line is entirely from a display vector min_pos == max_pos
20731 Line that ends at ZV ZV
20733 If you discover other use-cases, please add them here as
20734 appropriate. */
20735 if (row->ends_at_zv_p)
20736 row->maxpos = it->current.pos;
20737 else if (row->used[TEXT_AREA])
20739 bool seen_this_string = false;
20740 struct glyph_row *r1 = row - 1;
20742 /* Did we see the same display string on the previous row? */
20743 if (STRINGP (it->object)
20744 /* this is not the first row */
20745 && row > it->w->desired_matrix->rows
20746 /* previous row is not the header line */
20747 && !r1->mode_line_p
20748 /* previous row also ends in a newline from a string */
20749 && r1->ends_in_newline_from_string_p)
20751 struct glyph *start, *end;
20753 /* Search for the last glyph of the previous row that came
20754 from buffer or string. Depending on whether the row is
20755 L2R or R2L, we need to process it front to back or the
20756 other way round. */
20757 if (!r1->reversed_p)
20759 start = r1->glyphs[TEXT_AREA];
20760 end = start + r1->used[TEXT_AREA];
20761 /* Glyphs inserted by redisplay have nil as their object. */
20762 while (end > start
20763 && NILP ((end - 1)->object)
20764 && (end - 1)->charpos <= 0)
20765 --end;
20766 if (end > start)
20768 if (EQ ((end - 1)->object, it->object))
20769 seen_this_string = true;
20771 else
20772 /* If all the glyphs of the previous row were inserted
20773 by redisplay, it means the previous row was
20774 produced from a single newline, which is only
20775 possible if that newline came from the same string
20776 as the one which produced this ROW. */
20777 seen_this_string = true;
20779 else
20781 end = r1->glyphs[TEXT_AREA] - 1;
20782 start = end + r1->used[TEXT_AREA];
20783 while (end < start
20784 && NILP ((end + 1)->object)
20785 && (end + 1)->charpos <= 0)
20786 ++end;
20787 if (end < start)
20789 if (EQ ((end + 1)->object, it->object))
20790 seen_this_string = true;
20792 else
20793 seen_this_string = true;
20796 /* Take note of each display string that covers a newline only
20797 once, the first time we see it. This is for when a display
20798 string includes more than one newline in it. */
20799 if (row->ends_in_newline_from_string_p && !seen_this_string)
20801 /* If we were scanning the buffer forward when we displayed
20802 the string, we want to account for at least one buffer
20803 position that belongs to this row (position covered by
20804 the display string), so that cursor positioning will
20805 consider this row as a candidate when point is at the end
20806 of the visual line represented by this row. This is not
20807 required when scanning back, because max_pos will already
20808 have a much larger value. */
20809 if (CHARPOS (row->end.pos) > max_pos)
20810 INC_BOTH (max_pos, max_bpos);
20811 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20813 else if (CHARPOS (it->eol_pos) > 0)
20814 SET_TEXT_POS (row->maxpos,
20815 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20816 else if (row->continued_p)
20818 /* If max_pos is different from IT's current position, it
20819 means IT->method does not belong to the display element
20820 at max_pos. However, it also means that the display
20821 element at max_pos was displayed in its entirety on this
20822 line, which is equivalent to saying that the next line
20823 starts at the next buffer position. */
20824 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20825 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20826 else
20828 INC_BOTH (max_pos, max_bpos);
20829 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20832 else if (row->truncated_on_right_p)
20833 /* display_line already called reseat_at_next_visible_line_start,
20834 which puts the iterator at the beginning of the next line, in
20835 the logical order. */
20836 row->maxpos = it->current.pos;
20837 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20838 /* A line that is entirely from a string/image/stretch... */
20839 row->maxpos = row->minpos;
20840 else
20841 emacs_abort ();
20843 else
20844 row->maxpos = it->current.pos;
20847 /* Like display_count_lines, but capable of counting outside of the
20848 current narrowed region. */
20849 static ptrdiff_t
20850 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20851 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20853 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20854 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20856 ptrdiff_t val;
20857 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20858 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20859 Fwiden ();
20860 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20861 unbind_to (pdl_count, Qnil);
20862 return val;
20865 /* Count the number of screen lines in window IT->w between character
20866 position IT_CHARPOS(*IT) and the line showing that window's point. */
20867 static ptrdiff_t
20868 display_count_lines_visually (struct it *it)
20870 struct it tem_it;
20871 ptrdiff_t to;
20872 struct text_pos from;
20874 /* If we already calculated a relative line number, use that. This
20875 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20876 are laid out sequentially, one by one, for each sequence of calls
20877 to display_line or other similar function that follows a call to
20878 init_iterator. */
20879 if (it->lnum_bytepos > 0)
20880 return it->lnum + 1;
20881 else
20883 ptrdiff_t count = SPECPDL_INDEX ();
20885 if (IT_CHARPOS (*it) <= PT)
20887 from = it->current.pos;
20888 to = PT;
20890 else
20892 SET_TEXT_POS (from, PT, PT_BYTE);
20893 to = IT_CHARPOS (*it);
20895 start_display (&tem_it, it->w, from);
20896 /* Need to disable visual mode temporarily, since otherwise the
20897 call to move_it_to will cause infinite recursion. */
20898 specbind (Qdisplay_line_numbers, Qrelative);
20899 /* Some redisplay optimizations could invoke us very far from
20900 PT, which will make the caller painfully slow. There should
20901 be no need to go too far beyond the window's bottom, as any
20902 such optimization will fail to show point anyway. */
20903 move_it_to (&tem_it, to, -1,
20904 tem_it.last_visible_y
20905 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20906 -1, MOVE_TO_POS | MOVE_TO_Y);
20907 unbind_to (count, Qnil);
20908 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20912 /* Produce the line-number glyphs for the current glyph_row. If
20913 IT->glyph_row is non-NULL, populate the row with the produced
20914 glyphs. */
20915 static void
20916 maybe_produce_line_number (struct it *it)
20918 ptrdiff_t last_line = it->lnum;
20919 ptrdiff_t start_from, bytepos;
20920 ptrdiff_t this_line;
20921 bool first_time = false;
20922 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20923 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20924 void *itdata = bidi_shelve_cache ();
20926 if (EQ (Vdisplay_line_numbers, Qvisual))
20927 this_line = display_count_lines_visually (it);
20928 else
20930 if (!last_line)
20932 /* If possible, reuse data cached by line-number-mode. */
20933 if (it->w->base_line_number > 0
20934 && it->w->base_line_pos > 0
20935 && it->w->base_line_pos <= IT_CHARPOS (*it)
20936 /* line-number-mode always displays narrowed line
20937 numbers, so we cannot use its data if the user wants
20938 line numbers that disregard narrowing, or if the
20939 buffer's narrowing has just changed. */
20940 && !(display_line_numbers_widen
20941 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
20942 && !current_buffer->clip_changed)
20944 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20945 last_line = it->w->base_line_number - 1;
20947 else
20948 start_from = beg_byte;
20949 if (!it->lnum_bytepos)
20950 first_time = true;
20952 else
20953 start_from = it->lnum_bytepos;
20955 /* Paranoia: what if someone changes the narrowing since the
20956 last time display_line was called? Shouldn't really happen,
20957 but who knows what some crazy Lisp invoked by :eval could do? */
20958 if (!(beg_byte <= start_from && start_from <= z_byte))
20960 last_line = 0;
20961 start_from = beg_byte;
20964 this_line =
20965 last_line + display_count_lines_logically (start_from,
20966 IT_BYTEPOS (*it),
20967 IT_CHARPOS (*it), &bytepos);
20968 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20969 eassert (bytepos == IT_BYTEPOS (*it));
20972 /* Record the line number information. */
20973 if (this_line != last_line || !it->lnum_bytepos)
20975 it->lnum = this_line;
20976 it->lnum_bytepos = IT_BYTEPOS (*it);
20979 /* Produce the glyphs for the line number. */
20980 struct it tem_it;
20981 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20982 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20983 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20984 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20985 int current_lnum_face_id
20986 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20987 /* Compute point's line number if needed. */
20988 if ((EQ (Vdisplay_line_numbers, Qrelative)
20989 || EQ (Vdisplay_line_numbers, Qvisual)
20990 || lnum_face_id != current_lnum_face_id)
20991 && !it->pt_lnum)
20993 ptrdiff_t ignored;
20994 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20995 it->pt_lnum =
20996 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20997 PT, &ignored);
20998 else
20999 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21000 &ignored);
21002 /* Compute the required width if needed. */
21003 if (!it->lnum_width)
21005 if (NATNUMP (Vdisplay_line_numbers_width))
21006 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21008 /* Max line number to be displayed cannot be more than the one
21009 corresponding to the last row of the desired matrix. */
21010 ptrdiff_t max_lnum;
21012 if (NILP (Vdisplay_line_numbers_current_absolute)
21013 && (EQ (Vdisplay_line_numbers, Qrelative)
21014 || EQ (Vdisplay_line_numbers, Qvisual)))
21015 /* We subtract one more because the current line is always
21016 zero in this mode. */
21017 max_lnum = it->w->desired_matrix->nrows - 2;
21018 else if (EQ (Vdisplay_line_numbers, Qvisual))
21019 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21020 else
21021 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21022 max_lnum = max (1, max_lnum);
21023 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21024 eassert (it->lnum_width > 0);
21026 if (EQ (Vdisplay_line_numbers, Qrelative))
21027 lnum_offset = it->pt_lnum;
21028 else if (EQ (Vdisplay_line_numbers, Qvisual))
21029 lnum_offset = 0;
21031 /* Under 'relative', display the absolute line number for the
21032 current line, unless the user requests otherwise. */
21033 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21034 if ((EQ (Vdisplay_line_numbers, Qrelative)
21035 || EQ (Vdisplay_line_numbers, Qvisual))
21036 && lnum_to_display == 0
21037 && !NILP (Vdisplay_line_numbers_current_absolute))
21038 lnum_to_display = it->pt_lnum + 1;
21039 /* In L2R rows we need to append the blank separator, in R2L
21040 rows we need to prepend it. But this function is usually
21041 called when no display elements were produced from the
21042 following line, so the paragraph direction might be unknown.
21043 Therefore we cheat and add 2 blanks, one on either side. */
21044 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21045 strcat (lnum_buf, " ");
21047 /* Setup for producing the glyphs. */
21048 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21049 /* FIXME: Use specialized face. */
21050 DEFAULT_FACE_ID);
21051 scratch_glyph_row.reversed_p = false;
21052 scratch_glyph_row.used[TEXT_AREA] = 0;
21053 SET_TEXT_POS (tem_it.position, 0, 0);
21054 tem_it.avoid_cursor_p = true;
21055 tem_it.bidi_p = true;
21056 tem_it.bidi_it.type = WEAK_EN;
21057 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21058 1 level in R2L paragraphs. Emulate that, assuming we are in
21059 an L2R paragraph. */
21060 tem_it.bidi_it.resolved_level = 2;
21062 /* Produce glyphs for the line number in a scratch glyph_row. */
21063 int n_glyphs_before;
21064 for (const char *p = lnum_buf; *p; p++)
21066 /* For continuation lines and lines after ZV, instead of a line
21067 number, produce a blank prefix of the same width. Use the
21068 default face for the blank field beyond ZV. */
21069 if (beyond_zv)
21070 tem_it.face_id = it->base_face_id;
21071 else if (lnum_face_id != current_lnum_face_id
21072 && (EQ (Vdisplay_line_numbers, Qvisual)
21073 ? this_line == 0
21074 : this_line == it->pt_lnum))
21075 tem_it.face_id = current_lnum_face_id;
21076 else
21077 tem_it.face_id = lnum_face_id;
21078 if (beyond_zv
21079 /* Don't display the same line number more than once. */
21080 || (!EQ (Vdisplay_line_numbers, Qvisual)
21081 && (it->continuation_lines_width > 0
21082 || (this_line == last_line && !first_time))))
21083 tem_it.c = tem_it.char_to_display = ' ';
21084 else
21085 tem_it.c = tem_it.char_to_display = *p;
21086 tem_it.len = 1;
21087 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21088 /* Make sure these glyphs will have a "position" of -1. */
21089 SET_TEXT_POS (tem_it.position, -1, -1);
21090 PRODUCE_GLYPHS (&tem_it);
21092 /* Stop producing glyphs if we don't have enough space on
21093 this line. FIXME: should we refrain from producing the
21094 line number at all in that case? */
21095 if (tem_it.current_x > tem_it.last_visible_x)
21097 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21098 break;
21102 /* Record the width in pixels we need for the line number display. */
21103 it->lnum_pixel_width = tem_it.current_x;
21104 /* Copy the produced glyphs into IT's glyph_row. */
21105 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21106 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21107 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21108 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21110 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21112 for ( ; g < e; g++)
21114 it->current_x += g->pixel_width;
21115 /* The following is important when this function is called
21116 from move_it_in_display_line_to: HPOS is incremented only
21117 when we are in the visible portion of the glyph row. */
21118 if (it->current_x > it->first_visible_x)
21119 it->hpos++;
21120 if (p)
21122 *p++ = *g;
21123 (*u)++;
21127 /* Update IT's metrics due to glyphs produced for line numbers. */
21128 if (it->glyph_row)
21130 struct glyph_row *row = it->glyph_row;
21132 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21133 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21134 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21135 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21136 tem_it.max_phys_descent);
21138 else
21140 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21141 it->max_descent = max (it->max_descent, tem_it.max_descent);
21142 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21143 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21146 bidi_unshelve_cache (itdata, false);
21149 /* Return true if this glyph row needs a line number to be produced
21150 for it. */
21151 static bool
21152 should_produce_line_number (struct it *it)
21154 if (NILP (Vdisplay_line_numbers))
21155 return false;
21157 /* Don't display line numbers in minibuffer windows. */
21158 if (MINI_WINDOW_P (it->w))
21159 return false;
21161 #ifdef HAVE_WINDOW_SYSTEM
21162 /* Don't display line number in tooltip frames. */
21163 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame)
21164 #ifdef USE_GTK
21165 /* GTK builds store in tip_frame the frame that shows the tip,
21166 so we need an additional test. */
21167 && !NILP (Fframe_parameter (tip_frame, Qtooltip))
21168 #endif
21170 return false;
21171 #endif
21173 /* If the character at current position has a non-nil special
21174 property, disable line numbers for this row. This is for
21175 packages such as company-mode, which need this for their tricky
21176 layout, where line numbers get in the way. */
21177 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21178 Qdisplay_line_numbers_disable,
21179 it->window);
21180 /* For ZV, we need to also look in empty overlays at that point,
21181 because get-char-property always returns nil for ZV, except if
21182 the property is in 'default-text-properties'. */
21183 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21184 val = disable_line_numbers_overlay_at_eob ();
21185 return NILP (val) ? true : false;
21188 /* Return true if ROW has no glyphs except those inserted by the
21189 display engine. This is needed for indicate-empty-lines and
21190 similar features when the glyph row starts with glyphs which didn't
21191 come from buffer or string. */
21192 static bool
21193 row_text_area_empty (struct glyph_row *row)
21195 if (!row->reversed_p)
21197 for (struct glyph *g = row->glyphs[TEXT_AREA];
21198 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21199 g++)
21200 if (!NILP (g->object) || g->charpos > 0)
21201 return false;
21203 else
21205 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21206 g > row->glyphs[TEXT_AREA];
21207 g--)
21208 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21209 return false;
21212 return true;
21215 /* Construct the glyph row IT->glyph_row in the desired matrix of
21216 IT->w from text at the current position of IT. See dispextern.h
21217 for an overview of struct it. Value is true if
21218 IT->glyph_row displays text, as opposed to a line displaying ZV
21219 only. CURSOR_VPOS is the window-relative vertical position of
21220 the glyph row displaying the cursor, or -1 if unknown. */
21222 static bool
21223 display_line (struct it *it, int cursor_vpos)
21225 struct glyph_row *row = it->glyph_row;
21226 Lisp_Object overlay_arrow_string;
21227 struct it wrap_it;
21228 void *wrap_data = NULL;
21229 bool may_wrap = false;
21230 int wrap_x UNINIT;
21231 int wrap_row_used = -1;
21232 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21233 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21234 int wrap_row_extra_line_spacing UNINIT;
21235 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21236 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21237 int cvpos;
21238 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21239 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21240 bool pending_handle_line_prefix = false;
21241 int header_line = window_wants_header_line (it->w);
21242 bool hscroll_this_line = (cursor_vpos >= 0
21243 && it->vpos == cursor_vpos - header_line
21244 && hscrolling_current_line_p (it->w));
21245 int first_visible_x = it->first_visible_x;
21246 int last_visible_x = it->last_visible_x;
21247 int x_incr = 0;
21249 /* We always start displaying at hpos zero even if hscrolled. */
21250 eassert (it->hpos == 0 && it->current_x == 0);
21252 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21253 >= it->w->desired_matrix->nrows)
21255 it->w->nrows_scale_factor++;
21256 it->f->fonts_changed = true;
21257 return false;
21260 /* Clear the result glyph row and enable it. */
21261 prepare_desired_row (it->w, row, false);
21263 row->y = it->current_y;
21264 row->start = it->start;
21265 row->continuation_lines_width = it->continuation_lines_width;
21266 row->displays_text_p = true;
21267 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21268 it->starts_in_middle_of_char_p = false;
21270 /* Arrange the overlays nicely for our purposes. Usually, we call
21271 display_line on only one line at a time, in which case this
21272 can't really hurt too much, or we call it on lines which appear
21273 one after another in the buffer, in which case all calls to
21274 recenter_overlay_lists but the first will be pretty cheap. */
21275 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21277 /* If we are going to display the cursor's line, account for the
21278 hscroll of that line. We subtract the window's min_hscroll,
21279 because that was already accounted for in init_iterator. */
21280 if (hscroll_this_line)
21281 x_incr =
21282 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21283 * FRAME_COLUMN_WIDTH (it->f);
21285 bool line_number_needed = should_produce_line_number (it);
21287 /* Move over display elements that are not visible because we are
21288 hscrolled. This may stop at an x-position < first_visible_x
21289 if the first glyph is partially visible or if we hit a line end. */
21290 if (it->current_x < it->first_visible_x + x_incr)
21292 enum move_it_result move_result;
21294 this_line_min_pos = row->start.pos;
21295 if (hscroll_this_line)
21297 it->first_visible_x += x_incr;
21298 it->last_visible_x += x_incr;
21300 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21301 MOVE_TO_POS | MOVE_TO_X);
21302 /* If we are under a large hscroll, move_it_in_display_line_to
21303 could hit the end of the line without reaching
21304 first_visible_x. Pretend that we did reach it. This is
21305 especially important on a TTY, where we will call
21306 extend_face_to_end_of_line, which needs to know how many
21307 blank glyphs to produce. */
21308 if (it->current_x < it->first_visible_x
21309 && (move_result == MOVE_NEWLINE_OR_CR
21310 || move_result == MOVE_POS_MATCH_OR_ZV))
21311 it->current_x = it->first_visible_x;
21313 /* Record the smallest positions seen while we moved over
21314 display elements that are not visible. This is needed by
21315 redisplay_internal for optimizing the case where the cursor
21316 stays inside the same line. The rest of this function only
21317 considers positions that are actually displayed, so
21318 RECORD_MAX_MIN_POS will not otherwise record positions that
21319 are hscrolled to the left of the left edge of the window. */
21320 min_pos = CHARPOS (this_line_min_pos);
21321 min_bpos = BYTEPOS (this_line_min_pos);
21323 /* Produce line number, if needed. */
21324 if (line_number_needed)
21325 maybe_produce_line_number (it);
21327 else if (it->area == TEXT_AREA)
21329 /* Line numbers should precede the line-prefix or wrap-prefix. */
21330 if (line_number_needed)
21331 maybe_produce_line_number (it);
21333 /* We only do this when not calling move_it_in_display_line_to
21334 above, because that function calls itself handle_line_prefix. */
21335 handle_line_prefix (it);
21337 else
21339 /* Line-prefix and wrap-prefix are always displayed in the text
21340 area. But if this is the first call to display_line after
21341 init_iterator, the iterator might have been set up to write
21342 into a marginal area, e.g. if the line begins with some
21343 display property that writes to the margins. So we need to
21344 wait with the call to handle_line_prefix until whatever
21345 writes to the margin has done its job. */
21346 pending_handle_line_prefix = true;
21349 /* Get the initial row height. This is either the height of the
21350 text hscrolled, if there is any, or zero. */
21351 row->ascent = it->max_ascent;
21352 row->height = it->max_ascent + it->max_descent;
21353 row->phys_ascent = it->max_phys_ascent;
21354 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21355 row->extra_line_spacing = it->max_extra_line_spacing;
21357 /* Utility macro to record max and min buffer positions seen until now. */
21358 #define RECORD_MAX_MIN_POS(IT) \
21359 do \
21361 bool composition_p \
21362 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21363 ptrdiff_t current_pos = \
21364 composition_p ? (IT)->cmp_it.charpos \
21365 : IT_CHARPOS (*(IT)); \
21366 ptrdiff_t current_bpos = \
21367 composition_p ? CHAR_TO_BYTE (current_pos) \
21368 : IT_BYTEPOS (*(IT)); \
21369 if (current_pos < min_pos) \
21371 min_pos = current_pos; \
21372 min_bpos = current_bpos; \
21374 if (IT_CHARPOS (*it) > max_pos) \
21376 max_pos = IT_CHARPOS (*it); \
21377 max_bpos = IT_BYTEPOS (*it); \
21380 while (false)
21382 /* Loop generating characters. The loop is left with IT on the next
21383 character to display. */
21384 while (true)
21386 int n_glyphs_before, hpos_before, x_before;
21387 int x, nglyphs;
21388 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21390 /* Retrieve the next thing to display. Value is false if end of
21391 buffer reached. */
21392 if (!get_next_display_element (it))
21394 bool row_has_glyphs = false;
21395 /* Maybe add a space at the end of this line that is used to
21396 display the cursor there under X. Set the charpos of the
21397 first glyph of blank lines not corresponding to any text
21398 to -1. */
21399 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21400 row->exact_window_width_line_p = true;
21401 else if ((append_space_for_newline (it, true)
21402 && row->used[TEXT_AREA] == 1)
21403 || row->used[TEXT_AREA] == 0
21404 || (row_has_glyphs = row_text_area_empty (row)))
21406 row->glyphs[TEXT_AREA]->charpos = -1;
21407 /* Don't reset the displays_text_p flag if we are
21408 displaying line numbers or line-prefix. */
21409 if (!row_has_glyphs)
21410 row->displays_text_p = false;
21412 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21413 && (!MINI_WINDOW_P (it->w)))
21414 row->indicate_empty_line_p = true;
21417 it->continuation_lines_width = 0;
21418 /* Reset those iterator values set from display property
21419 values. This is for the case when the display property
21420 ends at ZV, and is not a replacing property, so pop_it is
21421 not called. */
21422 it->font_height = Qnil;
21423 it->voffset = 0;
21424 row->ends_at_zv_p = true;
21425 /* A row that displays right-to-left text must always have
21426 its last face extended all the way to the end of line,
21427 even if this row ends in ZV, because we still write to
21428 the screen left to right. We also need to extend the
21429 last face if the default face is remapped to some
21430 different face, otherwise the functions that clear
21431 portions of the screen will clear with the default face's
21432 background color. */
21433 if (row->reversed_p
21434 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21435 extend_face_to_end_of_line (it);
21436 break;
21439 /* Now, get the metrics of what we want to display. This also
21440 generates glyphs in `row' (which is IT->glyph_row). */
21441 n_glyphs_before = row->used[TEXT_AREA];
21442 x = it->current_x;
21444 /* Remember the line height so far in case the next element doesn't
21445 fit on the line. */
21446 if (it->line_wrap != TRUNCATE)
21448 ascent = it->max_ascent;
21449 descent = it->max_descent;
21450 phys_ascent = it->max_phys_ascent;
21451 phys_descent = it->max_phys_descent;
21453 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21455 if (IT_DISPLAYING_WHITESPACE (it))
21456 may_wrap = true;
21457 else if (may_wrap)
21459 SAVE_IT (wrap_it, *it, wrap_data);
21460 wrap_x = x;
21461 wrap_row_used = row->used[TEXT_AREA];
21462 wrap_row_ascent = row->ascent;
21463 wrap_row_height = row->height;
21464 wrap_row_phys_ascent = row->phys_ascent;
21465 wrap_row_phys_height = row->phys_height;
21466 wrap_row_extra_line_spacing = row->extra_line_spacing;
21467 wrap_row_min_pos = min_pos;
21468 wrap_row_min_bpos = min_bpos;
21469 wrap_row_max_pos = max_pos;
21470 wrap_row_max_bpos = max_bpos;
21471 may_wrap = false;
21476 PRODUCE_GLYPHS (it);
21478 /* If this display element was in marginal areas, continue with
21479 the next one. */
21480 if (it->area != TEXT_AREA)
21482 row->ascent = max (row->ascent, it->max_ascent);
21483 row->height = max (row->height, it->max_ascent + it->max_descent);
21484 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21485 row->phys_height = max (row->phys_height,
21486 it->max_phys_ascent + it->max_phys_descent);
21487 row->extra_line_spacing = max (row->extra_line_spacing,
21488 it->max_extra_line_spacing);
21489 set_iterator_to_next (it, true);
21490 /* If we didn't handle the line/wrap prefix above, and the
21491 call to set_iterator_to_next just switched to TEXT_AREA,
21492 process the prefix now. */
21493 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21495 /* Line numbers should precede the line-prefix or wrap-prefix. */
21496 if (line_number_needed)
21497 maybe_produce_line_number (it);
21499 pending_handle_line_prefix = false;
21500 handle_line_prefix (it);
21502 continue;
21505 /* Does the display element fit on the line? If we truncate
21506 lines, we should draw past the right edge of the window. If
21507 we don't truncate, we want to stop so that we can display the
21508 continuation glyph before the right margin. If lines are
21509 continued, there are two possible strategies for characters
21510 resulting in more than 1 glyph (e.g. tabs): Display as many
21511 glyphs as possible in this line and leave the rest for the
21512 continuation line, or display the whole element in the next
21513 line. Original redisplay did the former, so we do it also. */
21514 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21515 hpos_before = it->hpos;
21516 x_before = x;
21518 if (/* Not a newline. */
21519 nglyphs > 0
21520 /* Glyphs produced fit entirely in the line. */
21521 && it->current_x < it->last_visible_x)
21523 it->hpos += nglyphs;
21524 row->ascent = max (row->ascent, it->max_ascent);
21525 row->height = max (row->height, it->max_ascent + it->max_descent);
21526 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21527 row->phys_height = max (row->phys_height,
21528 it->max_phys_ascent + it->max_phys_descent);
21529 row->extra_line_spacing = max (row->extra_line_spacing,
21530 it->max_extra_line_spacing);
21531 if (it->current_x - it->pixel_width < it->first_visible_x
21532 /* In R2L rows, we arrange in extend_face_to_end_of_line
21533 to add a right offset to the line, by a suitable
21534 change to the stretch glyph that is the leftmost
21535 glyph of the line. */
21536 && !row->reversed_p)
21537 row->x = x - it->first_visible_x;
21538 /* Record the maximum and minimum buffer positions seen so
21539 far in glyphs that will be displayed by this row. */
21540 if (it->bidi_p)
21541 RECORD_MAX_MIN_POS (it);
21543 else
21545 int i, new_x;
21546 struct glyph *glyph;
21548 for (i = 0; i < nglyphs; ++i, x = new_x)
21550 /* Identify the glyphs added by the last call to
21551 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21552 the previous glyphs. */
21553 if (!row->reversed_p)
21554 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21555 else
21556 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21557 new_x = x + glyph->pixel_width;
21559 if (/* Lines are continued. */
21560 it->line_wrap != TRUNCATE
21561 && (/* Glyph doesn't fit on the line. */
21562 new_x > it->last_visible_x
21563 /* Or it fits exactly on a window system frame. */
21564 || (new_x == it->last_visible_x
21565 && FRAME_WINDOW_P (it->f)
21566 && (row->reversed_p
21567 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21568 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21570 /* End of a continued line. */
21572 if (it->hpos == 0
21573 || (new_x == it->last_visible_x
21574 && FRAME_WINDOW_P (it->f)
21575 && (row->reversed_p
21576 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21577 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21579 /* Current glyph is the only one on the line or
21580 fits exactly on the line. We must continue
21581 the line because we can't draw the cursor
21582 after the glyph. */
21583 row->continued_p = true;
21584 it->current_x = new_x;
21585 it->continuation_lines_width += new_x;
21586 ++it->hpos;
21587 if (i == nglyphs - 1)
21589 /* If line-wrap is on, check if a previous
21590 wrap point was found. */
21591 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21592 && wrap_row_used > 0
21593 /* Even if there is a previous wrap
21594 point, continue the line here as
21595 usual, if (i) the previous character
21596 was a space or tab AND (ii) the
21597 current character is not. */
21598 && (!may_wrap
21599 || IT_DISPLAYING_WHITESPACE (it)))
21600 goto back_to_wrap;
21602 /* Record the maximum and minimum buffer
21603 positions seen so far in glyphs that will be
21604 displayed by this row. */
21605 if (it->bidi_p)
21606 RECORD_MAX_MIN_POS (it);
21607 set_iterator_to_next (it, true);
21608 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21610 if (!get_next_display_element (it))
21612 row->exact_window_width_line_p = true;
21613 it->continuation_lines_width = 0;
21614 it->font_height = Qnil;
21615 it->voffset = 0;
21616 row->continued_p = false;
21617 row->ends_at_zv_p = true;
21619 else if (ITERATOR_AT_END_OF_LINE_P (it))
21621 row->continued_p = false;
21622 row->exact_window_width_line_p = true;
21624 /* If line-wrap is on, check if a
21625 previous wrap point was found. */
21626 else if (wrap_row_used > 0
21627 /* Even if there is a previous wrap
21628 point, continue the line here as
21629 usual, if (i) the previous character
21630 was a space or tab AND (ii) the
21631 current character is not. */
21632 && (!may_wrap
21633 || IT_DISPLAYING_WHITESPACE (it)))
21634 goto back_to_wrap;
21638 else if (it->bidi_p)
21639 RECORD_MAX_MIN_POS (it);
21640 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21641 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21642 extend_face_to_end_of_line (it);
21644 else if (CHAR_GLYPH_PADDING_P (*glyph)
21645 && !FRAME_WINDOW_P (it->f))
21647 /* A padding glyph that doesn't fit on this line.
21648 This means the whole character doesn't fit
21649 on the line. */
21650 if (row->reversed_p)
21651 unproduce_glyphs (it, row->used[TEXT_AREA]
21652 - n_glyphs_before);
21653 row->used[TEXT_AREA] = n_glyphs_before;
21655 /* Fill the rest of the row with continuation
21656 glyphs like in 20.x. */
21657 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21658 < row->glyphs[1 + TEXT_AREA])
21659 produce_special_glyphs (it, IT_CONTINUATION);
21661 row->continued_p = true;
21662 it->current_x = x_before;
21663 it->continuation_lines_width += x_before;
21665 /* Restore the height to what it was before the
21666 element not fitting on the line. */
21667 it->max_ascent = ascent;
21668 it->max_descent = descent;
21669 it->max_phys_ascent = phys_ascent;
21670 it->max_phys_descent = phys_descent;
21671 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21672 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21673 extend_face_to_end_of_line (it);
21675 else if (wrap_row_used > 0)
21677 back_to_wrap:
21678 if (row->reversed_p)
21679 unproduce_glyphs (it,
21680 row->used[TEXT_AREA] - wrap_row_used);
21681 RESTORE_IT (it, &wrap_it, wrap_data);
21682 it->continuation_lines_width += wrap_x;
21683 row->used[TEXT_AREA] = wrap_row_used;
21684 row->ascent = wrap_row_ascent;
21685 row->height = wrap_row_height;
21686 row->phys_ascent = wrap_row_phys_ascent;
21687 row->phys_height = wrap_row_phys_height;
21688 row->extra_line_spacing = wrap_row_extra_line_spacing;
21689 min_pos = wrap_row_min_pos;
21690 min_bpos = wrap_row_min_bpos;
21691 max_pos = wrap_row_max_pos;
21692 max_bpos = wrap_row_max_bpos;
21693 row->continued_p = true;
21694 row->ends_at_zv_p = false;
21695 row->exact_window_width_line_p = false;
21697 /* Make sure that a non-default face is extended
21698 up to the right margin of the window. */
21699 extend_face_to_end_of_line (it);
21701 else if ((it->what == IT_CHARACTER
21702 || it->what == IT_STRETCH
21703 || it->what == IT_COMPOSITION)
21704 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21706 /* A TAB that extends past the right edge of the
21707 window. This produces a single glyph on
21708 window system frames. We leave the glyph in
21709 this row and let it fill the row, but don't
21710 consume the TAB. */
21711 if ((row->reversed_p
21712 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21713 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21714 produce_special_glyphs (it, IT_CONTINUATION);
21715 it->continuation_lines_width += it->last_visible_x;
21716 row->ends_in_middle_of_char_p = true;
21717 row->continued_p = true;
21718 glyph->pixel_width = it->last_visible_x - x;
21719 it->starts_in_middle_of_char_p = true;
21720 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21721 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21722 extend_face_to_end_of_line (it);
21724 else
21726 /* Something other than a TAB that draws past
21727 the right edge of the window. Restore
21728 positions to values before the element. */
21729 if (row->reversed_p)
21730 unproduce_glyphs (it, row->used[TEXT_AREA]
21731 - (n_glyphs_before + i));
21732 row->used[TEXT_AREA] = n_glyphs_before + i;
21734 /* Display continuation glyphs. */
21735 it->current_x = x_before;
21736 it->continuation_lines_width += x;
21737 if (!FRAME_WINDOW_P (it->f)
21738 || (row->reversed_p
21739 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21740 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21741 produce_special_glyphs (it, IT_CONTINUATION);
21742 row->continued_p = true;
21744 extend_face_to_end_of_line (it);
21746 if (nglyphs > 1 && i > 0)
21748 row->ends_in_middle_of_char_p = true;
21749 it->starts_in_middle_of_char_p = true;
21752 /* Restore the height to what it was before the
21753 element not fitting on the line. */
21754 it->max_ascent = ascent;
21755 it->max_descent = descent;
21756 it->max_phys_ascent = phys_ascent;
21757 it->max_phys_descent = phys_descent;
21760 break;
21762 else if (new_x > it->first_visible_x)
21764 /* Increment number of glyphs actually displayed. */
21765 ++it->hpos;
21767 /* Record the maximum and minimum buffer positions
21768 seen so far in glyphs that will be displayed by
21769 this row. */
21770 if (it->bidi_p)
21771 RECORD_MAX_MIN_POS (it);
21773 if (x < it->first_visible_x && !row->reversed_p)
21774 /* Glyph is partially visible, i.e. row starts at
21775 negative X position. Don't do that in R2L
21776 rows, where we arrange to add a right offset to
21777 the line in extend_face_to_end_of_line, by a
21778 suitable change to the stretch glyph that is
21779 the leftmost glyph of the line. */
21780 row->x = x - it->first_visible_x;
21781 /* When the last glyph of an R2L row only fits
21782 partially on the line, we need to set row->x to a
21783 negative offset, so that the leftmost glyph is
21784 the one that is partially visible. But if we are
21785 going to produce the truncation glyph, this will
21786 be taken care of in produce_special_glyphs. */
21787 if (row->reversed_p
21788 && new_x > it->last_visible_x
21789 && !(it->line_wrap == TRUNCATE
21790 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21792 eassert (FRAME_WINDOW_P (it->f));
21793 row->x = it->last_visible_x - new_x;
21796 else
21798 /* Glyph is completely off the left margin of the
21799 window. This should not happen because of the
21800 move_it_in_display_line at the start of this
21801 function, unless the text display area of the
21802 window is empty. */
21803 eassert (it->first_visible_x <= it->last_visible_x);
21806 /* Even if this display element produced no glyphs at all,
21807 we want to record its position. */
21808 if (it->bidi_p && nglyphs == 0)
21809 RECORD_MAX_MIN_POS (it);
21811 row->ascent = max (row->ascent, it->max_ascent);
21812 row->height = max (row->height, it->max_ascent + it->max_descent);
21813 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21814 row->phys_height = max (row->phys_height,
21815 it->max_phys_ascent + it->max_phys_descent);
21816 row->extra_line_spacing = max (row->extra_line_spacing,
21817 it->max_extra_line_spacing);
21819 /* End of this display line if row is continued. */
21820 if (row->continued_p || row->ends_at_zv_p)
21821 break;
21824 at_end_of_line:
21825 /* Is this a line end? If yes, we're also done, after making
21826 sure that a non-default face is extended up to the right
21827 margin of the window. */
21828 if (ITERATOR_AT_END_OF_LINE_P (it))
21830 int used_before = row->used[TEXT_AREA];
21832 row->ends_in_newline_from_string_p = STRINGP (it->object);
21834 /* Add a space at the end of the line that is used to
21835 display the cursor there. */
21836 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21837 append_space_for_newline (it, false);
21839 /* Extend the face to the end of the line. */
21840 extend_face_to_end_of_line (it);
21842 /* Make sure we have the position. */
21843 if (used_before == 0)
21844 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21846 /* Record the position of the newline, for use in
21847 find_row_edges. */
21848 it->eol_pos = it->current.pos;
21850 /* Consume the line end. This skips over invisible lines. */
21851 set_iterator_to_next (it, true);
21852 it->continuation_lines_width = 0;
21853 break;
21856 /* Proceed with next display element. Note that this skips
21857 over lines invisible because of selective display. */
21858 set_iterator_to_next (it, true);
21860 /* If we truncate lines, we are done when the last displayed
21861 glyphs reach past the right margin of the window. */
21862 if (it->line_wrap == TRUNCATE
21863 && ((FRAME_WINDOW_P (it->f)
21864 /* Images are preprocessed in produce_image_glyph such
21865 that they are cropped at the right edge of the
21866 window, so an image glyph will always end exactly at
21867 last_visible_x, even if there's no right fringe. */
21868 && ((row->reversed_p
21869 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21870 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21871 || it->what == IT_IMAGE))
21872 ? (it->current_x >= it->last_visible_x)
21873 : (it->current_x > it->last_visible_x)))
21875 /* Maybe add truncation glyphs. */
21876 if (!FRAME_WINDOW_P (it->f)
21877 || (row->reversed_p
21878 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21879 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21881 int i, n;
21883 if (!row->reversed_p)
21885 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21886 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21887 break;
21889 else
21891 for (i = 0; i < row->used[TEXT_AREA]; i++)
21892 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21893 break;
21894 /* Remove any padding glyphs at the front of ROW, to
21895 make room for the truncation glyphs we will be
21896 adding below. The loop below always inserts at
21897 least one truncation glyph, so also remove the
21898 last glyph added to ROW. */
21899 unproduce_glyphs (it, i + 1);
21900 /* Adjust i for the loop below. */
21901 i = row->used[TEXT_AREA] - (i + 1);
21904 /* produce_special_glyphs overwrites the last glyph, so
21905 we don't want that if we want to keep that last
21906 glyph, which means it's an image. */
21907 if (it->current_x > it->last_visible_x)
21909 it->current_x = x_before;
21910 if (!FRAME_WINDOW_P (it->f))
21912 for (n = row->used[TEXT_AREA]; i < n; ++i)
21914 row->used[TEXT_AREA] = i;
21915 produce_special_glyphs (it, IT_TRUNCATION);
21918 else
21920 row->used[TEXT_AREA] = i;
21921 produce_special_glyphs (it, IT_TRUNCATION);
21923 it->hpos = hpos_before;
21926 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21928 /* Don't truncate if we can overflow newline into fringe. */
21929 if (!get_next_display_element (it))
21931 it->continuation_lines_width = 0;
21932 it->font_height = Qnil;
21933 it->voffset = 0;
21934 row->ends_at_zv_p = true;
21935 row->exact_window_width_line_p = true;
21936 break;
21938 if (ITERATOR_AT_END_OF_LINE_P (it))
21940 row->exact_window_width_line_p = true;
21941 goto at_end_of_line;
21943 it->current_x = x_before;
21944 it->hpos = hpos_before;
21947 row->truncated_on_right_p = true;
21948 it->continuation_lines_width = 0;
21949 reseat_at_next_visible_line_start (it, false);
21950 /* We insist below that IT's position be at ZV because in
21951 bidi-reordered lines the character at visible line start
21952 might not be the character that follows the newline in
21953 the logical order. */
21954 if (IT_BYTEPOS (*it) > BEG_BYTE)
21955 row->ends_at_zv_p =
21956 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21957 else
21958 row->ends_at_zv_p = false;
21959 break;
21963 if (wrap_data)
21964 bidi_unshelve_cache (wrap_data, true);
21966 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21967 at the left window margin. */
21968 if (it->first_visible_x
21969 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21971 if (!FRAME_WINDOW_P (it->f)
21972 || (((row->reversed_p
21973 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21974 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21975 /* Don't let insert_left_trunc_glyphs overwrite the
21976 first glyph of the row if it is an image. */
21977 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21978 insert_left_trunc_glyphs (it);
21979 row->truncated_on_left_p = true;
21982 /* Remember the position at which this line ends.
21984 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21985 cannot be before the call to find_row_edges below, since that is
21986 where these positions are determined. */
21987 row->end = it->current;
21988 if (!it->bidi_p)
21990 row->minpos = row->start.pos;
21991 row->maxpos = row->end.pos;
21993 else
21995 /* ROW->minpos and ROW->maxpos must be the smallest and
21996 `1 + the largest' buffer positions in ROW. But if ROW was
21997 bidi-reordered, these two positions can be anywhere in the
21998 row, so we must determine them now. */
21999 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
22002 /* If the start of this line is the overlay arrow-position, then
22003 mark this glyph row as the one containing the overlay arrow.
22004 This is clearly a mess with variable size fonts. It would be
22005 better to let it be displayed like cursors under X. */
22006 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22007 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22008 !NILP (overlay_arrow_string)))
22010 /* Overlay arrow in window redisplay is a fringe bitmap. */
22011 if (STRINGP (overlay_arrow_string))
22013 struct glyph_row *arrow_row
22014 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22015 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22016 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22017 struct glyph *p = row->glyphs[TEXT_AREA];
22018 struct glyph *p2, *end;
22020 /* Copy the arrow glyphs. */
22021 while (glyph < arrow_end)
22022 *p++ = *glyph++;
22024 /* Throw away padding glyphs. */
22025 p2 = p;
22026 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22027 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22028 ++p2;
22029 if (p2 > p)
22031 while (p2 < end)
22032 *p++ = *p2++;
22033 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22036 else
22038 eassert (INTEGERP (overlay_arrow_string));
22039 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22041 overlay_arrow_seen = true;
22044 /* Highlight trailing whitespace. */
22045 if (!NILP (Vshow_trailing_whitespace))
22046 highlight_trailing_whitespace (it->f, it->glyph_row);
22048 /* Compute pixel dimensions of this line. */
22049 compute_line_metrics (it);
22051 /* Implementation note: No changes in the glyphs of ROW or in their
22052 faces can be done past this point, because compute_line_metrics
22053 computes ROW's hash value and stores it within the glyph_row
22054 structure. */
22056 /* Record whether this row ends inside an ellipsis. */
22057 row->ends_in_ellipsis_p
22058 = (it->method == GET_FROM_DISPLAY_VECTOR
22059 && it->ellipsis_p);
22061 /* Save fringe bitmaps in this row. */
22062 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22063 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22064 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22065 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22067 it->left_user_fringe_bitmap = 0;
22068 it->left_user_fringe_face_id = 0;
22069 it->right_user_fringe_bitmap = 0;
22070 it->right_user_fringe_face_id = 0;
22072 /* Maybe set the cursor. */
22073 cvpos = it->w->cursor.vpos;
22074 if ((cvpos < 0
22075 /* In bidi-reordered rows, keep checking for proper cursor
22076 position even if one has been found already, because buffer
22077 positions in such rows change non-linearly with ROW->VPOS,
22078 when a line is continued. One exception: when we are at ZV,
22079 display cursor on the first suitable glyph row, since all
22080 the empty rows after that also have their position set to ZV. */
22081 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22082 lines' rows is implemented for bidi-reordered rows. */
22083 || (it->bidi_p
22084 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22085 && PT >= MATRIX_ROW_START_CHARPOS (row)
22086 && PT <= MATRIX_ROW_END_CHARPOS (row)
22087 && cursor_row_p (row))
22088 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22090 /* Prepare for the next line. This line starts horizontally at (X
22091 HPOS) = (0 0). Vertical positions are incremented. As a
22092 convenience for the caller, IT->glyph_row is set to the next
22093 row to be used. */
22094 it->current_x = it->hpos = 0;
22095 it->current_y += row->height;
22096 /* Restore the first and last visible X if we adjusted them for
22097 current-line hscrolling. */
22098 if (hscroll_this_line)
22100 it->first_visible_x = first_visible_x;
22101 it->last_visible_x = last_visible_x;
22103 SET_TEXT_POS (it->eol_pos, 0, 0);
22104 ++it->vpos;
22105 ++it->glyph_row;
22106 /* The next row should by default use the same value of the
22107 reversed_p flag as this one. set_iterator_to_next decides when
22108 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22109 the flag accordingly. */
22110 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22111 it->glyph_row->reversed_p = row->reversed_p;
22112 it->start = row->end;
22113 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22115 #undef RECORD_MAX_MIN_POS
22118 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22119 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22120 doc: /* Return paragraph direction at point in BUFFER.
22121 Value is either `left-to-right' or `right-to-left'.
22122 If BUFFER is omitted or nil, it defaults to the current buffer.
22124 Paragraph direction determines how the text in the paragraph is displayed.
22125 In left-to-right paragraphs, text begins at the left margin of the window
22126 and the reading direction is generally left to right. In right-to-left
22127 paragraphs, text begins at the right margin and is read from right to left.
22129 See also `bidi-paragraph-direction'. */)
22130 (Lisp_Object buffer)
22132 struct buffer *buf = current_buffer;
22133 struct buffer *old = buf;
22135 if (! NILP (buffer))
22137 CHECK_BUFFER (buffer);
22138 buf = XBUFFER (buffer);
22141 if (NILP (BVAR (buf, bidi_display_reordering))
22142 || NILP (BVAR (buf, enable_multibyte_characters))
22143 /* When we are loading loadup.el, the character property tables
22144 needed for bidi iteration are not yet available. */
22145 || redisplay__inhibit_bidi)
22146 return Qleft_to_right;
22147 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22148 return BVAR (buf, bidi_paragraph_direction);
22149 else
22151 /* Determine the direction from buffer text. We could try to
22152 use current_matrix if it is up to date, but this seems fast
22153 enough as it is. */
22154 struct bidi_it itb;
22155 ptrdiff_t pos = BUF_PT (buf);
22156 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22157 int c;
22158 void *itb_data = bidi_shelve_cache ();
22160 set_buffer_temp (buf);
22161 /* bidi_paragraph_init finds the base direction of the paragraph
22162 by searching forward from paragraph start. We need the base
22163 direction of the current or _previous_ paragraph, so we need
22164 to make sure we are within that paragraph. To that end, find
22165 the previous non-empty line. */
22166 if (pos >= ZV && pos > BEGV)
22167 DEC_BOTH (pos, bytepos);
22168 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22169 if (fast_looking_at (trailing_white_space,
22170 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22172 while ((c = FETCH_BYTE (bytepos)) == '\n'
22173 || c == ' ' || c == '\t' || c == '\f')
22175 if (bytepos <= BEGV_BYTE)
22176 break;
22177 bytepos--;
22178 pos--;
22180 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22181 bytepos--;
22183 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22184 itb.paragraph_dir = NEUTRAL_DIR;
22185 itb.string.s = NULL;
22186 itb.string.lstring = Qnil;
22187 itb.string.bufpos = 0;
22188 itb.string.from_disp_str = false;
22189 itb.string.unibyte = false;
22190 /* We have no window to use here for ignoring window-specific
22191 overlays. Using NULL for window pointer will cause
22192 compute_display_string_pos to use the current buffer. */
22193 itb.w = NULL;
22194 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22195 bidi_unshelve_cache (itb_data, false);
22196 set_buffer_temp (old);
22197 switch (itb.paragraph_dir)
22199 case L2R:
22200 return Qleft_to_right;
22201 break;
22202 case R2L:
22203 return Qright_to_left;
22204 break;
22205 default:
22206 emacs_abort ();
22211 DEFUN ("bidi-find-overridden-directionality",
22212 Fbidi_find_overridden_directionality,
22213 Sbidi_find_overridden_directionality, 2, 3, 0,
22214 doc: /* Return position between FROM and TO where directionality was overridden.
22216 This function returns the first character position in the specified
22217 region of OBJECT where there is a character whose `bidi-class' property
22218 is `L', but which was forced to display as `R' by a directional
22219 override, and likewise with characters whose `bidi-class' is `R'
22220 or `AL' that were forced to display as `L'.
22222 If no such character is found, the function returns nil.
22224 OBJECT is a Lisp string or buffer to search for overridden
22225 directionality, and defaults to the current buffer if nil or omitted.
22226 OBJECT can also be a window, in which case the function will search
22227 the buffer displayed in that window. Passing the window instead of
22228 a buffer is preferable when the buffer is displayed in some window,
22229 because this function will then be able to correctly account for
22230 window-specific overlays, which can affect the results.
22232 Strong directional characters `L', `R', and `AL' can have their
22233 intrinsic directionality overridden by directional override
22234 control characters RLO (u+202e) and LRO (u+202d). See the
22235 function `get-char-code-property' for a way to inquire about
22236 the `bidi-class' property of a character. */)
22237 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22239 struct buffer *buf = current_buffer;
22240 struct buffer *old = buf;
22241 struct window *w = NULL;
22242 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22243 struct bidi_it itb;
22244 ptrdiff_t from_pos, to_pos, from_bpos;
22245 void *itb_data;
22247 if (!NILP (object))
22249 if (BUFFERP (object))
22250 buf = XBUFFER (object);
22251 else if (WINDOWP (object))
22253 w = decode_live_window (object);
22254 buf = XBUFFER (w->contents);
22255 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22257 else
22258 CHECK_STRING (object);
22261 if (STRINGP (object))
22263 /* Characters in unibyte strings are always treated by bidi.c as
22264 strong LTR. */
22265 if (!STRING_MULTIBYTE (object)
22266 /* When we are loading loadup.el, the character property
22267 tables needed for bidi iteration are not yet
22268 available. */
22269 || redisplay__inhibit_bidi)
22270 return Qnil;
22272 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22273 if (from_pos >= SCHARS (object))
22274 return Qnil;
22276 /* Set up the bidi iterator. */
22277 itb_data = bidi_shelve_cache ();
22278 itb.paragraph_dir = NEUTRAL_DIR;
22279 itb.string.lstring = object;
22280 itb.string.s = NULL;
22281 itb.string.schars = SCHARS (object);
22282 itb.string.bufpos = 0;
22283 itb.string.from_disp_str = false;
22284 itb.string.unibyte = false;
22285 itb.w = w;
22286 bidi_init_it (0, 0, frame_window_p, &itb);
22288 else
22290 /* Nothing this fancy can happen in unibyte buffers, or in a
22291 buffer that disabled reordering, or if FROM is at EOB. */
22292 if (NILP (BVAR (buf, bidi_display_reordering))
22293 || NILP (BVAR (buf, enable_multibyte_characters))
22294 /* When we are loading loadup.el, the character property
22295 tables needed for bidi iteration are not yet
22296 available. */
22297 || redisplay__inhibit_bidi)
22298 return Qnil;
22300 set_buffer_temp (buf);
22301 validate_region (&from, &to);
22302 from_pos = XINT (from);
22303 to_pos = XINT (to);
22304 if (from_pos >= ZV)
22305 return Qnil;
22307 /* Set up the bidi iterator. */
22308 itb_data = bidi_shelve_cache ();
22309 from_bpos = CHAR_TO_BYTE (from_pos);
22310 if (from_pos == BEGV)
22312 itb.charpos = BEGV;
22313 itb.bytepos = BEGV_BYTE;
22315 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22317 itb.charpos = from_pos;
22318 itb.bytepos = from_bpos;
22320 else
22321 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22322 -1, &itb.bytepos);
22323 itb.paragraph_dir = NEUTRAL_DIR;
22324 itb.string.s = NULL;
22325 itb.string.lstring = Qnil;
22326 itb.string.bufpos = 0;
22327 itb.string.from_disp_str = false;
22328 itb.string.unibyte = false;
22329 itb.w = w;
22330 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22333 ptrdiff_t found;
22334 do {
22335 /* For the purposes of this function, the actual base direction of
22336 the paragraph doesn't matter, so just set it to L2R. */
22337 bidi_paragraph_init (L2R, &itb, false);
22338 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22340 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22342 bidi_unshelve_cache (itb_data, false);
22343 set_buffer_temp (old);
22345 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22348 DEFUN ("move-point-visually", Fmove_point_visually,
22349 Smove_point_visually, 1, 1, 0,
22350 doc: /* Move point in the visual order in the specified DIRECTION.
22351 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22352 left.
22354 Value is the new character position of point. */)
22355 (Lisp_Object direction)
22357 struct window *w = XWINDOW (selected_window);
22358 struct buffer *b = XBUFFER (w->contents);
22359 struct glyph_row *row;
22360 int dir;
22361 Lisp_Object paragraph_dir;
22363 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22364 (!(ROW)->continued_p \
22365 && NILP ((GLYPH)->object) \
22366 && (GLYPH)->type == CHAR_GLYPH \
22367 && (GLYPH)->u.ch == ' ' \
22368 && (GLYPH)->charpos >= 0 \
22369 && !(GLYPH)->avoid_cursor_p)
22371 CHECK_NUMBER (direction);
22372 dir = XINT (direction);
22373 if (dir > 0)
22374 dir = 1;
22375 else
22376 dir = -1;
22378 /* If current matrix is up-to-date, we can use the information
22379 recorded in the glyphs, at least as long as the goal is on the
22380 screen. */
22381 if (w->window_end_valid
22382 && !windows_or_buffers_changed
22383 && b
22384 && !b->clip_changed
22385 && !b->prevent_redisplay_optimizations_p
22386 && !window_outdated (w)
22387 /* We rely below on the cursor coordinates to be up to date, but
22388 we cannot trust them if some command moved point since the
22389 last complete redisplay. */
22390 && w->last_point == BUF_PT (b)
22391 && w->cursor.vpos >= 0
22392 && w->cursor.vpos < w->current_matrix->nrows
22393 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22395 struct glyph *g = row->glyphs[TEXT_AREA];
22396 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22397 struct glyph *gpt = g + w->cursor.hpos;
22399 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22401 if (BUFFERP (g->object) && g->charpos != PT)
22403 SET_PT (g->charpos);
22404 w->cursor.vpos = -1;
22405 return make_number (PT);
22407 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22409 ptrdiff_t new_pos;
22411 if (BUFFERP (gpt->object))
22413 new_pos = PT;
22414 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22415 new_pos += (row->reversed_p ? -dir : dir);
22416 else
22417 new_pos -= (row->reversed_p ? -dir : dir);
22419 else if (BUFFERP (g->object))
22420 new_pos = g->charpos;
22421 else
22422 break;
22423 SET_PT (new_pos);
22424 w->cursor.vpos = -1;
22425 return make_number (PT);
22427 else if (ROW_GLYPH_NEWLINE_P (row, g))
22429 /* Glyphs inserted at the end of a non-empty line for
22430 positioning the cursor have zero charpos, so we must
22431 deduce the value of point by other means. */
22432 if (g->charpos > 0)
22433 SET_PT (g->charpos);
22434 else if (row->ends_at_zv_p && PT != ZV)
22435 SET_PT (ZV);
22436 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22437 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22438 else
22439 break;
22440 w->cursor.vpos = -1;
22441 return make_number (PT);
22444 if (g == e || NILP (g->object))
22446 if (row->truncated_on_left_p || row->truncated_on_right_p)
22447 goto simulate_display;
22448 if (!row->reversed_p)
22449 row += dir;
22450 else
22451 row -= dir;
22452 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22453 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22454 goto simulate_display;
22456 if (dir > 0)
22458 if (row->reversed_p && !row->continued_p)
22460 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22461 w->cursor.vpos = -1;
22462 return make_number (PT);
22464 g = row->glyphs[TEXT_AREA];
22465 e = g + row->used[TEXT_AREA];
22466 for ( ; g < e; g++)
22468 if (BUFFERP (g->object)
22469 /* Empty lines have only one glyph, which stands
22470 for the newline, and whose charpos is the
22471 buffer position of the newline. */
22472 || ROW_GLYPH_NEWLINE_P (row, g)
22473 /* When the buffer ends in a newline, the line at
22474 EOB also has one glyph, but its charpos is -1. */
22475 || (row->ends_at_zv_p
22476 && !row->reversed_p
22477 && NILP (g->object)
22478 && g->type == CHAR_GLYPH
22479 && g->u.ch == ' '))
22481 if (g->charpos > 0)
22482 SET_PT (g->charpos);
22483 else if (!row->reversed_p
22484 && row->ends_at_zv_p
22485 && PT != ZV)
22486 SET_PT (ZV);
22487 else
22488 continue;
22489 w->cursor.vpos = -1;
22490 return make_number (PT);
22494 else
22496 if (!row->reversed_p && !row->continued_p)
22498 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22499 w->cursor.vpos = -1;
22500 return make_number (PT);
22502 e = row->glyphs[TEXT_AREA];
22503 g = e + row->used[TEXT_AREA] - 1;
22504 for ( ; g >= e; g--)
22506 if (BUFFERP (g->object)
22507 || (ROW_GLYPH_NEWLINE_P (row, g)
22508 && g->charpos > 0)
22509 /* Empty R2L lines on GUI frames have the buffer
22510 position of the newline stored in the stretch
22511 glyph. */
22512 || g->type == STRETCH_GLYPH
22513 || (row->ends_at_zv_p
22514 && row->reversed_p
22515 && NILP (g->object)
22516 && g->type == CHAR_GLYPH
22517 && g->u.ch == ' '))
22519 if (g->charpos > 0)
22520 SET_PT (g->charpos);
22521 else if (row->reversed_p
22522 && row->ends_at_zv_p
22523 && PT != ZV)
22524 SET_PT (ZV);
22525 else
22526 continue;
22527 w->cursor.vpos = -1;
22528 return make_number (PT);
22535 simulate_display:
22537 /* If we wind up here, we failed to move by using the glyphs, so we
22538 need to simulate display instead. */
22540 if (b)
22541 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22542 else
22543 paragraph_dir = Qleft_to_right;
22544 if (EQ (paragraph_dir, Qright_to_left))
22545 dir = -dir;
22546 if (PT <= BEGV && dir < 0)
22547 xsignal0 (Qbeginning_of_buffer);
22548 else if (PT >= ZV && dir > 0)
22549 xsignal0 (Qend_of_buffer);
22550 else
22552 struct text_pos pt;
22553 struct it it;
22554 int pt_x, target_x, pixel_width, pt_vpos;
22555 bool at_eol_p;
22556 bool overshoot_expected = false;
22557 bool target_is_eol_p = false;
22559 /* Setup the arena. */
22560 SET_TEXT_POS (pt, PT, PT_BYTE);
22561 start_display (&it, w, pt);
22562 /* When lines are truncated, we could be called with point
22563 outside of the windows edges, in which case move_it_*
22564 functions either prematurely stop at window's edge or jump to
22565 the next screen line, whereas we rely below on our ability to
22566 reach point, in order to start from its X coordinate. So we
22567 need to disregard the window's horizontal extent in that case. */
22568 if (it.line_wrap == TRUNCATE)
22569 it.last_visible_x = DISP_INFINITY;
22571 if (it.cmp_it.id < 0
22572 && it.method == GET_FROM_STRING
22573 && it.area == TEXT_AREA
22574 && it.string_from_display_prop_p
22575 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22576 overshoot_expected = true;
22578 /* Find the X coordinate of point. We start from the beginning
22579 of this or previous line to make sure we are before point in
22580 the logical order (since the move_it_* functions can only
22581 move forward). */
22582 reseat:
22583 reseat_at_previous_visible_line_start (&it);
22584 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22585 if (IT_CHARPOS (it) != PT)
22587 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22588 -1, -1, -1, MOVE_TO_POS);
22589 /* If we missed point because the character there is
22590 displayed out of a display vector that has more than one
22591 glyph, retry expecting overshoot. */
22592 if (it.method == GET_FROM_DISPLAY_VECTOR
22593 && it.current.dpvec_index > 0
22594 && !overshoot_expected)
22596 overshoot_expected = true;
22597 goto reseat;
22599 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22600 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22602 pt_x = it.current_x;
22603 pt_vpos = it.vpos;
22604 if (dir > 0 || overshoot_expected)
22606 struct glyph_row *row = it.glyph_row;
22608 /* When point is at beginning of line, we don't have
22609 information about the glyph there loaded into struct
22610 it. Calling get_next_display_element fixes that. */
22611 if (pt_x == 0)
22612 get_next_display_element (&it);
22613 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22614 it.glyph_row = NULL;
22615 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22616 it.glyph_row = row;
22617 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22618 it, lest it will become out of sync with it's buffer
22619 position. */
22620 it.current_x = pt_x;
22622 else
22623 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22624 pixel_width = it.pixel_width;
22625 if (overshoot_expected && at_eol_p)
22626 pixel_width = 0;
22627 else if (pixel_width <= 0)
22628 pixel_width = 1;
22630 /* If there's a display string (or something similar) at point,
22631 we are actually at the glyph to the left of point, so we need
22632 to correct the X coordinate. */
22633 if (overshoot_expected)
22635 if (it.bidi_p)
22636 pt_x += pixel_width * it.bidi_it.scan_dir;
22637 else
22638 pt_x += pixel_width;
22641 /* Compute target X coordinate, either to the left or to the
22642 right of point. On TTY frames, all characters have the same
22643 pixel width of 1, so we can use that. On GUI frames we don't
22644 have an easy way of getting at the pixel width of the
22645 character to the left of point, so we use a different method
22646 of getting to that place. */
22647 if (dir > 0)
22648 target_x = pt_x + pixel_width;
22649 else
22650 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22652 /* Target X coordinate could be one line above or below the line
22653 of point, in which case we need to adjust the target X
22654 coordinate. Also, if moving to the left, we need to begin at
22655 the left edge of the point's screen line. */
22656 if (dir < 0)
22658 if (pt_x > 0)
22660 start_display (&it, w, pt);
22661 if (it.line_wrap == TRUNCATE)
22662 it.last_visible_x = DISP_INFINITY;
22663 reseat_at_previous_visible_line_start (&it);
22664 it.current_x = it.current_y = it.hpos = 0;
22665 if (pt_vpos != 0)
22666 move_it_by_lines (&it, pt_vpos);
22668 else
22670 move_it_by_lines (&it, -1);
22671 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22672 target_is_eol_p = true;
22673 /* Under word-wrap, we don't know the x coordinate of
22674 the last character displayed on the previous line,
22675 which immediately precedes the wrap point. To find
22676 out its x coordinate, we try moving to the right
22677 margin of the window, which will stop at the wrap
22678 point, and then reset target_x to point at the
22679 character that precedes the wrap point. This is not
22680 needed on GUI frames, because (see below) there we
22681 move from the left margin one grapheme cluster at a
22682 time, and stop when we hit the wrap point. */
22683 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22685 void *it_data = NULL;
22686 struct it it2;
22688 SAVE_IT (it2, it, it_data);
22689 move_it_in_display_line_to (&it, ZV, target_x,
22690 MOVE_TO_POS | MOVE_TO_X);
22691 /* If we arrived at target_x, that _is_ the last
22692 character on the previous line. */
22693 if (it.current_x != target_x)
22694 target_x = it.current_x - 1;
22695 RESTORE_IT (&it, &it2, it_data);
22699 else
22701 if (at_eol_p
22702 || (target_x >= it.last_visible_x
22703 && it.line_wrap != TRUNCATE))
22705 if (pt_x > 0)
22706 move_it_by_lines (&it, 0);
22707 move_it_by_lines (&it, 1);
22708 target_x = 0;
22712 /* Move to the target X coordinate. */
22713 /* On GUI frames, as we don't know the X coordinate of the
22714 character to the left of point, moving point to the left
22715 requires walking, one grapheme cluster at a time, until we
22716 find ourself at a place immediately to the left of the
22717 character at point. */
22718 if (FRAME_WINDOW_P (it.f) && dir < 0)
22720 struct text_pos new_pos;
22721 enum move_it_result rc = MOVE_X_REACHED;
22723 if (it.current_x == 0)
22724 get_next_display_element (&it);
22725 if (it.what == IT_COMPOSITION)
22727 new_pos.charpos = it.cmp_it.charpos;
22728 new_pos.bytepos = -1;
22730 else
22731 new_pos = it.current.pos;
22733 while (it.current_x + it.pixel_width <= target_x
22734 && (rc == MOVE_X_REACHED
22735 /* Under word-wrap, move_it_in_display_line_to
22736 stops at correct coordinates, but sometimes
22737 returns MOVE_POS_MATCH_OR_ZV. */
22738 || (it.line_wrap == WORD_WRAP
22739 && rc == MOVE_POS_MATCH_OR_ZV)))
22741 int new_x = it.current_x + it.pixel_width;
22743 /* For composed characters, we want the position of the
22744 first character in the grapheme cluster (usually, the
22745 composition's base character), whereas it.current
22746 might give us the position of the _last_ one, e.g. if
22747 the composition is rendered in reverse due to bidi
22748 reordering. */
22749 if (it.what == IT_COMPOSITION)
22751 new_pos.charpos = it.cmp_it.charpos;
22752 new_pos.bytepos = -1;
22754 else
22755 new_pos = it.current.pos;
22756 if (new_x == it.current_x)
22757 new_x++;
22758 rc = move_it_in_display_line_to (&it, ZV, new_x,
22759 MOVE_TO_POS | MOVE_TO_X);
22760 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22761 break;
22763 /* The previous position we saw in the loop is the one we
22764 want. */
22765 if (new_pos.bytepos == -1)
22766 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22767 it.current.pos = new_pos;
22769 else if (it.current_x != target_x)
22770 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22772 /* If we ended up in a display string that covers point, move to
22773 buffer position to the right in the visual order. */
22774 if (dir > 0)
22776 while (IT_CHARPOS (it) == PT)
22778 set_iterator_to_next (&it, false);
22779 if (!get_next_display_element (&it))
22780 break;
22784 /* Move point to that position. */
22785 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22788 return make_number (PT);
22790 #undef ROW_GLYPH_NEWLINE_P
22793 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22794 Sbidi_resolved_levels, 0, 1, 0,
22795 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22797 The resolved levels are produced by the Emacs bidi reordering engine
22798 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22799 read the Unicode Standard Annex 9 (UAX#9) for background information
22800 about these levels.
22802 VPOS is the zero-based number of the current window's screen line
22803 for which to produce the resolved levels. If VPOS is nil or omitted,
22804 it defaults to the screen line of point. If the window displays a
22805 header line, VPOS of zero will report on the header line, and first
22806 line of text in the window will have VPOS of 1.
22808 Value is an array of resolved levels, indexed by glyph number.
22809 Glyphs are numbered from zero starting from the beginning of the
22810 screen line, i.e. the left edge of the window for left-to-right lines
22811 and from the right edge for right-to-left lines. The resolved levels
22812 are produced only for the window's text area; text in display margins
22813 is not included.
22815 If the selected window's display is not up-to-date, or if the specified
22816 screen line does not display text, this function returns nil. It is
22817 highly recommended to bind this function to some simple key, like F8,
22818 in order to avoid these problems.
22820 This function exists mainly for testing the correctness of the
22821 Emacs UBA implementation, in particular with the test suite. */)
22822 (Lisp_Object vpos)
22824 struct window *w = XWINDOW (selected_window);
22825 struct buffer *b = XBUFFER (w->contents);
22826 int nrow;
22827 struct glyph_row *row;
22829 if (NILP (vpos))
22831 int d1, d2, d3, d4, d5;
22833 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22835 else
22837 CHECK_NUMBER_COERCE_MARKER (vpos);
22838 nrow = XINT (vpos);
22841 /* We require up-to-date glyph matrix for this window. */
22842 if (w->window_end_valid
22843 && !windows_or_buffers_changed
22844 && b
22845 && !b->clip_changed
22846 && !b->prevent_redisplay_optimizations_p
22847 && !window_outdated (w)
22848 && nrow >= 0
22849 && nrow < w->current_matrix->nrows
22850 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22851 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22853 struct glyph *g, *e, *g1;
22854 int nglyphs, i;
22855 Lisp_Object levels;
22857 if (!row->reversed_p) /* Left-to-right glyph row. */
22859 g = g1 = row->glyphs[TEXT_AREA];
22860 e = g + row->used[TEXT_AREA];
22862 /* Skip over glyphs at the start of the row that was
22863 generated by redisplay for its own needs. */
22864 while (g < e
22865 && NILP (g->object)
22866 && g->charpos < 0)
22867 g++;
22868 g1 = g;
22870 /* Count the "interesting" glyphs in this row. */
22871 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22872 nglyphs++;
22874 /* Create and fill the array. */
22875 levels = make_uninit_vector (nglyphs);
22876 for (i = 0; g1 < g; i++, g1++)
22877 ASET (levels, i, make_number (g1->resolved_level));
22879 else /* Right-to-left glyph row. */
22881 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22882 e = row->glyphs[TEXT_AREA] - 1;
22883 while (g > e
22884 && NILP (g->object)
22885 && g->charpos < 0)
22886 g--;
22887 g1 = g;
22888 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22889 nglyphs++;
22890 levels = make_uninit_vector (nglyphs);
22891 for (i = 0; g1 > g; i++, g1--)
22892 ASET (levels, i, make_number (g1->resolved_level));
22894 return levels;
22896 else
22897 return Qnil;
22902 /***********************************************************************
22903 Menu Bar
22904 ***********************************************************************/
22906 /* Redisplay the menu bar in the frame for window W.
22908 The menu bar of X frames that don't have X toolkit support is
22909 displayed in a special window W->frame->menu_bar_window.
22911 The menu bar of terminal frames is treated specially as far as
22912 glyph matrices are concerned. Menu bar lines are not part of
22913 windows, so the update is done directly on the frame matrix rows
22914 for the menu bar. */
22916 static void
22917 display_menu_bar (struct window *w)
22919 struct frame *f = XFRAME (WINDOW_FRAME (w));
22920 struct it it;
22921 Lisp_Object items;
22922 int i;
22924 /* Don't do all this for graphical frames. */
22925 #ifdef HAVE_NTGUI
22926 if (FRAME_W32_P (f))
22927 return;
22928 #endif
22929 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22930 if (FRAME_X_P (f))
22931 return;
22932 #endif
22934 #ifdef HAVE_NS
22935 if (FRAME_NS_P (f))
22936 return;
22937 #endif /* HAVE_NS */
22939 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22940 eassert (!FRAME_WINDOW_P (f));
22941 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22942 it.first_visible_x = 0;
22943 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22944 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22945 if (FRAME_WINDOW_P (f))
22947 /* Menu bar lines are displayed in the desired matrix of the
22948 dummy window menu_bar_window. */
22949 struct window *menu_w;
22950 menu_w = XWINDOW (f->menu_bar_window);
22951 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22952 MENU_FACE_ID);
22953 it.first_visible_x = 0;
22954 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22956 else
22957 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22959 /* This is a TTY frame, i.e. character hpos/vpos are used as
22960 pixel x/y. */
22961 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22962 MENU_FACE_ID);
22963 it.first_visible_x = 0;
22964 it.last_visible_x = FRAME_COLS (f);
22967 /* FIXME: This should be controlled by a user option. See the
22968 comments in redisplay_tool_bar and display_mode_line about
22969 this. */
22970 it.paragraph_embedding = L2R;
22972 /* Clear all rows of the menu bar. */
22973 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22975 struct glyph_row *row = it.glyph_row + i;
22976 clear_glyph_row (row);
22977 row->enabled_p = true;
22978 row->full_width_p = true;
22979 row->reversed_p = false;
22982 /* Display all items of the menu bar. */
22983 items = FRAME_MENU_BAR_ITEMS (it.f);
22984 for (i = 0; i < ASIZE (items); i += 4)
22986 Lisp_Object string;
22988 /* Stop at nil string. */
22989 string = AREF (items, i + 1);
22990 if (NILP (string))
22991 break;
22993 /* Remember where item was displayed. */
22994 ASET (items, i + 3, make_number (it.hpos));
22996 /* Display the item, pad with one space. */
22997 if (it.current_x < it.last_visible_x)
22998 display_string (NULL, string, Qnil, 0, 0, &it,
22999 SCHARS (string) + 1, 0, 0, -1);
23002 /* Fill out the line with spaces. */
23003 if (it.current_x < it.last_visible_x)
23004 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23006 /* Compute the total height of the lines. */
23007 compute_line_metrics (&it);
23010 /* Deep copy of a glyph row, including the glyphs. */
23011 static void
23012 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23014 struct glyph *pointers[1 + LAST_AREA];
23015 int to_used = to->used[TEXT_AREA];
23017 /* Save glyph pointers of TO. */
23018 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23020 /* Do a structure assignment. */
23021 *to = *from;
23023 /* Restore original glyph pointers of TO. */
23024 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23026 /* Copy the glyphs. */
23027 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23028 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23030 /* If we filled only part of the TO row, fill the rest with
23031 space_glyph (which will display as empty space). */
23032 if (to_used > from->used[TEXT_AREA])
23033 fill_up_frame_row_with_spaces (to, to_used);
23036 /* Display one menu item on a TTY, by overwriting the glyphs in the
23037 frame F's desired glyph matrix with glyphs produced from the menu
23038 item text. Called from term.c to display TTY drop-down menus one
23039 item at a time.
23041 ITEM_TEXT is the menu item text as a C string.
23043 FACE_ID is the face ID to be used for this menu item. FACE_ID
23044 could specify one of 3 faces: a face for an enabled item, a face
23045 for a disabled item, or a face for a selected item.
23047 X and Y are coordinates of the first glyph in the frame's desired
23048 matrix to be overwritten by the menu item. Since this is a TTY, Y
23049 is the zero-based number of the glyph row and X is the zero-based
23050 glyph number in the row, starting from left, where to start
23051 displaying the item.
23053 SUBMENU means this menu item drops down a submenu, which
23054 should be indicated by displaying a proper visual cue after the
23055 item text. */
23057 void
23058 display_tty_menu_item (const char *item_text, int width, int face_id,
23059 int x, int y, bool submenu)
23061 struct it it;
23062 struct frame *f = SELECTED_FRAME ();
23063 struct window *w = XWINDOW (f->selected_window);
23064 struct glyph_row *row;
23065 size_t item_len = strlen (item_text);
23067 eassert (FRAME_TERMCAP_P (f));
23069 /* Don't write beyond the matrix's last row. This can happen for
23070 TTY screens that are not high enough to show the entire menu.
23071 (This is actually a bit of defensive programming, as
23072 tty_menu_display already limits the number of menu items to one
23073 less than the number of screen lines.) */
23074 if (y >= f->desired_matrix->nrows)
23075 return;
23077 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23078 it.first_visible_x = 0;
23079 it.last_visible_x = FRAME_COLS (f) - 1;
23080 row = it.glyph_row;
23081 /* Start with the row contents from the current matrix. */
23082 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23083 bool saved_width = row->full_width_p;
23084 row->full_width_p = true;
23085 bool saved_reversed = row->reversed_p;
23086 row->reversed_p = false;
23087 row->enabled_p = true;
23089 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23090 desired face. */
23091 eassert (x < f->desired_matrix->matrix_w);
23092 it.current_x = it.hpos = x;
23093 it.current_y = it.vpos = y;
23094 int saved_used = row->used[TEXT_AREA];
23095 bool saved_truncated = row->truncated_on_right_p;
23096 row->used[TEXT_AREA] = x;
23097 it.face_id = face_id;
23098 it.line_wrap = TRUNCATE;
23100 /* FIXME: This should be controlled by a user option. See the
23101 comments in redisplay_tool_bar and display_mode_line about this.
23102 Also, if paragraph_embedding could ever be R2L, changes will be
23103 needed to avoid shifting to the right the row characters in
23104 term.c:append_glyph. */
23105 it.paragraph_embedding = L2R;
23107 /* Pad with a space on the left. */
23108 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23109 width--;
23110 /* Display the menu item, pad with spaces to WIDTH. */
23111 if (submenu)
23113 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23114 item_len, 0, FRAME_COLS (f) - 1, -1);
23115 width -= item_len;
23116 /* Indicate with " >" that there's a submenu. */
23117 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23118 FRAME_COLS (f) - 1, -1);
23120 else
23121 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23122 width, 0, FRAME_COLS (f) - 1, -1);
23124 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23125 row->truncated_on_right_p = saved_truncated;
23126 row->hash = row_hash (row);
23127 row->full_width_p = saved_width;
23128 row->reversed_p = saved_reversed;
23131 /***********************************************************************
23132 Mode Line
23133 ***********************************************************************/
23135 /* Redisplay mode lines in the window tree whose root is WINDOW.
23136 If FORCE, redisplay mode lines unconditionally.
23137 Otherwise, redisplay only mode lines that are garbaged. Value is
23138 the number of windows whose mode lines were redisplayed. */
23140 static int
23141 redisplay_mode_lines (Lisp_Object window, bool force)
23143 int nwindows = 0;
23145 while (!NILP (window))
23147 struct window *w = XWINDOW (window);
23149 if (WINDOWP (w->contents))
23150 nwindows += redisplay_mode_lines (w->contents, force);
23151 else if (force
23152 || FRAME_GARBAGED_P (XFRAME (w->frame))
23153 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23155 struct text_pos lpoint;
23156 struct buffer *old = current_buffer;
23158 /* Set the window's buffer for the mode line display. */
23159 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23160 set_buffer_internal_1 (XBUFFER (w->contents));
23162 /* Point refers normally to the selected window. For any
23163 other window, set up appropriate value. */
23164 if (!EQ (window, selected_window))
23166 struct text_pos pt;
23168 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23169 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23172 /* Display mode lines. */
23173 clear_glyph_matrix (w->desired_matrix);
23174 if (display_mode_lines (w))
23175 ++nwindows;
23177 /* Restore old settings. */
23178 set_buffer_internal_1 (old);
23179 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23182 window = w->next;
23185 return nwindows;
23189 /* Display the mode and/or header line of window W. Value is the
23190 sum number of mode lines and header lines displayed. */
23192 static int
23193 display_mode_lines (struct window *w)
23195 Lisp_Object old_selected_window = selected_window;
23196 Lisp_Object old_selected_frame = selected_frame;
23197 Lisp_Object new_frame = w->frame;
23198 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23199 int n = 0;
23201 selected_frame = new_frame;
23202 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23203 or window's point, then we'd need select_window_1 here as well. */
23204 XSETWINDOW (selected_window, w);
23205 XFRAME (new_frame)->selected_window = selected_window;
23207 /* These will be set while the mode line specs are processed. */
23208 line_number_displayed = false;
23209 w->column_number_displayed = -1;
23211 if (window_wants_mode_line (w))
23213 Lisp_Object window_mode_line_format
23214 = window_parameter (w, Qmode_line_format);
23216 struct window *sel_w = XWINDOW (old_selected_window);
23218 /* Select mode line face based on the real selected window. */
23219 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23220 NILP (window_mode_line_format)
23221 ? BVAR (current_buffer, mode_line_format)
23222 : window_mode_line_format);
23223 ++n;
23226 if (window_wants_header_line (w))
23228 Lisp_Object window_header_line_format
23229 = window_parameter (w, Qheader_line_format);
23231 display_mode_line (w, HEADER_LINE_FACE_ID,
23232 NILP (window_header_line_format)
23233 ? BVAR (current_buffer, header_line_format)
23234 : window_header_line_format);
23235 ++n;
23238 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23239 selected_frame = old_selected_frame;
23240 selected_window = old_selected_window;
23241 if (n > 0)
23242 w->must_be_updated_p = true;
23243 return n;
23247 /* Display mode or header line of window W. FACE_ID specifies which
23248 line to display; it is either MODE_LINE_FACE_ID or
23249 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23250 display. Value is the pixel height of the mode/header line
23251 displayed. */
23253 static int
23254 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23256 struct it it;
23257 struct face *face;
23258 ptrdiff_t count = SPECPDL_INDEX ();
23260 init_iterator (&it, w, -1, -1, NULL, face_id);
23261 /* Don't extend on a previously drawn mode-line.
23262 This may happen if called from pos_visible_p. */
23263 it.glyph_row->enabled_p = false;
23264 prepare_desired_row (w, it.glyph_row, true);
23266 it.glyph_row->mode_line_p = true;
23268 /* FIXME: This should be controlled by a user option. But
23269 supporting such an option is not trivial, since the mode line is
23270 made up of many separate strings. */
23271 it.paragraph_embedding = L2R;
23273 record_unwind_protect (unwind_format_mode_line,
23274 format_mode_line_unwind_data (NULL, NULL,
23275 Qnil, false));
23277 mode_line_target = MODE_LINE_DISPLAY;
23279 /* Temporarily make frame's keyboard the current kboard so that
23280 kboard-local variables in the mode_line_format will get the right
23281 values. */
23282 push_kboard (FRAME_KBOARD (it.f));
23283 record_unwind_save_match_data ();
23284 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23285 pop_kboard ();
23287 unbind_to (count, Qnil);
23289 /* Fill up with spaces. */
23290 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23292 compute_line_metrics (&it);
23293 it.glyph_row->full_width_p = true;
23294 it.glyph_row->continued_p = false;
23295 it.glyph_row->truncated_on_left_p = false;
23296 it.glyph_row->truncated_on_right_p = false;
23298 /* Make a 3D mode-line have a shadow at its right end. */
23299 face = FACE_FROM_ID (it.f, face_id);
23300 extend_face_to_end_of_line (&it);
23301 if (face->box != FACE_NO_BOX)
23303 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23304 + it.glyph_row->used[TEXT_AREA] - 1);
23305 last->right_box_line_p = true;
23308 return it.glyph_row->height;
23311 /* Move element ELT in LIST to the front of LIST.
23312 Return the updated list. */
23314 static Lisp_Object
23315 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23317 register Lisp_Object tail, prev;
23318 register Lisp_Object tem;
23320 tail = list;
23321 prev = Qnil;
23322 while (CONSP (tail))
23324 tem = XCAR (tail);
23326 if (EQ (elt, tem))
23328 /* Splice out the link TAIL. */
23329 if (NILP (prev))
23330 list = XCDR (tail);
23331 else
23332 Fsetcdr (prev, XCDR (tail));
23334 /* Now make it the first. */
23335 Fsetcdr (tail, list);
23336 return tail;
23338 else
23339 prev = tail;
23340 tail = XCDR (tail);
23341 maybe_quit ();
23344 /* Not found--return unchanged LIST. */
23345 return list;
23348 /* Contribute ELT to the mode line for window IT->w. How it
23349 translates into text depends on its data type.
23351 IT describes the display environment in which we display, as usual.
23353 DEPTH is the depth in recursion. It is used to prevent
23354 infinite recursion here.
23356 FIELD_WIDTH is the number of characters the display of ELT should
23357 occupy in the mode line, and PRECISION is the maximum number of
23358 characters to display from ELT's representation. See
23359 display_string for details.
23361 Returns the hpos of the end of the text generated by ELT.
23363 PROPS is a property list to add to any string we encounter.
23365 If RISKY, remove (disregard) any properties in any string
23366 we encounter, and ignore :eval and :propertize.
23368 The global variable `mode_line_target' determines whether the
23369 output is passed to `store_mode_line_noprop',
23370 `store_mode_line_string', or `display_string'. */
23372 static int
23373 display_mode_element (struct it *it, int depth, int field_width, int precision,
23374 Lisp_Object elt, Lisp_Object props, bool risky)
23376 int n = 0, field, prec;
23377 bool literal = false;
23379 tail_recurse:
23380 if (depth > 100)
23381 elt = build_string ("*too-deep*");
23383 depth++;
23385 switch (XTYPE (elt))
23387 case Lisp_String:
23389 /* A string: output it and check for %-constructs within it. */
23390 unsigned char c;
23391 ptrdiff_t offset = 0;
23393 if (SCHARS (elt) > 0
23394 && (!NILP (props) || risky))
23396 Lisp_Object oprops, aelt;
23397 oprops = Ftext_properties_at (make_number (0), elt);
23399 /* If the starting string's properties are not what
23400 we want, translate the string. Also, if the string
23401 is risky, do that anyway. */
23403 if (NILP (Fequal (props, oprops)) || risky)
23405 /* If the starting string has properties,
23406 merge the specified ones onto the existing ones. */
23407 if (! NILP (oprops) && !risky)
23409 Lisp_Object tem;
23411 oprops = Fcopy_sequence (oprops);
23412 tem = props;
23413 while (CONSP (tem))
23415 oprops = Fplist_put (oprops, XCAR (tem),
23416 XCAR (XCDR (tem)));
23417 tem = XCDR (XCDR (tem));
23419 props = oprops;
23422 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23423 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23425 /* AELT is what we want. Move it to the front
23426 without consing. */
23427 elt = XCAR (aelt);
23428 mode_line_proptrans_alist
23429 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23431 else
23433 Lisp_Object tem;
23435 /* If AELT has the wrong props, it is useless.
23436 so get rid of it. */
23437 if (! NILP (aelt))
23438 mode_line_proptrans_alist
23439 = Fdelq (aelt, mode_line_proptrans_alist);
23441 elt = Fcopy_sequence (elt);
23442 Fset_text_properties (make_number (0), Flength (elt),
23443 props, elt);
23444 /* Add this item to mode_line_proptrans_alist. */
23445 mode_line_proptrans_alist
23446 = Fcons (Fcons (elt, props),
23447 mode_line_proptrans_alist);
23448 /* Truncate mode_line_proptrans_alist
23449 to at most 50 elements. */
23450 tem = Fnthcdr (make_number (50),
23451 mode_line_proptrans_alist);
23452 if (! NILP (tem))
23453 XSETCDR (tem, Qnil);
23458 offset = 0;
23460 if (literal)
23462 prec = precision - n;
23463 switch (mode_line_target)
23465 case MODE_LINE_NOPROP:
23466 case MODE_LINE_TITLE:
23467 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23468 break;
23469 case MODE_LINE_STRING:
23470 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23471 break;
23472 case MODE_LINE_DISPLAY:
23473 n += display_string (NULL, elt, Qnil, 0, 0, it,
23474 0, prec, 0, STRING_MULTIBYTE (elt));
23475 break;
23478 break;
23481 /* Handle the non-literal case. */
23483 while ((precision <= 0 || n < precision)
23484 && SREF (elt, offset) != 0
23485 && (mode_line_target != MODE_LINE_DISPLAY
23486 || it->current_x < it->last_visible_x))
23488 ptrdiff_t last_offset = offset;
23490 /* Advance to end of string or next format specifier. */
23491 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23494 if (offset - 1 != last_offset)
23496 ptrdiff_t nchars, nbytes;
23498 /* Output to end of string or up to '%'. Field width
23499 is length of string. Don't output more than
23500 PRECISION allows us. */
23501 offset--;
23503 prec = c_string_width (SDATA (elt) + last_offset,
23504 offset - last_offset, precision - n,
23505 &nchars, &nbytes);
23507 switch (mode_line_target)
23509 case MODE_LINE_NOPROP:
23510 case MODE_LINE_TITLE:
23511 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23512 break;
23513 case MODE_LINE_STRING:
23515 ptrdiff_t bytepos = last_offset;
23516 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23517 ptrdiff_t endpos = (precision <= 0
23518 ? string_byte_to_char (elt, offset)
23519 : charpos + nchars);
23520 Lisp_Object mode_string
23521 = Fsubstring (elt, make_number (charpos),
23522 make_number (endpos));
23523 n += store_mode_line_string (NULL, mode_string, false,
23524 0, 0, Qnil);
23526 break;
23527 case MODE_LINE_DISPLAY:
23529 ptrdiff_t bytepos = last_offset;
23530 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23532 if (precision <= 0)
23533 nchars = string_byte_to_char (elt, offset) - charpos;
23534 n += display_string (NULL, elt, Qnil, 0, charpos,
23535 it, 0, nchars, 0,
23536 STRING_MULTIBYTE (elt));
23538 break;
23541 else /* c == '%' */
23543 ptrdiff_t percent_position = offset;
23545 /* Get the specified minimum width. Zero means
23546 don't pad. */
23547 field = 0;
23548 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23549 field = field * 10 + c - '0';
23551 /* Don't pad beyond the total padding allowed. */
23552 if (field_width - n > 0 && field > field_width - n)
23553 field = field_width - n;
23555 /* Note that either PRECISION <= 0 or N < PRECISION. */
23556 prec = precision - n;
23558 if (c == 'M')
23559 n += display_mode_element (it, depth, field, prec,
23560 Vglobal_mode_string, props,
23561 risky);
23562 else if (c != 0)
23564 bool multibyte;
23565 ptrdiff_t bytepos, charpos;
23566 const char *spec;
23567 Lisp_Object string;
23569 bytepos = percent_position;
23570 charpos = (STRING_MULTIBYTE (elt)
23571 ? string_byte_to_char (elt, bytepos)
23572 : bytepos);
23573 spec = decode_mode_spec (it->w, c, field, &string);
23574 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23576 switch (mode_line_target)
23578 case MODE_LINE_NOPROP:
23579 case MODE_LINE_TITLE:
23580 n += store_mode_line_noprop (spec, field, prec);
23581 break;
23582 case MODE_LINE_STRING:
23584 Lisp_Object tem = build_string (spec);
23585 props = Ftext_properties_at (make_number (charpos), elt);
23586 /* Should only keep face property in props */
23587 n += store_mode_line_string (NULL, tem, false,
23588 field, prec, props);
23590 break;
23591 case MODE_LINE_DISPLAY:
23593 int nglyphs_before, nwritten;
23595 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23596 nwritten = display_string (spec, string, elt,
23597 charpos, 0, it,
23598 field, prec, 0,
23599 multibyte);
23601 /* Assign to the glyphs written above the
23602 string where the `%x' came from, position
23603 of the `%'. */
23604 if (nwritten > 0)
23606 struct glyph *glyph
23607 = (it->glyph_row->glyphs[TEXT_AREA]
23608 + nglyphs_before);
23609 int i;
23611 for (i = 0; i < nwritten; ++i)
23613 glyph[i].object = elt;
23614 glyph[i].charpos = charpos;
23617 n += nwritten;
23620 break;
23623 else /* c == 0 */
23624 break;
23628 break;
23630 case Lisp_Symbol:
23631 /* A symbol: process the value of the symbol recursively
23632 as if it appeared here directly. Avoid error if symbol void.
23633 Special case: if value of symbol is a string, output the string
23634 literally. */
23636 register Lisp_Object tem;
23638 /* If the variable is not marked as risky to set
23639 then its contents are risky to use. */
23640 if (NILP (Fget (elt, Qrisky_local_variable)))
23641 risky = true;
23643 tem = Fboundp (elt);
23644 if (!NILP (tem))
23646 tem = Fsymbol_value (elt);
23647 /* If value is a string, output that string literally:
23648 don't check for % within it. */
23649 if (STRINGP (tem))
23650 literal = true;
23652 if (!EQ (tem, elt))
23654 /* Give up right away for nil or t. */
23655 elt = tem;
23656 goto tail_recurse;
23660 break;
23662 case Lisp_Cons:
23664 register Lisp_Object car, tem;
23666 /* A cons cell: five distinct cases.
23667 If first element is :eval or :propertize, do something special.
23668 If first element is a string or a cons, process all the elements
23669 and effectively concatenate them.
23670 If first element is a negative number, truncate displaying cdr to
23671 at most that many characters. If positive, pad (with spaces)
23672 to at least that many characters.
23673 If first element is a symbol, process the cadr or caddr recursively
23674 according to whether the symbol's value is non-nil or nil. */
23675 car = XCAR (elt);
23676 if (EQ (car, QCeval))
23678 /* An element of the form (:eval FORM) means evaluate FORM
23679 and use the result as mode line elements. */
23681 if (risky)
23682 break;
23684 if (CONSP (XCDR (elt)))
23686 Lisp_Object spec;
23687 spec = safe__eval (true, XCAR (XCDR (elt)));
23688 /* The :eval form could delete the frame stored in the
23689 iterator, which will cause a crash if we try to
23690 access faces and other fields (e.g., FRAME_KBOARD)
23691 on that frame. This is a nonsensical thing to do,
23692 and signaling an error from redisplay might be
23693 dangerous, but we cannot continue with an invalid frame. */
23694 if (!FRAME_LIVE_P (it->f))
23695 signal_error (":eval deleted the frame being displayed", elt);
23696 n += display_mode_element (it, depth, field_width - n,
23697 precision - n, spec, props,
23698 risky);
23701 else if (EQ (car, QCpropertize))
23703 /* An element of the form (:propertize ELT PROPS...)
23704 means display ELT but applying properties PROPS. */
23706 if (risky)
23707 break;
23709 if (CONSP (XCDR (elt)))
23710 n += display_mode_element (it, depth, field_width - n,
23711 precision - n, XCAR (XCDR (elt)),
23712 XCDR (XCDR (elt)), risky);
23714 else if (SYMBOLP (car))
23716 tem = Fboundp (car);
23717 elt = XCDR (elt);
23718 if (!CONSP (elt))
23719 goto invalid;
23720 /* elt is now the cdr, and we know it is a cons cell.
23721 Use its car if CAR has a non-nil value. */
23722 if (!NILP (tem))
23724 tem = Fsymbol_value (car);
23725 if (!NILP (tem))
23727 elt = XCAR (elt);
23728 goto tail_recurse;
23731 /* Symbol's value is nil (or symbol is unbound)
23732 Get the cddr of the original list
23733 and if possible find the caddr and use that. */
23734 elt = XCDR (elt);
23735 if (NILP (elt))
23736 break;
23737 else if (!CONSP (elt))
23738 goto invalid;
23739 elt = XCAR (elt);
23740 goto tail_recurse;
23742 else if (INTEGERP (car))
23744 register int lim = XINT (car);
23745 elt = XCDR (elt);
23746 if (lim < 0)
23748 /* Negative int means reduce maximum width. */
23749 if (precision <= 0)
23750 precision = -lim;
23751 else
23752 precision = min (precision, -lim);
23754 else if (lim > 0)
23756 /* Padding specified. Don't let it be more than
23757 current maximum. */
23758 if (precision > 0)
23759 lim = min (precision, lim);
23761 /* If that's more padding than already wanted, queue it.
23762 But don't reduce padding already specified even if
23763 that is beyond the current truncation point. */
23764 field_width = max (lim, field_width);
23766 goto tail_recurse;
23768 else if (STRINGP (car) || CONSP (car))
23769 FOR_EACH_TAIL_SAFE (elt)
23771 if (0 < precision && precision <= n)
23772 break;
23773 n += display_mode_element (it, depth,
23774 /* Pad after only the last
23775 list element. */
23776 (! CONSP (XCDR (elt))
23777 ? field_width - n
23778 : 0),
23779 precision - n, XCAR (elt),
23780 props, risky);
23783 break;
23785 default:
23786 invalid:
23787 elt = build_string ("*invalid*");
23788 goto tail_recurse;
23791 /* Pad to FIELD_WIDTH. */
23792 if (field_width > 0 && n < field_width)
23794 switch (mode_line_target)
23796 case MODE_LINE_NOPROP:
23797 case MODE_LINE_TITLE:
23798 n += store_mode_line_noprop ("", field_width - n, 0);
23799 break;
23800 case MODE_LINE_STRING:
23801 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23802 Qnil);
23803 break;
23804 case MODE_LINE_DISPLAY:
23805 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23806 0, 0, 0);
23807 break;
23811 return n;
23814 /* Store a mode-line string element in mode_line_string_list.
23816 If STRING is non-null, display that C string. Otherwise, the Lisp
23817 string LISP_STRING is displayed.
23819 FIELD_WIDTH is the minimum number of output glyphs to produce.
23820 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23821 with spaces. FIELD_WIDTH <= 0 means don't pad.
23823 PRECISION is the maximum number of characters to output from
23824 STRING. PRECISION <= 0 means don't truncate the string.
23826 If COPY_STRING, make a copy of LISP_STRING before adding
23827 properties to the string.
23829 PROPS are the properties to add to the string.
23830 The mode_line_string_face face property is always added to the string.
23833 static int
23834 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23835 bool copy_string,
23836 int field_width, int precision, Lisp_Object props)
23838 ptrdiff_t len;
23839 int n = 0;
23841 if (string != NULL)
23843 len = strlen (string);
23844 if (precision > 0 && len > precision)
23845 len = precision;
23846 lisp_string = make_string (string, len);
23847 if (NILP (props))
23848 props = mode_line_string_face_prop;
23849 else if (!NILP (mode_line_string_face))
23851 Lisp_Object face = Fplist_get (props, Qface);
23852 props = Fcopy_sequence (props);
23853 if (NILP (face))
23854 face = mode_line_string_face;
23855 else
23856 face = list2 (face, mode_line_string_face);
23857 props = Fplist_put (props, Qface, face);
23859 Fadd_text_properties (make_number (0), make_number (len),
23860 props, lisp_string);
23862 else
23864 len = XFASTINT (Flength (lisp_string));
23865 if (precision > 0 && len > precision)
23867 len = precision;
23868 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23869 precision = -1;
23871 if (!NILP (mode_line_string_face))
23873 Lisp_Object face;
23874 if (NILP (props))
23875 props = Ftext_properties_at (make_number (0), lisp_string);
23876 face = Fplist_get (props, Qface);
23877 if (NILP (face))
23878 face = mode_line_string_face;
23879 else
23880 face = list2 (face, mode_line_string_face);
23881 props = list2 (Qface, face);
23882 if (copy_string)
23883 lisp_string = Fcopy_sequence (lisp_string);
23885 if (!NILP (props))
23886 Fadd_text_properties (make_number (0), make_number (len),
23887 props, lisp_string);
23890 if (len > 0)
23892 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23893 n += len;
23896 if (field_width > len)
23898 field_width -= len;
23899 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23900 if (!NILP (props))
23901 Fadd_text_properties (make_number (0), make_number (field_width),
23902 props, lisp_string);
23903 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23904 n += field_width;
23907 return n;
23911 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23912 1, 4, 0,
23913 doc: /* Format a string out of a mode line format specification.
23914 First arg FORMAT specifies the mode line format (see `mode-line-format'
23915 for details) to use.
23917 By default, the format is evaluated for the currently selected window.
23919 Optional second arg FACE specifies the face property to put on all
23920 characters for which no face is specified. The value nil means the
23921 default face. The value t means whatever face the window's mode line
23922 currently uses (either `mode-line' or `mode-line-inactive',
23923 depending on whether the window is the selected window or not).
23924 An integer value means the value string has no text
23925 properties.
23927 Optional third and fourth args WINDOW and BUFFER specify the window
23928 and buffer to use as the context for the formatting (defaults
23929 are the selected window and the WINDOW's buffer). */)
23930 (Lisp_Object format, Lisp_Object face,
23931 Lisp_Object window, Lisp_Object buffer)
23933 struct it it;
23934 int len;
23935 struct window *w;
23936 struct buffer *old_buffer = NULL;
23937 int face_id;
23938 bool no_props = INTEGERP (face);
23939 ptrdiff_t count = SPECPDL_INDEX ();
23940 Lisp_Object str;
23941 int string_start = 0;
23943 w = decode_any_window (window);
23944 XSETWINDOW (window, w);
23946 if (NILP (buffer))
23947 buffer = w->contents;
23948 CHECK_BUFFER (buffer);
23950 /* Make formatting the modeline a non-op when noninteractive, otherwise
23951 there will be problems later caused by a partially initialized frame. */
23952 if (NILP (format) || noninteractive)
23953 return empty_unibyte_string;
23955 if (no_props)
23956 face = Qnil;
23958 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23959 : EQ (face, Qt) ? (EQ (window, selected_window)
23960 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23961 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23962 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23963 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23964 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23965 : DEFAULT_FACE_ID;
23967 old_buffer = current_buffer;
23969 /* Save things including mode_line_proptrans_alist,
23970 and set that to nil so that we don't alter the outer value. */
23971 record_unwind_protect (unwind_format_mode_line,
23972 format_mode_line_unwind_data
23973 (XFRAME (WINDOW_FRAME (w)),
23974 old_buffer, selected_window, true));
23975 mode_line_proptrans_alist = Qnil;
23977 Fselect_window (window, Qt);
23978 set_buffer_internal_1 (XBUFFER (buffer));
23980 init_iterator (&it, w, -1, -1, NULL, face_id);
23982 if (no_props)
23984 mode_line_target = MODE_LINE_NOPROP;
23985 mode_line_string_face_prop = Qnil;
23986 mode_line_string_list = Qnil;
23987 string_start = MODE_LINE_NOPROP_LEN (0);
23989 else
23991 mode_line_target = MODE_LINE_STRING;
23992 mode_line_string_list = Qnil;
23993 mode_line_string_face = face;
23994 mode_line_string_face_prop
23995 = NILP (face) ? Qnil : list2 (Qface, face);
23998 push_kboard (FRAME_KBOARD (it.f));
23999 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
24000 pop_kboard ();
24002 if (no_props)
24004 len = MODE_LINE_NOPROP_LEN (string_start);
24005 str = make_string (mode_line_noprop_buf + string_start, len);
24007 else
24009 mode_line_string_list = Fnreverse (mode_line_string_list);
24010 str = Fmapconcat (Qidentity, mode_line_string_list,
24011 empty_unibyte_string);
24014 unbind_to (count, Qnil);
24015 return str;
24018 /* Write a null-terminated, right justified decimal representation of
24019 the positive integer D to BUF using a minimal field width WIDTH. */
24021 static void
24022 pint2str (register char *buf, register int width, register ptrdiff_t d)
24024 register char *p = buf;
24026 if (d <= 0)
24027 *p++ = '0';
24028 else
24030 while (d > 0)
24032 *p++ = d % 10 + '0';
24033 d /= 10;
24037 for (width -= (int) (p - buf); width > 0; --width)
24038 *p++ = ' ';
24039 *p-- = '\0';
24040 while (p > buf)
24042 d = *buf;
24043 *buf++ = *p;
24044 *p-- = d;
24048 /* Write a null-terminated, right justified decimal and "human
24049 readable" representation of the nonnegative integer D to BUF using
24050 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24052 static const char power_letter[] =
24054 0, /* no letter */
24055 'k', /* kilo */
24056 'M', /* mega */
24057 'G', /* giga */
24058 'T', /* tera */
24059 'P', /* peta */
24060 'E', /* exa */
24061 'Z', /* zetta */
24062 'Y' /* yotta */
24065 static void
24066 pint2hrstr (char *buf, int width, ptrdiff_t d)
24068 /* We aim to represent the nonnegative integer D as
24069 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24070 ptrdiff_t quotient = d;
24071 int remainder = 0;
24072 /* -1 means: do not use TENTHS. */
24073 int tenths = -1;
24074 int exponent = 0;
24076 /* Length of QUOTIENT.TENTHS as a string. */
24077 int length;
24079 char * psuffix;
24080 char * p;
24082 if (quotient >= 1000)
24084 /* Scale to the appropriate EXPONENT. */
24087 remainder = quotient % 1000;
24088 quotient /= 1000;
24089 exponent++;
24091 while (quotient >= 1000);
24093 /* Round to nearest and decide whether to use TENTHS or not. */
24094 if (quotient <= 9)
24096 tenths = remainder / 100;
24097 if (remainder % 100 >= 50)
24099 if (tenths < 9)
24100 tenths++;
24101 else
24103 quotient++;
24104 if (quotient == 10)
24105 tenths = -1;
24106 else
24107 tenths = 0;
24111 else
24112 if (remainder >= 500)
24114 if (quotient < 999)
24115 quotient++;
24116 else
24118 quotient = 1;
24119 exponent++;
24120 tenths = 0;
24125 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24126 if (tenths == -1 && quotient <= 99)
24127 if (quotient <= 9)
24128 length = 1;
24129 else
24130 length = 2;
24131 else
24132 length = 3;
24133 p = psuffix = buf + max (width, length);
24135 /* Print EXPONENT. */
24136 *psuffix++ = power_letter[exponent];
24137 *psuffix = '\0';
24139 /* Print TENTHS. */
24140 if (tenths >= 0)
24142 *--p = '0' + tenths;
24143 *--p = '.';
24146 /* Print QUOTIENT. */
24149 int digit = quotient % 10;
24150 *--p = '0' + digit;
24152 while ((quotient /= 10) != 0);
24154 /* Print leading spaces. */
24155 while (buf < p)
24156 *--p = ' ';
24159 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24160 If EOL_FLAG, set also a mnemonic character for end-of-line
24161 type of CODING_SYSTEM. Return updated pointer into BUF. */
24163 static unsigned char invalid_eol_type[] = "(*invalid*)";
24165 static char *
24166 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24168 Lisp_Object val;
24169 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24170 const unsigned char *eol_str;
24171 int eol_str_len;
24172 /* The EOL conversion we are using. */
24173 Lisp_Object eoltype;
24175 val = CODING_SYSTEM_SPEC (coding_system);
24176 eoltype = Qnil;
24178 if (!VECTORP (val)) /* Not yet decided. */
24180 *buf++ = multibyte ? '-' : ' ';
24181 if (eol_flag)
24182 eoltype = eol_mnemonic_undecided;
24183 /* Don't mention EOL conversion if it isn't decided. */
24185 else
24187 Lisp_Object attrs;
24188 Lisp_Object eolvalue;
24190 attrs = AREF (val, 0);
24191 eolvalue = AREF (val, 2);
24193 *buf++ = multibyte
24194 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24195 : ' ';
24197 if (eol_flag)
24199 /* The EOL conversion that is normal on this system. */
24201 if (NILP (eolvalue)) /* Not yet decided. */
24202 eoltype = eol_mnemonic_undecided;
24203 else if (VECTORP (eolvalue)) /* Not yet decided. */
24204 eoltype = eol_mnemonic_undecided;
24205 else /* eolvalue is Qunix, Qdos, or Qmac. */
24206 eoltype = (EQ (eolvalue, Qunix)
24207 ? eol_mnemonic_unix
24208 : EQ (eolvalue, Qdos)
24209 ? eol_mnemonic_dos : eol_mnemonic_mac);
24213 if (eol_flag)
24215 /* Mention the EOL conversion if it is not the usual one. */
24216 if (STRINGP (eoltype))
24218 eol_str = SDATA (eoltype);
24219 eol_str_len = SBYTES (eoltype);
24221 else if (CHARACTERP (eoltype))
24223 int c = XFASTINT (eoltype);
24224 return buf + CHAR_STRING (c, (unsigned char *) buf);
24226 else
24228 eol_str = invalid_eol_type;
24229 eol_str_len = sizeof (invalid_eol_type) - 1;
24231 memcpy (buf, eol_str, eol_str_len);
24232 buf += eol_str_len;
24235 return buf;
24238 /* Return the approximate percentage N is of D (rounding upward), or 99,
24239 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24241 static int
24242 percent99 (ptrdiff_t n, ptrdiff_t d)
24244 int percent = (d - 1 + 100.0 * n) / d;
24245 return min (percent, 99);
24248 /* Return a string for the output of a mode line %-spec for window W,
24249 generated by character C. FIELD_WIDTH > 0 means pad the string
24250 returned with spaces to that value. Return a Lisp string in
24251 *STRING if the resulting string is taken from that Lisp string.
24253 Note we operate on the current buffer for most purposes. */
24255 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24257 static const char *
24258 decode_mode_spec (struct window *w, register int c, int field_width,
24259 Lisp_Object *string)
24261 Lisp_Object obj;
24262 struct frame *f = XFRAME (WINDOW_FRAME (w));
24263 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24264 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24265 produce strings from numerical values, so limit preposterously
24266 large values of FIELD_WIDTH to avoid overrunning the buffer's
24267 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24268 bytes plus the terminating null. */
24269 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24270 struct buffer *b = current_buffer;
24272 obj = Qnil;
24273 *string = Qnil;
24275 switch (c)
24277 case '*':
24278 if (!NILP (BVAR (b, read_only)))
24279 return "%";
24280 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24281 return "*";
24282 return "-";
24284 case '+':
24285 /* This differs from %* only for a modified read-only buffer. */
24286 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24287 return "*";
24288 if (!NILP (BVAR (b, read_only)))
24289 return "%";
24290 return "-";
24292 case '&':
24293 /* This differs from %* in ignoring read-only-ness. */
24294 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24295 return "*";
24296 return "-";
24298 case '%':
24299 return "%";
24301 case '[':
24303 int i;
24304 char *p;
24306 if (command_loop_level > 5)
24307 return "[[[... ";
24308 p = decode_mode_spec_buf;
24309 for (i = 0; i < command_loop_level; i++)
24310 *p++ = '[';
24311 *p = 0;
24312 return decode_mode_spec_buf;
24315 case ']':
24317 int i;
24318 char *p;
24320 if (command_loop_level > 5)
24321 return " ...]]]";
24322 p = decode_mode_spec_buf;
24323 for (i = 0; i < command_loop_level; i++)
24324 *p++ = ']';
24325 *p = 0;
24326 return decode_mode_spec_buf;
24329 case '-':
24331 register int i;
24333 /* Let lots_of_dashes be a string of infinite length. */
24334 if (mode_line_target == MODE_LINE_NOPROP
24335 || mode_line_target == MODE_LINE_STRING)
24336 return "--";
24337 if (field_width <= 0
24338 || field_width > sizeof (lots_of_dashes))
24340 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24341 decode_mode_spec_buf[i] = '-';
24342 decode_mode_spec_buf[i] = '\0';
24343 return decode_mode_spec_buf;
24345 else
24346 return lots_of_dashes;
24349 case 'b':
24350 obj = BVAR (b, name);
24351 break;
24353 case 'c':
24354 case 'C':
24355 /* %c, %C, and %l are ignored in `frame-title-format'.
24356 (In redisplay_internal, the frame title is drawn _before_ the
24357 windows are updated, so the stuff which depends on actual
24358 window contents (such as %l) may fail to render properly, or
24359 even crash emacs.) */
24360 if (mode_line_target == MODE_LINE_TITLE)
24361 return "";
24362 else
24364 ptrdiff_t col = current_column ();
24365 int disp_col = (c == 'C') ? col + 1 : col;
24366 w->column_number_displayed = col;
24367 pint2str (decode_mode_spec_buf, width, disp_col);
24368 return decode_mode_spec_buf;
24371 case 'e':
24372 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24374 if (NILP (Vmemory_full))
24375 return "";
24376 else
24377 return "!MEM FULL! ";
24379 #else
24380 return "";
24381 #endif
24383 case 'F':
24384 /* %F displays the frame name. */
24385 if (!NILP (f->title))
24386 return SSDATA (f->title);
24387 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24388 return SSDATA (f->name);
24389 return "Emacs";
24391 case 'f':
24392 obj = BVAR (b, filename);
24393 break;
24395 case 'i':
24397 ptrdiff_t size = ZV - BEGV;
24398 pint2str (decode_mode_spec_buf, width, size);
24399 return decode_mode_spec_buf;
24402 case 'I':
24404 ptrdiff_t size = ZV - BEGV;
24405 pint2hrstr (decode_mode_spec_buf, width, size);
24406 return decode_mode_spec_buf;
24409 case 'l':
24411 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24412 ptrdiff_t topline, nlines, height;
24413 ptrdiff_t junk;
24415 /* %c, %C, and %l are ignored in `frame-title-format'. */
24416 if (mode_line_target == MODE_LINE_TITLE)
24417 return "";
24419 startpos = marker_position (w->start);
24420 startpos_byte = marker_byte_position (w->start);
24421 height = WINDOW_TOTAL_LINES (w);
24423 /* If we decided that this buffer isn't suitable for line numbers,
24424 don't forget that too fast. */
24425 if (w->base_line_pos == -1)
24426 goto no_value;
24428 /* If the buffer is very big, don't waste time. */
24429 if (INTEGERP (Vline_number_display_limit)
24430 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24432 w->base_line_pos = 0;
24433 w->base_line_number = 0;
24434 goto no_value;
24437 if (w->base_line_number > 0
24438 && w->base_line_pos > 0
24439 && w->base_line_pos <= startpos)
24441 line = w->base_line_number;
24442 linepos = w->base_line_pos;
24443 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24445 else
24447 line = 1;
24448 linepos = BUF_BEGV (b);
24449 linepos_byte = BUF_BEGV_BYTE (b);
24452 /* Count lines from base line to window start position. */
24453 nlines = display_count_lines (linepos_byte,
24454 startpos_byte,
24455 startpos, &junk);
24457 topline = nlines + line;
24459 /* Determine a new base line, if the old one is too close
24460 or too far away, or if we did not have one.
24461 "Too close" means it's plausible a scroll-down would
24462 go back past it. */
24463 if (startpos == BUF_BEGV (b))
24465 w->base_line_number = topline;
24466 w->base_line_pos = BUF_BEGV (b);
24468 else if (nlines < height + 25 || nlines > height * 3 + 50
24469 || linepos == BUF_BEGV (b))
24471 ptrdiff_t limit = BUF_BEGV (b);
24472 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24473 ptrdiff_t position;
24474 ptrdiff_t distance =
24475 (height * 2 + 30) * line_number_display_limit_width;
24477 if (startpos - distance > limit)
24479 limit = startpos - distance;
24480 limit_byte = CHAR_TO_BYTE (limit);
24483 nlines = display_count_lines (startpos_byte,
24484 limit_byte,
24485 - (height * 2 + 30),
24486 &position);
24487 /* If we couldn't find the lines we wanted within
24488 line_number_display_limit_width chars per line,
24489 give up on line numbers for this window. */
24490 if (position == limit_byte && limit == startpos - distance)
24492 w->base_line_pos = -1;
24493 w->base_line_number = 0;
24494 goto no_value;
24497 w->base_line_number = topline - nlines;
24498 w->base_line_pos = BYTE_TO_CHAR (position);
24501 /* Now count lines from the start pos to point. */
24502 nlines = display_count_lines (startpos_byte,
24503 PT_BYTE, PT, &junk);
24505 /* Record that we did display the line number. */
24506 line_number_displayed = true;
24508 /* Make the string to show. */
24509 pint2str (decode_mode_spec_buf, width, topline + nlines);
24510 return decode_mode_spec_buf;
24511 no_value:
24513 char *p = decode_mode_spec_buf;
24514 int pad = width - 2;
24515 while (pad-- > 0)
24516 *p++ = ' ';
24517 *p++ = '?';
24518 *p++ = '?';
24519 *p = '\0';
24520 return decode_mode_spec_buf;
24523 break;
24525 case 'm':
24526 obj = BVAR (b, mode_name);
24527 break;
24529 case 'n':
24530 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24531 return " Narrow";
24532 break;
24534 /* Display the "degree of travel" of the window through the buffer. */
24535 case 'o':
24537 ptrdiff_t toppos = marker_position (w->start);
24538 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24539 ptrdiff_t begv = BUF_BEGV (b);
24540 ptrdiff_t zv = BUF_ZV (b);
24542 if (zv <= botpos)
24543 return toppos <= begv ? "All" : "Bottom";
24544 else if (toppos <= begv)
24545 return "Top";
24546 else
24548 sprintf (decode_mode_spec_buf, "%2d%%",
24549 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24550 return decode_mode_spec_buf;
24554 /* Display percentage of buffer above the top of the screen. */
24555 case 'p':
24557 ptrdiff_t pos = marker_position (w->start);
24558 ptrdiff_t begv = BUF_BEGV (b);
24559 ptrdiff_t zv = BUF_ZV (b);
24561 if (w->window_end_pos <= BUF_Z (b) - zv)
24562 return pos <= begv ? "All" : "Bottom";
24563 else if (pos <= begv)
24564 return "Top";
24565 else
24567 sprintf (decode_mode_spec_buf, "%2d%%",
24568 percent99 (pos - begv, zv - begv));
24569 return decode_mode_spec_buf;
24573 /* Display percentage of size above the bottom of the screen. */
24574 case 'P':
24576 ptrdiff_t toppos = marker_position (w->start);
24577 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24578 ptrdiff_t begv = BUF_BEGV (b);
24579 ptrdiff_t zv = BUF_ZV (b);
24581 if (zv <= botpos)
24582 return toppos <= begv ? "All" : "Bottom";
24583 else
24585 sprintf (decode_mode_spec_buf,
24586 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24587 percent99 (botpos - begv, zv - begv));
24588 return decode_mode_spec_buf;
24592 /* Display percentage offsets of top and bottom of the window,
24593 using "All" (but not "Top" or "Bottom") where appropriate. */
24594 case 'q':
24596 ptrdiff_t toppos = marker_position (w->start);
24597 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24598 ptrdiff_t begv = BUF_BEGV (b);
24599 ptrdiff_t zv = BUF_ZV (b);
24600 int top_perc, bot_perc;
24602 if ((toppos <= begv) && (zv <= botpos))
24603 return "All ";
24605 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24606 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24608 if (top_perc == bot_perc)
24609 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24610 else
24611 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24613 return decode_mode_spec_buf;
24616 case 's':
24617 /* status of process */
24618 obj = Fget_buffer_process (Fcurrent_buffer ());
24619 if (NILP (obj))
24620 return "no process";
24621 #ifndef MSDOS
24622 obj = Fsymbol_name (Fprocess_status (obj));
24623 #endif
24624 break;
24626 case '@':
24628 ptrdiff_t count = inhibit_garbage_collection ();
24629 Lisp_Object curdir = BVAR (current_buffer, directory);
24630 Lisp_Object val = Qnil;
24632 if (STRINGP (curdir))
24633 val = call1 (intern ("file-remote-p"), curdir);
24635 unbind_to (count, Qnil);
24637 if (NILP (val))
24638 return "-";
24639 else
24640 return "@";
24643 case 'z':
24644 /* coding-system (not including end-of-line format) */
24645 case 'Z':
24646 /* coding-system (including end-of-line type) */
24648 bool eol_flag = (c == 'Z');
24649 char *p = decode_mode_spec_buf;
24651 if (! FRAME_WINDOW_P (f))
24653 /* No need to mention EOL here--the terminal never needs
24654 to do EOL conversion. */
24655 p = decode_mode_spec_coding (CODING_ID_NAME
24656 (FRAME_KEYBOARD_CODING (f)->id),
24657 p, false);
24658 p = decode_mode_spec_coding (CODING_ID_NAME
24659 (FRAME_TERMINAL_CODING (f)->id),
24660 p, false);
24662 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24663 p, eol_flag);
24665 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24666 #ifdef subprocesses
24667 obj = Fget_buffer_process (Fcurrent_buffer ());
24668 if (PROCESSP (obj))
24670 p = decode_mode_spec_coding
24671 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24672 p = decode_mode_spec_coding
24673 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24675 #endif /* subprocesses */
24676 #endif /* false */
24677 *p = 0;
24678 return decode_mode_spec_buf;
24682 if (STRINGP (obj))
24684 *string = obj;
24685 return SSDATA (obj);
24687 else
24688 return "";
24692 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24693 means count lines back from START_BYTE. But don't go beyond
24694 LIMIT_BYTE. Return the number of lines thus found (always
24695 nonnegative).
24697 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24698 either the position COUNT lines after/before START_BYTE, if we
24699 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24700 COUNT lines. */
24702 static ptrdiff_t
24703 display_count_lines (ptrdiff_t start_byte,
24704 ptrdiff_t limit_byte, ptrdiff_t count,
24705 ptrdiff_t *byte_pos_ptr)
24707 register unsigned char *cursor;
24708 unsigned char *base;
24710 register ptrdiff_t ceiling;
24711 register unsigned char *ceiling_addr;
24712 ptrdiff_t orig_count = count;
24714 /* If we are not in selective display mode,
24715 check only for newlines. */
24716 bool selective_display
24717 = (!NILP (BVAR (current_buffer, selective_display))
24718 && !INTEGERP (BVAR (current_buffer, selective_display)));
24720 if (count > 0)
24722 while (start_byte < limit_byte)
24724 ceiling = BUFFER_CEILING_OF (start_byte);
24725 ceiling = min (limit_byte - 1, ceiling);
24726 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24727 base = (cursor = BYTE_POS_ADDR (start_byte));
24731 if (selective_display)
24733 while (*cursor != '\n' && *cursor != 015
24734 && ++cursor != ceiling_addr)
24735 continue;
24736 if (cursor == ceiling_addr)
24737 break;
24739 else
24741 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24742 if (! cursor)
24743 break;
24746 cursor++;
24748 if (--count == 0)
24750 start_byte += cursor - base;
24751 *byte_pos_ptr = start_byte;
24752 return orig_count;
24755 while (cursor < ceiling_addr);
24757 start_byte += ceiling_addr - base;
24760 else
24762 while (start_byte > limit_byte)
24764 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24765 ceiling = max (limit_byte, ceiling);
24766 ceiling_addr = BYTE_POS_ADDR (ceiling);
24767 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24768 while (true)
24770 if (selective_display)
24772 while (--cursor >= ceiling_addr
24773 && *cursor != '\n' && *cursor != 015)
24774 continue;
24775 if (cursor < ceiling_addr)
24776 break;
24778 else
24780 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24781 if (! cursor)
24782 break;
24785 if (++count == 0)
24787 start_byte += cursor - base + 1;
24788 *byte_pos_ptr = start_byte;
24789 /* When scanning backwards, we should
24790 not count the newline posterior to which we stop. */
24791 return - orig_count - 1;
24794 start_byte += ceiling_addr - base;
24798 *byte_pos_ptr = limit_byte;
24800 if (count < 0)
24801 return - orig_count + count;
24802 return orig_count - count;
24808 /***********************************************************************
24809 Displaying strings
24810 ***********************************************************************/
24812 /* Display a NUL-terminated string, starting with index START.
24814 If STRING is non-null, display that C string. Otherwise, the Lisp
24815 string LISP_STRING is displayed. There's a case that STRING is
24816 non-null and LISP_STRING is not nil. It means STRING is a string
24817 data of LISP_STRING. In that case, we display LISP_STRING while
24818 ignoring its text properties.
24820 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24821 FACE_STRING. Display STRING or LISP_STRING with the face at
24822 FACE_STRING_POS in FACE_STRING:
24824 Display the string in the environment given by IT, but use the
24825 standard display table, temporarily.
24827 FIELD_WIDTH is the minimum number of output glyphs to produce.
24828 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24829 with spaces. If STRING has more characters, more than FIELD_WIDTH
24830 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24832 PRECISION is the maximum number of characters to output from
24833 STRING. PRECISION < 0 means don't truncate the string.
24835 This is roughly equivalent to printf format specifiers:
24837 FIELD_WIDTH PRECISION PRINTF
24838 ----------------------------------------
24839 -1 -1 %s
24840 -1 10 %.10s
24841 10 -1 %10s
24842 20 10 %20.10s
24844 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24845 display them, and < 0 means obey the current buffer's value of
24846 enable_multibyte_characters.
24848 Value is the number of columns displayed. */
24850 static int
24851 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24852 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24853 int field_width, int precision, int max_x, int multibyte)
24855 int hpos_at_start = it->hpos;
24856 int saved_face_id = it->face_id;
24857 struct glyph_row *row = it->glyph_row;
24858 ptrdiff_t it_charpos;
24860 /* Initialize the iterator IT for iteration over STRING beginning
24861 with index START. */
24862 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24863 precision, field_width, multibyte);
24864 if (string && STRINGP (lisp_string))
24865 /* LISP_STRING is the one returned by decode_mode_spec. We should
24866 ignore its text properties. */
24867 it->stop_charpos = it->end_charpos;
24869 /* If displaying STRING, set up the face of the iterator from
24870 FACE_STRING, if that's given. */
24871 if (STRINGP (face_string))
24873 ptrdiff_t endptr;
24874 struct face *face;
24876 it->face_id
24877 = face_at_string_position (it->w, face_string, face_string_pos,
24878 0, &endptr, it->base_face_id, false);
24879 face = FACE_FROM_ID (it->f, it->face_id);
24880 it->face_box_p = face->box != FACE_NO_BOX;
24883 /* Set max_x to the maximum allowed X position. Don't let it go
24884 beyond the right edge of the window. */
24885 if (max_x <= 0)
24886 max_x = it->last_visible_x;
24887 else
24888 max_x = min (max_x, it->last_visible_x);
24890 /* Skip over display elements that are not visible. because IT->w is
24891 hscrolled. */
24892 if (it->current_x < it->first_visible_x)
24893 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24894 MOVE_TO_POS | MOVE_TO_X);
24896 row->ascent = it->max_ascent;
24897 row->height = it->max_ascent + it->max_descent;
24898 row->phys_ascent = it->max_phys_ascent;
24899 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24900 row->extra_line_spacing = it->max_extra_line_spacing;
24902 if (STRINGP (it->string))
24903 it_charpos = IT_STRING_CHARPOS (*it);
24904 else
24905 it_charpos = IT_CHARPOS (*it);
24907 /* This condition is for the case that we are called with current_x
24908 past last_visible_x. */
24909 while (it->current_x < max_x)
24911 int x_before, x, n_glyphs_before, i, nglyphs;
24913 /* Get the next display element. */
24914 if (!get_next_display_element (it))
24915 break;
24917 /* Produce glyphs. */
24918 x_before = it->current_x;
24919 n_glyphs_before = row->used[TEXT_AREA];
24920 PRODUCE_GLYPHS (it);
24922 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24923 i = 0;
24924 x = x_before;
24925 while (i < nglyphs)
24927 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24929 if (it->line_wrap != TRUNCATE
24930 && x + glyph->pixel_width > max_x)
24932 /* End of continued line or max_x reached. */
24933 if (CHAR_GLYPH_PADDING_P (*glyph))
24935 /* A wide character is unbreakable. */
24936 if (row->reversed_p)
24937 unproduce_glyphs (it, row->used[TEXT_AREA]
24938 - n_glyphs_before);
24939 row->used[TEXT_AREA] = n_glyphs_before;
24940 it->current_x = x_before;
24942 else
24944 if (row->reversed_p)
24945 unproduce_glyphs (it, row->used[TEXT_AREA]
24946 - (n_glyphs_before + i));
24947 row->used[TEXT_AREA] = n_glyphs_before + i;
24948 it->current_x = x;
24950 break;
24952 else if (x + glyph->pixel_width >= it->first_visible_x)
24954 /* Glyph is at least partially visible. */
24955 ++it->hpos;
24956 if (x < it->first_visible_x)
24957 row->x = x - it->first_visible_x;
24959 else
24961 /* Glyph is off the left margin of the display area.
24962 Should not happen. */
24963 emacs_abort ();
24966 row->ascent = max (row->ascent, it->max_ascent);
24967 row->height = max (row->height, it->max_ascent + it->max_descent);
24968 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24969 row->phys_height = max (row->phys_height,
24970 it->max_phys_ascent + it->max_phys_descent);
24971 row->extra_line_spacing = max (row->extra_line_spacing,
24972 it->max_extra_line_spacing);
24973 x += glyph->pixel_width;
24974 ++i;
24977 /* Stop if max_x reached. */
24978 if (i < nglyphs)
24979 break;
24981 /* Stop at line ends. */
24982 if (ITERATOR_AT_END_OF_LINE_P (it))
24984 it->continuation_lines_width = 0;
24985 break;
24988 set_iterator_to_next (it, true);
24989 if (STRINGP (it->string))
24990 it_charpos = IT_STRING_CHARPOS (*it);
24991 else
24992 it_charpos = IT_CHARPOS (*it);
24994 /* Stop if truncating at the right edge. */
24995 if (it->line_wrap == TRUNCATE
24996 && it->current_x >= it->last_visible_x)
24998 /* Add truncation mark, but don't do it if the line is
24999 truncated at a padding space. */
25000 if (it_charpos < it->string_nchars)
25002 if (!FRAME_WINDOW_P (it->f))
25004 int ii, n;
25006 if (it->current_x > it->last_visible_x)
25008 if (!row->reversed_p)
25010 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25011 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25012 break;
25014 else
25016 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25017 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25018 break;
25019 unproduce_glyphs (it, ii + 1);
25020 ii = row->used[TEXT_AREA] - (ii + 1);
25022 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25024 row->used[TEXT_AREA] = ii;
25025 produce_special_glyphs (it, IT_TRUNCATION);
25028 produce_special_glyphs (it, IT_TRUNCATION);
25030 row->truncated_on_right_p = true;
25032 break;
25036 /* Maybe insert a truncation at the left. */
25037 if (it->first_visible_x
25038 && it_charpos > 0)
25040 if (!FRAME_WINDOW_P (it->f)
25041 || (row->reversed_p
25042 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25043 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25044 insert_left_trunc_glyphs (it);
25045 row->truncated_on_left_p = true;
25048 it->face_id = saved_face_id;
25050 /* Value is number of columns displayed. */
25051 return it->hpos - hpos_at_start;
25056 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25057 appears as an element of LIST or as the car of an element of LIST.
25058 If PROPVAL is a list, compare each element against LIST in that
25059 way, and return 1/2 if any element of PROPVAL is found in LIST.
25060 Otherwise return 0. This function cannot quit.
25061 The return value is 2 if the text is invisible but with an ellipsis
25062 and 1 if it's invisible and without an ellipsis. */
25065 invisible_prop (Lisp_Object propval, Lisp_Object list)
25067 Lisp_Object tail, proptail;
25069 for (tail = list; CONSP (tail); tail = XCDR (tail))
25071 register Lisp_Object tem;
25072 tem = XCAR (tail);
25073 if (EQ (propval, tem))
25074 return 1;
25075 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25076 return NILP (XCDR (tem)) ? 1 : 2;
25079 if (CONSP (propval))
25081 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25083 Lisp_Object propelt;
25084 propelt = XCAR (proptail);
25085 for (tail = list; CONSP (tail); tail = XCDR (tail))
25087 register Lisp_Object tem;
25088 tem = XCAR (tail);
25089 if (EQ (propelt, tem))
25090 return 1;
25091 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25092 return NILP (XCDR (tem)) ? 1 : 2;
25097 return 0;
25100 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25101 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25102 POS should be a marker or a buffer position; the value of the `invisible'
25103 property at that position in the current buffer is examined.
25104 POS can also be the actual value of the `invisible' text or overlay
25105 property of the text of interest, in which case the value itself is
25106 examined.
25108 The non-nil value returned can be t for currently invisible text that is
25109 entirely hidden on display, or some other non-nil, non-t value if the
25110 text is replaced by an ellipsis.
25112 Note that whether text with `invisible' property is actually hidden on
25113 display may depend on `buffer-invisibility-spec', which see. */)
25114 (Lisp_Object pos)
25116 Lisp_Object prop
25117 = (NATNUMP (pos) || MARKERP (pos)
25118 ? Fget_char_property (pos, Qinvisible, Qnil)
25119 : pos);
25120 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25121 return (invis == 0 ? Qnil
25122 : invis == 1 ? Qt
25123 : make_number (invis));
25126 /* Calculate a width or height in pixels from a specification using
25127 the following elements:
25129 SPEC ::=
25130 NUM - a (fractional) multiple of the default font width/height
25131 (NUM) - specifies exactly NUM pixels
25132 UNIT - a fixed number of pixels, see below.
25133 ELEMENT - size of a display element in pixels, see below.
25134 (NUM . SPEC) - equals NUM * SPEC
25135 (+ SPEC SPEC ...) - add pixel values
25136 (- SPEC SPEC ...) - subtract pixel values
25137 (- SPEC) - negate pixel value
25139 NUM ::=
25140 INT or FLOAT - a number constant
25141 SYMBOL - use symbol's (buffer local) variable binding.
25143 UNIT ::=
25144 in - pixels per inch *)
25145 mm - pixels per 1/1000 meter *)
25146 cm - pixels per 1/100 meter *)
25147 width - width of current font in pixels.
25148 height - height of current font in pixels.
25150 *) using the ratio(s) defined in display-pixels-per-inch.
25152 ELEMENT ::=
25154 left-fringe - left fringe width in pixels
25155 right-fringe - right fringe width in pixels
25157 left-margin - left margin width in pixels
25158 right-margin - right margin width in pixels
25160 scroll-bar - scroll-bar area width in pixels
25162 Examples:
25164 Pixels corresponding to 5 inches:
25165 (5 . in)
25167 Total width of non-text areas on left side of window (if scroll-bar is on left):
25168 '(space :width (+ left-fringe left-margin scroll-bar))
25170 Align to first text column (in header line):
25171 '(space :align-to 0)
25173 Align to middle of text area minus half the width of variable `my-image'
25174 containing a loaded image:
25175 '(space :align-to (0.5 . (- text my-image)))
25177 Width of left margin minus width of 1 character in the default font:
25178 '(space :width (- left-margin 1))
25180 Width of left margin minus width of 2 characters in the current font:
25181 '(space :width (- left-margin (2 . width)))
25183 Center 1 character over left-margin (in header line):
25184 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25186 Different ways to express width of left fringe plus left margin minus one pixel:
25187 '(space :width (- (+ left-fringe left-margin) (1)))
25188 '(space :width (+ left-fringe left-margin (- (1))))
25189 '(space :width (+ left-fringe left-margin (-1)))
25191 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25192 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25193 coordinate, and *RES is the additional pixel width from that point
25194 till the end of the stretch glyph.
25196 WIDTH_P non-zero means take the width dimension or X coordinate of
25197 the object specified by PROP, WIDTH_P zero means take the height
25198 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25199 non-NULL, WIDTH_P should be non-zero.)
25201 FONT is the font of the face of the surrounding text.
25203 The return value is non-zero if width or height were successfully
25204 calculated, i.e. if PROP is a valid spec. */
25206 static bool
25207 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25208 struct font *font, bool width_p, int *align_to)
25210 double pixels;
25212 # define OK_PIXELS(val) (*res = (val), true)
25213 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25215 if (NILP (prop))
25216 return OK_PIXELS (0);
25218 eassert (FRAME_LIVE_P (it->f));
25220 if (SYMBOLP (prop))
25222 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25224 char *unit = SSDATA (SYMBOL_NAME (prop));
25226 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25227 if (unit[0] == 'i' && unit[1] == 'n')
25228 pixels = 1.0;
25229 else if (unit[0] == 'm' && unit[1] == 'm')
25230 pixels = 25.4;
25231 else if (unit[0] == 'c' && unit[1] == 'm')
25232 pixels = 2.54;
25233 else
25234 pixels = 0;
25235 if (pixels > 0)
25237 double ppi = (width_p ? FRAME_RES_X (it->f)
25238 : FRAME_RES_Y (it->f));
25240 if (ppi > 0)
25241 return OK_PIXELS (ppi / pixels);
25242 return false;
25246 #ifdef HAVE_WINDOW_SYSTEM
25247 /* 'height': the height of FONT. */
25248 if (EQ (prop, Qheight))
25249 return OK_PIXELS (font
25250 ? normal_char_height (font, -1)
25251 : FRAME_LINE_HEIGHT (it->f));
25252 /* 'width': the width of FONT. */
25253 if (EQ (prop, Qwidth))
25254 return OK_PIXELS (font
25255 ? FONT_WIDTH (font)
25256 : FRAME_COLUMN_WIDTH (it->f));
25257 #else
25258 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25259 return OK_PIXELS (1);
25260 #endif
25262 /* 'text': the width or height of the text area. */
25263 if (EQ (prop, Qtext))
25264 return OK_PIXELS (width_p
25265 ? (window_box_width (it->w, TEXT_AREA)
25266 - it->lnum_pixel_width)
25267 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25269 /* ':align_to'. First time we compute the value, window
25270 elements are interpreted as the position of the element's
25271 left edge. */
25272 if (align_to && *align_to < 0)
25274 *res = 0;
25275 /* 'left': left edge of the text area. */
25276 if (EQ (prop, Qleft))
25277 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25278 + it->lnum_pixel_width);
25279 /* 'right': right edge of the text area. */
25280 if (EQ (prop, Qright))
25281 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25282 /* 'center': the center of the text area. */
25283 if (EQ (prop, Qcenter))
25284 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25285 + it->lnum_pixel_width
25286 + window_box_width (it->w, TEXT_AREA) / 2);
25287 /* 'left-fringe': left edge of the left fringe. */
25288 if (EQ (prop, Qleft_fringe))
25289 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25290 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25291 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25292 /* 'right-fringe': left edge of the right fringe. */
25293 if (EQ (prop, Qright_fringe))
25294 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25295 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25296 : window_box_right_offset (it->w, TEXT_AREA));
25297 /* 'left-margin': left edge of the left display margin. */
25298 if (EQ (prop, Qleft_margin))
25299 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25300 /* 'right-margin': left edge of the right display margin. */
25301 if (EQ (prop, Qright_margin))
25302 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25303 /* 'scroll-bar': left edge of the vertical scroll bar. */
25304 if (EQ (prop, Qscroll_bar))
25305 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25307 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25309 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25310 : 0)));
25312 else
25314 /* Otherwise, the elements stand for their width. */
25315 if (EQ (prop, Qleft_fringe))
25316 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25317 if (EQ (prop, Qright_fringe))
25318 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25319 if (EQ (prop, Qleft_margin))
25320 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25321 if (EQ (prop, Qright_margin))
25322 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25323 if (EQ (prop, Qscroll_bar))
25324 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25327 prop = buffer_local_value (prop, it->w->contents);
25328 if (EQ (prop, Qunbound))
25329 prop = Qnil;
25332 if (NUMBERP (prop))
25334 int base_unit = (width_p
25335 ? FRAME_COLUMN_WIDTH (it->f)
25336 : FRAME_LINE_HEIGHT (it->f));
25337 if (width_p && align_to && *align_to < 0)
25338 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25339 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25342 if (CONSP (prop))
25344 Lisp_Object car = XCAR (prop);
25345 Lisp_Object cdr = XCDR (prop);
25347 if (SYMBOLP (car))
25349 #ifdef HAVE_WINDOW_SYSTEM
25350 /* '(image PROPS...)': width or height of the specified image. */
25351 if (FRAME_WINDOW_P (it->f)
25352 && valid_image_p (prop))
25354 ptrdiff_t id = lookup_image (it->f, prop);
25355 struct image *img = IMAGE_FROM_ID (it->f, id);
25357 return OK_PIXELS (width_p ? img->width : img->height);
25359 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25360 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25362 /* TODO: Don't return dummy size. */
25363 return OK_PIXELS (100);
25365 #endif
25366 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25367 recursively calculated values. */
25368 if (EQ (car, Qplus) || EQ (car, Qminus))
25370 bool first = true;
25371 double px;
25373 pixels = 0;
25374 while (CONSP (cdr))
25376 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25377 font, width_p, align_to))
25378 return false;
25379 if (first)
25380 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25381 else
25382 pixels += px;
25383 cdr = XCDR (cdr);
25385 if (EQ (car, Qminus))
25386 pixels = -pixels;
25387 return OK_PIXELS (pixels);
25390 car = buffer_local_value (car, it->w->contents);
25391 if (EQ (car, Qunbound))
25392 car = Qnil;
25395 /* '(NUM)': absolute number of pixels. */
25396 if (NUMBERP (car))
25398 double fact;
25399 int offset =
25400 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25401 pixels = XFLOATINT (car);
25402 if (NILP (cdr))
25403 return OK_PIXELS (pixels + offset);
25404 if (calc_pixel_width_or_height (&fact, it, cdr,
25405 font, width_p, align_to))
25406 return OK_PIXELS (pixels * fact + offset);
25407 return false;
25410 return false;
25413 return false;
25416 void
25417 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25419 #ifdef HAVE_WINDOW_SYSTEM
25420 normal_char_ascent_descent (font, -1, ascent, descent);
25421 #else
25422 *ascent = 1;
25423 *descent = 0;
25424 #endif
25428 /***********************************************************************
25429 Glyph Display
25430 ***********************************************************************/
25432 #ifdef HAVE_WINDOW_SYSTEM
25434 #ifdef GLYPH_DEBUG
25436 void
25437 dump_glyph_string (struct glyph_string *s)
25439 fprintf (stderr, "glyph string\n");
25440 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25441 s->x, s->y, s->width, s->height);
25442 fprintf (stderr, " ybase = %d\n", s->ybase);
25443 fprintf (stderr, " hl = %u\n", s->hl);
25444 fprintf (stderr, " left overhang = %d, right = %d\n",
25445 s->left_overhang, s->right_overhang);
25446 fprintf (stderr, " nchars = %d\n", s->nchars);
25447 fprintf (stderr, " extends to end of line = %d\n",
25448 s->extends_to_end_of_line_p);
25449 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25450 fprintf (stderr, " bg width = %d\n", s->background_width);
25453 #endif /* GLYPH_DEBUG */
25455 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25456 of XChar2b structures for S; it can't be allocated in
25457 init_glyph_string because it must be allocated via `alloca'. W
25458 is the window on which S is drawn. ROW and AREA are the glyph row
25459 and area within the row from which S is constructed. START is the
25460 index of the first glyph structure covered by S. HL is a
25461 face-override for drawing S. */
25463 #ifdef HAVE_NTGUI
25464 #define OPTIONAL_HDC(hdc) HDC hdc,
25465 #define DECLARE_HDC(hdc) HDC hdc;
25466 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25467 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25468 #endif
25470 #ifndef OPTIONAL_HDC
25471 #define OPTIONAL_HDC(hdc)
25472 #define DECLARE_HDC(hdc)
25473 #define ALLOCATE_HDC(hdc, f)
25474 #define RELEASE_HDC(hdc, f)
25475 #endif
25477 static void
25478 init_glyph_string (struct glyph_string *s,
25479 OPTIONAL_HDC (hdc)
25480 XChar2b *char2b, struct window *w, struct glyph_row *row,
25481 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25483 memset (s, 0, sizeof *s);
25484 s->w = w;
25485 s->f = XFRAME (w->frame);
25486 #ifdef HAVE_NTGUI
25487 s->hdc = hdc;
25488 #endif
25489 s->display = FRAME_X_DISPLAY (s->f);
25490 s->char2b = char2b;
25491 s->hl = hl;
25492 s->row = row;
25493 s->area = area;
25494 s->first_glyph = row->glyphs[area] + start;
25495 s->height = row->height;
25496 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25497 s->ybase = s->y + row->ascent;
25501 /* Append the list of glyph strings with head H and tail T to the list
25502 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25504 static void
25505 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25506 struct glyph_string *h, struct glyph_string *t)
25508 if (h)
25510 if (*head)
25511 (*tail)->next = h;
25512 else
25513 *head = h;
25514 h->prev = *tail;
25515 *tail = t;
25520 /* Prepend the list of glyph strings with head H and tail T to the
25521 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25522 result. */
25524 static void
25525 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25526 struct glyph_string *h, struct glyph_string *t)
25528 if (h)
25530 if (*head)
25531 (*head)->prev = t;
25532 else
25533 *tail = t;
25534 t->next = *head;
25535 *head = h;
25540 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25541 Set *HEAD and *TAIL to the resulting list. */
25543 static void
25544 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25545 struct glyph_string *s)
25547 s->next = s->prev = NULL;
25548 append_glyph_string_lists (head, tail, s, s);
25552 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25553 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25554 make sure that X resources for the face returned are allocated.
25555 Value is a pointer to a realized face that is ready for display if
25556 DISPLAY_P. */
25558 static struct face *
25559 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25560 XChar2b *char2b, bool display_p)
25562 struct face *face = FACE_FROM_ID (f, face_id);
25563 unsigned code = 0;
25565 if (face->font)
25567 code = face->font->driver->encode_char (face->font, c);
25569 if (code == FONT_INVALID_CODE)
25570 code = 0;
25572 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25574 /* Make sure X resources of the face are allocated. */
25575 #ifdef HAVE_X_WINDOWS
25576 if (display_p)
25577 #endif
25579 eassert (face != NULL);
25580 prepare_face_for_display (f, face);
25583 return face;
25587 /* Get face and two-byte form of character glyph GLYPH on frame F.
25588 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25589 a pointer to a realized face that is ready for display. */
25591 static struct face *
25592 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25593 XChar2b *char2b)
25595 struct face *face;
25596 unsigned code = 0;
25598 eassert (glyph->type == CHAR_GLYPH);
25599 face = FACE_FROM_ID (f, glyph->face_id);
25601 /* Make sure X resources of the face are allocated. */
25602 prepare_face_for_display (f, face);
25604 if (face->font)
25606 if (CHAR_BYTE8_P (glyph->u.ch))
25607 code = CHAR_TO_BYTE8 (glyph->u.ch);
25608 else
25609 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25611 if (code == FONT_INVALID_CODE)
25612 code = 0;
25615 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25616 return face;
25620 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25621 Return true iff FONT has a glyph for C. */
25623 static bool
25624 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25626 unsigned code;
25628 if (CHAR_BYTE8_P (c))
25629 code = CHAR_TO_BYTE8 (c);
25630 else
25631 code = font->driver->encode_char (font, c);
25633 if (code == FONT_INVALID_CODE)
25634 return false;
25635 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25636 return true;
25640 /* Fill glyph string S with composition components specified by S->cmp.
25642 BASE_FACE is the base face of the composition.
25643 S->cmp_from is the index of the first component for S.
25645 OVERLAPS non-zero means S should draw the foreground only, and use
25646 its physical height for clipping. See also draw_glyphs.
25648 Value is the index of a component not in S. */
25650 static int
25651 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25652 int overlaps)
25654 int i;
25655 /* For all glyphs of this composition, starting at the offset
25656 S->cmp_from, until we reach the end of the definition or encounter a
25657 glyph that requires the different face, add it to S. */
25658 struct face *face;
25660 eassert (s);
25662 s->for_overlaps = overlaps;
25663 s->face = NULL;
25664 s->font = NULL;
25665 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25667 int c = COMPOSITION_GLYPH (s->cmp, i);
25669 /* TAB in a composition means display glyphs with padding space
25670 on the left or right. */
25671 if (c != '\t')
25673 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25674 -1, Qnil);
25676 face = get_char_face_and_encoding (s->f, c, face_id,
25677 s->char2b + i, true);
25678 if (face)
25680 if (! s->face)
25682 s->face = face;
25683 s->font = s->face->font;
25685 else if (s->face != face)
25686 break;
25689 ++s->nchars;
25691 s->cmp_to = i;
25693 if (s->face == NULL)
25695 s->face = base_face->ascii_face;
25696 s->font = s->face->font;
25699 /* All glyph strings for the same composition has the same width,
25700 i.e. the width set for the first component of the composition. */
25701 s->width = s->first_glyph->pixel_width;
25703 /* If the specified font could not be loaded, use the frame's
25704 default font, but record the fact that we couldn't load it in
25705 the glyph string so that we can draw rectangles for the
25706 characters of the glyph string. */
25707 if (s->font == NULL)
25709 s->font_not_found_p = true;
25710 s->font = FRAME_FONT (s->f);
25713 /* Adjust base line for subscript/superscript text. */
25714 s->ybase += s->first_glyph->voffset;
25716 return s->cmp_to;
25719 static int
25720 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25721 int start, int end, int overlaps)
25723 struct glyph *glyph, *last;
25724 Lisp_Object lgstring;
25725 int i;
25727 s->for_overlaps = overlaps;
25728 glyph = s->row->glyphs[s->area] + start;
25729 last = s->row->glyphs[s->area] + end;
25730 s->cmp_id = glyph->u.cmp.id;
25731 s->cmp_from = glyph->slice.cmp.from;
25732 s->cmp_to = glyph->slice.cmp.to + 1;
25733 s->face = FACE_FROM_ID (s->f, face_id);
25734 lgstring = composition_gstring_from_id (s->cmp_id);
25735 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25736 glyph++;
25737 while (glyph < last
25738 && glyph->u.cmp.automatic
25739 && glyph->u.cmp.id == s->cmp_id
25740 && s->cmp_to == glyph->slice.cmp.from)
25741 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25743 for (i = s->cmp_from; i < s->cmp_to; i++)
25745 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25746 unsigned code = LGLYPH_CODE (lglyph);
25748 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25750 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25751 return glyph - s->row->glyphs[s->area];
25755 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25756 See the comment of fill_glyph_string for arguments.
25757 Value is the index of the first glyph not in S. */
25760 static int
25761 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25762 int start, int end, int overlaps)
25764 struct glyph *glyph, *last;
25765 int voffset;
25767 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25768 s->for_overlaps = overlaps;
25769 glyph = s->row->glyphs[s->area] + start;
25770 last = s->row->glyphs[s->area] + end;
25771 voffset = glyph->voffset;
25772 s->face = FACE_FROM_ID (s->f, face_id);
25773 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25774 s->nchars = 1;
25775 s->width = glyph->pixel_width;
25776 glyph++;
25777 while (glyph < last
25778 && glyph->type == GLYPHLESS_GLYPH
25779 && glyph->voffset == voffset
25780 && glyph->face_id == face_id)
25782 s->nchars++;
25783 s->width += glyph->pixel_width;
25784 glyph++;
25786 s->ybase += voffset;
25787 return glyph - s->row->glyphs[s->area];
25791 /* Fill glyph string S from a sequence of character glyphs.
25793 FACE_ID is the face id of the string. START is the index of the
25794 first glyph to consider, END is the index of the last + 1.
25795 OVERLAPS non-zero means S should draw the foreground only, and use
25796 its physical height for clipping. See also draw_glyphs.
25798 Value is the index of the first glyph not in S. */
25800 static int
25801 fill_glyph_string (struct glyph_string *s, int face_id,
25802 int start, int end, int overlaps)
25804 struct glyph *glyph, *last;
25805 int voffset;
25806 bool glyph_not_available_p;
25808 eassert (s->f == XFRAME (s->w->frame));
25809 eassert (s->nchars == 0);
25810 eassert (start >= 0 && end > start);
25812 s->for_overlaps = overlaps;
25813 glyph = s->row->glyphs[s->area] + start;
25814 last = s->row->glyphs[s->area] + end;
25815 voffset = glyph->voffset;
25816 s->padding_p = glyph->padding_p;
25817 glyph_not_available_p = glyph->glyph_not_available_p;
25819 while (glyph < last
25820 && glyph->type == CHAR_GLYPH
25821 && glyph->voffset == voffset
25822 /* Same face id implies same font, nowadays. */
25823 && glyph->face_id == face_id
25824 && glyph->glyph_not_available_p == glyph_not_available_p)
25826 s->face = get_glyph_face_and_encoding (s->f, glyph,
25827 s->char2b + s->nchars);
25828 ++s->nchars;
25829 eassert (s->nchars <= end - start);
25830 s->width += glyph->pixel_width;
25831 if (glyph++->padding_p != s->padding_p)
25832 break;
25835 s->font = s->face->font;
25837 /* If the specified font could not be loaded, use the frame's font,
25838 but record the fact that we couldn't load it in
25839 S->font_not_found_p so that we can draw rectangles for the
25840 characters of the glyph string. */
25841 if (s->font == NULL || glyph_not_available_p)
25843 s->font_not_found_p = true;
25844 s->font = FRAME_FONT (s->f);
25847 /* Adjust base line for subscript/superscript text. */
25848 s->ybase += voffset;
25850 eassert (s->face && s->face->gc);
25851 return glyph - s->row->glyphs[s->area];
25855 /* Fill glyph string S from image glyph S->first_glyph. */
25857 static void
25858 fill_image_glyph_string (struct glyph_string *s)
25860 eassert (s->first_glyph->type == IMAGE_GLYPH);
25861 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25862 eassert (s->img);
25863 s->slice = s->first_glyph->slice.img;
25864 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25865 s->font = s->face->font;
25866 s->width = s->first_glyph->pixel_width;
25868 /* Adjust base line for subscript/superscript text. */
25869 s->ybase += s->first_glyph->voffset;
25873 #ifdef HAVE_XWIDGETS
25874 static void
25875 fill_xwidget_glyph_string (struct glyph_string *s)
25877 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25878 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25879 s->font = s->face->font;
25880 s->width = s->first_glyph->pixel_width;
25881 s->ybase += s->first_glyph->voffset;
25882 s->xwidget = s->first_glyph->u.xwidget;
25884 #endif
25885 /* Fill glyph string S from a sequence of stretch glyphs.
25887 START is the index of the first glyph to consider,
25888 END is the index of the last + 1.
25890 Value is the index of the first glyph not in S. */
25892 static int
25893 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25895 struct glyph *glyph, *last;
25896 int voffset, face_id;
25898 eassert (s->first_glyph->type == STRETCH_GLYPH);
25900 glyph = s->row->glyphs[s->area] + start;
25901 last = s->row->glyphs[s->area] + end;
25902 face_id = glyph->face_id;
25903 s->face = FACE_FROM_ID (s->f, face_id);
25904 s->font = s->face->font;
25905 s->width = glyph->pixel_width;
25906 s->nchars = 1;
25907 voffset = glyph->voffset;
25909 for (++glyph;
25910 (glyph < last
25911 && glyph->type == STRETCH_GLYPH
25912 && glyph->voffset == voffset
25913 && glyph->face_id == face_id);
25914 ++glyph)
25915 s->width += glyph->pixel_width;
25917 /* Adjust base line for subscript/superscript text. */
25918 s->ybase += voffset;
25920 /* The case that face->gc == 0 is handled when drawing the glyph
25921 string by calling prepare_face_for_display. */
25922 eassert (s->face);
25923 return glyph - s->row->glyphs[s->area];
25926 static struct font_metrics *
25927 get_per_char_metric (struct font *font, XChar2b *char2b)
25929 static struct font_metrics metrics;
25930 unsigned code;
25932 if (! font)
25933 return NULL;
25934 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25935 if (code == FONT_INVALID_CODE)
25936 return NULL;
25937 font->driver->text_extents (font, &code, 1, &metrics);
25938 return &metrics;
25941 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25942 for FONT. Values are taken from font-global ones, except for fonts
25943 that claim preposterously large values, but whose glyphs actually
25944 have reasonable dimensions. C is the character to use for metrics
25945 if the font-global values are too large; if C is negative, the
25946 function selects a default character. */
25947 static void
25948 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25950 *ascent = FONT_BASE (font);
25951 *descent = FONT_DESCENT (font);
25953 if (FONT_TOO_HIGH (font))
25955 XChar2b char2b;
25957 /* Get metrics of C, defaulting to a reasonably sized ASCII
25958 character. */
25959 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25961 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25963 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25965 /* We add 1 pixel to character dimensions as heuristics
25966 that produces nicer display, e.g. when the face has
25967 the box attribute. */
25968 *ascent = pcm->ascent + 1;
25969 *descent = pcm->descent + 1;
25975 /* A subroutine that computes a reasonable "normal character height"
25976 for fonts that claim preposterously large vertical dimensions, but
25977 whose glyphs are actually reasonably sized. C is the character
25978 whose metrics to use for those fonts, or -1 for default
25979 character. */
25980 static int
25981 normal_char_height (struct font *font, int c)
25983 int ascent, descent;
25985 normal_char_ascent_descent (font, c, &ascent, &descent);
25987 return ascent + descent;
25990 /* EXPORT for RIF:
25991 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25992 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25993 assumed to be zero. */
25995 void
25996 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25998 *left = *right = 0;
26000 if (glyph->type == CHAR_GLYPH)
26002 XChar2b char2b;
26003 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26004 if (face->font)
26006 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26007 if (pcm)
26009 if (pcm->rbearing > pcm->width)
26010 *right = pcm->rbearing - pcm->width;
26011 if (pcm->lbearing < 0)
26012 *left = -pcm->lbearing;
26016 else if (glyph->type == COMPOSITE_GLYPH)
26018 if (! glyph->u.cmp.automatic)
26020 struct composition *cmp = composition_table[glyph->u.cmp.id];
26022 if (cmp->rbearing > cmp->pixel_width)
26023 *right = cmp->rbearing - cmp->pixel_width;
26024 if (cmp->lbearing < 0)
26025 *left = - cmp->lbearing;
26027 else
26029 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26030 struct font_metrics metrics;
26032 composition_gstring_width (gstring, glyph->slice.cmp.from,
26033 glyph->slice.cmp.to + 1, &metrics);
26034 if (metrics.rbearing > metrics.width)
26035 *right = metrics.rbearing - metrics.width;
26036 if (metrics.lbearing < 0)
26037 *left = - metrics.lbearing;
26043 /* Return the index of the first glyph preceding glyph string S that
26044 is overwritten by S because of S's left overhang. Value is -1
26045 if no glyphs are overwritten. */
26047 static int
26048 left_overwritten (struct glyph_string *s)
26050 int k;
26052 if (s->left_overhang)
26054 int x = 0, i;
26055 struct glyph *glyphs = s->row->glyphs[s->area];
26056 int first = s->first_glyph - glyphs;
26058 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26059 x -= glyphs[i].pixel_width;
26061 k = i + 1;
26063 else
26064 k = -1;
26066 return k;
26070 /* Return the index of the first glyph preceding glyph string S that
26071 is overwriting S because of its right overhang. Value is -1 if no
26072 glyph in front of S overwrites S. */
26074 static int
26075 left_overwriting (struct glyph_string *s)
26077 int i, k, x;
26078 struct glyph *glyphs = s->row->glyphs[s->area];
26079 int first = s->first_glyph - glyphs;
26081 k = -1;
26082 x = 0;
26083 for (i = first - 1; i >= 0; --i)
26085 int left, right;
26086 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26087 if (x + right > 0)
26088 k = i;
26089 x -= glyphs[i].pixel_width;
26092 return k;
26096 /* Return the index of the last glyph following glyph string S that is
26097 overwritten by S because of S's right overhang. Value is -1 if
26098 no such glyph is found. */
26100 static int
26101 right_overwritten (struct glyph_string *s)
26103 int k = -1;
26105 if (s->right_overhang)
26107 int x = 0, i;
26108 struct glyph *glyphs = s->row->glyphs[s->area];
26109 int first = (s->first_glyph - glyphs
26110 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26111 int end = s->row->used[s->area];
26113 for (i = first; i < end && s->right_overhang > x; ++i)
26114 x += glyphs[i].pixel_width;
26116 k = i;
26119 return k;
26123 /* Return the index of the last glyph following glyph string S that
26124 overwrites S because of its left overhang. Value is negative
26125 if no such glyph is found. */
26127 static int
26128 right_overwriting (struct glyph_string *s)
26130 int i, k, x;
26131 int end = s->row->used[s->area];
26132 struct glyph *glyphs = s->row->glyphs[s->area];
26133 int first = (s->first_glyph - glyphs
26134 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26136 k = -1;
26137 x = 0;
26138 for (i = first; i < end; ++i)
26140 int left, right;
26141 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26142 if (x - left < 0)
26143 k = i;
26144 x += glyphs[i].pixel_width;
26147 return k;
26151 /* Set background width of glyph string S. START is the index of the
26152 first glyph following S. LAST_X is the right-most x-position + 1
26153 in the drawing area. */
26155 static void
26156 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26158 /* If the face of this glyph string has to be drawn to the end of
26159 the drawing area, set S->extends_to_end_of_line_p. */
26161 if (start == s->row->used[s->area]
26162 && ((s->row->fill_line_p
26163 && (s->hl == DRAW_NORMAL_TEXT
26164 || s->hl == DRAW_IMAGE_RAISED
26165 || s->hl == DRAW_IMAGE_SUNKEN))
26166 || s->hl == DRAW_MOUSE_FACE))
26167 s->extends_to_end_of_line_p = true;
26169 /* If S extends its face to the end of the line, set its
26170 background_width to the distance to the right edge of the drawing
26171 area. */
26172 if (s->extends_to_end_of_line_p)
26173 s->background_width = last_x - s->x + 1;
26174 else
26175 s->background_width = s->width;
26179 /* Return glyph string that shares background with glyph string S and
26180 whose `background_width' member has been set. */
26182 static struct glyph_string *
26183 glyph_string_containing_background_width (struct glyph_string *s)
26185 if (s->cmp)
26186 while (s->cmp_from)
26187 s = s->prev;
26189 return s;
26193 /* Compute overhangs and x-positions for glyph string S and its
26194 predecessors, or successors. X is the starting x-position for S.
26195 BACKWARD_P means process predecessors. */
26197 static void
26198 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26200 if (backward_p)
26202 while (s)
26204 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26205 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26206 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26207 x -= s->width;
26208 s->x = x;
26209 s = s->prev;
26212 else
26214 while (s)
26216 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26217 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26218 s->x = x;
26219 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26220 x += s->width;
26221 s = s->next;
26228 /* The following macros are only called from draw_glyphs below.
26229 They reference the following parameters of that function directly:
26230 `w', `row', `area', and `overlap_p'
26231 as well as the following local variables:
26232 `s', `f', and `hdc' (in W32) */
26234 #ifdef HAVE_NTGUI
26235 /* On W32, silently add local `hdc' variable to argument list of
26236 init_glyph_string. */
26237 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26238 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26239 #else
26240 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26241 init_glyph_string (s, char2b, w, row, area, start, hl)
26242 #endif
26244 /* Add a glyph string for a stretch glyph to the list of strings
26245 between HEAD and TAIL. START is the index of the stretch glyph in
26246 row area AREA of glyph row ROW. END is the index of the last glyph
26247 in that glyph row area. X is the current output position assigned
26248 to the new glyph string constructed. HL overrides that face of the
26249 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26250 is the right-most x-position of the drawing area. */
26252 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26253 and below -- keep them on one line. */
26254 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26255 do \
26257 s = alloca (sizeof *s); \
26258 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26259 START = fill_stretch_glyph_string (s, START, END); \
26260 append_glyph_string (&HEAD, &TAIL, s); \
26261 s->x = (X); \
26263 while (false)
26266 /* Add a glyph string for an image glyph to the list of strings
26267 between HEAD and TAIL. START is the index of the image glyph in
26268 row area AREA of glyph row ROW. END is the index of the last glyph
26269 in that glyph row area. X is the current output position assigned
26270 to the new glyph string constructed. HL overrides that face of the
26271 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26272 is the right-most x-position of the drawing area. */
26274 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26275 do \
26277 s = alloca (sizeof *s); \
26278 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26279 fill_image_glyph_string (s); \
26280 append_glyph_string (&HEAD, &TAIL, s); \
26281 ++START; \
26282 s->x = (X); \
26284 while (false)
26286 #ifndef HAVE_XWIDGETS
26287 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26288 eassume (false)
26289 #else
26290 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26291 do \
26293 s = alloca (sizeof *s); \
26294 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26295 fill_xwidget_glyph_string (s); \
26296 append_glyph_string (&(HEAD), &(TAIL), s); \
26297 ++(START); \
26298 s->x = (X); \
26300 while (false)
26301 #endif
26303 /* Add a glyph string for a sequence of character glyphs to the list
26304 of strings between HEAD and TAIL. START is the index of the first
26305 glyph in row area AREA of glyph row ROW that is part of the new
26306 glyph string. END is the index of the last glyph in that glyph row
26307 area. X is the current output position assigned to the new glyph
26308 string constructed. HL overrides that face of the glyph; e.g. it
26309 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26310 right-most x-position of the drawing area. */
26312 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26313 do \
26315 int face_id; \
26316 XChar2b *char2b; \
26318 face_id = (row)->glyphs[area][START].face_id; \
26320 s = alloca (sizeof *s); \
26321 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26322 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26323 append_glyph_string (&HEAD, &TAIL, s); \
26324 s->x = (X); \
26325 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26327 while (false)
26330 /* Add a glyph string for a composite sequence to the list of strings
26331 between HEAD and TAIL. START is the index of the first glyph in
26332 row area AREA of glyph row ROW that is part of the new glyph
26333 string. END is the index of the last glyph in that glyph row area.
26334 X is the current output position assigned to the new glyph string
26335 constructed. HL overrides that face of the glyph; e.g. it is
26336 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26337 x-position of the drawing area. */
26339 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26340 do { \
26341 int face_id = (row)->glyphs[area][START].face_id; \
26342 struct face *base_face = FACE_FROM_ID (f, face_id); \
26343 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26344 struct composition *cmp = composition_table[cmp_id]; \
26345 XChar2b *char2b; \
26346 struct glyph_string *first_s = NULL; \
26347 int n; \
26349 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26351 /* Make glyph_strings for each glyph sequence that is drawable by \
26352 the same face, and append them to HEAD/TAIL. */ \
26353 for (n = 0; n < cmp->glyph_len;) \
26355 s = alloca (sizeof *s); \
26356 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26357 append_glyph_string (&(HEAD), &(TAIL), s); \
26358 s->cmp = cmp; \
26359 s->cmp_from = n; \
26360 s->x = (X); \
26361 if (n == 0) \
26362 first_s = s; \
26363 n = fill_composite_glyph_string (s, base_face, overlaps); \
26366 ++START; \
26367 s = first_s; \
26368 } while (false)
26371 /* Add a glyph string for a glyph-string sequence to the list of strings
26372 between HEAD and TAIL. */
26374 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26375 do { \
26376 int face_id; \
26377 XChar2b *char2b; \
26378 Lisp_Object gstring; \
26380 face_id = (row)->glyphs[area][START].face_id; \
26381 gstring = (composition_gstring_from_id \
26382 ((row)->glyphs[area][START].u.cmp.id)); \
26383 s = alloca (sizeof *s); \
26384 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26385 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26386 append_glyph_string (&(HEAD), &(TAIL), s); \
26387 s->x = (X); \
26388 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26389 } while (false)
26392 /* Add a glyph string for a sequence of glyphless character's glyphs
26393 to the list of strings between HEAD and TAIL. The meanings of
26394 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26396 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26397 do \
26399 int face_id; \
26401 face_id = (row)->glyphs[area][START].face_id; \
26403 s = alloca (sizeof *s); \
26404 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26405 append_glyph_string (&HEAD, &TAIL, s); \
26406 s->x = (X); \
26407 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26408 overlaps); \
26410 while (false)
26413 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26414 of AREA of glyph row ROW on window W between indices START and END.
26415 HL overrides the face for drawing glyph strings, e.g. it is
26416 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26417 x-positions of the drawing area.
26419 This is an ugly monster macro construct because we must use alloca
26420 to allocate glyph strings (because draw_glyphs can be called
26421 asynchronously). */
26423 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26424 do \
26426 HEAD = TAIL = NULL; \
26427 while (START < END) \
26429 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26430 switch (first_glyph->type) \
26432 case CHAR_GLYPH: \
26433 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26434 HL, X, LAST_X); \
26435 break; \
26437 case COMPOSITE_GLYPH: \
26438 if (first_glyph->u.cmp.automatic) \
26439 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26440 HL, X, LAST_X); \
26441 else \
26442 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26443 HL, X, LAST_X); \
26444 break; \
26446 case STRETCH_GLYPH: \
26447 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26448 HL, X, LAST_X); \
26449 break; \
26451 case IMAGE_GLYPH: \
26452 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26453 HL, X, LAST_X); \
26454 break;
26456 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26457 case XWIDGET_GLYPH: \
26458 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26459 HL, X, LAST_X); \
26460 break;
26462 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26463 case GLYPHLESS_GLYPH: \
26464 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26465 HL, X, LAST_X); \
26466 break; \
26468 default: \
26469 emacs_abort (); \
26472 if (s) \
26474 set_glyph_string_background_width (s, START, LAST_X); \
26475 (X) += s->width; \
26478 } while (false)
26481 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26482 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26483 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26484 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26487 /* Draw glyphs between START and END in AREA of ROW on window W,
26488 starting at x-position X. X is relative to AREA in W. HL is a
26489 face-override with the following meaning:
26491 DRAW_NORMAL_TEXT draw normally
26492 DRAW_CURSOR draw in cursor face
26493 DRAW_MOUSE_FACE draw in mouse face.
26494 DRAW_INVERSE_VIDEO draw in mode line face
26495 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26496 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26498 If OVERLAPS is non-zero, draw only the foreground of characters and
26499 clip to the physical height of ROW. Non-zero value also defines
26500 the overlapping part to be drawn:
26502 OVERLAPS_PRED overlap with preceding rows
26503 OVERLAPS_SUCC overlap with succeeding rows
26504 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26505 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26507 Value is the x-position reached, relative to AREA of W. */
26509 static int
26510 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26511 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26512 enum draw_glyphs_face hl, int overlaps)
26514 struct glyph_string *head, *tail;
26515 struct glyph_string *s;
26516 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26517 int i, j, x_reached, last_x, area_left = 0;
26518 struct frame *f = XFRAME (WINDOW_FRAME (w));
26519 DECLARE_HDC (hdc);
26521 ALLOCATE_HDC (hdc, f);
26523 /* Let's rather be paranoid than getting a SEGV. */
26524 end = min (end, row->used[area]);
26525 start = clip_to_bounds (0, start, end);
26527 /* Translate X to frame coordinates. Set last_x to the right
26528 end of the drawing area. */
26529 if (row->full_width_p)
26531 /* X is relative to the left edge of W, without scroll bars
26532 or fringes. */
26533 area_left = WINDOW_LEFT_EDGE_X (w);
26534 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26535 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26537 else
26539 area_left = window_box_left (w, area);
26540 last_x = area_left + window_box_width (w, area);
26542 x += area_left;
26544 /* Build a doubly-linked list of glyph_string structures between
26545 head and tail from what we have to draw. Note that the macro
26546 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26547 the reason we use a separate variable `i'. */
26548 i = start;
26549 USE_SAFE_ALLOCA;
26550 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26551 if (tail)
26553 s = glyph_string_containing_background_width (tail);
26554 x_reached = s->x + s->background_width;
26556 else
26557 x_reached = x;
26559 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26560 the row, redraw some glyphs in front or following the glyph
26561 strings built above. */
26562 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26564 struct glyph_string *h, *t;
26565 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26566 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26567 bool check_mouse_face = false;
26568 int dummy_x = 0;
26570 /* If mouse highlighting is on, we may need to draw adjacent
26571 glyphs using mouse-face highlighting. */
26572 if (area == TEXT_AREA && row->mouse_face_p
26573 && hlinfo->mouse_face_beg_row >= 0
26574 && hlinfo->mouse_face_end_row >= 0)
26576 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26578 if (row_vpos >= hlinfo->mouse_face_beg_row
26579 && row_vpos <= hlinfo->mouse_face_end_row)
26581 check_mouse_face = true;
26582 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26583 ? hlinfo->mouse_face_beg_col : 0;
26584 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26585 ? hlinfo->mouse_face_end_col
26586 : row->used[TEXT_AREA];
26590 /* Compute overhangs for all glyph strings. */
26591 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26592 for (s = head; s; s = s->next)
26593 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26595 /* Prepend glyph strings for glyphs in front of the first glyph
26596 string that are overwritten because of the first glyph
26597 string's left overhang. The background of all strings
26598 prepended must be drawn because the first glyph string
26599 draws over it. */
26600 i = left_overwritten (head);
26601 if (i >= 0)
26603 enum draw_glyphs_face overlap_hl;
26605 /* If this row contains mouse highlighting, attempt to draw
26606 the overlapped glyphs with the correct highlight. This
26607 code fails if the overlap encompasses more than one glyph
26608 and mouse-highlight spans only some of these glyphs.
26609 However, making it work perfectly involves a lot more
26610 code, and I don't know if the pathological case occurs in
26611 practice, so we'll stick to this for now. --- cyd */
26612 if (check_mouse_face
26613 && mouse_beg_col < start && mouse_end_col > i)
26614 overlap_hl = DRAW_MOUSE_FACE;
26615 else
26616 overlap_hl = DRAW_NORMAL_TEXT;
26618 if (hl != overlap_hl)
26619 clip_head = head;
26620 j = i;
26621 BUILD_GLYPH_STRINGS (j, start, h, t,
26622 overlap_hl, dummy_x, last_x);
26623 start = i;
26624 compute_overhangs_and_x (t, head->x, true);
26625 prepend_glyph_string_lists (&head, &tail, h, t);
26626 if (clip_head == NULL)
26627 clip_head = head;
26630 /* Prepend glyph strings for glyphs in front of the first glyph
26631 string that overwrite that glyph string because of their
26632 right overhang. For these strings, only the foreground must
26633 be drawn, because it draws over the glyph string at `head'.
26634 The background must not be drawn because this would overwrite
26635 right overhangs of preceding glyphs for which no glyph
26636 strings exist. */
26637 i = left_overwriting (head);
26638 if (i >= 0)
26640 enum draw_glyphs_face overlap_hl;
26642 if (check_mouse_face
26643 && mouse_beg_col < start && mouse_end_col > i)
26644 overlap_hl = DRAW_MOUSE_FACE;
26645 else
26646 overlap_hl = DRAW_NORMAL_TEXT;
26648 if (hl == overlap_hl || clip_head == NULL)
26649 clip_head = head;
26650 BUILD_GLYPH_STRINGS (i, start, h, t,
26651 overlap_hl, dummy_x, last_x);
26652 for (s = h; s; s = s->next)
26653 s->background_filled_p = true;
26654 compute_overhangs_and_x (t, head->x, true);
26655 prepend_glyph_string_lists (&head, &tail, h, t);
26658 /* Append glyphs strings for glyphs following the last glyph
26659 string tail that are overwritten by tail. The background of
26660 these strings has to be drawn because tail's foreground draws
26661 over it. */
26662 i = right_overwritten (tail);
26663 if (i >= 0)
26665 enum draw_glyphs_face overlap_hl;
26667 if (check_mouse_face
26668 && mouse_beg_col < i && mouse_end_col > end)
26669 overlap_hl = DRAW_MOUSE_FACE;
26670 else
26671 overlap_hl = DRAW_NORMAL_TEXT;
26673 if (hl != overlap_hl)
26674 clip_tail = tail;
26675 BUILD_GLYPH_STRINGS (end, i, h, t,
26676 overlap_hl, x, last_x);
26677 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26678 we don't have `end = i;' here. */
26679 compute_overhangs_and_x (h, tail->x + tail->width, false);
26680 append_glyph_string_lists (&head, &tail, h, t);
26681 if (clip_tail == NULL)
26682 clip_tail = tail;
26685 /* Append glyph strings for glyphs following the last glyph
26686 string tail that overwrite tail. The foreground of such
26687 glyphs has to be drawn because it writes into the background
26688 of tail. The background must not be drawn because it could
26689 paint over the foreground of following glyphs. */
26690 i = right_overwriting (tail);
26691 if (i >= 0)
26693 enum draw_glyphs_face overlap_hl;
26694 if (check_mouse_face
26695 && mouse_beg_col < i && mouse_end_col > end)
26696 overlap_hl = DRAW_MOUSE_FACE;
26697 else
26698 overlap_hl = DRAW_NORMAL_TEXT;
26700 if (hl == overlap_hl || clip_tail == NULL)
26701 clip_tail = tail;
26702 i++; /* We must include the Ith glyph. */
26703 BUILD_GLYPH_STRINGS (end, i, h, t,
26704 overlap_hl, x, last_x);
26705 for (s = h; s; s = s->next)
26706 s->background_filled_p = true;
26707 compute_overhangs_and_x (h, tail->x + tail->width, false);
26708 append_glyph_string_lists (&head, &tail, h, t);
26710 tail = glyph_string_containing_background_width (tail);
26711 if (clip_tail)
26712 clip_tail = glyph_string_containing_background_width (clip_tail);
26713 if (clip_head || clip_tail)
26714 for (s = head; s; s = s->next)
26716 s->clip_head = clip_head;
26717 s->clip_tail = clip_tail;
26721 /* Draw all strings. */
26722 for (s = head; s; s = s->next)
26723 FRAME_RIF (f)->draw_glyph_string (s);
26725 #ifndef HAVE_NS
26726 /* When focus a sole frame and move horizontally, this clears on_p
26727 causing a failure to erase prev cursor position. */
26728 if (area == TEXT_AREA
26729 && !row->full_width_p
26730 /* When drawing overlapping rows, only the glyph strings'
26731 foreground is drawn, which doesn't erase a cursor
26732 completely. */
26733 && !overlaps)
26735 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26736 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26737 : (tail ? tail->x + tail->background_width : x));
26738 x0 -= area_left;
26739 x1 -= area_left;
26741 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26742 row->y, MATRIX_ROW_BOTTOM_Y (row));
26744 #endif
26746 /* Value is the x-position up to which drawn, relative to AREA of W.
26747 This doesn't include parts drawn because of overhangs. */
26748 if (row->full_width_p)
26749 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26750 else
26751 x_reached -= area_left;
26753 RELEASE_HDC (hdc, f);
26755 SAFE_FREE ();
26756 return x_reached;
26759 /* Find the first glyph in the run of underlined glyphs preceding the
26760 beginning of glyph string S, and return its font (which could be
26761 NULL). This is needed because that font determines the underline
26762 position and thickness for the entire run of the underlined glyphs.
26763 This function is called from the draw_glyph_string method of GUI
26764 frame's redisplay interface (RIF) when it needs to draw in an
26765 underlined face. */
26766 struct font *
26767 font_for_underline_metrics (struct glyph_string *s)
26769 struct glyph *g0 = s->row->glyphs[s->area], *g;
26771 for (g = s->first_glyph - 1; g >= g0; g--)
26773 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26774 if (!(prev_face && prev_face->underline_p))
26775 break;
26778 /* If preceding glyphs are not underlined, use the font of S. */
26779 if (g == s->first_glyph - 1)
26780 return s->font;
26781 else
26783 /* Otherwise use the font of the last glyph we saw in the above
26784 loop whose face had the underline_p flag set. */
26785 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26789 /* Expand row matrix if too narrow. Don't expand if area
26790 is not present. */
26792 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26794 if (!it->f->fonts_changed \
26795 && (it->glyph_row->glyphs[area] \
26796 < it->glyph_row->glyphs[area + 1])) \
26798 it->w->ncols_scale_factor++; \
26799 it->f->fonts_changed = true; \
26803 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26804 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26806 static void
26807 append_glyph (struct it *it)
26809 struct glyph *glyph;
26810 enum glyph_row_area area = it->area;
26812 eassert (it->glyph_row);
26813 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26815 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26816 if (glyph < it->glyph_row->glyphs[area + 1])
26818 /* If the glyph row is reversed, we need to prepend the glyph
26819 rather than append it. */
26820 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26822 struct glyph *g;
26824 /* Make room for the additional glyph. */
26825 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26826 g[1] = *g;
26827 glyph = it->glyph_row->glyphs[area];
26829 glyph->charpos = CHARPOS (it->position);
26830 glyph->object = it->object;
26831 if (it->pixel_width > 0)
26833 eassert (it->pixel_width <= SHRT_MAX);
26834 glyph->pixel_width = it->pixel_width;
26835 glyph->padding_p = false;
26837 else
26839 /* Assure at least 1-pixel width. Otherwise, cursor can't
26840 be displayed correctly. */
26841 glyph->pixel_width = 1;
26842 glyph->padding_p = true;
26844 glyph->ascent = it->ascent;
26845 glyph->descent = it->descent;
26846 glyph->voffset = it->voffset;
26847 glyph->type = CHAR_GLYPH;
26848 glyph->avoid_cursor_p = it->avoid_cursor_p;
26849 glyph->multibyte_p = it->multibyte_p;
26850 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26852 /* In R2L rows, the left and the right box edges need to be
26853 drawn in reverse direction. */
26854 glyph->right_box_line_p = it->start_of_box_run_p;
26855 glyph->left_box_line_p = it->end_of_box_run_p;
26857 else
26859 glyph->left_box_line_p = it->start_of_box_run_p;
26860 glyph->right_box_line_p = it->end_of_box_run_p;
26862 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26863 || it->phys_descent > it->descent);
26864 glyph->glyph_not_available_p = it->glyph_not_available_p;
26865 glyph->face_id = it->face_id;
26866 glyph->u.ch = it->char_to_display;
26867 glyph->slice.img = null_glyph_slice;
26868 glyph->font_type = FONT_TYPE_UNKNOWN;
26869 if (it->bidi_p)
26871 glyph->resolved_level = it->bidi_it.resolved_level;
26872 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26873 glyph->bidi_type = it->bidi_it.type;
26875 else
26877 glyph->resolved_level = 0;
26878 glyph->bidi_type = UNKNOWN_BT;
26880 ++it->glyph_row->used[area];
26882 else
26883 IT_EXPAND_MATRIX_WIDTH (it, area);
26886 /* Store one glyph for the composition IT->cmp_it.id in
26887 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26888 non-null. */
26890 static void
26891 append_composite_glyph (struct it *it)
26893 struct glyph *glyph;
26894 enum glyph_row_area area = it->area;
26896 eassert (it->glyph_row);
26898 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26899 if (glyph < it->glyph_row->glyphs[area + 1])
26901 /* If the glyph row is reversed, we need to prepend the glyph
26902 rather than append it. */
26903 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26905 struct glyph *g;
26907 /* Make room for the new glyph. */
26908 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26909 g[1] = *g;
26910 glyph = it->glyph_row->glyphs[it->area];
26912 glyph->charpos = it->cmp_it.charpos;
26913 glyph->object = it->object;
26914 eassert (it->pixel_width <= SHRT_MAX);
26915 glyph->pixel_width = it->pixel_width;
26916 glyph->ascent = it->ascent;
26917 glyph->descent = it->descent;
26918 glyph->voffset = it->voffset;
26919 glyph->type = COMPOSITE_GLYPH;
26920 if (it->cmp_it.ch < 0)
26922 glyph->u.cmp.automatic = false;
26923 glyph->u.cmp.id = it->cmp_it.id;
26924 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26926 else
26928 glyph->u.cmp.automatic = true;
26929 glyph->u.cmp.id = it->cmp_it.id;
26930 glyph->slice.cmp.from = it->cmp_it.from;
26931 glyph->slice.cmp.to = it->cmp_it.to - 1;
26933 glyph->avoid_cursor_p = it->avoid_cursor_p;
26934 glyph->multibyte_p = it->multibyte_p;
26935 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26937 /* In R2L rows, the left and the right box edges need to be
26938 drawn in reverse direction. */
26939 glyph->right_box_line_p = it->start_of_box_run_p;
26940 glyph->left_box_line_p = it->end_of_box_run_p;
26942 else
26944 glyph->left_box_line_p = it->start_of_box_run_p;
26945 glyph->right_box_line_p = it->end_of_box_run_p;
26947 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26948 || it->phys_descent > it->descent);
26949 glyph->padding_p = false;
26950 glyph->glyph_not_available_p = false;
26951 glyph->face_id = it->face_id;
26952 glyph->font_type = FONT_TYPE_UNKNOWN;
26953 if (it->bidi_p)
26955 glyph->resolved_level = it->bidi_it.resolved_level;
26956 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26957 glyph->bidi_type = it->bidi_it.type;
26959 ++it->glyph_row->used[area];
26961 else
26962 IT_EXPAND_MATRIX_WIDTH (it, area);
26966 /* Change IT->ascent and IT->height according to the setting of
26967 IT->voffset. */
26969 static void
26970 take_vertical_position_into_account (struct it *it)
26972 if (it->voffset)
26974 if (it->voffset < 0)
26975 /* Increase the ascent so that we can display the text higher
26976 in the line. */
26977 it->ascent -= it->voffset;
26978 else
26979 /* Increase the descent so that we can display the text lower
26980 in the line. */
26981 it->descent += it->voffset;
26986 /* Produce glyphs/get display metrics for the image IT is loaded with.
26987 See the description of struct display_iterator in dispextern.h for
26988 an overview of struct display_iterator. */
26990 static void
26991 produce_image_glyph (struct it *it)
26993 struct image *img;
26994 struct face *face;
26995 int glyph_ascent, crop;
26996 struct glyph_slice slice;
26998 eassert (it->what == IT_IMAGE);
27000 face = FACE_FROM_ID (it->f, it->face_id);
27001 /* Make sure X resources of the face is loaded. */
27002 prepare_face_for_display (it->f, face);
27004 if (it->image_id < 0)
27006 /* Fringe bitmap. */
27007 it->ascent = it->phys_ascent = 0;
27008 it->descent = it->phys_descent = 0;
27009 it->pixel_width = 0;
27010 it->nglyphs = 0;
27011 return;
27014 img = IMAGE_FROM_ID (it->f, it->image_id);
27015 /* Make sure X resources of the image is loaded. */
27016 prepare_image_for_display (it->f, img);
27018 slice.x = slice.y = 0;
27019 slice.width = img->width;
27020 slice.height = img->height;
27022 if (INTEGERP (it->slice.x))
27023 slice.x = XINT (it->slice.x);
27024 else if (FLOATP (it->slice.x))
27025 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27027 if (INTEGERP (it->slice.y))
27028 slice.y = XINT (it->slice.y);
27029 else if (FLOATP (it->slice.y))
27030 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27032 if (INTEGERP (it->slice.width))
27033 slice.width = XINT (it->slice.width);
27034 else if (FLOATP (it->slice.width))
27035 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27037 if (INTEGERP (it->slice.height))
27038 slice.height = XINT (it->slice.height);
27039 else if (FLOATP (it->slice.height))
27040 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27042 if (slice.x >= img->width)
27043 slice.x = img->width;
27044 if (slice.y >= img->height)
27045 slice.y = img->height;
27046 if (slice.x + slice.width >= img->width)
27047 slice.width = img->width - slice.x;
27048 if (slice.y + slice.height > img->height)
27049 slice.height = img->height - slice.y;
27051 if (slice.width == 0 || slice.height == 0)
27052 return;
27054 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27056 it->descent = slice.height - glyph_ascent;
27057 if (slice.y == 0)
27058 it->descent += img->vmargin;
27059 if (slice.y + slice.height == img->height)
27060 it->descent += img->vmargin;
27061 it->phys_descent = it->descent;
27063 it->pixel_width = slice.width;
27064 if (slice.x == 0)
27065 it->pixel_width += img->hmargin;
27066 if (slice.x + slice.width == img->width)
27067 it->pixel_width += img->hmargin;
27069 /* It's quite possible for images to have an ascent greater than
27070 their height, so don't get confused in that case. */
27071 if (it->descent < 0)
27072 it->descent = 0;
27074 it->nglyphs = 1;
27076 if (face->box != FACE_NO_BOX)
27078 if (face->box_line_width > 0)
27080 if (slice.y == 0)
27081 it->ascent += face->box_line_width;
27082 if (slice.y + slice.height == img->height)
27083 it->descent += face->box_line_width;
27086 if (it->start_of_box_run_p && slice.x == 0)
27087 it->pixel_width += eabs (face->box_line_width);
27088 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27089 it->pixel_width += eabs (face->box_line_width);
27092 take_vertical_position_into_account (it);
27094 /* Automatically crop wide image glyphs at right edge so we can
27095 draw the cursor on same display row. */
27096 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27097 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27099 it->pixel_width -= crop;
27100 slice.width -= crop;
27103 if (it->glyph_row)
27105 struct glyph *glyph;
27106 enum glyph_row_area area = it->area;
27108 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27109 if (it->glyph_row->reversed_p)
27111 struct glyph *g;
27113 /* Make room for the new glyph. */
27114 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27115 g[1] = *g;
27116 glyph = it->glyph_row->glyphs[it->area];
27118 if (glyph < it->glyph_row->glyphs[area + 1])
27120 glyph->charpos = CHARPOS (it->position);
27121 glyph->object = it->object;
27122 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27123 glyph->ascent = glyph_ascent;
27124 glyph->descent = it->descent;
27125 glyph->voffset = it->voffset;
27126 glyph->type = IMAGE_GLYPH;
27127 glyph->avoid_cursor_p = it->avoid_cursor_p;
27128 glyph->multibyte_p = it->multibyte_p;
27129 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27131 /* In R2L rows, the left and the right box edges need to be
27132 drawn in reverse direction. */
27133 glyph->right_box_line_p = it->start_of_box_run_p;
27134 glyph->left_box_line_p = it->end_of_box_run_p;
27136 else
27138 glyph->left_box_line_p = it->start_of_box_run_p;
27139 glyph->right_box_line_p = it->end_of_box_run_p;
27141 glyph->overlaps_vertically_p = false;
27142 glyph->padding_p = false;
27143 glyph->glyph_not_available_p = false;
27144 glyph->face_id = it->face_id;
27145 glyph->u.img_id = img->id;
27146 glyph->slice.img = slice;
27147 glyph->font_type = FONT_TYPE_UNKNOWN;
27148 if (it->bidi_p)
27150 glyph->resolved_level = it->bidi_it.resolved_level;
27151 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27152 glyph->bidi_type = it->bidi_it.type;
27154 ++it->glyph_row->used[area];
27156 else
27157 IT_EXPAND_MATRIX_WIDTH (it, area);
27161 static void
27162 produce_xwidget_glyph (struct it *it)
27164 #ifdef HAVE_XWIDGETS
27165 struct xwidget *xw;
27166 int glyph_ascent, crop;
27167 eassert (it->what == IT_XWIDGET);
27169 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27170 /* Make sure X resources of the face is loaded. */
27171 prepare_face_for_display (it->f, face);
27173 xw = it->xwidget;
27174 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27175 it->descent = xw->height/2;
27176 it->phys_descent = it->descent;
27177 it->pixel_width = xw->width;
27178 /* It's quite possible for images to have an ascent greater than
27179 their height, so don't get confused in that case. */
27180 if (it->descent < 0)
27181 it->descent = 0;
27183 it->nglyphs = 1;
27185 if (face->box != FACE_NO_BOX)
27187 if (face->box_line_width > 0)
27189 it->ascent += face->box_line_width;
27190 it->descent += face->box_line_width;
27193 if (it->start_of_box_run_p)
27194 it->pixel_width += eabs (face->box_line_width);
27195 it->pixel_width += eabs (face->box_line_width);
27198 take_vertical_position_into_account (it);
27200 /* Automatically crop wide image glyphs at right edge so we can
27201 draw the cursor on same display row. */
27202 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27203 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27204 it->pixel_width -= crop;
27206 if (it->glyph_row)
27208 enum glyph_row_area area = it->area;
27209 struct glyph *glyph
27210 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27212 if (it->glyph_row->reversed_p)
27214 struct glyph *g;
27216 /* Make room for the new glyph. */
27217 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27218 g[1] = *g;
27219 glyph = it->glyph_row->glyphs[it->area];
27221 if (glyph < it->glyph_row->glyphs[area + 1])
27223 glyph->charpos = CHARPOS (it->position);
27224 glyph->object = it->object;
27225 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27226 glyph->ascent = glyph_ascent;
27227 glyph->descent = it->descent;
27228 glyph->voffset = it->voffset;
27229 glyph->type = XWIDGET_GLYPH;
27230 glyph->avoid_cursor_p = it->avoid_cursor_p;
27231 glyph->multibyte_p = it->multibyte_p;
27232 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27234 /* In R2L rows, the left and the right box edges need to be
27235 drawn in reverse direction. */
27236 glyph->right_box_line_p = it->start_of_box_run_p;
27237 glyph->left_box_line_p = it->end_of_box_run_p;
27239 else
27241 glyph->left_box_line_p = it->start_of_box_run_p;
27242 glyph->right_box_line_p = it->end_of_box_run_p;
27244 glyph->overlaps_vertically_p = 0;
27245 glyph->padding_p = 0;
27246 glyph->glyph_not_available_p = 0;
27247 glyph->face_id = it->face_id;
27248 glyph->u.xwidget = it->xwidget;
27249 glyph->font_type = FONT_TYPE_UNKNOWN;
27250 if (it->bidi_p)
27252 glyph->resolved_level = it->bidi_it.resolved_level;
27253 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27254 glyph->bidi_type = it->bidi_it.type;
27256 ++it->glyph_row->used[area];
27258 else
27259 IT_EXPAND_MATRIX_WIDTH (it, area);
27261 #endif
27264 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27265 of the glyph, WIDTH and HEIGHT are the width and height of the
27266 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27268 static void
27269 append_stretch_glyph (struct it *it, Lisp_Object object,
27270 int width, int height, int ascent)
27272 struct glyph *glyph;
27273 enum glyph_row_area area = it->area;
27275 eassert (ascent >= 0 && ascent <= height);
27277 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27278 if (glyph < it->glyph_row->glyphs[area + 1])
27280 /* If the glyph row is reversed, we need to prepend the glyph
27281 rather than append it. */
27282 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27284 struct glyph *g;
27286 /* Make room for the additional glyph. */
27287 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27288 g[1] = *g;
27289 glyph = it->glyph_row->glyphs[area];
27291 /* Decrease the width of the first glyph of the row that
27292 begins before first_visible_x (e.g., due to hscroll).
27293 This is so the overall width of the row becomes smaller
27294 by the scroll amount, and the stretch glyph appended by
27295 extend_face_to_end_of_line will be wider, to shift the
27296 row glyphs to the right. (In L2R rows, the corresponding
27297 left-shift effect is accomplished by setting row->x to a
27298 negative value, which won't work with R2L rows.)
27300 This must leave us with a positive value of WIDTH, since
27301 otherwise the call to move_it_in_display_line_to at the
27302 beginning of display_line would have got past the entire
27303 first glyph, and then it->current_x would have been
27304 greater or equal to it->first_visible_x. */
27305 if (it->current_x < it->first_visible_x)
27306 width -= it->first_visible_x - it->current_x;
27307 eassert (width > 0);
27309 glyph->charpos = CHARPOS (it->position);
27310 glyph->object = object;
27311 /* FIXME: It would be better to use TYPE_MAX here, but
27312 __typeof__ is not portable enough... */
27313 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27314 glyph->ascent = ascent;
27315 glyph->descent = height - ascent;
27316 glyph->voffset = it->voffset;
27317 glyph->type = STRETCH_GLYPH;
27318 glyph->avoid_cursor_p = it->avoid_cursor_p;
27319 glyph->multibyte_p = it->multibyte_p;
27320 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27322 /* In R2L rows, the left and the right box edges need to be
27323 drawn in reverse direction. */
27324 glyph->right_box_line_p = it->start_of_box_run_p;
27325 glyph->left_box_line_p = it->end_of_box_run_p;
27327 else
27329 glyph->left_box_line_p = it->start_of_box_run_p;
27330 glyph->right_box_line_p = it->end_of_box_run_p;
27332 glyph->overlaps_vertically_p = false;
27333 glyph->padding_p = false;
27334 glyph->glyph_not_available_p = false;
27335 glyph->face_id = it->face_id;
27336 glyph->u.stretch.ascent = ascent;
27337 glyph->u.stretch.height = height;
27338 glyph->slice.img = null_glyph_slice;
27339 glyph->font_type = FONT_TYPE_UNKNOWN;
27340 if (it->bidi_p)
27342 glyph->resolved_level = it->bidi_it.resolved_level;
27343 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27344 glyph->bidi_type = it->bidi_it.type;
27346 else
27348 glyph->resolved_level = 0;
27349 glyph->bidi_type = UNKNOWN_BT;
27351 ++it->glyph_row->used[area];
27353 else
27354 IT_EXPAND_MATRIX_WIDTH (it, area);
27357 #endif /* HAVE_WINDOW_SYSTEM */
27359 /* Produce a stretch glyph for iterator IT. IT->object is the value
27360 of the glyph property displayed. The value must be a list
27361 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27362 being recognized:
27364 1. `:width WIDTH' specifies that the space should be WIDTH *
27365 canonical char width wide. WIDTH may be an integer or floating
27366 point number.
27368 2. `:relative-width FACTOR' specifies that the width of the stretch
27369 should be computed from the width of the first character having the
27370 `glyph' property, and should be FACTOR times that width.
27372 3. `:align-to HPOS' specifies that the space should be wide enough
27373 to reach HPOS, a value in canonical character units.
27375 Exactly one of the above pairs must be present.
27377 4. `:height HEIGHT' specifies that the height of the stretch produced
27378 should be HEIGHT, measured in canonical character units.
27380 5. `:relative-height FACTOR' specifies that the height of the
27381 stretch should be FACTOR times the height of the characters having
27382 the glyph property.
27384 Either none or exactly one of 4 or 5 must be present.
27386 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27387 of the stretch should be used for the ascent of the stretch.
27388 ASCENT must be in the range 0 <= ASCENT <= 100. */
27390 void
27391 produce_stretch_glyph (struct it *it)
27393 /* (space :width WIDTH :height HEIGHT ...) */
27394 Lisp_Object prop, plist;
27395 int width = 0, height = 0, align_to = -1;
27396 bool zero_width_ok_p = false;
27397 double tem;
27398 struct font *font = NULL;
27400 #ifdef HAVE_WINDOW_SYSTEM
27401 int ascent = 0;
27402 bool zero_height_ok_p = false;
27404 if (FRAME_WINDOW_P (it->f))
27406 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27407 font = face->font ? face->font : FRAME_FONT (it->f);
27408 prepare_face_for_display (it->f, face);
27410 #endif
27412 /* List should start with `space'. */
27413 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27414 plist = XCDR (it->object);
27416 /* Compute the width of the stretch. */
27417 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27418 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27420 /* Absolute width `:width WIDTH' specified and valid. */
27421 zero_width_ok_p = true;
27422 width = (int)tem;
27424 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27426 /* Relative width `:relative-width FACTOR' specified and valid.
27427 Compute the width of the characters having the `glyph'
27428 property. */
27429 struct it it2;
27430 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27432 it2 = *it;
27433 if (it->multibyte_p)
27434 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27435 else
27437 it2.c = it2.char_to_display = *p, it2.len = 1;
27438 if (! ASCII_CHAR_P (it2.c))
27439 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27442 it2.glyph_row = NULL;
27443 it2.what = IT_CHARACTER;
27444 PRODUCE_GLYPHS (&it2);
27445 width = NUMVAL (prop) * it2.pixel_width;
27447 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27448 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27449 &align_to))
27451 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27452 align_to = (align_to < 0
27454 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27455 else if (align_to < 0)
27456 align_to = window_box_left_offset (it->w, TEXT_AREA);
27457 width = max (0, (int)tem + align_to - it->current_x);
27458 zero_width_ok_p = true;
27460 else
27461 /* Nothing specified -> width defaults to canonical char width. */
27462 width = FRAME_COLUMN_WIDTH (it->f);
27464 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27465 width = 1;
27467 #ifdef HAVE_WINDOW_SYSTEM
27468 /* Compute height. */
27469 if (FRAME_WINDOW_P (it->f))
27471 int default_height = normal_char_height (font, ' ');
27473 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27474 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27476 height = (int)tem;
27477 zero_height_ok_p = true;
27479 else if (prop = Fplist_get (plist, QCrelative_height),
27480 NUMVAL (prop) > 0)
27481 height = default_height * NUMVAL (prop);
27482 else
27483 height = default_height;
27485 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27486 height = 1;
27488 /* Compute percentage of height used for ascent. If
27489 `:ascent ASCENT' is present and valid, use that. Otherwise,
27490 derive the ascent from the font in use. */
27491 if (prop = Fplist_get (plist, QCascent),
27492 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27493 ascent = height * NUMVAL (prop) / 100.0;
27494 else if (!NILP (prop)
27495 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27496 ascent = min (max (0, (int)tem), height);
27497 else
27498 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27500 else
27501 #endif /* HAVE_WINDOW_SYSTEM */
27502 height = 1;
27504 if (width > 0 && it->line_wrap != TRUNCATE
27505 && it->current_x + width > it->last_visible_x)
27507 width = it->last_visible_x - it->current_x;
27508 #ifdef HAVE_WINDOW_SYSTEM
27509 /* Subtract one more pixel from the stretch width, but only on
27510 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27511 width -= FRAME_WINDOW_P (it->f);
27512 #endif
27515 if (width > 0 && height > 0 && it->glyph_row)
27517 Lisp_Object o_object = it->object;
27518 Lisp_Object object = it->stack[it->sp - 1].string;
27519 int n = width;
27521 if (!STRINGP (object))
27522 object = it->w->contents;
27523 #ifdef HAVE_WINDOW_SYSTEM
27524 if (FRAME_WINDOW_P (it->f))
27525 append_stretch_glyph (it, object, width, height, ascent);
27526 else
27527 #endif
27529 it->object = object;
27530 it->char_to_display = ' ';
27531 it->pixel_width = it->len = 1;
27532 while (n--)
27533 tty_append_glyph (it);
27534 it->object = o_object;
27538 it->pixel_width = width;
27539 #ifdef HAVE_WINDOW_SYSTEM
27540 if (FRAME_WINDOW_P (it->f))
27542 it->ascent = it->phys_ascent = ascent;
27543 it->descent = it->phys_descent = height - it->ascent;
27544 it->nglyphs = width > 0 && height > 0;
27545 take_vertical_position_into_account (it);
27547 else
27548 #endif
27549 it->nglyphs = width;
27552 /* Get information about special display element WHAT in an
27553 environment described by IT. WHAT is one of IT_TRUNCATION or
27554 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27555 non-null glyph_row member. This function ensures that fields like
27556 face_id, c, len of IT are left untouched. */
27558 static void
27559 produce_special_glyphs (struct it *it, enum display_element_type what)
27561 struct it temp_it;
27562 Lisp_Object gc;
27563 GLYPH glyph;
27565 temp_it = *it;
27566 temp_it.object = Qnil;
27567 memset (&temp_it.current, 0, sizeof temp_it.current);
27569 if (what == IT_CONTINUATION)
27571 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27572 if (it->bidi_it.paragraph_dir == R2L)
27573 SET_GLYPH_FROM_CHAR (glyph, '/');
27574 else
27575 SET_GLYPH_FROM_CHAR (glyph, '\\');
27576 if (it->dp
27577 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27579 /* FIXME: Should we mirror GC for R2L lines? */
27580 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27581 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27584 else if (what == IT_TRUNCATION)
27586 /* Truncation glyph. */
27587 SET_GLYPH_FROM_CHAR (glyph, '$');
27588 if (it->dp
27589 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27591 /* FIXME: Should we mirror GC for R2L lines? */
27592 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27593 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27596 else
27597 emacs_abort ();
27599 #ifdef HAVE_WINDOW_SYSTEM
27600 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27601 is turned off, we precede the truncation/continuation glyphs by a
27602 stretch glyph whose width is computed such that these special
27603 glyphs are aligned at the window margin, even when very different
27604 fonts are used in different glyph rows. */
27605 if (FRAME_WINDOW_P (temp_it.f)
27606 /* init_iterator calls this with it->glyph_row == NULL, and it
27607 wants only the pixel width of the truncation/continuation
27608 glyphs. */
27609 && temp_it.glyph_row
27610 /* insert_left_trunc_glyphs calls us at the beginning of the
27611 row, and it has its own calculation of the stretch glyph
27612 width. */
27613 && temp_it.glyph_row->used[TEXT_AREA] > 0
27614 && (temp_it.glyph_row->reversed_p
27615 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27616 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27618 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27620 if (stretch_width > 0)
27622 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27623 struct font *font =
27624 face->font ? face->font : FRAME_FONT (temp_it.f);
27625 int stretch_ascent =
27626 (((temp_it.ascent + temp_it.descent)
27627 * FONT_BASE (font)) / FONT_HEIGHT (font));
27629 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27630 temp_it.ascent + temp_it.descent,
27631 stretch_ascent);
27634 #endif
27636 temp_it.dp = NULL;
27637 temp_it.what = IT_CHARACTER;
27638 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27639 temp_it.face_id = GLYPH_FACE (glyph);
27640 temp_it.len = CHAR_BYTES (temp_it.c);
27642 PRODUCE_GLYPHS (&temp_it);
27643 it->pixel_width = temp_it.pixel_width;
27644 it->nglyphs = temp_it.nglyphs;
27647 #ifdef HAVE_WINDOW_SYSTEM
27649 /* Calculate line-height and line-spacing properties.
27650 An integer value specifies explicit pixel value.
27651 A float value specifies relative value to current face height.
27652 A cons (float . face-name) specifies relative value to
27653 height of specified face font.
27655 Returns height in pixels, or nil. */
27657 static Lisp_Object
27658 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27659 int boff, bool override)
27661 Lisp_Object face_name = Qnil;
27662 int ascent, descent, height;
27664 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27665 return val;
27667 if (CONSP (val))
27669 face_name = XCAR (val);
27670 val = XCDR (val);
27671 if (!NUMBERP (val))
27672 val = make_number (1);
27673 if (NILP (face_name))
27675 height = it->ascent + it->descent;
27676 goto scale;
27680 if (NILP (face_name))
27682 font = FRAME_FONT (it->f);
27683 boff = FRAME_BASELINE_OFFSET (it->f);
27685 else if (EQ (face_name, Qt))
27687 override = false;
27689 else
27691 int face_id;
27692 struct face *face;
27694 face_id = lookup_named_face (it->f, face_name, false);
27695 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27696 if (face == NULL || ((font = face->font) == NULL))
27697 return make_number (-1);
27698 boff = font->baseline_offset;
27699 if (font->vertical_centering)
27700 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27703 normal_char_ascent_descent (font, -1, &ascent, &descent);
27705 if (override)
27707 it->override_ascent = ascent;
27708 it->override_descent = descent;
27709 it->override_boff = boff;
27712 height = ascent + descent;
27714 scale:
27715 if (FLOATP (val))
27716 height = (int)(XFLOAT_DATA (val) * height);
27717 else if (INTEGERP (val))
27718 height *= XINT (val);
27720 return make_number (height);
27724 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27725 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27726 and only if this is for a character for which no font was found.
27728 If the display method (it->glyphless_method) is
27729 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27730 length of the acronym or the hexadecimal string, UPPER_XOFF and
27731 UPPER_YOFF are pixel offsets for the upper part of the string,
27732 LOWER_XOFF and LOWER_YOFF are for the lower part.
27734 For the other display methods, LEN through LOWER_YOFF are zero. */
27736 static void
27737 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27738 short upper_xoff, short upper_yoff,
27739 short lower_xoff, short lower_yoff)
27741 struct glyph *glyph;
27742 enum glyph_row_area area = it->area;
27744 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27745 if (glyph < it->glyph_row->glyphs[area + 1])
27747 /* If the glyph row is reversed, we need to prepend the glyph
27748 rather than append it. */
27749 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27751 struct glyph *g;
27753 /* Make room for the additional glyph. */
27754 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27755 g[1] = *g;
27756 glyph = it->glyph_row->glyphs[area];
27758 glyph->charpos = CHARPOS (it->position);
27759 glyph->object = it->object;
27760 eassert (it->pixel_width <= SHRT_MAX);
27761 glyph->pixel_width = it->pixel_width;
27762 glyph->ascent = it->ascent;
27763 glyph->descent = it->descent;
27764 glyph->voffset = it->voffset;
27765 glyph->type = GLYPHLESS_GLYPH;
27766 glyph->u.glyphless.method = it->glyphless_method;
27767 glyph->u.glyphless.for_no_font = for_no_font;
27768 glyph->u.glyphless.len = len;
27769 glyph->u.glyphless.ch = it->c;
27770 glyph->slice.glyphless.upper_xoff = upper_xoff;
27771 glyph->slice.glyphless.upper_yoff = upper_yoff;
27772 glyph->slice.glyphless.lower_xoff = lower_xoff;
27773 glyph->slice.glyphless.lower_yoff = lower_yoff;
27774 glyph->avoid_cursor_p = it->avoid_cursor_p;
27775 glyph->multibyte_p = it->multibyte_p;
27776 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27778 /* In R2L rows, the left and the right box edges need to be
27779 drawn in reverse direction. */
27780 glyph->right_box_line_p = it->start_of_box_run_p;
27781 glyph->left_box_line_p = it->end_of_box_run_p;
27783 else
27785 glyph->left_box_line_p = it->start_of_box_run_p;
27786 glyph->right_box_line_p = it->end_of_box_run_p;
27788 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27789 || it->phys_descent > it->descent);
27790 glyph->padding_p = false;
27791 glyph->glyph_not_available_p = false;
27792 glyph->face_id = face_id;
27793 glyph->font_type = FONT_TYPE_UNKNOWN;
27794 if (it->bidi_p)
27796 glyph->resolved_level = it->bidi_it.resolved_level;
27797 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27798 glyph->bidi_type = it->bidi_it.type;
27800 ++it->glyph_row->used[area];
27802 else
27803 IT_EXPAND_MATRIX_WIDTH (it, area);
27807 /* Produce a glyph for a glyphless character for iterator IT.
27808 IT->glyphless_method specifies which method to use for displaying
27809 the character. See the description of enum
27810 glyphless_display_method in dispextern.h for the detail.
27812 FOR_NO_FONT is true if and only if this is for a character for
27813 which no font was found. ACRONYM, if non-nil, is an acronym string
27814 for the character. */
27816 static void
27817 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27819 int face_id;
27820 struct face *face;
27821 struct font *font;
27822 int base_width, base_height, width, height;
27823 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27824 int len;
27826 /* Get the metrics of the base font. We always refer to the current
27827 ASCII face. */
27828 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27829 font = face->font ? face->font : FRAME_FONT (it->f);
27830 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27831 it->ascent += font->baseline_offset;
27832 it->descent -= font->baseline_offset;
27833 base_height = it->ascent + it->descent;
27834 base_width = font->average_width;
27836 face_id = merge_glyphless_glyph_face (it);
27838 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27840 it->pixel_width = THIN_SPACE_WIDTH;
27841 len = 0;
27842 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27844 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27846 width = CHARACTER_WIDTH (it->c);
27847 if (width == 0)
27848 width = 1;
27849 else if (width > 4)
27850 width = 4;
27851 it->pixel_width = base_width * width;
27852 len = 0;
27853 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27855 else
27857 char buf[7];
27858 const char *str;
27859 unsigned int code[6];
27860 int upper_len;
27861 int ascent, descent;
27862 struct font_metrics metrics_upper, metrics_lower;
27864 face = FACE_FROM_ID (it->f, face_id);
27865 font = face->font ? face->font : FRAME_FONT (it->f);
27866 prepare_face_for_display (it->f, face);
27868 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27870 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27871 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27872 if (CONSP (acronym))
27873 acronym = XCAR (acronym);
27874 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27876 else
27878 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27879 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27880 str = buf;
27882 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27883 code[len] = font->driver->encode_char (font, str[len]);
27884 upper_len = (len + 1) / 2;
27885 font->driver->text_extents (font, code, upper_len,
27886 &metrics_upper);
27887 font->driver->text_extents (font, code + upper_len, len - upper_len,
27888 &metrics_lower);
27892 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27893 width = max (metrics_upper.width, metrics_lower.width) + 4;
27894 upper_xoff = upper_yoff = 2; /* the typical case */
27895 if (base_width >= width)
27897 /* Align the upper to the left, the lower to the right. */
27898 it->pixel_width = base_width;
27899 lower_xoff = base_width - 2 - metrics_lower.width;
27901 else
27903 /* Center the shorter one. */
27904 it->pixel_width = width;
27905 if (metrics_upper.width >= metrics_lower.width)
27906 lower_xoff = (width - metrics_lower.width) / 2;
27907 else
27909 /* FIXME: This code doesn't look right. It formerly was
27910 missing the "lower_xoff = 0;", which couldn't have
27911 been right since it left lower_xoff uninitialized. */
27912 lower_xoff = 0;
27913 upper_xoff = (width - metrics_upper.width) / 2;
27917 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27918 top, bottom, and between upper and lower strings. */
27919 height = (metrics_upper.ascent + metrics_upper.descent
27920 + metrics_lower.ascent + metrics_lower.descent) + 5;
27921 /* Center vertically.
27922 H:base_height, D:base_descent
27923 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27925 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27926 descent = D - H/2 + h/2;
27927 lower_yoff = descent - 2 - ld;
27928 upper_yoff = lower_yoff - la - 1 - ud; */
27929 ascent = - (it->descent - (base_height + height + 1) / 2);
27930 descent = it->descent - (base_height - height) / 2;
27931 lower_yoff = descent - 2 - metrics_lower.descent;
27932 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27933 - metrics_upper.descent);
27934 /* Don't make the height shorter than the base height. */
27935 if (height > base_height)
27937 it->ascent = ascent;
27938 it->descent = descent;
27942 it->phys_ascent = it->ascent;
27943 it->phys_descent = it->descent;
27944 if (it->glyph_row)
27945 append_glyphless_glyph (it, face_id, for_no_font, len,
27946 upper_xoff, upper_yoff,
27947 lower_xoff, lower_yoff);
27948 it->nglyphs = 1;
27949 take_vertical_position_into_account (it);
27953 /* RIF:
27954 Produce glyphs/get display metrics for the display element IT is
27955 loaded with. See the description of struct it in dispextern.h
27956 for an overview of struct it. */
27958 void
27959 x_produce_glyphs (struct it *it)
27961 int extra_line_spacing = it->extra_line_spacing;
27963 it->glyph_not_available_p = false;
27965 if (it->what == IT_CHARACTER)
27967 XChar2b char2b;
27968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27969 struct font *font = face->font;
27970 struct font_metrics *pcm = NULL;
27971 int boff; /* Baseline offset. */
27973 if (font == NULL)
27975 /* When no suitable font is found, display this character by
27976 the method specified in the first extra slot of
27977 Vglyphless_char_display. */
27978 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27980 eassert (it->what == IT_GLYPHLESS);
27981 produce_glyphless_glyph (it, true,
27982 STRINGP (acronym) ? acronym : Qnil);
27983 goto done;
27986 boff = font->baseline_offset;
27987 if (font->vertical_centering)
27988 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27990 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27992 it->nglyphs = 1;
27994 if (it->override_ascent >= 0)
27996 it->ascent = it->override_ascent;
27997 it->descent = it->override_descent;
27998 boff = it->override_boff;
28000 else
28002 it->ascent = FONT_BASE (font) + boff;
28003 it->descent = FONT_DESCENT (font) - boff;
28006 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28008 pcm = get_per_char_metric (font, &char2b);
28009 if (pcm->width == 0
28010 && pcm->rbearing == 0 && pcm->lbearing == 0)
28011 pcm = NULL;
28014 if (pcm)
28016 it->phys_ascent = pcm->ascent + boff;
28017 it->phys_descent = pcm->descent - boff;
28018 it->pixel_width = pcm->width;
28019 /* Don't use font-global values for ascent and descent
28020 if they result in an exceedingly large line height. */
28021 if (it->override_ascent < 0)
28023 if (FONT_TOO_HIGH (font))
28025 it->ascent = it->phys_ascent;
28026 it->descent = it->phys_descent;
28027 /* These limitations are enforced by an
28028 assertion near the end of this function. */
28029 if (it->ascent < 0)
28030 it->ascent = 0;
28031 if (it->descent < 0)
28032 it->descent = 0;
28036 else
28038 it->glyph_not_available_p = true;
28039 it->phys_ascent = it->ascent;
28040 it->phys_descent = it->descent;
28041 it->pixel_width = font->space_width;
28044 if (it->constrain_row_ascent_descent_p)
28046 if (it->descent > it->max_descent)
28048 it->ascent += it->descent - it->max_descent;
28049 it->descent = it->max_descent;
28051 if (it->ascent > it->max_ascent)
28053 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28054 it->ascent = it->max_ascent;
28056 it->phys_ascent = min (it->phys_ascent, it->ascent);
28057 it->phys_descent = min (it->phys_descent, it->descent);
28058 extra_line_spacing = 0;
28061 /* If this is a space inside a region of text with
28062 `space-width' property, change its width. */
28063 bool stretched_p
28064 = it->char_to_display == ' ' && !NILP (it->space_width);
28065 if (stretched_p)
28066 it->pixel_width *= XFLOATINT (it->space_width);
28068 /* If face has a box, add the box thickness to the character
28069 height. If character has a box line to the left and/or
28070 right, add the box line width to the character's width. */
28071 if (face->box != FACE_NO_BOX)
28073 int thick = face->box_line_width;
28075 if (thick > 0)
28077 it->ascent += thick;
28078 it->descent += thick;
28080 else
28081 thick = -thick;
28083 if (it->start_of_box_run_p)
28084 it->pixel_width += thick;
28085 if (it->end_of_box_run_p)
28086 it->pixel_width += thick;
28089 /* If face has an overline, add the height of the overline
28090 (1 pixel) and a 1 pixel margin to the character height. */
28091 if (face->overline_p)
28092 it->ascent += overline_margin;
28094 if (it->constrain_row_ascent_descent_p)
28096 if (it->ascent > it->max_ascent)
28097 it->ascent = it->max_ascent;
28098 if (it->descent > it->max_descent)
28099 it->descent = it->max_descent;
28102 take_vertical_position_into_account (it);
28104 /* If we have to actually produce glyphs, do it. */
28105 if (it->glyph_row)
28107 if (stretched_p)
28109 /* Translate a space with a `space-width' property
28110 into a stretch glyph. */
28111 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28112 / FONT_HEIGHT (font));
28113 append_stretch_glyph (it, it->object, it->pixel_width,
28114 it->ascent + it->descent, ascent);
28116 else
28117 append_glyph (it);
28119 /* If characters with lbearing or rbearing are displayed
28120 in this line, record that fact in a flag of the
28121 glyph row. This is used to optimize X output code. */
28122 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28123 it->glyph_row->contains_overlapping_glyphs_p = true;
28125 if (! stretched_p && it->pixel_width == 0)
28126 /* We assure that all visible glyphs have at least 1-pixel
28127 width. */
28128 it->pixel_width = 1;
28130 else if (it->char_to_display == '\n')
28132 /* A newline has no width, but we need the height of the
28133 line. But if previous part of the line sets a height,
28134 don't increase that height. */
28136 Lisp_Object height;
28137 Lisp_Object total_height = Qnil;
28139 it->override_ascent = -1;
28140 it->pixel_width = 0;
28141 it->nglyphs = 0;
28143 height = get_it_property (it, Qline_height);
28144 /* Split (line-height total-height) list. */
28145 if (CONSP (height)
28146 && CONSP (XCDR (height))
28147 && NILP (XCDR (XCDR (height))))
28149 total_height = XCAR (XCDR (height));
28150 height = XCAR (height);
28152 height = calc_line_height_property (it, height, font, boff, true);
28154 if (it->override_ascent >= 0)
28156 it->ascent = it->override_ascent;
28157 it->descent = it->override_descent;
28158 boff = it->override_boff;
28160 else
28162 if (FONT_TOO_HIGH (font))
28164 it->ascent = font->pixel_size + boff - 1;
28165 it->descent = -boff + 1;
28166 if (it->descent < 0)
28167 it->descent = 0;
28169 else
28171 it->ascent = FONT_BASE (font) + boff;
28172 it->descent = FONT_DESCENT (font) - boff;
28176 if (EQ (height, Qt))
28178 if (it->descent > it->max_descent)
28180 it->ascent += it->descent - it->max_descent;
28181 it->descent = it->max_descent;
28183 if (it->ascent > it->max_ascent)
28185 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28186 it->ascent = it->max_ascent;
28188 it->phys_ascent = min (it->phys_ascent, it->ascent);
28189 it->phys_descent = min (it->phys_descent, it->descent);
28190 it->constrain_row_ascent_descent_p = true;
28191 extra_line_spacing = 0;
28193 else
28195 Lisp_Object spacing;
28197 it->phys_ascent = it->ascent;
28198 it->phys_descent = it->descent;
28200 if ((it->max_ascent > 0 || it->max_descent > 0)
28201 && face->box != FACE_NO_BOX
28202 && face->box_line_width > 0)
28204 it->ascent += face->box_line_width;
28205 it->descent += face->box_line_width;
28207 if (!NILP (height)
28208 && XINT (height) > it->ascent + it->descent)
28209 it->ascent = XINT (height) - it->descent;
28211 if (!NILP (total_height))
28212 spacing = calc_line_height_property (it, total_height, font,
28213 boff, false);
28214 else
28216 spacing = get_it_property (it, Qline_spacing);
28217 spacing = calc_line_height_property (it, spacing, font,
28218 boff, false);
28220 if (INTEGERP (spacing))
28222 extra_line_spacing = XINT (spacing);
28223 if (!NILP (total_height))
28224 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28228 else /* i.e. (it->char_to_display == '\t') */
28230 if (font->space_width > 0)
28232 int tab_width = it->tab_width * font->space_width;
28233 int x = it->current_x + it->continuation_lines_width;
28234 int x0 = x;
28235 /* Adjust for line numbers, if needed. */
28236 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28237 x -= it->lnum_pixel_width;
28238 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28240 /* If the distance from the current position to the next tab
28241 stop is less than a space character width, use the
28242 tab stop after that. */
28243 if (next_tab_x - x < font->space_width)
28244 next_tab_x += tab_width;
28245 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28246 next_tab_x += (it->lnum_pixel_width
28247 - ((it->w->hscroll * font->space_width)
28248 % tab_width));
28250 it->pixel_width = next_tab_x - x0;
28251 it->nglyphs = 1;
28252 if (FONT_TOO_HIGH (font))
28254 if (get_char_glyph_code (' ', font, &char2b))
28256 pcm = get_per_char_metric (font, &char2b);
28257 if (pcm->width == 0
28258 && pcm->rbearing == 0 && pcm->lbearing == 0)
28259 pcm = NULL;
28262 if (pcm)
28264 it->ascent = pcm->ascent + boff;
28265 it->descent = pcm->descent - boff;
28267 else
28269 it->ascent = font->pixel_size + boff - 1;
28270 it->descent = -boff + 1;
28272 if (it->ascent < 0)
28273 it->ascent = 0;
28274 if (it->descent < 0)
28275 it->descent = 0;
28277 else
28279 it->ascent = FONT_BASE (font) + boff;
28280 it->descent = FONT_DESCENT (font) - boff;
28282 it->phys_ascent = it->ascent;
28283 it->phys_descent = it->descent;
28285 if (it->glyph_row)
28287 append_stretch_glyph (it, it->object, it->pixel_width,
28288 it->ascent + it->descent, it->ascent);
28291 else
28293 it->pixel_width = 0;
28294 it->nglyphs = 1;
28298 if (FONT_TOO_HIGH (font))
28300 int font_ascent, font_descent;
28302 /* For very large fonts, where we ignore the declared font
28303 dimensions, and go by per-character metrics instead,
28304 don't let the row ascent and descent values (and the row
28305 height computed from them) be smaller than the "normal"
28306 character metrics. This avoids unpleasant effects
28307 whereby lines on display would change their height
28308 depending on which characters are shown. */
28309 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28310 it->max_ascent = max (it->max_ascent, font_ascent);
28311 it->max_descent = max (it->max_descent, font_descent);
28314 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28316 /* A static composition.
28318 Note: A composition is represented as one glyph in the
28319 glyph matrix. There are no padding glyphs.
28321 Important note: pixel_width, ascent, and descent are the
28322 values of what is drawn by draw_glyphs (i.e. the values of
28323 the overall glyphs composed). */
28324 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28325 int boff; /* baseline offset */
28326 struct composition *cmp = composition_table[it->cmp_it.id];
28327 int glyph_len = cmp->glyph_len;
28328 struct font *font = face->font;
28330 it->nglyphs = 1;
28332 /* If we have not yet calculated pixel size data of glyphs of
28333 the composition for the current face font, calculate them
28334 now. Theoretically, we have to check all fonts for the
28335 glyphs, but that requires much time and memory space. So,
28336 here we check only the font of the first glyph. This may
28337 lead to incorrect display, but it's very rare, and C-l
28338 (recenter-top-bottom) can correct the display anyway. */
28339 if (! cmp->font || cmp->font != font)
28341 /* Ascent and descent of the font of the first character
28342 of this composition (adjusted by baseline offset).
28343 Ascent and descent of overall glyphs should not be less
28344 than these, respectively. */
28345 int font_ascent, font_descent, font_height;
28346 /* Bounding box of the overall glyphs. */
28347 int leftmost, rightmost, lowest, highest;
28348 int lbearing, rbearing;
28349 int i, width, ascent, descent;
28350 int c;
28351 XChar2b char2b;
28352 struct font_metrics *pcm;
28353 ptrdiff_t pos;
28355 eassume (0 < glyph_len); /* See Bug#8512. */
28357 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28358 while (c == '\t' && 0 < --glyph_len);
28360 bool right_padded = glyph_len < cmp->glyph_len;
28361 for (i = 0; i < glyph_len; i++)
28363 c = COMPOSITION_GLYPH (cmp, i);
28364 if (c != '\t')
28365 break;
28366 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28368 bool left_padded = i > 0;
28370 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28371 : IT_CHARPOS (*it));
28372 /* If no suitable font is found, use the default font. */
28373 bool font_not_found_p = font == NULL;
28374 if (font_not_found_p)
28376 face = face->ascii_face;
28377 font = face->font;
28379 boff = font->baseline_offset;
28380 if (font->vertical_centering)
28381 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28382 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28383 font_ascent += boff;
28384 font_descent -= boff;
28385 font_height = font_ascent + font_descent;
28387 cmp->font = font;
28389 pcm = NULL;
28390 if (! font_not_found_p)
28392 get_char_face_and_encoding (it->f, c, it->face_id,
28393 &char2b, false);
28394 pcm = get_per_char_metric (font, &char2b);
28397 /* Initialize the bounding box. */
28398 if (pcm)
28400 width = cmp->glyph_len > 0 ? pcm->width : 0;
28401 ascent = pcm->ascent;
28402 descent = pcm->descent;
28403 lbearing = pcm->lbearing;
28404 rbearing = pcm->rbearing;
28406 else
28408 width = cmp->glyph_len > 0 ? font->space_width : 0;
28409 ascent = FONT_BASE (font);
28410 descent = FONT_DESCENT (font);
28411 lbearing = 0;
28412 rbearing = width;
28415 rightmost = width;
28416 leftmost = 0;
28417 lowest = - descent + boff;
28418 highest = ascent + boff;
28420 if (! font_not_found_p
28421 && font->default_ascent
28422 && CHAR_TABLE_P (Vuse_default_ascent)
28423 && !NILP (Faref (Vuse_default_ascent,
28424 make_number (it->char_to_display))))
28425 highest = font->default_ascent + boff;
28427 /* Draw the first glyph at the normal position. It may be
28428 shifted to right later if some other glyphs are drawn
28429 at the left. */
28430 cmp->offsets[i * 2] = 0;
28431 cmp->offsets[i * 2 + 1] = boff;
28432 cmp->lbearing = lbearing;
28433 cmp->rbearing = rbearing;
28435 /* Set cmp->offsets for the remaining glyphs. */
28436 for (i++; i < glyph_len; i++)
28438 int left, right, btm, top;
28439 int ch = COMPOSITION_GLYPH (cmp, i);
28440 int face_id;
28441 struct face *this_face;
28443 if (ch == '\t')
28444 ch = ' ';
28445 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28446 this_face = FACE_FROM_ID (it->f, face_id);
28447 font = this_face->font;
28449 if (font == NULL)
28450 pcm = NULL;
28451 else
28453 get_char_face_and_encoding (it->f, ch, face_id,
28454 &char2b, false);
28455 pcm = get_per_char_metric (font, &char2b);
28457 if (! pcm)
28458 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28459 else
28461 width = pcm->width;
28462 ascent = pcm->ascent;
28463 descent = pcm->descent;
28464 lbearing = pcm->lbearing;
28465 rbearing = pcm->rbearing;
28466 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28468 /* Relative composition with or without
28469 alternate chars. */
28470 left = (leftmost + rightmost - width) / 2;
28471 btm = - descent + boff;
28472 if (font->relative_compose
28473 && (! CHAR_TABLE_P (Vignore_relative_composition)
28474 || NILP (Faref (Vignore_relative_composition,
28475 make_number (ch)))))
28478 if (- descent >= font->relative_compose)
28479 /* One extra pixel between two glyphs. */
28480 btm = highest + 1;
28481 else if (ascent <= 0)
28482 /* One extra pixel between two glyphs. */
28483 btm = lowest - 1 - ascent - descent;
28486 else
28488 /* A composition rule is specified by an integer
28489 value that encodes global and new reference
28490 points (GREF and NREF). GREF and NREF are
28491 specified by numbers as below:
28493 0---1---2 -- ascent
28497 9--10--11 -- center
28499 ---3---4---5--- baseline
28501 6---7---8 -- descent
28503 int rule = COMPOSITION_RULE (cmp, i);
28504 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28506 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28507 grefx = gref % 3, nrefx = nref % 3;
28508 grefy = gref / 3, nrefy = nref / 3;
28509 if (xoff)
28510 xoff = font_height * (xoff - 128) / 256;
28511 if (yoff)
28512 yoff = font_height * (yoff - 128) / 256;
28514 left = (leftmost
28515 + grefx * (rightmost - leftmost) / 2
28516 - nrefx * width / 2
28517 + xoff);
28519 btm = ((grefy == 0 ? highest
28520 : grefy == 1 ? 0
28521 : grefy == 2 ? lowest
28522 : (highest + lowest) / 2)
28523 - (nrefy == 0 ? ascent + descent
28524 : nrefy == 1 ? descent - boff
28525 : nrefy == 2 ? 0
28526 : (ascent + descent) / 2)
28527 + yoff);
28530 cmp->offsets[i * 2] = left;
28531 cmp->offsets[i * 2 + 1] = btm + descent;
28533 /* Update the bounding box of the overall glyphs. */
28534 if (width > 0)
28536 right = left + width;
28537 if (left < leftmost)
28538 leftmost = left;
28539 if (right > rightmost)
28540 rightmost = right;
28542 top = btm + descent + ascent;
28543 if (top > highest)
28544 highest = top;
28545 if (btm < lowest)
28546 lowest = btm;
28548 if (cmp->lbearing > left + lbearing)
28549 cmp->lbearing = left + lbearing;
28550 if (cmp->rbearing < left + rbearing)
28551 cmp->rbearing = left + rbearing;
28555 /* If there are glyphs whose x-offsets are negative,
28556 shift all glyphs to the right and make all x-offsets
28557 non-negative. */
28558 if (leftmost < 0)
28560 for (i = 0; i < cmp->glyph_len; i++)
28561 cmp->offsets[i * 2] -= leftmost;
28562 rightmost -= leftmost;
28563 cmp->lbearing -= leftmost;
28564 cmp->rbearing -= leftmost;
28567 if (left_padded && cmp->lbearing < 0)
28569 for (i = 0; i < cmp->glyph_len; i++)
28570 cmp->offsets[i * 2] -= cmp->lbearing;
28571 rightmost -= cmp->lbearing;
28572 cmp->rbearing -= cmp->lbearing;
28573 cmp->lbearing = 0;
28575 if (right_padded && rightmost < cmp->rbearing)
28577 rightmost = cmp->rbearing;
28580 cmp->pixel_width = rightmost;
28581 cmp->ascent = highest;
28582 cmp->descent = - lowest;
28583 if (cmp->ascent < font_ascent)
28584 cmp->ascent = font_ascent;
28585 if (cmp->descent < font_descent)
28586 cmp->descent = font_descent;
28589 if (it->glyph_row
28590 && (cmp->lbearing < 0
28591 || cmp->rbearing > cmp->pixel_width))
28592 it->glyph_row->contains_overlapping_glyphs_p = true;
28594 it->pixel_width = cmp->pixel_width;
28595 it->ascent = it->phys_ascent = cmp->ascent;
28596 it->descent = it->phys_descent = cmp->descent;
28597 if (face->box != FACE_NO_BOX)
28599 int thick = face->box_line_width;
28601 if (thick > 0)
28603 it->ascent += thick;
28604 it->descent += thick;
28606 else
28607 thick = - thick;
28609 if (it->start_of_box_run_p)
28610 it->pixel_width += thick;
28611 if (it->end_of_box_run_p)
28612 it->pixel_width += thick;
28615 /* If face has an overline, add the height of the overline
28616 (1 pixel) and a 1 pixel margin to the character height. */
28617 if (face->overline_p)
28618 it->ascent += overline_margin;
28620 take_vertical_position_into_account (it);
28621 if (it->ascent < 0)
28622 it->ascent = 0;
28623 if (it->descent < 0)
28624 it->descent = 0;
28626 if (it->glyph_row && cmp->glyph_len > 0)
28627 append_composite_glyph (it);
28629 else if (it->what == IT_COMPOSITION)
28631 /* A dynamic (automatic) composition. */
28632 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28633 Lisp_Object gstring;
28634 struct font_metrics metrics;
28636 it->nglyphs = 1;
28638 gstring = composition_gstring_from_id (it->cmp_it.id);
28639 it->pixel_width
28640 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28641 &metrics);
28642 if (it->glyph_row
28643 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28644 it->glyph_row->contains_overlapping_glyphs_p = true;
28645 it->ascent = it->phys_ascent = metrics.ascent;
28646 it->descent = it->phys_descent = metrics.descent;
28647 if (face->box != FACE_NO_BOX)
28649 int thick = face->box_line_width;
28651 if (thick > 0)
28653 it->ascent += thick;
28654 it->descent += thick;
28656 else
28657 thick = - thick;
28659 if (it->start_of_box_run_p)
28660 it->pixel_width += thick;
28661 if (it->end_of_box_run_p)
28662 it->pixel_width += thick;
28664 /* If face has an overline, add the height of the overline
28665 (1 pixel) and a 1 pixel margin to the character height. */
28666 if (face->overline_p)
28667 it->ascent += overline_margin;
28668 take_vertical_position_into_account (it);
28669 if (it->ascent < 0)
28670 it->ascent = 0;
28671 if (it->descent < 0)
28672 it->descent = 0;
28674 if (it->glyph_row)
28675 append_composite_glyph (it);
28677 else if (it->what == IT_GLYPHLESS)
28678 produce_glyphless_glyph (it, false, Qnil);
28679 else if (it->what == IT_IMAGE)
28680 produce_image_glyph (it);
28681 else if (it->what == IT_STRETCH)
28682 produce_stretch_glyph (it);
28683 else if (it->what == IT_XWIDGET)
28684 produce_xwidget_glyph (it);
28686 done:
28687 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28688 because this isn't true for images with `:ascent 100'. */
28689 eassert (it->ascent >= 0 && it->descent >= 0);
28690 if (it->area == TEXT_AREA)
28691 it->current_x += it->pixel_width;
28693 if (extra_line_spacing > 0)
28695 it->descent += extra_line_spacing;
28696 if (extra_line_spacing > it->max_extra_line_spacing)
28697 it->max_extra_line_spacing = extra_line_spacing;
28700 it->max_ascent = max (it->max_ascent, it->ascent);
28701 it->max_descent = max (it->max_descent, it->descent);
28702 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28703 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28706 /* EXPORT for RIF:
28707 Output LEN glyphs starting at START at the nominal cursor position.
28708 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28709 being updated, and UPDATED_AREA is the area of that row being updated. */
28711 void
28712 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28713 struct glyph *start, enum glyph_row_area updated_area, int len)
28715 int x, hpos, chpos = w->phys_cursor.hpos;
28717 eassert (updated_row);
28718 /* When the window is hscrolled, cursor hpos can legitimately be out
28719 of bounds, but we draw the cursor at the corresponding window
28720 margin in that case. */
28721 if (!updated_row->reversed_p && chpos < 0)
28722 chpos = 0;
28723 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28724 chpos = updated_row->used[TEXT_AREA] - 1;
28726 block_input ();
28728 /* Write glyphs. */
28730 hpos = start - updated_row->glyphs[updated_area];
28731 x = draw_glyphs (w, w->output_cursor.x,
28732 updated_row, updated_area,
28733 hpos, hpos + len,
28734 DRAW_NORMAL_TEXT, 0);
28736 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28737 if (updated_area == TEXT_AREA
28738 && w->phys_cursor_on_p
28739 && w->phys_cursor.vpos == w->output_cursor.vpos
28740 && chpos >= hpos
28741 && chpos < hpos + len)
28742 w->phys_cursor_on_p = false;
28744 unblock_input ();
28746 /* Advance the output cursor. */
28747 w->output_cursor.hpos += len;
28748 w->output_cursor.x = x;
28752 /* EXPORT for RIF:
28753 Insert LEN glyphs from START at the nominal cursor position. */
28755 void
28756 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28757 struct glyph *start, enum glyph_row_area updated_area, int len)
28759 struct frame *f;
28760 int line_height, shift_by_width, shifted_region_width;
28761 struct glyph_row *row;
28762 struct glyph *glyph;
28763 int frame_x, frame_y;
28764 ptrdiff_t hpos;
28766 eassert (updated_row);
28767 block_input ();
28768 f = XFRAME (WINDOW_FRAME (w));
28770 /* Get the height of the line we are in. */
28771 row = updated_row;
28772 line_height = row->height;
28774 /* Get the width of the glyphs to insert. */
28775 shift_by_width = 0;
28776 for (glyph = start; glyph < start + len; ++glyph)
28777 shift_by_width += glyph->pixel_width;
28779 /* Get the width of the region to shift right. */
28780 shifted_region_width = (window_box_width (w, updated_area)
28781 - w->output_cursor.x
28782 - shift_by_width);
28784 /* Shift right. */
28785 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28786 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28788 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28789 line_height, shift_by_width);
28791 /* Write the glyphs. */
28792 hpos = start - row->glyphs[updated_area];
28793 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28794 hpos, hpos + len,
28795 DRAW_NORMAL_TEXT, 0);
28797 /* Advance the output cursor. */
28798 w->output_cursor.hpos += len;
28799 w->output_cursor.x += shift_by_width;
28800 unblock_input ();
28804 /* EXPORT for RIF:
28805 Erase the current text line from the nominal cursor position
28806 (inclusive) to pixel column TO_X (exclusive). The idea is that
28807 everything from TO_X onward is already erased.
28809 TO_X is a pixel position relative to UPDATED_AREA of currently
28810 updated window W. TO_X == -1 means clear to the end of this area. */
28812 void
28813 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28814 enum glyph_row_area updated_area, int to_x)
28816 struct frame *f;
28817 int max_x, min_y, max_y;
28818 int from_x, from_y, to_y;
28820 eassert (updated_row);
28821 f = XFRAME (w->frame);
28823 if (updated_row->full_width_p)
28824 max_x = (WINDOW_PIXEL_WIDTH (w)
28825 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28826 else
28827 max_x = window_box_width (w, updated_area);
28828 max_y = window_text_bottom_y (w);
28830 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28831 of window. For TO_X > 0, truncate to end of drawing area. */
28832 if (to_x == 0)
28833 return;
28834 else if (to_x < 0)
28835 to_x = max_x;
28836 else
28837 to_x = min (to_x, max_x);
28839 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28841 /* Notice if the cursor will be cleared by this operation. */
28842 if (!updated_row->full_width_p)
28843 notice_overwritten_cursor (w, updated_area,
28844 w->output_cursor.x, -1,
28845 updated_row->y,
28846 MATRIX_ROW_BOTTOM_Y (updated_row));
28848 from_x = w->output_cursor.x;
28850 /* Translate to frame coordinates. */
28851 if (updated_row->full_width_p)
28853 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28854 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28856 else
28858 int area_left = window_box_left (w, updated_area);
28859 from_x += area_left;
28860 to_x += area_left;
28863 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28864 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28865 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28867 /* Prevent inadvertently clearing to end of the X window. */
28868 if (to_x > from_x && to_y > from_y)
28870 block_input ();
28871 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28872 to_x - from_x, to_y - from_y);
28873 unblock_input ();
28877 #endif /* HAVE_WINDOW_SYSTEM */
28881 /***********************************************************************
28882 Cursor types
28883 ***********************************************************************/
28885 /* Value is the internal representation of the specified cursor type
28886 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28887 of the bar cursor. */
28889 static enum text_cursor_kinds
28890 get_specified_cursor_type (Lisp_Object arg, int *width)
28892 enum text_cursor_kinds type;
28894 if (NILP (arg))
28895 return NO_CURSOR;
28897 if (EQ (arg, Qbox))
28898 return FILLED_BOX_CURSOR;
28900 if (EQ (arg, Qhollow))
28901 return HOLLOW_BOX_CURSOR;
28903 if (EQ (arg, Qbar))
28905 *width = 2;
28906 return BAR_CURSOR;
28909 if (CONSP (arg)
28910 && EQ (XCAR (arg), Qbar)
28911 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28913 *width = XINT (XCDR (arg));
28914 return BAR_CURSOR;
28917 if (EQ (arg, Qhbar))
28919 *width = 2;
28920 return HBAR_CURSOR;
28923 if (CONSP (arg)
28924 && EQ (XCAR (arg), Qhbar)
28925 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28927 *width = XINT (XCDR (arg));
28928 return HBAR_CURSOR;
28931 /* Treat anything unknown as "hollow box cursor".
28932 It was bad to signal an error; people have trouble fixing
28933 .Xdefaults with Emacs, when it has something bad in it. */
28934 type = HOLLOW_BOX_CURSOR;
28936 return type;
28939 /* Set the default cursor types for specified frame. */
28940 void
28941 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28943 int width = 1;
28944 Lisp_Object tem;
28946 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28947 FRAME_CURSOR_WIDTH (f) = width;
28949 /* By default, set up the blink-off state depending on the on-state. */
28951 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28952 if (!NILP (tem))
28954 FRAME_BLINK_OFF_CURSOR (f)
28955 = get_specified_cursor_type (XCDR (tem), &width);
28956 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28958 else
28959 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28961 /* Make sure the cursor gets redrawn. */
28962 f->cursor_type_changed = true;
28966 #ifdef HAVE_WINDOW_SYSTEM
28968 /* Return the cursor we want to be displayed in window W. Return
28969 width of bar/hbar cursor through WIDTH arg. Return with
28970 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28971 (i.e. if the `system caret' should track this cursor).
28973 In a mini-buffer window, we want the cursor only to appear if we
28974 are reading input from this window. For the selected window, we
28975 want the cursor type given by the frame parameter or buffer local
28976 setting of cursor-type. If explicitly marked off, draw no cursor.
28977 In all other cases, we want a hollow box cursor. */
28979 static enum text_cursor_kinds
28980 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28981 bool *active_cursor)
28983 struct frame *f = XFRAME (w->frame);
28984 struct buffer *b = XBUFFER (w->contents);
28985 int cursor_type = DEFAULT_CURSOR;
28986 Lisp_Object alt_cursor;
28987 bool non_selected = false;
28989 *active_cursor = true;
28991 /* Echo area */
28992 if (cursor_in_echo_area
28993 && FRAME_HAS_MINIBUF_P (f)
28994 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28996 if (w == XWINDOW (echo_area_window))
28998 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
29000 *width = FRAME_CURSOR_WIDTH (f);
29001 return FRAME_DESIRED_CURSOR (f);
29003 else
29004 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29007 *active_cursor = false;
29008 non_selected = true;
29011 /* Detect a nonselected window or nonselected frame. */
29012 else if (w != XWINDOW (f->selected_window)
29013 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29015 *active_cursor = false;
29017 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29018 return NO_CURSOR;
29020 non_selected = true;
29023 /* Never display a cursor in a window in which cursor-type is nil. */
29024 if (NILP (BVAR (b, cursor_type)))
29025 return NO_CURSOR;
29027 /* Get the normal cursor type for this window. */
29028 if (EQ (BVAR (b, cursor_type), Qt))
29030 cursor_type = FRAME_DESIRED_CURSOR (f);
29031 *width = FRAME_CURSOR_WIDTH (f);
29033 else
29034 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29036 /* Use cursor-in-non-selected-windows instead
29037 for non-selected window or frame. */
29038 if (non_selected)
29040 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29041 if (!EQ (Qt, alt_cursor))
29042 return get_specified_cursor_type (alt_cursor, width);
29043 /* t means modify the normal cursor type. */
29044 if (cursor_type == FILLED_BOX_CURSOR)
29045 cursor_type = HOLLOW_BOX_CURSOR;
29046 else if (cursor_type == BAR_CURSOR && *width > 1)
29047 --*width;
29048 return cursor_type;
29051 /* Use normal cursor if not blinked off. */
29052 if (!w->cursor_off_p)
29054 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29055 return NO_CURSOR;
29056 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29058 if (cursor_type == FILLED_BOX_CURSOR)
29060 /* Using a block cursor on large images can be very annoying.
29061 So use a hollow cursor for "large" images.
29062 If image is not transparent (no mask), also use hollow cursor. */
29063 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29064 if (img != NULL && IMAGEP (img->spec))
29066 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29067 where N = size of default frame font size.
29068 This should cover most of the "tiny" icons people may use. */
29069 if (!img->mask
29070 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29071 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29072 cursor_type = HOLLOW_BOX_CURSOR;
29075 else if (cursor_type != NO_CURSOR)
29077 /* Display current only supports BOX and HOLLOW cursors for images.
29078 So for now, unconditionally use a HOLLOW cursor when cursor is
29079 not a solid box cursor. */
29080 cursor_type = HOLLOW_BOX_CURSOR;
29083 return cursor_type;
29086 /* Cursor is blinked off, so determine how to "toggle" it. */
29088 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29089 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29090 return get_specified_cursor_type (XCDR (alt_cursor), width);
29092 /* Then see if frame has specified a specific blink off cursor type. */
29093 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29095 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29096 return FRAME_BLINK_OFF_CURSOR (f);
29099 #if false
29100 /* Some people liked having a permanently visible blinking cursor,
29101 while others had very strong opinions against it. So it was
29102 decided to remove it. KFS 2003-09-03 */
29104 /* Finally perform built-in cursor blinking:
29105 filled box <-> hollow box
29106 wide [h]bar <-> narrow [h]bar
29107 narrow [h]bar <-> no cursor
29108 other type <-> no cursor */
29110 if (cursor_type == FILLED_BOX_CURSOR)
29111 return HOLLOW_BOX_CURSOR;
29113 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29115 *width = 1;
29116 return cursor_type;
29118 #endif
29120 return NO_CURSOR;
29124 /* Notice when the text cursor of window W has been completely
29125 overwritten by a drawing operation that outputs glyphs in AREA
29126 starting at X0 and ending at X1 in the line starting at Y0 and
29127 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29128 the rest of the line after X0 has been written. Y coordinates
29129 are window-relative. */
29131 static void
29132 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29133 int x0, int x1, int y0, int y1)
29135 int cx0, cx1, cy0, cy1;
29136 struct glyph_row *row;
29138 if (!w->phys_cursor_on_p)
29139 return;
29140 if (area != TEXT_AREA)
29141 return;
29143 if (w->phys_cursor.vpos < 0
29144 || w->phys_cursor.vpos >= w->current_matrix->nrows
29145 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29146 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29147 return;
29149 if (row->cursor_in_fringe_p)
29151 row->cursor_in_fringe_p = false;
29152 draw_fringe_bitmap (w, row, row->reversed_p);
29153 w->phys_cursor_on_p = false;
29154 return;
29157 cx0 = w->phys_cursor.x;
29158 cx1 = cx0 + w->phys_cursor_width;
29159 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29160 return;
29162 /* The cursor image will be completely removed from the
29163 screen if the output area intersects the cursor area in
29164 y-direction. When we draw in [y0 y1[, and some part of
29165 the cursor is at y < y0, that part must have been drawn
29166 before. When scrolling, the cursor is erased before
29167 actually scrolling, so we don't come here. When not
29168 scrolling, the rows above the old cursor row must have
29169 changed, and in this case these rows must have written
29170 over the cursor image.
29172 Likewise if part of the cursor is below y1, with the
29173 exception of the cursor being in the first blank row at
29174 the buffer and window end because update_text_area
29175 doesn't draw that row. (Except when it does, but
29176 that's handled in update_text_area.) */
29178 cy0 = w->phys_cursor.y;
29179 cy1 = cy0 + w->phys_cursor_height;
29180 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29181 return;
29183 w->phys_cursor_on_p = false;
29186 #endif /* HAVE_WINDOW_SYSTEM */
29189 /************************************************************************
29190 Mouse Face
29191 ************************************************************************/
29193 #ifdef HAVE_WINDOW_SYSTEM
29195 /* EXPORT for RIF:
29196 Fix the display of area AREA of overlapping row ROW in window W
29197 with respect to the overlapping part OVERLAPS. */
29199 void
29200 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29201 enum glyph_row_area area, int overlaps)
29203 int i, x;
29205 block_input ();
29207 x = 0;
29208 for (i = 0; i < row->used[area];)
29210 if (row->glyphs[area][i].overlaps_vertically_p)
29212 int start = i, start_x = x;
29216 x += row->glyphs[area][i].pixel_width;
29217 ++i;
29219 while (i < row->used[area]
29220 && row->glyphs[area][i].overlaps_vertically_p);
29222 draw_glyphs (w, start_x, row, area,
29223 start, i,
29224 DRAW_NORMAL_TEXT, overlaps);
29226 else
29228 x += row->glyphs[area][i].pixel_width;
29229 ++i;
29233 unblock_input ();
29237 /* EXPORT:
29238 Draw the cursor glyph of window W in glyph row ROW. See the
29239 comment of draw_glyphs for the meaning of HL. */
29241 void
29242 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29243 enum draw_glyphs_face hl)
29245 /* If cursor hpos is out of bounds, don't draw garbage. This can
29246 happen in mini-buffer windows when switching between echo area
29247 glyphs and mini-buffer. */
29248 if ((row->reversed_p
29249 ? (w->phys_cursor.hpos >= 0)
29250 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29252 bool on_p = w->phys_cursor_on_p;
29253 int x1;
29254 int hpos = w->phys_cursor.hpos;
29256 /* When the window is hscrolled, cursor hpos can legitimately be
29257 out of bounds, but we draw the cursor at the corresponding
29258 window margin in that case. */
29259 if (!row->reversed_p && hpos < 0)
29260 hpos = 0;
29261 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29262 hpos = row->used[TEXT_AREA] - 1;
29264 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29265 hl, 0);
29266 w->phys_cursor_on_p = on_p;
29268 if (hl == DRAW_CURSOR)
29269 w->phys_cursor_width = x1 - w->phys_cursor.x;
29270 /* When we erase the cursor, and ROW is overlapped by other
29271 rows, make sure that these overlapping parts of other rows
29272 are redrawn. */
29273 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29275 w->phys_cursor_width = x1 - w->phys_cursor.x;
29277 if (row > w->current_matrix->rows
29278 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29279 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29280 OVERLAPS_ERASED_CURSOR);
29282 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29283 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29284 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29285 OVERLAPS_ERASED_CURSOR);
29291 /* Erase the image of a cursor of window W from the screen. */
29293 void
29294 erase_phys_cursor (struct window *w)
29296 struct frame *f = XFRAME (w->frame);
29297 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29298 int hpos = w->phys_cursor.hpos;
29299 int vpos = w->phys_cursor.vpos;
29300 bool mouse_face_here_p = false;
29301 struct glyph_matrix *active_glyphs = w->current_matrix;
29302 struct glyph_row *cursor_row;
29303 struct glyph *cursor_glyph;
29304 enum draw_glyphs_face hl;
29306 /* No cursor displayed or row invalidated => nothing to do on the
29307 screen. */
29308 if (w->phys_cursor_type == NO_CURSOR)
29309 goto mark_cursor_off;
29311 /* VPOS >= active_glyphs->nrows means that window has been resized.
29312 Don't bother to erase the cursor. */
29313 if (vpos >= active_glyphs->nrows)
29314 goto mark_cursor_off;
29316 /* If row containing cursor is marked invalid, there is nothing we
29317 can do. */
29318 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29319 if (!cursor_row->enabled_p)
29320 goto mark_cursor_off;
29322 /* If line spacing is > 0, old cursor may only be partially visible in
29323 window after split-window. So adjust visible height. */
29324 cursor_row->visible_height = min (cursor_row->visible_height,
29325 window_text_bottom_y (w) - cursor_row->y);
29327 /* If row is completely invisible, don't attempt to delete a cursor which
29328 isn't there. This can happen if cursor is at top of a window, and
29329 we switch to a buffer with a header line in that window. */
29330 if (cursor_row->visible_height <= 0)
29331 goto mark_cursor_off;
29333 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29334 if (cursor_row->cursor_in_fringe_p)
29336 cursor_row->cursor_in_fringe_p = false;
29337 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29338 goto mark_cursor_off;
29341 /* This can happen when the new row is shorter than the old one.
29342 In this case, either draw_glyphs or clear_end_of_line
29343 should have cleared the cursor. Note that we wouldn't be
29344 able to erase the cursor in this case because we don't have a
29345 cursor glyph at hand. */
29346 if ((cursor_row->reversed_p
29347 ? (w->phys_cursor.hpos < 0)
29348 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29349 goto mark_cursor_off;
29351 /* When the window is hscrolled, cursor hpos can legitimately be out
29352 of bounds, but we draw the cursor at the corresponding window
29353 margin in that case. */
29354 if (!cursor_row->reversed_p && hpos < 0)
29355 hpos = 0;
29356 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29357 hpos = cursor_row->used[TEXT_AREA] - 1;
29359 /* If the cursor is in the mouse face area, redisplay that when
29360 we clear the cursor. */
29361 if (! NILP (hlinfo->mouse_face_window)
29362 && coords_in_mouse_face_p (w, hpos, vpos)
29363 /* Don't redraw the cursor's spot in mouse face if it is at the
29364 end of a line (on a newline). The cursor appears there, but
29365 mouse highlighting does not. */
29366 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29367 mouse_face_here_p = true;
29369 /* Maybe clear the display under the cursor. */
29370 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29372 int x, y;
29373 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29374 int width;
29376 cursor_glyph = get_phys_cursor_glyph (w);
29377 if (cursor_glyph == NULL)
29378 goto mark_cursor_off;
29380 width = cursor_glyph->pixel_width;
29381 x = w->phys_cursor.x;
29382 if (x < 0)
29384 width += x;
29385 x = 0;
29387 width = min (width, window_box_width (w, TEXT_AREA) - x);
29388 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29389 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29391 if (width > 0)
29392 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29395 /* Erase the cursor by redrawing the character underneath it. */
29396 if (mouse_face_here_p)
29397 hl = DRAW_MOUSE_FACE;
29398 else
29399 hl = DRAW_NORMAL_TEXT;
29400 draw_phys_cursor_glyph (w, cursor_row, hl);
29402 mark_cursor_off:
29403 w->phys_cursor_on_p = false;
29404 w->phys_cursor_type = NO_CURSOR;
29408 /* Display or clear cursor of window W. If !ON, clear the cursor.
29409 If ON, display the cursor; where to put the cursor is specified by
29410 HPOS, VPOS, X and Y. */
29412 void
29413 display_and_set_cursor (struct window *w, bool on,
29414 int hpos, int vpos, int x, int y)
29416 struct frame *f = XFRAME (w->frame);
29417 int new_cursor_type;
29418 int new_cursor_width;
29419 bool active_cursor;
29420 struct glyph_row *glyph_row;
29421 struct glyph *glyph;
29423 /* This is pointless on invisible frames, and dangerous on garbaged
29424 windows and frames; in the latter case, the frame or window may
29425 be in the midst of changing its size, and x and y may be off the
29426 window. */
29427 if (! FRAME_VISIBLE_P (f)
29428 || vpos >= w->current_matrix->nrows
29429 || hpos >= w->current_matrix->matrix_w)
29430 return;
29432 /* If cursor is off and we want it off, return quickly. */
29433 if (!on && !w->phys_cursor_on_p)
29434 return;
29436 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29437 /* If cursor row is not enabled, we don't really know where to
29438 display the cursor. */
29439 if (!glyph_row->enabled_p)
29441 w->phys_cursor_on_p = false;
29442 return;
29445 /* A frame might be marked garbaged even though its cursor position
29446 is correct, and will not change upon subsequent redisplay. This
29447 happens in some rare situations, like toggling the sort order in
29448 Dired windows. We've already established that VPOS is valid, so
29449 it shouldn't do any harm to record the cursor position, as we are
29450 going to return without acting on it anyway. Otherwise, expose
29451 events might come in and call update_window_cursor, which will
29452 blindly use outdated values in w->phys_cursor. */
29453 if (FRAME_GARBAGED_P (f))
29455 if (on)
29457 w->phys_cursor.x = x;
29458 w->phys_cursor.y = glyph_row->y;
29459 w->phys_cursor.hpos = hpos;
29460 w->phys_cursor.vpos = vpos;
29462 return;
29465 glyph = NULL;
29466 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29467 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29469 eassert (input_blocked_p ());
29471 /* Set new_cursor_type to the cursor we want to be displayed. */
29472 new_cursor_type = get_window_cursor_type (w, glyph,
29473 &new_cursor_width, &active_cursor);
29475 /* If cursor is currently being shown and we don't want it to be or
29476 it is in the wrong place, or the cursor type is not what we want,
29477 erase it. */
29478 if (w->phys_cursor_on_p
29479 && (!on
29480 || w->phys_cursor.x != x
29481 || w->phys_cursor.y != y
29482 /* HPOS can be negative in R2L rows whose
29483 exact_window_width_line_p flag is set (i.e. their newline
29484 would "overflow into the fringe"). */
29485 || hpos < 0
29486 || new_cursor_type != w->phys_cursor_type
29487 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29488 && new_cursor_width != w->phys_cursor_width)))
29489 erase_phys_cursor (w);
29491 /* Don't check phys_cursor_on_p here because that flag is only set
29492 to false in some cases where we know that the cursor has been
29493 completely erased, to avoid the extra work of erasing the cursor
29494 twice. In other words, phys_cursor_on_p can be true and the cursor
29495 still not be visible, or it has only been partly erased. */
29496 if (on)
29498 w->phys_cursor_ascent = glyph_row->ascent;
29499 w->phys_cursor_height = glyph_row->height;
29501 /* Set phys_cursor_.* before x_draw_.* is called because some
29502 of them may need the information. */
29503 w->phys_cursor.x = x;
29504 w->phys_cursor.y = glyph_row->y;
29505 w->phys_cursor.hpos = hpos;
29506 w->phys_cursor.vpos = vpos;
29509 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29510 new_cursor_type, new_cursor_width,
29511 on, active_cursor);
29515 /* Switch the display of W's cursor on or off, according to the value
29516 of ON. */
29518 static void
29519 update_window_cursor (struct window *w, bool on)
29521 /* Don't update cursor in windows whose frame is in the process
29522 of being deleted. */
29523 if (w->current_matrix)
29525 int hpos = w->phys_cursor.hpos;
29526 int vpos = w->phys_cursor.vpos;
29527 struct glyph_row *row;
29529 if (vpos >= w->current_matrix->nrows
29530 || hpos >= w->current_matrix->matrix_w)
29531 return;
29533 row = MATRIX_ROW (w->current_matrix, vpos);
29535 /* When the window is hscrolled, cursor hpos can legitimately be
29536 out of bounds, but we draw the cursor at the corresponding
29537 window margin in that case. */
29538 if (!row->reversed_p && hpos < 0)
29539 hpos = 0;
29540 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29541 hpos = row->used[TEXT_AREA] - 1;
29543 block_input ();
29544 display_and_set_cursor (w, on, hpos, vpos,
29545 w->phys_cursor.x, w->phys_cursor.y);
29546 unblock_input ();
29551 /* Call update_window_cursor with parameter ON_P on all leaf windows
29552 in the window tree rooted at W. */
29554 static void
29555 update_cursor_in_window_tree (struct window *w, bool on_p)
29557 while (w)
29559 if (WINDOWP (w->contents))
29560 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29561 else
29562 update_window_cursor (w, on_p);
29564 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29569 /* EXPORT:
29570 Display the cursor on window W, or clear it, according to ON_P.
29571 Don't change the cursor's position. */
29573 void
29574 x_update_cursor (struct frame *f, bool on_p)
29576 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29580 /* EXPORT:
29581 Clear the cursor of window W to background color, and mark the
29582 cursor as not shown. This is used when the text where the cursor
29583 is about to be rewritten. */
29585 void
29586 x_clear_cursor (struct window *w)
29588 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29589 update_window_cursor (w, false);
29592 #endif /* HAVE_WINDOW_SYSTEM */
29594 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29595 and MSDOS. */
29596 static void
29597 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29598 int start_hpos, int end_hpos,
29599 enum draw_glyphs_face draw)
29601 #ifdef HAVE_WINDOW_SYSTEM
29602 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29604 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29605 return;
29607 #endif
29608 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29609 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29610 #endif
29613 /* Display the active region described by mouse_face_* according to DRAW. */
29615 static void
29616 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29618 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29619 struct frame *f = XFRAME (WINDOW_FRAME (w));
29621 if (/* If window is in the process of being destroyed, don't bother
29622 to do anything. */
29623 w->current_matrix != NULL
29624 /* Don't update mouse highlight if hidden. */
29625 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29626 /* Recognize when we are called to operate on rows that don't exist
29627 anymore. This can happen when a window is split. */
29628 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29630 bool phys_cursor_on_p = w->phys_cursor_on_p;
29631 struct glyph_row *row, *first, *last;
29633 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29634 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29636 for (row = first; row <= last && row->enabled_p; ++row)
29638 int start_hpos, end_hpos, start_x;
29640 /* For all but the first row, the highlight starts at column 0. */
29641 if (row == first)
29643 /* R2L rows have BEG and END in reversed order, but the
29644 screen drawing geometry is always left to right. So
29645 we need to mirror the beginning and end of the
29646 highlighted area in R2L rows. */
29647 if (!row->reversed_p)
29649 start_hpos = hlinfo->mouse_face_beg_col;
29650 start_x = hlinfo->mouse_face_beg_x;
29652 else if (row == last)
29654 start_hpos = hlinfo->mouse_face_end_col;
29655 start_x = hlinfo->mouse_face_end_x;
29657 else
29659 start_hpos = 0;
29660 start_x = 0;
29663 else if (row->reversed_p && row == last)
29665 start_hpos = hlinfo->mouse_face_end_col;
29666 start_x = hlinfo->mouse_face_end_x;
29668 else
29670 start_hpos = 0;
29671 start_x = 0;
29674 if (row == last)
29676 if (!row->reversed_p)
29677 end_hpos = hlinfo->mouse_face_end_col;
29678 else if (row == first)
29679 end_hpos = hlinfo->mouse_face_beg_col;
29680 else
29682 end_hpos = row->used[TEXT_AREA];
29683 if (draw == DRAW_NORMAL_TEXT)
29684 row->fill_line_p = true; /* Clear to end of line. */
29687 else if (row->reversed_p && row == first)
29688 end_hpos = hlinfo->mouse_face_beg_col;
29689 else
29691 end_hpos = row->used[TEXT_AREA];
29692 if (draw == DRAW_NORMAL_TEXT)
29693 row->fill_line_p = true; /* Clear to end of line. */
29696 if (end_hpos > start_hpos)
29698 draw_row_with_mouse_face (w, start_x, row,
29699 start_hpos, end_hpos, draw);
29701 row->mouse_face_p
29702 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29706 /* When we've written over the cursor, arrange for it to
29707 be displayed again. */
29708 if (FRAME_WINDOW_P (f)
29709 && phys_cursor_on_p && !w->phys_cursor_on_p)
29711 #ifdef HAVE_WINDOW_SYSTEM
29712 int hpos = w->phys_cursor.hpos;
29714 /* When the window is hscrolled, cursor hpos can legitimately be
29715 out of bounds, but we draw the cursor at the corresponding
29716 window margin in that case. */
29717 if (!row->reversed_p && hpos < 0)
29718 hpos = 0;
29719 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29720 hpos = row->used[TEXT_AREA] - 1;
29722 block_input ();
29723 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29724 w->phys_cursor.x, w->phys_cursor.y);
29725 unblock_input ();
29726 #endif /* HAVE_WINDOW_SYSTEM */
29730 #ifdef HAVE_WINDOW_SYSTEM
29731 /* Change the mouse cursor. */
29732 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29734 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29735 if (draw == DRAW_NORMAL_TEXT
29736 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29737 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29738 else
29739 #endif
29740 if (draw == DRAW_MOUSE_FACE)
29741 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29742 else
29743 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29745 #endif /* HAVE_WINDOW_SYSTEM */
29748 /* EXPORT:
29749 Clear out the mouse-highlighted active region.
29750 Redraw it un-highlighted first. Value is true if mouse
29751 face was actually drawn unhighlighted. */
29753 bool
29754 clear_mouse_face (Mouse_HLInfo *hlinfo)
29756 bool cleared
29757 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29758 if (cleared)
29759 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29760 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29761 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29762 hlinfo->mouse_face_window = Qnil;
29763 hlinfo->mouse_face_overlay = Qnil;
29764 return cleared;
29767 /* Return true if the coordinates HPOS and VPOS on windows W are
29768 within the mouse face on that window. */
29769 static bool
29770 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29772 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29774 /* Quickly resolve the easy cases. */
29775 if (!(WINDOWP (hlinfo->mouse_face_window)
29776 && XWINDOW (hlinfo->mouse_face_window) == w))
29777 return false;
29778 if (vpos < hlinfo->mouse_face_beg_row
29779 || vpos > hlinfo->mouse_face_end_row)
29780 return false;
29781 if (vpos > hlinfo->mouse_face_beg_row
29782 && vpos < hlinfo->mouse_face_end_row)
29783 return true;
29785 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29787 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29789 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29790 return true;
29792 else if ((vpos == hlinfo->mouse_face_beg_row
29793 && hpos >= hlinfo->mouse_face_beg_col)
29794 || (vpos == hlinfo->mouse_face_end_row
29795 && hpos < hlinfo->mouse_face_end_col))
29796 return true;
29798 else
29800 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29802 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29803 return true;
29805 else if ((vpos == hlinfo->mouse_face_beg_row
29806 && hpos <= hlinfo->mouse_face_beg_col)
29807 || (vpos == hlinfo->mouse_face_end_row
29808 && hpos > hlinfo->mouse_face_end_col))
29809 return true;
29811 return false;
29815 /* EXPORT:
29816 True if physical cursor of window W is within mouse face. */
29818 bool
29819 cursor_in_mouse_face_p (struct window *w)
29821 int hpos = w->phys_cursor.hpos;
29822 int vpos = w->phys_cursor.vpos;
29823 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29825 /* When the window is hscrolled, cursor hpos can legitimately be out
29826 of bounds, but we draw the cursor at the corresponding window
29827 margin in that case. */
29828 if (!row->reversed_p && hpos < 0)
29829 hpos = 0;
29830 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29831 hpos = row->used[TEXT_AREA] - 1;
29833 return coords_in_mouse_face_p (w, hpos, vpos);
29838 /* Find the glyph rows START_ROW and END_ROW of window W that display
29839 characters between buffer positions START_CHARPOS and END_CHARPOS
29840 (excluding END_CHARPOS). DISP_STRING is a display string that
29841 covers these buffer positions. This is similar to
29842 row_containing_pos, but is more accurate when bidi reordering makes
29843 buffer positions change non-linearly with glyph rows. */
29844 static void
29845 rows_from_pos_range (struct window *w,
29846 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29847 Lisp_Object disp_string,
29848 struct glyph_row **start, struct glyph_row **end)
29850 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29851 int last_y = window_text_bottom_y (w);
29852 struct glyph_row *row;
29854 *start = NULL;
29855 *end = NULL;
29857 while (!first->enabled_p
29858 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29859 first++;
29861 /* Find the START row. */
29862 for (row = first;
29863 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29864 row++)
29866 /* A row can potentially be the START row if the range of the
29867 characters it displays intersects the range
29868 [START_CHARPOS..END_CHARPOS). */
29869 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29870 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29871 /* See the commentary in row_containing_pos, for the
29872 explanation of the complicated way to check whether
29873 some position is beyond the end of the characters
29874 displayed by a row. */
29875 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29876 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29877 && !row->ends_at_zv_p
29878 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29879 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29880 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29881 && !row->ends_at_zv_p
29882 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29884 /* Found a candidate row. Now make sure at least one of the
29885 glyphs it displays has a charpos from the range
29886 [START_CHARPOS..END_CHARPOS).
29888 This is not obvious because bidi reordering could make
29889 buffer positions of a row be 1,2,3,102,101,100, and if we
29890 want to highlight characters in [50..60), we don't want
29891 this row, even though [50..60) does intersect [1..103),
29892 the range of character positions given by the row's start
29893 and end positions. */
29894 struct glyph *g = row->glyphs[TEXT_AREA];
29895 struct glyph *e = g + row->used[TEXT_AREA];
29897 while (g < e)
29899 if (((BUFFERP (g->object) || NILP (g->object))
29900 && start_charpos <= g->charpos && g->charpos < end_charpos)
29901 /* A glyph that comes from DISP_STRING is by
29902 definition to be highlighted. */
29903 || EQ (g->object, disp_string))
29904 *start = row;
29905 g++;
29907 if (*start)
29908 break;
29912 /* Find the END row. */
29913 if (!*start
29914 /* If the last row is partially visible, start looking for END
29915 from that row, instead of starting from FIRST. */
29916 && !(row->enabled_p
29917 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29918 row = first;
29919 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29921 struct glyph_row *next = row + 1;
29922 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29924 if (!next->enabled_p
29925 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29926 /* The first row >= START whose range of displayed characters
29927 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29928 is the row END + 1. */
29929 || (start_charpos < next_start
29930 && end_charpos < next_start)
29931 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29932 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29933 && !next->ends_at_zv_p
29934 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29935 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29936 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29937 && !next->ends_at_zv_p
29938 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29940 *end = row;
29941 break;
29943 else
29945 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29946 but none of the characters it displays are in the range, it is
29947 also END + 1. */
29948 struct glyph *g = next->glyphs[TEXT_AREA];
29949 struct glyph *s = g;
29950 struct glyph *e = g + next->used[TEXT_AREA];
29952 while (g < e)
29954 if (((BUFFERP (g->object) || NILP (g->object))
29955 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29956 /* If the buffer position of the first glyph in
29957 the row is equal to END_CHARPOS, it means
29958 the last character to be highlighted is the
29959 newline of ROW, and we must consider NEXT as
29960 END, not END+1. */
29961 || (((!next->reversed_p && g == s)
29962 || (next->reversed_p && g == e - 1))
29963 && (g->charpos == end_charpos
29964 /* Special case for when NEXT is an
29965 empty line at ZV. */
29966 || (g->charpos == -1
29967 && !row->ends_at_zv_p
29968 && next_start == end_charpos)))))
29969 /* A glyph that comes from DISP_STRING is by
29970 definition to be highlighted. */
29971 || EQ (g->object, disp_string))
29972 break;
29973 g++;
29975 if (g == e)
29977 *end = row;
29978 break;
29980 /* The first row that ends at ZV must be the last to be
29981 highlighted. */
29982 else if (next->ends_at_zv_p)
29984 *end = next;
29985 break;
29991 /* This function sets the mouse_face_* elements of HLINFO, assuming
29992 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29993 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29994 for the overlay or run of text properties specifying the mouse
29995 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29996 before-string and after-string that must also be highlighted.
29997 DISP_STRING, if non-nil, is a display string that may cover some
29998 or all of the highlighted text. */
30000 static void
30001 mouse_face_from_buffer_pos (Lisp_Object window,
30002 Mouse_HLInfo *hlinfo,
30003 ptrdiff_t mouse_charpos,
30004 ptrdiff_t start_charpos,
30005 ptrdiff_t end_charpos,
30006 Lisp_Object before_string,
30007 Lisp_Object after_string,
30008 Lisp_Object disp_string)
30010 struct window *w = XWINDOW (window);
30011 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30012 struct glyph_row *r1, *r2;
30013 struct glyph *glyph, *end;
30014 ptrdiff_t ignore, pos;
30015 int x;
30017 eassert (NILP (disp_string) || STRINGP (disp_string));
30018 eassert (NILP (before_string) || STRINGP (before_string));
30019 eassert (NILP (after_string) || STRINGP (after_string));
30021 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30022 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30023 if (r1 == NULL)
30024 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30025 /* If the before-string or display-string contains newlines,
30026 rows_from_pos_range skips to its last row. Move back. */
30027 if (!NILP (before_string) || !NILP (disp_string))
30029 struct glyph_row *prev;
30030 while ((prev = r1 - 1, prev >= first)
30031 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30032 && prev->used[TEXT_AREA] > 0)
30034 struct glyph *beg = prev->glyphs[TEXT_AREA];
30035 glyph = beg + prev->used[TEXT_AREA];
30036 while (--glyph >= beg && NILP (glyph->object));
30037 if (glyph < beg
30038 || !(EQ (glyph->object, before_string)
30039 || EQ (glyph->object, disp_string)))
30040 break;
30041 r1 = prev;
30044 if (r2 == NULL)
30046 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30047 hlinfo->mouse_face_past_end = true;
30049 else if (!NILP (after_string))
30051 /* If the after-string has newlines, advance to its last row. */
30052 struct glyph_row *next;
30053 struct glyph_row *last
30054 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30056 for (next = r2 + 1;
30057 next <= last
30058 && next->used[TEXT_AREA] > 0
30059 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30060 ++next)
30061 r2 = next;
30063 /* The rest of the display engine assumes that mouse_face_beg_row is
30064 either above mouse_face_end_row or identical to it. But with
30065 bidi-reordered continued lines, the row for START_CHARPOS could
30066 be below the row for END_CHARPOS. If so, swap the rows and store
30067 them in correct order. */
30068 if (r1->y > r2->y)
30070 struct glyph_row *tem = r2;
30072 r2 = r1;
30073 r1 = tem;
30076 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30077 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30079 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30080 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30081 could be anywhere in the row and in any order. The strategy
30082 below is to find the leftmost and the rightmost glyph that
30083 belongs to either of these 3 strings, or whose position is
30084 between START_CHARPOS and END_CHARPOS, and highlight all the
30085 glyphs between those two. This may cover more than just the text
30086 between START_CHARPOS and END_CHARPOS if the range of characters
30087 strides the bidi level boundary, e.g. if the beginning is in R2L
30088 text while the end is in L2R text or vice versa. */
30089 if (!r1->reversed_p)
30091 /* This row is in a left to right paragraph. Scan it left to
30092 right. */
30093 glyph = r1->glyphs[TEXT_AREA];
30094 end = glyph + r1->used[TEXT_AREA];
30095 x = r1->x;
30097 /* Skip truncation glyphs at the start of the glyph row. */
30098 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30099 for (; glyph < end
30100 && NILP (glyph->object)
30101 && glyph->charpos < 0;
30102 ++glyph)
30103 x += glyph->pixel_width;
30105 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30106 or DISP_STRING, and the first glyph from buffer whose
30107 position is between START_CHARPOS and END_CHARPOS. */
30108 for (; glyph < end
30109 && !NILP (glyph->object)
30110 && !EQ (glyph->object, disp_string)
30111 && !(BUFFERP (glyph->object)
30112 && (glyph->charpos >= start_charpos
30113 && glyph->charpos < end_charpos));
30114 ++glyph)
30116 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30117 are present at buffer positions between START_CHARPOS and
30118 END_CHARPOS, or if they come from an overlay. */
30119 if (EQ (glyph->object, before_string))
30121 pos = string_buffer_position (before_string,
30122 start_charpos);
30123 /* If pos == 0, it means before_string came from an
30124 overlay, not from a buffer position. */
30125 if (!pos || (pos >= start_charpos && pos < end_charpos))
30126 break;
30128 else if (EQ (glyph->object, after_string))
30130 pos = string_buffer_position (after_string, end_charpos);
30131 if (!pos || (pos >= start_charpos && pos < end_charpos))
30132 break;
30134 x += glyph->pixel_width;
30136 hlinfo->mouse_face_beg_x = x;
30137 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30139 else
30141 /* This row is in a right to left paragraph. Scan it right to
30142 left. */
30143 struct glyph *g;
30145 end = r1->glyphs[TEXT_AREA] - 1;
30146 glyph = end + r1->used[TEXT_AREA];
30148 /* Skip truncation glyphs at the start of the glyph row. */
30149 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30150 for (; glyph > end
30151 && NILP (glyph->object)
30152 && glyph->charpos < 0;
30153 --glyph)
30156 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30157 or DISP_STRING, and the first glyph from buffer whose
30158 position is between START_CHARPOS and END_CHARPOS. */
30159 for (; glyph > end
30160 && !NILP (glyph->object)
30161 && !EQ (glyph->object, disp_string)
30162 && !(BUFFERP (glyph->object)
30163 && (glyph->charpos >= start_charpos
30164 && glyph->charpos < end_charpos));
30165 --glyph)
30167 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30168 are present at buffer positions between START_CHARPOS and
30169 END_CHARPOS, or if they come from an overlay. */
30170 if (EQ (glyph->object, before_string))
30172 pos = string_buffer_position (before_string, start_charpos);
30173 /* If pos == 0, it means before_string came from an
30174 overlay, not from a buffer position. */
30175 if (!pos || (pos >= start_charpos && pos < end_charpos))
30176 break;
30178 else if (EQ (glyph->object, after_string))
30180 pos = string_buffer_position (after_string, end_charpos);
30181 if (!pos || (pos >= start_charpos && pos < end_charpos))
30182 break;
30186 glyph++; /* first glyph to the right of the highlighted area */
30187 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30188 x += g->pixel_width;
30189 hlinfo->mouse_face_beg_x = x;
30190 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30193 /* If the highlight ends in a different row, compute GLYPH and END
30194 for the end row. Otherwise, reuse the values computed above for
30195 the row where the highlight begins. */
30196 if (r2 != r1)
30198 if (!r2->reversed_p)
30200 glyph = r2->glyphs[TEXT_AREA];
30201 end = glyph + r2->used[TEXT_AREA];
30202 x = r2->x;
30204 else
30206 end = r2->glyphs[TEXT_AREA] - 1;
30207 glyph = end + r2->used[TEXT_AREA];
30211 if (!r2->reversed_p)
30213 /* Skip truncation and continuation glyphs near the end of the
30214 row, and also blanks and stretch glyphs inserted by
30215 extend_face_to_end_of_line. */
30216 while (end > glyph
30217 && NILP ((end - 1)->object))
30218 --end;
30219 /* Scan the rest of the glyph row from the end, looking for the
30220 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30221 DISP_STRING, or whose position is between START_CHARPOS
30222 and END_CHARPOS */
30223 for (--end;
30224 end > glyph
30225 && !NILP (end->object)
30226 && !EQ (end->object, disp_string)
30227 && !(BUFFERP (end->object)
30228 && (end->charpos >= start_charpos
30229 && end->charpos < end_charpos));
30230 --end)
30232 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30233 are present at buffer positions between START_CHARPOS and
30234 END_CHARPOS, or if they come from an overlay. */
30235 if (EQ (end->object, before_string))
30237 pos = string_buffer_position (before_string, start_charpos);
30238 if (!pos || (pos >= start_charpos && pos < end_charpos))
30239 break;
30241 else if (EQ (end->object, after_string))
30243 pos = string_buffer_position (after_string, end_charpos);
30244 if (!pos || (pos >= start_charpos && pos < end_charpos))
30245 break;
30248 /* Find the X coordinate of the last glyph to be highlighted. */
30249 for (; glyph <= end; ++glyph)
30250 x += glyph->pixel_width;
30252 hlinfo->mouse_face_end_x = x;
30253 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30255 else
30257 /* Skip truncation and continuation glyphs near the end of the
30258 row, and also blanks and stretch glyphs inserted by
30259 extend_face_to_end_of_line. */
30260 x = r2->x;
30261 end++;
30262 while (end < glyph
30263 && NILP (end->object))
30265 x += end->pixel_width;
30266 ++end;
30268 /* Scan the rest of the glyph row from the end, looking for the
30269 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30270 DISP_STRING, or whose position is between START_CHARPOS
30271 and END_CHARPOS */
30272 for ( ;
30273 end < glyph
30274 && !NILP (end->object)
30275 && !EQ (end->object, disp_string)
30276 && !(BUFFERP (end->object)
30277 && (end->charpos >= start_charpos
30278 && end->charpos < end_charpos));
30279 ++end)
30281 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30282 are present at buffer positions between START_CHARPOS and
30283 END_CHARPOS, or if they come from an overlay. */
30284 if (EQ (end->object, before_string))
30286 pos = string_buffer_position (before_string, start_charpos);
30287 if (!pos || (pos >= start_charpos && pos < end_charpos))
30288 break;
30290 else if (EQ (end->object, after_string))
30292 pos = string_buffer_position (after_string, end_charpos);
30293 if (!pos || (pos >= start_charpos && pos < end_charpos))
30294 break;
30296 x += end->pixel_width;
30298 /* If we exited the above loop because we arrived at the last
30299 glyph of the row, and its buffer position is still not in
30300 range, it means the last character in range is the preceding
30301 newline. Bump the end column and x values to get past the
30302 last glyph. */
30303 if (end == glyph
30304 && BUFFERP (end->object)
30305 && (end->charpos < start_charpos
30306 || end->charpos >= end_charpos))
30308 x += end->pixel_width;
30309 ++end;
30311 hlinfo->mouse_face_end_x = x;
30312 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30315 hlinfo->mouse_face_window = window;
30316 hlinfo->mouse_face_face_id
30317 = face_at_buffer_position (w, mouse_charpos, &ignore,
30318 mouse_charpos + 1,
30319 !hlinfo->mouse_face_hidden, -1);
30320 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30323 /* The following function is not used anymore (replaced with
30324 mouse_face_from_string_pos), but I leave it here for the time
30325 being, in case someone would. */
30327 #if false /* not used */
30329 /* Find the position of the glyph for position POS in OBJECT in
30330 window W's current matrix, and return in *X, *Y the pixel
30331 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30333 RIGHT_P means return the position of the right edge of the glyph.
30334 !RIGHT_P means return the left edge position.
30336 If no glyph for POS exists in the matrix, return the position of
30337 the glyph with the next smaller position that is in the matrix, if
30338 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30339 exists in the matrix, return the position of the glyph with the
30340 next larger position in OBJECT.
30342 Value is true if a glyph was found. */
30344 static bool
30345 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30346 int *hpos, int *vpos, int *x, int *y, bool right_p)
30348 int yb = window_text_bottom_y (w);
30349 struct glyph_row *r;
30350 struct glyph *best_glyph = NULL;
30351 struct glyph_row *best_row = NULL;
30352 int best_x = 0;
30354 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30355 r->enabled_p && r->y < yb;
30356 ++r)
30358 struct glyph *g = r->glyphs[TEXT_AREA];
30359 struct glyph *e = g + r->used[TEXT_AREA];
30360 int gx;
30362 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30363 if (EQ (g->object, object))
30365 if (g->charpos == pos)
30367 best_glyph = g;
30368 best_x = gx;
30369 best_row = r;
30370 goto found;
30372 else if (best_glyph == NULL
30373 || ((eabs (g->charpos - pos)
30374 < eabs (best_glyph->charpos - pos))
30375 && (right_p
30376 ? g->charpos < pos
30377 : g->charpos > pos)))
30379 best_glyph = g;
30380 best_x = gx;
30381 best_row = r;
30386 found:
30388 if (best_glyph)
30390 *x = best_x;
30391 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30393 if (right_p)
30395 *x += best_glyph->pixel_width;
30396 ++*hpos;
30399 *y = best_row->y;
30400 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30403 return best_glyph != NULL;
30405 #endif /* not used */
30407 /* Find the positions of the first and the last glyphs in window W's
30408 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30409 (assumed to be a string), and return in HLINFO's mouse_face_*
30410 members the pixel and column/row coordinates of those glyphs. */
30412 static void
30413 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30414 Lisp_Object object,
30415 ptrdiff_t startpos, ptrdiff_t endpos)
30417 int yb = window_text_bottom_y (w);
30418 struct glyph_row *r;
30419 struct glyph *g, *e;
30420 int gx;
30421 bool found = false;
30423 /* Find the glyph row with at least one position in the range
30424 [STARTPOS..ENDPOS), and the first glyph in that row whose
30425 position belongs to that range. */
30426 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30427 r->enabled_p && r->y < yb;
30428 ++r)
30430 if (!r->reversed_p)
30432 g = r->glyphs[TEXT_AREA];
30433 e = g + r->used[TEXT_AREA];
30434 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30435 if (EQ (g->object, object)
30436 && startpos <= g->charpos && g->charpos < endpos)
30438 hlinfo->mouse_face_beg_row
30439 = MATRIX_ROW_VPOS (r, w->current_matrix);
30440 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30441 hlinfo->mouse_face_beg_x = gx;
30442 found = true;
30443 break;
30446 else
30448 struct glyph *g1;
30450 e = r->glyphs[TEXT_AREA];
30451 g = e + r->used[TEXT_AREA];
30452 for ( ; g > e; --g)
30453 if (EQ ((g-1)->object, object)
30454 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30456 hlinfo->mouse_face_beg_row
30457 = MATRIX_ROW_VPOS (r, w->current_matrix);
30458 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30459 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30460 gx += g1->pixel_width;
30461 hlinfo->mouse_face_beg_x = gx;
30462 found = true;
30463 break;
30466 if (found)
30467 break;
30470 if (!found)
30471 return;
30473 /* Starting with the next row, look for the first row which does NOT
30474 include any glyphs whose positions are in the range. */
30475 for (++r; r->enabled_p && r->y < yb; ++r)
30477 g = r->glyphs[TEXT_AREA];
30478 e = g + r->used[TEXT_AREA];
30479 found = false;
30480 for ( ; g < e; ++g)
30481 if (EQ (g->object, object)
30482 && startpos <= g->charpos && g->charpos < endpos)
30484 found = true;
30485 break;
30487 if (!found)
30488 break;
30491 /* The highlighted region ends on the previous row. */
30492 r--;
30494 /* Set the end row. */
30495 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30497 /* Compute and set the end column and the end column's horizontal
30498 pixel coordinate. */
30499 if (!r->reversed_p)
30501 g = r->glyphs[TEXT_AREA];
30502 e = g + r->used[TEXT_AREA];
30503 for ( ; e > g; --e)
30504 if (EQ ((e-1)->object, object)
30505 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30506 break;
30507 hlinfo->mouse_face_end_col = e - g;
30509 for (gx = r->x; g < e; ++g)
30510 gx += g->pixel_width;
30511 hlinfo->mouse_face_end_x = gx;
30513 else
30515 e = r->glyphs[TEXT_AREA];
30516 g = e + r->used[TEXT_AREA];
30517 for (gx = r->x ; e < g; ++e)
30519 if (EQ (e->object, object)
30520 && startpos <= e->charpos && e->charpos < endpos)
30521 break;
30522 gx += e->pixel_width;
30524 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30525 hlinfo->mouse_face_end_x = gx;
30529 #ifdef HAVE_WINDOW_SYSTEM
30531 /* See if position X, Y is within a hot-spot of an image. */
30533 static bool
30534 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30536 if (!CONSP (hot_spot))
30537 return false;
30539 if (EQ (XCAR (hot_spot), Qrect))
30541 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30542 Lisp_Object rect = XCDR (hot_spot);
30543 Lisp_Object tem;
30544 if (!CONSP (rect))
30545 return false;
30546 if (!CONSP (XCAR (rect)))
30547 return false;
30548 if (!CONSP (XCDR (rect)))
30549 return false;
30550 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30551 return false;
30552 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30553 return false;
30554 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30555 return false;
30556 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30557 return false;
30558 return true;
30560 else if (EQ (XCAR (hot_spot), Qcircle))
30562 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30563 Lisp_Object circ = XCDR (hot_spot);
30564 Lisp_Object lr, lx0, ly0;
30565 if (CONSP (circ)
30566 && CONSP (XCAR (circ))
30567 && (lr = XCDR (circ), NUMBERP (lr))
30568 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30569 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30571 double r = XFLOATINT (lr);
30572 double dx = XINT (lx0) - x;
30573 double dy = XINT (ly0) - y;
30574 return (dx * dx + dy * dy <= r * r);
30577 else if (EQ (XCAR (hot_spot), Qpoly))
30579 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30580 if (VECTORP (XCDR (hot_spot)))
30582 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30583 Lisp_Object *poly = v->contents;
30584 ptrdiff_t n = v->header.size;
30585 ptrdiff_t i;
30586 bool inside = false;
30587 Lisp_Object lx, ly;
30588 int x0, y0;
30590 /* Need an even number of coordinates, and at least 3 edges. */
30591 if (n < 6 || n & 1)
30592 return false;
30594 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30595 If count is odd, we are inside polygon. Pixels on edges
30596 may or may not be included depending on actual geometry of the
30597 polygon. */
30598 if ((lx = poly[n-2], !INTEGERP (lx))
30599 || (ly = poly[n-1], !INTEGERP (lx)))
30600 return false;
30601 x0 = XINT (lx), y0 = XINT (ly);
30602 for (i = 0; i < n; i += 2)
30604 int x1 = x0, y1 = y0;
30605 if ((lx = poly[i], !INTEGERP (lx))
30606 || (ly = poly[i+1], !INTEGERP (ly)))
30607 return false;
30608 x0 = XINT (lx), y0 = XINT (ly);
30610 /* Does this segment cross the X line? */
30611 if (x0 >= x)
30613 if (x1 >= x)
30614 continue;
30616 else if (x1 < x)
30617 continue;
30618 if (y > y0 && y > y1)
30619 continue;
30620 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30621 inside = !inside;
30623 return inside;
30626 return false;
30629 Lisp_Object
30630 find_hot_spot (Lisp_Object map, int x, int y)
30632 while (CONSP (map))
30634 if (CONSP (XCAR (map))
30635 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30636 return XCAR (map);
30637 map = XCDR (map);
30640 return Qnil;
30643 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30644 3, 3, 0,
30645 doc: /* Lookup in image map MAP coordinates X and Y.
30646 An image map is an alist where each element has the format (AREA ID PLIST).
30647 An AREA is specified as either a rectangle, a circle, or a polygon:
30648 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30649 pixel coordinates of the upper left and bottom right corners.
30650 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30651 and the radius of the circle; r may be a float or integer.
30652 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30653 vector describes one corner in the polygon.
30654 Returns the alist element for the first matching AREA in MAP. */)
30655 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30657 if (NILP (map))
30658 return Qnil;
30660 CHECK_NUMBER (x);
30661 CHECK_NUMBER (y);
30663 return find_hot_spot (map,
30664 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30665 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30667 #endif /* HAVE_WINDOW_SYSTEM */
30670 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30671 static void
30672 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30674 #ifdef HAVE_WINDOW_SYSTEM
30675 if (!FRAME_WINDOW_P (f))
30676 return;
30678 /* Do not change cursor shape while dragging mouse. */
30679 if (EQ (do_mouse_tracking, Qdragging))
30680 return;
30682 if (!NILP (pointer))
30684 if (EQ (pointer, Qarrow))
30685 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30686 else if (EQ (pointer, Qhand))
30687 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30688 else if (EQ (pointer, Qtext))
30689 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30690 else if (EQ (pointer, intern ("hdrag")))
30691 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30692 else if (EQ (pointer, intern ("nhdrag")))
30693 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30694 # ifdef HAVE_X_WINDOWS
30695 else if (EQ (pointer, intern ("vdrag")))
30696 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30697 # endif
30698 else if (EQ (pointer, intern ("hourglass")))
30699 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30700 else if (EQ (pointer, Qmodeline))
30701 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30702 else
30703 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30706 if (cursor != No_Cursor)
30707 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30708 #endif
30711 /* Take proper action when mouse has moved to the mode or header line
30712 or marginal area AREA of window W, x-position X and y-position Y.
30713 X is relative to the start of the text display area of W, so the
30714 width of bitmap areas and scroll bars must be subtracted to get a
30715 position relative to the start of the mode line. */
30717 static void
30718 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30719 enum window_part area)
30721 struct window *w = XWINDOW (window);
30722 struct frame *f = XFRAME (w->frame);
30723 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30724 #ifdef HAVE_WINDOW_SYSTEM
30725 Display_Info *dpyinfo;
30726 #endif
30727 Cursor cursor = No_Cursor;
30728 Lisp_Object pointer = Qnil;
30729 int dx, dy, width, height;
30730 ptrdiff_t charpos;
30731 Lisp_Object string, object = Qnil;
30732 Lisp_Object pos UNINIT;
30733 Lisp_Object mouse_face;
30734 int original_x_pixel = x;
30735 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30736 struct glyph_row *row UNINIT;
30738 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30740 int x0;
30741 struct glyph *end;
30743 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30744 returns them in row/column units! */
30745 string = mode_line_string (w, area, &x, &y, &charpos,
30746 &object, &dx, &dy, &width, &height);
30748 row = (area == ON_MODE_LINE
30749 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30750 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30752 /* Find the glyph under the mouse pointer. */
30753 if (row->mode_line_p && row->enabled_p)
30755 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30756 end = glyph + row->used[TEXT_AREA];
30758 for (x0 = original_x_pixel;
30759 glyph < end && x0 >= glyph->pixel_width;
30760 ++glyph)
30761 x0 -= glyph->pixel_width;
30763 if (glyph >= end)
30764 glyph = NULL;
30767 else
30769 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30770 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30771 returns them in row/column units! */
30772 string = marginal_area_string (w, area, &x, &y, &charpos,
30773 &object, &dx, &dy, &width, &height);
30776 Lisp_Object help = Qnil;
30778 #ifdef HAVE_WINDOW_SYSTEM
30779 if (IMAGEP (object))
30781 Lisp_Object image_map, hotspot;
30782 if ((image_map = Fplist_get (XCDR (object), QCmap),
30783 !NILP (image_map))
30784 && (hotspot = find_hot_spot (image_map, dx, dy),
30785 CONSP (hotspot))
30786 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30788 Lisp_Object plist;
30790 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30791 If so, we could look for mouse-enter, mouse-leave
30792 properties in PLIST (and do something...). */
30793 hotspot = XCDR (hotspot);
30794 if (CONSP (hotspot)
30795 && (plist = XCAR (hotspot), CONSP (plist)))
30797 pointer = Fplist_get (plist, Qpointer);
30798 if (NILP (pointer))
30799 pointer = Qhand;
30800 help = Fplist_get (plist, Qhelp_echo);
30801 if (!NILP (help))
30803 help_echo_string = help;
30804 XSETWINDOW (help_echo_window, w);
30805 help_echo_object = w->contents;
30806 help_echo_pos = charpos;
30810 if (NILP (pointer))
30811 pointer = Fplist_get (XCDR (object), QCpointer);
30813 #endif /* HAVE_WINDOW_SYSTEM */
30815 if (STRINGP (string))
30816 pos = make_number (charpos);
30818 /* Set the help text and mouse pointer. If the mouse is on a part
30819 of the mode line without any text (e.g. past the right edge of
30820 the mode line text), use the default help text and pointer. */
30821 if (STRINGP (string) || area == ON_MODE_LINE)
30823 /* Arrange to display the help by setting the global variables
30824 help_echo_string, help_echo_object, and help_echo_pos. */
30825 if (NILP (help))
30827 if (STRINGP (string))
30828 help = Fget_text_property (pos, Qhelp_echo, string);
30830 if (!NILP (help))
30832 help_echo_string = help;
30833 XSETWINDOW (help_echo_window, w);
30834 help_echo_object = string;
30835 help_echo_pos = charpos;
30837 else if (area == ON_MODE_LINE)
30839 Lisp_Object default_help
30840 = buffer_local_value (Qmode_line_default_help_echo,
30841 w->contents);
30843 if (STRINGP (default_help))
30845 help_echo_string = default_help;
30846 XSETWINDOW (help_echo_window, w);
30847 help_echo_object = Qnil;
30848 help_echo_pos = -1;
30853 #ifdef HAVE_WINDOW_SYSTEM
30854 /* Change the mouse pointer according to what is under it. */
30855 if (FRAME_WINDOW_P (f))
30857 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30858 || minibuf_level
30859 || NILP (Vresize_mini_windows));
30861 dpyinfo = FRAME_DISPLAY_INFO (f);
30862 if (STRINGP (string))
30864 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30866 if (NILP (pointer))
30867 pointer = Fget_text_property (pos, Qpointer, string);
30869 /* Change the mouse pointer according to what is under X/Y. */
30870 if (NILP (pointer)
30871 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30873 Lisp_Object map;
30874 map = Fget_text_property (pos, Qlocal_map, string);
30875 if (!KEYMAPP (map))
30876 map = Fget_text_property (pos, Qkeymap, string);
30877 if (!KEYMAPP (map) && draggable)
30878 cursor = dpyinfo->vertical_scroll_bar_cursor;
30881 else if (draggable)
30882 /* Default mode-line pointer. */
30883 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30885 #endif
30888 /* Change the mouse face according to what is under X/Y. */
30889 bool mouse_face_shown = false;
30890 if (STRINGP (string))
30892 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30893 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30894 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30895 && glyph)
30897 Lisp_Object b, e;
30899 struct glyph * tmp_glyph;
30901 int gpos;
30902 int gseq_length;
30903 int total_pixel_width;
30904 ptrdiff_t begpos, endpos, ignore;
30906 int vpos, hpos;
30908 b = Fprevious_single_property_change (make_number (charpos + 1),
30909 Qmouse_face, string, Qnil);
30910 if (NILP (b))
30911 begpos = 0;
30912 else
30913 begpos = XINT (b);
30915 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30916 if (NILP (e))
30917 endpos = SCHARS (string);
30918 else
30919 endpos = XINT (e);
30921 /* Calculate the glyph position GPOS of GLYPH in the
30922 displayed string, relative to the beginning of the
30923 highlighted part of the string.
30925 Note: GPOS is different from CHARPOS. CHARPOS is the
30926 position of GLYPH in the internal string object. A mode
30927 line string format has structures which are converted to
30928 a flattened string by the Emacs Lisp interpreter. The
30929 internal string is an element of those structures. The
30930 displayed string is the flattened string. */
30931 tmp_glyph = row_start_glyph;
30932 while (tmp_glyph < glyph
30933 && (!(EQ (tmp_glyph->object, glyph->object)
30934 && begpos <= tmp_glyph->charpos
30935 && tmp_glyph->charpos < endpos)))
30936 tmp_glyph++;
30937 gpos = glyph - tmp_glyph;
30939 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30940 the highlighted part of the displayed string to which
30941 GLYPH belongs. Note: GSEQ_LENGTH is different from
30942 SCHARS (STRING), because the latter returns the length of
30943 the internal string. */
30944 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30945 tmp_glyph > glyph
30946 && (!(EQ (tmp_glyph->object, glyph->object)
30947 && begpos <= tmp_glyph->charpos
30948 && tmp_glyph->charpos < endpos));
30949 tmp_glyph--)
30951 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30953 /* Calculate the total pixel width of all the glyphs between
30954 the beginning of the highlighted area and GLYPH. */
30955 total_pixel_width = 0;
30956 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30957 total_pixel_width += tmp_glyph->pixel_width;
30959 /* Pre calculation of re-rendering position. Note: X is in
30960 column units here, after the call to mode_line_string or
30961 marginal_area_string. */
30962 hpos = x - gpos;
30963 vpos = (area == ON_MODE_LINE
30964 ? (w->current_matrix)->nrows - 1
30965 : 0);
30967 /* If GLYPH's position is included in the region that is
30968 already drawn in mouse face, we have nothing to do. */
30969 if ( EQ (window, hlinfo->mouse_face_window)
30970 && (!row->reversed_p
30971 ? (hlinfo->mouse_face_beg_col <= hpos
30972 && hpos < hlinfo->mouse_face_end_col)
30973 /* In R2L rows we swap BEG and END, see below. */
30974 : (hlinfo->mouse_face_end_col <= hpos
30975 && hpos < hlinfo->mouse_face_beg_col))
30976 && hlinfo->mouse_face_beg_row == vpos )
30977 return;
30979 if (clear_mouse_face (hlinfo))
30980 cursor = No_Cursor;
30982 if (!row->reversed_p)
30984 hlinfo->mouse_face_beg_col = hpos;
30985 hlinfo->mouse_face_beg_x = original_x_pixel
30986 - (total_pixel_width + dx);
30987 hlinfo->mouse_face_end_col = hpos + gseq_length;
30988 hlinfo->mouse_face_end_x = 0;
30990 else
30992 /* In R2L rows, show_mouse_face expects BEG and END
30993 coordinates to be swapped. */
30994 hlinfo->mouse_face_end_col = hpos;
30995 hlinfo->mouse_face_end_x = original_x_pixel
30996 - (total_pixel_width + dx);
30997 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30998 hlinfo->mouse_face_beg_x = 0;
31001 hlinfo->mouse_face_beg_row = vpos;
31002 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31003 hlinfo->mouse_face_past_end = false;
31004 hlinfo->mouse_face_window = window;
31006 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31007 charpos,
31008 0, &ignore,
31009 glyph->face_id,
31010 true);
31011 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31012 mouse_face_shown = true;
31014 if (NILP (pointer))
31015 pointer = Qhand;
31019 /* If mouse-face doesn't need to be shown, clear any existing
31020 mouse-face. */
31021 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31022 clear_mouse_face (hlinfo);
31024 define_frame_cursor1 (f, cursor, pointer);
31028 /* EXPORT:
31029 Take proper action when the mouse has moved to position X, Y on
31030 frame F with regards to highlighting portions of display that have
31031 mouse-face properties. Also de-highlight portions of display where
31032 the mouse was before, set the mouse pointer shape as appropriate
31033 for the mouse coordinates, and activate help echo (tooltips).
31034 X and Y can be negative or out of range. */
31036 void
31037 note_mouse_highlight (struct frame *f, int x, int y)
31039 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31040 enum window_part part = ON_NOTHING;
31041 Lisp_Object window;
31042 struct window *w;
31043 Cursor cursor = No_Cursor;
31044 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31045 struct buffer *b;
31047 /* When a menu is active, don't highlight because this looks odd. */
31048 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31049 if (popup_activated ())
31050 return;
31051 #endif
31053 if (!f->glyphs_initialized_p
31054 || f->pointer_invisible)
31055 return;
31057 hlinfo->mouse_face_mouse_x = x;
31058 hlinfo->mouse_face_mouse_y = y;
31059 hlinfo->mouse_face_mouse_frame = f;
31061 if (hlinfo->mouse_face_defer)
31062 return;
31064 /* Which window is that in? */
31065 window = window_from_coordinates (f, x, y, &part, true);
31067 /* If displaying active text in another window, clear that. */
31068 if (! EQ (window, hlinfo->mouse_face_window)
31069 /* Also clear if we move out of text area in same window. */
31070 || (!NILP (hlinfo->mouse_face_window)
31071 && !NILP (window)
31072 && part != ON_TEXT
31073 && part != ON_MODE_LINE
31074 && part != ON_HEADER_LINE))
31075 clear_mouse_face (hlinfo);
31077 /* Reset help_echo_string. It will get recomputed below. */
31078 help_echo_string = Qnil;
31080 #ifdef HAVE_WINDOW_SYSTEM
31081 /* If the cursor is on the internal border of FRAME and FRAME's
31082 internal border is draggable, provide some visual feedback. */
31083 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31084 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31086 enum internal_border_part part = frame_internal_border_part (f, x, y);
31088 switch (part)
31090 case INTERNAL_BORDER_NONE:
31091 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31092 /* Reset cursor. */
31093 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31094 break;
31095 case INTERNAL_BORDER_LEFT_EDGE:
31096 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31097 break;
31098 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31099 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31100 break;
31101 case INTERNAL_BORDER_TOP_EDGE:
31102 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31103 break;
31104 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31105 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31106 break;
31107 case INTERNAL_BORDER_RIGHT_EDGE:
31108 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31109 break;
31110 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31111 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31112 break;
31113 case INTERNAL_BORDER_BOTTOM_EDGE:
31114 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31115 break;
31116 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31117 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31118 break;
31119 default:
31120 /* This should not happen. */
31121 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31122 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31125 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31127 /* Do we really want a help echo here? */
31128 help_echo_string = build_string ("drag-mouse-1: resize frame");
31129 goto set_cursor;
31132 #endif /* HAVE_WINDOW_SYSTEM */
31134 /* Not on a window -> return. */
31135 if (!WINDOWP (window))
31136 return;
31138 /* Convert to window-relative pixel coordinates. */
31139 w = XWINDOW (window);
31140 frame_to_window_pixel_xy (w, &x, &y);
31142 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31143 /* Handle tool-bar window differently since it doesn't display a
31144 buffer. */
31145 if (EQ (window, f->tool_bar_window))
31147 note_tool_bar_highlight (f, x, y);
31148 return;
31150 #endif
31152 /* Mouse is on the mode, header line or margin? */
31153 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31154 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31156 note_mode_line_or_margin_highlight (window, x, y, part);
31158 #ifdef HAVE_WINDOW_SYSTEM
31159 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31161 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31162 /* Show non-text cursor (Bug#16647). */
31163 goto set_cursor;
31165 else
31166 #endif
31167 return;
31170 #ifdef HAVE_WINDOW_SYSTEM
31171 if (part == ON_VERTICAL_BORDER)
31173 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31174 help_echo_string = build_string ("drag-mouse-1: resize");
31175 goto set_cursor;
31177 else if (part == ON_RIGHT_DIVIDER)
31179 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31180 help_echo_string = build_string ("drag-mouse-1: resize");
31181 goto set_cursor;
31183 else if (part == ON_BOTTOM_DIVIDER)
31184 if (! WINDOW_BOTTOMMOST_P (w)
31185 || minibuf_level
31186 || NILP (Vresize_mini_windows))
31188 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31189 help_echo_string = build_string ("drag-mouse-1: resize");
31190 goto set_cursor;
31192 else
31193 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31194 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31195 || part == ON_VERTICAL_SCROLL_BAR
31196 || part == ON_HORIZONTAL_SCROLL_BAR)
31197 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31198 else
31199 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31200 #endif
31202 /* Are we in a window whose display is up to date?
31203 And verify the buffer's text has not changed. */
31204 b = XBUFFER (w->contents);
31205 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31207 int hpos, vpos, dx, dy, area = LAST_AREA;
31208 ptrdiff_t pos;
31209 struct glyph *glyph;
31210 Lisp_Object object;
31211 Lisp_Object mouse_face = Qnil, position;
31212 Lisp_Object *overlay_vec = NULL;
31213 ptrdiff_t i, noverlays;
31214 struct buffer *obuf;
31215 ptrdiff_t obegv, ozv;
31216 bool same_region;
31218 /* Find the glyph under X/Y. */
31219 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31221 #ifdef HAVE_WINDOW_SYSTEM
31222 /* Look for :pointer property on image. */
31223 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31225 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31226 if (img != NULL && IMAGEP (img->spec))
31228 Lisp_Object image_map, hotspot;
31229 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31230 !NILP (image_map))
31231 && (hotspot = find_hot_spot (image_map,
31232 glyph->slice.img.x + dx,
31233 glyph->slice.img.y + dy),
31234 CONSP (hotspot))
31235 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31237 Lisp_Object plist;
31239 /* Could check XCAR (hotspot) to see if we enter/leave
31240 this hot-spot.
31241 If so, we could look for mouse-enter, mouse-leave
31242 properties in PLIST (and do something...). */
31243 hotspot = XCDR (hotspot);
31244 if (CONSP (hotspot)
31245 && (plist = XCAR (hotspot), CONSP (plist)))
31247 pointer = Fplist_get (plist, Qpointer);
31248 if (NILP (pointer))
31249 pointer = Qhand;
31250 help_echo_string = Fplist_get (plist, Qhelp_echo);
31251 if (!NILP (help_echo_string))
31253 help_echo_window = window;
31254 help_echo_object = glyph->object;
31255 help_echo_pos = glyph->charpos;
31259 if (NILP (pointer))
31260 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31263 #endif /* HAVE_WINDOW_SYSTEM */
31265 /* Clear mouse face if X/Y not over text. */
31266 if (glyph == NULL
31267 || area != TEXT_AREA
31268 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31269 /* Glyph's OBJECT is nil for glyphs inserted by the
31270 display engine for its internal purposes, like truncation
31271 and continuation glyphs and blanks beyond the end of
31272 line's text on text terminals. If we are over such a
31273 glyph, we are not over any text. */
31274 || NILP (glyph->object)
31275 /* R2L rows have a stretch glyph at their front, which
31276 stands for no text, whereas L2R rows have no glyphs at
31277 all beyond the end of text. Treat such stretch glyphs
31278 like we do with NULL glyphs in L2R rows. */
31279 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31280 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31281 && glyph->type == STRETCH_GLYPH
31282 && glyph->avoid_cursor_p))
31284 if (clear_mouse_face (hlinfo))
31285 cursor = No_Cursor;
31286 if (FRAME_WINDOW_P (f) && NILP (pointer))
31288 #ifdef HAVE_WINDOW_SYSTEM
31289 if (area != TEXT_AREA)
31290 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31291 else
31292 pointer = Vvoid_text_area_pointer;
31293 #endif
31295 goto set_cursor;
31298 pos = glyph->charpos;
31299 object = glyph->object;
31300 if (!STRINGP (object) && !BUFFERP (object))
31301 goto set_cursor;
31303 /* If we get an out-of-range value, return now; avoid an error. */
31304 if (BUFFERP (object) && pos > BUF_Z (b))
31305 goto set_cursor;
31307 /* Make the window's buffer temporarily current for
31308 overlays_at and compute_char_face. */
31309 obuf = current_buffer;
31310 current_buffer = b;
31311 obegv = BEGV;
31312 ozv = ZV;
31313 BEGV = BEG;
31314 ZV = Z;
31316 /* Is this char mouse-active or does it have help-echo? */
31317 position = make_number (pos);
31319 USE_SAFE_ALLOCA;
31321 if (BUFFERP (object))
31323 /* Put all the overlays we want in a vector in overlay_vec. */
31324 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31325 /* Sort overlays into increasing priority order. */
31326 noverlays = sort_overlays (overlay_vec, noverlays, w);
31328 else
31329 noverlays = 0;
31331 if (NILP (Vmouse_highlight))
31333 clear_mouse_face (hlinfo);
31334 goto check_help_echo;
31337 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31339 if (same_region)
31340 cursor = No_Cursor;
31342 /* Check mouse-face highlighting. */
31343 if (! same_region
31344 /* If there exists an overlay with mouse-face overlapping
31345 the one we are currently highlighting, we have to
31346 check if we enter the overlapping overlay, and then
31347 highlight only that. */
31348 || (OVERLAYP (hlinfo->mouse_face_overlay)
31349 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31351 /* Find the highest priority overlay with a mouse-face. */
31352 Lisp_Object overlay = Qnil;
31353 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31355 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31356 if (!NILP (mouse_face))
31357 overlay = overlay_vec[i];
31360 /* If we're highlighting the same overlay as before, there's
31361 no need to do that again. */
31362 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31363 goto check_help_echo;
31365 /* Clear the display of the old active region, if any. */
31366 if (clear_mouse_face (hlinfo))
31367 cursor = No_Cursor;
31369 /* Record the overlay, if any, to be highlighted. */
31370 hlinfo->mouse_face_overlay = overlay;
31372 /* If no overlay applies, get a text property. */
31373 if (NILP (overlay))
31374 mouse_face = Fget_text_property (position, Qmouse_face, object);
31376 /* Next, compute the bounds of the mouse highlighting and
31377 display it. */
31378 if (!NILP (mouse_face) && STRINGP (object))
31380 /* The mouse-highlighting comes from a display string
31381 with a mouse-face. */
31382 Lisp_Object s, e;
31383 ptrdiff_t ignore;
31385 s = Fprevious_single_property_change
31386 (make_number (pos + 1), Qmouse_face, object, Qnil);
31387 e = Fnext_single_property_change
31388 (position, Qmouse_face, object, Qnil);
31389 if (NILP (s))
31390 s = make_number (0);
31391 if (NILP (e))
31392 e = make_number (SCHARS (object));
31393 mouse_face_from_string_pos (w, hlinfo, object,
31394 XINT (s), XINT (e));
31395 hlinfo->mouse_face_past_end = false;
31396 hlinfo->mouse_face_window = window;
31397 hlinfo->mouse_face_face_id
31398 = face_at_string_position (w, object, pos, 0, &ignore,
31399 glyph->face_id, true);
31400 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31401 cursor = No_Cursor;
31403 else
31405 /* The mouse-highlighting, if any, comes from an overlay
31406 or text property in the buffer. */
31407 Lisp_Object buffer UNINIT;
31408 Lisp_Object disp_string UNINIT;
31410 if (STRINGP (object))
31412 /* If we are on a display string with no mouse-face,
31413 check if the text under it has one. */
31414 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31415 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31416 pos = string_buffer_position (object, start);
31417 if (pos > 0)
31419 mouse_face = get_char_property_and_overlay
31420 (make_number (pos), Qmouse_face, w->contents, &overlay);
31421 buffer = w->contents;
31422 disp_string = object;
31425 else
31427 buffer = object;
31428 disp_string = Qnil;
31431 if (!NILP (mouse_face))
31433 Lisp_Object before, after;
31434 Lisp_Object before_string, after_string;
31435 /* To correctly find the limits of mouse highlight
31436 in a bidi-reordered buffer, we must not use the
31437 optimization of limiting the search in
31438 previous-single-property-change and
31439 next-single-property-change, because
31440 rows_from_pos_range needs the real start and end
31441 positions to DTRT in this case. That's because
31442 the first row visible in a window does not
31443 necessarily display the character whose position
31444 is the smallest. */
31445 Lisp_Object lim1
31446 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31447 ? Fmarker_position (w->start)
31448 : Qnil;
31449 Lisp_Object lim2
31450 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31451 ? make_number (BUF_Z (XBUFFER (buffer))
31452 - w->window_end_pos)
31453 : Qnil;
31455 if (NILP (overlay))
31457 /* Handle the text property case. */
31458 before = Fprevious_single_property_change
31459 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31460 after = Fnext_single_property_change
31461 (make_number (pos), Qmouse_face, buffer, lim2);
31462 before_string = after_string = Qnil;
31464 else
31466 /* Handle the overlay case. */
31467 before = Foverlay_start (overlay);
31468 after = Foverlay_end (overlay);
31469 before_string = Foverlay_get (overlay, Qbefore_string);
31470 after_string = Foverlay_get (overlay, Qafter_string);
31472 if (!STRINGP (before_string)) before_string = Qnil;
31473 if (!STRINGP (after_string)) after_string = Qnil;
31476 mouse_face_from_buffer_pos (window, hlinfo, pos,
31477 NILP (before)
31479 : XFASTINT (before),
31480 NILP (after)
31481 ? BUF_Z (XBUFFER (buffer))
31482 : XFASTINT (after),
31483 before_string, after_string,
31484 disp_string);
31485 cursor = No_Cursor;
31490 check_help_echo:
31492 /* Look for a `help-echo' property. */
31493 if (NILP (help_echo_string)) {
31494 Lisp_Object help, overlay;
31496 /* Check overlays first. */
31497 help = overlay = Qnil;
31498 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31500 overlay = overlay_vec[i];
31501 help = Foverlay_get (overlay, Qhelp_echo);
31504 if (!NILP (help))
31506 help_echo_string = help;
31507 help_echo_window = window;
31508 help_echo_object = overlay;
31509 help_echo_pos = pos;
31511 else
31513 Lisp_Object obj = glyph->object;
31514 ptrdiff_t charpos = glyph->charpos;
31516 /* Try text properties. */
31517 if (STRINGP (obj)
31518 && charpos >= 0
31519 && charpos < SCHARS (obj))
31521 help = Fget_text_property (make_number (charpos),
31522 Qhelp_echo, obj);
31523 if (NILP (help))
31525 /* If the string itself doesn't specify a help-echo,
31526 see if the buffer text ``under'' it does. */
31527 struct glyph_row *r
31528 = MATRIX_ROW (w->current_matrix, vpos);
31529 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31530 ptrdiff_t p = string_buffer_position (obj, start);
31531 if (p > 0)
31533 help = Fget_char_property (make_number (p),
31534 Qhelp_echo, w->contents);
31535 if (!NILP (help))
31537 charpos = p;
31538 obj = w->contents;
31543 else if (BUFFERP (obj)
31544 && charpos >= BEGV
31545 && charpos < ZV)
31546 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31547 obj);
31549 if (!NILP (help))
31551 help_echo_string = help;
31552 help_echo_window = window;
31553 help_echo_object = obj;
31554 help_echo_pos = charpos;
31559 #ifdef HAVE_WINDOW_SYSTEM
31560 /* Look for a `pointer' property. */
31561 if (FRAME_WINDOW_P (f) && NILP (pointer))
31563 /* Check overlays first. */
31564 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31565 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31567 if (NILP (pointer))
31569 Lisp_Object obj = glyph->object;
31570 ptrdiff_t charpos = glyph->charpos;
31572 /* Try text properties. */
31573 if (STRINGP (obj)
31574 && charpos >= 0
31575 && charpos < SCHARS (obj))
31577 pointer = Fget_text_property (make_number (charpos),
31578 Qpointer, obj);
31579 if (NILP (pointer))
31581 /* If the string itself doesn't specify a pointer,
31582 see if the buffer text ``under'' it does. */
31583 struct glyph_row *r
31584 = MATRIX_ROW (w->current_matrix, vpos);
31585 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31586 ptrdiff_t p = string_buffer_position (obj, start);
31587 if (p > 0)
31588 pointer = Fget_char_property (make_number (p),
31589 Qpointer, w->contents);
31592 else if (BUFFERP (obj)
31593 && charpos >= BEGV
31594 && charpos < ZV)
31595 pointer = Fget_text_property (make_number (charpos),
31596 Qpointer, obj);
31599 #endif /* HAVE_WINDOW_SYSTEM */
31601 BEGV = obegv;
31602 ZV = ozv;
31603 current_buffer = obuf;
31604 SAFE_FREE ();
31607 set_cursor:
31608 define_frame_cursor1 (f, cursor, pointer);
31612 /* EXPORT for RIF:
31613 Clear any mouse-face on window W. This function is part of the
31614 redisplay interface, and is called from try_window_id and similar
31615 functions to ensure the mouse-highlight is off. */
31617 void
31618 x_clear_window_mouse_face (struct window *w)
31620 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31621 Lisp_Object window;
31623 block_input ();
31624 XSETWINDOW (window, w);
31625 if (EQ (window, hlinfo->mouse_face_window))
31626 clear_mouse_face (hlinfo);
31627 unblock_input ();
31631 /* EXPORT:
31632 Just discard the mouse face information for frame F, if any.
31633 This is used when the size of F is changed. */
31635 void
31636 cancel_mouse_face (struct frame *f)
31638 Lisp_Object window;
31639 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31641 window = hlinfo->mouse_face_window;
31642 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31643 reset_mouse_highlight (hlinfo);
31648 /***********************************************************************
31649 Exposure Events
31650 ***********************************************************************/
31652 #ifdef HAVE_WINDOW_SYSTEM
31654 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31655 which intersects rectangle R. R is in window-relative coordinates. */
31657 static void
31658 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31659 enum glyph_row_area area)
31661 struct glyph *first = row->glyphs[area];
31662 struct glyph *end = row->glyphs[area] + row->used[area];
31663 struct glyph *last;
31664 int first_x, start_x, x;
31666 if (area == TEXT_AREA && row->fill_line_p)
31667 /* If row extends face to end of line write the whole line. */
31668 draw_glyphs (w, 0, row, area,
31669 0, row->used[area],
31670 DRAW_NORMAL_TEXT, 0);
31671 else
31673 /* Set START_X to the window-relative start position for drawing glyphs of
31674 AREA. The first glyph of the text area can be partially visible.
31675 The first glyphs of other areas cannot. */
31676 start_x = window_box_left_offset (w, area);
31677 x = start_x;
31678 if (area == TEXT_AREA)
31679 x += row->x;
31681 /* Find the first glyph that must be redrawn. */
31682 while (first < end
31683 && x + first->pixel_width < r->x)
31685 x += first->pixel_width;
31686 ++first;
31689 /* Find the last one. */
31690 last = first;
31691 first_x = x;
31692 /* Use a signed int intermediate value to avoid catastrophic
31693 failures due to comparison between signed and unsigned, when
31694 x is negative (can happen for wide images that are hscrolled). */
31695 int r_end = r->x + r->width;
31696 while (last < end && x < r_end)
31698 x += last->pixel_width;
31699 ++last;
31702 /* Repaint. */
31703 if (last > first)
31704 draw_glyphs (w, first_x - start_x, row, area,
31705 first - row->glyphs[area], last - row->glyphs[area],
31706 DRAW_NORMAL_TEXT, 0);
31711 /* Redraw the parts of the glyph row ROW on window W intersecting
31712 rectangle R. R is in window-relative coordinates. Value is
31713 true if mouse-face was overwritten. */
31715 static bool
31716 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31718 eassert (row->enabled_p);
31720 if (row->mode_line_p || w->pseudo_window_p)
31721 draw_glyphs (w, 0, row, TEXT_AREA,
31722 0, row->used[TEXT_AREA],
31723 DRAW_NORMAL_TEXT, 0);
31724 else
31726 if (row->used[LEFT_MARGIN_AREA])
31727 expose_area (w, row, r, LEFT_MARGIN_AREA);
31728 if (row->used[TEXT_AREA])
31729 expose_area (w, row, r, TEXT_AREA);
31730 if (row->used[RIGHT_MARGIN_AREA])
31731 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31732 draw_row_fringe_bitmaps (w, row);
31735 return row->mouse_face_p;
31739 /* Redraw those parts of glyphs rows during expose event handling that
31740 overlap other rows. Redrawing of an exposed line writes over parts
31741 of lines overlapping that exposed line; this function fixes that.
31743 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31744 row in W's current matrix that is exposed and overlaps other rows.
31745 LAST_OVERLAPPING_ROW is the last such row. */
31747 static void
31748 expose_overlaps (struct window *w,
31749 struct glyph_row *first_overlapping_row,
31750 struct glyph_row *last_overlapping_row,
31751 XRectangle *r)
31753 struct glyph_row *row;
31755 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31756 if (row->overlapping_p)
31758 eassert (row->enabled_p && !row->mode_line_p);
31760 row->clip = r;
31761 if (row->used[LEFT_MARGIN_AREA])
31762 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31764 if (row->used[TEXT_AREA])
31765 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31767 if (row->used[RIGHT_MARGIN_AREA])
31768 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31769 row->clip = NULL;
31774 /* Return true if W's cursor intersects rectangle R. */
31776 static bool
31777 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31779 XRectangle cr, result;
31780 struct glyph *cursor_glyph;
31781 struct glyph_row *row;
31783 if (w->phys_cursor.vpos >= 0
31784 && w->phys_cursor.vpos < w->current_matrix->nrows
31785 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31786 row->enabled_p)
31787 && row->cursor_in_fringe_p)
31789 /* Cursor is in the fringe. */
31790 cr.x = window_box_right_offset (w,
31791 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31792 ? RIGHT_MARGIN_AREA
31793 : TEXT_AREA));
31794 cr.y = row->y;
31795 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31796 cr.height = row->height;
31797 return x_intersect_rectangles (&cr, r, &result);
31800 cursor_glyph = get_phys_cursor_glyph (w);
31801 if (cursor_glyph)
31803 /* r is relative to W's box, but w->phys_cursor.x is relative
31804 to left edge of W's TEXT area. Adjust it. */
31805 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31806 cr.y = w->phys_cursor.y;
31807 cr.width = cursor_glyph->pixel_width;
31808 cr.height = w->phys_cursor_height;
31809 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31810 I assume the effect is the same -- and this is portable. */
31811 return x_intersect_rectangles (&cr, r, &result);
31813 /* If we don't understand the format, pretend we're not in the hot-spot. */
31814 return false;
31818 /* EXPORT:
31819 Draw a vertical window border to the right of window W if W doesn't
31820 have vertical scroll bars. */
31822 void
31823 x_draw_vertical_border (struct window *w)
31825 struct frame *f = XFRAME (WINDOW_FRAME (w));
31827 /* We could do better, if we knew what type of scroll-bar the adjacent
31828 windows (on either side) have... But we don't :-(
31829 However, I think this works ok. ++KFS 2003-04-25 */
31831 /* Redraw borders between horizontally adjacent windows. Don't
31832 do it for frames with vertical scroll bars because either the
31833 right scroll bar of a window, or the left scroll bar of its
31834 neighbor will suffice as a border. */
31835 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31836 return;
31838 /* Note: It is necessary to redraw both the left and the right
31839 borders, for when only this single window W is being
31840 redisplayed. */
31841 if (!WINDOW_RIGHTMOST_P (w)
31842 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31844 int x0, x1, y0, y1;
31846 window_box_edges (w, &x0, &y0, &x1, &y1);
31847 y1 -= 1;
31849 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31850 x1 -= 1;
31852 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31855 if (!WINDOW_LEFTMOST_P (w)
31856 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31858 int x0, x1, y0, y1;
31860 window_box_edges (w, &x0, &y0, &x1, &y1);
31861 y1 -= 1;
31863 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31864 x0 -= 1;
31866 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31871 /* Draw window dividers for window W. */
31873 void
31874 x_draw_right_divider (struct window *w)
31876 struct frame *f = WINDOW_XFRAME (w);
31878 if (w->mini || w->pseudo_window_p)
31879 return;
31880 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31882 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31883 int x1 = WINDOW_RIGHT_EDGE_X (w);
31884 int y0 = WINDOW_TOP_EDGE_Y (w);
31885 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31887 /* If W is horizontally combined and has a right sibling, don't
31888 draw over any bottom divider. */
31889 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31890 && !NILP (w->parent)
31891 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31892 && !NILP (w->next))
31893 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31895 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31899 static void
31900 x_draw_bottom_divider (struct window *w)
31902 struct frame *f = XFRAME (WINDOW_FRAME (w));
31904 if (w->mini || w->pseudo_window_p)
31905 return;
31906 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31908 int x0 = WINDOW_LEFT_EDGE_X (w);
31909 int x1 = WINDOW_RIGHT_EDGE_X (w);
31910 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31911 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31912 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31914 /* If W is vertically combined and has a sibling below, don't draw
31915 over any right divider. */
31916 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31917 && p
31918 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31919 && !NILP (w->next))
31920 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31921 && NILP (w->next)
31922 && !NILP (p->parent)
31923 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31924 && !NILP (XWINDOW (p->parent)->next))))
31925 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31927 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31931 /* Redraw the part of window W intersection rectangle FR. Pixel
31932 coordinates in FR are frame-relative. Call this function with
31933 input blocked. Value is true if the exposure overwrites
31934 mouse-face. */
31936 static bool
31937 expose_window (struct window *w, XRectangle *fr)
31939 struct frame *f = XFRAME (w->frame);
31940 XRectangle wr, r;
31941 bool mouse_face_overwritten_p = false;
31943 /* If window is not yet fully initialized, do nothing. This can
31944 happen when toolkit scroll bars are used and a window is split.
31945 Reconfiguring the scroll bar will generate an expose for a newly
31946 created window. */
31947 if (w->current_matrix == NULL)
31948 return false;
31950 /* When we're currently updating the window, display and current
31951 matrix usually don't agree. Arrange for a thorough display
31952 later. */
31953 if (w->must_be_updated_p)
31955 SET_FRAME_GARBAGED (f);
31956 return false;
31959 /* Frame-relative pixel rectangle of W. */
31960 wr.x = WINDOW_LEFT_EDGE_X (w);
31961 wr.y = WINDOW_TOP_EDGE_Y (w);
31962 wr.width = WINDOW_PIXEL_WIDTH (w);
31963 wr.height = WINDOW_PIXEL_HEIGHT (w);
31965 if (x_intersect_rectangles (fr, &wr, &r))
31967 int yb = window_text_bottom_y (w);
31968 struct glyph_row *row;
31969 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31971 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31972 r.x, r.y, r.width, r.height));
31974 /* Convert to window coordinates. */
31975 r.x -= WINDOW_LEFT_EDGE_X (w);
31976 r.y -= WINDOW_TOP_EDGE_Y (w);
31978 /* Turn off the cursor. */
31979 bool cursor_cleared_p = (!w->pseudo_window_p
31980 && phys_cursor_in_rect_p (w, &r));
31981 if (cursor_cleared_p)
31982 x_clear_cursor (w);
31984 /* If the row containing the cursor extends face to end of line,
31985 then expose_area might overwrite the cursor outside the
31986 rectangle and thus notice_overwritten_cursor might clear
31987 w->phys_cursor_on_p. We remember the original value and
31988 check later if it is changed. */
31989 bool phys_cursor_on_p = w->phys_cursor_on_p;
31991 /* Use a signed int intermediate value to avoid catastrophic
31992 failures due to comparison between signed and unsigned, when
31993 y0 or y1 is negative (can happen for tall images). */
31994 int r_bottom = r.y + r.height;
31996 /* Update lines intersecting rectangle R. */
31997 first_overlapping_row = last_overlapping_row = NULL;
31998 for (row = w->current_matrix->rows;
31999 row->enabled_p;
32000 ++row)
32002 int y0 = row->y;
32003 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32005 if ((y0 >= r.y && y0 < r_bottom)
32006 || (y1 > r.y && y1 < r_bottom)
32007 || (r.y >= y0 && r.y < y1)
32008 || (r_bottom > y0 && r_bottom < y1))
32010 /* A header line may be overlapping, but there is no need
32011 to fix overlapping areas for them. KFS 2005-02-12 */
32012 if (row->overlapping_p && !row->mode_line_p)
32014 if (first_overlapping_row == NULL)
32015 first_overlapping_row = row;
32016 last_overlapping_row = row;
32019 row->clip = fr;
32020 if (expose_line (w, row, &r))
32021 mouse_face_overwritten_p = true;
32022 row->clip = NULL;
32024 else if (row->overlapping_p)
32026 /* We must redraw a row overlapping the exposed area. */
32027 if (y0 < r.y
32028 ? y0 + row->phys_height > r.y
32029 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32031 if (first_overlapping_row == NULL)
32032 first_overlapping_row = row;
32033 last_overlapping_row = row;
32037 if (y1 >= yb)
32038 break;
32041 /* Display the mode line if there is one. */
32042 if (window_wants_mode_line (w)
32043 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32044 row->enabled_p)
32045 && row->y < r_bottom)
32047 if (expose_line (w, row, &r))
32048 mouse_face_overwritten_p = true;
32051 if (!w->pseudo_window_p)
32053 /* Fix the display of overlapping rows. */
32054 if (first_overlapping_row)
32055 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32056 fr);
32058 /* Draw border between windows. */
32059 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32060 x_draw_right_divider (w);
32061 else
32062 x_draw_vertical_border (w);
32064 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32065 x_draw_bottom_divider (w);
32067 /* Turn the cursor on again. */
32068 if (cursor_cleared_p
32069 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32070 update_window_cursor (w, true);
32074 return mouse_face_overwritten_p;
32079 /* Redraw (parts) of all windows in the window tree rooted at W that
32080 intersect R. R contains frame pixel coordinates. Value is
32081 true if the exposure overwrites mouse-face. */
32083 static bool
32084 expose_window_tree (struct window *w, XRectangle *r)
32086 struct frame *f = XFRAME (w->frame);
32087 bool mouse_face_overwritten_p = false;
32089 while (w && !FRAME_GARBAGED_P (f))
32091 mouse_face_overwritten_p
32092 |= (WINDOWP (w->contents)
32093 ? expose_window_tree (XWINDOW (w->contents), r)
32094 : expose_window (w, r));
32096 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32099 return mouse_face_overwritten_p;
32103 /* EXPORT:
32104 Redisplay an exposed area of frame F. X and Y are the upper-left
32105 corner of the exposed rectangle. W and H are width and height of
32106 the exposed area. All are pixel values. W or H zero means redraw
32107 the entire frame. */
32109 void
32110 expose_frame (struct frame *f, int x, int y, int w, int h)
32112 XRectangle r;
32113 bool mouse_face_overwritten_p = false;
32115 TRACE ((stderr, "expose_frame "));
32117 /* No need to redraw if frame will be redrawn soon. */
32118 if (FRAME_GARBAGED_P (f))
32120 TRACE ((stderr, " garbaged\n"));
32121 return;
32124 /* If basic faces haven't been realized yet, there is no point in
32125 trying to redraw anything. This can happen when we get an expose
32126 event while Emacs is starting, e.g. by moving another window. */
32127 if (FRAME_FACE_CACHE (f) == NULL
32128 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32130 TRACE ((stderr, " no faces\n"));
32131 return;
32134 if (w == 0 || h == 0)
32136 r.x = r.y = 0;
32137 r.width = FRAME_TEXT_WIDTH (f);
32138 r.height = FRAME_TEXT_HEIGHT (f);
32140 else
32142 r.x = x;
32143 r.y = y;
32144 r.width = w;
32145 r.height = h;
32148 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32149 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32151 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32152 if (WINDOWP (f->tool_bar_window))
32153 mouse_face_overwritten_p
32154 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32155 #endif
32157 #ifdef HAVE_X_WINDOWS
32158 #ifndef MSDOS
32159 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32160 if (WINDOWP (f->menu_bar_window))
32161 mouse_face_overwritten_p
32162 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32163 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32164 #endif
32165 #endif
32167 /* Some window managers support a focus-follows-mouse style with
32168 delayed raising of frames. Imagine a partially obscured frame,
32169 and moving the mouse into partially obscured mouse-face on that
32170 frame. The visible part of the mouse-face will be highlighted,
32171 then the WM raises the obscured frame. With at least one WM, KDE
32172 2.1, Emacs is not getting any event for the raising of the frame
32173 (even tried with SubstructureRedirectMask), only Expose events.
32174 These expose events will draw text normally, i.e. not
32175 highlighted. Which means we must redo the highlight here.
32176 Subsume it under ``we love X''. --gerd 2001-08-15 */
32177 /* Included in Windows version because Windows most likely does not
32178 do the right thing if any third party tool offers
32179 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32180 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32182 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32183 if (f == hlinfo->mouse_face_mouse_frame)
32185 int mouse_x = hlinfo->mouse_face_mouse_x;
32186 int mouse_y = hlinfo->mouse_face_mouse_y;
32187 clear_mouse_face (hlinfo);
32188 note_mouse_highlight (f, mouse_x, mouse_y);
32194 /* EXPORT:
32195 Determine the intersection of two rectangles R1 and R2. Return
32196 the intersection in *RESULT. Value is true if RESULT is not
32197 empty. */
32199 bool
32200 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32202 XRectangle *left, *right;
32203 XRectangle *upper, *lower;
32204 bool intersection_p = false;
32206 /* Rearrange so that R1 is the left-most rectangle. */
32207 if (r1->x < r2->x)
32208 left = r1, right = r2;
32209 else
32210 left = r2, right = r1;
32212 /* X0 of the intersection is right.x0, if this is inside R1,
32213 otherwise there is no intersection. */
32214 if (right->x <= left->x + left->width)
32216 result->x = right->x;
32218 /* The right end of the intersection is the minimum of
32219 the right ends of left and right. */
32220 result->width = (min (left->x + left->width, right->x + right->width)
32221 - result->x);
32223 /* Same game for Y. */
32224 if (r1->y < r2->y)
32225 upper = r1, lower = r2;
32226 else
32227 upper = r2, lower = r1;
32229 /* The upper end of the intersection is lower.y0, if this is inside
32230 of upper. Otherwise, there is no intersection. */
32231 if (lower->y <= upper->y + upper->height)
32233 result->y = lower->y;
32235 /* The lower end of the intersection is the minimum of the lower
32236 ends of upper and lower. */
32237 result->height = (min (lower->y + lower->height,
32238 upper->y + upper->height)
32239 - result->y);
32240 intersection_p = true;
32244 return intersection_p;
32247 #endif /* HAVE_WINDOW_SYSTEM */
32250 /***********************************************************************
32251 Initialization
32252 ***********************************************************************/
32254 void
32255 syms_of_xdisp (void)
32257 Vwith_echo_area_save_vector = Qnil;
32258 staticpro (&Vwith_echo_area_save_vector);
32260 Vmessage_stack = Qnil;
32261 staticpro (&Vmessage_stack);
32263 /* Non-nil means don't actually do any redisplay. */
32264 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32266 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32268 DEFVAR_BOOL("inhibit-message", inhibit_message,
32269 doc: /* Non-nil means calls to `message' are not displayed.
32270 They are still logged to the *Messages* buffer. */);
32271 inhibit_message = 0;
32273 message_dolog_marker1 = Fmake_marker ();
32274 staticpro (&message_dolog_marker1);
32275 message_dolog_marker2 = Fmake_marker ();
32276 staticpro (&message_dolog_marker2);
32277 message_dolog_marker3 = Fmake_marker ();
32278 staticpro (&message_dolog_marker3);
32280 defsubr (&Sset_buffer_redisplay);
32281 #ifdef GLYPH_DEBUG
32282 defsubr (&Sdump_frame_glyph_matrix);
32283 defsubr (&Sdump_glyph_matrix);
32284 defsubr (&Sdump_glyph_row);
32285 defsubr (&Sdump_tool_bar_row);
32286 defsubr (&Strace_redisplay);
32287 defsubr (&Strace_to_stderr);
32288 #endif
32289 #ifdef HAVE_WINDOW_SYSTEM
32290 defsubr (&Stool_bar_height);
32291 defsubr (&Slookup_image_map);
32292 #endif
32293 defsubr (&Sline_pixel_height);
32294 defsubr (&Sformat_mode_line);
32295 defsubr (&Sinvisible_p);
32296 defsubr (&Scurrent_bidi_paragraph_direction);
32297 defsubr (&Swindow_text_pixel_size);
32298 defsubr (&Smove_point_visually);
32299 defsubr (&Sbidi_find_overridden_directionality);
32301 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32302 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32303 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32304 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32305 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32306 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32307 DEFSYM (Qeval, "eval");
32308 DEFSYM (QCdata, ":data");
32310 /* Names of text properties relevant for redisplay. */
32311 DEFSYM (Qdisplay, "display");
32312 DEFSYM (Qspace_width, "space-width");
32313 DEFSYM (Qraise, "raise");
32314 DEFSYM (Qslice, "slice");
32315 DEFSYM (Qspace, "space");
32316 DEFSYM (Qmargin, "margin");
32317 DEFSYM (Qpointer, "pointer");
32318 DEFSYM (Qleft_margin, "left-margin");
32319 DEFSYM (Qright_margin, "right-margin");
32320 DEFSYM (Qcenter, "center");
32321 DEFSYM (Qline_height, "line-height");
32322 DEFSYM (QCalign_to, ":align-to");
32323 DEFSYM (QCrelative_width, ":relative-width");
32324 DEFSYM (QCrelative_height, ":relative-height");
32325 DEFSYM (QCeval, ":eval");
32326 DEFSYM (QCpropertize, ":propertize");
32327 DEFSYM (QCfile, ":file");
32328 DEFSYM (Qfontified, "fontified");
32329 DEFSYM (Qfontification_functions, "fontification-functions");
32331 /* Name of the symbol which disables Lisp evaluation in 'display'
32332 properties. This is used by enriched.el. */
32333 DEFSYM (Qdisable_eval, "disable-eval");
32335 /* Name of the face used to highlight trailing whitespace. */
32336 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32338 /* Names of the faces used to display line numbers. */
32339 DEFSYM (Qline_number, "line-number");
32340 DEFSYM (Qline_number_current_line, "line-number-current-line");
32341 /* Name of a text property which disables line-number display. */
32342 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32344 /* Name and number of the face used to highlight escape glyphs. */
32345 DEFSYM (Qescape_glyph, "escape-glyph");
32347 /* Name and number of the face used to highlight non-breaking
32348 spaces/hyphens. */
32349 DEFSYM (Qnobreak_space, "nobreak-space");
32350 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32352 /* The symbol 'image' which is the car of the lists used to represent
32353 images in Lisp. Also a tool bar style. */
32354 DEFSYM (Qimage, "image");
32356 /* Tool bar styles. */
32357 DEFSYM (Qtext, "text");
32358 DEFSYM (Qboth, "both");
32359 DEFSYM (Qboth_horiz, "both-horiz");
32360 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32362 /* The image map types. */
32363 DEFSYM (QCmap, ":map");
32364 DEFSYM (QCpointer, ":pointer");
32365 DEFSYM (Qrect, "rect");
32366 DEFSYM (Qcircle, "circle");
32367 DEFSYM (Qpoly, "poly");
32369 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32371 DEFSYM (Qgrow_only, "grow-only");
32372 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32373 DEFSYM (Qposition, "position");
32374 DEFSYM (Qbuffer_position, "buffer-position");
32375 DEFSYM (Qobject, "object");
32377 /* Cursor shapes. */
32378 DEFSYM (Qbar, "bar");
32379 DEFSYM (Qhbar, "hbar");
32380 DEFSYM (Qbox, "box");
32381 DEFSYM (Qhollow, "hollow");
32383 /* Pointer shapes. */
32384 DEFSYM (Qhand, "hand");
32385 DEFSYM (Qarrow, "arrow");
32386 /* also Qtext */
32388 DEFSYM (Qdragging, "dragging");
32390 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32392 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32393 staticpro (&list_of_error);
32395 /* Values of those variables at last redisplay are stored as
32396 properties on 'overlay-arrow-position' symbol. However, if
32397 Voverlay_arrow_position is a marker, last-arrow-position is its
32398 numerical position. */
32399 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32400 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32402 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32403 properties on a symbol in overlay-arrow-variable-list. */
32404 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32405 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32407 echo_buffer[0] = echo_buffer[1] = Qnil;
32408 staticpro (&echo_buffer[0]);
32409 staticpro (&echo_buffer[1]);
32411 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32412 staticpro (&echo_area_buffer[0]);
32413 staticpro (&echo_area_buffer[1]);
32415 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32416 staticpro (&Vmessages_buffer_name);
32418 mode_line_proptrans_alist = Qnil;
32419 staticpro (&mode_line_proptrans_alist);
32420 mode_line_string_list = Qnil;
32421 staticpro (&mode_line_string_list);
32422 mode_line_string_face = Qnil;
32423 staticpro (&mode_line_string_face);
32424 mode_line_string_face_prop = Qnil;
32425 staticpro (&mode_line_string_face_prop);
32426 Vmode_line_unwind_vector = Qnil;
32427 staticpro (&Vmode_line_unwind_vector);
32429 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32431 help_echo_string = Qnil;
32432 staticpro (&help_echo_string);
32433 help_echo_object = Qnil;
32434 staticpro (&help_echo_object);
32435 help_echo_window = Qnil;
32436 staticpro (&help_echo_window);
32437 previous_help_echo_string = Qnil;
32438 staticpro (&previous_help_echo_string);
32439 help_echo_pos = -1;
32441 DEFSYM (Qright_to_left, "right-to-left");
32442 DEFSYM (Qleft_to_right, "left-to-right");
32443 defsubr (&Sbidi_resolved_levels);
32445 #ifdef HAVE_WINDOW_SYSTEM
32446 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32447 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32448 For example, if a block cursor is over a tab, it will be drawn as
32449 wide as that tab on the display. */);
32450 x_stretch_cursor_p = 0;
32451 #endif
32453 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32454 doc: /* Non-nil means highlight trailing whitespace.
32455 The face used for trailing whitespace is `trailing-whitespace'. */);
32456 Vshow_trailing_whitespace = Qnil;
32458 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32459 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32460 If the value is t, Emacs highlights non-ASCII chars which have the
32461 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32462 or `nobreak-hyphen' face respectively.
32464 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32465 U+2011 (non-breaking hyphen) are affected.
32467 Any other non-nil value means to display these characters as a escape
32468 glyph followed by an ordinary space or hyphen.
32470 A value of nil means no special handling of these characters. */);
32471 Vnobreak_char_display = Qt;
32473 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32474 doc: /* The pointer shape to show in void text areas.
32475 A value of nil means to show the text pointer. Other options are
32476 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32477 `hourglass'. */);
32478 Vvoid_text_area_pointer = Qarrow;
32480 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32481 doc: /* Non-nil means don't actually do any redisplay.
32482 This is used for internal purposes. */);
32483 Vinhibit_redisplay = Qnil;
32485 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32486 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32487 Vglobal_mode_string = Qnil;
32489 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32490 doc: /* Marker for where to display an arrow on top of the buffer text.
32491 This must be the beginning of a line in order to work.
32492 See also `overlay-arrow-string'. */);
32493 Voverlay_arrow_position = Qnil;
32495 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32496 doc: /* String to display as an arrow in non-window frames.
32497 See also `overlay-arrow-position'. */);
32498 Voverlay_arrow_string = build_pure_c_string ("=>");
32500 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32501 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32502 The symbols on this list are examined during redisplay to determine
32503 where to display overlay arrows. */);
32504 Voverlay_arrow_variable_list
32505 = list1 (intern_c_string ("overlay-arrow-position"));
32507 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32508 doc: /* The number of lines to try scrolling a window by when point moves out.
32509 If that fails to bring point back on frame, point is centered instead.
32510 If this is zero, point is always centered after it moves off frame.
32511 If you want scrolling to always be a line at a time, you should set
32512 `scroll-conservatively' to a large value rather than set this to 1. */);
32514 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32515 doc: /* Scroll up to this many lines, to bring point back on screen.
32516 If point moves off-screen, redisplay will scroll by up to
32517 `scroll-conservatively' lines in order to bring point just barely
32518 onto the screen again. If that cannot be done, then redisplay
32519 recenters point as usual.
32521 If the value is greater than 100, redisplay will never recenter point,
32522 but will always scroll just enough text to bring point into view, even
32523 if you move far away.
32525 A value of zero means always recenter point if it moves off screen. */);
32526 scroll_conservatively = 0;
32528 DEFVAR_INT ("scroll-margin", scroll_margin,
32529 doc: /* Number of lines of margin at the top and bottom of a window.
32530 Recenter the window whenever point gets within this many lines
32531 of the top or bottom of the window. */);
32532 scroll_margin = 0;
32534 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32535 doc: /* Maximum effective value of `scroll-margin'.
32536 Given as a fraction of the current window's lines. The value should
32537 be a floating point number between 0.0 and 0.5. The effective maximum
32538 is limited to (/ (1- window-lines) 2). Non-float values for this
32539 variable are ignored and the default 0.25 is used instead. */);
32540 Vmaximum_scroll_margin = make_float (0.25);
32542 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32543 doc: /* Pixels per inch value for non-window system displays.
32544 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32545 Vdisplay_pixels_per_inch = make_float (72.0);
32547 #ifdef GLYPH_DEBUG
32548 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32549 #endif
32551 DEFVAR_LISP ("truncate-partial-width-windows",
32552 Vtruncate_partial_width_windows,
32553 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32554 For an integer value, truncate lines in each window narrower than the
32555 full frame width, provided the total window width in column units is less
32556 than that integer; otherwise, respect the value of `truncate-lines'.
32557 The total width of the window is as returned by `window-total-width', it
32558 includes the fringes, the continuation and truncation glyphs, the
32559 display margins (if any), and the scroll bar
32561 For any other non-nil value, truncate lines in all windows that do
32562 not span the full frame width.
32564 A value of nil means to respect the value of `truncate-lines'.
32566 If `word-wrap' is enabled, you might want to reduce this. */);
32567 Vtruncate_partial_width_windows = make_number (50);
32569 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32570 doc: /* Maximum buffer size for which line number should be displayed.
32571 If the buffer is bigger than this, the line number does not appear
32572 in the mode line. A value of nil means no limit. */);
32573 Vline_number_display_limit = Qnil;
32575 DEFVAR_INT ("line-number-display-limit-width",
32576 line_number_display_limit_width,
32577 doc: /* Maximum line width (in characters) for line number display.
32578 If the average length of the lines near point is bigger than this, then the
32579 line number may be omitted from the mode line. */);
32580 line_number_display_limit_width = 200;
32582 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32583 doc: /* Non-nil means highlight region even in nonselected windows. */);
32584 highlight_nonselected_windows = false;
32586 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32587 doc: /* Non-nil if more than one frame is visible on this display.
32588 Minibuffer-only frames don't count, but iconified frames do.
32589 This variable is not guaranteed to be accurate except while processing
32590 `frame-title-format' and `icon-title-format'. */);
32592 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32593 doc: /* Template for displaying the title bar of visible frames.
32594 \(Assuming the window manager supports this feature.)
32596 This variable has the same structure as `mode-line-format', except that
32597 the %c, %C, and %l constructs are ignored. It is used only on frames for
32598 which no explicit name has been set (see `modify-frame-parameters'). */);
32600 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32601 doc: /* Template for displaying the title bar of an iconified frame.
32602 \(Assuming the window manager supports this feature.)
32603 This variable has the same structure as `mode-line-format' (which see),
32604 and is used only on frames for which no explicit name has been set
32605 \(see `modify-frame-parameters'). */);
32606 Vicon_title_format
32607 = Vframe_title_format
32608 = listn (CONSTYPE_PURE, 3,
32609 intern_c_string ("multiple-frames"),
32610 build_pure_c_string ("%b"),
32611 listn (CONSTYPE_PURE, 4,
32612 empty_unibyte_string,
32613 intern_c_string ("invocation-name"),
32614 build_pure_c_string ("@"),
32615 intern_c_string ("system-name")));
32617 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32618 doc: /* Maximum number of lines to keep in the message log buffer.
32619 If nil, disable message logging. If t, log messages but don't truncate
32620 the buffer when it becomes large. */);
32621 Vmessage_log_max = make_number (1000);
32623 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32624 doc: /* List of functions to call before redisplaying a window with scrolling.
32625 Each function is called with two arguments, the window and its new
32626 display-start position.
32627 These functions are called whenever the `window-start' marker is modified,
32628 either to point into another buffer (e.g. via `set-window-buffer') or another
32629 place in the same buffer.
32630 When each function is called, the `window-start' marker of its window
32631 argument has been already set to the new value, and the buffer which that
32632 window will display is set to be the current buffer.
32633 Note that the value of `window-end' is not valid when these functions are
32634 called.
32636 Warning: Do not use this feature to alter the way the window
32637 is scrolled. It is not designed for that, and such use probably won't
32638 work. */);
32639 Vwindow_scroll_functions = Qnil;
32641 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32642 doc: /* Functions called when redisplay of a window reaches the end trigger.
32643 Each function is called with two arguments, the window and the end trigger value.
32644 See `set-window-redisplay-end-trigger'. */);
32645 Vredisplay_end_trigger_functions = Qnil;
32647 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32648 doc: /* Non-nil means autoselect window with mouse pointer.
32649 If nil, do not autoselect windows.
32650 A positive number means delay autoselection by that many seconds: a
32651 window is autoselected only after the mouse has remained in that
32652 window for the duration of the delay.
32653 A negative number has a similar effect, but causes windows to be
32654 autoselected only after the mouse has stopped moving. (Because of
32655 the way Emacs compares mouse events, you will occasionally wait twice
32656 that time before the window gets selected.)
32657 Any other value means to autoselect window instantaneously when the
32658 mouse pointer enters it.
32660 Autoselection selects the minibuffer only if it is active, and never
32661 unselects the minibuffer if it is active.
32663 When customizing this variable make sure that the actual value of
32664 `focus-follows-mouse' matches the behavior of your window manager. */);
32665 Vmouse_autoselect_window = Qnil;
32667 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32668 doc: /* Non-nil means automatically resize tool-bars.
32669 This dynamically changes the tool-bar's height to the minimum height
32670 that is needed to make all tool-bar items visible.
32671 If value is `grow-only', the tool-bar's height is only increased
32672 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32673 Vauto_resize_tool_bars = Qt;
32675 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32676 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32677 auto_raise_tool_bar_buttons_p = true;
32679 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32680 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32681 make_cursor_line_fully_visible_p = true;
32683 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32684 doc: /* Border below tool-bar in pixels.
32685 If an integer, use it as the height of the border.
32686 If it is one of `internal-border-width' or `border-width', use the
32687 value of the corresponding frame parameter.
32688 Otherwise, no border is added below the tool-bar. */);
32689 Vtool_bar_border = Qinternal_border_width;
32691 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32692 doc: /* Margin around tool-bar buttons in pixels.
32693 If an integer, use that for both horizontal and vertical margins.
32694 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32695 HORZ specifying the horizontal margin, and VERT specifying the
32696 vertical margin. */);
32697 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32699 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32700 doc: /* Relief thickness of tool-bar buttons. */);
32701 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32703 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32704 doc: /* Tool bar style to use.
32705 It can be one of
32706 image - show images only
32707 text - show text only
32708 both - show both, text below image
32709 both-horiz - show text to the right of the image
32710 text-image-horiz - show text to the left of the image
32711 any other - use system default or image if no system default.
32713 This variable only affects the GTK+ toolkit version of Emacs. */);
32714 Vtool_bar_style = Qnil;
32716 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32717 doc: /* Maximum number of characters a label can have to be shown.
32718 The tool bar style must also show labels for this to have any effect, see
32719 `tool-bar-style'. */);
32720 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32722 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32723 doc: /* List of functions to call to fontify regions of text.
32724 Each function is called with one argument POS. Functions must
32725 fontify a region starting at POS in the current buffer, and give
32726 fontified regions the property `fontified'. */);
32727 Vfontification_functions = Qnil;
32728 Fmake_variable_buffer_local (Qfontification_functions);
32730 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32731 unibyte_display_via_language_environment,
32732 doc: /* Non-nil means display unibyte text according to language environment.
32733 Specifically, this means that raw bytes in the range 160-255 decimal
32734 are displayed by converting them to the equivalent multibyte characters
32735 according to the current language environment. As a result, they are
32736 displayed according to the current fontset.
32738 Note that this variable affects only how these bytes are displayed,
32739 but does not change the fact they are interpreted as raw bytes. */);
32740 unibyte_display_via_language_environment = false;
32742 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32743 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32744 If a float, it specifies a fraction of the mini-window frame's height.
32745 If an integer, it specifies a number of lines. */);
32746 Vmax_mini_window_height = make_float (0.25);
32748 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32749 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32750 A value of nil means don't automatically resize mini-windows.
32751 A value of t means resize them to fit the text displayed in them.
32752 A value of `grow-only', the default, means let mini-windows grow only;
32753 they return to their normal size when the minibuffer is closed, or the
32754 echo area becomes empty. */);
32755 /* Contrary to the doc string, we initialize this to nil, so that
32756 loading loadup.el won't try to resize windows before loading
32757 window.el, where some functions we need to call for this live.
32758 We assign the 'grow-only' value right after loading window.el
32759 during loadup. */
32760 Vresize_mini_windows = Qnil;
32762 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32763 doc: /* Alist specifying how to blink the cursor off.
32764 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32765 `cursor-type' frame-parameter or variable equals ON-STATE,
32766 comparing using `equal', Emacs uses OFF-STATE to specify
32767 how to blink it off. ON-STATE and OFF-STATE are values for
32768 the `cursor-type' frame parameter.
32770 If a frame's ON-STATE has no entry in this list,
32771 the frame's other specifications determine how to blink the cursor off. */);
32772 Vblink_cursor_alist = Qnil;
32774 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32775 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32776 The value `current-line' means the line displaying point in each window
32777 is automatically scrolled horizontally to make point visible.
32778 Any other non-nil value means all the lines in a window are automatically
32779 scrolled horizontally to make point visible. */);
32780 automatic_hscrolling = Qt;
32781 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32782 DEFSYM (Qcurrent_line, "current-line");
32784 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32785 doc: /* How many columns away from the window edge point is allowed to get
32786 before automatic hscrolling will horizontally scroll the window. */);
32787 hscroll_margin = 5;
32789 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32790 doc: /* How many columns to scroll the window when point gets too close to the edge.
32791 When point is less than `hscroll-margin' columns from the window
32792 edge, automatic hscrolling will scroll the window by the amount of columns
32793 determined by this variable. If its value is a positive integer, scroll that
32794 many columns. If it's a positive floating-point number, it specifies the
32795 fraction of the window's width to scroll. If it's nil or zero, point will be
32796 centered horizontally after the scroll. Any other value, including negative
32797 numbers, are treated as if the value were zero.
32799 Automatic hscrolling always moves point outside the scroll margin, so if
32800 point was more than scroll step columns inside the margin, the window will
32801 scroll more than the value given by the scroll step.
32803 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32804 and `scroll-right' overrides this variable's effect. */);
32805 Vhscroll_step = make_number (0);
32807 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32808 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32809 Bind this around calls to `message' to let it take effect. */);
32810 message_truncate_lines = false;
32812 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32813 doc: /* Normal hook run to update the menu bar definitions.
32814 Redisplay runs this hook before it redisplays the menu bar.
32815 This is used to update menus such as Buffers, whose contents depend on
32816 various data. */);
32817 Vmenu_bar_update_hook = Qnil;
32819 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32820 doc: /* Frame for which we are updating a menu.
32821 The enable predicate for a menu binding should check this variable. */);
32822 Vmenu_updating_frame = Qnil;
32824 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32825 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32826 inhibit_menubar_update = false;
32828 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32829 doc: /* Prefix prepended to all continuation lines at display time.
32830 The value may be a string, an image, or a stretch-glyph; it is
32831 interpreted in the same way as the value of a `display' text property.
32833 This variable is overridden by any `wrap-prefix' text or overlay
32834 property.
32836 To add a prefix to non-continuation lines, use `line-prefix'. */);
32837 Vwrap_prefix = Qnil;
32838 DEFSYM (Qwrap_prefix, "wrap-prefix");
32839 Fmake_variable_buffer_local (Qwrap_prefix);
32841 DEFVAR_LISP ("line-prefix", Vline_prefix,
32842 doc: /* Prefix prepended to all non-continuation lines at display time.
32843 The value may be a string, an image, or a stretch-glyph; it is
32844 interpreted in the same way as the value of a `display' text property.
32846 This variable is overridden by any `line-prefix' text or overlay
32847 property.
32849 To add a prefix to continuation lines, use `wrap-prefix'. */);
32850 Vline_prefix = Qnil;
32851 DEFSYM (Qline_prefix, "line-prefix");
32852 Fmake_variable_buffer_local (Qline_prefix);
32854 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32855 doc: /* Non-nil means display line numbers.
32856 If the value is t, display the absolute number of each line of a buffer
32857 shown in a window. Absolute line numbers count from the beginning of
32858 the current narrowing, or from buffer beginning. If the value is
32859 `relative', display for each line not containing the window's point its
32860 relative number instead, i.e. the number of the line relative to the
32861 line showing the window's point.
32863 In either case, line numbers are displayed at the beginning of each
32864 non-continuation line that displays buffer text, i.e. after each newline
32865 character that comes from the buffer. The value `visual' is like
32866 `relative' but counts screen lines instead of buffer lines. In practice
32867 this means that continuation lines count as well when calculating the
32868 relative number of a line.
32870 Lisp programs can disable display of a line number of a particular
32871 buffer line by putting the `display-line-numbers-disable' text property
32872 or overlay property on the first visible character of that line. */);
32873 Vdisplay_line_numbers = Qnil;
32874 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32875 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32876 DEFSYM (Qrelative, "relative");
32877 DEFSYM (Qvisual, "visual");
32879 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32880 doc: /* Minimum width of space reserved for line number display.
32881 A positive number means reserve that many columns for line numbers,
32882 even if the actual number needs less space.
32883 The default value of nil means compute the space dynamically.
32884 Any other value is treated as nil. */);
32885 Vdisplay_line_numbers_width = Qnil;
32886 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32887 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32889 DEFVAR_LISP ("display-line-numbers-current-absolute",
32890 Vdisplay_line_numbers_current_absolute,
32891 doc: /* Non-nil means display absolute number of current line.
32892 This variable has effect only when `display-line-numbers' is
32893 either `relative' or `visual'. */);
32894 Vdisplay_line_numbers_current_absolute = Qt;
32896 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32897 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32898 display_line_numbers_widen = false;
32899 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32900 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32902 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32903 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32904 inhibit_eval_during_redisplay = false;
32906 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32907 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32908 inhibit_free_realized_faces = false;
32910 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32911 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32912 Intended for use during debugging and for testing bidi display;
32913 see biditest.el in the test suite. */);
32914 inhibit_bidi_mirroring = false;
32916 #ifdef GLYPH_DEBUG
32917 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32918 doc: /* Inhibit try_window_id display optimization. */);
32919 inhibit_try_window_id = false;
32921 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32922 doc: /* Inhibit try_window_reusing display optimization. */);
32923 inhibit_try_window_reusing = false;
32925 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32926 doc: /* Inhibit try_cursor_movement display optimization. */);
32927 inhibit_try_cursor_movement = false;
32928 #endif /* GLYPH_DEBUG */
32930 DEFVAR_INT ("overline-margin", overline_margin,
32931 doc: /* Space between overline and text, in pixels.
32932 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32933 margin to the character height. */);
32934 overline_margin = 2;
32936 DEFVAR_INT ("underline-minimum-offset",
32937 underline_minimum_offset,
32938 doc: /* Minimum distance between baseline and underline.
32939 This can improve legibility of underlined text at small font sizes,
32940 particularly when using variable `x-use-underline-position-properties'
32941 with fonts that specify an UNDERLINE_POSITION relatively close to the
32942 baseline. The default value is 1. */);
32943 underline_minimum_offset = 1;
32945 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32946 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32947 This feature only works when on a window system that can change
32948 cursor shapes. */);
32949 display_hourglass_p = true;
32951 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32952 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32953 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32955 #ifdef HAVE_WINDOW_SYSTEM
32956 hourglass_atimer = NULL;
32957 hourglass_shown_p = false;
32958 #endif /* HAVE_WINDOW_SYSTEM */
32960 /* Name of the face used to display glyphless characters. */
32961 DEFSYM (Qglyphless_char, "glyphless-char");
32963 /* Method symbols for Vglyphless_char_display. */
32964 DEFSYM (Qhex_code, "hex-code");
32965 DEFSYM (Qempty_box, "empty-box");
32966 DEFSYM (Qthin_space, "thin-space");
32967 DEFSYM (Qzero_width, "zero-width");
32969 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32970 doc: /* Function run just before redisplay.
32971 It is called with one argument, which is the set of windows that are to
32972 be redisplayed. This set can be nil (meaning, only the selected window),
32973 or t (meaning all windows). */);
32974 Vpre_redisplay_function = intern ("ignore");
32976 /* Symbol for the purpose of Vglyphless_char_display. */
32977 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32978 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32980 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32981 doc: /* Char-table defining glyphless characters.
32982 Each element, if non-nil, should be one of the following:
32983 an ASCII acronym string: display this string in a box
32984 `hex-code': display the hexadecimal code of a character in a box
32985 `empty-box': display as an empty box
32986 `thin-space': display as 1-pixel width space
32987 `zero-width': don't display
32988 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32989 display method for graphical terminals and text terminals respectively.
32990 GRAPHICAL and TEXT should each have one of the values listed above.
32992 The char-table has one extra slot to control the display of a character for
32993 which no font is found. This slot only takes effect on graphical terminals.
32994 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32995 `thin-space'. The default is `empty-box'.
32997 If a character has a non-nil entry in an active display table, the
32998 display table takes effect; in this case, Emacs does not consult
32999 `glyphless-char-display' at all. */);
33000 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33001 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33002 Qempty_box);
33004 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33005 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33006 Vdebug_on_message = Qnil;
33008 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33009 doc: /* */);
33010 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33012 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33013 doc: /* */);
33014 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33016 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33017 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33018 /* Initialize to t, since we need to disable reordering until
33019 loadup.el successfully loads charprop.el. */
33020 redisplay__inhibit_bidi = true;
33022 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33023 doc: /* Non-nil means display raw bytes in hexadecimal format.
33024 The default is to use octal format (\200) whereas hexadecimal (\x80)
33025 may be more familiar to users. */);
33026 display_raw_bytes_as_hex = false;
33031 /* Initialize this module when Emacs starts. */
33033 void
33034 init_xdisp (void)
33036 CHARPOS (this_line_start_pos) = 0;
33038 if (!noninteractive)
33040 struct window *m = XWINDOW (minibuf_window);
33041 Lisp_Object frame = m->frame;
33042 struct frame *f = XFRAME (frame);
33043 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33044 struct window *r = XWINDOW (root);
33045 int i;
33047 echo_area_window = minibuf_window;
33049 r->top_line = FRAME_TOP_MARGIN (f);
33050 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33051 r->total_cols = FRAME_COLS (f);
33052 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33053 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33054 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33056 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33057 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33058 m->total_cols = FRAME_COLS (f);
33059 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33060 m->total_lines = 1;
33061 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33063 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33064 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33065 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33067 /* The default ellipsis glyphs `...'. */
33068 for (i = 0; i < 3; ++i)
33069 default_invis_vector[i] = make_number ('.');
33073 /* Allocate the buffer for frame titles.
33074 Also used for `format-mode-line'. */
33075 int size = 100;
33076 mode_line_noprop_buf = xmalloc (size);
33077 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33078 mode_line_noprop_ptr = mode_line_noprop_buf;
33079 mode_line_target = MODE_LINE_DISPLAY;
33082 help_echo_showing_p = false;
33085 #ifdef HAVE_WINDOW_SYSTEM
33087 /* Platform-independent portion of hourglass implementation. */
33089 /* Timer function of hourglass_atimer. */
33091 static void
33092 show_hourglass (struct atimer *timer)
33094 /* The timer implementation will cancel this timer automatically
33095 after this function has run. Set hourglass_atimer to null
33096 so that we know the timer doesn't have to be canceled. */
33097 hourglass_atimer = NULL;
33099 if (!hourglass_shown_p)
33101 Lisp_Object tail, frame;
33103 block_input ();
33105 FOR_EACH_FRAME (tail, frame)
33107 struct frame *f = XFRAME (frame);
33109 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33110 && FRAME_RIF (f)->show_hourglass)
33111 FRAME_RIF (f)->show_hourglass (f);
33114 hourglass_shown_p = true;
33115 unblock_input ();
33119 /* Cancel a currently active hourglass timer, and start a new one. */
33121 void
33122 start_hourglass (void)
33124 struct timespec delay;
33126 cancel_hourglass ();
33128 if (INTEGERP (Vhourglass_delay)
33129 && XINT (Vhourglass_delay) > 0)
33130 delay = make_timespec (min (XINT (Vhourglass_delay),
33131 TYPE_MAXIMUM (time_t)),
33133 else if (FLOATP (Vhourglass_delay)
33134 && XFLOAT_DATA (Vhourglass_delay) > 0)
33135 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33136 else
33137 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33139 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33140 show_hourglass, NULL);
33143 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33144 shown. */
33146 void
33147 cancel_hourglass (void)
33149 if (hourglass_atimer)
33151 cancel_atimer (hourglass_atimer);
33152 hourglass_atimer = NULL;
33155 if (hourglass_shown_p)
33157 Lisp_Object tail, frame;
33159 block_input ();
33161 FOR_EACH_FRAME (tail, frame)
33163 struct frame *f = XFRAME (frame);
33165 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33166 && FRAME_RIF (f)->hide_hourglass)
33167 FRAME_RIF (f)->hide_hourglass (f);
33168 #ifdef HAVE_NTGUI
33169 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33170 else if (!FRAME_W32_P (f))
33171 w32_arrow_cursor ();
33172 #endif
33175 hourglass_shown_p = false;
33176 unblock_input ();
33180 #endif /* HAVE_WINDOW_SYSTEM */