eshell-eval-using-options: Avoid compiler warning differently
[emacs.git] / src / xdisp.c
blob44eb1ebf059b6e3bad2c6f7d3bde3195ea480e7d
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.
39 Under window systems like X, some portions of the redisplay code
40 are also called asynchronously, due to mouse movement or expose
41 events. "Asynchronously" in this context means that any C function
42 which calls maybe_quit or process_pending_signals could enter
43 redisplay via expose_frame and/or note_mouse_highlight, if X events
44 were recently reported to Emacs about mouse movements or frame(s)
45 that were exposed. And such redisplay could invoke the Lisp
46 interpreter, e.g. via the :eval forms in mode-line-format, and as
47 result the global state could change. It is therefore very
48 important that C functions which might cause such "asynchronous"
49 redisplay, but cannot tolerate the results, use
50 block_input/unblock_input around code fragments which assume that
51 global Lisp state doesn't change. If you don't follow this rule,
52 you will encounter bugs which are very hard to explain. One place
53 that needs to take such precautions is timer_check, some of whose
54 code cannot tolerate changes in timer alists while it processes
55 timers.
57 +--------------+ redisplay +----------------+
58 | Lisp machine |---------------->| Redisplay code |<--+
59 +--------------+ (xdisp.c) +----------------+ |
60 ^ | |
61 +----------------------------------+ |
62 Block input to prevent this when |
63 called asynchronously! |
65 note_mouse_highlight (asynchronous) |
67 X mouse events -----+
69 expose_frame (asynchronous) |
71 X expose events -----+
73 What does redisplay do? Obviously, it has to figure out somehow what
74 has been changed since the last time the display has been updated,
75 and to make these changes visible. Preferably it would do that in
76 a moderately intelligent way, i.e. fast.
78 Changes in buffer text can be deduced from window and buffer
79 structures, and from some global variables like `beg_unchanged' and
80 `end_unchanged'. The contents of the display are additionally
81 recorded in a `glyph matrix', a two-dimensional matrix of glyph
82 structures. Each row in such a matrix corresponds to a line on the
83 display, and each glyph in a row corresponds to a column displaying
84 a character, an image, or what else. This matrix is called the
85 `current glyph matrix' or `current matrix' in redisplay
86 terminology.
88 For buffer parts that have been changed since the last update, a
89 second glyph matrix is constructed, the so called `desired glyph
90 matrix' or short `desired matrix'. Current and desired matrix are
91 then compared to find a cheap way to update the display, e.g. by
92 reusing part of the display by scrolling lines.
94 You will find a lot of redisplay optimizations when you start
95 looking at the innards of redisplay. The overall goal of all these
96 optimizations is to make redisplay fast because it is done
97 frequently. Some of these optimizations are implemented by the
98 following functions:
100 . try_cursor_movement
102 This function tries to update the display if the text in the
103 window did not change and did not scroll, only point moved, and
104 it did not move off the displayed portion of the text.
106 . try_window_reusing_current_matrix
108 This function reuses the current matrix of a window when text
109 has not changed, but the window start changed (e.g., due to
110 scrolling).
112 . try_window_id
114 This function attempts to redisplay a window by reusing parts of
115 its existing display. It finds and reuses the part that was not
116 changed, and redraws the rest. (The "id" part in the function's
117 name stands for "insert/delete", not for "identification" or
118 somesuch.)
120 . try_window
122 This function performs the full redisplay of a single window
123 assuming that its fonts were not changed and that the cursor
124 will not end up in the scroll margins. (Loading fonts requires
125 re-adjustment of dimensions of glyph matrices, which makes this
126 method impossible to use.)
128 These optimizations are tried in sequence (some can be skipped if
129 it is known that they are not applicable). If none of the
130 optimizations were successful, redisplay calls redisplay_windows,
131 which performs a full redisplay of all windows.
133 Note that there's one more important optimization up Emacs's
134 sleeve, but it is related to actually redrawing the potentially
135 changed portions of the window/frame, not to reproducing the
136 desired matrices of those potentially changed portions. Namely,
137 the function update_frame and its subroutines, which you will find
138 in dispnew.c, compare the desired matrices with the current
139 matrices, and only redraw the portions that changed. So it could
140 happen that the functions in this file for some reason decide that
141 the entire desired matrix needs to be regenerated from scratch, and
142 still only parts of the Emacs display, or even nothing at all, will
143 be actually delivered to the glass, because update_frame has found
144 that the new and the old screen contents are similar or identical.
146 Desired matrices.
148 Desired matrices are always built per Emacs window. The function
149 `display_line' is the central function to look at if you are
150 interested. It constructs one row in a desired matrix given an
151 iterator structure containing both a buffer position and a
152 description of the environment in which the text is to be
153 displayed. But this is too early, read on.
155 Characters and pixmaps displayed for a range of buffer text depend
156 on various settings of buffers and windows, on overlays and text
157 properties, on display tables, on selective display. The good news
158 is that all this hairy stuff is hidden behind a small set of
159 interface functions taking an iterator structure (struct it)
160 argument.
162 Iteration over things to be displayed is then simple. It is
163 started by initializing an iterator with a call to init_iterator,
164 passing it the buffer position where to start iteration. For
165 iteration over strings, pass -1 as the position to init_iterator,
166 and call reseat_to_string when the string is ready, to initialize
167 the iterator for that string. Thereafter, calls to
168 get_next_display_element fill the iterator structure with relevant
169 information about the next thing to display. Calls to
170 set_iterator_to_next move the iterator to the next thing.
172 Besides this, an iterator also contains information about the
173 display environment in which glyphs for display elements are to be
174 produced. It has fields for the width and height of the display,
175 the information whether long lines are truncated or continued, a
176 current X and Y position, and lots of other stuff you can better
177 see in dispextern.h.
179 Glyphs in a desired matrix are normally constructed in a loop
180 calling get_next_display_element and then PRODUCE_GLYPHS. The call
181 to PRODUCE_GLYPHS will fill the iterator structure with pixel
182 information about the element being displayed and at the same time
183 produce glyphs for it. If the display element fits on the line
184 being displayed, set_iterator_to_next is called next, otherwise the
185 glyphs produced are discarded. The function display_line is the
186 workhorse of filling glyph rows in the desired matrix with glyphs.
187 In addition to producing glyphs, it also handles line truncation
188 and continuation, word wrap, and cursor positioning (for the
189 latter, see also set_cursor_from_row).
191 Frame matrices.
193 That just couldn't be all, could it? What about terminal types not
194 supporting operations on sub-windows of the screen? To update the
195 display on such a terminal, window-based glyph matrices are not
196 well suited. To be able to reuse part of the display (scrolling
197 lines up and down), we must instead have a view of the whole
198 screen. This is what `frame matrices' are for. They are a trick.
200 Frames on terminals like above have a glyph pool. Windows on such
201 a frame sub-allocate their glyph memory from their frame's glyph
202 pool. The frame itself is given its own glyph matrices. By
203 coincidence---or maybe something else---rows in window glyph
204 matrices are slices of corresponding rows in frame matrices. Thus
205 writing to window matrices implicitly updates a frame matrix which
206 provides us with the view of the whole screen that we originally
207 wanted to have without having to move many bytes around. To be
208 honest, there is a little bit more done, but not much more. If you
209 plan to extend that code, take a look at dispnew.c. The function
210 build_frame_matrix is a good starting point.
212 Bidirectional display.
214 Bidirectional display adds quite some hair to this already complex
215 design. The good news are that a large portion of that hairy stuff
216 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
217 reordering engine which is called by set_iterator_to_next and
218 returns the next character to display in the visual order. See
219 commentary on bidi.c for more details. As far as redisplay is
220 concerned, the effect of calling bidi_move_to_visually_next, the
221 main interface of the reordering engine, is that the iterator gets
222 magically placed on the buffer or string position that is to be
223 displayed next. In other words, a linear iteration through the
224 buffer/string is replaced with a non-linear one. All the rest of
225 the redisplay is oblivious to the bidi reordering.
227 Well, almost oblivious---there are still complications, most of
228 them due to the fact that buffer and string positions no longer
229 change monotonously with glyph indices in a glyph row. Moreover,
230 for continued lines, the buffer positions may not even be
231 monotonously changing with vertical positions. Also, accounting
232 for face changes, overlays, etc. becomes more complex because
233 non-linear iteration could potentially skip many positions with
234 changes, and then cross them again on the way back...
236 One other prominent effect of bidirectional display is that some
237 paragraphs of text need to be displayed starting at the right
238 margin of the window---the so-called right-to-left, or R2L
239 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
240 which have their reversed_p flag set. The bidi reordering engine
241 produces characters in such rows starting from the character which
242 should be the rightmost on display. PRODUCE_GLYPHS then reverses
243 the order, when it fills up the glyph row whose reversed_p flag is
244 set, by prepending each new glyph to what is already there, instead
245 of appending it. When the glyph row is complete, the function
246 extend_face_to_end_of_line fills the empty space to the left of the
247 leftmost character with special glyphs, which will display as,
248 well, empty. On text terminals, these special glyphs are simply
249 blank characters. On graphics terminals, there's a single stretch
250 glyph of a suitably computed width. Both the blanks and the
251 stretch glyph are given the face of the background of the line.
252 This way, the terminal-specific back-end can still draw the glyphs
253 left to right, even for R2L lines.
255 Bidirectional display and character compositions
257 Some scripts cannot be displayed by drawing each character
258 individually, because adjacent characters change each other's shape
259 on display. For example, Arabic and Indic scripts belong to this
260 category.
262 Emacs display supports this by providing "character compositions",
263 most of which is implemented in composite.c. During the buffer
264 scan that delivers characters to PRODUCE_GLYPHS, if the next
265 character to be delivered is a composed character, the iteration
266 calls composition_reseat_it and next_element_from_composition. If
267 they succeed to compose the character with one or more of the
268 following characters, the whole sequence of characters that where
269 composed is recorded in the `struct composition_it' object that is
270 part of the buffer iterator. The composed sequence could produce
271 one or more font glyphs (called "grapheme clusters") on the screen.
272 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
273 in the direction corresponding to the current bidi scan direction
274 (recorded in the scan_dir member of the `struct bidi_it' object
275 that is part of the buffer iterator). In particular, if the bidi
276 iterator currently scans the buffer backwards, the grapheme
277 clusters are delivered back to front. This reorders the grapheme
278 clusters as appropriate for the current bidi context. Note that
279 this means that the grapheme clusters are always stored in the
280 LGSTRING object (see composite.c) in the logical order.
282 Moving an iterator in bidirectional text
283 without producing glyphs
285 Note one important detail mentioned above: that the bidi reordering
286 engine, driven by the iterator, produces characters in R2L rows
287 starting at the character that will be the rightmost on display.
288 As far as the iterator is concerned, the geometry of such rows is
289 still left to right, i.e. the iterator "thinks" the first character
290 is at the leftmost pixel position. The iterator does not know that
291 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
292 delivers. This is important when functions from the move_it_*
293 family are used to get to certain screen position or to match
294 screen coordinates with buffer coordinates: these functions use the
295 iterator geometry, which is left to right even in R2L paragraphs.
296 This works well with most callers of move_it_*, because they need
297 to get to a specific column, and columns are still numbered in the
298 reading order, i.e. the rightmost character in a R2L paragraph is
299 still column zero. But some callers do not get well with this; a
300 notable example is mouse clicks that need to find the character
301 that corresponds to certain pixel coordinates. See
302 buffer_posn_from_coords in dispnew.c for how this is handled. */
304 #include <config.h>
305 #include <stdio.h>
306 #include <stdlib.h>
307 #include <limits.h>
308 #include <math.h>
310 #include "lisp.h"
311 #include "atimer.h"
312 #include "composite.h"
313 #include "keyboard.h"
314 #include "systime.h"
315 #include "frame.h"
316 #include "window.h"
317 #include "termchar.h"
318 #include "dispextern.h"
319 #include "character.h"
320 #include "buffer.h"
321 #include "charset.h"
322 #include "indent.h"
323 #include "commands.h"
324 #include "keymap.h"
325 #include "disptab.h"
326 #include "termhooks.h"
327 #include "termopts.h"
328 #include "intervals.h"
329 #include "coding.h"
330 #include "region-cache.h"
331 #include "font.h"
332 #include "fontset.h"
333 #include "blockinput.h"
334 #include "xwidget.h"
335 #ifdef HAVE_WINDOW_SYSTEM
336 #include TERM_HEADER
337 #endif /* HAVE_WINDOW_SYSTEM */
339 #ifndef FRAME_X_OUTPUT
340 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
341 #endif
343 #define DISP_INFINITY 10000000
345 /* Holds the list (error). */
346 static Lisp_Object list_of_error;
348 #ifdef HAVE_WINDOW_SYSTEM
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P ((IT)->f) \
356 && ((IT)->bidi_it.paragraph_dir == R2L \
357 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
358 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
359 && (IT)->current_x == (IT)->last_visible_x)
361 #else /* !HAVE_WINDOW_SYSTEM */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
363 #endif /* HAVE_WINDOW_SYSTEM */
365 /* Test if the display element loaded in IT, or the underlying buffer
366 or string character, is a space or a TAB character. This is used
367 to determine where word wrapping can occur. */
369 #define IT_DISPLAYING_WHITESPACE(it) \
370 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
371 || ((STRINGP (it->string) \
372 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
373 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
374 || (it->s \
375 && (it->s[IT_BYTEPOS (*it)] == ' ' \
376 || it->s[IT_BYTEPOS (*it)] == '\t')) \
377 || (IT_BYTEPOS (*it) < ZV_BYTE \
378 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
379 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
381 /* True means print newline to stdout before next mini-buffer message. */
383 bool noninteractive_need_newline;
385 /* True means print newline to message log before next message. */
387 static bool message_log_need_newline;
389 /* Three markers that message_dolog uses.
390 It could allocate them itself, but that causes trouble
391 in handling memory-full errors. */
392 static Lisp_Object message_dolog_marker1;
393 static Lisp_Object message_dolog_marker2;
394 static Lisp_Object message_dolog_marker3;
396 /* The buffer position of the first character appearing entirely or
397 partially on the line of the selected window which contains the
398 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
399 redisplay optimization in redisplay_internal. */
401 static struct text_pos this_line_start_pos;
403 /* Number of characters past the end of the line above, including the
404 terminating newline. */
406 static struct text_pos this_line_end_pos;
408 /* The vertical positions and the height of this line. */
410 static int this_line_vpos;
411 static int this_line_y;
412 static int this_line_pixel_height;
414 /* X position at which this display line starts. Usually zero;
415 negative if first character is partially visible. */
417 static int this_line_start_x;
419 /* The smallest character position seen by move_it_* functions as they
420 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
421 hscrolled lines, see display_line. */
423 static struct text_pos this_line_min_pos;
425 /* Buffer that this_line_.* variables are referring to. */
427 static struct buffer *this_line_buffer;
429 /* True if an overlay arrow has been displayed in this window. */
431 static bool overlay_arrow_seen;
433 /* Vector containing glyphs for an ellipsis `...'. */
435 static Lisp_Object default_invis_vector[3];
437 /* This is the window where the echo area message was displayed. It
438 is always a mini-buffer window, but it may not be the same window
439 currently active as a mini-buffer. */
441 Lisp_Object echo_area_window;
443 /* Stack of messages, which are pushed by push_message and popped and
444 displayed by restore_message. */
446 static Lisp_Object Vmessage_stack;
448 /* True means multibyte characters were enabled when the echo area
449 message was specified. */
451 static bool message_enable_multibyte;
453 /* At each redisplay cycle, we should refresh everything there is to refresh.
454 To do that efficiently, we use many optimizations that try to make sure we
455 don't waste too much time updating things that haven't changed.
456 The coarsest such optimization is that, in the most common cases, we only
457 look at the selected-window.
459 To know whether other windows should be considered for redisplay, we use the
460 variable windows_or_buffers_changed: as long as it is 0, it means that we
461 have not noticed anything that should require updating anything else than
462 the selected-window. If it is set to REDISPLAY_SOME, it means that since
463 last redisplay, some changes have been made which could impact other
464 windows. To know which ones need redisplay, every buffer, window, and frame
465 has a `redisplay' bit, which (if true) means that this object needs to be
466 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
467 looking for those `redisplay' bits (actually, there might be some such bits
468 set, but then only on objects which aren't displayed anyway).
470 OTOH if it's non-zero we wil have to loop through all windows and then check
471 the `redisplay' bit of the corresponding window, frame, and buffer, in order
472 to decide whether that window needs attention or not. Note that we can't
473 just look at the frame's redisplay bit to decide that the whole frame can be
474 skipped, since even if the frame's redisplay bit is unset, some of its
475 windows's redisplay bits may be set.
477 Mostly for historical reasons, windows_or_buffers_changed can also take
478 other non-zero values. In that case, the precise value doesn't matter (it
479 encodes the cause of the setting but is only used for debugging purposes),
480 and what it means is that we shouldn't pay attention to any `redisplay' bits
481 and we should simply try and redisplay every window out there. */
483 int windows_or_buffers_changed;
485 /* Nonzero if we should redraw the mode lines on the next redisplay.
486 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
487 then only redisplay the mode lines in those buffers/windows/frames where the
488 `redisplay' bit has been set.
489 For any other value, redisplay all mode lines (the number used is then only
490 used to track down the cause for this full-redisplay).
492 Since the frame title uses the same %-constructs as the mode line
493 (except %c, %C, and %l), if this variable is non-zero, we also consider
494 redisplaying the title of each frame, see x_consider_frame_title.
496 The `redisplay' bits are the same as those used for
497 windows_or_buffers_changed, and setting windows_or_buffers_changed also
498 causes recomputation of the mode lines of all those windows. IOW this
499 variable only has an effect if windows_or_buffers_changed is zero, in which
500 case we should only need to redisplay the mode-line of those objects with
501 a `redisplay' bit set but not the window's text content (tho we may still
502 need to refresh the text content of the selected-window). */
504 int update_mode_lines;
506 /* True after display_mode_line if %l was used and it displayed a
507 line number. */
509 static bool line_number_displayed;
511 /* The name of the *Messages* buffer, a string. */
513 static Lisp_Object Vmessages_buffer_name;
515 /* Current, index 0, and last displayed echo area message. Either
516 buffers from echo_buffers, or nil to indicate no message. */
518 Lisp_Object echo_area_buffer[2];
520 /* The buffers referenced from echo_area_buffer. */
522 static Lisp_Object echo_buffer[2];
524 /* A vector saved used in with_area_buffer to reduce consing. */
526 static Lisp_Object Vwith_echo_area_save_vector;
528 /* True means display_echo_area should display the last echo area
529 message again. Set by redisplay_preserve_echo_area. */
531 static bool display_last_displayed_message_p;
533 /* True if echo area is being used by print; false if being used by
534 message. */
536 static bool message_buf_print;
538 /* Set to true in clear_message to make redisplay_internal aware
539 of an emptied echo area. */
541 static bool message_cleared_p;
543 /* A scratch glyph row with contents used for generating truncation
544 glyphs. Also used in direct_output_for_insert. */
546 #define MAX_SCRATCH_GLYPHS 100
547 static struct glyph_row scratch_glyph_row;
548 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
550 /* Ascent and height of the last line processed by move_it_to. */
552 static int last_height;
554 /* True if there's a help-echo in the echo area. */
556 bool help_echo_showing_p;
558 /* The maximum distance to look ahead for text properties. Values
559 that are too small let us call compute_char_face and similar
560 functions too often which is expensive. Values that are too large
561 let us call compute_char_face and alike too often because we
562 might not be interested in text properties that far away. */
564 #define TEXT_PROP_DISTANCE_LIMIT 100
566 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
567 iterator state and later restore it. This is needed because the
568 bidi iterator on bidi.c keeps a stacked cache of its states, which
569 is really a singleton. When we use scratch iterator objects to
570 move around the buffer, we can cause the bidi cache to be pushed or
571 popped, and therefore we need to restore the cache state when we
572 return to the original iterator. */
573 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
574 do { \
575 if (CACHE) \
576 bidi_unshelve_cache (CACHE, true); \
577 ITCOPY = ITORIG; \
578 CACHE = bidi_shelve_cache (); \
579 } while (false)
581 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
582 do { \
583 if (pITORIG != pITCOPY) \
584 *(pITORIG) = *(pITCOPY); \
585 bidi_unshelve_cache (CACHE, false); \
586 CACHE = NULL; \
587 } while (false)
589 /* Functions to mark elements as needing redisplay. */
590 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
592 void
593 redisplay_other_windows (void)
595 if (!windows_or_buffers_changed)
596 windows_or_buffers_changed = REDISPLAY_SOME;
599 void
600 wset_redisplay (struct window *w)
602 /* Beware: selected_window can be nil during early stages. */
603 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
604 redisplay_other_windows ();
605 w->redisplay = true;
608 void
609 fset_redisplay (struct frame *f)
611 redisplay_other_windows ();
612 f->redisplay = true;
615 void
616 bset_redisplay (struct buffer *b)
618 int count = buffer_window_count (b);
619 if (count > 0)
621 /* ... it's visible in other window than selected, */
622 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
623 redisplay_other_windows ();
624 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
625 so that if we later set windows_or_buffers_changed, this buffer will
626 not be omitted. */
627 b->text->redisplay = true;
631 void
632 bset_update_mode_line (struct buffer *b)
634 if (!update_mode_lines)
635 update_mode_lines = REDISPLAY_SOME;
636 b->text->redisplay = true;
639 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
640 Sset_buffer_redisplay, 4, 4, 0,
641 doc: /* Mark the current buffer for redisplay.
642 This function may be passed to `add-variable-watcher'. */)
643 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
645 bset_update_mode_line (current_buffer);
646 current_buffer->prevent_redisplay_optimizations_p = true;
647 return Qnil;
650 #ifdef GLYPH_DEBUG
652 /* True means print traces of redisplay if compiled with
653 GLYPH_DEBUG defined. */
655 bool trace_redisplay_p;
657 #endif /* GLYPH_DEBUG */
659 #ifdef DEBUG_TRACE_MOVE
660 /* True means trace with TRACE_MOVE to stderr. */
661 static bool trace_move;
663 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
664 #else
665 #define TRACE_MOVE(x) (void) 0
666 #endif
668 /* Buffer being redisplayed -- for redisplay_window_error. */
670 static struct buffer *displayed_buffer;
672 /* Value returned from text property handlers (see below). */
674 enum prop_handled
676 HANDLED_NORMALLY,
677 HANDLED_RECOMPUTE_PROPS,
678 HANDLED_OVERLAY_STRING_CONSUMED,
679 HANDLED_RETURN
682 /* A description of text properties that redisplay is interested
683 in. */
685 struct props
687 /* The symbol index of the name of the property. */
688 short name;
690 /* A unique index for the property. */
691 enum prop_idx idx;
693 /* A handler function called to set up iterator IT from the property
694 at IT's current position. Value is used to steer handle_stop. */
695 enum prop_handled (*handler) (struct it *it);
698 static enum prop_handled handle_face_prop (struct it *);
699 static enum prop_handled handle_invisible_prop (struct it *);
700 static enum prop_handled handle_display_prop (struct it *);
701 static enum prop_handled handle_composition_prop (struct it *);
702 static enum prop_handled handle_overlay_change (struct it *);
703 static enum prop_handled handle_fontified_prop (struct it *);
705 /* Properties handled by iterators. */
707 static struct props it_props[] =
709 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
710 /* Handle `face' before `display' because some sub-properties of
711 `display' need to know the face. */
712 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
713 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
714 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
715 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
716 {0, 0, NULL}
719 /* Value is the position described by X. If X is a marker, value is
720 the marker_position of X. Otherwise, value is X. */
722 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
724 /* Enumeration returned by some move_it_.* functions internally. */
726 enum move_it_result
728 /* Not used. Undefined value. */
729 MOVE_UNDEFINED,
731 /* Move ended at the requested buffer position or ZV. */
732 MOVE_POS_MATCH_OR_ZV,
734 /* Move ended at the requested X pixel position. */
735 MOVE_X_REACHED,
737 /* Move within a line ended at the end of a line that must be
738 continued. */
739 MOVE_LINE_CONTINUED,
741 /* Move within a line ended at the end of a line that would
742 be displayed truncated. */
743 MOVE_LINE_TRUNCATED,
745 /* Move within a line ended at a line end. */
746 MOVE_NEWLINE_OR_CR
749 /* This counter is used to clear the face cache every once in a while
750 in redisplay_internal. It is incremented for each redisplay.
751 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
752 cleared. */
754 #define CLEAR_FACE_CACHE_COUNT 500
755 static int clear_face_cache_count;
757 /* Similarly for the image cache. */
759 #ifdef HAVE_WINDOW_SYSTEM
760 #define CLEAR_IMAGE_CACHE_COUNT 101
761 static int clear_image_cache_count;
763 /* Null glyph slice */
764 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
765 #endif
767 /* True while redisplay_internal is in progress. */
769 bool redisplaying_p;
771 /* If a string, XTread_socket generates an event to display that string.
772 (The display is done in read_char.) */
774 Lisp_Object help_echo_string;
775 Lisp_Object help_echo_window;
776 Lisp_Object help_echo_object;
777 ptrdiff_t help_echo_pos;
779 /* Temporary variable for XTread_socket. */
781 Lisp_Object previous_help_echo_string;
783 /* Platform-independent portion of hourglass implementation. */
785 #ifdef HAVE_WINDOW_SYSTEM
787 /* True means an hourglass cursor is currently shown. */
788 static bool hourglass_shown_p;
790 /* If non-null, an asynchronous timer that, when it expires, displays
791 an hourglass cursor on all frames. */
792 static struct atimer *hourglass_atimer;
794 #endif /* HAVE_WINDOW_SYSTEM */
796 /* Default number of seconds to wait before displaying an hourglass
797 cursor. */
798 #define DEFAULT_HOURGLASS_DELAY 1
800 #ifdef HAVE_WINDOW_SYSTEM
802 /* Default pixel width of `thin-space' display method. */
803 #define THIN_SPACE_WIDTH 1
805 #endif /* HAVE_WINDOW_SYSTEM */
807 /* Function prototypes. */
809 static void setup_for_ellipsis (struct it *, int);
810 static void set_iterator_to_next (struct it *, bool);
811 static void mark_window_display_accurate_1 (struct window *, bool);
812 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
813 static bool cursor_row_p (struct glyph_row *);
814 static int redisplay_mode_lines (Lisp_Object, bool);
816 static void handle_line_prefix (struct it *);
818 static void handle_stop_backwards (struct it *, ptrdiff_t);
819 static void unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static bool current_message_1 (ptrdiff_t, Lisp_Object);
822 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
823 static void set_message (Lisp_Object);
824 static bool set_message_1 (ptrdiff_t, Lisp_Object);
825 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
826 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
827 static void unwind_redisplay (void);
828 static void extend_face_to_end_of_line (struct it *);
829 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
830 static void push_it (struct it *, struct text_pos *);
831 static void iterate_out_of_display_property (struct it *);
832 static void pop_it (struct it *);
833 static void redisplay_internal (void);
834 static void echo_area_display (bool);
835 static void block_buffer_flips (void);
836 static void unblock_buffer_flips (void);
837 static void redisplay_windows (Lisp_Object);
838 static void redisplay_window (Lisp_Object, bool);
839 static Lisp_Object redisplay_window_error (Lisp_Object);
840 static Lisp_Object redisplay_window_0 (Lisp_Object);
841 static Lisp_Object redisplay_window_1 (Lisp_Object);
842 static bool set_cursor_from_row (struct window *, struct glyph_row *,
843 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
844 int, int);
845 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
846 static bool update_menu_bar (struct frame *, bool, bool);
847 static bool try_window_reusing_current_matrix (struct window *);
848 static int try_window_id (struct window *);
849 static void maybe_produce_line_number (struct it *);
850 static bool should_produce_line_number (struct it *);
851 static bool display_line (struct it *, int);
852 static int display_mode_lines (struct window *);
853 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
854 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
855 Lisp_Object, bool);
856 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
857 Lisp_Object);
858 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
859 static void display_menu_bar (struct window *);
860 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
861 ptrdiff_t *);
862 static void pint2str (register char *, register int, register ptrdiff_t);
864 static int display_string (const char *, Lisp_Object, Lisp_Object,
865 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
866 static void compute_line_metrics (struct it *);
867 static void run_redisplay_end_trigger_hook (struct it *);
868 static bool get_overlay_strings (struct it *, ptrdiff_t);
869 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
870 static void next_overlay_string (struct it *);
871 static void reseat (struct it *, struct text_pos, bool);
872 static void reseat_1 (struct it *, struct text_pos, bool);
873 static bool next_element_from_display_vector (struct it *);
874 static bool next_element_from_string (struct it *);
875 static bool next_element_from_c_string (struct it *);
876 static bool next_element_from_buffer (struct it *);
877 static bool next_element_from_composition (struct it *);
878 static bool next_element_from_image (struct it *);
879 static bool next_element_from_stretch (struct it *);
880 static bool next_element_from_xwidget (struct it *);
881 static void load_overlay_strings (struct it *, ptrdiff_t);
882 static bool get_next_display_element (struct it *);
883 static enum move_it_result
884 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
885 enum move_operation_enum);
886 static void get_visually_first_element (struct it *);
887 static void compute_stop_pos (struct it *);
888 static int face_before_or_after_it_pos (struct it *, bool);
889 static ptrdiff_t next_overlay_change (ptrdiff_t);
890 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
891 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
892 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
893 Lisp_Object, struct text_pos *,
894 ptrdiff_t, int, bool, bool);
895 static int underlying_face_id (struct it *);
897 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
898 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
900 #ifdef HAVE_WINDOW_SYSTEM
902 static void update_tool_bar (struct frame *, bool);
903 static void x_draw_bottom_divider (struct window *w);
904 static void notice_overwritten_cursor (struct window *,
905 enum glyph_row_area,
906 int, int, int, int);
907 static int normal_char_height (struct font *, int);
908 static void normal_char_ascent_descent (struct font *, int, int *, int *);
910 static void append_stretch_glyph (struct it *, Lisp_Object,
911 int, int, int);
913 static Lisp_Object get_it_property (struct it *, Lisp_Object);
914 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
915 struct font *, int, bool);
917 #endif /* HAVE_WINDOW_SYSTEM */
919 static void produce_special_glyphs (struct it *, enum display_element_type);
920 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
921 static bool coords_in_mouse_face_p (struct window *, int, int);
925 /***********************************************************************
926 Window display dimensions
927 ***********************************************************************/
929 /* Return the bottom boundary y-position for text lines in window W.
930 This is the first y position at which a line cannot start.
931 It is relative to the top of the window.
933 This is the height of W minus the height of a mode line, if any. */
936 window_text_bottom_y (struct window *w)
938 int height = WINDOW_PIXEL_HEIGHT (w);
940 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
942 if (window_wants_mode_line (w))
943 height -= CURRENT_MODE_LINE_HEIGHT (w);
945 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
947 return height;
950 /* Return the pixel width of display area AREA of window W.
951 ANY_AREA means return the total width of W, not including
952 fringes to the left and right of the window. */
955 window_box_width (struct window *w, enum glyph_row_area area)
957 int width = w->pixel_width;
959 if (!w->pseudo_window_p)
961 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
962 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
964 if (area == TEXT_AREA)
965 width -= (WINDOW_MARGINS_WIDTH (w)
966 + WINDOW_FRINGES_WIDTH (w));
967 else if (area == LEFT_MARGIN_AREA)
968 width = WINDOW_LEFT_MARGIN_WIDTH (w);
969 else if (area == RIGHT_MARGIN_AREA)
970 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
973 /* With wide margins, fringes, etc. we might end up with a negative
974 width, correct that here. */
975 return max (0, width);
979 /* Return the pixel height of the display area of window W, not
980 including mode lines of W, if any. */
983 window_box_height (struct window *w)
985 struct frame *f = XFRAME (w->frame);
986 int height = WINDOW_PIXEL_HEIGHT (w);
988 eassert (height >= 0);
990 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
991 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
993 /* Note: the code below that determines the mode-line/header-line
994 height is essentially the same as that contained in the macro
995 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
996 the appropriate glyph row has its `mode_line_p' flag set,
997 and if it doesn't, uses estimate_mode_line_height instead. */
999 if (window_wants_mode_line (w))
1001 struct glyph_row *ml_row
1002 = (w->current_matrix && w->current_matrix->rows
1003 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1004 : 0);
1005 if (ml_row && ml_row->mode_line_p)
1006 height -= ml_row->height;
1007 else
1008 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1011 if (window_wants_header_line (w))
1013 struct glyph_row *hl_row
1014 = (w->current_matrix && w->current_matrix->rows
1015 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1016 : 0);
1017 if (hl_row && hl_row->mode_line_p)
1018 height -= hl_row->height;
1019 else
1020 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1023 /* With a very small font and a mode-line that's taller than
1024 default, we might end up with a negative height. */
1025 return max (0, height);
1028 /* Return the window-relative coordinate of the left edge of display
1029 area AREA of window W. ANY_AREA means return the left edge of the
1030 whole window, to the right of the left fringe of W. */
1033 window_box_left_offset (struct window *w, enum glyph_row_area area)
1035 int x;
1037 if (w->pseudo_window_p)
1038 return 0;
1040 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1042 if (area == TEXT_AREA)
1043 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1044 + window_box_width (w, LEFT_MARGIN_AREA));
1045 else if (area == RIGHT_MARGIN_AREA)
1046 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1047 + window_box_width (w, LEFT_MARGIN_AREA)
1048 + window_box_width (w, TEXT_AREA)
1049 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1051 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1052 else if (area == LEFT_MARGIN_AREA
1053 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1054 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1056 /* Don't return more than the window's pixel width. */
1057 return min (x, w->pixel_width);
1061 /* Return the window-relative coordinate of the right edge of display
1062 area AREA of window W. ANY_AREA means return the right edge of the
1063 whole window, to the left of the right fringe of W. */
1065 static int
1066 window_box_right_offset (struct window *w, enum glyph_row_area area)
1068 /* Don't return more than the window's pixel width. */
1069 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1070 w->pixel_width);
1073 /* Return the frame-relative coordinate of the left edge of display
1074 area AREA of window W. ANY_AREA means return the left edge of the
1075 whole window, to the right of the left fringe of W. */
1078 window_box_left (struct window *w, enum glyph_row_area area)
1080 struct frame *f = XFRAME (w->frame);
1081 int x;
1083 if (w->pseudo_window_p)
1084 return FRAME_INTERNAL_BORDER_WIDTH (f);
1086 x = (WINDOW_LEFT_EDGE_X (w)
1087 + window_box_left_offset (w, area));
1089 return x;
1093 /* Return the frame-relative coordinate of the right edge of display
1094 area AREA of window W. ANY_AREA means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1098 window_box_right (struct window *w, enum glyph_row_area area)
1100 return window_box_left (w, area) + window_box_width (w, area);
1103 /* Get the bounding box of the display area AREA of window W, without
1104 mode lines, in frame-relative coordinates. ANY_AREA means the
1105 whole window, not including the left and right fringes of
1106 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1107 coordinates of the upper-left corner of the box. Return in
1108 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1110 void
1111 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1112 int *box_y, int *box_width, int *box_height)
1114 if (box_width)
1115 *box_width = window_box_width (w, area);
1116 if (box_height)
1117 *box_height = window_box_height (w);
1118 if (box_x)
1119 *box_x = window_box_left (w, area);
1120 if (box_y)
1122 *box_y = WINDOW_TOP_EDGE_Y (w);
1123 if (window_wants_header_line (w))
1124 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1128 #ifdef HAVE_WINDOW_SYSTEM
1130 /* Get the bounding box of the display area AREA of window W, without
1131 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1132 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1133 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1134 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1135 box. */
1137 static void
1138 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1139 int *bottom_right_x, int *bottom_right_y)
1141 window_box (w, ANY_AREA, top_left_x, top_left_y,
1142 bottom_right_x, bottom_right_y);
1143 *bottom_right_x += *top_left_x;
1144 *bottom_right_y += *top_left_y;
1147 #endif /* HAVE_WINDOW_SYSTEM */
1149 /***********************************************************************
1150 Utilities
1151 ***********************************************************************/
1153 /* Return the bottom y-position of the line the iterator IT is in.
1154 This can modify IT's settings. */
1157 line_bottom_y (struct it *it)
1159 int line_height = it->max_ascent + it->max_descent;
1160 int line_top_y = it->current_y;
1162 if (line_height == 0)
1164 if (last_height)
1165 line_height = last_height;
1166 else if (IT_CHARPOS (*it) < ZV)
1168 move_it_by_lines (it, 1);
1169 line_height = (it->max_ascent || it->max_descent
1170 ? it->max_ascent + it->max_descent
1171 : last_height);
1173 else
1175 struct glyph_row *row = it->glyph_row;
1177 /* Use the default character height. */
1178 it->glyph_row = NULL;
1179 it->what = IT_CHARACTER;
1180 it->c = ' ';
1181 it->len = 1;
1182 PRODUCE_GLYPHS (it);
1183 line_height = it->ascent + it->descent;
1184 it->glyph_row = row;
1188 return line_top_y + line_height;
1191 DEFUN ("line-pixel-height", Fline_pixel_height,
1192 Sline_pixel_height, 0, 0, 0,
1193 doc: /* Return height in pixels of text line in the selected window.
1195 Value is the height in pixels of the line at point. */)
1196 (void)
1198 struct it it;
1199 struct text_pos pt;
1200 struct window *w = XWINDOW (selected_window);
1201 struct buffer *old_buffer = NULL;
1202 Lisp_Object result;
1204 if (XBUFFER (w->contents) != current_buffer)
1206 old_buffer = current_buffer;
1207 set_buffer_internal_1 (XBUFFER (w->contents));
1209 SET_TEXT_POS (pt, PT, PT_BYTE);
1210 start_display (&it, w, pt);
1211 /* Start from the beginning of the screen line, to make sure we
1212 traverse all of its display elements, and thus capture the
1213 correct metrics. */
1214 move_it_by_lines (&it, 0);
1215 it.vpos = it.current_y = 0;
1216 last_height = 0;
1217 result = make_number (line_bottom_y (&it));
1218 if (old_buffer)
1219 set_buffer_internal_1 (old_buffer);
1221 return result;
1224 /* Return the default pixel height of text lines in window W. The
1225 value is the canonical height of the W frame's default font, plus
1226 any extra space required by the line-spacing variable or frame
1227 parameter.
1229 Implementation note: this ignores any line-spacing text properties
1230 put on the newline characters. This is because those properties
1231 only affect the _screen_ line ending in the newline (i.e., in a
1232 continued line, only the last screen line will be affected), which
1233 means only a small number of lines in a buffer can ever use this
1234 feature. Since this function is used to compute the default pixel
1235 equivalent of text lines in a window, we can safely ignore those
1236 few lines. For the same reasons, we ignore the line-height
1237 properties. */
1239 default_line_pixel_height (struct window *w)
1241 struct frame *f = WINDOW_XFRAME (w);
1242 int height = FRAME_LINE_HEIGHT (f);
1244 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1246 struct buffer *b = XBUFFER (w->contents);
1247 Lisp_Object val = BVAR (b, extra_line_spacing);
1249 if (NILP (val))
1250 val = BVAR (&buffer_defaults, extra_line_spacing);
1251 if (!NILP (val))
1253 if (RANGED_INTEGERP (0, val, INT_MAX))
1254 height += XFASTINT (val);
1255 else if (FLOATP (val))
1257 int addon = XFLOAT_DATA (val) * height + 0.5;
1259 if (addon >= 0)
1260 height += addon;
1263 else
1264 height += f->extra_line_spacing;
1267 return height;
1270 /* Subroutine of pos_visible_p below. Extracts a display string, if
1271 any, from the display spec given as its argument. */
1272 static Lisp_Object
1273 string_from_display_spec (Lisp_Object spec)
1275 if (VECTORP (spec))
1277 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1278 if (STRINGP (AREF (spec, i)))
1279 return AREF (spec, i);
1281 else
1283 for (; CONSP (spec); spec = XCDR (spec))
1284 if (STRINGP (XCAR (spec)))
1285 return XCAR (spec);
1287 return spec;
1291 /* Limit insanely large values of W->hscroll on frame F to the largest
1292 value that will still prevent first_visible_x and last_visible_x of
1293 'struct it' from overflowing an int. */
1294 static int
1295 window_hscroll_limited (struct window *w, struct frame *f)
1297 ptrdiff_t window_hscroll = w->hscroll;
1298 int window_text_width = window_box_width (w, TEXT_AREA);
1299 int colwidth = FRAME_COLUMN_WIDTH (f);
1301 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1302 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1304 return window_hscroll;
1307 /* Return true if position CHARPOS is visible in window W.
1308 CHARPOS < 0 means return info about WINDOW_END position.
1309 If visible, set *X and *Y to pixel coordinates of top left corner.
1310 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1311 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1313 bool
1314 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1315 int *rtop, int *rbot, int *rowh, int *vpos)
1317 struct it it;
1318 void *itdata = bidi_shelve_cache ();
1319 struct text_pos top;
1320 bool visible_p = false;
1321 struct buffer *old_buffer = NULL;
1322 bool r2l = false;
1324 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1325 return visible_p;
1327 if (XBUFFER (w->contents) != current_buffer)
1329 old_buffer = current_buffer;
1330 set_buffer_internal_1 (XBUFFER (w->contents));
1333 SET_TEXT_POS_FROM_MARKER (top, w->start);
1334 /* Scrolling a minibuffer window via scroll bar when the echo area
1335 shows long text sometimes resets the minibuffer contents behind
1336 our backs. Also, someone might narrow-to-region and immediately
1337 call a scroll function. */
1338 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1339 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1341 /* If the top of the window is after CHARPOS, the latter is surely
1342 not visible. */
1343 if (charpos >= 0 && CHARPOS (top) > charpos)
1344 return visible_p;
1346 /* Some Lisp hook could call us in the middle of redisplaying this
1347 very window. If, by some bad luck, we are retrying redisplay
1348 because we found that the mode-line height and/or header-line
1349 height needs to be updated, the assignment of mode_line_height
1350 and header_line_height below could disrupt that, due to the
1351 selected/nonselected window dance during mode-line display, and
1352 we could infloop. Avoid that. */
1353 int prev_mode_line_height = w->mode_line_height;
1354 int prev_header_line_height = w->header_line_height;
1355 /* Compute exact mode line heights. */
1356 if (window_wants_mode_line (w))
1358 Lisp_Object window_mode_line_format
1359 = window_parameter (w, Qmode_line_format);
1361 w->mode_line_height
1362 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1363 NILP (window_mode_line_format)
1364 ? BVAR (current_buffer, mode_line_format)
1365 : window_mode_line_format);
1368 if (window_wants_header_line (w))
1370 Lisp_Object window_header_line_format
1371 = window_parameter (w, Qheader_line_format);
1373 w->header_line_height
1374 = display_mode_line (w, HEADER_LINE_FACE_ID,
1375 NILP (window_header_line_format)
1376 ? BVAR (current_buffer, header_line_format)
1377 : window_header_line_format);
1380 start_display (&it, w, top);
1381 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1382 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1384 if (charpos >= 0
1385 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1386 && IT_CHARPOS (it) >= charpos)
1387 /* When scanning backwards under bidi iteration, move_it_to
1388 stops at or _before_ CHARPOS, because it stops at or to
1389 the _right_ of the character at CHARPOS. */
1390 || (it.bidi_p && it.bidi_it.scan_dir == -1
1391 && IT_CHARPOS (it) <= charpos)))
1393 /* We have reached CHARPOS, or passed it. How the call to
1394 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1395 or covered by a display property, move_it_to stops at the end
1396 of the invisible text, to the right of CHARPOS. (ii) If
1397 CHARPOS is in a display vector, move_it_to stops on its last
1398 glyph. */
1399 int top_x = it.current_x;
1400 int top_y = it.current_y;
1401 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1402 int bottom_y;
1403 struct it save_it;
1404 void *save_it_data = NULL;
1406 /* Calling line_bottom_y may change it.method, it.position, etc. */
1407 SAVE_IT (save_it, it, save_it_data);
1408 last_height = 0;
1409 bottom_y = line_bottom_y (&it);
1410 if (top_y < window_top_y)
1411 visible_p = bottom_y > window_top_y;
1412 else if (top_y < it.last_visible_y)
1413 visible_p = true;
1414 if (bottom_y >= it.last_visible_y
1415 && it.bidi_p && it.bidi_it.scan_dir == -1
1416 && IT_CHARPOS (it) < charpos)
1418 /* When the last line of the window is scanned backwards
1419 under bidi iteration, we could be duped into thinking
1420 that we have passed CHARPOS, when in fact move_it_to
1421 simply stopped short of CHARPOS because it reached
1422 last_visible_y. To see if that's what happened, we call
1423 move_it_to again with a slightly larger vertical limit,
1424 and see if it actually moved vertically; if it did, we
1425 didn't really reach CHARPOS, which is beyond window end. */
1426 /* Why 10? because we don't know how many canonical lines
1427 will the height of the next line(s) be. So we guess. */
1428 int ten_more_lines = 10 * default_line_pixel_height (w);
1430 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1431 MOVE_TO_POS | MOVE_TO_Y);
1432 if (it.current_y > top_y)
1433 visible_p = false;
1436 RESTORE_IT (&it, &save_it, save_it_data);
1437 if (visible_p)
1439 if (it.method == GET_FROM_DISPLAY_VECTOR)
1441 /* We stopped on the last glyph of a display vector.
1442 Try and recompute. Hack alert! */
1443 if (charpos < 2 || top.charpos >= charpos)
1444 top_x = it.glyph_row->x;
1445 else
1447 struct it it2, it2_prev;
1448 /* The idea is to get to the previous buffer
1449 position, consume the character there, and use
1450 the pixel coordinates we get after that. But if
1451 the previous buffer position is also displayed
1452 from a display vector, we need to consume all of
1453 the glyphs from that display vector. */
1454 start_display (&it2, w, top);
1455 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1456 /* If we didn't get to CHARPOS - 1, there's some
1457 replacing display property at that position, and
1458 we stopped after it. That is exactly the place
1459 whose coordinates we want. */
1460 if (IT_CHARPOS (it2) != charpos - 1)
1461 it2_prev = it2;
1462 else
1464 /* Iterate until we get out of the display
1465 vector that displays the character at
1466 CHARPOS - 1. */
1467 do {
1468 get_next_display_element (&it2);
1469 PRODUCE_GLYPHS (&it2);
1470 it2_prev = it2;
1471 set_iterator_to_next (&it2, true);
1472 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1473 && IT_CHARPOS (it2) < charpos);
1475 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1476 || it2_prev.current_x > it2_prev.last_visible_x)
1477 top_x = it.glyph_row->x;
1478 else
1480 top_x = it2_prev.current_x;
1481 top_y = it2_prev.current_y;
1485 else if (IT_CHARPOS (it) != charpos)
1487 Lisp_Object cpos = make_number (charpos);
1488 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1489 Lisp_Object string = string_from_display_spec (spec);
1490 struct text_pos tpos;
1491 bool newline_in_string
1492 = (STRINGP (string)
1493 && memchr (SDATA (string), '\n', SBYTES (string)));
1495 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1496 bool replacing_spec_p
1497 = (!NILP (spec)
1498 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1499 charpos, FRAME_WINDOW_P (it.f)));
1500 /* The tricky code below is needed because there's a
1501 discrepancy between move_it_to and how we set cursor
1502 when PT is at the beginning of a portion of text
1503 covered by a display property or an overlay with a
1504 display property, or the display line ends in a
1505 newline from a display string. move_it_to will stop
1506 _after_ such display strings, whereas
1507 set_cursor_from_row conspires with cursor_row_p to
1508 place the cursor on the first glyph produced from the
1509 display string. */
1511 /* We have overshoot PT because it is covered by a
1512 display property that replaces the text it covers.
1513 If the string includes embedded newlines, we are also
1514 in the wrong display line. Backtrack to the correct
1515 line, where the display property begins. */
1516 if (replacing_spec_p)
1518 Lisp_Object startpos, endpos;
1519 EMACS_INT start, end;
1520 struct it it3;
1522 /* Find the first and the last buffer positions
1523 covered by the display string. */
1524 endpos =
1525 Fnext_single_char_property_change (cpos, Qdisplay,
1526 Qnil, Qnil);
1527 startpos =
1528 Fprevious_single_char_property_change (endpos, Qdisplay,
1529 Qnil, Qnil);
1530 start = XFASTINT (startpos);
1531 end = XFASTINT (endpos);
1532 /* Move to the last buffer position before the
1533 display property. */
1534 start_display (&it3, w, top);
1535 if (start > CHARPOS (top))
1536 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1537 /* Move forward one more line if the position before
1538 the display string is a newline or if it is the
1539 rightmost character on a line that is
1540 continued or word-wrapped. */
1541 if (it3.method == GET_FROM_BUFFER
1542 && (it3.c == '\n'
1543 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1544 move_it_by_lines (&it3, 1);
1545 else if (move_it_in_display_line_to (&it3, -1,
1546 it3.current_x
1547 + it3.pixel_width,
1548 MOVE_TO_X)
1549 == MOVE_LINE_CONTINUED)
1551 move_it_by_lines (&it3, 1);
1552 /* When we are under word-wrap, the #$@%!
1553 move_it_by_lines moves 2 lines, so we need to
1554 fix that up. */
1555 if (it3.line_wrap == WORD_WRAP)
1556 move_it_by_lines (&it3, -1);
1559 /* Record the vertical coordinate of the display
1560 line where we wound up. */
1561 top_y = it3.current_y;
1562 if (it3.bidi_p)
1564 /* When characters are reordered for display,
1565 the character displayed to the left of the
1566 display string could be _after_ the display
1567 property in the logical order. Use the
1568 smallest vertical position of these two. */
1569 start_display (&it3, w, top);
1570 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1571 if (it3.current_y < top_y)
1572 top_y = it3.current_y;
1574 /* Move from the top of the window to the beginning
1575 of the display line where the display string
1576 begins. */
1577 start_display (&it3, w, top);
1578 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1579 /* If it3_moved stays false after the 'while' loop
1580 below, that means we already were at a newline
1581 before the loop (e.g., the display string begins
1582 with a newline), so we don't need to (and cannot)
1583 inspect the glyphs of it3.glyph_row, because
1584 PRODUCE_GLYPHS will not produce anything for a
1585 newline, and thus it3.glyph_row stays at its
1586 stale content it got at top of the window. */
1587 bool it3_moved = false;
1588 /* Finally, advance the iterator until we hit the
1589 first display element whose character position is
1590 CHARPOS, or until the first newline from the
1591 display string, which signals the end of the
1592 display line. */
1593 while (get_next_display_element (&it3))
1595 PRODUCE_GLYPHS (&it3);
1596 if (IT_CHARPOS (it3) == charpos
1597 || ITERATOR_AT_END_OF_LINE_P (&it3))
1598 break;
1599 it3_moved = true;
1600 set_iterator_to_next (&it3, false);
1602 top_x = it3.current_x - it3.pixel_width;
1603 /* Normally, we would exit the above loop because we
1604 found the display element whose character
1605 position is CHARPOS. For the contingency that we
1606 didn't, and stopped at the first newline from the
1607 display string, move back over the glyphs
1608 produced from the string, until we find the
1609 rightmost glyph not from the string. */
1610 if (it3_moved
1611 && newline_in_string
1612 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1614 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1615 + it3.glyph_row->used[TEXT_AREA];
1617 while (EQ ((g - 1)->object, string))
1619 --g;
1620 top_x -= g->pixel_width;
1622 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1623 + it3.glyph_row->used[TEXT_AREA]);
1628 *x = top_x;
1629 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1630 *rtop = max (0, window_top_y - top_y);
1631 *rbot = max (0, bottom_y - it.last_visible_y);
1632 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1633 - max (top_y, window_top_y)));
1634 *vpos = it.vpos;
1635 if (it.bidi_it.paragraph_dir == R2L)
1636 r2l = true;
1639 else
1641 /* Either we were asked to provide info about WINDOW_END, or
1642 CHARPOS is in the partially visible glyph row at end of
1643 window. */
1644 struct it it2;
1645 void *it2data = NULL;
1647 SAVE_IT (it2, it, it2data);
1648 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1649 move_it_by_lines (&it, 1);
1650 if (charpos < IT_CHARPOS (it)
1651 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1653 visible_p = true;
1654 RESTORE_IT (&it2, &it2, it2data);
1655 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1656 *x = it2.current_x;
1657 *y = it2.current_y + it2.max_ascent - it2.ascent;
1658 *rtop = max (0, -it2.current_y);
1659 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1660 - it.last_visible_y));
1661 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1662 it.last_visible_y)
1663 - max (it2.current_y,
1664 WINDOW_HEADER_LINE_HEIGHT (w))));
1665 *vpos = it2.vpos;
1666 if (it2.bidi_it.paragraph_dir == R2L)
1667 r2l = true;
1669 else
1670 bidi_unshelve_cache (it2data, true);
1672 bidi_unshelve_cache (itdata, false);
1674 if (old_buffer)
1675 set_buffer_internal_1 (old_buffer);
1677 if (visible_p)
1679 if (w->hscroll > 0)
1680 *x -=
1681 window_hscroll_limited (w, WINDOW_XFRAME (w))
1682 * WINDOW_FRAME_COLUMN_WIDTH (w);
1683 /* For lines in an R2L paragraph, we need to mirror the X pixel
1684 coordinate wrt the text area. For the reasons, see the
1685 commentary in buffer_posn_from_coords and the explanation of
1686 the geometry used by the move_it_* functions at the end of
1687 the large commentary near the beginning of this file. */
1688 if (r2l)
1689 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1692 #if false
1693 /* Debugging code. */
1694 if (visible_p)
1695 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1696 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1697 else
1698 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1699 #endif
1701 /* Restore potentially overwritten values. */
1702 w->mode_line_height = prev_mode_line_height;
1703 w->header_line_height = prev_header_line_height;
1705 return visible_p;
1709 /* Return the next character from STR. Return in *LEN the length of
1710 the character. This is like STRING_CHAR_AND_LENGTH but never
1711 returns an invalid character. If we find one, we return a `?', but
1712 with the length of the invalid character. */
1714 static int
1715 string_char_and_length (const unsigned char *str, int *len)
1717 int c;
1719 c = STRING_CHAR_AND_LENGTH (str, *len);
1720 if (!CHAR_VALID_P (c))
1721 /* We may not change the length here because other places in Emacs
1722 don't use this function, i.e. they silently accept invalid
1723 characters. */
1724 c = '?';
1726 return c;
1731 /* Given a position POS containing a valid character and byte position
1732 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734 static struct text_pos
1735 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 eassert (STRINGP (string) && nchars >= 0);
1739 if (STRING_MULTIBYTE (string))
1741 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1742 int len;
1744 while (nchars--)
1746 string_char_and_length (p, &len);
1747 p += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755 return pos;
1759 /* Value is the text position, i.e. character and byte position,
1760 for character position CHARPOS in STRING. */
1762 static struct text_pos
1763 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 struct text_pos pos;
1766 eassert (STRINGP (string));
1767 eassert (charpos >= 0);
1768 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1769 return pos;
1773 /* Value is a text position, i.e. character and byte position, for
1774 character position CHARPOS in C string S. MULTIBYTE_P
1775 means recognize multibyte characters. */
1777 static struct text_pos
1778 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 struct text_pos pos;
1782 eassert (s != NULL);
1783 eassert (charpos >= 0);
1785 if (multibyte_p)
1787 int len;
1789 SET_TEXT_POS (pos, 0, 0);
1790 while (charpos--)
1792 string_char_and_length ((const unsigned char *) s, &len);
1793 s += len;
1794 CHARPOS (pos) += 1;
1795 BYTEPOS (pos) += len;
1798 else
1799 SET_TEXT_POS (pos, charpos, charpos);
1801 return pos;
1805 /* Value is the number of characters in C string S. MULTIBYTE_P
1806 means recognize multibyte characters. */
1808 static ptrdiff_t
1809 number_of_chars (const char *s, bool multibyte_p)
1811 ptrdiff_t nchars;
1813 if (multibyte_p)
1815 ptrdiff_t rest = strlen (s);
1816 int len;
1817 const unsigned char *p = (const unsigned char *) s;
1819 for (nchars = 0; rest > 0; ++nchars)
1821 string_char_and_length (p, &len);
1822 rest -= len, p += len;
1825 else
1826 nchars = strlen (s);
1828 return nchars;
1832 /* Compute byte position NEWPOS->bytepos corresponding to
1833 NEWPOS->charpos. POS is a known position in string STRING.
1834 NEWPOS->charpos must be >= POS.charpos. */
1836 static void
1837 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 eassert (STRINGP (string));
1840 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842 if (STRING_MULTIBYTE (string))
1843 *newpos = string_pos_nchars_ahead (pos, string,
1844 CHARPOS (*newpos) - CHARPOS (pos));
1845 else
1846 BYTEPOS (*newpos) = CHARPOS (*newpos);
1849 /* EXPORT:
1850 Return an estimation of the pixel height of mode or header lines on
1851 frame F. FACE_ID specifies what line's height to estimate. */
1854 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1859 int height = FONT_HEIGHT (FRAME_FONT (f));
1861 /* This function is called so early when Emacs starts that the face
1862 cache and mode line face are not yet initialized. */
1863 if (FRAME_FACE_CACHE (f))
1865 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1866 if (face)
1868 if (face->font)
1869 height = normal_char_height (face->font, -1);
1870 if (face->box_line_width > 0)
1871 height += 2 * face->box_line_width;
1875 return height;
1877 #endif
1879 return 1;
1882 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1883 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1884 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1885 not force the value into range. */
1887 void
1888 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1889 NativeRectangle *bounds, bool noclip)
1892 #ifdef HAVE_WINDOW_SYSTEM
1893 if (FRAME_WINDOW_P (f))
1895 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1896 even for negative values. */
1897 if (pix_x < 0)
1898 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1899 if (pix_y < 0)
1900 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1903 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905 if (bounds)
1906 STORE_NATIVE_RECT (*bounds,
1907 FRAME_COL_TO_PIXEL_X (f, pix_x),
1908 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1909 FRAME_COLUMN_WIDTH (f) - 1,
1910 FRAME_LINE_HEIGHT (f) - 1);
1912 /* PXW: Should we clip pixels before converting to columns/lines? */
1913 if (!noclip)
1915 if (pix_x < 0)
1916 pix_x = 0;
1917 else if (pix_x > FRAME_TOTAL_COLS (f))
1918 pix_x = FRAME_TOTAL_COLS (f);
1920 if (pix_y < 0)
1921 pix_y = 0;
1922 else if (pix_y > FRAME_TOTAL_LINES (f))
1923 pix_y = FRAME_TOTAL_LINES (f);
1926 #endif
1928 *x = pix_x;
1929 *y = pix_y;
1933 /* Find the glyph under window-relative coordinates X/Y in window W.
1934 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1935 strings. Return in *HPOS and *VPOS the row and column number of
1936 the glyph found. Return in *AREA the glyph area containing X.
1937 Value is a pointer to the glyph found or null if X/Y is not on
1938 text, or we can't tell because W's current matrix is not up to
1939 date. */
1941 static struct glyph *
1942 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1943 int *dx, int *dy, int *area)
1945 struct glyph *glyph, *end;
1946 struct glyph_row *row = NULL;
1947 int x0, i;
1949 /* Find row containing Y. Give up if some row is not enabled. */
1950 for (i = 0; i < w->current_matrix->nrows; ++i)
1952 row = MATRIX_ROW (w->current_matrix, i);
1953 if (!row->enabled_p)
1954 return NULL;
1955 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1956 break;
1959 *vpos = i;
1960 *hpos = 0;
1962 /* Give up if Y is not in the window. */
1963 if (i == w->current_matrix->nrows)
1964 return NULL;
1966 /* Get the glyph area containing X. */
1967 if (w->pseudo_window_p)
1969 *area = TEXT_AREA;
1970 x0 = 0;
1972 else
1974 if (x < window_box_left_offset (w, TEXT_AREA))
1976 *area = LEFT_MARGIN_AREA;
1977 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1979 else if (x < window_box_right_offset (w, TEXT_AREA))
1981 *area = TEXT_AREA;
1982 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1984 else
1986 *area = RIGHT_MARGIN_AREA;
1987 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1991 /* Find glyph containing X. */
1992 glyph = row->glyphs[*area];
1993 end = glyph + row->used[*area];
1994 x -= x0;
1995 while (glyph < end && x >= glyph->pixel_width)
1997 x -= glyph->pixel_width;
1998 ++glyph;
2001 if (glyph == end)
2002 return NULL;
2004 if (dx)
2006 *dx = x;
2007 *dy = y - (row->y + row->ascent - glyph->ascent);
2010 *hpos = glyph - row->glyphs[*area];
2011 return glyph;
2014 /* Convert frame-relative x/y to coordinates relative to window W.
2015 Takes pseudo-windows into account. */
2017 static void
2018 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2020 if (w->pseudo_window_p)
2022 /* A pseudo-window is always full-width, and starts at the
2023 left edge of the frame, plus a frame border. */
2024 struct frame *f = XFRAME (w->frame);
2025 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2026 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2028 else
2030 *x -= WINDOW_LEFT_EDGE_X (w);
2031 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2035 #ifdef HAVE_WINDOW_SYSTEM
2037 /* EXPORT:
2038 Return in RECTS[] at most N clipping rectangles for glyph string S.
2039 Return the number of stored rectangles. */
2042 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2044 XRectangle r;
2046 if (n <= 0)
2047 return 0;
2049 if (s->row->full_width_p)
2051 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2052 r.x = WINDOW_LEFT_EDGE_X (s->w);
2053 if (s->row->mode_line_p)
2054 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2055 else
2056 r.width = WINDOW_PIXEL_WIDTH (s->w);
2058 /* Unless displaying a mode or menu bar line, which are always
2059 fully visible, clip to the visible part of the row. */
2060 if (s->w->pseudo_window_p)
2061 r.height = s->row->visible_height;
2062 else
2063 r.height = s->height;
2065 else
2067 /* This is a text line that may be partially visible. */
2068 r.x = window_box_left (s->w, s->area);
2069 r.width = window_box_width (s->w, s->area);
2070 r.height = s->row->visible_height;
2073 if (s->clip_head)
2074 if (r.x < s->clip_head->x)
2076 if (r.width >= s->clip_head->x - r.x)
2077 r.width -= s->clip_head->x - r.x;
2078 else
2079 r.width = 0;
2080 r.x = s->clip_head->x;
2082 if (s->clip_tail)
2083 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2085 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2086 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2087 else
2088 r.width = 0;
2091 /* If S draws overlapping rows, it's sufficient to use the top and
2092 bottom of the window for clipping because this glyph string
2093 intentionally draws over other lines. */
2094 if (s->for_overlaps)
2096 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2097 r.height = window_text_bottom_y (s->w) - r.y;
2099 /* Alas, the above simple strategy does not work for the
2100 environments with anti-aliased text: if the same text is
2101 drawn onto the same place multiple times, it gets thicker.
2102 If the overlap we are processing is for the erased cursor, we
2103 take the intersection with the rectangle of the cursor. */
2104 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2106 XRectangle rc, r_save = r;
2108 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2109 rc.y = s->w->phys_cursor.y;
2110 rc.width = s->w->phys_cursor_width;
2111 rc.height = s->w->phys_cursor_height;
2113 x_intersect_rectangles (&r_save, &rc, &r);
2116 else
2118 /* Don't use S->y for clipping because it doesn't take partially
2119 visible lines into account. For example, it can be negative for
2120 partially visible lines at the top of a window. */
2121 if (!s->row->full_width_p
2122 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2123 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2124 else
2125 r.y = max (0, s->row->y);
2128 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2130 /* If drawing the cursor, don't let glyph draw outside its
2131 advertised boundaries. Cleartype does this under some circumstances. */
2132 if (s->hl == DRAW_CURSOR)
2134 struct glyph *glyph = s->first_glyph;
2135 int height, max_y;
2137 if (s->x > r.x)
2139 if (r.width >= s->x - r.x)
2140 r.width -= s->x - r.x;
2141 else /* R2L hscrolled row with cursor outside text area */
2142 r.width = 0;
2143 r.x = s->x;
2145 r.width = min (r.width, glyph->pixel_width);
2147 /* If r.y is below window bottom, ensure that we still see a cursor. */
2148 height = min (glyph->ascent + glyph->descent,
2149 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2150 max_y = window_text_bottom_y (s->w) - height;
2151 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2152 if (s->ybase - glyph->ascent > max_y)
2154 r.y = max_y;
2155 r.height = height;
2157 else
2159 /* Don't draw cursor glyph taller than our actual glyph. */
2160 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2161 if (height < r.height)
2163 max_y = r.y + r.height;
2164 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2165 r.height = min (max_y - r.y, height);
2170 if (s->row->clip)
2172 XRectangle r_save = r;
2174 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2175 r.width = 0;
2178 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2179 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2181 #ifdef CONVERT_FROM_XRECT
2182 CONVERT_FROM_XRECT (r, *rects);
2183 #else
2184 *rects = r;
2185 #endif
2186 return 1;
2188 else
2190 /* If we are processing overlapping and allowed to return
2191 multiple clipping rectangles, we exclude the row of the glyph
2192 string from the clipping rectangle. This is to avoid drawing
2193 the same text on the environment with anti-aliasing. */
2194 #ifdef CONVERT_FROM_XRECT
2195 XRectangle rs[2];
2196 #else
2197 XRectangle *rs = rects;
2198 #endif
2199 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2201 if (s->for_overlaps & OVERLAPS_PRED)
2203 rs[i] = r;
2204 if (r.y + r.height > row_y)
2206 if (r.y < row_y)
2207 rs[i].height = row_y - r.y;
2208 else
2209 rs[i].height = 0;
2211 i++;
2213 if (s->for_overlaps & OVERLAPS_SUCC)
2215 rs[i] = r;
2216 if (r.y < row_y + s->row->visible_height)
2218 if (r.y + r.height > row_y + s->row->visible_height)
2220 rs[i].y = row_y + s->row->visible_height;
2221 rs[i].height = r.y + r.height - rs[i].y;
2223 else
2224 rs[i].height = 0;
2226 i++;
2229 n = i;
2230 #ifdef CONVERT_FROM_XRECT
2231 for (i = 0; i < n; i++)
2232 CONVERT_FROM_XRECT (rs[i], rects[i]);
2233 #endif
2234 return n;
2238 /* EXPORT:
2239 Return in *NR the clipping rectangle for glyph string S. */
2241 void
2242 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2244 get_glyph_string_clip_rects (s, nr, 1);
2248 /* EXPORT:
2249 Return the position and height of the phys cursor in window W.
2250 Set w->phys_cursor_width to width of phys cursor.
2253 void
2254 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2255 struct glyph *glyph, int *xp, int *yp, int *heightp)
2257 struct frame *f = XFRAME (WINDOW_FRAME (w));
2258 int x, y, wd, h, h0, y0, ascent;
2260 /* Compute the width of the rectangle to draw. If on a stretch
2261 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2262 rectangle as wide as the glyph, but use a canonical character
2263 width instead. */
2264 wd = glyph->pixel_width;
2266 x = w->phys_cursor.x;
2267 if (x < 0)
2269 wd += x;
2270 x = 0;
2273 if (glyph->type == STRETCH_GLYPH
2274 && !x_stretch_cursor_p)
2275 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2276 w->phys_cursor_width = wd;
2278 /* Don't let the hollow cursor glyph descend below the glyph row's
2279 ascent value, lest the hollow cursor looks funny. */
2280 y = w->phys_cursor.y;
2281 ascent = row->ascent;
2282 if (row->ascent < glyph->ascent)
2284 y -= glyph->ascent - row->ascent;
2285 ascent = glyph->ascent;
2288 /* If y is below window bottom, ensure that we still see a cursor. */
2289 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2291 h = max (h0, ascent + glyph->descent);
2292 h0 = min (h0, ascent + glyph->descent);
2294 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2295 if (y < y0)
2297 h = max (h - (y0 - y) + 1, h0);
2298 y = y0 - 1;
2300 else
2302 y0 = window_text_bottom_y (w) - h0;
2303 if (y > y0)
2305 h += y - y0;
2306 y = y0;
2310 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2311 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2312 *heightp = h;
2316 * Remember which glyph the mouse is over.
2319 void
2320 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2322 Lisp_Object window;
2323 struct window *w;
2324 struct glyph_row *r, *gr, *end_row;
2325 enum window_part part;
2326 enum glyph_row_area area;
2327 int x, y, width, height;
2329 /* Try to determine frame pixel position and size of the glyph under
2330 frame pixel coordinates X/Y on frame F. */
2332 if (window_resize_pixelwise)
2334 width = height = 1;
2335 goto virtual_glyph;
2337 else if (!f->glyphs_initialized_p
2338 || (window = window_from_coordinates (f, gx, gy, &part, false),
2339 NILP (window)))
2341 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2342 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2343 goto virtual_glyph;
2346 w = XWINDOW (window);
2347 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2348 height = WINDOW_FRAME_LINE_HEIGHT (w);
2350 x = window_relative_x_coord (w, part, gx);
2351 y = gy - WINDOW_TOP_EDGE_Y (w);
2353 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2354 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2356 if (w->pseudo_window_p)
2358 area = TEXT_AREA;
2359 part = ON_MODE_LINE; /* Don't adjust margin. */
2360 goto text_glyph;
2363 switch (part)
2365 case ON_LEFT_MARGIN:
2366 area = LEFT_MARGIN_AREA;
2367 goto text_glyph;
2369 case ON_RIGHT_MARGIN:
2370 area = RIGHT_MARGIN_AREA;
2371 goto text_glyph;
2373 case ON_HEADER_LINE:
2374 case ON_MODE_LINE:
2375 gr = (part == ON_HEADER_LINE
2376 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2377 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2378 gy = gr->y;
2379 area = TEXT_AREA;
2380 goto text_glyph_row_found;
2382 case ON_TEXT:
2383 area = TEXT_AREA;
2385 text_glyph:
2386 gr = 0; gy = 0;
2387 for (; r <= end_row && r->enabled_p; ++r)
2388 if (r->y + r->height > y)
2390 gr = r; gy = r->y;
2391 break;
2394 text_glyph_row_found:
2395 if (gr && gy <= y)
2397 struct glyph *g = gr->glyphs[area];
2398 struct glyph *end = g + gr->used[area];
2400 height = gr->height;
2401 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2402 if (gx + g->pixel_width > x)
2403 break;
2405 if (g < end)
2407 if (g->type == IMAGE_GLYPH)
2409 /* Don't remember when mouse is over image, as
2410 image may have hot-spots. */
2411 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2412 return;
2414 width = g->pixel_width;
2416 else
2418 /* Use nominal char spacing at end of line. */
2419 x -= gx;
2420 gx += (x / width) * width;
2423 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2425 gx += window_box_left_offset (w, area);
2426 /* Don't expand over the modeline to make sure the vertical
2427 drag cursor is shown early enough. */
2428 height = min (height,
2429 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2432 else
2434 /* Use nominal line height at end of window. */
2435 gx = (x / width) * width;
2436 y -= gy;
2437 gy += (y / height) * height;
2438 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2439 /* See comment above. */
2440 height = min (height,
2441 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2443 break;
2445 case ON_LEFT_FRINGE:
2446 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2447 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2448 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2449 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2450 goto row_glyph;
2452 case ON_RIGHT_FRINGE:
2453 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2454 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2455 : window_box_right_offset (w, TEXT_AREA));
2456 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2457 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2458 && !WINDOW_RIGHTMOST_P (w))
2459 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2460 /* Make sure the vertical border can get her own glyph to the
2461 right of the one we build here. */
2462 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2463 else
2464 width = WINDOW_PIXEL_WIDTH (w) - gx;
2465 else
2466 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2468 goto row_glyph;
2470 case ON_VERTICAL_BORDER:
2471 gx = WINDOW_PIXEL_WIDTH (w) - width;
2472 goto row_glyph;
2474 case ON_VERTICAL_SCROLL_BAR:
2475 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2477 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2478 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2479 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2480 : 0)));
2481 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2483 row_glyph:
2484 gr = 0, gy = 0;
2485 for (; r <= end_row && r->enabled_p; ++r)
2486 if (r->y + r->height > y)
2488 gr = r; gy = r->y;
2489 break;
2492 if (gr && gy <= y)
2493 height = gr->height;
2494 else
2496 /* Use nominal line height at end of window. */
2497 y -= gy;
2498 gy += (y / height) * height;
2500 break;
2502 case ON_RIGHT_DIVIDER:
2503 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2504 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2505 gy = 0;
2506 /* The bottom divider prevails. */
2507 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2508 goto add_edge;
2510 case ON_BOTTOM_DIVIDER:
2511 gx = 0;
2512 width = WINDOW_PIXEL_WIDTH (w);
2513 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2514 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2515 goto add_edge;
2517 default:
2519 virtual_glyph:
2520 /* If there is no glyph under the mouse, then we divide the screen
2521 into a grid of the smallest glyph in the frame, and use that
2522 as our "glyph". */
2524 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2525 round down even for negative values. */
2526 if (gx < 0)
2527 gx -= width - 1;
2528 if (gy < 0)
2529 gy -= height - 1;
2531 gx = (gx / width) * width;
2532 gy = (gy / height) * height;
2534 goto store_rect;
2537 add_edge:
2538 gx += WINDOW_LEFT_EDGE_X (w);
2539 gy += WINDOW_TOP_EDGE_Y (w);
2541 store_rect:
2542 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2544 /* Visible feedback for debugging. */
2545 #if false && defined HAVE_X_WINDOWS
2546 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2547 f->output_data.x->normal_gc,
2548 gx, gy, width, height);
2549 #endif
2553 #endif /* HAVE_WINDOW_SYSTEM */
2555 static void
2556 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2558 eassert (w);
2559 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2560 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2561 w->window_end_vpos
2562 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2565 static bool
2566 hscrolling_current_line_p (struct window *w)
2568 return (!w->suspend_auto_hscroll
2569 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2570 Qcurrent_line));
2573 /***********************************************************************
2574 Lisp form evaluation
2575 ***********************************************************************/
2577 /* Error handler for safe_eval and safe_call. */
2579 static Lisp_Object
2580 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2582 add_to_log ("Error during redisplay: %S signaled %S",
2583 Flist (nargs, args), arg);
2584 return Qnil;
2587 /* Call function FUNC with the rest of NARGS - 1 arguments
2588 following. Return the result, or nil if something went
2589 wrong. Prevent redisplay during the evaluation. */
2591 static Lisp_Object
2592 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2594 Lisp_Object val;
2596 if (inhibit_eval_during_redisplay)
2597 val = Qnil;
2598 else
2600 ptrdiff_t i;
2601 ptrdiff_t count = SPECPDL_INDEX ();
2602 Lisp_Object *args;
2603 USE_SAFE_ALLOCA;
2604 SAFE_ALLOCA_LISP (args, nargs);
2606 args[0] = func;
2607 for (i = 1; i < nargs; i++)
2608 args[i] = va_arg (ap, Lisp_Object);
2610 specbind (Qinhibit_redisplay, Qt);
2611 if (inhibit_quit)
2612 specbind (Qinhibit_quit, Qt);
2613 /* Use Qt to ensure debugger does not run,
2614 so there is no possibility of wanting to redisplay. */
2615 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2616 safe_eval_handler);
2617 SAFE_FREE ();
2618 val = unbind_to (count, val);
2621 return val;
2624 Lisp_Object
2625 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2627 Lisp_Object retval;
2628 va_list ap;
2630 va_start (ap, func);
2631 retval = safe__call (false, nargs, func, ap);
2632 va_end (ap);
2633 return retval;
2636 /* Call function FN with one argument ARG.
2637 Return the result, or nil if something went wrong. */
2639 Lisp_Object
2640 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2642 return safe_call (2, fn, arg);
2645 static Lisp_Object
2646 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2648 Lisp_Object retval;
2649 va_list ap;
2651 va_start (ap, fn);
2652 retval = safe__call (inhibit_quit, 2, fn, ap);
2653 va_end (ap);
2654 return retval;
2657 Lisp_Object
2658 safe_eval (Lisp_Object sexpr)
2660 return safe__call1 (false, Qeval, sexpr);
2663 static Lisp_Object
2664 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2666 return safe__call1 (inhibit_quit, Qeval, sexpr);
2669 /* Call function FN with two arguments ARG1 and ARG2.
2670 Return the result, or nil if something went wrong. */
2672 Lisp_Object
2673 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2675 return safe_call (3, fn, arg1, arg2);
2680 /***********************************************************************
2681 Debugging
2682 ***********************************************************************/
2684 /* Define CHECK_IT to perform sanity checks on iterators.
2685 This is for debugging. It is too slow to do unconditionally. */
2687 static void
2688 CHECK_IT (struct it *it)
2690 #if false
2691 if (it->method == GET_FROM_STRING)
2693 eassert (STRINGP (it->string));
2694 eassert (IT_STRING_CHARPOS (*it) >= 0);
2696 else
2698 eassert (IT_STRING_CHARPOS (*it) < 0);
2699 if (it->method == GET_FROM_BUFFER)
2701 /* Check that character and byte positions agree. */
2702 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2706 if (it->dpvec)
2707 eassert (it->current.dpvec_index >= 0);
2708 else
2709 eassert (it->current.dpvec_index < 0);
2710 #endif
2714 /* Check that the window end of window W is what we expect it
2715 to be---the last row in the current matrix displaying text. */
2717 static void
2718 CHECK_WINDOW_END (struct window *w)
2720 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2721 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2723 struct glyph_row *row;
2724 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2725 !row->enabled_p
2726 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2727 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2729 #endif
2732 /***********************************************************************
2733 Iterator initialization
2734 ***********************************************************************/
2736 /* Initialize IT for displaying current_buffer in window W, starting
2737 at character position CHARPOS. CHARPOS < 0 means that no buffer
2738 position is specified which is useful when the iterator is assigned
2739 a position later. BYTEPOS is the byte position corresponding to
2740 CHARPOS.
2742 If ROW is not null, calls to produce_glyphs with IT as parameter
2743 will produce glyphs in that row.
2745 BASE_FACE_ID is the id of a base face to use. It must be one of
2746 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2747 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2748 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2750 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2751 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2752 will be initialized to use the corresponding mode line glyph row of
2753 the desired matrix of W. */
2755 void
2756 init_iterator (struct it *it, struct window *w,
2757 ptrdiff_t charpos, ptrdiff_t bytepos,
2758 struct glyph_row *row, enum face_id base_face_id)
2760 enum face_id remapped_base_face_id = base_face_id;
2762 /* Some precondition checks. */
2763 eassert (w != NULL && it != NULL);
2764 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2765 && charpos <= ZV));
2767 /* If face attributes have been changed since the last redisplay,
2768 free realized faces now because they depend on face definitions
2769 that might have changed. Don't free faces while there might be
2770 desired matrices pending which reference these faces. */
2771 if (!inhibit_free_realized_faces)
2773 if (face_change)
2775 face_change = false;
2776 XFRAME (w->frame)->face_change = 0;
2777 free_all_realized_faces (Qnil);
2779 else if (XFRAME (w->frame)->face_change)
2781 XFRAME (w->frame)->face_change = 0;
2782 free_all_realized_faces (w->frame);
2786 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2787 if (! NILP (Vface_remapping_alist))
2788 remapped_base_face_id
2789 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2791 /* Use one of the mode line rows of W's desired matrix if
2792 appropriate. */
2793 if (row == NULL)
2795 if (base_face_id == MODE_LINE_FACE_ID
2796 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2797 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2798 else if (base_face_id == HEADER_LINE_FACE_ID)
2799 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2802 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2803 Other parts of redisplay rely on that. */
2804 memclear (it, sizeof *it);
2805 it->current.overlay_string_index = -1;
2806 it->current.dpvec_index = -1;
2807 it->base_face_id = remapped_base_face_id;
2808 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2809 it->paragraph_embedding = L2R;
2810 it->bidi_it.w = w;
2812 /* The window in which we iterate over current_buffer: */
2813 XSETWINDOW (it->window, w);
2814 it->w = w;
2815 it->f = XFRAME (w->frame);
2817 it->cmp_it.id = -1;
2819 /* Extra space between lines (on window systems only). */
2820 if (base_face_id == DEFAULT_FACE_ID
2821 && FRAME_WINDOW_P (it->f))
2823 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2824 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2825 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2826 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2827 * FRAME_LINE_HEIGHT (it->f));
2828 else if (it->f->extra_line_spacing > 0)
2829 it->extra_line_spacing = it->f->extra_line_spacing;
2832 /* If realized faces have been removed, e.g. because of face
2833 attribute changes of named faces, recompute them. When running
2834 in batch mode, the face cache of the initial frame is null. If
2835 we happen to get called, make a dummy face cache. */
2836 if (FRAME_FACE_CACHE (it->f) == NULL)
2837 init_frame_faces (it->f);
2838 if (FRAME_FACE_CACHE (it->f)->used == 0)
2839 recompute_basic_faces (it->f);
2841 it->override_ascent = -1;
2843 /* Are control characters displayed as `^C'? */
2844 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2846 /* -1 means everything between a CR and the following line end
2847 is invisible. >0 means lines indented more than this value are
2848 invisible. */
2849 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2850 ? (clip_to_bounds
2851 (-1, XINT (BVAR (current_buffer, selective_display)),
2852 PTRDIFF_MAX))
2853 : (!NILP (BVAR (current_buffer, selective_display))
2854 ? -1 : 0));
2855 it->selective_display_ellipsis_p
2856 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2858 /* Display table to use. */
2859 it->dp = window_display_table (w);
2861 /* Are multibyte characters enabled in current_buffer? */
2862 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2864 /* Get the position at which the redisplay_end_trigger hook should
2865 be run, if it is to be run at all. */
2866 if (MARKERP (w->redisplay_end_trigger)
2867 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2868 it->redisplay_end_trigger_charpos
2869 = marker_position (w->redisplay_end_trigger);
2870 else if (INTEGERP (w->redisplay_end_trigger))
2871 it->redisplay_end_trigger_charpos
2872 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2873 PTRDIFF_MAX);
2875 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2877 /* Are lines in the display truncated? */
2878 if (TRUNCATE != 0)
2879 it->line_wrap = TRUNCATE;
2880 if (base_face_id == DEFAULT_FACE_ID
2881 && !it->w->hscroll
2882 && (WINDOW_FULL_WIDTH_P (it->w)
2883 || NILP (Vtruncate_partial_width_windows)
2884 || (INTEGERP (Vtruncate_partial_width_windows)
2885 /* PXW: Shall we do something about this? */
2886 && (XINT (Vtruncate_partial_width_windows)
2887 <= WINDOW_TOTAL_COLS (it->w))))
2888 && NILP (BVAR (current_buffer, truncate_lines)))
2889 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2890 ? WINDOW_WRAP : WORD_WRAP;
2892 /* Get dimensions of truncation and continuation glyphs. These are
2893 displayed as fringe bitmaps under X, but we need them for such
2894 frames when the fringes are turned off. The no_special_glyphs slot
2895 of the iterator's frame, when set, suppresses their display - by
2896 default for tooltip frames and when set via the 'no-special-glyphs'
2897 frame parameter. */
2898 #ifdef HAVE_WINDOW_SYSTEM
2899 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2900 #endif
2902 if (it->line_wrap == TRUNCATE)
2904 /* We will need the truncation glyph. */
2905 eassert (it->glyph_row == NULL);
2906 produce_special_glyphs (it, IT_TRUNCATION);
2907 it->truncation_pixel_width = it->pixel_width;
2909 else
2911 /* We will need the continuation glyph. */
2912 eassert (it->glyph_row == NULL);
2913 produce_special_glyphs (it, IT_CONTINUATION);
2914 it->continuation_pixel_width = it->pixel_width;
2918 /* Reset these values to zero because the produce_special_glyphs
2919 above has changed them. */
2920 it->pixel_width = it->ascent = it->descent = 0;
2921 it->phys_ascent = it->phys_descent = 0;
2923 /* Set this after getting the dimensions of truncation and
2924 continuation glyphs, so that we don't produce glyphs when calling
2925 produce_special_glyphs, above. */
2926 it->glyph_row = row;
2927 it->area = TEXT_AREA;
2929 /* Get the dimensions of the display area. The display area
2930 consists of the visible window area plus a horizontally scrolled
2931 part to the left of the window. All x-values are relative to the
2932 start of this total display area. */
2933 if (base_face_id != DEFAULT_FACE_ID)
2935 /* Mode lines, menu bar in terminal frames. */
2936 it->first_visible_x = 0;
2937 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2939 else
2941 /* When hscrolling only the current line, don't apply the
2942 hscroll here, it will be applied by display_line when it gets
2943 to laying out the line showing point. However, if the
2944 window's min_hscroll is positive, the user specified a lower
2945 bound for automatic hscrolling, so they expect the
2946 non-current lines to obey that hscroll amount. */
2947 if (hscrolling_current_line_p (w))
2949 if (w->min_hscroll > 0)
2950 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2951 else
2952 it->first_visible_x = 0;
2954 else
2955 it->first_visible_x =
2956 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2957 it->last_visible_x = (it->first_visible_x
2958 + window_box_width (w, TEXT_AREA));
2960 /* If we truncate lines, leave room for the truncation glyph(s) at
2961 the right margin. Otherwise, leave room for the continuation
2962 glyph(s). Done only if the window has no right fringe. */
2963 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2965 if (it->line_wrap == TRUNCATE)
2966 it->last_visible_x -= it->truncation_pixel_width;
2967 else
2968 it->last_visible_x -= it->continuation_pixel_width;
2971 it->header_line_p = window_wants_header_line (w);
2972 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2975 /* Leave room for a border glyph. */
2976 if (!FRAME_WINDOW_P (it->f)
2977 && !WINDOW_RIGHTMOST_P (it->w))
2978 it->last_visible_x -= 1;
2980 it->last_visible_y = window_text_bottom_y (w);
2982 /* For mode lines and alike, arrange for the first glyph having a
2983 left box line if the face specifies a box. */
2984 if (base_face_id != DEFAULT_FACE_ID)
2986 struct face *face;
2988 it->face_id = remapped_base_face_id;
2990 /* If we have a boxed mode line, make the first character appear
2991 with a left box line. */
2992 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2993 if (face && face->box != FACE_NO_BOX)
2994 it->start_of_box_run_p = true;
2997 /* If a buffer position was specified, set the iterator there,
2998 getting overlays and face properties from that position. */
2999 if (charpos >= BUF_BEG (current_buffer))
3001 it->stop_charpos = charpos;
3002 it->end_charpos = ZV;
3003 eassert (charpos == BYTE_TO_CHAR (bytepos));
3004 IT_CHARPOS (*it) = charpos;
3005 IT_BYTEPOS (*it) = bytepos;
3007 /* We will rely on `reseat' to set this up properly, via
3008 handle_face_prop. */
3009 it->face_id = it->base_face_id;
3011 it->start = it->current;
3012 /* Do we need to reorder bidirectional text? Not if this is a
3013 unibyte buffer: by definition, none of the single-byte
3014 characters are strong R2L, so no reordering is needed. And
3015 bidi.c doesn't support unibyte buffers anyway. Also, don't
3016 reorder while we are loading loadup.el, since the tables of
3017 character properties needed for reordering are not yet
3018 available. */
3019 it->bidi_p =
3020 !redisplay__inhibit_bidi
3021 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3022 && it->multibyte_p;
3024 /* If we are to reorder bidirectional text, init the bidi
3025 iterator. */
3026 if (it->bidi_p)
3028 /* Since we don't know at this point whether there will be
3029 any R2L lines in the window, we reserve space for
3030 truncation/continuation glyphs even if only the left
3031 fringe is absent. */
3032 if (base_face_id == DEFAULT_FACE_ID
3033 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3034 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3036 if (it->line_wrap == TRUNCATE)
3037 it->last_visible_x -= it->truncation_pixel_width;
3038 else
3039 it->last_visible_x -= it->continuation_pixel_width;
3041 /* Note the paragraph direction that this buffer wants to
3042 use. */
3043 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3044 Qleft_to_right))
3045 it->paragraph_embedding = L2R;
3046 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3047 Qright_to_left))
3048 it->paragraph_embedding = R2L;
3049 else
3050 it->paragraph_embedding = NEUTRAL_DIR;
3051 bidi_unshelve_cache (NULL, false);
3052 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3053 &it->bidi_it);
3056 /* Compute faces etc. */
3057 reseat (it, it->current.pos, true);
3060 CHECK_IT (it);
3064 /* Initialize IT for the display of window W with window start POS. */
3066 void
3067 start_display (struct it *it, struct window *w, struct text_pos pos)
3069 struct glyph_row *row;
3070 bool first_vpos = window_wants_header_line (w);
3072 row = w->desired_matrix->rows + first_vpos;
3073 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3074 it->first_vpos = first_vpos;
3076 /* Don't reseat to previous visible line start if current start
3077 position is in a string or image. */
3078 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3080 int first_y = it->current_y;
3082 /* If window start is not at a line start, skip forward to POS to
3083 get the correct continuation lines width. */
3084 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3085 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3086 if (!start_at_line_beg_p)
3088 int new_x;
3090 reseat_at_previous_visible_line_start (it);
3091 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3093 new_x = it->current_x + it->pixel_width;
3095 /* If lines are continued, this line may end in the middle
3096 of a multi-glyph character (e.g. a control character
3097 displayed as \003, or in the middle of an overlay
3098 string). In this case move_it_to above will not have
3099 taken us to the start of the continuation line but to the
3100 end of the continued line. */
3101 if (it->current_x > 0
3102 && it->line_wrap != TRUNCATE /* Lines are continued. */
3103 && (/* And glyph doesn't fit on the line. */
3104 new_x > it->last_visible_x
3105 /* Or it fits exactly and we're on a window
3106 system frame. */
3107 || (new_x == it->last_visible_x
3108 && FRAME_WINDOW_P (it->f)
3109 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3110 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3111 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3113 if ((it->current.dpvec_index >= 0
3114 || it->current.overlay_string_index >= 0)
3115 /* If we are on a newline from a display vector or
3116 overlay string, then we are already at the end of
3117 a screen line; no need to go to the next line in
3118 that case, as this line is not really continued.
3119 (If we do go to the next line, C-e will not DTRT.) */
3120 && it->c != '\n')
3122 set_iterator_to_next (it, true);
3123 move_it_in_display_line_to (it, -1, -1, 0);
3126 it->continuation_lines_width += it->current_x;
3128 /* If the character at POS is displayed via a display
3129 vector, move_it_to above stops at the final glyph of
3130 IT->dpvec. To make the caller redisplay that character
3131 again (a.k.a. start at POS), we need to reset the
3132 dpvec_index to the beginning of IT->dpvec. */
3133 else if (it->current.dpvec_index >= 0)
3134 it->current.dpvec_index = 0;
3136 /* We're starting a new display line, not affected by the
3137 height of the continued line, so clear the appropriate
3138 fields in the iterator structure. */
3139 it->max_ascent = it->max_descent = 0;
3140 it->max_phys_ascent = it->max_phys_descent = 0;
3142 it->current_y = first_y;
3143 it->vpos = 0;
3144 it->current_x = it->hpos = 0;
3150 /* Return true if POS is a position in ellipses displayed for invisible
3151 text. W is the window we display, for text property lookup. */
3153 static bool
3154 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3156 Lisp_Object prop, window;
3157 bool ellipses_p = false;
3158 ptrdiff_t charpos = CHARPOS (pos->pos);
3160 /* If POS specifies a position in a display vector, this might
3161 be for an ellipsis displayed for invisible text. We won't
3162 get the iterator set up for delivering that ellipsis unless
3163 we make sure that it gets aware of the invisible text. */
3164 if (pos->dpvec_index >= 0
3165 && pos->overlay_string_index < 0
3166 && CHARPOS (pos->string_pos) < 0
3167 && charpos > BEGV
3168 && (XSETWINDOW (window, w),
3169 prop = Fget_char_property (make_number (charpos),
3170 Qinvisible, window),
3171 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3173 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3174 window);
3175 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3178 return ellipses_p;
3182 /* Initialize IT for stepping through current_buffer in window W,
3183 starting at position POS that includes overlay string and display
3184 vector/ control character translation position information. Value
3185 is false if there are overlay strings with newlines at POS. */
3187 static bool
3188 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3190 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3191 int i;
3192 bool overlay_strings_with_newlines = false;
3194 /* If POS specifies a position in a display vector, this might
3195 be for an ellipsis displayed for invisible text. We won't
3196 get the iterator set up for delivering that ellipsis unless
3197 we make sure that it gets aware of the invisible text. */
3198 if (in_ellipses_for_invisible_text_p (pos, w))
3200 --charpos;
3201 bytepos = 0;
3204 /* Keep in mind: the call to reseat in init_iterator skips invisible
3205 text, so we might end up at a position different from POS. This
3206 is only a problem when POS is a row start after a newline and an
3207 overlay starts there with an after-string, and the overlay has an
3208 invisible property. Since we don't skip invisible text in
3209 display_line and elsewhere immediately after consuming the
3210 newline before the row start, such a POS will not be in a string,
3211 but the call to init_iterator below will move us to the
3212 after-string. */
3213 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3215 /* This only scans the current chunk -- it should scan all chunks.
3216 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3217 to 16 in 22.1 to make this a lesser problem. */
3218 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3220 const char *s = SSDATA (it->overlay_strings[i]);
3221 const char *e = s + SBYTES (it->overlay_strings[i]);
3223 while (s < e && *s != '\n')
3224 ++s;
3226 if (s < e)
3228 overlay_strings_with_newlines = true;
3229 break;
3233 /* If position is within an overlay string, set up IT to the right
3234 overlay string. */
3235 if (pos->overlay_string_index >= 0)
3237 int relative_index;
3239 /* If the first overlay string happens to have a `display'
3240 property for an image, the iterator will be set up for that
3241 image, and we have to undo that setup first before we can
3242 correct the overlay string index. */
3243 if (it->method == GET_FROM_IMAGE)
3244 pop_it (it);
3246 /* We already have the first chunk of overlay strings in
3247 IT->overlay_strings. Load more until the one for
3248 pos->overlay_string_index is in IT->overlay_strings. */
3249 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3251 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3252 it->current.overlay_string_index = 0;
3253 while (n--)
3255 load_overlay_strings (it, 0);
3256 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3260 it->current.overlay_string_index = pos->overlay_string_index;
3261 relative_index = (it->current.overlay_string_index
3262 % OVERLAY_STRING_CHUNK_SIZE);
3263 it->string = it->overlay_strings[relative_index];
3264 eassert (STRINGP (it->string));
3265 it->current.string_pos = pos->string_pos;
3266 it->method = GET_FROM_STRING;
3267 it->end_charpos = SCHARS (it->string);
3268 /* Set up the bidi iterator for this overlay string. */
3269 if (it->bidi_p)
3271 it->bidi_it.string.lstring = it->string;
3272 it->bidi_it.string.s = NULL;
3273 it->bidi_it.string.schars = SCHARS (it->string);
3274 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3275 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3276 it->bidi_it.string.unibyte = !it->multibyte_p;
3277 it->bidi_it.w = it->w;
3278 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3279 FRAME_WINDOW_P (it->f), &it->bidi_it);
3281 /* Synchronize the state of the bidi iterator with
3282 pos->string_pos. For any string position other than
3283 zero, this will be done automagically when we resume
3284 iteration over the string and get_visually_first_element
3285 is called. But if string_pos is zero, and the string is
3286 to be reordered for display, we need to resync manually,
3287 since it could be that the iteration state recorded in
3288 pos ended at string_pos of 0 moving backwards in string. */
3289 if (CHARPOS (pos->string_pos) == 0)
3291 get_visually_first_element (it);
3292 if (IT_STRING_CHARPOS (*it) != 0)
3293 do {
3294 /* Paranoia. */
3295 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3296 bidi_move_to_visually_next (&it->bidi_it);
3297 } while (it->bidi_it.charpos != 0);
3299 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3300 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3304 if (CHARPOS (pos->string_pos) >= 0)
3306 /* Recorded position is not in an overlay string, but in another
3307 string. This can only be a string from a `display' property.
3308 IT should already be filled with that string. */
3309 it->current.string_pos = pos->string_pos;
3310 eassert (STRINGP (it->string));
3311 if (it->bidi_p)
3312 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3313 FRAME_WINDOW_P (it->f), &it->bidi_it);
3316 /* Restore position in display vector translations, control
3317 character translations or ellipses. */
3318 if (pos->dpvec_index >= 0)
3320 if (it->dpvec == NULL)
3321 get_next_display_element (it);
3322 eassert (it->dpvec && it->current.dpvec_index == 0);
3323 it->current.dpvec_index = pos->dpvec_index;
3326 CHECK_IT (it);
3327 return !overlay_strings_with_newlines;
3331 /* Initialize IT for stepping through current_buffer in window W
3332 starting at ROW->start. */
3334 static void
3335 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3337 init_from_display_pos (it, w, &row->start);
3338 it->start = row->start;
3339 it->continuation_lines_width = row->continuation_lines_width;
3340 CHECK_IT (it);
3344 /* Initialize IT for stepping through current_buffer in window W
3345 starting in the line following ROW, i.e. starting at ROW->end.
3346 Value is false if there are overlay strings with newlines at ROW's
3347 end position. */
3349 static bool
3350 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3352 bool success = false;
3354 if (init_from_display_pos (it, w, &row->end))
3356 if (row->continued_p)
3357 it->continuation_lines_width
3358 = row->continuation_lines_width + row->pixel_width;
3359 CHECK_IT (it);
3360 success = true;
3363 return success;
3369 /***********************************************************************
3370 Text properties
3371 ***********************************************************************/
3373 /* Called when IT reaches IT->stop_charpos. Handle text property and
3374 overlay changes. Set IT->stop_charpos to the next position where
3375 to stop. */
3377 static void
3378 handle_stop (struct it *it)
3380 enum prop_handled handled;
3381 bool handle_overlay_change_p;
3382 struct props *p;
3384 it->dpvec = NULL;
3385 it->current.dpvec_index = -1;
3386 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3387 it->ellipsis_p = false;
3389 /* Use face of preceding text for ellipsis (if invisible) */
3390 if (it->selective_display_ellipsis_p)
3391 it->saved_face_id = it->face_id;
3393 /* Here's the description of the semantics of, and the logic behind,
3394 the various HANDLED_* statuses:
3396 HANDLED_NORMALLY means the handler did its job, and the loop
3397 should proceed to calling the next handler in order.
3399 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3400 change in the properties and overlays at current position, so the
3401 loop should be restarted, to re-invoke the handlers that were
3402 already called. This happens when fontification-functions were
3403 called by handle_fontified_prop, and actually fontified
3404 something. Another case where HANDLED_RECOMPUTE_PROPS is
3405 returned is when we discover overlay strings that need to be
3406 displayed right away. The loop below will continue for as long
3407 as the status is HANDLED_RECOMPUTE_PROPS.
3409 HANDLED_RETURN means return immediately to the caller, to
3410 continue iteration without calling any further handlers. This is
3411 used when we need to act on some property right away, for example
3412 when we need to display the ellipsis or a replacing display
3413 property, such as display string or image.
3415 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3416 consumed, and the handler switched to the next overlay string.
3417 This signals the loop below to refrain from looking for more
3418 overlays before all the overlay strings of the current overlay
3419 are processed.
3421 Some of the handlers called by the loop push the iterator state
3422 onto the stack (see 'push_it'), and arrange for the iteration to
3423 continue with another object, such as an image, a display string,
3424 or an overlay string. In most such cases, it->stop_charpos is
3425 set to the first character of the string, so that when the
3426 iteration resumes, this function will immediately be called
3427 again, to examine the properties at the beginning of the string.
3429 When a display or overlay string is exhausted, the iterator state
3430 is popped (see 'pop_it'), and iteration continues with the
3431 previous object. Again, in many such cases this function is
3432 called again to find the next position where properties might
3433 change. */
3437 handled = HANDLED_NORMALLY;
3439 /* Call text property handlers. */
3440 for (p = it_props; p->handler; ++p)
3442 handled = p->handler (it);
3444 if (handled == HANDLED_RECOMPUTE_PROPS)
3445 break;
3446 else if (handled == HANDLED_RETURN)
3448 /* We still want to show before and after strings from
3449 overlays even if the actual buffer text is replaced. */
3450 if (!handle_overlay_change_p
3451 || it->sp > 1
3452 /* Don't call get_overlay_strings_1 if we already
3453 have overlay strings loaded, because doing so
3454 will load them again and push the iterator state
3455 onto the stack one more time, which is not
3456 expected by the rest of the code that processes
3457 overlay strings. */
3458 || (it->current.overlay_string_index < 0
3459 && !get_overlay_strings_1 (it, 0, false)))
3461 if (it->ellipsis_p)
3462 setup_for_ellipsis (it, 0);
3463 /* When handling a display spec, we might load an
3464 empty string. In that case, discard it here. We
3465 used to discard it in handle_single_display_spec,
3466 but that causes get_overlay_strings_1, above, to
3467 ignore overlay strings that we must check. */
3468 if (STRINGP (it->string) && !SCHARS (it->string))
3469 pop_it (it);
3470 return;
3472 else if (STRINGP (it->string) && !SCHARS (it->string))
3473 pop_it (it);
3474 else
3476 it->string_from_display_prop_p = false;
3477 it->from_disp_prop_p = false;
3478 handle_overlay_change_p = false;
3480 handled = HANDLED_RECOMPUTE_PROPS;
3481 break;
3483 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3484 handle_overlay_change_p = false;
3487 if (handled != HANDLED_RECOMPUTE_PROPS)
3489 /* Don't check for overlay strings below when set to deliver
3490 characters from a display vector. */
3491 if (it->method == GET_FROM_DISPLAY_VECTOR)
3492 handle_overlay_change_p = false;
3494 /* Handle overlay changes.
3495 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3496 if it finds overlays. */
3497 if (handle_overlay_change_p)
3498 handled = handle_overlay_change (it);
3501 if (it->ellipsis_p)
3503 setup_for_ellipsis (it, 0);
3504 break;
3507 while (handled == HANDLED_RECOMPUTE_PROPS);
3509 /* Determine where to stop next. */
3510 if (handled == HANDLED_NORMALLY)
3511 compute_stop_pos (it);
3515 /* Compute IT->stop_charpos from text property and overlay change
3516 information for IT's current position. */
3518 static void
3519 compute_stop_pos (struct it *it)
3521 register INTERVAL iv, next_iv;
3522 Lisp_Object object, limit, position;
3523 ptrdiff_t charpos, bytepos;
3525 if (STRINGP (it->string))
3527 /* Strings are usually short, so don't limit the search for
3528 properties. */
3529 it->stop_charpos = it->end_charpos;
3530 object = it->string;
3531 limit = Qnil;
3532 charpos = IT_STRING_CHARPOS (*it);
3533 bytepos = IT_STRING_BYTEPOS (*it);
3535 else
3537 ptrdiff_t pos;
3539 /* If end_charpos is out of range for some reason, such as a
3540 misbehaving display function, rationalize it (Bug#5984). */
3541 if (it->end_charpos > ZV)
3542 it->end_charpos = ZV;
3543 it->stop_charpos = it->end_charpos;
3545 /* If next overlay change is in front of the current stop pos
3546 (which is IT->end_charpos), stop there. Note: value of
3547 next_overlay_change is point-max if no overlay change
3548 follows. */
3549 charpos = IT_CHARPOS (*it);
3550 bytepos = IT_BYTEPOS (*it);
3551 pos = next_overlay_change (charpos);
3552 if (pos < it->stop_charpos)
3553 it->stop_charpos = pos;
3555 /* Set up variables for computing the stop position from text
3556 property changes. */
3557 XSETBUFFER (object, current_buffer);
3558 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3561 /* Get the interval containing IT's position. Value is a null
3562 interval if there isn't such an interval. */
3563 position = make_number (charpos);
3564 iv = validate_interval_range (object, &position, &position, false);
3565 if (iv)
3567 Lisp_Object values_here[LAST_PROP_IDX];
3568 struct props *p;
3570 /* Get properties here. */
3571 for (p = it_props; p->handler; ++p)
3572 values_here[p->idx] = textget (iv->plist,
3573 builtin_lisp_symbol (p->name));
3575 /* Look for an interval following iv that has different
3576 properties. */
3577 for (next_iv = next_interval (iv);
3578 (next_iv
3579 && (NILP (limit)
3580 || XFASTINT (limit) > next_iv->position));
3581 next_iv = next_interval (next_iv))
3583 for (p = it_props; p->handler; ++p)
3585 Lisp_Object new_value = textget (next_iv->plist,
3586 builtin_lisp_symbol (p->name));
3587 if (!EQ (values_here[p->idx], new_value))
3588 break;
3591 if (p->handler)
3592 break;
3595 if (next_iv)
3597 if (INTEGERP (limit)
3598 && next_iv->position >= XFASTINT (limit))
3599 /* No text property change up to limit. */
3600 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3601 else
3602 /* Text properties change in next_iv. */
3603 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3607 if (it->cmp_it.id < 0)
3609 ptrdiff_t stoppos = it->end_charpos;
3611 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3612 stoppos = -1;
3613 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3614 stoppos, it->string);
3617 eassert (STRINGP (it->string)
3618 || (it->stop_charpos >= BEGV
3619 && it->stop_charpos >= IT_CHARPOS (*it)));
3623 /* Return the position of the next overlay change after POS in
3624 current_buffer. Value is point-max if no overlay change
3625 follows. This is like `next-overlay-change' but doesn't use
3626 xmalloc. */
3628 static ptrdiff_t
3629 next_overlay_change (ptrdiff_t pos)
3631 ptrdiff_t i, noverlays;
3632 ptrdiff_t endpos;
3633 Lisp_Object *overlays;
3634 USE_SAFE_ALLOCA;
3636 /* Get all overlays at the given position. */
3637 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3639 /* If any of these overlays ends before endpos,
3640 use its ending point instead. */
3641 for (i = 0; i < noverlays; ++i)
3643 Lisp_Object oend;
3644 ptrdiff_t oendpos;
3646 oend = OVERLAY_END (overlays[i]);
3647 oendpos = OVERLAY_POSITION (oend);
3648 endpos = min (endpos, oendpos);
3651 SAFE_FREE ();
3652 return endpos;
3655 /* How many characters forward to search for a display property or
3656 display string. Searching too far forward makes the bidi display
3657 sluggish, especially in small windows. */
3658 #define MAX_DISP_SCAN 250
3660 /* Return the character position of a display string at or after
3661 position specified by POSITION. If no display string exists at or
3662 after POSITION, return ZV. A display string is either an overlay
3663 with `display' property whose value is a string, or a `display'
3664 text property whose value is a string. STRING is data about the
3665 string to iterate; if STRING->lstring is nil, we are iterating a
3666 buffer. FRAME_WINDOW_P is true when we are displaying a window
3667 on a GUI frame. DISP_PROP is set to zero if we searched
3668 MAX_DISP_SCAN characters forward without finding any display
3669 strings, non-zero otherwise. It is set to 2 if the display string
3670 uses any kind of `(space ...)' spec that will produce a stretch of
3671 white space in the text area. */
3672 ptrdiff_t
3673 compute_display_string_pos (struct text_pos *position,
3674 struct bidi_string_data *string,
3675 struct window *w,
3676 bool frame_window_p, int *disp_prop)
3678 /* OBJECT = nil means current buffer. */
3679 Lisp_Object object, object1;
3680 Lisp_Object pos, spec, limpos;
3681 bool string_p = string && (STRINGP (string->lstring) || string->s);
3682 ptrdiff_t eob = string_p ? string->schars : ZV;
3683 ptrdiff_t begb = string_p ? 0 : BEGV;
3684 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3685 ptrdiff_t lim =
3686 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3687 struct text_pos tpos;
3688 int rv = 0;
3690 if (string && STRINGP (string->lstring))
3691 object1 = object = string->lstring;
3692 else if (w && !string_p)
3694 XSETWINDOW (object, w);
3695 object1 = Qnil;
3697 else
3698 object1 = object = Qnil;
3700 *disp_prop = 1;
3702 if (charpos >= eob
3703 /* We don't support display properties whose values are strings
3704 that have display string properties. */
3705 || string->from_disp_str
3706 /* C strings cannot have display properties. */
3707 || (string->s && !STRINGP (object)))
3709 *disp_prop = 0;
3710 return eob;
3713 /* If the character at CHARPOS is where the display string begins,
3714 return CHARPOS. */
3715 pos = make_number (charpos);
3716 if (STRINGP (object))
3717 bufpos = string->bufpos;
3718 else
3719 bufpos = charpos;
3720 tpos = *position;
3721 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3722 && (charpos <= begb
3723 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3724 object),
3725 spec))
3726 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3727 frame_window_p)))
3729 if (rv == 2)
3730 *disp_prop = 2;
3731 return charpos;
3734 /* Look forward for the first character with a `display' property
3735 that will replace the underlying text when displayed. */
3736 limpos = make_number (lim);
3737 do {
3738 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3739 CHARPOS (tpos) = XFASTINT (pos);
3740 if (CHARPOS (tpos) >= lim)
3742 *disp_prop = 0;
3743 break;
3745 if (STRINGP (object))
3746 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3747 else
3748 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3749 spec = Fget_char_property (pos, Qdisplay, object);
3750 if (!STRINGP (object))
3751 bufpos = CHARPOS (tpos);
3752 } while (NILP (spec)
3753 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3754 bufpos, frame_window_p)));
3755 if (rv == 2)
3756 *disp_prop = 2;
3758 return CHARPOS (tpos);
3761 /* Return the character position of the end of the display string that
3762 started at CHARPOS. If there's no display string at CHARPOS,
3763 return -1. A display string is either an overlay with `display'
3764 property whose value is a string or a `display' text property whose
3765 value is a string. */
3766 ptrdiff_t
3767 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3769 /* OBJECT = nil means current buffer. */
3770 Lisp_Object object =
3771 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3772 Lisp_Object pos = make_number (charpos);
3773 ptrdiff_t eob =
3774 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3776 if (charpos >= eob || (string->s && !STRINGP (object)))
3777 return eob;
3779 /* It could happen that the display property or overlay was removed
3780 since we found it in compute_display_string_pos above. One way
3781 this can happen is if JIT font-lock was called (through
3782 handle_fontified_prop), and jit-lock-functions remove text
3783 properties or overlays from the portion of buffer that includes
3784 CHARPOS. Muse mode is known to do that, for example. In this
3785 case, we return -1 to the caller, to signal that no display
3786 string is actually present at CHARPOS. See bidi_fetch_char for
3787 how this is handled.
3789 An alternative would be to never look for display properties past
3790 it->stop_charpos. But neither compute_display_string_pos nor
3791 bidi_fetch_char that calls it know or care where the next
3792 stop_charpos is. */
3793 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3794 return -1;
3796 /* Look forward for the first character where the `display' property
3797 changes. */
3798 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3800 return XFASTINT (pos);
3805 /***********************************************************************
3806 Fontification
3807 ***********************************************************************/
3809 /* Handle changes in the `fontified' property of the current buffer by
3810 calling hook functions from Qfontification_functions to fontify
3811 regions of text. */
3813 static enum prop_handled
3814 handle_fontified_prop (struct it *it)
3816 Lisp_Object prop, pos;
3817 enum prop_handled handled = HANDLED_NORMALLY;
3819 if (!NILP (Vmemory_full))
3820 return handled;
3822 /* Get the value of the `fontified' property at IT's current buffer
3823 position. (The `fontified' property doesn't have a special
3824 meaning in strings.) If the value is nil, call functions from
3825 Qfontification_functions. */
3826 if (!STRINGP (it->string)
3827 && it->s == NULL
3828 && !NILP (Vfontification_functions)
3829 && !NILP (Vrun_hooks)
3830 && (pos = make_number (IT_CHARPOS (*it)),
3831 prop = Fget_char_property (pos, Qfontified, Qnil),
3832 /* Ignore the special cased nil value always present at EOB since
3833 no amount of fontifying will be able to change it. */
3834 NILP (prop) && IT_CHARPOS (*it) < Z))
3836 ptrdiff_t count = SPECPDL_INDEX ();
3837 Lisp_Object val;
3838 struct buffer *obuf = current_buffer;
3839 ptrdiff_t begv = BEGV, zv = ZV;
3840 bool old_clip_changed = current_buffer->clip_changed;
3842 val = Vfontification_functions;
3843 specbind (Qfontification_functions, Qnil);
3845 eassert (it->end_charpos == ZV);
3847 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3848 safe_call1 (val, pos);
3849 else
3851 Lisp_Object fns, fn;
3853 fns = Qnil;
3855 for (; CONSP (val); val = XCDR (val))
3857 fn = XCAR (val);
3859 if (EQ (fn, Qt))
3861 /* A value of t indicates this hook has a local
3862 binding; it means to run the global binding too.
3863 In a global value, t should not occur. If it
3864 does, we must ignore it to avoid an endless
3865 loop. */
3866 for (fns = Fdefault_value (Qfontification_functions);
3867 CONSP (fns);
3868 fns = XCDR (fns))
3870 fn = XCAR (fns);
3871 if (!EQ (fn, Qt))
3872 safe_call1 (fn, pos);
3875 else
3876 safe_call1 (fn, pos);
3880 unbind_to (count, Qnil);
3882 /* Fontification functions routinely call `save-restriction'.
3883 Normally, this tags clip_changed, which can confuse redisplay
3884 (see discussion in Bug#6671). Since we don't perform any
3885 special handling of fontification changes in the case where
3886 `save-restriction' isn't called, there's no point doing so in
3887 this case either. So, if the buffer's restrictions are
3888 actually left unchanged, reset clip_changed. */
3889 if (obuf == current_buffer)
3891 if (begv == BEGV && zv == ZV)
3892 current_buffer->clip_changed = old_clip_changed;
3894 /* There isn't much we can reasonably do to protect against
3895 misbehaving fontification, but here's a fig leaf. */
3896 else if (BUFFER_LIVE_P (obuf))
3897 set_buffer_internal_1 (obuf);
3899 /* The fontification code may have added/removed text.
3900 It could do even a lot worse, but let's at least protect against
3901 the most obvious case where only the text past `pos' gets changed',
3902 as is/was done in grep.el where some escapes sequences are turned
3903 into face properties (bug#7876). */
3904 it->end_charpos = ZV;
3906 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3907 something. This avoids an endless loop if they failed to
3908 fontify the text for which reason ever. */
3909 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3910 handled = HANDLED_RECOMPUTE_PROPS;
3913 return handled;
3918 /***********************************************************************
3919 Faces
3920 ***********************************************************************/
3922 /* Set up iterator IT from face properties at its current position.
3923 Called from handle_stop. */
3925 static enum prop_handled
3926 handle_face_prop (struct it *it)
3928 int new_face_id;
3929 ptrdiff_t next_stop;
3931 if (!STRINGP (it->string))
3933 new_face_id
3934 = face_at_buffer_position (it->w,
3935 IT_CHARPOS (*it),
3936 &next_stop,
3937 (IT_CHARPOS (*it)
3938 + TEXT_PROP_DISTANCE_LIMIT),
3939 false, it->base_face_id);
3941 /* Is this a start of a run of characters with box face?
3942 Caveat: this can be called for a freshly initialized
3943 iterator; face_id is -1 in this case. We know that the new
3944 face will not change until limit, i.e. if the new face has a
3945 box, all characters up to limit will have one. But, as
3946 usual, we don't know whether limit is really the end. */
3947 if (new_face_id != it->face_id)
3949 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3950 /* If it->face_id is -1, old_face below will be NULL, see
3951 the definition of FACE_FROM_ID_OR_NULL. This will happen
3952 if this is the initial call that gets the face. */
3953 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3955 /* If the value of face_id of the iterator is -1, we have to
3956 look in front of IT's position and see whether there is a
3957 face there that's different from new_face_id. */
3958 if (!old_face && IT_CHARPOS (*it) > BEG)
3960 int prev_face_id = face_before_it_pos (it);
3962 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3965 /* If the new face has a box, but the old face does not,
3966 this is the start of a run of characters with box face,
3967 i.e. this character has a shadow on the left side. */
3968 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3969 && (old_face == NULL || !old_face->box));
3970 it->face_box_p = new_face->box != FACE_NO_BOX;
3973 else
3975 int base_face_id;
3976 ptrdiff_t bufpos;
3977 int i;
3978 Lisp_Object from_overlay
3979 = (it->current.overlay_string_index >= 0
3980 ? it->string_overlays[it->current.overlay_string_index
3981 % OVERLAY_STRING_CHUNK_SIZE]
3982 : Qnil);
3984 /* See if we got to this string directly or indirectly from
3985 an overlay property. That includes the before-string or
3986 after-string of an overlay, strings in display properties
3987 provided by an overlay, their text properties, etc.
3989 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3990 if (! NILP (from_overlay))
3991 for (i = it->sp - 1; i >= 0; i--)
3993 if (it->stack[i].current.overlay_string_index >= 0)
3994 from_overlay
3995 = it->string_overlays[it->stack[i].current.overlay_string_index
3996 % OVERLAY_STRING_CHUNK_SIZE];
3997 else if (! NILP (it->stack[i].from_overlay))
3998 from_overlay = it->stack[i].from_overlay;
4000 if (!NILP (from_overlay))
4001 break;
4004 if (! NILP (from_overlay))
4006 bufpos = IT_CHARPOS (*it);
4007 /* For a string from an overlay, the base face depends
4008 only on text properties and ignores overlays. */
4009 base_face_id
4010 = face_for_overlay_string (it->w,
4011 IT_CHARPOS (*it),
4012 &next_stop,
4013 (IT_CHARPOS (*it)
4014 + TEXT_PROP_DISTANCE_LIMIT),
4015 false,
4016 from_overlay);
4018 else
4020 bufpos = 0;
4022 /* For strings from a `display' property, use the face at
4023 IT's current buffer position as the base face to merge
4024 with, so that overlay strings appear in the same face as
4025 surrounding text, unless they specify their own faces.
4026 For strings from wrap-prefix and line-prefix properties,
4027 use the default face, possibly remapped via
4028 Vface_remapping_alist. */
4029 /* Note that the fact that we use the face at _buffer_
4030 position means that a 'display' property on an overlay
4031 string will not inherit the face of that overlay string,
4032 but will instead revert to the face of buffer text
4033 covered by the overlay. This is visible, e.g., when the
4034 overlay specifies a box face, but neither the buffer nor
4035 the display string do. This sounds like a design bug,
4036 but Emacs always did that since v21.1, so changing that
4037 might be a big deal. */
4038 base_face_id = it->string_from_prefix_prop_p
4039 ? (!NILP (Vface_remapping_alist)
4040 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4041 : DEFAULT_FACE_ID)
4042 : underlying_face_id (it);
4045 new_face_id = face_at_string_position (it->w,
4046 it->string,
4047 IT_STRING_CHARPOS (*it),
4048 bufpos,
4049 &next_stop,
4050 base_face_id, false);
4052 /* Is this a start of a run of characters with box? Caveat:
4053 this can be called for a freshly allocated iterator; face_id
4054 is -1 is this case. We know that the new face will not
4055 change until the next check pos, i.e. if the new face has a
4056 box, all characters up to that position will have a
4057 box. But, as usual, we don't know whether that position
4058 is really the end. */
4059 if (new_face_id != it->face_id)
4061 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4062 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4064 /* If new face has a box but old face hasn't, this is the
4065 start of a run of characters with box, i.e. it has a
4066 shadow on the left side. */
4067 it->start_of_box_run_p
4068 = new_face->box && (old_face == NULL || !old_face->box);
4069 it->face_box_p = new_face->box != FACE_NO_BOX;
4073 it->face_id = new_face_id;
4074 return HANDLED_NORMALLY;
4078 /* Return the ID of the face ``underlying'' IT's current position,
4079 which is in a string. If the iterator is associated with a
4080 buffer, return the face at IT's current buffer position.
4081 Otherwise, use the iterator's base_face_id. */
4083 static int
4084 underlying_face_id (struct it *it)
4086 int face_id = it->base_face_id, i;
4088 eassert (STRINGP (it->string));
4090 for (i = it->sp - 1; i >= 0; --i)
4091 if (NILP (it->stack[i].string))
4092 face_id = it->stack[i].face_id;
4094 return face_id;
4098 /* Compute the face one character before or after the current position
4099 of IT, in the visual order. BEFORE_P means get the face
4100 in front (to the left in L2R paragraphs, to the right in R2L
4101 paragraphs) of IT's screen position. Value is the ID of the face. */
4103 static int
4104 face_before_or_after_it_pos (struct it *it, bool before_p)
4106 int face_id, limit;
4107 ptrdiff_t next_check_charpos;
4108 struct it it_copy;
4109 void *it_copy_data = NULL;
4111 eassert (it->s == NULL);
4113 if (STRINGP (it->string))
4115 ptrdiff_t bufpos, charpos;
4116 int base_face_id;
4118 /* No face change past the end of the string (for the case
4119 we are padding with spaces). No face change before the
4120 string start. */
4121 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4122 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4123 return it->face_id;
4125 if (!it->bidi_p)
4127 /* Set charpos to the position before or after IT's current
4128 position, in the logical order, which in the non-bidi
4129 case is the same as the visual order. */
4130 if (before_p)
4131 charpos = IT_STRING_CHARPOS (*it) - 1;
4132 else if (it->what == IT_COMPOSITION)
4133 /* For composition, we must check the character after the
4134 composition. */
4135 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4136 else
4137 charpos = IT_STRING_CHARPOS (*it) + 1;
4139 else
4141 if (before_p)
4143 /* With bidi iteration, the character before the current
4144 in the visual order cannot be found by simple
4145 iteration, because "reverse" reordering is not
4146 supported. Instead, we need to start from the string
4147 beginning and go all the way to the current string
4148 position, remembering the previous position. */
4149 /* Ignore face changes before the first visible
4150 character on this display line. */
4151 if (it->current_x <= it->first_visible_x)
4152 return it->face_id;
4153 SAVE_IT (it_copy, *it, it_copy_data);
4154 IT_STRING_CHARPOS (it_copy) = 0;
4155 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4159 charpos = IT_STRING_CHARPOS (it_copy);
4160 if (charpos >= SCHARS (it->string))
4161 break;
4162 bidi_move_to_visually_next (&it_copy.bidi_it);
4164 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4166 RESTORE_IT (it, it, it_copy_data);
4168 else
4170 /* Set charpos to the string position of the character
4171 that comes after IT's current position in the visual
4172 order. */
4173 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4175 it_copy = *it;
4176 while (n--)
4177 bidi_move_to_visually_next (&it_copy.bidi_it);
4179 charpos = it_copy.bidi_it.charpos;
4182 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4184 if (it->current.overlay_string_index >= 0)
4185 bufpos = IT_CHARPOS (*it);
4186 else
4187 bufpos = 0;
4189 base_face_id = underlying_face_id (it);
4191 /* Get the face for ASCII, or unibyte. */
4192 face_id = face_at_string_position (it->w,
4193 it->string,
4194 charpos,
4195 bufpos,
4196 &next_check_charpos,
4197 base_face_id, false);
4199 /* Correct the face for charsets different from ASCII. Do it
4200 for the multibyte case only. The face returned above is
4201 suitable for unibyte text if IT->string is unibyte. */
4202 if (STRING_MULTIBYTE (it->string))
4204 struct text_pos pos1 = string_pos (charpos, it->string);
4205 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4206 int c, len;
4207 struct face *face = FACE_FROM_ID (it->f, face_id);
4209 c = string_char_and_length (p, &len);
4210 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4213 else
4215 struct text_pos pos;
4217 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4218 || (IT_CHARPOS (*it) <= BEGV && before_p))
4219 return it->face_id;
4221 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4222 pos = it->current.pos;
4224 if (!it->bidi_p)
4226 if (before_p)
4227 DEC_TEXT_POS (pos, it->multibyte_p);
4228 else
4230 if (it->what == IT_COMPOSITION)
4232 /* For composition, we must check the position after
4233 the composition. */
4234 pos.charpos += it->cmp_it.nchars;
4235 pos.bytepos += it->len;
4237 else
4238 INC_TEXT_POS (pos, it->multibyte_p);
4241 else
4243 if (before_p)
4245 int current_x;
4247 /* With bidi iteration, the character before the current
4248 in the visual order cannot be found by simple
4249 iteration, because "reverse" reordering is not
4250 supported. Instead, we need to use the move_it_*
4251 family of functions, and move to the previous
4252 character starting from the beginning of the visual
4253 line. */
4254 /* Ignore face changes before the first visible
4255 character on this display line. */
4256 if (it->current_x <= it->first_visible_x)
4257 return it->face_id;
4258 SAVE_IT (it_copy, *it, it_copy_data);
4259 /* Implementation note: Since move_it_in_display_line
4260 works in the iterator geometry, and thinks the first
4261 character is always the leftmost, even in R2L lines,
4262 we don't need to distinguish between the R2L and L2R
4263 cases here. */
4264 current_x = it_copy.current_x;
4265 move_it_vertically_backward (&it_copy, 0);
4266 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4267 pos = it_copy.current.pos;
4268 RESTORE_IT (it, it, it_copy_data);
4270 else
4272 /* Set charpos to the buffer position of the character
4273 that comes after IT's current position in the visual
4274 order. */
4275 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4277 it_copy = *it;
4278 while (n--)
4279 bidi_move_to_visually_next (&it_copy.bidi_it);
4281 SET_TEXT_POS (pos,
4282 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4285 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4287 /* Determine face for CHARSET_ASCII, or unibyte. */
4288 face_id = face_at_buffer_position (it->w,
4289 CHARPOS (pos),
4290 &next_check_charpos,
4291 limit, false, -1);
4293 /* Correct the face for charsets different from ASCII. Do it
4294 for the multibyte case only. The face returned above is
4295 suitable for unibyte text if current_buffer is unibyte. */
4296 if (it->multibyte_p)
4298 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4299 struct face *face = FACE_FROM_ID (it->f, face_id);
4300 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4304 return face_id;
4309 /***********************************************************************
4310 Invisible text
4311 ***********************************************************************/
4313 /* Set up iterator IT from invisible properties at its current
4314 position. Called from handle_stop. */
4316 static enum prop_handled
4317 handle_invisible_prop (struct it *it)
4319 enum prop_handled handled = HANDLED_NORMALLY;
4320 int invis;
4321 Lisp_Object prop;
4323 if (STRINGP (it->string))
4325 Lisp_Object end_charpos, limit;
4327 /* Get the value of the invisible text property at the
4328 current position. Value will be nil if there is no such
4329 property. */
4330 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4331 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4332 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4334 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4336 /* Record whether we have to display an ellipsis for the
4337 invisible text. */
4338 bool display_ellipsis_p = (invis == 2);
4339 ptrdiff_t len, endpos;
4341 handled = HANDLED_RECOMPUTE_PROPS;
4343 /* Get the position at which the next visible text can be
4344 found in IT->string, if any. */
4345 endpos = len = SCHARS (it->string);
4346 XSETINT (limit, len);
4349 end_charpos
4350 = Fnext_single_property_change (end_charpos, Qinvisible,
4351 it->string, limit);
4352 /* Since LIMIT is always an integer, so should be the
4353 value returned by Fnext_single_property_change. */
4354 eassert (INTEGERP (end_charpos));
4355 if (INTEGERP (end_charpos))
4357 endpos = XFASTINT (end_charpos);
4358 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4359 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4360 if (invis == 2)
4361 display_ellipsis_p = true;
4363 else /* Should never happen; but if it does, exit the loop. */
4364 endpos = len;
4366 while (invis != 0 && endpos < len);
4368 if (display_ellipsis_p)
4369 it->ellipsis_p = true;
4371 if (endpos < len)
4373 /* Text at END_CHARPOS is visible. Move IT there. */
4374 struct text_pos old;
4375 ptrdiff_t oldpos;
4377 old = it->current.string_pos;
4378 oldpos = CHARPOS (old);
4379 if (it->bidi_p)
4381 if (it->bidi_it.first_elt
4382 && it->bidi_it.charpos < SCHARS (it->string))
4383 bidi_paragraph_init (it->paragraph_embedding,
4384 &it->bidi_it, true);
4385 /* Bidi-iterate out of the invisible text. */
4388 bidi_move_to_visually_next (&it->bidi_it);
4390 while (oldpos <= it->bidi_it.charpos
4391 && it->bidi_it.charpos < endpos
4392 && it->bidi_it.charpos < it->bidi_it.string.schars);
4394 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4395 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4396 if (IT_CHARPOS (*it) >= endpos)
4397 it->prev_stop = endpos;
4399 else
4401 IT_STRING_CHARPOS (*it) = endpos;
4402 compute_string_pos (&it->current.string_pos, old, it->string);
4405 else
4407 /* The rest of the string is invisible. If this is an
4408 overlay string, proceed with the next overlay string
4409 or whatever comes and return a character from there. */
4410 if (it->current.overlay_string_index >= 0
4411 && !display_ellipsis_p)
4413 next_overlay_string (it);
4414 /* Don't check for overlay strings when we just
4415 finished processing them. */
4416 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4418 else
4420 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4421 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4426 else
4428 ptrdiff_t newpos, next_stop, start_charpos, tem;
4429 Lisp_Object pos, overlay;
4431 /* First of all, is there invisible text at this position? */
4432 tem = start_charpos = IT_CHARPOS (*it);
4433 pos = make_number (tem);
4434 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4435 &overlay);
4436 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4438 /* If we are on invisible text, skip over it. */
4439 if (invis != 0 && start_charpos < it->end_charpos)
4441 /* Record whether we have to display an ellipsis for the
4442 invisible text. */
4443 bool display_ellipsis_p = invis == 2;
4445 handled = HANDLED_RECOMPUTE_PROPS;
4447 /* Loop skipping over invisible text. The loop is left at
4448 ZV or with IT on the first char being visible again. */
4451 /* Try to skip some invisible text. Return value is the
4452 position reached which can be equal to where we start
4453 if there is nothing invisible there. This skips both
4454 over invisible text properties and overlays with
4455 invisible property. */
4456 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4458 /* If we skipped nothing at all we weren't at invisible
4459 text in the first place. If everything to the end of
4460 the buffer was skipped, end the loop. */
4461 if (newpos == tem || newpos >= ZV)
4462 invis = 0;
4463 else
4465 /* We skipped some characters but not necessarily
4466 all there are. Check if we ended up on visible
4467 text. Fget_char_property returns the property of
4468 the char before the given position, i.e. if we
4469 get invis = 0, this means that the char at
4470 newpos is visible. */
4471 pos = make_number (newpos);
4472 prop = Fget_char_property (pos, Qinvisible, it->window);
4473 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4476 /* If we ended up on invisible text, proceed to
4477 skip starting with next_stop. */
4478 if (invis != 0)
4479 tem = next_stop;
4481 /* If there are adjacent invisible texts, don't lose the
4482 second one's ellipsis. */
4483 if (invis == 2)
4484 display_ellipsis_p = true;
4486 while (invis != 0);
4488 /* The position newpos is now either ZV or on visible text. */
4489 if (it->bidi_p)
4491 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4492 bool on_newline
4493 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4494 bool after_newline
4495 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4497 /* If the invisible text ends on a newline or on a
4498 character after a newline, we can avoid the costly,
4499 character by character, bidi iteration to NEWPOS, and
4500 instead simply reseat the iterator there. That's
4501 because all bidi reordering information is tossed at
4502 the newline. This is a big win for modes that hide
4503 complete lines, like Outline, Org, etc. */
4504 if (on_newline || after_newline)
4506 struct text_pos tpos;
4507 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4509 SET_TEXT_POS (tpos, newpos, bpos);
4510 reseat_1 (it, tpos, false);
4511 /* If we reseat on a newline/ZV, we need to prep the
4512 bidi iterator for advancing to the next character
4513 after the newline/EOB, keeping the current paragraph
4514 direction (so that PRODUCE_GLYPHS does TRT wrt
4515 prepending/appending glyphs to a glyph row). */
4516 if (on_newline)
4518 it->bidi_it.first_elt = false;
4519 it->bidi_it.paragraph_dir = pdir;
4520 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4521 it->bidi_it.nchars = 1;
4522 it->bidi_it.ch_len = 1;
4525 else /* Must use the slow method. */
4527 /* With bidi iteration, the region of invisible text
4528 could start and/or end in the middle of a
4529 non-base embedding level. Therefore, we need to
4530 skip invisible text using the bidi iterator,
4531 starting at IT's current position, until we find
4532 ourselves outside of the invisible text.
4533 Skipping invisible text _after_ bidi iteration
4534 avoids affecting the visual order of the
4535 displayed text when invisible properties are
4536 added or removed. */
4537 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4539 /* If we were `reseat'ed to a new paragraph,
4540 determine the paragraph base direction. We
4541 need to do it now because
4542 next_element_from_buffer may not have a
4543 chance to do it, if we are going to skip any
4544 text at the beginning, which resets the
4545 FIRST_ELT flag. */
4546 bidi_paragraph_init (it->paragraph_embedding,
4547 &it->bidi_it, true);
4551 bidi_move_to_visually_next (&it->bidi_it);
4553 while (it->stop_charpos <= it->bidi_it.charpos
4554 && it->bidi_it.charpos < newpos);
4555 IT_CHARPOS (*it) = it->bidi_it.charpos;
4556 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4557 /* If we overstepped NEWPOS, record its position in
4558 the iterator, so that we skip invisible text if
4559 later the bidi iteration lands us in the
4560 invisible region again. */
4561 if (IT_CHARPOS (*it) >= newpos)
4562 it->prev_stop = newpos;
4565 else
4567 IT_CHARPOS (*it) = newpos;
4568 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4571 if (display_ellipsis_p)
4573 /* Make sure that the glyphs of the ellipsis will get
4574 correct `charpos' values. If we would not update
4575 it->position here, the glyphs would belong to the
4576 last visible character _before_ the invisible
4577 text, which confuses `set_cursor_from_row'.
4579 We use the last invisible position instead of the
4580 first because this way the cursor is always drawn on
4581 the first "." of the ellipsis, whenever PT is inside
4582 the invisible text. Otherwise the cursor would be
4583 placed _after_ the ellipsis when the point is after the
4584 first invisible character. */
4585 if (!STRINGP (it->object))
4587 it->position.charpos = newpos - 1;
4588 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4592 /* If there are before-strings at the start of invisible
4593 text, and the text is invisible because of a text
4594 property, arrange to show before-strings because 20.x did
4595 it that way. (If the text is invisible because of an
4596 overlay property instead of a text property, this is
4597 already handled in the overlay code.) */
4598 if (NILP (overlay)
4599 && get_overlay_strings (it, it->stop_charpos))
4601 handled = HANDLED_RECOMPUTE_PROPS;
4602 if (it->sp > 0)
4604 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4605 /* The call to get_overlay_strings above recomputes
4606 it->stop_charpos, but it only considers changes
4607 in properties and overlays beyond iterator's
4608 current position. This causes us to miss changes
4609 that happen exactly where the invisible property
4610 ended. So we play it safe here and force the
4611 iterator to check for potential stop positions
4612 immediately after the invisible text. Note that
4613 if get_overlay_strings returns true, it
4614 normally also pushed the iterator stack, so we
4615 need to update the stop position in the slot
4616 below the current one. */
4617 it->stack[it->sp - 1].stop_charpos
4618 = CHARPOS (it->stack[it->sp - 1].current.pos);
4621 else if (display_ellipsis_p)
4623 it->ellipsis_p = true;
4624 /* Let the ellipsis display before
4625 considering any properties of the following char.
4626 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4627 handled = HANDLED_RETURN;
4632 return handled;
4636 /* Make iterator IT return `...' next.
4637 Replaces LEN characters from buffer. */
4639 static void
4640 setup_for_ellipsis (struct it *it, int len)
4642 /* Use the display table definition for `...'. Invalid glyphs
4643 will be handled by the method returning elements from dpvec. */
4644 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4646 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4647 it->dpvec = v->contents;
4648 it->dpend = v->contents + v->header.size;
4650 else
4652 /* Default `...'. */
4653 it->dpvec = default_invis_vector;
4654 it->dpend = default_invis_vector + 3;
4657 it->dpvec_char_len = len;
4658 it->current.dpvec_index = 0;
4659 it->dpvec_face_id = -1;
4661 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4662 face as the preceding text. IT->saved_face_id was set in
4663 handle_stop to the face of the preceding character, and will be
4664 different from IT->face_id only if the invisible text skipped in
4665 handle_invisible_prop has some non-default face on its first
4666 character. We thus ignore the face of the invisible text when we
4667 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4668 if (it->saved_face_id >= 0)
4669 it->face_id = it->saved_face_id;
4671 /* If the ellipsis represents buffer text, it means we advanced in
4672 the buffer, so we should no longer ignore overlay strings. */
4673 if (it->method == GET_FROM_BUFFER)
4674 it->ignore_overlay_strings_at_pos_p = false;
4676 it->method = GET_FROM_DISPLAY_VECTOR;
4677 it->ellipsis_p = true;
4682 /***********************************************************************
4683 'display' property
4684 ***********************************************************************/
4686 /* Set up iterator IT from `display' property at its current position.
4687 Called from handle_stop.
4688 We return HANDLED_RETURN if some part of the display property
4689 overrides the display of the buffer text itself.
4690 Otherwise we return HANDLED_NORMALLY. */
4692 static enum prop_handled
4693 handle_display_prop (struct it *it)
4695 Lisp_Object propval, object, overlay;
4696 struct text_pos *position;
4697 ptrdiff_t bufpos;
4698 /* Nonzero if some property replaces the display of the text itself. */
4699 int display_replaced = 0;
4701 if (STRINGP (it->string))
4703 object = it->string;
4704 position = &it->current.string_pos;
4705 bufpos = CHARPOS (it->current.pos);
4707 else
4709 XSETWINDOW (object, it->w);
4710 position = &it->current.pos;
4711 bufpos = CHARPOS (*position);
4714 /* Reset those iterator values set from display property values. */
4715 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4716 it->space_width = Qnil;
4717 it->font_height = Qnil;
4718 it->voffset = 0;
4720 /* We don't support recursive `display' properties, i.e. string
4721 values that have a string `display' property, that have a string
4722 `display' property etc. */
4723 if (!it->string_from_display_prop_p)
4724 it->area = TEXT_AREA;
4726 propval = get_char_property_and_overlay (make_number (position->charpos),
4727 Qdisplay, object, &overlay);
4728 if (NILP (propval))
4729 return HANDLED_NORMALLY;
4730 /* Now OVERLAY is the overlay that gave us this property, or nil
4731 if it was a text property. */
4733 if (!STRINGP (it->string))
4734 object = it->w->contents;
4736 display_replaced = handle_display_spec (it, propval, object, overlay,
4737 position, bufpos,
4738 FRAME_WINDOW_P (it->f));
4739 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4742 /* Subroutine of handle_display_prop. Returns non-zero if the display
4743 specification in SPEC is a replacing specification, i.e. it would
4744 replace the text covered by `display' property with something else,
4745 such as an image or a display string. If SPEC includes any kind or
4746 `(space ...) specification, the value is 2; this is used by
4747 compute_display_string_pos, which see.
4749 See handle_single_display_spec for documentation of arguments.
4750 FRAME_WINDOW_P is true if the window being redisplayed is on a
4751 GUI frame; this argument is used only if IT is NULL, see below.
4753 IT can be NULL, if this is called by the bidi reordering code
4754 through compute_display_string_pos, which see. In that case, this
4755 function only examines SPEC, but does not otherwise "handle" it, in
4756 the sense that it doesn't set up members of IT from the display
4757 spec. */
4758 static int
4759 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4760 Lisp_Object overlay, struct text_pos *position,
4761 ptrdiff_t bufpos, bool frame_window_p)
4763 int replacing = 0;
4764 bool enable_eval = true;
4766 /* Support (disable-eval PROP) which is used by enriched.el. */
4767 if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
4769 enable_eval = false;
4770 spec = XCAR (XCDR (spec));
4773 if (CONSP (spec)
4774 /* Simple specifications. */
4775 && !EQ (XCAR (spec), Qimage)
4776 #ifdef HAVE_XWIDGETS
4777 && !EQ (XCAR (spec), Qxwidget)
4778 #endif
4779 && !EQ (XCAR (spec), Qspace)
4780 && !EQ (XCAR (spec), Qwhen)
4781 && !EQ (XCAR (spec), Qslice)
4782 && !EQ (XCAR (spec), Qspace_width)
4783 && !EQ (XCAR (spec), Qheight)
4784 && !EQ (XCAR (spec), Qraise)
4785 /* Marginal area specifications. */
4786 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4787 && !EQ (XCAR (spec), Qleft_fringe)
4788 && !EQ (XCAR (spec), Qright_fringe)
4789 && !NILP (XCAR (spec)))
4791 for (; CONSP (spec); spec = XCDR (spec))
4793 int rv = handle_single_display_spec (it, XCAR (spec), object,
4794 overlay, position, bufpos,
4795 replacing, frame_window_p,
4796 enable_eval);
4797 if (rv != 0)
4799 replacing = rv;
4800 /* If some text in a string is replaced, `position' no
4801 longer points to the position of `object'. */
4802 if (!it || STRINGP (object))
4803 break;
4807 else if (VECTORP (spec))
4809 ptrdiff_t i;
4810 for (i = 0; i < ASIZE (spec); ++i)
4812 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4813 overlay, position, bufpos,
4814 replacing, frame_window_p,
4815 enable_eval);
4816 if (rv != 0)
4818 replacing = rv;
4819 /* If some text in a string is replaced, `position' no
4820 longer points to the position of `object'. */
4821 if (!it || STRINGP (object))
4822 break;
4826 else
4827 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4828 bufpos, 0, frame_window_p,
4829 enable_eval);
4830 return replacing;
4833 /* Value is the position of the end of the `display' property starting
4834 at START_POS in OBJECT. */
4836 static struct text_pos
4837 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4839 Lisp_Object end;
4840 struct text_pos end_pos;
4842 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4843 Qdisplay, object, Qnil);
4844 CHARPOS (end_pos) = XFASTINT (end);
4845 if (STRINGP (object))
4846 compute_string_pos (&end_pos, start_pos, it->string);
4847 else
4848 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4850 return end_pos;
4854 /* Set up IT from a single `display' property specification SPEC. OBJECT
4855 is the object in which the `display' property was found. *POSITION
4856 is the position in OBJECT at which the `display' property was found.
4857 BUFPOS is the buffer position of OBJECT (different from POSITION if
4858 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4859 previously saw a display specification which already replaced text
4860 display with something else, for example an image; we ignore such
4861 properties after the first one has been processed.
4863 OVERLAY is the overlay this `display' property came from,
4864 or nil if it was a text property.
4866 If SPEC is a `space' or `image' specification, and in some other
4867 cases too, set *POSITION to the position where the `display'
4868 property ends.
4870 If IT is NULL, only examine the property specification in SPEC, but
4871 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4872 is intended to be displayed in a window on a GUI frame.
4874 Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
4876 Value is non-zero if something was found which replaces the display
4877 of buffer or string text. */
4879 static int
4880 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4881 Lisp_Object overlay, struct text_pos *position,
4882 ptrdiff_t bufpos, int display_replaced,
4883 bool frame_window_p, bool enable_eval_p)
4885 Lisp_Object form;
4886 Lisp_Object location, value;
4887 struct text_pos start_pos = *position;
4889 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4890 If the result is non-nil, use VALUE instead of SPEC. */
4891 form = Qt;
4892 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4894 spec = XCDR (spec);
4895 if (!CONSP (spec))
4896 return 0;
4897 form = XCAR (spec);
4898 spec = XCDR (spec);
4901 if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
4902 form = Qnil;
4903 if (!NILP (form) && !EQ (form, Qt))
4905 ptrdiff_t count = SPECPDL_INDEX ();
4907 /* Bind `object' to the object having the `display' property, a
4908 buffer or string. Bind `position' to the position in the
4909 object where the property was found, and `buffer-position'
4910 to the current position in the buffer. */
4912 if (NILP (object))
4913 XSETBUFFER (object, current_buffer);
4914 specbind (Qobject, object);
4915 specbind (Qposition, make_number (CHARPOS (*position)));
4916 specbind (Qbuffer_position, make_number (bufpos));
4917 form = safe_eval (form);
4918 unbind_to (count, Qnil);
4921 if (NILP (form))
4922 return 0;
4924 /* Handle `(height HEIGHT)' specifications. */
4925 if (CONSP (spec)
4926 && EQ (XCAR (spec), Qheight)
4927 && CONSP (XCDR (spec)))
4929 if (it)
4931 if (!FRAME_WINDOW_P (it->f))
4932 return 0;
4934 it->font_height = XCAR (XCDR (spec));
4935 if (!NILP (it->font_height))
4937 int new_height = -1;
4939 if (CONSP (it->font_height)
4940 && (EQ (XCAR (it->font_height), Qplus)
4941 || EQ (XCAR (it->font_height), Qminus))
4942 && CONSP (XCDR (it->font_height))
4943 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4945 /* `(+ N)' or `(- N)' where N is an integer. */
4946 int steps = XINT (XCAR (XCDR (it->font_height)));
4947 if (EQ (XCAR (it->font_height), Qplus))
4948 steps = - steps;
4949 it->face_id = smaller_face (it->f, it->face_id, steps);
4951 else if (FUNCTIONP (it->font_height) && enable_eval_p)
4953 /* Call function with current height as argument.
4954 Value is the new height. */
4955 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4956 Lisp_Object height;
4957 height = safe_call1 (it->font_height,
4958 face->lface[LFACE_HEIGHT_INDEX]);
4959 if (NUMBERP (height))
4960 new_height = XFLOATINT (height);
4962 else if (NUMBERP (it->font_height))
4964 /* Value is a multiple of the canonical char height. */
4965 struct face *f;
4967 f = FACE_FROM_ID (it->f,
4968 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4969 new_height = (XFLOATINT (it->font_height)
4970 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4972 else if (enable_eval_p)
4974 /* Evaluate IT->font_height with `height' bound to the
4975 current specified height to get the new height. */
4976 ptrdiff_t count = SPECPDL_INDEX ();
4977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4979 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4980 value = safe_eval (it->font_height);
4981 unbind_to (count, Qnil);
4983 if (NUMBERP (value))
4984 new_height = XFLOATINT (value);
4987 if (new_height > 0)
4988 it->face_id = face_with_height (it->f, it->face_id, new_height);
4992 return 0;
4995 /* Handle `(space-width WIDTH)'. */
4996 if (CONSP (spec)
4997 && EQ (XCAR (spec), Qspace_width)
4998 && CONSP (XCDR (spec)))
5000 if (it)
5002 if (!FRAME_WINDOW_P (it->f))
5003 return 0;
5005 value = XCAR (XCDR (spec));
5006 if (NUMBERP (value) && XFLOATINT (value) > 0)
5007 it->space_width = value;
5010 return 0;
5013 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5014 if (CONSP (spec)
5015 && EQ (XCAR (spec), Qslice))
5017 Lisp_Object tem;
5019 if (it)
5021 if (!FRAME_WINDOW_P (it->f))
5022 return 0;
5024 if (tem = XCDR (spec), CONSP (tem))
5026 it->slice.x = XCAR (tem);
5027 if (tem = XCDR (tem), CONSP (tem))
5029 it->slice.y = XCAR (tem);
5030 if (tem = XCDR (tem), CONSP (tem))
5032 it->slice.width = XCAR (tem);
5033 if (tem = XCDR (tem), CONSP (tem))
5034 it->slice.height = XCAR (tem);
5040 return 0;
5043 /* Handle `(raise FACTOR)'. */
5044 if (CONSP (spec)
5045 && EQ (XCAR (spec), Qraise)
5046 && CONSP (XCDR (spec)))
5048 if (it)
5050 if (!FRAME_WINDOW_P (it->f))
5051 return 0;
5053 #ifdef HAVE_WINDOW_SYSTEM
5054 value = XCAR (XCDR (spec));
5055 if (NUMBERP (value))
5057 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5058 it->voffset = - (XFLOATINT (value)
5059 * (normal_char_height (face->font, -1)));
5061 #endif /* HAVE_WINDOW_SYSTEM */
5064 return 0;
5067 /* Don't handle the other kinds of display specifications
5068 inside a string that we got from a `display' property. */
5069 if (it && it->string_from_display_prop_p)
5070 return 0;
5072 /* Characters having this form of property are not displayed, so
5073 we have to find the end of the property. */
5074 if (it)
5076 start_pos = *position;
5077 *position = display_prop_end (it, object, start_pos);
5078 /* If the display property comes from an overlay, don't consider
5079 any potential stop_charpos values before the end of that
5080 overlay. Since display_prop_end will happily find another
5081 'display' property coming from some other overlay or text
5082 property on buffer positions before this overlay's end, we
5083 need to ignore them, or else we risk displaying this
5084 overlay's display string/image twice. */
5085 if (!NILP (overlay))
5087 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5089 /* Some borderline-sane Lisp might call us with the current
5090 buffer narrowed so that overlay-end is outside the
5091 POINT_MIN..POINT_MAX region, which will then cause
5092 various assertion violations and crashes down the road,
5093 starting with pop_it when it will attempt to use POSITION
5094 set below. Prevent that. */
5095 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5097 if (ovendpos > CHARPOS (*position))
5098 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5101 value = Qnil;
5103 /* Stop the scan at that end position--we assume that all
5104 text properties change there. */
5105 if (it)
5106 it->stop_charpos = position->charpos;
5108 /* Handle `(left-fringe BITMAP [FACE])'
5109 and `(right-fringe BITMAP [FACE])'. */
5110 if (CONSP (spec)
5111 && (EQ (XCAR (spec), Qleft_fringe)
5112 || EQ (XCAR (spec), Qright_fringe))
5113 && CONSP (XCDR (spec)))
5115 if (it)
5117 if (!FRAME_WINDOW_P (it->f))
5118 /* If we return here, POSITION has been advanced
5119 across the text with this property. */
5121 /* Synchronize the bidi iterator with POSITION. This is
5122 needed because we are not going to push the iterator
5123 on behalf of this display property, so there will be
5124 no pop_it call to do this synchronization for us. */
5125 if (it->bidi_p)
5127 it->position = *position;
5128 iterate_out_of_display_property (it);
5129 *position = it->position;
5131 return 1;
5134 else if (!frame_window_p)
5135 return 1;
5137 #ifdef HAVE_WINDOW_SYSTEM
5138 value = XCAR (XCDR (spec));
5139 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5140 if (! fringe_bitmap)
5141 /* If we return here, POSITION has been advanced
5142 across the text with this property. */
5144 if (it && it->bidi_p)
5146 it->position = *position;
5147 iterate_out_of_display_property (it);
5148 *position = it->position;
5150 return 1;
5153 if (it)
5155 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5157 if (CONSP (XCDR (XCDR (spec))))
5159 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5160 int face_id2 = lookup_derived_face (it->f, face_name,
5161 FRINGE_FACE_ID, false);
5162 if (face_id2 >= 0)
5163 face_id = face_id2;
5166 /* Save current settings of IT so that we can restore them
5167 when we are finished with the glyph property value. */
5168 push_it (it, position);
5170 it->area = TEXT_AREA;
5171 it->what = IT_IMAGE;
5172 it->image_id = -1; /* no image */
5173 it->position = start_pos;
5174 it->object = NILP (object) ? it->w->contents : object;
5175 it->method = GET_FROM_IMAGE;
5176 it->from_overlay = Qnil;
5177 it->face_id = face_id;
5178 it->from_disp_prop_p = true;
5180 /* Say that we haven't consumed the characters with
5181 `display' property yet. The call to pop_it in
5182 set_iterator_to_next will clean this up. */
5183 *position = start_pos;
5185 if (EQ (XCAR (spec), Qleft_fringe))
5187 it->left_user_fringe_bitmap = fringe_bitmap;
5188 it->left_user_fringe_face_id = face_id;
5190 else
5192 it->right_user_fringe_bitmap = fringe_bitmap;
5193 it->right_user_fringe_face_id = face_id;
5196 #endif /* HAVE_WINDOW_SYSTEM */
5197 return 1;
5200 /* Prepare to handle `((margin left-margin) ...)',
5201 `((margin right-margin) ...)' and `((margin nil) ...)'
5202 prefixes for display specifications. */
5203 location = Qunbound;
5204 if (CONSP (spec) && CONSP (XCAR (spec)))
5206 Lisp_Object tem;
5208 value = XCDR (spec);
5209 if (CONSP (value))
5210 value = XCAR (value);
5212 tem = XCAR (spec);
5213 if (EQ (XCAR (tem), Qmargin)
5214 && (tem = XCDR (tem),
5215 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5216 (NILP (tem)
5217 || EQ (tem, Qleft_margin)
5218 || EQ (tem, Qright_margin))))
5219 location = tem;
5222 if (EQ (location, Qunbound))
5224 location = Qnil;
5225 value = spec;
5228 /* After this point, VALUE is the property after any
5229 margin prefix has been stripped. It must be a string,
5230 an image specification, or `(space ...)'.
5232 LOCATION specifies where to display: `left-margin',
5233 `right-margin' or nil. */
5235 bool valid_p = (STRINGP (value)
5236 #ifdef HAVE_WINDOW_SYSTEM
5237 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5238 && valid_image_p (value))
5239 #endif /* not HAVE_WINDOW_SYSTEM */
5240 || (CONSP (value) && EQ (XCAR (value), Qspace))
5241 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5242 && valid_xwidget_spec_p (value)));
5244 if (valid_p && display_replaced == 0)
5246 int retval = 1;
5248 if (!it)
5250 /* Callers need to know whether the display spec is any kind
5251 of `(space ...)' spec that is about to affect text-area
5252 display. */
5253 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5254 retval = 2;
5255 return retval;
5258 /* Save current settings of IT so that we can restore them
5259 when we are finished with the glyph property value. */
5260 push_it (it, position);
5261 it->from_overlay = overlay;
5262 it->from_disp_prop_p = true;
5264 if (NILP (location))
5265 it->area = TEXT_AREA;
5266 else if (EQ (location, Qleft_margin))
5267 it->area = LEFT_MARGIN_AREA;
5268 else
5269 it->area = RIGHT_MARGIN_AREA;
5271 if (STRINGP (value))
5273 it->string = value;
5274 it->multibyte_p = STRING_MULTIBYTE (it->string);
5275 it->current.overlay_string_index = -1;
5276 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5277 it->end_charpos = it->string_nchars = SCHARS (it->string);
5278 it->method = GET_FROM_STRING;
5279 it->stop_charpos = 0;
5280 it->prev_stop = 0;
5281 it->base_level_stop = 0;
5282 it->string_from_display_prop_p = true;
5283 it->cmp_it.id = -1;
5284 /* Say that we haven't consumed the characters with
5285 `display' property yet. The call to pop_it in
5286 set_iterator_to_next will clean this up. */
5287 if (BUFFERP (object))
5288 *position = start_pos;
5290 /* Force paragraph direction to be that of the parent
5291 object. If the parent object's paragraph direction is
5292 not yet determined, default to L2R. */
5293 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5294 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5295 else
5296 it->paragraph_embedding = L2R;
5298 /* Set up the bidi iterator for this display string. */
5299 if (it->bidi_p)
5301 it->bidi_it.string.lstring = it->string;
5302 it->bidi_it.string.s = NULL;
5303 it->bidi_it.string.schars = it->end_charpos;
5304 it->bidi_it.string.bufpos = bufpos;
5305 it->bidi_it.string.from_disp_str = true;
5306 it->bidi_it.string.unibyte = !it->multibyte_p;
5307 it->bidi_it.w = it->w;
5308 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5311 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5313 it->method = GET_FROM_STRETCH;
5314 it->object = value;
5315 *position = it->position = start_pos;
5316 retval = 1 + (it->area == TEXT_AREA);
5318 else if (valid_xwidget_spec_p (value))
5320 it->what = IT_XWIDGET;
5321 it->method = GET_FROM_XWIDGET;
5322 it->position = start_pos;
5323 it->object = NILP (object) ? it->w->contents : object;
5324 *position = start_pos;
5325 it->xwidget = lookup_xwidget (value);
5327 #ifdef HAVE_WINDOW_SYSTEM
5328 else
5330 it->what = IT_IMAGE;
5331 it->image_id = lookup_image (it->f, value);
5332 it->position = start_pos;
5333 it->object = NILP (object) ? it->w->contents : object;
5334 it->method = GET_FROM_IMAGE;
5336 /* Say that we haven't consumed the characters with
5337 `display' property yet. The call to pop_it in
5338 set_iterator_to_next will clean this up. */
5339 *position = start_pos;
5341 #endif /* HAVE_WINDOW_SYSTEM */
5343 return retval;
5346 /* Invalid property or property not supported. Restore
5347 POSITION to what it was before. */
5348 *position = start_pos;
5349 return 0;
5352 /* Check if PROP is a display property value whose text should be
5353 treated as intangible. OVERLAY is the overlay from which PROP
5354 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5355 specify the buffer position covered by PROP. */
5357 bool
5358 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5359 ptrdiff_t charpos, ptrdiff_t bytepos)
5361 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5362 struct text_pos position;
5364 SET_TEXT_POS (position, charpos, bytepos);
5365 return (handle_display_spec (NULL, prop, Qnil, overlay,
5366 &position, charpos, frame_window_p)
5367 != 0);
5371 /* Return true if PROP is a display sub-property value containing STRING.
5373 Implementation note: this and the following function are really
5374 special cases of handle_display_spec and
5375 handle_single_display_spec, and should ideally use the same code.
5376 Until they do, these two pairs must be consistent and must be
5377 modified in sync. */
5379 static bool
5380 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5382 if (EQ (string, prop))
5383 return true;
5385 /* Skip over `when FORM'. */
5386 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5388 prop = XCDR (prop);
5389 if (!CONSP (prop))
5390 return false;
5391 /* Actually, the condition following `when' should be eval'ed,
5392 like handle_single_display_spec does, and we should return
5393 false if it evaluates to nil. However, this function is
5394 called only when the buffer was already displayed and some
5395 glyph in the glyph matrix was found to come from a display
5396 string. Therefore, the condition was already evaluated, and
5397 the result was non-nil, otherwise the display string wouldn't
5398 have been displayed and we would have never been called for
5399 this property. Thus, we can skip the evaluation and assume
5400 its result is non-nil. */
5401 prop = XCDR (prop);
5404 if (CONSP (prop))
5405 /* Skip over `margin LOCATION'. */
5406 if (EQ (XCAR (prop), Qmargin))
5408 prop = XCDR (prop);
5409 if (!CONSP (prop))
5410 return false;
5412 prop = XCDR (prop);
5413 if (!CONSP (prop))
5414 return false;
5417 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5421 /* Return true if STRING appears in the `display' property PROP. */
5423 static bool
5424 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5426 if (CONSP (prop)
5427 && !EQ (XCAR (prop), Qwhen)
5428 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5430 /* A list of sub-properties. */
5431 while (CONSP (prop))
5433 if (single_display_spec_string_p (XCAR (prop), string))
5434 return true;
5435 prop = XCDR (prop);
5438 else if (VECTORP (prop))
5440 /* A vector of sub-properties. */
5441 ptrdiff_t i;
5442 for (i = 0; i < ASIZE (prop); ++i)
5443 if (single_display_spec_string_p (AREF (prop, i), string))
5444 return true;
5446 else
5447 return single_display_spec_string_p (prop, string);
5449 return false;
5452 /* Look for STRING in overlays and text properties in the current
5453 buffer, between character positions FROM and TO (excluding TO).
5454 BACK_P means look back (in this case, TO is supposed to be
5455 less than FROM).
5456 Value is the first character position where STRING was found, or
5457 zero if it wasn't found before hitting TO.
5459 This function may only use code that doesn't eval because it is
5460 called asynchronously from note_mouse_highlight. */
5462 static ptrdiff_t
5463 string_buffer_position_lim (Lisp_Object string,
5464 ptrdiff_t from, ptrdiff_t to, bool back_p)
5466 Lisp_Object limit, prop, pos;
5467 bool found = false;
5469 pos = make_number (max (from, BEGV));
5471 if (!back_p) /* looking forward */
5473 limit = make_number (min (to, ZV));
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 = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5481 limit);
5484 else /* looking back */
5486 limit = make_number (max (to, BEGV));
5487 while (!found && !EQ (pos, limit))
5489 prop = Fget_char_property (pos, Qdisplay, Qnil);
5490 if (!NILP (prop) && display_prop_string_p (prop, string))
5491 found = true;
5492 else
5493 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5494 limit);
5498 return found ? XINT (pos) : 0;
5501 /* Determine which buffer position in current buffer STRING comes from.
5502 AROUND_CHARPOS is an approximate position where it could come from.
5503 Value is the buffer position or 0 if it couldn't be determined.
5505 This function is necessary because we don't record buffer positions
5506 in glyphs generated from strings (to keep struct glyph small).
5507 This function may only use code that doesn't eval because it is
5508 called asynchronously from note_mouse_highlight. */
5510 static ptrdiff_t
5511 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5513 const int MAX_DISTANCE = 1000;
5514 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5515 around_charpos + MAX_DISTANCE,
5516 false);
5518 if (!found)
5519 found = string_buffer_position_lim (string, around_charpos,
5520 around_charpos - MAX_DISTANCE, true);
5521 return found;
5526 /***********************************************************************
5527 `composition' property
5528 ***********************************************************************/
5530 /* Set up iterator IT from `composition' property at its current
5531 position. Called from handle_stop. */
5533 static enum prop_handled
5534 handle_composition_prop (struct it *it)
5536 Lisp_Object prop, string;
5537 ptrdiff_t pos, pos_byte, start, end;
5539 if (STRINGP (it->string))
5541 unsigned char *s;
5543 pos = IT_STRING_CHARPOS (*it);
5544 pos_byte = IT_STRING_BYTEPOS (*it);
5545 string = it->string;
5546 s = SDATA (string) + pos_byte;
5547 it->c = STRING_CHAR (s);
5549 else
5551 pos = IT_CHARPOS (*it);
5552 pos_byte = IT_BYTEPOS (*it);
5553 string = Qnil;
5554 it->c = FETCH_CHAR (pos_byte);
5557 /* If there's a valid composition and point is not inside of the
5558 composition (in the case that the composition is from the current
5559 buffer), draw a glyph composed from the composition components. */
5560 if (find_composition (pos, -1, &start, &end, &prop, string)
5561 && composition_valid_p (start, end, prop)
5562 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5564 if (start < pos)
5565 /* As we can't handle this situation (perhaps font-lock added
5566 a new composition), we just return here hoping that next
5567 redisplay will detect this composition much earlier. */
5568 return HANDLED_NORMALLY;
5569 if (start != pos)
5571 if (STRINGP (it->string))
5572 pos_byte = string_char_to_byte (it->string, start);
5573 else
5574 pos_byte = CHAR_TO_BYTE (start);
5576 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5577 prop, string);
5579 if (it->cmp_it.id >= 0)
5581 it->cmp_it.ch = -1;
5582 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5583 it->cmp_it.nglyphs = -1;
5587 return HANDLED_NORMALLY;
5592 /***********************************************************************
5593 Overlay strings
5594 ***********************************************************************/
5596 /* The following structure is used to record overlay strings for
5597 later sorting in load_overlay_strings. */
5599 struct overlay_entry
5601 Lisp_Object overlay;
5602 Lisp_Object string;
5603 EMACS_INT priority;
5604 bool after_string_p;
5608 /* Set up iterator IT from overlay strings at its current position.
5609 Called from handle_stop. */
5611 static enum prop_handled
5612 handle_overlay_change (struct it *it)
5614 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5615 return HANDLED_RECOMPUTE_PROPS;
5616 else
5617 return HANDLED_NORMALLY;
5621 /* Set up the next overlay string for delivery by IT, if there is an
5622 overlay string to deliver. Called by set_iterator_to_next when the
5623 end of the current overlay string is reached. If there are more
5624 overlay strings to display, IT->string and
5625 IT->current.overlay_string_index are set appropriately here.
5626 Otherwise IT->string is set to nil. */
5628 static void
5629 next_overlay_string (struct it *it)
5631 ++it->current.overlay_string_index;
5632 if (it->current.overlay_string_index == it->n_overlay_strings)
5634 /* No more overlay strings. Restore IT's settings to what
5635 they were before overlay strings were processed, and
5636 continue to deliver from current_buffer. */
5638 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5639 pop_it (it);
5640 eassert (it->sp > 0
5641 || (NILP (it->string)
5642 && it->method == GET_FROM_BUFFER
5643 && it->stop_charpos >= BEGV
5644 && it->stop_charpos <= it->end_charpos));
5645 it->current.overlay_string_index = -1;
5646 it->n_overlay_strings = 0;
5647 /* If there's an empty display string on the stack, pop the
5648 stack, to resync the bidi iterator with IT's position. Such
5649 empty strings are pushed onto the stack in
5650 get_overlay_strings_1. */
5651 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5652 pop_it (it);
5654 /* Since we've exhausted overlay strings at this buffer
5655 position, set the flag to ignore overlays until we move to
5656 another position. (The flag will be reset in
5657 next_element_from_buffer.) But don't do that if the overlay
5658 strings were loaded at position other than the current one,
5659 which could happen if we called pop_it above, or if the
5660 overlay strings were loaded by handle_invisible_prop at the
5661 beginning of invisible text. */
5662 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5663 it->ignore_overlay_strings_at_pos_p = true;
5665 /* If we're at the end of the buffer, record that we have
5666 processed the overlay strings there already, so that
5667 next_element_from_buffer doesn't try it again. */
5668 if (NILP (it->string)
5669 && IT_CHARPOS (*it) >= it->end_charpos
5670 && it->overlay_strings_charpos >= it->end_charpos)
5671 it->overlay_strings_at_end_processed_p = true;
5672 /* Note: we reset overlay_strings_charpos only here, to make
5673 sure the just-processed overlays were indeed at EOB.
5674 Otherwise, overlays on text with invisible text property,
5675 which are processed with IT's position past the invisible
5676 text, might fool us into thinking the overlays at EOB were
5677 already processed (linum-mode can cause this, for
5678 example). */
5679 it->overlay_strings_charpos = -1;
5681 else
5683 /* There are more overlay strings to process. If
5684 IT->current.overlay_string_index has advanced to a position
5685 where we must load IT->overlay_strings with more strings, do
5686 it. We must load at the IT->overlay_strings_charpos where
5687 IT->n_overlay_strings was originally computed; when invisible
5688 text is present, this might not be IT_CHARPOS (Bug#7016). */
5689 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5691 if (it->current.overlay_string_index && i == 0)
5692 load_overlay_strings (it, it->overlay_strings_charpos);
5694 /* Initialize IT to deliver display elements from the overlay
5695 string. */
5696 it->string = it->overlay_strings[i];
5697 it->multibyte_p = STRING_MULTIBYTE (it->string);
5698 SET_TEXT_POS (it->current.string_pos, 0, 0);
5699 it->method = GET_FROM_STRING;
5700 it->stop_charpos = 0;
5701 it->end_charpos = SCHARS (it->string);
5702 if (it->cmp_it.stop_pos >= 0)
5703 it->cmp_it.stop_pos = 0;
5704 it->prev_stop = 0;
5705 it->base_level_stop = 0;
5707 /* Set up the bidi iterator for this overlay string. */
5708 if (it->bidi_p)
5710 it->bidi_it.string.lstring = it->string;
5711 it->bidi_it.string.s = NULL;
5712 it->bidi_it.string.schars = SCHARS (it->string);
5713 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5714 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5715 it->bidi_it.string.unibyte = !it->multibyte_p;
5716 it->bidi_it.w = it->w;
5717 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5721 CHECK_IT (it);
5725 /* Compare two overlay_entry structures E1 and E2. Used as a
5726 comparison function for qsort in load_overlay_strings. Overlay
5727 strings for the same position are sorted so that
5729 1. All after-strings come in front of before-strings, except
5730 when they come from the same overlay.
5732 2. Within after-strings, strings are sorted so that overlay strings
5733 from overlays with higher priorities come first.
5735 2. Within before-strings, strings are sorted so that overlay
5736 strings from overlays with higher priorities come last.
5738 Value is analogous to strcmp. */
5741 static int
5742 compare_overlay_entries (const void *e1, const void *e2)
5744 struct overlay_entry const *entry1 = e1;
5745 struct overlay_entry const *entry2 = e2;
5746 int result;
5748 if (entry1->after_string_p != entry2->after_string_p)
5750 /* Let after-strings appear in front of before-strings if
5751 they come from different overlays. */
5752 if (EQ (entry1->overlay, entry2->overlay))
5753 result = entry1->after_string_p ? 1 : -1;
5754 else
5755 result = entry1->after_string_p ? -1 : 1;
5757 else if (entry1->priority != entry2->priority)
5759 if (entry1->after_string_p)
5760 /* After-strings sorted in order of decreasing priority. */
5761 result = entry2->priority < entry1->priority ? -1 : 1;
5762 else
5763 /* Before-strings sorted in order of increasing priority. */
5764 result = entry1->priority < entry2->priority ? -1 : 1;
5766 else
5767 result = 0;
5769 return result;
5773 /* Load the vector IT->overlay_strings with overlay strings from IT's
5774 current buffer position, or from CHARPOS if that is > 0. Set
5775 IT->n_overlays to the total number of overlay strings found.
5777 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5778 a time. On entry into load_overlay_strings,
5779 IT->current.overlay_string_index gives the number of overlay
5780 strings that have already been loaded by previous calls to this
5781 function.
5783 IT->add_overlay_start contains an additional overlay start
5784 position to consider for taking overlay strings from, if non-zero.
5785 This position comes into play when the overlay has an `invisible'
5786 property, and both before and after-strings. When we've skipped to
5787 the end of the overlay, because of its `invisible' property, we
5788 nevertheless want its before-string to appear.
5789 IT->add_overlay_start will contain the overlay start position
5790 in this case.
5792 Overlay strings are sorted so that after-string strings come in
5793 front of before-string strings. Within before and after-strings,
5794 strings are sorted by overlay priority. See also function
5795 compare_overlay_entries. */
5797 static void
5798 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5800 Lisp_Object overlay, window, str, invisible;
5801 struct Lisp_Overlay *ov;
5802 ptrdiff_t start, end;
5803 ptrdiff_t n = 0, i, j;
5804 int invis;
5805 struct overlay_entry entriesbuf[20];
5806 ptrdiff_t size = ARRAYELTS (entriesbuf);
5807 struct overlay_entry *entries = entriesbuf;
5808 USE_SAFE_ALLOCA;
5810 if (charpos <= 0)
5811 charpos = IT_CHARPOS (*it);
5813 /* Append the overlay string STRING of overlay OVERLAY to vector
5814 `entries' which has size `size' and currently contains `n'
5815 elements. AFTER_P means STRING is an after-string of
5816 OVERLAY. */
5817 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5818 do \
5820 Lisp_Object priority; \
5822 if (n == size) \
5824 struct overlay_entry *old = entries; \
5825 SAFE_NALLOCA (entries, 2, size); \
5826 memcpy (entries, old, size * sizeof *entries); \
5827 size *= 2; \
5830 entries[n].string = (STRING); \
5831 entries[n].overlay = (OVERLAY); \
5832 priority = Foverlay_get ((OVERLAY), Qpriority); \
5833 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5834 entries[n].after_string_p = (AFTER_P); \
5835 ++n; \
5837 while (false)
5839 /* Process overlay before the overlay center. */
5840 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5842 XSETMISC (overlay, ov);
5843 eassert (OVERLAYP (overlay));
5844 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5845 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5847 if (end < charpos)
5848 break;
5850 /* Skip this overlay if it doesn't start or end at IT's current
5851 position. */
5852 if (end != charpos && start != charpos)
5853 continue;
5855 /* Skip this overlay if it doesn't apply to IT->w. */
5856 window = Foverlay_get (overlay, Qwindow);
5857 if (WINDOWP (window) && XWINDOW (window) != it->w)
5858 continue;
5860 /* If the text ``under'' the overlay is invisible, both before-
5861 and after-strings from this overlay are visible; start and
5862 end position are indistinguishable. */
5863 invisible = Foverlay_get (overlay, Qinvisible);
5864 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5866 /* If overlay has a non-empty before-string, record it. */
5867 if ((start == charpos || (end == charpos && invis != 0))
5868 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5869 && SCHARS (str))
5870 RECORD_OVERLAY_STRING (overlay, str, false);
5872 /* If overlay has a non-empty after-string, record it. */
5873 if ((end == charpos || (start == charpos && invis != 0))
5874 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5875 && SCHARS (str))
5876 RECORD_OVERLAY_STRING (overlay, str, true);
5879 /* Process overlays after the overlay center. */
5880 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5882 XSETMISC (overlay, ov);
5883 eassert (OVERLAYP (overlay));
5884 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5885 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5887 if (start > charpos)
5888 break;
5890 /* Skip this overlay if it doesn't start or end at IT's current
5891 position. */
5892 if (end != charpos && start != charpos)
5893 continue;
5895 /* Skip this overlay if it doesn't apply to IT->w. */
5896 window = Foverlay_get (overlay, Qwindow);
5897 if (WINDOWP (window) && XWINDOW (window) != it->w)
5898 continue;
5900 /* If the text ``under'' the overlay is invisible, it has a zero
5901 dimension, and both before- and after-strings apply. */
5902 invisible = Foverlay_get (overlay, Qinvisible);
5903 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5905 /* If overlay has a non-empty before-string, record it. */
5906 if ((start == charpos || (end == charpos && invis != 0))
5907 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5908 && SCHARS (str))
5909 RECORD_OVERLAY_STRING (overlay, str, false);
5911 /* If overlay has a non-empty after-string, record it. */
5912 if ((end == charpos || (start == charpos && invis != 0))
5913 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5914 && SCHARS (str))
5915 RECORD_OVERLAY_STRING (overlay, str, true);
5918 #undef RECORD_OVERLAY_STRING
5920 /* Sort entries. */
5921 if (n > 1)
5922 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5924 /* Record number of overlay strings, and where we computed it. */
5925 it->n_overlay_strings = n;
5926 it->overlay_strings_charpos = charpos;
5928 /* IT->current.overlay_string_index is the number of overlay strings
5929 that have already been consumed by IT. Copy some of the
5930 remaining overlay strings to IT->overlay_strings. */
5931 i = 0;
5932 j = it->current.overlay_string_index;
5933 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5935 it->overlay_strings[i] = entries[j].string;
5936 it->string_overlays[i++] = entries[j++].overlay;
5939 CHECK_IT (it);
5940 SAFE_FREE ();
5944 /* Get the first chunk of overlay strings at IT's current buffer
5945 position, or at CHARPOS if that is > 0. Value is true if at
5946 least one overlay string was found. */
5948 static bool
5949 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5951 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5952 process. This fills IT->overlay_strings with strings, and sets
5953 IT->n_overlay_strings to the total number of strings to process.
5954 IT->pos.overlay_string_index has to be set temporarily to zero
5955 because load_overlay_strings needs this; it must be set to -1
5956 when no overlay strings are found because a zero value would
5957 indicate a position in the first overlay string. */
5958 it->current.overlay_string_index = 0;
5959 load_overlay_strings (it, charpos);
5961 /* If we found overlay strings, set up IT to deliver display
5962 elements from the first one. Otherwise set up IT to deliver
5963 from current_buffer. */
5964 if (it->n_overlay_strings)
5966 /* Make sure we know settings in current_buffer, so that we can
5967 restore meaningful values when we're done with the overlay
5968 strings. */
5969 if (compute_stop_p)
5970 compute_stop_pos (it);
5971 eassert (it->face_id >= 0);
5973 /* Save IT's settings. They are restored after all overlay
5974 strings have been processed. */
5975 eassert (!compute_stop_p || it->sp == 0);
5977 /* When called from handle_stop, there might be an empty display
5978 string loaded. In that case, don't bother saving it. But
5979 don't use this optimization with the bidi iterator, since we
5980 need the corresponding pop_it call to resync the bidi
5981 iterator's position with IT's position, after we are done
5982 with the overlay strings. (The corresponding call to pop_it
5983 in case of an empty display string is in
5984 next_overlay_string.) */
5985 if (!(!it->bidi_p
5986 && STRINGP (it->string) && !SCHARS (it->string)))
5987 push_it (it, NULL);
5989 /* Set up IT to deliver display elements from the first overlay
5990 string. */
5991 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5992 it->string = it->overlay_strings[0];
5993 it->from_overlay = Qnil;
5994 it->stop_charpos = 0;
5995 eassert (STRINGP (it->string));
5996 it->end_charpos = SCHARS (it->string);
5997 it->prev_stop = 0;
5998 it->base_level_stop = 0;
5999 it->multibyte_p = STRING_MULTIBYTE (it->string);
6000 it->method = GET_FROM_STRING;
6001 it->from_disp_prop_p = 0;
6002 it->cmp_it.id = -1;
6004 /* Force paragraph direction to be that of the parent
6005 buffer. */
6006 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
6007 it->paragraph_embedding = it->bidi_it.paragraph_dir;
6008 else
6009 it->paragraph_embedding = L2R;
6011 /* Set up the bidi iterator for this overlay string. */
6012 if (it->bidi_p)
6014 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
6016 it->bidi_it.string.lstring = it->string;
6017 it->bidi_it.string.s = NULL;
6018 it->bidi_it.string.schars = SCHARS (it->string);
6019 it->bidi_it.string.bufpos = pos;
6020 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6021 it->bidi_it.string.unibyte = !it->multibyte_p;
6022 it->bidi_it.w = it->w;
6023 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6025 return true;
6028 it->current.overlay_string_index = -1;
6029 return false;
6032 static bool
6033 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6035 it->string = Qnil;
6036 it->method = GET_FROM_BUFFER;
6038 get_overlay_strings_1 (it, charpos, true);
6040 CHECK_IT (it);
6042 /* Value is true if we found at least one overlay string. */
6043 return STRINGP (it->string);
6048 /***********************************************************************
6049 Saving and restoring state
6050 ***********************************************************************/
6052 /* Save current settings of IT on IT->stack. Called, for example,
6053 before setting up IT for an overlay string, to be able to restore
6054 IT's settings to what they were after the overlay string has been
6055 processed. If POSITION is non-NULL, it is the position to save on
6056 the stack instead of IT->position. */
6058 static void
6059 push_it (struct it *it, struct text_pos *position)
6061 struct iterator_stack_entry *p;
6063 eassert (it->sp < IT_STACK_SIZE);
6064 p = it->stack + it->sp;
6066 p->stop_charpos = it->stop_charpos;
6067 p->prev_stop = it->prev_stop;
6068 p->base_level_stop = it->base_level_stop;
6069 p->cmp_it = it->cmp_it;
6070 eassert (it->face_id >= 0);
6071 p->face_id = it->face_id;
6072 p->string = it->string;
6073 p->method = it->method;
6074 p->from_overlay = it->from_overlay;
6075 switch (p->method)
6077 case GET_FROM_IMAGE:
6078 p->u.image.object = it->object;
6079 p->u.image.image_id = it->image_id;
6080 p->u.image.slice = it->slice;
6081 break;
6082 case GET_FROM_STRETCH:
6083 p->u.stretch.object = it->object;
6084 break;
6085 case GET_FROM_XWIDGET:
6086 p->u.xwidget.object = it->object;
6087 break;
6088 case GET_FROM_BUFFER:
6089 case GET_FROM_DISPLAY_VECTOR:
6090 case GET_FROM_STRING:
6091 case GET_FROM_C_STRING:
6092 break;
6093 default:
6094 emacs_abort ();
6096 p->position = position ? *position : it->position;
6097 p->current = it->current;
6098 p->end_charpos = it->end_charpos;
6099 p->string_nchars = it->string_nchars;
6100 p->area = it->area;
6101 p->multibyte_p = it->multibyte_p;
6102 p->avoid_cursor_p = it->avoid_cursor_p;
6103 p->space_width = it->space_width;
6104 p->font_height = it->font_height;
6105 p->voffset = it->voffset;
6106 p->string_from_display_prop_p = it->string_from_display_prop_p;
6107 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6108 p->display_ellipsis_p = false;
6109 p->line_wrap = it->line_wrap;
6110 p->bidi_p = it->bidi_p;
6111 p->paragraph_embedding = it->paragraph_embedding;
6112 p->from_disp_prop_p = it->from_disp_prop_p;
6113 ++it->sp;
6115 /* Save the state of the bidi iterator as well. */
6116 if (it->bidi_p)
6117 bidi_push_it (&it->bidi_it);
6120 static void
6121 iterate_out_of_display_property (struct it *it)
6123 bool buffer_p = !STRINGP (it->string);
6124 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6125 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6127 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6129 /* Maybe initialize paragraph direction. If we are at the beginning
6130 of a new paragraph, next_element_from_buffer may not have a
6131 chance to do that. */
6132 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6133 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6134 /* prev_stop can be zero, so check against BEGV as well. */
6135 while (it->bidi_it.charpos >= bob
6136 && it->prev_stop <= it->bidi_it.charpos
6137 && it->bidi_it.charpos < CHARPOS (it->position)
6138 && it->bidi_it.charpos < eob)
6139 bidi_move_to_visually_next (&it->bidi_it);
6140 /* Record the stop_pos we just crossed, for when we cross it
6141 back, maybe. */
6142 if (it->bidi_it.charpos > CHARPOS (it->position))
6143 it->prev_stop = CHARPOS (it->position);
6144 /* If we ended up not where pop_it put us, resync IT's
6145 positional members with the bidi iterator. */
6146 if (it->bidi_it.charpos != CHARPOS (it->position))
6147 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6148 if (buffer_p)
6149 it->current.pos = it->position;
6150 else
6151 it->current.string_pos = it->position;
6154 /* Restore IT's settings from IT->stack. Called, for example, when no
6155 more overlay strings must be processed, and we return to delivering
6156 display elements from a buffer, or when the end of a string from a
6157 `display' property is reached and we return to delivering display
6158 elements from an overlay string, or from a buffer. */
6160 static void
6161 pop_it (struct it *it)
6163 struct iterator_stack_entry *p;
6164 bool from_display_prop = it->from_disp_prop_p;
6165 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6167 eassert (it->sp > 0);
6168 --it->sp;
6169 p = it->stack + it->sp;
6170 it->stop_charpos = p->stop_charpos;
6171 it->prev_stop = p->prev_stop;
6172 it->base_level_stop = p->base_level_stop;
6173 it->cmp_it = p->cmp_it;
6174 it->face_id = p->face_id;
6175 it->current = p->current;
6176 it->position = p->position;
6177 it->string = p->string;
6178 it->from_overlay = p->from_overlay;
6179 if (NILP (it->string))
6180 SET_TEXT_POS (it->current.string_pos, -1, -1);
6181 it->method = p->method;
6182 switch (it->method)
6184 case GET_FROM_IMAGE:
6185 it->image_id = p->u.image.image_id;
6186 it->object = p->u.image.object;
6187 it->slice = p->u.image.slice;
6188 break;
6189 case GET_FROM_XWIDGET:
6190 it->object = p->u.xwidget.object;
6191 break;
6192 case GET_FROM_STRETCH:
6193 it->object = p->u.stretch.object;
6194 break;
6195 case GET_FROM_BUFFER:
6196 it->object = it->w->contents;
6197 break;
6198 case GET_FROM_STRING:
6200 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6202 /* Restore the face_box_p flag, since it could have been
6203 overwritten by the face of the object that we just finished
6204 displaying. */
6205 if (face)
6206 it->face_box_p = face->box != FACE_NO_BOX;
6207 it->object = it->string;
6209 break;
6210 case GET_FROM_DISPLAY_VECTOR:
6211 if (it->s)
6212 it->method = GET_FROM_C_STRING;
6213 else if (STRINGP (it->string))
6214 it->method = GET_FROM_STRING;
6215 else
6217 it->method = GET_FROM_BUFFER;
6218 it->object = it->w->contents;
6220 break;
6221 case GET_FROM_C_STRING:
6222 break;
6223 default:
6224 emacs_abort ();
6226 it->end_charpos = p->end_charpos;
6227 it->string_nchars = p->string_nchars;
6228 it->area = p->area;
6229 it->multibyte_p = p->multibyte_p;
6230 it->avoid_cursor_p = p->avoid_cursor_p;
6231 it->space_width = p->space_width;
6232 it->font_height = p->font_height;
6233 it->voffset = p->voffset;
6234 it->string_from_display_prop_p = p->string_from_display_prop_p;
6235 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6236 it->line_wrap = p->line_wrap;
6237 it->bidi_p = p->bidi_p;
6238 it->paragraph_embedding = p->paragraph_embedding;
6239 it->from_disp_prop_p = p->from_disp_prop_p;
6240 if (it->bidi_p)
6242 bidi_pop_it (&it->bidi_it);
6243 /* Bidi-iterate until we get out of the portion of text, if any,
6244 covered by a `display' text property or by an overlay with
6245 `display' property. (We cannot just jump there, because the
6246 internal coherency of the bidi iterator state can not be
6247 preserved across such jumps.) We also must determine the
6248 paragraph base direction if the overlay we just processed is
6249 at the beginning of a new paragraph. */
6250 if (from_display_prop
6251 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6252 iterate_out_of_display_property (it);
6254 eassert ((BUFFERP (it->object)
6255 && IT_CHARPOS (*it) == it->bidi_it.charpos
6256 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6257 || (STRINGP (it->object)
6258 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6259 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6260 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6262 /* If we move the iterator over text covered by a display property
6263 to a new buffer position, any info about previously seen overlays
6264 is no longer valid. */
6265 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6266 it->ignore_overlay_strings_at_pos_p = false;
6271 /***********************************************************************
6272 Moving over lines
6273 ***********************************************************************/
6275 /* Set IT's current position to the previous line start. */
6277 static void
6278 back_to_previous_line_start (struct it *it)
6280 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6282 DEC_BOTH (cp, bp);
6283 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6287 /* Move IT to the next line start.
6289 Value is true if a newline was found. Set *SKIPPED_P to true if
6290 we skipped over part of the text (as opposed to moving the iterator
6291 continuously over the text). Otherwise, don't change the value
6292 of *SKIPPED_P.
6294 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6295 iterator on the newline, if it was found.
6297 Newlines may come from buffer text, overlay strings, or strings
6298 displayed via the `display' property. That's the reason we can't
6299 simply use find_newline_no_quit.
6301 Note that this function may not skip over invisible text that is so
6302 because of text properties and immediately follows a newline. If
6303 it would, function reseat_at_next_visible_line_start, when called
6304 from set_iterator_to_next, would effectively make invisible
6305 characters following a newline part of the wrong glyph row, which
6306 leads to wrong cursor motion. */
6308 static bool
6309 forward_to_next_line_start (struct it *it, bool *skipped_p,
6310 struct bidi_it *bidi_it_prev)
6312 ptrdiff_t old_selective;
6313 bool newline_found_p = false;
6314 int n;
6315 const int MAX_NEWLINE_DISTANCE = 500;
6317 /* If already on a newline, just consume it to avoid unintended
6318 skipping over invisible text below. */
6319 if (it->what == IT_CHARACTER
6320 && it->c == '\n'
6321 && CHARPOS (it->position) == IT_CHARPOS (*it))
6323 if (it->bidi_p && bidi_it_prev)
6324 *bidi_it_prev = it->bidi_it;
6325 set_iterator_to_next (it, false);
6326 it->c = 0;
6327 return true;
6330 /* Don't handle selective display in the following. It's (a)
6331 unnecessary because it's done by the caller, and (b) leads to an
6332 infinite recursion because next_element_from_ellipsis indirectly
6333 calls this function. */
6334 old_selective = it->selective;
6335 it->selective = 0;
6337 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6338 from buffer text. */
6339 for (n = 0;
6340 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6341 n += !STRINGP (it->string))
6343 if (!get_next_display_element (it))
6344 return false;
6345 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6346 if (newline_found_p && it->bidi_p && bidi_it_prev)
6347 *bidi_it_prev = it->bidi_it;
6348 set_iterator_to_next (it, false);
6351 /* If we didn't find a newline near enough, see if we can use a
6352 short-cut. */
6353 if (!newline_found_p)
6355 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6356 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6357 1, &bytepos);
6358 Lisp_Object pos;
6360 eassert (!STRINGP (it->string));
6362 /* If there isn't any `display' property in sight, and no
6363 overlays, we can just use the position of the newline in
6364 buffer text. */
6365 if (it->stop_charpos >= limit
6366 || ((pos = Fnext_single_property_change (make_number (start),
6367 Qdisplay, Qnil,
6368 make_number (limit)),
6369 NILP (pos))
6370 && next_overlay_change (start) == ZV))
6372 if (!it->bidi_p)
6374 IT_CHARPOS (*it) = limit;
6375 IT_BYTEPOS (*it) = bytepos;
6377 else
6379 struct bidi_it bprev;
6381 /* Help bidi.c avoid expensive searches for display
6382 properties and overlays, by telling it that there are
6383 none up to `limit'. */
6384 if (it->bidi_it.disp_pos < limit)
6386 it->bidi_it.disp_pos = limit;
6387 it->bidi_it.disp_prop = 0;
6389 do {
6390 bprev = it->bidi_it;
6391 bidi_move_to_visually_next (&it->bidi_it);
6392 } while (it->bidi_it.charpos != limit);
6393 IT_CHARPOS (*it) = limit;
6394 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6395 if (bidi_it_prev)
6396 *bidi_it_prev = bprev;
6398 *skipped_p = newline_found_p = true;
6400 else
6402 while (!newline_found_p)
6404 if (!get_next_display_element (it))
6405 break;
6406 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6407 if (newline_found_p && it->bidi_p && bidi_it_prev)
6408 *bidi_it_prev = it->bidi_it;
6409 set_iterator_to_next (it, false);
6414 it->selective = old_selective;
6415 return newline_found_p;
6419 /* Set IT's current position to the previous visible line start. Skip
6420 invisible text that is so either due to text properties or due to
6421 selective display. Caution: this does not change IT->current_x and
6422 IT->hpos. */
6424 static void
6425 back_to_previous_visible_line_start (struct it *it)
6427 while (IT_CHARPOS (*it) > BEGV)
6429 back_to_previous_line_start (it);
6431 if (IT_CHARPOS (*it) <= BEGV)
6432 break;
6434 /* If selective > 0, then lines indented more than its value are
6435 invisible. */
6436 if (it->selective > 0
6437 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6438 it->selective))
6439 continue;
6441 /* Check the newline before point for invisibility. */
6443 Lisp_Object prop;
6444 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6445 Qinvisible, it->window);
6446 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6447 continue;
6450 if (IT_CHARPOS (*it) <= BEGV)
6451 break;
6454 struct it it2;
6455 void *it2data = NULL;
6456 ptrdiff_t pos;
6457 ptrdiff_t beg, end;
6458 Lisp_Object val, overlay;
6460 SAVE_IT (it2, *it, it2data);
6462 /* If newline is part of a composition, continue from start of composition */
6463 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6464 && beg < IT_CHARPOS (*it))
6465 goto replaced;
6467 /* If newline is replaced by a display property, find start of overlay
6468 or interval and continue search from that point. */
6469 pos = --IT_CHARPOS (it2);
6470 --IT_BYTEPOS (it2);
6471 it2.sp = 0;
6472 bidi_unshelve_cache (NULL, false);
6473 it2.string_from_display_prop_p = false;
6474 it2.from_disp_prop_p = false;
6475 if (handle_display_prop (&it2) == HANDLED_RETURN
6476 && !NILP (val = get_char_property_and_overlay
6477 (make_number (pos), Qdisplay, Qnil, &overlay))
6478 && (OVERLAYP (overlay)
6479 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6480 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6482 RESTORE_IT (it, it, it2data);
6483 goto replaced;
6486 /* Newline is not replaced by anything -- so we are done. */
6487 RESTORE_IT (it, it, it2data);
6488 break;
6490 replaced:
6491 if (beg < BEGV)
6492 beg = BEGV;
6493 IT_CHARPOS (*it) = beg;
6494 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6498 it->continuation_lines_width = 0;
6500 eassert (IT_CHARPOS (*it) >= BEGV);
6501 eassert (IT_CHARPOS (*it) == BEGV
6502 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6503 CHECK_IT (it);
6507 /* Reseat iterator IT at the previous visible line start. Skip
6508 invisible text that is so either due to text properties or due to
6509 selective display. At the end, update IT's overlay information,
6510 face information etc. */
6512 void
6513 reseat_at_previous_visible_line_start (struct it *it)
6515 back_to_previous_visible_line_start (it);
6516 reseat (it, it->current.pos, true);
6517 CHECK_IT (it);
6521 /* Reseat iterator IT on the next visible line start in the current
6522 buffer. ON_NEWLINE_P means position IT on the newline
6523 preceding the line start. Skip over invisible text that is so
6524 because of selective display. Compute faces, overlays etc at the
6525 new position. Note that this function does not skip over text that
6526 is invisible because of text properties. */
6528 static void
6529 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6531 bool skipped_p = false;
6532 struct bidi_it bidi_it_prev;
6533 bool newline_found_p
6534 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6536 /* Skip over lines that are invisible because they are indented
6537 more than the value of IT->selective. */
6538 if (it->selective > 0)
6539 while (IT_CHARPOS (*it) < ZV
6540 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6541 it->selective))
6543 eassert (IT_BYTEPOS (*it) == BEGV
6544 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6545 newline_found_p =
6546 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6549 /* Position on the newline if that's what's requested. */
6550 if (on_newline_p && newline_found_p)
6552 if (STRINGP (it->string))
6554 if (IT_STRING_CHARPOS (*it) > 0)
6556 if (!it->bidi_p)
6558 --IT_STRING_CHARPOS (*it);
6559 --IT_STRING_BYTEPOS (*it);
6561 else
6563 /* We need to restore the bidi iterator to the state
6564 it had on the newline, and resync the IT's
6565 position with that. */
6566 it->bidi_it = bidi_it_prev;
6567 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6568 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6572 else if (IT_CHARPOS (*it) > BEGV)
6574 if (!it->bidi_p)
6576 --IT_CHARPOS (*it);
6577 --IT_BYTEPOS (*it);
6579 else
6581 /* We need to restore the bidi iterator to the state it
6582 had on the newline and resync IT with that. */
6583 it->bidi_it = bidi_it_prev;
6584 IT_CHARPOS (*it) = it->bidi_it.charpos;
6585 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6587 reseat (it, it->current.pos, false);
6590 else if (skipped_p)
6591 reseat (it, it->current.pos, false);
6593 CHECK_IT (it);
6598 /***********************************************************************
6599 Changing an iterator's position
6600 ***********************************************************************/
6602 /* Change IT's current position to POS in current_buffer.
6603 If FORCE_P, always check for text properties at the new position.
6604 Otherwise, text properties are only looked up if POS >=
6605 IT->check_charpos of a property. */
6607 static void
6608 reseat (struct it *it, struct text_pos pos, bool force_p)
6610 ptrdiff_t original_pos = IT_CHARPOS (*it);
6612 reseat_1 (it, pos, false);
6614 /* Determine where to check text properties. Avoid doing it
6615 where possible because text property lookup is very expensive. */
6616 if (force_p
6617 || CHARPOS (pos) > it->stop_charpos
6618 || CHARPOS (pos) < original_pos)
6620 if (it->bidi_p)
6622 /* For bidi iteration, we need to prime prev_stop and
6623 base_level_stop with our best estimations. */
6624 /* Implementation note: Of course, POS is not necessarily a
6625 stop position, so assigning prev_pos to it is a lie; we
6626 should have called compute_stop_backwards. However, if
6627 the current buffer does not include any R2L characters,
6628 that call would be a waste of cycles, because the
6629 iterator will never move back, and thus never cross this
6630 "fake" stop position. So we delay that backward search
6631 until the time we really need it, in next_element_from_buffer. */
6632 if (CHARPOS (pos) != it->prev_stop)
6633 it->prev_stop = CHARPOS (pos);
6634 if (CHARPOS (pos) < it->base_level_stop)
6635 it->base_level_stop = 0; /* meaning it's unknown */
6636 handle_stop (it);
6638 else
6640 handle_stop (it);
6641 it->prev_stop = it->base_level_stop = 0;
6646 CHECK_IT (it);
6650 /* Change IT's buffer position to POS. SET_STOP_P means set
6651 IT->stop_pos to POS, also. */
6653 static void
6654 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6656 /* Don't call this function when scanning a C string. */
6657 eassert (it->s == NULL);
6659 /* POS must be a reasonable value. */
6660 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6662 it->current.pos = it->position = pos;
6663 it->end_charpos = ZV;
6664 it->dpvec = NULL;
6665 it->current.dpvec_index = -1;
6666 it->current.overlay_string_index = -1;
6667 IT_STRING_CHARPOS (*it) = -1;
6668 IT_STRING_BYTEPOS (*it) = -1;
6669 it->string = Qnil;
6670 it->method = GET_FROM_BUFFER;
6671 it->object = it->w->contents;
6672 it->area = TEXT_AREA;
6673 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6674 it->sp = 0;
6675 it->string_from_display_prop_p = false;
6676 it->string_from_prefix_prop_p = false;
6678 it->from_disp_prop_p = false;
6679 it->face_before_selective_p = false;
6680 if (it->bidi_p)
6682 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6683 &it->bidi_it);
6684 bidi_unshelve_cache (NULL, false);
6685 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6686 it->bidi_it.string.s = NULL;
6687 it->bidi_it.string.lstring = Qnil;
6688 it->bidi_it.string.bufpos = 0;
6689 it->bidi_it.string.from_disp_str = false;
6690 it->bidi_it.string.unibyte = false;
6691 it->bidi_it.w = it->w;
6694 if (set_stop_p)
6696 it->stop_charpos = CHARPOS (pos);
6697 it->base_level_stop = CHARPOS (pos);
6699 /* This make the information stored in it->cmp_it invalidate. */
6700 it->cmp_it.id = -1;
6704 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6705 If S is non-null, it is a C string to iterate over. Otherwise,
6706 STRING gives a Lisp string to iterate over.
6708 If PRECISION > 0, don't return more then PRECISION number of
6709 characters from the string.
6711 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6712 characters have been returned. FIELD_WIDTH < 0 means an infinite
6713 field width.
6715 MULTIBYTE = 0 means disable processing of multibyte characters,
6716 MULTIBYTE > 0 means enable it,
6717 MULTIBYTE < 0 means use IT->multibyte_p.
6719 IT must be initialized via a prior call to init_iterator before
6720 calling this function. */
6722 static void
6723 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6724 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6725 int multibyte)
6727 /* No text property checks performed by default, but see below. */
6728 it->stop_charpos = -1;
6730 /* Set iterator position and end position. */
6731 memset (&it->current, 0, sizeof it->current);
6732 it->current.overlay_string_index = -1;
6733 it->current.dpvec_index = -1;
6734 eassert (charpos >= 0);
6736 /* If STRING is specified, use its multibyteness, otherwise use the
6737 setting of MULTIBYTE, if specified. */
6738 if (multibyte >= 0)
6739 it->multibyte_p = multibyte > 0;
6741 /* Bidirectional reordering of strings is controlled by the default
6742 value of bidi-display-reordering. Don't try to reorder while
6743 loading loadup.el, as the necessary character property tables are
6744 not yet available. */
6745 it->bidi_p =
6746 !redisplay__inhibit_bidi
6747 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6749 if (s == NULL)
6751 eassert (STRINGP (string));
6752 it->string = string;
6753 it->s = NULL;
6754 it->end_charpos = it->string_nchars = SCHARS (string);
6755 it->method = GET_FROM_STRING;
6756 it->current.string_pos = string_pos (charpos, string);
6758 if (it->bidi_p)
6760 it->bidi_it.string.lstring = string;
6761 it->bidi_it.string.s = NULL;
6762 it->bidi_it.string.schars = it->end_charpos;
6763 it->bidi_it.string.bufpos = 0;
6764 it->bidi_it.string.from_disp_str = false;
6765 it->bidi_it.string.unibyte = !it->multibyte_p;
6766 it->bidi_it.w = it->w;
6767 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6768 FRAME_WINDOW_P (it->f), &it->bidi_it);
6771 else
6773 it->s = (const unsigned char *) s;
6774 it->string = Qnil;
6776 /* Note that we use IT->current.pos, not it->current.string_pos,
6777 for displaying C strings. */
6778 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6779 if (it->multibyte_p)
6781 it->current.pos = c_string_pos (charpos, s, true);
6782 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6784 else
6786 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6787 it->end_charpos = it->string_nchars = strlen (s);
6790 if (it->bidi_p)
6792 it->bidi_it.string.lstring = Qnil;
6793 it->bidi_it.string.s = (const unsigned char *) s;
6794 it->bidi_it.string.schars = it->end_charpos;
6795 it->bidi_it.string.bufpos = 0;
6796 it->bidi_it.string.from_disp_str = false;
6797 it->bidi_it.string.unibyte = !it->multibyte_p;
6798 it->bidi_it.w = it->w;
6799 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6800 &it->bidi_it);
6802 it->method = GET_FROM_C_STRING;
6805 /* PRECISION > 0 means don't return more than PRECISION characters
6806 from the string. */
6807 if (precision > 0 && it->end_charpos - charpos > precision)
6809 it->end_charpos = it->string_nchars = charpos + precision;
6810 if (it->bidi_p)
6811 it->bidi_it.string.schars = it->end_charpos;
6814 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6815 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6816 FIELD_WIDTH < 0 means infinite field width. This is useful for
6817 padding with `-' at the end of a mode line. */
6818 if (field_width < 0)
6819 field_width = DISP_INFINITY;
6820 /* Implementation note: We deliberately don't enlarge
6821 it->bidi_it.string.schars here to fit it->end_charpos, because
6822 the bidi iterator cannot produce characters out of thin air. */
6823 if (field_width > it->end_charpos - charpos)
6824 it->end_charpos = charpos + field_width;
6826 /* Use the standard display table for displaying strings. */
6827 if (DISP_TABLE_P (Vstandard_display_table))
6828 it->dp = XCHAR_TABLE (Vstandard_display_table);
6830 it->stop_charpos = charpos;
6831 it->prev_stop = charpos;
6832 it->base_level_stop = 0;
6833 if (it->bidi_p)
6835 it->bidi_it.first_elt = true;
6836 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6837 it->bidi_it.disp_pos = -1;
6839 if (s == NULL && it->multibyte_p)
6841 ptrdiff_t endpos = SCHARS (it->string);
6842 if (endpos > it->end_charpos)
6843 endpos = it->end_charpos;
6844 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6845 it->string);
6847 CHECK_IT (it);
6852 /***********************************************************************
6853 Iteration
6854 ***********************************************************************/
6856 /* Map enum it_method value to corresponding next_element_from_* function. */
6858 typedef bool (*next_element_function) (struct it *);
6860 static next_element_function const get_next_element[NUM_IT_METHODS] =
6862 next_element_from_buffer,
6863 next_element_from_display_vector,
6864 next_element_from_string,
6865 next_element_from_c_string,
6866 next_element_from_image,
6867 next_element_from_stretch,
6868 next_element_from_xwidget,
6871 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6874 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6875 (possibly with the following characters). */
6877 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6878 ((IT)->cmp_it.id >= 0 \
6879 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6880 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6881 END_CHARPOS, (IT)->w, \
6882 FACE_FROM_ID_OR_NULL ((IT)->f, \
6883 (IT)->face_id), \
6884 (IT)->string)))
6887 /* Lookup the char-table Vglyphless_char_display for character C (-1
6888 if we want information for no-font case), and return the display
6889 method symbol. By side-effect, update it->what and
6890 it->glyphless_method. This function is called from
6891 get_next_display_element for each character element, and from
6892 x_produce_glyphs when no suitable font was found. */
6894 Lisp_Object
6895 lookup_glyphless_char_display (int c, struct it *it)
6897 Lisp_Object glyphless_method = Qnil;
6899 if (CHAR_TABLE_P (Vglyphless_char_display)
6900 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6902 if (c >= 0)
6904 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6905 if (CONSP (glyphless_method))
6906 glyphless_method = FRAME_WINDOW_P (it->f)
6907 ? XCAR (glyphless_method)
6908 : XCDR (glyphless_method);
6910 else
6911 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6914 retry:
6915 if (NILP (glyphless_method))
6917 if (c >= 0)
6918 /* The default is to display the character by a proper font. */
6919 return Qnil;
6920 /* The default for the no-font case is to display an empty box. */
6921 glyphless_method = Qempty_box;
6923 if (EQ (glyphless_method, Qzero_width))
6925 if (c >= 0)
6926 return glyphless_method;
6927 /* This method can't be used for the no-font case. */
6928 glyphless_method = Qempty_box;
6930 if (EQ (glyphless_method, Qthin_space))
6931 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6932 else if (EQ (glyphless_method, Qempty_box))
6933 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6934 else if (EQ (glyphless_method, Qhex_code))
6935 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6936 else if (STRINGP (glyphless_method))
6937 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6938 else
6940 /* Invalid value. We use the default method. */
6941 glyphless_method = Qnil;
6942 goto retry;
6944 it->what = IT_GLYPHLESS;
6945 return glyphless_method;
6948 /* Merge escape glyph face and cache the result. */
6950 static struct frame *last_escape_glyph_frame = NULL;
6951 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6952 static int last_escape_glyph_merged_face_id = 0;
6954 static int
6955 merge_escape_glyph_face (struct it *it)
6957 int face_id;
6959 if (it->f == last_escape_glyph_frame
6960 && it->face_id == last_escape_glyph_face_id)
6961 face_id = last_escape_glyph_merged_face_id;
6962 else
6964 /* Merge the `escape-glyph' face into the current face. */
6965 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6966 last_escape_glyph_frame = it->f;
6967 last_escape_glyph_face_id = it->face_id;
6968 last_escape_glyph_merged_face_id = face_id;
6970 return face_id;
6973 /* Likewise for glyphless glyph face. */
6975 static struct frame *last_glyphless_glyph_frame = NULL;
6976 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6977 static int last_glyphless_glyph_merged_face_id = 0;
6980 merge_glyphless_glyph_face (struct it *it)
6982 int face_id;
6984 if (it->f == last_glyphless_glyph_frame
6985 && it->face_id == last_glyphless_glyph_face_id)
6986 face_id = last_glyphless_glyph_merged_face_id;
6987 else
6989 /* Merge the `glyphless-char' face into the current face. */
6990 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6991 last_glyphless_glyph_frame = it->f;
6992 last_glyphless_glyph_face_id = it->face_id;
6993 last_glyphless_glyph_merged_face_id = face_id;
6995 return face_id;
6998 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6999 be called before redisplaying windows, and when the frame's face
7000 cache is freed. */
7001 void
7002 forget_escape_and_glyphless_faces (void)
7004 last_escape_glyph_frame = NULL;
7005 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
7006 last_glyphless_glyph_frame = NULL;
7007 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
7010 /* Load IT's display element fields with information about the next
7011 display element from the current position of IT. Value is false if
7012 end of buffer (or C string) is reached. */
7014 static bool
7015 get_next_display_element (struct it *it)
7017 /* True means that we found a display element. False means that
7018 we hit the end of what we iterate over. Performance note: the
7019 function pointer `method' used here turns out to be faster than
7020 using a sequence of if-statements. */
7021 bool success_p;
7023 get_next:
7024 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7026 if (it->what == IT_CHARACTER)
7028 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7029 and only if (a) the resolved directionality of that character
7030 is R..." */
7031 /* FIXME: Do we need an exception for characters from display
7032 tables? */
7033 if (it->bidi_p && it->bidi_it.type == STRONG_R
7034 && !inhibit_bidi_mirroring)
7035 it->c = bidi_mirror_char (it->c);
7036 /* Map via display table or translate control characters.
7037 IT->c, IT->len etc. have been set to the next character by
7038 the function call above. If we have a display table, and it
7039 contains an entry for IT->c, translate it. Don't do this if
7040 IT->c itself comes from a display table, otherwise we could
7041 end up in an infinite recursion. (An alternative could be to
7042 count the recursion depth of this function and signal an
7043 error when a certain maximum depth is reached.) Is it worth
7044 it? */
7045 if (success_p && it->dpvec == NULL)
7047 Lisp_Object dv;
7048 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7049 bool nonascii_space_p = false;
7050 bool nonascii_hyphen_p = false;
7051 int c = it->c; /* This is the character to display. */
7053 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7055 eassert (SINGLE_BYTE_CHAR_P (c));
7056 if (unibyte_display_via_language_environment)
7058 c = DECODE_CHAR (unibyte, c);
7059 if (c < 0)
7060 c = BYTE8_TO_CHAR (it->c);
7062 else
7063 c = BYTE8_TO_CHAR (it->c);
7066 if (it->dp
7067 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7068 VECTORP (dv)))
7070 struct Lisp_Vector *v = XVECTOR (dv);
7072 /* Return the first character from the display table
7073 entry, if not empty. If empty, don't display the
7074 current character. */
7075 if (v->header.size)
7077 it->dpvec_char_len = it->len;
7078 it->dpvec = v->contents;
7079 it->dpend = v->contents + v->header.size;
7080 it->current.dpvec_index = 0;
7081 it->dpvec_face_id = -1;
7082 it->saved_face_id = it->face_id;
7083 it->method = GET_FROM_DISPLAY_VECTOR;
7084 it->ellipsis_p = false;
7086 else
7088 set_iterator_to_next (it, false);
7090 goto get_next;
7093 if (! NILP (lookup_glyphless_char_display (c, it)))
7095 if (it->what == IT_GLYPHLESS)
7096 goto done;
7097 /* Don't display this character. */
7098 set_iterator_to_next (it, false);
7099 goto get_next;
7102 /* If `nobreak-char-display' is non-nil, we display
7103 non-ASCII spaces and hyphens specially. */
7104 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7106 if (c == NO_BREAK_SPACE)
7107 nonascii_space_p = true;
7108 else if (c == SOFT_HYPHEN || c == HYPHEN
7109 || c == NON_BREAKING_HYPHEN)
7110 nonascii_hyphen_p = true;
7113 /* Translate control characters into `\003' or `^C' form.
7114 Control characters coming from a display table entry are
7115 currently not translated because we use IT->dpvec to hold
7116 the translation. This could easily be changed but I
7117 don't believe that it is worth doing.
7119 The characters handled by `nobreak-char-display' must be
7120 translated too.
7122 Non-printable characters and raw-byte characters are also
7123 translated to octal or hexadecimal form. */
7124 if (((c < ' ' || c == 127) /* ASCII control chars. */
7125 ? (it->area != TEXT_AREA
7126 /* In mode line, treat \n, \t like other crl chars. */
7127 || (c != '\t'
7128 && it->glyph_row
7129 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7130 || (c != '\n' && c != '\t'))
7131 : (nonascii_space_p
7132 || nonascii_hyphen_p
7133 || CHAR_BYTE8_P (c)
7134 || ! CHAR_PRINTABLE_P (c))))
7136 /* C is a control character, non-ASCII space/hyphen,
7137 raw-byte, or a non-printable character which must be
7138 displayed either as '\003' or as `^C' where the '\\'
7139 and '^' can be defined in the display table. Fill
7140 IT->ctl_chars with glyphs for what we have to
7141 display. Then, set IT->dpvec to these glyphs. */
7142 Lisp_Object gc;
7143 int ctl_len;
7144 int face_id;
7145 int lface_id = 0;
7146 int escape_glyph;
7148 /* Handle control characters with ^. */
7150 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7152 int g;
7154 g = '^'; /* default glyph for Control */
7155 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7156 if (it->dp
7157 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7159 g = GLYPH_CODE_CHAR (gc);
7160 lface_id = GLYPH_CODE_FACE (gc);
7163 face_id = (lface_id
7164 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7165 : merge_escape_glyph_face (it));
7167 XSETINT (it->ctl_chars[0], g);
7168 XSETINT (it->ctl_chars[1], c ^ 0100);
7169 ctl_len = 2;
7170 goto display_control;
7173 /* Handle non-ascii space in the mode where it only gets
7174 highlighting. */
7176 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7178 /* Merge `nobreak-space' into the current face. */
7179 face_id = merge_faces (it->f, Qnobreak_space, 0,
7180 it->face_id);
7181 XSETINT (it->ctl_chars[0], ' ');
7182 ctl_len = 1;
7183 goto display_control;
7186 /* Handle non-ascii hyphens in the mode where it only
7187 gets highlighting. */
7189 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7191 /* Merge `nobreak-space' into the current face. */
7192 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7193 it->face_id);
7194 XSETINT (it->ctl_chars[0], '-');
7195 ctl_len = 1;
7196 goto display_control;
7199 /* Handle sequences that start with the "escape glyph". */
7201 /* the default escape glyph is \. */
7202 escape_glyph = '\\';
7204 if (it->dp
7205 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7207 escape_glyph = GLYPH_CODE_CHAR (gc);
7208 lface_id = GLYPH_CODE_FACE (gc);
7211 face_id = (lface_id
7212 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7213 : merge_escape_glyph_face (it));
7215 /* Draw non-ASCII space/hyphen with escape glyph: */
7217 if (nonascii_space_p || nonascii_hyphen_p)
7219 XSETINT (it->ctl_chars[0], escape_glyph);
7220 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7221 ctl_len = 2;
7222 goto display_control;
7226 char str[10];
7227 int len, i;
7229 if (CHAR_BYTE8_P (c))
7230 /* Display \200 or \x80 instead of \17777600. */
7231 c = CHAR_TO_BYTE8 (c);
7232 const char *format_string = display_raw_bytes_as_hex
7233 ? "x%02x"
7234 : "%03o";
7235 len = sprintf (str, format_string, c + 0u);
7237 XSETINT (it->ctl_chars[0], escape_glyph);
7238 for (i = 0; i < len; i++)
7239 XSETINT (it->ctl_chars[i + 1], str[i]);
7240 ctl_len = len + 1;
7243 display_control:
7244 /* Set up IT->dpvec and return first character from it. */
7245 it->dpvec_char_len = it->len;
7246 it->dpvec = it->ctl_chars;
7247 it->dpend = it->dpvec + ctl_len;
7248 it->current.dpvec_index = 0;
7249 it->dpvec_face_id = face_id;
7250 it->saved_face_id = it->face_id;
7251 it->method = GET_FROM_DISPLAY_VECTOR;
7252 it->ellipsis_p = false;
7253 goto get_next;
7255 it->char_to_display = c;
7257 else if (success_p)
7259 it->char_to_display = it->c;
7263 #ifdef HAVE_WINDOW_SYSTEM
7264 /* Adjust face id for a multibyte character. There are no multibyte
7265 character in unibyte text. */
7266 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7267 && it->multibyte_p
7268 && success_p
7269 && FRAME_WINDOW_P (it->f))
7271 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7273 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7275 /* Automatic composition with glyph-string. */
7276 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7278 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7280 else
7282 ptrdiff_t pos = (it->s ? -1
7283 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7284 : IT_CHARPOS (*it));
7285 int c;
7287 if (it->what == IT_CHARACTER)
7288 c = it->char_to_display;
7289 else
7291 struct composition *cmp = composition_table[it->cmp_it.id];
7292 int i;
7294 c = ' ';
7295 for (i = 0; i < cmp->glyph_len; i++)
7296 /* TAB in a composition means display glyphs with
7297 padding space on the left or right. */
7298 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7299 break;
7301 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7304 #endif /* HAVE_WINDOW_SYSTEM */
7306 done:
7307 /* Is this character the last one of a run of characters with
7308 box? If yes, set IT->end_of_box_run_p to true. */
7309 if (it->face_box_p
7310 && it->s == NULL)
7312 if (it->method == GET_FROM_STRING && it->sp)
7314 int face_id = underlying_face_id (it);
7315 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7317 if (face)
7319 if (face->box == FACE_NO_BOX)
7321 /* If the box comes from face properties in a
7322 display string, check faces in that string. */
7323 int string_face_id = face_after_it_pos (it);
7324 it->end_of_box_run_p
7325 = (FACE_FROM_ID (it->f, string_face_id)->box
7326 == FACE_NO_BOX);
7328 /* Otherwise, the box comes from the underlying face.
7329 If this is the last string character displayed, check
7330 the next buffer location. */
7331 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7332 /* n_overlay_strings is unreliable unless
7333 overlay_string_index is non-negative. */
7334 && ((it->current.overlay_string_index >= 0
7335 && (it->current.overlay_string_index
7336 == it->n_overlay_strings - 1))
7337 /* A string from display property. */
7338 || it->from_disp_prop_p))
7340 ptrdiff_t ignore;
7341 int next_face_id;
7342 bool text_from_string = false;
7343 /* Normally, the next buffer location is stored in
7344 IT->current.pos... */
7345 struct text_pos pos = it->current.pos;
7347 /* ...but for a string from a display property, the
7348 next buffer position is stored in the 'position'
7349 member of the iteration stack slot below the
7350 current one, see handle_single_display_spec. By
7351 contrast, it->current.pos was not yet updated to
7352 point to that buffer position; that will happen
7353 in pop_it, after we finish displaying the current
7354 string. Note that we already checked above that
7355 it->sp is positive, so subtracting one from it is
7356 safe. */
7357 if (it->from_disp_prop_p)
7359 int stackp = it->sp - 1;
7361 /* Find the stack level with data from buffer. */
7362 while (stackp >= 0
7363 && STRINGP ((it->stack + stackp)->string))
7364 stackp--;
7365 if (stackp < 0)
7367 /* If no stack slot was found for iterating
7368 a buffer, we are displaying text from a
7369 string, most probably the mode line or
7370 the header line, and that string has a
7371 display string on some of its
7372 characters. */
7373 text_from_string = true;
7374 pos = it->stack[it->sp - 1].position;
7376 else
7377 pos = (it->stack + stackp)->position;
7379 else
7380 INC_TEXT_POS (pos, it->multibyte_p);
7382 if (text_from_string)
7384 Lisp_Object base_string = it->stack[it->sp - 1].string;
7386 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7387 it->end_of_box_run_p = true;
7388 else
7390 next_face_id
7391 = face_at_string_position (it->w, base_string,
7392 CHARPOS (pos), 0,
7393 &ignore, face_id, false);
7394 it->end_of_box_run_p
7395 = (FACE_FROM_ID (it->f, next_face_id)->box
7396 == FACE_NO_BOX);
7399 else if (CHARPOS (pos) >= ZV)
7400 it->end_of_box_run_p = true;
7401 else
7403 next_face_id =
7404 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7405 CHARPOS (pos)
7406 + TEXT_PROP_DISTANCE_LIMIT,
7407 false, -1);
7408 it->end_of_box_run_p
7409 = (FACE_FROM_ID (it->f, next_face_id)->box
7410 == FACE_NO_BOX);
7415 /* next_element_from_display_vector sets this flag according to
7416 faces of the display vector glyphs, see there. */
7417 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7419 int face_id = face_after_it_pos (it);
7420 it->end_of_box_run_p
7421 = (face_id != it->face_id
7422 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7425 /* If we reached the end of the object we've been iterating (e.g., a
7426 display string or an overlay string), and there's something on
7427 IT->stack, proceed with what's on the stack. It doesn't make
7428 sense to return false if there's unprocessed stuff on the stack,
7429 because otherwise that stuff will never be displayed. */
7430 if (!success_p && it->sp > 0)
7432 set_iterator_to_next (it, false);
7433 success_p = get_next_display_element (it);
7436 /* Value is false if end of buffer or string reached. */
7437 return success_p;
7441 /* Move IT to the next display element.
7443 RESEAT_P means if called on a newline in buffer text,
7444 skip to the next visible line start.
7446 Functions get_next_display_element and set_iterator_to_next are
7447 separate because I find this arrangement easier to handle than a
7448 get_next_display_element function that also increments IT's
7449 position. The way it is we can first look at an iterator's current
7450 display element, decide whether it fits on a line, and if it does,
7451 increment the iterator position. The other way around we probably
7452 would either need a flag indicating whether the iterator has to be
7453 incremented the next time, or we would have to implement a
7454 decrement position function which would not be easy to write. */
7456 void
7457 set_iterator_to_next (struct it *it, bool reseat_p)
7459 /* Reset flags indicating start and end of a sequence of characters
7460 with box. Reset them at the start of this function because
7461 moving the iterator to a new position might set them. */
7462 it->start_of_box_run_p = it->end_of_box_run_p = false;
7464 switch (it->method)
7466 case GET_FROM_BUFFER:
7467 /* The current display element of IT is a character from
7468 current_buffer. Advance in the buffer, and maybe skip over
7469 invisible lines that are so because of selective display. */
7470 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7471 reseat_at_next_visible_line_start (it, false);
7472 else if (it->cmp_it.id >= 0)
7474 /* We are currently getting glyphs from a composition. */
7475 if (! it->bidi_p)
7477 IT_CHARPOS (*it) += it->cmp_it.nchars;
7478 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7480 else
7482 int i;
7484 /* Update IT's char/byte positions to point to the first
7485 character of the next grapheme cluster, or to the
7486 character visually after the current composition. */
7487 for (i = 0; i < it->cmp_it.nchars; i++)
7488 bidi_move_to_visually_next (&it->bidi_it);
7489 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7490 IT_CHARPOS (*it) = it->bidi_it.charpos;
7493 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7494 && it->cmp_it.to < it->cmp_it.nglyphs)
7496 /* Composition created while scanning forward. Proceed
7497 to the next grapheme cluster. */
7498 it->cmp_it.from = it->cmp_it.to;
7500 else if ((it->bidi_p && it->cmp_it.reversed_p)
7501 && it->cmp_it.from > 0)
7503 /* Composition created while scanning backward. Proceed
7504 to the previous grapheme cluster. */
7505 it->cmp_it.to = it->cmp_it.from;
7507 else
7509 /* No more grapheme clusters in this composition.
7510 Find the next stop position. */
7511 ptrdiff_t stop = it->end_charpos;
7513 if (it->bidi_it.scan_dir < 0)
7514 /* Now we are scanning backward and don't know
7515 where to stop. */
7516 stop = -1;
7517 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7518 IT_BYTEPOS (*it), stop, Qnil);
7521 else
7523 eassert (it->len != 0);
7525 if (!it->bidi_p)
7527 IT_BYTEPOS (*it) += it->len;
7528 IT_CHARPOS (*it) += 1;
7530 else
7532 int prev_scan_dir = it->bidi_it.scan_dir;
7533 /* If this is a new paragraph, determine its base
7534 direction (a.k.a. its base embedding level). */
7535 if (it->bidi_it.new_paragraph)
7536 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7537 false);
7538 bidi_move_to_visually_next (&it->bidi_it);
7539 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7540 IT_CHARPOS (*it) = it->bidi_it.charpos;
7541 if (prev_scan_dir != it->bidi_it.scan_dir)
7543 /* As the scan direction was changed, we must
7544 re-compute the stop position for composition. */
7545 ptrdiff_t stop = it->end_charpos;
7546 if (it->bidi_it.scan_dir < 0)
7547 stop = -1;
7548 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7549 IT_BYTEPOS (*it), stop, Qnil);
7552 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7554 break;
7556 case GET_FROM_C_STRING:
7557 /* Current display element of IT is from a C string. */
7558 if (!it->bidi_p
7559 /* If the string position is beyond string's end, it means
7560 next_element_from_c_string is padding the string with
7561 blanks, in which case we bypass the bidi iterator,
7562 because it cannot deal with such virtual characters. */
7563 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7565 IT_BYTEPOS (*it) += it->len;
7566 IT_CHARPOS (*it) += 1;
7568 else
7570 bidi_move_to_visually_next (&it->bidi_it);
7571 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7572 IT_CHARPOS (*it) = it->bidi_it.charpos;
7574 break;
7576 case GET_FROM_DISPLAY_VECTOR:
7577 /* Current display element of IT is from a display table entry.
7578 Advance in the display table definition. Reset it to null if
7579 end reached, and continue with characters from buffers/
7580 strings. */
7581 ++it->current.dpvec_index;
7583 /* Restore face of the iterator to what they were before the
7584 display vector entry (these entries may contain faces). */
7585 it->face_id = it->saved_face_id;
7587 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7589 bool recheck_faces = it->ellipsis_p;
7591 if (it->s)
7592 it->method = GET_FROM_C_STRING;
7593 else if (STRINGP (it->string))
7594 it->method = GET_FROM_STRING;
7595 else
7597 it->method = GET_FROM_BUFFER;
7598 it->object = it->w->contents;
7601 it->dpvec = NULL;
7602 it->current.dpvec_index = -1;
7604 /* Skip over characters which were displayed via IT->dpvec. */
7605 if (it->dpvec_char_len < 0)
7606 reseat_at_next_visible_line_start (it, true);
7607 else if (it->dpvec_char_len > 0)
7609 it->len = it->dpvec_char_len;
7610 set_iterator_to_next (it, reseat_p);
7613 /* Maybe recheck faces after display vector. */
7614 if (recheck_faces)
7616 if (it->method == GET_FROM_STRING)
7617 it->stop_charpos = IT_STRING_CHARPOS (*it);
7618 else
7619 it->stop_charpos = IT_CHARPOS (*it);
7622 break;
7624 case GET_FROM_STRING:
7625 /* Current display element is a character from a Lisp string. */
7626 eassert (it->s == NULL && STRINGP (it->string));
7627 /* Don't advance past string end. These conditions are true
7628 when set_iterator_to_next is called at the end of
7629 get_next_display_element, in which case the Lisp string is
7630 already exhausted, and all we want is pop the iterator
7631 stack. */
7632 if (it->current.overlay_string_index >= 0)
7634 /* This is an overlay string, so there's no padding with
7635 spaces, and the number of characters in the string is
7636 where the string ends. */
7637 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7638 goto consider_string_end;
7640 else
7642 /* Not an overlay string. There could be padding, so test
7643 against it->end_charpos. */
7644 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7645 goto consider_string_end;
7647 if (it->cmp_it.id >= 0)
7649 /* We are delivering display elements from a composition.
7650 Update the string position past the grapheme cluster
7651 we've just processed. */
7652 if (! it->bidi_p)
7654 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7655 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7657 else
7659 int i;
7661 for (i = 0; i < it->cmp_it.nchars; i++)
7662 bidi_move_to_visually_next (&it->bidi_it);
7663 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7664 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7667 /* Did we exhaust all the grapheme clusters of this
7668 composition? */
7669 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7670 && (it->cmp_it.to < it->cmp_it.nglyphs))
7672 /* Not all the grapheme clusters were processed yet;
7673 advance to the next cluster. */
7674 it->cmp_it.from = it->cmp_it.to;
7676 else if ((it->bidi_p && it->cmp_it.reversed_p)
7677 && it->cmp_it.from > 0)
7679 /* Likewise: advance to the next cluster, but going in
7680 the reverse direction. */
7681 it->cmp_it.to = it->cmp_it.from;
7683 else
7685 /* This composition was fully processed; find the next
7686 candidate place for checking for composed
7687 characters. */
7688 /* Always limit string searches to the string length;
7689 any padding spaces are not part of the string, and
7690 there cannot be any compositions in that padding. */
7691 ptrdiff_t stop = SCHARS (it->string);
7693 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7694 stop = -1;
7695 else if (it->end_charpos < stop)
7697 /* Cf. PRECISION in reseat_to_string: we might be
7698 limited in how many of the string characters we
7699 need to deliver. */
7700 stop = it->end_charpos;
7702 composition_compute_stop_pos (&it->cmp_it,
7703 IT_STRING_CHARPOS (*it),
7704 IT_STRING_BYTEPOS (*it), stop,
7705 it->string);
7708 else
7710 if (!it->bidi_p
7711 /* If the string position is beyond string's end, it
7712 means next_element_from_string is padding the string
7713 with blanks, in which case we bypass the bidi
7714 iterator, because it cannot deal with such virtual
7715 characters. */
7716 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7718 IT_STRING_BYTEPOS (*it) += it->len;
7719 IT_STRING_CHARPOS (*it) += 1;
7721 else
7723 int prev_scan_dir = it->bidi_it.scan_dir;
7725 bidi_move_to_visually_next (&it->bidi_it);
7726 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7727 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7728 /* If the scan direction changes, we may need to update
7729 the place where to check for composed characters. */
7730 if (prev_scan_dir != it->bidi_it.scan_dir)
7732 ptrdiff_t stop = SCHARS (it->string);
7734 if (it->bidi_it.scan_dir < 0)
7735 stop = -1;
7736 else if (it->end_charpos < stop)
7737 stop = it->end_charpos;
7739 composition_compute_stop_pos (&it->cmp_it,
7740 IT_STRING_CHARPOS (*it),
7741 IT_STRING_BYTEPOS (*it), stop,
7742 it->string);
7747 consider_string_end:
7749 if (it->current.overlay_string_index >= 0)
7751 /* IT->string is an overlay string. Advance to the
7752 next, if there is one. */
7753 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7755 it->ellipsis_p = false;
7756 next_overlay_string (it);
7757 if (it->ellipsis_p)
7758 setup_for_ellipsis (it, 0);
7761 else
7763 /* IT->string is not an overlay string. If we reached
7764 its end, and there is something on IT->stack, proceed
7765 with what is on the stack. This can be either another
7766 string, this time an overlay string, or a buffer. */
7767 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7768 && it->sp > 0)
7770 pop_it (it);
7771 if (it->method == GET_FROM_STRING)
7772 goto consider_string_end;
7775 break;
7777 case GET_FROM_IMAGE:
7778 case GET_FROM_STRETCH:
7779 case GET_FROM_XWIDGET:
7781 /* The position etc with which we have to proceed are on
7782 the stack. The position may be at the end of a string,
7783 if the `display' property takes up the whole string. */
7784 eassert (it->sp > 0);
7785 pop_it (it);
7786 if (it->method == GET_FROM_STRING)
7787 goto consider_string_end;
7788 break;
7790 default:
7791 /* There are no other methods defined, so this should be a bug. */
7792 emacs_abort ();
7795 eassert (it->method != GET_FROM_STRING
7796 || (STRINGP (it->string)
7797 && IT_STRING_CHARPOS (*it) >= 0));
7800 /* Load IT's display element fields with information about the next
7801 display element which comes from a display table entry or from the
7802 result of translating a control character to one of the forms `^C'
7803 or `\003'.
7805 IT->dpvec holds the glyphs to return as characters.
7806 IT->saved_face_id holds the face id before the display vector--it
7807 is restored into IT->face_id in set_iterator_to_next. */
7809 static bool
7810 next_element_from_display_vector (struct it *it)
7812 Lisp_Object gc;
7813 int prev_face_id = it->face_id;
7814 int next_face_id;
7816 /* Precondition. */
7817 eassert (it->dpvec && it->current.dpvec_index >= 0);
7819 it->face_id = it->saved_face_id;
7821 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7822 That seemed totally bogus - so I changed it... */
7823 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7824 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7826 struct face *this_face, *prev_face, *next_face;
7828 it->c = GLYPH_CODE_CHAR (gc);
7829 it->len = CHAR_BYTES (it->c);
7831 /* The entry may contain a face id to use. Such a face id is
7832 the id of a Lisp face, not a realized face. A face id of
7833 zero means no face is specified. */
7834 if (it->dpvec_face_id >= 0)
7835 it->face_id = it->dpvec_face_id;
7836 else
7838 int lface_id = GLYPH_CODE_FACE (gc);
7839 if (lface_id > 0)
7840 it->face_id = merge_faces (it->f, Qt, lface_id,
7841 it->saved_face_id);
7844 /* Glyphs in the display vector could have the box face, so we
7845 need to set the related flags in the iterator, as
7846 appropriate. */
7847 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7848 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7850 /* Is this character the first character of a box-face run? */
7851 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7852 && (!prev_face
7853 || prev_face->box == FACE_NO_BOX));
7855 /* For the last character of the box-face run, we need to look
7856 either at the next glyph from the display vector, or at the
7857 face we saw before the display vector. */
7858 next_face_id = it->saved_face_id;
7859 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7861 if (it->dpvec_face_id >= 0)
7862 next_face_id = it->dpvec_face_id;
7863 else
7865 int lface_id =
7866 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7868 if (lface_id > 0)
7869 next_face_id = merge_faces (it->f, Qt, lface_id,
7870 it->saved_face_id);
7873 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7874 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7875 && (!next_face
7876 || next_face->box == FACE_NO_BOX));
7877 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7879 else
7880 /* Display table entry is invalid. Return a space. */
7881 it->c = ' ', it->len = 1;
7883 /* Don't change position and object of the iterator here. They are
7884 still the values of the character that had this display table
7885 entry or was translated, and that's what we want. */
7886 it->what = IT_CHARACTER;
7887 return true;
7890 /* Get the first element of string/buffer in the visual order, after
7891 being reseated to a new position in a string or a buffer. */
7892 static void
7893 get_visually_first_element (struct it *it)
7895 bool string_p = STRINGP (it->string) || it->s;
7896 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7897 ptrdiff_t bob = (string_p ? 0 : BEGV);
7899 if (STRINGP (it->string))
7901 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7902 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7904 else
7906 it->bidi_it.charpos = IT_CHARPOS (*it);
7907 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7910 if (it->bidi_it.charpos == eob)
7912 /* Nothing to do, but reset the FIRST_ELT flag, like
7913 bidi_paragraph_init does, because we are not going to
7914 call it. */
7915 it->bidi_it.first_elt = false;
7917 else if (it->bidi_it.charpos == bob
7918 || (!string_p
7919 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7920 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7922 /* If we are at the beginning of a line/string, we can produce
7923 the next element right away. */
7924 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7925 bidi_move_to_visually_next (&it->bidi_it);
7927 else
7929 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7931 /* We need to prime the bidi iterator starting at the line's or
7932 string's beginning, before we will be able to produce the
7933 next element. */
7934 if (string_p)
7935 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7936 else
7937 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7938 IT_BYTEPOS (*it), -1,
7939 &it->bidi_it.bytepos);
7940 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7943 /* Now return to buffer/string position where we were asked
7944 to get the next display element, and produce that. */
7945 bidi_move_to_visually_next (&it->bidi_it);
7947 while (it->bidi_it.bytepos != orig_bytepos
7948 && it->bidi_it.charpos < eob);
7951 /* Adjust IT's position information to where we ended up. */
7952 if (STRINGP (it->string))
7954 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7955 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7957 else
7959 IT_CHARPOS (*it) = it->bidi_it.charpos;
7960 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7963 if (STRINGP (it->string) || !it->s)
7965 ptrdiff_t stop, charpos, bytepos;
7967 if (STRINGP (it->string))
7969 eassert (!it->s);
7970 stop = SCHARS (it->string);
7971 if (stop > it->end_charpos)
7972 stop = it->end_charpos;
7973 charpos = IT_STRING_CHARPOS (*it);
7974 bytepos = IT_STRING_BYTEPOS (*it);
7976 else
7978 stop = it->end_charpos;
7979 charpos = IT_CHARPOS (*it);
7980 bytepos = IT_BYTEPOS (*it);
7982 if (it->bidi_it.scan_dir < 0)
7983 stop = -1;
7984 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7985 it->string);
7989 /* Load IT with the next display element from Lisp string IT->string.
7990 IT->current.string_pos is the current position within the string.
7991 If IT->current.overlay_string_index >= 0, the Lisp string is an
7992 overlay string. */
7994 static bool
7995 next_element_from_string (struct it *it)
7997 struct text_pos position;
7999 eassert (STRINGP (it->string));
8000 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
8001 eassert (IT_STRING_CHARPOS (*it) >= 0);
8002 position = it->current.string_pos;
8004 /* With bidi reordering, the character to display might not be the
8005 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
8006 that we were reseat()ed to a new string, whose paragraph
8007 direction is not known. */
8008 if (it->bidi_p && it->bidi_it.first_elt)
8010 get_visually_first_element (it);
8011 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
8014 /* Time to check for invisible text? */
8015 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
8017 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
8019 if (!(!it->bidi_p
8020 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8021 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
8023 /* With bidi non-linear iteration, we could find
8024 ourselves far beyond the last computed stop_charpos,
8025 with several other stop positions in between that we
8026 missed. Scan them all now, in buffer's logical
8027 order, until we find and handle the last stop_charpos
8028 that precedes our current position. */
8029 handle_stop_backwards (it, it->stop_charpos);
8030 return GET_NEXT_DISPLAY_ELEMENT (it);
8032 else
8034 if (it->bidi_p)
8036 /* Take note of the stop position we just moved
8037 across, for when we will move back across it. */
8038 it->prev_stop = it->stop_charpos;
8039 /* If we are at base paragraph embedding level, take
8040 note of the last stop position seen at this
8041 level. */
8042 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8043 it->base_level_stop = it->stop_charpos;
8045 handle_stop (it);
8047 /* Since a handler may have changed IT->method, we must
8048 recurse here. */
8049 return GET_NEXT_DISPLAY_ELEMENT (it);
8052 else if (it->bidi_p
8053 /* If we are before prev_stop, we may have overstepped
8054 on our way backwards a stop_pos, and if so, we need
8055 to handle that stop_pos. */
8056 && IT_STRING_CHARPOS (*it) < it->prev_stop
8057 /* We can sometimes back up for reasons that have nothing
8058 to do with bidi reordering. E.g., compositions. The
8059 code below is only needed when we are above the base
8060 embedding level, so test for that explicitly. */
8061 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8063 /* If we lost track of base_level_stop, we have no better
8064 place for handle_stop_backwards to start from than string
8065 beginning. This happens, e.g., when we were reseated to
8066 the previous screenful of text by vertical-motion. */
8067 if (it->base_level_stop <= 0
8068 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8069 it->base_level_stop = 0;
8070 handle_stop_backwards (it, it->base_level_stop);
8071 return GET_NEXT_DISPLAY_ELEMENT (it);
8075 if (it->current.overlay_string_index >= 0)
8077 /* Get the next character from an overlay string. In overlay
8078 strings, there is no field width or padding with spaces to
8079 do. */
8080 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8082 it->what = IT_EOB;
8083 return false;
8085 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8086 IT_STRING_BYTEPOS (*it),
8087 it->bidi_it.scan_dir < 0
8088 ? -1
8089 : SCHARS (it->string))
8090 && next_element_from_composition (it))
8092 return true;
8094 else if (STRING_MULTIBYTE (it->string))
8096 const unsigned char *s = (SDATA (it->string)
8097 + IT_STRING_BYTEPOS (*it));
8098 it->c = string_char_and_length (s, &it->len);
8100 else
8102 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8103 it->len = 1;
8106 else
8108 /* Get the next character from a Lisp string that is not an
8109 overlay string. Such strings come from the mode line, for
8110 example. We may have to pad with spaces, or truncate the
8111 string. See also next_element_from_c_string. */
8112 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8114 it->what = IT_EOB;
8115 return false;
8117 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8119 /* Pad with spaces. */
8120 it->c = ' ', it->len = 1;
8121 CHARPOS (position) = BYTEPOS (position) = -1;
8123 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8124 IT_STRING_BYTEPOS (*it),
8125 it->bidi_it.scan_dir < 0
8126 ? -1
8127 : it->string_nchars)
8128 && next_element_from_composition (it))
8130 return true;
8132 else if (STRING_MULTIBYTE (it->string))
8134 const unsigned char *s = (SDATA (it->string)
8135 + IT_STRING_BYTEPOS (*it));
8136 it->c = string_char_and_length (s, &it->len);
8138 else
8140 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8141 it->len = 1;
8145 /* Record what we have and where it came from. */
8146 it->what = IT_CHARACTER;
8147 it->object = it->string;
8148 it->position = position;
8149 return true;
8153 /* Load IT with next display element from C string IT->s.
8154 IT->string_nchars is the maximum number of characters to return
8155 from the string. IT->end_charpos may be greater than
8156 IT->string_nchars when this function is called, in which case we
8157 may have to return padding spaces. Value is false if end of string
8158 reached, including padding spaces. */
8160 static bool
8161 next_element_from_c_string (struct it *it)
8163 bool success_p = true;
8165 eassert (it->s);
8166 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8167 it->what = IT_CHARACTER;
8168 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8169 it->object = make_number (0);
8171 /* With bidi reordering, the character to display might not be the
8172 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8173 we were reseated to a new string, whose paragraph direction is
8174 not known. */
8175 if (it->bidi_p && it->bidi_it.first_elt)
8176 get_visually_first_element (it);
8178 /* IT's position can be greater than IT->string_nchars in case a
8179 field width or precision has been specified when the iterator was
8180 initialized. */
8181 if (IT_CHARPOS (*it) >= it->end_charpos)
8183 /* End of the game. */
8184 it->what = IT_EOB;
8185 success_p = false;
8187 else if (IT_CHARPOS (*it) >= it->string_nchars)
8189 /* Pad with spaces. */
8190 it->c = ' ', it->len = 1;
8191 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8193 else if (it->multibyte_p)
8194 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8195 else
8196 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8198 return success_p;
8202 /* Set up IT to return characters from an ellipsis, if appropriate.
8203 The definition of the ellipsis glyphs may come from a display table
8204 entry. This function fills IT with the first glyph from the
8205 ellipsis if an ellipsis is to be displayed. */
8207 static bool
8208 next_element_from_ellipsis (struct it *it)
8210 if (it->selective_display_ellipsis_p)
8211 setup_for_ellipsis (it, it->len);
8212 else
8214 /* The face at the current position may be different from the
8215 face we find after the invisible text. Remember what it
8216 was in IT->saved_face_id, and signal that it's there by
8217 setting face_before_selective_p. */
8218 it->saved_face_id = it->face_id;
8219 it->method = GET_FROM_BUFFER;
8220 it->object = it->w->contents;
8221 reseat_at_next_visible_line_start (it, true);
8222 it->face_before_selective_p = true;
8225 return GET_NEXT_DISPLAY_ELEMENT (it);
8229 /* Deliver an image display element. The iterator IT is already
8230 filled with image information (done in handle_display_prop). Value
8231 is always true. */
8234 static bool
8235 next_element_from_image (struct it *it)
8237 it->what = IT_IMAGE;
8238 return true;
8241 static bool
8242 next_element_from_xwidget (struct it *it)
8244 it->what = IT_XWIDGET;
8245 return true;
8249 /* Fill iterator IT with next display element from a stretch glyph
8250 property. IT->object is the value of the text property. Value is
8251 always true. */
8253 static bool
8254 next_element_from_stretch (struct it *it)
8256 it->what = IT_STRETCH;
8257 return true;
8260 /* Scan backwards from IT's current position until we find a stop
8261 position, or until BEGV. This is called when we find ourself
8262 before both the last known prev_stop and base_level_stop while
8263 reordering bidirectional text. */
8265 static void
8266 compute_stop_pos_backwards (struct it *it)
8268 const int SCAN_BACK_LIMIT = 1000;
8269 struct text_pos pos;
8270 struct display_pos save_current = it->current;
8271 struct text_pos save_position = it->position;
8272 ptrdiff_t charpos = IT_CHARPOS (*it);
8273 ptrdiff_t where_we_are = charpos;
8274 ptrdiff_t save_stop_pos = it->stop_charpos;
8275 ptrdiff_t save_end_pos = it->end_charpos;
8277 eassert (NILP (it->string) && !it->s);
8278 eassert (it->bidi_p);
8279 it->bidi_p = false;
8282 it->end_charpos = min (charpos + 1, ZV);
8283 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8284 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8285 reseat_1 (it, pos, false);
8286 compute_stop_pos (it);
8287 /* We must advance forward, right? */
8288 if (it->stop_charpos <= charpos)
8289 emacs_abort ();
8291 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8293 if (it->stop_charpos <= where_we_are)
8294 it->prev_stop = it->stop_charpos;
8295 else
8296 it->prev_stop = BEGV;
8297 it->bidi_p = true;
8298 it->current = save_current;
8299 it->position = save_position;
8300 it->stop_charpos = save_stop_pos;
8301 it->end_charpos = save_end_pos;
8304 /* Scan forward from CHARPOS in the current buffer/string, until we
8305 find a stop position > current IT's position. Then handle the stop
8306 position before that. This is called when we bump into a stop
8307 position while reordering bidirectional text. CHARPOS should be
8308 the last previously processed stop_pos (or BEGV/0, if none were
8309 processed yet) whose position is less that IT's current
8310 position. */
8312 static void
8313 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8315 bool bufp = !STRINGP (it->string);
8316 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8317 struct display_pos save_current = it->current;
8318 struct text_pos save_position = it->position;
8319 struct text_pos pos1;
8320 ptrdiff_t next_stop;
8322 /* Scan in strict logical order. */
8323 eassert (it->bidi_p);
8324 it->bidi_p = false;
8327 it->prev_stop = charpos;
8328 if (bufp)
8330 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8331 reseat_1 (it, pos1, false);
8333 else
8334 it->current.string_pos = string_pos (charpos, it->string);
8335 compute_stop_pos (it);
8336 /* We must advance forward, right? */
8337 if (it->stop_charpos <= it->prev_stop)
8338 emacs_abort ();
8339 charpos = it->stop_charpos;
8341 while (charpos <= where_we_are);
8343 it->bidi_p = true;
8344 it->current = save_current;
8345 it->position = save_position;
8346 next_stop = it->stop_charpos;
8347 it->stop_charpos = it->prev_stop;
8348 handle_stop (it);
8349 it->stop_charpos = next_stop;
8352 /* Load IT with the next display element from current_buffer. Value
8353 is false if end of buffer reached. IT->stop_charpos is the next
8354 position at which to stop and check for text properties or buffer
8355 end. */
8357 static bool
8358 next_element_from_buffer (struct it *it)
8360 bool success_p = true;
8362 eassert (IT_CHARPOS (*it) >= BEGV);
8363 eassert (NILP (it->string) && !it->s);
8364 eassert (!it->bidi_p
8365 || (EQ (it->bidi_it.string.lstring, Qnil)
8366 && it->bidi_it.string.s == NULL));
8368 /* With bidi reordering, the character to display might not be the
8369 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8370 we were reseat()ed to a new buffer position, which is potentially
8371 a different paragraph. */
8372 if (it->bidi_p && it->bidi_it.first_elt)
8374 get_visually_first_element (it);
8375 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8378 if (IT_CHARPOS (*it) >= it->stop_charpos)
8380 if (IT_CHARPOS (*it) >= it->end_charpos)
8382 bool overlay_strings_follow_p;
8384 /* End of the game, except when overlay strings follow that
8385 haven't been returned yet. */
8386 if (it->overlay_strings_at_end_processed_p)
8387 overlay_strings_follow_p = false;
8388 else
8390 it->overlay_strings_at_end_processed_p = true;
8391 overlay_strings_follow_p = get_overlay_strings (it, 0);
8394 if (overlay_strings_follow_p)
8395 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8396 else
8398 it->what = IT_EOB;
8399 it->position = it->current.pos;
8400 success_p = false;
8403 else if (!(!it->bidi_p
8404 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8405 || IT_CHARPOS (*it) == it->stop_charpos))
8407 /* With bidi non-linear iteration, we could find ourselves
8408 far beyond the last computed stop_charpos, with several
8409 other stop positions in between that we missed. Scan
8410 them all now, in buffer's logical order, until we find
8411 and handle the last stop_charpos that precedes our
8412 current position. */
8413 handle_stop_backwards (it, it->stop_charpos);
8414 it->ignore_overlay_strings_at_pos_p = false;
8415 return GET_NEXT_DISPLAY_ELEMENT (it);
8417 else
8419 if (it->bidi_p)
8421 /* Take note of the stop position we just moved across,
8422 for when we will move back across it. */
8423 it->prev_stop = it->stop_charpos;
8424 /* If we are at base paragraph embedding level, take
8425 note of the last stop position seen at this
8426 level. */
8427 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8428 it->base_level_stop = it->stop_charpos;
8430 handle_stop (it);
8431 it->ignore_overlay_strings_at_pos_p = false;
8432 return GET_NEXT_DISPLAY_ELEMENT (it);
8435 else if (it->bidi_p
8436 /* If we are before prev_stop, we may have overstepped on
8437 our way backwards a stop_pos, and if so, we need to
8438 handle that stop_pos. */
8439 && IT_CHARPOS (*it) < it->prev_stop
8440 /* We can sometimes back up for reasons that have nothing
8441 to do with bidi reordering. E.g., compositions. The
8442 code below is only needed when we are above the base
8443 embedding level, so test for that explicitly. */
8444 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8446 if (it->base_level_stop <= 0
8447 || IT_CHARPOS (*it) < it->base_level_stop)
8449 /* If we lost track of base_level_stop, we need to find
8450 prev_stop by looking backwards. This happens, e.g., when
8451 we were reseated to the previous screenful of text by
8452 vertical-motion. */
8453 it->base_level_stop = BEGV;
8454 compute_stop_pos_backwards (it);
8455 handle_stop_backwards (it, it->prev_stop);
8457 else
8458 handle_stop_backwards (it, it->base_level_stop);
8459 it->ignore_overlay_strings_at_pos_p = false;
8460 return GET_NEXT_DISPLAY_ELEMENT (it);
8462 else
8464 /* No face changes, overlays etc. in sight, so just return a
8465 character from current_buffer. */
8466 unsigned char *p;
8467 ptrdiff_t stop;
8469 /* We moved to the next buffer position, so any info about
8470 previously seen overlays is no longer valid. */
8471 it->ignore_overlay_strings_at_pos_p = false;
8473 /* Maybe run the redisplay end trigger hook. Performance note:
8474 This doesn't seem to cost measurable time. */
8475 if (it->redisplay_end_trigger_charpos
8476 && it->glyph_row
8477 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8478 run_redisplay_end_trigger_hook (it);
8480 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8481 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8482 stop)
8483 && next_element_from_composition (it))
8485 return true;
8488 /* Get the next character, maybe multibyte. */
8489 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8490 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8491 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8492 else
8493 it->c = *p, it->len = 1;
8495 /* Record what we have and where it came from. */
8496 it->what = IT_CHARACTER;
8497 it->object = it->w->contents;
8498 it->position = it->current.pos;
8500 /* Normally we return the character found above, except when we
8501 really want to return an ellipsis for selective display. */
8502 if (it->selective)
8504 if (it->c == '\n')
8506 /* A value of selective > 0 means hide lines indented more
8507 than that number of columns. */
8508 if (it->selective > 0
8509 && IT_CHARPOS (*it) + 1 < ZV
8510 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8511 IT_BYTEPOS (*it) + 1,
8512 it->selective))
8514 success_p = next_element_from_ellipsis (it);
8515 it->dpvec_char_len = -1;
8518 else if (it->c == '\r' && it->selective == -1)
8520 /* A value of selective == -1 means that everything from the
8521 CR to the end of the line is invisible, with maybe an
8522 ellipsis displayed for it. */
8523 success_p = next_element_from_ellipsis (it);
8524 it->dpvec_char_len = -1;
8529 /* Value is false if end of buffer reached. */
8530 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8531 return success_p;
8535 /* Run the redisplay end trigger hook for IT. */
8537 static void
8538 run_redisplay_end_trigger_hook (struct it *it)
8540 /* IT->glyph_row should be non-null, i.e. we should be actually
8541 displaying something, or otherwise we should not run the hook. */
8542 eassert (it->glyph_row);
8544 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8545 it->redisplay_end_trigger_charpos = 0;
8547 /* Since we are *trying* to run these functions, don't try to run
8548 them again, even if they get an error. */
8549 wset_redisplay_end_trigger (it->w, Qnil);
8550 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8551 make_number (charpos));
8553 /* Notice if it changed the face of the character we are on. */
8554 handle_face_prop (it);
8558 /* Deliver a composition display element. Unlike the other
8559 next_element_from_XXX, this function is not registered in the array
8560 get_next_element[]. It is called from next_element_from_buffer and
8561 next_element_from_string when necessary. */
8563 static bool
8564 next_element_from_composition (struct it *it)
8566 it->what = IT_COMPOSITION;
8567 it->len = it->cmp_it.nbytes;
8568 if (STRINGP (it->string))
8570 if (it->c < 0)
8572 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8573 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8574 return false;
8576 it->position = it->current.string_pos;
8577 it->object = it->string;
8578 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8579 IT_STRING_BYTEPOS (*it), it->string);
8581 else
8583 if (it->c < 0)
8585 IT_CHARPOS (*it) += it->cmp_it.nchars;
8586 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8587 if (it->bidi_p)
8589 if (it->bidi_it.new_paragraph)
8590 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8591 false);
8592 /* Resync the bidi iterator with IT's new position.
8593 FIXME: this doesn't support bidirectional text. */
8594 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8595 bidi_move_to_visually_next (&it->bidi_it);
8597 return false;
8599 it->position = it->current.pos;
8600 it->object = it->w->contents;
8601 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8602 IT_BYTEPOS (*it), Qnil);
8604 return true;
8609 /***********************************************************************
8610 Moving an iterator without producing glyphs
8611 ***********************************************************************/
8613 /* Check if iterator is at a position corresponding to a valid buffer
8614 position after some move_it_ call. */
8616 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8617 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8620 /* Move iterator IT to a specified buffer or X position within one
8621 line on the display without producing glyphs.
8623 OP should be a bit mask including some or all of these bits:
8624 MOVE_TO_X: Stop upon reaching x-position TO_X.
8625 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8626 Regardless of OP's value, stop upon reaching the end of the display line.
8628 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8629 This means, in particular, that TO_X includes window's horizontal
8630 scroll amount.
8632 The return value has several possible values that
8633 say what condition caused the scan to stop:
8635 MOVE_POS_MATCH_OR_ZV
8636 - when TO_POS or ZV was reached.
8638 MOVE_X_REACHED
8639 -when TO_X was reached before TO_POS or ZV were reached.
8641 MOVE_LINE_CONTINUED
8642 - when we reached the end of the display area and the line must
8643 be continued.
8645 MOVE_LINE_TRUNCATED
8646 - when we reached the end of the display area and the line is
8647 truncated.
8649 MOVE_NEWLINE_OR_CR
8650 - when we stopped at a line end, i.e. a newline or a CR and selective
8651 display is on. */
8653 static enum move_it_result
8654 move_it_in_display_line_to (struct it *it,
8655 ptrdiff_t to_charpos, int to_x,
8656 enum move_operation_enum op)
8658 enum move_it_result result = MOVE_UNDEFINED;
8659 struct glyph_row *saved_glyph_row;
8660 struct it wrap_it, atpos_it, atx_it, ppos_it;
8661 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8662 void *ppos_data = NULL;
8663 bool may_wrap = false;
8664 enum it_method prev_method = it->method;
8665 ptrdiff_t closest_pos UNINIT;
8666 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8667 bool saw_smaller_pos = prev_pos < to_charpos;
8668 bool line_number_pending = false;
8670 /* Don't produce glyphs in produce_glyphs. */
8671 saved_glyph_row = it->glyph_row;
8672 it->glyph_row = NULL;
8674 /* Use wrap_it to save a copy of IT wherever a word wrap could
8675 occur. Use atpos_it to save a copy of IT at the desired buffer
8676 position, if found, so that we can scan ahead and check if the
8677 word later overshoots the window edge. Use atx_it similarly, for
8678 pixel positions. */
8679 wrap_it.sp = -1;
8680 atpos_it.sp = -1;
8681 atx_it.sp = -1;
8683 /* Use ppos_it under bidi reordering to save a copy of IT for the
8684 initial position. We restore that position in IT when we have
8685 scanned the entire display line without finding a match for
8686 TO_CHARPOS and all the character positions are greater than
8687 TO_CHARPOS. We then restart the scan from the initial position,
8688 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8689 the closest to TO_CHARPOS. */
8690 if (it->bidi_p)
8692 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8694 SAVE_IT (ppos_it, *it, ppos_data);
8695 closest_pos = IT_CHARPOS (*it);
8697 else
8698 closest_pos = ZV;
8701 #define BUFFER_POS_REACHED_P() \
8702 ((op & MOVE_TO_POS) != 0 \
8703 && BUFFERP (it->object) \
8704 && (IT_CHARPOS (*it) == to_charpos \
8705 || ((!it->bidi_p \
8706 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8707 && IT_CHARPOS (*it) > to_charpos) \
8708 || (it->what == IT_COMPOSITION \
8709 && ((IT_CHARPOS (*it) > to_charpos \
8710 && to_charpos >= it->cmp_it.charpos) \
8711 || (IT_CHARPOS (*it) < to_charpos \
8712 && to_charpos <= it->cmp_it.charpos)))) \
8713 && (it->method == GET_FROM_BUFFER \
8714 || (it->method == GET_FROM_DISPLAY_VECTOR \
8715 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8717 if (it->hpos == 0)
8719 /* If line numbers are being displayed, produce a line number.
8720 But don't do that if we are to reach first_visible_x, because
8721 line numbers are not relevant to stuff that is not visible on
8722 display. */
8723 if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
8724 && should_produce_line_number (it))
8726 if (it->current_x == it->first_visible_x)
8727 maybe_produce_line_number (it);
8728 else
8729 line_number_pending = true;
8731 /* If there's a line-/wrap-prefix, handle it. */
8732 if (it->method == GET_FROM_BUFFER)
8733 handle_line_prefix (it);
8736 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8737 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8739 while (true)
8741 int x, i, ascent = 0, descent = 0;
8743 /* Utility macro to reset an iterator with x, ascent, and descent. */
8744 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8745 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8746 (IT)->max_descent = descent)
8748 /* Stop if we move beyond TO_CHARPOS (after an image or a
8749 display string or stretch glyph). */
8750 if ((op & MOVE_TO_POS) != 0
8751 && BUFFERP (it->object)
8752 && it->method == GET_FROM_BUFFER
8753 && (((!it->bidi_p
8754 /* When the iterator is at base embedding level, we
8755 are guaranteed that characters are delivered for
8756 display in strictly increasing order of their
8757 buffer positions. */
8758 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8759 && IT_CHARPOS (*it) > to_charpos)
8760 || (it->bidi_p
8761 && (prev_method == GET_FROM_IMAGE
8762 || prev_method == GET_FROM_STRETCH
8763 || prev_method == GET_FROM_STRING)
8764 /* Passed TO_CHARPOS from left to right. */
8765 && ((prev_pos < to_charpos
8766 && IT_CHARPOS (*it) > to_charpos)
8767 /* Passed TO_CHARPOS from right to left. */
8768 || (prev_pos > to_charpos
8769 && IT_CHARPOS (*it) < to_charpos)))))
8771 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8773 result = MOVE_POS_MATCH_OR_ZV;
8774 break;
8776 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8777 /* If wrap_it is valid, the current position might be in a
8778 word that is wrapped. So, save the iterator in
8779 atpos_it and continue to see if wrapping happens. */
8780 SAVE_IT (atpos_it, *it, atpos_data);
8783 /* Stop when ZV reached.
8784 We used to stop here when TO_CHARPOS reached as well, but that is
8785 too soon if this glyph does not fit on this line. So we handle it
8786 explicitly below. */
8787 if (!get_next_display_element (it))
8789 result = MOVE_POS_MATCH_OR_ZV;
8790 break;
8793 if (it->line_wrap == TRUNCATE)
8795 /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
8796 produced something that doesn't consume any screen estate
8797 in the text area, so we don't want to exit the loop at
8798 TO_CHARPOS, before we produce the glyph for that buffer
8799 position. This happens, e.g., when there's an overlay at
8800 TO_CHARPOS that draws a fringe bitmap. */
8801 if (BUFFER_POS_REACHED_P ()
8802 && (it->pixel_width > 0
8803 || IT_CHARPOS (*it) > to_charpos
8804 || it->area != TEXT_AREA))
8806 result = MOVE_POS_MATCH_OR_ZV;
8807 break;
8810 else
8812 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8814 if (IT_DISPLAYING_WHITESPACE (it))
8815 may_wrap = true;
8816 else if (may_wrap)
8818 /* We have reached a glyph that follows one or more
8819 whitespace characters. If the position is
8820 already found, we are done. */
8821 if (atpos_it.sp >= 0)
8823 RESTORE_IT (it, &atpos_it, atpos_data);
8824 result = MOVE_POS_MATCH_OR_ZV;
8825 goto done;
8827 if (atx_it.sp >= 0)
8829 RESTORE_IT (it, &atx_it, atx_data);
8830 result = MOVE_X_REACHED;
8831 goto done;
8833 /* Otherwise, we can wrap here. */
8834 SAVE_IT (wrap_it, *it, wrap_data);
8835 may_wrap = false;
8840 /* Remember the line height for the current line, in case
8841 the next element doesn't fit on the line. */
8842 ascent = it->max_ascent;
8843 descent = it->max_descent;
8845 /* The call to produce_glyphs will get the metrics of the
8846 display element IT is loaded with. Record the x-position
8847 before this display element, in case it doesn't fit on the
8848 line. */
8849 x = it->current_x;
8851 PRODUCE_GLYPHS (it);
8853 if (it->area != TEXT_AREA)
8855 prev_method = it->method;
8856 if (it->method == GET_FROM_BUFFER)
8857 prev_pos = IT_CHARPOS (*it);
8858 set_iterator_to_next (it, true);
8859 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8860 SET_TEXT_POS (this_line_min_pos,
8861 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8862 if (it->bidi_p
8863 && (op & MOVE_TO_POS)
8864 && IT_CHARPOS (*it) > to_charpos
8865 && IT_CHARPOS (*it) < closest_pos)
8866 closest_pos = IT_CHARPOS (*it);
8867 continue;
8870 /* The number of glyphs we get back in IT->nglyphs will normally
8871 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8872 character on a terminal frame, or (iii) a line end. For the
8873 second case, IT->nglyphs - 1 padding glyphs will be present.
8874 (On X frames, there is only one glyph produced for a
8875 composite character.)
8877 The behavior implemented below means, for continuation lines,
8878 that as many spaces of a TAB as fit on the current line are
8879 displayed there. For terminal frames, as many glyphs of a
8880 multi-glyph character are displayed in the current line, too.
8881 This is what the old redisplay code did, and we keep it that
8882 way. Under X, the whole shape of a complex character must
8883 fit on the line or it will be completely displayed in the
8884 next line.
8886 Note that both for tabs and padding glyphs, all glyphs have
8887 the same width. */
8888 if (it->nglyphs)
8890 /* More than one glyph or glyph doesn't fit on line. All
8891 glyphs have the same width. */
8892 int single_glyph_width = it->pixel_width / it->nglyphs;
8893 int new_x;
8894 int x_before_this_char = x;
8895 int hpos_before_this_char = it->hpos;
8897 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8899 new_x = x + single_glyph_width;
8901 /* We want to leave anything reaching TO_X to the caller. */
8902 if ((op & MOVE_TO_X) && new_x > to_x)
8904 if (BUFFER_POS_REACHED_P ())
8906 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8907 goto buffer_pos_reached;
8908 if (atpos_it.sp < 0)
8910 SAVE_IT (atpos_it, *it, atpos_data);
8911 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8914 else
8916 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8918 it->current_x = x;
8919 result = MOVE_X_REACHED;
8920 break;
8922 if (atx_it.sp < 0)
8924 SAVE_IT (atx_it, *it, atx_data);
8925 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8930 if (/* Lines are continued. */
8931 it->line_wrap != TRUNCATE
8932 && (/* And glyph doesn't fit on the line. */
8933 new_x > it->last_visible_x
8934 /* Or it fits exactly and we're on a window
8935 system frame. */
8936 || (new_x == it->last_visible_x
8937 && FRAME_WINDOW_P (it->f)
8938 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8939 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8940 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8942 bool moved_forward = false;
8944 if (/* IT->hpos == 0 means the very first glyph
8945 doesn't fit on the line, e.g. a wide image. */
8946 it->hpos == 0
8947 || (new_x == it->last_visible_x
8948 && FRAME_WINDOW_P (it->f)))
8950 ++it->hpos;
8951 it->current_x = new_x;
8953 /* The character's last glyph just barely fits
8954 in this row. */
8955 if (i == it->nglyphs - 1)
8957 /* If this is the destination position,
8958 return a position *before* it in this row,
8959 now that we know it fits in this row. */
8960 if (BUFFER_POS_REACHED_P ())
8962 bool can_wrap = true;
8964 /* If we are at a whitespace character
8965 that barely fits on this screen line,
8966 but the next character is also
8967 whitespace, we cannot wrap here. */
8968 if (it->line_wrap == WORD_WRAP
8969 && wrap_it.sp >= 0
8970 && may_wrap
8971 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8973 struct it tem_it;
8974 void *tem_data = NULL;
8976 SAVE_IT (tem_it, *it, tem_data);
8977 set_iterator_to_next (it, true);
8978 if (get_next_display_element (it)
8979 && IT_DISPLAYING_WHITESPACE (it))
8980 can_wrap = false;
8981 RESTORE_IT (it, &tem_it, tem_data);
8983 if (it->line_wrap != WORD_WRAP
8984 || wrap_it.sp < 0
8985 /* If we've just found whitespace
8986 where we can wrap, effectively
8987 ignore the previous wrap point --
8988 it is no longer relevant, but we
8989 won't have an opportunity to
8990 update it, since we've reached
8991 the edge of this screen line. */
8992 || (may_wrap && can_wrap
8993 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8995 it->hpos = hpos_before_this_char;
8996 it->current_x = x_before_this_char;
8997 result = MOVE_POS_MATCH_OR_ZV;
8998 break;
9000 if (it->line_wrap == WORD_WRAP
9001 && atpos_it.sp < 0)
9003 SAVE_IT (atpos_it, *it, atpos_data);
9004 atpos_it.current_x = x_before_this_char;
9005 atpos_it.hpos = hpos_before_this_char;
9009 prev_method = it->method;
9010 if (it->method == GET_FROM_BUFFER)
9011 prev_pos = IT_CHARPOS (*it);
9012 set_iterator_to_next (it, true);
9013 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9014 SET_TEXT_POS (this_line_min_pos,
9015 IT_CHARPOS (*it), IT_BYTEPOS (*it));
9016 /* On graphical terminals, newlines may
9017 "overflow" into the fringe if
9018 overflow-newline-into-fringe is non-nil.
9019 On text terminals, and on graphical
9020 terminals with no right margin, newlines
9021 may overflow into the last glyph on the
9022 display line.*/
9023 if (!FRAME_WINDOW_P (it->f)
9024 || ((it->bidi_p
9025 && it->bidi_it.paragraph_dir == R2L)
9026 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9027 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9028 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9030 if (!get_next_display_element (it))
9032 result = MOVE_POS_MATCH_OR_ZV;
9033 break;
9035 moved_forward = true;
9036 if (BUFFER_POS_REACHED_P ())
9038 if (ITERATOR_AT_END_OF_LINE_P (it))
9039 result = MOVE_POS_MATCH_OR_ZV;
9040 else
9041 result = MOVE_LINE_CONTINUED;
9042 break;
9044 if (ITERATOR_AT_END_OF_LINE_P (it)
9045 && (it->line_wrap != WORD_WRAP
9046 || wrap_it.sp < 0
9047 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9049 result = MOVE_NEWLINE_OR_CR;
9050 break;
9055 else
9056 IT_RESET_X_ASCENT_DESCENT (it);
9058 /* If the screen line ends with whitespace, and we
9059 are under word-wrap, don't use wrap_it: it is no
9060 longer relevant, but we won't have an opportunity
9061 to update it, since we are done with this screen
9062 line. */
9063 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9064 /* If the character after the one which set the
9065 may_wrap flag is also whitespace, we can't
9066 wrap here, since the screen line cannot be
9067 wrapped in the middle of whitespace.
9068 Therefore, wrap_it _is_ relevant in that
9069 case. */
9070 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9072 /* If we've found TO_X, go back there, as we now
9073 know the last word fits on this screen line. */
9074 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9075 && atx_it.sp >= 0)
9077 RESTORE_IT (it, &atx_it, atx_data);
9078 atpos_it.sp = -1;
9079 atx_it.sp = -1;
9080 result = MOVE_X_REACHED;
9081 break;
9084 else if (wrap_it.sp >= 0)
9086 RESTORE_IT (it, &wrap_it, wrap_data);
9087 atpos_it.sp = -1;
9088 atx_it.sp = -1;
9091 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9092 IT_CHARPOS (*it)));
9093 result = MOVE_LINE_CONTINUED;
9094 break;
9097 if (BUFFER_POS_REACHED_P ())
9099 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9100 goto buffer_pos_reached;
9101 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9103 SAVE_IT (atpos_it, *it, atpos_data);
9104 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9108 if (new_x > it->first_visible_x)
9110 /* If we have reached the visible portion of the
9111 screen line, produce the line number if needed. */
9112 if (line_number_pending)
9114 line_number_pending = false;
9115 it->current_x = it->first_visible_x;
9116 maybe_produce_line_number (it);
9117 it->current_x += new_x - it->first_visible_x;
9119 /* Glyph is visible. Increment number of glyphs that
9120 would be displayed. */
9121 ++it->hpos;
9125 if (result != MOVE_UNDEFINED)
9126 break;
9128 else if (BUFFER_POS_REACHED_P ())
9130 buffer_pos_reached:
9131 IT_RESET_X_ASCENT_DESCENT (it);
9132 result = MOVE_POS_MATCH_OR_ZV;
9133 break;
9135 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9137 /* Stop when TO_X specified and reached. This check is
9138 necessary here because of lines consisting of a line end,
9139 only. The line end will not produce any glyphs and we
9140 would never get MOVE_X_REACHED. */
9141 eassert (it->nglyphs == 0);
9142 result = MOVE_X_REACHED;
9143 break;
9146 /* Is this a line end? If yes, we're done. */
9147 if (ITERATOR_AT_END_OF_LINE_P (it))
9149 /* If we are past TO_CHARPOS, but never saw any character
9150 positions smaller than TO_CHARPOS, return
9151 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9152 did. */
9153 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9155 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9157 if (closest_pos < ZV)
9159 RESTORE_IT (it, &ppos_it, ppos_data);
9160 /* Don't recurse if closest_pos is equal to
9161 to_charpos, since we have just tried that. */
9162 if (closest_pos != to_charpos)
9163 move_it_in_display_line_to (it, closest_pos, -1,
9164 MOVE_TO_POS);
9165 result = MOVE_POS_MATCH_OR_ZV;
9167 else
9168 goto buffer_pos_reached;
9170 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9171 && IT_CHARPOS (*it) > to_charpos)
9172 goto buffer_pos_reached;
9173 else
9174 result = MOVE_NEWLINE_OR_CR;
9176 else
9177 result = MOVE_NEWLINE_OR_CR;
9178 /* If we've processed the newline, make sure this flag is
9179 reset, as it must only be set when the newline itself is
9180 processed. */
9181 if (result == MOVE_NEWLINE_OR_CR)
9182 it->constrain_row_ascent_descent_p = false;
9183 break;
9186 prev_method = it->method;
9187 if (it->method == GET_FROM_BUFFER)
9188 prev_pos = IT_CHARPOS (*it);
9189 /* The current display element has been consumed. Advance
9190 to the next. */
9191 set_iterator_to_next (it, true);
9192 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9193 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9194 if (IT_CHARPOS (*it) < to_charpos)
9195 saw_smaller_pos = true;
9196 if (it->bidi_p
9197 && (op & MOVE_TO_POS)
9198 && IT_CHARPOS (*it) >= to_charpos
9199 && IT_CHARPOS (*it) < closest_pos)
9200 closest_pos = IT_CHARPOS (*it);
9202 /* Stop if lines are truncated and IT's current x-position is
9203 past the right edge of the window now. */
9204 if (it->line_wrap == TRUNCATE
9205 && it->current_x >= it->last_visible_x)
9207 if (!FRAME_WINDOW_P (it->f)
9208 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9209 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9210 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9211 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9213 bool at_eob_p = false;
9215 if ((at_eob_p = !get_next_display_element (it))
9216 || BUFFER_POS_REACHED_P ()
9217 /* If we are past TO_CHARPOS, but never saw any
9218 character positions smaller than TO_CHARPOS,
9219 return MOVE_POS_MATCH_OR_ZV, like the
9220 unidirectional display did. */
9221 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9222 && !saw_smaller_pos
9223 && IT_CHARPOS (*it) > to_charpos))
9225 if (it->bidi_p
9226 && !BUFFER_POS_REACHED_P ()
9227 && !at_eob_p && closest_pos < ZV)
9229 RESTORE_IT (it, &ppos_it, ppos_data);
9230 if (closest_pos != to_charpos)
9231 move_it_in_display_line_to (it, closest_pos, -1,
9232 MOVE_TO_POS);
9234 result = MOVE_POS_MATCH_OR_ZV;
9235 break;
9237 if (ITERATOR_AT_END_OF_LINE_P (it))
9239 result = MOVE_NEWLINE_OR_CR;
9240 break;
9243 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9244 && !saw_smaller_pos
9245 && IT_CHARPOS (*it) > to_charpos)
9247 if (closest_pos < ZV)
9249 RESTORE_IT (it, &ppos_it, ppos_data);
9250 if (closest_pos != to_charpos)
9251 move_it_in_display_line_to (it, closest_pos, -1,
9252 MOVE_TO_POS);
9254 result = MOVE_POS_MATCH_OR_ZV;
9255 break;
9257 result = MOVE_LINE_TRUNCATED;
9258 break;
9260 #undef IT_RESET_X_ASCENT_DESCENT
9263 #undef BUFFER_POS_REACHED_P
9265 /* If we scanned beyond TO_POS, restore the saved iterator either to
9266 the wrap point (if found), or to atpos/atx location. We decide which
9267 data to use to restore the saved iterator state by their X coordinates,
9268 since buffer positions might increase non-monotonically with screen
9269 coordinates due to bidi reordering. */
9270 if (result == MOVE_LINE_CONTINUED
9271 && it->line_wrap == WORD_WRAP
9272 && wrap_it.sp >= 0
9273 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9274 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9275 RESTORE_IT (it, &wrap_it, wrap_data);
9276 else if (atpos_it.sp >= 0)
9277 RESTORE_IT (it, &atpos_it, atpos_data);
9278 else if (atx_it.sp >= 0)
9279 RESTORE_IT (it, &atx_it, atx_data);
9281 done:
9283 if (atpos_data)
9284 bidi_unshelve_cache (atpos_data, true);
9285 if (atx_data)
9286 bidi_unshelve_cache (atx_data, true);
9287 if (wrap_data)
9288 bidi_unshelve_cache (wrap_data, true);
9289 if (ppos_data)
9290 bidi_unshelve_cache (ppos_data, true);
9292 /* Restore the iterator settings altered at the beginning of this
9293 function. */
9294 it->glyph_row = saved_glyph_row;
9295 return result;
9298 /* For external use. */
9299 void
9300 move_it_in_display_line (struct it *it,
9301 ptrdiff_t to_charpos, int to_x,
9302 enum move_operation_enum op)
9304 if (it->line_wrap == WORD_WRAP
9305 && (op & MOVE_TO_X))
9307 struct it save_it;
9308 void *save_data = NULL;
9309 int skip;
9311 SAVE_IT (save_it, *it, save_data);
9312 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9313 /* When word-wrap is on, TO_X may lie past the end
9314 of a wrapped line. Then it->current is the
9315 character on the next line, so backtrack to the
9316 space before the wrap point. */
9317 if (skip == MOVE_LINE_CONTINUED)
9319 int prev_x = max (it->current_x - 1, 0);
9320 RESTORE_IT (it, &save_it, save_data);
9321 move_it_in_display_line_to
9322 (it, -1, prev_x, MOVE_TO_X);
9324 else
9325 bidi_unshelve_cache (save_data, true);
9327 else
9328 move_it_in_display_line_to (it, to_charpos, to_x, op);
9332 /* Move IT forward until it satisfies one or more of the criteria in
9333 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9335 OP is a bit-mask that specifies where to stop, and in particular,
9336 which of those four position arguments makes a difference. See the
9337 description of enum move_operation_enum.
9339 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9340 screen line, this function will set IT to the next position that is
9341 displayed to the right of TO_CHARPOS on the screen.
9343 Return the maximum pixel length of any line scanned but never more
9344 than it.last_visible_x. */
9347 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9349 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9350 int line_height, line_start_x = 0, reached = 0;
9351 int max_current_x = 0;
9352 void *backup_data = NULL;
9354 for (;;)
9356 if (op & MOVE_TO_VPOS)
9358 /* If no TO_CHARPOS and no TO_X specified, stop at the
9359 start of the line TO_VPOS. */
9360 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9362 if (it->vpos == to_vpos)
9364 reached = 1;
9365 break;
9367 else
9368 skip = move_it_in_display_line_to (it, -1, -1, 0);
9370 else
9372 /* TO_VPOS >= 0 means stop at TO_X in the line at
9373 TO_VPOS, or at TO_POS, whichever comes first. */
9374 if (it->vpos == to_vpos)
9376 reached = 2;
9377 break;
9380 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9382 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9384 reached = 3;
9385 break;
9387 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9389 /* We have reached TO_X but not in the line we want. */
9390 skip = move_it_in_display_line_to (it, to_charpos,
9391 -1, MOVE_TO_POS);
9392 if (skip == MOVE_POS_MATCH_OR_ZV)
9394 reached = 4;
9395 break;
9400 else if (op & MOVE_TO_Y)
9402 struct it it_backup;
9404 if (it->line_wrap == WORD_WRAP)
9405 SAVE_IT (it_backup, *it, backup_data);
9407 /* TO_Y specified means stop at TO_X in the line containing
9408 TO_Y---or at TO_CHARPOS if this is reached first. The
9409 problem is that we can't really tell whether the line
9410 contains TO_Y before we have completely scanned it, and
9411 this may skip past TO_X. What we do is to first scan to
9412 TO_X.
9414 If TO_X is not specified, use a TO_X of zero. The reason
9415 is to make the outcome of this function more predictable.
9416 If we didn't use TO_X == 0, we would stop at the end of
9417 the line which is probably not what a caller would expect
9418 to happen. */
9419 skip = move_it_in_display_line_to
9420 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9421 (MOVE_TO_X | (op & MOVE_TO_POS)));
9423 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9424 if (skip == MOVE_POS_MATCH_OR_ZV)
9425 reached = 5;
9426 else if (skip == MOVE_X_REACHED)
9428 /* If TO_X was reached, we want to know whether TO_Y is
9429 in the line. We know this is the case if the already
9430 scanned glyphs make the line tall enough. Otherwise,
9431 we must check by scanning the rest of the line. */
9432 line_height = it->max_ascent + it->max_descent;
9433 if (to_y >= it->current_y
9434 && to_y < it->current_y + line_height)
9436 reached = 6;
9437 break;
9439 SAVE_IT (it_backup, *it, backup_data);
9440 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9441 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9442 op & MOVE_TO_POS);
9443 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9444 line_height = it->max_ascent + it->max_descent;
9445 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9447 if (to_y >= it->current_y
9448 && to_y < it->current_y + line_height)
9450 /* If TO_Y is in this line and TO_X was reached
9451 above, we scanned too far. We have to restore
9452 IT's settings to the ones before skipping. But
9453 keep the more accurate values of max_ascent and
9454 max_descent we've found while skipping the rest
9455 of the line, for the sake of callers, such as
9456 pos_visible_p, that need to know the line
9457 height. */
9458 int max_ascent = it->max_ascent;
9459 int max_descent = it->max_descent;
9461 RESTORE_IT (it, &it_backup, backup_data);
9462 it->max_ascent = max_ascent;
9463 it->max_descent = max_descent;
9464 reached = 6;
9466 else
9468 skip = skip2;
9469 if (skip == MOVE_POS_MATCH_OR_ZV)
9470 reached = 7;
9473 else
9475 /* Check whether TO_Y is in this line. */
9476 line_height = it->max_ascent + it->max_descent;
9477 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9479 if (to_y >= it->current_y
9480 && to_y < it->current_y + line_height)
9482 if (to_y > it->current_y)
9483 max_current_x = max (it->current_x, max_current_x);
9485 /* When word-wrap is on, TO_X may lie past the end
9486 of a wrapped line. Then it->current is the
9487 character on the next line, so backtrack to the
9488 space before the wrap point. */
9489 if (skip == MOVE_LINE_CONTINUED
9490 && it->line_wrap == WORD_WRAP)
9492 int prev_x = max (it->current_x - 1, 0);
9493 RESTORE_IT (it, &it_backup, backup_data);
9494 skip = move_it_in_display_line_to
9495 (it, -1, prev_x, MOVE_TO_X);
9498 reached = 6;
9502 if (reached)
9504 max_current_x = max (it->current_x, max_current_x);
9505 break;
9508 else if (BUFFERP (it->object)
9509 && (it->method == GET_FROM_BUFFER
9510 || it->method == GET_FROM_STRETCH)
9511 && IT_CHARPOS (*it) >= to_charpos
9512 /* Under bidi iteration, a call to set_iterator_to_next
9513 can scan far beyond to_charpos if the initial
9514 portion of the next line needs to be reordered. In
9515 that case, give move_it_in_display_line_to another
9516 chance below. */
9517 && !(it->bidi_p
9518 && it->bidi_it.scan_dir == -1))
9519 skip = MOVE_POS_MATCH_OR_ZV;
9520 else
9521 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9523 switch (skip)
9525 case MOVE_POS_MATCH_OR_ZV:
9526 max_current_x = max (it->current_x, max_current_x);
9527 reached = 8;
9528 goto out;
9530 case MOVE_NEWLINE_OR_CR:
9531 max_current_x = max (it->current_x, max_current_x);
9532 set_iterator_to_next (it, true);
9533 it->continuation_lines_width = 0;
9534 break;
9536 case MOVE_LINE_TRUNCATED:
9537 max_current_x = it->last_visible_x;
9538 it->continuation_lines_width = 0;
9539 reseat_at_next_visible_line_start (it, false);
9540 if ((op & MOVE_TO_POS) != 0
9541 && IT_CHARPOS (*it) > to_charpos)
9543 reached = 9;
9544 goto out;
9546 break;
9548 case MOVE_LINE_CONTINUED:
9549 max_current_x = it->last_visible_x;
9550 /* For continued lines ending in a tab, some of the glyphs
9551 associated with the tab are displayed on the current
9552 line. Since it->current_x does not include these glyphs,
9553 we use it->last_visible_x instead. */
9554 if (it->c == '\t')
9556 it->continuation_lines_width += it->last_visible_x;
9557 /* When moving by vpos, ensure that the iterator really
9558 advances to the next line (bug#847, bug#969). Fixme:
9559 do we need to do this in other circumstances? */
9560 if (it->current_x != it->last_visible_x
9561 && (op & MOVE_TO_VPOS)
9562 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9564 line_start_x = it->current_x + it->pixel_width
9565 - it->last_visible_x;
9566 if (FRAME_WINDOW_P (it->f))
9568 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9569 struct font *face_font = face->font;
9571 /* When display_line produces a continued line
9572 that ends in a TAB, it skips a tab stop that
9573 is closer than the font's space character
9574 width (see x_produce_glyphs where it produces
9575 the stretch glyph which represents a TAB).
9576 We need to reproduce the same logic here. */
9577 eassert (face_font);
9578 if (face_font)
9580 if (line_start_x < face_font->space_width)
9581 line_start_x
9582 += it->tab_width * face_font->space_width;
9585 set_iterator_to_next (it, false);
9588 else
9589 it->continuation_lines_width += it->current_x;
9590 break;
9592 default:
9593 emacs_abort ();
9596 /* Reset/increment for the next run. */
9597 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9598 it->current_x = line_start_x;
9599 line_start_x = 0;
9600 it->hpos = 0;
9601 it->current_y += it->max_ascent + it->max_descent;
9602 ++it->vpos;
9603 last_height = it->max_ascent + it->max_descent;
9604 it->max_ascent = it->max_descent = 0;
9607 out:
9609 /* On text terminals, we may stop at the end of a line in the middle
9610 of a multi-character glyph. If the glyph itself is continued,
9611 i.e. it is actually displayed on the next line, don't treat this
9612 stopping point as valid; move to the next line instead (unless
9613 that brings us offscreen). */
9614 if (!FRAME_WINDOW_P (it->f)
9615 && op & MOVE_TO_POS
9616 && IT_CHARPOS (*it) == to_charpos
9617 && it->what == IT_CHARACTER
9618 && it->nglyphs > 1
9619 && it->line_wrap == WINDOW_WRAP
9620 && it->current_x == it->last_visible_x - 1
9621 && it->c != '\n'
9622 && it->c != '\t'
9623 && it->w->window_end_valid
9624 && it->vpos < it->w->window_end_vpos)
9626 it->continuation_lines_width += it->current_x;
9627 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9628 it->current_y += it->max_ascent + it->max_descent;
9629 ++it->vpos;
9630 last_height = it->max_ascent + it->max_descent;
9633 if (backup_data)
9634 bidi_unshelve_cache (backup_data, true);
9636 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9638 return max_current_x;
9642 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9644 If DY > 0, move IT backward at least that many pixels. DY = 0
9645 means move IT backward to the preceding line start or BEGV. This
9646 function may move over more than DY pixels if IT->current_y - DY
9647 ends up in the middle of a line; in this case IT->current_y will be
9648 set to the top of the line moved to. */
9650 void
9651 move_it_vertically_backward (struct it *it, int dy)
9653 int nlines, h;
9654 struct it it2, it3;
9655 void *it2data = NULL, *it3data = NULL;
9656 ptrdiff_t start_pos;
9657 int nchars_per_row
9658 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9659 ptrdiff_t pos_limit;
9661 move_further_back:
9662 eassert (dy >= 0);
9664 start_pos = IT_CHARPOS (*it);
9666 /* Estimate how many newlines we must move back. */
9667 nlines = max (1, dy / default_line_pixel_height (it->w));
9668 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9669 pos_limit = BEGV;
9670 else
9671 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9673 /* Set the iterator's position that many lines back. But don't go
9674 back more than NLINES full screen lines -- this wins a day with
9675 buffers which have very long lines. */
9676 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9677 back_to_previous_visible_line_start (it);
9679 /* Reseat the iterator here. When moving backward, we don't want
9680 reseat to skip forward over invisible text, set up the iterator
9681 to deliver from overlay strings at the new position etc. So,
9682 use reseat_1 here. */
9683 reseat_1 (it, it->current.pos, true);
9685 /* We are now surely at a line start. */
9686 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9687 reordering is in effect. */
9688 it->continuation_lines_width = 0;
9690 /* Move forward and see what y-distance we moved. First move to the
9691 start of the next line so that we get its height. We need this
9692 height to be able to tell whether we reached the specified
9693 y-distance. */
9694 SAVE_IT (it2, *it, it2data);
9695 it2.max_ascent = it2.max_descent = 0;
9698 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9699 MOVE_TO_POS | MOVE_TO_VPOS);
9701 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9702 /* If we are in a display string which starts at START_POS,
9703 and that display string includes a newline, and we are
9704 right after that newline (i.e. at the beginning of a
9705 display line), exit the loop, because otherwise we will
9706 infloop, since move_it_to will see that it is already at
9707 START_POS and will not move. */
9708 || (it2.method == GET_FROM_STRING
9709 && IT_CHARPOS (it2) == start_pos
9710 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9711 eassert (IT_CHARPOS (*it) >= BEGV);
9712 SAVE_IT (it3, it2, it3data);
9714 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9715 eassert (IT_CHARPOS (*it) >= BEGV);
9716 /* H is the actual vertical distance from the position in *IT
9717 and the starting position. */
9718 h = it2.current_y - it->current_y;
9719 /* NLINES is the distance in number of lines. */
9720 nlines = it2.vpos - it->vpos;
9722 /* Correct IT's y and vpos position
9723 so that they are relative to the starting point. */
9724 it->vpos -= nlines;
9725 it->current_y -= h;
9727 if (dy == 0)
9729 /* DY == 0 means move to the start of the screen line. The
9730 value of nlines is > 0 if continuation lines were involved,
9731 or if the original IT position was at start of a line. */
9732 RESTORE_IT (it, it, it2data);
9733 if (nlines > 0)
9734 move_it_by_lines (it, nlines);
9735 /* The above code moves us to some position NLINES down,
9736 usually to its first glyph (leftmost in an L2R line), but
9737 that's not necessarily the start of the line, under bidi
9738 reordering. We want to get to the character position
9739 that is immediately after the newline of the previous
9740 line. */
9741 if (it->bidi_p
9742 && !it->continuation_lines_width
9743 && !STRINGP (it->string)
9744 && IT_CHARPOS (*it) > BEGV
9745 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9747 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9749 DEC_BOTH (cp, bp);
9750 cp = find_newline_no_quit (cp, bp, -1, NULL);
9751 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9753 bidi_unshelve_cache (it3data, true);
9755 else
9757 /* The y-position we try to reach, relative to *IT.
9758 Note that H has been subtracted in front of the if-statement. */
9759 int target_y = it->current_y + h - dy;
9760 int y0 = it3.current_y;
9761 int y1;
9762 int line_height;
9764 RESTORE_IT (&it3, &it3, it3data);
9765 y1 = line_bottom_y (&it3);
9766 line_height = y1 - y0;
9767 RESTORE_IT (it, it, it2data);
9768 /* If we did not reach target_y, try to move further backward if
9769 we can. If we moved too far backward, try to move forward. */
9770 if (target_y < it->current_y
9771 /* This is heuristic. In a window that's 3 lines high, with
9772 a line height of 13 pixels each, recentering with point
9773 on the bottom line will try to move -39/2 = 19 pixels
9774 backward. Try to avoid moving into the first line. */
9775 && (it->current_y - target_y
9776 > min (window_box_height (it->w), line_height * 2 / 3))
9777 && IT_CHARPOS (*it) > BEGV)
9779 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9780 target_y - it->current_y));
9781 dy = it->current_y - target_y;
9782 goto move_further_back;
9784 else if (target_y >= it->current_y + line_height
9785 && IT_CHARPOS (*it) < ZV)
9787 /* Should move forward by at least one line, maybe more.
9789 Note: Calling move_it_by_lines can be expensive on
9790 terminal frames, where compute_motion is used (via
9791 vmotion) to do the job, when there are very long lines
9792 and truncate-lines is nil. That's the reason for
9793 treating terminal frames specially here. */
9795 if (!FRAME_WINDOW_P (it->f))
9796 move_it_vertically (it, target_y - it->current_y);
9797 else
9801 move_it_by_lines (it, 1);
9803 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9810 /* Move IT by a specified amount of pixel lines DY. DY negative means
9811 move backwards. DY = 0 means move to start of screen line. At the
9812 end, IT will be on the start of a screen line. */
9814 void
9815 move_it_vertically (struct it *it, int dy)
9817 if (dy <= 0)
9818 move_it_vertically_backward (it, -dy);
9819 else
9821 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9822 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9823 MOVE_TO_POS | MOVE_TO_Y);
9824 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9826 /* If buffer ends in ZV without a newline, move to the start of
9827 the line to satisfy the post-condition. */
9828 if (IT_CHARPOS (*it) == ZV
9829 && ZV > BEGV
9830 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9831 move_it_by_lines (it, 0);
9836 /* Move iterator IT past the end of the text line it is in. */
9838 void
9839 move_it_past_eol (struct it *it)
9841 enum move_it_result rc;
9843 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9844 if (rc == MOVE_NEWLINE_OR_CR)
9845 set_iterator_to_next (it, false);
9849 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9850 negative means move up. DVPOS == 0 means move to the start of the
9851 screen line.
9853 Optimization idea: If we would know that IT->f doesn't use
9854 a face with proportional font, we could be faster for
9855 truncate-lines nil. */
9857 void
9858 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9861 /* The commented-out optimization uses vmotion on terminals. This
9862 gives bad results, because elements like it->what, on which
9863 callers such as pos_visible_p rely, aren't updated. */
9864 /* struct position pos;
9865 if (!FRAME_WINDOW_P (it->f))
9867 struct text_pos textpos;
9869 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9870 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9871 reseat (it, textpos, true);
9872 it->vpos += pos.vpos;
9873 it->current_y += pos.vpos;
9875 else */
9877 if (dvpos == 0)
9879 /* DVPOS == 0 means move to the start of the screen line. */
9880 move_it_vertically_backward (it, 0);
9881 /* Let next call to line_bottom_y calculate real line height. */
9882 last_height = 0;
9884 else if (dvpos > 0)
9886 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9887 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9889 /* Only move to the next buffer position if we ended up in a
9890 string from display property, not in an overlay string
9891 (before-string or after-string). That is because the
9892 latter don't conceal the underlying buffer position, so
9893 we can ask to move the iterator to the exact position we
9894 are interested in. Note that, even if we are already at
9895 IT_CHARPOS (*it), the call below is not a no-op, as it
9896 will detect that we are at the end of the string, pop the
9897 iterator, and compute it->current_x and it->hpos
9898 correctly. */
9899 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9900 -1, -1, -1, MOVE_TO_POS);
9903 else
9905 struct it it2;
9906 void *it2data = NULL;
9907 ptrdiff_t start_charpos, i;
9908 int nchars_per_row
9909 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9910 bool hit_pos_limit = false;
9911 ptrdiff_t pos_limit;
9913 /* Start at the beginning of the screen line containing IT's
9914 position. This may actually move vertically backwards,
9915 in case of overlays, so adjust dvpos accordingly. */
9916 dvpos += it->vpos;
9917 move_it_vertically_backward (it, 0);
9918 dvpos -= it->vpos;
9920 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9921 screen lines, and reseat the iterator there. */
9922 start_charpos = IT_CHARPOS (*it);
9923 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9924 pos_limit = BEGV;
9925 else
9926 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9928 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9929 back_to_previous_visible_line_start (it);
9930 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9931 hit_pos_limit = true;
9932 reseat (it, it->current.pos, true);
9934 /* Move further back if we end up in a string or an image. */
9935 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9937 /* First try to move to start of display line. */
9938 dvpos += it->vpos;
9939 move_it_vertically_backward (it, 0);
9940 dvpos -= it->vpos;
9941 if (IT_POS_VALID_AFTER_MOVE_P (it))
9942 break;
9943 /* If start of line is still in string or image,
9944 move further back. */
9945 back_to_previous_visible_line_start (it);
9946 reseat (it, it->current.pos, true);
9947 dvpos--;
9950 it->current_x = it->hpos = 0;
9952 /* Above call may have moved too far if continuation lines
9953 are involved. Scan forward and see if it did. */
9954 SAVE_IT (it2, *it, it2data);
9955 it2.vpos = it2.current_y = 0;
9956 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9957 it->vpos -= it2.vpos;
9958 it->current_y -= it2.current_y;
9959 it->current_x = it->hpos = 0;
9961 /* If we moved too far back, move IT some lines forward. */
9962 if (it2.vpos > -dvpos)
9964 int delta = it2.vpos + dvpos;
9966 RESTORE_IT (&it2, &it2, it2data);
9967 SAVE_IT (it2, *it, it2data);
9968 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9969 /* Move back again if we got too far ahead. */
9970 if (IT_CHARPOS (*it) >= start_charpos)
9971 RESTORE_IT (it, &it2, it2data);
9972 else
9973 bidi_unshelve_cache (it2data, true);
9975 else if (hit_pos_limit && pos_limit > BEGV
9976 && dvpos < 0 && it2.vpos < -dvpos)
9978 /* If we hit the limit, but still didn't make it far enough
9979 back, that means there's a display string with a newline
9980 covering a large chunk of text, and that caused
9981 back_to_previous_visible_line_start try to go too far.
9982 Punish those who commit such atrocities by going back
9983 until we've reached DVPOS, after lifting the limit, which
9984 could make it slow for very long lines. "If it hurts,
9985 don't do that!" */
9986 dvpos += it2.vpos;
9987 RESTORE_IT (it, it, it2data);
9988 for (i = -dvpos; i > 0; --i)
9990 back_to_previous_visible_line_start (it);
9991 it->vpos--;
9993 reseat_1 (it, it->current.pos, true);
9995 else
9996 RESTORE_IT (it, it, it2data);
10001 partial_line_height (struct it *it_origin)
10003 int partial_height;
10004 void *it_data = NULL;
10005 struct it it;
10006 SAVE_IT (it, *it_origin, it_data);
10007 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
10008 MOVE_TO_POS | MOVE_TO_Y);
10009 if (it.what == IT_EOB)
10011 int vis_height = it.last_visible_y - it.current_y;
10012 int height = it.ascent + it.descent;
10013 partial_height = (vis_height < height) ? vis_height : 0;
10015 else
10017 int last_line_y = it.current_y;
10018 move_it_by_lines (&it, 1);
10019 partial_height = (it.current_y > it.last_visible_y)
10020 ? it.last_visible_y - last_line_y : 0;
10022 RESTORE_IT (&it, &it, it_data);
10023 return partial_height;
10026 /* Return true if IT points into the middle of a display vector. */
10028 bool
10029 in_display_vector_p (struct it *it)
10031 return (it->method == GET_FROM_DISPLAY_VECTOR
10032 && it->current.dpvec_index > 0
10033 && it->dpvec + it->current.dpvec_index != it->dpend);
10036 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
10037 doc: /* Return the size of the text of WINDOW's buffer in pixels.
10038 WINDOW must be a live window and defaults to the selected one. The
10039 return value is a cons of the maximum pixel-width of any text line and
10040 the maximum pixel-height of all text lines.
10042 The optional argument FROM, if non-nil, specifies the first text
10043 position and defaults to the minimum accessible position of the buffer.
10044 If FROM is t, use the minimum accessible position that starts a
10045 non-empty line. TO, if non-nil, specifies the last text position and
10046 defaults to the maximum accessible position of the buffer. If TO is t,
10047 use the maximum accessible position that ends a non-empty line.
10049 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10050 width that can be returned. X-LIMIT nil or omitted, means to use the
10051 pixel-width of WINDOW's body; use this if you want to know how high
10052 WINDOW should be become in order to fit all of its buffer's text with
10053 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10054 if you intend to change WINDOW's width. In any case, text whose
10055 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10056 of long lines can take some time, it's always a good idea to make this
10057 argument as small as possible; in particular, if the buffer contains
10058 long lines that shall be truncated anyway.
10060 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10061 height (excluding the height of the mode- or header-line, if any) that
10062 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10063 ignored. Since calculating the text height of a large buffer can take
10064 some time, it makes sense to specify this argument if the size of the
10065 buffer is large or unknown.
10067 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10068 include the height of the mode- or header-line of WINDOW in the return
10069 value. If it is either the symbol `mode-line' or `header-line', include
10070 only the height of that line, if present, in the return value. If t,
10071 include the height of both, if present, in the return value. */)
10072 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10073 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10075 struct window *w = decode_live_window (window);
10076 Lisp_Object buffer = w->contents;
10077 struct buffer *b;
10078 struct it it;
10079 struct buffer *old_b = NULL;
10080 ptrdiff_t start, end, pos;
10081 struct text_pos startp;
10082 void *itdata = NULL;
10083 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10085 CHECK_BUFFER (buffer);
10086 b = XBUFFER (buffer);
10088 if (b != current_buffer)
10090 old_b = current_buffer;
10091 set_buffer_internal (b);
10094 if (NILP (from))
10095 start = BEGV;
10096 else if (EQ (from, Qt))
10098 start = pos = BEGV;
10099 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10100 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10101 start = pos;
10102 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10103 start = pos;
10105 else
10107 CHECK_NUMBER_COERCE_MARKER (from);
10108 start = min (max (XINT (from), BEGV), ZV);
10111 if (NILP (to))
10112 end = ZV;
10113 else if (EQ (to, Qt))
10115 end = pos = ZV;
10116 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10117 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10118 end = pos;
10119 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10120 end = pos;
10122 else
10124 CHECK_NUMBER_COERCE_MARKER (to);
10125 end = max (start, min (XINT (to), ZV));
10128 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10129 max_x = XINT (x_limit);
10131 if (NILP (y_limit))
10132 max_y = INT_MAX;
10133 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10134 max_y = XINT (y_limit);
10136 itdata = bidi_shelve_cache ();
10137 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10138 start_display (&it, w, startp);
10139 /* It makes no sense to measure dimensions of region of text that
10140 crosses the point where bidi reordering changes scan direction.
10141 By using unidirectional movement here we at least support the use
10142 case of measuring regions of text that have a uniformly R2L
10143 directionality, and regions that begin and end in text of the
10144 same directionality. */
10145 it.bidi_p = false;
10146 void *it2data = NULL;
10147 struct it it2;
10148 SAVE_IT (it2, it, it2data);
10150 int move_op = MOVE_TO_POS | MOVE_TO_Y;
10151 int to_x = -1;
10152 if (!NILP (x_limit))
10154 /* Actually, we never want move_it_to stop at to_x. But to make
10155 sure that move_it_in_display_line_to always moves far enough,
10156 we set to_x to INT_MAX and specify MOVE_TO_X. */
10157 move_op |= MOVE_TO_X;
10158 to_x = INT_MAX;
10161 x = move_it_to (&it, end, to_x, max_y, -1, move_op);
10163 /* We could have a display property at END, in which case asking
10164 move_it_to to stop at END will overshoot and stop at position
10165 after END. So we try again, stopping before END, and account for
10166 the width of the last buffer position manually. */
10167 if (IT_CHARPOS (it) > end)
10169 end--;
10170 RESTORE_IT (&it, &it2, it2data);
10171 x = move_it_to (&it, end, to_x, max_y, -1, move_op);
10172 /* Add the width of the thing at TO, but only if we didn't
10173 overshoot it; if we did, it is already accounted for. */
10174 if (IT_CHARPOS (it) == end)
10175 x += it.pixel_width;
10177 if (!NILP (x_limit))
10179 /* Don't return more than X-LIMIT. */
10180 if (x > max_x)
10181 x = max_x;
10184 /* Subtract height of header-line which was counted automatically by
10185 start_display. */
10186 y = it.current_y + it.max_ascent + it.max_descent
10187 - WINDOW_HEADER_LINE_HEIGHT (w);
10188 /* Don't return more than Y-LIMIT. */
10189 if (y > max_y)
10190 y = max_y;
10192 if (EQ (mode_and_header_line, Qheader_line)
10193 || EQ (mode_and_header_line, Qt))
10194 /* Re-add height of header-line as requested. */
10195 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10197 if (EQ (mode_and_header_line, Qmode_line)
10198 || EQ (mode_and_header_line, Qt))
10199 /* Add height of mode-line as requested. */
10200 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10202 bidi_unshelve_cache (itdata, false);
10204 if (old_b)
10205 set_buffer_internal (old_b);
10207 return Fcons (make_number (x), make_number (y));
10210 /***********************************************************************
10211 Messages
10212 ***********************************************************************/
10214 /* Return the number of arguments the format string FORMAT needs. */
10216 static ptrdiff_t
10217 format_nargs (char const *format)
10219 ptrdiff_t nargs = 0;
10220 for (char const *p = format; (p = strchr (p, '%')); p++)
10221 if (p[1] == '%')
10222 p++;
10223 else
10224 nargs++;
10225 return nargs;
10228 /* Add a message with format string FORMAT and formatted arguments
10229 to *Messages*. */
10231 void
10232 add_to_log (const char *format, ...)
10234 va_list ap;
10235 va_start (ap, format);
10236 vadd_to_log (format, ap);
10237 va_end (ap);
10240 void
10241 vadd_to_log (char const *format, va_list ap)
10243 ptrdiff_t form_nargs = format_nargs (format);
10244 ptrdiff_t nargs = 1 + form_nargs;
10245 Lisp_Object args[10];
10246 eassert (nargs <= ARRAYELTS (args));
10247 AUTO_STRING (args0, format);
10248 args[0] = args0;
10249 for (ptrdiff_t i = 1; i <= nargs; i++)
10250 args[i] = va_arg (ap, Lisp_Object);
10251 Lisp_Object msg = Qnil;
10252 msg = Fformat_message (nargs, args);
10254 ptrdiff_t len = SBYTES (msg) + 1;
10255 USE_SAFE_ALLOCA;
10256 char *buffer = SAFE_ALLOCA (len);
10257 memcpy (buffer, SDATA (msg), len);
10259 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10260 SAFE_FREE ();
10264 /* Output a newline in the *Messages* buffer if "needs" one. */
10266 void
10267 message_log_maybe_newline (void)
10269 if (message_log_need_newline)
10270 message_dolog ("", 0, true, false);
10274 /* Add a string M of length NBYTES to the message log, optionally
10275 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10276 true, means interpret the contents of M as multibyte. This
10277 function calls low-level routines in order to bypass text property
10278 hooks, etc. which might not be safe to run.
10280 This may GC (insert may run before/after change hooks),
10281 so the buffer M must NOT point to a Lisp string. */
10283 void
10284 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10286 const unsigned char *msg = (const unsigned char *) m;
10288 if (!NILP (Vmemory_full))
10289 return;
10291 if (!NILP (Vmessage_log_max))
10293 struct buffer *oldbuf;
10294 Lisp_Object oldpoint, oldbegv, oldzv;
10295 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10296 ptrdiff_t point_at_end = 0;
10297 ptrdiff_t zv_at_end = 0;
10298 Lisp_Object old_deactivate_mark;
10300 old_deactivate_mark = Vdeactivate_mark;
10301 oldbuf = current_buffer;
10303 /* Ensure the Messages buffer exists, and switch to it.
10304 If we created it, set the major-mode. */
10305 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10306 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10307 if (newbuffer
10308 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10309 call0 (intern ("messages-buffer-mode"));
10311 bset_undo_list (current_buffer, Qt);
10312 bset_cache_long_scans (current_buffer, Qnil);
10314 oldpoint = message_dolog_marker1;
10315 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10316 oldbegv = message_dolog_marker2;
10317 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10318 oldzv = message_dolog_marker3;
10319 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10321 if (PT == Z)
10322 point_at_end = 1;
10323 if (ZV == Z)
10324 zv_at_end = 1;
10326 BEGV = BEG;
10327 BEGV_BYTE = BEG_BYTE;
10328 ZV = Z;
10329 ZV_BYTE = Z_BYTE;
10330 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10332 /* Insert the string--maybe converting multibyte to single byte
10333 or vice versa, so that all the text fits the buffer. */
10334 if (multibyte
10335 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10337 ptrdiff_t i;
10338 int c, char_bytes;
10339 char work[1];
10341 /* Convert a multibyte string to single-byte
10342 for the *Message* buffer. */
10343 for (i = 0; i < nbytes; i += char_bytes)
10345 c = string_char_and_length (msg + i, &char_bytes);
10346 work[0] = CHAR_TO_BYTE8 (c);
10347 insert_1_both (work, 1, 1, true, false, false);
10350 else if (! multibyte
10351 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10353 ptrdiff_t i;
10354 int c, char_bytes;
10355 unsigned char str[MAX_MULTIBYTE_LENGTH];
10356 /* Convert a single-byte string to multibyte
10357 for the *Message* buffer. */
10358 for (i = 0; i < nbytes; i++)
10360 c = msg[i];
10361 MAKE_CHAR_MULTIBYTE (c);
10362 char_bytes = CHAR_STRING (c, str);
10363 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10366 else if (nbytes)
10367 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10368 true, false, false);
10370 if (nlflag)
10372 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10373 printmax_t dups;
10375 insert_1_both ("\n", 1, 1, true, false, false);
10377 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10378 this_bol = PT;
10379 this_bol_byte = PT_BYTE;
10381 /* See if this line duplicates the previous one.
10382 If so, combine duplicates. */
10383 if (this_bol > BEG)
10385 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10386 prev_bol = PT;
10387 prev_bol_byte = PT_BYTE;
10389 dups = message_log_check_duplicate (prev_bol_byte,
10390 this_bol_byte);
10391 if (dups)
10393 del_range_both (prev_bol, prev_bol_byte,
10394 this_bol, this_bol_byte, false);
10395 if (dups > 1)
10397 char dupstr[sizeof " [ times]"
10398 + INT_STRLEN_BOUND (printmax_t)];
10400 /* If you change this format, don't forget to also
10401 change message_log_check_duplicate. */
10402 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10403 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10404 insert_1_both (dupstr, duplen, duplen,
10405 true, false, true);
10410 /* If we have more than the desired maximum number of lines
10411 in the *Messages* buffer now, delete the oldest ones.
10412 This is safe because we don't have undo in this buffer. */
10414 if (NATNUMP (Vmessage_log_max))
10416 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10417 -XFASTINT (Vmessage_log_max) - 1, false);
10418 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10421 BEGV = marker_position (oldbegv);
10422 BEGV_BYTE = marker_byte_position (oldbegv);
10424 if (zv_at_end)
10426 ZV = Z;
10427 ZV_BYTE = Z_BYTE;
10429 else
10431 ZV = marker_position (oldzv);
10432 ZV_BYTE = marker_byte_position (oldzv);
10435 if (point_at_end)
10436 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10437 else
10438 /* We can't do Fgoto_char (oldpoint) because it will run some
10439 Lisp code. */
10440 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10441 marker_byte_position (oldpoint));
10443 unchain_marker (XMARKER (oldpoint));
10444 unchain_marker (XMARKER (oldbegv));
10445 unchain_marker (XMARKER (oldzv));
10447 /* We called insert_1_both above with its 5th argument (PREPARE)
10448 false, which prevents insert_1_both from calling
10449 prepare_to_modify_buffer, which in turns prevents us from
10450 incrementing windows_or_buffers_changed even if *Messages* is
10451 shown in some window. So we must manually set
10452 windows_or_buffers_changed here to make up for that. */
10453 windows_or_buffers_changed = old_windows_or_buffers_changed;
10454 bset_redisplay (current_buffer);
10456 set_buffer_internal (oldbuf);
10458 message_log_need_newline = !nlflag;
10459 Vdeactivate_mark = old_deactivate_mark;
10464 /* We are at the end of the buffer after just having inserted a newline.
10465 (Note: We depend on the fact we won't be crossing the gap.)
10466 Check to see if the most recent message looks a lot like the previous one.
10467 Return 0 if different, 1 if the new one should just replace it, or a
10468 value N > 1 if we should also append " [N times]". */
10470 static intmax_t
10471 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10473 ptrdiff_t i;
10474 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10475 bool seen_dots = false;
10476 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10477 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10479 for (i = 0; i < len; i++)
10481 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10482 seen_dots = true;
10483 if (p1[i] != p2[i])
10484 return seen_dots;
10486 p1 += len;
10487 if (*p1 == '\n')
10488 return 2;
10489 if (*p1++ == ' ' && *p1++ == '[')
10491 char *pend;
10492 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10493 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10494 return n + 1;
10496 return 0;
10500 /* Display an echo area message M with a specified length of NBYTES
10501 bytes. The string may include null characters. If M is not a
10502 string, clear out any existing message, and let the mini-buffer
10503 text show through.
10505 This function cancels echoing. */
10507 void
10508 message3 (Lisp_Object m)
10510 clear_message (true, true);
10511 cancel_echoing ();
10513 /* First flush out any partial line written with print. */
10514 message_log_maybe_newline ();
10515 if (STRINGP (m))
10517 ptrdiff_t nbytes = SBYTES (m);
10518 bool multibyte = STRING_MULTIBYTE (m);
10519 char *buffer;
10520 USE_SAFE_ALLOCA;
10521 SAFE_ALLOCA_STRING (buffer, m);
10522 message_dolog (buffer, nbytes, true, multibyte);
10523 SAFE_FREE ();
10525 if (! inhibit_message)
10526 message3_nolog (m);
10529 /* Log the message M to stderr. Log an empty line if M is not a string. */
10531 static void
10532 message_to_stderr (Lisp_Object m)
10534 if (noninteractive_need_newline)
10536 noninteractive_need_newline = false;
10537 fputc ('\n', stderr);
10539 if (STRINGP (m))
10541 Lisp_Object coding_system = Vlocale_coding_system;
10542 Lisp_Object s;
10544 if (!NILP (Vcoding_system_for_write))
10545 coding_system = Vcoding_system_for_write;
10546 if (!NILP (coding_system))
10547 s = code_convert_string_norecord (m, coding_system, true);
10548 else
10549 s = m;
10551 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10553 if (!cursor_in_echo_area)
10554 fputc ('\n', stderr);
10555 fflush (stderr);
10558 /* The non-logging version of message3.
10559 This does not cancel echoing, because it is used for echoing.
10560 Perhaps we need to make a separate function for echoing
10561 and make this cancel echoing. */
10563 void
10564 message3_nolog (Lisp_Object m)
10566 struct frame *sf = SELECTED_FRAME ();
10568 if (FRAME_INITIAL_P (sf))
10569 message_to_stderr (m);
10570 /* Error messages get reported properly by cmd_error, so this must be just an
10571 informative message; if the frame hasn't really been initialized yet, just
10572 toss it. */
10573 else if (INTERACTIVE && sf->glyphs_initialized_p)
10575 /* Get the frame containing the mini-buffer
10576 that the selected frame is using. */
10577 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10578 Lisp_Object frame = XWINDOW (mini_window)->frame;
10579 struct frame *f = XFRAME (frame);
10581 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10582 Fmake_frame_visible (frame);
10584 if (STRINGP (m) && SCHARS (m) > 0)
10586 set_message (m);
10587 if (minibuffer_auto_raise)
10588 Fraise_frame (frame);
10589 /* Assume we are not echoing.
10590 (If we are, echo_now will override this.) */
10591 echo_message_buffer = Qnil;
10593 else
10594 clear_message (true, true);
10596 do_pending_window_change (false);
10597 echo_area_display (true);
10598 do_pending_window_change (false);
10599 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10600 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10605 /* Display a null-terminated echo area message M. If M is 0, clear
10606 out any existing message, and let the mini-buffer text show through.
10608 The buffer M must continue to exist until after the echo area gets
10609 cleared or some other message gets displayed there. Do not pass
10610 text that is stored in a Lisp string. Do not pass text in a buffer
10611 that was alloca'd. */
10613 void
10614 message1 (const char *m)
10616 message3 (m ? build_unibyte_string (m) : Qnil);
10620 /* The non-logging counterpart of message1. */
10622 void
10623 message1_nolog (const char *m)
10625 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10628 /* Display a message M which contains a single %s
10629 which gets replaced with STRING. */
10631 void
10632 message_with_string (const char *m, Lisp_Object string, bool log)
10634 CHECK_STRING (string);
10636 bool need_message;
10637 if (noninteractive)
10638 need_message = !!m;
10639 else if (!INTERACTIVE)
10640 need_message = false;
10641 else
10643 /* The frame whose minibuffer we're going to display the message on.
10644 It may be larger than the selected frame, so we need
10645 to use its buffer, not the selected frame's buffer. */
10646 Lisp_Object mini_window;
10647 struct frame *f, *sf = SELECTED_FRAME ();
10649 /* Get the frame containing the minibuffer
10650 that the selected frame is using. */
10651 mini_window = FRAME_MINIBUF_WINDOW (sf);
10652 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10654 /* Error messages get reported properly by cmd_error, so this must be
10655 just an informative message; if the frame hasn't really been
10656 initialized yet, just toss it. */
10657 need_message = f->glyphs_initialized_p;
10660 if (need_message)
10662 AUTO_STRING (fmt, m);
10663 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10665 if (noninteractive)
10666 message_to_stderr (msg);
10667 else
10669 if (log)
10670 message3 (msg);
10671 else
10672 message3_nolog (msg);
10674 /* Print should start at the beginning of the message
10675 buffer next time. */
10676 message_buf_print = false;
10682 /* Dump an informative message to the minibuf. If M is 0, clear out
10683 any existing message, and let the mini-buffer text show through.
10685 The message must be safe ASCII (because when Emacs is
10686 non-interactive the message is sent straight to stderr without
10687 encoding first) and the format must not contain ` or ' (because
10688 this function does not account for `text-quoting-style'). If your
10689 message and format do not fit into this category, convert your
10690 arguments to Lisp objects and use Fmessage instead. */
10692 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10693 vmessage (const char *m, va_list ap)
10695 if (noninteractive)
10697 if (m)
10699 if (noninteractive_need_newline)
10700 putc ('\n', stderr);
10701 noninteractive_need_newline = false;
10702 vfprintf (stderr, m, ap);
10703 if (!cursor_in_echo_area)
10704 fprintf (stderr, "\n");
10705 fflush (stderr);
10708 else if (INTERACTIVE)
10710 /* The frame whose mini-buffer we're going to display the message
10711 on. It may be larger than the selected frame, so we need to
10712 use its buffer, not the selected frame's buffer. */
10713 Lisp_Object mini_window;
10714 struct frame *f, *sf = SELECTED_FRAME ();
10716 /* Get the frame containing the mini-buffer
10717 that the selected frame is using. */
10718 mini_window = FRAME_MINIBUF_WINDOW (sf);
10719 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10721 /* Error messages get reported properly by cmd_error, so this must be
10722 just an informative message; if the frame hasn't really been
10723 initialized yet, just toss it. */
10724 if (f->glyphs_initialized_p)
10726 if (m)
10728 ptrdiff_t len;
10729 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10730 USE_SAFE_ALLOCA;
10731 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10733 len = doprnt (message_buf, maxsize, m, 0, ap);
10735 message3 (make_string (message_buf, len));
10736 SAFE_FREE ();
10738 else
10739 message1 (0);
10741 /* Print should start at the beginning of the message
10742 buffer next time. */
10743 message_buf_print = false;
10748 /* See vmessage for restrictions on the text of the message. */
10749 void
10750 message (const char *m, ...)
10752 va_list ap;
10753 va_start (ap, m);
10754 vmessage (m, ap);
10755 va_end (ap);
10759 /* Display the current message in the current mini-buffer. This is
10760 only called from error handlers in process.c, and is not time
10761 critical. */
10763 void
10764 update_echo_area (void)
10766 if (!NILP (echo_area_buffer[0]))
10768 Lisp_Object string;
10769 string = Fcurrent_message ();
10770 message3 (string);
10775 /* Make sure echo area buffers in `echo_buffers' are live.
10776 If they aren't, make new ones. */
10778 static void
10779 ensure_echo_area_buffers (void)
10781 for (int i = 0; i < 2; i++)
10782 if (!BUFFERP (echo_buffer[i])
10783 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10785 Lisp_Object old_buffer = echo_buffer[i];
10786 static char const name_fmt[] = " *Echo Area %d*";
10787 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10788 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10789 echo_buffer[i] = Fget_buffer_create (lname);
10790 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10791 /* to force word wrap in echo area -
10792 it was decided to postpone this*/
10793 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10795 for (int j = 0; j < 2; j++)
10796 if (EQ (old_buffer, echo_area_buffer[j]))
10797 echo_area_buffer[j] = echo_buffer[i];
10802 /* Call FN with args A1..A2 with either the current or last displayed
10803 echo_area_buffer as current buffer.
10805 WHICH zero means use the current message buffer
10806 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10807 from echo_buffer[] and clear it.
10809 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10810 suitable buffer from echo_buffer[] and clear it.
10812 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10813 that the current message becomes the last displayed one, choose a
10814 suitable buffer for echo_area_buffer[0], and clear it.
10816 Value is what FN returns. */
10818 static bool
10819 with_echo_area_buffer (struct window *w, int which,
10820 bool (*fn) (ptrdiff_t, Lisp_Object),
10821 ptrdiff_t a1, Lisp_Object a2)
10823 Lisp_Object buffer;
10824 bool this_one, the_other, clear_buffer_p, rc;
10825 ptrdiff_t count = SPECPDL_INDEX ();
10827 /* If buffers aren't live, make new ones. */
10828 ensure_echo_area_buffers ();
10830 clear_buffer_p = false;
10832 if (which == 0)
10833 this_one = false, the_other = true;
10834 else if (which > 0)
10835 this_one = true, the_other = false;
10836 else
10838 this_one = false, the_other = true;
10839 clear_buffer_p = true;
10841 /* We need a fresh one in case the current echo buffer equals
10842 the one containing the last displayed echo area message. */
10843 if (!NILP (echo_area_buffer[this_one])
10844 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10845 echo_area_buffer[this_one] = Qnil;
10848 /* Choose a suitable buffer from echo_buffer[] if we don't
10849 have one. */
10850 if (NILP (echo_area_buffer[this_one]))
10852 echo_area_buffer[this_one]
10853 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10854 ? echo_buffer[the_other]
10855 : echo_buffer[this_one]);
10856 clear_buffer_p = true;
10859 buffer = echo_area_buffer[this_one];
10861 /* Don't get confused by reusing the buffer used for echoing
10862 for a different purpose. */
10863 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10864 cancel_echoing ();
10866 record_unwind_protect (unwind_with_echo_area_buffer,
10867 with_echo_area_buffer_unwind_data (w));
10869 /* Make the echo area buffer current. Note that for display
10870 purposes, it is not necessary that the displayed window's buffer
10871 == current_buffer, except for text property lookup. So, let's
10872 only set that buffer temporarily here without doing a full
10873 Fset_window_buffer. We must also change w->pointm, though,
10874 because otherwise an assertions in unshow_buffer fails, and Emacs
10875 aborts. */
10876 set_buffer_internal_1 (XBUFFER (buffer));
10877 if (w)
10879 wset_buffer (w, buffer);
10880 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10881 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10884 bset_undo_list (current_buffer, Qt);
10885 bset_read_only (current_buffer, Qnil);
10886 specbind (Qinhibit_read_only, Qt);
10887 specbind (Qinhibit_modification_hooks, Qt);
10889 if (clear_buffer_p && Z > BEG)
10890 del_range (BEG, Z);
10892 eassert (BEGV >= BEG);
10893 eassert (ZV <= Z && ZV >= BEGV);
10895 rc = fn (a1, a2);
10897 eassert (BEGV >= BEG);
10898 eassert (ZV <= Z && ZV >= BEGV);
10900 unbind_to (count, Qnil);
10901 return rc;
10905 /* Save state that should be preserved around the call to the function
10906 FN called in with_echo_area_buffer. */
10908 static Lisp_Object
10909 with_echo_area_buffer_unwind_data (struct window *w)
10911 int i = 0;
10912 Lisp_Object vector, tmp;
10914 /* Reduce consing by keeping one vector in
10915 Vwith_echo_area_save_vector. */
10916 vector = Vwith_echo_area_save_vector;
10917 Vwith_echo_area_save_vector = Qnil;
10919 if (NILP (vector))
10920 vector = Fmake_vector (make_number (11), Qnil);
10922 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10923 ASET (vector, i, Vdeactivate_mark); ++i;
10924 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10926 if (w)
10928 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10929 ASET (vector, i, w->contents); ++i;
10930 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10931 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10932 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10933 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10934 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10935 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10937 else
10939 int end = i + 8;
10940 for (; i < end; ++i)
10941 ASET (vector, i, Qnil);
10944 eassert (i == ASIZE (vector));
10945 return vector;
10949 /* Restore global state from VECTOR which was created by
10950 with_echo_area_buffer_unwind_data. */
10952 static void
10953 unwind_with_echo_area_buffer (Lisp_Object vector)
10955 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10956 Vdeactivate_mark = AREF (vector, 1);
10957 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10959 if (WINDOWP (AREF (vector, 3)))
10961 struct window *w;
10962 Lisp_Object buffer;
10964 w = XWINDOW (AREF (vector, 3));
10965 buffer = AREF (vector, 4);
10967 wset_buffer (w, buffer);
10968 set_marker_both (w->pointm, buffer,
10969 XFASTINT (AREF (vector, 5)),
10970 XFASTINT (AREF (vector, 6)));
10971 set_marker_both (w->old_pointm, buffer,
10972 XFASTINT (AREF (vector, 7)),
10973 XFASTINT (AREF (vector, 8)));
10974 set_marker_both (w->start, buffer,
10975 XFASTINT (AREF (vector, 9)),
10976 XFASTINT (AREF (vector, 10)));
10979 Vwith_echo_area_save_vector = vector;
10983 /* Set up the echo area for use by print functions. MULTIBYTE_P
10984 means we will print multibyte. */
10986 void
10987 setup_echo_area_for_printing (bool multibyte_p)
10989 /* If we can't find an echo area any more, exit. */
10990 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10991 Fkill_emacs (Qnil);
10993 ensure_echo_area_buffers ();
10995 if (!message_buf_print)
10997 /* A message has been output since the last time we printed.
10998 Choose a fresh echo area buffer. */
10999 if (EQ (echo_area_buffer[1], echo_buffer[0]))
11000 echo_area_buffer[0] = echo_buffer[1];
11001 else
11002 echo_area_buffer[0] = echo_buffer[0];
11004 /* Switch to that buffer and clear it. */
11005 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11006 bset_truncate_lines (current_buffer, Qnil);
11008 if (Z > BEG)
11010 ptrdiff_t count = SPECPDL_INDEX ();
11011 specbind (Qinhibit_read_only, Qt);
11012 /* Note that undo recording is always disabled. */
11013 del_range (BEG, Z);
11014 unbind_to (count, Qnil);
11016 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11018 /* Set up the buffer for the multibyteness we need. We always
11019 set it to be multibyte, except when
11020 unibyte-display-via-language-environment is non-nil and the
11021 buffer from which we are called is unibyte, because in that
11022 case unibyte characters should not be displayed as octal
11023 escapes. */
11024 if (unibyte_display_via_language_environment
11025 && !multibyte_p
11026 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11027 Fset_buffer_multibyte (Qnil);
11028 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
11029 Fset_buffer_multibyte (Qt);
11031 /* Raise the frame containing the echo area. */
11032 if (minibuffer_auto_raise)
11034 struct frame *sf = SELECTED_FRAME ();
11035 Lisp_Object mini_window;
11036 mini_window = FRAME_MINIBUF_WINDOW (sf);
11037 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
11040 message_log_maybe_newline ();
11041 message_buf_print = true;
11043 else
11045 if (NILP (echo_area_buffer[0]))
11047 if (EQ (echo_area_buffer[1], echo_buffer[0]))
11048 echo_area_buffer[0] = echo_buffer[1];
11049 else
11050 echo_area_buffer[0] = echo_buffer[0];
11053 if (current_buffer != XBUFFER (echo_area_buffer[0]))
11055 /* Someone switched buffers between print requests. */
11056 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11057 bset_truncate_lines (current_buffer, Qnil);
11063 /* Display an echo area message in window W. Value is true if W's
11064 height is changed. If display_last_displayed_message_p,
11065 display the message that was last displayed, otherwise
11066 display the current message. */
11068 static bool
11069 display_echo_area (struct window *w)
11071 bool no_message_p, window_height_changed_p;
11073 /* Temporarily disable garbage collections while displaying the echo
11074 area. This is done because a GC can print a message itself.
11075 That message would modify the echo area buffer's contents while a
11076 redisplay of the buffer is going on, and seriously confuse
11077 redisplay. */
11078 ptrdiff_t count = inhibit_garbage_collection ();
11080 /* If there is no message, we must call display_echo_area_1
11081 nevertheless because it resizes the window. But we will have to
11082 reset the echo_area_buffer in question to nil at the end because
11083 with_echo_area_buffer will sets it to an empty buffer. */
11084 bool i = display_last_displayed_message_p;
11085 /* According to the C99, C11 and C++11 standards, the integral value
11086 of a "bool" is always 0 or 1, so this array access is safe here,
11087 if oddly typed. */
11088 no_message_p = NILP (echo_area_buffer[i]);
11090 window_height_changed_p
11091 = with_echo_area_buffer (w, display_last_displayed_message_p,
11092 display_echo_area_1,
11093 (intptr_t) w, Qnil);
11095 if (no_message_p)
11096 echo_area_buffer[i] = Qnil;
11098 unbind_to (count, Qnil);
11099 return window_height_changed_p;
11103 /* Helper for display_echo_area. Display the current buffer which
11104 contains the current echo area message in window W, a mini-window,
11105 a pointer to which is passed in A1. A2..A4 are currently not used.
11106 Change the height of W so that all of the message is displayed.
11107 Value is true if height of W was changed. */
11109 static bool
11110 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11112 intptr_t i1 = a1;
11113 struct window *w = (struct window *) i1;
11114 Lisp_Object window;
11115 struct text_pos start;
11117 /* We are about to enter redisplay without going through
11118 redisplay_internal, so we need to forget these faces by hand
11119 here. */
11120 forget_escape_and_glyphless_faces ();
11122 /* Do this before displaying, so that we have a large enough glyph
11123 matrix for the display. If we can't get enough space for the
11124 whole text, display the last N lines. That works by setting w->start. */
11125 bool window_height_changed_p = resize_mini_window (w, false);
11127 /* Use the starting position chosen by resize_mini_window. */
11128 SET_TEXT_POS_FROM_MARKER (start, w->start);
11130 /* Display. */
11131 clear_glyph_matrix (w->desired_matrix);
11132 XSETWINDOW (window, w);
11133 try_window (window, start, 0);
11135 return window_height_changed_p;
11139 /* Resize the echo area window to exactly the size needed for the
11140 currently displayed message, if there is one. If a mini-buffer
11141 is active, don't shrink it. */
11143 void
11144 resize_echo_area_exactly (void)
11146 if (BUFFERP (echo_area_buffer[0])
11147 && WINDOWP (echo_area_window))
11149 struct window *w = XWINDOW (echo_area_window);
11150 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11151 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11152 (intptr_t) w, resize_exactly);
11153 if (resized_p)
11155 windows_or_buffers_changed = 42;
11156 update_mode_lines = 30;
11157 redisplay_internal ();
11163 /* Callback function for with_echo_area_buffer, when used from
11164 resize_echo_area_exactly. A1 contains a pointer to the window to
11165 resize, EXACTLY non-nil means resize the mini-window exactly to the
11166 size of the text displayed. A3 and A4 are not used. Value is what
11167 resize_mini_window returns. */
11169 static bool
11170 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11172 intptr_t i1 = a1;
11173 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11177 /* Resize mini-window W to fit the size of its contents. EXACT_P
11178 means size the window exactly to the size needed. Otherwise, it's
11179 only enlarged until W's buffer is empty.
11181 Set W->start to the right place to begin display. If the whole
11182 contents fit, start at the beginning. Otherwise, start so as
11183 to make the end of the contents appear. This is particularly
11184 important for y-or-n-p, but seems desirable generally.
11186 Value is true if the window height has been changed. */
11188 bool
11189 resize_mini_window (struct window *w, bool exact_p)
11191 struct frame *f = XFRAME (w->frame);
11192 bool window_height_changed_p = false;
11194 eassert (MINI_WINDOW_P (w));
11196 /* By default, start display at the beginning. */
11197 set_marker_both (w->start, w->contents,
11198 BUF_BEGV (XBUFFER (w->contents)),
11199 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11201 /* Don't resize windows while redisplaying a window; it would
11202 confuse redisplay functions when the size of the window they are
11203 displaying changes from under them. Such a resizing can happen,
11204 for instance, when which-func prints a long message while
11205 we are running fontification-functions. We're running these
11206 functions with safe_call which binds inhibit-redisplay to t. */
11207 if (!NILP (Vinhibit_redisplay))
11208 return false;
11210 /* Nil means don't try to resize. */
11211 if (NILP (Vresize_mini_windows)
11212 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11213 return false;
11215 if (!FRAME_MINIBUF_ONLY_P (f))
11217 struct it it;
11218 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11219 + WINDOW_PIXEL_HEIGHT (w));
11220 int unit = FRAME_LINE_HEIGHT (f);
11221 int height, max_height;
11222 struct text_pos start;
11223 struct buffer *old_current_buffer = NULL;
11225 if (current_buffer != XBUFFER (w->contents))
11227 old_current_buffer = current_buffer;
11228 set_buffer_internal (XBUFFER (w->contents));
11231 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11233 /* Compute the max. number of lines specified by the user. */
11234 if (FLOATP (Vmax_mini_window_height))
11235 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11236 else if (INTEGERP (Vmax_mini_window_height))
11237 max_height = XINT (Vmax_mini_window_height) * unit;
11238 else
11239 max_height = total_height / 4;
11241 /* Correct that max. height if it's bogus. */
11242 max_height = clip_to_bounds (unit, max_height, total_height);
11244 /* Find out the height of the text in the window. */
11245 if (it.line_wrap == TRUNCATE)
11246 height = unit;
11247 else
11249 last_height = 0;
11250 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11251 if (it.max_ascent == 0 && it.max_descent == 0)
11252 height = it.current_y + last_height;
11253 else
11254 height = it.current_y + it.max_ascent + it.max_descent;
11255 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11258 /* Compute a suitable window start. */
11259 if (height > max_height)
11261 height = (max_height / unit) * unit;
11262 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11263 move_it_vertically_backward (&it, height - unit);
11264 start = it.current.pos;
11266 else
11267 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11268 SET_MARKER_FROM_TEXT_POS (w->start, start);
11270 if (EQ (Vresize_mini_windows, Qgrow_only))
11272 /* Let it grow only, until we display an empty message, in which
11273 case the window shrinks again. */
11274 if (height > WINDOW_PIXEL_HEIGHT (w))
11276 int old_height = WINDOW_PIXEL_HEIGHT (w);
11278 FRAME_WINDOWS_FROZEN (f) = true;
11279 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11280 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11282 else if (height < WINDOW_PIXEL_HEIGHT (w)
11283 && (exact_p || BEGV == ZV))
11285 int old_height = WINDOW_PIXEL_HEIGHT (w);
11287 FRAME_WINDOWS_FROZEN (f) = false;
11288 shrink_mini_window (w, true);
11289 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11292 else
11294 /* Always resize to exact size needed. */
11295 if (height > WINDOW_PIXEL_HEIGHT (w))
11297 int old_height = WINDOW_PIXEL_HEIGHT (w);
11299 FRAME_WINDOWS_FROZEN (f) = true;
11300 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11301 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11303 else if (height < WINDOW_PIXEL_HEIGHT (w))
11305 int old_height = WINDOW_PIXEL_HEIGHT (w);
11307 FRAME_WINDOWS_FROZEN (f) = false;
11308 shrink_mini_window (w, true);
11310 if (height)
11312 FRAME_WINDOWS_FROZEN (f) = true;
11313 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11316 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11320 if (old_current_buffer)
11321 set_buffer_internal (old_current_buffer);
11324 return window_height_changed_p;
11328 /* Value is the current message, a string, or nil if there is no
11329 current message. */
11331 Lisp_Object
11332 current_message (void)
11334 Lisp_Object msg;
11336 if (!BUFFERP (echo_area_buffer[0]))
11337 msg = Qnil;
11338 else
11340 with_echo_area_buffer (0, 0, current_message_1,
11341 (intptr_t) &msg, Qnil);
11342 if (NILP (msg))
11343 echo_area_buffer[0] = Qnil;
11346 return msg;
11350 static bool
11351 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11353 intptr_t i1 = a1;
11354 Lisp_Object *msg = (Lisp_Object *) i1;
11356 if (Z > BEG)
11357 *msg = make_buffer_string (BEG, Z, true);
11358 else
11359 *msg = Qnil;
11360 return false;
11364 /* Push the current message on Vmessage_stack for later restoration
11365 by restore_message. Value is true if the current message isn't
11366 empty. This is a relatively infrequent operation, so it's not
11367 worth optimizing. */
11369 bool
11370 push_message (void)
11372 Lisp_Object msg = current_message ();
11373 Vmessage_stack = Fcons (msg, Vmessage_stack);
11374 return STRINGP (msg);
11378 /* Restore message display from the top of Vmessage_stack. */
11380 void
11381 restore_message (void)
11383 eassert (CONSP (Vmessage_stack));
11384 message3_nolog (XCAR (Vmessage_stack));
11388 /* Handler for unwind-protect calling pop_message. */
11390 void
11391 pop_message_unwind (void)
11393 /* Pop the top-most entry off Vmessage_stack. */
11394 eassert (CONSP (Vmessage_stack));
11395 Vmessage_stack = XCDR (Vmessage_stack);
11399 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11400 exits. If the stack is not empty, we have a missing pop_message
11401 somewhere. */
11403 void
11404 check_message_stack (void)
11406 if (!NILP (Vmessage_stack))
11407 emacs_abort ();
11411 /* Truncate to NCHARS what will be displayed in the echo area the next
11412 time we display it---but don't redisplay it now. */
11414 void
11415 truncate_echo_area (ptrdiff_t nchars)
11417 if (nchars == 0)
11418 echo_area_buffer[0] = Qnil;
11419 else if (!noninteractive
11420 && INTERACTIVE
11421 && !NILP (echo_area_buffer[0]))
11423 struct frame *sf = SELECTED_FRAME ();
11424 /* Error messages get reported properly by cmd_error, so this must be
11425 just an informative message; if the frame hasn't really been
11426 initialized yet, just toss it. */
11427 if (sf->glyphs_initialized_p)
11428 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11433 /* Helper function for truncate_echo_area. Truncate the current
11434 message to at most NCHARS characters. */
11436 static bool
11437 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11439 if (BEG + nchars < Z)
11440 del_range (BEG + nchars, Z);
11441 if (Z == BEG)
11442 echo_area_buffer[0] = Qnil;
11443 return false;
11446 /* Set the current message to STRING. */
11448 static void
11449 set_message (Lisp_Object string)
11451 eassert (STRINGP (string));
11453 message_enable_multibyte = STRING_MULTIBYTE (string);
11455 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11456 message_buf_print = false;
11457 help_echo_showing_p = false;
11459 if (STRINGP (Vdebug_on_message)
11460 && STRINGP (string)
11461 && fast_string_match (Vdebug_on_message, string) >= 0)
11462 call_debugger (list2 (Qerror, string));
11466 /* Helper function for set_message. First argument is ignored and second
11467 argument has the same meaning as for set_message.
11468 This function is called with the echo area buffer being current. */
11470 static bool
11471 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11473 eassert (STRINGP (string));
11475 /* Change multibyteness of the echo buffer appropriately. We always
11476 set it to be multibyte, except when
11477 unibyte-display-via-language-environment is non-nil and the
11478 string to display is unibyte, because in that case unibyte
11479 characters should not be displayed as octal escapes. */
11480 if (!message_enable_multibyte
11481 && unibyte_display_via_language_environment
11482 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11483 Fset_buffer_multibyte (Qnil);
11484 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
11485 Fset_buffer_multibyte (Qt);
11487 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11488 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11489 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11491 /* Insert new message at BEG. */
11492 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11494 /* This function takes care of single/multibyte conversion.
11495 We just have to ensure that the echo area buffer has the right
11496 setting of enable_multibyte_characters. */
11497 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11499 return false;
11503 /* Clear messages. CURRENT_P means clear the current message.
11504 LAST_DISPLAYED_P means clear the message last displayed. */
11506 void
11507 clear_message (bool current_p, bool last_displayed_p)
11509 if (current_p)
11511 echo_area_buffer[0] = Qnil;
11512 message_cleared_p = true;
11515 if (last_displayed_p)
11516 echo_area_buffer[1] = Qnil;
11518 message_buf_print = false;
11521 /* Clear garbaged frames.
11523 This function is used where the old redisplay called
11524 redraw_garbaged_frames which in turn called redraw_frame which in
11525 turn called clear_frame. The call to clear_frame was a source of
11526 flickering. I believe a clear_frame is not necessary. It should
11527 suffice in the new redisplay to invalidate all current matrices,
11528 and ensure a complete redisplay of all windows. */
11530 static void
11531 clear_garbaged_frames (void)
11533 if (frame_garbaged)
11535 Lisp_Object tail, frame;
11536 struct frame *sf = SELECTED_FRAME ();
11538 FOR_EACH_FRAME (tail, frame)
11540 struct frame *f = XFRAME (frame);
11542 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11544 if (f->resized_p
11545 /* It makes no sense to redraw a non-selected TTY
11546 frame, since that will actually clear the
11547 selected frame, and might leave the selected
11548 frame with corrupted display, if it happens not
11549 to be marked garbaged. */
11550 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11551 redraw_frame (f);
11552 else
11553 clear_current_matrices (f);
11555 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11556 x_clear_under_internal_border (f);
11557 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11559 fset_redisplay (f);
11560 f->garbaged = false;
11561 f->resized_p = false;
11565 frame_garbaged = false;
11570 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11571 selected_frame. */
11573 static void
11574 echo_area_display (bool update_frame_p)
11576 Lisp_Object mini_window;
11577 struct window *w;
11578 struct frame *f;
11579 bool window_height_changed_p = false;
11580 struct frame *sf = SELECTED_FRAME ();
11582 mini_window = FRAME_MINIBUF_WINDOW (sf);
11583 if (NILP (mini_window))
11584 return;
11586 w = XWINDOW (mini_window);
11587 f = XFRAME (WINDOW_FRAME (w));
11589 /* Don't display if frame is invisible or not yet initialized. */
11590 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11591 return;
11593 #ifdef HAVE_WINDOW_SYSTEM
11594 /* When Emacs starts, selected_frame may be the initial terminal
11595 frame. If we let this through, a message would be displayed on
11596 the terminal. */
11597 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11598 return;
11599 #endif /* HAVE_WINDOW_SYSTEM */
11601 /* Redraw garbaged frames. */
11602 clear_garbaged_frames ();
11604 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11606 echo_area_window = mini_window;
11607 window_height_changed_p = display_echo_area (w);
11608 w->must_be_updated_p = true;
11610 /* Update the display, unless called from redisplay_internal.
11611 Also don't update the screen during redisplay itself. The
11612 update will happen at the end of redisplay, and an update
11613 here could cause confusion. */
11614 if (update_frame_p && !redisplaying_p)
11616 int n = 0;
11618 /* If the display update has been interrupted by pending
11619 input, update mode lines in the frame. Due to the
11620 pending input, it might have been that redisplay hasn't
11621 been called, so that mode lines above the echo area are
11622 garbaged. This looks odd, so we prevent it here. */
11623 if (!display_completed)
11625 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11627 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11628 x_clear_under_internal_border (f);
11629 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11633 if (window_height_changed_p
11634 /* Don't do this if Emacs is shutting down. Redisplay
11635 needs to run hooks. */
11636 && !NILP (Vrun_hooks))
11638 /* Must update other windows. Likewise as in other
11639 cases, don't let this update be interrupted by
11640 pending input. */
11641 ptrdiff_t count = SPECPDL_INDEX ();
11642 specbind (Qredisplay_dont_pause, Qt);
11643 fset_redisplay (f);
11644 redisplay_internal ();
11645 unbind_to (count, Qnil);
11647 else if (FRAME_WINDOW_P (f) && n == 0)
11649 /* Window configuration is the same as before.
11650 Can do with a display update of the echo area,
11651 unless we displayed some mode lines. */
11652 update_single_window (w);
11653 flush_frame (f);
11655 else
11656 update_frame (f, true, true);
11658 /* If cursor is in the echo area, make sure that the next
11659 redisplay displays the minibuffer, so that the cursor will
11660 be replaced with what the minibuffer wants. */
11661 if (cursor_in_echo_area)
11662 wset_redisplay (XWINDOW (mini_window));
11665 else if (!EQ (mini_window, selected_window))
11666 wset_redisplay (XWINDOW (mini_window));
11668 /* Last displayed message is now the current message. */
11669 echo_area_buffer[1] = echo_area_buffer[0];
11670 /* Inform read_char that we're not echoing. */
11671 echo_message_buffer = Qnil;
11673 /* Prevent redisplay optimization in redisplay_internal by resetting
11674 this_line_start_pos. This is done because the mini-buffer now
11675 displays the message instead of its buffer text. */
11676 if (EQ (mini_window, selected_window))
11677 CHARPOS (this_line_start_pos) = 0;
11679 if (window_height_changed_p)
11681 fset_redisplay (f);
11683 /* If window configuration was changed, frames may have been
11684 marked garbaged. Clear them or we will experience
11685 surprises wrt scrolling.
11686 FIXME: How/why/when? */
11687 clear_garbaged_frames ();
11691 /* True if W's buffer was changed but not saved. */
11693 static bool
11694 window_buffer_changed (struct window *w)
11696 struct buffer *b = XBUFFER (w->contents);
11698 eassert (BUFFER_LIVE_P (b));
11700 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11703 /* True if W has %c or %C in its mode line and mode line should be updated. */
11705 static bool
11706 mode_line_update_needed (struct window *w)
11708 return (w->column_number_displayed != -1
11709 && !(PT == w->last_point && !window_outdated (w))
11710 && (w->column_number_displayed != current_column ()));
11713 /* True if window start of W is frozen and may not be changed during
11714 redisplay. */
11716 static bool
11717 window_frozen_p (struct window *w)
11719 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11721 Lisp_Object window;
11723 XSETWINDOW (window, w);
11724 if (MINI_WINDOW_P (w))
11725 return false;
11726 else if (EQ (window, selected_window))
11727 return false;
11728 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11729 && EQ (window, Vminibuf_scroll_window))
11730 /* This special window can't be frozen too. */
11731 return false;
11732 else
11733 return true;
11735 return false;
11738 /***********************************************************************
11739 Mode Lines and Frame Titles
11740 ***********************************************************************/
11742 /* A buffer for constructing non-propertized mode-line strings and
11743 frame titles in it; allocated from the heap in init_xdisp and
11744 resized as needed in store_mode_line_noprop_char. */
11746 static char *mode_line_noprop_buf;
11748 /* The buffer's end, and a current output position in it. */
11750 static char *mode_line_noprop_buf_end;
11751 static char *mode_line_noprop_ptr;
11753 #define MODE_LINE_NOPROP_LEN(start) \
11754 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11756 static enum {
11757 MODE_LINE_DISPLAY = 0,
11758 MODE_LINE_TITLE,
11759 MODE_LINE_NOPROP,
11760 MODE_LINE_STRING
11761 } mode_line_target;
11763 /* Alist that caches the results of :propertize.
11764 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11765 static Lisp_Object mode_line_proptrans_alist;
11767 /* List of strings making up the mode-line. */
11768 static Lisp_Object mode_line_string_list;
11770 /* Base face property when building propertized mode line string. */
11771 static Lisp_Object mode_line_string_face;
11772 static Lisp_Object mode_line_string_face_prop;
11775 /* Unwind data for mode line strings */
11777 static Lisp_Object Vmode_line_unwind_vector;
11779 static Lisp_Object
11780 format_mode_line_unwind_data (struct frame *target_frame,
11781 struct buffer *obuf,
11782 Lisp_Object owin,
11783 bool save_proptrans)
11785 Lisp_Object vector, tmp;
11787 /* Reduce consing by keeping one vector in
11788 Vwith_echo_area_save_vector. */
11789 vector = Vmode_line_unwind_vector;
11790 Vmode_line_unwind_vector = Qnil;
11792 if (NILP (vector))
11793 vector = Fmake_vector (make_number (10), Qnil);
11795 ASET (vector, 0, make_number (mode_line_target));
11796 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11797 ASET (vector, 2, mode_line_string_list);
11798 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11799 ASET (vector, 4, mode_line_string_face);
11800 ASET (vector, 5, mode_line_string_face_prop);
11802 if (obuf)
11803 XSETBUFFER (tmp, obuf);
11804 else
11805 tmp = Qnil;
11806 ASET (vector, 6, tmp);
11807 ASET (vector, 7, owin);
11808 if (target_frame)
11810 /* Similarly to `with-selected-window', if the operation selects
11811 a window on another frame, we must restore that frame's
11812 selected window, and (for a tty) the top-frame. */
11813 ASET (vector, 8, target_frame->selected_window);
11814 if (FRAME_TERMCAP_P (target_frame))
11815 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11818 return vector;
11821 static void
11822 unwind_format_mode_line (Lisp_Object vector)
11824 Lisp_Object old_window = AREF (vector, 7);
11825 Lisp_Object target_frame_window = AREF (vector, 8);
11826 Lisp_Object old_top_frame = AREF (vector, 9);
11828 mode_line_target = XINT (AREF (vector, 0));
11829 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11830 mode_line_string_list = AREF (vector, 2);
11831 if (! EQ (AREF (vector, 3), Qt))
11832 mode_line_proptrans_alist = AREF (vector, 3);
11833 mode_line_string_face = AREF (vector, 4);
11834 mode_line_string_face_prop = AREF (vector, 5);
11836 /* Select window before buffer, since it may change the buffer. */
11837 if (!NILP (old_window))
11839 /* If the operation that we are unwinding had selected a window
11840 on a different frame, reset its frame-selected-window. For a
11841 text terminal, reset its top-frame if necessary. */
11842 if (!NILP (target_frame_window))
11844 Lisp_Object frame
11845 = WINDOW_FRAME (XWINDOW (target_frame_window));
11847 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11848 Fselect_window (target_frame_window, Qt);
11850 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11851 Fselect_frame (old_top_frame, Qt);
11854 Fselect_window (old_window, Qt);
11857 if (!NILP (AREF (vector, 6)))
11859 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11860 ASET (vector, 6, Qnil);
11863 Vmode_line_unwind_vector = vector;
11867 /* Store a single character C for the frame title in mode_line_noprop_buf.
11868 Re-allocate mode_line_noprop_buf if necessary. */
11870 static void
11871 store_mode_line_noprop_char (char c)
11873 /* If output position has reached the end of the allocated buffer,
11874 increase the buffer's size. */
11875 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11877 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11878 ptrdiff_t size = len;
11879 mode_line_noprop_buf =
11880 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11881 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11882 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11885 *mode_line_noprop_ptr++ = c;
11889 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11890 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11891 characters that yield more columns than PRECISION; PRECISION <= 0
11892 means copy the whole string. Pad with spaces until FIELD_WIDTH
11893 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11894 pad. Called from display_mode_element when it is used to build a
11895 frame title. */
11897 static int
11898 store_mode_line_noprop (const char *string, int field_width, int precision)
11900 const unsigned char *str = (const unsigned char *) string;
11901 int n = 0;
11902 ptrdiff_t dummy, nbytes;
11904 /* Copy at most PRECISION chars from STR. */
11905 nbytes = strlen (string);
11906 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11907 while (nbytes--)
11908 store_mode_line_noprop_char (*str++);
11910 /* Fill up with spaces until FIELD_WIDTH reached. */
11911 while (field_width > 0
11912 && n < field_width)
11914 store_mode_line_noprop_char (' ');
11915 ++n;
11918 return n;
11921 /***********************************************************************
11922 Frame Titles
11923 ***********************************************************************/
11925 #ifdef HAVE_WINDOW_SYSTEM
11927 /* Set the title of FRAME, if it has changed. The title format is
11928 Vicon_title_format if FRAME is iconified, otherwise it is
11929 frame_title_format. */
11931 static void
11932 x_consider_frame_title (Lisp_Object frame)
11934 struct frame *f = XFRAME (frame);
11936 if ((FRAME_WINDOW_P (f)
11937 || FRAME_MINIBUF_ONLY_P (f)
11938 || f->explicit_name)
11939 && !FRAME_TOOLTIP_P (f))
11941 /* Do we have more than one visible frame on this X display? */
11942 Lisp_Object tail, other_frame, fmt;
11943 ptrdiff_t title_start;
11944 char *title;
11945 ptrdiff_t len;
11946 struct it it;
11947 ptrdiff_t count = SPECPDL_INDEX ();
11949 FOR_EACH_FRAME (tail, other_frame)
11951 struct frame *tf = XFRAME (other_frame);
11953 if (tf != f
11954 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11955 && !FRAME_MINIBUF_ONLY_P (tf)
11956 && !FRAME_PARENT_FRAME (tf)
11957 && !FRAME_TOOLTIP_P (tf)
11958 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11959 break;
11962 /* Set global variable indicating that multiple frames exist. */
11963 multiple_frames = CONSP (tail);
11965 /* Switch to the buffer of selected window of the frame. Set up
11966 mode_line_target so that display_mode_element will output into
11967 mode_line_noprop_buf; then display the title. */
11968 record_unwind_protect (unwind_format_mode_line,
11969 format_mode_line_unwind_data
11970 (f, current_buffer, selected_window, false));
11971 /* select-frame calls resize_mini_window, which could resize the
11972 mini-window and by that undo the effect of this redisplay
11973 cycle wrt minibuffer and echo-area display. Binding
11974 inhibit-redisplay to t makes the call to resize_mini_window a
11975 no-op, thus avoiding the adverse side effects. */
11976 specbind (Qinhibit_redisplay, Qt);
11978 Fselect_window (f->selected_window, Qt);
11979 set_buffer_internal_1
11980 (XBUFFER (XWINDOW (f->selected_window)->contents));
11981 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11983 mode_line_target = MODE_LINE_TITLE;
11984 title_start = MODE_LINE_NOPROP_LEN (0);
11985 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11986 NULL, DEFAULT_FACE_ID);
11987 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11988 len = MODE_LINE_NOPROP_LEN (title_start);
11989 title = mode_line_noprop_buf + title_start;
11990 unbind_to (count, Qnil);
11992 /* Set the title only if it's changed. This avoids consing in
11993 the common case where it hasn't. (If it turns out that we've
11994 already wasted too much time by walking through the list with
11995 display_mode_element, then we might need to optimize at a
11996 higher level than this.) */
11997 if (! STRINGP (f->name)
11998 || SBYTES (f->name) != len
11999 || memcmp (title, SDATA (f->name), len) != 0)
12000 x_implicitly_set_name (f, make_string (title, len), Qnil);
12004 #endif /* not HAVE_WINDOW_SYSTEM */
12007 /***********************************************************************
12008 Menu Bars
12009 ***********************************************************************/
12011 /* True if we will not redisplay all visible windows. */
12012 #define REDISPLAY_SOME_P() \
12013 ((windows_or_buffers_changed == 0 \
12014 || windows_or_buffers_changed == REDISPLAY_SOME) \
12015 && (update_mode_lines == 0 \
12016 || update_mode_lines == REDISPLAY_SOME))
12018 /* Prepare for redisplay by updating menu-bar item lists when
12019 appropriate. This can call eval. */
12021 static void
12022 prepare_menu_bars (void)
12024 bool all_windows = windows_or_buffers_changed || update_mode_lines;
12025 bool some_windows = REDISPLAY_SOME_P ();
12027 if (FUNCTIONP (Vpre_redisplay_function))
12029 Lisp_Object windows = all_windows ? Qt : Qnil;
12030 if (all_windows && some_windows)
12032 Lisp_Object ws = window_list ();
12033 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
12035 Lisp_Object this = XCAR (ws);
12036 struct window *w = XWINDOW (this);
12037 if (w->redisplay
12038 || XFRAME (w->frame)->redisplay
12039 || XBUFFER (w->contents)->text->redisplay)
12041 windows = Fcons (this, windows);
12045 safe__call1 (true, Vpre_redisplay_function, windows);
12048 /* Update all frame titles based on their buffer names, etc. We do
12049 this before the menu bars so that the buffer-menu will show the
12050 up-to-date frame titles. */
12051 #ifdef HAVE_WINDOW_SYSTEM
12052 if (all_windows)
12054 Lisp_Object tail, frame;
12056 FOR_EACH_FRAME (tail, frame)
12058 struct frame *f = XFRAME (frame);
12059 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12060 if (some_windows
12061 && !f->redisplay
12062 && !w->redisplay
12063 && !XBUFFER (w->contents)->text->redisplay)
12064 continue;
12066 if (!FRAME_TOOLTIP_P (f)
12067 && !FRAME_PARENT_FRAME (f)
12068 && (FRAME_ICONIFIED_P (f)
12069 || FRAME_VISIBLE_P (f) == 1
12070 /* Exclude TTY frames that are obscured because they
12071 are not the top frame on their console. This is
12072 because x_consider_frame_title actually switches
12073 to the frame, which for TTY frames means it is
12074 marked as garbaged, and will be completely
12075 redrawn on the next redisplay cycle. This causes
12076 TTY frames to be completely redrawn, when there
12077 are more than one of them, even though nothing
12078 should be changed on display. */
12079 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12080 x_consider_frame_title (frame);
12083 #endif /* HAVE_WINDOW_SYSTEM */
12085 /* Update the menu bar item lists, if appropriate. This has to be
12086 done before any actual redisplay or generation of display lines. */
12088 if (all_windows)
12090 Lisp_Object tail, frame;
12091 ptrdiff_t count = SPECPDL_INDEX ();
12092 /* True means that update_menu_bar has run its hooks
12093 so any further calls to update_menu_bar shouldn't do so again. */
12094 bool menu_bar_hooks_run = false;
12096 record_unwind_save_match_data ();
12098 FOR_EACH_FRAME (tail, frame)
12100 struct frame *f = XFRAME (frame);
12101 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12103 /* Ignore tooltip frame. */
12104 if (FRAME_TOOLTIP_P (f))
12105 continue;
12107 if (some_windows
12108 && !f->redisplay
12109 && !w->redisplay
12110 && !XBUFFER (w->contents)->text->redisplay)
12111 continue;
12113 run_window_size_change_functions (frame);
12115 if (FRAME_PARENT_FRAME (f))
12116 continue;
12118 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12119 #ifdef HAVE_WINDOW_SYSTEM
12120 update_tool_bar (f, false);
12121 #endif
12124 unbind_to (count, Qnil);
12126 else
12128 struct frame *sf = SELECTED_FRAME ();
12129 update_menu_bar (sf, true, false);
12130 #ifdef HAVE_WINDOW_SYSTEM
12131 update_tool_bar (sf, true);
12132 #endif
12137 /* Update the menu bar item list for frame F. This has to be done
12138 before we start to fill in any display lines, because it can call
12139 eval.
12141 If SAVE_MATCH_DATA, we must save and restore it here.
12143 If HOOKS_RUN, a previous call to update_menu_bar
12144 already ran the menu bar hooks for this redisplay, so there
12145 is no need to run them again. The return value is the
12146 updated value of this flag, to pass to the next call. */
12148 static bool
12149 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12151 Lisp_Object window;
12152 struct window *w;
12154 /* If called recursively during a menu update, do nothing. This can
12155 happen when, for instance, an activate-menubar-hook causes a
12156 redisplay. */
12157 if (inhibit_menubar_update)
12158 return hooks_run;
12160 window = FRAME_SELECTED_WINDOW (f);
12161 w = XWINDOW (window);
12163 if (FRAME_WINDOW_P (f)
12165 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12166 || defined (HAVE_NS) || defined (USE_GTK)
12167 FRAME_EXTERNAL_MENU_BAR (f)
12168 #else
12169 FRAME_MENU_BAR_LINES (f) > 0
12170 #endif
12171 : FRAME_MENU_BAR_LINES (f) > 0)
12173 /* If the user has switched buffers or windows, we need to
12174 recompute to reflect the new bindings. But we'll
12175 recompute when update_mode_lines is set too; that means
12176 that people can use force-mode-line-update to request
12177 that the menu bar be recomputed. The adverse effect on
12178 the rest of the redisplay algorithm is about the same as
12179 windows_or_buffers_changed anyway. */
12180 if (windows_or_buffers_changed
12181 /* This used to test w->update_mode_line, but we believe
12182 there is no need to recompute the menu in that case. */
12183 || update_mode_lines
12184 || window_buffer_changed (w))
12186 struct buffer *prev = current_buffer;
12187 ptrdiff_t count = SPECPDL_INDEX ();
12189 specbind (Qinhibit_menubar_update, Qt);
12191 set_buffer_internal_1 (XBUFFER (w->contents));
12192 if (save_match_data)
12193 record_unwind_save_match_data ();
12194 if (NILP (Voverriding_local_map_menu_flag))
12196 specbind (Qoverriding_terminal_local_map, Qnil);
12197 specbind (Qoverriding_local_map, Qnil);
12200 if (!hooks_run)
12202 /* Run the Lucid hook. */
12203 safe_run_hooks (Qactivate_menubar_hook);
12205 /* If it has changed current-menubar from previous value,
12206 really recompute the menu-bar from the value. */
12207 if (! NILP (Vlucid_menu_bar_dirty_flag))
12208 call0 (Qrecompute_lucid_menubar);
12210 safe_run_hooks (Qmenu_bar_update_hook);
12212 hooks_run = true;
12215 XSETFRAME (Vmenu_updating_frame, f);
12216 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12218 /* Redisplay the menu bar in case we changed it. */
12219 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12220 || defined (HAVE_NS) || defined (USE_GTK)
12221 if (FRAME_WINDOW_P (f))
12223 #if defined (HAVE_NS)
12224 /* All frames on Mac OS share the same menubar. So only
12225 the selected frame should be allowed to set it. */
12226 if (f == SELECTED_FRAME ())
12227 #endif
12228 set_frame_menubar (f, false, false);
12230 else
12231 /* On a terminal screen, the menu bar is an ordinary screen
12232 line, and this makes it get updated. */
12233 w->update_mode_line = true;
12234 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12235 /* In the non-toolkit version, the menu bar is an ordinary screen
12236 line, and this makes it get updated. */
12237 w->update_mode_line = true;
12238 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12240 unbind_to (count, Qnil);
12241 set_buffer_internal_1 (prev);
12245 return hooks_run;
12248 /***********************************************************************
12249 Tool-bars
12250 ***********************************************************************/
12252 #ifdef HAVE_WINDOW_SYSTEM
12254 /* Select `frame' temporarily without running all the code in
12255 do_switch_frame.
12256 FIXME: Maybe do_switch_frame should be trimmed down similarly
12257 when `norecord' is set. */
12258 static void
12259 fast_set_selected_frame (Lisp_Object frame)
12261 if (!EQ (selected_frame, frame))
12263 selected_frame = frame;
12264 selected_window = XFRAME (frame)->selected_window;
12268 /* Update the tool-bar item list for frame F. This has to be done
12269 before we start to fill in any display lines. Called from
12270 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12271 and restore it here. */
12273 static void
12274 update_tool_bar (struct frame *f, bool save_match_data)
12276 #if defined (USE_GTK) || defined (HAVE_NS)
12277 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12278 #else
12279 bool do_update = (WINDOWP (f->tool_bar_window)
12280 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12281 #endif
12283 if (do_update)
12285 Lisp_Object window;
12286 struct window *w;
12288 window = FRAME_SELECTED_WINDOW (f);
12289 w = XWINDOW (window);
12291 /* If the user has switched buffers or windows, we need to
12292 recompute to reflect the new bindings. But we'll
12293 recompute when update_mode_lines is set too; that means
12294 that people can use force-mode-line-update to request
12295 that the menu bar be recomputed. The adverse effect on
12296 the rest of the redisplay algorithm is about the same as
12297 windows_or_buffers_changed anyway. */
12298 if (windows_or_buffers_changed
12299 || w->update_mode_line
12300 || update_mode_lines
12301 || window_buffer_changed (w))
12303 struct buffer *prev = current_buffer;
12304 ptrdiff_t count = SPECPDL_INDEX ();
12305 Lisp_Object frame, new_tool_bar;
12306 int new_n_tool_bar;
12308 /* Set current_buffer to the buffer of the selected
12309 window of the frame, so that we get the right local
12310 keymaps. */
12311 set_buffer_internal_1 (XBUFFER (w->contents));
12313 /* Save match data, if we must. */
12314 if (save_match_data)
12315 record_unwind_save_match_data ();
12317 /* Make sure that we don't accidentally use bogus keymaps. */
12318 if (NILP (Voverriding_local_map_menu_flag))
12320 specbind (Qoverriding_terminal_local_map, Qnil);
12321 specbind (Qoverriding_local_map, Qnil);
12324 /* We must temporarily set the selected frame to this frame
12325 before calling tool_bar_items, because the calculation of
12326 the tool-bar keymap uses the selected frame (see
12327 `tool-bar-make-keymap' in tool-bar.el). */
12328 eassert (EQ (selected_window,
12329 /* Since we only explicitly preserve selected_frame,
12330 check that selected_window would be redundant. */
12331 XFRAME (selected_frame)->selected_window));
12332 record_unwind_protect (fast_set_selected_frame, selected_frame);
12333 XSETFRAME (frame, f);
12334 fast_set_selected_frame (frame);
12336 /* Build desired tool-bar items from keymaps. */
12337 new_tool_bar
12338 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12339 &new_n_tool_bar);
12341 /* Redisplay the tool-bar if we changed it. */
12342 if (new_n_tool_bar != f->n_tool_bar_items
12343 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12345 /* Redisplay that happens asynchronously due to an expose event
12346 may access f->tool_bar_items. Make sure we update both
12347 variables within BLOCK_INPUT so no such event interrupts. */
12348 block_input ();
12349 fset_tool_bar_items (f, new_tool_bar);
12350 f->n_tool_bar_items = new_n_tool_bar;
12351 w->update_mode_line = true;
12352 unblock_input ();
12355 unbind_to (count, Qnil);
12356 set_buffer_internal_1 (prev);
12361 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12363 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12364 F's desired tool-bar contents. F->tool_bar_items must have
12365 been set up previously by calling prepare_menu_bars. */
12367 static void
12368 build_desired_tool_bar_string (struct frame *f)
12370 int i, size, size_needed;
12371 Lisp_Object image, plist;
12373 image = plist = Qnil;
12375 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12376 Otherwise, make a new string. */
12378 /* The size of the string we might be able to reuse. */
12379 size = (STRINGP (f->desired_tool_bar_string)
12380 ? SCHARS (f->desired_tool_bar_string)
12381 : 0);
12383 /* We need one space in the string for each image. */
12384 size_needed = f->n_tool_bar_items;
12386 /* Reuse f->desired_tool_bar_string, if possible. */
12387 if (size < size_needed || NILP (f->desired_tool_bar_string))
12388 fset_desired_tool_bar_string
12389 (f, Fmake_string (make_number (size_needed), make_number (' '), Qnil));
12390 else
12392 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12393 Fremove_text_properties (make_number (0), make_number (size),
12394 props, f->desired_tool_bar_string);
12397 /* Put a `display' property on the string for the images to display,
12398 put a `menu_item' property on tool-bar items with a value that
12399 is the index of the item in F's tool-bar item vector. */
12400 for (i = 0; i < f->n_tool_bar_items; ++i)
12402 #define PROP(IDX) \
12403 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12405 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12406 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12407 int hmargin, vmargin, relief, idx, end;
12409 /* If image is a vector, choose the image according to the
12410 button state. */
12411 image = PROP (TOOL_BAR_ITEM_IMAGES);
12412 if (VECTORP (image))
12414 if (enabled_p)
12415 idx = (selected_p
12416 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12417 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12418 else
12419 idx = (selected_p
12420 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12421 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12423 eassert (ASIZE (image) >= idx);
12424 image = AREF (image, idx);
12426 else
12427 idx = -1;
12429 /* Ignore invalid image specifications. */
12430 if (!valid_image_p (image))
12431 continue;
12433 /* Display the tool-bar button pressed, or depressed. */
12434 plist = Fcopy_sequence (XCDR (image));
12436 /* Compute margin and relief to draw. */
12437 relief = (tool_bar_button_relief >= 0
12438 ? tool_bar_button_relief
12439 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12440 hmargin = vmargin = relief;
12442 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12443 INT_MAX - max (hmargin, vmargin)))
12445 hmargin += XFASTINT (Vtool_bar_button_margin);
12446 vmargin += XFASTINT (Vtool_bar_button_margin);
12448 else if (CONSP (Vtool_bar_button_margin))
12450 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12451 INT_MAX - hmargin))
12452 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12454 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12455 INT_MAX - vmargin))
12456 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12459 if (auto_raise_tool_bar_buttons_p)
12461 /* Add a `:relief' property to the image spec if the item is
12462 selected. */
12463 if (selected_p)
12465 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12466 hmargin -= relief;
12467 vmargin -= relief;
12470 else
12472 /* If image is selected, display it pressed, i.e. with a
12473 negative relief. If it's not selected, display it with a
12474 raised relief. */
12475 plist = Fplist_put (plist, QCrelief,
12476 (selected_p
12477 ? make_number (-relief)
12478 : make_number (relief)));
12479 hmargin -= relief;
12480 vmargin -= relief;
12483 /* Put a margin around the image. */
12484 if (hmargin || vmargin)
12486 if (hmargin == vmargin)
12487 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12488 else
12489 plist = Fplist_put (plist, QCmargin,
12490 Fcons (make_number (hmargin),
12491 make_number (vmargin)));
12494 /* If button is not enabled, and we don't have special images
12495 for the disabled state, make the image appear disabled by
12496 applying an appropriate algorithm to it. */
12497 if (!enabled_p && idx < 0)
12498 plist = Fplist_put (plist, QCconversion, Qdisabled);
12500 /* Put a `display' text property on the string for the image to
12501 display. Put a `menu-item' property on the string that gives
12502 the start of this item's properties in the tool-bar items
12503 vector. */
12504 image = Fcons (Qimage, plist);
12505 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12506 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12508 /* Let the last image hide all remaining spaces in the tool bar
12509 string. The string can be longer than needed when we reuse a
12510 previous string. */
12511 if (i + 1 == f->n_tool_bar_items)
12512 end = SCHARS (f->desired_tool_bar_string);
12513 else
12514 end = i + 1;
12515 Fadd_text_properties (make_number (i), make_number (end),
12516 props, f->desired_tool_bar_string);
12517 #undef PROP
12522 /* Display one line of the tool-bar of frame IT->f.
12524 HEIGHT specifies the desired height of the tool-bar line.
12525 If the actual height of the glyph row is less than HEIGHT, the
12526 row's height is increased to HEIGHT, and the icons are centered
12527 vertically in the new height.
12529 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12530 count a final empty row in case the tool-bar width exactly matches
12531 the window width.
12534 static void
12535 display_tool_bar_line (struct it *it, int height)
12537 struct glyph_row *row = it->glyph_row;
12538 int max_x = it->last_visible_x;
12539 struct glyph *last;
12541 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12542 clear_glyph_row (row);
12543 row->enabled_p = true;
12544 row->y = it->current_y;
12546 /* Note that this isn't made use of if the face hasn't a box,
12547 so there's no need to check the face here. */
12548 it->start_of_box_run_p = true;
12550 while (it->current_x < max_x)
12552 int x, n_glyphs_before, i, nglyphs;
12553 struct it it_before;
12555 /* Get the next display element. */
12556 if (!get_next_display_element (it))
12558 /* Don't count empty row if we are counting needed tool-bar lines. */
12559 if (height < 0 && !it->hpos)
12560 return;
12561 break;
12564 /* Produce glyphs. */
12565 n_glyphs_before = row->used[TEXT_AREA];
12566 it_before = *it;
12568 PRODUCE_GLYPHS (it);
12570 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12571 i = 0;
12572 x = it_before.current_x;
12573 while (i < nglyphs)
12575 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12577 if (x + glyph->pixel_width > max_x)
12579 /* Glyph doesn't fit on line. Backtrack. */
12580 row->used[TEXT_AREA] = n_glyphs_before;
12581 *it = it_before;
12582 /* If this is the only glyph on this line, it will never fit on the
12583 tool-bar, so skip it. But ensure there is at least one glyph,
12584 so we don't accidentally disable the tool-bar. */
12585 if (n_glyphs_before == 0
12586 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12587 break;
12588 goto out;
12591 ++it->hpos;
12592 x += glyph->pixel_width;
12593 ++i;
12596 /* Stop at line end. */
12597 if (ITERATOR_AT_END_OF_LINE_P (it))
12598 break;
12600 set_iterator_to_next (it, true);
12603 out:;
12605 row->displays_text_p = row->used[TEXT_AREA] != 0;
12607 /* Use default face for the border below the tool bar.
12609 FIXME: When auto-resize-tool-bars is grow-only, there is
12610 no additional border below the possibly empty tool-bar lines.
12611 So to make the extra empty lines look "normal", we have to
12612 use the tool-bar face for the border too. */
12613 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12614 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12615 it->face_id = DEFAULT_FACE_ID;
12617 extend_face_to_end_of_line (it);
12618 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12619 last->right_box_line_p = true;
12620 if (last == row->glyphs[TEXT_AREA])
12621 last->left_box_line_p = true;
12623 /* Make line the desired height and center it vertically. */
12624 if ((height -= it->max_ascent + it->max_descent) > 0)
12626 /* Don't add more than one line height. */
12627 height %= FRAME_LINE_HEIGHT (it->f);
12628 it->max_ascent += height / 2;
12629 it->max_descent += (height + 1) / 2;
12632 compute_line_metrics (it);
12634 /* If line is empty, make it occupy the rest of the tool-bar. */
12635 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12637 row->height = row->phys_height = it->last_visible_y - row->y;
12638 row->visible_height = row->height;
12639 row->ascent = row->phys_ascent = 0;
12640 row->extra_line_spacing = 0;
12643 row->full_width_p = true;
12644 row->continued_p = false;
12645 row->truncated_on_left_p = false;
12646 row->truncated_on_right_p = false;
12648 it->current_x = it->hpos = 0;
12649 it->current_y += row->height;
12650 ++it->vpos;
12651 ++it->glyph_row;
12655 /* Value is the number of pixels needed to make all tool-bar items of
12656 frame F visible. The actual number of glyph rows needed is
12657 returned in *N_ROWS if non-NULL. */
12658 static int
12659 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12661 struct window *w = XWINDOW (f->tool_bar_window);
12662 struct it it;
12663 /* tool_bar_height is called from redisplay_tool_bar after building
12664 the desired matrix, so use (unused) mode-line row as temporary row to
12665 avoid destroying the first tool-bar row. */
12666 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12668 /* Initialize an iterator for iteration over
12669 F->desired_tool_bar_string in the tool-bar window of frame F. */
12670 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12671 temp_row->reversed_p = false;
12672 it.first_visible_x = 0;
12673 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12674 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12675 it.paragraph_embedding = L2R;
12677 while (!ITERATOR_AT_END_P (&it))
12679 clear_glyph_row (temp_row);
12680 it.glyph_row = temp_row;
12681 display_tool_bar_line (&it, -1);
12683 clear_glyph_row (temp_row);
12685 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12686 if (n_rows)
12687 *n_rows = it.vpos > 0 ? it.vpos : -1;
12689 if (pixelwise)
12690 return it.current_y;
12691 else
12692 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12695 #endif /* !USE_GTK && !HAVE_NS */
12697 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12698 0, 2, 0,
12699 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12700 If FRAME is nil or omitted, use the selected frame. Optional argument
12701 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12702 (Lisp_Object frame, Lisp_Object pixelwise)
12704 int height = 0;
12706 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12707 struct frame *f = decode_any_frame (frame);
12709 if (WINDOWP (f->tool_bar_window)
12710 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12712 update_tool_bar (f, true);
12713 if (f->n_tool_bar_items)
12715 build_desired_tool_bar_string (f);
12716 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12719 #endif
12721 return make_number (height);
12725 /* Display the tool-bar of frame F. Value is true if tool-bar's
12726 height should be changed. */
12727 static bool
12728 redisplay_tool_bar (struct frame *f)
12730 f->tool_bar_redisplayed = true;
12731 #if defined (USE_GTK) || defined (HAVE_NS)
12733 if (FRAME_EXTERNAL_TOOL_BAR (f))
12734 update_frame_tool_bar (f);
12735 return false;
12737 #else /* !USE_GTK && !HAVE_NS */
12739 struct window *w;
12740 struct it it;
12741 struct glyph_row *row;
12743 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12744 do anything. This means you must start with tool-bar-lines
12745 non-zero to get the auto-sizing effect. Or in other words, you
12746 can turn off tool-bars by specifying tool-bar-lines zero. */
12747 if (!WINDOWP (f->tool_bar_window)
12748 || (w = XWINDOW (f->tool_bar_window),
12749 WINDOW_TOTAL_LINES (w) == 0))
12750 return false;
12752 /* Set up an iterator for the tool-bar window. */
12753 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12754 it.first_visible_x = 0;
12755 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12756 row = it.glyph_row;
12757 row->reversed_p = false;
12759 /* Build a string that represents the contents of the tool-bar. */
12760 build_desired_tool_bar_string (f);
12761 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12762 /* FIXME: This should be controlled by a user option. But it
12763 doesn't make sense to have an R2L tool bar if the menu bar cannot
12764 be drawn also R2L, and making the menu bar R2L is tricky due
12765 toolkit-specific code that implements it. If an R2L tool bar is
12766 ever supported, display_tool_bar_line should also be augmented to
12767 call unproduce_glyphs like display_line and display_string
12768 do. */
12769 it.paragraph_embedding = L2R;
12771 if (f->n_tool_bar_rows == 0)
12773 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12775 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12777 x_change_tool_bar_height (f, new_height);
12778 frame_default_tool_bar_height = new_height;
12779 /* Always do that now. */
12780 clear_glyph_matrix (w->desired_matrix);
12781 f->fonts_changed = true;
12782 return true;
12786 /* Display as many lines as needed to display all tool-bar items. */
12788 if (f->n_tool_bar_rows > 0)
12790 int border, rows, height, extra;
12792 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12793 border = XINT (Vtool_bar_border);
12794 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12795 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12796 else if (EQ (Vtool_bar_border, Qborder_width))
12797 border = f->border_width;
12798 else
12799 border = 0;
12800 if (border < 0)
12801 border = 0;
12803 rows = f->n_tool_bar_rows;
12804 height = max (1, (it.last_visible_y - border) / rows);
12805 extra = it.last_visible_y - border - height * rows;
12807 while (it.current_y < it.last_visible_y)
12809 int h = 0;
12810 if (extra > 0 && rows-- > 0)
12812 h = (extra + rows - 1) / rows;
12813 extra -= h;
12815 display_tool_bar_line (&it, height + h);
12818 else
12820 while (it.current_y < it.last_visible_y)
12821 display_tool_bar_line (&it, 0);
12824 /* It doesn't make much sense to try scrolling in the tool-bar
12825 window, so don't do it. */
12826 w->desired_matrix->no_scrolling_p = true;
12827 w->must_be_updated_p = true;
12829 if (!NILP (Vauto_resize_tool_bars))
12831 bool change_height_p = true;
12833 /* If we couldn't display everything, change the tool-bar's
12834 height if there is room for more. */
12835 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12836 change_height_p = true;
12838 /* We subtract 1 because display_tool_bar_line advances the
12839 glyph_row pointer before returning to its caller. We want to
12840 examine the last glyph row produced by
12841 display_tool_bar_line. */
12842 row = it.glyph_row - 1;
12844 /* If there are blank lines at the end, except for a partially
12845 visible blank line at the end that is smaller than
12846 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12847 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12848 && row->height >= FRAME_LINE_HEIGHT (f))
12849 change_height_p = true;
12851 /* If row displays tool-bar items, but is partially visible,
12852 change the tool-bar's height. */
12853 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12854 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12855 change_height_p = true;
12857 /* Resize windows as needed by changing the `tool-bar-lines'
12858 frame parameter. */
12859 if (change_height_p)
12861 int nrows;
12862 int new_height = tool_bar_height (f, &nrows, true);
12864 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12865 && !f->minimize_tool_bar_window_p)
12866 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12867 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12868 f->minimize_tool_bar_window_p = false;
12870 if (change_height_p)
12872 x_change_tool_bar_height (f, new_height);
12873 frame_default_tool_bar_height = new_height;
12874 clear_glyph_matrix (w->desired_matrix);
12875 f->n_tool_bar_rows = nrows;
12876 f->fonts_changed = true;
12878 return true;
12883 f->minimize_tool_bar_window_p = false;
12884 return false;
12886 #endif /* USE_GTK || HAVE_NS */
12889 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12891 /* Get information about the tool-bar item which is displayed in GLYPH
12892 on frame F. Return in *PROP_IDX the index where tool-bar item
12893 properties start in F->tool_bar_items. Value is false if
12894 GLYPH doesn't display a tool-bar item. */
12896 static bool
12897 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12899 Lisp_Object prop;
12900 int charpos;
12902 /* This function can be called asynchronously, which means we must
12903 exclude any possibility that Fget_text_property signals an
12904 error. */
12905 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12906 charpos = max (0, charpos);
12908 /* Get the text property `menu-item' at pos. The value of that
12909 property is the start index of this item's properties in
12910 F->tool_bar_items. */
12911 prop = Fget_text_property (make_number (charpos),
12912 Qmenu_item, f->current_tool_bar_string);
12913 if (! INTEGERP (prop))
12914 return false;
12915 *prop_idx = XINT (prop);
12916 return true;
12920 /* Get information about the tool-bar item at position X/Y on frame F.
12921 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12922 the current matrix of the tool-bar window of F, or NULL if not
12923 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12924 item in F->tool_bar_items. Value is
12926 -1 if X/Y is not on a tool-bar item
12927 0 if X/Y is on the same item that was highlighted before.
12928 1 otherwise. */
12930 static int
12931 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12932 int *hpos, int *vpos, int *prop_idx)
12934 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12935 struct window *w = XWINDOW (f->tool_bar_window);
12936 int area;
12938 /* Find the glyph under X/Y. */
12939 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12940 if (*glyph == NULL)
12941 return -1;
12943 /* Get the start of this tool-bar item's properties in
12944 f->tool_bar_items. */
12945 if (!tool_bar_item_info (f, *glyph, prop_idx))
12946 return -1;
12948 /* Is mouse on the highlighted item? */
12949 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12950 && *vpos >= hlinfo->mouse_face_beg_row
12951 && *vpos <= hlinfo->mouse_face_end_row
12952 && (*vpos > hlinfo->mouse_face_beg_row
12953 || *hpos >= hlinfo->mouse_face_beg_col)
12954 && (*vpos < hlinfo->mouse_face_end_row
12955 || *hpos < hlinfo->mouse_face_end_col
12956 || hlinfo->mouse_face_past_end))
12957 return 0;
12959 return 1;
12963 /* EXPORT:
12964 Handle mouse button event on the tool-bar of frame F, at
12965 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12966 false for button release. MODIFIERS is event modifiers for button
12967 release. */
12969 void
12970 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12971 int modifiers)
12973 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12974 struct window *w = XWINDOW (f->tool_bar_window);
12975 int hpos, vpos, prop_idx;
12976 struct glyph *glyph;
12977 Lisp_Object enabled_p;
12978 int ts;
12980 /* If not on the highlighted tool-bar item, and mouse-highlight is
12981 non-nil, return. This is so we generate the tool-bar button
12982 click only when the mouse button is released on the same item as
12983 where it was pressed. However, when mouse-highlight is disabled,
12984 generate the click when the button is released regardless of the
12985 highlight, since tool-bar items are not highlighted in that
12986 case. */
12987 frame_to_window_pixel_xy (w, &x, &y);
12988 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12989 if (ts == -1
12990 || (ts != 0 && !NILP (Vmouse_highlight)))
12991 return;
12993 /* When mouse-highlight is off, generate the click for the item
12994 where the button was pressed, disregarding where it was
12995 released. */
12996 if (NILP (Vmouse_highlight) && !down_p)
12997 prop_idx = f->last_tool_bar_item;
12999 /* If item is disabled, do nothing. */
13000 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13001 if (NILP (enabled_p))
13002 return;
13004 if (down_p)
13006 /* Show item in pressed state. */
13007 if (!NILP (Vmouse_highlight))
13008 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
13009 f->last_tool_bar_item = prop_idx;
13011 else
13013 Lisp_Object key, frame;
13014 struct input_event event;
13015 EVENT_INIT (event);
13017 /* Show item in released state. */
13018 if (!NILP (Vmouse_highlight))
13019 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
13021 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
13023 XSETFRAME (frame, f);
13024 event.kind = TOOL_BAR_EVENT;
13025 event.frame_or_window = frame;
13026 event.arg = frame;
13027 kbd_buffer_store_event (&event);
13029 event.kind = TOOL_BAR_EVENT;
13030 event.frame_or_window = frame;
13031 event.arg = key;
13032 event.modifiers = modifiers;
13033 kbd_buffer_store_event (&event);
13034 f->last_tool_bar_item = -1;
13039 /* Possibly highlight a tool-bar item on frame F when mouse moves to
13040 tool-bar window-relative coordinates X/Y. Called from
13041 note_mouse_highlight. */
13043 static void
13044 note_tool_bar_highlight (struct frame *f, int x, int y)
13046 Lisp_Object window = f->tool_bar_window;
13047 struct window *w = XWINDOW (window);
13048 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
13049 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
13050 int hpos, vpos;
13051 struct glyph *glyph;
13052 struct glyph_row *row;
13053 int i;
13054 Lisp_Object enabled_p;
13055 int prop_idx;
13056 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
13057 bool mouse_down_p;
13058 int rc;
13060 /* Function note_mouse_highlight is called with negative X/Y
13061 values when mouse moves outside of the frame. */
13062 if (x <= 0 || y <= 0)
13064 clear_mouse_face (hlinfo);
13065 return;
13068 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13069 if (rc < 0)
13071 /* Not on tool-bar item. */
13072 clear_mouse_face (hlinfo);
13073 return;
13075 else if (rc == 0)
13076 /* On same tool-bar item as before. */
13077 goto set_help_echo;
13079 clear_mouse_face (hlinfo);
13081 /* Mouse is down, but on different tool-bar item? */
13082 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13083 && f == dpyinfo->last_mouse_frame);
13085 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13086 return;
13088 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13090 /* If tool-bar item is not enabled, don't highlight it. */
13091 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13092 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13094 /* Compute the x-position of the glyph. In front and past the
13095 image is a space. We include this in the highlighted area. */
13096 row = MATRIX_ROW (w->current_matrix, vpos);
13097 for (i = x = 0; i < hpos; ++i)
13098 x += row->glyphs[TEXT_AREA][i].pixel_width;
13100 /* Record this as the current active region. */
13101 hlinfo->mouse_face_beg_col = hpos;
13102 hlinfo->mouse_face_beg_row = vpos;
13103 hlinfo->mouse_face_beg_x = x;
13104 hlinfo->mouse_face_past_end = false;
13106 hlinfo->mouse_face_end_col = hpos + 1;
13107 hlinfo->mouse_face_end_row = vpos;
13108 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13109 hlinfo->mouse_face_window = window;
13110 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13112 /* Display it as active. */
13113 show_mouse_face (hlinfo, draw);
13116 set_help_echo:
13118 /* Set help_echo_string to a help string to display for this tool-bar item.
13119 XTread_socket does the rest. */
13120 help_echo_object = help_echo_window = Qnil;
13121 help_echo_pos = -1;
13122 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13123 if (NILP (help_echo_string))
13124 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13127 #endif /* !USE_GTK && !HAVE_NS */
13129 #endif /* HAVE_WINDOW_SYSTEM */
13133 /************************************************************************
13134 Horizontal scrolling
13135 ************************************************************************/
13137 /* For all leaf windows in the window tree rooted at WINDOW, set their
13138 hscroll value so that PT is (i) visible in the window, and (ii) so
13139 that it is not within a certain margin at the window's left and
13140 right border. Value is true if any window's hscroll has been
13141 changed. */
13143 static bool
13144 hscroll_window_tree (Lisp_Object window)
13146 bool hscrolled_p = false;
13147 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13148 int hscroll_step_abs = 0;
13149 double hscroll_step_rel = 0;
13151 if (hscroll_relative_p)
13153 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13154 if (hscroll_step_rel < 0)
13156 hscroll_relative_p = false;
13157 hscroll_step_abs = 0;
13160 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13162 hscroll_step_abs = XINT (Vhscroll_step);
13163 if (hscroll_step_abs < 0)
13164 hscroll_step_abs = 0;
13166 else
13167 hscroll_step_abs = 0;
13169 while (WINDOWP (window))
13171 struct window *w = XWINDOW (window);
13173 if (WINDOWP (w->contents))
13174 hscrolled_p |= hscroll_window_tree (w->contents);
13175 else if (w->cursor.vpos >= 0)
13177 int h_margin;
13178 int text_area_width;
13179 struct glyph_row *cursor_row;
13180 struct glyph_row *bottom_row;
13182 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13183 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13184 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13185 else
13186 cursor_row = bottom_row - 1;
13188 if (!cursor_row->enabled_p)
13190 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13191 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13192 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13193 else
13194 cursor_row = bottom_row - 1;
13196 bool row_r2l_p = cursor_row->reversed_p;
13197 bool hscl = hscrolling_current_line_p (w);
13198 int x_offset = 0;
13199 /* When line numbers are displayed, we need to account for
13200 the horizontal space they consume. */
13201 if (!NILP (Vdisplay_line_numbers))
13203 struct glyph *g;
13204 if (!row_r2l_p)
13206 for (g = cursor_row->glyphs[TEXT_AREA];
13207 g < cursor_row->glyphs[TEXT_AREA]
13208 + cursor_row->used[TEXT_AREA];
13209 g++)
13211 if (!(NILP (g->object) && g->charpos < 0))
13212 break;
13213 x_offset += g->pixel_width;
13216 else
13218 for (g = cursor_row->glyphs[TEXT_AREA]
13219 + cursor_row->used[TEXT_AREA];
13220 g > cursor_row->glyphs[TEXT_AREA];
13221 g--)
13223 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13224 break;
13225 x_offset += (g - 1)->pixel_width;
13229 if (cursor_row->truncated_on_left_p)
13231 /* On TTY frames, don't count the left truncation glyph. */
13232 struct frame *f = XFRAME (WINDOW_FRAME (w));
13233 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13236 text_area_width = window_box_width (w, TEXT_AREA);
13238 /* Scroll when cursor is inside this scroll margin. */
13239 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13241 /* If the position of this window's point has explicitly
13242 changed, no more suspend auto hscrolling. */
13243 if (w->suspend_auto_hscroll
13244 && NILP (Fequal (Fwindow_point (window),
13245 Fwindow_old_point (window))))
13247 w->suspend_auto_hscroll = false;
13248 /* When hscrolling just the current line, and the rest
13249 of lines were temporarily hscrolled, but no longer
13250 are, force thorough redisplay of this window, to show
13251 the effect of disabling hscroll suspension immediately. */
13252 if (w->min_hscroll == 0 && w->hscroll > 0
13253 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
13254 Qcurrent_line))
13255 SET_FRAME_GARBAGED (XFRAME (w->frame));
13258 /* Remember window point. */
13259 Fset_marker (w->old_pointm,
13260 ((w == XWINDOW (selected_window))
13261 ? make_number (BUF_PT (XBUFFER (w->contents)))
13262 : Fmarker_position (w->pointm)),
13263 w->contents);
13265 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13266 && !w->suspend_auto_hscroll
13267 /* In some pathological cases, like restoring a window
13268 configuration into a frame that is much smaller than
13269 the one from which the configuration was saved, we
13270 get glyph rows whose start and end have zero buffer
13271 positions, which we cannot handle below. Just skip
13272 such windows. */
13273 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13274 /* For left-to-right rows, hscroll when cursor is either
13275 (i) inside the right hscroll margin, or (ii) if it is
13276 inside the left margin and the window is already
13277 hscrolled. */
13278 && ((!row_r2l_p
13279 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13280 || (cursor_row->enabled_p
13281 && cursor_row->truncated_on_right_p
13282 && (w->cursor.x >= text_area_width - h_margin))))
13283 /* For right-to-left rows, the logic is similar,
13284 except that rules for scrolling to left and right
13285 are reversed. E.g., if cursor.x <= h_margin, we
13286 need to hscroll "to the right" unconditionally,
13287 and that will scroll the screen to the left so as
13288 to reveal the next portion of the row. */
13289 || (row_r2l_p
13290 && ((cursor_row->enabled_p
13291 /* FIXME: It is confusing to set the
13292 truncated_on_right_p flag when R2L rows
13293 are actually truncated on the left. */
13294 && cursor_row->truncated_on_right_p
13295 && w->cursor.x <= h_margin)
13296 || (w->hscroll
13297 && (w->cursor.x >= (text_area_width - h_margin
13298 - x_offset)))))
13299 /* This last condition is needed when moving
13300 vertically from an hscrolled line to a short line
13301 that doesn't need to be hscrolled. If we omit
13302 this condition, the line from which we move will
13303 remain hscrolled. */
13304 || (hscl
13305 && w->hscroll != w->min_hscroll
13306 && !cursor_row->truncated_on_left_p)))
13308 struct it it;
13309 ptrdiff_t hscroll;
13310 struct buffer *saved_current_buffer;
13311 ptrdiff_t pt;
13312 int wanted_x;
13314 /* Find point in a display of infinite width. */
13315 saved_current_buffer = current_buffer;
13316 current_buffer = XBUFFER (w->contents);
13318 if (w == XWINDOW (selected_window))
13319 pt = PT;
13320 else
13321 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13323 /* Move iterator to pt starting at cursor_row->start in
13324 a line with infinite width. */
13325 init_to_row_start (&it, w, cursor_row);
13326 if (hscl)
13327 it.first_visible_x = window_hscroll_limited (w, it.f)
13328 * FRAME_COLUMN_WIDTH (it.f);
13329 it.last_visible_x = DISP_INFINITY;
13330 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13331 /* If the line ends in an overlay string with a newline,
13332 we might infloop, because displaying the window will
13333 want to put the cursor after the overlay, i.e. at X
13334 coordinate of zero on the next screen line. So we
13335 use the buffer position prior to the overlay string
13336 instead. */
13337 if (it.method == GET_FROM_STRING && pt > 1)
13339 init_to_row_start (&it, w, cursor_row);
13340 if (hscl)
13341 it.first_visible_x = (window_hscroll_limited (w, it.f)
13342 * FRAME_COLUMN_WIDTH (it.f));
13343 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13345 current_buffer = saved_current_buffer;
13347 /* Position cursor in window. */
13348 if (!hscroll_relative_p && hscroll_step_abs == 0)
13349 hscroll = max (0, (it.current_x
13350 - (ITERATOR_AT_END_OF_LINE_P (&it)
13351 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13352 : (text_area_width / 2))))
13353 / FRAME_COLUMN_WIDTH (it.f);
13354 else if ((!row_r2l_p
13355 && w->cursor.x >= text_area_width - h_margin)
13356 || (row_r2l_p && w->cursor.x <= h_margin))
13358 if (hscroll_relative_p)
13359 wanted_x = text_area_width * (1 - hscroll_step_rel)
13360 - h_margin;
13361 else
13362 wanted_x = text_area_width
13363 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13364 - h_margin;
13365 hscroll
13366 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13368 else
13370 if (hscroll_relative_p)
13371 wanted_x = text_area_width * hscroll_step_rel
13372 + h_margin;
13373 else
13374 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13375 + h_margin;
13376 hscroll
13377 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13379 hscroll = max (hscroll, w->min_hscroll);
13381 /* Don't prevent redisplay optimizations if hscroll
13382 hasn't changed, as it will unnecessarily slow down
13383 redisplay. */
13384 if (w->hscroll != hscroll
13385 /* When hscrolling only the current line, we need to
13386 report hscroll even if its value is equal to the
13387 previous one, because the new line might need a
13388 different value. */
13389 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13391 struct buffer *b = XBUFFER (w->contents);
13392 b->prevent_redisplay_optimizations_p = true;
13393 w->hscroll = hscroll;
13394 hscrolled_p = true;
13399 window = w->next;
13402 /* Value is true if hscroll of any leaf window has been changed. */
13403 return hscrolled_p;
13407 /* Set hscroll so that cursor is visible and not inside horizontal
13408 scroll margins for all windows in the tree rooted at WINDOW. See
13409 also hscroll_window_tree above. Value is true if any window's
13410 hscroll has been changed. If it has, desired matrices on the frame
13411 of WINDOW are cleared. */
13413 static bool
13414 hscroll_windows (Lisp_Object window)
13416 bool hscrolled_p = hscroll_window_tree (window);
13417 if (hscrolled_p)
13418 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13419 return hscrolled_p;
13424 /************************************************************************
13425 Redisplay
13426 ************************************************************************/
13428 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13429 This is sometimes handy to have in a debugger session. */
13431 #ifdef GLYPH_DEBUG
13433 /* First and last unchanged row for try_window_id. */
13435 static int debug_first_unchanged_at_end_vpos;
13436 static int debug_last_unchanged_at_beg_vpos;
13438 /* Delta vpos and y. */
13440 static int debug_dvpos, debug_dy;
13442 /* Delta in characters and bytes for try_window_id. */
13444 static ptrdiff_t debug_delta, debug_delta_bytes;
13446 /* Values of window_end_pos and window_end_vpos at the end of
13447 try_window_id. */
13449 static ptrdiff_t debug_end_vpos;
13451 /* Append a string to W->desired_matrix->method. FMT is a printf
13452 format string. If trace_redisplay_p is true also printf the
13453 resulting string to stderr. */
13455 static void debug_method_add (struct window *, char const *, ...)
13456 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13458 static void
13459 debug_method_add (struct window *w, char const *fmt, ...)
13461 void *ptr = w;
13462 char *method = w->desired_matrix->method;
13463 int len = strlen (method);
13464 int size = sizeof w->desired_matrix->method;
13465 int remaining = size - len - 1;
13466 va_list ap;
13468 if (len && remaining)
13470 method[len] = '|';
13471 --remaining, ++len;
13474 va_start (ap, fmt);
13475 vsnprintf (method + len, remaining + 1, fmt, ap);
13476 va_end (ap);
13478 if (trace_redisplay_p)
13479 fprintf (stderr, "%p (%s): %s\n",
13480 ptr,
13481 ((BUFFERP (w->contents)
13482 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13483 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13484 : "no buffer"),
13485 method + len);
13488 #endif /* GLYPH_DEBUG */
13491 /* Value is true if all changes in window W, which displays
13492 current_buffer, are in the text between START and END. START is a
13493 buffer position, END is given as a distance from Z. Used in
13494 redisplay_internal for display optimization. */
13496 static bool
13497 text_outside_line_unchanged_p (struct window *w,
13498 ptrdiff_t start, ptrdiff_t end)
13500 bool unchanged_p = true;
13502 /* If text or overlays have changed, see where. */
13503 if (window_outdated (w))
13505 /* Gap in the line? */
13506 if (GPT < start || Z - GPT < end)
13507 unchanged_p = false;
13509 /* Changes start in front of the line, or end after it? */
13510 if (unchanged_p
13511 && (BEG_UNCHANGED < start - 1
13512 || END_UNCHANGED < end))
13513 unchanged_p = false;
13515 /* If selective display, can't optimize if changes start at the
13516 beginning of the line. */
13517 if (unchanged_p
13518 && INTEGERP (BVAR (current_buffer, selective_display))
13519 && XINT (BVAR (current_buffer, selective_display)) > 0
13520 && (BEG_UNCHANGED < start || GPT <= start))
13521 unchanged_p = false;
13523 /* If there are overlays at the start or end of the line, these
13524 may have overlay strings with newlines in them. A change at
13525 START, for instance, may actually concern the display of such
13526 overlay strings as well, and they are displayed on different
13527 lines. So, quickly rule out this case. (For the future, it
13528 might be desirable to implement something more telling than
13529 just BEG/END_UNCHANGED.) */
13530 if (unchanged_p)
13532 if (BEG + BEG_UNCHANGED == start
13533 && overlay_touches_p (start))
13534 unchanged_p = false;
13535 if (END_UNCHANGED == end
13536 && overlay_touches_p (Z - end))
13537 unchanged_p = false;
13540 /* Under bidi reordering, adding or deleting a character in the
13541 beginning of a paragraph, before the first strong directional
13542 character, can change the base direction of the paragraph (unless
13543 the buffer specifies a fixed paragraph direction), which will
13544 require redisplaying the whole paragraph. It might be worthwhile
13545 to find the paragraph limits and widen the range of redisplayed
13546 lines to that, but for now just give up this optimization. */
13547 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13548 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13549 unchanged_p = false;
13552 return unchanged_p;
13556 /* Do a frame update, taking possible shortcuts into account. This is
13557 the main external entry point for redisplay.
13559 If the last redisplay displayed an echo area message and that message
13560 is no longer requested, we clear the echo area or bring back the
13561 mini-buffer if that is in use. */
13563 void
13564 redisplay (void)
13566 redisplay_internal ();
13570 static Lisp_Object
13571 overlay_arrow_string_or_property (Lisp_Object var)
13573 Lisp_Object val;
13575 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13576 return val;
13578 return Voverlay_arrow_string;
13581 /* Return true if there are any overlay-arrows in current_buffer. */
13582 static bool
13583 overlay_arrow_in_current_buffer_p (void)
13585 Lisp_Object vlist;
13587 for (vlist = Voverlay_arrow_variable_list;
13588 CONSP (vlist);
13589 vlist = XCDR (vlist))
13591 Lisp_Object var = XCAR (vlist);
13592 Lisp_Object val;
13594 if (!SYMBOLP (var))
13595 continue;
13596 val = find_symbol_value (var);
13597 if (MARKERP (val)
13598 && current_buffer == XMARKER (val)->buffer)
13599 return true;
13601 return false;
13605 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13606 has changed.
13607 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13608 buffers that are affected. */
13610 static bool
13611 overlay_arrows_changed_p (bool set_redisplay)
13613 Lisp_Object vlist;
13614 bool changed = false;
13616 for (vlist = Voverlay_arrow_variable_list;
13617 CONSP (vlist);
13618 vlist = XCDR (vlist))
13620 Lisp_Object var = XCAR (vlist);
13621 Lisp_Object val, pstr;
13623 if (!SYMBOLP (var))
13624 continue;
13625 val = find_symbol_value (var);
13626 if (!MARKERP (val))
13627 continue;
13628 if (! EQ (COERCE_MARKER (val),
13629 /* FIXME: Don't we have a problem, using such a global
13630 * "last-position" if the variable is buffer-local? */
13631 Fget (var, Qlast_arrow_position))
13632 || ! (pstr = overlay_arrow_string_or_property (var),
13633 EQ (pstr, Fget (var, Qlast_arrow_string))))
13635 struct buffer *buf = XMARKER (val)->buffer;
13637 if (set_redisplay)
13639 if (buf)
13640 bset_redisplay (buf);
13641 changed = true;
13643 else
13644 return true;
13647 return changed;
13650 /* Mark overlay arrows to be updated on next redisplay. */
13652 static void
13653 update_overlay_arrows (int up_to_date)
13655 Lisp_Object vlist;
13657 for (vlist = Voverlay_arrow_variable_list;
13658 CONSP (vlist);
13659 vlist = XCDR (vlist))
13661 Lisp_Object var = XCAR (vlist);
13663 if (!SYMBOLP (var))
13664 continue;
13666 if (up_to_date > 0)
13668 Lisp_Object val = find_symbol_value (var);
13669 if (!MARKERP (val))
13670 continue;
13671 Fput (var, Qlast_arrow_position,
13672 COERCE_MARKER (val));
13673 Fput (var, Qlast_arrow_string,
13674 overlay_arrow_string_or_property (var));
13676 else if (up_to_date < 0
13677 || !NILP (Fget (var, Qlast_arrow_position)))
13679 Fput (var, Qlast_arrow_position, Qt);
13680 Fput (var, Qlast_arrow_string, Qt);
13686 /* Return overlay arrow string to display at row.
13687 Return integer (bitmap number) for arrow bitmap in left fringe.
13688 Return nil if no overlay arrow. */
13690 static Lisp_Object
13691 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13693 Lisp_Object vlist;
13695 for (vlist = Voverlay_arrow_variable_list;
13696 CONSP (vlist);
13697 vlist = XCDR (vlist))
13699 Lisp_Object var = XCAR (vlist);
13700 Lisp_Object val;
13702 if (!SYMBOLP (var))
13703 continue;
13705 val = find_symbol_value (var);
13707 if (MARKERP (val)
13708 && current_buffer == XMARKER (val)->buffer
13709 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13711 if (FRAME_WINDOW_P (it->f)
13712 /* FIXME: if ROW->reversed_p is set, this should test
13713 the right fringe, not the left one. */
13714 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13716 #ifdef HAVE_WINDOW_SYSTEM
13717 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13719 int fringe_bitmap = lookup_fringe_bitmap (val);
13720 if (fringe_bitmap != 0)
13721 return make_number (fringe_bitmap);
13723 #endif
13724 return make_number (-1); /* Use default arrow bitmap. */
13726 return overlay_arrow_string_or_property (var);
13730 return Qnil;
13733 /* Return true if point moved out of or into a composition. Otherwise
13734 return false. PREV_BUF and PREV_PT are the last point buffer and
13735 position. BUF and PT are the current point buffer and position. */
13737 static bool
13738 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13739 struct buffer *buf, ptrdiff_t pt)
13741 ptrdiff_t start, end;
13742 Lisp_Object prop;
13743 Lisp_Object buffer;
13745 XSETBUFFER (buffer, buf);
13746 /* Check a composition at the last point if point moved within the
13747 same buffer. */
13748 if (prev_buf == buf)
13750 if (prev_pt == pt)
13751 /* Point didn't move. */
13752 return false;
13754 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13755 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13756 && composition_valid_p (start, end, prop)
13757 && start < prev_pt && end > prev_pt)
13758 /* The last point was within the composition. Return true iff
13759 point moved out of the composition. */
13760 return (pt <= start || pt >= end);
13763 /* Check a composition at the current point. */
13764 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13765 && find_composition (pt, -1, &start, &end, &prop, buffer)
13766 && composition_valid_p (start, end, prop)
13767 && start < pt && end > pt);
13770 /* Reconsider the clip changes of buffer which is displayed in W. */
13772 static void
13773 reconsider_clip_changes (struct window *w)
13775 struct buffer *b = XBUFFER (w->contents);
13777 if (b->clip_changed
13778 && w->window_end_valid
13779 && w->current_matrix->buffer == b
13780 && w->current_matrix->zv == BUF_ZV (b)
13781 && w->current_matrix->begv == BUF_BEGV (b))
13782 b->clip_changed = false;
13784 /* If display wasn't paused, and W is not a tool bar window, see if
13785 point has been moved into or out of a composition. In that case,
13786 set b->clip_changed to force updating the screen. If
13787 b->clip_changed has already been set, skip this check. */
13788 if (!b->clip_changed && w->window_end_valid)
13790 ptrdiff_t pt = (w == XWINDOW (selected_window)
13791 ? PT : marker_position (w->pointm));
13793 if ((w->current_matrix->buffer != b || pt != w->last_point)
13794 && check_point_in_composition (w->current_matrix->buffer,
13795 w->last_point, b, pt))
13796 b->clip_changed = true;
13800 static void
13801 propagate_buffer_redisplay (void)
13802 { /* Resetting b->text->redisplay is problematic!
13803 We can't just reset it in the case that some window that displays
13804 it has not been redisplayed; and such a window can stay
13805 unredisplayed for a long time if it's currently invisible.
13806 But we do want to reset it at the end of redisplay otherwise
13807 its displayed windows will keep being redisplayed over and over
13808 again.
13809 So we copy all b->text->redisplay flags up to their windows here,
13810 such that mark_window_display_accurate can safely reset
13811 b->text->redisplay. */
13812 Lisp_Object ws = window_list ();
13813 for (; CONSP (ws); ws = XCDR (ws))
13815 struct window *thisw = XWINDOW (XCAR (ws));
13816 struct buffer *thisb = XBUFFER (thisw->contents);
13817 if (thisb->text->redisplay)
13818 thisw->redisplay = true;
13822 #define STOP_POLLING \
13823 do { if (! polling_stopped_here) stop_polling (); \
13824 polling_stopped_here = true; } while (false)
13826 #define RESUME_POLLING \
13827 do { if (polling_stopped_here) start_polling (); \
13828 polling_stopped_here = false; } while (false)
13831 /* Perhaps in the future avoid recentering windows if it
13832 is not necessary; currently that causes some problems. */
13834 static void
13835 redisplay_internal (void)
13837 struct window *w = XWINDOW (selected_window);
13838 struct window *sw;
13839 struct frame *fr;
13840 bool pending;
13841 bool must_finish = false, match_p;
13842 struct text_pos tlbufpos, tlendpos;
13843 int number_of_visible_frames;
13844 ptrdiff_t count;
13845 struct frame *sf;
13846 bool polling_stopped_here = false;
13847 Lisp_Object tail, frame;
13849 /* Set a limit to the number of retries we perform due to horizontal
13850 scrolling, this avoids getting stuck in an uninterruptible
13851 infinite loop (Bug #24633). */
13852 enum { MAX_HSCROLL_RETRIES = 16 };
13853 int hscroll_retries = 0;
13855 /* Limit the number of retries for when frame(s) become garbaged as
13856 result of redisplaying them. Some packages set various redisplay
13857 hooks, such as window-scroll-functions, to run Lisp that always
13858 calls APIs which cause the frame's garbaged flag to become set,
13859 so we loop indefinitely. */
13860 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13861 int garbaged_frame_retries = 0;
13863 /* True means redisplay has to consider all windows on all
13864 frames. False, only selected_window is considered. */
13865 bool consider_all_windows_p;
13867 /* True means redisplay has to redisplay the miniwindow. */
13868 bool update_miniwindow_p = false;
13870 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13872 /* No redisplay if running in batch mode or frame is not yet fully
13873 initialized, or redisplay is explicitly turned off by setting
13874 Vinhibit_redisplay. */
13875 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13876 || !NILP (Vinhibit_redisplay))
13877 return;
13879 /* Don't examine these until after testing Vinhibit_redisplay.
13880 When Emacs is shutting down, perhaps because its connection to
13881 X has dropped, we should not look at them at all. */
13882 fr = XFRAME (w->frame);
13883 sf = SELECTED_FRAME ();
13885 if (!fr->glyphs_initialized_p)
13886 return;
13888 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13889 if (popup_activated ())
13890 return;
13891 #endif
13893 /* I don't think this happens but let's be paranoid. */
13894 if (redisplaying_p)
13895 return;
13897 /* Record a function that clears redisplaying_p
13898 when we leave this function. */
13899 count = SPECPDL_INDEX ();
13900 record_unwind_protect_void (unwind_redisplay);
13901 redisplaying_p = true;
13902 block_buffer_flips ();
13903 specbind (Qinhibit_free_realized_faces, Qnil);
13905 /* Record this function, so it appears on the profiler's backtraces. */
13906 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13908 FOR_EACH_FRAME (tail, frame)
13909 XFRAME (frame)->already_hscrolled_p = false;
13911 retry:
13912 /* Remember the currently selected window. */
13913 sw = w;
13915 pending = false;
13916 forget_escape_and_glyphless_faces ();
13918 inhibit_free_realized_faces = false;
13920 /* If face_change, init_iterator will free all realized faces, which
13921 includes the faces referenced from current matrices. So, we
13922 can't reuse current matrices in this case. */
13923 if (face_change)
13924 windows_or_buffers_changed = 47;
13926 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13927 && FRAME_TTY (sf)->previous_frame != sf)
13929 /* Since frames on a single ASCII terminal share the same
13930 display area, displaying a different frame means redisplay
13931 the whole thing. */
13932 SET_FRAME_GARBAGED (sf);
13933 #ifndef DOS_NT
13934 set_tty_color_mode (FRAME_TTY (sf), sf);
13935 #endif
13936 FRAME_TTY (sf)->previous_frame = sf;
13939 /* Set the visible flags for all frames. Do this before checking for
13940 resized or garbaged frames; they want to know if their frames are
13941 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13942 number_of_visible_frames = 0;
13944 FOR_EACH_FRAME (tail, frame)
13946 struct frame *f = XFRAME (frame);
13948 if (FRAME_VISIBLE_P (f))
13950 ++number_of_visible_frames;
13951 /* Adjust matrices for visible frames only. */
13952 if (f->fonts_changed)
13954 adjust_frame_glyphs (f);
13955 /* Disable all redisplay optimizations for this frame.
13956 This is because adjust_frame_glyphs resets the
13957 enabled_p flag for all glyph rows of all windows, so
13958 many optimizations will fail anyway, and some might
13959 fail to test that flag and do bogus things as
13960 result. */
13961 SET_FRAME_GARBAGED (f);
13962 f->fonts_changed = false;
13964 /* If cursor type has been changed on the frame
13965 other than selected, consider all frames. */
13966 if (f != sf && f->cursor_type_changed)
13967 fset_redisplay (f);
13969 clear_desired_matrices (f);
13972 /* Notice any pending interrupt request to change frame size. */
13973 do_pending_window_change (true);
13975 /* do_pending_window_change could change the selected_window due to
13976 frame resizing which makes the selected window too small. */
13977 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13978 sw = w;
13980 /* Clear frames marked as garbaged. */
13981 clear_garbaged_frames ();
13983 /* Build menubar and tool-bar items. */
13984 if (NILP (Vmemory_full))
13985 prepare_menu_bars ();
13987 reconsider_clip_changes (w);
13989 /* In most cases selected window displays current buffer. */
13990 match_p = XBUFFER (w->contents) == current_buffer;
13991 if (match_p)
13993 /* Detect case that we need to write or remove a star in the mode line. */
13994 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13995 w->update_mode_line = true;
13997 if (mode_line_update_needed (w))
13998 w->update_mode_line = true;
14000 /* If reconsider_clip_changes above decided that the narrowing
14001 in the current buffer changed, make sure all other windows
14002 showing that buffer will be redisplayed. */
14003 if (current_buffer->clip_changed)
14004 bset_update_mode_line (current_buffer);
14007 /* Normally the message* functions will have already displayed and
14008 updated the echo area, but the frame may have been trashed, or
14009 the update may have been preempted, so display the echo area
14010 again here. Checking message_cleared_p captures the case that
14011 the echo area should be cleared. */
14012 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
14013 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
14014 || (message_cleared_p
14015 && minibuf_level == 0
14016 /* If the mini-window is currently selected, this means the
14017 echo-area doesn't show through. */
14018 && !MINI_WINDOW_P (XWINDOW (selected_window))))
14020 echo_area_display (false);
14022 /* If echo_area_display resizes the mini-window, the redisplay and
14023 window_sizes_changed flags of the selected frame are set, but
14024 it's too late for the hooks in window-size-change-functions,
14025 which have been examined already in prepare_menu_bars. So in
14026 that case we call the hooks here only for the selected frame. */
14027 if (sf->redisplay)
14029 ptrdiff_t count1 = SPECPDL_INDEX ();
14031 record_unwind_save_match_data ();
14032 run_window_size_change_functions (selected_frame);
14033 unbind_to (count1, Qnil);
14036 if (message_cleared_p)
14037 update_miniwindow_p = true;
14039 must_finish = true;
14041 /* If we don't display the current message, don't clear the
14042 message_cleared_p flag, because, if we did, we wouldn't clear
14043 the echo area in the next redisplay which doesn't preserve
14044 the echo area. */
14045 if (!display_last_displayed_message_p)
14046 message_cleared_p = false;
14048 else if (EQ (selected_window, minibuf_window)
14049 && (current_buffer->clip_changed || window_outdated (w))
14050 && resize_mini_window (w, false))
14052 if (sf->redisplay)
14054 ptrdiff_t count1 = SPECPDL_INDEX ();
14056 record_unwind_save_match_data ();
14057 run_window_size_change_functions (selected_frame);
14058 unbind_to (count1, Qnil);
14061 /* Resized active mini-window to fit the size of what it is
14062 showing if its contents might have changed. */
14063 must_finish = true;
14065 /* If window configuration was changed, frames may have been
14066 marked garbaged. Clear them or we will experience
14067 surprises wrt scrolling. */
14068 clear_garbaged_frames ();
14071 if (windows_or_buffers_changed && !update_mode_lines)
14072 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14073 only the windows's contents needs to be refreshed, or whether the
14074 mode-lines also need a refresh. */
14075 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14076 ? REDISPLAY_SOME : 32);
14078 /* If specs for an arrow have changed, do thorough redisplay
14079 to ensure we remove any arrow that should no longer exist. */
14080 /* Apparently, this is the only case where we update other windows,
14081 without updating other mode-lines. */
14082 overlay_arrows_changed_p (true);
14084 consider_all_windows_p = (update_mode_lines
14085 || windows_or_buffers_changed);
14087 #define AINC(a,i) \
14089 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14090 if (INTEGERP (entry)) \
14091 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14094 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14095 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14097 /* Optimize the case that only the line containing the cursor in the
14098 selected window has changed. Variables starting with this_ are
14099 set in display_line and record information about the line
14100 containing the cursor. */
14101 tlbufpos = this_line_start_pos;
14102 tlendpos = this_line_end_pos;
14103 if (!consider_all_windows_p
14104 && CHARPOS (tlbufpos) > 0
14105 && !w->update_mode_line
14106 && !current_buffer->clip_changed
14107 && !current_buffer->prevent_redisplay_optimizations_p
14108 && FRAME_VISIBLE_P (XFRAME (w->frame))
14109 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14110 && !XFRAME (w->frame)->cursor_type_changed
14111 && !XFRAME (w->frame)->face_change
14112 /* Make sure recorded data applies to current buffer, etc. */
14113 && this_line_buffer == current_buffer
14114 && match_p
14115 && !w->force_start
14116 && !w->optional_new_start
14117 /* Point must be on the line that we have info recorded about. */
14118 && PT >= CHARPOS (tlbufpos)
14119 && PT <= Z - CHARPOS (tlendpos)
14120 /* All text outside that line, including its final newline,
14121 must be unchanged. */
14122 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14123 CHARPOS (tlendpos)))
14125 if (CHARPOS (tlbufpos) > BEGV
14126 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14127 && (CHARPOS (tlbufpos) == ZV
14128 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14129 /* Former continuation line has disappeared by becoming empty. */
14130 goto cancel;
14131 else if (window_outdated (w) || MINI_WINDOW_P (w))
14133 /* We have to handle the case of continuation around a
14134 wide-column character (see the comment in indent.c around
14135 line 1340).
14137 For instance, in the following case:
14139 -------- Insert --------
14140 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14141 J_I_ ==> J_I_ `^^' are cursors.
14142 ^^ ^^
14143 -------- --------
14145 As we have to redraw the line above, we cannot use this
14146 optimization. */
14148 struct it it;
14149 int line_height_before = this_line_pixel_height;
14151 /* Note that start_display will handle the case that the
14152 line starting at tlbufpos is a continuation line. */
14153 start_display (&it, w, tlbufpos);
14155 /* Implementation note: It this still necessary? */
14156 if (it.current_x != this_line_start_x)
14157 goto cancel;
14159 TRACE ((stderr, "trying display optimization 1\n"));
14160 w->cursor.vpos = -1;
14161 overlay_arrow_seen = false;
14162 it.vpos = this_line_vpos;
14163 it.current_y = this_line_y;
14164 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14165 display_line (&it, -1);
14167 /* If line contains point, is not continued,
14168 and ends at same distance from eob as before, we win. */
14169 if (w->cursor.vpos >= 0
14170 /* Line is not continued, otherwise this_line_start_pos
14171 would have been set to 0 in display_line. */
14172 && CHARPOS (this_line_start_pos)
14173 /* Line ends as before. */
14174 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14175 /* Line has same height as before. Otherwise other lines
14176 would have to be shifted up or down. */
14177 && this_line_pixel_height == line_height_before)
14179 /* If this is not the window's last line, we must adjust
14180 the charstarts of the lines below. */
14181 if (it.current_y < it.last_visible_y)
14183 struct glyph_row *row
14184 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14185 ptrdiff_t delta, delta_bytes;
14187 /* We used to distinguish between two cases here,
14188 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14189 when the line ends in a newline or the end of the
14190 buffer's accessible portion. But both cases did
14191 the same, so they were collapsed. */
14192 delta = (Z
14193 - CHARPOS (tlendpos)
14194 - MATRIX_ROW_START_CHARPOS (row));
14195 delta_bytes = (Z_BYTE
14196 - BYTEPOS (tlendpos)
14197 - MATRIX_ROW_START_BYTEPOS (row));
14199 increment_matrix_positions (w->current_matrix,
14200 this_line_vpos + 1,
14201 w->current_matrix->nrows,
14202 delta, delta_bytes);
14205 /* If this row displays text now but previously didn't,
14206 or vice versa, w->window_end_vpos may have to be
14207 adjusted. */
14208 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14210 if (w->window_end_vpos < this_line_vpos)
14211 w->window_end_vpos = this_line_vpos;
14213 else if (w->window_end_vpos == this_line_vpos
14214 && this_line_vpos > 0)
14215 w->window_end_vpos = this_line_vpos - 1;
14216 w->window_end_valid = false;
14218 /* Update hint: No need to try to scroll in update_window. */
14219 w->desired_matrix->no_scrolling_p = true;
14221 #ifdef GLYPH_DEBUG
14222 *w->desired_matrix->method = 0;
14223 debug_method_add (w, "optimization 1");
14224 #endif
14225 #ifdef HAVE_WINDOW_SYSTEM
14226 update_window_fringes (w, false);
14227 #endif
14228 goto update;
14230 else
14231 goto cancel;
14233 else if (/* Cursor position hasn't changed. */
14234 PT == w->last_point
14235 /* Make sure the cursor was last displayed
14236 in this window. Otherwise we have to reposition it. */
14238 /* PXW: Must be converted to pixels, probably. */
14239 && 0 <= w->cursor.vpos
14240 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14242 if (!must_finish)
14244 do_pending_window_change (true);
14245 /* If selected_window changed, redisplay again. */
14246 if (WINDOWP (selected_window)
14247 && (w = XWINDOW (selected_window)) != sw)
14248 goto retry;
14250 /* We used to always goto end_of_redisplay here, but this
14251 isn't enough if we have a blinking cursor. */
14252 if (w->cursor_off_p == w->last_cursor_off_p)
14253 goto end_of_redisplay;
14255 goto update;
14257 /* If highlighting the region, or if the cursor is in the echo area,
14258 then we can't just move the cursor. */
14259 else if (NILP (Vshow_trailing_whitespace)
14260 && !cursor_in_echo_area)
14262 struct it it;
14263 struct glyph_row *row;
14265 /* Skip from tlbufpos to PT and see where it is. Note that
14266 PT may be in invisible text. If so, we will end at the
14267 next visible position. */
14268 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14269 NULL, DEFAULT_FACE_ID);
14270 it.current_x = this_line_start_x;
14271 it.current_y = this_line_y;
14272 it.vpos = this_line_vpos;
14274 /* The call to move_it_to stops in front of PT, but
14275 moves over before-strings. */
14276 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14278 if (it.vpos == this_line_vpos
14279 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14280 row->enabled_p))
14282 eassert (this_line_vpos == it.vpos);
14283 eassert (this_line_y == it.current_y);
14284 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14285 if (cursor_row_fully_visible_p (w, false, true))
14287 #ifdef GLYPH_DEBUG
14288 *w->desired_matrix->method = 0;
14289 debug_method_add (w, "optimization 3");
14290 #endif
14291 goto update;
14293 else
14294 goto cancel;
14296 else
14297 goto cancel;
14300 cancel:
14301 /* Text changed drastically or point moved off of line. */
14302 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14305 CHARPOS (this_line_start_pos) = 0;
14306 ++clear_face_cache_count;
14307 #ifdef HAVE_WINDOW_SYSTEM
14308 ++clear_image_cache_count;
14309 #endif
14311 /* Build desired matrices, and update the display. If
14312 consider_all_windows_p, do it for all windows on all frames that
14313 require redisplay, as specified by their 'redisplay' flag.
14314 Otherwise do it for selected_window, only. */
14316 if (consider_all_windows_p)
14318 FOR_EACH_FRAME (tail, frame)
14319 XFRAME (frame)->updated_p = false;
14321 propagate_buffer_redisplay ();
14323 FOR_EACH_FRAME (tail, frame)
14325 struct frame *f = XFRAME (frame);
14327 /* We don't have to do anything for unselected terminal
14328 frames. */
14329 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14330 && !EQ (FRAME_TTY (f)->top_frame, frame))
14331 continue;
14333 retry_frame:
14334 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14336 bool gcscrollbars
14337 /* Only GC scrollbars when we redisplay the whole frame. */
14338 = f->redisplay || !REDISPLAY_SOME_P ();
14339 bool f_redisplay_flag = f->redisplay;
14340 /* Mark all the scroll bars to be removed; we'll redeem
14341 the ones we want when we redisplay their windows. */
14342 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14343 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14345 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14346 redisplay_windows (FRAME_ROOT_WINDOW (f));
14347 /* Remember that the invisible frames need to be redisplayed next
14348 time they're visible. */
14349 else if (!REDISPLAY_SOME_P ())
14350 f->redisplay = true;
14352 /* The X error handler may have deleted that frame. */
14353 if (!FRAME_LIVE_P (f))
14354 continue;
14356 /* Any scroll bars which redisplay_windows should have
14357 nuked should now go away. */
14358 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14359 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14361 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14363 /* If fonts changed on visible frame, display again. */
14364 if (f->fonts_changed)
14366 adjust_frame_glyphs (f);
14367 /* Disable all redisplay optimizations for this
14368 frame. For the reasons, see the comment near
14369 the previous call to adjust_frame_glyphs above. */
14370 SET_FRAME_GARBAGED (f);
14371 f->fonts_changed = false;
14372 goto retry_frame;
14375 /* See if we have to hscroll. */
14376 if (!f->already_hscrolled_p)
14378 f->already_hscrolled_p = true;
14379 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14380 && hscroll_windows (f->root_window))
14382 hscroll_retries++;
14383 goto retry_frame;
14387 /* If the frame's redisplay flag was not set before
14388 we went about redisplaying its windows, but it is
14389 set now, that means we employed some redisplay
14390 optimizations inside redisplay_windows, and
14391 bypassed producing some screen lines. But if
14392 f->redisplay is now set, it might mean the old
14393 faces are no longer valid (e.g., if redisplaying
14394 some window called some Lisp which defined a new
14395 face or redefined an existing face), so trying to
14396 use them in update_frame will segfault.
14397 Therefore, we must redisplay this frame. */
14398 if (!f_redisplay_flag && f->redisplay)
14399 goto retry_frame;
14400 /* In some case (e.g., window resize), we notice
14401 only during window updating that the window
14402 content changed unpredictably (e.g., a GTK
14403 scrollbar moved, or some Lisp hook that winds up
14404 calling adjust_frame_glyphs) and that our
14405 previous estimation of the frame content was
14406 garbage. We have to start over. These cases
14407 should be rare, so going all the way back to the
14408 top of redisplay should be good enough. */
14409 if (FRAME_GARBAGED_P (f)
14410 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14411 goto retry;
14413 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14414 x_clear_under_internal_border (f);
14415 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14417 /* Prevent various kinds of signals during display
14418 update. stdio is not robust about handling
14419 signals, which can cause an apparent I/O error. */
14420 if (interrupt_input)
14421 unrequest_sigio ();
14422 STOP_POLLING;
14424 pending |= update_frame (f, false, false);
14425 f->cursor_type_changed = false;
14426 f->updated_p = true;
14431 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14433 if (!pending)
14435 /* Do the mark_window_display_accurate after all windows have
14436 been redisplayed because this call resets flags in buffers
14437 which are needed for proper redisplay. */
14438 FOR_EACH_FRAME (tail, frame)
14440 struct frame *f = XFRAME (frame);
14441 if (f->updated_p)
14443 f->redisplay = false;
14444 f->garbaged = false;
14445 mark_window_display_accurate (f->root_window, true);
14446 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14447 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14452 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14454 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14455 /* Use list_of_error, not Qerror, so that
14456 we catch only errors and don't run the debugger. */
14457 internal_condition_case_1 (redisplay_window_1, selected_window,
14458 list_of_error,
14459 redisplay_window_error);
14460 if (update_miniwindow_p)
14461 internal_condition_case_1 (redisplay_window_1,
14462 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14463 redisplay_window_error);
14465 /* Compare desired and current matrices, perform output. */
14467 update:
14468 /* If fonts changed, display again. Likewise if redisplay_window_1
14469 above caused some change (e.g., a change in faces) that requires
14470 considering the entire frame again. */
14471 if (sf->fonts_changed || sf->redisplay)
14473 if (sf->redisplay)
14475 /* Set this to force a more thorough redisplay.
14476 Otherwise, we might immediately loop back to the
14477 above "else-if" clause (since all the conditions that
14478 led here might still be true), and we will then
14479 infloop, because the selected-frame's redisplay flag
14480 is not (and cannot be) reset. */
14481 windows_or_buffers_changed = 50;
14483 goto retry;
14486 /* Prevent freeing of realized faces, since desired matrices are
14487 pending that reference the faces we computed and cached. */
14488 inhibit_free_realized_faces = true;
14490 /* Prevent various kinds of signals during display update.
14491 stdio is not robust about handling signals,
14492 which can cause an apparent I/O error. */
14493 if (interrupt_input)
14494 unrequest_sigio ();
14495 STOP_POLLING;
14497 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14499 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14500 && hscroll_windows (selected_window))
14502 hscroll_retries++;
14503 goto retry;
14506 XWINDOW (selected_window)->must_be_updated_p = true;
14507 pending = update_frame (sf, false, false);
14508 sf->cursor_type_changed = false;
14511 /* We may have called echo_area_display at the top of this
14512 function. If the echo area is on another frame, that may
14513 have put text on a frame other than the selected one, so the
14514 above call to update_frame would not have caught it. Catch
14515 it here. */
14516 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14517 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14519 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14521 XWINDOW (mini_window)->must_be_updated_p = true;
14522 pending |= update_frame (mini_frame, false, false);
14523 mini_frame->cursor_type_changed = false;
14524 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14525 && hscroll_windows (mini_window))
14527 hscroll_retries++;
14528 goto retry;
14533 /* If display was paused because of pending input, make sure we do a
14534 thorough update the next time. */
14535 if (pending)
14537 /* Prevent the optimization at the beginning of
14538 redisplay_internal that tries a single-line update of the
14539 line containing the cursor in the selected window. */
14540 CHARPOS (this_line_start_pos) = 0;
14542 /* Let the overlay arrow be updated the next time. */
14543 update_overlay_arrows (0);
14545 /* If we pause after scrolling, some rows in the current
14546 matrices of some windows are not valid. */
14547 if (!WINDOW_FULL_WIDTH_P (w)
14548 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14549 update_mode_lines = 36;
14551 else
14553 if (!consider_all_windows_p)
14555 /* This has already been done above if
14556 consider_all_windows_p is set. */
14557 if (XBUFFER (w->contents)->text->redisplay
14558 && buffer_window_count (XBUFFER (w->contents)) > 1)
14559 /* This can happen if b->text->redisplay was set during
14560 jit-lock. */
14561 propagate_buffer_redisplay ();
14562 mark_window_display_accurate_1 (w, true);
14564 /* Say overlay arrows are up to date. */
14565 update_overlay_arrows (1);
14567 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14568 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14571 update_mode_lines = 0;
14572 windows_or_buffers_changed = 0;
14575 /* Start SIGIO interrupts coming again. Having them off during the
14576 code above makes it less likely one will discard output, but not
14577 impossible, since there might be stuff in the system buffer here.
14578 But it is much hairier to try to do anything about that. */
14579 if (interrupt_input)
14580 request_sigio ();
14581 RESUME_POLLING;
14583 /* If a frame has become visible which was not before, redisplay
14584 again, so that we display it. Expose events for such a frame
14585 (which it gets when becoming visible) don't call the parts of
14586 redisplay constructing glyphs, so simply exposing a frame won't
14587 display anything in this case. So, we have to display these
14588 frames here explicitly. */
14589 if (!pending)
14591 int new_count = 0;
14593 FOR_EACH_FRAME (tail, frame)
14595 if (XFRAME (frame)->visible)
14596 new_count++;
14599 if (new_count != number_of_visible_frames)
14600 windows_or_buffers_changed = 52;
14603 /* Change frame size now if a change is pending. */
14604 do_pending_window_change (true);
14606 /* If we just did a pending size change, or have additional
14607 visible frames, or selected_window changed, redisplay again. */
14608 if ((windows_or_buffers_changed && !pending)
14609 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14610 goto retry;
14612 /* Clear the face and image caches.
14614 We used to do this only if consider_all_windows_p. But the cache
14615 needs to be cleared if a timer creates images in the current
14616 buffer (e.g. the test case in Bug#6230). */
14618 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14620 clear_face_cache (false);
14621 clear_face_cache_count = 0;
14624 #ifdef HAVE_WINDOW_SYSTEM
14625 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14627 clear_image_caches (Qnil);
14628 clear_image_cache_count = 0;
14630 #endif /* HAVE_WINDOW_SYSTEM */
14632 end_of_redisplay:
14633 #ifdef HAVE_NS
14634 ns_set_doc_edited ();
14635 #endif
14636 if (interrupt_input && interrupts_deferred)
14637 request_sigio ();
14639 unbind_to (count, Qnil);
14640 RESUME_POLLING;
14643 static void
14644 unwind_redisplay_preserve_echo_area (void)
14646 unblock_buffer_flips ();
14649 /* Redisplay, but leave alone any recent echo area message unless
14650 another message has been requested in its place.
14652 This is useful in situations where you need to redisplay but no
14653 user action has occurred, making it inappropriate for the message
14654 area to be cleared. See tracking_off and
14655 wait_reading_process_output for examples of these situations.
14657 FROM_WHERE is an integer saying from where this function was
14658 called. This is useful for debugging. */
14660 void
14661 redisplay_preserve_echo_area (int from_where)
14663 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14665 block_input ();
14666 ptrdiff_t count = SPECPDL_INDEX ();
14667 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14668 block_buffer_flips ();
14669 unblock_input ();
14671 if (!NILP (echo_area_buffer[1]))
14673 /* We have a previously displayed message, but no current
14674 message. Redisplay the previous message. */
14675 display_last_displayed_message_p = true;
14676 redisplay_internal ();
14677 display_last_displayed_message_p = false;
14679 else
14680 redisplay_internal ();
14682 flush_frame (SELECTED_FRAME ());
14683 unbind_to (count, Qnil);
14687 /* Function registered with record_unwind_protect in redisplay_internal. */
14689 static void
14690 unwind_redisplay (void)
14692 redisplaying_p = false;
14693 unblock_buffer_flips ();
14697 /* Mark the display of leaf window W as accurate or inaccurate.
14698 If ACCURATE_P, mark display of W as accurate.
14699 If !ACCURATE_P, arrange for W to be redisplayed the next
14700 time redisplay_internal is called. */
14702 static void
14703 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14705 struct buffer *b = XBUFFER (w->contents);
14707 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14708 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14709 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14711 if (accurate_p)
14713 b->clip_changed = false;
14714 b->prevent_redisplay_optimizations_p = false;
14715 eassert (buffer_window_count (b) > 0);
14716 /* Resetting b->text->redisplay is problematic!
14717 In order to make it safer to do it here, redisplay_internal must
14718 have copied all b->text->redisplay to their respective windows. */
14719 b->text->redisplay = false;
14721 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14722 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14723 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14724 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14726 w->current_matrix->buffer = b;
14727 w->current_matrix->begv = BUF_BEGV (b);
14728 w->current_matrix->zv = BUF_ZV (b);
14730 w->last_cursor_vpos = w->cursor.vpos;
14731 w->last_cursor_off_p = w->cursor_off_p;
14733 if (w == XWINDOW (selected_window))
14734 w->last_point = BUF_PT (b);
14735 else
14736 w->last_point = marker_position (w->pointm);
14738 w->window_end_valid = true;
14739 w->update_mode_line = false;
14742 w->redisplay = !accurate_p;
14746 /* Mark the display of windows in the window tree rooted at WINDOW as
14747 accurate or inaccurate. If ACCURATE_P, mark display of
14748 windows as accurate. If !ACCURATE_P, arrange for windows to
14749 be redisplayed the next time redisplay_internal is called. */
14751 void
14752 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14754 struct window *w;
14756 for (; !NILP (window); window = w->next)
14758 w = XWINDOW (window);
14759 if (WINDOWP (w->contents))
14760 mark_window_display_accurate (w->contents, accurate_p);
14761 else
14762 mark_window_display_accurate_1 (w, accurate_p);
14765 if (accurate_p)
14766 update_overlay_arrows (1);
14767 else
14768 /* Force a thorough redisplay the next time by setting
14769 last_arrow_position and last_arrow_string to t, which is
14770 unequal to any useful value of Voverlay_arrow_... */
14771 update_overlay_arrows (-1);
14775 /* Return value in display table DP (Lisp_Char_Table *) for character
14776 C. Since a display table doesn't have any parent, we don't have to
14777 follow parent. Do not call this function directly but use the
14778 macro DISP_CHAR_VECTOR. */
14780 Lisp_Object
14781 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14783 Lisp_Object val;
14785 if (ASCII_CHAR_P (c))
14787 val = dp->ascii;
14788 if (SUB_CHAR_TABLE_P (val))
14789 val = XSUB_CHAR_TABLE (val)->contents[c];
14791 else
14793 Lisp_Object table;
14795 XSETCHAR_TABLE (table, dp);
14796 val = char_table_ref (table, c);
14798 if (NILP (val))
14799 val = dp->defalt;
14800 return val;
14803 static int buffer_flip_blocked_depth;
14805 static void
14806 block_buffer_flips (void)
14808 eassert (buffer_flip_blocked_depth >= 0);
14809 buffer_flip_blocked_depth++;
14812 static void
14813 unblock_buffer_flips (void)
14815 eassert (buffer_flip_blocked_depth > 0);
14816 if (--buffer_flip_blocked_depth == 0)
14818 Lisp_Object tail, frame;
14819 block_input ();
14820 FOR_EACH_FRAME (tail, frame)
14822 struct frame *f = XFRAME (frame);
14823 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14824 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14826 unblock_input ();
14830 bool
14831 buffer_flipping_blocked_p (void)
14833 return buffer_flip_blocked_depth > 0;
14837 /***********************************************************************
14838 Window Redisplay
14839 ***********************************************************************/
14841 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14843 static void
14844 redisplay_windows (Lisp_Object window)
14846 while (!NILP (window))
14848 struct window *w = XWINDOW (window);
14850 if (WINDOWP (w->contents))
14851 redisplay_windows (w->contents);
14852 else if (BUFFERP (w->contents))
14854 displayed_buffer = XBUFFER (w->contents);
14855 /* Use list_of_error, not Qerror, so that
14856 we catch only errors and don't run the debugger. */
14857 internal_condition_case_1 (redisplay_window_0, window,
14858 list_of_error,
14859 redisplay_window_error);
14862 window = w->next;
14866 static Lisp_Object
14867 redisplay_window_error (Lisp_Object ignore)
14869 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14870 return Qnil;
14873 static Lisp_Object
14874 redisplay_window_0 (Lisp_Object window)
14876 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14877 redisplay_window (window, false);
14878 return Qnil;
14881 static Lisp_Object
14882 redisplay_window_1 (Lisp_Object window)
14884 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14885 redisplay_window (window, true);
14886 return Qnil;
14890 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14891 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14892 which positions recorded in ROW differ from current buffer
14893 positions.
14895 Return true iff cursor is on this row. */
14897 static bool
14898 set_cursor_from_row (struct window *w, struct glyph_row *row,
14899 struct glyph_matrix *matrix,
14900 ptrdiff_t delta, ptrdiff_t delta_bytes,
14901 int dy, int dvpos)
14903 struct glyph *glyph = row->glyphs[TEXT_AREA];
14904 struct glyph *end = glyph + row->used[TEXT_AREA];
14905 struct glyph *cursor = NULL;
14906 /* The last known character position in row. */
14907 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14908 int x = row->x;
14909 ptrdiff_t pt_old = PT - delta;
14910 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14911 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14912 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14913 /* A glyph beyond the edge of TEXT_AREA which we should never
14914 touch. */
14915 struct glyph *glyphs_end = end;
14916 /* True means we've found a match for cursor position, but that
14917 glyph has the avoid_cursor_p flag set. */
14918 bool match_with_avoid_cursor = false;
14919 /* True means we've seen at least one glyph that came from a
14920 display string. */
14921 bool string_seen = false;
14922 /* Largest and smallest buffer positions seen so far during scan of
14923 glyph row. */
14924 ptrdiff_t bpos_max = pos_before;
14925 ptrdiff_t bpos_min = pos_after;
14926 /* Last buffer position covered by an overlay string with an integer
14927 `cursor' property. */
14928 ptrdiff_t bpos_covered = 0;
14929 /* True means the display string on which to display the cursor
14930 comes from a text property, not from an overlay. */
14931 bool string_from_text_prop = false;
14933 /* Don't even try doing anything if called for a mode-line or
14934 header-line row, since the rest of the code isn't prepared to
14935 deal with such calamities. */
14936 eassert (!row->mode_line_p);
14937 if (row->mode_line_p)
14938 return false;
14940 /* Skip over glyphs not having an object at the start and the end of
14941 the row. These are special glyphs like truncation marks on
14942 terminal frames. */
14943 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14945 if (!row->reversed_p)
14947 while (glyph < end
14948 && NILP (glyph->object)
14949 && glyph->charpos < 0)
14951 x += glyph->pixel_width;
14952 ++glyph;
14954 while (end > glyph
14955 && NILP ((end - 1)->object)
14956 /* CHARPOS is zero for blanks and stretch glyphs
14957 inserted by extend_face_to_end_of_line. */
14958 && (end - 1)->charpos <= 0)
14959 --end;
14960 glyph_before = glyph - 1;
14961 glyph_after = end;
14963 else
14965 struct glyph *g;
14967 /* If the glyph row is reversed, we need to process it from back
14968 to front, so swap the edge pointers. */
14969 glyphs_end = end = glyph - 1;
14970 glyph += row->used[TEXT_AREA] - 1;
14972 while (glyph > end + 1
14973 && NILP (glyph->object)
14974 && glyph->charpos < 0)
14975 --glyph;
14976 if (NILP (glyph->object) && glyph->charpos < 0)
14977 --glyph;
14978 /* By default, in reversed rows we put the cursor on the
14979 rightmost (first in the reading order) glyph. */
14980 for (x = 0, g = end + 1; g < glyph; g++)
14981 x += g->pixel_width;
14982 while (end < glyph
14983 && NILP ((end + 1)->object)
14984 && (end + 1)->charpos <= 0)
14985 ++end;
14986 glyph_before = glyph + 1;
14987 glyph_after = end;
14990 else if (row->reversed_p)
14992 /* In R2L rows that don't display text, put the cursor on the
14993 rightmost glyph. Case in point: an empty last line that is
14994 part of an R2L paragraph. */
14995 cursor = end - 1;
14996 /* Avoid placing the cursor on the last glyph of the row, where
14997 on terminal frames we hold the vertical border between
14998 adjacent windows. */
14999 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
15000 && !WINDOW_RIGHTMOST_P (w)
15001 && cursor == row->glyphs[LAST_AREA] - 1)
15002 cursor--;
15003 x = -1; /* will be computed below, at label compute_x */
15006 /* Step 1: Try to find the glyph whose character position
15007 corresponds to point. If that's not possible, find 2 glyphs
15008 whose character positions are the closest to point, one before
15009 point, the other after it. */
15010 if (!row->reversed_p)
15011 while (/* not marched to end of glyph row */
15012 glyph < end
15013 /* glyph was not inserted by redisplay for internal purposes */
15014 && !NILP (glyph->object))
15016 if (BUFFERP (glyph->object))
15018 ptrdiff_t dpos = glyph->charpos - pt_old;
15020 if (glyph->charpos > bpos_max)
15021 bpos_max = glyph->charpos;
15022 if (glyph->charpos < bpos_min)
15023 bpos_min = glyph->charpos;
15024 if (!glyph->avoid_cursor_p)
15026 /* If we hit point, we've found the glyph on which to
15027 display the cursor. */
15028 if (dpos == 0)
15030 match_with_avoid_cursor = false;
15031 break;
15033 /* See if we've found a better approximation to
15034 POS_BEFORE or to POS_AFTER. */
15035 if (0 > dpos && dpos > pos_before - pt_old)
15037 pos_before = glyph->charpos;
15038 glyph_before = glyph;
15040 else if (0 < dpos && dpos < pos_after - pt_old)
15042 pos_after = glyph->charpos;
15043 glyph_after = glyph;
15046 else if (dpos == 0)
15047 match_with_avoid_cursor = true;
15049 else if (STRINGP (glyph->object))
15051 Lisp_Object chprop;
15052 ptrdiff_t glyph_pos = glyph->charpos;
15054 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15055 glyph->object);
15056 if (!NILP (chprop))
15058 /* If the string came from a `display' text property,
15059 look up the buffer position of that property and
15060 use that position to update bpos_max, as if we
15061 actually saw such a position in one of the row's
15062 glyphs. This helps with supporting integer values
15063 of `cursor' property on the display string in
15064 situations where most or all of the row's buffer
15065 text is completely covered by display properties,
15066 so that no glyph with valid buffer positions is
15067 ever seen in the row. */
15068 ptrdiff_t prop_pos =
15069 string_buffer_position_lim (glyph->object, pos_before,
15070 pos_after, false);
15072 if (prop_pos >= pos_before)
15073 bpos_max = prop_pos;
15075 if (INTEGERP (chprop))
15077 bpos_covered = bpos_max + XINT (chprop);
15078 /* If the `cursor' property covers buffer positions up
15079 to and including point, we should display cursor on
15080 this glyph. Note that, if a `cursor' property on one
15081 of the string's characters has an integer value, we
15082 will break out of the loop below _before_ we get to
15083 the position match above. IOW, integer values of
15084 the `cursor' property override the "exact match for
15085 point" strategy of positioning the cursor. */
15086 /* Implementation note: bpos_max == pt_old when, e.g.,
15087 we are in an empty line, where bpos_max is set to
15088 MATRIX_ROW_START_CHARPOS, see above. */
15089 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15091 cursor = glyph;
15092 break;
15096 string_seen = true;
15098 x += glyph->pixel_width;
15099 ++glyph;
15101 else if (glyph > end) /* row is reversed */
15102 while (!NILP (glyph->object))
15104 if (BUFFERP (glyph->object))
15106 ptrdiff_t dpos = glyph->charpos - pt_old;
15108 if (glyph->charpos > bpos_max)
15109 bpos_max = glyph->charpos;
15110 if (glyph->charpos < bpos_min)
15111 bpos_min = glyph->charpos;
15112 if (!glyph->avoid_cursor_p)
15114 if (dpos == 0)
15116 match_with_avoid_cursor = false;
15117 break;
15119 if (0 > dpos && dpos > pos_before - pt_old)
15121 pos_before = glyph->charpos;
15122 glyph_before = glyph;
15124 else if (0 < dpos && dpos < pos_after - pt_old)
15126 pos_after = glyph->charpos;
15127 glyph_after = glyph;
15130 else if (dpos == 0)
15131 match_with_avoid_cursor = true;
15133 else if (STRINGP (glyph->object))
15135 Lisp_Object chprop;
15136 ptrdiff_t glyph_pos = glyph->charpos;
15138 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15139 glyph->object);
15140 if (!NILP (chprop))
15142 ptrdiff_t prop_pos =
15143 string_buffer_position_lim (glyph->object, pos_before,
15144 pos_after, false);
15146 if (prop_pos >= pos_before)
15147 bpos_max = prop_pos;
15149 if (INTEGERP (chprop))
15151 bpos_covered = bpos_max + XINT (chprop);
15152 /* If the `cursor' property covers buffer positions up
15153 to and including point, we should display cursor on
15154 this glyph. */
15155 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15157 cursor = glyph;
15158 break;
15161 string_seen = true;
15163 --glyph;
15164 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15166 x--; /* can't use any pixel_width */
15167 break;
15169 x -= glyph->pixel_width;
15172 /* Step 2: If we didn't find an exact match for point, we need to
15173 look for a proper place to put the cursor among glyphs between
15174 GLYPH_BEFORE and GLYPH_AFTER. */
15175 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15176 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15177 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15179 /* An empty line has a single glyph whose OBJECT is nil and
15180 whose CHARPOS is the position of a newline on that line.
15181 Note that on a TTY, there are more glyphs after that, which
15182 were produced by extend_face_to_end_of_line, but their
15183 CHARPOS is zero or negative. */
15184 bool empty_line_p =
15185 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15186 && NILP (glyph->object) && glyph->charpos > 0
15187 /* On a TTY, continued and truncated rows also have a glyph at
15188 their end whose OBJECT is nil and whose CHARPOS is
15189 positive (the continuation and truncation glyphs), but such
15190 rows are obviously not "empty". */
15191 && !(row->continued_p || row->truncated_on_right_p));
15193 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15195 ptrdiff_t ellipsis_pos;
15197 /* Scan back over the ellipsis glyphs. */
15198 if (!row->reversed_p)
15200 ellipsis_pos = (glyph - 1)->charpos;
15201 while (glyph > row->glyphs[TEXT_AREA]
15202 && (glyph - 1)->charpos == ellipsis_pos)
15203 glyph--, x -= glyph->pixel_width;
15204 /* That loop always goes one position too far, including
15205 the glyph before the ellipsis. So scan forward over
15206 that one. */
15207 x += glyph->pixel_width;
15208 glyph++;
15210 else /* row is reversed */
15212 ellipsis_pos = (glyph + 1)->charpos;
15213 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15214 && (glyph + 1)->charpos == ellipsis_pos)
15215 glyph++, x += glyph->pixel_width;
15216 x -= glyph->pixel_width;
15217 glyph--;
15220 else if (match_with_avoid_cursor)
15222 cursor = glyph_after;
15223 x = -1;
15225 else if (string_seen)
15227 int incr = row->reversed_p ? -1 : +1;
15229 /* Need to find the glyph that came out of a string which is
15230 present at point. That glyph is somewhere between
15231 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15232 positioned between POS_BEFORE and POS_AFTER in the
15233 buffer. */
15234 struct glyph *start, *stop;
15235 ptrdiff_t pos = pos_before;
15237 x = -1;
15239 /* If the row ends in a newline from a display string,
15240 reordering could have moved the glyphs belonging to the
15241 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15242 in this case we extend the search to the last glyph in
15243 the row that was not inserted by redisplay. */
15244 if (row->ends_in_newline_from_string_p)
15246 glyph_after = end;
15247 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15250 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15251 correspond to POS_BEFORE and POS_AFTER, respectively. We
15252 need START and STOP in the order that corresponds to the
15253 row's direction as given by its reversed_p flag. If the
15254 directionality of characters between POS_BEFORE and
15255 POS_AFTER is the opposite of the row's base direction,
15256 these characters will have been reordered for display,
15257 and we need to reverse START and STOP. */
15258 if (!row->reversed_p)
15260 start = min (glyph_before, glyph_after);
15261 stop = max (glyph_before, glyph_after);
15263 else
15265 start = max (glyph_before, glyph_after);
15266 stop = min (glyph_before, glyph_after);
15268 for (glyph = start + incr;
15269 row->reversed_p ? glyph > stop : glyph < stop; )
15272 /* Any glyphs that come from the buffer are here because
15273 of bidi reordering. Skip them, and only pay
15274 attention to glyphs that came from some string. */
15275 if (STRINGP (glyph->object))
15277 Lisp_Object str;
15278 ptrdiff_t tem;
15279 /* If the display property covers the newline, we
15280 need to search for it one position farther. */
15281 ptrdiff_t lim = pos_after
15282 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15284 string_from_text_prop = false;
15285 str = glyph->object;
15286 tem = string_buffer_position_lim (str, pos, lim, false);
15287 if (tem == 0 /* from overlay */
15288 || pos <= tem)
15290 /* If the string from which this glyph came is
15291 found in the buffer at point, or at position
15292 that is closer to point than pos_after, then
15293 we've found the glyph we've been looking for.
15294 If it comes from an overlay (tem == 0), and
15295 it has the `cursor' property on one of its
15296 glyphs, record that glyph as a candidate for
15297 displaying the cursor. (As in the
15298 unidirectional version, we will display the
15299 cursor on the last candidate we find.) */
15300 if (tem == 0
15301 || tem == pt_old
15302 || (tem - pt_old > 0 && tem < pos_after))
15304 /* The glyphs from this string could have
15305 been reordered. Find the one with the
15306 smallest string position. Or there could
15307 be a character in the string with the
15308 `cursor' property, which means display
15309 cursor on that character's glyph. */
15310 ptrdiff_t strpos = glyph->charpos;
15312 if (tem)
15314 cursor = glyph;
15315 string_from_text_prop = true;
15317 for ( ;
15318 (row->reversed_p ? glyph > stop : glyph < stop)
15319 && EQ (glyph->object, str);
15320 glyph += incr)
15322 Lisp_Object cprop;
15323 ptrdiff_t gpos = glyph->charpos;
15325 cprop = Fget_char_property (make_number (gpos),
15326 Qcursor,
15327 glyph->object);
15328 if (!NILP (cprop))
15330 cursor = glyph;
15331 break;
15333 if (tem && glyph->charpos < strpos)
15335 strpos = glyph->charpos;
15336 cursor = glyph;
15340 if (tem == pt_old
15341 || (tem - pt_old > 0 && tem < pos_after))
15342 goto compute_x;
15344 if (tem)
15345 pos = tem + 1; /* don't find previous instances */
15347 /* This string is not what we want; skip all of the
15348 glyphs that came from it. */
15349 while ((row->reversed_p ? glyph > stop : glyph < stop)
15350 && EQ (glyph->object, str))
15351 glyph += incr;
15353 else
15354 glyph += incr;
15357 /* If we reached the end of the line, and END was from a string,
15358 the cursor is not on this line. */
15359 if (cursor == NULL
15360 && (row->reversed_p ? glyph <= end : glyph >= end)
15361 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15362 && STRINGP (end->object)
15363 && row->continued_p)
15364 return false;
15366 /* A truncated row may not include PT among its character positions.
15367 Setting the cursor inside the scroll margin will trigger
15368 recalculation of hscroll in hscroll_window_tree. But if a
15369 display string covers point, defer to the string-handling
15370 code below to figure this out. */
15371 else if (row->truncated_on_left_p && pt_old < bpos_min)
15373 cursor = glyph_before;
15374 x = -1;
15376 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15377 /* Zero-width characters produce no glyphs. */
15378 || (!empty_line_p
15379 && (row->reversed_p
15380 ? glyph_after > glyphs_end
15381 : glyph_after < glyphs_end)))
15383 cursor = glyph_after;
15384 x = -1;
15388 compute_x:
15389 if (cursor != NULL)
15390 glyph = cursor;
15391 else if (glyph == glyphs_end
15392 && pos_before == pos_after
15393 && STRINGP ((row->reversed_p
15394 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15395 : row->glyphs[TEXT_AREA])->object))
15397 /* If all the glyphs of this row came from strings, put the
15398 cursor on the first glyph of the row. This avoids having the
15399 cursor outside of the text area in this very rare and hard
15400 use case. */
15401 glyph =
15402 row->reversed_p
15403 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15404 : row->glyphs[TEXT_AREA];
15406 if (x < 0)
15408 struct glyph *g;
15410 /* Need to compute x that corresponds to GLYPH. */
15411 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15413 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15414 emacs_abort ();
15415 x += g->pixel_width;
15419 /* ROW could be part of a continued line, which, under bidi
15420 reordering, might have other rows whose start and end charpos
15421 occlude point. Only set w->cursor if we found a better
15422 approximation to the cursor position than we have from previously
15423 examined candidate rows belonging to the same continued line. */
15424 if (/* We already have a candidate row. */
15425 w->cursor.vpos >= 0
15426 /* That candidate is not the row we are processing. */
15427 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15428 /* Make sure cursor.vpos specifies a row whose start and end
15429 charpos occlude point, and it is valid candidate for being a
15430 cursor-row. This is because some callers of this function
15431 leave cursor.vpos at the row where the cursor was displayed
15432 during the last redisplay cycle. */
15433 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15434 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15435 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15437 struct glyph *g1
15438 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15440 /* Don't consider glyphs that are outside TEXT_AREA. */
15441 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15442 return false;
15443 /* Keep the candidate whose buffer position is the closest to
15444 point or has the `cursor' property. */
15445 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15446 w->cursor.hpos >= 0
15447 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15448 && ((BUFFERP (g1->object)
15449 && (g1->charpos == pt_old /* An exact match always wins. */
15450 || (BUFFERP (glyph->object)
15451 && eabs (g1->charpos - pt_old)
15452 < eabs (glyph->charpos - pt_old))))
15453 /* Previous candidate is a glyph from a string that has
15454 a non-nil `cursor' property. */
15455 || (STRINGP (g1->object)
15456 && (!NILP (Fget_char_property (make_number (g1->charpos),
15457 Qcursor, g1->object))
15458 /* Previous candidate is from the same display
15459 string as this one, and the display string
15460 came from a text property. */
15461 || (EQ (g1->object, glyph->object)
15462 && string_from_text_prop)
15463 /* this candidate is from newline and its
15464 position is not an exact match */
15465 || (NILP (glyph->object)
15466 && glyph->charpos != pt_old)))))
15467 return false;
15468 /* If this candidate gives an exact match, use that. */
15469 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15470 /* If this candidate is a glyph created for the
15471 terminating newline of a line, and point is on that
15472 newline, it wins because it's an exact match. */
15473 || (!row->continued_p
15474 && NILP (glyph->object)
15475 && glyph->charpos == 0
15476 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15477 /* Otherwise, keep the candidate that comes from a row
15478 spanning less buffer positions. This may win when one or
15479 both candidate positions are on glyphs that came from
15480 display strings, for which we cannot compare buffer
15481 positions. */
15482 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15483 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15484 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15485 return false;
15487 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15488 w->cursor.x = x;
15489 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15490 w->cursor.y = row->y + dy;
15492 if (w == XWINDOW (selected_window))
15494 if (!row->continued_p
15495 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15496 && row->x == 0)
15498 this_line_buffer = XBUFFER (w->contents);
15500 CHARPOS (this_line_start_pos)
15501 = MATRIX_ROW_START_CHARPOS (row) + delta;
15502 BYTEPOS (this_line_start_pos)
15503 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15505 CHARPOS (this_line_end_pos)
15506 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15507 BYTEPOS (this_line_end_pos)
15508 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15510 this_line_y = w->cursor.y;
15511 this_line_pixel_height = row->height;
15512 this_line_vpos = w->cursor.vpos;
15513 this_line_start_x = row->x;
15515 else
15516 CHARPOS (this_line_start_pos) = 0;
15519 return true;
15523 /* Run window scroll functions, if any, for WINDOW with new window
15524 start STARTP. Sets the window start of WINDOW to that position.
15526 We assume that the window's buffer is really current. */
15528 static struct text_pos
15529 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15531 struct window *w = XWINDOW (window);
15532 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15534 eassert (current_buffer == XBUFFER (w->contents));
15536 if (!NILP (Vwindow_scroll_functions))
15538 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15539 make_number (CHARPOS (startp)));
15540 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15541 /* In case the hook functions switch buffers. */
15542 set_buffer_internal (XBUFFER (w->contents));
15545 return startp;
15549 /* Make sure the line containing the cursor is fully visible.
15550 A value of true means there is nothing to be done.
15551 (Either the line is fully visible, or it cannot be made so,
15552 or we cannot tell.)
15554 If FORCE_P, return false even if partial visible cursor row
15555 is higher than window.
15557 If CURRENT_MATRIX_P, use the information from the
15558 window's current glyph matrix; otherwise use the desired glyph
15559 matrix.
15561 A value of false means the caller should do scrolling
15562 as if point had gone off the screen. */
15564 static bool
15565 cursor_row_fully_visible_p (struct window *w, bool force_p,
15566 bool current_matrix_p)
15568 struct glyph_matrix *matrix;
15569 struct glyph_row *row;
15570 int window_height;
15572 if (!make_cursor_line_fully_visible_p)
15573 return true;
15575 /* It's not always possible to find the cursor, e.g, when a window
15576 is full of overlay strings. Don't do anything in that case. */
15577 if (w->cursor.vpos < 0)
15578 return true;
15580 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15581 row = MATRIX_ROW (matrix, w->cursor.vpos);
15583 /* If the cursor row is not partially visible, there's nothing to do. */
15584 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15585 return true;
15587 /* If the row the cursor is in is taller than the window's height,
15588 it's not clear what to do, so do nothing. */
15589 window_height = window_box_height (w);
15590 if (row->height >= window_height)
15592 if (!force_p || MINI_WINDOW_P (w)
15593 || w->vscroll || w->cursor.vpos == 0)
15594 return true;
15596 return false;
15600 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15601 means only WINDOW is redisplayed in redisplay_internal.
15602 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15603 in redisplay_window to bring a partially visible line into view in
15604 the case that only the cursor has moved.
15606 LAST_LINE_MISFIT should be true if we're scrolling because the
15607 last screen line's vertical height extends past the end of the screen.
15609 Value is
15611 1 if scrolling succeeded
15613 0 if scrolling didn't find point.
15615 -1 if new fonts have been loaded so that we must interrupt
15616 redisplay, adjust glyph matrices, and try again. */
15618 enum
15620 SCROLLING_SUCCESS,
15621 SCROLLING_FAILED,
15622 SCROLLING_NEED_LARGER_MATRICES
15625 /* If scroll-conservatively is more than this, never recenter.
15627 If you change this, don't forget to update the doc string of
15628 `scroll-conservatively' and the Emacs manual. */
15629 #define SCROLL_LIMIT 100
15631 static int
15632 try_scrolling (Lisp_Object window, bool just_this_one_p,
15633 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15634 bool temp_scroll_step, bool last_line_misfit)
15636 struct window *w = XWINDOW (window);
15637 struct text_pos pos, startp;
15638 struct it it;
15639 int this_scroll_margin, scroll_max, rc, height;
15640 int dy = 0, amount_to_scroll = 0;
15641 bool scroll_down_p = false;
15642 int extra_scroll_margin_lines = last_line_misfit;
15643 Lisp_Object aggressive;
15644 /* We will never try scrolling more than this number of lines. */
15645 int scroll_limit = SCROLL_LIMIT;
15646 int frame_line_height = default_line_pixel_height (w);
15648 #ifdef GLYPH_DEBUG
15649 debug_method_add (w, "try_scrolling");
15650 #endif
15652 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15654 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15656 /* Force arg_scroll_conservatively to have a reasonable value, to
15657 avoid scrolling too far away with slow move_it_* functions. Note
15658 that the user can supply scroll-conservatively equal to
15659 `most-positive-fixnum', which can be larger than INT_MAX. */
15660 if (arg_scroll_conservatively > scroll_limit)
15662 arg_scroll_conservatively = scroll_limit + 1;
15663 scroll_max = scroll_limit * frame_line_height;
15665 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15666 /* Compute how much we should try to scroll maximally to bring
15667 point into view. */
15668 scroll_max = (max (scroll_step,
15669 max (arg_scroll_conservatively, temp_scroll_step))
15670 * frame_line_height);
15671 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15672 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15673 /* We're trying to scroll because of aggressive scrolling but no
15674 scroll_step is set. Choose an arbitrary one. */
15675 scroll_max = 10 * frame_line_height;
15676 else
15677 scroll_max = 0;
15679 too_near_end:
15681 /* Decide whether to scroll down. */
15682 if (PT > CHARPOS (startp))
15684 int scroll_margin_y;
15686 /* Compute the pixel ypos of the scroll margin, then move IT to
15687 either that ypos or PT, whichever comes first. */
15688 start_display (&it, w, startp);
15689 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15690 - this_scroll_margin
15691 - frame_line_height * extra_scroll_margin_lines;
15692 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15693 (MOVE_TO_POS | MOVE_TO_Y));
15695 if (PT > CHARPOS (it.current.pos))
15697 int y0 = line_bottom_y (&it);
15698 /* Compute how many pixels below window bottom to stop searching
15699 for PT. This avoids costly search for PT that is far away if
15700 the user limited scrolling by a small number of lines, but
15701 always finds PT if scroll_conservatively is set to a large
15702 number, such as most-positive-fixnum. */
15703 int slack = max (scroll_max, 10 * frame_line_height);
15704 int y_to_move = it.last_visible_y + slack;
15706 /* Compute the distance from the scroll margin to PT or to
15707 the scroll limit, whichever comes first. This should
15708 include the height of the cursor line, to make that line
15709 fully visible. */
15710 move_it_to (&it, PT, -1, y_to_move,
15711 -1, MOVE_TO_POS | MOVE_TO_Y);
15712 dy = line_bottom_y (&it) - y0;
15714 if (dy > scroll_max)
15715 return SCROLLING_FAILED;
15717 if (dy > 0)
15718 scroll_down_p = true;
15720 else if (PT == IT_CHARPOS (it)
15721 && IT_CHARPOS (it) < ZV
15722 && it.method == GET_FROM_STRING
15723 && arg_scroll_conservatively > scroll_limit
15724 && it.current_x == 0)
15726 enum move_it_result skip;
15727 int y1 = it.current_y;
15728 int vpos;
15730 /* A before-string that includes newlines and is displayed
15731 on the last visible screen line could fail us under
15732 scroll-conservatively > 100, because we will be unable to
15733 position the cursor on that last visible line. Try to
15734 recover by finding the first screen line that has some
15735 glyphs coming from the buffer text. */
15736 do {
15737 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15738 if (skip != MOVE_NEWLINE_OR_CR
15739 || IT_CHARPOS (it) != PT
15740 || it.method == GET_FROM_BUFFER)
15741 break;
15742 vpos = it.vpos;
15743 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15744 } while (it.vpos > vpos);
15746 dy = it.current_y - y1;
15748 if (dy > scroll_max)
15749 return SCROLLING_FAILED;
15751 if (dy > 0)
15752 scroll_down_p = true;
15756 if (scroll_down_p)
15758 /* Point is in or below the bottom scroll margin, so move the
15759 window start down. If scrolling conservatively, move it just
15760 enough down to make point visible. If scroll_step is set,
15761 move it down by scroll_step. */
15762 if (arg_scroll_conservatively)
15763 amount_to_scroll
15764 = min (max (dy, frame_line_height),
15765 frame_line_height * arg_scroll_conservatively);
15766 else if (scroll_step || temp_scroll_step)
15767 amount_to_scroll = scroll_max;
15768 else
15770 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15771 height = WINDOW_BOX_TEXT_HEIGHT (w);
15772 if (NUMBERP (aggressive))
15774 double float_amount = XFLOATINT (aggressive) * height;
15775 int aggressive_scroll = float_amount;
15776 if (aggressive_scroll == 0 && float_amount > 0)
15777 aggressive_scroll = 1;
15778 /* Don't let point enter the scroll margin near top of
15779 the window. This could happen if the value of
15780 scroll_up_aggressively is too large and there are
15781 non-zero margins, because scroll_up_aggressively
15782 means put point that fraction of window height
15783 _from_the_bottom_margin_. */
15784 if (aggressive_scroll + 2 * this_scroll_margin > height)
15785 aggressive_scroll = height - 2 * this_scroll_margin;
15786 amount_to_scroll = dy + aggressive_scroll;
15790 if (amount_to_scroll <= 0)
15791 return SCROLLING_FAILED;
15793 start_display (&it, w, startp);
15794 if (arg_scroll_conservatively <= scroll_limit)
15795 move_it_vertically (&it, amount_to_scroll);
15796 else
15798 /* Extra precision for users who set scroll-conservatively
15799 to a large number: make sure the amount we scroll
15800 the window start is never less than amount_to_scroll,
15801 which was computed as distance from window bottom to
15802 point. This matters when lines at window top and lines
15803 below window bottom have different height. */
15804 struct it it1;
15805 void *it1data = NULL;
15806 /* We use a temporary it1 because line_bottom_y can modify
15807 its argument, if it moves one line down; see there. */
15808 int start_y;
15810 SAVE_IT (it1, it, it1data);
15811 start_y = line_bottom_y (&it1);
15812 do {
15813 RESTORE_IT (&it, &it, it1data);
15814 move_it_by_lines (&it, 1);
15815 SAVE_IT (it1, it, it1data);
15816 } while (IT_CHARPOS (it) < ZV
15817 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15818 bidi_unshelve_cache (it1data, true);
15821 /* If STARTP is unchanged, move it down another screen line. */
15822 if (IT_CHARPOS (it) == CHARPOS (startp))
15823 move_it_by_lines (&it, 1);
15824 startp = it.current.pos;
15826 else
15828 struct text_pos scroll_margin_pos = startp;
15829 int y_offset = 0;
15831 /* See if point is inside the scroll margin at the top of the
15832 window. */
15833 if (this_scroll_margin)
15835 int y_start;
15837 start_display (&it, w, startp);
15838 y_start = it.current_y;
15839 move_it_vertically (&it, this_scroll_margin);
15840 scroll_margin_pos = it.current.pos;
15841 /* If we didn't move enough before hitting ZV, request
15842 additional amount of scroll, to move point out of the
15843 scroll margin. */
15844 if (IT_CHARPOS (it) == ZV
15845 && it.current_y - y_start < this_scroll_margin)
15846 y_offset = this_scroll_margin - (it.current_y - y_start);
15849 if (PT < CHARPOS (scroll_margin_pos))
15851 /* Point is in the scroll margin at the top of the window or
15852 above what is displayed in the window. */
15853 int y0, y_to_move;
15855 /* Compute the vertical distance from PT to the scroll
15856 margin position. Move as far as scroll_max allows, or
15857 one screenful, or 10 screen lines, whichever is largest.
15858 Give up if distance is greater than scroll_max or if we
15859 didn't reach the scroll margin position. */
15860 SET_TEXT_POS (pos, PT, PT_BYTE);
15861 start_display (&it, w, pos);
15862 y0 = it.current_y;
15863 y_to_move = max (it.last_visible_y,
15864 max (scroll_max, 10 * frame_line_height));
15865 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15866 y_to_move, -1,
15867 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15868 dy = it.current_y - y0;
15869 if (dy > scroll_max
15870 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15871 return SCROLLING_FAILED;
15873 /* Additional scroll for when ZV was too close to point. */
15874 dy += y_offset;
15876 /* Compute new window start. */
15877 start_display (&it, w, startp);
15879 if (arg_scroll_conservatively)
15880 amount_to_scroll = max (dy, frame_line_height
15881 * max (scroll_step, temp_scroll_step));
15882 else if (scroll_step || temp_scroll_step)
15883 amount_to_scroll = scroll_max;
15884 else
15886 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15887 height = WINDOW_BOX_TEXT_HEIGHT (w);
15888 if (NUMBERP (aggressive))
15890 double float_amount = XFLOATINT (aggressive) * height;
15891 int aggressive_scroll = float_amount;
15892 if (aggressive_scroll == 0 && float_amount > 0)
15893 aggressive_scroll = 1;
15894 /* Don't let point enter the scroll margin near
15895 bottom of the window, if the value of
15896 scroll_down_aggressively happens to be too
15897 large. */
15898 if (aggressive_scroll + 2 * this_scroll_margin > height)
15899 aggressive_scroll = height - 2 * this_scroll_margin;
15900 amount_to_scroll = dy + aggressive_scroll;
15904 if (amount_to_scroll <= 0)
15905 return SCROLLING_FAILED;
15907 move_it_vertically_backward (&it, amount_to_scroll);
15908 startp = it.current.pos;
15912 /* Run window scroll functions. */
15913 startp = run_window_scroll_functions (window, startp);
15915 /* Display the window. Give up if new fonts are loaded, or if point
15916 doesn't appear. */
15917 if (!try_window (window, startp, 0))
15918 rc = SCROLLING_NEED_LARGER_MATRICES;
15919 else if (w->cursor.vpos < 0)
15921 clear_glyph_matrix (w->desired_matrix);
15922 rc = SCROLLING_FAILED;
15924 else
15926 /* Maybe forget recorded base line for line number display. */
15927 if (!just_this_one_p
15928 || current_buffer->clip_changed
15929 || BEG_UNCHANGED < CHARPOS (startp))
15930 w->base_line_number = 0;
15932 /* If cursor ends up on a partially visible line,
15933 treat that as being off the bottom of the screen. */
15934 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15935 false)
15936 /* It's possible that the cursor is on the first line of the
15937 buffer, which is partially obscured due to a vscroll
15938 (Bug#7537). In that case, avoid looping forever. */
15939 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15941 clear_glyph_matrix (w->desired_matrix);
15942 ++extra_scroll_margin_lines;
15943 goto too_near_end;
15945 rc = SCROLLING_SUCCESS;
15948 return rc;
15952 /* Compute a suitable window start for window W if display of W starts
15953 on a continuation line. Value is true if a new window start
15954 was computed.
15956 The new window start will be computed, based on W's width, starting
15957 from the start of the continued line. It is the start of the
15958 screen line with the minimum distance from the old start W->start,
15959 which is still before point (otherwise point will definitely not
15960 be visible in the window). */
15962 static bool
15963 compute_window_start_on_continuation_line (struct window *w)
15965 struct text_pos pos, start_pos, pos_before_pt;
15966 bool window_start_changed_p = false;
15968 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15970 /* If window start is on a continuation line... Window start may be
15971 < BEGV in case there's invisible text at the start of the
15972 buffer (M-x rmail, for example). */
15973 if (CHARPOS (start_pos) > BEGV
15974 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15976 struct it it;
15977 struct glyph_row *row;
15979 /* Handle the case that the window start is out of range. */
15980 if (CHARPOS (start_pos) < BEGV)
15981 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15982 else if (CHARPOS (start_pos) > ZV)
15983 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15985 /* Find the start of the continued line. This should be fast
15986 because find_newline is fast (newline cache). */
15987 row = w->desired_matrix->rows + window_wants_header_line (w);
15988 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15989 row, DEFAULT_FACE_ID);
15990 reseat_at_previous_visible_line_start (&it);
15992 /* If the line start is "too far" away from the window start,
15993 say it takes too much time to compute a new window start.
15994 Also, give up if the line start is after point, as in that
15995 case point will not be visible with any window start we
15996 compute. */
15997 if (IT_CHARPOS (it) <= PT
15998 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15999 /* PXW: Do we need upper bounds here? */
16000 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
16002 int min_distance, distance;
16004 /* Move forward by display lines to find the new window
16005 start. If window width was enlarged, the new start can
16006 be expected to be > the old start. If window width was
16007 decreased, the new window start will be < the old start.
16008 So, we're looking for the display line start with the
16009 minimum distance from the old window start. */
16010 pos_before_pt = pos = it.current.pos;
16011 min_distance = DISP_INFINITY;
16012 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
16013 distance < min_distance)
16015 min_distance = distance;
16016 if (CHARPOS (pos) <= PT)
16017 pos_before_pt = pos;
16018 pos = it.current.pos;
16019 if (it.line_wrap == WORD_WRAP)
16021 /* Under WORD_WRAP, move_it_by_lines is likely to
16022 overshoot and stop not at the first, but the
16023 second character from the left margin. So in
16024 that case, we need a more tight control on the X
16025 coordinate of the iterator than move_it_by_lines
16026 promises in its contract. The method is to first
16027 go to the last (rightmost) visible character of a
16028 line, then move to the leftmost character on the
16029 next line in a separate call. */
16030 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
16031 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16032 move_it_to (&it, ZV, 0,
16033 it.current_y + it.max_ascent + it.max_descent, -1,
16034 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16036 else
16037 move_it_by_lines (&it, 1);
16040 /* It makes very little sense to make the new window start
16041 after point, as point won't be visible. If that's what
16042 the loop above finds, fall back on the candidate before
16043 or at point that is closest to the old window start. */
16044 if (CHARPOS (pos) > PT)
16045 pos = pos_before_pt;
16047 /* Set the window start there. */
16048 SET_MARKER_FROM_TEXT_POS (w->start, pos);
16049 window_start_changed_p = true;
16053 return window_start_changed_p;
16057 /* Try cursor movement in case text has not changed in window WINDOW,
16058 with window start STARTP. Value is
16060 CURSOR_MOVEMENT_SUCCESS if successful
16062 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16064 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16065 display. *SCROLL_STEP is set to true, under certain circumstances, if
16066 we want to scroll as if scroll-step were set to 1. See the code.
16068 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16069 which case we have to abort this redisplay, and adjust matrices
16070 first. */
16072 enum
16074 CURSOR_MOVEMENT_SUCCESS,
16075 CURSOR_MOVEMENT_CANNOT_BE_USED,
16076 CURSOR_MOVEMENT_MUST_SCROLL,
16077 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16080 static int
16081 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16082 bool *scroll_step)
16084 struct window *w = XWINDOW (window);
16085 struct frame *f = XFRAME (w->frame);
16086 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16088 #ifdef GLYPH_DEBUG
16089 if (inhibit_try_cursor_movement)
16090 return rc;
16091 #endif
16093 /* Previously, there was a check for Lisp integer in the
16094 if-statement below. Now, this field is converted to
16095 ptrdiff_t, thus zero means invalid position in a buffer. */
16096 eassert (w->last_point > 0);
16097 /* Likewise there was a check whether window_end_vpos is nil or larger
16098 than the window. Now window_end_vpos is int and so never nil, but
16099 let's leave eassert to check whether it fits in the window. */
16100 eassert (!w->window_end_valid
16101 || w->window_end_vpos < w->current_matrix->nrows);
16103 /* Handle case where text has not changed, only point, and it has
16104 not moved off the frame. */
16105 if (/* Point may be in this window. */
16106 PT >= CHARPOS (startp)
16107 /* Selective display hasn't changed. */
16108 && !current_buffer->clip_changed
16109 /* Function force-mode-line-update is used to force a thorough
16110 redisplay. It sets either windows_or_buffers_changed or
16111 update_mode_lines. So don't take a shortcut here for these
16112 cases. */
16113 && !update_mode_lines
16114 && !windows_or_buffers_changed
16115 && !f->cursor_type_changed
16116 && NILP (Vshow_trailing_whitespace)
16117 /* When display-line-numbers is in relative mode, moving point
16118 requires to redraw the entire window. */
16119 && !EQ (Vdisplay_line_numbers, Qrelative)
16120 && !EQ (Vdisplay_line_numbers, Qvisual)
16121 /* When the current line number should be displayed in a
16122 distinct face, moving point cannot be handled in optimized
16123 way as below. */
16124 && !(!NILP (Vdisplay_line_numbers)
16125 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16126 Qline_number_current_line,
16127 w->frame)))
16128 /* This code is not used for mini-buffer for the sake of the case
16129 of redisplaying to replace an echo area message; since in
16130 that case the mini-buffer contents per se are usually
16131 unchanged. This code is of no real use in the mini-buffer
16132 since the handling of this_line_start_pos, etc., in redisplay
16133 handles the same cases. */
16134 && !EQ (window, minibuf_window)
16135 /* When overlay arrow is shown in current buffer, point movement
16136 is no longer "simple", as it typically causes the overlay
16137 arrow to move as well. */
16138 && !overlay_arrow_in_current_buffer_p ())
16140 int this_scroll_margin, top_scroll_margin;
16141 struct glyph_row *row = NULL;
16143 #ifdef GLYPH_DEBUG
16144 debug_method_add (w, "cursor movement");
16145 #endif
16147 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16149 top_scroll_margin = this_scroll_margin;
16150 if (window_wants_header_line (w))
16151 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16153 /* Start with the row the cursor was displayed during the last
16154 not paused redisplay. Give up if that row is not valid. */
16155 if (w->last_cursor_vpos < 0
16156 || w->last_cursor_vpos >= w->current_matrix->nrows)
16157 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16158 else
16160 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16161 if (row->mode_line_p)
16162 ++row;
16163 if (!row->enabled_p)
16164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16167 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16169 bool scroll_p = false, must_scroll = false;
16170 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16172 if (PT > w->last_point)
16174 /* Point has moved forward. */
16175 while (MATRIX_ROW_END_CHARPOS (row) < PT
16176 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16178 eassert (row->enabled_p);
16179 ++row;
16182 /* If the end position of a row equals the start
16183 position of the next row, and PT is at that position,
16184 we would rather display cursor in the next line. */
16185 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16186 && MATRIX_ROW_END_CHARPOS (row) == PT
16187 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16188 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16189 && !cursor_row_p (row))
16190 ++row;
16192 /* If within the scroll margin, scroll. Note that
16193 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16194 the next line would be drawn, and that
16195 this_scroll_margin can be zero. */
16196 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16197 || PT > MATRIX_ROW_END_CHARPOS (row)
16198 /* Line is completely visible last line in window
16199 and PT is to be set in the next line. */
16200 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16201 && PT == MATRIX_ROW_END_CHARPOS (row)
16202 && !row->ends_at_zv_p
16203 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16204 scroll_p = true;
16206 else if (PT < w->last_point)
16208 /* Cursor has to be moved backward. Note that PT >=
16209 CHARPOS (startp) because of the outer if-statement. */
16210 while (!row->mode_line_p
16211 && (MATRIX_ROW_START_CHARPOS (row) > PT
16212 || (MATRIX_ROW_START_CHARPOS (row) == PT
16213 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16214 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16215 row > w->current_matrix->rows
16216 && (row-1)->ends_in_newline_from_string_p))))
16217 && (row->y > top_scroll_margin
16218 || CHARPOS (startp) == BEGV))
16220 eassert (row->enabled_p);
16221 --row;
16224 /* Consider the following case: Window starts at BEGV,
16225 there is invisible, intangible text at BEGV, so that
16226 display starts at some point START > BEGV. It can
16227 happen that we are called with PT somewhere between
16228 BEGV and START. Try to handle that case. */
16229 if (row < w->current_matrix->rows
16230 || row->mode_line_p)
16232 row = w->current_matrix->rows;
16233 if (row->mode_line_p)
16234 ++row;
16237 /* Due to newlines in overlay strings, we may have to
16238 skip forward over overlay strings. */
16239 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16240 && MATRIX_ROW_END_CHARPOS (row) == PT
16241 && !cursor_row_p (row))
16242 ++row;
16244 /* If within the scroll margin, scroll. */
16245 if (row->y < top_scroll_margin
16246 && CHARPOS (startp) != BEGV)
16247 scroll_p = true;
16249 else
16251 /* Cursor did not move. So don't scroll even if cursor line
16252 is partially visible, as it was so before. */
16253 rc = CURSOR_MOVEMENT_SUCCESS;
16256 if (PT < MATRIX_ROW_START_CHARPOS (row)
16257 || PT > MATRIX_ROW_END_CHARPOS (row))
16259 /* if PT is not in the glyph row, give up. */
16260 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16261 must_scroll = true;
16263 else if (rc != CURSOR_MOVEMENT_SUCCESS
16264 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16266 struct glyph_row *row1;
16268 /* If rows are bidi-reordered and point moved, back up
16269 until we find a row that does not belong to a
16270 continuation line. This is because we must consider
16271 all rows of a continued line as candidates for the
16272 new cursor positioning, since row start and end
16273 positions change non-linearly with vertical position
16274 in such rows. */
16275 /* FIXME: Revisit this when glyph ``spilling'' in
16276 continuation lines' rows is implemented for
16277 bidi-reordered rows. */
16278 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16279 MATRIX_ROW_CONTINUATION_LINE_P (row);
16280 --row)
16282 /* If we hit the beginning of the displayed portion
16283 without finding the first row of a continued
16284 line, give up. */
16285 if (row <= row1)
16287 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16288 break;
16290 eassert (row->enabled_p);
16293 if (must_scroll)
16295 else if (rc != CURSOR_MOVEMENT_SUCCESS
16296 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16297 /* Make sure this isn't a header line by any chance, since
16298 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16299 && !row->mode_line_p
16300 && make_cursor_line_fully_visible_p)
16302 if (PT == MATRIX_ROW_END_CHARPOS (row)
16303 && !row->ends_at_zv_p
16304 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16305 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16306 else if (row->height > window_box_height (w))
16308 /* If we end up in a partially visible line, let's
16309 make it fully visible, except when it's taller
16310 than the window, in which case we can't do much
16311 about it. */
16312 *scroll_step = true;
16313 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16315 else
16317 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16318 if (!cursor_row_fully_visible_p (w, false, true))
16319 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16320 else
16321 rc = CURSOR_MOVEMENT_SUCCESS;
16324 else if (scroll_p)
16325 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16326 else if (rc != CURSOR_MOVEMENT_SUCCESS
16327 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16329 /* With bidi-reordered rows, there could be more than
16330 one candidate row whose start and end positions
16331 occlude point. We need to let set_cursor_from_row
16332 find the best candidate. */
16333 /* FIXME: Revisit this when glyph ``spilling'' in
16334 continuation lines' rows is implemented for
16335 bidi-reordered rows. */
16336 bool rv = false;
16340 bool at_zv_p = false, exact_match_p = false;
16342 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16343 && PT <= MATRIX_ROW_END_CHARPOS (row)
16344 && cursor_row_p (row))
16345 rv |= set_cursor_from_row (w, row, w->current_matrix,
16346 0, 0, 0, 0);
16347 /* As soon as we've found the exact match for point,
16348 or the first suitable row whose ends_at_zv_p flag
16349 is set, we are done. */
16350 if (rv)
16352 at_zv_p = MATRIX_ROW (w->current_matrix,
16353 w->cursor.vpos)->ends_at_zv_p;
16354 if (!at_zv_p
16355 && w->cursor.hpos >= 0
16356 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16357 w->cursor.vpos))
16359 struct glyph_row *candidate =
16360 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16361 struct glyph *g =
16362 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16363 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16365 exact_match_p =
16366 (BUFFERP (g->object) && g->charpos == PT)
16367 || (NILP (g->object)
16368 && (g->charpos == PT
16369 || (g->charpos == 0 && endpos - 1 == PT)));
16371 if (at_zv_p || exact_match_p)
16373 rc = CURSOR_MOVEMENT_SUCCESS;
16374 break;
16377 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16378 break;
16379 ++row;
16381 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16382 || row->continued_p)
16383 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16384 || (MATRIX_ROW_START_CHARPOS (row) == PT
16385 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16386 /* If we didn't find any candidate rows, or exited the
16387 loop before all the candidates were examined, signal
16388 to the caller that this method failed. */
16389 if (rc != CURSOR_MOVEMENT_SUCCESS
16390 && !(rv
16391 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16392 && !row->continued_p))
16393 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16394 else if (rv)
16395 rc = CURSOR_MOVEMENT_SUCCESS;
16397 else
16401 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16403 rc = CURSOR_MOVEMENT_SUCCESS;
16404 break;
16406 ++row;
16408 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16409 && MATRIX_ROW_START_CHARPOS (row) == PT
16410 && cursor_row_p (row));
16415 return rc;
16419 void
16420 set_vertical_scroll_bar (struct window *w)
16422 ptrdiff_t start, end, whole;
16424 /* Calculate the start and end positions for the current window.
16425 At some point, it would be nice to choose between scrollbars
16426 which reflect the whole buffer size, with special markers
16427 indicating narrowing, and scrollbars which reflect only the
16428 visible region.
16430 Note that mini-buffers sometimes aren't displaying any text. */
16431 if (!MINI_WINDOW_P (w)
16432 || (w == XWINDOW (minibuf_window)
16433 && NILP (echo_area_buffer[0])))
16435 struct buffer *buf = XBUFFER (w->contents);
16436 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16437 start = marker_position (w->start) - BUF_BEGV (buf);
16438 /* I don't think this is guaranteed to be right. For the
16439 moment, we'll pretend it is. */
16440 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16442 if (end < start)
16443 end = start;
16444 if (whole < (end - start))
16445 whole = end - start;
16447 else
16448 start = end = whole = 0;
16450 /* Indicate what this scroll bar ought to be displaying now. */
16451 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16452 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16453 (w, end - start, whole, start);
16457 void
16458 set_horizontal_scroll_bar (struct window *w)
16460 int start, end, whole, portion;
16462 if (!MINI_WINDOW_P (w)
16463 || (w == XWINDOW (minibuf_window)
16464 && NILP (echo_area_buffer[0])))
16466 struct buffer *b = XBUFFER (w->contents);
16467 struct buffer *old_buffer = NULL;
16468 struct it it;
16469 struct text_pos startp;
16471 if (b != current_buffer)
16473 old_buffer = current_buffer;
16474 set_buffer_internal (b);
16477 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16478 start_display (&it, w, startp);
16479 it.last_visible_x = INT_MAX;
16480 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16481 MOVE_TO_X | MOVE_TO_Y);
16482 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16483 window_box_height (w), -1,
16484 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16486 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16487 end = start + window_box_width (w, TEXT_AREA);
16488 portion = end - start;
16489 /* After enlarging a horizontally scrolled window such that it
16490 gets at least as wide as the text it contains, make sure that
16491 the thumb doesn't fill the entire scroll bar so we can still
16492 drag it back to see the entire text. */
16493 whole = max (whole, end);
16495 if (it.bidi_p)
16497 Lisp_Object pdir;
16499 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16500 if (EQ (pdir, Qright_to_left))
16502 start = whole - end;
16503 end = start + portion;
16507 if (old_buffer)
16508 set_buffer_internal (old_buffer);
16510 else
16511 start = end = whole = portion = 0;
16513 w->hscroll_whole = whole;
16515 /* Indicate what this scroll bar ought to be displaying now. */
16516 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16517 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16518 (w, portion, whole, start);
16522 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16523 selected_window is redisplayed.
16525 We can return without actually redisplaying the window if fonts has been
16526 changed on window's frame. In that case, redisplay_internal will retry.
16528 As one of the important parts of redisplaying a window, we need to
16529 decide whether the previous window-start position (stored in the
16530 window's w->start marker position) is still valid, and if it isn't,
16531 recompute it. Some details about that:
16533 . The previous window-start could be in a continuation line, in
16534 which case we need to recompute it when the window width
16535 changes. See compute_window_start_on_continuation_line and its
16536 call below.
16538 . The text that changed since last redisplay could include the
16539 previous window-start position. In that case, we try to salvage
16540 what we can from the current glyph matrix by calling
16541 try_scrolling, which see.
16543 . Some Emacs command could force us to use a specific window-start
16544 position by setting the window's force_start flag, or gently
16545 propose doing that by setting the window's optional_new_start
16546 flag. In these cases, we try using the specified start point if
16547 that succeeds (i.e. the window desired matrix is successfully
16548 recomputed, and point location is within the window). In case
16549 of optional_new_start, we first check if the specified start
16550 position is feasible, i.e. if it will allow point to be
16551 displayed in the window. If using the specified start point
16552 fails, e.g., if new fonts are needed to be loaded, we abort the
16553 redisplay cycle and leave it up to the next cycle to figure out
16554 things.
16556 . Note that the window's force_start flag is sometimes set by
16557 redisplay itself, when it decides that the previous window start
16558 point is fine and should be kept. Search for "goto force_start"
16559 below to see the details. Like the values of window-start
16560 specified outside of redisplay, these internally-deduced values
16561 are tested for feasibility, and ignored if found to be
16562 unfeasible.
16564 . Note that the function try_window, used to completely redisplay
16565 a window, accepts the window's start point as its argument.
16566 This is used several times in the redisplay code to control
16567 where the window start will be, according to user options such
16568 as scroll-conservatively, and also to ensure the screen line
16569 showing point will be fully (as opposed to partially) visible on
16570 display. */
16572 static void
16573 redisplay_window (Lisp_Object window, bool just_this_one_p)
16575 struct window *w = XWINDOW (window);
16576 struct frame *f = XFRAME (w->frame);
16577 struct buffer *buffer = XBUFFER (w->contents);
16578 struct buffer *old = current_buffer;
16579 struct text_pos lpoint, opoint, startp;
16580 bool update_mode_line;
16581 int tem;
16582 struct it it;
16583 /* Record it now because it's overwritten. */
16584 bool current_matrix_up_to_date_p = false;
16585 bool used_current_matrix_p = false;
16586 /* This is less strict than current_matrix_up_to_date_p.
16587 It indicates that the buffer contents and narrowing are unchanged. */
16588 bool buffer_unchanged_p = false;
16589 bool temp_scroll_step = false;
16590 ptrdiff_t count = SPECPDL_INDEX ();
16591 int rc;
16592 int centering_position = -1;
16593 bool last_line_misfit = false;
16594 ptrdiff_t beg_unchanged, end_unchanged;
16595 int frame_line_height, margin;
16596 bool use_desired_matrix;
16597 void *itdata = NULL;
16599 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16600 opoint = lpoint;
16602 #ifdef GLYPH_DEBUG
16603 *w->desired_matrix->method = 0;
16604 #endif
16606 if (!just_this_one_p
16607 && REDISPLAY_SOME_P ()
16608 && !w->redisplay
16609 && !w->update_mode_line
16610 && !f->face_change
16611 && !f->redisplay
16612 && !buffer->text->redisplay
16613 && BUF_PT (buffer) == w->last_point)
16614 return;
16616 /* Make sure that both W's markers are valid. */
16617 eassert (XMARKER (w->start)->buffer == buffer);
16618 eassert (XMARKER (w->pointm)->buffer == buffer);
16620 reconsider_clip_changes (w);
16621 frame_line_height = default_line_pixel_height (w);
16622 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16625 /* Has the mode line to be updated? */
16626 update_mode_line = (w->update_mode_line
16627 || update_mode_lines
16628 || buffer->clip_changed
16629 || buffer->prevent_redisplay_optimizations_p);
16631 if (!just_this_one_p)
16632 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16633 cleverly elsewhere. */
16634 w->must_be_updated_p = true;
16636 if (MINI_WINDOW_P (w))
16638 if (w == XWINDOW (echo_area_window)
16639 && !NILP (echo_area_buffer[0]))
16641 if (update_mode_line)
16642 /* We may have to update a tty frame's menu bar or a
16643 tool-bar. Example `M-x C-h C-h C-g'. */
16644 goto finish_menu_bars;
16645 else
16646 /* We've already displayed the echo area glyphs in this window. */
16647 goto finish_scroll_bars;
16649 else if ((w != XWINDOW (minibuf_window)
16650 || minibuf_level == 0)
16651 /* When buffer is nonempty, redisplay window normally. */
16652 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16653 /* Quail displays non-mini buffers in minibuffer window.
16654 In that case, redisplay the window normally. */
16655 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16657 /* W is a mini-buffer window, but it's not active, so clear
16658 it. */
16659 int yb = window_text_bottom_y (w);
16660 struct glyph_row *row;
16661 int y;
16663 for (y = 0, row = w->desired_matrix->rows;
16664 y < yb;
16665 y += row->height, ++row)
16666 blank_row (w, row, y);
16667 goto finish_scroll_bars;
16670 clear_glyph_matrix (w->desired_matrix);
16673 /* Otherwise set up data on this window; select its buffer and point
16674 value. */
16675 /* Really select the buffer, for the sake of buffer-local
16676 variables. */
16677 set_buffer_internal_1 (XBUFFER (w->contents));
16679 current_matrix_up_to_date_p
16680 = (w->window_end_valid
16681 && !current_buffer->clip_changed
16682 && !current_buffer->prevent_redisplay_optimizations_p
16683 && !window_outdated (w)
16684 && !hscrolling_current_line_p (w));
16686 beg_unchanged = BEG_UNCHANGED;
16687 end_unchanged = END_UNCHANGED;
16689 SET_TEXT_POS (opoint, PT, PT_BYTE);
16691 specbind (Qinhibit_point_motion_hooks, Qt);
16693 buffer_unchanged_p
16694 = (w->window_end_valid
16695 && !current_buffer->clip_changed
16696 && !window_outdated (w));
16698 /* When windows_or_buffers_changed is non-zero, we can't rely
16699 on the window end being valid, so set it to zero there. */
16700 if (windows_or_buffers_changed)
16702 /* If window starts on a continuation line, maybe adjust the
16703 window start in case the window's width changed. */
16704 if (XMARKER (w->start)->buffer == current_buffer)
16705 compute_window_start_on_continuation_line (w);
16707 w->window_end_valid = false;
16708 /* If so, we also can't rely on current matrix
16709 and should not fool try_cursor_movement below. */
16710 current_matrix_up_to_date_p = false;
16713 /* Some sanity checks. */
16714 CHECK_WINDOW_END (w);
16715 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16716 emacs_abort ();
16717 if (BYTEPOS (opoint) < CHARPOS (opoint))
16718 emacs_abort ();
16720 if (mode_line_update_needed (w))
16721 update_mode_line = true;
16723 /* Point refers normally to the selected window. For any other
16724 window, set up appropriate value. */
16725 if (!EQ (window, selected_window))
16727 ptrdiff_t new_pt = marker_position (w->pointm);
16728 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16730 if (new_pt < BEGV)
16732 new_pt = BEGV;
16733 new_pt_byte = BEGV_BYTE;
16734 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16736 else if (new_pt > (ZV - 1))
16738 new_pt = ZV;
16739 new_pt_byte = ZV_BYTE;
16740 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16743 /* We don't use SET_PT so that the point-motion hooks don't run. */
16744 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16747 /* If any of the character widths specified in the display table
16748 have changed, invalidate the width run cache. It's true that
16749 this may be a bit late to catch such changes, but the rest of
16750 redisplay goes (non-fatally) haywire when the display table is
16751 changed, so why should we worry about doing any better? */
16752 if (current_buffer->width_run_cache
16753 || (current_buffer->base_buffer
16754 && current_buffer->base_buffer->width_run_cache))
16756 struct Lisp_Char_Table *disptab = buffer_display_table ();
16758 if (! disptab_matches_widthtab
16759 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16761 struct buffer *buf = current_buffer;
16763 if (buf->base_buffer)
16764 buf = buf->base_buffer;
16765 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16766 recompute_width_table (current_buffer, disptab);
16770 /* If window-start is screwed up, choose a new one. */
16771 if (XMARKER (w->start)->buffer != current_buffer)
16772 goto recenter;
16774 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16776 /* If someone specified a new starting point but did not insist,
16777 check whether it can be used. */
16778 if ((w->optional_new_start || window_frozen_p (w))
16779 && CHARPOS (startp) >= BEGV
16780 && CHARPOS (startp) <= ZV)
16782 ptrdiff_t it_charpos;
16784 w->optional_new_start = false;
16785 start_display (&it, w, startp);
16786 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16787 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16788 /* Record IT's position now, since line_bottom_y might change
16789 that. */
16790 it_charpos = IT_CHARPOS (it);
16791 /* Make sure we set the force_start flag only if the cursor row
16792 will be fully visible. Otherwise, the code under force_start
16793 label below will try to move point back into view, which is
16794 not what the code which sets optional_new_start wants. */
16795 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16796 && !w->force_start)
16798 if (it_charpos == PT)
16799 w->force_start = true;
16800 /* IT may overshoot PT if text at PT is invisible. */
16801 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16802 w->force_start = true;
16803 #ifdef GLYPH_DEBUG
16804 if (w->force_start)
16806 if (window_frozen_p (w))
16807 debug_method_add (w, "set force_start from frozen window start");
16808 else
16809 debug_method_add (w, "set force_start from optional_new_start");
16811 #endif
16815 force_start:
16817 /* Handle case where place to start displaying has been specified,
16818 unless the specified location is outside the accessible range. */
16819 if (w->force_start)
16821 /* We set this later on if we have to adjust point. */
16822 int new_vpos = -1;
16824 w->force_start = false;
16825 w->vscroll = 0;
16826 w->window_end_valid = false;
16828 /* Forget any recorded base line for line number display. */
16829 if (!buffer_unchanged_p)
16830 w->base_line_number = 0;
16832 /* Redisplay the mode line. Select the buffer properly for that.
16833 Also, run the hook window-scroll-functions
16834 because we have scrolled. */
16835 /* Note, we do this after clearing force_start because
16836 if there's an error, it is better to forget about force_start
16837 than to get into an infinite loop calling the hook functions
16838 and having them get more errors. */
16839 if (!update_mode_line
16840 || ! NILP (Vwindow_scroll_functions))
16842 update_mode_line = true;
16843 w->update_mode_line = true;
16844 startp = run_window_scroll_functions (window, startp);
16847 if (CHARPOS (startp) < BEGV)
16848 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16849 else if (CHARPOS (startp) > ZV)
16850 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16852 /* Redisplay, then check if cursor has been set during the
16853 redisplay. Give up if new fonts were loaded. */
16854 /* We used to issue a CHECK_MARGINS argument to try_window here,
16855 but this causes scrolling to fail when point begins inside
16856 the scroll margin (bug#148) -- cyd */
16857 if (!try_window (window, startp, 0))
16859 w->force_start = true;
16860 clear_glyph_matrix (w->desired_matrix);
16861 goto need_larger_matrices;
16864 if (w->cursor.vpos < 0)
16866 /* If point does not appear, try to move point so it does
16867 appear. The desired matrix has been built above, so we
16868 can use it here. First see if point is in invisible
16869 text, and if so, move it to the first visible buffer
16870 position past that. */
16871 struct glyph_row *r = NULL;
16872 Lisp_Object invprop =
16873 get_char_property_and_overlay (make_number (PT), Qinvisible,
16874 Qnil, NULL);
16876 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16878 ptrdiff_t alt_pt;
16879 Lisp_Object invprop_end =
16880 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16881 Qnil, Qnil);
16883 if (NATNUMP (invprop_end))
16884 alt_pt = XFASTINT (invprop_end);
16885 else
16886 alt_pt = ZV;
16887 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16888 NULL, 0);
16890 if (r)
16891 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16892 else /* Give up and just move to the middle of the window. */
16893 new_vpos = window_box_height (w) / 2;
16896 if (!cursor_row_fully_visible_p (w, false, false))
16898 /* Point does appear, but on a line partly visible at end of window.
16899 Move it back to a fully-visible line. */
16900 new_vpos = window_box_height (w);
16901 /* But if window_box_height suggests a Y coordinate that is
16902 not less than we already have, that line will clearly not
16903 be fully visible, so give up and scroll the display.
16904 This can happen when the default face uses a font whose
16905 dimensions are different from the frame's default
16906 font. */
16907 if (new_vpos >= w->cursor.y)
16909 w->cursor.vpos = -1;
16910 clear_glyph_matrix (w->desired_matrix);
16911 goto try_to_scroll;
16914 else if (w->cursor.vpos >= 0)
16916 /* Some people insist on not letting point enter the scroll
16917 margin, even though this part handles windows that didn't
16918 scroll at all. */
16919 int pixel_margin = margin * frame_line_height;
16920 bool header_line = window_wants_header_line (w);
16922 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16923 below, which finds the row to move point to, advances by
16924 the Y coordinate of the _next_ row, see the definition of
16925 MATRIX_ROW_BOTTOM_Y. */
16926 if (w->cursor.vpos < margin + header_line)
16928 w->cursor.vpos = -1;
16929 clear_glyph_matrix (w->desired_matrix);
16930 goto try_to_scroll;
16932 else
16934 int window_height = window_box_height (w);
16936 if (header_line)
16937 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16938 if (w->cursor.y >= window_height - pixel_margin)
16940 w->cursor.vpos = -1;
16941 clear_glyph_matrix (w->desired_matrix);
16942 goto try_to_scroll;
16947 /* If we need to move point for either of the above reasons,
16948 now actually do it. */
16949 if (new_vpos >= 0)
16951 struct glyph_row *row;
16953 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16954 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16955 ++row;
16957 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16958 MATRIX_ROW_START_BYTEPOS (row));
16960 if (w != XWINDOW (selected_window))
16961 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16962 else if (current_buffer == old)
16963 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16965 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16967 /* Re-run pre-redisplay-function so it can update the region
16968 according to the new position of point. */
16969 /* Other than the cursor, w's redisplay is done so we can set its
16970 redisplay to false. Also the buffer's redisplay can be set to
16971 false, since propagate_buffer_redisplay should have already
16972 propagated its info to `w' anyway. */
16973 w->redisplay = false;
16974 XBUFFER (w->contents)->text->redisplay = false;
16975 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16977 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16978 || ((EQ (Vdisplay_line_numbers, Qrelative)
16979 || EQ (Vdisplay_line_numbers, Qvisual))
16980 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16982 /* Either pre-redisplay-function made changes (e.g. move
16983 the region), or we moved point in a window that is
16984 under display-line-numbers = relative mode. We need
16985 another round of redisplay. */
16986 clear_glyph_matrix (w->desired_matrix);
16987 if (!try_window (window, startp, 0))
16988 goto need_larger_matrices;
16991 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16993 clear_glyph_matrix (w->desired_matrix);
16994 goto try_to_scroll;
16997 #ifdef GLYPH_DEBUG
16998 debug_method_add (w, "forced window start");
16999 #endif
17000 goto done;
17003 /* Handle case where text has not changed, only point, and it has
17004 not moved off the frame, and we are not retrying after hscroll.
17005 (current_matrix_up_to_date_p is true when retrying.) */
17006 if (current_matrix_up_to_date_p
17007 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
17008 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
17010 switch (rc)
17012 case CURSOR_MOVEMENT_SUCCESS:
17013 used_current_matrix_p = true;
17014 goto done;
17016 case CURSOR_MOVEMENT_MUST_SCROLL:
17017 goto try_to_scroll;
17019 default:
17020 emacs_abort ();
17023 /* If current starting point was originally the beginning of a line
17024 but no longer is, find a new starting point. */
17025 else if (w->start_at_line_beg
17026 && !(CHARPOS (startp) <= BEGV
17027 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
17029 #ifdef GLYPH_DEBUG
17030 debug_method_add (w, "recenter 1");
17031 #endif
17032 goto recenter;
17035 /* Try scrolling with try_window_id. Value is > 0 if update has
17036 been done, it is -1 if we know that the same window start will
17037 not work. It is 0 if unsuccessful for some other reason. */
17038 else if ((tem = try_window_id (w)) != 0)
17040 #ifdef GLYPH_DEBUG
17041 debug_method_add (w, "try_window_id %d", tem);
17042 #endif
17044 if (f->fonts_changed)
17045 goto need_larger_matrices;
17046 if (tem > 0)
17047 goto done;
17049 /* Otherwise try_window_id has returned -1 which means that we
17050 don't want the alternative below this comment to execute. */
17052 else if (CHARPOS (startp) >= BEGV
17053 && CHARPOS (startp) <= ZV
17054 && PT >= CHARPOS (startp)
17055 && (CHARPOS (startp) < ZV
17056 /* Avoid starting at end of buffer. */
17057 || CHARPOS (startp) == BEGV
17058 || !window_outdated (w)))
17060 int d1, d2, d5, d6;
17061 int rtop, rbot;
17063 /* If first window line is a continuation line, and window start
17064 is inside the modified region, but the first change is before
17065 current window start, we must select a new window start.
17067 However, if this is the result of a down-mouse event (e.g. by
17068 extending the mouse-drag-overlay), we don't want to select a
17069 new window start, since that would change the position under
17070 the mouse, resulting in an unwanted mouse-movement rather
17071 than a simple mouse-click. */
17072 if (!w->start_at_line_beg
17073 && NILP (do_mouse_tracking)
17074 && CHARPOS (startp) > BEGV
17075 && CHARPOS (startp) > BEG + beg_unchanged
17076 && CHARPOS (startp) <= Z - end_unchanged
17077 /* Even if w->start_at_line_beg is nil, a new window may
17078 start at a line_beg, since that's how set_buffer_window
17079 sets it. So, we need to check the return value of
17080 compute_window_start_on_continuation_line. (See also
17081 bug#197). */
17082 && XMARKER (w->start)->buffer == current_buffer
17083 && compute_window_start_on_continuation_line (w)
17084 /* It doesn't make sense to force the window start like we
17085 do at label force_start if it is already known that point
17086 will not be fully visible in the resulting window, because
17087 doing so will move point from its correct position
17088 instead of scrolling the window to bring point into view.
17089 See bug#9324. */
17090 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17091 /* A very tall row could need more than the window height,
17092 in which case we accept that it is partially visible. */
17093 && (rtop != 0) == (rbot != 0))
17095 w->force_start = true;
17096 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17097 #ifdef GLYPH_DEBUG
17098 debug_method_add (w, "recomputed window start in continuation line");
17099 #endif
17100 goto force_start;
17103 #ifdef GLYPH_DEBUG
17104 debug_method_add (w, "same window start");
17105 #endif
17107 /* Try to redisplay starting at same place as before.
17108 If point has not moved off frame, accept the results. */
17109 if (!current_matrix_up_to_date_p
17110 /* Don't use try_window_reusing_current_matrix in this case
17111 because a window scroll function can have changed the
17112 buffer. */
17113 || !NILP (Vwindow_scroll_functions)
17114 || MINI_WINDOW_P (w)
17115 || !(used_current_matrix_p
17116 = try_window_reusing_current_matrix (w)))
17118 IF_DEBUG (debug_method_add (w, "1"));
17119 clear_glyph_matrix (w->desired_matrix);
17120 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17121 /* -1 means we need to scroll.
17122 0 means we need new matrices, but fonts_changed
17123 is set in that case, so we will detect it below. */
17124 goto try_to_scroll;
17127 if (f->fonts_changed)
17128 goto need_larger_matrices;
17130 if (w->cursor.vpos >= 0)
17132 if (!just_this_one_p
17133 || current_buffer->clip_changed
17134 || BEG_UNCHANGED < CHARPOS (startp))
17135 /* Forget any recorded base line for line number display. */
17136 w->base_line_number = 0;
17138 if (!cursor_row_fully_visible_p (w, true, false))
17140 clear_glyph_matrix (w->desired_matrix);
17141 last_line_misfit = true;
17143 /* Drop through and scroll. */
17144 else
17145 goto done;
17147 else
17148 clear_glyph_matrix (w->desired_matrix);
17151 try_to_scroll:
17153 /* Redisplay the mode line. Select the buffer properly for that. */
17154 if (!update_mode_line)
17156 update_mode_line = true;
17157 w->update_mode_line = true;
17160 /* Try to scroll by specified few lines. */
17161 if ((scroll_conservatively
17162 || emacs_scroll_step
17163 || temp_scroll_step
17164 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17165 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17166 && CHARPOS (startp) >= BEGV
17167 && CHARPOS (startp) <= ZV)
17169 /* The function returns -1 if new fonts were loaded, 1 if
17170 successful, 0 if not successful. */
17171 int ss = try_scrolling (window, just_this_one_p,
17172 scroll_conservatively,
17173 emacs_scroll_step,
17174 temp_scroll_step, last_line_misfit);
17175 switch (ss)
17177 case SCROLLING_SUCCESS:
17178 goto done;
17180 case SCROLLING_NEED_LARGER_MATRICES:
17181 goto need_larger_matrices;
17183 case SCROLLING_FAILED:
17184 break;
17186 default:
17187 emacs_abort ();
17191 /* Finally, just choose a place to start which positions point
17192 according to user preferences. */
17194 recenter:
17196 #ifdef GLYPH_DEBUG
17197 debug_method_add (w, "recenter");
17198 #endif
17200 /* Forget any previously recorded base line for line number display. */
17201 if (!buffer_unchanged_p)
17202 w->base_line_number = 0;
17204 /* Determine the window start relative to point. */
17205 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17206 it.current_y = it.last_visible_y;
17207 if (centering_position < 0)
17209 ptrdiff_t margin_pos = CHARPOS (startp);
17210 Lisp_Object aggressive;
17211 bool scrolling_up;
17213 /* If there is a scroll margin at the top of the window, find
17214 its character position. */
17215 if (margin
17216 /* Cannot call start_display if startp is not in the
17217 accessible region of the buffer. This can happen when we
17218 have just switched to a different buffer and/or changed
17219 its restriction. In that case, startp is initialized to
17220 the character position 1 (BEGV) because we did not yet
17221 have chance to display the buffer even once. */
17222 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17224 struct it it1;
17225 void *it1data = NULL;
17227 SAVE_IT (it1, it, it1data);
17228 start_display (&it1, w, startp);
17229 move_it_vertically (&it1, margin * frame_line_height);
17230 margin_pos = IT_CHARPOS (it1);
17231 RESTORE_IT (&it, &it, it1data);
17233 scrolling_up = PT > margin_pos;
17234 aggressive =
17235 scrolling_up
17236 ? BVAR (current_buffer, scroll_up_aggressively)
17237 : BVAR (current_buffer, scroll_down_aggressively);
17239 if (!MINI_WINDOW_P (w)
17240 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17242 int pt_offset = 0;
17244 /* Setting scroll-conservatively overrides
17245 scroll-*-aggressively. */
17246 if (!scroll_conservatively && NUMBERP (aggressive))
17248 double float_amount = XFLOATINT (aggressive);
17250 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17251 if (pt_offset == 0 && float_amount > 0)
17252 pt_offset = 1;
17253 if (pt_offset && margin > 0)
17254 margin -= 1;
17256 /* Compute how much to move the window start backward from
17257 point so that point will be displayed where the user
17258 wants it. */
17259 if (scrolling_up)
17261 centering_position = it.last_visible_y;
17262 if (pt_offset)
17263 centering_position -= pt_offset;
17264 centering_position -=
17265 (frame_line_height * (1 + margin + last_line_misfit)
17266 + WINDOW_HEADER_LINE_HEIGHT (w));
17267 /* Don't let point enter the scroll margin near top of
17268 the window. */
17269 if (centering_position < margin * frame_line_height)
17270 centering_position = margin * frame_line_height;
17272 else
17273 centering_position = margin * frame_line_height + pt_offset;
17275 else
17276 /* Set the window start half the height of the window backward
17277 from point. */
17278 centering_position = window_box_height (w) / 2;
17280 move_it_vertically_backward (&it, centering_position);
17282 eassert (IT_CHARPOS (it) >= BEGV);
17284 /* The function move_it_vertically_backward may move over more
17285 than the specified y-distance. If it->w is small, e.g. a
17286 mini-buffer window, we may end up in front of the window's
17287 display area. Start displaying at the start of the line
17288 containing PT in this case. */
17289 if (it.current_y <= 0)
17291 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17292 move_it_vertically_backward (&it, 0);
17293 it.current_y = 0;
17296 it.current_x = it.hpos = 0;
17298 /* Set the window start position here explicitly, to avoid an
17299 infinite loop in case the functions in window-scroll-functions
17300 get errors. */
17301 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17303 /* Run scroll hooks. */
17304 startp = run_window_scroll_functions (window, it.current.pos);
17306 /* We invoke try_window and try_window_reusing_current_matrix below,
17307 and they manipulate the bidi cache. Save and restore the cache
17308 state of our iterator, so we could continue using it after that. */
17309 itdata = bidi_shelve_cache ();
17311 /* Redisplay the window. */
17312 use_desired_matrix = false;
17313 if (!current_matrix_up_to_date_p
17314 || windows_or_buffers_changed
17315 || f->cursor_type_changed
17316 /* Don't use try_window_reusing_current_matrix in this case
17317 because it can have changed the buffer. */
17318 || !NILP (Vwindow_scroll_functions)
17319 || !just_this_one_p
17320 || MINI_WINDOW_P (w)
17321 || !(used_current_matrix_p
17322 = try_window_reusing_current_matrix (w)))
17323 use_desired_matrix = (try_window (window, startp, 0) == 1);
17325 bidi_unshelve_cache (itdata, false);
17327 /* If new fonts have been loaded (due to fontsets), give up. We
17328 have to start a new redisplay since we need to re-adjust glyph
17329 matrices. */
17330 if (f->fonts_changed)
17331 goto need_larger_matrices;
17333 /* If cursor did not appear assume that the middle of the window is
17334 in the first line of the window. Do it again with the next line.
17335 (Imagine a window of height 100, displaying two lines of height
17336 60. Moving back 50 from it->last_visible_y will end in the first
17337 line.) */
17338 if (w->cursor.vpos < 0)
17340 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17342 clear_glyph_matrix (w->desired_matrix);
17343 move_it_by_lines (&it, 1);
17344 try_window (window, it.current.pos, 0);
17346 else if (PT < IT_CHARPOS (it))
17348 clear_glyph_matrix (w->desired_matrix);
17349 move_it_by_lines (&it, -1);
17350 try_window (window, it.current.pos, 0);
17352 else if (scroll_conservatively > SCROLL_LIMIT
17353 && (it.method == GET_FROM_STRING
17354 || overlay_touches_p (IT_CHARPOS (it)))
17355 && IT_CHARPOS (it) < ZV)
17357 /* If the window starts with a before-string that spans more
17358 than one screen line, using that position to display the
17359 window might fail to bring point into the view, because
17360 start_display will always start by displaying the string,
17361 whereas the code above determines where to set w->start
17362 by the buffer position of the place where it takes screen
17363 coordinates. Try to recover by finding the next screen
17364 line that displays buffer text. */
17365 ptrdiff_t pos0 = IT_CHARPOS (it);
17367 clear_glyph_matrix (w->desired_matrix);
17368 do {
17369 move_it_by_lines (&it, 1);
17370 } while (IT_CHARPOS (it) == pos0);
17371 try_window (window, it.current.pos, 0);
17373 else
17375 /* Not much we can do about it. */
17379 /* Consider the following case: Window starts at BEGV, there is
17380 invisible, intangible text at BEGV, so that display starts at
17381 some point START > BEGV. It can happen that we are called with
17382 PT somewhere between BEGV and START. Try to handle that case,
17383 and similar ones. */
17384 if (w->cursor.vpos < 0)
17386 /* Prefer the desired matrix to the current matrix, if possible,
17387 in the fallback calculations below. This is because using
17388 the current matrix might completely goof, e.g. if its first
17389 row is after point. */
17390 struct glyph_matrix *matrix =
17391 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17392 /* First, try locating the proper glyph row for PT. */
17393 struct glyph_row *row =
17394 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17396 /* Sometimes point is at the beginning of invisible text that is
17397 before the 1st character displayed in the row. In that case,
17398 row_containing_pos fails to find the row, because no glyphs
17399 with appropriate buffer positions are present in the row.
17400 Therefore, we next try to find the row which shows the 1st
17401 position after the invisible text. */
17402 if (!row)
17404 Lisp_Object val =
17405 get_char_property_and_overlay (make_number (PT), Qinvisible,
17406 Qnil, NULL);
17408 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17410 ptrdiff_t alt_pos;
17411 Lisp_Object invis_end =
17412 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17413 Qnil, Qnil);
17415 if (NATNUMP (invis_end))
17416 alt_pos = XFASTINT (invis_end);
17417 else
17418 alt_pos = ZV;
17419 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17422 /* Finally, fall back on the first row of the window after the
17423 header line (if any). This is slightly better than not
17424 displaying the cursor at all. */
17425 if (!row)
17427 row = matrix->rows;
17428 if (row->mode_line_p)
17429 ++row;
17431 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17434 if (!cursor_row_fully_visible_p (w, false, false))
17436 /* If vscroll is enabled, disable it and try again. */
17437 if (w->vscroll)
17439 w->vscroll = 0;
17440 clear_glyph_matrix (w->desired_matrix);
17441 goto recenter;
17444 /* Users who set scroll-conservatively to a large number want
17445 point just above/below the scroll margin. If we ended up
17446 with point's row partially visible, move the window start to
17447 make that row fully visible and out of the margin. */
17448 if (scroll_conservatively > SCROLL_LIMIT)
17450 int window_total_lines
17451 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17452 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17454 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17455 clear_glyph_matrix (w->desired_matrix);
17456 if (1 == try_window (window, it.current.pos,
17457 TRY_WINDOW_CHECK_MARGINS))
17458 goto done;
17461 /* If centering point failed to make the whole line visible,
17462 put point at the top instead. That has to make the whole line
17463 visible, if it can be done. */
17464 if (centering_position == 0)
17465 goto done;
17467 clear_glyph_matrix (w->desired_matrix);
17468 centering_position = 0;
17469 goto recenter;
17472 done:
17474 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17475 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17476 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17478 /* Display the mode line, if we must. */
17479 if ((update_mode_line
17480 /* If window not full width, must redo its mode line
17481 if (a) the window to its side is being redone and
17482 (b) we do a frame-based redisplay. This is a consequence
17483 of how inverted lines are drawn in frame-based redisplay. */
17484 || (!just_this_one_p
17485 && !FRAME_WINDOW_P (f)
17486 && !WINDOW_FULL_WIDTH_P (w))
17487 /* Line number to display. */
17488 || w->base_line_pos > 0
17489 /* Column number is displayed and different from the one displayed. */
17490 || (w->column_number_displayed != -1
17491 && (w->column_number_displayed != current_column ())))
17492 /* This means that the window has a mode line. */
17493 && (window_wants_mode_line (w)
17494 || window_wants_header_line (w)))
17497 display_mode_lines (w);
17499 /* If mode line height has changed, arrange for a thorough
17500 immediate redisplay using the correct mode line height. */
17501 if (window_wants_mode_line (w)
17502 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17504 f->fonts_changed = true;
17505 w->mode_line_height = -1;
17506 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17507 = DESIRED_MODE_LINE_HEIGHT (w);
17510 /* If header line height has changed, arrange for a thorough
17511 immediate redisplay using the correct header line height. */
17512 if (window_wants_header_line (w)
17513 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17515 f->fonts_changed = true;
17516 w->header_line_height = -1;
17517 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17518 = DESIRED_HEADER_LINE_HEIGHT (w);
17521 if (f->fonts_changed)
17522 goto need_larger_matrices;
17525 if (!line_number_displayed && w->base_line_pos != -1)
17527 w->base_line_pos = 0;
17528 w->base_line_number = 0;
17531 finish_menu_bars:
17533 /* When we reach a frame's selected window, redo the frame's menu
17534 bar and the frame's title. */
17535 if (update_mode_line
17536 && EQ (FRAME_SELECTED_WINDOW (f), window))
17538 bool redisplay_menu_p;
17540 if (FRAME_WINDOW_P (f))
17542 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17543 || defined (HAVE_NS) || defined (USE_GTK)
17544 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17545 #else
17546 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17547 #endif
17549 else
17550 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17552 if (redisplay_menu_p)
17553 display_menu_bar (w);
17555 #ifdef HAVE_WINDOW_SYSTEM
17556 if (FRAME_WINDOW_P (f))
17558 #if defined (USE_GTK) || defined (HAVE_NS)
17559 if (FRAME_EXTERNAL_TOOL_BAR (f))
17560 redisplay_tool_bar (f);
17561 #else
17562 if (WINDOWP (f->tool_bar_window)
17563 && (FRAME_TOOL_BAR_LINES (f) > 0
17564 || !NILP (Vauto_resize_tool_bars))
17565 && redisplay_tool_bar (f))
17566 ignore_mouse_drag_p = true;
17567 #endif
17569 x_consider_frame_title (w->frame);
17570 #endif
17573 #ifdef HAVE_WINDOW_SYSTEM
17574 if (FRAME_WINDOW_P (f)
17575 && update_window_fringes (w, (just_this_one_p
17576 || (!used_current_matrix_p && !overlay_arrow_seen)
17577 || w->pseudo_window_p)))
17579 update_begin (f);
17580 block_input ();
17581 if (draw_window_fringes (w, true))
17583 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17584 x_draw_right_divider (w);
17585 else
17586 x_draw_vertical_border (w);
17588 unblock_input ();
17589 update_end (f);
17592 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17593 x_draw_bottom_divider (w);
17594 #endif /* HAVE_WINDOW_SYSTEM */
17596 /* We go to this label, with fonts_changed set, if it is
17597 necessary to try again using larger glyph matrices.
17598 We have to redeem the scroll bar even in this case,
17599 because the loop in redisplay_internal expects that. */
17600 need_larger_matrices:
17602 finish_scroll_bars:
17604 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17606 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17607 /* Set the thumb's position and size. */
17608 set_vertical_scroll_bar (w);
17610 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17611 /* Set the thumb's position and size. */
17612 set_horizontal_scroll_bar (w);
17614 /* Note that we actually used the scroll bar attached to this
17615 window, so it shouldn't be deleted at the end of redisplay. */
17616 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17617 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17620 /* Restore current_buffer and value of point in it. The window
17621 update may have changed the buffer, so first make sure `opoint'
17622 is still valid (Bug#6177). */
17623 if (CHARPOS (opoint) < BEGV)
17624 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17625 else if (CHARPOS (opoint) > ZV)
17626 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17627 else
17628 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17630 set_buffer_internal_1 (old);
17631 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17632 shorter. This can be caused by log truncation in *Messages*. */
17633 if (CHARPOS (lpoint) <= ZV)
17634 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17636 unbind_to (count, Qnil);
17640 /* Build the complete desired matrix of WINDOW with a window start
17641 buffer position POS.
17643 Value is 1 if successful. It is zero if fonts were loaded during
17644 redisplay which makes re-adjusting glyph matrices necessary, and -1
17645 if point would appear in the scroll margins.
17646 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17647 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17648 set in FLAGS.) */
17651 try_window (Lisp_Object window, struct text_pos pos, int flags)
17653 struct window *w = XWINDOW (window);
17654 struct it it;
17655 struct glyph_row *last_text_row = NULL;
17656 struct frame *f = XFRAME (w->frame);
17657 int cursor_vpos = w->cursor.vpos;
17659 /* Make POS the new window start. */
17660 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17662 /* Mark cursor position as unknown. No overlay arrow seen. */
17663 w->cursor.vpos = -1;
17664 overlay_arrow_seen = false;
17666 /* Initialize iterator and info to start at POS. */
17667 start_display (&it, w, pos);
17668 it.glyph_row->reversed_p = false;
17670 /* Display all lines of W. */
17671 while (it.current_y < it.last_visible_y)
17673 if (display_line (&it, cursor_vpos))
17674 last_text_row = it.glyph_row - 1;
17675 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17676 return 0;
17679 /* Save the character position of 'it' before we call
17680 'start_display' again. */
17681 ptrdiff_t it_charpos = IT_CHARPOS (it);
17683 /* Don't let the cursor end in the scroll margins. */
17684 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17685 && !MINI_WINDOW_P (w))
17687 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17688 start_display (&it, w, pos);
17690 if ((w->cursor.y >= 0 /* not vscrolled */
17691 && w->cursor.y < this_scroll_margin
17692 && CHARPOS (pos) > BEGV
17693 && it_charpos < ZV)
17694 /* rms: considering make_cursor_line_fully_visible_p here
17695 seems to give wrong results. We don't want to recenter
17696 when the last line is partly visible, we want to allow
17697 that case to be handled in the usual way. */
17698 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17699 - this_scroll_margin - 1))
17701 w->cursor.vpos = -1;
17702 clear_glyph_matrix (w->desired_matrix);
17703 return -1;
17707 /* If bottom moved off end of frame, change mode line percentage. */
17708 if (w->window_end_pos <= 0 && Z != it_charpos)
17709 w->update_mode_line = true;
17711 /* Set window_end_pos to the offset of the last character displayed
17712 on the window from the end of current_buffer. Set
17713 window_end_vpos to its row number. */
17714 if (last_text_row)
17716 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17717 adjust_window_ends (w, last_text_row, false);
17718 eassert
17719 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17720 w->window_end_vpos)));
17722 else
17724 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17725 w->window_end_pos = Z - ZV;
17726 w->window_end_vpos = 0;
17729 /* But that is not valid info until redisplay finishes. */
17730 w->window_end_valid = false;
17731 return 1;
17736 /************************************************************************
17737 Window redisplay reusing current matrix when buffer has not changed
17738 ************************************************************************/
17740 /* Try redisplay of window W showing an unchanged buffer with a
17741 different window start than the last time it was displayed by
17742 reusing its current matrix. Value is true if successful.
17743 W->start is the new window start. */
17745 static bool
17746 try_window_reusing_current_matrix (struct window *w)
17748 struct frame *f = XFRAME (w->frame);
17749 struct glyph_row *bottom_row;
17750 struct it it;
17751 struct run run;
17752 struct text_pos start, new_start;
17753 int nrows_scrolled, i;
17754 struct glyph_row *last_text_row;
17755 struct glyph_row *last_reused_text_row;
17756 struct glyph_row *start_row;
17757 int start_vpos, min_y, max_y;
17759 #ifdef GLYPH_DEBUG
17760 if (inhibit_try_window_reusing)
17761 return false;
17762 #endif
17764 if (/* This function doesn't handle terminal frames. */
17765 !FRAME_WINDOW_P (f)
17766 /* Don't try to reuse the display if windows have been split
17767 or such. */
17768 || windows_or_buffers_changed
17769 || f->cursor_type_changed
17770 /* This function cannot handle buffers where the overlay arrow
17771 is shown on the fringes, because if the arrow position
17772 changes, we cannot just reuse the current matrix. */
17773 || overlay_arrow_in_current_buffer_p ())
17774 return false;
17776 /* Can't do this if showing trailing whitespace. */
17777 if (!NILP (Vshow_trailing_whitespace))
17778 return false;
17780 /* If top-line visibility has changed, give up. */
17781 if (window_wants_header_line (w)
17782 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17783 return false;
17785 /* Give up if old or new display is scrolled vertically. We could
17786 make this function handle this, but right now it doesn't. */
17787 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17788 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17789 return false;
17791 /* Clear the desired matrix for the display below. */
17792 clear_glyph_matrix (w->desired_matrix);
17794 /* Give up if line numbers are being displayed, because reusing the
17795 current matrix might use the wrong width for line-number
17796 display. */
17797 if (!NILP (Vdisplay_line_numbers))
17798 return false;
17800 /* The variable new_start now holds the new window start. The old
17801 start `start' can be determined from the current matrix. */
17802 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17803 start = start_row->minpos;
17804 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17806 if (CHARPOS (new_start) <= CHARPOS (start))
17808 /* Don't use this method if the display starts with an ellipsis
17809 displayed for invisible text. It's not easy to handle that case
17810 below, and it's certainly not worth the effort since this is
17811 not a frequent case. */
17812 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17813 return false;
17815 IF_DEBUG (debug_method_add (w, "twu1"));
17817 /* Display up to a row that can be reused. The variable
17818 last_text_row is set to the last row displayed that displays
17819 text. Note that it.vpos == 0 if or if not there is a
17820 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17821 start_display (&it, w, new_start);
17822 w->cursor.vpos = -1;
17823 last_text_row = last_reused_text_row = NULL;
17825 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17827 /* If we have reached into the characters in the START row,
17828 that means the line boundaries have changed. So we
17829 can't start copying with the row START. Maybe it will
17830 work to start copying with the following row. */
17831 while (IT_CHARPOS (it) > CHARPOS (start))
17833 /* Advance to the next row as the "start". */
17834 start_row++;
17835 start = start_row->minpos;
17836 /* If there are no more rows to try, or just one, give up. */
17837 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17838 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17839 || CHARPOS (start) == ZV)
17841 clear_glyph_matrix (w->desired_matrix);
17842 return false;
17845 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17847 /* If we have reached alignment, we can copy the rest of the
17848 rows. */
17849 if (IT_CHARPOS (it) == CHARPOS (start)
17850 /* Don't accept "alignment" inside a display vector,
17851 since start_row could have started in the middle of
17852 that same display vector (thus their character
17853 positions match), and we have no way of telling if
17854 that is the case. */
17855 && it.current.dpvec_index < 0)
17856 break;
17858 it.glyph_row->reversed_p = false;
17859 if (display_line (&it, -1))
17860 last_text_row = it.glyph_row - 1;
17864 /* A value of current_y < last_visible_y means that we stopped
17865 at the previous window start, which in turn means that we
17866 have at least one reusable row. */
17867 if (it.current_y < it.last_visible_y)
17869 struct glyph_row *row;
17871 /* IT.vpos always starts from 0; it counts text lines. */
17872 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17874 /* Find PT if not already found in the lines displayed. */
17875 if (w->cursor.vpos < 0)
17877 int dy = it.current_y - start_row->y;
17879 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17880 row = row_containing_pos (w, PT, row, NULL, dy);
17881 if (row)
17882 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17883 dy, nrows_scrolled);
17884 else
17886 clear_glyph_matrix (w->desired_matrix);
17887 return false;
17891 /* Scroll the display. Do it before the current matrix is
17892 changed. The problem here is that update has not yet
17893 run, i.e. part of the current matrix is not up to date.
17894 scroll_run_hook will clear the cursor, and use the
17895 current matrix to get the height of the row the cursor is
17896 in. */
17897 run.current_y = start_row->y;
17898 run.desired_y = it.current_y;
17899 run.height = it.last_visible_y - it.current_y;
17901 if (run.height > 0 && run.current_y != run.desired_y)
17903 update_begin (f);
17904 FRAME_RIF (f)->update_window_begin_hook (w);
17905 FRAME_RIF (f)->clear_window_mouse_face (w);
17906 FRAME_RIF (f)->scroll_run_hook (w, &run);
17907 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17908 update_end (f);
17911 /* Shift current matrix down by nrows_scrolled lines. */
17912 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17913 rotate_matrix (w->current_matrix,
17914 start_vpos,
17915 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17916 nrows_scrolled);
17918 /* Disable lines that must be updated. */
17919 for (i = 0; i < nrows_scrolled; ++i)
17920 (start_row + i)->enabled_p = false;
17922 /* Re-compute Y positions. */
17923 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17924 max_y = it.last_visible_y;
17925 for (row = start_row + nrows_scrolled;
17926 row < bottom_row;
17927 ++row)
17929 row->y = it.current_y;
17930 row->visible_height = row->height;
17932 if (row->y < min_y)
17933 row->visible_height -= min_y - row->y;
17934 if (row->y + row->height > max_y)
17935 row->visible_height -= row->y + row->height - max_y;
17936 if (row->fringe_bitmap_periodic_p)
17937 row->redraw_fringe_bitmaps_p = true;
17939 it.current_y += row->height;
17941 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17942 last_reused_text_row = row;
17943 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17944 break;
17947 /* Disable lines in the current matrix which are now
17948 below the window. */
17949 for (++row; row < bottom_row; ++row)
17950 row->enabled_p = row->mode_line_p = false;
17953 /* Update window_end_pos etc.; last_reused_text_row is the last
17954 reused row from the current matrix containing text, if any.
17955 The value of last_text_row is the last displayed line
17956 containing text. */
17957 if (last_reused_text_row)
17958 adjust_window_ends (w, last_reused_text_row, true);
17959 else if (last_text_row)
17960 adjust_window_ends (w, last_text_row, false);
17961 else
17963 /* This window must be completely empty. */
17964 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17965 w->window_end_pos = Z - ZV;
17966 w->window_end_vpos = 0;
17968 w->window_end_valid = false;
17970 /* Update hint: don't try scrolling again in update_window. */
17971 w->desired_matrix->no_scrolling_p = true;
17973 #ifdef GLYPH_DEBUG
17974 debug_method_add (w, "try_window_reusing_current_matrix 1");
17975 #endif
17976 return true;
17978 else if (CHARPOS (new_start) > CHARPOS (start))
17980 struct glyph_row *pt_row, *row;
17981 struct glyph_row *first_reusable_row;
17982 struct glyph_row *first_row_to_display;
17983 int dy;
17984 int yb = window_text_bottom_y (w);
17986 /* Find the row starting at new_start, if there is one. Don't
17987 reuse a partially visible line at the end. */
17988 first_reusable_row = start_row;
17989 while (first_reusable_row->enabled_p
17990 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17991 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17992 < CHARPOS (new_start)))
17993 ++first_reusable_row;
17995 /* Give up if there is no row to reuse. */
17996 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17997 || !first_reusable_row->enabled_p
17998 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17999 != CHARPOS (new_start)))
18000 return false;
18002 /* We can reuse fully visible rows beginning with
18003 first_reusable_row to the end of the window. Set
18004 first_row_to_display to the first row that cannot be reused.
18005 Set pt_row to the row containing point, if there is any. */
18006 pt_row = NULL;
18007 for (first_row_to_display = first_reusable_row;
18008 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
18009 ++first_row_to_display)
18011 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
18012 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
18013 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
18014 && first_row_to_display->ends_at_zv_p
18015 && pt_row == NULL)))
18016 pt_row = first_row_to_display;
18019 /* Start displaying at the start of first_row_to_display. */
18020 eassert (first_row_to_display->y < yb);
18021 init_to_row_start (&it, w, first_row_to_display);
18023 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
18024 - start_vpos);
18025 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
18026 - nrows_scrolled);
18027 it.current_y = (first_row_to_display->y - first_reusable_row->y
18028 + WINDOW_HEADER_LINE_HEIGHT (w));
18030 /* Display lines beginning with first_row_to_display in the
18031 desired matrix. Set last_text_row to the last row displayed
18032 that displays text. */
18033 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
18034 if (pt_row == NULL)
18035 w->cursor.vpos = -1;
18036 last_text_row = NULL;
18037 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18038 if (display_line (&it, w->cursor.vpos))
18039 last_text_row = it.glyph_row - 1;
18041 /* If point is in a reused row, adjust y and vpos of the cursor
18042 position. */
18043 if (pt_row)
18045 w->cursor.vpos -= nrows_scrolled;
18046 w->cursor.y -= first_reusable_row->y - start_row->y;
18049 /* Give up if point isn't in a row displayed or reused. (This
18050 also handles the case where w->cursor.vpos < nrows_scrolled
18051 after the calls to display_line, which can happen with scroll
18052 margins. See bug#1295.) */
18053 if (w->cursor.vpos < 0)
18055 clear_glyph_matrix (w->desired_matrix);
18056 return false;
18059 /* Scroll the display. */
18060 run.current_y = first_reusable_row->y;
18061 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
18062 run.height = it.last_visible_y - run.current_y;
18063 dy = run.current_y - run.desired_y;
18065 if (run.height)
18067 update_begin (f);
18068 FRAME_RIF (f)->update_window_begin_hook (w);
18069 FRAME_RIF (f)->clear_window_mouse_face (w);
18070 FRAME_RIF (f)->scroll_run_hook (w, &run);
18071 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18072 update_end (f);
18075 /* Adjust Y positions of reused rows. */
18076 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18077 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18078 max_y = it.last_visible_y;
18079 for (row = first_reusable_row; row < first_row_to_display; ++row)
18081 row->y -= dy;
18082 row->visible_height = row->height;
18083 if (row->y < min_y)
18084 row->visible_height -= min_y - row->y;
18085 if (row->y + row->height > max_y)
18086 row->visible_height -= row->y + row->height - max_y;
18087 if (row->fringe_bitmap_periodic_p)
18088 row->redraw_fringe_bitmaps_p = true;
18091 /* Scroll the current matrix. */
18092 eassert (nrows_scrolled > 0);
18093 rotate_matrix (w->current_matrix,
18094 start_vpos,
18095 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18096 -nrows_scrolled);
18098 /* Disable rows not reused. */
18099 for (row -= nrows_scrolled; row < bottom_row; ++row)
18100 row->enabled_p = false;
18102 /* Point may have moved to a different line, so we cannot assume that
18103 the previous cursor position is valid; locate the correct row. */
18104 if (pt_row)
18106 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18107 row < bottom_row
18108 && PT >= MATRIX_ROW_END_CHARPOS (row)
18109 && !row->ends_at_zv_p;
18110 row++)
18112 w->cursor.vpos++;
18113 w->cursor.y = row->y;
18115 if (row < bottom_row)
18117 /* Can't simply scan the row for point with
18118 bidi-reordered glyph rows. Let set_cursor_from_row
18119 figure out where to put the cursor, and if it fails,
18120 give up. */
18121 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18123 if (!set_cursor_from_row (w, row, w->current_matrix,
18124 0, 0, 0, 0))
18126 clear_glyph_matrix (w->desired_matrix);
18127 return false;
18130 else
18132 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18133 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18135 for (; glyph < end
18136 && (!BUFFERP (glyph->object)
18137 || glyph->charpos < PT);
18138 glyph++)
18140 w->cursor.hpos++;
18141 w->cursor.x += glyph->pixel_width;
18147 /* Adjust window end. A null value of last_text_row means that
18148 the window end is in reused rows which in turn means that
18149 only its vpos can have changed. */
18150 if (last_text_row)
18151 adjust_window_ends (w, last_text_row, false);
18152 else
18153 w->window_end_vpos -= nrows_scrolled;
18155 w->window_end_valid = false;
18156 w->desired_matrix->no_scrolling_p = true;
18158 #ifdef GLYPH_DEBUG
18159 debug_method_add (w, "try_window_reusing_current_matrix 2");
18160 #endif
18161 return true;
18164 return false;
18169 /************************************************************************
18170 Window redisplay reusing current matrix when buffer has changed
18171 ************************************************************************/
18173 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18174 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18175 ptrdiff_t *, ptrdiff_t *);
18176 static struct glyph_row *
18177 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18178 struct glyph_row *);
18181 /* Return the last row in MATRIX displaying text. If row START is
18182 non-null, start searching with that row. IT gives the dimensions
18183 of the display. Value is null if matrix is empty; otherwise it is
18184 a pointer to the row found. */
18186 static struct glyph_row *
18187 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18188 struct glyph_row *start)
18190 struct glyph_row *row, *row_found;
18192 /* Set row_found to the last row in IT->w's current matrix
18193 displaying text. The loop looks funny but think of partially
18194 visible lines. */
18195 row_found = NULL;
18196 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18197 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18199 eassert (row->enabled_p);
18200 row_found = row;
18201 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18202 break;
18203 ++row;
18206 return row_found;
18210 /* Return the last row in the current matrix of W that is not affected
18211 by changes at the start of current_buffer that occurred since W's
18212 current matrix was built. Value is null if no such row exists.
18214 BEG_UNCHANGED us the number of characters unchanged at the start of
18215 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18216 first changed character in current_buffer. Characters at positions <
18217 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18218 when the current matrix was built. */
18220 static struct glyph_row *
18221 find_last_unchanged_at_beg_row (struct window *w)
18223 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18224 struct glyph_row *row;
18225 struct glyph_row *row_found = NULL;
18226 int yb = window_text_bottom_y (w);
18228 /* Find the last row displaying unchanged text. */
18229 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18230 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18231 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18232 ++row)
18234 if (/* If row ends before first_changed_pos, it is unchanged,
18235 except in some case. */
18236 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18237 /* When row ends in ZV and we write at ZV it is not
18238 unchanged. */
18239 && !row->ends_at_zv_p
18240 /* When first_changed_pos is the end of a continued line,
18241 row is not unchanged because it may be no longer
18242 continued. */
18243 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18244 && (row->continued_p
18245 || row->exact_window_width_line_p))
18246 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18247 needs to be recomputed, so don't consider this row as
18248 unchanged. This happens when the last line was
18249 bidi-reordered and was killed immediately before this
18250 redisplay cycle. In that case, ROW->end stores the
18251 buffer position of the first visual-order character of
18252 the killed text, which is now beyond ZV. */
18253 && CHARPOS (row->end.pos) <= ZV)
18254 row_found = row;
18256 /* Stop if last visible row. */
18257 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18258 break;
18261 return row_found;
18265 /* Find the first glyph row in the current matrix of W that is not
18266 affected by changes at the end of current_buffer since the
18267 time W's current matrix was built.
18269 Return in *DELTA the number of chars by which buffer positions in
18270 unchanged text at the end of current_buffer must be adjusted.
18272 Return in *DELTA_BYTES the corresponding number of bytes.
18274 Value is null if no such row exists, i.e. all rows are affected by
18275 changes. */
18277 static struct glyph_row *
18278 find_first_unchanged_at_end_row (struct window *w,
18279 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18281 struct glyph_row *row;
18282 struct glyph_row *row_found = NULL;
18284 *delta = *delta_bytes = 0;
18286 /* Display must not have been paused, otherwise the current matrix
18287 is not up to date. */
18288 eassert (w->window_end_valid);
18290 /* A value of window_end_pos >= END_UNCHANGED means that the window
18291 end is in the range of changed text. If so, there is no
18292 unchanged row at the end of W's current matrix. */
18293 if (w->window_end_pos >= END_UNCHANGED)
18294 return NULL;
18296 /* Set row to the last row in W's current matrix displaying text. */
18297 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18299 /* If matrix is entirely empty, no unchanged row exists. */
18300 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18302 /* The value of row is the last glyph row in the matrix having a
18303 meaningful buffer position in it. The end position of row
18304 corresponds to window_end_pos. This allows us to translate
18305 buffer positions in the current matrix to current buffer
18306 positions for characters not in changed text. */
18307 ptrdiff_t Z_old =
18308 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18309 ptrdiff_t Z_BYTE_old =
18310 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18311 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18312 struct glyph_row *first_text_row
18313 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18315 *delta = Z - Z_old;
18316 *delta_bytes = Z_BYTE - Z_BYTE_old;
18318 /* Set last_unchanged_pos to the buffer position of the last
18319 character in the buffer that has not been changed. Z is the
18320 index + 1 of the last character in current_buffer, i.e. by
18321 subtracting END_UNCHANGED we get the index of the last
18322 unchanged character, and we have to add BEG to get its buffer
18323 position. */
18324 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18325 last_unchanged_pos_old = last_unchanged_pos - *delta;
18327 /* Search backward from ROW for a row displaying a line that
18328 starts at a minimum position >= last_unchanged_pos_old. */
18329 for (; row > first_text_row; --row)
18331 /* This used to abort, but it can happen.
18332 It is ok to just stop the search instead here. KFS. */
18333 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18334 break;
18336 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18337 row_found = row;
18341 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18343 return row_found;
18347 /* Make sure that glyph rows in the current matrix of window W
18348 reference the same glyph memory as corresponding rows in the
18349 frame's frame matrix. This function is called after scrolling W's
18350 current matrix on a terminal frame in try_window_id and
18351 try_window_reusing_current_matrix. */
18353 static void
18354 sync_frame_with_window_matrix_rows (struct window *w)
18356 struct frame *f = XFRAME (w->frame);
18357 struct glyph_row *window_row, *window_row_end, *frame_row;
18359 /* Preconditions: W must be a leaf window and full-width. Its frame
18360 must have a frame matrix. */
18361 eassert (BUFFERP (w->contents));
18362 eassert (WINDOW_FULL_WIDTH_P (w));
18363 eassert (!FRAME_WINDOW_P (f));
18365 /* If W is a full-width window, glyph pointers in W's current matrix
18366 have, by definition, to be the same as glyph pointers in the
18367 corresponding frame matrix. Note that frame matrices have no
18368 marginal areas (see build_frame_matrix). */
18369 window_row = w->current_matrix->rows;
18370 window_row_end = window_row + w->current_matrix->nrows;
18371 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18372 while (window_row < window_row_end)
18374 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18375 struct glyph *end = window_row->glyphs[LAST_AREA];
18377 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18378 frame_row->glyphs[TEXT_AREA] = start;
18379 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18380 frame_row->glyphs[LAST_AREA] = end;
18382 /* Disable frame rows whose corresponding window rows have
18383 been disabled in try_window_id. */
18384 if (!window_row->enabled_p)
18385 frame_row->enabled_p = false;
18387 ++window_row, ++frame_row;
18392 /* Find the glyph row in window W containing CHARPOS. Consider all
18393 rows between START and END (not inclusive). END null means search
18394 all rows to the end of the display area of W. Value is the row
18395 containing CHARPOS or null. */
18397 struct glyph_row *
18398 row_containing_pos (struct window *w, ptrdiff_t charpos,
18399 struct glyph_row *start, struct glyph_row *end, int dy)
18401 struct glyph_row *row = start;
18402 struct glyph_row *best_row = NULL;
18403 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18404 int last_y;
18406 /* If we happen to start on a header-line, skip that. */
18407 if (row->mode_line_p)
18408 ++row;
18410 if ((end && row >= end) || !row->enabled_p)
18411 return NULL;
18413 last_y = window_text_bottom_y (w) - dy;
18415 while (true)
18417 /* Give up if we have gone too far. */
18418 if ((end && row >= end) || !row->enabled_p)
18419 return NULL;
18420 /* This formerly returned if they were equal.
18421 I think that both quantities are of a "last plus one" type;
18422 if so, when they are equal, the row is within the screen. -- rms. */
18423 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18424 return NULL;
18426 /* If it is in this row, return this row. */
18427 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18428 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18429 /* The end position of a row equals the start
18430 position of the next row. If CHARPOS is there, we
18431 would rather consider it displayed in the next
18432 line, except when this line ends in ZV. */
18433 && !row_for_charpos_p (row, charpos)))
18434 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18436 struct glyph *g;
18438 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18439 || (!best_row && !row->continued_p))
18440 return row;
18441 /* In bidi-reordered rows, there could be several rows whose
18442 edges surround CHARPOS, all of these rows belonging to
18443 the same continued line. We need to find the row which
18444 fits CHARPOS the best. */
18445 for (g = row->glyphs[TEXT_AREA];
18446 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18447 g++)
18449 if (!STRINGP (g->object))
18451 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18453 mindif = eabs (g->charpos - charpos);
18454 best_row = row;
18455 /* Exact match always wins. */
18456 if (mindif == 0)
18457 return best_row;
18462 else if (best_row && !row->continued_p)
18463 return best_row;
18464 ++row;
18469 /* Try to redisplay window W by reusing its existing display. W's
18470 current matrix must be up to date when this function is called,
18471 i.e., window_end_valid must be true.
18473 Value is
18475 >= 1 if successful, i.e. display has been updated
18476 specifically:
18477 1 means the changes were in front of a newline that precedes
18478 the window start, and the whole current matrix was reused
18479 2 means the changes were after the last position displayed
18480 in the window, and the whole current matrix was reused
18481 3 means portions of the current matrix were reused, while
18482 some of the screen lines were redrawn
18483 -1 if redisplay with same window start is known not to succeed
18484 0 if otherwise unsuccessful
18486 The following steps are performed:
18488 1. Find the last row in the current matrix of W that is not
18489 affected by changes at the start of current_buffer. If no such row
18490 is found, give up.
18492 2. Find the first row in W's current matrix that is not affected by
18493 changes at the end of current_buffer. Maybe there is no such row.
18495 3. Display lines beginning with the row + 1 found in step 1 to the
18496 row found in step 2 or, if step 2 didn't find a row, to the end of
18497 the window.
18499 4. If cursor is not known to appear on the window, give up.
18501 5. If display stopped at the row found in step 2, scroll the
18502 display and current matrix as needed.
18504 6. Maybe display some lines at the end of W, if we must. This can
18505 happen under various circumstances, like a partially visible line
18506 becoming fully visible, or because newly displayed lines are displayed
18507 in smaller font sizes.
18509 7. Update W's window end information. */
18511 static int
18512 try_window_id (struct window *w)
18514 struct frame *f = XFRAME (w->frame);
18515 struct glyph_matrix *current_matrix = w->current_matrix;
18516 struct glyph_matrix *desired_matrix = w->desired_matrix;
18517 struct glyph_row *last_unchanged_at_beg_row;
18518 struct glyph_row *first_unchanged_at_end_row;
18519 struct glyph_row *row;
18520 struct glyph_row *bottom_row;
18521 int bottom_vpos;
18522 struct it it;
18523 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18524 int dvpos, dy;
18525 struct text_pos start_pos;
18526 struct run run;
18527 int first_unchanged_at_end_vpos = 0;
18528 struct glyph_row *last_text_row, *last_text_row_at_end;
18529 struct text_pos start;
18530 ptrdiff_t first_changed_charpos, last_changed_charpos;
18532 #ifdef GLYPH_DEBUG
18533 if (inhibit_try_window_id)
18534 return 0;
18535 #endif
18537 /* This is handy for debugging. */
18538 #if false
18539 #define GIVE_UP(X) \
18540 do { \
18541 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18542 return 0; \
18543 } while (false)
18544 #else
18545 #define GIVE_UP(X) return 0
18546 #endif
18548 SET_TEXT_POS_FROM_MARKER (start, w->start);
18550 /* Don't use this for mini-windows because these can show
18551 messages and mini-buffers, and we don't handle that here. */
18552 if (MINI_WINDOW_P (w))
18553 GIVE_UP (1);
18555 /* This flag is used to prevent redisplay optimizations. */
18556 if (windows_or_buffers_changed || f->cursor_type_changed)
18557 GIVE_UP (2);
18559 /* This function's optimizations cannot be used if overlays have
18560 changed in the buffer displayed by the window, so give up if they
18561 have. */
18562 if (w->last_overlay_modified != OVERLAY_MODIFF)
18563 GIVE_UP (200);
18565 /* Verify that narrowing has not changed.
18566 Also verify that we were not told to prevent redisplay optimizations.
18567 It would be nice to further
18568 reduce the number of cases where this prevents try_window_id. */
18569 if (current_buffer->clip_changed
18570 || current_buffer->prevent_redisplay_optimizations_p)
18571 GIVE_UP (3);
18573 /* Window must either use window-based redisplay or be full width. */
18574 if (!FRAME_WINDOW_P (f)
18575 && (!FRAME_LINE_INS_DEL_OK (f)
18576 || !WINDOW_FULL_WIDTH_P (w)))
18577 GIVE_UP (4);
18579 /* Give up if point is known NOT to appear in W. */
18580 if (PT < CHARPOS (start))
18581 GIVE_UP (5);
18583 /* Another way to prevent redisplay optimizations. */
18584 if (w->last_modified == 0)
18585 GIVE_UP (6);
18587 /* Verify that window is not hscrolled. */
18588 if (w->hscroll != 0)
18589 GIVE_UP (7);
18591 /* Verify that display wasn't paused. */
18592 if (!w->window_end_valid)
18593 GIVE_UP (8);
18595 /* Likewise if highlighting trailing whitespace. */
18596 if (!NILP (Vshow_trailing_whitespace))
18597 GIVE_UP (11);
18599 /* Can't use this if overlay arrow position and/or string have
18600 changed. */
18601 if (overlay_arrows_changed_p (false))
18602 GIVE_UP (12);
18604 /* When word-wrap is on, adding a space to the first word of a
18605 wrapped line can change the wrap position, altering the line
18606 above it. It might be worthwhile to handle this more
18607 intelligently, but for now just redisplay from scratch. */
18608 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18609 GIVE_UP (21);
18611 /* Under bidi reordering, adding or deleting a character in the
18612 beginning of a paragraph, before the first strong directional
18613 character, can change the base direction of the paragraph (unless
18614 the buffer specifies a fixed paragraph direction), which will
18615 require redisplaying the whole paragraph. It might be worthwhile
18616 to find the paragraph limits and widen the range of redisplayed
18617 lines to that, but for now just give up this optimization and
18618 redisplay from scratch. */
18619 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18620 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18621 GIVE_UP (22);
18623 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18624 to that variable require thorough redisplay. */
18625 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18626 GIVE_UP (23);
18628 /* Give up if display-line-numbers is in relative mode, or when the
18629 current line's number needs to be displayed in a distinct face. */
18630 if (EQ (Vdisplay_line_numbers, Qrelative)
18631 || EQ (Vdisplay_line_numbers, Qvisual)
18632 || (!NILP (Vdisplay_line_numbers)
18633 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18634 Qline_number_current_line,
18635 w->frame))))
18636 GIVE_UP (24);
18638 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18639 only if buffer has really changed. The reason is that the gap is
18640 initially at Z for freshly visited files. The code below would
18641 set end_unchanged to 0 in that case. */
18642 if (MODIFF > SAVE_MODIFF
18643 /* This seems to happen sometimes after saving a buffer. */
18644 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18646 if (GPT - BEG < BEG_UNCHANGED)
18647 BEG_UNCHANGED = GPT - BEG;
18648 if (Z - GPT < END_UNCHANGED)
18649 END_UNCHANGED = Z - GPT;
18652 /* The position of the first and last character that has been changed. */
18653 first_changed_charpos = BEG + BEG_UNCHANGED;
18654 last_changed_charpos = Z - END_UNCHANGED;
18656 /* If window starts after a line end, and the last change is in
18657 front of that newline, then changes don't affect the display.
18658 This case happens with stealth-fontification. Note that although
18659 the display is unchanged, glyph positions in the matrix have to
18660 be adjusted, of course. */
18661 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18662 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18663 && ((last_changed_charpos < CHARPOS (start)
18664 && CHARPOS (start) == BEGV)
18665 || (last_changed_charpos < CHARPOS (start) - 1
18666 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18668 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18669 struct glyph_row *r0;
18671 /* Compute how many chars/bytes have been added to or removed
18672 from the buffer. */
18673 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18674 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18675 Z_delta = Z - Z_old;
18676 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18678 /* Give up if PT is not in the window. Note that it already has
18679 been checked at the start of try_window_id that PT is not in
18680 front of the window start. */
18681 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18682 GIVE_UP (13);
18684 /* If window start is unchanged, we can reuse the whole matrix
18685 as is, after adjusting glyph positions. No need to compute
18686 the window end again, since its offset from Z hasn't changed. */
18687 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18688 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18689 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18690 /* PT must not be in a partially visible line. */
18691 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18692 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18694 /* Adjust positions in the glyph matrix. */
18695 if (Z_delta || Z_delta_bytes)
18697 struct glyph_row *r1
18698 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18699 increment_matrix_positions (w->current_matrix,
18700 MATRIX_ROW_VPOS (r0, current_matrix),
18701 MATRIX_ROW_VPOS (r1, current_matrix),
18702 Z_delta, Z_delta_bytes);
18705 /* Set the cursor. */
18706 row = row_containing_pos (w, PT, r0, NULL, 0);
18707 if (row)
18708 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18709 return 1;
18713 /* Handle the case that changes are all below what is displayed in
18714 the window, and that PT is in the window. This shortcut cannot
18715 be taken if ZV is visible in the window, and text has been added
18716 there that is visible in the window. */
18717 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18718 /* ZV is not visible in the window, or there are no
18719 changes at ZV, actually. */
18720 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18721 || first_changed_charpos == last_changed_charpos))
18723 struct glyph_row *r0;
18725 /* Give up if PT is not in the window. Note that it already has
18726 been checked at the start of try_window_id that PT is not in
18727 front of the window start. */
18728 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18729 GIVE_UP (14);
18731 /* If window start is unchanged, we can reuse the whole matrix
18732 as is, without changing glyph positions since no text has
18733 been added/removed in front of the window end. */
18734 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18735 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18736 /* PT must not be in a partially visible line. */
18737 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18738 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18740 /* We have to compute the window end anew since text
18741 could have been added/removed after it. */
18742 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18743 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18745 /* Set the cursor. */
18746 row = row_containing_pos (w, PT, r0, NULL, 0);
18747 if (row)
18748 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18749 return 2;
18753 /* Give up if window start is in the changed area.
18755 The condition used to read
18757 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18759 but why that was tested escapes me at the moment. */
18760 if (CHARPOS (start) >= first_changed_charpos
18761 && CHARPOS (start) <= last_changed_charpos)
18762 GIVE_UP (15);
18764 /* Check that window start agrees with the start of the first glyph
18765 row in its current matrix. Check this after we know the window
18766 start is not in changed text, otherwise positions would not be
18767 comparable. */
18768 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18769 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18770 GIVE_UP (16);
18772 /* Give up if the window ends in strings. Overlay strings
18773 at the end are difficult to handle, so don't try. */
18774 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18775 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18776 GIVE_UP (20);
18778 /* Compute the position at which we have to start displaying new
18779 lines. Some of the lines at the top of the window might be
18780 reusable because they are not displaying changed text. Find the
18781 last row in W's current matrix not affected by changes at the
18782 start of current_buffer. Value is null if changes start in the
18783 first line of window. */
18784 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18785 if (last_unchanged_at_beg_row)
18787 /* Avoid starting to display in the middle of a character, a TAB
18788 for instance. This is easier than to set up the iterator
18789 exactly, and it's not a frequent case, so the additional
18790 effort wouldn't really pay off. */
18791 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18792 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18793 && last_unchanged_at_beg_row > w->current_matrix->rows)
18794 --last_unchanged_at_beg_row;
18796 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18797 GIVE_UP (17);
18799 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18800 GIVE_UP (18);
18801 start_pos = it.current.pos;
18803 /* Start displaying new lines in the desired matrix at the same
18804 vpos we would use in the current matrix, i.e. below
18805 last_unchanged_at_beg_row. */
18806 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18807 current_matrix);
18808 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18809 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18811 eassert (it.hpos == 0 && it.current_x == 0);
18813 else
18815 /* There are no reusable lines at the start of the window.
18816 Start displaying in the first text line. */
18817 start_display (&it, w, start);
18818 it.vpos = it.first_vpos;
18819 start_pos = it.current.pos;
18822 /* Find the first row that is not affected by changes at the end of
18823 the buffer. Value will be null if there is no unchanged row, in
18824 which case we must redisplay to the end of the window. delta
18825 will be set to the value by which buffer positions beginning with
18826 first_unchanged_at_end_row have to be adjusted due to text
18827 changes. */
18828 first_unchanged_at_end_row
18829 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18830 IF_DEBUG (debug_delta = delta);
18831 IF_DEBUG (debug_delta_bytes = delta_bytes);
18833 /* Set stop_pos to the buffer position up to which we will have to
18834 display new lines. If first_unchanged_at_end_row != NULL, this
18835 is the buffer position of the start of the line displayed in that
18836 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18837 that we don't stop at a buffer position. */
18838 stop_pos = 0;
18839 if (first_unchanged_at_end_row)
18841 eassert (last_unchanged_at_beg_row == NULL
18842 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18844 /* If this is a continuation line, move forward to the next one
18845 that isn't. Changes in lines above affect this line.
18846 Caution: this may move first_unchanged_at_end_row to a row
18847 not displaying text. */
18848 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18849 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18850 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18851 < it.last_visible_y))
18852 ++first_unchanged_at_end_row;
18854 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18855 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18856 >= it.last_visible_y))
18857 first_unchanged_at_end_row = NULL;
18858 else
18860 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18861 + delta);
18862 first_unchanged_at_end_vpos
18863 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18864 eassert (stop_pos >= Z - END_UNCHANGED);
18867 else if (last_unchanged_at_beg_row == NULL)
18868 GIVE_UP (19);
18871 #ifdef GLYPH_DEBUG
18873 /* Either there is no unchanged row at the end, or the one we have
18874 now displays text. This is a necessary condition for the window
18875 end pos calculation at the end of this function. */
18876 eassert (first_unchanged_at_end_row == NULL
18877 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18879 debug_last_unchanged_at_beg_vpos
18880 = (last_unchanged_at_beg_row
18881 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18882 : -1);
18883 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18885 #endif /* GLYPH_DEBUG */
18888 /* Display new lines. Set last_text_row to the last new line
18889 displayed which has text on it, i.e. might end up as being the
18890 line where the window_end_vpos is. */
18891 w->cursor.vpos = -1;
18892 last_text_row = NULL;
18893 overlay_arrow_seen = false;
18894 if (it.current_y < it.last_visible_y
18895 && !f->fonts_changed
18896 && (first_unchanged_at_end_row == NULL
18897 || IT_CHARPOS (it) < stop_pos))
18898 it.glyph_row->reversed_p = false;
18899 while (it.current_y < it.last_visible_y
18900 && !f->fonts_changed
18901 && (first_unchanged_at_end_row == NULL
18902 || IT_CHARPOS (it) < stop_pos))
18904 if (display_line (&it, -1))
18905 last_text_row = it.glyph_row - 1;
18908 if (f->fonts_changed)
18909 return -1;
18911 /* The redisplay iterations in display_line above could have
18912 triggered font-lock, which could have done something that
18913 invalidates IT->w window's end-point information, on which we
18914 rely below. E.g., one package, which will remain unnamed, used
18915 to install a font-lock-fontify-region-function that called
18916 bury-buffer, whose side effect is to switch the buffer displayed
18917 by IT->w, and that predictably resets IT->w's window_end_valid
18918 flag, which we already tested at the entry to this function.
18919 Amply punish such packages/modes by giving up on this
18920 optimization in those cases. */
18921 if (!w->window_end_valid)
18923 clear_glyph_matrix (w->desired_matrix);
18924 return -1;
18927 /* Compute differences in buffer positions, y-positions etc. for
18928 lines reused at the bottom of the window. Compute what we can
18929 scroll. */
18930 if (first_unchanged_at_end_row
18931 /* No lines reused because we displayed everything up to the
18932 bottom of the window. */
18933 && it.current_y < it.last_visible_y)
18935 dvpos = (it.vpos
18936 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18937 current_matrix));
18938 dy = it.current_y - first_unchanged_at_end_row->y;
18939 run.current_y = first_unchanged_at_end_row->y;
18940 run.desired_y = run.current_y + dy;
18941 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18943 else
18945 delta = delta_bytes = dvpos = dy
18946 = run.current_y = run.desired_y = run.height = 0;
18947 first_unchanged_at_end_row = NULL;
18949 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18952 /* Find the cursor if not already found. We have to decide whether
18953 PT will appear on this window (it sometimes doesn't, but this is
18954 not a very frequent case.) This decision has to be made before
18955 the current matrix is altered. A value of cursor.vpos < 0 means
18956 that PT is either in one of the lines beginning at
18957 first_unchanged_at_end_row or below the window. Don't care for
18958 lines that might be displayed later at the window end; as
18959 mentioned, this is not a frequent case. */
18960 if (w->cursor.vpos < 0)
18962 /* Cursor in unchanged rows at the top? */
18963 if (PT < CHARPOS (start_pos)
18964 && last_unchanged_at_beg_row)
18966 row = row_containing_pos (w, PT,
18967 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18968 last_unchanged_at_beg_row + 1, 0);
18969 if (row)
18970 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18973 /* Start from first_unchanged_at_end_row looking for PT. */
18974 else if (first_unchanged_at_end_row)
18976 row = row_containing_pos (w, PT - delta,
18977 first_unchanged_at_end_row, NULL, 0);
18978 if (row)
18979 set_cursor_from_row (w, row, w->current_matrix, delta,
18980 delta_bytes, dy, dvpos);
18983 /* Give up if cursor was not found. */
18984 if (w->cursor.vpos < 0)
18986 clear_glyph_matrix (w->desired_matrix);
18987 return -1;
18991 /* Don't let the cursor end in the scroll margins. */
18993 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18994 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18996 if ((w->cursor.y < this_scroll_margin
18997 && CHARPOS (start) > BEGV)
18998 /* Old redisplay didn't take scroll margin into account at the bottom,
18999 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
19000 || (w->cursor.y + (make_cursor_line_fully_visible_p
19001 ? cursor_height + this_scroll_margin
19002 : 1)) > it.last_visible_y)
19004 w->cursor.vpos = -1;
19005 clear_glyph_matrix (w->desired_matrix);
19006 return -1;
19010 /* Scroll the display. Do it before changing the current matrix so
19011 that xterm.c doesn't get confused about where the cursor glyph is
19012 found. */
19013 if (dy && run.height)
19015 update_begin (f);
19017 if (FRAME_WINDOW_P (f))
19019 FRAME_RIF (f)->update_window_begin_hook (w);
19020 FRAME_RIF (f)->clear_window_mouse_face (w);
19021 FRAME_RIF (f)->scroll_run_hook (w, &run);
19022 FRAME_RIF (f)->update_window_end_hook (w, false, false);
19024 else
19026 /* Terminal frame. In this case, dvpos gives the number of
19027 lines to scroll by; dvpos < 0 means scroll up. */
19028 int from_vpos
19029 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
19030 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
19031 int end = (WINDOW_TOP_EDGE_LINE (w)
19032 + window_wants_header_line (w)
19033 + window_internal_height (w));
19035 #if defined (HAVE_GPM) || defined (MSDOS)
19036 x_clear_window_mouse_face (w);
19037 #endif
19038 /* Perform the operation on the screen. */
19039 if (dvpos > 0)
19041 /* Scroll last_unchanged_at_beg_row to the end of the
19042 window down dvpos lines. */
19043 set_terminal_window (f, end);
19045 /* On dumb terminals delete dvpos lines at the end
19046 before inserting dvpos empty lines. */
19047 if (!FRAME_SCROLL_REGION_OK (f))
19048 ins_del_lines (f, end - dvpos, -dvpos);
19050 /* Insert dvpos empty lines in front of
19051 last_unchanged_at_beg_row. */
19052 ins_del_lines (f, from, dvpos);
19054 else if (dvpos < 0)
19056 /* Scroll up last_unchanged_at_beg_vpos to the end of
19057 the window to last_unchanged_at_beg_vpos - |dvpos|. */
19058 set_terminal_window (f, end);
19060 /* Delete dvpos lines in front of
19061 last_unchanged_at_beg_vpos. ins_del_lines will set
19062 the cursor to the given vpos and emit |dvpos| delete
19063 line sequences. */
19064 ins_del_lines (f, from + dvpos, dvpos);
19066 /* On a dumb terminal insert dvpos empty lines at the
19067 end. */
19068 if (!FRAME_SCROLL_REGION_OK (f))
19069 ins_del_lines (f, end + dvpos, -dvpos);
19072 set_terminal_window (f, 0);
19075 update_end (f);
19078 /* Shift reused rows of the current matrix to the right position.
19079 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19080 text. */
19081 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19082 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19083 if (dvpos < 0)
19085 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19086 bottom_vpos, dvpos);
19087 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19088 bottom_vpos);
19090 else if (dvpos > 0)
19092 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19093 bottom_vpos, dvpos);
19094 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19095 first_unchanged_at_end_vpos + dvpos);
19098 /* For frame-based redisplay, make sure that current frame and window
19099 matrix are in sync with respect to glyph memory. */
19100 if (!FRAME_WINDOW_P (f))
19101 sync_frame_with_window_matrix_rows (w);
19103 /* Adjust buffer positions in reused rows. */
19104 if (delta || delta_bytes)
19105 increment_matrix_positions (current_matrix,
19106 first_unchanged_at_end_vpos + dvpos,
19107 bottom_vpos, delta, delta_bytes);
19109 /* Adjust Y positions. */
19110 if (dy)
19111 shift_glyph_matrix (w, current_matrix,
19112 first_unchanged_at_end_vpos + dvpos,
19113 bottom_vpos, dy);
19115 if (first_unchanged_at_end_row)
19117 first_unchanged_at_end_row += dvpos;
19118 if (first_unchanged_at_end_row->y >= it.last_visible_y
19119 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19120 first_unchanged_at_end_row = NULL;
19123 /* If scrolling up, there may be some lines to display at the end of
19124 the window. */
19125 last_text_row_at_end = NULL;
19126 if (dy < 0)
19128 /* Scrolling up can leave for example a partially visible line
19129 at the end of the window to be redisplayed. */
19130 /* Set last_row to the glyph row in the current matrix where the
19131 window end line is found. It has been moved up or down in
19132 the matrix by dvpos. */
19133 int last_vpos = w->window_end_vpos + dvpos;
19134 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19136 /* If last_row is the window end line, it should display text. */
19137 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19139 /* If window end line was partially visible before, begin
19140 displaying at that line. Otherwise begin displaying with the
19141 line following it. */
19142 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19144 init_to_row_start (&it, w, last_row);
19145 it.vpos = last_vpos;
19146 it.current_y = last_row->y;
19148 else
19150 init_to_row_end (&it, w, last_row);
19151 it.vpos = 1 + last_vpos;
19152 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19153 ++last_row;
19156 /* We may start in a continuation line. If so, we have to
19157 get the right continuation_lines_width and current_x. */
19158 it.continuation_lines_width = last_row->continuation_lines_width;
19159 it.hpos = it.current_x = 0;
19161 /* Display the rest of the lines at the window end. */
19162 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19163 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19165 /* Is it always sure that the display agrees with lines in
19166 the current matrix? I don't think so, so we mark rows
19167 displayed invalid in the current matrix by setting their
19168 enabled_p flag to false. */
19169 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19170 if (display_line (&it, w->cursor.vpos))
19171 last_text_row_at_end = it.glyph_row - 1;
19175 /* Update window_end_pos and window_end_vpos. */
19176 if (first_unchanged_at_end_row && !last_text_row_at_end)
19178 /* Window end line if one of the preserved rows from the current
19179 matrix. Set row to the last row displaying text in current
19180 matrix starting at first_unchanged_at_end_row, after
19181 scrolling. */
19182 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19183 row = find_last_row_displaying_text (w->current_matrix, &it,
19184 first_unchanged_at_end_row);
19185 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19186 adjust_window_ends (w, row, true);
19187 eassert (w->window_end_bytepos >= 0);
19188 IF_DEBUG (debug_method_add (w, "A"));
19190 else if (last_text_row_at_end)
19192 adjust_window_ends (w, last_text_row_at_end, false);
19193 eassert (w->window_end_bytepos >= 0);
19194 IF_DEBUG (debug_method_add (w, "B"));
19196 else if (last_text_row)
19198 /* We have displayed either to the end of the window or at the
19199 end of the window, i.e. the last row with text is to be found
19200 in the desired matrix. */
19201 adjust_window_ends (w, last_text_row, false);
19202 eassert (w->window_end_bytepos >= 0);
19204 else if (first_unchanged_at_end_row == NULL
19205 && last_text_row == NULL
19206 && last_text_row_at_end == NULL)
19208 /* Displayed to end of window, but no line containing text was
19209 displayed. Lines were deleted at the end of the window. */
19210 bool first_vpos = window_wants_header_line (w);
19211 int vpos = w->window_end_vpos;
19212 struct glyph_row *current_row = current_matrix->rows + vpos;
19213 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19215 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19217 eassert (first_vpos <= vpos);
19218 if (desired_row->enabled_p)
19220 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19221 row = desired_row;
19223 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19224 row = current_row;
19227 w->window_end_vpos = vpos + 1;
19228 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19229 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19230 eassert (w->window_end_bytepos >= 0);
19231 IF_DEBUG (debug_method_add (w, "C"));
19233 else
19234 emacs_abort ();
19236 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19237 debug_end_vpos = w->window_end_vpos));
19239 /* Record that display has not been completed. */
19240 w->window_end_valid = false;
19241 w->desired_matrix->no_scrolling_p = true;
19242 return 3;
19244 #undef GIVE_UP
19249 /***********************************************************************
19250 More debugging support
19251 ***********************************************************************/
19253 #ifdef GLYPH_DEBUG
19255 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19256 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19257 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19260 /* Dump the contents of glyph matrix MATRIX on stderr.
19262 GLYPHS 0 means don't show glyph contents.
19263 GLYPHS 1 means show glyphs in short form
19264 GLYPHS > 1 means show glyphs in long form. */
19266 void
19267 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19269 int i;
19270 for (i = 0; i < matrix->nrows; ++i)
19271 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19275 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19276 the glyph row and area where the glyph comes from. */
19278 void
19279 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19281 if (glyph->type == CHAR_GLYPH
19282 || glyph->type == GLYPHLESS_GLYPH)
19284 fprintf (stderr,
19285 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19286 glyph - row->glyphs[TEXT_AREA],
19287 (glyph->type == CHAR_GLYPH
19288 ? 'C'
19289 : 'G'),
19290 glyph->charpos,
19291 (BUFFERP (glyph->object)
19292 ? 'B'
19293 : (STRINGP (glyph->object)
19294 ? 'S'
19295 : (NILP (glyph->object)
19296 ? '0'
19297 : '-'))),
19298 glyph->pixel_width,
19299 glyph->u.ch,
19300 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19301 ? (int) glyph->u.ch
19302 : '.'),
19303 glyph->face_id,
19304 glyph->left_box_line_p,
19305 glyph->right_box_line_p);
19307 else if (glyph->type == STRETCH_GLYPH)
19309 fprintf (stderr,
19310 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19311 glyph - row->glyphs[TEXT_AREA],
19312 'S',
19313 glyph->charpos,
19314 (BUFFERP (glyph->object)
19315 ? 'B'
19316 : (STRINGP (glyph->object)
19317 ? 'S'
19318 : (NILP (glyph->object)
19319 ? '0'
19320 : '-'))),
19321 glyph->pixel_width,
19323 ' ',
19324 glyph->face_id,
19325 glyph->left_box_line_p,
19326 glyph->right_box_line_p);
19328 else if (glyph->type == IMAGE_GLYPH)
19330 fprintf (stderr,
19331 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19332 glyph - row->glyphs[TEXT_AREA],
19333 'I',
19334 glyph->charpos,
19335 (BUFFERP (glyph->object)
19336 ? 'B'
19337 : (STRINGP (glyph->object)
19338 ? 'S'
19339 : (NILP (glyph->object)
19340 ? '0'
19341 : '-'))),
19342 glyph->pixel_width,
19343 (unsigned int) glyph->u.img_id,
19344 '.',
19345 glyph->face_id,
19346 glyph->left_box_line_p,
19347 glyph->right_box_line_p);
19349 else if (glyph->type == COMPOSITE_GLYPH)
19351 fprintf (stderr,
19352 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19353 glyph - row->glyphs[TEXT_AREA],
19354 '+',
19355 glyph->charpos,
19356 (BUFFERP (glyph->object)
19357 ? 'B'
19358 : (STRINGP (glyph->object)
19359 ? 'S'
19360 : (NILP (glyph->object)
19361 ? '0'
19362 : '-'))),
19363 glyph->pixel_width,
19364 (unsigned int) glyph->u.cmp.id);
19365 if (glyph->u.cmp.automatic)
19366 fprintf (stderr,
19367 "[%d-%d]",
19368 glyph->slice.cmp.from, glyph->slice.cmp.to);
19369 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19370 glyph->face_id,
19371 glyph->left_box_line_p,
19372 glyph->right_box_line_p);
19374 else if (glyph->type == XWIDGET_GLYPH)
19376 #ifndef HAVE_XWIDGETS
19377 eassume (false);
19378 #else
19379 fprintf (stderr,
19380 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19381 glyph - row->glyphs[TEXT_AREA],
19382 'X',
19383 glyph->charpos,
19384 (BUFFERP (glyph->object)
19385 ? 'B'
19386 : (STRINGP (glyph->object)
19387 ? 'S'
19388 : '-')),
19389 glyph->pixel_width,
19390 glyph->u.xwidget,
19391 '.',
19392 glyph->face_id,
19393 glyph->left_box_line_p,
19394 glyph->right_box_line_p);
19395 #endif
19400 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19401 GLYPHS 0 means don't show glyph contents.
19402 GLYPHS 1 means show glyphs in short form
19403 GLYPHS > 1 means show glyphs in long form. */
19405 void
19406 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19408 if (glyphs != 1)
19410 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19411 fprintf (stderr, "==============================================================================\n");
19413 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19414 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19415 vpos,
19416 MATRIX_ROW_START_CHARPOS (row),
19417 MATRIX_ROW_END_CHARPOS (row),
19418 row->used[TEXT_AREA],
19419 row->contains_overlapping_glyphs_p,
19420 row->enabled_p,
19421 row->truncated_on_left_p,
19422 row->truncated_on_right_p,
19423 row->continued_p,
19424 MATRIX_ROW_CONTINUATION_LINE_P (row),
19425 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19426 row->ends_at_zv_p,
19427 row->fill_line_p,
19428 row->ends_in_middle_of_char_p,
19429 row->starts_in_middle_of_char_p,
19430 row->mouse_face_p,
19431 row->x,
19432 row->y,
19433 row->pixel_width,
19434 row->height,
19435 row->visible_height,
19436 row->ascent,
19437 row->phys_ascent);
19438 /* The next 3 lines should align to "Start" in the header. */
19439 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19440 row->end.overlay_string_index,
19441 row->continuation_lines_width);
19442 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19443 CHARPOS (row->start.string_pos),
19444 CHARPOS (row->end.string_pos));
19445 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19446 row->end.dpvec_index);
19449 if (glyphs > 1)
19451 int area;
19453 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19455 struct glyph *glyph = row->glyphs[area];
19456 struct glyph *glyph_end = glyph + row->used[area];
19458 /* Glyph for a line end in text. */
19459 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19460 ++glyph_end;
19462 if (glyph < glyph_end)
19463 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19465 for (; glyph < glyph_end; ++glyph)
19466 dump_glyph (row, glyph, area);
19469 else if (glyphs == 1)
19471 int area;
19472 char s[SHRT_MAX + 4];
19474 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19476 int i;
19478 for (i = 0; i < row->used[area]; ++i)
19480 struct glyph *glyph = row->glyphs[area] + i;
19481 if (i == row->used[area] - 1
19482 && area == TEXT_AREA
19483 && NILP (glyph->object)
19484 && glyph->type == CHAR_GLYPH
19485 && glyph->u.ch == ' ')
19487 strcpy (&s[i], "[\\n]");
19488 i += 4;
19490 else if (glyph->type == CHAR_GLYPH
19491 && glyph->u.ch < 0x80
19492 && glyph->u.ch >= ' ')
19493 s[i] = glyph->u.ch;
19494 else
19495 s[i] = '.';
19498 s[i] = '\0';
19499 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19505 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19506 Sdump_glyph_matrix, 0, 1, "p",
19507 doc: /* Dump the current matrix of the selected window to stderr.
19508 Shows contents of glyph row structures. With non-nil
19509 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19510 glyphs in short form, otherwise show glyphs in long form.
19512 Interactively, no argument means show glyphs in short form;
19513 with numeric argument, its value is passed as the GLYPHS flag. */)
19514 (Lisp_Object glyphs)
19516 struct window *w = XWINDOW (selected_window);
19517 struct buffer *buffer = XBUFFER (w->contents);
19519 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19520 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19521 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19522 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19523 fprintf (stderr, "=============================================\n");
19524 dump_glyph_matrix (w->current_matrix,
19525 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19526 return Qnil;
19530 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19531 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19532 Only text-mode frames have frame glyph matrices. */)
19533 (void)
19535 struct frame *f = XFRAME (selected_frame);
19537 if (f->current_matrix)
19538 dump_glyph_matrix (f->current_matrix, 1);
19539 else
19540 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19541 return Qnil;
19545 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19546 doc: /* Dump glyph row ROW to stderr.
19547 Interactively, ROW is the prefix numeric argument and defaults to
19548 the row which displays point.
19549 Optional argument GLYPHS 0 means don't dump glyphs.
19550 GLYPHS 1 means dump glyphs in short form.
19551 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19552 (Lisp_Object row, Lisp_Object glyphs)
19554 struct glyph_matrix *matrix;
19555 EMACS_INT vpos;
19557 if (NILP (row))
19559 int d1, d2, d3, d4, d5, ypos;
19560 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19561 &d1, &d2, &d3, &d4, &d5, &ypos);
19562 if (visible_p)
19563 vpos = ypos;
19564 else
19565 vpos = 0;
19567 else
19569 CHECK_NUMBER (row);
19570 vpos = XINT (row);
19572 matrix = XWINDOW (selected_window)->current_matrix;
19573 if (vpos >= 0 && vpos < matrix->nrows)
19574 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19575 vpos,
19576 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19577 return Qnil;
19581 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19582 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19583 Interactively, ROW is the prefix numeric argument and defaults to zero.
19584 GLYPHS 0 means don't dump glyphs.
19585 GLYPHS 1 means dump glyphs in short form.
19586 GLYPHS > 1 or omitted means dump glyphs in long form.
19588 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19589 do nothing. */)
19590 (Lisp_Object row, Lisp_Object glyphs)
19592 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19593 struct frame *sf = SELECTED_FRAME ();
19594 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19595 EMACS_INT vpos;
19597 if (NILP (row))
19598 vpos = 0;
19599 else
19601 CHECK_NUMBER (row);
19602 vpos = XINT (row);
19604 if (vpos >= 0 && vpos < m->nrows)
19605 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19606 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19607 #endif
19608 return Qnil;
19612 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19613 doc: /* Toggle tracing of redisplay.
19614 With ARG, turn tracing on if and only if ARG is positive. */)
19615 (Lisp_Object arg)
19617 if (NILP (arg))
19618 trace_redisplay_p = !trace_redisplay_p;
19619 else
19621 arg = Fprefix_numeric_value (arg);
19622 trace_redisplay_p = XINT (arg) > 0;
19625 return Qnil;
19629 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19630 doc: /* Like `format', but print result to stderr.
19631 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19632 (ptrdiff_t nargs, Lisp_Object *args)
19634 Lisp_Object s = Fformat (nargs, args);
19635 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19636 return Qnil;
19639 #endif /* GLYPH_DEBUG */
19643 /***********************************************************************
19644 Building Desired Matrix Rows
19645 ***********************************************************************/
19647 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19648 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19650 static struct glyph_row *
19651 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19653 struct frame *f = XFRAME (WINDOW_FRAME (w));
19654 struct buffer *buffer = XBUFFER (w->contents);
19655 struct buffer *old = current_buffer;
19656 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19657 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19658 const unsigned char *arrow_end = arrow_string + arrow_len;
19659 const unsigned char *p;
19660 struct it it;
19661 bool multibyte_p;
19662 int n_glyphs_before;
19664 set_buffer_temp (buffer);
19665 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19666 scratch_glyph_row.reversed_p = false;
19667 it.glyph_row->used[TEXT_AREA] = 0;
19668 SET_TEXT_POS (it.position, 0, 0);
19670 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19671 p = arrow_string;
19672 while (p < arrow_end)
19674 Lisp_Object face, ilisp;
19676 /* Get the next character. */
19677 if (multibyte_p)
19678 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19679 else
19681 it.c = it.char_to_display = *p, it.len = 1;
19682 if (! ASCII_CHAR_P (it.c))
19683 it.char_to_display = BYTE8_TO_CHAR (it.c);
19685 p += it.len;
19687 /* Get its face. */
19688 ilisp = make_number (p - arrow_string);
19689 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19690 it.face_id = compute_char_face (f, it.char_to_display, face);
19692 /* Compute its width, get its glyphs. */
19693 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19694 SET_TEXT_POS (it.position, -1, -1);
19695 PRODUCE_GLYPHS (&it);
19697 /* If this character doesn't fit any more in the line, we have
19698 to remove some glyphs. */
19699 if (it.current_x > it.last_visible_x)
19701 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19702 break;
19706 set_buffer_temp (old);
19707 return it.glyph_row;
19711 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19712 glyphs to insert is determined by produce_special_glyphs. */
19714 static void
19715 insert_left_trunc_glyphs (struct it *it)
19717 struct it truncate_it;
19718 struct glyph *from, *end, *to, *toend;
19720 eassert (!FRAME_WINDOW_P (it->f)
19721 || (!it->glyph_row->reversed_p
19722 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19723 || (it->glyph_row->reversed_p
19724 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19726 /* Get the truncation glyphs. */
19727 truncate_it = *it;
19728 truncate_it.current_x = 0;
19729 truncate_it.face_id = DEFAULT_FACE_ID;
19730 truncate_it.glyph_row = &scratch_glyph_row;
19731 truncate_it.area = TEXT_AREA;
19732 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19733 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19734 truncate_it.object = Qnil;
19735 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19737 /* Overwrite glyphs from IT with truncation glyphs. */
19738 if (!it->glyph_row->reversed_p)
19740 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19742 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19743 end = from + tused;
19744 to = it->glyph_row->glyphs[TEXT_AREA];
19745 toend = to + it->glyph_row->used[TEXT_AREA];
19746 if (FRAME_WINDOW_P (it->f))
19748 /* On GUI frames, when variable-size fonts are displayed,
19749 the truncation glyphs may need more pixels than the row's
19750 glyphs they overwrite. We overwrite more glyphs to free
19751 enough screen real estate, and enlarge the stretch glyph
19752 on the right (see display_line), if there is one, to
19753 preserve the screen position of the truncation glyphs on
19754 the right. */
19755 int w = 0;
19756 struct glyph *g = to;
19757 short used;
19759 /* The first glyph could be partially visible, in which case
19760 it->glyph_row->x will be negative. But we want the left
19761 truncation glyphs to be aligned at the left margin of the
19762 window, so we override the x coordinate at which the row
19763 will begin. */
19764 it->glyph_row->x = 0;
19765 while (g < toend && w < it->truncation_pixel_width)
19767 w += g->pixel_width;
19768 ++g;
19770 if (g - to - tused > 0)
19772 memmove (to + tused, g, (toend - g) * sizeof(*g));
19773 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19775 used = it->glyph_row->used[TEXT_AREA];
19776 if (it->glyph_row->truncated_on_right_p
19777 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19778 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19779 == STRETCH_GLYPH)
19781 int extra = w - it->truncation_pixel_width;
19783 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19787 while (from < end)
19788 *to++ = *from++;
19790 /* There may be padding glyphs left over. Overwrite them too. */
19791 if (!FRAME_WINDOW_P (it->f))
19793 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19795 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19796 while (from < end)
19797 *to++ = *from++;
19801 if (to > toend)
19802 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19804 else
19806 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19808 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19809 that back to front. */
19810 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19811 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19812 toend = it->glyph_row->glyphs[TEXT_AREA];
19813 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19814 if (FRAME_WINDOW_P (it->f))
19816 int w = 0;
19817 struct glyph *g = to;
19819 while (g >= toend && w < it->truncation_pixel_width)
19821 w += g->pixel_width;
19822 --g;
19824 if (to - g - tused > 0)
19825 to = g + tused;
19826 if (it->glyph_row->truncated_on_right_p
19827 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19828 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19830 int extra = w - it->truncation_pixel_width;
19832 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19836 while (from >= end && to >= toend)
19837 *to-- = *from--;
19838 if (!FRAME_WINDOW_P (it->f))
19840 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19842 from =
19843 truncate_it.glyph_row->glyphs[TEXT_AREA]
19844 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19845 while (from >= end && to >= toend)
19846 *to-- = *from--;
19849 if (from >= end)
19851 /* Need to free some room before prepending additional
19852 glyphs. */
19853 int move_by = from - end + 1;
19854 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19855 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19857 for ( ; g >= g0; g--)
19858 g[move_by] = *g;
19859 while (from >= end)
19860 *to-- = *from--;
19861 it->glyph_row->used[TEXT_AREA] += move_by;
19866 /* Compute the hash code for ROW. */
19867 unsigned
19868 row_hash (struct glyph_row *row)
19870 int area, k;
19871 unsigned hashval = 0;
19873 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19874 for (k = 0; k < row->used[area]; ++k)
19875 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19876 + row->glyphs[area][k].u.val
19877 + row->glyphs[area][k].face_id
19878 + row->glyphs[area][k].padding_p
19879 + (row->glyphs[area][k].type << 2));
19881 return hashval;
19884 /* Compute the pixel height and width of IT->glyph_row.
19886 Most of the time, ascent and height of a display line will be equal
19887 to the max_ascent and max_height values of the display iterator
19888 structure. This is not the case if
19890 1. We hit ZV without displaying anything. In this case, max_ascent
19891 and max_height will be zero.
19893 2. We have some glyphs that don't contribute to the line height.
19894 (The glyph row flag contributes_to_line_height_p is for future
19895 pixmap extensions).
19897 The first case is easily covered by using default values because in
19898 these cases, the line height does not really matter, except that it
19899 must not be zero. */
19901 static void
19902 compute_line_metrics (struct it *it)
19904 struct glyph_row *row = it->glyph_row;
19906 if (FRAME_WINDOW_P (it->f))
19908 int i, min_y, max_y;
19910 /* The line may consist of one space only, that was added to
19911 place the cursor on it. If so, the row's height hasn't been
19912 computed yet. */
19913 if (row->height == 0)
19915 if (it->max_ascent + it->max_descent == 0)
19916 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19917 row->ascent = it->max_ascent;
19918 row->height = it->max_ascent + it->max_descent;
19919 row->phys_ascent = it->max_phys_ascent;
19920 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19921 row->extra_line_spacing = it->max_extra_line_spacing;
19924 /* Compute the width of this line. */
19925 row->pixel_width = row->x;
19926 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19927 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19929 eassert (row->pixel_width >= 0);
19930 eassert (row->ascent >= 0 && row->height > 0);
19932 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19933 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19935 /* If first line's physical ascent is larger than its logical
19936 ascent, use the physical ascent, and make the row taller.
19937 This makes accented characters fully visible. */
19938 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19939 && row->phys_ascent > row->ascent)
19941 row->height += row->phys_ascent - row->ascent;
19942 row->ascent = row->phys_ascent;
19945 /* Compute how much of the line is visible. */
19946 row->visible_height = row->height;
19948 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19949 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19951 if (row->y < min_y)
19952 row->visible_height -= min_y - row->y;
19953 if (row->y + row->height > max_y)
19954 row->visible_height -= row->y + row->height - max_y;
19956 else
19958 row->pixel_width = row->used[TEXT_AREA];
19959 if (row->continued_p)
19960 row->pixel_width -= it->continuation_pixel_width;
19961 else if (row->truncated_on_right_p)
19962 row->pixel_width -= it->truncation_pixel_width;
19963 row->ascent = row->phys_ascent = 0;
19964 row->height = row->phys_height = row->visible_height = 1;
19965 row->extra_line_spacing = 0;
19968 /* Compute a hash code for this row. */
19969 row->hash = row_hash (row);
19971 it->max_ascent = it->max_descent = 0;
19972 it->max_phys_ascent = it->max_phys_descent = 0;
19976 /* Append one space to the glyph row of iterator IT if doing a
19977 window-based redisplay. The space has the same face as
19978 IT->face_id. Value is true if a space was added.
19980 This function is called to make sure that there is always one glyph
19981 at the end of a glyph row that the cursor can be set on under
19982 window-systems. (If there weren't such a glyph we would not know
19983 how wide and tall a box cursor should be displayed).
19985 At the same time this space let's a nicely handle clearing to the
19986 end of the line if the row ends in italic text. */
19988 static bool
19989 append_space_for_newline (struct it *it, bool default_face_p)
19991 if (FRAME_WINDOW_P (it->f))
19993 int n = it->glyph_row->used[TEXT_AREA];
19995 if (it->glyph_row->glyphs[TEXT_AREA] + n
19996 < it->glyph_row->glyphs[1 + TEXT_AREA])
19998 /* Save some values that must not be changed.
19999 Must save IT->c and IT->len because otherwise
20000 ITERATOR_AT_END_P wouldn't work anymore after
20001 append_space_for_newline has been called. */
20002 enum display_element_type saved_what = it->what;
20003 int saved_c = it->c, saved_len = it->len;
20004 int saved_char_to_display = it->char_to_display;
20005 int saved_x = it->current_x;
20006 int saved_face_id = it->face_id;
20007 bool saved_box_end = it->end_of_box_run_p;
20008 struct text_pos saved_pos;
20009 Lisp_Object saved_object;
20010 struct face *face;
20012 saved_object = it->object;
20013 saved_pos = it->position;
20015 it->what = IT_CHARACTER;
20016 memset (&it->position, 0, sizeof it->position);
20017 it->object = Qnil;
20018 it->c = it->char_to_display = ' ';
20019 it->len = 1;
20021 /* If the default face was remapped, be sure to use the
20022 remapped face for the appended newline. */
20023 if (default_face_p)
20024 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
20025 else if (it->face_before_selective_p)
20026 it->face_id = it->saved_face_id;
20027 face = FACE_FROM_ID (it->f, it->face_id);
20028 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
20029 /* In R2L rows, we will prepend a stretch glyph that will
20030 have the end_of_box_run_p flag set for it, so there's no
20031 need for the appended newline glyph to have that flag
20032 set. */
20033 if (it->glyph_row->reversed_p
20034 /* But if the appended newline glyph goes all the way to
20035 the end of the row, there will be no stretch glyph,
20036 so leave the box flag set. */
20037 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
20038 it->end_of_box_run_p = false;
20040 PRODUCE_GLYPHS (it);
20042 #ifdef HAVE_WINDOW_SYSTEM
20043 /* Make sure this space glyph has the right ascent and
20044 descent values, or else cursor at end of line will look
20045 funny, and height of empty lines will be incorrect. */
20046 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
20047 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20048 if (n == 0)
20050 Lisp_Object height, total_height;
20051 int extra_line_spacing = it->extra_line_spacing;
20052 int boff = font->baseline_offset;
20054 if (font->vertical_centering)
20055 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20057 it->object = saved_object; /* get_it_property needs this */
20058 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
20059 /* Must do a subset of line height processing from
20060 x_produce_glyph for newline characters. */
20061 height = get_it_property (it, Qline_height);
20062 if (CONSP (height)
20063 && CONSP (XCDR (height))
20064 && NILP (XCDR (XCDR (height))))
20066 total_height = XCAR (XCDR (height));
20067 height = XCAR (height);
20069 else
20070 total_height = Qnil;
20071 height = calc_line_height_property (it, height, font, boff, true);
20073 if (it->override_ascent >= 0)
20075 it->ascent = it->override_ascent;
20076 it->descent = it->override_descent;
20077 boff = it->override_boff;
20079 if (EQ (height, Qt))
20080 extra_line_spacing = 0;
20081 else
20083 Lisp_Object spacing;
20085 it->phys_ascent = it->ascent;
20086 it->phys_descent = it->descent;
20087 if (!NILP (height)
20088 && XINT (height) > it->ascent + it->descent)
20089 it->ascent = XINT (height) - it->descent;
20091 if (!NILP (total_height))
20092 spacing = calc_line_height_property (it, total_height, font,
20093 boff, false);
20094 else
20096 spacing = get_it_property (it, Qline_spacing);
20097 spacing = calc_line_height_property (it, spacing, font,
20098 boff, false);
20100 if (INTEGERP (spacing))
20102 extra_line_spacing = XINT (spacing);
20103 if (!NILP (total_height))
20104 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20107 if (extra_line_spacing > 0)
20109 it->descent += extra_line_spacing;
20110 if (extra_line_spacing > it->max_extra_line_spacing)
20111 it->max_extra_line_spacing = extra_line_spacing;
20113 it->max_ascent = it->ascent;
20114 it->max_descent = it->descent;
20115 /* Make sure compute_line_metrics recomputes the row height. */
20116 it->glyph_row->height = 0;
20119 g->ascent = it->max_ascent;
20120 g->descent = it->max_descent;
20121 #endif
20123 it->override_ascent = -1;
20124 it->constrain_row_ascent_descent_p = false;
20125 it->current_x = saved_x;
20126 it->object = saved_object;
20127 it->position = saved_pos;
20128 it->what = saved_what;
20129 it->face_id = saved_face_id;
20130 it->len = saved_len;
20131 it->c = saved_c;
20132 it->char_to_display = saved_char_to_display;
20133 it->end_of_box_run_p = saved_box_end;
20134 return true;
20138 return false;
20142 /* Extend the face of the last glyph in the text area of IT->glyph_row
20143 to the end of the display line. Called from display_line. If the
20144 glyph row is empty, add a space glyph to it so that we know the
20145 face to draw. Set the glyph row flag fill_line_p. If the glyph
20146 row is R2L, prepend a stretch glyph to cover the empty space to the
20147 left of the leftmost glyph. */
20149 static void
20150 extend_face_to_end_of_line (struct it *it)
20152 struct face *face, *default_face;
20153 struct frame *f = it->f;
20155 /* If line is already filled, do nothing. Non window-system frames
20156 get a grace of one more ``pixel'' because their characters are
20157 1-``pixel'' wide, so they hit the equality too early. This grace
20158 is needed only for R2L rows that are not continued, to produce
20159 one extra blank where we could display the cursor. */
20160 if ((it->current_x >= it->last_visible_x
20161 + (!FRAME_WINDOW_P (f)
20162 && it->glyph_row->reversed_p
20163 && !it->glyph_row->continued_p))
20164 /* If the window has display margins, we will need to extend
20165 their face even if the text area is filled. */
20166 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20167 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20168 return;
20170 /* The default face, possibly remapped. */
20171 default_face = FACE_FROM_ID_OR_NULL (f,
20172 lookup_basic_face (f, DEFAULT_FACE_ID));
20174 /* Face extension extends the background and box of IT->face_id
20175 to the end of the line. If the background equals the background
20176 of the frame, we don't have to do anything. */
20177 face = FACE_FROM_ID (f, (it->face_before_selective_p
20178 ? it->saved_face_id
20179 : it->face_id));
20181 if (FRAME_WINDOW_P (f)
20182 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20183 && face->box == FACE_NO_BOX
20184 && face->background == FRAME_BACKGROUND_PIXEL (f)
20185 #ifdef HAVE_WINDOW_SYSTEM
20186 && !face->stipple
20187 #endif
20188 && !it->glyph_row->reversed_p)
20189 return;
20191 /* Set the glyph row flag indicating that the face of the last glyph
20192 in the text area has to be drawn to the end of the text area. */
20193 it->glyph_row->fill_line_p = true;
20195 /* If current character of IT is not ASCII, make sure we have the
20196 ASCII face. This will be automatically undone the next time
20197 get_next_display_element returns a multibyte character. Note
20198 that the character will always be single byte in unibyte
20199 text. */
20200 if (!ASCII_CHAR_P (it->c))
20202 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20205 if (FRAME_WINDOW_P (f))
20207 /* If the row is empty, add a space with the current face of IT,
20208 so that we know which face to draw. */
20209 if (it->glyph_row->used[TEXT_AREA] == 0)
20211 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20212 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20213 it->glyph_row->used[TEXT_AREA] = 1;
20215 /* Mode line and the header line don't have margins, and
20216 likewise the frame's tool-bar window, if there is any. */
20217 if (!(it->glyph_row->mode_line_p
20218 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20219 || (WINDOWP (f->tool_bar_window)
20220 && it->w == XWINDOW (f->tool_bar_window))
20221 #endif
20224 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20225 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20227 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20228 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20229 default_face->id;
20230 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20232 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20233 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20235 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20236 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20237 default_face->id;
20238 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20241 #ifdef HAVE_WINDOW_SYSTEM
20242 if (it->glyph_row->reversed_p)
20244 /* Prepend a stretch glyph to the row, such that the
20245 rightmost glyph will be drawn flushed all the way to the
20246 right margin of the window. The stretch glyph that will
20247 occupy the empty space, if any, to the left of the
20248 glyphs. */
20249 struct font *font = face->font ? face->font : FRAME_FONT (f);
20250 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20251 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20252 struct glyph *g;
20253 int row_width, stretch_ascent, stretch_width;
20254 struct text_pos saved_pos;
20255 int saved_face_id;
20256 bool saved_avoid_cursor, saved_box_start;
20258 for (row_width = 0, g = row_start; g < row_end; g++)
20259 row_width += g->pixel_width;
20261 /* FIXME: There are various minor display glitches in R2L
20262 rows when only one of the fringes is missing. The
20263 strange condition below produces the least bad effect. */
20264 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20265 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20266 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20267 stretch_width = window_box_width (it->w, TEXT_AREA);
20268 else
20269 stretch_width = it->last_visible_x - it->first_visible_x;
20270 stretch_width -= row_width;
20272 if (stretch_width > 0)
20274 stretch_ascent =
20275 (((it->ascent + it->descent)
20276 * FONT_BASE (font)) / FONT_HEIGHT (font));
20277 saved_pos = it->position;
20278 memset (&it->position, 0, sizeof it->position);
20279 saved_avoid_cursor = it->avoid_cursor_p;
20280 it->avoid_cursor_p = true;
20281 saved_face_id = it->face_id;
20282 saved_box_start = it->start_of_box_run_p;
20283 /* The last row's stretch glyph should get the default
20284 face, to avoid painting the rest of the window with
20285 the region face, if the region ends at ZV. */
20286 if (it->glyph_row->ends_at_zv_p)
20287 it->face_id = default_face->id;
20288 else
20289 it->face_id = face->id;
20290 it->start_of_box_run_p = false;
20291 append_stretch_glyph (it, Qnil, stretch_width,
20292 it->ascent + it->descent, stretch_ascent);
20293 it->position = saved_pos;
20294 it->avoid_cursor_p = saved_avoid_cursor;
20295 it->face_id = saved_face_id;
20296 it->start_of_box_run_p = saved_box_start;
20298 /* If stretch_width comes out negative, it means that the
20299 last glyph is only partially visible. In R2L rows, we
20300 want the leftmost glyph to be partially visible, so we
20301 need to give the row the corresponding left offset. */
20302 if (stretch_width < 0)
20303 it->glyph_row->x = stretch_width;
20305 #endif /* HAVE_WINDOW_SYSTEM */
20307 else
20309 /* Save some values that must not be changed. */
20310 int saved_x = it->current_x;
20311 struct text_pos saved_pos;
20312 Lisp_Object saved_object;
20313 enum display_element_type saved_what = it->what;
20314 int saved_face_id = it->face_id;
20316 saved_object = it->object;
20317 saved_pos = it->position;
20319 it->what = IT_CHARACTER;
20320 memset (&it->position, 0, sizeof it->position);
20321 it->object = Qnil;
20322 it->c = it->char_to_display = ' ';
20323 it->len = 1;
20325 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20326 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20327 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20328 && !it->glyph_row->mode_line_p
20329 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20331 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20332 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20334 for (it->current_x = 0; g < e; g++)
20335 it->current_x += g->pixel_width;
20337 it->area = LEFT_MARGIN_AREA;
20338 it->face_id = default_face->id;
20339 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20340 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20341 && g < it->glyph_row->glyphs[TEXT_AREA])
20343 PRODUCE_GLYPHS (it);
20344 /* term.c:produce_glyphs advances it->current_x only for
20345 TEXT_AREA. */
20346 it->current_x += it->pixel_width;
20347 g++;
20350 it->current_x = saved_x;
20351 it->area = TEXT_AREA;
20354 /* The last row's blank glyphs should get the default face, to
20355 avoid painting the rest of the window with the region face,
20356 if the region ends at ZV. */
20357 if (it->glyph_row->ends_at_zv_p)
20358 it->face_id = default_face->id;
20359 else
20360 it->face_id = face->id;
20361 PRODUCE_GLYPHS (it);
20363 while (it->current_x <= it->last_visible_x)
20364 PRODUCE_GLYPHS (it);
20366 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20367 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20368 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20369 && !it->glyph_row->mode_line_p
20370 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20372 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20373 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20375 for ( ; g < e; g++)
20376 it->current_x += g->pixel_width;
20378 it->area = RIGHT_MARGIN_AREA;
20379 it->face_id = default_face->id;
20380 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20381 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20382 && g < it->glyph_row->glyphs[LAST_AREA])
20384 PRODUCE_GLYPHS (it);
20385 it->current_x += it->pixel_width;
20386 g++;
20389 it->area = TEXT_AREA;
20392 /* Don't count these blanks really. It would let us insert a left
20393 truncation glyph below and make us set the cursor on them, maybe. */
20394 it->current_x = saved_x;
20395 it->object = saved_object;
20396 it->position = saved_pos;
20397 it->what = saved_what;
20398 it->face_id = saved_face_id;
20403 /* Value is true if text starting at CHARPOS in current_buffer is
20404 trailing whitespace. */
20406 static bool
20407 trailing_whitespace_p (ptrdiff_t charpos)
20409 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20410 int c = 0;
20412 while (bytepos < ZV_BYTE
20413 && (c = FETCH_CHAR (bytepos),
20414 c == ' ' || c == '\t'))
20415 ++bytepos;
20417 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20419 if (bytepos != PT_BYTE)
20420 return true;
20422 return false;
20426 /* Highlight trailing whitespace, if any, in ROW. */
20428 static void
20429 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20431 int used = row->used[TEXT_AREA];
20433 if (used)
20435 struct glyph *start = row->glyphs[TEXT_AREA];
20436 struct glyph *glyph = start + used - 1;
20438 if (row->reversed_p)
20440 /* Right-to-left rows need to be processed in the opposite
20441 direction, so swap the edge pointers. */
20442 glyph = start;
20443 start = row->glyphs[TEXT_AREA] + used - 1;
20446 /* Skip over glyphs inserted to display the cursor at the
20447 end of a line, for extending the face of the last glyph
20448 to the end of the line on terminals, and for truncation
20449 and continuation glyphs. */
20450 if (!row->reversed_p)
20452 while (glyph >= start
20453 && glyph->type == CHAR_GLYPH
20454 && NILP (glyph->object))
20455 --glyph;
20457 else
20459 while (glyph <= start
20460 && glyph->type == CHAR_GLYPH
20461 && NILP (glyph->object))
20462 ++glyph;
20465 /* If last glyph is a space or stretch, and it's trailing
20466 whitespace, set the face of all trailing whitespace glyphs in
20467 IT->glyph_row to `trailing-whitespace'. */
20468 if ((row->reversed_p ? glyph <= start : glyph >= start)
20469 && BUFFERP (glyph->object)
20470 && (glyph->type == STRETCH_GLYPH
20471 || (glyph->type == CHAR_GLYPH
20472 && glyph->u.ch == ' '))
20473 && trailing_whitespace_p (glyph->charpos))
20475 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20476 if (face_id < 0)
20477 return;
20479 if (!row->reversed_p)
20481 while (glyph >= start
20482 && BUFFERP (glyph->object)
20483 && (glyph->type == STRETCH_GLYPH
20484 || (glyph->type == CHAR_GLYPH
20485 && glyph->u.ch == ' ')))
20486 (glyph--)->face_id = face_id;
20488 else
20490 while (glyph <= start
20491 && BUFFERP (glyph->object)
20492 && (glyph->type == STRETCH_GLYPH
20493 || (glyph->type == CHAR_GLYPH
20494 && glyph->u.ch == ' ')))
20495 (glyph++)->face_id = face_id;
20502 /* Value is true if glyph row ROW should be
20503 considered to hold the buffer position CHARPOS. */
20505 static bool
20506 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20508 bool result = true;
20510 if (charpos == CHARPOS (row->end.pos)
20511 || charpos == MATRIX_ROW_END_CHARPOS (row))
20513 /* Suppose the row ends on a string.
20514 Unless the row is continued, that means it ends on a newline
20515 in the string. If it's anything other than a display string
20516 (e.g., a before-string from an overlay), we don't want the
20517 cursor there. (This heuristic seems to give the optimal
20518 behavior for the various types of multi-line strings.)
20519 One exception: if the string has `cursor' property on one of
20520 its characters, we _do_ want the cursor there. */
20521 if (CHARPOS (row->end.string_pos) >= 0)
20523 if (row->continued_p)
20524 result = true;
20525 else
20527 /* Check for `display' property. */
20528 struct glyph *beg = row->glyphs[TEXT_AREA];
20529 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20530 struct glyph *glyph;
20532 result = false;
20533 for (glyph = end; glyph >= beg; --glyph)
20534 if (STRINGP (glyph->object))
20536 Lisp_Object prop
20537 = Fget_char_property (make_number (charpos),
20538 Qdisplay, Qnil);
20539 result =
20540 (!NILP (prop)
20541 && display_prop_string_p (prop, glyph->object));
20542 /* If there's a `cursor' property on one of the
20543 string's characters, this row is a cursor row,
20544 even though this is not a display string. */
20545 if (!result)
20547 Lisp_Object s = glyph->object;
20549 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20551 ptrdiff_t gpos = glyph->charpos;
20553 if (!NILP (Fget_char_property (make_number (gpos),
20554 Qcursor, s)))
20556 result = true;
20557 break;
20561 break;
20565 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20567 /* If the row ends in middle of a real character,
20568 and the line is continued, we want the cursor here.
20569 That's because CHARPOS (ROW->end.pos) would equal
20570 PT if PT is before the character. */
20571 if (!row->ends_in_ellipsis_p)
20572 result = row->continued_p;
20573 else
20574 /* If the row ends in an ellipsis, then
20575 CHARPOS (ROW->end.pos) will equal point after the
20576 invisible text. We want that position to be displayed
20577 after the ellipsis. */
20578 result = false;
20580 /* If the row ends at ZV, display the cursor at the end of that
20581 row instead of at the start of the row below. */
20582 else
20583 result = row->ends_at_zv_p;
20586 return result;
20589 /* Value is true if glyph row ROW should be
20590 used to hold the cursor. */
20592 static bool
20593 cursor_row_p (struct glyph_row *row)
20595 return row_for_charpos_p (row, PT);
20600 /* Push the property PROP so that it will be rendered at the current
20601 position in IT. Return true if PROP was successfully pushed, false
20602 otherwise. Called from handle_line_prefix to handle the
20603 `line-prefix' and `wrap-prefix' properties. */
20605 static bool
20606 push_prefix_prop (struct it *it, Lisp_Object prop)
20608 struct text_pos pos =
20609 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20611 eassert (it->method == GET_FROM_BUFFER
20612 || it->method == GET_FROM_DISPLAY_VECTOR
20613 || it->method == GET_FROM_STRING
20614 || it->method == GET_FROM_IMAGE);
20616 /* We need to save the current buffer/string position, so it will be
20617 restored by pop_it, because iterate_out_of_display_property
20618 depends on that being set correctly, but some situations leave
20619 it->position not yet set when this function is called. */
20620 push_it (it, &pos);
20622 if (STRINGP (prop))
20624 if (SCHARS (prop) == 0)
20626 pop_it (it);
20627 return false;
20630 it->string = prop;
20631 it->string_from_prefix_prop_p = true;
20632 it->multibyte_p = STRING_MULTIBYTE (it->string);
20633 it->current.overlay_string_index = -1;
20634 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20635 it->end_charpos = it->string_nchars = SCHARS (it->string);
20636 it->method = GET_FROM_STRING;
20637 it->stop_charpos = 0;
20638 it->prev_stop = 0;
20639 it->base_level_stop = 0;
20640 it->cmp_it.id = -1;
20642 /* Force paragraph direction to be that of the parent
20643 buffer/string. */
20644 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20645 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20646 else
20647 it->paragraph_embedding = L2R;
20649 /* Set up the bidi iterator for this display string. */
20650 if (it->bidi_p)
20652 it->bidi_it.string.lstring = it->string;
20653 it->bidi_it.string.s = NULL;
20654 it->bidi_it.string.schars = it->end_charpos;
20655 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20656 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20657 it->bidi_it.string.unibyte = !it->multibyte_p;
20658 it->bidi_it.w = it->w;
20659 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20662 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20664 it->method = GET_FROM_STRETCH;
20665 it->object = prop;
20667 #ifdef HAVE_WINDOW_SYSTEM
20668 else if (IMAGEP (prop))
20670 it->what = IT_IMAGE;
20671 it->image_id = lookup_image (it->f, prop);
20672 it->method = GET_FROM_IMAGE;
20674 #endif /* HAVE_WINDOW_SYSTEM */
20675 else
20677 pop_it (it); /* bogus display property, give up */
20678 return false;
20681 return true;
20684 /* Return the character-property PROP at the current position in IT. */
20686 static Lisp_Object
20687 get_it_property (struct it *it, Lisp_Object prop)
20689 Lisp_Object position, object = it->object;
20691 if (STRINGP (object))
20692 position = make_number (IT_STRING_CHARPOS (*it));
20693 else if (BUFFERP (object))
20695 position = make_number (IT_CHARPOS (*it));
20696 object = it->window;
20698 else
20699 return Qnil;
20701 return Fget_char_property (position, prop, object);
20704 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20706 static void
20707 handle_line_prefix (struct it *it)
20709 Lisp_Object prefix;
20711 if (it->continuation_lines_width > 0)
20713 prefix = get_it_property (it, Qwrap_prefix);
20714 if (NILP (prefix))
20715 prefix = Vwrap_prefix;
20717 else
20719 prefix = get_it_property (it, Qline_prefix);
20720 if (NILP (prefix))
20721 prefix = Vline_prefix;
20723 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20725 /* If the prefix is wider than the window, and we try to wrap
20726 it, it would acquire its own wrap prefix, and so on till the
20727 iterator stack overflows. So, don't wrap the prefix. */
20728 it->line_wrap = TRUNCATE;
20729 it->avoid_cursor_p = true;
20735 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20736 only for R2L lines from display_line and display_string, when they
20737 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20738 the line/string needs to be continued on the next glyph row. */
20739 static void
20740 unproduce_glyphs (struct it *it, int n)
20742 struct glyph *glyph, *end;
20744 eassert (it->glyph_row);
20745 eassert (it->glyph_row->reversed_p);
20746 eassert (it->area == TEXT_AREA);
20747 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20749 if (n > it->glyph_row->used[TEXT_AREA])
20750 n = it->glyph_row->used[TEXT_AREA];
20751 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20752 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20753 for ( ; glyph < end; glyph++)
20754 glyph[-n] = *glyph;
20757 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20758 and ROW->maxpos. */
20759 static void
20760 find_row_edges (struct it *it, struct glyph_row *row,
20761 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20762 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20764 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20765 lines' rows is implemented for bidi-reordered rows. */
20767 /* ROW->minpos is the value of min_pos, the minimal buffer position
20768 we have in ROW, or ROW->start.pos if that is smaller. */
20769 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20770 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20771 else
20772 /* We didn't find buffer positions smaller than ROW->start, or
20773 didn't find _any_ valid buffer positions in any of the glyphs,
20774 so we must trust the iterator's computed positions. */
20775 row->minpos = row->start.pos;
20776 if (max_pos <= 0)
20778 max_pos = CHARPOS (it->current.pos);
20779 max_bpos = BYTEPOS (it->current.pos);
20782 /* Here are the various use-cases for ending the row, and the
20783 corresponding values for ROW->maxpos:
20785 Line ends in a newline from buffer eol_pos + 1
20786 Line is continued from buffer max_pos + 1
20787 Line is truncated on right it->current.pos
20788 Line ends in a newline from string max_pos + 1(*)
20789 (*) + 1 only when line ends in a forward scan
20790 Line is continued from string max_pos
20791 Line is continued from display vector max_pos
20792 Line is entirely from a string min_pos == max_pos
20793 Line is entirely from a display vector min_pos == max_pos
20794 Line that ends at ZV ZV
20796 If you discover other use-cases, please add them here as
20797 appropriate. */
20798 if (row->ends_at_zv_p)
20799 row->maxpos = it->current.pos;
20800 else if (row->used[TEXT_AREA])
20802 bool seen_this_string = false;
20803 struct glyph_row *r1 = row - 1;
20805 /* Did we see the same display string on the previous row? */
20806 if (STRINGP (it->object)
20807 /* this is not the first row */
20808 && row > it->w->desired_matrix->rows
20809 /* previous row is not the header line */
20810 && !r1->mode_line_p
20811 /* previous row also ends in a newline from a string */
20812 && r1->ends_in_newline_from_string_p)
20814 struct glyph *start, *end;
20816 /* Search for the last glyph of the previous row that came
20817 from buffer or string. Depending on whether the row is
20818 L2R or R2L, we need to process it front to back or the
20819 other way round. */
20820 if (!r1->reversed_p)
20822 start = r1->glyphs[TEXT_AREA];
20823 end = start + r1->used[TEXT_AREA];
20824 /* Glyphs inserted by redisplay have nil as their object. */
20825 while (end > start
20826 && NILP ((end - 1)->object)
20827 && (end - 1)->charpos <= 0)
20828 --end;
20829 if (end > start)
20831 if (EQ ((end - 1)->object, it->object))
20832 seen_this_string = true;
20834 else
20835 /* If all the glyphs of the previous row were inserted
20836 by redisplay, it means the previous row was
20837 produced from a single newline, which is only
20838 possible if that newline came from the same string
20839 as the one which produced this ROW. */
20840 seen_this_string = true;
20842 else
20844 end = r1->glyphs[TEXT_AREA] - 1;
20845 start = end + r1->used[TEXT_AREA];
20846 while (end < start
20847 && NILP ((end + 1)->object)
20848 && (end + 1)->charpos <= 0)
20849 ++end;
20850 if (end < start)
20852 if (EQ ((end + 1)->object, it->object))
20853 seen_this_string = true;
20855 else
20856 seen_this_string = true;
20859 /* Take note of each display string that covers a newline only
20860 once, the first time we see it. This is for when a display
20861 string includes more than one newline in it. */
20862 if (row->ends_in_newline_from_string_p && !seen_this_string)
20864 /* If we were scanning the buffer forward when we displayed
20865 the string, we want to account for at least one buffer
20866 position that belongs to this row (position covered by
20867 the display string), so that cursor positioning will
20868 consider this row as a candidate when point is at the end
20869 of the visual line represented by this row. This is not
20870 required when scanning back, because max_pos will already
20871 have a much larger value. */
20872 if (CHARPOS (row->end.pos) > max_pos)
20873 INC_BOTH (max_pos, max_bpos);
20874 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20876 else if (CHARPOS (it->eol_pos) > 0)
20877 SET_TEXT_POS (row->maxpos,
20878 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20879 else if (row->continued_p)
20881 /* If max_pos is different from IT's current position, it
20882 means IT->method does not belong to the display element
20883 at max_pos. However, it also means that the display
20884 element at max_pos was displayed in its entirety on this
20885 line, which is equivalent to saying that the next line
20886 starts at the next buffer position. */
20887 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20888 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20889 else
20891 INC_BOTH (max_pos, max_bpos);
20892 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20895 else if (row->truncated_on_right_p)
20896 /* display_line already called reseat_at_next_visible_line_start,
20897 which puts the iterator at the beginning of the next line, in
20898 the logical order. */
20899 row->maxpos = it->current.pos;
20900 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20901 /* A line that is entirely from a string/image/stretch... */
20902 row->maxpos = row->minpos;
20903 else
20904 emacs_abort ();
20906 else
20907 row->maxpos = it->current.pos;
20910 /* Like display_count_lines, but capable of counting outside of the
20911 current narrowed region. */
20912 static ptrdiff_t
20913 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20914 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20916 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20917 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20919 ptrdiff_t val;
20920 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20921 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20922 Fwiden ();
20923 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20924 unbind_to (pdl_count, Qnil);
20925 return val;
20928 /* Count the number of screen lines in window IT->w between character
20929 position IT_CHARPOS(*IT) and the line showing that window's point. */
20930 static ptrdiff_t
20931 display_count_lines_visually (struct it *it)
20933 struct it tem_it;
20934 ptrdiff_t to;
20935 struct text_pos from;
20937 /* If we already calculated a relative line number, use that. This
20938 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20939 are laid out sequentially, one by one, for each sequence of calls
20940 to display_line or other similar function that follows a call to
20941 init_iterator. */
20942 if (it->lnum_bytepos > 0)
20943 return it->lnum + 1;
20944 else
20946 ptrdiff_t count = SPECPDL_INDEX ();
20948 if (IT_CHARPOS (*it) <= PT)
20950 from = it->current.pos;
20951 to = PT;
20953 else
20955 SET_TEXT_POS (from, PT, PT_BYTE);
20956 to = IT_CHARPOS (*it);
20958 start_display (&tem_it, it->w, from);
20959 /* Need to disable visual mode temporarily, since otherwise the
20960 call to move_it_to will cause infinite recursion. */
20961 specbind (Qdisplay_line_numbers, Qrelative);
20962 /* Some redisplay optimizations could invoke us very far from
20963 PT, which will make the caller painfully slow. There should
20964 be no need to go too far beyond the window's bottom, as any
20965 such optimization will fail to show point anyway. */
20966 move_it_to (&tem_it, to, -1,
20967 tem_it.last_visible_y
20968 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20969 -1, MOVE_TO_POS | MOVE_TO_Y);
20970 unbind_to (count, Qnil);
20971 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20975 /* Produce the line-number glyphs for the current glyph_row. If
20976 IT->glyph_row is non-NULL, populate the row with the produced
20977 glyphs. */
20978 static void
20979 maybe_produce_line_number (struct it *it)
20981 ptrdiff_t last_line = it->lnum;
20982 ptrdiff_t start_from, bytepos;
20983 ptrdiff_t this_line;
20984 bool first_time = false;
20985 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20986 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20987 void *itdata = bidi_shelve_cache ();
20989 if (EQ (Vdisplay_line_numbers, Qvisual))
20990 this_line = display_count_lines_visually (it);
20991 else
20993 if (!last_line)
20995 /* If possible, reuse data cached by line-number-mode. */
20996 if (it->w->base_line_number > 0
20997 && it->w->base_line_pos > 0
20998 && it->w->base_line_pos <= IT_CHARPOS (*it)
20999 /* line-number-mode always displays narrowed line
21000 numbers, so we cannot use its data if the user wants
21001 line numbers that disregard narrowing, or if the
21002 buffer's narrowing has just changed. */
21003 && !(display_line_numbers_widen
21004 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
21005 && !current_buffer->clip_changed)
21007 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
21008 last_line = it->w->base_line_number - 1;
21010 else
21011 start_from = beg_byte;
21012 if (!it->lnum_bytepos)
21013 first_time = true;
21015 else
21016 start_from = it->lnum_bytepos;
21018 /* Paranoia: what if someone changes the narrowing since the
21019 last time display_line was called? Shouldn't really happen,
21020 but who knows what some crazy Lisp invoked by :eval could do? */
21021 if (!(beg_byte <= start_from && start_from <= z_byte))
21023 last_line = 0;
21024 start_from = beg_byte;
21027 this_line =
21028 last_line + display_count_lines_logically (start_from,
21029 IT_BYTEPOS (*it),
21030 IT_CHARPOS (*it), &bytepos);
21031 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
21032 eassert (bytepos == IT_BYTEPOS (*it));
21035 /* Record the line number information. */
21036 if (this_line != last_line || !it->lnum_bytepos)
21038 it->lnum = this_line;
21039 it->lnum_bytepos = IT_BYTEPOS (*it);
21042 /* Produce the glyphs for the line number. */
21043 struct it tem_it;
21044 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
21045 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
21046 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
21047 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
21048 int current_lnum_face_id
21049 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
21050 /* Compute point's line number if needed. */
21051 if ((EQ (Vdisplay_line_numbers, Qrelative)
21052 || EQ (Vdisplay_line_numbers, Qvisual)
21053 || lnum_face_id != current_lnum_face_id)
21054 && !it->pt_lnum)
21056 ptrdiff_t ignored;
21057 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
21058 it->pt_lnum =
21059 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
21060 PT, &ignored);
21061 else
21062 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21063 &ignored);
21065 /* Compute the required width if needed. */
21066 if (!it->lnum_width)
21068 if (NATNUMP (Vdisplay_line_numbers_width))
21069 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21071 /* Max line number to be displayed cannot be more than the one
21072 corresponding to the last row of the desired matrix. */
21073 ptrdiff_t max_lnum;
21075 if (NILP (Vdisplay_line_numbers_current_absolute)
21076 && (EQ (Vdisplay_line_numbers, Qrelative)
21077 || EQ (Vdisplay_line_numbers, Qvisual)))
21078 /* We subtract one more because the current line is always
21079 zero in this mode. */
21080 max_lnum = it->w->desired_matrix->nrows - 2;
21081 else if (EQ (Vdisplay_line_numbers, Qvisual))
21082 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21083 else
21084 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21085 max_lnum = max (1, max_lnum);
21086 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21087 eassert (it->lnum_width > 0);
21089 if (EQ (Vdisplay_line_numbers, Qrelative))
21090 lnum_offset = it->pt_lnum;
21091 else if (EQ (Vdisplay_line_numbers, Qvisual))
21092 lnum_offset = 0;
21094 /* Under 'relative', display the absolute line number for the
21095 current line, unless the user requests otherwise. */
21096 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21097 if ((EQ (Vdisplay_line_numbers, Qrelative)
21098 || EQ (Vdisplay_line_numbers, Qvisual))
21099 && lnum_to_display == 0
21100 && !NILP (Vdisplay_line_numbers_current_absolute))
21101 lnum_to_display = it->pt_lnum + 1;
21102 /* In L2R rows we need to append the blank separator, in R2L
21103 rows we need to prepend it. But this function is usually
21104 called when no display elements were produced from the
21105 following line, so the paragraph direction might be unknown.
21106 Therefore we cheat and add 2 blanks, one on either side. */
21107 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21108 strcat (lnum_buf, " ");
21110 /* Setup for producing the glyphs. */
21111 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21112 /* FIXME: Use specialized face. */
21113 DEFAULT_FACE_ID);
21114 scratch_glyph_row.reversed_p = false;
21115 scratch_glyph_row.used[TEXT_AREA] = 0;
21116 SET_TEXT_POS (tem_it.position, 0, 0);
21117 tem_it.avoid_cursor_p = true;
21118 tem_it.bidi_p = true;
21119 tem_it.bidi_it.type = WEAK_EN;
21120 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21121 1 level in R2L paragraphs. Emulate that, assuming we are in
21122 an L2R paragraph. */
21123 tem_it.bidi_it.resolved_level = 2;
21125 /* Produce glyphs for the line number in a scratch glyph_row. */
21126 int n_glyphs_before;
21127 for (const char *p = lnum_buf; *p; p++)
21129 /* For continuation lines and lines after ZV, instead of a line
21130 number, produce a blank prefix of the same width. Use the
21131 default face for the blank field beyond ZV. */
21132 if (beyond_zv)
21133 tem_it.face_id = it->base_face_id;
21134 else if (lnum_face_id != current_lnum_face_id
21135 && (EQ (Vdisplay_line_numbers, Qvisual)
21136 ? this_line == 0
21137 : this_line == it->pt_lnum))
21138 tem_it.face_id = current_lnum_face_id;
21139 else
21140 tem_it.face_id = lnum_face_id;
21141 if (beyond_zv
21142 /* Don't display the same line number more than once. */
21143 || (!EQ (Vdisplay_line_numbers, Qvisual)
21144 && (it->continuation_lines_width > 0
21145 || (this_line == last_line && !first_time))))
21146 tem_it.c = tem_it.char_to_display = ' ';
21147 else
21148 tem_it.c = tem_it.char_to_display = *p;
21149 tem_it.len = 1;
21150 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21151 /* Make sure these glyphs will have a "position" of -1. */
21152 SET_TEXT_POS (tem_it.position, -1, -1);
21153 PRODUCE_GLYPHS (&tem_it);
21155 /* Stop producing glyphs if we don't have enough space on
21156 this line. FIXME: should we refrain from producing the
21157 line number at all in that case? */
21158 if (tem_it.current_x > tem_it.last_visible_x)
21160 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21161 break;
21165 /* Record the width in pixels we need for the line number display. */
21166 it->lnum_pixel_width = tem_it.current_x;
21167 /* Copy the produced glyphs into IT's glyph_row. */
21168 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21169 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21170 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21171 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21173 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21175 for ( ; g < e; g++)
21177 it->current_x += g->pixel_width;
21178 /* The following is important when this function is called
21179 from move_it_in_display_line_to: HPOS is incremented only
21180 when we are in the visible portion of the glyph row. */
21181 if (it->current_x > it->first_visible_x)
21182 it->hpos++;
21183 if (p)
21185 *p++ = *g;
21186 (*u)++;
21190 /* Update IT's metrics due to glyphs produced for line numbers. */
21191 if (it->glyph_row)
21193 struct glyph_row *row = it->glyph_row;
21195 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21196 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21197 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21198 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21199 tem_it.max_phys_descent);
21201 else
21203 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21204 it->max_descent = max (it->max_descent, tem_it.max_descent);
21205 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21206 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21209 it->line_number_produced_p = true;
21211 bidi_unshelve_cache (itdata, false);
21214 /* Return true if this glyph row needs a line number to be produced
21215 for it. */
21216 static bool
21217 should_produce_line_number (struct it *it)
21219 if (NILP (Vdisplay_line_numbers))
21220 return false;
21222 /* Don't display line numbers in minibuffer windows. */
21223 if (MINI_WINDOW_P (it->w))
21224 return false;
21226 #ifdef HAVE_WINDOW_SYSTEM
21227 /* Don't display line number in tooltip frames. */
21228 if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
21229 return false;
21230 #endif
21232 /* If the character at current position has a non-nil special
21233 property, disable line numbers for this row. This is for
21234 packages such as company-mode, which need this for their tricky
21235 layout, where line numbers get in the way. */
21236 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21237 Qdisplay_line_numbers_disable,
21238 it->window);
21239 /* For ZV, we need to also look in empty overlays at that point,
21240 because get-char-property always returns nil for ZV, except if
21241 the property is in 'default-text-properties'. */
21242 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21243 val = disable_line_numbers_overlay_at_eob ();
21244 return NILP (val) ? true : false;
21247 /* Return true if ROW has no glyphs except those inserted by the
21248 display engine. This is needed for indicate-empty-lines and
21249 similar features when the glyph row starts with glyphs which didn't
21250 come from buffer or string. */
21251 static bool
21252 row_text_area_empty (struct glyph_row *row)
21254 if (!row->reversed_p)
21256 for (struct glyph *g = row->glyphs[TEXT_AREA];
21257 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21258 g++)
21259 if (!NILP (g->object) || g->charpos > 0)
21260 return false;
21262 else
21264 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21265 g > row->glyphs[TEXT_AREA];
21266 g--)
21267 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21268 return false;
21271 return true;
21274 /* Construct the glyph row IT->glyph_row in the desired matrix of
21275 IT->w from text at the current position of IT. See dispextern.h
21276 for an overview of struct it. Value is true if
21277 IT->glyph_row displays text, as opposed to a line displaying ZV
21278 only. CURSOR_VPOS is the window-relative vertical position of
21279 the glyph row displaying the cursor, or -1 if unknown. */
21281 static bool
21282 display_line (struct it *it, int cursor_vpos)
21284 struct glyph_row *row = it->glyph_row;
21285 Lisp_Object overlay_arrow_string;
21286 struct it wrap_it;
21287 void *wrap_data = NULL;
21288 bool may_wrap = false;
21289 int wrap_x UNINIT;
21290 int wrap_row_used = -1;
21291 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21292 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21293 int wrap_row_extra_line_spacing UNINIT;
21294 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21295 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21296 int cvpos;
21297 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21298 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21299 bool pending_handle_line_prefix = false;
21300 int header_line = window_wants_header_line (it->w);
21301 bool hscroll_this_line = (cursor_vpos >= 0
21302 && it->vpos == cursor_vpos - header_line
21303 && hscrolling_current_line_p (it->w));
21304 int first_visible_x = it->first_visible_x;
21305 int last_visible_x = it->last_visible_x;
21306 int x_incr = 0;
21308 /* We always start displaying at hpos zero even if hscrolled. */
21309 eassert (it->hpos == 0 && it->current_x == 0);
21311 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21312 >= it->w->desired_matrix->nrows)
21314 it->w->nrows_scale_factor++;
21315 it->f->fonts_changed = true;
21316 return false;
21319 /* Clear the result glyph row and enable it. */
21320 prepare_desired_row (it->w, row, false);
21322 row->y = it->current_y;
21323 row->start = it->start;
21324 row->continuation_lines_width = it->continuation_lines_width;
21325 row->displays_text_p = true;
21326 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21327 it->starts_in_middle_of_char_p = false;
21328 it->tab_offset = 0;
21329 it->line_number_produced_p = false;
21331 /* Arrange the overlays nicely for our purposes. Usually, we call
21332 display_line on only one line at a time, in which case this
21333 can't really hurt too much, or we call it on lines which appear
21334 one after another in the buffer, in which case all calls to
21335 recenter_overlay_lists but the first will be pretty cheap. */
21336 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21338 /* If we are going to display the cursor's line, account for the
21339 hscroll of that line. We subtract the window's min_hscroll,
21340 because that was already accounted for in init_iterator. */
21341 if (hscroll_this_line)
21342 x_incr =
21343 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21344 * FRAME_COLUMN_WIDTH (it->f);
21346 bool line_number_needed = should_produce_line_number (it);
21348 /* Move over display elements that are not visible because we are
21349 hscrolled. This may stop at an x-position < first_visible_x
21350 if the first glyph is partially visible or if we hit a line end. */
21351 if (it->current_x < it->first_visible_x + x_incr)
21353 enum move_it_result move_result;
21355 this_line_min_pos = row->start.pos;
21356 if (hscroll_this_line)
21358 it->first_visible_x += x_incr;
21359 it->last_visible_x += x_incr;
21361 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21362 MOVE_TO_POS | MOVE_TO_X);
21363 /* If we are under a large hscroll, move_it_in_display_line_to
21364 could hit the end of the line without reaching
21365 first_visible_x. Pretend that we did reach it. This is
21366 especially important on a TTY, where we will call
21367 extend_face_to_end_of_line, which needs to know how many
21368 blank glyphs to produce. */
21369 if (it->current_x < it->first_visible_x
21370 && (move_result == MOVE_NEWLINE_OR_CR
21371 || move_result == MOVE_POS_MATCH_OR_ZV))
21372 it->current_x = it->first_visible_x;
21374 /* In case move_it_in_display_line_to above "produced" the line
21375 number. */
21376 it->line_number_produced_p = false;
21378 /* Record the smallest positions seen while we moved over
21379 display elements that are not visible. This is needed by
21380 redisplay_internal for optimizing the case where the cursor
21381 stays inside the same line. The rest of this function only
21382 considers positions that are actually displayed, so
21383 RECORD_MAX_MIN_POS will not otherwise record positions that
21384 are hscrolled to the left of the left edge of the window. */
21385 min_pos = CHARPOS (this_line_min_pos);
21386 min_bpos = BYTEPOS (this_line_min_pos);
21388 /* Produce line number, if needed. */
21389 if (line_number_needed)
21390 maybe_produce_line_number (it);
21392 else if (it->area == TEXT_AREA)
21394 /* Line numbers should precede the line-prefix or wrap-prefix. */
21395 if (line_number_needed)
21396 maybe_produce_line_number (it);
21398 /* We only do this when not calling move_it_in_display_line_to
21399 above, because that function calls itself handle_line_prefix. */
21400 handle_line_prefix (it);
21402 else
21404 /* Line-prefix and wrap-prefix are always displayed in the text
21405 area. But if this is the first call to display_line after
21406 init_iterator, the iterator might have been set up to write
21407 into a marginal area, e.g. if the line begins with some
21408 display property that writes to the margins. So we need to
21409 wait with the call to handle_line_prefix until whatever
21410 writes to the margin has done its job. */
21411 pending_handle_line_prefix = true;
21414 /* Get the initial row height. This is either the height of the
21415 text hscrolled, if there is any, or zero. */
21416 row->ascent = it->max_ascent;
21417 row->height = it->max_ascent + it->max_descent;
21418 row->phys_ascent = it->max_phys_ascent;
21419 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21420 row->extra_line_spacing = it->max_extra_line_spacing;
21422 /* Utility macro to record max and min buffer positions seen until now. */
21423 #define RECORD_MAX_MIN_POS(IT) \
21424 do \
21426 bool composition_p \
21427 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21428 ptrdiff_t current_pos = \
21429 composition_p ? (IT)->cmp_it.charpos \
21430 : IT_CHARPOS (*(IT)); \
21431 ptrdiff_t current_bpos = \
21432 composition_p ? CHAR_TO_BYTE (current_pos) \
21433 : IT_BYTEPOS (*(IT)); \
21434 if (current_pos < min_pos) \
21436 min_pos = current_pos; \
21437 min_bpos = current_bpos; \
21439 if (IT_CHARPOS (*it) > max_pos) \
21441 max_pos = IT_CHARPOS (*it); \
21442 max_bpos = IT_BYTEPOS (*it); \
21445 while (false)
21447 /* Loop generating characters. The loop is left with IT on the next
21448 character to display. */
21449 while (true)
21451 int n_glyphs_before, hpos_before, x_before;
21452 int x, nglyphs;
21453 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21455 /* Retrieve the next thing to display. Value is false if end of
21456 buffer reached. */
21457 if (!get_next_display_element (it))
21459 bool row_has_glyphs = false;
21460 /* Maybe add a space at the end of this line that is used to
21461 display the cursor there under X. Set the charpos of the
21462 first glyph of blank lines not corresponding to any text
21463 to -1. */
21464 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21465 row->exact_window_width_line_p = true;
21466 else if ((append_space_for_newline (it, true)
21467 && row->used[TEXT_AREA] == 1)
21468 || row->used[TEXT_AREA] == 0
21469 || (row_has_glyphs = row_text_area_empty (row)))
21471 row->glyphs[TEXT_AREA]->charpos = -1;
21472 /* Don't reset the displays_text_p flag if we are
21473 displaying line numbers or line-prefix. */
21474 if (!row_has_glyphs)
21475 row->displays_text_p = false;
21477 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21478 && (!MINI_WINDOW_P (it->w)))
21479 row->indicate_empty_line_p = true;
21482 it->continuation_lines_width = 0;
21483 /* Reset those iterator values set from display property
21484 values. This is for the case when the display property
21485 ends at ZV, and is not a replacing property, so pop_it is
21486 not called. */
21487 it->font_height = Qnil;
21488 it->voffset = 0;
21489 row->ends_at_zv_p = true;
21490 /* A row that displays right-to-left text must always have
21491 its last face extended all the way to the end of line,
21492 even if this row ends in ZV, because we still write to
21493 the screen left to right. We also need to extend the
21494 last face if the default face is remapped to some
21495 different face, otherwise the functions that clear
21496 portions of the screen will clear with the default face's
21497 background color. */
21498 if (row->reversed_p
21499 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21500 extend_face_to_end_of_line (it);
21501 break;
21504 /* Now, get the metrics of what we want to display. This also
21505 generates glyphs in `row' (which is IT->glyph_row). */
21506 n_glyphs_before = row->used[TEXT_AREA];
21507 x = it->current_x;
21509 /* Remember the line height so far in case the next element doesn't
21510 fit on the line. */
21511 if (it->line_wrap != TRUNCATE)
21513 ascent = it->max_ascent;
21514 descent = it->max_descent;
21515 phys_ascent = it->max_phys_ascent;
21516 phys_descent = it->max_phys_descent;
21518 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21520 if (IT_DISPLAYING_WHITESPACE (it))
21521 may_wrap = true;
21522 else if (may_wrap)
21524 SAVE_IT (wrap_it, *it, wrap_data);
21525 wrap_x = x;
21526 wrap_row_used = row->used[TEXT_AREA];
21527 wrap_row_ascent = row->ascent;
21528 wrap_row_height = row->height;
21529 wrap_row_phys_ascent = row->phys_ascent;
21530 wrap_row_phys_height = row->phys_height;
21531 wrap_row_extra_line_spacing = row->extra_line_spacing;
21532 wrap_row_min_pos = min_pos;
21533 wrap_row_min_bpos = min_bpos;
21534 wrap_row_max_pos = max_pos;
21535 wrap_row_max_bpos = max_bpos;
21536 may_wrap = false;
21541 PRODUCE_GLYPHS (it);
21543 /* If this display element was in marginal areas, continue with
21544 the next one. */
21545 if (it->area != TEXT_AREA)
21547 row->ascent = max (row->ascent, it->max_ascent);
21548 row->height = max (row->height, it->max_ascent + it->max_descent);
21549 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21550 row->phys_height = max (row->phys_height,
21551 it->max_phys_ascent + it->max_phys_descent);
21552 row->extra_line_spacing = max (row->extra_line_spacing,
21553 it->max_extra_line_spacing);
21554 set_iterator_to_next (it, true);
21555 /* If we didn't handle the line/wrap prefix above, and the
21556 call to set_iterator_to_next just switched to TEXT_AREA,
21557 process the prefix now. */
21558 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21560 /* Line numbers should precede the line-prefix or wrap-prefix. */
21561 if (line_number_needed)
21562 maybe_produce_line_number (it);
21564 pending_handle_line_prefix = false;
21565 handle_line_prefix (it);
21567 continue;
21570 /* Does the display element fit on the line? If we truncate
21571 lines, we should draw past the right edge of the window. If
21572 we don't truncate, we want to stop so that we can display the
21573 continuation glyph before the right margin. If lines are
21574 continued, there are two possible strategies for characters
21575 resulting in more than 1 glyph (e.g. tabs): Display as many
21576 glyphs as possible in this line and leave the rest for the
21577 continuation line, or display the whole element in the next
21578 line. Original redisplay did the former, so we do it also. */
21579 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21580 hpos_before = it->hpos;
21581 x_before = x;
21583 if (/* Not a newline. */
21584 nglyphs > 0
21585 /* Glyphs produced fit entirely in the line. */
21586 && it->current_x < it->last_visible_x)
21588 it->hpos += nglyphs;
21589 row->ascent = max (row->ascent, it->max_ascent);
21590 row->height = max (row->height, it->max_ascent + it->max_descent);
21591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21592 row->phys_height = max (row->phys_height,
21593 it->max_phys_ascent + it->max_phys_descent);
21594 row->extra_line_spacing = max (row->extra_line_spacing,
21595 it->max_extra_line_spacing);
21596 if (it->current_x - it->pixel_width < it->first_visible_x
21597 /* When line numbers are displayed, row->x should not be
21598 offset, as the first glyph after the line number can
21599 never be partially visible. */
21600 && !line_number_needed
21601 /* In R2L rows, we arrange in extend_face_to_end_of_line
21602 to add a right offset to the line, by a suitable
21603 change to the stretch glyph that is the leftmost
21604 glyph of the line. */
21605 && !row->reversed_p)
21606 row->x = x - it->first_visible_x;
21607 /* Record the maximum and minimum buffer positions seen so
21608 far in glyphs that will be displayed by this row. */
21609 if (it->bidi_p)
21610 RECORD_MAX_MIN_POS (it);
21612 else
21614 int i, new_x;
21615 struct glyph *glyph;
21617 for (i = 0; i < nglyphs; ++i, x = new_x)
21619 /* Identify the glyphs added by the last call to
21620 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21621 the previous glyphs. */
21622 if (!row->reversed_p)
21623 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21624 else
21625 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21626 new_x = x + glyph->pixel_width;
21628 if (/* Lines are continued. */
21629 it->line_wrap != TRUNCATE
21630 && (/* Glyph doesn't fit on the line. */
21631 new_x > it->last_visible_x
21632 /* Or it fits exactly on a window system frame. */
21633 || (new_x == it->last_visible_x
21634 && FRAME_WINDOW_P (it->f)
21635 && (row->reversed_p
21636 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21637 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21639 /* End of a continued line. */
21641 if (it->hpos == 0
21642 || (new_x == it->last_visible_x
21643 && FRAME_WINDOW_P (it->f)
21644 && (row->reversed_p
21645 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21646 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21648 /* Current glyph is the only one on the line or
21649 fits exactly on the line. We must continue
21650 the line because we can't draw the cursor
21651 after the glyph. */
21652 row->continued_p = true;
21653 it->current_x = new_x;
21654 it->continuation_lines_width += new_x;
21655 ++it->hpos;
21656 if (i == nglyphs - 1)
21658 /* If line-wrap is on, check if a previous
21659 wrap point was found. */
21660 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21661 && wrap_row_used > 0
21662 /* Even if there is a previous wrap
21663 point, continue the line here as
21664 usual, if (i) the previous character
21665 was a space or tab AND (ii) the
21666 current character is not. */
21667 && (!may_wrap
21668 || IT_DISPLAYING_WHITESPACE (it)))
21669 goto back_to_wrap;
21671 /* Record the maximum and minimum buffer
21672 positions seen so far in glyphs that will be
21673 displayed by this row. */
21674 if (it->bidi_p)
21675 RECORD_MAX_MIN_POS (it);
21676 set_iterator_to_next (it, true);
21677 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21679 if (!get_next_display_element (it))
21681 row->exact_window_width_line_p = true;
21682 it->continuation_lines_width = 0;
21683 it->font_height = Qnil;
21684 it->voffset = 0;
21685 row->continued_p = false;
21686 row->ends_at_zv_p = true;
21688 else if (ITERATOR_AT_END_OF_LINE_P (it))
21690 row->continued_p = false;
21691 row->exact_window_width_line_p = true;
21693 /* If line-wrap is on, check if a
21694 previous wrap point was found. */
21695 else if (wrap_row_used > 0
21696 /* Even if there is a previous wrap
21697 point, continue the line here as
21698 usual, if (i) the previous character
21699 was a space or tab AND (ii) the
21700 current character is not. */
21701 && (!may_wrap
21702 || IT_DISPLAYING_WHITESPACE (it)))
21703 goto back_to_wrap;
21707 else if (it->bidi_p)
21708 RECORD_MAX_MIN_POS (it);
21709 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21710 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21711 extend_face_to_end_of_line (it);
21713 else if (CHAR_GLYPH_PADDING_P (*glyph)
21714 && !FRAME_WINDOW_P (it->f))
21716 /* A padding glyph that doesn't fit on this line.
21717 This means the whole character doesn't fit
21718 on the line. */
21719 if (row->reversed_p)
21720 unproduce_glyphs (it, row->used[TEXT_AREA]
21721 - n_glyphs_before);
21722 row->used[TEXT_AREA] = n_glyphs_before;
21724 /* Fill the rest of the row with continuation
21725 glyphs like in 20.x. */
21726 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21727 < row->glyphs[1 + TEXT_AREA])
21728 produce_special_glyphs (it, IT_CONTINUATION);
21730 row->continued_p = true;
21731 it->current_x = x_before;
21732 it->continuation_lines_width += x_before;
21734 /* Restore the height to what it was before the
21735 element not fitting on the line. */
21736 it->max_ascent = ascent;
21737 it->max_descent = descent;
21738 it->max_phys_ascent = phys_ascent;
21739 it->max_phys_descent = phys_descent;
21740 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21741 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21742 extend_face_to_end_of_line (it);
21744 else if (wrap_row_used > 0)
21746 back_to_wrap:
21747 if (row->reversed_p)
21748 unproduce_glyphs (it,
21749 row->used[TEXT_AREA] - wrap_row_used);
21750 RESTORE_IT (it, &wrap_it, wrap_data);
21751 it->continuation_lines_width += wrap_x;
21752 row->used[TEXT_AREA] = wrap_row_used;
21753 row->ascent = wrap_row_ascent;
21754 row->height = wrap_row_height;
21755 row->phys_ascent = wrap_row_phys_ascent;
21756 row->phys_height = wrap_row_phys_height;
21757 row->extra_line_spacing = wrap_row_extra_line_spacing;
21758 min_pos = wrap_row_min_pos;
21759 min_bpos = wrap_row_min_bpos;
21760 max_pos = wrap_row_max_pos;
21761 max_bpos = wrap_row_max_bpos;
21762 row->continued_p = true;
21763 row->ends_at_zv_p = false;
21764 row->exact_window_width_line_p = false;
21766 /* Make sure that a non-default face is extended
21767 up to the right margin of the window. */
21768 extend_face_to_end_of_line (it);
21770 else if ((it->what == IT_CHARACTER
21771 || it->what == IT_STRETCH
21772 || it->what == IT_COMPOSITION)
21773 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21775 /* A TAB that extends past the right edge of the
21776 window. This produces a single glyph on
21777 window system frames. We leave the glyph in
21778 this row and let it fill the row, but don't
21779 consume the TAB. */
21780 if ((row->reversed_p
21781 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21782 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21783 produce_special_glyphs (it, IT_CONTINUATION);
21784 it->continuation_lines_width += it->last_visible_x;
21785 row->ends_in_middle_of_char_p = true;
21786 row->continued_p = true;
21787 glyph->pixel_width = it->last_visible_x - x;
21788 it->starts_in_middle_of_char_p = true;
21789 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21790 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21791 extend_face_to_end_of_line (it);
21793 else
21795 /* Something other than a TAB that draws past
21796 the right edge of the window. Restore
21797 positions to values before the element. */
21798 if (row->reversed_p)
21799 unproduce_glyphs (it, row->used[TEXT_AREA]
21800 - (n_glyphs_before + i));
21801 row->used[TEXT_AREA] = n_glyphs_before + i;
21803 /* Display continuation glyphs. */
21804 it->current_x = x_before;
21805 it->continuation_lines_width += x;
21806 if (!FRAME_WINDOW_P (it->f)
21807 || (row->reversed_p
21808 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21809 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21810 produce_special_glyphs (it, IT_CONTINUATION);
21811 row->continued_p = true;
21813 extend_face_to_end_of_line (it);
21815 if (nglyphs > 1 && i > 0)
21817 row->ends_in_middle_of_char_p = true;
21818 it->starts_in_middle_of_char_p = true;
21821 /* Restore the height to what it was before the
21822 element not fitting on the line. */
21823 it->max_ascent = ascent;
21824 it->max_descent = descent;
21825 it->max_phys_ascent = phys_ascent;
21826 it->max_phys_descent = phys_descent;
21829 break;
21831 else if (new_x > it->first_visible_x)
21833 /* Increment number of glyphs actually displayed. */
21834 ++it->hpos;
21836 /* Record the maximum and minimum buffer positions
21837 seen so far in glyphs that will be displayed by
21838 this row. */
21839 if (it->bidi_p)
21840 RECORD_MAX_MIN_POS (it);
21842 if (x < it->first_visible_x && !row->reversed_p
21843 && !line_number_needed)
21844 /* Glyph is partially visible, i.e. row starts at
21845 negative X position. Don't do that in R2L
21846 rows, where we arrange to add a right offset to
21847 the line in extend_face_to_end_of_line, by a
21848 suitable change to the stretch glyph that is
21849 the leftmost glyph of the line. */
21850 row->x = x - it->first_visible_x;
21851 /* When the last glyph of an R2L row only fits
21852 partially on the line, we need to set row->x to a
21853 negative offset, so that the leftmost glyph is
21854 the one that is partially visible. But if we are
21855 going to produce the truncation glyph, this will
21856 be taken care of in produce_special_glyphs. */
21857 if (row->reversed_p
21858 && new_x > it->last_visible_x
21859 && !line_number_needed
21860 && !(it->line_wrap == TRUNCATE
21861 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21863 eassert (FRAME_WINDOW_P (it->f));
21864 row->x = it->last_visible_x - new_x;
21867 else
21869 /* Glyph is completely off the left margin of the
21870 window. This should not happen because of the
21871 move_it_in_display_line at the start of this
21872 function, unless the text display area of the
21873 window is empty. */
21874 eassert (it->first_visible_x <= it->last_visible_x);
21877 /* Even if this display element produced no glyphs at all,
21878 we want to record its position. */
21879 if (it->bidi_p && nglyphs == 0)
21880 RECORD_MAX_MIN_POS (it);
21882 row->ascent = max (row->ascent, it->max_ascent);
21883 row->height = max (row->height, it->max_ascent + it->max_descent);
21884 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21885 row->phys_height = max (row->phys_height,
21886 it->max_phys_ascent + it->max_phys_descent);
21887 row->extra_line_spacing = max (row->extra_line_spacing,
21888 it->max_extra_line_spacing);
21890 /* End of this display line if row is continued. */
21891 if (row->continued_p || row->ends_at_zv_p)
21892 break;
21895 at_end_of_line:
21896 /* Is this a line end? If yes, we're also done, after making
21897 sure that a non-default face is extended up to the right
21898 margin of the window. */
21899 if (ITERATOR_AT_END_OF_LINE_P (it))
21901 int used_before = row->used[TEXT_AREA];
21903 row->ends_in_newline_from_string_p = STRINGP (it->object);
21905 /* Add a space at the end of the line that is used to
21906 display the cursor there. */
21907 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21908 append_space_for_newline (it, false);
21910 /* Extend the face to the end of the line. */
21911 extend_face_to_end_of_line (it);
21913 /* Make sure we have the position. */
21914 if (used_before == 0)
21915 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21917 /* Record the position of the newline, for use in
21918 find_row_edges. */
21919 it->eol_pos = it->current.pos;
21921 /* Consume the line end. This skips over invisible lines. */
21922 set_iterator_to_next (it, true);
21923 it->continuation_lines_width = 0;
21924 break;
21927 /* Proceed with next display element. Note that this skips
21928 over lines invisible because of selective display. */
21929 set_iterator_to_next (it, true);
21931 /* If we truncate lines, we are done when the last displayed
21932 glyphs reach past the right margin of the window. */
21933 if (it->line_wrap == TRUNCATE
21934 && ((FRAME_WINDOW_P (it->f)
21935 /* Images are preprocessed in produce_image_glyph such
21936 that they are cropped at the right edge of the
21937 window, so an image glyph will always end exactly at
21938 last_visible_x, even if there's no right fringe. */
21939 && ((row->reversed_p
21940 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21941 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21942 || it->what == IT_IMAGE))
21943 ? (it->current_x >= it->last_visible_x)
21944 : (it->current_x > it->last_visible_x)))
21946 /* Maybe add truncation glyphs. */
21947 if (!FRAME_WINDOW_P (it->f)
21948 || (row->reversed_p
21949 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21950 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21952 int i, n;
21954 if (!row->reversed_p)
21956 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21957 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21958 break;
21960 else
21962 for (i = 0; i < row->used[TEXT_AREA]; i++)
21963 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21964 break;
21965 /* Remove any padding glyphs at the front of ROW, to
21966 make room for the truncation glyphs we will be
21967 adding below. The loop below always inserts at
21968 least one truncation glyph, so also remove the
21969 last glyph added to ROW. */
21970 unproduce_glyphs (it, i + 1);
21971 /* Adjust i for the loop below. */
21972 i = row->used[TEXT_AREA] - (i + 1);
21975 /* produce_special_glyphs overwrites the last glyph, so
21976 we don't want that if we want to keep that last
21977 glyph, which means it's an image. */
21978 if (it->current_x > it->last_visible_x)
21980 it->current_x = x_before;
21981 if (!FRAME_WINDOW_P (it->f))
21983 for (n = row->used[TEXT_AREA]; i < n; ++i)
21985 row->used[TEXT_AREA] = i;
21986 produce_special_glyphs (it, IT_TRUNCATION);
21989 else
21991 row->used[TEXT_AREA] = i;
21992 produce_special_glyphs (it, IT_TRUNCATION);
21994 it->hpos = hpos_before;
21997 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21999 /* Don't truncate if we can overflow newline into fringe. */
22000 if (!get_next_display_element (it))
22002 it->continuation_lines_width = 0;
22003 it->font_height = Qnil;
22004 it->voffset = 0;
22005 row->ends_at_zv_p = true;
22006 row->exact_window_width_line_p = true;
22007 break;
22009 if (ITERATOR_AT_END_OF_LINE_P (it))
22011 row->exact_window_width_line_p = true;
22012 goto at_end_of_line;
22014 it->current_x = x_before;
22015 it->hpos = hpos_before;
22018 row->truncated_on_right_p = true;
22019 it->continuation_lines_width = 0;
22020 reseat_at_next_visible_line_start (it, false);
22021 /* We insist below that IT's position be at ZV because in
22022 bidi-reordered lines the character at visible line start
22023 might not be the character that follows the newline in
22024 the logical order. */
22025 if (IT_BYTEPOS (*it) > BEG_BYTE)
22026 row->ends_at_zv_p =
22027 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
22028 else
22029 row->ends_at_zv_p = false;
22030 break;
22034 if (wrap_data)
22035 bidi_unshelve_cache (wrap_data, true);
22037 /* If line is not empty and hscrolled, maybe insert truncation glyphs
22038 at the left window margin. */
22039 if (it->first_visible_x
22040 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
22042 if (!FRAME_WINDOW_P (it->f)
22043 || (((row->reversed_p
22044 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22045 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22046 /* Don't let insert_left_trunc_glyphs overwrite the
22047 first glyph of the row if it is an image. */
22048 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
22049 insert_left_trunc_glyphs (it);
22050 row->truncated_on_left_p = true;
22053 /* Remember the position at which this line ends.
22055 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
22056 cannot be before the call to find_row_edges below, since that is
22057 where these positions are determined. */
22058 row->end = it->current;
22059 if (!it->bidi_p)
22061 row->minpos = row->start.pos;
22062 row->maxpos = row->end.pos;
22064 else
22066 /* ROW->minpos and ROW->maxpos must be the smallest and
22067 `1 + the largest' buffer positions in ROW. But if ROW was
22068 bidi-reordered, these two positions can be anywhere in the
22069 row, so we must determine them now. */
22070 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
22073 /* If the start of this line is the overlay arrow-position, then
22074 mark this glyph row as the one containing the overlay arrow.
22075 This is clearly a mess with variable size fonts. It would be
22076 better to let it be displayed like cursors under X. */
22077 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22078 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22079 !NILP (overlay_arrow_string)))
22081 /* Overlay arrow in window redisplay is a fringe bitmap. */
22082 if (STRINGP (overlay_arrow_string))
22084 struct glyph_row *arrow_row
22085 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22086 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22087 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22088 struct glyph *p = row->glyphs[TEXT_AREA];
22089 struct glyph *p2, *end;
22091 /* Copy the arrow glyphs. */
22092 while (glyph < arrow_end)
22093 *p++ = *glyph++;
22095 /* Throw away padding glyphs. */
22096 p2 = p;
22097 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22098 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22099 ++p2;
22100 if (p2 > p)
22102 while (p2 < end)
22103 *p++ = *p2++;
22104 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22107 else
22109 eassert (INTEGERP (overlay_arrow_string));
22110 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22112 overlay_arrow_seen = true;
22115 /* Highlight trailing whitespace. */
22116 if (!NILP (Vshow_trailing_whitespace))
22117 highlight_trailing_whitespace (it->f, it->glyph_row);
22119 /* Compute pixel dimensions of this line. */
22120 compute_line_metrics (it);
22122 /* Implementation note: No changes in the glyphs of ROW or in their
22123 faces can be done past this point, because compute_line_metrics
22124 computes ROW's hash value and stores it within the glyph_row
22125 structure. */
22127 /* Record whether this row ends inside an ellipsis. */
22128 row->ends_in_ellipsis_p
22129 = (it->method == GET_FROM_DISPLAY_VECTOR
22130 && it->ellipsis_p);
22132 /* Save fringe bitmaps in this row. */
22133 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22134 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22135 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22136 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22138 it->left_user_fringe_bitmap = 0;
22139 it->left_user_fringe_face_id = 0;
22140 it->right_user_fringe_bitmap = 0;
22141 it->right_user_fringe_face_id = 0;
22143 /* Maybe set the cursor. */
22144 cvpos = it->w->cursor.vpos;
22145 if ((cvpos < 0
22146 /* In bidi-reordered rows, keep checking for proper cursor
22147 position even if one has been found already, because buffer
22148 positions in such rows change non-linearly with ROW->VPOS,
22149 when a line is continued. One exception: when we are at ZV,
22150 display cursor on the first suitable glyph row, since all
22151 the empty rows after that also have their position set to ZV. */
22152 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22153 lines' rows is implemented for bidi-reordered rows. */
22154 || (it->bidi_p
22155 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22156 && PT >= MATRIX_ROW_START_CHARPOS (row)
22157 && PT <= MATRIX_ROW_END_CHARPOS (row)
22158 && cursor_row_p (row))
22159 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22161 /* Prepare for the next line. This line starts horizontally at (X
22162 HPOS) = (0 0). Vertical positions are incremented. As a
22163 convenience for the caller, IT->glyph_row is set to the next
22164 row to be used. */
22165 it->current_x = it->hpos = 0;
22166 it->current_y += row->height;
22167 /* Restore the first and last visible X if we adjusted them for
22168 current-line hscrolling. */
22169 if (hscroll_this_line)
22171 it->first_visible_x = first_visible_x;
22172 it->last_visible_x = last_visible_x;
22174 SET_TEXT_POS (it->eol_pos, 0, 0);
22175 ++it->vpos;
22176 ++it->glyph_row;
22177 /* The next row should by default use the same value of the
22178 reversed_p flag as this one. set_iterator_to_next decides when
22179 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22180 the flag accordingly. */
22181 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22182 it->glyph_row->reversed_p = row->reversed_p;
22183 it->start = row->end;
22184 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22186 #undef RECORD_MAX_MIN_POS
22189 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22190 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22191 doc: /* Return paragraph direction at point in BUFFER.
22192 Value is either `left-to-right' or `right-to-left'.
22193 If BUFFER is omitted or nil, it defaults to the current buffer.
22195 Paragraph direction determines how the text in the paragraph is displayed.
22196 In left-to-right paragraphs, text begins at the left margin of the window
22197 and the reading direction is generally left to right. In right-to-left
22198 paragraphs, text begins at the right margin and is read from right to left.
22200 See also `bidi-paragraph-direction'. */)
22201 (Lisp_Object buffer)
22203 struct buffer *buf = current_buffer;
22204 struct buffer *old = buf;
22206 if (! NILP (buffer))
22208 CHECK_BUFFER (buffer);
22209 buf = XBUFFER (buffer);
22212 if (NILP (BVAR (buf, bidi_display_reordering))
22213 || NILP (BVAR (buf, enable_multibyte_characters))
22214 /* When we are loading loadup.el, the character property tables
22215 needed for bidi iteration are not yet available. */
22216 || redisplay__inhibit_bidi)
22217 return Qleft_to_right;
22218 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22219 return BVAR (buf, bidi_paragraph_direction);
22220 else
22222 /* Determine the direction from buffer text. We could try to
22223 use current_matrix if it is up to date, but this seems fast
22224 enough as it is. */
22225 struct bidi_it itb;
22226 ptrdiff_t pos = BUF_PT (buf);
22227 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22228 int c;
22229 void *itb_data = bidi_shelve_cache ();
22231 set_buffer_temp (buf);
22232 /* bidi_paragraph_init finds the base direction of the paragraph
22233 by searching forward from paragraph start. We need the base
22234 direction of the current or _previous_ paragraph, so we need
22235 to make sure we are within that paragraph. To that end, find
22236 the previous non-empty line. */
22237 if (pos >= ZV && pos > BEGV)
22238 DEC_BOTH (pos, bytepos);
22239 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22240 if (fast_looking_at (trailing_white_space,
22241 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22243 while ((c = FETCH_BYTE (bytepos)) == '\n'
22244 || c == ' ' || c == '\t' || c == '\f')
22246 if (bytepos <= BEGV_BYTE)
22247 break;
22248 bytepos--;
22249 pos--;
22251 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22252 bytepos--;
22254 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22255 itb.paragraph_dir = NEUTRAL_DIR;
22256 itb.string.s = NULL;
22257 itb.string.lstring = Qnil;
22258 itb.string.bufpos = 0;
22259 itb.string.from_disp_str = false;
22260 itb.string.unibyte = false;
22261 /* We have no window to use here for ignoring window-specific
22262 overlays. Using NULL for window pointer will cause
22263 compute_display_string_pos to use the current buffer. */
22264 itb.w = NULL;
22265 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22266 bidi_unshelve_cache (itb_data, false);
22267 set_buffer_temp (old);
22268 switch (itb.paragraph_dir)
22270 case L2R:
22271 return Qleft_to_right;
22272 break;
22273 case R2L:
22274 return Qright_to_left;
22275 break;
22276 default:
22277 emacs_abort ();
22282 DEFUN ("bidi-find-overridden-directionality",
22283 Fbidi_find_overridden_directionality,
22284 Sbidi_find_overridden_directionality, 2, 3, 0,
22285 doc: /* Return position between FROM and TO where directionality was overridden.
22287 This function returns the first character position in the specified
22288 region of OBJECT where there is a character whose `bidi-class' property
22289 is `L', but which was forced to display as `R' by a directional
22290 override, and likewise with characters whose `bidi-class' is `R'
22291 or `AL' that were forced to display as `L'.
22293 If no such character is found, the function returns nil.
22295 OBJECT is a Lisp string or buffer to search for overridden
22296 directionality, and defaults to the current buffer if nil or omitted.
22297 OBJECT can also be a window, in which case the function will search
22298 the buffer displayed in that window. Passing the window instead of
22299 a buffer is preferable when the buffer is displayed in some window,
22300 because this function will then be able to correctly account for
22301 window-specific overlays, which can affect the results.
22303 Strong directional characters `L', `R', and `AL' can have their
22304 intrinsic directionality overridden by directional override
22305 control characters RLO (u+202e) and LRO (u+202d). See the
22306 function `get-char-code-property' for a way to inquire about
22307 the `bidi-class' property of a character. */)
22308 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22310 struct buffer *buf = current_buffer;
22311 struct buffer *old = buf;
22312 struct window *w = NULL;
22313 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22314 struct bidi_it itb;
22315 ptrdiff_t from_pos, to_pos, from_bpos;
22316 void *itb_data;
22318 if (!NILP (object))
22320 if (BUFFERP (object))
22321 buf = XBUFFER (object);
22322 else if (WINDOWP (object))
22324 w = decode_live_window (object);
22325 buf = XBUFFER (w->contents);
22326 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22328 else
22329 CHECK_STRING (object);
22332 if (STRINGP (object))
22334 /* Characters in unibyte strings are always treated by bidi.c as
22335 strong LTR. */
22336 if (!STRING_MULTIBYTE (object)
22337 /* When we are loading loadup.el, the character property
22338 tables needed for bidi iteration are not yet
22339 available. */
22340 || redisplay__inhibit_bidi)
22341 return Qnil;
22343 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22344 if (from_pos >= SCHARS (object))
22345 return Qnil;
22347 /* Set up the bidi iterator. */
22348 itb_data = bidi_shelve_cache ();
22349 itb.paragraph_dir = NEUTRAL_DIR;
22350 itb.string.lstring = object;
22351 itb.string.s = NULL;
22352 itb.string.schars = SCHARS (object);
22353 itb.string.bufpos = 0;
22354 itb.string.from_disp_str = false;
22355 itb.string.unibyte = false;
22356 itb.w = w;
22357 bidi_init_it (0, 0, frame_window_p, &itb);
22359 else
22361 /* Nothing this fancy can happen in unibyte buffers, or in a
22362 buffer that disabled reordering, or if FROM is at EOB. */
22363 if (NILP (BVAR (buf, bidi_display_reordering))
22364 || NILP (BVAR (buf, enable_multibyte_characters))
22365 /* When we are loading loadup.el, the character property
22366 tables needed for bidi iteration are not yet
22367 available. */
22368 || redisplay__inhibit_bidi)
22369 return Qnil;
22371 set_buffer_temp (buf);
22372 validate_region (&from, &to);
22373 from_pos = XINT (from);
22374 to_pos = XINT (to);
22375 if (from_pos >= ZV)
22376 return Qnil;
22378 /* Set up the bidi iterator. */
22379 itb_data = bidi_shelve_cache ();
22380 from_bpos = CHAR_TO_BYTE (from_pos);
22381 if (from_pos == BEGV)
22383 itb.charpos = BEGV;
22384 itb.bytepos = BEGV_BYTE;
22386 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22388 itb.charpos = from_pos;
22389 itb.bytepos = from_bpos;
22391 else
22392 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22393 -1, &itb.bytepos);
22394 itb.paragraph_dir = NEUTRAL_DIR;
22395 itb.string.s = NULL;
22396 itb.string.lstring = Qnil;
22397 itb.string.bufpos = 0;
22398 itb.string.from_disp_str = false;
22399 itb.string.unibyte = false;
22400 itb.w = w;
22401 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22404 ptrdiff_t found;
22405 do {
22406 /* For the purposes of this function, the actual base direction of
22407 the paragraph doesn't matter, so just set it to L2R. */
22408 bidi_paragraph_init (L2R, &itb, false);
22409 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22411 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22413 bidi_unshelve_cache (itb_data, false);
22414 set_buffer_temp (old);
22416 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22419 DEFUN ("move-point-visually", Fmove_point_visually,
22420 Smove_point_visually, 1, 1, 0,
22421 doc: /* Move point in the visual order in the specified DIRECTION.
22422 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22423 left.
22425 Value is the new character position of point. */)
22426 (Lisp_Object direction)
22428 struct window *w = XWINDOW (selected_window);
22429 struct buffer *b = XBUFFER (w->contents);
22430 struct glyph_row *row;
22431 int dir;
22432 Lisp_Object paragraph_dir;
22434 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22435 (!(ROW)->continued_p \
22436 && NILP ((GLYPH)->object) \
22437 && (GLYPH)->type == CHAR_GLYPH \
22438 && (GLYPH)->u.ch == ' ' \
22439 && (GLYPH)->charpos >= 0 \
22440 && !(GLYPH)->avoid_cursor_p)
22442 CHECK_NUMBER (direction);
22443 dir = XINT (direction);
22444 if (dir > 0)
22445 dir = 1;
22446 else
22447 dir = -1;
22449 /* If current matrix is up-to-date, we can use the information
22450 recorded in the glyphs, at least as long as the goal is on the
22451 screen. */
22452 if (w->window_end_valid
22453 && !windows_or_buffers_changed
22454 && b
22455 && !b->clip_changed
22456 && !b->prevent_redisplay_optimizations_p
22457 && !window_outdated (w)
22458 /* We rely below on the cursor coordinates to be up to date, but
22459 we cannot trust them if some command moved point since the
22460 last complete redisplay. */
22461 && w->last_point == BUF_PT (b)
22462 && w->cursor.vpos >= 0
22463 && w->cursor.vpos < w->current_matrix->nrows
22464 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22466 struct glyph *g = row->glyphs[TEXT_AREA];
22467 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22468 struct glyph *gpt = g + w->cursor.hpos;
22470 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22472 if (BUFFERP (g->object) && g->charpos != PT)
22474 SET_PT (g->charpos);
22475 w->cursor.vpos = -1;
22476 return make_number (PT);
22478 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22480 ptrdiff_t new_pos;
22482 if (BUFFERP (gpt->object))
22484 new_pos = PT;
22485 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22486 new_pos += (row->reversed_p ? -dir : dir);
22487 else
22488 new_pos -= (row->reversed_p ? -dir : dir);
22490 else if (BUFFERP (g->object))
22491 new_pos = g->charpos;
22492 else
22493 break;
22494 SET_PT (new_pos);
22495 w->cursor.vpos = -1;
22496 return make_number (PT);
22498 else if (ROW_GLYPH_NEWLINE_P (row, g))
22500 /* Glyphs inserted at the end of a non-empty line for
22501 positioning the cursor have zero charpos, so we must
22502 deduce the value of point by other means. */
22503 if (g->charpos > 0)
22504 SET_PT (g->charpos);
22505 else if (row->ends_at_zv_p && PT != ZV)
22506 SET_PT (ZV);
22507 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22508 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22509 else
22510 break;
22511 w->cursor.vpos = -1;
22512 return make_number (PT);
22515 if (g == e || NILP (g->object))
22517 if (row->truncated_on_left_p || row->truncated_on_right_p)
22518 goto simulate_display;
22519 if (!row->reversed_p)
22520 row += dir;
22521 else
22522 row -= dir;
22523 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22524 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22525 goto simulate_display;
22527 if (dir > 0)
22529 if (row->reversed_p && !row->continued_p)
22531 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22532 w->cursor.vpos = -1;
22533 return make_number (PT);
22535 g = row->glyphs[TEXT_AREA];
22536 e = g + row->used[TEXT_AREA];
22537 for ( ; g < e; g++)
22539 if (BUFFERP (g->object)
22540 /* Empty lines have only one glyph, which stands
22541 for the newline, and whose charpos is the
22542 buffer position of the newline. */
22543 || ROW_GLYPH_NEWLINE_P (row, g)
22544 /* When the buffer ends in a newline, the line at
22545 EOB also has one glyph, but its charpos is -1. */
22546 || (row->ends_at_zv_p
22547 && !row->reversed_p
22548 && NILP (g->object)
22549 && g->type == CHAR_GLYPH
22550 && g->u.ch == ' '))
22552 if (g->charpos > 0)
22553 SET_PT (g->charpos);
22554 else if (!row->reversed_p
22555 && row->ends_at_zv_p
22556 && PT != ZV)
22557 SET_PT (ZV);
22558 else
22559 continue;
22560 w->cursor.vpos = -1;
22561 return make_number (PT);
22565 else
22567 if (!row->reversed_p && !row->continued_p)
22569 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22570 w->cursor.vpos = -1;
22571 return make_number (PT);
22573 e = row->glyphs[TEXT_AREA];
22574 g = e + row->used[TEXT_AREA] - 1;
22575 for ( ; g >= e; g--)
22577 if (BUFFERP (g->object)
22578 || (ROW_GLYPH_NEWLINE_P (row, g)
22579 && g->charpos > 0)
22580 /* Empty R2L lines on GUI frames have the buffer
22581 position of the newline stored in the stretch
22582 glyph. */
22583 || g->type == STRETCH_GLYPH
22584 || (row->ends_at_zv_p
22585 && row->reversed_p
22586 && NILP (g->object)
22587 && g->type == CHAR_GLYPH
22588 && g->u.ch == ' '))
22590 if (g->charpos > 0)
22591 SET_PT (g->charpos);
22592 else if (row->reversed_p
22593 && row->ends_at_zv_p
22594 && PT != ZV)
22595 SET_PT (ZV);
22596 else
22597 continue;
22598 w->cursor.vpos = -1;
22599 return make_number (PT);
22606 simulate_display:
22608 /* If we wind up here, we failed to move by using the glyphs, so we
22609 need to simulate display instead. */
22611 if (b)
22612 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22613 else
22614 paragraph_dir = Qleft_to_right;
22615 if (EQ (paragraph_dir, Qright_to_left))
22616 dir = -dir;
22617 if (PT <= BEGV && dir < 0)
22618 xsignal0 (Qbeginning_of_buffer);
22619 else if (PT >= ZV && dir > 0)
22620 xsignal0 (Qend_of_buffer);
22621 else
22623 struct text_pos pt;
22624 struct it it;
22625 int pt_x, target_x, pixel_width, pt_vpos;
22626 bool at_eol_p;
22627 bool overshoot_expected = false;
22628 bool target_is_eol_p = false;
22630 /* Setup the arena. */
22631 SET_TEXT_POS (pt, PT, PT_BYTE);
22632 start_display (&it, w, pt);
22633 /* When lines are truncated, we could be called with point
22634 outside of the windows edges, in which case move_it_*
22635 functions either prematurely stop at window's edge or jump to
22636 the next screen line, whereas we rely below on our ability to
22637 reach point, in order to start from its X coordinate. So we
22638 need to disregard the window's horizontal extent in that case. */
22639 if (it.line_wrap == TRUNCATE)
22640 it.last_visible_x = DISP_INFINITY;
22642 if (it.cmp_it.id < 0
22643 && it.method == GET_FROM_STRING
22644 && it.area == TEXT_AREA
22645 && it.string_from_display_prop_p
22646 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22647 overshoot_expected = true;
22649 /* Find the X coordinate of point. We start from the beginning
22650 of this or previous line to make sure we are before point in
22651 the logical order (since the move_it_* functions can only
22652 move forward). */
22653 reseat:
22654 reseat_at_previous_visible_line_start (&it);
22655 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22656 if (IT_CHARPOS (it) != PT)
22658 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22659 -1, -1, -1, MOVE_TO_POS);
22660 /* If we missed point because the character there is
22661 displayed out of a display vector that has more than one
22662 glyph, retry expecting overshoot. */
22663 if (it.method == GET_FROM_DISPLAY_VECTOR
22664 && it.current.dpvec_index > 0
22665 && !overshoot_expected)
22667 overshoot_expected = true;
22668 goto reseat;
22670 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22671 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22673 pt_x = it.current_x;
22674 pt_vpos = it.vpos;
22675 if (dir > 0 || overshoot_expected)
22677 struct glyph_row *row = it.glyph_row;
22679 /* When point is at beginning of line, we don't have
22680 information about the glyph there loaded into struct
22681 it. Calling get_next_display_element fixes that. */
22682 if (pt_x == 0)
22683 get_next_display_element (&it);
22684 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22685 it.glyph_row = NULL;
22686 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22687 it.glyph_row = row;
22688 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22689 it, lest it will become out of sync with it's buffer
22690 position. */
22691 it.current_x = pt_x;
22693 else
22694 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22695 pixel_width = it.pixel_width;
22696 if (overshoot_expected && at_eol_p)
22697 pixel_width = 0;
22698 else if (pixel_width <= 0)
22699 pixel_width = 1;
22701 /* If there's a display string (or something similar) at point,
22702 we are actually at the glyph to the left of point, so we need
22703 to correct the X coordinate. */
22704 if (overshoot_expected)
22706 if (it.bidi_p)
22707 pt_x += pixel_width * it.bidi_it.scan_dir;
22708 else
22709 pt_x += pixel_width;
22712 /* Compute target X coordinate, either to the left or to the
22713 right of point. On TTY frames, all characters have the same
22714 pixel width of 1, so we can use that. On GUI frames we don't
22715 have an easy way of getting at the pixel width of the
22716 character to the left of point, so we use a different method
22717 of getting to that place. */
22718 if (dir > 0)
22719 target_x = pt_x + pixel_width;
22720 else
22721 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22723 /* Target X coordinate could be one line above or below the line
22724 of point, in which case we need to adjust the target X
22725 coordinate. Also, if moving to the left, we need to begin at
22726 the left edge of the point's screen line. */
22727 if (dir < 0)
22729 if (pt_x > 0)
22731 start_display (&it, w, pt);
22732 if (it.line_wrap == TRUNCATE)
22733 it.last_visible_x = DISP_INFINITY;
22734 reseat_at_previous_visible_line_start (&it);
22735 it.current_x = it.current_y = it.hpos = 0;
22736 if (pt_vpos != 0)
22737 move_it_by_lines (&it, pt_vpos);
22739 else
22741 move_it_by_lines (&it, -1);
22742 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22743 target_is_eol_p = true;
22744 /* Under word-wrap, we don't know the x coordinate of
22745 the last character displayed on the previous line,
22746 which immediately precedes the wrap point. To find
22747 out its x coordinate, we try moving to the right
22748 margin of the window, which will stop at the wrap
22749 point, and then reset target_x to point at the
22750 character that precedes the wrap point. This is not
22751 needed on GUI frames, because (see below) there we
22752 move from the left margin one grapheme cluster at a
22753 time, and stop when we hit the wrap point. */
22754 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22756 void *it_data = NULL;
22757 struct it it2;
22759 SAVE_IT (it2, it, it_data);
22760 move_it_in_display_line_to (&it, ZV, target_x,
22761 MOVE_TO_POS | MOVE_TO_X);
22762 /* If we arrived at target_x, that _is_ the last
22763 character on the previous line. */
22764 if (it.current_x != target_x)
22765 target_x = it.current_x - 1;
22766 RESTORE_IT (&it, &it2, it_data);
22770 else
22772 if (at_eol_p
22773 || (target_x >= it.last_visible_x
22774 && it.line_wrap != TRUNCATE))
22776 if (pt_x > 0)
22777 move_it_by_lines (&it, 0);
22778 move_it_by_lines (&it, 1);
22779 target_x = 0;
22783 /* Move to the target X coordinate. */
22784 /* On GUI frames, as we don't know the X coordinate of the
22785 character to the left of point, moving point to the left
22786 requires walking, one grapheme cluster at a time, until we
22787 find ourself at a place immediately to the left of the
22788 character at point. */
22789 if (FRAME_WINDOW_P (it.f) && dir < 0)
22791 struct text_pos new_pos;
22792 enum move_it_result rc = MOVE_X_REACHED;
22794 if (it.current_x == 0)
22795 get_next_display_element (&it);
22796 if (it.what == IT_COMPOSITION)
22798 new_pos.charpos = it.cmp_it.charpos;
22799 new_pos.bytepos = -1;
22801 else
22802 new_pos = it.current.pos;
22804 while (it.current_x + it.pixel_width <= target_x
22805 && (rc == MOVE_X_REACHED
22806 /* Under word-wrap, move_it_in_display_line_to
22807 stops at correct coordinates, but sometimes
22808 returns MOVE_POS_MATCH_OR_ZV. */
22809 || (it.line_wrap == WORD_WRAP
22810 && rc == MOVE_POS_MATCH_OR_ZV)))
22812 int new_x = it.current_x + it.pixel_width;
22814 /* For composed characters, we want the position of the
22815 first character in the grapheme cluster (usually, the
22816 composition's base character), whereas it.current
22817 might give us the position of the _last_ one, e.g. if
22818 the composition is rendered in reverse due to bidi
22819 reordering. */
22820 if (it.what == IT_COMPOSITION)
22822 new_pos.charpos = it.cmp_it.charpos;
22823 new_pos.bytepos = -1;
22825 else
22826 new_pos = it.current.pos;
22827 if (new_x == it.current_x)
22828 new_x++;
22829 rc = move_it_in_display_line_to (&it, ZV, new_x,
22830 MOVE_TO_POS | MOVE_TO_X);
22831 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22832 break;
22834 /* The previous position we saw in the loop is the one we
22835 want. */
22836 if (new_pos.bytepos == -1)
22837 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22838 it.current.pos = new_pos;
22840 else if (it.current_x != target_x)
22841 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22843 /* If we ended up in a display string that covers point, move to
22844 buffer position to the right in the visual order. */
22845 if (dir > 0)
22847 while (IT_CHARPOS (it) == PT)
22849 set_iterator_to_next (&it, false);
22850 if (!get_next_display_element (&it))
22851 break;
22855 /* Move point to that position. */
22856 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22859 return make_number (PT);
22861 #undef ROW_GLYPH_NEWLINE_P
22864 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22865 Sbidi_resolved_levels, 0, 1, 0,
22866 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22868 The resolved levels are produced by the Emacs bidi reordering engine
22869 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22870 read the Unicode Standard Annex 9 (UAX#9) for background information
22871 about these levels.
22873 VPOS is the zero-based number of the current window's screen line
22874 for which to produce the resolved levels. If VPOS is nil or omitted,
22875 it defaults to the screen line of point. If the window displays a
22876 header line, VPOS of zero will report on the header line, and first
22877 line of text in the window will have VPOS of 1.
22879 Value is an array of resolved levels, indexed by glyph number.
22880 Glyphs are numbered from zero starting from the beginning of the
22881 screen line, i.e. the left edge of the window for left-to-right lines
22882 and from the right edge for right-to-left lines. The resolved levels
22883 are produced only for the window's text area; text in display margins
22884 is not included.
22886 If the selected window's display is not up-to-date, or if the specified
22887 screen line does not display text, this function returns nil. It is
22888 highly recommended to bind this function to some simple key, like F8,
22889 in order to avoid these problems.
22891 This function exists mainly for testing the correctness of the
22892 Emacs UBA implementation, in particular with the test suite. */)
22893 (Lisp_Object vpos)
22895 struct window *w = XWINDOW (selected_window);
22896 struct buffer *b = XBUFFER (w->contents);
22897 int nrow;
22898 struct glyph_row *row;
22900 if (NILP (vpos))
22902 int d1, d2, d3, d4, d5;
22904 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22906 else
22908 CHECK_NUMBER_COERCE_MARKER (vpos);
22909 nrow = XINT (vpos);
22912 /* We require up-to-date glyph matrix for this window. */
22913 if (w->window_end_valid
22914 && !windows_or_buffers_changed
22915 && b
22916 && !b->clip_changed
22917 && !b->prevent_redisplay_optimizations_p
22918 && !window_outdated (w)
22919 && nrow >= 0
22920 && nrow < w->current_matrix->nrows
22921 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22922 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22924 struct glyph *g, *e, *g1;
22925 int nglyphs, i;
22926 Lisp_Object levels;
22928 if (!row->reversed_p) /* Left-to-right glyph row. */
22930 g = g1 = row->glyphs[TEXT_AREA];
22931 e = g + row->used[TEXT_AREA];
22933 /* Skip over glyphs at the start of the row that was
22934 generated by redisplay for its own needs. */
22935 while (g < e
22936 && NILP (g->object)
22937 && g->charpos < 0)
22938 g++;
22939 g1 = g;
22941 /* Count the "interesting" glyphs in this row. */
22942 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22943 nglyphs++;
22945 /* Create and fill the array. */
22946 levels = make_uninit_vector (nglyphs);
22947 for (i = 0; g1 < g; i++, g1++)
22948 ASET (levels, i, make_number (g1->resolved_level));
22950 else /* Right-to-left glyph row. */
22952 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22953 e = row->glyphs[TEXT_AREA] - 1;
22954 while (g > e
22955 && NILP (g->object)
22956 && g->charpos < 0)
22957 g--;
22958 g1 = g;
22959 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22960 nglyphs++;
22961 levels = make_uninit_vector (nglyphs);
22962 for (i = 0; g1 > g; i++, g1--)
22963 ASET (levels, i, make_number (g1->resolved_level));
22965 return levels;
22967 else
22968 return Qnil;
22973 /***********************************************************************
22974 Menu Bar
22975 ***********************************************************************/
22977 /* Redisplay the menu bar in the frame for window W.
22979 The menu bar of X frames that don't have X toolkit support is
22980 displayed in a special window W->frame->menu_bar_window.
22982 The menu bar of terminal frames is treated specially as far as
22983 glyph matrices are concerned. Menu bar lines are not part of
22984 windows, so the update is done directly on the frame matrix rows
22985 for the menu bar. */
22987 static void
22988 display_menu_bar (struct window *w)
22990 struct frame *f = XFRAME (WINDOW_FRAME (w));
22991 struct it it;
22992 Lisp_Object items;
22993 int i;
22995 /* Don't do all this for graphical frames. */
22996 #ifdef HAVE_NTGUI
22997 if (FRAME_W32_P (f))
22998 return;
22999 #endif
23000 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
23001 if (FRAME_X_P (f))
23002 return;
23003 #endif
23005 #ifdef HAVE_NS
23006 if (FRAME_NS_P (f))
23007 return;
23008 #endif /* HAVE_NS */
23010 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
23011 eassert (!FRAME_WINDOW_P (f));
23012 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
23013 it.first_visible_x = 0;
23014 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
23015 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
23016 if (FRAME_WINDOW_P (f))
23018 /* Menu bar lines are displayed in the desired matrix of the
23019 dummy window menu_bar_window. */
23020 struct window *menu_w;
23021 menu_w = XWINDOW (f->menu_bar_window);
23022 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
23023 MENU_FACE_ID);
23024 it.first_visible_x = 0;
23025 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
23027 else
23028 #endif /* not USE_X_TOOLKIT and not USE_GTK */
23030 /* This is a TTY frame, i.e. character hpos/vpos are used as
23031 pixel x/y. */
23032 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
23033 MENU_FACE_ID);
23034 it.first_visible_x = 0;
23035 it.last_visible_x = FRAME_COLS (f);
23038 /* FIXME: This should be controlled by a user option. See the
23039 comments in redisplay_tool_bar and display_mode_line about
23040 this. */
23041 it.paragraph_embedding = L2R;
23043 /* Clear all rows of the menu bar. */
23044 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
23046 struct glyph_row *row = it.glyph_row + i;
23047 clear_glyph_row (row);
23048 row->enabled_p = true;
23049 row->full_width_p = true;
23050 row->reversed_p = false;
23053 /* Display all items of the menu bar. */
23054 items = FRAME_MENU_BAR_ITEMS (it.f);
23055 for (i = 0; i < ASIZE (items); i += 4)
23057 Lisp_Object string;
23059 /* Stop at nil string. */
23060 string = AREF (items, i + 1);
23061 if (NILP (string))
23062 break;
23064 /* Remember where item was displayed. */
23065 ASET (items, i + 3, make_number (it.hpos));
23067 /* Display the item, pad with one space. */
23068 if (it.current_x < it.last_visible_x)
23069 display_string (NULL, string, Qnil, 0, 0, &it,
23070 SCHARS (string) + 1, 0, 0, -1);
23073 /* Fill out the line with spaces. */
23074 if (it.current_x < it.last_visible_x)
23075 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23077 /* Compute the total height of the lines. */
23078 compute_line_metrics (&it);
23081 /* Deep copy of a glyph row, including the glyphs. */
23082 static void
23083 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23085 struct glyph *pointers[1 + LAST_AREA];
23086 int to_used = to->used[TEXT_AREA];
23088 /* Save glyph pointers of TO. */
23089 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23091 /* Do a structure assignment. */
23092 *to = *from;
23094 /* Restore original glyph pointers of TO. */
23095 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23097 /* Copy the glyphs. */
23098 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23099 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23101 /* If we filled only part of the TO row, fill the rest with
23102 space_glyph (which will display as empty space). */
23103 if (to_used > from->used[TEXT_AREA])
23104 fill_up_frame_row_with_spaces (to, to_used);
23107 /* Display one menu item on a TTY, by overwriting the glyphs in the
23108 frame F's desired glyph matrix with glyphs produced from the menu
23109 item text. Called from term.c to display TTY drop-down menus one
23110 item at a time.
23112 ITEM_TEXT is the menu item text as a C string.
23114 FACE_ID is the face ID to be used for this menu item. FACE_ID
23115 could specify one of 3 faces: a face for an enabled item, a face
23116 for a disabled item, or a face for a selected item.
23118 X and Y are coordinates of the first glyph in the frame's desired
23119 matrix to be overwritten by the menu item. Since this is a TTY, Y
23120 is the zero-based number of the glyph row and X is the zero-based
23121 glyph number in the row, starting from left, where to start
23122 displaying the item.
23124 SUBMENU means this menu item drops down a submenu, which
23125 should be indicated by displaying a proper visual cue after the
23126 item text. */
23128 void
23129 display_tty_menu_item (const char *item_text, int width, int face_id,
23130 int x, int y, bool submenu)
23132 struct it it;
23133 struct frame *f = SELECTED_FRAME ();
23134 struct window *w = XWINDOW (f->selected_window);
23135 struct glyph_row *row;
23136 size_t item_len = strlen (item_text);
23138 eassert (FRAME_TERMCAP_P (f));
23140 /* Don't write beyond the matrix's last row. This can happen for
23141 TTY screens that are not high enough to show the entire menu.
23142 (This is actually a bit of defensive programming, as
23143 tty_menu_display already limits the number of menu items to one
23144 less than the number of screen lines.) */
23145 if (y >= f->desired_matrix->nrows)
23146 return;
23148 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23149 it.first_visible_x = 0;
23150 it.last_visible_x = FRAME_COLS (f) - 1;
23151 row = it.glyph_row;
23152 /* Start with the row contents from the current matrix. */
23153 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23154 bool saved_width = row->full_width_p;
23155 row->full_width_p = true;
23156 bool saved_reversed = row->reversed_p;
23157 row->reversed_p = false;
23158 row->enabled_p = true;
23160 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23161 desired face. */
23162 eassert (x < f->desired_matrix->matrix_w);
23163 it.current_x = it.hpos = x;
23164 it.current_y = it.vpos = y;
23165 int saved_used = row->used[TEXT_AREA];
23166 bool saved_truncated = row->truncated_on_right_p;
23167 row->used[TEXT_AREA] = x;
23168 it.face_id = face_id;
23169 it.line_wrap = TRUNCATE;
23171 /* FIXME: This should be controlled by a user option. See the
23172 comments in redisplay_tool_bar and display_mode_line about this.
23173 Also, if paragraph_embedding could ever be R2L, changes will be
23174 needed to avoid shifting to the right the row characters in
23175 term.c:append_glyph. */
23176 it.paragraph_embedding = L2R;
23178 /* Pad with a space on the left. */
23179 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23180 width--;
23181 /* Display the menu item, pad with spaces to WIDTH. */
23182 if (submenu)
23184 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23185 item_len, 0, FRAME_COLS (f) - 1, -1);
23186 width -= item_len;
23187 /* Indicate with " >" that there's a submenu. */
23188 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23189 FRAME_COLS (f) - 1, -1);
23191 else
23192 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23193 width, 0, FRAME_COLS (f) - 1, -1);
23195 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23196 row->truncated_on_right_p = saved_truncated;
23197 row->hash = row_hash (row);
23198 row->full_width_p = saved_width;
23199 row->reversed_p = saved_reversed;
23202 /***********************************************************************
23203 Mode Line
23204 ***********************************************************************/
23206 /* Redisplay mode lines in the window tree whose root is WINDOW.
23207 If FORCE, redisplay mode lines unconditionally.
23208 Otherwise, redisplay only mode lines that are garbaged. Value is
23209 the number of windows whose mode lines were redisplayed. */
23211 static int
23212 redisplay_mode_lines (Lisp_Object window, bool force)
23214 int nwindows = 0;
23216 while (!NILP (window))
23218 struct window *w = XWINDOW (window);
23220 if (WINDOWP (w->contents))
23221 nwindows += redisplay_mode_lines (w->contents, force);
23222 else if (force
23223 || FRAME_GARBAGED_P (XFRAME (w->frame))
23224 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23226 struct text_pos lpoint;
23227 struct buffer *old = current_buffer;
23229 /* Set the window's buffer for the mode line display. */
23230 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23231 set_buffer_internal_1 (XBUFFER (w->contents));
23233 /* Point refers normally to the selected window. For any
23234 other window, set up appropriate value. */
23235 if (!EQ (window, selected_window))
23237 struct text_pos pt;
23239 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23240 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23243 /* Display mode lines. */
23244 clear_glyph_matrix (w->desired_matrix);
23245 if (display_mode_lines (w))
23246 ++nwindows;
23248 /* Restore old settings. */
23249 set_buffer_internal_1 (old);
23250 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23253 window = w->next;
23256 return nwindows;
23260 /* Display the mode and/or header line of window W. Value is the
23261 sum number of mode lines and header lines displayed. */
23263 static int
23264 display_mode_lines (struct window *w)
23266 Lisp_Object old_selected_window = selected_window;
23267 Lisp_Object old_selected_frame = selected_frame;
23268 Lisp_Object new_frame = w->frame;
23269 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23270 int n = 0;
23272 if (window_wants_mode_line (w))
23274 Lisp_Object window;
23275 Lisp_Object default_help
23276 = buffer_local_value (Qmode_line_default_help_echo, w->contents);
23278 /* Set up mode line help echo. Do this before selecting w so it
23279 can reasonably tell whether a mouse click will select w. */
23280 XSETWINDOW (window, w);
23281 if (FUNCTIONP (default_help))
23282 wset_mode_line_help_echo (w, safe_call1 (default_help, window));
23283 else if (STRINGP (default_help))
23284 wset_mode_line_help_echo (w, default_help);
23285 else
23286 wset_mode_line_help_echo (w, Qnil);
23289 selected_frame = new_frame;
23290 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23291 or window's point, then we'd need select_window_1 here as well. */
23292 XSETWINDOW (selected_window, w);
23293 XFRAME (new_frame)->selected_window = selected_window;
23295 /* These will be set while the mode line specs are processed. */
23296 line_number_displayed = false;
23297 w->column_number_displayed = -1;
23299 if (window_wants_mode_line (w))
23301 Lisp_Object window_mode_line_format
23302 = window_parameter (w, Qmode_line_format);
23303 struct window *sel_w = XWINDOW (old_selected_window);
23305 /* Select mode line face based on the real selected window. */
23306 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23307 NILP (window_mode_line_format)
23308 ? BVAR (current_buffer, mode_line_format)
23309 : window_mode_line_format);
23310 ++n;
23313 if (window_wants_header_line (w))
23315 Lisp_Object window_header_line_format
23316 = window_parameter (w, Qheader_line_format);
23318 display_mode_line (w, HEADER_LINE_FACE_ID,
23319 NILP (window_header_line_format)
23320 ? BVAR (current_buffer, header_line_format)
23321 : window_header_line_format);
23322 ++n;
23325 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23326 selected_frame = old_selected_frame;
23327 selected_window = old_selected_window;
23328 if (n > 0)
23329 w->must_be_updated_p = true;
23330 return n;
23334 /* Display mode or header line of window W. FACE_ID specifies which
23335 line to display; it is either MODE_LINE_FACE_ID or
23336 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23337 display. Value is the pixel height of the mode/header line
23338 displayed. */
23340 static int
23341 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23343 struct it it;
23344 struct face *face;
23345 ptrdiff_t count = SPECPDL_INDEX ();
23347 init_iterator (&it, w, -1, -1, NULL, face_id);
23348 /* Don't extend on a previously drawn mode-line.
23349 This may happen if called from pos_visible_p. */
23350 it.glyph_row->enabled_p = false;
23351 prepare_desired_row (w, it.glyph_row, true);
23353 it.glyph_row->mode_line_p = true;
23355 /* FIXME: This should be controlled by a user option. But
23356 supporting such an option is not trivial, since the mode line is
23357 made up of many separate strings. */
23358 it.paragraph_embedding = L2R;
23360 record_unwind_protect (unwind_format_mode_line,
23361 format_mode_line_unwind_data (NULL, NULL,
23362 Qnil, false));
23364 mode_line_target = MODE_LINE_DISPLAY;
23366 /* Temporarily make frame's keyboard the current kboard so that
23367 kboard-local variables in the mode_line_format will get the right
23368 values. */
23369 push_kboard (FRAME_KBOARD (it.f));
23370 record_unwind_save_match_data ();
23371 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23372 pop_kboard ();
23374 unbind_to (count, Qnil);
23376 /* Fill up with spaces. */
23377 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23379 compute_line_metrics (&it);
23380 it.glyph_row->full_width_p = true;
23381 it.glyph_row->continued_p = false;
23382 it.glyph_row->truncated_on_left_p = false;
23383 it.glyph_row->truncated_on_right_p = false;
23385 /* Make a 3D mode-line have a shadow at its right end. */
23386 face = FACE_FROM_ID (it.f, face_id);
23387 extend_face_to_end_of_line (&it);
23388 if (face->box != FACE_NO_BOX)
23390 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23391 + it.glyph_row->used[TEXT_AREA] - 1);
23392 last->right_box_line_p = true;
23395 return it.glyph_row->height;
23398 /* Move element ELT in LIST to the front of LIST.
23399 Return the updated list. */
23401 static Lisp_Object
23402 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23404 register Lisp_Object tail, prev;
23405 register Lisp_Object tem;
23407 tail = list;
23408 prev = Qnil;
23409 while (CONSP (tail))
23411 tem = XCAR (tail);
23413 if (EQ (elt, tem))
23415 /* Splice out the link TAIL. */
23416 if (NILP (prev))
23417 list = XCDR (tail);
23418 else
23419 Fsetcdr (prev, XCDR (tail));
23421 /* Now make it the first. */
23422 Fsetcdr (tail, list);
23423 return tail;
23425 else
23426 prev = tail;
23427 tail = XCDR (tail);
23428 maybe_quit ();
23431 /* Not found--return unchanged LIST. */
23432 return list;
23435 /* Contribute ELT to the mode line for window IT->w. How it
23436 translates into text depends on its data type.
23438 IT describes the display environment in which we display, as usual.
23440 DEPTH is the depth in recursion. It is used to prevent
23441 infinite recursion here.
23443 FIELD_WIDTH is the number of characters the display of ELT should
23444 occupy in the mode line, and PRECISION is the maximum number of
23445 characters to display from ELT's representation. See
23446 display_string for details.
23448 Returns the hpos of the end of the text generated by ELT.
23450 PROPS is a property list to add to any string we encounter.
23452 If RISKY, remove (disregard) any properties in any string
23453 we encounter, and ignore :eval and :propertize.
23455 The global variable `mode_line_target' determines whether the
23456 output is passed to `store_mode_line_noprop',
23457 `store_mode_line_string', or `display_string'. */
23459 static int
23460 display_mode_element (struct it *it, int depth, int field_width, int precision,
23461 Lisp_Object elt, Lisp_Object props, bool risky)
23463 int n = 0, field, prec;
23464 bool literal = false;
23466 tail_recurse:
23467 if (depth > 100)
23468 elt = build_string ("*too-deep*");
23470 depth++;
23472 switch (XTYPE (elt))
23474 case Lisp_String:
23476 /* A string: output it and check for %-constructs within it. */
23477 unsigned char c;
23478 ptrdiff_t offset = 0;
23480 if (SCHARS (elt) > 0
23481 && (!NILP (props) || risky))
23483 Lisp_Object oprops, aelt;
23484 oprops = Ftext_properties_at (make_number (0), elt);
23486 /* If the starting string's properties are not what
23487 we want, translate the string. Also, if the string
23488 is risky, do that anyway. */
23490 if (NILP (Fequal (props, oprops)) || risky)
23492 /* If the starting string has properties,
23493 merge the specified ones onto the existing ones. */
23494 if (! NILP (oprops) && !risky)
23496 Lisp_Object tem;
23498 oprops = Fcopy_sequence (oprops);
23499 tem = props;
23500 while (CONSP (tem))
23502 oprops = Fplist_put (oprops, XCAR (tem),
23503 XCAR (XCDR (tem)));
23504 tem = XCDR (XCDR (tem));
23506 props = oprops;
23509 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23510 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23512 /* AELT is what we want. Move it to the front
23513 without consing. */
23514 elt = XCAR (aelt);
23515 mode_line_proptrans_alist
23516 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23518 else
23520 Lisp_Object tem;
23522 /* If AELT has the wrong props, it is useless.
23523 so get rid of it. */
23524 if (! NILP (aelt))
23525 mode_line_proptrans_alist
23526 = Fdelq (aelt, mode_line_proptrans_alist);
23528 elt = Fcopy_sequence (elt);
23529 Fset_text_properties (make_number (0), Flength (elt),
23530 props, elt);
23531 /* Add this item to mode_line_proptrans_alist. */
23532 mode_line_proptrans_alist
23533 = Fcons (Fcons (elt, props),
23534 mode_line_proptrans_alist);
23535 /* Truncate mode_line_proptrans_alist
23536 to at most 50 elements. */
23537 tem = Fnthcdr (make_number (50),
23538 mode_line_proptrans_alist);
23539 if (! NILP (tem))
23540 XSETCDR (tem, Qnil);
23545 offset = 0;
23547 if (literal)
23549 prec = precision - n;
23550 switch (mode_line_target)
23552 case MODE_LINE_NOPROP:
23553 case MODE_LINE_TITLE:
23554 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23555 break;
23556 case MODE_LINE_STRING:
23557 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23558 break;
23559 case MODE_LINE_DISPLAY:
23560 n += display_string (NULL, elt, Qnil, 0, 0, it,
23561 0, prec, 0, STRING_MULTIBYTE (elt));
23562 break;
23565 break;
23568 /* Handle the non-literal case. */
23570 while ((precision <= 0 || n < precision)
23571 && SREF (elt, offset) != 0
23572 && (mode_line_target != MODE_LINE_DISPLAY
23573 || it->current_x < it->last_visible_x))
23575 ptrdiff_t last_offset = offset;
23577 /* Advance to end of string or next format specifier. */
23578 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23581 if (offset - 1 != last_offset)
23583 ptrdiff_t nchars, nbytes;
23585 /* Output to end of string or up to '%'. Field width
23586 is length of string. Don't output more than
23587 PRECISION allows us. */
23588 offset--;
23590 prec = c_string_width (SDATA (elt) + last_offset,
23591 offset - last_offset, precision - n,
23592 &nchars, &nbytes);
23594 switch (mode_line_target)
23596 case MODE_LINE_NOPROP:
23597 case MODE_LINE_TITLE:
23598 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23599 break;
23600 case MODE_LINE_STRING:
23602 ptrdiff_t bytepos = last_offset;
23603 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23604 ptrdiff_t endpos = (precision <= 0
23605 ? string_byte_to_char (elt, offset)
23606 : charpos + nchars);
23607 Lisp_Object mode_string
23608 = Fsubstring (elt, make_number (charpos),
23609 make_number (endpos));
23610 n += store_mode_line_string (NULL, mode_string, false,
23611 0, 0, Qnil);
23613 break;
23614 case MODE_LINE_DISPLAY:
23616 ptrdiff_t bytepos = last_offset;
23617 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23619 if (precision <= 0)
23620 nchars = string_byte_to_char (elt, offset) - charpos;
23621 n += display_string (NULL, elt, Qnil, 0, charpos,
23622 it, 0, nchars, 0,
23623 STRING_MULTIBYTE (elt));
23625 break;
23628 else /* c == '%' */
23630 ptrdiff_t percent_position = offset;
23632 /* Get the specified minimum width. Zero means
23633 don't pad. */
23634 field = 0;
23635 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23636 field = field * 10 + c - '0';
23638 /* Don't pad beyond the total padding allowed. */
23639 if (field_width - n > 0 && field > field_width - n)
23640 field = field_width - n;
23642 /* Note that either PRECISION <= 0 or N < PRECISION. */
23643 prec = precision - n;
23645 if (c == 'M')
23646 n += display_mode_element (it, depth, field, prec,
23647 Vglobal_mode_string, props,
23648 risky);
23649 else if (c != 0)
23651 bool multibyte;
23652 ptrdiff_t bytepos, charpos;
23653 const char *spec;
23654 Lisp_Object string;
23656 bytepos = percent_position;
23657 charpos = (STRING_MULTIBYTE (elt)
23658 ? string_byte_to_char (elt, bytepos)
23659 : bytepos);
23660 spec = decode_mode_spec (it->w, c, field, &string);
23661 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23663 switch (mode_line_target)
23665 case MODE_LINE_NOPROP:
23666 case MODE_LINE_TITLE:
23667 n += store_mode_line_noprop (spec, field, prec);
23668 break;
23669 case MODE_LINE_STRING:
23671 Lisp_Object tem = build_string (spec);
23672 props = Ftext_properties_at (make_number (charpos), elt);
23673 /* Should only keep face property in props */
23674 n += store_mode_line_string (NULL, tem, false,
23675 field, prec, props);
23677 break;
23678 case MODE_LINE_DISPLAY:
23680 int nglyphs_before, nwritten;
23682 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23683 nwritten = display_string (spec, string, elt,
23684 charpos, 0, it,
23685 field, prec, 0,
23686 multibyte);
23688 /* Assign to the glyphs written above the
23689 string where the `%x' came from, position
23690 of the `%'. */
23691 if (nwritten > 0)
23693 struct glyph *glyph
23694 = (it->glyph_row->glyphs[TEXT_AREA]
23695 + nglyphs_before);
23696 int i;
23698 for (i = 0; i < nwritten; ++i)
23700 glyph[i].object = elt;
23701 glyph[i].charpos = charpos;
23704 n += nwritten;
23707 break;
23710 else /* c == 0 */
23711 break;
23715 break;
23717 case Lisp_Symbol:
23718 /* A symbol: process the value of the symbol recursively
23719 as if it appeared here directly. Avoid error if symbol void.
23720 Special case: if value of symbol is a string, output the string
23721 literally. */
23723 register Lisp_Object tem;
23725 /* If the variable is not marked as risky to set
23726 then its contents are risky to use. */
23727 if (NILP (Fget (elt, Qrisky_local_variable)))
23728 risky = true;
23730 tem = Fboundp (elt);
23731 if (!NILP (tem))
23733 tem = Fsymbol_value (elt);
23734 /* If value is a string, output that string literally:
23735 don't check for % within it. */
23736 if (STRINGP (tem))
23737 literal = true;
23739 if (!EQ (tem, elt))
23741 /* Give up right away for nil or t. */
23742 elt = tem;
23743 goto tail_recurse;
23747 break;
23749 case Lisp_Cons:
23751 register Lisp_Object car, tem;
23753 /* A cons cell: five distinct cases.
23754 If first element is :eval or :propertize, do something special.
23755 If first element is a string or a cons, process all the elements
23756 and effectively concatenate them.
23757 If first element is a negative number, truncate displaying cdr to
23758 at most that many characters. If positive, pad (with spaces)
23759 to at least that many characters.
23760 If first element is a symbol, process the cadr or caddr recursively
23761 according to whether the symbol's value is non-nil or nil. */
23762 car = XCAR (elt);
23763 if (EQ (car, QCeval))
23765 /* An element of the form (:eval FORM) means evaluate FORM
23766 and use the result as mode line elements. */
23768 if (risky)
23769 break;
23771 if (CONSP (XCDR (elt)))
23773 Lisp_Object spec;
23774 spec = safe__eval (true, XCAR (XCDR (elt)));
23775 /* The :eval form could delete the frame stored in the
23776 iterator, which will cause a crash if we try to
23777 access faces and other fields (e.g., FRAME_KBOARD)
23778 on that frame. This is a nonsensical thing to do,
23779 and signaling an error from redisplay might be
23780 dangerous, but we cannot continue with an invalid frame. */
23781 if (!FRAME_LIVE_P (it->f))
23782 signal_error (":eval deleted the frame being displayed", elt);
23783 n += display_mode_element (it, depth, field_width - n,
23784 precision - n, spec, props,
23785 risky);
23788 else if (EQ (car, QCpropertize))
23790 /* An element of the form (:propertize ELT PROPS...)
23791 means display ELT but applying properties PROPS. */
23793 if (risky)
23794 break;
23796 if (CONSP (XCDR (elt)))
23797 n += display_mode_element (it, depth, field_width - n,
23798 precision - n, XCAR (XCDR (elt)),
23799 XCDR (XCDR (elt)), risky);
23801 else if (SYMBOLP (car))
23803 tem = Fboundp (car);
23804 elt = XCDR (elt);
23805 if (!CONSP (elt))
23806 goto invalid;
23807 /* elt is now the cdr, and we know it is a cons cell.
23808 Use its car if CAR has a non-nil value. */
23809 if (!NILP (tem))
23811 tem = Fsymbol_value (car);
23812 if (!NILP (tem))
23814 elt = XCAR (elt);
23815 goto tail_recurse;
23818 /* Symbol's value is nil (or symbol is unbound)
23819 Get the cddr of the original list
23820 and if possible find the caddr and use that. */
23821 elt = XCDR (elt);
23822 if (NILP (elt))
23823 break;
23824 else if (!CONSP (elt))
23825 goto invalid;
23826 elt = XCAR (elt);
23827 goto tail_recurse;
23829 else if (INTEGERP (car))
23831 register int lim = XINT (car);
23832 elt = XCDR (elt);
23833 if (lim < 0)
23835 /* Negative int means reduce maximum width. */
23836 if (precision <= 0)
23837 precision = -lim;
23838 else
23839 precision = min (precision, -lim);
23841 else if (lim > 0)
23843 /* Padding specified. Don't let it be more than
23844 current maximum. */
23845 if (precision > 0)
23846 lim = min (precision, lim);
23848 /* If that's more padding than already wanted, queue it.
23849 But don't reduce padding already specified even if
23850 that is beyond the current truncation point. */
23851 field_width = max (lim, field_width);
23853 goto tail_recurse;
23855 else if (STRINGP (car) || CONSP (car))
23856 FOR_EACH_TAIL_SAFE (elt)
23858 if (0 < precision && precision <= n)
23859 break;
23860 n += display_mode_element (it, depth,
23861 /* Pad after only the last
23862 list element. */
23863 (! CONSP (XCDR (elt))
23864 ? field_width - n
23865 : 0),
23866 precision - n, XCAR (elt),
23867 props, risky);
23870 break;
23872 default:
23873 invalid:
23874 elt = build_string ("*invalid*");
23875 goto tail_recurse;
23878 /* Pad to FIELD_WIDTH. */
23879 if (field_width > 0 && n < field_width)
23881 switch (mode_line_target)
23883 case MODE_LINE_NOPROP:
23884 case MODE_LINE_TITLE:
23885 n += store_mode_line_noprop ("", field_width - n, 0);
23886 break;
23887 case MODE_LINE_STRING:
23888 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23889 Qnil);
23890 break;
23891 case MODE_LINE_DISPLAY:
23892 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23893 0, 0, 0);
23894 break;
23898 return n;
23901 /* Store a mode-line string element in mode_line_string_list.
23903 If STRING is non-null, display that C string. Otherwise, the Lisp
23904 string LISP_STRING is displayed.
23906 FIELD_WIDTH is the minimum number of output glyphs to produce.
23907 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23908 with spaces. FIELD_WIDTH <= 0 means don't pad.
23910 PRECISION is the maximum number of characters to output from
23911 STRING. PRECISION <= 0 means don't truncate the string.
23913 If COPY_STRING, make a copy of LISP_STRING before adding
23914 properties to the string.
23916 PROPS are the properties to add to the string.
23917 The mode_line_string_face face property is always added to the string.
23920 static int
23921 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23922 bool copy_string,
23923 int field_width, int precision, Lisp_Object props)
23925 ptrdiff_t len;
23926 int n = 0;
23928 if (string != NULL)
23930 len = strlen (string);
23931 if (precision > 0 && len > precision)
23932 len = precision;
23933 lisp_string = make_string (string, len);
23934 if (NILP (props))
23935 props = mode_line_string_face_prop;
23936 else if (!NILP (mode_line_string_face))
23938 Lisp_Object face = Fplist_get (props, Qface);
23939 props = Fcopy_sequence (props);
23940 if (NILP (face))
23941 face = mode_line_string_face;
23942 else
23943 face = list2 (face, mode_line_string_face);
23944 props = Fplist_put (props, Qface, face);
23946 Fadd_text_properties (make_number (0), make_number (len),
23947 props, lisp_string);
23949 else
23951 len = XFASTINT (Flength (lisp_string));
23952 if (precision > 0 && len > precision)
23954 len = precision;
23955 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23956 precision = -1;
23958 if (!NILP (mode_line_string_face))
23960 Lisp_Object face;
23961 if (NILP (props))
23962 props = Ftext_properties_at (make_number (0), lisp_string);
23963 face = Fplist_get (props, Qface);
23964 if (NILP (face))
23965 face = mode_line_string_face;
23966 else
23967 face = list2 (face, mode_line_string_face);
23968 props = list2 (Qface, face);
23969 if (copy_string)
23970 lisp_string = Fcopy_sequence (lisp_string);
23972 if (!NILP (props))
23973 Fadd_text_properties (make_number (0), make_number (len),
23974 props, lisp_string);
23977 if (len > 0)
23979 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23980 n += len;
23983 if (field_width > len)
23985 field_width -= len;
23986 lisp_string = Fmake_string (make_number (field_width), make_number (' '),
23987 Qnil);
23988 if (!NILP (props))
23989 Fadd_text_properties (make_number (0), make_number (field_width),
23990 props, lisp_string);
23991 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23992 n += field_width;
23995 return n;
23999 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
24000 1, 4, 0,
24001 doc: /* Format a string out of a mode line format specification.
24002 First arg FORMAT specifies the mode line format (see `mode-line-format'
24003 for details) to use.
24005 By default, the format is evaluated for the currently selected window.
24007 Optional second arg FACE specifies the face property to put on all
24008 characters for which no face is specified. The value nil means the
24009 default face. The value t means whatever face the window's mode line
24010 currently uses (either `mode-line' or `mode-line-inactive',
24011 depending on whether the window is the selected window or not).
24012 An integer value means the value string has no text
24013 properties.
24015 Optional third and fourth args WINDOW and BUFFER specify the window
24016 and buffer to use as the context for the formatting (defaults
24017 are the selected window and the WINDOW's buffer). */)
24018 (Lisp_Object format, Lisp_Object face,
24019 Lisp_Object window, Lisp_Object buffer)
24021 struct it it;
24022 int len;
24023 struct window *w;
24024 struct buffer *old_buffer = NULL;
24025 int face_id;
24026 bool no_props = INTEGERP (face);
24027 ptrdiff_t count = SPECPDL_INDEX ();
24028 Lisp_Object str;
24029 int string_start = 0;
24031 w = decode_any_window (window);
24032 XSETWINDOW (window, w);
24034 if (NILP (buffer))
24035 buffer = w->contents;
24036 CHECK_BUFFER (buffer);
24038 /* Make formatting the modeline a non-op when noninteractive, otherwise
24039 there will be problems later caused by a partially initialized frame. */
24040 if (NILP (format) || noninteractive)
24041 return empty_unibyte_string;
24043 if (no_props)
24044 face = Qnil;
24046 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
24047 : EQ (face, Qt) ? (EQ (window, selected_window)
24048 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
24049 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
24050 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
24051 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
24052 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
24053 : DEFAULT_FACE_ID;
24055 old_buffer = current_buffer;
24057 /* Save things including mode_line_proptrans_alist,
24058 and set that to nil so that we don't alter the outer value. */
24059 record_unwind_protect (unwind_format_mode_line,
24060 format_mode_line_unwind_data
24061 (XFRAME (WINDOW_FRAME (w)),
24062 old_buffer, selected_window, true));
24063 mode_line_proptrans_alist = Qnil;
24065 Fselect_window (window, Qt);
24066 set_buffer_internal_1 (XBUFFER (buffer));
24068 init_iterator (&it, w, -1, -1, NULL, face_id);
24070 if (no_props)
24072 mode_line_target = MODE_LINE_NOPROP;
24073 mode_line_string_face_prop = Qnil;
24074 mode_line_string_list = Qnil;
24075 string_start = MODE_LINE_NOPROP_LEN (0);
24077 else
24079 mode_line_target = MODE_LINE_STRING;
24080 mode_line_string_list = Qnil;
24081 mode_line_string_face = face;
24082 mode_line_string_face_prop
24083 = NILP (face) ? Qnil : list2 (Qface, face);
24086 push_kboard (FRAME_KBOARD (it.f));
24087 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
24088 pop_kboard ();
24090 if (no_props)
24092 len = MODE_LINE_NOPROP_LEN (string_start);
24093 str = make_string (mode_line_noprop_buf + string_start, len);
24095 else
24097 mode_line_string_list = Fnreverse (mode_line_string_list);
24098 str = Fmapconcat (Qidentity, mode_line_string_list,
24099 empty_unibyte_string);
24102 unbind_to (count, Qnil);
24103 return str;
24106 /* Write a null-terminated, right justified decimal representation of
24107 the positive integer D to BUF using a minimal field width WIDTH. */
24109 static void
24110 pint2str (register char *buf, register int width, register ptrdiff_t d)
24112 register char *p = buf;
24114 if (d <= 0)
24115 *p++ = '0';
24116 else
24118 while (d > 0)
24120 *p++ = d % 10 + '0';
24121 d /= 10;
24125 for (width -= (int) (p - buf); width > 0; --width)
24126 *p++ = ' ';
24127 *p-- = '\0';
24128 while (p > buf)
24130 d = *buf;
24131 *buf++ = *p;
24132 *p-- = d;
24136 /* Write a null-terminated, right justified decimal and "human
24137 readable" representation of the nonnegative integer D to BUF using
24138 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24140 static const char power_letter[] =
24142 0, /* no letter */
24143 'k', /* kilo */
24144 'M', /* mega */
24145 'G', /* giga */
24146 'T', /* tera */
24147 'P', /* peta */
24148 'E', /* exa */
24149 'Z', /* zetta */
24150 'Y' /* yotta */
24153 static void
24154 pint2hrstr (char *buf, int width, ptrdiff_t d)
24156 /* We aim to represent the nonnegative integer D as
24157 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24158 ptrdiff_t quotient = d;
24159 int remainder = 0;
24160 /* -1 means: do not use TENTHS. */
24161 int tenths = -1;
24162 int exponent = 0;
24164 /* Length of QUOTIENT.TENTHS as a string. */
24165 int length;
24167 char * psuffix;
24168 char * p;
24170 if (quotient >= 1000)
24172 /* Scale to the appropriate EXPONENT. */
24175 remainder = quotient % 1000;
24176 quotient /= 1000;
24177 exponent++;
24179 while (quotient >= 1000);
24181 /* Round to nearest and decide whether to use TENTHS or not. */
24182 if (quotient <= 9)
24184 tenths = remainder / 100;
24185 if (remainder % 100 >= 50)
24187 if (tenths < 9)
24188 tenths++;
24189 else
24191 quotient++;
24192 if (quotient == 10)
24193 tenths = -1;
24194 else
24195 tenths = 0;
24199 else
24200 if (remainder >= 500)
24202 if (quotient < 999)
24203 quotient++;
24204 else
24206 quotient = 1;
24207 exponent++;
24208 tenths = 0;
24213 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24214 if (tenths == -1 && quotient <= 99)
24215 if (quotient <= 9)
24216 length = 1;
24217 else
24218 length = 2;
24219 else
24220 length = 3;
24221 p = psuffix = buf + max (width, length);
24223 /* Print EXPONENT. */
24224 *psuffix++ = power_letter[exponent];
24225 *psuffix = '\0';
24227 /* Print TENTHS. */
24228 if (tenths >= 0)
24230 *--p = '0' + tenths;
24231 *--p = '.';
24234 /* Print QUOTIENT. */
24237 int digit = quotient % 10;
24238 *--p = '0' + digit;
24240 while ((quotient /= 10) != 0);
24242 /* Print leading spaces. */
24243 while (buf < p)
24244 *--p = ' ';
24247 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24248 If EOL_FLAG, set also a mnemonic character for end-of-line
24249 type of CODING_SYSTEM. Return updated pointer into BUF. */
24251 static unsigned char invalid_eol_type[] = "(*invalid*)";
24253 static char *
24254 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24256 Lisp_Object val;
24257 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24258 const unsigned char *eol_str;
24259 int eol_str_len;
24260 /* The EOL conversion we are using. */
24261 Lisp_Object eoltype;
24263 val = CODING_SYSTEM_SPEC (coding_system);
24264 eoltype = Qnil;
24266 if (!VECTORP (val)) /* Not yet decided. */
24268 *buf++ = multibyte ? '-' : ' ';
24269 if (eol_flag)
24270 eoltype = eol_mnemonic_undecided;
24271 /* Don't mention EOL conversion if it isn't decided. */
24273 else
24275 Lisp_Object attrs;
24276 Lisp_Object eolvalue;
24278 attrs = AREF (val, 0);
24279 eolvalue = AREF (val, 2);
24281 *buf++ = multibyte
24282 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24283 : ' ';
24285 if (eol_flag)
24287 /* The EOL conversion that is normal on this system. */
24289 if (NILP (eolvalue)) /* Not yet decided. */
24290 eoltype = eol_mnemonic_undecided;
24291 else if (VECTORP (eolvalue)) /* Not yet decided. */
24292 eoltype = eol_mnemonic_undecided;
24293 else /* eolvalue is Qunix, Qdos, or Qmac. */
24294 eoltype = (EQ (eolvalue, Qunix)
24295 ? eol_mnemonic_unix
24296 : EQ (eolvalue, Qdos)
24297 ? eol_mnemonic_dos : eol_mnemonic_mac);
24301 if (eol_flag)
24303 /* Mention the EOL conversion if it is not the usual one. */
24304 if (STRINGP (eoltype))
24306 eol_str = SDATA (eoltype);
24307 eol_str_len = SBYTES (eoltype);
24309 else if (CHARACTERP (eoltype))
24311 int c = XFASTINT (eoltype);
24312 return buf + CHAR_STRING (c, (unsigned char *) buf);
24314 else
24316 eol_str = invalid_eol_type;
24317 eol_str_len = sizeof (invalid_eol_type) - 1;
24319 memcpy (buf, eol_str, eol_str_len);
24320 buf += eol_str_len;
24323 return buf;
24326 /* Return the approximate percentage N is of D (rounding upward), or 99,
24327 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24329 static int
24330 percent99 (ptrdiff_t n, ptrdiff_t d)
24332 int percent = (d - 1 + 100.0 * n) / d;
24333 return min (percent, 99);
24336 /* Return a string for the output of a mode line %-spec for window W,
24337 generated by character C. FIELD_WIDTH > 0 means pad the string
24338 returned with spaces to that value. Return a Lisp string in
24339 *STRING if the resulting string is taken from that Lisp string.
24341 Note we operate on the current buffer for most purposes. */
24343 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24345 static const char *
24346 decode_mode_spec (struct window *w, register int c, int field_width,
24347 Lisp_Object *string)
24349 Lisp_Object obj;
24350 struct frame *f = XFRAME (WINDOW_FRAME (w));
24351 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24352 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24353 produce strings from numerical values, so limit preposterously
24354 large values of FIELD_WIDTH to avoid overrunning the buffer's
24355 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24356 bytes plus the terminating null. */
24357 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24358 struct buffer *b = current_buffer;
24360 obj = Qnil;
24361 *string = Qnil;
24363 switch (c)
24365 case '*':
24366 if (!NILP (BVAR (b, read_only)))
24367 return "%";
24368 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24369 return "*";
24370 return "-";
24372 case '+':
24373 /* This differs from %* only for a modified read-only buffer. */
24374 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24375 return "*";
24376 if (!NILP (BVAR (b, read_only)))
24377 return "%";
24378 return "-";
24380 case '&':
24381 /* This differs from %* in ignoring read-only-ness. */
24382 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24383 return "*";
24384 return "-";
24386 case '%':
24387 return "%";
24389 case '[':
24391 int i;
24392 char *p;
24394 if (command_loop_level > 5)
24395 return "[[[... ";
24396 p = decode_mode_spec_buf;
24397 for (i = 0; i < command_loop_level; i++)
24398 *p++ = '[';
24399 *p = 0;
24400 return decode_mode_spec_buf;
24403 case ']':
24405 int i;
24406 char *p;
24408 if (command_loop_level > 5)
24409 return " ...]]]";
24410 p = decode_mode_spec_buf;
24411 for (i = 0; i < command_loop_level; i++)
24412 *p++ = ']';
24413 *p = 0;
24414 return decode_mode_spec_buf;
24417 case '-':
24419 register int i;
24421 /* Let lots_of_dashes be a string of infinite length. */
24422 if (mode_line_target == MODE_LINE_NOPROP
24423 || mode_line_target == MODE_LINE_STRING)
24424 return "--";
24425 if (field_width <= 0
24426 || field_width > sizeof (lots_of_dashes))
24428 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24429 decode_mode_spec_buf[i] = '-';
24430 decode_mode_spec_buf[i] = '\0';
24431 return decode_mode_spec_buf;
24433 else
24434 return lots_of_dashes;
24437 case 'b':
24438 obj = BVAR (b, name);
24439 break;
24441 case 'c':
24442 case 'C':
24443 /* %c, %C, and %l are ignored in `frame-title-format'.
24444 (In redisplay_internal, the frame title is drawn _before_ the
24445 windows are updated, so the stuff which depends on actual
24446 window contents (such as %l) may fail to render properly, or
24447 even crash emacs.) */
24448 if (mode_line_target == MODE_LINE_TITLE)
24449 return "";
24450 else
24452 ptrdiff_t col = current_column ();
24453 int disp_col = (c == 'C') ? col + 1 : col;
24454 w->column_number_displayed = col;
24455 pint2str (decode_mode_spec_buf, width, disp_col);
24456 return decode_mode_spec_buf;
24459 case 'e':
24460 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24462 if (NILP (Vmemory_full))
24463 return "";
24464 else
24465 return "!MEM FULL! ";
24467 #else
24468 return "";
24469 #endif
24471 case 'F':
24472 /* %F displays the frame name. */
24473 if (!NILP (f->title))
24474 return SSDATA (f->title);
24475 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24476 return SSDATA (f->name);
24477 return "Emacs";
24479 case 'f':
24480 obj = BVAR (b, filename);
24481 break;
24483 case 'i':
24485 ptrdiff_t size = ZV - BEGV;
24486 pint2str (decode_mode_spec_buf, width, size);
24487 return decode_mode_spec_buf;
24490 case 'I':
24492 ptrdiff_t size = ZV - BEGV;
24493 pint2hrstr (decode_mode_spec_buf, width, size);
24494 return decode_mode_spec_buf;
24497 case 'l':
24499 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24500 ptrdiff_t topline, nlines, height;
24501 ptrdiff_t junk;
24503 /* %c, %C, and %l are ignored in `frame-title-format'. */
24504 if (mode_line_target == MODE_LINE_TITLE)
24505 return "";
24507 startpos = marker_position (w->start);
24508 startpos_byte = marker_byte_position (w->start);
24509 height = WINDOW_TOTAL_LINES (w);
24511 /* If we decided that this buffer isn't suitable for line numbers,
24512 don't forget that too fast. */
24513 if (w->base_line_pos == -1)
24514 goto no_value;
24516 /* If the buffer is very big, don't waste time. */
24517 if (INTEGERP (Vline_number_display_limit)
24518 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24520 w->base_line_pos = 0;
24521 w->base_line_number = 0;
24522 goto no_value;
24525 if (w->base_line_number > 0
24526 && w->base_line_pos > 0
24527 && w->base_line_pos <= startpos)
24529 line = w->base_line_number;
24530 linepos = w->base_line_pos;
24531 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24533 else
24535 line = 1;
24536 linepos = BUF_BEGV (b);
24537 linepos_byte = BUF_BEGV_BYTE (b);
24540 /* Count lines from base line to window start position. */
24541 nlines = display_count_lines (linepos_byte,
24542 startpos_byte,
24543 startpos, &junk);
24545 topline = nlines + line;
24547 /* Determine a new base line, if the old one is too close
24548 or too far away, or if we did not have one.
24549 "Too close" means it's plausible a scroll-down would
24550 go back past it. */
24551 if (startpos == BUF_BEGV (b))
24553 w->base_line_number = topline;
24554 w->base_line_pos = BUF_BEGV (b);
24556 else if (nlines < height + 25 || nlines > height * 3 + 50
24557 || linepos == BUF_BEGV (b))
24559 ptrdiff_t limit = BUF_BEGV (b);
24560 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24561 ptrdiff_t position;
24562 ptrdiff_t distance =
24563 (height * 2 + 30) * line_number_display_limit_width;
24565 if (startpos - distance > limit)
24567 limit = startpos - distance;
24568 limit_byte = CHAR_TO_BYTE (limit);
24571 nlines = display_count_lines (startpos_byte,
24572 limit_byte,
24573 - (height * 2 + 30),
24574 &position);
24575 /* If we couldn't find the lines we wanted within
24576 line_number_display_limit_width chars per line,
24577 give up on line numbers for this window. */
24578 if (position == limit_byte && limit == startpos - distance)
24580 w->base_line_pos = -1;
24581 w->base_line_number = 0;
24582 goto no_value;
24585 w->base_line_number = topline - nlines;
24586 w->base_line_pos = BYTE_TO_CHAR (position);
24589 /* Now count lines from the start pos to point. */
24590 nlines = display_count_lines (startpos_byte,
24591 PT_BYTE, PT, &junk);
24593 /* Record that we did display the line number. */
24594 line_number_displayed = true;
24596 /* Make the string to show. */
24597 pint2str (decode_mode_spec_buf, width, topline + nlines);
24598 return decode_mode_spec_buf;
24599 no_value:
24601 char *p = decode_mode_spec_buf;
24602 int pad = width - 2;
24603 while (pad-- > 0)
24604 *p++ = ' ';
24605 *p++ = '?';
24606 *p++ = '?';
24607 *p = '\0';
24608 return decode_mode_spec_buf;
24611 break;
24613 case 'm':
24614 obj = BVAR (b, mode_name);
24615 break;
24617 case 'n':
24618 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24619 return " Narrow";
24620 break;
24622 /* Display the "degree of travel" of the window through the buffer. */
24623 case 'o':
24625 ptrdiff_t toppos = marker_position (w->start);
24626 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24627 ptrdiff_t begv = BUF_BEGV (b);
24628 ptrdiff_t zv = BUF_ZV (b);
24630 if (zv <= botpos)
24631 return toppos <= begv ? "All" : "Bottom";
24632 else if (toppos <= begv)
24633 return "Top";
24634 else
24636 sprintf (decode_mode_spec_buf, "%2d%%",
24637 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24638 return decode_mode_spec_buf;
24642 /* Display percentage of buffer above the top of the screen. */
24643 case 'p':
24645 ptrdiff_t pos = marker_position (w->start);
24646 ptrdiff_t begv = BUF_BEGV (b);
24647 ptrdiff_t zv = BUF_ZV (b);
24649 if (w->window_end_pos <= BUF_Z (b) - zv)
24650 return pos <= begv ? "All" : "Bottom";
24651 else if (pos <= begv)
24652 return "Top";
24653 else
24655 sprintf (decode_mode_spec_buf, "%2d%%",
24656 percent99 (pos - begv, zv - begv));
24657 return decode_mode_spec_buf;
24661 /* Display percentage of size above the bottom of the screen. */
24662 case 'P':
24664 ptrdiff_t toppos = marker_position (w->start);
24665 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24666 ptrdiff_t begv = BUF_BEGV (b);
24667 ptrdiff_t zv = BUF_ZV (b);
24669 if (zv <= botpos)
24670 return toppos <= begv ? "All" : "Bottom";
24671 else
24673 sprintf (decode_mode_spec_buf,
24674 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24675 percent99 (botpos - begv, zv - begv));
24676 return decode_mode_spec_buf;
24680 /* Display percentage offsets of top and bottom of the window,
24681 using "All" (but not "Top" or "Bottom") where appropriate. */
24682 case 'q':
24684 ptrdiff_t toppos = marker_position (w->start);
24685 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24686 ptrdiff_t begv = BUF_BEGV (b);
24687 ptrdiff_t zv = BUF_ZV (b);
24688 int top_perc, bot_perc;
24690 if ((toppos <= begv) && (zv <= botpos))
24691 return "All ";
24693 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24694 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24696 if (top_perc == bot_perc)
24697 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24698 else
24699 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24701 return decode_mode_spec_buf;
24704 case 's':
24705 /* status of process */
24706 obj = Fget_buffer_process (Fcurrent_buffer ());
24707 if (NILP (obj))
24708 return "no process";
24709 #ifndef MSDOS
24710 obj = Fsymbol_name (Fprocess_status (obj));
24711 #endif
24712 break;
24714 case '@':
24716 ptrdiff_t count = inhibit_garbage_collection ();
24717 Lisp_Object curdir = BVAR (current_buffer, directory);
24718 Lisp_Object val = Qnil;
24720 if (STRINGP (curdir))
24721 val = call1 (intern ("file-remote-p"), curdir);
24723 unbind_to (count, Qnil);
24725 if (NILP (val))
24726 return "-";
24727 else
24728 return "@";
24731 case 'z':
24732 /* coding-system (not including end-of-line format) */
24733 case 'Z':
24734 /* coding-system (including end-of-line type) */
24736 bool eol_flag = (c == 'Z');
24737 char *p = decode_mode_spec_buf;
24739 if (! FRAME_WINDOW_P (f))
24741 /* No need to mention EOL here--the terminal never needs
24742 to do EOL conversion. */
24743 p = decode_mode_spec_coding (CODING_ID_NAME
24744 (FRAME_KEYBOARD_CODING (f)->id),
24745 p, false);
24746 p = decode_mode_spec_coding (CODING_ID_NAME
24747 (FRAME_TERMINAL_CODING (f)->id),
24748 p, false);
24750 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24751 p, eol_flag);
24753 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24754 #ifdef subprocesses
24755 obj = Fget_buffer_process (Fcurrent_buffer ());
24756 if (PROCESSP (obj))
24758 p = decode_mode_spec_coding
24759 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24760 p = decode_mode_spec_coding
24761 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24763 #endif /* subprocesses */
24764 #endif /* false */
24765 *p = 0;
24766 return decode_mode_spec_buf;
24770 if (STRINGP (obj))
24772 *string = obj;
24773 return SSDATA (obj);
24775 else
24776 return "";
24780 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24781 means count lines back from START_BYTE. But don't go beyond
24782 LIMIT_BYTE. Return the number of lines thus found (always
24783 nonnegative).
24785 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24786 either the position COUNT lines after/before START_BYTE, if we
24787 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24788 COUNT lines. */
24790 static ptrdiff_t
24791 display_count_lines (ptrdiff_t start_byte,
24792 ptrdiff_t limit_byte, ptrdiff_t count,
24793 ptrdiff_t *byte_pos_ptr)
24795 register unsigned char *cursor;
24796 unsigned char *base;
24798 register ptrdiff_t ceiling;
24799 register unsigned char *ceiling_addr;
24800 ptrdiff_t orig_count = count;
24802 /* If we are not in selective display mode,
24803 check only for newlines. */
24804 bool selective_display
24805 = (!NILP (BVAR (current_buffer, selective_display))
24806 && !INTEGERP (BVAR (current_buffer, selective_display)));
24808 if (count > 0)
24810 while (start_byte < limit_byte)
24812 ceiling = BUFFER_CEILING_OF (start_byte);
24813 ceiling = min (limit_byte - 1, ceiling);
24814 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24815 base = (cursor = BYTE_POS_ADDR (start_byte));
24819 if (selective_display)
24821 while (*cursor != '\n' && *cursor != 015
24822 && ++cursor != ceiling_addr)
24823 continue;
24824 if (cursor == ceiling_addr)
24825 break;
24827 else
24829 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24830 if (! cursor)
24831 break;
24834 cursor++;
24836 if (--count == 0)
24838 start_byte += cursor - base;
24839 *byte_pos_ptr = start_byte;
24840 return orig_count;
24843 while (cursor < ceiling_addr);
24845 start_byte += ceiling_addr - base;
24848 else
24850 while (start_byte > limit_byte)
24852 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24853 ceiling = max (limit_byte, ceiling);
24854 ceiling_addr = BYTE_POS_ADDR (ceiling);
24855 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24856 while (true)
24858 if (selective_display)
24860 while (--cursor >= ceiling_addr
24861 && *cursor != '\n' && *cursor != 015)
24862 continue;
24863 if (cursor < ceiling_addr)
24864 break;
24866 else
24868 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24869 if (! cursor)
24870 break;
24873 if (++count == 0)
24875 start_byte += cursor - base + 1;
24876 *byte_pos_ptr = start_byte;
24877 /* When scanning backwards, we should
24878 not count the newline posterior to which we stop. */
24879 return - orig_count - 1;
24882 start_byte += ceiling_addr - base;
24886 *byte_pos_ptr = limit_byte;
24888 if (count < 0)
24889 return - orig_count + count;
24890 return orig_count - count;
24896 /***********************************************************************
24897 Displaying strings
24898 ***********************************************************************/
24900 /* Display a NUL-terminated string, starting with index START.
24902 If STRING is non-null, display that C string. Otherwise, the Lisp
24903 string LISP_STRING is displayed. There's a case that STRING is
24904 non-null and LISP_STRING is not nil. It means STRING is a string
24905 data of LISP_STRING. In that case, we display LISP_STRING while
24906 ignoring its text properties.
24908 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24909 FACE_STRING. Display STRING or LISP_STRING with the face at
24910 FACE_STRING_POS in FACE_STRING:
24912 Display the string in the environment given by IT, but use the
24913 standard display table, temporarily.
24915 FIELD_WIDTH is the minimum number of output glyphs to produce.
24916 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24917 with spaces. If STRING has more characters, more than FIELD_WIDTH
24918 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24920 PRECISION is the maximum number of characters to output from
24921 STRING. PRECISION < 0 means don't truncate the string.
24923 This is roughly equivalent to printf format specifiers:
24925 FIELD_WIDTH PRECISION PRINTF
24926 ----------------------------------------
24927 -1 -1 %s
24928 -1 10 %.10s
24929 10 -1 %10s
24930 20 10 %20.10s
24932 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24933 display them, and < 0 means obey the current buffer's value of
24934 enable_multibyte_characters.
24936 Value is the number of columns displayed. */
24938 static int
24939 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24940 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24941 int field_width, int precision, int max_x, int multibyte)
24943 int hpos_at_start = it->hpos;
24944 int saved_face_id = it->face_id;
24945 struct glyph_row *row = it->glyph_row;
24946 ptrdiff_t it_charpos;
24948 /* Initialize the iterator IT for iteration over STRING beginning
24949 with index START. */
24950 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24951 precision, field_width, multibyte);
24952 if (string && STRINGP (lisp_string))
24953 /* LISP_STRING is the one returned by decode_mode_spec. We should
24954 ignore its text properties. */
24955 it->stop_charpos = it->end_charpos;
24957 /* If displaying STRING, set up the face of the iterator from
24958 FACE_STRING, if that's given. */
24959 if (STRINGP (face_string))
24961 ptrdiff_t endptr;
24962 struct face *face;
24964 it->face_id
24965 = face_at_string_position (it->w, face_string, face_string_pos,
24966 0, &endptr, it->base_face_id, false);
24967 face = FACE_FROM_ID (it->f, it->face_id);
24968 it->face_box_p = face->box != FACE_NO_BOX;
24971 /* Set max_x to the maximum allowed X position. Don't let it go
24972 beyond the right edge of the window. */
24973 if (max_x <= 0)
24974 max_x = it->last_visible_x;
24975 else
24976 max_x = min (max_x, it->last_visible_x);
24978 /* Skip over display elements that are not visible. because IT->w is
24979 hscrolled. */
24980 if (it->current_x < it->first_visible_x)
24981 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24982 MOVE_TO_POS | MOVE_TO_X);
24984 row->ascent = it->max_ascent;
24985 row->height = it->max_ascent + it->max_descent;
24986 row->phys_ascent = it->max_phys_ascent;
24987 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24988 row->extra_line_spacing = it->max_extra_line_spacing;
24990 if (STRINGP (it->string))
24991 it_charpos = IT_STRING_CHARPOS (*it);
24992 else
24993 it_charpos = IT_CHARPOS (*it);
24995 /* This condition is for the case that we are called with current_x
24996 past last_visible_x. */
24997 while (it->current_x < max_x)
24999 int x_before, x, n_glyphs_before, i, nglyphs;
25001 /* Get the next display element. */
25002 if (!get_next_display_element (it))
25003 break;
25005 /* Produce glyphs. */
25006 x_before = it->current_x;
25007 n_glyphs_before = row->used[TEXT_AREA];
25008 PRODUCE_GLYPHS (it);
25010 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
25011 i = 0;
25012 x = x_before;
25013 while (i < nglyphs)
25015 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
25017 if (it->line_wrap != TRUNCATE
25018 && x + glyph->pixel_width > max_x)
25020 /* End of continued line or max_x reached. */
25021 if (CHAR_GLYPH_PADDING_P (*glyph))
25023 /* A wide character is unbreakable. */
25024 if (row->reversed_p)
25025 unproduce_glyphs (it, row->used[TEXT_AREA]
25026 - n_glyphs_before);
25027 row->used[TEXT_AREA] = n_glyphs_before;
25028 it->current_x = x_before;
25030 else
25032 if (row->reversed_p)
25033 unproduce_glyphs (it, row->used[TEXT_AREA]
25034 - (n_glyphs_before + i));
25035 row->used[TEXT_AREA] = n_glyphs_before + i;
25036 it->current_x = x;
25038 break;
25040 else if (x + glyph->pixel_width >= it->first_visible_x)
25042 /* Glyph is at least partially visible. */
25043 ++it->hpos;
25044 if (x < it->first_visible_x)
25045 row->x = x - it->first_visible_x;
25047 else
25049 /* Glyph is off the left margin of the display area.
25050 Should not happen. */
25051 emacs_abort ();
25054 row->ascent = max (row->ascent, it->max_ascent);
25055 row->height = max (row->height, it->max_ascent + it->max_descent);
25056 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
25057 row->phys_height = max (row->phys_height,
25058 it->max_phys_ascent + it->max_phys_descent);
25059 row->extra_line_spacing = max (row->extra_line_spacing,
25060 it->max_extra_line_spacing);
25061 x += glyph->pixel_width;
25062 ++i;
25065 /* Stop if max_x reached. */
25066 if (i < nglyphs)
25067 break;
25069 /* Stop at line ends. */
25070 if (ITERATOR_AT_END_OF_LINE_P (it))
25072 it->continuation_lines_width = 0;
25073 break;
25076 set_iterator_to_next (it, true);
25077 if (STRINGP (it->string))
25078 it_charpos = IT_STRING_CHARPOS (*it);
25079 else
25080 it_charpos = IT_CHARPOS (*it);
25082 /* Stop if truncating at the right edge. */
25083 if (it->line_wrap == TRUNCATE
25084 && it->current_x >= it->last_visible_x)
25086 /* Add truncation mark, but don't do it if the line is
25087 truncated at a padding space. */
25088 if (it_charpos < it->string_nchars)
25090 if (!FRAME_WINDOW_P (it->f))
25092 int ii, n;
25094 if (it->current_x > it->last_visible_x)
25096 if (!row->reversed_p)
25098 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25099 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25100 break;
25102 else
25104 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25105 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25106 break;
25107 unproduce_glyphs (it, ii + 1);
25108 ii = row->used[TEXT_AREA] - (ii + 1);
25110 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25112 row->used[TEXT_AREA] = ii;
25113 produce_special_glyphs (it, IT_TRUNCATION);
25116 produce_special_glyphs (it, IT_TRUNCATION);
25118 row->truncated_on_right_p = true;
25120 break;
25124 /* Maybe insert a truncation at the left. */
25125 if (it->first_visible_x
25126 && it_charpos > 0)
25128 if (!FRAME_WINDOW_P (it->f)
25129 || (row->reversed_p
25130 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25131 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25132 insert_left_trunc_glyphs (it);
25133 row->truncated_on_left_p = true;
25136 it->face_id = saved_face_id;
25138 /* Value is number of columns displayed. */
25139 return it->hpos - hpos_at_start;
25144 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25145 appears as an element of LIST or as the car of an element of LIST.
25146 If PROPVAL is a list, compare each element against LIST in that
25147 way, and return 1/2 if any element of PROPVAL is found in LIST.
25148 Otherwise return 0. This function cannot quit.
25149 The return value is 2 if the text is invisible but with an ellipsis
25150 and 1 if it's invisible and without an ellipsis. */
25153 invisible_prop (Lisp_Object propval, Lisp_Object list)
25155 Lisp_Object tail, proptail;
25157 for (tail = list; CONSP (tail); tail = XCDR (tail))
25159 register Lisp_Object tem;
25160 tem = XCAR (tail);
25161 if (EQ (propval, tem))
25162 return 1;
25163 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25164 return NILP (XCDR (tem)) ? 1 : 2;
25167 if (CONSP (propval))
25169 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25171 Lisp_Object propelt;
25172 propelt = XCAR (proptail);
25173 for (tail = list; CONSP (tail); tail = XCDR (tail))
25175 register Lisp_Object tem;
25176 tem = XCAR (tail);
25177 if (EQ (propelt, tem))
25178 return 1;
25179 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25180 return NILP (XCDR (tem)) ? 1 : 2;
25185 return 0;
25188 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25189 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25190 POS should be a marker or a buffer position; the value of the `invisible'
25191 property at that position in the current buffer is examined.
25192 POS can also be the actual value of the `invisible' text or overlay
25193 property of the text of interest, in which case the value itself is
25194 examined.
25196 The non-nil value returned can be t for currently invisible text that is
25197 entirely hidden on display, or some other non-nil, non-t value if the
25198 text is replaced by an ellipsis.
25200 Note that whether text with `invisible' property is actually hidden on
25201 display may depend on `buffer-invisibility-spec', which see. */)
25202 (Lisp_Object pos)
25204 Lisp_Object prop
25205 = (NATNUMP (pos) || MARKERP (pos)
25206 ? Fget_char_property (pos, Qinvisible, Qnil)
25207 : pos);
25208 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25209 return (invis == 0 ? Qnil
25210 : invis == 1 ? Qt
25211 : make_number (invis));
25214 /* Calculate a width or height in pixels from a specification using
25215 the following elements:
25217 SPEC ::=
25218 NUM - a (fractional) multiple of the default font width/height
25219 (NUM) - specifies exactly NUM pixels
25220 UNIT - a fixed number of pixels, see below.
25221 ELEMENT - size of a display element in pixels, see below.
25222 (NUM . SPEC) - equals NUM * SPEC
25223 (+ SPEC SPEC ...) - add pixel values
25224 (- SPEC SPEC ...) - subtract pixel values
25225 (- SPEC) - negate pixel value
25227 NUM ::=
25228 INT or FLOAT - a number constant
25229 SYMBOL - use symbol's (buffer local) variable binding.
25231 UNIT ::=
25232 in - pixels per inch *)
25233 mm - pixels per 1/1000 meter *)
25234 cm - pixels per 1/100 meter *)
25235 width - width of current font in pixels.
25236 height - height of current font in pixels.
25238 *) using the ratio(s) defined in display-pixels-per-inch.
25240 ELEMENT ::=
25242 left-fringe - left fringe width in pixels
25243 right-fringe - right fringe width in pixels
25245 left-margin - left margin width in pixels
25246 right-margin - right margin width in pixels
25248 scroll-bar - scroll-bar area width in pixels
25250 Examples:
25252 Pixels corresponding to 5 inches:
25253 (5 . in)
25255 Total width of non-text areas on left side of window (if scroll-bar is on left):
25256 '(space :width (+ left-fringe left-margin scroll-bar))
25258 Align to first text column (in header line):
25259 '(space :align-to 0)
25261 Align to middle of text area minus half the width of variable `my-image'
25262 containing a loaded image:
25263 '(space :align-to (0.5 . (- text my-image)))
25265 Width of left margin minus width of 1 character in the default font:
25266 '(space :width (- left-margin 1))
25268 Width of left margin minus width of 2 characters in the current font:
25269 '(space :width (- left-margin (2 . width)))
25271 Center 1 character over left-margin (in header line):
25272 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25274 Different ways to express width of left fringe plus left margin minus one pixel:
25275 '(space :width (- (+ left-fringe left-margin) (1)))
25276 '(space :width (+ left-fringe left-margin (- (1))))
25277 '(space :width (+ left-fringe left-margin (-1)))
25279 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25280 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25281 coordinate, and *RES is the additional pixel width from that point
25282 till the end of the stretch glyph.
25284 WIDTH_P non-zero means take the width dimension or X coordinate of
25285 the object specified by PROP, WIDTH_P zero means take the height
25286 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25287 non-NULL, WIDTH_P should be non-zero.)
25289 FONT is the font of the face of the surrounding text.
25291 The return value is non-zero if width or height were successfully
25292 calculated, i.e. if PROP is a valid spec. */
25294 static bool
25295 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25296 struct font *font, bool width_p, int *align_to)
25298 double pixels;
25300 # define OK_PIXELS(val) (*res = (val), true)
25301 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25303 if (NILP (prop))
25304 return OK_PIXELS (0);
25306 eassert (FRAME_LIVE_P (it->f));
25308 if (SYMBOLP (prop))
25310 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25312 char *unit = SSDATA (SYMBOL_NAME (prop));
25314 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25315 if (unit[0] == 'i' && unit[1] == 'n')
25316 pixels = 1.0;
25317 else if (unit[0] == 'm' && unit[1] == 'm')
25318 pixels = 25.4;
25319 else if (unit[0] == 'c' && unit[1] == 'm')
25320 pixels = 2.54;
25321 else
25322 pixels = 0;
25323 if (pixels > 0)
25325 double ppi = (width_p ? FRAME_RES_X (it->f)
25326 : FRAME_RES_Y (it->f));
25328 if (ppi > 0)
25329 return OK_PIXELS (ppi / pixels);
25330 return false;
25334 #ifdef HAVE_WINDOW_SYSTEM
25335 /* 'height': the height of FONT. */
25336 if (EQ (prop, Qheight))
25337 return OK_PIXELS (font
25338 ? normal_char_height (font, -1)
25339 : FRAME_LINE_HEIGHT (it->f));
25340 /* 'width': the width of FONT. */
25341 if (EQ (prop, Qwidth))
25342 return OK_PIXELS (font
25343 ? FONT_WIDTH (font)
25344 : FRAME_COLUMN_WIDTH (it->f));
25345 #else
25346 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25347 return OK_PIXELS (1);
25348 #endif
25350 /* 'text': the width or height of the text area. */
25351 if (EQ (prop, Qtext))
25352 return OK_PIXELS (width_p
25353 ? (window_box_width (it->w, TEXT_AREA)
25354 - it->lnum_pixel_width)
25355 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25357 /* ':align_to'. First time we compute the value, window
25358 elements are interpreted as the position of the element's
25359 left edge. */
25360 if (align_to && *align_to < 0)
25362 *res = 0;
25363 /* 'left': left edge of the text area. */
25364 if (EQ (prop, Qleft))
25365 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25366 + it->lnum_pixel_width);
25367 /* 'right': right edge of the text area. */
25368 if (EQ (prop, Qright))
25369 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25370 /* 'center': the center of the text area. */
25371 if (EQ (prop, Qcenter))
25372 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25373 + it->lnum_pixel_width
25374 + window_box_width (it->w, TEXT_AREA) / 2);
25375 /* 'left-fringe': left edge of the left fringe. */
25376 if (EQ (prop, Qleft_fringe))
25377 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25378 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25379 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25380 /* 'right-fringe': left edge of the right fringe. */
25381 if (EQ (prop, Qright_fringe))
25382 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25383 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25384 : window_box_right_offset (it->w, TEXT_AREA));
25385 /* 'left-margin': left edge of the left display margin. */
25386 if (EQ (prop, Qleft_margin))
25387 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25388 /* 'right-margin': left edge of the right display margin. */
25389 if (EQ (prop, Qright_margin))
25390 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25391 /* 'scroll-bar': left edge of the vertical scroll bar. */
25392 if (EQ (prop, Qscroll_bar))
25393 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25395 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25396 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25397 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25398 : 0)));
25400 else
25402 /* Otherwise, the elements stand for their width. */
25403 if (EQ (prop, Qleft_fringe))
25404 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25405 if (EQ (prop, Qright_fringe))
25406 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25407 if (EQ (prop, Qleft_margin))
25408 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25409 if (EQ (prop, Qright_margin))
25410 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25411 if (EQ (prop, Qscroll_bar))
25412 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25415 prop = buffer_local_value (prop, it->w->contents);
25416 if (EQ (prop, Qunbound))
25417 prop = Qnil;
25420 if (NUMBERP (prop))
25422 int base_unit = (width_p
25423 ? FRAME_COLUMN_WIDTH (it->f)
25424 : FRAME_LINE_HEIGHT (it->f));
25425 if (width_p && align_to && *align_to < 0)
25426 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25427 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25430 if (CONSP (prop))
25432 Lisp_Object car = XCAR (prop);
25433 Lisp_Object cdr = XCDR (prop);
25435 if (SYMBOLP (car))
25437 #ifdef HAVE_WINDOW_SYSTEM
25438 /* '(image PROPS...)': width or height of the specified image. */
25439 if (FRAME_WINDOW_P (it->f)
25440 && valid_image_p (prop))
25442 ptrdiff_t id = lookup_image (it->f, prop);
25443 struct image *img = IMAGE_FROM_ID (it->f, id);
25445 return OK_PIXELS (width_p ? img->width : img->height);
25447 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25448 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25450 /* TODO: Don't return dummy size. */
25451 return OK_PIXELS (100);
25453 #endif
25454 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25455 recursively calculated values. */
25456 if (EQ (car, Qplus) || EQ (car, Qminus))
25458 bool first = true;
25459 double px;
25461 pixels = 0;
25462 while (CONSP (cdr))
25464 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25465 font, width_p, align_to))
25466 return false;
25467 if (first)
25468 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25469 else
25470 pixels += px;
25471 cdr = XCDR (cdr);
25473 if (EQ (car, Qminus))
25474 pixels = -pixels;
25475 return OK_PIXELS (pixels);
25478 car = buffer_local_value (car, it->w->contents);
25479 if (EQ (car, Qunbound))
25480 car = Qnil;
25483 /* '(NUM)': absolute number of pixels. */
25484 if (NUMBERP (car))
25486 double fact;
25487 int offset =
25488 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25489 pixels = XFLOATINT (car);
25490 if (NILP (cdr))
25491 return OK_PIXELS (pixels + offset);
25492 if (calc_pixel_width_or_height (&fact, it, cdr,
25493 font, width_p, align_to))
25494 return OK_PIXELS (pixels * fact + offset);
25495 return false;
25498 return false;
25501 return false;
25504 void
25505 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25507 #ifdef HAVE_WINDOW_SYSTEM
25508 normal_char_ascent_descent (font, -1, ascent, descent);
25509 #else
25510 *ascent = 1;
25511 *descent = 0;
25512 #endif
25516 /***********************************************************************
25517 Glyph Display
25518 ***********************************************************************/
25520 #ifdef HAVE_WINDOW_SYSTEM
25522 #ifdef GLYPH_DEBUG
25524 void
25525 dump_glyph_string (struct glyph_string *s)
25527 fprintf (stderr, "glyph string\n");
25528 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25529 s->x, s->y, s->width, s->height);
25530 fprintf (stderr, " ybase = %d\n", s->ybase);
25531 fprintf (stderr, " hl = %u\n", s->hl);
25532 fprintf (stderr, " left overhang = %d, right = %d\n",
25533 s->left_overhang, s->right_overhang);
25534 fprintf (stderr, " nchars = %d\n", s->nchars);
25535 fprintf (stderr, " extends to end of line = %d\n",
25536 s->extends_to_end_of_line_p);
25537 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25538 fprintf (stderr, " bg width = %d\n", s->background_width);
25541 #endif /* GLYPH_DEBUG */
25543 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25544 of XChar2b structures for S; it can't be allocated in
25545 init_glyph_string because it must be allocated via `alloca'. W
25546 is the window on which S is drawn. ROW and AREA are the glyph row
25547 and area within the row from which S is constructed. START is the
25548 index of the first glyph structure covered by S. HL is a
25549 face-override for drawing S. */
25551 #ifdef HAVE_NTGUI
25552 #define OPTIONAL_HDC(hdc) HDC hdc,
25553 #define DECLARE_HDC(hdc) HDC hdc;
25554 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25555 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25556 #endif
25558 #ifndef OPTIONAL_HDC
25559 #define OPTIONAL_HDC(hdc)
25560 #define DECLARE_HDC(hdc)
25561 #define ALLOCATE_HDC(hdc, f)
25562 #define RELEASE_HDC(hdc, f)
25563 #endif
25565 static void
25566 init_glyph_string (struct glyph_string *s,
25567 OPTIONAL_HDC (hdc)
25568 XChar2b *char2b, struct window *w, struct glyph_row *row,
25569 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25571 memset (s, 0, sizeof *s);
25572 s->w = w;
25573 s->f = XFRAME (w->frame);
25574 #ifdef HAVE_NTGUI
25575 s->hdc = hdc;
25576 #endif
25577 s->display = FRAME_X_DISPLAY (s->f);
25578 s->char2b = char2b;
25579 s->hl = hl;
25580 s->row = row;
25581 s->area = area;
25582 s->first_glyph = row->glyphs[area] + start;
25583 s->height = row->height;
25584 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25585 s->ybase = s->y + row->ascent;
25589 /* Append the list of glyph strings with head H and tail T to the list
25590 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25592 static void
25593 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25594 struct glyph_string *h, struct glyph_string *t)
25596 if (h)
25598 if (*head)
25599 (*tail)->next = h;
25600 else
25601 *head = h;
25602 h->prev = *tail;
25603 *tail = t;
25608 /* Prepend the list of glyph strings with head H and tail T to the
25609 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25610 result. */
25612 static void
25613 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25614 struct glyph_string *h, struct glyph_string *t)
25616 if (h)
25618 if (*head)
25619 (*head)->prev = t;
25620 else
25621 *tail = t;
25622 t->next = *head;
25623 *head = h;
25628 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25629 Set *HEAD and *TAIL to the resulting list. */
25631 static void
25632 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25633 struct glyph_string *s)
25635 s->next = s->prev = NULL;
25636 append_glyph_string_lists (head, tail, s, s);
25640 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25641 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25642 make sure that X resources for the face returned are allocated.
25643 Value is a pointer to a realized face that is ready for display if
25644 DISPLAY_P. */
25646 static struct face *
25647 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25648 XChar2b *char2b, bool display_p)
25650 struct face *face = FACE_FROM_ID (f, face_id);
25651 unsigned code = 0;
25653 if (face->font)
25655 code = face->font->driver->encode_char (face->font, c);
25657 if (code == FONT_INVALID_CODE)
25658 code = 0;
25660 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25662 /* Make sure X resources of the face are allocated. */
25663 #ifdef HAVE_X_WINDOWS
25664 if (display_p)
25665 #endif
25667 eassert (face != NULL);
25668 prepare_face_for_display (f, face);
25671 return face;
25675 /* Get face and two-byte form of character glyph GLYPH on frame F.
25676 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25677 a pointer to a realized face that is ready for display. */
25679 static struct face *
25680 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25681 XChar2b *char2b)
25683 struct face *face;
25684 unsigned code = 0;
25686 eassert (glyph->type == CHAR_GLYPH);
25687 face = FACE_FROM_ID (f, glyph->face_id);
25689 /* Make sure X resources of the face are allocated. */
25690 prepare_face_for_display (f, face);
25692 if (face->font)
25694 if (CHAR_BYTE8_P (glyph->u.ch))
25695 code = CHAR_TO_BYTE8 (glyph->u.ch);
25696 else
25697 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25699 if (code == FONT_INVALID_CODE)
25700 code = 0;
25703 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25704 return face;
25708 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25709 Return true iff FONT has a glyph for C. */
25711 static bool
25712 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25714 unsigned code;
25716 if (CHAR_BYTE8_P (c))
25717 code = CHAR_TO_BYTE8 (c);
25718 else
25719 code = font->driver->encode_char (font, c);
25721 if (code == FONT_INVALID_CODE)
25722 return false;
25723 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25724 return true;
25728 /* Fill glyph string S with composition components specified by S->cmp.
25730 BASE_FACE is the base face of the composition.
25731 S->cmp_from is the index of the first component for S.
25733 OVERLAPS non-zero means S should draw the foreground only, and use
25734 its physical height for clipping. See also draw_glyphs.
25736 Value is the index of a component not in S. */
25738 static int
25739 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25740 int overlaps)
25742 int i;
25743 /* For all glyphs of this composition, starting at the offset
25744 S->cmp_from, until we reach the end of the definition or encounter a
25745 glyph that requires the different face, add it to S. */
25746 struct face *face;
25748 eassert (s);
25750 s->for_overlaps = overlaps;
25751 s->face = NULL;
25752 s->font = NULL;
25753 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25755 int c = COMPOSITION_GLYPH (s->cmp, i);
25757 /* TAB in a composition means display glyphs with padding space
25758 on the left or right. */
25759 if (c != '\t')
25761 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25762 -1, Qnil);
25764 face = get_char_face_and_encoding (s->f, c, face_id,
25765 s->char2b + i, true);
25766 if (face)
25768 if (! s->face)
25770 s->face = face;
25771 s->font = s->face->font;
25773 else if (s->face != face)
25774 break;
25777 ++s->nchars;
25779 s->cmp_to = i;
25781 if (s->face == NULL)
25783 s->face = base_face->ascii_face;
25784 s->font = s->face->font;
25787 /* All glyph strings for the same composition has the same width,
25788 i.e. the width set for the first component of the composition. */
25789 s->width = s->first_glyph->pixel_width;
25791 /* If the specified font could not be loaded, use the frame's
25792 default font, but record the fact that we couldn't load it in
25793 the glyph string so that we can draw rectangles for the
25794 characters of the glyph string. */
25795 if (s->font == NULL)
25797 s->font_not_found_p = true;
25798 s->font = FRAME_FONT (s->f);
25801 /* Adjust base line for subscript/superscript text. */
25802 s->ybase += s->first_glyph->voffset;
25804 return s->cmp_to;
25807 static int
25808 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25809 int start, int end, int overlaps)
25811 struct glyph *glyph, *last;
25812 Lisp_Object lgstring;
25813 int i;
25815 s->for_overlaps = overlaps;
25816 glyph = s->row->glyphs[s->area] + start;
25817 last = s->row->glyphs[s->area] + end;
25818 s->cmp_id = glyph->u.cmp.id;
25819 s->cmp_from = glyph->slice.cmp.from;
25820 s->cmp_to = glyph->slice.cmp.to + 1;
25821 s->face = FACE_FROM_ID (s->f, face_id);
25822 lgstring = composition_gstring_from_id (s->cmp_id);
25823 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25824 glyph++;
25825 while (glyph < last
25826 && glyph->u.cmp.automatic
25827 && glyph->u.cmp.id == s->cmp_id
25828 && s->cmp_to == glyph->slice.cmp.from)
25829 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25831 for (i = s->cmp_from; i < s->cmp_to; i++)
25833 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25834 unsigned code = LGLYPH_CODE (lglyph);
25836 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25838 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25839 return glyph - s->row->glyphs[s->area];
25843 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25844 See the comment of fill_glyph_string for arguments.
25845 Value is the index of the first glyph not in S. */
25848 static int
25849 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25850 int start, int end, int overlaps)
25852 struct glyph *glyph, *last;
25853 int voffset;
25855 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25856 s->for_overlaps = overlaps;
25857 glyph = s->row->glyphs[s->area] + start;
25858 last = s->row->glyphs[s->area] + end;
25859 voffset = glyph->voffset;
25860 s->face = FACE_FROM_ID (s->f, face_id);
25861 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25862 s->nchars = 1;
25863 s->width = glyph->pixel_width;
25864 glyph++;
25865 while (glyph < last
25866 && glyph->type == GLYPHLESS_GLYPH
25867 && glyph->voffset == voffset
25868 && glyph->face_id == face_id)
25870 s->nchars++;
25871 s->width += glyph->pixel_width;
25872 glyph++;
25874 s->ybase += voffset;
25875 return glyph - s->row->glyphs[s->area];
25879 /* Fill glyph string S from a sequence of character glyphs.
25881 FACE_ID is the face id of the string. START is the index of the
25882 first glyph to consider, END is the index of the last + 1.
25883 OVERLAPS non-zero means S should draw the foreground only, and use
25884 its physical height for clipping. See also draw_glyphs.
25886 Value is the index of the first glyph not in S. */
25888 static int
25889 fill_glyph_string (struct glyph_string *s, int face_id,
25890 int start, int end, int overlaps)
25892 struct glyph *glyph, *last;
25893 int voffset;
25894 bool glyph_not_available_p;
25896 eassert (s->f == XFRAME (s->w->frame));
25897 eassert (s->nchars == 0);
25898 eassert (start >= 0 && end > start);
25900 s->for_overlaps = overlaps;
25901 glyph = s->row->glyphs[s->area] + start;
25902 last = s->row->glyphs[s->area] + end;
25903 voffset = glyph->voffset;
25904 s->padding_p = glyph->padding_p;
25905 glyph_not_available_p = glyph->glyph_not_available_p;
25907 while (glyph < last
25908 && glyph->type == CHAR_GLYPH
25909 && glyph->voffset == voffset
25910 /* Same face id implies same font, nowadays. */
25911 && glyph->face_id == face_id
25912 && glyph->glyph_not_available_p == glyph_not_available_p)
25914 s->face = get_glyph_face_and_encoding (s->f, glyph,
25915 s->char2b + s->nchars);
25916 ++s->nchars;
25917 eassert (s->nchars <= end - start);
25918 s->width += glyph->pixel_width;
25919 if (glyph++->padding_p != s->padding_p)
25920 break;
25923 s->font = s->face->font;
25925 /* If the specified font could not be loaded, use the frame's font,
25926 but record the fact that we couldn't load it in
25927 S->font_not_found_p so that we can draw rectangles for the
25928 characters of the glyph string. */
25929 if (s->font == NULL || glyph_not_available_p)
25931 s->font_not_found_p = true;
25932 s->font = FRAME_FONT (s->f);
25935 /* Adjust base line for subscript/superscript text. */
25936 s->ybase += voffset;
25938 eassert (s->face && s->face->gc);
25939 return glyph - s->row->glyphs[s->area];
25943 /* Fill glyph string S from image glyph S->first_glyph. */
25945 static void
25946 fill_image_glyph_string (struct glyph_string *s)
25948 eassert (s->first_glyph->type == IMAGE_GLYPH);
25949 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25950 eassert (s->img);
25951 s->slice = s->first_glyph->slice.img;
25952 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25953 s->font = s->face->font;
25954 s->width = s->first_glyph->pixel_width;
25956 /* Adjust base line for subscript/superscript text. */
25957 s->ybase += s->first_glyph->voffset;
25961 #ifdef HAVE_XWIDGETS
25962 static void
25963 fill_xwidget_glyph_string (struct glyph_string *s)
25965 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25966 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25967 s->font = s->face->font;
25968 s->width = s->first_glyph->pixel_width;
25969 s->ybase += s->first_glyph->voffset;
25970 s->xwidget = s->first_glyph->u.xwidget;
25972 #endif
25973 /* Fill glyph string S from a sequence of stretch glyphs.
25975 START is the index of the first glyph to consider,
25976 END is the index of the last + 1.
25978 Value is the index of the first glyph not in S. */
25980 static int
25981 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25983 struct glyph *glyph, *last;
25984 int voffset, face_id;
25986 eassert (s->first_glyph->type == STRETCH_GLYPH);
25988 glyph = s->row->glyphs[s->area] + start;
25989 last = s->row->glyphs[s->area] + end;
25990 face_id = glyph->face_id;
25991 s->face = FACE_FROM_ID (s->f, face_id);
25992 s->font = s->face->font;
25993 s->width = glyph->pixel_width;
25994 s->nchars = 1;
25995 voffset = glyph->voffset;
25997 for (++glyph;
25998 (glyph < last
25999 && glyph->type == STRETCH_GLYPH
26000 && glyph->voffset == voffset
26001 && glyph->face_id == face_id);
26002 ++glyph)
26003 s->width += glyph->pixel_width;
26005 /* Adjust base line for subscript/superscript text. */
26006 s->ybase += voffset;
26008 /* The case that face->gc == 0 is handled when drawing the glyph
26009 string by calling prepare_face_for_display. */
26010 eassert (s->face);
26011 return glyph - s->row->glyphs[s->area];
26014 static struct font_metrics *
26015 get_per_char_metric (struct font *font, XChar2b *char2b)
26017 static struct font_metrics metrics;
26018 unsigned code;
26020 if (! font)
26021 return NULL;
26022 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
26023 if (code == FONT_INVALID_CODE)
26024 return NULL;
26025 font->driver->text_extents (font, &code, 1, &metrics);
26026 return &metrics;
26029 /* A subroutine that computes "normal" values of ASCENT and DESCENT
26030 for FONT. Values are taken from font-global ones, except for fonts
26031 that claim preposterously large values, but whose glyphs actually
26032 have reasonable dimensions. C is the character to use for metrics
26033 if the font-global values are too large; if C is negative, the
26034 function selects a default character. */
26035 static void
26036 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
26038 *ascent = FONT_BASE (font);
26039 *descent = FONT_DESCENT (font);
26041 if (FONT_TOO_HIGH (font))
26043 XChar2b char2b;
26045 /* Get metrics of C, defaulting to a reasonably sized ASCII
26046 character. */
26047 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
26049 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
26051 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
26053 /* We add 1 pixel to character dimensions as heuristics
26054 that produces nicer display, e.g. when the face has
26055 the box attribute. */
26056 *ascent = pcm->ascent + 1;
26057 *descent = pcm->descent + 1;
26063 /* A subroutine that computes a reasonable "normal character height"
26064 for fonts that claim preposterously large vertical dimensions, but
26065 whose glyphs are actually reasonably sized. C is the character
26066 whose metrics to use for those fonts, or -1 for default
26067 character. */
26068 static int
26069 normal_char_height (struct font *font, int c)
26071 int ascent, descent;
26073 normal_char_ascent_descent (font, c, &ascent, &descent);
26075 return ascent + descent;
26078 /* EXPORT for RIF:
26079 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
26080 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
26081 assumed to be zero. */
26083 void
26084 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
26086 *left = *right = 0;
26088 if (glyph->type == CHAR_GLYPH)
26090 XChar2b char2b;
26091 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26092 if (face->font)
26094 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26095 if (pcm)
26097 if (pcm->rbearing > pcm->width)
26098 *right = pcm->rbearing - pcm->width;
26099 if (pcm->lbearing < 0)
26100 *left = -pcm->lbearing;
26104 else if (glyph->type == COMPOSITE_GLYPH)
26106 if (! glyph->u.cmp.automatic)
26108 struct composition *cmp = composition_table[glyph->u.cmp.id];
26110 if (cmp->rbearing > cmp->pixel_width)
26111 *right = cmp->rbearing - cmp->pixel_width;
26112 if (cmp->lbearing < 0)
26113 *left = - cmp->lbearing;
26115 else
26117 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26118 struct font_metrics metrics;
26120 composition_gstring_width (gstring, glyph->slice.cmp.from,
26121 glyph->slice.cmp.to + 1, &metrics);
26122 if (metrics.rbearing > metrics.width)
26123 *right = metrics.rbearing - metrics.width;
26124 if (metrics.lbearing < 0)
26125 *left = - metrics.lbearing;
26131 /* Return the index of the first glyph preceding glyph string S that
26132 is overwritten by S because of S's left overhang. Value is -1
26133 if no glyphs are overwritten. */
26135 static int
26136 left_overwritten (struct glyph_string *s)
26138 int k;
26140 if (s->left_overhang)
26142 int x = 0, i;
26143 struct glyph *glyphs = s->row->glyphs[s->area];
26144 int first = s->first_glyph - glyphs;
26146 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26147 x -= glyphs[i].pixel_width;
26149 k = i + 1;
26151 else
26152 k = -1;
26154 return k;
26158 /* Return the index of the first glyph preceding glyph string S that
26159 is overwriting S because of its right overhang. Value is -1 if no
26160 glyph in front of S overwrites S. */
26162 static int
26163 left_overwriting (struct glyph_string *s)
26165 int i, k, x;
26166 struct glyph *glyphs = s->row->glyphs[s->area];
26167 int first = s->first_glyph - glyphs;
26169 k = -1;
26170 x = 0;
26171 for (i = first - 1; i >= 0; --i)
26173 int left, right;
26174 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26175 if (x + right > 0)
26176 k = i;
26177 x -= glyphs[i].pixel_width;
26180 return k;
26184 /* Return the index of the last glyph following glyph string S that is
26185 overwritten by S because of S's right overhang. Value is -1 if
26186 no such glyph is found. */
26188 static int
26189 right_overwritten (struct glyph_string *s)
26191 int k = -1;
26193 if (s->right_overhang)
26195 int x = 0, i;
26196 struct glyph *glyphs = s->row->glyphs[s->area];
26197 int first = (s->first_glyph - glyphs
26198 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26199 int end = s->row->used[s->area];
26201 for (i = first; i < end && s->right_overhang > x; ++i)
26202 x += glyphs[i].pixel_width;
26204 k = i;
26207 return k;
26211 /* Return the index of the last glyph following glyph string S that
26212 overwrites S because of its left overhang. Value is negative
26213 if no such glyph is found. */
26215 static int
26216 right_overwriting (struct glyph_string *s)
26218 int i, k, x;
26219 int end = s->row->used[s->area];
26220 struct glyph *glyphs = s->row->glyphs[s->area];
26221 int first = (s->first_glyph - glyphs
26222 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26224 k = -1;
26225 x = 0;
26226 for (i = first; i < end; ++i)
26228 int left, right;
26229 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26230 if (x - left < 0)
26231 k = i;
26232 x += glyphs[i].pixel_width;
26235 return k;
26239 /* Set background width of glyph string S. START is the index of the
26240 first glyph following S. LAST_X is the right-most x-position + 1
26241 in the drawing area. */
26243 static void
26244 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26246 /* If the face of this glyph string has to be drawn to the end of
26247 the drawing area, set S->extends_to_end_of_line_p. */
26249 if (start == s->row->used[s->area]
26250 && ((s->row->fill_line_p
26251 && (s->hl == DRAW_NORMAL_TEXT
26252 || s->hl == DRAW_IMAGE_RAISED
26253 || s->hl == DRAW_IMAGE_SUNKEN))
26254 || s->hl == DRAW_MOUSE_FACE))
26255 s->extends_to_end_of_line_p = true;
26257 /* If S extends its face to the end of the line, set its
26258 background_width to the distance to the right edge of the drawing
26259 area. */
26260 if (s->extends_to_end_of_line_p)
26261 s->background_width = last_x - s->x + 1;
26262 else
26263 s->background_width = s->width;
26267 /* Return glyph string that shares background with glyph string S and
26268 whose `background_width' member has been set. */
26270 static struct glyph_string *
26271 glyph_string_containing_background_width (struct glyph_string *s)
26273 if (s->cmp)
26274 while (s->cmp_from)
26275 s = s->prev;
26277 return s;
26281 /* Compute overhangs and x-positions for glyph string S and its
26282 predecessors, or successors. X is the starting x-position for S.
26283 BACKWARD_P means process predecessors. */
26285 static void
26286 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26288 if (backward_p)
26290 while (s)
26292 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26293 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26294 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26295 x -= s->width;
26296 s->x = x;
26297 s = s->prev;
26300 else
26302 while (s)
26304 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26305 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26306 s->x = x;
26307 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26308 x += s->width;
26309 s = s->next;
26316 /* The following macros are only called from draw_glyphs below.
26317 They reference the following parameters of that function directly:
26318 `w', `row', `area', and `overlap_p'
26319 as well as the following local variables:
26320 `s', `f', and `hdc' (in W32) */
26322 #ifdef HAVE_NTGUI
26323 /* On W32, silently add local `hdc' variable to argument list of
26324 init_glyph_string. */
26325 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26326 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26327 #else
26328 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26329 init_glyph_string (s, char2b, w, row, area, start, hl)
26330 #endif
26332 /* Add a glyph string for a stretch glyph to the list of strings
26333 between HEAD and TAIL. START is the index of the stretch glyph in
26334 row area AREA of glyph row ROW. END is the index of the last glyph
26335 in that glyph row area. X is the current output position assigned
26336 to the new glyph string constructed. HL overrides that face of the
26337 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26338 is the right-most x-position of the drawing area. */
26340 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26341 and below -- keep them on one line. */
26342 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26343 do \
26345 s = alloca (sizeof *s); \
26346 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26347 START = fill_stretch_glyph_string (s, START, END); \
26348 append_glyph_string (&HEAD, &TAIL, s); \
26349 s->x = (X); \
26351 while (false)
26354 /* Add a glyph string for an image glyph to the list of strings
26355 between HEAD and TAIL. START is the index of the image glyph in
26356 row area AREA of glyph row ROW. END is the index of the last glyph
26357 in that glyph row area. X is the current output position assigned
26358 to the new glyph string constructed. HL overrides that face of the
26359 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26360 is the right-most x-position of the drawing area. */
26362 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26363 do \
26365 s = alloca (sizeof *s); \
26366 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26367 fill_image_glyph_string (s); \
26368 append_glyph_string (&HEAD, &TAIL, s); \
26369 ++START; \
26370 s->x = (X); \
26372 while (false)
26374 #ifndef HAVE_XWIDGETS
26375 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26376 eassume (false)
26377 #else
26378 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26379 do \
26381 s = alloca (sizeof *s); \
26382 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26383 fill_xwidget_glyph_string (s); \
26384 append_glyph_string (&(HEAD), &(TAIL), s); \
26385 ++(START); \
26386 s->x = (X); \
26388 while (false)
26389 #endif
26391 /* Add a glyph string for a sequence of character glyphs to the list
26392 of strings between HEAD and TAIL. START is the index of the first
26393 glyph in row area AREA of glyph row ROW that is part of the new
26394 glyph string. END is the index of the last glyph in that glyph row
26395 area. X is the current output position assigned to the new glyph
26396 string constructed. HL overrides that face of the glyph; e.g. it
26397 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26398 right-most x-position of the drawing area. */
26400 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26401 do \
26403 int face_id; \
26404 XChar2b *char2b; \
26406 face_id = (row)->glyphs[area][START].face_id; \
26408 s = alloca (sizeof *s); \
26409 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26410 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26411 append_glyph_string (&HEAD, &TAIL, s); \
26412 s->x = (X); \
26413 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26415 while (false)
26418 /* Add a glyph string for a composite sequence to the list of strings
26419 between HEAD and TAIL. START is the index of the first glyph in
26420 row area AREA of glyph row ROW that is part of the new glyph
26421 string. END is the index of the last glyph in that glyph row area.
26422 X is the current output position assigned to the new glyph string
26423 constructed. HL overrides that face of the glyph; e.g. it is
26424 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26425 x-position of the drawing area. */
26427 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26428 do { \
26429 int face_id = (row)->glyphs[area][START].face_id; \
26430 struct face *base_face = FACE_FROM_ID (f, face_id); \
26431 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26432 struct composition *cmp = composition_table[cmp_id]; \
26433 XChar2b *char2b; \
26434 struct glyph_string *first_s = NULL; \
26435 int n; \
26437 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26439 /* Make glyph_strings for each glyph sequence that is drawable by \
26440 the same face, and append them to HEAD/TAIL. */ \
26441 for (n = 0; n < cmp->glyph_len;) \
26443 s = alloca (sizeof *s); \
26444 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26445 append_glyph_string (&(HEAD), &(TAIL), s); \
26446 s->cmp = cmp; \
26447 s->cmp_from = n; \
26448 s->x = (X); \
26449 if (n == 0) \
26450 first_s = s; \
26451 n = fill_composite_glyph_string (s, base_face, overlaps); \
26454 ++START; \
26455 s = first_s; \
26456 } while (false)
26459 /* Add a glyph string for a glyph-string sequence to the list of strings
26460 between HEAD and TAIL. */
26462 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26463 do { \
26464 int face_id; \
26465 XChar2b *char2b; \
26466 Lisp_Object gstring; \
26468 face_id = (row)->glyphs[area][START].face_id; \
26469 gstring = (composition_gstring_from_id \
26470 ((row)->glyphs[area][START].u.cmp.id)); \
26471 s = alloca (sizeof *s); \
26472 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26473 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26474 append_glyph_string (&(HEAD), &(TAIL), s); \
26475 s->x = (X); \
26476 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26477 } while (false)
26480 /* Add a glyph string for a sequence of glyphless character's glyphs
26481 to the list of strings between HEAD and TAIL. The meanings of
26482 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26484 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26485 do \
26487 int face_id; \
26489 face_id = (row)->glyphs[area][START].face_id; \
26491 s = alloca (sizeof *s); \
26492 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26493 append_glyph_string (&HEAD, &TAIL, s); \
26494 s->x = (X); \
26495 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26496 overlaps); \
26498 while (false)
26501 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26502 of AREA of glyph row ROW on window W between indices START and END.
26503 HL overrides the face for drawing glyph strings, e.g. it is
26504 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26505 x-positions of the drawing area.
26507 This is an ugly monster macro construct because we must use alloca
26508 to allocate glyph strings (because draw_glyphs can be called
26509 asynchronously). */
26511 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26512 do \
26514 HEAD = TAIL = NULL; \
26515 while (START < END) \
26517 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26518 switch (first_glyph->type) \
26520 case CHAR_GLYPH: \
26521 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26522 HL, X, LAST_X); \
26523 break; \
26525 case COMPOSITE_GLYPH: \
26526 if (first_glyph->u.cmp.automatic) \
26527 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26528 HL, X, LAST_X); \
26529 else \
26530 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26531 HL, X, LAST_X); \
26532 break; \
26534 case STRETCH_GLYPH: \
26535 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26536 HL, X, LAST_X); \
26537 break; \
26539 case IMAGE_GLYPH: \
26540 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26541 HL, X, LAST_X); \
26542 break;
26544 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26545 case XWIDGET_GLYPH: \
26546 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26547 HL, X, LAST_X); \
26548 break;
26550 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26551 case GLYPHLESS_GLYPH: \
26552 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26553 HL, X, LAST_X); \
26554 break; \
26556 default: \
26557 emacs_abort (); \
26560 if (s) \
26562 set_glyph_string_background_width (s, START, LAST_X); \
26563 (X) += s->width; \
26566 } while (false)
26569 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26570 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26571 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26572 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26575 /* Draw glyphs between START and END in AREA of ROW on window W,
26576 starting at x-position X. X is relative to AREA in W. HL is a
26577 face-override with the following meaning:
26579 DRAW_NORMAL_TEXT draw normally
26580 DRAW_CURSOR draw in cursor face
26581 DRAW_MOUSE_FACE draw in mouse face.
26582 DRAW_INVERSE_VIDEO draw in mode line face
26583 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26584 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26586 If OVERLAPS is non-zero, draw only the foreground of characters and
26587 clip to the physical height of ROW. Non-zero value also defines
26588 the overlapping part to be drawn:
26590 OVERLAPS_PRED overlap with preceding rows
26591 OVERLAPS_SUCC overlap with succeeding rows
26592 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26593 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26595 Value is the x-position reached, relative to AREA of W. */
26597 static int
26598 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26599 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26600 enum draw_glyphs_face hl, int overlaps)
26602 struct glyph_string *head, *tail;
26603 struct glyph_string *s;
26604 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26605 int i, j, x_reached, last_x, area_left = 0;
26606 struct frame *f = XFRAME (WINDOW_FRAME (w));
26607 DECLARE_HDC (hdc);
26609 ALLOCATE_HDC (hdc, f);
26611 /* Let's rather be paranoid than getting a SEGV. */
26612 end = min (end, row->used[area]);
26613 start = clip_to_bounds (0, start, end);
26615 /* Translate X to frame coordinates. Set last_x to the right
26616 end of the drawing area. */
26617 if (row->full_width_p)
26619 /* X is relative to the left edge of W, without scroll bars
26620 or fringes. */
26621 area_left = WINDOW_LEFT_EDGE_X (w);
26622 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26623 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26625 else
26627 area_left = window_box_left (w, area);
26628 last_x = area_left + window_box_width (w, area);
26630 x += area_left;
26632 /* Build a doubly-linked list of glyph_string structures between
26633 head and tail from what we have to draw. Note that the macro
26634 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26635 the reason we use a separate variable `i'. */
26636 i = start;
26637 USE_SAFE_ALLOCA;
26638 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26639 if (tail)
26641 s = glyph_string_containing_background_width (tail);
26642 x_reached = s->x + s->background_width;
26644 else
26645 x_reached = x;
26647 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26648 the row, redraw some glyphs in front or following the glyph
26649 strings built above. */
26650 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26652 struct glyph_string *h, *t;
26653 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26654 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26655 bool check_mouse_face = false;
26656 int dummy_x = 0;
26658 /* If mouse highlighting is on, we may need to draw adjacent
26659 glyphs using mouse-face highlighting. */
26660 if (area == TEXT_AREA && row->mouse_face_p
26661 && hlinfo->mouse_face_beg_row >= 0
26662 && hlinfo->mouse_face_end_row >= 0)
26664 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26666 if (row_vpos >= hlinfo->mouse_face_beg_row
26667 && row_vpos <= hlinfo->mouse_face_end_row)
26669 check_mouse_face = true;
26670 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26671 ? hlinfo->mouse_face_beg_col : 0;
26672 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26673 ? hlinfo->mouse_face_end_col
26674 : row->used[TEXT_AREA];
26678 /* Compute overhangs for all glyph strings. */
26679 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26680 for (s = head; s; s = s->next)
26681 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26683 /* Prepend glyph strings for glyphs in front of the first glyph
26684 string that are overwritten because of the first glyph
26685 string's left overhang. The background of all strings
26686 prepended must be drawn because the first glyph string
26687 draws over it. */
26688 i = left_overwritten (head);
26689 if (i >= 0)
26691 enum draw_glyphs_face overlap_hl;
26693 /* If this row contains mouse highlighting, attempt to draw
26694 the overlapped glyphs with the correct highlight. This
26695 code fails if the overlap encompasses more than one glyph
26696 and mouse-highlight spans only some of these glyphs.
26697 However, making it work perfectly involves a lot more
26698 code, and I don't know if the pathological case occurs in
26699 practice, so we'll stick to this for now. --- cyd */
26700 if (check_mouse_face
26701 && mouse_beg_col < start && mouse_end_col > i)
26702 overlap_hl = DRAW_MOUSE_FACE;
26703 else
26704 overlap_hl = DRAW_NORMAL_TEXT;
26706 if (hl != overlap_hl)
26707 clip_head = head;
26708 j = i;
26709 BUILD_GLYPH_STRINGS (j, start, h, t,
26710 overlap_hl, dummy_x, last_x);
26711 start = i;
26712 compute_overhangs_and_x (t, head->x, true);
26713 prepend_glyph_string_lists (&head, &tail, h, t);
26714 if (clip_head == NULL)
26715 clip_head = head;
26718 /* Prepend glyph strings for glyphs in front of the first glyph
26719 string that overwrite that glyph string because of their
26720 right overhang. For these strings, only the foreground must
26721 be drawn, because it draws over the glyph string at `head'.
26722 The background must not be drawn because this would overwrite
26723 right overhangs of preceding glyphs for which no glyph
26724 strings exist. */
26725 i = left_overwriting (head);
26726 if (i >= 0)
26728 enum draw_glyphs_face overlap_hl;
26730 if (check_mouse_face
26731 && mouse_beg_col < start && mouse_end_col > i)
26732 overlap_hl = DRAW_MOUSE_FACE;
26733 else
26734 overlap_hl = DRAW_NORMAL_TEXT;
26736 if (hl == overlap_hl || clip_head == NULL)
26737 clip_head = head;
26738 BUILD_GLYPH_STRINGS (i, start, h, t,
26739 overlap_hl, dummy_x, last_x);
26740 for (s = h; s; s = s->next)
26741 s->background_filled_p = true;
26742 compute_overhangs_and_x (t, head->x, true);
26743 prepend_glyph_string_lists (&head, &tail, h, t);
26746 /* Append glyphs strings for glyphs following the last glyph
26747 string tail that are overwritten by tail. The background of
26748 these strings has to be drawn because tail's foreground draws
26749 over it. */
26750 i = right_overwritten (tail);
26751 if (i >= 0)
26753 enum draw_glyphs_face overlap_hl;
26755 if (check_mouse_face
26756 && mouse_beg_col < i && mouse_end_col > end)
26757 overlap_hl = DRAW_MOUSE_FACE;
26758 else
26759 overlap_hl = DRAW_NORMAL_TEXT;
26761 if (hl != overlap_hl)
26762 clip_tail = tail;
26763 BUILD_GLYPH_STRINGS (end, i, h, t,
26764 overlap_hl, x, last_x);
26765 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26766 we don't have `end = i;' here. */
26767 compute_overhangs_and_x (h, tail->x + tail->width, false);
26768 append_glyph_string_lists (&head, &tail, h, t);
26769 if (clip_tail == NULL)
26770 clip_tail = tail;
26773 /* Append glyph strings for glyphs following the last glyph
26774 string tail that overwrite tail. The foreground of such
26775 glyphs has to be drawn because it writes into the background
26776 of tail. The background must not be drawn because it could
26777 paint over the foreground of following glyphs. */
26778 i = right_overwriting (tail);
26779 if (i >= 0)
26781 enum draw_glyphs_face overlap_hl;
26782 if (check_mouse_face
26783 && mouse_beg_col < i && mouse_end_col > end)
26784 overlap_hl = DRAW_MOUSE_FACE;
26785 else
26786 overlap_hl = DRAW_NORMAL_TEXT;
26788 if (hl == overlap_hl || clip_tail == NULL)
26789 clip_tail = tail;
26790 i++; /* We must include the Ith glyph. */
26791 BUILD_GLYPH_STRINGS (end, i, h, t,
26792 overlap_hl, x, last_x);
26793 for (s = h; s; s = s->next)
26794 s->background_filled_p = true;
26795 compute_overhangs_and_x (h, tail->x + tail->width, false);
26796 append_glyph_string_lists (&head, &tail, h, t);
26798 tail = glyph_string_containing_background_width (tail);
26799 if (clip_tail)
26800 clip_tail = glyph_string_containing_background_width (clip_tail);
26801 if (clip_head || clip_tail)
26802 for (s = head; s; s = s->next)
26804 s->clip_head = clip_head;
26805 s->clip_tail = clip_tail;
26809 /* Draw all strings. */
26810 for (s = head; s; s = s->next)
26811 FRAME_RIF (f)->draw_glyph_string (s);
26813 #ifndef HAVE_NS
26814 /* When focus a sole frame and move horizontally, this clears on_p
26815 causing a failure to erase prev cursor position. */
26816 if (area == TEXT_AREA
26817 && !row->full_width_p
26818 /* When drawing overlapping rows, only the glyph strings'
26819 foreground is drawn, which doesn't erase a cursor
26820 completely. */
26821 && !overlaps)
26823 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26824 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26825 : (tail ? tail->x + tail->background_width : x));
26826 x0 -= area_left;
26827 x1 -= area_left;
26829 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26830 row->y, MATRIX_ROW_BOTTOM_Y (row));
26832 #endif
26834 /* Value is the x-position up to which drawn, relative to AREA of W.
26835 This doesn't include parts drawn because of overhangs. */
26836 if (row->full_width_p)
26837 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26838 else
26839 x_reached -= area_left;
26841 RELEASE_HDC (hdc, f);
26843 SAFE_FREE ();
26844 return x_reached;
26847 /* Find the first glyph in the run of underlined glyphs preceding the
26848 beginning of glyph string S, and return its font (which could be
26849 NULL). This is needed because that font determines the underline
26850 position and thickness for the entire run of the underlined glyphs.
26851 This function is called from the draw_glyph_string method of GUI
26852 frame's redisplay interface (RIF) when it needs to draw in an
26853 underlined face. */
26854 struct font *
26855 font_for_underline_metrics (struct glyph_string *s)
26857 struct glyph *g0 = s->row->glyphs[s->area], *g;
26859 for (g = s->first_glyph - 1; g >= g0; g--)
26861 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26862 if (!(prev_face && prev_face->underline_p))
26863 break;
26866 /* If preceding glyphs are not underlined, use the font of S. */
26867 if (g == s->first_glyph - 1)
26868 return s->font;
26869 else
26871 /* Otherwise use the font of the last glyph we saw in the above
26872 loop whose face had the underline_p flag set. */
26873 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26877 /* Expand row matrix if too narrow. Don't expand if area
26878 is not present. */
26880 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26882 if (!it->f->fonts_changed \
26883 && (it->glyph_row->glyphs[area] \
26884 < it->glyph_row->glyphs[area + 1])) \
26886 it->w->ncols_scale_factor++; \
26887 it->f->fonts_changed = true; \
26891 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26892 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26894 static void
26895 append_glyph (struct it *it)
26897 struct glyph *glyph;
26898 enum glyph_row_area area = it->area;
26900 eassert (it->glyph_row);
26901 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26903 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26904 if (glyph < it->glyph_row->glyphs[area + 1])
26906 /* If the glyph row is reversed, we need to prepend the glyph
26907 rather than append it. */
26908 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26910 struct glyph *g;
26912 /* Make room for the additional glyph. */
26913 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26914 g[1] = *g;
26915 glyph = it->glyph_row->glyphs[area];
26917 glyph->charpos = CHARPOS (it->position);
26918 glyph->object = it->object;
26919 if (it->pixel_width > 0)
26921 eassert (it->pixel_width <= SHRT_MAX);
26922 glyph->pixel_width = it->pixel_width;
26923 glyph->padding_p = false;
26925 else
26927 /* Assure at least 1-pixel width. Otherwise, cursor can't
26928 be displayed correctly. */
26929 glyph->pixel_width = 1;
26930 glyph->padding_p = true;
26932 glyph->ascent = it->ascent;
26933 glyph->descent = it->descent;
26934 glyph->voffset = it->voffset;
26935 glyph->type = CHAR_GLYPH;
26936 glyph->avoid_cursor_p = it->avoid_cursor_p;
26937 glyph->multibyte_p = it->multibyte_p;
26938 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26940 /* In R2L rows, the left and the right box edges need to be
26941 drawn in reverse direction. */
26942 glyph->right_box_line_p = it->start_of_box_run_p;
26943 glyph->left_box_line_p = it->end_of_box_run_p;
26945 else
26947 glyph->left_box_line_p = it->start_of_box_run_p;
26948 glyph->right_box_line_p = it->end_of_box_run_p;
26950 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26951 || it->phys_descent > it->descent);
26952 glyph->glyph_not_available_p = it->glyph_not_available_p;
26953 glyph->face_id = it->face_id;
26954 glyph->u.ch = it->char_to_display;
26955 glyph->slice.img = null_glyph_slice;
26956 glyph->font_type = FONT_TYPE_UNKNOWN;
26957 if (it->bidi_p)
26959 glyph->resolved_level = it->bidi_it.resolved_level;
26960 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26961 glyph->bidi_type = it->bidi_it.type;
26963 else
26965 glyph->resolved_level = 0;
26966 glyph->bidi_type = UNKNOWN_BT;
26968 ++it->glyph_row->used[area];
26970 else
26971 IT_EXPAND_MATRIX_WIDTH (it, area);
26974 /* Store one glyph for the composition IT->cmp_it.id in
26975 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26976 non-null. */
26978 static void
26979 append_composite_glyph (struct it *it)
26981 struct glyph *glyph;
26982 enum glyph_row_area area = it->area;
26984 eassert (it->glyph_row);
26986 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26987 if (glyph < it->glyph_row->glyphs[area + 1])
26989 /* If the glyph row is reversed, we need to prepend the glyph
26990 rather than append it. */
26991 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26993 struct glyph *g;
26995 /* Make room for the new glyph. */
26996 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26997 g[1] = *g;
26998 glyph = it->glyph_row->glyphs[it->area];
27000 glyph->charpos = it->cmp_it.charpos;
27001 glyph->object = it->object;
27002 eassert (it->pixel_width <= SHRT_MAX);
27003 glyph->pixel_width = it->pixel_width;
27004 glyph->ascent = it->ascent;
27005 glyph->descent = it->descent;
27006 glyph->voffset = it->voffset;
27007 glyph->type = COMPOSITE_GLYPH;
27008 if (it->cmp_it.ch < 0)
27010 glyph->u.cmp.automatic = false;
27011 glyph->u.cmp.id = it->cmp_it.id;
27012 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
27014 else
27016 glyph->u.cmp.automatic = true;
27017 glyph->u.cmp.id = it->cmp_it.id;
27018 glyph->slice.cmp.from = it->cmp_it.from;
27019 glyph->slice.cmp.to = it->cmp_it.to - 1;
27021 glyph->avoid_cursor_p = it->avoid_cursor_p;
27022 glyph->multibyte_p = it->multibyte_p;
27023 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27025 /* In R2L rows, the left and the right box edges need to be
27026 drawn in reverse direction. */
27027 glyph->right_box_line_p = it->start_of_box_run_p;
27028 glyph->left_box_line_p = it->end_of_box_run_p;
27030 else
27032 glyph->left_box_line_p = it->start_of_box_run_p;
27033 glyph->right_box_line_p = it->end_of_box_run_p;
27035 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27036 || it->phys_descent > it->descent);
27037 glyph->padding_p = false;
27038 glyph->glyph_not_available_p = false;
27039 glyph->face_id = it->face_id;
27040 glyph->font_type = FONT_TYPE_UNKNOWN;
27041 if (it->bidi_p)
27043 glyph->resolved_level = it->bidi_it.resolved_level;
27044 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27045 glyph->bidi_type = it->bidi_it.type;
27047 ++it->glyph_row->used[area];
27049 else
27050 IT_EXPAND_MATRIX_WIDTH (it, area);
27054 /* Change IT->ascent and IT->height according to the setting of
27055 IT->voffset. */
27057 static void
27058 take_vertical_position_into_account (struct it *it)
27060 if (it->voffset)
27062 if (it->voffset < 0)
27063 /* Increase the ascent so that we can display the text higher
27064 in the line. */
27065 it->ascent -= it->voffset;
27066 else
27067 /* Increase the descent so that we can display the text lower
27068 in the line. */
27069 it->descent += it->voffset;
27074 /* Produce glyphs/get display metrics for the image IT is loaded with.
27075 See the description of struct display_iterator in dispextern.h for
27076 an overview of struct display_iterator. */
27078 static void
27079 produce_image_glyph (struct it *it)
27081 struct image *img;
27082 struct face *face;
27083 int glyph_ascent, crop;
27084 struct glyph_slice slice;
27086 eassert (it->what == IT_IMAGE);
27088 face = FACE_FROM_ID (it->f, it->face_id);
27089 /* Make sure X resources of the face is loaded. */
27090 prepare_face_for_display (it->f, face);
27092 if (it->image_id < 0)
27094 /* Fringe bitmap. */
27095 it->ascent = it->phys_ascent = 0;
27096 it->descent = it->phys_descent = 0;
27097 it->pixel_width = 0;
27098 it->nglyphs = 0;
27099 return;
27102 img = IMAGE_FROM_ID (it->f, it->image_id);
27103 /* Make sure X resources of the image is loaded. */
27104 prepare_image_for_display (it->f, img);
27106 slice.x = slice.y = 0;
27107 slice.width = img->width;
27108 slice.height = img->height;
27110 if (INTEGERP (it->slice.x))
27111 slice.x = XINT (it->slice.x);
27112 else if (FLOATP (it->slice.x))
27113 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27115 if (INTEGERP (it->slice.y))
27116 slice.y = XINT (it->slice.y);
27117 else if (FLOATP (it->slice.y))
27118 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27120 if (INTEGERP (it->slice.width))
27121 slice.width = XINT (it->slice.width);
27122 else if (FLOATP (it->slice.width))
27123 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27125 if (INTEGERP (it->slice.height))
27126 slice.height = XINT (it->slice.height);
27127 else if (FLOATP (it->slice.height))
27128 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27130 if (slice.x >= img->width)
27131 slice.x = img->width;
27132 if (slice.y >= img->height)
27133 slice.y = img->height;
27134 if (slice.x + slice.width >= img->width)
27135 slice.width = img->width - slice.x;
27136 if (slice.y + slice.height > img->height)
27137 slice.height = img->height - slice.y;
27139 if (slice.width == 0 || slice.height == 0)
27140 return;
27142 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27144 it->descent = slice.height - glyph_ascent;
27145 if (slice.y == 0)
27146 it->descent += img->vmargin;
27147 if (slice.y + slice.height == img->height)
27148 it->descent += img->vmargin;
27149 it->phys_descent = it->descent;
27151 it->pixel_width = slice.width;
27152 if (slice.x == 0)
27153 it->pixel_width += img->hmargin;
27154 if (slice.x + slice.width == img->width)
27155 it->pixel_width += img->hmargin;
27157 /* It's quite possible for images to have an ascent greater than
27158 their height, so don't get confused in that case. */
27159 if (it->descent < 0)
27160 it->descent = 0;
27162 it->nglyphs = 1;
27164 if (face->box != FACE_NO_BOX)
27166 if (face->box_line_width > 0)
27168 if (slice.y == 0)
27169 it->ascent += face->box_line_width;
27170 if (slice.y + slice.height == img->height)
27171 it->descent += face->box_line_width;
27174 if (it->start_of_box_run_p && slice.x == 0)
27175 it->pixel_width += eabs (face->box_line_width);
27176 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27177 it->pixel_width += eabs (face->box_line_width);
27180 take_vertical_position_into_account (it);
27182 /* Automatically crop wide image glyphs at right edge so we can
27183 draw the cursor on same display row. */
27184 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27185 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27187 it->pixel_width -= crop;
27188 slice.width -= crop;
27191 if (it->glyph_row)
27193 struct glyph *glyph;
27194 enum glyph_row_area area = it->area;
27196 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27197 if (it->glyph_row->reversed_p)
27199 struct glyph *g;
27201 /* Make room for the new glyph. */
27202 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27203 g[1] = *g;
27204 glyph = it->glyph_row->glyphs[it->area];
27206 if (glyph < it->glyph_row->glyphs[area + 1])
27208 glyph->charpos = CHARPOS (it->position);
27209 glyph->object = it->object;
27210 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27211 glyph->ascent = glyph_ascent;
27212 glyph->descent = it->descent;
27213 glyph->voffset = it->voffset;
27214 glyph->type = IMAGE_GLYPH;
27215 glyph->avoid_cursor_p = it->avoid_cursor_p;
27216 glyph->multibyte_p = it->multibyte_p;
27217 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27219 /* In R2L rows, the left and the right box edges need to be
27220 drawn in reverse direction. */
27221 glyph->right_box_line_p = it->start_of_box_run_p;
27222 glyph->left_box_line_p = it->end_of_box_run_p;
27224 else
27226 glyph->left_box_line_p = it->start_of_box_run_p;
27227 glyph->right_box_line_p = it->end_of_box_run_p;
27229 glyph->overlaps_vertically_p = false;
27230 glyph->padding_p = false;
27231 glyph->glyph_not_available_p = false;
27232 glyph->face_id = it->face_id;
27233 glyph->u.img_id = img->id;
27234 glyph->slice.img = slice;
27235 glyph->font_type = FONT_TYPE_UNKNOWN;
27236 if (it->bidi_p)
27238 glyph->resolved_level = it->bidi_it.resolved_level;
27239 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27240 glyph->bidi_type = it->bidi_it.type;
27242 ++it->glyph_row->used[area];
27244 else
27245 IT_EXPAND_MATRIX_WIDTH (it, area);
27249 static void
27250 produce_xwidget_glyph (struct it *it)
27252 #ifdef HAVE_XWIDGETS
27253 struct xwidget *xw;
27254 int glyph_ascent, crop;
27255 eassert (it->what == IT_XWIDGET);
27257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27258 /* Make sure X resources of the face is loaded. */
27259 prepare_face_for_display (it->f, face);
27261 xw = it->xwidget;
27262 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27263 it->descent = xw->height/2;
27264 it->phys_descent = it->descent;
27265 it->pixel_width = xw->width;
27266 /* It's quite possible for images to have an ascent greater than
27267 their height, so don't get confused in that case. */
27268 if (it->descent < 0)
27269 it->descent = 0;
27271 it->nglyphs = 1;
27273 if (face->box != FACE_NO_BOX)
27275 if (face->box_line_width > 0)
27277 it->ascent += face->box_line_width;
27278 it->descent += face->box_line_width;
27281 if (it->start_of_box_run_p)
27282 it->pixel_width += eabs (face->box_line_width);
27283 it->pixel_width += eabs (face->box_line_width);
27286 take_vertical_position_into_account (it);
27288 /* Automatically crop wide image glyphs at right edge so we can
27289 draw the cursor on same display row. */
27290 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27291 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27292 it->pixel_width -= crop;
27294 if (it->glyph_row)
27296 enum glyph_row_area area = it->area;
27297 struct glyph *glyph
27298 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27300 if (it->glyph_row->reversed_p)
27302 struct glyph *g;
27304 /* Make room for the new glyph. */
27305 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27306 g[1] = *g;
27307 glyph = it->glyph_row->glyphs[it->area];
27309 if (glyph < it->glyph_row->glyphs[area + 1])
27311 glyph->charpos = CHARPOS (it->position);
27312 glyph->object = it->object;
27313 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27314 glyph->ascent = glyph_ascent;
27315 glyph->descent = it->descent;
27316 glyph->voffset = it->voffset;
27317 glyph->type = XWIDGET_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 = 0;
27333 glyph->padding_p = 0;
27334 glyph->glyph_not_available_p = 0;
27335 glyph->face_id = it->face_id;
27336 glyph->u.xwidget = it->xwidget;
27337 glyph->font_type = FONT_TYPE_UNKNOWN;
27338 if (it->bidi_p)
27340 glyph->resolved_level = it->bidi_it.resolved_level;
27341 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27342 glyph->bidi_type = it->bidi_it.type;
27344 ++it->glyph_row->used[area];
27346 else
27347 IT_EXPAND_MATRIX_WIDTH (it, area);
27349 #endif
27352 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27353 of the glyph, WIDTH and HEIGHT are the width and height of the
27354 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27356 static void
27357 append_stretch_glyph (struct it *it, Lisp_Object object,
27358 int width, int height, int ascent)
27360 struct glyph *glyph;
27361 enum glyph_row_area area = it->area;
27363 eassert (ascent >= 0 && ascent <= height);
27365 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27366 if (glyph < it->glyph_row->glyphs[area + 1])
27368 /* If the glyph row is reversed, we need to prepend the glyph
27369 rather than append it. */
27370 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27372 struct glyph *g;
27374 /* Make room for the additional glyph. */
27375 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27376 g[1] = *g;
27377 glyph = it->glyph_row->glyphs[area];
27379 /* Decrease the width of the first glyph of the row that
27380 begins before first_visible_x (e.g., due to hscroll).
27381 This is so the overall width of the row becomes smaller
27382 by the scroll amount, and the stretch glyph appended by
27383 extend_face_to_end_of_line will be wider, to shift the
27384 row glyphs to the right. (In L2R rows, the corresponding
27385 left-shift effect is accomplished by setting row->x to a
27386 negative value, which won't work with R2L rows.)
27388 This must leave us with a positive value of WIDTH, since
27389 otherwise the call to move_it_in_display_line_to at the
27390 beginning of display_line would have got past the entire
27391 first glyph, and then it->current_x would have been
27392 greater or equal to it->first_visible_x. */
27393 if (it->current_x < it->first_visible_x)
27394 width -= it->first_visible_x - it->current_x;
27395 eassert (width > 0);
27397 glyph->charpos = CHARPOS (it->position);
27398 glyph->object = object;
27399 /* FIXME: It would be better to use TYPE_MAX here, but
27400 __typeof__ is not portable enough... */
27401 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27402 glyph->ascent = ascent;
27403 glyph->descent = height - ascent;
27404 glyph->voffset = it->voffset;
27405 glyph->type = STRETCH_GLYPH;
27406 glyph->avoid_cursor_p = it->avoid_cursor_p;
27407 glyph->multibyte_p = it->multibyte_p;
27408 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27410 /* In R2L rows, the left and the right box edges need to be
27411 drawn in reverse direction. */
27412 glyph->right_box_line_p = it->start_of_box_run_p;
27413 glyph->left_box_line_p = it->end_of_box_run_p;
27415 else
27417 glyph->left_box_line_p = it->start_of_box_run_p;
27418 glyph->right_box_line_p = it->end_of_box_run_p;
27420 glyph->overlaps_vertically_p = false;
27421 glyph->padding_p = false;
27422 glyph->glyph_not_available_p = false;
27423 glyph->face_id = it->face_id;
27424 glyph->u.stretch.ascent = ascent;
27425 glyph->u.stretch.height = height;
27426 glyph->slice.img = null_glyph_slice;
27427 glyph->font_type = FONT_TYPE_UNKNOWN;
27428 if (it->bidi_p)
27430 glyph->resolved_level = it->bidi_it.resolved_level;
27431 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27432 glyph->bidi_type = it->bidi_it.type;
27434 else
27436 glyph->resolved_level = 0;
27437 glyph->bidi_type = UNKNOWN_BT;
27439 ++it->glyph_row->used[area];
27441 else
27442 IT_EXPAND_MATRIX_WIDTH (it, area);
27445 #endif /* HAVE_WINDOW_SYSTEM */
27447 /* Produce a stretch glyph for iterator IT. IT->object is the value
27448 of the glyph property displayed. The value must be a list
27449 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27450 being recognized:
27452 1. `:width WIDTH' specifies that the space should be WIDTH *
27453 canonical char width wide. WIDTH may be an integer or floating
27454 point number.
27456 2. `:relative-width FACTOR' specifies that the width of the stretch
27457 should be computed from the width of the first character having the
27458 `glyph' property, and should be FACTOR times that width.
27460 3. `:align-to HPOS' specifies that the space should be wide enough
27461 to reach HPOS, a value in canonical character units.
27463 Exactly one of the above pairs must be present.
27465 4. `:height HEIGHT' specifies that the height of the stretch produced
27466 should be HEIGHT, measured in canonical character units.
27468 5. `:relative-height FACTOR' specifies that the height of the
27469 stretch should be FACTOR times the height of the characters having
27470 the glyph property.
27472 Either none or exactly one of 4 or 5 must be present.
27474 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27475 of the stretch should be used for the ascent of the stretch.
27476 ASCENT must be in the range 0 <= ASCENT <= 100. */
27478 void
27479 produce_stretch_glyph (struct it *it)
27481 /* (space :width WIDTH :height HEIGHT ...) */
27482 Lisp_Object prop, plist;
27483 int width = 0, height = 0, align_to = -1;
27484 bool zero_width_ok_p = false;
27485 double tem;
27486 struct font *font = NULL;
27488 #ifdef HAVE_WINDOW_SYSTEM
27489 int ascent = 0;
27490 bool zero_height_ok_p = false;
27492 if (FRAME_WINDOW_P (it->f))
27494 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27495 font = face->font ? face->font : FRAME_FONT (it->f);
27496 prepare_face_for_display (it->f, face);
27498 #endif
27500 /* List should start with `space'. */
27501 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27502 plist = XCDR (it->object);
27504 /* Compute the width of the stretch. */
27505 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27506 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27508 /* Absolute width `:width WIDTH' specified and valid. */
27509 zero_width_ok_p = true;
27510 width = (int)tem;
27512 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27514 /* Relative width `:relative-width FACTOR' specified and valid.
27515 Compute the width of the characters having the `glyph'
27516 property. */
27517 struct it it2;
27518 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27520 it2 = *it;
27521 if (it->multibyte_p)
27522 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27523 else
27525 it2.c = it2.char_to_display = *p, it2.len = 1;
27526 if (! ASCII_CHAR_P (it2.c))
27527 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27530 it2.glyph_row = NULL;
27531 it2.what = IT_CHARACTER;
27532 PRODUCE_GLYPHS (&it2);
27533 width = NUMVAL (prop) * it2.pixel_width;
27535 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27536 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27537 &align_to))
27539 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27540 align_to = (align_to < 0
27542 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27543 else if (align_to < 0)
27544 align_to = window_box_left_offset (it->w, TEXT_AREA);
27545 width = max (0, (int)tem + align_to - it->current_x);
27546 zero_width_ok_p = true;
27548 else
27549 /* Nothing specified -> width defaults to canonical char width. */
27550 width = FRAME_COLUMN_WIDTH (it->f);
27552 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27553 width = 1;
27555 #ifdef HAVE_WINDOW_SYSTEM
27556 /* Compute height. */
27557 if (FRAME_WINDOW_P (it->f))
27559 int default_height = normal_char_height (font, ' ');
27561 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27562 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27564 height = (int)tem;
27565 zero_height_ok_p = true;
27567 else if (prop = Fplist_get (plist, QCrelative_height),
27568 NUMVAL (prop) > 0)
27569 height = default_height * NUMVAL (prop);
27570 else
27571 height = default_height;
27573 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27574 height = 1;
27576 /* Compute percentage of height used for ascent. If
27577 `:ascent ASCENT' is present and valid, use that. Otherwise,
27578 derive the ascent from the font in use. */
27579 if (prop = Fplist_get (plist, QCascent),
27580 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27581 ascent = height * NUMVAL (prop) / 100.0;
27582 else if (!NILP (prop)
27583 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27584 ascent = min (max (0, (int)tem), height);
27585 else
27586 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27588 else
27589 #endif /* HAVE_WINDOW_SYSTEM */
27590 height = 1;
27592 if (width > 0 && it->line_wrap != TRUNCATE
27593 && it->current_x + width > it->last_visible_x)
27595 width = it->last_visible_x - it->current_x;
27596 #ifdef HAVE_WINDOW_SYSTEM
27597 /* Subtract one more pixel from the stretch width, but only on
27598 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27599 width -= FRAME_WINDOW_P (it->f);
27600 #endif
27603 if (width > 0 && height > 0 && it->glyph_row)
27605 Lisp_Object o_object = it->object;
27606 Lisp_Object object = it->stack[it->sp - 1].string;
27607 int n = width;
27609 if (!STRINGP (object))
27610 object = it->w->contents;
27611 #ifdef HAVE_WINDOW_SYSTEM
27612 if (FRAME_WINDOW_P (it->f))
27613 append_stretch_glyph (it, object, width, height, ascent);
27614 else
27615 #endif
27617 it->object = object;
27618 it->char_to_display = ' ';
27619 it->pixel_width = it->len = 1;
27620 while (n--)
27621 tty_append_glyph (it);
27622 it->object = o_object;
27626 it->pixel_width = width;
27627 #ifdef HAVE_WINDOW_SYSTEM
27628 if (FRAME_WINDOW_P (it->f))
27630 it->ascent = it->phys_ascent = ascent;
27631 it->descent = it->phys_descent = height - it->ascent;
27632 it->nglyphs = width > 0 && height > 0;
27633 take_vertical_position_into_account (it);
27635 else
27636 #endif
27637 it->nglyphs = width;
27640 /* Get information about special display element WHAT in an
27641 environment described by IT. WHAT is one of IT_TRUNCATION or
27642 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27643 non-null glyph_row member. This function ensures that fields like
27644 face_id, c, len of IT are left untouched. */
27646 static void
27647 produce_special_glyphs (struct it *it, enum display_element_type what)
27649 struct it temp_it;
27650 Lisp_Object gc;
27651 GLYPH glyph;
27653 temp_it = *it;
27654 temp_it.object = Qnil;
27655 memset (&temp_it.current, 0, sizeof temp_it.current);
27657 if (what == IT_CONTINUATION)
27659 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27660 if (it->bidi_it.paragraph_dir == R2L)
27661 SET_GLYPH_FROM_CHAR (glyph, '/');
27662 else
27663 SET_GLYPH_FROM_CHAR (glyph, '\\');
27664 if (it->dp
27665 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27667 /* FIXME: Should we mirror GC for R2L lines? */
27668 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27669 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27672 else if (what == IT_TRUNCATION)
27674 /* Truncation glyph. */
27675 SET_GLYPH_FROM_CHAR (glyph, '$');
27676 if (it->dp
27677 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27679 /* FIXME: Should we mirror GC for R2L lines? */
27680 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27681 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27684 else
27685 emacs_abort ();
27687 #ifdef HAVE_WINDOW_SYSTEM
27688 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27689 is turned off, we precede the truncation/continuation glyphs by a
27690 stretch glyph whose width is computed such that these special
27691 glyphs are aligned at the window margin, even when very different
27692 fonts are used in different glyph rows. */
27693 if (FRAME_WINDOW_P (temp_it.f)
27694 /* init_iterator calls this with it->glyph_row == NULL, and it
27695 wants only the pixel width of the truncation/continuation
27696 glyphs. */
27697 && temp_it.glyph_row
27698 /* insert_left_trunc_glyphs calls us at the beginning of the
27699 row, and it has its own calculation of the stretch glyph
27700 width. */
27701 && temp_it.glyph_row->used[TEXT_AREA] > 0
27702 && (temp_it.glyph_row->reversed_p
27703 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27704 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27706 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27708 if (stretch_width > 0)
27710 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27711 struct font *font =
27712 face->font ? face->font : FRAME_FONT (temp_it.f);
27713 int stretch_ascent =
27714 (((temp_it.ascent + temp_it.descent)
27715 * FONT_BASE (font)) / FONT_HEIGHT (font));
27717 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27718 temp_it.ascent + temp_it.descent,
27719 stretch_ascent);
27722 #endif
27724 temp_it.dp = NULL;
27725 temp_it.what = IT_CHARACTER;
27726 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27727 temp_it.face_id = GLYPH_FACE (glyph);
27728 temp_it.len = CHAR_BYTES (temp_it.c);
27730 PRODUCE_GLYPHS (&temp_it);
27731 it->pixel_width = temp_it.pixel_width;
27732 it->nglyphs = temp_it.nglyphs;
27735 #ifdef HAVE_WINDOW_SYSTEM
27737 /* Calculate line-height and line-spacing properties.
27738 An integer value specifies explicit pixel value.
27739 A float value specifies relative value to current face height.
27740 A cons (float . face-name) specifies relative value to
27741 height of specified face font.
27743 Returns height in pixels, or nil. */
27745 static Lisp_Object
27746 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27747 int boff, bool override)
27749 Lisp_Object face_name = Qnil;
27750 int ascent, descent, height;
27752 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27753 return val;
27755 if (CONSP (val))
27757 face_name = XCAR (val);
27758 val = XCDR (val);
27759 if (!NUMBERP (val))
27760 val = make_number (1);
27761 if (NILP (face_name))
27763 height = it->ascent + it->descent;
27764 goto scale;
27768 if (NILP (face_name))
27770 font = FRAME_FONT (it->f);
27771 boff = FRAME_BASELINE_OFFSET (it->f);
27773 else if (EQ (face_name, Qt))
27775 override = false;
27777 else
27779 int face_id;
27780 struct face *face;
27782 face_id = lookup_named_face (it->f, face_name, false);
27783 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27784 if (face == NULL || ((font = face->font) == NULL))
27785 return make_number (-1);
27786 boff = font->baseline_offset;
27787 if (font->vertical_centering)
27788 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27791 normal_char_ascent_descent (font, -1, &ascent, &descent);
27793 if (override)
27795 it->override_ascent = ascent;
27796 it->override_descent = descent;
27797 it->override_boff = boff;
27800 height = ascent + descent;
27802 scale:
27803 if (FLOATP (val))
27804 height = (int)(XFLOAT_DATA (val) * height);
27805 else if (INTEGERP (val))
27806 height *= XINT (val);
27808 return make_number (height);
27812 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27813 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27814 and only if this is for a character for which no font was found.
27816 If the display method (it->glyphless_method) is
27817 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27818 length of the acronym or the hexadecimal string, UPPER_XOFF and
27819 UPPER_YOFF are pixel offsets for the upper part of the string,
27820 LOWER_XOFF and LOWER_YOFF are for the lower part.
27822 For the other display methods, LEN through LOWER_YOFF are zero. */
27824 static void
27825 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27826 short upper_xoff, short upper_yoff,
27827 short lower_xoff, short lower_yoff)
27829 struct glyph *glyph;
27830 enum glyph_row_area area = it->area;
27832 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27833 if (glyph < it->glyph_row->glyphs[area + 1])
27835 /* If the glyph row is reversed, we need to prepend the glyph
27836 rather than append it. */
27837 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27839 struct glyph *g;
27841 /* Make room for the additional glyph. */
27842 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27843 g[1] = *g;
27844 glyph = it->glyph_row->glyphs[area];
27846 glyph->charpos = CHARPOS (it->position);
27847 glyph->object = it->object;
27848 eassert (it->pixel_width <= SHRT_MAX);
27849 glyph->pixel_width = it->pixel_width;
27850 glyph->ascent = it->ascent;
27851 glyph->descent = it->descent;
27852 glyph->voffset = it->voffset;
27853 glyph->type = GLYPHLESS_GLYPH;
27854 glyph->u.glyphless.method = it->glyphless_method;
27855 glyph->u.glyphless.for_no_font = for_no_font;
27856 glyph->u.glyphless.len = len;
27857 glyph->u.glyphless.ch = it->c;
27858 glyph->slice.glyphless.upper_xoff = upper_xoff;
27859 glyph->slice.glyphless.upper_yoff = upper_yoff;
27860 glyph->slice.glyphless.lower_xoff = lower_xoff;
27861 glyph->slice.glyphless.lower_yoff = lower_yoff;
27862 glyph->avoid_cursor_p = it->avoid_cursor_p;
27863 glyph->multibyte_p = it->multibyte_p;
27864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27866 /* In R2L rows, the left and the right box edges need to be
27867 drawn in reverse direction. */
27868 glyph->right_box_line_p = it->start_of_box_run_p;
27869 glyph->left_box_line_p = it->end_of_box_run_p;
27871 else
27873 glyph->left_box_line_p = it->start_of_box_run_p;
27874 glyph->right_box_line_p = it->end_of_box_run_p;
27876 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27877 || it->phys_descent > it->descent);
27878 glyph->padding_p = false;
27879 glyph->glyph_not_available_p = false;
27880 glyph->face_id = face_id;
27881 glyph->font_type = FONT_TYPE_UNKNOWN;
27882 if (it->bidi_p)
27884 glyph->resolved_level = it->bidi_it.resolved_level;
27885 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27886 glyph->bidi_type = it->bidi_it.type;
27888 ++it->glyph_row->used[area];
27890 else
27891 IT_EXPAND_MATRIX_WIDTH (it, area);
27895 /* Produce a glyph for a glyphless character for iterator IT.
27896 IT->glyphless_method specifies which method to use for displaying
27897 the character. See the description of enum
27898 glyphless_display_method in dispextern.h for the detail.
27900 FOR_NO_FONT is true if and only if this is for a character for
27901 which no font was found. ACRONYM, if non-nil, is an acronym string
27902 for the character. */
27904 static void
27905 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27907 int face_id;
27908 struct face *face;
27909 struct font *font;
27910 int base_width, base_height, width, height;
27911 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27912 int len;
27914 /* Get the metrics of the base font. We always refer to the current
27915 ASCII face. */
27916 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27917 font = face->font ? face->font : FRAME_FONT (it->f);
27918 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27919 it->ascent += font->baseline_offset;
27920 it->descent -= font->baseline_offset;
27921 base_height = it->ascent + it->descent;
27922 base_width = font->average_width;
27924 face_id = merge_glyphless_glyph_face (it);
27926 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27928 it->pixel_width = THIN_SPACE_WIDTH;
27929 len = 0;
27930 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27932 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27934 width = CHARACTER_WIDTH (it->c);
27935 if (width == 0)
27936 width = 1;
27937 else if (width > 4)
27938 width = 4;
27939 it->pixel_width = base_width * width;
27940 len = 0;
27941 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27943 else
27945 char buf[7];
27946 const char *str;
27947 unsigned int code[6];
27948 int upper_len;
27949 int ascent, descent;
27950 struct font_metrics metrics_upper, metrics_lower;
27952 face = FACE_FROM_ID (it->f, face_id);
27953 font = face->font ? face->font : FRAME_FONT (it->f);
27954 prepare_face_for_display (it->f, face);
27956 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27958 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27959 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27960 if (CONSP (acronym))
27961 acronym = XCAR (acronym);
27962 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27964 else
27966 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27967 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27968 str = buf;
27970 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27971 code[len] = font->driver->encode_char (font, str[len]);
27972 upper_len = (len + 1) / 2;
27973 font->driver->text_extents (font, code, upper_len,
27974 &metrics_upper);
27975 font->driver->text_extents (font, code + upper_len, len - upper_len,
27976 &metrics_lower);
27980 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27981 width = max (metrics_upper.width, metrics_lower.width) + 4;
27982 upper_xoff = upper_yoff = 2; /* the typical case */
27983 if (base_width >= width)
27985 /* Align the upper to the left, the lower to the right. */
27986 it->pixel_width = base_width;
27987 lower_xoff = base_width - 2 - metrics_lower.width;
27989 else
27991 /* Center the shorter one. */
27992 it->pixel_width = width;
27993 if (metrics_upper.width >= metrics_lower.width)
27994 lower_xoff = (width - metrics_lower.width) / 2;
27995 else
27997 /* FIXME: This code doesn't look right. It formerly was
27998 missing the "lower_xoff = 0;", which couldn't have
27999 been right since it left lower_xoff uninitialized. */
28000 lower_xoff = 0;
28001 upper_xoff = (width - metrics_upper.width) / 2;
28005 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
28006 top, bottom, and between upper and lower strings. */
28007 height = (metrics_upper.ascent + metrics_upper.descent
28008 + metrics_lower.ascent + metrics_lower.descent) + 5;
28009 /* Center vertically.
28010 H:base_height, D:base_descent
28011 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
28013 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
28014 descent = D - H/2 + h/2;
28015 lower_yoff = descent - 2 - ld;
28016 upper_yoff = lower_yoff - la - 1 - ud; */
28017 ascent = - (it->descent - (base_height + height + 1) / 2);
28018 descent = it->descent - (base_height - height) / 2;
28019 lower_yoff = descent - 2 - metrics_lower.descent;
28020 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
28021 - metrics_upper.descent);
28022 /* Don't make the height shorter than the base height. */
28023 if (height > base_height)
28025 it->ascent = ascent;
28026 it->descent = descent;
28030 it->phys_ascent = it->ascent;
28031 it->phys_descent = it->descent;
28032 if (it->glyph_row)
28033 append_glyphless_glyph (it, face_id, for_no_font, len,
28034 upper_xoff, upper_yoff,
28035 lower_xoff, lower_yoff);
28036 it->nglyphs = 1;
28037 take_vertical_position_into_account (it);
28041 /* RIF:
28042 Produce glyphs/get display metrics for the display element IT is
28043 loaded with. See the description of struct it in dispextern.h
28044 for an overview of struct it. */
28046 void
28047 x_produce_glyphs (struct it *it)
28049 int extra_line_spacing = it->extra_line_spacing;
28051 it->glyph_not_available_p = false;
28053 if (it->what == IT_CHARACTER)
28055 XChar2b char2b;
28056 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28057 struct font *font = face->font;
28058 struct font_metrics *pcm = NULL;
28059 int boff; /* Baseline offset. */
28061 if (font == NULL)
28063 /* When no suitable font is found, display this character by
28064 the method specified in the first extra slot of
28065 Vglyphless_char_display. */
28066 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
28068 eassert (it->what == IT_GLYPHLESS);
28069 produce_glyphless_glyph (it, true,
28070 STRINGP (acronym) ? acronym : Qnil);
28071 goto done;
28074 boff = font->baseline_offset;
28075 if (font->vertical_centering)
28076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28078 if (it->char_to_display != '\n' && it->char_to_display != '\t')
28080 it->nglyphs = 1;
28082 if (it->override_ascent >= 0)
28084 it->ascent = it->override_ascent;
28085 it->descent = it->override_descent;
28086 boff = it->override_boff;
28088 else
28090 it->ascent = FONT_BASE (font) + boff;
28091 it->descent = FONT_DESCENT (font) - boff;
28094 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28096 pcm = get_per_char_metric (font, &char2b);
28097 if (pcm->width == 0
28098 && pcm->rbearing == 0 && pcm->lbearing == 0)
28099 pcm = NULL;
28102 if (pcm)
28104 it->phys_ascent = pcm->ascent + boff;
28105 it->phys_descent = pcm->descent - boff;
28106 it->pixel_width = pcm->width;
28107 /* Don't use font-global values for ascent and descent
28108 if they result in an exceedingly large line height. */
28109 if (it->override_ascent < 0)
28111 if (FONT_TOO_HIGH (font))
28113 it->ascent = it->phys_ascent;
28114 it->descent = it->phys_descent;
28115 /* These limitations are enforced by an
28116 assertion near the end of this function. */
28117 if (it->ascent < 0)
28118 it->ascent = 0;
28119 if (it->descent < 0)
28120 it->descent = 0;
28124 else
28126 it->glyph_not_available_p = true;
28127 it->phys_ascent = it->ascent;
28128 it->phys_descent = it->descent;
28129 it->pixel_width = font->space_width;
28132 if (it->constrain_row_ascent_descent_p)
28134 if (it->descent > it->max_descent)
28136 it->ascent += it->descent - it->max_descent;
28137 it->descent = it->max_descent;
28139 if (it->ascent > it->max_ascent)
28141 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28142 it->ascent = it->max_ascent;
28144 it->phys_ascent = min (it->phys_ascent, it->ascent);
28145 it->phys_descent = min (it->phys_descent, it->descent);
28146 extra_line_spacing = 0;
28149 /* If this is a space inside a region of text with
28150 `space-width' property, change its width. */
28151 bool stretched_p
28152 = it->char_to_display == ' ' && !NILP (it->space_width);
28153 if (stretched_p)
28154 it->pixel_width *= XFLOATINT (it->space_width);
28156 /* If face has a box, add the box thickness to the character
28157 height. If character has a box line to the left and/or
28158 right, add the box line width to the character's width. */
28159 if (face->box != FACE_NO_BOX)
28161 int thick = face->box_line_width;
28163 if (thick > 0)
28165 it->ascent += thick;
28166 it->descent += thick;
28168 else
28169 thick = -thick;
28171 if (it->start_of_box_run_p)
28172 it->pixel_width += thick;
28173 if (it->end_of_box_run_p)
28174 it->pixel_width += thick;
28177 /* If face has an overline, add the height of the overline
28178 (1 pixel) and a 1 pixel margin to the character height. */
28179 if (face->overline_p)
28180 it->ascent += overline_margin;
28182 if (it->constrain_row_ascent_descent_p)
28184 if (it->ascent > it->max_ascent)
28185 it->ascent = it->max_ascent;
28186 if (it->descent > it->max_descent)
28187 it->descent = it->max_descent;
28190 take_vertical_position_into_account (it);
28192 /* If we have to actually produce glyphs, do it. */
28193 if (it->glyph_row)
28195 if (stretched_p)
28197 /* Translate a space with a `space-width' property
28198 into a stretch glyph. */
28199 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28200 / FONT_HEIGHT (font));
28201 append_stretch_glyph (it, it->object, it->pixel_width,
28202 it->ascent + it->descent, ascent);
28204 else
28205 append_glyph (it);
28207 /* If characters with lbearing or rbearing are displayed
28208 in this line, record that fact in a flag of the
28209 glyph row. This is used to optimize X output code. */
28210 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28211 it->glyph_row->contains_overlapping_glyphs_p = true;
28213 if (! stretched_p && it->pixel_width == 0)
28214 /* We assure that all visible glyphs have at least 1-pixel
28215 width. */
28216 it->pixel_width = 1;
28218 else if (it->char_to_display == '\n')
28220 /* A newline has no width, but we need the height of the
28221 line. But if previous part of the line sets a height,
28222 don't increase that height. */
28224 Lisp_Object height;
28225 Lisp_Object total_height = Qnil;
28227 it->override_ascent = -1;
28228 it->pixel_width = 0;
28229 it->nglyphs = 0;
28231 height = get_it_property (it, Qline_height);
28232 /* Split (line-height total-height) list. */
28233 if (CONSP (height)
28234 && CONSP (XCDR (height))
28235 && NILP (XCDR (XCDR (height))))
28237 total_height = XCAR (XCDR (height));
28238 height = XCAR (height);
28240 height = calc_line_height_property (it, height, font, boff, true);
28242 if (it->override_ascent >= 0)
28244 it->ascent = it->override_ascent;
28245 it->descent = it->override_descent;
28246 boff = it->override_boff;
28248 else
28250 if (FONT_TOO_HIGH (font))
28252 it->ascent = font->pixel_size + boff - 1;
28253 it->descent = -boff + 1;
28254 if (it->descent < 0)
28255 it->descent = 0;
28257 else
28259 it->ascent = FONT_BASE (font) + boff;
28260 it->descent = FONT_DESCENT (font) - boff;
28264 if (EQ (height, Qt))
28266 if (it->descent > it->max_descent)
28268 it->ascent += it->descent - it->max_descent;
28269 it->descent = it->max_descent;
28271 if (it->ascent > it->max_ascent)
28273 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28274 it->ascent = it->max_ascent;
28276 it->phys_ascent = min (it->phys_ascent, it->ascent);
28277 it->phys_descent = min (it->phys_descent, it->descent);
28278 it->constrain_row_ascent_descent_p = true;
28279 extra_line_spacing = 0;
28281 else
28283 Lisp_Object spacing;
28285 it->phys_ascent = it->ascent;
28286 it->phys_descent = it->descent;
28288 if ((it->max_ascent > 0 || it->max_descent > 0)
28289 && face->box != FACE_NO_BOX
28290 && face->box_line_width > 0)
28292 it->ascent += face->box_line_width;
28293 it->descent += face->box_line_width;
28295 if (!NILP (height)
28296 && XINT (height) > it->ascent + it->descent)
28297 it->ascent = XINT (height) - it->descent;
28299 if (!NILP (total_height))
28300 spacing = calc_line_height_property (it, total_height, font,
28301 boff, false);
28302 else
28304 spacing = get_it_property (it, Qline_spacing);
28305 spacing = calc_line_height_property (it, spacing, font,
28306 boff, false);
28308 if (INTEGERP (spacing))
28310 extra_line_spacing = XINT (spacing);
28311 if (!NILP (total_height))
28312 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28316 else /* i.e. (it->char_to_display == '\t') */
28318 if (font->space_width > 0)
28320 int tab_width = it->tab_width * font->space_width;
28321 int x = it->current_x + it->continuation_lines_width;
28322 int x0 = x;
28323 /* Adjust for line numbers, if needed. */
28324 if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
28326 x -= it->lnum_pixel_width;
28327 /* Restore the original TAB width, if required. */
28328 if (x + it->tab_offset >= it->first_visible_x)
28329 x += it->tab_offset;
28332 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28334 /* If the distance from the current position to the next tab
28335 stop is less than a space character width, use the
28336 tab stop after that. */
28337 if (next_tab_x - x < font->space_width)
28338 next_tab_x += tab_width;
28339 if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
28341 next_tab_x += it->lnum_pixel_width;
28342 /* If the line is hscrolled, and the TAB starts before
28343 the first visible pixel, simulate negative row->x. */
28344 if (x < it->first_visible_x)
28346 next_tab_x -= it->first_visible_x - x;
28347 it->tab_offset = it->first_visible_x - x;
28349 else
28350 next_tab_x -= it->tab_offset;
28353 it->pixel_width = next_tab_x - x0;
28354 it->nglyphs = 1;
28355 if (FONT_TOO_HIGH (font))
28357 if (get_char_glyph_code (' ', font, &char2b))
28359 pcm = get_per_char_metric (font, &char2b);
28360 if (pcm->width == 0
28361 && pcm->rbearing == 0 && pcm->lbearing == 0)
28362 pcm = NULL;
28365 if (pcm)
28367 it->ascent = pcm->ascent + boff;
28368 it->descent = pcm->descent - boff;
28370 else
28372 it->ascent = font->pixel_size + boff - 1;
28373 it->descent = -boff + 1;
28375 if (it->ascent < 0)
28376 it->ascent = 0;
28377 if (it->descent < 0)
28378 it->descent = 0;
28380 else
28382 it->ascent = FONT_BASE (font) + boff;
28383 it->descent = FONT_DESCENT (font) - boff;
28385 it->phys_ascent = it->ascent;
28386 it->phys_descent = it->descent;
28388 if (it->glyph_row)
28390 append_stretch_glyph (it, it->object, it->pixel_width,
28391 it->ascent + it->descent, it->ascent);
28394 else
28396 it->pixel_width = 0;
28397 it->nglyphs = 1;
28401 if (FONT_TOO_HIGH (font))
28403 int font_ascent, font_descent;
28405 /* For very large fonts, where we ignore the declared font
28406 dimensions, and go by per-character metrics instead,
28407 don't let the row ascent and descent values (and the row
28408 height computed from them) be smaller than the "normal"
28409 character metrics. This avoids unpleasant effects
28410 whereby lines on display would change their height
28411 depending on which characters are shown. */
28412 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28413 it->max_ascent = max (it->max_ascent, font_ascent);
28414 it->max_descent = max (it->max_descent, font_descent);
28417 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28419 /* A static composition.
28421 Note: A composition is represented as one glyph in the
28422 glyph matrix. There are no padding glyphs.
28424 Important note: pixel_width, ascent, and descent are the
28425 values of what is drawn by draw_glyphs (i.e. the values of
28426 the overall glyphs composed). */
28427 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28428 int boff; /* baseline offset */
28429 struct composition *cmp = composition_table[it->cmp_it.id];
28430 int glyph_len = cmp->glyph_len;
28431 struct font *font = face->font;
28433 it->nglyphs = 1;
28435 /* If we have not yet calculated pixel size data of glyphs of
28436 the composition for the current face font, calculate them
28437 now. Theoretically, we have to check all fonts for the
28438 glyphs, but that requires much time and memory space. So,
28439 here we check only the font of the first glyph. This may
28440 lead to incorrect display, but it's very rare, and C-l
28441 (recenter-top-bottom) can correct the display anyway. */
28442 if (! cmp->font || cmp->font != font)
28444 /* Ascent and descent of the font of the first character
28445 of this composition (adjusted by baseline offset).
28446 Ascent and descent of overall glyphs should not be less
28447 than these, respectively. */
28448 int font_ascent, font_descent, font_height;
28449 /* Bounding box of the overall glyphs. */
28450 int leftmost, rightmost, lowest, highest;
28451 int lbearing, rbearing;
28452 int i, width, ascent, descent;
28453 int c;
28454 XChar2b char2b;
28455 struct font_metrics *pcm;
28456 ptrdiff_t pos;
28458 eassume (0 < glyph_len); /* See Bug#8512. */
28460 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28461 while (c == '\t' && 0 < --glyph_len);
28463 bool right_padded = glyph_len < cmp->glyph_len;
28464 for (i = 0; i < glyph_len; i++)
28466 c = COMPOSITION_GLYPH (cmp, i);
28467 if (c != '\t')
28468 break;
28469 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28471 bool left_padded = i > 0;
28473 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28474 : IT_CHARPOS (*it));
28475 /* If no suitable font is found, use the default font. */
28476 bool font_not_found_p = font == NULL;
28477 if (font_not_found_p)
28479 face = face->ascii_face;
28480 font = face->font;
28482 boff = font->baseline_offset;
28483 if (font->vertical_centering)
28484 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28485 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28486 font_ascent += boff;
28487 font_descent -= boff;
28488 font_height = font_ascent + font_descent;
28490 cmp->font = font;
28492 pcm = NULL;
28493 if (! font_not_found_p)
28495 get_char_face_and_encoding (it->f, c, it->face_id,
28496 &char2b, false);
28497 pcm = get_per_char_metric (font, &char2b);
28500 /* Initialize the bounding box. */
28501 if (pcm)
28503 width = cmp->glyph_len > 0 ? pcm->width : 0;
28504 ascent = pcm->ascent;
28505 descent = pcm->descent;
28506 lbearing = pcm->lbearing;
28507 rbearing = pcm->rbearing;
28509 else
28511 width = cmp->glyph_len > 0 ? font->space_width : 0;
28512 ascent = FONT_BASE (font);
28513 descent = FONT_DESCENT (font);
28514 lbearing = 0;
28515 rbearing = width;
28518 rightmost = width;
28519 leftmost = 0;
28520 lowest = - descent + boff;
28521 highest = ascent + boff;
28523 if (! font_not_found_p
28524 && font->default_ascent
28525 && CHAR_TABLE_P (Vuse_default_ascent)
28526 && !NILP (Faref (Vuse_default_ascent,
28527 make_number (it->char_to_display))))
28528 highest = font->default_ascent + boff;
28530 /* Draw the first glyph at the normal position. It may be
28531 shifted to right later if some other glyphs are drawn
28532 at the left. */
28533 cmp->offsets[i * 2] = 0;
28534 cmp->offsets[i * 2 + 1] = boff;
28535 cmp->lbearing = lbearing;
28536 cmp->rbearing = rbearing;
28538 /* Set cmp->offsets for the remaining glyphs. */
28539 for (i++; i < glyph_len; i++)
28541 int left, right, btm, top;
28542 int ch = COMPOSITION_GLYPH (cmp, i);
28543 int face_id;
28544 struct face *this_face;
28546 if (ch == '\t')
28547 ch = ' ';
28548 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28549 this_face = FACE_FROM_ID (it->f, face_id);
28550 font = this_face->font;
28552 if (font == NULL)
28553 pcm = NULL;
28554 else
28556 get_char_face_and_encoding (it->f, ch, face_id,
28557 &char2b, false);
28558 pcm = get_per_char_metric (font, &char2b);
28560 if (! pcm)
28561 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28562 else
28564 width = pcm->width;
28565 ascent = pcm->ascent;
28566 descent = pcm->descent;
28567 lbearing = pcm->lbearing;
28568 rbearing = pcm->rbearing;
28569 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28571 /* Relative composition with or without
28572 alternate chars. */
28573 left = (leftmost + rightmost - width) / 2;
28574 btm = - descent + boff;
28575 if (font->relative_compose
28576 && (! CHAR_TABLE_P (Vignore_relative_composition)
28577 || NILP (Faref (Vignore_relative_composition,
28578 make_number (ch)))))
28581 if (- descent >= font->relative_compose)
28582 /* One extra pixel between two glyphs. */
28583 btm = highest + 1;
28584 else if (ascent <= 0)
28585 /* One extra pixel between two glyphs. */
28586 btm = lowest - 1 - ascent - descent;
28589 else
28591 /* A composition rule is specified by an integer
28592 value that encodes global and new reference
28593 points (GREF and NREF). GREF and NREF are
28594 specified by numbers as below:
28596 0---1---2 -- ascent
28600 9--10--11 -- center
28602 ---3---4---5--- baseline
28604 6---7---8 -- descent
28606 int rule = COMPOSITION_RULE (cmp, i);
28607 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28609 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28610 grefx = gref % 3, nrefx = nref % 3;
28611 grefy = gref / 3, nrefy = nref / 3;
28612 if (xoff)
28613 xoff = font_height * (xoff - 128) / 256;
28614 if (yoff)
28615 yoff = font_height * (yoff - 128) / 256;
28617 left = (leftmost
28618 + grefx * (rightmost - leftmost) / 2
28619 - nrefx * width / 2
28620 + xoff);
28622 btm = ((grefy == 0 ? highest
28623 : grefy == 1 ? 0
28624 : grefy == 2 ? lowest
28625 : (highest + lowest) / 2)
28626 - (nrefy == 0 ? ascent + descent
28627 : nrefy == 1 ? descent - boff
28628 : nrefy == 2 ? 0
28629 : (ascent + descent) / 2)
28630 + yoff);
28633 cmp->offsets[i * 2] = left;
28634 cmp->offsets[i * 2 + 1] = btm + descent;
28636 /* Update the bounding box of the overall glyphs. */
28637 if (width > 0)
28639 right = left + width;
28640 if (left < leftmost)
28641 leftmost = left;
28642 if (right > rightmost)
28643 rightmost = right;
28645 top = btm + descent + ascent;
28646 if (top > highest)
28647 highest = top;
28648 if (btm < lowest)
28649 lowest = btm;
28651 if (cmp->lbearing > left + lbearing)
28652 cmp->lbearing = left + lbearing;
28653 if (cmp->rbearing < left + rbearing)
28654 cmp->rbearing = left + rbearing;
28658 /* If there are glyphs whose x-offsets are negative,
28659 shift all glyphs to the right and make all x-offsets
28660 non-negative. */
28661 if (leftmost < 0)
28663 for (i = 0; i < cmp->glyph_len; i++)
28664 cmp->offsets[i * 2] -= leftmost;
28665 rightmost -= leftmost;
28666 cmp->lbearing -= leftmost;
28667 cmp->rbearing -= leftmost;
28670 if (left_padded && cmp->lbearing < 0)
28672 for (i = 0; i < cmp->glyph_len; i++)
28673 cmp->offsets[i * 2] -= cmp->lbearing;
28674 rightmost -= cmp->lbearing;
28675 cmp->rbearing -= cmp->lbearing;
28676 cmp->lbearing = 0;
28678 if (right_padded && rightmost < cmp->rbearing)
28680 rightmost = cmp->rbearing;
28683 cmp->pixel_width = rightmost;
28684 cmp->ascent = highest;
28685 cmp->descent = - lowest;
28686 if (cmp->ascent < font_ascent)
28687 cmp->ascent = font_ascent;
28688 if (cmp->descent < font_descent)
28689 cmp->descent = font_descent;
28692 if (it->glyph_row
28693 && (cmp->lbearing < 0
28694 || cmp->rbearing > cmp->pixel_width))
28695 it->glyph_row->contains_overlapping_glyphs_p = true;
28697 it->pixel_width = cmp->pixel_width;
28698 it->ascent = it->phys_ascent = cmp->ascent;
28699 it->descent = it->phys_descent = cmp->descent;
28700 if (face->box != FACE_NO_BOX)
28702 int thick = face->box_line_width;
28704 if (thick > 0)
28706 it->ascent += thick;
28707 it->descent += thick;
28709 else
28710 thick = - thick;
28712 if (it->start_of_box_run_p)
28713 it->pixel_width += thick;
28714 if (it->end_of_box_run_p)
28715 it->pixel_width += thick;
28718 /* If face has an overline, add the height of the overline
28719 (1 pixel) and a 1 pixel margin to the character height. */
28720 if (face->overline_p)
28721 it->ascent += overline_margin;
28723 take_vertical_position_into_account (it);
28724 if (it->ascent < 0)
28725 it->ascent = 0;
28726 if (it->descent < 0)
28727 it->descent = 0;
28729 if (it->glyph_row && cmp->glyph_len > 0)
28730 append_composite_glyph (it);
28732 else if (it->what == IT_COMPOSITION)
28734 /* A dynamic (automatic) composition. */
28735 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28736 Lisp_Object gstring;
28737 struct font_metrics metrics;
28739 it->nglyphs = 1;
28741 gstring = composition_gstring_from_id (it->cmp_it.id);
28742 it->pixel_width
28743 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28744 &metrics);
28745 if (it->glyph_row
28746 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28747 it->glyph_row->contains_overlapping_glyphs_p = true;
28748 it->ascent = it->phys_ascent = metrics.ascent;
28749 it->descent = it->phys_descent = metrics.descent;
28750 if (face->box != FACE_NO_BOX)
28752 int thick = face->box_line_width;
28754 if (thick > 0)
28756 it->ascent += thick;
28757 it->descent += thick;
28759 else
28760 thick = - thick;
28762 if (it->start_of_box_run_p)
28763 it->pixel_width += thick;
28764 if (it->end_of_box_run_p)
28765 it->pixel_width += thick;
28767 /* If face has an overline, add the height of the overline
28768 (1 pixel) and a 1 pixel margin to the character height. */
28769 if (face->overline_p)
28770 it->ascent += overline_margin;
28771 take_vertical_position_into_account (it);
28772 if (it->ascent < 0)
28773 it->ascent = 0;
28774 if (it->descent < 0)
28775 it->descent = 0;
28777 if (it->glyph_row)
28778 append_composite_glyph (it);
28780 else if (it->what == IT_GLYPHLESS)
28781 produce_glyphless_glyph (it, false, Qnil);
28782 else if (it->what == IT_IMAGE)
28783 produce_image_glyph (it);
28784 else if (it->what == IT_STRETCH)
28785 produce_stretch_glyph (it);
28786 else if (it->what == IT_XWIDGET)
28787 produce_xwidget_glyph (it);
28789 done:
28790 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28791 because this isn't true for images with `:ascent 100'. */
28792 eassert (it->ascent >= 0 && it->descent >= 0);
28793 if (it->area == TEXT_AREA)
28794 it->current_x += it->pixel_width;
28796 if (extra_line_spacing > 0)
28798 it->descent += extra_line_spacing;
28799 if (extra_line_spacing > it->max_extra_line_spacing)
28800 it->max_extra_line_spacing = extra_line_spacing;
28803 it->max_ascent = max (it->max_ascent, it->ascent);
28804 it->max_descent = max (it->max_descent, it->descent);
28805 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28806 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28809 /* EXPORT for RIF:
28810 Output LEN glyphs starting at START at the nominal cursor position.
28811 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28812 being updated, and UPDATED_AREA is the area of that row being updated. */
28814 void
28815 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28816 struct glyph *start, enum glyph_row_area updated_area, int len)
28818 int x, hpos, chpos = w->phys_cursor.hpos;
28820 eassert (updated_row);
28821 /* When the window is hscrolled, cursor hpos can legitimately be out
28822 of bounds, but we draw the cursor at the corresponding window
28823 margin in that case. */
28824 if (!updated_row->reversed_p && chpos < 0)
28825 chpos = 0;
28826 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28827 chpos = updated_row->used[TEXT_AREA] - 1;
28829 block_input ();
28831 /* Write glyphs. */
28833 hpos = start - updated_row->glyphs[updated_area];
28834 x = draw_glyphs (w, w->output_cursor.x,
28835 updated_row, updated_area,
28836 hpos, hpos + len,
28837 DRAW_NORMAL_TEXT, 0);
28839 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28840 if (updated_area == TEXT_AREA
28841 && w->phys_cursor_on_p
28842 && w->phys_cursor.vpos == w->output_cursor.vpos
28843 && chpos >= hpos
28844 && chpos < hpos + len)
28845 w->phys_cursor_on_p = false;
28847 unblock_input ();
28849 /* Advance the output cursor. */
28850 w->output_cursor.hpos += len;
28851 w->output_cursor.x = x;
28855 /* EXPORT for RIF:
28856 Insert LEN glyphs from START at the nominal cursor position. */
28858 void
28859 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28860 struct glyph *start, enum glyph_row_area updated_area, int len)
28862 struct frame *f;
28863 int line_height, shift_by_width, shifted_region_width;
28864 struct glyph_row *row;
28865 struct glyph *glyph;
28866 int frame_x, frame_y;
28867 ptrdiff_t hpos;
28869 eassert (updated_row);
28870 block_input ();
28871 f = XFRAME (WINDOW_FRAME (w));
28873 /* Get the height of the line we are in. */
28874 row = updated_row;
28875 line_height = row->height;
28877 /* Get the width of the glyphs to insert. */
28878 shift_by_width = 0;
28879 for (glyph = start; glyph < start + len; ++glyph)
28880 shift_by_width += glyph->pixel_width;
28882 /* Get the width of the region to shift right. */
28883 shifted_region_width = (window_box_width (w, updated_area)
28884 - w->output_cursor.x
28885 - shift_by_width);
28887 /* Shift right. */
28888 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28889 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28891 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28892 line_height, shift_by_width);
28894 /* Write the glyphs. */
28895 hpos = start - row->glyphs[updated_area];
28896 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28897 hpos, hpos + len,
28898 DRAW_NORMAL_TEXT, 0);
28900 /* Advance the output cursor. */
28901 w->output_cursor.hpos += len;
28902 w->output_cursor.x += shift_by_width;
28903 unblock_input ();
28907 /* EXPORT for RIF:
28908 Erase the current text line from the nominal cursor position
28909 (inclusive) to pixel column TO_X (exclusive). The idea is that
28910 everything from TO_X onward is already erased.
28912 TO_X is a pixel position relative to UPDATED_AREA of currently
28913 updated window W. TO_X == -1 means clear to the end of this area. */
28915 void
28916 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28917 enum glyph_row_area updated_area, int to_x)
28919 struct frame *f;
28920 int max_x, min_y, max_y;
28921 int from_x, from_y, to_y;
28923 eassert (updated_row);
28924 f = XFRAME (w->frame);
28926 if (updated_row->full_width_p)
28927 max_x = (WINDOW_PIXEL_WIDTH (w)
28928 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28929 else
28930 max_x = window_box_width (w, updated_area);
28931 max_y = window_text_bottom_y (w);
28933 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28934 of window. For TO_X > 0, truncate to end of drawing area. */
28935 if (to_x == 0)
28936 return;
28937 else if (to_x < 0)
28938 to_x = max_x;
28939 else
28940 to_x = min (to_x, max_x);
28942 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28944 /* Notice if the cursor will be cleared by this operation. */
28945 if (!updated_row->full_width_p)
28946 notice_overwritten_cursor (w, updated_area,
28947 w->output_cursor.x, -1,
28948 updated_row->y,
28949 MATRIX_ROW_BOTTOM_Y (updated_row));
28951 from_x = w->output_cursor.x;
28953 /* Translate to frame coordinates. */
28954 if (updated_row->full_width_p)
28956 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28957 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28959 else
28961 int area_left = window_box_left (w, updated_area);
28962 from_x += area_left;
28963 to_x += area_left;
28966 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28967 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28968 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28970 /* Prevent inadvertently clearing to end of the X window. */
28971 if (to_x > from_x && to_y > from_y)
28973 block_input ();
28974 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28975 to_x - from_x, to_y - from_y);
28976 unblock_input ();
28980 #endif /* HAVE_WINDOW_SYSTEM */
28984 /***********************************************************************
28985 Cursor types
28986 ***********************************************************************/
28988 /* Value is the internal representation of the specified cursor type
28989 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28990 of the bar cursor. */
28992 static enum text_cursor_kinds
28993 get_specified_cursor_type (Lisp_Object arg, int *width)
28995 enum text_cursor_kinds type;
28997 if (NILP (arg))
28998 return NO_CURSOR;
29000 if (EQ (arg, Qbox))
29001 return FILLED_BOX_CURSOR;
29003 if (EQ (arg, Qhollow))
29004 return HOLLOW_BOX_CURSOR;
29006 if (EQ (arg, Qbar))
29008 *width = 2;
29009 return BAR_CURSOR;
29012 if (CONSP (arg)
29013 && EQ (XCAR (arg), Qbar)
29014 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
29016 *width = XINT (XCDR (arg));
29017 return BAR_CURSOR;
29020 if (EQ (arg, Qhbar))
29022 *width = 2;
29023 return HBAR_CURSOR;
29026 if (CONSP (arg)
29027 && EQ (XCAR (arg), Qhbar)
29028 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
29030 *width = XINT (XCDR (arg));
29031 return HBAR_CURSOR;
29034 /* Treat anything unknown as "hollow box cursor".
29035 It was bad to signal an error; people have trouble fixing
29036 .Xdefaults with Emacs, when it has something bad in it. */
29037 type = HOLLOW_BOX_CURSOR;
29039 return type;
29042 /* Set the default cursor types for specified frame. */
29043 void
29044 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
29046 int width = 1;
29047 Lisp_Object tem;
29049 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
29050 FRAME_CURSOR_WIDTH (f) = width;
29052 /* By default, set up the blink-off state depending on the on-state. */
29054 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
29055 if (!NILP (tem))
29057 FRAME_BLINK_OFF_CURSOR (f)
29058 = get_specified_cursor_type (XCDR (tem), &width);
29059 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
29061 else
29062 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
29064 /* Make sure the cursor gets redrawn. */
29065 f->cursor_type_changed = true;
29069 #ifdef HAVE_WINDOW_SYSTEM
29071 /* Return the cursor we want to be displayed in window W. Return
29072 width of bar/hbar cursor through WIDTH arg. Return with
29073 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
29074 (i.e. if the `system caret' should track this cursor).
29076 In a mini-buffer window, we want the cursor only to appear if we
29077 are reading input from this window. For the selected window, we
29078 want the cursor type given by the frame parameter or buffer local
29079 setting of cursor-type. If explicitly marked off, draw no cursor.
29080 In all other cases, we want a hollow box cursor. */
29082 static enum text_cursor_kinds
29083 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
29084 bool *active_cursor)
29086 struct frame *f = XFRAME (w->frame);
29087 struct buffer *b = XBUFFER (w->contents);
29088 int cursor_type = DEFAULT_CURSOR;
29089 Lisp_Object alt_cursor;
29090 bool non_selected = false;
29092 *active_cursor = true;
29094 /* Echo area */
29095 if (cursor_in_echo_area
29096 && FRAME_HAS_MINIBUF_P (f)
29097 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
29099 if (w == XWINDOW (echo_area_window))
29101 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
29103 *width = FRAME_CURSOR_WIDTH (f);
29104 return FRAME_DESIRED_CURSOR (f);
29106 else
29107 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29110 *active_cursor = false;
29111 non_selected = true;
29114 /* Detect a nonselected window or nonselected frame. */
29115 else if (w != XWINDOW (f->selected_window)
29116 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29118 *active_cursor = false;
29120 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29121 return NO_CURSOR;
29123 non_selected = true;
29126 /* Never display a cursor in a window in which cursor-type is nil. */
29127 if (NILP (BVAR (b, cursor_type)))
29128 return NO_CURSOR;
29130 /* Get the normal cursor type for this window. */
29131 if (EQ (BVAR (b, cursor_type), Qt))
29133 cursor_type = FRAME_DESIRED_CURSOR (f);
29134 *width = FRAME_CURSOR_WIDTH (f);
29136 else
29137 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29139 /* Use cursor-in-non-selected-windows instead
29140 for non-selected window or frame. */
29141 if (non_selected)
29143 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29144 if (!EQ (Qt, alt_cursor))
29145 return get_specified_cursor_type (alt_cursor, width);
29146 /* t means modify the normal cursor type. */
29147 if (cursor_type == FILLED_BOX_CURSOR)
29148 cursor_type = HOLLOW_BOX_CURSOR;
29149 else if (cursor_type == BAR_CURSOR && *width > 1)
29150 --*width;
29151 return cursor_type;
29154 /* Use normal cursor if not blinked off. */
29155 if (!w->cursor_off_p)
29157 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29158 return NO_CURSOR;
29159 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29161 if (cursor_type == FILLED_BOX_CURSOR)
29163 /* Using a block cursor on large images can be very annoying.
29164 So use a hollow cursor for "large" images.
29165 If image is not transparent (no mask), also use hollow cursor. */
29166 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29167 if (img != NULL && IMAGEP (img->spec))
29169 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29170 where N = size of default frame font size.
29171 This should cover most of the "tiny" icons people may use. */
29172 if (!img->mask
29173 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29174 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29175 cursor_type = HOLLOW_BOX_CURSOR;
29178 else if (cursor_type != NO_CURSOR)
29180 /* Display current only supports BOX and HOLLOW cursors for images.
29181 So for now, unconditionally use a HOLLOW cursor when cursor is
29182 not a solid box cursor. */
29183 cursor_type = HOLLOW_BOX_CURSOR;
29186 return cursor_type;
29189 /* Cursor is blinked off, so determine how to "toggle" it. */
29191 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29192 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29193 return get_specified_cursor_type (XCDR (alt_cursor), width);
29195 /* Then see if frame has specified a specific blink off cursor type. */
29196 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29198 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29199 return FRAME_BLINK_OFF_CURSOR (f);
29202 #if false
29203 /* Some people liked having a permanently visible blinking cursor,
29204 while others had very strong opinions against it. So it was
29205 decided to remove it. KFS 2003-09-03 */
29207 /* Finally perform built-in cursor blinking:
29208 filled box <-> hollow box
29209 wide [h]bar <-> narrow [h]bar
29210 narrow [h]bar <-> no cursor
29211 other type <-> no cursor */
29213 if (cursor_type == FILLED_BOX_CURSOR)
29214 return HOLLOW_BOX_CURSOR;
29216 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29218 *width = 1;
29219 return cursor_type;
29221 #endif
29223 return NO_CURSOR;
29227 /* Notice when the text cursor of window W has been completely
29228 overwritten by a drawing operation that outputs glyphs in AREA
29229 starting at X0 and ending at X1 in the line starting at Y0 and
29230 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29231 the rest of the line after X0 has been written. Y coordinates
29232 are window-relative. */
29234 static void
29235 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29236 int x0, int x1, int y0, int y1)
29238 int cx0, cx1, cy0, cy1;
29239 struct glyph_row *row;
29241 if (!w->phys_cursor_on_p)
29242 return;
29243 if (area != TEXT_AREA)
29244 return;
29246 if (w->phys_cursor.vpos < 0
29247 || w->phys_cursor.vpos >= w->current_matrix->nrows
29248 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29249 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29250 return;
29252 if (row->cursor_in_fringe_p)
29254 row->cursor_in_fringe_p = false;
29255 draw_fringe_bitmap (w, row, row->reversed_p);
29256 w->phys_cursor_on_p = false;
29257 return;
29260 cx0 = w->phys_cursor.x;
29261 cx1 = cx0 + w->phys_cursor_width;
29262 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29263 return;
29265 /* The cursor image will be completely removed from the
29266 screen if the output area intersects the cursor area in
29267 y-direction. When we draw in [y0 y1[, and some part of
29268 the cursor is at y < y0, that part must have been drawn
29269 before. When scrolling, the cursor is erased before
29270 actually scrolling, so we don't come here. When not
29271 scrolling, the rows above the old cursor row must have
29272 changed, and in this case these rows must have written
29273 over the cursor image.
29275 Likewise if part of the cursor is below y1, with the
29276 exception of the cursor being in the first blank row at
29277 the buffer and window end because update_text_area
29278 doesn't draw that row. (Except when it does, but
29279 that's handled in update_text_area.) */
29281 cy0 = w->phys_cursor.y;
29282 cy1 = cy0 + w->phys_cursor_height;
29283 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29284 return;
29286 w->phys_cursor_on_p = false;
29289 #endif /* HAVE_WINDOW_SYSTEM */
29292 /************************************************************************
29293 Mouse Face
29294 ************************************************************************/
29296 #ifdef HAVE_WINDOW_SYSTEM
29298 /* EXPORT for RIF:
29299 Fix the display of area AREA of overlapping row ROW in window W
29300 with respect to the overlapping part OVERLAPS. */
29302 void
29303 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29304 enum glyph_row_area area, int overlaps)
29306 int i, x;
29308 block_input ();
29310 x = 0;
29311 for (i = 0; i < row->used[area];)
29313 if (row->glyphs[area][i].overlaps_vertically_p)
29315 int start = i, start_x = x;
29319 x += row->glyphs[area][i].pixel_width;
29320 ++i;
29322 while (i < row->used[area]
29323 && row->glyphs[area][i].overlaps_vertically_p);
29325 draw_glyphs (w, start_x, row, area,
29326 start, i,
29327 DRAW_NORMAL_TEXT, overlaps);
29329 else
29331 x += row->glyphs[area][i].pixel_width;
29332 ++i;
29336 unblock_input ();
29340 /* EXPORT:
29341 Draw the cursor glyph of window W in glyph row ROW. See the
29342 comment of draw_glyphs for the meaning of HL. */
29344 void
29345 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29346 enum draw_glyphs_face hl)
29348 /* If cursor hpos is out of bounds, don't draw garbage. This can
29349 happen in mini-buffer windows when switching between echo area
29350 glyphs and mini-buffer. */
29351 if ((row->reversed_p
29352 ? (w->phys_cursor.hpos >= 0)
29353 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29355 bool on_p = w->phys_cursor_on_p;
29356 int x1;
29357 int hpos = w->phys_cursor.hpos;
29359 /* When the window is hscrolled, cursor hpos can legitimately be
29360 out of bounds, but we draw the cursor at the corresponding
29361 window margin in that case. */
29362 if (!row->reversed_p && hpos < 0)
29363 hpos = 0;
29364 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29365 hpos = row->used[TEXT_AREA] - 1;
29367 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29368 hl, 0);
29369 w->phys_cursor_on_p = on_p;
29371 if (hl == DRAW_CURSOR)
29372 w->phys_cursor_width = x1 - w->phys_cursor.x;
29373 /* When we erase the cursor, and ROW is overlapped by other
29374 rows, make sure that these overlapping parts of other rows
29375 are redrawn. */
29376 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29378 w->phys_cursor_width = x1 - w->phys_cursor.x;
29380 if (row > w->current_matrix->rows
29381 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29382 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29383 OVERLAPS_ERASED_CURSOR);
29385 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29386 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29387 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29388 OVERLAPS_ERASED_CURSOR);
29394 /* Erase the image of a cursor of window W from the screen. */
29396 void
29397 erase_phys_cursor (struct window *w)
29399 struct frame *f = XFRAME (w->frame);
29400 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29401 int hpos = w->phys_cursor.hpos;
29402 int vpos = w->phys_cursor.vpos;
29403 bool mouse_face_here_p = false;
29404 struct glyph_matrix *active_glyphs = w->current_matrix;
29405 struct glyph_row *cursor_row;
29406 struct glyph *cursor_glyph;
29407 enum draw_glyphs_face hl;
29409 /* No cursor displayed or row invalidated => nothing to do on the
29410 screen. */
29411 if (w->phys_cursor_type == NO_CURSOR)
29412 goto mark_cursor_off;
29414 /* VPOS >= active_glyphs->nrows means that window has been resized.
29415 Don't bother to erase the cursor. */
29416 if (vpos >= active_glyphs->nrows)
29417 goto mark_cursor_off;
29419 /* If row containing cursor is marked invalid, there is nothing we
29420 can do. */
29421 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29422 if (!cursor_row->enabled_p)
29423 goto mark_cursor_off;
29425 /* If line spacing is > 0, old cursor may only be partially visible in
29426 window after split-window. So adjust visible height. */
29427 cursor_row->visible_height = min (cursor_row->visible_height,
29428 window_text_bottom_y (w) - cursor_row->y);
29430 /* If row is completely invisible, don't attempt to delete a cursor which
29431 isn't there. This can happen if cursor is at top of a window, and
29432 we switch to a buffer with a header line in that window. */
29433 if (cursor_row->visible_height <= 0)
29434 goto mark_cursor_off;
29436 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29437 if (cursor_row->cursor_in_fringe_p)
29439 cursor_row->cursor_in_fringe_p = false;
29440 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29441 goto mark_cursor_off;
29444 /* This can happen when the new row is shorter than the old one.
29445 In this case, either draw_glyphs or clear_end_of_line
29446 should have cleared the cursor. Note that we wouldn't be
29447 able to erase the cursor in this case because we don't have a
29448 cursor glyph at hand. */
29449 if ((cursor_row->reversed_p
29450 ? (w->phys_cursor.hpos < 0)
29451 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29452 goto mark_cursor_off;
29454 /* When the window is hscrolled, cursor hpos can legitimately be out
29455 of bounds, but we draw the cursor at the corresponding window
29456 margin in that case. */
29457 if (!cursor_row->reversed_p && hpos < 0)
29458 hpos = 0;
29459 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29460 hpos = cursor_row->used[TEXT_AREA] - 1;
29462 /* If the cursor is in the mouse face area, redisplay that when
29463 we clear the cursor. */
29464 if (! NILP (hlinfo->mouse_face_window)
29465 && coords_in_mouse_face_p (w, hpos, vpos)
29466 /* Don't redraw the cursor's spot in mouse face if it is at the
29467 end of a line (on a newline). The cursor appears there, but
29468 mouse highlighting does not. */
29469 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29470 mouse_face_here_p = true;
29472 /* Maybe clear the display under the cursor. */
29473 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29475 int x, y;
29476 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29477 int width;
29479 cursor_glyph = get_phys_cursor_glyph (w);
29480 if (cursor_glyph == NULL)
29481 goto mark_cursor_off;
29483 width = cursor_glyph->pixel_width;
29484 x = w->phys_cursor.x;
29485 if (x < 0)
29487 width += x;
29488 x = 0;
29490 width = min (width, window_box_width (w, TEXT_AREA) - x);
29491 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29492 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29494 if (width > 0)
29495 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29498 /* Erase the cursor by redrawing the character underneath it. */
29499 if (mouse_face_here_p)
29500 hl = DRAW_MOUSE_FACE;
29501 else
29502 hl = DRAW_NORMAL_TEXT;
29503 draw_phys_cursor_glyph (w, cursor_row, hl);
29505 mark_cursor_off:
29506 w->phys_cursor_on_p = false;
29507 w->phys_cursor_type = NO_CURSOR;
29511 /* Display or clear cursor of window W. If !ON, clear the cursor.
29512 If ON, display the cursor; where to put the cursor is specified by
29513 HPOS, VPOS, X and Y. */
29515 void
29516 display_and_set_cursor (struct window *w, bool on,
29517 int hpos, int vpos, int x, int y)
29519 struct frame *f = XFRAME (w->frame);
29520 int new_cursor_type;
29521 int new_cursor_width;
29522 bool active_cursor;
29523 struct glyph_row *glyph_row;
29524 struct glyph *glyph;
29526 /* This is pointless on invisible frames, and dangerous on garbaged
29527 windows and frames; in the latter case, the frame or window may
29528 be in the midst of changing its size, and x and y may be off the
29529 window. */
29530 if (! FRAME_VISIBLE_P (f)
29531 || vpos >= w->current_matrix->nrows
29532 || hpos >= w->current_matrix->matrix_w)
29533 return;
29535 /* If cursor is off and we want it off, return quickly. */
29536 if (!on && !w->phys_cursor_on_p)
29537 return;
29539 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29540 /* If cursor row is not enabled, we don't really know where to
29541 display the cursor. */
29542 if (!glyph_row->enabled_p)
29544 w->phys_cursor_on_p = false;
29545 return;
29548 /* A frame might be marked garbaged even though its cursor position
29549 is correct, and will not change upon subsequent redisplay. This
29550 happens in some rare situations, like toggling the sort order in
29551 Dired windows. We've already established that VPOS is valid, so
29552 it shouldn't do any harm to record the cursor position, as we are
29553 going to return without acting on it anyway. Otherwise, expose
29554 events might come in and call update_window_cursor, which will
29555 blindly use outdated values in w->phys_cursor. */
29556 if (FRAME_GARBAGED_P (f))
29558 if (on)
29560 w->phys_cursor.x = x;
29561 w->phys_cursor.y = glyph_row->y;
29562 w->phys_cursor.hpos = hpos;
29563 w->phys_cursor.vpos = vpos;
29565 return;
29568 glyph = NULL;
29569 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29570 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29572 eassert (input_blocked_p ());
29574 /* Set new_cursor_type to the cursor we want to be displayed. */
29575 new_cursor_type = get_window_cursor_type (w, glyph,
29576 &new_cursor_width, &active_cursor);
29578 /* If cursor is currently being shown and we don't want it to be or
29579 it is in the wrong place, or the cursor type is not what we want,
29580 erase it. */
29581 if (w->phys_cursor_on_p
29582 && (!on
29583 || w->phys_cursor.x != x
29584 || w->phys_cursor.y != y
29585 /* HPOS can be negative in R2L rows whose
29586 exact_window_width_line_p flag is set (i.e. their newline
29587 would "overflow into the fringe"). */
29588 || hpos < 0
29589 || new_cursor_type != w->phys_cursor_type
29590 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29591 && new_cursor_width != w->phys_cursor_width)))
29592 erase_phys_cursor (w);
29594 /* Don't check phys_cursor_on_p here because that flag is only set
29595 to false in some cases where we know that the cursor has been
29596 completely erased, to avoid the extra work of erasing the cursor
29597 twice. In other words, phys_cursor_on_p can be true and the cursor
29598 still not be visible, or it has only been partly erased. */
29599 if (on)
29601 w->phys_cursor_ascent = glyph_row->ascent;
29602 w->phys_cursor_height = glyph_row->height;
29604 /* Set phys_cursor_.* before x_draw_.* is called because some
29605 of them may need the information. */
29606 w->phys_cursor.x = x;
29607 w->phys_cursor.y = glyph_row->y;
29608 w->phys_cursor.hpos = hpos;
29609 w->phys_cursor.vpos = vpos;
29612 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29613 new_cursor_type, new_cursor_width,
29614 on, active_cursor);
29618 /* Switch the display of W's cursor on or off, according to the value
29619 of ON. */
29621 static void
29622 update_window_cursor (struct window *w, bool on)
29624 /* Don't update cursor in windows whose frame is in the process
29625 of being deleted. */
29626 if (w->current_matrix)
29628 int hpos = w->phys_cursor.hpos;
29629 int vpos = w->phys_cursor.vpos;
29630 struct glyph_row *row;
29632 if (vpos >= w->current_matrix->nrows
29633 || hpos >= w->current_matrix->matrix_w)
29634 return;
29636 row = MATRIX_ROW (w->current_matrix, vpos);
29638 /* When the window is hscrolled, cursor hpos can legitimately be
29639 out of bounds, but we draw the cursor at the corresponding
29640 window margin in that case. */
29641 if (!row->reversed_p && hpos < 0)
29642 hpos = 0;
29643 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29644 hpos = row->used[TEXT_AREA] - 1;
29646 block_input ();
29647 display_and_set_cursor (w, on, hpos, vpos,
29648 w->phys_cursor.x, w->phys_cursor.y);
29649 unblock_input ();
29654 /* Call update_window_cursor with parameter ON_P on all leaf windows
29655 in the window tree rooted at W. */
29657 static void
29658 update_cursor_in_window_tree (struct window *w, bool on_p)
29660 while (w)
29662 if (WINDOWP (w->contents))
29663 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29664 else
29665 update_window_cursor (w, on_p);
29667 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29672 /* EXPORT:
29673 Display the cursor on window W, or clear it, according to ON_P.
29674 Don't change the cursor's position. */
29676 void
29677 x_update_cursor (struct frame *f, bool on_p)
29679 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29683 /* EXPORT:
29684 Clear the cursor of window W to background color, and mark the
29685 cursor as not shown. This is used when the text where the cursor
29686 is about to be rewritten. */
29688 void
29689 x_clear_cursor (struct window *w)
29691 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29692 update_window_cursor (w, false);
29695 #endif /* HAVE_WINDOW_SYSTEM */
29697 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29698 and MSDOS. */
29699 static void
29700 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29701 int start_hpos, int end_hpos,
29702 enum draw_glyphs_face draw)
29704 #ifdef HAVE_WINDOW_SYSTEM
29705 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29707 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29708 return;
29710 #endif
29711 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29712 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29713 #endif
29716 /* Display the active region described by mouse_face_* according to DRAW. */
29718 static void
29719 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29721 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29722 struct frame *f = XFRAME (WINDOW_FRAME (w));
29724 if (/* If window is in the process of being destroyed, don't bother
29725 to do anything. */
29726 w->current_matrix != NULL
29727 /* Don't update mouse highlight if hidden. */
29728 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29729 /* Recognize when we are called to operate on rows that don't exist
29730 anymore. This can happen when a window is split. */
29731 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29733 bool phys_cursor_on_p = w->phys_cursor_on_p;
29734 struct glyph_row *row, *first, *last;
29736 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29737 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29739 for (row = first; row <= last && row->enabled_p; ++row)
29741 int start_hpos, end_hpos, start_x;
29743 /* For all but the first row, the highlight starts at column 0. */
29744 if (row == first)
29746 /* R2L rows have BEG and END in reversed order, but the
29747 screen drawing geometry is always left to right. So
29748 we need to mirror the beginning and end of the
29749 highlighted area in R2L rows. */
29750 if (!row->reversed_p)
29752 start_hpos = hlinfo->mouse_face_beg_col;
29753 start_x = hlinfo->mouse_face_beg_x;
29755 else if (row == last)
29757 start_hpos = hlinfo->mouse_face_end_col;
29758 start_x = hlinfo->mouse_face_end_x;
29760 else
29762 start_hpos = 0;
29763 start_x = 0;
29766 else if (row->reversed_p && row == last)
29768 start_hpos = hlinfo->mouse_face_end_col;
29769 start_x = hlinfo->mouse_face_end_x;
29771 else
29773 start_hpos = 0;
29774 start_x = 0;
29777 if (row == last)
29779 if (!row->reversed_p)
29780 end_hpos = hlinfo->mouse_face_end_col;
29781 else if (row == first)
29782 end_hpos = hlinfo->mouse_face_beg_col;
29783 else
29785 end_hpos = row->used[TEXT_AREA];
29786 if (draw == DRAW_NORMAL_TEXT)
29787 row->fill_line_p = true; /* Clear to end of line. */
29790 else if (row->reversed_p && row == first)
29791 end_hpos = hlinfo->mouse_face_beg_col;
29792 else
29794 end_hpos = row->used[TEXT_AREA];
29795 if (draw == DRAW_NORMAL_TEXT)
29796 row->fill_line_p = true; /* Clear to end of line. */
29799 if (end_hpos > start_hpos)
29801 draw_row_with_mouse_face (w, start_x, row,
29802 start_hpos, end_hpos, draw);
29804 row->mouse_face_p
29805 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29809 /* When we've written over the cursor, arrange for it to
29810 be displayed again. */
29811 if (FRAME_WINDOW_P (f)
29812 && phys_cursor_on_p && !w->phys_cursor_on_p)
29814 #ifdef HAVE_WINDOW_SYSTEM
29815 int hpos = w->phys_cursor.hpos;
29817 /* When the window is hscrolled, cursor hpos can legitimately be
29818 out of bounds, but we draw the cursor at the corresponding
29819 window margin in that case. */
29820 if (!row->reversed_p && hpos < 0)
29821 hpos = 0;
29822 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29823 hpos = row->used[TEXT_AREA] - 1;
29825 block_input ();
29826 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29827 w->phys_cursor.x, w->phys_cursor.y);
29828 unblock_input ();
29829 #endif /* HAVE_WINDOW_SYSTEM */
29833 #ifdef HAVE_WINDOW_SYSTEM
29834 /* Change the mouse cursor. */
29835 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29837 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29838 if (draw == DRAW_NORMAL_TEXT
29839 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29840 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29841 else
29842 #endif
29843 if (draw == DRAW_MOUSE_FACE)
29844 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29845 else
29846 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29848 #endif /* HAVE_WINDOW_SYSTEM */
29851 /* EXPORT:
29852 Clear out the mouse-highlighted active region.
29853 Redraw it un-highlighted first. Value is true if mouse
29854 face was actually drawn unhighlighted. */
29856 bool
29857 clear_mouse_face (Mouse_HLInfo *hlinfo)
29859 bool cleared
29860 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29861 if (cleared)
29862 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29863 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29864 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29865 hlinfo->mouse_face_window = Qnil;
29866 hlinfo->mouse_face_overlay = Qnil;
29867 return cleared;
29870 /* Return true if the coordinates HPOS and VPOS on windows W are
29871 within the mouse face on that window. */
29872 static bool
29873 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29875 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29877 /* Quickly resolve the easy cases. */
29878 if (!(WINDOWP (hlinfo->mouse_face_window)
29879 && XWINDOW (hlinfo->mouse_face_window) == w))
29880 return false;
29881 if (vpos < hlinfo->mouse_face_beg_row
29882 || vpos > hlinfo->mouse_face_end_row)
29883 return false;
29884 if (vpos > hlinfo->mouse_face_beg_row
29885 && vpos < hlinfo->mouse_face_end_row)
29886 return true;
29888 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29890 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29892 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29893 return true;
29895 else if ((vpos == hlinfo->mouse_face_beg_row
29896 && hpos >= hlinfo->mouse_face_beg_col)
29897 || (vpos == hlinfo->mouse_face_end_row
29898 && hpos < hlinfo->mouse_face_end_col))
29899 return true;
29901 else
29903 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29905 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29906 return true;
29908 else if ((vpos == hlinfo->mouse_face_beg_row
29909 && hpos <= hlinfo->mouse_face_beg_col)
29910 || (vpos == hlinfo->mouse_face_end_row
29911 && hpos > hlinfo->mouse_face_end_col))
29912 return true;
29914 return false;
29918 /* EXPORT:
29919 True if physical cursor of window W is within mouse face. */
29921 bool
29922 cursor_in_mouse_face_p (struct window *w)
29924 int hpos = w->phys_cursor.hpos;
29925 int vpos = w->phys_cursor.vpos;
29926 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29928 /* When the window is hscrolled, cursor hpos can legitimately be out
29929 of bounds, but we draw the cursor at the corresponding window
29930 margin in that case. */
29931 if (!row->reversed_p && hpos < 0)
29932 hpos = 0;
29933 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29934 hpos = row->used[TEXT_AREA] - 1;
29936 return coords_in_mouse_face_p (w, hpos, vpos);
29941 /* Find the glyph rows START_ROW and END_ROW of window W that display
29942 characters between buffer positions START_CHARPOS and END_CHARPOS
29943 (excluding END_CHARPOS). DISP_STRING is a display string that
29944 covers these buffer positions. This is similar to
29945 row_containing_pos, but is more accurate when bidi reordering makes
29946 buffer positions change non-linearly with glyph rows. */
29947 static void
29948 rows_from_pos_range (struct window *w,
29949 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29950 Lisp_Object disp_string,
29951 struct glyph_row **start, struct glyph_row **end)
29953 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29954 int last_y = window_text_bottom_y (w);
29955 struct glyph_row *row;
29957 *start = NULL;
29958 *end = NULL;
29960 while (!first->enabled_p
29961 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29962 first++;
29964 /* Find the START row. */
29965 for (row = first;
29966 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29967 row++)
29969 /* A row can potentially be the START row if the range of the
29970 characters it displays intersects the range
29971 [START_CHARPOS..END_CHARPOS). */
29972 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29973 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29974 /* See the commentary in row_containing_pos, for the
29975 explanation of the complicated way to check whether
29976 some position is beyond the end of the characters
29977 displayed by a row. */
29978 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29979 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29980 && !row->ends_at_zv_p
29981 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29982 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29983 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29984 && !row->ends_at_zv_p
29985 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29987 /* Found a candidate row. Now make sure at least one of the
29988 glyphs it displays has a charpos from the range
29989 [START_CHARPOS..END_CHARPOS).
29991 This is not obvious because bidi reordering could make
29992 buffer positions of a row be 1,2,3,102,101,100, and if we
29993 want to highlight characters in [50..60), we don't want
29994 this row, even though [50..60) does intersect [1..103),
29995 the range of character positions given by the row's start
29996 and end positions. */
29997 struct glyph *g = row->glyphs[TEXT_AREA];
29998 struct glyph *e = g + row->used[TEXT_AREA];
30000 while (g < e)
30002 if (((BUFFERP (g->object) || NILP (g->object))
30003 && start_charpos <= g->charpos && g->charpos < end_charpos)
30004 /* A glyph that comes from DISP_STRING is by
30005 definition to be highlighted. */
30006 || EQ (g->object, disp_string))
30007 *start = row;
30008 g++;
30010 if (*start)
30011 break;
30015 /* Find the END row. */
30016 if (!*start
30017 /* If the last row is partially visible, start looking for END
30018 from that row, instead of starting from FIRST. */
30019 && !(row->enabled_p
30020 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
30021 row = first;
30022 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
30024 struct glyph_row *next = row + 1;
30025 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
30027 if (!next->enabled_p
30028 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
30029 /* The first row >= START whose range of displayed characters
30030 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
30031 is the row END + 1. */
30032 || (start_charpos < next_start
30033 && end_charpos < next_start)
30034 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
30035 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
30036 && !next->ends_at_zv_p
30037 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
30038 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
30039 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
30040 && !next->ends_at_zv_p
30041 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
30043 *end = row;
30044 break;
30046 else
30048 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
30049 but none of the characters it displays are in the range, it is
30050 also END + 1. */
30051 struct glyph *g = next->glyphs[TEXT_AREA];
30052 struct glyph *s = g;
30053 struct glyph *e = g + next->used[TEXT_AREA];
30055 while (g < e)
30057 if (((BUFFERP (g->object) || NILP (g->object))
30058 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
30059 /* If the buffer position of the first glyph in
30060 the row is equal to END_CHARPOS, it means
30061 the last character to be highlighted is the
30062 newline of ROW, and we must consider NEXT as
30063 END, not END+1. */
30064 || (((!next->reversed_p && g == s)
30065 || (next->reversed_p && g == e - 1))
30066 && (g->charpos == end_charpos
30067 /* Special case for when NEXT is an
30068 empty line at ZV. */
30069 || (g->charpos == -1
30070 && !row->ends_at_zv_p
30071 && next_start == end_charpos)))))
30072 /* A glyph that comes from DISP_STRING is by
30073 definition to be highlighted. */
30074 || EQ (g->object, disp_string))
30075 break;
30076 g++;
30078 if (g == e)
30080 *end = row;
30081 break;
30083 /* The first row that ends at ZV must be the last to be
30084 highlighted. */
30085 else if (next->ends_at_zv_p)
30087 *end = next;
30088 break;
30094 /* This function sets the mouse_face_* elements of HLINFO, assuming
30095 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
30096 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
30097 for the overlay or run of text properties specifying the mouse
30098 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
30099 before-string and after-string that must also be highlighted.
30100 DISP_STRING, if non-nil, is a display string that may cover some
30101 or all of the highlighted text. */
30103 static void
30104 mouse_face_from_buffer_pos (Lisp_Object window,
30105 Mouse_HLInfo *hlinfo,
30106 ptrdiff_t mouse_charpos,
30107 ptrdiff_t start_charpos,
30108 ptrdiff_t end_charpos,
30109 Lisp_Object before_string,
30110 Lisp_Object after_string,
30111 Lisp_Object disp_string)
30113 struct window *w = XWINDOW (window);
30114 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30115 struct glyph_row *r1, *r2;
30116 struct glyph *glyph, *end;
30117 ptrdiff_t ignore, pos;
30118 int x;
30120 eassert (NILP (disp_string) || STRINGP (disp_string));
30121 eassert (NILP (before_string) || STRINGP (before_string));
30122 eassert (NILP (after_string) || STRINGP (after_string));
30124 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30125 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30126 if (r1 == NULL)
30127 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30128 /* If the before-string or display-string contains newlines,
30129 rows_from_pos_range skips to its last row. Move back. */
30130 if (!NILP (before_string) || !NILP (disp_string))
30132 struct glyph_row *prev;
30133 while ((prev = r1 - 1, prev >= first)
30134 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30135 && prev->used[TEXT_AREA] > 0)
30137 struct glyph *beg = prev->glyphs[TEXT_AREA];
30138 glyph = beg + prev->used[TEXT_AREA];
30139 while (--glyph >= beg && NILP (glyph->object));
30140 if (glyph < beg
30141 || !(EQ (glyph->object, before_string)
30142 || EQ (glyph->object, disp_string)))
30143 break;
30144 r1 = prev;
30147 if (r2 == NULL)
30149 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30150 hlinfo->mouse_face_past_end = true;
30152 else if (!NILP (after_string))
30154 /* If the after-string has newlines, advance to its last row. */
30155 struct glyph_row *next;
30156 struct glyph_row *last
30157 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30159 for (next = r2 + 1;
30160 next <= last
30161 && next->used[TEXT_AREA] > 0
30162 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30163 ++next)
30164 r2 = next;
30166 /* The rest of the display engine assumes that mouse_face_beg_row is
30167 either above mouse_face_end_row or identical to it. But with
30168 bidi-reordered continued lines, the row for START_CHARPOS could
30169 be below the row for END_CHARPOS. If so, swap the rows and store
30170 them in correct order. */
30171 if (r1->y > r2->y)
30173 struct glyph_row *tem = r2;
30175 r2 = r1;
30176 r1 = tem;
30179 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30180 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30182 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30183 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30184 could be anywhere in the row and in any order. The strategy
30185 below is to find the leftmost and the rightmost glyph that
30186 belongs to either of these 3 strings, or whose position is
30187 between START_CHARPOS and END_CHARPOS, and highlight all the
30188 glyphs between those two. This may cover more than just the text
30189 between START_CHARPOS and END_CHARPOS if the range of characters
30190 strides the bidi level boundary, e.g. if the beginning is in R2L
30191 text while the end is in L2R text or vice versa. */
30192 if (!r1->reversed_p)
30194 /* This row is in a left to right paragraph. Scan it left to
30195 right. */
30196 glyph = r1->glyphs[TEXT_AREA];
30197 end = glyph + r1->used[TEXT_AREA];
30198 x = r1->x;
30200 /* Skip truncation glyphs at the start of the glyph row. */
30201 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30202 for (; glyph < end
30203 && NILP (glyph->object)
30204 && glyph->charpos < 0;
30205 ++glyph)
30206 x += glyph->pixel_width;
30208 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30209 or DISP_STRING, and the first glyph from buffer whose
30210 position is between START_CHARPOS and END_CHARPOS. */
30211 for (; glyph < end
30212 && !NILP (glyph->object)
30213 && !EQ (glyph->object, disp_string)
30214 && !(BUFFERP (glyph->object)
30215 && (glyph->charpos >= start_charpos
30216 && glyph->charpos < end_charpos));
30217 ++glyph)
30219 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30220 are present at buffer positions between START_CHARPOS and
30221 END_CHARPOS, or if they come from an overlay. */
30222 if (EQ (glyph->object, before_string))
30224 pos = string_buffer_position (before_string,
30225 start_charpos);
30226 /* If pos == 0, it means before_string came from an
30227 overlay, not from a buffer position. */
30228 if (!pos || (pos >= start_charpos && pos < end_charpos))
30229 break;
30231 else if (EQ (glyph->object, after_string))
30233 pos = string_buffer_position (after_string, end_charpos);
30234 if (!pos || (pos >= start_charpos && pos < end_charpos))
30235 break;
30237 x += glyph->pixel_width;
30239 hlinfo->mouse_face_beg_x = x;
30240 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30242 else
30244 /* This row is in a right to left paragraph. Scan it right to
30245 left. */
30246 struct glyph *g;
30248 end = r1->glyphs[TEXT_AREA] - 1;
30249 glyph = end + r1->used[TEXT_AREA];
30251 /* Skip truncation glyphs at the start of the glyph row. */
30252 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30253 for (; glyph > end
30254 && NILP (glyph->object)
30255 && glyph->charpos < 0;
30256 --glyph)
30259 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30260 or DISP_STRING, and the first glyph from buffer whose
30261 position is between START_CHARPOS and END_CHARPOS. */
30262 for (; glyph > end
30263 && !NILP (glyph->object)
30264 && !EQ (glyph->object, disp_string)
30265 && !(BUFFERP (glyph->object)
30266 && (glyph->charpos >= start_charpos
30267 && glyph->charpos < end_charpos));
30268 --glyph)
30270 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30271 are present at buffer positions between START_CHARPOS and
30272 END_CHARPOS, or if they come from an overlay. */
30273 if (EQ (glyph->object, before_string))
30275 pos = string_buffer_position (before_string, start_charpos);
30276 /* If pos == 0, it means before_string came from an
30277 overlay, not from a buffer position. */
30278 if (!pos || (pos >= start_charpos && pos < end_charpos))
30279 break;
30281 else if (EQ (glyph->object, after_string))
30283 pos = string_buffer_position (after_string, end_charpos);
30284 if (!pos || (pos >= start_charpos && pos < end_charpos))
30285 break;
30289 glyph++; /* first glyph to the right of the highlighted area */
30290 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30291 x += g->pixel_width;
30292 hlinfo->mouse_face_beg_x = x;
30293 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30296 /* If the highlight ends in a different row, compute GLYPH and END
30297 for the end row. Otherwise, reuse the values computed above for
30298 the row where the highlight begins. */
30299 if (r2 != r1)
30301 if (!r2->reversed_p)
30303 glyph = r2->glyphs[TEXT_AREA];
30304 end = glyph + r2->used[TEXT_AREA];
30305 x = r2->x;
30307 else
30309 end = r2->glyphs[TEXT_AREA] - 1;
30310 glyph = end + r2->used[TEXT_AREA];
30314 if (!r2->reversed_p)
30316 /* Skip truncation and continuation glyphs near the end of the
30317 row, and also blanks and stretch glyphs inserted by
30318 extend_face_to_end_of_line. */
30319 while (end > glyph
30320 && NILP ((end - 1)->object))
30321 --end;
30322 /* Scan the rest of the glyph row from the end, looking for the
30323 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30324 DISP_STRING, or whose position is between START_CHARPOS
30325 and END_CHARPOS */
30326 for (--end;
30327 end > glyph
30328 && !NILP (end->object)
30329 && !EQ (end->object, disp_string)
30330 && !(BUFFERP (end->object)
30331 && (end->charpos >= start_charpos
30332 && end->charpos < end_charpos));
30333 --end)
30335 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30336 are present at buffer positions between START_CHARPOS and
30337 END_CHARPOS, or if they come from an overlay. */
30338 if (EQ (end->object, before_string))
30340 pos = string_buffer_position (before_string, start_charpos);
30341 if (!pos || (pos >= start_charpos && pos < end_charpos))
30342 break;
30344 else if (EQ (end->object, after_string))
30346 pos = string_buffer_position (after_string, end_charpos);
30347 if (!pos || (pos >= start_charpos && pos < end_charpos))
30348 break;
30351 /* Find the X coordinate of the last glyph to be highlighted. */
30352 for (; glyph <= end; ++glyph)
30353 x += glyph->pixel_width;
30355 hlinfo->mouse_face_end_x = x;
30356 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30358 else
30360 /* Skip truncation and continuation glyphs near the end of the
30361 row, and also blanks and stretch glyphs inserted by
30362 extend_face_to_end_of_line. */
30363 x = r2->x;
30364 end++;
30365 while (end < glyph
30366 && NILP (end->object))
30368 x += end->pixel_width;
30369 ++end;
30371 /* Scan the rest of the glyph row from the end, looking for the
30372 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30373 DISP_STRING, or whose position is between START_CHARPOS
30374 and END_CHARPOS */
30375 for ( ;
30376 end < glyph
30377 && !NILP (end->object)
30378 && !EQ (end->object, disp_string)
30379 && !(BUFFERP (end->object)
30380 && (end->charpos >= start_charpos
30381 && end->charpos < end_charpos));
30382 ++end)
30384 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30385 are present at buffer positions between START_CHARPOS and
30386 END_CHARPOS, or if they come from an overlay. */
30387 if (EQ (end->object, before_string))
30389 pos = string_buffer_position (before_string, start_charpos);
30390 if (!pos || (pos >= start_charpos && pos < end_charpos))
30391 break;
30393 else if (EQ (end->object, after_string))
30395 pos = string_buffer_position (after_string, end_charpos);
30396 if (!pos || (pos >= start_charpos && pos < end_charpos))
30397 break;
30399 x += end->pixel_width;
30401 /* If we exited the above loop because we arrived at the last
30402 glyph of the row, and its buffer position is still not in
30403 range, it means the last character in range is the preceding
30404 newline. Bump the end column and x values to get past the
30405 last glyph. */
30406 if (end == glyph
30407 && BUFFERP (end->object)
30408 && (end->charpos < start_charpos
30409 || end->charpos >= end_charpos))
30411 x += end->pixel_width;
30412 ++end;
30414 hlinfo->mouse_face_end_x = x;
30415 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30418 hlinfo->mouse_face_window = window;
30419 hlinfo->mouse_face_face_id
30420 = face_at_buffer_position (w, mouse_charpos, &ignore,
30421 mouse_charpos + 1,
30422 !hlinfo->mouse_face_hidden, -1);
30423 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30426 /* The following function is not used anymore (replaced with
30427 mouse_face_from_string_pos), but I leave it here for the time
30428 being, in case someone would. */
30430 #if false /* not used */
30432 /* Find the position of the glyph for position POS in OBJECT in
30433 window W's current matrix, and return in *X, *Y the pixel
30434 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30436 RIGHT_P means return the position of the right edge of the glyph.
30437 !RIGHT_P means return the left edge position.
30439 If no glyph for POS exists in the matrix, return the position of
30440 the glyph with the next smaller position that is in the matrix, if
30441 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30442 exists in the matrix, return the position of the glyph with the
30443 next larger position in OBJECT.
30445 Value is true if a glyph was found. */
30447 static bool
30448 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30449 int *hpos, int *vpos, int *x, int *y, bool right_p)
30451 int yb = window_text_bottom_y (w);
30452 struct glyph_row *r;
30453 struct glyph *best_glyph = NULL;
30454 struct glyph_row *best_row = NULL;
30455 int best_x = 0;
30457 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30458 r->enabled_p && r->y < yb;
30459 ++r)
30461 struct glyph *g = r->glyphs[TEXT_AREA];
30462 struct glyph *e = g + r->used[TEXT_AREA];
30463 int gx;
30465 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30466 if (EQ (g->object, object))
30468 if (g->charpos == pos)
30470 best_glyph = g;
30471 best_x = gx;
30472 best_row = r;
30473 goto found;
30475 else if (best_glyph == NULL
30476 || ((eabs (g->charpos - pos)
30477 < eabs (best_glyph->charpos - pos))
30478 && (right_p
30479 ? g->charpos < pos
30480 : g->charpos > pos)))
30482 best_glyph = g;
30483 best_x = gx;
30484 best_row = r;
30489 found:
30491 if (best_glyph)
30493 *x = best_x;
30494 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30496 if (right_p)
30498 *x += best_glyph->pixel_width;
30499 ++*hpos;
30502 *y = best_row->y;
30503 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30506 return best_glyph != NULL;
30508 #endif /* not used */
30510 /* Find the positions of the first and the last glyphs in window W's
30511 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30512 (assumed to be a string), and return in HLINFO's mouse_face_*
30513 members the pixel and column/row coordinates of those glyphs. */
30515 static void
30516 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30517 Lisp_Object object,
30518 ptrdiff_t startpos, ptrdiff_t endpos)
30520 int yb = window_text_bottom_y (w);
30521 struct glyph_row *r;
30522 struct glyph *g, *e;
30523 int gx;
30524 bool found = false;
30526 /* Find the glyph row with at least one position in the range
30527 [STARTPOS..ENDPOS), and the first glyph in that row whose
30528 position belongs to that range. */
30529 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30530 r->enabled_p && r->y < yb;
30531 ++r)
30533 if (!r->reversed_p)
30535 g = r->glyphs[TEXT_AREA];
30536 e = g + r->used[TEXT_AREA];
30537 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30538 if (EQ (g->object, object)
30539 && startpos <= g->charpos && g->charpos < endpos)
30541 hlinfo->mouse_face_beg_row
30542 = MATRIX_ROW_VPOS (r, w->current_matrix);
30543 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30544 hlinfo->mouse_face_beg_x = gx;
30545 found = true;
30546 break;
30549 else
30551 struct glyph *g1;
30553 e = r->glyphs[TEXT_AREA];
30554 g = e + r->used[TEXT_AREA];
30555 for ( ; g > e; --g)
30556 if (EQ ((g-1)->object, object)
30557 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30559 hlinfo->mouse_face_beg_row
30560 = MATRIX_ROW_VPOS (r, w->current_matrix);
30561 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30562 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30563 gx += g1->pixel_width;
30564 hlinfo->mouse_face_beg_x = gx;
30565 found = true;
30566 break;
30569 if (found)
30570 break;
30573 if (!found)
30574 return;
30576 /* Starting with the next row, look for the first row which does NOT
30577 include any glyphs whose positions are in the range. */
30578 for (++r; r->enabled_p && r->y < yb; ++r)
30580 g = r->glyphs[TEXT_AREA];
30581 e = g + r->used[TEXT_AREA];
30582 found = false;
30583 for ( ; g < e; ++g)
30584 if (EQ (g->object, object)
30585 && startpos <= g->charpos && g->charpos < endpos)
30587 found = true;
30588 break;
30590 if (!found)
30591 break;
30594 /* The highlighted region ends on the previous row. */
30595 r--;
30597 /* Set the end row. */
30598 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30600 /* Compute and set the end column and the end column's horizontal
30601 pixel coordinate. */
30602 if (!r->reversed_p)
30604 g = r->glyphs[TEXT_AREA];
30605 e = g + r->used[TEXT_AREA];
30606 for ( ; e > g; --e)
30607 if (EQ ((e-1)->object, object)
30608 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30609 break;
30610 hlinfo->mouse_face_end_col = e - g;
30612 for (gx = r->x; g < e; ++g)
30613 gx += g->pixel_width;
30614 hlinfo->mouse_face_end_x = gx;
30616 else
30618 e = r->glyphs[TEXT_AREA];
30619 g = e + r->used[TEXT_AREA];
30620 for (gx = r->x ; e < g; ++e)
30622 if (EQ (e->object, object)
30623 && startpos <= e->charpos && e->charpos < endpos)
30624 break;
30625 gx += e->pixel_width;
30627 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30628 hlinfo->mouse_face_end_x = gx;
30632 #ifdef HAVE_WINDOW_SYSTEM
30634 /* See if position X, Y is within a hot-spot of an image. */
30636 static bool
30637 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30639 if (!CONSP (hot_spot))
30640 return false;
30642 if (EQ (XCAR (hot_spot), Qrect))
30644 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30645 Lisp_Object rect = XCDR (hot_spot);
30646 Lisp_Object tem;
30647 if (!CONSP (rect))
30648 return false;
30649 if (!CONSP (XCAR (rect)))
30650 return false;
30651 if (!CONSP (XCDR (rect)))
30652 return false;
30653 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30654 return false;
30655 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30656 return false;
30657 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30658 return false;
30659 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30660 return false;
30661 return true;
30663 else if (EQ (XCAR (hot_spot), Qcircle))
30665 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30666 Lisp_Object circ = XCDR (hot_spot);
30667 Lisp_Object lr, lx0, ly0;
30668 if (CONSP (circ)
30669 && CONSP (XCAR (circ))
30670 && (lr = XCDR (circ), NUMBERP (lr))
30671 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30672 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30674 double r = XFLOATINT (lr);
30675 double dx = XINT (lx0) - x;
30676 double dy = XINT (ly0) - y;
30677 return (dx * dx + dy * dy <= r * r);
30680 else if (EQ (XCAR (hot_spot), Qpoly))
30682 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30683 if (VECTORP (XCDR (hot_spot)))
30685 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30686 Lisp_Object *poly = v->contents;
30687 ptrdiff_t n = v->header.size;
30688 ptrdiff_t i;
30689 bool inside = false;
30690 Lisp_Object lx, ly;
30691 int x0, y0;
30693 /* Need an even number of coordinates, and at least 3 edges. */
30694 if (n < 6 || n & 1)
30695 return false;
30697 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30698 If count is odd, we are inside polygon. Pixels on edges
30699 may or may not be included depending on actual geometry of the
30700 polygon. */
30701 if ((lx = poly[n-2], !INTEGERP (lx))
30702 || (ly = poly[n-1], !INTEGERP (lx)))
30703 return false;
30704 x0 = XINT (lx), y0 = XINT (ly);
30705 for (i = 0; i < n; i += 2)
30707 int x1 = x0, y1 = y0;
30708 if ((lx = poly[i], !INTEGERP (lx))
30709 || (ly = poly[i+1], !INTEGERP (ly)))
30710 return false;
30711 x0 = XINT (lx), y0 = XINT (ly);
30713 /* Does this segment cross the X line? */
30714 if (x0 >= x)
30716 if (x1 >= x)
30717 continue;
30719 else if (x1 < x)
30720 continue;
30721 if (y > y0 && y > y1)
30722 continue;
30723 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30724 inside = !inside;
30726 return inside;
30729 return false;
30732 Lisp_Object
30733 find_hot_spot (Lisp_Object map, int x, int y)
30735 while (CONSP (map))
30737 if (CONSP (XCAR (map))
30738 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30739 return XCAR (map);
30740 map = XCDR (map);
30743 return Qnil;
30746 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30747 3, 3, 0,
30748 doc: /* Lookup in image map MAP coordinates X and Y.
30749 An image map is an alist where each element has the format (AREA ID PLIST).
30750 An AREA is specified as either a rectangle, a circle, or a polygon:
30751 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30752 pixel coordinates of the upper left and bottom right corners.
30753 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30754 and the radius of the circle; r may be a float or integer.
30755 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30756 vector describes one corner in the polygon.
30757 Returns the alist element for the first matching AREA in MAP. */)
30758 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30760 if (NILP (map))
30761 return Qnil;
30763 CHECK_NUMBER (x);
30764 CHECK_NUMBER (y);
30766 return find_hot_spot (map,
30767 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30768 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30770 #endif /* HAVE_WINDOW_SYSTEM */
30773 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30774 static void
30775 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30777 #ifdef HAVE_WINDOW_SYSTEM
30778 if (!FRAME_WINDOW_P (f))
30779 return;
30781 /* Do not change cursor shape while dragging mouse. */
30782 if (EQ (do_mouse_tracking, Qdragging))
30783 return;
30785 if (!NILP (pointer))
30787 if (EQ (pointer, Qarrow))
30788 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30789 else if (EQ (pointer, Qhand))
30790 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30791 else if (EQ (pointer, Qtext))
30792 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30793 else if (EQ (pointer, intern ("hdrag")))
30794 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30795 else if (EQ (pointer, intern ("nhdrag")))
30796 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30797 # ifdef HAVE_X_WINDOWS
30798 else if (EQ (pointer, intern ("vdrag")))
30799 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30800 # endif
30801 else if (EQ (pointer, intern ("hourglass")))
30802 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30803 else if (EQ (pointer, Qmodeline))
30804 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30805 else
30806 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30809 if (cursor != No_Cursor)
30810 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30811 #endif
30814 /* Take proper action when mouse has moved to the mode or header line
30815 or marginal area AREA of window W, x-position X and y-position Y.
30816 X is relative to the start of the text display area of W, so the
30817 width of bitmap areas and scroll bars must be subtracted to get a
30818 position relative to the start of the mode line. */
30820 static void
30821 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30822 enum window_part area)
30824 struct window *w = XWINDOW (window);
30825 struct frame *f = XFRAME (w->frame);
30826 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30827 Cursor cursor = No_Cursor;
30828 Lisp_Object pointer = Qnil;
30829 int dx, dy, width, height;
30830 ptrdiff_t charpos;
30831 Lisp_Object string, object = Qnil;
30832 Lisp_Object pos UNINIT;
30833 Lisp_Object mouse_face;
30834 int original_x_pixel = x;
30835 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30836 struct glyph_row *row UNINIT;
30838 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30840 int x0;
30841 struct glyph *end;
30843 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30844 returns them in row/column units! */
30845 string = mode_line_string (w, area, &x, &y, &charpos,
30846 &object, &dx, &dy, &width, &height);
30848 row = (area == ON_MODE_LINE
30849 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30850 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30852 /* Find the glyph under the mouse pointer. */
30853 if (row->mode_line_p && row->enabled_p)
30855 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30856 end = glyph + row->used[TEXT_AREA];
30858 for (x0 = original_x_pixel;
30859 glyph < end && x0 >= glyph->pixel_width;
30860 ++glyph)
30861 x0 -= glyph->pixel_width;
30863 if (glyph >= end)
30864 glyph = NULL;
30867 else
30869 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30870 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30871 returns them in row/column units! */
30872 string = marginal_area_string (w, area, &x, &y, &charpos,
30873 &object, &dx, &dy, &width, &height);
30876 Lisp_Object help = Qnil;
30878 #ifdef HAVE_WINDOW_SYSTEM
30879 if (IMAGEP (object))
30881 Lisp_Object image_map, hotspot;
30882 if ((image_map = Fplist_get (XCDR (object), QCmap),
30883 !NILP (image_map))
30884 && (hotspot = find_hot_spot (image_map, dx, dy),
30885 CONSP (hotspot))
30886 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30888 Lisp_Object plist;
30890 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30891 If so, we could look for mouse-enter, mouse-leave
30892 properties in PLIST (and do something...). */
30893 hotspot = XCDR (hotspot);
30894 if (CONSP (hotspot)
30895 && (plist = XCAR (hotspot), CONSP (plist)))
30897 pointer = Fplist_get (plist, Qpointer);
30898 if (NILP (pointer))
30899 pointer = Qhand;
30900 help = Fplist_get (plist, Qhelp_echo);
30901 if (!NILP (help))
30903 help_echo_string = help;
30904 XSETWINDOW (help_echo_window, w);
30905 help_echo_object = w->contents;
30906 help_echo_pos = charpos;
30910 if (NILP (pointer))
30911 pointer = Fplist_get (XCDR (object), QCpointer);
30913 #endif /* HAVE_WINDOW_SYSTEM */
30915 if (STRINGP (string))
30916 pos = make_number (charpos);
30918 /* Set the help text and mouse pointer. If the mouse is on a part
30919 of the mode line without any text (e.g. past the right edge of
30920 the mode line text), use that windows's mode line help echo if it
30921 has been set. */
30922 if (STRINGP (string) || area == ON_MODE_LINE)
30924 /* Arrange to display the help by setting the global variables
30925 help_echo_string, help_echo_object, and help_echo_pos. */
30926 if (NILP (help))
30928 if (STRINGP (string))
30929 help = Fget_text_property (pos, Qhelp_echo, string);
30931 if (!NILP (help))
30933 help_echo_string = help;
30934 XSETWINDOW (help_echo_window, w);
30935 help_echo_object = string;
30936 help_echo_pos = charpos;
30938 else if (area == ON_MODE_LINE
30939 && !NILP (w->mode_line_help_echo))
30941 help_echo_string = w->mode_line_help_echo;
30942 XSETWINDOW (help_echo_window, w);
30943 help_echo_object = Qnil;
30944 help_echo_pos = -1;
30948 #ifdef HAVE_WINDOW_SYSTEM
30949 /* Change the mouse pointer according to what is under it. */
30950 if (FRAME_WINDOW_P (f))
30952 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30953 || minibuf_level
30954 || NILP (Vresize_mini_windows));
30956 if (STRINGP (string))
30958 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30960 if (NILP (pointer))
30961 pointer = Fget_text_property (pos, Qpointer, string);
30963 /* Change the mouse pointer according to what is under X/Y. */
30964 if (NILP (pointer)
30965 && (area == ON_MODE_LINE || area == ON_HEADER_LINE))
30967 Lisp_Object map;
30969 map = Fget_text_property (pos, Qlocal_map, string);
30970 if (!KEYMAPP (map))
30971 map = Fget_text_property (pos, Qkeymap, string);
30972 if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
30973 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30976 else if (draggable && area == ON_MODE_LINE)
30977 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30978 else
30979 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30981 #endif
30984 /* Change the mouse face according to what is under X/Y. */
30985 bool mouse_face_shown = false;
30987 if (STRINGP (string))
30989 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30990 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30991 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30992 && glyph)
30994 Lisp_Object b, e;
30996 struct glyph * tmp_glyph;
30998 int gpos;
30999 int gseq_length;
31000 int total_pixel_width;
31001 ptrdiff_t begpos, endpos, ignore;
31003 int vpos, hpos;
31005 b = Fprevious_single_property_change (make_number (charpos + 1),
31006 Qmouse_face, string, Qnil);
31007 if (NILP (b))
31008 begpos = 0;
31009 else
31010 begpos = XINT (b);
31012 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
31013 if (NILP (e))
31014 endpos = SCHARS (string);
31015 else
31016 endpos = XINT (e);
31018 /* Calculate the glyph position GPOS of GLYPH in the
31019 displayed string, relative to the beginning of the
31020 highlighted part of the string.
31022 Note: GPOS is different from CHARPOS. CHARPOS is the
31023 position of GLYPH in the internal string object. A mode
31024 line string format has structures which are converted to
31025 a flattened string by the Emacs Lisp interpreter. The
31026 internal string is an element of those structures. The
31027 displayed string is the flattened string. */
31028 tmp_glyph = row_start_glyph;
31029 while (tmp_glyph < glyph
31030 && (!(EQ (tmp_glyph->object, glyph->object)
31031 && begpos <= tmp_glyph->charpos
31032 && tmp_glyph->charpos < endpos)))
31033 tmp_glyph++;
31034 gpos = glyph - tmp_glyph;
31036 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
31037 the highlighted part of the displayed string to which
31038 GLYPH belongs. Note: GSEQ_LENGTH is different from
31039 SCHARS (STRING), because the latter returns the length of
31040 the internal string. */
31041 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
31042 tmp_glyph > glyph
31043 && (!(EQ (tmp_glyph->object, glyph->object)
31044 && begpos <= tmp_glyph->charpos
31045 && tmp_glyph->charpos < endpos));
31046 tmp_glyph--)
31048 gseq_length = gpos + (tmp_glyph - glyph) + 1;
31050 /* Calculate the total pixel width of all the glyphs between
31051 the beginning of the highlighted area and GLYPH. */
31052 total_pixel_width = 0;
31053 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
31054 total_pixel_width += tmp_glyph->pixel_width;
31056 /* Pre calculation of re-rendering position. Note: X is in
31057 column units here, after the call to mode_line_string or
31058 marginal_area_string. */
31059 hpos = x - gpos;
31060 vpos = (area == ON_MODE_LINE
31061 ? (w->current_matrix)->nrows - 1
31062 : 0);
31064 /* If GLYPH's position is included in the region that is
31065 already drawn in mouse face, we have nothing to do. */
31066 if ( EQ (window, hlinfo->mouse_face_window)
31067 && (!row->reversed_p
31068 ? (hlinfo->mouse_face_beg_col <= hpos
31069 && hpos < hlinfo->mouse_face_end_col)
31070 /* In R2L rows we swap BEG and END, see below. */
31071 : (hlinfo->mouse_face_end_col <= hpos
31072 && hpos < hlinfo->mouse_face_beg_col))
31073 && hlinfo->mouse_face_beg_row == vpos )
31074 return;
31076 if (clear_mouse_face (hlinfo))
31077 cursor = No_Cursor;
31079 if (!row->reversed_p)
31081 hlinfo->mouse_face_beg_col = hpos;
31082 hlinfo->mouse_face_beg_x = original_x_pixel
31083 - (total_pixel_width + dx);
31084 hlinfo->mouse_face_end_col = hpos + gseq_length;
31085 hlinfo->mouse_face_end_x = 0;
31087 else
31089 /* In R2L rows, show_mouse_face expects BEG and END
31090 coordinates to be swapped. */
31091 hlinfo->mouse_face_end_col = hpos;
31092 hlinfo->mouse_face_end_x = original_x_pixel
31093 - (total_pixel_width + dx);
31094 hlinfo->mouse_face_beg_col = hpos + gseq_length;
31095 hlinfo->mouse_face_beg_x = 0;
31098 hlinfo->mouse_face_beg_row = vpos;
31099 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31100 hlinfo->mouse_face_past_end = false;
31101 hlinfo->mouse_face_window = window;
31103 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31104 charpos,
31105 0, &ignore,
31106 glyph->face_id,
31107 true);
31108 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31109 mouse_face_shown = true;
31111 if (NILP (pointer))
31112 pointer = Qhand;
31116 /* If mouse-face doesn't need to be shown, clear any existing
31117 mouse-face. */
31118 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31119 clear_mouse_face (hlinfo);
31121 define_frame_cursor1 (f, cursor, pointer);
31125 /* EXPORT:
31126 Take proper action when the mouse has moved to position X, Y on
31127 frame F with regards to highlighting portions of display that have
31128 mouse-face properties. Also de-highlight portions of display where
31129 the mouse was before, set the mouse pointer shape as appropriate
31130 for the mouse coordinates, and activate help echo (tooltips).
31131 X and Y can be negative or out of range. */
31133 void
31134 note_mouse_highlight (struct frame *f, int x, int y)
31136 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31137 enum window_part part = ON_NOTHING;
31138 Lisp_Object window;
31139 struct window *w;
31140 Cursor cursor = No_Cursor;
31141 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31142 struct buffer *b;
31144 /* When a menu is active, don't highlight because this looks odd. */
31145 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31146 if (popup_activated ())
31147 return;
31148 #endif
31150 if (!f->glyphs_initialized_p
31151 || f->pointer_invisible)
31152 return;
31154 hlinfo->mouse_face_mouse_x = x;
31155 hlinfo->mouse_face_mouse_y = y;
31156 hlinfo->mouse_face_mouse_frame = f;
31158 if (hlinfo->mouse_face_defer)
31159 return;
31161 /* Which window is that in? */
31162 window = window_from_coordinates (f, x, y, &part, true);
31164 /* If displaying active text in another window, clear that. */
31165 if (! EQ (window, hlinfo->mouse_face_window)
31166 /* Also clear if we move out of text area in same window. */
31167 || (!NILP (hlinfo->mouse_face_window)
31168 && !NILP (window)
31169 && part != ON_TEXT
31170 && part != ON_MODE_LINE
31171 && part != ON_HEADER_LINE))
31172 clear_mouse_face (hlinfo);
31174 /* Reset help_echo_string. It will get recomputed below. */
31175 help_echo_string = Qnil;
31177 #ifdef HAVE_WINDOW_SYSTEM
31178 /* If the cursor is on the internal border of FRAME and FRAME's
31179 internal border is draggable, provide some visual feedback. */
31180 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31181 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31183 enum internal_border_part part = frame_internal_border_part (f, x, y);
31185 switch (part)
31187 case INTERNAL_BORDER_NONE:
31188 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31189 /* Reset cursor. */
31190 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31191 break;
31192 case INTERNAL_BORDER_LEFT_EDGE:
31193 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31194 break;
31195 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31196 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31197 break;
31198 case INTERNAL_BORDER_TOP_EDGE:
31199 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31200 break;
31201 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31202 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31203 break;
31204 case INTERNAL_BORDER_RIGHT_EDGE:
31205 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31206 break;
31207 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31208 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31209 break;
31210 case INTERNAL_BORDER_BOTTOM_EDGE:
31211 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31212 break;
31213 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31214 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31215 break;
31216 default:
31217 /* This should not happen. */
31218 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31219 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31222 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31224 /* Do we really want a help echo here? */
31225 help_echo_string = build_string ("drag-mouse-1: resize frame");
31226 goto set_cursor;
31229 #endif /* HAVE_WINDOW_SYSTEM */
31231 /* Not on a window -> return. */
31232 if (!WINDOWP (window))
31233 return;
31235 /* Convert to window-relative pixel coordinates. */
31236 w = XWINDOW (window);
31237 frame_to_window_pixel_xy (w, &x, &y);
31239 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31240 /* Handle tool-bar window differently since it doesn't display a
31241 buffer. */
31242 if (EQ (window, f->tool_bar_window))
31244 note_tool_bar_highlight (f, x, y);
31245 return;
31247 #endif
31249 /* Mouse is on the mode, header line or margin? */
31250 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31251 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31253 note_mode_line_or_margin_highlight (window, x, y, part);
31255 #ifdef HAVE_WINDOW_SYSTEM
31256 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31258 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31259 /* Show non-text cursor (Bug#16647). */
31260 goto set_cursor;
31262 else
31263 #endif
31264 return;
31267 #ifdef HAVE_WINDOW_SYSTEM
31268 if (part == ON_VERTICAL_BORDER)
31270 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31271 help_echo_string = build_string ("drag-mouse-1: resize");
31272 goto set_cursor;
31274 else if (part == ON_RIGHT_DIVIDER)
31276 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31277 help_echo_string = build_string ("drag-mouse-1: resize");
31278 goto set_cursor;
31280 else if (part == ON_BOTTOM_DIVIDER)
31281 if (! WINDOW_BOTTOMMOST_P (w)
31282 || minibuf_level
31283 || NILP (Vresize_mini_windows))
31285 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31286 help_echo_string = build_string ("drag-mouse-1: resize");
31287 goto set_cursor;
31289 else
31290 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31291 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31292 || part == ON_VERTICAL_SCROLL_BAR
31293 || part == ON_HORIZONTAL_SCROLL_BAR)
31294 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31295 else
31296 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31297 #endif
31299 /* Are we in a window whose display is up to date?
31300 And verify the buffer's text has not changed. */
31301 b = XBUFFER (w->contents);
31302 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31304 int hpos, vpos, dx, dy, area = LAST_AREA;
31305 ptrdiff_t pos;
31306 struct glyph *glyph;
31307 Lisp_Object object;
31308 Lisp_Object mouse_face = Qnil, position;
31309 Lisp_Object *overlay_vec = NULL;
31310 ptrdiff_t i, noverlays;
31311 struct buffer *obuf;
31312 ptrdiff_t obegv, ozv;
31313 bool same_region;
31315 /* Find the glyph under X/Y. */
31316 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31318 #ifdef HAVE_WINDOW_SYSTEM
31319 /* Look for :pointer property on image. */
31320 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31322 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31323 if (img != NULL && IMAGEP (img->spec))
31325 Lisp_Object image_map, hotspot;
31326 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31327 !NILP (image_map))
31328 && (hotspot = find_hot_spot (image_map,
31329 glyph->slice.img.x + dx,
31330 glyph->slice.img.y + dy),
31331 CONSP (hotspot))
31332 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31334 Lisp_Object plist;
31336 /* Could check XCAR (hotspot) to see if we enter/leave
31337 this hot-spot.
31338 If so, we could look for mouse-enter, mouse-leave
31339 properties in PLIST (and do something...). */
31340 hotspot = XCDR (hotspot);
31341 if (CONSP (hotspot)
31342 && (plist = XCAR (hotspot), CONSP (plist)))
31344 pointer = Fplist_get (plist, Qpointer);
31345 if (NILP (pointer))
31346 pointer = Qhand;
31347 help_echo_string = Fplist_get (plist, Qhelp_echo);
31348 if (!NILP (help_echo_string))
31350 help_echo_window = window;
31351 help_echo_object = glyph->object;
31352 help_echo_pos = glyph->charpos;
31356 if (NILP (pointer))
31357 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31360 #endif /* HAVE_WINDOW_SYSTEM */
31362 /* Clear mouse face if X/Y not over text. */
31363 if (glyph == NULL
31364 || area != TEXT_AREA
31365 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31366 /* Glyph's OBJECT is nil for glyphs inserted by the
31367 display engine for its internal purposes, like truncation
31368 and continuation glyphs and blanks beyond the end of
31369 line's text on text terminals. If we are over such a
31370 glyph, we are not over any text. */
31371 || NILP (glyph->object)
31372 /* R2L rows have a stretch glyph at their front, which
31373 stands for no text, whereas L2R rows have no glyphs at
31374 all beyond the end of text. Treat such stretch glyphs
31375 like we do with NULL glyphs in L2R rows. */
31376 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31377 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31378 && glyph->type == STRETCH_GLYPH
31379 && glyph->avoid_cursor_p))
31381 if (clear_mouse_face (hlinfo))
31382 cursor = No_Cursor;
31383 if (FRAME_WINDOW_P (f) && NILP (pointer))
31385 #ifdef HAVE_WINDOW_SYSTEM
31386 if (area != TEXT_AREA)
31387 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31388 else
31389 pointer = Vvoid_text_area_pointer;
31390 #endif
31392 goto set_cursor;
31395 pos = glyph->charpos;
31396 object = glyph->object;
31397 if (!STRINGP (object) && !BUFFERP (object))
31398 goto set_cursor;
31400 /* If we get an out-of-range value, return now; avoid an error. */
31401 if (BUFFERP (object) && pos > BUF_Z (b))
31402 goto set_cursor;
31404 /* Make the window's buffer temporarily current for
31405 overlays_at and compute_char_face. */
31406 obuf = current_buffer;
31407 current_buffer = b;
31408 obegv = BEGV;
31409 ozv = ZV;
31410 BEGV = BEG;
31411 ZV = Z;
31413 /* Is this char mouse-active or does it have help-echo? */
31414 position = make_number (pos);
31416 USE_SAFE_ALLOCA;
31418 if (BUFFERP (object))
31420 /* Put all the overlays we want in a vector in overlay_vec. */
31421 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31422 /* Sort overlays into increasing priority order. */
31423 noverlays = sort_overlays (overlay_vec, noverlays, w);
31425 else
31426 noverlays = 0;
31428 if (NILP (Vmouse_highlight))
31430 clear_mouse_face (hlinfo);
31431 goto check_help_echo;
31434 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31436 if (same_region)
31437 cursor = No_Cursor;
31439 /* Check mouse-face highlighting. */
31440 if (! same_region
31441 /* If there exists an overlay with mouse-face overlapping
31442 the one we are currently highlighting, we have to check
31443 if we enter the overlapping overlay, and then highlight
31444 only that. Skip the check when mouse-face highlighting
31445 is currently hidden to avoid Bug#30519. */
31446 || (!hlinfo->mouse_face_hidden
31447 && OVERLAYP (hlinfo->mouse_face_overlay)
31448 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31450 /* Find the highest priority overlay with a mouse-face. */
31451 Lisp_Object overlay = Qnil;
31452 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31454 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31455 if (!NILP (mouse_face))
31456 overlay = overlay_vec[i];
31459 /* If we're highlighting the same overlay as before, there's
31460 no need to do that again. */
31461 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31462 goto check_help_echo;
31464 /* Clear the display of the old active region, if any. */
31465 if (clear_mouse_face (hlinfo))
31466 cursor = No_Cursor;
31468 /* Record the overlay, if any, to be highlighted. */
31469 hlinfo->mouse_face_overlay = overlay;
31471 /* If no overlay applies, get a text property. */
31472 if (NILP (overlay))
31473 mouse_face = Fget_text_property (position, Qmouse_face, object);
31475 /* Next, compute the bounds of the mouse highlighting and
31476 display it. */
31477 if (!NILP (mouse_face) && STRINGP (object))
31479 /* The mouse-highlighting comes from a display string
31480 with a mouse-face. */
31481 Lisp_Object s, e;
31482 ptrdiff_t ignore;
31484 s = Fprevious_single_property_change
31485 (make_number (pos + 1), Qmouse_face, object, Qnil);
31486 e = Fnext_single_property_change
31487 (position, Qmouse_face, object, Qnil);
31488 if (NILP (s))
31489 s = make_number (0);
31490 if (NILP (e))
31491 e = make_number (SCHARS (object));
31492 mouse_face_from_string_pos (w, hlinfo, object,
31493 XINT (s), XINT (e));
31494 hlinfo->mouse_face_past_end = false;
31495 hlinfo->mouse_face_window = window;
31496 hlinfo->mouse_face_face_id
31497 = face_at_string_position (w, object, pos, 0, &ignore,
31498 glyph->face_id, true);
31499 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31500 cursor = No_Cursor;
31502 else
31504 /* The mouse-highlighting, if any, comes from an overlay
31505 or text property in the buffer. */
31506 Lisp_Object buffer UNINIT;
31507 Lisp_Object disp_string UNINIT;
31509 if (STRINGP (object))
31511 /* If we are on a display string with no mouse-face,
31512 check if the text under it has one. */
31513 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31514 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31515 pos = string_buffer_position (object, start);
31516 if (pos > 0)
31518 mouse_face = get_char_property_and_overlay
31519 (make_number (pos), Qmouse_face, w->contents, &overlay);
31520 buffer = w->contents;
31521 disp_string = object;
31524 else
31526 buffer = object;
31527 disp_string = Qnil;
31530 if (!NILP (mouse_face))
31532 Lisp_Object before, after;
31533 Lisp_Object before_string, after_string;
31534 /* To correctly find the limits of mouse highlight
31535 in a bidi-reordered buffer, we must not use the
31536 optimization of limiting the search in
31537 previous-single-property-change and
31538 next-single-property-change, because
31539 rows_from_pos_range needs the real start and end
31540 positions to DTRT in this case. That's because
31541 the first row visible in a window does not
31542 necessarily display the character whose position
31543 is the smallest. */
31544 Lisp_Object lim1
31545 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31546 ? Fmarker_position (w->start)
31547 : Qnil;
31548 Lisp_Object lim2
31549 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31550 ? make_number (BUF_Z (XBUFFER (buffer))
31551 - w->window_end_pos)
31552 : Qnil;
31554 if (NILP (overlay))
31556 /* Handle the text property case. */
31557 before = Fprevious_single_property_change
31558 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31559 after = Fnext_single_property_change
31560 (make_number (pos), Qmouse_face, buffer, lim2);
31561 before_string = after_string = Qnil;
31563 else
31565 /* Handle the overlay case. */
31566 before = Foverlay_start (overlay);
31567 after = Foverlay_end (overlay);
31568 before_string = Foverlay_get (overlay, Qbefore_string);
31569 after_string = Foverlay_get (overlay, Qafter_string);
31571 if (!STRINGP (before_string)) before_string = Qnil;
31572 if (!STRINGP (after_string)) after_string = Qnil;
31575 mouse_face_from_buffer_pos (window, hlinfo, pos,
31576 NILP (before)
31578 : XFASTINT (before),
31579 NILP (after)
31580 ? BUF_Z (XBUFFER (buffer))
31581 : XFASTINT (after),
31582 before_string, after_string,
31583 disp_string);
31584 cursor = No_Cursor;
31589 check_help_echo:
31591 /* Look for a `help-echo' property. */
31592 if (NILP (help_echo_string)) {
31593 Lisp_Object help, overlay;
31595 /* Check overlays first. */
31596 help = overlay = Qnil;
31597 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31599 overlay = overlay_vec[i];
31600 help = Foverlay_get (overlay, Qhelp_echo);
31603 if (!NILP (help))
31605 help_echo_string = help;
31606 help_echo_window = window;
31607 help_echo_object = overlay;
31608 help_echo_pos = pos;
31610 else
31612 Lisp_Object obj = glyph->object;
31613 ptrdiff_t charpos = glyph->charpos;
31615 /* Try text properties. */
31616 if (STRINGP (obj)
31617 && charpos >= 0
31618 && charpos < SCHARS (obj))
31620 help = Fget_text_property (make_number (charpos),
31621 Qhelp_echo, obj);
31622 if (NILP (help))
31624 /* If the string itself doesn't specify a help-echo,
31625 see if the buffer text ``under'' it does. */
31626 struct glyph_row *r
31627 = MATRIX_ROW (w->current_matrix, vpos);
31628 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31629 ptrdiff_t p = string_buffer_position (obj, start);
31630 if (p > 0)
31632 help = Fget_char_property (make_number (p),
31633 Qhelp_echo, w->contents);
31634 if (!NILP (help))
31636 charpos = p;
31637 obj = w->contents;
31642 else if (BUFFERP (obj)
31643 && charpos >= BEGV
31644 && charpos < ZV)
31645 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31646 obj);
31648 if (!NILP (help))
31650 help_echo_string = help;
31651 help_echo_window = window;
31652 help_echo_object = obj;
31653 help_echo_pos = charpos;
31658 #ifdef HAVE_WINDOW_SYSTEM
31659 /* Look for a `pointer' property. */
31660 if (FRAME_WINDOW_P (f) && NILP (pointer))
31662 /* Check overlays first. */
31663 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31664 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31666 if (NILP (pointer))
31668 Lisp_Object obj = glyph->object;
31669 ptrdiff_t charpos = glyph->charpos;
31671 /* Try text properties. */
31672 if (STRINGP (obj)
31673 && charpos >= 0
31674 && charpos < SCHARS (obj))
31676 pointer = Fget_text_property (make_number (charpos),
31677 Qpointer, obj);
31678 if (NILP (pointer))
31680 /* If the string itself doesn't specify a pointer,
31681 see if the buffer text ``under'' it does. */
31682 struct glyph_row *r
31683 = MATRIX_ROW (w->current_matrix, vpos);
31684 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31685 ptrdiff_t p = string_buffer_position (obj, start);
31686 if (p > 0)
31687 pointer = Fget_char_property (make_number (p),
31688 Qpointer, w->contents);
31691 else if (BUFFERP (obj)
31692 && charpos >= BEGV
31693 && charpos < ZV)
31694 pointer = Fget_text_property (make_number (charpos),
31695 Qpointer, obj);
31698 #endif /* HAVE_WINDOW_SYSTEM */
31700 BEGV = obegv;
31701 ZV = ozv;
31702 current_buffer = obuf;
31703 SAFE_FREE ();
31706 set_cursor:
31707 define_frame_cursor1 (f, cursor, pointer);
31711 /* EXPORT for RIF:
31712 Clear any mouse-face on window W. This function is part of the
31713 redisplay interface, and is called from try_window_id and similar
31714 functions to ensure the mouse-highlight is off. */
31716 void
31717 x_clear_window_mouse_face (struct window *w)
31719 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31720 Lisp_Object window;
31722 block_input ();
31723 XSETWINDOW (window, w);
31724 if (EQ (window, hlinfo->mouse_face_window))
31725 clear_mouse_face (hlinfo);
31726 unblock_input ();
31730 /* EXPORT:
31731 Just discard the mouse face information for frame F, if any.
31732 This is used when the size of F is changed. */
31734 void
31735 cancel_mouse_face (struct frame *f)
31737 Lisp_Object window;
31738 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31740 window = hlinfo->mouse_face_window;
31741 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31742 reset_mouse_highlight (hlinfo);
31747 /***********************************************************************
31748 Exposure Events
31749 ***********************************************************************/
31751 #ifdef HAVE_WINDOW_SYSTEM
31753 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31754 which intersects rectangle R. R is in window-relative coordinates. */
31756 static void
31757 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31758 enum glyph_row_area area)
31760 struct glyph *first = row->glyphs[area];
31761 struct glyph *end = row->glyphs[area] + row->used[area];
31762 struct glyph *last;
31763 int first_x, start_x, x;
31765 if (area == TEXT_AREA && row->fill_line_p)
31766 /* If row extends face to end of line write the whole line. */
31767 draw_glyphs (w, 0, row, area,
31768 0, row->used[area],
31769 DRAW_NORMAL_TEXT, 0);
31770 else
31772 /* Set START_X to the window-relative start position for drawing glyphs of
31773 AREA. The first glyph of the text area can be partially visible.
31774 The first glyphs of other areas cannot. */
31775 start_x = window_box_left_offset (w, area);
31776 x = start_x;
31777 if (area == TEXT_AREA)
31778 x += row->x;
31780 /* Find the first glyph that must be redrawn. */
31781 while (first < end
31782 && x + first->pixel_width < r->x)
31784 x += first->pixel_width;
31785 ++first;
31788 /* Find the last one. */
31789 last = first;
31790 first_x = x;
31791 /* Use a signed int intermediate value to avoid catastrophic
31792 failures due to comparison between signed and unsigned, when
31793 x is negative (can happen for wide images that are hscrolled). */
31794 int r_end = r->x + r->width;
31795 while (last < end && x < r_end)
31797 x += last->pixel_width;
31798 ++last;
31801 /* Repaint. */
31802 if (last > first)
31803 draw_glyphs (w, first_x - start_x, row, area,
31804 first - row->glyphs[area], last - row->glyphs[area],
31805 DRAW_NORMAL_TEXT, 0);
31810 /* Redraw the parts of the glyph row ROW on window W intersecting
31811 rectangle R. R is in window-relative coordinates. Value is
31812 true if mouse-face was overwritten. */
31814 static bool
31815 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31817 eassert (row->enabled_p);
31819 if (row->mode_line_p || w->pseudo_window_p)
31820 draw_glyphs (w, 0, row, TEXT_AREA,
31821 0, row->used[TEXT_AREA],
31822 DRAW_NORMAL_TEXT, 0);
31823 else
31825 if (row->used[LEFT_MARGIN_AREA])
31826 expose_area (w, row, r, LEFT_MARGIN_AREA);
31827 if (row->used[TEXT_AREA])
31828 expose_area (w, row, r, TEXT_AREA);
31829 if (row->used[RIGHT_MARGIN_AREA])
31830 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31831 draw_row_fringe_bitmaps (w, row);
31834 return row->mouse_face_p;
31838 /* Redraw those parts of glyphs rows during expose event handling that
31839 overlap other rows. Redrawing of an exposed line writes over parts
31840 of lines overlapping that exposed line; this function fixes that.
31842 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31843 row in W's current matrix that is exposed and overlaps other rows.
31844 LAST_OVERLAPPING_ROW is the last such row. */
31846 static void
31847 expose_overlaps (struct window *w,
31848 struct glyph_row *first_overlapping_row,
31849 struct glyph_row *last_overlapping_row,
31850 XRectangle *r)
31852 struct glyph_row *row;
31854 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31855 if (row->overlapping_p)
31857 eassert (row->enabled_p && !row->mode_line_p);
31859 row->clip = r;
31860 if (row->used[LEFT_MARGIN_AREA])
31861 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31863 if (row->used[TEXT_AREA])
31864 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31866 if (row->used[RIGHT_MARGIN_AREA])
31867 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31868 row->clip = NULL;
31873 /* Return true if W's cursor intersects rectangle R. */
31875 static bool
31876 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31878 XRectangle cr, result;
31879 struct glyph *cursor_glyph;
31880 struct glyph_row *row;
31882 if (w->phys_cursor.vpos >= 0
31883 && w->phys_cursor.vpos < w->current_matrix->nrows
31884 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31885 row->enabled_p)
31886 && row->cursor_in_fringe_p)
31888 /* Cursor is in the fringe. */
31889 cr.x = window_box_right_offset (w,
31890 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31891 ? RIGHT_MARGIN_AREA
31892 : TEXT_AREA));
31893 cr.y = row->y;
31894 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31895 cr.height = row->height;
31896 return x_intersect_rectangles (&cr, r, &result);
31899 cursor_glyph = get_phys_cursor_glyph (w);
31900 if (cursor_glyph)
31902 /* r is relative to W's box, but w->phys_cursor.x is relative
31903 to left edge of W's TEXT area. Adjust it. */
31904 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31905 cr.y = w->phys_cursor.y;
31906 cr.width = cursor_glyph->pixel_width;
31907 cr.height = w->phys_cursor_height;
31908 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31909 I assume the effect is the same -- and this is portable. */
31910 return x_intersect_rectangles (&cr, r, &result);
31912 /* If we don't understand the format, pretend we're not in the hot-spot. */
31913 return false;
31917 /* EXPORT:
31918 Draw a vertical window border to the right of window W if W doesn't
31919 have vertical scroll bars. */
31921 void
31922 x_draw_vertical_border (struct window *w)
31924 struct frame *f = XFRAME (WINDOW_FRAME (w));
31926 /* We could do better, if we knew what type of scroll-bar the adjacent
31927 windows (on either side) have... But we don't :-(
31928 However, I think this works ok. ++KFS 2003-04-25 */
31930 /* Redraw borders between horizontally adjacent windows. Don't
31931 do it for frames with vertical scroll bars because either the
31932 right scroll bar of a window, or the left scroll bar of its
31933 neighbor will suffice as a border. */
31934 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31935 return;
31937 /* Note: It is necessary to redraw both the left and the right
31938 borders, for when only this single window W is being
31939 redisplayed. */
31940 if (!WINDOW_RIGHTMOST_P (w)
31941 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31943 int x0, x1, y0, y1;
31945 window_box_edges (w, &x0, &y0, &x1, &y1);
31946 y1 -= 1;
31948 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31949 x1 -= 1;
31951 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31954 if (!WINDOW_LEFTMOST_P (w)
31955 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31957 int x0, x1, y0, y1;
31959 window_box_edges (w, &x0, &y0, &x1, &y1);
31960 y1 -= 1;
31962 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31963 x0 -= 1;
31965 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31970 /* Draw window dividers for window W. */
31972 void
31973 x_draw_right_divider (struct window *w)
31975 struct frame *f = WINDOW_XFRAME (w);
31977 if (w->mini || w->pseudo_window_p)
31978 return;
31979 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31981 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31982 int x1 = WINDOW_RIGHT_EDGE_X (w);
31983 int y0 = WINDOW_TOP_EDGE_Y (w);
31984 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31986 /* If W is horizontally combined and has a right sibling, don't
31987 draw over any bottom divider. */
31988 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31989 && !NILP (w->parent)
31990 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31991 && !NILP (w->next))
31992 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31994 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31998 static void
31999 x_draw_bottom_divider (struct window *w)
32001 struct frame *f = XFRAME (WINDOW_FRAME (w));
32003 if (w->mini || w->pseudo_window_p)
32004 return;
32005 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32007 int x0 = WINDOW_LEFT_EDGE_X (w);
32008 int x1 = WINDOW_RIGHT_EDGE_X (w);
32009 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
32010 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
32011 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
32013 /* If W is vertically combined and has a sibling below, don't draw
32014 over any right divider. */
32015 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
32016 && p
32017 && ((WINDOW_VERTICAL_COMBINATION_P (p)
32018 && !NILP (w->next))
32019 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
32020 && NILP (w->next)
32021 && !NILP (p->parent)
32022 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
32023 && !NILP (XWINDOW (p->parent)->next))))
32024 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
32026 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
32030 /* Redraw the part of window W intersection rectangle FR. Pixel
32031 coordinates in FR are frame-relative. Call this function with
32032 input blocked. Value is true if the exposure overwrites
32033 mouse-face. */
32035 static bool
32036 expose_window (struct window *w, XRectangle *fr)
32038 struct frame *f = XFRAME (w->frame);
32039 XRectangle wr, r;
32040 bool mouse_face_overwritten_p = false;
32042 /* If window is not yet fully initialized, do nothing. This can
32043 happen when toolkit scroll bars are used and a window is split.
32044 Reconfiguring the scroll bar will generate an expose for a newly
32045 created window. */
32046 if (w->current_matrix == NULL)
32047 return false;
32049 /* When we're currently updating the window, display and current
32050 matrix usually don't agree. Arrange for a thorough display
32051 later. */
32052 if (w->must_be_updated_p)
32054 SET_FRAME_GARBAGED (f);
32055 return false;
32058 /* Frame-relative pixel rectangle of W. */
32059 wr.x = WINDOW_LEFT_EDGE_X (w);
32060 wr.y = WINDOW_TOP_EDGE_Y (w);
32061 wr.width = WINDOW_PIXEL_WIDTH (w);
32062 wr.height = WINDOW_PIXEL_HEIGHT (w);
32064 if (x_intersect_rectangles (fr, &wr, &r))
32066 int yb = window_text_bottom_y (w);
32067 struct glyph_row *row;
32068 struct glyph_row *first_overlapping_row, *last_overlapping_row;
32070 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
32071 r.x, r.y, r.width, r.height));
32073 /* Convert to window coordinates. */
32074 r.x -= WINDOW_LEFT_EDGE_X (w);
32075 r.y -= WINDOW_TOP_EDGE_Y (w);
32077 /* Turn off the cursor. */
32078 bool cursor_cleared_p = (!w->pseudo_window_p
32079 && phys_cursor_in_rect_p (w, &r));
32080 if (cursor_cleared_p)
32081 x_clear_cursor (w);
32083 /* If the row containing the cursor extends face to end of line,
32084 then expose_area might overwrite the cursor outside the
32085 rectangle and thus notice_overwritten_cursor might clear
32086 w->phys_cursor_on_p. We remember the original value and
32087 check later if it is changed. */
32088 bool phys_cursor_on_p = w->phys_cursor_on_p;
32090 /* Use a signed int intermediate value to avoid catastrophic
32091 failures due to comparison between signed and unsigned, when
32092 y0 or y1 is negative (can happen for tall images). */
32093 int r_bottom = r.y + r.height;
32095 /* Update lines intersecting rectangle R. */
32096 first_overlapping_row = last_overlapping_row = NULL;
32097 for (row = w->current_matrix->rows;
32098 row->enabled_p;
32099 ++row)
32101 int y0 = row->y;
32102 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32104 if ((y0 >= r.y && y0 < r_bottom)
32105 || (y1 > r.y && y1 < r_bottom)
32106 || (r.y >= y0 && r.y < y1)
32107 || (r_bottom > y0 && r_bottom < y1))
32109 /* A header line may be overlapping, but there is no need
32110 to fix overlapping areas for them. KFS 2005-02-12 */
32111 if (row->overlapping_p && !row->mode_line_p)
32113 if (first_overlapping_row == NULL)
32114 first_overlapping_row = row;
32115 last_overlapping_row = row;
32118 row->clip = fr;
32119 if (expose_line (w, row, &r))
32120 mouse_face_overwritten_p = true;
32121 row->clip = NULL;
32123 else if (row->overlapping_p)
32125 /* We must redraw a row overlapping the exposed area. */
32126 if (y0 < r.y
32127 ? y0 + row->phys_height > r.y
32128 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32130 if (first_overlapping_row == NULL)
32131 first_overlapping_row = row;
32132 last_overlapping_row = row;
32136 if (y1 >= yb)
32137 break;
32140 /* Display the mode line if there is one. */
32141 if (window_wants_mode_line (w)
32142 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32143 row->enabled_p)
32144 && row->y < r_bottom)
32146 if (expose_line (w, row, &r))
32147 mouse_face_overwritten_p = true;
32150 if (!w->pseudo_window_p)
32152 /* Fix the display of overlapping rows. */
32153 if (first_overlapping_row)
32154 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32155 fr);
32157 /* Draw border between windows. */
32158 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32159 x_draw_right_divider (w);
32160 else
32161 x_draw_vertical_border (w);
32163 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32164 x_draw_bottom_divider (w);
32166 /* Turn the cursor on again. */
32167 if (cursor_cleared_p
32168 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32169 update_window_cursor (w, true);
32173 return mouse_face_overwritten_p;
32178 /* Redraw (parts) of all windows in the window tree rooted at W that
32179 intersect R. R contains frame pixel coordinates. Value is
32180 true if the exposure overwrites mouse-face. */
32182 static bool
32183 expose_window_tree (struct window *w, XRectangle *r)
32185 struct frame *f = XFRAME (w->frame);
32186 bool mouse_face_overwritten_p = false;
32188 while (w && !FRAME_GARBAGED_P (f))
32190 mouse_face_overwritten_p
32191 |= (WINDOWP (w->contents)
32192 ? expose_window_tree (XWINDOW (w->contents), r)
32193 : expose_window (w, r));
32195 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32198 return mouse_face_overwritten_p;
32202 /* EXPORT:
32203 Redisplay an exposed area of frame F. X and Y are the upper-left
32204 corner of the exposed rectangle. W and H are width and height of
32205 the exposed area. All are pixel values. W or H zero means redraw
32206 the entire frame. */
32208 void
32209 expose_frame (struct frame *f, int x, int y, int w, int h)
32211 XRectangle r;
32212 bool mouse_face_overwritten_p = false;
32214 TRACE ((stderr, "expose_frame "));
32216 /* No need to redraw if frame will be redrawn soon. */
32217 if (FRAME_GARBAGED_P (f))
32219 TRACE ((stderr, " garbaged\n"));
32220 return;
32223 /* If basic faces haven't been realized yet, there is no point in
32224 trying to redraw anything. This can happen when we get an expose
32225 event while Emacs is starting, e.g. by moving another window. */
32226 if (FRAME_FACE_CACHE (f) == NULL
32227 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32229 TRACE ((stderr, " no faces\n"));
32230 return;
32233 if (w == 0 || h == 0)
32235 r.x = r.y = 0;
32236 r.width = FRAME_TEXT_WIDTH (f);
32237 r.height = FRAME_TEXT_HEIGHT (f);
32239 else
32241 r.x = x;
32242 r.y = y;
32243 r.width = w;
32244 r.height = h;
32247 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32248 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32250 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32251 if (WINDOWP (f->tool_bar_window))
32252 mouse_face_overwritten_p
32253 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32254 #endif
32256 #ifdef HAVE_X_WINDOWS
32257 #ifndef MSDOS
32258 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32259 if (WINDOWP (f->menu_bar_window))
32260 mouse_face_overwritten_p
32261 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32262 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32263 #endif
32264 #endif
32266 /* Some window managers support a focus-follows-mouse style with
32267 delayed raising of frames. Imagine a partially obscured frame,
32268 and moving the mouse into partially obscured mouse-face on that
32269 frame. The visible part of the mouse-face will be highlighted,
32270 then the WM raises the obscured frame. With at least one WM, KDE
32271 2.1, Emacs is not getting any event for the raising of the frame
32272 (even tried with SubstructureRedirectMask), only Expose events.
32273 These expose events will draw text normally, i.e. not
32274 highlighted. Which means we must redo the highlight here.
32275 Subsume it under ``we love X''. --gerd 2001-08-15 */
32276 /* Included in Windows version because Windows most likely does not
32277 do the right thing if any third party tool offers
32278 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32279 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32281 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32282 if (f == hlinfo->mouse_face_mouse_frame)
32284 int mouse_x = hlinfo->mouse_face_mouse_x;
32285 int mouse_y = hlinfo->mouse_face_mouse_y;
32286 clear_mouse_face (hlinfo);
32287 note_mouse_highlight (f, mouse_x, mouse_y);
32293 /* EXPORT:
32294 Determine the intersection of two rectangles R1 and R2. Return
32295 the intersection in *RESULT. Value is true if RESULT is not
32296 empty. */
32298 bool
32299 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32301 XRectangle *left, *right;
32302 XRectangle *upper, *lower;
32303 bool intersection_p = false;
32305 /* Rearrange so that R1 is the left-most rectangle. */
32306 if (r1->x < r2->x)
32307 left = r1, right = r2;
32308 else
32309 left = r2, right = r1;
32311 /* X0 of the intersection is right.x0, if this is inside R1,
32312 otherwise there is no intersection. */
32313 if (right->x <= left->x + left->width)
32315 result->x = right->x;
32317 /* The right end of the intersection is the minimum of
32318 the right ends of left and right. */
32319 result->width = (min (left->x + left->width, right->x + right->width)
32320 - result->x);
32322 /* Same game for Y. */
32323 if (r1->y < r2->y)
32324 upper = r1, lower = r2;
32325 else
32326 upper = r2, lower = r1;
32328 /* The upper end of the intersection is lower.y0, if this is inside
32329 of upper. Otherwise, there is no intersection. */
32330 if (lower->y <= upper->y + upper->height)
32332 result->y = lower->y;
32334 /* The lower end of the intersection is the minimum of the lower
32335 ends of upper and lower. */
32336 result->height = (min (lower->y + lower->height,
32337 upper->y + upper->height)
32338 - result->y);
32339 intersection_p = true;
32343 return intersection_p;
32346 #endif /* HAVE_WINDOW_SYSTEM */
32349 /***********************************************************************
32350 Initialization
32351 ***********************************************************************/
32353 void
32354 syms_of_xdisp (void)
32356 Vwith_echo_area_save_vector = Qnil;
32357 staticpro (&Vwith_echo_area_save_vector);
32359 Vmessage_stack = Qnil;
32360 staticpro (&Vmessage_stack);
32362 /* Non-nil means don't actually do any redisplay. */
32363 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32365 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32367 DEFVAR_BOOL("inhibit-message", inhibit_message,
32368 doc: /* Non-nil means calls to `message' are not displayed.
32369 They are still logged to the *Messages* buffer. */);
32370 inhibit_message = 0;
32372 message_dolog_marker1 = Fmake_marker ();
32373 staticpro (&message_dolog_marker1);
32374 message_dolog_marker2 = Fmake_marker ();
32375 staticpro (&message_dolog_marker2);
32376 message_dolog_marker3 = Fmake_marker ();
32377 staticpro (&message_dolog_marker3);
32379 defsubr (&Sset_buffer_redisplay);
32380 #ifdef GLYPH_DEBUG
32381 defsubr (&Sdump_frame_glyph_matrix);
32382 defsubr (&Sdump_glyph_matrix);
32383 defsubr (&Sdump_glyph_row);
32384 defsubr (&Sdump_tool_bar_row);
32385 defsubr (&Strace_redisplay);
32386 defsubr (&Strace_to_stderr);
32387 #endif
32388 #ifdef HAVE_WINDOW_SYSTEM
32389 defsubr (&Stool_bar_height);
32390 defsubr (&Slookup_image_map);
32391 #endif
32392 defsubr (&Sline_pixel_height);
32393 defsubr (&Sformat_mode_line);
32394 defsubr (&Sinvisible_p);
32395 defsubr (&Scurrent_bidi_paragraph_direction);
32396 defsubr (&Swindow_text_pixel_size);
32397 defsubr (&Smove_point_visually);
32398 defsubr (&Sbidi_find_overridden_directionality);
32400 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32401 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32402 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32403 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32404 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32405 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32406 DEFSYM (Qeval, "eval");
32407 DEFSYM (QCdata, ":data");
32409 /* Names of text properties relevant for redisplay. */
32410 DEFSYM (Qdisplay, "display");
32411 DEFSYM (Qspace_width, "space-width");
32412 DEFSYM (Qraise, "raise");
32413 DEFSYM (Qslice, "slice");
32414 DEFSYM (Qspace, "space");
32415 DEFSYM (Qmargin, "margin");
32416 DEFSYM (Qpointer, "pointer");
32417 DEFSYM (Qleft_margin, "left-margin");
32418 DEFSYM (Qright_margin, "right-margin");
32419 DEFSYM (Qcenter, "center");
32420 DEFSYM (Qline_height, "line-height");
32421 DEFSYM (QCalign_to, ":align-to");
32422 DEFSYM (QCrelative_width, ":relative-width");
32423 DEFSYM (QCrelative_height, ":relative-height");
32424 DEFSYM (QCeval, ":eval");
32425 DEFSYM (QCpropertize, ":propertize");
32426 DEFSYM (QCfile, ":file");
32427 DEFSYM (Qfontified, "fontified");
32428 DEFSYM (Qfontification_functions, "fontification-functions");
32430 /* Name of the symbol which disables Lisp evaluation in 'display'
32431 properties. This is used by enriched.el. */
32432 DEFSYM (Qdisable_eval, "disable-eval");
32434 /* Name of the face used to highlight trailing whitespace. */
32435 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32437 /* Names of the faces used to display line numbers. */
32438 DEFSYM (Qline_number, "line-number");
32439 DEFSYM (Qline_number_current_line, "line-number-current-line");
32440 /* Name of a text property which disables line-number display. */
32441 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32443 /* Name and number of the face used to highlight escape glyphs. */
32444 DEFSYM (Qescape_glyph, "escape-glyph");
32446 /* Name and number of the face used to highlight non-breaking
32447 spaces/hyphens. */
32448 DEFSYM (Qnobreak_space, "nobreak-space");
32449 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32451 /* The symbol 'image' which is the car of the lists used to represent
32452 images in Lisp. Also a tool bar style. */
32453 DEFSYM (Qimage, "image");
32455 /* Tool bar styles. */
32456 DEFSYM (Qtext, "text");
32457 DEFSYM (Qboth, "both");
32458 DEFSYM (Qboth_horiz, "both-horiz");
32459 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32461 /* The image map types. */
32462 DEFSYM (QCmap, ":map");
32463 DEFSYM (QCpointer, ":pointer");
32464 DEFSYM (Qrect, "rect");
32465 DEFSYM (Qcircle, "circle");
32466 DEFSYM (Qpoly, "poly");
32468 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32470 DEFSYM (Qgrow_only, "grow-only");
32471 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32472 DEFSYM (Qposition, "position");
32473 DEFSYM (Qbuffer_position, "buffer-position");
32474 DEFSYM (Qobject, "object");
32476 /* Cursor shapes. */
32477 DEFSYM (Qbar, "bar");
32478 DEFSYM (Qhbar, "hbar");
32479 DEFSYM (Qbox, "box");
32480 DEFSYM (Qhollow, "hollow");
32482 /* Pointer shapes. */
32483 DEFSYM (Qhand, "hand");
32484 DEFSYM (Qarrow, "arrow");
32485 /* also Qtext */
32487 DEFSYM (Qdragging, "dragging");
32489 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32491 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32492 staticpro (&list_of_error);
32494 /* Values of those variables at last redisplay are stored as
32495 properties on 'overlay-arrow-position' symbol. However, if
32496 Voverlay_arrow_position is a marker, last-arrow-position is its
32497 numerical position. */
32498 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32499 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32501 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32502 properties on a symbol in overlay-arrow-variable-list. */
32503 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32504 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32506 echo_buffer[0] = echo_buffer[1] = Qnil;
32507 staticpro (&echo_buffer[0]);
32508 staticpro (&echo_buffer[1]);
32510 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32511 staticpro (&echo_area_buffer[0]);
32512 staticpro (&echo_area_buffer[1]);
32514 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32515 staticpro (&Vmessages_buffer_name);
32517 mode_line_proptrans_alist = Qnil;
32518 staticpro (&mode_line_proptrans_alist);
32519 mode_line_string_list = Qnil;
32520 staticpro (&mode_line_string_list);
32521 mode_line_string_face = Qnil;
32522 staticpro (&mode_line_string_face);
32523 mode_line_string_face_prop = Qnil;
32524 staticpro (&mode_line_string_face_prop);
32525 Vmode_line_unwind_vector = Qnil;
32526 staticpro (&Vmode_line_unwind_vector);
32528 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32530 help_echo_string = Qnil;
32531 staticpro (&help_echo_string);
32532 help_echo_object = Qnil;
32533 staticpro (&help_echo_object);
32534 help_echo_window = Qnil;
32535 staticpro (&help_echo_window);
32536 previous_help_echo_string = Qnil;
32537 staticpro (&previous_help_echo_string);
32538 help_echo_pos = -1;
32540 DEFSYM (Qright_to_left, "right-to-left");
32541 DEFSYM (Qleft_to_right, "left-to-right");
32542 defsubr (&Sbidi_resolved_levels);
32544 #ifdef HAVE_WINDOW_SYSTEM
32545 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32546 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32547 For example, if a block cursor is over a tab, it will be drawn as
32548 wide as that tab on the display. */);
32549 x_stretch_cursor_p = 0;
32550 #endif
32552 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32553 doc: /* Non-nil means highlight trailing whitespace.
32554 The face used for trailing whitespace is `trailing-whitespace'. */);
32555 Vshow_trailing_whitespace = Qnil;
32557 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32558 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32559 If the value is t, Emacs highlights non-ASCII chars which have the
32560 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32561 or `nobreak-hyphen' face respectively.
32563 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32564 U+2011 (non-breaking hyphen) are affected.
32566 Any other non-nil value means to display these characters as an escape
32567 glyph followed by an ordinary space or hyphen.
32569 A value of nil means no special handling of these characters. */);
32570 Vnobreak_char_display = Qt;
32572 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32573 doc: /* The pointer shape to show in void text areas.
32574 A value of nil means to show the text pointer. Other options are
32575 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32576 `hourglass'. */);
32577 Vvoid_text_area_pointer = Qarrow;
32579 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32580 doc: /* Non-nil means don't actually do any redisplay.
32581 This is used for internal purposes. */);
32582 Vinhibit_redisplay = Qnil;
32584 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32585 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32586 Vglobal_mode_string = Qnil;
32588 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32589 doc: /* Marker for where to display an arrow on top of the buffer text.
32590 This must be the beginning of a line in order to work.
32591 See also `overlay-arrow-string'. */);
32592 Voverlay_arrow_position = Qnil;
32594 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32595 doc: /* String to display as an arrow in non-window frames.
32596 See also `overlay-arrow-position'. */);
32597 Voverlay_arrow_string = build_pure_c_string ("=>");
32599 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32600 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32601 The symbols on this list are examined during redisplay to determine
32602 where to display overlay arrows. */);
32603 Voverlay_arrow_variable_list
32604 = list1 (intern_c_string ("overlay-arrow-position"));
32606 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32607 doc: /* The number of lines to try scrolling a window by when point moves out.
32608 If that fails to bring point back on frame, point is centered instead.
32609 If this is zero, point is always centered after it moves off frame.
32610 If you want scrolling to always be a line at a time, you should set
32611 `scroll-conservatively' to a large value rather than set this to 1. */);
32613 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32614 doc: /* Scroll up to this many lines, to bring point back on screen.
32615 If point moves off-screen, redisplay will scroll by up to
32616 `scroll-conservatively' lines in order to bring point just barely
32617 onto the screen again. If that cannot be done, then redisplay
32618 recenters point as usual.
32620 If the value is greater than 100, redisplay will never recenter point,
32621 but will always scroll just enough text to bring point into view, even
32622 if you move far away.
32624 A value of zero means always recenter point if it moves off screen. */);
32625 scroll_conservatively = 0;
32627 DEFVAR_INT ("scroll-margin", scroll_margin,
32628 doc: /* Number of lines of margin at the top and bottom of a window.
32629 Trigger automatic scrolling whenever point gets within this many lines
32630 of the top or bottom of the window (see info node `Auto Scrolling'). */);
32631 scroll_margin = 0;
32633 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32634 doc: /* Maximum effective value of `scroll-margin'.
32635 Given as a fraction of the current window's lines. The value should
32636 be a floating point number between 0.0 and 0.5. The effective maximum
32637 is limited to (/ (1- window-lines) 2). Non-float values for this
32638 variable are ignored and the default 0.25 is used instead. */);
32639 Vmaximum_scroll_margin = make_float (0.25);
32641 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32642 doc: /* Pixels per inch value for non-window system displays.
32643 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32644 Vdisplay_pixels_per_inch = make_float (72.0);
32646 #ifdef GLYPH_DEBUG
32647 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32648 #endif
32650 DEFVAR_LISP ("truncate-partial-width-windows",
32651 Vtruncate_partial_width_windows,
32652 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32653 For an integer value, truncate lines in each window narrower than the
32654 full frame width, provided the total window width in column units is less
32655 than that integer; otherwise, respect the value of `truncate-lines'.
32656 The total width of the window is as returned by `window-total-width', it
32657 includes the fringes, the continuation and truncation glyphs, the
32658 display margins (if any), and the scroll bar
32660 For any other non-nil value, truncate lines in all windows that do
32661 not span the full frame width.
32663 A value of nil means to respect the value of `truncate-lines'.
32665 If `word-wrap' is enabled, you might want to reduce this. */);
32666 Vtruncate_partial_width_windows = make_number (50);
32668 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32669 doc: /* Maximum buffer size for which line number should be displayed.
32670 If the buffer is bigger than this, the line number does not appear
32671 in the mode line. A value of nil means no limit. */);
32672 Vline_number_display_limit = Qnil;
32674 DEFVAR_INT ("line-number-display-limit-width",
32675 line_number_display_limit_width,
32676 doc: /* Maximum line width (in characters) for line number display.
32677 If the average length of the lines near point is bigger than this, then the
32678 line number may be omitted from the mode line. */);
32679 line_number_display_limit_width = 200;
32681 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32682 doc: /* Non-nil means highlight region even in nonselected windows. */);
32683 highlight_nonselected_windows = false;
32685 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32686 doc: /* Non-nil if more than one frame is visible on this display.
32687 Minibuffer-only frames don't count, but iconified frames do.
32688 This variable is not guaranteed to be accurate except while processing
32689 `frame-title-format' and `icon-title-format'. */);
32691 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32692 doc: /* Template for displaying the title bar of visible frames.
32693 \(Assuming the window manager supports this feature.)
32695 This variable has the same structure as `mode-line-format', except that
32696 the %c, %C, and %l constructs are ignored. It is used only on frames for
32697 which no explicit name has been set (see `modify-frame-parameters'). */);
32699 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32700 doc: /* Template for displaying the title bar of an iconified frame.
32701 \(Assuming the window manager supports this feature.)
32702 This variable has the same structure as `mode-line-format' (which see),
32703 and is used only on frames for which no explicit name has been set
32704 \(see `modify-frame-parameters'). */);
32705 Vicon_title_format
32706 = Vframe_title_format
32707 = listn (CONSTYPE_PURE, 3,
32708 intern_c_string ("multiple-frames"),
32709 build_pure_c_string ("%b"),
32710 listn (CONSTYPE_PURE, 4,
32711 empty_unibyte_string,
32712 intern_c_string ("invocation-name"),
32713 build_pure_c_string ("@"),
32714 intern_c_string ("system-name")));
32716 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32717 doc: /* Maximum number of lines to keep in the message log buffer.
32718 If nil, disable message logging. If t, log messages but don't truncate
32719 the buffer when it becomes large. */);
32720 Vmessage_log_max = make_number (1000);
32722 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32723 doc: /* List of functions to call before redisplaying a window with scrolling.
32724 Each function is called with two arguments, the window and its new
32725 display-start position.
32726 These functions are called whenever the `window-start' marker is modified,
32727 either to point into another buffer (e.g. via `set-window-buffer') or another
32728 place in the same buffer.
32729 When each function is called, the `window-start' marker of its window
32730 argument has been already set to the new value, and the buffer which that
32731 window will display is set to be the current buffer.
32732 Note that the value of `window-end' is not valid when these functions are
32733 called.
32735 Warning: Do not use this feature to alter the way the window
32736 is scrolled. It is not designed for that, and such use probably won't
32737 work. */);
32738 Vwindow_scroll_functions = Qnil;
32740 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32741 doc: /* Functions called when redisplay of a window reaches the end trigger.
32742 Each function is called with two arguments, the window and the end trigger value.
32743 See `set-window-redisplay-end-trigger'. */);
32744 Vredisplay_end_trigger_functions = Qnil;
32746 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32747 doc: /* Non-nil means autoselect window with mouse pointer.
32748 If nil, do not autoselect windows.
32749 A positive number means delay autoselection by that many seconds: a
32750 window is autoselected only after the mouse has remained in that
32751 window for the duration of the delay.
32752 A negative number has a similar effect, but causes windows to be
32753 autoselected only after the mouse has stopped moving. (Because of
32754 the way Emacs compares mouse events, you will occasionally wait twice
32755 that time before the window gets selected.)
32756 Any other value means to autoselect window instantaneously when the
32757 mouse pointer enters it.
32759 Autoselection selects the minibuffer only if it is active, and never
32760 unselects the minibuffer if it is active.
32762 When customizing this variable make sure that the actual value of
32763 `focus-follows-mouse' matches the behavior of your window manager. */);
32764 Vmouse_autoselect_window = Qnil;
32766 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32767 doc: /* Non-nil means automatically resize tool-bars.
32768 This dynamically changes the tool-bar's height to the minimum height
32769 that is needed to make all tool-bar items visible.
32770 If value is `grow-only', the tool-bar's height is only increased
32771 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32772 Vauto_resize_tool_bars = Qt;
32774 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32775 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32776 auto_raise_tool_bar_buttons_p = true;
32778 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32779 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32780 make_cursor_line_fully_visible_p = true;
32782 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32783 doc: /* Border below tool-bar in pixels.
32784 If an integer, use it as the height of the border.
32785 If it is one of `internal-border-width' or `border-width', use the
32786 value of the corresponding frame parameter.
32787 Otherwise, no border is added below the tool-bar. */);
32788 Vtool_bar_border = Qinternal_border_width;
32790 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32791 doc: /* Margin around tool-bar buttons in pixels.
32792 If an integer, use that for both horizontal and vertical margins.
32793 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32794 HORZ specifying the horizontal margin, and VERT specifying the
32795 vertical margin. */);
32796 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32798 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32799 doc: /* Relief thickness of tool-bar buttons. */);
32800 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32802 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32803 doc: /* Tool bar style to use.
32804 It can be one of
32805 image - show images only
32806 text - show text only
32807 both - show both, text below image
32808 both-horiz - show text to the right of the image
32809 text-image-horiz - show text to the left of the image
32810 any other - use system default or image if no system default.
32812 This variable only affects the GTK+ toolkit version of Emacs. */);
32813 Vtool_bar_style = Qnil;
32815 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32816 doc: /* Maximum number of characters a label can have to be shown.
32817 The tool bar style must also show labels for this to have any effect, see
32818 `tool-bar-style'. */);
32819 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32821 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32822 doc: /* List of functions to call to fontify regions of text.
32823 Each function is called with one argument POS. Functions must
32824 fontify a region starting at POS in the current buffer, and give
32825 fontified regions the property `fontified'. */);
32826 Vfontification_functions = Qnil;
32827 Fmake_variable_buffer_local (Qfontification_functions);
32829 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32830 unibyte_display_via_language_environment,
32831 doc: /* Non-nil means display unibyte text according to language environment.
32832 Specifically, this means that raw bytes in the range 160-255 decimal
32833 are displayed by converting them to the equivalent multibyte characters
32834 according to the current language environment. As a result, they are
32835 displayed according to the current fontset.
32837 Note that this variable affects only how these bytes are displayed,
32838 but does not change the fact they are interpreted as raw bytes. */);
32839 unibyte_display_via_language_environment = false;
32841 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32842 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32843 If a float, it specifies a fraction of the mini-window frame's height.
32844 If an integer, it specifies a number of lines. */);
32845 Vmax_mini_window_height = make_float (0.25);
32847 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32848 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32849 A value of nil means don't automatically resize mini-windows.
32850 A value of t means resize them to fit the text displayed in them.
32851 A value of `grow-only', the default, means let mini-windows grow only;
32852 they return to their normal size when the minibuffer is closed, or the
32853 echo area becomes empty. */);
32854 /* Contrary to the doc string, we initialize this to nil, so that
32855 loading loadup.el won't try to resize windows before loading
32856 window.el, where some functions we need to call for this live.
32857 We assign the 'grow-only' value right after loading window.el
32858 during loadup. */
32859 Vresize_mini_windows = Qnil;
32861 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32862 doc: /* Alist specifying how to blink the cursor off.
32863 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32864 `cursor-type' frame-parameter or variable equals ON-STATE,
32865 comparing using `equal', Emacs uses OFF-STATE to specify
32866 how to blink it off. ON-STATE and OFF-STATE are values for
32867 the `cursor-type' frame parameter.
32869 If a frame's ON-STATE has no entry in this list,
32870 the frame's other specifications determine how to blink the cursor off. */);
32871 Vblink_cursor_alist = Qnil;
32873 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32874 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32875 The value `current-line' means the line displaying point in each window
32876 is automatically scrolled horizontally to make point visible.
32877 Any other non-nil value means all the lines in a window are automatically
32878 scrolled horizontally to make point visible. */);
32879 automatic_hscrolling = Qt;
32880 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32881 DEFSYM (Qcurrent_line, "current-line");
32883 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32884 doc: /* How many columns away from the window edge point is allowed to get
32885 before automatic hscrolling will horizontally scroll the window. */);
32886 hscroll_margin = 5;
32888 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32889 doc: /* How many columns to scroll the window when point gets too close to the edge.
32890 When point is less than `hscroll-margin' columns from the window
32891 edge, automatic hscrolling will scroll the window by the amount of columns
32892 determined by this variable. If its value is a positive integer, scroll that
32893 many columns. If it's a positive floating-point number, it specifies the
32894 fraction of the window's width to scroll. If it's nil or zero, point will be
32895 centered horizontally after the scroll. Any other value, including negative
32896 numbers, are treated as if the value were zero.
32898 Automatic hscrolling always moves point outside the scroll margin, so if
32899 point was more than scroll step columns inside the margin, the window will
32900 scroll more than the value given by the scroll step.
32902 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32903 and `scroll-right' overrides this variable's effect. */);
32904 Vhscroll_step = make_number (0);
32906 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32907 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32908 Bind this around calls to `message' to let it take effect. */);
32909 message_truncate_lines = false;
32911 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32912 doc: /* Normal hook run to update the menu bar definitions.
32913 Redisplay runs this hook before it redisplays the menu bar.
32914 This is used to update menus such as Buffers, whose contents depend on
32915 various data. */);
32916 Vmenu_bar_update_hook = Qnil;
32918 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32919 doc: /* Frame for which we are updating a menu.
32920 The enable predicate for a menu binding should check this variable. */);
32921 Vmenu_updating_frame = Qnil;
32923 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32924 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32925 inhibit_menubar_update = false;
32927 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32928 doc: /* Prefix prepended to all continuation lines at display time.
32929 The value may be a string, an image, or a stretch-glyph; it is
32930 interpreted in the same way as the value of a `display' text property.
32932 This variable is overridden by any `wrap-prefix' text or overlay
32933 property.
32935 To add a prefix to non-continuation lines, use `line-prefix'. */);
32936 Vwrap_prefix = Qnil;
32937 DEFSYM (Qwrap_prefix, "wrap-prefix");
32938 Fmake_variable_buffer_local (Qwrap_prefix);
32940 DEFVAR_LISP ("line-prefix", Vline_prefix,
32941 doc: /* Prefix prepended to all non-continuation lines at display time.
32942 The value may be a string, an image, or a stretch-glyph; it is
32943 interpreted in the same way as the value of a `display' text property.
32945 This variable is overridden by any `line-prefix' text or overlay
32946 property.
32948 To add a prefix to continuation lines, use `wrap-prefix'. */);
32949 Vline_prefix = Qnil;
32950 DEFSYM (Qline_prefix, "line-prefix");
32951 Fmake_variable_buffer_local (Qline_prefix);
32953 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32954 doc: /* Non-nil means display line numbers.
32955 If the value is t, display the absolute number of each line of a buffer
32956 shown in a window. Absolute line numbers count from the beginning of
32957 the current narrowing, or from buffer beginning. If the value is
32958 `relative', display for each line not containing the window's point its
32959 relative number instead, i.e. the number of the line relative to the
32960 line showing the window's point.
32962 In either case, line numbers are displayed at the beginning of each
32963 non-continuation line that displays buffer text, i.e. after each newline
32964 character that comes from the buffer. The value `visual' is like
32965 `relative' but counts screen lines instead of buffer lines. In practice
32966 this means that continuation lines count as well when calculating the
32967 relative number of a line.
32969 Lisp programs can disable display of a line number of a particular
32970 buffer line by putting the `display-line-numbers-disable' text property
32971 or overlay property on the first visible character of that line. */);
32972 Vdisplay_line_numbers = Qnil;
32973 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32974 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32975 DEFSYM (Qrelative, "relative");
32976 DEFSYM (Qvisual, "visual");
32978 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32979 doc: /* Minimum width of space reserved for line number display.
32980 A positive number means reserve that many columns for line numbers,
32981 even if the actual number needs less space.
32982 The default value of nil means compute the space dynamically.
32983 Any other value is treated as nil. */);
32984 Vdisplay_line_numbers_width = Qnil;
32985 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32986 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32988 DEFVAR_LISP ("display-line-numbers-current-absolute",
32989 Vdisplay_line_numbers_current_absolute,
32990 doc: /* Non-nil means display absolute number of current line.
32991 This variable has effect only when `display-line-numbers' is
32992 either `relative' or `visual'. */);
32993 Vdisplay_line_numbers_current_absolute = Qt;
32995 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32996 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32997 display_line_numbers_widen = false;
32998 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32999 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
33001 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
33002 doc: /* Non-nil means don't eval Lisp during redisplay. */);
33003 inhibit_eval_during_redisplay = false;
33005 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
33006 doc: /* Non-nil means don't free realized faces. Internal use only. */);
33007 inhibit_free_realized_faces = false;
33009 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
33010 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
33011 Intended for use during debugging and for testing bidi display;
33012 see biditest.el in the test suite. */);
33013 inhibit_bidi_mirroring = false;
33015 #ifdef GLYPH_DEBUG
33016 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
33017 doc: /* Inhibit try_window_id display optimization. */);
33018 inhibit_try_window_id = false;
33020 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
33021 doc: /* Inhibit try_window_reusing display optimization. */);
33022 inhibit_try_window_reusing = false;
33024 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
33025 doc: /* Inhibit try_cursor_movement display optimization. */);
33026 inhibit_try_cursor_movement = false;
33027 #endif /* GLYPH_DEBUG */
33029 DEFVAR_INT ("overline-margin", overline_margin,
33030 doc: /* Space between overline and text, in pixels.
33031 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
33032 margin to the character height. */);
33033 overline_margin = 2;
33035 DEFVAR_INT ("underline-minimum-offset",
33036 underline_minimum_offset,
33037 doc: /* Minimum distance between baseline and underline.
33038 This can improve legibility of underlined text at small font sizes,
33039 particularly when using variable `x-use-underline-position-properties'
33040 with fonts that specify an UNDERLINE_POSITION relatively close to the
33041 baseline. The default value is 1. */);
33042 underline_minimum_offset = 1;
33043 DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
33045 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
33046 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
33047 This feature only works when on a window system that can change
33048 cursor shapes. */);
33049 display_hourglass_p = true;
33051 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
33052 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
33053 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
33055 #ifdef HAVE_WINDOW_SYSTEM
33056 hourglass_atimer = NULL;
33057 hourglass_shown_p = false;
33058 #endif /* HAVE_WINDOW_SYSTEM */
33060 /* Name of the face used to display glyphless characters. */
33061 DEFSYM (Qglyphless_char, "glyphless-char");
33063 /* Method symbols for Vglyphless_char_display. */
33064 DEFSYM (Qhex_code, "hex-code");
33065 DEFSYM (Qempty_box, "empty-box");
33066 DEFSYM (Qthin_space, "thin-space");
33067 DEFSYM (Qzero_width, "zero-width");
33069 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
33070 doc: /* Function run just before redisplay.
33071 It is called with one argument, which is the set of windows that are to
33072 be redisplayed. This set can be nil (meaning, only the selected window),
33073 or t (meaning all windows). */);
33074 Vpre_redisplay_function = intern ("ignore");
33076 /* Symbol for the purpose of Vglyphless_char_display. */
33077 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
33078 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
33080 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
33081 doc: /* Char-table defining glyphless characters.
33082 Each element, if non-nil, should be one of the following:
33083 an ASCII acronym string: display this string in a box
33084 `hex-code': display the hexadecimal code of a character in a box
33085 `empty-box': display as an empty box
33086 `thin-space': display as 1-pixel width space
33087 `zero-width': don't display
33088 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
33089 display method for graphical terminals and text terminals respectively.
33090 GRAPHICAL and TEXT should each have one of the values listed above.
33092 The char-table has one extra slot to control the display of a character for
33093 which no font is found. This slot only takes effect on graphical terminals.
33094 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
33095 `thin-space'. The default is `empty-box'.
33097 If a character has a non-nil entry in an active display table, the
33098 display table takes effect; in this case, Emacs does not consult
33099 `glyphless-char-display' at all. */);
33100 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33101 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33102 Qempty_box);
33104 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33105 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33106 Vdebug_on_message = Qnil;
33108 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33109 doc: /* */);
33110 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33112 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33113 doc: /* */);
33114 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33116 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33117 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33118 /* Initialize to t, since we need to disable reordering until
33119 loadup.el successfully loads charprop.el. */
33120 redisplay__inhibit_bidi = true;
33122 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33123 doc: /* Non-nil means display raw bytes in hexadecimal format.
33124 The default is to use octal format (\200) whereas hexadecimal (\x80)
33125 may be more familiar to users. */);
33126 display_raw_bytes_as_hex = false;
33131 /* Initialize this module when Emacs starts. */
33133 void
33134 init_xdisp (void)
33136 CHARPOS (this_line_start_pos) = 0;
33138 if (!noninteractive)
33140 struct window *m = XWINDOW (minibuf_window);
33141 Lisp_Object frame = m->frame;
33142 struct frame *f = XFRAME (frame);
33143 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33144 struct window *r = XWINDOW (root);
33145 int i;
33147 echo_area_window = minibuf_window;
33149 r->top_line = FRAME_TOP_MARGIN (f);
33150 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33151 r->total_cols = FRAME_COLS (f);
33152 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33153 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33154 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33156 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33157 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33158 m->total_cols = FRAME_COLS (f);
33159 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33160 m->total_lines = 1;
33161 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33163 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33164 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33165 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33167 /* The default ellipsis glyphs `...'. */
33168 for (i = 0; i < 3; ++i)
33169 default_invis_vector[i] = make_number ('.');
33173 /* Allocate the buffer for frame titles.
33174 Also used for `format-mode-line'. */
33175 int size = 100;
33176 mode_line_noprop_buf = xmalloc (size);
33177 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33178 mode_line_noprop_ptr = mode_line_noprop_buf;
33179 mode_line_target = MODE_LINE_DISPLAY;
33182 help_echo_showing_p = false;
33185 #ifdef HAVE_WINDOW_SYSTEM
33187 /* Platform-independent portion of hourglass implementation. */
33189 /* Timer function of hourglass_atimer. */
33191 static void
33192 show_hourglass (struct atimer *timer)
33194 /* The timer implementation will cancel this timer automatically
33195 after this function has run. Set hourglass_atimer to null
33196 so that we know the timer doesn't have to be canceled. */
33197 hourglass_atimer = NULL;
33199 if (!hourglass_shown_p)
33201 Lisp_Object tail, frame;
33203 block_input ();
33205 FOR_EACH_FRAME (tail, frame)
33207 struct frame *f = XFRAME (frame);
33209 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33210 && FRAME_RIF (f)->show_hourglass)
33211 FRAME_RIF (f)->show_hourglass (f);
33214 hourglass_shown_p = true;
33215 unblock_input ();
33219 /* Cancel a currently active hourglass timer, and start a new one. */
33221 void
33222 start_hourglass (void)
33224 struct timespec delay;
33226 cancel_hourglass ();
33228 if (INTEGERP (Vhourglass_delay)
33229 && XINT (Vhourglass_delay) > 0)
33230 delay = make_timespec (min (XINT (Vhourglass_delay),
33231 TYPE_MAXIMUM (time_t)),
33233 else if (FLOATP (Vhourglass_delay)
33234 && XFLOAT_DATA (Vhourglass_delay) > 0)
33235 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33236 else
33237 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33239 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33240 show_hourglass, NULL);
33243 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33244 shown. */
33246 void
33247 cancel_hourglass (void)
33249 if (hourglass_atimer)
33251 cancel_atimer (hourglass_atimer);
33252 hourglass_atimer = NULL;
33255 if (hourglass_shown_p)
33257 Lisp_Object tail, frame;
33259 block_input ();
33261 FOR_EACH_FRAME (tail, frame)
33263 struct frame *f = XFRAME (frame);
33265 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33266 && FRAME_RIF (f)->hide_hourglass)
33267 FRAME_RIF (f)->hide_hourglass (f);
33268 #ifdef HAVE_NTGUI
33269 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33270 else if (!FRAME_W32_P (f))
33271 w32_arrow_cursor ();
33272 #endif
33275 hourglass_shown_p = false;
33276 unblock_input ();
33280 #endif /* HAVE_WINDOW_SYSTEM */