Merge from origin/emacs-26
[emacs.git] / src / xdisp.c
blobc0fdeca484737a906c4e471c3664696b0154b441
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;
10147 int move_op = MOVE_TO_POS | MOVE_TO_Y;
10148 int to_x = -1;
10149 if (!NILP (x_limit))
10151 it.last_visible_x = max_x;
10152 /* Actually, we never want move_it_to stop at to_x. But to make
10153 sure that move_it_in_display_line_to always moves far enough,
10154 we set to_x to INT_MAX and specify MOVE_TO_X. */
10155 move_op |= MOVE_TO_X;
10156 to_x = INT_MAX;
10159 void *it2data = NULL;
10160 struct it it2;
10161 SAVE_IT (it2, it, it2data);
10163 x = move_it_to (&it, end, to_x, max_y, -1, move_op);
10165 /* We could have a display property at END, in which case asking
10166 move_it_to to stop at END will overshoot and stop at position
10167 after END. So we try again, stopping before END, and account for
10168 the width of the last buffer position manually. */
10169 if (IT_CHARPOS (it) > end)
10171 end--;
10172 RESTORE_IT (&it, &it2, it2data);
10173 x = move_it_to (&it, end, to_x, max_y, -1, move_op);
10174 /* Add the width of the thing at TO, but only if we didn't
10175 overshoot it; if we did, it is already accounted for. */
10176 if (IT_CHARPOS (it) == end)
10177 x += it.pixel_width;
10179 if (!NILP (x_limit))
10181 /* Don't return more than X-LIMIT. */
10182 if (x > max_x)
10183 x = max_x;
10186 /* Subtract height of header-line which was counted automatically by
10187 start_display. */
10188 y = it.current_y + it.max_ascent + it.max_descent
10189 - WINDOW_HEADER_LINE_HEIGHT (w);
10190 /* Don't return more than Y-LIMIT. */
10191 if (y > max_y)
10192 y = max_y;
10194 if (EQ (mode_and_header_line, Qheader_line)
10195 || EQ (mode_and_header_line, Qt))
10196 /* Re-add height of header-line as requested. */
10197 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10199 if (EQ (mode_and_header_line, Qmode_line)
10200 || EQ (mode_and_header_line, Qt))
10201 /* Add height of mode-line as requested. */
10202 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10204 bidi_unshelve_cache (itdata, false);
10206 if (old_b)
10207 set_buffer_internal (old_b);
10209 return Fcons (make_number (x), make_number (y));
10212 /***********************************************************************
10213 Messages
10214 ***********************************************************************/
10216 /* Return the number of arguments the format string FORMAT needs. */
10218 static ptrdiff_t
10219 format_nargs (char const *format)
10221 ptrdiff_t nargs = 0;
10222 for (char const *p = format; (p = strchr (p, '%')); p++)
10223 if (p[1] == '%')
10224 p++;
10225 else
10226 nargs++;
10227 return nargs;
10230 /* Add a message with format string FORMAT and formatted arguments
10231 to *Messages*. */
10233 void
10234 add_to_log (const char *format, ...)
10236 va_list ap;
10237 va_start (ap, format);
10238 vadd_to_log (format, ap);
10239 va_end (ap);
10242 void
10243 vadd_to_log (char const *format, va_list ap)
10245 ptrdiff_t form_nargs = format_nargs (format);
10246 ptrdiff_t nargs = 1 + form_nargs;
10247 Lisp_Object args[10];
10248 eassert (nargs <= ARRAYELTS (args));
10249 AUTO_STRING (args0, format);
10250 args[0] = args0;
10251 for (ptrdiff_t i = 1; i <= nargs; i++)
10252 args[i] = va_arg (ap, Lisp_Object);
10253 Lisp_Object msg = Qnil;
10254 msg = Fformat_message (nargs, args);
10256 ptrdiff_t len = SBYTES (msg) + 1;
10257 USE_SAFE_ALLOCA;
10258 char *buffer = SAFE_ALLOCA (len);
10259 memcpy (buffer, SDATA (msg), len);
10261 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10262 SAFE_FREE ();
10266 /* Output a newline in the *Messages* buffer if "needs" one. */
10268 void
10269 message_log_maybe_newline (void)
10271 if (message_log_need_newline)
10272 message_dolog ("", 0, true, false);
10276 /* Add a string M of length NBYTES to the message log, optionally
10277 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10278 true, means interpret the contents of M as multibyte. This
10279 function calls low-level routines in order to bypass text property
10280 hooks, etc. which might not be safe to run.
10282 This may GC (insert may run before/after change hooks),
10283 so the buffer M must NOT point to a Lisp string. */
10285 void
10286 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10288 const unsigned char *msg = (const unsigned char *) m;
10290 if (!NILP (Vmemory_full))
10291 return;
10293 if (!NILP (Vmessage_log_max))
10295 struct buffer *oldbuf;
10296 Lisp_Object oldpoint, oldbegv, oldzv;
10297 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10298 ptrdiff_t point_at_end = 0;
10299 ptrdiff_t zv_at_end = 0;
10300 Lisp_Object old_deactivate_mark;
10302 old_deactivate_mark = Vdeactivate_mark;
10303 oldbuf = current_buffer;
10305 /* Ensure the Messages buffer exists, and switch to it.
10306 If we created it, set the major-mode. */
10307 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10308 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10309 if (newbuffer
10310 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10311 call0 (intern ("messages-buffer-mode"));
10313 bset_undo_list (current_buffer, Qt);
10314 bset_cache_long_scans (current_buffer, Qnil);
10316 oldpoint = message_dolog_marker1;
10317 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10318 oldbegv = message_dolog_marker2;
10319 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10320 oldzv = message_dolog_marker3;
10321 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10323 if (PT == Z)
10324 point_at_end = 1;
10325 if (ZV == Z)
10326 zv_at_end = 1;
10328 BEGV = BEG;
10329 BEGV_BYTE = BEG_BYTE;
10330 ZV = Z;
10331 ZV_BYTE = Z_BYTE;
10332 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10334 /* Insert the string--maybe converting multibyte to single byte
10335 or vice versa, so that all the text fits the buffer. */
10336 if (multibyte
10337 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10339 ptrdiff_t i;
10340 int c, char_bytes;
10341 char work[1];
10343 /* Convert a multibyte string to single-byte
10344 for the *Message* buffer. */
10345 for (i = 0; i < nbytes; i += char_bytes)
10347 c = string_char_and_length (msg + i, &char_bytes);
10348 work[0] = CHAR_TO_BYTE8 (c);
10349 insert_1_both (work, 1, 1, true, false, false);
10352 else if (! multibyte
10353 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10355 ptrdiff_t i;
10356 int c, char_bytes;
10357 unsigned char str[MAX_MULTIBYTE_LENGTH];
10358 /* Convert a single-byte string to multibyte
10359 for the *Message* buffer. */
10360 for (i = 0; i < nbytes; i++)
10362 c = msg[i];
10363 MAKE_CHAR_MULTIBYTE (c);
10364 char_bytes = CHAR_STRING (c, str);
10365 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10368 else if (nbytes)
10369 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10370 true, false, false);
10372 if (nlflag)
10374 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10375 printmax_t dups;
10377 insert_1_both ("\n", 1, 1, true, false, false);
10379 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10380 this_bol = PT;
10381 this_bol_byte = PT_BYTE;
10383 /* See if this line duplicates the previous one.
10384 If so, combine duplicates. */
10385 if (this_bol > BEG)
10387 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10388 prev_bol = PT;
10389 prev_bol_byte = PT_BYTE;
10391 dups = message_log_check_duplicate (prev_bol_byte,
10392 this_bol_byte);
10393 if (dups)
10395 del_range_both (prev_bol, prev_bol_byte,
10396 this_bol, this_bol_byte, false);
10397 if (dups > 1)
10399 char dupstr[sizeof " [ times]"
10400 + INT_STRLEN_BOUND (printmax_t)];
10402 /* If you change this format, don't forget to also
10403 change message_log_check_duplicate. */
10404 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10405 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10406 insert_1_both (dupstr, duplen, duplen,
10407 true, false, true);
10412 /* If we have more than the desired maximum number of lines
10413 in the *Messages* buffer now, delete the oldest ones.
10414 This is safe because we don't have undo in this buffer. */
10416 if (NATNUMP (Vmessage_log_max))
10418 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10419 -XFASTINT (Vmessage_log_max) - 1, false);
10420 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10423 BEGV = marker_position (oldbegv);
10424 BEGV_BYTE = marker_byte_position (oldbegv);
10426 if (zv_at_end)
10428 ZV = Z;
10429 ZV_BYTE = Z_BYTE;
10431 else
10433 ZV = marker_position (oldzv);
10434 ZV_BYTE = marker_byte_position (oldzv);
10437 if (point_at_end)
10438 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10439 else
10440 /* We can't do Fgoto_char (oldpoint) because it will run some
10441 Lisp code. */
10442 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10443 marker_byte_position (oldpoint));
10445 unchain_marker (XMARKER (oldpoint));
10446 unchain_marker (XMARKER (oldbegv));
10447 unchain_marker (XMARKER (oldzv));
10449 /* We called insert_1_both above with its 5th argument (PREPARE)
10450 false, which prevents insert_1_both from calling
10451 prepare_to_modify_buffer, which in turns prevents us from
10452 incrementing windows_or_buffers_changed even if *Messages* is
10453 shown in some window. So we must manually set
10454 windows_or_buffers_changed here to make up for that. */
10455 windows_or_buffers_changed = old_windows_or_buffers_changed;
10456 bset_redisplay (current_buffer);
10458 set_buffer_internal (oldbuf);
10460 message_log_need_newline = !nlflag;
10461 Vdeactivate_mark = old_deactivate_mark;
10466 /* We are at the end of the buffer after just having inserted a newline.
10467 (Note: We depend on the fact we won't be crossing the gap.)
10468 Check to see if the most recent message looks a lot like the previous one.
10469 Return 0 if different, 1 if the new one should just replace it, or a
10470 value N > 1 if we should also append " [N times]". */
10472 static intmax_t
10473 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10475 ptrdiff_t i;
10476 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10477 bool seen_dots = false;
10478 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10479 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10481 for (i = 0; i < len; i++)
10483 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10484 seen_dots = true;
10485 if (p1[i] != p2[i])
10486 return seen_dots;
10488 p1 += len;
10489 if (*p1 == '\n')
10490 return 2;
10491 if (*p1++ == ' ' && *p1++ == '[')
10493 char *pend;
10494 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10495 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10496 return n + 1;
10498 return 0;
10502 /* Display an echo area message M with a specified length of NBYTES
10503 bytes. The string may include null characters. If M is not a
10504 string, clear out any existing message, and let the mini-buffer
10505 text show through.
10507 This function cancels echoing. */
10509 void
10510 message3 (Lisp_Object m)
10512 clear_message (true, true);
10513 cancel_echoing ();
10515 /* First flush out any partial line written with print. */
10516 message_log_maybe_newline ();
10517 if (STRINGP (m))
10519 ptrdiff_t nbytes = SBYTES (m);
10520 bool multibyte = STRING_MULTIBYTE (m);
10521 char *buffer;
10522 USE_SAFE_ALLOCA;
10523 SAFE_ALLOCA_STRING (buffer, m);
10524 message_dolog (buffer, nbytes, true, multibyte);
10525 SAFE_FREE ();
10527 if (! inhibit_message)
10528 message3_nolog (m);
10531 /* Log the message M to stderr. Log an empty line if M is not a string. */
10533 static void
10534 message_to_stderr (Lisp_Object m)
10536 if (noninteractive_need_newline)
10538 noninteractive_need_newline = false;
10539 fputc ('\n', stderr);
10541 if (STRINGP (m))
10543 Lisp_Object coding_system = Vlocale_coding_system;
10544 Lisp_Object s;
10546 if (!NILP (Vcoding_system_for_write))
10547 coding_system = Vcoding_system_for_write;
10548 if (!NILP (coding_system))
10549 s = code_convert_string_norecord (m, coding_system, true);
10550 else
10551 s = m;
10553 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10555 if (!cursor_in_echo_area)
10556 fputc ('\n', stderr);
10557 fflush (stderr);
10560 /* The non-logging version of message3.
10561 This does not cancel echoing, because it is used for echoing.
10562 Perhaps we need to make a separate function for echoing
10563 and make this cancel echoing. */
10565 void
10566 message3_nolog (Lisp_Object m)
10568 struct frame *sf = SELECTED_FRAME ();
10570 if (FRAME_INITIAL_P (sf))
10571 message_to_stderr (m);
10572 /* Error messages get reported properly by cmd_error, so this must be just an
10573 informative message; if the frame hasn't really been initialized yet, just
10574 toss it. */
10575 else if (INTERACTIVE && sf->glyphs_initialized_p)
10577 /* Get the frame containing the mini-buffer
10578 that the selected frame is using. */
10579 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10580 Lisp_Object frame = XWINDOW (mini_window)->frame;
10581 struct frame *f = XFRAME (frame);
10583 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10584 Fmake_frame_visible (frame);
10586 if (STRINGP (m) && SCHARS (m) > 0)
10588 set_message (m);
10589 if (minibuffer_auto_raise)
10590 Fraise_frame (frame);
10591 /* Assume we are not echoing.
10592 (If we are, echo_now will override this.) */
10593 echo_message_buffer = Qnil;
10595 else
10596 clear_message (true, true);
10598 do_pending_window_change (false);
10599 echo_area_display (true);
10600 do_pending_window_change (false);
10601 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10602 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10607 /* Display a null-terminated echo area message M. If M is 0, clear
10608 out any existing message, and let the mini-buffer text show through.
10610 The buffer M must continue to exist until after the echo area gets
10611 cleared or some other message gets displayed there. Do not pass
10612 text that is stored in a Lisp string. Do not pass text in a buffer
10613 that was alloca'd. */
10615 void
10616 message1 (const char *m)
10618 message3 (m ? build_unibyte_string (m) : Qnil);
10622 /* The non-logging counterpart of message1. */
10624 void
10625 message1_nolog (const char *m)
10627 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10630 /* Display a message M which contains a single %s
10631 which gets replaced with STRING. */
10633 void
10634 message_with_string (const char *m, Lisp_Object string, bool log)
10636 CHECK_STRING (string);
10638 bool need_message;
10639 if (noninteractive)
10640 need_message = !!m;
10641 else if (!INTERACTIVE)
10642 need_message = false;
10643 else
10645 /* The frame whose minibuffer we're going to display the message on.
10646 It may be larger than the selected frame, so we need
10647 to use its buffer, not the selected frame's buffer. */
10648 Lisp_Object mini_window;
10649 struct frame *f, *sf = SELECTED_FRAME ();
10651 /* Get the frame containing the minibuffer
10652 that the selected frame is using. */
10653 mini_window = FRAME_MINIBUF_WINDOW (sf);
10654 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10656 /* Error messages get reported properly by cmd_error, so this must be
10657 just an informative message; if the frame hasn't really been
10658 initialized yet, just toss it. */
10659 need_message = f->glyphs_initialized_p;
10662 if (need_message)
10664 AUTO_STRING (fmt, m);
10665 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10667 if (noninteractive)
10668 message_to_stderr (msg);
10669 else
10671 if (log)
10672 message3 (msg);
10673 else
10674 message3_nolog (msg);
10676 /* Print should start at the beginning of the message
10677 buffer next time. */
10678 message_buf_print = false;
10684 /* Dump an informative message to the minibuf. If M is 0, clear out
10685 any existing message, and let the mini-buffer text show through.
10687 The message must be safe ASCII (because when Emacs is
10688 non-interactive the message is sent straight to stderr without
10689 encoding first) and the format must not contain ` or ' (because
10690 this function does not account for `text-quoting-style'). If your
10691 message and format do not fit into this category, convert your
10692 arguments to Lisp objects and use Fmessage instead. */
10694 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10695 vmessage (const char *m, va_list ap)
10697 if (noninteractive)
10699 if (m)
10701 if (noninteractive_need_newline)
10702 putc ('\n', stderr);
10703 noninteractive_need_newline = false;
10704 vfprintf (stderr, m, ap);
10705 if (!cursor_in_echo_area)
10706 fprintf (stderr, "\n");
10707 fflush (stderr);
10710 else if (INTERACTIVE)
10712 /* The frame whose mini-buffer we're going to display the message
10713 on. It may be larger than the selected frame, so we need to
10714 use its buffer, not the selected frame's buffer. */
10715 Lisp_Object mini_window;
10716 struct frame *f, *sf = SELECTED_FRAME ();
10718 /* Get the frame containing the mini-buffer
10719 that the selected frame is using. */
10720 mini_window = FRAME_MINIBUF_WINDOW (sf);
10721 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10723 /* Error messages get reported properly by cmd_error, so this must be
10724 just an informative message; if the frame hasn't really been
10725 initialized yet, just toss it. */
10726 if (f->glyphs_initialized_p)
10728 if (m)
10730 ptrdiff_t len;
10731 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10732 USE_SAFE_ALLOCA;
10733 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10735 len = doprnt (message_buf, maxsize, m, 0, ap);
10737 message3 (make_string (message_buf, len));
10738 SAFE_FREE ();
10740 else
10741 message1 (0);
10743 /* Print should start at the beginning of the message
10744 buffer next time. */
10745 message_buf_print = false;
10750 /* See vmessage for restrictions on the text of the message. */
10751 void
10752 message (const char *m, ...)
10754 va_list ap;
10755 va_start (ap, m);
10756 vmessage (m, ap);
10757 va_end (ap);
10761 /* Display the current message in the current mini-buffer. This is
10762 only called from error handlers in process.c, and is not time
10763 critical. */
10765 void
10766 update_echo_area (void)
10768 if (!NILP (echo_area_buffer[0]))
10770 Lisp_Object string;
10771 string = Fcurrent_message ();
10772 message3 (string);
10777 /* Make sure echo area buffers in `echo_buffers' are live.
10778 If they aren't, make new ones. */
10780 static void
10781 ensure_echo_area_buffers (void)
10783 for (int i = 0; i < 2; i++)
10784 if (!BUFFERP (echo_buffer[i])
10785 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10787 Lisp_Object old_buffer = echo_buffer[i];
10788 static char const name_fmt[] = " *Echo Area %d*";
10789 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10790 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10791 echo_buffer[i] = Fget_buffer_create (lname);
10792 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10793 /* to force word wrap in echo area -
10794 it was decided to postpone this*/
10795 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10797 for (int j = 0; j < 2; j++)
10798 if (EQ (old_buffer, echo_area_buffer[j]))
10799 echo_area_buffer[j] = echo_buffer[i];
10804 /* Call FN with args A1..A2 with either the current or last displayed
10805 echo_area_buffer as current buffer.
10807 WHICH zero means use the current message buffer
10808 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10809 from echo_buffer[] and clear it.
10811 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10812 suitable buffer from echo_buffer[] and clear it.
10814 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10815 that the current message becomes the last displayed one, choose a
10816 suitable buffer for echo_area_buffer[0], and clear it.
10818 Value is what FN returns. */
10820 static bool
10821 with_echo_area_buffer (struct window *w, int which,
10822 bool (*fn) (ptrdiff_t, Lisp_Object),
10823 ptrdiff_t a1, Lisp_Object a2)
10825 Lisp_Object buffer;
10826 bool this_one, the_other, clear_buffer_p, rc;
10827 ptrdiff_t count = SPECPDL_INDEX ();
10829 /* If buffers aren't live, make new ones. */
10830 ensure_echo_area_buffers ();
10832 clear_buffer_p = false;
10834 if (which == 0)
10835 this_one = false, the_other = true;
10836 else if (which > 0)
10837 this_one = true, the_other = false;
10838 else
10840 this_one = false, the_other = true;
10841 clear_buffer_p = true;
10843 /* We need a fresh one in case the current echo buffer equals
10844 the one containing the last displayed echo area message. */
10845 if (!NILP (echo_area_buffer[this_one])
10846 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10847 echo_area_buffer[this_one] = Qnil;
10850 /* Choose a suitable buffer from echo_buffer[] if we don't
10851 have one. */
10852 if (NILP (echo_area_buffer[this_one]))
10854 echo_area_buffer[this_one]
10855 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10856 ? echo_buffer[the_other]
10857 : echo_buffer[this_one]);
10858 clear_buffer_p = true;
10861 buffer = echo_area_buffer[this_one];
10863 /* Don't get confused by reusing the buffer used for echoing
10864 for a different purpose. */
10865 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10866 cancel_echoing ();
10868 record_unwind_protect (unwind_with_echo_area_buffer,
10869 with_echo_area_buffer_unwind_data (w));
10871 /* Make the echo area buffer current. Note that for display
10872 purposes, it is not necessary that the displayed window's buffer
10873 == current_buffer, except for text property lookup. So, let's
10874 only set that buffer temporarily here without doing a full
10875 Fset_window_buffer. We must also change w->pointm, though,
10876 because otherwise an assertions in unshow_buffer fails, and Emacs
10877 aborts. */
10878 set_buffer_internal_1 (XBUFFER (buffer));
10879 if (w)
10881 wset_buffer (w, buffer);
10882 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10883 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10886 bset_undo_list (current_buffer, Qt);
10887 bset_read_only (current_buffer, Qnil);
10888 specbind (Qinhibit_read_only, Qt);
10889 specbind (Qinhibit_modification_hooks, Qt);
10891 if (clear_buffer_p && Z > BEG)
10892 del_range (BEG, Z);
10894 eassert (BEGV >= BEG);
10895 eassert (ZV <= Z && ZV >= BEGV);
10897 rc = fn (a1, a2);
10899 eassert (BEGV >= BEG);
10900 eassert (ZV <= Z && ZV >= BEGV);
10902 unbind_to (count, Qnil);
10903 return rc;
10907 /* Save state that should be preserved around the call to the function
10908 FN called in with_echo_area_buffer. */
10910 static Lisp_Object
10911 with_echo_area_buffer_unwind_data (struct window *w)
10913 int i = 0;
10914 Lisp_Object vector, tmp;
10916 /* Reduce consing by keeping one vector in
10917 Vwith_echo_area_save_vector. */
10918 vector = Vwith_echo_area_save_vector;
10919 Vwith_echo_area_save_vector = Qnil;
10921 if (NILP (vector))
10922 vector = Fmake_vector (make_number (11), Qnil);
10924 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10925 ASET (vector, i, Vdeactivate_mark); ++i;
10926 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10928 if (w)
10930 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10931 ASET (vector, i, w->contents); ++i;
10932 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10933 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10934 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10935 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10936 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10937 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10939 else
10941 int end = i + 8;
10942 for (; i < end; ++i)
10943 ASET (vector, i, Qnil);
10946 eassert (i == ASIZE (vector));
10947 return vector;
10951 /* Restore global state from VECTOR which was created by
10952 with_echo_area_buffer_unwind_data. */
10954 static void
10955 unwind_with_echo_area_buffer (Lisp_Object vector)
10957 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10958 Vdeactivate_mark = AREF (vector, 1);
10959 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10961 if (WINDOWP (AREF (vector, 3)))
10963 struct window *w;
10964 Lisp_Object buffer;
10966 w = XWINDOW (AREF (vector, 3));
10967 buffer = AREF (vector, 4);
10969 wset_buffer (w, buffer);
10970 set_marker_both (w->pointm, buffer,
10971 XFASTINT (AREF (vector, 5)),
10972 XFASTINT (AREF (vector, 6)));
10973 set_marker_both (w->old_pointm, buffer,
10974 XFASTINT (AREF (vector, 7)),
10975 XFASTINT (AREF (vector, 8)));
10976 set_marker_both (w->start, buffer,
10977 XFASTINT (AREF (vector, 9)),
10978 XFASTINT (AREF (vector, 10)));
10981 Vwith_echo_area_save_vector = vector;
10985 /* Set up the echo area for use by print functions. MULTIBYTE_P
10986 means we will print multibyte. */
10988 void
10989 setup_echo_area_for_printing (bool multibyte_p)
10991 /* If we can't find an echo area any more, exit. */
10992 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10993 Fkill_emacs (Qnil);
10995 ensure_echo_area_buffers ();
10997 if (!message_buf_print)
10999 /* A message has been output since the last time we printed.
11000 Choose a fresh echo area buffer. */
11001 if (EQ (echo_area_buffer[1], echo_buffer[0]))
11002 echo_area_buffer[0] = echo_buffer[1];
11003 else
11004 echo_area_buffer[0] = echo_buffer[0];
11006 /* Switch to that buffer and clear it. */
11007 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11008 bset_truncate_lines (current_buffer, Qnil);
11010 if (Z > BEG)
11012 ptrdiff_t count = SPECPDL_INDEX ();
11013 specbind (Qinhibit_read_only, Qt);
11014 /* Note that undo recording is always disabled. */
11015 del_range (BEG, Z);
11016 unbind_to (count, Qnil);
11018 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11020 /* Set up the buffer for the multibyteness we need. We always
11021 set it to be multibyte, except when
11022 unibyte-display-via-language-environment is non-nil and the
11023 buffer from which we are called is unibyte, because in that
11024 case unibyte characters should not be displayed as octal
11025 escapes. */
11026 if (unibyte_display_via_language_environment
11027 && !multibyte_p
11028 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11029 Fset_buffer_multibyte (Qnil);
11030 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
11031 Fset_buffer_multibyte (Qt);
11033 /* Raise the frame containing the echo area. */
11034 if (minibuffer_auto_raise)
11036 struct frame *sf = SELECTED_FRAME ();
11037 Lisp_Object mini_window;
11038 mini_window = FRAME_MINIBUF_WINDOW (sf);
11039 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
11042 message_log_maybe_newline ();
11043 message_buf_print = true;
11045 else
11047 if (NILP (echo_area_buffer[0]))
11049 if (EQ (echo_area_buffer[1], echo_buffer[0]))
11050 echo_area_buffer[0] = echo_buffer[1];
11051 else
11052 echo_area_buffer[0] = echo_buffer[0];
11055 if (current_buffer != XBUFFER (echo_area_buffer[0]))
11057 /* Someone switched buffers between print requests. */
11058 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11059 bset_truncate_lines (current_buffer, Qnil);
11065 /* Display an echo area message in window W. Value is true if W's
11066 height is changed. If display_last_displayed_message_p,
11067 display the message that was last displayed, otherwise
11068 display the current message. */
11070 static bool
11071 display_echo_area (struct window *w)
11073 bool no_message_p, window_height_changed_p;
11075 /* Temporarily disable garbage collections while displaying the echo
11076 area. This is done because a GC can print a message itself.
11077 That message would modify the echo area buffer's contents while a
11078 redisplay of the buffer is going on, and seriously confuse
11079 redisplay. */
11080 ptrdiff_t count = inhibit_garbage_collection ();
11082 /* If there is no message, we must call display_echo_area_1
11083 nevertheless because it resizes the window. But we will have to
11084 reset the echo_area_buffer in question to nil at the end because
11085 with_echo_area_buffer will sets it to an empty buffer. */
11086 bool i = display_last_displayed_message_p;
11087 /* According to the C99, C11 and C++11 standards, the integral value
11088 of a "bool" is always 0 or 1, so this array access is safe here,
11089 if oddly typed. */
11090 no_message_p = NILP (echo_area_buffer[i]);
11092 window_height_changed_p
11093 = with_echo_area_buffer (w, display_last_displayed_message_p,
11094 display_echo_area_1,
11095 (intptr_t) w, Qnil);
11097 if (no_message_p)
11098 echo_area_buffer[i] = Qnil;
11100 unbind_to (count, Qnil);
11101 return window_height_changed_p;
11105 /* Helper for display_echo_area. Display the current buffer which
11106 contains the current echo area message in window W, a mini-window,
11107 a pointer to which is passed in A1. A2..A4 are currently not used.
11108 Change the height of W so that all of the message is displayed.
11109 Value is true if height of W was changed. */
11111 static bool
11112 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11114 intptr_t i1 = a1;
11115 struct window *w = (struct window *) i1;
11116 Lisp_Object window;
11117 struct text_pos start;
11119 /* We are about to enter redisplay without going through
11120 redisplay_internal, so we need to forget these faces by hand
11121 here. */
11122 forget_escape_and_glyphless_faces ();
11124 /* Do this before displaying, so that we have a large enough glyph
11125 matrix for the display. If we can't get enough space for the
11126 whole text, display the last N lines. That works by setting w->start. */
11127 bool window_height_changed_p = resize_mini_window (w, false);
11129 /* Use the starting position chosen by resize_mini_window. */
11130 SET_TEXT_POS_FROM_MARKER (start, w->start);
11132 /* Display. */
11133 clear_glyph_matrix (w->desired_matrix);
11134 XSETWINDOW (window, w);
11135 try_window (window, start, 0);
11137 return window_height_changed_p;
11141 /* Resize the echo area window to exactly the size needed for the
11142 currently displayed message, if there is one. If a mini-buffer
11143 is active, don't shrink it. */
11145 void
11146 resize_echo_area_exactly (void)
11148 if (BUFFERP (echo_area_buffer[0])
11149 && WINDOWP (echo_area_window))
11151 struct window *w = XWINDOW (echo_area_window);
11152 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11153 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11154 (intptr_t) w, resize_exactly);
11155 if (resized_p)
11157 windows_or_buffers_changed = 42;
11158 update_mode_lines = 30;
11159 redisplay_internal ();
11165 /* Callback function for with_echo_area_buffer, when used from
11166 resize_echo_area_exactly. A1 contains a pointer to the window to
11167 resize, EXACTLY non-nil means resize the mini-window exactly to the
11168 size of the text displayed. A3 and A4 are not used. Value is what
11169 resize_mini_window returns. */
11171 static bool
11172 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11174 intptr_t i1 = a1;
11175 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11179 /* Resize mini-window W to fit the size of its contents. EXACT_P
11180 means size the window exactly to the size needed. Otherwise, it's
11181 only enlarged until W's buffer is empty.
11183 Set W->start to the right place to begin display. If the whole
11184 contents fit, start at the beginning. Otherwise, start so as
11185 to make the end of the contents appear. This is particularly
11186 important for y-or-n-p, but seems desirable generally.
11188 Value is true if the window height has been changed. */
11190 bool
11191 resize_mini_window (struct window *w, bool exact_p)
11193 struct frame *f = XFRAME (w->frame);
11194 bool window_height_changed_p = false;
11196 eassert (MINI_WINDOW_P (w));
11198 /* By default, start display at the beginning. */
11199 set_marker_both (w->start, w->contents,
11200 BUF_BEGV (XBUFFER (w->contents)),
11201 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11203 /* Don't resize windows while redisplaying a window; it would
11204 confuse redisplay functions when the size of the window they are
11205 displaying changes from under them. Such a resizing can happen,
11206 for instance, when which-func prints a long message while
11207 we are running fontification-functions. We're running these
11208 functions with safe_call which binds inhibit-redisplay to t. */
11209 if (!NILP (Vinhibit_redisplay))
11210 return false;
11212 /* Nil means don't try to resize. */
11213 if (NILP (Vresize_mini_windows)
11214 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11215 return false;
11217 if (!FRAME_MINIBUF_ONLY_P (f))
11219 struct it it;
11220 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11221 + WINDOW_PIXEL_HEIGHT (w));
11222 int unit = FRAME_LINE_HEIGHT (f);
11223 int height, max_height;
11224 struct text_pos start;
11225 struct buffer *old_current_buffer = NULL;
11227 if (current_buffer != XBUFFER (w->contents))
11229 old_current_buffer = current_buffer;
11230 set_buffer_internal (XBUFFER (w->contents));
11233 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11235 /* Compute the max. number of lines specified by the user. */
11236 if (FLOATP (Vmax_mini_window_height))
11237 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11238 else if (INTEGERP (Vmax_mini_window_height))
11239 max_height = XINT (Vmax_mini_window_height) * unit;
11240 else
11241 max_height = total_height / 4;
11243 /* Correct that max. height if it's bogus. */
11244 max_height = clip_to_bounds (unit, max_height, total_height);
11246 /* Find out the height of the text in the window. */
11247 if (it.line_wrap == TRUNCATE)
11248 height = unit;
11249 else
11251 last_height = 0;
11252 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11253 if (it.max_ascent == 0 && it.max_descent == 0)
11254 height = it.current_y + last_height;
11255 else
11256 height = it.current_y + it.max_ascent + it.max_descent;
11257 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11260 /* Compute a suitable window start. */
11261 if (height > max_height)
11263 height = (max_height / unit) * unit;
11264 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11265 move_it_vertically_backward (&it, height - unit);
11266 start = it.current.pos;
11268 else
11269 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11270 SET_MARKER_FROM_TEXT_POS (w->start, start);
11272 if (EQ (Vresize_mini_windows, Qgrow_only))
11274 /* Let it grow only, until we display an empty message, in which
11275 case the window shrinks again. */
11276 if (height > WINDOW_PIXEL_HEIGHT (w))
11278 int old_height = WINDOW_PIXEL_HEIGHT (w);
11280 FRAME_WINDOWS_FROZEN (f) = true;
11281 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11282 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11284 else if (height < WINDOW_PIXEL_HEIGHT (w)
11285 && (exact_p || BEGV == ZV))
11287 int old_height = WINDOW_PIXEL_HEIGHT (w);
11289 FRAME_WINDOWS_FROZEN (f) = false;
11290 shrink_mini_window (w, true);
11291 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11294 else
11296 /* Always resize to exact size needed. */
11297 if (height > WINDOW_PIXEL_HEIGHT (w))
11299 int old_height = WINDOW_PIXEL_HEIGHT (w);
11301 FRAME_WINDOWS_FROZEN (f) = true;
11302 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11303 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11305 else if (height < WINDOW_PIXEL_HEIGHT (w))
11307 int old_height = WINDOW_PIXEL_HEIGHT (w);
11309 FRAME_WINDOWS_FROZEN (f) = false;
11310 shrink_mini_window (w, true);
11312 if (height)
11314 FRAME_WINDOWS_FROZEN (f) = true;
11315 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11318 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11322 if (old_current_buffer)
11323 set_buffer_internal (old_current_buffer);
11326 return window_height_changed_p;
11330 /* Value is the current message, a string, or nil if there is no
11331 current message. */
11333 Lisp_Object
11334 current_message (void)
11336 Lisp_Object msg;
11338 if (!BUFFERP (echo_area_buffer[0]))
11339 msg = Qnil;
11340 else
11342 with_echo_area_buffer (0, 0, current_message_1,
11343 (intptr_t) &msg, Qnil);
11344 if (NILP (msg))
11345 echo_area_buffer[0] = Qnil;
11348 return msg;
11352 static bool
11353 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11355 intptr_t i1 = a1;
11356 Lisp_Object *msg = (Lisp_Object *) i1;
11358 if (Z > BEG)
11359 *msg = make_buffer_string (BEG, Z, true);
11360 else
11361 *msg = Qnil;
11362 return false;
11366 /* Push the current message on Vmessage_stack for later restoration
11367 by restore_message. Value is true if the current message isn't
11368 empty. This is a relatively infrequent operation, so it's not
11369 worth optimizing. */
11371 bool
11372 push_message (void)
11374 Lisp_Object msg = current_message ();
11375 Vmessage_stack = Fcons (msg, Vmessage_stack);
11376 return STRINGP (msg);
11380 /* Restore message display from the top of Vmessage_stack. */
11382 void
11383 restore_message (void)
11385 eassert (CONSP (Vmessage_stack));
11386 message3_nolog (XCAR (Vmessage_stack));
11390 /* Handler for unwind-protect calling pop_message. */
11392 void
11393 pop_message_unwind (void)
11395 /* Pop the top-most entry off Vmessage_stack. */
11396 eassert (CONSP (Vmessage_stack));
11397 Vmessage_stack = XCDR (Vmessage_stack);
11401 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11402 exits. If the stack is not empty, we have a missing pop_message
11403 somewhere. */
11405 void
11406 check_message_stack (void)
11408 if (!NILP (Vmessage_stack))
11409 emacs_abort ();
11413 /* Truncate to NCHARS what will be displayed in the echo area the next
11414 time we display it---but don't redisplay it now. */
11416 void
11417 truncate_echo_area (ptrdiff_t nchars)
11419 if (nchars == 0)
11420 echo_area_buffer[0] = Qnil;
11421 else if (!noninteractive
11422 && INTERACTIVE
11423 && !NILP (echo_area_buffer[0]))
11425 struct frame *sf = SELECTED_FRAME ();
11426 /* Error messages get reported properly by cmd_error, so this must be
11427 just an informative message; if the frame hasn't really been
11428 initialized yet, just toss it. */
11429 if (sf->glyphs_initialized_p)
11430 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11435 /* Helper function for truncate_echo_area. Truncate the current
11436 message to at most NCHARS characters. */
11438 static bool
11439 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11441 if (BEG + nchars < Z)
11442 del_range (BEG + nchars, Z);
11443 if (Z == BEG)
11444 echo_area_buffer[0] = Qnil;
11445 return false;
11448 /* Set the current message to STRING. */
11450 static void
11451 set_message (Lisp_Object string)
11453 eassert (STRINGP (string));
11455 message_enable_multibyte = STRING_MULTIBYTE (string);
11457 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11458 message_buf_print = false;
11459 help_echo_showing_p = false;
11461 if (STRINGP (Vdebug_on_message)
11462 && STRINGP (string)
11463 && fast_string_match (Vdebug_on_message, string) >= 0)
11464 call_debugger (list2 (Qerror, string));
11468 /* Helper function for set_message. First argument is ignored and second
11469 argument has the same meaning as for set_message.
11470 This function is called with the echo area buffer being current. */
11472 static bool
11473 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11475 eassert (STRINGP (string));
11477 /* Change multibyteness of the echo buffer appropriately. We always
11478 set it to be multibyte, except when
11479 unibyte-display-via-language-environment is non-nil and the
11480 string to display is unibyte, because in that case unibyte
11481 characters should not be displayed as octal escapes. */
11482 if (!message_enable_multibyte
11483 && unibyte_display_via_language_environment
11484 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11485 Fset_buffer_multibyte (Qnil);
11486 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
11487 Fset_buffer_multibyte (Qt);
11489 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11490 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11491 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11493 /* Insert new message at BEG. */
11494 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11496 /* This function takes care of single/multibyte conversion.
11497 We just have to ensure that the echo area buffer has the right
11498 setting of enable_multibyte_characters. */
11499 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11501 return false;
11505 /* Clear messages. CURRENT_P means clear the current message.
11506 LAST_DISPLAYED_P means clear the message last displayed. */
11508 void
11509 clear_message (bool current_p, bool last_displayed_p)
11511 if (current_p)
11513 echo_area_buffer[0] = Qnil;
11514 message_cleared_p = true;
11517 if (last_displayed_p)
11518 echo_area_buffer[1] = Qnil;
11520 message_buf_print = false;
11523 /* Clear garbaged frames.
11525 This function is used where the old redisplay called
11526 redraw_garbaged_frames which in turn called redraw_frame which in
11527 turn called clear_frame. The call to clear_frame was a source of
11528 flickering. I believe a clear_frame is not necessary. It should
11529 suffice in the new redisplay to invalidate all current matrices,
11530 and ensure a complete redisplay of all windows. */
11532 static void
11533 clear_garbaged_frames (void)
11535 if (frame_garbaged)
11537 Lisp_Object tail, frame;
11538 struct frame *sf = SELECTED_FRAME ();
11540 FOR_EACH_FRAME (tail, frame)
11542 struct frame *f = XFRAME (frame);
11544 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11546 if (f->resized_p
11547 /* It makes no sense to redraw a non-selected TTY
11548 frame, since that will actually clear the
11549 selected frame, and might leave the selected
11550 frame with corrupted display, if it happens not
11551 to be marked garbaged. */
11552 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11553 redraw_frame (f);
11554 else
11555 clear_current_matrices (f);
11557 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11558 x_clear_under_internal_border (f);
11559 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11561 fset_redisplay (f);
11562 f->garbaged = false;
11563 f->resized_p = false;
11567 frame_garbaged = false;
11572 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11573 selected_frame. */
11575 static void
11576 echo_area_display (bool update_frame_p)
11578 Lisp_Object mini_window;
11579 struct window *w;
11580 struct frame *f;
11581 bool window_height_changed_p = false;
11582 struct frame *sf = SELECTED_FRAME ();
11584 mini_window = FRAME_MINIBUF_WINDOW (sf);
11585 if (NILP (mini_window))
11586 return;
11588 w = XWINDOW (mini_window);
11589 f = XFRAME (WINDOW_FRAME (w));
11591 /* Don't display if frame is invisible or not yet initialized. */
11592 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11593 return;
11595 #ifdef HAVE_WINDOW_SYSTEM
11596 /* When Emacs starts, selected_frame may be the initial terminal
11597 frame. If we let this through, a message would be displayed on
11598 the terminal. */
11599 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11600 return;
11601 #endif /* HAVE_WINDOW_SYSTEM */
11603 /* Redraw garbaged frames. */
11604 clear_garbaged_frames ();
11606 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11608 echo_area_window = mini_window;
11609 window_height_changed_p = display_echo_area (w);
11610 w->must_be_updated_p = true;
11612 /* Update the display, unless called from redisplay_internal.
11613 Also don't update the screen during redisplay itself. The
11614 update will happen at the end of redisplay, and an update
11615 here could cause confusion. */
11616 if (update_frame_p && !redisplaying_p)
11618 int n = 0;
11620 /* If the display update has been interrupted by pending
11621 input, update mode lines in the frame. Due to the
11622 pending input, it might have been that redisplay hasn't
11623 been called, so that mode lines above the echo area are
11624 garbaged. This looks odd, so we prevent it here. */
11625 if (!display_completed)
11627 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11629 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11630 x_clear_under_internal_border (f);
11631 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11635 if (window_height_changed_p
11636 /* Don't do this if Emacs is shutting down. Redisplay
11637 needs to run hooks. */
11638 && !NILP (Vrun_hooks))
11640 /* Must update other windows. Likewise as in other
11641 cases, don't let this update be interrupted by
11642 pending input. */
11643 ptrdiff_t count = SPECPDL_INDEX ();
11644 specbind (Qredisplay_dont_pause, Qt);
11645 fset_redisplay (f);
11646 redisplay_internal ();
11647 unbind_to (count, Qnil);
11649 else if (FRAME_WINDOW_P (f) && n == 0)
11651 /* Window configuration is the same as before.
11652 Can do with a display update of the echo area,
11653 unless we displayed some mode lines. */
11654 update_single_window (w);
11655 flush_frame (f);
11657 else
11658 update_frame (f, true, true);
11660 /* If cursor is in the echo area, make sure that the next
11661 redisplay displays the minibuffer, so that the cursor will
11662 be replaced with what the minibuffer wants. */
11663 if (cursor_in_echo_area)
11664 wset_redisplay (XWINDOW (mini_window));
11667 else if (!EQ (mini_window, selected_window))
11668 wset_redisplay (XWINDOW (mini_window));
11670 /* Last displayed message is now the current message. */
11671 echo_area_buffer[1] = echo_area_buffer[0];
11672 /* Inform read_char that we're not echoing. */
11673 echo_message_buffer = Qnil;
11675 /* Prevent redisplay optimization in redisplay_internal by resetting
11676 this_line_start_pos. This is done because the mini-buffer now
11677 displays the message instead of its buffer text. */
11678 if (EQ (mini_window, selected_window))
11679 CHARPOS (this_line_start_pos) = 0;
11681 if (window_height_changed_p)
11683 fset_redisplay (f);
11685 /* If window configuration was changed, frames may have been
11686 marked garbaged. Clear them or we will experience
11687 surprises wrt scrolling.
11688 FIXME: How/why/when? */
11689 clear_garbaged_frames ();
11693 /* True if W's buffer was changed but not saved. */
11695 static bool
11696 window_buffer_changed (struct window *w)
11698 struct buffer *b = XBUFFER (w->contents);
11700 eassert (BUFFER_LIVE_P (b));
11702 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11705 /* True if W has %c or %C in its mode line and mode line should be updated. */
11707 static bool
11708 mode_line_update_needed (struct window *w)
11710 return (w->column_number_displayed != -1
11711 && !(PT == w->last_point && !window_outdated (w))
11712 && (w->column_number_displayed != current_column ()));
11715 /* True if window start of W is frozen and may not be changed during
11716 redisplay. */
11718 static bool
11719 window_frozen_p (struct window *w)
11721 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11723 Lisp_Object window;
11725 XSETWINDOW (window, w);
11726 if (MINI_WINDOW_P (w))
11727 return false;
11728 else if (EQ (window, selected_window))
11729 return false;
11730 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11731 && EQ (window, Vminibuf_scroll_window))
11732 /* This special window can't be frozen too. */
11733 return false;
11734 else
11735 return true;
11737 return false;
11740 /***********************************************************************
11741 Mode Lines and Frame Titles
11742 ***********************************************************************/
11744 /* A buffer for constructing non-propertized mode-line strings and
11745 frame titles in it; allocated from the heap in init_xdisp and
11746 resized as needed in store_mode_line_noprop_char. */
11748 static char *mode_line_noprop_buf;
11750 /* The buffer's end, and a current output position in it. */
11752 static char *mode_line_noprop_buf_end;
11753 static char *mode_line_noprop_ptr;
11755 #define MODE_LINE_NOPROP_LEN(start) \
11756 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11758 static enum {
11759 MODE_LINE_DISPLAY = 0,
11760 MODE_LINE_TITLE,
11761 MODE_LINE_NOPROP,
11762 MODE_LINE_STRING
11763 } mode_line_target;
11765 /* Alist that caches the results of :propertize.
11766 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11767 static Lisp_Object mode_line_proptrans_alist;
11769 /* List of strings making up the mode-line. */
11770 static Lisp_Object mode_line_string_list;
11772 /* Base face property when building propertized mode line string. */
11773 static Lisp_Object mode_line_string_face;
11774 static Lisp_Object mode_line_string_face_prop;
11777 /* Unwind data for mode line strings */
11779 static Lisp_Object Vmode_line_unwind_vector;
11781 static Lisp_Object
11782 format_mode_line_unwind_data (struct frame *target_frame,
11783 struct buffer *obuf,
11784 Lisp_Object owin,
11785 bool save_proptrans)
11787 Lisp_Object vector, tmp;
11789 /* Reduce consing by keeping one vector in
11790 Vwith_echo_area_save_vector. */
11791 vector = Vmode_line_unwind_vector;
11792 Vmode_line_unwind_vector = Qnil;
11794 if (NILP (vector))
11795 vector = Fmake_vector (make_number (10), Qnil);
11797 ASET (vector, 0, make_number (mode_line_target));
11798 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11799 ASET (vector, 2, mode_line_string_list);
11800 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11801 ASET (vector, 4, mode_line_string_face);
11802 ASET (vector, 5, mode_line_string_face_prop);
11804 if (obuf)
11805 XSETBUFFER (tmp, obuf);
11806 else
11807 tmp = Qnil;
11808 ASET (vector, 6, tmp);
11809 ASET (vector, 7, owin);
11810 if (target_frame)
11812 /* Similarly to `with-selected-window', if the operation selects
11813 a window on another frame, we must restore that frame's
11814 selected window, and (for a tty) the top-frame. */
11815 ASET (vector, 8, target_frame->selected_window);
11816 if (FRAME_TERMCAP_P (target_frame))
11817 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11820 return vector;
11823 static void
11824 unwind_format_mode_line (Lisp_Object vector)
11826 Lisp_Object old_window = AREF (vector, 7);
11827 Lisp_Object target_frame_window = AREF (vector, 8);
11828 Lisp_Object old_top_frame = AREF (vector, 9);
11830 mode_line_target = XINT (AREF (vector, 0));
11831 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11832 mode_line_string_list = AREF (vector, 2);
11833 if (! EQ (AREF (vector, 3), Qt))
11834 mode_line_proptrans_alist = AREF (vector, 3);
11835 mode_line_string_face = AREF (vector, 4);
11836 mode_line_string_face_prop = AREF (vector, 5);
11838 /* Select window before buffer, since it may change the buffer. */
11839 if (!NILP (old_window))
11841 /* If the operation that we are unwinding had selected a window
11842 on a different frame, reset its frame-selected-window. For a
11843 text terminal, reset its top-frame if necessary. */
11844 if (!NILP (target_frame_window))
11846 Lisp_Object frame
11847 = WINDOW_FRAME (XWINDOW (target_frame_window));
11849 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11850 Fselect_window (target_frame_window, Qt);
11852 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11853 Fselect_frame (old_top_frame, Qt);
11856 Fselect_window (old_window, Qt);
11859 if (!NILP (AREF (vector, 6)))
11861 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11862 ASET (vector, 6, Qnil);
11865 Vmode_line_unwind_vector = vector;
11869 /* Store a single character C for the frame title in mode_line_noprop_buf.
11870 Re-allocate mode_line_noprop_buf if necessary. */
11872 static void
11873 store_mode_line_noprop_char (char c)
11875 /* If output position has reached the end of the allocated buffer,
11876 increase the buffer's size. */
11877 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11879 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11880 ptrdiff_t size = len;
11881 mode_line_noprop_buf =
11882 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11883 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11884 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11887 *mode_line_noprop_ptr++ = c;
11891 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11892 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11893 characters that yield more columns than PRECISION; PRECISION <= 0
11894 means copy the whole string. Pad with spaces until FIELD_WIDTH
11895 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11896 pad. Called from display_mode_element when it is used to build a
11897 frame title. */
11899 static int
11900 store_mode_line_noprop (const char *string, int field_width, int precision)
11902 const unsigned char *str = (const unsigned char *) string;
11903 int n = 0;
11904 ptrdiff_t dummy, nbytes;
11906 /* Copy at most PRECISION chars from STR. */
11907 nbytes = strlen (string);
11908 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11909 while (nbytes--)
11910 store_mode_line_noprop_char (*str++);
11912 /* Fill up with spaces until FIELD_WIDTH reached. */
11913 while (field_width > 0
11914 && n < field_width)
11916 store_mode_line_noprop_char (' ');
11917 ++n;
11920 return n;
11923 /***********************************************************************
11924 Frame Titles
11925 ***********************************************************************/
11927 #ifdef HAVE_WINDOW_SYSTEM
11929 /* Set the title of FRAME, if it has changed. The title format is
11930 Vicon_title_format if FRAME is iconified, otherwise it is
11931 frame_title_format. */
11933 static void
11934 x_consider_frame_title (Lisp_Object frame)
11936 struct frame *f = XFRAME (frame);
11938 if ((FRAME_WINDOW_P (f)
11939 || FRAME_MINIBUF_ONLY_P (f)
11940 || f->explicit_name)
11941 && !FRAME_TOOLTIP_P (f))
11943 /* Do we have more than one visible frame on this X display? */
11944 Lisp_Object tail, other_frame, fmt;
11945 ptrdiff_t title_start;
11946 char *title;
11947 ptrdiff_t len;
11948 struct it it;
11949 ptrdiff_t count = SPECPDL_INDEX ();
11951 FOR_EACH_FRAME (tail, other_frame)
11953 struct frame *tf = XFRAME (other_frame);
11955 if (tf != f
11956 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11957 && !FRAME_MINIBUF_ONLY_P (tf)
11958 && !FRAME_PARENT_FRAME (tf)
11959 && !FRAME_TOOLTIP_P (tf)
11960 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11961 break;
11964 /* Set global variable indicating that multiple frames exist. */
11965 multiple_frames = CONSP (tail);
11967 /* Switch to the buffer of selected window of the frame. Set up
11968 mode_line_target so that display_mode_element will output into
11969 mode_line_noprop_buf; then display the title. */
11970 record_unwind_protect (unwind_format_mode_line,
11971 format_mode_line_unwind_data
11972 (f, current_buffer, selected_window, false));
11973 /* select-frame calls resize_mini_window, which could resize the
11974 mini-window and by that undo the effect of this redisplay
11975 cycle wrt minibuffer and echo-area display. Binding
11976 inhibit-redisplay to t makes the call to resize_mini_window a
11977 no-op, thus avoiding the adverse side effects. */
11978 specbind (Qinhibit_redisplay, Qt);
11980 Fselect_window (f->selected_window, Qt);
11981 set_buffer_internal_1
11982 (XBUFFER (XWINDOW (f->selected_window)->contents));
11983 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11985 mode_line_target = MODE_LINE_TITLE;
11986 title_start = MODE_LINE_NOPROP_LEN (0);
11987 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11988 NULL, DEFAULT_FACE_ID);
11989 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11990 len = MODE_LINE_NOPROP_LEN (title_start);
11991 title = mode_line_noprop_buf + title_start;
11992 unbind_to (count, Qnil);
11994 /* Set the title only if it's changed. This avoids consing in
11995 the common case where it hasn't. (If it turns out that we've
11996 already wasted too much time by walking through the list with
11997 display_mode_element, then we might need to optimize at a
11998 higher level than this.) */
11999 if (! STRINGP (f->name)
12000 || SBYTES (f->name) != len
12001 || memcmp (title, SDATA (f->name), len) != 0)
12002 x_implicitly_set_name (f, make_string (title, len), Qnil);
12006 #endif /* not HAVE_WINDOW_SYSTEM */
12009 /***********************************************************************
12010 Menu Bars
12011 ***********************************************************************/
12013 /* True if we will not redisplay all visible windows. */
12014 #define REDISPLAY_SOME_P() \
12015 ((windows_or_buffers_changed == 0 \
12016 || windows_or_buffers_changed == REDISPLAY_SOME) \
12017 && (update_mode_lines == 0 \
12018 || update_mode_lines == REDISPLAY_SOME))
12020 /* Prepare for redisplay by updating menu-bar item lists when
12021 appropriate. This can call eval. */
12023 static void
12024 prepare_menu_bars (void)
12026 bool all_windows = windows_or_buffers_changed || update_mode_lines;
12027 bool some_windows = REDISPLAY_SOME_P ();
12029 if (FUNCTIONP (Vpre_redisplay_function))
12031 Lisp_Object windows = all_windows ? Qt : Qnil;
12032 if (all_windows && some_windows)
12034 Lisp_Object ws = window_list ();
12035 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
12037 Lisp_Object this = XCAR (ws);
12038 struct window *w = XWINDOW (this);
12039 if (w->redisplay
12040 || XFRAME (w->frame)->redisplay
12041 || XBUFFER (w->contents)->text->redisplay)
12043 windows = Fcons (this, windows);
12047 safe__call1 (true, Vpre_redisplay_function, windows);
12050 /* Update all frame titles based on their buffer names, etc. We do
12051 this before the menu bars so that the buffer-menu will show the
12052 up-to-date frame titles. */
12053 #ifdef HAVE_WINDOW_SYSTEM
12054 if (all_windows)
12056 Lisp_Object tail, frame;
12058 FOR_EACH_FRAME (tail, frame)
12060 struct frame *f = XFRAME (frame);
12061 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12062 if (some_windows
12063 && !f->redisplay
12064 && !w->redisplay
12065 && !XBUFFER (w->contents)->text->redisplay)
12066 continue;
12068 if (!FRAME_TOOLTIP_P (f)
12069 && !FRAME_PARENT_FRAME (f)
12070 && (FRAME_ICONIFIED_P (f)
12071 || FRAME_VISIBLE_P (f) == 1
12072 /* Exclude TTY frames that are obscured because they
12073 are not the top frame on their console. This is
12074 because x_consider_frame_title actually switches
12075 to the frame, which for TTY frames means it is
12076 marked as garbaged, and will be completely
12077 redrawn on the next redisplay cycle. This causes
12078 TTY frames to be completely redrawn, when there
12079 are more than one of them, even though nothing
12080 should be changed on display. */
12081 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12082 x_consider_frame_title (frame);
12085 #endif /* HAVE_WINDOW_SYSTEM */
12087 /* Update the menu bar item lists, if appropriate. This has to be
12088 done before any actual redisplay or generation of display lines. */
12090 if (all_windows)
12092 Lisp_Object tail, frame;
12093 ptrdiff_t count = SPECPDL_INDEX ();
12094 /* True means that update_menu_bar has run its hooks
12095 so any further calls to update_menu_bar shouldn't do so again. */
12096 bool menu_bar_hooks_run = false;
12098 record_unwind_save_match_data ();
12100 FOR_EACH_FRAME (tail, frame)
12102 struct frame *f = XFRAME (frame);
12103 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12105 /* Ignore tooltip frame. */
12106 if (FRAME_TOOLTIP_P (f))
12107 continue;
12109 if (some_windows
12110 && !f->redisplay
12111 && !w->redisplay
12112 && !XBUFFER (w->contents)->text->redisplay)
12113 continue;
12115 run_window_size_change_functions (frame);
12117 if (FRAME_PARENT_FRAME (f))
12118 continue;
12120 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12121 #ifdef HAVE_WINDOW_SYSTEM
12122 update_tool_bar (f, false);
12123 #endif
12126 unbind_to (count, Qnil);
12128 else
12130 struct frame *sf = SELECTED_FRAME ();
12131 update_menu_bar (sf, true, false);
12132 #ifdef HAVE_WINDOW_SYSTEM
12133 update_tool_bar (sf, true);
12134 #endif
12139 /* Update the menu bar item list for frame F. This has to be done
12140 before we start to fill in any display lines, because it can call
12141 eval.
12143 If SAVE_MATCH_DATA, we must save and restore it here.
12145 If HOOKS_RUN, a previous call to update_menu_bar
12146 already ran the menu bar hooks for this redisplay, so there
12147 is no need to run them again. The return value is the
12148 updated value of this flag, to pass to the next call. */
12150 static bool
12151 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12153 Lisp_Object window;
12154 struct window *w;
12156 /* If called recursively during a menu update, do nothing. This can
12157 happen when, for instance, an activate-menubar-hook causes a
12158 redisplay. */
12159 if (inhibit_menubar_update)
12160 return hooks_run;
12162 window = FRAME_SELECTED_WINDOW (f);
12163 w = XWINDOW (window);
12165 if (FRAME_WINDOW_P (f)
12167 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12168 || defined (HAVE_NS) || defined (USE_GTK)
12169 FRAME_EXTERNAL_MENU_BAR (f)
12170 #else
12171 FRAME_MENU_BAR_LINES (f) > 0
12172 #endif
12173 : FRAME_MENU_BAR_LINES (f) > 0)
12175 /* If the user has switched buffers or windows, we need to
12176 recompute to reflect the new bindings. But we'll
12177 recompute when update_mode_lines is set too; that means
12178 that people can use force-mode-line-update to request
12179 that the menu bar be recomputed. The adverse effect on
12180 the rest of the redisplay algorithm is about the same as
12181 windows_or_buffers_changed anyway. */
12182 if (windows_or_buffers_changed
12183 /* This used to test w->update_mode_line, but we believe
12184 there is no need to recompute the menu in that case. */
12185 || update_mode_lines
12186 || window_buffer_changed (w))
12188 struct buffer *prev = current_buffer;
12189 ptrdiff_t count = SPECPDL_INDEX ();
12191 specbind (Qinhibit_menubar_update, Qt);
12193 set_buffer_internal_1 (XBUFFER (w->contents));
12194 if (save_match_data)
12195 record_unwind_save_match_data ();
12196 if (NILP (Voverriding_local_map_menu_flag))
12198 specbind (Qoverriding_terminal_local_map, Qnil);
12199 specbind (Qoverriding_local_map, Qnil);
12202 if (!hooks_run)
12204 /* Run the Lucid hook. */
12205 safe_run_hooks (Qactivate_menubar_hook);
12207 /* If it has changed current-menubar from previous value,
12208 really recompute the menu-bar from the value. */
12209 if (! NILP (Vlucid_menu_bar_dirty_flag))
12210 call0 (Qrecompute_lucid_menubar);
12212 safe_run_hooks (Qmenu_bar_update_hook);
12214 hooks_run = true;
12217 XSETFRAME (Vmenu_updating_frame, f);
12218 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12220 /* Redisplay the menu bar in case we changed it. */
12221 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12222 || defined (HAVE_NS) || defined (USE_GTK)
12223 if (FRAME_WINDOW_P (f))
12225 #if defined (HAVE_NS)
12226 /* All frames on Mac OS share the same menubar. So only
12227 the selected frame should be allowed to set it. */
12228 if (f == SELECTED_FRAME ())
12229 #endif
12230 set_frame_menubar (f, false, false);
12232 else
12233 /* On a terminal screen, the menu bar is an ordinary screen
12234 line, and this makes it get updated. */
12235 w->update_mode_line = true;
12236 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12237 /* In the non-toolkit version, the menu bar is an ordinary screen
12238 line, and this makes it get updated. */
12239 w->update_mode_line = true;
12240 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12242 unbind_to (count, Qnil);
12243 set_buffer_internal_1 (prev);
12247 return hooks_run;
12250 /***********************************************************************
12251 Tool-bars
12252 ***********************************************************************/
12254 #ifdef HAVE_WINDOW_SYSTEM
12256 /* Select `frame' temporarily without running all the code in
12257 do_switch_frame.
12258 FIXME: Maybe do_switch_frame should be trimmed down similarly
12259 when `norecord' is set. */
12260 static void
12261 fast_set_selected_frame (Lisp_Object frame)
12263 if (!EQ (selected_frame, frame))
12265 selected_frame = frame;
12266 selected_window = XFRAME (frame)->selected_window;
12270 /* Update the tool-bar item list for frame F. This has to be done
12271 before we start to fill in any display lines. Called from
12272 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12273 and restore it here. */
12275 static void
12276 update_tool_bar (struct frame *f, bool save_match_data)
12278 #if defined (USE_GTK) || defined (HAVE_NS)
12279 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12280 #else
12281 bool do_update = (WINDOWP (f->tool_bar_window)
12282 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12283 #endif
12285 if (do_update)
12287 Lisp_Object window;
12288 struct window *w;
12290 window = FRAME_SELECTED_WINDOW (f);
12291 w = XWINDOW (window);
12293 /* If the user has switched buffers or windows, we need to
12294 recompute to reflect the new bindings. But we'll
12295 recompute when update_mode_lines is set too; that means
12296 that people can use force-mode-line-update to request
12297 that the menu bar be recomputed. The adverse effect on
12298 the rest of the redisplay algorithm is about the same as
12299 windows_or_buffers_changed anyway. */
12300 if (windows_or_buffers_changed
12301 || w->update_mode_line
12302 || update_mode_lines
12303 || window_buffer_changed (w))
12305 struct buffer *prev = current_buffer;
12306 ptrdiff_t count = SPECPDL_INDEX ();
12307 Lisp_Object frame, new_tool_bar;
12308 int new_n_tool_bar;
12310 /* Set current_buffer to the buffer of the selected
12311 window of the frame, so that we get the right local
12312 keymaps. */
12313 set_buffer_internal_1 (XBUFFER (w->contents));
12315 /* Save match data, if we must. */
12316 if (save_match_data)
12317 record_unwind_save_match_data ();
12319 /* Make sure that we don't accidentally use bogus keymaps. */
12320 if (NILP (Voverriding_local_map_menu_flag))
12322 specbind (Qoverriding_terminal_local_map, Qnil);
12323 specbind (Qoverriding_local_map, Qnil);
12326 /* We must temporarily set the selected frame to this frame
12327 before calling tool_bar_items, because the calculation of
12328 the tool-bar keymap uses the selected frame (see
12329 `tool-bar-make-keymap' in tool-bar.el). */
12330 eassert (EQ (selected_window,
12331 /* Since we only explicitly preserve selected_frame,
12332 check that selected_window would be redundant. */
12333 XFRAME (selected_frame)->selected_window));
12334 record_unwind_protect (fast_set_selected_frame, selected_frame);
12335 XSETFRAME (frame, f);
12336 fast_set_selected_frame (frame);
12338 /* Build desired tool-bar items from keymaps. */
12339 new_tool_bar
12340 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12341 &new_n_tool_bar);
12343 /* Redisplay the tool-bar if we changed it. */
12344 if (new_n_tool_bar != f->n_tool_bar_items
12345 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12347 /* Redisplay that happens asynchronously due to an expose event
12348 may access f->tool_bar_items. Make sure we update both
12349 variables within BLOCK_INPUT so no such event interrupts. */
12350 block_input ();
12351 fset_tool_bar_items (f, new_tool_bar);
12352 f->n_tool_bar_items = new_n_tool_bar;
12353 w->update_mode_line = true;
12354 unblock_input ();
12357 unbind_to (count, Qnil);
12358 set_buffer_internal_1 (prev);
12363 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12365 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12366 F's desired tool-bar contents. F->tool_bar_items must have
12367 been set up previously by calling prepare_menu_bars. */
12369 static void
12370 build_desired_tool_bar_string (struct frame *f)
12372 int i, size, size_needed;
12373 Lisp_Object image, plist;
12375 image = plist = Qnil;
12377 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12378 Otherwise, make a new string. */
12380 /* The size of the string we might be able to reuse. */
12381 size = (STRINGP (f->desired_tool_bar_string)
12382 ? SCHARS (f->desired_tool_bar_string)
12383 : 0);
12385 /* We need one space in the string for each image. */
12386 size_needed = f->n_tool_bar_items;
12388 /* Reuse f->desired_tool_bar_string, if possible. */
12389 if (size < size_needed || NILP (f->desired_tool_bar_string))
12390 fset_desired_tool_bar_string
12391 (f, Fmake_string (make_number (size_needed), make_number (' '), Qnil));
12392 else
12394 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12395 Fremove_text_properties (make_number (0), make_number (size),
12396 props, f->desired_tool_bar_string);
12399 /* Put a `display' property on the string for the images to display,
12400 put a `menu_item' property on tool-bar items with a value that
12401 is the index of the item in F's tool-bar item vector. */
12402 for (i = 0; i < f->n_tool_bar_items; ++i)
12404 #define PROP(IDX) \
12405 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12407 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12408 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12409 int hmargin, vmargin, relief, idx, end;
12411 /* If image is a vector, choose the image according to the
12412 button state. */
12413 image = PROP (TOOL_BAR_ITEM_IMAGES);
12414 if (VECTORP (image))
12416 if (enabled_p)
12417 idx = (selected_p
12418 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12419 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12420 else
12421 idx = (selected_p
12422 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12423 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12425 eassert (ASIZE (image) >= idx);
12426 image = AREF (image, idx);
12428 else
12429 idx = -1;
12431 /* Ignore invalid image specifications. */
12432 if (!valid_image_p (image))
12433 continue;
12435 /* Display the tool-bar button pressed, or depressed. */
12436 plist = Fcopy_sequence (XCDR (image));
12438 /* Compute margin and relief to draw. */
12439 relief = (tool_bar_button_relief >= 0
12440 ? tool_bar_button_relief
12441 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12442 hmargin = vmargin = relief;
12444 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12445 INT_MAX - max (hmargin, vmargin)))
12447 hmargin += XFASTINT (Vtool_bar_button_margin);
12448 vmargin += XFASTINT (Vtool_bar_button_margin);
12450 else if (CONSP (Vtool_bar_button_margin))
12452 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12453 INT_MAX - hmargin))
12454 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12456 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12457 INT_MAX - vmargin))
12458 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12461 if (auto_raise_tool_bar_buttons_p)
12463 /* Add a `:relief' property to the image spec if the item is
12464 selected. */
12465 if (selected_p)
12467 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12468 hmargin -= relief;
12469 vmargin -= relief;
12472 else
12474 /* If image is selected, display it pressed, i.e. with a
12475 negative relief. If it's not selected, display it with a
12476 raised relief. */
12477 plist = Fplist_put (plist, QCrelief,
12478 (selected_p
12479 ? make_number (-relief)
12480 : make_number (relief)));
12481 hmargin -= relief;
12482 vmargin -= relief;
12485 /* Put a margin around the image. */
12486 if (hmargin || vmargin)
12488 if (hmargin == vmargin)
12489 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12490 else
12491 plist = Fplist_put (plist, QCmargin,
12492 Fcons (make_number (hmargin),
12493 make_number (vmargin)));
12496 /* If button is not enabled, and we don't have special images
12497 for the disabled state, make the image appear disabled by
12498 applying an appropriate algorithm to it. */
12499 if (!enabled_p && idx < 0)
12500 plist = Fplist_put (plist, QCconversion, Qdisabled);
12502 /* Put a `display' text property on the string for the image to
12503 display. Put a `menu-item' property on the string that gives
12504 the start of this item's properties in the tool-bar items
12505 vector. */
12506 image = Fcons (Qimage, plist);
12507 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12508 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12510 /* Let the last image hide all remaining spaces in the tool bar
12511 string. The string can be longer than needed when we reuse a
12512 previous string. */
12513 if (i + 1 == f->n_tool_bar_items)
12514 end = SCHARS (f->desired_tool_bar_string);
12515 else
12516 end = i + 1;
12517 Fadd_text_properties (make_number (i), make_number (end),
12518 props, f->desired_tool_bar_string);
12519 #undef PROP
12524 /* Display one line of the tool-bar of frame IT->f.
12526 HEIGHT specifies the desired height of the tool-bar line.
12527 If the actual height of the glyph row is less than HEIGHT, the
12528 row's height is increased to HEIGHT, and the icons are centered
12529 vertically in the new height.
12531 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12532 count a final empty row in case the tool-bar width exactly matches
12533 the window width.
12536 static void
12537 display_tool_bar_line (struct it *it, int height)
12539 struct glyph_row *row = it->glyph_row;
12540 int max_x = it->last_visible_x;
12541 struct glyph *last;
12543 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12544 clear_glyph_row (row);
12545 row->enabled_p = true;
12546 row->y = it->current_y;
12548 /* Note that this isn't made use of if the face hasn't a box,
12549 so there's no need to check the face here. */
12550 it->start_of_box_run_p = true;
12552 while (it->current_x < max_x)
12554 int x, n_glyphs_before, i, nglyphs;
12555 struct it it_before;
12557 /* Get the next display element. */
12558 if (!get_next_display_element (it))
12560 /* Don't count empty row if we are counting needed tool-bar lines. */
12561 if (height < 0 && !it->hpos)
12562 return;
12563 break;
12566 /* Produce glyphs. */
12567 n_glyphs_before = row->used[TEXT_AREA];
12568 it_before = *it;
12570 PRODUCE_GLYPHS (it);
12572 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12573 i = 0;
12574 x = it_before.current_x;
12575 while (i < nglyphs)
12577 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12579 if (x + glyph->pixel_width > max_x)
12581 /* Glyph doesn't fit on line. Backtrack. */
12582 row->used[TEXT_AREA] = n_glyphs_before;
12583 *it = it_before;
12584 /* If this is the only glyph on this line, it will never fit on the
12585 tool-bar, so skip it. But ensure there is at least one glyph,
12586 so we don't accidentally disable the tool-bar. */
12587 if (n_glyphs_before == 0
12588 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12589 break;
12590 goto out;
12593 ++it->hpos;
12594 x += glyph->pixel_width;
12595 ++i;
12598 /* Stop at line end. */
12599 if (ITERATOR_AT_END_OF_LINE_P (it))
12600 break;
12602 set_iterator_to_next (it, true);
12605 out:;
12607 row->displays_text_p = row->used[TEXT_AREA] != 0;
12609 /* Use default face for the border below the tool bar.
12611 FIXME: When auto-resize-tool-bars is grow-only, there is
12612 no additional border below the possibly empty tool-bar lines.
12613 So to make the extra empty lines look "normal", we have to
12614 use the tool-bar face for the border too. */
12615 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12616 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12617 it->face_id = DEFAULT_FACE_ID;
12619 extend_face_to_end_of_line (it);
12620 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12621 last->right_box_line_p = true;
12622 if (last == row->glyphs[TEXT_AREA])
12623 last->left_box_line_p = true;
12625 /* Make line the desired height and center it vertically. */
12626 if ((height -= it->max_ascent + it->max_descent) > 0)
12628 /* Don't add more than one line height. */
12629 height %= FRAME_LINE_HEIGHT (it->f);
12630 it->max_ascent += height / 2;
12631 it->max_descent += (height + 1) / 2;
12634 compute_line_metrics (it);
12636 /* If line is empty, make it occupy the rest of the tool-bar. */
12637 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12639 row->height = row->phys_height = it->last_visible_y - row->y;
12640 row->visible_height = row->height;
12641 row->ascent = row->phys_ascent = 0;
12642 row->extra_line_spacing = 0;
12645 row->full_width_p = true;
12646 row->continued_p = false;
12647 row->truncated_on_left_p = false;
12648 row->truncated_on_right_p = false;
12650 it->current_x = it->hpos = 0;
12651 it->current_y += row->height;
12652 ++it->vpos;
12653 ++it->glyph_row;
12657 /* Value is the number of pixels needed to make all tool-bar items of
12658 frame F visible. The actual number of glyph rows needed is
12659 returned in *N_ROWS if non-NULL. */
12660 static int
12661 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12663 struct window *w = XWINDOW (f->tool_bar_window);
12664 struct it it;
12665 /* tool_bar_height is called from redisplay_tool_bar after building
12666 the desired matrix, so use (unused) mode-line row as temporary row to
12667 avoid destroying the first tool-bar row. */
12668 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12670 /* Initialize an iterator for iteration over
12671 F->desired_tool_bar_string in the tool-bar window of frame F. */
12672 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12673 temp_row->reversed_p = false;
12674 it.first_visible_x = 0;
12675 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12676 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12677 it.paragraph_embedding = L2R;
12679 while (!ITERATOR_AT_END_P (&it))
12681 clear_glyph_row (temp_row);
12682 it.glyph_row = temp_row;
12683 display_tool_bar_line (&it, -1);
12685 clear_glyph_row (temp_row);
12687 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12688 if (n_rows)
12689 *n_rows = it.vpos > 0 ? it.vpos : -1;
12691 if (pixelwise)
12692 return it.current_y;
12693 else
12694 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12697 #endif /* !USE_GTK && !HAVE_NS */
12699 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12700 0, 2, 0,
12701 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12702 If FRAME is nil or omitted, use the selected frame. Optional argument
12703 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12704 (Lisp_Object frame, Lisp_Object pixelwise)
12706 int height = 0;
12708 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12709 struct frame *f = decode_any_frame (frame);
12711 if (WINDOWP (f->tool_bar_window)
12712 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12714 update_tool_bar (f, true);
12715 if (f->n_tool_bar_items)
12717 build_desired_tool_bar_string (f);
12718 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12721 #endif
12723 return make_number (height);
12727 /* Display the tool-bar of frame F. Value is true if tool-bar's
12728 height should be changed. */
12729 static bool
12730 redisplay_tool_bar (struct frame *f)
12732 f->tool_bar_redisplayed = true;
12733 #if defined (USE_GTK) || defined (HAVE_NS)
12735 if (FRAME_EXTERNAL_TOOL_BAR (f))
12736 update_frame_tool_bar (f);
12737 return false;
12739 #else /* !USE_GTK && !HAVE_NS */
12741 struct window *w;
12742 struct it it;
12743 struct glyph_row *row;
12745 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12746 do anything. This means you must start with tool-bar-lines
12747 non-zero to get the auto-sizing effect. Or in other words, you
12748 can turn off tool-bars by specifying tool-bar-lines zero. */
12749 if (!WINDOWP (f->tool_bar_window)
12750 || (w = XWINDOW (f->tool_bar_window),
12751 WINDOW_TOTAL_LINES (w) == 0))
12752 return false;
12754 /* Set up an iterator for the tool-bar window. */
12755 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12756 it.first_visible_x = 0;
12757 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12758 row = it.glyph_row;
12759 row->reversed_p = false;
12761 /* Build a string that represents the contents of the tool-bar. */
12762 build_desired_tool_bar_string (f);
12763 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12764 /* FIXME: This should be controlled by a user option. But it
12765 doesn't make sense to have an R2L tool bar if the menu bar cannot
12766 be drawn also R2L, and making the menu bar R2L is tricky due
12767 toolkit-specific code that implements it. If an R2L tool bar is
12768 ever supported, display_tool_bar_line should also be augmented to
12769 call unproduce_glyphs like display_line and display_string
12770 do. */
12771 it.paragraph_embedding = L2R;
12773 if (f->n_tool_bar_rows == 0)
12775 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12777 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12779 x_change_tool_bar_height (f, new_height);
12780 frame_default_tool_bar_height = new_height;
12781 /* Always do that now. */
12782 clear_glyph_matrix (w->desired_matrix);
12783 f->fonts_changed = true;
12784 return true;
12788 /* Display as many lines as needed to display all tool-bar items. */
12790 if (f->n_tool_bar_rows > 0)
12792 int border, rows, height, extra;
12794 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12795 border = XINT (Vtool_bar_border);
12796 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12797 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12798 else if (EQ (Vtool_bar_border, Qborder_width))
12799 border = f->border_width;
12800 else
12801 border = 0;
12802 if (border < 0)
12803 border = 0;
12805 rows = f->n_tool_bar_rows;
12806 height = max (1, (it.last_visible_y - border) / rows);
12807 extra = it.last_visible_y - border - height * rows;
12809 while (it.current_y < it.last_visible_y)
12811 int h = 0;
12812 if (extra > 0 && rows-- > 0)
12814 h = (extra + rows - 1) / rows;
12815 extra -= h;
12817 display_tool_bar_line (&it, height + h);
12820 else
12822 while (it.current_y < it.last_visible_y)
12823 display_tool_bar_line (&it, 0);
12826 /* It doesn't make much sense to try scrolling in the tool-bar
12827 window, so don't do it. */
12828 w->desired_matrix->no_scrolling_p = true;
12829 w->must_be_updated_p = true;
12831 if (!NILP (Vauto_resize_tool_bars))
12833 bool change_height_p = true;
12835 /* If we couldn't display everything, change the tool-bar's
12836 height if there is room for more. */
12837 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12838 change_height_p = true;
12840 /* We subtract 1 because display_tool_bar_line advances the
12841 glyph_row pointer before returning to its caller. We want to
12842 examine the last glyph row produced by
12843 display_tool_bar_line. */
12844 row = it.glyph_row - 1;
12846 /* If there are blank lines at the end, except for a partially
12847 visible blank line at the end that is smaller than
12848 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12849 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12850 && row->height >= FRAME_LINE_HEIGHT (f))
12851 change_height_p = true;
12853 /* If row displays tool-bar items, but is partially visible,
12854 change the tool-bar's height. */
12855 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12856 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12857 change_height_p = true;
12859 /* Resize windows as needed by changing the `tool-bar-lines'
12860 frame parameter. */
12861 if (change_height_p)
12863 int nrows;
12864 int new_height = tool_bar_height (f, &nrows, true);
12866 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12867 && !f->minimize_tool_bar_window_p)
12868 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12869 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12870 f->minimize_tool_bar_window_p = false;
12872 if (change_height_p)
12874 x_change_tool_bar_height (f, new_height);
12875 frame_default_tool_bar_height = new_height;
12876 clear_glyph_matrix (w->desired_matrix);
12877 f->n_tool_bar_rows = nrows;
12878 f->fonts_changed = true;
12880 return true;
12885 f->minimize_tool_bar_window_p = false;
12886 return false;
12888 #endif /* USE_GTK || HAVE_NS */
12891 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12893 /* Get information about the tool-bar item which is displayed in GLYPH
12894 on frame F. Return in *PROP_IDX the index where tool-bar item
12895 properties start in F->tool_bar_items. Value is false if
12896 GLYPH doesn't display a tool-bar item. */
12898 static bool
12899 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12901 Lisp_Object prop;
12902 int charpos;
12904 /* This function can be called asynchronously, which means we must
12905 exclude any possibility that Fget_text_property signals an
12906 error. */
12907 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12908 charpos = max (0, charpos);
12910 /* Get the text property `menu-item' at pos. The value of that
12911 property is the start index of this item's properties in
12912 F->tool_bar_items. */
12913 prop = Fget_text_property (make_number (charpos),
12914 Qmenu_item, f->current_tool_bar_string);
12915 if (! INTEGERP (prop))
12916 return false;
12917 *prop_idx = XINT (prop);
12918 return true;
12922 /* Get information about the tool-bar item at position X/Y on frame F.
12923 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12924 the current matrix of the tool-bar window of F, or NULL if not
12925 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12926 item in F->tool_bar_items. Value is
12928 -1 if X/Y is not on a tool-bar item
12929 0 if X/Y is on the same item that was highlighted before.
12930 1 otherwise. */
12932 static int
12933 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12934 int *hpos, int *vpos, int *prop_idx)
12936 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12937 struct window *w = XWINDOW (f->tool_bar_window);
12938 int area;
12940 /* Find the glyph under X/Y. */
12941 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12942 if (*glyph == NULL)
12943 return -1;
12945 /* Get the start of this tool-bar item's properties in
12946 f->tool_bar_items. */
12947 if (!tool_bar_item_info (f, *glyph, prop_idx))
12948 return -1;
12950 /* Is mouse on the highlighted item? */
12951 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12952 && *vpos >= hlinfo->mouse_face_beg_row
12953 && *vpos <= hlinfo->mouse_face_end_row
12954 && (*vpos > hlinfo->mouse_face_beg_row
12955 || *hpos >= hlinfo->mouse_face_beg_col)
12956 && (*vpos < hlinfo->mouse_face_end_row
12957 || *hpos < hlinfo->mouse_face_end_col
12958 || hlinfo->mouse_face_past_end))
12959 return 0;
12961 return 1;
12965 /* EXPORT:
12966 Handle mouse button event on the tool-bar of frame F, at
12967 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12968 false for button release. MODIFIERS is event modifiers for button
12969 release. */
12971 void
12972 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12973 int modifiers)
12975 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12976 struct window *w = XWINDOW (f->tool_bar_window);
12977 int hpos, vpos, prop_idx;
12978 struct glyph *glyph;
12979 Lisp_Object enabled_p;
12980 int ts;
12982 /* If not on the highlighted tool-bar item, and mouse-highlight is
12983 non-nil, return. This is so we generate the tool-bar button
12984 click only when the mouse button is released on the same item as
12985 where it was pressed. However, when mouse-highlight is disabled,
12986 generate the click when the button is released regardless of the
12987 highlight, since tool-bar items are not highlighted in that
12988 case. */
12989 frame_to_window_pixel_xy (w, &x, &y);
12990 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12991 if (ts == -1
12992 || (ts != 0 && !NILP (Vmouse_highlight)))
12993 return;
12995 /* When mouse-highlight is off, generate the click for the item
12996 where the button was pressed, disregarding where it was
12997 released. */
12998 if (NILP (Vmouse_highlight) && !down_p)
12999 prop_idx = f->last_tool_bar_item;
13001 /* If item is disabled, do nothing. */
13002 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13003 if (NILP (enabled_p))
13004 return;
13006 if (down_p)
13008 /* Show item in pressed state. */
13009 if (!NILP (Vmouse_highlight))
13010 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
13011 f->last_tool_bar_item = prop_idx;
13013 else
13015 Lisp_Object key, frame;
13016 struct input_event event;
13017 EVENT_INIT (event);
13019 /* Show item in released state. */
13020 if (!NILP (Vmouse_highlight))
13021 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
13023 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
13025 XSETFRAME (frame, f);
13026 event.kind = TOOL_BAR_EVENT;
13027 event.frame_or_window = frame;
13028 event.arg = frame;
13029 kbd_buffer_store_event (&event);
13031 event.kind = TOOL_BAR_EVENT;
13032 event.frame_or_window = frame;
13033 event.arg = key;
13034 event.modifiers = modifiers;
13035 kbd_buffer_store_event (&event);
13036 f->last_tool_bar_item = -1;
13041 /* Possibly highlight a tool-bar item on frame F when mouse moves to
13042 tool-bar window-relative coordinates X/Y. Called from
13043 note_mouse_highlight. */
13045 static void
13046 note_tool_bar_highlight (struct frame *f, int x, int y)
13048 Lisp_Object window = f->tool_bar_window;
13049 struct window *w = XWINDOW (window);
13050 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
13051 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
13052 int hpos, vpos;
13053 struct glyph *glyph;
13054 struct glyph_row *row;
13055 int i;
13056 Lisp_Object enabled_p;
13057 int prop_idx;
13058 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
13059 bool mouse_down_p;
13060 int rc;
13062 /* Function note_mouse_highlight is called with negative X/Y
13063 values when mouse moves outside of the frame. */
13064 if (x <= 0 || y <= 0)
13066 clear_mouse_face (hlinfo);
13067 return;
13070 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13071 if (rc < 0)
13073 /* Not on tool-bar item. */
13074 clear_mouse_face (hlinfo);
13075 return;
13077 else if (rc == 0)
13078 /* On same tool-bar item as before. */
13079 goto set_help_echo;
13081 clear_mouse_face (hlinfo);
13083 /* Mouse is down, but on different tool-bar item? */
13084 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13085 && f == dpyinfo->last_mouse_frame);
13087 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13088 return;
13090 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13092 /* If tool-bar item is not enabled, don't highlight it. */
13093 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13094 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13096 /* Compute the x-position of the glyph. In front and past the
13097 image is a space. We include this in the highlighted area. */
13098 row = MATRIX_ROW (w->current_matrix, vpos);
13099 for (i = x = 0; i < hpos; ++i)
13100 x += row->glyphs[TEXT_AREA][i].pixel_width;
13102 /* Record this as the current active region. */
13103 hlinfo->mouse_face_beg_col = hpos;
13104 hlinfo->mouse_face_beg_row = vpos;
13105 hlinfo->mouse_face_beg_x = x;
13106 hlinfo->mouse_face_past_end = false;
13108 hlinfo->mouse_face_end_col = hpos + 1;
13109 hlinfo->mouse_face_end_row = vpos;
13110 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13111 hlinfo->mouse_face_window = window;
13112 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13114 /* Display it as active. */
13115 show_mouse_face (hlinfo, draw);
13118 set_help_echo:
13120 /* Set help_echo_string to a help string to display for this tool-bar item.
13121 XTread_socket does the rest. */
13122 help_echo_object = help_echo_window = Qnil;
13123 help_echo_pos = -1;
13124 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13125 if (NILP (help_echo_string))
13126 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13129 #endif /* !USE_GTK && !HAVE_NS */
13131 #endif /* HAVE_WINDOW_SYSTEM */
13135 /************************************************************************
13136 Horizontal scrolling
13137 ************************************************************************/
13139 /* For all leaf windows in the window tree rooted at WINDOW, set their
13140 hscroll value so that PT is (i) visible in the window, and (ii) so
13141 that it is not within a certain margin at the window's left and
13142 right border. Value is true if any window's hscroll has been
13143 changed. */
13145 static bool
13146 hscroll_window_tree (Lisp_Object window)
13148 bool hscrolled_p = false;
13149 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13150 int hscroll_step_abs = 0;
13151 double hscroll_step_rel = 0;
13153 if (hscroll_relative_p)
13155 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13156 if (hscroll_step_rel < 0)
13158 hscroll_relative_p = false;
13159 hscroll_step_abs = 0;
13162 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13164 hscroll_step_abs = XINT (Vhscroll_step);
13165 if (hscroll_step_abs < 0)
13166 hscroll_step_abs = 0;
13168 else
13169 hscroll_step_abs = 0;
13171 while (WINDOWP (window))
13173 struct window *w = XWINDOW (window);
13175 if (WINDOWP (w->contents))
13176 hscrolled_p |= hscroll_window_tree (w->contents);
13177 else if (w->cursor.vpos >= 0)
13179 int h_margin;
13180 int text_area_width;
13181 struct glyph_row *cursor_row;
13182 struct glyph_row *bottom_row;
13184 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13185 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13186 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13187 else
13188 cursor_row = bottom_row - 1;
13190 if (!cursor_row->enabled_p)
13192 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13193 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13194 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13195 else
13196 cursor_row = bottom_row - 1;
13198 bool row_r2l_p = cursor_row->reversed_p;
13199 bool hscl = hscrolling_current_line_p (w);
13200 int x_offset = 0;
13201 /* When line numbers are displayed, we need to account for
13202 the horizontal space they consume. */
13203 if (!NILP (Vdisplay_line_numbers))
13205 struct glyph *g;
13206 if (!row_r2l_p)
13208 for (g = cursor_row->glyphs[TEXT_AREA];
13209 g < cursor_row->glyphs[TEXT_AREA]
13210 + cursor_row->used[TEXT_AREA];
13211 g++)
13213 if (!(NILP (g->object) && g->charpos < 0))
13214 break;
13215 x_offset += g->pixel_width;
13218 else
13220 for (g = cursor_row->glyphs[TEXT_AREA]
13221 + cursor_row->used[TEXT_AREA];
13222 g > cursor_row->glyphs[TEXT_AREA];
13223 g--)
13225 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13226 break;
13227 x_offset += (g - 1)->pixel_width;
13231 if (cursor_row->truncated_on_left_p)
13233 /* On TTY frames, don't count the left truncation glyph. */
13234 struct frame *f = XFRAME (WINDOW_FRAME (w));
13235 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13238 text_area_width = window_box_width (w, TEXT_AREA);
13240 /* Scroll when cursor is inside this scroll margin. */
13241 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13243 /* If the position of this window's point has explicitly
13244 changed, no more suspend auto hscrolling. */
13245 if (w->suspend_auto_hscroll
13246 && NILP (Fequal (Fwindow_point (window),
13247 Fwindow_old_point (window))))
13249 w->suspend_auto_hscroll = false;
13250 /* When hscrolling just the current line, and the rest
13251 of lines were temporarily hscrolled, but no longer
13252 are, force thorough redisplay of this window, to show
13253 the effect of disabling hscroll suspension immediately. */
13254 if (w->min_hscroll == 0 && w->hscroll > 0
13255 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
13256 Qcurrent_line))
13257 SET_FRAME_GARBAGED (XFRAME (w->frame));
13260 /* Remember window point. */
13261 Fset_marker (w->old_pointm,
13262 ((w == XWINDOW (selected_window))
13263 ? make_number (BUF_PT (XBUFFER (w->contents)))
13264 : Fmarker_position (w->pointm)),
13265 w->contents);
13267 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13268 && !w->suspend_auto_hscroll
13269 /* In some pathological cases, like restoring a window
13270 configuration into a frame that is much smaller than
13271 the one from which the configuration was saved, we
13272 get glyph rows whose start and end have zero buffer
13273 positions, which we cannot handle below. Just skip
13274 such windows. */
13275 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13276 /* For left-to-right rows, hscroll when cursor is either
13277 (i) inside the right hscroll margin, or (ii) if it is
13278 inside the left margin and the window is already
13279 hscrolled. */
13280 && ((!row_r2l_p
13281 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13282 || (cursor_row->enabled_p
13283 && cursor_row->truncated_on_right_p
13284 && (w->cursor.x >= text_area_width - h_margin))))
13285 /* For right-to-left rows, the logic is similar,
13286 except that rules for scrolling to left and right
13287 are reversed. E.g., if cursor.x <= h_margin, we
13288 need to hscroll "to the right" unconditionally,
13289 and that will scroll the screen to the left so as
13290 to reveal the next portion of the row. */
13291 || (row_r2l_p
13292 && ((cursor_row->enabled_p
13293 /* FIXME: It is confusing to set the
13294 truncated_on_right_p flag when R2L rows
13295 are actually truncated on the left. */
13296 && cursor_row->truncated_on_right_p
13297 && w->cursor.x <= h_margin)
13298 || (w->hscroll
13299 && (w->cursor.x >= (text_area_width - h_margin
13300 - x_offset)))))
13301 /* This last condition is needed when moving
13302 vertically from an hscrolled line to a short line
13303 that doesn't need to be hscrolled. If we omit
13304 this condition, the line from which we move will
13305 remain hscrolled. */
13306 || (hscl
13307 && w->hscroll != w->min_hscroll
13308 && !cursor_row->truncated_on_left_p)))
13310 struct it it;
13311 ptrdiff_t hscroll;
13312 struct buffer *saved_current_buffer;
13313 ptrdiff_t pt;
13314 int wanted_x;
13316 /* Find point in a display of infinite width. */
13317 saved_current_buffer = current_buffer;
13318 current_buffer = XBUFFER (w->contents);
13320 if (w == XWINDOW (selected_window))
13321 pt = PT;
13322 else
13323 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13325 /* Move iterator to pt starting at cursor_row->start in
13326 a line with infinite width. */
13327 init_to_row_start (&it, w, cursor_row);
13328 if (hscl)
13329 it.first_visible_x = window_hscroll_limited (w, it.f)
13330 * FRAME_COLUMN_WIDTH (it.f);
13331 it.last_visible_x = DISP_INFINITY;
13332 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13333 /* If the line ends in an overlay string with a newline,
13334 we might infloop, because displaying the window will
13335 want to put the cursor after the overlay, i.e. at X
13336 coordinate of zero on the next screen line. So we
13337 use the buffer position prior to the overlay string
13338 instead. */
13339 if (it.method == GET_FROM_STRING && pt > 1)
13341 init_to_row_start (&it, w, cursor_row);
13342 if (hscl)
13343 it.first_visible_x = (window_hscroll_limited (w, it.f)
13344 * FRAME_COLUMN_WIDTH (it.f));
13345 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13347 current_buffer = saved_current_buffer;
13349 /* Position cursor in window. */
13350 if (!hscroll_relative_p && hscroll_step_abs == 0)
13351 hscroll = max (0, (it.current_x
13352 - (ITERATOR_AT_END_OF_LINE_P (&it)
13353 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13354 : (text_area_width / 2))))
13355 / FRAME_COLUMN_WIDTH (it.f);
13356 else if ((!row_r2l_p
13357 && w->cursor.x >= text_area_width - h_margin)
13358 || (row_r2l_p && w->cursor.x <= h_margin))
13360 if (hscroll_relative_p)
13361 wanted_x = text_area_width * (1 - hscroll_step_rel)
13362 - h_margin;
13363 else
13364 wanted_x = text_area_width
13365 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13366 - h_margin;
13367 hscroll
13368 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13370 else
13372 if (hscroll_relative_p)
13373 wanted_x = text_area_width * hscroll_step_rel
13374 + h_margin;
13375 else
13376 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13377 + h_margin;
13378 hscroll
13379 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13381 hscroll = max (hscroll, w->min_hscroll);
13383 /* Don't prevent redisplay optimizations if hscroll
13384 hasn't changed, as it will unnecessarily slow down
13385 redisplay. */
13386 if (w->hscroll != hscroll
13387 /* When hscrolling only the current line, we need to
13388 report hscroll even if its value is equal to the
13389 previous one, because the new line might need a
13390 different value. */
13391 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13393 struct buffer *b = XBUFFER (w->contents);
13394 b->prevent_redisplay_optimizations_p = true;
13395 w->hscroll = hscroll;
13396 hscrolled_p = true;
13401 window = w->next;
13404 /* Value is true if hscroll of any leaf window has been changed. */
13405 return hscrolled_p;
13409 /* Set hscroll so that cursor is visible and not inside horizontal
13410 scroll margins for all windows in the tree rooted at WINDOW. See
13411 also hscroll_window_tree above. Value is true if any window's
13412 hscroll has been changed. If it has, desired matrices on the frame
13413 of WINDOW are cleared. */
13415 static bool
13416 hscroll_windows (Lisp_Object window)
13418 bool hscrolled_p = hscroll_window_tree (window);
13419 if (hscrolled_p)
13420 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13421 return hscrolled_p;
13426 /************************************************************************
13427 Redisplay
13428 ************************************************************************/
13430 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13431 This is sometimes handy to have in a debugger session. */
13433 #ifdef GLYPH_DEBUG
13435 /* First and last unchanged row for try_window_id. */
13437 static int debug_first_unchanged_at_end_vpos;
13438 static int debug_last_unchanged_at_beg_vpos;
13440 /* Delta vpos and y. */
13442 static int debug_dvpos, debug_dy;
13444 /* Delta in characters and bytes for try_window_id. */
13446 static ptrdiff_t debug_delta, debug_delta_bytes;
13448 /* Values of window_end_pos and window_end_vpos at the end of
13449 try_window_id. */
13451 static ptrdiff_t debug_end_vpos;
13453 /* Append a string to W->desired_matrix->method. FMT is a printf
13454 format string. If trace_redisplay_p is true also printf the
13455 resulting string to stderr. */
13457 static void debug_method_add (struct window *, char const *, ...)
13458 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13460 static void
13461 debug_method_add (struct window *w, char const *fmt, ...)
13463 void *ptr = w;
13464 char *method = w->desired_matrix->method;
13465 int len = strlen (method);
13466 int size = sizeof w->desired_matrix->method;
13467 int remaining = size - len - 1;
13468 va_list ap;
13470 if (len && remaining)
13472 method[len] = '|';
13473 --remaining, ++len;
13476 va_start (ap, fmt);
13477 vsnprintf (method + len, remaining + 1, fmt, ap);
13478 va_end (ap);
13480 if (trace_redisplay_p)
13481 fprintf (stderr, "%p (%s): %s\n",
13482 ptr,
13483 ((BUFFERP (w->contents)
13484 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13485 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13486 : "no buffer"),
13487 method + len);
13490 #endif /* GLYPH_DEBUG */
13493 /* Value is true if all changes in window W, which displays
13494 current_buffer, are in the text between START and END. START is a
13495 buffer position, END is given as a distance from Z. Used in
13496 redisplay_internal for display optimization. */
13498 static bool
13499 text_outside_line_unchanged_p (struct window *w,
13500 ptrdiff_t start, ptrdiff_t end)
13502 bool unchanged_p = true;
13504 /* If text or overlays have changed, see where. */
13505 if (window_outdated (w))
13507 /* Gap in the line? */
13508 if (GPT < start || Z - GPT < end)
13509 unchanged_p = false;
13511 /* Changes start in front of the line, or end after it? */
13512 if (unchanged_p
13513 && (BEG_UNCHANGED < start - 1
13514 || END_UNCHANGED < end))
13515 unchanged_p = false;
13517 /* If selective display, can't optimize if changes start at the
13518 beginning of the line. */
13519 if (unchanged_p
13520 && INTEGERP (BVAR (current_buffer, selective_display))
13521 && XINT (BVAR (current_buffer, selective_display)) > 0
13522 && (BEG_UNCHANGED < start || GPT <= start))
13523 unchanged_p = false;
13525 /* If there are overlays at the start or end of the line, these
13526 may have overlay strings with newlines in them. A change at
13527 START, for instance, may actually concern the display of such
13528 overlay strings as well, and they are displayed on different
13529 lines. So, quickly rule out this case. (For the future, it
13530 might be desirable to implement something more telling than
13531 just BEG/END_UNCHANGED.) */
13532 if (unchanged_p)
13534 if (BEG + BEG_UNCHANGED == start
13535 && overlay_touches_p (start))
13536 unchanged_p = false;
13537 if (END_UNCHANGED == end
13538 && overlay_touches_p (Z - end))
13539 unchanged_p = false;
13542 /* Under bidi reordering, adding or deleting a character in the
13543 beginning of a paragraph, before the first strong directional
13544 character, can change the base direction of the paragraph (unless
13545 the buffer specifies a fixed paragraph direction), which will
13546 require redisplaying the whole paragraph. It might be worthwhile
13547 to find the paragraph limits and widen the range of redisplayed
13548 lines to that, but for now just give up this optimization. */
13549 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13550 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13551 unchanged_p = false;
13554 return unchanged_p;
13558 /* Do a frame update, taking possible shortcuts into account. This is
13559 the main external entry point for redisplay.
13561 If the last redisplay displayed an echo area message and that message
13562 is no longer requested, we clear the echo area or bring back the
13563 mini-buffer if that is in use. */
13565 void
13566 redisplay (void)
13568 redisplay_internal ();
13572 static Lisp_Object
13573 overlay_arrow_string_or_property (Lisp_Object var)
13575 Lisp_Object val;
13577 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13578 return val;
13580 return Voverlay_arrow_string;
13583 /* Return true if there are any overlay-arrows in current_buffer. */
13584 static bool
13585 overlay_arrow_in_current_buffer_p (void)
13587 Lisp_Object vlist;
13589 for (vlist = Voverlay_arrow_variable_list;
13590 CONSP (vlist);
13591 vlist = XCDR (vlist))
13593 Lisp_Object var = XCAR (vlist);
13594 Lisp_Object val;
13596 if (!SYMBOLP (var))
13597 continue;
13598 val = find_symbol_value (var);
13599 if (MARKERP (val)
13600 && current_buffer == XMARKER (val)->buffer)
13601 return true;
13603 return false;
13607 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13608 has changed.
13609 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13610 buffers that are affected. */
13612 static bool
13613 overlay_arrows_changed_p (bool set_redisplay)
13615 Lisp_Object vlist;
13616 bool changed = false;
13618 for (vlist = Voverlay_arrow_variable_list;
13619 CONSP (vlist);
13620 vlist = XCDR (vlist))
13622 Lisp_Object var = XCAR (vlist);
13623 Lisp_Object val, pstr;
13625 if (!SYMBOLP (var))
13626 continue;
13627 val = find_symbol_value (var);
13628 if (!MARKERP (val))
13629 continue;
13630 if (! EQ (COERCE_MARKER (val),
13631 /* FIXME: Don't we have a problem, using such a global
13632 * "last-position" if the variable is buffer-local? */
13633 Fget (var, Qlast_arrow_position))
13634 || ! (pstr = overlay_arrow_string_or_property (var),
13635 EQ (pstr, Fget (var, Qlast_arrow_string))))
13637 struct buffer *buf = XMARKER (val)->buffer;
13639 if (set_redisplay)
13641 if (buf)
13642 bset_redisplay (buf);
13643 changed = true;
13645 else
13646 return true;
13649 return changed;
13652 /* Mark overlay arrows to be updated on next redisplay. */
13654 static void
13655 update_overlay_arrows (int up_to_date)
13657 Lisp_Object vlist;
13659 for (vlist = Voverlay_arrow_variable_list;
13660 CONSP (vlist);
13661 vlist = XCDR (vlist))
13663 Lisp_Object var = XCAR (vlist);
13665 if (!SYMBOLP (var))
13666 continue;
13668 if (up_to_date > 0)
13670 Lisp_Object val = find_symbol_value (var);
13671 if (!MARKERP (val))
13672 continue;
13673 Fput (var, Qlast_arrow_position,
13674 COERCE_MARKER (val));
13675 Fput (var, Qlast_arrow_string,
13676 overlay_arrow_string_or_property (var));
13678 else if (up_to_date < 0
13679 || !NILP (Fget (var, Qlast_arrow_position)))
13681 Fput (var, Qlast_arrow_position, Qt);
13682 Fput (var, Qlast_arrow_string, Qt);
13688 /* Return overlay arrow string to display at row.
13689 Return integer (bitmap number) for arrow bitmap in left fringe.
13690 Return nil if no overlay arrow. */
13692 static Lisp_Object
13693 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13695 Lisp_Object vlist;
13697 for (vlist = Voverlay_arrow_variable_list;
13698 CONSP (vlist);
13699 vlist = XCDR (vlist))
13701 Lisp_Object var = XCAR (vlist);
13702 Lisp_Object val;
13704 if (!SYMBOLP (var))
13705 continue;
13707 val = find_symbol_value (var);
13709 if (MARKERP (val)
13710 && current_buffer == XMARKER (val)->buffer
13711 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13713 if (FRAME_WINDOW_P (it->f)
13714 /* FIXME: if ROW->reversed_p is set, this should test
13715 the right fringe, not the left one. */
13716 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13718 #ifdef HAVE_WINDOW_SYSTEM
13719 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13721 int fringe_bitmap = lookup_fringe_bitmap (val);
13722 if (fringe_bitmap != 0)
13723 return make_number (fringe_bitmap);
13725 #endif
13726 return make_number (-1); /* Use default arrow bitmap. */
13728 return overlay_arrow_string_or_property (var);
13732 return Qnil;
13735 /* Return true if point moved out of or into a composition. Otherwise
13736 return false. PREV_BUF and PREV_PT are the last point buffer and
13737 position. BUF and PT are the current point buffer and position. */
13739 static bool
13740 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13741 struct buffer *buf, ptrdiff_t pt)
13743 ptrdiff_t start, end;
13744 Lisp_Object prop;
13745 Lisp_Object buffer;
13747 XSETBUFFER (buffer, buf);
13748 /* Check a composition at the last point if point moved within the
13749 same buffer. */
13750 if (prev_buf == buf)
13752 if (prev_pt == pt)
13753 /* Point didn't move. */
13754 return false;
13756 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13757 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13758 && composition_valid_p (start, end, prop)
13759 && start < prev_pt && end > prev_pt)
13760 /* The last point was within the composition. Return true iff
13761 point moved out of the composition. */
13762 return (pt <= start || pt >= end);
13765 /* Check a composition at the current point. */
13766 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13767 && find_composition (pt, -1, &start, &end, &prop, buffer)
13768 && composition_valid_p (start, end, prop)
13769 && start < pt && end > pt);
13772 /* Reconsider the clip changes of buffer which is displayed in W. */
13774 static void
13775 reconsider_clip_changes (struct window *w)
13777 struct buffer *b = XBUFFER (w->contents);
13779 if (b->clip_changed
13780 && w->window_end_valid
13781 && w->current_matrix->buffer == b
13782 && w->current_matrix->zv == BUF_ZV (b)
13783 && w->current_matrix->begv == BUF_BEGV (b))
13784 b->clip_changed = false;
13786 /* If display wasn't paused, and W is not a tool bar window, see if
13787 point has been moved into or out of a composition. In that case,
13788 set b->clip_changed to force updating the screen. If
13789 b->clip_changed has already been set, skip this check. */
13790 if (!b->clip_changed && w->window_end_valid)
13792 ptrdiff_t pt = (w == XWINDOW (selected_window)
13793 ? PT : marker_position (w->pointm));
13795 if ((w->current_matrix->buffer != b || pt != w->last_point)
13796 && check_point_in_composition (w->current_matrix->buffer,
13797 w->last_point, b, pt))
13798 b->clip_changed = true;
13802 static void
13803 propagate_buffer_redisplay (void)
13804 { /* Resetting b->text->redisplay is problematic!
13805 We can't just reset it in the case that some window that displays
13806 it has not been redisplayed; and such a window can stay
13807 unredisplayed for a long time if it's currently invisible.
13808 But we do want to reset it at the end of redisplay otherwise
13809 its displayed windows will keep being redisplayed over and over
13810 again.
13811 So we copy all b->text->redisplay flags up to their windows here,
13812 such that mark_window_display_accurate can safely reset
13813 b->text->redisplay. */
13814 Lisp_Object ws = window_list ();
13815 for (; CONSP (ws); ws = XCDR (ws))
13817 struct window *thisw = XWINDOW (XCAR (ws));
13818 struct buffer *thisb = XBUFFER (thisw->contents);
13819 if (thisb->text->redisplay)
13820 thisw->redisplay = true;
13824 #define STOP_POLLING \
13825 do { if (! polling_stopped_here) stop_polling (); \
13826 polling_stopped_here = true; } while (false)
13828 #define RESUME_POLLING \
13829 do { if (polling_stopped_here) start_polling (); \
13830 polling_stopped_here = false; } while (false)
13833 /* Perhaps in the future avoid recentering windows if it
13834 is not necessary; currently that causes some problems. */
13836 static void
13837 redisplay_internal (void)
13839 struct window *w = XWINDOW (selected_window);
13840 struct window *sw;
13841 struct frame *fr;
13842 bool pending;
13843 bool must_finish = false, match_p;
13844 struct text_pos tlbufpos, tlendpos;
13845 int number_of_visible_frames;
13846 ptrdiff_t count;
13847 struct frame *sf;
13848 bool polling_stopped_here = false;
13849 Lisp_Object tail, frame;
13851 /* Set a limit to the number of retries we perform due to horizontal
13852 scrolling, this avoids getting stuck in an uninterruptible
13853 infinite loop (Bug #24633). */
13854 enum { MAX_HSCROLL_RETRIES = 16 };
13855 int hscroll_retries = 0;
13857 /* Limit the number of retries for when frame(s) become garbaged as
13858 result of redisplaying them. Some packages set various redisplay
13859 hooks, such as window-scroll-functions, to run Lisp that always
13860 calls APIs which cause the frame's garbaged flag to become set,
13861 so we loop indefinitely. */
13862 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13863 int garbaged_frame_retries = 0;
13865 /* True means redisplay has to consider all windows on all
13866 frames. False, only selected_window is considered. */
13867 bool consider_all_windows_p;
13869 /* True means redisplay has to redisplay the miniwindow. */
13870 bool update_miniwindow_p = false;
13872 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13874 /* No redisplay if running in batch mode or frame is not yet fully
13875 initialized, or redisplay is explicitly turned off by setting
13876 Vinhibit_redisplay. */
13877 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13878 || !NILP (Vinhibit_redisplay))
13879 return;
13881 /* Don't examine these until after testing Vinhibit_redisplay.
13882 When Emacs is shutting down, perhaps because its connection to
13883 X has dropped, we should not look at them at all. */
13884 fr = XFRAME (w->frame);
13885 sf = SELECTED_FRAME ();
13887 if (!fr->glyphs_initialized_p)
13888 return;
13890 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13891 if (popup_activated ())
13892 return;
13893 #endif
13895 /* I don't think this happens but let's be paranoid. */
13896 if (redisplaying_p)
13897 return;
13899 /* Record a function that clears redisplaying_p
13900 when we leave this function. */
13901 count = SPECPDL_INDEX ();
13902 record_unwind_protect_void (unwind_redisplay);
13903 redisplaying_p = true;
13904 block_buffer_flips ();
13905 specbind (Qinhibit_free_realized_faces, Qnil);
13907 /* Record this function, so it appears on the profiler's backtraces. */
13908 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13910 FOR_EACH_FRAME (tail, frame)
13911 XFRAME (frame)->already_hscrolled_p = false;
13913 retry:
13914 /* Remember the currently selected window. */
13915 sw = w;
13917 pending = false;
13918 forget_escape_and_glyphless_faces ();
13920 inhibit_free_realized_faces = false;
13922 /* If face_change, init_iterator will free all realized faces, which
13923 includes the faces referenced from current matrices. So, we
13924 can't reuse current matrices in this case. */
13925 if (face_change)
13926 windows_or_buffers_changed = 47;
13928 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13929 && FRAME_TTY (sf)->previous_frame != sf)
13931 /* Since frames on a single ASCII terminal share the same
13932 display area, displaying a different frame means redisplay
13933 the whole thing. */
13934 SET_FRAME_GARBAGED (sf);
13935 #ifndef DOS_NT
13936 set_tty_color_mode (FRAME_TTY (sf), sf);
13937 #endif
13938 FRAME_TTY (sf)->previous_frame = sf;
13941 /* Set the visible flags for all frames. Do this before checking for
13942 resized or garbaged frames; they want to know if their frames are
13943 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13944 number_of_visible_frames = 0;
13946 FOR_EACH_FRAME (tail, frame)
13948 struct frame *f = XFRAME (frame);
13950 if (FRAME_VISIBLE_P (f))
13952 ++number_of_visible_frames;
13953 /* Adjust matrices for visible frames only. */
13954 if (f->fonts_changed)
13956 adjust_frame_glyphs (f);
13957 /* Disable all redisplay optimizations for this frame.
13958 This is because adjust_frame_glyphs resets the
13959 enabled_p flag for all glyph rows of all windows, so
13960 many optimizations will fail anyway, and some might
13961 fail to test that flag and do bogus things as
13962 result. */
13963 SET_FRAME_GARBAGED (f);
13964 f->fonts_changed = false;
13966 /* If cursor type has been changed on the frame
13967 other than selected, consider all frames. */
13968 if (f != sf && f->cursor_type_changed)
13969 fset_redisplay (f);
13971 clear_desired_matrices (f);
13974 /* Notice any pending interrupt request to change frame size. */
13975 do_pending_window_change (true);
13977 /* do_pending_window_change could change the selected_window due to
13978 frame resizing which makes the selected window too small. */
13979 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13980 sw = w;
13982 /* Clear frames marked as garbaged. */
13983 clear_garbaged_frames ();
13985 /* Build menubar and tool-bar items. */
13986 if (NILP (Vmemory_full))
13987 prepare_menu_bars ();
13989 reconsider_clip_changes (w);
13991 /* In most cases selected window displays current buffer. */
13992 match_p = XBUFFER (w->contents) == current_buffer;
13993 if (match_p)
13995 /* Detect case that we need to write or remove a star in the mode line. */
13996 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13997 w->update_mode_line = true;
13999 if (mode_line_update_needed (w))
14000 w->update_mode_line = true;
14002 /* If reconsider_clip_changes above decided that the narrowing
14003 in the current buffer changed, make sure all other windows
14004 showing that buffer will be redisplayed. */
14005 if (current_buffer->clip_changed)
14006 bset_update_mode_line (current_buffer);
14009 /* Normally the message* functions will have already displayed and
14010 updated the echo area, but the frame may have been trashed, or
14011 the update may have been preempted, so display the echo area
14012 again here. Checking message_cleared_p captures the case that
14013 the echo area should be cleared. */
14014 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
14015 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
14016 || (message_cleared_p
14017 && minibuf_level == 0
14018 /* If the mini-window is currently selected, this means the
14019 echo-area doesn't show through. */
14020 && !MINI_WINDOW_P (XWINDOW (selected_window))))
14022 echo_area_display (false);
14024 /* If echo_area_display resizes the mini-window, the redisplay and
14025 window_sizes_changed flags of the selected frame are set, but
14026 it's too late for the hooks in window-size-change-functions,
14027 which have been examined already in prepare_menu_bars. So in
14028 that case we call the hooks here only for the selected frame. */
14029 if (sf->redisplay)
14031 ptrdiff_t count1 = SPECPDL_INDEX ();
14033 record_unwind_save_match_data ();
14034 run_window_size_change_functions (selected_frame);
14035 unbind_to (count1, Qnil);
14038 if (message_cleared_p)
14039 update_miniwindow_p = true;
14041 must_finish = true;
14043 /* If we don't display the current message, don't clear the
14044 message_cleared_p flag, because, if we did, we wouldn't clear
14045 the echo area in the next redisplay which doesn't preserve
14046 the echo area. */
14047 if (!display_last_displayed_message_p)
14048 message_cleared_p = false;
14050 else if (EQ (selected_window, minibuf_window)
14051 && (current_buffer->clip_changed || window_outdated (w))
14052 && resize_mini_window (w, false))
14054 if (sf->redisplay)
14056 ptrdiff_t count1 = SPECPDL_INDEX ();
14058 record_unwind_save_match_data ();
14059 run_window_size_change_functions (selected_frame);
14060 unbind_to (count1, Qnil);
14063 /* Resized active mini-window to fit the size of what it is
14064 showing if its contents might have changed. */
14065 must_finish = true;
14067 /* If window configuration was changed, frames may have been
14068 marked garbaged. Clear them or we will experience
14069 surprises wrt scrolling. */
14070 clear_garbaged_frames ();
14073 if (windows_or_buffers_changed && !update_mode_lines)
14074 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14075 only the windows's contents needs to be refreshed, or whether the
14076 mode-lines also need a refresh. */
14077 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14078 ? REDISPLAY_SOME : 32);
14080 /* If specs for an arrow have changed, do thorough redisplay
14081 to ensure we remove any arrow that should no longer exist. */
14082 /* Apparently, this is the only case where we update other windows,
14083 without updating other mode-lines. */
14084 overlay_arrows_changed_p (true);
14086 consider_all_windows_p = (update_mode_lines
14087 || windows_or_buffers_changed);
14089 #define AINC(a,i) \
14091 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14092 if (INTEGERP (entry)) \
14093 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14096 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14097 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14099 /* Optimize the case that only the line containing the cursor in the
14100 selected window has changed. Variables starting with this_ are
14101 set in display_line and record information about the line
14102 containing the cursor. */
14103 tlbufpos = this_line_start_pos;
14104 tlendpos = this_line_end_pos;
14105 if (!consider_all_windows_p
14106 && CHARPOS (tlbufpos) > 0
14107 && !w->update_mode_line
14108 && !current_buffer->clip_changed
14109 && !current_buffer->prevent_redisplay_optimizations_p
14110 && FRAME_VISIBLE_P (XFRAME (w->frame))
14111 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14112 && !XFRAME (w->frame)->cursor_type_changed
14113 && !XFRAME (w->frame)->face_change
14114 /* Make sure recorded data applies to current buffer, etc. */
14115 && this_line_buffer == current_buffer
14116 && match_p
14117 && !w->force_start
14118 && !w->optional_new_start
14119 /* Point must be on the line that we have info recorded about. */
14120 && PT >= CHARPOS (tlbufpos)
14121 && PT <= Z - CHARPOS (tlendpos)
14122 /* All text outside that line, including its final newline,
14123 must be unchanged. */
14124 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14125 CHARPOS (tlendpos)))
14127 if (CHARPOS (tlbufpos) > BEGV
14128 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14129 && (CHARPOS (tlbufpos) == ZV
14130 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14131 /* Former continuation line has disappeared by becoming empty. */
14132 goto cancel;
14133 else if (window_outdated (w) || MINI_WINDOW_P (w))
14135 /* We have to handle the case of continuation around a
14136 wide-column character (see the comment in indent.c around
14137 line 1340).
14139 For instance, in the following case:
14141 -------- Insert --------
14142 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14143 J_I_ ==> J_I_ `^^' are cursors.
14144 ^^ ^^
14145 -------- --------
14147 As we have to redraw the line above, we cannot use this
14148 optimization. */
14150 struct it it;
14151 int line_height_before = this_line_pixel_height;
14153 /* Note that start_display will handle the case that the
14154 line starting at tlbufpos is a continuation line. */
14155 start_display (&it, w, tlbufpos);
14157 /* Implementation note: It this still necessary? */
14158 if (it.current_x != this_line_start_x)
14159 goto cancel;
14161 TRACE ((stderr, "trying display optimization 1\n"));
14162 w->cursor.vpos = -1;
14163 overlay_arrow_seen = false;
14164 it.vpos = this_line_vpos;
14165 it.current_y = this_line_y;
14166 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14167 display_line (&it, -1);
14169 /* If line contains point, is not continued,
14170 and ends at same distance from eob as before, we win. */
14171 if (w->cursor.vpos >= 0
14172 /* Line is not continued, otherwise this_line_start_pos
14173 would have been set to 0 in display_line. */
14174 && CHARPOS (this_line_start_pos)
14175 /* Line ends as before. */
14176 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14177 /* Line has same height as before. Otherwise other lines
14178 would have to be shifted up or down. */
14179 && this_line_pixel_height == line_height_before)
14181 /* If this is not the window's last line, we must adjust
14182 the charstarts of the lines below. */
14183 if (it.current_y < it.last_visible_y)
14185 struct glyph_row *row
14186 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14187 ptrdiff_t delta, delta_bytes;
14189 /* We used to distinguish between two cases here,
14190 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14191 when the line ends in a newline or the end of the
14192 buffer's accessible portion. But both cases did
14193 the same, so they were collapsed. */
14194 delta = (Z
14195 - CHARPOS (tlendpos)
14196 - MATRIX_ROW_START_CHARPOS (row));
14197 delta_bytes = (Z_BYTE
14198 - BYTEPOS (tlendpos)
14199 - MATRIX_ROW_START_BYTEPOS (row));
14201 increment_matrix_positions (w->current_matrix,
14202 this_line_vpos + 1,
14203 w->current_matrix->nrows,
14204 delta, delta_bytes);
14207 /* If this row displays text now but previously didn't,
14208 or vice versa, w->window_end_vpos may have to be
14209 adjusted. */
14210 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14212 if (w->window_end_vpos < this_line_vpos)
14213 w->window_end_vpos = this_line_vpos;
14215 else if (w->window_end_vpos == this_line_vpos
14216 && this_line_vpos > 0)
14217 w->window_end_vpos = this_line_vpos - 1;
14218 w->window_end_valid = false;
14220 /* Update hint: No need to try to scroll in update_window. */
14221 w->desired_matrix->no_scrolling_p = true;
14223 #ifdef GLYPH_DEBUG
14224 *w->desired_matrix->method = 0;
14225 debug_method_add (w, "optimization 1");
14226 #endif
14227 #ifdef HAVE_WINDOW_SYSTEM
14228 update_window_fringes (w, false);
14229 #endif
14230 goto update;
14232 else
14233 goto cancel;
14235 else if (/* Cursor position hasn't changed. */
14236 PT == w->last_point
14237 /* Make sure the cursor was last displayed
14238 in this window. Otherwise we have to reposition it. */
14240 /* PXW: Must be converted to pixels, probably. */
14241 && 0 <= w->cursor.vpos
14242 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14244 if (!must_finish)
14246 do_pending_window_change (true);
14247 /* If selected_window changed, redisplay again. */
14248 if (WINDOWP (selected_window)
14249 && (w = XWINDOW (selected_window)) != sw)
14250 goto retry;
14252 /* We used to always goto end_of_redisplay here, but this
14253 isn't enough if we have a blinking cursor. */
14254 if (w->cursor_off_p == w->last_cursor_off_p)
14255 goto end_of_redisplay;
14257 goto update;
14259 /* If highlighting the region, or if the cursor is in the echo area,
14260 then we can't just move the cursor. */
14261 else if (NILP (Vshow_trailing_whitespace)
14262 && !cursor_in_echo_area)
14264 struct it it;
14265 struct glyph_row *row;
14267 /* Skip from tlbufpos to PT and see where it is. Note that
14268 PT may be in invisible text. If so, we will end at the
14269 next visible position. */
14270 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14271 NULL, DEFAULT_FACE_ID);
14272 it.current_x = this_line_start_x;
14273 it.current_y = this_line_y;
14274 it.vpos = this_line_vpos;
14276 /* The call to move_it_to stops in front of PT, but
14277 moves over before-strings. */
14278 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14280 if (it.vpos == this_line_vpos
14281 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14282 row->enabled_p))
14284 eassert (this_line_vpos == it.vpos);
14285 eassert (this_line_y == it.current_y);
14286 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14287 if (cursor_row_fully_visible_p (w, false, true))
14289 #ifdef GLYPH_DEBUG
14290 *w->desired_matrix->method = 0;
14291 debug_method_add (w, "optimization 3");
14292 #endif
14293 goto update;
14295 else
14296 goto cancel;
14298 else
14299 goto cancel;
14302 cancel:
14303 /* Text changed drastically or point moved off of line. */
14304 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14307 CHARPOS (this_line_start_pos) = 0;
14308 ++clear_face_cache_count;
14309 #ifdef HAVE_WINDOW_SYSTEM
14310 ++clear_image_cache_count;
14311 #endif
14313 /* Build desired matrices, and update the display. If
14314 consider_all_windows_p, do it for all windows on all frames that
14315 require redisplay, as specified by their 'redisplay' flag.
14316 Otherwise do it for selected_window, only. */
14318 if (consider_all_windows_p)
14320 FOR_EACH_FRAME (tail, frame)
14321 XFRAME (frame)->updated_p = false;
14323 propagate_buffer_redisplay ();
14325 FOR_EACH_FRAME (tail, frame)
14327 struct frame *f = XFRAME (frame);
14329 /* We don't have to do anything for unselected terminal
14330 frames. */
14331 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14332 && !EQ (FRAME_TTY (f)->top_frame, frame))
14333 continue;
14335 retry_frame:
14336 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14338 bool gcscrollbars
14339 /* Only GC scrollbars when we redisplay the whole frame. */
14340 = f->redisplay || !REDISPLAY_SOME_P ();
14341 bool f_redisplay_flag = f->redisplay;
14342 /* Mark all the scroll bars to be removed; we'll redeem
14343 the ones we want when we redisplay their windows. */
14344 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14345 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14347 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14348 redisplay_windows (FRAME_ROOT_WINDOW (f));
14349 /* Remember that the invisible frames need to be redisplayed next
14350 time they're visible. */
14351 else if (!REDISPLAY_SOME_P ())
14352 f->redisplay = true;
14354 /* The X error handler may have deleted that frame. */
14355 if (!FRAME_LIVE_P (f))
14356 continue;
14358 /* Any scroll bars which redisplay_windows should have
14359 nuked should now go away. */
14360 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14361 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14363 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14365 /* If fonts changed on visible frame, display again. */
14366 if (f->fonts_changed)
14368 adjust_frame_glyphs (f);
14369 /* Disable all redisplay optimizations for this
14370 frame. For the reasons, see the comment near
14371 the previous call to adjust_frame_glyphs above. */
14372 SET_FRAME_GARBAGED (f);
14373 f->fonts_changed = false;
14374 goto retry_frame;
14377 /* See if we have to hscroll. */
14378 if (!f->already_hscrolled_p)
14380 f->already_hscrolled_p = true;
14381 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14382 && hscroll_windows (f->root_window))
14384 hscroll_retries++;
14385 goto retry_frame;
14389 /* If the frame's redisplay flag was not set before
14390 we went about redisplaying its windows, but it is
14391 set now, that means we employed some redisplay
14392 optimizations inside redisplay_windows, and
14393 bypassed producing some screen lines. But if
14394 f->redisplay is now set, it might mean the old
14395 faces are no longer valid (e.g., if redisplaying
14396 some window called some Lisp which defined a new
14397 face or redefined an existing face), so trying to
14398 use them in update_frame will segfault.
14399 Therefore, we must redisplay this frame. */
14400 if (!f_redisplay_flag && f->redisplay)
14401 goto retry_frame;
14402 /* In some case (e.g., window resize), we notice
14403 only during window updating that the window
14404 content changed unpredictably (e.g., a GTK
14405 scrollbar moved, or some Lisp hook that winds up
14406 calling adjust_frame_glyphs) and that our
14407 previous estimation of the frame content was
14408 garbage. We have to start over. These cases
14409 should be rare, so going all the way back to the
14410 top of redisplay should be good enough. */
14411 if (FRAME_GARBAGED_P (f)
14412 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14413 goto retry;
14415 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14416 x_clear_under_internal_border (f);
14417 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14419 /* Prevent various kinds of signals during display
14420 update. stdio is not robust about handling
14421 signals, which can cause an apparent I/O error. */
14422 if (interrupt_input)
14423 unrequest_sigio ();
14424 STOP_POLLING;
14426 pending |= update_frame (f, false, false);
14427 f->cursor_type_changed = false;
14428 f->updated_p = true;
14433 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14435 if (!pending)
14437 /* Do the mark_window_display_accurate after all windows have
14438 been redisplayed because this call resets flags in buffers
14439 which are needed for proper redisplay. */
14440 FOR_EACH_FRAME (tail, frame)
14442 struct frame *f = XFRAME (frame);
14443 if (f->updated_p)
14445 f->redisplay = false;
14446 f->garbaged = false;
14447 mark_window_display_accurate (f->root_window, true);
14448 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14449 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14454 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14456 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14457 /* Use list_of_error, not Qerror, so that
14458 we catch only errors and don't run the debugger. */
14459 internal_condition_case_1 (redisplay_window_1, selected_window,
14460 list_of_error,
14461 redisplay_window_error);
14462 if (update_miniwindow_p)
14463 internal_condition_case_1 (redisplay_window_1,
14464 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14465 redisplay_window_error);
14467 /* Compare desired and current matrices, perform output. */
14469 update:
14470 /* If fonts changed, display again. Likewise if redisplay_window_1
14471 above caused some change (e.g., a change in faces) that requires
14472 considering the entire frame again. */
14473 if (sf->fonts_changed || sf->redisplay)
14475 if (sf->redisplay)
14477 /* Set this to force a more thorough redisplay.
14478 Otherwise, we might immediately loop back to the
14479 above "else-if" clause (since all the conditions that
14480 led here might still be true), and we will then
14481 infloop, because the selected-frame's redisplay flag
14482 is not (and cannot be) reset. */
14483 windows_or_buffers_changed = 50;
14485 goto retry;
14488 /* Prevent freeing of realized faces, since desired matrices are
14489 pending that reference the faces we computed and cached. */
14490 inhibit_free_realized_faces = true;
14492 /* Prevent various kinds of signals during display update.
14493 stdio is not robust about handling signals,
14494 which can cause an apparent I/O error. */
14495 if (interrupt_input)
14496 unrequest_sigio ();
14497 STOP_POLLING;
14499 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14501 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14502 && hscroll_windows (selected_window))
14504 hscroll_retries++;
14505 goto retry;
14508 XWINDOW (selected_window)->must_be_updated_p = true;
14509 pending = update_frame (sf, false, false);
14510 sf->cursor_type_changed = false;
14513 /* We may have called echo_area_display at the top of this
14514 function. If the echo area is on another frame, that may
14515 have put text on a frame other than the selected one, so the
14516 above call to update_frame would not have caught it. Catch
14517 it here. */
14518 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14519 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14521 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14523 XWINDOW (mini_window)->must_be_updated_p = true;
14524 pending |= update_frame (mini_frame, false, false);
14525 mini_frame->cursor_type_changed = false;
14526 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14527 && hscroll_windows (mini_window))
14529 hscroll_retries++;
14530 goto retry;
14535 /* If display was paused because of pending input, make sure we do a
14536 thorough update the next time. */
14537 if (pending)
14539 /* Prevent the optimization at the beginning of
14540 redisplay_internal that tries a single-line update of the
14541 line containing the cursor in the selected window. */
14542 CHARPOS (this_line_start_pos) = 0;
14544 /* Let the overlay arrow be updated the next time. */
14545 update_overlay_arrows (0);
14547 /* If we pause after scrolling, some rows in the current
14548 matrices of some windows are not valid. */
14549 if (!WINDOW_FULL_WIDTH_P (w)
14550 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14551 update_mode_lines = 36;
14553 else
14555 if (!consider_all_windows_p)
14557 /* This has already been done above if
14558 consider_all_windows_p is set. */
14559 if (XBUFFER (w->contents)->text->redisplay
14560 && buffer_window_count (XBUFFER (w->contents)) > 1)
14561 /* This can happen if b->text->redisplay was set during
14562 jit-lock. */
14563 propagate_buffer_redisplay ();
14564 mark_window_display_accurate_1 (w, true);
14566 /* Say overlay arrows are up to date. */
14567 update_overlay_arrows (1);
14569 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14570 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14573 update_mode_lines = 0;
14574 windows_or_buffers_changed = 0;
14577 /* Start SIGIO interrupts coming again. Having them off during the
14578 code above makes it less likely one will discard output, but not
14579 impossible, since there might be stuff in the system buffer here.
14580 But it is much hairier to try to do anything about that. */
14581 if (interrupt_input)
14582 request_sigio ();
14583 RESUME_POLLING;
14585 /* If a frame has become visible which was not before, redisplay
14586 again, so that we display it. Expose events for such a frame
14587 (which it gets when becoming visible) don't call the parts of
14588 redisplay constructing glyphs, so simply exposing a frame won't
14589 display anything in this case. So, we have to display these
14590 frames here explicitly. */
14591 if (!pending)
14593 int new_count = 0;
14595 FOR_EACH_FRAME (tail, frame)
14597 if (XFRAME (frame)->visible)
14598 new_count++;
14601 if (new_count != number_of_visible_frames)
14602 windows_or_buffers_changed = 52;
14605 /* Change frame size now if a change is pending. */
14606 do_pending_window_change (true);
14608 /* If we just did a pending size change, or have additional
14609 visible frames, or selected_window changed, redisplay again. */
14610 if ((windows_or_buffers_changed && !pending)
14611 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14612 goto retry;
14614 /* Clear the face and image caches.
14616 We used to do this only if consider_all_windows_p. But the cache
14617 needs to be cleared if a timer creates images in the current
14618 buffer (e.g. the test case in Bug#6230). */
14620 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14622 clear_face_cache (false);
14623 clear_face_cache_count = 0;
14626 #ifdef HAVE_WINDOW_SYSTEM
14627 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14629 clear_image_caches (Qnil);
14630 clear_image_cache_count = 0;
14632 #endif /* HAVE_WINDOW_SYSTEM */
14634 end_of_redisplay:
14635 #ifdef HAVE_NS
14636 ns_set_doc_edited ();
14637 #endif
14638 if (interrupt_input && interrupts_deferred)
14639 request_sigio ();
14641 unbind_to (count, Qnil);
14642 RESUME_POLLING;
14645 static void
14646 unwind_redisplay_preserve_echo_area (void)
14648 unblock_buffer_flips ();
14651 /* Redisplay, but leave alone any recent echo area message unless
14652 another message has been requested in its place.
14654 This is useful in situations where you need to redisplay but no
14655 user action has occurred, making it inappropriate for the message
14656 area to be cleared. See tracking_off and
14657 wait_reading_process_output for examples of these situations.
14659 FROM_WHERE is an integer saying from where this function was
14660 called. This is useful for debugging. */
14662 void
14663 redisplay_preserve_echo_area (int from_where)
14665 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14667 block_input ();
14668 ptrdiff_t count = SPECPDL_INDEX ();
14669 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14670 block_buffer_flips ();
14671 unblock_input ();
14673 if (!NILP (echo_area_buffer[1]))
14675 /* We have a previously displayed message, but no current
14676 message. Redisplay the previous message. */
14677 display_last_displayed_message_p = true;
14678 redisplay_internal ();
14679 display_last_displayed_message_p = false;
14681 else
14682 redisplay_internal ();
14684 flush_frame (SELECTED_FRAME ());
14685 unbind_to (count, Qnil);
14689 /* Function registered with record_unwind_protect in redisplay_internal. */
14691 static void
14692 unwind_redisplay (void)
14694 redisplaying_p = false;
14695 unblock_buffer_flips ();
14699 /* Mark the display of leaf window W as accurate or inaccurate.
14700 If ACCURATE_P, mark display of W as accurate.
14701 If !ACCURATE_P, arrange for W to be redisplayed the next
14702 time redisplay_internal is called. */
14704 static void
14705 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14707 struct buffer *b = XBUFFER (w->contents);
14709 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14710 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14711 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14713 if (accurate_p)
14715 b->clip_changed = false;
14716 b->prevent_redisplay_optimizations_p = false;
14717 eassert (buffer_window_count (b) > 0);
14718 /* Resetting b->text->redisplay is problematic!
14719 In order to make it safer to do it here, redisplay_internal must
14720 have copied all b->text->redisplay to their respective windows. */
14721 b->text->redisplay = false;
14723 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14724 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14725 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14726 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14728 w->current_matrix->buffer = b;
14729 w->current_matrix->begv = BUF_BEGV (b);
14730 w->current_matrix->zv = BUF_ZV (b);
14732 w->last_cursor_vpos = w->cursor.vpos;
14733 w->last_cursor_off_p = w->cursor_off_p;
14735 if (w == XWINDOW (selected_window))
14736 w->last_point = BUF_PT (b);
14737 else
14738 w->last_point = marker_position (w->pointm);
14740 w->window_end_valid = true;
14741 w->update_mode_line = false;
14744 w->redisplay = !accurate_p;
14748 /* Mark the display of windows in the window tree rooted at WINDOW as
14749 accurate or inaccurate. If ACCURATE_P, mark display of
14750 windows as accurate. If !ACCURATE_P, arrange for windows to
14751 be redisplayed the next time redisplay_internal is called. */
14753 void
14754 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14756 struct window *w;
14758 for (; !NILP (window); window = w->next)
14760 w = XWINDOW (window);
14761 if (WINDOWP (w->contents))
14762 mark_window_display_accurate (w->contents, accurate_p);
14763 else
14764 mark_window_display_accurate_1 (w, accurate_p);
14767 if (accurate_p)
14768 update_overlay_arrows (1);
14769 else
14770 /* Force a thorough redisplay the next time by setting
14771 last_arrow_position and last_arrow_string to t, which is
14772 unequal to any useful value of Voverlay_arrow_... */
14773 update_overlay_arrows (-1);
14777 /* Return value in display table DP (Lisp_Char_Table *) for character
14778 C. Since a display table doesn't have any parent, we don't have to
14779 follow parent. Do not call this function directly but use the
14780 macro DISP_CHAR_VECTOR. */
14782 Lisp_Object
14783 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14785 Lisp_Object val;
14787 if (ASCII_CHAR_P (c))
14789 val = dp->ascii;
14790 if (SUB_CHAR_TABLE_P (val))
14791 val = XSUB_CHAR_TABLE (val)->contents[c];
14793 else
14795 Lisp_Object table;
14797 XSETCHAR_TABLE (table, dp);
14798 val = char_table_ref (table, c);
14800 if (NILP (val))
14801 val = dp->defalt;
14802 return val;
14805 static int buffer_flip_blocked_depth;
14807 static void
14808 block_buffer_flips (void)
14810 eassert (buffer_flip_blocked_depth >= 0);
14811 buffer_flip_blocked_depth++;
14814 static void
14815 unblock_buffer_flips (void)
14817 eassert (buffer_flip_blocked_depth > 0);
14818 if (--buffer_flip_blocked_depth == 0)
14820 Lisp_Object tail, frame;
14821 block_input ();
14822 FOR_EACH_FRAME (tail, frame)
14824 struct frame *f = XFRAME (frame);
14825 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14826 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14828 unblock_input ();
14832 bool
14833 buffer_flipping_blocked_p (void)
14835 return buffer_flip_blocked_depth > 0;
14839 /***********************************************************************
14840 Window Redisplay
14841 ***********************************************************************/
14843 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14845 static void
14846 redisplay_windows (Lisp_Object window)
14848 while (!NILP (window))
14850 struct window *w = XWINDOW (window);
14852 if (WINDOWP (w->contents))
14853 redisplay_windows (w->contents);
14854 else if (BUFFERP (w->contents))
14856 displayed_buffer = XBUFFER (w->contents);
14857 /* Use list_of_error, not Qerror, so that
14858 we catch only errors and don't run the debugger. */
14859 internal_condition_case_1 (redisplay_window_0, window,
14860 list_of_error,
14861 redisplay_window_error);
14864 window = w->next;
14868 static Lisp_Object
14869 redisplay_window_error (Lisp_Object ignore)
14871 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14872 return Qnil;
14875 static Lisp_Object
14876 redisplay_window_0 (Lisp_Object window)
14878 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14879 redisplay_window (window, false);
14880 return Qnil;
14883 static Lisp_Object
14884 redisplay_window_1 (Lisp_Object window)
14886 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14887 redisplay_window (window, true);
14888 return Qnil;
14892 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14893 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14894 which positions recorded in ROW differ from current buffer
14895 positions.
14897 Return true iff cursor is on this row. */
14899 static bool
14900 set_cursor_from_row (struct window *w, struct glyph_row *row,
14901 struct glyph_matrix *matrix,
14902 ptrdiff_t delta, ptrdiff_t delta_bytes,
14903 int dy, int dvpos)
14905 struct glyph *glyph = row->glyphs[TEXT_AREA];
14906 struct glyph *end = glyph + row->used[TEXT_AREA];
14907 struct glyph *cursor = NULL;
14908 /* The last known character position in row. */
14909 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14910 int x = row->x;
14911 ptrdiff_t pt_old = PT - delta;
14912 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14913 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14914 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14915 /* A glyph beyond the edge of TEXT_AREA which we should never
14916 touch. */
14917 struct glyph *glyphs_end = end;
14918 /* True means we've found a match for cursor position, but that
14919 glyph has the avoid_cursor_p flag set. */
14920 bool match_with_avoid_cursor = false;
14921 /* True means we've seen at least one glyph that came from a
14922 display string. */
14923 bool string_seen = false;
14924 /* Largest and smallest buffer positions seen so far during scan of
14925 glyph row. */
14926 ptrdiff_t bpos_max = pos_before;
14927 ptrdiff_t bpos_min = pos_after;
14928 /* Last buffer position covered by an overlay string with an integer
14929 `cursor' property. */
14930 ptrdiff_t bpos_covered = 0;
14931 /* True means the display string on which to display the cursor
14932 comes from a text property, not from an overlay. */
14933 bool string_from_text_prop = false;
14935 /* Don't even try doing anything if called for a mode-line or
14936 header-line row, since the rest of the code isn't prepared to
14937 deal with such calamities. */
14938 eassert (!row->mode_line_p);
14939 if (row->mode_line_p)
14940 return false;
14942 /* Skip over glyphs not having an object at the start and the end of
14943 the row. These are special glyphs like truncation marks on
14944 terminal frames. */
14945 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14947 if (!row->reversed_p)
14949 while (glyph < end
14950 && NILP (glyph->object)
14951 && glyph->charpos < 0)
14953 x += glyph->pixel_width;
14954 ++glyph;
14956 while (end > glyph
14957 && NILP ((end - 1)->object)
14958 /* CHARPOS is zero for blanks and stretch glyphs
14959 inserted by extend_face_to_end_of_line. */
14960 && (end - 1)->charpos <= 0)
14961 --end;
14962 glyph_before = glyph - 1;
14963 glyph_after = end;
14965 else
14967 struct glyph *g;
14969 /* If the glyph row is reversed, we need to process it from back
14970 to front, so swap the edge pointers. */
14971 glyphs_end = end = glyph - 1;
14972 glyph += row->used[TEXT_AREA] - 1;
14974 while (glyph > end + 1
14975 && NILP (glyph->object)
14976 && glyph->charpos < 0)
14977 --glyph;
14978 if (NILP (glyph->object) && glyph->charpos < 0)
14979 --glyph;
14980 /* By default, in reversed rows we put the cursor on the
14981 rightmost (first in the reading order) glyph. */
14982 for (x = 0, g = end + 1; g < glyph; g++)
14983 x += g->pixel_width;
14984 while (end < glyph
14985 && NILP ((end + 1)->object)
14986 && (end + 1)->charpos <= 0)
14987 ++end;
14988 glyph_before = glyph + 1;
14989 glyph_after = end;
14992 else if (row->reversed_p)
14994 /* In R2L rows that don't display text, put the cursor on the
14995 rightmost glyph. Case in point: an empty last line that is
14996 part of an R2L paragraph. */
14997 cursor = end - 1;
14998 /* Avoid placing the cursor on the last glyph of the row, where
14999 on terminal frames we hold the vertical border between
15000 adjacent windows. */
15001 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
15002 && !WINDOW_RIGHTMOST_P (w)
15003 && cursor == row->glyphs[LAST_AREA] - 1)
15004 cursor--;
15005 x = -1; /* will be computed below, at label compute_x */
15008 /* Step 1: Try to find the glyph whose character position
15009 corresponds to point. If that's not possible, find 2 glyphs
15010 whose character positions are the closest to point, one before
15011 point, the other after it. */
15012 if (!row->reversed_p)
15013 while (/* not marched to end of glyph row */
15014 glyph < end
15015 /* glyph was not inserted by redisplay for internal purposes */
15016 && !NILP (glyph->object))
15018 if (BUFFERP (glyph->object))
15020 ptrdiff_t dpos = glyph->charpos - pt_old;
15022 if (glyph->charpos > bpos_max)
15023 bpos_max = glyph->charpos;
15024 if (glyph->charpos < bpos_min)
15025 bpos_min = glyph->charpos;
15026 if (!glyph->avoid_cursor_p)
15028 /* If we hit point, we've found the glyph on which to
15029 display the cursor. */
15030 if (dpos == 0)
15032 match_with_avoid_cursor = false;
15033 break;
15035 /* See if we've found a better approximation to
15036 POS_BEFORE or to POS_AFTER. */
15037 if (0 > dpos && dpos > pos_before - pt_old)
15039 pos_before = glyph->charpos;
15040 glyph_before = glyph;
15042 else if (0 < dpos && dpos < pos_after - pt_old)
15044 pos_after = glyph->charpos;
15045 glyph_after = glyph;
15048 else if (dpos == 0)
15049 match_with_avoid_cursor = true;
15051 else if (STRINGP (glyph->object))
15053 Lisp_Object chprop;
15054 ptrdiff_t glyph_pos = glyph->charpos;
15056 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15057 glyph->object);
15058 if (!NILP (chprop))
15060 /* If the string came from a `display' text property,
15061 look up the buffer position of that property and
15062 use that position to update bpos_max, as if we
15063 actually saw such a position in one of the row's
15064 glyphs. This helps with supporting integer values
15065 of `cursor' property on the display string in
15066 situations where most or all of the row's buffer
15067 text is completely covered by display properties,
15068 so that no glyph with valid buffer positions is
15069 ever seen in the row. */
15070 ptrdiff_t prop_pos =
15071 string_buffer_position_lim (glyph->object, pos_before,
15072 pos_after, false);
15074 if (prop_pos >= pos_before)
15075 bpos_max = prop_pos;
15077 if (INTEGERP (chprop))
15079 bpos_covered = bpos_max + XINT (chprop);
15080 /* If the `cursor' property covers buffer positions up
15081 to and including point, we should display cursor on
15082 this glyph. Note that, if a `cursor' property on one
15083 of the string's characters has an integer value, we
15084 will break out of the loop below _before_ we get to
15085 the position match above. IOW, integer values of
15086 the `cursor' property override the "exact match for
15087 point" strategy of positioning the cursor. */
15088 /* Implementation note: bpos_max == pt_old when, e.g.,
15089 we are in an empty line, where bpos_max is set to
15090 MATRIX_ROW_START_CHARPOS, see above. */
15091 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15093 cursor = glyph;
15094 break;
15098 string_seen = true;
15100 x += glyph->pixel_width;
15101 ++glyph;
15103 else if (glyph > end) /* row is reversed */
15104 while (!NILP (glyph->object))
15106 if (BUFFERP (glyph->object))
15108 ptrdiff_t dpos = glyph->charpos - pt_old;
15110 if (glyph->charpos > bpos_max)
15111 bpos_max = glyph->charpos;
15112 if (glyph->charpos < bpos_min)
15113 bpos_min = glyph->charpos;
15114 if (!glyph->avoid_cursor_p)
15116 if (dpos == 0)
15118 match_with_avoid_cursor = false;
15119 break;
15121 if (0 > dpos && dpos > pos_before - pt_old)
15123 pos_before = glyph->charpos;
15124 glyph_before = glyph;
15126 else if (0 < dpos && dpos < pos_after - pt_old)
15128 pos_after = glyph->charpos;
15129 glyph_after = glyph;
15132 else if (dpos == 0)
15133 match_with_avoid_cursor = true;
15135 else if (STRINGP (glyph->object))
15137 Lisp_Object chprop;
15138 ptrdiff_t glyph_pos = glyph->charpos;
15140 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15141 glyph->object);
15142 if (!NILP (chprop))
15144 ptrdiff_t prop_pos =
15145 string_buffer_position_lim (glyph->object, pos_before,
15146 pos_after, false);
15148 if (prop_pos >= pos_before)
15149 bpos_max = prop_pos;
15151 if (INTEGERP (chprop))
15153 bpos_covered = bpos_max + XINT (chprop);
15154 /* If the `cursor' property covers buffer positions up
15155 to and including point, we should display cursor on
15156 this glyph. */
15157 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15159 cursor = glyph;
15160 break;
15163 string_seen = true;
15165 --glyph;
15166 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15168 x--; /* can't use any pixel_width */
15169 break;
15171 x -= glyph->pixel_width;
15174 /* Step 2: If we didn't find an exact match for point, we need to
15175 look for a proper place to put the cursor among glyphs between
15176 GLYPH_BEFORE and GLYPH_AFTER. */
15177 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15178 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15179 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15181 /* An empty line has a single glyph whose OBJECT is nil and
15182 whose CHARPOS is the position of a newline on that line.
15183 Note that on a TTY, there are more glyphs after that, which
15184 were produced by extend_face_to_end_of_line, but their
15185 CHARPOS is zero or negative. */
15186 bool empty_line_p =
15187 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15188 && NILP (glyph->object) && glyph->charpos > 0
15189 /* On a TTY, continued and truncated rows also have a glyph at
15190 their end whose OBJECT is nil and whose CHARPOS is
15191 positive (the continuation and truncation glyphs), but such
15192 rows are obviously not "empty". */
15193 && !(row->continued_p || row->truncated_on_right_p));
15195 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15197 ptrdiff_t ellipsis_pos;
15199 /* Scan back over the ellipsis glyphs. */
15200 if (!row->reversed_p)
15202 ellipsis_pos = (glyph - 1)->charpos;
15203 while (glyph > row->glyphs[TEXT_AREA]
15204 && (glyph - 1)->charpos == ellipsis_pos)
15205 glyph--, x -= glyph->pixel_width;
15206 /* That loop always goes one position too far, including
15207 the glyph before the ellipsis. So scan forward over
15208 that one. */
15209 x += glyph->pixel_width;
15210 glyph++;
15212 else /* row is reversed */
15214 ellipsis_pos = (glyph + 1)->charpos;
15215 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15216 && (glyph + 1)->charpos == ellipsis_pos)
15217 glyph++, x += glyph->pixel_width;
15218 x -= glyph->pixel_width;
15219 glyph--;
15222 else if (match_with_avoid_cursor)
15224 cursor = glyph_after;
15225 x = -1;
15227 else if (string_seen)
15229 int incr = row->reversed_p ? -1 : +1;
15231 /* Need to find the glyph that came out of a string which is
15232 present at point. That glyph is somewhere between
15233 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15234 positioned between POS_BEFORE and POS_AFTER in the
15235 buffer. */
15236 struct glyph *start, *stop;
15237 ptrdiff_t pos = pos_before;
15239 x = -1;
15241 /* If the row ends in a newline from a display string,
15242 reordering could have moved the glyphs belonging to the
15243 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15244 in this case we extend the search to the last glyph in
15245 the row that was not inserted by redisplay. */
15246 if (row->ends_in_newline_from_string_p)
15248 glyph_after = end;
15249 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15252 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15253 correspond to POS_BEFORE and POS_AFTER, respectively. We
15254 need START and STOP in the order that corresponds to the
15255 row's direction as given by its reversed_p flag. If the
15256 directionality of characters between POS_BEFORE and
15257 POS_AFTER is the opposite of the row's base direction,
15258 these characters will have been reordered for display,
15259 and we need to reverse START and STOP. */
15260 if (!row->reversed_p)
15262 start = min (glyph_before, glyph_after);
15263 stop = max (glyph_before, glyph_after);
15265 else
15267 start = max (glyph_before, glyph_after);
15268 stop = min (glyph_before, glyph_after);
15270 for (glyph = start + incr;
15271 row->reversed_p ? glyph > stop : glyph < stop; )
15274 /* Any glyphs that come from the buffer are here because
15275 of bidi reordering. Skip them, and only pay
15276 attention to glyphs that came from some string. */
15277 if (STRINGP (glyph->object))
15279 Lisp_Object str;
15280 ptrdiff_t tem;
15281 /* If the display property covers the newline, we
15282 need to search for it one position farther. */
15283 ptrdiff_t lim = pos_after
15284 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15286 string_from_text_prop = false;
15287 str = glyph->object;
15288 tem = string_buffer_position_lim (str, pos, lim, false);
15289 if (tem == 0 /* from overlay */
15290 || pos <= tem)
15292 /* If the string from which this glyph came is
15293 found in the buffer at point, or at position
15294 that is closer to point than pos_after, then
15295 we've found the glyph we've been looking for.
15296 If it comes from an overlay (tem == 0), and
15297 it has the `cursor' property on one of its
15298 glyphs, record that glyph as a candidate for
15299 displaying the cursor. (As in the
15300 unidirectional version, we will display the
15301 cursor on the last candidate we find.) */
15302 if (tem == 0
15303 || tem == pt_old
15304 || (tem - pt_old > 0 && tem < pos_after))
15306 /* The glyphs from this string could have
15307 been reordered. Find the one with the
15308 smallest string position. Or there could
15309 be a character in the string with the
15310 `cursor' property, which means display
15311 cursor on that character's glyph. */
15312 ptrdiff_t strpos = glyph->charpos;
15314 if (tem)
15316 cursor = glyph;
15317 string_from_text_prop = true;
15319 for ( ;
15320 (row->reversed_p ? glyph > stop : glyph < stop)
15321 && EQ (glyph->object, str);
15322 glyph += incr)
15324 Lisp_Object cprop;
15325 ptrdiff_t gpos = glyph->charpos;
15327 cprop = Fget_char_property (make_number (gpos),
15328 Qcursor,
15329 glyph->object);
15330 if (!NILP (cprop))
15332 cursor = glyph;
15333 break;
15335 if (tem && glyph->charpos < strpos)
15337 strpos = glyph->charpos;
15338 cursor = glyph;
15342 if (tem == pt_old
15343 || (tem - pt_old > 0 && tem < pos_after))
15344 goto compute_x;
15346 if (tem)
15347 pos = tem + 1; /* don't find previous instances */
15349 /* This string is not what we want; skip all of the
15350 glyphs that came from it. */
15351 while ((row->reversed_p ? glyph > stop : glyph < stop)
15352 && EQ (glyph->object, str))
15353 glyph += incr;
15355 else
15356 glyph += incr;
15359 /* If we reached the end of the line, and END was from a string,
15360 the cursor is not on this line. */
15361 if (cursor == NULL
15362 && (row->reversed_p ? glyph <= end : glyph >= end)
15363 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15364 && STRINGP (end->object)
15365 && row->continued_p)
15366 return false;
15368 /* A truncated row may not include PT among its character positions.
15369 Setting the cursor inside the scroll margin will trigger
15370 recalculation of hscroll in hscroll_window_tree. But if a
15371 display string covers point, defer to the string-handling
15372 code below to figure this out. */
15373 else if (row->truncated_on_left_p && pt_old < bpos_min)
15375 cursor = glyph_before;
15376 x = -1;
15378 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15379 /* Zero-width characters produce no glyphs. */
15380 || (!empty_line_p
15381 && (row->reversed_p
15382 ? glyph_after > glyphs_end
15383 : glyph_after < glyphs_end)))
15385 cursor = glyph_after;
15386 x = -1;
15390 compute_x:
15391 if (cursor != NULL)
15392 glyph = cursor;
15393 else if (glyph == glyphs_end
15394 && pos_before == pos_after
15395 && STRINGP ((row->reversed_p
15396 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15397 : row->glyphs[TEXT_AREA])->object))
15399 /* If all the glyphs of this row came from strings, put the
15400 cursor on the first glyph of the row. This avoids having the
15401 cursor outside of the text area in this very rare and hard
15402 use case. */
15403 glyph =
15404 row->reversed_p
15405 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15406 : row->glyphs[TEXT_AREA];
15408 if (x < 0)
15410 struct glyph *g;
15412 /* Need to compute x that corresponds to GLYPH. */
15413 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15415 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15416 emacs_abort ();
15417 x += g->pixel_width;
15421 /* ROW could be part of a continued line, which, under bidi
15422 reordering, might have other rows whose start and end charpos
15423 occlude point. Only set w->cursor if we found a better
15424 approximation to the cursor position than we have from previously
15425 examined candidate rows belonging to the same continued line. */
15426 if (/* We already have a candidate row. */
15427 w->cursor.vpos >= 0
15428 /* That candidate is not the row we are processing. */
15429 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15430 /* Make sure cursor.vpos specifies a row whose start and end
15431 charpos occlude point, and it is valid candidate for being a
15432 cursor-row. This is because some callers of this function
15433 leave cursor.vpos at the row where the cursor was displayed
15434 during the last redisplay cycle. */
15435 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15436 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15437 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15439 struct glyph *g1
15440 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15442 /* Don't consider glyphs that are outside TEXT_AREA. */
15443 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15444 return false;
15445 /* Keep the candidate whose buffer position is the closest to
15446 point or has the `cursor' property. */
15447 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15448 w->cursor.hpos >= 0
15449 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15450 && ((BUFFERP (g1->object)
15451 && (g1->charpos == pt_old /* An exact match always wins. */
15452 || (BUFFERP (glyph->object)
15453 && eabs (g1->charpos - pt_old)
15454 < eabs (glyph->charpos - pt_old))))
15455 /* Previous candidate is a glyph from a string that has
15456 a non-nil `cursor' property. */
15457 || (STRINGP (g1->object)
15458 && (!NILP (Fget_char_property (make_number (g1->charpos),
15459 Qcursor, g1->object))
15460 /* Previous candidate is from the same display
15461 string as this one, and the display string
15462 came from a text property. */
15463 || (EQ (g1->object, glyph->object)
15464 && string_from_text_prop)
15465 /* this candidate is from newline and its
15466 position is not an exact match */
15467 || (NILP (glyph->object)
15468 && glyph->charpos != pt_old)))))
15469 return false;
15470 /* If this candidate gives an exact match, use that. */
15471 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15472 /* If this candidate is a glyph created for the
15473 terminating newline of a line, and point is on that
15474 newline, it wins because it's an exact match. */
15475 || (!row->continued_p
15476 && NILP (glyph->object)
15477 && glyph->charpos == 0
15478 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15479 /* Otherwise, keep the candidate that comes from a row
15480 spanning less buffer positions. This may win when one or
15481 both candidate positions are on glyphs that came from
15482 display strings, for which we cannot compare buffer
15483 positions. */
15484 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15485 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15486 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15487 return false;
15489 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15490 w->cursor.x = x;
15491 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15492 w->cursor.y = row->y + dy;
15494 if (w == XWINDOW (selected_window))
15496 if (!row->continued_p
15497 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15498 && row->x == 0)
15500 this_line_buffer = XBUFFER (w->contents);
15502 CHARPOS (this_line_start_pos)
15503 = MATRIX_ROW_START_CHARPOS (row) + delta;
15504 BYTEPOS (this_line_start_pos)
15505 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15507 CHARPOS (this_line_end_pos)
15508 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15509 BYTEPOS (this_line_end_pos)
15510 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15512 this_line_y = w->cursor.y;
15513 this_line_pixel_height = row->height;
15514 this_line_vpos = w->cursor.vpos;
15515 this_line_start_x = row->x;
15517 else
15518 CHARPOS (this_line_start_pos) = 0;
15521 return true;
15525 /* Run window scroll functions, if any, for WINDOW with new window
15526 start STARTP. Sets the window start of WINDOW to that position.
15528 We assume that the window's buffer is really current. */
15530 static struct text_pos
15531 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15533 struct window *w = XWINDOW (window);
15534 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15536 eassert (current_buffer == XBUFFER (w->contents));
15538 if (!NILP (Vwindow_scroll_functions))
15540 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15541 make_number (CHARPOS (startp)));
15542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15543 /* In case the hook functions switch buffers. */
15544 set_buffer_internal (XBUFFER (w->contents));
15547 return startp;
15551 /* Make sure the line containing the cursor is fully visible.
15552 A value of true means there is nothing to be done.
15553 (Either the line is fully visible, or it cannot be made so,
15554 or we cannot tell.)
15556 If FORCE_P, return false even if partial visible cursor row
15557 is higher than window.
15559 If CURRENT_MATRIX_P, use the information from the
15560 window's current glyph matrix; otherwise use the desired glyph
15561 matrix.
15563 A value of false means the caller should do scrolling
15564 as if point had gone off the screen. */
15566 static bool
15567 cursor_row_fully_visible_p (struct window *w, bool force_p,
15568 bool current_matrix_p)
15570 struct glyph_matrix *matrix;
15571 struct glyph_row *row;
15572 int window_height;
15574 if (!make_cursor_line_fully_visible_p)
15575 return true;
15577 /* It's not always possible to find the cursor, e.g, when a window
15578 is full of overlay strings. Don't do anything in that case. */
15579 if (w->cursor.vpos < 0)
15580 return true;
15582 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15583 row = MATRIX_ROW (matrix, w->cursor.vpos);
15585 /* If the cursor row is not partially visible, there's nothing to do. */
15586 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15587 return true;
15589 /* If the row the cursor is in is taller than the window's height,
15590 it's not clear what to do, so do nothing. */
15591 window_height = window_box_height (w);
15592 if (row->height >= window_height)
15594 if (!force_p || MINI_WINDOW_P (w)
15595 || w->vscroll || w->cursor.vpos == 0)
15596 return true;
15598 return false;
15602 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15603 means only WINDOW is redisplayed in redisplay_internal.
15604 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15605 in redisplay_window to bring a partially visible line into view in
15606 the case that only the cursor has moved.
15608 LAST_LINE_MISFIT should be true if we're scrolling because the
15609 last screen line's vertical height extends past the end of the screen.
15611 Value is
15613 1 if scrolling succeeded
15615 0 if scrolling didn't find point.
15617 -1 if new fonts have been loaded so that we must interrupt
15618 redisplay, adjust glyph matrices, and try again. */
15620 enum
15622 SCROLLING_SUCCESS,
15623 SCROLLING_FAILED,
15624 SCROLLING_NEED_LARGER_MATRICES
15627 /* If scroll-conservatively is more than this, never recenter.
15629 If you change this, don't forget to update the doc string of
15630 `scroll-conservatively' and the Emacs manual. */
15631 #define SCROLL_LIMIT 100
15633 static int
15634 try_scrolling (Lisp_Object window, bool just_this_one_p,
15635 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15636 bool temp_scroll_step, bool last_line_misfit)
15638 struct window *w = XWINDOW (window);
15639 struct text_pos pos, startp;
15640 struct it it;
15641 int this_scroll_margin, scroll_max, rc, height;
15642 int dy = 0, amount_to_scroll = 0;
15643 bool scroll_down_p = false;
15644 int extra_scroll_margin_lines = last_line_misfit;
15645 Lisp_Object aggressive;
15646 /* We will never try scrolling more than this number of lines. */
15647 int scroll_limit = SCROLL_LIMIT;
15648 int frame_line_height = default_line_pixel_height (w);
15650 #ifdef GLYPH_DEBUG
15651 debug_method_add (w, "try_scrolling");
15652 #endif
15654 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15656 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15658 /* Force arg_scroll_conservatively to have a reasonable value, to
15659 avoid scrolling too far away with slow move_it_* functions. Note
15660 that the user can supply scroll-conservatively equal to
15661 `most-positive-fixnum', which can be larger than INT_MAX. */
15662 if (arg_scroll_conservatively > scroll_limit)
15664 arg_scroll_conservatively = scroll_limit + 1;
15665 scroll_max = scroll_limit * frame_line_height;
15667 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15668 /* Compute how much we should try to scroll maximally to bring
15669 point into view. */
15670 scroll_max = (max (scroll_step,
15671 max (arg_scroll_conservatively, temp_scroll_step))
15672 * frame_line_height);
15673 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15674 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15675 /* We're trying to scroll because of aggressive scrolling but no
15676 scroll_step is set. Choose an arbitrary one. */
15677 scroll_max = 10 * frame_line_height;
15678 else
15679 scroll_max = 0;
15681 too_near_end:
15683 /* Decide whether to scroll down. */
15684 if (PT > CHARPOS (startp))
15686 int scroll_margin_y;
15688 /* Compute the pixel ypos of the scroll margin, then move IT to
15689 either that ypos or PT, whichever comes first. */
15690 start_display (&it, w, startp);
15691 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15692 - this_scroll_margin
15693 - frame_line_height * extra_scroll_margin_lines;
15694 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15695 (MOVE_TO_POS | MOVE_TO_Y));
15697 if (PT > CHARPOS (it.current.pos))
15699 int y0 = line_bottom_y (&it);
15700 /* Compute how many pixels below window bottom to stop searching
15701 for PT. This avoids costly search for PT that is far away if
15702 the user limited scrolling by a small number of lines, but
15703 always finds PT if scroll_conservatively is set to a large
15704 number, such as most-positive-fixnum. */
15705 int slack = max (scroll_max, 10 * frame_line_height);
15706 int y_to_move = it.last_visible_y + slack;
15708 /* Compute the distance from the scroll margin to PT or to
15709 the scroll limit, whichever comes first. This should
15710 include the height of the cursor line, to make that line
15711 fully visible. */
15712 move_it_to (&it, PT, -1, y_to_move,
15713 -1, MOVE_TO_POS | MOVE_TO_Y);
15714 dy = line_bottom_y (&it) - y0;
15716 if (dy > scroll_max)
15717 return SCROLLING_FAILED;
15719 if (dy > 0)
15720 scroll_down_p = true;
15722 else if (PT == IT_CHARPOS (it)
15723 && IT_CHARPOS (it) < ZV
15724 && it.method == GET_FROM_STRING
15725 && arg_scroll_conservatively > scroll_limit
15726 && it.current_x == 0)
15728 enum move_it_result skip;
15729 int y1 = it.current_y;
15730 int vpos;
15732 /* A before-string that includes newlines and is displayed
15733 on the last visible screen line could fail us under
15734 scroll-conservatively > 100, because we will be unable to
15735 position the cursor on that last visible line. Try to
15736 recover by finding the first screen line that has some
15737 glyphs coming from the buffer text. */
15738 do {
15739 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15740 if (skip != MOVE_NEWLINE_OR_CR
15741 || IT_CHARPOS (it) != PT
15742 || it.method == GET_FROM_BUFFER)
15743 break;
15744 vpos = it.vpos;
15745 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15746 } while (it.vpos > vpos);
15748 dy = it.current_y - y1;
15750 if (dy > scroll_max)
15751 return SCROLLING_FAILED;
15753 if (dy > 0)
15754 scroll_down_p = true;
15758 if (scroll_down_p)
15760 /* Point is in or below the bottom scroll margin, so move the
15761 window start down. If scrolling conservatively, move it just
15762 enough down to make point visible. If scroll_step is set,
15763 move it down by scroll_step. */
15764 if (arg_scroll_conservatively)
15765 amount_to_scroll
15766 = min (max (dy, frame_line_height),
15767 frame_line_height * arg_scroll_conservatively);
15768 else if (scroll_step || temp_scroll_step)
15769 amount_to_scroll = scroll_max;
15770 else
15772 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15773 height = WINDOW_BOX_TEXT_HEIGHT (w);
15774 if (NUMBERP (aggressive))
15776 double float_amount = XFLOATINT (aggressive) * height;
15777 int aggressive_scroll = float_amount;
15778 if (aggressive_scroll == 0 && float_amount > 0)
15779 aggressive_scroll = 1;
15780 /* Don't let point enter the scroll margin near top of
15781 the window. This could happen if the value of
15782 scroll_up_aggressively is too large and there are
15783 non-zero margins, because scroll_up_aggressively
15784 means put point that fraction of window height
15785 _from_the_bottom_margin_. */
15786 if (aggressive_scroll + 2 * this_scroll_margin > height)
15787 aggressive_scroll = height - 2 * this_scroll_margin;
15788 amount_to_scroll = dy + aggressive_scroll;
15792 if (amount_to_scroll <= 0)
15793 return SCROLLING_FAILED;
15795 start_display (&it, w, startp);
15796 if (arg_scroll_conservatively <= scroll_limit)
15797 move_it_vertically (&it, amount_to_scroll);
15798 else
15800 /* Extra precision for users who set scroll-conservatively
15801 to a large number: make sure the amount we scroll
15802 the window start is never less than amount_to_scroll,
15803 which was computed as distance from window bottom to
15804 point. This matters when lines at window top and lines
15805 below window bottom have different height. */
15806 struct it it1;
15807 void *it1data = NULL;
15808 /* We use a temporary it1 because line_bottom_y can modify
15809 its argument, if it moves one line down; see there. */
15810 int start_y;
15812 SAVE_IT (it1, it, it1data);
15813 start_y = line_bottom_y (&it1);
15814 do {
15815 RESTORE_IT (&it, &it, it1data);
15816 move_it_by_lines (&it, 1);
15817 SAVE_IT (it1, it, it1data);
15818 } while (IT_CHARPOS (it) < ZV
15819 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15820 bidi_unshelve_cache (it1data, true);
15823 /* If STARTP is unchanged, move it down another screen line. */
15824 if (IT_CHARPOS (it) == CHARPOS (startp))
15825 move_it_by_lines (&it, 1);
15826 startp = it.current.pos;
15828 else
15830 struct text_pos scroll_margin_pos = startp;
15831 int y_offset = 0;
15833 /* See if point is inside the scroll margin at the top of the
15834 window. */
15835 if (this_scroll_margin)
15837 int y_start;
15839 start_display (&it, w, startp);
15840 y_start = it.current_y;
15841 move_it_vertically (&it, this_scroll_margin);
15842 scroll_margin_pos = it.current.pos;
15843 /* If we didn't move enough before hitting ZV, request
15844 additional amount of scroll, to move point out of the
15845 scroll margin. */
15846 if (IT_CHARPOS (it) == ZV
15847 && it.current_y - y_start < this_scroll_margin)
15848 y_offset = this_scroll_margin - (it.current_y - y_start);
15851 if (PT < CHARPOS (scroll_margin_pos))
15853 /* Point is in the scroll margin at the top of the window or
15854 above what is displayed in the window. */
15855 int y0, y_to_move;
15857 /* Compute the vertical distance from PT to the scroll
15858 margin position. Move as far as scroll_max allows, or
15859 one screenful, or 10 screen lines, whichever is largest.
15860 Give up if distance is greater than scroll_max or if we
15861 didn't reach the scroll margin position. */
15862 SET_TEXT_POS (pos, PT, PT_BYTE);
15863 start_display (&it, w, pos);
15864 y0 = it.current_y;
15865 y_to_move = max (it.last_visible_y,
15866 max (scroll_max, 10 * frame_line_height));
15867 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15868 y_to_move, -1,
15869 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15870 dy = it.current_y - y0;
15871 if (dy > scroll_max
15872 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15873 return SCROLLING_FAILED;
15875 /* Additional scroll for when ZV was too close to point. */
15876 dy += y_offset;
15878 /* Compute new window start. */
15879 start_display (&it, w, startp);
15881 if (arg_scroll_conservatively)
15882 amount_to_scroll = max (dy, frame_line_height
15883 * max (scroll_step, temp_scroll_step));
15884 else if (scroll_step || temp_scroll_step)
15885 amount_to_scroll = scroll_max;
15886 else
15888 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15889 height = WINDOW_BOX_TEXT_HEIGHT (w);
15890 if (NUMBERP (aggressive))
15892 double float_amount = XFLOATINT (aggressive) * height;
15893 int aggressive_scroll = float_amount;
15894 if (aggressive_scroll == 0 && float_amount > 0)
15895 aggressive_scroll = 1;
15896 /* Don't let point enter the scroll margin near
15897 bottom of the window, if the value of
15898 scroll_down_aggressively happens to be too
15899 large. */
15900 if (aggressive_scroll + 2 * this_scroll_margin > height)
15901 aggressive_scroll = height - 2 * this_scroll_margin;
15902 amount_to_scroll = dy + aggressive_scroll;
15906 if (amount_to_scroll <= 0)
15907 return SCROLLING_FAILED;
15909 move_it_vertically_backward (&it, amount_to_scroll);
15910 startp = it.current.pos;
15914 /* Run window scroll functions. */
15915 startp = run_window_scroll_functions (window, startp);
15917 /* Display the window. Give up if new fonts are loaded, or if point
15918 doesn't appear. */
15919 if (!try_window (window, startp, 0))
15920 rc = SCROLLING_NEED_LARGER_MATRICES;
15921 else if (w->cursor.vpos < 0)
15923 clear_glyph_matrix (w->desired_matrix);
15924 rc = SCROLLING_FAILED;
15926 else
15928 /* Maybe forget recorded base line for line number display. */
15929 if (!just_this_one_p
15930 || current_buffer->clip_changed
15931 || BEG_UNCHANGED < CHARPOS (startp))
15932 w->base_line_number = 0;
15934 /* If cursor ends up on a partially visible line,
15935 treat that as being off the bottom of the screen. */
15936 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15937 false)
15938 /* It's possible that the cursor is on the first line of the
15939 buffer, which is partially obscured due to a vscroll
15940 (Bug#7537). In that case, avoid looping forever. */
15941 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15943 clear_glyph_matrix (w->desired_matrix);
15944 ++extra_scroll_margin_lines;
15945 goto too_near_end;
15947 rc = SCROLLING_SUCCESS;
15950 return rc;
15954 /* Compute a suitable window start for window W if display of W starts
15955 on a continuation line. Value is true if a new window start
15956 was computed.
15958 The new window start will be computed, based on W's width, starting
15959 from the start of the continued line. It is the start of the
15960 screen line with the minimum distance from the old start W->start,
15961 which is still before point (otherwise point will definitely not
15962 be visible in the window). */
15964 static bool
15965 compute_window_start_on_continuation_line (struct window *w)
15967 struct text_pos pos, start_pos, pos_before_pt;
15968 bool window_start_changed_p = false;
15970 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15972 /* If window start is on a continuation line... Window start may be
15973 < BEGV in case there's invisible text at the start of the
15974 buffer (M-x rmail, for example). */
15975 if (CHARPOS (start_pos) > BEGV
15976 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15978 struct it it;
15979 struct glyph_row *row;
15981 /* Handle the case that the window start is out of range. */
15982 if (CHARPOS (start_pos) < BEGV)
15983 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15984 else if (CHARPOS (start_pos) > ZV)
15985 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15987 /* Find the start of the continued line. This should be fast
15988 because find_newline is fast (newline cache). */
15989 row = w->desired_matrix->rows + window_wants_header_line (w);
15990 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15991 row, DEFAULT_FACE_ID);
15992 reseat_at_previous_visible_line_start (&it);
15994 /* If the line start is "too far" away from the window start,
15995 say it takes too much time to compute a new window start.
15996 Also, give up if the line start is after point, as in that
15997 case point will not be visible with any window start we
15998 compute. */
15999 if (IT_CHARPOS (it) <= PT
16000 || (CHARPOS (start_pos) - IT_CHARPOS (it)
16001 /* PXW: Do we need upper bounds here? */
16002 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
16004 int min_distance, distance;
16006 /* Move forward by display lines to find the new window
16007 start. If window width was enlarged, the new start can
16008 be expected to be > the old start. If window width was
16009 decreased, the new window start will be < the old start.
16010 So, we're looking for the display line start with the
16011 minimum distance from the old window start. */
16012 pos_before_pt = pos = it.current.pos;
16013 min_distance = DISP_INFINITY;
16014 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
16015 distance < min_distance)
16017 min_distance = distance;
16018 if (CHARPOS (pos) <= PT)
16019 pos_before_pt = pos;
16020 pos = it.current.pos;
16021 if (it.line_wrap == WORD_WRAP)
16023 /* Under WORD_WRAP, move_it_by_lines is likely to
16024 overshoot and stop not at the first, but the
16025 second character from the left margin. So in
16026 that case, we need a more tight control on the X
16027 coordinate of the iterator than move_it_by_lines
16028 promises in its contract. The method is to first
16029 go to the last (rightmost) visible character of a
16030 line, then move to the leftmost character on the
16031 next line in a separate call. */
16032 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
16033 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16034 move_it_to (&it, ZV, 0,
16035 it.current_y + it.max_ascent + it.max_descent, -1,
16036 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16038 else
16039 move_it_by_lines (&it, 1);
16042 /* It makes very little sense to make the new window start
16043 after point, as point won't be visible. If that's what
16044 the loop above finds, fall back on the candidate before
16045 or at point that is closest to the old window start. */
16046 if (CHARPOS (pos) > PT)
16047 pos = pos_before_pt;
16049 /* Set the window start there. */
16050 SET_MARKER_FROM_TEXT_POS (w->start, pos);
16051 window_start_changed_p = true;
16055 return window_start_changed_p;
16059 /* Try cursor movement in case text has not changed in window WINDOW,
16060 with window start STARTP. Value is
16062 CURSOR_MOVEMENT_SUCCESS if successful
16064 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16066 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16067 display. *SCROLL_STEP is set to true, under certain circumstances, if
16068 we want to scroll as if scroll-step were set to 1. See the code.
16070 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16071 which case we have to abort this redisplay, and adjust matrices
16072 first. */
16074 enum
16076 CURSOR_MOVEMENT_SUCCESS,
16077 CURSOR_MOVEMENT_CANNOT_BE_USED,
16078 CURSOR_MOVEMENT_MUST_SCROLL,
16079 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16082 static int
16083 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16084 bool *scroll_step)
16086 struct window *w = XWINDOW (window);
16087 struct frame *f = XFRAME (w->frame);
16088 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16090 #ifdef GLYPH_DEBUG
16091 if (inhibit_try_cursor_movement)
16092 return rc;
16093 #endif
16095 /* Previously, there was a check for Lisp integer in the
16096 if-statement below. Now, this field is converted to
16097 ptrdiff_t, thus zero means invalid position in a buffer. */
16098 eassert (w->last_point > 0);
16099 /* Likewise there was a check whether window_end_vpos is nil or larger
16100 than the window. Now window_end_vpos is int and so never nil, but
16101 let's leave eassert to check whether it fits in the window. */
16102 eassert (!w->window_end_valid
16103 || w->window_end_vpos < w->current_matrix->nrows);
16105 /* Handle case where text has not changed, only point, and it has
16106 not moved off the frame. */
16107 if (/* Point may be in this window. */
16108 PT >= CHARPOS (startp)
16109 /* Selective display hasn't changed. */
16110 && !current_buffer->clip_changed
16111 /* Function force-mode-line-update is used to force a thorough
16112 redisplay. It sets either windows_or_buffers_changed or
16113 update_mode_lines. So don't take a shortcut here for these
16114 cases. */
16115 && !update_mode_lines
16116 && !windows_or_buffers_changed
16117 && !f->cursor_type_changed
16118 && NILP (Vshow_trailing_whitespace)
16119 /* When display-line-numbers is in relative mode, moving point
16120 requires to redraw the entire window. */
16121 && !EQ (Vdisplay_line_numbers, Qrelative)
16122 && !EQ (Vdisplay_line_numbers, Qvisual)
16123 /* When the current line number should be displayed in a
16124 distinct face, moving point cannot be handled in optimized
16125 way as below. */
16126 && !(!NILP (Vdisplay_line_numbers)
16127 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16128 Qline_number_current_line,
16129 w->frame)))
16130 /* This code is not used for mini-buffer for the sake of the case
16131 of redisplaying to replace an echo area message; since in
16132 that case the mini-buffer contents per se are usually
16133 unchanged. This code is of no real use in the mini-buffer
16134 since the handling of this_line_start_pos, etc., in redisplay
16135 handles the same cases. */
16136 && !EQ (window, minibuf_window)
16137 /* When overlay arrow is shown in current buffer, point movement
16138 is no longer "simple", as it typically causes the overlay
16139 arrow to move as well. */
16140 && !overlay_arrow_in_current_buffer_p ())
16142 int this_scroll_margin, top_scroll_margin;
16143 struct glyph_row *row = NULL;
16145 #ifdef GLYPH_DEBUG
16146 debug_method_add (w, "cursor movement");
16147 #endif
16149 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16151 top_scroll_margin = this_scroll_margin;
16152 if (window_wants_header_line (w))
16153 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16155 /* Start with the row the cursor was displayed during the last
16156 not paused redisplay. Give up if that row is not valid. */
16157 if (w->last_cursor_vpos < 0
16158 || w->last_cursor_vpos >= w->current_matrix->nrows)
16159 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16160 else
16162 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16163 if (row->mode_line_p)
16164 ++row;
16165 if (!row->enabled_p)
16166 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16169 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16171 bool scroll_p = false, must_scroll = false;
16172 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16174 if (PT > w->last_point)
16176 /* Point has moved forward. */
16177 while (MATRIX_ROW_END_CHARPOS (row) < PT
16178 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16180 eassert (row->enabled_p);
16181 ++row;
16184 /* If the end position of a row equals the start
16185 position of the next row, and PT is at that position,
16186 we would rather display cursor in the next line. */
16187 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16188 && MATRIX_ROW_END_CHARPOS (row) == PT
16189 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16190 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16191 && !cursor_row_p (row))
16192 ++row;
16194 /* If within the scroll margin, scroll. Note that
16195 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16196 the next line would be drawn, and that
16197 this_scroll_margin can be zero. */
16198 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16199 || PT > MATRIX_ROW_END_CHARPOS (row)
16200 /* Line is completely visible last line in window
16201 and PT is to be set in the next line. */
16202 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16203 && PT == MATRIX_ROW_END_CHARPOS (row)
16204 && !row->ends_at_zv_p
16205 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16206 scroll_p = true;
16208 else if (PT < w->last_point)
16210 /* Cursor has to be moved backward. Note that PT >=
16211 CHARPOS (startp) because of the outer if-statement. */
16212 while (!row->mode_line_p
16213 && (MATRIX_ROW_START_CHARPOS (row) > PT
16214 || (MATRIX_ROW_START_CHARPOS (row) == PT
16215 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16216 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16217 row > w->current_matrix->rows
16218 && (row-1)->ends_in_newline_from_string_p))))
16219 && (row->y > top_scroll_margin
16220 || CHARPOS (startp) == BEGV))
16222 eassert (row->enabled_p);
16223 --row;
16226 /* Consider the following case: Window starts at BEGV,
16227 there is invisible, intangible text at BEGV, so that
16228 display starts at some point START > BEGV. It can
16229 happen that we are called with PT somewhere between
16230 BEGV and START. Try to handle that case. */
16231 if (row < w->current_matrix->rows
16232 || row->mode_line_p)
16234 row = w->current_matrix->rows;
16235 if (row->mode_line_p)
16236 ++row;
16239 /* Due to newlines in overlay strings, we may have to
16240 skip forward over overlay strings. */
16241 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16242 && MATRIX_ROW_END_CHARPOS (row) == PT
16243 && !cursor_row_p (row))
16244 ++row;
16246 /* If within the scroll margin, scroll. */
16247 if (row->y < top_scroll_margin
16248 && CHARPOS (startp) != BEGV)
16249 scroll_p = true;
16251 else
16253 /* Cursor did not move. So don't scroll even if cursor line
16254 is partially visible, as it was so before. */
16255 rc = CURSOR_MOVEMENT_SUCCESS;
16258 if (PT < MATRIX_ROW_START_CHARPOS (row)
16259 || PT > MATRIX_ROW_END_CHARPOS (row))
16261 /* if PT is not in the glyph row, give up. */
16262 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16263 must_scroll = true;
16265 else if (rc != CURSOR_MOVEMENT_SUCCESS
16266 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16268 struct glyph_row *row1;
16270 /* If rows are bidi-reordered and point moved, back up
16271 until we find a row that does not belong to a
16272 continuation line. This is because we must consider
16273 all rows of a continued line as candidates for the
16274 new cursor positioning, since row start and end
16275 positions change non-linearly with vertical position
16276 in such rows. */
16277 /* FIXME: Revisit this when glyph ``spilling'' in
16278 continuation lines' rows is implemented for
16279 bidi-reordered rows. */
16280 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16281 MATRIX_ROW_CONTINUATION_LINE_P (row);
16282 --row)
16284 /* If we hit the beginning of the displayed portion
16285 without finding the first row of a continued
16286 line, give up. */
16287 if (row <= row1)
16289 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16290 break;
16292 eassert (row->enabled_p);
16295 if (must_scroll)
16297 else if (rc != CURSOR_MOVEMENT_SUCCESS
16298 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16299 /* Make sure this isn't a header line by any chance, since
16300 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16301 && !row->mode_line_p
16302 && make_cursor_line_fully_visible_p)
16304 if (PT == MATRIX_ROW_END_CHARPOS (row)
16305 && !row->ends_at_zv_p
16306 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16307 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16308 else if (row->height > window_box_height (w))
16310 /* If we end up in a partially visible line, let's
16311 make it fully visible, except when it's taller
16312 than the window, in which case we can't do much
16313 about it. */
16314 *scroll_step = true;
16315 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16317 else
16319 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16320 if (!cursor_row_fully_visible_p (w, false, true))
16321 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16322 else
16323 rc = CURSOR_MOVEMENT_SUCCESS;
16326 else if (scroll_p)
16327 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16328 else if (rc != CURSOR_MOVEMENT_SUCCESS
16329 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16331 /* With bidi-reordered rows, there could be more than
16332 one candidate row whose start and end positions
16333 occlude point. We need to let set_cursor_from_row
16334 find the best candidate. */
16335 /* FIXME: Revisit this when glyph ``spilling'' in
16336 continuation lines' rows is implemented for
16337 bidi-reordered rows. */
16338 bool rv = false;
16342 bool at_zv_p = false, exact_match_p = false;
16344 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16345 && PT <= MATRIX_ROW_END_CHARPOS (row)
16346 && cursor_row_p (row))
16347 rv |= set_cursor_from_row (w, row, w->current_matrix,
16348 0, 0, 0, 0);
16349 /* As soon as we've found the exact match for point,
16350 or the first suitable row whose ends_at_zv_p flag
16351 is set, we are done. */
16352 if (rv)
16354 at_zv_p = MATRIX_ROW (w->current_matrix,
16355 w->cursor.vpos)->ends_at_zv_p;
16356 if (!at_zv_p
16357 && w->cursor.hpos >= 0
16358 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16359 w->cursor.vpos))
16361 struct glyph_row *candidate =
16362 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16363 struct glyph *g =
16364 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16365 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16367 exact_match_p =
16368 (BUFFERP (g->object) && g->charpos == PT)
16369 || (NILP (g->object)
16370 && (g->charpos == PT
16371 || (g->charpos == 0 && endpos - 1 == PT)));
16373 if (at_zv_p || exact_match_p)
16375 rc = CURSOR_MOVEMENT_SUCCESS;
16376 break;
16379 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16380 break;
16381 ++row;
16383 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16384 || row->continued_p)
16385 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16386 || (MATRIX_ROW_START_CHARPOS (row) == PT
16387 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16388 /* If we didn't find any candidate rows, or exited the
16389 loop before all the candidates were examined, signal
16390 to the caller that this method failed. */
16391 if (rc != CURSOR_MOVEMENT_SUCCESS
16392 && !(rv
16393 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16394 && !row->continued_p))
16395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16396 else if (rv)
16397 rc = CURSOR_MOVEMENT_SUCCESS;
16399 else
16403 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16405 rc = CURSOR_MOVEMENT_SUCCESS;
16406 break;
16408 ++row;
16410 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16411 && MATRIX_ROW_START_CHARPOS (row) == PT
16412 && cursor_row_p (row));
16417 return rc;
16421 void
16422 set_vertical_scroll_bar (struct window *w)
16424 ptrdiff_t start, end, whole;
16426 /* Calculate the start and end positions for the current window.
16427 At some point, it would be nice to choose between scrollbars
16428 which reflect the whole buffer size, with special markers
16429 indicating narrowing, and scrollbars which reflect only the
16430 visible region.
16432 Note that mini-buffers sometimes aren't displaying any text. */
16433 if (!MINI_WINDOW_P (w)
16434 || (w == XWINDOW (minibuf_window)
16435 && NILP (echo_area_buffer[0])))
16437 struct buffer *buf = XBUFFER (w->contents);
16438 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16439 start = marker_position (w->start) - BUF_BEGV (buf);
16440 /* I don't think this is guaranteed to be right. For the
16441 moment, we'll pretend it is. */
16442 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16444 if (end < start)
16445 end = start;
16446 if (whole < (end - start))
16447 whole = end - start;
16449 else
16450 start = end = whole = 0;
16452 /* Indicate what this scroll bar ought to be displaying now. */
16453 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16454 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16455 (w, end - start, whole, start);
16459 void
16460 set_horizontal_scroll_bar (struct window *w)
16462 int start, end, whole, portion;
16464 if (!MINI_WINDOW_P (w)
16465 || (w == XWINDOW (minibuf_window)
16466 && NILP (echo_area_buffer[0])))
16468 struct buffer *b = XBUFFER (w->contents);
16469 struct buffer *old_buffer = NULL;
16470 struct it it;
16471 struct text_pos startp;
16473 if (b != current_buffer)
16475 old_buffer = current_buffer;
16476 set_buffer_internal (b);
16479 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16480 start_display (&it, w, startp);
16481 it.last_visible_x = INT_MAX;
16482 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16483 MOVE_TO_X | MOVE_TO_Y);
16484 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16485 window_box_height (w), -1,
16486 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16488 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16489 end = start + window_box_width (w, TEXT_AREA);
16490 portion = end - start;
16491 /* After enlarging a horizontally scrolled window such that it
16492 gets at least as wide as the text it contains, make sure that
16493 the thumb doesn't fill the entire scroll bar so we can still
16494 drag it back to see the entire text. */
16495 whole = max (whole, end);
16497 if (it.bidi_p)
16499 Lisp_Object pdir;
16501 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16502 if (EQ (pdir, Qright_to_left))
16504 start = whole - end;
16505 end = start + portion;
16509 if (old_buffer)
16510 set_buffer_internal (old_buffer);
16512 else
16513 start = end = whole = portion = 0;
16515 w->hscroll_whole = whole;
16517 /* Indicate what this scroll bar ought to be displaying now. */
16518 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16519 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16520 (w, portion, whole, start);
16524 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16525 selected_window is redisplayed.
16527 We can return without actually redisplaying the window if fonts has been
16528 changed on window's frame. In that case, redisplay_internal will retry.
16530 As one of the important parts of redisplaying a window, we need to
16531 decide whether the previous window-start position (stored in the
16532 window's w->start marker position) is still valid, and if it isn't,
16533 recompute it. Some details about that:
16535 . The previous window-start could be in a continuation line, in
16536 which case we need to recompute it when the window width
16537 changes. See compute_window_start_on_continuation_line and its
16538 call below.
16540 . The text that changed since last redisplay could include the
16541 previous window-start position. In that case, we try to salvage
16542 what we can from the current glyph matrix by calling
16543 try_scrolling, which see.
16545 . Some Emacs command could force us to use a specific window-start
16546 position by setting the window's force_start flag, or gently
16547 propose doing that by setting the window's optional_new_start
16548 flag. In these cases, we try using the specified start point if
16549 that succeeds (i.e. the window desired matrix is successfully
16550 recomputed, and point location is within the window). In case
16551 of optional_new_start, we first check if the specified start
16552 position is feasible, i.e. if it will allow point to be
16553 displayed in the window. If using the specified start point
16554 fails, e.g., if new fonts are needed to be loaded, we abort the
16555 redisplay cycle and leave it up to the next cycle to figure out
16556 things.
16558 . Note that the window's force_start flag is sometimes set by
16559 redisplay itself, when it decides that the previous window start
16560 point is fine and should be kept. Search for "goto force_start"
16561 below to see the details. Like the values of window-start
16562 specified outside of redisplay, these internally-deduced values
16563 are tested for feasibility, and ignored if found to be
16564 unfeasible.
16566 . Note that the function try_window, used to completely redisplay
16567 a window, accepts the window's start point as its argument.
16568 This is used several times in the redisplay code to control
16569 where the window start will be, according to user options such
16570 as scroll-conservatively, and also to ensure the screen line
16571 showing point will be fully (as opposed to partially) visible on
16572 display. */
16574 static void
16575 redisplay_window (Lisp_Object window, bool just_this_one_p)
16577 struct window *w = XWINDOW (window);
16578 struct frame *f = XFRAME (w->frame);
16579 struct buffer *buffer = XBUFFER (w->contents);
16580 struct buffer *old = current_buffer;
16581 struct text_pos lpoint, opoint, startp;
16582 bool update_mode_line;
16583 int tem;
16584 struct it it;
16585 /* Record it now because it's overwritten. */
16586 bool current_matrix_up_to_date_p = false;
16587 bool used_current_matrix_p = false;
16588 /* This is less strict than current_matrix_up_to_date_p.
16589 It indicates that the buffer contents and narrowing are unchanged. */
16590 bool buffer_unchanged_p = false;
16591 bool temp_scroll_step = false;
16592 ptrdiff_t count = SPECPDL_INDEX ();
16593 int rc;
16594 int centering_position = -1;
16595 bool last_line_misfit = false;
16596 ptrdiff_t beg_unchanged, end_unchanged;
16597 int frame_line_height, margin;
16598 bool use_desired_matrix;
16599 void *itdata = NULL;
16601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16602 opoint = lpoint;
16604 #ifdef GLYPH_DEBUG
16605 *w->desired_matrix->method = 0;
16606 #endif
16608 if (!just_this_one_p
16609 && REDISPLAY_SOME_P ()
16610 && !w->redisplay
16611 && !w->update_mode_line
16612 && !f->face_change
16613 && !f->redisplay
16614 && !buffer->text->redisplay
16615 && BUF_PT (buffer) == w->last_point)
16616 return;
16618 /* Make sure that both W's markers are valid. */
16619 eassert (XMARKER (w->start)->buffer == buffer);
16620 eassert (XMARKER (w->pointm)->buffer == buffer);
16622 reconsider_clip_changes (w);
16623 frame_line_height = default_line_pixel_height (w);
16624 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16627 /* Has the mode line to be updated? */
16628 update_mode_line = (w->update_mode_line
16629 || update_mode_lines
16630 || buffer->clip_changed
16631 || buffer->prevent_redisplay_optimizations_p);
16633 if (!just_this_one_p)
16634 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16635 cleverly elsewhere. */
16636 w->must_be_updated_p = true;
16638 if (MINI_WINDOW_P (w))
16640 if (w == XWINDOW (echo_area_window)
16641 && !NILP (echo_area_buffer[0]))
16643 if (update_mode_line)
16644 /* We may have to update a tty frame's menu bar or a
16645 tool-bar. Example `M-x C-h C-h C-g'. */
16646 goto finish_menu_bars;
16647 else
16648 /* We've already displayed the echo area glyphs in this window. */
16649 goto finish_scroll_bars;
16651 else if ((w != XWINDOW (minibuf_window)
16652 || minibuf_level == 0)
16653 /* When buffer is nonempty, redisplay window normally. */
16654 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16655 /* Quail displays non-mini buffers in minibuffer window.
16656 In that case, redisplay the window normally. */
16657 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16659 /* W is a mini-buffer window, but it's not active, so clear
16660 it. */
16661 int yb = window_text_bottom_y (w);
16662 struct glyph_row *row;
16663 int y;
16665 for (y = 0, row = w->desired_matrix->rows;
16666 y < yb;
16667 y += row->height, ++row)
16668 blank_row (w, row, y);
16669 goto finish_scroll_bars;
16672 clear_glyph_matrix (w->desired_matrix);
16675 /* Otherwise set up data on this window; select its buffer and point
16676 value. */
16677 /* Really select the buffer, for the sake of buffer-local
16678 variables. */
16679 set_buffer_internal_1 (XBUFFER (w->contents));
16681 current_matrix_up_to_date_p
16682 = (w->window_end_valid
16683 && !current_buffer->clip_changed
16684 && !current_buffer->prevent_redisplay_optimizations_p
16685 && !window_outdated (w)
16686 && !hscrolling_current_line_p (w));
16688 beg_unchanged = BEG_UNCHANGED;
16689 end_unchanged = END_UNCHANGED;
16691 SET_TEXT_POS (opoint, PT, PT_BYTE);
16693 specbind (Qinhibit_point_motion_hooks, Qt);
16695 buffer_unchanged_p
16696 = (w->window_end_valid
16697 && !current_buffer->clip_changed
16698 && !window_outdated (w));
16700 /* When windows_or_buffers_changed is non-zero, we can't rely
16701 on the window end being valid, so set it to zero there. */
16702 if (windows_or_buffers_changed)
16704 /* If window starts on a continuation line, maybe adjust the
16705 window start in case the window's width changed. */
16706 if (XMARKER (w->start)->buffer == current_buffer)
16707 compute_window_start_on_continuation_line (w);
16709 w->window_end_valid = false;
16710 /* If so, we also can't rely on current matrix
16711 and should not fool try_cursor_movement below. */
16712 current_matrix_up_to_date_p = false;
16715 /* Some sanity checks. */
16716 CHECK_WINDOW_END (w);
16717 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16718 emacs_abort ();
16719 if (BYTEPOS (opoint) < CHARPOS (opoint))
16720 emacs_abort ();
16722 if (mode_line_update_needed (w))
16723 update_mode_line = true;
16725 /* Point refers normally to the selected window. For any other
16726 window, set up appropriate value. */
16727 if (!EQ (window, selected_window))
16729 ptrdiff_t new_pt = marker_position (w->pointm);
16730 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16732 if (new_pt < BEGV)
16734 new_pt = BEGV;
16735 new_pt_byte = BEGV_BYTE;
16736 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16738 else if (new_pt > (ZV - 1))
16740 new_pt = ZV;
16741 new_pt_byte = ZV_BYTE;
16742 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16745 /* We don't use SET_PT so that the point-motion hooks don't run. */
16746 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16749 /* If any of the character widths specified in the display table
16750 have changed, invalidate the width run cache. It's true that
16751 this may be a bit late to catch such changes, but the rest of
16752 redisplay goes (non-fatally) haywire when the display table is
16753 changed, so why should we worry about doing any better? */
16754 if (current_buffer->width_run_cache
16755 || (current_buffer->base_buffer
16756 && current_buffer->base_buffer->width_run_cache))
16758 struct Lisp_Char_Table *disptab = buffer_display_table ();
16760 if (! disptab_matches_widthtab
16761 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16763 struct buffer *buf = current_buffer;
16765 if (buf->base_buffer)
16766 buf = buf->base_buffer;
16767 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16768 recompute_width_table (current_buffer, disptab);
16772 /* If window-start is screwed up, choose a new one. */
16773 if (XMARKER (w->start)->buffer != current_buffer)
16774 goto recenter;
16776 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16778 /* If someone specified a new starting point but did not insist,
16779 check whether it can be used. */
16780 if ((w->optional_new_start || window_frozen_p (w))
16781 && CHARPOS (startp) >= BEGV
16782 && CHARPOS (startp) <= ZV)
16784 ptrdiff_t it_charpos;
16786 w->optional_new_start = false;
16787 start_display (&it, w, startp);
16788 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16789 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16790 /* Record IT's position now, since line_bottom_y might change
16791 that. */
16792 it_charpos = IT_CHARPOS (it);
16793 /* Make sure we set the force_start flag only if the cursor row
16794 will be fully visible. Otherwise, the code under force_start
16795 label below will try to move point back into view, which is
16796 not what the code which sets optional_new_start wants. */
16797 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16798 && !w->force_start)
16800 if (it_charpos == PT)
16801 w->force_start = true;
16802 /* IT may overshoot PT if text at PT is invisible. */
16803 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16804 w->force_start = true;
16805 #ifdef GLYPH_DEBUG
16806 if (w->force_start)
16808 if (window_frozen_p (w))
16809 debug_method_add (w, "set force_start from frozen window start");
16810 else
16811 debug_method_add (w, "set force_start from optional_new_start");
16813 #endif
16817 force_start:
16819 /* Handle case where place to start displaying has been specified,
16820 unless the specified location is outside the accessible range. */
16821 if (w->force_start)
16823 /* We set this later on if we have to adjust point. */
16824 int new_vpos = -1;
16826 w->force_start = false;
16827 w->vscroll = 0;
16828 w->window_end_valid = false;
16830 /* Forget any recorded base line for line number display. */
16831 if (!buffer_unchanged_p)
16832 w->base_line_number = 0;
16834 /* Redisplay the mode line. Select the buffer properly for that.
16835 Also, run the hook window-scroll-functions
16836 because we have scrolled. */
16837 /* Note, we do this after clearing force_start because
16838 if there's an error, it is better to forget about force_start
16839 than to get into an infinite loop calling the hook functions
16840 and having them get more errors. */
16841 if (!update_mode_line
16842 || ! NILP (Vwindow_scroll_functions))
16844 update_mode_line = true;
16845 w->update_mode_line = true;
16846 startp = run_window_scroll_functions (window, startp);
16849 if (CHARPOS (startp) < BEGV)
16850 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16851 else if (CHARPOS (startp) > ZV)
16852 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16854 /* Redisplay, then check if cursor has been set during the
16855 redisplay. Give up if new fonts were loaded. */
16856 /* We used to issue a CHECK_MARGINS argument to try_window here,
16857 but this causes scrolling to fail when point begins inside
16858 the scroll margin (bug#148) -- cyd */
16859 if (!try_window (window, startp, 0))
16861 w->force_start = true;
16862 clear_glyph_matrix (w->desired_matrix);
16863 goto need_larger_matrices;
16866 if (w->cursor.vpos < 0)
16868 /* If point does not appear, try to move point so it does
16869 appear. The desired matrix has been built above, so we
16870 can use it here. First see if point is in invisible
16871 text, and if so, move it to the first visible buffer
16872 position past that. */
16873 struct glyph_row *r = NULL;
16874 Lisp_Object invprop =
16875 get_char_property_and_overlay (make_number (PT), Qinvisible,
16876 Qnil, NULL);
16878 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16880 ptrdiff_t alt_pt;
16881 Lisp_Object invprop_end =
16882 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16883 Qnil, Qnil);
16885 if (NATNUMP (invprop_end))
16886 alt_pt = XFASTINT (invprop_end);
16887 else
16888 alt_pt = ZV;
16889 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16890 NULL, 0);
16892 if (r)
16893 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16894 else /* Give up and just move to the middle of the window. */
16895 new_vpos = window_box_height (w) / 2;
16898 if (!cursor_row_fully_visible_p (w, false, false))
16900 /* Point does appear, but on a line partly visible at end of window.
16901 Move it back to a fully-visible line. */
16902 new_vpos = window_box_height (w);
16903 /* But if window_box_height suggests a Y coordinate that is
16904 not less than we already have, that line will clearly not
16905 be fully visible, so give up and scroll the display.
16906 This can happen when the default face uses a font whose
16907 dimensions are different from the frame's default
16908 font. */
16909 if (new_vpos >= w->cursor.y)
16911 w->cursor.vpos = -1;
16912 clear_glyph_matrix (w->desired_matrix);
16913 goto try_to_scroll;
16916 else if (w->cursor.vpos >= 0)
16918 /* Some people insist on not letting point enter the scroll
16919 margin, even though this part handles windows that didn't
16920 scroll at all. */
16921 int pixel_margin = margin * frame_line_height;
16922 bool header_line = window_wants_header_line (w);
16924 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16925 below, which finds the row to move point to, advances by
16926 the Y coordinate of the _next_ row, see the definition of
16927 MATRIX_ROW_BOTTOM_Y. */
16928 if (w->cursor.vpos < margin + header_line)
16930 w->cursor.vpos = -1;
16931 clear_glyph_matrix (w->desired_matrix);
16932 goto try_to_scroll;
16934 else
16936 int window_height = window_box_height (w);
16938 if (header_line)
16939 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16940 if (w->cursor.y >= window_height - pixel_margin)
16942 w->cursor.vpos = -1;
16943 clear_glyph_matrix (w->desired_matrix);
16944 goto try_to_scroll;
16949 /* If we need to move point for either of the above reasons,
16950 now actually do it. */
16951 if (new_vpos >= 0)
16953 struct glyph_row *row;
16955 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16956 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16957 ++row;
16959 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16960 MATRIX_ROW_START_BYTEPOS (row));
16962 if (w != XWINDOW (selected_window))
16963 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16964 else if (current_buffer == old)
16965 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16967 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16969 /* Re-run pre-redisplay-function so it can update the region
16970 according to the new position of point. */
16971 /* Other than the cursor, w's redisplay is done so we can set its
16972 redisplay to false. Also the buffer's redisplay can be set to
16973 false, since propagate_buffer_redisplay should have already
16974 propagated its info to `w' anyway. */
16975 w->redisplay = false;
16976 XBUFFER (w->contents)->text->redisplay = false;
16977 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16979 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16980 || ((EQ (Vdisplay_line_numbers, Qrelative)
16981 || EQ (Vdisplay_line_numbers, Qvisual))
16982 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16984 /* Either pre-redisplay-function made changes (e.g. move
16985 the region), or we moved point in a window that is
16986 under display-line-numbers = relative mode. We need
16987 another round of redisplay. */
16988 clear_glyph_matrix (w->desired_matrix);
16989 if (!try_window (window, startp, 0))
16990 goto need_larger_matrices;
16993 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16995 clear_glyph_matrix (w->desired_matrix);
16996 goto try_to_scroll;
16999 #ifdef GLYPH_DEBUG
17000 debug_method_add (w, "forced window start");
17001 #endif
17002 goto done;
17005 /* Handle case where text has not changed, only point, and it has
17006 not moved off the frame, and we are not retrying after hscroll.
17007 (current_matrix_up_to_date_p is true when retrying.) */
17008 if (current_matrix_up_to_date_p
17009 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
17010 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
17012 switch (rc)
17014 case CURSOR_MOVEMENT_SUCCESS:
17015 used_current_matrix_p = true;
17016 goto done;
17018 case CURSOR_MOVEMENT_MUST_SCROLL:
17019 goto try_to_scroll;
17021 default:
17022 emacs_abort ();
17025 /* If current starting point was originally the beginning of a line
17026 but no longer is, find a new starting point. */
17027 else if (w->start_at_line_beg
17028 && !(CHARPOS (startp) <= BEGV
17029 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
17031 #ifdef GLYPH_DEBUG
17032 debug_method_add (w, "recenter 1");
17033 #endif
17034 goto recenter;
17037 /* Try scrolling with try_window_id. Value is > 0 if update has
17038 been done, it is -1 if we know that the same window start will
17039 not work. It is 0 if unsuccessful for some other reason. */
17040 else if ((tem = try_window_id (w)) != 0)
17042 #ifdef GLYPH_DEBUG
17043 debug_method_add (w, "try_window_id %d", tem);
17044 #endif
17046 if (f->fonts_changed)
17047 goto need_larger_matrices;
17048 if (tem > 0)
17049 goto done;
17051 /* Otherwise try_window_id has returned -1 which means that we
17052 don't want the alternative below this comment to execute. */
17054 else if (CHARPOS (startp) >= BEGV
17055 && CHARPOS (startp) <= ZV
17056 && PT >= CHARPOS (startp)
17057 && (CHARPOS (startp) < ZV
17058 /* Avoid starting at end of buffer. */
17059 || CHARPOS (startp) == BEGV
17060 || !window_outdated (w)))
17062 int d1, d2, d5, d6;
17063 int rtop, rbot;
17065 /* If first window line is a continuation line, and window start
17066 is inside the modified region, but the first change is before
17067 current window start, we must select a new window start.
17069 However, if this is the result of a down-mouse event (e.g. by
17070 extending the mouse-drag-overlay), we don't want to select a
17071 new window start, since that would change the position under
17072 the mouse, resulting in an unwanted mouse-movement rather
17073 than a simple mouse-click. */
17074 if (!w->start_at_line_beg
17075 && NILP (do_mouse_tracking)
17076 && CHARPOS (startp) > BEGV
17077 && CHARPOS (startp) > BEG + beg_unchanged
17078 && CHARPOS (startp) <= Z - end_unchanged
17079 /* Even if w->start_at_line_beg is nil, a new window may
17080 start at a line_beg, since that's how set_buffer_window
17081 sets it. So, we need to check the return value of
17082 compute_window_start_on_continuation_line. (See also
17083 bug#197). */
17084 && XMARKER (w->start)->buffer == current_buffer
17085 && compute_window_start_on_continuation_line (w)
17086 /* It doesn't make sense to force the window start like we
17087 do at label force_start if it is already known that point
17088 will not be fully visible in the resulting window, because
17089 doing so will move point from its correct position
17090 instead of scrolling the window to bring point into view.
17091 See bug#9324. */
17092 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17093 /* A very tall row could need more than the window height,
17094 in which case we accept that it is partially visible. */
17095 && (rtop != 0) == (rbot != 0))
17097 w->force_start = true;
17098 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17099 #ifdef GLYPH_DEBUG
17100 debug_method_add (w, "recomputed window start in continuation line");
17101 #endif
17102 goto force_start;
17105 #ifdef GLYPH_DEBUG
17106 debug_method_add (w, "same window start");
17107 #endif
17109 /* Try to redisplay starting at same place as before.
17110 If point has not moved off frame, accept the results. */
17111 if (!current_matrix_up_to_date_p
17112 /* Don't use try_window_reusing_current_matrix in this case
17113 because a window scroll function can have changed the
17114 buffer. */
17115 || !NILP (Vwindow_scroll_functions)
17116 || MINI_WINDOW_P (w)
17117 || !(used_current_matrix_p
17118 = try_window_reusing_current_matrix (w)))
17120 IF_DEBUG (debug_method_add (w, "1"));
17121 clear_glyph_matrix (w->desired_matrix);
17122 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17123 /* -1 means we need to scroll.
17124 0 means we need new matrices, but fonts_changed
17125 is set in that case, so we will detect it below. */
17126 goto try_to_scroll;
17129 if (f->fonts_changed)
17130 goto need_larger_matrices;
17132 if (w->cursor.vpos >= 0)
17134 if (!just_this_one_p
17135 || current_buffer->clip_changed
17136 || BEG_UNCHANGED < CHARPOS (startp))
17137 /* Forget any recorded base line for line number display. */
17138 w->base_line_number = 0;
17140 if (!cursor_row_fully_visible_p (w, true, false))
17142 clear_glyph_matrix (w->desired_matrix);
17143 last_line_misfit = true;
17145 /* Drop through and scroll. */
17146 else
17147 goto done;
17149 else
17150 clear_glyph_matrix (w->desired_matrix);
17153 try_to_scroll:
17155 /* Redisplay the mode line. Select the buffer properly for that. */
17156 if (!update_mode_line)
17158 update_mode_line = true;
17159 w->update_mode_line = true;
17162 /* Try to scroll by specified few lines. */
17163 if ((scroll_conservatively
17164 || emacs_scroll_step
17165 || temp_scroll_step
17166 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17167 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17168 && CHARPOS (startp) >= BEGV
17169 && CHARPOS (startp) <= ZV)
17171 /* The function returns -1 if new fonts were loaded, 1 if
17172 successful, 0 if not successful. */
17173 int ss = try_scrolling (window, just_this_one_p,
17174 scroll_conservatively,
17175 emacs_scroll_step,
17176 temp_scroll_step, last_line_misfit);
17177 switch (ss)
17179 case SCROLLING_SUCCESS:
17180 goto done;
17182 case SCROLLING_NEED_LARGER_MATRICES:
17183 goto need_larger_matrices;
17185 case SCROLLING_FAILED:
17186 break;
17188 default:
17189 emacs_abort ();
17193 /* Finally, just choose a place to start which positions point
17194 according to user preferences. */
17196 recenter:
17198 #ifdef GLYPH_DEBUG
17199 debug_method_add (w, "recenter");
17200 #endif
17202 /* Forget any previously recorded base line for line number display. */
17203 if (!buffer_unchanged_p)
17204 w->base_line_number = 0;
17206 /* Determine the window start relative to point. */
17207 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17208 it.current_y = it.last_visible_y;
17209 if (centering_position < 0)
17211 ptrdiff_t margin_pos = CHARPOS (startp);
17212 Lisp_Object aggressive;
17213 bool scrolling_up;
17215 /* If there is a scroll margin at the top of the window, find
17216 its character position. */
17217 if (margin
17218 /* Cannot call start_display if startp is not in the
17219 accessible region of the buffer. This can happen when we
17220 have just switched to a different buffer and/or changed
17221 its restriction. In that case, startp is initialized to
17222 the character position 1 (BEGV) because we did not yet
17223 have chance to display the buffer even once. */
17224 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17226 struct it it1;
17227 void *it1data = NULL;
17229 SAVE_IT (it1, it, it1data);
17230 start_display (&it1, w, startp);
17231 move_it_vertically (&it1, margin * frame_line_height);
17232 margin_pos = IT_CHARPOS (it1);
17233 RESTORE_IT (&it, &it, it1data);
17235 scrolling_up = PT > margin_pos;
17236 aggressive =
17237 scrolling_up
17238 ? BVAR (current_buffer, scroll_up_aggressively)
17239 : BVAR (current_buffer, scroll_down_aggressively);
17241 if (!MINI_WINDOW_P (w)
17242 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17244 int pt_offset = 0;
17246 /* Setting scroll-conservatively overrides
17247 scroll-*-aggressively. */
17248 if (!scroll_conservatively && NUMBERP (aggressive))
17250 double float_amount = XFLOATINT (aggressive);
17252 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17253 if (pt_offset == 0 && float_amount > 0)
17254 pt_offset = 1;
17255 if (pt_offset && margin > 0)
17256 margin -= 1;
17258 /* Compute how much to move the window start backward from
17259 point so that point will be displayed where the user
17260 wants it. */
17261 if (scrolling_up)
17263 centering_position = it.last_visible_y;
17264 if (pt_offset)
17265 centering_position -= pt_offset;
17266 centering_position -=
17267 (frame_line_height * (1 + margin + last_line_misfit)
17268 + WINDOW_HEADER_LINE_HEIGHT (w));
17269 /* Don't let point enter the scroll margin near top of
17270 the window. */
17271 if (centering_position < margin * frame_line_height)
17272 centering_position = margin * frame_line_height;
17274 else
17275 centering_position = margin * frame_line_height + pt_offset;
17277 else
17278 /* Set the window start half the height of the window backward
17279 from point. */
17280 centering_position = window_box_height (w) / 2;
17282 move_it_vertically_backward (&it, centering_position);
17284 eassert (IT_CHARPOS (it) >= BEGV);
17286 /* The function move_it_vertically_backward may move over more
17287 than the specified y-distance. If it->w is small, e.g. a
17288 mini-buffer window, we may end up in front of the window's
17289 display area. Start displaying at the start of the line
17290 containing PT in this case. */
17291 if (it.current_y <= 0)
17293 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17294 move_it_vertically_backward (&it, 0);
17295 it.current_y = 0;
17298 it.current_x = it.hpos = 0;
17300 /* Set the window start position here explicitly, to avoid an
17301 infinite loop in case the functions in window-scroll-functions
17302 get errors. */
17303 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17305 /* Run scroll hooks. */
17306 startp = run_window_scroll_functions (window, it.current.pos);
17308 /* We invoke try_window and try_window_reusing_current_matrix below,
17309 and they manipulate the bidi cache. Save and restore the cache
17310 state of our iterator, so we could continue using it after that. */
17311 itdata = bidi_shelve_cache ();
17313 /* Redisplay the window. */
17314 use_desired_matrix = false;
17315 if (!current_matrix_up_to_date_p
17316 || windows_or_buffers_changed
17317 || f->cursor_type_changed
17318 /* Don't use try_window_reusing_current_matrix in this case
17319 because it can have changed the buffer. */
17320 || !NILP (Vwindow_scroll_functions)
17321 || !just_this_one_p
17322 || MINI_WINDOW_P (w)
17323 || !(used_current_matrix_p
17324 = try_window_reusing_current_matrix (w)))
17325 use_desired_matrix = (try_window (window, startp, 0) == 1);
17327 bidi_unshelve_cache (itdata, false);
17329 /* If new fonts have been loaded (due to fontsets), give up. We
17330 have to start a new redisplay since we need to re-adjust glyph
17331 matrices. */
17332 if (f->fonts_changed)
17333 goto need_larger_matrices;
17335 /* If cursor did not appear assume that the middle of the window is
17336 in the first line of the window. Do it again with the next line.
17337 (Imagine a window of height 100, displaying two lines of height
17338 60. Moving back 50 from it->last_visible_y will end in the first
17339 line.) */
17340 if (w->cursor.vpos < 0)
17342 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17344 clear_glyph_matrix (w->desired_matrix);
17345 move_it_by_lines (&it, 1);
17346 try_window (window, it.current.pos, 0);
17348 else if (PT < IT_CHARPOS (it))
17350 clear_glyph_matrix (w->desired_matrix);
17351 move_it_by_lines (&it, -1);
17352 try_window (window, it.current.pos, 0);
17354 else if (scroll_conservatively > SCROLL_LIMIT
17355 && (it.method == GET_FROM_STRING
17356 || overlay_touches_p (IT_CHARPOS (it)))
17357 && IT_CHARPOS (it) < ZV)
17359 /* If the window starts with a before-string that spans more
17360 than one screen line, using that position to display the
17361 window might fail to bring point into the view, because
17362 start_display will always start by displaying the string,
17363 whereas the code above determines where to set w->start
17364 by the buffer position of the place where it takes screen
17365 coordinates. Try to recover by finding the next screen
17366 line that displays buffer text. */
17367 ptrdiff_t pos0 = IT_CHARPOS (it);
17369 clear_glyph_matrix (w->desired_matrix);
17370 do {
17371 move_it_by_lines (&it, 1);
17372 } while (IT_CHARPOS (it) == pos0);
17373 try_window (window, it.current.pos, 0);
17375 else
17377 /* Not much we can do about it. */
17381 /* Consider the following case: Window starts at BEGV, there is
17382 invisible, intangible text at BEGV, so that display starts at
17383 some point START > BEGV. It can happen that we are called with
17384 PT somewhere between BEGV and START. Try to handle that case,
17385 and similar ones. */
17386 if (w->cursor.vpos < 0)
17388 /* Prefer the desired matrix to the current matrix, if possible,
17389 in the fallback calculations below. This is because using
17390 the current matrix might completely goof, e.g. if its first
17391 row is after point. */
17392 struct glyph_matrix *matrix =
17393 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17394 /* First, try locating the proper glyph row for PT. */
17395 struct glyph_row *row =
17396 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17398 /* Sometimes point is at the beginning of invisible text that is
17399 before the 1st character displayed in the row. In that case,
17400 row_containing_pos fails to find the row, because no glyphs
17401 with appropriate buffer positions are present in the row.
17402 Therefore, we next try to find the row which shows the 1st
17403 position after the invisible text. */
17404 if (!row)
17406 Lisp_Object val =
17407 get_char_property_and_overlay (make_number (PT), Qinvisible,
17408 Qnil, NULL);
17410 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17412 ptrdiff_t alt_pos;
17413 Lisp_Object invis_end =
17414 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17415 Qnil, Qnil);
17417 if (NATNUMP (invis_end))
17418 alt_pos = XFASTINT (invis_end);
17419 else
17420 alt_pos = ZV;
17421 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17424 /* Finally, fall back on the first row of the window after the
17425 header line (if any). This is slightly better than not
17426 displaying the cursor at all. */
17427 if (!row)
17429 row = matrix->rows;
17430 if (row->mode_line_p)
17431 ++row;
17433 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17436 if (!cursor_row_fully_visible_p (w, false, false))
17438 /* If vscroll is enabled, disable it and try again. */
17439 if (w->vscroll)
17441 w->vscroll = 0;
17442 clear_glyph_matrix (w->desired_matrix);
17443 goto recenter;
17446 /* Users who set scroll-conservatively to a large number want
17447 point just above/below the scroll margin. If we ended up
17448 with point's row partially visible, move the window start to
17449 make that row fully visible and out of the margin. */
17450 if (scroll_conservatively > SCROLL_LIMIT)
17452 int window_total_lines
17453 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17454 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17456 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17457 clear_glyph_matrix (w->desired_matrix);
17458 if (1 == try_window (window, it.current.pos,
17459 TRY_WINDOW_CHECK_MARGINS))
17460 goto done;
17463 /* If centering point failed to make the whole line visible,
17464 put point at the top instead. That has to make the whole line
17465 visible, if it can be done. */
17466 if (centering_position == 0)
17467 goto done;
17469 clear_glyph_matrix (w->desired_matrix);
17470 centering_position = 0;
17471 goto recenter;
17474 done:
17476 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17477 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17478 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17480 /* Display the mode line, if we must. */
17481 if ((update_mode_line
17482 /* If window not full width, must redo its mode line
17483 if (a) the window to its side is being redone and
17484 (b) we do a frame-based redisplay. This is a consequence
17485 of how inverted lines are drawn in frame-based redisplay. */
17486 || (!just_this_one_p
17487 && !FRAME_WINDOW_P (f)
17488 && !WINDOW_FULL_WIDTH_P (w))
17489 /* Line number to display. */
17490 || w->base_line_pos > 0
17491 /* Column number is displayed and different from the one displayed. */
17492 || (w->column_number_displayed != -1
17493 && (w->column_number_displayed != current_column ())))
17494 /* This means that the window has a mode line. */
17495 && (window_wants_mode_line (w)
17496 || window_wants_header_line (w)))
17499 display_mode_lines (w);
17501 /* If mode line height has changed, arrange for a thorough
17502 immediate redisplay using the correct mode line height. */
17503 if (window_wants_mode_line (w)
17504 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17506 f->fonts_changed = true;
17507 w->mode_line_height = -1;
17508 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17509 = DESIRED_MODE_LINE_HEIGHT (w);
17512 /* If header line height has changed, arrange for a thorough
17513 immediate redisplay using the correct header line height. */
17514 if (window_wants_header_line (w)
17515 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17517 f->fonts_changed = true;
17518 w->header_line_height = -1;
17519 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17520 = DESIRED_HEADER_LINE_HEIGHT (w);
17523 if (f->fonts_changed)
17524 goto need_larger_matrices;
17527 if (!line_number_displayed && w->base_line_pos != -1)
17529 w->base_line_pos = 0;
17530 w->base_line_number = 0;
17533 finish_menu_bars:
17535 /* When we reach a frame's selected window, redo the frame's menu
17536 bar and the frame's title. */
17537 if (update_mode_line
17538 && EQ (FRAME_SELECTED_WINDOW (f), window))
17540 bool redisplay_menu_p;
17542 if (FRAME_WINDOW_P (f))
17544 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17545 || defined (HAVE_NS) || defined (USE_GTK)
17546 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17547 #else
17548 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17549 #endif
17551 else
17552 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17554 if (redisplay_menu_p)
17555 display_menu_bar (w);
17557 #ifdef HAVE_WINDOW_SYSTEM
17558 if (FRAME_WINDOW_P (f))
17560 #if defined (USE_GTK) || defined (HAVE_NS)
17561 if (FRAME_EXTERNAL_TOOL_BAR (f))
17562 redisplay_tool_bar (f);
17563 #else
17564 if (WINDOWP (f->tool_bar_window)
17565 && (FRAME_TOOL_BAR_LINES (f) > 0
17566 || !NILP (Vauto_resize_tool_bars))
17567 && redisplay_tool_bar (f))
17568 ignore_mouse_drag_p = true;
17569 #endif
17571 x_consider_frame_title (w->frame);
17572 #endif
17575 #ifdef HAVE_WINDOW_SYSTEM
17576 if (FRAME_WINDOW_P (f)
17577 && update_window_fringes (w, (just_this_one_p
17578 || (!used_current_matrix_p && !overlay_arrow_seen)
17579 || w->pseudo_window_p)))
17581 update_begin (f);
17582 block_input ();
17583 if (draw_window_fringes (w, true))
17585 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17586 x_draw_right_divider (w);
17587 else
17588 x_draw_vertical_border (w);
17590 unblock_input ();
17591 update_end (f);
17594 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17595 x_draw_bottom_divider (w);
17596 #endif /* HAVE_WINDOW_SYSTEM */
17598 /* We go to this label, with fonts_changed set, if it is
17599 necessary to try again using larger glyph matrices.
17600 We have to redeem the scroll bar even in this case,
17601 because the loop in redisplay_internal expects that. */
17602 need_larger_matrices:
17604 finish_scroll_bars:
17606 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17608 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17609 /* Set the thumb's position and size. */
17610 set_vertical_scroll_bar (w);
17612 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17613 /* Set the thumb's position and size. */
17614 set_horizontal_scroll_bar (w);
17616 /* Note that we actually used the scroll bar attached to this
17617 window, so it shouldn't be deleted at the end of redisplay. */
17618 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17619 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17622 /* Restore current_buffer and value of point in it. The window
17623 update may have changed the buffer, so first make sure `opoint'
17624 is still valid (Bug#6177). */
17625 if (CHARPOS (opoint) < BEGV)
17626 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17627 else if (CHARPOS (opoint) > ZV)
17628 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17629 else
17630 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17632 set_buffer_internal_1 (old);
17633 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17634 shorter. This can be caused by log truncation in *Messages*. */
17635 if (CHARPOS (lpoint) <= ZV)
17636 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17638 unbind_to (count, Qnil);
17642 /* Build the complete desired matrix of WINDOW with a window start
17643 buffer position POS.
17645 Value is 1 if successful. It is zero if fonts were loaded during
17646 redisplay which makes re-adjusting glyph matrices necessary, and -1
17647 if point would appear in the scroll margins.
17648 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17649 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17650 set in FLAGS.) */
17653 try_window (Lisp_Object window, struct text_pos pos, int flags)
17655 struct window *w = XWINDOW (window);
17656 struct it it;
17657 struct glyph_row *last_text_row = NULL;
17658 struct frame *f = XFRAME (w->frame);
17659 int cursor_vpos = w->cursor.vpos;
17661 /* Make POS the new window start. */
17662 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17664 /* Mark cursor position as unknown. No overlay arrow seen. */
17665 w->cursor.vpos = -1;
17666 overlay_arrow_seen = false;
17668 /* Initialize iterator and info to start at POS. */
17669 start_display (&it, w, pos);
17670 it.glyph_row->reversed_p = false;
17672 /* Display all lines of W. */
17673 while (it.current_y < it.last_visible_y)
17675 if (display_line (&it, cursor_vpos))
17676 last_text_row = it.glyph_row - 1;
17677 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17678 return 0;
17681 /* Save the character position of 'it' before we call
17682 'start_display' again. */
17683 ptrdiff_t it_charpos = IT_CHARPOS (it);
17685 /* Don't let the cursor end in the scroll margins. */
17686 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17687 && !MINI_WINDOW_P (w))
17689 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17690 start_display (&it, w, pos);
17692 if ((w->cursor.y >= 0 /* not vscrolled */
17693 && w->cursor.y < this_scroll_margin
17694 && CHARPOS (pos) > BEGV
17695 && it_charpos < ZV)
17696 /* rms: considering make_cursor_line_fully_visible_p here
17697 seems to give wrong results. We don't want to recenter
17698 when the last line is partly visible, we want to allow
17699 that case to be handled in the usual way. */
17700 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17701 - this_scroll_margin - 1))
17703 w->cursor.vpos = -1;
17704 clear_glyph_matrix (w->desired_matrix);
17705 return -1;
17709 /* If bottom moved off end of frame, change mode line percentage. */
17710 if (w->window_end_pos <= 0 && Z != it_charpos)
17711 w->update_mode_line = true;
17713 /* Set window_end_pos to the offset of the last character displayed
17714 on the window from the end of current_buffer. Set
17715 window_end_vpos to its row number. */
17716 if (last_text_row)
17718 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17719 adjust_window_ends (w, last_text_row, false);
17720 eassert
17721 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17722 w->window_end_vpos)));
17724 else
17726 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17727 w->window_end_pos = Z - ZV;
17728 w->window_end_vpos = 0;
17731 /* But that is not valid info until redisplay finishes. */
17732 w->window_end_valid = false;
17733 return 1;
17738 /************************************************************************
17739 Window redisplay reusing current matrix when buffer has not changed
17740 ************************************************************************/
17742 /* Try redisplay of window W showing an unchanged buffer with a
17743 different window start than the last time it was displayed by
17744 reusing its current matrix. Value is true if successful.
17745 W->start is the new window start. */
17747 static bool
17748 try_window_reusing_current_matrix (struct window *w)
17750 struct frame *f = XFRAME (w->frame);
17751 struct glyph_row *bottom_row;
17752 struct it it;
17753 struct run run;
17754 struct text_pos start, new_start;
17755 int nrows_scrolled, i;
17756 struct glyph_row *last_text_row;
17757 struct glyph_row *last_reused_text_row;
17758 struct glyph_row *start_row;
17759 int start_vpos, min_y, max_y;
17761 #ifdef GLYPH_DEBUG
17762 if (inhibit_try_window_reusing)
17763 return false;
17764 #endif
17766 if (/* This function doesn't handle terminal frames. */
17767 !FRAME_WINDOW_P (f)
17768 /* Don't try to reuse the display if windows have been split
17769 or such. */
17770 || windows_or_buffers_changed
17771 || f->cursor_type_changed
17772 /* This function cannot handle buffers where the overlay arrow
17773 is shown on the fringes, because if the arrow position
17774 changes, we cannot just reuse the current matrix. */
17775 || overlay_arrow_in_current_buffer_p ())
17776 return false;
17778 /* Can't do this if showing trailing whitespace. */
17779 if (!NILP (Vshow_trailing_whitespace))
17780 return false;
17782 /* If top-line visibility has changed, give up. */
17783 if (window_wants_header_line (w)
17784 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17785 return false;
17787 /* Give up if old or new display is scrolled vertically. We could
17788 make this function handle this, but right now it doesn't. */
17789 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17790 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17791 return false;
17793 /* Clear the desired matrix for the display below. */
17794 clear_glyph_matrix (w->desired_matrix);
17796 /* Give up if line numbers are being displayed, because reusing the
17797 current matrix might use the wrong width for line-number
17798 display. */
17799 if (!NILP (Vdisplay_line_numbers))
17800 return false;
17802 /* The variable new_start now holds the new window start. The old
17803 start `start' can be determined from the current matrix. */
17804 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17805 start = start_row->minpos;
17806 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17808 if (CHARPOS (new_start) <= CHARPOS (start))
17810 /* Don't use this method if the display starts with an ellipsis
17811 displayed for invisible text. It's not easy to handle that case
17812 below, and it's certainly not worth the effort since this is
17813 not a frequent case. */
17814 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17815 return false;
17817 IF_DEBUG (debug_method_add (w, "twu1"));
17819 /* Display up to a row that can be reused. The variable
17820 last_text_row is set to the last row displayed that displays
17821 text. Note that it.vpos == 0 if or if not there is a
17822 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17823 start_display (&it, w, new_start);
17824 w->cursor.vpos = -1;
17825 last_text_row = last_reused_text_row = NULL;
17827 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17829 /* If we have reached into the characters in the START row,
17830 that means the line boundaries have changed. So we
17831 can't start copying with the row START. Maybe it will
17832 work to start copying with the following row. */
17833 while (IT_CHARPOS (it) > CHARPOS (start))
17835 /* Advance to the next row as the "start". */
17836 start_row++;
17837 start = start_row->minpos;
17838 /* If there are no more rows to try, or just one, give up. */
17839 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17840 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17841 || CHARPOS (start) == ZV)
17843 clear_glyph_matrix (w->desired_matrix);
17844 return false;
17847 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17849 /* If we have reached alignment, we can copy the rest of the
17850 rows. */
17851 if (IT_CHARPOS (it) == CHARPOS (start)
17852 /* Don't accept "alignment" inside a display vector,
17853 since start_row could have started in the middle of
17854 that same display vector (thus their character
17855 positions match), and we have no way of telling if
17856 that is the case. */
17857 && it.current.dpvec_index < 0)
17858 break;
17860 it.glyph_row->reversed_p = false;
17861 if (display_line (&it, -1))
17862 last_text_row = it.glyph_row - 1;
17866 /* A value of current_y < last_visible_y means that we stopped
17867 at the previous window start, which in turn means that we
17868 have at least one reusable row. */
17869 if (it.current_y < it.last_visible_y)
17871 struct glyph_row *row;
17873 /* IT.vpos always starts from 0; it counts text lines. */
17874 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17876 /* Find PT if not already found in the lines displayed. */
17877 if (w->cursor.vpos < 0)
17879 int dy = it.current_y - start_row->y;
17881 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17882 row = row_containing_pos (w, PT, row, NULL, dy);
17883 if (row)
17884 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17885 dy, nrows_scrolled);
17886 else
17888 clear_glyph_matrix (w->desired_matrix);
17889 return false;
17893 /* Scroll the display. Do it before the current matrix is
17894 changed. The problem here is that update has not yet
17895 run, i.e. part of the current matrix is not up to date.
17896 scroll_run_hook will clear the cursor, and use the
17897 current matrix to get the height of the row the cursor is
17898 in. */
17899 run.current_y = start_row->y;
17900 run.desired_y = it.current_y;
17901 run.height = it.last_visible_y - it.current_y;
17903 if (run.height > 0 && run.current_y != run.desired_y)
17905 update_begin (f);
17906 FRAME_RIF (f)->update_window_begin_hook (w);
17907 FRAME_RIF (f)->clear_window_mouse_face (w);
17908 FRAME_RIF (f)->scroll_run_hook (w, &run);
17909 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17910 update_end (f);
17913 /* Shift current matrix down by nrows_scrolled lines. */
17914 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17915 rotate_matrix (w->current_matrix,
17916 start_vpos,
17917 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17918 nrows_scrolled);
17920 /* Disable lines that must be updated. */
17921 for (i = 0; i < nrows_scrolled; ++i)
17922 (start_row + i)->enabled_p = false;
17924 /* Re-compute Y positions. */
17925 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17926 max_y = it.last_visible_y;
17927 for (row = start_row + nrows_scrolled;
17928 row < bottom_row;
17929 ++row)
17931 row->y = it.current_y;
17932 row->visible_height = row->height;
17934 if (row->y < min_y)
17935 row->visible_height -= min_y - row->y;
17936 if (row->y + row->height > max_y)
17937 row->visible_height -= row->y + row->height - max_y;
17938 if (row->fringe_bitmap_periodic_p)
17939 row->redraw_fringe_bitmaps_p = true;
17941 it.current_y += row->height;
17943 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17944 last_reused_text_row = row;
17945 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17946 break;
17949 /* Disable lines in the current matrix which are now
17950 below the window. */
17951 for (++row; row < bottom_row; ++row)
17952 row->enabled_p = row->mode_line_p = false;
17955 /* Update window_end_pos etc.; last_reused_text_row is the last
17956 reused row from the current matrix containing text, if any.
17957 The value of last_text_row is the last displayed line
17958 containing text. */
17959 if (last_reused_text_row)
17960 adjust_window_ends (w, last_reused_text_row, true);
17961 else if (last_text_row)
17962 adjust_window_ends (w, last_text_row, false);
17963 else
17965 /* This window must be completely empty. */
17966 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17967 w->window_end_pos = Z - ZV;
17968 w->window_end_vpos = 0;
17970 w->window_end_valid = false;
17972 /* Update hint: don't try scrolling again in update_window. */
17973 w->desired_matrix->no_scrolling_p = true;
17975 #ifdef GLYPH_DEBUG
17976 debug_method_add (w, "try_window_reusing_current_matrix 1");
17977 #endif
17978 return true;
17980 else if (CHARPOS (new_start) > CHARPOS (start))
17982 struct glyph_row *pt_row, *row;
17983 struct glyph_row *first_reusable_row;
17984 struct glyph_row *first_row_to_display;
17985 int dy;
17986 int yb = window_text_bottom_y (w);
17988 /* Find the row starting at new_start, if there is one. Don't
17989 reuse a partially visible line at the end. */
17990 first_reusable_row = start_row;
17991 while (first_reusable_row->enabled_p
17992 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17993 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17994 < CHARPOS (new_start)))
17995 ++first_reusable_row;
17997 /* Give up if there is no row to reuse. */
17998 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17999 || !first_reusable_row->enabled_p
18000 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
18001 != CHARPOS (new_start)))
18002 return false;
18004 /* We can reuse fully visible rows beginning with
18005 first_reusable_row to the end of the window. Set
18006 first_row_to_display to the first row that cannot be reused.
18007 Set pt_row to the row containing point, if there is any. */
18008 pt_row = NULL;
18009 for (first_row_to_display = first_reusable_row;
18010 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
18011 ++first_row_to_display)
18013 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
18014 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
18015 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
18016 && first_row_to_display->ends_at_zv_p
18017 && pt_row == NULL)))
18018 pt_row = first_row_to_display;
18021 /* Start displaying at the start of first_row_to_display. */
18022 eassert (first_row_to_display->y < yb);
18023 init_to_row_start (&it, w, first_row_to_display);
18025 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
18026 - start_vpos);
18027 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
18028 - nrows_scrolled);
18029 it.current_y = (first_row_to_display->y - first_reusable_row->y
18030 + WINDOW_HEADER_LINE_HEIGHT (w));
18032 /* Display lines beginning with first_row_to_display in the
18033 desired matrix. Set last_text_row to the last row displayed
18034 that displays text. */
18035 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
18036 if (pt_row == NULL)
18037 w->cursor.vpos = -1;
18038 last_text_row = NULL;
18039 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18040 if (display_line (&it, w->cursor.vpos))
18041 last_text_row = it.glyph_row - 1;
18043 /* If point is in a reused row, adjust y and vpos of the cursor
18044 position. */
18045 if (pt_row)
18047 w->cursor.vpos -= nrows_scrolled;
18048 w->cursor.y -= first_reusable_row->y - start_row->y;
18051 /* Give up if point isn't in a row displayed or reused. (This
18052 also handles the case where w->cursor.vpos < nrows_scrolled
18053 after the calls to display_line, which can happen with scroll
18054 margins. See bug#1295.) */
18055 if (w->cursor.vpos < 0)
18057 clear_glyph_matrix (w->desired_matrix);
18058 return false;
18061 /* Scroll the display. */
18062 run.current_y = first_reusable_row->y;
18063 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
18064 run.height = it.last_visible_y - run.current_y;
18065 dy = run.current_y - run.desired_y;
18067 if (run.height)
18069 update_begin (f);
18070 FRAME_RIF (f)->update_window_begin_hook (w);
18071 FRAME_RIF (f)->clear_window_mouse_face (w);
18072 FRAME_RIF (f)->scroll_run_hook (w, &run);
18073 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18074 update_end (f);
18077 /* Adjust Y positions of reused rows. */
18078 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18079 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18080 max_y = it.last_visible_y;
18081 for (row = first_reusable_row; row < first_row_to_display; ++row)
18083 row->y -= dy;
18084 row->visible_height = row->height;
18085 if (row->y < min_y)
18086 row->visible_height -= min_y - row->y;
18087 if (row->y + row->height > max_y)
18088 row->visible_height -= row->y + row->height - max_y;
18089 if (row->fringe_bitmap_periodic_p)
18090 row->redraw_fringe_bitmaps_p = true;
18093 /* Scroll the current matrix. */
18094 eassert (nrows_scrolled > 0);
18095 rotate_matrix (w->current_matrix,
18096 start_vpos,
18097 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18098 -nrows_scrolled);
18100 /* Disable rows not reused. */
18101 for (row -= nrows_scrolled; row < bottom_row; ++row)
18102 row->enabled_p = false;
18104 /* Point may have moved to a different line, so we cannot assume that
18105 the previous cursor position is valid; locate the correct row. */
18106 if (pt_row)
18108 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18109 row < bottom_row
18110 && PT >= MATRIX_ROW_END_CHARPOS (row)
18111 && !row->ends_at_zv_p;
18112 row++)
18114 w->cursor.vpos++;
18115 w->cursor.y = row->y;
18117 if (row < bottom_row)
18119 /* Can't simply scan the row for point with
18120 bidi-reordered glyph rows. Let set_cursor_from_row
18121 figure out where to put the cursor, and if it fails,
18122 give up. */
18123 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18125 if (!set_cursor_from_row (w, row, w->current_matrix,
18126 0, 0, 0, 0))
18128 clear_glyph_matrix (w->desired_matrix);
18129 return false;
18132 else
18134 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18135 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18137 for (; glyph < end
18138 && (!BUFFERP (glyph->object)
18139 || glyph->charpos < PT);
18140 glyph++)
18142 w->cursor.hpos++;
18143 w->cursor.x += glyph->pixel_width;
18149 /* Adjust window end. A null value of last_text_row means that
18150 the window end is in reused rows which in turn means that
18151 only its vpos can have changed. */
18152 if (last_text_row)
18153 adjust_window_ends (w, last_text_row, false);
18154 else
18155 w->window_end_vpos -= nrows_scrolled;
18157 w->window_end_valid = false;
18158 w->desired_matrix->no_scrolling_p = true;
18160 #ifdef GLYPH_DEBUG
18161 debug_method_add (w, "try_window_reusing_current_matrix 2");
18162 #endif
18163 return true;
18166 return false;
18171 /************************************************************************
18172 Window redisplay reusing current matrix when buffer has changed
18173 ************************************************************************/
18175 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18176 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18177 ptrdiff_t *, ptrdiff_t *);
18178 static struct glyph_row *
18179 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18180 struct glyph_row *);
18183 /* Return the last row in MATRIX displaying text. If row START is
18184 non-null, start searching with that row. IT gives the dimensions
18185 of the display. Value is null if matrix is empty; otherwise it is
18186 a pointer to the row found. */
18188 static struct glyph_row *
18189 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18190 struct glyph_row *start)
18192 struct glyph_row *row, *row_found;
18194 /* Set row_found to the last row in IT->w's current matrix
18195 displaying text. The loop looks funny but think of partially
18196 visible lines. */
18197 row_found = NULL;
18198 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18199 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18201 eassert (row->enabled_p);
18202 row_found = row;
18203 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18204 break;
18205 ++row;
18208 return row_found;
18212 /* Return the last row in the current matrix of W that is not affected
18213 by changes at the start of current_buffer that occurred since W's
18214 current matrix was built. Value is null if no such row exists.
18216 BEG_UNCHANGED us the number of characters unchanged at the start of
18217 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18218 first changed character in current_buffer. Characters at positions <
18219 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18220 when the current matrix was built. */
18222 static struct glyph_row *
18223 find_last_unchanged_at_beg_row (struct window *w)
18225 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18226 struct glyph_row *row;
18227 struct glyph_row *row_found = NULL;
18228 int yb = window_text_bottom_y (w);
18230 /* Find the last row displaying unchanged text. */
18231 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18232 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18233 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18234 ++row)
18236 if (/* If row ends before first_changed_pos, it is unchanged,
18237 except in some case. */
18238 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18239 /* When row ends in ZV and we write at ZV it is not
18240 unchanged. */
18241 && !row->ends_at_zv_p
18242 /* When first_changed_pos is the end of a continued line,
18243 row is not unchanged because it may be no longer
18244 continued. */
18245 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18246 && (row->continued_p
18247 || row->exact_window_width_line_p))
18248 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18249 needs to be recomputed, so don't consider this row as
18250 unchanged. This happens when the last line was
18251 bidi-reordered and was killed immediately before this
18252 redisplay cycle. In that case, ROW->end stores the
18253 buffer position of the first visual-order character of
18254 the killed text, which is now beyond ZV. */
18255 && CHARPOS (row->end.pos) <= ZV)
18256 row_found = row;
18258 /* Stop if last visible row. */
18259 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18260 break;
18263 return row_found;
18267 /* Find the first glyph row in the current matrix of W that is not
18268 affected by changes at the end of current_buffer since the
18269 time W's current matrix was built.
18271 Return in *DELTA the number of chars by which buffer positions in
18272 unchanged text at the end of current_buffer must be adjusted.
18274 Return in *DELTA_BYTES the corresponding number of bytes.
18276 Value is null if no such row exists, i.e. all rows are affected by
18277 changes. */
18279 static struct glyph_row *
18280 find_first_unchanged_at_end_row (struct window *w,
18281 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18283 struct glyph_row *row;
18284 struct glyph_row *row_found = NULL;
18286 *delta = *delta_bytes = 0;
18288 /* Display must not have been paused, otherwise the current matrix
18289 is not up to date. */
18290 eassert (w->window_end_valid);
18292 /* A value of window_end_pos >= END_UNCHANGED means that the window
18293 end is in the range of changed text. If so, there is no
18294 unchanged row at the end of W's current matrix. */
18295 if (w->window_end_pos >= END_UNCHANGED)
18296 return NULL;
18298 /* Set row to the last row in W's current matrix displaying text. */
18299 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18301 /* If matrix is entirely empty, no unchanged row exists. */
18302 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18304 /* The value of row is the last glyph row in the matrix having a
18305 meaningful buffer position in it. The end position of row
18306 corresponds to window_end_pos. This allows us to translate
18307 buffer positions in the current matrix to current buffer
18308 positions for characters not in changed text. */
18309 ptrdiff_t Z_old =
18310 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18311 ptrdiff_t Z_BYTE_old =
18312 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18313 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18314 struct glyph_row *first_text_row
18315 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18317 *delta = Z - Z_old;
18318 *delta_bytes = Z_BYTE - Z_BYTE_old;
18320 /* Set last_unchanged_pos to the buffer position of the last
18321 character in the buffer that has not been changed. Z is the
18322 index + 1 of the last character in current_buffer, i.e. by
18323 subtracting END_UNCHANGED we get the index of the last
18324 unchanged character, and we have to add BEG to get its buffer
18325 position. */
18326 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18327 last_unchanged_pos_old = last_unchanged_pos - *delta;
18329 /* Search backward from ROW for a row displaying a line that
18330 starts at a minimum position >= last_unchanged_pos_old. */
18331 for (; row > first_text_row; --row)
18333 /* This used to abort, but it can happen.
18334 It is ok to just stop the search instead here. KFS. */
18335 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18336 break;
18338 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18339 row_found = row;
18343 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18345 return row_found;
18349 /* Make sure that glyph rows in the current matrix of window W
18350 reference the same glyph memory as corresponding rows in the
18351 frame's frame matrix. This function is called after scrolling W's
18352 current matrix on a terminal frame in try_window_id and
18353 try_window_reusing_current_matrix. */
18355 static void
18356 sync_frame_with_window_matrix_rows (struct window *w)
18358 struct frame *f = XFRAME (w->frame);
18359 struct glyph_row *window_row, *window_row_end, *frame_row;
18361 /* Preconditions: W must be a leaf window and full-width. Its frame
18362 must have a frame matrix. */
18363 eassert (BUFFERP (w->contents));
18364 eassert (WINDOW_FULL_WIDTH_P (w));
18365 eassert (!FRAME_WINDOW_P (f));
18367 /* If W is a full-width window, glyph pointers in W's current matrix
18368 have, by definition, to be the same as glyph pointers in the
18369 corresponding frame matrix. Note that frame matrices have no
18370 marginal areas (see build_frame_matrix). */
18371 window_row = w->current_matrix->rows;
18372 window_row_end = window_row + w->current_matrix->nrows;
18373 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18374 while (window_row < window_row_end)
18376 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18377 struct glyph *end = window_row->glyphs[LAST_AREA];
18379 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18380 frame_row->glyphs[TEXT_AREA] = start;
18381 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18382 frame_row->glyphs[LAST_AREA] = end;
18384 /* Disable frame rows whose corresponding window rows have
18385 been disabled in try_window_id. */
18386 if (!window_row->enabled_p)
18387 frame_row->enabled_p = false;
18389 ++window_row, ++frame_row;
18394 /* Find the glyph row in window W containing CHARPOS. Consider all
18395 rows between START and END (not inclusive). END null means search
18396 all rows to the end of the display area of W. Value is the row
18397 containing CHARPOS or null. */
18399 struct glyph_row *
18400 row_containing_pos (struct window *w, ptrdiff_t charpos,
18401 struct glyph_row *start, struct glyph_row *end, int dy)
18403 struct glyph_row *row = start;
18404 struct glyph_row *best_row = NULL;
18405 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18406 int last_y;
18408 /* If we happen to start on a header-line, skip that. */
18409 if (row->mode_line_p)
18410 ++row;
18412 if ((end && row >= end) || !row->enabled_p)
18413 return NULL;
18415 last_y = window_text_bottom_y (w) - dy;
18417 while (true)
18419 /* Give up if we have gone too far. */
18420 if ((end && row >= end) || !row->enabled_p)
18421 return NULL;
18422 /* This formerly returned if they were equal.
18423 I think that both quantities are of a "last plus one" type;
18424 if so, when they are equal, the row is within the screen. -- rms. */
18425 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18426 return NULL;
18428 /* If it is in this row, return this row. */
18429 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18430 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18431 /* The end position of a row equals the start
18432 position of the next row. If CHARPOS is there, we
18433 would rather consider it displayed in the next
18434 line, except when this line ends in ZV. */
18435 && !row_for_charpos_p (row, charpos)))
18436 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18438 struct glyph *g;
18440 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18441 || (!best_row && !row->continued_p))
18442 return row;
18443 /* In bidi-reordered rows, there could be several rows whose
18444 edges surround CHARPOS, all of these rows belonging to
18445 the same continued line. We need to find the row which
18446 fits CHARPOS the best. */
18447 for (g = row->glyphs[TEXT_AREA];
18448 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18449 g++)
18451 if (!STRINGP (g->object))
18453 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18455 mindif = eabs (g->charpos - charpos);
18456 best_row = row;
18457 /* Exact match always wins. */
18458 if (mindif == 0)
18459 return best_row;
18464 else if (best_row && !row->continued_p)
18465 return best_row;
18466 ++row;
18471 /* Try to redisplay window W by reusing its existing display. W's
18472 current matrix must be up to date when this function is called,
18473 i.e., window_end_valid must be true.
18475 Value is
18477 >= 1 if successful, i.e. display has been updated
18478 specifically:
18479 1 means the changes were in front of a newline that precedes
18480 the window start, and the whole current matrix was reused
18481 2 means the changes were after the last position displayed
18482 in the window, and the whole current matrix was reused
18483 3 means portions of the current matrix were reused, while
18484 some of the screen lines were redrawn
18485 -1 if redisplay with same window start is known not to succeed
18486 0 if otherwise unsuccessful
18488 The following steps are performed:
18490 1. Find the last row in the current matrix of W that is not
18491 affected by changes at the start of current_buffer. If no such row
18492 is found, give up.
18494 2. Find the first row in W's current matrix that is not affected by
18495 changes at the end of current_buffer. Maybe there is no such row.
18497 3. Display lines beginning with the row + 1 found in step 1 to the
18498 row found in step 2 or, if step 2 didn't find a row, to the end of
18499 the window.
18501 4. If cursor is not known to appear on the window, give up.
18503 5. If display stopped at the row found in step 2, scroll the
18504 display and current matrix as needed.
18506 6. Maybe display some lines at the end of W, if we must. This can
18507 happen under various circumstances, like a partially visible line
18508 becoming fully visible, or because newly displayed lines are displayed
18509 in smaller font sizes.
18511 7. Update W's window end information. */
18513 static int
18514 try_window_id (struct window *w)
18516 struct frame *f = XFRAME (w->frame);
18517 struct glyph_matrix *current_matrix = w->current_matrix;
18518 struct glyph_matrix *desired_matrix = w->desired_matrix;
18519 struct glyph_row *last_unchanged_at_beg_row;
18520 struct glyph_row *first_unchanged_at_end_row;
18521 struct glyph_row *row;
18522 struct glyph_row *bottom_row;
18523 int bottom_vpos;
18524 struct it it;
18525 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18526 int dvpos, dy;
18527 struct text_pos start_pos;
18528 struct run run;
18529 int first_unchanged_at_end_vpos = 0;
18530 struct glyph_row *last_text_row, *last_text_row_at_end;
18531 struct text_pos start;
18532 ptrdiff_t first_changed_charpos, last_changed_charpos;
18534 #ifdef GLYPH_DEBUG
18535 if (inhibit_try_window_id)
18536 return 0;
18537 #endif
18539 /* This is handy for debugging. */
18540 #if false
18541 #define GIVE_UP(X) \
18542 do { \
18543 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18544 return 0; \
18545 } while (false)
18546 #else
18547 #define GIVE_UP(X) return 0
18548 #endif
18550 SET_TEXT_POS_FROM_MARKER (start, w->start);
18552 /* Don't use this for mini-windows because these can show
18553 messages and mini-buffers, and we don't handle that here. */
18554 if (MINI_WINDOW_P (w))
18555 GIVE_UP (1);
18557 /* This flag is used to prevent redisplay optimizations. */
18558 if (windows_or_buffers_changed || f->cursor_type_changed)
18559 GIVE_UP (2);
18561 /* This function's optimizations cannot be used if overlays have
18562 changed in the buffer displayed by the window, so give up if they
18563 have. */
18564 if (w->last_overlay_modified != OVERLAY_MODIFF)
18565 GIVE_UP (200);
18567 /* Verify that narrowing has not changed.
18568 Also verify that we were not told to prevent redisplay optimizations.
18569 It would be nice to further
18570 reduce the number of cases where this prevents try_window_id. */
18571 if (current_buffer->clip_changed
18572 || current_buffer->prevent_redisplay_optimizations_p)
18573 GIVE_UP (3);
18575 /* Window must either use window-based redisplay or be full width. */
18576 if (!FRAME_WINDOW_P (f)
18577 && (!FRAME_LINE_INS_DEL_OK (f)
18578 || !WINDOW_FULL_WIDTH_P (w)))
18579 GIVE_UP (4);
18581 /* Give up if point is known NOT to appear in W. */
18582 if (PT < CHARPOS (start))
18583 GIVE_UP (5);
18585 /* Another way to prevent redisplay optimizations. */
18586 if (w->last_modified == 0)
18587 GIVE_UP (6);
18589 /* Verify that window is not hscrolled. */
18590 if (w->hscroll != 0)
18591 GIVE_UP (7);
18593 /* Verify that display wasn't paused. */
18594 if (!w->window_end_valid)
18595 GIVE_UP (8);
18597 /* Likewise if highlighting trailing whitespace. */
18598 if (!NILP (Vshow_trailing_whitespace))
18599 GIVE_UP (11);
18601 /* Can't use this if overlay arrow position and/or string have
18602 changed. */
18603 if (overlay_arrows_changed_p (false))
18604 GIVE_UP (12);
18606 /* When word-wrap is on, adding a space to the first word of a
18607 wrapped line can change the wrap position, altering the line
18608 above it. It might be worthwhile to handle this more
18609 intelligently, but for now just redisplay from scratch. */
18610 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18611 GIVE_UP (21);
18613 /* Under bidi reordering, adding or deleting a character in the
18614 beginning of a paragraph, before the first strong directional
18615 character, can change the base direction of the paragraph (unless
18616 the buffer specifies a fixed paragraph direction), which will
18617 require redisplaying the whole paragraph. It might be worthwhile
18618 to find the paragraph limits and widen the range of redisplayed
18619 lines to that, but for now just give up this optimization and
18620 redisplay from scratch. */
18621 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18622 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18623 GIVE_UP (22);
18625 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18626 to that variable require thorough redisplay. */
18627 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18628 GIVE_UP (23);
18630 /* Give up if display-line-numbers is in relative mode, or when the
18631 current line's number needs to be displayed in a distinct face. */
18632 if (EQ (Vdisplay_line_numbers, Qrelative)
18633 || EQ (Vdisplay_line_numbers, Qvisual)
18634 || (!NILP (Vdisplay_line_numbers)
18635 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18636 Qline_number_current_line,
18637 w->frame))))
18638 GIVE_UP (24);
18640 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18641 only if buffer has really changed. The reason is that the gap is
18642 initially at Z for freshly visited files. The code below would
18643 set end_unchanged to 0 in that case. */
18644 if (MODIFF > SAVE_MODIFF
18645 /* This seems to happen sometimes after saving a buffer. */
18646 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18648 if (GPT - BEG < BEG_UNCHANGED)
18649 BEG_UNCHANGED = GPT - BEG;
18650 if (Z - GPT < END_UNCHANGED)
18651 END_UNCHANGED = Z - GPT;
18654 /* The position of the first and last character that has been changed. */
18655 first_changed_charpos = BEG + BEG_UNCHANGED;
18656 last_changed_charpos = Z - END_UNCHANGED;
18658 /* If window starts after a line end, and the last change is in
18659 front of that newline, then changes don't affect the display.
18660 This case happens with stealth-fontification. Note that although
18661 the display is unchanged, glyph positions in the matrix have to
18662 be adjusted, of course. */
18663 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18664 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18665 && ((last_changed_charpos < CHARPOS (start)
18666 && CHARPOS (start) == BEGV)
18667 || (last_changed_charpos < CHARPOS (start) - 1
18668 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18670 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18671 struct glyph_row *r0;
18673 /* Compute how many chars/bytes have been added to or removed
18674 from the buffer. */
18675 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18676 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18677 Z_delta = Z - Z_old;
18678 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18680 /* Give up if PT is not in the window. Note that it already has
18681 been checked at the start of try_window_id that PT is not in
18682 front of the window start. */
18683 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18684 GIVE_UP (13);
18686 /* If window start is unchanged, we can reuse the whole matrix
18687 as is, after adjusting glyph positions. No need to compute
18688 the window end again, since its offset from Z hasn't changed. */
18689 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18690 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18691 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18692 /* PT must not be in a partially visible line. */
18693 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18694 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18696 /* Adjust positions in the glyph matrix. */
18697 if (Z_delta || Z_delta_bytes)
18699 struct glyph_row *r1
18700 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18701 increment_matrix_positions (w->current_matrix,
18702 MATRIX_ROW_VPOS (r0, current_matrix),
18703 MATRIX_ROW_VPOS (r1, current_matrix),
18704 Z_delta, Z_delta_bytes);
18707 /* Set the cursor. */
18708 row = row_containing_pos (w, PT, r0, NULL, 0);
18709 if (row)
18710 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18711 return 1;
18715 /* Handle the case that changes are all below what is displayed in
18716 the window, and that PT is in the window. This shortcut cannot
18717 be taken if ZV is visible in the window, and text has been added
18718 there that is visible in the window. */
18719 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18720 /* ZV is not visible in the window, or there are no
18721 changes at ZV, actually. */
18722 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18723 || first_changed_charpos == last_changed_charpos))
18725 struct glyph_row *r0;
18727 /* Give up if PT is not in the window. Note that it already has
18728 been checked at the start of try_window_id that PT is not in
18729 front of the window start. */
18730 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18731 GIVE_UP (14);
18733 /* If window start is unchanged, we can reuse the whole matrix
18734 as is, without changing glyph positions since no text has
18735 been added/removed in front of the window end. */
18736 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18737 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18738 /* PT must not be in a partially visible line. */
18739 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18740 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18742 /* We have to compute the window end anew since text
18743 could have been added/removed after it. */
18744 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18745 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18747 /* Set the cursor. */
18748 row = row_containing_pos (w, PT, r0, NULL, 0);
18749 if (row)
18750 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18751 return 2;
18755 /* Give up if window start is in the changed area.
18757 The condition used to read
18759 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18761 but why that was tested escapes me at the moment. */
18762 if (CHARPOS (start) >= first_changed_charpos
18763 && CHARPOS (start) <= last_changed_charpos)
18764 GIVE_UP (15);
18766 /* Check that window start agrees with the start of the first glyph
18767 row in its current matrix. Check this after we know the window
18768 start is not in changed text, otherwise positions would not be
18769 comparable. */
18770 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18771 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18772 GIVE_UP (16);
18774 /* Give up if the window ends in strings. Overlay strings
18775 at the end are difficult to handle, so don't try. */
18776 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18777 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18778 GIVE_UP (20);
18780 /* Compute the position at which we have to start displaying new
18781 lines. Some of the lines at the top of the window might be
18782 reusable because they are not displaying changed text. Find the
18783 last row in W's current matrix not affected by changes at the
18784 start of current_buffer. Value is null if changes start in the
18785 first line of window. */
18786 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18787 if (last_unchanged_at_beg_row)
18789 /* Avoid starting to display in the middle of a character, a TAB
18790 for instance. This is easier than to set up the iterator
18791 exactly, and it's not a frequent case, so the additional
18792 effort wouldn't really pay off. */
18793 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18794 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18795 && last_unchanged_at_beg_row > w->current_matrix->rows)
18796 --last_unchanged_at_beg_row;
18798 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18799 GIVE_UP (17);
18801 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18802 GIVE_UP (18);
18803 start_pos = it.current.pos;
18805 /* Start displaying new lines in the desired matrix at the same
18806 vpos we would use in the current matrix, i.e. below
18807 last_unchanged_at_beg_row. */
18808 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18809 current_matrix);
18810 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18811 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18813 eassert (it.hpos == 0 && it.current_x == 0);
18815 else
18817 /* There are no reusable lines at the start of the window.
18818 Start displaying in the first text line. */
18819 start_display (&it, w, start);
18820 it.vpos = it.first_vpos;
18821 start_pos = it.current.pos;
18824 /* Find the first row that is not affected by changes at the end of
18825 the buffer. Value will be null if there is no unchanged row, in
18826 which case we must redisplay to the end of the window. delta
18827 will be set to the value by which buffer positions beginning with
18828 first_unchanged_at_end_row have to be adjusted due to text
18829 changes. */
18830 first_unchanged_at_end_row
18831 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18832 IF_DEBUG (debug_delta = delta);
18833 IF_DEBUG (debug_delta_bytes = delta_bytes);
18835 /* Set stop_pos to the buffer position up to which we will have to
18836 display new lines. If first_unchanged_at_end_row != NULL, this
18837 is the buffer position of the start of the line displayed in that
18838 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18839 that we don't stop at a buffer position. */
18840 stop_pos = 0;
18841 if (first_unchanged_at_end_row)
18843 eassert (last_unchanged_at_beg_row == NULL
18844 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18846 /* If this is a continuation line, move forward to the next one
18847 that isn't. Changes in lines above affect this line.
18848 Caution: this may move first_unchanged_at_end_row to a row
18849 not displaying text. */
18850 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18851 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18852 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18853 < it.last_visible_y))
18854 ++first_unchanged_at_end_row;
18856 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18857 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18858 >= it.last_visible_y))
18859 first_unchanged_at_end_row = NULL;
18860 else
18862 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18863 + delta);
18864 first_unchanged_at_end_vpos
18865 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18866 eassert (stop_pos >= Z - END_UNCHANGED);
18869 else if (last_unchanged_at_beg_row == NULL)
18870 GIVE_UP (19);
18873 #ifdef GLYPH_DEBUG
18875 /* Either there is no unchanged row at the end, or the one we have
18876 now displays text. This is a necessary condition for the window
18877 end pos calculation at the end of this function. */
18878 eassert (first_unchanged_at_end_row == NULL
18879 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18881 debug_last_unchanged_at_beg_vpos
18882 = (last_unchanged_at_beg_row
18883 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18884 : -1);
18885 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18887 #endif /* GLYPH_DEBUG */
18890 /* Display new lines. Set last_text_row to the last new line
18891 displayed which has text on it, i.e. might end up as being the
18892 line where the window_end_vpos is. */
18893 w->cursor.vpos = -1;
18894 last_text_row = NULL;
18895 overlay_arrow_seen = false;
18896 if (it.current_y < it.last_visible_y
18897 && !f->fonts_changed
18898 && (first_unchanged_at_end_row == NULL
18899 || IT_CHARPOS (it) < stop_pos))
18900 it.glyph_row->reversed_p = false;
18901 while (it.current_y < it.last_visible_y
18902 && !f->fonts_changed
18903 && (first_unchanged_at_end_row == NULL
18904 || IT_CHARPOS (it) < stop_pos))
18906 if (display_line (&it, -1))
18907 last_text_row = it.glyph_row - 1;
18910 if (f->fonts_changed)
18911 return -1;
18913 /* The redisplay iterations in display_line above could have
18914 triggered font-lock, which could have done something that
18915 invalidates IT->w window's end-point information, on which we
18916 rely below. E.g., one package, which will remain unnamed, used
18917 to install a font-lock-fontify-region-function that called
18918 bury-buffer, whose side effect is to switch the buffer displayed
18919 by IT->w, and that predictably resets IT->w's window_end_valid
18920 flag, which we already tested at the entry to this function.
18921 Amply punish such packages/modes by giving up on this
18922 optimization in those cases. */
18923 if (!w->window_end_valid)
18925 clear_glyph_matrix (w->desired_matrix);
18926 return -1;
18929 /* Compute differences in buffer positions, y-positions etc. for
18930 lines reused at the bottom of the window. Compute what we can
18931 scroll. */
18932 if (first_unchanged_at_end_row
18933 /* No lines reused because we displayed everything up to the
18934 bottom of the window. */
18935 && it.current_y < it.last_visible_y)
18937 dvpos = (it.vpos
18938 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18939 current_matrix));
18940 dy = it.current_y - first_unchanged_at_end_row->y;
18941 run.current_y = first_unchanged_at_end_row->y;
18942 run.desired_y = run.current_y + dy;
18943 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18945 else
18947 delta = delta_bytes = dvpos = dy
18948 = run.current_y = run.desired_y = run.height = 0;
18949 first_unchanged_at_end_row = NULL;
18951 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18954 /* Find the cursor if not already found. We have to decide whether
18955 PT will appear on this window (it sometimes doesn't, but this is
18956 not a very frequent case.) This decision has to be made before
18957 the current matrix is altered. A value of cursor.vpos < 0 means
18958 that PT is either in one of the lines beginning at
18959 first_unchanged_at_end_row or below the window. Don't care for
18960 lines that might be displayed later at the window end; as
18961 mentioned, this is not a frequent case. */
18962 if (w->cursor.vpos < 0)
18964 /* Cursor in unchanged rows at the top? */
18965 if (PT < CHARPOS (start_pos)
18966 && last_unchanged_at_beg_row)
18968 row = row_containing_pos (w, PT,
18969 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18970 last_unchanged_at_beg_row + 1, 0);
18971 if (row)
18972 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18975 /* Start from first_unchanged_at_end_row looking for PT. */
18976 else if (first_unchanged_at_end_row)
18978 row = row_containing_pos (w, PT - delta,
18979 first_unchanged_at_end_row, NULL, 0);
18980 if (row)
18981 set_cursor_from_row (w, row, w->current_matrix, delta,
18982 delta_bytes, dy, dvpos);
18985 /* Give up if cursor was not found. */
18986 if (w->cursor.vpos < 0)
18988 clear_glyph_matrix (w->desired_matrix);
18989 return -1;
18993 /* Don't let the cursor end in the scroll margins. */
18995 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18996 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18998 if ((w->cursor.y < this_scroll_margin
18999 && CHARPOS (start) > BEGV)
19000 /* Old redisplay didn't take scroll margin into account at the bottom,
19001 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
19002 || (w->cursor.y + (make_cursor_line_fully_visible_p
19003 ? cursor_height + this_scroll_margin
19004 : 1)) > it.last_visible_y)
19006 w->cursor.vpos = -1;
19007 clear_glyph_matrix (w->desired_matrix);
19008 return -1;
19012 /* Scroll the display. Do it before changing the current matrix so
19013 that xterm.c doesn't get confused about where the cursor glyph is
19014 found. */
19015 if (dy && run.height)
19017 update_begin (f);
19019 if (FRAME_WINDOW_P (f))
19021 FRAME_RIF (f)->update_window_begin_hook (w);
19022 FRAME_RIF (f)->clear_window_mouse_face (w);
19023 FRAME_RIF (f)->scroll_run_hook (w, &run);
19024 FRAME_RIF (f)->update_window_end_hook (w, false, false);
19026 else
19028 /* Terminal frame. In this case, dvpos gives the number of
19029 lines to scroll by; dvpos < 0 means scroll up. */
19030 int from_vpos
19031 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
19032 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
19033 int end = (WINDOW_TOP_EDGE_LINE (w)
19034 + window_wants_header_line (w)
19035 + window_internal_height (w));
19037 #if defined (HAVE_GPM) || defined (MSDOS)
19038 x_clear_window_mouse_face (w);
19039 #endif
19040 /* Perform the operation on the screen. */
19041 if (dvpos > 0)
19043 /* Scroll last_unchanged_at_beg_row to the end of the
19044 window down dvpos lines. */
19045 set_terminal_window (f, end);
19047 /* On dumb terminals delete dvpos lines at the end
19048 before inserting dvpos empty lines. */
19049 if (!FRAME_SCROLL_REGION_OK (f))
19050 ins_del_lines (f, end - dvpos, -dvpos);
19052 /* Insert dvpos empty lines in front of
19053 last_unchanged_at_beg_row. */
19054 ins_del_lines (f, from, dvpos);
19056 else if (dvpos < 0)
19058 /* Scroll up last_unchanged_at_beg_vpos to the end of
19059 the window to last_unchanged_at_beg_vpos - |dvpos|. */
19060 set_terminal_window (f, end);
19062 /* Delete dvpos lines in front of
19063 last_unchanged_at_beg_vpos. ins_del_lines will set
19064 the cursor to the given vpos and emit |dvpos| delete
19065 line sequences. */
19066 ins_del_lines (f, from + dvpos, dvpos);
19068 /* On a dumb terminal insert dvpos empty lines at the
19069 end. */
19070 if (!FRAME_SCROLL_REGION_OK (f))
19071 ins_del_lines (f, end + dvpos, -dvpos);
19074 set_terminal_window (f, 0);
19077 update_end (f);
19080 /* Shift reused rows of the current matrix to the right position.
19081 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19082 text. */
19083 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19084 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19085 if (dvpos < 0)
19087 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19088 bottom_vpos, dvpos);
19089 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19090 bottom_vpos);
19092 else if (dvpos > 0)
19094 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19095 bottom_vpos, dvpos);
19096 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19097 first_unchanged_at_end_vpos + dvpos);
19100 /* For frame-based redisplay, make sure that current frame and window
19101 matrix are in sync with respect to glyph memory. */
19102 if (!FRAME_WINDOW_P (f))
19103 sync_frame_with_window_matrix_rows (w);
19105 /* Adjust buffer positions in reused rows. */
19106 if (delta || delta_bytes)
19107 increment_matrix_positions (current_matrix,
19108 first_unchanged_at_end_vpos + dvpos,
19109 bottom_vpos, delta, delta_bytes);
19111 /* Adjust Y positions. */
19112 if (dy)
19113 shift_glyph_matrix (w, current_matrix,
19114 first_unchanged_at_end_vpos + dvpos,
19115 bottom_vpos, dy);
19117 if (first_unchanged_at_end_row)
19119 first_unchanged_at_end_row += dvpos;
19120 if (first_unchanged_at_end_row->y >= it.last_visible_y
19121 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19122 first_unchanged_at_end_row = NULL;
19125 /* If scrolling up, there may be some lines to display at the end of
19126 the window. */
19127 last_text_row_at_end = NULL;
19128 if (dy < 0)
19130 /* Scrolling up can leave for example a partially visible line
19131 at the end of the window to be redisplayed. */
19132 /* Set last_row to the glyph row in the current matrix where the
19133 window end line is found. It has been moved up or down in
19134 the matrix by dvpos. */
19135 int last_vpos = w->window_end_vpos + dvpos;
19136 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19138 /* If last_row is the window end line, it should display text. */
19139 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19141 /* If window end line was partially visible before, begin
19142 displaying at that line. Otherwise begin displaying with the
19143 line following it. */
19144 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19146 init_to_row_start (&it, w, last_row);
19147 it.vpos = last_vpos;
19148 it.current_y = last_row->y;
19150 else
19152 init_to_row_end (&it, w, last_row);
19153 it.vpos = 1 + last_vpos;
19154 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19155 ++last_row;
19158 /* We may start in a continuation line. If so, we have to
19159 get the right continuation_lines_width and current_x. */
19160 it.continuation_lines_width = last_row->continuation_lines_width;
19161 it.hpos = it.current_x = 0;
19163 /* Display the rest of the lines at the window end. */
19164 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19165 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19167 /* Is it always sure that the display agrees with lines in
19168 the current matrix? I don't think so, so we mark rows
19169 displayed invalid in the current matrix by setting their
19170 enabled_p flag to false. */
19171 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19172 if (display_line (&it, w->cursor.vpos))
19173 last_text_row_at_end = it.glyph_row - 1;
19177 /* Update window_end_pos and window_end_vpos. */
19178 if (first_unchanged_at_end_row && !last_text_row_at_end)
19180 /* Window end line if one of the preserved rows from the current
19181 matrix. Set row to the last row displaying text in current
19182 matrix starting at first_unchanged_at_end_row, after
19183 scrolling. */
19184 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19185 row = find_last_row_displaying_text (w->current_matrix, &it,
19186 first_unchanged_at_end_row);
19187 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19188 adjust_window_ends (w, row, true);
19189 eassert (w->window_end_bytepos >= 0);
19190 IF_DEBUG (debug_method_add (w, "A"));
19192 else if (last_text_row_at_end)
19194 adjust_window_ends (w, last_text_row_at_end, false);
19195 eassert (w->window_end_bytepos >= 0);
19196 IF_DEBUG (debug_method_add (w, "B"));
19198 else if (last_text_row)
19200 /* We have displayed either to the end of the window or at the
19201 end of the window, i.e. the last row with text is to be found
19202 in the desired matrix. */
19203 adjust_window_ends (w, last_text_row, false);
19204 eassert (w->window_end_bytepos >= 0);
19206 else if (first_unchanged_at_end_row == NULL
19207 && last_text_row == NULL
19208 && last_text_row_at_end == NULL)
19210 /* Displayed to end of window, but no line containing text was
19211 displayed. Lines were deleted at the end of the window. */
19212 bool first_vpos = window_wants_header_line (w);
19213 int vpos = w->window_end_vpos;
19214 struct glyph_row *current_row = current_matrix->rows + vpos;
19215 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19217 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19219 eassert (first_vpos <= vpos);
19220 if (desired_row->enabled_p)
19222 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19223 row = desired_row;
19225 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19226 row = current_row;
19229 w->window_end_vpos = vpos + 1;
19230 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19231 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19232 eassert (w->window_end_bytepos >= 0);
19233 IF_DEBUG (debug_method_add (w, "C"));
19235 else
19236 emacs_abort ();
19238 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19239 debug_end_vpos = w->window_end_vpos));
19241 /* Record that display has not been completed. */
19242 w->window_end_valid = false;
19243 w->desired_matrix->no_scrolling_p = true;
19244 return 3;
19246 #undef GIVE_UP
19251 /***********************************************************************
19252 More debugging support
19253 ***********************************************************************/
19255 #ifdef GLYPH_DEBUG
19257 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19258 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19259 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19262 /* Dump the contents of glyph matrix MATRIX on stderr.
19264 GLYPHS 0 means don't show glyph contents.
19265 GLYPHS 1 means show glyphs in short form
19266 GLYPHS > 1 means show glyphs in long form. */
19268 void
19269 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19271 int i;
19272 for (i = 0; i < matrix->nrows; ++i)
19273 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19277 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19278 the glyph row and area where the glyph comes from. */
19280 void
19281 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19283 if (glyph->type == CHAR_GLYPH
19284 || glyph->type == GLYPHLESS_GLYPH)
19286 fprintf (stderr,
19287 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19288 glyph - row->glyphs[TEXT_AREA],
19289 (glyph->type == CHAR_GLYPH
19290 ? 'C'
19291 : 'G'),
19292 glyph->charpos,
19293 (BUFFERP (glyph->object)
19294 ? 'B'
19295 : (STRINGP (glyph->object)
19296 ? 'S'
19297 : (NILP (glyph->object)
19298 ? '0'
19299 : '-'))),
19300 glyph->pixel_width,
19301 glyph->u.ch,
19302 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19303 ? (int) glyph->u.ch
19304 : '.'),
19305 glyph->face_id,
19306 glyph->left_box_line_p,
19307 glyph->right_box_line_p);
19309 else if (glyph->type == STRETCH_GLYPH)
19311 fprintf (stderr,
19312 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19313 glyph - row->glyphs[TEXT_AREA],
19314 'S',
19315 glyph->charpos,
19316 (BUFFERP (glyph->object)
19317 ? 'B'
19318 : (STRINGP (glyph->object)
19319 ? 'S'
19320 : (NILP (glyph->object)
19321 ? '0'
19322 : '-'))),
19323 glyph->pixel_width,
19325 ' ',
19326 glyph->face_id,
19327 glyph->left_box_line_p,
19328 glyph->right_box_line_p);
19330 else if (glyph->type == IMAGE_GLYPH)
19332 fprintf (stderr,
19333 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19334 glyph - row->glyphs[TEXT_AREA],
19335 'I',
19336 glyph->charpos,
19337 (BUFFERP (glyph->object)
19338 ? 'B'
19339 : (STRINGP (glyph->object)
19340 ? 'S'
19341 : (NILP (glyph->object)
19342 ? '0'
19343 : '-'))),
19344 glyph->pixel_width,
19345 (unsigned int) glyph->u.img_id,
19346 '.',
19347 glyph->face_id,
19348 glyph->left_box_line_p,
19349 glyph->right_box_line_p);
19351 else if (glyph->type == COMPOSITE_GLYPH)
19353 fprintf (stderr,
19354 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19355 glyph - row->glyphs[TEXT_AREA],
19356 '+',
19357 glyph->charpos,
19358 (BUFFERP (glyph->object)
19359 ? 'B'
19360 : (STRINGP (glyph->object)
19361 ? 'S'
19362 : (NILP (glyph->object)
19363 ? '0'
19364 : '-'))),
19365 glyph->pixel_width,
19366 (unsigned int) glyph->u.cmp.id);
19367 if (glyph->u.cmp.automatic)
19368 fprintf (stderr,
19369 "[%d-%d]",
19370 glyph->slice.cmp.from, glyph->slice.cmp.to);
19371 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19372 glyph->face_id,
19373 glyph->left_box_line_p,
19374 glyph->right_box_line_p);
19376 else if (glyph->type == XWIDGET_GLYPH)
19378 #ifndef HAVE_XWIDGETS
19379 eassume (false);
19380 #else
19381 fprintf (stderr,
19382 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19383 glyph - row->glyphs[TEXT_AREA],
19384 'X',
19385 glyph->charpos,
19386 (BUFFERP (glyph->object)
19387 ? 'B'
19388 : (STRINGP (glyph->object)
19389 ? 'S'
19390 : '-')),
19391 glyph->pixel_width,
19392 glyph->u.xwidget,
19393 '.',
19394 glyph->face_id,
19395 glyph->left_box_line_p,
19396 glyph->right_box_line_p);
19397 #endif
19402 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19403 GLYPHS 0 means don't show glyph contents.
19404 GLYPHS 1 means show glyphs in short form
19405 GLYPHS > 1 means show glyphs in long form. */
19407 void
19408 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19410 if (glyphs != 1)
19412 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19413 fprintf (stderr, "==============================================================================\n");
19415 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19416 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19417 vpos,
19418 MATRIX_ROW_START_CHARPOS (row),
19419 MATRIX_ROW_END_CHARPOS (row),
19420 row->used[TEXT_AREA],
19421 row->contains_overlapping_glyphs_p,
19422 row->enabled_p,
19423 row->truncated_on_left_p,
19424 row->truncated_on_right_p,
19425 row->continued_p,
19426 MATRIX_ROW_CONTINUATION_LINE_P (row),
19427 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19428 row->ends_at_zv_p,
19429 row->fill_line_p,
19430 row->ends_in_middle_of_char_p,
19431 row->starts_in_middle_of_char_p,
19432 row->mouse_face_p,
19433 row->x,
19434 row->y,
19435 row->pixel_width,
19436 row->height,
19437 row->visible_height,
19438 row->ascent,
19439 row->phys_ascent);
19440 /* The next 3 lines should align to "Start" in the header. */
19441 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19442 row->end.overlay_string_index,
19443 row->continuation_lines_width);
19444 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19445 CHARPOS (row->start.string_pos),
19446 CHARPOS (row->end.string_pos));
19447 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19448 row->end.dpvec_index);
19451 if (glyphs > 1)
19453 int area;
19455 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19457 struct glyph *glyph = row->glyphs[area];
19458 struct glyph *glyph_end = glyph + row->used[area];
19460 /* Glyph for a line end in text. */
19461 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19462 ++glyph_end;
19464 if (glyph < glyph_end)
19465 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19467 for (; glyph < glyph_end; ++glyph)
19468 dump_glyph (row, glyph, area);
19471 else if (glyphs == 1)
19473 int area;
19474 char s[SHRT_MAX + 4];
19476 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19478 int i;
19480 for (i = 0; i < row->used[area]; ++i)
19482 struct glyph *glyph = row->glyphs[area] + i;
19483 if (i == row->used[area] - 1
19484 && area == TEXT_AREA
19485 && NILP (glyph->object)
19486 && glyph->type == CHAR_GLYPH
19487 && glyph->u.ch == ' ')
19489 strcpy (&s[i], "[\\n]");
19490 i += 4;
19492 else if (glyph->type == CHAR_GLYPH
19493 && glyph->u.ch < 0x80
19494 && glyph->u.ch >= ' ')
19495 s[i] = glyph->u.ch;
19496 else
19497 s[i] = '.';
19500 s[i] = '\0';
19501 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19507 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19508 Sdump_glyph_matrix, 0, 1, "p",
19509 doc: /* Dump the current matrix of the selected window to stderr.
19510 Shows contents of glyph row structures. With non-nil
19511 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19512 glyphs in short form, otherwise show glyphs in long form.
19514 Interactively, no argument means show glyphs in short form;
19515 with numeric argument, its value is passed as the GLYPHS flag. */)
19516 (Lisp_Object glyphs)
19518 struct window *w = XWINDOW (selected_window);
19519 struct buffer *buffer = XBUFFER (w->contents);
19521 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19522 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19523 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19524 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19525 fprintf (stderr, "=============================================\n");
19526 dump_glyph_matrix (w->current_matrix,
19527 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19528 return Qnil;
19532 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19533 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19534 Only text-mode frames have frame glyph matrices. */)
19535 (void)
19537 struct frame *f = XFRAME (selected_frame);
19539 if (f->current_matrix)
19540 dump_glyph_matrix (f->current_matrix, 1);
19541 else
19542 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19543 return Qnil;
19547 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19548 doc: /* Dump glyph row ROW to stderr.
19549 Interactively, ROW is the prefix numeric argument and defaults to
19550 the row which displays point.
19551 Optional argument GLYPHS 0 means don't dump glyphs.
19552 GLYPHS 1 means dump glyphs in short form.
19553 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19554 (Lisp_Object row, Lisp_Object glyphs)
19556 struct glyph_matrix *matrix;
19557 EMACS_INT vpos;
19559 if (NILP (row))
19561 int d1, d2, d3, d4, d5, ypos;
19562 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19563 &d1, &d2, &d3, &d4, &d5, &ypos);
19564 if (visible_p)
19565 vpos = ypos;
19566 else
19567 vpos = 0;
19569 else
19571 CHECK_NUMBER (row);
19572 vpos = XINT (row);
19574 matrix = XWINDOW (selected_window)->current_matrix;
19575 if (vpos >= 0 && vpos < matrix->nrows)
19576 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19577 vpos,
19578 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19579 return Qnil;
19583 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19584 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19585 Interactively, ROW is the prefix numeric argument and defaults to zero.
19586 GLYPHS 0 means don't dump glyphs.
19587 GLYPHS 1 means dump glyphs in short form.
19588 GLYPHS > 1 or omitted means dump glyphs in long form.
19590 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19591 do nothing. */)
19592 (Lisp_Object row, Lisp_Object glyphs)
19594 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19595 struct frame *sf = SELECTED_FRAME ();
19596 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19597 EMACS_INT vpos;
19599 if (NILP (row))
19600 vpos = 0;
19601 else
19603 CHECK_NUMBER (row);
19604 vpos = XINT (row);
19606 if (vpos >= 0 && vpos < m->nrows)
19607 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19608 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19609 #endif
19610 return Qnil;
19614 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19615 doc: /* Toggle tracing of redisplay.
19616 With ARG, turn tracing on if and only if ARG is positive. */)
19617 (Lisp_Object arg)
19619 if (NILP (arg))
19620 trace_redisplay_p = !trace_redisplay_p;
19621 else
19623 arg = Fprefix_numeric_value (arg);
19624 trace_redisplay_p = XINT (arg) > 0;
19627 return Qnil;
19631 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19632 doc: /* Like `format', but print result to stderr.
19633 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19634 (ptrdiff_t nargs, Lisp_Object *args)
19636 Lisp_Object s = Fformat (nargs, args);
19637 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19638 return Qnil;
19641 #endif /* GLYPH_DEBUG */
19645 /***********************************************************************
19646 Building Desired Matrix Rows
19647 ***********************************************************************/
19649 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19650 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19652 static struct glyph_row *
19653 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19655 struct frame *f = XFRAME (WINDOW_FRAME (w));
19656 struct buffer *buffer = XBUFFER (w->contents);
19657 struct buffer *old = current_buffer;
19658 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19659 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19660 const unsigned char *arrow_end = arrow_string + arrow_len;
19661 const unsigned char *p;
19662 struct it it;
19663 bool multibyte_p;
19664 int n_glyphs_before;
19666 set_buffer_temp (buffer);
19667 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19668 scratch_glyph_row.reversed_p = false;
19669 it.glyph_row->used[TEXT_AREA] = 0;
19670 SET_TEXT_POS (it.position, 0, 0);
19672 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19673 p = arrow_string;
19674 while (p < arrow_end)
19676 Lisp_Object face, ilisp;
19678 /* Get the next character. */
19679 if (multibyte_p)
19680 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19681 else
19683 it.c = it.char_to_display = *p, it.len = 1;
19684 if (! ASCII_CHAR_P (it.c))
19685 it.char_to_display = BYTE8_TO_CHAR (it.c);
19687 p += it.len;
19689 /* Get its face. */
19690 ilisp = make_number (p - arrow_string);
19691 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19692 it.face_id = compute_char_face (f, it.char_to_display, face);
19694 /* Compute its width, get its glyphs. */
19695 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19696 SET_TEXT_POS (it.position, -1, -1);
19697 PRODUCE_GLYPHS (&it);
19699 /* If this character doesn't fit any more in the line, we have
19700 to remove some glyphs. */
19701 if (it.current_x > it.last_visible_x)
19703 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19704 break;
19708 set_buffer_temp (old);
19709 return it.glyph_row;
19713 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19714 glyphs to insert is determined by produce_special_glyphs. */
19716 static void
19717 insert_left_trunc_glyphs (struct it *it)
19719 struct it truncate_it;
19720 struct glyph *from, *end, *to, *toend;
19722 eassert (!FRAME_WINDOW_P (it->f)
19723 || (!it->glyph_row->reversed_p
19724 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19725 || (it->glyph_row->reversed_p
19726 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19728 /* Get the truncation glyphs. */
19729 truncate_it = *it;
19730 truncate_it.current_x = 0;
19731 truncate_it.face_id = DEFAULT_FACE_ID;
19732 truncate_it.glyph_row = &scratch_glyph_row;
19733 truncate_it.area = TEXT_AREA;
19734 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19735 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19736 truncate_it.object = Qnil;
19737 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19739 /* Overwrite glyphs from IT with truncation glyphs. */
19740 if (!it->glyph_row->reversed_p)
19742 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19744 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19745 end = from + tused;
19746 to = it->glyph_row->glyphs[TEXT_AREA];
19747 toend = to + it->glyph_row->used[TEXT_AREA];
19748 if (FRAME_WINDOW_P (it->f))
19750 /* On GUI frames, when variable-size fonts are displayed,
19751 the truncation glyphs may need more pixels than the row's
19752 glyphs they overwrite. We overwrite more glyphs to free
19753 enough screen real estate, and enlarge the stretch glyph
19754 on the right (see display_line), if there is one, to
19755 preserve the screen position of the truncation glyphs on
19756 the right. */
19757 int w = 0;
19758 struct glyph *g = to;
19759 short used;
19761 /* The first glyph could be partially visible, in which case
19762 it->glyph_row->x will be negative. But we want the left
19763 truncation glyphs to be aligned at the left margin of the
19764 window, so we override the x coordinate at which the row
19765 will begin. */
19766 it->glyph_row->x = 0;
19767 while (g < toend && w < it->truncation_pixel_width)
19769 w += g->pixel_width;
19770 ++g;
19772 if (g - to - tused > 0)
19774 memmove (to + tused, g, (toend - g) * sizeof(*g));
19775 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19777 used = it->glyph_row->used[TEXT_AREA];
19778 if (it->glyph_row->truncated_on_right_p
19779 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19780 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19781 == STRETCH_GLYPH)
19783 int extra = w - it->truncation_pixel_width;
19785 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19789 while (from < end)
19790 *to++ = *from++;
19792 /* There may be padding glyphs left over. Overwrite them too. */
19793 if (!FRAME_WINDOW_P (it->f))
19795 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19797 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19798 while (from < end)
19799 *to++ = *from++;
19803 if (to > toend)
19804 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19806 else
19808 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19810 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19811 that back to front. */
19812 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19813 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19814 toend = it->glyph_row->glyphs[TEXT_AREA];
19815 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19816 if (FRAME_WINDOW_P (it->f))
19818 int w = 0;
19819 struct glyph *g = to;
19821 while (g >= toend && w < it->truncation_pixel_width)
19823 w += g->pixel_width;
19824 --g;
19826 if (to - g - tused > 0)
19827 to = g + tused;
19828 if (it->glyph_row->truncated_on_right_p
19829 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19830 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19832 int extra = w - it->truncation_pixel_width;
19834 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19838 while (from >= end && to >= toend)
19839 *to-- = *from--;
19840 if (!FRAME_WINDOW_P (it->f))
19842 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19844 from =
19845 truncate_it.glyph_row->glyphs[TEXT_AREA]
19846 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19847 while (from >= end && to >= toend)
19848 *to-- = *from--;
19851 if (from >= end)
19853 /* Need to free some room before prepending additional
19854 glyphs. */
19855 int move_by = from - end + 1;
19856 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19857 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19859 for ( ; g >= g0; g--)
19860 g[move_by] = *g;
19861 while (from >= end)
19862 *to-- = *from--;
19863 it->glyph_row->used[TEXT_AREA] += move_by;
19868 /* Compute the hash code for ROW. */
19869 unsigned
19870 row_hash (struct glyph_row *row)
19872 int area, k;
19873 unsigned hashval = 0;
19875 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19876 for (k = 0; k < row->used[area]; ++k)
19877 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19878 + row->glyphs[area][k].u.val
19879 + row->glyphs[area][k].face_id
19880 + row->glyphs[area][k].padding_p
19881 + (row->glyphs[area][k].type << 2));
19883 return hashval;
19886 /* Compute the pixel height and width of IT->glyph_row.
19888 Most of the time, ascent and height of a display line will be equal
19889 to the max_ascent and max_height values of the display iterator
19890 structure. This is not the case if
19892 1. We hit ZV without displaying anything. In this case, max_ascent
19893 and max_height will be zero.
19895 2. We have some glyphs that don't contribute to the line height.
19896 (The glyph row flag contributes_to_line_height_p is for future
19897 pixmap extensions).
19899 The first case is easily covered by using default values because in
19900 these cases, the line height does not really matter, except that it
19901 must not be zero. */
19903 static void
19904 compute_line_metrics (struct it *it)
19906 struct glyph_row *row = it->glyph_row;
19908 if (FRAME_WINDOW_P (it->f))
19910 int i, min_y, max_y;
19912 /* The line may consist of one space only, that was added to
19913 place the cursor on it. If so, the row's height hasn't been
19914 computed yet. */
19915 if (row->height == 0)
19917 if (it->max_ascent + it->max_descent == 0)
19918 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19919 row->ascent = it->max_ascent;
19920 row->height = it->max_ascent + it->max_descent;
19921 row->phys_ascent = it->max_phys_ascent;
19922 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19923 row->extra_line_spacing = it->max_extra_line_spacing;
19926 /* Compute the width of this line. */
19927 row->pixel_width = row->x;
19928 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19929 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19931 eassert (row->pixel_width >= 0);
19932 eassert (row->ascent >= 0 && row->height > 0);
19934 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19935 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19937 /* If first line's physical ascent is larger than its logical
19938 ascent, use the physical ascent, and make the row taller.
19939 This makes accented characters fully visible. */
19940 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19941 && row->phys_ascent > row->ascent)
19943 row->height += row->phys_ascent - row->ascent;
19944 row->ascent = row->phys_ascent;
19947 /* Compute how much of the line is visible. */
19948 row->visible_height = row->height;
19950 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19951 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19953 if (row->y < min_y)
19954 row->visible_height -= min_y - row->y;
19955 if (row->y + row->height > max_y)
19956 row->visible_height -= row->y + row->height - max_y;
19958 else
19960 row->pixel_width = row->used[TEXT_AREA];
19961 if (row->continued_p)
19962 row->pixel_width -= it->continuation_pixel_width;
19963 else if (row->truncated_on_right_p)
19964 row->pixel_width -= it->truncation_pixel_width;
19965 row->ascent = row->phys_ascent = 0;
19966 row->height = row->phys_height = row->visible_height = 1;
19967 row->extra_line_spacing = 0;
19970 /* Compute a hash code for this row. */
19971 row->hash = row_hash (row);
19973 it->max_ascent = it->max_descent = 0;
19974 it->max_phys_ascent = it->max_phys_descent = 0;
19978 /* Append one space to the glyph row of iterator IT if doing a
19979 window-based redisplay. The space has the same face as
19980 IT->face_id. Value is true if a space was added.
19982 This function is called to make sure that there is always one glyph
19983 at the end of a glyph row that the cursor can be set on under
19984 window-systems. (If there weren't such a glyph we would not know
19985 how wide and tall a box cursor should be displayed).
19987 At the same time this space let's a nicely handle clearing to the
19988 end of the line if the row ends in italic text. */
19990 static bool
19991 append_space_for_newline (struct it *it, bool default_face_p)
19993 if (FRAME_WINDOW_P (it->f))
19995 int n = it->glyph_row->used[TEXT_AREA];
19997 if (it->glyph_row->glyphs[TEXT_AREA] + n
19998 < it->glyph_row->glyphs[1 + TEXT_AREA])
20000 /* Save some values that must not be changed.
20001 Must save IT->c and IT->len because otherwise
20002 ITERATOR_AT_END_P wouldn't work anymore after
20003 append_space_for_newline has been called. */
20004 enum display_element_type saved_what = it->what;
20005 int saved_c = it->c, saved_len = it->len;
20006 int saved_char_to_display = it->char_to_display;
20007 int saved_x = it->current_x;
20008 int saved_face_id = it->face_id;
20009 bool saved_box_end = it->end_of_box_run_p;
20010 struct text_pos saved_pos;
20011 Lisp_Object saved_object;
20012 struct face *face;
20014 saved_object = it->object;
20015 saved_pos = it->position;
20017 it->what = IT_CHARACTER;
20018 memset (&it->position, 0, sizeof it->position);
20019 it->object = Qnil;
20020 it->c = it->char_to_display = ' ';
20021 it->len = 1;
20023 /* If the default face was remapped, be sure to use the
20024 remapped face for the appended newline. */
20025 if (default_face_p)
20026 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
20027 else if (it->face_before_selective_p)
20028 it->face_id = it->saved_face_id;
20029 face = FACE_FROM_ID (it->f, it->face_id);
20030 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
20031 /* In R2L rows, we will prepend a stretch glyph that will
20032 have the end_of_box_run_p flag set for it, so there's no
20033 need for the appended newline glyph to have that flag
20034 set. */
20035 if (it->glyph_row->reversed_p
20036 /* But if the appended newline glyph goes all the way to
20037 the end of the row, there will be no stretch glyph,
20038 so leave the box flag set. */
20039 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
20040 it->end_of_box_run_p = false;
20042 PRODUCE_GLYPHS (it);
20044 #ifdef HAVE_WINDOW_SYSTEM
20045 /* Make sure this space glyph has the right ascent and
20046 descent values, or else cursor at end of line will look
20047 funny, and height of empty lines will be incorrect. */
20048 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
20049 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20050 if (n == 0)
20052 Lisp_Object height, total_height;
20053 int extra_line_spacing = it->extra_line_spacing;
20054 int boff = font->baseline_offset;
20056 if (font->vertical_centering)
20057 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20059 it->object = saved_object; /* get_it_property needs this */
20060 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
20061 /* Must do a subset of line height processing from
20062 x_produce_glyph for newline characters. */
20063 height = get_it_property (it, Qline_height);
20064 if (CONSP (height)
20065 && CONSP (XCDR (height))
20066 && NILP (XCDR (XCDR (height))))
20068 total_height = XCAR (XCDR (height));
20069 height = XCAR (height);
20071 else
20072 total_height = Qnil;
20073 height = calc_line_height_property (it, height, font, boff, true);
20075 if (it->override_ascent >= 0)
20077 it->ascent = it->override_ascent;
20078 it->descent = it->override_descent;
20079 boff = it->override_boff;
20081 if (EQ (height, Qt))
20082 extra_line_spacing = 0;
20083 else
20085 Lisp_Object spacing;
20087 it->phys_ascent = it->ascent;
20088 it->phys_descent = it->descent;
20089 if (!NILP (height)
20090 && XINT (height) > it->ascent + it->descent)
20091 it->ascent = XINT (height) - it->descent;
20093 if (!NILP (total_height))
20094 spacing = calc_line_height_property (it, total_height, font,
20095 boff, false);
20096 else
20098 spacing = get_it_property (it, Qline_spacing);
20099 spacing = calc_line_height_property (it, spacing, font,
20100 boff, false);
20102 if (INTEGERP (spacing))
20104 extra_line_spacing = XINT (spacing);
20105 if (!NILP (total_height))
20106 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20109 if (extra_line_spacing > 0)
20111 it->descent += extra_line_spacing;
20112 if (extra_line_spacing > it->max_extra_line_spacing)
20113 it->max_extra_line_spacing = extra_line_spacing;
20115 it->max_ascent = it->ascent;
20116 it->max_descent = it->descent;
20117 /* Make sure compute_line_metrics recomputes the row height. */
20118 it->glyph_row->height = 0;
20121 g->ascent = it->max_ascent;
20122 g->descent = it->max_descent;
20123 #endif
20125 it->override_ascent = -1;
20126 it->constrain_row_ascent_descent_p = false;
20127 it->current_x = saved_x;
20128 it->object = saved_object;
20129 it->position = saved_pos;
20130 it->what = saved_what;
20131 it->face_id = saved_face_id;
20132 it->len = saved_len;
20133 it->c = saved_c;
20134 it->char_to_display = saved_char_to_display;
20135 it->end_of_box_run_p = saved_box_end;
20136 return true;
20140 return false;
20144 /* Extend the face of the last glyph in the text area of IT->glyph_row
20145 to the end of the display line. Called from display_line. If the
20146 glyph row is empty, add a space glyph to it so that we know the
20147 face to draw. Set the glyph row flag fill_line_p. If the glyph
20148 row is R2L, prepend a stretch glyph to cover the empty space to the
20149 left of the leftmost glyph. */
20151 static void
20152 extend_face_to_end_of_line (struct it *it)
20154 struct face *face, *default_face;
20155 struct frame *f = it->f;
20157 /* If line is already filled, do nothing. Non window-system frames
20158 get a grace of one more ``pixel'' because their characters are
20159 1-``pixel'' wide, so they hit the equality too early. This grace
20160 is needed only for R2L rows that are not continued, to produce
20161 one extra blank where we could display the cursor. */
20162 if ((it->current_x >= it->last_visible_x
20163 + (!FRAME_WINDOW_P (f)
20164 && it->glyph_row->reversed_p
20165 && !it->glyph_row->continued_p))
20166 /* If the window has display margins, we will need to extend
20167 their face even if the text area is filled. */
20168 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20169 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20170 return;
20172 /* The default face, possibly remapped. */
20173 default_face = FACE_FROM_ID_OR_NULL (f,
20174 lookup_basic_face (f, DEFAULT_FACE_ID));
20176 /* Face extension extends the background and box of IT->face_id
20177 to the end of the line. If the background equals the background
20178 of the frame, we don't have to do anything. */
20179 face = FACE_FROM_ID (f, (it->face_before_selective_p
20180 ? it->saved_face_id
20181 : it->face_id));
20183 if (FRAME_WINDOW_P (f)
20184 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20185 && face->box == FACE_NO_BOX
20186 && face->background == FRAME_BACKGROUND_PIXEL (f)
20187 #ifdef HAVE_WINDOW_SYSTEM
20188 && !face->stipple
20189 #endif
20190 && !it->glyph_row->reversed_p)
20191 return;
20193 /* Set the glyph row flag indicating that the face of the last glyph
20194 in the text area has to be drawn to the end of the text area. */
20195 it->glyph_row->fill_line_p = true;
20197 /* If current character of IT is not ASCII, make sure we have the
20198 ASCII face. This will be automatically undone the next time
20199 get_next_display_element returns a multibyte character. Note
20200 that the character will always be single byte in unibyte
20201 text. */
20202 if (!ASCII_CHAR_P (it->c))
20204 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20207 if (FRAME_WINDOW_P (f))
20209 /* If the row is empty, add a space with the current face of IT,
20210 so that we know which face to draw. */
20211 if (it->glyph_row->used[TEXT_AREA] == 0)
20213 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20214 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20215 it->glyph_row->used[TEXT_AREA] = 1;
20217 /* Mode line and the header line don't have margins, and
20218 likewise the frame's tool-bar window, if there is any. */
20219 if (!(it->glyph_row->mode_line_p
20220 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20221 || (WINDOWP (f->tool_bar_window)
20222 && it->w == XWINDOW (f->tool_bar_window))
20223 #endif
20226 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20227 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20229 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20230 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20231 default_face->id;
20232 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20234 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20235 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20237 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20238 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20239 default_face->id;
20240 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20243 #ifdef HAVE_WINDOW_SYSTEM
20244 if (it->glyph_row->reversed_p)
20246 /* Prepend a stretch glyph to the row, such that the
20247 rightmost glyph will be drawn flushed all the way to the
20248 right margin of the window. The stretch glyph that will
20249 occupy the empty space, if any, to the left of the
20250 glyphs. */
20251 struct font *font = face->font ? face->font : FRAME_FONT (f);
20252 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20253 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20254 struct glyph *g;
20255 int row_width, stretch_ascent, stretch_width;
20256 struct text_pos saved_pos;
20257 int saved_face_id;
20258 bool saved_avoid_cursor, saved_box_start;
20260 for (row_width = 0, g = row_start; g < row_end; g++)
20261 row_width += g->pixel_width;
20263 /* FIXME: There are various minor display glitches in R2L
20264 rows when only one of the fringes is missing. The
20265 strange condition below produces the least bad effect. */
20266 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20267 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20268 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20269 stretch_width = window_box_width (it->w, TEXT_AREA);
20270 else
20271 stretch_width = it->last_visible_x - it->first_visible_x;
20272 stretch_width -= row_width;
20274 if (stretch_width > 0)
20276 stretch_ascent =
20277 (((it->ascent + it->descent)
20278 * FONT_BASE (font)) / FONT_HEIGHT (font));
20279 saved_pos = it->position;
20280 memset (&it->position, 0, sizeof it->position);
20281 saved_avoid_cursor = it->avoid_cursor_p;
20282 it->avoid_cursor_p = true;
20283 saved_face_id = it->face_id;
20284 saved_box_start = it->start_of_box_run_p;
20285 /* The last row's stretch glyph should get the default
20286 face, to avoid painting the rest of the window with
20287 the region face, if the region ends at ZV. */
20288 if (it->glyph_row->ends_at_zv_p)
20289 it->face_id = default_face->id;
20290 else
20291 it->face_id = face->id;
20292 it->start_of_box_run_p = false;
20293 append_stretch_glyph (it, Qnil, stretch_width,
20294 it->ascent + it->descent, stretch_ascent);
20295 it->position = saved_pos;
20296 it->avoid_cursor_p = saved_avoid_cursor;
20297 it->face_id = saved_face_id;
20298 it->start_of_box_run_p = saved_box_start;
20300 /* If stretch_width comes out negative, it means that the
20301 last glyph is only partially visible. In R2L rows, we
20302 want the leftmost glyph to be partially visible, so we
20303 need to give the row the corresponding left offset. */
20304 if (stretch_width < 0)
20305 it->glyph_row->x = stretch_width;
20307 #endif /* HAVE_WINDOW_SYSTEM */
20309 else
20311 /* Save some values that must not be changed. */
20312 int saved_x = it->current_x;
20313 struct text_pos saved_pos;
20314 Lisp_Object saved_object;
20315 enum display_element_type saved_what = it->what;
20316 int saved_face_id = it->face_id;
20318 saved_object = it->object;
20319 saved_pos = it->position;
20321 it->what = IT_CHARACTER;
20322 memset (&it->position, 0, sizeof it->position);
20323 it->object = Qnil;
20324 it->c = it->char_to_display = ' ';
20325 it->len = 1;
20327 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20328 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20329 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20330 && !it->glyph_row->mode_line_p
20331 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20333 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20334 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20336 for (it->current_x = 0; g < e; g++)
20337 it->current_x += g->pixel_width;
20339 it->area = LEFT_MARGIN_AREA;
20340 it->face_id = default_face->id;
20341 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20342 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20343 && g < it->glyph_row->glyphs[TEXT_AREA])
20345 PRODUCE_GLYPHS (it);
20346 /* term.c:produce_glyphs advances it->current_x only for
20347 TEXT_AREA. */
20348 it->current_x += it->pixel_width;
20349 g++;
20352 it->current_x = saved_x;
20353 it->area = TEXT_AREA;
20356 /* The last row's blank glyphs should get the default face, to
20357 avoid painting the rest of the window with the region face,
20358 if the region ends at ZV. */
20359 if (it->glyph_row->ends_at_zv_p)
20360 it->face_id = default_face->id;
20361 else
20362 it->face_id = face->id;
20363 PRODUCE_GLYPHS (it);
20365 while (it->current_x <= it->last_visible_x)
20366 PRODUCE_GLYPHS (it);
20368 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20369 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20370 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20371 && !it->glyph_row->mode_line_p
20372 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20374 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20375 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20377 for ( ; g < e; g++)
20378 it->current_x += g->pixel_width;
20380 it->area = RIGHT_MARGIN_AREA;
20381 it->face_id = default_face->id;
20382 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20383 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20384 && g < it->glyph_row->glyphs[LAST_AREA])
20386 PRODUCE_GLYPHS (it);
20387 it->current_x += it->pixel_width;
20388 g++;
20391 it->area = TEXT_AREA;
20394 /* Don't count these blanks really. It would let us insert a left
20395 truncation glyph below and make us set the cursor on them, maybe. */
20396 it->current_x = saved_x;
20397 it->object = saved_object;
20398 it->position = saved_pos;
20399 it->what = saved_what;
20400 it->face_id = saved_face_id;
20405 /* Value is true if text starting at CHARPOS in current_buffer is
20406 trailing whitespace. */
20408 static bool
20409 trailing_whitespace_p (ptrdiff_t charpos)
20411 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20412 int c = 0;
20414 while (bytepos < ZV_BYTE
20415 && (c = FETCH_CHAR (bytepos),
20416 c == ' ' || c == '\t'))
20417 ++bytepos;
20419 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20421 if (bytepos != PT_BYTE)
20422 return true;
20424 return false;
20428 /* Highlight trailing whitespace, if any, in ROW. */
20430 static void
20431 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20433 int used = row->used[TEXT_AREA];
20435 if (used)
20437 struct glyph *start = row->glyphs[TEXT_AREA];
20438 struct glyph *glyph = start + used - 1;
20440 if (row->reversed_p)
20442 /* Right-to-left rows need to be processed in the opposite
20443 direction, so swap the edge pointers. */
20444 glyph = start;
20445 start = row->glyphs[TEXT_AREA] + used - 1;
20448 /* Skip over glyphs inserted to display the cursor at the
20449 end of a line, for extending the face of the last glyph
20450 to the end of the line on terminals, and for truncation
20451 and continuation glyphs. */
20452 if (!row->reversed_p)
20454 while (glyph >= start
20455 && glyph->type == CHAR_GLYPH
20456 && NILP (glyph->object))
20457 --glyph;
20459 else
20461 while (glyph <= start
20462 && glyph->type == CHAR_GLYPH
20463 && NILP (glyph->object))
20464 ++glyph;
20467 /* If last glyph is a space or stretch, and it's trailing
20468 whitespace, set the face of all trailing whitespace glyphs in
20469 IT->glyph_row to `trailing-whitespace'. */
20470 if ((row->reversed_p ? glyph <= start : glyph >= start)
20471 && BUFFERP (glyph->object)
20472 && (glyph->type == STRETCH_GLYPH
20473 || (glyph->type == CHAR_GLYPH
20474 && glyph->u.ch == ' '))
20475 && trailing_whitespace_p (glyph->charpos))
20477 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20478 if (face_id < 0)
20479 return;
20481 if (!row->reversed_p)
20483 while (glyph >= start
20484 && BUFFERP (glyph->object)
20485 && (glyph->type == STRETCH_GLYPH
20486 || (glyph->type == CHAR_GLYPH
20487 && glyph->u.ch == ' ')))
20488 (glyph--)->face_id = face_id;
20490 else
20492 while (glyph <= start
20493 && BUFFERP (glyph->object)
20494 && (glyph->type == STRETCH_GLYPH
20495 || (glyph->type == CHAR_GLYPH
20496 && glyph->u.ch == ' ')))
20497 (glyph++)->face_id = face_id;
20504 /* Value is true if glyph row ROW should be
20505 considered to hold the buffer position CHARPOS. */
20507 static bool
20508 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20510 bool result = true;
20512 if (charpos == CHARPOS (row->end.pos)
20513 || charpos == MATRIX_ROW_END_CHARPOS (row))
20515 /* Suppose the row ends on a string.
20516 Unless the row is continued, that means it ends on a newline
20517 in the string. If it's anything other than a display string
20518 (e.g., a before-string from an overlay), we don't want the
20519 cursor there. (This heuristic seems to give the optimal
20520 behavior for the various types of multi-line strings.)
20521 One exception: if the string has `cursor' property on one of
20522 its characters, we _do_ want the cursor there. */
20523 if (CHARPOS (row->end.string_pos) >= 0)
20525 if (row->continued_p)
20526 result = true;
20527 else
20529 /* Check for `display' property. */
20530 struct glyph *beg = row->glyphs[TEXT_AREA];
20531 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20532 struct glyph *glyph;
20534 result = false;
20535 for (glyph = end; glyph >= beg; --glyph)
20536 if (STRINGP (glyph->object))
20538 Lisp_Object prop
20539 = Fget_char_property (make_number (charpos),
20540 Qdisplay, Qnil);
20541 result =
20542 (!NILP (prop)
20543 && display_prop_string_p (prop, glyph->object));
20544 /* If there's a `cursor' property on one of the
20545 string's characters, this row is a cursor row,
20546 even though this is not a display string. */
20547 if (!result)
20549 Lisp_Object s = glyph->object;
20551 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20553 ptrdiff_t gpos = glyph->charpos;
20555 if (!NILP (Fget_char_property (make_number (gpos),
20556 Qcursor, s)))
20558 result = true;
20559 break;
20563 break;
20567 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20569 /* If the row ends in middle of a real character,
20570 and the line is continued, we want the cursor here.
20571 That's because CHARPOS (ROW->end.pos) would equal
20572 PT if PT is before the character. */
20573 if (!row->ends_in_ellipsis_p)
20574 result = row->continued_p;
20575 else
20576 /* If the row ends in an ellipsis, then
20577 CHARPOS (ROW->end.pos) will equal point after the
20578 invisible text. We want that position to be displayed
20579 after the ellipsis. */
20580 result = false;
20582 /* If the row ends at ZV, display the cursor at the end of that
20583 row instead of at the start of the row below. */
20584 else
20585 result = row->ends_at_zv_p;
20588 return result;
20591 /* Value is true if glyph row ROW should be
20592 used to hold the cursor. */
20594 static bool
20595 cursor_row_p (struct glyph_row *row)
20597 return row_for_charpos_p (row, PT);
20602 /* Push the property PROP so that it will be rendered at the current
20603 position in IT. Return true if PROP was successfully pushed, false
20604 otherwise. Called from handle_line_prefix to handle the
20605 `line-prefix' and `wrap-prefix' properties. */
20607 static bool
20608 push_prefix_prop (struct it *it, Lisp_Object prop)
20610 struct text_pos pos =
20611 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20613 eassert (it->method == GET_FROM_BUFFER
20614 || it->method == GET_FROM_DISPLAY_VECTOR
20615 || it->method == GET_FROM_STRING
20616 || it->method == GET_FROM_IMAGE);
20618 /* We need to save the current buffer/string position, so it will be
20619 restored by pop_it, because iterate_out_of_display_property
20620 depends on that being set correctly, but some situations leave
20621 it->position not yet set when this function is called. */
20622 push_it (it, &pos);
20624 if (STRINGP (prop))
20626 if (SCHARS (prop) == 0)
20628 pop_it (it);
20629 return false;
20632 it->string = prop;
20633 it->string_from_prefix_prop_p = true;
20634 it->multibyte_p = STRING_MULTIBYTE (it->string);
20635 it->current.overlay_string_index = -1;
20636 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20637 it->end_charpos = it->string_nchars = SCHARS (it->string);
20638 it->method = GET_FROM_STRING;
20639 it->stop_charpos = 0;
20640 it->prev_stop = 0;
20641 it->base_level_stop = 0;
20642 it->cmp_it.id = -1;
20644 /* Force paragraph direction to be that of the parent
20645 buffer/string. */
20646 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20647 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20648 else
20649 it->paragraph_embedding = L2R;
20651 /* Set up the bidi iterator for this display string. */
20652 if (it->bidi_p)
20654 it->bidi_it.string.lstring = it->string;
20655 it->bidi_it.string.s = NULL;
20656 it->bidi_it.string.schars = it->end_charpos;
20657 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20658 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20659 it->bidi_it.string.unibyte = !it->multibyte_p;
20660 it->bidi_it.w = it->w;
20661 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20664 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20666 it->method = GET_FROM_STRETCH;
20667 it->object = prop;
20669 #ifdef HAVE_WINDOW_SYSTEM
20670 else if (IMAGEP (prop))
20672 it->what = IT_IMAGE;
20673 it->image_id = lookup_image (it->f, prop);
20674 it->method = GET_FROM_IMAGE;
20676 #endif /* HAVE_WINDOW_SYSTEM */
20677 else
20679 pop_it (it); /* bogus display property, give up */
20680 return false;
20683 return true;
20686 /* Return the character-property PROP at the current position in IT. */
20688 static Lisp_Object
20689 get_it_property (struct it *it, Lisp_Object prop)
20691 Lisp_Object position, object = it->object;
20693 if (STRINGP (object))
20694 position = make_number (IT_STRING_CHARPOS (*it));
20695 else if (BUFFERP (object))
20697 position = make_number (IT_CHARPOS (*it));
20698 object = it->window;
20700 else
20701 return Qnil;
20703 return Fget_char_property (position, prop, object);
20706 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20708 static void
20709 handle_line_prefix (struct it *it)
20711 Lisp_Object prefix;
20713 if (it->continuation_lines_width > 0)
20715 prefix = get_it_property (it, Qwrap_prefix);
20716 if (NILP (prefix))
20717 prefix = Vwrap_prefix;
20719 else
20721 prefix = get_it_property (it, Qline_prefix);
20722 if (NILP (prefix))
20723 prefix = Vline_prefix;
20725 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20727 /* If the prefix is wider than the window, and we try to wrap
20728 it, it would acquire its own wrap prefix, and so on till the
20729 iterator stack overflows. So, don't wrap the prefix. */
20730 it->line_wrap = TRUNCATE;
20731 it->avoid_cursor_p = true;
20737 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20738 only for R2L lines from display_line and display_string, when they
20739 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20740 the line/string needs to be continued on the next glyph row. */
20741 static void
20742 unproduce_glyphs (struct it *it, int n)
20744 struct glyph *glyph, *end;
20746 eassert (it->glyph_row);
20747 eassert (it->glyph_row->reversed_p);
20748 eassert (it->area == TEXT_AREA);
20749 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20751 if (n > it->glyph_row->used[TEXT_AREA])
20752 n = it->glyph_row->used[TEXT_AREA];
20753 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20754 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20755 for ( ; glyph < end; glyph++)
20756 glyph[-n] = *glyph;
20759 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20760 and ROW->maxpos. */
20761 static void
20762 find_row_edges (struct it *it, struct glyph_row *row,
20763 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20764 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20766 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20767 lines' rows is implemented for bidi-reordered rows. */
20769 /* ROW->minpos is the value of min_pos, the minimal buffer position
20770 we have in ROW, or ROW->start.pos if that is smaller. */
20771 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20772 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20773 else
20774 /* We didn't find buffer positions smaller than ROW->start, or
20775 didn't find _any_ valid buffer positions in any of the glyphs,
20776 so we must trust the iterator's computed positions. */
20777 row->minpos = row->start.pos;
20778 if (max_pos <= 0)
20780 max_pos = CHARPOS (it->current.pos);
20781 max_bpos = BYTEPOS (it->current.pos);
20784 /* Here are the various use-cases for ending the row, and the
20785 corresponding values for ROW->maxpos:
20787 Line ends in a newline from buffer eol_pos + 1
20788 Line is continued from buffer max_pos + 1
20789 Line is truncated on right it->current.pos
20790 Line ends in a newline from string max_pos + 1(*)
20791 (*) + 1 only when line ends in a forward scan
20792 Line is continued from string max_pos
20793 Line is continued from display vector max_pos
20794 Line is entirely from a string min_pos == max_pos
20795 Line is entirely from a display vector min_pos == max_pos
20796 Line that ends at ZV ZV
20798 If you discover other use-cases, please add them here as
20799 appropriate. */
20800 if (row->ends_at_zv_p)
20801 row->maxpos = it->current.pos;
20802 else if (row->used[TEXT_AREA])
20804 bool seen_this_string = false;
20805 struct glyph_row *r1 = row - 1;
20807 /* Did we see the same display string on the previous row? */
20808 if (STRINGP (it->object)
20809 /* this is not the first row */
20810 && row > it->w->desired_matrix->rows
20811 /* previous row is not the header line */
20812 && !r1->mode_line_p
20813 /* previous row also ends in a newline from a string */
20814 && r1->ends_in_newline_from_string_p)
20816 struct glyph *start, *end;
20818 /* Search for the last glyph of the previous row that came
20819 from buffer or string. Depending on whether the row is
20820 L2R or R2L, we need to process it front to back or the
20821 other way round. */
20822 if (!r1->reversed_p)
20824 start = r1->glyphs[TEXT_AREA];
20825 end = start + r1->used[TEXT_AREA];
20826 /* Glyphs inserted by redisplay have nil as their object. */
20827 while (end > start
20828 && NILP ((end - 1)->object)
20829 && (end - 1)->charpos <= 0)
20830 --end;
20831 if (end > start)
20833 if (EQ ((end - 1)->object, it->object))
20834 seen_this_string = true;
20836 else
20837 /* If all the glyphs of the previous row were inserted
20838 by redisplay, it means the previous row was
20839 produced from a single newline, which is only
20840 possible if that newline came from the same string
20841 as the one which produced this ROW. */
20842 seen_this_string = true;
20844 else
20846 end = r1->glyphs[TEXT_AREA] - 1;
20847 start = end + r1->used[TEXT_AREA];
20848 while (end < start
20849 && NILP ((end + 1)->object)
20850 && (end + 1)->charpos <= 0)
20851 ++end;
20852 if (end < start)
20854 if (EQ ((end + 1)->object, it->object))
20855 seen_this_string = true;
20857 else
20858 seen_this_string = true;
20861 /* Take note of each display string that covers a newline only
20862 once, the first time we see it. This is for when a display
20863 string includes more than one newline in it. */
20864 if (row->ends_in_newline_from_string_p && !seen_this_string)
20866 /* If we were scanning the buffer forward when we displayed
20867 the string, we want to account for at least one buffer
20868 position that belongs to this row (position covered by
20869 the display string), so that cursor positioning will
20870 consider this row as a candidate when point is at the end
20871 of the visual line represented by this row. This is not
20872 required when scanning back, because max_pos will already
20873 have a much larger value. */
20874 if (CHARPOS (row->end.pos) > max_pos)
20875 INC_BOTH (max_pos, max_bpos);
20876 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20878 else if (CHARPOS (it->eol_pos) > 0)
20879 SET_TEXT_POS (row->maxpos,
20880 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20881 else if (row->continued_p)
20883 /* If max_pos is different from IT's current position, it
20884 means IT->method does not belong to the display element
20885 at max_pos. However, it also means that the display
20886 element at max_pos was displayed in its entirety on this
20887 line, which is equivalent to saying that the next line
20888 starts at the next buffer position. */
20889 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20890 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20891 else
20893 INC_BOTH (max_pos, max_bpos);
20894 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20897 else if (row->truncated_on_right_p)
20898 /* display_line already called reseat_at_next_visible_line_start,
20899 which puts the iterator at the beginning of the next line, in
20900 the logical order. */
20901 row->maxpos = it->current.pos;
20902 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20903 /* A line that is entirely from a string/image/stretch... */
20904 row->maxpos = row->minpos;
20905 else
20906 emacs_abort ();
20908 else
20909 row->maxpos = it->current.pos;
20912 /* Like display_count_lines, but capable of counting outside of the
20913 current narrowed region. */
20914 static ptrdiff_t
20915 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20916 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20918 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20919 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20921 ptrdiff_t val;
20922 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20923 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20924 Fwiden ();
20925 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20926 unbind_to (pdl_count, Qnil);
20927 return val;
20930 /* Count the number of screen lines in window IT->w between character
20931 position IT_CHARPOS(*IT) and the line showing that window's point. */
20932 static ptrdiff_t
20933 display_count_lines_visually (struct it *it)
20935 struct it tem_it;
20936 ptrdiff_t to;
20937 struct text_pos from;
20939 /* If we already calculated a relative line number, use that. This
20940 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20941 are laid out sequentially, one by one, for each sequence of calls
20942 to display_line or other similar function that follows a call to
20943 init_iterator. */
20944 if (it->lnum_bytepos > 0)
20945 return it->lnum + 1;
20946 else
20948 ptrdiff_t count = SPECPDL_INDEX ();
20950 if (IT_CHARPOS (*it) <= PT)
20952 from = it->current.pos;
20953 to = PT;
20955 else
20957 SET_TEXT_POS (from, PT, PT_BYTE);
20958 to = IT_CHARPOS (*it);
20960 start_display (&tem_it, it->w, from);
20961 /* Need to disable visual mode temporarily, since otherwise the
20962 call to move_it_to will cause infinite recursion. */
20963 specbind (Qdisplay_line_numbers, Qrelative);
20964 /* Some redisplay optimizations could invoke us very far from
20965 PT, which will make the caller painfully slow. There should
20966 be no need to go too far beyond the window's bottom, as any
20967 such optimization will fail to show point anyway. */
20968 move_it_to (&tem_it, to, -1,
20969 tem_it.last_visible_y
20970 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20971 -1, MOVE_TO_POS | MOVE_TO_Y);
20972 unbind_to (count, Qnil);
20973 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20977 /* Produce the line-number glyphs for the current glyph_row. If
20978 IT->glyph_row is non-NULL, populate the row with the produced
20979 glyphs. */
20980 static void
20981 maybe_produce_line_number (struct it *it)
20983 ptrdiff_t last_line = it->lnum;
20984 ptrdiff_t start_from, bytepos;
20985 ptrdiff_t this_line;
20986 bool first_time = false;
20987 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20988 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20989 void *itdata = bidi_shelve_cache ();
20991 if (EQ (Vdisplay_line_numbers, Qvisual))
20992 this_line = display_count_lines_visually (it);
20993 else
20995 if (!last_line)
20997 /* If possible, reuse data cached by line-number-mode. */
20998 if (it->w->base_line_number > 0
20999 && it->w->base_line_pos > 0
21000 && it->w->base_line_pos <= IT_CHARPOS (*it)
21001 /* line-number-mode always displays narrowed line
21002 numbers, so we cannot use its data if the user wants
21003 line numbers that disregard narrowing, or if the
21004 buffer's narrowing has just changed. */
21005 && !(display_line_numbers_widen
21006 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
21007 && !current_buffer->clip_changed)
21009 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
21010 last_line = it->w->base_line_number - 1;
21012 else
21013 start_from = beg_byte;
21014 if (!it->lnum_bytepos)
21015 first_time = true;
21017 else
21018 start_from = it->lnum_bytepos;
21020 /* Paranoia: what if someone changes the narrowing since the
21021 last time display_line was called? Shouldn't really happen,
21022 but who knows what some crazy Lisp invoked by :eval could do? */
21023 if (!(beg_byte <= start_from && start_from <= z_byte))
21025 last_line = 0;
21026 start_from = beg_byte;
21029 this_line =
21030 last_line + display_count_lines_logically (start_from,
21031 IT_BYTEPOS (*it),
21032 IT_CHARPOS (*it), &bytepos);
21033 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
21034 eassert (bytepos == IT_BYTEPOS (*it));
21037 /* Record the line number information. */
21038 if (this_line != last_line || !it->lnum_bytepos)
21040 it->lnum = this_line;
21041 it->lnum_bytepos = IT_BYTEPOS (*it);
21044 /* Produce the glyphs for the line number. */
21045 struct it tem_it;
21046 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
21047 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
21048 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
21049 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
21050 int current_lnum_face_id
21051 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
21052 /* Compute point's line number if needed. */
21053 if ((EQ (Vdisplay_line_numbers, Qrelative)
21054 || EQ (Vdisplay_line_numbers, Qvisual)
21055 || lnum_face_id != current_lnum_face_id)
21056 && !it->pt_lnum)
21058 ptrdiff_t ignored;
21059 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
21060 it->pt_lnum =
21061 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
21062 PT, &ignored);
21063 else
21064 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21065 &ignored);
21067 /* Compute the required width if needed. */
21068 if (!it->lnum_width)
21070 if (NATNUMP (Vdisplay_line_numbers_width))
21071 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21073 /* Max line number to be displayed cannot be more than the one
21074 corresponding to the last row of the desired matrix. */
21075 ptrdiff_t max_lnum;
21077 if (NILP (Vdisplay_line_numbers_current_absolute)
21078 && (EQ (Vdisplay_line_numbers, Qrelative)
21079 || EQ (Vdisplay_line_numbers, Qvisual)))
21080 /* We subtract one more because the current line is always
21081 zero in this mode. */
21082 max_lnum = it->w->desired_matrix->nrows - 2;
21083 else if (EQ (Vdisplay_line_numbers, Qvisual))
21084 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21085 else
21086 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21087 max_lnum = max (1, max_lnum);
21088 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21089 eassert (it->lnum_width > 0);
21091 if (EQ (Vdisplay_line_numbers, Qrelative))
21092 lnum_offset = it->pt_lnum;
21093 else if (EQ (Vdisplay_line_numbers, Qvisual))
21094 lnum_offset = 0;
21096 /* Under 'relative', display the absolute line number for the
21097 current line, unless the user requests otherwise. */
21098 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21099 if ((EQ (Vdisplay_line_numbers, Qrelative)
21100 || EQ (Vdisplay_line_numbers, Qvisual))
21101 && lnum_to_display == 0
21102 && !NILP (Vdisplay_line_numbers_current_absolute))
21103 lnum_to_display = it->pt_lnum + 1;
21104 /* In L2R rows we need to append the blank separator, in R2L
21105 rows we need to prepend it. But this function is usually
21106 called when no display elements were produced from the
21107 following line, so the paragraph direction might be unknown.
21108 Therefore we cheat and add 2 blanks, one on either side. */
21109 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21110 strcat (lnum_buf, " ");
21112 /* Setup for producing the glyphs. */
21113 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21114 /* FIXME: Use specialized face. */
21115 DEFAULT_FACE_ID);
21116 scratch_glyph_row.reversed_p = false;
21117 scratch_glyph_row.used[TEXT_AREA] = 0;
21118 SET_TEXT_POS (tem_it.position, 0, 0);
21119 tem_it.avoid_cursor_p = true;
21120 tem_it.bidi_p = true;
21121 tem_it.bidi_it.type = WEAK_EN;
21122 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21123 1 level in R2L paragraphs. Emulate that, assuming we are in
21124 an L2R paragraph. */
21125 tem_it.bidi_it.resolved_level = 2;
21127 /* Produce glyphs for the line number in a scratch glyph_row. */
21128 int n_glyphs_before;
21129 for (const char *p = lnum_buf; *p; p++)
21131 /* For continuation lines and lines after ZV, instead of a line
21132 number, produce a blank prefix of the same width. Use the
21133 default face for the blank field beyond ZV. */
21134 if (beyond_zv)
21135 tem_it.face_id = it->base_face_id;
21136 else if (lnum_face_id != current_lnum_face_id
21137 && (EQ (Vdisplay_line_numbers, Qvisual)
21138 ? this_line == 0
21139 : this_line == it->pt_lnum))
21140 tem_it.face_id = current_lnum_face_id;
21141 else
21142 tem_it.face_id = lnum_face_id;
21143 if (beyond_zv
21144 /* Don't display the same line number more than once. */
21145 || (!EQ (Vdisplay_line_numbers, Qvisual)
21146 && (it->continuation_lines_width > 0
21147 || (this_line == last_line && !first_time))))
21148 tem_it.c = tem_it.char_to_display = ' ';
21149 else
21150 tem_it.c = tem_it.char_to_display = *p;
21151 tem_it.len = 1;
21152 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21153 /* Make sure these glyphs will have a "position" of -1. */
21154 SET_TEXT_POS (tem_it.position, -1, -1);
21155 PRODUCE_GLYPHS (&tem_it);
21157 /* Stop producing glyphs if we don't have enough space on
21158 this line. FIXME: should we refrain from producing the
21159 line number at all in that case? */
21160 if (tem_it.current_x > tem_it.last_visible_x)
21162 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21163 break;
21167 /* Record the width in pixels we need for the line number display. */
21168 it->lnum_pixel_width = tem_it.current_x;
21169 /* Copy the produced glyphs into IT's glyph_row. */
21170 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21171 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21172 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21173 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21175 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21177 for ( ; g < e; g++)
21179 it->current_x += g->pixel_width;
21180 /* The following is important when this function is called
21181 from move_it_in_display_line_to: HPOS is incremented only
21182 when we are in the visible portion of the glyph row. */
21183 if (it->current_x > it->first_visible_x)
21184 it->hpos++;
21185 if (p)
21187 *p++ = *g;
21188 (*u)++;
21192 /* Update IT's metrics due to glyphs produced for line numbers. */
21193 if (it->glyph_row)
21195 struct glyph_row *row = it->glyph_row;
21197 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21198 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21199 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21200 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21201 tem_it.max_phys_descent);
21203 else
21205 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21206 it->max_descent = max (it->max_descent, tem_it.max_descent);
21207 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21208 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21211 it->line_number_produced_p = true;
21213 bidi_unshelve_cache (itdata, false);
21216 /* Return true if this glyph row needs a line number to be produced
21217 for it. */
21218 static bool
21219 should_produce_line_number (struct it *it)
21221 if (NILP (Vdisplay_line_numbers))
21222 return false;
21224 /* Don't display line numbers in minibuffer windows. */
21225 if (MINI_WINDOW_P (it->w))
21226 return false;
21228 #ifdef HAVE_WINDOW_SYSTEM
21229 /* Don't display line number in tooltip frames. */
21230 if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
21231 return false;
21232 #endif
21234 /* If the character at current position has a non-nil special
21235 property, disable line numbers for this row. This is for
21236 packages such as company-mode, which need this for their tricky
21237 layout, where line numbers get in the way. */
21238 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21239 Qdisplay_line_numbers_disable,
21240 it->window);
21241 /* For ZV, we need to also look in empty overlays at that point,
21242 because get-char-property always returns nil for ZV, except if
21243 the property is in 'default-text-properties'. */
21244 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21245 val = disable_line_numbers_overlay_at_eob ();
21246 return NILP (val) ? true : false;
21249 /* Return true if ROW has no glyphs except those inserted by the
21250 display engine. This is needed for indicate-empty-lines and
21251 similar features when the glyph row starts with glyphs which didn't
21252 come from buffer or string. */
21253 static bool
21254 row_text_area_empty (struct glyph_row *row)
21256 if (!row->reversed_p)
21258 for (struct glyph *g = row->glyphs[TEXT_AREA];
21259 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21260 g++)
21261 if (!NILP (g->object) || g->charpos > 0)
21262 return false;
21264 else
21266 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21267 g > row->glyphs[TEXT_AREA];
21268 g--)
21269 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21270 return false;
21273 return true;
21276 /* Construct the glyph row IT->glyph_row in the desired matrix of
21277 IT->w from text at the current position of IT. See dispextern.h
21278 for an overview of struct it. Value is true if
21279 IT->glyph_row displays text, as opposed to a line displaying ZV
21280 only. CURSOR_VPOS is the window-relative vertical position of
21281 the glyph row displaying the cursor, or -1 if unknown. */
21283 static bool
21284 display_line (struct it *it, int cursor_vpos)
21286 struct glyph_row *row = it->glyph_row;
21287 Lisp_Object overlay_arrow_string;
21288 struct it wrap_it;
21289 void *wrap_data = NULL;
21290 bool may_wrap = false;
21291 int wrap_x UNINIT;
21292 int wrap_row_used = -1;
21293 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21294 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21295 int wrap_row_extra_line_spacing UNINIT;
21296 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21297 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21298 int cvpos;
21299 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21300 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21301 bool pending_handle_line_prefix = false;
21302 int header_line = window_wants_header_line (it->w);
21303 bool hscroll_this_line = (cursor_vpos >= 0
21304 && it->vpos == cursor_vpos - header_line
21305 && hscrolling_current_line_p (it->w));
21306 int first_visible_x = it->first_visible_x;
21307 int last_visible_x = it->last_visible_x;
21308 int x_incr = 0;
21310 /* We always start displaying at hpos zero even if hscrolled. */
21311 eassert (it->hpos == 0 && it->current_x == 0);
21313 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21314 >= it->w->desired_matrix->nrows)
21316 it->w->nrows_scale_factor++;
21317 it->f->fonts_changed = true;
21318 return false;
21321 /* Clear the result glyph row and enable it. */
21322 prepare_desired_row (it->w, row, false);
21324 row->y = it->current_y;
21325 row->start = it->start;
21326 row->continuation_lines_width = it->continuation_lines_width;
21327 row->displays_text_p = true;
21328 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21329 it->starts_in_middle_of_char_p = false;
21330 it->tab_offset = 0;
21331 it->line_number_produced_p = false;
21333 /* Arrange the overlays nicely for our purposes. Usually, we call
21334 display_line on only one line at a time, in which case this
21335 can't really hurt too much, or we call it on lines which appear
21336 one after another in the buffer, in which case all calls to
21337 recenter_overlay_lists but the first will be pretty cheap. */
21338 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21340 /* If we are going to display the cursor's line, account for the
21341 hscroll of that line. We subtract the window's min_hscroll,
21342 because that was already accounted for in init_iterator. */
21343 if (hscroll_this_line)
21344 x_incr =
21345 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21346 * FRAME_COLUMN_WIDTH (it->f);
21348 bool line_number_needed = should_produce_line_number (it);
21350 /* Move over display elements that are not visible because we are
21351 hscrolled. This may stop at an x-position < first_visible_x
21352 if the first glyph is partially visible or if we hit a line end. */
21353 if (it->current_x < it->first_visible_x + x_incr)
21355 enum move_it_result move_result;
21357 this_line_min_pos = row->start.pos;
21358 if (hscroll_this_line)
21360 it->first_visible_x += x_incr;
21361 it->last_visible_x += x_incr;
21363 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21364 MOVE_TO_POS | MOVE_TO_X);
21365 /* If we are under a large hscroll, move_it_in_display_line_to
21366 could hit the end of the line without reaching
21367 first_visible_x. Pretend that we did reach it. This is
21368 especially important on a TTY, where we will call
21369 extend_face_to_end_of_line, which needs to know how many
21370 blank glyphs to produce. */
21371 if (it->current_x < it->first_visible_x
21372 && (move_result == MOVE_NEWLINE_OR_CR
21373 || move_result == MOVE_POS_MATCH_OR_ZV))
21374 it->current_x = it->first_visible_x;
21376 /* In case move_it_in_display_line_to above "produced" the line
21377 number. */
21378 it->line_number_produced_p = false;
21380 /* Record the smallest positions seen while we moved over
21381 display elements that are not visible. This is needed by
21382 redisplay_internal for optimizing the case where the cursor
21383 stays inside the same line. The rest of this function only
21384 considers positions that are actually displayed, so
21385 RECORD_MAX_MIN_POS will not otherwise record positions that
21386 are hscrolled to the left of the left edge of the window. */
21387 min_pos = CHARPOS (this_line_min_pos);
21388 min_bpos = BYTEPOS (this_line_min_pos);
21390 /* Produce line number, if needed. */
21391 if (line_number_needed)
21392 maybe_produce_line_number (it);
21394 else if (it->area == TEXT_AREA)
21396 /* Line numbers should precede the line-prefix or wrap-prefix. */
21397 if (line_number_needed)
21398 maybe_produce_line_number (it);
21400 /* We only do this when not calling move_it_in_display_line_to
21401 above, because that function calls itself handle_line_prefix. */
21402 handle_line_prefix (it);
21404 else
21406 /* Line-prefix and wrap-prefix are always displayed in the text
21407 area. But if this is the first call to display_line after
21408 init_iterator, the iterator might have been set up to write
21409 into a marginal area, e.g. if the line begins with some
21410 display property that writes to the margins. So we need to
21411 wait with the call to handle_line_prefix until whatever
21412 writes to the margin has done its job. */
21413 pending_handle_line_prefix = true;
21416 /* Get the initial row height. This is either the height of the
21417 text hscrolled, if there is any, or zero. */
21418 row->ascent = it->max_ascent;
21419 row->height = it->max_ascent + it->max_descent;
21420 row->phys_ascent = it->max_phys_ascent;
21421 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21422 row->extra_line_spacing = it->max_extra_line_spacing;
21424 /* Utility macro to record max and min buffer positions seen until now. */
21425 #define RECORD_MAX_MIN_POS(IT) \
21426 do \
21428 bool composition_p \
21429 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21430 ptrdiff_t current_pos = \
21431 composition_p ? (IT)->cmp_it.charpos \
21432 : IT_CHARPOS (*(IT)); \
21433 ptrdiff_t current_bpos = \
21434 composition_p ? CHAR_TO_BYTE (current_pos) \
21435 : IT_BYTEPOS (*(IT)); \
21436 if (current_pos < min_pos) \
21438 min_pos = current_pos; \
21439 min_bpos = current_bpos; \
21441 if (IT_CHARPOS (*it) > max_pos) \
21443 max_pos = IT_CHARPOS (*it); \
21444 max_bpos = IT_BYTEPOS (*it); \
21447 while (false)
21449 /* Loop generating characters. The loop is left with IT on the next
21450 character to display. */
21451 while (true)
21453 int n_glyphs_before, hpos_before, x_before;
21454 int x, nglyphs;
21455 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21457 /* Retrieve the next thing to display. Value is false if end of
21458 buffer reached. */
21459 if (!get_next_display_element (it))
21461 bool row_has_glyphs = false;
21462 /* Maybe add a space at the end of this line that is used to
21463 display the cursor there under X. Set the charpos of the
21464 first glyph of blank lines not corresponding to any text
21465 to -1. */
21466 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21467 row->exact_window_width_line_p = true;
21468 else if ((append_space_for_newline (it, true)
21469 && row->used[TEXT_AREA] == 1)
21470 || row->used[TEXT_AREA] == 0
21471 || (row_has_glyphs = row_text_area_empty (row)))
21473 row->glyphs[TEXT_AREA]->charpos = -1;
21474 /* Don't reset the displays_text_p flag if we are
21475 displaying line numbers or line-prefix. */
21476 if (!row_has_glyphs)
21477 row->displays_text_p = false;
21479 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21480 && (!MINI_WINDOW_P (it->w)))
21481 row->indicate_empty_line_p = true;
21484 it->continuation_lines_width = 0;
21485 /* Reset those iterator values set from display property
21486 values. This is for the case when the display property
21487 ends at ZV, and is not a replacing property, so pop_it is
21488 not called. */
21489 it->font_height = Qnil;
21490 it->voffset = 0;
21491 row->ends_at_zv_p = true;
21492 /* A row that displays right-to-left text must always have
21493 its last face extended all the way to the end of line,
21494 even if this row ends in ZV, because we still write to
21495 the screen left to right. We also need to extend the
21496 last face if the default face is remapped to some
21497 different face, otherwise the functions that clear
21498 portions of the screen will clear with the default face's
21499 background color. */
21500 if (row->reversed_p
21501 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21502 extend_face_to_end_of_line (it);
21503 break;
21506 /* Now, get the metrics of what we want to display. This also
21507 generates glyphs in `row' (which is IT->glyph_row). */
21508 n_glyphs_before = row->used[TEXT_AREA];
21509 x = it->current_x;
21511 /* Remember the line height so far in case the next element doesn't
21512 fit on the line. */
21513 if (it->line_wrap != TRUNCATE)
21515 ascent = it->max_ascent;
21516 descent = it->max_descent;
21517 phys_ascent = it->max_phys_ascent;
21518 phys_descent = it->max_phys_descent;
21520 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21522 if (IT_DISPLAYING_WHITESPACE (it))
21523 may_wrap = true;
21524 else if (may_wrap)
21526 SAVE_IT (wrap_it, *it, wrap_data);
21527 wrap_x = x;
21528 wrap_row_used = row->used[TEXT_AREA];
21529 wrap_row_ascent = row->ascent;
21530 wrap_row_height = row->height;
21531 wrap_row_phys_ascent = row->phys_ascent;
21532 wrap_row_phys_height = row->phys_height;
21533 wrap_row_extra_line_spacing = row->extra_line_spacing;
21534 wrap_row_min_pos = min_pos;
21535 wrap_row_min_bpos = min_bpos;
21536 wrap_row_max_pos = max_pos;
21537 wrap_row_max_bpos = max_bpos;
21538 may_wrap = false;
21543 PRODUCE_GLYPHS (it);
21545 /* If this display element was in marginal areas, continue with
21546 the next one. */
21547 if (it->area != TEXT_AREA)
21549 row->ascent = max (row->ascent, it->max_ascent);
21550 row->height = max (row->height, it->max_ascent + it->max_descent);
21551 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21552 row->phys_height = max (row->phys_height,
21553 it->max_phys_ascent + it->max_phys_descent);
21554 row->extra_line_spacing = max (row->extra_line_spacing,
21555 it->max_extra_line_spacing);
21556 set_iterator_to_next (it, true);
21557 /* If we didn't handle the line/wrap prefix above, and the
21558 call to set_iterator_to_next just switched to TEXT_AREA,
21559 process the prefix now. */
21560 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21562 /* Line numbers should precede the line-prefix or wrap-prefix. */
21563 if (line_number_needed)
21564 maybe_produce_line_number (it);
21566 pending_handle_line_prefix = false;
21567 handle_line_prefix (it);
21569 continue;
21572 /* Does the display element fit on the line? If we truncate
21573 lines, we should draw past the right edge of the window. If
21574 we don't truncate, we want to stop so that we can display the
21575 continuation glyph before the right margin. If lines are
21576 continued, there are two possible strategies for characters
21577 resulting in more than 1 glyph (e.g. tabs): Display as many
21578 glyphs as possible in this line and leave the rest for the
21579 continuation line, or display the whole element in the next
21580 line. Original redisplay did the former, so we do it also. */
21581 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21582 hpos_before = it->hpos;
21583 x_before = x;
21585 if (/* Not a newline. */
21586 nglyphs > 0
21587 /* Glyphs produced fit entirely in the line. */
21588 && it->current_x < it->last_visible_x)
21590 it->hpos += nglyphs;
21591 row->ascent = max (row->ascent, it->max_ascent);
21592 row->height = max (row->height, it->max_ascent + it->max_descent);
21593 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21594 row->phys_height = max (row->phys_height,
21595 it->max_phys_ascent + it->max_phys_descent);
21596 row->extra_line_spacing = max (row->extra_line_spacing,
21597 it->max_extra_line_spacing);
21598 if (it->current_x - it->pixel_width < it->first_visible_x
21599 /* When line numbers are displayed, row->x should not be
21600 offset, as the first glyph after the line number can
21601 never be partially visible. */
21602 && !line_number_needed
21603 /* In R2L rows, we arrange in extend_face_to_end_of_line
21604 to add a right offset to the line, by a suitable
21605 change to the stretch glyph that is the leftmost
21606 glyph of the line. */
21607 && !row->reversed_p)
21608 row->x = x - it->first_visible_x;
21609 /* Record the maximum and minimum buffer positions seen so
21610 far in glyphs that will be displayed by this row. */
21611 if (it->bidi_p)
21612 RECORD_MAX_MIN_POS (it);
21614 else
21616 int i, new_x;
21617 struct glyph *glyph;
21619 for (i = 0; i < nglyphs; ++i, x = new_x)
21621 /* Identify the glyphs added by the last call to
21622 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21623 the previous glyphs. */
21624 if (!row->reversed_p)
21625 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21626 else
21627 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21628 new_x = x + glyph->pixel_width;
21630 if (/* Lines are continued. */
21631 it->line_wrap != TRUNCATE
21632 && (/* Glyph doesn't fit on the line. */
21633 new_x > it->last_visible_x
21634 /* Or it fits exactly on a window system frame. */
21635 || (new_x == it->last_visible_x
21636 && FRAME_WINDOW_P (it->f)
21637 && (row->reversed_p
21638 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21639 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21641 /* End of a continued line. */
21643 if (it->hpos == 0
21644 || (new_x == it->last_visible_x
21645 && FRAME_WINDOW_P (it->f)
21646 && (row->reversed_p
21647 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21648 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21650 /* Current glyph is the only one on the line or
21651 fits exactly on the line. We must continue
21652 the line because we can't draw the cursor
21653 after the glyph. */
21654 row->continued_p = true;
21655 it->current_x = new_x;
21656 it->continuation_lines_width += new_x;
21657 ++it->hpos;
21658 if (i == nglyphs - 1)
21660 /* If line-wrap is on, check if a previous
21661 wrap point was found. */
21662 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21663 && wrap_row_used > 0
21664 /* Even if there is a previous wrap
21665 point, continue the line here as
21666 usual, if (i) the previous character
21667 was a space or tab AND (ii) the
21668 current character is not. */
21669 && (!may_wrap
21670 || IT_DISPLAYING_WHITESPACE (it)))
21671 goto back_to_wrap;
21673 /* Record the maximum and minimum buffer
21674 positions seen so far in glyphs that will be
21675 displayed by this row. */
21676 if (it->bidi_p)
21677 RECORD_MAX_MIN_POS (it);
21678 set_iterator_to_next (it, true);
21679 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21681 if (!get_next_display_element (it))
21683 row->exact_window_width_line_p = true;
21684 it->continuation_lines_width = 0;
21685 it->font_height = Qnil;
21686 it->voffset = 0;
21687 row->continued_p = false;
21688 row->ends_at_zv_p = true;
21690 else if (ITERATOR_AT_END_OF_LINE_P (it))
21692 row->continued_p = false;
21693 row->exact_window_width_line_p = true;
21695 /* If line-wrap is on, check if a
21696 previous wrap point was found. */
21697 else if (wrap_row_used > 0
21698 /* Even if there is a previous wrap
21699 point, continue the line here as
21700 usual, if (i) the previous character
21701 was a space or tab AND (ii) the
21702 current character is not. */
21703 && (!may_wrap
21704 || IT_DISPLAYING_WHITESPACE (it)))
21705 goto back_to_wrap;
21709 else if (it->bidi_p)
21710 RECORD_MAX_MIN_POS (it);
21711 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21712 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21713 extend_face_to_end_of_line (it);
21715 else if (CHAR_GLYPH_PADDING_P (*glyph)
21716 && !FRAME_WINDOW_P (it->f))
21718 /* A padding glyph that doesn't fit on this line.
21719 This means the whole character doesn't fit
21720 on the line. */
21721 if (row->reversed_p)
21722 unproduce_glyphs (it, row->used[TEXT_AREA]
21723 - n_glyphs_before);
21724 row->used[TEXT_AREA] = n_glyphs_before;
21726 /* Fill the rest of the row with continuation
21727 glyphs like in 20.x. */
21728 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21729 < row->glyphs[1 + TEXT_AREA])
21730 produce_special_glyphs (it, IT_CONTINUATION);
21732 row->continued_p = true;
21733 it->current_x = x_before;
21734 it->continuation_lines_width += x_before;
21736 /* Restore the height to what it was before the
21737 element not fitting on the line. */
21738 it->max_ascent = ascent;
21739 it->max_descent = descent;
21740 it->max_phys_ascent = phys_ascent;
21741 it->max_phys_descent = phys_descent;
21742 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21743 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21744 extend_face_to_end_of_line (it);
21746 else if (wrap_row_used > 0)
21748 back_to_wrap:
21749 if (row->reversed_p)
21750 unproduce_glyphs (it,
21751 row->used[TEXT_AREA] - wrap_row_used);
21752 RESTORE_IT (it, &wrap_it, wrap_data);
21753 it->continuation_lines_width += wrap_x;
21754 row->used[TEXT_AREA] = wrap_row_used;
21755 row->ascent = wrap_row_ascent;
21756 row->height = wrap_row_height;
21757 row->phys_ascent = wrap_row_phys_ascent;
21758 row->phys_height = wrap_row_phys_height;
21759 row->extra_line_spacing = wrap_row_extra_line_spacing;
21760 min_pos = wrap_row_min_pos;
21761 min_bpos = wrap_row_min_bpos;
21762 max_pos = wrap_row_max_pos;
21763 max_bpos = wrap_row_max_bpos;
21764 row->continued_p = true;
21765 row->ends_at_zv_p = false;
21766 row->exact_window_width_line_p = false;
21768 /* Make sure that a non-default face is extended
21769 up to the right margin of the window. */
21770 extend_face_to_end_of_line (it);
21772 else if ((it->what == IT_CHARACTER
21773 || it->what == IT_STRETCH
21774 || it->what == IT_COMPOSITION)
21775 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21777 /* A TAB that extends past the right edge of the
21778 window. This produces a single glyph on
21779 window system frames. We leave the glyph in
21780 this row and let it fill the row, but don't
21781 consume the TAB. */
21782 if ((row->reversed_p
21783 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21784 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21785 produce_special_glyphs (it, IT_CONTINUATION);
21786 it->continuation_lines_width += it->last_visible_x;
21787 row->ends_in_middle_of_char_p = true;
21788 row->continued_p = true;
21789 glyph->pixel_width = it->last_visible_x - x;
21790 it->starts_in_middle_of_char_p = true;
21791 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21792 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21793 extend_face_to_end_of_line (it);
21795 else
21797 /* Something other than a TAB that draws past
21798 the right edge of the window. Restore
21799 positions to values before the element. */
21800 if (row->reversed_p)
21801 unproduce_glyphs (it, row->used[TEXT_AREA]
21802 - (n_glyphs_before + i));
21803 row->used[TEXT_AREA] = n_glyphs_before + i;
21805 /* Display continuation glyphs. */
21806 it->current_x = x_before;
21807 it->continuation_lines_width += x;
21808 if (!FRAME_WINDOW_P (it->f)
21809 || (row->reversed_p
21810 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21811 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21812 produce_special_glyphs (it, IT_CONTINUATION);
21813 row->continued_p = true;
21815 extend_face_to_end_of_line (it);
21817 if (nglyphs > 1 && i > 0)
21819 row->ends_in_middle_of_char_p = true;
21820 it->starts_in_middle_of_char_p = true;
21823 /* Restore the height to what it was before the
21824 element not fitting on the line. */
21825 it->max_ascent = ascent;
21826 it->max_descent = descent;
21827 it->max_phys_ascent = phys_ascent;
21828 it->max_phys_descent = phys_descent;
21831 break;
21833 else if (new_x > it->first_visible_x)
21835 /* Increment number of glyphs actually displayed. */
21836 ++it->hpos;
21838 /* Record the maximum and minimum buffer positions
21839 seen so far in glyphs that will be displayed by
21840 this row. */
21841 if (it->bidi_p)
21842 RECORD_MAX_MIN_POS (it);
21844 if (x < it->first_visible_x && !row->reversed_p
21845 && !line_number_needed)
21846 /* Glyph is partially visible, i.e. row starts at
21847 negative X position. Don't do that in R2L
21848 rows, where we arrange to add a right offset to
21849 the line in extend_face_to_end_of_line, by a
21850 suitable change to the stretch glyph that is
21851 the leftmost glyph of the line. */
21852 row->x = x - it->first_visible_x;
21853 /* When the last glyph of an R2L row only fits
21854 partially on the line, we need to set row->x to a
21855 negative offset, so that the leftmost glyph is
21856 the one that is partially visible. But if we are
21857 going to produce the truncation glyph, this will
21858 be taken care of in produce_special_glyphs. */
21859 if (row->reversed_p
21860 && new_x > it->last_visible_x
21861 && !line_number_needed
21862 && !(it->line_wrap == TRUNCATE
21863 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21865 eassert (FRAME_WINDOW_P (it->f));
21866 row->x = it->last_visible_x - new_x;
21869 else
21871 /* Glyph is completely off the left margin of the
21872 window. This should not happen because of the
21873 move_it_in_display_line at the start of this
21874 function, unless the text display area of the
21875 window is empty. */
21876 eassert (it->first_visible_x <= it->last_visible_x);
21879 /* Even if this display element produced no glyphs at all,
21880 we want to record its position. */
21881 if (it->bidi_p && nglyphs == 0)
21882 RECORD_MAX_MIN_POS (it);
21884 row->ascent = max (row->ascent, it->max_ascent);
21885 row->height = max (row->height, it->max_ascent + it->max_descent);
21886 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21887 row->phys_height = max (row->phys_height,
21888 it->max_phys_ascent + it->max_phys_descent);
21889 row->extra_line_spacing = max (row->extra_line_spacing,
21890 it->max_extra_line_spacing);
21892 /* End of this display line if row is continued. */
21893 if (row->continued_p || row->ends_at_zv_p)
21894 break;
21897 at_end_of_line:
21898 /* Is this a line end? If yes, we're also done, after making
21899 sure that a non-default face is extended up to the right
21900 margin of the window. */
21901 if (ITERATOR_AT_END_OF_LINE_P (it))
21903 int used_before = row->used[TEXT_AREA];
21905 row->ends_in_newline_from_string_p = STRINGP (it->object);
21907 /* Add a space at the end of the line that is used to
21908 display the cursor there. */
21909 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21910 append_space_for_newline (it, false);
21912 /* Extend the face to the end of the line. */
21913 extend_face_to_end_of_line (it);
21915 /* Make sure we have the position. */
21916 if (used_before == 0)
21917 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21919 /* Record the position of the newline, for use in
21920 find_row_edges. */
21921 it->eol_pos = it->current.pos;
21923 /* Consume the line end. This skips over invisible lines. */
21924 set_iterator_to_next (it, true);
21925 it->continuation_lines_width = 0;
21926 break;
21929 /* Proceed with next display element. Note that this skips
21930 over lines invisible because of selective display. */
21931 set_iterator_to_next (it, true);
21933 /* If we truncate lines, we are done when the last displayed
21934 glyphs reach past the right margin of the window. */
21935 if (it->line_wrap == TRUNCATE
21936 && ((FRAME_WINDOW_P (it->f)
21937 /* Images are preprocessed in produce_image_glyph such
21938 that they are cropped at the right edge of the
21939 window, so an image glyph will always end exactly at
21940 last_visible_x, even if there's no right fringe. */
21941 && ((row->reversed_p
21942 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21943 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21944 || it->what == IT_IMAGE))
21945 ? (it->current_x >= it->last_visible_x)
21946 : (it->current_x > it->last_visible_x)))
21948 /* Maybe add truncation glyphs. */
21949 if (!FRAME_WINDOW_P (it->f)
21950 || (row->reversed_p
21951 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21952 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21954 int i, n;
21956 if (!row->reversed_p)
21958 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21959 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21960 break;
21962 else
21964 for (i = 0; i < row->used[TEXT_AREA]; i++)
21965 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21966 break;
21967 /* Remove any padding glyphs at the front of ROW, to
21968 make room for the truncation glyphs we will be
21969 adding below. The loop below always inserts at
21970 least one truncation glyph, so also remove the
21971 last glyph added to ROW. */
21972 unproduce_glyphs (it, i + 1);
21973 /* Adjust i for the loop below. */
21974 i = row->used[TEXT_AREA] - (i + 1);
21977 /* produce_special_glyphs overwrites the last glyph, so
21978 we don't want that if we want to keep that last
21979 glyph, which means it's an image. */
21980 if (it->current_x > it->last_visible_x)
21982 it->current_x = x_before;
21983 if (!FRAME_WINDOW_P (it->f))
21985 for (n = row->used[TEXT_AREA]; i < n; ++i)
21987 row->used[TEXT_AREA] = i;
21988 produce_special_glyphs (it, IT_TRUNCATION);
21991 else
21993 row->used[TEXT_AREA] = i;
21994 produce_special_glyphs (it, IT_TRUNCATION);
21996 it->hpos = hpos_before;
21999 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
22001 /* Don't truncate if we can overflow newline into fringe. */
22002 if (!get_next_display_element (it))
22004 it->continuation_lines_width = 0;
22005 it->font_height = Qnil;
22006 it->voffset = 0;
22007 row->ends_at_zv_p = true;
22008 row->exact_window_width_line_p = true;
22009 break;
22011 if (ITERATOR_AT_END_OF_LINE_P (it))
22013 row->exact_window_width_line_p = true;
22014 goto at_end_of_line;
22016 it->current_x = x_before;
22017 it->hpos = hpos_before;
22020 row->truncated_on_right_p = true;
22021 it->continuation_lines_width = 0;
22022 reseat_at_next_visible_line_start (it, false);
22023 /* We insist below that IT's position be at ZV because in
22024 bidi-reordered lines the character at visible line start
22025 might not be the character that follows the newline in
22026 the logical order. */
22027 if (IT_BYTEPOS (*it) > BEG_BYTE)
22028 row->ends_at_zv_p =
22029 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
22030 else
22031 row->ends_at_zv_p = false;
22032 break;
22036 if (wrap_data)
22037 bidi_unshelve_cache (wrap_data, true);
22039 /* If line is not empty and hscrolled, maybe insert truncation glyphs
22040 at the left window margin. */
22041 if (it->first_visible_x
22042 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
22044 if (!FRAME_WINDOW_P (it->f)
22045 || (((row->reversed_p
22046 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22047 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22048 /* Don't let insert_left_trunc_glyphs overwrite the
22049 first glyph of the row if it is an image. */
22050 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
22051 insert_left_trunc_glyphs (it);
22052 row->truncated_on_left_p = true;
22055 /* Remember the position at which this line ends.
22057 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
22058 cannot be before the call to find_row_edges below, since that is
22059 where these positions are determined. */
22060 row->end = it->current;
22061 if (!it->bidi_p)
22063 row->minpos = row->start.pos;
22064 row->maxpos = row->end.pos;
22066 else
22068 /* ROW->minpos and ROW->maxpos must be the smallest and
22069 `1 + the largest' buffer positions in ROW. But if ROW was
22070 bidi-reordered, these two positions can be anywhere in the
22071 row, so we must determine them now. */
22072 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
22075 /* If the start of this line is the overlay arrow-position, then
22076 mark this glyph row as the one containing the overlay arrow.
22077 This is clearly a mess with variable size fonts. It would be
22078 better to let it be displayed like cursors under X. */
22079 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22080 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22081 !NILP (overlay_arrow_string)))
22083 /* Overlay arrow in window redisplay is a fringe bitmap. */
22084 if (STRINGP (overlay_arrow_string))
22086 struct glyph_row *arrow_row
22087 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22088 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22089 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22090 struct glyph *p = row->glyphs[TEXT_AREA];
22091 struct glyph *p2, *end;
22093 /* Copy the arrow glyphs. */
22094 while (glyph < arrow_end)
22095 *p++ = *glyph++;
22097 /* Throw away padding glyphs. */
22098 p2 = p;
22099 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22100 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22101 ++p2;
22102 if (p2 > p)
22104 while (p2 < end)
22105 *p++ = *p2++;
22106 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22109 else
22111 eassert (INTEGERP (overlay_arrow_string));
22112 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22114 overlay_arrow_seen = true;
22117 /* Highlight trailing whitespace. */
22118 if (!NILP (Vshow_trailing_whitespace))
22119 highlight_trailing_whitespace (it->f, it->glyph_row);
22121 /* Compute pixel dimensions of this line. */
22122 compute_line_metrics (it);
22124 /* Implementation note: No changes in the glyphs of ROW or in their
22125 faces can be done past this point, because compute_line_metrics
22126 computes ROW's hash value and stores it within the glyph_row
22127 structure. */
22129 /* Record whether this row ends inside an ellipsis. */
22130 row->ends_in_ellipsis_p
22131 = (it->method == GET_FROM_DISPLAY_VECTOR
22132 && it->ellipsis_p);
22134 /* Save fringe bitmaps in this row. */
22135 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22136 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22137 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22138 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22140 it->left_user_fringe_bitmap = 0;
22141 it->left_user_fringe_face_id = 0;
22142 it->right_user_fringe_bitmap = 0;
22143 it->right_user_fringe_face_id = 0;
22145 /* Maybe set the cursor. */
22146 cvpos = it->w->cursor.vpos;
22147 if ((cvpos < 0
22148 /* In bidi-reordered rows, keep checking for proper cursor
22149 position even if one has been found already, because buffer
22150 positions in such rows change non-linearly with ROW->VPOS,
22151 when a line is continued. One exception: when we are at ZV,
22152 display cursor on the first suitable glyph row, since all
22153 the empty rows after that also have their position set to ZV. */
22154 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22155 lines' rows is implemented for bidi-reordered rows. */
22156 || (it->bidi_p
22157 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22158 && PT >= MATRIX_ROW_START_CHARPOS (row)
22159 && PT <= MATRIX_ROW_END_CHARPOS (row)
22160 && cursor_row_p (row))
22161 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22163 /* Prepare for the next line. This line starts horizontally at (X
22164 HPOS) = (0 0). Vertical positions are incremented. As a
22165 convenience for the caller, IT->glyph_row is set to the next
22166 row to be used. */
22167 it->current_x = it->hpos = 0;
22168 it->current_y += row->height;
22169 /* Restore the first and last visible X if we adjusted them for
22170 current-line hscrolling. */
22171 if (hscroll_this_line)
22173 it->first_visible_x = first_visible_x;
22174 it->last_visible_x = last_visible_x;
22176 SET_TEXT_POS (it->eol_pos, 0, 0);
22177 ++it->vpos;
22178 ++it->glyph_row;
22179 /* The next row should by default use the same value of the
22180 reversed_p flag as this one. set_iterator_to_next decides when
22181 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22182 the flag accordingly. */
22183 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22184 it->glyph_row->reversed_p = row->reversed_p;
22185 it->start = row->end;
22186 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22188 #undef RECORD_MAX_MIN_POS
22191 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22192 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22193 doc: /* Return paragraph direction at point in BUFFER.
22194 Value is either `left-to-right' or `right-to-left'.
22195 If BUFFER is omitted or nil, it defaults to the current buffer.
22197 Paragraph direction determines how the text in the paragraph is displayed.
22198 In left-to-right paragraphs, text begins at the left margin of the window
22199 and the reading direction is generally left to right. In right-to-left
22200 paragraphs, text begins at the right margin and is read from right to left.
22202 See also `bidi-paragraph-direction'. */)
22203 (Lisp_Object buffer)
22205 struct buffer *buf = current_buffer;
22206 struct buffer *old = buf;
22208 if (! NILP (buffer))
22210 CHECK_BUFFER (buffer);
22211 buf = XBUFFER (buffer);
22214 if (NILP (BVAR (buf, bidi_display_reordering))
22215 || NILP (BVAR (buf, enable_multibyte_characters))
22216 /* When we are loading loadup.el, the character property tables
22217 needed for bidi iteration are not yet available. */
22218 || redisplay__inhibit_bidi)
22219 return Qleft_to_right;
22220 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22221 return BVAR (buf, bidi_paragraph_direction);
22222 else
22224 /* Determine the direction from buffer text. We could try to
22225 use current_matrix if it is up to date, but this seems fast
22226 enough as it is. */
22227 struct bidi_it itb;
22228 ptrdiff_t pos = BUF_PT (buf);
22229 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22230 int c;
22231 void *itb_data = bidi_shelve_cache ();
22233 set_buffer_temp (buf);
22234 /* bidi_paragraph_init finds the base direction of the paragraph
22235 by searching forward from paragraph start. We need the base
22236 direction of the current or _previous_ paragraph, so we need
22237 to make sure we are within that paragraph. To that end, find
22238 the previous non-empty line. */
22239 if (pos >= ZV && pos > BEGV)
22240 DEC_BOTH (pos, bytepos);
22241 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22242 if (fast_looking_at (trailing_white_space,
22243 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22245 while ((c = FETCH_BYTE (bytepos)) == '\n'
22246 || c == ' ' || c == '\t' || c == '\f')
22248 if (bytepos <= BEGV_BYTE)
22249 break;
22250 bytepos--;
22251 pos--;
22253 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22254 bytepos--;
22256 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22257 itb.paragraph_dir = NEUTRAL_DIR;
22258 itb.string.s = NULL;
22259 itb.string.lstring = Qnil;
22260 itb.string.bufpos = 0;
22261 itb.string.from_disp_str = false;
22262 itb.string.unibyte = false;
22263 /* We have no window to use here for ignoring window-specific
22264 overlays. Using NULL for window pointer will cause
22265 compute_display_string_pos to use the current buffer. */
22266 itb.w = NULL;
22267 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22268 bidi_unshelve_cache (itb_data, false);
22269 set_buffer_temp (old);
22270 switch (itb.paragraph_dir)
22272 case L2R:
22273 return Qleft_to_right;
22274 break;
22275 case R2L:
22276 return Qright_to_left;
22277 break;
22278 default:
22279 emacs_abort ();
22284 DEFUN ("bidi-find-overridden-directionality",
22285 Fbidi_find_overridden_directionality,
22286 Sbidi_find_overridden_directionality, 2, 3, 0,
22287 doc: /* Return position between FROM and TO where directionality was overridden.
22289 This function returns the first character position in the specified
22290 region of OBJECT where there is a character whose `bidi-class' property
22291 is `L', but which was forced to display as `R' by a directional
22292 override, and likewise with characters whose `bidi-class' is `R'
22293 or `AL' that were forced to display as `L'.
22295 If no such character is found, the function returns nil.
22297 OBJECT is a Lisp string or buffer to search for overridden
22298 directionality, and defaults to the current buffer if nil or omitted.
22299 OBJECT can also be a window, in which case the function will search
22300 the buffer displayed in that window. Passing the window instead of
22301 a buffer is preferable when the buffer is displayed in some window,
22302 because this function will then be able to correctly account for
22303 window-specific overlays, which can affect the results.
22305 Strong directional characters `L', `R', and `AL' can have their
22306 intrinsic directionality overridden by directional override
22307 control characters RLO (u+202e) and LRO (u+202d). See the
22308 function `get-char-code-property' for a way to inquire about
22309 the `bidi-class' property of a character. */)
22310 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22312 struct buffer *buf = current_buffer;
22313 struct buffer *old = buf;
22314 struct window *w = NULL;
22315 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22316 struct bidi_it itb;
22317 ptrdiff_t from_pos, to_pos, from_bpos;
22318 void *itb_data;
22320 if (!NILP (object))
22322 if (BUFFERP (object))
22323 buf = XBUFFER (object);
22324 else if (WINDOWP (object))
22326 w = decode_live_window (object);
22327 buf = XBUFFER (w->contents);
22328 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22330 else
22331 CHECK_STRING (object);
22334 if (STRINGP (object))
22336 /* Characters in unibyte strings are always treated by bidi.c as
22337 strong LTR. */
22338 if (!STRING_MULTIBYTE (object)
22339 /* When we are loading loadup.el, the character property
22340 tables needed for bidi iteration are not yet
22341 available. */
22342 || redisplay__inhibit_bidi)
22343 return Qnil;
22345 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22346 if (from_pos >= SCHARS (object))
22347 return Qnil;
22349 /* Set up the bidi iterator. */
22350 itb_data = bidi_shelve_cache ();
22351 itb.paragraph_dir = NEUTRAL_DIR;
22352 itb.string.lstring = object;
22353 itb.string.s = NULL;
22354 itb.string.schars = SCHARS (object);
22355 itb.string.bufpos = 0;
22356 itb.string.from_disp_str = false;
22357 itb.string.unibyte = false;
22358 itb.w = w;
22359 bidi_init_it (0, 0, frame_window_p, &itb);
22361 else
22363 /* Nothing this fancy can happen in unibyte buffers, or in a
22364 buffer that disabled reordering, or if FROM is at EOB. */
22365 if (NILP (BVAR (buf, bidi_display_reordering))
22366 || NILP (BVAR (buf, enable_multibyte_characters))
22367 /* When we are loading loadup.el, the character property
22368 tables needed for bidi iteration are not yet
22369 available. */
22370 || redisplay__inhibit_bidi)
22371 return Qnil;
22373 set_buffer_temp (buf);
22374 validate_region (&from, &to);
22375 from_pos = XINT (from);
22376 to_pos = XINT (to);
22377 if (from_pos >= ZV)
22378 return Qnil;
22380 /* Set up the bidi iterator. */
22381 itb_data = bidi_shelve_cache ();
22382 from_bpos = CHAR_TO_BYTE (from_pos);
22383 if (from_pos == BEGV)
22385 itb.charpos = BEGV;
22386 itb.bytepos = BEGV_BYTE;
22388 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22390 itb.charpos = from_pos;
22391 itb.bytepos = from_bpos;
22393 else
22394 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22395 -1, &itb.bytepos);
22396 itb.paragraph_dir = NEUTRAL_DIR;
22397 itb.string.s = NULL;
22398 itb.string.lstring = Qnil;
22399 itb.string.bufpos = 0;
22400 itb.string.from_disp_str = false;
22401 itb.string.unibyte = false;
22402 itb.w = w;
22403 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22406 ptrdiff_t found;
22407 do {
22408 /* For the purposes of this function, the actual base direction of
22409 the paragraph doesn't matter, so just set it to L2R. */
22410 bidi_paragraph_init (L2R, &itb, false);
22411 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22413 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22415 bidi_unshelve_cache (itb_data, false);
22416 set_buffer_temp (old);
22418 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22421 DEFUN ("move-point-visually", Fmove_point_visually,
22422 Smove_point_visually, 1, 1, 0,
22423 doc: /* Move point in the visual order in the specified DIRECTION.
22424 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22425 left.
22427 Value is the new character position of point. */)
22428 (Lisp_Object direction)
22430 struct window *w = XWINDOW (selected_window);
22431 struct buffer *b = XBUFFER (w->contents);
22432 struct glyph_row *row;
22433 int dir;
22434 Lisp_Object paragraph_dir;
22436 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22437 (!(ROW)->continued_p \
22438 && NILP ((GLYPH)->object) \
22439 && (GLYPH)->type == CHAR_GLYPH \
22440 && (GLYPH)->u.ch == ' ' \
22441 && (GLYPH)->charpos >= 0 \
22442 && !(GLYPH)->avoid_cursor_p)
22444 CHECK_NUMBER (direction);
22445 dir = XINT (direction);
22446 if (dir > 0)
22447 dir = 1;
22448 else
22449 dir = -1;
22451 /* If current matrix is up-to-date, we can use the information
22452 recorded in the glyphs, at least as long as the goal is on the
22453 screen. */
22454 if (w->window_end_valid
22455 && !windows_or_buffers_changed
22456 && b
22457 && !b->clip_changed
22458 && !b->prevent_redisplay_optimizations_p
22459 && !window_outdated (w)
22460 /* We rely below on the cursor coordinates to be up to date, but
22461 we cannot trust them if some command moved point since the
22462 last complete redisplay. */
22463 && w->last_point == BUF_PT (b)
22464 && w->cursor.vpos >= 0
22465 && w->cursor.vpos < w->current_matrix->nrows
22466 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22468 struct glyph *g = row->glyphs[TEXT_AREA];
22469 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22470 struct glyph *gpt = g + w->cursor.hpos;
22472 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22474 if (BUFFERP (g->object) && g->charpos != PT)
22476 SET_PT (g->charpos);
22477 w->cursor.vpos = -1;
22478 return make_number (PT);
22480 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22482 ptrdiff_t new_pos;
22484 if (BUFFERP (gpt->object))
22486 new_pos = PT;
22487 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22488 new_pos += (row->reversed_p ? -dir : dir);
22489 else
22490 new_pos -= (row->reversed_p ? -dir : dir);
22491 new_pos = clip_to_bounds (BEGV, new_pos, ZV);
22492 /* If we didn't move, we've hit BEGV or ZV, so we
22493 need to signal a suitable error. */
22494 if (new_pos == PT)
22495 break;
22497 else if (BUFFERP (g->object))
22498 new_pos = g->charpos;
22499 else
22500 break;
22501 SET_PT (new_pos);
22502 w->cursor.vpos = -1;
22503 return make_number (PT);
22505 else if (ROW_GLYPH_NEWLINE_P (row, g))
22507 /* Glyphs inserted at the end of a non-empty line for
22508 positioning the cursor have zero charpos, so we must
22509 deduce the value of point by other means. */
22510 if (g->charpos > 0)
22511 SET_PT (g->charpos);
22512 else if (row->ends_at_zv_p && PT != ZV)
22513 SET_PT (ZV);
22514 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22515 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22516 else
22517 break;
22518 w->cursor.vpos = -1;
22519 return make_number (PT);
22522 if (g == e || NILP (g->object))
22524 if (row->truncated_on_left_p || row->truncated_on_right_p)
22525 goto simulate_display;
22526 if (!row->reversed_p)
22527 row += dir;
22528 else
22529 row -= dir;
22530 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22531 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22532 goto simulate_display;
22534 if (dir > 0)
22536 if (row->reversed_p && !row->continued_p)
22538 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22539 w->cursor.vpos = -1;
22540 return make_number (PT);
22542 g = row->glyphs[TEXT_AREA];
22543 e = g + row->used[TEXT_AREA];
22544 for ( ; g < e; g++)
22546 if (BUFFERP (g->object)
22547 /* Empty lines have only one glyph, which stands
22548 for the newline, and whose charpos is the
22549 buffer position of the newline. */
22550 || ROW_GLYPH_NEWLINE_P (row, g)
22551 /* When the buffer ends in a newline, the line at
22552 EOB also has one glyph, but its charpos is -1. */
22553 || (row->ends_at_zv_p
22554 && !row->reversed_p
22555 && NILP (g->object)
22556 && g->type == CHAR_GLYPH
22557 && g->u.ch == ' '))
22559 if (g->charpos > 0)
22560 SET_PT (g->charpos);
22561 else if (!row->reversed_p
22562 && row->ends_at_zv_p
22563 && PT != ZV)
22564 SET_PT (ZV);
22565 else
22566 continue;
22567 w->cursor.vpos = -1;
22568 return make_number (PT);
22572 else
22574 if (!row->reversed_p && !row->continued_p)
22576 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22577 w->cursor.vpos = -1;
22578 return make_number (PT);
22580 e = row->glyphs[TEXT_AREA];
22581 g = e + row->used[TEXT_AREA] - 1;
22582 for ( ; g >= e; g--)
22584 if (BUFFERP (g->object)
22585 || (ROW_GLYPH_NEWLINE_P (row, g)
22586 && g->charpos > 0)
22587 /* Empty R2L lines on GUI frames have the buffer
22588 position of the newline stored in the stretch
22589 glyph. */
22590 || g->type == STRETCH_GLYPH
22591 || (row->ends_at_zv_p
22592 && row->reversed_p
22593 && NILP (g->object)
22594 && g->type == CHAR_GLYPH
22595 && g->u.ch == ' '))
22597 if (g->charpos > 0)
22598 SET_PT (g->charpos);
22599 else if (row->reversed_p
22600 && row->ends_at_zv_p
22601 && PT != ZV)
22602 SET_PT (ZV);
22603 else
22604 continue;
22605 w->cursor.vpos = -1;
22606 return make_number (PT);
22613 simulate_display:
22615 /* If we wind up here, we failed to move by using the glyphs, so we
22616 need to simulate display instead. */
22618 if (b)
22619 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22620 else
22621 paragraph_dir = Qleft_to_right;
22622 if (EQ (paragraph_dir, Qright_to_left))
22623 dir = -dir;
22624 if (PT <= BEGV && dir < 0)
22625 xsignal0 (Qbeginning_of_buffer);
22626 else if (PT >= ZV && dir > 0)
22627 xsignal0 (Qend_of_buffer);
22628 else
22630 struct text_pos pt;
22631 struct it it;
22632 int pt_x, target_x, pixel_width, pt_vpos;
22633 bool at_eol_p;
22634 bool overshoot_expected = false;
22635 bool target_is_eol_p = false;
22637 /* Setup the arena. */
22638 SET_TEXT_POS (pt, PT, PT_BYTE);
22639 start_display (&it, w, pt);
22640 /* When lines are truncated, we could be called with point
22641 outside of the windows edges, in which case move_it_*
22642 functions either prematurely stop at window's edge or jump to
22643 the next screen line, whereas we rely below on our ability to
22644 reach point, in order to start from its X coordinate. So we
22645 need to disregard the window's horizontal extent in that case. */
22646 if (it.line_wrap == TRUNCATE)
22647 it.last_visible_x = DISP_INFINITY;
22649 if (it.cmp_it.id < 0
22650 && it.method == GET_FROM_STRING
22651 && it.area == TEXT_AREA
22652 && it.string_from_display_prop_p
22653 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22654 overshoot_expected = true;
22656 /* Find the X coordinate of point. We start from the beginning
22657 of this or previous line to make sure we are before point in
22658 the logical order (since the move_it_* functions can only
22659 move forward). */
22660 reseat:
22661 reseat_at_previous_visible_line_start (&it);
22662 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22663 if (IT_CHARPOS (it) != PT)
22665 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22666 -1, -1, -1, MOVE_TO_POS);
22667 /* If we missed point because the character there is
22668 displayed out of a display vector that has more than one
22669 glyph, retry expecting overshoot. */
22670 if (it.method == GET_FROM_DISPLAY_VECTOR
22671 && it.current.dpvec_index > 0
22672 && !overshoot_expected)
22674 overshoot_expected = true;
22675 goto reseat;
22677 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22678 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22680 pt_x = it.current_x;
22681 pt_vpos = it.vpos;
22682 if (dir > 0 || overshoot_expected)
22684 struct glyph_row *row = it.glyph_row;
22686 /* When point is at beginning of line, we don't have
22687 information about the glyph there loaded into struct
22688 it. Calling get_next_display_element fixes that. */
22689 if (pt_x == 0)
22690 get_next_display_element (&it);
22691 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22692 it.glyph_row = NULL;
22693 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22694 it.glyph_row = row;
22695 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22696 it, lest it will become out of sync with it's buffer
22697 position. */
22698 it.current_x = pt_x;
22700 else
22701 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22702 pixel_width = it.pixel_width;
22703 if (overshoot_expected && at_eol_p)
22704 pixel_width = 0;
22705 else if (pixel_width <= 0)
22706 pixel_width = 1;
22708 /* If there's a display string (or something similar) at point,
22709 we are actually at the glyph to the left of point, so we need
22710 to correct the X coordinate. */
22711 if (overshoot_expected)
22713 if (it.bidi_p)
22714 pt_x += pixel_width * it.bidi_it.scan_dir;
22715 else
22716 pt_x += pixel_width;
22719 /* Compute target X coordinate, either to the left or to the
22720 right of point. On TTY frames, all characters have the same
22721 pixel width of 1, so we can use that. On GUI frames we don't
22722 have an easy way of getting at the pixel width of the
22723 character to the left of point, so we use a different method
22724 of getting to that place. */
22725 if (dir > 0)
22726 target_x = pt_x + pixel_width;
22727 else
22728 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22730 /* Target X coordinate could be one line above or below the line
22731 of point, in which case we need to adjust the target X
22732 coordinate. Also, if moving to the left, we need to begin at
22733 the left edge of the point's screen line. */
22734 if (dir < 0)
22736 if (pt_x > 0)
22738 start_display (&it, w, pt);
22739 if (it.line_wrap == TRUNCATE)
22740 it.last_visible_x = DISP_INFINITY;
22741 reseat_at_previous_visible_line_start (&it);
22742 it.current_x = it.current_y = it.hpos = 0;
22743 if (pt_vpos != 0)
22744 move_it_by_lines (&it, pt_vpos);
22746 else
22748 move_it_by_lines (&it, -1);
22749 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22750 target_is_eol_p = true;
22751 /* Under word-wrap, we don't know the x coordinate of
22752 the last character displayed on the previous line,
22753 which immediately precedes the wrap point. To find
22754 out its x coordinate, we try moving to the right
22755 margin of the window, which will stop at the wrap
22756 point, and then reset target_x to point at the
22757 character that precedes the wrap point. This is not
22758 needed on GUI frames, because (see below) there we
22759 move from the left margin one grapheme cluster at a
22760 time, and stop when we hit the wrap point. */
22761 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22763 void *it_data = NULL;
22764 struct it it2;
22766 SAVE_IT (it2, it, it_data);
22767 move_it_in_display_line_to (&it, ZV, target_x,
22768 MOVE_TO_POS | MOVE_TO_X);
22769 /* If we arrived at target_x, that _is_ the last
22770 character on the previous line. */
22771 if (it.current_x != target_x)
22772 target_x = it.current_x - 1;
22773 RESTORE_IT (&it, &it2, it_data);
22777 else
22779 if (at_eol_p
22780 || (target_x >= it.last_visible_x
22781 && it.line_wrap != TRUNCATE))
22783 if (pt_x > 0)
22784 move_it_by_lines (&it, 0);
22785 move_it_by_lines (&it, 1);
22786 target_x = 0;
22790 /* Move to the target X coordinate. */
22791 /* On GUI frames, as we don't know the X coordinate of the
22792 character to the left of point, moving point to the left
22793 requires walking, one grapheme cluster at a time, until we
22794 find ourself at a place immediately to the left of the
22795 character at point. */
22796 if (FRAME_WINDOW_P (it.f) && dir < 0)
22798 struct text_pos new_pos;
22799 enum move_it_result rc = MOVE_X_REACHED;
22801 if (it.current_x == 0)
22802 get_next_display_element (&it);
22803 if (it.what == IT_COMPOSITION)
22805 new_pos.charpos = it.cmp_it.charpos;
22806 new_pos.bytepos = -1;
22808 else
22809 new_pos = it.current.pos;
22811 while (it.current_x + it.pixel_width <= target_x
22812 && (rc == MOVE_X_REACHED
22813 /* Under word-wrap, move_it_in_display_line_to
22814 stops at correct coordinates, but sometimes
22815 returns MOVE_POS_MATCH_OR_ZV. */
22816 || (it.line_wrap == WORD_WRAP
22817 && rc == MOVE_POS_MATCH_OR_ZV)))
22819 int new_x = it.current_x + it.pixel_width;
22821 /* For composed characters, we want the position of the
22822 first character in the grapheme cluster (usually, the
22823 composition's base character), whereas it.current
22824 might give us the position of the _last_ one, e.g. if
22825 the composition is rendered in reverse due to bidi
22826 reordering. */
22827 if (it.what == IT_COMPOSITION)
22829 new_pos.charpos = it.cmp_it.charpos;
22830 new_pos.bytepos = -1;
22832 else
22833 new_pos = it.current.pos;
22834 if (new_x == it.current_x)
22835 new_x++;
22836 rc = move_it_in_display_line_to (&it, ZV, new_x,
22837 MOVE_TO_POS | MOVE_TO_X);
22838 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22839 break;
22841 /* The previous position we saw in the loop is the one we
22842 want. */
22843 if (new_pos.bytepos == -1)
22844 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22845 it.current.pos = new_pos;
22847 else if (it.current_x != target_x)
22848 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22850 /* If we ended up in a display string that covers point, move to
22851 buffer position to the right in the visual order. */
22852 if (dir > 0)
22854 while (IT_CHARPOS (it) == PT)
22856 set_iterator_to_next (&it, false);
22857 if (!get_next_display_element (&it))
22858 break;
22862 /* Move point to that position. */
22863 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22866 return make_number (PT);
22868 #undef ROW_GLYPH_NEWLINE_P
22871 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22872 Sbidi_resolved_levels, 0, 1, 0,
22873 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22875 The resolved levels are produced by the Emacs bidi reordering engine
22876 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22877 read the Unicode Standard Annex 9 (UAX#9) for background information
22878 about these levels.
22880 VPOS is the zero-based number of the current window's screen line
22881 for which to produce the resolved levels. If VPOS is nil or omitted,
22882 it defaults to the screen line of point. If the window displays a
22883 header line, VPOS of zero will report on the header line, and first
22884 line of text in the window will have VPOS of 1.
22886 Value is an array of resolved levels, indexed by glyph number.
22887 Glyphs are numbered from zero starting from the beginning of the
22888 screen line, i.e. the left edge of the window for left-to-right lines
22889 and from the right edge for right-to-left lines. The resolved levels
22890 are produced only for the window's text area; text in display margins
22891 is not included.
22893 If the selected window's display is not up-to-date, or if the specified
22894 screen line does not display text, this function returns nil. It is
22895 highly recommended to bind this function to some simple key, like F8,
22896 in order to avoid these problems.
22898 This function exists mainly for testing the correctness of the
22899 Emacs UBA implementation, in particular with the test suite. */)
22900 (Lisp_Object vpos)
22902 struct window *w = XWINDOW (selected_window);
22903 struct buffer *b = XBUFFER (w->contents);
22904 int nrow;
22905 struct glyph_row *row;
22907 if (NILP (vpos))
22909 int d1, d2, d3, d4, d5;
22911 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22913 else
22915 CHECK_NUMBER_COERCE_MARKER (vpos);
22916 nrow = XINT (vpos);
22919 /* We require up-to-date glyph matrix for this window. */
22920 if (w->window_end_valid
22921 && !windows_or_buffers_changed
22922 && b
22923 && !b->clip_changed
22924 && !b->prevent_redisplay_optimizations_p
22925 && !window_outdated (w)
22926 && nrow >= 0
22927 && nrow < w->current_matrix->nrows
22928 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22929 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22931 struct glyph *g, *e, *g1;
22932 int nglyphs, i;
22933 Lisp_Object levels;
22935 if (!row->reversed_p) /* Left-to-right glyph row. */
22937 g = g1 = row->glyphs[TEXT_AREA];
22938 e = g + row->used[TEXT_AREA];
22940 /* Skip over glyphs at the start of the row that was
22941 generated by redisplay for its own needs. */
22942 while (g < e
22943 && NILP (g->object)
22944 && g->charpos < 0)
22945 g++;
22946 g1 = g;
22948 /* Count the "interesting" glyphs in this row. */
22949 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22950 nglyphs++;
22952 /* Create and fill the array. */
22953 levels = make_uninit_vector (nglyphs);
22954 for (i = 0; g1 < g; i++, g1++)
22955 ASET (levels, i, make_number (g1->resolved_level));
22957 else /* Right-to-left glyph row. */
22959 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22960 e = row->glyphs[TEXT_AREA] - 1;
22961 while (g > e
22962 && NILP (g->object)
22963 && g->charpos < 0)
22964 g--;
22965 g1 = g;
22966 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22967 nglyphs++;
22968 levels = make_uninit_vector (nglyphs);
22969 for (i = 0; g1 > g; i++, g1--)
22970 ASET (levels, i, make_number (g1->resolved_level));
22972 return levels;
22974 else
22975 return Qnil;
22980 /***********************************************************************
22981 Menu Bar
22982 ***********************************************************************/
22984 /* Redisplay the menu bar in the frame for window W.
22986 The menu bar of X frames that don't have X toolkit support is
22987 displayed in a special window W->frame->menu_bar_window.
22989 The menu bar of terminal frames is treated specially as far as
22990 glyph matrices are concerned. Menu bar lines are not part of
22991 windows, so the update is done directly on the frame matrix rows
22992 for the menu bar. */
22994 static void
22995 display_menu_bar (struct window *w)
22997 struct frame *f = XFRAME (WINDOW_FRAME (w));
22998 struct it it;
22999 Lisp_Object items;
23000 int i;
23002 /* Don't do all this for graphical frames. */
23003 #ifdef HAVE_NTGUI
23004 if (FRAME_W32_P (f))
23005 return;
23006 #endif
23007 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
23008 if (FRAME_X_P (f))
23009 return;
23010 #endif
23012 #ifdef HAVE_NS
23013 if (FRAME_NS_P (f))
23014 return;
23015 #endif /* HAVE_NS */
23017 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
23018 eassert (!FRAME_WINDOW_P (f));
23019 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
23020 it.first_visible_x = 0;
23021 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
23022 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
23023 if (FRAME_WINDOW_P (f))
23025 /* Menu bar lines are displayed in the desired matrix of the
23026 dummy window menu_bar_window. */
23027 struct window *menu_w;
23028 menu_w = XWINDOW (f->menu_bar_window);
23029 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
23030 MENU_FACE_ID);
23031 it.first_visible_x = 0;
23032 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
23034 else
23035 #endif /* not USE_X_TOOLKIT and not USE_GTK */
23037 /* This is a TTY frame, i.e. character hpos/vpos are used as
23038 pixel x/y. */
23039 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
23040 MENU_FACE_ID);
23041 it.first_visible_x = 0;
23042 it.last_visible_x = FRAME_COLS (f);
23045 /* FIXME: This should be controlled by a user option. See the
23046 comments in redisplay_tool_bar and display_mode_line about
23047 this. */
23048 it.paragraph_embedding = L2R;
23050 /* Clear all rows of the menu bar. */
23051 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
23053 struct glyph_row *row = it.glyph_row + i;
23054 clear_glyph_row (row);
23055 row->enabled_p = true;
23056 row->full_width_p = true;
23057 row->reversed_p = false;
23060 /* Display all items of the menu bar. */
23061 items = FRAME_MENU_BAR_ITEMS (it.f);
23062 for (i = 0; i < ASIZE (items); i += 4)
23064 Lisp_Object string;
23066 /* Stop at nil string. */
23067 string = AREF (items, i + 1);
23068 if (NILP (string))
23069 break;
23071 /* Remember where item was displayed. */
23072 ASET (items, i + 3, make_number (it.hpos));
23074 /* Display the item, pad with one space. */
23075 if (it.current_x < it.last_visible_x)
23076 display_string (NULL, string, Qnil, 0, 0, &it,
23077 SCHARS (string) + 1, 0, 0, -1);
23080 /* Fill out the line with spaces. */
23081 if (it.current_x < it.last_visible_x)
23082 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23084 /* Compute the total height of the lines. */
23085 compute_line_metrics (&it);
23088 /* Deep copy of a glyph row, including the glyphs. */
23089 static void
23090 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23092 struct glyph *pointers[1 + LAST_AREA];
23093 int to_used = to->used[TEXT_AREA];
23095 /* Save glyph pointers of TO. */
23096 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23098 /* Do a structure assignment. */
23099 *to = *from;
23101 /* Restore original glyph pointers of TO. */
23102 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23104 /* Copy the glyphs. */
23105 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23106 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23108 /* If we filled only part of the TO row, fill the rest with
23109 space_glyph (which will display as empty space). */
23110 if (to_used > from->used[TEXT_AREA])
23111 fill_up_frame_row_with_spaces (to, to_used);
23114 /* Display one menu item on a TTY, by overwriting the glyphs in the
23115 frame F's desired glyph matrix with glyphs produced from the menu
23116 item text. Called from term.c to display TTY drop-down menus one
23117 item at a time.
23119 ITEM_TEXT is the menu item text as a C string.
23121 FACE_ID is the face ID to be used for this menu item. FACE_ID
23122 could specify one of 3 faces: a face for an enabled item, a face
23123 for a disabled item, or a face for a selected item.
23125 X and Y are coordinates of the first glyph in the frame's desired
23126 matrix to be overwritten by the menu item. Since this is a TTY, Y
23127 is the zero-based number of the glyph row and X is the zero-based
23128 glyph number in the row, starting from left, where to start
23129 displaying the item.
23131 SUBMENU means this menu item drops down a submenu, which
23132 should be indicated by displaying a proper visual cue after the
23133 item text. */
23135 void
23136 display_tty_menu_item (const char *item_text, int width, int face_id,
23137 int x, int y, bool submenu)
23139 struct it it;
23140 struct frame *f = SELECTED_FRAME ();
23141 struct window *w = XWINDOW (f->selected_window);
23142 struct glyph_row *row;
23143 size_t item_len = strlen (item_text);
23145 eassert (FRAME_TERMCAP_P (f));
23147 /* Don't write beyond the matrix's last row. This can happen for
23148 TTY screens that are not high enough to show the entire menu.
23149 (This is actually a bit of defensive programming, as
23150 tty_menu_display already limits the number of menu items to one
23151 less than the number of screen lines.) */
23152 if (y >= f->desired_matrix->nrows)
23153 return;
23155 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23156 it.first_visible_x = 0;
23157 it.last_visible_x = FRAME_COLS (f) - 1;
23158 row = it.glyph_row;
23159 /* Start with the row contents from the current matrix. */
23160 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23161 bool saved_width = row->full_width_p;
23162 row->full_width_p = true;
23163 bool saved_reversed = row->reversed_p;
23164 row->reversed_p = false;
23165 row->enabled_p = true;
23167 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23168 desired face. */
23169 eassert (x < f->desired_matrix->matrix_w);
23170 it.current_x = it.hpos = x;
23171 it.current_y = it.vpos = y;
23172 int saved_used = row->used[TEXT_AREA];
23173 bool saved_truncated = row->truncated_on_right_p;
23174 row->used[TEXT_AREA] = x;
23175 it.face_id = face_id;
23176 it.line_wrap = TRUNCATE;
23178 /* FIXME: This should be controlled by a user option. See the
23179 comments in redisplay_tool_bar and display_mode_line about this.
23180 Also, if paragraph_embedding could ever be R2L, changes will be
23181 needed to avoid shifting to the right the row characters in
23182 term.c:append_glyph. */
23183 it.paragraph_embedding = L2R;
23185 /* Pad with a space on the left. */
23186 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23187 width--;
23188 /* Display the menu item, pad with spaces to WIDTH. */
23189 if (submenu)
23191 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23192 item_len, 0, FRAME_COLS (f) - 1, -1);
23193 width -= item_len;
23194 /* Indicate with " >" that there's a submenu. */
23195 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23196 FRAME_COLS (f) - 1, -1);
23198 else
23199 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23200 width, 0, FRAME_COLS (f) - 1, -1);
23202 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23203 row->truncated_on_right_p = saved_truncated;
23204 row->hash = row_hash (row);
23205 row->full_width_p = saved_width;
23206 row->reversed_p = saved_reversed;
23209 /***********************************************************************
23210 Mode Line
23211 ***********************************************************************/
23213 /* Redisplay mode lines in the window tree whose root is WINDOW.
23214 If FORCE, redisplay mode lines unconditionally.
23215 Otherwise, redisplay only mode lines that are garbaged. Value is
23216 the number of windows whose mode lines were redisplayed. */
23218 static int
23219 redisplay_mode_lines (Lisp_Object window, bool force)
23221 int nwindows = 0;
23223 while (!NILP (window))
23225 struct window *w = XWINDOW (window);
23227 if (WINDOWP (w->contents))
23228 nwindows += redisplay_mode_lines (w->contents, force);
23229 else if (force
23230 || FRAME_GARBAGED_P (XFRAME (w->frame))
23231 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23233 struct text_pos lpoint;
23234 struct buffer *old = current_buffer;
23236 /* Set the window's buffer for the mode line display. */
23237 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23238 set_buffer_internal_1 (XBUFFER (w->contents));
23240 /* Point refers normally to the selected window. For any
23241 other window, set up appropriate value. */
23242 if (!EQ (window, selected_window))
23244 struct text_pos pt;
23246 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23247 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23250 /* Display mode lines. */
23251 clear_glyph_matrix (w->desired_matrix);
23252 if (display_mode_lines (w))
23253 ++nwindows;
23255 /* Restore old settings. */
23256 set_buffer_internal_1 (old);
23257 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23260 window = w->next;
23263 return nwindows;
23267 /* Display the mode and/or header line of window W. Value is the
23268 sum number of mode lines and header lines displayed. */
23270 static int
23271 display_mode_lines (struct window *w)
23273 Lisp_Object old_selected_window = selected_window;
23274 Lisp_Object old_selected_frame = selected_frame;
23275 Lisp_Object new_frame = w->frame;
23276 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23277 int n = 0;
23279 if (window_wants_mode_line (w))
23281 Lisp_Object window;
23282 Lisp_Object default_help
23283 = buffer_local_value (Qmode_line_default_help_echo, w->contents);
23285 /* Set up mode line help echo. Do this before selecting w so it
23286 can reasonably tell whether a mouse click will select w. */
23287 XSETWINDOW (window, w);
23288 if (FUNCTIONP (default_help))
23289 wset_mode_line_help_echo (w, safe_call1 (default_help, window));
23290 else if (STRINGP (default_help))
23291 wset_mode_line_help_echo (w, default_help);
23292 else
23293 wset_mode_line_help_echo (w, Qnil);
23296 selected_frame = new_frame;
23297 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23298 or window's point, then we'd need select_window_1 here as well. */
23299 XSETWINDOW (selected_window, w);
23300 XFRAME (new_frame)->selected_window = selected_window;
23302 /* These will be set while the mode line specs are processed. */
23303 line_number_displayed = false;
23304 w->column_number_displayed = -1;
23306 if (window_wants_mode_line (w))
23308 Lisp_Object window_mode_line_format
23309 = window_parameter (w, Qmode_line_format);
23310 struct window *sel_w = XWINDOW (old_selected_window);
23312 /* Select mode line face based on the real selected window. */
23313 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23314 NILP (window_mode_line_format)
23315 ? BVAR (current_buffer, mode_line_format)
23316 : window_mode_line_format);
23317 ++n;
23320 if (window_wants_header_line (w))
23322 Lisp_Object window_header_line_format
23323 = window_parameter (w, Qheader_line_format);
23325 display_mode_line (w, HEADER_LINE_FACE_ID,
23326 NILP (window_header_line_format)
23327 ? BVAR (current_buffer, header_line_format)
23328 : window_header_line_format);
23329 ++n;
23332 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23333 selected_frame = old_selected_frame;
23334 selected_window = old_selected_window;
23335 if (n > 0)
23336 w->must_be_updated_p = true;
23337 return n;
23341 /* Display mode or header line of window W. FACE_ID specifies which
23342 line to display; it is either MODE_LINE_FACE_ID or
23343 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23344 display. Value is the pixel height of the mode/header line
23345 displayed. */
23347 static int
23348 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23350 struct it it;
23351 struct face *face;
23352 ptrdiff_t count = SPECPDL_INDEX ();
23354 init_iterator (&it, w, -1, -1, NULL, face_id);
23355 /* Don't extend on a previously drawn mode-line.
23356 This may happen if called from pos_visible_p. */
23357 it.glyph_row->enabled_p = false;
23358 prepare_desired_row (w, it.glyph_row, true);
23360 it.glyph_row->mode_line_p = true;
23362 /* FIXME: This should be controlled by a user option. But
23363 supporting such an option is not trivial, since the mode line is
23364 made up of many separate strings. */
23365 it.paragraph_embedding = L2R;
23367 record_unwind_protect (unwind_format_mode_line,
23368 format_mode_line_unwind_data (NULL, NULL,
23369 Qnil, false));
23371 mode_line_target = MODE_LINE_DISPLAY;
23373 /* Temporarily make frame's keyboard the current kboard so that
23374 kboard-local variables in the mode_line_format will get the right
23375 values. */
23376 push_kboard (FRAME_KBOARD (it.f));
23377 record_unwind_save_match_data ();
23378 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23379 pop_kboard ();
23381 unbind_to (count, Qnil);
23383 /* Fill up with spaces. */
23384 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23386 compute_line_metrics (&it);
23387 it.glyph_row->full_width_p = true;
23388 it.glyph_row->continued_p = false;
23389 it.glyph_row->truncated_on_left_p = false;
23390 it.glyph_row->truncated_on_right_p = false;
23392 /* Make a 3D mode-line have a shadow at its right end. */
23393 face = FACE_FROM_ID (it.f, face_id);
23394 extend_face_to_end_of_line (&it);
23395 if (face->box != FACE_NO_BOX)
23397 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23398 + it.glyph_row->used[TEXT_AREA] - 1);
23399 last->right_box_line_p = true;
23402 return it.glyph_row->height;
23405 /* Move element ELT in LIST to the front of LIST.
23406 Return the updated list. */
23408 static Lisp_Object
23409 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23411 register Lisp_Object tail, prev;
23412 register Lisp_Object tem;
23414 tail = list;
23415 prev = Qnil;
23416 while (CONSP (tail))
23418 tem = XCAR (tail);
23420 if (EQ (elt, tem))
23422 /* Splice out the link TAIL. */
23423 if (NILP (prev))
23424 list = XCDR (tail);
23425 else
23426 Fsetcdr (prev, XCDR (tail));
23428 /* Now make it the first. */
23429 Fsetcdr (tail, list);
23430 return tail;
23432 else
23433 prev = tail;
23434 tail = XCDR (tail);
23435 maybe_quit ();
23438 /* Not found--return unchanged LIST. */
23439 return list;
23442 /* Contribute ELT to the mode line for window IT->w. How it
23443 translates into text depends on its data type.
23445 IT describes the display environment in which we display, as usual.
23447 DEPTH is the depth in recursion. It is used to prevent
23448 infinite recursion here.
23450 FIELD_WIDTH is the number of characters the display of ELT should
23451 occupy in the mode line, and PRECISION is the maximum number of
23452 characters to display from ELT's representation. See
23453 display_string for details.
23455 Returns the hpos of the end of the text generated by ELT.
23457 PROPS is a property list to add to any string we encounter.
23459 If RISKY, remove (disregard) any properties in any string
23460 we encounter, and ignore :eval and :propertize.
23462 The global variable `mode_line_target' determines whether the
23463 output is passed to `store_mode_line_noprop',
23464 `store_mode_line_string', or `display_string'. */
23466 static int
23467 display_mode_element (struct it *it, int depth, int field_width, int precision,
23468 Lisp_Object elt, Lisp_Object props, bool risky)
23470 int n = 0, field, prec;
23471 bool literal = false;
23473 tail_recurse:
23474 if (depth > 100)
23475 elt = build_string ("*too-deep*");
23477 depth++;
23479 switch (XTYPE (elt))
23481 case Lisp_String:
23483 /* A string: output it and check for %-constructs within it. */
23484 unsigned char c;
23485 ptrdiff_t offset = 0;
23487 if (SCHARS (elt) > 0
23488 && (!NILP (props) || risky))
23490 Lisp_Object oprops, aelt;
23491 oprops = Ftext_properties_at (make_number (0), elt);
23493 /* If the starting string's properties are not what
23494 we want, translate the string. Also, if the string
23495 is risky, do that anyway. */
23497 if (NILP (Fequal (props, oprops)) || risky)
23499 /* If the starting string has properties,
23500 merge the specified ones onto the existing ones. */
23501 if (! NILP (oprops) && !risky)
23503 Lisp_Object tem;
23505 oprops = Fcopy_sequence (oprops);
23506 tem = props;
23507 while (CONSP (tem))
23509 oprops = Fplist_put (oprops, XCAR (tem),
23510 XCAR (XCDR (tem)));
23511 tem = XCDR (XCDR (tem));
23513 props = oprops;
23516 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23517 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23519 /* AELT is what we want. Move it to the front
23520 without consing. */
23521 elt = XCAR (aelt);
23522 mode_line_proptrans_alist
23523 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23525 else
23527 Lisp_Object tem;
23529 /* If AELT has the wrong props, it is useless.
23530 so get rid of it. */
23531 if (! NILP (aelt))
23532 mode_line_proptrans_alist
23533 = Fdelq (aelt, mode_line_proptrans_alist);
23535 elt = Fcopy_sequence (elt);
23536 Fset_text_properties (make_number (0), Flength (elt),
23537 props, elt);
23538 /* Add this item to mode_line_proptrans_alist. */
23539 mode_line_proptrans_alist
23540 = Fcons (Fcons (elt, props),
23541 mode_line_proptrans_alist);
23542 /* Truncate mode_line_proptrans_alist
23543 to at most 50 elements. */
23544 tem = Fnthcdr (make_number (50),
23545 mode_line_proptrans_alist);
23546 if (! NILP (tem))
23547 XSETCDR (tem, Qnil);
23552 offset = 0;
23554 if (literal)
23556 prec = precision - n;
23557 switch (mode_line_target)
23559 case MODE_LINE_NOPROP:
23560 case MODE_LINE_TITLE:
23561 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23562 break;
23563 case MODE_LINE_STRING:
23564 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23565 break;
23566 case MODE_LINE_DISPLAY:
23567 n += display_string (NULL, elt, Qnil, 0, 0, it,
23568 0, prec, 0, STRING_MULTIBYTE (elt));
23569 break;
23572 break;
23575 /* Handle the non-literal case. */
23577 while ((precision <= 0 || n < precision)
23578 && SREF (elt, offset) != 0
23579 && (mode_line_target != MODE_LINE_DISPLAY
23580 || it->current_x < it->last_visible_x))
23582 ptrdiff_t last_offset = offset;
23584 /* Advance to end of string or next format specifier. */
23585 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23588 if (offset - 1 != last_offset)
23590 ptrdiff_t nchars, nbytes;
23592 /* Output to end of string or up to '%'. Field width
23593 is length of string. Don't output more than
23594 PRECISION allows us. */
23595 offset--;
23597 prec = c_string_width (SDATA (elt) + last_offset,
23598 offset - last_offset, precision - n,
23599 &nchars, &nbytes);
23601 switch (mode_line_target)
23603 case MODE_LINE_NOPROP:
23604 case MODE_LINE_TITLE:
23605 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23606 break;
23607 case MODE_LINE_STRING:
23609 ptrdiff_t bytepos = last_offset;
23610 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23611 ptrdiff_t endpos = (precision <= 0
23612 ? string_byte_to_char (elt, offset)
23613 : charpos + nchars);
23614 Lisp_Object mode_string
23615 = Fsubstring (elt, make_number (charpos),
23616 make_number (endpos));
23617 n += store_mode_line_string (NULL, mode_string, false,
23618 0, 0, Qnil);
23620 break;
23621 case MODE_LINE_DISPLAY:
23623 ptrdiff_t bytepos = last_offset;
23624 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23626 if (precision <= 0)
23627 nchars = string_byte_to_char (elt, offset) - charpos;
23628 n += display_string (NULL, elt, Qnil, 0, charpos,
23629 it, 0, nchars, 0,
23630 STRING_MULTIBYTE (elt));
23632 break;
23635 else /* c == '%' */
23637 ptrdiff_t percent_position = offset;
23639 /* Get the specified minimum width. Zero means
23640 don't pad. */
23641 field = 0;
23642 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23643 field = field * 10 + c - '0';
23645 /* Don't pad beyond the total padding allowed. */
23646 if (field_width - n > 0 && field > field_width - n)
23647 field = field_width - n;
23649 /* Note that either PRECISION <= 0 or N < PRECISION. */
23650 prec = precision - n;
23652 if (c == 'M')
23653 n += display_mode_element (it, depth, field, prec,
23654 Vglobal_mode_string, props,
23655 risky);
23656 else if (c != 0)
23658 bool multibyte;
23659 ptrdiff_t bytepos, charpos;
23660 const char *spec;
23661 Lisp_Object string;
23663 bytepos = percent_position;
23664 charpos = (STRING_MULTIBYTE (elt)
23665 ? string_byte_to_char (elt, bytepos)
23666 : bytepos);
23667 spec = decode_mode_spec (it->w, c, field, &string);
23668 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23670 switch (mode_line_target)
23672 case MODE_LINE_NOPROP:
23673 case MODE_LINE_TITLE:
23674 n += store_mode_line_noprop (spec, field, prec);
23675 break;
23676 case MODE_LINE_STRING:
23678 Lisp_Object tem = build_string (spec);
23679 props = Ftext_properties_at (make_number (charpos), elt);
23680 /* Should only keep face property in props */
23681 n += store_mode_line_string (NULL, tem, false,
23682 field, prec, props);
23684 break;
23685 case MODE_LINE_DISPLAY:
23687 int nglyphs_before, nwritten;
23689 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23690 nwritten = display_string (spec, string, elt,
23691 charpos, 0, it,
23692 field, prec, 0,
23693 multibyte);
23695 /* Assign to the glyphs written above the
23696 string where the `%x' came from, position
23697 of the `%'. */
23698 if (nwritten > 0)
23700 struct glyph *glyph
23701 = (it->glyph_row->glyphs[TEXT_AREA]
23702 + nglyphs_before);
23703 int i;
23705 for (i = 0; i < nwritten; ++i)
23707 glyph[i].object = elt;
23708 glyph[i].charpos = charpos;
23711 n += nwritten;
23714 break;
23717 else /* c == 0 */
23718 break;
23722 break;
23724 case Lisp_Symbol:
23725 /* A symbol: process the value of the symbol recursively
23726 as if it appeared here directly. Avoid error if symbol void.
23727 Special case: if value of symbol is a string, output the string
23728 literally. */
23730 register Lisp_Object tem;
23732 /* If the variable is not marked as risky to set
23733 then its contents are risky to use. */
23734 if (NILP (Fget (elt, Qrisky_local_variable)))
23735 risky = true;
23737 tem = Fboundp (elt);
23738 if (!NILP (tem))
23740 tem = Fsymbol_value (elt);
23741 /* If value is a string, output that string literally:
23742 don't check for % within it. */
23743 if (STRINGP (tem))
23744 literal = true;
23746 if (!EQ (tem, elt))
23748 /* Give up right away for nil or t. */
23749 elt = tem;
23750 goto tail_recurse;
23754 break;
23756 case Lisp_Cons:
23758 register Lisp_Object car, tem;
23760 /* A cons cell: five distinct cases.
23761 If first element is :eval or :propertize, do something special.
23762 If first element is a string or a cons, process all the elements
23763 and effectively concatenate them.
23764 If first element is a negative number, truncate displaying cdr to
23765 at most that many characters. If positive, pad (with spaces)
23766 to at least that many characters.
23767 If first element is a symbol, process the cadr or caddr recursively
23768 according to whether the symbol's value is non-nil or nil. */
23769 car = XCAR (elt);
23770 if (EQ (car, QCeval))
23772 /* An element of the form (:eval FORM) means evaluate FORM
23773 and use the result as mode line elements. */
23775 if (risky)
23776 break;
23778 if (CONSP (XCDR (elt)))
23780 Lisp_Object spec;
23781 spec = safe__eval (true, XCAR (XCDR (elt)));
23782 /* The :eval form could delete the frame stored in the
23783 iterator, which will cause a crash if we try to
23784 access faces and other fields (e.g., FRAME_KBOARD)
23785 on that frame. This is a nonsensical thing to do,
23786 and signaling an error from redisplay might be
23787 dangerous, but we cannot continue with an invalid frame. */
23788 if (!FRAME_LIVE_P (it->f))
23789 signal_error (":eval deleted the frame being displayed", elt);
23790 n += display_mode_element (it, depth, field_width - n,
23791 precision - n, spec, props,
23792 risky);
23795 else if (EQ (car, QCpropertize))
23797 /* An element of the form (:propertize ELT PROPS...)
23798 means display ELT but applying properties PROPS. */
23800 if (risky)
23801 break;
23803 if (CONSP (XCDR (elt)))
23804 n += display_mode_element (it, depth, field_width - n,
23805 precision - n, XCAR (XCDR (elt)),
23806 XCDR (XCDR (elt)), risky);
23808 else if (SYMBOLP (car))
23810 tem = Fboundp (car);
23811 elt = XCDR (elt);
23812 if (!CONSP (elt))
23813 goto invalid;
23814 /* elt is now the cdr, and we know it is a cons cell.
23815 Use its car if CAR has a non-nil value. */
23816 if (!NILP (tem))
23818 tem = Fsymbol_value (car);
23819 if (!NILP (tem))
23821 elt = XCAR (elt);
23822 goto tail_recurse;
23825 /* Symbol's value is nil (or symbol is unbound)
23826 Get the cddr of the original list
23827 and if possible find the caddr and use that. */
23828 elt = XCDR (elt);
23829 if (NILP (elt))
23830 break;
23831 else if (!CONSP (elt))
23832 goto invalid;
23833 elt = XCAR (elt);
23834 goto tail_recurse;
23836 else if (INTEGERP (car))
23838 register int lim = XINT (car);
23839 elt = XCDR (elt);
23840 if (lim < 0)
23842 /* Negative int means reduce maximum width. */
23843 if (precision <= 0)
23844 precision = -lim;
23845 else
23846 precision = min (precision, -lim);
23848 else if (lim > 0)
23850 /* Padding specified. Don't let it be more than
23851 current maximum. */
23852 if (precision > 0)
23853 lim = min (precision, lim);
23855 /* If that's more padding than already wanted, queue it.
23856 But don't reduce padding already specified even if
23857 that is beyond the current truncation point. */
23858 field_width = max (lim, field_width);
23860 goto tail_recurse;
23862 else if (STRINGP (car) || CONSP (car))
23863 FOR_EACH_TAIL_SAFE (elt)
23865 if (0 < precision && precision <= n)
23866 break;
23867 n += display_mode_element (it, depth,
23868 /* Pad after only the last
23869 list element. */
23870 (! CONSP (XCDR (elt))
23871 ? field_width - n
23872 : 0),
23873 precision - n, XCAR (elt),
23874 props, risky);
23877 break;
23879 default:
23880 invalid:
23881 elt = build_string ("*invalid*");
23882 goto tail_recurse;
23885 /* Pad to FIELD_WIDTH. */
23886 if (field_width > 0 && n < field_width)
23888 switch (mode_line_target)
23890 case MODE_LINE_NOPROP:
23891 case MODE_LINE_TITLE:
23892 n += store_mode_line_noprop ("", field_width - n, 0);
23893 break;
23894 case MODE_LINE_STRING:
23895 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23896 Qnil);
23897 break;
23898 case MODE_LINE_DISPLAY:
23899 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23900 0, 0, 0);
23901 break;
23905 return n;
23908 /* Store a mode-line string element in mode_line_string_list.
23910 If STRING is non-null, display that C string. Otherwise, the Lisp
23911 string LISP_STRING is displayed.
23913 FIELD_WIDTH is the minimum number of output glyphs to produce.
23914 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23915 with spaces. FIELD_WIDTH <= 0 means don't pad.
23917 PRECISION is the maximum number of characters to output from
23918 STRING. PRECISION <= 0 means don't truncate the string.
23920 If COPY_STRING, make a copy of LISP_STRING before adding
23921 properties to the string.
23923 PROPS are the properties to add to the string.
23924 The mode_line_string_face face property is always added to the string.
23927 static int
23928 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23929 bool copy_string,
23930 int field_width, int precision, Lisp_Object props)
23932 ptrdiff_t len;
23933 int n = 0;
23935 if (string != NULL)
23937 len = strlen (string);
23938 if (precision > 0 && len > precision)
23939 len = precision;
23940 lisp_string = make_string (string, len);
23941 if (NILP (props))
23942 props = mode_line_string_face_prop;
23943 else if (!NILP (mode_line_string_face))
23945 Lisp_Object face = Fplist_get (props, Qface);
23946 props = Fcopy_sequence (props);
23947 if (NILP (face))
23948 face = mode_line_string_face;
23949 else
23950 face = list2 (face, mode_line_string_face);
23951 props = Fplist_put (props, Qface, face);
23953 Fadd_text_properties (make_number (0), make_number (len),
23954 props, lisp_string);
23956 else
23958 len = XFASTINT (Flength (lisp_string));
23959 if (precision > 0 && len > precision)
23961 len = precision;
23962 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23963 precision = -1;
23965 if (!NILP (mode_line_string_face))
23967 Lisp_Object face;
23968 if (NILP (props))
23969 props = Ftext_properties_at (make_number (0), lisp_string);
23970 face = Fplist_get (props, Qface);
23971 if (NILP (face))
23972 face = mode_line_string_face;
23973 else
23974 face = list2 (face, mode_line_string_face);
23975 props = list2 (Qface, face);
23976 if (copy_string)
23977 lisp_string = Fcopy_sequence (lisp_string);
23979 if (!NILP (props))
23980 Fadd_text_properties (make_number (0), make_number (len),
23981 props, lisp_string);
23984 if (len > 0)
23986 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23987 n += len;
23990 if (field_width > len)
23992 field_width -= len;
23993 lisp_string = Fmake_string (make_number (field_width), make_number (' '),
23994 Qnil);
23995 if (!NILP (props))
23996 Fadd_text_properties (make_number (0), make_number (field_width),
23997 props, lisp_string);
23998 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23999 n += field_width;
24002 return n;
24006 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
24007 1, 4, 0,
24008 doc: /* Format a string out of a mode line format specification.
24009 First arg FORMAT specifies the mode line format (see `mode-line-format'
24010 for details) to use.
24012 By default, the format is evaluated for the currently selected window.
24014 Optional second arg FACE specifies the face property to put on all
24015 characters for which no face is specified. The value nil means the
24016 default face. The value t means whatever face the window's mode line
24017 currently uses (either `mode-line' or `mode-line-inactive',
24018 depending on whether the window is the selected window or not).
24019 An integer value means the value string has no text
24020 properties.
24022 Optional third and fourth args WINDOW and BUFFER specify the window
24023 and buffer to use as the context for the formatting (defaults
24024 are the selected window and the WINDOW's buffer). */)
24025 (Lisp_Object format, Lisp_Object face,
24026 Lisp_Object window, Lisp_Object buffer)
24028 struct it it;
24029 int len;
24030 struct window *w;
24031 struct buffer *old_buffer = NULL;
24032 int face_id;
24033 bool no_props = INTEGERP (face);
24034 ptrdiff_t count = SPECPDL_INDEX ();
24035 Lisp_Object str;
24036 int string_start = 0;
24038 w = decode_any_window (window);
24039 XSETWINDOW (window, w);
24041 if (NILP (buffer))
24042 buffer = w->contents;
24043 CHECK_BUFFER (buffer);
24045 /* Make formatting the modeline a non-op when noninteractive, otherwise
24046 there will be problems later caused by a partially initialized frame. */
24047 if (NILP (format) || noninteractive)
24048 return empty_unibyte_string;
24050 if (no_props)
24051 face = Qnil;
24053 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
24054 : EQ (face, Qt) ? (EQ (window, selected_window)
24055 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
24056 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
24057 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
24058 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
24059 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
24060 : DEFAULT_FACE_ID;
24062 old_buffer = current_buffer;
24064 /* Save things including mode_line_proptrans_alist,
24065 and set that to nil so that we don't alter the outer value. */
24066 record_unwind_protect (unwind_format_mode_line,
24067 format_mode_line_unwind_data
24068 (XFRAME (WINDOW_FRAME (w)),
24069 old_buffer, selected_window, true));
24070 mode_line_proptrans_alist = Qnil;
24072 Fselect_window (window, Qt);
24073 set_buffer_internal_1 (XBUFFER (buffer));
24075 init_iterator (&it, w, -1, -1, NULL, face_id);
24077 if (no_props)
24079 mode_line_target = MODE_LINE_NOPROP;
24080 mode_line_string_face_prop = Qnil;
24081 mode_line_string_list = Qnil;
24082 string_start = MODE_LINE_NOPROP_LEN (0);
24084 else
24086 mode_line_target = MODE_LINE_STRING;
24087 mode_line_string_list = Qnil;
24088 mode_line_string_face = face;
24089 mode_line_string_face_prop
24090 = NILP (face) ? Qnil : list2 (Qface, face);
24093 push_kboard (FRAME_KBOARD (it.f));
24094 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
24095 pop_kboard ();
24097 if (no_props)
24099 len = MODE_LINE_NOPROP_LEN (string_start);
24100 str = make_string (mode_line_noprop_buf + string_start, len);
24102 else
24104 mode_line_string_list = Fnreverse (mode_line_string_list);
24105 str = Fmapconcat (Qidentity, mode_line_string_list,
24106 empty_unibyte_string);
24109 unbind_to (count, Qnil);
24110 return str;
24113 /* Write a null-terminated, right justified decimal representation of
24114 the positive integer D to BUF using a minimal field width WIDTH. */
24116 static void
24117 pint2str (register char *buf, register int width, register ptrdiff_t d)
24119 register char *p = buf;
24121 if (d <= 0)
24122 *p++ = '0';
24123 else
24125 while (d > 0)
24127 *p++ = d % 10 + '0';
24128 d /= 10;
24132 for (width -= (int) (p - buf); width > 0; --width)
24133 *p++ = ' ';
24134 *p-- = '\0';
24135 while (p > buf)
24137 d = *buf;
24138 *buf++ = *p;
24139 *p-- = d;
24143 /* Write a null-terminated, right justified decimal and "human
24144 readable" representation of the nonnegative integer D to BUF using
24145 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24147 static const char power_letter[] =
24149 0, /* no letter */
24150 'k', /* kilo */
24151 'M', /* mega */
24152 'G', /* giga */
24153 'T', /* tera */
24154 'P', /* peta */
24155 'E', /* exa */
24156 'Z', /* zetta */
24157 'Y' /* yotta */
24160 static void
24161 pint2hrstr (char *buf, int width, ptrdiff_t d)
24163 /* We aim to represent the nonnegative integer D as
24164 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24165 ptrdiff_t quotient = d;
24166 int remainder = 0;
24167 /* -1 means: do not use TENTHS. */
24168 int tenths = -1;
24169 int exponent = 0;
24171 /* Length of QUOTIENT.TENTHS as a string. */
24172 int length;
24174 char * psuffix;
24175 char * p;
24177 if (quotient >= 1000)
24179 /* Scale to the appropriate EXPONENT. */
24182 remainder = quotient % 1000;
24183 quotient /= 1000;
24184 exponent++;
24186 while (quotient >= 1000);
24188 /* Round to nearest and decide whether to use TENTHS or not. */
24189 if (quotient <= 9)
24191 tenths = remainder / 100;
24192 if (remainder % 100 >= 50)
24194 if (tenths < 9)
24195 tenths++;
24196 else
24198 quotient++;
24199 if (quotient == 10)
24200 tenths = -1;
24201 else
24202 tenths = 0;
24206 else
24207 if (remainder >= 500)
24209 if (quotient < 999)
24210 quotient++;
24211 else
24213 quotient = 1;
24214 exponent++;
24215 tenths = 0;
24220 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24221 if (tenths == -1 && quotient <= 99)
24222 if (quotient <= 9)
24223 length = 1;
24224 else
24225 length = 2;
24226 else
24227 length = 3;
24228 p = psuffix = buf + max (width, length);
24230 /* Print EXPONENT. */
24231 *psuffix++ = power_letter[exponent];
24232 *psuffix = '\0';
24234 /* Print TENTHS. */
24235 if (tenths >= 0)
24237 *--p = '0' + tenths;
24238 *--p = '.';
24241 /* Print QUOTIENT. */
24244 int digit = quotient % 10;
24245 *--p = '0' + digit;
24247 while ((quotient /= 10) != 0);
24249 /* Print leading spaces. */
24250 while (buf < p)
24251 *--p = ' ';
24254 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24255 If EOL_FLAG, set also a mnemonic character for end-of-line
24256 type of CODING_SYSTEM. Return updated pointer into BUF. */
24258 static unsigned char invalid_eol_type[] = "(*invalid*)";
24260 static char *
24261 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24263 Lisp_Object val;
24264 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24265 const unsigned char *eol_str;
24266 int eol_str_len;
24267 /* The EOL conversion we are using. */
24268 Lisp_Object eoltype;
24270 val = CODING_SYSTEM_SPEC (coding_system);
24271 eoltype = Qnil;
24273 if (!VECTORP (val)) /* Not yet decided. */
24275 *buf++ = multibyte ? '-' : ' ';
24276 if (eol_flag)
24277 eoltype = eol_mnemonic_undecided;
24278 /* Don't mention EOL conversion if it isn't decided. */
24280 else
24282 Lisp_Object attrs;
24283 Lisp_Object eolvalue;
24285 attrs = AREF (val, 0);
24286 eolvalue = AREF (val, 2);
24288 *buf++ = multibyte
24289 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24290 : ' ';
24292 if (eol_flag)
24294 /* The EOL conversion that is normal on this system. */
24296 if (NILP (eolvalue)) /* Not yet decided. */
24297 eoltype = eol_mnemonic_undecided;
24298 else if (VECTORP (eolvalue)) /* Not yet decided. */
24299 eoltype = eol_mnemonic_undecided;
24300 else /* eolvalue is Qunix, Qdos, or Qmac. */
24301 eoltype = (EQ (eolvalue, Qunix)
24302 ? eol_mnemonic_unix
24303 : EQ (eolvalue, Qdos)
24304 ? eol_mnemonic_dos : eol_mnemonic_mac);
24308 if (eol_flag)
24310 /* Mention the EOL conversion if it is not the usual one. */
24311 if (STRINGP (eoltype))
24313 eol_str = SDATA (eoltype);
24314 eol_str_len = SBYTES (eoltype);
24316 else if (CHARACTERP (eoltype))
24318 int c = XFASTINT (eoltype);
24319 return buf + CHAR_STRING (c, (unsigned char *) buf);
24321 else
24323 eol_str = invalid_eol_type;
24324 eol_str_len = sizeof (invalid_eol_type) - 1;
24326 memcpy (buf, eol_str, eol_str_len);
24327 buf += eol_str_len;
24330 return buf;
24333 /* Return the approximate percentage N is of D (rounding upward), or 99,
24334 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24336 static int
24337 percent99 (ptrdiff_t n, ptrdiff_t d)
24339 int percent = (d - 1 + 100.0 * n) / d;
24340 return min (percent, 99);
24343 /* Return a string for the output of a mode line %-spec for window W,
24344 generated by character C. FIELD_WIDTH > 0 means pad the string
24345 returned with spaces to that value. Return a Lisp string in
24346 *STRING if the resulting string is taken from that Lisp string.
24348 Note we operate on the current buffer for most purposes. */
24350 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24352 static const char *
24353 decode_mode_spec (struct window *w, register int c, int field_width,
24354 Lisp_Object *string)
24356 Lisp_Object obj;
24357 struct frame *f = XFRAME (WINDOW_FRAME (w));
24358 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24359 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24360 produce strings from numerical values, so limit preposterously
24361 large values of FIELD_WIDTH to avoid overrunning the buffer's
24362 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24363 bytes plus the terminating null. */
24364 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24365 struct buffer *b = current_buffer;
24367 obj = Qnil;
24368 *string = Qnil;
24370 switch (c)
24372 case '*':
24373 if (!NILP (BVAR (b, read_only)))
24374 return "%";
24375 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24376 return "*";
24377 return "-";
24379 case '+':
24380 /* This differs from %* only for a modified read-only buffer. */
24381 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24382 return "*";
24383 if (!NILP (BVAR (b, read_only)))
24384 return "%";
24385 return "-";
24387 case '&':
24388 /* This differs from %* in ignoring read-only-ness. */
24389 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24390 return "*";
24391 return "-";
24393 case '%':
24394 return "%";
24396 case '[':
24398 int i;
24399 char *p;
24401 if (command_loop_level > 5)
24402 return "[[[... ";
24403 p = decode_mode_spec_buf;
24404 for (i = 0; i < command_loop_level; i++)
24405 *p++ = '[';
24406 *p = 0;
24407 return decode_mode_spec_buf;
24410 case ']':
24412 int i;
24413 char *p;
24415 if (command_loop_level > 5)
24416 return " ...]]]";
24417 p = decode_mode_spec_buf;
24418 for (i = 0; i < command_loop_level; i++)
24419 *p++ = ']';
24420 *p = 0;
24421 return decode_mode_spec_buf;
24424 case '-':
24426 register int i;
24428 /* Let lots_of_dashes be a string of infinite length. */
24429 if (mode_line_target == MODE_LINE_NOPROP
24430 || mode_line_target == MODE_LINE_STRING)
24431 return "--";
24432 if (field_width <= 0
24433 || field_width > sizeof (lots_of_dashes))
24435 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24436 decode_mode_spec_buf[i] = '-';
24437 decode_mode_spec_buf[i] = '\0';
24438 return decode_mode_spec_buf;
24440 else
24441 return lots_of_dashes;
24444 case 'b':
24445 obj = BVAR (b, name);
24446 break;
24448 case 'c':
24449 case 'C':
24450 /* %c, %C, and %l are ignored in `frame-title-format'.
24451 (In redisplay_internal, the frame title is drawn _before_ the
24452 windows are updated, so the stuff which depends on actual
24453 window contents (such as %l) may fail to render properly, or
24454 even crash emacs.) */
24455 if (mode_line_target == MODE_LINE_TITLE)
24456 return "";
24457 else
24459 ptrdiff_t col = current_column ();
24460 int disp_col = (c == 'C') ? col + 1 : col;
24461 w->column_number_displayed = col;
24462 pint2str (decode_mode_spec_buf, width, disp_col);
24463 return decode_mode_spec_buf;
24466 case 'e':
24467 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24469 if (NILP (Vmemory_full))
24470 return "";
24471 else
24472 return "!MEM FULL! ";
24474 #else
24475 return "";
24476 #endif
24478 case 'F':
24479 /* %F displays the frame name. */
24480 if (!NILP (f->title))
24481 return SSDATA (f->title);
24482 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24483 return SSDATA (f->name);
24484 return "Emacs";
24486 case 'f':
24487 obj = BVAR (b, filename);
24488 break;
24490 case 'i':
24492 ptrdiff_t size = ZV - BEGV;
24493 pint2str (decode_mode_spec_buf, width, size);
24494 return decode_mode_spec_buf;
24497 case 'I':
24499 ptrdiff_t size = ZV - BEGV;
24500 pint2hrstr (decode_mode_spec_buf, width, size);
24501 return decode_mode_spec_buf;
24504 case 'l':
24506 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24507 ptrdiff_t topline, nlines, height;
24508 ptrdiff_t junk;
24510 /* %c, %C, and %l are ignored in `frame-title-format'. */
24511 if (mode_line_target == MODE_LINE_TITLE)
24512 return "";
24514 startpos = marker_position (w->start);
24515 startpos_byte = marker_byte_position (w->start);
24516 height = WINDOW_TOTAL_LINES (w);
24518 /* If we decided that this buffer isn't suitable for line numbers,
24519 don't forget that too fast. */
24520 if (w->base_line_pos == -1)
24521 goto no_value;
24523 /* If the buffer is very big, don't waste time. */
24524 if (INTEGERP (Vline_number_display_limit)
24525 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24527 w->base_line_pos = 0;
24528 w->base_line_number = 0;
24529 goto no_value;
24532 if (w->base_line_number > 0
24533 && w->base_line_pos > 0
24534 && w->base_line_pos <= startpos)
24536 line = w->base_line_number;
24537 linepos = w->base_line_pos;
24538 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24540 else
24542 line = 1;
24543 linepos = BUF_BEGV (b);
24544 linepos_byte = BUF_BEGV_BYTE (b);
24547 /* Count lines from base line to window start position. */
24548 nlines = display_count_lines (linepos_byte,
24549 startpos_byte,
24550 startpos, &junk);
24552 topline = nlines + line;
24554 /* Determine a new base line, if the old one is too close
24555 or too far away, or if we did not have one.
24556 "Too close" means it's plausible a scroll-down would
24557 go back past it. */
24558 if (startpos == BUF_BEGV (b))
24560 w->base_line_number = topline;
24561 w->base_line_pos = BUF_BEGV (b);
24563 else if (nlines < height + 25 || nlines > height * 3 + 50
24564 || linepos == BUF_BEGV (b))
24566 ptrdiff_t limit = BUF_BEGV (b);
24567 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24568 ptrdiff_t position;
24569 ptrdiff_t distance =
24570 (height * 2 + 30) * line_number_display_limit_width;
24572 if (startpos - distance > limit)
24574 limit = startpos - distance;
24575 limit_byte = CHAR_TO_BYTE (limit);
24578 nlines = display_count_lines (startpos_byte,
24579 limit_byte,
24580 - (height * 2 + 30),
24581 &position);
24582 /* If we couldn't find the lines we wanted within
24583 line_number_display_limit_width chars per line,
24584 give up on line numbers for this window. */
24585 if (position == limit_byte && limit == startpos - distance)
24587 w->base_line_pos = -1;
24588 w->base_line_number = 0;
24589 goto no_value;
24592 w->base_line_number = topline - nlines;
24593 w->base_line_pos = BYTE_TO_CHAR (position);
24596 /* Now count lines from the start pos to point. */
24597 nlines = display_count_lines (startpos_byte,
24598 PT_BYTE, PT, &junk);
24600 /* Record that we did display the line number. */
24601 line_number_displayed = true;
24603 /* Make the string to show. */
24604 pint2str (decode_mode_spec_buf, width, topline + nlines);
24605 return decode_mode_spec_buf;
24606 no_value:
24608 char *p = decode_mode_spec_buf;
24609 int pad = width - 2;
24610 while (pad-- > 0)
24611 *p++ = ' ';
24612 *p++ = '?';
24613 *p++ = '?';
24614 *p = '\0';
24615 return decode_mode_spec_buf;
24618 break;
24620 case 'm':
24621 obj = BVAR (b, mode_name);
24622 break;
24624 case 'n':
24625 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24626 return " Narrow";
24627 break;
24629 /* Display the "degree of travel" of the window through the buffer. */
24630 case 'o':
24632 ptrdiff_t toppos = marker_position (w->start);
24633 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24634 ptrdiff_t begv = BUF_BEGV (b);
24635 ptrdiff_t zv = BUF_ZV (b);
24637 if (zv <= botpos)
24638 return toppos <= begv ? "All" : "Bottom";
24639 else if (toppos <= begv)
24640 return "Top";
24641 else
24643 sprintf (decode_mode_spec_buf, "%2d%%",
24644 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24645 return decode_mode_spec_buf;
24649 /* Display percentage of buffer above the top of the screen. */
24650 case 'p':
24652 ptrdiff_t pos = marker_position (w->start);
24653 ptrdiff_t begv = BUF_BEGV (b);
24654 ptrdiff_t zv = BUF_ZV (b);
24656 if (w->window_end_pos <= BUF_Z (b) - zv)
24657 return pos <= begv ? "All" : "Bottom";
24658 else if (pos <= begv)
24659 return "Top";
24660 else
24662 sprintf (decode_mode_spec_buf, "%2d%%",
24663 percent99 (pos - begv, zv - begv));
24664 return decode_mode_spec_buf;
24668 /* Display percentage of size above the bottom of the screen. */
24669 case 'P':
24671 ptrdiff_t toppos = marker_position (w->start);
24672 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24673 ptrdiff_t begv = BUF_BEGV (b);
24674 ptrdiff_t zv = BUF_ZV (b);
24676 if (zv <= botpos)
24677 return toppos <= begv ? "All" : "Bottom";
24678 else
24680 sprintf (decode_mode_spec_buf,
24681 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24682 percent99 (botpos - begv, zv - begv));
24683 return decode_mode_spec_buf;
24687 /* Display percentage offsets of top and bottom of the window,
24688 using "All" (but not "Top" or "Bottom") where appropriate. */
24689 case 'q':
24691 ptrdiff_t toppos = marker_position (w->start);
24692 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24693 ptrdiff_t begv = BUF_BEGV (b);
24694 ptrdiff_t zv = BUF_ZV (b);
24695 int top_perc, bot_perc;
24697 if ((toppos <= begv) && (zv <= botpos))
24698 return "All ";
24700 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24701 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24703 if (top_perc == bot_perc)
24704 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24705 else
24706 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24708 return decode_mode_spec_buf;
24711 case 's':
24712 /* status of process */
24713 obj = Fget_buffer_process (Fcurrent_buffer ());
24714 if (NILP (obj))
24715 return "no process";
24716 #ifndef MSDOS
24717 obj = Fsymbol_name (Fprocess_status (obj));
24718 #endif
24719 break;
24721 case '@':
24723 ptrdiff_t count = inhibit_garbage_collection ();
24724 Lisp_Object curdir = BVAR (current_buffer, directory);
24725 Lisp_Object val = Qnil;
24727 if (STRINGP (curdir))
24728 val = call1 (intern ("file-remote-p"), curdir);
24730 unbind_to (count, Qnil);
24732 if (NILP (val))
24733 return "-";
24734 else
24735 return "@";
24738 case 'z':
24739 /* coding-system (not including end-of-line format) */
24740 case 'Z':
24741 /* coding-system (including end-of-line type) */
24743 bool eol_flag = (c == 'Z');
24744 char *p = decode_mode_spec_buf;
24746 if (! FRAME_WINDOW_P (f))
24748 /* No need to mention EOL here--the terminal never needs
24749 to do EOL conversion. */
24750 p = decode_mode_spec_coding (CODING_ID_NAME
24751 (FRAME_KEYBOARD_CODING (f)->id),
24752 p, false);
24753 p = decode_mode_spec_coding (CODING_ID_NAME
24754 (FRAME_TERMINAL_CODING (f)->id),
24755 p, false);
24757 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24758 p, eol_flag);
24760 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24761 #ifdef subprocesses
24762 obj = Fget_buffer_process (Fcurrent_buffer ());
24763 if (PROCESSP (obj))
24765 p = decode_mode_spec_coding
24766 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24767 p = decode_mode_spec_coding
24768 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24770 #endif /* subprocesses */
24771 #endif /* false */
24772 *p = 0;
24773 return decode_mode_spec_buf;
24777 if (STRINGP (obj))
24779 *string = obj;
24780 return SSDATA (obj);
24782 else
24783 return "";
24787 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24788 means count lines back from START_BYTE. But don't go beyond
24789 LIMIT_BYTE. Return the number of lines thus found (always
24790 nonnegative).
24792 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24793 either the position COUNT lines after/before START_BYTE, if we
24794 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24795 COUNT lines. */
24797 static ptrdiff_t
24798 display_count_lines (ptrdiff_t start_byte,
24799 ptrdiff_t limit_byte, ptrdiff_t count,
24800 ptrdiff_t *byte_pos_ptr)
24802 register unsigned char *cursor;
24803 unsigned char *base;
24805 register ptrdiff_t ceiling;
24806 register unsigned char *ceiling_addr;
24807 ptrdiff_t orig_count = count;
24809 /* If we are not in selective display mode,
24810 check only for newlines. */
24811 bool selective_display
24812 = (!NILP (BVAR (current_buffer, selective_display))
24813 && !INTEGERP (BVAR (current_buffer, selective_display)));
24815 if (count > 0)
24817 while (start_byte < limit_byte)
24819 ceiling = BUFFER_CEILING_OF (start_byte);
24820 ceiling = min (limit_byte - 1, ceiling);
24821 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24822 base = (cursor = BYTE_POS_ADDR (start_byte));
24826 if (selective_display)
24828 while (*cursor != '\n' && *cursor != 015
24829 && ++cursor != ceiling_addr)
24830 continue;
24831 if (cursor == ceiling_addr)
24832 break;
24834 else
24836 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24837 if (! cursor)
24838 break;
24841 cursor++;
24843 if (--count == 0)
24845 start_byte += cursor - base;
24846 *byte_pos_ptr = start_byte;
24847 return orig_count;
24850 while (cursor < ceiling_addr);
24852 start_byte += ceiling_addr - base;
24855 else
24857 while (start_byte > limit_byte)
24859 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24860 ceiling = max (limit_byte, ceiling);
24861 ceiling_addr = BYTE_POS_ADDR (ceiling);
24862 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24863 while (true)
24865 if (selective_display)
24867 while (--cursor >= ceiling_addr
24868 && *cursor != '\n' && *cursor != 015)
24869 continue;
24870 if (cursor < ceiling_addr)
24871 break;
24873 else
24875 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24876 if (! cursor)
24877 break;
24880 if (++count == 0)
24882 start_byte += cursor - base + 1;
24883 *byte_pos_ptr = start_byte;
24884 /* When scanning backwards, we should
24885 not count the newline posterior to which we stop. */
24886 return - orig_count - 1;
24889 start_byte += ceiling_addr - base;
24893 *byte_pos_ptr = limit_byte;
24895 if (count < 0)
24896 return - orig_count + count;
24897 return orig_count - count;
24903 /***********************************************************************
24904 Displaying strings
24905 ***********************************************************************/
24907 /* Display a NUL-terminated string, starting with index START.
24909 If STRING is non-null, display that C string. Otherwise, the Lisp
24910 string LISP_STRING is displayed. There's a case that STRING is
24911 non-null and LISP_STRING is not nil. It means STRING is a string
24912 data of LISP_STRING. In that case, we display LISP_STRING while
24913 ignoring its text properties.
24915 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24916 FACE_STRING. Display STRING or LISP_STRING with the face at
24917 FACE_STRING_POS in FACE_STRING:
24919 Display the string in the environment given by IT, but use the
24920 standard display table, temporarily.
24922 FIELD_WIDTH is the minimum number of output glyphs to produce.
24923 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24924 with spaces. If STRING has more characters, more than FIELD_WIDTH
24925 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24927 PRECISION is the maximum number of characters to output from
24928 STRING. PRECISION < 0 means don't truncate the string.
24930 This is roughly equivalent to printf format specifiers:
24932 FIELD_WIDTH PRECISION PRINTF
24933 ----------------------------------------
24934 -1 -1 %s
24935 -1 10 %.10s
24936 10 -1 %10s
24937 20 10 %20.10s
24939 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24940 display them, and < 0 means obey the current buffer's value of
24941 enable_multibyte_characters.
24943 Value is the number of columns displayed. */
24945 static int
24946 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24947 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24948 int field_width, int precision, int max_x, int multibyte)
24950 int hpos_at_start = it->hpos;
24951 int saved_face_id = it->face_id;
24952 struct glyph_row *row = it->glyph_row;
24953 ptrdiff_t it_charpos;
24955 /* Initialize the iterator IT for iteration over STRING beginning
24956 with index START. */
24957 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24958 precision, field_width, multibyte);
24959 if (string && STRINGP (lisp_string))
24960 /* LISP_STRING is the one returned by decode_mode_spec. We should
24961 ignore its text properties. */
24962 it->stop_charpos = it->end_charpos;
24964 /* If displaying STRING, set up the face of the iterator from
24965 FACE_STRING, if that's given. */
24966 if (STRINGP (face_string))
24968 ptrdiff_t endptr;
24969 struct face *face;
24971 it->face_id
24972 = face_at_string_position (it->w, face_string, face_string_pos,
24973 0, &endptr, it->base_face_id, false);
24974 face = FACE_FROM_ID (it->f, it->face_id);
24975 it->face_box_p = face->box != FACE_NO_BOX;
24978 /* Set max_x to the maximum allowed X position. Don't let it go
24979 beyond the right edge of the window. */
24980 if (max_x <= 0)
24981 max_x = it->last_visible_x;
24982 else
24983 max_x = min (max_x, it->last_visible_x);
24985 /* Skip over display elements that are not visible. because IT->w is
24986 hscrolled. */
24987 if (it->current_x < it->first_visible_x)
24988 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24989 MOVE_TO_POS | MOVE_TO_X);
24991 row->ascent = it->max_ascent;
24992 row->height = it->max_ascent + it->max_descent;
24993 row->phys_ascent = it->max_phys_ascent;
24994 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24995 row->extra_line_spacing = it->max_extra_line_spacing;
24997 if (STRINGP (it->string))
24998 it_charpos = IT_STRING_CHARPOS (*it);
24999 else
25000 it_charpos = IT_CHARPOS (*it);
25002 /* This condition is for the case that we are called with current_x
25003 past last_visible_x. */
25004 while (it->current_x < max_x)
25006 int x_before, x, n_glyphs_before, i, nglyphs;
25008 /* Get the next display element. */
25009 if (!get_next_display_element (it))
25010 break;
25012 /* Produce glyphs. */
25013 x_before = it->current_x;
25014 n_glyphs_before = row->used[TEXT_AREA];
25015 PRODUCE_GLYPHS (it);
25017 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
25018 i = 0;
25019 x = x_before;
25020 while (i < nglyphs)
25022 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
25024 if (it->line_wrap != TRUNCATE
25025 && x + glyph->pixel_width > max_x)
25027 /* End of continued line or max_x reached. */
25028 if (CHAR_GLYPH_PADDING_P (*glyph))
25030 /* A wide character is unbreakable. */
25031 if (row->reversed_p)
25032 unproduce_glyphs (it, row->used[TEXT_AREA]
25033 - n_glyphs_before);
25034 row->used[TEXT_AREA] = n_glyphs_before;
25035 it->current_x = x_before;
25037 else
25039 if (row->reversed_p)
25040 unproduce_glyphs (it, row->used[TEXT_AREA]
25041 - (n_glyphs_before + i));
25042 row->used[TEXT_AREA] = n_glyphs_before + i;
25043 it->current_x = x;
25045 break;
25047 else if (x + glyph->pixel_width >= it->first_visible_x)
25049 /* Glyph is at least partially visible. */
25050 ++it->hpos;
25051 if (x < it->first_visible_x)
25052 row->x = x - it->first_visible_x;
25054 else
25056 /* Glyph is off the left margin of the display area.
25057 Should not happen. */
25058 emacs_abort ();
25061 row->ascent = max (row->ascent, it->max_ascent);
25062 row->height = max (row->height, it->max_ascent + it->max_descent);
25063 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
25064 row->phys_height = max (row->phys_height,
25065 it->max_phys_ascent + it->max_phys_descent);
25066 row->extra_line_spacing = max (row->extra_line_spacing,
25067 it->max_extra_line_spacing);
25068 x += glyph->pixel_width;
25069 ++i;
25072 /* Stop if max_x reached. */
25073 if (i < nglyphs)
25074 break;
25076 /* Stop at line ends. */
25077 if (ITERATOR_AT_END_OF_LINE_P (it))
25079 it->continuation_lines_width = 0;
25080 break;
25083 set_iterator_to_next (it, true);
25084 if (STRINGP (it->string))
25085 it_charpos = IT_STRING_CHARPOS (*it);
25086 else
25087 it_charpos = IT_CHARPOS (*it);
25089 /* Stop if truncating at the right edge. */
25090 if (it->line_wrap == TRUNCATE
25091 && it->current_x >= it->last_visible_x)
25093 /* Add truncation mark, but don't do it if the line is
25094 truncated at a padding space. */
25095 if (it_charpos < it->string_nchars)
25097 if (!FRAME_WINDOW_P (it->f))
25099 int ii, n;
25101 if (it->current_x > it->last_visible_x)
25103 if (!row->reversed_p)
25105 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25106 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25107 break;
25109 else
25111 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25112 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25113 break;
25114 unproduce_glyphs (it, ii + 1);
25115 ii = row->used[TEXT_AREA] - (ii + 1);
25117 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25119 row->used[TEXT_AREA] = ii;
25120 produce_special_glyphs (it, IT_TRUNCATION);
25123 produce_special_glyphs (it, IT_TRUNCATION);
25125 row->truncated_on_right_p = true;
25127 break;
25131 /* Maybe insert a truncation at the left. */
25132 if (it->first_visible_x
25133 && it_charpos > 0)
25135 if (!FRAME_WINDOW_P (it->f)
25136 || (row->reversed_p
25137 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25138 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25139 insert_left_trunc_glyphs (it);
25140 row->truncated_on_left_p = true;
25143 it->face_id = saved_face_id;
25145 /* Value is number of columns displayed. */
25146 return it->hpos - hpos_at_start;
25151 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25152 appears as an element of LIST or as the car of an element of LIST.
25153 If PROPVAL is a list, compare each element against LIST in that
25154 way, and return 1/2 if any element of PROPVAL is found in LIST.
25155 Otherwise return 0. This function cannot quit.
25156 The return value is 2 if the text is invisible but with an ellipsis
25157 and 1 if it's invisible and without an ellipsis. */
25160 invisible_prop (Lisp_Object propval, Lisp_Object list)
25162 Lisp_Object tail, proptail;
25164 for (tail = list; CONSP (tail); tail = XCDR (tail))
25166 register Lisp_Object tem;
25167 tem = XCAR (tail);
25168 if (EQ (propval, tem))
25169 return 1;
25170 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25171 return NILP (XCDR (tem)) ? 1 : 2;
25174 if (CONSP (propval))
25176 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25178 Lisp_Object propelt;
25179 propelt = XCAR (proptail);
25180 for (tail = list; CONSP (tail); tail = XCDR (tail))
25182 register Lisp_Object tem;
25183 tem = XCAR (tail);
25184 if (EQ (propelt, tem))
25185 return 1;
25186 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25187 return NILP (XCDR (tem)) ? 1 : 2;
25192 return 0;
25195 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25196 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25197 POS should be a marker or a buffer position; the value of the `invisible'
25198 property at that position in the current buffer is examined.
25199 POS can also be the actual value of the `invisible' text or overlay
25200 property of the text of interest, in which case the value itself is
25201 examined.
25203 The non-nil value returned can be t for currently invisible text that is
25204 entirely hidden on display, or some other non-nil, non-t value if the
25205 text is replaced by an ellipsis.
25207 Note that whether text with `invisible' property is actually hidden on
25208 display may depend on `buffer-invisibility-spec', which see. */)
25209 (Lisp_Object pos)
25211 Lisp_Object prop
25212 = (NATNUMP (pos) || MARKERP (pos)
25213 ? Fget_char_property (pos, Qinvisible, Qnil)
25214 : pos);
25215 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25216 return (invis == 0 ? Qnil
25217 : invis == 1 ? Qt
25218 : make_number (invis));
25221 /* Calculate a width or height in pixels from a specification using
25222 the following elements:
25224 SPEC ::=
25225 NUM - a (fractional) multiple of the default font width/height
25226 (NUM) - specifies exactly NUM pixels
25227 UNIT - a fixed number of pixels, see below.
25228 ELEMENT - size of a display element in pixels, see below.
25229 (NUM . SPEC) - equals NUM * SPEC
25230 (+ SPEC SPEC ...) - add pixel values
25231 (- SPEC SPEC ...) - subtract pixel values
25232 (- SPEC) - negate pixel value
25234 NUM ::=
25235 INT or FLOAT - a number constant
25236 SYMBOL - use symbol's (buffer local) variable binding.
25238 UNIT ::=
25239 in - pixels per inch *)
25240 mm - pixels per 1/1000 meter *)
25241 cm - pixels per 1/100 meter *)
25242 width - width of current font in pixels.
25243 height - height of current font in pixels.
25245 *) using the ratio(s) defined in display-pixels-per-inch.
25247 ELEMENT ::=
25249 left-fringe - left fringe width in pixels
25250 right-fringe - right fringe width in pixels
25252 left-margin - left margin width in pixels
25253 right-margin - right margin width in pixels
25255 scroll-bar - scroll-bar area width in pixels
25257 Examples:
25259 Pixels corresponding to 5 inches:
25260 (5 . in)
25262 Total width of non-text areas on left side of window (if scroll-bar is on left):
25263 '(space :width (+ left-fringe left-margin scroll-bar))
25265 Align to first text column (in header line):
25266 '(space :align-to 0)
25268 Align to middle of text area minus half the width of variable `my-image'
25269 containing a loaded image:
25270 '(space :align-to (0.5 . (- text my-image)))
25272 Width of left margin minus width of 1 character in the default font:
25273 '(space :width (- left-margin 1))
25275 Width of left margin minus width of 2 characters in the current font:
25276 '(space :width (- left-margin (2 . width)))
25278 Center 1 character over left-margin (in header line):
25279 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25281 Different ways to express width of left fringe plus left margin minus one pixel:
25282 '(space :width (- (+ left-fringe left-margin) (1)))
25283 '(space :width (+ left-fringe left-margin (- (1))))
25284 '(space :width (+ left-fringe left-margin (-1)))
25286 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25287 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25288 coordinate, and *RES is the additional pixel width from that point
25289 till the end of the stretch glyph.
25291 WIDTH_P non-zero means take the width dimension or X coordinate of
25292 the object specified by PROP, WIDTH_P zero means take the height
25293 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25294 non-NULL, WIDTH_P should be non-zero.)
25296 FONT is the font of the face of the surrounding text.
25298 The return value is non-zero if width or height were successfully
25299 calculated, i.e. if PROP is a valid spec. */
25301 static bool
25302 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25303 struct font *font, bool width_p, int *align_to)
25305 double pixels;
25307 # define OK_PIXELS(val) (*res = (val), true)
25308 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25310 if (NILP (prop))
25311 return OK_PIXELS (0);
25313 eassert (FRAME_LIVE_P (it->f));
25315 if (SYMBOLP (prop))
25317 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25319 char *unit = SSDATA (SYMBOL_NAME (prop));
25321 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25322 if (unit[0] == 'i' && unit[1] == 'n')
25323 pixels = 1.0;
25324 else if (unit[0] == 'm' && unit[1] == 'm')
25325 pixels = 25.4;
25326 else if (unit[0] == 'c' && unit[1] == 'm')
25327 pixels = 2.54;
25328 else
25329 pixels = 0;
25330 if (pixels > 0)
25332 double ppi = (width_p ? FRAME_RES_X (it->f)
25333 : FRAME_RES_Y (it->f));
25335 if (ppi > 0)
25336 return OK_PIXELS (ppi / pixels);
25337 return false;
25341 #ifdef HAVE_WINDOW_SYSTEM
25342 /* 'height': the height of FONT. */
25343 if (EQ (prop, Qheight))
25344 return OK_PIXELS (font
25345 ? normal_char_height (font, -1)
25346 : FRAME_LINE_HEIGHT (it->f));
25347 /* 'width': the width of FONT. */
25348 if (EQ (prop, Qwidth))
25349 return OK_PIXELS (font
25350 ? FONT_WIDTH (font)
25351 : FRAME_COLUMN_WIDTH (it->f));
25352 #else
25353 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25354 return OK_PIXELS (1);
25355 #endif
25357 /* 'text': the width or height of the text area. */
25358 if (EQ (prop, Qtext))
25359 return OK_PIXELS (width_p
25360 ? (window_box_width (it->w, TEXT_AREA)
25361 - it->lnum_pixel_width)
25362 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25364 /* ':align_to'. First time we compute the value, window
25365 elements are interpreted as the position of the element's
25366 left edge. */
25367 if (align_to && *align_to < 0)
25369 *res = 0;
25370 /* 'left': left edge of the text area. */
25371 if (EQ (prop, Qleft))
25372 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25373 + it->lnum_pixel_width);
25374 /* 'right': right edge of the text area. */
25375 if (EQ (prop, Qright))
25376 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25377 /* 'center': the center of the text area. */
25378 if (EQ (prop, Qcenter))
25379 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25380 + it->lnum_pixel_width
25381 + window_box_width (it->w, TEXT_AREA) / 2);
25382 /* 'left-fringe': left edge of the left fringe. */
25383 if (EQ (prop, Qleft_fringe))
25384 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25385 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25386 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25387 /* 'right-fringe': left edge of the right fringe. */
25388 if (EQ (prop, Qright_fringe))
25389 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25390 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25391 : window_box_right_offset (it->w, TEXT_AREA));
25392 /* 'left-margin': left edge of the left display margin. */
25393 if (EQ (prop, Qleft_margin))
25394 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25395 /* 'right-margin': left edge of the right display margin. */
25396 if (EQ (prop, Qright_margin))
25397 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25398 /* 'scroll-bar': left edge of the vertical scroll bar. */
25399 if (EQ (prop, Qscroll_bar))
25400 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25402 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25403 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25404 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25405 : 0)));
25407 else
25409 /* Otherwise, the elements stand for their width. */
25410 if (EQ (prop, Qleft_fringe))
25411 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25412 if (EQ (prop, Qright_fringe))
25413 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25414 if (EQ (prop, Qleft_margin))
25415 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25416 if (EQ (prop, Qright_margin))
25417 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25418 if (EQ (prop, Qscroll_bar))
25419 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25422 prop = buffer_local_value (prop, it->w->contents);
25423 if (EQ (prop, Qunbound))
25424 prop = Qnil;
25427 if (NUMBERP (prop))
25429 int base_unit = (width_p
25430 ? FRAME_COLUMN_WIDTH (it->f)
25431 : FRAME_LINE_HEIGHT (it->f));
25432 if (width_p && align_to && *align_to < 0)
25433 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25434 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25437 if (CONSP (prop))
25439 Lisp_Object car = XCAR (prop);
25440 Lisp_Object cdr = XCDR (prop);
25442 if (SYMBOLP (car))
25444 #ifdef HAVE_WINDOW_SYSTEM
25445 /* '(image PROPS...)': width or height of the specified image. */
25446 if (FRAME_WINDOW_P (it->f)
25447 && valid_image_p (prop))
25449 ptrdiff_t id = lookup_image (it->f, prop);
25450 struct image *img = IMAGE_FROM_ID (it->f, id);
25452 return OK_PIXELS (width_p ? img->width : img->height);
25454 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25455 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25457 /* TODO: Don't return dummy size. */
25458 return OK_PIXELS (100);
25460 #endif
25461 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25462 recursively calculated values. */
25463 if (EQ (car, Qplus) || EQ (car, Qminus))
25465 bool first = true;
25466 double px;
25468 pixels = 0;
25469 while (CONSP (cdr))
25471 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25472 font, width_p, align_to))
25473 return false;
25474 if (first)
25475 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25476 else
25477 pixels += px;
25478 cdr = XCDR (cdr);
25480 if (EQ (car, Qminus))
25481 pixels = -pixels;
25482 return OK_PIXELS (pixels);
25485 car = buffer_local_value (car, it->w->contents);
25486 if (EQ (car, Qunbound))
25487 car = Qnil;
25490 /* '(NUM)': absolute number of pixels. */
25491 if (NUMBERP (car))
25493 double fact;
25494 int offset =
25495 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25496 pixels = XFLOATINT (car);
25497 if (NILP (cdr))
25498 return OK_PIXELS (pixels + offset);
25499 if (calc_pixel_width_or_height (&fact, it, cdr,
25500 font, width_p, align_to))
25501 return OK_PIXELS (pixels * fact + offset);
25502 return false;
25505 return false;
25508 return false;
25511 void
25512 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25514 #ifdef HAVE_WINDOW_SYSTEM
25515 normal_char_ascent_descent (font, -1, ascent, descent);
25516 #else
25517 *ascent = 1;
25518 *descent = 0;
25519 #endif
25523 /***********************************************************************
25524 Glyph Display
25525 ***********************************************************************/
25527 #ifdef HAVE_WINDOW_SYSTEM
25529 #ifdef GLYPH_DEBUG
25531 void
25532 dump_glyph_string (struct glyph_string *s)
25534 fprintf (stderr, "glyph string\n");
25535 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25536 s->x, s->y, s->width, s->height);
25537 fprintf (stderr, " ybase = %d\n", s->ybase);
25538 fprintf (stderr, " hl = %u\n", s->hl);
25539 fprintf (stderr, " left overhang = %d, right = %d\n",
25540 s->left_overhang, s->right_overhang);
25541 fprintf (stderr, " nchars = %d\n", s->nchars);
25542 fprintf (stderr, " extends to end of line = %d\n",
25543 s->extends_to_end_of_line_p);
25544 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25545 fprintf (stderr, " bg width = %d\n", s->background_width);
25548 #endif /* GLYPH_DEBUG */
25550 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25551 of XChar2b structures for S; it can't be allocated in
25552 init_glyph_string because it must be allocated via `alloca'. W
25553 is the window on which S is drawn. ROW and AREA are the glyph row
25554 and area within the row from which S is constructed. START is the
25555 index of the first glyph structure covered by S. HL is a
25556 face-override for drawing S. */
25558 #ifdef HAVE_NTGUI
25559 #define OPTIONAL_HDC(hdc) HDC hdc,
25560 #define DECLARE_HDC(hdc) HDC hdc;
25561 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25562 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25563 #endif
25565 #ifndef OPTIONAL_HDC
25566 #define OPTIONAL_HDC(hdc)
25567 #define DECLARE_HDC(hdc)
25568 #define ALLOCATE_HDC(hdc, f)
25569 #define RELEASE_HDC(hdc, f)
25570 #endif
25572 static void
25573 init_glyph_string (struct glyph_string *s,
25574 OPTIONAL_HDC (hdc)
25575 XChar2b *char2b, struct window *w, struct glyph_row *row,
25576 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25578 memset (s, 0, sizeof *s);
25579 s->w = w;
25580 s->f = XFRAME (w->frame);
25581 #ifdef HAVE_NTGUI
25582 s->hdc = hdc;
25583 #endif
25584 s->display = FRAME_X_DISPLAY (s->f);
25585 s->char2b = char2b;
25586 s->hl = hl;
25587 s->row = row;
25588 s->area = area;
25589 s->first_glyph = row->glyphs[area] + start;
25590 s->height = row->height;
25591 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25592 s->ybase = s->y + row->ascent;
25596 /* Append the list of glyph strings with head H and tail T to the list
25597 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25599 static void
25600 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25601 struct glyph_string *h, struct glyph_string *t)
25603 if (h)
25605 if (*head)
25606 (*tail)->next = h;
25607 else
25608 *head = h;
25609 h->prev = *tail;
25610 *tail = t;
25615 /* Prepend the list of glyph strings with head H and tail T to the
25616 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25617 result. */
25619 static void
25620 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25621 struct glyph_string *h, struct glyph_string *t)
25623 if (h)
25625 if (*head)
25626 (*head)->prev = t;
25627 else
25628 *tail = t;
25629 t->next = *head;
25630 *head = h;
25635 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25636 Set *HEAD and *TAIL to the resulting list. */
25638 static void
25639 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25640 struct glyph_string *s)
25642 s->next = s->prev = NULL;
25643 append_glyph_string_lists (head, tail, s, s);
25647 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25648 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25649 make sure that X resources for the face returned are allocated.
25650 Value is a pointer to a realized face that is ready for display if
25651 DISPLAY_P. */
25653 static struct face *
25654 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25655 XChar2b *char2b, bool display_p)
25657 struct face *face = FACE_FROM_ID (f, face_id);
25658 unsigned code = 0;
25660 if (face->font)
25662 code = face->font->driver->encode_char (face->font, c);
25664 if (code == FONT_INVALID_CODE)
25665 code = 0;
25667 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25669 /* Make sure X resources of the face are allocated. */
25670 #ifdef HAVE_X_WINDOWS
25671 if (display_p)
25672 #endif
25674 eassert (face != NULL);
25675 prepare_face_for_display (f, face);
25678 return face;
25682 /* Get face and two-byte form of character glyph GLYPH on frame F.
25683 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25684 a pointer to a realized face that is ready for display. */
25686 static struct face *
25687 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25688 XChar2b *char2b)
25690 struct face *face;
25691 unsigned code = 0;
25693 eassert (glyph->type == CHAR_GLYPH);
25694 face = FACE_FROM_ID (f, glyph->face_id);
25696 /* Make sure X resources of the face are allocated. */
25697 prepare_face_for_display (f, face);
25699 if (face->font)
25701 if (CHAR_BYTE8_P (glyph->u.ch))
25702 code = CHAR_TO_BYTE8 (glyph->u.ch);
25703 else
25704 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25706 if (code == FONT_INVALID_CODE)
25707 code = 0;
25710 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25711 return face;
25715 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25716 Return true iff FONT has a glyph for C. */
25718 static bool
25719 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25721 unsigned code;
25723 if (CHAR_BYTE8_P (c))
25724 code = CHAR_TO_BYTE8 (c);
25725 else
25726 code = font->driver->encode_char (font, c);
25728 if (code == FONT_INVALID_CODE)
25729 return false;
25730 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25731 return true;
25735 /* Fill glyph string S with composition components specified by S->cmp.
25737 BASE_FACE is the base face of the composition.
25738 S->cmp_from is the index of the first component for S.
25740 OVERLAPS non-zero means S should draw the foreground only, and use
25741 its physical height for clipping. See also draw_glyphs.
25743 Value is the index of a component not in S. */
25745 static int
25746 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25747 int overlaps)
25749 int i;
25750 /* For all glyphs of this composition, starting at the offset
25751 S->cmp_from, until we reach the end of the definition or encounter a
25752 glyph that requires the different face, add it to S. */
25753 struct face *face;
25755 eassert (s);
25757 s->for_overlaps = overlaps;
25758 s->face = NULL;
25759 s->font = NULL;
25760 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25762 int c = COMPOSITION_GLYPH (s->cmp, i);
25764 /* TAB in a composition means display glyphs with padding space
25765 on the left or right. */
25766 if (c != '\t')
25768 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25769 -1, Qnil);
25771 face = get_char_face_and_encoding (s->f, c, face_id,
25772 s->char2b + i, true);
25773 if (face)
25775 if (! s->face)
25777 s->face = face;
25778 s->font = s->face->font;
25780 else if (s->face != face)
25781 break;
25784 ++s->nchars;
25786 s->cmp_to = i;
25788 if (s->face == NULL)
25790 s->face = base_face->ascii_face;
25791 s->font = s->face->font;
25794 /* All glyph strings for the same composition has the same width,
25795 i.e. the width set for the first component of the composition. */
25796 s->width = s->first_glyph->pixel_width;
25798 /* If the specified font could not be loaded, use the frame's
25799 default font, but record the fact that we couldn't load it in
25800 the glyph string so that we can draw rectangles for the
25801 characters of the glyph string. */
25802 if (s->font == NULL)
25804 s->font_not_found_p = true;
25805 s->font = FRAME_FONT (s->f);
25808 /* Adjust base line for subscript/superscript text. */
25809 s->ybase += s->first_glyph->voffset;
25811 return s->cmp_to;
25814 static int
25815 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25816 int start, int end, int overlaps)
25818 struct glyph *glyph, *last;
25819 Lisp_Object lgstring;
25820 int i;
25822 s->for_overlaps = overlaps;
25823 glyph = s->row->glyphs[s->area] + start;
25824 last = s->row->glyphs[s->area] + end;
25825 s->cmp_id = glyph->u.cmp.id;
25826 s->cmp_from = glyph->slice.cmp.from;
25827 s->cmp_to = glyph->slice.cmp.to + 1;
25828 s->face = FACE_FROM_ID (s->f, face_id);
25829 lgstring = composition_gstring_from_id (s->cmp_id);
25830 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25831 glyph++;
25832 while (glyph < last
25833 && glyph->u.cmp.automatic
25834 && glyph->u.cmp.id == s->cmp_id
25835 && s->cmp_to == glyph->slice.cmp.from)
25836 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25838 for (i = s->cmp_from; i < s->cmp_to; i++)
25840 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25841 unsigned code = LGLYPH_CODE (lglyph);
25843 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25845 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25846 return glyph - s->row->glyphs[s->area];
25850 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25851 See the comment of fill_glyph_string for arguments.
25852 Value is the index of the first glyph not in S. */
25855 static int
25856 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25857 int start, int end, int overlaps)
25859 struct glyph *glyph, *last;
25860 int voffset;
25862 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25863 s->for_overlaps = overlaps;
25864 glyph = s->row->glyphs[s->area] + start;
25865 last = s->row->glyphs[s->area] + end;
25866 voffset = glyph->voffset;
25867 s->face = FACE_FROM_ID (s->f, face_id);
25868 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25869 s->nchars = 1;
25870 s->width = glyph->pixel_width;
25871 glyph++;
25872 while (glyph < last
25873 && glyph->type == GLYPHLESS_GLYPH
25874 && glyph->voffset == voffset
25875 && glyph->face_id == face_id)
25877 s->nchars++;
25878 s->width += glyph->pixel_width;
25879 glyph++;
25881 s->ybase += voffset;
25882 return glyph - s->row->glyphs[s->area];
25886 /* Fill glyph string S from a sequence of character glyphs.
25888 FACE_ID is the face id of the string. START is the index of the
25889 first glyph to consider, END is the index of the last + 1.
25890 OVERLAPS non-zero means S should draw the foreground only, and use
25891 its physical height for clipping. See also draw_glyphs.
25893 Value is the index of the first glyph not in S. */
25895 static int
25896 fill_glyph_string (struct glyph_string *s, int face_id,
25897 int start, int end, int overlaps)
25899 struct glyph *glyph, *last;
25900 int voffset;
25901 bool glyph_not_available_p;
25903 eassert (s->f == XFRAME (s->w->frame));
25904 eassert (s->nchars == 0);
25905 eassert (start >= 0 && end > start);
25907 s->for_overlaps = overlaps;
25908 glyph = s->row->glyphs[s->area] + start;
25909 last = s->row->glyphs[s->area] + end;
25910 voffset = glyph->voffset;
25911 s->padding_p = glyph->padding_p;
25912 glyph_not_available_p = glyph->glyph_not_available_p;
25914 while (glyph < last
25915 && glyph->type == CHAR_GLYPH
25916 && glyph->voffset == voffset
25917 /* Same face id implies same font, nowadays. */
25918 && glyph->face_id == face_id
25919 && glyph->glyph_not_available_p == glyph_not_available_p)
25921 s->face = get_glyph_face_and_encoding (s->f, glyph,
25922 s->char2b + s->nchars);
25923 ++s->nchars;
25924 eassert (s->nchars <= end - start);
25925 s->width += glyph->pixel_width;
25926 if (glyph++->padding_p != s->padding_p)
25927 break;
25930 s->font = s->face->font;
25932 /* If the specified font could not be loaded, use the frame's font,
25933 but record the fact that we couldn't load it in
25934 S->font_not_found_p so that we can draw rectangles for the
25935 characters of the glyph string. */
25936 if (s->font == NULL || glyph_not_available_p)
25938 s->font_not_found_p = true;
25939 s->font = FRAME_FONT (s->f);
25942 /* Adjust base line for subscript/superscript text. */
25943 s->ybase += voffset;
25945 eassert (s->face && s->face->gc);
25946 return glyph - s->row->glyphs[s->area];
25950 /* Fill glyph string S from image glyph S->first_glyph. */
25952 static void
25953 fill_image_glyph_string (struct glyph_string *s)
25955 eassert (s->first_glyph->type == IMAGE_GLYPH);
25956 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25957 eassert (s->img);
25958 s->slice = s->first_glyph->slice.img;
25959 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25960 s->font = s->face->font;
25961 s->width = s->first_glyph->pixel_width;
25963 /* Adjust base line for subscript/superscript text. */
25964 s->ybase += s->first_glyph->voffset;
25968 #ifdef HAVE_XWIDGETS
25969 static void
25970 fill_xwidget_glyph_string (struct glyph_string *s)
25972 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25973 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25974 s->font = s->face->font;
25975 s->width = s->first_glyph->pixel_width;
25976 s->ybase += s->first_glyph->voffset;
25977 s->xwidget = s->first_glyph->u.xwidget;
25979 #endif
25980 /* Fill glyph string S from a sequence of stretch glyphs.
25982 START is the index of the first glyph to consider,
25983 END is the index of the last + 1.
25985 Value is the index of the first glyph not in S. */
25987 static int
25988 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25990 struct glyph *glyph, *last;
25991 int voffset, face_id;
25993 eassert (s->first_glyph->type == STRETCH_GLYPH);
25995 glyph = s->row->glyphs[s->area] + start;
25996 last = s->row->glyphs[s->area] + end;
25997 face_id = glyph->face_id;
25998 s->face = FACE_FROM_ID (s->f, face_id);
25999 s->font = s->face->font;
26000 s->width = glyph->pixel_width;
26001 s->nchars = 1;
26002 voffset = glyph->voffset;
26004 for (++glyph;
26005 (glyph < last
26006 && glyph->type == STRETCH_GLYPH
26007 && glyph->voffset == voffset
26008 && glyph->face_id == face_id);
26009 ++glyph)
26010 s->width += glyph->pixel_width;
26012 /* Adjust base line for subscript/superscript text. */
26013 s->ybase += voffset;
26015 /* The case that face->gc == 0 is handled when drawing the glyph
26016 string by calling prepare_face_for_display. */
26017 eassert (s->face);
26018 return glyph - s->row->glyphs[s->area];
26021 static struct font_metrics *
26022 get_per_char_metric (struct font *font, XChar2b *char2b)
26024 static struct font_metrics metrics;
26025 unsigned code;
26027 if (! font)
26028 return NULL;
26029 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
26030 if (code == FONT_INVALID_CODE)
26031 return NULL;
26032 font->driver->text_extents (font, &code, 1, &metrics);
26033 return &metrics;
26036 /* A subroutine that computes "normal" values of ASCENT and DESCENT
26037 for FONT. Values are taken from font-global ones, except for fonts
26038 that claim preposterously large values, but whose glyphs actually
26039 have reasonable dimensions. C is the character to use for metrics
26040 if the font-global values are too large; if C is negative, the
26041 function selects a default character. */
26042 static void
26043 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
26045 *ascent = FONT_BASE (font);
26046 *descent = FONT_DESCENT (font);
26048 if (FONT_TOO_HIGH (font))
26050 XChar2b char2b;
26052 /* Get metrics of C, defaulting to a reasonably sized ASCII
26053 character. */
26054 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
26056 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
26058 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
26060 /* We add 1 pixel to character dimensions as heuristics
26061 that produces nicer display, e.g. when the face has
26062 the box attribute. */
26063 *ascent = pcm->ascent + 1;
26064 *descent = pcm->descent + 1;
26070 /* A subroutine that computes a reasonable "normal character height"
26071 for fonts that claim preposterously large vertical dimensions, but
26072 whose glyphs are actually reasonably sized. C is the character
26073 whose metrics to use for those fonts, or -1 for default
26074 character. */
26075 static int
26076 normal_char_height (struct font *font, int c)
26078 int ascent, descent;
26080 normal_char_ascent_descent (font, c, &ascent, &descent);
26082 return ascent + descent;
26085 /* EXPORT for RIF:
26086 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
26087 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
26088 assumed to be zero. */
26090 void
26091 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
26093 *left = *right = 0;
26095 if (glyph->type == CHAR_GLYPH)
26097 XChar2b char2b;
26098 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26099 if (face->font)
26101 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26102 if (pcm)
26104 if (pcm->rbearing > pcm->width)
26105 *right = pcm->rbearing - pcm->width;
26106 if (pcm->lbearing < 0)
26107 *left = -pcm->lbearing;
26111 else if (glyph->type == COMPOSITE_GLYPH)
26113 if (! glyph->u.cmp.automatic)
26115 struct composition *cmp = composition_table[glyph->u.cmp.id];
26117 if (cmp->rbearing > cmp->pixel_width)
26118 *right = cmp->rbearing - cmp->pixel_width;
26119 if (cmp->lbearing < 0)
26120 *left = - cmp->lbearing;
26122 else
26124 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26125 struct font_metrics metrics;
26127 composition_gstring_width (gstring, glyph->slice.cmp.from,
26128 glyph->slice.cmp.to + 1, &metrics);
26129 if (metrics.rbearing > metrics.width)
26130 *right = metrics.rbearing - metrics.width;
26131 if (metrics.lbearing < 0)
26132 *left = - metrics.lbearing;
26138 /* Return the index of the first glyph preceding glyph string S that
26139 is overwritten by S because of S's left overhang. Value is -1
26140 if no glyphs are overwritten. */
26142 static int
26143 left_overwritten (struct glyph_string *s)
26145 int k;
26147 if (s->left_overhang)
26149 int x = 0, i;
26150 struct glyph *glyphs = s->row->glyphs[s->area];
26151 int first = s->first_glyph - glyphs;
26153 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26154 x -= glyphs[i].pixel_width;
26156 k = i + 1;
26158 else
26159 k = -1;
26161 return k;
26165 /* Return the index of the first glyph preceding glyph string S that
26166 is overwriting S because of its right overhang. Value is -1 if no
26167 glyph in front of S overwrites S. */
26169 static int
26170 left_overwriting (struct glyph_string *s)
26172 int i, k, x;
26173 struct glyph *glyphs = s->row->glyphs[s->area];
26174 int first = s->first_glyph - glyphs;
26176 k = -1;
26177 x = 0;
26178 for (i = first - 1; i >= 0; --i)
26180 int left, right;
26181 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26182 if (x + right > 0)
26183 k = i;
26184 x -= glyphs[i].pixel_width;
26187 return k;
26191 /* Return the index of the last glyph following glyph string S that is
26192 overwritten by S because of S's right overhang. Value is -1 if
26193 no such glyph is found. */
26195 static int
26196 right_overwritten (struct glyph_string *s)
26198 int k = -1;
26200 if (s->right_overhang)
26202 int x = 0, i;
26203 struct glyph *glyphs = s->row->glyphs[s->area];
26204 int first = (s->first_glyph - glyphs
26205 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26206 int end = s->row->used[s->area];
26208 for (i = first; i < end && s->right_overhang > x; ++i)
26209 x += glyphs[i].pixel_width;
26211 k = i;
26214 return k;
26218 /* Return the index of the last glyph following glyph string S that
26219 overwrites S because of its left overhang. Value is negative
26220 if no such glyph is found. */
26222 static int
26223 right_overwriting (struct glyph_string *s)
26225 int i, k, x;
26226 int end = s->row->used[s->area];
26227 struct glyph *glyphs = s->row->glyphs[s->area];
26228 int first = (s->first_glyph - glyphs
26229 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26231 k = -1;
26232 x = 0;
26233 for (i = first; i < end; ++i)
26235 int left, right;
26236 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26237 if (x - left < 0)
26238 k = i;
26239 x += glyphs[i].pixel_width;
26242 return k;
26246 /* Set background width of glyph string S. START is the index of the
26247 first glyph following S. LAST_X is the right-most x-position + 1
26248 in the drawing area. */
26250 static void
26251 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26253 /* If the face of this glyph string has to be drawn to the end of
26254 the drawing area, set S->extends_to_end_of_line_p. */
26256 if (start == s->row->used[s->area]
26257 && ((s->row->fill_line_p
26258 && (s->hl == DRAW_NORMAL_TEXT
26259 || s->hl == DRAW_IMAGE_RAISED
26260 || s->hl == DRAW_IMAGE_SUNKEN))
26261 || s->hl == DRAW_MOUSE_FACE))
26262 s->extends_to_end_of_line_p = true;
26264 /* If S extends its face to the end of the line, set its
26265 background_width to the distance to the right edge of the drawing
26266 area. */
26267 if (s->extends_to_end_of_line_p)
26268 s->background_width = last_x - s->x + 1;
26269 else
26270 s->background_width = s->width;
26274 /* Return glyph string that shares background with glyph string S and
26275 whose `background_width' member has been set. */
26277 static struct glyph_string *
26278 glyph_string_containing_background_width (struct glyph_string *s)
26280 if (s->cmp)
26281 while (s->cmp_from)
26282 s = s->prev;
26284 return s;
26288 /* Compute overhangs and x-positions for glyph string S and its
26289 predecessors, or successors. X is the starting x-position for S.
26290 BACKWARD_P means process predecessors. */
26292 static void
26293 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26295 if (backward_p)
26297 while (s)
26299 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26300 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26301 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26302 x -= s->width;
26303 s->x = x;
26304 s = s->prev;
26307 else
26309 while (s)
26311 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26312 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26313 s->x = x;
26314 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26315 x += s->width;
26316 s = s->next;
26323 /* The following macros are only called from draw_glyphs below.
26324 They reference the following parameters of that function directly:
26325 `w', `row', `area', and `overlap_p'
26326 as well as the following local variables:
26327 `s', `f', and `hdc' (in W32) */
26329 #ifdef HAVE_NTGUI
26330 /* On W32, silently add local `hdc' variable to argument list of
26331 init_glyph_string. */
26332 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26333 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26334 #else
26335 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26336 init_glyph_string (s, char2b, w, row, area, start, hl)
26337 #endif
26339 /* Add a glyph string for a stretch glyph to the list of strings
26340 between HEAD and TAIL. START is the index of the stretch glyph in
26341 row area AREA of glyph row ROW. END is the index of the last glyph
26342 in that glyph row area. X is the current output position assigned
26343 to the new glyph string constructed. HL overrides that face of the
26344 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26345 is the right-most x-position of the drawing area. */
26347 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26348 and below -- keep them on one line. */
26349 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26350 do \
26352 s = alloca (sizeof *s); \
26353 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26354 START = fill_stretch_glyph_string (s, START, END); \
26355 append_glyph_string (&HEAD, &TAIL, s); \
26356 s->x = (X); \
26358 while (false)
26361 /* Add a glyph string for an image glyph to the list of strings
26362 between HEAD and TAIL. START is the index of the image glyph in
26363 row area AREA of glyph row ROW. END is the index of the last glyph
26364 in that glyph row area. X is the current output position assigned
26365 to the new glyph string constructed. HL overrides that face of the
26366 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26367 is the right-most x-position of the drawing area. */
26369 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26370 do \
26372 s = alloca (sizeof *s); \
26373 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26374 fill_image_glyph_string (s); \
26375 append_glyph_string (&HEAD, &TAIL, s); \
26376 ++START; \
26377 s->x = (X); \
26379 while (false)
26381 #ifndef HAVE_XWIDGETS
26382 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26383 eassume (false)
26384 #else
26385 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26386 do \
26388 s = alloca (sizeof *s); \
26389 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26390 fill_xwidget_glyph_string (s); \
26391 append_glyph_string (&(HEAD), &(TAIL), s); \
26392 ++(START); \
26393 s->x = (X); \
26395 while (false)
26396 #endif
26398 /* Add a glyph string for a sequence of character glyphs to the list
26399 of strings between HEAD and TAIL. START is the index of the first
26400 glyph in row area AREA of glyph row ROW that is part of the new
26401 glyph string. END is the index of the last glyph in that glyph row
26402 area. X is the current output position assigned to the new glyph
26403 string constructed. HL overrides that face of the glyph; e.g. it
26404 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26405 right-most x-position of the drawing area. */
26407 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26408 do \
26410 int face_id; \
26411 XChar2b *char2b; \
26413 face_id = (row)->glyphs[area][START].face_id; \
26415 s = alloca (sizeof *s); \
26416 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26417 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26418 append_glyph_string (&HEAD, &TAIL, s); \
26419 s->x = (X); \
26420 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26422 while (false)
26425 /* Add a glyph string for a composite sequence to the list of strings
26426 between HEAD and TAIL. START is the index of the first glyph in
26427 row area AREA of glyph row ROW that is part of the new glyph
26428 string. END is the index of the last glyph in that glyph row area.
26429 X is the current output position assigned to the new glyph string
26430 constructed. HL overrides that face of the glyph; e.g. it is
26431 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26432 x-position of the drawing area. */
26434 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26435 do { \
26436 int face_id = (row)->glyphs[area][START].face_id; \
26437 struct face *base_face = FACE_FROM_ID (f, face_id); \
26438 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26439 struct composition *cmp = composition_table[cmp_id]; \
26440 XChar2b *char2b; \
26441 struct glyph_string *first_s = NULL; \
26442 int n; \
26444 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26446 /* Make glyph_strings for each glyph sequence that is drawable by \
26447 the same face, and append them to HEAD/TAIL. */ \
26448 for (n = 0; n < cmp->glyph_len;) \
26450 s = alloca (sizeof *s); \
26451 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26452 append_glyph_string (&(HEAD), &(TAIL), s); \
26453 s->cmp = cmp; \
26454 s->cmp_from = n; \
26455 s->x = (X); \
26456 if (n == 0) \
26457 first_s = s; \
26458 n = fill_composite_glyph_string (s, base_face, overlaps); \
26461 ++START; \
26462 s = first_s; \
26463 } while (false)
26466 /* Add a glyph string for a glyph-string sequence to the list of strings
26467 between HEAD and TAIL. */
26469 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26470 do { \
26471 int face_id; \
26472 XChar2b *char2b; \
26473 Lisp_Object gstring; \
26475 face_id = (row)->glyphs[area][START].face_id; \
26476 gstring = (composition_gstring_from_id \
26477 ((row)->glyphs[area][START].u.cmp.id)); \
26478 s = alloca (sizeof *s); \
26479 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26480 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26481 append_glyph_string (&(HEAD), &(TAIL), s); \
26482 s->x = (X); \
26483 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26484 } while (false)
26487 /* Add a glyph string for a sequence of glyphless character's glyphs
26488 to the list of strings between HEAD and TAIL. The meanings of
26489 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26491 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26492 do \
26494 int face_id; \
26496 face_id = (row)->glyphs[area][START].face_id; \
26498 s = alloca (sizeof *s); \
26499 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26500 append_glyph_string (&HEAD, &TAIL, s); \
26501 s->x = (X); \
26502 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26503 overlaps); \
26505 while (false)
26508 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26509 of AREA of glyph row ROW on window W between indices START and END.
26510 HL overrides the face for drawing glyph strings, e.g. it is
26511 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26512 x-positions of the drawing area.
26514 This is an ugly monster macro construct because we must use alloca
26515 to allocate glyph strings (because draw_glyphs can be called
26516 asynchronously). */
26518 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26519 do \
26521 HEAD = TAIL = NULL; \
26522 while (START < END) \
26524 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26525 switch (first_glyph->type) \
26527 case CHAR_GLYPH: \
26528 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26529 HL, X, LAST_X); \
26530 break; \
26532 case COMPOSITE_GLYPH: \
26533 if (first_glyph->u.cmp.automatic) \
26534 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26535 HL, X, LAST_X); \
26536 else \
26537 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26538 HL, X, LAST_X); \
26539 break; \
26541 case STRETCH_GLYPH: \
26542 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26543 HL, X, LAST_X); \
26544 break; \
26546 case IMAGE_GLYPH: \
26547 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26548 HL, X, LAST_X); \
26549 break;
26551 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26552 case XWIDGET_GLYPH: \
26553 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26554 HL, X, LAST_X); \
26555 break;
26557 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26558 case GLYPHLESS_GLYPH: \
26559 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26560 HL, X, LAST_X); \
26561 break; \
26563 default: \
26564 emacs_abort (); \
26567 if (s) \
26569 set_glyph_string_background_width (s, START, LAST_X); \
26570 (X) += s->width; \
26573 } while (false)
26576 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26577 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26578 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26579 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26582 /* Draw glyphs between START and END in AREA of ROW on window W,
26583 starting at x-position X. X is relative to AREA in W. HL is a
26584 face-override with the following meaning:
26586 DRAW_NORMAL_TEXT draw normally
26587 DRAW_CURSOR draw in cursor face
26588 DRAW_MOUSE_FACE draw in mouse face.
26589 DRAW_INVERSE_VIDEO draw in mode line face
26590 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26591 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26593 If OVERLAPS is non-zero, draw only the foreground of characters and
26594 clip to the physical height of ROW. Non-zero value also defines
26595 the overlapping part to be drawn:
26597 OVERLAPS_PRED overlap with preceding rows
26598 OVERLAPS_SUCC overlap with succeeding rows
26599 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26600 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26602 Value is the x-position reached, relative to AREA of W. */
26604 static int
26605 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26606 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26607 enum draw_glyphs_face hl, int overlaps)
26609 struct glyph_string *head, *tail;
26610 struct glyph_string *s;
26611 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26612 int i, j, x_reached, last_x, area_left = 0;
26613 struct frame *f = XFRAME (WINDOW_FRAME (w));
26614 DECLARE_HDC (hdc);
26616 ALLOCATE_HDC (hdc, f);
26618 /* Let's rather be paranoid than getting a SEGV. */
26619 end = min (end, row->used[area]);
26620 start = clip_to_bounds (0, start, end);
26622 /* Translate X to frame coordinates. Set last_x to the right
26623 end of the drawing area. */
26624 if (row->full_width_p)
26626 /* X is relative to the left edge of W, without scroll bars
26627 or fringes. */
26628 area_left = WINDOW_LEFT_EDGE_X (w);
26629 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26630 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26632 else
26634 area_left = window_box_left (w, area);
26635 last_x = area_left + window_box_width (w, area);
26637 x += area_left;
26639 /* Build a doubly-linked list of glyph_string structures between
26640 head and tail from what we have to draw. Note that the macro
26641 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26642 the reason we use a separate variable `i'. */
26643 i = start;
26644 USE_SAFE_ALLOCA;
26645 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26646 if (tail)
26648 s = glyph_string_containing_background_width (tail);
26649 x_reached = s->x + s->background_width;
26651 else
26652 x_reached = x;
26654 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26655 the row, redraw some glyphs in front or following the glyph
26656 strings built above. */
26657 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26659 struct glyph_string *h, *t;
26660 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26661 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26662 bool check_mouse_face = false;
26663 int dummy_x = 0;
26665 /* If mouse highlighting is on, we may need to draw adjacent
26666 glyphs using mouse-face highlighting. */
26667 if (area == TEXT_AREA && row->mouse_face_p
26668 && hlinfo->mouse_face_beg_row >= 0
26669 && hlinfo->mouse_face_end_row >= 0)
26671 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26673 if (row_vpos >= hlinfo->mouse_face_beg_row
26674 && row_vpos <= hlinfo->mouse_face_end_row)
26676 check_mouse_face = true;
26677 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26678 ? hlinfo->mouse_face_beg_col : 0;
26679 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26680 ? hlinfo->mouse_face_end_col
26681 : row->used[TEXT_AREA];
26685 /* Compute overhangs for all glyph strings. */
26686 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26687 for (s = head; s; s = s->next)
26688 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26690 /* Prepend glyph strings for glyphs in front of the first glyph
26691 string that are overwritten because of the first glyph
26692 string's left overhang. The background of all strings
26693 prepended must be drawn because the first glyph string
26694 draws over it. */
26695 i = left_overwritten (head);
26696 if (i >= 0)
26698 enum draw_glyphs_face overlap_hl;
26700 /* If this row contains mouse highlighting, attempt to draw
26701 the overlapped glyphs with the correct highlight. This
26702 code fails if the overlap encompasses more than one glyph
26703 and mouse-highlight spans only some of these glyphs.
26704 However, making it work perfectly involves a lot more
26705 code, and I don't know if the pathological case occurs in
26706 practice, so we'll stick to this for now. --- cyd */
26707 if (check_mouse_face
26708 && mouse_beg_col < start && mouse_end_col > i)
26709 overlap_hl = DRAW_MOUSE_FACE;
26710 else
26711 overlap_hl = DRAW_NORMAL_TEXT;
26713 if (hl != overlap_hl)
26714 clip_head = head;
26715 j = i;
26716 BUILD_GLYPH_STRINGS (j, start, h, t,
26717 overlap_hl, dummy_x, last_x);
26718 start = i;
26719 compute_overhangs_and_x (t, head->x, true);
26720 prepend_glyph_string_lists (&head, &tail, h, t);
26721 if (clip_head == NULL)
26722 clip_head = head;
26725 /* Prepend glyph strings for glyphs in front of the first glyph
26726 string that overwrite that glyph string because of their
26727 right overhang. For these strings, only the foreground must
26728 be drawn, because it draws over the glyph string at `head'.
26729 The background must not be drawn because this would overwrite
26730 right overhangs of preceding glyphs for which no glyph
26731 strings exist. */
26732 i = left_overwriting (head);
26733 if (i >= 0)
26735 enum draw_glyphs_face overlap_hl;
26737 if (check_mouse_face
26738 && mouse_beg_col < start && mouse_end_col > i)
26739 overlap_hl = DRAW_MOUSE_FACE;
26740 else
26741 overlap_hl = DRAW_NORMAL_TEXT;
26743 if (hl == overlap_hl || clip_head == NULL)
26744 clip_head = head;
26745 BUILD_GLYPH_STRINGS (i, start, h, t,
26746 overlap_hl, dummy_x, last_x);
26747 for (s = h; s; s = s->next)
26748 s->background_filled_p = true;
26749 compute_overhangs_and_x (t, head->x, true);
26750 prepend_glyph_string_lists (&head, &tail, h, t);
26753 /* Append glyphs strings for glyphs following the last glyph
26754 string tail that are overwritten by tail. The background of
26755 these strings has to be drawn because tail's foreground draws
26756 over it. */
26757 i = right_overwritten (tail);
26758 if (i >= 0)
26760 enum draw_glyphs_face overlap_hl;
26762 if (check_mouse_face
26763 && mouse_beg_col < i && mouse_end_col > end)
26764 overlap_hl = DRAW_MOUSE_FACE;
26765 else
26766 overlap_hl = DRAW_NORMAL_TEXT;
26768 if (hl != overlap_hl)
26769 clip_tail = tail;
26770 BUILD_GLYPH_STRINGS (end, i, h, t,
26771 overlap_hl, x, last_x);
26772 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26773 we don't have `end = i;' here. */
26774 compute_overhangs_and_x (h, tail->x + tail->width, false);
26775 append_glyph_string_lists (&head, &tail, h, t);
26776 if (clip_tail == NULL)
26777 clip_tail = tail;
26780 /* Append glyph strings for glyphs following the last glyph
26781 string tail that overwrite tail. The foreground of such
26782 glyphs has to be drawn because it writes into the background
26783 of tail. The background must not be drawn because it could
26784 paint over the foreground of following glyphs. */
26785 i = right_overwriting (tail);
26786 if (i >= 0)
26788 enum draw_glyphs_face overlap_hl;
26789 if (check_mouse_face
26790 && mouse_beg_col < i && mouse_end_col > end)
26791 overlap_hl = DRAW_MOUSE_FACE;
26792 else
26793 overlap_hl = DRAW_NORMAL_TEXT;
26795 if (hl == overlap_hl || clip_tail == NULL)
26796 clip_tail = tail;
26797 i++; /* We must include the Ith glyph. */
26798 BUILD_GLYPH_STRINGS (end, i, h, t,
26799 overlap_hl, x, last_x);
26800 for (s = h; s; s = s->next)
26801 s->background_filled_p = true;
26802 compute_overhangs_and_x (h, tail->x + tail->width, false);
26803 append_glyph_string_lists (&head, &tail, h, t);
26805 tail = glyph_string_containing_background_width (tail);
26806 if (clip_tail)
26807 clip_tail = glyph_string_containing_background_width (clip_tail);
26808 if (clip_head || clip_tail)
26809 for (s = head; s; s = s->next)
26811 s->clip_head = clip_head;
26812 s->clip_tail = clip_tail;
26816 /* Draw all strings. */
26817 for (s = head; s; s = s->next)
26818 FRAME_RIF (f)->draw_glyph_string (s);
26820 #ifndef HAVE_NS
26821 /* When focus a sole frame and move horizontally, this clears on_p
26822 causing a failure to erase prev cursor position. */
26823 if (area == TEXT_AREA
26824 && !row->full_width_p
26825 /* When drawing overlapping rows, only the glyph strings'
26826 foreground is drawn, which doesn't erase a cursor
26827 completely. */
26828 && !overlaps)
26830 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26831 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26832 : (tail ? tail->x + tail->background_width : x));
26833 x0 -= area_left;
26834 x1 -= area_left;
26836 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26837 row->y, MATRIX_ROW_BOTTOM_Y (row));
26839 #endif
26841 /* Value is the x-position up to which drawn, relative to AREA of W.
26842 This doesn't include parts drawn because of overhangs. */
26843 if (row->full_width_p)
26844 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26845 else
26846 x_reached -= area_left;
26848 RELEASE_HDC (hdc, f);
26850 SAFE_FREE ();
26851 return x_reached;
26854 /* Find the first glyph in the run of underlined glyphs preceding the
26855 beginning of glyph string S, and return its font (which could be
26856 NULL). This is needed because that font determines the underline
26857 position and thickness for the entire run of the underlined glyphs.
26858 This function is called from the draw_glyph_string method of GUI
26859 frame's redisplay interface (RIF) when it needs to draw in an
26860 underlined face. */
26861 struct font *
26862 font_for_underline_metrics (struct glyph_string *s)
26864 struct glyph *g0 = s->row->glyphs[s->area], *g;
26866 for (g = s->first_glyph - 1; g >= g0; g--)
26868 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26869 if (!(prev_face && prev_face->underline_p))
26870 break;
26873 /* If preceding glyphs are not underlined, use the font of S. */
26874 if (g == s->first_glyph - 1)
26875 return s->font;
26876 else
26878 /* Otherwise use the font of the last glyph we saw in the above
26879 loop whose face had the underline_p flag set. */
26880 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26884 /* Expand row matrix if too narrow. Don't expand if area
26885 is not present. */
26887 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26889 if (!it->f->fonts_changed \
26890 && (it->glyph_row->glyphs[area] \
26891 < it->glyph_row->glyphs[area + 1])) \
26893 it->w->ncols_scale_factor++; \
26894 it->f->fonts_changed = true; \
26898 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26899 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26901 static void
26902 append_glyph (struct it *it)
26904 struct glyph *glyph;
26905 enum glyph_row_area area = it->area;
26907 eassert (it->glyph_row);
26908 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26910 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26911 if (glyph < it->glyph_row->glyphs[area + 1])
26913 /* If the glyph row is reversed, we need to prepend the glyph
26914 rather than append it. */
26915 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26917 struct glyph *g;
26919 /* Make room for the additional glyph. */
26920 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26921 g[1] = *g;
26922 glyph = it->glyph_row->glyphs[area];
26924 glyph->charpos = CHARPOS (it->position);
26925 glyph->object = it->object;
26926 if (it->pixel_width > 0)
26928 eassert (it->pixel_width <= SHRT_MAX);
26929 glyph->pixel_width = it->pixel_width;
26930 glyph->padding_p = false;
26932 else
26934 /* Assure at least 1-pixel width. Otherwise, cursor can't
26935 be displayed correctly. */
26936 glyph->pixel_width = 1;
26937 glyph->padding_p = true;
26939 glyph->ascent = it->ascent;
26940 glyph->descent = it->descent;
26941 glyph->voffset = it->voffset;
26942 glyph->type = CHAR_GLYPH;
26943 glyph->avoid_cursor_p = it->avoid_cursor_p;
26944 glyph->multibyte_p = it->multibyte_p;
26945 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26947 /* In R2L rows, the left and the right box edges need to be
26948 drawn in reverse direction. */
26949 glyph->right_box_line_p = it->start_of_box_run_p;
26950 glyph->left_box_line_p = it->end_of_box_run_p;
26952 else
26954 glyph->left_box_line_p = it->start_of_box_run_p;
26955 glyph->right_box_line_p = it->end_of_box_run_p;
26957 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26958 || it->phys_descent > it->descent);
26959 glyph->glyph_not_available_p = it->glyph_not_available_p;
26960 glyph->face_id = it->face_id;
26961 glyph->u.ch = it->char_to_display;
26962 glyph->slice.img = null_glyph_slice;
26963 glyph->font_type = FONT_TYPE_UNKNOWN;
26964 if (it->bidi_p)
26966 glyph->resolved_level = it->bidi_it.resolved_level;
26967 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26968 glyph->bidi_type = it->bidi_it.type;
26970 else
26972 glyph->resolved_level = 0;
26973 glyph->bidi_type = UNKNOWN_BT;
26975 ++it->glyph_row->used[area];
26977 else
26978 IT_EXPAND_MATRIX_WIDTH (it, area);
26981 /* Store one glyph for the composition IT->cmp_it.id in
26982 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26983 non-null. */
26985 static void
26986 append_composite_glyph (struct it *it)
26988 struct glyph *glyph;
26989 enum glyph_row_area area = it->area;
26991 eassert (it->glyph_row);
26993 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26994 if (glyph < it->glyph_row->glyphs[area + 1])
26996 /* If the glyph row is reversed, we need to prepend the glyph
26997 rather than append it. */
26998 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
27000 struct glyph *g;
27002 /* Make room for the new glyph. */
27003 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27004 g[1] = *g;
27005 glyph = it->glyph_row->glyphs[it->area];
27007 glyph->charpos = it->cmp_it.charpos;
27008 glyph->object = it->object;
27009 eassert (it->pixel_width <= SHRT_MAX);
27010 glyph->pixel_width = it->pixel_width;
27011 glyph->ascent = it->ascent;
27012 glyph->descent = it->descent;
27013 glyph->voffset = it->voffset;
27014 glyph->type = COMPOSITE_GLYPH;
27015 if (it->cmp_it.ch < 0)
27017 glyph->u.cmp.automatic = false;
27018 glyph->u.cmp.id = it->cmp_it.id;
27019 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
27021 else
27023 glyph->u.cmp.automatic = true;
27024 glyph->u.cmp.id = it->cmp_it.id;
27025 glyph->slice.cmp.from = it->cmp_it.from;
27026 glyph->slice.cmp.to = it->cmp_it.to - 1;
27028 glyph->avoid_cursor_p = it->avoid_cursor_p;
27029 glyph->multibyte_p = it->multibyte_p;
27030 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27032 /* In R2L rows, the left and the right box edges need to be
27033 drawn in reverse direction. */
27034 glyph->right_box_line_p = it->start_of_box_run_p;
27035 glyph->left_box_line_p = it->end_of_box_run_p;
27037 else
27039 glyph->left_box_line_p = it->start_of_box_run_p;
27040 glyph->right_box_line_p = it->end_of_box_run_p;
27042 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27043 || it->phys_descent > it->descent);
27044 glyph->padding_p = false;
27045 glyph->glyph_not_available_p = false;
27046 glyph->face_id = it->face_id;
27047 glyph->font_type = FONT_TYPE_UNKNOWN;
27048 if (it->bidi_p)
27050 glyph->resolved_level = it->bidi_it.resolved_level;
27051 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27052 glyph->bidi_type = it->bidi_it.type;
27054 ++it->glyph_row->used[area];
27056 else
27057 IT_EXPAND_MATRIX_WIDTH (it, area);
27061 /* Change IT->ascent and IT->height according to the setting of
27062 IT->voffset. */
27064 static void
27065 take_vertical_position_into_account (struct it *it)
27067 if (it->voffset)
27069 if (it->voffset < 0)
27070 /* Increase the ascent so that we can display the text higher
27071 in the line. */
27072 it->ascent -= it->voffset;
27073 else
27074 /* Increase the descent so that we can display the text lower
27075 in the line. */
27076 it->descent += it->voffset;
27081 /* Produce glyphs/get display metrics for the image IT is loaded with.
27082 See the description of struct display_iterator in dispextern.h for
27083 an overview of struct display_iterator. */
27085 static void
27086 produce_image_glyph (struct it *it)
27088 struct image *img;
27089 struct face *face;
27090 int glyph_ascent, crop;
27091 struct glyph_slice slice;
27093 eassert (it->what == IT_IMAGE);
27095 face = FACE_FROM_ID (it->f, it->face_id);
27096 /* Make sure X resources of the face is loaded. */
27097 prepare_face_for_display (it->f, face);
27099 if (it->image_id < 0)
27101 /* Fringe bitmap. */
27102 it->ascent = it->phys_ascent = 0;
27103 it->descent = it->phys_descent = 0;
27104 it->pixel_width = 0;
27105 it->nglyphs = 0;
27106 return;
27109 img = IMAGE_FROM_ID (it->f, it->image_id);
27110 /* Make sure X resources of the image is loaded. */
27111 prepare_image_for_display (it->f, img);
27113 slice.x = slice.y = 0;
27114 slice.width = img->width;
27115 slice.height = img->height;
27117 if (INTEGERP (it->slice.x))
27118 slice.x = XINT (it->slice.x);
27119 else if (FLOATP (it->slice.x))
27120 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27122 if (INTEGERP (it->slice.y))
27123 slice.y = XINT (it->slice.y);
27124 else if (FLOATP (it->slice.y))
27125 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27127 if (INTEGERP (it->slice.width))
27128 slice.width = XINT (it->slice.width);
27129 else if (FLOATP (it->slice.width))
27130 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27132 if (INTEGERP (it->slice.height))
27133 slice.height = XINT (it->slice.height);
27134 else if (FLOATP (it->slice.height))
27135 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27137 if (slice.x >= img->width)
27138 slice.x = img->width;
27139 if (slice.y >= img->height)
27140 slice.y = img->height;
27141 if (slice.x + slice.width >= img->width)
27142 slice.width = img->width - slice.x;
27143 if (slice.y + slice.height > img->height)
27144 slice.height = img->height - slice.y;
27146 if (slice.width == 0 || slice.height == 0)
27147 return;
27149 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27151 it->descent = slice.height - glyph_ascent;
27152 if (slice.y == 0)
27153 it->descent += img->vmargin;
27154 if (slice.y + slice.height == img->height)
27155 it->descent += img->vmargin;
27156 it->phys_descent = it->descent;
27158 it->pixel_width = slice.width;
27159 if (slice.x == 0)
27160 it->pixel_width += img->hmargin;
27161 if (slice.x + slice.width == img->width)
27162 it->pixel_width += img->hmargin;
27164 /* It's quite possible for images to have an ascent greater than
27165 their height, so don't get confused in that case. */
27166 if (it->descent < 0)
27167 it->descent = 0;
27169 it->nglyphs = 1;
27171 if (face->box != FACE_NO_BOX)
27173 if (face->box_line_width > 0)
27175 if (slice.y == 0)
27176 it->ascent += face->box_line_width;
27177 if (slice.y + slice.height == img->height)
27178 it->descent += face->box_line_width;
27181 if (it->start_of_box_run_p && slice.x == 0)
27182 it->pixel_width += eabs (face->box_line_width);
27183 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27184 it->pixel_width += eabs (face->box_line_width);
27187 take_vertical_position_into_account (it);
27189 /* Automatically crop wide image glyphs at right edge so we can
27190 draw the cursor on same display row. */
27191 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27192 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27194 it->pixel_width -= crop;
27195 slice.width -= crop;
27198 if (it->glyph_row)
27200 struct glyph *glyph;
27201 enum glyph_row_area area = it->area;
27203 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27204 if (it->glyph_row->reversed_p)
27206 struct glyph *g;
27208 /* Make room for the new glyph. */
27209 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27210 g[1] = *g;
27211 glyph = it->glyph_row->glyphs[it->area];
27213 if (glyph < it->glyph_row->glyphs[area + 1])
27215 glyph->charpos = CHARPOS (it->position);
27216 glyph->object = it->object;
27217 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27218 glyph->ascent = glyph_ascent;
27219 glyph->descent = it->descent;
27220 glyph->voffset = it->voffset;
27221 glyph->type = IMAGE_GLYPH;
27222 glyph->avoid_cursor_p = it->avoid_cursor_p;
27223 glyph->multibyte_p = it->multibyte_p;
27224 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27226 /* In R2L rows, the left and the right box edges need to be
27227 drawn in reverse direction. */
27228 glyph->right_box_line_p = it->start_of_box_run_p;
27229 glyph->left_box_line_p = it->end_of_box_run_p;
27231 else
27233 glyph->left_box_line_p = it->start_of_box_run_p;
27234 glyph->right_box_line_p = it->end_of_box_run_p;
27236 glyph->overlaps_vertically_p = false;
27237 glyph->padding_p = false;
27238 glyph->glyph_not_available_p = false;
27239 glyph->face_id = it->face_id;
27240 glyph->u.img_id = img->id;
27241 glyph->slice.img = slice;
27242 glyph->font_type = FONT_TYPE_UNKNOWN;
27243 if (it->bidi_p)
27245 glyph->resolved_level = it->bidi_it.resolved_level;
27246 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27247 glyph->bidi_type = it->bidi_it.type;
27249 ++it->glyph_row->used[area];
27251 else
27252 IT_EXPAND_MATRIX_WIDTH (it, area);
27256 static void
27257 produce_xwidget_glyph (struct it *it)
27259 #ifdef HAVE_XWIDGETS
27260 struct xwidget *xw;
27261 int glyph_ascent, crop;
27262 eassert (it->what == IT_XWIDGET);
27264 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27265 /* Make sure X resources of the face is loaded. */
27266 prepare_face_for_display (it->f, face);
27268 xw = it->xwidget;
27269 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27270 it->descent = xw->height/2;
27271 it->phys_descent = it->descent;
27272 it->pixel_width = xw->width;
27273 /* It's quite possible for images to have an ascent greater than
27274 their height, so don't get confused in that case. */
27275 if (it->descent < 0)
27276 it->descent = 0;
27278 it->nglyphs = 1;
27280 if (face->box != FACE_NO_BOX)
27282 if (face->box_line_width > 0)
27284 it->ascent += face->box_line_width;
27285 it->descent += face->box_line_width;
27288 if (it->start_of_box_run_p)
27289 it->pixel_width += eabs (face->box_line_width);
27290 it->pixel_width += eabs (face->box_line_width);
27293 take_vertical_position_into_account (it);
27295 /* Automatically crop wide image glyphs at right edge so we can
27296 draw the cursor on same display row. */
27297 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27298 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27299 it->pixel_width -= crop;
27301 if (it->glyph_row)
27303 enum glyph_row_area area = it->area;
27304 struct glyph *glyph
27305 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27307 if (it->glyph_row->reversed_p)
27309 struct glyph *g;
27311 /* Make room for the new glyph. */
27312 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27313 g[1] = *g;
27314 glyph = it->glyph_row->glyphs[it->area];
27316 if (glyph < it->glyph_row->glyphs[area + 1])
27318 glyph->charpos = CHARPOS (it->position);
27319 glyph->object = it->object;
27320 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27321 glyph->ascent = glyph_ascent;
27322 glyph->descent = it->descent;
27323 glyph->voffset = it->voffset;
27324 glyph->type = XWIDGET_GLYPH;
27325 glyph->avoid_cursor_p = it->avoid_cursor_p;
27326 glyph->multibyte_p = it->multibyte_p;
27327 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27329 /* In R2L rows, the left and the right box edges need to be
27330 drawn in reverse direction. */
27331 glyph->right_box_line_p = it->start_of_box_run_p;
27332 glyph->left_box_line_p = it->end_of_box_run_p;
27334 else
27336 glyph->left_box_line_p = it->start_of_box_run_p;
27337 glyph->right_box_line_p = it->end_of_box_run_p;
27339 glyph->overlaps_vertically_p = 0;
27340 glyph->padding_p = 0;
27341 glyph->glyph_not_available_p = 0;
27342 glyph->face_id = it->face_id;
27343 glyph->u.xwidget = it->xwidget;
27344 glyph->font_type = FONT_TYPE_UNKNOWN;
27345 if (it->bidi_p)
27347 glyph->resolved_level = it->bidi_it.resolved_level;
27348 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27349 glyph->bidi_type = it->bidi_it.type;
27351 ++it->glyph_row->used[area];
27353 else
27354 IT_EXPAND_MATRIX_WIDTH (it, area);
27356 #endif
27359 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27360 of the glyph, WIDTH and HEIGHT are the width and height of the
27361 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27363 static void
27364 append_stretch_glyph (struct it *it, Lisp_Object object,
27365 int width, int height, int ascent)
27367 struct glyph *glyph;
27368 enum glyph_row_area area = it->area;
27370 eassert (ascent >= 0 && ascent <= height);
27372 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27373 if (glyph < it->glyph_row->glyphs[area + 1])
27375 /* If the glyph row is reversed, we need to prepend the glyph
27376 rather than append it. */
27377 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27379 struct glyph *g;
27381 /* Make room for the additional glyph. */
27382 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27383 g[1] = *g;
27384 glyph = it->glyph_row->glyphs[area];
27386 /* Decrease the width of the first glyph of the row that
27387 begins before first_visible_x (e.g., due to hscroll).
27388 This is so the overall width of the row becomes smaller
27389 by the scroll amount, and the stretch glyph appended by
27390 extend_face_to_end_of_line will be wider, to shift the
27391 row glyphs to the right. (In L2R rows, the corresponding
27392 left-shift effect is accomplished by setting row->x to a
27393 negative value, which won't work with R2L rows.)
27395 This must leave us with a positive value of WIDTH, since
27396 otherwise the call to move_it_in_display_line_to at the
27397 beginning of display_line would have got past the entire
27398 first glyph, and then it->current_x would have been
27399 greater or equal to it->first_visible_x. */
27400 if (it->current_x < it->first_visible_x)
27401 width -= it->first_visible_x - it->current_x;
27402 eassert (width > 0);
27404 glyph->charpos = CHARPOS (it->position);
27405 glyph->object = object;
27406 /* FIXME: It would be better to use TYPE_MAX here, but
27407 __typeof__ is not portable enough... */
27408 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27409 glyph->ascent = ascent;
27410 glyph->descent = height - ascent;
27411 glyph->voffset = it->voffset;
27412 glyph->type = STRETCH_GLYPH;
27413 glyph->avoid_cursor_p = it->avoid_cursor_p;
27414 glyph->multibyte_p = it->multibyte_p;
27415 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27417 /* In R2L rows, the left and the right box edges need to be
27418 drawn in reverse direction. */
27419 glyph->right_box_line_p = it->start_of_box_run_p;
27420 glyph->left_box_line_p = it->end_of_box_run_p;
27422 else
27424 glyph->left_box_line_p = it->start_of_box_run_p;
27425 glyph->right_box_line_p = it->end_of_box_run_p;
27427 glyph->overlaps_vertically_p = false;
27428 glyph->padding_p = false;
27429 glyph->glyph_not_available_p = false;
27430 glyph->face_id = it->face_id;
27431 glyph->u.stretch.ascent = ascent;
27432 glyph->u.stretch.height = height;
27433 glyph->slice.img = null_glyph_slice;
27434 glyph->font_type = FONT_TYPE_UNKNOWN;
27435 if (it->bidi_p)
27437 glyph->resolved_level = it->bidi_it.resolved_level;
27438 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27439 glyph->bidi_type = it->bidi_it.type;
27441 else
27443 glyph->resolved_level = 0;
27444 glyph->bidi_type = UNKNOWN_BT;
27446 ++it->glyph_row->used[area];
27448 else
27449 IT_EXPAND_MATRIX_WIDTH (it, area);
27452 #endif /* HAVE_WINDOW_SYSTEM */
27454 /* Produce a stretch glyph for iterator IT. IT->object is the value
27455 of the glyph property displayed. The value must be a list
27456 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27457 being recognized:
27459 1. `:width WIDTH' specifies that the space should be WIDTH *
27460 canonical char width wide. WIDTH may be an integer or floating
27461 point number.
27463 2. `:relative-width FACTOR' specifies that the width of the stretch
27464 should be computed from the width of the first character having the
27465 `glyph' property, and should be FACTOR times that width.
27467 3. `:align-to HPOS' specifies that the space should be wide enough
27468 to reach HPOS, a value in canonical character units.
27470 Exactly one of the above pairs must be present.
27472 4. `:height HEIGHT' specifies that the height of the stretch produced
27473 should be HEIGHT, measured in canonical character units.
27475 5. `:relative-height FACTOR' specifies that the height of the
27476 stretch should be FACTOR times the height of the characters having
27477 the glyph property.
27479 Either none or exactly one of 4 or 5 must be present.
27481 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27482 of the stretch should be used for the ascent of the stretch.
27483 ASCENT must be in the range 0 <= ASCENT <= 100. */
27485 void
27486 produce_stretch_glyph (struct it *it)
27488 /* (space :width WIDTH :height HEIGHT ...) */
27489 Lisp_Object prop, plist;
27490 int width = 0, height = 0, align_to = -1;
27491 bool zero_width_ok_p = false;
27492 double tem;
27493 struct font *font = NULL;
27495 #ifdef HAVE_WINDOW_SYSTEM
27496 int ascent = 0;
27497 bool zero_height_ok_p = false;
27499 if (FRAME_WINDOW_P (it->f))
27501 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27502 font = face->font ? face->font : FRAME_FONT (it->f);
27503 prepare_face_for_display (it->f, face);
27505 #endif
27507 /* List should start with `space'. */
27508 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27509 plist = XCDR (it->object);
27511 /* Compute the width of the stretch. */
27512 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27513 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27515 /* Absolute width `:width WIDTH' specified and valid. */
27516 zero_width_ok_p = true;
27517 width = (int)tem;
27519 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27521 /* Relative width `:relative-width FACTOR' specified and valid.
27522 Compute the width of the characters having the `glyph'
27523 property. */
27524 struct it it2;
27525 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27527 it2 = *it;
27528 if (it->multibyte_p)
27529 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27530 else
27532 it2.c = it2.char_to_display = *p, it2.len = 1;
27533 if (! ASCII_CHAR_P (it2.c))
27534 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27537 it2.glyph_row = NULL;
27538 it2.what = IT_CHARACTER;
27539 PRODUCE_GLYPHS (&it2);
27540 width = NUMVAL (prop) * it2.pixel_width;
27542 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27543 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27544 &align_to))
27546 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27547 align_to = (align_to < 0
27549 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27550 else if (align_to < 0)
27551 align_to = window_box_left_offset (it->w, TEXT_AREA);
27552 width = max (0, (int)tem + align_to - it->current_x);
27553 zero_width_ok_p = true;
27555 else
27556 /* Nothing specified -> width defaults to canonical char width. */
27557 width = FRAME_COLUMN_WIDTH (it->f);
27559 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27560 width = 1;
27562 #ifdef HAVE_WINDOW_SYSTEM
27563 /* Compute height. */
27564 if (FRAME_WINDOW_P (it->f))
27566 int default_height = normal_char_height (font, ' ');
27568 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27569 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27571 height = (int)tem;
27572 zero_height_ok_p = true;
27574 else if (prop = Fplist_get (plist, QCrelative_height),
27575 NUMVAL (prop) > 0)
27576 height = default_height * NUMVAL (prop);
27577 else
27578 height = default_height;
27580 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27581 height = 1;
27583 /* Compute percentage of height used for ascent. If
27584 `:ascent ASCENT' is present and valid, use that. Otherwise,
27585 derive the ascent from the font in use. */
27586 if (prop = Fplist_get (plist, QCascent),
27587 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27588 ascent = height * NUMVAL (prop) / 100.0;
27589 else if (!NILP (prop)
27590 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27591 ascent = min (max (0, (int)tem), height);
27592 else
27593 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27595 else
27596 #endif /* HAVE_WINDOW_SYSTEM */
27597 height = 1;
27599 if (width > 0 && it->line_wrap != TRUNCATE
27600 && it->current_x + width > it->last_visible_x)
27602 width = it->last_visible_x - it->current_x;
27603 #ifdef HAVE_WINDOW_SYSTEM
27604 /* Subtract one more pixel from the stretch width, but only on
27605 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27606 width -= FRAME_WINDOW_P (it->f);
27607 #endif
27610 if (width > 0 && height > 0 && it->glyph_row)
27612 Lisp_Object o_object = it->object;
27613 Lisp_Object object = it->stack[it->sp - 1].string;
27614 int n = width;
27616 if (!STRINGP (object))
27617 object = it->w->contents;
27618 #ifdef HAVE_WINDOW_SYSTEM
27619 if (FRAME_WINDOW_P (it->f))
27620 append_stretch_glyph (it, object, width, height, ascent);
27621 else
27622 #endif
27624 it->object = object;
27625 it->char_to_display = ' ';
27626 it->pixel_width = it->len = 1;
27627 while (n--)
27628 tty_append_glyph (it);
27629 it->object = o_object;
27633 it->pixel_width = width;
27634 #ifdef HAVE_WINDOW_SYSTEM
27635 if (FRAME_WINDOW_P (it->f))
27637 it->ascent = it->phys_ascent = ascent;
27638 it->descent = it->phys_descent = height - it->ascent;
27639 it->nglyphs = width > 0 && height > 0;
27640 take_vertical_position_into_account (it);
27642 else
27643 #endif
27644 it->nglyphs = width;
27647 /* Get information about special display element WHAT in an
27648 environment described by IT. WHAT is one of IT_TRUNCATION or
27649 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27650 non-null glyph_row member. This function ensures that fields like
27651 face_id, c, len of IT are left untouched. */
27653 static void
27654 produce_special_glyphs (struct it *it, enum display_element_type what)
27656 struct it temp_it;
27657 Lisp_Object gc;
27658 GLYPH glyph;
27660 temp_it = *it;
27661 temp_it.object = Qnil;
27662 memset (&temp_it.current, 0, sizeof temp_it.current);
27664 if (what == IT_CONTINUATION)
27666 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27667 if (it->bidi_it.paragraph_dir == R2L)
27668 SET_GLYPH_FROM_CHAR (glyph, '/');
27669 else
27670 SET_GLYPH_FROM_CHAR (glyph, '\\');
27671 if (it->dp
27672 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27674 /* FIXME: Should we mirror GC for R2L lines? */
27675 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27676 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27679 else if (what == IT_TRUNCATION)
27681 /* Truncation glyph. */
27682 SET_GLYPH_FROM_CHAR (glyph, '$');
27683 if (it->dp
27684 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27686 /* FIXME: Should we mirror GC for R2L lines? */
27687 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27688 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27691 else
27692 emacs_abort ();
27694 #ifdef HAVE_WINDOW_SYSTEM
27695 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27696 is turned off, we precede the truncation/continuation glyphs by a
27697 stretch glyph whose width is computed such that these special
27698 glyphs are aligned at the window margin, even when very different
27699 fonts are used in different glyph rows. */
27700 if (FRAME_WINDOW_P (temp_it.f)
27701 /* init_iterator calls this with it->glyph_row == NULL, and it
27702 wants only the pixel width of the truncation/continuation
27703 glyphs. */
27704 && temp_it.glyph_row
27705 /* insert_left_trunc_glyphs calls us at the beginning of the
27706 row, and it has its own calculation of the stretch glyph
27707 width. */
27708 && temp_it.glyph_row->used[TEXT_AREA] > 0
27709 && (temp_it.glyph_row->reversed_p
27710 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27711 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27713 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27715 if (stretch_width > 0)
27717 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27718 struct font *font =
27719 face->font ? face->font : FRAME_FONT (temp_it.f);
27720 int stretch_ascent =
27721 (((temp_it.ascent + temp_it.descent)
27722 * FONT_BASE (font)) / FONT_HEIGHT (font));
27724 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27725 temp_it.ascent + temp_it.descent,
27726 stretch_ascent);
27729 #endif
27731 temp_it.dp = NULL;
27732 temp_it.what = IT_CHARACTER;
27733 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27734 temp_it.face_id = GLYPH_FACE (glyph);
27735 temp_it.len = CHAR_BYTES (temp_it.c);
27737 PRODUCE_GLYPHS (&temp_it);
27738 it->pixel_width = temp_it.pixel_width;
27739 it->nglyphs = temp_it.nglyphs;
27742 #ifdef HAVE_WINDOW_SYSTEM
27744 /* Calculate line-height and line-spacing properties.
27745 An integer value specifies explicit pixel value.
27746 A float value specifies relative value to current face height.
27747 A cons (float . face-name) specifies relative value to
27748 height of specified face font.
27750 Returns height in pixels, or nil. */
27752 static Lisp_Object
27753 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27754 int boff, bool override)
27756 Lisp_Object face_name = Qnil;
27757 int ascent, descent, height;
27759 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27760 return val;
27762 if (CONSP (val))
27764 face_name = XCAR (val);
27765 val = XCDR (val);
27766 if (!NUMBERP (val))
27767 val = make_number (1);
27768 if (NILP (face_name))
27770 height = it->ascent + it->descent;
27771 goto scale;
27775 if (NILP (face_name))
27777 font = FRAME_FONT (it->f);
27778 boff = FRAME_BASELINE_OFFSET (it->f);
27780 else if (EQ (face_name, Qt))
27782 override = false;
27784 else
27786 int face_id;
27787 struct face *face;
27789 face_id = lookup_named_face (it->f, face_name, false);
27790 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27791 if (face == NULL || ((font = face->font) == NULL))
27792 return make_number (-1);
27793 boff = font->baseline_offset;
27794 if (font->vertical_centering)
27795 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27798 normal_char_ascent_descent (font, -1, &ascent, &descent);
27800 if (override)
27802 it->override_ascent = ascent;
27803 it->override_descent = descent;
27804 it->override_boff = boff;
27807 height = ascent + descent;
27809 scale:
27810 if (FLOATP (val))
27811 height = (int)(XFLOAT_DATA (val) * height);
27812 else if (INTEGERP (val))
27813 height *= XINT (val);
27815 return make_number (height);
27819 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27820 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27821 and only if this is for a character for which no font was found.
27823 If the display method (it->glyphless_method) is
27824 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27825 length of the acronym or the hexadecimal string, UPPER_XOFF and
27826 UPPER_YOFF are pixel offsets for the upper part of the string,
27827 LOWER_XOFF and LOWER_YOFF are for the lower part.
27829 For the other display methods, LEN through LOWER_YOFF are zero. */
27831 static void
27832 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27833 short upper_xoff, short upper_yoff,
27834 short lower_xoff, short lower_yoff)
27836 struct glyph *glyph;
27837 enum glyph_row_area area = it->area;
27839 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27840 if (glyph < it->glyph_row->glyphs[area + 1])
27842 /* If the glyph row is reversed, we need to prepend the glyph
27843 rather than append it. */
27844 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27846 struct glyph *g;
27848 /* Make room for the additional glyph. */
27849 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27850 g[1] = *g;
27851 glyph = it->glyph_row->glyphs[area];
27853 glyph->charpos = CHARPOS (it->position);
27854 glyph->object = it->object;
27855 eassert (it->pixel_width <= SHRT_MAX);
27856 glyph->pixel_width = it->pixel_width;
27857 glyph->ascent = it->ascent;
27858 glyph->descent = it->descent;
27859 glyph->voffset = it->voffset;
27860 glyph->type = GLYPHLESS_GLYPH;
27861 glyph->u.glyphless.method = it->glyphless_method;
27862 glyph->u.glyphless.for_no_font = for_no_font;
27863 glyph->u.glyphless.len = len;
27864 glyph->u.glyphless.ch = it->c;
27865 glyph->slice.glyphless.upper_xoff = upper_xoff;
27866 glyph->slice.glyphless.upper_yoff = upper_yoff;
27867 glyph->slice.glyphless.lower_xoff = lower_xoff;
27868 glyph->slice.glyphless.lower_yoff = lower_yoff;
27869 glyph->avoid_cursor_p = it->avoid_cursor_p;
27870 glyph->multibyte_p = it->multibyte_p;
27871 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27873 /* In R2L rows, the left and the right box edges need to be
27874 drawn in reverse direction. */
27875 glyph->right_box_line_p = it->start_of_box_run_p;
27876 glyph->left_box_line_p = it->end_of_box_run_p;
27878 else
27880 glyph->left_box_line_p = it->start_of_box_run_p;
27881 glyph->right_box_line_p = it->end_of_box_run_p;
27883 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27884 || it->phys_descent > it->descent);
27885 glyph->padding_p = false;
27886 glyph->glyph_not_available_p = false;
27887 glyph->face_id = face_id;
27888 glyph->font_type = FONT_TYPE_UNKNOWN;
27889 if (it->bidi_p)
27891 glyph->resolved_level = it->bidi_it.resolved_level;
27892 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27893 glyph->bidi_type = it->bidi_it.type;
27895 ++it->glyph_row->used[area];
27897 else
27898 IT_EXPAND_MATRIX_WIDTH (it, area);
27902 /* Produce a glyph for a glyphless character for iterator IT.
27903 IT->glyphless_method specifies which method to use for displaying
27904 the character. See the description of enum
27905 glyphless_display_method in dispextern.h for the detail.
27907 FOR_NO_FONT is true if and only if this is for a character for
27908 which no font was found. ACRONYM, if non-nil, is an acronym string
27909 for the character. */
27911 static void
27912 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27914 int face_id;
27915 struct face *face;
27916 struct font *font;
27917 int base_width, base_height, width, height;
27918 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27919 int len;
27921 /* Get the metrics of the base font. We always refer to the current
27922 ASCII face. */
27923 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27924 font = face->font ? face->font : FRAME_FONT (it->f);
27925 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27926 it->ascent += font->baseline_offset;
27927 it->descent -= font->baseline_offset;
27928 base_height = it->ascent + it->descent;
27929 base_width = font->average_width;
27931 face_id = merge_glyphless_glyph_face (it);
27933 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27935 it->pixel_width = THIN_SPACE_WIDTH;
27936 len = 0;
27937 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27939 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27941 width = CHARACTER_WIDTH (it->c);
27942 if (width == 0)
27943 width = 1;
27944 else if (width > 4)
27945 width = 4;
27946 it->pixel_width = base_width * width;
27947 len = 0;
27948 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27950 else
27952 char buf[7];
27953 const char *str;
27954 unsigned int code[6];
27955 int upper_len;
27956 int ascent, descent;
27957 struct font_metrics metrics_upper, metrics_lower;
27959 face = FACE_FROM_ID (it->f, face_id);
27960 font = face->font ? face->font : FRAME_FONT (it->f);
27961 prepare_face_for_display (it->f, face);
27963 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27965 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27966 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27967 if (CONSP (acronym))
27968 acronym = XCAR (acronym);
27969 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27971 else
27973 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27974 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27975 str = buf;
27977 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27978 code[len] = font->driver->encode_char (font, str[len]);
27979 upper_len = (len + 1) / 2;
27980 font->driver->text_extents (font, code, upper_len,
27981 &metrics_upper);
27982 font->driver->text_extents (font, code + upper_len, len - upper_len,
27983 &metrics_lower);
27987 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27988 width = max (metrics_upper.width, metrics_lower.width) + 4;
27989 upper_xoff = upper_yoff = 2; /* the typical case */
27990 if (base_width >= width)
27992 /* Align the upper to the left, the lower to the right. */
27993 it->pixel_width = base_width;
27994 lower_xoff = base_width - 2 - metrics_lower.width;
27996 else
27998 /* Center the shorter one. */
27999 it->pixel_width = width;
28000 if (metrics_upper.width >= metrics_lower.width)
28001 lower_xoff = (width - metrics_lower.width) / 2;
28002 else
28004 /* FIXME: This code doesn't look right. It formerly was
28005 missing the "lower_xoff = 0;", which couldn't have
28006 been right since it left lower_xoff uninitialized. */
28007 lower_xoff = 0;
28008 upper_xoff = (width - metrics_upper.width) / 2;
28012 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
28013 top, bottom, and between upper and lower strings. */
28014 height = (metrics_upper.ascent + metrics_upper.descent
28015 + metrics_lower.ascent + metrics_lower.descent) + 5;
28016 /* Center vertically.
28017 H:base_height, D:base_descent
28018 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
28020 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
28021 descent = D - H/2 + h/2;
28022 lower_yoff = descent - 2 - ld;
28023 upper_yoff = lower_yoff - la - 1 - ud; */
28024 ascent = - (it->descent - (base_height + height + 1) / 2);
28025 descent = it->descent - (base_height - height) / 2;
28026 lower_yoff = descent - 2 - metrics_lower.descent;
28027 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
28028 - metrics_upper.descent);
28029 /* Don't make the height shorter than the base height. */
28030 if (height > base_height)
28032 it->ascent = ascent;
28033 it->descent = descent;
28037 it->phys_ascent = it->ascent;
28038 it->phys_descent = it->descent;
28039 if (it->glyph_row)
28040 append_glyphless_glyph (it, face_id, for_no_font, len,
28041 upper_xoff, upper_yoff,
28042 lower_xoff, lower_yoff);
28043 it->nglyphs = 1;
28044 take_vertical_position_into_account (it);
28048 /* RIF:
28049 Produce glyphs/get display metrics for the display element IT is
28050 loaded with. See the description of struct it in dispextern.h
28051 for an overview of struct it. */
28053 void
28054 x_produce_glyphs (struct it *it)
28056 int extra_line_spacing = it->extra_line_spacing;
28058 it->glyph_not_available_p = false;
28060 if (it->what == IT_CHARACTER)
28062 XChar2b char2b;
28063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28064 struct font *font = face->font;
28065 struct font_metrics *pcm = NULL;
28066 int boff; /* Baseline offset. */
28068 if (font == NULL)
28070 /* When no suitable font is found, display this character by
28071 the method specified in the first extra slot of
28072 Vglyphless_char_display. */
28073 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
28075 eassert (it->what == IT_GLYPHLESS);
28076 produce_glyphless_glyph (it, true,
28077 STRINGP (acronym) ? acronym : Qnil);
28078 goto done;
28081 boff = font->baseline_offset;
28082 if (font->vertical_centering)
28083 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28085 if (it->char_to_display != '\n' && it->char_to_display != '\t')
28087 it->nglyphs = 1;
28089 if (it->override_ascent >= 0)
28091 it->ascent = it->override_ascent;
28092 it->descent = it->override_descent;
28093 boff = it->override_boff;
28095 else
28097 it->ascent = FONT_BASE (font) + boff;
28098 it->descent = FONT_DESCENT (font) - boff;
28101 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28103 pcm = get_per_char_metric (font, &char2b);
28104 if (pcm->width == 0
28105 && pcm->rbearing == 0 && pcm->lbearing == 0)
28106 pcm = NULL;
28109 if (pcm)
28111 it->phys_ascent = pcm->ascent + boff;
28112 it->phys_descent = pcm->descent - boff;
28113 it->pixel_width = pcm->width;
28114 /* Don't use font-global values for ascent and descent
28115 if they result in an exceedingly large line height. */
28116 if (it->override_ascent < 0)
28118 if (FONT_TOO_HIGH (font))
28120 it->ascent = it->phys_ascent;
28121 it->descent = it->phys_descent;
28122 /* These limitations are enforced by an
28123 assertion near the end of this function. */
28124 if (it->ascent < 0)
28125 it->ascent = 0;
28126 if (it->descent < 0)
28127 it->descent = 0;
28131 else
28133 it->glyph_not_available_p = true;
28134 it->phys_ascent = it->ascent;
28135 it->phys_descent = it->descent;
28136 it->pixel_width = font->space_width;
28139 if (it->constrain_row_ascent_descent_p)
28141 if (it->descent > it->max_descent)
28143 it->ascent += it->descent - it->max_descent;
28144 it->descent = it->max_descent;
28146 if (it->ascent > it->max_ascent)
28148 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28149 it->ascent = it->max_ascent;
28151 it->phys_ascent = min (it->phys_ascent, it->ascent);
28152 it->phys_descent = min (it->phys_descent, it->descent);
28153 extra_line_spacing = 0;
28156 /* If this is a space inside a region of text with
28157 `space-width' property, change its width. */
28158 bool stretched_p
28159 = it->char_to_display == ' ' && !NILP (it->space_width);
28160 if (stretched_p)
28161 it->pixel_width *= XFLOATINT (it->space_width);
28163 /* If face has a box, add the box thickness to the character
28164 height. If character has a box line to the left and/or
28165 right, add the box line width to the character's width. */
28166 if (face->box != FACE_NO_BOX)
28168 int thick = face->box_line_width;
28170 if (thick > 0)
28172 it->ascent += thick;
28173 it->descent += thick;
28175 else
28176 thick = -thick;
28178 if (it->start_of_box_run_p)
28179 it->pixel_width += thick;
28180 if (it->end_of_box_run_p)
28181 it->pixel_width += thick;
28184 /* If face has an overline, add the height of the overline
28185 (1 pixel) and a 1 pixel margin to the character height. */
28186 if (face->overline_p)
28187 it->ascent += overline_margin;
28189 if (it->constrain_row_ascent_descent_p)
28191 if (it->ascent > it->max_ascent)
28192 it->ascent = it->max_ascent;
28193 if (it->descent > it->max_descent)
28194 it->descent = it->max_descent;
28197 take_vertical_position_into_account (it);
28199 /* If we have to actually produce glyphs, do it. */
28200 if (it->glyph_row)
28202 if (stretched_p)
28204 /* Translate a space with a `space-width' property
28205 into a stretch glyph. */
28206 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28207 / FONT_HEIGHT (font));
28208 append_stretch_glyph (it, it->object, it->pixel_width,
28209 it->ascent + it->descent, ascent);
28211 else
28212 append_glyph (it);
28214 /* If characters with lbearing or rbearing are displayed
28215 in this line, record that fact in a flag of the
28216 glyph row. This is used to optimize X output code. */
28217 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28218 it->glyph_row->contains_overlapping_glyphs_p = true;
28220 if (! stretched_p && it->pixel_width == 0)
28221 /* We assure that all visible glyphs have at least 1-pixel
28222 width. */
28223 it->pixel_width = 1;
28225 else if (it->char_to_display == '\n')
28227 /* A newline has no width, but we need the height of the
28228 line. But if previous part of the line sets a height,
28229 don't increase that height. */
28231 Lisp_Object height;
28232 Lisp_Object total_height = Qnil;
28234 it->override_ascent = -1;
28235 it->pixel_width = 0;
28236 it->nglyphs = 0;
28238 height = get_it_property (it, Qline_height);
28239 /* Split (line-height total-height) list. */
28240 if (CONSP (height)
28241 && CONSP (XCDR (height))
28242 && NILP (XCDR (XCDR (height))))
28244 total_height = XCAR (XCDR (height));
28245 height = XCAR (height);
28247 height = calc_line_height_property (it, height, font, boff, true);
28249 if (it->override_ascent >= 0)
28251 it->ascent = it->override_ascent;
28252 it->descent = it->override_descent;
28253 boff = it->override_boff;
28255 else
28257 if (FONT_TOO_HIGH (font))
28259 it->ascent = font->pixel_size + boff - 1;
28260 it->descent = -boff + 1;
28261 if (it->descent < 0)
28262 it->descent = 0;
28264 else
28266 it->ascent = FONT_BASE (font) + boff;
28267 it->descent = FONT_DESCENT (font) - boff;
28271 if (EQ (height, Qt))
28273 if (it->descent > it->max_descent)
28275 it->ascent += it->descent - it->max_descent;
28276 it->descent = it->max_descent;
28278 if (it->ascent > it->max_ascent)
28280 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28281 it->ascent = it->max_ascent;
28283 it->phys_ascent = min (it->phys_ascent, it->ascent);
28284 it->phys_descent = min (it->phys_descent, it->descent);
28285 it->constrain_row_ascent_descent_p = true;
28286 extra_line_spacing = 0;
28288 else
28290 Lisp_Object spacing;
28292 it->phys_ascent = it->ascent;
28293 it->phys_descent = it->descent;
28295 if ((it->max_ascent > 0 || it->max_descent > 0)
28296 && face->box != FACE_NO_BOX
28297 && face->box_line_width > 0)
28299 it->ascent += face->box_line_width;
28300 it->descent += face->box_line_width;
28302 if (!NILP (height)
28303 && XINT (height) > it->ascent + it->descent)
28304 it->ascent = XINT (height) - it->descent;
28306 if (!NILP (total_height))
28307 spacing = calc_line_height_property (it, total_height, font,
28308 boff, false);
28309 else
28311 spacing = get_it_property (it, Qline_spacing);
28312 spacing = calc_line_height_property (it, spacing, font,
28313 boff, false);
28315 if (INTEGERP (spacing))
28317 extra_line_spacing = XINT (spacing);
28318 if (!NILP (total_height))
28319 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28323 else /* i.e. (it->char_to_display == '\t') */
28325 if (font->space_width > 0)
28327 int tab_width = it->tab_width * font->space_width;
28328 int x = it->current_x + it->continuation_lines_width;
28329 int x0 = x;
28330 /* Adjust for line numbers, if needed. */
28331 if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
28333 x -= it->lnum_pixel_width;
28334 /* Restore the original TAB width, if required. */
28335 if (x + it->tab_offset >= it->first_visible_x)
28336 x += it->tab_offset;
28339 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28341 /* If the distance from the current position to the next tab
28342 stop is less than a space character width, use the
28343 tab stop after that. */
28344 if (next_tab_x - x < font->space_width)
28345 next_tab_x += tab_width;
28346 if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
28348 next_tab_x += it->lnum_pixel_width;
28349 /* If the line is hscrolled, and the TAB starts before
28350 the first visible pixel, simulate negative row->x. */
28351 if (x < it->first_visible_x)
28353 next_tab_x -= it->first_visible_x - x;
28354 it->tab_offset = it->first_visible_x - x;
28356 else
28357 next_tab_x -= it->tab_offset;
28360 it->pixel_width = next_tab_x - x0;
28361 it->nglyphs = 1;
28362 if (FONT_TOO_HIGH (font))
28364 if (get_char_glyph_code (' ', font, &char2b))
28366 pcm = get_per_char_metric (font, &char2b);
28367 if (pcm->width == 0
28368 && pcm->rbearing == 0 && pcm->lbearing == 0)
28369 pcm = NULL;
28372 if (pcm)
28374 it->ascent = pcm->ascent + boff;
28375 it->descent = pcm->descent - boff;
28377 else
28379 it->ascent = font->pixel_size + boff - 1;
28380 it->descent = -boff + 1;
28382 if (it->ascent < 0)
28383 it->ascent = 0;
28384 if (it->descent < 0)
28385 it->descent = 0;
28387 else
28389 it->ascent = FONT_BASE (font) + boff;
28390 it->descent = FONT_DESCENT (font) - boff;
28392 it->phys_ascent = it->ascent;
28393 it->phys_descent = it->descent;
28395 if (it->glyph_row)
28397 append_stretch_glyph (it, it->object, it->pixel_width,
28398 it->ascent + it->descent, it->ascent);
28401 else
28403 it->pixel_width = 0;
28404 it->nglyphs = 1;
28408 if (FONT_TOO_HIGH (font))
28410 int font_ascent, font_descent;
28412 /* For very large fonts, where we ignore the declared font
28413 dimensions, and go by per-character metrics instead,
28414 don't let the row ascent and descent values (and the row
28415 height computed from them) be smaller than the "normal"
28416 character metrics. This avoids unpleasant effects
28417 whereby lines on display would change their height
28418 depending on which characters are shown. */
28419 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28420 it->max_ascent = max (it->max_ascent, font_ascent);
28421 it->max_descent = max (it->max_descent, font_descent);
28424 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28426 /* A static composition.
28428 Note: A composition is represented as one glyph in the
28429 glyph matrix. There are no padding glyphs.
28431 Important note: pixel_width, ascent, and descent are the
28432 values of what is drawn by draw_glyphs (i.e. the values of
28433 the overall glyphs composed). */
28434 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28435 int boff; /* baseline offset */
28436 struct composition *cmp = composition_table[it->cmp_it.id];
28437 int glyph_len = cmp->glyph_len;
28438 struct font *font = face->font;
28440 it->nglyphs = 1;
28442 /* If we have not yet calculated pixel size data of glyphs of
28443 the composition for the current face font, calculate them
28444 now. Theoretically, we have to check all fonts for the
28445 glyphs, but that requires much time and memory space. So,
28446 here we check only the font of the first glyph. This may
28447 lead to incorrect display, but it's very rare, and C-l
28448 (recenter-top-bottom) can correct the display anyway. */
28449 if (! cmp->font || cmp->font != font)
28451 /* Ascent and descent of the font of the first character
28452 of this composition (adjusted by baseline offset).
28453 Ascent and descent of overall glyphs should not be less
28454 than these, respectively. */
28455 int font_ascent, font_descent, font_height;
28456 /* Bounding box of the overall glyphs. */
28457 int leftmost, rightmost, lowest, highest;
28458 int lbearing, rbearing;
28459 int i, width, ascent, descent;
28460 int c;
28461 XChar2b char2b;
28462 struct font_metrics *pcm;
28463 ptrdiff_t pos;
28465 eassume (0 < glyph_len); /* See Bug#8512. */
28467 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28468 while (c == '\t' && 0 < --glyph_len);
28470 bool right_padded = glyph_len < cmp->glyph_len;
28471 for (i = 0; i < glyph_len; i++)
28473 c = COMPOSITION_GLYPH (cmp, i);
28474 if (c != '\t')
28475 break;
28476 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28478 bool left_padded = i > 0;
28480 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28481 : IT_CHARPOS (*it));
28482 /* If no suitable font is found, use the default font. */
28483 bool font_not_found_p = font == NULL;
28484 if (font_not_found_p)
28486 face = face->ascii_face;
28487 font = face->font;
28489 boff = font->baseline_offset;
28490 if (font->vertical_centering)
28491 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28492 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28493 font_ascent += boff;
28494 font_descent -= boff;
28495 font_height = font_ascent + font_descent;
28497 cmp->font = font;
28499 pcm = NULL;
28500 if (! font_not_found_p)
28502 get_char_face_and_encoding (it->f, c, it->face_id,
28503 &char2b, false);
28504 pcm = get_per_char_metric (font, &char2b);
28507 /* Initialize the bounding box. */
28508 if (pcm)
28510 width = cmp->glyph_len > 0 ? pcm->width : 0;
28511 ascent = pcm->ascent;
28512 descent = pcm->descent;
28513 lbearing = pcm->lbearing;
28514 rbearing = pcm->rbearing;
28516 else
28518 width = cmp->glyph_len > 0 ? font->space_width : 0;
28519 ascent = FONT_BASE (font);
28520 descent = FONT_DESCENT (font);
28521 lbearing = 0;
28522 rbearing = width;
28525 rightmost = width;
28526 leftmost = 0;
28527 lowest = - descent + boff;
28528 highest = ascent + boff;
28530 if (! font_not_found_p
28531 && font->default_ascent
28532 && CHAR_TABLE_P (Vuse_default_ascent)
28533 && !NILP (Faref (Vuse_default_ascent,
28534 make_number (it->char_to_display))))
28535 highest = font->default_ascent + boff;
28537 /* Draw the first glyph at the normal position. It may be
28538 shifted to right later if some other glyphs are drawn
28539 at the left. */
28540 cmp->offsets[i * 2] = 0;
28541 cmp->offsets[i * 2 + 1] = boff;
28542 cmp->lbearing = lbearing;
28543 cmp->rbearing = rbearing;
28545 /* Set cmp->offsets for the remaining glyphs. */
28546 for (i++; i < glyph_len; i++)
28548 int left, right, btm, top;
28549 int ch = COMPOSITION_GLYPH (cmp, i);
28550 int face_id;
28551 struct face *this_face;
28553 if (ch == '\t')
28554 ch = ' ';
28555 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28556 this_face = FACE_FROM_ID (it->f, face_id);
28557 font = this_face->font;
28559 if (font == NULL)
28560 pcm = NULL;
28561 else
28563 get_char_face_and_encoding (it->f, ch, face_id,
28564 &char2b, false);
28565 pcm = get_per_char_metric (font, &char2b);
28567 if (! pcm)
28568 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28569 else
28571 width = pcm->width;
28572 ascent = pcm->ascent;
28573 descent = pcm->descent;
28574 lbearing = pcm->lbearing;
28575 rbearing = pcm->rbearing;
28576 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28578 /* Relative composition with or without
28579 alternate chars. */
28580 left = (leftmost + rightmost - width) / 2;
28581 btm = - descent + boff;
28582 if (font->relative_compose
28583 && (! CHAR_TABLE_P (Vignore_relative_composition)
28584 || NILP (Faref (Vignore_relative_composition,
28585 make_number (ch)))))
28588 if (- descent >= font->relative_compose)
28589 /* One extra pixel between two glyphs. */
28590 btm = highest + 1;
28591 else if (ascent <= 0)
28592 /* One extra pixel between two glyphs. */
28593 btm = lowest - 1 - ascent - descent;
28596 else
28598 /* A composition rule is specified by an integer
28599 value that encodes global and new reference
28600 points (GREF and NREF). GREF and NREF are
28601 specified by numbers as below:
28603 0---1---2 -- ascent
28607 9--10--11 -- center
28609 ---3---4---5--- baseline
28611 6---7---8 -- descent
28613 int rule = COMPOSITION_RULE (cmp, i);
28614 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28616 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28617 grefx = gref % 3, nrefx = nref % 3;
28618 grefy = gref / 3, nrefy = nref / 3;
28619 if (xoff)
28620 xoff = font_height * (xoff - 128) / 256;
28621 if (yoff)
28622 yoff = font_height * (yoff - 128) / 256;
28624 left = (leftmost
28625 + grefx * (rightmost - leftmost) / 2
28626 - nrefx * width / 2
28627 + xoff);
28629 btm = ((grefy == 0 ? highest
28630 : grefy == 1 ? 0
28631 : grefy == 2 ? lowest
28632 : (highest + lowest) / 2)
28633 - (nrefy == 0 ? ascent + descent
28634 : nrefy == 1 ? descent - boff
28635 : nrefy == 2 ? 0
28636 : (ascent + descent) / 2)
28637 + yoff);
28640 cmp->offsets[i * 2] = left;
28641 cmp->offsets[i * 2 + 1] = btm + descent;
28643 /* Update the bounding box of the overall glyphs. */
28644 if (width > 0)
28646 right = left + width;
28647 if (left < leftmost)
28648 leftmost = left;
28649 if (right > rightmost)
28650 rightmost = right;
28652 top = btm + descent + ascent;
28653 if (top > highest)
28654 highest = top;
28655 if (btm < lowest)
28656 lowest = btm;
28658 if (cmp->lbearing > left + lbearing)
28659 cmp->lbearing = left + lbearing;
28660 if (cmp->rbearing < left + rbearing)
28661 cmp->rbearing = left + rbearing;
28665 /* If there are glyphs whose x-offsets are negative,
28666 shift all glyphs to the right and make all x-offsets
28667 non-negative. */
28668 if (leftmost < 0)
28670 for (i = 0; i < cmp->glyph_len; i++)
28671 cmp->offsets[i * 2] -= leftmost;
28672 rightmost -= leftmost;
28673 cmp->lbearing -= leftmost;
28674 cmp->rbearing -= leftmost;
28677 if (left_padded && cmp->lbearing < 0)
28679 for (i = 0; i < cmp->glyph_len; i++)
28680 cmp->offsets[i * 2] -= cmp->lbearing;
28681 rightmost -= cmp->lbearing;
28682 cmp->rbearing -= cmp->lbearing;
28683 cmp->lbearing = 0;
28685 if (right_padded && rightmost < cmp->rbearing)
28687 rightmost = cmp->rbearing;
28690 cmp->pixel_width = rightmost;
28691 cmp->ascent = highest;
28692 cmp->descent = - lowest;
28693 if (cmp->ascent < font_ascent)
28694 cmp->ascent = font_ascent;
28695 if (cmp->descent < font_descent)
28696 cmp->descent = font_descent;
28699 if (it->glyph_row
28700 && (cmp->lbearing < 0
28701 || cmp->rbearing > cmp->pixel_width))
28702 it->glyph_row->contains_overlapping_glyphs_p = true;
28704 it->pixel_width = cmp->pixel_width;
28705 it->ascent = it->phys_ascent = cmp->ascent;
28706 it->descent = it->phys_descent = cmp->descent;
28707 if (face->box != FACE_NO_BOX)
28709 int thick = face->box_line_width;
28711 if (thick > 0)
28713 it->ascent += thick;
28714 it->descent += thick;
28716 else
28717 thick = - thick;
28719 if (it->start_of_box_run_p)
28720 it->pixel_width += thick;
28721 if (it->end_of_box_run_p)
28722 it->pixel_width += thick;
28725 /* If face has an overline, add the height of the overline
28726 (1 pixel) and a 1 pixel margin to the character height. */
28727 if (face->overline_p)
28728 it->ascent += overline_margin;
28730 take_vertical_position_into_account (it);
28731 if (it->ascent < 0)
28732 it->ascent = 0;
28733 if (it->descent < 0)
28734 it->descent = 0;
28736 if (it->glyph_row && cmp->glyph_len > 0)
28737 append_composite_glyph (it);
28739 else if (it->what == IT_COMPOSITION)
28741 /* A dynamic (automatic) composition. */
28742 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28743 Lisp_Object gstring;
28744 struct font_metrics metrics;
28746 it->nglyphs = 1;
28748 gstring = composition_gstring_from_id (it->cmp_it.id);
28749 it->pixel_width
28750 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28751 &metrics);
28752 if (it->glyph_row
28753 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28754 it->glyph_row->contains_overlapping_glyphs_p = true;
28755 it->ascent = it->phys_ascent = metrics.ascent;
28756 it->descent = it->phys_descent = metrics.descent;
28757 if (face->box != FACE_NO_BOX)
28759 int thick = face->box_line_width;
28761 if (thick > 0)
28763 it->ascent += thick;
28764 it->descent += thick;
28766 else
28767 thick = - thick;
28769 if (it->start_of_box_run_p)
28770 it->pixel_width += thick;
28771 if (it->end_of_box_run_p)
28772 it->pixel_width += thick;
28774 /* If face has an overline, add the height of the overline
28775 (1 pixel) and a 1 pixel margin to the character height. */
28776 if (face->overline_p)
28777 it->ascent += overline_margin;
28778 take_vertical_position_into_account (it);
28779 if (it->ascent < 0)
28780 it->ascent = 0;
28781 if (it->descent < 0)
28782 it->descent = 0;
28784 if (it->glyph_row)
28785 append_composite_glyph (it);
28787 else if (it->what == IT_GLYPHLESS)
28788 produce_glyphless_glyph (it, false, Qnil);
28789 else if (it->what == IT_IMAGE)
28790 produce_image_glyph (it);
28791 else if (it->what == IT_STRETCH)
28792 produce_stretch_glyph (it);
28793 else if (it->what == IT_XWIDGET)
28794 produce_xwidget_glyph (it);
28796 done:
28797 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28798 because this isn't true for images with `:ascent 100'. */
28799 eassert (it->ascent >= 0 && it->descent >= 0);
28800 if (it->area == TEXT_AREA)
28801 it->current_x += it->pixel_width;
28803 if (extra_line_spacing > 0)
28805 it->descent += extra_line_spacing;
28806 if (extra_line_spacing > it->max_extra_line_spacing)
28807 it->max_extra_line_spacing = extra_line_spacing;
28810 it->max_ascent = max (it->max_ascent, it->ascent);
28811 it->max_descent = max (it->max_descent, it->descent);
28812 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28813 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28816 /* EXPORT for RIF:
28817 Output LEN glyphs starting at START at the nominal cursor position.
28818 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28819 being updated, and UPDATED_AREA is the area of that row being updated. */
28821 void
28822 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28823 struct glyph *start, enum glyph_row_area updated_area, int len)
28825 int x, hpos, chpos = w->phys_cursor.hpos;
28827 eassert (updated_row);
28828 /* When the window is hscrolled, cursor hpos can legitimately be out
28829 of bounds, but we draw the cursor at the corresponding window
28830 margin in that case. */
28831 if (!updated_row->reversed_p && chpos < 0)
28832 chpos = 0;
28833 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28834 chpos = updated_row->used[TEXT_AREA] - 1;
28836 block_input ();
28838 /* Write glyphs. */
28840 hpos = start - updated_row->glyphs[updated_area];
28841 x = draw_glyphs (w, w->output_cursor.x,
28842 updated_row, updated_area,
28843 hpos, hpos + len,
28844 DRAW_NORMAL_TEXT, 0);
28846 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28847 if (updated_area == TEXT_AREA
28848 && w->phys_cursor_on_p
28849 && w->phys_cursor.vpos == w->output_cursor.vpos
28850 && chpos >= hpos
28851 && chpos < hpos + len)
28852 w->phys_cursor_on_p = false;
28854 unblock_input ();
28856 /* Advance the output cursor. */
28857 w->output_cursor.hpos += len;
28858 w->output_cursor.x = x;
28862 /* EXPORT for RIF:
28863 Insert LEN glyphs from START at the nominal cursor position. */
28865 void
28866 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28867 struct glyph *start, enum glyph_row_area updated_area, int len)
28869 struct frame *f;
28870 int line_height, shift_by_width, shifted_region_width;
28871 struct glyph_row *row;
28872 struct glyph *glyph;
28873 int frame_x, frame_y;
28874 ptrdiff_t hpos;
28876 eassert (updated_row);
28877 block_input ();
28878 f = XFRAME (WINDOW_FRAME (w));
28880 /* Get the height of the line we are in. */
28881 row = updated_row;
28882 line_height = row->height;
28884 /* Get the width of the glyphs to insert. */
28885 shift_by_width = 0;
28886 for (glyph = start; glyph < start + len; ++glyph)
28887 shift_by_width += glyph->pixel_width;
28889 /* Get the width of the region to shift right. */
28890 shifted_region_width = (window_box_width (w, updated_area)
28891 - w->output_cursor.x
28892 - shift_by_width);
28894 /* Shift right. */
28895 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28896 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28898 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28899 line_height, shift_by_width);
28901 /* Write the glyphs. */
28902 hpos = start - row->glyphs[updated_area];
28903 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28904 hpos, hpos + len,
28905 DRAW_NORMAL_TEXT, 0);
28907 /* Advance the output cursor. */
28908 w->output_cursor.hpos += len;
28909 w->output_cursor.x += shift_by_width;
28910 unblock_input ();
28914 /* EXPORT for RIF:
28915 Erase the current text line from the nominal cursor position
28916 (inclusive) to pixel column TO_X (exclusive). The idea is that
28917 everything from TO_X onward is already erased.
28919 TO_X is a pixel position relative to UPDATED_AREA of currently
28920 updated window W. TO_X == -1 means clear to the end of this area. */
28922 void
28923 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28924 enum glyph_row_area updated_area, int to_x)
28926 struct frame *f;
28927 int max_x, min_y, max_y;
28928 int from_x, from_y, to_y;
28930 eassert (updated_row);
28931 f = XFRAME (w->frame);
28933 if (updated_row->full_width_p)
28934 max_x = (WINDOW_PIXEL_WIDTH (w)
28935 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28936 else
28937 max_x = window_box_width (w, updated_area);
28938 max_y = window_text_bottom_y (w);
28940 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28941 of window. For TO_X > 0, truncate to end of drawing area. */
28942 if (to_x == 0)
28943 return;
28944 else if (to_x < 0)
28945 to_x = max_x;
28946 else
28947 to_x = min (to_x, max_x);
28949 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28951 /* Notice if the cursor will be cleared by this operation. */
28952 if (!updated_row->full_width_p)
28953 notice_overwritten_cursor (w, updated_area,
28954 w->output_cursor.x, -1,
28955 updated_row->y,
28956 MATRIX_ROW_BOTTOM_Y (updated_row));
28958 from_x = w->output_cursor.x;
28960 /* Translate to frame coordinates. */
28961 if (updated_row->full_width_p)
28963 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28964 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28966 else
28968 int area_left = window_box_left (w, updated_area);
28969 from_x += area_left;
28970 to_x += area_left;
28973 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28974 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28975 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28977 /* Prevent inadvertently clearing to end of the X window. */
28978 if (to_x > from_x && to_y > from_y)
28980 block_input ();
28981 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28982 to_x - from_x, to_y - from_y);
28983 unblock_input ();
28987 #endif /* HAVE_WINDOW_SYSTEM */
28991 /***********************************************************************
28992 Cursor types
28993 ***********************************************************************/
28995 /* Value is the internal representation of the specified cursor type
28996 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28997 of the bar cursor. */
28999 static enum text_cursor_kinds
29000 get_specified_cursor_type (Lisp_Object arg, int *width)
29002 enum text_cursor_kinds type;
29004 if (NILP (arg))
29005 return NO_CURSOR;
29007 if (EQ (arg, Qbox))
29008 return FILLED_BOX_CURSOR;
29010 if (EQ (arg, Qhollow))
29011 return HOLLOW_BOX_CURSOR;
29013 if (EQ (arg, Qbar))
29015 *width = 2;
29016 return BAR_CURSOR;
29019 if (CONSP (arg)
29020 && EQ (XCAR (arg), Qbar)
29021 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
29023 *width = XINT (XCDR (arg));
29024 return BAR_CURSOR;
29027 if (EQ (arg, Qhbar))
29029 *width = 2;
29030 return HBAR_CURSOR;
29033 if (CONSP (arg)
29034 && EQ (XCAR (arg), Qhbar)
29035 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
29037 *width = XINT (XCDR (arg));
29038 return HBAR_CURSOR;
29041 /* Treat anything unknown as "hollow box cursor".
29042 It was bad to signal an error; people have trouble fixing
29043 .Xdefaults with Emacs, when it has something bad in it. */
29044 type = HOLLOW_BOX_CURSOR;
29046 return type;
29049 /* Set the default cursor types for specified frame. */
29050 void
29051 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
29053 int width = 1;
29054 Lisp_Object tem;
29056 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
29057 FRAME_CURSOR_WIDTH (f) = width;
29059 /* By default, set up the blink-off state depending on the on-state. */
29061 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
29062 if (!NILP (tem))
29064 FRAME_BLINK_OFF_CURSOR (f)
29065 = get_specified_cursor_type (XCDR (tem), &width);
29066 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
29068 else
29069 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
29071 /* Make sure the cursor gets redrawn. */
29072 f->cursor_type_changed = true;
29076 #ifdef HAVE_WINDOW_SYSTEM
29078 /* Return the cursor we want to be displayed in window W. Return
29079 width of bar/hbar cursor through WIDTH arg. Return with
29080 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
29081 (i.e. if the `system caret' should track this cursor).
29083 In a mini-buffer window, we want the cursor only to appear if we
29084 are reading input from this window. For the selected window, we
29085 want the cursor type given by the frame parameter or buffer local
29086 setting of cursor-type. If explicitly marked off, draw no cursor.
29087 In all other cases, we want a hollow box cursor. */
29089 static enum text_cursor_kinds
29090 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
29091 bool *active_cursor)
29093 struct frame *f = XFRAME (w->frame);
29094 struct buffer *b = XBUFFER (w->contents);
29095 int cursor_type = DEFAULT_CURSOR;
29096 Lisp_Object alt_cursor;
29097 bool non_selected = false;
29099 *active_cursor = true;
29101 /* Echo area */
29102 if (cursor_in_echo_area
29103 && FRAME_HAS_MINIBUF_P (f)
29104 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
29106 if (w == XWINDOW (echo_area_window))
29108 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
29110 *width = FRAME_CURSOR_WIDTH (f);
29111 return FRAME_DESIRED_CURSOR (f);
29113 else
29114 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29117 *active_cursor = false;
29118 non_selected = true;
29121 /* Detect a nonselected window or nonselected frame. */
29122 else if (w != XWINDOW (f->selected_window)
29123 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29125 *active_cursor = false;
29127 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29128 return NO_CURSOR;
29130 non_selected = true;
29133 /* Never display a cursor in a window in which cursor-type is nil. */
29134 if (NILP (BVAR (b, cursor_type)))
29135 return NO_CURSOR;
29137 /* Get the normal cursor type for this window. */
29138 if (EQ (BVAR (b, cursor_type), Qt))
29140 cursor_type = FRAME_DESIRED_CURSOR (f);
29141 *width = FRAME_CURSOR_WIDTH (f);
29143 else
29144 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29146 /* Use cursor-in-non-selected-windows instead
29147 for non-selected window or frame. */
29148 if (non_selected)
29150 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29151 if (!EQ (Qt, alt_cursor))
29152 return get_specified_cursor_type (alt_cursor, width);
29153 /* t means modify the normal cursor type. */
29154 if (cursor_type == FILLED_BOX_CURSOR)
29155 cursor_type = HOLLOW_BOX_CURSOR;
29156 else if (cursor_type == BAR_CURSOR && *width > 1)
29157 --*width;
29158 return cursor_type;
29161 /* Use normal cursor if not blinked off. */
29162 if (!w->cursor_off_p)
29164 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29165 return NO_CURSOR;
29166 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29168 if (cursor_type == FILLED_BOX_CURSOR)
29170 /* Using a block cursor on large images can be very annoying.
29171 So use a hollow cursor for "large" images.
29172 If image is not transparent (no mask), also use hollow cursor. */
29173 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29174 if (img != NULL && IMAGEP (img->spec))
29176 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29177 where N = size of default frame font size.
29178 This should cover most of the "tiny" icons people may use. */
29179 if (!img->mask
29180 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29181 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29182 cursor_type = HOLLOW_BOX_CURSOR;
29185 else if (cursor_type != NO_CURSOR)
29187 /* Display current only supports BOX and HOLLOW cursors for images.
29188 So for now, unconditionally use a HOLLOW cursor when cursor is
29189 not a solid box cursor. */
29190 cursor_type = HOLLOW_BOX_CURSOR;
29193 return cursor_type;
29196 /* Cursor is blinked off, so determine how to "toggle" it. */
29198 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29199 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29200 return get_specified_cursor_type (XCDR (alt_cursor), width);
29202 /* Then see if frame has specified a specific blink off cursor type. */
29203 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29205 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29206 return FRAME_BLINK_OFF_CURSOR (f);
29209 #if false
29210 /* Some people liked having a permanently visible blinking cursor,
29211 while others had very strong opinions against it. So it was
29212 decided to remove it. KFS 2003-09-03 */
29214 /* Finally perform built-in cursor blinking:
29215 filled box <-> hollow box
29216 wide [h]bar <-> narrow [h]bar
29217 narrow [h]bar <-> no cursor
29218 other type <-> no cursor */
29220 if (cursor_type == FILLED_BOX_CURSOR)
29221 return HOLLOW_BOX_CURSOR;
29223 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29225 *width = 1;
29226 return cursor_type;
29228 #endif
29230 return NO_CURSOR;
29234 /* Notice when the text cursor of window W has been completely
29235 overwritten by a drawing operation that outputs glyphs in AREA
29236 starting at X0 and ending at X1 in the line starting at Y0 and
29237 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29238 the rest of the line after X0 has been written. Y coordinates
29239 are window-relative. */
29241 static void
29242 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29243 int x0, int x1, int y0, int y1)
29245 int cx0, cx1, cy0, cy1;
29246 struct glyph_row *row;
29248 if (!w->phys_cursor_on_p)
29249 return;
29250 if (area != TEXT_AREA)
29251 return;
29253 if (w->phys_cursor.vpos < 0
29254 || w->phys_cursor.vpos >= w->current_matrix->nrows
29255 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29256 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29257 return;
29259 if (row->cursor_in_fringe_p)
29261 row->cursor_in_fringe_p = false;
29262 draw_fringe_bitmap (w, row, row->reversed_p);
29263 w->phys_cursor_on_p = false;
29264 return;
29267 cx0 = w->phys_cursor.x;
29268 cx1 = cx0 + w->phys_cursor_width;
29269 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29270 return;
29272 /* The cursor image will be completely removed from the
29273 screen if the output area intersects the cursor area in
29274 y-direction. When we draw in [y0 y1[, and some part of
29275 the cursor is at y < y0, that part must have been drawn
29276 before. When scrolling, the cursor is erased before
29277 actually scrolling, so we don't come here. When not
29278 scrolling, the rows above the old cursor row must have
29279 changed, and in this case these rows must have written
29280 over the cursor image.
29282 Likewise if part of the cursor is below y1, with the
29283 exception of the cursor being in the first blank row at
29284 the buffer and window end because update_text_area
29285 doesn't draw that row. (Except when it does, but
29286 that's handled in update_text_area.) */
29288 cy0 = w->phys_cursor.y;
29289 cy1 = cy0 + w->phys_cursor_height;
29290 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29291 return;
29293 w->phys_cursor_on_p = false;
29296 #endif /* HAVE_WINDOW_SYSTEM */
29299 /************************************************************************
29300 Mouse Face
29301 ************************************************************************/
29303 #ifdef HAVE_WINDOW_SYSTEM
29305 /* EXPORT for RIF:
29306 Fix the display of area AREA of overlapping row ROW in window W
29307 with respect to the overlapping part OVERLAPS. */
29309 void
29310 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29311 enum glyph_row_area area, int overlaps)
29313 int i, x;
29315 block_input ();
29317 x = 0;
29318 for (i = 0; i < row->used[area];)
29320 if (row->glyphs[area][i].overlaps_vertically_p)
29322 int start = i, start_x = x;
29326 x += row->glyphs[area][i].pixel_width;
29327 ++i;
29329 while (i < row->used[area]
29330 && row->glyphs[area][i].overlaps_vertically_p);
29332 draw_glyphs (w, start_x, row, area,
29333 start, i,
29334 DRAW_NORMAL_TEXT, overlaps);
29336 else
29338 x += row->glyphs[area][i].pixel_width;
29339 ++i;
29343 unblock_input ();
29347 /* EXPORT:
29348 Draw the cursor glyph of window W in glyph row ROW. See the
29349 comment of draw_glyphs for the meaning of HL. */
29351 void
29352 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29353 enum draw_glyphs_face hl)
29355 /* If cursor hpos is out of bounds, don't draw garbage. This can
29356 happen in mini-buffer windows when switching between echo area
29357 glyphs and mini-buffer. */
29358 if ((row->reversed_p
29359 ? (w->phys_cursor.hpos >= 0)
29360 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29362 bool on_p = w->phys_cursor_on_p;
29363 int x1;
29364 int hpos = w->phys_cursor.hpos;
29366 /* When the window is hscrolled, cursor hpos can legitimately be
29367 out of bounds, but we draw the cursor at the corresponding
29368 window margin in that case. */
29369 if (!row->reversed_p && hpos < 0)
29370 hpos = 0;
29371 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29372 hpos = row->used[TEXT_AREA] - 1;
29374 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29375 hl, 0);
29376 w->phys_cursor_on_p = on_p;
29378 if (hl == DRAW_CURSOR)
29379 w->phys_cursor_width = x1 - w->phys_cursor.x;
29380 /* When we erase the cursor, and ROW is overlapped by other
29381 rows, make sure that these overlapping parts of other rows
29382 are redrawn. */
29383 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29385 w->phys_cursor_width = x1 - w->phys_cursor.x;
29387 if (row > w->current_matrix->rows
29388 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29389 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29390 OVERLAPS_ERASED_CURSOR);
29392 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29393 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29394 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29395 OVERLAPS_ERASED_CURSOR);
29401 /* Erase the image of a cursor of window W from the screen. */
29403 void
29404 erase_phys_cursor (struct window *w)
29406 struct frame *f = XFRAME (w->frame);
29407 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29408 int hpos = w->phys_cursor.hpos;
29409 int vpos = w->phys_cursor.vpos;
29410 bool mouse_face_here_p = false;
29411 struct glyph_matrix *active_glyphs = w->current_matrix;
29412 struct glyph_row *cursor_row;
29413 struct glyph *cursor_glyph;
29414 enum draw_glyphs_face hl;
29416 /* No cursor displayed or row invalidated => nothing to do on the
29417 screen. */
29418 if (w->phys_cursor_type == NO_CURSOR)
29419 goto mark_cursor_off;
29421 /* VPOS >= active_glyphs->nrows means that window has been resized.
29422 Don't bother to erase the cursor. */
29423 if (vpos >= active_glyphs->nrows)
29424 goto mark_cursor_off;
29426 /* If row containing cursor is marked invalid, there is nothing we
29427 can do. */
29428 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29429 if (!cursor_row->enabled_p)
29430 goto mark_cursor_off;
29432 /* If line spacing is > 0, old cursor may only be partially visible in
29433 window after split-window. So adjust visible height. */
29434 cursor_row->visible_height = min (cursor_row->visible_height,
29435 window_text_bottom_y (w) - cursor_row->y);
29437 /* If row is completely invisible, don't attempt to delete a cursor which
29438 isn't there. This can happen if cursor is at top of a window, and
29439 we switch to a buffer with a header line in that window. */
29440 if (cursor_row->visible_height <= 0)
29441 goto mark_cursor_off;
29443 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29444 if (cursor_row->cursor_in_fringe_p)
29446 cursor_row->cursor_in_fringe_p = false;
29447 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29448 goto mark_cursor_off;
29451 /* This can happen when the new row is shorter than the old one.
29452 In this case, either draw_glyphs or clear_end_of_line
29453 should have cleared the cursor. Note that we wouldn't be
29454 able to erase the cursor in this case because we don't have a
29455 cursor glyph at hand. */
29456 if ((cursor_row->reversed_p
29457 ? (w->phys_cursor.hpos < 0)
29458 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29459 goto mark_cursor_off;
29461 /* When the window is hscrolled, cursor hpos can legitimately be out
29462 of bounds, but we draw the cursor at the corresponding window
29463 margin in that case. */
29464 if (!cursor_row->reversed_p && hpos < 0)
29465 hpos = 0;
29466 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29467 hpos = cursor_row->used[TEXT_AREA] - 1;
29469 /* If the cursor is in the mouse face area, redisplay that when
29470 we clear the cursor. */
29471 if (! NILP (hlinfo->mouse_face_window)
29472 && coords_in_mouse_face_p (w, hpos, vpos)
29473 /* Don't redraw the cursor's spot in mouse face if it is at the
29474 end of a line (on a newline). The cursor appears there, but
29475 mouse highlighting does not. */
29476 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29477 mouse_face_here_p = true;
29479 /* Maybe clear the display under the cursor. */
29480 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29482 int x, y;
29483 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29484 int width;
29486 cursor_glyph = get_phys_cursor_glyph (w);
29487 if (cursor_glyph == NULL)
29488 goto mark_cursor_off;
29490 width = cursor_glyph->pixel_width;
29491 x = w->phys_cursor.x;
29492 if (x < 0)
29494 width += x;
29495 x = 0;
29497 width = min (width, window_box_width (w, TEXT_AREA) - x);
29498 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29499 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29501 if (width > 0)
29502 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29505 /* Erase the cursor by redrawing the character underneath it. */
29506 if (mouse_face_here_p)
29507 hl = DRAW_MOUSE_FACE;
29508 else
29509 hl = DRAW_NORMAL_TEXT;
29510 draw_phys_cursor_glyph (w, cursor_row, hl);
29512 mark_cursor_off:
29513 w->phys_cursor_on_p = false;
29514 w->phys_cursor_type = NO_CURSOR;
29518 /* Display or clear cursor of window W. If !ON, clear the cursor.
29519 If ON, display the cursor; where to put the cursor is specified by
29520 HPOS, VPOS, X and Y. */
29522 void
29523 display_and_set_cursor (struct window *w, bool on,
29524 int hpos, int vpos, int x, int y)
29526 struct frame *f = XFRAME (w->frame);
29527 int new_cursor_type;
29528 int new_cursor_width;
29529 bool active_cursor;
29530 struct glyph_row *glyph_row;
29531 struct glyph *glyph;
29533 /* This is pointless on invisible frames, and dangerous on garbaged
29534 windows and frames; in the latter case, the frame or window may
29535 be in the midst of changing its size, and x and y may be off the
29536 window. */
29537 if (! FRAME_VISIBLE_P (f)
29538 || vpos >= w->current_matrix->nrows
29539 || hpos >= w->current_matrix->matrix_w)
29540 return;
29542 /* If cursor is off and we want it off, return quickly. */
29543 if (!on && !w->phys_cursor_on_p)
29544 return;
29546 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29547 /* If cursor row is not enabled, we don't really know where to
29548 display the cursor. */
29549 if (!glyph_row->enabled_p)
29551 w->phys_cursor_on_p = false;
29552 return;
29555 /* A frame might be marked garbaged even though its cursor position
29556 is correct, and will not change upon subsequent redisplay. This
29557 happens in some rare situations, like toggling the sort order in
29558 Dired windows. We've already established that VPOS is valid, so
29559 it shouldn't do any harm to record the cursor position, as we are
29560 going to return without acting on it anyway. Otherwise, expose
29561 events might come in and call update_window_cursor, which will
29562 blindly use outdated values in w->phys_cursor. */
29563 if (FRAME_GARBAGED_P (f))
29565 if (on)
29567 w->phys_cursor.x = x;
29568 w->phys_cursor.y = glyph_row->y;
29569 w->phys_cursor.hpos = hpos;
29570 w->phys_cursor.vpos = vpos;
29572 return;
29575 glyph = NULL;
29576 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29577 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29579 eassert (input_blocked_p ());
29581 /* Set new_cursor_type to the cursor we want to be displayed. */
29582 new_cursor_type = get_window_cursor_type (w, glyph,
29583 &new_cursor_width, &active_cursor);
29585 /* If cursor is currently being shown and we don't want it to be or
29586 it is in the wrong place, or the cursor type is not what we want,
29587 erase it. */
29588 if (w->phys_cursor_on_p
29589 && (!on
29590 || w->phys_cursor.x != x
29591 || w->phys_cursor.y != y
29592 /* HPOS can be negative in R2L rows whose
29593 exact_window_width_line_p flag is set (i.e. their newline
29594 would "overflow into the fringe"). */
29595 || hpos < 0
29596 || new_cursor_type != w->phys_cursor_type
29597 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29598 && new_cursor_width != w->phys_cursor_width)))
29599 erase_phys_cursor (w);
29601 /* Don't check phys_cursor_on_p here because that flag is only set
29602 to false in some cases where we know that the cursor has been
29603 completely erased, to avoid the extra work of erasing the cursor
29604 twice. In other words, phys_cursor_on_p can be true and the cursor
29605 still not be visible, or it has only been partly erased. */
29606 if (on)
29608 w->phys_cursor_ascent = glyph_row->ascent;
29609 w->phys_cursor_height = glyph_row->height;
29611 /* Set phys_cursor_.* before x_draw_.* is called because some
29612 of them may need the information. */
29613 w->phys_cursor.x = x;
29614 w->phys_cursor.y = glyph_row->y;
29615 w->phys_cursor.hpos = hpos;
29616 w->phys_cursor.vpos = vpos;
29619 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29620 new_cursor_type, new_cursor_width,
29621 on, active_cursor);
29625 /* Switch the display of W's cursor on or off, according to the value
29626 of ON. */
29628 static void
29629 update_window_cursor (struct window *w, bool on)
29631 /* Don't update cursor in windows whose frame is in the process
29632 of being deleted. */
29633 if (w->current_matrix)
29635 int hpos = w->phys_cursor.hpos;
29636 int vpos = w->phys_cursor.vpos;
29637 struct glyph_row *row;
29639 if (vpos >= w->current_matrix->nrows
29640 || hpos >= w->current_matrix->matrix_w)
29641 return;
29643 row = MATRIX_ROW (w->current_matrix, vpos);
29645 /* When the window is hscrolled, cursor hpos can legitimately be
29646 out of bounds, but we draw the cursor at the corresponding
29647 window margin in that case. */
29648 if (!row->reversed_p && hpos < 0)
29649 hpos = 0;
29650 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29651 hpos = row->used[TEXT_AREA] - 1;
29653 block_input ();
29654 display_and_set_cursor (w, on, hpos, vpos,
29655 w->phys_cursor.x, w->phys_cursor.y);
29656 unblock_input ();
29661 /* Call update_window_cursor with parameter ON_P on all leaf windows
29662 in the window tree rooted at W. */
29664 static void
29665 update_cursor_in_window_tree (struct window *w, bool on_p)
29667 while (w)
29669 if (WINDOWP (w->contents))
29670 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29671 else
29672 update_window_cursor (w, on_p);
29674 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29679 /* EXPORT:
29680 Display the cursor on window W, or clear it, according to ON_P.
29681 Don't change the cursor's position. */
29683 void
29684 x_update_cursor (struct frame *f, bool on_p)
29686 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29690 /* EXPORT:
29691 Clear the cursor of window W to background color, and mark the
29692 cursor as not shown. This is used when the text where the cursor
29693 is about to be rewritten. */
29695 void
29696 x_clear_cursor (struct window *w)
29698 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29699 update_window_cursor (w, false);
29702 #endif /* HAVE_WINDOW_SYSTEM */
29704 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29705 and MSDOS. */
29706 static void
29707 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29708 int start_hpos, int end_hpos,
29709 enum draw_glyphs_face draw)
29711 #ifdef HAVE_WINDOW_SYSTEM
29712 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29714 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29715 return;
29717 #endif
29718 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29719 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29720 #endif
29723 /* Display the active region described by mouse_face_* according to DRAW. */
29725 static void
29726 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29728 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29729 struct frame *f = XFRAME (WINDOW_FRAME (w));
29731 if (/* If window is in the process of being destroyed, don't bother
29732 to do anything. */
29733 w->current_matrix != NULL
29734 /* Don't update mouse highlight if hidden. */
29735 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29736 /* Recognize when we are called to operate on rows that don't exist
29737 anymore. This can happen when a window is split. */
29738 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29740 bool phys_cursor_on_p = w->phys_cursor_on_p;
29741 struct glyph_row *row, *first, *last;
29743 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29744 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29746 for (row = first; row <= last && row->enabled_p; ++row)
29748 int start_hpos, end_hpos, start_x;
29750 /* For all but the first row, the highlight starts at column 0. */
29751 if (row == first)
29753 /* R2L rows have BEG and END in reversed order, but the
29754 screen drawing geometry is always left to right. So
29755 we need to mirror the beginning and end of the
29756 highlighted area in R2L rows. */
29757 if (!row->reversed_p)
29759 start_hpos = hlinfo->mouse_face_beg_col;
29760 start_x = hlinfo->mouse_face_beg_x;
29762 else if (row == last)
29764 start_hpos = hlinfo->mouse_face_end_col;
29765 start_x = hlinfo->mouse_face_end_x;
29767 else
29769 start_hpos = 0;
29770 start_x = 0;
29773 else if (row->reversed_p && row == last)
29775 start_hpos = hlinfo->mouse_face_end_col;
29776 start_x = hlinfo->mouse_face_end_x;
29778 else
29780 start_hpos = 0;
29781 start_x = 0;
29784 if (row == last)
29786 if (!row->reversed_p)
29787 end_hpos = hlinfo->mouse_face_end_col;
29788 else if (row == first)
29789 end_hpos = hlinfo->mouse_face_beg_col;
29790 else
29792 end_hpos = row->used[TEXT_AREA];
29793 if (draw == DRAW_NORMAL_TEXT)
29794 row->fill_line_p = true; /* Clear to end of line. */
29797 else if (row->reversed_p && row == first)
29798 end_hpos = hlinfo->mouse_face_beg_col;
29799 else
29801 end_hpos = row->used[TEXT_AREA];
29802 if (draw == DRAW_NORMAL_TEXT)
29803 row->fill_line_p = true; /* Clear to end of line. */
29806 if (end_hpos > start_hpos)
29808 draw_row_with_mouse_face (w, start_x, row,
29809 start_hpos, end_hpos, draw);
29811 row->mouse_face_p
29812 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29816 /* When we've written over the cursor, arrange for it to
29817 be displayed again. */
29818 if (FRAME_WINDOW_P (f)
29819 && phys_cursor_on_p && !w->phys_cursor_on_p)
29821 #ifdef HAVE_WINDOW_SYSTEM
29822 int hpos = w->phys_cursor.hpos;
29824 /* When the window is hscrolled, cursor hpos can legitimately be
29825 out of bounds, but we draw the cursor at the corresponding
29826 window margin in that case. */
29827 if (!row->reversed_p && hpos < 0)
29828 hpos = 0;
29829 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29830 hpos = row->used[TEXT_AREA] - 1;
29832 block_input ();
29833 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29834 w->phys_cursor.x, w->phys_cursor.y);
29835 unblock_input ();
29836 #endif /* HAVE_WINDOW_SYSTEM */
29840 #ifdef HAVE_WINDOW_SYSTEM
29841 /* Change the mouse cursor. */
29842 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29844 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29845 if (draw == DRAW_NORMAL_TEXT
29846 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29847 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29848 else
29849 #endif
29850 if (draw == DRAW_MOUSE_FACE)
29851 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29852 else
29853 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29855 #endif /* HAVE_WINDOW_SYSTEM */
29858 /* EXPORT:
29859 Clear out the mouse-highlighted active region.
29860 Redraw it un-highlighted first. Value is true if mouse
29861 face was actually drawn unhighlighted. */
29863 bool
29864 clear_mouse_face (Mouse_HLInfo *hlinfo)
29866 bool cleared
29867 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29868 if (cleared)
29869 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29870 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29871 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29872 hlinfo->mouse_face_window = Qnil;
29873 hlinfo->mouse_face_overlay = Qnil;
29874 return cleared;
29877 /* Return true if the coordinates HPOS and VPOS on windows W are
29878 within the mouse face on that window. */
29879 static bool
29880 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29882 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29884 /* Quickly resolve the easy cases. */
29885 if (!(WINDOWP (hlinfo->mouse_face_window)
29886 && XWINDOW (hlinfo->mouse_face_window) == w))
29887 return false;
29888 if (vpos < hlinfo->mouse_face_beg_row
29889 || vpos > hlinfo->mouse_face_end_row)
29890 return false;
29891 if (vpos > hlinfo->mouse_face_beg_row
29892 && vpos < hlinfo->mouse_face_end_row)
29893 return true;
29895 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29897 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29899 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29900 return true;
29902 else if ((vpos == hlinfo->mouse_face_beg_row
29903 && hpos >= hlinfo->mouse_face_beg_col)
29904 || (vpos == hlinfo->mouse_face_end_row
29905 && hpos < hlinfo->mouse_face_end_col))
29906 return true;
29908 else
29910 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29912 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29913 return true;
29915 else if ((vpos == hlinfo->mouse_face_beg_row
29916 && hpos <= hlinfo->mouse_face_beg_col)
29917 || (vpos == hlinfo->mouse_face_end_row
29918 && hpos > hlinfo->mouse_face_end_col))
29919 return true;
29921 return false;
29925 /* EXPORT:
29926 True if physical cursor of window W is within mouse face. */
29928 bool
29929 cursor_in_mouse_face_p (struct window *w)
29931 int hpos = w->phys_cursor.hpos;
29932 int vpos = w->phys_cursor.vpos;
29933 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29935 /* When the window is hscrolled, cursor hpos can legitimately be out
29936 of bounds, but we draw the cursor at the corresponding window
29937 margin in that case. */
29938 if (!row->reversed_p && hpos < 0)
29939 hpos = 0;
29940 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29941 hpos = row->used[TEXT_AREA] - 1;
29943 return coords_in_mouse_face_p (w, hpos, vpos);
29948 /* Find the glyph rows START_ROW and END_ROW of window W that display
29949 characters between buffer positions START_CHARPOS and END_CHARPOS
29950 (excluding END_CHARPOS). DISP_STRING is a display string that
29951 covers these buffer positions. This is similar to
29952 row_containing_pos, but is more accurate when bidi reordering makes
29953 buffer positions change non-linearly with glyph rows. */
29954 static void
29955 rows_from_pos_range (struct window *w,
29956 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29957 Lisp_Object disp_string,
29958 struct glyph_row **start, struct glyph_row **end)
29960 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29961 int last_y = window_text_bottom_y (w);
29962 struct glyph_row *row;
29964 *start = NULL;
29965 *end = NULL;
29967 while (!first->enabled_p
29968 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29969 first++;
29971 /* Find the START row. */
29972 for (row = first;
29973 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29974 row++)
29976 /* A row can potentially be the START row if the range of the
29977 characters it displays intersects the range
29978 [START_CHARPOS..END_CHARPOS). */
29979 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29980 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29981 /* See the commentary in row_containing_pos, for the
29982 explanation of the complicated way to check whether
29983 some position is beyond the end of the characters
29984 displayed by a row. */
29985 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29986 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29987 && !row->ends_at_zv_p
29988 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29989 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29990 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29991 && !row->ends_at_zv_p
29992 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29994 /* Found a candidate row. Now make sure at least one of the
29995 glyphs it displays has a charpos from the range
29996 [START_CHARPOS..END_CHARPOS).
29998 This is not obvious because bidi reordering could make
29999 buffer positions of a row be 1,2,3,102,101,100, and if we
30000 want to highlight characters in [50..60), we don't want
30001 this row, even though [50..60) does intersect [1..103),
30002 the range of character positions given by the row's start
30003 and end positions. */
30004 struct glyph *g = row->glyphs[TEXT_AREA];
30005 struct glyph *e = g + row->used[TEXT_AREA];
30007 while (g < e)
30009 if (((BUFFERP (g->object) || NILP (g->object))
30010 && start_charpos <= g->charpos && g->charpos < end_charpos)
30011 /* A glyph that comes from DISP_STRING is by
30012 definition to be highlighted. */
30013 || EQ (g->object, disp_string))
30014 *start = row;
30015 g++;
30017 if (*start)
30018 break;
30022 /* Find the END row. */
30023 if (!*start
30024 /* If the last row is partially visible, start looking for END
30025 from that row, instead of starting from FIRST. */
30026 && !(row->enabled_p
30027 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
30028 row = first;
30029 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
30031 struct glyph_row *next = row + 1;
30032 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
30034 if (!next->enabled_p
30035 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
30036 /* The first row >= START whose range of displayed characters
30037 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
30038 is the row END + 1. */
30039 || (start_charpos < next_start
30040 && end_charpos < next_start)
30041 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
30042 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
30043 && !next->ends_at_zv_p
30044 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
30045 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
30046 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
30047 && !next->ends_at_zv_p
30048 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
30050 *end = row;
30051 break;
30053 else
30055 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
30056 but none of the characters it displays are in the range, it is
30057 also END + 1. */
30058 struct glyph *g = next->glyphs[TEXT_AREA];
30059 struct glyph *s = g;
30060 struct glyph *e = g + next->used[TEXT_AREA];
30062 while (g < e)
30064 if (((BUFFERP (g->object) || NILP (g->object))
30065 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
30066 /* If the buffer position of the first glyph in
30067 the row is equal to END_CHARPOS, it means
30068 the last character to be highlighted is the
30069 newline of ROW, and we must consider NEXT as
30070 END, not END+1. */
30071 || (((!next->reversed_p && g == s)
30072 || (next->reversed_p && g == e - 1))
30073 && (g->charpos == end_charpos
30074 /* Special case for when NEXT is an
30075 empty line at ZV. */
30076 || (g->charpos == -1
30077 && !row->ends_at_zv_p
30078 && next_start == end_charpos)))))
30079 /* A glyph that comes from DISP_STRING is by
30080 definition to be highlighted. */
30081 || EQ (g->object, disp_string))
30082 break;
30083 g++;
30085 if (g == e)
30087 *end = row;
30088 break;
30090 /* The first row that ends at ZV must be the last to be
30091 highlighted. */
30092 else if (next->ends_at_zv_p)
30094 *end = next;
30095 break;
30101 /* This function sets the mouse_face_* elements of HLINFO, assuming
30102 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
30103 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
30104 for the overlay or run of text properties specifying the mouse
30105 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
30106 before-string and after-string that must also be highlighted.
30107 DISP_STRING, if non-nil, is a display string that may cover some
30108 or all of the highlighted text. */
30110 static void
30111 mouse_face_from_buffer_pos (Lisp_Object window,
30112 Mouse_HLInfo *hlinfo,
30113 ptrdiff_t mouse_charpos,
30114 ptrdiff_t start_charpos,
30115 ptrdiff_t end_charpos,
30116 Lisp_Object before_string,
30117 Lisp_Object after_string,
30118 Lisp_Object disp_string)
30120 struct window *w = XWINDOW (window);
30121 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30122 struct glyph_row *r1, *r2;
30123 struct glyph *glyph, *end;
30124 ptrdiff_t ignore, pos;
30125 int x;
30127 eassert (NILP (disp_string) || STRINGP (disp_string));
30128 eassert (NILP (before_string) || STRINGP (before_string));
30129 eassert (NILP (after_string) || STRINGP (after_string));
30131 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30132 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30133 if (r1 == NULL)
30134 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30135 /* If the before-string or display-string contains newlines,
30136 rows_from_pos_range skips to its last row. Move back. */
30137 if (!NILP (before_string) || !NILP (disp_string))
30139 struct glyph_row *prev;
30140 while ((prev = r1 - 1, prev >= first)
30141 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30142 && prev->used[TEXT_AREA] > 0)
30144 struct glyph *beg = prev->glyphs[TEXT_AREA];
30145 glyph = beg + prev->used[TEXT_AREA];
30146 while (--glyph >= beg && NILP (glyph->object));
30147 if (glyph < beg
30148 || !(EQ (glyph->object, before_string)
30149 || EQ (glyph->object, disp_string)))
30150 break;
30151 r1 = prev;
30154 if (r2 == NULL)
30156 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30157 hlinfo->mouse_face_past_end = true;
30159 else if (!NILP (after_string))
30161 /* If the after-string has newlines, advance to its last row. */
30162 struct glyph_row *next;
30163 struct glyph_row *last
30164 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30166 for (next = r2 + 1;
30167 next <= last
30168 && next->used[TEXT_AREA] > 0
30169 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30170 ++next)
30171 r2 = next;
30173 /* The rest of the display engine assumes that mouse_face_beg_row is
30174 either above mouse_face_end_row or identical to it. But with
30175 bidi-reordered continued lines, the row for START_CHARPOS could
30176 be below the row for END_CHARPOS. If so, swap the rows and store
30177 them in correct order. */
30178 if (r1->y > r2->y)
30180 struct glyph_row *tem = r2;
30182 r2 = r1;
30183 r1 = tem;
30186 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30187 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30189 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30190 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30191 could be anywhere in the row and in any order. The strategy
30192 below is to find the leftmost and the rightmost glyph that
30193 belongs to either of these 3 strings, or whose position is
30194 between START_CHARPOS and END_CHARPOS, and highlight all the
30195 glyphs between those two. This may cover more than just the text
30196 between START_CHARPOS and END_CHARPOS if the range of characters
30197 strides the bidi level boundary, e.g. if the beginning is in R2L
30198 text while the end is in L2R text or vice versa. */
30199 if (!r1->reversed_p)
30201 /* This row is in a left to right paragraph. Scan it left to
30202 right. */
30203 glyph = r1->glyphs[TEXT_AREA];
30204 end = glyph + r1->used[TEXT_AREA];
30205 x = r1->x;
30207 /* Skip truncation glyphs at the start of the glyph row. */
30208 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30209 for (; glyph < end
30210 && NILP (glyph->object)
30211 && glyph->charpos < 0;
30212 ++glyph)
30213 x += glyph->pixel_width;
30215 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30216 or DISP_STRING, and the first glyph from buffer whose
30217 position is between START_CHARPOS and END_CHARPOS. */
30218 for (; glyph < end
30219 && !NILP (glyph->object)
30220 && !EQ (glyph->object, disp_string)
30221 && !(BUFFERP (glyph->object)
30222 && (glyph->charpos >= start_charpos
30223 && glyph->charpos < end_charpos));
30224 ++glyph)
30226 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30227 are present at buffer positions between START_CHARPOS and
30228 END_CHARPOS, or if they come from an overlay. */
30229 if (EQ (glyph->object, before_string))
30231 pos = string_buffer_position (before_string,
30232 start_charpos);
30233 /* If pos == 0, it means before_string came from an
30234 overlay, not from a buffer position. */
30235 if (!pos || (pos >= start_charpos && pos < end_charpos))
30236 break;
30238 else if (EQ (glyph->object, after_string))
30240 pos = string_buffer_position (after_string, end_charpos);
30241 if (!pos || (pos >= start_charpos && pos < end_charpos))
30242 break;
30244 x += glyph->pixel_width;
30246 hlinfo->mouse_face_beg_x = x;
30247 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30249 else
30251 /* This row is in a right to left paragraph. Scan it right to
30252 left. */
30253 struct glyph *g;
30255 end = r1->glyphs[TEXT_AREA] - 1;
30256 glyph = end + r1->used[TEXT_AREA];
30258 /* Skip truncation glyphs at the start of the glyph row. */
30259 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30260 for (; glyph > end
30261 && NILP (glyph->object)
30262 && glyph->charpos < 0;
30263 --glyph)
30266 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30267 or DISP_STRING, and the first glyph from buffer whose
30268 position is between START_CHARPOS and END_CHARPOS. */
30269 for (; glyph > end
30270 && !NILP (glyph->object)
30271 && !EQ (glyph->object, disp_string)
30272 && !(BUFFERP (glyph->object)
30273 && (glyph->charpos >= start_charpos
30274 && glyph->charpos < end_charpos));
30275 --glyph)
30277 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30278 are present at buffer positions between START_CHARPOS and
30279 END_CHARPOS, or if they come from an overlay. */
30280 if (EQ (glyph->object, before_string))
30282 pos = string_buffer_position (before_string, start_charpos);
30283 /* If pos == 0, it means before_string came from an
30284 overlay, not from a buffer position. */
30285 if (!pos || (pos >= start_charpos && pos < end_charpos))
30286 break;
30288 else if (EQ (glyph->object, after_string))
30290 pos = string_buffer_position (after_string, end_charpos);
30291 if (!pos || (pos >= start_charpos && pos < end_charpos))
30292 break;
30296 glyph++; /* first glyph to the right of the highlighted area */
30297 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30298 x += g->pixel_width;
30299 hlinfo->mouse_face_beg_x = x;
30300 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30303 /* If the highlight ends in a different row, compute GLYPH and END
30304 for the end row. Otherwise, reuse the values computed above for
30305 the row where the highlight begins. */
30306 if (r2 != r1)
30308 if (!r2->reversed_p)
30310 glyph = r2->glyphs[TEXT_AREA];
30311 end = glyph + r2->used[TEXT_AREA];
30312 x = r2->x;
30314 else
30316 end = r2->glyphs[TEXT_AREA] - 1;
30317 glyph = end + r2->used[TEXT_AREA];
30321 if (!r2->reversed_p)
30323 /* Skip truncation and continuation glyphs near the end of the
30324 row, and also blanks and stretch glyphs inserted by
30325 extend_face_to_end_of_line. */
30326 while (end > glyph
30327 && NILP ((end - 1)->object))
30328 --end;
30329 /* Scan the rest of the glyph row from the end, looking for the
30330 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30331 DISP_STRING, or whose position is between START_CHARPOS
30332 and END_CHARPOS */
30333 for (--end;
30334 end > glyph
30335 && !NILP (end->object)
30336 && !EQ (end->object, disp_string)
30337 && !(BUFFERP (end->object)
30338 && (end->charpos >= start_charpos
30339 && end->charpos < end_charpos));
30340 --end)
30342 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30343 are present at buffer positions between START_CHARPOS and
30344 END_CHARPOS, or if they come from an overlay. */
30345 if (EQ (end->object, before_string))
30347 pos = string_buffer_position (before_string, start_charpos);
30348 if (!pos || (pos >= start_charpos && pos < end_charpos))
30349 break;
30351 else if (EQ (end->object, after_string))
30353 pos = string_buffer_position (after_string, end_charpos);
30354 if (!pos || (pos >= start_charpos && pos < end_charpos))
30355 break;
30358 /* Find the X coordinate of the last glyph to be highlighted. */
30359 for (; glyph <= end; ++glyph)
30360 x += glyph->pixel_width;
30362 hlinfo->mouse_face_end_x = x;
30363 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30365 else
30367 /* Skip truncation and continuation glyphs near the end of the
30368 row, and also blanks and stretch glyphs inserted by
30369 extend_face_to_end_of_line. */
30370 x = r2->x;
30371 end++;
30372 while (end < glyph
30373 && NILP (end->object))
30375 x += end->pixel_width;
30376 ++end;
30378 /* Scan the rest of the glyph row from the end, looking for the
30379 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30380 DISP_STRING, or whose position is between START_CHARPOS
30381 and END_CHARPOS */
30382 for ( ;
30383 end < glyph
30384 && !NILP (end->object)
30385 && !EQ (end->object, disp_string)
30386 && !(BUFFERP (end->object)
30387 && (end->charpos >= start_charpos
30388 && end->charpos < end_charpos));
30389 ++end)
30391 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30392 are present at buffer positions between START_CHARPOS and
30393 END_CHARPOS, or if they come from an overlay. */
30394 if (EQ (end->object, before_string))
30396 pos = string_buffer_position (before_string, start_charpos);
30397 if (!pos || (pos >= start_charpos && pos < end_charpos))
30398 break;
30400 else if (EQ (end->object, after_string))
30402 pos = string_buffer_position (after_string, end_charpos);
30403 if (!pos || (pos >= start_charpos && pos < end_charpos))
30404 break;
30406 x += end->pixel_width;
30408 /* If we exited the above loop because we arrived at the last
30409 glyph of the row, and its buffer position is still not in
30410 range, it means the last character in range is the preceding
30411 newline. Bump the end column and x values to get past the
30412 last glyph. */
30413 if (end == glyph
30414 && BUFFERP (end->object)
30415 && (end->charpos < start_charpos
30416 || end->charpos >= end_charpos))
30418 x += end->pixel_width;
30419 ++end;
30421 hlinfo->mouse_face_end_x = x;
30422 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30425 hlinfo->mouse_face_window = window;
30426 hlinfo->mouse_face_face_id
30427 = face_at_buffer_position (w, mouse_charpos, &ignore,
30428 mouse_charpos + 1,
30429 !hlinfo->mouse_face_hidden, -1);
30430 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30433 /* The following function is not used anymore (replaced with
30434 mouse_face_from_string_pos), but I leave it here for the time
30435 being, in case someone would. */
30437 #if false /* not used */
30439 /* Find the position of the glyph for position POS in OBJECT in
30440 window W's current matrix, and return in *X, *Y the pixel
30441 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30443 RIGHT_P means return the position of the right edge of the glyph.
30444 !RIGHT_P means return the left edge position.
30446 If no glyph for POS exists in the matrix, return the position of
30447 the glyph with the next smaller position that is in the matrix, if
30448 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30449 exists in the matrix, return the position of the glyph with the
30450 next larger position in OBJECT.
30452 Value is true if a glyph was found. */
30454 static bool
30455 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30456 int *hpos, int *vpos, int *x, int *y, bool right_p)
30458 int yb = window_text_bottom_y (w);
30459 struct glyph_row *r;
30460 struct glyph *best_glyph = NULL;
30461 struct glyph_row *best_row = NULL;
30462 int best_x = 0;
30464 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30465 r->enabled_p && r->y < yb;
30466 ++r)
30468 struct glyph *g = r->glyphs[TEXT_AREA];
30469 struct glyph *e = g + r->used[TEXT_AREA];
30470 int gx;
30472 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30473 if (EQ (g->object, object))
30475 if (g->charpos == pos)
30477 best_glyph = g;
30478 best_x = gx;
30479 best_row = r;
30480 goto found;
30482 else if (best_glyph == NULL
30483 || ((eabs (g->charpos - pos)
30484 < eabs (best_glyph->charpos - pos))
30485 && (right_p
30486 ? g->charpos < pos
30487 : g->charpos > pos)))
30489 best_glyph = g;
30490 best_x = gx;
30491 best_row = r;
30496 found:
30498 if (best_glyph)
30500 *x = best_x;
30501 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30503 if (right_p)
30505 *x += best_glyph->pixel_width;
30506 ++*hpos;
30509 *y = best_row->y;
30510 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30513 return best_glyph != NULL;
30515 #endif /* not used */
30517 /* Find the positions of the first and the last glyphs in window W's
30518 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30519 (assumed to be a string), and return in HLINFO's mouse_face_*
30520 members the pixel and column/row coordinates of those glyphs. */
30522 static void
30523 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30524 Lisp_Object object,
30525 ptrdiff_t startpos, ptrdiff_t endpos)
30527 int yb = window_text_bottom_y (w);
30528 struct glyph_row *r;
30529 struct glyph *g, *e;
30530 int gx;
30531 bool found = false;
30533 /* Find the glyph row with at least one position in the range
30534 [STARTPOS..ENDPOS), and the first glyph in that row whose
30535 position belongs to that range. */
30536 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30537 r->enabled_p && r->y < yb;
30538 ++r)
30540 if (!r->reversed_p)
30542 g = r->glyphs[TEXT_AREA];
30543 e = g + r->used[TEXT_AREA];
30544 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30545 if (EQ (g->object, object)
30546 && startpos <= g->charpos && g->charpos < endpos)
30548 hlinfo->mouse_face_beg_row
30549 = MATRIX_ROW_VPOS (r, w->current_matrix);
30550 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30551 hlinfo->mouse_face_beg_x = gx;
30552 found = true;
30553 break;
30556 else
30558 struct glyph *g1;
30560 e = r->glyphs[TEXT_AREA];
30561 g = e + r->used[TEXT_AREA];
30562 for ( ; g > e; --g)
30563 if (EQ ((g-1)->object, object)
30564 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30566 hlinfo->mouse_face_beg_row
30567 = MATRIX_ROW_VPOS (r, w->current_matrix);
30568 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30569 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30570 gx += g1->pixel_width;
30571 hlinfo->mouse_face_beg_x = gx;
30572 found = true;
30573 break;
30576 if (found)
30577 break;
30580 if (!found)
30581 return;
30583 /* Starting with the next row, look for the first row which does NOT
30584 include any glyphs whose positions are in the range. */
30585 for (++r; r->enabled_p && r->y < yb; ++r)
30587 g = r->glyphs[TEXT_AREA];
30588 e = g + r->used[TEXT_AREA];
30589 found = false;
30590 for ( ; g < e; ++g)
30591 if (EQ (g->object, object)
30592 && startpos <= g->charpos && g->charpos < endpos)
30594 found = true;
30595 break;
30597 if (!found)
30598 break;
30601 /* The highlighted region ends on the previous row. */
30602 r--;
30604 /* Set the end row. */
30605 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30607 /* Compute and set the end column and the end column's horizontal
30608 pixel coordinate. */
30609 if (!r->reversed_p)
30611 g = r->glyphs[TEXT_AREA];
30612 e = g + r->used[TEXT_AREA];
30613 for ( ; e > g; --e)
30614 if (EQ ((e-1)->object, object)
30615 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30616 break;
30617 hlinfo->mouse_face_end_col = e - g;
30619 for (gx = r->x; g < e; ++g)
30620 gx += g->pixel_width;
30621 hlinfo->mouse_face_end_x = gx;
30623 else
30625 e = r->glyphs[TEXT_AREA];
30626 g = e + r->used[TEXT_AREA];
30627 for (gx = r->x ; e < g; ++e)
30629 if (EQ (e->object, object)
30630 && startpos <= e->charpos && e->charpos < endpos)
30631 break;
30632 gx += e->pixel_width;
30634 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30635 hlinfo->mouse_face_end_x = gx;
30639 #ifdef HAVE_WINDOW_SYSTEM
30641 /* See if position X, Y is within a hot-spot of an image. */
30643 static bool
30644 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30646 if (!CONSP (hot_spot))
30647 return false;
30649 if (EQ (XCAR (hot_spot), Qrect))
30651 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30652 Lisp_Object rect = XCDR (hot_spot);
30653 Lisp_Object tem;
30654 if (!CONSP (rect))
30655 return false;
30656 if (!CONSP (XCAR (rect)))
30657 return false;
30658 if (!CONSP (XCDR (rect)))
30659 return false;
30660 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30661 return false;
30662 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30663 return false;
30664 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30665 return false;
30666 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30667 return false;
30668 return true;
30670 else if (EQ (XCAR (hot_spot), Qcircle))
30672 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30673 Lisp_Object circ = XCDR (hot_spot);
30674 Lisp_Object lr, lx0, ly0;
30675 if (CONSP (circ)
30676 && CONSP (XCAR (circ))
30677 && (lr = XCDR (circ), NUMBERP (lr))
30678 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30679 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30681 double r = XFLOATINT (lr);
30682 double dx = XINT (lx0) - x;
30683 double dy = XINT (ly0) - y;
30684 return (dx * dx + dy * dy <= r * r);
30687 else if (EQ (XCAR (hot_spot), Qpoly))
30689 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30690 if (VECTORP (XCDR (hot_spot)))
30692 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30693 Lisp_Object *poly = v->contents;
30694 ptrdiff_t n = v->header.size;
30695 ptrdiff_t i;
30696 bool inside = false;
30697 Lisp_Object lx, ly;
30698 int x0, y0;
30700 /* Need an even number of coordinates, and at least 3 edges. */
30701 if (n < 6 || n & 1)
30702 return false;
30704 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30705 If count is odd, we are inside polygon. Pixels on edges
30706 may or may not be included depending on actual geometry of the
30707 polygon. */
30708 if ((lx = poly[n-2], !INTEGERP (lx))
30709 || (ly = poly[n-1], !INTEGERP (lx)))
30710 return false;
30711 x0 = XINT (lx), y0 = XINT (ly);
30712 for (i = 0; i < n; i += 2)
30714 int x1 = x0, y1 = y0;
30715 if ((lx = poly[i], !INTEGERP (lx))
30716 || (ly = poly[i+1], !INTEGERP (ly)))
30717 return false;
30718 x0 = XINT (lx), y0 = XINT (ly);
30720 /* Does this segment cross the X line? */
30721 if (x0 >= x)
30723 if (x1 >= x)
30724 continue;
30726 else if (x1 < x)
30727 continue;
30728 if (y > y0 && y > y1)
30729 continue;
30730 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30731 inside = !inside;
30733 return inside;
30736 return false;
30739 Lisp_Object
30740 find_hot_spot (Lisp_Object map, int x, int y)
30742 while (CONSP (map))
30744 if (CONSP (XCAR (map))
30745 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30746 return XCAR (map);
30747 map = XCDR (map);
30750 return Qnil;
30753 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30754 3, 3, 0,
30755 doc: /* Lookup in image map MAP coordinates X and Y.
30756 An image map is an alist where each element has the format (AREA ID PLIST).
30757 An AREA is specified as either a rectangle, a circle, or a polygon:
30758 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30759 pixel coordinates of the upper left and bottom right corners.
30760 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30761 and the radius of the circle; r may be a float or integer.
30762 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30763 vector describes one corner in the polygon.
30764 Returns the alist element for the first matching AREA in MAP. */)
30765 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30767 if (NILP (map))
30768 return Qnil;
30770 CHECK_NUMBER (x);
30771 CHECK_NUMBER (y);
30773 return find_hot_spot (map,
30774 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30775 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30777 #endif /* HAVE_WINDOW_SYSTEM */
30780 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30781 static void
30782 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30784 #ifdef HAVE_WINDOW_SYSTEM
30785 if (!FRAME_WINDOW_P (f))
30786 return;
30788 /* Do not change cursor shape while dragging mouse. */
30789 if (EQ (do_mouse_tracking, Qdragging))
30790 return;
30792 if (!NILP (pointer))
30794 if (EQ (pointer, Qarrow))
30795 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30796 else if (EQ (pointer, Qhand))
30797 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30798 else if (EQ (pointer, Qtext))
30799 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30800 else if (EQ (pointer, intern ("hdrag")))
30801 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30802 else if (EQ (pointer, intern ("nhdrag")))
30803 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30804 # ifdef HAVE_X_WINDOWS
30805 else if (EQ (pointer, intern ("vdrag")))
30806 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30807 # endif
30808 else if (EQ (pointer, intern ("hourglass")))
30809 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30810 else if (EQ (pointer, Qmodeline))
30811 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30812 else
30813 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30816 if (cursor != No_Cursor)
30817 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30818 #endif
30821 /* Take proper action when mouse has moved to the mode or header line
30822 or marginal area AREA of window W, x-position X and y-position Y.
30823 X is relative to the start of the text display area of W, so the
30824 width of bitmap areas and scroll bars must be subtracted to get a
30825 position relative to the start of the mode line. */
30827 static void
30828 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30829 enum window_part area)
30831 struct window *w = XWINDOW (window);
30832 struct frame *f = XFRAME (w->frame);
30833 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30834 Cursor cursor = No_Cursor;
30835 Lisp_Object pointer = Qnil;
30836 int dx, dy, width, height;
30837 ptrdiff_t charpos;
30838 Lisp_Object string, object = Qnil;
30839 Lisp_Object pos UNINIT;
30840 Lisp_Object mouse_face;
30841 int original_x_pixel = x;
30842 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30843 struct glyph_row *row UNINIT;
30845 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30847 int x0;
30848 struct glyph *end;
30850 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30851 returns them in row/column units! */
30852 string = mode_line_string (w, area, &x, &y, &charpos,
30853 &object, &dx, &dy, &width, &height);
30855 row = (area == ON_MODE_LINE
30856 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30857 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30859 /* Find the glyph under the mouse pointer. */
30860 if (row->mode_line_p && row->enabled_p)
30862 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30863 end = glyph + row->used[TEXT_AREA];
30865 for (x0 = original_x_pixel;
30866 glyph < end && x0 >= glyph->pixel_width;
30867 ++glyph)
30868 x0 -= glyph->pixel_width;
30870 if (glyph >= end)
30871 glyph = NULL;
30874 else
30876 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30877 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30878 returns them in row/column units! */
30879 string = marginal_area_string (w, area, &x, &y, &charpos,
30880 &object, &dx, &dy, &width, &height);
30883 Lisp_Object help = Qnil;
30885 #ifdef HAVE_WINDOW_SYSTEM
30886 if (IMAGEP (object))
30888 Lisp_Object image_map, hotspot;
30889 if ((image_map = Fplist_get (XCDR (object), QCmap),
30890 !NILP (image_map))
30891 && (hotspot = find_hot_spot (image_map, dx, dy),
30892 CONSP (hotspot))
30893 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30895 Lisp_Object plist;
30897 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30898 If so, we could look for mouse-enter, mouse-leave
30899 properties in PLIST (and do something...). */
30900 hotspot = XCDR (hotspot);
30901 if (CONSP (hotspot)
30902 && (plist = XCAR (hotspot), CONSP (plist)))
30904 pointer = Fplist_get (plist, Qpointer);
30905 if (NILP (pointer))
30906 pointer = Qhand;
30907 help = Fplist_get (plist, Qhelp_echo);
30908 if (!NILP (help))
30910 help_echo_string = help;
30911 XSETWINDOW (help_echo_window, w);
30912 help_echo_object = w->contents;
30913 help_echo_pos = charpos;
30917 if (NILP (pointer))
30918 pointer = Fplist_get (XCDR (object), QCpointer);
30920 #endif /* HAVE_WINDOW_SYSTEM */
30922 if (STRINGP (string))
30923 pos = make_number (charpos);
30925 /* Set the help text and mouse pointer. If the mouse is on a part
30926 of the mode line without any text (e.g. past the right edge of
30927 the mode line text), use that windows's mode line help echo if it
30928 has been set. */
30929 if (STRINGP (string) || area == ON_MODE_LINE)
30931 /* Arrange to display the help by setting the global variables
30932 help_echo_string, help_echo_object, and help_echo_pos. */
30933 if (NILP (help))
30935 if (STRINGP (string))
30936 help = Fget_text_property (pos, Qhelp_echo, string);
30938 if (!NILP (help))
30940 help_echo_string = help;
30941 XSETWINDOW (help_echo_window, w);
30942 help_echo_object = string;
30943 help_echo_pos = charpos;
30945 else if (area == ON_MODE_LINE
30946 && !NILP (w->mode_line_help_echo))
30948 help_echo_string = w->mode_line_help_echo;
30949 XSETWINDOW (help_echo_window, w);
30950 help_echo_object = Qnil;
30951 help_echo_pos = -1;
30955 #ifdef HAVE_WINDOW_SYSTEM
30956 /* Change the mouse pointer according to what is under it. */
30957 if (FRAME_WINDOW_P (f))
30959 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30960 || minibuf_level
30961 || NILP (Vresize_mini_windows));
30963 if (STRINGP (string))
30965 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30967 if (NILP (pointer))
30968 pointer = Fget_text_property (pos, Qpointer, string);
30970 /* Change the mouse pointer according to what is under X/Y. */
30971 if (NILP (pointer)
30972 && (area == ON_MODE_LINE || area == ON_HEADER_LINE))
30974 Lisp_Object map;
30976 map = Fget_text_property (pos, Qlocal_map, string);
30977 if (!KEYMAPP (map))
30978 map = Fget_text_property (pos, Qkeymap, string);
30979 if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
30980 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30983 else if (draggable && area == ON_MODE_LINE)
30984 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30985 else
30986 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30988 #endif
30991 /* Change the mouse face according to what is under X/Y. */
30992 bool mouse_face_shown = false;
30994 if (STRINGP (string))
30996 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30997 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30998 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30999 && glyph)
31001 Lisp_Object b, e;
31003 struct glyph * tmp_glyph;
31005 int gpos;
31006 int gseq_length;
31007 int total_pixel_width;
31008 ptrdiff_t begpos, endpos, ignore;
31010 int vpos, hpos;
31012 b = Fprevious_single_property_change (make_number (charpos + 1),
31013 Qmouse_face, string, Qnil);
31014 if (NILP (b))
31015 begpos = 0;
31016 else
31017 begpos = XINT (b);
31019 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
31020 if (NILP (e))
31021 endpos = SCHARS (string);
31022 else
31023 endpos = XINT (e);
31025 /* Calculate the glyph position GPOS of GLYPH in the
31026 displayed string, relative to the beginning of the
31027 highlighted part of the string.
31029 Note: GPOS is different from CHARPOS. CHARPOS is the
31030 position of GLYPH in the internal string object. A mode
31031 line string format has structures which are converted to
31032 a flattened string by the Emacs Lisp interpreter. The
31033 internal string is an element of those structures. The
31034 displayed string is the flattened string. */
31035 tmp_glyph = row_start_glyph;
31036 while (tmp_glyph < glyph
31037 && (!(EQ (tmp_glyph->object, glyph->object)
31038 && begpos <= tmp_glyph->charpos
31039 && tmp_glyph->charpos < endpos)))
31040 tmp_glyph++;
31041 gpos = glyph - tmp_glyph;
31043 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
31044 the highlighted part of the displayed string to which
31045 GLYPH belongs. Note: GSEQ_LENGTH is different from
31046 SCHARS (STRING), because the latter returns the length of
31047 the internal string. */
31048 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
31049 tmp_glyph > glyph
31050 && (!(EQ (tmp_glyph->object, glyph->object)
31051 && begpos <= tmp_glyph->charpos
31052 && tmp_glyph->charpos < endpos));
31053 tmp_glyph--)
31055 gseq_length = gpos + (tmp_glyph - glyph) + 1;
31057 /* Calculate the total pixel width of all the glyphs between
31058 the beginning of the highlighted area and GLYPH. */
31059 total_pixel_width = 0;
31060 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
31061 total_pixel_width += tmp_glyph->pixel_width;
31063 /* Pre calculation of re-rendering position. Note: X is in
31064 column units here, after the call to mode_line_string or
31065 marginal_area_string. */
31066 hpos = x - gpos;
31067 vpos = (area == ON_MODE_LINE
31068 ? (w->current_matrix)->nrows - 1
31069 : 0);
31071 /* If GLYPH's position is included in the region that is
31072 already drawn in mouse face, we have nothing to do. */
31073 if ( EQ (window, hlinfo->mouse_face_window)
31074 && (!row->reversed_p
31075 ? (hlinfo->mouse_face_beg_col <= hpos
31076 && hpos < hlinfo->mouse_face_end_col)
31077 /* In R2L rows we swap BEG and END, see below. */
31078 : (hlinfo->mouse_face_end_col <= hpos
31079 && hpos < hlinfo->mouse_face_beg_col))
31080 && hlinfo->mouse_face_beg_row == vpos )
31081 return;
31083 if (clear_mouse_face (hlinfo))
31084 cursor = No_Cursor;
31086 if (!row->reversed_p)
31088 hlinfo->mouse_face_beg_col = hpos;
31089 hlinfo->mouse_face_beg_x = original_x_pixel
31090 - (total_pixel_width + dx);
31091 hlinfo->mouse_face_end_col = hpos + gseq_length;
31092 hlinfo->mouse_face_end_x = 0;
31094 else
31096 /* In R2L rows, show_mouse_face expects BEG and END
31097 coordinates to be swapped. */
31098 hlinfo->mouse_face_end_col = hpos;
31099 hlinfo->mouse_face_end_x = original_x_pixel
31100 - (total_pixel_width + dx);
31101 hlinfo->mouse_face_beg_col = hpos + gseq_length;
31102 hlinfo->mouse_face_beg_x = 0;
31105 hlinfo->mouse_face_beg_row = vpos;
31106 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31107 hlinfo->mouse_face_past_end = false;
31108 hlinfo->mouse_face_window = window;
31110 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31111 charpos,
31112 0, &ignore,
31113 glyph->face_id,
31114 true);
31115 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31116 mouse_face_shown = true;
31118 if (NILP (pointer))
31119 pointer = Qhand;
31123 /* If mouse-face doesn't need to be shown, clear any existing
31124 mouse-face. */
31125 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31126 clear_mouse_face (hlinfo);
31128 define_frame_cursor1 (f, cursor, pointer);
31132 /* EXPORT:
31133 Take proper action when the mouse has moved to position X, Y on
31134 frame F with regards to highlighting portions of display that have
31135 mouse-face properties. Also de-highlight portions of display where
31136 the mouse was before, set the mouse pointer shape as appropriate
31137 for the mouse coordinates, and activate help echo (tooltips).
31138 X and Y can be negative or out of range. */
31140 void
31141 note_mouse_highlight (struct frame *f, int x, int y)
31143 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31144 enum window_part part = ON_NOTHING;
31145 Lisp_Object window;
31146 struct window *w;
31147 Cursor cursor = No_Cursor;
31148 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31149 struct buffer *b;
31151 /* When a menu is active, don't highlight because this looks odd. */
31152 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31153 if (popup_activated ())
31154 return;
31155 #endif
31157 if (!f->glyphs_initialized_p
31158 || f->pointer_invisible)
31159 return;
31161 hlinfo->mouse_face_mouse_x = x;
31162 hlinfo->mouse_face_mouse_y = y;
31163 hlinfo->mouse_face_mouse_frame = f;
31165 if (hlinfo->mouse_face_defer)
31166 return;
31168 /* Which window is that in? */
31169 window = window_from_coordinates (f, x, y, &part, true);
31171 /* If displaying active text in another window, clear that. */
31172 if (! EQ (window, hlinfo->mouse_face_window)
31173 /* Also clear if we move out of text area in same window. */
31174 || (!NILP (hlinfo->mouse_face_window)
31175 && !NILP (window)
31176 && part != ON_TEXT
31177 && part != ON_MODE_LINE
31178 && part != ON_HEADER_LINE))
31179 clear_mouse_face (hlinfo);
31181 /* Reset help_echo_string. It will get recomputed below. */
31182 help_echo_string = Qnil;
31184 #ifdef HAVE_WINDOW_SYSTEM
31185 /* If the cursor is on the internal border of FRAME and FRAME's
31186 internal border is draggable, provide some visual feedback. */
31187 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31188 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31190 enum internal_border_part part = frame_internal_border_part (f, x, y);
31192 switch (part)
31194 case INTERNAL_BORDER_NONE:
31195 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31196 /* Reset cursor. */
31197 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31198 break;
31199 case INTERNAL_BORDER_LEFT_EDGE:
31200 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31201 break;
31202 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31203 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31204 break;
31205 case INTERNAL_BORDER_TOP_EDGE:
31206 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31207 break;
31208 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31209 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31210 break;
31211 case INTERNAL_BORDER_RIGHT_EDGE:
31212 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31213 break;
31214 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31215 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31216 break;
31217 case INTERNAL_BORDER_BOTTOM_EDGE:
31218 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31219 break;
31220 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31221 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31222 break;
31223 default:
31224 /* This should not happen. */
31225 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31226 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31229 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31231 /* Do we really want a help echo here? */
31232 help_echo_string = build_string ("drag-mouse-1: resize frame");
31233 goto set_cursor;
31236 #endif /* HAVE_WINDOW_SYSTEM */
31238 /* Not on a window -> return. */
31239 if (!WINDOWP (window))
31240 return;
31242 /* Convert to window-relative pixel coordinates. */
31243 w = XWINDOW (window);
31244 frame_to_window_pixel_xy (w, &x, &y);
31246 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31247 /* Handle tool-bar window differently since it doesn't display a
31248 buffer. */
31249 if (EQ (window, f->tool_bar_window))
31251 note_tool_bar_highlight (f, x, y);
31252 return;
31254 #endif
31256 /* Mouse is on the mode, header line or margin? */
31257 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31258 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31260 note_mode_line_or_margin_highlight (window, x, y, part);
31262 #ifdef HAVE_WINDOW_SYSTEM
31263 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31265 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31266 /* Show non-text cursor (Bug#16647). */
31267 goto set_cursor;
31269 else
31270 #endif
31271 return;
31274 #ifdef HAVE_WINDOW_SYSTEM
31275 if (part == ON_VERTICAL_BORDER)
31277 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31278 help_echo_string = build_string ("drag-mouse-1: resize");
31279 goto set_cursor;
31281 else if (part == ON_RIGHT_DIVIDER)
31283 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31284 help_echo_string = build_string ("drag-mouse-1: resize");
31285 goto set_cursor;
31287 else if (part == ON_BOTTOM_DIVIDER)
31288 if (! WINDOW_BOTTOMMOST_P (w)
31289 || minibuf_level
31290 || NILP (Vresize_mini_windows))
31292 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31293 help_echo_string = build_string ("drag-mouse-1: resize");
31294 goto set_cursor;
31296 else
31297 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31298 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31299 || part == ON_VERTICAL_SCROLL_BAR
31300 || part == ON_HORIZONTAL_SCROLL_BAR)
31301 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31302 else
31303 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31304 #endif
31306 /* Are we in a window whose display is up to date?
31307 And verify the buffer's text has not changed. */
31308 b = XBUFFER (w->contents);
31309 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31311 int hpos, vpos, dx, dy, area = LAST_AREA;
31312 ptrdiff_t pos;
31313 struct glyph *glyph;
31314 Lisp_Object object;
31315 Lisp_Object mouse_face = Qnil, position;
31316 Lisp_Object *overlay_vec = NULL;
31317 ptrdiff_t i, noverlays;
31318 struct buffer *obuf;
31319 ptrdiff_t obegv, ozv;
31320 bool same_region;
31322 /* Find the glyph under X/Y. */
31323 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31325 #ifdef HAVE_WINDOW_SYSTEM
31326 /* Look for :pointer property on image. */
31327 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31329 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31330 if (img != NULL && IMAGEP (img->spec))
31332 Lisp_Object image_map, hotspot;
31333 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31334 !NILP (image_map))
31335 && (hotspot = find_hot_spot (image_map,
31336 glyph->slice.img.x + dx,
31337 glyph->slice.img.y + dy),
31338 CONSP (hotspot))
31339 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31341 Lisp_Object plist;
31343 /* Could check XCAR (hotspot) to see if we enter/leave
31344 this hot-spot.
31345 If so, we could look for mouse-enter, mouse-leave
31346 properties in PLIST (and do something...). */
31347 hotspot = XCDR (hotspot);
31348 if (CONSP (hotspot)
31349 && (plist = XCAR (hotspot), CONSP (plist)))
31351 pointer = Fplist_get (plist, Qpointer);
31352 if (NILP (pointer))
31353 pointer = Qhand;
31354 help_echo_string = Fplist_get (plist, Qhelp_echo);
31355 if (!NILP (help_echo_string))
31357 help_echo_window = window;
31358 help_echo_object = glyph->object;
31359 help_echo_pos = glyph->charpos;
31363 if (NILP (pointer))
31364 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31367 #endif /* HAVE_WINDOW_SYSTEM */
31369 /* Clear mouse face if X/Y not over text. */
31370 if (glyph == NULL
31371 || area != TEXT_AREA
31372 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31373 /* Glyph's OBJECT is nil for glyphs inserted by the
31374 display engine for its internal purposes, like truncation
31375 and continuation glyphs and blanks beyond the end of
31376 line's text on text terminals. If we are over such a
31377 glyph, we are not over any text. */
31378 || NILP (glyph->object)
31379 /* R2L rows have a stretch glyph at their front, which
31380 stands for no text, whereas L2R rows have no glyphs at
31381 all beyond the end of text. Treat such stretch glyphs
31382 like we do with NULL glyphs in L2R rows. */
31383 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31384 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31385 && glyph->type == STRETCH_GLYPH
31386 && glyph->avoid_cursor_p))
31388 if (clear_mouse_face (hlinfo))
31389 cursor = No_Cursor;
31390 if (FRAME_WINDOW_P (f) && NILP (pointer))
31392 #ifdef HAVE_WINDOW_SYSTEM
31393 if (area != TEXT_AREA)
31394 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31395 else
31396 pointer = Vvoid_text_area_pointer;
31397 #endif
31399 goto set_cursor;
31402 pos = glyph->charpos;
31403 object = glyph->object;
31404 if (!STRINGP (object) && !BUFFERP (object))
31405 goto set_cursor;
31407 /* If we get an out-of-range value, return now; avoid an error. */
31408 if (BUFFERP (object) && pos > BUF_Z (b))
31409 goto set_cursor;
31411 /* Make the window's buffer temporarily current for
31412 overlays_at and compute_char_face. */
31413 obuf = current_buffer;
31414 current_buffer = b;
31415 obegv = BEGV;
31416 ozv = ZV;
31417 BEGV = BEG;
31418 ZV = Z;
31420 /* Is this char mouse-active or does it have help-echo? */
31421 position = make_number (pos);
31423 USE_SAFE_ALLOCA;
31425 if (BUFFERP (object))
31427 /* Put all the overlays we want in a vector in overlay_vec. */
31428 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31429 /* Sort overlays into increasing priority order. */
31430 noverlays = sort_overlays (overlay_vec, noverlays, w);
31432 else
31433 noverlays = 0;
31435 if (NILP (Vmouse_highlight))
31437 clear_mouse_face (hlinfo);
31438 goto check_help_echo;
31441 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31443 if (same_region)
31444 cursor = No_Cursor;
31446 /* Check mouse-face highlighting. */
31447 if (! same_region
31448 /* If there exists an overlay with mouse-face overlapping
31449 the one we are currently highlighting, we have to check
31450 if we enter the overlapping overlay, and then highlight
31451 only that. Skip the check when mouse-face highlighting
31452 is currently hidden to avoid Bug#30519. */
31453 || (!hlinfo->mouse_face_hidden
31454 && OVERLAYP (hlinfo->mouse_face_overlay)
31455 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31457 /* Find the highest priority overlay with a mouse-face. */
31458 Lisp_Object overlay = Qnil;
31459 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31461 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31462 if (!NILP (mouse_face))
31463 overlay = overlay_vec[i];
31466 /* If we're highlighting the same overlay as before, there's
31467 no need to do that again. */
31468 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31469 goto check_help_echo;
31471 /* Clear the display of the old active region, if any. */
31472 if (clear_mouse_face (hlinfo))
31473 cursor = No_Cursor;
31475 /* Record the overlay, if any, to be highlighted. */
31476 hlinfo->mouse_face_overlay = overlay;
31478 /* If no overlay applies, get a text property. */
31479 if (NILP (overlay))
31480 mouse_face = Fget_text_property (position, Qmouse_face, object);
31482 /* Next, compute the bounds of the mouse highlighting and
31483 display it. */
31484 if (!NILP (mouse_face) && STRINGP (object))
31486 /* The mouse-highlighting comes from a display string
31487 with a mouse-face. */
31488 Lisp_Object s, e;
31489 ptrdiff_t ignore;
31491 s = Fprevious_single_property_change
31492 (make_number (pos + 1), Qmouse_face, object, Qnil);
31493 e = Fnext_single_property_change
31494 (position, Qmouse_face, object, Qnil);
31495 if (NILP (s))
31496 s = make_number (0);
31497 if (NILP (e))
31498 e = make_number (SCHARS (object));
31499 mouse_face_from_string_pos (w, hlinfo, object,
31500 XINT (s), XINT (e));
31501 hlinfo->mouse_face_past_end = false;
31502 hlinfo->mouse_face_window = window;
31503 hlinfo->mouse_face_face_id
31504 = face_at_string_position (w, object, pos, 0, &ignore,
31505 glyph->face_id, true);
31506 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31507 cursor = No_Cursor;
31509 else
31511 /* The mouse-highlighting, if any, comes from an overlay
31512 or text property in the buffer. */
31513 Lisp_Object buffer UNINIT;
31514 Lisp_Object disp_string UNINIT;
31516 if (STRINGP (object))
31518 /* If we are on a display string with no mouse-face,
31519 check if the text under it has one. */
31520 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31521 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31522 pos = string_buffer_position (object, start);
31523 if (pos > 0)
31525 mouse_face = get_char_property_and_overlay
31526 (make_number (pos), Qmouse_face, w->contents, &overlay);
31527 buffer = w->contents;
31528 disp_string = object;
31531 else
31533 buffer = object;
31534 disp_string = Qnil;
31537 if (!NILP (mouse_face))
31539 Lisp_Object before, after;
31540 Lisp_Object before_string, after_string;
31541 /* To correctly find the limits of mouse highlight
31542 in a bidi-reordered buffer, we must not use the
31543 optimization of limiting the search in
31544 previous-single-property-change and
31545 next-single-property-change, because
31546 rows_from_pos_range needs the real start and end
31547 positions to DTRT in this case. That's because
31548 the first row visible in a window does not
31549 necessarily display the character whose position
31550 is the smallest. */
31551 Lisp_Object lim1
31552 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31553 ? Fmarker_position (w->start)
31554 : Qnil;
31555 Lisp_Object lim2
31556 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31557 ? make_number (BUF_Z (XBUFFER (buffer))
31558 - w->window_end_pos)
31559 : Qnil;
31561 if (NILP (overlay))
31563 /* Handle the text property case. */
31564 before = Fprevious_single_property_change
31565 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31566 after = Fnext_single_property_change
31567 (make_number (pos), Qmouse_face, buffer, lim2);
31568 before_string = after_string = Qnil;
31570 else
31572 /* Handle the overlay case. */
31573 before = Foverlay_start (overlay);
31574 after = Foverlay_end (overlay);
31575 before_string = Foverlay_get (overlay, Qbefore_string);
31576 after_string = Foverlay_get (overlay, Qafter_string);
31578 if (!STRINGP (before_string)) before_string = Qnil;
31579 if (!STRINGP (after_string)) after_string = Qnil;
31582 mouse_face_from_buffer_pos (window, hlinfo, pos,
31583 NILP (before)
31585 : XFASTINT (before),
31586 NILP (after)
31587 ? BUF_Z (XBUFFER (buffer))
31588 : XFASTINT (after),
31589 before_string, after_string,
31590 disp_string);
31591 cursor = No_Cursor;
31596 check_help_echo:
31598 /* Look for a `help-echo' property. */
31599 if (NILP (help_echo_string)) {
31600 Lisp_Object help, overlay;
31602 /* Check overlays first. */
31603 help = overlay = Qnil;
31604 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31606 overlay = overlay_vec[i];
31607 help = Foverlay_get (overlay, Qhelp_echo);
31610 if (!NILP (help))
31612 help_echo_string = help;
31613 help_echo_window = window;
31614 help_echo_object = overlay;
31615 help_echo_pos = pos;
31617 else
31619 Lisp_Object obj = glyph->object;
31620 ptrdiff_t charpos = glyph->charpos;
31622 /* Try text properties. */
31623 if (STRINGP (obj)
31624 && charpos >= 0
31625 && charpos < SCHARS (obj))
31627 help = Fget_text_property (make_number (charpos),
31628 Qhelp_echo, obj);
31629 if (NILP (help))
31631 /* If the string itself doesn't specify a help-echo,
31632 see if the buffer text ``under'' it does. */
31633 struct glyph_row *r
31634 = MATRIX_ROW (w->current_matrix, vpos);
31635 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31636 ptrdiff_t p = string_buffer_position (obj, start);
31637 if (p > 0)
31639 help = Fget_char_property (make_number (p),
31640 Qhelp_echo, w->contents);
31641 if (!NILP (help))
31643 charpos = p;
31644 obj = w->contents;
31649 else if (BUFFERP (obj)
31650 && charpos >= BEGV
31651 && charpos < ZV)
31652 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31653 obj);
31655 if (!NILP (help))
31657 help_echo_string = help;
31658 help_echo_window = window;
31659 help_echo_object = obj;
31660 help_echo_pos = charpos;
31665 #ifdef HAVE_WINDOW_SYSTEM
31666 /* Look for a `pointer' property. */
31667 if (FRAME_WINDOW_P (f) && NILP (pointer))
31669 /* Check overlays first. */
31670 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31671 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31673 if (NILP (pointer))
31675 Lisp_Object obj = glyph->object;
31676 ptrdiff_t charpos = glyph->charpos;
31678 /* Try text properties. */
31679 if (STRINGP (obj)
31680 && charpos >= 0
31681 && charpos < SCHARS (obj))
31683 pointer = Fget_text_property (make_number (charpos),
31684 Qpointer, obj);
31685 if (NILP (pointer))
31687 /* If the string itself doesn't specify a pointer,
31688 see if the buffer text ``under'' it does. */
31689 struct glyph_row *r
31690 = MATRIX_ROW (w->current_matrix, vpos);
31691 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31692 ptrdiff_t p = string_buffer_position (obj, start);
31693 if (p > 0)
31694 pointer = Fget_char_property (make_number (p),
31695 Qpointer, w->contents);
31698 else if (BUFFERP (obj)
31699 && charpos >= BEGV
31700 && charpos < ZV)
31701 pointer = Fget_text_property (make_number (charpos),
31702 Qpointer, obj);
31705 #endif /* HAVE_WINDOW_SYSTEM */
31707 BEGV = obegv;
31708 ZV = ozv;
31709 current_buffer = obuf;
31710 SAFE_FREE ();
31713 set_cursor:
31714 define_frame_cursor1 (f, cursor, pointer);
31718 /* EXPORT for RIF:
31719 Clear any mouse-face on window W. This function is part of the
31720 redisplay interface, and is called from try_window_id and similar
31721 functions to ensure the mouse-highlight is off. */
31723 void
31724 x_clear_window_mouse_face (struct window *w)
31726 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31727 Lisp_Object window;
31729 block_input ();
31730 XSETWINDOW (window, w);
31731 if (EQ (window, hlinfo->mouse_face_window))
31732 clear_mouse_face (hlinfo);
31733 unblock_input ();
31737 /* EXPORT:
31738 Just discard the mouse face information for frame F, if any.
31739 This is used when the size of F is changed. */
31741 void
31742 cancel_mouse_face (struct frame *f)
31744 Lisp_Object window;
31745 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31747 window = hlinfo->mouse_face_window;
31748 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31749 reset_mouse_highlight (hlinfo);
31754 /***********************************************************************
31755 Exposure Events
31756 ***********************************************************************/
31758 #ifdef HAVE_WINDOW_SYSTEM
31760 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31761 which intersects rectangle R. R is in window-relative coordinates. */
31763 static void
31764 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31765 enum glyph_row_area area)
31767 struct glyph *first = row->glyphs[area];
31768 struct glyph *end = row->glyphs[area] + row->used[area];
31769 struct glyph *last;
31770 int first_x, start_x, x;
31772 if (area == TEXT_AREA && row->fill_line_p)
31773 /* If row extends face to end of line write the whole line. */
31774 draw_glyphs (w, 0, row, area,
31775 0, row->used[area],
31776 DRAW_NORMAL_TEXT, 0);
31777 else
31779 /* Set START_X to the window-relative start position for drawing glyphs of
31780 AREA. The first glyph of the text area can be partially visible.
31781 The first glyphs of other areas cannot. */
31782 start_x = window_box_left_offset (w, area);
31783 x = start_x;
31784 if (area == TEXT_AREA)
31785 x += row->x;
31787 /* Find the first glyph that must be redrawn. */
31788 while (first < end
31789 && x + first->pixel_width < r->x)
31791 x += first->pixel_width;
31792 ++first;
31795 /* Find the last one. */
31796 last = first;
31797 first_x = x;
31798 /* Use a signed int intermediate value to avoid catastrophic
31799 failures due to comparison between signed and unsigned, when
31800 x is negative (can happen for wide images that are hscrolled). */
31801 int r_end = r->x + r->width;
31802 while (last < end && x < r_end)
31804 x += last->pixel_width;
31805 ++last;
31808 /* Repaint. */
31809 if (last > first)
31810 draw_glyphs (w, first_x - start_x, row, area,
31811 first - row->glyphs[area], last - row->glyphs[area],
31812 DRAW_NORMAL_TEXT, 0);
31817 /* Redraw the parts of the glyph row ROW on window W intersecting
31818 rectangle R. R is in window-relative coordinates. Value is
31819 true if mouse-face was overwritten. */
31821 static bool
31822 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31824 eassert (row->enabled_p);
31826 if (row->mode_line_p || w->pseudo_window_p)
31827 draw_glyphs (w, 0, row, TEXT_AREA,
31828 0, row->used[TEXT_AREA],
31829 DRAW_NORMAL_TEXT, 0);
31830 else
31832 if (row->used[LEFT_MARGIN_AREA])
31833 expose_area (w, row, r, LEFT_MARGIN_AREA);
31834 if (row->used[TEXT_AREA])
31835 expose_area (w, row, r, TEXT_AREA);
31836 if (row->used[RIGHT_MARGIN_AREA])
31837 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31838 draw_row_fringe_bitmaps (w, row);
31841 return row->mouse_face_p;
31845 /* Redraw those parts of glyphs rows during expose event handling that
31846 overlap other rows. Redrawing of an exposed line writes over parts
31847 of lines overlapping that exposed line; this function fixes that.
31849 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31850 row in W's current matrix that is exposed and overlaps other rows.
31851 LAST_OVERLAPPING_ROW is the last such row. */
31853 static void
31854 expose_overlaps (struct window *w,
31855 struct glyph_row *first_overlapping_row,
31856 struct glyph_row *last_overlapping_row,
31857 XRectangle *r)
31859 struct glyph_row *row;
31861 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31862 if (row->overlapping_p)
31864 eassert (row->enabled_p && !row->mode_line_p);
31866 row->clip = r;
31867 if (row->used[LEFT_MARGIN_AREA])
31868 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31870 if (row->used[TEXT_AREA])
31871 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31873 if (row->used[RIGHT_MARGIN_AREA])
31874 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31875 row->clip = NULL;
31880 /* Return true if W's cursor intersects rectangle R. */
31882 static bool
31883 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31885 XRectangle cr, result;
31886 struct glyph *cursor_glyph;
31887 struct glyph_row *row;
31889 if (w->phys_cursor.vpos >= 0
31890 && w->phys_cursor.vpos < w->current_matrix->nrows
31891 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31892 row->enabled_p)
31893 && row->cursor_in_fringe_p)
31895 /* Cursor is in the fringe. */
31896 cr.x = window_box_right_offset (w,
31897 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31898 ? RIGHT_MARGIN_AREA
31899 : TEXT_AREA));
31900 cr.y = row->y;
31901 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31902 cr.height = row->height;
31903 return x_intersect_rectangles (&cr, r, &result);
31906 cursor_glyph = get_phys_cursor_glyph (w);
31907 if (cursor_glyph)
31909 /* r is relative to W's box, but w->phys_cursor.x is relative
31910 to left edge of W's TEXT area. Adjust it. */
31911 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31912 cr.y = w->phys_cursor.y;
31913 cr.width = cursor_glyph->pixel_width;
31914 cr.height = w->phys_cursor_height;
31915 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31916 I assume the effect is the same -- and this is portable. */
31917 return x_intersect_rectangles (&cr, r, &result);
31919 /* If we don't understand the format, pretend we're not in the hot-spot. */
31920 return false;
31924 /* EXPORT:
31925 Draw a vertical window border to the right of window W if W doesn't
31926 have vertical scroll bars. */
31928 void
31929 x_draw_vertical_border (struct window *w)
31931 struct frame *f = XFRAME (WINDOW_FRAME (w));
31933 /* We could do better, if we knew what type of scroll-bar the adjacent
31934 windows (on either side) have... But we don't :-(
31935 However, I think this works ok. ++KFS 2003-04-25 */
31937 /* Redraw borders between horizontally adjacent windows. Don't
31938 do it for frames with vertical scroll bars because either the
31939 right scroll bar of a window, or the left scroll bar of its
31940 neighbor will suffice as a border. */
31941 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31942 return;
31944 /* Note: It is necessary to redraw both the left and the right
31945 borders, for when only this single window W is being
31946 redisplayed. */
31947 if (!WINDOW_RIGHTMOST_P (w)
31948 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31950 int x0, x1, y0, y1;
31952 window_box_edges (w, &x0, &y0, &x1, &y1);
31953 y1 -= 1;
31955 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31956 x1 -= 1;
31958 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31961 if (!WINDOW_LEFTMOST_P (w)
31962 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31964 int x0, x1, y0, y1;
31966 window_box_edges (w, &x0, &y0, &x1, &y1);
31967 y1 -= 1;
31969 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31970 x0 -= 1;
31972 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31977 /* Draw window dividers for window W. */
31979 void
31980 x_draw_right_divider (struct window *w)
31982 struct frame *f = WINDOW_XFRAME (w);
31984 if (w->mini || w->pseudo_window_p)
31985 return;
31986 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31988 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31989 int x1 = WINDOW_RIGHT_EDGE_X (w);
31990 int y0 = WINDOW_TOP_EDGE_Y (w);
31991 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31993 /* If W is horizontally combined and has a right sibling, don't
31994 draw over any bottom divider. */
31995 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31996 && !NILP (w->parent)
31997 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31998 && !NILP (w->next))
31999 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
32001 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
32005 static void
32006 x_draw_bottom_divider (struct window *w)
32008 struct frame *f = XFRAME (WINDOW_FRAME (w));
32010 if (w->mini || w->pseudo_window_p)
32011 return;
32012 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32014 int x0 = WINDOW_LEFT_EDGE_X (w);
32015 int x1 = WINDOW_RIGHT_EDGE_X (w);
32016 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
32017 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
32018 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
32020 /* If W is vertically combined and has a sibling below, don't draw
32021 over any right divider. */
32022 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
32023 && p
32024 && ((WINDOW_VERTICAL_COMBINATION_P (p)
32025 && !NILP (w->next))
32026 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
32027 && NILP (w->next)
32028 && !NILP (p->parent)
32029 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
32030 && !NILP (XWINDOW (p->parent)->next))))
32031 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
32033 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
32037 /* Redraw the part of window W intersection rectangle FR. Pixel
32038 coordinates in FR are frame-relative. Call this function with
32039 input blocked. Value is true if the exposure overwrites
32040 mouse-face. */
32042 static bool
32043 expose_window (struct window *w, XRectangle *fr)
32045 struct frame *f = XFRAME (w->frame);
32046 XRectangle wr, r;
32047 bool mouse_face_overwritten_p = false;
32049 /* If window is not yet fully initialized, do nothing. This can
32050 happen when toolkit scroll bars are used and a window is split.
32051 Reconfiguring the scroll bar will generate an expose for a newly
32052 created window. */
32053 if (w->current_matrix == NULL)
32054 return false;
32056 /* When we're currently updating the window, display and current
32057 matrix usually don't agree. Arrange for a thorough display
32058 later. */
32059 if (w->must_be_updated_p)
32061 SET_FRAME_GARBAGED (f);
32062 return false;
32065 /* Frame-relative pixel rectangle of W. */
32066 wr.x = WINDOW_LEFT_EDGE_X (w);
32067 wr.y = WINDOW_TOP_EDGE_Y (w);
32068 wr.width = WINDOW_PIXEL_WIDTH (w);
32069 wr.height = WINDOW_PIXEL_HEIGHT (w);
32071 if (x_intersect_rectangles (fr, &wr, &r))
32073 int yb = window_text_bottom_y (w);
32074 struct glyph_row *row;
32075 struct glyph_row *first_overlapping_row, *last_overlapping_row;
32077 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
32078 r.x, r.y, r.width, r.height));
32080 /* Convert to window coordinates. */
32081 r.x -= WINDOW_LEFT_EDGE_X (w);
32082 r.y -= WINDOW_TOP_EDGE_Y (w);
32084 /* Turn off the cursor. */
32085 bool cursor_cleared_p = (!w->pseudo_window_p
32086 && phys_cursor_in_rect_p (w, &r));
32087 if (cursor_cleared_p)
32088 x_clear_cursor (w);
32090 /* If the row containing the cursor extends face to end of line,
32091 then expose_area might overwrite the cursor outside the
32092 rectangle and thus notice_overwritten_cursor might clear
32093 w->phys_cursor_on_p. We remember the original value and
32094 check later if it is changed. */
32095 bool phys_cursor_on_p = w->phys_cursor_on_p;
32097 /* Use a signed int intermediate value to avoid catastrophic
32098 failures due to comparison between signed and unsigned, when
32099 y0 or y1 is negative (can happen for tall images). */
32100 int r_bottom = r.y + r.height;
32102 /* Update lines intersecting rectangle R. */
32103 first_overlapping_row = last_overlapping_row = NULL;
32104 for (row = w->current_matrix->rows;
32105 row->enabled_p;
32106 ++row)
32108 int y0 = row->y;
32109 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32111 if ((y0 >= r.y && y0 < r_bottom)
32112 || (y1 > r.y && y1 < r_bottom)
32113 || (r.y >= y0 && r.y < y1)
32114 || (r_bottom > y0 && r_bottom < y1))
32116 /* A header line may be overlapping, but there is no need
32117 to fix overlapping areas for them. KFS 2005-02-12 */
32118 if (row->overlapping_p && !row->mode_line_p)
32120 if (first_overlapping_row == NULL)
32121 first_overlapping_row = row;
32122 last_overlapping_row = row;
32125 row->clip = fr;
32126 if (expose_line (w, row, &r))
32127 mouse_face_overwritten_p = true;
32128 row->clip = NULL;
32130 else if (row->overlapping_p)
32132 /* We must redraw a row overlapping the exposed area. */
32133 if (y0 < r.y
32134 ? y0 + row->phys_height > r.y
32135 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32137 if (first_overlapping_row == NULL)
32138 first_overlapping_row = row;
32139 last_overlapping_row = row;
32143 if (y1 >= yb)
32144 break;
32147 /* Display the mode line if there is one. */
32148 if (window_wants_mode_line (w)
32149 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32150 row->enabled_p)
32151 && row->y < r_bottom)
32153 if (expose_line (w, row, &r))
32154 mouse_face_overwritten_p = true;
32157 if (!w->pseudo_window_p)
32159 /* Fix the display of overlapping rows. */
32160 if (first_overlapping_row)
32161 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32162 fr);
32164 /* Draw border between windows. */
32165 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32166 x_draw_right_divider (w);
32167 else
32168 x_draw_vertical_border (w);
32170 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32171 x_draw_bottom_divider (w);
32173 /* Turn the cursor on again. */
32174 if (cursor_cleared_p
32175 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32176 update_window_cursor (w, true);
32180 return mouse_face_overwritten_p;
32185 /* Redraw (parts) of all windows in the window tree rooted at W that
32186 intersect R. R contains frame pixel coordinates. Value is
32187 true if the exposure overwrites mouse-face. */
32189 static bool
32190 expose_window_tree (struct window *w, XRectangle *r)
32192 struct frame *f = XFRAME (w->frame);
32193 bool mouse_face_overwritten_p = false;
32195 while (w && !FRAME_GARBAGED_P (f))
32197 mouse_face_overwritten_p
32198 |= (WINDOWP (w->contents)
32199 ? expose_window_tree (XWINDOW (w->contents), r)
32200 : expose_window (w, r));
32202 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32205 return mouse_face_overwritten_p;
32209 /* EXPORT:
32210 Redisplay an exposed area of frame F. X and Y are the upper-left
32211 corner of the exposed rectangle. W and H are width and height of
32212 the exposed area. All are pixel values. W or H zero means redraw
32213 the entire frame. */
32215 void
32216 expose_frame (struct frame *f, int x, int y, int w, int h)
32218 XRectangle r;
32219 bool mouse_face_overwritten_p = false;
32221 TRACE ((stderr, "expose_frame "));
32223 /* No need to redraw if frame will be redrawn soon. */
32224 if (FRAME_GARBAGED_P (f))
32226 TRACE ((stderr, " garbaged\n"));
32227 return;
32230 /* If basic faces haven't been realized yet, there is no point in
32231 trying to redraw anything. This can happen when we get an expose
32232 event while Emacs is starting, e.g. by moving another window. */
32233 if (FRAME_FACE_CACHE (f) == NULL
32234 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32236 TRACE ((stderr, " no faces\n"));
32237 return;
32240 if (w == 0 || h == 0)
32242 r.x = r.y = 0;
32243 r.width = FRAME_TEXT_WIDTH (f);
32244 r.height = FRAME_TEXT_HEIGHT (f);
32246 else
32248 r.x = x;
32249 r.y = y;
32250 r.width = w;
32251 r.height = h;
32254 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32255 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32257 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32258 if (WINDOWP (f->tool_bar_window))
32259 mouse_face_overwritten_p
32260 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32261 #endif
32263 #ifdef HAVE_X_WINDOWS
32264 #ifndef MSDOS
32265 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32266 if (WINDOWP (f->menu_bar_window))
32267 mouse_face_overwritten_p
32268 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32269 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32270 #endif
32271 #endif
32273 /* Some window managers support a focus-follows-mouse style with
32274 delayed raising of frames. Imagine a partially obscured frame,
32275 and moving the mouse into partially obscured mouse-face on that
32276 frame. The visible part of the mouse-face will be highlighted,
32277 then the WM raises the obscured frame. With at least one WM, KDE
32278 2.1, Emacs is not getting any event for the raising of the frame
32279 (even tried with SubstructureRedirectMask), only Expose events.
32280 These expose events will draw text normally, i.e. not
32281 highlighted. Which means we must redo the highlight here.
32282 Subsume it under ``we love X''. --gerd 2001-08-15 */
32283 /* Included in Windows version because Windows most likely does not
32284 do the right thing if any third party tool offers
32285 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32286 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32289 if (f == hlinfo->mouse_face_mouse_frame)
32291 int mouse_x = hlinfo->mouse_face_mouse_x;
32292 int mouse_y = hlinfo->mouse_face_mouse_y;
32293 clear_mouse_face (hlinfo);
32294 note_mouse_highlight (f, mouse_x, mouse_y);
32300 /* EXPORT:
32301 Determine the intersection of two rectangles R1 and R2. Return
32302 the intersection in *RESULT. Value is true if RESULT is not
32303 empty. */
32305 bool
32306 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32308 XRectangle *left, *right;
32309 XRectangle *upper, *lower;
32310 bool intersection_p = false;
32312 /* Rearrange so that R1 is the left-most rectangle. */
32313 if (r1->x < r2->x)
32314 left = r1, right = r2;
32315 else
32316 left = r2, right = r1;
32318 /* X0 of the intersection is right.x0, if this is inside R1,
32319 otherwise there is no intersection. */
32320 if (right->x <= left->x + left->width)
32322 result->x = right->x;
32324 /* The right end of the intersection is the minimum of
32325 the right ends of left and right. */
32326 result->width = (min (left->x + left->width, right->x + right->width)
32327 - result->x);
32329 /* Same game for Y. */
32330 if (r1->y < r2->y)
32331 upper = r1, lower = r2;
32332 else
32333 upper = r2, lower = r1;
32335 /* The upper end of the intersection is lower.y0, if this is inside
32336 of upper. Otherwise, there is no intersection. */
32337 if (lower->y <= upper->y + upper->height)
32339 result->y = lower->y;
32341 /* The lower end of the intersection is the minimum of the lower
32342 ends of upper and lower. */
32343 result->height = (min (lower->y + lower->height,
32344 upper->y + upper->height)
32345 - result->y);
32346 intersection_p = true;
32350 return intersection_p;
32353 #endif /* HAVE_WINDOW_SYSTEM */
32356 /***********************************************************************
32357 Initialization
32358 ***********************************************************************/
32360 void
32361 syms_of_xdisp (void)
32363 Vwith_echo_area_save_vector = Qnil;
32364 staticpro (&Vwith_echo_area_save_vector);
32366 Vmessage_stack = Qnil;
32367 staticpro (&Vmessage_stack);
32369 /* Non-nil means don't actually do any redisplay. */
32370 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32372 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32374 DEFVAR_BOOL("inhibit-message", inhibit_message,
32375 doc: /* Non-nil means calls to `message' are not displayed.
32376 They are still logged to the *Messages* buffer. */);
32377 inhibit_message = 0;
32379 message_dolog_marker1 = Fmake_marker ();
32380 staticpro (&message_dolog_marker1);
32381 message_dolog_marker2 = Fmake_marker ();
32382 staticpro (&message_dolog_marker2);
32383 message_dolog_marker3 = Fmake_marker ();
32384 staticpro (&message_dolog_marker3);
32386 defsubr (&Sset_buffer_redisplay);
32387 #ifdef GLYPH_DEBUG
32388 defsubr (&Sdump_frame_glyph_matrix);
32389 defsubr (&Sdump_glyph_matrix);
32390 defsubr (&Sdump_glyph_row);
32391 defsubr (&Sdump_tool_bar_row);
32392 defsubr (&Strace_redisplay);
32393 defsubr (&Strace_to_stderr);
32394 #endif
32395 #ifdef HAVE_WINDOW_SYSTEM
32396 defsubr (&Stool_bar_height);
32397 defsubr (&Slookup_image_map);
32398 #endif
32399 defsubr (&Sline_pixel_height);
32400 defsubr (&Sformat_mode_line);
32401 defsubr (&Sinvisible_p);
32402 defsubr (&Scurrent_bidi_paragraph_direction);
32403 defsubr (&Swindow_text_pixel_size);
32404 defsubr (&Smove_point_visually);
32405 defsubr (&Sbidi_find_overridden_directionality);
32407 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32408 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32409 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32410 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32411 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32412 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32413 DEFSYM (Qeval, "eval");
32414 DEFSYM (QCdata, ":data");
32416 /* Names of text properties relevant for redisplay. */
32417 DEFSYM (Qdisplay, "display");
32418 DEFSYM (Qspace_width, "space-width");
32419 DEFSYM (Qraise, "raise");
32420 DEFSYM (Qslice, "slice");
32421 DEFSYM (Qspace, "space");
32422 DEFSYM (Qmargin, "margin");
32423 DEFSYM (Qpointer, "pointer");
32424 DEFSYM (Qleft_margin, "left-margin");
32425 DEFSYM (Qright_margin, "right-margin");
32426 DEFSYM (Qcenter, "center");
32427 DEFSYM (Qline_height, "line-height");
32428 DEFSYM (QCalign_to, ":align-to");
32429 DEFSYM (QCrelative_width, ":relative-width");
32430 DEFSYM (QCrelative_height, ":relative-height");
32431 DEFSYM (QCeval, ":eval");
32432 DEFSYM (QCpropertize, ":propertize");
32433 DEFSYM (QCfile, ":file");
32434 DEFSYM (Qfontified, "fontified");
32435 DEFSYM (Qfontification_functions, "fontification-functions");
32437 /* Name of the symbol which disables Lisp evaluation in 'display'
32438 properties. This is used by enriched.el. */
32439 DEFSYM (Qdisable_eval, "disable-eval");
32441 /* Name of the face used to highlight trailing whitespace. */
32442 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32444 /* Names of the faces used to display line numbers. */
32445 DEFSYM (Qline_number, "line-number");
32446 DEFSYM (Qline_number_current_line, "line-number-current-line");
32447 /* Name of a text property which disables line-number display. */
32448 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32450 /* Name and number of the face used to highlight escape glyphs. */
32451 DEFSYM (Qescape_glyph, "escape-glyph");
32453 /* Name and number of the face used to highlight non-breaking
32454 spaces/hyphens. */
32455 DEFSYM (Qnobreak_space, "nobreak-space");
32456 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32458 /* The symbol 'image' which is the car of the lists used to represent
32459 images in Lisp. Also a tool bar style. */
32460 DEFSYM (Qimage, "image");
32462 /* Tool bar styles. */
32463 DEFSYM (Qtext, "text");
32464 DEFSYM (Qboth, "both");
32465 DEFSYM (Qboth_horiz, "both-horiz");
32466 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32468 /* The image map types. */
32469 DEFSYM (QCmap, ":map");
32470 DEFSYM (QCpointer, ":pointer");
32471 DEFSYM (Qrect, "rect");
32472 DEFSYM (Qcircle, "circle");
32473 DEFSYM (Qpoly, "poly");
32475 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32477 DEFSYM (Qgrow_only, "grow-only");
32478 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32479 DEFSYM (Qposition, "position");
32480 DEFSYM (Qbuffer_position, "buffer-position");
32481 DEFSYM (Qobject, "object");
32483 /* Cursor shapes. */
32484 DEFSYM (Qbar, "bar");
32485 DEFSYM (Qhbar, "hbar");
32486 DEFSYM (Qbox, "box");
32487 DEFSYM (Qhollow, "hollow");
32489 /* Pointer shapes. */
32490 DEFSYM (Qhand, "hand");
32491 DEFSYM (Qarrow, "arrow");
32492 /* also Qtext */
32494 DEFSYM (Qdragging, "dragging");
32496 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32498 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32499 staticpro (&list_of_error);
32501 /* Values of those variables at last redisplay are stored as
32502 properties on 'overlay-arrow-position' symbol. However, if
32503 Voverlay_arrow_position is a marker, last-arrow-position is its
32504 numerical position. */
32505 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32506 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32508 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32509 properties on a symbol in overlay-arrow-variable-list. */
32510 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32511 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32513 echo_buffer[0] = echo_buffer[1] = Qnil;
32514 staticpro (&echo_buffer[0]);
32515 staticpro (&echo_buffer[1]);
32517 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32518 staticpro (&echo_area_buffer[0]);
32519 staticpro (&echo_area_buffer[1]);
32521 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32522 staticpro (&Vmessages_buffer_name);
32524 mode_line_proptrans_alist = Qnil;
32525 staticpro (&mode_line_proptrans_alist);
32526 mode_line_string_list = Qnil;
32527 staticpro (&mode_line_string_list);
32528 mode_line_string_face = Qnil;
32529 staticpro (&mode_line_string_face);
32530 mode_line_string_face_prop = Qnil;
32531 staticpro (&mode_line_string_face_prop);
32532 Vmode_line_unwind_vector = Qnil;
32533 staticpro (&Vmode_line_unwind_vector);
32535 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32537 help_echo_string = Qnil;
32538 staticpro (&help_echo_string);
32539 help_echo_object = Qnil;
32540 staticpro (&help_echo_object);
32541 help_echo_window = Qnil;
32542 staticpro (&help_echo_window);
32543 previous_help_echo_string = Qnil;
32544 staticpro (&previous_help_echo_string);
32545 help_echo_pos = -1;
32547 DEFSYM (Qright_to_left, "right-to-left");
32548 DEFSYM (Qleft_to_right, "left-to-right");
32549 defsubr (&Sbidi_resolved_levels);
32551 #ifdef HAVE_WINDOW_SYSTEM
32552 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32553 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32554 For example, if a block cursor is over a tab, it will be drawn as
32555 wide as that tab on the display. */);
32556 x_stretch_cursor_p = 0;
32557 #endif
32559 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32560 doc: /* Non-nil means highlight trailing whitespace.
32561 The face used for trailing whitespace is `trailing-whitespace'. */);
32562 Vshow_trailing_whitespace = Qnil;
32564 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32565 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32566 If the value is t, Emacs highlights non-ASCII chars which have the
32567 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32568 or `nobreak-hyphen' face respectively.
32570 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32571 U+2011 (non-breaking hyphen) are affected.
32573 Any other non-nil value means to display these characters as an escape
32574 glyph followed by an ordinary space or hyphen.
32576 A value of nil means no special handling of these characters. */);
32577 Vnobreak_char_display = Qt;
32579 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32580 doc: /* The pointer shape to show in void text areas.
32581 A value of nil means to show the text pointer. Other options are
32582 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32583 `hourglass'. */);
32584 Vvoid_text_area_pointer = Qarrow;
32586 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32587 doc: /* Non-nil means don't actually do any redisplay.
32588 This is used for internal purposes. */);
32589 Vinhibit_redisplay = Qnil;
32591 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32592 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32593 Vglobal_mode_string = Qnil;
32595 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32596 doc: /* Marker for where to display an arrow on top of the buffer text.
32597 This must be the beginning of a line in order to work.
32598 See also `overlay-arrow-string'. */);
32599 Voverlay_arrow_position = Qnil;
32601 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32602 doc: /* String to display as an arrow in non-window frames.
32603 See also `overlay-arrow-position'. */);
32604 Voverlay_arrow_string = build_pure_c_string ("=>");
32606 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32607 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32608 The symbols on this list are examined during redisplay to determine
32609 where to display overlay arrows. */);
32610 Voverlay_arrow_variable_list
32611 = list1 (intern_c_string ("overlay-arrow-position"));
32613 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32614 doc: /* The number of lines to try scrolling a window by when point moves out.
32615 If that fails to bring point back on frame, point is centered instead.
32616 If this is zero, point is always centered after it moves off frame.
32617 If you want scrolling to always be a line at a time, you should set
32618 `scroll-conservatively' to a large value rather than set this to 1. */);
32620 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32621 doc: /* Scroll up to this many lines, to bring point back on screen.
32622 If point moves off-screen, redisplay will scroll by up to
32623 `scroll-conservatively' lines in order to bring point just barely
32624 onto the screen again. If that cannot be done, then redisplay
32625 recenters point as usual.
32627 If the value is greater than 100, redisplay will never recenter point,
32628 but will always scroll just enough text to bring point into view, even
32629 if you move far away.
32631 A value of zero means always recenter point if it moves off screen. */);
32632 scroll_conservatively = 0;
32634 DEFVAR_INT ("scroll-margin", scroll_margin,
32635 doc: /* Number of lines of margin at the top and bottom of a window.
32636 Trigger automatic scrolling whenever point gets within this many lines
32637 of the top or bottom of the window (see info node `Auto Scrolling'). */);
32638 scroll_margin = 0;
32640 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32641 doc: /* Maximum effective value of `scroll-margin'.
32642 Given as a fraction of the current window's lines. The value should
32643 be a floating point number between 0.0 and 0.5. The effective maximum
32644 is limited to (/ (1- window-lines) 2). Non-float values for this
32645 variable are ignored and the default 0.25 is used instead. */);
32646 Vmaximum_scroll_margin = make_float (0.25);
32648 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32649 doc: /* Pixels per inch value for non-window system displays.
32650 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32651 Vdisplay_pixels_per_inch = make_float (72.0);
32653 #ifdef GLYPH_DEBUG
32654 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32655 #endif
32657 DEFVAR_LISP ("truncate-partial-width-windows",
32658 Vtruncate_partial_width_windows,
32659 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32660 For an integer value, truncate lines in each window narrower than the
32661 full frame width, provided the total window width in column units is less
32662 than that integer; otherwise, respect the value of `truncate-lines'.
32663 The total width of the window is as returned by `window-total-width', it
32664 includes the fringes, the continuation and truncation glyphs, the
32665 display margins (if any), and the scroll bar
32667 For any other non-nil value, truncate lines in all windows that do
32668 not span the full frame width.
32670 A value of nil means to respect the value of `truncate-lines'.
32672 If `word-wrap' is enabled, you might want to reduce this. */);
32673 Vtruncate_partial_width_windows = make_number (50);
32675 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32676 doc: /* Maximum buffer size for which line number should be displayed.
32677 If the buffer is bigger than this, the line number does not appear
32678 in the mode line. A value of nil means no limit. */);
32679 Vline_number_display_limit = Qnil;
32681 DEFVAR_INT ("line-number-display-limit-width",
32682 line_number_display_limit_width,
32683 doc: /* Maximum line width (in characters) for line number display.
32684 If the average length of the lines near point is bigger than this, then the
32685 line number may be omitted from the mode line. */);
32686 line_number_display_limit_width = 200;
32688 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32689 doc: /* Non-nil means highlight region even in nonselected windows. */);
32690 highlight_nonselected_windows = false;
32692 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32693 doc: /* Non-nil if more than one frame is visible on this display.
32694 Minibuffer-only frames don't count, but iconified frames do.
32695 This variable is not guaranteed to be accurate except while processing
32696 `frame-title-format' and `icon-title-format'. */);
32698 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32699 doc: /* Template for displaying the title bar of visible frames.
32700 \(Assuming the window manager supports this feature.)
32702 This variable has the same structure as `mode-line-format', except that
32703 the %c, %C, and %l constructs are ignored. It is used only on frames for
32704 which no explicit name has been set (see `modify-frame-parameters'). */);
32706 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32707 doc: /* Template for displaying the title bar of an iconified frame.
32708 \(Assuming the window manager supports this feature.)
32709 This variable has the same structure as `mode-line-format' (which see),
32710 and is used only on frames for which no explicit name has been set
32711 \(see `modify-frame-parameters'). */);
32712 Vicon_title_format
32713 = Vframe_title_format
32714 = listn (CONSTYPE_PURE, 3,
32715 intern_c_string ("multiple-frames"),
32716 build_pure_c_string ("%b"),
32717 listn (CONSTYPE_PURE, 4,
32718 empty_unibyte_string,
32719 intern_c_string ("invocation-name"),
32720 build_pure_c_string ("@"),
32721 intern_c_string ("system-name")));
32723 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32724 doc: /* Maximum number of lines to keep in the message log buffer.
32725 If nil, disable message logging. If t, log messages but don't truncate
32726 the buffer when it becomes large. */);
32727 Vmessage_log_max = make_number (1000);
32729 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32730 doc: /* List of functions to call before redisplaying a window with scrolling.
32731 Each function is called with two arguments, the window and its new
32732 display-start position.
32733 These functions are called whenever the `window-start' marker is modified,
32734 either to point into another buffer (e.g. via `set-window-buffer') or another
32735 place in the same buffer.
32736 When each function is called, the `window-start' marker of its window
32737 argument has been already set to the new value, and the buffer which that
32738 window will display is set to be the current buffer.
32739 Note that the value of `window-end' is not valid when these functions are
32740 called.
32742 Warning: Do not use this feature to alter the way the window
32743 is scrolled. It is not designed for that, and such use probably won't
32744 work. */);
32745 Vwindow_scroll_functions = Qnil;
32747 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32748 doc: /* Functions called when redisplay of a window reaches the end trigger.
32749 Each function is called with two arguments, the window and the end trigger value.
32750 See `set-window-redisplay-end-trigger'. */);
32751 Vredisplay_end_trigger_functions = Qnil;
32753 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32754 doc: /* Non-nil means autoselect window with mouse pointer.
32755 If nil, do not autoselect windows.
32756 A positive number means delay autoselection by that many seconds: a
32757 window is autoselected only after the mouse has remained in that
32758 window for the duration of the delay.
32759 A negative number has a similar effect, but causes windows to be
32760 autoselected only after the mouse has stopped moving. (Because of
32761 the way Emacs compares mouse events, you will occasionally wait twice
32762 that time before the window gets selected.)
32763 Any other value means to autoselect window instantaneously when the
32764 mouse pointer enters it.
32766 Autoselection selects the minibuffer only if it is active, and never
32767 unselects the minibuffer if it is active.
32769 When customizing this variable make sure that the actual value of
32770 `focus-follows-mouse' matches the behavior of your window manager. */);
32771 Vmouse_autoselect_window = Qnil;
32773 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32774 doc: /* Non-nil means automatically resize tool-bars.
32775 This dynamically changes the tool-bar's height to the minimum height
32776 that is needed to make all tool-bar items visible.
32777 If value is `grow-only', the tool-bar's height is only increased
32778 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32779 Vauto_resize_tool_bars = Qt;
32781 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32782 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32783 auto_raise_tool_bar_buttons_p = true;
32785 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32786 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32787 make_cursor_line_fully_visible_p = true;
32789 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32790 doc: /* Border below tool-bar in pixels.
32791 If an integer, use it as the height of the border.
32792 If it is one of `internal-border-width' or `border-width', use the
32793 value of the corresponding frame parameter.
32794 Otherwise, no border is added below the tool-bar. */);
32795 Vtool_bar_border = Qinternal_border_width;
32797 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32798 doc: /* Margin around tool-bar buttons in pixels.
32799 If an integer, use that for both horizontal and vertical margins.
32800 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32801 HORZ specifying the horizontal margin, and VERT specifying the
32802 vertical margin. */);
32803 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32805 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32806 doc: /* Relief thickness of tool-bar buttons. */);
32807 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32809 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32810 doc: /* Tool bar style to use.
32811 It can be one of
32812 image - show images only
32813 text - show text only
32814 both - show both, text below image
32815 both-horiz - show text to the right of the image
32816 text-image-horiz - show text to the left of the image
32817 any other - use system default or image if no system default.
32819 This variable only affects the GTK+ toolkit version of Emacs. */);
32820 Vtool_bar_style = Qnil;
32822 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32823 doc: /* Maximum number of characters a label can have to be shown.
32824 The tool bar style must also show labels for this to have any effect, see
32825 `tool-bar-style'. */);
32826 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32828 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32829 doc: /* List of functions to call to fontify regions of text.
32830 Each function is called with one argument POS. Functions must
32831 fontify a region starting at POS in the current buffer, and give
32832 fontified regions the property `fontified'. */);
32833 Vfontification_functions = Qnil;
32834 Fmake_variable_buffer_local (Qfontification_functions);
32836 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32837 unibyte_display_via_language_environment,
32838 doc: /* Non-nil means display unibyte text according to language environment.
32839 Specifically, this means that raw bytes in the range 160-255 decimal
32840 are displayed by converting them to the equivalent multibyte characters
32841 according to the current language environment. As a result, they are
32842 displayed according to the current fontset.
32844 Note that this variable affects only how these bytes are displayed,
32845 but does not change the fact they are interpreted as raw bytes. */);
32846 unibyte_display_via_language_environment = false;
32848 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32849 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32850 If a float, it specifies a fraction of the mini-window frame's height.
32851 If an integer, it specifies a number of lines. */);
32852 Vmax_mini_window_height = make_float (0.25);
32854 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32855 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32856 A value of nil means don't automatically resize mini-windows.
32857 A value of t means resize them to fit the text displayed in them.
32858 A value of `grow-only', the default, means let mini-windows grow only;
32859 they return to their normal size when the minibuffer is closed, or the
32860 echo area becomes empty. */);
32861 /* Contrary to the doc string, we initialize this to nil, so that
32862 loading loadup.el won't try to resize windows before loading
32863 window.el, where some functions we need to call for this live.
32864 We assign the 'grow-only' value right after loading window.el
32865 during loadup. */
32866 Vresize_mini_windows = Qnil;
32868 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32869 doc: /* Alist specifying how to blink the cursor off.
32870 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32871 `cursor-type' frame-parameter or variable equals ON-STATE,
32872 comparing using `equal', Emacs uses OFF-STATE to specify
32873 how to blink it off. ON-STATE and OFF-STATE are values for
32874 the `cursor-type' frame parameter.
32876 If a frame's ON-STATE has no entry in this list,
32877 the frame's other specifications determine how to blink the cursor off. */);
32878 Vblink_cursor_alist = Qnil;
32880 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32881 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32882 The value `current-line' means the line displaying point in each window
32883 is automatically scrolled horizontally to make point visible.
32884 Any other non-nil value means all the lines in a window are automatically
32885 scrolled horizontally to make point visible. */);
32886 automatic_hscrolling = Qt;
32887 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32888 DEFSYM (Qcurrent_line, "current-line");
32890 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32891 doc: /* How many columns away from the window edge point is allowed to get
32892 before automatic hscrolling will horizontally scroll the window. */);
32893 hscroll_margin = 5;
32895 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32896 doc: /* How many columns to scroll the window when point gets too close to the edge.
32897 When point is less than `hscroll-margin' columns from the window
32898 edge, automatic hscrolling will scroll the window by the amount of columns
32899 determined by this variable. If its value is a positive integer, scroll that
32900 many columns. If it's a positive floating-point number, it specifies the
32901 fraction of the window's width to scroll. If it's nil or zero, point will be
32902 centered horizontally after the scroll. Any other value, including negative
32903 numbers, are treated as if the value were zero.
32905 Automatic hscrolling always moves point outside the scroll margin, so if
32906 point was more than scroll step columns inside the margin, the window will
32907 scroll more than the value given by the scroll step.
32909 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32910 and `scroll-right' overrides this variable's effect. */);
32911 Vhscroll_step = make_number (0);
32913 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32914 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32915 Bind this around calls to `message' to let it take effect. */);
32916 message_truncate_lines = false;
32918 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32919 doc: /* Normal hook run to update the menu bar definitions.
32920 Redisplay runs this hook before it redisplays the menu bar.
32921 This is used to update menus such as Buffers, whose contents depend on
32922 various data. */);
32923 Vmenu_bar_update_hook = Qnil;
32925 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32926 doc: /* Frame for which we are updating a menu.
32927 The enable predicate for a menu binding should check this variable. */);
32928 Vmenu_updating_frame = Qnil;
32930 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32931 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32932 inhibit_menubar_update = false;
32934 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32935 doc: /* Prefix prepended to all continuation lines at display time.
32936 The value may be a string, an image, or a stretch-glyph; it is
32937 interpreted in the same way as the value of a `display' text property.
32939 This variable is overridden by any `wrap-prefix' text or overlay
32940 property.
32942 To add a prefix to non-continuation lines, use `line-prefix'. */);
32943 Vwrap_prefix = Qnil;
32944 DEFSYM (Qwrap_prefix, "wrap-prefix");
32945 Fmake_variable_buffer_local (Qwrap_prefix);
32947 DEFVAR_LISP ("line-prefix", Vline_prefix,
32948 doc: /* Prefix prepended to all non-continuation lines at display time.
32949 The value may be a string, an image, or a stretch-glyph; it is
32950 interpreted in the same way as the value of a `display' text property.
32952 This variable is overridden by any `line-prefix' text or overlay
32953 property.
32955 To add a prefix to continuation lines, use `wrap-prefix'. */);
32956 Vline_prefix = Qnil;
32957 DEFSYM (Qline_prefix, "line-prefix");
32958 Fmake_variable_buffer_local (Qline_prefix);
32960 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32961 doc: /* Non-nil means display line numbers.
32962 If the value is t, display the absolute number of each line of a buffer
32963 shown in a window. Absolute line numbers count from the beginning of
32964 the current narrowing, or from buffer beginning. If the value is
32965 `relative', display for each line not containing the window's point its
32966 relative number instead, i.e. the number of the line relative to the
32967 line showing the window's point.
32969 In either case, line numbers are displayed at the beginning of each
32970 non-continuation line that displays buffer text, i.e. after each newline
32971 character that comes from the buffer. The value `visual' is like
32972 `relative' but counts screen lines instead of buffer lines. In practice
32973 this means that continuation lines count as well when calculating the
32974 relative number of a line.
32976 Lisp programs can disable display of a line number of a particular
32977 buffer line by putting the `display-line-numbers-disable' text property
32978 or overlay property on the first visible character of that line. */);
32979 Vdisplay_line_numbers = Qnil;
32980 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32981 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32982 DEFSYM (Qrelative, "relative");
32983 DEFSYM (Qvisual, "visual");
32985 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32986 doc: /* Minimum width of space reserved for line number display.
32987 A positive number means reserve that many columns for line numbers,
32988 even if the actual number needs less space.
32989 The default value of nil means compute the space dynamically.
32990 Any other value is treated as nil. */);
32991 Vdisplay_line_numbers_width = Qnil;
32992 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32993 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32995 DEFVAR_LISP ("display-line-numbers-current-absolute",
32996 Vdisplay_line_numbers_current_absolute,
32997 doc: /* Non-nil means display absolute number of current line.
32998 This variable has effect only when `display-line-numbers' is
32999 either `relative' or `visual'. */);
33000 Vdisplay_line_numbers_current_absolute = Qt;
33002 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
33003 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
33004 display_line_numbers_widen = false;
33005 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
33006 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
33008 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
33009 doc: /* Non-nil means don't eval Lisp during redisplay. */);
33010 inhibit_eval_during_redisplay = false;
33012 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
33013 doc: /* Non-nil means don't free realized faces. Internal use only. */);
33014 inhibit_free_realized_faces = false;
33016 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
33017 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
33018 Intended for use during debugging and for testing bidi display;
33019 see biditest.el in the test suite. */);
33020 inhibit_bidi_mirroring = false;
33022 #ifdef GLYPH_DEBUG
33023 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
33024 doc: /* Inhibit try_window_id display optimization. */);
33025 inhibit_try_window_id = false;
33027 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
33028 doc: /* Inhibit try_window_reusing display optimization. */);
33029 inhibit_try_window_reusing = false;
33031 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
33032 doc: /* Inhibit try_cursor_movement display optimization. */);
33033 inhibit_try_cursor_movement = false;
33034 #endif /* GLYPH_DEBUG */
33036 DEFVAR_INT ("overline-margin", overline_margin,
33037 doc: /* Space between overline and text, in pixels.
33038 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
33039 margin to the character height. */);
33040 overline_margin = 2;
33042 DEFVAR_INT ("underline-minimum-offset",
33043 underline_minimum_offset,
33044 doc: /* Minimum distance between baseline and underline.
33045 This can improve legibility of underlined text at small font sizes,
33046 particularly when using variable `x-use-underline-position-properties'
33047 with fonts that specify an UNDERLINE_POSITION relatively close to the
33048 baseline. The default value is 1. */);
33049 underline_minimum_offset = 1;
33050 DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
33052 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
33053 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
33054 This feature only works when on a window system that can change
33055 cursor shapes. */);
33056 display_hourglass_p = true;
33058 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
33059 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
33060 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
33062 #ifdef HAVE_WINDOW_SYSTEM
33063 hourglass_atimer = NULL;
33064 hourglass_shown_p = false;
33065 #endif /* HAVE_WINDOW_SYSTEM */
33067 /* Name of the face used to display glyphless characters. */
33068 DEFSYM (Qglyphless_char, "glyphless-char");
33070 /* Method symbols for Vglyphless_char_display. */
33071 DEFSYM (Qhex_code, "hex-code");
33072 DEFSYM (Qempty_box, "empty-box");
33073 DEFSYM (Qthin_space, "thin-space");
33074 DEFSYM (Qzero_width, "zero-width");
33076 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
33077 doc: /* Function run just before redisplay.
33078 It is called with one argument, which is the set of windows that are to
33079 be redisplayed. This set can be nil (meaning, only the selected window),
33080 or t (meaning all windows). */);
33081 Vpre_redisplay_function = intern ("ignore");
33083 /* Symbol for the purpose of Vglyphless_char_display. */
33084 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
33085 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
33087 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
33088 doc: /* Char-table defining glyphless characters.
33089 Each element, if non-nil, should be one of the following:
33090 an ASCII acronym string: display this string in a box
33091 `hex-code': display the hexadecimal code of a character in a box
33092 `empty-box': display as an empty box
33093 `thin-space': display as 1-pixel width space
33094 `zero-width': don't display
33095 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
33096 display method for graphical terminals and text terminals respectively.
33097 GRAPHICAL and TEXT should each have one of the values listed above.
33099 The char-table has one extra slot to control the display of a character for
33100 which no font is found. This slot only takes effect on graphical terminals.
33101 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
33102 `thin-space'. The default is `empty-box'.
33104 If a character has a non-nil entry in an active display table, the
33105 display table takes effect; in this case, Emacs does not consult
33106 `glyphless-char-display' at all. */);
33107 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33108 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33109 Qempty_box);
33111 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33112 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33113 Vdebug_on_message = Qnil;
33115 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33116 doc: /* */);
33117 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33119 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33120 doc: /* */);
33121 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33123 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33124 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33125 /* Initialize to t, since we need to disable reordering until
33126 loadup.el successfully loads charprop.el. */
33127 redisplay__inhibit_bidi = true;
33129 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33130 doc: /* Non-nil means display raw bytes in hexadecimal format.
33131 The default is to use octal format (\200) whereas hexadecimal (\x80)
33132 may be more familiar to users. */);
33133 display_raw_bytes_as_hex = false;
33138 /* Initialize this module when Emacs starts. */
33140 void
33141 init_xdisp (void)
33143 CHARPOS (this_line_start_pos) = 0;
33145 if (!noninteractive)
33147 struct window *m = XWINDOW (minibuf_window);
33148 Lisp_Object frame = m->frame;
33149 struct frame *f = XFRAME (frame);
33150 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33151 struct window *r = XWINDOW (root);
33152 int i;
33154 echo_area_window = minibuf_window;
33156 r->top_line = FRAME_TOP_MARGIN (f);
33157 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33158 r->total_cols = FRAME_COLS (f);
33159 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33160 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33161 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33163 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33164 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33165 m->total_cols = FRAME_COLS (f);
33166 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33167 m->total_lines = 1;
33168 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33170 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33171 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33172 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33174 /* The default ellipsis glyphs `...'. */
33175 for (i = 0; i < 3; ++i)
33176 default_invis_vector[i] = make_number ('.');
33180 /* Allocate the buffer for frame titles.
33181 Also used for `format-mode-line'. */
33182 int size = 100;
33183 mode_line_noprop_buf = xmalloc (size);
33184 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33185 mode_line_noprop_ptr = mode_line_noprop_buf;
33186 mode_line_target = MODE_LINE_DISPLAY;
33189 help_echo_showing_p = false;
33192 #ifdef HAVE_WINDOW_SYSTEM
33194 /* Platform-independent portion of hourglass implementation. */
33196 /* Timer function of hourglass_atimer. */
33198 static void
33199 show_hourglass (struct atimer *timer)
33201 /* The timer implementation will cancel this timer automatically
33202 after this function has run. Set hourglass_atimer to null
33203 so that we know the timer doesn't have to be canceled. */
33204 hourglass_atimer = NULL;
33206 if (!hourglass_shown_p)
33208 Lisp_Object tail, frame;
33210 block_input ();
33212 FOR_EACH_FRAME (tail, frame)
33214 struct frame *f = XFRAME (frame);
33216 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33217 && FRAME_RIF (f)->show_hourglass)
33218 FRAME_RIF (f)->show_hourglass (f);
33221 hourglass_shown_p = true;
33222 unblock_input ();
33226 /* Cancel a currently active hourglass timer, and start a new one. */
33228 void
33229 start_hourglass (void)
33231 struct timespec delay;
33233 cancel_hourglass ();
33235 if (INTEGERP (Vhourglass_delay)
33236 && XINT (Vhourglass_delay) > 0)
33237 delay = make_timespec (min (XINT (Vhourglass_delay),
33238 TYPE_MAXIMUM (time_t)),
33240 else if (FLOATP (Vhourglass_delay)
33241 && XFLOAT_DATA (Vhourglass_delay) > 0)
33242 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33243 else
33244 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33246 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33247 show_hourglass, NULL);
33250 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33251 shown. */
33253 void
33254 cancel_hourglass (void)
33256 if (hourglass_atimer)
33258 cancel_atimer (hourglass_atimer);
33259 hourglass_atimer = NULL;
33262 if (hourglass_shown_p)
33264 Lisp_Object tail, frame;
33266 block_input ();
33268 FOR_EACH_FRAME (tail, frame)
33270 struct frame *f = XFRAME (frame);
33272 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33273 && FRAME_RIF (f)->hide_hourglass)
33274 FRAME_RIF (f)->hide_hourglass (f);
33275 #ifdef HAVE_NTGUI
33276 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33277 else if (!FRAME_W32_P (f))
33278 w32_arrow_cursor ();
33279 #endif
33282 hourglass_shown_p = false;
33283 unblock_input ();
33287 #endif /* HAVE_WINDOW_SYSTEM */